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