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