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