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