Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.82
      1 /*	$NetBSD: in_gif.c,v 1.82 2016/08/01 03:15:30 ozaki-r Exp $	*/
      2 /*	$KAME: in_gif.c,v 1.66 2001/07/29 04:46:09 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 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: in_gif.c,v 1.82 2016/08/01 03:15:30 ozaki-r Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_inet.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/socket.h>
     43 #include <sys/sockio.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/errno.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/syslog.h>
     48 #include <sys/kernel.h>
     49 
     50 #include <net/if.h>
     51 #include <net/route.h>
     52 
     53 #include <netinet/in.h>
     54 #include <netinet/in_systm.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/ip_var.h>
     57 #include <netinet/in_gif.h>
     58 #include <netinet/in_var.h>
     59 #include <netinet/ip_encap.h>
     60 #include <netinet/ip_ecn.h>
     61 
     62 #ifdef INET6
     63 #include <netinet/ip6.h>
     64 #endif
     65 
     66 #include <net/if_gif.h>
     67 
     68 #include "gif.h"
     69 
     70 #include <net/net_osdep.h>
     71 
     72 static int gif_validate4(const struct ip *, struct gif_softc *,
     73 	struct ifnet *);
     74 
     75 #if NGIF > 0
     76 int ip_gif_ttl = GIF_TTL;
     77 #else
     78 int ip_gif_ttl = 0;
     79 #endif
     80 
     81 static const struct encapsw in_gif_encapsw = {
     82 	.encapsw4 = {
     83 		.pr_input	= in_gif_input,
     84 		.pr_ctlinput	= NULL,
     85 	}
     86 };
     87 
     88 int
     89 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
     90 {
     91 	struct rtentry *rt;
     92 	struct gif_softc *sc = ifp->if_softc;
     93 	struct sockaddr_in *sin_src = satosin(sc->gif_psrc);
     94 	struct sockaddr_in *sin_dst = satosin(sc->gif_pdst);
     95 	struct ip iphdr;	/* capsule IP header, host byte ordered */
     96 	int proto, error;
     97 	u_int8_t tos;
     98 	union {
     99 		struct sockaddr		dst;
    100 		struct sockaddr_in	dst4;
    101 	} u;
    102 
    103 	if (sin_src == NULL || sin_dst == NULL ||
    104 	    sin_src->sin_family != AF_INET ||
    105 	    sin_dst->sin_family != AF_INET) {
    106 		m_freem(m);
    107 		return EAFNOSUPPORT;
    108 	}
    109 
    110 	switch (family) {
    111 #ifdef INET
    112 	case AF_INET:
    113 	    {
    114 		const struct ip *ip;
    115 
    116 		proto = IPPROTO_IPV4;
    117 		if (m->m_len < sizeof(*ip)) {
    118 			m = m_pullup(m, sizeof(*ip));
    119 			if (m == NULL)
    120 				return ENOBUFS;
    121 		}
    122 		ip = mtod(m, const struct ip *);
    123 		tos = ip->ip_tos;
    124 		break;
    125 	    }
    126 #endif /* INET */
    127 #ifdef INET6
    128 	case AF_INET6:
    129 	    {
    130 		const struct ip6_hdr *ip6;
    131 		proto = IPPROTO_IPV6;
    132 		if (m->m_len < sizeof(*ip6)) {
    133 			m = m_pullup(m, sizeof(*ip6));
    134 			if (m == NULL)
    135 				return ENOBUFS;
    136 		}
    137 		ip6 = mtod(m, const struct ip6_hdr *);
    138 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    139 		break;
    140 	    }
    141 #endif /* INET6 */
    142 	default:
    143 #ifdef DEBUG
    144 		printf("in_gif_output: warning: unknown family %d passed\n",
    145 			family);
    146 #endif
    147 		m_freem(m);
    148 		return EAFNOSUPPORT;
    149 	}
    150 
    151 	memset(&iphdr, 0, sizeof(iphdr));
    152 	iphdr.ip_src = sin_src->sin_addr;
    153 	/* bidirectional configured tunnel mode */
    154 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    155 		iphdr.ip_dst = sin_dst->sin_addr;
    156 	else {
    157 		m_freem(m);
    158 		return ENETUNREACH;
    159 	}
    160 	iphdr.ip_p = proto;
    161 	/* version will be set in ip_output() */
    162 	iphdr.ip_ttl = ip_gif_ttl;
    163 	iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
    164 	if (ifp->if_flags & IFF_LINK1)
    165 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
    166 	else
    167 		ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
    168 
    169 	/* prepend new IP header */
    170 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    171 	/* XXX Is m_pullup really necessary after M_PREPEND? */
    172 	if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
    173 		m = m_pullup(m, sizeof(struct ip));
    174 	if (m == NULL)
    175 		return ENOBUFS;
    176 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
    177 
    178 	sockaddr_in_init(&u.dst4, &sin_dst->sin_addr, 0);
    179 	if ((rt = rtcache_lookup(&sc->gif_ro, &u.dst)) == NULL) {
    180 		m_freem(m);
    181 		return ENETUNREACH;
    182 	}
    183 
    184 	/* If the route constitutes infinite encapsulation, punt. */
    185 	if (rt->rt_ifp == ifp) {
    186 		rtcache_free(&sc->gif_ro);
    187 		m_freem(m);
    188 		return ENETUNREACH;	/*XXX*/
    189 	}
    190 
    191 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
    192 	return (error);
    193 }
    194 
    195 void
    196 in_gif_input(struct mbuf *m, int off, int proto)
    197 {
    198 	struct ifnet *gifp = NULL;
    199 	const struct ip *ip;
    200 	int af;
    201 	u_int8_t otos;
    202 
    203 	ip = mtod(m, const struct ip *);
    204 
    205 	gifp = (struct ifnet *)encap_getarg(m);
    206 
    207 	if (gifp == NULL || (gifp->if_flags & (IFF_UP|IFF_RUNNING))
    208 		!= (IFF_UP|IFF_RUNNING)) {
    209 		m_freem(m);
    210 		ip_statinc(IP_STAT_NOGIF);
    211 		return;
    212 	}
    213 #ifndef GIF_ENCAPCHECK
    214 	struct gif_softc *sc = (struct gif_softc *)gifp->if_softc;
    215 	/* other CPU do delete_tunnel */
    216 	if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    217 		m_freem(m);
    218 		ip_statinc(IP_STAT_NOGIF);
    219 		return;
    220 	}
    221 
    222 	struct ifnet *rcvif;
    223 	struct psref psref;
    224 	rcvif = m_get_rcvif_psref(m, &psref);
    225 	if (!gif_validate4(ip, sc, rcvif)) {
    226 		m_put_rcvif_psref(rcvif, &psref);
    227 		m_freem(m);
    228 		ip_statinc(IP_STAT_NOGIF);
    229 		return;
    230 	}
    231 	m_put_rcvif_psref(rcvif, &psref);
    232 #endif
    233 	otos = ip->ip_tos;
    234 	m_adj(m, off);
    235 
    236 	switch (proto) {
    237 #ifdef INET
    238 	case IPPROTO_IPV4:
    239 	    {
    240 		struct ip *xip;
    241 		af = AF_INET;
    242 		if (M_UNWRITABLE(m, sizeof(*xip))) {
    243 			if ((m = m_pullup(m, sizeof(*xip))) == NULL)
    244 				return;
    245 		}
    246 		xip = mtod(m, struct ip *);
    247 		if (gifp->if_flags & IFF_LINK1)
    248 			ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
    249 		else
    250 			ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
    251 		break;
    252 	    }
    253 #endif
    254 #ifdef INET6
    255 	case IPPROTO_IPV6:
    256 	    {
    257 		struct ip6_hdr *ip6;
    258 		u_int8_t itos;
    259 		af = AF_INET6;
    260 		if (M_UNWRITABLE(m, sizeof(*ip6))) {
    261 			if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
    262 				return;
    263 		}
    264 		ip6 = mtod(m, struct ip6_hdr *);
    265 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    266 		if (gifp->if_flags & IFF_LINK1)
    267 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    268 		else
    269 			ip_ecn_egress(ECN_NOCARE, &otos, &itos);
    270 		ip6->ip6_flow &= ~htonl(0xff << 20);
    271 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    272 		break;
    273 	    }
    274 #endif /* INET6 */
    275 	default:
    276 		ip_statinc(IP_STAT_NOGIF);
    277 		m_freem(m);
    278 		return;
    279 	}
    280 	gif_input(m, af, gifp);
    281 	return;
    282 }
    283 
    284 /*
    285  * validate outer address.
    286  */
    287 static int
    288 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
    289 {
    290 	struct sockaddr_in *src, *dst;
    291 	struct in_ifaddr *ia4;
    292 	int s;
    293 
    294 	src = satosin(sc->gif_psrc);
    295 	dst = satosin(sc->gif_pdst);
    296 
    297 	/* check for address match */
    298 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
    299 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
    300 		return 0;
    301 
    302 	/* martian filters on outer source - NOT done in ip_input! */
    303 	if (IN_MULTICAST(ip->ip_src.s_addr))
    304 		return 0;
    305 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
    306 	case 0: case 127: case 255:
    307 		return 0;
    308 	}
    309 	/* reject packets with broadcast on source */
    310 	s = pserialize_read_enter();
    311 	IN_ADDRLIST_READER_FOREACH(ia4) {
    312 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
    313 			continue;
    314 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
    315 			pserialize_read_exit(s);
    316 			return 0;
    317 		}
    318 	}
    319 	pserialize_read_exit(s);
    320 
    321 	/* ingress filters on outer source */
    322 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
    323 		union {
    324 			struct sockaddr sa;
    325 			struct sockaddr_in sin;
    326 		} u;
    327 		struct rtentry *rt;
    328 
    329 		sockaddr_in_init(&u.sin, &ip->ip_src, 0);
    330 		rt = rtalloc1(&u.sa, 0);
    331 		if (rt == NULL || rt->rt_ifp != ifp) {
    332 #if 0
    333 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
    334 			    "due to ingress filter\n", if_name(&sc->gif_if),
    335 			    (u_int32_t)ntohl(u.sin.sin_addr.s_addr));
    336 #endif
    337 			if (rt != NULL)
    338 				rtfree(rt);
    339 			return 0;
    340 		}
    341 		rtfree(rt);
    342 	}
    343 
    344 	return 32 * 2;
    345 }
    346 
    347 #ifdef GIF_ENCAPCHECK
    348 /*
    349  * we know that we are in IFF_UP, outer address available, and outer family
    350  * matched the physical addr family.  see gif_encapcheck().
    351  */
    352 int
    353 gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
    354 {
    355 	struct ip ip;
    356 	struct gif_softc *sc;
    357 	struct ifnet *ifp = NULL;
    358 	int r;
    359 	struct psref psref;
    360 
    361 	/* sanity check done in caller */
    362 	sc = arg;
    363 
    364 	m_copydata(m, 0, sizeof(ip), &ip);
    365 	if ((m->m_flags & M_PKTHDR) != 0)
    366 		ifp = m_get_rcvif_psref(m, &psref);
    367 
    368 	r = gif_validate4(&ip, sc, ifp);
    369 
    370 	m_put_rcvif_psref(ifp, &psref);
    371 	return r;
    372 }
    373 #endif
    374 
    375 int
    376 in_gif_attach(struct gif_softc *sc)
    377 {
    378 #ifndef GIF_ENCAPCHECK
    379 	struct sockaddr_in mask4;
    380 
    381 	memset(&mask4, 0, sizeof(mask4));
    382 	mask4.sin_len = sizeof(struct sockaddr_in);
    383 	mask4.sin_addr.s_addr = ~0;
    384 
    385 	if (!sc->gif_psrc || !sc->gif_pdst)
    386 		return EINVAL;
    387 	sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
    388 	    (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
    389 	    &in_gif_encapsw, sc);
    390 #else
    391 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
    392 	    &in_gif_encapsw, sc);
    393 #endif
    394 	if (sc->encap_cookie4 == NULL)
    395 		return EEXIST;
    396 	return 0;
    397 }
    398 
    399 int
    400 in_gif_detach(struct gif_softc *sc)
    401 {
    402 	int error;
    403 
    404 	error = in_gif_pause(sc);
    405 
    406 	rtcache_free(&sc->gif_ro);
    407 
    408 	return error;
    409 }
    410 
    411 int
    412 in_gif_pause(struct gif_softc *sc)
    413 {
    414 	int error;
    415 
    416 	error = encap_detach(sc->encap_cookie4);
    417 	if (error == 0)
    418 		sc->encap_cookie4 = NULL;
    419 
    420 	return error;
    421 }
    422