1 /*- 2 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 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. 14 * 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. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_inet.h" 31 #include "opt_inet6.h" 32 33 #include <sys/param.h> 34 #include <sys/jail.h> 35 #include <sys/systm.h> 36 #include <sys/socket.h> 37 #include <sys/socketvar.h> 38 #include <sys/sockio.h> 39 #include <sys/mbuf.h> 40 #include <sys/errno.h> 41 #include <sys/kernel.h> 42 #include <sys/sysctl.h> 43 #include <sys/malloc.h> 44 #include <sys/proc.h> 45 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <net/if_private.h> 49 #include <net/vnet.h> 50 51 #include <netinet/in.h> 52 #ifdef INET 53 #include <net/ethernet.h> 54 #include <netinet/ip.h> 55 #endif 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> 66 67 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM; 68 #define V_ip6_gre_hlim VNET(ip6_gre_hlim) 69 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"); 73 74 struct in6_gre_socket { 75 struct gre_socket base; 76 struct in6_addr addr; /* scope zone id is embedded */ 77 }; 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) 92 93 static uint32_t 94 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst) 95 { 96 uint32_t ret; 97 98 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT); 99 return (fnv_32_buf(dst, sizeof(*dst), ret)); 100 } 101 102 static struct gre_socket* 103 in6_gre_lookup_socket(const struct in6_addr *addr) 104 { 105 struct gre_socket *gs; 106 struct in6_gre_socket *s; 107 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)) 111 break; 112 } 113 return (gs); 114 } 115 116 static int 117 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src, 118 const struct in6_addr *dst, uint32_t opts) 119 { 120 struct gre_list *head; 121 struct gre_softc *tmp; 122 struct gre_socket *gs; 123 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)) 128 return (EEXIST); 129 130 if (opts & GRE_UDPENCAP) { 131 gs = in6_gre_lookup_socket(src); 132 if (gs == NULL) 133 return (0); 134 head = &gs->list; 135 } else 136 head = &GRE_HASH(src, dst); 137 138 CK_LIST_FOREACH(tmp, head, chain) { 139 if (tmp == sc) 140 continue; 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); 144 } 145 return (0); 146 } 147 148 static int 149 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg) 150 { 151 const struct ip6_hdr *ip6; 152 struct gre_softc *sc; 153 154 if (V_ipv6_hashtbl == NULL) 155 return (0); 156 157 NET_EPOCH_ASSERT(); 158 ip6 = mtod(m, const struct ip6_hdr *); 159 CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) { 160 /* 161 * This is an inbound packet, its ip6_dst is source address 162 * in softc. 163 */ 164 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, 165 &ip6->ip6_dst) && 166 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, 167 &ip6->ip6_src)) { 168 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) 169 return (0); 170 *arg = sc; 171 return (ENCAP_DRV_LOOKUP); 172 } 173 } 174 return (0); 175 } 176 177 /* 178 * Check that ingress address belongs to local host. 179 */ 180 static void 181 in6_gre_set_running(struct gre_softc *sc) 182 { 183 184 if (in6_localip(&sc->gre_oip6.ip6_src)) 185 GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING; 186 else 187 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING; 188 } 189 190 /* 191 * ifaddr_event handler. 192 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent 193 * source address spoofing. 194 */ 195 static void 196 in6_gre_srcaddr(void *arg __unused, const struct sockaddr *sa, 197 int event __unused) 198 { 199 const struct sockaddr_in6 *sin; 200 struct gre_softc *sc; 201 202 /* Check that VNET is ready */ 203 if (V_ipv6_hashtbl == NULL) 204 return; 205 206 NET_EPOCH_ASSERT(); 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) 211 continue; 212 in6_gre_set_running(sc); 213 } 214 } 215 216 static bool 217 in6_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp, 218 const struct sockaddr *sa, void *ctx) 219 { 220 struct gre_socket *gs; 221 struct gre_softc *sc; 222 struct sockaddr_in6 dst; 223 224 NET_EPOCH_ASSERT(); 225 226 gs = (struct gre_socket *)ctx; 227 dst = *(const struct sockaddr_in6 *)sa; 228 if (sa6_embedscope(&dst, 0)) { 229 m_freem(m); 230 return (true); 231 } 232 CK_LIST_FOREACH(sc, &gs->list, chain) { 233 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &dst.sin6_addr)) 234 break; 235 } 236 if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){ 237 gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc); 238 return (true); 239 } 240 m_freem(m); 241 242 return (true); 243 } 244 245 static int 246 in6_gre_setup_socket(struct gre_softc *sc) 247 { 248 struct sockopt sopt; 249 struct sockaddr_in6 sin6; 250 struct in6_gre_socket *s; 251 struct gre_socket *gs; 252 int error, value; 253 254 /* 255 * NOTE: we are protected with gre_ioctl_sx lock. 256 * 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 260 * and close socket. 261 */ 262 gs = sc->gre_so; 263 if (gs != NULL) { 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); 268 soclose(gs->so); 269 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx); 270 } 271 gs = sc->gre_so = NULL; 272 } 273 } 274 275 if (gs == NULL) { 276 /* 277 * Check that socket for given address is already 278 * configured. 279 */ 280 gs = in6_gre_lookup_socket(&sc->gre_oip6.ip6_src); 281 if (gs == NULL) { 282 s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO); 283 s->addr = sc->gre_oip6.ip6_src; 284 gs = &s->base; 285 286 error = socreate(sc->gre_family, &gs->so, 287 SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred, 288 curthread); 289 if (error != 0) { 290 if_printf(GRE2IFP(sc), 291 "cannot create socket: %d\n", error); 292 free(s, M_GRE); 293 return (error); 294 } 295 296 error = udp_set_kernel_tunneling(gs->so, 297 in6_gre_udp_input, NULL, gs); 298 if (error != 0) { 299 if_printf(GRE2IFP(sc), 300 "cannot set UDP tunneling: %d\n", error); 301 goto fail; 302 } 303 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); 310 value = 1; 311 error = sosetopt(gs->so, &sopt); 312 if (error != 0) { 313 if_printf(GRE2IFP(sc), 314 "cannot set IPV6_BINDANY opt: %d\n", 315 error); 316 goto fail; 317 } 318 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); 325 if (error != 0) { 326 if_printf(GRE2IFP(sc), 327 "cannot determine scope zone id: %d\n", 328 error); 329 goto fail; 330 } 331 error = sobind(gs->so, (struct sockaddr *)&sin6, 332 curthread); 333 if (error != 0) { 334 if_printf(GRE2IFP(sc), 335 "cannot bind socket: %d\n", error); 336 goto fail; 337 } 338 /* Add socket to the chain */ 339 CK_LIST_INSERT_HEAD( 340 &GRE_SOCKHASH(&sc->gre_oip6.ip6_src), gs, chain); 341 } 342 } 343 344 /* Add softc to the socket's list */ 345 CK_LIST_INSERT_HEAD(&gs->list, sc, chain); 346 sc->gre_so = gs; 347 return (0); 348 fail: 349 soclose(gs->so); 350 free(s, M_GRE); 351 return (error); 352 } 353 354 static int 355 in6_gre_attach(struct gre_softc *sc) 356 { 357 struct grehdr *gh; 358 int error; 359 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)); 367 } else { 368 sc->gre_hlen = sizeof(struct greip6); 369 sc->gre_oip6.ip6_nxt = IPPROTO_GRE; 370 gh = &sc->gre_ip6hdr->gi6_gre; 371 } 372 sc->gre_oip6.ip6_vfc = IPV6_VERSION; 373 gre_update_hdr(sc, gh); 374 375 /* 376 * If we return error, this means that sc is not linked, 377 * and caller should reset gre_family and free(sc->gre_hdr). 378 */ 379 if (sc->gre_options & GRE_UDPENCAP) { 380 error = in6_gre_setup_socket(sc); 381 if (error != 0) 382 return (error); 383 } else 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); 386 387 /* Set IFF_DRV_RUNNING if interface is ready */ 388 in6_gre_set_running(sc); 389 return (0); 390 } 391 392 int 393 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value) 394 { 395 int error; 396 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); 400 401 /* 402 * If we are going to change encapsulation protocol, do check 403 * for duplicate tunnels. Return EEXIST here to do not confuse 404 * user. 405 */ 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) 410 return (EEXIST); 411 412 CK_LIST_REMOVE(sc, chain); 413 CK_LIST_REMOVE(sc, srchash); 414 GRE_WAIT(); 415 switch (cmd) { 416 case GRESKEY: 417 sc->gre_key = value; 418 break; 419 case GRESOPTS: 420 sc->gre_options = value; 421 break; 422 case GRESPORT: 423 sc->gre_port = value; 424 break; 425 } 426 error = in6_gre_attach(sc); 427 if (error != 0) { 428 sc->gre_family = 0; 429 free(sc->gre_hdr, M_GRE); 430 } 431 return (error); 432 } 433 434 int 435 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data) 436 { 437 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 438 struct sockaddr_in6 *dst, *src; 439 struct ip6_hdr *ip6; 440 int error; 441 442 /* NOTE: we are protected with gre_ioctl_sx lock */ 443 error = EINVAL; 444 switch (cmd) { 445 case SIOCSIFPHYADDR_IN6: 446 src = &((struct in6_aliasreq *)data)->ifra_addr; 447 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr; 448 449 /* sanity checks */ 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)) 454 break; 455 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) || 456 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) { 457 error = EADDRNOTAVAIL; 458 break; 459 } 460 /* 461 * Check validity of the scope zone ID of the 462 * addresses, and convert it into the kernel 463 * internal form if necessary. 464 */ 465 if ((error = sa6_embedscope(src, 0)) != 0 || 466 (error = sa6_embedscope(dst, 0)) != 0) 467 break; 468 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(); 473 } 474 error = in6_gre_checkdup(sc, &src->sin6_addr, 475 &dst->sin6_addr, sc->gre_options); 476 if (error == EADDRNOTAVAIL) 477 break; 478 if (error == EEXIST) { 479 /* Addresses are the same. Just return. */ 480 error = 0; 481 break; 482 } 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); 491 GRE_WAIT(); 492 free(sc->gre_hdr, M_GRE); 493 /* XXX: should we notify about link state change? */ 494 } 495 sc->gre_family = AF_INET6; 496 sc->gre_hdr = ip6; 497 sc->gre_oseq = 0; 498 sc->gre_iseq = UINT32_MAX; 499 error = in6_gre_attach(sc); 500 if (error != 0) { 501 sc->gre_family = 0; 502 free(sc->gre_hdr, M_GRE); 503 } 504 break; 505 case SIOCGIFPSRCADDR_IN6: 506 case SIOCGIFPDSTADDR_IN6: 507 if (sc->gre_family != AF_INET6) { 508 error = EADDRNOTAVAIL; 509 break; 510 } 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); 518 if (error == 0) 519 error = sa6_recoverscope(src); 520 if (error != 0) 521 memset(src, 0, sizeof(*src)); 522 break; 523 } 524 return (error); 525 } 526 527 int 528 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused, 529 uint32_t flowid) 530 { 531 struct greip6 *gi6; 532 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)); 537 } 538 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) + 544 #ifdef INET 545 sizeof(struct ip), 546 #else 547 sizeof(struct ip6_hdr), 548 #endif 549 .exact_match = ENCAP_DRV_LOOKUP, 550 .lookup = in6_gre_lookup, 551 .input = gre_input 552 }; 553 554 void 555 in6_gre_init(void) 556 { 557 558 if (!IS_DEFAULT_VNET(curvnet)) 559 return; 560 ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gre_srcaddr, 561 NULL, M_WAITOK); 562 ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK); 563 } 564 565 void 566 in6_gre_uninit(void) 567 { 568 569 if (IS_DEFAULT_VNET(curvnet)) { 570 ip6_encap_detach(ecookie); 571 ip6_encap_unregister_srcaddr(ipv6_srcaddrtab); 572 } 573 if (V_ipv6_hashtbl != NULL) { 574 gre_hashdestroy(V_ipv6_hashtbl); 575 V_ipv6_hashtbl = NULL; 576 GRE_WAIT(); 577 gre_hashdestroy(V_ipv6_srchashtbl); 578 gre_hashdestroy((struct gre_list *)V_ipv6_sockets); 579 } 580 } 581