2 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sockio.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
47 #include <net/if_var.h>
48 #include <net/if_private.h>
51 #include <netinet/in.h>
53 #include <net/ethernet.h>
54 #include <netinet/ip.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_encap.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/ip6.h>
60 #include <netinet/udp.h>
61 #include <netinet/udp_var.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/in6_var.h>
64 #include <netinet6/scope6_var.h>
65 #include <net/if_gre.h>
67 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
68 #define V_ip6_gre_hlim VNET(ip6_gre_hlim)
70 SYSCTL_DECL(_net_inet6_ip6);
71 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
72 &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
74 struct in6_gre_socket {
75 struct gre_socket base;
76 struct in6_addr addr; /* scope zone id is embedded */
78 VNET_DEFINE_STATIC(struct gre_sockets *, ipv6_sockets) = NULL;
79 VNET_DEFINE_STATIC(struct gre_list *, ipv6_hashtbl) = NULL;
80 VNET_DEFINE_STATIC(struct gre_list *, ipv6_srchashtbl) = NULL;
81 #define V_ipv6_sockets VNET(ipv6_sockets)
82 #define V_ipv6_hashtbl VNET(ipv6_hashtbl)
83 #define V_ipv6_srchashtbl VNET(ipv6_srchashtbl)
84 #define GRE_HASH(src, dst) (V_ipv6_hashtbl[\
85 in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
86 #define GRE_SRCHASH(src) (V_ipv6_srchashtbl[\
87 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
88 #define GRE_SOCKHASH(src) (V_ipv6_sockets[\
89 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
90 #define GRE_HASH_SC(sc) GRE_HASH(&(sc)->gre_oip6.ip6_src,\
91 &(sc)->gre_oip6.ip6_dst)
94 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst)
98 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
99 return (fnv_32_buf(dst, sizeof(*dst), ret));
102 static struct gre_socket*
103 in6_gre_lookup_socket(const struct in6_addr *addr)
105 struct gre_socket *gs;
106 struct in6_gre_socket *s;
108 CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
109 s = __containerof(gs, struct in6_gre_socket, base);
110 if (IN6_ARE_ADDR_EQUAL(&s->addr, addr))
117 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src,
118 const struct in6_addr *dst, uint32_t opts)
120 struct gre_list *head;
121 struct gre_softc *tmp;
122 struct gre_socket *gs;
124 if (sc->gre_family == AF_INET6 &&
125 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) &&
126 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst) &&
127 (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP))
130 if (opts & GRE_UDPENCAP) {
131 gs = in6_gre_lookup_socket(src);
136 head = &GRE_HASH(src, dst);
138 CK_LIST_FOREACH(tmp, head, chain) {
141 if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) &&
142 IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst))
143 return (EADDRNOTAVAIL);
149 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
151 const struct ip6_hdr *ip6;
152 struct gre_softc *sc;
154 if (V_ipv6_hashtbl == NULL)
158 ip6 = mtod(m, const struct ip6_hdr *);
159 CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
161 * This is an inbound packet, its ip6_dst is source address
164 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
166 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst,
168 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
171 return (ENCAP_DRV_LOOKUP);
178 * Check that ingress address belongs to local host.
181 in6_gre_set_running(struct gre_softc *sc)
184 if (in6_localip(&sc->gre_oip6.ip6_src))
185 GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
187 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
191 * ifaddr_event handler.
192 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
193 * source address spoofing.
196 in6_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
199 const struct sockaddr_in6 *sin;
200 struct gre_softc *sc;
202 /* Check that VNET is ready */
203 if (V_ipv6_hashtbl == NULL)
207 sin = (const struct sockaddr_in6 *)sa;
208 CK_LIST_FOREACH(sc, &GRE_SRCHASH(&sin->sin6_addr), srchash) {
209 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
210 &sin->sin6_addr) == 0)
212 in6_gre_set_running(sc);
217 in6_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
218 const struct sockaddr *sa, void *ctx)
220 struct gre_socket *gs;
221 struct gre_softc *sc;
222 struct sockaddr_in6 dst;
226 gs = (struct gre_socket *)ctx;
227 dst = *(const struct sockaddr_in6 *)sa;
228 if (sa6_embedscope(&dst, 0)) {
232 CK_LIST_FOREACH(sc, &gs->list, chain) {
233 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &dst.sin6_addr))
236 if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
237 gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
246 in6_gre_setup_socket(struct gre_softc *sc)
249 struct sockaddr_in6 sin6;
250 struct in6_gre_socket *s;
251 struct gre_socket *gs;
255 * NOTE: we are protected with gre_ioctl_sx lock.
257 * First check that socket is already configured.
258 * If so, check that source address was not changed.
259 * If address is different, check that there are no other tunnels
264 s = __containerof(gs, struct in6_gre_socket, base);
265 if (!IN6_ARE_ADDR_EQUAL(&s->addr, &sc->gre_oip6.ip6_src)) {
266 if (CK_LIST_EMPTY(&gs->list)) {
267 CK_LIST_REMOVE(gs, chain);
269 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
271 gs = sc->gre_so = NULL;
277 * Check that socket for given address is already
280 gs = in6_gre_lookup_socket(&sc->gre_oip6.ip6_src);
282 s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
283 s->addr = sc->gre_oip6.ip6_src;
286 error = socreate(sc->gre_family, &gs->so,
287 SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
290 if_printf(GRE2IFP(sc),
291 "cannot create socket: %d\n", error);
296 error = udp_set_kernel_tunneling(gs->so,
297 in6_gre_udp_input, NULL, gs);
299 if_printf(GRE2IFP(sc),
300 "cannot set UDP tunneling: %d\n", error);
304 memset(&sopt, 0, sizeof(sopt));
305 sopt.sopt_dir = SOPT_SET;
306 sopt.sopt_level = IPPROTO_IPV6;
307 sopt.sopt_name = IPV6_BINDANY;
308 sopt.sopt_val = &value;
309 sopt.sopt_valsize = sizeof(value);
311 error = sosetopt(gs->so, &sopt);
313 if_printf(GRE2IFP(sc),
314 "cannot set IPV6_BINDANY opt: %d\n",
319 memset(&sin6, 0, sizeof(sin6));
320 sin6.sin6_family = AF_INET6;
321 sin6.sin6_len = sizeof(sin6);
322 sin6.sin6_addr = sc->gre_oip6.ip6_src;
323 sin6.sin6_port = htons(GRE_UDPPORT);
324 error = sa6_recoverscope(&sin6);
326 if_printf(GRE2IFP(sc),
327 "cannot determine scope zone id: %d\n",
331 error = sobind(gs->so, (struct sockaddr *)&sin6,
334 if_printf(GRE2IFP(sc),
335 "cannot bind socket: %d\n", error);
338 /* Add socket to the chain */
340 &GRE_SOCKHASH(&sc->gre_oip6.ip6_src), gs, chain);
344 /* Add softc to the socket's list */
345 CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
355 in6_gre_attach(struct gre_softc *sc)
360 if (sc->gre_options & GRE_UDPENCAP) {
361 sc->gre_csumflags = CSUM_UDP_IPV6;
362 sc->gre_hlen = sizeof(struct greudp6);
363 sc->gre_oip6.ip6_nxt = IPPROTO_UDP;
364 gh = &sc->gre_udp6hdr->gi6_gre;
365 gre_update_udphdr(sc, &sc->gre_udp6,
366 in6_cksum_pseudo(&sc->gre_oip6, 0, 0, 0));
368 sc->gre_hlen = sizeof(struct greip6);
369 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
370 gh = &sc->gre_ip6hdr->gi6_gre;
372 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
373 gre_update_hdr(sc, gh);
376 * If we return error, this means that sc is not linked,
377 * and caller should reset gre_family and free(sc->gre_hdr).
379 if (sc->gre_options & GRE_UDPENCAP) {
380 error = in6_gre_setup_socket(sc);
384 CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
385 CK_LIST_INSERT_HEAD(&GRE_SRCHASH(&sc->gre_oip6.ip6_src), sc, srchash);
387 /* Set IFF_DRV_RUNNING if interface is ready */
388 in6_gre_set_running(sc);
393 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
397 /* NOTE: we are protected with gre_ioctl_sx lock */
398 MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
399 MPASS(sc->gre_family == AF_INET6);
402 * If we are going to change encapsulation protocol, do check
403 * for duplicate tunnels. Return EEXIST here to do not confuse
406 if (cmd == GRESOPTS &&
407 (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
408 in6_gre_checkdup(sc, &sc->gre_oip6.ip6_src,
409 &sc->gre_oip6.ip6_dst, value) == EADDRNOTAVAIL)
412 CK_LIST_REMOVE(sc, chain);
413 CK_LIST_REMOVE(sc, srchash);
420 sc->gre_options = value;
423 sc->gre_port = value;
426 error = in6_gre_attach(sc);
429 free(sc->gre_hdr, M_GRE);
435 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
437 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
438 struct sockaddr_in6 *dst, *src;
442 /* NOTE: we are protected with gre_ioctl_sx lock */
445 case SIOCSIFPHYADDR_IN6:
446 src = &((struct in6_aliasreq *)data)->ifra_addr;
447 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
450 if (src->sin6_family != dst->sin6_family ||
451 src->sin6_family != AF_INET6 ||
452 src->sin6_len != dst->sin6_len ||
453 src->sin6_len != sizeof(*src))
455 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
456 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
457 error = EADDRNOTAVAIL;
461 * Check validity of the scope zone ID of the
462 * addresses, and convert it into the kernel
463 * internal form if necessary.
465 if ((error = sa6_embedscope(src, 0)) != 0 ||
466 (error = sa6_embedscope(dst, 0)) != 0)
469 if (V_ipv6_hashtbl == NULL) {
470 V_ipv6_hashtbl = gre_hashinit();
471 V_ipv6_srchashtbl = gre_hashinit();
472 V_ipv6_sockets = (struct gre_sockets *)gre_hashinit();
474 error = in6_gre_checkdup(sc, &src->sin6_addr,
475 &dst->sin6_addr, sc->gre_options);
476 if (error == EADDRNOTAVAIL)
478 if (error == EEXIST) {
479 /* Addresses are the same. Just return. */
483 ip6 = malloc(sizeof(struct greudp6) + 3 * sizeof(uint32_t),
484 M_GRE, M_WAITOK | M_ZERO);
485 ip6->ip6_src = src->sin6_addr;
486 ip6->ip6_dst = dst->sin6_addr;
487 if (sc->gre_family != 0) {
488 /* Detach existing tunnel first */
489 CK_LIST_REMOVE(sc, chain);
490 CK_LIST_REMOVE(sc, srchash);
492 free(sc->gre_hdr, M_GRE);
493 /* XXX: should we notify about link state change? */
495 sc->gre_family = AF_INET6;
498 sc->gre_iseq = UINT32_MAX;
499 error = in6_gre_attach(sc);
502 free(sc->gre_hdr, M_GRE);
505 case SIOCGIFPSRCADDR_IN6:
506 case SIOCGIFPDSTADDR_IN6:
507 if (sc->gre_family != AF_INET6) {
508 error = EADDRNOTAVAIL;
511 src = (struct sockaddr_in6 *)&ifr->ifr_addr;
512 memset(src, 0, sizeof(*src));
513 src->sin6_family = AF_INET6;
514 src->sin6_len = sizeof(*src);
515 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
516 sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst;
517 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
519 error = sa6_recoverscope(src);
521 memset(src, 0, sizeof(*src));
528 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused,
533 gi6 = mtod(m, struct greip6 *);
534 gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
535 gi6->gi6_ip6.ip6_flow |= flowid & IPV6_FLOWLABEL_MASK;
536 return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
539 static const struct srcaddrtab *ipv6_srcaddrtab = NULL;
540 static const struct encaptab *ecookie = NULL;
541 static const struct encap_config ipv6_encap_cfg = {
542 .proto = IPPROTO_GRE,
543 .min_length = sizeof(struct greip6) +
547 sizeof(struct ip6_hdr),
549 .exact_match = ENCAP_DRV_LOOKUP,
550 .lookup = in6_gre_lookup,
558 if (!IS_DEFAULT_VNET(curvnet))
560 ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gre_srcaddr,
562 ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
569 if (IS_DEFAULT_VNET(curvnet)) {
570 ip6_encap_detach(ecookie);
571 ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
573 if (V_ipv6_hashtbl != NULL) {
574 gre_hashdestroy(V_ipv6_hashtbl);
575 V_ipv6_hashtbl = NULL;
577 gre_hashdestroy(V_ipv6_srchashtbl);
578 gre_hashdestroy((struct gre_list *)V_ipv6_sockets);