Home | History | Annotate | Line # | Download | only in netinet
ip_mroute.c revision 1.12
      1 /*
      2  * Copyright (c) 1989 Stephen Deering
      3  * Copyright (c) 1992 Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Stephen Deering of Stanford University.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	from: @(#)ip_mroute.c	7.4 (Berkeley) 11/19/92
     38  *	$Id $
     39  */
     40 
     41 /*
     42  * Procedures for the kernel part of DVMRP,
     43  * a Distance-Vector Multicast Routing Protocol.
     44  * (See RFC-1075.)
     45  *
     46  * Written by David Waitzman, BBN Labs, August 1988.
     47  * Modified by Steve Deering, Stanford, February 1989.
     48  *
     49  * MROUTING 1.1
     50  */
     51 
     52 #ifndef MROUTING
     53 int	ip_mrtproto;				/* for netstat only */
     54 #else
     55 
     56 #include <sys/param.h>
     57 #include <sys/errno.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/malloc.h>
     60 #include <sys/mbuf.h>
     61 #include <sys/protosw.h>
     62 #include <sys/socket.h>
     63 #include <sys/socketvar.h>
     64 #include <sys/time.h>
     65 
     66 #include <net/if.h>
     67 #include <net/route.h>
     68 #include <net/raw_cb.h>
     69 
     70 #include <netinet/in.h>
     71 #include <netinet/in_systm.h>
     72 #include <netinet/ip.h>
     73 #include <netinet/in_pcb.h>
     74 #include <netinet/in_var.h>
     75 #include <netinet/ip_var.h>
     76 
     77 #include <netinet/igmp.h>
     78 #include <netinet/igmp_var.h>
     79 #include <netinet/ip_mroute.h>
     80 
     81 /* Static forwards */
     82 static	int ip_mrouter_init __P((struct socket *));
     83 static	int add_vif __P((struct vifctl *));
     84 static	int del_vif __P((vifi_t *vifip));
     85 static	int add_lgrp __P((struct lgrplctl *));
     86 static	int del_lgrp __P((struct lgrplctl *));
     87 static	int grplst_member __P((struct vif *, struct in_addr));
     88 static	u_long nethash __P((u_long in));
     89 static	int add_mrt __P((struct mrtctl *));
     90 static	int del_mrt __P((struct in_addr *));
     91 static	struct mrt *mrtfind __P((u_long));
     92 static	void phyint_send __P((struct ip *, struct vif *, struct mbuf *));
     93 static	void srcrt_send __P((struct ip *, struct vif *, struct mbuf *));
     94 static	void encap_send __P((struct ip *, struct vif *, struct mbuf *));
     95 static	void multiencap_decap __P((struct mbuf *, int hlen));
     96 
     97 #define	INSIZ	sizeof(struct in_addr)
     98 #define	same(a1, a2)	(bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
     99 #define	satosin(sa)	((struct sockaddr_in *)(sa))
    100 
    101 /*
    102  * Globals.  All but ip_mrouter and ip_mrtproto could be static,
    103  * except for netstat or debugging purposes.
    104  */
    105 struct	socket *ip_mrouter = NULL;
    106 int	ip_mrtproto = IGMP_DVMRP;		/* for netstat only */
    107 
    108 struct	mrt *mrttable[MRTHASHSIZ];
    109 struct	vif viftable[MAXVIFS];
    110 struct	mrtstat	mrtstat;
    111 
    112 /*
    113  * 'Interfaces' associated with decapsulator (so we can tell
    114  * packets that went through it from ones that get reflected
    115  * by a broken gateway).  These interfaces are never linked into
    116  * the system ifnet list & no routes point to them.  I.e., packets
    117  * can't be sent this way.  They only exist as a placeholder for
    118  * multicast source verification.
    119  */
    120 struct ifnet multicast_decap_if[MAXVIFS];
    121 
    122 #define	ENCAP_TTL 64
    123 #define	ENCAP_PROTO 4
    124 
    125 /* prototype IP hdr for encapsulated packets */
    126 struct ip multicast_encap_iphdr = {
    127 #if defined(ultrix) || defined(i386)
    128 	sizeof(struct ip) >> 2, IPVERSION,
    129 #else
    130 	IPVERSION, sizeof(struct ip) >> 2,
    131 #endif
    132 	0,				/* tos */
    133 	sizeof(struct ip),		/* total length */
    134 	0,				/* id */
    135 	0,				/* frag offset */
    136 	ENCAP_TTL, ENCAP_PROTO,
    137 	0,				/* checksum */
    138 };
    139 
    140 /*
    141  * Private variables.
    142  */
    143 static	vifi_t numvifs = 0;
    144 static	struct mrt *cached_mrt = NULL;
    145 static	u_long cached_origin;
    146 static	u_long cached_originmask;
    147 
    148 static void (*encap_oldrawip)();
    149 
    150 /*
    151  * one-back cache used by multiencap_decap to locate a tunnel's vif
    152  * given a datagram's src ip address.
    153  */
    154 static u_long last_encap_src;
    155 static struct vif *last_encap_vif;
    156 
    157 /*
    158  * A simple hash function: returns MRTHASHMOD of the low-order octet of
    159  * the argument's network or subnet number.
    160  */
    161 static u_long
    162 nethash(n)
    163 	u_long n;
    164 {
    165 	struct in_addr in;
    166 
    167 	in.s_addr = n;
    168 	n = in_netof(in);
    169 	while ((n & 0xff) == 0)
    170 		n >>= 8;
    171 	return (MRTHASHMOD(n));
    172 }
    173 
    174 /*
    175  * this is a direct-mapped cache used to speed the mapping from a
    176  * datagram source address to the associated multicast route.  Note
    177  * that unlike mrttable, the hash is on IP address, not IP net number.
    178  */
    179 #define	MSRCHASHSIZ	1024
    180 #define	MSRCHASH(a)	((((a) >> 20) ^ ((a) >> 10) ^ (a)) & (MSRCHASHSIZ - 1))
    181 struct mrt *mrtsrchash[MSRCHASHSIZ];
    182 
    183 /*
    184  * Find a route for a given origin IP address.
    185  */
    186 #define	MRTFIND(o, rt) { \
    187 	register u_int _mrhash = o; \
    188 	_mrhash = MSRCHASH(_mrhash); \
    189 	++mrtstat.mrts_mrt_lookups; \
    190 	rt = mrtsrchash[_mrhash]; \
    191 	if (rt == NULL || \
    192 	    (o & rt->mrt_originmask.s_addr) != rt->mrt_origin.s_addr) \
    193 		if ((rt = mrtfind(o)) != NULL) \
    194 		    mrtsrchash[_mrhash] = rt; \
    195 }
    196 
    197 static struct mrt *
    198 mrtfind(origin)
    199 	u_long origin;
    200 {
    201 	register struct mrt *rt;
    202 	register u_int hash;
    203 
    204 	mrtstat.mrts_mrt_misses++;
    205 
    206 	hash = nethash(origin);
    207 	for (rt = mrttable[hash]; rt; rt = rt->mrt_next) {
    208 		if ((origin & rt->mrt_originmask.s_addr) ==
    209 		    rt->mrt_origin.s_addr)
    210 			return (rt);
    211 	}
    212 	return (NULL);
    213 }
    214 
    215 /*
    216  * Handle DVMRP setsockopt commands to modify the multicast routing tables.
    217  */
    218 int
    219 ip_mrouter_cmd(cmd, so, m)
    220 	register int cmd;
    221 	register struct socket *so;
    222 	register struct mbuf *m;
    223 {
    224 	register int error = 0;
    225 
    226 	if (cmd != DVMRP_INIT && so != ip_mrouter)
    227 		error = EACCES;
    228 	else switch (cmd) {
    229 
    230 	case DVMRP_INIT:
    231 		error = ip_mrouter_init(so);
    232 		break;
    233 
    234 	case DVMRP_DONE:
    235 		error = ip_mrouter_done();
    236 		break;
    237 
    238 	case DVMRP_ADD_VIF:
    239 		if (m == NULL || m->m_len < sizeof(struct vifctl))
    240 			error = EINVAL;
    241 		else
    242 			error = add_vif(mtod(m, struct vifctl *));
    243 		break;
    244 
    245 	case DVMRP_DEL_VIF:
    246 		if (m == NULL || m->m_len < sizeof(short))
    247 			error = EINVAL;
    248 		else
    249 			error = del_vif(mtod(m, vifi_t *));
    250 		break;
    251 
    252 	case DVMRP_ADD_LGRP:
    253 		if (m == NULL || m->m_len < sizeof(struct lgrplctl))
    254 			error = EINVAL;
    255 		else
    256 			error = add_lgrp(mtod(m, struct lgrplctl *));
    257 		break;
    258 
    259 	case DVMRP_DEL_LGRP:
    260 		if (m == NULL || m->m_len < sizeof(struct lgrplctl))
    261 			error = EINVAL;
    262 		else
    263 			error = del_lgrp(mtod(m, struct lgrplctl *));
    264 		break;
    265 
    266 	case DVMRP_ADD_MRT:
    267 		if (m == NULL || m->m_len < sizeof(struct mrtctl))
    268 			error = EINVAL;
    269 		else
    270 			error = add_mrt(mtod(m, struct mrtctl *));
    271 		break;
    272 
    273 	case DVMRP_DEL_MRT:
    274 		if (m == NULL || m->m_len < sizeof(struct in_addr))
    275 			error = EINVAL;
    276 		else
    277 			error = del_mrt(mtod(m, struct in_addr *));
    278 		break;
    279 
    280 	default:
    281 		error = EOPNOTSUPP;
    282 		break;
    283 	}
    284 	return (error);
    285 }
    286 
    287 /*
    288  * Enable multicast routing
    289  */
    290 static int
    291 ip_mrouter_init(so)
    292 	register struct socket *so;
    293 {
    294 	if (so->so_type != SOCK_RAW ||
    295 	    so->so_proto->pr_protocol != IPPROTO_IGMP)
    296 		return (EOPNOTSUPP);
    297 
    298 	if (ip_mrouter != NULL)
    299 		return (EADDRINUSE);
    300 
    301 	ip_mrouter = so;
    302 
    303 	return (0);
    304 }
    305 
    306 /*
    307  * Disable multicast routing
    308  */
    309 int
    310 ip_mrouter_done()
    311 {
    312 	register vifi_t vifi;
    313 	register int i;
    314 	register struct ifnet *ifp;
    315 	register int s;
    316 	struct ifreq ifr;
    317 
    318 	s = splnet();
    319 
    320 	/*
    321 	 * For each phyint in use, free its local group list and
    322 	 * disable promiscuous reception of all IP multicasts.
    323 	 */
    324 	for (vifi = 0; vifi < numvifs; vifi++) {
    325 		if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
    326 		    !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
    327 			if (viftable[vifi].v_lcl_grps)
    328 				free(viftable[vifi].v_lcl_grps, M_MRTABLE);
    329 			satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    330 			satosin(&ifr.ifr_addr)->sin_addr.s_addr = INADDR_ANY;
    331 			ifp = viftable[vifi].v_ifp;
    332 			(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
    333 		}
    334 	}
    335 	bzero((caddr_t)viftable, sizeof(viftable));
    336 	numvifs = 0;
    337 
    338 	/*
    339 	 * Free any multicast route entries.
    340 	 */
    341 	for (i = 0; i < MRTHASHSIZ; i++)
    342 		if (mrttable[i])
    343 			free(mrttable[i], M_MRTABLE);
    344 	bzero((caddr_t)mrttable, sizeof(mrttable));
    345 	bzero((caddr_t)mrtsrchash, sizeof(mrtsrchash));
    346 
    347 	ip_mrouter = NULL;
    348 
    349 	splx(s);
    350 	return (0);
    351 }
    352 
    353 /*
    354  * Add a vif to the vif table
    355  */
    356 static int
    357 add_vif(vifcp)
    358 	register struct vifctl *vifcp;
    359 {
    360 	register struct vif *vifp = viftable + vifcp->vifc_vifi;
    361 	register struct ifaddr *ifa;
    362 	register struct ifnet *ifp;
    363 	struct ifreq ifr;
    364 	register int error, s;
    365 	static struct sockaddr_in sin = { sizeof(sin), AF_INET };
    366 
    367 	if (vifcp->vifc_vifi >= MAXVIFS)
    368 		return (EINVAL);
    369 	if (vifp->v_lcl_addr.s_addr != 0)
    370 		return (EADDRINUSE);
    371 
    372 	/* Find the interface with an address in AF_INET family */
    373 	sin.sin_addr = vifcp->vifc_lcl_addr;
    374 	ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
    375 	if (ifa == 0)
    376 		return (EADDRNOTAVAIL);
    377 	ifp = ifa->ifa_ifp;
    378 
    379 	if (vifcp->vifc_flags & VIFF_TUNNEL) {
    380 		if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
    381 			/*
    382 			 * An encapsulating tunnel is wanted.  If we
    383 			 * haven't done so already, put our decap routine
    384 			 * in front of raw_input so we have a chance to
    385 			 * decapsulate incoming packets.  Then set the
    386 			 * arrival 'interface' to be the decapsulator.
    387 			 */
    388 			if (encap_oldrawip == 0) {
    389 				extern struct protosw inetsw[];
    390 				extern u_char ip_protox[];
    391 				register int pr = ip_protox[ENCAP_PROTO];
    392 
    393 				encap_oldrawip = inetsw[pr].pr_input;
    394 				inetsw[pr].pr_input = multiencap_decap;
    395 				for (s = 0; s < MAXVIFS; ++s) {
    396 					multicast_decap_if[s].if_name =
    397 						"mdecap";
    398 					multicast_decap_if[s].if_unit = s;
    399 				}
    400 			}
    401 			ifp = &multicast_decap_if[vifcp->vifc_vifi];
    402 		} else {
    403 			ifp = 0;
    404 		}
    405 	} else {
    406 		/* Make sure the interface supports multicast */
    407 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
    408 			return EOPNOTSUPP;
    409 
    410 		/*
    411 		 * Enable promiscuous reception of all
    412 		 * IP multicasts from the if
    413 		 */
    414 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
    415 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr =
    416 			INADDR_ANY;
    417 		s = splnet();
    418 		error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
    419 		splx(s);
    420 		if (error)
    421 			return error;
    422 	}
    423 
    424 	s = splnet();
    425 	vifp->v_flags = vifcp->vifc_flags;
    426 	vifp->v_threshold = vifcp->vifc_threshold;
    427 	vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
    428 	vifp->v_ifp = ifp;
    429 	vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
    430 	splx(s);
    431 
    432 	/* Adjust numvifs up if the vifi is higher than numvifs */
    433 	if (numvifs <= vifcp->vifc_vifi)
    434 		numvifs = vifcp->vifc_vifi + 1;
    435 
    436 	splx(s);
    437 	return (0);
    438 }
    439 
    440 /*
    441  * Delete a vif from the vif table
    442  */
    443 static int
    444 del_vif(vifip)
    445 	register vifi_t *vifip;
    446 {
    447 	register struct vif *vifp = viftable + *vifip;
    448 	register struct ifnet *ifp;
    449 	register int i, s;
    450 	struct ifreq ifr;
    451 
    452 	if (*vifip >= numvifs)
    453 		return (EINVAL);
    454 	if (vifp->v_lcl_addr.s_addr == 0)
    455 		return (EADDRNOTAVAIL);
    456 
    457 	s = splnet();
    458 
    459 	if (!(vifp->v_flags & VIFF_TUNNEL)) {
    460 		if (vifp->v_lcl_grps)
    461 			free(vifp->v_lcl_grps, M_MRTABLE);
    462 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    463 		satosin(&ifr.ifr_addr)->sin_addr.s_addr = INADDR_ANY;
    464 		ifp = vifp->v_ifp;
    465 		(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
    466 	}
    467 	if (vifp == last_encap_vif) {
    468 		last_encap_vif = 0;
    469 		last_encap_src = 0;
    470 	}
    471 	bzero((caddr_t)vifp, sizeof (*vifp));
    472 
    473 	/* Adjust numvifs down */
    474 	for (i = numvifs - 1; i >= 0; i--)
    475 		if (viftable[i].v_lcl_addr.s_addr != 0)
    476 			break;
    477 	numvifs = i + 1;
    478 
    479 	splx(s);
    480 	return (0);
    481 }
    482 
    483 /*
    484  * Add the multicast group in the lgrpctl to the list of local multicast
    485  * group memberships associated with the vif indexed by gcp->lgc_vifi.
    486  */
    487 static int
    488 add_lgrp(gcp)
    489 	register struct lgrplctl *gcp;
    490 {
    491 	register struct vif *vifp;
    492 	register int s;
    493 
    494 	if (gcp->lgc_vifi >= numvifs)
    495 		return (EINVAL);
    496 
    497 	vifp = viftable + gcp->lgc_vifi;
    498 	if (vifp->v_lcl_addr.s_addr == 0 || (vifp->v_flags & VIFF_TUNNEL))
    499 		return (EADDRNOTAVAIL);
    500 
    501 	/* If not enough space in existing list, allocate a larger one */
    502 	s = splnet();
    503 	if (vifp->v_lcl_grps_n + 1 >= vifp->v_lcl_grps_max) {
    504 		register int num;
    505 		register struct in_addr *ip;
    506 
    507 		num = vifp->v_lcl_grps_max;
    508 		if (num <= 0)
    509 			num = 32;	/* initial number */
    510 		else
    511 			num += num;	/* double last number */
    512 		ip = (struct in_addr *)malloc(num * sizeof(*ip),
    513 		    M_MRTABLE, M_NOWAIT);
    514 		if (ip == NULL) {
    515 			splx(s);
    516 			return (ENOBUFS);
    517 		}
    518 
    519 		bzero((caddr_t)ip, num * sizeof(*ip));	/* XXX paranoid */
    520 		bcopy((caddr_t)vifp->v_lcl_grps, (caddr_t)ip,
    521 		    vifp->v_lcl_grps_n * sizeof(*ip));
    522 
    523 		vifp->v_lcl_grps_max = num;
    524 		if (vifp->v_lcl_grps)
    525 			free(vifp->v_lcl_grps, M_MRTABLE);
    526 		vifp->v_lcl_grps = ip;
    527 	}
    528 
    529 	vifp->v_lcl_grps[vifp->v_lcl_grps_n++] = gcp->lgc_gaddr;
    530 
    531 	if (gcp->lgc_gaddr.s_addr == vifp->v_cached_group)
    532 		vifp->v_cached_result = 1;
    533 
    534 	splx(s);
    535 	return (0);
    536 }
    537 
    538 /*
    539  * Delete the the local multicast group associated with the vif
    540  * indexed by gcp->lgc_vifi.
    541  */
    542 static int
    543 del_lgrp(gcp)
    544 	register struct lgrplctl *gcp;
    545 {
    546 	register struct vif *vifp;
    547 	register int i, error, s;
    548 
    549 	if (gcp->lgc_vifi >= numvifs)
    550 		return (EINVAL);
    551 	vifp = viftable + gcp->lgc_vifi;
    552 	if (vifp->v_lcl_addr.s_addr == 0 || (vifp->v_flags & VIFF_TUNNEL))
    553 		return (EADDRNOTAVAIL);
    554 
    555 	s = splnet();
    556 
    557 	if (gcp->lgc_gaddr.s_addr == vifp->v_cached_group)
    558 		vifp->v_cached_result = 0;
    559 
    560 	error = EADDRNOTAVAIL;
    561 	for (i = 0; i < vifp->v_lcl_grps_n; ++i)
    562 		if (same(&gcp->lgc_gaddr, &vifp->v_lcl_grps[i])) {
    563 			error = 0;
    564 			--vifp->v_lcl_grps_n;
    565 			for (; i < vifp->v_lcl_grps_n; ++i)
    566 				vifp->v_lcl_grps[i] = vifp->v_lcl_grps[i + 1];
    567 			error = 0;
    568 			break;
    569 		}
    570 
    571 	splx(s);
    572 	return (error);
    573 }
    574 
    575 /*
    576  * Return 1 if gaddr is a member of the local group list for vifp.
    577  */
    578 static int
    579 grplst_member(vifp, gaddr)
    580 	register struct vif *vifp;
    581 	struct in_addr gaddr;
    582 {
    583 	register int i, s;
    584 	register u_long addr;
    585 
    586 	mrtstat.mrts_grp_lookups++;
    587 
    588 	addr = gaddr.s_addr;
    589 	if (addr == vifp->v_cached_group)
    590 		return (vifp->v_cached_result);
    591 
    592 	mrtstat.mrts_grp_misses++;
    593 
    594 	for (i = 0; i < vifp->v_lcl_grps_n; ++i)
    595 		if (addr == vifp->v_lcl_grps[i].s_addr) {
    596 			s = splnet();
    597 			vifp->v_cached_group = addr;
    598 			vifp->v_cached_result = 1;
    599 			splx(s);
    600 			return (1);
    601 		}
    602 	s = splnet();
    603 	vifp->v_cached_group = addr;
    604 	vifp->v_cached_result = 0;
    605 	splx(s);
    606 	return (0);
    607 }
    608 
    609 /*
    610  * Add an mrt entry
    611  */
    612 static int
    613 add_mrt(mrtcp)
    614 	register struct mrtctl *mrtcp;
    615 {
    616 	struct mrt *rt;
    617 	u_long hash;
    618 	int s;
    619 
    620 	if (rt = mrtfind(mrtcp->mrtc_origin.s_addr)) {
    621 		/* Just update the route */
    622 		s = splnet();
    623 		rt->mrt_parent = mrtcp->mrtc_parent;
    624 		VIFM_COPY(mrtcp->mrtc_children, rt->mrt_children);
    625 		VIFM_COPY(mrtcp->mrtc_leaves, rt->mrt_leaves);
    626 		splx(s);
    627 		return (0);
    628 	}
    629 
    630 	s = splnet();
    631 
    632 	rt = (struct mrt *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
    633 	if (rt == NULL) {
    634 		splx(s);
    635 		return (ENOBUFS);
    636 	}
    637 
    638 	/*
    639 	 * insert new entry at head of hash chain
    640 	 */
    641 	rt->mrt_origin = mrtcp->mrtc_origin;
    642 	rt->mrt_originmask = mrtcp->mrtc_originmask;
    643 	rt->mrt_parent = mrtcp->mrtc_parent;
    644 	VIFM_COPY(mrtcp->mrtc_children, rt->mrt_children);
    645 	VIFM_COPY(mrtcp->mrtc_leaves, rt->mrt_leaves);
    646 	/* link into table */
    647 	hash = nethash(mrtcp->mrtc_origin.s_addr);
    648 	rt->mrt_next = mrttable[hash];
    649 	mrttable[hash] = rt;
    650 
    651 	splx(s);
    652 	return (0);
    653 }
    654 
    655 /*
    656  * Delete an mrt entry
    657  */
    658 static int
    659 del_mrt(origin)
    660 	register struct in_addr *origin;
    661 {
    662 	register struct mrt *rt, *prev_rt;
    663 	register u_long hash = nethash(origin->s_addr);
    664 	register struct mrt **cmrt, **cmrtend;
    665 	register int s;
    666 
    667 	for (prev_rt = rt = mrttable[hash]; rt; prev_rt = rt, rt = rt->mrt_next)
    668 		if (origin->s_addr == rt->mrt_origin.s_addr)
    669 			break;
    670 	if (!rt)
    671 		return (ESRCH);
    672 
    673 	s = splnet();
    674 
    675 	cmrt = mrtsrchash;
    676 	cmrtend = cmrt + MSRCHASHSIZ;
    677 	for ( ; cmrt < cmrtend; ++cmrt)
    678 		if (*cmrt == rt)
    679 			*cmrt = 0;
    680 
    681 	if (prev_rt == rt)
    682 		mrttable[hash] = rt->mrt_next;
    683 	else
    684 		prev_rt->mrt_next = rt->mrt_next;
    685 	free(rt, M_MRTABLE);
    686 
    687 	splx(s);
    688 	return (0);
    689 }
    690 
    691 /*
    692  * IP multicast forwarding function. This function assumes that the packet
    693  * pointed to by "ip" has arrived on (or is about to be sent to) the interface
    694  * pointed to by "ifp", and the packet is to be relayed to other networks
    695  * that have members of the packet's destination IP multicast group.
    696  *
    697  * The packet is returned unscathed to the caller, unless it is tunneled
    698  * or erroneous, in which case a non-zero return value tells the caller to
    699  * discard it.
    700  */
    701 
    702 #define	IP_HDR_LEN  20	/* # bytes of fixed IP header (excluding options) */
    703 #define	TUNNEL_LEN  12	/* # bytes of IP option for tunnel encapsulation  */
    704 
    705 int
    706 ip_mforward(m, ifp)
    707 	register struct mbuf *m;
    708 	register struct ifnet *ifp;
    709 {
    710 	register struct ip *ip = mtod(m, struct ip *);
    711 	register struct mrt *rt;
    712 	register struct vif *vifp;
    713 	register int vifi;
    714 	register u_char *ipoptions;
    715 	u_long tunnel_src;
    716 
    717 	if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
    718 	    (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR) {
    719 		/*
    720 		 * Packet arrived via a physical interface or was
    721 		 * decapsulated off an encapsulating tunnel.
    722 		 * If ifp is one of the multicast_decap_if[]
    723 		 * dummy interfaces, we know it arrived on an
    724 		 * encapsulating tunnel, and we set tunnel_src to 1.
    725 		 * We can detect the dummy interface easily since
    726 		 * it's output function is null.
    727 		 */
    728 		tunnel_src = (ifp->if_output == 0) ? 1 : 0;
    729 	} else {
    730 		/*
    731 		 * Packet arrived through a tunnel.
    732 		 *
    733 		 * A tunneled packet has a single NOP option and a
    734 		 * two-element loose-source-and-record-route (LSRR)
    735 		 * option immediately following the fixed-size part of
    736 		 * the IP header.  At this point in processing, the IP
    737 		 * header should contain the following IP addresses:
    738 		 *
    739 		 * original source          - in the source address field
    740 		 * destination group        - in the destination address field
    741 		 * remote tunnel end-point  - in the first  element of LSRR
    742 		 * one of this host's addrs - in the second element of LSRR
    743 		 *
    744 		 * NOTE: RFC-1075 would have the original source and
    745 		 * remote tunnel end-point addresses swapped.  However,
    746 		 * that could cause delivery of ICMP error messages to
    747 		 * innocent applications on intermediate routing
    748 		 * hosts!  Therefore, we hereby change the spec.
    749 		 */
    750 
    751 		/*
    752 		 * Verify that the tunnel options are well-formed.
    753 		 */
    754 		if (ipoptions[0] != IPOPT_NOP ||
    755 		    ipoptions[2] != 11 ||	/* LSRR option length   */
    756 		    ipoptions[3] != 12 ||	/* LSRR address pointer */
    757 		    (tunnel_src = *(u_long *)(&ipoptions[4])) == 0) {
    758 			mrtstat.mrts_bad_tunnel++;
    759 			return (1);
    760 		}
    761 
    762 		/*
    763 		 * Delete the tunnel options from the packet.
    764 		 */
    765 		ovbcopy((caddr_t)(ipoptions + TUNNEL_LEN), (caddr_t)ipoptions,
    766 		    (unsigned)(m->m_len - (IP_HDR_LEN + TUNNEL_LEN)));
    767 		m->m_len -= TUNNEL_LEN;
    768 		ip->ip_len -= TUNNEL_LEN;
    769 		ip->ip_hl -= TUNNEL_LEN >> 2;
    770 
    771 		ifp = 0;
    772 	}
    773 
    774 	/*
    775 	 * Don't forward a packet with time-to-live of zero or one,
    776 	 * or a packet destined to a local-only group.
    777 	 */
    778 	if (ip->ip_ttl <= 1 ||
    779 	    ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
    780 		return ((int)tunnel_src);
    781 
    782 	/*
    783 	 * Don't forward if we don't have a route for the packet's origin.
    784 	 */
    785 	MRTFIND(ip->ip_src.s_addr, rt)
    786 	if (rt == NULL) {
    787 		mrtstat.mrts_no_route++;
    788 		return ((int)tunnel_src);
    789 	}
    790 
    791 	/*
    792 	 * Don't forward if it didn't arrive from the
    793 	 * parent vif for its origin.
    794 	 *
    795 	 * Notes: v_ifp is zero for src route tunnels, multicast_decap_if
    796 	 * for encapsulated tunnels and a real ifnet for non-tunnels so
    797 	 * the first part of the if catches wrong physical interface or
    798 	 * tunnel type; v_rmt_addr is zero for non-tunneled packets so
    799 	 * the 2nd part catches both packets that arrive via a tunnel
    800 	 * that shouldn't and packets that arrive via the wrong tunnel.
    801 	 */
    802 	vifi = rt->mrt_parent;
    803 	if (viftable[vifi].v_ifp != ifp ||
    804 	    (ifp == 0 && viftable[vifi].v_rmt_addr.s_addr != tunnel_src)) {
    805 		/* came in the wrong interface */
    806 		++mrtstat.mrts_wrong_if;
    807 		return (int)tunnel_src;
    808 	}
    809 
    810 	/*
    811 	 * For each vif, decide if a copy of the packet should be forwarded.
    812 	 * Forward if:
    813 	 *		- the ttl exceeds the vif's threshold AND
    814 	 *		- the vif is a child in the origin's route AND
    815 	 *		- ( the vif is not a leaf in the origin's route OR
    816 	 *		    the destination group has members on the vif )
    817 	 *
    818 	 * (This might be speeded up with some sort of cache -- someday.)
    819 	 */
    820 	for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++) {
    821 		if (ip->ip_ttl > vifp->v_threshold &&
    822 		    VIFM_ISSET(vifi, rt->mrt_children) &&
    823 		    (!VIFM_ISSET(vifi, rt->mrt_leaves) ||
    824 		    grplst_member(vifp, ip->ip_dst))) {
    825 			if (vifp->v_flags & VIFF_SRCRT)
    826 				srcrt_send(ip, vifp, m);
    827 			else if (vifp->v_flags & VIFF_TUNNEL)
    828 				encap_send(ip, vifp, m);
    829 			else
    830 				phyint_send(ip, vifp, m);
    831 		}
    832 	}
    833 	return ((int)tunnel_src);
    834 }
    835 
    836 static void
    837 phyint_send(ip, vifp, m)
    838 	register struct ip *ip;
    839 	register struct vif *vifp;
    840 	register struct mbuf *m;
    841 {
    842 	register struct mbuf *mb_copy;
    843 	register struct ip_moptions *imo;
    844 	register int error;
    845 	struct ip_moptions simo;
    846 
    847 	mb_copy = m_copy(m, 0, M_COPYALL);
    848 	if (mb_copy == NULL)
    849 		return;
    850 
    851 	imo = &simo;
    852 	imo->imo_multicast_ifp = vifp->v_ifp;
    853 	imo->imo_multicast_ttl = ip->ip_ttl - 1;
    854 	imo->imo_multicast_loop = 1;
    855 
    856 	error = ip_output(mb_copy, NULL, NULL, IP_FORWARDING, imo);
    857 }
    858 
    859 static void
    860 srcrt_send(ip, vifp, m)
    861 	register struct ip *ip;
    862 	register struct vif *vifp;
    863 	register struct mbuf *m;
    864 {
    865 	register struct mbuf *mb_copy, *mb_opts;
    866 	register struct ip *ip_copy;
    867 	register int error;
    868 	register u_char *cp;
    869 
    870 	/*
    871 	 * Make sure that adding the tunnel options won't exceed the
    872 	 * maximum allowed number of option bytes.
    873 	 */
    874 	if (ip->ip_hl > (60 - TUNNEL_LEN) >> 2) {
    875 		mrtstat.mrts_cant_tunnel++;
    876 		return;
    877 	}
    878 
    879 	mb_copy = m_copy(m, 0, M_COPYALL);
    880 	if (mb_copy == NULL)
    881 		return;
    882 	ip_copy = mtod(mb_copy, struct ip *);
    883 	ip_copy->ip_ttl--;
    884 	ip_copy->ip_dst = vifp->v_rmt_addr;	/* remote tunnel end-point */
    885 	/*
    886 	 * Adjust the ip header length to account for the tunnel options.
    887 	 */
    888 	ip_copy->ip_hl += TUNNEL_LEN >> 2;
    889 	ip_copy->ip_len += TUNNEL_LEN;
    890 	MGETHDR(mb_opts, M_DONTWAIT, MT_HEADER);
    891 	if (mb_opts == NULL) {
    892 		m_freem(mb_copy);
    893 		return;
    894 	}
    895 	/*
    896 	 * 'Delete' the base ip header from the mb_copy chain
    897 	 */
    898 	mb_copy->m_len -= IP_HDR_LEN;
    899 	mb_copy->m_data += IP_HDR_LEN;
    900 	/*
    901 	 * Make mb_opts be the new head of the packet chain.
    902 	 * Any options of the packet were left in the old packet chain head
    903 	 */
    904 	mb_opts->m_next = mb_copy;
    905 	mb_opts->m_len = IP_HDR_LEN + TUNNEL_LEN;
    906 	mb_opts->m_pkthdr.len = mb_copy->m_pkthdr.len + TUNNEL_LEN;
    907 	mb_opts->m_pkthdr.rcvif = mb_copy->m_pkthdr.rcvif;
    908 	mb_opts->m_data += MSIZE - mb_opts->m_len;
    909 	/*
    910 	 * Copy the base ip header from the mb_copy chain to the new head mbuf
    911 	 */
    912 	bcopy((caddr_t)ip_copy, mtod(mb_opts, caddr_t), IP_HDR_LEN);
    913 	/*
    914 	 * Add the NOP and LSRR after the base ip header
    915 	 */
    916 	cp = mtod(mb_opts, u_char *) + IP_HDR_LEN;
    917 	*cp++ = IPOPT_NOP;
    918 	*cp++ = IPOPT_LSRR;
    919 	*cp++ = 11;		/* LSRR option length */
    920 	*cp++ = 8;		/* LSSR pointer to second element */
    921 	*(u_long*)cp = vifp->v_lcl_addr.s_addr;	/* local tunnel end-point */
    922 	cp += 4;
    923 	*(u_long*)cp = ip->ip_dst.s_addr;		/* destination group */
    924 
    925 	error = ip_output(mb_opts, NULL, NULL, IP_FORWARDING, NULL);
    926 }
    927 
    928 static void
    929 encap_send(ip, vifp, m)
    930 	register struct ip *ip;
    931 	register struct vif *vifp;
    932 	register struct mbuf *m;
    933 {
    934 	register struct mbuf *mb_copy;
    935 	register struct ip *ip_copy;
    936 	register int i, len = ip->ip_len;
    937 
    938 	/*
    939 	 * copy the old packet & pullup it's IP header into the
    940 	 * new mbuf so we can modify it.  Try to fill the new
    941 	 * mbuf since if we don't the ethernet driver will.
    942 	 */
    943 	MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
    944 	if (mb_copy == NULL)
    945 		return;
    946 	mb_copy->m_data += 16;
    947 	mb_copy->m_len = sizeof(multicast_encap_iphdr);
    948 	if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
    949 		m_freem(mb_copy);
    950 		return;
    951 	}
    952 	i = MHLEN - 16;
    953 	if (i > len)
    954 		i = len;
    955 	mb_copy = m_pullup(mb_copy, i);
    956 	if (mb_copy == NULL)
    957 		return;
    958 
    959 	/*
    960 	 * fill in the encapsulating IP header.
    961 	 */
    962 	ip_copy = mtod(mb_copy, struct ip *);
    963 	*ip_copy = multicast_encap_iphdr;
    964 	ip_copy->ip_id = htons(ip_id++);
    965 	ip_copy->ip_len += len;
    966 	ip_copy->ip_src = vifp->v_lcl_addr;
    967 	ip_copy->ip_dst = vifp->v_rmt_addr;
    968 
    969 	/*
    970 	 * turn the encapsulated IP header back into a valid one.
    971 	 */
    972 	ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
    973 	--ip->ip_ttl;
    974 	HTONS(ip->ip_len);
    975 	HTONS(ip->ip_off);
    976 	ip->ip_sum = 0;
    977 #if defined(LBL) && !defined(ultrix) && !defined(i386)
    978 	ip->ip_sum = ~oc_cksum((caddr_t)ip, ip->ip_hl << 2, 0);
    979 #else
    980 	mb_copy->m_data += sizeof(multicast_encap_iphdr);
    981 	ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
    982 	mb_copy->m_data -= sizeof(multicast_encap_iphdr);
    983 	mb_copy->m_pkthdr.len = m->m_pkthdr.len + sizeof(multicast_encap_iphdr);
    984 	mb_copy->m_pkthdr.rcvif = m->m_pkthdr.rcvif;
    985 #endif
    986 	ip_output(mb_copy, (struct mbuf *)0, (struct route *)0,
    987 		  IP_FORWARDING, (struct ip_moptions *)0);
    988 }
    989 
    990 /*
    991  * De-encapsulate a packet and feed it back through ip input (this
    992  * routine is called whenever IP gets a packet with proto type
    993  * ENCAP_PROTO and a local destination address).
    994  */
    995 static void
    996 multiencap_decap(m, hlen)
    997 	register struct mbuf *m;
    998 	int hlen;
    999 {
   1000 	struct ifnet *ifp;
   1001 	register struct ip *ip = mtod(m, struct ip *);
   1002 	register int s;
   1003 	register struct ifqueue *ifq;
   1004 	register struct vif *vifp;
   1005 
   1006 	if (ip->ip_p != ENCAP_PROTO) {
   1007 		(*encap_oldrawip)(m, hlen);
   1008 		return;
   1009 	}
   1010 	/*
   1011 	 * dump the packet if it's not to a multicast destination or if
   1012 	 * we don't have an encapsulating tunnel with the source.
   1013 	 * Note:  This code assumes that the remote site IP address
   1014 	 * uniquely identifies the tunnel (i.e., that this site has
   1015 	 * at most one tunnel with the remote site).
   1016 	 */
   1017 	if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
   1018 		++mrtstat.mrts_bad_tunnel;
   1019 		m_freem(m);
   1020 		return;
   1021 	}
   1022 	if (ip->ip_src.s_addr != last_encap_src) {
   1023 		register struct vif *vife;
   1024 
   1025 		vifp = viftable;
   1026 		vife = vifp + numvifs;
   1027 		last_encap_src = ip->ip_src.s_addr;
   1028 		last_encap_vif = 0;
   1029 		for ( ; vifp < vife; ++vifp)
   1030 			if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
   1031 				if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
   1032 				    == VIFF_TUNNEL)
   1033 					last_encap_vif = vifp;
   1034 				break;
   1035 			}
   1036 	}
   1037 	if ((vifp = last_encap_vif) == 0) {
   1038 		mrtstat.mrts_cant_tunnel++; /*XXX*/
   1039 		m_freem(m);
   1040 		return;
   1041 	}
   1042 	ifp = vifp->v_ifp;
   1043 	m->m_data += hlen;
   1044 	m->m_len -= hlen;
   1045 	m->m_pkthdr.rcvif = ifp;
   1046 	m->m_pkthdr.len -= hlen;
   1047 	ifq = &ipintrq;
   1048 	s = splimp();
   1049 	if (IF_QFULL(ifq)) {
   1050 		IF_DROP(ifq);
   1051 		m_freem(m);
   1052 	} else {
   1053 		IF_ENQUEUE(ifq, m);
   1054 		/*
   1055 		 * normally we would need a "schednetisr(NETISR_IP)"
   1056 		 * here but we were called by ip_input and it is going
   1057 		 * to loop back & try to dequeue the packet we just
   1058 		 * queued as soon as we return so we avoid the
   1059 		 * unnecessary software interrrupt.
   1060 		 */
   1061 	}
   1062 	splx(s);
   1063 }
   1064 #endif
   1065