Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.65
      1 /*	$NetBSD: in_gif.c,v 1.65 2015/08/24 22:21:26 pooka 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.65 2015/08/24 22:21:26 pooka 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/protosw.h>
     49 #include <sys/kernel.h>
     50 
     51 #include <net/if.h>
     52 #include <net/route.h>
     53 
     54 #include <netinet/in.h>
     55 #include <netinet/in_systm.h>
     56 #include <netinet/ip.h>
     57 #include <netinet/ip_var.h>
     58 #include <netinet/in_gif.h>
     59 #include <netinet/in_var.h>
     60 #include <netinet/ip_encap.h>
     61 #include <netinet/ip_ecn.h>
     62 
     63 #ifdef INET6
     64 #include <netinet/ip6.h>
     65 #endif
     66 
     67 #include <net/if_gif.h>
     68 
     69 #include "gif.h"
     70 
     71 #include <net/net_osdep.h>
     72 
     73 static int gif_validate4(const struct ip *, struct gif_softc *,
     74 	struct ifnet *);
     75 
     76 #if NGIF > 0
     77 int ip_gif_ttl = GIF_TTL;
     78 #else
     79 int ip_gif_ttl = 0;
     80 #endif
     81 
     82 const struct protosw in_gif_protosw = {
     83 	.pr_type	= SOCK_RAW,
     84 	.pr_domain	= &inetdomain,
     85 	.pr_protocol	= 0 /* IPPROTO_IPV[46] */,
     86 	.pr_flags	= PR_ATOMIC|PR_ADDR,
     87 	.pr_input	= in_gif_input,
     88 	.pr_output	= rip_output,
     89 	.pr_ctlinput	= NULL,
     90 	.pr_ctloutput	= rip_ctloutput,
     91 	.pr_usrreqs	= &rip_usrreqs,
     92 };
     93 
     94 int
     95 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
     96 {
     97 	struct rtentry *rt;
     98 	struct gif_softc *sc = ifp->if_softc;
     99 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
    100 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
    101 	struct ip iphdr;	/* capsule IP header, host byte ordered */
    102 	int proto, error;
    103 	u_int8_t tos;
    104 	union {
    105 		struct sockaddr		dst;
    106 		struct sockaddr_in	dst4;
    107 	} u;
    108 
    109 	if (sin_src == NULL || sin_dst == NULL ||
    110 	    sin_src->sin_family != AF_INET ||
    111 	    sin_dst->sin_family != AF_INET) {
    112 		m_freem(m);
    113 		return EAFNOSUPPORT;
    114 	}
    115 
    116 	switch (family) {
    117 #ifdef INET
    118 	case AF_INET:
    119 	    {
    120 		const struct ip *ip;
    121 
    122 		proto = IPPROTO_IPV4;
    123 		if (m->m_len < sizeof(*ip)) {
    124 			m = m_pullup(m, sizeof(*ip));
    125 			if (m == NULL)
    126 				return ENOBUFS;
    127 		}
    128 		ip = mtod(m, const struct ip *);
    129 		tos = ip->ip_tos;
    130 		break;
    131 	    }
    132 #endif /* INET */
    133 #ifdef INET6
    134 	case AF_INET6:
    135 	    {
    136 		const struct ip6_hdr *ip6;
    137 		proto = IPPROTO_IPV6;
    138 		if (m->m_len < sizeof(*ip6)) {
    139 			m = m_pullup(m, sizeof(*ip6));
    140 			if (m == NULL)
    141 				return ENOBUFS;
    142 		}
    143 		ip6 = mtod(m, const struct ip6_hdr *);
    144 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    145 		break;
    146 	    }
    147 #endif /* INET6 */
    148 	default:
    149 #ifdef DEBUG
    150 		printf("in_gif_output: warning: unknown family %d passed\n",
    151 			family);
    152 #endif
    153 		m_freem(m);
    154 		return EAFNOSUPPORT;
    155 	}
    156 
    157 	memset(&iphdr, 0, sizeof(iphdr));
    158 	iphdr.ip_src = sin_src->sin_addr;
    159 	/* bidirectional configured tunnel mode */
    160 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    161 		iphdr.ip_dst = sin_dst->sin_addr;
    162 	else {
    163 		m_freem(m);
    164 		return ENETUNREACH;
    165 	}
    166 	iphdr.ip_p = proto;
    167 	/* version will be set in ip_output() */
    168 	iphdr.ip_ttl = ip_gif_ttl;
    169 	iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
    170 	if (ifp->if_flags & IFF_LINK1)
    171 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
    172 	else
    173 		ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
    174 
    175 	/* prepend new IP header */
    176 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    177 	/* XXX Is m_pullup really necessary after M_PREPEND? */
    178 	if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
    179 		m = m_pullup(m, sizeof(struct ip));
    180 	if (m == NULL)
    181 		return ENOBUFS;
    182 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
    183 
    184 	sockaddr_in_init(&u.dst4, &sin_dst->sin_addr, 0);
    185 	if ((rt = rtcache_lookup(&sc->gif_ro, &u.dst)) == NULL) {
    186 		m_freem(m);
    187 		return ENETUNREACH;
    188 	}
    189 
    190 	/* If the route constitutes infinite encapsulation, punt. */
    191 	if (rt->rt_ifp == ifp) {
    192 		rtcache_free(&sc->gif_ro);
    193 		m_freem(m);
    194 		return ENETUNREACH;	/*XXX*/
    195 	}
    196 
    197 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
    198 	return (error);
    199 }
    200 
    201 void
    202 in_gif_input(struct mbuf *m, ...)
    203 {
    204 	int off, proto;
    205 	struct ifnet *gifp = NULL;
    206 	const struct ip *ip;
    207 	va_list ap;
    208 	int af;
    209 	u_int8_t otos;
    210 
    211 	va_start(ap, m);
    212 	off = va_arg(ap, int);
    213 	proto = va_arg(ap, int);
    214 	va_end(ap);
    215 
    216 	ip = mtod(m, const struct ip *);
    217 
    218 	gifp = (struct ifnet *)encap_getarg(m);
    219 
    220 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
    221 		m_freem(m);
    222 		ip_statinc(IP_STAT_NOGIF);
    223 		return;
    224 	}
    225 #ifndef GIF_ENCAPCHECK
    226 	if (!gif_validate4(ip, gifp->if_softc, m->m_pkthdr.rcvif)) {
    227 		m_freem(m);
    228 		ip_statinc(IP_STAT_NOGIF);
    229 		return;
    230 	}
    231 #endif
    232 
    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 
    293 	src = (struct sockaddr_in *)sc->gif_psrc;
    294 	dst = (struct sockaddr_in *)sc->gif_pdst;
    295 
    296 	/* check for address match */
    297 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
    298 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
    299 		return 0;
    300 
    301 	/* martian filters on outer source - NOT done in ip_input! */
    302 	if (IN_MULTICAST(ip->ip_src.s_addr))
    303 		return 0;
    304 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
    305 	case 0: case 127: case 255:
    306 		return 0;
    307 	}
    308 	/* reject packets with broadcast on source */
    309 	TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
    310 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
    311 			continue;
    312 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
    313 			return 0;
    314 	}
    315 
    316 	/* ingress filters on outer source */
    317 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
    318 		union {
    319 			struct sockaddr sa;
    320 			struct sockaddr_in sin;
    321 		} u;
    322 		struct rtentry *rt;
    323 
    324 		sockaddr_in_init(&u.sin, &ip->ip_src, 0);
    325 		rt = rtalloc1(&u.sa, 0);
    326 		if (rt == NULL || rt->rt_ifp != ifp) {
    327 #if 0
    328 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
    329 			    "due to ingress filter\n", if_name(&sc->gif_if),
    330 			    (u_int32_t)ntohl(u.sin.sin_addr.s_addr));
    331 #endif
    332 			if (rt != NULL)
    333 				rtfree(rt);
    334 			return 0;
    335 		}
    336 		rtfree(rt);
    337 	}
    338 
    339 	return 32 * 2;
    340 }
    341 
    342 #ifdef GIF_ENCAPCHECK
    343 /*
    344  * we know that we are in IFF_UP, outer address available, and outer family
    345  * matched the physical addr family.  see gif_encapcheck().
    346  */
    347 int
    348 gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
    349 {
    350 	struct ip ip;
    351 	struct gif_softc *sc;
    352 	struct ifnet *ifp;
    353 
    354 	/* sanity check done in caller */
    355 	sc = arg;
    356 
    357 	m_copydata(m, 0, sizeof(ip), &ip);
    358 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
    359 
    360 	return gif_validate4(&ip, sc, ifp);
    361 }
    362 #endif
    363 
    364 int
    365 in_gif_attach(struct gif_softc *sc)
    366 {
    367 #ifndef GIF_ENCAPCHECK
    368 	struct sockaddr_in mask4;
    369 
    370 	memset(&mask4, 0, sizeof(mask4));
    371 	mask4.sin_len = sizeof(struct sockaddr_in);
    372 	mask4.sin_addr.s_addr = ~0;
    373 
    374 	if (!sc->gif_psrc || !sc->gif_pdst)
    375 		return EINVAL;
    376 	sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
    377 	    (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
    378 	    (const struct protosw *)&in_gif_protosw, sc);
    379 #else
    380 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
    381 	    &in_gif_protosw, sc);
    382 #endif
    383 	if (sc->encap_cookie4 == NULL)
    384 		return EEXIST;
    385 	return 0;
    386 }
    387 
    388 int
    389 in_gif_detach(struct gif_softc *sc)
    390 {
    391 	int error;
    392 
    393 	error = encap_detach(sc->encap_cookie4);
    394 	if (error == 0)
    395 		sc->encap_cookie4 = NULL;
    396 
    397 	rtcache_free(&sc->gif_ro);
    398 
    399 	return error;
    400 }
    401