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