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