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