Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.60
      1 /*	$NetBSD: in_gif.c,v 1.60 2008/11/07 00:20:18 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.60 2008/11/07 00:20:18 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 
     70 #include <machine/stdarg.h>
     71 
     72 #include <net/net_osdep.h>
     73 
     74 static int gif_validate4(const struct ip *, struct gif_softc *,
     75 	struct ifnet *);
     76 
     77 #if NGIF > 0
     78 int ip_gif_ttl = GIF_TTL;
     79 #else
     80 int ip_gif_ttl = 0;
     81 #endif
     82 
     83 const struct protosw in_gif_protosw =
     84 { SOCK_RAW,	&inetdomain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
     85   in_gif_input, rip_output,	0,		rip_ctloutput,
     86   rip_usrreq,
     87   0,            0,              0,              0,
     88 };
     89 
     90 int
     91 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
     92 {
     93 	struct rtentry *rt;
     94 	struct gif_softc *sc = ifp->if_softc;
     95 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
     96 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
     97 	struct ip iphdr;	/* capsule IP header, host byte ordered */
     98 	int proto, error;
     99 	u_int8_t tos;
    100 	union {
    101 		struct sockaddr		dst;
    102 		struct sockaddr_in	dst4;
    103 	} u;
    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 		const 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 == NULL)
    122 				return ENOBUFS;
    123 		}
    124 		ip = mtod(m, const struct ip *);
    125 		tos = ip->ip_tos;
    126 		break;
    127 	    }
    128 #endif /* INET */
    129 #ifdef INET6
    130 	case AF_INET6:
    131 	    {
    132 		const 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, const 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 	default:
    151 #ifdef DEBUG
    152 		printf("in_gif_output: warning: unknown family %d passed\n",
    153 			family);
    154 #endif
    155 		m_freem(m);
    156 		return EAFNOSUPPORT;
    157 	}
    158 
    159 	memset(&iphdr, 0, sizeof(iphdr));
    160 	iphdr.ip_src = sin_src->sin_addr;
    161 	/* bidirectional configured tunnel mode */
    162 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    163 		iphdr.ip_dst = sin_dst->sin_addr;
    164 	else {
    165 		m_freem(m);
    166 		return ENETUNREACH;
    167 	}
    168 	iphdr.ip_p = proto;
    169 	/* version will be set in ip_output() */
    170 	iphdr.ip_ttl = ip_gif_ttl;
    171 	iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
    172 	if (ifp->if_flags & IFF_LINK1)
    173 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
    174 	else
    175 		ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
    176 
    177 	/* prepend new IP header */
    178 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    179 	/* XXX Is m_pullup really necessary after M_PREPEND? */
    180 	if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
    181 		m = m_pullup(m, sizeof(struct ip));
    182 	if (m == NULL)
    183 		return ENOBUFS;
    184 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
    185 
    186 	sockaddr_in_init(&u.dst4, &sin_dst->sin_addr, 0);
    187 	if ((rt = rtcache_lookup(&sc->gif_ro, &u.dst)) == NULL) {
    188 		m_freem(m);
    189 		return ENETUNREACH;
    190 	}
    191 
    192 	/* If the route constitutes infinite encapsulation, punt. */
    193 	if (rt->rt_ifp == ifp) {
    194 		rtcache_free(&sc->gif_ro);
    195 		m_freem(m);
    196 		return ENETUNREACH;	/*XXX*/
    197 	}
    198 
    199 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
    200 	return (error);
    201 }
    202 
    203 void
    204 in_gif_input(struct mbuf *m, ...)
    205 {
    206 	int off, proto;
    207 	struct ifnet *gifp = NULL;
    208 	const struct ip *ip;
    209 	va_list ap;
    210 	int af;
    211 	u_int8_t otos;
    212 
    213 	va_start(ap, m);
    214 	off = va_arg(ap, int);
    215 	proto = va_arg(ap, int);
    216 	va_end(ap);
    217 
    218 	ip = mtod(m, const struct ip *);
    219 
    220 	gifp = (struct ifnet *)encap_getarg(m);
    221 
    222 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
    223 		m_freem(m);
    224 		ip_statinc(IP_STAT_NOGIF);
    225 		return;
    226 	}
    227 #ifndef GIF_ENCAPCHECK
    228 	if (!gif_validate4(ip, gifp->if_softc, m->m_pkthdr.rcvif)) {
    229 		m_freem(m);
    230 		ip_statinc(IP_STAT_NOGIF);
    231 		return;
    232 	}
    233 #endif
    234 
    235 	otos = ip->ip_tos;
    236 	m_adj(m, off);
    237 
    238 	switch (proto) {
    239 #ifdef INET
    240 	case IPPROTO_IPV4:
    241 	    {
    242 		struct ip *xip;
    243 		af = AF_INET;
    244 		if (M_UNWRITABLE(m, sizeof(*xip))) {
    245 			if ((m = m_pullup(m, sizeof(*xip))) == NULL)
    246 				return;
    247 		}
    248 		xip = mtod(m, struct ip *);
    249 		if (gifp->if_flags & IFF_LINK1)
    250 			ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
    251 		else
    252 			ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
    253 		break;
    254 	    }
    255 #endif
    256 #ifdef INET6
    257 	case IPPROTO_IPV6:
    258 	    {
    259 		struct ip6_hdr *ip6;
    260 		u_int8_t itos;
    261 		af = AF_INET6;
    262 		if (M_UNWRITABLE(m, sizeof(*ip6))) {
    263 			if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
    264 				return;
    265 		}
    266 		ip6 = mtod(m, struct ip6_hdr *);
    267 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    268 		if (gifp->if_flags & IFF_LINK1)
    269 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    270 		else
    271 			ip_ecn_egress(ECN_NOCARE, &otos, &itos);
    272 		ip6->ip6_flow &= ~htonl(0xff << 20);
    273 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    274 		break;
    275 	    }
    276 #endif /* INET6 */
    277 #ifdef ISO
    278 	case IPPROTO_EON:
    279 		af = AF_ISO;
    280 		break;
    281 #endif
    282 	default:
    283 		ip_statinc(IP_STAT_NOGIF);
    284 		m_freem(m);
    285 		return;
    286 	}
    287 	gif_input(m, af, gifp);
    288 	return;
    289 }
    290 
    291 /*
    292  * validate outer address.
    293  */
    294 static int
    295 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
    296 {
    297 	struct sockaddr_in *src, *dst;
    298 	struct in_ifaddr *ia4;
    299 
    300 	src = (struct sockaddr_in *)sc->gif_psrc;
    301 	dst = (struct sockaddr_in *)sc->gif_pdst;
    302 
    303 	/* check for address match */
    304 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
    305 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
    306 		return 0;
    307 
    308 	/* martian filters on outer source - NOT done in ip_input! */
    309 	if (IN_MULTICAST(ip->ip_src.s_addr))
    310 		return 0;
    311 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
    312 	case 0: case 127: case 255:
    313 		return 0;
    314 	}
    315 	/* reject packets with broadcast on source */
    316 	TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
    317 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
    318 			continue;
    319 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
    320 			return 0;
    321 	}
    322 
    323 	/* ingress filters on outer source */
    324 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
    325 		union {
    326 			struct sockaddr sa;
    327 			struct sockaddr_in sin;
    328 		} u;
    329 		struct rtentry *rt;
    330 
    331 		sockaddr_in_init(&u.sin, &ip->ip_src, 0);
    332 		rt = rtalloc1(&u.sa, 0);
    333 		if (rt == NULL || rt->rt_ifp != ifp) {
    334 #if 0
    335 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
    336 			    "due to ingress filter\n", if_name(&sc->gif_if),
    337 			    (u_int32_t)ntohl(u.sin.sin_addr.s_addr));
    338 #endif
    339 			if (rt != NULL)
    340 				rtfree(rt);
    341 			return 0;
    342 		}
    343 		rtfree(rt);
    344 	}
    345 
    346 	return 32 * 2;
    347 }
    348 
    349 #ifdef GIF_ENCAPCHECK
    350 /*
    351  * we know that we are in IFF_UP, outer address available, and outer family
    352  * matched the physical addr family.  see gif_encapcheck().
    353  */
    354 int
    355 gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
    356 {
    357 	struct ip ip;
    358 	struct gif_softc *sc;
    359 	struct ifnet *ifp;
    360 
    361 	/* sanity check done in caller */
    362 	sc = arg;
    363 
    364 	m_copydata(m, 0, sizeof(ip), &ip);
    365 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
    366 
    367 	return gif_validate4(&ip, sc, ifp);
    368 }
    369 #endif
    370 
    371 int
    372 in_gif_attach(struct gif_softc *sc)
    373 {
    374 #ifndef GIF_ENCAPCHECK
    375 	struct sockaddr_in mask4;
    376 
    377 	memset(&mask4, 0, sizeof(mask4));
    378 	mask4.sin_len = sizeof(struct sockaddr_in);
    379 	mask4.sin_addr.s_addr = ~0;
    380 
    381 	if (!sc->gif_psrc || !sc->gif_pdst)
    382 		return EINVAL;
    383 	sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
    384 	    (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
    385 	    (const struct protosw *)&in_gif_protosw, sc);
    386 #else
    387 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
    388 	    &in_gif_protosw, sc);
    389 #endif
    390 	if (sc->encap_cookie4 == NULL)
    391 		return EEXIST;
    392 	return 0;
    393 }
    394 
    395 int
    396 in_gif_detach(struct gif_softc *sc)
    397 {
    398 	int error;
    399 
    400 	error = encap_detach(sc->encap_cookie4);
    401 	if (error == 0)
    402 		sc->encap_cookie4 = NULL;
    403 
    404 	rtcache_free(&sc->gif_ro);
    405 
    406 	return error;
    407 }
    408