ip_mroute.c revision 166972
1157873Simp/*-
2157873Simp * Copyright (c) 1989 Stephen Deering
3157873Simp * Copyright (c) 1992, 1993
4157873Simp *      The Regents of the University of California.  All rights reserved.
5157873Simp *
6157873Simp * This code is derived from software contributed to Berkeley by
7157873Simp * Stephen Deering of Stanford University.
8157873Simp *
9157873Simp * Redistribution and use in source and binary forms, with or without
10157873Simp * modification, are permitted provided that the following conditions
11157873Simp * are met:
12157873Simp * 1. Redistributions of source code must retain the above copyright
13157873Simp *    notice, this list of conditions and the following disclaimer.
14157873Simp * 2. Redistributions in binary form must reproduce the above copyright
15157873Simp *    notice, this list of conditions and the following disclaimer in the
16157873Simp *    documentation and/or other materials provided with the distribution.
17157873Simp * 4. Neither the name of the University nor the names of its contributors
18157873Simp *    may be used to endorse or promote products derived from this software
19157873Simp *    without specific prior written permission.
20157873Simp *
21157873Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22157873Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23157873Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24157873Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25157873Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26157873Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27157873Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28157873Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29157873Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30157873Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31157873Simp * SUCH DAMAGE.
32157873Simp *
33157873Simp *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
34157873Simp */
35157873Simp
36157873Simp/*
37157873Simp * IP multicast forwarding procedures
38157873Simp *
39157873Simp * Written by David Waitzman, BBN Labs, August 1988.
40157873Simp * Modified by Steve Deering, Stanford, February 1989.
41157873Simp * Modified by Mark J. Steiglitz, Stanford, May, 1991
42161190Simp * Modified by Van Jacobson, LBL, January 1993
43157873Simp * Modified by Ajit Thyagarajan, PARC, August 1993
44157873Simp * Modified by Bill Fenner, PARC, April 1995
45157873Simp * Modified by Ahmed Helmy, SGI, June 1996
46157873Simp * Modified by George Edmond Eddy (Rusty), ISI, February 1998
47157873Simp * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
48157873Simp * Modified by Hitoshi Asaeda, WIDE, August 2000
49157873Simp * Modified by Pavlin Radoslavov, ICSI, October 2002
50157873Simp *
51157873Simp * MROUTING Revision: 3.5
52157873Simp * and PIM-SMv2 and PIM-DM support, advanced API support,
53157873Simp * bandwidth metering and signaling
54157873Simp *
55157873Simp * $FreeBSD: head/sys/netinet/ip_mroute.c 166972 2007-02-25 14:22:03Z bms $
56157873Simp */
57161190Simp
58157873Simp#include "opt_inet.h"
59161190Simp#include "opt_inet6.h"
60157873Simp#include "opt_mac.h"
61161190Simp#include "opt_mrouting.h"
62157873Simp
63157873Simp#define _PIM_VT 1
64157873Simp
65157873Simp#include <sys/param.h>
66157873Simp#include <sys/kernel.h>
67161190Simp#include <sys/lock.h>
68157873Simp#include <sys/malloc.h>
69157873Simp#include <sys/mbuf.h>
70157873Simp#include <sys/module.h>
71157873Simp#include <sys/priv.h>
72157873Simp#include <sys/protosw.h>
73157873Simp#include <sys/signalvar.h>
74157873Simp#include <sys/socket.h>
75157873Simp#include <sys/socketvar.h>
76157873Simp#include <sys/sockio.h>
77157873Simp#include <sys/sx.h>
78157873Simp#include <sys/sysctl.h>
79157873Simp#include <sys/syslog.h>
80157873Simp#include <sys/systm.h>
81161190Simp#include <sys/time.h>
82161190Simp#include <net/if.h>
83161190Simp#include <net/netisr.h>
84161190Simp#include <net/route.h>
85157873Simp#include <netinet/in.h>
86157873Simp#include <netinet/igmp.h>
87157873Simp#include <netinet/in_systm.h>
88157873Simp#include <netinet/in_var.h>
89157873Simp#include <netinet/ip.h>
90157873Simp#include <netinet/ip_encap.h>
91157873Simp#include <netinet/ip_mroute.h>
92157873Simp#include <netinet/ip_var.h>
93157873Simp#include <netinet/ip_options.h>
94157873Simp#include <netinet/pim.h>
95157873Simp#include <netinet/pim_var.h>
96157873Simp#include <netinet/udp.h>
97157873Simp#ifdef INET6
98157873Simp#include <netinet/ip6.h>
99157873Simp#include <netinet6/in6_var.h>
100157873Simp#include <netinet6/ip6_mroute.h>
101157873Simp#include <netinet6/ip6_var.h>
102157873Simp#endif
103157873Simp#include <machine/in_cksum.h>
104157873Simp
105157873Simp#include <security/mac/mac_framework.h>
106157873Simp
107157873Simp/*
108157873Simp * Control debugging code for rsvp and multicast routing code.
109157873Simp * Can only set them with the debugger.
110157873Simp */
111157873Simpstatic u_int    rsvpdebug;		/* non-zero enables debugging	*/
112157873Simp
113157873Simpstatic u_int	mrtdebug;		/* any set of the flags below	*/
114157873Simp#define		DEBUG_MFC	0x02
115157873Simp#define		DEBUG_FORWARD	0x04
116157873Simp#define		DEBUG_EXPIRE	0x08
117157873Simp#define		DEBUG_XMIT	0x10
118157873Simp#define		DEBUG_PIM	0x20
119157873Simp
120157873Simp#define		VIFI_INVALID	((vifi_t) -1)
121157873Simp
122157873Simp#define M_HASCL(m)	((m)->m_flags & M_EXT)
123157873Simp
124157873Simpstatic MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
125157873Simp
126157873Simp/*
127157873Simp * Locking.  We use two locks: one for the virtual interface table and
128157873Simp * one for the forwarding table.  These locks may be nested in which case
129 * the VIF lock must always be taken first.  Note that each lock is used
130 * to cover not only the specific data structure but also related data
131 * structures.  It may be better to add more fine-grained locking later;
132 * it's not clear how performance-critical this code is.
133 *
134 * XXX: This module could particularly benefit from being cleaned
135 *      up to use the <sys/queue.h> macros.
136 *
137 */
138
139static struct mrtstat	mrtstat;
140SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
141    &mrtstat, mrtstat,
142    "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
143
144static struct mfc	*mfctable[MFCTBLSIZ];
145SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD,
146    &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]",
147    "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
148
149static struct mtx mfc_mtx;
150#define	MFC_LOCK()	mtx_lock(&mfc_mtx)
151#define	MFC_UNLOCK()	mtx_unlock(&mfc_mtx)
152#define	MFC_LOCK_ASSERT()	do {					\
153	mtx_assert(&mfc_mtx, MA_OWNED);					\
154	NET_ASSERT_GIANT();						\
155} while (0)
156#define	MFC_LOCK_INIT()	mtx_init(&mfc_mtx, "mroute mfc table", NULL, MTX_DEF)
157#define	MFC_LOCK_DESTROY()	mtx_destroy(&mfc_mtx)
158
159static struct vif	viftable[MAXVIFS];
160SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD,
161    &viftable, sizeof(viftable), "S,vif[MAXVIFS]",
162    "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
163
164static struct mtx vif_mtx;
165#define	VIF_LOCK()	mtx_lock(&vif_mtx)
166#define	VIF_UNLOCK()	mtx_unlock(&vif_mtx)
167#define	VIF_LOCK_ASSERT()	mtx_assert(&vif_mtx, MA_OWNED)
168#define	VIF_LOCK_INIT()	mtx_init(&vif_mtx, "mroute vif table", NULL, MTX_DEF)
169#define	VIF_LOCK_DESTROY()	mtx_destroy(&vif_mtx)
170
171static u_char		nexpire[MFCTBLSIZ];
172
173static eventhandler_tag if_detach_event_tag = NULL;
174
175static struct callout expire_upcalls_ch;
176
177#define		EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second		*/
178#define		UPCALL_EXPIRE	6		/* number of timeouts	*/
179
180#define ENCAP_TTL 64
181
182/*
183 * Bandwidth meter variables and constants
184 */
185static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
186/*
187 * Pending timeouts are stored in a hash table, the key being the
188 * expiration time. Periodically, the entries are analysed and processed.
189 */
190#define BW_METER_BUCKETS	1024
191static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
192static struct callout bw_meter_ch;
193#define BW_METER_PERIOD (hz)		/* periodical handling of bw meters */
194
195/*
196 * Pending upcalls are stored in a vector which is flushed when
197 * full, or periodically
198 */
199static struct bw_upcall	bw_upcalls[BW_UPCALLS_MAX];
200static u_int	bw_upcalls_n; /* # of pending upcalls */
201static struct callout bw_upcalls_ch;
202#define BW_UPCALLS_PERIOD (hz)		/* periodical flush of bw upcalls */
203
204static struct pimstat pimstat;
205
206SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
207SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD,
208    &pimstat, pimstat,
209    "PIM Statistics (struct pimstat, netinet/pim_var.h)");
210
211static u_long	pim_squelch_wholepkt = 0;
212SYSCTL_ULONG(_net_inet_pim, OID_AUTO, squelch_wholepkt, CTLFLAG_RW,
213    &pim_squelch_wholepkt, 0,
214    "Disable IGMP_WHOLEPKT notifications if rendezvous point is unspecified");
215
216extern  struct domain inetdomain;
217struct protosw in_pim_protosw = {
218	.pr_type =		SOCK_RAW,
219	.pr_domain =		&inetdomain,
220	.pr_protocol =		IPPROTO_PIM,
221	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
222	.pr_input =		pim_input,
223	.pr_output =		(pr_output_t*)rip_output,
224	.pr_ctloutput =		rip_ctloutput,
225	.pr_usrreqs =		&rip_usrreqs
226};
227static const struct encaptab *pim_encap_cookie;
228
229#ifdef INET6
230/* ip6_mroute.c glue */
231extern struct in6_protosw in6_pim_protosw;
232static const struct encaptab *pim6_encap_cookie;
233
234extern int X_ip6_mrouter_set(struct socket *, struct sockopt *);
235extern int X_ip6_mrouter_get(struct socket *, struct sockopt *);
236extern int X_ip6_mrouter_done(void);
237extern int X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
238extern int X_mrt6_ioctl(int, caddr_t);
239#endif
240
241static int pim_encapcheck(const struct mbuf *, int, int, void *);
242
243/*
244 * Note: the PIM Register encapsulation adds the following in front of a
245 * data packet:
246 *
247 * struct pim_encap_hdr {
248 *    struct ip ip;
249 *    struct pim_encap_pimhdr  pim;
250 * }
251 *
252 */
253
254struct pim_encap_pimhdr {
255	struct pim pim;
256	uint32_t   flags;
257};
258
259static struct ip pim_encap_iphdr = {
260#if BYTE_ORDER == LITTLE_ENDIAN
261	sizeof(struct ip) >> 2,
262	IPVERSION,
263#else
264	IPVERSION,
265	sizeof(struct ip) >> 2,
266#endif
267	0,			/* tos */
268	sizeof(struct ip),	/* total length */
269	0,			/* id */
270	0,			/* frag offset */
271	ENCAP_TTL,
272	IPPROTO_PIM,
273	0,			/* checksum */
274};
275
276static struct pim_encap_pimhdr pim_encap_pimhdr = {
277    {
278	PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
279	0,			/* reserved */
280	0,			/* checksum */
281    },
282    0				/* flags */
283};
284
285static struct ifnet multicast_register_if;
286static vifi_t reg_vif_num = VIFI_INVALID;
287
288/*
289 * Private variables.
290 */
291static vifi_t	   numvifs;
292
293static u_long	X_ip_mcast_src(int vifi);
294static int	X_ip_mforward(struct ip *ip, struct ifnet *ifp,
295			struct mbuf *m, struct ip_moptions *imo);
296static int	X_ip_mrouter_done(void);
297static int	X_ip_mrouter_get(struct socket *so, struct sockopt *m);
298static int	X_ip_mrouter_set(struct socket *so, struct sockopt *m);
299static int	X_legal_vif_num(int vif);
300static int	X_mrt_ioctl(int cmd, caddr_t data);
301
302static int get_sg_cnt(struct sioc_sg_req *);
303static int get_vif_cnt(struct sioc_vif_req *);
304static void if_detached_event(void *arg __unused, struct ifnet *);
305static int ip_mrouter_init(struct socket *, int);
306static int add_vif(struct vifctl *);
307static int del_vif_locked(vifi_t);
308static int del_vif(vifi_t);
309static int add_mfc(struct mfcctl2 *);
310static int del_mfc(struct mfcctl2 *);
311static int set_api_config(uint32_t *); /* chose API capabilities */
312static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
313static int set_assert(int);
314static void expire_upcalls(void *);
315static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
316static void phyint_send(struct ip *, struct vif *, struct mbuf *);
317static void send_packet(struct vif *, struct mbuf *);
318
319/*
320 * Bandwidth monitoring
321 */
322static void free_bw_list(struct bw_meter *list);
323static int add_bw_upcall(struct bw_upcall *);
324static int del_bw_upcall(struct bw_upcall *);
325static void bw_meter_receive_packet(struct bw_meter *x, int plen,
326		struct timeval *nowp);
327static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp);
328static void bw_upcalls_send(void);
329static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp);
330static void unschedule_bw_meter(struct bw_meter *x);
331static void bw_meter_process(void);
332static void expire_bw_upcalls_send(void *);
333static void expire_bw_meter_process(void *);
334
335static int pim_register_send(struct ip *, struct vif *,
336		struct mbuf *, struct mfc *);
337static int pim_register_send_rp(struct ip *, struct vif *,
338		struct mbuf *, struct mfc *);
339static int pim_register_send_upcall(struct ip *, struct vif *,
340		struct mbuf *, struct mfc *);
341static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
342
343/*
344 * whether or not special PIM assert processing is enabled.
345 */
346static int pim_assert;
347/*
348 * Rate limit for assert notification messages, in usec
349 */
350#define ASSERT_MSG_TIME		3000000
351
352/*
353 * Kernel multicast routing API capabilities and setup.
354 * If more API capabilities are added to the kernel, they should be
355 * recorded in `mrt_api_support'.
356 */
357static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
358					 MRT_MFC_FLAGS_BORDER_VIF |
359					 MRT_MFC_RP |
360					 MRT_MFC_BW_UPCALL);
361static uint32_t mrt_api_config = 0;
362
363/*
364 * Hash function for a source, group entry
365 */
366#define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
367			((g) >> 20) ^ ((g) >> 10) ^ (g))
368
369/*
370 * Find a route for a given origin IP address and Multicast group address
371 * Type of service parameter to be added in the future!!!
372 * Statistics are updated by the caller if needed
373 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
374 */
375static struct mfc *
376mfc_find(in_addr_t o, in_addr_t g)
377{
378    struct mfc *rt;
379
380    MFC_LOCK_ASSERT();
381
382    for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next)
383	if ((rt->mfc_origin.s_addr == o) &&
384		(rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL))
385	    break;
386    return rt;
387}
388
389/*
390 * Macros to compute elapsed time efficiently
391 * Borrowed from Van Jacobson's scheduling code
392 */
393#define TV_DELTA(a, b, delta) {					\
394	int xxs;						\
395	delta = (a).tv_usec - (b).tv_usec;			\
396	if ((xxs = (a).tv_sec - (b).tv_sec)) {			\
397		switch (xxs) {					\
398		case 2:						\
399		      delta += 1000000;				\
400		      /* FALLTHROUGH */				\
401		case 1:						\
402		      delta += 1000000;				\
403		      break;					\
404		default:					\
405		      delta += (1000000 * xxs);			\
406		}						\
407	}							\
408}
409
410#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
411	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
412
413/*
414 * Handle MRT setsockopt commands to modify the multicast routing tables.
415 */
416static int
417X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
418{
419    int	error, optval;
420    vifi_t	vifi;
421    struct	vifctl vifc;
422    struct	mfcctl2 mfc;
423    struct	bw_upcall bw_upcall;
424    uint32_t	i;
425
426    if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
427	return EPERM;
428
429    error = 0;
430    switch (sopt->sopt_name) {
431    case MRT_INIT:
432	error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
433	if (error)
434	    break;
435	error = ip_mrouter_init(so, optval);
436	break;
437
438    case MRT_DONE:
439	error = ip_mrouter_done();
440	break;
441
442    case MRT_ADD_VIF:
443	error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
444	if (error)
445	    break;
446	error = add_vif(&vifc);
447	break;
448
449    case MRT_DEL_VIF:
450	error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
451	if (error)
452	    break;
453	error = del_vif(vifi);
454	break;
455
456    case MRT_ADD_MFC:
457    case MRT_DEL_MFC:
458	/*
459	 * select data size depending on API version.
460	 */
461	if (sopt->sopt_name == MRT_ADD_MFC &&
462		mrt_api_config & MRT_API_FLAGS_ALL) {
463	    error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
464				sizeof(struct mfcctl2));
465	} else {
466	    error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
467				sizeof(struct mfcctl));
468	    bzero((caddr_t)&mfc + sizeof(struct mfcctl),
469			sizeof(mfc) - sizeof(struct mfcctl));
470	}
471	if (error)
472	    break;
473	if (sopt->sopt_name == MRT_ADD_MFC)
474	    error = add_mfc(&mfc);
475	else
476	    error = del_mfc(&mfc);
477	break;
478
479    case MRT_ASSERT:
480	error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
481	if (error)
482	    break;
483	set_assert(optval);
484	break;
485
486    case MRT_API_CONFIG:
487	error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
488	if (!error)
489	    error = set_api_config(&i);
490	if (!error)
491	    error = sooptcopyout(sopt, &i, sizeof i);
492	break;
493
494    case MRT_ADD_BW_UPCALL:
495    case MRT_DEL_BW_UPCALL:
496	error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
497				sizeof bw_upcall);
498	if (error)
499	    break;
500	if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
501	    error = add_bw_upcall(&bw_upcall);
502	else
503	    error = del_bw_upcall(&bw_upcall);
504	break;
505
506    default:
507	error = EOPNOTSUPP;
508	break;
509    }
510    return error;
511}
512
513/*
514 * Handle MRT getsockopt commands
515 */
516static int
517X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
518{
519    int error;
520    static int version = 0x0305; /* !!! why is this here? XXX */
521
522    switch (sopt->sopt_name) {
523    case MRT_VERSION:
524	error = sooptcopyout(sopt, &version, sizeof version);
525	break;
526
527    case MRT_ASSERT:
528	error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
529	break;
530
531    case MRT_API_SUPPORT:
532	error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support);
533	break;
534
535    case MRT_API_CONFIG:
536	error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config);
537	break;
538
539    default:
540	error = EOPNOTSUPP;
541	break;
542    }
543    return error;
544}
545
546/*
547 * Handle ioctl commands to obtain information from the cache
548 */
549static int
550X_mrt_ioctl(int cmd, caddr_t data)
551{
552    int error = 0;
553
554    /*
555     * Currently the only function calling this ioctl routine is rtioctl().
556     * Typically, only root can create the raw socket in order to execute
557     * this ioctl method, however the request might be coming from a prison
558     */
559    error = priv_check(curthread, PRIV_NETINET_MROUTE);
560    if (error)
561	return (error);
562    switch (cmd) {
563    case (SIOCGETVIFCNT):
564	error = get_vif_cnt((struct sioc_vif_req *)data);
565	break;
566
567    case (SIOCGETSGCNT):
568	error = get_sg_cnt((struct sioc_sg_req *)data);
569	break;
570
571    default:
572	error = EINVAL;
573	break;
574    }
575    return error;
576}
577
578/*
579 * returns the packet, byte, rpf-failure count for the source group provided
580 */
581static int
582get_sg_cnt(struct sioc_sg_req *req)
583{
584    struct mfc *rt;
585
586    MFC_LOCK();
587    rt = mfc_find(req->src.s_addr, req->grp.s_addr);
588    if (rt == NULL) {
589	MFC_UNLOCK();
590	req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
591	return EADDRNOTAVAIL;
592    }
593    req->pktcnt = rt->mfc_pkt_cnt;
594    req->bytecnt = rt->mfc_byte_cnt;
595    req->wrong_if = rt->mfc_wrong_if;
596    MFC_UNLOCK();
597    return 0;
598}
599
600/*
601 * returns the input and output packet and byte counts on the vif provided
602 */
603static int
604get_vif_cnt(struct sioc_vif_req *req)
605{
606    vifi_t vifi = req->vifi;
607
608    VIF_LOCK();
609    if (vifi >= numvifs) {
610	VIF_UNLOCK();
611	return EINVAL;
612    }
613
614    req->icount = viftable[vifi].v_pkt_in;
615    req->ocount = viftable[vifi].v_pkt_out;
616    req->ibytes = viftable[vifi].v_bytes_in;
617    req->obytes = viftable[vifi].v_bytes_out;
618    VIF_UNLOCK();
619
620    return 0;
621}
622
623static void
624ip_mrouter_reset(void)
625{
626    bzero((caddr_t)mfctable, sizeof(mfctable));
627    bzero((caddr_t)nexpire, sizeof(nexpire));
628
629    pim_assert = 0;
630    mrt_api_config = 0;
631
632    callout_init(&expire_upcalls_ch, NET_CALLOUT_MPSAFE);
633
634    bw_upcalls_n = 0;
635    bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers));
636    callout_init(&bw_upcalls_ch, NET_CALLOUT_MPSAFE);
637    callout_init(&bw_meter_ch, NET_CALLOUT_MPSAFE);
638}
639
640static struct mtx mrouter_mtx;
641
642static void
643if_detached_event(void *arg __unused, struct ifnet *ifp)
644{
645    vifi_t vifi;
646    int i;
647    struct mfc *mfc;
648    struct mfc *nmfc;
649    struct mfc **ppmfc;	/* Pointer to previous node's next-pointer */
650    struct rtdetq *pq;
651    struct rtdetq *npq;
652
653    mtx_lock(&mrouter_mtx);
654    if (ip_mrouter == NULL) {
655	mtx_unlock(&mrouter_mtx);
656    }
657
658    /*
659     * Tear down multicast forwarder state associated with this ifnet.
660     * 1. Walk the vif list, matching vifs against this ifnet.
661     * 2. Walk the multicast forwarding cache (mfc) looking for
662     *    inner matches with this vif's index.
663     * 3. Free any pending mbufs for this mfc.
664     * 4. Free the associated mfc entry and state associated with this vif.
665     *    Be very careful about unlinking from a singly-linked list whose
666     *    "head node" is a pointer in a simple array.
667     * 5. Free vif state. This should disable ALLMULTI on the interface.
668     */
669    VIF_LOCK();
670    MFC_LOCK();
671    for (vifi = 0; vifi < numvifs; vifi++) {
672	if (viftable[vifi].v_ifp != ifp)
673		continue;
674	for (i = 0; i < MFCTBLSIZ; i++) {
675	    ppmfc = &mfctable[i];
676	    for (mfc = mfctable[i]; mfc != NULL; ) {
677		nmfc = mfc->mfc_next;
678		if (mfc->mfc_parent == vifi) {
679		    for (pq = mfc->mfc_stall; pq != NULL; ) {
680			npq = pq->next;
681			m_freem(pq->m);
682			free(pq, M_MRTABLE);
683			pq = npq;
684		    }
685		    free_bw_list(mfc->mfc_bw_meter);
686		    free(mfc, M_MRTABLE);
687		    *ppmfc = nmfc;
688		} else {
689		    ppmfc = &mfc->mfc_next;
690		}
691		mfc = nmfc;
692	    }
693	}
694	del_vif_locked(vifi);
695    }
696    MFC_UNLOCK();
697    VIF_UNLOCK();
698
699    mtx_unlock(&mrouter_mtx);
700}
701
702/*
703 * Enable multicast routing
704 */
705static int
706ip_mrouter_init(struct socket *so, int version)
707{
708    if (mrtdebug)
709	log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
710	    so->so_type, so->so_proto->pr_protocol);
711
712    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
713	return EOPNOTSUPP;
714
715    if (version != 1)
716	return ENOPROTOOPT;
717
718    mtx_lock(&mrouter_mtx);
719
720    if (ip_mrouter != NULL) {
721	mtx_unlock(&mrouter_mtx);
722	return EADDRINUSE;
723    }
724
725    if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
726        if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
727    if (if_detach_event_tag == NULL) {
728	mtx_unlock(&mrouter_mtx);
729	return (ENOMEM);
730    }
731
732    callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
733
734    callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
735	expire_bw_upcalls_send, NULL);
736    callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
737
738    ip_mrouter = so;
739
740    mtx_unlock(&mrouter_mtx);
741
742    if (mrtdebug)
743	log(LOG_DEBUG, "ip_mrouter_init\n");
744
745    return 0;
746}
747
748/*
749 * Disable multicast routing
750 */
751static int
752X_ip_mrouter_done(void)
753{
754    vifi_t vifi;
755    int i;
756    struct ifnet *ifp;
757    struct ifreq ifr;
758    struct mfc *rt;
759    struct rtdetq *rte;
760
761    mtx_lock(&mrouter_mtx);
762
763    if (ip_mrouter == NULL) {
764	mtx_unlock(&mrouter_mtx);
765	return EINVAL;
766    }
767
768    /*
769     * Detach/disable hooks to the reset of the system.
770     */
771    ip_mrouter = NULL;
772    mrt_api_config = 0;
773
774    VIF_LOCK();
775    /*
776     * For each phyint in use, disable promiscuous reception of all IP
777     * multicasts.
778     */
779    for (vifi = 0; vifi < numvifs; vifi++) {
780	if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
781		!(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
782	    struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
783
784	    so->sin_len = sizeof(struct sockaddr_in);
785	    so->sin_family = AF_INET;
786	    so->sin_addr.s_addr = INADDR_ANY;
787	    ifp = viftable[vifi].v_ifp;
788	    if_allmulti(ifp, 0);
789	}
790    }
791    bzero((caddr_t)viftable, sizeof(viftable));
792    numvifs = 0;
793    pim_assert = 0;
794    VIF_UNLOCK();
795    EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
796
797    /*
798     * Free all multicast forwarding cache entries.
799     */
800    callout_stop(&expire_upcalls_ch);
801    callout_stop(&bw_upcalls_ch);
802    callout_stop(&bw_meter_ch);
803
804    MFC_LOCK();
805    for (i = 0; i < MFCTBLSIZ; i++) {
806	for (rt = mfctable[i]; rt != NULL; ) {
807	    struct mfc *nr = rt->mfc_next;
808
809	    for (rte = rt->mfc_stall; rte != NULL; ) {
810		struct rtdetq *n = rte->next;
811
812		m_freem(rte->m);
813		free(rte, M_MRTABLE);
814		rte = n;
815	    }
816	    free_bw_list(rt->mfc_bw_meter);
817	    free(rt, M_MRTABLE);
818	    rt = nr;
819	}
820    }
821    bzero((caddr_t)mfctable, sizeof(mfctable));
822    bzero((caddr_t)nexpire, sizeof(nexpire));
823    bw_upcalls_n = 0;
824    bzero(bw_meter_timers, sizeof(bw_meter_timers));
825    MFC_UNLOCK();
826
827    reg_vif_num = VIFI_INVALID;
828
829    mtx_unlock(&mrouter_mtx);
830
831    if (mrtdebug)
832	log(LOG_DEBUG, "ip_mrouter_done\n");
833
834    return 0;
835}
836
837/*
838 * Set PIM assert processing global
839 */
840static int
841set_assert(int i)
842{
843    if ((i != 1) && (i != 0))
844	return EINVAL;
845
846    pim_assert = i;
847
848    return 0;
849}
850
851/*
852 * Configure API capabilities
853 */
854int
855set_api_config(uint32_t *apival)
856{
857    int i;
858
859    /*
860     * We can set the API capabilities only if it is the first operation
861     * after MRT_INIT. I.e.:
862     *  - there are no vifs installed
863     *  - pim_assert is not enabled
864     *  - the MFC table is empty
865     */
866    if (numvifs > 0) {
867	*apival = 0;
868	return EPERM;
869    }
870    if (pim_assert) {
871	*apival = 0;
872	return EPERM;
873    }
874    for (i = 0; i < MFCTBLSIZ; i++) {
875	if (mfctable[i] != NULL) {
876	    *apival = 0;
877	    return EPERM;
878	}
879    }
880
881    mrt_api_config = *apival & mrt_api_support;
882    *apival = mrt_api_config;
883
884    return 0;
885}
886
887/*
888 * Add a vif to the vif table
889 */
890static int
891add_vif(struct vifctl *vifcp)
892{
893    struct vif *vifp = viftable + vifcp->vifc_vifi;
894    struct sockaddr_in sin = {sizeof sin, AF_INET};
895    struct ifaddr *ifa;
896    struct ifnet *ifp;
897    int error;
898
899    VIF_LOCK();
900    if (vifcp->vifc_vifi >= MAXVIFS) {
901	VIF_UNLOCK();
902	return EINVAL;
903    }
904    /* rate limiting is no longer supported by this code */
905    if (vifcp->vifc_rate_limit != 0) {
906	log(LOG_ERR, "rate limiting is no longer supported\n");
907	VIF_UNLOCK();
908	return EINVAL;
909    }
910    if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
911	VIF_UNLOCK();
912	return EADDRINUSE;
913    }
914    if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
915	VIF_UNLOCK();
916	return EADDRNOTAVAIL;
917    }
918
919    /* Find the interface with an address in AF_INET family */
920    if (vifcp->vifc_flags & VIFF_REGISTER) {
921	/*
922	 * XXX: Because VIFF_REGISTER does not really need a valid
923	 * local interface (e.g. it could be 127.0.0.2), we don't
924	 * check its address.
925	 */
926	ifp = NULL;
927    } else {
928	sin.sin_addr = vifcp->vifc_lcl_addr;
929	ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
930	if (ifa == NULL) {
931	    VIF_UNLOCK();
932	    return EADDRNOTAVAIL;
933	}
934	ifp = ifa->ifa_ifp;
935    }
936
937    if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
938	log(LOG_ERR, "tunnels are no longer supported\n");
939	VIF_UNLOCK();
940	return EOPNOTSUPP;
941    } else if (vifcp->vifc_flags & VIFF_REGISTER) {
942	ifp = &multicast_register_if;
943	if (mrtdebug)
944	    log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
945		    (void *)&multicast_register_if);
946	if (reg_vif_num == VIFI_INVALID) {
947	    if_initname(&multicast_register_if, "register_vif", 0);
948	    multicast_register_if.if_flags = IFF_LOOPBACK;
949	    reg_vif_num = vifcp->vifc_vifi;
950	}
951    } else {		/* Make sure the interface supports multicast */
952	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
953	    VIF_UNLOCK();
954	    return EOPNOTSUPP;
955	}
956
957	/* Enable promiscuous reception of all IP multicasts from the if */
958	error = if_allmulti(ifp, 1);
959	if (error) {
960	    VIF_UNLOCK();
961	    return error;
962	}
963    }
964
965    vifp->v_flags     = vifcp->vifc_flags;
966    vifp->v_threshold = vifcp->vifc_threshold;
967    vifp->v_lcl_addr  = vifcp->vifc_lcl_addr;
968    vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
969    vifp->v_ifp       = ifp;
970    vifp->v_rsvp_on   = 0;
971    vifp->v_rsvpd     = NULL;
972    /* initialize per vif pkt counters */
973    vifp->v_pkt_in    = 0;
974    vifp->v_pkt_out   = 0;
975    vifp->v_bytes_in  = 0;
976    vifp->v_bytes_out = 0;
977    bzero(&vifp->v_route, sizeof(vifp->v_route));
978
979    /* Adjust numvifs up if the vifi is higher than numvifs */
980    if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
981
982    VIF_UNLOCK();
983
984    if (mrtdebug)
985	log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x\n",
986	    vifcp->vifc_vifi,
987	    (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
988	    (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
989	    (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
990	    vifcp->vifc_threshold);
991
992    return 0;
993}
994
995/*
996 * Delete a vif from the vif table
997 */
998static int
999del_vif_locked(vifi_t vifi)
1000{
1001    struct vif *vifp;
1002
1003    VIF_LOCK_ASSERT();
1004
1005    if (vifi >= numvifs) {
1006	return EINVAL;
1007    }
1008    vifp = &viftable[vifi];
1009    if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1010	return EADDRNOTAVAIL;
1011    }
1012
1013    if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1014	if_allmulti(vifp->v_ifp, 0);
1015
1016    if (vifp->v_flags & VIFF_REGISTER)
1017	reg_vif_num = VIFI_INVALID;
1018
1019    bzero((caddr_t)vifp, sizeof (*vifp));
1020
1021    if (mrtdebug)
1022	log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1023
1024    /* Adjust numvifs down */
1025    for (vifi = numvifs; vifi > 0; vifi--)
1026	if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1027	    break;
1028    numvifs = vifi;
1029
1030    return 0;
1031}
1032
1033static int
1034del_vif(vifi_t vifi)
1035{
1036    int cc;
1037
1038    VIF_LOCK();
1039    cc = del_vif_locked(vifi);
1040    VIF_UNLOCK();
1041
1042    return cc;
1043}
1044
1045/*
1046 * update an mfc entry without resetting counters and S,G addresses.
1047 */
1048static void
1049update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1050{
1051    int i;
1052
1053    rt->mfc_parent = mfccp->mfcc_parent;
1054    for (i = 0; i < numvifs; i++) {
1055	rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1056	rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1057	    MRT_MFC_FLAGS_ALL;
1058    }
1059    /* set the RP address */
1060    if (mrt_api_config & MRT_MFC_RP)
1061	rt->mfc_rp = mfccp->mfcc_rp;
1062    else
1063	rt->mfc_rp.s_addr = INADDR_ANY;
1064}
1065
1066/*
1067 * fully initialize an mfc entry from the parameter.
1068 */
1069static void
1070init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1071{
1072    rt->mfc_origin     = mfccp->mfcc_origin;
1073    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
1074
1075    update_mfc_params(rt, mfccp);
1076
1077    /* initialize pkt counters per src-grp */
1078    rt->mfc_pkt_cnt    = 0;
1079    rt->mfc_byte_cnt   = 0;
1080    rt->mfc_wrong_if   = 0;
1081    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1082}
1083
1084
1085/*
1086 * Add an mfc entry
1087 */
1088static int
1089add_mfc(struct mfcctl2 *mfccp)
1090{
1091    struct mfc *rt;
1092    u_long hash;
1093    struct rtdetq *rte;
1094    u_short nstl;
1095
1096    VIF_LOCK();
1097    MFC_LOCK();
1098
1099    rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1100
1101    /* If an entry already exists, just update the fields */
1102    if (rt) {
1103	if (mrtdebug & DEBUG_MFC)
1104	    log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1105		(u_long)ntohl(mfccp->mfcc_origin.s_addr),
1106		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1107		mfccp->mfcc_parent);
1108
1109	update_mfc_params(rt, mfccp);
1110	MFC_UNLOCK();
1111	VIF_UNLOCK();
1112	return 0;
1113    }
1114
1115    /*
1116     * Find the entry for which the upcall was made and update
1117     */
1118    hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1119    for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1120
1121	if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1122		(rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1123		(rt->mfc_stall != NULL)) {
1124
1125	    if (nstl++)
1126		log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1127		    "multiple kernel entries",
1128		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1129		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1130		    mfccp->mfcc_parent, (void *)rt->mfc_stall);
1131
1132	    if (mrtdebug & DEBUG_MFC)
1133		log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1134		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1135		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1136		    mfccp->mfcc_parent, (void *)rt->mfc_stall);
1137
1138	    init_mfc_params(rt, mfccp);
1139
1140	    rt->mfc_expire = 0;	/* Don't clean this guy up */
1141	    nexpire[hash]--;
1142
1143	    /* free packets Qed at the end of this entry */
1144	    for (rte = rt->mfc_stall; rte != NULL; ) {
1145		struct rtdetq *n = rte->next;
1146
1147		ip_mdq(rte->m, rte->ifp, rt, -1);
1148		m_freem(rte->m);
1149		free(rte, M_MRTABLE);
1150		rte = n;
1151	    }
1152	    rt->mfc_stall = NULL;
1153	}
1154    }
1155
1156    /*
1157     * It is possible that an entry is being inserted without an upcall
1158     */
1159    if (nstl == 0) {
1160	if (mrtdebug & DEBUG_MFC)
1161	    log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1162		hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1163		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1164		mfccp->mfcc_parent);
1165
1166	for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1167	    if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1168		    (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1169		init_mfc_params(rt, mfccp);
1170		if (rt->mfc_expire)
1171		    nexpire[hash]--;
1172		rt->mfc_expire = 0;
1173		break; /* XXX */
1174	    }
1175	}
1176	if (rt == NULL) {		/* no upcall, so make a new entry */
1177	    rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1178	    if (rt == NULL) {
1179		MFC_UNLOCK();
1180		VIF_UNLOCK();
1181		return ENOBUFS;
1182	    }
1183
1184	    init_mfc_params(rt, mfccp);
1185	    rt->mfc_expire     = 0;
1186	    rt->mfc_stall      = NULL;
1187
1188	    rt->mfc_bw_meter = NULL;
1189	    /* insert new entry at head of hash chain */
1190	    rt->mfc_next = mfctable[hash];
1191	    mfctable[hash] = rt;
1192	}
1193    }
1194    MFC_UNLOCK();
1195    VIF_UNLOCK();
1196    return 0;
1197}
1198
1199/*
1200 * Delete an mfc entry
1201 */
1202static int
1203del_mfc(struct mfcctl2 *mfccp)
1204{
1205    struct in_addr	origin;
1206    struct in_addr	mcastgrp;
1207    struct mfc		*rt;
1208    struct mfc		**nptr;
1209    u_long		hash;
1210    struct bw_meter	*list;
1211
1212    origin = mfccp->mfcc_origin;
1213    mcastgrp = mfccp->mfcc_mcastgrp;
1214
1215    if (mrtdebug & DEBUG_MFC)
1216	log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1217	    (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1218
1219    MFC_LOCK();
1220
1221    hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1222    for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1223	if (origin.s_addr == rt->mfc_origin.s_addr &&
1224		mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1225		rt->mfc_stall == NULL)
1226	    break;
1227    if (rt == NULL) {
1228	MFC_UNLOCK();
1229	return EADDRNOTAVAIL;
1230    }
1231
1232    *nptr = rt->mfc_next;
1233
1234    /*
1235     * free the bw_meter entries
1236     */
1237    list = rt->mfc_bw_meter;
1238    rt->mfc_bw_meter = NULL;
1239
1240    free(rt, M_MRTABLE);
1241
1242    free_bw_list(list);
1243
1244    MFC_UNLOCK();
1245
1246    return 0;
1247}
1248
1249/*
1250 * Send a message to the routing daemon on the multicast routing socket
1251 */
1252static int
1253socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1254{
1255    if (s) {
1256	SOCKBUF_LOCK(&s->so_rcv);
1257	if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1258	    NULL) != 0) {
1259	    sorwakeup_locked(s);
1260	    return 0;
1261	}
1262	SOCKBUF_UNLOCK(&s->so_rcv);
1263    }
1264    m_freem(mm);
1265    return -1;
1266}
1267
1268/*
1269 * IP multicast forwarding function. This function assumes that the packet
1270 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1271 * pointed to by "ifp", and the packet is to be relayed to other networks
1272 * that have members of the packet's destination IP multicast group.
1273 *
1274 * The packet is returned unscathed to the caller, unless it is
1275 * erroneous, in which case a non-zero return value tells the caller to
1276 * discard it.
1277 */
1278
1279#define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
1280
1281static int
1282X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1283    struct ip_moptions *imo)
1284{
1285    struct mfc *rt;
1286    int error;
1287    vifi_t vifi;
1288
1289    if (mrtdebug & DEBUG_FORWARD)
1290	log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1291	    (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1292	    (void *)ifp);
1293
1294    if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1295		((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1296	/*
1297	 * Packet arrived via a physical interface or
1298	 * an encapsulated tunnel or a register_vif.
1299	 */
1300    } else {
1301	/*
1302	 * Packet arrived through a source-route tunnel.
1303	 * Source-route tunnels are no longer supported.
1304	 */
1305	static int last_log;
1306	if (last_log != time_uptime) {
1307	    last_log = time_uptime;
1308	    log(LOG_ERR,
1309		"ip_mforward: received source-routed packet from %lx\n",
1310		(u_long)ntohl(ip->ip_src.s_addr));
1311	}
1312	return 1;
1313    }
1314
1315    VIF_LOCK();
1316    MFC_LOCK();
1317    if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1318	if (ip->ip_ttl < MAXTTL)
1319	    ip->ip_ttl++;	/* compensate for -1 in *_send routines */
1320	if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1321	    struct vif *vifp = viftable + vifi;
1322
1323	    printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1324		(long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1325		vifi,
1326		(vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1327		vifp->v_ifp->if_xname);
1328	}
1329	error = ip_mdq(m, ifp, NULL, vifi);
1330	MFC_UNLOCK();
1331	VIF_UNLOCK();
1332	return error;
1333    }
1334    if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1335	printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1336	    (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1337	if (!imo)
1338	    printf("In fact, no options were specified at all\n");
1339    }
1340
1341    /*
1342     * Don't forward a packet with time-to-live of zero or one,
1343     * or a packet destined to a local-only group.
1344     */
1345    if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1346	MFC_UNLOCK();
1347	VIF_UNLOCK();
1348	return 0;
1349    }
1350
1351    /*
1352     * Determine forwarding vifs from the forwarding cache table
1353     */
1354    ++mrtstat.mrts_mfc_lookups;
1355    rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1356
1357    /* Entry exists, so forward if necessary */
1358    if (rt != NULL) {
1359	error = ip_mdq(m, ifp, rt, -1);
1360	MFC_UNLOCK();
1361	VIF_UNLOCK();
1362	return error;
1363    } else {
1364	/*
1365	 * If we don't have a route for packet's origin,
1366	 * Make a copy of the packet & send message to routing daemon
1367	 */
1368
1369	struct mbuf *mb0;
1370	struct rtdetq *rte;
1371	u_long hash;
1372	int hlen = ip->ip_hl << 2;
1373
1374	++mrtstat.mrts_mfc_misses;
1375
1376	mrtstat.mrts_no_route++;
1377	if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1378	    log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1379		(u_long)ntohl(ip->ip_src.s_addr),
1380		(u_long)ntohl(ip->ip_dst.s_addr));
1381
1382	/*
1383	 * Allocate mbufs early so that we don't do extra work if we are
1384	 * just going to fail anyway.  Make sure to pullup the header so
1385	 * that other people can't step on it.
1386	 */
1387	rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1388	if (rte == NULL) {
1389	    MFC_UNLOCK();
1390	    VIF_UNLOCK();
1391	    return ENOBUFS;
1392	}
1393	mb0 = m_copypacket(m, M_DONTWAIT);
1394	if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1395	    mb0 = m_pullup(mb0, hlen);
1396	if (mb0 == NULL) {
1397	    free(rte, M_MRTABLE);
1398	    MFC_UNLOCK();
1399	    VIF_UNLOCK();
1400	    return ENOBUFS;
1401	}
1402
1403	/* is there an upcall waiting for this flow ? */
1404	hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1405	for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1406	    if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1407		    (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1408		    (rt->mfc_stall != NULL))
1409		break;
1410	}
1411
1412	if (rt == NULL) {
1413	    int i;
1414	    struct igmpmsg *im;
1415	    struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1416	    struct mbuf *mm;
1417
1418	    /*
1419	     * Locate the vifi for the incoming interface for this packet.
1420	     * If none found, drop packet.
1421	     */
1422	    for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1423		;
1424	    if (vifi >= numvifs)	/* vif not found, drop packet */
1425		goto non_fatal;
1426
1427	    /* no upcall, so make a new entry */
1428	    rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1429	    if (rt == NULL)
1430		goto fail;
1431	    /* Make a copy of the header to send to the user level process */
1432	    mm = m_copy(mb0, 0, hlen);
1433	    if (mm == NULL)
1434		goto fail1;
1435
1436	    /*
1437	     * Send message to routing daemon to install
1438	     * a route into the kernel table
1439	     */
1440
1441	    im = mtod(mm, struct igmpmsg *);
1442	    im->im_msgtype = IGMPMSG_NOCACHE;
1443	    im->im_mbz = 0;
1444	    im->im_vif = vifi;
1445
1446	    mrtstat.mrts_upcalls++;
1447
1448	    k_igmpsrc.sin_addr = ip->ip_src;
1449	    if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1450		log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1451		++mrtstat.mrts_upq_sockfull;
1452fail1:
1453		free(rt, M_MRTABLE);
1454fail:
1455		free(rte, M_MRTABLE);
1456		m_freem(mb0);
1457		MFC_UNLOCK();
1458		VIF_UNLOCK();
1459		return ENOBUFS;
1460	    }
1461
1462	    /* insert new entry at head of hash chain */
1463	    rt->mfc_origin.s_addr     = ip->ip_src.s_addr;
1464	    rt->mfc_mcastgrp.s_addr   = ip->ip_dst.s_addr;
1465	    rt->mfc_expire	      = UPCALL_EXPIRE;
1466	    nexpire[hash]++;
1467	    for (i = 0; i < numvifs; i++) {
1468		rt->mfc_ttls[i] = 0;
1469		rt->mfc_flags[i] = 0;
1470	    }
1471	    rt->mfc_parent = -1;
1472
1473	    rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1474
1475	    rt->mfc_bw_meter = NULL;
1476
1477	    /* link into table */
1478	    rt->mfc_next   = mfctable[hash];
1479	    mfctable[hash] = rt;
1480	    rt->mfc_stall = rte;
1481
1482	} else {
1483	    /* determine if q has overflowed */
1484	    int npkts = 0;
1485	    struct rtdetq **p;
1486
1487	    /*
1488	     * XXX ouch! we need to append to the list, but we
1489	     * only have a pointer to the front, so we have to
1490	     * scan the entire list every time.
1491	     */
1492	    for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1493		npkts++;
1494
1495	    if (npkts > MAX_UPQ) {
1496		mrtstat.mrts_upq_ovflw++;
1497non_fatal:
1498		free(rte, M_MRTABLE);
1499		m_freem(mb0);
1500		MFC_UNLOCK();
1501		VIF_UNLOCK();
1502		return 0;
1503	    }
1504
1505	    /* Add this entry to the end of the queue */
1506	    *p = rte;
1507	}
1508
1509	rte->m			= mb0;
1510	rte->ifp		= ifp;
1511	rte->next		= NULL;
1512
1513	MFC_UNLOCK();
1514	VIF_UNLOCK();
1515
1516	return 0;
1517    }
1518}
1519
1520/*
1521 * Clean up the cache entry if upcall is not serviced
1522 */
1523static void
1524expire_upcalls(void *unused)
1525{
1526    struct rtdetq *rte;
1527    struct mfc *mfc, **nptr;
1528    int i;
1529
1530    MFC_LOCK();
1531    for (i = 0; i < MFCTBLSIZ; i++) {
1532	if (nexpire[i] == 0)
1533	    continue;
1534	nptr = &mfctable[i];
1535	for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1536	    /*
1537	     * Skip real cache entries
1538	     * Make sure it wasn't marked to not expire (shouldn't happen)
1539	     * If it expires now
1540	     */
1541	    if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1542		    --mfc->mfc_expire == 0) {
1543		if (mrtdebug & DEBUG_EXPIRE)
1544		    log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1545			(u_long)ntohl(mfc->mfc_origin.s_addr),
1546			(u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1547		/*
1548		 * drop all the packets
1549		 * free the mbuf with the pkt, if, timing info
1550		 */
1551		for (rte = mfc->mfc_stall; rte; ) {
1552		    struct rtdetq *n = rte->next;
1553
1554		    m_freem(rte->m);
1555		    free(rte, M_MRTABLE);
1556		    rte = n;
1557		}
1558		++mrtstat.mrts_cache_cleanups;
1559		nexpire[i]--;
1560
1561		/*
1562		 * free the bw_meter entries
1563		 */
1564		while (mfc->mfc_bw_meter != NULL) {
1565		    struct bw_meter *x = mfc->mfc_bw_meter;
1566
1567		    mfc->mfc_bw_meter = x->bm_mfc_next;
1568		    free(x, M_BWMETER);
1569		}
1570
1571		*nptr = mfc->mfc_next;
1572		free(mfc, M_MRTABLE);
1573	    } else {
1574		nptr = &mfc->mfc_next;
1575	    }
1576	}
1577    }
1578    MFC_UNLOCK();
1579
1580    callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1581}
1582
1583/*
1584 * Packet forwarding routine once entry in the cache is made
1585 */
1586static int
1587ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1588{
1589    struct ip  *ip = mtod(m, struct ip *);
1590    vifi_t vifi;
1591    int plen = ip->ip_len;
1592
1593    VIF_LOCK_ASSERT();
1594
1595    /*
1596     * If xmt_vif is not -1, send on only the requested vif.
1597     *
1598     * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1599     */
1600    if (xmt_vif < numvifs) {
1601	if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1602		pim_register_send(ip, viftable + xmt_vif, m, rt);
1603	else
1604		phyint_send(ip, viftable + xmt_vif, m);
1605	return 1;
1606    }
1607
1608    /*
1609     * Don't forward if it didn't arrive from the parent vif for its origin.
1610     */
1611    vifi = rt->mfc_parent;
1612    if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1613	/* came in the wrong interface */
1614	if (mrtdebug & DEBUG_FORWARD)
1615	    log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1616		(void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1617	++mrtstat.mrts_wrong_if;
1618	++rt->mfc_wrong_if;
1619	/*
1620	 * If we are doing PIM assert processing, send a message
1621	 * to the routing daemon.
1622	 *
1623	 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1624	 * can complete the SPT switch, regardless of the type
1625	 * of the iif (broadcast media, GRE tunnel, etc).
1626	 */
1627	if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1628	    struct timeval now;
1629	    u_long delta;
1630
1631	    if (ifp == &multicast_register_if)
1632		pimstat.pims_rcv_registers_wrongiif++;
1633
1634	    /* Get vifi for the incoming packet */
1635	    for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1636		;
1637	    if (vifi >= numvifs)
1638		return 0;	/* The iif is not found: ignore the packet. */
1639
1640	    if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1641		return 0;	/* WRONGVIF disabled: ignore the packet */
1642
1643	    GET_TIME(now);
1644
1645	    TV_DELTA(now, rt->mfc_last_assert, delta);
1646
1647	    if (delta > ASSERT_MSG_TIME) {
1648		struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1649		struct igmpmsg *im;
1650		int hlen = ip->ip_hl << 2;
1651		struct mbuf *mm = m_copy(m, 0, hlen);
1652
1653		if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1654		    mm = m_pullup(mm, hlen);
1655		if (mm == NULL)
1656		    return ENOBUFS;
1657
1658		rt->mfc_last_assert = now;
1659
1660		im = mtod(mm, struct igmpmsg *);
1661		im->im_msgtype	= IGMPMSG_WRONGVIF;
1662		im->im_mbz		= 0;
1663		im->im_vif		= vifi;
1664
1665		mrtstat.mrts_upcalls++;
1666
1667		k_igmpsrc.sin_addr = im->im_src;
1668		if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1669		    log(LOG_WARNING,
1670			"ip_mforward: ip_mrouter socket queue full\n");
1671		    ++mrtstat.mrts_upq_sockfull;
1672		    return ENOBUFS;
1673		}
1674	    }
1675	}
1676	return 0;
1677    }
1678
1679    /* If I sourced this packet, it counts as output, else it was input. */
1680    if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1681	viftable[vifi].v_pkt_out++;
1682	viftable[vifi].v_bytes_out += plen;
1683    } else {
1684	viftable[vifi].v_pkt_in++;
1685	viftable[vifi].v_bytes_in += plen;
1686    }
1687    rt->mfc_pkt_cnt++;
1688    rt->mfc_byte_cnt += plen;
1689
1690    /*
1691     * For each vif, decide if a copy of the packet should be forwarded.
1692     * Forward if:
1693     *		- the ttl exceeds the vif's threshold
1694     *		- there are group members downstream on interface
1695     */
1696    for (vifi = 0; vifi < numvifs; vifi++)
1697	if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1698	    viftable[vifi].v_pkt_out++;
1699	    viftable[vifi].v_bytes_out += plen;
1700	    if (viftable[vifi].v_flags & VIFF_REGISTER)
1701		pim_register_send(ip, viftable + vifi, m, rt);
1702	    else
1703		phyint_send(ip, viftable + vifi, m);
1704	}
1705
1706    /*
1707     * Perform upcall-related bw measuring.
1708     */
1709    if (rt->mfc_bw_meter != NULL) {
1710	struct bw_meter *x;
1711	struct timeval now;
1712
1713	GET_TIME(now);
1714	MFC_LOCK_ASSERT();
1715	for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1716	    bw_meter_receive_packet(x, plen, &now);
1717    }
1718
1719    return 0;
1720}
1721
1722/*
1723 * check if a vif number is legal/ok. This is used by ip_output.
1724 */
1725static int
1726X_legal_vif_num(int vif)
1727{
1728    /* XXX unlocked, matter? */
1729    return (vif >= 0 && vif < numvifs);
1730}
1731
1732/*
1733 * Return the local address used by this vif
1734 */
1735static u_long
1736X_ip_mcast_src(int vifi)
1737{
1738    /* XXX unlocked, matter? */
1739    if (vifi >= 0 && vifi < numvifs)
1740	return viftable[vifi].v_lcl_addr.s_addr;
1741    else
1742	return INADDR_ANY;
1743}
1744
1745static void
1746phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1747{
1748    struct mbuf *mb_copy;
1749    int hlen = ip->ip_hl << 2;
1750
1751    VIF_LOCK_ASSERT();
1752
1753    /*
1754     * Make a new reference to the packet; make sure that
1755     * the IP header is actually copied, not just referenced,
1756     * so that ip_output() only scribbles on the copy.
1757     */
1758    mb_copy = m_copypacket(m, M_DONTWAIT);
1759    if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1760	mb_copy = m_pullup(mb_copy, hlen);
1761    if (mb_copy == NULL)
1762	return;
1763
1764    send_packet(vifp, mb_copy);
1765}
1766
1767static void
1768send_packet(struct vif *vifp, struct mbuf *m)
1769{
1770	struct ip_moptions imo;
1771	struct in_multi *imm[2];
1772	int error;
1773
1774	VIF_LOCK_ASSERT();
1775
1776	imo.imo_multicast_ifp  = vifp->v_ifp;
1777	imo.imo_multicast_ttl  = mtod(m, struct ip *)->ip_ttl - 1;
1778	imo.imo_multicast_loop = 1;
1779	imo.imo_multicast_vif  = -1;
1780	imo.imo_num_memberships = 0;
1781	imo.imo_max_memberships = 2;
1782	imo.imo_membership  = &imm[0];
1783
1784	/*
1785	 * Re-entrancy should not be a problem here, because
1786	 * the packets that we send out and are looped back at us
1787	 * should get rejected because they appear to come from
1788	 * the loopback interface, thus preventing looping.
1789	 */
1790	error = ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, &imo, NULL);
1791	if (mrtdebug & DEBUG_XMIT) {
1792	    log(LOG_DEBUG, "phyint_send on vif %td err %d\n",
1793		vifp - viftable, error);
1794	}
1795}
1796
1797static int
1798X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
1799{
1800    int error, vifi;
1801
1802    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1803	return EOPNOTSUPP;
1804
1805    error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
1806    if (error)
1807	return error;
1808
1809    VIF_LOCK();
1810
1811    if (vifi < 0 || vifi >= numvifs) {	/* Error if vif is invalid */
1812	VIF_UNLOCK();
1813	return EADDRNOTAVAIL;
1814    }
1815
1816    if (sopt->sopt_name == IP_RSVP_VIF_ON) {
1817	/* Check if socket is available. */
1818	if (viftable[vifi].v_rsvpd != NULL) {
1819	    VIF_UNLOCK();
1820	    return EADDRINUSE;
1821	}
1822
1823	viftable[vifi].v_rsvpd = so;
1824	/* This may seem silly, but we need to be sure we don't over-increment
1825	 * the RSVP counter, in case something slips up.
1826	 */
1827	if (!viftable[vifi].v_rsvp_on) {
1828	    viftable[vifi].v_rsvp_on = 1;
1829	    rsvp_on++;
1830	}
1831    } else { /* must be VIF_OFF */
1832	/*
1833	 * XXX as an additional consistency check, one could make sure
1834	 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
1835	 * first parameter is pretty useless.
1836	 */
1837	viftable[vifi].v_rsvpd = NULL;
1838	/*
1839	 * This may seem silly, but we need to be sure we don't over-decrement
1840	 * the RSVP counter, in case something slips up.
1841	 */
1842	if (viftable[vifi].v_rsvp_on) {
1843	    viftable[vifi].v_rsvp_on = 0;
1844	    rsvp_on--;
1845	}
1846    }
1847    VIF_UNLOCK();
1848    return 0;
1849}
1850
1851static void
1852X_ip_rsvp_force_done(struct socket *so)
1853{
1854    int vifi;
1855
1856    /* Don't bother if it is not the right type of socket. */
1857    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1858	return;
1859
1860    VIF_LOCK();
1861
1862    /* The socket may be attached to more than one vif...this
1863     * is perfectly legal.
1864     */
1865    for (vifi = 0; vifi < numvifs; vifi++) {
1866	if (viftable[vifi].v_rsvpd == so) {
1867	    viftable[vifi].v_rsvpd = NULL;
1868	    /* This may seem silly, but we need to be sure we don't
1869	     * over-decrement the RSVP counter, in case something slips up.
1870	     */
1871	    if (viftable[vifi].v_rsvp_on) {
1872		viftable[vifi].v_rsvp_on = 0;
1873		rsvp_on--;
1874	    }
1875	}
1876    }
1877
1878    VIF_UNLOCK();
1879}
1880
1881static void
1882X_rsvp_input(struct mbuf *m, int off)
1883{
1884    int vifi;
1885    struct ip *ip = mtod(m, struct ip *);
1886    struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
1887    struct ifnet *ifp;
1888
1889    if (rsvpdebug)
1890	printf("rsvp_input: rsvp_on %d\n",rsvp_on);
1891
1892    /* Can still get packets with rsvp_on = 0 if there is a local member
1893     * of the group to which the RSVP packet is addressed.  But in this
1894     * case we want to throw the packet away.
1895     */
1896    if (!rsvp_on) {
1897	m_freem(m);
1898	return;
1899    }
1900
1901    if (rsvpdebug)
1902	printf("rsvp_input: check vifs\n");
1903
1904#ifdef DIAGNOSTIC
1905    M_ASSERTPKTHDR(m);
1906#endif
1907
1908    ifp = m->m_pkthdr.rcvif;
1909
1910    VIF_LOCK();
1911    /* Find which vif the packet arrived on. */
1912    for (vifi = 0; vifi < numvifs; vifi++)
1913	if (viftable[vifi].v_ifp == ifp)
1914	    break;
1915
1916    if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
1917	/*
1918	 * Drop the lock here to avoid holding it across rip_input.
1919	 * This could make rsvpdebug printfs wrong.  If you care,
1920	 * record the state of stuff before dropping the lock.
1921	 */
1922	VIF_UNLOCK();
1923	/*
1924	 * If the old-style non-vif-associated socket is set,
1925	 * then use it.  Otherwise, drop packet since there
1926	 * is no specific socket for this vif.
1927	 */
1928	if (ip_rsvpd != NULL) {
1929	    if (rsvpdebug)
1930		printf("rsvp_input: Sending packet up old-style socket\n");
1931	    rip_input(m, off);  /* xxx */
1932	} else {
1933	    if (rsvpdebug && vifi == numvifs)
1934		printf("rsvp_input: Can't find vif for packet.\n");
1935	    else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
1936		printf("rsvp_input: No socket defined for vif %d\n",vifi);
1937	    m_freem(m);
1938	}
1939	return;
1940    }
1941    rsvp_src.sin_addr = ip->ip_src;
1942
1943    if (rsvpdebug && m)
1944	printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
1945	       m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
1946
1947    if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
1948	if (rsvpdebug)
1949	    printf("rsvp_input: Failed to append to socket\n");
1950    } else {
1951	if (rsvpdebug)
1952	    printf("rsvp_input: send packet up\n");
1953    }
1954    VIF_UNLOCK();
1955}
1956
1957/*
1958 * Code for bandwidth monitors
1959 */
1960
1961/*
1962 * Define common interface for timeval-related methods
1963 */
1964#define	BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
1965#define	BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
1966#define	BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
1967
1968static uint32_t
1969compute_bw_meter_flags(struct bw_upcall *req)
1970{
1971    uint32_t flags = 0;
1972
1973    if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
1974	flags |= BW_METER_UNIT_PACKETS;
1975    if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
1976	flags |= BW_METER_UNIT_BYTES;
1977    if (req->bu_flags & BW_UPCALL_GEQ)
1978	flags |= BW_METER_GEQ;
1979    if (req->bu_flags & BW_UPCALL_LEQ)
1980	flags |= BW_METER_LEQ;
1981
1982    return flags;
1983}
1984
1985/*
1986 * Add a bw_meter entry
1987 */
1988static int
1989add_bw_upcall(struct bw_upcall *req)
1990{
1991    struct mfc *mfc;
1992    struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
1993		BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
1994    struct timeval now;
1995    struct bw_meter *x;
1996    uint32_t flags;
1997
1998    if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
1999	return EOPNOTSUPP;
2000
2001    /* Test if the flags are valid */
2002    if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2003	return EINVAL;
2004    if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2005	return EINVAL;
2006    if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2007	    == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2008	return EINVAL;
2009
2010    /* Test if the threshold time interval is valid */
2011    if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2012	return EINVAL;
2013
2014    flags = compute_bw_meter_flags(req);
2015
2016    /*
2017     * Find if we have already same bw_meter entry
2018     */
2019    MFC_LOCK();
2020    mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2021    if (mfc == NULL) {
2022	MFC_UNLOCK();
2023	return EADDRNOTAVAIL;
2024    }
2025    for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2026	if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2027			   &req->bu_threshold.b_time, ==)) &&
2028	    (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2029	    (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2030	    (x->bm_flags & BW_METER_USER_FLAGS) == flags)  {
2031	    MFC_UNLOCK();
2032	    return 0;		/* XXX Already installed */
2033	}
2034    }
2035
2036    /* Allocate the new bw_meter entry */
2037    x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2038    if (x == NULL) {
2039	MFC_UNLOCK();
2040	return ENOBUFS;
2041    }
2042
2043    /* Set the new bw_meter entry */
2044    x->bm_threshold.b_time = req->bu_threshold.b_time;
2045    GET_TIME(now);
2046    x->bm_start_time = now;
2047    x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2048    x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2049    x->bm_measured.b_packets = 0;
2050    x->bm_measured.b_bytes = 0;
2051    x->bm_flags = flags;
2052    x->bm_time_next = NULL;
2053    x->bm_time_hash = BW_METER_BUCKETS;
2054
2055    /* Add the new bw_meter entry to the front of entries for this MFC */
2056    x->bm_mfc = mfc;
2057    x->bm_mfc_next = mfc->mfc_bw_meter;
2058    mfc->mfc_bw_meter = x;
2059    schedule_bw_meter(x, &now);
2060    MFC_UNLOCK();
2061
2062    return 0;
2063}
2064
2065static void
2066free_bw_list(struct bw_meter *list)
2067{
2068    while (list != NULL) {
2069	struct bw_meter *x = list;
2070
2071	list = list->bm_mfc_next;
2072	unschedule_bw_meter(x);
2073	free(x, M_BWMETER);
2074    }
2075}
2076
2077/*
2078 * Delete one or multiple bw_meter entries
2079 */
2080static int
2081del_bw_upcall(struct bw_upcall *req)
2082{
2083    struct mfc *mfc;
2084    struct bw_meter *x;
2085
2086    if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2087	return EOPNOTSUPP;
2088
2089    MFC_LOCK();
2090    /* Find the corresponding MFC entry */
2091    mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2092    if (mfc == NULL) {
2093	MFC_UNLOCK();
2094	return EADDRNOTAVAIL;
2095    } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2096	/*
2097	 * Delete all bw_meter entries for this mfc
2098	 */
2099	struct bw_meter *list;
2100
2101	list = mfc->mfc_bw_meter;
2102	mfc->mfc_bw_meter = NULL;
2103	free_bw_list(list);
2104	MFC_UNLOCK();
2105	return 0;
2106    } else {			/* Delete a single bw_meter entry */
2107	struct bw_meter *prev;
2108	uint32_t flags = 0;
2109
2110	flags = compute_bw_meter_flags(req);
2111
2112	/* Find the bw_meter entry to delete */
2113	for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2114	     prev = x, x = x->bm_mfc_next) {
2115	    if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2116			       &req->bu_threshold.b_time, ==)) &&
2117		(x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2118		(x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2119		(x->bm_flags & BW_METER_USER_FLAGS) == flags)
2120		break;
2121	}
2122	if (x != NULL) { /* Delete entry from the list for this MFC */
2123	    if (prev != NULL)
2124		prev->bm_mfc_next = x->bm_mfc_next;	/* remove from middle*/
2125	    else
2126		x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2127
2128	    unschedule_bw_meter(x);
2129	    MFC_UNLOCK();
2130	    /* Free the bw_meter entry */
2131	    free(x, M_BWMETER);
2132	    return 0;
2133	} else {
2134	    MFC_UNLOCK();
2135	    return EINVAL;
2136	}
2137    }
2138    /* NOTREACHED */
2139}
2140
2141/*
2142 * Perform bandwidth measurement processing that may result in an upcall
2143 */
2144static void
2145bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2146{
2147    struct timeval delta;
2148
2149    MFC_LOCK_ASSERT();
2150
2151    delta = *nowp;
2152    BW_TIMEVALDECR(&delta, &x->bm_start_time);
2153
2154    if (x->bm_flags & BW_METER_GEQ) {
2155	/*
2156	 * Processing for ">=" type of bw_meter entry
2157	 */
2158	if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2159	    /* Reset the bw_meter entry */
2160	    x->bm_start_time = *nowp;
2161	    x->bm_measured.b_packets = 0;
2162	    x->bm_measured.b_bytes = 0;
2163	    x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2164	}
2165
2166	/* Record that a packet is received */
2167	x->bm_measured.b_packets++;
2168	x->bm_measured.b_bytes += plen;
2169
2170	/*
2171	 * Test if we should deliver an upcall
2172	 */
2173	if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2174	    if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2175		 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2176		((x->bm_flags & BW_METER_UNIT_BYTES) &&
2177		 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2178		/* Prepare an upcall for delivery */
2179		bw_meter_prepare_upcall(x, nowp);
2180		x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2181	    }
2182	}
2183    } else if (x->bm_flags & BW_METER_LEQ) {
2184	/*
2185	 * Processing for "<=" type of bw_meter entry
2186	 */
2187	if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2188	    /*
2189	     * We are behind time with the multicast forwarding table
2190	     * scanning for "<=" type of bw_meter entries, so test now
2191	     * if we should deliver an upcall.
2192	     */
2193	    if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2194		 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2195		((x->bm_flags & BW_METER_UNIT_BYTES) &&
2196		 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2197		/* Prepare an upcall for delivery */
2198		bw_meter_prepare_upcall(x, nowp);
2199	    }
2200	    /* Reschedule the bw_meter entry */
2201	    unschedule_bw_meter(x);
2202	    schedule_bw_meter(x, nowp);
2203	}
2204
2205	/* Record that a packet is received */
2206	x->bm_measured.b_packets++;
2207	x->bm_measured.b_bytes += plen;
2208
2209	/*
2210	 * Test if we should restart the measuring interval
2211	 */
2212	if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2213	     x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2214	    (x->bm_flags & BW_METER_UNIT_BYTES &&
2215	     x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2216	    /* Don't restart the measuring interval */
2217	} else {
2218	    /* Do restart the measuring interval */
2219	    /*
2220	     * XXX: note that we don't unschedule and schedule, because this
2221	     * might be too much overhead per packet. Instead, when we process
2222	     * all entries for a given timer hash bin, we check whether it is
2223	     * really a timeout. If not, we reschedule at that time.
2224	     */
2225	    x->bm_start_time = *nowp;
2226	    x->bm_measured.b_packets = 0;
2227	    x->bm_measured.b_bytes = 0;
2228	    x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2229	}
2230    }
2231}
2232
2233/*
2234 * Prepare a bandwidth-related upcall
2235 */
2236static void
2237bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2238{
2239    struct timeval delta;
2240    struct bw_upcall *u;
2241
2242    MFC_LOCK_ASSERT();
2243
2244    /*
2245     * Compute the measured time interval
2246     */
2247    delta = *nowp;
2248    BW_TIMEVALDECR(&delta, &x->bm_start_time);
2249
2250    /*
2251     * If there are too many pending upcalls, deliver them now
2252     */
2253    if (bw_upcalls_n >= BW_UPCALLS_MAX)
2254	bw_upcalls_send();
2255
2256    /*
2257     * Set the bw_upcall entry
2258     */
2259    u = &bw_upcalls[bw_upcalls_n++];
2260    u->bu_src = x->bm_mfc->mfc_origin;
2261    u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2262    u->bu_threshold.b_time = x->bm_threshold.b_time;
2263    u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2264    u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2265    u->bu_measured.b_time = delta;
2266    u->bu_measured.b_packets = x->bm_measured.b_packets;
2267    u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2268    u->bu_flags = 0;
2269    if (x->bm_flags & BW_METER_UNIT_PACKETS)
2270	u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2271    if (x->bm_flags & BW_METER_UNIT_BYTES)
2272	u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2273    if (x->bm_flags & BW_METER_GEQ)
2274	u->bu_flags |= BW_UPCALL_GEQ;
2275    if (x->bm_flags & BW_METER_LEQ)
2276	u->bu_flags |= BW_UPCALL_LEQ;
2277}
2278
2279/*
2280 * Send the pending bandwidth-related upcalls
2281 */
2282static void
2283bw_upcalls_send(void)
2284{
2285    struct mbuf *m;
2286    int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2287    struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2288    static struct igmpmsg igmpmsg = { 0,		/* unused1 */
2289				      0,		/* unused2 */
2290				      IGMPMSG_BW_UPCALL,/* im_msgtype */
2291				      0,		/* im_mbz  */
2292				      0,		/* im_vif  */
2293				      0,		/* unused3 */
2294				      { 0 },		/* im_src  */
2295				      { 0 } };		/* im_dst  */
2296
2297    MFC_LOCK_ASSERT();
2298
2299    if (bw_upcalls_n == 0)
2300	return;			/* No pending upcalls */
2301
2302    bw_upcalls_n = 0;
2303
2304    /*
2305     * Allocate a new mbuf, initialize it with the header and
2306     * the payload for the pending calls.
2307     */
2308    MGETHDR(m, M_DONTWAIT, MT_DATA);
2309    if (m == NULL) {
2310	log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2311	return;
2312    }
2313
2314    m->m_len = m->m_pkthdr.len = 0;
2315    m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2316    m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2317
2318    /*
2319     * Send the upcalls
2320     * XXX do we need to set the address in k_igmpsrc ?
2321     */
2322    mrtstat.mrts_upcalls++;
2323    if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2324	log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2325	++mrtstat.mrts_upq_sockfull;
2326    }
2327}
2328
2329/*
2330 * Compute the timeout hash value for the bw_meter entries
2331 */
2332#define	BW_METER_TIMEHASH(bw_meter, hash)				\
2333    do {								\
2334	struct timeval next_timeval = (bw_meter)->bm_start_time;	\
2335									\
2336	BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2337	(hash) = next_timeval.tv_sec;					\
2338	if (next_timeval.tv_usec)					\
2339	    (hash)++; /* XXX: make sure we don't timeout early */	\
2340	(hash) %= BW_METER_BUCKETS;					\
2341    } while (0)
2342
2343/*
2344 * Schedule a timer to process periodically bw_meter entry of type "<="
2345 * by linking the entry in the proper hash bucket.
2346 */
2347static void
2348schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2349{
2350    int time_hash;
2351
2352    MFC_LOCK_ASSERT();
2353
2354    if (!(x->bm_flags & BW_METER_LEQ))
2355	return;		/* XXX: we schedule timers only for "<=" entries */
2356
2357    /*
2358     * Reset the bw_meter entry
2359     */
2360    x->bm_start_time = *nowp;
2361    x->bm_measured.b_packets = 0;
2362    x->bm_measured.b_bytes = 0;
2363    x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2364
2365    /*
2366     * Compute the timeout hash value and insert the entry
2367     */
2368    BW_METER_TIMEHASH(x, time_hash);
2369    x->bm_time_next = bw_meter_timers[time_hash];
2370    bw_meter_timers[time_hash] = x;
2371    x->bm_time_hash = time_hash;
2372}
2373
2374/*
2375 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2376 * by removing the entry from the proper hash bucket.
2377 */
2378static void
2379unschedule_bw_meter(struct bw_meter *x)
2380{
2381    int time_hash;
2382    struct bw_meter *prev, *tmp;
2383
2384    MFC_LOCK_ASSERT();
2385
2386    if (!(x->bm_flags & BW_METER_LEQ))
2387	return;		/* XXX: we schedule timers only for "<=" entries */
2388
2389    /*
2390     * Compute the timeout hash value and delete the entry
2391     */
2392    time_hash = x->bm_time_hash;
2393    if (time_hash >= BW_METER_BUCKETS)
2394	return;		/* Entry was not scheduled */
2395
2396    for (prev = NULL, tmp = bw_meter_timers[time_hash];
2397	     tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2398	if (tmp == x)
2399	    break;
2400
2401    if (tmp == NULL)
2402	panic("unschedule_bw_meter: bw_meter entry not found");
2403
2404    if (prev != NULL)
2405	prev->bm_time_next = x->bm_time_next;
2406    else
2407	bw_meter_timers[time_hash] = x->bm_time_next;
2408
2409    x->bm_time_next = NULL;
2410    x->bm_time_hash = BW_METER_BUCKETS;
2411}
2412
2413
2414/*
2415 * Process all "<=" type of bw_meter that should be processed now,
2416 * and for each entry prepare an upcall if necessary. Each processed
2417 * entry is rescheduled again for the (periodic) processing.
2418 *
2419 * This is run periodically (once per second normally). On each round,
2420 * all the potentially matching entries are in the hash slot that we are
2421 * looking at.
2422 */
2423static void
2424bw_meter_process()
2425{
2426    static uint32_t last_tv_sec;	/* last time we processed this */
2427
2428    uint32_t loops;
2429    int i;
2430    struct timeval now, process_endtime;
2431
2432    GET_TIME(now);
2433    if (last_tv_sec == now.tv_sec)
2434	return;		/* nothing to do */
2435
2436    loops = now.tv_sec - last_tv_sec;
2437    last_tv_sec = now.tv_sec;
2438    if (loops > BW_METER_BUCKETS)
2439	loops = BW_METER_BUCKETS;
2440
2441    MFC_LOCK();
2442    /*
2443     * Process all bins of bw_meter entries from the one after the last
2444     * processed to the current one. On entry, i points to the last bucket
2445     * visited, so we need to increment i at the beginning of the loop.
2446     */
2447    for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2448	struct bw_meter *x, *tmp_list;
2449
2450	if (++i >= BW_METER_BUCKETS)
2451	    i = 0;
2452
2453	/* Disconnect the list of bw_meter entries from the bin */
2454	tmp_list = bw_meter_timers[i];
2455	bw_meter_timers[i] = NULL;
2456
2457	/* Process the list of bw_meter entries */
2458	while (tmp_list != NULL) {
2459	    x = tmp_list;
2460	    tmp_list = tmp_list->bm_time_next;
2461
2462	    /* Test if the time interval is over */
2463	    process_endtime = x->bm_start_time;
2464	    BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2465	    if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2466		/* Not yet: reschedule, but don't reset */
2467		int time_hash;
2468
2469		BW_METER_TIMEHASH(x, time_hash);
2470		if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2471		    /*
2472		     * XXX: somehow the bin processing is a bit ahead of time.
2473		     * Put the entry in the next bin.
2474		     */
2475		    if (++time_hash >= BW_METER_BUCKETS)
2476			time_hash = 0;
2477		}
2478		x->bm_time_next = bw_meter_timers[time_hash];
2479		bw_meter_timers[time_hash] = x;
2480		x->bm_time_hash = time_hash;
2481
2482		continue;
2483	    }
2484
2485	    /*
2486	     * Test if we should deliver an upcall
2487	     */
2488	    if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2489		 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2490		((x->bm_flags & BW_METER_UNIT_BYTES) &&
2491		 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2492		/* Prepare an upcall for delivery */
2493		bw_meter_prepare_upcall(x, &now);
2494	    }
2495
2496	    /*
2497	     * Reschedule for next processing
2498	     */
2499	    schedule_bw_meter(x, &now);
2500	}
2501    }
2502
2503    /* Send all upcalls that are pending delivery */
2504    bw_upcalls_send();
2505
2506    MFC_UNLOCK();
2507}
2508
2509/*
2510 * A periodic function for sending all upcalls that are pending delivery
2511 */
2512static void
2513expire_bw_upcalls_send(void *unused)
2514{
2515    MFC_LOCK();
2516    bw_upcalls_send();
2517    MFC_UNLOCK();
2518
2519    callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
2520	expire_bw_upcalls_send, NULL);
2521}
2522
2523/*
2524 * A periodic function for periodic scanning of the multicast forwarding
2525 * table for processing all "<=" bw_meter entries.
2526 */
2527static void
2528expire_bw_meter_process(void *unused)
2529{
2530    if (mrt_api_config & MRT_MFC_BW_UPCALL)
2531	bw_meter_process();
2532
2533    callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
2534}
2535
2536/*
2537 * End of bandwidth monitoring code
2538 */
2539
2540/*
2541 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2542 *
2543 */
2544static int
2545pim_register_send(struct ip *ip, struct vif *vifp,
2546	struct mbuf *m, struct mfc *rt)
2547{
2548    struct mbuf *mb_copy, *mm;
2549
2550    if (mrtdebug & DEBUG_PIM)
2551	log(LOG_DEBUG, "pim_register_send: ");
2552
2553    /*
2554     * Do not send IGMP_WHOLEPKT notifications to userland, if the
2555     * rendezvous point was unspecified, and we were told not to.
2556     */
2557    if (pim_squelch_wholepkt != 0 && (mrt_api_config & MRT_MFC_RP) &&
2558	(rt->mfc_rp.s_addr == INADDR_ANY))
2559	return 0;
2560
2561    mb_copy = pim_register_prepare(ip, m);
2562    if (mb_copy == NULL)
2563	return ENOBUFS;
2564
2565    /*
2566     * Send all the fragments. Note that the mbuf for each fragment
2567     * is freed by the sending machinery.
2568     */
2569    for (mm = mb_copy; mm; mm = mb_copy) {
2570	mb_copy = mm->m_nextpkt;
2571	mm->m_nextpkt = 0;
2572	mm = m_pullup(mm, sizeof(struct ip));
2573	if (mm != NULL) {
2574	    ip = mtod(mm, struct ip *);
2575	    if ((mrt_api_config & MRT_MFC_RP) &&
2576		(rt->mfc_rp.s_addr != INADDR_ANY)) {
2577		pim_register_send_rp(ip, vifp, mm, rt);
2578	    } else {
2579		pim_register_send_upcall(ip, vifp, mm, rt);
2580	    }
2581	}
2582    }
2583
2584    return 0;
2585}
2586
2587/*
2588 * Return a copy of the data packet that is ready for PIM Register
2589 * encapsulation.
2590 * XXX: Note that in the returned copy the IP header is a valid one.
2591 */
2592static struct mbuf *
2593pim_register_prepare(struct ip *ip, struct mbuf *m)
2594{
2595    struct mbuf *mb_copy = NULL;
2596    int mtu;
2597
2598    /* Take care of delayed checksums */
2599    if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2600	in_delayed_cksum(m);
2601	m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2602    }
2603
2604    /*
2605     * Copy the old packet & pullup its IP header into the
2606     * new mbuf so we can modify it.
2607     */
2608    mb_copy = m_copypacket(m, M_DONTWAIT);
2609    if (mb_copy == NULL)
2610	return NULL;
2611    mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2612    if (mb_copy == NULL)
2613	return NULL;
2614
2615    /* take care of the TTL */
2616    ip = mtod(mb_copy, struct ip *);
2617    --ip->ip_ttl;
2618
2619    /* Compute the MTU after the PIM Register encapsulation */
2620    mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2621
2622    if (ip->ip_len <= mtu) {
2623	/* Turn the IP header into a valid one */
2624	ip->ip_len = htons(ip->ip_len);
2625	ip->ip_off = htons(ip->ip_off);
2626	ip->ip_sum = 0;
2627	ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2628    } else {
2629	/* Fragment the packet */
2630	if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2631	    m_freem(mb_copy);
2632	    return NULL;
2633	}
2634    }
2635    return mb_copy;
2636}
2637
2638/*
2639 * Send an upcall with the data packet to the user-level process.
2640 */
2641static int
2642pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2643	struct mbuf *mb_copy, struct mfc *rt)
2644{
2645    struct mbuf *mb_first;
2646    int len = ntohs(ip->ip_len);
2647    struct igmpmsg *im;
2648    struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2649
2650    VIF_LOCK_ASSERT();
2651
2652    /*
2653     * Add a new mbuf with an upcall header
2654     */
2655    MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2656    if (mb_first == NULL) {
2657	m_freem(mb_copy);
2658	return ENOBUFS;
2659    }
2660    mb_first->m_data += max_linkhdr;
2661    mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2662    mb_first->m_len = sizeof(struct igmpmsg);
2663    mb_first->m_next = mb_copy;
2664
2665    /* Send message to routing daemon */
2666    im = mtod(mb_first, struct igmpmsg *);
2667    im->im_msgtype	= IGMPMSG_WHOLEPKT;
2668    im->im_mbz		= 0;
2669    im->im_vif		= vifp - viftable;
2670    im->im_src		= ip->ip_src;
2671    im->im_dst		= ip->ip_dst;
2672
2673    k_igmpsrc.sin_addr	= ip->ip_src;
2674
2675    mrtstat.mrts_upcalls++;
2676
2677    if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2678	if (mrtdebug & DEBUG_PIM)
2679	    log(LOG_WARNING,
2680		"mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2681	++mrtstat.mrts_upq_sockfull;
2682	return ENOBUFS;
2683    }
2684
2685    /* Keep statistics */
2686    pimstat.pims_snd_registers_msgs++;
2687    pimstat.pims_snd_registers_bytes += len;
2688
2689    return 0;
2690}
2691
2692/*
2693 * Encapsulate the data packet in PIM Register message and send it to the RP.
2694 */
2695static int
2696pim_register_send_rp(struct ip *ip, struct vif *vifp,
2697	struct mbuf *mb_copy, struct mfc *rt)
2698{
2699    struct mbuf *mb_first;
2700    struct ip *ip_outer;
2701    struct pim_encap_pimhdr *pimhdr;
2702    int len = ntohs(ip->ip_len);
2703    vifi_t vifi = rt->mfc_parent;
2704
2705    VIF_LOCK_ASSERT();
2706
2707    if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2708	m_freem(mb_copy);
2709	return EADDRNOTAVAIL;		/* The iif vif is invalid */
2710    }
2711
2712    /*
2713     * Add a new mbuf with the encapsulating header
2714     */
2715    MGETHDR(mb_first, M_DONTWAIT, MT_DATA);
2716    if (mb_first == NULL) {
2717	m_freem(mb_copy);
2718	return ENOBUFS;
2719    }
2720    mb_first->m_data += max_linkhdr;
2721    mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2722    mb_first->m_next = mb_copy;
2723
2724    mb_first->m_pkthdr.len = len + mb_first->m_len;
2725
2726    /*
2727     * Fill in the encapsulating IP and PIM header
2728     */
2729    ip_outer = mtod(mb_first, struct ip *);
2730    *ip_outer = pim_encap_iphdr;
2731    ip_outer->ip_id = ip_newid();
2732    ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2733    ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2734    ip_outer->ip_dst = rt->mfc_rp;
2735    /*
2736     * Copy the inner header TOS to the outer header, and take care of the
2737     * IP_DF bit.
2738     */
2739    ip_outer->ip_tos = ip->ip_tos;
2740    if (ntohs(ip->ip_off) & IP_DF)
2741	ip_outer->ip_off |= IP_DF;
2742    pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2743					 + sizeof(pim_encap_iphdr));
2744    *pimhdr = pim_encap_pimhdr;
2745    /* If the iif crosses a border, set the Border-bit */
2746    if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2747	pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2748
2749    mb_first->m_data += sizeof(pim_encap_iphdr);
2750    pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2751    mb_first->m_data -= sizeof(pim_encap_iphdr);
2752
2753    send_packet(vifp, mb_first);
2754
2755    /* Keep statistics */
2756    pimstat.pims_snd_registers_msgs++;
2757    pimstat.pims_snd_registers_bytes += len;
2758
2759    return 0;
2760}
2761
2762/*
2763 * pim_encapcheck() is called by the encap[46]_input() path at runtime to
2764 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
2765 * into the kernel.
2766 */
2767static int
2768pim_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
2769{
2770
2771#ifdef DIAGNOSTIC
2772    KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
2773#endif
2774    if (proto != IPPROTO_PIM)
2775	return 0;	/* not for us; reject the datagram. */
2776
2777    return 64;		/* claim the datagram. */
2778}
2779
2780/*
2781 * PIM-SMv2 and PIM-DM messages processing.
2782 * Receives and verifies the PIM control messages, and passes them
2783 * up to the listening socket, using rip_input().
2784 * The only message with special processing is the PIM_REGISTER message
2785 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2786 * is passed to if_simloop().
2787 */
2788void
2789pim_input(struct mbuf *m, int off)
2790{
2791    struct ip *ip = mtod(m, struct ip *);
2792    struct pim *pim;
2793    int minlen;
2794    int datalen = ip->ip_len;
2795    int ip_tos;
2796    int iphlen = off;
2797
2798    /* Keep statistics */
2799    pimstat.pims_rcv_total_msgs++;
2800    pimstat.pims_rcv_total_bytes += datalen;
2801
2802    /*
2803     * Validate lengths
2804     */
2805    if (datalen < PIM_MINLEN) {
2806	pimstat.pims_rcv_tooshort++;
2807	log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
2808	    datalen, (u_long)ip->ip_src.s_addr);
2809	m_freem(m);
2810	return;
2811    }
2812
2813    /*
2814     * If the packet is at least as big as a REGISTER, go agead
2815     * and grab the PIM REGISTER header size, to avoid another
2816     * possible m_pullup() later.
2817     *
2818     * PIM_MINLEN       == pimhdr + u_int32_t == 4 + 4 = 8
2819     * PIM_REG_MINLEN   == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
2820     */
2821    minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
2822    /*
2823     * Get the IP and PIM headers in contiguous memory, and
2824     * possibly the PIM REGISTER header.
2825     */
2826    if ((m->m_flags & M_EXT || m->m_len < minlen) &&
2827	(m = m_pullup(m, minlen)) == 0) {
2828	log(LOG_ERR, "pim_input: m_pullup failure\n");
2829	return;
2830    }
2831    /* m_pullup() may have given us a new mbuf so reset ip. */
2832    ip = mtod(m, struct ip *);
2833    ip_tos = ip->ip_tos;
2834
2835    /* adjust mbuf to point to the PIM header */
2836    m->m_data += iphlen;
2837    m->m_len  -= iphlen;
2838    pim = mtod(m, struct pim *);
2839
2840    /*
2841     * Validate checksum. If PIM REGISTER, exclude the data packet.
2842     *
2843     * XXX: some older PIMv2 implementations don't make this distinction,
2844     * so for compatibility reason perform the checksum over part of the
2845     * message, and if error, then over the whole message.
2846     */
2847    if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
2848	/* do nothing, checksum okay */
2849    } else if (in_cksum(m, datalen)) {
2850	pimstat.pims_rcv_badsum++;
2851	if (mrtdebug & DEBUG_PIM)
2852	    log(LOG_DEBUG, "pim_input: invalid checksum");
2853	m_freem(m);
2854	return;
2855    }
2856
2857    /* PIM version check */
2858    if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
2859	pimstat.pims_rcv_badversion++;
2860	log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
2861	    PIM_VT_V(pim->pim_vt), PIM_VERSION);
2862	m_freem(m);
2863	return;
2864    }
2865
2866    /* restore mbuf back to the outer IP */
2867    m->m_data -= iphlen;
2868    m->m_len  += iphlen;
2869
2870    if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
2871	/*
2872	 * Since this is a REGISTER, we'll make a copy of the register
2873	 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
2874	 * routing daemon.
2875	 */
2876	struct sockaddr_in dst = { sizeof(dst), AF_INET };
2877	struct mbuf *mcp;
2878	struct ip *encap_ip;
2879	u_int32_t *reghdr;
2880	struct ifnet *vifp;
2881
2882	VIF_LOCK();
2883	if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
2884	    VIF_UNLOCK();
2885	    if (mrtdebug & DEBUG_PIM)
2886		log(LOG_DEBUG,
2887		    "pim_input: register vif not set: %d\n", reg_vif_num);
2888	    m_freem(m);
2889	    return;
2890	}
2891	/* XXX need refcnt? */
2892	vifp = viftable[reg_vif_num].v_ifp;
2893	VIF_UNLOCK();
2894
2895	/*
2896	 * Validate length
2897	 */
2898	if (datalen < PIM_REG_MINLEN) {
2899	    pimstat.pims_rcv_tooshort++;
2900	    pimstat.pims_rcv_badregisters++;
2901	    log(LOG_ERR,
2902		"pim_input: register packet size too small %d from %lx\n",
2903		datalen, (u_long)ip->ip_src.s_addr);
2904	    m_freem(m);
2905	    return;
2906	}
2907
2908	reghdr = (u_int32_t *)(pim + 1);
2909	encap_ip = (struct ip *)(reghdr + 1);
2910
2911	if (mrtdebug & DEBUG_PIM) {
2912	    log(LOG_DEBUG,
2913		"pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
2914		(u_long)ntohl(encap_ip->ip_src.s_addr),
2915		(u_long)ntohl(encap_ip->ip_dst.s_addr),
2916		ntohs(encap_ip->ip_len));
2917	}
2918
2919	/* verify the version number of the inner packet */
2920	if (encap_ip->ip_v != IPVERSION) {
2921	    pimstat.pims_rcv_badregisters++;
2922	    if (mrtdebug & DEBUG_PIM) {
2923		log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
2924		    "of the inner packet\n", encap_ip->ip_v);
2925	    }
2926	    m_freem(m);
2927	    return;
2928	}
2929
2930	/* verify the inner packet is destined to a mcast group */
2931	if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
2932	    pimstat.pims_rcv_badregisters++;
2933	    if (mrtdebug & DEBUG_PIM)
2934		log(LOG_DEBUG,
2935		    "pim_input: inner packet of register is not "
2936		    "multicast %lx\n",
2937		    (u_long)ntohl(encap_ip->ip_dst.s_addr));
2938	    m_freem(m);
2939	    return;
2940	}
2941
2942	/* If a NULL_REGISTER, pass it to the daemon */
2943	if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
2944	    goto pim_input_to_daemon;
2945
2946	/*
2947	 * Copy the TOS from the outer IP header to the inner IP header.
2948	 */
2949	if (encap_ip->ip_tos != ip_tos) {
2950	    /* Outer TOS -> inner TOS */
2951	    encap_ip->ip_tos = ip_tos;
2952	    /* Recompute the inner header checksum. Sigh... */
2953
2954	    /* adjust mbuf to point to the inner IP header */
2955	    m->m_data += (iphlen + PIM_MINLEN);
2956	    m->m_len  -= (iphlen + PIM_MINLEN);
2957
2958	    encap_ip->ip_sum = 0;
2959	    encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
2960
2961	    /* restore mbuf to point back to the outer IP header */
2962	    m->m_data -= (iphlen + PIM_MINLEN);
2963	    m->m_len  += (iphlen + PIM_MINLEN);
2964	}
2965
2966	/*
2967	 * Decapsulate the inner IP packet and loopback to forward it
2968	 * as a normal multicast packet. Also, make a copy of the
2969	 *     outer_iphdr + pimhdr + reghdr + encap_iphdr
2970	 * to pass to the daemon later, so it can take the appropriate
2971	 * actions (e.g., send back PIM_REGISTER_STOP).
2972	 * XXX: here m->m_data points to the outer IP header.
2973	 */
2974	mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
2975	if (mcp == NULL) {
2976	    log(LOG_ERR,
2977		"pim_input: pim register: could not copy register head\n");
2978	    m_freem(m);
2979	    return;
2980	}
2981
2982	/* Keep statistics */
2983	/* XXX: registers_bytes include only the encap. mcast pkt */
2984	pimstat.pims_rcv_registers_msgs++;
2985	pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
2986
2987	/*
2988	 * forward the inner ip packet; point m_data at the inner ip.
2989	 */
2990	m_adj(m, iphlen + PIM_MINLEN);
2991
2992	if (mrtdebug & DEBUG_PIM) {
2993	    log(LOG_DEBUG,
2994		"pim_input: forwarding decapsulated register: "
2995		"src %lx, dst %lx, vif %d\n",
2996		(u_long)ntohl(encap_ip->ip_src.s_addr),
2997		(u_long)ntohl(encap_ip->ip_dst.s_addr),
2998		reg_vif_num);
2999	}
3000	/* NB: vifp was collected above; can it change on us? */
3001	if_simloop(vifp, m, dst.sin_family, 0);
3002
3003	/* prepare the register head to send to the mrouting daemon */
3004	m = mcp;
3005    }
3006
3007pim_input_to_daemon:
3008    /*
3009     * Pass the PIM message up to the daemon; if it is a Register message,
3010     * pass the 'head' only up to the daemon. This includes the
3011     * outer IP header, PIM header, PIM-Register header and the
3012     * inner IP header.
3013     * XXX: the outer IP header pkt size of a Register is not adjust to
3014     * reflect the fact that the inner multicast data is truncated.
3015     */
3016    rip_input(m, iphlen);
3017
3018    return;
3019}
3020
3021/*
3022 * XXX: This is common code for dealing with initialization for both
3023 * the IPv4 and IPv6 multicast forwarding paths. It could do with cleanup.
3024 */
3025static int
3026ip_mroute_modevent(module_t mod, int type, void *unused)
3027{
3028    switch (type) {
3029    case MOD_LOAD:
3030	mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
3031	MFC_LOCK_INIT();
3032	VIF_LOCK_INIT();
3033	ip_mrouter_reset();
3034	TUNABLE_ULONG_FETCH("net.inet.pim.squelch_wholepkt",
3035	    &pim_squelch_wholepkt);
3036
3037	pim_encap_cookie = encap_attach_func(AF_INET, IPPROTO_PIM,
3038	    pim_encapcheck, &in_pim_protosw, NULL);
3039	if (pim_encap_cookie == NULL) {
3040		printf("ip_mroute: unable to attach pim encap\n");
3041		VIF_LOCK_DESTROY();
3042		MFC_LOCK_DESTROY();
3043		mtx_destroy(&mrouter_mtx);
3044		return (EINVAL);
3045	}
3046
3047#ifdef INET6
3048	pim6_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_PIM,
3049	    pim_encapcheck, (struct protosw *)&in6_pim_protosw, NULL);
3050	if (pim6_encap_cookie == NULL) {
3051		printf("ip_mroute: unable to attach pim6 encap\n");
3052		if (pim_encap_cookie) {
3053		    encap_detach(pim_encap_cookie);
3054		    pim_encap_cookie = NULL;
3055		}
3056		VIF_LOCK_DESTROY();
3057		MFC_LOCK_DESTROY();
3058		mtx_destroy(&mrouter_mtx);
3059		return (EINVAL);
3060	}
3061#endif
3062
3063	ip_mcast_src = X_ip_mcast_src;
3064	ip_mforward = X_ip_mforward;
3065	ip_mrouter_done = X_ip_mrouter_done;
3066	ip_mrouter_get = X_ip_mrouter_get;
3067	ip_mrouter_set = X_ip_mrouter_set;
3068
3069#ifdef INET6
3070	ip6_mforward = X_ip6_mforward;
3071	ip6_mrouter_done = X_ip6_mrouter_done;
3072	ip6_mrouter_get = X_ip6_mrouter_get;
3073	ip6_mrouter_set = X_ip6_mrouter_set;
3074	mrt6_ioctl = X_mrt6_ioctl;
3075#endif
3076
3077	ip_rsvp_force_done = X_ip_rsvp_force_done;
3078	ip_rsvp_vif = X_ip_rsvp_vif;
3079
3080	legal_vif_num = X_legal_vif_num;
3081	mrt_ioctl = X_mrt_ioctl;
3082	rsvp_input_p = X_rsvp_input;
3083	break;
3084
3085    case MOD_UNLOAD:
3086	/*
3087	 * Typically module unload happens after the user-level
3088	 * process has shutdown the kernel services (the check
3089	 * below insures someone can't just yank the module out
3090	 * from under a running process).  But if the module is
3091	 * just loaded and then unloaded w/o starting up a user
3092	 * process we still need to cleanup.
3093	 */
3094	if (ip_mrouter
3095#ifdef INET6
3096	    || ip6_mrouter
3097#endif
3098	)
3099	    return EINVAL;
3100
3101#ifdef INET6
3102	if (pim6_encap_cookie) {
3103	    encap_detach(pim6_encap_cookie);
3104	    pim6_encap_cookie = NULL;
3105	}
3106	X_ip6_mrouter_done();
3107	ip6_mforward = NULL;
3108	ip6_mrouter_done = NULL;
3109	ip6_mrouter_get = NULL;
3110	ip6_mrouter_set = NULL;
3111	mrt6_ioctl = NULL;
3112#endif
3113
3114	if (pim_encap_cookie) {
3115	    encap_detach(pim_encap_cookie);
3116	    pim_encap_cookie = NULL;
3117	}
3118	X_ip_mrouter_done();
3119	ip_mcast_src = NULL;
3120	ip_mforward = NULL;
3121	ip_mrouter_done = NULL;
3122	ip_mrouter_get = NULL;
3123	ip_mrouter_set = NULL;
3124
3125	ip_rsvp_force_done = NULL;
3126	ip_rsvp_vif = NULL;
3127
3128	legal_vif_num = NULL;
3129	mrt_ioctl = NULL;
3130	rsvp_input_p = NULL;
3131
3132	VIF_LOCK_DESTROY();
3133	MFC_LOCK_DESTROY();
3134	mtx_destroy(&mrouter_mtx);
3135	break;
3136
3137    default:
3138	return EOPNOTSUPP;
3139    }
3140    return 0;
3141}
3142
3143static moduledata_t ip_mroutemod = {
3144    "ip_mroute",
3145    ip_mroute_modevent,
3146    0
3147};
3148DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3149