in_gif.c revision 196039
178064Sume/*	$KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $	*/
262587Sitojun
3139823Simp/*-
454263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
554263Sshin * All rights reserved.
654263Sshin *
754263Sshin * Redistribution and use in source and binary forms, with or without
854263Sshin * modification, are permitted provided that the following conditions
954263Sshin * are met:
1054263Sshin * 1. Redistributions of source code must retain the above copyright
1154263Sshin *    notice, this list of conditions and the following disclaimer.
1254263Sshin * 2. Redistributions in binary form must reproduce the above copyright
1354263Sshin *    notice, this list of conditions and the following disclaimer in the
1454263Sshin *    documentation and/or other materials provided with the distribution.
1554263Sshin * 3. Neither the name of the project nor the names of its contributors
1654263Sshin *    may be used to endorse or promote products derived from this software
1754263Sshin *    without specific prior written permission.
1854263Sshin *
1954263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2054263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2154263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2254263Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2354263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2454263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2554263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2654263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2754263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2854263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2954263Sshin * SUCH DAMAGE.
3054263Sshin */
3154263Sshin
32172467Ssilby#include <sys/cdefs.h>
33172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/in_gif.c 196039 2009-08-02 19:43:32Z rwatson $");
34172467Ssilby
3554263Sshin#include "opt_mrouting.h"
3662587Sitojun#include "opt_inet.h"
3754263Sshin#include "opt_inet6.h"
3854263Sshin
3954263Sshin#include <sys/param.h>
4054263Sshin#include <sys/systm.h>
4154263Sshin#include <sys/socket.h>
4254263Sshin#include <sys/sockio.h>
4354263Sshin#include <sys/mbuf.h>
4454263Sshin#include <sys/errno.h>
4554263Sshin#include <sys/kernel.h>
4654263Sshin#include <sys/sysctl.h>
47105293Sume#include <sys/protosw.h>
4862587Sitojun#include <sys/malloc.h>
4962587Sitojun
5054263Sshin#include <net/if.h>
5154263Sshin#include <net/route.h>
52196019Srwatson#include <net/vnet.h>
5354263Sshin
5454263Sshin#include <netinet/in.h>
5554263Sshin#include <netinet/in_systm.h>
5654263Sshin#include <netinet/ip.h>
5754263Sshin#include <netinet/ip_var.h>
5854263Sshin#include <netinet/in_gif.h>
5962587Sitojun#include <netinet/in_var.h>
6062587Sitojun#include <netinet/ip_encap.h>
6155009Sshin#include <netinet/ip_ecn.h>
6262587Sitojun
6354263Sshin#ifdef INET6
6462587Sitojun#include <netinet/ip6.h>
6554263Sshin#endif
6654263Sshin
6754263Sshin#ifdef MROUTING
6854263Sshin#include <netinet/ip_mroute.h>
6954263Sshin#endif /* MROUTING */
7054263Sshin
7162587Sitojun#include <net/if_gif.h>
7254263Sshin
73105293Sumestatic int gif_validate4(const struct ip *, struct gif_softc *,
74105293Sume	struct ifnet *);
75105293Sume
76105293Sumeextern  struct domain inetdomain;
77152242Srustruct protosw in_gif_protosw = {
78152242Sru	.pr_type =		SOCK_RAW,
79152242Sru	.pr_domain =		&inetdomain,
80152242Sru	.pr_protocol =		0/* IPPROTO_IPV[46] */,
81152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
82152242Sru	.pr_input =		in_gif_input,
83152242Sru	.pr_output =		(pr_output_t*)rip_output,
84152242Sru	.pr_ctloutput =		rip_ctloutput,
85152242Sru	.pr_usrreqs =		&rip_usrreqs
86105293Sume};
87105293Sume
88195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
89195699Srwatson	&VNET_NAME(ip_gif_ttl), 0, "");
9054263Sshin
9154263Sshinint
92169454Srwatsonin_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
9354263Sshin{
94147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
9554263Sshin	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
9654263Sshin	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
9754263Sshin	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
9854263Sshin	struct ip iphdr;	/* capsule IP header, host byte ordered */
99153621Sthompsa	struct etherip_header eiphdr;
100189494Smarius	int error, len, proto;
10154263Sshin	u_int8_t tos;
10254263Sshin
103155037Sglebius	GIF_LOCK_ASSERT(sc);
104155037Sglebius
10554263Sshin	if (sin_src == NULL || sin_dst == NULL ||
10654263Sshin	    sin_src->sin_family != AF_INET ||
10754263Sshin	    sin_dst->sin_family != AF_INET) {
10854263Sshin		m_freem(m);
10954263Sshin		return EAFNOSUPPORT;
11054263Sshin	}
11154263Sshin
11254263Sshin	switch (family) {
11362587Sitojun#ifdef INET
11454263Sshin	case AF_INET:
11554263Sshin	    {
11654263Sshin		struct ip *ip;
11754263Sshin
11854263Sshin		proto = IPPROTO_IPV4;
11954263Sshin		if (m->m_len < sizeof(*ip)) {
12054263Sshin			m = m_pullup(m, sizeof(*ip));
12154263Sshin			if (!m)
12254263Sshin				return ENOBUFS;
12354263Sshin		}
12454263Sshin		ip = mtod(m, struct ip *);
12554263Sshin		tos = ip->ip_tos;
12654263Sshin		break;
12754263Sshin	    }
12895023Ssuz#endif /* INET */
12954263Sshin#ifdef INET6
13054263Sshin	case AF_INET6:
13154263Sshin	    {
13262587Sitojun		struct ip6_hdr *ip6;
13354263Sshin		proto = IPPROTO_IPV6;
13454263Sshin		if (m->m_len < sizeof(*ip6)) {
13554263Sshin			m = m_pullup(m, sizeof(*ip6));
13654263Sshin			if (!m)
13754263Sshin				return ENOBUFS;
13854263Sshin		}
13954263Sshin		ip6 = mtod(m, struct ip6_hdr *);
14054263Sshin		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
14154263Sshin		break;
14254263Sshin	    }
14395023Ssuz#endif /* INET6 */
144153621Sthompsa	case AF_LINK:
145153621Sthompsa 		proto = IPPROTO_ETHERIP;
146193664Shrs
147193664Shrs		/*
148193664Shrs		 * GIF_SEND_REVETHIP (disabled by default) intentionally
149193664Shrs		 * sends an EtherIP packet with revered version field in
150193664Shrs		 * the header.  This is a knob for backward compatibility
151193664Shrs		 * with FreeBSD 7.2R or prior.
152193664Shrs		 */
153193664Shrs		if ((sc->gif_options & GIF_SEND_REVETHIP)) {
154193664Shrs 			eiphdr.eip_ver = 0;
155193664Shrs 			eiphdr.eip_resvl = ETHERIP_VERSION;
156193664Shrs 			eiphdr.eip_resvh = 0;
157193664Shrs		} else {
158193664Shrs 			eiphdr.eip_ver = ETHERIP_VERSION;
159193664Shrs 			eiphdr.eip_resvl = 0;
160193664Shrs 			eiphdr.eip_resvh = 0;
161193664Shrs		}
162153621Sthompsa 		/* prepend Ethernet-in-IP header */
163153621Sthompsa 		M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
164153621Sthompsa 		if (m && m->m_len < sizeof(struct etherip_header))
165153621Sthompsa 			m = m_pullup(m, sizeof(struct etherip_header));
166153621Sthompsa 		if (m == NULL)
167153621Sthompsa 			return ENOBUFS;
168153621Sthompsa 		bcopy(&eiphdr, mtod(m, struct etherip_header *),
169153621Sthompsa		    sizeof(struct etherip_header));
170153621Sthompsa		break;
171153621Sthompsa
17254263Sshin	default:
17362587Sitojun#ifdef DEBUG
17454263Sshin		printf("in_gif_output: warning: unknown family %d passed\n",
17554263Sshin			family);
17654263Sshin#endif
17754263Sshin		m_freem(m);
17854263Sshin		return EAFNOSUPPORT;
17954263Sshin	}
18054263Sshin
18154263Sshin	bzero(&iphdr, sizeof(iphdr));
18254263Sshin	iphdr.ip_src = sin_src->sin_addr;
18378064Sume	/* bidirectional configured tunnel mode */
18478064Sume	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
18578064Sume		iphdr.ip_dst = sin_dst->sin_addr;
18678064Sume	else {
18778064Sume		m_freem(m);
18878064Sume		return ENETUNREACH;
18954263Sshin	}
19054263Sshin	iphdr.ip_p = proto;
19154263Sshin	/* version will be set in ip_output() */
192181803Sbz	iphdr.ip_ttl = V_ip_gif_ttl;
19354263Sshin	iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
194121684Sume	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
195121684Sume		       &iphdr.ip_tos, &tos);
19654263Sshin
19754263Sshin	/* prepend new IP header */
198189494Smarius	len = sizeof(struct ip);
199189494Smarius#ifndef __NO_STRICT_ALIGNMENT
200189494Smarius	if (family == AF_LINK)
201189494Smarius		len += ETHERIP_ALIGN;
202189494Smarius#endif
203189494Smarius	M_PREPEND(m, len, M_DONTWAIT);
204189494Smarius	if (m != NULL && m->m_len < len)
205189494Smarius		m = m_pullup(m, len);
20654263Sshin	if (m == NULL) {
20754263Sshin		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
20854263Sshin		return ENOBUFS;
20954263Sshin	}
210189494Smarius#ifndef __NO_STRICT_ALIGNMENT
211189494Smarius	if (family == AF_LINK) {
212189494Smarius		len = mtod(m, vm_offset_t) & 3;
213189494Smarius		KASSERT(len == 0 || len == ETHERIP_ALIGN,
214189494Smarius		    ("in_gif_output: unexpected misalignment"));
215189494Smarius		m->m_data += len;
216189494Smarius		m->m_len -= ETHERIP_ALIGN;
217189494Smarius	}
218189494Smarius#endif
21962587Sitojun	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
22054263Sshin
221178888Sjulian	M_SETFIB(m, sc->gif_fibnum);
222178888Sjulian
22354263Sshin	if (dst->sin_family != sin_dst->sin_family ||
22454263Sshin	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
22554263Sshin		/* cache route doesn't match */
226130662Sbms		bzero(dst, sizeof(*dst));
22754263Sshin		dst->sin_family = sin_dst->sin_family;
22854263Sshin		dst->sin_len = sizeof(struct sockaddr_in);
22954263Sshin		dst->sin_addr = sin_dst->sin_addr;
23054263Sshin		if (sc->gif_ro.ro_rt) {
23154263Sshin			RTFREE(sc->gif_ro.ro_rt);
23254263Sshin			sc->gif_ro.ro_rt = NULL;
23354263Sshin		}
23462587Sitojun#if 0
235147256Sbrooks		GIF2IFP(sc)->if_mtu = GIF_MTU;
23662587Sitojun#endif
23754263Sshin	}
23854263Sshin
23954263Sshin	if (sc->gif_ro.ro_rt == NULL) {
240178888Sjulian		in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
24154263Sshin		if (sc->gif_ro.ro_rt == NULL) {
24254263Sshin			m_freem(m);
24354263Sshin			return ENETUNREACH;
24454263Sshin		}
24562587Sitojun
24662587Sitojun		/* if it constitutes infinite encapsulation, punt. */
24762587Sitojun		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
24862587Sitojun			m_freem(m);
24995023Ssuz			return ENETUNREACH;	/* XXX */
25062587Sitojun		}
25162587Sitojun#if 0
25262587Sitojun		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
25362587Sitojun			- sizeof(struct ip);
25462587Sitojun#endif
25554263Sshin	}
25654263Sshin
257105194Ssam	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
258138470Sglebius
259147256Sbrooks	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
260138653Sglebius	    sc->gif_ro.ro_rt != NULL) {
261138470Sglebius		RTFREE(sc->gif_ro.ro_rt);
262138470Sglebius		sc->gif_ro.ro_rt = NULL;
263138470Sglebius	}
264138470Sglebius
265120885Sume	return (error);
26654263Sshin}
26754263Sshin
26854263Sshinvoid
269169454Srwatsonin_gif_input(struct mbuf *m, int off)
27054263Sshin{
27154263Sshin	struct ifnet *gifp = NULL;
272147503Sbz	struct gif_softc *sc;
27354263Sshin	struct ip *ip;
27462587Sitojun	int af;
27555009Sshin	u_int8_t otos;
27682884Sjulian	int proto;
27754263Sshin
27854263Sshin	ip = mtod(m, struct ip *);
27982884Sjulian	proto = ip->ip_p;
28054263Sshin
281147503Sbz	sc = (struct gif_softc *)encap_getarg(m);
282147503Sbz	if (sc == NULL) {
283147503Sbz		m_freem(m);
284196039Srwatson		KMOD_IPSTAT_INC(ips_nogif);
285147503Sbz		return;
286147503Sbz	}
28754263Sshin
288147503Sbz	gifp = GIF2IFP(sc);
28962587Sitojun	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
29054263Sshin		m_freem(m);
291196039Srwatson		KMOD_IPSTAT_INC(ips_nogif);
29254263Sshin		return;
29354263Sshin	}
29454263Sshin
29555009Sshin	otos = ip->ip_tos;
29654263Sshin	m_adj(m, off);
29754263Sshin
29854263Sshin	switch (proto) {
29962587Sitojun#ifdef INET
30054263Sshin	case IPPROTO_IPV4:
30154263Sshin	    {
30254263Sshin		struct ip *ip;
30354263Sshin		af = AF_INET;
30454263Sshin		if (m->m_len < sizeof(*ip)) {
30554263Sshin			m = m_pullup(m, sizeof(*ip));
30654263Sshin			if (!m)
30754263Sshin				return;
30854263Sshin		}
30954263Sshin		ip = mtod(m, struct ip *);
310121684Sume		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
311121684Sume				  ECN_ALLOWED : ECN_NOCARE,
312121684Sume				  &otos, &ip->ip_tos) == 0) {
313121684Sume			m_freem(m);
314121684Sume			return;
315121684Sume		}
31654263Sshin		break;
31754263Sshin	    }
31862587Sitojun#endif
31954263Sshin#ifdef INET6
32054263Sshin	case IPPROTO_IPV6:
32154263Sshin	    {
32254263Sshin		struct ip6_hdr *ip6;
323121684Sume		u_int8_t itos, oitos;
324121684Sume
32554263Sshin		af = AF_INET6;
32654263Sshin		if (m->m_len < sizeof(*ip6)) {
32754263Sshin			m = m_pullup(m, sizeof(*ip6));
32854263Sshin			if (!m)
32954263Sshin				return;
33054263Sshin		}
33154263Sshin		ip6 = mtod(m, struct ip6_hdr *);
332121684Sume		itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
333121684Sume		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
334121684Sume				  ECN_ALLOWED : ECN_NOCARE,
335121684Sume				  &otos, &itos) == 0) {
336121684Sume			m_freem(m);
337121684Sume			return;
338121684Sume		}
339121684Sume		if (itos != oitos) {
340121684Sume			ip6->ip6_flow &= ~htonl(0xff << 20);
341121684Sume			ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
342121684Sume		}
34354263Sshin		break;
34454263Sshin	    }
34554263Sshin#endif /* INET6 */
346153621Sthompsa 	case IPPROTO_ETHERIP:
347153621Sthompsa 		af = AF_LINK;
348153621Sthompsa 		break;
349153621Sthompsa
35054263Sshin	default:
351196039Srwatson		KMOD_IPSTAT_INC(ips_nogif);
35254263Sshin		m_freem(m);
35354263Sshin		return;
35454263Sshin	}
35554263Sshin	gif_input(m, af, gifp);
35654263Sshin	return;
35754263Sshin}
35862587Sitojun
35962587Sitojun/*
360105293Sume * validate outer address.
36162587Sitojun */
362105293Sumestatic int
363169454Srwatsongif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
36462587Sitojun{
36562587Sitojun	struct sockaddr_in *src, *dst;
36662587Sitojun	struct in_ifaddr *ia4;
36762587Sitojun
36862587Sitojun	src = (struct sockaddr_in *)sc->gif_psrc;
36962587Sitojun	dst = (struct sockaddr_in *)sc->gif_pdst;
37062587Sitojun
37162587Sitojun	/* check for address match */
372105293Sume	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
373105293Sume	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
37462587Sitojun		return 0;
37562587Sitojun
37662587Sitojun	/* martian filters on outer source - NOT done in ip_input! */
377105293Sume	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
37862587Sitojun		return 0;
379105293Sume	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
38062587Sitojun	case 0: case 127: case 255:
38162587Sitojun		return 0;
38262587Sitojun	}
383194951Srwatson
38462587Sitojun	/* reject packets with broadcast on source */
385194951Srwatson	/* XXXRW: should use hash lists? */
386194951Srwatson	IN_IFADDR_RLOCK();
387181803Sbz	TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
38862587Sitojun		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
38962587Sitojun			continue;
390194951Srwatson		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
391194951Srwatson			IN_IFADDR_RUNLOCK();
39262587Sitojun			return 0;
393194951Srwatson		}
39462587Sitojun	}
395194951Srwatson	IN_IFADDR_RUNLOCK();
39662587Sitojun
39762587Sitojun	/* ingress filters on outer source */
398147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
39962587Sitojun		struct sockaddr_in sin;
40062587Sitojun		struct rtentry *rt;
40162587Sitojun
40262587Sitojun		bzero(&sin, sizeof(sin));
40362587Sitojun		sin.sin_family = AF_INET;
40462587Sitojun		sin.sin_len = sizeof(struct sockaddr_in);
405105293Sume		sin.sin_addr = ip->ip_src;
406178888Sjulian		/* XXX MRT  check for the interface we would use on output */
407178888Sjulian		rt = in_rtalloc1((struct sockaddr *)&sin, 0,
408178888Sjulian		    0UL, sc->gif_fibnum);
409105293Sume		if (!rt || rt->rt_ifp != ifp) {
41078064Sume#if 0
41178064Sume			log(LOG_WARNING, "%s: packet from 0x%x dropped "
412147256Sbrooks			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
41378064Sume			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
41478064Sume#endif
41578064Sume			if (rt)
416172307Scsjp				RTFREE_LOCKED(rt);
41762587Sitojun			return 0;
41862587Sitojun		}
419172307Scsjp		RTFREE_LOCKED(rt);
42062587Sitojun	}
42162587Sitojun
42278064Sume	return 32 * 2;
42362587Sitojun}
424105293Sume
425105293Sume/*
426105293Sume * we know that we are in IFF_UP, outer address available, and outer family
427105293Sume * matched the physical addr family.  see gif_encapcheck().
428105293Sume */
429105293Sumeint
430169454Srwatsongif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
431105293Sume{
432105293Sume	struct ip ip;
433105293Sume	struct gif_softc *sc;
434105293Sume	struct ifnet *ifp;
435105293Sume
436105293Sume	/* sanity check done in caller */
437105293Sume	sc = (struct gif_softc *)arg;
438105293Sume
439105293Sume	/* LINTED const cast */
440105293Sume	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
441105293Sume	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
442105293Sume
443105293Sume	return gif_validate4(&ip, sc, ifp);
444105293Sume}
445105293Sume
446105293Sumeint
447169454Srwatsonin_gif_attach(struct gif_softc *sc)
448105293Sume{
449105293Sume	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
450105293Sume	    &in_gif_protosw, sc);
451105293Sume	if (sc->encap_cookie4 == NULL)
452105293Sume		return EEXIST;
453105293Sume	return 0;
454105293Sume}
455105293Sume
456105293Sumeint
457169454Srwatsonin_gif_detach(struct gif_softc *sc)
458105293Sume{
459105293Sume	int error;
460105293Sume
461105293Sume	error = encap_detach(sc->encap_cookie4);
462105293Sume	if (error == 0)
463105293Sume		sc->encap_cookie4 = NULL;
464105293Sume	return error;
465105293Sume}
466