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