Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.7
      1  1.7   itojun /*	$NetBSD: in_gif.c,v 1.7 1999/12/13 15:17:20 itojun Exp $	*/
      2  1.3  thorpej 
      3  1.2   itojun /*
      4  1.2   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.2   itojun  * All rights reserved.
      6  1.2   itojun  *
      7  1.2   itojun  * Redistribution and use in source and binary forms, with or without
      8  1.2   itojun  * modification, are permitted provided that the following conditions
      9  1.2   itojun  * are met:
     10  1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.2   itojun  *    documentation and/or other materials provided with the distribution.
     15  1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.2   itojun  *    may be used to endorse or promote products derived from this software
     17  1.2   itojun  *    without specific prior written permission.
     18  1.2   itojun  *
     19  1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.2   itojun  * SUCH DAMAGE.
     30  1.2   itojun  */
     31  1.2   itojun 
     32  1.2   itojun /*
     33  1.2   itojun  * in_gif.c
     34  1.2   itojun  */
     35  1.2   itojun 
     36  1.2   itojun #ifdef __FreeBSD__
     37  1.2   itojun #include "opt_mrouting.h"
     38  1.2   itojun #endif
     39  1.2   itojun #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
     40  1.2   itojun #include "opt_inet.h"
     41  1.7   itojun #ifdef __NetBSD__	/*XXX*/
     42  1.5  thorpej #include "opt_ipsec.h"
     43  1.2   itojun #endif
     44  1.7   itojun #endif
     45  1.2   itojun 
     46  1.2   itojun #include <sys/param.h>
     47  1.2   itojun #include <sys/systm.h>
     48  1.2   itojun #include <sys/socket.h>
     49  1.2   itojun #include <sys/sockio.h>
     50  1.2   itojun #include <sys/mbuf.h>
     51  1.2   itojun #include <sys/errno.h>
     52  1.7   itojun #ifdef __FreeBSD__
     53  1.7   itojun #include <sys/kernel.h>
     54  1.7   itojun #include <sys/sysctl.h>
     55  1.7   itojun #endif
     56  1.2   itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     57  1.2   itojun #include <sys/ioctl.h>
     58  1.2   itojun #endif
     59  1.2   itojun #include <sys/protosw.h>
     60  1.2   itojun 
     61  1.2   itojun #include <net/if.h>
     62  1.2   itojun #include <net/route.h>
     63  1.2   itojun #include <net/if_gif.h>
     64  1.2   itojun 
     65  1.2   itojun #include <netinet/in.h>
     66  1.2   itojun #include <netinet/in_systm.h>
     67  1.2   itojun #include <netinet/ip.h>
     68  1.2   itojun #include <netinet/ip_var.h>
     69  1.2   itojun #include <netinet/in_gif.h>
     70  1.2   itojun #include <netinet/ip_ecn.h>
     71  1.2   itojun 
     72  1.2   itojun #ifdef INET6
     73  1.2   itojun #include <netinet/ip6.h>
     74  1.2   itojun #endif
     75  1.2   itojun 
     76  1.2   itojun #ifdef MROUTING
     77  1.2   itojun #include <netinet/ip_mroute.h>
     78  1.2   itojun #endif /* MROUTING */
     79  1.2   itojun 
     80  1.2   itojun #include <net/if_gif.h>
     81  1.2   itojun 
     82  1.2   itojun #include "gif.h"
     83  1.2   itojun 
     84  1.2   itojun #include <machine/stdarg.h>
     85  1.7   itojun 
     86  1.7   itojun #include <net/net_osdep.h>
     87  1.2   itojun 
     88  1.2   itojun #if NGIF > 0
     89  1.4   itojun int ip_gif_ttl = GIF_TTL;
     90  1.2   itojun #else
     91  1.4   itojun int ip_gif_ttl = 0;
     92  1.2   itojun #endif
     93  1.7   itojun #ifdef __FreeBSD__
     94  1.7   itojun SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
     95  1.7   itojun 	&ip_gif_ttl,	0, "");
     96  1.7   itojun #endif
     97  1.2   itojun 
     98  1.2   itojun int
     99  1.2   itojun in_gif_output(ifp, family, m, rt)
    100  1.2   itojun 	struct ifnet	*ifp;
    101  1.2   itojun 	int		family;
    102  1.2   itojun 	struct mbuf	*m;
    103  1.2   itojun 	struct rtentry *rt;
    104  1.2   itojun {
    105  1.2   itojun 	register struct gif_softc *sc = (struct gif_softc*)ifp;
    106  1.2   itojun 	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
    107  1.2   itojun 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
    108  1.2   itojun 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
    109  1.2   itojun 	struct ip iphdr;	/* capsule IP header, host byte ordered */
    110  1.2   itojun 	int proto, error;
    111  1.2   itojun 	u_int8_t tos;
    112  1.2   itojun 
    113  1.2   itojun 	if (sin_src == NULL || sin_dst == NULL ||
    114  1.2   itojun 	    sin_src->sin_family != AF_INET ||
    115  1.2   itojun 	    sin_dst->sin_family != AF_INET) {
    116  1.2   itojun 		m_freem(m);
    117  1.2   itojun 		return EAFNOSUPPORT;
    118  1.2   itojun 	}
    119  1.2   itojun 
    120  1.2   itojun 	switch (family) {
    121  1.2   itojun #ifdef INET
    122  1.2   itojun 	case AF_INET:
    123  1.2   itojun 	    {
    124  1.2   itojun 		struct ip *ip;
    125  1.2   itojun 
    126  1.2   itojun 		proto = IPPROTO_IPV4;
    127  1.2   itojun 		if (m->m_len < sizeof(*ip)) {
    128  1.2   itojun 			m = m_pullup(m, sizeof(*ip));
    129  1.2   itojun 			if (!m)
    130  1.2   itojun 				return ENOBUFS;
    131  1.2   itojun 		}
    132  1.2   itojun 		ip = mtod(m, struct ip *);
    133  1.2   itojun 		tos = ip->ip_tos;
    134  1.2   itojun 		break;
    135  1.2   itojun 	    }
    136  1.2   itojun #endif /*INET*/
    137  1.2   itojun #ifdef INET6
    138  1.2   itojun 	case AF_INET6:
    139  1.2   itojun 	    {
    140  1.2   itojun 		struct ip6_hdr *ip6;
    141  1.2   itojun 		proto = IPPROTO_IPV6;
    142  1.2   itojun 		if (m->m_len < sizeof(*ip6)) {
    143  1.2   itojun 			m = m_pullup(m, sizeof(*ip6));
    144  1.2   itojun 			if (!m)
    145  1.2   itojun 				return ENOBUFS;
    146  1.2   itojun 		}
    147  1.2   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    148  1.2   itojun 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    149  1.2   itojun 		break;
    150  1.2   itojun 	    }
    151  1.2   itojun #endif /*INET6*/
    152  1.2   itojun 	default:
    153  1.2   itojun #ifdef DIAGNOSTIC
    154  1.2   itojun 		printf("in_gif_output: warning: unknown family %d passed\n",
    155  1.2   itojun 			family);
    156  1.2   itojun #endif
    157  1.2   itojun 		m_freem(m);
    158  1.2   itojun 		return EAFNOSUPPORT;
    159  1.2   itojun 	}
    160  1.2   itojun 
    161  1.2   itojun 	bzero(&iphdr, sizeof(iphdr));
    162  1.2   itojun 	iphdr.ip_src = sin_src->sin_addr;
    163  1.2   itojun 	if (ifp->if_flags & IFF_LINK0) {
    164  1.2   itojun 		/* multi-destination mode */
    165  1.2   itojun 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    166  1.2   itojun 			iphdr.ip_dst = sin_dst->sin_addr;
    167  1.2   itojun 		else if (rt) {
    168  1.7   itojun 			if (family != AF_INET) {
    169  1.7   itojun 				m_freem(m);
    170  1.7   itojun 				return EINVAL;	/*XXX*/
    171  1.7   itojun 			}
    172  1.2   itojun 			iphdr.ip_dst = ((struct sockaddr_in *)
    173  1.2   itojun 					(rt->rt_gateway))->sin_addr;
    174  1.2   itojun 		} else {
    175  1.2   itojun 			m_freem(m);
    176  1.2   itojun 			return ENETUNREACH;
    177  1.2   itojun 		}
    178  1.2   itojun 	} else {
    179  1.2   itojun 		/* bidirectional configured tunnel mode */
    180  1.2   itojun 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    181  1.2   itojun 			iphdr.ip_dst = sin_dst->sin_addr;
    182  1.2   itojun 		else {
    183  1.2   itojun 			m_freem(m);
    184  1.2   itojun 			return ENETUNREACH;
    185  1.2   itojun 		}
    186  1.2   itojun 	}
    187  1.2   itojun 	iphdr.ip_p = proto;
    188  1.2   itojun 	/* version will be set in ip_output() */
    189  1.4   itojun 	iphdr.ip_ttl = ip_gif_ttl;
    190  1.2   itojun 	iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
    191  1.2   itojun 	if (ifp->if_flags & IFF_LINK1)
    192  1.2   itojun 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
    193  1.2   itojun 
    194  1.2   itojun 	/* prepend new IP header */
    195  1.2   itojun 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    196  1.2   itojun 	if (m && m->m_len < sizeof(struct ip))
    197  1.2   itojun 		m = m_pullup(m, sizeof(struct ip));
    198  1.2   itojun 	if (m == NULL) {
    199  1.2   itojun 		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
    200  1.2   itojun 		return ENOBUFS;
    201  1.2   itojun 	}
    202  1.2   itojun 
    203  1.2   itojun 	*(mtod(m, struct ip *)) = iphdr;
    204  1.2   itojun 
    205  1.2   itojun 	if (dst->sin_family != sin_dst->sin_family ||
    206  1.2   itojun 	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
    207  1.2   itojun 		/* cache route doesn't match */
    208  1.2   itojun 		dst->sin_family = sin_dst->sin_family;
    209  1.2   itojun 		dst->sin_len = sizeof(struct sockaddr_in);
    210  1.2   itojun 		dst->sin_addr = sin_dst->sin_addr;
    211  1.2   itojun 		if (sc->gif_ro.ro_rt) {
    212  1.2   itojun 			RTFREE(sc->gif_ro.ro_rt);
    213  1.2   itojun 			sc->gif_ro.ro_rt = NULL;
    214  1.2   itojun 		}
    215  1.2   itojun #if 0
    216  1.2   itojun 		sc->gif_if.if_mtu = GIF_MTU;
    217  1.2   itojun #endif
    218  1.2   itojun 	}
    219  1.2   itojun 
    220  1.2   itojun 	if (sc->gif_ro.ro_rt == NULL) {
    221  1.2   itojun 		rtalloc(&sc->gif_ro);
    222  1.2   itojun 		if (sc->gif_ro.ro_rt == NULL) {
    223  1.2   itojun 			m_freem(m);
    224  1.2   itojun 			return ENETUNREACH;
    225  1.2   itojun 		}
    226  1.2   itojun #if 0
    227  1.2   itojun 		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
    228  1.2   itojun 			- sizeof(struct ip);
    229  1.2   itojun #endif
    230  1.2   itojun 	}
    231  1.2   itojun 
    232  1.2   itojun #ifdef IPSEC
    233  1.7   itojun #ifndef __OpenBSD__	/*KAME IPSEC*/
    234  1.2   itojun 	m->m_pkthdr.rcvif = NULL;
    235  1.7   itojun #endif
    236  1.2   itojun #endif /*IPSEC*/
    237  1.7   itojun #ifndef __OpenBSD__
    238  1.7   itojun 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL);
    239  1.7   itojun #else
    240  1.7   itojun 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
    241  1.7   itojun #endif
    242  1.2   itojun 	return(error);
    243  1.2   itojun }
    244  1.2   itojun 
    245  1.2   itojun void
    246  1.2   itojun #if __STDC__
    247  1.2   itojun in_gif_input(struct mbuf *m, ...)
    248  1.2   itojun #else
    249  1.2   itojun in_gif_input(m, va_alist)
    250  1.2   itojun 	struct mbuf *m;
    251  1.2   itojun 	va_dcl
    252  1.2   itojun #endif
    253  1.2   itojun {
    254  1.2   itojun 	int off, proto;
    255  1.2   itojun 	struct gif_softc *sc;
    256  1.2   itojun 	struct ifnet *gifp = NULL;
    257  1.2   itojun 	struct ip *ip;
    258  1.2   itojun 	int i, af;
    259  1.2   itojun 	va_list ap;
    260  1.2   itojun 	u_int8_t otos;
    261  1.2   itojun 
    262  1.2   itojun 	va_start(ap, m);
    263  1.2   itojun 	off = va_arg(ap, int);
    264  1.2   itojun 	proto = va_arg(ap, int);
    265  1.2   itojun 	va_end(ap);
    266  1.2   itojun 
    267  1.2   itojun 	ip = mtod(m, struct ip *);
    268  1.2   itojun 
    269  1.2   itojun 	/* this code will be soon improved. */
    270  1.2   itojun #define	satosin(sa)	((struct sockaddr_in *)(sa))
    271  1.2   itojun 	for (i = 0, sc = gif; i < ngif; i++, sc++) {
    272  1.2   itojun 		if (sc->gif_psrc == NULL
    273  1.2   itojun 		 || sc->gif_pdst == NULL
    274  1.2   itojun 		 || sc->gif_psrc->sa_family != AF_INET
    275  1.2   itojun 		 || sc->gif_pdst->sa_family != AF_INET) {
    276  1.2   itojun 			continue;
    277  1.2   itojun 		}
    278  1.6   itojun 
    279  1.6   itojun 		if ((sc->gif_if.if_flags & IFF_UP) == 0)
    280  1.6   itojun 			continue;
    281  1.2   itojun 
    282  1.2   itojun 		if ((sc->gif_if.if_flags & IFF_LINK0)
    283  1.2   itojun 		 && satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    284  1.2   itojun 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == INADDR_ANY) {
    285  1.2   itojun 			gifp = &sc->gif_if;
    286  1.2   itojun 			continue;
    287  1.2   itojun 		}
    288  1.2   itojun 
    289  1.2   itojun 		if (satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    290  1.2   itojun 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == ip->ip_src.s_addr)
    291  1.2   itojun 		{
    292  1.2   itojun 			gifp = &sc->gif_if;
    293  1.2   itojun 			break;
    294  1.2   itojun 		}
    295  1.2   itojun 	}
    296  1.2   itojun 
    297  1.2   itojun 	if (gifp == NULL) {
    298  1.2   itojun #ifdef MROUTING
    299  1.2   itojun 		/* for backward compatibility */
    300  1.2   itojun 		if (proto == IPPROTO_IPV4) {
    301  1.2   itojun 			ipip_input(m, off, proto);
    302  1.2   itojun 			return;
    303  1.2   itojun 		}
    304  1.2   itojun #endif /*MROUTING*/
    305  1.2   itojun 		m_freem(m);
    306  1.2   itojun 		ipstat.ips_nogif++;
    307  1.2   itojun 		return;
    308  1.2   itojun 	}
    309  1.2   itojun 
    310  1.2   itojun 	otos = ip->ip_tos;
    311  1.2   itojun 	m_adj(m, off);
    312  1.2   itojun 
    313  1.2   itojun 	switch (proto) {
    314  1.2   itojun #ifdef INET
    315  1.2   itojun 	case IPPROTO_IPV4:
    316  1.2   itojun 	    {
    317  1.2   itojun 		struct ip *ip;
    318  1.2   itojun 		af = AF_INET;
    319  1.2   itojun 		if (m->m_len < sizeof(*ip)) {
    320  1.2   itojun 			m = m_pullup(m, sizeof(*ip));
    321  1.2   itojun 			if (!m)
    322  1.2   itojun 				return;
    323  1.2   itojun 		}
    324  1.2   itojun 		ip = mtod(m, struct ip *);
    325  1.2   itojun 		if (gifp->if_flags & IFF_LINK1)
    326  1.2   itojun 			ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
    327  1.2   itojun 		break;
    328  1.2   itojun 	    }
    329  1.2   itojun #endif
    330  1.2   itojun #ifdef INET6
    331  1.2   itojun 	case IPPROTO_IPV6:
    332  1.2   itojun 	    {
    333  1.2   itojun 		struct ip6_hdr *ip6;
    334  1.2   itojun 		u_int8_t itos;
    335  1.2   itojun 		af = AF_INET6;
    336  1.2   itojun 		if (m->m_len < sizeof(*ip6)) {
    337  1.2   itojun 			m = m_pullup(m, sizeof(*ip6));
    338  1.2   itojun 			if (!m)
    339  1.2   itojun 				return;
    340  1.2   itojun 		}
    341  1.2   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    342  1.2   itojun 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    343  1.2   itojun 		if (gifp->if_flags & IFF_LINK1)
    344  1.2   itojun 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    345  1.2   itojun 		ip6->ip6_flow &= ~htonl(0xff << 20);
    346  1.2   itojun 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    347  1.2   itojun 		break;
    348  1.2   itojun 	    }
    349  1.2   itojun #endif /* INET6 */
    350  1.2   itojun 	default:
    351  1.2   itojun 		ipstat.ips_nogif++;
    352  1.2   itojun 		m_freem(m);
    353  1.2   itojun 		return;
    354  1.2   itojun 	}
    355  1.2   itojun 	gif_input(m, af, gifp);
    356  1.2   itojun 	return;
    357  1.2   itojun }
    358