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