Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.5
      1 /*	$NetBSD: in_gif.c,v 1.5 1999/07/09 22:57:17 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * in_gif.c
     34  */
     35 
     36 #ifdef __FreeBSD__
     37 #include "opt_mrouting.h"
     38 #endif
     39 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
     40 #include "opt_inet.h"
     41 #include "opt_ipsec.h"
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/socket.h>
     47 #include <sys/sockio.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/errno.h>
     50 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     51 #include <sys/ioctl.h>
     52 #endif
     53 #include <sys/protosw.h>
     54 
     55 #include <net/if.h>
     56 #include <net/route.h>
     57 #include <net/if_gif.h>
     58 
     59 #include <netinet/in.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/ip.h>
     62 #include <netinet/ip_var.h>
     63 #include <netinet/in_gif.h>
     64 #include <netinet/ip_ecn.h>
     65 
     66 #ifdef INET6
     67 #include <netinet/ip6.h>
     68 #endif
     69 
     70 #ifdef MROUTING
     71 #include <netinet/ip_mroute.h>
     72 #endif /* MROUTING */
     73 
     74 #include <net/if_gif.h>
     75 
     76 #include "gif.h"
     77 
     78 #ifdef __NetBSD__
     79 #include <machine/stdarg.h>
     80 #endif
     81 
     82 #if NGIF > 0
     83 int ip_gif_ttl = GIF_TTL;
     84 #else
     85 int ip_gif_ttl = 0;
     86 #endif
     87 
     88 int
     89 in_gif_output(ifp, family, m, rt)
     90 	struct ifnet	*ifp;
     91 	int		family;
     92 	struct mbuf	*m;
     93 	struct rtentry *rt;
     94 {
     95 	register struct gif_softc *sc = (struct gif_softc*)ifp;
     96 	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
     97 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
     98 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
     99 	struct ip iphdr;	/* capsule IP header, host byte ordered */
    100 	int proto, error;
    101 	u_int8_t tos;
    102 
    103 	if (sin_src == NULL || sin_dst == NULL ||
    104 	    sin_src->sin_family != AF_INET ||
    105 	    sin_dst->sin_family != AF_INET) {
    106 		m_freem(m);
    107 		return EAFNOSUPPORT;
    108 	}
    109 
    110 	switch (family) {
    111 #ifdef INET
    112 	case AF_INET:
    113 	    {
    114 		struct ip *ip;
    115 
    116 		proto = IPPROTO_IPV4;
    117 		if (m->m_len < sizeof(*ip)) {
    118 			m = m_pullup(m, sizeof(*ip));
    119 			if (!m)
    120 				return ENOBUFS;
    121 		}
    122 		ip = mtod(m, struct ip *);
    123 		tos = ip->ip_tos;
    124 		break;
    125 	    }
    126 #endif /*INET*/
    127 #ifdef INET6
    128 	case AF_INET6:
    129 	    {
    130 		struct ip6_hdr *ip6;
    131 		proto = IPPROTO_IPV6;
    132 		if (m->m_len < sizeof(*ip6)) {
    133 			m = m_pullup(m, sizeof(*ip6));
    134 			if (!m)
    135 				return ENOBUFS;
    136 		}
    137 		ip6 = mtod(m, struct ip6_hdr *);
    138 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    139 		break;
    140 	    }
    141 #endif /*INET6*/
    142 	default:
    143 #ifdef DIAGNOSTIC
    144 		printf("in_gif_output: warning: unknown family %d passed\n",
    145 			family);
    146 #endif
    147 		m_freem(m);
    148 		return EAFNOSUPPORT;
    149 	}
    150 
    151 	bzero(&iphdr, sizeof(iphdr));
    152 	iphdr.ip_src = sin_src->sin_addr;
    153 	if (ifp->if_flags & IFF_LINK0) {
    154 		/* multi-destination mode */
    155 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    156 			iphdr.ip_dst = sin_dst->sin_addr;
    157 		else if (rt) {
    158 			iphdr.ip_dst = ((struct sockaddr_in *)
    159 					(rt->rt_gateway))->sin_addr;
    160 		} else {
    161 			m_freem(m);
    162 			return ENETUNREACH;
    163 		}
    164 	} else {
    165 		/* bidirectional configured tunnel mode */
    166 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    167 			iphdr.ip_dst = sin_dst->sin_addr;
    168 		else {
    169 			m_freem(m);
    170 			return ENETUNREACH;
    171 		}
    172 	}
    173 	iphdr.ip_p = proto;
    174 	/* version will be set in ip_output() */
    175 	iphdr.ip_ttl = ip_gif_ttl;
    176 	iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
    177 	if (ifp->if_flags & IFF_LINK1)
    178 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
    179 
    180 	/* prepend new IP header */
    181 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    182 	if (m && m->m_len < sizeof(struct ip))
    183 		m = m_pullup(m, sizeof(struct ip));
    184 	if (m == NULL) {
    185 		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
    186 		return ENOBUFS;
    187 	}
    188 
    189 	*(mtod(m, struct ip *)) = iphdr;
    190 
    191 	if (dst->sin_family != sin_dst->sin_family ||
    192 	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
    193 		/* cache route doesn't match */
    194 		dst->sin_family = sin_dst->sin_family;
    195 		dst->sin_len = sizeof(struct sockaddr_in);
    196 		dst->sin_addr = sin_dst->sin_addr;
    197 		if (sc->gif_ro.ro_rt) {
    198 			RTFREE(sc->gif_ro.ro_rt);
    199 			sc->gif_ro.ro_rt = NULL;
    200 		}
    201 #if 0
    202 		sc->gif_if.if_mtu = GIF_MTU;
    203 #endif
    204 	}
    205 
    206 	if (sc->gif_ro.ro_rt == NULL) {
    207 		rtalloc(&sc->gif_ro);
    208 		if (sc->gif_ro.ro_rt == NULL) {
    209 			m_freem(m);
    210 			return ENETUNREACH;
    211 		}
    212 #if 0
    213 		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
    214 			- sizeof(struct ip);
    215 #endif
    216 	}
    217 
    218 #ifdef IPSEC
    219 	m->m_pkthdr.rcvif = NULL;
    220 #endif /*IPSEC*/
    221 	error = ip_output(m, 0, &sc->gif_ro, 0, 0);
    222 	return(error);
    223 }
    224 
    225 void
    226 #ifndef __NetBSD__
    227 in_gif_input(m, off, proto)
    228 	struct mbuf *m;
    229 	int off;
    230 	int proto;
    231 {
    232 #else /* __NetBSD__ */
    233 #if __STDC__
    234 in_gif_input(struct mbuf *m, ...)
    235 #else
    236 in_gif_input(m, va_alist)
    237 	struct mbuf *m;
    238 	va_dcl
    239 #endif
    240 {
    241 	int off, proto;
    242 #endif /* __NetBSD__ */
    243 	struct gif_softc *sc;
    244 	struct ifnet *gifp = NULL;
    245 	struct ip *ip;
    246 	int i, af;
    247 #ifdef __NetBSD__
    248 	va_list ap;
    249 #endif /* __NetBSD__ */
    250 	u_int8_t otos;
    251 
    252 #ifdef __NetBSD__
    253 	va_start(ap, m);
    254 	off = va_arg(ap, int);
    255 	proto = va_arg(ap, int);
    256 	va_end(ap);
    257 #endif /* __NetBSD__ */
    258 
    259 	ip = mtod(m, struct ip *);
    260 
    261 	/* this code will be soon improved. */
    262 #define	satosin(sa)	((struct sockaddr_in *)(sa))
    263 	for (i = 0, sc = gif; i < ngif; i++, sc++) {
    264 		if (sc->gif_psrc == NULL
    265 		 || sc->gif_pdst == NULL
    266 		 || sc->gif_psrc->sa_family != AF_INET
    267 		 || sc->gif_pdst->sa_family != AF_INET) {
    268 			continue;
    269 		}
    270 
    271 		if ((sc->gif_if.if_flags & IFF_LINK0)
    272 		 && satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    273 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == INADDR_ANY) {
    274 			gifp = &sc->gif_if;
    275 			continue;
    276 		}
    277 
    278 		if (satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    279 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == ip->ip_src.s_addr)
    280 		{
    281 			gifp = &sc->gif_if;
    282 			break;
    283 		}
    284 	}
    285 
    286 	if (gifp == NULL) {
    287 #ifdef MROUTING
    288 		/* for backward compatibility */
    289 		if (proto == IPPROTO_IPV4) {
    290 			ipip_input(m, off, proto);
    291 			return;
    292 		}
    293 #endif /*MROUTING*/
    294 		m_freem(m);
    295 		ipstat.ips_nogif++;
    296 		return;
    297 	}
    298 
    299 	otos = ip->ip_tos;
    300 	m_adj(m, off);
    301 
    302 	switch (proto) {
    303 #ifdef INET
    304 	case IPPROTO_IPV4:
    305 	    {
    306 		struct ip *ip;
    307 		af = AF_INET;
    308 		if (m->m_len < sizeof(*ip)) {
    309 			m = m_pullup(m, sizeof(*ip));
    310 			if (!m)
    311 				return;
    312 		}
    313 		ip = mtod(m, struct ip *);
    314 		if (gifp->if_flags & IFF_LINK1)
    315 			ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
    316 		break;
    317 	    }
    318 #endif
    319 #ifdef INET6
    320 	case IPPROTO_IPV6:
    321 	    {
    322 		struct ip6_hdr *ip6;
    323 		u_int8_t itos;
    324 		af = AF_INET6;
    325 		if (m->m_len < sizeof(*ip6)) {
    326 			m = m_pullup(m, sizeof(*ip6));
    327 			if (!m)
    328 				return;
    329 		}
    330 		ip6 = mtod(m, struct ip6_hdr *);
    331 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    332 		if (gifp->if_flags & IFF_LINK1)
    333 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    334 		ip6->ip6_flow &= ~htonl(0xff << 20);
    335 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    336 		break;
    337 	    }
    338 #endif /* INET6 */
    339 	default:
    340 		ipstat.ips_nogif++;
    341 		m_freem(m);
    342 		return;
    343 	}
    344 	gif_input(m, af, gifp);
    345 	return;
    346 }
    347