Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.4
      1 /*	$NetBSD: in_gif.c,v 1.4 1999/07/06 12:23:20 itojun 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 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/socket.h>
     46 #include <sys/sockio.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/errno.h>
     49 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     50 #include <sys/ioctl.h>
     51 #endif
     52 #include <sys/protosw.h>
     53 
     54 #include <net/if.h>
     55 #include <net/route.h>
     56 #include <net/if_gif.h>
     57 
     58 #include <netinet/in.h>
     59 #include <netinet/in_systm.h>
     60 #include <netinet/ip.h>
     61 #include <netinet/ip_var.h>
     62 #include <netinet/in_gif.h>
     63 #include <netinet/ip_ecn.h>
     64 
     65 #ifdef INET6
     66 #include <netinet/ip6.h>
     67 #endif
     68 
     69 #ifdef MROUTING
     70 #include <netinet/ip_mroute.h>
     71 #endif /* MROUTING */
     72 
     73 #include <net/if_gif.h>
     74 
     75 #include "gif.h"
     76 
     77 #ifdef __NetBSD__
     78 #include <machine/stdarg.h>
     79 #endif
     80 
     81 #if NGIF > 0
     82 int ip_gif_ttl = GIF_TTL;
     83 #else
     84 int ip_gif_ttl = 0;
     85 #endif
     86 
     87 int
     88 in_gif_output(ifp, family, m, rt)
     89 	struct ifnet	*ifp;
     90 	int		family;
     91 	struct mbuf	*m;
     92 	struct rtentry *rt;
     93 {
     94 	register 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 	int proto, error;
    100 	u_int8_t tos;
    101 
    102 	if (sin_src == NULL || sin_dst == NULL ||
    103 	    sin_src->sin_family != AF_INET ||
    104 	    sin_dst->sin_family != AF_INET) {
    105 		m_freem(m);
    106 		return EAFNOSUPPORT;
    107 	}
    108 
    109 	switch (family) {
    110 #ifdef INET
    111 	case AF_INET:
    112 	    {
    113 		struct ip *ip;
    114 
    115 		proto = IPPROTO_IPV4;
    116 		if (m->m_len < sizeof(*ip)) {
    117 			m = m_pullup(m, sizeof(*ip));
    118 			if (!m)
    119 				return ENOBUFS;
    120 		}
    121 		ip = mtod(m, struct ip *);
    122 		tos = ip->ip_tos;
    123 		break;
    124 	    }
    125 #endif /*INET*/
    126 #ifdef INET6
    127 	case AF_INET6:
    128 	    {
    129 		struct ip6_hdr *ip6;
    130 		proto = IPPROTO_IPV6;
    131 		if (m->m_len < sizeof(*ip6)) {
    132 			m = m_pullup(m, sizeof(*ip6));
    133 			if (!m)
    134 				return ENOBUFS;
    135 		}
    136 		ip6 = mtod(m, struct ip6_hdr *);
    137 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    138 		break;
    139 	    }
    140 #endif /*INET6*/
    141 	default:
    142 #ifdef DIAGNOSTIC
    143 		printf("in_gif_output: warning: unknown family %d passed\n",
    144 			family);
    145 #endif
    146 		m_freem(m);
    147 		return EAFNOSUPPORT;
    148 	}
    149 
    150 	bzero(&iphdr, sizeof(iphdr));
    151 	iphdr.ip_src = sin_src->sin_addr;
    152 	if (ifp->if_flags & IFF_LINK0) {
    153 		/* multi-destination mode */
    154 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    155 			iphdr.ip_dst = sin_dst->sin_addr;
    156 		else if (rt) {
    157 			iphdr.ip_dst = ((struct sockaddr_in *)
    158 					(rt->rt_gateway))->sin_addr;
    159 		} else {
    160 			m_freem(m);
    161 			return ENETUNREACH;
    162 		}
    163 	} else {
    164 		/* bidirectional configured tunnel mode */
    165 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    166 			iphdr.ip_dst = sin_dst->sin_addr;
    167 		else {
    168 			m_freem(m);
    169 			return ENETUNREACH;
    170 		}
    171 	}
    172 	iphdr.ip_p = proto;
    173 	/* version will be set in ip_output() */
    174 	iphdr.ip_ttl = ip_gif_ttl;
    175 	iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
    176 	if (ifp->if_flags & IFF_LINK1)
    177 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
    178 
    179 	/* prepend new IP header */
    180 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    181 	if (m && m->m_len < sizeof(struct ip))
    182 		m = m_pullup(m, sizeof(struct ip));
    183 	if (m == NULL) {
    184 		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
    185 		return ENOBUFS;
    186 	}
    187 
    188 	*(mtod(m, struct ip *)) = iphdr;
    189 
    190 	if (dst->sin_family != sin_dst->sin_family ||
    191 	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
    192 		/* cache route doesn't match */
    193 		dst->sin_family = sin_dst->sin_family;
    194 		dst->sin_len = sizeof(struct sockaddr_in);
    195 		dst->sin_addr = sin_dst->sin_addr;
    196 		if (sc->gif_ro.ro_rt) {
    197 			RTFREE(sc->gif_ro.ro_rt);
    198 			sc->gif_ro.ro_rt = NULL;
    199 		}
    200 #if 0
    201 		sc->gif_if.if_mtu = GIF_MTU;
    202 #endif
    203 	}
    204 
    205 	if (sc->gif_ro.ro_rt == NULL) {
    206 		rtalloc(&sc->gif_ro);
    207 		if (sc->gif_ro.ro_rt == NULL) {
    208 			m_freem(m);
    209 			return ENETUNREACH;
    210 		}
    211 #if 0
    212 		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
    213 			- sizeof(struct ip);
    214 #endif
    215 	}
    216 
    217 #ifdef IPSEC
    218 	m->m_pkthdr.rcvif = NULL;
    219 #endif /*IPSEC*/
    220 	error = ip_output(m, 0, &sc->gif_ro, 0, 0);
    221 	return(error);
    222 }
    223 
    224 void
    225 #ifndef __NetBSD__
    226 in_gif_input(m, off, proto)
    227 	struct mbuf *m;
    228 	int off;
    229 	int proto;
    230 {
    231 #else /* __NetBSD__ */
    232 #if __STDC__
    233 in_gif_input(struct mbuf *m, ...)
    234 #else
    235 in_gif_input(m, va_alist)
    236 	struct mbuf *m;
    237 	va_dcl
    238 #endif
    239 {
    240 	int off, proto;
    241 #endif /* __NetBSD__ */
    242 	struct gif_softc *sc;
    243 	struct ifnet *gifp = NULL;
    244 	struct ip *ip;
    245 	int i, af;
    246 #ifdef __NetBSD__
    247 	va_list ap;
    248 #endif /* __NetBSD__ */
    249 	u_int8_t otos;
    250 
    251 #ifdef __NetBSD__
    252 	va_start(ap, m);
    253 	off = va_arg(ap, int);
    254 	proto = va_arg(ap, int);
    255 	va_end(ap);
    256 #endif /* __NetBSD__ */
    257 
    258 	ip = mtod(m, struct ip *);
    259 
    260 	/* this code will be soon improved. */
    261 #define	satosin(sa)	((struct sockaddr_in *)(sa))
    262 	for (i = 0, sc = gif; i < ngif; i++, sc++) {
    263 		if (sc->gif_psrc == NULL
    264 		 || sc->gif_pdst == NULL
    265 		 || sc->gif_psrc->sa_family != AF_INET
    266 		 || sc->gif_pdst->sa_family != AF_INET) {
    267 			continue;
    268 		}
    269 
    270 		if ((sc->gif_if.if_flags & IFF_LINK0)
    271 		 && satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    272 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == INADDR_ANY) {
    273 			gifp = &sc->gif_if;
    274 			continue;
    275 		}
    276 
    277 		if (satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    278 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == ip->ip_src.s_addr)
    279 		{
    280 			gifp = &sc->gif_if;
    281 			break;
    282 		}
    283 	}
    284 
    285 	if (gifp == NULL) {
    286 #ifdef MROUTING
    287 		/* for backward compatibility */
    288 		if (proto == IPPROTO_IPV4) {
    289 			ipip_input(m, off, proto);
    290 			return;
    291 		}
    292 #endif /*MROUTING*/
    293 		m_freem(m);
    294 		ipstat.ips_nogif++;
    295 		return;
    296 	}
    297 
    298 	otos = ip->ip_tos;
    299 	m_adj(m, off);
    300 
    301 	switch (proto) {
    302 #ifdef INET
    303 	case IPPROTO_IPV4:
    304 	    {
    305 		struct ip *ip;
    306 		af = AF_INET;
    307 		if (m->m_len < sizeof(*ip)) {
    308 			m = m_pullup(m, sizeof(*ip));
    309 			if (!m)
    310 				return;
    311 		}
    312 		ip = mtod(m, struct ip *);
    313 		if (gifp->if_flags & IFF_LINK1)
    314 			ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
    315 		break;
    316 	    }
    317 #endif
    318 #ifdef INET6
    319 	case IPPROTO_IPV6:
    320 	    {
    321 		struct ip6_hdr *ip6;
    322 		u_int8_t itos;
    323 		af = AF_INET6;
    324 		if (m->m_len < sizeof(*ip6)) {
    325 			m = m_pullup(m, sizeof(*ip6));
    326 			if (!m)
    327 				return;
    328 		}
    329 		ip6 = mtod(m, struct ip6_hdr *);
    330 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    331 		if (gifp->if_flags & IFF_LINK1)
    332 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    333 		ip6->ip6_flow &= ~htonl(0xff << 20);
    334 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    335 		break;
    336 	    }
    337 #endif /* INET6 */
    338 	default:
    339 		ipstat.ips_nogif++;
    340 		m_freem(m);
    341 		return;
    342 	}
    343 	gif_input(m, af, gifp);
    344 	return;
    345 }
    346