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