xref: /freebsd/sys/netinet6/in6_gif.c (revision 2a58b312)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/jail.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 
54 #include <net/ethernet.h>
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_private.h>
58 #include <net/route.h>
59 #include <net/vnet.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #ifdef INET
64 #include <netinet/ip.h>
65 #include <netinet/ip_ecn.h>
66 #endif
67 #include <netinet/ip_encap.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/scope6_var.h>
72 #include <netinet6/ip6_ecn.h>
73 #include <netinet6/in6_fib.h>
74 
75 #include <net/if_gif.h>
76 
77 #define GIF_HLIM	30
78 VNET_DEFINE_STATIC(int, ip6_gif_hlim) = GIF_HLIM;
79 #define	V_ip6_gif_hlim			VNET(ip6_gif_hlim)
80 
81 SYSCTL_DECL(_net_inet6_ip6);
82 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim,
83     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_gif_hlim), 0,
84     "Default hop limit for encapsulated packets");
85 
86 /*
87  * We keep interfaces in a hash table using src+dst as key.
88  * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
89  */
90 VNET_DEFINE_STATIC(struct gif_list *, ipv6_hashtbl) = NULL;
91 VNET_DEFINE_STATIC(struct gif_list *, ipv6_srchashtbl) = NULL;
92 VNET_DEFINE_STATIC(struct gif_list, ipv6_list) = CK_LIST_HEAD_INITIALIZER();
93 #define	V_ipv6_hashtbl		VNET(ipv6_hashtbl)
94 #define	V_ipv6_srchashtbl	VNET(ipv6_srchashtbl)
95 #define	V_ipv6_list		VNET(ipv6_list)
96 
97 #define	GIF_HASH(src, dst)	(V_ipv6_hashtbl[\
98     in6_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
99 #define	GIF_SRCHASH(src)	(V_ipv6_srchashtbl[\
100     fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
101 #define	GIF_HASH_SC(sc)		GIF_HASH(&(sc)->gif_ip6hdr->ip6_src,\
102     &(sc)->gif_ip6hdr->ip6_dst)
103 static uint32_t
104 in6_gif_hashval(const struct in6_addr *src, const struct in6_addr *dst)
105 {
106 	uint32_t ret;
107 
108 	ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
109 	return (fnv_32_buf(dst, sizeof(*dst), ret));
110 }
111 
112 static int
113 in6_gif_checkdup(const struct gif_softc *sc, const struct in6_addr *src,
114     const struct in6_addr *dst)
115 {
116 	struct gif_softc *tmp;
117 
118 	if (sc->gif_family == AF_INET6 &&
119 	    IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, src) &&
120 	    IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, dst))
121 		return (EEXIST);
122 
123 	CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
124 		if (tmp == sc)
125 			continue;
126 		if (IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_src, src) &&
127 		    IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_dst, dst))
128 			return (EADDRNOTAVAIL);
129 	}
130 	return (0);
131 }
132 
133 /*
134  * Check that ingress address belongs to local host.
135  */
136 static void
137 in6_gif_set_running(struct gif_softc *sc)
138 {
139 
140 	if (in6_localip(&sc->gif_ip6hdr->ip6_src))
141 		GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
142 	else
143 		GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
144 }
145 
146 /*
147  * ifaddr_event handler.
148  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
149  * source address spoofing.
150  */
151 static void
152 in6_gif_srcaddr(void *arg __unused, const struct sockaddr *sa, int event)
153 {
154 	const struct sockaddr_in6 *sin;
155 	struct gif_softc *sc;
156 
157 	/* Check that VNET is ready */
158 	if (V_ipv6_hashtbl == NULL)
159 		return;
160 
161 	NET_EPOCH_ASSERT();
162 	sin = (const struct sockaddr_in6 *)sa;
163 	CK_LIST_FOREACH(sc, &GIF_SRCHASH(&sin->sin6_addr), srchash) {
164 		if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
165 		    &sin->sin6_addr) == 0)
166 			continue;
167 		in6_gif_set_running(sc);
168 	}
169 }
170 
171 static void
172 in6_gif_attach(struct gif_softc *sc)
173 {
174 
175 	if (sc->gif_options & GIF_IGNORE_SOURCE)
176 		CK_LIST_INSERT_HEAD(&V_ipv6_list, sc, chain);
177 	else
178 		CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
179 
180 	CK_LIST_INSERT_HEAD(&GIF_SRCHASH(&sc->gif_ip6hdr->ip6_src),
181 	    sc, srchash);
182 }
183 
184 int
185 in6_gif_setopts(struct gif_softc *sc, u_int options)
186 {
187 
188 	/* NOTE: we are protected with gif_ioctl_sx lock */
189 	MPASS(sc->gif_family == AF_INET6);
190 	MPASS(sc->gif_options != options);
191 
192 	if ((options & GIF_IGNORE_SOURCE) !=
193 	    (sc->gif_options & GIF_IGNORE_SOURCE)) {
194 		CK_LIST_REMOVE(sc, srchash);
195 		CK_LIST_REMOVE(sc, chain);
196 		sc->gif_options = options;
197 		in6_gif_attach(sc);
198 	}
199 	return (0);
200 }
201 
202 int
203 in6_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
204 {
205 	struct in6_ifreq *ifr = (struct in6_ifreq *)data;
206 	struct sockaddr_in6 *dst, *src;
207 	struct ip6_hdr *ip6;
208 	int error;
209 
210 	/* NOTE: we are protected with gif_ioctl_sx lock */
211 	error = EINVAL;
212 	switch (cmd) {
213 	case SIOCSIFPHYADDR_IN6:
214 		src = &((struct in6_aliasreq *)data)->ifra_addr;
215 		dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
216 
217 		/* sanity checks */
218 		if (src->sin6_family != dst->sin6_family ||
219 		    src->sin6_family != AF_INET6 ||
220 		    src->sin6_len != dst->sin6_len ||
221 		    src->sin6_len != sizeof(*src))
222 			break;
223 		if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
224 		    IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
225 			error = EADDRNOTAVAIL;
226 			break;
227 		}
228 		/*
229 		 * Check validity of the scope zone ID of the
230 		 * addresses, and convert it into the kernel
231 		 * internal form if necessary.
232 		 */
233 		if ((error = sa6_embedscope(src, 0)) != 0 ||
234 		    (error = sa6_embedscope(dst, 0)) != 0)
235 			break;
236 
237 		if (V_ipv6_hashtbl == NULL) {
238 			V_ipv6_hashtbl = gif_hashinit();
239 			V_ipv6_srchashtbl = gif_hashinit();
240 		}
241 		error = in6_gif_checkdup(sc, &src->sin6_addr,
242 		    &dst->sin6_addr);
243 		if (error == EADDRNOTAVAIL)
244 			break;
245 		if (error == EEXIST) {
246 			/* Addresses are the same. Just return. */
247 			error = 0;
248 			break;
249 		}
250 		ip6 = malloc(sizeof(*ip6), M_GIF, M_WAITOK | M_ZERO);
251 		ip6->ip6_src = src->sin6_addr;
252 		ip6->ip6_dst = dst->sin6_addr;
253 		ip6->ip6_vfc = IPV6_VERSION;
254 		if (sc->gif_family != 0) {
255 			/* Detach existing tunnel first */
256 			CK_LIST_REMOVE(sc, srchash);
257 			CK_LIST_REMOVE(sc, chain);
258 			GIF_WAIT();
259 			free(sc->gif_hdr, M_GIF);
260 			/* XXX: should we notify about link state change? */
261 		}
262 		sc->gif_family = AF_INET6;
263 		sc->gif_ip6hdr = ip6;
264 		in6_gif_attach(sc);
265 		in6_gif_set_running(sc);
266 		break;
267 	case SIOCGIFPSRCADDR_IN6:
268 	case SIOCGIFPDSTADDR_IN6:
269 		if (sc->gif_family != AF_INET6) {
270 			error = EADDRNOTAVAIL;
271 			break;
272 		}
273 		src = (struct sockaddr_in6 *)&ifr->ifr_addr;
274 		memset(src, 0, sizeof(*src));
275 		src->sin6_family = AF_INET6;
276 		src->sin6_len = sizeof(*src);
277 		src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
278 		    sc->gif_ip6hdr->ip6_src: sc->gif_ip6hdr->ip6_dst;
279 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
280 		if (error == 0)
281 			error = sa6_recoverscope(src);
282 		if (error != 0)
283 			memset(src, 0, sizeof(*src));
284 		break;
285 	}
286 	return (error);
287 }
288 
289 int
290 in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
291 {
292 	struct gif_softc *sc = ifp->if_softc;
293 	struct ip6_hdr *ip6;
294 	int len;
295 
296 	/* prepend new IP header */
297 	NET_EPOCH_ASSERT();
298 	len = sizeof(struct ip6_hdr);
299 #ifndef __NO_STRICT_ALIGNMENT
300 	if (proto == IPPROTO_ETHERIP)
301 		len += ETHERIP_ALIGN;
302 #endif
303 	M_PREPEND(m, len, M_NOWAIT);
304 	if (m == NULL)
305 		return (ENOBUFS);
306 #ifndef __NO_STRICT_ALIGNMENT
307 	if (proto == IPPROTO_ETHERIP) {
308 		len = mtod(m, vm_offset_t) & 3;
309 		KASSERT(len == 0 || len == ETHERIP_ALIGN,
310 		    ("in6_gif_output: unexpected misalignment"));
311 		m->m_data += len;
312 		m->m_len -= ETHERIP_ALIGN;
313 	}
314 #endif
315 
316 	ip6 = mtod(m, struct ip6_hdr *);
317 	MPASS(sc->gif_family == AF_INET6);
318 	bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
319 
320 	ip6->ip6_flow  |= htonl((uint32_t)ecn << 20);
321 	ip6->ip6_nxt	= proto;
322 	ip6->ip6_hlim	= V_ip6_gif_hlim;
323 	/*
324 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
325 	 * it is too painful to ask for resend of inner packet, to achieve
326 	 * path MTU discovery for encapsulated packets.
327 	 */
328 	return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
329 }
330 
331 static int
332 in6_gif_input(struct mbuf *m, int off, int proto, void *arg)
333 {
334 	struct gif_softc *sc = arg;
335 	struct ifnet *gifp;
336 	struct ip6_hdr *ip6;
337 	uint8_t ecn;
338 
339 	NET_EPOCH_ASSERT();
340 	if (sc == NULL) {
341 		m_freem(m);
342 		IP6STAT_INC(ip6s_nogif);
343 		return (IPPROTO_DONE);
344 	}
345 	gifp = GIF2IFP(sc);
346 	if ((gifp->if_flags & IFF_UP) != 0) {
347 		ip6 = mtod(m, struct ip6_hdr *);
348 		ecn = IPV6_TRAFFIC_CLASS(ip6);
349 		m_adj(m, off);
350 		gif_input(m, gifp, proto, ecn);
351 	} else {
352 		m_freem(m);
353 		IP6STAT_INC(ip6s_nogif);
354 	}
355 	return (IPPROTO_DONE);
356 }
357 
358 static int
359 in6_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
360 {
361 	const struct ip6_hdr *ip6;
362 	struct gif_softc *sc;
363 	int ret;
364 
365 	if (V_ipv6_hashtbl == NULL)
366 		return (0);
367 
368 	NET_EPOCH_ASSERT();
369 	/*
370 	 * NOTE: it is safe to iterate without any locking here, because softc
371 	 * can be reclaimed only when we are not within net_epoch_preempt
372 	 * section, but ip_encap lookup+input are executed in epoch section.
373 	 */
374 	ip6 = mtod(m, const struct ip6_hdr *);
375 	ret = 0;
376 	CK_LIST_FOREACH(sc, &GIF_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
377 		/*
378 		 * This is an inbound packet, its ip6_dst is source address
379 		 * in softc.
380 		 */
381 		if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
382 		    &ip6->ip6_dst) &&
383 		    IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst,
384 		    &ip6->ip6_src)) {
385 			ret = ENCAP_DRV_LOOKUP;
386 			goto done;
387 		}
388 	}
389 	/*
390 	 * No exact match.
391 	 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
392 	 */
393 	CK_LIST_FOREACH(sc, &V_ipv6_list, chain) {
394 		if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
395 		    &ip6->ip6_dst)) {
396 			ret = 128 + 8; /* src + proto */
397 			goto done;
398 		}
399 	}
400 	return (0);
401 done:
402 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
403 		return (0);
404 	/* ingress filters on outer source */
405 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
406 		if (fib6_check_urpf(sc->gif_fibnum, &ip6->ip6_src,
407 		    ntohs(in6_getscope(&ip6->ip6_src)), NHR_NONE,
408 		    m->m_pkthdr.rcvif) == 0)
409 			return (0);
410 	}
411 	*arg = sc;
412 	return (ret);
413 }
414 
415 static const struct srcaddrtab *ipv6_srcaddrtab;
416 static struct {
417 	const struct encap_config encap;
418 	const struct encaptab *cookie;
419 } ipv6_encap_cfg[] = {
420 #ifdef INET
421 	{
422 		.encap = {
423 			.proto = IPPROTO_IPV4,
424 			.min_length = sizeof(struct ip6_hdr) +
425 			    sizeof(struct ip),
426 			.exact_match = ENCAP_DRV_LOOKUP,
427 			.lookup = in6_gif_lookup,
428 			.input = in6_gif_input
429 		},
430 	},
431 #endif
432 	{
433 		.encap = {
434 			.proto = IPPROTO_IPV6,
435 			.min_length = 2 * sizeof(struct ip6_hdr),
436 			.exact_match = ENCAP_DRV_LOOKUP,
437 			.lookup = in6_gif_lookup,
438 			.input = in6_gif_input
439 		},
440 	},
441 	{
442 		.encap = {
443 			.proto = IPPROTO_ETHERIP,
444 			.min_length = sizeof(struct ip6_hdr) +
445 			    sizeof(struct etherip_header) +
446 			    sizeof(struct ether_header),
447 			.exact_match = ENCAP_DRV_LOOKUP,
448 			.lookup = in6_gif_lookup,
449 			.input = in6_gif_input
450 		},
451 	}
452 };
453 
454 void
455 in6_gif_init(void)
456 {
457 	int i;
458 
459 	if (!IS_DEFAULT_VNET(curvnet))
460 		return;
461 
462 	ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gif_srcaddr,
463 	    NULL, M_WAITOK);
464 	for (i = 0; i < nitems(ipv6_encap_cfg); i++)
465 		ipv6_encap_cfg[i].cookie = ip6_encap_attach(
466 		    &ipv6_encap_cfg[i].encap, NULL, M_WAITOK);
467 }
468 
469 void
470 in6_gif_uninit(void)
471 {
472 	int i;
473 
474 	if (IS_DEFAULT_VNET(curvnet)) {
475 		for (i = 0; i < nitems(ipv6_encap_cfg); i++)
476 			ip6_encap_detach(ipv6_encap_cfg[i].cookie);
477 		ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
478 	}
479 	if (V_ipv6_hashtbl != NULL) {
480 		gif_hashdestroy(V_ipv6_hashtbl);
481 		V_ipv6_hashtbl = NULL;
482 		GIF_WAIT();
483 		gif_hashdestroy(V_ipv6_srchashtbl);
484 	}
485 }
486