xref: /freebsd/sys/netinet/in_gif.c (revision 009d82ee0fefdc797f55d25a50d3b6ebe61d56e4)
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: in_gif.c,v 1.54 2001/05/14 14:02:16 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/sysctl.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52 
53 #include <net/ethernet.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58 
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_encap.h>
65 #include <netinet/ip_ecn.h>
66 #include <netinet/in_fib.h>
67 
68 #ifdef INET6
69 #include <netinet/ip6.h>
70 #endif
71 
72 #include <net/if_gif.h>
73 
74 #define GIF_TTL		30
75 VNET_DEFINE_STATIC(int, ip_gif_ttl) = GIF_TTL;
76 #define	V_ip_gif_ttl		VNET(ip_gif_ttl)
77 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
78     &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets");
79 
80 /*
81  * We keep interfaces in a hash table using src+dst as key.
82  * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
83  */
84 VNET_DEFINE_STATIC(struct gif_list *, ipv4_hashtbl) = NULL;
85 VNET_DEFINE_STATIC(struct gif_list *, ipv4_srchashtbl) = NULL;
86 VNET_DEFINE_STATIC(struct gif_list, ipv4_list) = CK_LIST_HEAD_INITIALIZER();
87 #define	V_ipv4_hashtbl		VNET(ipv4_hashtbl)
88 #define	V_ipv4_srchashtbl	VNET(ipv4_srchashtbl)
89 #define	V_ipv4_list		VNET(ipv4_list)
90 
91 #define	GIF_HASH(src, dst)	(V_ipv4_hashtbl[\
92     in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
93 #define	GIF_SRCHASH(src)	(V_ipv4_srchashtbl[\
94     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
95 #define	GIF_HASH_SC(sc)		GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
96     (sc)->gif_iphdr->ip_dst.s_addr)
97 static uint32_t
98 in_gif_hashval(in_addr_t src, in_addr_t dst)
99 {
100 	uint32_t ret;
101 
102 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
103 	return (fnv_32_buf(&dst, sizeof(dst), ret));
104 }
105 
106 static int
107 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
108 {
109 	struct gif_softc *tmp;
110 
111 	if (sc->gif_family == AF_INET &&
112 	    sc->gif_iphdr->ip_src.s_addr == src &&
113 	    sc->gif_iphdr->ip_dst.s_addr == dst)
114 		return (EEXIST);
115 
116 	CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
117 		if (tmp == sc)
118 			continue;
119 		if (tmp->gif_iphdr->ip_src.s_addr == src &&
120 		    tmp->gif_iphdr->ip_dst.s_addr == dst)
121 			return (EADDRNOTAVAIL);
122 	}
123 	return (0);
124 }
125 
126 /*
127  * Check that ingress address belongs to local host.
128  */
129 static void
130 in_gif_set_running(struct gif_softc *sc)
131 {
132 
133 	if (in_localip(sc->gif_iphdr->ip_src))
134 		GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
135 	else
136 		GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
137 }
138 
139 /*
140  * ifaddr_event handler.
141  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
142  * source address spoofing.
143  */
144 static void
145 in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa,
146     int event __unused)
147 {
148 	const struct sockaddr_in *sin;
149 	struct gif_softc *sc;
150 
151 	if (V_ipv4_srchashtbl == NULL)
152 		return;
153 
154 	MPASS(in_epoch(net_epoch_preempt));
155 	sin = (const struct sockaddr_in *)sa;
156 	CK_LIST_FOREACH(sc, &GIF_SRCHASH(sin->sin_addr.s_addr), srchash) {
157 		if (sc->gif_iphdr->ip_src.s_addr != sin->sin_addr.s_addr)
158 			continue;
159 		in_gif_set_running(sc);
160 	}
161 }
162 
163 static void
164 in_gif_attach(struct gif_softc *sc)
165 {
166 
167 	if (sc->gif_options & GIF_IGNORE_SOURCE)
168 		CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
169 	else
170 		CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
171 
172 	CK_LIST_INSERT_HEAD(&GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
173 	    sc, srchash);
174 }
175 
176 int
177 in_gif_setopts(struct gif_softc *sc, u_int options)
178 {
179 
180 	/* NOTE: we are protected with gif_ioctl_sx lock */
181 	MPASS(sc->gif_family == AF_INET);
182 	MPASS(sc->gif_options != options);
183 
184 	if ((options & GIF_IGNORE_SOURCE) !=
185 	    (sc->gif_options & GIF_IGNORE_SOURCE)) {
186 		CK_LIST_REMOVE(sc, srchash);
187 		CK_LIST_REMOVE(sc, chain);
188 		sc->gif_options = options;
189 		in_gif_attach(sc);
190 	}
191 	return (0);
192 }
193 
194 int
195 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
196 {
197 	struct ifreq *ifr = (struct ifreq *)data;
198 	struct sockaddr_in *dst, *src;
199 	struct ip *ip;
200 	int error;
201 
202 	/* NOTE: we are protected with gif_ioctl_sx lock */
203 	error = EINVAL;
204 	switch (cmd) {
205 	case SIOCSIFPHYADDR:
206 		src = &((struct in_aliasreq *)data)->ifra_addr;
207 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
208 
209 		/* sanity checks */
210 		if (src->sin_family != dst->sin_family ||
211 		    src->sin_family != AF_INET ||
212 		    src->sin_len != dst->sin_len ||
213 		    src->sin_len != sizeof(*src))
214 			break;
215 		if (src->sin_addr.s_addr == INADDR_ANY ||
216 		    dst->sin_addr.s_addr == INADDR_ANY) {
217 			error = EADDRNOTAVAIL;
218 			break;
219 		}
220 		if (V_ipv4_hashtbl == NULL) {
221 			V_ipv4_hashtbl = gif_hashinit();
222 			V_ipv4_srchashtbl = gif_hashinit();
223 		}
224 		error = in_gif_checkdup(sc, src->sin_addr.s_addr,
225 		    dst->sin_addr.s_addr);
226 		if (error == EADDRNOTAVAIL)
227 			break;
228 		if (error == EEXIST) {
229 			/* Addresses are the same. Just return. */
230 			error = 0;
231 			break;
232 		}
233 		ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
234 		ip->ip_src.s_addr = src->sin_addr.s_addr;
235 		ip->ip_dst.s_addr = dst->sin_addr.s_addr;
236 		if (sc->gif_family != 0) {
237 			/* Detach existing tunnel first */
238 			CK_LIST_REMOVE(sc, srchash);
239 			CK_LIST_REMOVE(sc, chain);
240 			GIF_WAIT();
241 			free(sc->gif_hdr, M_GIF);
242 			/* XXX: should we notify about link state change? */
243 		}
244 		sc->gif_family = AF_INET;
245 		sc->gif_iphdr = ip;
246 		in_gif_attach(sc);
247 		in_gif_set_running(sc);
248 		break;
249 	case SIOCGIFPSRCADDR:
250 	case SIOCGIFPDSTADDR:
251 		if (sc->gif_family != AF_INET) {
252 			error = EADDRNOTAVAIL;
253 			break;
254 		}
255 		src = (struct sockaddr_in *)&ifr->ifr_addr;
256 		memset(src, 0, sizeof(*src));
257 		src->sin_family = AF_INET;
258 		src->sin_len = sizeof(*src);
259 		src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
260 		    sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
261 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
262 		if (error != 0)
263 			memset(src, 0, sizeof(*src));
264 		break;
265 	}
266 	return (error);
267 }
268 
269 int
270 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
271 {
272 	struct gif_softc *sc = ifp->if_softc;
273 	struct ip *ip;
274 	int len;
275 
276 	/* prepend new IP header */
277 	MPASS(in_epoch(net_epoch_preempt));
278 	len = sizeof(struct ip);
279 #ifndef __NO_STRICT_ALIGNMENT
280 	if (proto == IPPROTO_ETHERIP)
281 		len += ETHERIP_ALIGN;
282 #endif
283 	M_PREPEND(m, len, M_NOWAIT);
284 	if (m == NULL)
285 		return (ENOBUFS);
286 #ifndef __NO_STRICT_ALIGNMENT
287 	if (proto == IPPROTO_ETHERIP) {
288 		len = mtod(m, vm_offset_t) & 3;
289 		KASSERT(len == 0 || len == ETHERIP_ALIGN,
290 		    ("in_gif_output: unexpected misalignment"));
291 		m->m_data += len;
292 		m->m_len -= ETHERIP_ALIGN;
293 	}
294 #endif
295 	ip = mtod(m, struct ip *);
296 
297 	MPASS(sc->gif_family == AF_INET);
298 	bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
299 	ip->ip_p = proto;
300 	/* version will be set in ip_output() */
301 	ip->ip_ttl = V_ip_gif_ttl;
302 	ip->ip_len = htons(m->m_pkthdr.len);
303 	ip->ip_tos = ecn;
304 
305 	return (ip_output(m, NULL, NULL, 0, NULL, NULL));
306 }
307 
308 static int
309 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
310 {
311 	struct gif_softc *sc = arg;
312 	struct ifnet *gifp;
313 	struct ip *ip;
314 	uint8_t ecn;
315 
316 	MPASS(in_epoch(net_epoch_preempt));
317 	if (sc == NULL) {
318 		m_freem(m);
319 		KMOD_IPSTAT_INC(ips_nogif);
320 		return (IPPROTO_DONE);
321 	}
322 	gifp = GIF2IFP(sc);
323 	if ((gifp->if_flags & IFF_UP) != 0) {
324 		ip = mtod(m, struct ip *);
325 		ecn = ip->ip_tos;
326 		m_adj(m, off);
327 		gif_input(m, gifp, proto, ecn);
328 	} else {
329 		m_freem(m);
330 		KMOD_IPSTAT_INC(ips_nogif);
331 	}
332 	return (IPPROTO_DONE);
333 }
334 
335 static int
336 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
337 {
338 	const struct ip *ip;
339 	struct gif_softc *sc;
340 	int ret;
341 
342 	if (V_ipv4_hashtbl == NULL)
343 		return (0);
344 
345 	MPASS(in_epoch(net_epoch_preempt));
346 	ip = mtod(m, const struct ip *);
347 	/*
348 	 * NOTE: it is safe to iterate without any locking here, because softc
349 	 * can be reclaimed only when we are not within net_epoch_preempt
350 	 * section, but ip_encap lookup+input are executed in epoch section.
351 	 */
352 	ret = 0;
353 	CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
354 	    ip->ip_src.s_addr), chain) {
355 		/*
356 		 * This is an inbound packet, its ip_dst is source address
357 		 * in softc.
358 		 */
359 		if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
360 		    sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
361 			ret = ENCAP_DRV_LOOKUP;
362 			goto done;
363 		}
364 	}
365 	/*
366 	 * No exact match.
367 	 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
368 	 */
369 	CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
370 		if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
371 			ret = 32 + 8; /* src + proto */
372 			goto done;
373 		}
374 	}
375 	return (0);
376 done:
377 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
378 		return (0);
379 	/* ingress filters on outer source */
380 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
381 		struct nhop4_basic nh4;
382 		struct in_addr dst;
383 
384 		dst = ip->ip_src;
385 		if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0)
386 			return (0);
387 		if (nh4.nh_ifp != m->m_pkthdr.rcvif)
388 			return (0);
389 	}
390 	*arg = sc;
391 	return (ret);
392 }
393 
394 static const struct srcaddrtab *ipv4_srcaddrtab;
395 static struct {
396 	const struct encap_config encap;
397 	const struct encaptab *cookie;
398 } ipv4_encap_cfg[] = {
399 	{
400 		.encap = {
401 			.proto = IPPROTO_IPV4,
402 			.min_length = 2 * sizeof(struct ip),
403 			.exact_match = ENCAP_DRV_LOOKUP,
404 			.lookup = in_gif_lookup,
405 			.input = in_gif_input
406 		},
407 	},
408 #ifdef INET6
409 	{
410 		.encap = {
411 			.proto = IPPROTO_IPV6,
412 			.min_length = sizeof(struct ip) +
413 			    sizeof(struct ip6_hdr),
414 			.exact_match = ENCAP_DRV_LOOKUP,
415 			.lookup = in_gif_lookup,
416 			.input = in_gif_input
417 		},
418 	},
419 #endif
420 	{
421 		.encap = {
422 			.proto = IPPROTO_ETHERIP,
423 			.min_length = sizeof(struct ip) +
424 			    sizeof(struct etherip_header) +
425 			    sizeof(struct ether_header),
426 			.exact_match = ENCAP_DRV_LOOKUP,
427 			.lookup = in_gif_lookup,
428 			.input = in_gif_input
429 		},
430 	}
431 };
432 
433 void
434 in_gif_init(void)
435 {
436 	int i;
437 
438 	if (!IS_DEFAULT_VNET(curvnet))
439 		return;
440 
441 	ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
442 	    NULL, M_WAITOK);
443 	for (i = 0; i < nitems(ipv4_encap_cfg); i++)
444 		ipv4_encap_cfg[i].cookie = ip_encap_attach(
445 		    &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
446 }
447 
448 void
449 in_gif_uninit(void)
450 {
451 	int i;
452 
453 	if (IS_DEFAULT_VNET(curvnet)) {
454 		for (i = 0; i < nitems(ipv4_encap_cfg); i++)
455 			ip_encap_detach(ipv4_encap_cfg[i].cookie);
456 		ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
457 	}
458 	if (V_ipv4_hashtbl != NULL) {
459 		gif_hashdestroy(V_ipv4_hashtbl);
460 		gif_hashdestroy(V_ipv4_srchashtbl);
461 	}
462 }
463 
464