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