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>
29 #include "opt_inet6.h"
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/sockio.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
45 #include <net/if_var.h>
46 #include <net/if_private.h>
49 #include <netinet/in.h>
51 #include <net/ethernet.h>
52 #include <netinet/ip.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_encap.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/ip6.h>
58 #include <netinet/udp.h>
59 #include <netinet/udp_var.h>
60 #include <netinet6/ip6_var.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet6/scope6_var.h>
63 #include <net/if_gre.h>
65 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
66 #define V_ip6_gre_hlim VNET(ip6_gre_hlim)
68 SYSCTL_DECL(_net_inet6_ip6);
69 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
70 &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
72 struct in6_gre_socket {
73 struct gre_socket base;
74 struct in6_addr addr; /* scope zone id is embedded */
76 VNET_DEFINE_STATIC(struct gre_sockets *, ipv6_sockets) = NULL;
77 VNET_DEFINE_STATIC(struct gre_list *, ipv6_hashtbl) = NULL;
78 VNET_DEFINE_STATIC(struct gre_list *, ipv6_srchashtbl) = NULL;
79 #define V_ipv6_sockets VNET(ipv6_sockets)
80 #define V_ipv6_hashtbl VNET(ipv6_hashtbl)
81 #define V_ipv6_srchashtbl VNET(ipv6_srchashtbl)
82 #define GRE_HASH(src, dst) (V_ipv6_hashtbl[\
83 in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
84 #define GRE_SRCHASH(src) (V_ipv6_srchashtbl[\
85 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
86 #define GRE_SOCKHASH(src) (V_ipv6_sockets[\
87 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
88 #define GRE_HASH_SC(sc) GRE_HASH(&(sc)->gre_oip6.ip6_src,\
89 &(sc)->gre_oip6.ip6_dst)
92 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst)
96 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
97 return (fnv_32_buf(dst, sizeof(*dst), ret));
100 static struct gre_socket*
101 in6_gre_lookup_socket(const struct in6_addr *addr)
103 struct gre_socket *gs;
104 struct in6_gre_socket *s;
106 CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
107 s = __containerof(gs, struct in6_gre_socket, base);
108 if (IN6_ARE_ADDR_EQUAL(&s->addr, addr))
115 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src,
116 const struct in6_addr *dst, uint32_t opts)
118 struct gre_list *head;
119 struct gre_softc *tmp;
120 struct gre_socket *gs;
122 if (sc->gre_family == AF_INET6 &&
123 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) &&
124 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst) &&
125 (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP))
128 if (opts & GRE_UDPENCAP) {
129 gs = in6_gre_lookup_socket(src);
134 head = &GRE_HASH(src, dst);
136 CK_LIST_FOREACH(tmp, head, chain) {
139 if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) &&
140 IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst))
141 return (EADDRNOTAVAIL);
147 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
149 const struct ip6_hdr *ip6;
150 struct gre_softc *sc;
152 if (V_ipv6_hashtbl == NULL)
156 ip6 = mtod(m, const struct ip6_hdr *);
157 CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
159 * This is an inbound packet, its ip6_dst is source address
162 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
164 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst,
166 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
169 return (ENCAP_DRV_LOOKUP);
176 * Check that ingress address belongs to local host.
179 in6_gre_set_running(struct gre_softc *sc)
182 if (in6_localip(&sc->gre_oip6.ip6_src))
183 GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
185 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
189 * ifaddr_event handler.
190 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
191 * source address spoofing.
194 in6_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
197 const struct sockaddr_in6 *sin;
198 struct gre_softc *sc;
200 /* Check that VNET is ready */
201 if (V_ipv6_hashtbl == NULL)
205 sin = (const struct sockaddr_in6 *)sa;
206 CK_LIST_FOREACH(sc, &GRE_SRCHASH(&sin->sin6_addr), srchash) {
207 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
208 &sin->sin6_addr) == 0)
210 in6_gre_set_running(sc);
215 in6_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
216 const struct sockaddr *sa, void *ctx)
218 struct gre_socket *gs;
219 struct gre_softc *sc;
220 struct sockaddr_in6 dst;
224 gs = (struct gre_socket *)ctx;
225 dst = *(const struct sockaddr_in6 *)sa;
226 if (sa6_embedscope(&dst, 0)) {
230 CK_LIST_FOREACH(sc, &gs->list, chain) {
231 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &dst.sin6_addr))
234 if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
235 gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
244 in6_gre_setup_socket(struct gre_softc *sc)
247 struct sockaddr_in6 sin6;
248 struct in6_gre_socket *s;
249 struct gre_socket *gs;
253 * NOTE: we are protected with gre_ioctl_sx lock.
255 * First check that socket is already configured.
256 * If so, check that source address was not changed.
257 * If address is different, check that there are no other tunnels
262 s = __containerof(gs, struct in6_gre_socket, base);
263 if (!IN6_ARE_ADDR_EQUAL(&s->addr, &sc->gre_oip6.ip6_src)) {
264 if (CK_LIST_EMPTY(&gs->list)) {
265 CK_LIST_REMOVE(gs, chain);
267 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
269 gs = sc->gre_so = NULL;
275 * Check that socket for given address is already
278 gs = in6_gre_lookup_socket(&sc->gre_oip6.ip6_src);
280 s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
281 s->addr = sc->gre_oip6.ip6_src;
284 error = socreate(sc->gre_family, &gs->so,
285 SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
288 if_printf(GRE2IFP(sc),
289 "cannot create socket: %d\n", error);
294 error = udp_set_kernel_tunneling(gs->so,
295 in6_gre_udp_input, NULL, gs);
297 if_printf(GRE2IFP(sc),
298 "cannot set UDP tunneling: %d\n", error);
302 memset(&sopt, 0, sizeof(sopt));
303 sopt.sopt_dir = SOPT_SET;
304 sopt.sopt_level = IPPROTO_IPV6;
305 sopt.sopt_name = IPV6_BINDANY;
306 sopt.sopt_val = &value;
307 sopt.sopt_valsize = sizeof(value);
309 error = sosetopt(gs->so, &sopt);
311 if_printf(GRE2IFP(sc),
312 "cannot set IPV6_BINDANY opt: %d\n",
317 memset(&sin6, 0, sizeof(sin6));
318 sin6.sin6_family = AF_INET6;
319 sin6.sin6_len = sizeof(sin6);
320 sin6.sin6_addr = sc->gre_oip6.ip6_src;
321 sin6.sin6_port = htons(GRE_UDPPORT);
322 error = sa6_recoverscope(&sin6);
324 if_printf(GRE2IFP(sc),
325 "cannot determine scope zone id: %d\n",
329 error = sobind(gs->so, (struct sockaddr *)&sin6,
332 if_printf(GRE2IFP(sc),
333 "cannot bind socket: %d\n", error);
336 /* Add socket to the chain */
338 &GRE_SOCKHASH(&sc->gre_oip6.ip6_src), gs, chain);
342 /* Add softc to the socket's list */
343 CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
353 in6_gre_attach(struct gre_softc *sc)
358 if (sc->gre_options & GRE_UDPENCAP) {
359 sc->gre_csumflags = CSUM_UDP_IPV6;
360 sc->gre_hlen = sizeof(struct greudp6);
361 sc->gre_oip6.ip6_nxt = IPPROTO_UDP;
362 gh = &sc->gre_udp6hdr->gi6_gre;
363 gre_update_udphdr(sc, &sc->gre_udp6,
364 in6_cksum_pseudo(&sc->gre_oip6, 0, 0, 0));
366 sc->gre_hlen = sizeof(struct greip6);
367 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
368 gh = &sc->gre_ip6hdr->gi6_gre;
370 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
371 gre_update_hdr(sc, gh);
374 * If we return error, this means that sc is not linked,
375 * and caller should reset gre_family and free(sc->gre_hdr).
377 if (sc->gre_options & GRE_UDPENCAP) {
378 error = in6_gre_setup_socket(sc);
382 CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
383 CK_LIST_INSERT_HEAD(&GRE_SRCHASH(&sc->gre_oip6.ip6_src), sc, srchash);
385 /* Set IFF_DRV_RUNNING if interface is ready */
386 in6_gre_set_running(sc);
391 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
395 /* NOTE: we are protected with gre_ioctl_sx lock */
396 MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
397 MPASS(sc->gre_family == AF_INET6);
400 * If we are going to change encapsulation protocol, do check
401 * for duplicate tunnels. Return EEXIST here to do not confuse
404 if (cmd == GRESOPTS &&
405 (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
406 in6_gre_checkdup(sc, &sc->gre_oip6.ip6_src,
407 &sc->gre_oip6.ip6_dst, value) == EADDRNOTAVAIL)
410 CK_LIST_REMOVE(sc, chain);
411 CK_LIST_REMOVE(sc, srchash);
418 sc->gre_options = value;
421 sc->gre_port = value;
424 error = in6_gre_attach(sc);
427 free(sc->gre_hdr, M_GRE);
433 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
435 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
436 struct sockaddr_in6 *dst, *src;
440 /* NOTE: we are protected with gre_ioctl_sx lock */
443 case SIOCSIFPHYADDR_IN6:
444 src = &((struct in6_aliasreq *)data)->ifra_addr;
445 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
448 if (src->sin6_family != dst->sin6_family ||
449 src->sin6_family != AF_INET6 ||
450 src->sin6_len != dst->sin6_len ||
451 src->sin6_len != sizeof(*src))
453 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
454 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
455 error = EADDRNOTAVAIL;
459 * Check validity of the scope zone ID of the
460 * addresses, and convert it into the kernel
461 * internal form if necessary.
463 if ((error = sa6_embedscope(src, 0)) != 0 ||
464 (error = sa6_embedscope(dst, 0)) != 0)
467 if (V_ipv6_hashtbl == NULL) {
468 V_ipv6_hashtbl = gre_hashinit();
469 V_ipv6_srchashtbl = gre_hashinit();
470 V_ipv6_sockets = (struct gre_sockets *)gre_hashinit();
472 error = in6_gre_checkdup(sc, &src->sin6_addr,
473 &dst->sin6_addr, sc->gre_options);
474 if (error == EADDRNOTAVAIL)
476 if (error == EEXIST) {
477 /* Addresses are the same. Just return. */
481 ip6 = malloc(sizeof(struct greudp6) + 3 * sizeof(uint32_t),
482 M_GRE, M_WAITOK | M_ZERO);
483 ip6->ip6_src = src->sin6_addr;
484 ip6->ip6_dst = dst->sin6_addr;
485 if (sc->gre_family != 0) {
486 /* Detach existing tunnel first */
487 CK_LIST_REMOVE(sc, chain);
488 CK_LIST_REMOVE(sc, srchash);
490 free(sc->gre_hdr, M_GRE);
491 /* XXX: should we notify about link state change? */
493 sc->gre_family = AF_INET6;
496 sc->gre_iseq = UINT32_MAX;
497 error = in6_gre_attach(sc);
500 free(sc->gre_hdr, M_GRE);
503 case SIOCGIFPSRCADDR_IN6:
504 case SIOCGIFPDSTADDR_IN6:
505 if (sc->gre_family != AF_INET6) {
506 error = EADDRNOTAVAIL;
509 src = (struct sockaddr_in6 *)&ifr->ifr_addr;
510 memset(src, 0, sizeof(*src));
511 src->sin6_family = AF_INET6;
512 src->sin6_len = sizeof(*src);
513 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
514 sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst;
515 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
517 error = sa6_recoverscope(src);
519 memset(src, 0, sizeof(*src));
526 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused,
531 gi6 = mtod(m, struct greip6 *);
532 gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
533 gi6->gi6_ip6.ip6_flow |= flowid & IPV6_FLOWLABEL_MASK;
534 return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
537 static const struct srcaddrtab *ipv6_srcaddrtab = NULL;
538 static const struct encaptab *ecookie = NULL;
539 static const struct encap_config ipv6_encap_cfg = {
540 .proto = IPPROTO_GRE,
541 .min_length = sizeof(struct greip6) +
545 sizeof(struct ip6_hdr),
547 .exact_match = ENCAP_DRV_LOOKUP,
548 .lookup = in6_gre_lookup,
556 if (!IS_DEFAULT_VNET(curvnet))
558 ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gre_srcaddr,
560 ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
567 if (IS_DEFAULT_VNET(curvnet)) {
568 ip6_encap_detach(ecookie);
569 ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
571 if (V_ipv6_hashtbl != NULL) {
572 gre_hashdestroy(V_ipv6_hashtbl);
573 V_ipv6_hashtbl = NULL;
575 gre_hashdestroy(V_ipv6_srchashtbl);
576 gre_hashdestroy((struct gre_list *)V_ipv6_sockets);