2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
11 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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.
34 * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_inet6.h"
43 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sockio.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
57 #include <net/if_var.h>
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>
70 #include <netinet/ip6.h>
73 #include <net/if_gre.h>
74 #include <machine/in_cksum.h>
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");
82 struct in_gre_socket {
83 struct gre_socket base;
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)
102 in_gre_hashval(in_addr_t src, in_addr_t dst)
106 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
107 return (fnv_32_buf(&dst, sizeof(dst), ret));
110 static struct gre_socket*
111 in_gre_lookup_socket(in_addr_t addr)
113 struct gre_socket *gs;
114 struct in_gre_socket *s;
116 CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
117 s = __containerof(gs, struct in_gre_socket, base);
125 in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst,
128 struct gre_list *head;
129 struct gre_softc *tmp;
130 struct gre_socket *gs;
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))
138 if (opts & GRE_UDPENCAP) {
139 gs = in_gre_lookup_socket(src);
144 head = &GRE_HASH(src, dst);
146 CK_LIST_FOREACH(tmp, head, chain) {
149 if (tmp->gre_oip.ip_src.s_addr == src &&
150 tmp->gre_oip.ip_dst.s_addr == dst)
151 return (EADDRNOTAVAIL);
157 in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
160 struct gre_softc *sc;
162 if (V_ipv4_hashtbl == NULL)
165 MPASS(in_epoch(net_epoch_preempt));
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) {
170 * This is an inbound packet, its ip_dst is source address
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)
178 return (ENCAP_DRV_LOOKUP);
185 * Check that ingress address belongs to local host.
188 in_gre_set_running(struct gre_softc *sc)
191 if (in_localip(sc->gre_oip.ip_src))
192 GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
194 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
198 * ifaddr_event handler.
199 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
200 * source address spoofing.
203 in_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
206 const struct sockaddr_in *sin;
207 struct gre_softc *sc;
209 /* Check that VNET is ready */
210 if (V_ipv4_hashtbl == NULL)
213 MPASS(in_epoch(net_epoch_preempt));
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)
218 in_gre_set_running(sc);
223 in_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
224 const struct sockaddr *sa, void *ctx)
226 struct epoch_tracker et;
227 struct gre_socket *gs;
228 struct gre_softc *sc;
233 * udp_append() holds reference to inp, it is safe to check
234 * inp_flags2 without INP_RLOCK().
235 * If socket was closed before we have entered NET_EPOCH section,
236 * INP_FREED flag should be set. Otherwise it should be safe to
237 * make access to ctx data, because gre_so will be freed by
238 * gre_sofree() via epoch_call().
240 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
246 gs = (struct gre_socket *)ctx;
247 dst = ((const struct sockaddr_in *)sa)->sin_addr.s_addr;
248 CK_LIST_FOREACH(sc, &gs->list, chain) {
249 if (sc->gre_oip.ip_dst.s_addr == dst)
252 if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
253 gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
262 in_gre_setup_socket(struct gre_softc *sc)
265 struct sockaddr_in sin;
266 struct in_gre_socket *s;
267 struct gre_socket *gs;
272 * NOTE: we are protected with gre_ioctl_sx lock.
274 * First check that socket is already configured.
275 * If so, check that source addres was not changed.
276 * If address is different, check that there are no other tunnels
279 addr = sc->gre_oip.ip_src.s_addr;
282 s = __containerof(gs, struct in_gre_socket, base);
283 if (s->addr != addr) {
284 if (CK_LIST_EMPTY(&gs->list)) {
285 CK_LIST_REMOVE(gs, chain);
287 epoch_call(net_epoch_preempt, &gs->epoch_ctx,
290 gs = sc->gre_so = NULL;
296 * Check that socket for given address is already
299 gs = in_gre_lookup_socket(addr);
301 s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
305 error = socreate(sc->gre_family, &gs->so,
306 SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
309 if_printf(GRE2IFP(sc),
310 "cannot create socket: %d\n", error);
315 error = udp_set_kernel_tunneling(gs->so,
316 in_gre_udp_input, NULL, gs);
318 if_printf(GRE2IFP(sc),
319 "cannot set UDP tunneling: %d\n", error);
323 memset(&sopt, 0, sizeof(sopt));
324 sopt.sopt_dir = SOPT_SET;
325 sopt.sopt_level = IPPROTO_IP;
326 sopt.sopt_name = IP_BINDANY;
327 sopt.sopt_val = &value;
328 sopt.sopt_valsize = sizeof(value);
330 error = sosetopt(gs->so, &sopt);
332 if_printf(GRE2IFP(sc),
333 "cannot set IP_BINDANY opt: %d\n", error);
337 memset(&sin, 0, sizeof(sin));
338 sin.sin_family = AF_INET;
339 sin.sin_len = sizeof(sin);
340 sin.sin_addr.s_addr = addr;
341 sin.sin_port = htons(GRE_UDPPORT);
342 error = sobind(gs->so, (struct sockaddr *)&sin,
345 if_printf(GRE2IFP(sc),
346 "cannot bind socket: %d\n", error);
349 /* Add socket to the chain */
350 CK_LIST_INSERT_HEAD(&GRE_SOCKHASH(addr), gs, chain);
354 /* Add softc to the socket's list */
355 CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
365 in_gre_attach(struct gre_softc *sc)
370 if (sc->gre_options & GRE_UDPENCAP) {
371 sc->gre_csumflags = CSUM_UDP;
372 sc->gre_hlen = sizeof(struct greudp);
373 sc->gre_oip.ip_p = IPPROTO_UDP;
374 gh = &sc->gre_udphdr->gi_gre;
375 gre_update_udphdr(sc, &sc->gre_udp,
376 in_pseudo(sc->gre_oip.ip_src.s_addr,
377 sc->gre_oip.ip_dst.s_addr, 0));
379 sc->gre_hlen = sizeof(struct greip);
380 sc->gre_oip.ip_p = IPPROTO_GRE;
381 gh = &sc->gre_iphdr->gi_gre;
383 sc->gre_oip.ip_v = IPVERSION;
384 sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
385 gre_update_hdr(sc, gh);
388 * If we return error, this means that sc is not linked,
389 * and caller should reset gre_family and free(sc->gre_hdr).
391 if (sc->gre_options & GRE_UDPENCAP) {
392 error = in_gre_setup_socket(sc);
396 CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
397 CK_LIST_INSERT_HEAD(&GRE_SRCHASH(sc->gre_oip.ip_src.s_addr),
400 /* Set IFF_DRV_RUNNING if interface is ready */
401 in_gre_set_running(sc);
406 in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
410 /* NOTE: we are protected with gre_ioctl_sx lock */
411 MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
412 MPASS(sc->gre_family == AF_INET);
415 * If we are going to change encapsulation protocol, do check
416 * for duplicate tunnels. Return EEXIST here to do not confuse
419 if (cmd == GRESOPTS &&
420 (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
421 in_gre_checkdup(sc, sc->gre_oip.ip_src.s_addr,
422 sc->gre_oip.ip_dst.s_addr, value) == EADDRNOTAVAIL)
425 CK_LIST_REMOVE(sc, chain);
426 CK_LIST_REMOVE(sc, srchash);
433 sc->gre_options = value;
436 sc->gre_port = value;
439 error = in_gre_attach(sc);
442 free(sc->gre_hdr, M_GRE);
448 in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
450 struct ifreq *ifr = (struct ifreq *)data;
451 struct sockaddr_in *dst, *src;
455 /* NOTE: we are protected with gre_ioctl_sx lock */
459 src = &((struct in_aliasreq *)data)->ifra_addr;
460 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
463 if (src->sin_family != dst->sin_family ||
464 src->sin_family != AF_INET ||
465 src->sin_len != dst->sin_len ||
466 src->sin_len != sizeof(*src))
468 if (src->sin_addr.s_addr == INADDR_ANY ||
469 dst->sin_addr.s_addr == INADDR_ANY) {
470 error = EADDRNOTAVAIL;
473 if (V_ipv4_hashtbl == NULL) {
474 V_ipv4_hashtbl = gre_hashinit();
475 V_ipv4_srchashtbl = gre_hashinit();
476 V_ipv4_sockets = (struct gre_sockets *)gre_hashinit();
478 error = in_gre_checkdup(sc, src->sin_addr.s_addr,
479 dst->sin_addr.s_addr, sc->gre_options);
480 if (error == EADDRNOTAVAIL)
482 if (error == EEXIST) {
483 /* Addresses are the same. Just return. */
487 ip = malloc(sizeof(struct greudp) + 3 * sizeof(uint32_t),
488 M_GRE, M_WAITOK | M_ZERO);
489 ip->ip_src.s_addr = src->sin_addr.s_addr;
490 ip->ip_dst.s_addr = dst->sin_addr.s_addr;
491 if (sc->gre_family != 0) {
492 /* Detach existing tunnel first */
493 CK_LIST_REMOVE(sc, chain);
494 CK_LIST_REMOVE(sc, srchash);
496 free(sc->gre_hdr, M_GRE);
497 /* XXX: should we notify about link state change? */
499 sc->gre_family = AF_INET;
502 sc->gre_iseq = UINT32_MAX;
503 error = in_gre_attach(sc);
506 free(sc->gre_hdr, M_GRE);
509 case SIOCGIFPSRCADDR:
510 case SIOCGIFPDSTADDR:
511 if (sc->gre_family != AF_INET) {
512 error = EADDRNOTAVAIL;
515 src = (struct sockaddr_in *)&ifr->ifr_addr;
516 memset(src, 0, sizeof(*src));
517 src->sin_family = AF_INET;
518 src->sin_len = sizeof(*src);
519 src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
520 sc->gre_oip.ip_src: sc->gre_oip.ip_dst;
521 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
523 memset(src, 0, sizeof(*src));
530 in_gre_output(struct mbuf *m, int af, int hlen)
534 gi = mtod(m, struct greip *);
538 * gre_transmit() has used M_PREPEND() that doesn't guarantee
539 * m_data is contiguous more than hlen bytes. Use m_copydata()
540 * here to avoid m_pullup().
542 m_copydata(m, hlen + offsetof(struct ip, ip_tos),
543 sizeof(u_char), &gi->gi_ip.ip_tos);
544 m_copydata(m, hlen + offsetof(struct ip, ip_id),
545 sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
549 gi->gi_ip.ip_tos = 0; /* XXX */
550 ip_fillid(&gi->gi_ip);
554 gi->gi_ip.ip_ttl = V_ip_gre_ttl;
555 gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
556 return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
559 static const struct srcaddrtab *ipv4_srcaddrtab = NULL;
560 static const struct encaptab *ecookie = NULL;
561 static const struct encap_config ipv4_encap_cfg = {
562 .proto = IPPROTO_GRE,
563 .min_length = sizeof(struct greip) + sizeof(struct ip),
564 .exact_match = ENCAP_DRV_LOOKUP,
565 .lookup = in_gre_lookup,
573 if (!IS_DEFAULT_VNET(curvnet))
575 ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gre_srcaddr,
577 ecookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
584 if (IS_DEFAULT_VNET(curvnet)) {
585 ip_encap_detach(ecookie);
586 ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
588 if (V_ipv4_hashtbl != NULL) {
589 gre_hashdestroy(V_ipv4_hashtbl);
590 V_ipv4_hashtbl = NULL;
592 gre_hashdestroy(V_ipv4_srchashtbl);
593 gre_hashdestroy((struct gre_list *)V_ipv4_sockets);