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