xref: /freebsd/sys/netinet/ip_gre.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Heiko W.Rupp <hwr@pilhuhn.de>
10  *
11  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42 
43 #include <sys/param.h>
44 #include <sys/jail.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sockio.h>
49 #include <sys/mbuf.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
54 #include <sys/proc.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/vnet.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_encap.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
68 
69 #ifdef INET6
70 #include <netinet/ip6.h>
71 #endif
72 
73 #include <net/if_gre.h>
74 #include <machine/in_cksum.h>
75 
76 #define	GRE_TTL			30
77 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
78 #define	V_ip_gre_ttl		VNET(ip_gre_ttl)
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW,
80     &VNET_NAME(ip_gre_ttl), 0, "Default TTL value for encapsulated packets");
81 
82 struct in_gre_socket {
83 	struct gre_socket		base;
84 	in_addr_t			addr;
85 };
86 VNET_DEFINE_STATIC(struct gre_sockets *, ipv4_sockets) = NULL;
87 VNET_DEFINE_STATIC(struct gre_list *, ipv4_hashtbl) = NULL;
88 VNET_DEFINE_STATIC(struct gre_list *, ipv4_srchashtbl) = NULL;
89 #define	V_ipv4_sockets		VNET(ipv4_sockets)
90 #define	V_ipv4_hashtbl		VNET(ipv4_hashtbl)
91 #define	V_ipv4_srchashtbl	VNET(ipv4_srchashtbl)
92 #define	GRE_HASH(src, dst)	(V_ipv4_hashtbl[\
93     in_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
94 #define	GRE_SRCHASH(src)	(V_ipv4_srchashtbl[\
95     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
96 #define	GRE_SOCKHASH(src)	(V_ipv4_sockets[\
97     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
98 #define	GRE_HASH_SC(sc)		GRE_HASH((sc)->gre_oip.ip_src.s_addr,\
99     (sc)->gre_oip.ip_dst.s_addr)
100 
101 static uint32_t
102 in_gre_hashval(in_addr_t src, in_addr_t dst)
103 {
104 	uint32_t ret;
105 
106 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
107 	return (fnv_32_buf(&dst, sizeof(dst), ret));
108 }
109 
110 static struct gre_socket*
111 in_gre_lookup_socket(in_addr_t addr)
112 {
113 	struct gre_socket *gs;
114 	struct in_gre_socket *s;
115 
116 	CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
117 		s = __containerof(gs, struct in_gre_socket, base);
118 		if (s->addr == addr)
119 			break;
120 	}
121 	return (gs);
122 }
123 
124 static int
125 in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst,
126     uint32_t opts)
127 {
128 	struct gre_list *head;
129 	struct gre_softc *tmp;
130 	struct gre_socket *gs;
131 
132 	if (sc->gre_family == AF_INET &&
133 	    sc->gre_oip.ip_src.s_addr == src &&
134 	    sc->gre_oip.ip_dst.s_addr == dst &&
135 	    (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP))
136 		return (EEXIST);
137 
138 	if (opts & GRE_UDPENCAP) {
139 		gs = in_gre_lookup_socket(src);
140 		if (gs == NULL)
141 			return (0);
142 		head = &gs->list;
143 	} else
144 		head = &GRE_HASH(src, dst);
145 
146 	CK_LIST_FOREACH(tmp, head, chain) {
147 		if (tmp == sc)
148 			continue;
149 		if (tmp->gre_oip.ip_src.s_addr == src &&
150 		    tmp->gre_oip.ip_dst.s_addr == dst)
151 			return (EADDRNOTAVAIL);
152 	}
153 	return (0);
154 }
155 
156 static int
157 in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
158 {
159 	const struct ip *ip;
160 	struct gre_softc *sc;
161 
162 	if (V_ipv4_hashtbl == NULL)
163 		return (0);
164 
165 	NET_EPOCH_ASSERT();
166 	ip = mtod(m, const struct ip *);
167 	CK_LIST_FOREACH(sc, &GRE_HASH(ip->ip_dst.s_addr,
168 	    ip->ip_src.s_addr), chain) {
169 		/*
170 		 * This is an inbound packet, its ip_dst is source address
171 		 * in softc.
172 		 */
173 		if (sc->gre_oip.ip_src.s_addr == ip->ip_dst.s_addr &&
174 		    sc->gre_oip.ip_dst.s_addr == ip->ip_src.s_addr) {
175 			if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
176 				return (0);
177 			*arg = sc;
178 			return (ENCAP_DRV_LOOKUP);
179 		}
180 	}
181 	return (0);
182 }
183 
184 /*
185  * Check that ingress address belongs to local host.
186  */
187 static void
188 in_gre_set_running(struct gre_softc *sc)
189 {
190 
191 	if (in_localip(sc->gre_oip.ip_src))
192 		GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
193 	else
194 		GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
195 }
196 
197 /*
198  * ifaddr_event handler.
199  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
200  * source address spoofing.
201  */
202 static void
203 in_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
204     int event __unused)
205 {
206 	const struct sockaddr_in *sin;
207 	struct gre_softc *sc;
208 
209 	/* Check that VNET is ready */
210 	if (V_ipv4_hashtbl == NULL)
211 		return;
212 
213 	NET_EPOCH_ASSERT();
214 	sin = (const struct sockaddr_in *)sa;
215 	CK_LIST_FOREACH(sc, &GRE_SRCHASH(sin->sin_addr.s_addr), srchash) {
216 		if (sc->gre_oip.ip_src.s_addr != sin->sin_addr.s_addr)
217 			continue;
218 		in_gre_set_running(sc);
219 	}
220 }
221 
222 static bool
223 in_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
224     const struct sockaddr *sa, void *ctx)
225 {
226 	struct gre_socket *gs;
227 	struct gre_softc *sc;
228 	in_addr_t dst;
229 
230 	NET_EPOCH_ASSERT();
231 
232 	gs = (struct gre_socket *)ctx;
233 	dst = ((const struct sockaddr_in *)sa)->sin_addr.s_addr;
234 	CK_LIST_FOREACH(sc, &gs->list, chain) {
235 		if (sc->gre_oip.ip_dst.s_addr == dst)
236 			break;
237 	}
238 	if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
239 		gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
240 		return (true);
241 	}
242 	m_freem(m);
243 
244 	return (true);
245 }
246 
247 static int
248 in_gre_setup_socket(struct gre_softc *sc)
249 {
250 	struct sockopt sopt;
251 	struct sockaddr_in sin;
252 	struct in_gre_socket *s;
253 	struct gre_socket *gs;
254 	in_addr_t addr;
255 	int error, value;
256 
257 	/*
258 	 * NOTE: we are protected with gre_ioctl_sx lock.
259 	 *
260 	 * First check that socket is already configured.
261 	 * If so, check that source addres was not changed.
262 	 * If address is different, check that there are no other tunnels
263 	 * and close socket.
264 	 */
265 	addr = sc->gre_oip.ip_src.s_addr;
266 	gs = sc->gre_so;
267 	if (gs != NULL) {
268 		s = __containerof(gs, struct in_gre_socket, base);
269 		if (s->addr != addr) {
270 			if (CK_LIST_EMPTY(&gs->list)) {
271 				CK_LIST_REMOVE(gs, chain);
272 				soclose(gs->so);
273 				NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
274 			}
275 			gs = sc->gre_so = NULL;
276 		}
277 	}
278 
279 	if (gs == NULL) {
280 		/*
281 		 * Check that socket for given address is already
282 		 * configured.
283 		 */
284 		gs = in_gre_lookup_socket(addr);
285 		if (gs == NULL) {
286 			s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
287 			s->addr = addr;
288 			gs = &s->base;
289 
290 			error = socreate(sc->gre_family, &gs->so,
291 			    SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
292 			    curthread);
293 			if (error != 0) {
294 				if_printf(GRE2IFP(sc),
295 				    "cannot create socket: %d\n", error);
296 				free(s, M_GRE);
297 				return (error);
298 			}
299 
300 			error = udp_set_kernel_tunneling(gs->so,
301 			    in_gre_udp_input, NULL, gs);
302 			if (error != 0) {
303 				if_printf(GRE2IFP(sc),
304 				    "cannot set UDP tunneling: %d\n", error);
305 				goto fail;
306 			}
307 
308 			memset(&sopt, 0, sizeof(sopt));
309 			sopt.sopt_dir = SOPT_SET;
310 			sopt.sopt_level = IPPROTO_IP;
311 			sopt.sopt_name = IP_BINDANY;
312 			sopt.sopt_val = &value;
313 			sopt.sopt_valsize = sizeof(value);
314 			value = 1;
315 			error = sosetopt(gs->so, &sopt);
316 			if (error != 0) {
317 				if_printf(GRE2IFP(sc),
318 				    "cannot set IP_BINDANY opt: %d\n", error);
319 				goto fail;
320 			}
321 
322 			memset(&sin, 0, sizeof(sin));
323 			sin.sin_family = AF_INET;
324 			sin.sin_len = sizeof(sin);
325 			sin.sin_addr.s_addr = addr;
326 			sin.sin_port = htons(GRE_UDPPORT);
327 			error = sobind(gs->so, (struct sockaddr *)&sin,
328 			    curthread);
329 			if (error != 0) {
330 				if_printf(GRE2IFP(sc),
331 				    "cannot bind socket: %d\n", error);
332 				goto fail;
333 			}
334 			/* Add socket to the chain */
335 			CK_LIST_INSERT_HEAD(&GRE_SOCKHASH(addr), gs, chain);
336 		}
337 	}
338 
339 	/* Add softc to the socket's list */
340 	CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
341 	sc->gre_so = gs;
342 	return (0);
343 fail:
344 	soclose(gs->so);
345 	free(s, M_GRE);
346 	return (error);
347 }
348 
349 static int
350 in_gre_attach(struct gre_softc *sc)
351 {
352 	struct epoch_tracker et;
353 	struct grehdr *gh;
354 	int error;
355 
356 	if (sc->gre_options & GRE_UDPENCAP) {
357 		sc->gre_csumflags = CSUM_UDP;
358 		sc->gre_hlen = sizeof(struct greudp);
359 		sc->gre_oip.ip_p = IPPROTO_UDP;
360 		gh = &sc->gre_udphdr->gi_gre;
361 		gre_update_udphdr(sc, &sc->gre_udp,
362 		    in_pseudo(sc->gre_oip.ip_src.s_addr,
363 		    sc->gre_oip.ip_dst.s_addr, 0));
364 	} else {
365 		sc->gre_hlen = sizeof(struct greip);
366 		sc->gre_oip.ip_p = IPPROTO_GRE;
367 		gh = &sc->gre_iphdr->gi_gre;
368 	}
369 	sc->gre_oip.ip_v = IPVERSION;
370 	sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
371 	gre_update_hdr(sc, gh);
372 
373 	/*
374 	 * If we return error, this means that sc is not linked,
375 	 * and caller should reset gre_family and free(sc->gre_hdr).
376 	 */
377 	if (sc->gre_options & GRE_UDPENCAP) {
378 		error = in_gre_setup_socket(sc);
379 		if (error != 0)
380 			return (error);
381 	} else
382 		CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
383 	CK_LIST_INSERT_HEAD(&GRE_SRCHASH(sc->gre_oip.ip_src.s_addr),
384 	    sc, srchash);
385 
386 	/* Set IFF_DRV_RUNNING if interface is ready */
387 	NET_EPOCH_ENTER(et);
388 	in_gre_set_running(sc);
389 	NET_EPOCH_EXIT(et);
390 	return (0);
391 }
392 
393 int
394 in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
395 {
396 	int error;
397 
398 	/* NOTE: we are protected with gre_ioctl_sx lock */
399 	MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
400 	MPASS(sc->gre_family == AF_INET);
401 
402 	/*
403 	 * If we are going to change encapsulation protocol, do check
404 	 * for duplicate tunnels. Return EEXIST here to do not confuse
405 	 * user.
406 	 */
407 	if (cmd == GRESOPTS &&
408 	    (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
409 	    in_gre_checkdup(sc, sc->gre_oip.ip_src.s_addr,
410 		sc->gre_oip.ip_dst.s_addr, value) == EADDRNOTAVAIL)
411 		return (EEXIST);
412 
413 	CK_LIST_REMOVE(sc, chain);
414 	CK_LIST_REMOVE(sc, srchash);
415 	GRE_WAIT();
416 	switch (cmd) {
417 	case GRESKEY:
418 		sc->gre_key = value;
419 		break;
420 	case GRESOPTS:
421 		sc->gre_options = value;
422 		break;
423 	case GRESPORT:
424 		sc->gre_port = value;
425 		break;
426 	}
427 	error = in_gre_attach(sc);
428 	if (error != 0) {
429 		sc->gre_family = 0;
430 		free(sc->gre_hdr, M_GRE);
431 	}
432 	return (error);
433 }
434 
435 int
436 in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
437 {
438 	struct ifreq *ifr = (struct ifreq *)data;
439 	struct sockaddr_in *dst, *src;
440 	struct ip *ip;
441 	int error;
442 
443 	/* NOTE: we are protected with gre_ioctl_sx lock */
444 	error = EINVAL;
445 	switch (cmd) {
446 	case SIOCSIFPHYADDR:
447 		src = &((struct in_aliasreq *)data)->ifra_addr;
448 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
449 
450 		/* sanity checks */
451 		if (src->sin_family != dst->sin_family ||
452 		    src->sin_family != AF_INET ||
453 		    src->sin_len != dst->sin_len ||
454 		    src->sin_len != sizeof(*src))
455 			break;
456 		if (src->sin_addr.s_addr == INADDR_ANY ||
457 		    dst->sin_addr.s_addr == INADDR_ANY) {
458 			error = EADDRNOTAVAIL;
459 			break;
460 		}
461 		if (V_ipv4_hashtbl == NULL) {
462 			V_ipv4_hashtbl = gre_hashinit();
463 			V_ipv4_srchashtbl = gre_hashinit();
464 			V_ipv4_sockets = (struct gre_sockets *)gre_hashinit();
465 		}
466 		error = in_gre_checkdup(sc, src->sin_addr.s_addr,
467 		    dst->sin_addr.s_addr, sc->gre_options);
468 		if (error == EADDRNOTAVAIL)
469 			break;
470 		if (error == EEXIST) {
471 			/* Addresses are the same. Just return. */
472 			error = 0;
473 			break;
474 		}
475 		ip = malloc(sizeof(struct greudp) + 3 * sizeof(uint32_t),
476 		    M_GRE, M_WAITOK | M_ZERO);
477 		ip->ip_src.s_addr = src->sin_addr.s_addr;
478 		ip->ip_dst.s_addr = dst->sin_addr.s_addr;
479 		if (sc->gre_family != 0) {
480 			/* Detach existing tunnel first */
481 			CK_LIST_REMOVE(sc, chain);
482 			CK_LIST_REMOVE(sc, srchash);
483 			GRE_WAIT();
484 			free(sc->gre_hdr, M_GRE);
485 			/* XXX: should we notify about link state change? */
486 		}
487 		sc->gre_family = AF_INET;
488 		sc->gre_hdr = ip;
489 		sc->gre_oseq = 0;
490 		sc->gre_iseq = UINT32_MAX;
491 		error = in_gre_attach(sc);
492 		if (error != 0) {
493 			sc->gre_family = 0;
494 			free(sc->gre_hdr, M_GRE);
495 		}
496 		break;
497 	case SIOCGIFPSRCADDR:
498 	case SIOCGIFPDSTADDR:
499 		if (sc->gre_family != AF_INET) {
500 			error = EADDRNOTAVAIL;
501 			break;
502 		}
503 		src = (struct sockaddr_in *)&ifr->ifr_addr;
504 		memset(src, 0, sizeof(*src));
505 		src->sin_family = AF_INET;
506 		src->sin_len = sizeof(*src);
507 		src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
508 		    sc->gre_oip.ip_src: sc->gre_oip.ip_dst;
509 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
510 		if (error != 0)
511 			memset(src, 0, sizeof(*src));
512 		break;
513 	}
514 	return (error);
515 }
516 
517 int
518 in_gre_output(struct mbuf *m, int af, int hlen)
519 {
520 	struct greip *gi;
521 
522 	gi = mtod(m, struct greip *);
523 	switch (af) {
524 	case AF_INET:
525 		/*
526 		 * gre_transmit() has used M_PREPEND() that doesn't guarantee
527 		 * m_data is contiguous more than hlen bytes. Use m_copydata()
528 		 * here to avoid m_pullup().
529 		 */
530 		m_copydata(m, hlen + offsetof(struct ip, ip_tos),
531 		    sizeof(u_char), &gi->gi_ip.ip_tos);
532 		m_copydata(m, hlen + offsetof(struct ip, ip_id),
533 		    sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
534 		break;
535 #ifdef INET6
536 	case AF_INET6:
537 		gi->gi_ip.ip_tos = 0; /* XXX */
538 		ip_fillid(&gi->gi_ip);
539 		break;
540 #endif
541 	}
542 	gi->gi_ip.ip_ttl = V_ip_gre_ttl;
543 	gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
544 	return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
545 }
546 
547 static const struct srcaddrtab *ipv4_srcaddrtab = NULL;
548 static const struct encaptab *ecookie = NULL;
549 static const struct encap_config ipv4_encap_cfg = {
550 	.proto = IPPROTO_GRE,
551 	.min_length = sizeof(struct greip) + sizeof(struct ip),
552 	.exact_match = ENCAP_DRV_LOOKUP,
553 	.lookup = in_gre_lookup,
554 	.input = gre_input
555 };
556 
557 void
558 in_gre_init(void)
559 {
560 
561 	if (!IS_DEFAULT_VNET(curvnet))
562 		return;
563 	ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gre_srcaddr,
564 	    NULL, M_WAITOK);
565 	ecookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
566 }
567 
568 void
569 in_gre_uninit(void)
570 {
571 
572 	if (IS_DEFAULT_VNET(curvnet)) {
573 		ip_encap_detach(ecookie);
574 		ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
575 	}
576 	if (V_ipv4_hashtbl != NULL) {
577 		gre_hashdestroy(V_ipv4_hashtbl);
578 		V_ipv4_hashtbl = NULL;
579 		GRE_WAIT();
580 		gre_hashdestroy(V_ipv4_srchashtbl);
581 		gre_hashdestroy((struct gre_list *)V_ipv4_sockets);
582 	}
583 }
584