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