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