Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.49
      1 /*	$NetBSD: in_gif.c,v 1.49 2006/07/28 17:04:27 dyoung 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.49 2006/07/28 17:04:27 dyoung Exp $");
     35 
     36 #include "opt_inet.h"
     37 #include "opt_iso.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/socket.h>
     42 #include <sys/sockio.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/syslog.h>
     47 #include <sys/protosw.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 #include "bridge.h"
     70 #include <net/if_ether.h>
     71 
     72 #include <machine/stdarg.h>
     73 
     74 #include <net/net_osdep.h>
     75 
     76 static int gif_validate4(const struct ip *, struct gif_softc *,
     77 	struct ifnet *);
     78 
     79 #if NGIF > 0
     80 int ip_gif_ttl = GIF_TTL;
     81 #else
     82 int ip_gif_ttl = 0;
     83 #endif
     84 
     85 const struct protosw in_gif_protosw =
     86 { SOCK_RAW,	&inetdomain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
     87   in_gif_input, rip_output,	0,		rip_ctloutput,
     88   rip_usrreq,
     89   0,            0,              0,              0,
     90 };
     91 
     92 int
     93 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
     94 {
     95 	struct gif_softc *sc = (struct gif_softc*)ifp;
     96 	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
     97 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
     98 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
     99 	struct ip iphdr;	/* capsule IP header, host byte ordered */
    100 #if NBRIDGE > 0
    101 	struct etherip_header eiphdr;
    102 #endif
    103 	int proto, error;
    104 	u_int8_t tos;
    105 
    106 	if (sin_src == NULL || sin_dst == NULL ||
    107 	    sin_src->sin_family != AF_INET ||
    108 	    sin_dst->sin_family != AF_INET) {
    109 		m_freem(m);
    110 		return EAFNOSUPPORT;
    111 	}
    112 
    113 	switch (family) {
    114 #ifdef INET
    115 	case AF_INET:
    116 	    {
    117 		const struct ip *ip;
    118 
    119 		proto = IPPROTO_IPV4;
    120 		if (m->m_len < sizeof(*ip)) {
    121 			m = m_pullup(m, sizeof(*ip));
    122 			if (m == NULL)
    123 				return ENOBUFS;
    124 		}
    125 		ip = mtod(m, const struct ip *);
    126 		tos = ip->ip_tos;
    127 		break;
    128 	    }
    129 #endif /* INET */
    130 #ifdef INET6
    131 	case AF_INET6:
    132 	    {
    133 		const struct ip6_hdr *ip6;
    134 		proto = IPPROTO_IPV6;
    135 		if (m->m_len < sizeof(*ip6)) {
    136 			m = m_pullup(m, sizeof(*ip6));
    137 			if (!m)
    138 				return ENOBUFS;
    139 		}
    140 		ip6 = mtod(m, const struct ip6_hdr *);
    141 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    142 		break;
    143 	    }
    144 #endif /* INET6 */
    145 #ifdef ISO
    146 	case AF_ISO:
    147 		proto = IPPROTO_EON;
    148 		tos = 0;
    149 		break;
    150 #endif
    151 #if NBRIDGE > 0
    152 	case AF_LINK:
    153 		proto = IPPROTO_ETHERIP;
    154 		eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
    155 		eiphdr.eip_pad = 0;
    156 		/* prepend Ethernet-in-IP header */
    157 		M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
    158 		if (m == NULL)
    159 			return ENOBUFS;
    160 		if (m->m_len < sizeof(struct etherip_header) || M_READONLY(m)) {
    161 			m = m_pullup(m, sizeof(struct etherip_header));
    162 			if (m == NULL)
    163 				return ENOBUFS;
    164 		}
    165 		bcopy(&eiphdr, mtod(m, struct etherip_header *),
    166 		    sizeof(struct etherip_header));
    167 		break;
    168 #endif
    169 	default:
    170 #ifdef DEBUG
    171 		printf("in_gif_output: warning: unknown family %d passed\n",
    172 			family);
    173 #endif
    174 		m_freem(m);
    175 		return EAFNOSUPPORT;
    176 	}
    177 
    178 	bzero(&iphdr, sizeof(iphdr));
    179 	iphdr.ip_src = sin_src->sin_addr;
    180 	/* bidirectional configured tunnel mode */
    181 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    182 		iphdr.ip_dst = sin_dst->sin_addr;
    183 	else {
    184 		m_freem(m);
    185 		return ENETUNREACH;
    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 = htons(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 	else
    194 		ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
    195 
    196 	/* prepend new IP header */
    197 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    198 	/* XXX Is m_pullup really necessary after M_PREPEND? */
    199 	if (m != NULL && (m->m_len < sizeof(struct ip) || M_READONLY(m)))
    200 		m = m_pullup(m, sizeof(struct ip));
    201 	if (m == NULL)
    202 		return ENOBUFS;
    203 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
    204 
    205 	if (sc->gif_route_expire - time_second <= 0 ||
    206 	    dst->sin_family != sin_dst->sin_family ||
    207 	    !in_hosteq(dst->sin_addr, sin_dst->sin_addr)) {
    208 		/* cache route doesn't match */
    209 		bzero(dst, sizeof(*dst));
    210 		dst->sin_family = sin_dst->sin_family;
    211 		dst->sin_len = sizeof(struct sockaddr_in);
    212 		dst->sin_addr = sin_dst->sin_addr;
    213 		if (sc->gif_ro.ro_rt) {
    214 			RTFREE(sc->gif_ro.ro_rt);
    215 			sc->gif_ro.ro_rt = NULL;
    216 		}
    217 	}
    218 
    219 	if (sc->gif_ro.ro_rt == NULL) {
    220 		rtalloc(&sc->gif_ro);
    221 		if (sc->gif_ro.ro_rt == NULL) {
    222 			m_freem(m);
    223 			return ENETUNREACH;
    224 		}
    225 
    226 		/* if it constitutes infinite encapsulation, punt. */
    227 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
    228 			m_freem(m);
    229 			return ENETUNREACH;	/*XXX*/
    230 		}
    231 
    232 		sc->gif_route_expire = time_second + GIF_ROUTE_TTL;
    233 	}
    234 
    235 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
    236 	return (error);
    237 }
    238 
    239 void
    240 in_gif_input(struct mbuf *m, ...)
    241 {
    242 	int off, proto;
    243 	struct ifnet *gifp = NULL;
    244 	const struct ip *ip;
    245 	va_list ap;
    246 	int af;
    247 	u_int8_t otos;
    248 
    249 	va_start(ap, m);
    250 	off = va_arg(ap, int);
    251 	proto = va_arg(ap, int);
    252 	va_end(ap);
    253 
    254 	ip = mtod(m, const struct ip *);
    255 
    256 	gifp = (struct ifnet *)encap_getarg(m);
    257 
    258 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
    259 		m_freem(m);
    260 		ipstat.ips_nogif++;
    261 		return;
    262 	}
    263 #ifndef GIF_ENCAPCHECK
    264 	if (!gif_validate4(ip, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
    265 		m_freem(m);
    266 		ipstat.ips_nogif++;
    267 		return;
    268 	}
    269 #endif
    270 
    271 	otos = ip->ip_tos;
    272 	m_adj(m, off);
    273 
    274 	switch (proto) {
    275 #ifdef INET
    276 	case IPPROTO_IPV4:
    277 	    {
    278 		struct ip *xip;
    279 		af = AF_INET;
    280 		if (m->m_len < sizeof(*xip) || M_READONLY(m)) {
    281 			if ((m = m_pullup(m, sizeof(*xip))) == NULL)
    282 				return;
    283 		}
    284 		xip = mtod(m, struct ip *);
    285 		if (gifp->if_flags & IFF_LINK1)
    286 			ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
    287 		else
    288 			ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
    289 		break;
    290 	    }
    291 #endif
    292 #ifdef INET6
    293 	case IPPROTO_IPV6:
    294 	    {
    295 		struct ip6_hdr *ip6;
    296 		u_int8_t itos;
    297 		af = AF_INET6;
    298 		if (m->m_len < sizeof(*ip6) || M_READONLY(m)) {
    299 			if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
    300 				return;
    301 		}
    302 		ip6 = mtod(m, struct ip6_hdr *);
    303 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    304 		if (gifp->if_flags & IFF_LINK1)
    305 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    306 		else
    307 			ip_ecn_egress(ECN_NOCARE, &otos, &itos);
    308 		ip6->ip6_flow &= ~htonl(0xff << 20);
    309 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    310 		break;
    311 	    }
    312 #endif /* INET6 */
    313 #ifdef ISO
    314 	case IPPROTO_EON:
    315 		af = AF_ISO;
    316 		break;
    317 #endif
    318 #if NBRIDGE > 0
    319 	case IPPROTO_ETHERIP:
    320 		af = AF_LINK;
    321 		break;
    322 #endif
    323 	default:
    324 		ipstat.ips_nogif++;
    325 		m_freem(m);
    326 		return;
    327 	}
    328 	gif_input(m, af, gifp);
    329 	return;
    330 }
    331 
    332 /*
    333  * validate outer address.
    334  */
    335 static int
    336 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
    337 {
    338 	struct sockaddr_in *src, *dst;
    339 	struct in_ifaddr *ia4;
    340 
    341 	src = (struct sockaddr_in *)sc->gif_psrc;
    342 	dst = (struct sockaddr_in *)sc->gif_pdst;
    343 
    344 	/* check for address match */
    345 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
    346 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
    347 		return 0;
    348 
    349 	/* martian filters on outer source - NOT done in ip_input! */
    350 	if (IN_MULTICAST(ip->ip_src.s_addr))
    351 		return 0;
    352 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
    353 	case 0: case 127: case 255:
    354 		return 0;
    355 	}
    356 	/* reject packets with broadcast on source */
    357 	TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
    358 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
    359 			continue;
    360 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
    361 			return 0;
    362 	}
    363 
    364 	/* ingress filters on outer source */
    365 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
    366 		struct sockaddr_in sin;
    367 		struct rtentry *rt;
    368 
    369 		bzero(&sin, sizeof(sin));
    370 		sin.sin_family = AF_INET;
    371 		sin.sin_len = sizeof(struct sockaddr_in);
    372 		sin.sin_addr = ip->ip_src;
    373 		rt = rtalloc1((struct sockaddr *)&sin, 0);
    374 		if (!rt || rt->rt_ifp != ifp) {
    375 #if 0
    376 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
    377 			    "due to ingress filter\n", if_name(&sc->gif_if),
    378 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
    379 #endif
    380 			if (rt)
    381 				rtfree(rt);
    382 			return 0;
    383 		}
    384 		rtfree(rt);
    385 	}
    386 
    387 	return 32 * 2;
    388 }
    389 
    390 #ifdef GIF_ENCAPCHECK
    391 /*
    392  * we know that we are in IFF_UP, outer address available, and outer family
    393  * matched the physical addr family.  see gif_encapcheck().
    394  */
    395 int
    396 gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
    397 {
    398 	struct ip ip;
    399 	struct gif_softc *sc;
    400 	struct ifnet *ifp;
    401 
    402 	/* sanity check done in caller */
    403 	sc = (struct gif_softc *)arg;
    404 
    405 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
    406 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
    407 
    408 	return gif_validate4(&ip, sc, ifp);
    409 }
    410 #endif
    411 
    412 int
    413 in_gif_attach(struct gif_softc *sc)
    414 {
    415 #ifndef GIF_ENCAPCHECK
    416 	struct sockaddr_in mask4;
    417 
    418 	bzero(&mask4, sizeof(mask4));
    419 	mask4.sin_len = sizeof(struct sockaddr_in);
    420 	mask4.sin_addr.s_addr = ~0;
    421 
    422 	if (!sc->gif_psrc || !sc->gif_pdst)
    423 		return EINVAL;
    424 	sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
    425 	    (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
    426 	    (const struct protosw *)&in_gif_protosw, sc);
    427 #else
    428 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
    429 	    &in_gif_protosw, sc);
    430 #endif
    431 	if (sc->encap_cookie4 == NULL)
    432 		return EEXIST;
    433 	return 0;
    434 }
    435 
    436 int
    437 in_gif_detach(struct gif_softc *sc)
    438 {
    439 	int error;
    440 
    441 	error = encap_detach(sc->encap_cookie4);
    442 	if (error == 0)
    443 		sc->encap_cookie4 = NULL;
    444 
    445 	if (sc->gif_ro.ro_rt) {
    446 		RTFREE(sc->gif_ro.ro_rt);
    447 		sc->gif_ro.ro_rt = NULL;
    448 	}
    449 
    450 	return error;
    451 }
    452