in6_gif.c revision 283852
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: stable/10/sys/netinet6/in6_gif.c 283852 2015-05-31 22:58:41Z 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>
6754263Sshin#include <netinet6/in6_gif.h>
6862587Sitojun#include <netinet6/in6_var.h>
6962587Sitojun#endif
70105293Sume#include <netinet6/ip6protosw.h>
7155009Sshin#include <netinet/ip_ecn.h>
7262587Sitojun#ifdef INET6
7355009Sshin#include <netinet6/ip6_ecn.h>
7462587Sitojun#endif
7554263Sshin
7654263Sshin#include <net/if_gif.h>
7754263Sshin
78207369SbzVNET_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 *);
87105293Sume
88105293Sumeextern  struct domain inet6domain;
89186791Sbzstruct ip6protosw in6_gif_protosw = {
90186791Sbz	.pr_type =	SOCK_RAW,
91186791Sbz	.pr_domain =	&inet6domain,
92186791Sbz	.pr_protocol =	0,			/* IPPROTO_IPV[46] */
93186791Sbz	.pr_flags =	PR_ATOMIC|PR_ADDR,
94186791Sbz	.pr_input =	in6_gif_input,
95186791Sbz	.pr_output =	rip6_output,
96186791Sbz	.pr_ctloutput =	rip6_ctloutput,
97186791Sbz	.pr_usrreqs =	&rip6_usrreqs
98105293Sume};
99105293Sume
10054263Sshinint
101276149Saein6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
10254263Sshin{
103276149Sae	GIF_RLOCK_TRACKER;
104147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
10554263Sshin	struct ip6_hdr *ip6;
106276149Sae	int len;
10754263Sshin
10854263Sshin	/* prepend new IP header */
109189494Smarius	len = sizeof(struct ip6_hdr);
110189494Smarius#ifndef __NO_STRICT_ALIGNMENT
111276149Sae	if (proto == IPPROTO_ETHERIP)
112189494Smarius		len += ETHERIP_ALIGN;
113189494Smarius#endif
114243882Sglebius	M_PREPEND(m, len, M_NOWAIT);
115276149Sae	if (m == NULL)
116276149Sae		return (ENOBUFS);
117189494Smarius#ifndef __NO_STRICT_ALIGNMENT
118276149Sae	if (proto == IPPROTO_ETHERIP) {
119189494Smarius		len = mtod(m, vm_offset_t) & 3;
120189494Smarius		KASSERT(len == 0 || len == ETHERIP_ALIGN,
121189494Smarius		    ("in6_gif_output: unexpected misalignment"));
122189494Smarius		m->m_data += len;
123189494Smarius		m->m_len -= ETHERIP_ALIGN;
124189494Smarius	}
125189494Smarius#endif
12654263Sshin
12754263Sshin	ip6 = mtod(m, struct ip6_hdr *);
128276149Sae	GIF_RLOCK(sc);
129276149Sae	if (sc->gif_family != AF_INET6) {
13078064Sume		m_freem(m);
131276149Sae		GIF_RUNLOCK(sc);
132276149Sae		return (ENETDOWN);
13354263Sshin	}
134276149Sae	bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
135276149Sae	GIF_RUNLOCK(sc);
13654263Sshin
137276149Sae	ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
138276149Sae	ip6->ip6_nxt	= proto;
139276149Sae	ip6->ip6_hlim	= V_ip6_gif_hlim;
14062587Sitojun	/*
14162587Sitojun	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
14262587Sitojun	 * it is too painful to ask for resend of inner packet, to achieve
14362587Sitojun	 * path MTU discovery for encapsulated packets.
14462587Sitojun	 */
145276149Sae	return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
14654263Sshin}
14754263Sshin
148120913Sumeint
149171259Sdelphijin6_gif_input(struct mbuf **mp, int *offp, int proto)
15054263Sshin{
15154263Sshin	struct mbuf *m = *mp;
152276149Sae	struct ifnet *gifp;
153147507Sume	struct gif_softc *sc;
15454263Sshin	struct ip6_hdr *ip6;
155276149Sae	uint8_t ecn;
15654263Sshin
157276149Sae	sc = encap_getarg(m);
158147507Sume	if (sc == NULL) {
159147507Sume		m_freem(m);
160249294Sae		IP6STAT_INC(ip6s_nogif);
161276149Sae		return (IPPROTO_DONE);
162147507Sume	}
163147507Sume	gifp = GIF2IFP(sc);
164276149Sae	if ((gifp->if_flags & IFF_UP) != 0) {
165276149Sae		ip6 = mtod(m, struct ip6_hdr *);
166276149Sae		ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
167276149Sae		m_adj(m, *offp);
168276149Sae		gif_input(m, gifp, proto, ecn);
169276149Sae	} else {
17054263Sshin		m_freem(m);
171249294Sae		IP6STAT_INC(ip6s_nogif);
17254263Sshin	}
173276149Sae	return (IPPROTO_DONE);
17454263Sshin}
17562587Sitojun
17662587Sitojun/*
177105293Sume * validate outer address.
17862587Sitojun */
179105293Sumestatic int
180171259Sdelphijgif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
181171259Sdelphij    struct ifnet *ifp)
18262587Sitojun{
183283852Sae	int ret;
18462587Sitojun
185276149Sae	GIF_RLOCK_ASSERT(sc);
186105293Sume	/*
187105293Sume	 * Check for address match.  Note that the check is for an incoming
188105293Sume	 * packet.  We should compare the *source* address in our configuration
189105293Sume	 * and the *destination* address of the packet, and vice versa.
190105293Sume	 */
191283852Sae	if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst))
192276149Sae		return (0);
193283852Sae	ret = 128;
194283852Sae	if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src)) {
195283852Sae		if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
196283852Sae			return (0);
197283852Sae	} else
198283852Sae		ret += 128;
19962587Sitojun
20062587Sitojun	/* martian filters on outer source - done in ip6_input */
20162587Sitojun
20262587Sitojun	/* ingress filters on outer source */
203147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
20462587Sitojun		struct sockaddr_in6 sin6;
20562587Sitojun		struct rtentry *rt;
20662587Sitojun
20762587Sitojun		bzero(&sin6, sizeof(sin6));
20862587Sitojun		sin6.sin6_family = AF_INET6;
20962587Sitojun		sin6.sin6_len = sizeof(struct sockaddr_in6);
210105293Sume		sin6.sin6_addr = ip6->ip6_src;
211105293Sume		sin6.sin6_scope_id = 0; /* XXX */
212105293Sume
213231852Sbz		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
214231852Sbz		    sc->gif_fibnum);
215105293Sume		if (!rt || rt->rt_ifp != ifp) {
21678064Sume			if (rt)
217187946Sbz				RTFREE_LOCKED(rt);
218276149Sae			return (0);
21962587Sitojun		}
220187946Sbz		RTFREE_LOCKED(rt);
22162587Sitojun	}
22262587Sitojun
223283852Sae	return (ret);
22462587Sitojun}
225105293Sume
226105293Sume/*
227105293Sume * we know that we are in IFF_UP, outer address available, and outer family
228105293Sume * matched the physical addr family.  see gif_encapcheck().
229105293Sume */
230105293Sumeint
231276149Saein6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
232105293Sume{
233105293Sume	struct ip6_hdr ip6;
234105293Sume	struct gif_softc *sc;
235105293Sume	struct ifnet *ifp;
236105293Sume
237105293Sume	/* sanity check done in caller */
238105293Sume	sc = (struct gif_softc *)arg;
239276149Sae	GIF_RLOCK_ASSERT(sc);
240105293Sume
241105293Sume	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
242105293Sume	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
243276149Sae	return (gif_validate6(&ip6, sc, ifp));
244105293Sume}
245105293Sume
246105293Sumeint
247171259Sdelphijin6_gif_attach(struct gif_softc *sc)
248105293Sume{
249276149Sae
250276149Sae	KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
251276149Sae	sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
252155333Sume	    (void *)&in6_gif_protosw, sc);
253276149Sae	if (sc->gif_ecookie == NULL)
254276149Sae		return (EEXIST);
255276149Sae	return (0);
256105293Sume}
257