in6_gif.c revision 181803
1139826Simp/*-
254263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
354263Sshin * All rights reserved.
454263Sshin *
554263Sshin * Redistribution and use in source and binary forms, with or without
654263Sshin * modification, are permitted provided that the following conditions
754263Sshin * are met:
854263Sshin * 1. Redistributions of source code must retain the above copyright
954263Sshin *    notice, this list of conditions and the following disclaimer.
1054263Sshin * 2. Redistributions in binary form must reproduce the above copyright
1154263Sshin *    notice, this list of conditions and the following disclaimer in the
1254263Sshin *    documentation and/or other materials provided with the distribution.
1354263Sshin * 3. Neither the name of the project nor the names of its contributors
1454263Sshin *    may be used to endorse or promote products derived from this software
1554263Sshin *    without specific prior written permission.
1654263Sshin *
1754263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1854263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1954263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2054263Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2154263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2254263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2354263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2454263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2554263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2654263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2754263Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
3054263Sshin */
3154263Sshin
32174510Sobrien#include <sys/cdefs.h>
33174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 181803 2008-08-17 23:27:27Z bz $");
34174510Sobrien
3554263Sshin#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
3754263Sshin
3854263Sshin#include <sys/param.h>
3954263Sshin#include <sys/systm.h>
4054263Sshin#include <sys/socket.h>
4154263Sshin#include <sys/sockio.h>
4254263Sshin#include <sys/mbuf.h>
4354263Sshin#include <sys/errno.h>
4478064Sume#include <sys/queue.h>
4578064Sume#include <sys/syslog.h>
46105293Sume#include <sys/protosw.h>
4754263Sshin
4878064Sume#include <sys/malloc.h>
49181803Sbz#include <sys/vimage.h>
5078064Sume
5154263Sshin#include <net/if.h>
5254263Sshin#include <net/route.h>
5354263Sshin
5454263Sshin#include <netinet/in.h>
5554263Sshin#include <netinet/in_systm.h>
5654263Sshin#ifdef INET
5754263Sshin#include <netinet/ip.h>
5854263Sshin#endif
5962587Sitojun#include <netinet/ip_encap.h>
6062587Sitojun#ifdef INET6
6162587Sitojun#include <netinet/ip6.h>
6254263Sshin#include <netinet6/ip6_var.h>
6354263Sshin#include <netinet6/in6_gif.h>
6462587Sitojun#include <netinet6/in6_var.h>
6562587Sitojun#endif
66105293Sume#include <netinet6/ip6protosw.h>
6755009Sshin#include <netinet/ip_ecn.h>
6862587Sitojun#ifdef INET6
6955009Sshin#include <netinet6/ip6_ecn.h>
7062587Sitojun#endif
7154263Sshin
7254263Sshin#include <net/if_gif.h>
7354263Sshin
74105293Sumestatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
75105293Sume			 struct ifnet *);
76105293Sume
77105293Sumeextern  struct domain inet6domain;
78105293Sumestruct ip6protosw in6_gif_protosw =
79120913Sume{ SOCK_RAW,	&inet6domain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
80105293Sume  in6_gif_input, rip6_output,	0,		rip6_ctloutput,
81105293Sume  0,
82105293Sume  0,		0,		0,		0,
83105293Sume  &rip6_usrreqs
84105293Sume};
85105293Sume
8654263Sshinint
87171259Sdelphijin6_gif_output(struct ifnet *ifp,
88171259Sdelphij    int family,			/* family of the packet to be encapsulate */
89171259Sdelphij    struct mbuf *m)
9054263Sshin{
91147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
9254263Sshin	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
9354263Sshin	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
9454263Sshin	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
9554263Sshin	struct ip6_hdr *ip6;
96153621Sthompsa	struct etherip_header eiphdr;
97138619Sglebius	int proto, error;
9855009Sshin	u_int8_t itos, otos;
9954263Sshin
100155037Sglebius	GIF_LOCK_ASSERT(sc);
101155037Sglebius
10254263Sshin	if (sin6_src == NULL || sin6_dst == NULL ||
10354263Sshin	    sin6_src->sin6_family != AF_INET6 ||
10454263Sshin	    sin6_dst->sin6_family != AF_INET6) {
10554263Sshin		m_freem(m);
10654263Sshin		return EAFNOSUPPORT;
10754263Sshin	}
10854263Sshin
10954263Sshin	switch (family) {
11054263Sshin#ifdef INET
11154263Sshin	case AF_INET:
11254263Sshin	    {
11354263Sshin		struct ip *ip;
11454263Sshin
11554263Sshin		proto = IPPROTO_IPV4;
11654263Sshin		if (m->m_len < sizeof(*ip)) {
11754263Sshin			m = m_pullup(m, sizeof(*ip));
11854263Sshin			if (!m)
11954263Sshin				return ENOBUFS;
12054263Sshin		}
12154263Sshin		ip = mtod(m, struct ip *);
12255009Sshin		itos = ip->ip_tos;
12354263Sshin		break;
12454263Sshin	    }
12554263Sshin#endif
12662587Sitojun#ifdef INET6
12754263Sshin	case AF_INET6:
12854263Sshin	    {
12954263Sshin		struct ip6_hdr *ip6;
13054263Sshin		proto = IPPROTO_IPV6;
13154263Sshin		if (m->m_len < sizeof(*ip6)) {
13254263Sshin			m = m_pullup(m, sizeof(*ip6));
13354263Sshin			if (!m)
13454263Sshin				return ENOBUFS;
13554263Sshin		}
13654263Sshin		ip6 = mtod(m, struct ip6_hdr *);
13755009Sshin		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
13854263Sshin		break;
13954263Sshin	    }
14062587Sitojun#endif
141153621Sthompsa	case AF_LINK:
142171260Sdelphij		proto = IPPROTO_ETHERIP;
143171260Sdelphij		eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
144171260Sdelphij		eiphdr.eip_pad = 0;
145171260Sdelphij		/* prepend Ethernet-in-IP header */
146171260Sdelphij		M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
147171260Sdelphij		if (m && m->m_len < sizeof(struct etherip_header))
148171260Sdelphij			m = m_pullup(m, sizeof(struct etherip_header));
149171260Sdelphij		if (m == NULL)
150171260Sdelphij			return ENOBUFS;
151171260Sdelphij		bcopy(&eiphdr, mtod(m, struct etherip_header *),
152153621Sthompsa		    sizeof(struct etherip_header));
153153621Sthompsa		break;
154153621Sthompsa
15554263Sshin	default:
15662587Sitojun#ifdef DEBUG
15754263Sshin		printf("in6_gif_output: warning: unknown family %d passed\n",
15854263Sshin			family);
15954263Sshin#endif
16054263Sshin		m_freem(m);
16154263Sshin		return EAFNOSUPPORT;
16254263Sshin	}
163120913Sume
16454263Sshin	/* prepend new IP header */
165111119Simp	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
16654263Sshin	if (m && m->m_len < sizeof(struct ip6_hdr))
16754263Sshin		m = m_pullup(m, sizeof(struct ip6_hdr));
16854263Sshin	if (m == NULL) {
16954263Sshin		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
17054263Sshin		return ENOBUFS;
17154263Sshin	}
17254263Sshin
17354263Sshin	ip6 = mtod(m, struct ip6_hdr *);
17454263Sshin	ip6->ip6_flow	= 0;
17562587Sitojun	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
17662587Sitojun	ip6->ip6_vfc	|= IPV6_VERSION;
17754263Sshin	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
17854263Sshin	ip6->ip6_nxt	= proto;
179181803Sbz	ip6->ip6_hlim	= V_ip6_gif_hlim;
18054263Sshin	ip6->ip6_src	= sin6_src->sin6_addr;
18178064Sume	/* bidirectional configured tunnel mode */
18278064Sume	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
18378064Sume		ip6->ip6_dst = sin6_dst->sin6_addr;
18478064Sume	else  {
18578064Sume		m_freem(m);
18678064Sume		return ENETUNREACH;
18754263Sshin	}
188121684Sume	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
189121684Sume		       &otos, &itos);
190121684Sume	ip6->ip6_flow &= ~htonl(0xff << 20);
19178064Sume	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
19254263Sshin
19354263Sshin	if (dst->sin6_family != sin6_dst->sin6_family ||
19454263Sshin	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
19554263Sshin		/* cache route doesn't match */
19654263Sshin		bzero(dst, sizeof(*dst));
19754263Sshin		dst->sin6_family = sin6_dst->sin6_family;
19854263Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
19954263Sshin		dst->sin6_addr = sin6_dst->sin6_addr;
20054263Sshin		if (sc->gif_ro6.ro_rt) {
20154263Sshin			RTFREE(sc->gif_ro6.ro_rt);
20254263Sshin			sc->gif_ro6.ro_rt = NULL;
20354263Sshin		}
20462587Sitojun#if 0
205147256Sbrooks		GIF2IFP(sc)->if_mtu = GIF_MTU;
20662587Sitojun#endif
20754263Sshin	}
20854263Sshin
20954263Sshin	if (sc->gif_ro6.ro_rt == NULL) {
21054263Sshin		rtalloc((struct route *)&sc->gif_ro6);
21154263Sshin		if (sc->gif_ro6.ro_rt == NULL) {
21254263Sshin			m_freem(m);
21354263Sshin			return ENETUNREACH;
21454263Sshin		}
21562587Sitojun
21662587Sitojun		/* if it constitutes infinite encapsulation, punt. */
21762587Sitojun		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
21862587Sitojun			m_freem(m);
21962587Sitojun			return ENETUNREACH;	/*XXX*/
22062587Sitojun		}
22162587Sitojun#if 0
22262587Sitojun		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
22362587Sitojun			- sizeof(struct ip6_hdr);
22462587Sitojun#endif
22554263Sshin	}
226120913Sume
22762587Sitojun#ifdef IPV6_MINMTU
22862587Sitojun	/*
22962587Sitojun	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
23062587Sitojun	 * it is too painful to ask for resend of inner packet, to achieve
23162587Sitojun	 * path MTU discovery for encapsulated packets.
23262587Sitojun	 */
233138620Sglebius	error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
23462587Sitojun#else
235138620Sglebius	error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
23662587Sitojun#endif
237138619Sglebius
238147256Sbrooks	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
239138653Sglebius	    sc->gif_ro6.ro_rt != NULL) {
240138619Sglebius		RTFREE(sc->gif_ro6.ro_rt);
241138619Sglebius		sc->gif_ro6.ro_rt = NULL;
242138619Sglebius	}
243138619Sglebius
244138619Sglebius	return (error);
24554263Sshin}
24654263Sshin
247120913Sumeint
248171259Sdelphijin6_gif_input(struct mbuf **mp, int *offp, int proto)
24954263Sshin{
25054263Sshin	struct mbuf *m = *mp;
25154263Sshin	struct ifnet *gifp = NULL;
252147507Sume	struct gif_softc *sc;
25354263Sshin	struct ip6_hdr *ip6;
25454263Sshin	int af = 0;
25555009Sshin	u_int32_t otos;
25654263Sshin
25754263Sshin	ip6 = mtod(m, struct ip6_hdr *);
25854263Sshin
259147507Sume	sc = (struct gif_softc *)encap_getarg(m);
260147507Sume	if (sc == NULL) {
261147507Sume		m_freem(m);
262181803Sbz		V_ip6stat.ip6s_nogif++;
263147507Sume		return IPPROTO_DONE;
264147507Sume	}
26554263Sshin
266147507Sume	gifp = GIF2IFP(sc);
26762587Sitojun	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
26854263Sshin		m_freem(m);
269181803Sbz		V_ip6stat.ip6s_nogif++;
27054263Sshin		return IPPROTO_DONE;
27154263Sshin	}
27262587Sitojun
27355009Sshin	otos = ip6->ip6_flow;
27454263Sshin	m_adj(m, *offp);
27554263Sshin
27654263Sshin	switch (proto) {
27754263Sshin#ifdef INET
27854263Sshin	case IPPROTO_IPV4:
27954263Sshin	    {
28054263Sshin		struct ip *ip;
28155009Sshin		u_int8_t otos8;
28254263Sshin		af = AF_INET;
28355009Sshin		otos8 = (ntohl(otos) >> 20) & 0xff;
28454263Sshin		if (m->m_len < sizeof(*ip)) {
28554263Sshin			m = m_pullup(m, sizeof(*ip));
28654263Sshin			if (!m)
28754263Sshin				return IPPROTO_DONE;
28854263Sshin		}
28954263Sshin		ip = mtod(m, struct ip *);
290121684Sume		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
291121684Sume				  ECN_ALLOWED : ECN_NOCARE,
292121684Sume				  &otos8, &ip->ip_tos) == 0) {
293121684Sume			m_freem(m);
294121684Sume			return IPPROTO_DONE;
295121684Sume		}
29654263Sshin		break;
29754263Sshin	    }
29854263Sshin#endif /* INET */
29962587Sitojun#ifdef INET6
30054263Sshin	case IPPROTO_IPV6:
30154263Sshin	    {
30254263Sshin		struct ip6_hdr *ip6;
30354263Sshin		af = AF_INET6;
30454263Sshin		if (m->m_len < sizeof(*ip6)) {
30554263Sshin			m = m_pullup(m, sizeof(*ip6));
30654263Sshin			if (!m)
30754263Sshin				return IPPROTO_DONE;
30854263Sshin		}
30954263Sshin		ip6 = mtod(m, struct ip6_hdr *);
310121684Sume		if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
311121684Sume				   ECN_ALLOWED : ECN_NOCARE,
312121684Sume				   &otos, &ip6->ip6_flow) == 0) {
313121684Sume			m_freem(m);
314121684Sume			return IPPROTO_DONE;
315121684Sume		}
31654263Sshin		break;
31754263Sshin	    }
31862587Sitojun#endif
319171260Sdelphij	case IPPROTO_ETHERIP:
320171260Sdelphij		af = AF_LINK;
321171260Sdelphij		break;
322153621Sthompsa
32354263Sshin	default:
324181803Sbz		V_ip6stat.ip6s_nogif++;
32554263Sshin		m_freem(m);
32654263Sshin		return IPPROTO_DONE;
32754263Sshin	}
328120913Sume
32954263Sshin	gif_input(m, af, gifp);
33054263Sshin	return IPPROTO_DONE;
33154263Sshin}
33262587Sitojun
33362587Sitojun/*
334105293Sume * validate outer address.
33562587Sitojun */
336105293Sumestatic int
337171259Sdelphijgif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
338171259Sdelphij    struct ifnet *ifp)
33962587Sitojun{
34062587Sitojun	struct sockaddr_in6 *src, *dst;
34162587Sitojun
34262587Sitojun	src = (struct sockaddr_in6 *)sc->gif_psrc;
34362587Sitojun	dst = (struct sockaddr_in6 *)sc->gif_pdst;
34462587Sitojun
345105293Sume	/*
346105293Sume	 * Check for address match.  Note that the check is for an incoming
347105293Sume	 * packet.  We should compare the *source* address in our configuration
348105293Sume	 * and the *destination* address of the packet, and vice versa.
349105293Sume	 */
350105293Sume	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
351105293Sume	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
35262587Sitojun		return 0;
35362587Sitojun
35462587Sitojun	/* martian filters on outer source - done in ip6_input */
35562587Sitojun
35662587Sitojun	/* ingress filters on outer source */
357147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
35862587Sitojun		struct sockaddr_in6 sin6;
35962587Sitojun		struct rtentry *rt;
36062587Sitojun
36162587Sitojun		bzero(&sin6, sizeof(sin6));
36262587Sitojun		sin6.sin6_family = AF_INET6;
36362587Sitojun		sin6.sin6_len = sizeof(struct sockaddr_in6);
364105293Sume		sin6.sin6_addr = ip6->ip6_src;
365105293Sume		sin6.sin6_scope_id = 0; /* XXX */
366105293Sume
36762587Sitojun		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
368105293Sume		if (!rt || rt->rt_ifp != ifp) {
36978064Sume#if 0
370165118Sbz			char ip6buf[INET6_ADDRSTRLEN];
37178064Sume			log(LOG_WARNING, "%s: packet from %s dropped "
372147256Sbrooks			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
373165118Sbz			    ip6_sprintf(ip6buf, &sin6.sin6_addr));
37478064Sume#endif
37578064Sume			if (rt)
37678064Sume				rtfree(rt);
37762587Sitojun			return 0;
37862587Sitojun		}
37962587Sitojun		rtfree(rt);
38062587Sitojun	}
38162587Sitojun
38278064Sume	return 128 * 2;
38362587Sitojun}
384105293Sume
385105293Sume/*
386105293Sume * we know that we are in IFF_UP, outer address available, and outer family
387105293Sume * matched the physical addr family.  see gif_encapcheck().
388105293Sume * sanity check for arg should have been done in the caller.
389105293Sume */
390105293Sumeint
391171259Sdelphijgif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
392105293Sume{
393105293Sume	struct ip6_hdr ip6;
394105293Sume	struct gif_softc *sc;
395105293Sume	struct ifnet *ifp;
396105293Sume
397105293Sume	/* sanity check done in caller */
398105293Sume	sc = (struct gif_softc *)arg;
399105293Sume
400105293Sume	/* LINTED const cast */
401105293Sume	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
402105293Sume	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
403105293Sume
404105293Sume	return gif_validate6(&ip6, sc, ifp);
405105293Sume}
406105293Sume
407105293Sumeint
408171259Sdelphijin6_gif_attach(struct gif_softc *sc)
409105293Sume{
410105293Sume	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
411155333Sume	    (void *)&in6_gif_protosw, sc);
412105293Sume	if (sc->encap_cookie6 == NULL)
413105293Sume		return EEXIST;
414105293Sume	return 0;
415105293Sume}
416105293Sume
417105293Sumeint
418171259Sdelphijin6_gif_detach(struct gif_softc *sc)
419105293Sume{
420105293Sume	int error;
421105293Sume
422105293Sume	error = encap_detach(sc->encap_cookie6);
423105293Sume	if (error == 0)
424105293Sume		sc->encap_cookie6 = NULL;
425105293Sume	return error;
426105293Sume}
427