1 /** 2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org> 3 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License v2 as published by 7 * the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 */ 15 16 #include <errno.h> 17 #include <fcntl.h> 18 #include <signal.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <unistd.h> 22 #include <stdbool.h> 23 #include <arpa/inet.h> 24 #include <net/route.h> 25 26 #include <libubox/utils.h> 27 28 #include "router.h" 29 #include "odhcpd.h" 30 #include "statefiles.h" 31 32 33 static void forward_router_solicitation(const struct interface *iface); 34 static void forward_router_advertisement(const struct interface *iface, uint8_t *data, size_t len); 35 36 static void handle_icmpv6(void *addr, void *data, size_t len, 37 struct interface *iface, void *dest); 38 static void trigger_router_advert(struct uloop_timeout *event); 39 static void router_netevent_cb(unsigned long event, struct netevent_handler_info *info); 40 41 static struct netevent_handler router_netevent_handler = { .cb = router_netevent_cb, }; 42 43 static FILE *fp_route = NULL; 44 45 46 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX) 47 48 /* Shutdown helper: close fp_route if open. Safe to call multiple times. */ 49 static void router_shutdown(void) 50 { 51 if (fp_route) { 52 fclose(fp_route); 53 fp_route = NULL; 54 } 55 } 56 57 int router_init(void) 58 { 59 int ret = 0; 60 61 if (!(fp_route = fopen("/proc/net/ipv6_route", "r"))) { 62 error("fopen(/proc/net/ipv6_route): %m"); 63 ret = -1; 64 goto out; 65 } 66 67 if (netlink_add_netevent_handler(&router_netevent_handler) < 0) { 68 error("Failed to add netevent handler"); 69 ret = -1; 70 } 71 72 atexit(router_shutdown); 73 74 out: 75 if (ret < 0) 76 router_shutdown(); 77 78 return ret; 79 } 80 81 82 int router_setup_interface(struct interface *iface, bool enable) 83 { 84 int ret = 0; 85 86 enable = enable && (iface->ra != MODE_DISABLED); 87 88 if (!fp_route) { 89 ret = -1; 90 goto out; 91 } 92 93 94 if (!enable && iface->router_event.uloop.fd >= 0) { 95 if (!iface->master) { 96 uloop_timeout_cancel(&iface->timer_rs); 97 iface->timer_rs.cb = NULL; 98 99 trigger_router_advert(&iface->timer_rs); 100 } 101 102 uloop_fd_delete(&iface->router_event.uloop); 103 close(iface->router_event.uloop.fd); 104 iface->router_event.uloop.fd = -1; 105 } else if (enable) { 106 struct icmp6_filter filt; 107 struct ipv6_mreq mreq; 108 int val = 2; 109 110 if (iface->router_event.uloop.fd < 0) { 111 /* Open ICMPv6 socket */ 112 iface->router_event.uloop.fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, 113 IPPROTO_ICMPV6); 114 if (iface->router_event.uloop.fd < 0) { 115 error("socket(AF_INET6): %m"); 116 ret = -1; 117 goto out; 118 } 119 120 if (setsockopt(iface->router_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE, 121 iface->ifname, strlen(iface->ifname)) < 0) { 122 error("setsockopt(SO_BINDTODEVICE): %m"); 123 ret = -1; 124 goto out; 125 } 126 127 /* Let the kernel compute our checksums */ 128 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_RAW, IPV6_CHECKSUM, 129 &val, sizeof(val)) < 0) { 130 error("setsockopt(IPV6_CHECKSUM): %m"); 131 ret = -1; 132 goto out; 133 } 134 135 /* This is required by RFC 4861 */ 136 val = 255; 137 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 138 &val, sizeof(val)) < 0) { 139 error("setsockopt(IPV6_MULTICAST_HOPS): %m"); 140 ret = -1; 141 goto out; 142 } 143 144 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 145 &val, sizeof(val)) < 0) { 146 error("setsockopt(IPV6_UNICAST_HOPS): %m"); 147 ret = -1; 148 goto out; 149 } 150 151 /* We need to know the source interface */ 152 val = 1; 153 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, 154 &val, sizeof(val)) < 0) { 155 error("setsockopt(IPV6_RECVPKTINFO): %m"); 156 ret = -1; 157 goto out; 158 } 159 160 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, 161 &val, sizeof(val)) < 0) { 162 error("setsockopt(IPV6_RECVHOPLIMIT): %m"); 163 ret = -1; 164 goto out; 165 } 166 167 /* Don't loop back */ 168 val = 0; 169 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 170 &val, sizeof(val)) < 0) { 171 error("setsockopt(IPV6_MULTICAST_LOOP): %m"); 172 ret = -1; 173 goto out; 174 } 175 176 /* Filter ICMPv6 package types */ 177 ICMP6_FILTER_SETBLOCKALL(&filt); 178 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt); 179 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt); 180 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_ICMPV6, ICMP6_FILTER, 181 &filt, sizeof(filt)) < 0) { 182 error("setsockopt(ICMP6_FILTER): %m"); 183 ret = -1; 184 goto out; 185 } 186 187 iface->router_event.handle_dgram = handle_icmpv6; 188 iface->ra_sent = 0; 189 odhcpd_register(&iface->router_event); 190 } else { 191 uloop_timeout_cancel(&iface->timer_rs); 192 iface->timer_rs.cb = NULL; 193 194 memset(&mreq, 0, sizeof(mreq)); 195 mreq.ipv6mr_interface = iface->ifindex; 196 inet_pton(AF_INET6, ALL_IPV6_NODES, &mreq.ipv6mr_multiaddr); 197 setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, 198 &mreq, sizeof(mreq)); 199 200 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &mreq.ipv6mr_multiaddr); 201 setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, 202 &mreq, sizeof(mreq)); 203 } 204 205 memset(&mreq, 0, sizeof(mreq)); 206 mreq.ipv6mr_interface = iface->ifindex; 207 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &mreq.ipv6mr_multiaddr); 208 209 if (iface->ra == MODE_RELAY && iface->master) { 210 inet_pton(AF_INET6, ALL_IPV6_NODES, &mreq.ipv6mr_multiaddr); 211 forward_router_solicitation(iface); 212 } else if (iface->ra == MODE_SERVER) { 213 iface->timer_rs.cb = trigger_router_advert; 214 uloop_timeout_set(&iface->timer_rs, 1000); 215 } 216 217 if (setsockopt(iface->router_event.uloop.fd, IPPROTO_IPV6, 218 IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { 219 ret = -1; 220 error("setsockopt(IPV6_ADD_MEMBERSHIP): %m"); 221 goto out; 222 } 223 } 224 out: 225 if (ret < 0 && iface->router_event.uloop.fd >= 0) { 226 if (iface->router_event.uloop.registered) 227 uloop_fd_delete(&iface->router_event.uloop); 228 229 close(iface->router_event.uloop.fd); 230 iface->router_event.uloop.fd = -1; 231 } 232 233 return ret; 234 } 235 236 237 static void router_netevent_cb(unsigned long event, struct netevent_handler_info *info) 238 { 239 struct interface *iface; 240 241 switch (event) { 242 case NETEV_IFINDEX_CHANGE: 243 iface = info->iface; 244 if (iface && iface->router_event.uloop.fd >= 0) { 245 if (iface->router_event.uloop.registered) 246 uloop_fd_delete(&iface->router_event.uloop); 247 248 close(iface->router_event.uloop.fd); 249 iface->router_event.uloop.fd = -1; 250 } 251 break; 252 case NETEV_ROUTE6_ADD: 253 case NETEV_ROUTE6_DEL: 254 if (info->rt.dst_len) 255 break; 256 257 avl_for_each_element(&interfaces, iface, avl) { 258 if (iface->ra == MODE_SERVER && !iface->master) 259 uloop_timeout_set(&iface->timer_rs, 1000); 260 } 261 break; 262 case NETEV_ADDR6LIST_CHANGE: 263 iface = info->iface; 264 if (iface && iface->ra == MODE_SERVER && !iface->master) 265 uloop_timeout_set(&iface->timer_rs, 1000); 266 break; 267 default: 268 break; 269 } 270 } 271 272 273 static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len) 274 { 275 struct icmp6_hdr *hdr = (struct icmp6_hdr *)data; 276 struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len]; 277 278 /* Hoplimit is already checked in odhcpd_receive_packets */ 279 if (len < sizeof(*hdr) || hdr->icmp6_code) 280 return false; 281 282 switch (hdr->icmp6_type) { 283 case ND_ROUTER_ADVERT: 284 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr)) 285 return false; 286 287 opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1); 288 break; 289 290 case ND_ROUTER_SOLICIT: 291 opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1); 292 break; 293 294 default: 295 return false; 296 } 297 298 icmpv6_for_each_option(opt, opt, end) 299 if (opt->type == ND_OPT_SOURCE_LINKADDR && 300 IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) && 301 hdr->icmp6_type == ND_ROUTER_SOLICIT) 302 return false; 303 304 /* Check all options parsed successfully */ 305 return opt == end; 306 } 307 308 309 /* Detect whether a default route exists, also find the source prefixes */ 310 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len) 311 { 312 struct odhcpd_ipaddr p = { 313 .addr.in6 = IN6ADDR_ANY_INIT, 314 .prefix_len = 0, 315 .dprefix_len = 0, 316 .preferred_lt = 0, 317 .valid_lt = 0 318 }; 319 bool found_default = false; 320 char line[512], ifname[16]; 321 322 rewind(fp_route); 323 324 while (fgets(line, sizeof(line), fp_route)) { 325 uint32_t rflags; 326 if (sscanf(line, "00000000000000000000000000000000 00 " 327 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname) && 328 strcmp(ifname, "lo")) { 329 found_default = true; 330 } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s " 331 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo", 332 &p.addr.in6.s6_addr32[0], &p.addr.in6.s6_addr32[1], &p.prefix_len, &rflags) && 333 p.prefix_len > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) { 334 // Find source prefixes by scanning through unreachable-routes 335 p.addr.in6.s6_addr32[0] = htonl(p.addr.in6.s6_addr32[0]); 336 p.addr.in6.s6_addr32[1] = htonl(p.addr.in6.s6_addr32[1]); 337 338 for (ssize_t i = 0; i < len; ++i) { 339 if (n[i].prefix_len <= 64 && n[i].prefix_len >= p.prefix_len && 340 !odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix_len)) { 341 n[i].dprefix_len = p.prefix_len; 342 break; 343 } 344 } 345 } 346 } 347 348 return found_default; 349 } 350 351 static int calc_adv_interval(struct interface *iface, uint32_t lowest_found_lifetime, 352 uint32_t *maxival) 353 { 354 uint32_t minival = iface->ra_mininterval; 355 int msecs; 356 357 *maxival = iface->ra_maxinterval; 358 359 if (*maxival > lowest_found_lifetime) 360 *maxival = lowest_found_lifetime; 361 362 odhcpd_urandom(&msecs, sizeof(msecs)); 363 msecs = (labs(msecs) % ((*maxival != minival) ? (*maxival - minival)*1000 : 500)) + 364 minival*1000; 365 366 /* RFC 2461 6.2.4 For the first MAX_INITIAL_RTR_ADVERTISEMENTS advertisements 367 * if the timer is bigger than MAX_INITIAL_RTR_ADVERT_INTERVAL it should be 368 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL 369 * Off by one as an initial interval timer has already expired 370 */ 371 if ((iface->ra_sent + 1) < MaxInitialRtAdvs && msecs > MaxInitialRtrAdvInterval*1000) 372 msecs = MaxInitialRtrAdvInterval*1000; 373 374 return msecs; 375 } 376 377 static uint32_t calc_ra_lifetime(struct interface *iface, uint32_t maxival) 378 { 379 uint32_t lifetime = iface->ra_lifetime; 380 381 /* RFC4861 §6.2.1: zero, or at least MaxRtrAdvInterval */ 382 if (lifetime > 0 && lifetime < maxival) 383 lifetime = maxival; 384 385 return lifetime; 386 } 387 388 enum { 389 IOV_RA_ADV=0, 390 IOV_RA_PFXS, 391 IOV_RA_ROUTES, 392 IOV_RA_DNS, 393 IOV_RA_SEARCH, 394 IOV_RA_PREF64, 395 IOV_RA_DNR, 396 IOV_RA_ADV_INTERVAL, 397 IOV_RA_CAPT_PORTAL, 398 IOV_RA_TOTAL, 399 }; 400 401 struct adv_msg { 402 struct nd_router_advert h; 403 struct icmpv6_opt lladdr; 404 struct nd_opt_mtu mtu; 405 }; 406 407 struct nd_opt_dns_server { 408 uint8_t type; 409 uint8_t len; 410 uint8_t pad; 411 uint8_t pad2; 412 uint32_t lifetime; 413 struct in6_addr addr[]; 414 }; 415 416 struct nd_opt_search_list { 417 uint8_t type; 418 uint8_t len; 419 uint16_t reserved; 420 uint32_t lifetime; 421 uint8_t name[]; 422 } _o_packed; 423 424 struct nd_opt_route_info { 425 uint8_t type; 426 uint8_t len; 427 uint8_t prefix_len; 428 uint8_t flags; 429 uint32_t lifetime; 430 uint32_t addr[4]; 431 }; 432 433 struct nd_opt_pref64_info { 434 uint8_t type; 435 uint8_t len; 436 uint16_t lifetime_plc; 437 uint32_t prefix[3]; 438 }; 439 440 struct nd_opt_dnr_info { 441 uint8_t type; 442 uint8_t len; 443 uint16_t priority; 444 uint32_t lifetime; 445 uint16_t adn_len; 446 uint8_t body[]; 447 }; 448 449 struct nd_opt_capt_portal { 450 uint8_t type; 451 uint8_t len; 452 uint8_t data[]; 453 }; 454 455 /* IPv6 RA PIOs */ 456 inline static int router_compare_pio_addr(const struct ra_pio *pio, const struct odhcpd_ipaddr *addr) 457 { 458 uint8_t cmp_len = max(64, max(pio->length, addr->prefix_len)); 459 460 return odhcpd_bmemcmp(&pio->prefix, &addr->addr.in6, cmp_len); 461 } 462 463 static struct ra_pio *router_find_ra_pio(struct interface *iface, 464 struct odhcpd_ipaddr *addr) 465 { 466 for (size_t i = 0; i < iface->pio_cnt; i++) { 467 struct ra_pio *cur_pio = &iface->pios[i]; 468 469 if (!router_compare_pio_addr(cur_pio, addr)) 470 return cur_pio; 471 } 472 473 return NULL; 474 } 475 476 static void router_add_ra_pio(struct interface *iface, 477 struct odhcpd_ipaddr *addr) 478 { 479 char ipv6_str[INET6_ADDRSTRLEN]; 480 struct ra_pio *new_pios, *pio; 481 482 pio = router_find_ra_pio(iface, addr); 483 if (pio) { 484 if (memcmp(&pio->prefix, &addr->addr.in6, sizeof(struct in6_addr)) != 0 || 485 pio->length != addr->prefix_len) 486 { 487 char new_ipv6_str[INET6_ADDRSTRLEN]; 488 489 iface->pio_update = true; 490 warn("rfc9096: %s: changed %s/%u -> %s/%u", 491 iface->ifname, 492 inet_ntop(AF_INET6, &pio->prefix, ipv6_str, sizeof(ipv6_str)), 493 pio->length, 494 inet_ntop(AF_INET6, &addr->addr.in6, new_ipv6_str, sizeof(new_ipv6_str)), 495 addr->prefix_len); 496 497 memcpy(&pio->prefix, &addr->addr.in6, sizeof(struct in6_addr)); 498 pio->length = addr->prefix_len; 499 } 500 501 if (pio->lifetime) { 502 pio->lifetime = 0; 503 504 iface->pio_update = true; 505 warn("rfc9096: %s: renew %s/%u", 506 iface->ifname, 507 inet_ntop(AF_INET6, &pio->prefix, ipv6_str, sizeof(ipv6_str)), 508 pio->length); 509 } 510 511 return; 512 } 513 514 new_pios = realloc(iface->pios, sizeof(struct ra_pio) * (iface->pio_cnt + 1)); 515 if (!new_pios) 516 return; 517 518 iface->pios = new_pios; 519 pio = &iface->pios[iface->pio_cnt]; 520 iface->pio_cnt++; 521 522 memcpy(&pio->prefix, &addr->addr.in6, sizeof(struct in6_addr)); 523 pio->length = addr->prefix_len; 524 pio->lifetime = 0; 525 526 iface->pio_update = true; 527 info("rfc9096: %s: add %s/%u", 528 iface->ifname, 529 inet_ntop(AF_INET6, &pio->prefix, ipv6_str, sizeof(ipv6_str)), 530 pio->length); 531 } 532 533 static void router_clear_duplicated_ra_pio(struct interface *iface) 534 { 535 size_t pio_cnt = iface->pio_cnt; 536 char ipv6_str[INET6_ADDRSTRLEN]; 537 538 for (size_t i = 0; i < iface->pio_cnt; i++) { 539 struct ra_pio *pio_a = &iface->pios[i]; 540 size_t j = i + 1; 541 542 while (j < iface->pio_cnt) { 543 struct ra_pio *pio_b = &iface->pios[j]; 544 545 if (pio_a->length == pio_b->length && 546 !memcmp(&pio_a->prefix, &pio_b->prefix, sizeof(struct in6_addr))) { 547 warn("rfc9096: %s: clear duplicated %s/%u", 548 iface->ifname, 549 inet_ntop(AF_INET6, &pio_a->prefix, ipv6_str, sizeof(ipv6_str)), 550 pio_a->length); 551 552 iface->pios[j] = iface->pios[iface->pio_cnt - 1]; 553 iface->pio_cnt--; 554 } else { 555 j++; 556 } 557 } 558 } 559 560 if (iface->pio_cnt != pio_cnt) { 561 struct ra_pio *new_pios = realloc(iface->pios, sizeof(struct ra_pio) * iface->pio_cnt); 562 563 if (new_pios) 564 iface->pios = new_pios; 565 } 566 } 567 568 static void router_clear_expired_ra_pio(time_t now, 569 struct interface *iface) 570 { 571 size_t i = 0, pio_cnt = iface->pio_cnt; 572 char ipv6_str[INET6_ADDRSTRLEN]; 573 574 while (i < iface->pio_cnt) { 575 struct ra_pio *cur_pio = &iface->pios[i]; 576 577 if (ra_pio_expired(cur_pio, now)) { 578 info("rfc9096: %s: clear expired %s/%u", 579 iface->ifname, 580 inet_ntop(AF_INET6, &cur_pio->prefix, ipv6_str, sizeof(ipv6_str)), 581 cur_pio->length); 582 583 iface->pios[i] = iface->pios[iface->pio_cnt - 1]; 584 iface->pio_cnt--; 585 } else { 586 i++; 587 } 588 } 589 590 if (!iface->pio_cnt) { 591 free(iface->pios); 592 iface->pios = NULL; 593 } else if (iface->pio_cnt != pio_cnt) { 594 struct ra_pio *new_pios = realloc(iface->pios, sizeof(struct ra_pio) * iface->pio_cnt); 595 596 if (new_pios) 597 iface->pios = new_pios; 598 } 599 } 600 601 static void router_stale_ra_pio(struct interface *iface, 602 struct odhcpd_ipaddr *addr, 603 time_t now) 604 { 605 struct ra_pio *pio = router_find_ra_pio(iface, addr); 606 char ipv6_str[INET6_ADDRSTRLEN]; 607 608 if (!pio || pio->lifetime) 609 return; 610 611 pio->lifetime = now + iface->max_valid_lifetime; 612 613 iface->pio_update = true; 614 warn("rfc9096: %s: stale %s/%u", 615 iface->ifname, 616 inet_ntop(AF_INET6, &pio->prefix, ipv6_str, sizeof(ipv6_str)), 617 pio->length); 618 } 619 620 /* Router Advert server mode */ 621 static int send_router_advert(struct interface *iface, const struct in6_addr *from) 622 { 623 time_t now = odhcpd_time(); 624 struct odhcpd_ipaddr *addrs = NULL; 625 struct adv_msg adv; 626 struct nd_opt_prefix_info *pfxs = NULL; 627 struct nd_opt_dns_server *dns = NULL; 628 struct nd_opt_search_list *search = NULL; 629 struct nd_opt_route_info *routes = NULL; 630 struct nd_opt_pref64_info *pref64 = NULL; 631 struct nd_opt_dnr_info *dnrs = NULL; 632 struct nd_opt_adv_interval adv_interval; 633 struct nd_opt_capt_portal *capt_portal = NULL; 634 struct iovec iov[IOV_RA_TOTAL]; 635 struct sockaddr_in6 dest; 636 size_t dns_sz = 0, search_sz = 0, pref64_sz = 0, dnrs_sz = 0; 637 size_t pfxs_cnt = 0, routes_cnt = 0; 638 size_t total_addr_cnt = 0, valid_addr_cnt = 0; 639 size_t capt_portal_sz = 0; 640 /* 641 * lowest_found_lifetime stores the lowest lifetime of all prefixes; 642 * necessary to find longest adv interval necessary 643 * for shortest lived prefix 644 */ 645 uint32_t lowest_found_lifetime = UINT32_MAX, highest_found_lifetime = 0, maxival, ra_lifetime, opt_lifetime; 646 int msecs, hlim = iface->ra_hoplimit; 647 bool default_route = false; 648 bool valid_prefix = false; 649 char buf[INET6_ADDRSTRLEN]; 650 651 router_clear_expired_ra_pio(now, iface); 652 653 memset(&adv, 0, sizeof(adv)); 654 adv.h.nd_ra_type = ND_ROUTER_ADVERT; 655 656 if (hlim == 0) 657 hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit"); 658 659 if (hlim > 0) 660 adv.h.nd_ra_curhoplimit = hlim; 661 662 adv.h.nd_ra_flags_reserved = iface->ra_flags; 663 664 if (iface->route_preference < 0) 665 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW; 666 else if (iface->route_preference > 0) 667 adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH; 668 669 if (iface->dhcpv6 != MODE_DISABLED && iface->dhcpv6_pd && iface->dhcpv6_pd_preferred) { 670 /* RFC9762 § 5 671 * If the network desires to delegate prefixes to devices that support 672 * DHCPv6 prefix delegation but do not support the P flag, it SHOULD 673 * also set the M or O bits in the RA to 1 674 */ 675 adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED; 676 } 677 678 adv.h.nd_ra_reachable = htonl(iface->ra_reachabletime); 679 adv.h.nd_ra_retransmit = htonl(iface->ra_retranstime); 680 681 adv.lladdr.type = ND_OPT_SOURCE_LINKADDR; 682 adv.lladdr.len = 1; 683 odhcpd_get_mac(iface, adv.lladdr.data); 684 685 adv.mtu.nd_opt_mtu_type = ND_OPT_MTU; 686 adv.mtu.nd_opt_mtu_len = 1; 687 688 adv.mtu.nd_opt_mtu_mtu = htonl(iface->ra_mtu); 689 690 iov[IOV_RA_ADV].iov_base = &adv; 691 iov[IOV_RA_ADV].iov_len = sizeof(adv); 692 693 valid_addr_cnt = (iface->timer_rs.cb /* if not shutdown */ ? iface->addr6_len : 0); 694 695 // check ra_default 696 if (iface->default_router) { 697 default_route = true; 698 699 if (iface->default_router > 1) 700 valid_prefix = true; 701 } 702 703 if (valid_addr_cnt + iface->pio_cnt) { 704 addrs = alloca(sizeof(*addrs) * (valid_addr_cnt + iface->pio_cnt)); 705 706 if (valid_addr_cnt) { 707 memcpy(addrs, iface->addr6, sizeof(*addrs) * valid_addr_cnt); 708 total_addr_cnt = valid_addr_cnt; 709 710 /* Check default route */ 711 if (!default_route && parse_routes(addrs, valid_addr_cnt)) 712 default_route = true; 713 } 714 715 for (size_t i = 0; i < iface->pio_cnt; i++) { 716 struct ra_pio *cur_pio = &iface->pios[i]; 717 bool pio_found = false; 718 719 for (size_t j = 0; j < valid_addr_cnt; j++) { 720 struct odhcpd_ipaddr *cur_addr = &addrs[j]; 721 722 if (!router_compare_pio_addr(cur_pio, cur_addr)) { 723 pio_found = true; 724 break; 725 } 726 } 727 728 if (!pio_found) { 729 struct odhcpd_ipaddr *addr = &addrs[total_addr_cnt]; 730 731 memcpy(&addr->addr.in6, &cur_pio->prefix, sizeof(addr->addr.in6)); 732 addr->prefix_len = cur_pio->length; 733 addr->preferred_lt = 0; 734 addr->valid_lt = (uint32_t) (now + ND_VALID_LIMIT); 735 total_addr_cnt++; 736 } 737 } 738 } 739 740 /* Construct Prefix Information options */ 741 if (total_addr_cnt > 0) { 742 pfxs = alloca(total_addr_cnt * sizeof(*pfxs)); 743 memset(pfxs, 0, total_addr_cnt * sizeof(*pfxs)); 744 } 745 for (size_t i = 0; i < total_addr_cnt; ++i) { 746 struct odhcpd_ipaddr *addr = &addrs[i]; 747 struct nd_opt_prefix_info *p = NULL; 748 uint32_t preferred_lt = 0; 749 uint32_t valid_lt = 0; 750 751 if (addr->prefix_len > 96 || (i < valid_addr_cnt && addr->valid_lt <= (uint32_t)now)) { 752 info("Address %s (prefix %d, valid-lifetime %u) not suitable as RA prefix on %s", 753 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), addr->prefix_len, 754 addr->valid_lt, iface->name); 755 continue; 756 } 757 758 if (ADDR_MATCH_PIO_FILTER(addr, iface)) { 759 info("Address %s filtered out as RA prefix on %s", 760 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), 761 iface->name); 762 continue; /* PIO filtered out of this RA */ 763 } 764 765 for (size_t j = 0; j < pfxs_cnt; ++j) { 766 if (addr->prefix_len == pfxs[j].nd_opt_pi_prefix_len && 767 !odhcpd_bmemcmp(&pfxs[j].nd_opt_pi_prefix, 768 &addr->addr.in6, addr->prefix_len)) 769 p = &pfxs[j]; 770 } 771 772 if (!p) 773 p = &pfxs[pfxs_cnt++]; 774 775 if (addr->preferred_lt > (uint32_t)now) { 776 preferred_lt = TIME_LEFT(addr->preferred_lt, now); 777 778 if (iface->max_preferred_lifetime && preferred_lt > iface->max_preferred_lifetime) { 779 preferred_lt = iface->max_preferred_lifetime; 780 } 781 } 782 783 if (addr->valid_lt > (uint32_t)now) { 784 valid_lt = TIME_LEFT(addr->valid_lt, now); 785 786 if (iface->max_valid_lifetime && valid_lt > iface->max_valid_lifetime) 787 valid_lt = iface->max_valid_lifetime; 788 } 789 790 if (preferred_lt > valid_lt) { 791 /* 792 * RFC4861 § 6.2.1 793 * This value [AdvPreferredLifetime] MUST NOT be larger than 794 * AdvValidLifetime. 795 */ 796 preferred_lt = valid_lt; 797 } 798 799 if (lowest_found_lifetime > valid_lt) 800 lowest_found_lifetime = valid_lt; 801 802 if ((!IN6_IS_ADDR_ULA(&addr->addr.in6) || iface->default_router) && valid_lt) 803 valid_prefix = true; 804 805 if (!IN6_IS_ADDR_ULA(&addr->addr.in6) && valid_lt) { 806 if (highest_found_lifetime < valid_lt) 807 highest_found_lifetime = valid_lt; 808 } 809 810 odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr.in6, 811 (iface->ra_advrouter) ? 128 : addr->prefix_len); 812 p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 813 p->nd_opt_pi_len = 4; 814 p->nd_opt_pi_prefix_len = (addr->prefix_len < 64) ? 64 : addr->prefix_len; 815 /* RFC9762 DHCPv6-PD Preferred Flag § 6: 816 * Routers SHOULD set the P flag to zero by default... 817 */ 818 p->nd_opt_pi_flags_reserved = 0; 819 if (!iface->ra_not_onlink) 820 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK; 821 if (iface->ra_slaac && addr->prefix_len <= 64) 822 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO; 823 if (iface->dhcpv6 != MODE_DISABLED && iface->dhcpv6_pd && iface->dhcpv6_pd_preferred) 824 /* RFC9762 DHCPv6-PD Preferred Flag 825 * We can run both SLAAC and DHCPv6-PD. 826 * §6: 827 * "Routers MUST allow the P flag to be configured separately from the A flag. 828 * ...en/disabling the P flag MUST NOT trigger automatic changes in the A flag 829 * value set by the router." 830 */ 831 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_PD_PREFERRED; 832 if (iface->ra_advrouter) 833 // RFC6275, §7.2 834 p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR; 835 if (i >= valid_addr_cnt || !preferred_lt) { 836 /* 837 * RFC9096 § 3.5 838 * 839 * - Any prefixes that were previously advertised by the CE router 840 * via PIOs in RA messages, but that have now become stale, MUST 841 * be advertised with PIOs that have the "Valid Lifetime" and the 842 * "Preferred Lifetime" set to 0 and the "A" and "L" bits 843 * unchanged. 844 */ 845 p->nd_opt_pi_preferred_time = 0; 846 p->nd_opt_pi_valid_time = 0; 847 848 router_stale_ra_pio(iface, addr, now); 849 } else { 850 p->nd_opt_pi_preferred_time = htonl(preferred_lt); 851 p->nd_opt_pi_valid_time = htonl(valid_lt); 852 853 router_add_ra_pio(iface, addr); 854 } 855 } 856 857 router_clear_duplicated_ra_pio(iface); 858 859 iov[IOV_RA_PFXS].iov_base = pfxs; 860 iov[IOV_RA_PFXS].iov_len = pfxs_cnt * sizeof(*pfxs); 861 862 /* Calculate periodic transmit */ 863 msecs = calc_adv_interval(iface, lowest_found_lifetime, &maxival); 864 ra_lifetime = calc_ra_lifetime(iface, maxival); 865 866 /* Not a default router: use the RFC8106/RFC8781 default for the options */ 867 opt_lifetime = ra_lifetime ? ra_lifetime : 3 * maxival; 868 if (!highest_found_lifetime) 869 highest_found_lifetime = opt_lifetime; 870 871 if (!iface->have_link_local) { 872 notice("Skip sending a RA on %s as no link local address is available", iface->name); 873 goto out; 874 } 875 876 if (default_route && valid_prefix) { 877 adv.h.nd_ra_router_lifetime = htons(ra_lifetime < UINT16_MAX ? ra_lifetime : UINT16_MAX); 878 } else { 879 adv.h.nd_ra_router_lifetime = 0; 880 881 if (default_route) 882 warn("A default route is present but there is no public prefix " 883 "on %s thus we announce no default route by setting ra_lifetime to 0!", iface->name); 884 else 885 warn("No default route present, setting ra_lifetime to 0!"); 886 } 887 888 debug("Using a RA lifetime of %d seconds on %s", ntohs(adv.h.nd_ra_router_lifetime), iface->name); 889 890 /* Recursive DNS Server aka RDNSS; RFC8106, §5.1 */ 891 if (iface->ra_dns) { 892 struct in6_addr *dns_addrs6 = NULL, dns_addr6; 893 size_t dns_addrs6_cnt = 0; 894 895 if (iface->dns_addrs6_cnt > 0) { 896 dns_addrs6 = iface->dns_addrs6; 897 dns_addrs6_cnt = iface->dns_addrs6_cnt; 898 } else if (!odhcpd_get_interface_dns_addr6(iface, &dns_addr6)) { 899 dns_addrs6 = &dns_addr6; 900 dns_addrs6_cnt = 1; 901 } 902 903 /* The RDNSS len field is a uint8_t counting 8-byte units, so it 904 * holds at most 127 addresses (1 + 2*127 == 255); drop any extra 905 * so the on-wire length stays consistent with the bytes sent. */ 906 if (dns_addrs6_cnt > 127) 907 dns_addrs6_cnt = 127; 908 909 if (dns_addrs6_cnt) { 910 dns_sz = sizeof(*dns) + dns_addrs6_cnt * sizeof(*dns_addrs6); 911 912 dns = alloca(dns_sz); 913 memset(dns, 0, dns_sz); 914 dns->type = ND_OPT_RECURSIVE_DNS; 915 dns->len = 1 + (2 * dns_addrs6_cnt); 916 dns->lifetime = htonl(highest_found_lifetime); 917 memcpy(dns->addr, dns_addrs6, dns_addrs6_cnt * sizeof(*dns_addrs6)); 918 } 919 } 920 iov[IOV_RA_DNS].iov_base = dns; 921 iov[IOV_RA_DNS].iov_len = dns_sz; 922 923 /* DNS Search List aka DNSSL; RFC8106, §5.2 */ 924 /* The DNSSL len field is a uint8_t counting 8-byte units, so the whole 925 * option must fit in UINT8_MAX*8 bytes; skip it otherwise rather than 926 * emit an option whose length field does not match its real size. */ 927 if (iface->ra_dns && iface->dns_search_len > 0 && 928 sizeof(*search) + ((iface->dns_search_len + 7) & ~7) <= UINT8_MAX * 8) { 929 search_sz = sizeof(*search) + ((iface->dns_search_len + 7) & ~7); 930 search = alloca(search_sz); 931 memset(search, 0, search_sz); 932 *search = (struct nd_opt_search_list) { 933 .type = ND_OPT_DNS_SEARCH, 934 .len = search_sz / 8, 935 .reserved = 0, 936 .lifetime = htonl(highest_found_lifetime), 937 }; 938 memcpy(search->name, iface->dns_search, iface->dns_search_len); 939 } 940 iov[IOV_RA_SEARCH].iov_base = search; 941 iov[IOV_RA_SEARCH].iov_len = search_sz; 942 943 if (iface->pref64_length) { 944 /* RFC 8781 § 4.1 rounding up lifetime to multiple of 8 */ 945 uint16_t pref64_lifetime = opt_lifetime < (UINT16_MAX - 7) ? opt_lifetime + 7 : (UINT16_MAX - 7); 946 947 pref64_sz = sizeof(*pref64); 948 pref64 = alloca(pref64_sz); 949 pref64->type = ND_OPT_PREF64; 950 pref64->len = 2; 951 pref64->lifetime_plc = htons((0xfff8 & pref64_lifetime) | 952 (0x7 & iface->pref64_plc)); 953 memcpy(pref64->prefix, iface->pref64_prefix, sizeof(pref64->prefix)); 954 } 955 iov[IOV_RA_PREF64].iov_base = pref64; 956 iov[IOV_RA_PREF64].iov_len = pref64_sz; 957 958 if (iface->dnr_cnt) { 959 size_t dnr_sz[iface->dnr_cnt]; 960 961 for (unsigned i = 0; i < iface->dnr_cnt; i++) { 962 dnr_sz[i] = sizeof(struct nd_opt_dnr_info) + iface->dnr[i].adn_len; 963 if (iface->dnr[i].addr6_cnt > 0 || iface->dnr[i].svc_len > 0) { 964 dnr_sz[i] += 2 + iface->dnr[i].addr6_cnt * sizeof(struct in6_addr); 965 dnr_sz[i] += 2 + iface->dnr[i].svc_len; 966 } 967 dnr_sz[i] = (dnr_sz[i] + 7) & ~7; 968 dnrs_sz += dnr_sz[i]; 969 } 970 971 /* dnrs are sized in multiples of 8, so each dnr should be aligned */ 972 dnrs = alloca(dnrs_sz); 973 memset(dnrs, 0, dnrs_sz); 974 975 uint8_t *pos = (uint8_t *)dnrs; 976 for (unsigned i = 0; i < iface->dnr_cnt; pos += dnr_sz[i], i++) { 977 struct nd_opt_dnr_info *dnr = (struct nd_opt_dnr_info *)pos; 978 size_t dnr_addr6_sz = iface->dnr[i].addr6_cnt * sizeof(struct in6_addr); 979 uint8_t *tmp = dnr->body; 980 981 dnr->type = ND_OPT_DNR; 982 dnr->len = dnr_sz[i] / 8; 983 dnr->priority = htons(iface->dnr[i].priority); 984 if (iface->dnr[i].lifetime_set) 985 dnr->lifetime = htonl(iface->dnr[i].lifetime); 986 else 987 dnr->lifetime = htonl(highest_found_lifetime); 988 989 dnr->adn_len = htons(iface->dnr[i].adn_len); 990 memcpy(tmp, iface->dnr[i].adn, iface->dnr[i].adn_len); 991 tmp += iface->dnr[i].adn_len; 992 993 *(tmp++) = dnr_addr6_sz >> 8; 994 *(tmp++) = dnr_addr6_sz & 0xff; 995 memcpy(tmp, iface->dnr[i].addr6, dnr_addr6_sz); 996 tmp += dnr_addr6_sz; 997 998 *(tmp++) = iface->dnr[i].svc_len >> 8; 999 *(tmp++) = iface->dnr[i].svc_len & 0xff; 1000 memcpy(tmp, iface->dnr[i].svc, iface->dnr[i].svc_len); 1001 } 1002 } 1003 iov[IOV_RA_DNR].iov_base = dnrs; 1004 iov[IOV_RA_DNR].iov_len = dnrs_sz; 1005 1006 /* 1007 * RFC7084 § 4.3 : 1008 * L-3: An IPv6 CE router MUST advertise itself as a router for the 1009 * delegated prefix(es) (and ULA prefix if configured to provide 1010 * ULA addressing) using the "Route Information Option" specified 1011 * in Section 2.3 of [RFC4191]. This advertisement is 1012 * independent of having or not having IPv6 connectivity on the 1013 * WAN interface. 1014 */ 1015 if (valid_addr_cnt > 0) { 1016 routes = alloca(valid_addr_cnt * sizeof(*routes)); 1017 memset(routes, 0, valid_addr_cnt * sizeof(*routes)); 1018 } 1019 for (size_t i = 0; i < valid_addr_cnt; ++i) { 1020 struct odhcpd_ipaddr *addr = &addrs[i]; 1021 uint32_t valid_lt; 1022 1023 if (addr->dprefix_len >= 64 || addr->dprefix_len == 0 || addr->valid_lt <= (uint32_t)now) { 1024 debug("Address %s (dprefix %d, valid-lifetime %u) not suitable as RA route on %s", 1025 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), 1026 addr->dprefix_len, addr->valid_lt, iface->name); 1027 continue; 1028 } 1029 1030 if (ADDR_MATCH_PIO_FILTER(addr, iface)) { 1031 debug("Address %s filtered out as RA route on %s", 1032 inet_ntop(AF_INET6, &addr->addr.in6, buf, sizeof(buf)), 1033 iface->name); 1034 continue; 1035 } 1036 1037 if (addr->dprefix_len > 32) { 1038 addr->addr.in6.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix_len)) - 1)); 1039 } else if (addr->dprefix_len <= 32) { 1040 addr->addr.in6.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix_len)) - 1)); 1041 addr->addr.in6.s6_addr32[1] = 0; 1042 } 1043 1044 routes[routes_cnt].type = ND_OPT_ROUTE_INFO; 1045 routes[routes_cnt].len = sizeof(*routes) / 8; 1046 routes[routes_cnt].prefix_len = addr->dprefix_len; 1047 routes[routes_cnt].flags = 0; 1048 if (iface->route_preference < 0) 1049 routes[routes_cnt].flags |= ND_RA_PREF_LOW; 1050 else if (iface->route_preference > 0) 1051 routes[routes_cnt].flags |= ND_RA_PREF_HIGH; 1052 1053 valid_lt = TIME_LEFT(addr->valid_lt, now); 1054 if (iface->max_valid_lifetime && valid_lt > iface->max_valid_lifetime) 1055 valid_lt = iface->max_valid_lifetime; 1056 routes[routes_cnt].lifetime = htonl(valid_lt); 1057 routes[routes_cnt].addr[0] = addr->addr.in6.s6_addr32[0]; 1058 routes[routes_cnt].addr[1] = addr->addr.in6.s6_addr32[1]; 1059 routes[routes_cnt].addr[2] = 0; 1060 routes[routes_cnt].addr[3] = 0; 1061 1062 routes_cnt++; 1063 } 1064 iov[IOV_RA_ROUTES].iov_base = routes; 1065 iov[IOV_RA_ROUTES].iov_len = routes_cnt * sizeof(*routes); 1066 1067 memset(&adv_interval, 0, sizeof(adv_interval)); 1068 adv_interval.nd_opt_adv_interval_type = ND_OPT_RTR_ADV_INTERVAL; 1069 adv_interval.nd_opt_adv_interval_len = 1; 1070 adv_interval.nd_opt_adv_interval_ival = htonl(maxival*1000); 1071 1072 iov[IOV_RA_ADV_INTERVAL].iov_base = &adv_interval; 1073 iov[IOV_RA_ADV_INTERVAL].iov_len = adv_interval.nd_opt_adv_interval_len * 8; 1074 1075 /* RFC 8910 Captive Portal */ 1076 if (iface->captive_portal_uri_len > 0) { 1077 /* compute pad so that (header + data + pad) is a multiple of 8 */ 1078 capt_portal_sz = (sizeof(struct nd_opt_capt_portal) + iface->captive_portal_uri_len + 7) & ~7; 1079 1080 capt_portal = alloca(capt_portal_sz); 1081 memset(capt_portal, 0, capt_portal_sz); 1082 1083 capt_portal->type = ND_OPT_CAPTIVE_PORTAL; 1084 capt_portal->len = capt_portal_sz / 8; 1085 1086 memcpy(capt_portal->data, iface->captive_portal_uri, iface->captive_portal_uri_len); 1087 /* remaining padding bytes already set to 0x00 */ 1088 } 1089 1090 iov[IOV_RA_CAPT_PORTAL].iov_base = capt_portal; 1091 iov[IOV_RA_CAPT_PORTAL].iov_len = capt_portal_sz; 1092 1093 memset(&dest, 0, sizeof(dest)); 1094 dest.sin6_family = AF_INET6; 1095 1096 if (from && !IN6_IS_ADDR_UNSPECIFIED(from)) 1097 dest.sin6_addr = *from; 1098 else 1099 inet_pton(AF_INET6, ALL_IPV6_NODES, &dest.sin6_addr); 1100 1101 debug("Sending a RA on %s", iface->name); 1102 1103 if (odhcpd_try_send_with_src(iface->router_event.uloop.fd, &dest, iov, ARRAY_SIZE(iov), iface) > 0) { 1104 iface->ra_sent++; 1105 1106 statefiles_write_prefix_information(iface); 1107 } 1108 1109 out: 1110 return msecs; 1111 } 1112 1113 1114 static void trigger_router_advert(struct uloop_timeout *event) 1115 { 1116 struct interface *iface = container_of(event, struct interface, timer_rs); 1117 int msecs = send_router_advert(iface, NULL); 1118 1119 /* Rearm timer if not shut down */ 1120 if (event->cb) 1121 uloop_timeout_set(event, msecs); 1122 } 1123 1124 1125 /* Event handler for incoming ICMPv6 packets */ 1126 static void handle_icmpv6(void *addr, void *data, size_t len, 1127 struct interface *iface, _o_unused void *dest) 1128 { 1129 struct icmp6_hdr *hdr = data; 1130 struct sockaddr_in6 *from = addr; 1131 1132 if (!router_icmpv6_valid(addr, data, len)) 1133 return; 1134 1135 if ((iface->ra == MODE_SERVER && !iface->master)) { /* Server mode */ 1136 if (hdr->icmp6_type == ND_ROUTER_SOLICIT) 1137 send_router_advert(iface, &from->sin6_addr); 1138 } else if (iface->ra == MODE_RELAY) { /* Relay mode */ 1139 if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master) { 1140 struct interface *c; 1141 1142 avl_for_each_element(&interfaces, c, avl) { 1143 if (!c->master || c->ra != MODE_RELAY) 1144 continue; 1145 1146 forward_router_solicitation(c); 1147 } 1148 } else if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master) 1149 forward_router_advertisement(iface, data, len); 1150 } 1151 } 1152 1153 1154 /* Forward a router solicitation from slave to master interface */ 1155 static void forward_router_solicitation(const struct interface *iface) 1156 { 1157 struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}}; 1158 struct iovec iov = {&rs, sizeof(rs)}; 1159 struct sockaddr_in6 all_routers; 1160 1161 if (!iface) 1162 return; 1163 1164 memset(&all_routers, 0, sizeof(all_routers)); 1165 all_routers.sin6_family = AF_INET6; 1166 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &all_routers.sin6_addr); 1167 all_routers.sin6_scope_id = iface->ifindex; 1168 1169 notice("Sending RS to %s", iface->name); 1170 odhcpd_send(iface->router_event.uloop.fd, &all_routers, &iov, 1, iface); 1171 } 1172 1173 1174 /* Forward a router advertisement from master to slave interfaces */ 1175 static void forward_router_advertisement(const struct interface *iface, uint8_t *data, size_t len) 1176 { 1177 struct nd_router_advert *adv = (struct nd_router_advert *)data; 1178 struct sockaddr_in6 all_nodes; 1179 struct icmpv6_opt *opt; 1180 struct interface *c; 1181 struct iovec iov = { .iov_base = data, .iov_len = len }; 1182 /* Rewrite options */ 1183 uint8_t *end = data + len; 1184 uint8_t *mac_ptr = NULL; 1185 struct in6_addr *dns_addrs6 = NULL, *dns_addrs6_orig = NULL; 1186 size_t dns_addrs6_cnt = 0; 1187 // MTU option 1188 struct nd_opt_mtu *mtu_opt = NULL; 1189 uint32_t ingress_mtu_val = 0; 1190 /* PIO L/A/R/P flag and RA M/O Flags */ 1191 uint8_t ra_flags; 1192 size_t pio_count = 0; 1193 struct fwd_pio_flags { 1194 uint8_t *ptr; 1195 uint8_t flags; 1196 } *pio_flags = NULL; 1197 1198 icmpv6_for_each_option(opt, &adv[1], end) { 1199 /* check our packet content is not truncated */ 1200 if (opt->len == 0 || (uint8_t *)opt + opt->len * 8 > end) { 1201 error("Ingress RA packet option for relaying has incorrect length"); 1202 return; 1203 } 1204 1205 switch(opt->type) { 1206 case ND_OPT_PREFIX_INFORMATION: 1207 pio_count++; 1208 break; 1209 } 1210 } 1211 1212 if (pio_count > 0) { 1213 pio_flags = alloca(sizeof(*pio_flags) * pio_count); 1214 pio_count = 0; 1215 } 1216 1217 /* Parse existing options */ 1218 icmpv6_for_each_option(opt, &adv[1], end) { 1219 switch (opt->type) { 1220 case ND_OPT_SOURCE_LINKADDR: 1221 mac_ptr = opt->data; 1222 break; 1223 1224 case ND_OPT_RECURSIVE_DNS: 1225 if (opt->len > 1) { 1226 dns_addrs6 = (struct in6_addr *)&opt->data[6]; 1227 dns_addrs6_cnt = (opt->len - 1) / 2; 1228 } 1229 break; 1230 1231 case ND_OPT_MTU: 1232 if (opt->len == 1 && (uint8_t *)opt + sizeof(struct nd_opt_mtu) <= end) { 1233 mtu_opt = (struct nd_opt_mtu *)opt; 1234 ingress_mtu_val = ntohl(mtu_opt->nd_opt_mtu_mtu); 1235 } 1236 break; 1237 case ND_OPT_PREFIX_INFORMATION: 1238 /* Store options for each PIO */ 1239 pio_flags[pio_count].ptr = &opt->data[1]; 1240 pio_flags[pio_count].flags = opt->data[1]; 1241 pio_count++; 1242 break; 1243 } 1244 } 1245 1246 info("Got a RA on %s", iface->name); 1247 1248 /* Indicate a proxy, however we don't follow the rest of RFC 4389 yet 1249 * store original upstream RA state 1250 */ 1251 ra_flags = adv->nd_ra_flags_reserved | ND_RA_FLAG_PROXY; 1252 1253 /* Forward advertisement to all slave interfaces */ 1254 memset(&all_nodes, 0, sizeof(all_nodes)); 1255 all_nodes.sin6_family = AF_INET6; 1256 inet_pton(AF_INET6, ALL_IPV6_NODES, &all_nodes.sin6_addr); 1257 1258 /* Preserve the upstream DNS addresses: they are rewritten in place in the 1259 * single shared packet buffer, so without restoring them a later slave 1260 * would inherit a previous slave's rewritten values. */ 1261 if (dns_addrs6 && dns_addrs6_cnt > 0) { 1262 dns_addrs6_orig = alloca(dns_addrs6_cnt * sizeof(*dns_addrs6_orig)); 1263 memcpy(dns_addrs6_orig, dns_addrs6, dns_addrs6_cnt * sizeof(*dns_addrs6_orig)); 1264 } 1265 1266 avl_for_each_element(&interfaces, c, avl) { 1267 if (c->ra != MODE_RELAY || c->master) 1268 continue; 1269 1270 /* Restore the upstream DNS addresses and MTU value that a previous 1271 * slave may have rewritten in the shared buffer. */ 1272 if (dns_addrs6_orig) 1273 memcpy(dns_addrs6, dns_addrs6_orig, dns_addrs6_cnt * sizeof(*dns_addrs6)); 1274 if (mtu_opt) 1275 mtu_opt->nd_opt_mtu_mtu = htonl(ingress_mtu_val); 1276 1277 /* Fixup source hardware address option */ 1278 if (mac_ptr) 1279 odhcpd_get_mac(c, mac_ptr); 1280 1281 if (pio_count > 0) 1282 debug("RA forward: Rewriting RA PIO flags"); 1283 1284 for (size_t i = 0; i < pio_count; i++) { 1285 /* restore the flags byte to its upstream state before applying per-interface policy */ 1286 *pio_flags[i].ptr = pio_flags[i].flags; 1287 /* ensure L flag (on-link) cleared; relayed == not on-link */ 1288 *pio_flags[i].ptr &= ~ND_OPT_PI_FLAG_ONLINK; 1289 /* upstream no SLAAC, downstream no SLAAC: no change 1290 * upstream no SLAAC, downstream SLAAC: no change 1291 * upstream SLAAC, downstream SLAAC: no change 1292 * upstream SLAAC, downstream no SLAAC: clear flag 1293 * Why? We shall not SLAAC downstream if upstream disables it. Sometimes 1294 * we just inform about a prefix for DHCPv6 and routing info. 1295 */ 1296 if (!c->ra_slaac) 1297 *pio_flags[i].ptr &= ~ND_OPT_PI_FLAG_AUTO;/* ensure A flag cleared */ 1298 1299 /* we have no opinion on the R flag - it can be forwarded */ 1300 1301 if (c->dhcpv6 == MODE_DISABLED || !c->dhcpv6_pd || !c->dhcpv6_pd_preferred) 1302 *pio_flags[i].ptr &= ~ND_OPT_PI_FLAG_PD_PREFERRED;/* ensure P flag (DHCPv6-PD) cleared */ 1303 } 1304 1305 /* Apply per-interface modifications of upstream RA state */ 1306 adv->nd_ra_flags_reserved = ra_flags; 1307 /* Rewrite M/O flags unless we relay DHCPv6 */ 1308 if (c->dhcpv6 != MODE_RELAY) { 1309 /* Clear the relayed M/O bits */ 1310 adv->nd_ra_flags_reserved &= ~(ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER); 1311 /* Apply the locally configured ra_flags for M and O */ 1312 adv->nd_ra_flags_reserved |= c->ra_flags & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER); 1313 } 1314 1315 /* If we have to rewrite DNS entries */ 1316 if (c->always_rewrite_dns && dns_addrs6 && dns_addrs6_cnt > 0) { 1317 const struct in6_addr *rewrite = c->dns_addrs6; 1318 struct in6_addr addr; 1319 size_t rewrite_cnt = c->dns_addrs6_cnt; 1320 1321 if (rewrite_cnt == 0) { 1322 if (odhcpd_get_interface_dns_addr6(c, &addr)) 1323 continue; /* Unable to comply */ 1324 1325 rewrite = &addr; 1326 rewrite_cnt = 1; 1327 } 1328 1329 /* Copy over any other addresses */ 1330 for (size_t i = 0; i < dns_addrs6_cnt; ++i) { 1331 size_t j = (i < rewrite_cnt) ? i : rewrite_cnt - 1; 1332 dns_addrs6[i] = rewrite[j]; 1333 } 1334 } 1335 1336 /* Rewrite MTU option if local RA MTU is configured */ 1337 if (c->ra_mtu && mtu_opt) { 1338 if (ingress_mtu_val != c->ra_mtu) { 1339 debug("Rewriting RA MTU from %u to %u on %s", 1340 ingress_mtu_val, c->ra_mtu, c->name); 1341 mtu_opt->nd_opt_mtu_mtu = htonl(c->ra_mtu); 1342 } 1343 } 1344 1345 info("Forward a RA on %s", c->name); 1346 odhcpd_send(c->router_event.uloop.fd, &all_nodes, &iov, 1, c); 1347 } 1348 } 1349
This page was automatically generated by LXR 0.3.1. • OpenWrt