Home | History | Annotate | Line # | Download | only in netinet
ip_mroute.c revision 1.12
      1   1.1  hpeyerl /*
      2   1.1  hpeyerl  * Copyright (c) 1989 Stephen Deering
      3  1.12   brezak  * Copyright (c) 1992 Regents of the University of California.
      4  1.12   brezak  * 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.12   brezak  *	from: @(#)ip_mroute.c	7.4 (Berkeley) 11/19/92
     38  1.12   brezak  *	$Id $
     39   1.1  hpeyerl  */
     40   1.1  hpeyerl 
     41   1.1  hpeyerl /*
     42   1.1  hpeyerl  * Procedures for the kernel part of DVMRP,
     43   1.1  hpeyerl  * a Distance-Vector Multicast Routing Protocol.
     44   1.1  hpeyerl  * (See RFC-1075.)
     45   1.1  hpeyerl  *
     46   1.1  hpeyerl  * Written by David Waitzman, BBN Labs, August 1988.
     47   1.1  hpeyerl  * Modified by Steve Deering, Stanford, February 1989.
     48   1.1  hpeyerl  *
     49   1.1  hpeyerl  * MROUTING 1.1
     50   1.1  hpeyerl  */
     51   1.1  hpeyerl 
     52   1.1  hpeyerl #ifndef MROUTING
     53   1.1  hpeyerl int	ip_mrtproto;				/* for netstat only */
     54   1.1  hpeyerl #else
     55   1.1  hpeyerl 
     56   1.1  hpeyerl #include <sys/param.h>
     57   1.1  hpeyerl #include <sys/errno.h>
     58   1.1  hpeyerl #include <sys/ioctl.h>
     59   1.1  hpeyerl #include <sys/malloc.h>
     60   1.1  hpeyerl #include <sys/mbuf.h>
     61   1.1  hpeyerl #include <sys/protosw.h>
     62   1.1  hpeyerl #include <sys/socket.h>
     63   1.1  hpeyerl #include <sys/socketvar.h>
     64   1.1  hpeyerl #include <sys/time.h>
     65   1.1  hpeyerl 
     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.12   brezak 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.12   brezak static	struct mrt *mrtfind __P((u_long));
     92  1.12   brezak static	void phyint_send __P((struct ip *, struct vif *, struct mbuf *));
     93  1.12   brezak static	void srcrt_send __P((struct ip *, struct vif *, struct mbuf *));
     94  1.12   brezak static	void encap_send __P((struct ip *, struct vif *, struct mbuf *));
     95  1.12   brezak static	void multiencap_decap __P((struct mbuf *, int hlen));
     96   1.1  hpeyerl 
     97  1.12   brezak #define	INSIZ	sizeof(struct in_addr)
     98  1.12   brezak #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.12   brezak  * 'Interfaces' associated with decapsulator (so we can tell
    114  1.12   brezak  * packets that went through it from ones that get reflected
    115  1.12   brezak  * by a broken gateway).  These interfaces are never linked into
    116  1.12   brezak  * the system ifnet list & no routes point to them.  I.e., packets
    117  1.12   brezak  * can't be sent this way.  They only exist as a placeholder for
    118  1.12   brezak  * multicast source verification.
    119  1.12   brezak  */
    120  1.12   brezak struct ifnet multicast_decap_if[MAXVIFS];
    121  1.12   brezak 
    122  1.12   brezak #define	ENCAP_TTL 64
    123  1.12   brezak #define	ENCAP_PROTO 4
    124  1.12   brezak 
    125  1.12   brezak /* prototype IP hdr for encapsulated packets */
    126  1.12   brezak struct ip multicast_encap_iphdr = {
    127  1.12   brezak #if defined(ultrix) || defined(i386)
    128  1.12   brezak 	sizeof(struct ip) >> 2, IPVERSION,
    129  1.12   brezak #else
    130  1.12   brezak 	IPVERSION, sizeof(struct ip) >> 2,
    131  1.12   brezak #endif
    132  1.12   brezak 	0,				/* tos */
    133  1.12   brezak 	sizeof(struct ip),		/* total length */
    134  1.12   brezak 	0,				/* id */
    135  1.12   brezak 	0,				/* frag offset */
    136  1.12   brezak 	ENCAP_TTL, ENCAP_PROTO,
    137  1.12   brezak 	0,				/* checksum */
    138  1.12   brezak };
    139  1.12   brezak 
    140  1.12   brezak /*
    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.12   brezak static void (*encap_oldrawip)();
    149  1.12   brezak 
    150  1.12   brezak /*
    151  1.12   brezak  * one-back cache used by multiencap_decap to locate a tunnel's vif
    152  1.12   brezak  * given a datagram's src ip address.
    153  1.12   brezak  */
    154  1.12   brezak static u_long last_encap_src;
    155  1.12   brezak static struct vif *last_encap_vif;
    156  1.12   brezak 
    157  1.12   brezak /*
    158  1.12   brezak  * A simple hash function: returns MRTHASHMOD of the low-order octet of
    159  1.12   brezak  * the argument's network or subnet number.
    160  1.12   brezak  */
    161  1.12   brezak static u_long
    162  1.12   brezak nethash(n)
    163  1.12   brezak 	u_long n;
    164  1.12   brezak {
    165  1.12   brezak 	struct in_addr in;
    166  1.12   brezak 
    167  1.12   brezak 	in.s_addr = n;
    168  1.12   brezak 	n = in_netof(in);
    169  1.12   brezak 	while ((n & 0xff) == 0)
    170  1.12   brezak 		n >>= 8;
    171  1.12   brezak 	return (MRTHASHMOD(n));
    172  1.12   brezak }
    173  1.12   brezak 
    174  1.12   brezak /*
    175  1.12   brezak  * this is a direct-mapped cache used to speed the mapping from a
    176  1.12   brezak  * datagram source address to the associated multicast route.  Note
    177  1.12   brezak  * that unlike mrttable, the hash is on IP address, not IP net number.
    178  1.12   brezak  */
    179  1.12   brezak #define	MSRCHASHSIZ	1024
    180  1.12   brezak #define	MSRCHASH(a)	((((a) >> 20) ^ ((a) >> 10) ^ (a)) & (MSRCHASHSIZ - 1))
    181  1.12   brezak struct mrt *mrtsrchash[MSRCHASHSIZ];
    182  1.12   brezak 
    183  1.12   brezak /*
    184  1.12   brezak  * Find a route for a given origin IP address.
    185  1.12   brezak  */
    186  1.12   brezak #define	MRTFIND(o, rt) { \
    187  1.12   brezak 	register u_int _mrhash = o; \
    188  1.12   brezak 	_mrhash = MSRCHASH(_mrhash); \
    189  1.12   brezak 	++mrtstat.mrts_mrt_lookups; \
    190  1.12   brezak 	rt = mrtsrchash[_mrhash]; \
    191  1.12   brezak 	if (rt == NULL || \
    192  1.12   brezak 	    (o & rt->mrt_originmask.s_addr) != rt->mrt_origin.s_addr) \
    193  1.12   brezak 		if ((rt = mrtfind(o)) != NULL) \
    194  1.12   brezak 		    mrtsrchash[_mrhash] = rt; \
    195  1.12   brezak }
    196  1.12   brezak 
    197  1.12   brezak static struct mrt *
    198  1.12   brezak mrtfind(origin)
    199  1.12   brezak 	u_long origin;
    200  1.12   brezak {
    201  1.12   brezak 	register struct mrt *rt;
    202  1.12   brezak 	register u_int hash;
    203  1.12   brezak 
    204  1.12   brezak 	mrtstat.mrts_mrt_misses++;
    205  1.12   brezak 
    206  1.12   brezak 	hash = nethash(origin);
    207  1.12   brezak 	for (rt = mrttable[hash]; rt; rt = rt->mrt_next) {
    208  1.12   brezak 		if ((origin & rt->mrt_originmask.s_addr) ==
    209  1.12   brezak 		    rt->mrt_origin.s_addr)
    210  1.12   brezak 			return (rt);
    211  1.12   brezak 	}
    212  1.12   brezak 	return (NULL);
    213  1.12   brezak }
    214  1.12   brezak 
    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.12   brezak 	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.12   brezak 	ifp = ifa->ifa_ifp;
    378   1.1  hpeyerl 
    379  1.12   brezak 	if (vifcp->vifc_flags & VIFF_TUNNEL) {
    380  1.12   brezak 		if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
    381  1.12   brezak 			/*
    382  1.12   brezak 			 * An encapsulating tunnel is wanted.  If we
    383  1.12   brezak 			 * haven't done so already, put our decap routine
    384  1.12   brezak 			 * in front of raw_input so we have a chance to
    385  1.12   brezak 			 * decapsulate incoming packets.  Then set the
    386  1.12   brezak 			 * arrival 'interface' to be the decapsulator.
    387  1.12   brezak 			 */
    388  1.12   brezak 			if (encap_oldrawip == 0) {
    389  1.12   brezak 				extern struct protosw inetsw[];
    390  1.12   brezak 				extern u_char ip_protox[];
    391  1.12   brezak 				register int pr = ip_protox[ENCAP_PROTO];
    392  1.12   brezak 
    393  1.12   brezak 				encap_oldrawip = inetsw[pr].pr_input;
    394  1.12   brezak 				inetsw[pr].pr_input = multiencap_decap;
    395  1.12   brezak 				for (s = 0; s < MAXVIFS; ++s) {
    396  1.12   brezak 					multicast_decap_if[s].if_name =
    397  1.12   brezak 						"mdecap";
    398  1.12   brezak 					multicast_decap_if[s].if_unit = s;
    399  1.12   brezak 				}
    400  1.12   brezak 			}
    401  1.12   brezak 			ifp = &multicast_decap_if[vifcp->vifc_vifi];
    402  1.12   brezak 		} else {
    403  1.12   brezak 			ifp = 0;
    404  1.12   brezak 		}
    405  1.12   brezak 	} else {
    406  1.12   brezak 		/* Make sure the interface supports multicast */
    407  1.12   brezak 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
    408  1.12   brezak 			return EOPNOTSUPP;
    409  1.10  mycroft 
    410   1.1  hpeyerl 		/*
    411  1.12   brezak 		 * Enable promiscuous reception of all
    412  1.12   brezak 		 * IP multicasts from the if
    413   1.1  hpeyerl 		 */
    414  1.12   brezak 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
    415  1.12   brezak 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr =
    416  1.12   brezak 			INADDR_ANY;
    417  1.12   brezak 		s = splnet();
    418   1.1  hpeyerl 		error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
    419  1.12   brezak 		splx(s);
    420  1.12   brezak 		if (error)
    421  1.12   brezak 			return error;
    422   1.1  hpeyerl 	}
    423   1.1  hpeyerl 
    424  1.12   brezak 	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.12   brezak 	vifp->v_ifp = ifp;
    429  1.12   brezak 	vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
    430  1.12   brezak 	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.12   brezak 	if (vifp == last_encap_vif) {
    468  1.12   brezak 		last_encap_vif = 0;
    469  1.12   brezak 		last_encap_src = 0;
    470  1.12   brezak 	}
    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 static int
    543   1.1  hpeyerl del_lgrp(gcp)
    544   1.1  hpeyerl 	register struct lgrplctl *gcp;
    545   1.1  hpeyerl {
    546   1.1  hpeyerl 	register struct vif *vifp;
    547   1.1  hpeyerl 	register int i, error, s;
    548   1.1  hpeyerl 
    549   1.1  hpeyerl 	if (gcp->lgc_vifi >= numvifs)
    550   1.1  hpeyerl 		return (EINVAL);
    551   1.1  hpeyerl 	vifp = viftable + gcp->lgc_vifi;
    552   1.1  hpeyerl 	if (vifp->v_lcl_addr.s_addr == 0 || (vifp->v_flags & VIFF_TUNNEL))
    553   1.1  hpeyerl 		return (EADDRNOTAVAIL);
    554   1.1  hpeyerl 
    555   1.1  hpeyerl 	s = splnet();
    556   1.1  hpeyerl 
    557   1.1  hpeyerl 	if (gcp->lgc_gaddr.s_addr == vifp->v_cached_group)
    558   1.1  hpeyerl 		vifp->v_cached_result = 0;
    559   1.1  hpeyerl 
    560   1.1  hpeyerl 	error = EADDRNOTAVAIL;
    561   1.1  hpeyerl 	for (i = 0; i < vifp->v_lcl_grps_n; ++i)
    562   1.1  hpeyerl 		if (same(&gcp->lgc_gaddr, &vifp->v_lcl_grps[i])) {
    563   1.1  hpeyerl 			error = 0;
    564  1.12   brezak 			--vifp->v_lcl_grps_n;
    565  1.12   brezak 			for (; i < vifp->v_lcl_grps_n; ++i)
    566  1.12   brezak 				vifp->v_lcl_grps[i] = vifp->v_lcl_grps[i + 1];
    567   1.1  hpeyerl 			error = 0;
    568   1.1  hpeyerl 			break;
    569   1.1  hpeyerl 		}
    570   1.1  hpeyerl 
    571   1.1  hpeyerl 	splx(s);
    572   1.1  hpeyerl 	return (error);
    573   1.1  hpeyerl }
    574   1.1  hpeyerl 
    575   1.1  hpeyerl /*
    576   1.1  hpeyerl  * Return 1 if gaddr is a member of the local group list for vifp.
    577   1.1  hpeyerl  */
    578   1.1  hpeyerl static int
    579   1.1  hpeyerl grplst_member(vifp, gaddr)
    580   1.1  hpeyerl 	register struct vif *vifp;
    581   1.1  hpeyerl 	struct in_addr gaddr;
    582   1.1  hpeyerl {
    583   1.1  hpeyerl 	register int i, s;
    584   1.1  hpeyerl 	register u_long addr;
    585   1.1  hpeyerl 
    586   1.1  hpeyerl 	mrtstat.mrts_grp_lookups++;
    587   1.1  hpeyerl 
    588   1.1  hpeyerl 	addr = gaddr.s_addr;
    589   1.1  hpeyerl 	if (addr == vifp->v_cached_group)
    590   1.1  hpeyerl 		return (vifp->v_cached_result);
    591   1.1  hpeyerl 
    592   1.1  hpeyerl 	mrtstat.mrts_grp_misses++;
    593   1.1  hpeyerl 
    594   1.1  hpeyerl 	for (i = 0; i < vifp->v_lcl_grps_n; ++i)
    595   1.1  hpeyerl 		if (addr == vifp->v_lcl_grps[i].s_addr) {
    596   1.1  hpeyerl 			s = splnet();
    597   1.1  hpeyerl 			vifp->v_cached_group = addr;
    598   1.1  hpeyerl 			vifp->v_cached_result = 1;
    599   1.1  hpeyerl 			splx(s);
    600   1.1  hpeyerl 			return (1);
    601   1.1  hpeyerl 		}
    602   1.1  hpeyerl 	s = splnet();
    603   1.1  hpeyerl 	vifp->v_cached_group = addr;
    604   1.1  hpeyerl 	vifp->v_cached_result = 0;
    605   1.1  hpeyerl 	splx(s);
    606   1.1  hpeyerl 	return (0);
    607   1.1  hpeyerl }
    608   1.1  hpeyerl 
    609   1.1  hpeyerl /*
    610   1.1  hpeyerl  * Add an mrt entry
    611   1.1  hpeyerl  */
    612   1.1  hpeyerl static int
    613   1.1  hpeyerl add_mrt(mrtcp)
    614   1.1  hpeyerl 	register struct mrtctl *mrtcp;
    615   1.1  hpeyerl {
    616   1.1  hpeyerl 	struct mrt *rt;
    617   1.1  hpeyerl 	u_long hash;
    618   1.1  hpeyerl 	int s;
    619   1.1  hpeyerl 
    620  1.12   brezak 	if (rt = mrtfind(mrtcp->mrtc_origin.s_addr)) {
    621   1.1  hpeyerl 		/* Just update the route */
    622   1.1  hpeyerl 		s = splnet();
    623   1.1  hpeyerl 		rt->mrt_parent = mrtcp->mrtc_parent;
    624   1.1  hpeyerl 		VIFM_COPY(mrtcp->mrtc_children, rt->mrt_children);
    625   1.1  hpeyerl 		VIFM_COPY(mrtcp->mrtc_leaves, rt->mrt_leaves);
    626   1.1  hpeyerl 		splx(s);
    627   1.1  hpeyerl 		return (0);
    628   1.1  hpeyerl 	}
    629   1.1  hpeyerl 
    630   1.1  hpeyerl 	s = splnet();
    631   1.1  hpeyerl 
    632   1.1  hpeyerl 	rt = (struct mrt *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
    633   1.1  hpeyerl 	if (rt == NULL) {
    634   1.1  hpeyerl 		splx(s);
    635   1.1  hpeyerl 		return (ENOBUFS);
    636   1.1  hpeyerl 	}
    637   1.1  hpeyerl 
    638   1.1  hpeyerl 	/*
    639   1.1  hpeyerl 	 * insert new entry at head of hash chain
    640   1.1  hpeyerl 	 */
    641   1.1  hpeyerl 	rt->mrt_origin = mrtcp->mrtc_origin;
    642   1.1  hpeyerl 	rt->mrt_originmask = mrtcp->mrtc_originmask;
    643   1.1  hpeyerl 	rt->mrt_parent = mrtcp->mrtc_parent;
    644   1.1  hpeyerl 	VIFM_COPY(mrtcp->mrtc_children, rt->mrt_children);
    645   1.1  hpeyerl 	VIFM_COPY(mrtcp->mrtc_leaves, rt->mrt_leaves);
    646   1.1  hpeyerl 	/* link into table */
    647  1.12   brezak 	hash = nethash(mrtcp->mrtc_origin.s_addr);
    648   1.1  hpeyerl 	rt->mrt_next = mrttable[hash];
    649   1.1  hpeyerl 	mrttable[hash] = rt;
    650   1.1  hpeyerl 
    651   1.1  hpeyerl 	splx(s);
    652   1.1  hpeyerl 	return (0);
    653   1.1  hpeyerl }
    654   1.1  hpeyerl 
    655   1.1  hpeyerl /*
    656   1.1  hpeyerl  * Delete an mrt entry
    657   1.1  hpeyerl  */
    658   1.1  hpeyerl static int
    659   1.1  hpeyerl del_mrt(origin)
    660   1.1  hpeyerl 	register struct in_addr *origin;
    661   1.1  hpeyerl {
    662   1.1  hpeyerl 	register struct mrt *rt, *prev_rt;
    663  1.12   brezak 	register u_long hash = nethash(origin->s_addr);
    664  1.12   brezak 	register struct mrt **cmrt, **cmrtend;
    665   1.1  hpeyerl 	register int s;
    666   1.1  hpeyerl 
    667   1.1  hpeyerl 	for (prev_rt = rt = mrttable[hash]; rt; prev_rt = rt, rt = rt->mrt_next)
    668   1.1  hpeyerl 		if (origin->s_addr == rt->mrt_origin.s_addr)
    669   1.1  hpeyerl 			break;
    670   1.1  hpeyerl 	if (!rt)
    671   1.1  hpeyerl 		return (ESRCH);
    672   1.1  hpeyerl 
    673   1.1  hpeyerl 	s = splnet();
    674   1.1  hpeyerl 
    675  1.12   brezak 	cmrt = mrtsrchash;
    676  1.12   brezak 	cmrtend = cmrt + MSRCHASHSIZ;
    677  1.12   brezak 	for ( ; cmrt < cmrtend; ++cmrt)
    678  1.12   brezak 		if (*cmrt == rt)
    679  1.12   brezak 			*cmrt = 0;
    680   1.1  hpeyerl 
    681   1.1  hpeyerl 	if (prev_rt == rt)
    682   1.1  hpeyerl 		mrttable[hash] = rt->mrt_next;
    683   1.1  hpeyerl 	else
    684   1.1  hpeyerl 		prev_rt->mrt_next = rt->mrt_next;
    685   1.1  hpeyerl 	free(rt, M_MRTABLE);
    686   1.1  hpeyerl 
    687   1.1  hpeyerl 	splx(s);
    688   1.1  hpeyerl 	return (0);
    689   1.1  hpeyerl }
    690   1.1  hpeyerl 
    691   1.1  hpeyerl /*
    692   1.1  hpeyerl  * IP multicast forwarding function. This function assumes that the packet
    693   1.1  hpeyerl  * pointed to by "ip" has arrived on (or is about to be sent to) the interface
    694   1.1  hpeyerl  * pointed to by "ifp", and the packet is to be relayed to other networks
    695   1.1  hpeyerl  * that have members of the packet's destination IP multicast group.
    696   1.1  hpeyerl  *
    697   1.1  hpeyerl  * The packet is returned unscathed to the caller, unless it is tunneled
    698   1.1  hpeyerl  * or erroneous, in which case a non-zero return value tells the caller to
    699   1.1  hpeyerl  * discard it.
    700   1.1  hpeyerl  */
    701   1.1  hpeyerl 
    702  1.12   brezak #define	IP_HDR_LEN  20	/* # bytes of fixed IP header (excluding options) */
    703  1.12   brezak #define	TUNNEL_LEN  12	/* # bytes of IP option for tunnel encapsulation  */
    704   1.1  hpeyerl 
    705   1.1  hpeyerl int
    706  1.10  mycroft ip_mforward(m, ifp)
    707  1.10  mycroft 	register struct mbuf *m;
    708   1.1  hpeyerl 	register struct ifnet *ifp;
    709   1.1  hpeyerl {
    710  1.10  mycroft 	register struct ip *ip = mtod(m, struct ip *);
    711   1.1  hpeyerl 	register struct mrt *rt;
    712   1.1  hpeyerl 	register struct vif *vifp;
    713   1.1  hpeyerl 	register int vifi;
    714   1.1  hpeyerl 	register u_char *ipoptions;
    715   1.1  hpeyerl 	u_long tunnel_src;
    716   1.1  hpeyerl 
    717   1.1  hpeyerl 	if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
    718  1.12   brezak 	    (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR) {
    719   1.1  hpeyerl 		/*
    720  1.12   brezak 		 * Packet arrived via a physical interface or was
    721  1.12   brezak 		 * decapsulated off an encapsulating tunnel.
    722  1.12   brezak 		 * If ifp is one of the multicast_decap_if[]
    723  1.12   brezak 		 * dummy interfaces, we know it arrived on an
    724  1.12   brezak 		 * encapsulating tunnel, and we set tunnel_src to 1.
    725  1.12   brezak 		 * We can detect the dummy interface easily since
    726  1.12   brezak 		 * it's output function is null.
    727   1.1  hpeyerl 		 */
    728  1.12   brezak 		tunnel_src = (ifp->if_output == 0) ? 1 : 0;
    729   1.1  hpeyerl 	} else {
    730   1.1  hpeyerl 		/*
    731   1.1  hpeyerl 		 * Packet arrived through a tunnel.
    732   1.1  hpeyerl 		 *
    733   1.1  hpeyerl 		 * A tunneled packet has a single NOP option and a
    734   1.1  hpeyerl 		 * two-element loose-source-and-record-route (LSRR)
    735   1.1  hpeyerl 		 * option immediately following the fixed-size part of
    736   1.1  hpeyerl 		 * the IP header.  At this point in processing, the IP
    737   1.1  hpeyerl 		 * header should contain the following IP addresses:
    738   1.1  hpeyerl 		 *
    739   1.1  hpeyerl 		 * original source          - in the source address field
    740   1.1  hpeyerl 		 * destination group        - in the destination address field
    741   1.1  hpeyerl 		 * remote tunnel end-point  - in the first  element of LSRR
    742   1.1  hpeyerl 		 * one of this host's addrs - in the second element of LSRR
    743   1.1  hpeyerl 		 *
    744   1.1  hpeyerl 		 * NOTE: RFC-1075 would have the original source and
    745   1.1  hpeyerl 		 * remote tunnel end-point addresses swapped.  However,
    746   1.1  hpeyerl 		 * that could cause delivery of ICMP error messages to
    747   1.1  hpeyerl 		 * innocent applications on intermediate routing
    748   1.1  hpeyerl 		 * hosts!  Therefore, we hereby change the spec.
    749   1.1  hpeyerl 		 */
    750   1.1  hpeyerl 
    751   1.1  hpeyerl 		/*
    752   1.1  hpeyerl 		 * Verify that the tunnel options are well-formed.
    753   1.1  hpeyerl 		 */
    754   1.1  hpeyerl 		if (ipoptions[0] != IPOPT_NOP ||
    755   1.1  hpeyerl 		    ipoptions[2] != 11 ||	/* LSRR option length   */
    756   1.1  hpeyerl 		    ipoptions[3] != 12 ||	/* LSRR address pointer */
    757   1.1  hpeyerl 		    (tunnel_src = *(u_long *)(&ipoptions[4])) == 0) {
    758   1.1  hpeyerl 			mrtstat.mrts_bad_tunnel++;
    759   1.1  hpeyerl 			return (1);
    760   1.1  hpeyerl 		}
    761   1.1  hpeyerl 
    762   1.1  hpeyerl 		/*
    763   1.1  hpeyerl 		 * Delete the tunnel options from the packet.
    764   1.1  hpeyerl 		 */
    765   1.1  hpeyerl 		ovbcopy((caddr_t)(ipoptions + TUNNEL_LEN), (caddr_t)ipoptions,
    766   1.1  hpeyerl 		    (unsigned)(m->m_len - (IP_HDR_LEN + TUNNEL_LEN)));
    767   1.1  hpeyerl 		m->m_len -= TUNNEL_LEN;
    768   1.1  hpeyerl 		ip->ip_len -= TUNNEL_LEN;
    769   1.1  hpeyerl 		ip->ip_hl -= TUNNEL_LEN >> 2;
    770  1.12   brezak 
    771  1.12   brezak 		ifp = 0;
    772   1.1  hpeyerl 	}
    773   1.1  hpeyerl 
    774   1.1  hpeyerl 	/*
    775   1.1  hpeyerl 	 * Don't forward a packet with time-to-live of zero or one,
    776   1.1  hpeyerl 	 * or a packet destined to a local-only group.
    777   1.1  hpeyerl 	 */
    778   1.1  hpeyerl 	if (ip->ip_ttl <= 1 ||
    779   1.1  hpeyerl 	    ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
    780   1.1  hpeyerl 		return ((int)tunnel_src);
    781   1.1  hpeyerl 
    782   1.1  hpeyerl 	/*
    783   1.1  hpeyerl 	 * Don't forward if we don't have a route for the packet's origin.
    784   1.1  hpeyerl 	 */
    785  1.12   brezak 	MRTFIND(ip->ip_src.s_addr, rt)
    786  1.12   brezak 	if (rt == NULL) {
    787   1.1  hpeyerl 		mrtstat.mrts_no_route++;
    788   1.1  hpeyerl 		return ((int)tunnel_src);
    789   1.1  hpeyerl 	}
    790   1.1  hpeyerl 
    791   1.1  hpeyerl 	/*
    792  1.12   brezak 	 * Don't forward if it didn't arrive from the
    793  1.12   brezak 	 * parent vif for its origin.
    794  1.12   brezak 	 *
    795  1.12   brezak 	 * Notes: v_ifp is zero for src route tunnels, multicast_decap_if
    796  1.12   brezak 	 * for encapsulated tunnels and a real ifnet for non-tunnels so
    797  1.12   brezak 	 * the first part of the if catches wrong physical interface or
    798  1.12   brezak 	 * tunnel type; v_rmt_addr is zero for non-tunneled packets so
    799  1.12   brezak 	 * the 2nd part catches both packets that arrive via a tunnel
    800  1.12   brezak 	 * that shouldn't and packets that arrive via the wrong tunnel.
    801   1.1  hpeyerl 	 */
    802   1.1  hpeyerl 	vifi = rt->mrt_parent;
    803  1.12   brezak 	if (viftable[vifi].v_ifp != ifp ||
    804  1.12   brezak 	    (ifp == 0 && viftable[vifi].v_rmt_addr.s_addr != tunnel_src)) {
    805  1.12   brezak 		/* came in the wrong interface */
    806  1.12   brezak 		++mrtstat.mrts_wrong_if;
    807  1.12   brezak 		return (int)tunnel_src;
    808   1.1  hpeyerl 	}
    809   1.1  hpeyerl 
    810   1.1  hpeyerl 	/*
    811   1.1  hpeyerl 	 * For each vif, decide if a copy of the packet should be forwarded.
    812   1.1  hpeyerl 	 * Forward if:
    813   1.1  hpeyerl 	 *		- the ttl exceeds the vif's threshold AND
    814   1.1  hpeyerl 	 *		- the vif is a child in the origin's route AND
    815   1.1  hpeyerl 	 *		- ( the vif is not a leaf in the origin's route OR
    816   1.1  hpeyerl 	 *		    the destination group has members on the vif )
    817   1.1  hpeyerl 	 *
    818   1.1  hpeyerl 	 * (This might be speeded up with some sort of cache -- someday.)
    819   1.1  hpeyerl 	 */
    820   1.1  hpeyerl 	for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++) {
    821   1.1  hpeyerl 		if (ip->ip_ttl > vifp->v_threshold &&
    822   1.1  hpeyerl 		    VIFM_ISSET(vifi, rt->mrt_children) &&
    823   1.1  hpeyerl 		    (!VIFM_ISSET(vifi, rt->mrt_leaves) ||
    824   1.1  hpeyerl 		    grplst_member(vifp, ip->ip_dst))) {
    825  1.12   brezak 			if (vifp->v_flags & VIFF_SRCRT)
    826  1.12   brezak 				srcrt_send(ip, vifp, m);
    827  1.12   brezak 			else if (vifp->v_flags & VIFF_TUNNEL)
    828  1.12   brezak 				encap_send(ip, vifp, m);
    829   1.1  hpeyerl 			else
    830  1.12   brezak 				phyint_send(ip, vifp, m);
    831   1.1  hpeyerl 		}
    832   1.1  hpeyerl 	}
    833   1.1  hpeyerl 	return ((int)tunnel_src);
    834   1.1  hpeyerl }
    835   1.1  hpeyerl 
    836   1.1  hpeyerl static void
    837  1.12   brezak phyint_send(ip, vifp, m)
    838  1.12   brezak 	register struct ip *ip;
    839  1.12   brezak 	register struct vif *vifp;
    840  1.10  mycroft 	register struct mbuf *m;
    841   1.1  hpeyerl {
    842   1.1  hpeyerl 	register struct mbuf *mb_copy;
    843   1.1  hpeyerl 	register struct ip_moptions *imo;
    844   1.1  hpeyerl 	register int error;
    845   1.1  hpeyerl 	struct ip_moptions simo;
    846   1.1  hpeyerl 
    847   1.1  hpeyerl 	mb_copy = m_copy(m, 0, M_COPYALL);
    848   1.1  hpeyerl 	if (mb_copy == NULL)
    849   1.1  hpeyerl 		return;
    850   1.1  hpeyerl 
    851   1.1  hpeyerl 	imo = &simo;
    852   1.1  hpeyerl 	imo->imo_multicast_ifp = vifp->v_ifp;
    853   1.1  hpeyerl 	imo->imo_multicast_ttl = ip->ip_ttl - 1;
    854   1.1  hpeyerl 	imo->imo_multicast_loop = 1;
    855   1.1  hpeyerl 
    856   1.4  mycroft 	error = ip_output(mb_copy, NULL, NULL, IP_FORWARDING, imo);
    857   1.1  hpeyerl }
    858   1.1  hpeyerl 
    859   1.1  hpeyerl static void
    860  1.12   brezak srcrt_send(ip, vifp, m)
    861  1.12   brezak 	register struct ip *ip;
    862  1.12   brezak 	register struct vif *vifp;
    863  1.10  mycroft 	register struct mbuf *m;
    864   1.1  hpeyerl {
    865   1.1  hpeyerl 	register struct mbuf *mb_copy, *mb_opts;
    866   1.1  hpeyerl 	register struct ip *ip_copy;
    867   1.1  hpeyerl 	register int error;
    868   1.1  hpeyerl 	register u_char *cp;
    869   1.1  hpeyerl 
    870   1.1  hpeyerl 	/*
    871   1.1  hpeyerl 	 * Make sure that adding the tunnel options won't exceed the
    872   1.1  hpeyerl 	 * maximum allowed number of option bytes.
    873   1.1  hpeyerl 	 */
    874   1.1  hpeyerl 	if (ip->ip_hl > (60 - TUNNEL_LEN) >> 2) {
    875   1.1  hpeyerl 		mrtstat.mrts_cant_tunnel++;
    876   1.1  hpeyerl 		return;
    877   1.1  hpeyerl 	}
    878   1.1  hpeyerl 
    879  1.12   brezak 	mb_copy = m_copy(m, 0, M_COPYALL);
    880   1.1  hpeyerl 	if (mb_copy == NULL)
    881   1.1  hpeyerl 		return;
    882  1.12   brezak 	ip_copy = mtod(mb_copy, struct ip *);
    883  1.12   brezak 	ip_copy->ip_ttl--;
    884  1.12   brezak 	ip_copy->ip_dst = vifp->v_rmt_addr;	/* remote tunnel end-point */
    885  1.12   brezak 	/*
    886  1.12   brezak 	 * Adjust the ip header length to account for the tunnel options.
    887  1.12   brezak 	 */
    888  1.12   brezak 	ip_copy->ip_hl += TUNNEL_LEN >> 2;
    889  1.12   brezak 	ip_copy->ip_len += TUNNEL_LEN;
    890   1.1  hpeyerl 	MGETHDR(mb_opts, M_DONTWAIT, MT_HEADER);
    891   1.1  hpeyerl 	if (mb_opts == NULL) {
    892   1.1  hpeyerl 		m_freem(mb_copy);
    893   1.1  hpeyerl 		return;
    894   1.1  hpeyerl 	}
    895   1.1  hpeyerl 	/*
    896  1.12   brezak 	 * 'Delete' the base ip header from the mb_copy chain
    897  1.12   brezak 	 */
    898  1.12   brezak 	mb_copy->m_len -= IP_HDR_LEN;
    899  1.12   brezak 	mb_copy->m_data += IP_HDR_LEN;
    900  1.12   brezak 	/*
    901   1.1  hpeyerl 	 * Make mb_opts be the new head of the packet chain.
    902   1.1  hpeyerl 	 * Any options of the packet were left in the old packet chain head
    903   1.1  hpeyerl 	 */
    904   1.1  hpeyerl 	mb_opts->m_next = mb_copy;
    905   1.1  hpeyerl 	mb_opts->m_len = IP_HDR_LEN + TUNNEL_LEN;
    906  1.12   brezak 	mb_opts->m_pkthdr.len = mb_copy->m_pkthdr.len + TUNNEL_LEN;
    907  1.12   brezak 	mb_opts->m_pkthdr.rcvif = mb_copy->m_pkthdr.rcvif;
    908   1.1  hpeyerl 	mb_opts->m_data += MSIZE - mb_opts->m_len;
    909  1.10  mycroft 	/*
    910  1.12   brezak 	 * Copy the base ip header from the mb_copy chain to the new head mbuf
    911  1.10  mycroft 	 */
    912  1.12   brezak 	bcopy((caddr_t)ip_copy, mtod(mb_opts, caddr_t), IP_HDR_LEN);
    913   1.1  hpeyerl 	/*
    914   1.1  hpeyerl 	 * Add the NOP and LSRR after the base ip header
    915   1.1  hpeyerl 	 */
    916  1.12   brezak 	cp = mtod(mb_opts, u_char *) + IP_HDR_LEN;
    917   1.1  hpeyerl 	*cp++ = IPOPT_NOP;
    918   1.1  hpeyerl 	*cp++ = IPOPT_LSRR;
    919   1.1  hpeyerl 	*cp++ = 11;		/* LSRR option length */
    920   1.1  hpeyerl 	*cp++ = 8;		/* LSSR pointer to second element */
    921   1.1  hpeyerl 	*(u_long*)cp = vifp->v_lcl_addr.s_addr;	/* local tunnel end-point */
    922   1.1  hpeyerl 	cp += 4;
    923   1.1  hpeyerl 	*(u_long*)cp = ip->ip_dst.s_addr;		/* destination group */
    924   1.1  hpeyerl 
    925   1.1  hpeyerl 	error = ip_output(mb_opts, NULL, NULL, IP_FORWARDING, NULL);
    926  1.12   brezak }
    927  1.12   brezak 
    928  1.12   brezak static void
    929  1.12   brezak encap_send(ip, vifp, m)
    930  1.12   brezak 	register struct ip *ip;
    931  1.12   brezak 	register struct vif *vifp;
    932  1.12   brezak 	register struct mbuf *m;
    933  1.12   brezak {
    934  1.12   brezak 	register struct mbuf *mb_copy;
    935  1.12   brezak 	register struct ip *ip_copy;
    936  1.12   brezak 	register int i, len = ip->ip_len;
    937  1.12   brezak 
    938  1.12   brezak 	/*
    939  1.12   brezak 	 * copy the old packet & pullup it's IP header into the
    940  1.12   brezak 	 * new mbuf so we can modify it.  Try to fill the new
    941  1.12   brezak 	 * mbuf since if we don't the ethernet driver will.
    942  1.12   brezak 	 */
    943  1.12   brezak 	MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
    944  1.12   brezak 	if (mb_copy == NULL)
    945  1.12   brezak 		return;
    946  1.12   brezak 	mb_copy->m_data += 16;
    947  1.12   brezak 	mb_copy->m_len = sizeof(multicast_encap_iphdr);
    948  1.12   brezak 	if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
    949  1.12   brezak 		m_freem(mb_copy);
    950  1.12   brezak 		return;
    951  1.12   brezak 	}
    952  1.12   brezak 	i = MHLEN - 16;
    953  1.12   brezak 	if (i > len)
    954  1.12   brezak 		i = len;
    955  1.12   brezak 	mb_copy = m_pullup(mb_copy, i);
    956  1.12   brezak 	if (mb_copy == NULL)
    957  1.12   brezak 		return;
    958  1.12   brezak 
    959  1.12   brezak 	/*
    960  1.12   brezak 	 * fill in the encapsulating IP header.
    961  1.12   brezak 	 */
    962  1.12   brezak 	ip_copy = mtod(mb_copy, struct ip *);
    963  1.12   brezak 	*ip_copy = multicast_encap_iphdr;
    964  1.12   brezak 	ip_copy->ip_id = htons(ip_id++);
    965  1.12   brezak 	ip_copy->ip_len += len;
    966  1.12   brezak 	ip_copy->ip_src = vifp->v_lcl_addr;
    967  1.12   brezak 	ip_copy->ip_dst = vifp->v_rmt_addr;
    968  1.12   brezak 
    969  1.12   brezak 	/*
    970  1.12   brezak 	 * turn the encapsulated IP header back into a valid one.
    971  1.12   brezak 	 */
    972  1.12   brezak 	ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
    973  1.12   brezak 	--ip->ip_ttl;
    974  1.12   brezak 	HTONS(ip->ip_len);
    975  1.12   brezak 	HTONS(ip->ip_off);
    976  1.12   brezak 	ip->ip_sum = 0;
    977  1.12   brezak #if defined(LBL) && !defined(ultrix) && !defined(i386)
    978  1.12   brezak 	ip->ip_sum = ~oc_cksum((caddr_t)ip, ip->ip_hl << 2, 0);
    979  1.12   brezak #else
    980  1.12   brezak 	mb_copy->m_data += sizeof(multicast_encap_iphdr);
    981  1.12   brezak 	ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
    982  1.12   brezak 	mb_copy->m_data -= sizeof(multicast_encap_iphdr);
    983  1.12   brezak 	mb_copy->m_pkthdr.len = m->m_pkthdr.len + sizeof(multicast_encap_iphdr);
    984  1.12   brezak 	mb_copy->m_pkthdr.rcvif = m->m_pkthdr.rcvif;
    985  1.12   brezak #endif
    986  1.12   brezak 	ip_output(mb_copy, (struct mbuf *)0, (struct route *)0,
    987  1.12   brezak 		  IP_FORWARDING, (struct ip_moptions *)0);
    988  1.12   brezak }
    989  1.12   brezak 
    990  1.12   brezak /*
    991  1.12   brezak  * De-encapsulate a packet and feed it back through ip input (this
    992  1.12   brezak  * routine is called whenever IP gets a packet with proto type
    993  1.12   brezak  * ENCAP_PROTO and a local destination address).
    994  1.12   brezak  */
    995  1.12   brezak static void
    996  1.12   brezak multiencap_decap(m, hlen)
    997  1.12   brezak 	register struct mbuf *m;
    998  1.12   brezak 	int hlen;
    999  1.12   brezak {
   1000  1.12   brezak 	struct ifnet *ifp;
   1001  1.12   brezak 	register struct ip *ip = mtod(m, struct ip *);
   1002  1.12   brezak 	register int s;
   1003  1.12   brezak 	register struct ifqueue *ifq;
   1004  1.12   brezak 	register struct vif *vifp;
   1005  1.12   brezak 
   1006  1.12   brezak 	if (ip->ip_p != ENCAP_PROTO) {
   1007  1.12   brezak 		(*encap_oldrawip)(m, hlen);
   1008  1.12   brezak 		return;
   1009  1.12   brezak 	}
   1010  1.12   brezak 	/*
   1011  1.12   brezak 	 * dump the packet if it's not to a multicast destination or if
   1012  1.12   brezak 	 * we don't have an encapsulating tunnel with the source.
   1013  1.12   brezak 	 * Note:  This code assumes that the remote site IP address
   1014  1.12   brezak 	 * uniquely identifies the tunnel (i.e., that this site has
   1015  1.12   brezak 	 * at most one tunnel with the remote site).
   1016  1.12   brezak 	 */
   1017  1.12   brezak 	if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
   1018  1.12   brezak 		++mrtstat.mrts_bad_tunnel;
   1019  1.12   brezak 		m_freem(m);
   1020  1.12   brezak 		return;
   1021  1.12   brezak 	}
   1022  1.12   brezak 	if (ip->ip_src.s_addr != last_encap_src) {
   1023  1.12   brezak 		register struct vif *vife;
   1024  1.12   brezak 
   1025  1.12   brezak 		vifp = viftable;
   1026  1.12   brezak 		vife = vifp + numvifs;
   1027  1.12   brezak 		last_encap_src = ip->ip_src.s_addr;
   1028  1.12   brezak 		last_encap_vif = 0;
   1029  1.12   brezak 		for ( ; vifp < vife; ++vifp)
   1030  1.12   brezak 			if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
   1031  1.12   brezak 				if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
   1032  1.12   brezak 				    == VIFF_TUNNEL)
   1033  1.12   brezak 					last_encap_vif = vifp;
   1034  1.12   brezak 				break;
   1035  1.12   brezak 			}
   1036  1.12   brezak 	}
   1037  1.12   brezak 	if ((vifp = last_encap_vif) == 0) {
   1038  1.12   brezak 		mrtstat.mrts_cant_tunnel++; /*XXX*/
   1039  1.12   brezak 		m_freem(m);
   1040  1.12   brezak 		return;
   1041  1.12   brezak 	}
   1042  1.12   brezak 	ifp = vifp->v_ifp;
   1043  1.12   brezak 	m->m_data += hlen;
   1044  1.12   brezak 	m->m_len -= hlen;
   1045  1.12   brezak 	m->m_pkthdr.rcvif = ifp;
   1046  1.12   brezak 	m->m_pkthdr.len -= hlen;
   1047  1.12   brezak 	ifq = &ipintrq;
   1048  1.12   brezak 	s = splimp();
   1049  1.12   brezak 	if (IF_QFULL(ifq)) {
   1050  1.12   brezak 		IF_DROP(ifq);
   1051  1.12   brezak 		m_freem(m);
   1052  1.12   brezak 	} else {
   1053  1.12   brezak 		IF_ENQUEUE(ifq, m);
   1054  1.12   brezak 		/*
   1055  1.12   brezak 		 * normally we would need a "schednetisr(NETISR_IP)"
   1056  1.12   brezak 		 * here but we were called by ip_input and it is going
   1057  1.12   brezak 		 * to loop back & try to dequeue the packet we just
   1058  1.12   brezak 		 * queued as soon as we return so we avoid the
   1059  1.12   brezak 		 * unnecessary software interrrupt.
   1060  1.12   brezak 		 */
   1061  1.12   brezak 	}
   1062  1.12   brezak 	splx(s);
   1063   1.1  hpeyerl }
   1064   1.1  hpeyerl #endif
   1065