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