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: releng/10.3/sys/netinet6/in6_gif.c 284072 2015-06-06 13:26:13Z ae $");
34174510Sobrien
3554263Sshin#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
3754263Sshin
3854263Sshin#include <sys/param.h>
39276149Sae#include <sys/lock.h>
40276149Sae#include <sys/rmlock.h>
4154263Sshin#include <sys/systm.h>
4254263Sshin#include <sys/socket.h>
4354263Sshin#include <sys/sockio.h>
4454263Sshin#include <sys/mbuf.h>
4554263Sshin#include <sys/errno.h>
46207369Sbz#include <sys/kernel.h>
4778064Sume#include <sys/queue.h>
4878064Sume#include <sys/syslog.h>
49207369Sbz#include <sys/sysctl.h>
50105293Sume#include <sys/protosw.h>
5178064Sume#include <sys/malloc.h>
5278064Sume
5354263Sshin#include <net/if.h>
54276149Sae#include <net/if_var.h>
5554263Sshin#include <net/route.h>
56276149Sae#include <net/vnet.h>
5754263Sshin
5854263Sshin#include <netinet/in.h>
5954263Sshin#include <netinet/in_systm.h>
6054263Sshin#ifdef INET
6154263Sshin#include <netinet/ip.h>
6254263Sshin#endif
6362587Sitojun#include <netinet/ip_encap.h>
6462587Sitojun#ifdef INET6
6562587Sitojun#include <netinet/ip6.h>
6654263Sshin#include <netinet6/ip6_var.h>
6762587Sitojun#include <netinet6/in6_var.h>
6862587Sitojun#endif
69105293Sume#include <netinet6/ip6protosw.h>
7055009Sshin#include <netinet/ip_ecn.h>
7162587Sitojun#ifdef INET6
7255009Sshin#include <netinet6/ip6_ecn.h>
7362587Sitojun#endif
7454263Sshin
7554263Sshin#include <net/if_gif.h>
7654263Sshin
77284072Sae#define GIF_HLIM	30
78284072Saestatic VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
79207369Sbz#define	V_ip6_gif_hlim			VNET(ip6_gif_hlim)
80207369Sbz
81207369SbzSYSCTL_DECL(_net_inet6_ip6);
82207369SbzSYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_RW,
83207369Sbz    &VNET_NAME(ip6_gif_hlim), 0, "");
84207369Sbz
85105293Sumestatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
86105293Sume			 struct ifnet *);
87284072Saestatic int in6_gif_input(struct mbuf **, int *, int);
88105293Sume
89105293Sumeextern  struct domain inet6domain;
90284072Saestatic struct ip6protosw in6_gif_protosw = {
91186791Sbz	.pr_type =	SOCK_RAW,
92186791Sbz	.pr_domain =	&inet6domain,
93186791Sbz	.pr_protocol =	0,			/* IPPROTO_IPV[46] */
94186791Sbz	.pr_flags =	PR_ATOMIC|PR_ADDR,
95186791Sbz	.pr_input =	in6_gif_input,
96186791Sbz	.pr_output =	rip6_output,
97186791Sbz	.pr_ctloutput =	rip6_ctloutput,
98186791Sbz	.pr_usrreqs =	&rip6_usrreqs
99105293Sume};
100105293Sume
10154263Sshinint
102276149Saein6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
10354263Sshin{
104276149Sae	GIF_RLOCK_TRACKER;
105147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
10654263Sshin	struct ip6_hdr *ip6;
107276149Sae	int len;
10854263Sshin
10954263Sshin	/* prepend new IP header */
110189494Smarius	len = sizeof(struct ip6_hdr);
111189494Smarius#ifndef __NO_STRICT_ALIGNMENT
112276149Sae	if (proto == IPPROTO_ETHERIP)
113189494Smarius		len += ETHERIP_ALIGN;
114189494Smarius#endif
115243882Sglebius	M_PREPEND(m, len, M_NOWAIT);
116276149Sae	if (m == NULL)
117276149Sae		return (ENOBUFS);
118189494Smarius#ifndef __NO_STRICT_ALIGNMENT
119276149Sae	if (proto == IPPROTO_ETHERIP) {
120189494Smarius		len = mtod(m, vm_offset_t) & 3;
121189494Smarius		KASSERT(len == 0 || len == ETHERIP_ALIGN,
122189494Smarius		    ("in6_gif_output: unexpected misalignment"));
123189494Smarius		m->m_data += len;
124189494Smarius		m->m_len -= ETHERIP_ALIGN;
125189494Smarius	}
126189494Smarius#endif
12754263Sshin
12854263Sshin	ip6 = mtod(m, struct ip6_hdr *);
129276149Sae	GIF_RLOCK(sc);
130276149Sae	if (sc->gif_family != AF_INET6) {
13178064Sume		m_freem(m);
132276149Sae		GIF_RUNLOCK(sc);
133276149Sae		return (ENETDOWN);
13454263Sshin	}
135276149Sae	bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
136276149Sae	GIF_RUNLOCK(sc);
13754263Sshin
138276149Sae	ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
139276149Sae	ip6->ip6_nxt	= proto;
140276149Sae	ip6->ip6_hlim	= V_ip6_gif_hlim;
14162587Sitojun	/*
14262587Sitojun	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
14362587Sitojun	 * it is too painful to ask for resend of inner packet, to achieve
14462587Sitojun	 * path MTU discovery for encapsulated packets.
14562587Sitojun	 */
146276149Sae	return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
14754263Sshin}
14854263Sshin
149284072Saestatic int
150171259Sdelphijin6_gif_input(struct mbuf **mp, int *offp, int proto)
15154263Sshin{
15254263Sshin	struct mbuf *m = *mp;
153276149Sae	struct ifnet *gifp;
154147507Sume	struct gif_softc *sc;
15554263Sshin	struct ip6_hdr *ip6;
156276149Sae	uint8_t ecn;
15754263Sshin
158276149Sae	sc = encap_getarg(m);
159147507Sume	if (sc == NULL) {
160147507Sume		m_freem(m);
161249294Sae		IP6STAT_INC(ip6s_nogif);
162276149Sae		return (IPPROTO_DONE);
163147507Sume	}
164147507Sume	gifp = GIF2IFP(sc);
165276149Sae	if ((gifp->if_flags & IFF_UP) != 0) {
166276149Sae		ip6 = mtod(m, struct ip6_hdr *);
167276149Sae		ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
168276149Sae		m_adj(m, *offp);
169276149Sae		gif_input(m, gifp, proto, ecn);
170276149Sae	} else {
17154263Sshin		m_freem(m);
172249294Sae		IP6STAT_INC(ip6s_nogif);
17354263Sshin	}
174276149Sae	return (IPPROTO_DONE);
17554263Sshin}
17662587Sitojun
17762587Sitojun/*
178105293Sume * validate outer address.
17962587Sitojun */
180105293Sumestatic int
181171259Sdelphijgif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
182171259Sdelphij    struct ifnet *ifp)
18362587Sitojun{
184283852Sae	int ret;
18562587Sitojun
186276149Sae	GIF_RLOCK_ASSERT(sc);
187105293Sume	/*
188105293Sume	 * Check for address match.  Note that the check is for an incoming
189105293Sume	 * packet.  We should compare the *source* address in our configuration
190105293Sume	 * and the *destination* address of the packet, and vice versa.
191105293Sume	 */
192283852Sae	if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst))
193276149Sae		return (0);
194283852Sae	ret = 128;
195283852Sae	if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src)) {
196283852Sae		if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
197283852Sae			return (0);
198283852Sae	} else
199283852Sae		ret += 128;
20062587Sitojun
20162587Sitojun	/* martian filters on outer source - done in ip6_input */
20262587Sitojun
20362587Sitojun	/* ingress filters on outer source */
204147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
20562587Sitojun		struct sockaddr_in6 sin6;
20662587Sitojun		struct rtentry *rt;
20762587Sitojun
20862587Sitojun		bzero(&sin6, sizeof(sin6));
20962587Sitojun		sin6.sin6_family = AF_INET6;
21062587Sitojun		sin6.sin6_len = sizeof(struct sockaddr_in6);
211105293Sume		sin6.sin6_addr = ip6->ip6_src;
212105293Sume		sin6.sin6_scope_id = 0; /* XXX */
213105293Sume
214231852Sbz		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
215231852Sbz		    sc->gif_fibnum);
216105293Sume		if (!rt || rt->rt_ifp != ifp) {
21778064Sume			if (rt)
218187946Sbz				RTFREE_LOCKED(rt);
219276149Sae			return (0);
22062587Sitojun		}
221187946Sbz		RTFREE_LOCKED(rt);
22262587Sitojun	}
22362587Sitojun
224283852Sae	return (ret);
22562587Sitojun}
226105293Sume
227105293Sume/*
228105293Sume * we know that we are in IFF_UP, outer address available, and outer family
229105293Sume * matched the physical addr family.  see gif_encapcheck().
230105293Sume */
231105293Sumeint
232276149Saein6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
233105293Sume{
234105293Sume	struct ip6_hdr ip6;
235105293Sume	struct gif_softc *sc;
236105293Sume	struct ifnet *ifp;
237105293Sume
238105293Sume	/* sanity check done in caller */
239105293Sume	sc = (struct gif_softc *)arg;
240276149Sae	GIF_RLOCK_ASSERT(sc);
241105293Sume
242105293Sume	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
243105293Sume	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
244276149Sae	return (gif_validate6(&ip6, sc, ifp));
245105293Sume}
246105293Sume
247105293Sumeint
248171259Sdelphijin6_gif_attach(struct gif_softc *sc)
249105293Sume{
250276149Sae
251276149Sae	KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
252276149Sae	sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
253155333Sume	    (void *)&in6_gif_protosw, sc);
254276149Sae	if (sc->gif_ecookie == NULL)
255276149Sae		return (EEXIST);
256276149Sae	return (0);
257105293Sume}
258