in_gif.c revision 243882
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 243882 2012-12-05 08:04:20Z glebius $");
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
88207369SbzVNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
89207369Sbz#define	V_ip_gif_ttl		VNET(ip_gif_ttl)
90195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
91195699Srwatson	&VNET_NAME(ip_gif_ttl), 0, "");
9254263Sshin
9354263Sshinint
94169454Srwatsonin_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
9554263Sshin{
96147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
9754263Sshin	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
9854263Sshin	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
9954263Sshin	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
10054263Sshin	struct ip iphdr;	/* capsule IP header, host byte ordered */
101153621Sthompsa	struct etherip_header eiphdr;
102189494Smarius	int error, len, proto;
10354263Sshin	u_int8_t tos;
10454263Sshin
105155037Sglebius	GIF_LOCK_ASSERT(sc);
106155037Sglebius
10754263Sshin	if (sin_src == NULL || sin_dst == NULL ||
10854263Sshin	    sin_src->sin_family != AF_INET ||
10954263Sshin	    sin_dst->sin_family != AF_INET) {
11054263Sshin		m_freem(m);
11154263Sshin		return EAFNOSUPPORT;
11254263Sshin	}
11354263Sshin
11454263Sshin	switch (family) {
11562587Sitojun#ifdef INET
11654263Sshin	case AF_INET:
11754263Sshin	    {
11854263Sshin		struct ip *ip;
11954263Sshin
12054263Sshin		proto = IPPROTO_IPV4;
12154263Sshin		if (m->m_len < sizeof(*ip)) {
12254263Sshin			m = m_pullup(m, sizeof(*ip));
12354263Sshin			if (!m)
12454263Sshin				return ENOBUFS;
12554263Sshin		}
12654263Sshin		ip = mtod(m, struct ip *);
12754263Sshin		tos = ip->ip_tos;
12854263Sshin		break;
12954263Sshin	    }
13095023Ssuz#endif /* INET */
13154263Sshin#ifdef INET6
13254263Sshin	case AF_INET6:
13354263Sshin	    {
13462587Sitojun		struct ip6_hdr *ip6;
13554263Sshin		proto = IPPROTO_IPV6;
13654263Sshin		if (m->m_len < sizeof(*ip6)) {
13754263Sshin			m = m_pullup(m, sizeof(*ip6));
13854263Sshin			if (!m)
13954263Sshin				return ENOBUFS;
14054263Sshin		}
14154263Sshin		ip6 = mtod(m, struct ip6_hdr *);
14254263Sshin		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
14354263Sshin		break;
14454263Sshin	    }
14595023Ssuz#endif /* INET6 */
146153621Sthompsa	case AF_LINK:
147153621Sthompsa 		proto = IPPROTO_ETHERIP;
148193664Shrs
149193664Shrs		/*
150193664Shrs		 * GIF_SEND_REVETHIP (disabled by default) intentionally
151193664Shrs		 * sends an EtherIP packet with revered version field in
152193664Shrs		 * the header.  This is a knob for backward compatibility
153193664Shrs		 * with FreeBSD 7.2R or prior.
154193664Shrs		 */
155193664Shrs		if ((sc->gif_options & GIF_SEND_REVETHIP)) {
156193664Shrs 			eiphdr.eip_ver = 0;
157193664Shrs 			eiphdr.eip_resvl = ETHERIP_VERSION;
158193664Shrs 			eiphdr.eip_resvh = 0;
159193664Shrs		} else {
160193664Shrs 			eiphdr.eip_ver = ETHERIP_VERSION;
161193664Shrs 			eiphdr.eip_resvl = 0;
162193664Shrs 			eiphdr.eip_resvh = 0;
163193664Shrs		}
164153621Sthompsa 		/* prepend Ethernet-in-IP header */
165243882Sglebius 		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
166153621Sthompsa 		if (m && m->m_len < sizeof(struct etherip_header))
167153621Sthompsa 			m = m_pullup(m, sizeof(struct etherip_header));
168153621Sthompsa 		if (m == NULL)
169153621Sthompsa 			return ENOBUFS;
170153621Sthompsa 		bcopy(&eiphdr, mtod(m, struct etherip_header *),
171153621Sthompsa		    sizeof(struct etherip_header));
172153621Sthompsa		break;
173153621Sthompsa
17454263Sshin	default:
17562587Sitojun#ifdef DEBUG
17654263Sshin		printf("in_gif_output: warning: unknown family %d passed\n",
17754263Sshin			family);
17854263Sshin#endif
17954263Sshin		m_freem(m);
18054263Sshin		return EAFNOSUPPORT;
18154263Sshin	}
18254263Sshin
18354263Sshin	bzero(&iphdr, sizeof(iphdr));
18454263Sshin	iphdr.ip_src = sin_src->sin_addr;
18578064Sume	/* bidirectional configured tunnel mode */
18678064Sume	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
18778064Sume		iphdr.ip_dst = sin_dst->sin_addr;
18878064Sume	else {
18978064Sume		m_freem(m);
19078064Sume		return ENETUNREACH;
19154263Sshin	}
19254263Sshin	iphdr.ip_p = proto;
19354263Sshin	/* version will be set in ip_output() */
194181803Sbz	iphdr.ip_ttl = V_ip_gif_ttl;
195241913Sglebius	iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
196121684Sume	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
197121684Sume		       &iphdr.ip_tos, &tos);
19854263Sshin
19954263Sshin	/* prepend new IP header */
200189494Smarius	len = sizeof(struct ip);
201189494Smarius#ifndef __NO_STRICT_ALIGNMENT
202189494Smarius	if (family == AF_LINK)
203189494Smarius		len += ETHERIP_ALIGN;
204189494Smarius#endif
205243882Sglebius	M_PREPEND(m, len, M_NOWAIT);
206189494Smarius	if (m != NULL && m->m_len < len)
207189494Smarius		m = m_pullup(m, len);
20854263Sshin	if (m == NULL) {
20954263Sshin		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
21054263Sshin		return ENOBUFS;
21154263Sshin	}
212189494Smarius#ifndef __NO_STRICT_ALIGNMENT
213189494Smarius	if (family == AF_LINK) {
214189494Smarius		len = mtod(m, vm_offset_t) & 3;
215189494Smarius		KASSERT(len == 0 || len == ETHERIP_ALIGN,
216189494Smarius		    ("in_gif_output: unexpected misalignment"));
217189494Smarius		m->m_data += len;
218189494Smarius		m->m_len -= ETHERIP_ALIGN;
219189494Smarius	}
220189494Smarius#endif
22162587Sitojun	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
22254263Sshin
223178888Sjulian	M_SETFIB(m, sc->gif_fibnum);
224178888Sjulian
22554263Sshin	if (dst->sin_family != sin_dst->sin_family ||
22654263Sshin	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
22754263Sshin		/* cache route doesn't match */
228130662Sbms		bzero(dst, sizeof(*dst));
22954263Sshin		dst->sin_family = sin_dst->sin_family;
23054263Sshin		dst->sin_len = sizeof(struct sockaddr_in);
23154263Sshin		dst->sin_addr = sin_dst->sin_addr;
23254263Sshin		if (sc->gif_ro.ro_rt) {
23354263Sshin			RTFREE(sc->gif_ro.ro_rt);
23454263Sshin			sc->gif_ro.ro_rt = NULL;
23554263Sshin		}
23662587Sitojun#if 0
237147256Sbrooks		GIF2IFP(sc)->if_mtu = GIF_MTU;
23862587Sitojun#endif
23954263Sshin	}
24054263Sshin
24154263Sshin	if (sc->gif_ro.ro_rt == NULL) {
242178888Sjulian		in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
24354263Sshin		if (sc->gif_ro.ro_rt == NULL) {
24454263Sshin			m_freem(m);
24554263Sshin			return ENETUNREACH;
24654263Sshin		}
24762587Sitojun
24862587Sitojun		/* if it constitutes infinite encapsulation, punt. */
24962587Sitojun		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
25062587Sitojun			m_freem(m);
25195023Ssuz			return ENETUNREACH;	/* XXX */
25262587Sitojun		}
25362587Sitojun#if 0
25462587Sitojun		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
25562587Sitojun			- sizeof(struct ip);
25662587Sitojun#endif
25754263Sshin	}
25854263Sshin
259105194Ssam	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
260138470Sglebius
261147256Sbrooks	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
262138653Sglebius	    sc->gif_ro.ro_rt != NULL) {
263138470Sglebius		RTFREE(sc->gif_ro.ro_rt);
264138470Sglebius		sc->gif_ro.ro_rt = NULL;
265138470Sglebius	}
266138470Sglebius
267120885Sume	return (error);
26854263Sshin}
26954263Sshin
27054263Sshinvoid
271169454Srwatsonin_gif_input(struct mbuf *m, int off)
27254263Sshin{
27354263Sshin	struct ifnet *gifp = NULL;
274147503Sbz	struct gif_softc *sc;
27554263Sshin	struct ip *ip;
27662587Sitojun	int af;
27755009Sshin	u_int8_t otos;
27882884Sjulian	int proto;
27954263Sshin
28054263Sshin	ip = mtod(m, struct ip *);
28182884Sjulian	proto = ip->ip_p;
28254263Sshin
283147503Sbz	sc = (struct gif_softc *)encap_getarg(m);
284147503Sbz	if (sc == NULL) {
285147503Sbz		m_freem(m);
286196039Srwatson		KMOD_IPSTAT_INC(ips_nogif);
287147503Sbz		return;
288147503Sbz	}
28954263Sshin
290147503Sbz	gifp = GIF2IFP(sc);
29162587Sitojun	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
29254263Sshin		m_freem(m);
293196039Srwatson		KMOD_IPSTAT_INC(ips_nogif);
29454263Sshin		return;
29554263Sshin	}
29654263Sshin
29755009Sshin	otos = ip->ip_tos;
29854263Sshin	m_adj(m, off);
29954263Sshin
30054263Sshin	switch (proto) {
30162587Sitojun#ifdef INET
30254263Sshin	case IPPROTO_IPV4:
30354263Sshin	    {
30454263Sshin		struct ip *ip;
30554263Sshin		af = AF_INET;
30654263Sshin		if (m->m_len < sizeof(*ip)) {
30754263Sshin			m = m_pullup(m, sizeof(*ip));
30854263Sshin			if (!m)
30954263Sshin				return;
31054263Sshin		}
31154263Sshin		ip = mtod(m, struct ip *);
312121684Sume		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
313121684Sume				  ECN_ALLOWED : ECN_NOCARE,
314121684Sume				  &otos, &ip->ip_tos) == 0) {
315121684Sume			m_freem(m);
316121684Sume			return;
317121684Sume		}
31854263Sshin		break;
31954263Sshin	    }
32062587Sitojun#endif
32154263Sshin#ifdef INET6
32254263Sshin	case IPPROTO_IPV6:
32354263Sshin	    {
32454263Sshin		struct ip6_hdr *ip6;
325121684Sume		u_int8_t itos, oitos;
326121684Sume
32754263Sshin		af = AF_INET6;
32854263Sshin		if (m->m_len < sizeof(*ip6)) {
32954263Sshin			m = m_pullup(m, sizeof(*ip6));
33054263Sshin			if (!m)
33154263Sshin				return;
33254263Sshin		}
33354263Sshin		ip6 = mtod(m, struct ip6_hdr *);
334121684Sume		itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
335121684Sume		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
336121684Sume				  ECN_ALLOWED : ECN_NOCARE,
337121684Sume				  &otos, &itos) == 0) {
338121684Sume			m_freem(m);
339121684Sume			return;
340121684Sume		}
341121684Sume		if (itos != oitos) {
342121684Sume			ip6->ip6_flow &= ~htonl(0xff << 20);
343121684Sume			ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
344121684Sume		}
34554263Sshin		break;
34654263Sshin	    }
34754263Sshin#endif /* INET6 */
348153621Sthompsa 	case IPPROTO_ETHERIP:
349153621Sthompsa 		af = AF_LINK;
350153621Sthompsa 		break;
351153621Sthompsa
35254263Sshin	default:
353196039Srwatson		KMOD_IPSTAT_INC(ips_nogif);
35454263Sshin		m_freem(m);
35554263Sshin		return;
35654263Sshin	}
35754263Sshin	gif_input(m, af, gifp);
35854263Sshin	return;
35954263Sshin}
36062587Sitojun
36162587Sitojun/*
362105293Sume * validate outer address.
36362587Sitojun */
364105293Sumestatic int
365169454Srwatsongif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
36662587Sitojun{
36762587Sitojun	struct sockaddr_in *src, *dst;
36862587Sitojun	struct in_ifaddr *ia4;
36962587Sitojun
37062587Sitojun	src = (struct sockaddr_in *)sc->gif_psrc;
37162587Sitojun	dst = (struct sockaddr_in *)sc->gif_pdst;
37262587Sitojun
37362587Sitojun	/* check for address match */
374105293Sume	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
375105293Sume	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
37662587Sitojun		return 0;
37762587Sitojun
37862587Sitojun	/* martian filters on outer source - NOT done in ip_input! */
379105293Sume	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
38062587Sitojun		return 0;
381105293Sume	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
38262587Sitojun	case 0: case 127: case 255:
38362587Sitojun		return 0;
38462587Sitojun	}
385194951Srwatson
38662587Sitojun	/* reject packets with broadcast on source */
387194951Srwatson	/* XXXRW: should use hash lists? */
388194951Srwatson	IN_IFADDR_RLOCK();
389181803Sbz	TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
39062587Sitojun		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
39162587Sitojun			continue;
392194951Srwatson		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
393194951Srwatson			IN_IFADDR_RUNLOCK();
39462587Sitojun			return 0;
395194951Srwatson		}
39662587Sitojun	}
397194951Srwatson	IN_IFADDR_RUNLOCK();
39862587Sitojun
39962587Sitojun	/* ingress filters on outer source */
400147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
40162587Sitojun		struct sockaddr_in sin;
40262587Sitojun		struct rtentry *rt;
40362587Sitojun
40462587Sitojun		bzero(&sin, sizeof(sin));
40562587Sitojun		sin.sin_family = AF_INET;
40662587Sitojun		sin.sin_len = sizeof(struct sockaddr_in);
407105293Sume		sin.sin_addr = ip->ip_src;
408178888Sjulian		/* XXX MRT  check for the interface we would use on output */
409178888Sjulian		rt = in_rtalloc1((struct sockaddr *)&sin, 0,
410178888Sjulian		    0UL, sc->gif_fibnum);
411105293Sume		if (!rt || rt->rt_ifp != ifp) {
41278064Sume#if 0
41378064Sume			log(LOG_WARNING, "%s: packet from 0x%x dropped "
414147256Sbrooks			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
41578064Sume			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
41678064Sume#endif
41778064Sume			if (rt)
418172307Scsjp				RTFREE_LOCKED(rt);
41962587Sitojun			return 0;
42062587Sitojun		}
421172307Scsjp		RTFREE_LOCKED(rt);
42262587Sitojun	}
42362587Sitojun
42478064Sume	return 32 * 2;
42562587Sitojun}
426105293Sume
427105293Sume/*
428105293Sume * we know that we are in IFF_UP, outer address available, and outer family
429105293Sume * matched the physical addr family.  see gif_encapcheck().
430105293Sume */
431105293Sumeint
432169454Srwatsongif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
433105293Sume{
434105293Sume	struct ip ip;
435105293Sume	struct gif_softc *sc;
436105293Sume	struct ifnet *ifp;
437105293Sume
438105293Sume	/* sanity check done in caller */
439105293Sume	sc = (struct gif_softc *)arg;
440105293Sume
441105293Sume	/* LINTED const cast */
442105293Sume	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
443105293Sume	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
444105293Sume
445105293Sume	return gif_validate4(&ip, sc, ifp);
446105293Sume}
447105293Sume
448105293Sumeint
449169454Srwatsonin_gif_attach(struct gif_softc *sc)
450105293Sume{
451105293Sume	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
452105293Sume	    &in_gif_protosw, sc);
453105293Sume	if (sc->encap_cookie4 == NULL)
454105293Sume		return EEXIST;
455105293Sume	return 0;
456105293Sume}
457105293Sume
458105293Sumeint
459169454Srwatsonin_gif_detach(struct gif_softc *sc)
460105293Sume{
461105293Sume	int error;
462105293Sume
463105293Sume	error = encap_detach(sc->encap_cookie4);
464105293Sume	if (error == 0)
465105293Sume		sc->encap_cookie4 = NULL;
466105293Sume	return error;
467105293Sume}
468