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