xref: /freebsd/sys/netinet6/in6_gif.c (revision 93c2d7d5265c56c8734e70c19f838bd56190954a)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4cfa1ca9dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5b941bc1dSAndrey V. Elsukov  * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6cfa1ca9dSYoshinobu Inoue  * All rights reserved.
7cfa1ca9dSYoshinobu Inoue  *
8cfa1ca9dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
9cfa1ca9dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
10cfa1ca9dSYoshinobu Inoue  * are met:
11cfa1ca9dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
12cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
13cfa1ca9dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
14cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
15cfa1ca9dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
16cfa1ca9dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
17cfa1ca9dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
18cfa1ca9dSYoshinobu Inoue  *    without specific prior written permission.
19cfa1ca9dSYoshinobu Inoue  *
20cfa1ca9dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21cfa1ca9dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22cfa1ca9dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23cfa1ca9dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24cfa1ca9dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25cfa1ca9dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26cfa1ca9dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27cfa1ca9dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28cfa1ca9dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29cfa1ca9dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30cfa1ca9dSYoshinobu Inoue  * SUCH DAMAGE.
31b48287a3SDavid E. O'Brien  *
32b48287a3SDavid E. O'Brien  *	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
33cfa1ca9dSYoshinobu Inoue  */
34cfa1ca9dSYoshinobu Inoue 
35b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
36cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
37686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
38cfa1ca9dSYoshinobu Inoue 
39cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
40cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
41b941bc1dSAndrey V. Elsukov #include <sys/jail.h>
42cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
43cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
4682cea7e6SBjoern A. Zeeb #include <sys/kernel.h>
4733841545SHajimu UMEMOTO #include <sys/syslog.h>
4882cea7e6SBjoern A. Zeeb #include <sys/sysctl.h>
4933841545SHajimu UMEMOTO #include <sys/malloc.h>
506573d758SMatt Macy #include <sys/proc.h>
51cfa1ca9dSYoshinobu Inoue 
52b941bc1dSAndrey V. Elsukov #include <net/ethernet.h>
53cfa1ca9dSYoshinobu Inoue #include <net/if.h>
5476039bc8SGleb Smirnoff #include <net/if_var.h>
553d0d5b21SJustin Hibbits #include <net/if_private.h>
56cfa1ca9dSYoshinobu Inoue #include <net/route.h>
5776039bc8SGleb Smirnoff #include <net/vnet.h>
58cfa1ca9dSYoshinobu Inoue 
59cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
60cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
61cfa1ca9dSYoshinobu Inoue #ifdef INET
62cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
63b941bc1dSAndrey V. Elsukov #include <netinet/ip_ecn.h>
64cfa1ca9dSYoshinobu Inoue #endif
65686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
66686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
67cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
68686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
69b941bc1dSAndrey V. Elsukov #include <netinet6/scope6_var.h>
706a800098SYoshinobu Inoue #include <netinet6/ip6_ecn.h>
7165ff3638SAlexander V. Chernikov #include <netinet6/in6_fib.h>
72cfa1ca9dSYoshinobu Inoue 
73cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
74cfa1ca9dSYoshinobu Inoue 
75132c4490SAndrey V. Elsukov #define GIF_HLIM	30
765f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip6_gif_hlim) = GIF_HLIM;
7782cea7e6SBjoern A. Zeeb #define	V_ip6_gif_hlim			VNET(ip6_gif_hlim)
7882cea7e6SBjoern A. Zeeb 
7982cea7e6SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_ip6);
806d8fdfa9SAndrey V. Elsukov SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim,
816d8fdfa9SAndrey V. Elsukov     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_gif_hlim), 0,
826d8fdfa9SAndrey V. Elsukov     "Default hop limit for encapsulated packets");
839426aedfSHajimu UMEMOTO 
84b941bc1dSAndrey V. Elsukov /*
85b941bc1dSAndrey V. Elsukov  * We keep interfaces in a hash table using src+dst as key.
86b941bc1dSAndrey V. Elsukov  * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
87b941bc1dSAndrey V. Elsukov  */
885f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gif_list *, ipv6_hashtbl) = NULL;
89009d82eeSAndrey V. Elsukov VNET_DEFINE_STATIC(struct gif_list *, ipv6_srchashtbl) = NULL;
905f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gif_list, ipv6_list) = CK_LIST_HEAD_INITIALIZER();
91b941bc1dSAndrey V. Elsukov #define	V_ipv6_hashtbl		VNET(ipv6_hashtbl)
92009d82eeSAndrey V. Elsukov #define	V_ipv6_srchashtbl	VNET(ipv6_srchashtbl)
93b941bc1dSAndrey V. Elsukov #define	V_ipv6_list		VNET(ipv6_list)
94b941bc1dSAndrey V. Elsukov 
95b941bc1dSAndrey V. Elsukov #define	GIF_HASH(src, dst)	(V_ipv6_hashtbl[\
96b941bc1dSAndrey V. Elsukov     in6_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
97009d82eeSAndrey V. Elsukov #define	GIF_SRCHASH(src)	(V_ipv6_srchashtbl[\
98009d82eeSAndrey V. Elsukov     fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
99b941bc1dSAndrey V. Elsukov #define	GIF_HASH_SC(sc)		GIF_HASH(&(sc)->gif_ip6hdr->ip6_src,\
100b941bc1dSAndrey V. Elsukov     &(sc)->gif_ip6hdr->ip6_dst)
101b941bc1dSAndrey V. Elsukov static uint32_t
in6_gif_hashval(const struct in6_addr * src,const struct in6_addr * dst)102b941bc1dSAndrey V. Elsukov in6_gif_hashval(const struct in6_addr *src, const struct in6_addr *dst)
103b941bc1dSAndrey V. Elsukov {
104b941bc1dSAndrey V. Elsukov 	uint32_t ret;
105b941bc1dSAndrey V. Elsukov 
106b941bc1dSAndrey V. Elsukov 	ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
107b941bc1dSAndrey V. Elsukov 	return (fnv_32_buf(dst, sizeof(*dst), ret));
108b941bc1dSAndrey V. Elsukov }
109b941bc1dSAndrey V. Elsukov 
110b941bc1dSAndrey V. Elsukov static int
in6_gif_checkdup(const struct gif_softc * sc,const struct in6_addr * src,const struct in6_addr * dst)111b941bc1dSAndrey V. Elsukov in6_gif_checkdup(const struct gif_softc *sc, const struct in6_addr *src,
112b941bc1dSAndrey V. Elsukov     const struct in6_addr *dst)
113b941bc1dSAndrey V. Elsukov {
114b941bc1dSAndrey V. Elsukov 	struct gif_softc *tmp;
115b941bc1dSAndrey V. Elsukov 
116b941bc1dSAndrey V. Elsukov 	if (sc->gif_family == AF_INET6 &&
117b941bc1dSAndrey V. Elsukov 	    IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, src) &&
118b941bc1dSAndrey V. Elsukov 	    IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, dst))
119b941bc1dSAndrey V. Elsukov 		return (EEXIST);
120b941bc1dSAndrey V. Elsukov 
121b941bc1dSAndrey V. Elsukov 	CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
122b941bc1dSAndrey V. Elsukov 		if (tmp == sc)
123b941bc1dSAndrey V. Elsukov 			continue;
124b941bc1dSAndrey V. Elsukov 		if (IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_src, src) &&
125b941bc1dSAndrey V. Elsukov 		    IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_dst, dst))
126b941bc1dSAndrey V. Elsukov 			return (EADDRNOTAVAIL);
127b941bc1dSAndrey V. Elsukov 	}
128b941bc1dSAndrey V. Elsukov 	return (0);
129b941bc1dSAndrey V. Elsukov }
130b941bc1dSAndrey V. Elsukov 
131009d82eeSAndrey V. Elsukov /*
132009d82eeSAndrey V. Elsukov  * Check that ingress address belongs to local host.
133009d82eeSAndrey V. Elsukov  */
134009d82eeSAndrey V. Elsukov static void
in6_gif_set_running(struct gif_softc * sc)135009d82eeSAndrey V. Elsukov in6_gif_set_running(struct gif_softc *sc)
136009d82eeSAndrey V. Elsukov {
137009d82eeSAndrey V. Elsukov 
138009d82eeSAndrey V. Elsukov 	if (in6_localip(&sc->gif_ip6hdr->ip6_src))
139009d82eeSAndrey V. Elsukov 		GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
140009d82eeSAndrey V. Elsukov 	else
141009d82eeSAndrey V. Elsukov 		GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
142009d82eeSAndrey V. Elsukov }
143009d82eeSAndrey V. Elsukov 
144009d82eeSAndrey V. Elsukov /*
145009d82eeSAndrey V. Elsukov  * ifaddr_event handler.
146009d82eeSAndrey V. Elsukov  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
147009d82eeSAndrey V. Elsukov  * source address spoofing.
148009d82eeSAndrey V. Elsukov  */
149009d82eeSAndrey V. Elsukov static void
in6_gif_srcaddr(void * arg __unused,const struct sockaddr * sa,int event)150009d82eeSAndrey V. Elsukov in6_gif_srcaddr(void *arg __unused, const struct sockaddr *sa, int event)
151009d82eeSAndrey V. Elsukov {
152009d82eeSAndrey V. Elsukov 	const struct sockaddr_in6 *sin;
153009d82eeSAndrey V. Elsukov 	struct gif_softc *sc;
154009d82eeSAndrey V. Elsukov 
1558796e291SAndrey V. Elsukov 	/* Check that VNET is ready */
1568796e291SAndrey V. Elsukov 	if (V_ipv6_hashtbl == NULL)
157009d82eeSAndrey V. Elsukov 		return;
158009d82eeSAndrey V. Elsukov 
15997168be8SGleb Smirnoff 	NET_EPOCH_ASSERT();
160009d82eeSAndrey V. Elsukov 	sin = (const struct sockaddr_in6 *)sa;
161009d82eeSAndrey V. Elsukov 	CK_LIST_FOREACH(sc, &GIF_SRCHASH(&sin->sin6_addr), srchash) {
162009d82eeSAndrey V. Elsukov 		if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
163009d82eeSAndrey V. Elsukov 		    &sin->sin6_addr) == 0)
164009d82eeSAndrey V. Elsukov 			continue;
165009d82eeSAndrey V. Elsukov 		in6_gif_set_running(sc);
166009d82eeSAndrey V. Elsukov 	}
167009d82eeSAndrey V. Elsukov }
168009d82eeSAndrey V. Elsukov 
169b941bc1dSAndrey V. Elsukov static void
in6_gif_attach(struct gif_softc * sc)170b941bc1dSAndrey V. Elsukov in6_gif_attach(struct gif_softc *sc)
171b941bc1dSAndrey V. Elsukov {
172b941bc1dSAndrey V. Elsukov 
173b941bc1dSAndrey V. Elsukov 	if (sc->gif_options & GIF_IGNORE_SOURCE)
174b941bc1dSAndrey V. Elsukov 		CK_LIST_INSERT_HEAD(&V_ipv6_list, sc, chain);
175b941bc1dSAndrey V. Elsukov 	else
176b941bc1dSAndrey V. Elsukov 		CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
177009d82eeSAndrey V. Elsukov 
178009d82eeSAndrey V. Elsukov 	CK_LIST_INSERT_HEAD(&GIF_SRCHASH(&sc->gif_ip6hdr->ip6_src),
179009d82eeSAndrey V. Elsukov 	    sc, srchash);
180b941bc1dSAndrey V. Elsukov }
181b941bc1dSAndrey V. Elsukov 
182b941bc1dSAndrey V. Elsukov int
in6_gif_setopts(struct gif_softc * sc,u_int options)183b941bc1dSAndrey V. Elsukov in6_gif_setopts(struct gif_softc *sc, u_int options)
184b941bc1dSAndrey V. Elsukov {
185b941bc1dSAndrey V. Elsukov 
186b941bc1dSAndrey V. Elsukov 	/* NOTE: we are protected with gif_ioctl_sx lock */
187b941bc1dSAndrey V. Elsukov 	MPASS(sc->gif_family == AF_INET6);
188b941bc1dSAndrey V. Elsukov 	MPASS(sc->gif_options != options);
189b941bc1dSAndrey V. Elsukov 
190b941bc1dSAndrey V. Elsukov 	if ((options & GIF_IGNORE_SOURCE) !=
191b941bc1dSAndrey V. Elsukov 	    (sc->gif_options & GIF_IGNORE_SOURCE)) {
192009d82eeSAndrey V. Elsukov 		CK_LIST_REMOVE(sc, srchash);
193b941bc1dSAndrey V. Elsukov 		CK_LIST_REMOVE(sc, chain);
194b941bc1dSAndrey V. Elsukov 		sc->gif_options = options;
195b941bc1dSAndrey V. Elsukov 		in6_gif_attach(sc);
196b941bc1dSAndrey V. Elsukov 	}
197*93c2d7d5SKoichiro Iwao 
198*93c2d7d5SKoichiro Iwao 	if ((options & GIF_NOCLAMP) !=
199*93c2d7d5SKoichiro Iwao 	    (sc->gif_options & GIF_NOCLAMP)) {
200*93c2d7d5SKoichiro Iwao 		sc->gif_options = options;
201*93c2d7d5SKoichiro Iwao 	}
202b941bc1dSAndrey V. Elsukov 	return (0);
203b941bc1dSAndrey V. Elsukov }
204b941bc1dSAndrey V. Elsukov 
205b941bc1dSAndrey V. Elsukov int
in6_gif_ioctl(struct gif_softc * sc,u_long cmd,caddr_t data)206b941bc1dSAndrey V. Elsukov in6_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
207b941bc1dSAndrey V. Elsukov {
208b941bc1dSAndrey V. Elsukov 	struct in6_ifreq *ifr = (struct in6_ifreq *)data;
209b941bc1dSAndrey V. Elsukov 	struct sockaddr_in6 *dst, *src;
210b941bc1dSAndrey V. Elsukov 	struct ip6_hdr *ip6;
211b941bc1dSAndrey V. Elsukov 	int error;
212b941bc1dSAndrey V. Elsukov 
213b941bc1dSAndrey V. Elsukov 	/* NOTE: we are protected with gif_ioctl_sx lock */
214b941bc1dSAndrey V. Elsukov 	error = EINVAL;
215b941bc1dSAndrey V. Elsukov 	switch (cmd) {
216b941bc1dSAndrey V. Elsukov 	case SIOCSIFPHYADDR_IN6:
217b941bc1dSAndrey V. Elsukov 		src = &((struct in6_aliasreq *)data)->ifra_addr;
218b941bc1dSAndrey V. Elsukov 		dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
219b941bc1dSAndrey V. Elsukov 
220b941bc1dSAndrey V. Elsukov 		/* sanity checks */
221b941bc1dSAndrey V. Elsukov 		if (src->sin6_family != dst->sin6_family ||
222b941bc1dSAndrey V. Elsukov 		    src->sin6_family != AF_INET6 ||
223b941bc1dSAndrey V. Elsukov 		    src->sin6_len != dst->sin6_len ||
224b941bc1dSAndrey V. Elsukov 		    src->sin6_len != sizeof(*src))
225b941bc1dSAndrey V. Elsukov 			break;
226b941bc1dSAndrey V. Elsukov 		if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
227b941bc1dSAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
228b941bc1dSAndrey V. Elsukov 			error = EADDRNOTAVAIL;
229b941bc1dSAndrey V. Elsukov 			break;
230b941bc1dSAndrey V. Elsukov 		}
231b941bc1dSAndrey V. Elsukov 		/*
232b941bc1dSAndrey V. Elsukov 		 * Check validity of the scope zone ID of the
233b941bc1dSAndrey V. Elsukov 		 * addresses, and convert it into the kernel
234b941bc1dSAndrey V. Elsukov 		 * internal form if necessary.
235b941bc1dSAndrey V. Elsukov 		 */
236b941bc1dSAndrey V. Elsukov 		if ((error = sa6_embedscope(src, 0)) != 0 ||
237b941bc1dSAndrey V. Elsukov 		    (error = sa6_embedscope(dst, 0)) != 0)
238b941bc1dSAndrey V. Elsukov 			break;
239b941bc1dSAndrey V. Elsukov 
240009d82eeSAndrey V. Elsukov 		if (V_ipv6_hashtbl == NULL) {
241b941bc1dSAndrey V. Elsukov 			V_ipv6_hashtbl = gif_hashinit();
242009d82eeSAndrey V. Elsukov 			V_ipv6_srchashtbl = gif_hashinit();
243009d82eeSAndrey V. Elsukov 		}
244b941bc1dSAndrey V. Elsukov 		error = in6_gif_checkdup(sc, &src->sin6_addr,
245b941bc1dSAndrey V. Elsukov 		    &dst->sin6_addr);
246b941bc1dSAndrey V. Elsukov 		if (error == EADDRNOTAVAIL)
247b941bc1dSAndrey V. Elsukov 			break;
248b941bc1dSAndrey V. Elsukov 		if (error == EEXIST) {
249b941bc1dSAndrey V. Elsukov 			/* Addresses are the same. Just return. */
250b941bc1dSAndrey V. Elsukov 			error = 0;
251b941bc1dSAndrey V. Elsukov 			break;
252b941bc1dSAndrey V. Elsukov 		}
253b941bc1dSAndrey V. Elsukov 		ip6 = malloc(sizeof(*ip6), M_GIF, M_WAITOK | M_ZERO);
254b941bc1dSAndrey V. Elsukov 		ip6->ip6_src = src->sin6_addr;
255b941bc1dSAndrey V. Elsukov 		ip6->ip6_dst = dst->sin6_addr;
2568065bd0bSAndrey V. Elsukov 		ip6->ip6_vfc = IPV6_VERSION;
257b941bc1dSAndrey V. Elsukov 		if (sc->gif_family != 0) {
258b941bc1dSAndrey V. Elsukov 			/* Detach existing tunnel first */
259009d82eeSAndrey V. Elsukov 			CK_LIST_REMOVE(sc, srchash);
260b941bc1dSAndrey V. Elsukov 			CK_LIST_REMOVE(sc, chain);
261b941bc1dSAndrey V. Elsukov 			GIF_WAIT();
262b941bc1dSAndrey V. Elsukov 			free(sc->gif_hdr, M_GIF);
263b941bc1dSAndrey V. Elsukov 			/* XXX: should we notify about link state change? */
264b941bc1dSAndrey V. Elsukov 		}
265b941bc1dSAndrey V. Elsukov 		sc->gif_family = AF_INET6;
266b941bc1dSAndrey V. Elsukov 		sc->gif_ip6hdr = ip6;
267b941bc1dSAndrey V. Elsukov 		in6_gif_attach(sc);
268009d82eeSAndrey V. Elsukov 		in6_gif_set_running(sc);
269b941bc1dSAndrey V. Elsukov 		break;
270b941bc1dSAndrey V. Elsukov 	case SIOCGIFPSRCADDR_IN6:
271b941bc1dSAndrey V. Elsukov 	case SIOCGIFPDSTADDR_IN6:
272b941bc1dSAndrey V. Elsukov 		if (sc->gif_family != AF_INET6) {
273b941bc1dSAndrey V. Elsukov 			error = EADDRNOTAVAIL;
274b941bc1dSAndrey V. Elsukov 			break;
275b941bc1dSAndrey V. Elsukov 		}
276b941bc1dSAndrey V. Elsukov 		src = (struct sockaddr_in6 *)&ifr->ifr_addr;
277b941bc1dSAndrey V. Elsukov 		memset(src, 0, sizeof(*src));
278b941bc1dSAndrey V. Elsukov 		src->sin6_family = AF_INET6;
279b941bc1dSAndrey V. Elsukov 		src->sin6_len = sizeof(*src);
280b941bc1dSAndrey V. Elsukov 		src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
281b941bc1dSAndrey V. Elsukov 		    sc->gif_ip6hdr->ip6_src: sc->gif_ip6hdr->ip6_dst;
282b941bc1dSAndrey V. Elsukov 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
283b941bc1dSAndrey V. Elsukov 		if (error == 0)
284b941bc1dSAndrey V. Elsukov 			error = sa6_recoverscope(src);
285b941bc1dSAndrey V. Elsukov 		if (error != 0)
286b941bc1dSAndrey V. Elsukov 			memset(src, 0, sizeof(*src));
287b941bc1dSAndrey V. Elsukov 		break;
288b941bc1dSAndrey V. Elsukov 	}
289b941bc1dSAndrey V. Elsukov 	return (error);
290b941bc1dSAndrey V. Elsukov }
291b941bc1dSAndrey V. Elsukov 
292cfa1ca9dSYoshinobu Inoue int
in6_gif_output(struct ifnet * ifp,struct mbuf * m,int proto,uint8_t ecn)2930b9f5f8aSAndrey V. Elsukov in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
294cfa1ca9dSYoshinobu Inoue {
295fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
296cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr *ip6;
297*93c2d7d5SKoichiro Iwao 	u_long mtu;
298cfa1ca9dSYoshinobu Inoue 
299cfa1ca9dSYoshinobu Inoue 	/* prepend new IP header */
30097168be8SGleb Smirnoff 	NET_EPOCH_ASSERT();
301e82d7b29SMarius Strobl 	M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
3020b9f5f8aSAndrey V. Elsukov 	if (m == NULL)
3030b9f5f8aSAndrey V. Elsukov 		return (ENOBUFS);
304cfa1ca9dSYoshinobu Inoue 
305cfa1ca9dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
306b941bc1dSAndrey V. Elsukov 	MPASS(sc->gif_family == AF_INET6);
307e82d7b29SMarius Strobl 	memcpy(ip6, sc->gif_ip6hdr, sizeof(struct ip6_hdr));
3080b9f5f8aSAndrey V. Elsukov 
3090b9f5f8aSAndrey V. Elsukov 	ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
310cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt	= proto;
311603724d3SBjoern A. Zeeb 	ip6->ip6_hlim	= V_ip6_gif_hlim;
312686cdd19SJun-ichiro itojun Hagino 	/*
313*93c2d7d5SKoichiro Iwao 	 * Enforce fragmentation to minimum MTU, even if the interface MTU
314*93c2d7d5SKoichiro Iwao 	 * is larger, to avoid path MTU discovery when NOCLAMP is not
315*93c2d7d5SKoichiro Iwao 	 * set (default).  IPv6 does not allow fragmentation on intermediate
316*93c2d7d5SKoichiro Iwao 	 * router nodes, so it is too painful to ask for resend of inner
317*93c2d7d5SKoichiro Iwao 	 * packet, to achieve path MTU discovery for encapsulated packets.
318686cdd19SJun-ichiro itojun Hagino 	 */
319*93c2d7d5SKoichiro Iwao 	mtu = ((sc->gif_options & GIF_NOCLAMP) == 0) ? IPV6_MINMTU : 0;
320*93c2d7d5SKoichiro Iwao 
321*93c2d7d5SKoichiro Iwao 	return (ip6_output(m, 0, NULL, mtu, 0, NULL, NULL));
322cfa1ca9dSYoshinobu Inoue }
323cfa1ca9dSYoshinobu Inoue 
324132c4490SAndrey V. Elsukov static int
in6_gif_input(struct mbuf * m,int off,int proto,void * arg)3256d8fdfa9SAndrey V. Elsukov in6_gif_input(struct mbuf *m, int off, int proto, void *arg)
326cfa1ca9dSYoshinobu Inoue {
3276d8fdfa9SAndrey V. Elsukov 	struct gif_softc *sc = arg;
3280b9f5f8aSAndrey V. Elsukov 	struct ifnet *gifp;
329cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr *ip6;
3300b9f5f8aSAndrey V. Elsukov 	uint8_t ecn;
331cfa1ca9dSYoshinobu Inoue 
33297168be8SGleb Smirnoff 	NET_EPOCH_ASSERT();
333d098c2c1SHajimu UMEMOTO 	if (sc == NULL) {
334d098c2c1SHajimu UMEMOTO 		m_freem(m);
3359cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_nogif);
3360b9f5f8aSAndrey V. Elsukov 		return (IPPROTO_DONE);
337d098c2c1SHajimu UMEMOTO 	}
338d098c2c1SHajimu UMEMOTO 	gifp = GIF2IFP(sc);
3390b9f5f8aSAndrey V. Elsukov 	if ((gifp->if_flags & IFF_UP) != 0) {
340cfa1ca9dSYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
341bb4a7d94SKristof Provost 		ecn = IPV6_TRAFFIC_CLASS(ip6);
3426d8fdfa9SAndrey V. Elsukov 		m_adj(m, off);
3430b9f5f8aSAndrey V. Elsukov 		gif_input(m, gifp, proto, ecn);
3440b9f5f8aSAndrey V. Elsukov 	} else {
34559dfcba4SHajimu UMEMOTO 		m_freem(m);
3469cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_nogif);
347cfa1ca9dSYoshinobu Inoue 	}
3480b9f5f8aSAndrey V. Elsukov 	return (IPPROTO_DONE);
349cfa1ca9dSYoshinobu Inoue }
350686cdd19SJun-ichiro itojun Hagino 
351b941bc1dSAndrey V. Elsukov static int
in6_gif_lookup(const struct mbuf * m,int off,int proto,void ** arg)352b941bc1dSAndrey V. Elsukov in6_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
3539426aedfSHajimu UMEMOTO {
35410a0e0bfSAndrey V. Elsukov 	const struct ip6_hdr *ip6;
35510a0e0bfSAndrey V. Elsukov 	struct gif_softc *sc;
356c1b4f79dSAndrey V. Elsukov 	int ret;
357686cdd19SJun-ichiro itojun Hagino 
3586e081509SAndrey V. Elsukov 	if (V_ipv6_hashtbl == NULL)
3596e081509SAndrey V. Elsukov 		return (0);
3606e081509SAndrey V. Elsukov 
36197168be8SGleb Smirnoff 	NET_EPOCH_ASSERT();
3629426aedfSHajimu UMEMOTO 	/*
363b941bc1dSAndrey V. Elsukov 	 * NOTE: it is safe to iterate without any locking here, because softc
364b941bc1dSAndrey V. Elsukov 	 * can be reclaimed only when we are not within net_epoch_preempt
365b941bc1dSAndrey V. Elsukov 	 * section, but ip_encap lookup+input are executed in epoch section.
3669426aedfSHajimu UMEMOTO 	 */
36710a0e0bfSAndrey V. Elsukov 	ip6 = mtod(m, const struct ip6_hdr *);
368b941bc1dSAndrey V. Elsukov 	ret = 0;
369b941bc1dSAndrey V. Elsukov 	CK_LIST_FOREACH(sc, &GIF_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
370b941bc1dSAndrey V. Elsukov 		/*
371b941bc1dSAndrey V. Elsukov 		 * This is an inbound packet, its ip6_dst is source address
372b941bc1dSAndrey V. Elsukov 		 * in softc.
373b941bc1dSAndrey V. Elsukov 		 */
374b941bc1dSAndrey V. Elsukov 		if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
375b941bc1dSAndrey V. Elsukov 		    &ip6->ip6_dst) &&
376b941bc1dSAndrey V. Elsukov 		    IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst,
377b941bc1dSAndrey V. Elsukov 		    &ip6->ip6_src)) {
378b941bc1dSAndrey V. Elsukov 			ret = ENCAP_DRV_LOOKUP;
379b941bc1dSAndrey V. Elsukov 			goto done;
380b941bc1dSAndrey V. Elsukov 		}
381b941bc1dSAndrey V. Elsukov 	}
382b941bc1dSAndrey V. Elsukov 	/*
383b941bc1dSAndrey V. Elsukov 	 * No exact match.
384b941bc1dSAndrey V. Elsukov 	 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
385b941bc1dSAndrey V. Elsukov 	 */
386b941bc1dSAndrey V. Elsukov 	CK_LIST_FOREACH(sc, &V_ipv6_list, chain) {
387b941bc1dSAndrey V. Elsukov 		if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
388b941bc1dSAndrey V. Elsukov 		    &ip6->ip6_dst)) {
389b941bc1dSAndrey V. Elsukov 			ret = 128 + 8; /* src + proto */
390b941bc1dSAndrey V. Elsukov 			goto done;
391b941bc1dSAndrey V. Elsukov 		}
392b941bc1dSAndrey V. Elsukov 	}
3930b9f5f8aSAndrey V. Elsukov 	return (0);
394b941bc1dSAndrey V. Elsukov done:
395b941bc1dSAndrey V. Elsukov 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
396c1b4f79dSAndrey V. Elsukov 		return (0);
397686cdd19SJun-ichiro itojun Hagino 	/* ingress filters on outer source */
39810a0e0bfSAndrey V. Elsukov 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
3997bfc98afSAlexander V. Chernikov 		if (fib6_check_urpf(sc->gif_fibnum, &ip6->ip6_src,
4007bfc98afSAlexander V. Chernikov 		    ntohs(in6_getscope(&ip6->ip6_src)), NHR_NONE,
4017bfc98afSAlexander V. Chernikov 		    m->m_pkthdr.rcvif) == 0)
40265ff3638SAlexander V. Chernikov 			return (0);
403686cdd19SJun-ichiro itojun Hagino 	}
404b941bc1dSAndrey V. Elsukov 	*arg = sc;
405c1b4f79dSAndrey V. Elsukov 	return (ret);
406686cdd19SJun-ichiro itojun Hagino }
4079426aedfSHajimu UMEMOTO 
408009d82eeSAndrey V. Elsukov static const struct srcaddrtab *ipv6_srcaddrtab;
409b941bc1dSAndrey V. Elsukov static struct {
410b941bc1dSAndrey V. Elsukov 	const struct encap_config encap;
411b941bc1dSAndrey V. Elsukov 	const struct encaptab *cookie;
412b941bc1dSAndrey V. Elsukov } ipv6_encap_cfg[] = {
413b941bc1dSAndrey V. Elsukov #ifdef INET
414b941bc1dSAndrey V. Elsukov 	{
415b941bc1dSAndrey V. Elsukov 		.encap = {
416b941bc1dSAndrey V. Elsukov 			.proto = IPPROTO_IPV4,
417b941bc1dSAndrey V. Elsukov 			.min_length = sizeof(struct ip6_hdr) +
418b941bc1dSAndrey V. Elsukov 			    sizeof(struct ip),
419b941bc1dSAndrey V. Elsukov 			.exact_match = ENCAP_DRV_LOOKUP,
420b941bc1dSAndrey V. Elsukov 			.lookup = in6_gif_lookup,
4216d8fdfa9SAndrey V. Elsukov 			.input = in6_gif_input
422b941bc1dSAndrey V. Elsukov 		},
423b941bc1dSAndrey V. Elsukov 	},
424b941bc1dSAndrey V. Elsukov #endif
425b941bc1dSAndrey V. Elsukov 	{
426b941bc1dSAndrey V. Elsukov 		.encap = {
427b941bc1dSAndrey V. Elsukov 			.proto = IPPROTO_IPV6,
428b941bc1dSAndrey V. Elsukov 			.min_length = 2 * sizeof(struct ip6_hdr),
429b941bc1dSAndrey V. Elsukov 			.exact_match = ENCAP_DRV_LOOKUP,
430b941bc1dSAndrey V. Elsukov 			.lookup = in6_gif_lookup,
431b941bc1dSAndrey V. Elsukov 			.input = in6_gif_input
432b941bc1dSAndrey V. Elsukov 		},
433b941bc1dSAndrey V. Elsukov 	},
434b941bc1dSAndrey V. Elsukov 	{
435b941bc1dSAndrey V. Elsukov 		.encap = {
436b941bc1dSAndrey V. Elsukov 			.proto = IPPROTO_ETHERIP,
437b941bc1dSAndrey V. Elsukov 			.min_length = sizeof(struct ip6_hdr) +
438b941bc1dSAndrey V. Elsukov 			    sizeof(struct etherip_header) +
439b941bc1dSAndrey V. Elsukov 			    sizeof(struct ether_header),
440b941bc1dSAndrey V. Elsukov 			.exact_match = ENCAP_DRV_LOOKUP,
441b941bc1dSAndrey V. Elsukov 			.lookup = in6_gif_lookup,
442b941bc1dSAndrey V. Elsukov 			.input = in6_gif_input
443b941bc1dSAndrey V. Elsukov 		},
444b941bc1dSAndrey V. Elsukov 	}
4456d8fdfa9SAndrey V. Elsukov };
4466d8fdfa9SAndrey V. Elsukov 
447b941bc1dSAndrey V. Elsukov void
in6_gif_init(void)448b941bc1dSAndrey V. Elsukov in6_gif_init(void)
4499426aedfSHajimu UMEMOTO {
450b941bc1dSAndrey V. Elsukov 	int i;
4510b9f5f8aSAndrey V. Elsukov 
452b941bc1dSAndrey V. Elsukov 	if (!IS_DEFAULT_VNET(curvnet))
453b941bc1dSAndrey V. Elsukov 		return;
454009d82eeSAndrey V. Elsukov 
455009d82eeSAndrey V. Elsukov 	ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gif_srcaddr,
456009d82eeSAndrey V. Elsukov 	    NULL, M_WAITOK);
457b941bc1dSAndrey V. Elsukov 	for (i = 0; i < nitems(ipv6_encap_cfg); i++)
458b941bc1dSAndrey V. Elsukov 		ipv6_encap_cfg[i].cookie = ip6_encap_attach(
459b941bc1dSAndrey V. Elsukov 		    &ipv6_encap_cfg[i].encap, NULL, M_WAITOK);
460b941bc1dSAndrey V. Elsukov }
461b941bc1dSAndrey V. Elsukov 
462b941bc1dSAndrey V. Elsukov void
in6_gif_uninit(void)463b941bc1dSAndrey V. Elsukov in6_gif_uninit(void)
464b941bc1dSAndrey V. Elsukov {
465b941bc1dSAndrey V. Elsukov 	int i;
466b941bc1dSAndrey V. Elsukov 
467b941bc1dSAndrey V. Elsukov 	if (IS_DEFAULT_VNET(curvnet)) {
468b941bc1dSAndrey V. Elsukov 		for (i = 0; i < nitems(ipv6_encap_cfg); i++)
469b941bc1dSAndrey V. Elsukov 			ip6_encap_detach(ipv6_encap_cfg[i].cookie);
470009d82eeSAndrey V. Elsukov 		ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
471b941bc1dSAndrey V. Elsukov 	}
472009d82eeSAndrey V. Elsukov 	if (V_ipv6_hashtbl != NULL) {
473b941bc1dSAndrey V. Elsukov 		gif_hashdestroy(V_ipv6_hashtbl);
4748796e291SAndrey V. Elsukov 		V_ipv6_hashtbl = NULL;
4758796e291SAndrey V. Elsukov 		GIF_WAIT();
476009d82eeSAndrey V. Elsukov 		gif_hashdestroy(V_ipv6_srchashtbl);
477009d82eeSAndrey V. Elsukov 	}
4789426aedfSHajimu UMEMOTO }
479