Home | History | Annotate | Line # | Download | only in netinet
in_gif.c revision 1.9
      1 /*	$NetBSD: in_gif.c,v 1.9 2000/02/07 06:15:16 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 #include "opt_inet.h"
     37 #include "opt_ipsec.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/protosw.h>
     47 
     48 #include <net/if.h>
     49 #include <net/route.h>
     50 #include <net/if_gif.h>
     51 
     52 #include <netinet/in.h>
     53 #include <netinet/in_systm.h>
     54 #include <netinet/ip.h>
     55 #include <netinet/ip_var.h>
     56 #include <netinet/in_gif.h>
     57 #include <netinet/ip_ecn.h>
     58 
     59 #ifdef INET6
     60 #include <netinet/ip6.h>
     61 #endif
     62 
     63 #ifdef MROUTING
     64 #include <netinet/ip_mroute.h>
     65 #endif /* MROUTING */
     66 
     67 #include <net/if_gif.h>
     68 
     69 #include "gif.h"
     70 
     71 #include <machine/stdarg.h>
     72 
     73 #include <net/net_osdep.h>
     74 
     75 #if NGIF > 0
     76 int ip_gif_ttl = GIF_TTL;
     77 #else
     78 int ip_gif_ttl = 0;
     79 #endif
     80 
     81 int
     82 in_gif_output(ifp, family, m, rt)
     83 	struct ifnet	*ifp;
     84 	int		family;
     85 	struct mbuf	*m;
     86 	struct rtentry *rt;
     87 {
     88 	register struct gif_softc *sc = (struct gif_softc*)ifp;
     89 	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
     90 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
     91 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
     92 	struct ip iphdr;	/* capsule IP header, host byte ordered */
     93 	int proto, error;
     94 	u_int8_t tos;
     95 
     96 	if (sin_src == NULL || sin_dst == NULL ||
     97 	    sin_src->sin_family != AF_INET ||
     98 	    sin_dst->sin_family != AF_INET) {
     99 		m_freem(m);
    100 		return EAFNOSUPPORT;
    101 	}
    102 
    103 	switch (family) {
    104 #ifdef INET
    105 	case AF_INET:
    106 	    {
    107 		struct ip *ip;
    108 
    109 		proto = IPPROTO_IPV4;
    110 		if (m->m_len < sizeof(*ip)) {
    111 			m = m_pullup(m, sizeof(*ip));
    112 			if (!m)
    113 				return ENOBUFS;
    114 		}
    115 		ip = mtod(m, struct ip *);
    116 		tos = ip->ip_tos;
    117 		break;
    118 	    }
    119 #endif /*INET*/
    120 #ifdef INET6
    121 	case AF_INET6:
    122 	    {
    123 		struct ip6_hdr *ip6;
    124 		proto = IPPROTO_IPV6;
    125 		if (m->m_len < sizeof(*ip6)) {
    126 			m = m_pullup(m, sizeof(*ip6));
    127 			if (!m)
    128 				return ENOBUFS;
    129 		}
    130 		ip6 = mtod(m, struct ip6_hdr *);
    131 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    132 		break;
    133 	    }
    134 #endif /*INET6*/
    135 	default:
    136 #ifdef DEBUG
    137 		printf("in_gif_output: warning: unknown family %d passed\n",
    138 			family);
    139 #endif
    140 		m_freem(m);
    141 		return EAFNOSUPPORT;
    142 	}
    143 
    144 	bzero(&iphdr, sizeof(iphdr));
    145 	iphdr.ip_src = sin_src->sin_addr;
    146 	if (ifp->if_flags & IFF_LINK0) {
    147 		/* multi-destination mode */
    148 		if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    149 			iphdr.ip_dst = sin_dst->sin_addr;
    150 		else if (rt) {
    151 			if (family != AF_INET) {
    152 				m_freem(m);
    153 				return EINVAL;	/*XXX*/
    154 			}
    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 = ip_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 #ifndef __OpenBSD__	/*KAME IPSEC*/
    217 	m->m_pkthdr.rcvif = NULL;
    218 #endif
    219 #endif /*IPSEC*/
    220 #ifndef __OpenBSD__
    221 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL);
    222 #else
    223 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
    224 #endif
    225 	return(error);
    226 }
    227 
    228 void
    229 #if __STDC__
    230 in_gif_input(struct mbuf *m, ...)
    231 #else
    232 in_gif_input(m, va_alist)
    233 	struct mbuf *m;
    234 	va_dcl
    235 #endif
    236 {
    237 	int off, proto;
    238 	struct gif_softc *sc;
    239 	struct ifnet *gifp = NULL;
    240 	struct ip *ip;
    241 	int i, af;
    242 	va_list ap;
    243 	u_int8_t otos;
    244 
    245 	va_start(ap, m);
    246 	off = va_arg(ap, int);
    247 	proto = va_arg(ap, int);
    248 	va_end(ap);
    249 
    250 	ip = mtod(m, struct ip *);
    251 
    252 	/* this code will be soon improved. */
    253 #define	satosin(sa)	((struct sockaddr_in *)(sa))
    254 	for (i = 0, sc = gif; i < ngif; i++, sc++) {
    255 		if (sc->gif_psrc == NULL
    256 		 || sc->gif_pdst == NULL
    257 		 || sc->gif_psrc->sa_family != AF_INET
    258 		 || sc->gif_pdst->sa_family != AF_INET) {
    259 			continue;
    260 		}
    261 
    262 		if ((sc->gif_if.if_flags & IFF_UP) == 0)
    263 			continue;
    264 
    265 		if ((sc->gif_if.if_flags & IFF_LINK0)
    266 		 && satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    267 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == INADDR_ANY) {
    268 			gifp = &sc->gif_if;
    269 			continue;
    270 		}
    271 
    272 		if (satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr
    273 		 && satosin(sc->gif_pdst)->sin_addr.s_addr == ip->ip_src.s_addr)
    274 		{
    275 			gifp = &sc->gif_if;
    276 			break;
    277 		}
    278 	}
    279 
    280 	if (gifp == NULL) {
    281 #ifdef MROUTING
    282 		/* for backward compatibility */
    283 		if (proto == IPPROTO_IPV4) {
    284 			ipip_input(m, off, proto);
    285 			return;
    286 		}
    287 #endif /*MROUTING*/
    288 		m_freem(m);
    289 		ipstat.ips_nogif++;
    290 		return;
    291 	}
    292 
    293 	otos = ip->ip_tos;
    294 	m_adj(m, off);
    295 
    296 	switch (proto) {
    297 #ifdef INET
    298 	case IPPROTO_IPV4:
    299 	    {
    300 		struct ip *ip;
    301 		af = AF_INET;
    302 		if (m->m_len < sizeof(*ip)) {
    303 			m = m_pullup(m, sizeof(*ip));
    304 			if (!m)
    305 				return;
    306 		}
    307 		ip = mtod(m, struct ip *);
    308 		if (gifp->if_flags & IFF_LINK1)
    309 			ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
    310 		break;
    311 	    }
    312 #endif
    313 #ifdef INET6
    314 	case IPPROTO_IPV6:
    315 	    {
    316 		struct ip6_hdr *ip6;
    317 		u_int8_t itos;
    318 		af = AF_INET6;
    319 		if (m->m_len < sizeof(*ip6)) {
    320 			m = m_pullup(m, sizeof(*ip6));
    321 			if (!m)
    322 				return;
    323 		}
    324 		ip6 = mtod(m, struct ip6_hdr *);
    325 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    326 		if (gifp->if_flags & IFF_LINK1)
    327 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    328 		ip6->ip6_flow &= ~htonl(0xff << 20);
    329 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    330 		break;
    331 	    }
    332 #endif /* INET6 */
    333 	default:
    334 		ipstat.ips_nogif++;
    335 		m_freem(m);
    336 		return;
    337 	}
    338 	gif_input(m, af, gifp);
    339 	return;
    340 }
    341