ip_mroute.c revision 83708
1/*
2 * IP multicast forwarding procedures
3 *
4 * Written by David Waitzman, BBN Labs, August 1988.
5 * Modified by Steve Deering, Stanford, February 1989.
6 * Modified by Mark J. Steiglitz, Stanford, May, 1991
7 * Modified by Van Jacobson, LBL, January 1993
8 * Modified by Ajit Thyagarajan, PARC, August 1993
9 * Modified by Bill Fenner, PARC, April 1995
10 *
11 * MROUTING Revision: 3.5
12 * $FreeBSD: head/sys/netinet/ip_mroute.c 83708 2001-09-20 07:59:45Z sumikawa $
13 */
14
15#include "opt_mrouting.h"
16#include "opt_random_ip_id.h"
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/malloc.h>
21#include <sys/mbuf.h>
22#include <sys/socket.h>
23#include <sys/socketvar.h>
24#include <sys/protosw.h>
25#include <sys/time.h>
26#include <sys/kernel.h>
27#include <sys/sysctl.h>
28#include <sys/sockio.h>
29#include <sys/syslog.h>
30#include <net/if.h>
31#include <net/route.h>
32#include <netinet/in.h>
33#include <netinet/in_systm.h>
34#include <netinet/ip.h>
35#include <netinet/ip_var.h>
36#include <netinet/in_var.h>
37#include <netinet/igmp.h>
38#include <netinet/ip_encap.h>
39#include <netinet/ip_mroute.h>
40#include <netinet/udp.h>
41#include <machine/in_cksum.h>
42
43#ifndef MROUTING
44extern u_long	_ip_mcast_src __P((int vifi));
45extern int	_ip_mforward __P((struct ip *ip, struct ifnet *ifp,
46				  struct mbuf *m, struct ip_moptions *imo));
47extern int	_ip_mrouter_done __P((void));
48extern int	_ip_mrouter_get __P((struct socket *so, struct sockopt *sopt));
49extern int	_ip_mrouter_set __P((struct socket *so, struct sockopt *sopt));
50extern int	_mrt_ioctl __P((int req, caddr_t data));
51
52/*
53 * Dummy routines and globals used when multicast routing is not compiled in.
54 */
55
56struct socket  *ip_mrouter  = NULL;
57u_int		rsvpdebug = 0;
58
59int
60_ip_mrouter_set(so, sopt)
61	struct socket *so;
62	struct sockopt *sopt;
63{
64	return(EOPNOTSUPP);
65}
66
67int (*ip_mrouter_set)(struct socket *, struct sockopt *) = _ip_mrouter_set;
68
69
70int
71_ip_mrouter_get(so, sopt)
72	struct socket *so;
73	struct sockopt *sopt;
74{
75	return(EOPNOTSUPP);
76}
77
78int (*ip_mrouter_get)(struct socket *, struct sockopt *) = _ip_mrouter_get;
79
80int
81_ip_mrouter_done()
82{
83	return(0);
84}
85
86int (*ip_mrouter_done)(void) = _ip_mrouter_done;
87
88int
89_ip_mforward(ip, ifp, m, imo)
90	struct ip *ip;
91	struct ifnet *ifp;
92	struct mbuf *m;
93	struct ip_moptions *imo;
94{
95	return(0);
96}
97
98int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
99		   struct ip_moptions *) = _ip_mforward;
100
101int
102_mrt_ioctl(int req, caddr_t data)
103{
104	return EOPNOTSUPP;
105}
106
107int (*mrt_ioctl)(int, caddr_t) = _mrt_ioctl;
108
109void
110rsvp_input(m, off)		/* XXX must fixup manually */
111	struct mbuf *m;
112	int off;
113{
114    /* Can still get packets with rsvp_on = 0 if there is a local member
115     * of the group to which the RSVP packet is addressed.  But in this
116     * case we want to throw the packet away.
117     */
118    if (!rsvp_on) {
119	m_freem(m);
120	return;
121    }
122
123    if (ip_rsvpd != NULL) {
124	if (rsvpdebug)
125	    printf("rsvp_input: Sending packet up old-style socket\n");
126	rip_input(m, off);
127	return;
128    }
129    /* Drop the packet */
130    m_freem(m);
131}
132
133int (*legal_vif_num)(int) = 0;
134
135/*
136 * This should never be called, since IP_MULTICAST_VIF should fail, but
137 * just in case it does get called, the code a little lower in ip_output
138 * will assign the packet a local address.
139 */
140u_long
141_ip_mcast_src(int vifi) { return INADDR_ANY; }
142u_long (*ip_mcast_src)(int) = _ip_mcast_src;
143
144int
145ip_rsvp_vif_init(so, sopt)
146    struct socket *so;
147    struct sockopt *sopt;
148{
149    return(EINVAL);
150}
151
152int
153ip_rsvp_vif_done(so, sopt)
154    struct socket *so;
155    struct sockopt *sopt;
156{
157    return(EINVAL);
158}
159
160void
161ip_rsvp_force_done(so)
162    struct socket *so;
163{
164    return;
165}
166
167#else /* MROUTING */
168
169#define M_HASCL(m)	((m)->m_flags & M_EXT)
170
171static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables");
172
173#ifndef MROUTE_KLD
174/* The socket used to communicate with the multicast routing daemon.  */
175struct socket  *ip_mrouter  = NULL;
176#endif
177
178#if defined(MROUTING) || defined(MROUTE_KLD)
179static struct mrtstat	mrtstat;
180SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW,
181    &mrtstat, mrtstat, "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
182#endif
183
184static struct mfc	*mfctable[MFCTBLSIZ];
185static u_char		nexpire[MFCTBLSIZ];
186static struct vif	viftable[MAXVIFS];
187static u_int	mrtdebug = 0;	  /* debug level 	*/
188#define		DEBUG_MFC	0x02
189#define		DEBUG_FORWARD	0x04
190#define		DEBUG_EXPIRE	0x08
191#define		DEBUG_XMIT	0x10
192static u_int  	tbfdebug = 0;     /* tbf debug level 	*/
193static u_int	rsvpdebug = 0;	  /* rsvp debug level   */
194
195static struct callout_handle expire_upcalls_ch;
196
197#define		EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second		*/
198#define		UPCALL_EXPIRE	6		/* number of timeouts	*/
199
200/*
201 * Define the token bucket filter structures
202 * tbftable -> each vif has one of these for storing info
203 */
204
205static struct tbf tbftable[MAXVIFS];
206#define		TBF_REPROCESS	(hz / 100)	/* 100x / second */
207
208/*
209 * 'Interfaces' associated with decapsulator (so we can tell
210 * packets that went through it from ones that get reflected
211 * by a broken gateway).  These interfaces are never linked into
212 * the system ifnet list & no routes point to them.  I.e., packets
213 * can't be sent this way.  They only exist as a placeholder for
214 * multicast source verification.
215 */
216static struct ifnet multicast_decap_if[MAXVIFS];
217
218#define ENCAP_TTL 64
219#define ENCAP_PROTO IPPROTO_IPIP	/* 4 */
220
221/* prototype IP hdr for encapsulated packets */
222static struct ip multicast_encap_iphdr = {
223#if BYTE_ORDER == LITTLE_ENDIAN
224	sizeof(struct ip) >> 2, IPVERSION,
225#else
226	IPVERSION, sizeof(struct ip) >> 2,
227#endif
228	0,				/* tos */
229	sizeof(struct ip),		/* total length */
230	0,				/* id */
231	0,				/* frag offset */
232	ENCAP_TTL, ENCAP_PROTO,
233	0,				/* checksum */
234};
235
236/*
237 * Private variables.
238 */
239static vifi_t	   numvifs = 0;
240static const struct encaptab *encap_cookie = NULL;
241
242/*
243 * one-back cache used by mroute_encapcheck to locate a tunnel's vif
244 * given a datagram's src ip address.
245 */
246static u_long last_encap_src;
247static struct vif *last_encap_vif;
248
249static u_long	X_ip_mcast_src __P((int vifi));
250static int	X_ip_mforward __P((struct ip *ip, struct ifnet *ifp, struct mbuf *m, struct ip_moptions *imo));
251static int	X_ip_mrouter_done __P((void));
252static int	X_ip_mrouter_get __P((struct socket *so, struct sockopt *m));
253static int	X_ip_mrouter_set __P((struct socket *so, struct sockopt *m));
254static int	X_legal_vif_num __P((int vif));
255static int	X_mrt_ioctl __P((int cmd, caddr_t data));
256
257static int get_sg_cnt(struct sioc_sg_req *);
258static int get_vif_cnt(struct sioc_vif_req *);
259static int ip_mrouter_init(struct socket *, int);
260static int add_vif(struct vifctl *);
261static int del_vif(vifi_t);
262static int add_mfc(struct mfcctl *);
263static int del_mfc(struct mfcctl *);
264static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
265static int set_assert(int);
266static void expire_upcalls(void *);
267static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *,
268		  vifi_t);
269static void phyint_send(struct ip *, struct vif *, struct mbuf *);
270static void encap_send(struct ip *, struct vif *, struct mbuf *);
271static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
272static void tbf_queue(struct vif *, struct mbuf *);
273static void tbf_process_q(struct vif *);
274static void tbf_reprocess_q(void *);
275static int tbf_dq_sel(struct vif *, struct ip *);
276static void tbf_send_packet(struct vif *, struct mbuf *);
277static void tbf_update_tokens(struct vif *);
278static int priority(struct vif *, struct ip *);
279
280/*
281 * whether or not special PIM assert processing is enabled.
282 */
283static int pim_assert;
284/*
285 * Rate limit for assert notification messages, in usec
286 */
287#define ASSERT_MSG_TIME		3000000
288
289/*
290 * Hash function for a source, group entry
291 */
292#define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
293			((g) >> 20) ^ ((g) >> 10) ^ (g))
294
295/*
296 * Find a route for a given origin IP address and Multicast group address
297 * Type of service parameter to be added in the future!!!
298 */
299
300#define MFCFIND(o, g, rt) { \
301	register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
302	rt = NULL; \
303	++mrtstat.mrts_mfc_lookups; \
304	while (_rt) { \
305		if ((_rt->mfc_origin.s_addr == o) && \
306		    (_rt->mfc_mcastgrp.s_addr == g) && \
307		    (_rt->mfc_stall == NULL)) { \
308			rt = _rt; \
309			break; \
310		} \
311		_rt = _rt->mfc_next; \
312	} \
313	if (rt == NULL) { \
314		++mrtstat.mrts_mfc_misses; \
315	} \
316}
317
318
319/*
320 * Macros to compute elapsed time efficiently
321 * Borrowed from Van Jacobson's scheduling code
322 */
323#define TV_DELTA(a, b, delta) { \
324	    register int xxs; \
325		\
326	    delta = (a).tv_usec - (b).tv_usec; \
327	    if ((xxs = (a).tv_sec - (b).tv_sec)) { \
328	       switch (xxs) { \
329		      case 2: \
330			  delta += 1000000; \
331			      /* fall through */ \
332		      case 1: \
333			  delta += 1000000; \
334			  break; \
335		      default: \
336			  delta += (1000000 * xxs); \
337	       } \
338	    } \
339}
340
341#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
342	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
343
344#ifdef UPCALL_TIMING
345u_long upcall_data[51];
346static void collate(struct timeval *);
347#endif /* UPCALL_TIMING */
348
349
350/*
351 * Handle MRT setsockopt commands to modify the multicast routing tables.
352 */
353static int
354X_ip_mrouter_set(so, sopt)
355	struct socket *so;
356	struct sockopt *sopt;
357{
358	int	error, optval;
359	vifi_t	vifi;
360	struct	vifctl vifc;
361	struct	mfcctl mfc;
362
363	if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
364		return (EPERM);
365
366	error = 0;
367	switch (sopt->sopt_name) {
368	case MRT_INIT:
369		error = sooptcopyin(sopt, &optval, sizeof optval,
370				    sizeof optval);
371		if (error)
372			break;
373		error = ip_mrouter_init(so, optval);
374		break;
375
376	case MRT_DONE:
377		error = ip_mrouter_done();
378		break;
379
380	case MRT_ADD_VIF:
381		error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
382		if (error)
383			break;
384		error = add_vif(&vifc);
385		break;
386
387	case MRT_DEL_VIF:
388		error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
389		if (error)
390			break;
391		error = del_vif(vifi);
392		break;
393
394	case MRT_ADD_MFC:
395	case MRT_DEL_MFC:
396		error = sooptcopyin(sopt, &mfc, sizeof mfc, sizeof mfc);
397		if (error)
398			break;
399		if (sopt->sopt_name == MRT_ADD_MFC)
400			error = add_mfc(&mfc);
401		else
402			error = del_mfc(&mfc);
403		break;
404
405	case MRT_ASSERT:
406		error = sooptcopyin(sopt, &optval, sizeof optval,
407				    sizeof optval);
408		if (error)
409			break;
410		set_assert(optval);
411		break;
412
413	default:
414		error = EOPNOTSUPP;
415		break;
416	}
417	return (error);
418}
419
420#ifndef MROUTE_KLD
421int (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set;
422#endif
423
424/*
425 * Handle MRT getsockopt commands
426 */
427static int
428X_ip_mrouter_get(so, sopt)
429	struct socket *so;
430	struct sockopt *sopt;
431{
432	int error;
433	static int version = 0x0305; /* !!! why is this here? XXX */
434
435	switch (sopt->sopt_name) {
436	case MRT_VERSION:
437		error = sooptcopyout(sopt, &version, sizeof version);
438		break;
439
440	case MRT_ASSERT:
441		error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
442		break;
443	default:
444		error = EOPNOTSUPP;
445		break;
446	}
447	return (error);
448}
449
450#ifndef MROUTE_KLD
451int (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get;
452#endif
453
454/*
455 * Handle ioctl commands to obtain information from the cache
456 */
457static int
458X_mrt_ioctl(cmd, data)
459    int cmd;
460    caddr_t data;
461{
462    int error = 0;
463
464    switch (cmd) {
465	case (SIOCGETVIFCNT):
466	    return (get_vif_cnt((struct sioc_vif_req *)data));
467	    break;
468	case (SIOCGETSGCNT):
469	    return (get_sg_cnt((struct sioc_sg_req *)data));
470	    break;
471	default:
472	    return (EINVAL);
473	    break;
474    }
475    return error;
476}
477
478#ifndef MROUTE_KLD
479int (*mrt_ioctl)(int, caddr_t) = X_mrt_ioctl;
480#endif
481
482/*
483 * returns the packet, byte, rpf-failure count for the source group provided
484 */
485static int
486get_sg_cnt(req)
487    register struct sioc_sg_req *req;
488{
489    register struct mfc *rt;
490    int s;
491
492    s = splnet();
493    MFCFIND(req->src.s_addr, req->grp.s_addr, rt);
494    splx(s);
495    if (rt != NULL) {
496	req->pktcnt = rt->mfc_pkt_cnt;
497	req->bytecnt = rt->mfc_byte_cnt;
498	req->wrong_if = rt->mfc_wrong_if;
499    } else
500	req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
501
502    return 0;
503}
504
505/*
506 * returns the input and output packet and byte counts on the vif provided
507 */
508static int
509get_vif_cnt(req)
510    register struct sioc_vif_req *req;
511{
512    register vifi_t vifi = req->vifi;
513
514    if (vifi >= numvifs) return EINVAL;
515
516    req->icount = viftable[vifi].v_pkt_in;
517    req->ocount = viftable[vifi].v_pkt_out;
518    req->ibytes = viftable[vifi].v_bytes_in;
519    req->obytes = viftable[vifi].v_bytes_out;
520
521    return 0;
522}
523
524/*
525 * Enable multicast routing
526 */
527static int
528ip_mrouter_init(so, version)
529	struct socket *so;
530	int version;
531{
532    if (mrtdebug)
533	log(LOG_DEBUG,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
534		so->so_type, so->so_proto->pr_protocol);
535
536    if (so->so_type != SOCK_RAW ||
537	so->so_proto->pr_protocol != IPPROTO_IGMP) return EOPNOTSUPP;
538
539    if (version != 1)
540	return ENOPROTOOPT;
541
542    if (ip_mrouter != NULL) return EADDRINUSE;
543
544    ip_mrouter = so;
545
546    bzero((caddr_t)mfctable, sizeof(mfctable));
547    bzero((caddr_t)nexpire, sizeof(nexpire));
548
549    pim_assert = 0;
550
551    expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
552
553    if (mrtdebug)
554	log(LOG_DEBUG, "ip_mrouter_init\n");
555
556    return 0;
557}
558
559/*
560 * Disable multicast routing
561 */
562static int
563X_ip_mrouter_done()
564{
565    vifi_t vifi;
566    int i;
567    struct ifnet *ifp;
568    struct ifreq ifr;
569    struct mfc *rt;
570    struct rtdetq *rte;
571    int s;
572
573    s = splnet();
574
575    /*
576     * For each phyint in use, disable promiscuous reception of all IP
577     * multicasts.
578     */
579    for (vifi = 0; vifi < numvifs; vifi++) {
580	if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
581	    !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
582	    ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
583	    ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr
584								= INADDR_ANY;
585	    ifp = viftable[vifi].v_ifp;
586	    if_allmulti(ifp, 0);
587	}
588    }
589    bzero((caddr_t)tbftable, sizeof(tbftable));
590    bzero((caddr_t)viftable, sizeof(viftable));
591    numvifs = 0;
592    pim_assert = 0;
593
594    untimeout(expire_upcalls, (caddr_t)NULL, expire_upcalls_ch);
595
596    /*
597     * Free all multicast forwarding cache entries.
598     */
599    for (i = 0; i < MFCTBLSIZ; i++) {
600	for (rt = mfctable[i]; rt != NULL; ) {
601	    struct mfc *nr = rt->mfc_next;
602
603	    for (rte = rt->mfc_stall; rte != NULL; ) {
604		struct rtdetq *n = rte->next;
605
606		m_freem(rte->m);
607		free(rte, M_MRTABLE);
608		rte = n;
609	    }
610	    free(rt, M_MRTABLE);
611	    rt = nr;
612	}
613    }
614
615    bzero((caddr_t)mfctable, sizeof(mfctable));
616
617    /*
618     * Reset de-encapsulation cache
619     */
620    last_encap_src = 0;
621    last_encap_vif = NULL;
622    if (encap_cookie) {
623	encap_detach(encap_cookie);
624	encap_cookie = NULL;
625    }
626
627    ip_mrouter = NULL;
628
629    splx(s);
630
631    if (mrtdebug)
632	log(LOG_DEBUG, "ip_mrouter_done\n");
633
634    return 0;
635}
636
637#ifndef MROUTE_KLD
638int (*ip_mrouter_done)(void) = X_ip_mrouter_done;
639#endif
640
641/*
642 * Set PIM assert processing global
643 */
644static int
645set_assert(i)
646	int i;
647{
648    if ((i != 1) && (i != 0))
649	return EINVAL;
650
651    pim_assert = i;
652
653    return 0;
654}
655
656/*
657 * Decide if a packet is from a tunnelled peer.
658 * Return 0 if not, 64 if so.
659 */
660static int
661mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
662{
663    struct ip *ip = mtod(m, struct ip *);
664    int hlen = ip->ip_hl << 2;
665    register struct vif *vifp;
666
667    /*
668     * don't claim the packet if it's not to a multicast destination or if
669     * we don't have an encapsulating tunnel with the source.
670     * Note:  This code assumes that the remote site IP address
671     * uniquely identifies the tunnel (i.e., that this site has
672     * at most one tunnel with the remote site).
673     */
674    if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
675	return 0;
676    }
677    if (ip->ip_src.s_addr != last_encap_src) {
678	register struct vif *vife;
679
680	vifp = viftable;
681	vife = vifp + numvifs;
682	last_encap_src = ip->ip_src.s_addr;
683	last_encap_vif = 0;
684	for ( ; vifp < vife; ++vifp)
685	    if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
686		if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
687		    == VIFF_TUNNEL)
688		    last_encap_vif = vifp;
689		break;
690	    }
691    }
692    if ((vifp = last_encap_vif) == 0) {
693	last_encap_src = 0;
694	return 0;
695    }
696    return 64;
697}
698
699/*
700 * De-encapsulate a packet and feed it back through ip input (this
701 * routine is called whenever IP gets a packet that mroute_encap_func()
702 * claimed).
703 */
704static void
705mroute_encap_input(struct mbuf *m, int off)
706{
707    struct ip *ip = mtod(m, struct ip *);
708    int hlen = ip->ip_hl << 2;
709
710    if (hlen > sizeof(struct ip))
711      ip_stripoptions(m, (struct mbuf *) 0);
712    m->m_data += sizeof(struct ip);
713    m->m_len -= sizeof(struct ip);
714    m->m_pkthdr.len -= sizeof(struct ip);
715
716    m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
717
718    (void) IF_HANDOFF(&ipintrq, m, NULL);
719	/*
720	 * normally we would need a "schednetisr(NETISR_IP)"
721	 * here but we were called by ip_input and it is going
722	 * to loop back & try to dequeue the packet we just
723	 * queued as soon as we return so we avoid the
724	 * unnecessary software interrrupt.
725	 */
726}
727
728extern struct domain inetdomain;
729static struct protosw mroute_encap_protosw =
730{ SOCK_RAW,	&inetdomain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR,
731  mroute_encap_input,	0,	0,		rip_ctloutput,
732  0,
733  0,		0,		0,		0,
734  &rip_usrreqs
735};
736
737/*
738 * Add a vif to the vif table
739 */
740static int
741add_vif(vifcp)
742    register struct vifctl *vifcp;
743{
744    register struct vif *vifp = viftable + vifcp->vifc_vifi;
745    static struct sockaddr_in sin = {sizeof sin, AF_INET};
746    struct ifaddr *ifa;
747    struct ifnet *ifp;
748    int error, s;
749    struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
750
751    if (vifcp->vifc_vifi >= MAXVIFS)  return EINVAL;
752    if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE;
753
754    /* Find the interface with an address in AF_INET family */
755    sin.sin_addr = vifcp->vifc_lcl_addr;
756    ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
757    if (ifa == 0) return EADDRNOTAVAIL;
758    ifp = ifa->ifa_ifp;
759
760    if (vifcp->vifc_flags & VIFF_TUNNEL) {
761	if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
762		/*
763		 * An encapsulating tunnel is wanted.  Tell
764		 * mroute_encap_input() to start paying attention
765		 * to encapsulated packets.
766		 */
767		if (encap_cookie == NULL) {
768			encap_cookie = encap_attach_func(AF_INET, -1,
769				mroute_encapcheck,
770				(struct protosw *)&mroute_encap_protosw, NULL);
771
772			if (encap_cookie == NULL) {
773				printf("ip_mroute: unable to attach encap\n");
774				return (EIO);	/* XXX */
775			}
776			for (s = 0; s < MAXVIFS; ++s) {
777				multicast_decap_if[s].if_name = "mdecap";
778				multicast_decap_if[s].if_unit = s;
779			}
780		}
781		/*
782		 * Set interface to fake encapsulator interface
783		 */
784		ifp = &multicast_decap_if[vifcp->vifc_vifi];
785		/*
786		 * Prepare cached route entry
787		 */
788		bzero(&vifp->v_route, sizeof(vifp->v_route));
789	} else {
790	    log(LOG_ERR, "source routed tunnels not supported\n");
791	    return EOPNOTSUPP;
792	}
793    } else {
794	/* Make sure the interface supports multicast */
795	if ((ifp->if_flags & IFF_MULTICAST) == 0)
796	    return EOPNOTSUPP;
797
798	/* Enable promiscuous reception of all IP multicasts from the if */
799	s = splnet();
800	error = if_allmulti(ifp, 1);
801	splx(s);
802	if (error)
803	    return error;
804    }
805
806    s = splnet();
807    /* define parameters for the tbf structure */
808    vifp->v_tbf = v_tbf;
809    GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
810    vifp->v_tbf->tbf_n_tok = 0;
811    vifp->v_tbf->tbf_q_len = 0;
812    vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
813    vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
814
815    vifp->v_flags     = vifcp->vifc_flags;
816    vifp->v_threshold = vifcp->vifc_threshold;
817    vifp->v_lcl_addr  = vifcp->vifc_lcl_addr;
818    vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
819    vifp->v_ifp       = ifp;
820    /* scaling up here allows division by 1024 in critical code */
821    vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
822    vifp->v_rsvp_on   = 0;
823    vifp->v_rsvpd     = NULL;
824    /* initialize per vif pkt counters */
825    vifp->v_pkt_in    = 0;
826    vifp->v_pkt_out   = 0;
827    vifp->v_bytes_in  = 0;
828    vifp->v_bytes_out = 0;
829    splx(s);
830
831    /* Adjust numvifs up if the vifi is higher than numvifs */
832    if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
833
834    if (mrtdebug)
835	log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
836	    vifcp->vifc_vifi,
837	    (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
838	    (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
839	    (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
840	    vifcp->vifc_threshold,
841	    vifcp->vifc_rate_limit);
842
843    return 0;
844}
845
846/*
847 * Delete a vif from the vif table
848 */
849static int
850del_vif(vifi)
851	vifi_t vifi;
852{
853    register struct vif *vifp = &viftable[vifi];
854    register struct mbuf *m;
855    struct ifnet *ifp;
856    struct ifreq ifr;
857    int s;
858
859    if (vifi >= numvifs) return EINVAL;
860    if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL;
861
862    s = splnet();
863
864    if (!(vifp->v_flags & VIFF_TUNNEL)) {
865	((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
866	((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
867	ifp = vifp->v_ifp;
868	if_allmulti(ifp, 0);
869    }
870
871    if (vifp == last_encap_vif) {
872	last_encap_vif = 0;
873	last_encap_src = 0;
874    }
875
876    /*
877     * Free packets queued at the interface
878     */
879    while (vifp->v_tbf->tbf_q) {
880	m = vifp->v_tbf->tbf_q;
881	vifp->v_tbf->tbf_q = m->m_act;
882	m_freem(m);
883    }
884
885    bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
886    bzero((caddr_t)vifp, sizeof (*vifp));
887
888    if (mrtdebug)
889      log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
890
891    /* Adjust numvifs down */
892    for (vifi = numvifs; vifi > 0; vifi--)
893	if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break;
894    numvifs = vifi;
895
896    splx(s);
897
898    return 0;
899}
900
901/*
902 * Add an mfc entry
903 */
904static int
905add_mfc(mfccp)
906    struct mfcctl *mfccp;
907{
908    struct mfc *rt;
909    u_long hash;
910    struct rtdetq *rte;
911    register u_short nstl;
912    int s;
913    int i;
914
915    MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt);
916
917    /* If an entry already exists, just update the fields */
918    if (rt) {
919	if (mrtdebug & DEBUG_MFC)
920	    log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
921		(u_long)ntohl(mfccp->mfcc_origin.s_addr),
922		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
923		mfccp->mfcc_parent);
924
925	s = splnet();
926	rt->mfc_parent = mfccp->mfcc_parent;
927	for (i = 0; i < numvifs; i++)
928	    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
929	splx(s);
930	return 0;
931    }
932
933    /*
934     * Find the entry for which the upcall was made and update
935     */
936    s = splnet();
937    hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
938    for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
939
940	if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
941	    (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
942	    (rt->mfc_stall != NULL)) {
943
944	    if (nstl++)
945		log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
946		    "multiple kernel entries",
947		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
948		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
949		    mfccp->mfcc_parent, (void *)rt->mfc_stall);
950
951	    if (mrtdebug & DEBUG_MFC)
952		log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
953		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
954		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
955		    mfccp->mfcc_parent, (void *)rt->mfc_stall);
956
957	    rt->mfc_origin     = mfccp->mfcc_origin;
958	    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
959	    rt->mfc_parent     = mfccp->mfcc_parent;
960	    for (i = 0; i < numvifs; i++)
961		rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
962	    /* initialize pkt counters per src-grp */
963	    rt->mfc_pkt_cnt    = 0;
964	    rt->mfc_byte_cnt   = 0;
965	    rt->mfc_wrong_if   = 0;
966	    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
967
968	    rt->mfc_expire = 0;	/* Don't clean this guy up */
969	    nexpire[hash]--;
970
971	    /* free packets Qed at the end of this entry */
972	    for (rte = rt->mfc_stall; rte != NULL; ) {
973		struct rtdetq *n = rte->next;
974
975		ip_mdq(rte->m, rte->ifp, rt, -1);
976		m_freem(rte->m);
977#ifdef UPCALL_TIMING
978		collate(&(rte->t));
979#endif /* UPCALL_TIMING */
980		free(rte, M_MRTABLE);
981		rte = n;
982	    }
983	    rt->mfc_stall = NULL;
984	}
985    }
986
987    /*
988     * It is possible that an entry is being inserted without an upcall
989     */
990    if (nstl == 0) {
991	if (mrtdebug & DEBUG_MFC)
992	    log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
993		hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
994		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
995		mfccp->mfcc_parent);
996
997	for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
998
999	    if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1000		(rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1001
1002		rt->mfc_origin     = mfccp->mfcc_origin;
1003		rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
1004		rt->mfc_parent     = mfccp->mfcc_parent;
1005		for (i = 0; i < numvifs; i++)
1006		    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1007		/* initialize pkt counters per src-grp */
1008		rt->mfc_pkt_cnt    = 0;
1009		rt->mfc_byte_cnt   = 0;
1010		rt->mfc_wrong_if   = 0;
1011		rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1012		if (rt->mfc_expire)
1013		    nexpire[hash]--;
1014		rt->mfc_expire	   = 0;
1015	    }
1016	}
1017	if (rt == NULL) {
1018	    /* no upcall, so make a new entry */
1019	    rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1020	    if (rt == NULL) {
1021		splx(s);
1022		return ENOBUFS;
1023	    }
1024
1025	    /* insert new entry at head of hash chain */
1026	    rt->mfc_origin     = mfccp->mfcc_origin;
1027	    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
1028	    rt->mfc_parent     = mfccp->mfcc_parent;
1029	    for (i = 0; i < numvifs; i++)
1030		    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1031	    /* initialize pkt counters per src-grp */
1032	    rt->mfc_pkt_cnt    = 0;
1033	    rt->mfc_byte_cnt   = 0;
1034	    rt->mfc_wrong_if   = 0;
1035	    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1036	    rt->mfc_expire     = 0;
1037	    rt->mfc_stall      = NULL;
1038
1039	    /* link into table */
1040	    rt->mfc_next = mfctable[hash];
1041	    mfctable[hash] = rt;
1042	}
1043    }
1044    splx(s);
1045    return 0;
1046}
1047
1048#ifdef UPCALL_TIMING
1049/*
1050 * collect delay statistics on the upcalls
1051 */
1052static void collate(t)
1053register struct timeval *t;
1054{
1055    register u_long d;
1056    register struct timeval tp;
1057    register u_long delta;
1058
1059    GET_TIME(tp);
1060
1061    if (TV_LT(*t, tp))
1062    {
1063	TV_DELTA(tp, *t, delta);
1064
1065	d = delta >> 10;
1066	if (d > 50)
1067	    d = 50;
1068
1069	++upcall_data[d];
1070    }
1071}
1072#endif /* UPCALL_TIMING */
1073
1074/*
1075 * Delete an mfc entry
1076 */
1077static int
1078del_mfc(mfccp)
1079    struct mfcctl *mfccp;
1080{
1081    struct in_addr 	origin;
1082    struct in_addr 	mcastgrp;
1083    struct mfc 		*rt;
1084    struct mfc	 	**nptr;
1085    u_long 		hash;
1086    int s;
1087
1088    origin = mfccp->mfcc_origin;
1089    mcastgrp = mfccp->mfcc_mcastgrp;
1090    hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1091
1092    if (mrtdebug & DEBUG_MFC)
1093	log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1094	    (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1095
1096    s = splnet();
1097
1098    nptr = &mfctable[hash];
1099    while ((rt = *nptr) != NULL) {
1100	if (origin.s_addr == rt->mfc_origin.s_addr &&
1101	    mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1102	    rt->mfc_stall == NULL)
1103	    break;
1104
1105	nptr = &rt->mfc_next;
1106    }
1107    if (rt == NULL) {
1108	splx(s);
1109	return EADDRNOTAVAIL;
1110    }
1111
1112    *nptr = rt->mfc_next;
1113    free(rt, M_MRTABLE);
1114
1115    splx(s);
1116
1117    return 0;
1118}
1119
1120/*
1121 * Send a message to mrouted on the multicast routing socket
1122 */
1123static int
1124socket_send(s, mm, src)
1125	struct socket *s;
1126	struct mbuf *mm;
1127	struct sockaddr_in *src;
1128{
1129	if (s) {
1130		if (sbappendaddr(&s->so_rcv,
1131				 (struct sockaddr *)src,
1132				 mm, (struct mbuf *)0) != 0) {
1133			sorwakeup(s);
1134			return 0;
1135		}
1136	}
1137	m_freem(mm);
1138	return -1;
1139}
1140
1141/*
1142 * IP multicast forwarding function. This function assumes that the packet
1143 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1144 * pointed to by "ifp", and the packet is to be relayed to other networks
1145 * that have members of the packet's destination IP multicast group.
1146 *
1147 * The packet is returned unscathed to the caller, unless it is
1148 * erroneous, in which case a non-zero return value tells the caller to
1149 * discard it.
1150 */
1151
1152#define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
1153
1154static int
1155X_ip_mforward(ip, ifp, m, imo)
1156    register struct ip *ip;
1157    struct ifnet *ifp;
1158    struct mbuf *m;
1159    struct ip_moptions *imo;
1160{
1161    register struct mfc *rt;
1162    register u_char *ipoptions;
1163    static struct sockaddr_in 	k_igmpsrc	= { sizeof k_igmpsrc, AF_INET };
1164    static int srctun = 0;
1165    register struct mbuf *mm;
1166    int s;
1167    vifi_t vifi;
1168    struct vif *vifp;
1169
1170    if (mrtdebug & DEBUG_FORWARD)
1171	log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1172	    (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1173	    (void *)ifp);
1174
1175    if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1176	(ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1177	/*
1178	 * Packet arrived via a physical interface or
1179	 * an encapsulated tunnel.
1180	 */
1181    } else {
1182	/*
1183	 * Packet arrived through a source-route tunnel.
1184	 * Source-route tunnels are no longer supported.
1185	 */
1186	if ((srctun++ % 1000) == 0)
1187	    log(LOG_ERR,
1188		"ip_mforward: received source-routed packet from %lx\n",
1189		(u_long)ntohl(ip->ip_src.s_addr));
1190
1191	return 1;
1192    }
1193
1194    if ((imo) && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1195	if (ip->ip_ttl < 255)
1196		ip->ip_ttl++;	/* compensate for -1 in *_send routines */
1197	if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1198	    vifp = viftable + vifi;
1199	    printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1200		ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), vifi,
1201		(vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1202		vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1203	}
1204	return (ip_mdq(m, ifp, NULL, vifi));
1205    }
1206    if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1207	printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1208	    ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr));
1209	if(!imo)
1210		printf("In fact, no options were specified at all\n");
1211    }
1212
1213    /*
1214     * Don't forward a packet with time-to-live of zero or one,
1215     * or a packet destined to a local-only group.
1216     */
1217    if (ip->ip_ttl <= 1 ||
1218	ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1219	return 0;
1220
1221    /*
1222     * Determine forwarding vifs from the forwarding cache table
1223     */
1224    s = splnet();
1225    MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt);
1226
1227    /* Entry exists, so forward if necessary */
1228    if (rt != NULL) {
1229	splx(s);
1230	return (ip_mdq(m, ifp, rt, -1));
1231    } else {
1232	/*
1233	 * If we don't have a route for packet's origin,
1234	 * Make a copy of the packet &
1235	 * send message to routing daemon
1236	 */
1237
1238	register struct mbuf *mb0;
1239	register struct rtdetq *rte;
1240	register u_long hash;
1241	int hlen = ip->ip_hl << 2;
1242#ifdef UPCALL_TIMING
1243	struct timeval tp;
1244
1245	GET_TIME(tp);
1246#endif
1247
1248	mrtstat.mrts_no_route++;
1249	if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1250	    log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1251		(u_long)ntohl(ip->ip_src.s_addr),
1252		(u_long)ntohl(ip->ip_dst.s_addr));
1253
1254	/*
1255	 * Allocate mbufs early so that we don't do extra work if we are
1256	 * just going to fail anyway.  Make sure to pullup the header so
1257	 * that other people can't step on it.
1258	 */
1259	rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1260	if (rte == NULL) {
1261	    splx(s);
1262	    return ENOBUFS;
1263	}
1264	mb0 = m_copy(m, 0, M_COPYALL);
1265	if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1266	    mb0 = m_pullup(mb0, hlen);
1267	if (mb0 == NULL) {
1268	    free(rte, M_MRTABLE);
1269	    splx(s);
1270	    return ENOBUFS;
1271	}
1272
1273	/* is there an upcall waiting for this packet? */
1274	hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1275	for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1276	    if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1277		(ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1278		(rt->mfc_stall != NULL))
1279		break;
1280	}
1281
1282	if (rt == NULL) {
1283	    int i;
1284	    struct igmpmsg *im;
1285
1286	    /* no upcall, so make a new entry */
1287	    rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1288	    if (rt == NULL) {
1289		free(rte, M_MRTABLE);
1290		m_freem(mb0);
1291		splx(s);
1292		return ENOBUFS;
1293	    }
1294	    /* Make a copy of the header to send to the user level process */
1295	    mm = m_copy(mb0, 0, hlen);
1296	    if (mm == NULL) {
1297		free(rte, M_MRTABLE);
1298		m_freem(mb0);
1299		free(rt, M_MRTABLE);
1300		splx(s);
1301		return ENOBUFS;
1302	    }
1303
1304	    /*
1305	     * Send message to routing daemon to install
1306	     * a route into the kernel table
1307	     */
1308	    k_igmpsrc.sin_addr = ip->ip_src;
1309
1310	    im = mtod(mm, struct igmpmsg *);
1311	    im->im_msgtype	= IGMPMSG_NOCACHE;
1312	    im->im_mbz		= 0;
1313
1314	    mrtstat.mrts_upcalls++;
1315
1316	    if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1317		log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1318		++mrtstat.mrts_upq_sockfull;
1319		free(rte, M_MRTABLE);
1320		m_freem(mb0);
1321		free(rt, M_MRTABLE);
1322		splx(s);
1323		return ENOBUFS;
1324	    }
1325
1326	    /* insert new entry at head of hash chain */
1327	    rt->mfc_origin.s_addr     = ip->ip_src.s_addr;
1328	    rt->mfc_mcastgrp.s_addr   = ip->ip_dst.s_addr;
1329	    rt->mfc_expire	      = UPCALL_EXPIRE;
1330	    nexpire[hash]++;
1331	    for (i = 0; i < numvifs; i++)
1332		rt->mfc_ttls[i] = 0;
1333	    rt->mfc_parent = -1;
1334
1335	    /* link into table */
1336	    rt->mfc_next   = mfctable[hash];
1337	    mfctable[hash] = rt;
1338	    rt->mfc_stall = rte;
1339
1340	} else {
1341	    /* determine if q has overflowed */
1342	    int npkts = 0;
1343	    struct rtdetq **p;
1344
1345	    for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1346		npkts++;
1347
1348	    if (npkts > MAX_UPQ) {
1349		mrtstat.mrts_upq_ovflw++;
1350		free(rte, M_MRTABLE);
1351		m_freem(mb0);
1352		splx(s);
1353		return 0;
1354	    }
1355
1356	    /* Add this entry to the end of the queue */
1357	    *p = rte;
1358	}
1359
1360	rte->m 			= mb0;
1361	rte->ifp 		= ifp;
1362#ifdef UPCALL_TIMING
1363	rte->t			= tp;
1364#endif
1365	rte->next		= NULL;
1366
1367	splx(s);
1368
1369	return 0;
1370    }
1371}
1372
1373#ifndef MROUTE_KLD
1374int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
1375		   struct ip_moptions *) = X_ip_mforward;
1376#endif
1377
1378/*
1379 * Clean up the cache entry if upcall is not serviced
1380 */
1381static void
1382expire_upcalls(void *unused)
1383{
1384    struct rtdetq *rte;
1385    struct mfc *mfc, **nptr;
1386    int i;
1387    int s;
1388
1389    s = splnet();
1390    for (i = 0; i < MFCTBLSIZ; i++) {
1391	if (nexpire[i] == 0)
1392	    continue;
1393	nptr = &mfctable[i];
1394	for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1395	    /*
1396	     * Skip real cache entries
1397	     * Make sure it wasn't marked to not expire (shouldn't happen)
1398	     * If it expires now
1399	     */
1400	    if (mfc->mfc_stall != NULL &&
1401	        mfc->mfc_expire != 0 &&
1402		--mfc->mfc_expire == 0) {
1403		if (mrtdebug & DEBUG_EXPIRE)
1404		    log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1405			(u_long)ntohl(mfc->mfc_origin.s_addr),
1406			(u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1407		/*
1408		 * drop all the packets
1409		 * free the mbuf with the pkt, if, timing info
1410		 */
1411		for (rte = mfc->mfc_stall; rte; ) {
1412		    struct rtdetq *n = rte->next;
1413
1414		    m_freem(rte->m);
1415		    free(rte, M_MRTABLE);
1416		    rte = n;
1417		}
1418		++mrtstat.mrts_cache_cleanups;
1419		nexpire[i]--;
1420
1421		*nptr = mfc->mfc_next;
1422		free(mfc, M_MRTABLE);
1423	    } else {
1424		nptr = &mfc->mfc_next;
1425	    }
1426	}
1427    }
1428    splx(s);
1429    expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1430}
1431
1432/*
1433 * Packet forwarding routine once entry in the cache is made
1434 */
1435static int
1436ip_mdq(m, ifp, rt, xmt_vif)
1437    register struct mbuf *m;
1438    register struct ifnet *ifp;
1439    register struct mfc *rt;
1440    register vifi_t xmt_vif;
1441{
1442    register struct ip  *ip = mtod(m, struct ip *);
1443    register vifi_t vifi;
1444    register struct vif *vifp;
1445    register int plen = ip->ip_len;
1446
1447/*
1448 * Macro to send packet on vif.  Since RSVP packets don't get counted on
1449 * input, they shouldn't get counted on output, so statistics keeping is
1450 * separate.
1451 */
1452#define MC_SEND(ip,vifp,m) {                             \
1453                if ((vifp)->v_flags & VIFF_TUNNEL)  	 \
1454                    encap_send((ip), (vifp), (m));       \
1455                else                                     \
1456                    phyint_send((ip), (vifp), (m));      \
1457}
1458
1459    /*
1460     * If xmt_vif is not -1, send on only the requested vif.
1461     *
1462     * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1463     */
1464    if (xmt_vif < numvifs) {
1465	MC_SEND(ip, viftable + xmt_vif, m);
1466	return 1;
1467    }
1468
1469    /*
1470     * Don't forward if it didn't arrive from the parent vif for its origin.
1471     */
1472    vifi = rt->mfc_parent;
1473    if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1474	/* came in the wrong interface */
1475	if (mrtdebug & DEBUG_FORWARD)
1476	    log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1477		(void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1478	++mrtstat.mrts_wrong_if;
1479	++rt->mfc_wrong_if;
1480	/*
1481	 * If we are doing PIM assert processing, and we are forwarding
1482	 * packets on this interface, and it is a broadcast medium
1483	 * interface (and not a tunnel), send a message to the routing daemon.
1484	 */
1485	if (pim_assert && rt->mfc_ttls[vifi] &&
1486		(ifp->if_flags & IFF_BROADCAST) &&
1487		!(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1488	    struct sockaddr_in k_igmpsrc;
1489	    struct mbuf *mm;
1490	    struct igmpmsg *im;
1491	    int hlen = ip->ip_hl << 2;
1492	    struct timeval now;
1493	    register u_long delta;
1494
1495	    GET_TIME(now);
1496
1497	    TV_DELTA(rt->mfc_last_assert, now, delta);
1498
1499	    if (delta > ASSERT_MSG_TIME) {
1500		mm = m_copy(m, 0, hlen);
1501		if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1502		    mm = m_pullup(mm, hlen);
1503		if (mm == NULL) {
1504		    return ENOBUFS;
1505		}
1506
1507		rt->mfc_last_assert = now;
1508
1509		im = mtod(mm, struct igmpmsg *);
1510		im->im_msgtype	= IGMPMSG_WRONGVIF;
1511		im->im_mbz		= 0;
1512		im->im_vif		= vifi;
1513
1514		k_igmpsrc.sin_addr = im->im_src;
1515
1516		socket_send(ip_mrouter, mm, &k_igmpsrc);
1517	    }
1518	}
1519	return 0;
1520    }
1521
1522    /* If I sourced this packet, it counts as output, else it was input. */
1523    if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1524	viftable[vifi].v_pkt_out++;
1525	viftable[vifi].v_bytes_out += plen;
1526    } else {
1527	viftable[vifi].v_pkt_in++;
1528	viftable[vifi].v_bytes_in += plen;
1529    }
1530    rt->mfc_pkt_cnt++;
1531    rt->mfc_byte_cnt += plen;
1532
1533    /*
1534     * For each vif, decide if a copy of the packet should be forwarded.
1535     * Forward if:
1536     *		- the ttl exceeds the vif's threshold
1537     *		- there are group members downstream on interface
1538     */
1539    for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1540	if ((rt->mfc_ttls[vifi] > 0) &&
1541	    (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1542	    vifp->v_pkt_out++;
1543	    vifp->v_bytes_out += plen;
1544	    MC_SEND(ip, vifp, m);
1545	}
1546
1547    return 0;
1548}
1549
1550/*
1551 * check if a vif number is legal/ok. This is used by ip_output, to export
1552 * numvifs there,
1553 */
1554static int
1555X_legal_vif_num(vif)
1556    int vif;
1557{
1558    if (vif >= 0 && vif < numvifs)
1559       return(1);
1560    else
1561       return(0);
1562}
1563
1564#ifndef MROUTE_KLD
1565int (*legal_vif_num)(int) = X_legal_vif_num;
1566#endif
1567
1568/*
1569 * Return the local address used by this vif
1570 */
1571static u_long
1572X_ip_mcast_src(vifi)
1573    int vifi;
1574{
1575    if (vifi >= 0 && vifi < numvifs)
1576	return viftable[vifi].v_lcl_addr.s_addr;
1577    else
1578	return INADDR_ANY;
1579}
1580
1581#ifndef MROUTE_KLD
1582u_long (*ip_mcast_src)(int) = X_ip_mcast_src;
1583#endif
1584
1585static void
1586phyint_send(ip, vifp, m)
1587    struct ip *ip;
1588    struct vif *vifp;
1589    struct mbuf *m;
1590{
1591    register struct mbuf *mb_copy;
1592    register int hlen = ip->ip_hl << 2;
1593
1594    /*
1595     * Make a new reference to the packet; make sure that
1596     * the IP header is actually copied, not just referenced,
1597     * so that ip_output() only scribbles on the copy.
1598     */
1599    mb_copy = m_copy(m, 0, M_COPYALL);
1600    if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1601	mb_copy = m_pullup(mb_copy, hlen);
1602    if (mb_copy == NULL)
1603	return;
1604
1605    if (vifp->v_rate_limit == 0)
1606	tbf_send_packet(vifp, mb_copy);
1607    else
1608	tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1609}
1610
1611static void
1612encap_send(ip, vifp, m)
1613    register struct ip *ip;
1614    register struct vif *vifp;
1615    register struct mbuf *m;
1616{
1617    register struct mbuf *mb_copy;
1618    register struct ip *ip_copy;
1619    register int i, len = ip->ip_len;
1620
1621    /*
1622     * copy the old packet & pullup its IP header into the
1623     * new mbuf so we can modify it.  Try to fill the new
1624     * mbuf since if we don't the ethernet driver will.
1625     */
1626    MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1627    if (mb_copy == NULL)
1628	return;
1629    mb_copy->m_data += max_linkhdr;
1630    mb_copy->m_len = sizeof(multicast_encap_iphdr);
1631
1632    if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1633	m_freem(mb_copy);
1634	return;
1635    }
1636    i = MHLEN - M_LEADINGSPACE(mb_copy);
1637    if (i > len)
1638	i = len;
1639    mb_copy = m_pullup(mb_copy, i);
1640    if (mb_copy == NULL)
1641	return;
1642    mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1643
1644    /*
1645     * fill in the encapsulating IP header.
1646     */
1647    ip_copy = mtod(mb_copy, struct ip *);
1648    *ip_copy = multicast_encap_iphdr;
1649#ifdef RANDOM_IP_ID
1650    ip_copy->ip_id = ip_randomid();
1651#else
1652    ip_copy->ip_id = htons(ip_id++);
1653#endif
1654    ip_copy->ip_len += len;
1655    ip_copy->ip_src = vifp->v_lcl_addr;
1656    ip_copy->ip_dst = vifp->v_rmt_addr;
1657
1658    /*
1659     * turn the encapsulated IP header back into a valid one.
1660     */
1661    ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1662    --ip->ip_ttl;
1663    HTONS(ip->ip_len);
1664    HTONS(ip->ip_off);
1665    ip->ip_sum = 0;
1666    mb_copy->m_data += sizeof(multicast_encap_iphdr);
1667    ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1668    mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1669
1670    if (vifp->v_rate_limit == 0)
1671	tbf_send_packet(vifp, mb_copy);
1672    else
1673	tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1674}
1675
1676/*
1677 * Token bucket filter module
1678 */
1679
1680static void
1681tbf_control(vifp, m, ip, p_len)
1682	register struct vif *vifp;
1683	register struct mbuf *m;
1684	register struct ip *ip;
1685	register u_long p_len;
1686{
1687    register struct tbf *t = vifp->v_tbf;
1688
1689    if (p_len > MAX_BKT_SIZE) {
1690	/* drop if packet is too large */
1691	mrtstat.mrts_pkt2large++;
1692	m_freem(m);
1693	return;
1694    }
1695
1696    tbf_update_tokens(vifp);
1697
1698    /* if there are enough tokens,
1699     * and the queue is empty,
1700     * send this packet out
1701     */
1702
1703    if (t->tbf_q_len == 0) {
1704	/* queue empty, send packet if enough tokens */
1705	if (p_len <= t->tbf_n_tok) {
1706	    t->tbf_n_tok -= p_len;
1707	    tbf_send_packet(vifp, m);
1708	} else {
1709	    /* queue packet and timeout till later */
1710	    tbf_queue(vifp, m);
1711	    timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1712	}
1713    } else if (t->tbf_q_len < t->tbf_max_q_len) {
1714	/* finite queue length, so queue pkts and process queue */
1715	tbf_queue(vifp, m);
1716	tbf_process_q(vifp);
1717    } else {
1718	/* queue length too much, try to dq and queue and process */
1719	if (!tbf_dq_sel(vifp, ip)) {
1720	    mrtstat.mrts_q_overflow++;
1721	    m_freem(m);
1722	    return;
1723	} else {
1724	    tbf_queue(vifp, m);
1725	    tbf_process_q(vifp);
1726	}
1727    }
1728    return;
1729}
1730
1731/*
1732 * adds a packet to the queue at the interface
1733 */
1734static void
1735tbf_queue(vifp, m)
1736	register struct vif *vifp;
1737	register struct mbuf *m;
1738{
1739    register int s = splnet();
1740    register struct tbf *t = vifp->v_tbf;
1741
1742    if (t->tbf_t == NULL) {
1743	/* Queue was empty */
1744	t->tbf_q = m;
1745    } else {
1746	/* Insert at tail */
1747	t->tbf_t->m_act = m;
1748    }
1749
1750    /* Set new tail pointer */
1751    t->tbf_t = m;
1752
1753#ifdef DIAGNOSTIC
1754    /* Make sure we didn't get fed a bogus mbuf */
1755    if (m->m_act)
1756	panic("tbf_queue: m_act");
1757#endif
1758    m->m_act = NULL;
1759
1760    t->tbf_q_len++;
1761
1762    splx(s);
1763}
1764
1765
1766/*
1767 * processes the queue at the interface
1768 */
1769static void
1770tbf_process_q(vifp)
1771    register struct vif *vifp;
1772{
1773    register struct mbuf *m;
1774    register int len;
1775    register int s = splnet();
1776    register struct tbf *t = vifp->v_tbf;
1777
1778    /* loop through the queue at the interface and send as many packets
1779     * as possible
1780     */
1781    while (t->tbf_q_len > 0) {
1782	m = t->tbf_q;
1783
1784	len = mtod(m, struct ip *)->ip_len;
1785
1786	/* determine if the packet can be sent */
1787	if (len <= t->tbf_n_tok) {
1788	    /* if so,
1789	     * reduce no of tokens, dequeue the packet,
1790	     * send the packet.
1791	     */
1792	    t->tbf_n_tok -= len;
1793
1794	    t->tbf_q = m->m_act;
1795	    if (--t->tbf_q_len == 0)
1796		t->tbf_t = NULL;
1797
1798	    m->m_act = NULL;
1799	    tbf_send_packet(vifp, m);
1800
1801	} else break;
1802    }
1803    splx(s);
1804}
1805
1806static void
1807tbf_reprocess_q(xvifp)
1808	void *xvifp;
1809{
1810    register struct vif *vifp = xvifp;
1811    if (ip_mrouter == NULL)
1812	return;
1813
1814    tbf_update_tokens(vifp);
1815
1816    tbf_process_q(vifp);
1817
1818    if (vifp->v_tbf->tbf_q_len)
1819	timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1820}
1821
1822/* function that will selectively discard a member of the queue
1823 * based on the precedence value and the priority
1824 */
1825static int
1826tbf_dq_sel(vifp, ip)
1827    register struct vif *vifp;
1828    register struct ip *ip;
1829{
1830    register int s = splnet();
1831    register u_int p;
1832    register struct mbuf *m, *last;
1833    register struct mbuf **np;
1834    register struct tbf *t = vifp->v_tbf;
1835
1836    p = priority(vifp, ip);
1837
1838    np = &t->tbf_q;
1839    last = NULL;
1840    while ((m = *np) != NULL) {
1841	if (p > priority(vifp, mtod(m, struct ip *))) {
1842	    *np = m->m_act;
1843	    /* If we're removing the last packet, fix the tail pointer */
1844	    if (m == t->tbf_t)
1845		t->tbf_t = last;
1846	    m_freem(m);
1847	    /* it's impossible for the queue to be empty, but
1848	     * we check anyway. */
1849	    if (--t->tbf_q_len == 0)
1850		t->tbf_t = NULL;
1851	    splx(s);
1852	    mrtstat.mrts_drop_sel++;
1853	    return(1);
1854	}
1855	np = &m->m_act;
1856	last = m;
1857    }
1858    splx(s);
1859    return(0);
1860}
1861
1862static void
1863tbf_send_packet(vifp, m)
1864    register struct vif *vifp;
1865    register struct mbuf *m;
1866{
1867    struct ip_moptions imo;
1868    int error;
1869    static struct route ro;
1870    int s = splnet();
1871
1872    if (vifp->v_flags & VIFF_TUNNEL) {
1873	/* If tunnel options */
1874	ip_output(m, (struct mbuf *)0, &vifp->v_route,
1875		  IP_FORWARDING, (struct ip_moptions *)0);
1876    } else {
1877	imo.imo_multicast_ifp  = vifp->v_ifp;
1878	imo.imo_multicast_ttl  = mtod(m, struct ip *)->ip_ttl - 1;
1879	imo.imo_multicast_loop = 1;
1880	imo.imo_multicast_vif  = -1;
1881
1882	/*
1883	 * Re-entrancy should not be a problem here, because
1884	 * the packets that we send out and are looped back at us
1885	 * should get rejected because they appear to come from
1886	 * the loopback interface, thus preventing looping.
1887	 */
1888	error = ip_output(m, (struct mbuf *)0, &ro,
1889			  IP_FORWARDING, &imo);
1890
1891	if (mrtdebug & DEBUG_XMIT)
1892	    log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1893		vifp - viftable, error);
1894    }
1895    splx(s);
1896}
1897
1898/* determine the current time and then
1899 * the elapsed time (between the last time and time now)
1900 * in milliseconds & update the no. of tokens in the bucket
1901 */
1902static void
1903tbf_update_tokens(vifp)
1904    register struct vif *vifp;
1905{
1906    struct timeval tp;
1907    register u_long tm;
1908    register int s = splnet();
1909    register struct tbf *t = vifp->v_tbf;
1910
1911    GET_TIME(tp);
1912
1913    TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1914
1915    /*
1916     * This formula is actually
1917     * "time in seconds" * "bytes/second".
1918     *
1919     * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1920     *
1921     * The (1000/1024) was introduced in add_vif to optimize
1922     * this divide into a shift.
1923     */
1924    t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1925    t->tbf_last_pkt_t = tp;
1926
1927    if (t->tbf_n_tok > MAX_BKT_SIZE)
1928	t->tbf_n_tok = MAX_BKT_SIZE;
1929
1930    splx(s);
1931}
1932
1933static int
1934priority(vifp, ip)
1935    register struct vif *vifp;
1936    register struct ip *ip;
1937{
1938    register int prio;
1939
1940    /* temporary hack; may add general packet classifier some day */
1941
1942    /*
1943     * The UDP port space is divided up into four priority ranges:
1944     * [0, 16384)     : unclassified - lowest priority
1945     * [16384, 32768) : audio - highest priority
1946     * [32768, 49152) : whiteboard - medium priority
1947     * [49152, 65536) : video - low priority
1948     */
1949    if (ip->ip_p == IPPROTO_UDP) {
1950	struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1951	switch (ntohs(udp->uh_dport) & 0xc000) {
1952	    case 0x4000:
1953		prio = 70;
1954		break;
1955	    case 0x8000:
1956		prio = 60;
1957		break;
1958	    case 0xc000:
1959		prio = 55;
1960		break;
1961	    default:
1962		prio = 50;
1963		break;
1964	}
1965	if (tbfdebug > 1)
1966		log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio);
1967    } else {
1968	    prio = 50;
1969    }
1970    return prio;
1971}
1972
1973/*
1974 * End of token bucket filter modifications
1975 */
1976
1977int
1978ip_rsvp_vif_init(so, sopt)
1979	struct socket *so;
1980	struct sockopt *sopt;
1981{
1982    int error, i, s;
1983
1984    if (rsvpdebug)
1985	printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
1986	       so->so_type, so->so_proto->pr_protocol);
1987
1988    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1989	return EOPNOTSUPP;
1990
1991    /* Check mbuf. */
1992    error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1993    if (error)
1994	    return (error);
1995
1996    if (rsvpdebug)
1997	printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
1998
1999    s = splnet();
2000
2001    /* Check vif. */
2002    if (!legal_vif_num(i)) {
2003	splx(s);
2004	return EADDRNOTAVAIL;
2005    }
2006
2007    /* Check if socket is available. */
2008    if (viftable[i].v_rsvpd != NULL) {
2009	splx(s);
2010	return EADDRINUSE;
2011    }
2012
2013    viftable[i].v_rsvpd = so;
2014    /* This may seem silly, but we need to be sure we don't over-increment
2015     * the RSVP counter, in case something slips up.
2016     */
2017    if (!viftable[i].v_rsvp_on) {
2018	viftable[i].v_rsvp_on = 1;
2019	rsvp_on++;
2020    }
2021
2022    splx(s);
2023    return 0;
2024}
2025
2026int
2027ip_rsvp_vif_done(so, sopt)
2028	struct socket *so;
2029	struct sockopt *sopt;
2030{
2031	int error, i, s;
2032
2033	if (rsvpdebug)
2034		printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2035		       so->so_type, so->so_proto->pr_protocol);
2036
2037	if (so->so_type != SOCK_RAW ||
2038	    so->so_proto->pr_protocol != IPPROTO_RSVP)
2039		return EOPNOTSUPP;
2040
2041	error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2042	if (error)
2043		return (error);
2044
2045	s = splnet();
2046
2047	/* Check vif. */
2048	if (!legal_vif_num(i)) {
2049		splx(s);
2050		return EADDRNOTAVAIL;
2051	}
2052
2053	if (rsvpdebug)
2054		printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2055		       viftable[i].v_rsvpd, so);
2056
2057	viftable[i].v_rsvpd = NULL;
2058	/*
2059	 * This may seem silly, but we need to be sure we don't over-decrement
2060	 * the RSVP counter, in case something slips up.
2061	 */
2062	if (viftable[i].v_rsvp_on) {
2063		viftable[i].v_rsvp_on = 0;
2064		rsvp_on--;
2065	}
2066
2067	splx(s);
2068	return 0;
2069}
2070
2071void
2072ip_rsvp_force_done(so)
2073    struct socket *so;
2074{
2075    int vifi;
2076    register int s;
2077
2078    /* Don't bother if it is not the right type of socket. */
2079    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2080	return;
2081
2082    s = splnet();
2083
2084    /* The socket may be attached to more than one vif...this
2085     * is perfectly legal.
2086     */
2087    for (vifi = 0; vifi < numvifs; vifi++) {
2088	if (viftable[vifi].v_rsvpd == so) {
2089	    viftable[vifi].v_rsvpd = NULL;
2090	    /* This may seem silly, but we need to be sure we don't
2091	     * over-decrement the RSVP counter, in case something slips up.
2092	     */
2093	    if (viftable[vifi].v_rsvp_on) {
2094		viftable[vifi].v_rsvp_on = 0;
2095		rsvp_on--;
2096	    }
2097	}
2098    }
2099
2100    splx(s);
2101    return;
2102}
2103
2104void
2105rsvp_input(m, off)
2106	struct mbuf *m;
2107	int off;
2108{
2109    int vifi;
2110    register struct ip *ip = mtod(m, struct ip *);
2111    static struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2112    register int s;
2113    struct ifnet *ifp;
2114
2115    if (rsvpdebug)
2116	printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2117
2118    /* Can still get packets with rsvp_on = 0 if there is a local member
2119     * of the group to which the RSVP packet is addressed.  But in this
2120     * case we want to throw the packet away.
2121     */
2122    if (!rsvp_on) {
2123	m_freem(m);
2124	return;
2125    }
2126
2127    s = splnet();
2128
2129    if (rsvpdebug)
2130	printf("rsvp_input: check vifs\n");
2131
2132#ifdef DIAGNOSTIC
2133    if (!(m->m_flags & M_PKTHDR))
2134	    panic("rsvp_input no hdr");
2135#endif
2136
2137    ifp = m->m_pkthdr.rcvif;
2138    /* Find which vif the packet arrived on. */
2139    for (vifi = 0; vifi < numvifs; vifi++)
2140	if (viftable[vifi].v_ifp == ifp)
2141	    break;
2142
2143    if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2144	/*
2145	 * If the old-style non-vif-associated socket is set,
2146	 * then use it.  Otherwise, drop packet since there
2147	 * is no specific socket for this vif.
2148	 */
2149	if (ip_rsvpd != NULL) {
2150	    if (rsvpdebug)
2151		printf("rsvp_input: Sending packet up old-style socket\n");
2152	    rip_input(m, off);  /* xxx */
2153	} else {
2154	    if (rsvpdebug && vifi == numvifs)
2155		printf("rsvp_input: Can't find vif for packet.\n");
2156	    else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2157		printf("rsvp_input: No socket defined for vif %d\n",vifi);
2158	    m_freem(m);
2159	}
2160	splx(s);
2161	return;
2162    }
2163    rsvp_src.sin_addr = ip->ip_src;
2164
2165    if (rsvpdebug && m)
2166	printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2167	       m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2168
2169    if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2170	if (rsvpdebug)
2171	    printf("rsvp_input: Failed to append to socket\n");
2172    } else {
2173	if (rsvpdebug)
2174	    printf("rsvp_input: send packet up\n");
2175    }
2176
2177    splx(s);
2178}
2179
2180#ifdef MROUTE_KLD
2181
2182static int
2183ip_mroute_modevent(module_t mod, int type, void *unused)
2184{
2185	int s;
2186
2187	switch (type) {
2188		static u_long (*old_ip_mcast_src)(int);
2189		static int (*old_ip_mrouter_set)(struct socket *,
2190			struct sockopt *);
2191		static int (*old_ip_mrouter_get)(struct socket *,
2192			struct sockopt *);
2193		static int (*old_ip_mrouter_done)(void);
2194		static int (*old_ip_mforward)(struct ip *, struct ifnet *,
2195			struct mbuf *, struct ip_moptions *);
2196		static int (*old_mrt_ioctl)(int, caddr_t);
2197		static int (*old_legal_vif_num)(int);
2198
2199	case MOD_LOAD:
2200		s = splnet();
2201		/* XXX Protect against multiple loading */
2202		old_ip_mcast_src = ip_mcast_src;
2203		ip_mcast_src = X_ip_mcast_src;
2204		old_ip_mrouter_get = ip_mrouter_get;
2205		ip_mrouter_get = X_ip_mrouter_get;
2206		old_ip_mrouter_set = ip_mrouter_set;
2207		ip_mrouter_set = X_ip_mrouter_set;
2208		old_ip_mrouter_done = ip_mrouter_done;
2209		ip_mrouter_done = X_ip_mrouter_done;
2210		old_ip_mforward = ip_mforward;
2211		ip_mforward = X_ip_mforward;
2212		old_mrt_ioctl = mrt_ioctl;
2213		mrt_ioctl = X_mrt_ioctl;
2214		old_legal_vif_num = legal_vif_num;
2215		legal_vif_num = X_legal_vif_num;
2216
2217		splx(s);
2218		return 0;
2219
2220	case MOD_UNLOAD:
2221		if (ip_mrouter)
2222		  return EINVAL;
2223
2224		s = splnet();
2225		ip_mrouter_get = old_ip_mrouter_get;
2226		ip_mrouter_set = old_ip_mrouter_set;
2227		ip_mrouter_done = old_ip_mrouter_done;
2228		ip_mforward = old_ip_mforward;
2229		mrt_ioctl = old_mrt_ioctl;
2230		legal_vif_num = old_legal_vif_num;
2231		splx(s);
2232		return 0;
2233
2234	default:
2235		break;
2236	}
2237	return 0;
2238}
2239
2240static moduledata_t ip_mroutemod = {
2241	"ip_mroute",
2242	ip_mroute_modevent,
2243	0
2244};
2245DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2246
2247#endif /* MROUTE_KLD */
2248#endif /* MROUTING */
2249