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