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