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