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