1 /* 2 * dhcpcd - IPv6 ND handling 3 * SPDX-License-Identifier: BSD-2-Clause 4 * Copyright (c) 2006-2025 Roy Marples <roy (at) marples.name> 5 * All rights reserved 6 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #define __APPLE_USE_RFC_3542 30 31 #include <sys/param.h> 32 #include <sys/ioctl.h> 33 #include <sys/socket.h> 34 35 #include <net/if.h> 36 #include <net/route.h> 37 #include <netinet/in.h> 38 #include <netinet/ip6.h> 39 #include <netinet/icmp6.h> 40 41 #include <assert.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <stddef.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <syslog.h> 48 #include <unistd.h> 49 50 #define ELOOP_QUEUE ELOOP_IPV6ND 51 #include "common.h" 52 #include "dhcp-common.h" 53 #include "dhcp6.h" 54 #include "dhcpcd.h" 55 #include "eloop.h" 56 #include "if.h" 57 #include "ipv6.h" 58 #include "ipv6nd.h" 59 #include "logerr.h" 60 #include "privsep.h" 61 #include "route.h" 62 #include "script.h" 63 64 /* Debugging Router Solicitations is a lot of spam, so disable it */ 65 // #define DEBUG_RS 66 67 #ifndef ND_RA_FLAG_HOME_AGENT 68 #define ND_RA_FLAG_HOME_AGENT 0x20 /* Home Agent flag in RA */ 69 #endif 70 #ifndef ND_RA_FLAG_PROXY 71 #define ND_RA_FLAG_PROXY 0x04 /* Proxy */ 72 #endif 73 #ifndef ND_OPT_PI_FLAG_ROUTER 74 #define ND_OPT_PI_FLAG_ROUTER 0x20 /* Router flag in PI */ 75 #endif 76 77 #ifndef ND_OPT_RI 78 #define ND_OPT_RI 24 79 struct nd_opt_ri { /* Route Information option RFC4191 */ 80 uint8_t nd_opt_ri_type; 81 uint8_t nd_opt_ri_len; 82 uint8_t nd_opt_ri_prefixlen; 83 uint8_t nd_opt_ri_flags_reserved; 84 uint32_t nd_opt_ri_lifetime; 85 struct in6_addr nd_opt_ri_prefix; 86 }; 87 __CTASSERT(sizeof(struct nd_opt_ri) == 24); 88 #define OPT_RI_FLAG_PREFERENCE(flags) ((flags & 0x18) >> 3) 89 #endif 90 91 #ifndef ND_OPT_RDNSS 92 #define ND_OPT_RDNSS 25 93 struct nd_opt_rdnss { /* RDNSS option RFC 6106 */ 94 uint8_t nd_opt_rdnss_type; 95 uint8_t nd_opt_rdnss_len; 96 uint16_t nd_opt_rdnss_reserved; 97 uint32_t nd_opt_rdnss_lifetime; 98 /* followed by list of IP prefixes */ 99 }; 100 __CTASSERT(sizeof(struct nd_opt_rdnss) == 8); 101 #endif 102 103 #ifndef ND_OPT_DNSSL 104 #define ND_OPT_DNSSL 31 105 struct nd_opt_dnssl { /* DNSSL option RFC 6106 */ 106 uint8_t nd_opt_dnssl_type; 107 uint8_t nd_opt_dnssl_len; 108 uint16_t nd_opt_dnssl_reserved; 109 uint32_t nd_opt_dnssl_lifetime; 110 /* followed by list of DNS servers */ 111 }; 112 __CTASSERT(sizeof(struct nd_opt_dnssl) == 8); 113 #endif 114 115 /* Impossible options, so we can easily add extras */ 116 #define _ND_OPT_PREFIX_ADDR 255 + 1 117 118 /* Minimal IPv6 MTU */ 119 #ifndef IPV6_MMTU 120 #define IPV6_MMTU 1280 121 #endif 122 123 #ifndef ND_RA_FLAG_RTPREF_HIGH 124 #define ND_RA_FLAG_RTPREF_MASK 0x18 125 #define ND_RA_FLAG_RTPREF_HIGH 0x08 126 #define ND_RA_FLAG_RTPREF_MEDIUM 0x00 127 #define ND_RA_FLAG_RTPREF_LOW 0x18 128 #define ND_RA_FLAG_RTPREF_RSV 0x10 129 #endif 130 131 #define EXPIRED_MAX \ 132 5 /* Remember 5 expired routers to avoid \ 133 logspam. */ 134 135 #define MIN_RANDOM_FACTOR 500 /* millisecs */ 136 #define MAX_RANDOM_FACTOR 1500 /* millisecs */ 137 #define MIN_RANDOM_FACTOR_U MIN_RANDOM_FACTOR * 1000 /* usecs */ 138 #define MAX_RANDOM_FACTOR_U MAX_RANDOM_FACTOR * 1000 /* usecs */ 139 140 #if BYTE_ORDER == BIG_ENDIAN 141 #define IPV6_ADDR_INT32_ONE 1 142 #define IPV6_ADDR_INT16_MLL 0xff02 143 #elif BYTE_ORDER == LITTLE_ENDIAN 144 #define IPV6_ADDR_INT32_ONE 0x01000000 145 #define IPV6_ADDR_INT16_MLL 0x02ff 146 #endif 147 148 /* Debugging Neighbor Solicitations is a lot of spam, so disable it */ 149 // #define DEBUG_NS 150 // 151 152 static void ipv6nd_handledata(void *, unsigned short); 153 static struct routeinfo *routeinfo_findalloc(struct ra *, 154 const struct in6_addr *, uint8_t); 155 static void routeinfohead_free(struct routeinfohead *); 156 157 /* 158 * Android ships buggy ICMP6 filter headers. 159 * Supply our own until they fix their shit. 160 * References: 161 * https://android-review.googlesource.com/#/c/58438/ 162 * http://code.google.com/p/android/issues/original?id=32621&seq=24 163 */ 164 #ifdef __ANDROID__ 165 #undef ICMP6_FILTER_WILLPASS 166 #undef ICMP6_FILTER_WILLBLOCK 167 #undef ICMP6_FILTER_SETPASS 168 #undef ICMP6_FILTER_SETBLOCK 169 #undef ICMP6_FILTER_SETPASSALL 170 #undef ICMP6_FILTER_SETBLOCKALL 171 #define ICMP6_FILTER_WILLPASS(type, filterp) \ 172 ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) == 0) 173 #define ICMP6_FILTER_WILLBLOCK(type, filterp) \ 174 ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) != 0) 175 #define ICMP6_FILTER_SETPASS(type, filterp) \ 176 ((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31)))) 177 #define ICMP6_FILTER_SETBLOCK(type, filterp) \ 178 ((((filterp)->icmp6_filt[(type) >> 5]) |= (1 << ((type) & 31)))) 179 #define ICMP6_FILTER_SETPASSALL(filterp) \ 180 memset(filterp, 0, sizeof(struct icmp6_filter)); 181 #define ICMP6_FILTER_SETBLOCKALL(filterp) \ 182 memset(filterp, 0xff, sizeof(struct icmp6_filter)); 183 #endif 184 185 /* Support older systems with different defines */ 186 #if !defined(IPV6_RECVHOPLIMIT) && defined(IPV6_HOPLIMIT) 187 #define IPV6_RECVHOPLIMIT IPV6_HOPLIMIT 188 #endif 189 #if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO) 190 #define IPV6_RECVPKTINFO IPV6_PKTINFO 191 #endif 192 193 /* Handy defines */ 194 #define ipv6nd_free_ra(ra) ipv6nd_freedrop_ra((ra), 0) 195 #define ipv6nd_drop_ra(ra) ipv6nd_freedrop_ra((ra), 1) 196 197 /* Clear these addrflags on receipt of a new RA before adding the new flags 198 * dervived from the RA. */ 199 #define RA_STALE_FLAGS \ 200 (IPV6_AF_ONLINK | IPV6_AF_AUTOCONF | IPV6_AF_ROUTER | IPV6_AF_STALE) 201 202 void 203 ipv6nd_printoptions(const struct dhcpcd_ctx *ctx, const struct dhcp_opt *opts, 204 size_t opts_len) 205 { 206 size_t i, j; 207 const struct dhcp_opt *opt, *opt2; 208 int cols; 209 210 for (i = 0, opt = ctx->nd_opts; i < ctx->nd_opts_len; i++, opt++) { 211 for (j = 0, opt2 = opts; j < opts_len; j++, opt2++) 212 if (opt2->option == opt->option) 213 break; 214 if (j == opts_len) { 215 cols = printf("%03d %s", opt->option, opt->var); 216 dhcp_print_option_encoding(opt, cols); 217 } 218 } 219 for (i = 0, opt = opts; i < opts_len; i++, opt++) { 220 cols = printf("%03d %s", opt->option, opt->var); 221 dhcp_print_option_encoding(opt, cols); 222 } 223 } 224 225 int 226 ipv6nd_open(bool recv) 227 { 228 int fd, on; 229 struct icmp6_filter filt; 230 231 fd = xsocket(PF_INET6, SOCK_RAW | SOCK_CXNB, IPPROTO_ICMPV6); 232 if (fd == -1) 233 return -1; 234 235 ICMP6_FILTER_SETBLOCKALL(&filt); 236 237 /* RFC4861 4.1 */ 238 on = 255; 239 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &on, 240 sizeof(on)) == -1) 241 goto eexit; 242 243 if (recv) { 244 on = 1; 245 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, 246 sizeof(on)) == -1) 247 goto eexit; 248 249 on = 1; 250 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, 251 sizeof(on)) == -1) 252 goto eexit; 253 254 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt); 255 256 #ifdef SO_RERROR 257 on = 1; 258 if (setsockopt(fd, SOL_SOCKET, SO_RERROR, &on, sizeof(on)) == 259 -1) 260 goto eexit; 261 #endif 262 } 263 264 if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt)) == 265 -1) 266 goto eexit; 267 268 return fd; 269 270 eexit: 271 close(fd); 272 return -1; 273 } 274 275 #ifdef __sun 276 int 277 ipv6nd_openif(unsigned int ifindex) 278 { 279 int fd; 280 struct ipv6_mreq mreq = { 281 .ipv6mr_multiaddr = IN6ADDR_LINKLOCAL_ALLNODES_INIT, 282 .ipv6mr_interface = ifindex, 283 }; 284 285 fd = ipv6nd_open(true); 286 if (fd == -1) 287 return -1; 288 289 if (setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &ifindex, 290 sizeof(ifindex)) == -1) 291 goto err; 292 293 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, 294 sizeof(mreq)) == -1) 295 goto err; 296 297 return fd; 298 299 err: 300 close(fd); 301 return -1; 302 } 303 #endif 304 305 static int 306 ipv6nd_makersprobe(struct interface *ifp) 307 { 308 struct rs_state *state; 309 struct nd_router_solicit *rs; 310 311 state = RS_STATE(ifp); 312 free(state->rs); 313 state->rslen = sizeof(*rs); 314 if (ifp->hwlen != 0) 315 state->rslen += (size_t)ROUNDUP8(ifp->hwlen + 2); 316 state->rs = calloc(1, state->rslen); 317 if (state->rs == NULL) 318 return -1; 319 rs = state->rs; 320 rs->nd_rs_type = ND_ROUTER_SOLICIT; 321 // rs->nd_rs_code = 0; 322 // rs->nd_rs_cksum = 0; 323 // rs->nd_rs_reserved = 0; 324 325 if (ifp->hwlen != 0) { 326 struct nd_opt_hdr *nd; 327 328 nd = (struct nd_opt_hdr *)(state->rs + 1); 329 nd->nd_opt_type = ND_OPT_SOURCE_LINKADDR; 330 nd->nd_opt_len = (uint8_t)((ROUNDUP8(ifp->hwlen + 2)) >> 3); 331 memcpy(nd + 1, ifp->hwaddr, ifp->hwlen); 332 } 333 return 0; 334 } 335 336 static void 337 ipv6nd_sendrsprobe(void *arg) 338 { 339 struct interface *ifp = arg; 340 struct rs_state *state = RS_STATE(ifp); 341 struct sockaddr_in6 dst = { 342 .sin6_family = AF_INET6, 343 .sin6_addr = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT, 344 .sin6_scope_id = ifp->index, 345 }; 346 struct iovec iov = { .iov_base = state->rs, .iov_len = state->rslen }; 347 union { 348 struct cmsghdr hdr; 349 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; 350 } cmsgbuf = { .buf = { 0 } }; 351 struct msghdr msg = { 352 .msg_name = &dst, 353 .msg_namelen = sizeof(dst), 354 .msg_iov = &iov, 355 .msg_iovlen = 1, 356 .msg_control = cmsgbuf.buf, 357 .msg_controllen = sizeof(cmsgbuf.buf), 358 }; 359 struct cmsghdr *cm; 360 struct in6_pktinfo pi = { .ipi6_ifindex = ifp->index }; 361 int s; 362 #ifndef __sun 363 struct dhcpcd_ctx *ctx = ifp->ctx; 364 #endif 365 366 if (ipv6_linklocal(ifp) == NULL) { 367 logdebugx("%s: delaying Router Solicitation for LL address", 368 ifp->name); 369 ipv6_addlinklocalcallback(ifp, ipv6nd_sendrsprobe, ifp); 370 return; 371 } 372 373 #ifdef HAVE_SA_LEN 374 dst.sin6_len = sizeof(dst); 375 #endif 376 377 /* Set the outbound interface */ 378 cm = CMSG_FIRSTHDR(&msg); 379 if (cm == NULL) /* unlikely */ 380 return; 381 cm->cmsg_level = IPPROTO_IPV6; 382 cm->cmsg_type = IPV6_PKTINFO; 383 cm->cmsg_len = CMSG_LEN(sizeof(pi)); 384 memcpy(CMSG_DATA(cm), &pi, sizeof(pi)); 385 386 logdebugx("%s: sending Router Solicitation", ifp->name); 387 #ifdef PRIVSEP 388 if (IN_PRIVSEP(ifp->ctx)) { 389 if (ps_inet_sendnd(ifp, &msg) == -1) 390 logerr(__func__); 391 goto sent; 392 } 393 #endif 394 #ifdef __sun 395 if (state->nd_fd == -1) { 396 state->nd_fd = ipv6nd_openif(ifp->index); 397 if (state->nd_fd == -1) { 398 logerr(__func__); 399 return; 400 } 401 if (eloop_event_add(ifp->ctx->eloop, state->nd_fd, ELE_READ, 402 ipv6nd_handledata, ifp) == -1) { 403 logerr(__func__); 404 close(state->nd_fd); 405 state->nd_fd = -1; 406 return; 407 } 408 } 409 s = state->nd_fd; 410 #else 411 if (ctx->nd_fd == -1) { 412 ctx->nd_fd = ipv6nd_open(true); 413 if (ctx->nd_fd == -1) { 414 logerr(__func__); 415 return; 416 } 417 if (eloop_event_add(ctx->eloop, ctx->nd_fd, ELE_READ, 418 ipv6nd_handledata, ctx) == -1) 419 logerr("%s: eloop_event_add", __func__); 420 } 421 s = ifp->ctx->nd_fd; 422 #endif 423 if (sendmsg(s, &msg, 0) == -1) { 424 logerr(__func__); 425 /* Allow IPv6ND to continue .... at most a few errors 426 * would be logged. 427 * Generally the error is ENOBUFS when struggling to 428 * associate with an access point. */ 429 } 430 431 #ifdef PRIVSEP 432 sent: 433 #endif 434 if (state->rsprobes++ < MAX_RTR_SOLICITATIONS) 435 eloop_timeout_add_sec(ifp->ctx->eloop, 436 RTR_SOLICITATION_INTERVAL, ipv6nd_sendrsprobe, ifp); 437 else 438 logwarnx("%s: no IPv6 Routers available", ifp->name); 439 } 440 441 static void 442 ipv6nd_expire(void *arg) 443 { 444 struct interface *ifp = arg; 445 struct ra *rap; 446 447 if (ifp->ctx->ra_routers == NULL) 448 return; 449 450 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 451 if (rap->iface == ifp && rap->willexpire) 452 rap->doexpire = true; 453 } 454 ipv6nd_expirera(ifp); 455 } 456 457 static void 458 ipv6nd_cancelexpire(struct interface *ifp) 459 { 460 eloop_q_timeout_delete(ifp->ctx->eloop, ELOOP_IPV6RA_EXPIRE, 461 ipv6nd_expire, ifp); 462 } 463 464 void 465 ipv6nd_startexpire(struct interface *ifp) 466 { 467 struct ra *rap; 468 bool found = false; 469 470 if (ifp->ctx->ra_routers == NULL) 471 return; 472 473 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 474 if (rap->iface == ifp) { 475 rap->willexpire = true; 476 found = true; 477 } 478 } 479 if (!found) { 480 ipv6nd_cancelexpire(ifp); 481 return; 482 } 483 eloop_q_timeout_add_sec(ifp->ctx->eloop, ELOOP_IPV6RA_EXPIRE, 484 RTR_CARRIER_EXPIRE, ipv6nd_expire, ifp); 485 } 486 487 int 488 ipv6nd_rtpref(uint8_t flags) 489 { 490 switch (flags & ND_RA_FLAG_RTPREF_MASK) { 491 case ND_RA_FLAG_RTPREF_HIGH: 492 return RTPREF_HIGH; 493 case ND_RA_FLAG_RTPREF_MEDIUM: 494 case ND_RA_FLAG_RTPREF_RSV: 495 return RTPREF_MEDIUM; 496 case ND_RA_FLAG_RTPREF_LOW: 497 return RTPREF_LOW; 498 default: 499 logerrx("%s: impossible RA flag %x", __func__, flags); 500 return RTPREF_INVALID; 501 } 502 /* NOTREACHED */ 503 } 504 505 static void 506 ipv6nd_sortrouters(struct dhcpcd_ctx *ctx) 507 { 508 struct ra_head sorted_routers = TAILQ_HEAD_INITIALIZER(sorted_routers); 509 struct ra *ra1, *ra2; 510 511 while ((ra1 = TAILQ_FIRST(ctx->ra_routers)) != NULL) { 512 TAILQ_REMOVE(ctx->ra_routers, ra1, next); 513 TAILQ_FOREACH(ra2, &sorted_routers, next) { 514 if (ra1->iface->metric > ra2->iface->metric) 515 continue; 516 if (ra1->expired && !ra2->expired) 517 continue; 518 if (ra1->willexpire && !ra2->willexpire) 519 continue; 520 if (ra1->lifetime == 0 && ra2->lifetime != 0) 521 continue; 522 if (!ra1->isreachable && ra2->isreachable) 523 continue; 524 if (ipv6nd_rtpref(ra1->flags) <= 525 ipv6nd_rtpref(ra2->flags)) 526 continue; 527 /* All things being equal, prefer older routers. */ 528 /* We don't need to check time, becase newer 529 * routers are always added to the tail and then 530 * sorted. */ 531 TAILQ_INSERT_BEFORE(ra2, ra1, next); 532 break; 533 } 534 if (ra2 == NULL) 535 TAILQ_INSERT_TAIL(&sorted_routers, ra1, next); 536 } 537 538 TAILQ_CONCAT(ctx->ra_routers, &sorted_routers, next); 539 } 540 541 static void 542 ipv6nd_applyra(struct interface *ifp) 543 { 544 struct ra *rap; 545 struct rs_state *state = RS_STATE(ifp); 546 struct ra defra = { 547 .iface = ifp, 548 .hoplimit = IPV6_DEFHLIM, 549 .reachable = REACHABLE_TIME, 550 .retrans = RETRANS_TIMER, 551 }; 552 553 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 554 if (rap->iface == ifp) 555 break; 556 } 557 558 /* If we have no Router Advertisement, then set default values. */ 559 if (rap == NULL || rap->expired || rap->willexpire) 560 rap = &defra; 561 562 state->retrans = rap->retrans; 563 if (if_applyra(rap) == -1 && errno != ENOENT) 564 logerr(__func__); 565 } 566 567 /* 568 * Neighbour reachability. 569 * 570 * RFC 4681 6.2.5 says when a node is no longer a router it MUST 571 * send a RA with a zero lifetime. 572 * All OS's I know of set the NA router flag if they are a router 573 * or not and disregard that they are actively advertising or 574 * shutting down. If the interface is disabled, it cant't send a NA at all. 575 * 576 * As such we CANNOT rely on the NA Router flag and MUST use 577 * unreachability or receive a RA with a lifetime of zero to remove 578 * the node as a default router. 579 */ 580 void 581 ipv6nd_neighbour(struct dhcpcd_ctx *ctx, struct in6_addr *addr, bool reachable) 582 { 583 struct ra *rap, *rapr; 584 585 if (ctx->ra_routers == NULL) 586 return; 587 588 TAILQ_FOREACH(rap, ctx->ra_routers, next) { 589 if (IN6_ARE_ADDR_EQUAL(&rap->from, addr)) 590 break; 591 } 592 593 if (rap == NULL || rap->expired || rap->isreachable == reachable) 594 return; 595 596 rap->isreachable = reachable; 597 loginfox("%s: %s is %s", rap->iface->name, rap->sfrom, 598 reachable ? "reachable again" : "unreachable"); 599 600 /* See if we can install a reachable default router. */ 601 ipv6nd_sortrouters(ctx); 602 ipv6nd_applyra(rap->iface); 603 rt_build(ctx, AF_INET6); 604 605 if (reachable) 606 return; 607 608 /* If we have no reachable default routers, try and solicit one. */ 609 TAILQ_FOREACH(rapr, ctx->ra_routers, next) { 610 if (rap == rapr || rap->iface != rapr->iface) 611 continue; 612 if (rapr->isreachable && !rapr->expired && rapr->lifetime) 613 break; 614 } 615 616 if (rapr == NULL) 617 ipv6nd_startrs(rap->iface); 618 } 619 620 const struct ipv6_addr * 621 ipv6nd_iffindaddr(const struct interface *ifp, const struct in6_addr *addr, 622 unsigned int flags) 623 { 624 struct ra *rap; 625 struct ipv6_addr *ap; 626 627 if (ifp->ctx->ra_routers == NULL) 628 return NULL; 629 630 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 631 if (rap->iface != ifp) 632 continue; 633 TAILQ_FOREACH(ap, &rap->addrs, next) { 634 if (ipv6_findaddrmatch(ap, addr, flags)) 635 return ap; 636 } 637 } 638 return NULL; 639 } 640 641 struct ipv6_addr * 642 ipv6nd_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, 643 unsigned int flags) 644 { 645 struct ra *rap; 646 struct ipv6_addr *ap; 647 648 if (ctx->ra_routers == NULL) 649 return NULL; 650 651 TAILQ_FOREACH(rap, ctx->ra_routers, next) { 652 TAILQ_FOREACH(ap, &rap->addrs, next) { 653 if (ipv6_findaddrmatch(ap, addr, flags)) 654 return ap; 655 } 656 } 657 return NULL; 658 } 659 660 static struct ipv6_addr * 661 ipv6nd_rapfindprefix(struct ra *rap, const struct in6_addr *pfx, uint8_t pfxlen) 662 { 663 struct ipv6_addr *ia; 664 665 TAILQ_FOREACH(ia, &rap->addrs, next) { 666 if (ia->prefix_vltime == 0) 667 continue; 668 if (ia->prefix_len == pfxlen && 669 IN6_ARE_ADDR_EQUAL(&ia->prefix, pfx)) 670 break; 671 } 672 return ia; 673 } 674 675 struct ipv6_addr * 676 ipv6nd_iffindprefix(struct interface *ifp, const struct in6_addr *pfx, 677 uint8_t pfxlen) 678 { 679 struct ra *rap; 680 struct ipv6_addr *ia; 681 682 ia = NULL; 683 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 684 if (rap->iface != ifp) 685 continue; 686 ia = ipv6nd_rapfindprefix(rap, pfx, pfxlen); 687 if (ia != NULL) 688 break; 689 } 690 return ia; 691 } 692 693 static void 694 ipv6nd_removefreedrop_ra(struct ra *rap, int remove_ra, int drop_ra) 695 { 696 struct dhcpcd_ctx *ctx = rap->iface->ctx; 697 698 eloop_timeout_delete(ctx->eloop, NULL, rap->iface); 699 eloop_timeout_delete(ctx->eloop, NULL, rap); 700 if (remove_ra) 701 TAILQ_REMOVE(ctx->ra_routers, rap, next); 702 ipv6_freedrop_addrs(&rap->addrs, drop_ra, 0, NULL); 703 routeinfohead_free(&rap->rinfos); 704 free(rap->data); 705 free(rap); 706 } 707 708 static void 709 ipv6nd_freedrop_ra(struct ra *rap, int drop) 710 { 711 ipv6nd_removefreedrop_ra(rap, 1, drop); 712 } 713 714 ssize_t 715 ipv6nd_free(struct interface *ifp) 716 { 717 struct rs_state *state; 718 struct ra *rap, *ran; 719 struct dhcpcd_ctx *ctx; 720 ssize_t n; 721 722 state = RS_STATE(ifp); 723 if (state == NULL) 724 return 0; 725 726 ctx = ifp->ctx; 727 ipv6nd_cancelexpire(ifp); 728 #ifdef __sun 729 eloop_event_delete(ctx->eloop, state->nd_fd); 730 close(state->nd_fd); 731 #endif 732 free(state->rs); 733 free(state); 734 ifp->if_data[IF_DATA_IPV6ND] = NULL; 735 n = 0; 736 TAILQ_FOREACH_SAFE(rap, ifp->ctx->ra_routers, next, ran) { 737 if (rap->iface == ifp) { 738 ipv6nd_free_ra(rap); 739 n++; 740 } 741 } 742 743 #ifndef __sun 744 /* If we don't have any more IPv6 enabled interfaces, 745 * close the global socket and release resources */ 746 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 747 if (RS_STATE(ifp)) 748 break; 749 } 750 if (ifp == NULL) { 751 if (ctx->nd_fd != -1) { 752 eloop_event_delete(ctx->eloop, ctx->nd_fd); 753 close(ctx->nd_fd); 754 ctx->nd_fd = -1; 755 } 756 } 757 #endif 758 759 return n; 760 } 761 762 static void 763 ipv6nd_scriptrun(struct ra *rap) 764 { 765 int hasdns, hasaddress; 766 struct ipv6_addr *ap; 767 768 hasaddress = 0; 769 /* If all addresses have completed DAD run the script */ 770 TAILQ_FOREACH(ap, &rap->addrs, next) { 771 if ((ap->flags & (IPV6_AF_AUTOCONF | IPV6_AF_ADDED)) == 772 (IPV6_AF_AUTOCONF | IPV6_AF_ADDED)) { 773 hasaddress = 1; 774 if (!(ap->flags & IPV6_AF_DADCOMPLETED) && 775 ipv6_iffindaddr(ap->iface, &ap->addr, 776 IN6_IFF_TENTATIVE)) 777 ap->flags |= IPV6_AF_DADCOMPLETED; 778 if ((ap->flags & IPV6_AF_DADCOMPLETED) == 0) { 779 logdebugx("%s: waiting for Router Advertisement" 780 " DAD to complete", 781 rap->iface->name); 782 return; 783 } 784 } 785 } 786 787 /* If we don't require RDNSS then set hasdns = 1 so we fork */ 788 if (!(rap->iface->options->options & DHCPCD_IPV6RA_REQRDNSS)) 789 hasdns = 1; 790 else { 791 hasdns = rap->hasdns; 792 } 793 794 script_runreason(rap->iface, "ROUTERADVERT"); 795 if (hasdns && 796 (hasaddress || 797 !(rap->flags & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER)))) 798 dhcpcd_daemonise(rap->iface->ctx); 799 #if 0 800 else if (options & DHCPCD_DAEMONISE && 801 !(options & DHCPCD_DAEMONISED) && new_data) 802 logwarnx("%s: did not fork due to an absent" 803 " RDNSS option in the RA", 804 ifp->name); 805 #endif 806 } 807 808 static void 809 ipv6nd_addaddr(void *arg) 810 { 811 struct ipv6_addr *ap = arg; 812 813 ipv6_addaddr(ap, NULL); 814 } 815 816 int 817 ipv6nd_dadcompleted(const struct interface *ifp) 818 { 819 const struct ra *rap; 820 const struct ipv6_addr *ap; 821 822 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 823 if (rap->iface != ifp) 824 continue; 825 TAILQ_FOREACH(ap, &rap->addrs, next) { 826 if (ap->flags & IPV6_AF_AUTOCONF && 827 ap->flags & IPV6_AF_ADDED && 828 !(ap->flags & IPV6_AF_DADCOMPLETED)) 829 return 0; 830 } 831 } 832 return 1; 833 } 834 835 static void 836 ipv6nd_dadcallback(void *arg) 837 { 838 struct ipv6_addr *ia = arg, *rapap; 839 struct interface *ifp; 840 struct ra *rap; 841 int wascompleted, found; 842 char buf[INET6_ADDRSTRLEN]; 843 const char *p; 844 int dadcounter; 845 846 ifp = ia->iface; 847 wascompleted = (ia->flags & IPV6_AF_DADCOMPLETED); 848 ia->flags |= IPV6_AF_DADCOMPLETED; 849 if (ia->addr_flags & IN6_IFF_DUPLICATED) { 850 ia->dadcounter++; 851 logwarnx("%s: DAD detected %s", ifp->name, ia->saddr); 852 853 /* Try and make another stable private address. 854 * Because ap->dadcounter is always increamented, 855 * a different address is generated. */ 856 /* XXX Cache DAD counter per prefix/id/ssid? */ 857 if (ifp->options->options & DHCPCD_SLAACPRIVATE && 858 IA6_CANAUTOCONF(ia)) { 859 unsigned int delay; 860 861 if (ia->dadcounter >= IDGEN_RETRIES) { 862 logerrx("%s: unable to obtain a" 863 " stable private address", 864 ifp->name); 865 goto try_script; 866 } 867 loginfox("%s: deleting address %s", ifp->name, 868 ia->saddr); 869 if (if_address6(RTM_DELADDR, ia) == -1 && 870 errno != EADDRNOTAVAIL && errno != ENXIO) 871 logerr(__func__); 872 dadcounter = ia->dadcounter; 873 if (ipv6_makestableprivate(&ia->addr, &ia->prefix, 874 ia->prefix_len, ifp, &dadcounter) == -1) { 875 logerr("ipv6_makestableprivate"); 876 return; 877 } 878 ia->dadcounter = dadcounter; 879 ia->flags &= ~(IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED); 880 ia->flags |= IPV6_AF_NEW; 881 p = inet_ntop(AF_INET6, &ia->addr, buf, sizeof(buf)); 882 if (p) 883 snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", 884 p, ia->prefix_len); 885 else 886 ia->saddr[0] = '\0'; 887 delay = arc4random_uniform(IDGEN_DELAY * MSEC_PER_SEC); 888 eloop_timeout_add_msec(ifp->ctx->eloop, delay, 889 ipv6nd_addaddr, ia); 890 return; 891 } 892 } 893 894 try_script: 895 if (!wascompleted) { 896 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 897 if (rap->iface != ifp) 898 continue; 899 wascompleted = 1; 900 found = 0; 901 TAILQ_FOREACH(rapap, &rap->addrs, next) { 902 if (rapap->flags & IPV6_AF_AUTOCONF && 903 rapap->flags & IPV6_AF_ADDED && 904 (rapap->flags & IPV6_AF_DADCOMPLETED) == 905 0) { 906 wascompleted = 0; 907 break; 908 } 909 if (rapap == ia) 910 found = 1; 911 } 912 913 if (wascompleted && found) { 914 logdebugx("%s: Router Advertisement DAD " 915 "completed", 916 rap->iface->name); 917 ipv6nd_scriptrun(rap); 918 } 919 } 920 } 921 } 922 923 static struct ipv6_addr * 924 ipv6nd_findmarkstale(struct ra *rap, struct ipv6_addr *ia, bool mark) 925 { 926 struct dhcpcd_ctx *ctx = ia->iface->ctx; 927 struct ra *rap2; 928 struct ipv6_addr *ia2; 929 930 TAILQ_FOREACH(rap2, ctx->ra_routers, next) { 931 if (rap2 == rap || rap2->iface != rap->iface || rap2->expired) 932 continue; 933 TAILQ_FOREACH(ia2, &rap2->addrs, next) { 934 if (!IN6_ARE_ADDR_EQUAL(&ia->prefix, &ia2->prefix)) 935 continue; 936 if (!(ia2->flags & IPV6_AF_STALE)) 937 return ia2; 938 if (mark) 939 ia2->prefix_pltime = 0; 940 } 941 } 942 return NULL; 943 } 944 945 #ifndef DHCP6 946 /* If DHCPv6 is compiled out, supply a shim to provide an error message 947 * if IPv6RA requests DHCPv6. */ 948 enum DH6S { 949 DH6S_REQUEST, 950 DH6S_INFORM, 951 }; 952 static int 953 dhcp6_start(__unused struct interface *ifp, __unused enum DH6S init_state) 954 { 955 errno = ENOTSUP; 956 return -1; 957 } 958 #endif 959 960 struct nd_policy_ctx { 961 struct dhcpcd_ctx *ctx; 962 int loglevel; 963 const char *ifname; 964 const char *sfrom; 965 struct icmp6_hdr *icp; 966 size_t len; 967 }; 968 969 static int 970 nd_require(uint32_t option, void *arg) 971 { 972 struct nd_policy_ctx *nd_ctx = arg; 973 size_t len = nd_ctx->len, olen; 974 uint8_t *p; 975 struct nd_opt_hdr ndo; 976 977 struct dhcpcd_ctx *ctx = nd_ctx->ctx; 978 const char *soption; 979 980 len -= sizeof(struct nd_router_advert); 981 p = ((uint8_t *)nd_ctx->icp) + sizeof(struct nd_router_advert); 982 for (; len > 0; p += olen, len -= olen) { 983 if (len < sizeof(ndo)) 984 break; 985 memcpy(&ndo, p, sizeof(ndo)); 986 olen = (size_t)ndo.nd_opt_len * 8; 987 if (olen > len) 988 break; 989 if (ndo.nd_opt_type == option) 990 return 0; 991 } 992 993 soption = dhcp_option_string(ctx->nd_opts, ctx->nd_opts_len, option); 994 logmessage(nd_ctx->loglevel, 995 "%s: reject RA (missing option %s) from %s", nd_ctx->ifname, 996 soption, nd_ctx->sfrom); 997 return -1; 998 } 999 1000 static void 1001 ipv6nd_handlera(struct dhcpcd_ctx *ctx, const struct sockaddr_in6 *from, 1002 const char *sfrom, struct interface *ifp, struct icmp6_hdr *icp, size_t len, 1003 int hoplimit) 1004 { 1005 size_t olen, rlen; 1006 struct nd_router_advert *nd_ra; 1007 struct nd_opt_hdr ndo; 1008 struct nd_policy_ctx policy = { 1009 .ctx = ctx, 1010 .icp = icp, 1011 .len = len, 1012 .sfrom = sfrom, 1013 }; 1014 struct nd_opt_prefix_info pi; 1015 struct nd_opt_mtu mtu; 1016 struct nd_opt_rdnss rdnss; 1017 struct nd_opt_ri ri; 1018 struct routeinfo *rinfo; 1019 struct if_options *ifo; 1020 const struct dho_policy_group *pg; 1021 uint8_t *p; 1022 struct ra *rap; 1023 struct in6_addr pi_prefix; 1024 struct ipv6_addr *ia; 1025 bool new_rap, new_data, has_address; 1026 uint32_t old_lifetime; 1027 int err, ifmtu, loglevel; 1028 unsigned int flags; 1029 #ifdef IPV6_MANAGETEMPADDR 1030 bool new_ia; 1031 #endif 1032 1033 #define FREE_RAP(rap) \ 1034 if (new_rap) \ 1035 ipv6nd_removefreedrop_ra(rap, 0, 0); \ 1036 else \ 1037 ipv6nd_free_ra(rap); 1038 1039 if (ifp == NULL || RS_STATE(ifp) == NULL) { 1040 #ifdef DEBUG_RS 1041 logdebugx("RA for unexpected interface from %s", sfrom); 1042 #endif 1043 return; 1044 } 1045 1046 if (len < sizeof(struct nd_router_advert)) { 1047 logerrx("IPv6 RA packet too short from %s", sfrom); 1048 return; 1049 } 1050 1051 /* RFC 4861 7.1.2 */ 1052 if (hoplimit != 255) { 1053 logerrx("invalid hoplimit(%d) in RA from %s", hoplimit, sfrom); 1054 return; 1055 } 1056 if (!IN6_IS_ADDR_LINKLOCAL(&from->sin6_addr)) { 1057 logerrx("RA from non local address %s", sfrom); 1058 return; 1059 } 1060 1061 if (!(ifp->options->options & DHCPCD_IPV6RS)) { 1062 #ifdef DEBUG_RS 1063 logerrx("%s: unexpected RA from %s", ifp->name, sfrom); 1064 #endif 1065 return; 1066 } 1067 1068 /* We could receive a RA before we sent a RS*/ 1069 if (ipv6_linklocal(ifp) == NULL) { 1070 #ifdef DEBUG_RS 1071 logdebugx("%s: received RA from %s (no link-local)", ifp->name, 1072 sfrom); 1073 #endif 1074 return; 1075 } 1076 1077 if (ipv6_iffindaddr(ifp, &from->sin6_addr, IN6_IFF_TENTATIVE)) { 1078 logdebugx("%s: ignoring RA from ourself %s", ifp->name, sfrom); 1079 return; 1080 } 1081 1082 /* 1083 * Because we preserve RA's and expire them quickly after 1084 * carrier up, it's important to reset the kernels notion of 1085 * reachable timers back to default values before applying 1086 * new RA values. 1087 */ 1088 TAILQ_FOREACH(rap, ctx->ra_routers, next) { 1089 if (ifp == rap->iface) 1090 break; 1091 } 1092 if (rap != NULL && rap->willexpire) 1093 ipv6nd_applyra(ifp); 1094 1095 TAILQ_FOREACH(rap, ctx->ra_routers, next) { 1096 if (ifp == rap->iface && 1097 IN6_ARE_ADDR_EQUAL(&rap->from, &from->sin6_addr)) 1098 break; 1099 } 1100 1101 nd_ra = (struct nd_router_advert *)icp; 1102 1103 loglevel = rap == NULL || rap->willexpire || !rap->isreachable ? 1104 LOG_ERR : 1105 LOG_DEBUG; 1106 1107 policy.loglevel = loglevel; 1108 policy.ifname = ifp->name; 1109 ifo = ifp->options; 1110 pg = &ifo->dhopg_nd; 1111 1112 /* Validate */ 1113 rlen = len; 1114 len -= sizeof(struct nd_router_advert); 1115 p = ((uint8_t *)icp) + sizeof(struct nd_router_advert); 1116 for (; len > 0; p += olen, len -= olen) { 1117 if (len < sizeof(ndo)) { 1118 logmessage(loglevel, "%s: short RA option from %s", 1119 ifp->name, sfrom); 1120 break; 1121 } 1122 memcpy(&ndo, p, sizeof(ndo)); 1123 olen = (size_t)ndo.nd_opt_len * 8; 1124 if (olen == 0) { 1125 /* RFC4681 4.6 says we MUST discard this ND packet. */ 1126 logmessage(loglevel, "%s: zero length RA option %s", 1127 ifp->name, sfrom); 1128 return; 1129 } 1130 if (olen > len) { 1131 logmessage(loglevel, 1132 "%s: RA option length exceeds message from %s", 1133 ifp->name, sfrom); 1134 break; 1135 } 1136 1137 if (!dho_policy_allowed(pg, ndo.nd_opt_type)) { 1138 const char *soption = dhcp_option_string(ctx->nd_opts, 1139 ctx->nd_opts_len, ndo.nd_opt_type); 1140 logmessage(loglevel, 1141 "%s: reject RA (option %s) from %s", ifp->name, 1142 soption, sfrom); 1143 return; 1144 } 1145 } 1146 len = rlen; 1147 1148 err = dho_policy_check(&pg->dhop_require, nd_require, &policy); 1149 if (err == -1) 1150 return; 1151 1152 /* We don't want to spam the log with the fact we got an RA every 1153 * 30 seconds or so, so only spam the log if it's different. */ 1154 if (rap == NULL || 1155 (rap->data_len != len || 1156 memcmp(rap->data, (unsigned char *)icp, rap->data_len) != 0)) { 1157 if (rap) { 1158 free(rap->data); 1159 rap->data_len = 0; 1160 } 1161 new_data = true; 1162 } else 1163 new_data = false; 1164 if (rap == NULL) { 1165 rap = calloc(1, sizeof(*rap)); 1166 if (rap == NULL) { 1167 logerr(__func__); 1168 return; 1169 } 1170 rap->iface = ifp; 1171 rap->from = from->sin6_addr; 1172 strlcpy(rap->sfrom, sfrom, sizeof(rap->sfrom)); 1173 TAILQ_INIT(&rap->addrs); 1174 TAILQ_INIT(&rap->rinfos); 1175 new_rap = true; 1176 rap->isreachable = true; 1177 } else 1178 new_rap = false; 1179 if (rap->data_len == 0) { 1180 rap->data = malloc(len); 1181 if (rap->data == NULL) { 1182 logerr(__func__); 1183 if (new_rap) 1184 free(rap); 1185 return; 1186 } 1187 memcpy(rap->data, icp, len); 1188 rap->data_len = len; 1189 } 1190 1191 /* We could change the debug level based on new_data, but some 1192 * routers like to decrease the advertised valid and preferred times 1193 * in accordance with the own prefix times which would result in too 1194 * much needless log spam. */ 1195 if (rap->willexpire) 1196 new_data = true; 1197 loglevel = new_rap || rap->willexpire || !rap->isreachable ? LOG_INFO : 1198 LOG_DEBUG; 1199 logmessage(loglevel, "%s: Router Advertisement from %s", ifp->name, 1200 rap->sfrom); 1201 1202 clock_gettime(CLOCK_MONOTONIC, &rap->acquired); 1203 rap->flags = nd_ra->nd_ra_flags_reserved; 1204 old_lifetime = rap->lifetime; 1205 rap->lifetime = ntohs(nd_ra->nd_ra_router_lifetime); 1206 if (nd_ra->nd_ra_curhoplimit != 0) 1207 rap->hoplimit = nd_ra->nd_ra_curhoplimit; 1208 else 1209 rap->hoplimit = IPV6_DEFHLIM; 1210 if (nd_ra->nd_ra_reachable != 0) { 1211 rap->reachable = ntohl(nd_ra->nd_ra_reachable); 1212 if (rap->reachable > MAX_REACHABLE_TIME) 1213 rap->reachable = 0; 1214 } else 1215 rap->reachable = REACHABLE_TIME; 1216 if (nd_ra->nd_ra_retransmit != 0) 1217 rap->retrans = ntohl(nd_ra->nd_ra_retransmit); 1218 else 1219 rap->retrans = RETRANS_TIMER; 1220 rap->expired = rap->willexpire = rap->doexpire = false; 1221 rap->hasdns = false; 1222 rap->isreachable = true; 1223 has_address = false; 1224 rap->mtu = 0; 1225 1226 #ifdef IPV6_AF_TEMPORARY 1227 ipv6_markaddrsstale(ifp, IPV6_AF_TEMPORARY); 1228 #endif 1229 TAILQ_FOREACH(ia, &rap->addrs, next) { 1230 ia->flags |= IPV6_AF_STALE; 1231 } 1232 1233 len -= sizeof(struct nd_router_advert); 1234 p = ((uint8_t *)icp) + sizeof(struct nd_router_advert); 1235 for (; len > 0; p += olen, len -= olen) { 1236 if (len < sizeof(ndo)) 1237 break; 1238 memcpy(&ndo, p, sizeof(ndo)); 1239 olen = (size_t)ndo.nd_opt_len * 8; 1240 if (olen > len) 1241 break; 1242 1243 if (!dho_policy_allowed(pg, ndo.nd_opt_type)) 1244 continue; 1245 1246 switch (ndo.nd_opt_type) { 1247 case ND_OPT_PREFIX_INFORMATION: { 1248 uint32_t vltime, pltime; 1249 1250 loglevel = new_data ? LOG_ERR : LOG_DEBUG; 1251 if (ndo.nd_opt_len != 4) { 1252 logmessage(loglevel, 1253 "%s: invalid option len for prefix", 1254 ifp->name); 1255 continue; 1256 } 1257 memcpy(&pi, p, sizeof(pi)); 1258 if (pi.nd_opt_pi_prefix_len > 128) { 1259 logmessage(loglevel, "%s: invalid prefix len", 1260 ifp->name); 1261 continue; 1262 } 1263 /* nd_opt_pi_prefix is not aligned. */ 1264 memcpy(&pi_prefix, &pi.nd_opt_pi_prefix, 1265 sizeof(pi_prefix)); 1266 if (IN6_IS_ADDR_MULTICAST(&pi_prefix) || 1267 IN6_IS_ADDR_LINKLOCAL(&pi_prefix)) { 1268 logmessage(loglevel, "%s: invalid prefix in RA", 1269 ifp->name); 1270 continue; 1271 } 1272 1273 vltime = ntohl(pi.nd_opt_pi_valid_time); 1274 pltime = ntohl(pi.nd_opt_pi_preferred_time); 1275 if (pltime > vltime) { 1276 logmessage(loglevel, "%s: pltime > vltime", 1277 ifp->name); 1278 continue; 1279 } 1280 1281 flags = IPV6_AF_RAPFX; 1282 /* If no flags are set, that means the prefix is 1283 * available via the router. */ 1284 if (pi.nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK) 1285 flags |= IPV6_AF_ONLINK; 1286 if (pi.nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO && 1287 rap->iface->options->options & 1288 DHCPCD_IPV6RA_AUTOCONF) 1289 flags |= IPV6_AF_AUTOCONF; 1290 if (pi.nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ROUTER) 1291 flags |= IPV6_AF_ROUTER; 1292 1293 ia = ipv6nd_rapfindprefix(rap, &pi_prefix, 1294 pi.nd_opt_pi_prefix_len); 1295 if (ia == NULL) { 1296 ia = ipv6_newaddr(rap->iface, &pi_prefix, 1297 pi.nd_opt_pi_prefix_len, flags); 1298 if (ia == NULL) 1299 break; 1300 1301 ia->prefix = pi_prefix; 1302 ia->created = ia->acquired = rap->acquired; 1303 ia->prefix_vltime = vltime; 1304 ia->prefix_pltime = pltime; 1305 1306 if (flags & IPV6_AF_AUTOCONF) 1307 ia->dadcallback = ipv6nd_dadcallback; 1308 1309 TAILQ_INSERT_TAIL(&rap->addrs, ia, next); 1310 1311 #ifdef IPV6_MANAGETEMPADDR 1312 /* New address to dhcpcd RA handling. 1313 * If the address already exists and a valid 1314 * temporary address also exists then 1315 * extend the existing one rather than 1316 * create a new one */ 1317 if (flags & IPV6_AF_AUTOCONF && 1318 ipv6_iffindaddr(ifp, &ia->addr, 1319 IN6_IFF_NOTUSEABLE) && 1320 ipv6_settemptime(ia, 0)) 1321 new_ia = false; 1322 else 1323 new_ia = true; 1324 #endif 1325 1326 } else { 1327 uint32_t rmtime; 1328 1329 /* 1330 * RFC 4862 5.5.3.e 1331 * Don't terminate existing connections. 1332 * This means that to actually remove the 1333 * existing prefix, the RA needs to stop 1334 * broadcasting the prefix and just let it 1335 * expire in 2 hours. 1336 * It might want to broadcast it to reduce 1337 * the vltime if it was greater than 2 hours 1338 * to start with/ 1339 */ 1340 ia->prefix_pltime = pltime; 1341 if (ia->prefix_vltime) { 1342 uint32_t elapsed; 1343 1344 elapsed = (uint32_t) 1345 eloop_timespec_diff(&rap->acquired, 1346 &ia->acquired, NULL); 1347 rmtime = ia->prefix_vltime - elapsed; 1348 if (rmtime > ia->prefix_vltime) 1349 rmtime = 0; 1350 } else 1351 rmtime = 0; 1352 if (vltime > MIN_EXTENDED_VLTIME || 1353 vltime > rmtime) 1354 ia->prefix_vltime = vltime; 1355 else if (rmtime <= MIN_EXTENDED_VLTIME) 1356 /* No SEND support from RFC 3971 so 1357 * leave vltime alone */ 1358 ia->prefix_vltime = rmtime; 1359 else 1360 ia->prefix_vltime = MIN_EXTENDED_VLTIME; 1361 1362 /* Ensure pltime still fits */ 1363 if (pltime < ia->prefix_vltime) 1364 ia->prefix_pltime = pltime; 1365 else 1366 ia->prefix_pltime = ia->prefix_vltime; 1367 1368 ia->flags &= ~RA_STALE_FLAGS; 1369 ia->flags |= flags; 1370 ia->acquired = rap->acquired; 1371 1372 #ifdef IPV6_MANAGETEMPADDR 1373 new_ia = false; 1374 #endif 1375 } 1376 1377 if (ia->prefix_vltime != 0 && 1378 ia->flags & IPV6_AF_AUTOCONF) 1379 has_address = true; 1380 1381 #ifdef IPV6_MANAGETEMPADDR 1382 /* RFC4941 Section 3.3.3 */ 1383 if (ia->flags & IPV6_AF_AUTOCONF && 1384 ia->iface->options->options & DHCPCD_SLAACTEMP && 1385 IA6_CANAUTOCONF(ia)) { 1386 if (!new_ia) { 1387 if (ipv6_settemptime(ia, 1) == NULL) 1388 new_ia = true; 1389 } 1390 if (new_ia && ia->prefix_pltime) { 1391 if (ipv6_createtempaddr(ia, 1392 &ia->acquired) == NULL) 1393 logerr("ipv6_createtempaddr"); 1394 } 1395 } 1396 #endif 1397 break; 1398 } 1399 1400 case ND_OPT_MTU: 1401 if (len < sizeof(mtu)) { 1402 logmessage(loglevel, "%s: short MTU option", 1403 ifp->name); 1404 break; 1405 } 1406 memcpy(&mtu, p, sizeof(mtu)); 1407 mtu.nd_opt_mtu_mtu = ntohl(mtu.nd_opt_mtu_mtu); 1408 if (mtu.nd_opt_mtu_mtu < IPV6_MMTU) { 1409 logmessage(loglevel, "%s: invalid MTU %d", 1410 ifp->name, mtu.nd_opt_mtu_mtu); 1411 break; 1412 } 1413 ifmtu = if_getmtu(ifp); 1414 if (ifmtu == -1) 1415 logerr("if_getmtu"); 1416 else if (mtu.nd_opt_mtu_mtu > (uint32_t)ifmtu) { 1417 logmessage(loglevel, 1418 "%s: advertised MTU %d" 1419 " is greater than link MTU %d", 1420 ifp->name, mtu.nd_opt_mtu_mtu, ifmtu); 1421 rap->mtu = (uint32_t)ifmtu; 1422 } else 1423 rap->mtu = mtu.nd_opt_mtu_mtu; 1424 break; 1425 case ND_OPT_RDNSS: 1426 if (len < sizeof(rdnss)) { 1427 logmessage(loglevel, "%s: short RDNSS option", 1428 ifp->name); 1429 break; 1430 } 1431 memcpy(&rdnss, p, sizeof(rdnss)); 1432 if (rdnss.nd_opt_rdnss_lifetime && 1433 rdnss.nd_opt_rdnss_len > 1) 1434 rap->hasdns = 1; 1435 break; 1436 case ND_OPT_RI: 1437 if (ndo.nd_opt_len > 3) { 1438 logmessage(loglevel, 1439 "%s: invalid route info option", ifp->name); 1440 break; 1441 } 1442 memset(&ri, 0, sizeof(ri)); 1443 memcpy(&ri, p, olen); /* may be smaller than sizeof(ri), 1444 pad with zero */ 1445 if (ri.nd_opt_ri_prefixlen > 128) { 1446 logmessage(loglevel, 1447 "%s: invalid route info prefix length", 1448 ifp->name); 1449 break; 1450 } 1451 1452 /* rfc4191 3.1 - RI for ::/0 applies to default route */ 1453 if (ri.nd_opt_ri_prefixlen == 0) { 1454 rap->lifetime = ntohl(ri.nd_opt_ri_lifetime); 1455 1456 /* Update preference leaving other flags intact 1457 */ 1458 rap->flags = 1459 ((rap->flags & 1460 (~(unsigned int) 1461 ND_RA_FLAG_RTPREF_MASK)) | 1462 ri.nd_opt_ri_flags_reserved) & 1463 0xff; 1464 1465 break; 1466 } 1467 1468 /* Update existing route info instead of rebuilding all 1469 routes so that previously announced but now absent 1470 routes can stay alive. To kill a route early, an RI 1471 with lifetime=0 needs to be received (rfc4191 3.1)*/ 1472 rinfo = routeinfo_findalloc(rap, &ri.nd_opt_ri_prefix, 1473 ri.nd_opt_ri_prefixlen); 1474 if (rinfo == NULL) { 1475 logerr(__func__); 1476 break; 1477 } 1478 1479 /* Update/initialize other route info params */ 1480 rinfo->flags = ri.nd_opt_ri_flags_reserved; 1481 rinfo->lifetime = ntohl(ri.nd_opt_ri_lifetime); 1482 rinfo->acquired = rap->acquired; 1483 1484 break; 1485 default: 1486 continue; 1487 } 1488 } 1489 1490 TAILQ_FOREACH(ia, &rap->addrs, next) { 1491 if (!(ia->flags & IPV6_AF_STALE) || ia->prefix_pltime == 0) 1492 continue; 1493 if (ipv6nd_findmarkstale(rap, ia, false) != NULL) 1494 continue; 1495 ipv6nd_findmarkstale(rap, ia, true); 1496 logdebugx("%s: %s: became stale", ifp->name, ia->saddr); 1497 /* Technically this violates RFC 4861 6.3.4, 1498 * but we need a mechanism to tell the kernel to 1499 * try and prefer other addresses. */ 1500 ia->prefix_pltime = 0; 1501 } 1502 1503 if (!new_rap && rap->lifetime == 0 && old_lifetime != 0) 1504 logwarnx("%s: %s: no longer a default router (lifetime = 0)", 1505 ifp->name, rap->sfrom); 1506 1507 if (new_data && !has_address && rap->lifetime && 1508 ifp->options->options & DHCPCD_GATEWAY && !ipv6_anyglobal(ifp)) 1509 logwarnx("%s: no global addresses for default route", 1510 ifp->name); 1511 1512 if (new_rap) 1513 TAILQ_INSERT_TAIL(ctx->ra_routers, rap, next); 1514 if (new_data) 1515 ipv6nd_sortrouters(ifp->ctx); 1516 1517 if (ifp->ctx->options & DHCPCD_TEST) { 1518 script_runreason(ifp, "TEST"); 1519 goto handle_flag; 1520 } 1521 1522 if (!(ifp->options->options & DHCPCD_CONFIGURE)) 1523 goto run; 1524 1525 ipv6nd_applyra(ifp); 1526 ipv6_addaddrs(&rap->addrs); 1527 #ifdef IPV6_MANAGETEMPADDR 1528 ipv6_addtempaddrs(ifp, &rap->acquired); 1529 #endif 1530 rt_build(ifp->ctx, AF_INET6); 1531 1532 run: 1533 ipv6nd_scriptrun(rap); 1534 1535 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 1536 eloop_timeout_delete(ifp->ctx->eloop, NULL, rap); /* reachable timer */ 1537 1538 handle_flag: 1539 if (!(ifp->options->options & DHCPCD_DHCP6)) 1540 goto nodhcp6; 1541 /* Only log a DHCPv6 start error if compiled in or debugging is enabled. */ 1542 #ifdef DHCP6 1543 #define LOG_DHCP6 logerr 1544 #else 1545 #define LOG_DHCP6 logdebug 1546 #endif 1547 if (rap->flags & ND_RA_FLAG_MANAGED) { 1548 if (new_data && dhcp6_start(ifp, DH6S_REQUEST) == -1) 1549 LOG_DHCP6("dhcp6_start: %s", ifp->name); 1550 } else if (rap->flags & ND_RA_FLAG_OTHER) { 1551 if (new_data && dhcp6_start(ifp, DH6S_INFORM) == -1) 1552 LOG_DHCP6("dhcp6_start: %s", ifp->name); 1553 } else { 1554 #ifdef DHCP6 1555 if (new_data) 1556 logdebugx("%s: No DHCPv6 instruction in RA", ifp->name); 1557 #endif 1558 nodhcp6: 1559 if (ifp->ctx->options & DHCPCD_TEST) { 1560 eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS); 1561 return; 1562 } 1563 } 1564 1565 /* Expire should be called last as the rap object could be destroyed */ 1566 ipv6nd_expirera(ifp); 1567 #undef FREE_RAP 1568 } 1569 1570 bool 1571 ipv6nd_hasralifetime(const struct interface *ifp, bool lifetime) 1572 { 1573 const struct ra *rap; 1574 1575 if (ifp->ctx->ra_routers) { 1576 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) 1577 if (rap->iface == ifp && !rap->expired && 1578 (!lifetime || rap->lifetime)) 1579 return true; 1580 } 1581 return false; 1582 } 1583 1584 bool 1585 ipv6nd_hasradhcp(const struct interface *ifp, bool managed) 1586 { 1587 const struct ra *rap; 1588 1589 if (ifp->ctx->ra_routers) { 1590 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 1591 if (rap->iface == ifp && !rap->expired && 1592 !rap->willexpire && 1593 ((managed && rap->flags & ND_RA_FLAG_MANAGED) || 1594 (!managed && rap->flags & ND_RA_FLAG_OTHER))) 1595 return true; 1596 } 1597 } 1598 return false; 1599 } 1600 1601 static const uint8_t * 1602 ipv6nd_getoption(struct dhcpcd_ctx *ctx, size_t *os, unsigned int *code, 1603 size_t *len, const uint8_t *od, size_t ol, struct dhcp_opt **oopt) 1604 { 1605 struct nd_opt_hdr ndo; 1606 size_t i; 1607 struct dhcp_opt *opt; 1608 1609 if (od) { 1610 *os = sizeof(ndo); 1611 if (ol < *os) { 1612 errno = EINVAL; 1613 return NULL; 1614 } 1615 memcpy(&ndo, od, sizeof(ndo)); 1616 i = (size_t)(ndo.nd_opt_len * 8); 1617 if (i > ol) { 1618 errno = EINVAL; 1619 return NULL; 1620 } 1621 *len = i; 1622 *code = ndo.nd_opt_type; 1623 } 1624 1625 for (i = 0, opt = ctx->nd_opts; i < ctx->nd_opts_len; i++, opt++) { 1626 if (opt->option == *code) { 1627 *oopt = opt; 1628 break; 1629 } 1630 } 1631 1632 if (od) 1633 return od + sizeof(ndo); 1634 return NULL; 1635 } 1636 1637 ssize_t 1638 ipv6nd_env(FILE *fp, const struct interface *ifp) 1639 { 1640 size_t i, j, n, len, olen; 1641 struct ra *rap; 1642 char ndprefix[32]; 1643 struct dhcp_opt *opt; 1644 uint8_t *p; 1645 struct nd_opt_hdr ndo; 1646 struct ipv6_addr *ia; 1647 struct timespec now; 1648 int pref; 1649 const struct dho_policy_group *pg; 1650 1651 clock_gettime(CLOCK_MONOTONIC, &now); 1652 i = n = 0; 1653 TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { 1654 if (rap->iface != ifp || rap->expired) 1655 continue; 1656 pg = &rap->iface->options->dhopg_nd; 1657 i++; 1658 snprintf(ndprefix, sizeof(ndprefix), "nd%zu", i); 1659 if (efprintf(fp, "%s_from=%s", ndprefix, rap->sfrom) == -1) 1660 return -1; 1661 if (efprintf(fp, "%s_acquired=%lld", ndprefix, 1662 (long long)rap->acquired.tv_sec) == -1) 1663 return -1; 1664 if (efprintf(fp, "%s_now=%lld", ndprefix, 1665 (long long)now.tv_sec) == -1) 1666 return -1; 1667 if (efprintf(fp, "%s_hoplimit=%u", ndprefix, rap->hoplimit) == 1668 -1) 1669 return -1; 1670 pref = ipv6nd_rtpref(rap->flags); 1671 if (efprintf(fp, "%s_flags=%s%s%s%s%s", ndprefix, 1672 rap->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1673 rap->flags & ND_RA_FLAG_OTHER ? "O" : "", 1674 rap->flags & ND_RA_FLAG_HOME_AGENT ? "H" : "", 1675 pref == RTPREF_HIGH ? "h" : 1676 pref == RTPREF_LOW ? "l" : 1677 "", 1678 rap->flags & ND_RA_FLAG_PROXY ? "P" : "") == -1) 1679 return -1; 1680 if (efprintf(fp, "%s_lifetime=%u", ndprefix, rap->lifetime) == 1681 -1) 1682 return -1; 1683 1684 /* Zero our indexes */ 1685 for (j = 0, opt = rap->iface->ctx->nd_opts; 1686 j < rap->iface->ctx->nd_opts_len; j++, opt++) 1687 dhcp_zero_index(opt); 1688 for (j = 0, opt = rap->iface->options->nd_override; 1689 j < rap->iface->options->nd_override_len; j++, opt++) 1690 dhcp_zero_index(opt); 1691 1692 /* Unlike DHCP, ND6 options *may* occur more than once. 1693 * There is also no provision for option concatenation 1694 * unlike DHCP. */ 1695 len = rap->data_len - sizeof(struct nd_router_advert); 1696 for (p = rap->data + sizeof(struct nd_router_advert); 1697 len >= sizeof(ndo); p += olen, len -= olen) { 1698 memcpy(&ndo, p, sizeof(ndo)); 1699 olen = (size_t)(ndo.nd_opt_len * 8); 1700 if (olen > len) { 1701 errno = EINVAL; 1702 break; 1703 } 1704 if (!dho_policy_allowed(pg, ndo.nd_opt_type)) 1705 continue; 1706 for (j = 0, opt = rap->iface->options->nd_override; 1707 j < rap->iface->options->nd_override_len; 1708 j++, opt++) 1709 if (opt->option == ndo.nd_opt_type) 1710 break; 1711 if (j == rap->iface->options->nd_override_len) { 1712 for (j = 0, opt = rap->iface->ctx->nd_opts; 1713 j < rap->iface->ctx->nd_opts_len; 1714 j++, opt++) 1715 if (opt->option == ndo.nd_opt_type) 1716 break; 1717 if (j == rap->iface->ctx->nd_opts_len) 1718 opt = NULL; 1719 } 1720 if (opt == NULL) 1721 continue; 1722 dhcp_envoption(rap->iface->ctx, fp, ndprefix, 1723 rap->iface->name, opt, ipv6nd_getoption, 1724 p + sizeof(ndo), olen - sizeof(ndo)); 1725 } 1726 1727 /* We need to output the addresses we actually made 1728 * from the prefix information options as well. */ 1729 j = 0; 1730 TAILQ_FOREACH(ia, &rap->addrs, next) { 1731 if (!(ia->flags & IPV6_AF_AUTOCONF) || 1732 #ifdef IPV6_AF_TEMPORARY 1733 ia->flags & IPV6_AF_TEMPORARY || 1734 #endif 1735 !(ia->flags & IPV6_AF_ADDED) || 1736 ia->prefix_vltime == 0) 1737 continue; 1738 if (efprintf(fp, "%s_addr%zu=%s", ndprefix, ++j, 1739 ia->saddr) == -1) 1740 return -1; 1741 } 1742 } 1743 return 1; 1744 } 1745 1746 void 1747 ipv6nd_handleifa(int cmd, struct ipv6_addr *addr, pid_t pid) 1748 { 1749 struct ra *rap; 1750 1751 /* IPv6 init may not have happened yet if we are learning 1752 * existing addresses when dhcpcd starts. */ 1753 if (addr->iface->ctx->ra_routers == NULL) 1754 return; 1755 1756 TAILQ_FOREACH(rap, addr->iface->ctx->ra_routers, next) { 1757 if (rap->iface != addr->iface) 1758 continue; 1759 ipv6_handleifa_addrs(cmd, &rap->addrs, addr, pid); 1760 } 1761 } 1762 1763 void 1764 ipv6nd_expirera(void *arg) 1765 { 1766 struct interface *ifp; 1767 const struct dho_policy_group *pg; 1768 struct ra *rap, *ran; 1769 struct timespec now; 1770 bool expired, valid; 1771 struct ipv6_addr *ia; 1772 struct routeinfo *rinfo, *rinfob; 1773 size_t len, olen; 1774 uint8_t *p; 1775 struct nd_opt_hdr ndo; 1776 #if 0 1777 struct nd_opt_prefix_info pi; 1778 #endif 1779 struct nd_opt_dnssl dnssl; 1780 struct nd_opt_rdnss rdnss; 1781 uint32_t next = 0, ltime, elapsed; 1782 size_t nexpired = 0; 1783 1784 ifp = arg; 1785 timespecclear(&now); 1786 expired = false; 1787 1788 TAILQ_FOREACH_SAFE(rap, ifp->ctx->ra_routers, next, ran) { 1789 if (rap->iface != ifp || rap->expired) 1790 continue; 1791 valid = false; 1792 pg = &rap->iface->options->dhopg_nd; 1793 /* lifetime may be set to infinite by rfc4191 route information 1794 */ 1795 if (rap->lifetime) { 1796 ltime = lifetime_left(rap->lifetime, &rap->acquired, 1797 &now); 1798 if (ltime == 0 || rap->doexpire) { 1799 if (!rap->expired) { 1800 logwarnx("%s: %s: router expired", 1801 ifp->name, rap->sfrom); 1802 rap->lifetime = 0; 1803 expired = true; 1804 } 1805 } else { 1806 valid = true; 1807 if (next == 0 || ltime < next) 1808 next = ltime; 1809 } 1810 } 1811 1812 /* Not every prefix is tied to an address which 1813 * the kernel can expire, so we need to handle it ourself. 1814 * Also, some OS don't support address lifetimes (Solaris). */ 1815 TAILQ_FOREACH(ia, &rap->addrs, next) { 1816 if (ia->prefix_vltime == 0) 1817 continue; 1818 ltime = lifetime_left(ia->prefix_vltime, &ia->acquired, 1819 &now); 1820 if (ltime == 0 || rap->doexpire) { 1821 if (ia->flags & IPV6_AF_ADDED) { 1822 logwarnx("%s: expired %s %s", 1823 ia->iface->name, 1824 ia->flags & IPV6_AF_AUTOCONF ? 1825 "address" : 1826 "prefix", 1827 ia->saddr); 1828 if (if_address6(RTM_DELADDR, ia) == 1829 -1 && 1830 errno != EADDRNOTAVAIL && 1831 errno != ENXIO) 1832 logerr(__func__); 1833 } 1834 ia->prefix_vltime = ia->prefix_pltime = 0; 1835 ia->flags &= ~( 1836 IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED); 1837 expired = true; 1838 } else { 1839 valid = true; 1840 if (next == 0 || ltime < next) 1841 next = ltime; 1842 } 1843 } 1844 1845 /* Expire route information */ 1846 TAILQ_FOREACH_SAFE(rinfo, &rap->rinfos, next, rinfob) { 1847 ltime = lifetime_left(rinfo->lifetime, &rinfo->acquired, 1848 &now); 1849 if (ltime == 0 || rap->doexpire) { 1850 logwarnx("%s: expired route %s", 1851 rap->iface->name, rinfo->sprefix); 1852 TAILQ_REMOVE(&rap->rinfos, rinfo, next); 1853 free(rinfo); 1854 } 1855 } 1856 1857 /* Work out expiry for ND options */ 1858 elapsed = (uint32_t)eloop_timespec_diff(&now, &rap->acquired, 1859 NULL); 1860 len = rap->data_len - sizeof(struct nd_router_advert); 1861 for (p = rap->data + sizeof(struct nd_router_advert); 1862 len >= sizeof(ndo); p += olen, len -= olen) { 1863 memcpy(&ndo, p, sizeof(ndo)); 1864 olen = (size_t)(ndo.nd_opt_len * 8); 1865 if (olen > len) { 1866 errno = EINVAL; 1867 break; 1868 } 1869 1870 if (!dho_policy_allowed(pg, ndo.nd_opt_type)) 1871 continue; 1872 1873 switch (ndo.nd_opt_type) { 1874 /* Prefix info is already checked in the above 1875 * loop. */ 1876 #if 0 1877 case ND_OPT_PREFIX_INFORMATION: 1878 if (len < sizeof(pi)) 1879 break; 1880 memcpy(&pi, p, sizeof(pi)); 1881 ltime = pi.nd_opt_pi_valid_time; 1882 break; 1883 #endif 1884 case ND_OPT_DNSSL: 1885 if (len < sizeof(dnssl)) 1886 continue; 1887 memcpy(&dnssl, p, sizeof(dnssl)); 1888 ltime = dnssl.nd_opt_dnssl_lifetime; 1889 break; 1890 case ND_OPT_RDNSS: 1891 if (len < sizeof(rdnss)) 1892 continue; 1893 memcpy(&rdnss, p, sizeof(rdnss)); 1894 ltime = rdnss.nd_opt_rdnss_lifetime; 1895 break; 1896 default: 1897 continue; 1898 } 1899 1900 if (ltime == 0) 1901 continue; 1902 if (rap->doexpire) { 1903 expired = true; 1904 continue; 1905 } 1906 if (ltime == ND6_INFINITE_LIFETIME) { 1907 valid = true; 1908 continue; 1909 } 1910 1911 ltime = ntohl(ltime); 1912 if (elapsed >= ltime) { 1913 expired = true; 1914 continue; 1915 } 1916 1917 valid = true; 1918 ltime -= elapsed; 1919 if (next == 0 || ltime < next) 1920 next = ltime; 1921 } 1922 1923 if (valid) 1924 continue; 1925 1926 /* Router has expired. Let's not keep a lot of them. */ 1927 rap->expired = true; 1928 if (++nexpired > EXPIRED_MAX) 1929 ipv6nd_free_ra(rap); 1930 } 1931 1932 if (next != 0) 1933 eloop_timeout_add_sec(ifp->ctx->eloop, next, ipv6nd_expirera, 1934 ifp); 1935 if (expired) { 1936 logwarnx("%s: part of a Router Advertisement expired", 1937 ifp->name); 1938 ipv6nd_sortrouters(ifp->ctx); 1939 ipv6nd_applyra(ifp); 1940 rt_build(ifp->ctx, AF_INET6); 1941 script_runreason(ifp, "ROUTERADVERT"); 1942 } 1943 } 1944 1945 void 1946 ipv6nd_drop(struct interface *ifp) 1947 { 1948 struct ra *rap, *ran; 1949 bool expired = false; 1950 1951 ipv6nd_cancelexpire(ifp); 1952 if (ifp->ctx->ra_routers == NULL) 1953 return; 1954 1955 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 1956 TAILQ_FOREACH_SAFE(rap, ifp->ctx->ra_routers, next, ran) { 1957 if (rap->iface == ifp) { 1958 rap->expired = expired = true; 1959 ipv6nd_drop_ra(rap); 1960 } 1961 } 1962 if (expired) { 1963 ipv6nd_applyra(ifp); 1964 rt_build(ifp->ctx, AF_INET6); 1965 if ((ifp->options->options & DHCPCD_NODROP) != DHCPCD_NODROP) 1966 script_runreason(ifp, "ROUTERADVERT"); 1967 } 1968 } 1969 1970 void 1971 ipv6nd_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg) 1972 { 1973 struct sockaddr_in6 *from = (struct sockaddr_in6 *)msg->msg_name; 1974 char sfrom[INET6_ADDRSTRLEN]; 1975 int hoplimit = 0; 1976 struct icmp6_hdr *icp; 1977 struct interface *ifp; 1978 size_t len = msg->msg_iov[0].iov_len; 1979 1980 inet_ntop(AF_INET6, &from->sin6_addr, sfrom, sizeof(sfrom)); 1981 if ((size_t)len < sizeof(struct icmp6_hdr)) { 1982 logerrx("IPv6 ICMP packet too short from %s", sfrom); 1983 return; 1984 } 1985 1986 ifp = if_findifpfromcmsg(ctx, msg, &hoplimit); 1987 if (ifp == NULL) { 1988 logerr(__func__); 1989 return; 1990 } 1991 1992 /* Don't do anything if the user hasn't configured it. */ 1993 if (ifp->active != IF_ACTIVE_USER || 1994 ifp->options->options & DHCPCD_STOPPING || 1995 !(ifp->options->options & DHCPCD_IPV6)) 1996 return; 1997 1998 icp = (struct icmp6_hdr *)msg->msg_iov[0].iov_base; 1999 if (icp->icmp6_code == 0) { 2000 switch (icp->icmp6_type) { 2001 case ND_ROUTER_ADVERT: 2002 ipv6nd_handlera(ctx, from, sfrom, ifp, icp, (size_t)len, 2003 hoplimit); 2004 return; 2005 } 2006 } 2007 2008 logerrx("invalid IPv6 type %d or code %d from %s", icp->icmp6_type, 2009 icp->icmp6_code, sfrom); 2010 } 2011 2012 static void 2013 ipv6nd_handledata(void *arg, unsigned short events) 2014 { 2015 struct dhcpcd_ctx *ctx; 2016 int fd; 2017 struct sockaddr_in6 from; 2018 union { 2019 struct icmp6_hdr hdr; 2020 uint8_t buf[64 * 1024]; /* Maximum ICMPv6 size */ 2021 } iovbuf; 2022 struct iovec iov = { 2023 .iov_base = iovbuf.buf, 2024 .iov_len = sizeof(iovbuf.buf), 2025 }; 2026 union { 2027 struct cmsghdr hdr; 2028 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo)) + 2029 CMSG_SPACE(sizeof(int))]; 2030 } cmsgbuf = { .buf = { 0 } }; 2031 struct msghdr msg = { 2032 .msg_name = &from, 2033 .msg_namelen = sizeof(from), 2034 .msg_iov = &iov, 2035 .msg_iovlen = 1, 2036 .msg_control = cmsgbuf.buf, 2037 .msg_controllen = sizeof(cmsgbuf.buf), 2038 }; 2039 ssize_t len; 2040 2041 #ifdef __sun 2042 struct interface *ifp; 2043 struct rs_state *state; 2044 2045 ifp = arg; 2046 state = RS_STATE(ifp); 2047 ctx = ifp->ctx; 2048 fd = state->nd_fd; 2049 #else 2050 ctx = arg; 2051 fd = ctx->nd_fd; 2052 #endif 2053 2054 if (events != ELE_READ) 2055 logerrx("%s: unexpected event 0x%04x", __func__, events); 2056 2057 len = recvmsg(fd, &msg, 0); 2058 if (len == -1) { 2059 logerr(__func__); 2060 return; 2061 } 2062 2063 iov.iov_len = (size_t)len; 2064 ipv6nd_recvmsg(ctx, &msg); 2065 } 2066 2067 static void 2068 ipv6nd_startrs2(void *arg) 2069 { 2070 struct interface *ifp = arg; 2071 struct rs_state *state; 2072 2073 loginfox("%s: soliciting an IPv6 router", ifp->name); 2074 state = RS_STATE(ifp); 2075 if (state == NULL) { 2076 ifp->if_data[IF_DATA_IPV6ND] = calloc(1, sizeof(*state)); 2077 state = RS_STATE(ifp); 2078 if (state == NULL) { 2079 logerr(__func__); 2080 return; 2081 } 2082 #ifdef __sun 2083 state->nd_fd = -1; 2084 #ifdef PRIVSEP 2085 if (IN_PRIVSEP(ifp->ctx) && ps_inet_opennd(ifp) == -1) { 2086 logerr("%s: ps_inet_opennd", ifp->name); 2087 return; 2088 } 2089 #endif 2090 #endif 2091 } 2092 2093 /* Always make a new probe as the underlying hardware 2094 * address could have changed. */ 2095 ipv6nd_makersprobe(ifp); 2096 if (state->rs == NULL) { 2097 logerr(__func__); 2098 return; 2099 } 2100 2101 state->retrans = RETRANS_TIMER; 2102 state->rsprobes = 0; 2103 ipv6nd_sendrsprobe(ifp); 2104 } 2105 2106 static void 2107 ipv6nd_startrs1(void *arg) 2108 { 2109 struct interface *ifp = arg; 2110 unsigned int delay; 2111 2112 if (!(ifp->options->options & DHCPCD_INITIAL_DELAY)) { 2113 ipv6nd_startrs2(ifp); 2114 return; 2115 } 2116 2117 delay = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MSEC_PER_SEC); 2118 logdebugx("%s: delaying IPv6 router solicitation for %0.1f seconds", 2119 ifp->name, (float)delay / MSEC_PER_SEC); 2120 eloop_timeout_add_msec(ifp->ctx->eloop, delay, ipv6nd_startrs2, ifp); 2121 } 2122 2123 void 2124 ipv6nd_startrs(struct interface *ifp) 2125 { 2126 if (ipv6_linklocal(ifp) == NULL) { 2127 logdebugx("%s: " 2128 "delaying IPv6 Router Solicitation for LL address", 2129 ifp->name); 2130 ipv6_addlinklocalcallback(ifp, ipv6nd_startrs1, ifp); 2131 } else 2132 ipv6nd_startrs1(ifp); 2133 } 2134 2135 void 2136 ipv6nd_abort(struct interface *ifp) 2137 { 2138 eloop_timeout_delete(ifp->ctx->eloop, ipv6nd_startrs1, ifp); 2139 eloop_timeout_delete(ifp->ctx->eloop, ipv6nd_startrs2, ifp); 2140 eloop_timeout_delete(ifp->ctx->eloop, ipv6nd_sendrsprobe, ifp); 2141 } 2142 2143 static struct routeinfo * 2144 routeinfo_findalloc(struct ra *rap, const struct in6_addr *prefix, 2145 uint8_t prefix_len) 2146 { 2147 struct routeinfo *ri; 2148 char buf[INET6_ADDRSTRLEN]; 2149 const char *p; 2150 2151 TAILQ_FOREACH(ri, &rap->rinfos, next) { 2152 if (ri->prefix_len == prefix_len && 2153 IN6_ARE_ADDR_EQUAL(&ri->prefix, prefix)) 2154 return ri; 2155 } 2156 2157 ri = malloc(sizeof(struct routeinfo)); 2158 if (ri == NULL) 2159 return NULL; 2160 2161 memcpy(&ri->prefix, prefix, sizeof(ri->prefix)); 2162 ri->prefix_len = prefix_len; 2163 p = inet_ntop(AF_INET6, prefix, buf, sizeof(buf)); 2164 if (p) 2165 snprintf(ri->sprefix, sizeof(ri->sprefix), "%s/%d", p, 2166 prefix_len); 2167 else 2168 ri->sprefix[0] = '\0'; 2169 TAILQ_INSERT_TAIL(&rap->rinfos, ri, next); 2170 return ri; 2171 } 2172 2173 static void 2174 routeinfohead_free(struct routeinfohead *head) 2175 { 2176 struct routeinfo *ri; 2177 2178 while ((ri = TAILQ_FIRST(head))) { 2179 TAILQ_REMOVE(head, ri, next); 2180 free(ri); 2181 } 2182 } 2183