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