1 /* 2 * dhcpcd - DHCP client daemon 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/types.h> 32 #include <sys/utsname.h> 33 34 #include <netinet/in.h> 35 #include <netinet/ip6.h> 36 37 #include <assert.h> 38 #include <ctype.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <inttypes.h> 42 #include <stdbool.h> 43 #include <stddef.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <syslog.h> 47 #include <unistd.h> 48 49 #define ELOOP_QUEUE ELOOP_DHCP6 50 #include "config.h" // IWYU pragma: keep 51 #include "common.h" 52 #include "dhcp.h" 53 #include "dhcp6.h" 54 #include "duid.h" 55 #include "eloop.h" 56 #include "if-options.h" 57 #include "if.h" 58 #include "ipv6nd.h" 59 #include "logerr.h" 60 #include "privsep.h" 61 #include "script.h" 62 63 #ifdef HAVE_SYS_BITOPS_H 64 #include <sys/bitops.h> 65 #else 66 #include "compat/bitops.h" 67 #endif 68 69 /* DHCPCD Project has been assigned an IANA PEN of 40712 */ 70 #define DHCPCD_IANA_PEN 40712 71 72 /* Unsure if I want this */ 73 // #define VENDOR_SPLIT 74 75 /* Support older systems with different defines */ 76 #if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO) 77 #define IPV6_RECVPKTINFO IPV6_PKTINFO 78 #endif 79 80 #ifdef DHCP6 81 82 /* Assert the correct structure size for on wire */ 83 struct dhcp6_message { 84 uint8_t type; 85 uint8_t xid[3]; 86 /* followed by options */ 87 }; 88 __CTASSERT(sizeof(struct dhcp6_message) == 4); 89 90 struct dhcp6_option { 91 uint16_t code; 92 uint16_t len; 93 /* followed by data */ 94 }; 95 __CTASSERT(sizeof(struct dhcp6_option) == 4); 96 97 struct dhcp6_ia_na { 98 uint8_t iaid[4]; 99 uint32_t t1; 100 uint32_t t2; 101 }; 102 __CTASSERT(sizeof(struct dhcp6_ia_na) == 12); 103 104 struct dhcp6_ia_ta { 105 uint8_t iaid[4]; 106 }; 107 __CTASSERT(sizeof(struct dhcp6_ia_ta) == 4); 108 109 struct dhcp6_ia_addr { 110 struct in6_addr addr; 111 uint32_t pltime; 112 uint32_t vltime; 113 }; 114 __CTASSERT(sizeof(struct dhcp6_ia_addr) == 16 + 8); 115 116 /* Some compilers do not support packed structures. 117 * We manually decode this. */ 118 #if 0 119 struct dhcp6_pd_addr { 120 uint32_t pltime; 121 uint32_t vltime; 122 uint8_t prefix_len; 123 struct in6_addr prefix; 124 } __packed; 125 __CTASSERT(sizeof(struct dhcp6_pd_addr) == 8 + 1 + 16); 126 #endif 127 128 #define DHCP6_PD_ADDR_SIZE (8 + 1 + 16) 129 #define DHCP6_PD_ADDR_PLTIME 0 130 #define DHCP6_PD_ADDR_VLTIME 4 131 #define DHCP6_PD_ADDR_PLEN 8 132 #define DHCP6_PD_ADDR_PREFIX 9 133 134 struct dhcp6_op { 135 uint16_t type; 136 const char *name; 137 }; 138 139 static const struct dhcp6_op dhcp6_ops[] = { { DHCP6_SOLICIT, "SOLICIT6" }, 140 { DHCP6_ADVERTISE, "ADVERTISE6" }, { DHCP6_REQUEST, "REQUEST6" }, 141 { DHCP6_REPLY, "REPLY6" }, { DHCP6_RENEW, "RENEW6" }, 142 { DHCP6_REBIND, "REBIND6" }, { DHCP6_CONFIRM, "CONFIRM6" }, 143 { DHCP6_INFORMATION_REQ, "INFORM6" }, { DHCP6_RELEASE, "RELEASE6" }, 144 { DHCP6_RECONFIGURE, "RECONFIGURE6" }, { DHCP6_DECLINE, "DECLINE6" }, 145 { 0, NULL } }; 146 147 struct dhcp_compat { 148 uint8_t dhcp_opt; 149 uint16_t dhcp6_opt; 150 }; 151 152 /* 153 * RFC 5908 deprecates OPTION_SNTP_SERVERS. 154 * But we can support both as the hook scripts will uniqify the 155 * results if the server returns both options. 156 */ 157 #ifdef INET 158 static const struct dhcp_compat dhcp_compats[] = { { DHO_DNSSERVER, 159 D6_OPTION_DNS_SERVERS }, 160 { DHO_HOSTNAME, D6_OPTION_FQDN }, { DHO_DNSDOMAIN, D6_OPTION_FQDN }, 161 { DHO_NISSERVER, D6_OPTION_NIS_SERVERS }, 162 { DHO_NTPSERVER, D6_OPTION_SNTP_SERVERS }, 163 { DHO_NTPSERVER, D6_OPTION_NTP_SERVER }, 164 { DHO_RAPIDCOMMIT, D6_OPTION_RAPID_COMMIT }, 165 { DHO_FQDN, D6_OPTION_FQDN }, { DHO_VIVCO, D6_OPTION_VENDOR_CLASS }, 166 { DHO_VIVSO, D6_OPTION_VENDOR_OPTS }, 167 { DHO_DNSSEARCH, D6_OPTION_DOMAIN_LIST }, { 0, 0 } }; 168 #endif 169 170 static const char *const dhcp6_statuses[] = { "Success", "Unspecified Failure", 171 "No Addresses Available", "No Binding", "Not On Link", "Use Multicast", 172 "No Prefix Available" }; 173 174 static void dhcp6_bind(struct interface *, const char *, const char *); 175 static void dhcp6_failinform(void *); 176 static void dhcp6_startrebind(void *arg); 177 static void dhcp6_recvaddr(void *, unsigned short); 178 static void dhcp6_startdecline(struct interface *); 179 static void dhcp6_startrequest(struct interface *); 180 181 #ifdef SMALL 182 #define dhcp6_hasprefixdelegation(a) (0) 183 #else 184 static int dhcp6_hasprefixdelegation(struct interface *); 185 #endif 186 187 #define DECLINE_IA(ia) \ 188 ((ia)->addr_flags & IN6_IFF_DUPLICATED && (ia)->ia_type != 0 && \ 189 (ia)->ia_type != D6_OPTION_IA_PD && \ 190 !((ia)->flags & IPV6_AF_STALE) && (ia)->prefix_vltime != 0) 191 192 /* Gets a pointer to the length part of the option to fill it 193 * in later. */ 194 #define NEXTLEN(p) ((p) + offsetof(struct dhcp6_option, len)) 195 196 void 197 dhcp6_printoptions(const struct dhcpcd_ctx *ctx, const struct dhcp_opt *opts, 198 size_t opts_len) 199 { 200 size_t i, j; 201 const struct dhcp_opt *opt, *opt2; 202 int cols; 203 204 for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; 205 i++, opt++) { 206 for (j = 0, opt2 = opts; j < opts_len; j++, opt2++) 207 if (opt2->option == opt->option) 208 break; 209 if (j == opts_len) { 210 cols = printf("%05d %s", opt->option, opt->var); 211 dhcp_print_option_encoding(opt, cols); 212 } 213 } 214 for (i = 0, opt = opts; i < opts_len; i++, opt++) { 215 cols = printf("%05d %s", opt->option, opt->var); 216 dhcp_print_option_encoding(opt, cols); 217 } 218 } 219 220 static size_t 221 dhcp6_makeuser(void *data, const struct interface *ifp) 222 { 223 const struct if_options *ifo = ifp->options; 224 struct dhcp6_option o; 225 uint8_t *p; 226 const uint8_t *up, *ue; 227 uint16_t ulen, unlen; 228 size_t olen; 229 230 /* Convert the DHCPv4 user class option to DHCPv6 */ 231 up = ifo->userclass; 232 ulen = *up++; 233 if (ulen == 0) 234 return 0; 235 236 p = data; 237 olen = 0; 238 if (p != NULL) 239 p += sizeof(o); 240 241 ue = up + ulen; 242 for (; up < ue; up += ulen) { 243 ulen = *up++; 244 olen += sizeof(ulen) + ulen; 245 if (data == NULL) 246 continue; 247 unlen = htons(ulen); 248 memcpy(p, &unlen, sizeof(unlen)); 249 p += sizeof(unlen); 250 memcpy(p, up, ulen); 251 p += ulen; 252 } 253 if (data != NULL) { 254 o.code = htons(D6_OPTION_USER_CLASS); 255 o.len = htons((uint16_t)olen); 256 memcpy(data, &o, sizeof(o)); 257 } 258 259 return sizeof(o) + olen; 260 } 261 262 #ifndef SMALL 263 /* DHCPv6 Option 16 (Vendor Class Option) */ 264 static size_t 265 dhcp6_makevendor(void *data, const struct interface *ifp) 266 { 267 const struct if_options *ifo; 268 size_t len = 0, optlen, vlen, i; 269 uint8_t *p; 270 const struct vivco *vivco; 271 struct dhcp6_option o; 272 273 ifo = ifp->options; 274 if (ifo->vivco_len > 0) { 275 for (i = 0, vivco = ifo->vivco; i < ifo->vivco_len; 276 i++, vivco++) 277 len += sizeof(o) + sizeof(uint32_t) + sizeof(uint16_t) + 278 vivco->len; 279 } else if (ifo->vendorclassid[0] != '\0') { 280 /* dhcpcd owns DHCPCD_IANA_PEN. 281 * If you need your own string, get your own IANA PEN. */ 282 vlen = strlen(ifp->ctx->vendor); 283 len += sizeof(o) + sizeof(uint32_t) + sizeof(uint16_t) + vlen; 284 } else 285 return 0; 286 287 if (len > UINT16_MAX) { 288 logerrx("%s: DHCPv6 Vendor Class too big", ifp->name); 289 return 0; 290 } 291 292 if (data != NULL) { 293 uint32_t pen; 294 uint16_t hvlen; 295 296 p = data; 297 298 if (ifo->vivco_len > 0) { 299 for (i = 0, vivco = ifo->vivco; i < ifo->vivco_len; 300 i++, vivco++) { 301 optlen = sizeof(uint32_t) + sizeof(uint16_t) + 302 vivco->len; 303 o.code = htons(D6_OPTION_VENDOR_CLASS); 304 o.len = htons((uint16_t)optlen); 305 memcpy(p, &o, sizeof(o)); 306 p += sizeof(o); 307 pen = htonl(vivco->en); 308 memcpy(p, &pen, sizeof(pen)); 309 p += sizeof(pen); 310 hvlen = htons((uint16_t)vivco->len); 311 memcpy(p, &hvlen, sizeof(hvlen)); 312 p += sizeof(hvlen); 313 memcpy(p, vivco->data, vivco->len); 314 p += vivco->len; 315 } 316 } else if (ifo->vendorclassid[0] != '\0') { 317 optlen = sizeof(uint32_t) + sizeof(uint16_t) + vlen; 318 o.code = htons(D6_OPTION_VENDOR_CLASS); 319 o.len = htons((uint16_t)optlen); 320 memcpy(p, &o, sizeof(o)); 321 p += sizeof(o); 322 pen = htonl(DHCPCD_IANA_PEN); 323 memcpy(p, &pen, sizeof(pen)); 324 p += sizeof(pen); 325 hvlen = htons((uint16_t)vlen); 326 memcpy(p, &hvlen, sizeof(hvlen)); 327 p += sizeof(hvlen); 328 memcpy(p, ifp->ctx->vendor, vlen); 329 } 330 } 331 return len; 332 } 333 334 /* DHCPv6 Option 17 (Vendor-Specific Information Option) */ 335 static size_t 336 dhcp6_makevendoropts(void *data, const struct interface *ifp) 337 { 338 uint8_t *p = data, *olenp; 339 const struct if_options *ifo = ifp->options; 340 size_t len = 0, olen; 341 const struct vsio *vsio, *vsio_endp = ifo->vsio6 + ifo->vsio6_len; 342 const struct vsio_so *so, *so_endp; 343 struct dhcp6_option o; 344 uint32_t en; 345 uint16_t opt, slen; 346 347 for (vsio = ifo->vsio6; vsio != vsio_endp; ++vsio) { 348 if (vsio->so_len == 0) 349 continue; 350 351 if (p != NULL) { 352 olenp = NEXTLEN(p); 353 o.code = htons(D6_OPTION_VENDOR_OPTS); 354 o.len = 0; 355 memcpy(p, &o, sizeof(o)); 356 p += sizeof(o); 357 358 en = htonl(vsio->en); 359 memcpy(p, &en, sizeof(en)); 360 p += sizeof(en); 361 } else 362 olenp = NULL; 363 364 olen = sizeof(en); 365 366 so_endp = vsio->so + vsio->so_len; 367 for (so = vsio->so; so != so_endp; so++) { 368 if (olen + sizeof(opt) + sizeof(slen) + so->len > 369 UINT16_MAX) { 370 logerrx("%s: option too big", __func__); 371 break; 372 } 373 374 if (p != NULL) { 375 opt = htons(so->opt); 376 memcpy(p, &opt, sizeof(opt)); 377 p += sizeof(opt); 378 slen = htons(so->len); 379 memcpy(p, &slen, sizeof(slen)); 380 p += sizeof(slen); 381 memcpy(p, so->data, so->len); 382 p += so->len; 383 } 384 385 olen += sizeof(opt) + sizeof(slen) + so->len; 386 } 387 388 if (olenp != NULL) { 389 slen = htons((uint16_t)olen); 390 memcpy(olenp, &slen, sizeof(slen)); 391 } 392 393 len += sizeof(o) + olen; 394 } 395 396 return len; 397 } 398 #endif 399 400 static void * 401 dhcp6_findoption(void *data, size_t data_len, uint16_t code, uint16_t *len) 402 { 403 uint8_t *d; 404 struct dhcp6_option o; 405 406 code = htons(code); 407 for (d = data; data_len != 0; d += o.len, data_len -= o.len) { 408 if (data_len < sizeof(o)) { 409 errno = EINVAL; 410 return NULL; 411 } 412 memcpy(&o, d, sizeof(o)); 413 d += sizeof(o); 414 data_len -= sizeof(o); 415 o.len = htons(o.len); 416 if (data_len < o.len) { 417 errno = EINVAL; 418 return NULL; 419 } 420 if (o.code == code) { 421 if (len != NULL) 422 *len = o.len; 423 return d; 424 } 425 } 426 427 errno = ENOENT; 428 return NULL; 429 } 430 431 static void * 432 dhcp6_findmoption(void *data, size_t data_len, uint16_t code, uint16_t *len) 433 { 434 uint8_t *d; 435 436 if (data_len < sizeof(struct dhcp6_message)) { 437 errno = EINVAL; 438 return false; 439 } 440 d = data; 441 d += sizeof(struct dhcp6_message); 442 data_len -= sizeof(struct dhcp6_message); 443 return dhcp6_findoption(d, data_len, code, len); 444 } 445 446 static const uint8_t * 447 dhcp6_getoption(struct dhcpcd_ctx *ctx, size_t *os, unsigned int *code, 448 size_t *len, const uint8_t *od, size_t ol, struct dhcp_opt **oopt) 449 { 450 struct dhcp6_option o; 451 size_t i; 452 struct dhcp_opt *opt; 453 454 if (od != NULL) { 455 *os = sizeof(o); 456 if (ol < *os) { 457 errno = EINVAL; 458 return NULL; 459 } 460 memcpy(&o, od, sizeof(o)); 461 *len = ntohs(o.len); 462 if (*len > ol - *os) { 463 errno = ERANGE; 464 return NULL; 465 } 466 *code = ntohs(o.code); 467 } 468 469 *oopt = NULL; 470 for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; 471 i++, opt++) { 472 if (opt->option == *code) { 473 *oopt = opt; 474 break; 475 } 476 } 477 478 if (od != NULL) 479 return od + sizeof(o); 480 return NULL; 481 } 482 483 static bool 484 dhcp6_updateelapsed(struct interface *ifp, struct dhcp6_message *m, size_t len) 485 { 486 uint8_t *opt; 487 uint16_t opt_len; 488 struct dhcp6_state *state; 489 struct timespec tv; 490 unsigned long long hsec; 491 uint16_t sec; 492 493 opt = dhcp6_findmoption(m, len, D6_OPTION_ELAPSED, &opt_len); 494 if (opt == NULL) 495 return false; 496 if (opt_len != sizeof(sec)) { 497 errno = EINVAL; 498 return false; 499 } 500 501 state = D6_STATE(ifp); 502 clock_gettime(CLOCK_MONOTONIC, &tv); 503 if (state->RTC == 0) { 504 /* An RTC of zero means we're the first message 505 * out of the door, so the elapsed time is zero. */ 506 state->started = tv; 507 hsec = 0; 508 } else { 509 unsigned long long secs; 510 unsigned int nsecs; 511 512 secs = eloop_timespec_diff(&tv, &state->started, &nsecs); 513 /* Elapsed time is measured in centiseconds. 514 * We need to be sure it will not potentially overflow. */ 515 if (secs >= (UINT16_MAX / CSEC_PER_SEC) + 1) 516 hsec = UINT16_MAX; 517 else { 518 hsec = (secs * CSEC_PER_SEC) + (nsecs / NSEC_PER_CSEC); 519 if (hsec > UINT16_MAX) 520 hsec = UINT16_MAX; 521 } 522 } 523 sec = htons((uint16_t)hsec); 524 memcpy(opt, &sec, sizeof(sec)); 525 return true; 526 } 527 528 static void 529 dhcp6_newxid(const struct interface *ifp, struct dhcp6_message *m) 530 { 531 const struct interface *ifp1; 532 const struct dhcp6_state *state1; 533 uint32_t xid; 534 535 if (ifp->options->options & DHCPCD_XID_HWADDR && 536 ifp->hwlen >= sizeof(xid)) 537 /* The lower bits are probably more unique on the network */ 538 memcpy(&xid, (ifp->hwaddr + ifp->hwlen) - sizeof(xid), 539 sizeof(xid)); 540 else { 541 again: 542 xid = arc4random(); 543 } 544 545 m->xid[0] = (xid >> 16) & 0xff; 546 m->xid[1] = (xid >> 8) & 0xff; 547 m->xid[2] = xid & 0xff; 548 549 /* Ensure it's unique */ 550 TAILQ_FOREACH(ifp1, ifp->ctx->ifaces, next) { 551 if (ifp == ifp1) 552 continue; 553 if ((state1 = D6_CSTATE(ifp1)) == NULL) 554 continue; 555 if (state1->send != NULL && state1->send->xid[0] == m->xid[0] && 556 state1->send->xid[1] == m->xid[1] && 557 state1->send->xid[2] == m->xid[2]) 558 break; 559 } 560 561 if (ifp1 != NULL) { 562 if (ifp->options->options & DHCPCD_XID_HWADDR && 563 ifp->hwlen >= sizeof(xid)) { 564 logerrx("%s: duplicate xid on %s", ifp->name, 565 ifp1->name); 566 return; 567 } 568 goto again; 569 } 570 } 571 572 #ifndef SMALL 573 static const struct if_sla * 574 dhcp6_findselfsla(struct interface *ifp) 575 { 576 size_t i, j; 577 struct if_ia *ia; 578 579 for (i = 0; i < ifp->options->ia_len; i++) { 580 ia = &ifp->options->ia[i]; 581 if (ia->ia_type != D6_OPTION_IA_PD) 582 continue; 583 for (j = 0; j < ia->sla_len; j++) { 584 if (strcmp(ia->sla[j].ifname, ifp->name) == 0) 585 return &ia->sla[j]; 586 } 587 } 588 return NULL; 589 } 590 591 static int 592 dhcp6_delegateaddr(struct in6_addr *addr, struct interface *ifp, 593 const struct ipv6_addr *prefix, const struct if_sla *sla, struct if_ia *ia) 594 { 595 struct dhcp6_state *state; 596 struct if_sla asla; 597 char sabuf[INET6_ADDRSTRLEN]; 598 const char *sa; 599 600 state = D6_STATE(ifp); 601 if (state == NULL) { 602 ifp->if_data[IF_DATA_DHCP6] = calloc(1, sizeof(*state)); 603 state = D6_STATE(ifp); 604 if (state == NULL) { 605 logerr(__func__); 606 return -1; 607 } 608 609 TAILQ_INIT(&state->addrs); 610 state->state = DH6S_DELEGATED; 611 state->reason = "DELEGATED6"; 612 } 613 614 if (sla == NULL || !sla->sla_set) { 615 /* No SLA set, so make an assumption of 616 * desired SLA and prefix length. */ 617 asla.sla = ifp->index; 618 asla.prefix_len = 0; 619 asla.sla_set = false; 620 sla = &asla; 621 } else if (sla->prefix_len == 0) { 622 /* An SLA was given, but prefix length was not. 623 * We need to work out a suitable prefix length for 624 * potentially more than one interface. */ 625 asla.sla = sla->sla; 626 asla.prefix_len = 0; 627 asla.sla_set = sla->sla_set; 628 sla = &asla; 629 } 630 631 if (sla->prefix_len == 0) { 632 uint32_t sla_max; 633 int bits; 634 635 sla_max = ia->sla_max; 636 if (sla_max == 0 && (sla == NULL || !sla->sla_set)) { 637 const struct interface *ifi; 638 639 TAILQ_FOREACH(ifi, ifp->ctx->ifaces, next) { 640 if (ifi->index > sla_max) 641 sla_max = ifi->index; 642 } 643 } 644 645 bits = fls32(sla_max); 646 647 if (prefix->prefix_len + bits > (int)UINT8_MAX) 648 asla.prefix_len = UINT8_MAX; 649 else { 650 asla.prefix_len = (uint8_t)(prefix->prefix_len + bits); 651 652 /* Make a 64 prefix by default, as this makes SLAAC 653 * possible. 654 * Otherwise round up to the nearest 4 bits. */ 655 if (asla.prefix_len <= 64) 656 asla.prefix_len = 64; 657 else 658 asla.prefix_len = (uint8_t)ROUNDUP4( 659 asla.prefix_len); 660 } 661 662 #define BIT(n) (1UL << (n)) 663 #define BIT_MASK(len) (BIT(len) - 1) 664 if (ia->sla_max == 0) { 665 /* Work out the real sla_max from our bits used */ 666 bits = asla.prefix_len - prefix->prefix_len; 667 /* Make static analysis happy. 668 * Bits cannot be bigger than 32 thanks to fls32. */ 669 assert(bits <= 32); 670 ia->sla_max = (uint32_t)BIT_MASK(bits); 671 } 672 } 673 674 if (ipv6_userprefix(&prefix->prefix, prefix->prefix_len, sla->sla, addr, 675 sla->prefix_len) == -1) { 676 sa = inet_ntop(AF_INET6, &prefix->prefix, sabuf, sizeof(sabuf)); 677 logerr("%s: invalid prefix %s/%d + %d/%d", ifp->name, sa, 678 prefix->prefix_len, sla->sla, sla->prefix_len); 679 return -1; 680 } 681 682 if (prefix->prefix_exclude_len && 683 IN6_ARE_ADDR_EQUAL(addr, &prefix->prefix_exclude)) { 684 sa = inet_ntop(AF_INET6, &prefix->prefix_exclude, sabuf, 685 sizeof(sabuf)); 686 logerrx("%s: cannot delegate excluded prefix %s/%d", ifp->name, 687 sa, prefix->prefix_exclude_len); 688 return -1; 689 } 690 691 return sla->prefix_len; 692 } 693 #endif 694 695 static int 696 dhcp6_makemessage(struct interface *ifp) 697 { 698 struct dhcpcd_ctx *ctx = ifp->ctx; 699 struct dhcp6_state *state; 700 struct dhcp6_message *m; 701 struct dhcp6_option o; 702 uint8_t *p, *si, *unicast, IA; 703 size_t n, l, len, ml, hl; 704 uint8_t type; 705 uint16_t si_len, uni_len, n_options; 706 uint8_t *o_lenp; 707 struct if_options *ifo = ifp->options; 708 const struct dho_policy_group *pg = &ifo->dhopg_dhcp6; 709 const struct dhcp_opt *opt, *opt2; 710 const struct ipv6_addr *ap; 711 char hbuf[HOSTNAME_MAX_LEN + 1]; 712 const char *hostname; 713 int fqdn; 714 struct dhcp6_ia_na ia_na; 715 uint16_t ia_na_len; 716 struct if_ia *ifia; 717 #ifdef AUTH 718 uint16_t auth_len; 719 #endif 720 uint8_t duid[DUID_LEN]; 721 size_t duid_len = 0; 722 723 state = D6_STATE(ifp); 724 if (state->send) { 725 free(state->send); 726 state->send = NULL; 727 } 728 729 switch (state->state) { 730 case DH6S_INIT: /* FALLTHROUGH */ 731 case DH6S_DISCOVER: 732 type = DHCP6_SOLICIT; 733 break; 734 case DH6S_REQUEST: 735 type = DHCP6_REQUEST; 736 break; 737 case DH6S_CONFIRM: 738 type = DHCP6_CONFIRM; 739 break; 740 case DH6S_REBIND: 741 type = DHCP6_REBIND; 742 break; 743 case DH6S_RENEW: 744 type = DHCP6_RENEW; 745 break; 746 case DH6S_INFORM: 747 type = DHCP6_INFORMATION_REQ; 748 break; 749 case DH6S_RELEASE: 750 type = DHCP6_RELEASE; 751 break; 752 case DH6S_DECLINE: 753 type = DHCP6_DECLINE; 754 break; 755 default: 756 errno = EINVAL; 757 return -1; 758 } 759 760 /* RFC 4704 Section 5 says we can only send FQDN for these 761 * message types. */ 762 switch (type) { 763 case DHCP6_SOLICIT: 764 case DHCP6_REQUEST: 765 case DHCP6_RENEW: 766 case DHCP6_REBIND: 767 fqdn = ifo->fqdn; 768 break; 769 default: 770 fqdn = FQDN_DISABLE; 771 break; 772 } 773 774 if (fqdn == FQDN_DISABLE && ifo->options & DHCPCD_HOSTNAME) { 775 /* We're sending the DHCPv4 hostname option, so send FQDN as 776 * DHCPv6 has no FQDN option and DHCPv4 must not send 777 * hostname and FQDN according to RFC4702 */ 778 fqdn = FQDN_BOTH; 779 } 780 if (fqdn != FQDN_DISABLE) 781 hostname = dhcp_get_hostname(ctx, hbuf, sizeof(hbuf), ifo); 782 else 783 hostname = NULL; /* appearse gcc */ 784 785 /* Work out option size first */ 786 n_options = 0; 787 len = 0; 788 si = NULL; 789 hl = 0; /* Appease gcc */ 790 if (state->state != DH6S_RELEASE && state->state != DH6S_DECLINE) { 791 for (l = 0, opt = ctx->dhcp6_opts; l < ctx->dhcp6_opts_len; 792 l++, opt++) { 793 for (n = 0, opt2 = ifo->dhcp6_override; 794 n < ifo->dhcp6_override_len; n++, opt2++) { 795 if (opt->option == opt2->option) 796 break; 797 } 798 if (n < ifo->dhcp6_override_len) 799 continue; 800 if (!dho_policy_opt_requested(pg, opt)) 801 continue; 802 n_options++; 803 len += sizeof(o.len); 804 } 805 #ifndef SMALL 806 for (l = 0, opt = ifo->dhcp6_override; 807 l < ifo->dhcp6_override_len; l++, opt++) { 808 if (!dho_policy_opt_requested(pg, opt)) 809 continue; 810 n_options++; 811 len += sizeof(o.len); 812 } 813 if (dhcp6_findselfsla(ifp)) { 814 n_options++; 815 len += sizeof(o.len); 816 } 817 #endif 818 if (len) 819 len += sizeof(o); 820 821 if (fqdn != FQDN_DISABLE) { 822 hl = encode_rfc1035(hostname, NULL); 823 len += sizeof(o) + 1 + hl; 824 } 825 826 if (dho_policy_allowed(pg, D6_OPTION_MUDURL) && ifo->mudurl[0]) 827 len += sizeof(o) + ifo->mudurl[0]; 828 829 #ifdef AUTH 830 if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) != 831 DHCPCD_AUTH_SENDREQUIRE && 832 dho_policy_allowed(pg, D6_OPTION_RECONF_ACCEPT)) 833 len += sizeof(o); /* Reconfigure Accept */ 834 #endif 835 } 836 837 len += sizeof(*state->send); 838 len += sizeof(o) + sizeof(uint16_t); /* elapsed */ 839 840 if (ifo->options & DHCPCD_ANONYMOUS) { 841 duid_len = duid_make(duid, ifp, DUID_LL); 842 len += sizeof(o) + duid_len; 843 } else { 844 len += sizeof(o) + ctx->duid_len; 845 } 846 847 if (dho_policy_allowed(pg, D6_OPTION_USER_CLASS)) 848 len += dhcp6_makeuser(NULL, ifp); 849 850 #ifndef SMALL 851 if (dho_policy_allowed(pg, D6_OPTION_VENDOR_CLASS)) 852 len += dhcp6_makevendor(NULL, ifp); 853 if (dho_policy_allowed(pg, D6_OPTION_VENDOR_OPTS)) 854 len += dhcp6_makevendoropts(NULL, ifp); 855 #endif 856 857 /* IA */ 858 m = NULL; 859 ml = 0; 860 switch (state->state) { 861 case DH6S_REQUEST: 862 m = state->recv; 863 ml = state->recv_len; 864 /* FALLTHROUGH */ 865 case DH6S_DECLINE: 866 /* FALLTHROUGH */ 867 case DH6S_RELEASE: 868 /* FALLTHROUGH */ 869 case DH6S_RENEW: 870 if (m == NULL) { 871 m = state->new; 872 ml = state->new_len; 873 } 874 si = dhcp6_findmoption(m, ml, D6_OPTION_SERVERID, &si_len); 875 if (si == NULL) 876 return -1; 877 len += sizeof(o) + si_len; 878 /* FALLTHROUGH */ 879 case DH6S_REBIND: 880 /* FALLTHROUGH */ 881 case DH6S_CONFIRM: 882 /* FALLTHROUGH */ 883 case DH6S_DISCOVER: 884 if (m == NULL) { 885 m = state->new; 886 ml = state->new_len; 887 } 888 TAILQ_FOREACH(ap, &state->addrs, next) { 889 if (ap->flags & IPV6_AF_STALE) 890 continue; 891 if (!(ap->flags & IPV6_AF_REQUEST) && 892 (ap->prefix_vltime == 0 || 893 state->state == DH6S_DISCOVER)) 894 continue; 895 if (DECLINE_IA(ap) && state->state != DH6S_DECLINE) 896 continue; 897 if (ap->ia_type == D6_OPTION_IA_PD) { 898 #ifndef SMALL 899 len += sizeof(o) + DHCP6_PD_ADDR_SIZE; 900 if (ap->prefix_exclude_len) 901 len += sizeof(o) + 1 + 902 (uint8_t)((ap->prefix_exclude_len - 903 ap->prefix_len - 1) / 904 NBBY) + 905 1; 906 #endif 907 } else 908 len += sizeof(o) + sizeof(struct dhcp6_ia_addr); 909 } 910 /* FALLTHROUGH */ 911 case DH6S_INIT: 912 for (l = 0; l < ifo->ia_len; l++) { 913 len += sizeof(o) + sizeof(uint32_t); /* IAID */ 914 /* IA_TA does not have T1 or T2 timers */ 915 if (ifo->ia[l].ia_type != D6_OPTION_IA_TA) 916 len += sizeof(uint32_t) + sizeof(uint32_t); 917 } 918 IA = 1; 919 break; 920 default: 921 IA = 0; 922 } 923 924 if (state->state == DH6S_DISCOVER && !(ctx->options & DHCPCD_TEST) && 925 dho_policy_requested(pg, D6_OPTION_RAPID_COMMIT)) 926 len += sizeof(o); 927 928 if (m == NULL) { 929 m = state->new; 930 ml = state->new_len; 931 } 932 933 switch (state->state) { 934 case DH6S_REQUEST: /* FALLTHROUGH */ 935 case DH6S_RENEW: /* FALLTHROUGH */ 936 case DH6S_RELEASE: 937 if (!dho_policy_allowed(pg, D6_OPTION_UNICAST)) { 938 unicast = NULL; 939 break; 940 } 941 unicast = dhcp6_findmoption(m, ml, D6_OPTION_UNICAST, &uni_len); 942 break; 943 default: 944 unicast = NULL; 945 break; 946 } 947 948 /* In non manager mode we listen and send from fixed addresses. 949 * We should try and match an address we have to unicast to, 950 * but for now this is the safest policy. */ 951 if (unicast != NULL && !(ctx->options & DHCPCD_MANAGER)) { 952 logdebugx("%s: ignoring unicast option as not manager", 953 ifp->name); 954 unicast = NULL; 955 } 956 957 #ifdef AUTH 958 auth_len = 0; 959 if (ifo->auth.options & DHCPCD_AUTH_SEND) { 960 ssize_t alen = dhcp_auth_encode(ctx, &ifo->auth, 961 state->auth.token, NULL, 0, 6, type, NULL, 0); 962 if (alen != -1 && alen > UINT16_MAX) { 963 errno = ERANGE; 964 alen = -1; 965 } 966 if (alen == -1) 967 logerr("%s: %s: dhcp_auth_encode", __func__, ifp->name); 968 else if (alen != 0) { 969 auth_len = (uint16_t)alen; 970 len += sizeof(o) + auth_len; 971 } 972 } 973 #endif 974 975 state->send = malloc(len); 976 if (state->send == NULL) 977 return -1; 978 979 state->send_len = len; 980 state->send->type = type; 981 982 /* If we found a unicast option, copy it to our state for sending */ 983 if (unicast && uni_len == sizeof(state->unicast)) 984 memcpy(&state->unicast, unicast, sizeof(state->unicast)); 985 else 986 state->unicast = in6addr_any; 987 988 dhcp6_newxid(ifp, state->send); 989 990 #define COPYIN1(_code, _len) \ 991 { \ 992 o.code = htons((_code)); \ 993 o.len = htons((_len)); \ 994 memcpy(p, &o, sizeof(o)); \ 995 p += sizeof(o); \ 996 } 997 #define COPYIN(_code, _data, _len) \ 998 do { \ 999 COPYIN1((_code), (_len)); \ 1000 if ((_len) != 0) { \ 1001 memcpy(p, (_data), (_len)); \ 1002 p += (_len); \ 1003 } \ 1004 } while (0 /* CONSTCOND */) 1005 1006 /* Options are listed in numerical order as per RFC 7844 Section 4.1 1007 * XXX: They should be randomised. */ 1008 1009 p = (uint8_t *)state->send + sizeof(*state->send); 1010 if (ifo->options & DHCPCD_ANONYMOUS) 1011 COPYIN(D6_OPTION_CLIENTID, duid, (uint16_t)duid_len); 1012 else 1013 COPYIN(D6_OPTION_CLIENTID, ctx->duid, (uint16_t)ctx->duid_len); 1014 1015 if (si != NULL) 1016 COPYIN(D6_OPTION_SERVERID, si, si_len); 1017 1018 for (l = 0; IA && l < ifo->ia_len; l++) { 1019 ifia = &ifo->ia[l]; 1020 o_lenp = NEXTLEN(p); 1021 /* TA structure is the same as the others, 1022 * it just lacks the T1 and T2 timers. 1023 * These happen to be at the end of the struct, 1024 * so we just don't copy them in. */ 1025 if (ifia->ia_type == D6_OPTION_IA_TA) 1026 ia_na_len = sizeof(struct dhcp6_ia_ta); 1027 else 1028 ia_na_len = sizeof(ia_na); 1029 memcpy(ia_na.iaid, ifia->iaid, sizeof(ia_na.iaid)); 1030 /* RFC 8415 21.4 and 21.21 state that T1 and T2 should be zero. 1031 * An RFC compliant server MUST ignore them anyway. */ 1032 ia_na.t1 = 0; 1033 ia_na.t2 = 0; 1034 COPYIN(ifia->ia_type, &ia_na, ia_na_len); 1035 TAILQ_FOREACH(ap, &state->addrs, next) { 1036 if (ap->flags & IPV6_AF_STALE) 1037 continue; 1038 if (!(ap->flags & IPV6_AF_REQUEST) && 1039 (ap->prefix_vltime == 0 || 1040 state->state == DH6S_DISCOVER)) 1041 continue; 1042 if (DECLINE_IA(ap) && state->state != DH6S_DECLINE) 1043 continue; 1044 if (ap->ia_type != ifia->ia_type) 1045 continue; 1046 if (memcmp(ap->iaid, ifia->iaid, sizeof(ap->iaid))) 1047 continue; 1048 if (ap->ia_type == D6_OPTION_IA_PD) { 1049 #ifndef SMALL 1050 uint8_t pdp[DHCP6_PD_ADDR_SIZE]; 1051 1052 memset(pdp, 0, DHCP6_PD_ADDR_PLEN); 1053 pdp[DHCP6_PD_ADDR_PLEN] = (uint8_t) 1054 ap->prefix_len; 1055 memcpy(pdp + DHCP6_PD_ADDR_PREFIX, &ap->prefix, 1056 DHCP6_PD_ADDR_SIZE - DHCP6_PD_ADDR_PREFIX); 1057 COPYIN(D6_OPTION_IAPREFIX, pdp, sizeof(pdp)); 1058 1059 ia_na_len = (uint16_t)(ia_na_len + sizeof(o) + 1060 sizeof(pdp)); 1061 1062 /* RFC6603 Section 4.2 */ 1063 if (ap->prefix_exclude_len) { 1064 uint8_t exb[17], *ep, u8; 1065 const uint8_t *pp; 1066 1067 n = (size_t)((ap->prefix_exclude_len - 1068 ap->prefix_len - 1) / 1069 NBBY) + 1070 1; 1071 ep = exb; 1072 *ep++ = (uint8_t)ap->prefix_exclude_len; 1073 pp = ap->prefix_exclude.s6_addr; 1074 pp += (size_t)((ap->prefix_len - 1) / 1075 NBBY) + 1076 (n - 1); 1077 u8 = ap->prefix_len % NBBY; 1078 if (u8) 1079 n--; 1080 while (n-- > 0) 1081 *ep++ = *pp--; 1082 n = (size_t)(ep - exb); 1083 if (u8) { 1084 *ep = (uint8_t)(*pp << u8); 1085 n++; 1086 } 1087 COPYIN(D6_OPTION_PD_EXCLUDE, exb, 1088 (uint16_t)n); 1089 ia_na_len = (uint16_t)(ia_na_len + 1090 sizeof(o) + n); 1091 } 1092 #endif 1093 } else { 1094 struct dhcp6_ia_addr ia = { 1095 .addr = ap->addr, 1096 /* 1097 * RFC 8415 21.6 states that the 1098 * valid and preferred lifetimes sent by 1099 * the client SHOULD be zero and MUST 1100 * be ignored by the server. 1101 */ 1102 }; 1103 1104 COPYIN(D6_OPTION_IA_ADDR, &ia, sizeof(ia)); 1105 ia_na_len = (uint16_t)(ia_na_len + sizeof(o) + 1106 sizeof(ia)); 1107 } 1108 } 1109 1110 /* Update the total option lenth. */ 1111 ia_na_len = htons(ia_na_len); 1112 memcpy(o_lenp, &ia_na_len, sizeof(ia_na_len)); 1113 } 1114 1115 if (state->send->type != DHCP6_RELEASE && 1116 state->send->type != DHCP6_DECLINE && n_options) { 1117 o_lenp = NEXTLEN(p); 1118 o.len = 0; 1119 COPYIN1(D6_OPTION_ORO, 0); 1120 for (l = 0, opt = ctx->dhcp6_opts; l < ctx->dhcp6_opts_len; 1121 l++, opt++) { 1122 #ifndef SMALL 1123 for (n = 0, opt2 = ifo->dhcp6_override; 1124 n < ifo->dhcp6_override_len; n++, opt2++) { 1125 if (opt->option == opt2->option) 1126 break; 1127 } 1128 if (n < ifo->dhcp6_override_len) 1129 continue; 1130 #endif 1131 if (!dho_policy_opt_requested(pg, opt)) 1132 continue; 1133 o.code = htons((uint16_t)opt->option); 1134 memcpy(p, &o.code, sizeof(o.code)); 1135 p += sizeof(o.code); 1136 o.len = (uint16_t)(o.len + sizeof(o.code)); 1137 } 1138 #ifndef SMALL 1139 for (l = 0, opt = ifo->dhcp6_override; 1140 l < ifo->dhcp6_override_len; l++, opt++) { 1141 if (!dho_policy_opt_requested(pg, opt)) 1142 continue; 1143 o.code = htons((uint16_t)opt->option); 1144 memcpy(p, &o.code, sizeof(o.code)); 1145 p += sizeof(o.code); 1146 o.len = (uint16_t)(o.len + sizeof(o.code)); 1147 } 1148 if (dhcp6_findselfsla(ifp)) { 1149 o.code = htons(D6_OPTION_PD_EXCLUDE); 1150 memcpy(p, &o.code, sizeof(o.code)); 1151 p += sizeof(o.code); 1152 o.len = (uint16_t)(o.len + sizeof(o.code)); 1153 } 1154 #endif 1155 o.len = htons(o.len); 1156 memcpy(o_lenp, &o.len, sizeof(o.len)); 1157 } 1158 1159 si_len = 0; 1160 COPYIN(D6_OPTION_ELAPSED, &si_len, sizeof(si_len)); 1161 1162 if (state->state == DH6S_DISCOVER && !(ctx->options & DHCPCD_TEST) && 1163 dho_policy_requested(pg, D6_OPTION_RAPID_COMMIT)) 1164 COPYIN1(D6_OPTION_RAPID_COMMIT, 0); 1165 1166 if (dho_policy_allowed(pg, D6_OPTION_USER_CLASS)) 1167 p += dhcp6_makeuser(p, ifp); 1168 1169 #ifndef SMALL 1170 if (dho_policy_allowed(pg, D6_OPTION_VENDOR_CLASS)) 1171 p += dhcp6_makevendor(p, ifp); 1172 if (dho_policy_allowed(pg, D6_OPTION_VENDOR_OPTS)) 1173 p += dhcp6_makevendoropts(p, ifp); 1174 #endif 1175 1176 if (state->send->type != DHCP6_RELEASE && 1177 state->send->type != DHCP6_DECLINE) { 1178 if (fqdn != FQDN_DISABLE) { 1179 o_lenp = NEXTLEN(p); 1180 COPYIN1(D6_OPTION_FQDN, 0); 1181 if (hl == 0) 1182 *p = D6_FQDN_NONE; 1183 else { 1184 switch (fqdn) { 1185 case FQDN_BOTH: 1186 *p = D6_FQDN_BOTH; 1187 break; 1188 case FQDN_PTR: 1189 *p = D6_FQDN_PTR; 1190 break; 1191 default: 1192 *p = D6_FQDN_NONE; 1193 break; 1194 } 1195 } 1196 p++; 1197 encode_rfc1035(hostname, p); 1198 p += hl; 1199 o.len = htons((uint16_t)(hl + 1)); 1200 memcpy(o_lenp, &o.len, sizeof(o.len)); 1201 } 1202 1203 if (dho_policy_allowed(pg, D6_OPTION_MUDURL) && ifo->mudurl[0]) 1204 COPYIN(D6_OPTION_MUDURL, ifo->mudurl + 1, 1205 ifo->mudurl[0]); 1206 1207 #ifdef AUTH 1208 if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) != 1209 DHCPCD_AUTH_SENDREQUIRE && 1210 dho_policy_allowed(pg, D6_OPTION_RECONF_ACCEPT)) 1211 COPYIN1(D6_OPTION_RECONF_ACCEPT, 0); 1212 #endif 1213 } 1214 1215 #ifdef AUTH 1216 /* This has to be the last option */ 1217 if (ifo->auth.options & DHCPCD_AUTH_SEND && auth_len != 0) { 1218 COPYIN1(D6_OPTION_AUTH, auth_len); 1219 /* data will be filled at send message time */ 1220 } 1221 #endif 1222 1223 return 0; 1224 } 1225 1226 static const char * 1227 dhcp6_get_op(uint16_t type) 1228 { 1229 const struct dhcp6_op *d; 1230 1231 for (d = dhcp6_ops; d->name; d++) 1232 if (d->type == type) 1233 return d->name; 1234 return NULL; 1235 } 1236 1237 static void 1238 dhcp6_freedrop_addrs(struct interface *ifp, int drop, unsigned int notflags, 1239 const struct interface *ifd) 1240 { 1241 struct dhcp6_state *state; 1242 1243 state = D6_STATE(ifp); 1244 if (state) { 1245 ipv6_freedrop_addrs(&state->addrs, drop, notflags, ifd); 1246 if (drop) 1247 rt_build(ifp->ctx, AF_INET6); 1248 } 1249 } 1250 1251 #ifndef SMALL 1252 static void 1253 dhcp6_delete_delegates(struct interface *ifp) 1254 { 1255 struct interface *ifp0; 1256 1257 if (ifp->ctx->ifaces) { 1258 TAILQ_FOREACH(ifp0, ifp->ctx->ifaces, next) { 1259 if (ifp0 != ifp) 1260 dhcp6_freedrop_addrs(ifp0, 1, 0, ifp); 1261 } 1262 } 1263 } 1264 #endif 1265 1266 #ifdef AUTH 1267 static ssize_t 1268 dhcp6_update_auth(struct interface *ifp, struct dhcp6_message *m, size_t len) 1269 { 1270 struct dhcp6_state *state; 1271 uint8_t *opt; 1272 uint16_t opt_len; 1273 1274 opt = dhcp6_findmoption(m, len, D6_OPTION_AUTH, &opt_len); 1275 if (opt == NULL) 1276 return -1; 1277 1278 state = D6_STATE(ifp); 1279 return dhcp_auth_encode(ifp->ctx, &ifp->options->auth, 1280 state->auth.token, (uint8_t *)state->send, state->send_len, 6, 1281 state->send->type, opt, opt_len); 1282 } 1283 #endif 1284 1285 static const struct in6_addr alldhcp = IN6ADDR_LINKLOCAL_ALLDHCP_INIT; 1286 static int 1287 dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) 1288 { 1289 struct dhcp6_state *state = D6_STATE(ifp); 1290 struct dhcpcd_ctx *ctx = ifp->ctx; 1291 unsigned int RT; 1292 bool multicast = true; 1293 struct sockaddr_in6 dst = { 1294 .sin6_family = AF_INET6, 1295 /* Setting the port on Linux gives EINVAL when sending. 1296 * This looks like a kernel bug as the equivalent works 1297 * fine with the DHCP counterpart. */ 1298 #ifndef __linux__ 1299 .sin6_port = htons(DHCP6_SERVER_PORT), 1300 #endif 1301 }; 1302 struct udphdr udp = { 1303 .uh_sport = htons(DHCP6_CLIENT_PORT), 1304 .uh_dport = htons(DHCP6_SERVER_PORT), 1305 .uh_ulen = htons((uint16_t)(sizeof(udp) + state->send_len)), 1306 }; 1307 struct iovec iov[] = { 1308 { 1309 .iov_base = &udp, 1310 .iov_len = sizeof(udp), 1311 }, 1312 { 1313 .iov_base = state->send, 1314 .iov_len = state->send_len, 1315 }, 1316 }; 1317 union { 1318 struct cmsghdr hdr; 1319 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; 1320 } cmsgbuf = { .buf = { 0 } }; 1321 struct msghdr msg = { 1322 .msg_name = &dst, 1323 .msg_namelen = sizeof(dst), 1324 .msg_iov = iov, 1325 .msg_iovlen = __arraycount(iov), 1326 }; 1327 char uaddr[INET6_ADDRSTRLEN]; 1328 1329 if (!callback && !if_is_link_up(ifp)) 1330 return 0; 1331 1332 if (!IN6_IS_ADDR_UNSPECIFIED(&state->unicast)) { 1333 switch (state->send->type) { 1334 case DHCP6_SOLICIT: /* FALLTHROUGH */ 1335 case DHCP6_CONFIRM: /* FALLTHROUGH */ 1336 case DHCP6_REBIND: 1337 /* Unicasting is denied for these types. */ 1338 break; 1339 default: 1340 multicast = false; 1341 inet_ntop(AF_INET6, &state->unicast, uaddr, 1342 sizeof(uaddr)); 1343 break; 1344 } 1345 } 1346 dst.sin6_addr = multicast ? alldhcp : state->unicast; 1347 1348 if (!callback) { 1349 logdebugx("%s: %s %s with xid 0x%02x%02x%02x%s%s", ifp->name, 1350 multicast ? "multicasting" : "unicasting", 1351 dhcp6_get_op(state->send->type), state->send->xid[0], 1352 state->send->xid[1], state->send->xid[2], 1353 !multicast ? " " : "", !multicast ? uaddr : ""); 1354 RT = 0; 1355 } else { 1356 if (state->IMD && 1357 !(ifp->options->options & DHCPCD_INITIAL_DELAY)) 1358 state->IMD = 0; 1359 if (state->IMD) { 1360 state->RT = state->IMD * MSEC_PER_SEC; 1361 /* Some buggy PPP servers close the link too early 1362 * after sending an invalid status in their reply 1363 * which means this host won't see it. 1364 * 1 second grace seems to be the sweet spot. */ 1365 if (ifp->flags & IFF_POINTOPOINT) 1366 state->RT += MSEC_PER_SEC; 1367 } else if (state->RTC == 0) 1368 state->RT = state->IRT * MSEC_PER_SEC; 1369 1370 if (state->MRT != 0) { 1371 unsigned int mrt = state->MRT * MSEC_PER_SEC; 1372 1373 if (state->RT > mrt) 1374 state->RT = mrt; 1375 } 1376 1377 /* Add -.1 to .1 * RT randomness as per RFC8415 section 15 */ 1378 uint32_t lru = arc4random_uniform(state->RTC == 0 ? 1379 DHCP6_RAND_MAX : 1380 DHCP6_RAND_MAX - DHCP6_RAND_MIN); 1381 int lr = (int)lru - (state->RTC == 0 ? 0 : DHCP6_RAND_MAX); 1382 RT = state->RT + 1383 (unsigned int)((float)state->RT * 1384 ((float)lr / DHCP6_RAND_DIV)); 1385 1386 if (if_is_link_up(ifp)) 1387 logdebugx("%s: %s %s (xid 0x%02x%02x%02x)%s%s," 1388 " next in %0.1f seconds", 1389 ifp->name, 1390 state->IMD != 0 ? "delaying" : 1391 multicast ? "multicasting" : 1392 "unicasting", 1393 dhcp6_get_op(state->send->type), 1394 state->send->xid[0], state->send->xid[1], 1395 state->send->xid[2], 1396 state->IMD == 0 && !multicast ? " " : "", 1397 state->IMD == 0 && !multicast ? uaddr : "", 1398 (float)RT / MSEC_PER_SEC); 1399 1400 /* Wait the initial delay */ 1401 if (state->IMD != 0) { 1402 state->IMD = 0; 1403 eloop_timeout_add_msec(ctx->eloop, RT, callback, ifp); 1404 return 0; 1405 } 1406 } 1407 1408 if (!if_is_link_up(ifp)) 1409 return 0; 1410 1411 /* Update the elapsed time */ 1412 dhcp6_updateelapsed(ifp, state->send, state->send_len); 1413 #ifdef AUTH 1414 if (ifp->options->auth.options & DHCPCD_AUTH_SEND && 1415 dhcp6_update_auth(ifp, state->send, state->send_len) == -1) { 1416 logerr("%s: %s: dhcp6_updateauth", __func__, ifp->name); 1417 if (errno != ESRCH) 1418 return -1; 1419 } 1420 #endif 1421 1422 /* Set the outbound interface */ 1423 if (multicast) { 1424 struct cmsghdr *cm; 1425 struct in6_pktinfo pi = { .ipi6_ifindex = ifp->index }; 1426 1427 dst.sin6_scope_id = ifp->index; 1428 msg.msg_control = cmsgbuf.buf; 1429 msg.msg_controllen = sizeof(cmsgbuf.buf); 1430 cm = CMSG_FIRSTHDR(&msg); 1431 if (cm == NULL) /* unlikely */ 1432 return -1; 1433 cm->cmsg_level = IPPROTO_IPV6; 1434 cm->cmsg_type = IPV6_PKTINFO; 1435 cm->cmsg_len = CMSG_LEN(sizeof(pi)); 1436 memcpy(CMSG_DATA(cm), &pi, sizeof(pi)); 1437 } 1438 1439 #ifdef PRIVSEP 1440 if (IN_PRIVSEP(ifp->ctx)) { 1441 if (ps_inet_senddhcp6(ifp, &msg) == -1) 1442 logerr(__func__); 1443 goto sent; 1444 } 1445 #endif 1446 1447 if (sendmsg(ctx->dhcp6_wfd, &msg, 0) == -1) { 1448 logerr("%s: %s: sendmsg", __func__, ifp->name); 1449 /* Allow DHCPv6 to continue .... the errors 1450 * would be rate limited by the protocol. 1451 * Generally the error is ENOBUFS when struggling to 1452 * associate with an access point. */ 1453 } 1454 1455 #ifdef PRIVSEP 1456 sent: 1457 #endif 1458 state->RTC++; 1459 if (callback) { 1460 state->RT = RT * 2; 1461 if (state->RT < RT) /* Check overflow */ 1462 state->RT = RT; 1463 if (state->MRC == 0 || state->RTC <= state->MRC) 1464 eloop_timeout_add_msec(ctx->eloop, RT, callback, ifp); 1465 else if (state->MRC != 0 && state->MRCcallback) 1466 eloop_timeout_add_msec(ctx->eloop, RT, 1467 state->MRCcallback, ifp); 1468 else 1469 logwarnx("%s: sent %d times with no reply", ifp->name, 1470 state->RTC); 1471 } 1472 return 0; 1473 } 1474 1475 static void 1476 dhcp6_sendinform(void *arg) 1477 { 1478 dhcp6_sendmessage(arg, dhcp6_sendinform); 1479 } 1480 1481 static void 1482 dhcp6_senddiscover2(void *arg) 1483 { 1484 dhcp6_sendmessage(arg, dhcp6_senddiscover2); 1485 } 1486 1487 static void 1488 dhcp6_senddiscover1(void *arg) 1489 { 1490 /* 1491 * So the initial RT has elapsed. 1492 * If we have any ADVERTs we can now REQUEST them. 1493 * RFC 8415 15 and 18.2.1 1494 */ 1495 struct interface *ifp = arg; 1496 struct dhcp6_state *state = D6_STATE(ifp); 1497 1498 if (state->recv == NULL || state->recv->type != DHCP6_ADVERTISE) 1499 dhcp6_sendmessage(arg, dhcp6_senddiscover2); 1500 else 1501 dhcp6_startrequest(ifp); 1502 } 1503 1504 static void 1505 dhcp6_senddiscover(void *arg) 1506 { 1507 struct interface *ifp = arg; 1508 struct dhcp6_state *state = D6_STATE(ifp); 1509 1510 dhcp6_sendmessage(arg, 1511 state->IMD != 0 ? dhcp6_senddiscover : dhcp6_senddiscover1); 1512 } 1513 1514 static void 1515 dhcp6_sendrequest(void *arg) 1516 { 1517 dhcp6_sendmessage(arg, dhcp6_sendrequest); 1518 } 1519 1520 static void 1521 dhcp6_sendrebind(void *arg) 1522 { 1523 dhcp6_sendmessage(arg, dhcp6_sendrebind); 1524 } 1525 1526 static void 1527 dhcp6_sendrenew(void *arg) 1528 { 1529 dhcp6_sendmessage(arg, dhcp6_sendrenew); 1530 } 1531 1532 static void 1533 dhcp6_sendconfirm(void *arg) 1534 { 1535 dhcp6_sendmessage(arg, dhcp6_sendconfirm); 1536 } 1537 1538 static void 1539 dhcp6_senddecline(void *arg) 1540 { 1541 dhcp6_sendmessage(arg, dhcp6_senddecline); 1542 } 1543 1544 static void 1545 dhcp6_sendrelease(void *arg) 1546 { 1547 dhcp6_sendmessage(arg, dhcp6_sendrelease); 1548 } 1549 1550 static void 1551 dhcp6_startrenew(void *arg) 1552 { 1553 struct interface *ifp; 1554 struct dhcp6_state *state; 1555 1556 ifp = arg; 1557 if ((state = D6_STATE(ifp)) == NULL) 1558 return; 1559 1560 /* Only renew in the bound or renew states */ 1561 if (state->state != DH6S_BOUND && state->state != DH6S_RENEW) 1562 return; 1563 1564 /* Remove the timeout as the renew may have been forced. */ 1565 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_startrenew, ifp); 1566 1567 state->state = DH6S_RENEW; 1568 state->RTC = 0; 1569 state->IMD = REN_MAX_DELAY; 1570 state->IRT = REN_TIMEOUT; 1571 state->MRT = REN_MAX_RT; 1572 state->MRC = 0; 1573 1574 if (dhcp6_makemessage(ifp) == -1) 1575 logerr("%s: %s", __func__, ifp->name); 1576 else 1577 dhcp6_sendrenew(ifp); 1578 } 1579 1580 void 1581 dhcp6_renew(struct interface *ifp) 1582 { 1583 dhcp6_startrenew(ifp); 1584 } 1585 1586 bool 1587 dhcp6_dadcompleted(const struct interface *ifp) 1588 { 1589 const struct dhcp6_state *state; 1590 const struct ipv6_addr *ap; 1591 1592 state = D6_CSTATE(ifp); 1593 TAILQ_FOREACH(ap, &state->addrs, next) { 1594 if (ap->flags & IPV6_AF_ADDED && 1595 !(ap->flags & IPV6_AF_DADCOMPLETED)) 1596 return false; 1597 } 1598 return true; 1599 } 1600 1601 static void 1602 dhcp6_dadcallback(void *arg) 1603 { 1604 struct ipv6_addr *ia = arg; 1605 struct interface *ifp; 1606 struct dhcp6_state *state; 1607 struct ipv6_addr *ia2; 1608 bool completed, valid, oneduplicated; 1609 1610 completed = (ia->flags & IPV6_AF_DADCOMPLETED); 1611 ia->flags |= IPV6_AF_DADCOMPLETED; 1612 if (ia->addr_flags & IN6_IFF_DUPLICATED) 1613 logwarnx("%s: DAD detected %s", ia->iface->name, ia->saddr); 1614 1615 if (completed) 1616 return; 1617 1618 ifp = ia->iface; 1619 state = D6_STATE(ifp); 1620 if (state->state != DH6S_BOUND && state->state != DH6S_DELEGATED) 1621 return; 1622 1623 #ifdef SMALL 1624 valid = true; 1625 #else 1626 valid = (ia->delegating_prefix == NULL); 1627 #endif 1628 completed = true; 1629 oneduplicated = false; 1630 TAILQ_FOREACH(ia2, &state->addrs, next) { 1631 if (ia2->flags & IPV6_AF_ADDED && 1632 !(ia2->flags & IPV6_AF_DADCOMPLETED)) { 1633 completed = false; 1634 break; 1635 } 1636 if (DECLINE_IA(ia)) 1637 oneduplicated = true; 1638 } 1639 if (!completed) 1640 return; 1641 1642 logdebugx("%s: DHCPv6 DAD completed", ifp->name); 1643 1644 if (oneduplicated && state->state == DH6S_BOUND) { 1645 dhcp6_startdecline(ifp); 1646 return; 1647 } 1648 1649 script_runreason(ifp, 1650 #ifndef SMALL 1651 ia->delegating_prefix ? "DELEGATED6" : 1652 #endif 1653 state->reason); 1654 if (valid) 1655 dhcpcd_daemonise(ifp->ctx); 1656 } 1657 1658 static void 1659 dhcp6_addrequestedaddrs(struct interface *ifp) 1660 { 1661 struct dhcp6_state *state; 1662 size_t i; 1663 struct if_ia *ia; 1664 struct ipv6_addr *a; 1665 1666 state = D6_STATE(ifp); 1667 /* Add any requested prefixes / addresses */ 1668 for (i = 0; i < ifp->options->ia_len; i++) { 1669 ia = &ifp->options->ia[i]; 1670 if (!((ia->ia_type == D6_OPTION_IA_PD && ia->prefix_len) || 1671 !IN6_IS_ADDR_UNSPECIFIED(&ia->addr))) 1672 continue; 1673 a = ipv6_newaddr(ifp, &ia->addr, 1674 /* 1675 * RFC 5942 Section 5 1676 * We cannot assume any prefix length, nor tie the 1677 * address to an existing one as it could expire 1678 * before the address. 1679 * As such we just give it a 128 prefix. 1680 */ 1681 ia->ia_type == D6_OPTION_IA_PD ? ia->prefix_len : 128, 1682 IPV6_AF_REQUEST); 1683 if (a == NULL) 1684 continue; 1685 a->dadcallback = dhcp6_dadcallback; 1686 memcpy(&a->iaid, &ia->iaid, sizeof(a->iaid)); 1687 a->ia_type = ia->ia_type; 1688 TAILQ_INSERT_TAIL(&state->addrs, a, next); 1689 } 1690 } 1691 1692 static void 1693 dhcp6_startdiscover(void *arg) 1694 { 1695 struct interface *ifp; 1696 struct if_options *ifo; 1697 struct dho_policy_group *pg; 1698 struct dhcp6_state *state; 1699 int llevel; 1700 struct ipv6_addr *ia; 1701 1702 ifp = arg; 1703 state = D6_STATE(ifp); 1704 ifo = ifp->options; 1705 pg = &ifo->dhopg_dhcp6; 1706 #ifndef SMALL 1707 if (state->reason == NULL || strcmp(state->reason, "TIMEOUT6") != 0) 1708 dhcp6_delete_delegates(ifp); 1709 #endif 1710 /* Ensure we never request INFO_REFRESH_TIME, 1711 * this only belongs in Information-Request messages */ 1712 dho_policy_del(&pg->dhop_request, D6_OPTION_INFO_REFRESH_TIME); 1713 1714 if (state->new == NULL && !state->failed) 1715 llevel = LOG_INFO; 1716 else 1717 llevel = LOG_DEBUG; 1718 logmessage(llevel, "%s: soliciting a DHCPv6 lease", ifp->name); 1719 state->state = DH6S_DISCOVER; 1720 state->RTC = 0; 1721 state->IMD = SOL_MAX_DELAY; 1722 state->IRT = SOL_TIMEOUT; 1723 state->MRT = state->sol_max_rt; 1724 state->MRC = SOL_MAX_RC; 1725 1726 /* If we fail to renew or confirm, our requested addreses will 1727 * be marked as stale. 1728 To re-request them, just mark them as not stale. */ 1729 TAILQ_FOREACH(ia, &state->addrs, next) { 1730 if (ia->flags & IPV6_AF_REQUEST) 1731 ia->flags &= ~IPV6_AF_STALE; 1732 } 1733 1734 if (dhcp6_makemessage(ifp) == -1) 1735 logerr("%s: %s", __func__, ifp->name); 1736 else 1737 dhcp6_senddiscover(ifp); 1738 } 1739 1740 static void 1741 dhcp6_startinform(void *arg) 1742 { 1743 struct interface *ifp; 1744 struct dhcp6_state *state; 1745 int llevel; 1746 struct if_options *ifo; 1747 struct dho_policy_group *pg; 1748 1749 ifp = arg; 1750 state = D6_STATE(ifp); 1751 ifo = ifp->options; 1752 llevel = state->failed ? LOG_DEBUG : LOG_INFO; 1753 logmessage(llevel, "%s: requesting DHCPv6 information", ifp->name); 1754 state->state = DH6S_INFORM; 1755 state->RTC = 0; 1756 state->IMD = INF_MAX_DELAY; 1757 state->IRT = INF_TIMEOUT; 1758 state->MRT = state->inf_max_rt; 1759 state->MRC = 0; 1760 1761 /* Ensure we always request INFO_REFRESH_TIME as per rfc8415 */ 1762 pg = &ifo->dhopg_dhcp6; 1763 dho_policy_add(&pg->dhop_request, D6_OPTION_INFO_REFRESH_TIME); 1764 1765 if (dhcp6_makemessage(ifp) == -1) { 1766 logerr("%s: %s", __func__, ifp->name); 1767 return; 1768 } 1769 dhcp6_sendinform(ifp); 1770 /* RFC3315 18.1.2 says that if CONFIRM failed then the prior addresses 1771 * SHOULD be used. The wording here is poor, because the addresses are 1772 * merely one facet of the lease as a whole. 1773 * This poor wording might explain the lack of similar text for INFORM 1774 * in 18.1.5 because there are no addresses in the INFORM message. */ 1775 if (!state->failed) 1776 eloop_timeout_add_sec(ifp->ctx->eloop, INF_MAX_RD, 1777 dhcp6_failinform, ifp); 1778 } 1779 1780 static bool 1781 dhcp6_startdiscoinform(struct interface *ifp) 1782 { 1783 unsigned long long opts = ifp->options->options; 1784 1785 if (opts & DHCPCD_IA_FORCED || ipv6nd_hasradhcp(ifp, true)) 1786 dhcp6_startdiscover(ifp); 1787 else if (opts & DHCPCD_INFORM6 || ipv6nd_hasradhcp(ifp, false)) 1788 dhcp6_startinform(ifp); 1789 else 1790 return false; 1791 return true; 1792 } 1793 1794 static void 1795 dhcp6_fail(struct interface *ifp, bool drop) 1796 { 1797 struct dhcp6_state *state = D6_STATE(ifp); 1798 1799 state->failed = true; 1800 1801 if (drop) { 1802 dhcp6_freedrop_addrs(ifp, 1, 1803 IPV6_AF_DELEGATED | IPV6_AF_PFXDELEGATION, NULL); 1804 #ifndef SMALL 1805 dhcp6_delete_delegates(ifp); 1806 #endif 1807 free(state->old); 1808 state->old = state->new; 1809 state->old_len = state->new_len; 1810 state->new = NULL; 1811 state->new_len = 0; 1812 if (state->old != NULL) 1813 script_runreason(ifp, "EXPIRE6"); 1814 dhcp_unlink(ifp->ctx, state->leasefile); 1815 dhcp6_addrequestedaddrs(ifp); 1816 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 1817 } else if ((state->state == DH6S_CONFIRM || 1818 state->state == DH6S_REBIND) && 1819 ifp->options->options & DHCPCD_LASTLEASE) { 1820 dhcp6_bind(ifp, NULL, NULL); 1821 state->state = DH6S_REBIND; 1822 dhcp6_startrebind(ifp); 1823 return; 1824 } else if (state->new) { 1825 script_runreason(ifp, "TIMEOUT6"); 1826 // We need to keep the expire timeout alive 1827 } 1828 1829 if (!dhcp6_startdiscoinform(ifp)) { 1830 logwarnx("%s: no advertising IPv6 router wants DHCP", 1831 ifp->name); 1832 state->state = DH6S_INIT; 1833 } 1834 } 1835 1836 static int 1837 dhcp6_failloglevel(struct interface *ifp) 1838 { 1839 const struct dhcp6_state *state = D6_CSTATE(ifp); 1840 1841 return state->failed ? LOG_DEBUG : LOG_ERR; 1842 } 1843 1844 static void 1845 dhcp6_failconfirm(void *arg) 1846 { 1847 struct interface *ifp = arg; 1848 int llevel = dhcp6_failloglevel(ifp); 1849 1850 logmessage(llevel, "%s: failed to confirm prior DHCPv6 address", 1851 ifp->name); 1852 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendconfirm, ifp); 1853 1854 /* RFC8415 18.2.3 says that prior addresses SHOULD be used on failure. 1855 */ 1856 dhcp6_fail(ifp, false); 1857 } 1858 1859 static void 1860 dhcp6_failrequest(void *arg) 1861 { 1862 struct interface *ifp = arg; 1863 int llevel = dhcp6_failloglevel(ifp); 1864 1865 logmessage(llevel, "%s: failed to request DHCPv6 address", ifp->name); 1866 dhcp6_fail(ifp, true); 1867 } 1868 1869 static void 1870 dhcp6_failinform(void *arg) 1871 { 1872 struct interface *ifp = arg; 1873 int llevel = dhcp6_failloglevel(ifp); 1874 1875 logmessage(llevel, "%s: failed to request DHCPv6 information", 1876 ifp->name); 1877 dhcp6_fail(ifp, true); 1878 } 1879 1880 #ifndef SMALL 1881 static void 1882 dhcp6_failrebindpd(void *arg) 1883 { 1884 struct interface *ifp = arg; 1885 1886 logerrx("%s: failed to rebind prior DHCPv6 delegation", ifp->name); 1887 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendrebind, ifp); 1888 1889 /* RFC8415 18.2.3 says that prior addresses SHOULD be used on failure. 1890 * 18.2 says REBIND rather than CONFIRM with PD but use CONFIRM timings. 1891 */ 1892 dhcp6_fail(ifp, false); 1893 } 1894 1895 static int 1896 dhcp6_hasprefixdelegation(struct interface *ifp) 1897 { 1898 size_t i; 1899 uint16_t t; 1900 1901 t = 0; 1902 for (i = 0; i < ifp->options->ia_len; i++) { 1903 if (t && t != ifp->options->ia[i].ia_type) { 1904 if (t == D6_OPTION_IA_PD || 1905 ifp->options->ia[i].ia_type == D6_OPTION_IA_PD) 1906 return 2; 1907 } 1908 t = ifp->options->ia[i].ia_type; 1909 } 1910 return t == D6_OPTION_IA_PD ? 1 : 0; 1911 } 1912 #endif 1913 1914 static void 1915 dhcp6_startrebind(void *arg) 1916 { 1917 struct interface *ifp; 1918 struct dhcp6_state *state; 1919 1920 ifp = arg; 1921 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendrenew, ifp); 1922 state = D6_STATE(ifp); 1923 1924 state->IMD = REB_MAX_DELAY; 1925 state->IRT = REB_TIMEOUT; 1926 state->MRT = REB_MAX_RT; 1927 state->RTC = 0; 1928 state->MRC = 0; 1929 1930 if (state->state == DH6S_RENEW) 1931 logwarnx("%s: failed to renew DHCPv6, rebinding", ifp->name); 1932 else { 1933 loginfox("%s: rebinding prior DHCPv6 lease", ifp->name); 1934 1935 #ifndef SMALL 1936 /* RFC 8415 18.2.5 */ 1937 if (dhcp6_hasprefixdelegation(ifp)) { 1938 state->IMD = CNF_MAX_DELAY; 1939 state->IRT = CNF_TIMEOUT; 1940 state->MRT = CNF_MAX_RT; 1941 eloop_timeout_add_sec(ifp->ctx->eloop, CNF_MAX_RD, 1942 dhcp6_failrebindpd, ifp); 1943 } 1944 #endif 1945 } 1946 1947 state->state = DH6S_REBIND; 1948 if (dhcp6_makemessage(ifp) == -1) 1949 logerr("%s: %s", __func__, ifp->name); 1950 else 1951 dhcp6_sendrebind(ifp); 1952 } 1953 1954 static void 1955 dhcp6_startrequest(struct interface *ifp) 1956 { 1957 struct dhcp6_state *state; 1958 1959 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_senddiscover, ifp); 1960 state = D6_STATE(ifp); 1961 state->state = DH6S_REQUEST; 1962 state->RTC = 0; 1963 state->IMD = 0; 1964 state->IRT = REQ_TIMEOUT; 1965 state->MRT = REQ_MAX_RT; 1966 state->MRC = REQ_MAX_RC; 1967 state->MRCcallback = dhcp6_failrequest; 1968 1969 if (dhcp6_makemessage(ifp) == -1) { 1970 logerr("%s: %s", __func__, ifp->name); 1971 return; 1972 } 1973 1974 dhcp6_sendrequest(ifp); 1975 } 1976 1977 static void 1978 dhcp6_startconfirm(struct interface *ifp) 1979 { 1980 struct dhcp6_state *state; 1981 struct ipv6_addr *ia; 1982 1983 state = D6_STATE(ifp); 1984 1985 TAILQ_FOREACH(ia, &state->addrs, next) { 1986 if (!DECLINE_IA(ia)) 1987 continue; 1988 logerrx("%s: prior DHCPv6 has a duplicated address", ifp->name); 1989 dhcp6_startdecline(ifp); 1990 return; 1991 } 1992 1993 state->state = DH6S_CONFIRM; 1994 state->RTC = 0; 1995 state->IMD = CNF_MAX_DELAY; 1996 state->IRT = CNF_TIMEOUT; 1997 state->MRT = CNF_MAX_RT; 1998 state->MRC = CNF_MAX_RC; 1999 2000 loginfox("%s: confirming prior DHCPv6 lease", ifp->name); 2001 2002 if (dhcp6_makemessage(ifp) == -1) { 2003 logerr("%s: %s", __func__, ifp->name); 2004 return; 2005 } 2006 dhcp6_sendconfirm(ifp); 2007 eloop_timeout_add_sec(ifp->ctx->eloop, CNF_MAX_RD, dhcp6_failconfirm, 2008 ifp); 2009 } 2010 2011 static void 2012 dhcp6_startexpire(void *arg) 2013 { 2014 struct interface *ifp; 2015 2016 ifp = arg; 2017 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendrebind, ifp); 2018 2019 logerrx("%s: DHCPv6 lease expired", ifp->name); 2020 dhcp6_fail(ifp, true); 2021 } 2022 2023 static void 2024 dhcp6_faildecline(void *arg) 2025 { 2026 struct interface *ifp = arg; 2027 2028 logerrx("%s: failed to decline duplicated DHCPv6 addresses", ifp->name); 2029 dhcp6_fail(ifp, true); 2030 } 2031 2032 static void 2033 dhcp6_startdecline(struct interface *ifp) 2034 { 2035 struct dhcp6_state *state; 2036 2037 state = D6_STATE(ifp); 2038 loginfox("%s: declining failed DHCPv6 addresses", ifp->name); 2039 state->state = DH6S_DECLINE; 2040 state->RTC = 0; 2041 state->IMD = 0; 2042 state->IRT = DEC_TIMEOUT; 2043 state->MRT = 0; 2044 state->MRC = DEC_MAX_RC; 2045 state->MRCcallback = dhcp6_faildecline; 2046 2047 if (dhcp6_makemessage(ifp) == -1) 2048 logerr("%s: %s", __func__, ifp->name); 2049 else 2050 dhcp6_senddecline(ifp); 2051 } 2052 2053 static void 2054 dhcp6_finishrelease(void *arg) 2055 { 2056 struct interface *ifp; 2057 struct dhcp6_state *state; 2058 2059 ifp = (struct interface *)arg; 2060 if ((state = D6_STATE(ifp)) != NULL) { 2061 state->state = DH6S_RELEASED; 2062 dhcp6_drop(ifp, "RELEASE6"); 2063 } 2064 } 2065 2066 static void 2067 dhcp6_startrelease(struct interface *ifp) 2068 { 2069 struct dhcp6_state *state; 2070 2071 state = D6_STATE(ifp); 2072 if (state->state != DH6S_BOUND) { 2073 dhcp6_finishrelease(ifp); 2074 return; 2075 } 2076 2077 state->state = DH6S_RELEASE; 2078 state->RTC = 0; 2079 state->IMD = REL_MAX_DELAY; 2080 state->IRT = REL_TIMEOUT; 2081 state->MRT = REL_MAX_RT; 2082 state->MRC = REL_MAX_RC; 2083 state->MRCcallback = dhcp6_finishrelease; 2084 2085 if (dhcp6_makemessage(ifp) == -1) { 2086 logerr("%s: %s", __func__, ifp->name); 2087 /* not much we can do apart from finish now */ 2088 dhcp6_finishrelease(ifp); 2089 } else 2090 dhcp6_sendrelease(ifp); 2091 } 2092 2093 static int 2094 dhcp6_checkstatusok(const struct interface *ifp, struct dhcp6_message *m, 2095 uint8_t *p, size_t len) 2096 { 2097 struct dhcp6_state *state; 2098 uint8_t *opt; 2099 uint16_t opt_len, code; 2100 size_t mlen; 2101 void *(*f)(void *, size_t, uint16_t, uint16_t *), *farg; 2102 char buf[32], *sbuf; 2103 const char *status; 2104 int loglevel; 2105 2106 state = D6_STATE(ifp); 2107 f = p ? dhcp6_findoption : dhcp6_findmoption; 2108 if (p) 2109 farg = p; 2110 else 2111 farg = m; 2112 if ((opt = f(farg, len, D6_OPTION_STATUS_CODE, &opt_len)) == NULL) { 2113 // logdebugx("%s: no status", ifp->name); 2114 state->lerror = 0; 2115 errno = ESRCH; 2116 return 0; 2117 } 2118 2119 if (opt_len < sizeof(code)) { 2120 logerrx("%s: status truncated", ifp->name); 2121 return -1; 2122 } 2123 memcpy(&code, opt, sizeof(code)); 2124 code = ntohs(code); 2125 if (code == D6_STATUS_OK) { 2126 state->lerror = 0; 2127 errno = 0; 2128 return 0; 2129 } 2130 2131 /* Anything after the code is a message. */ 2132 opt += sizeof(code); 2133 mlen = opt_len - sizeof(code); 2134 if (mlen == 0) { 2135 status_code: 2136 sbuf = NULL; 2137 if (code < sizeof(dhcp6_statuses) / sizeof(char *)) 2138 status = dhcp6_statuses[code]; 2139 else { 2140 snprintf(buf, sizeof(buf), "Unknown Status (%d)", code); 2141 status = buf; 2142 } 2143 } else { 2144 size_t slen; 2145 ssize_t plen = print_string(NULL, 0, OT_ESCSTRING, opt, mlen); 2146 2147 if (plen == -1) 2148 goto status_code; /* log something */ 2149 slen = (size_t)plen + 1; 2150 sbuf = malloc(slen); 2151 if (sbuf == NULL) 2152 goto status_code; /* log something */ 2153 2154 if (print_string(sbuf, slen, OT_ESCSTRING, opt, mlen) == -1) { 2155 free(sbuf); 2156 goto status_code; 2157 } 2158 status = sbuf; 2159 } 2160 2161 if (state->lerror == code || state->state == DH6S_INIT) 2162 loglevel = LOG_DEBUG; 2163 else 2164 loglevel = LOG_ERR; 2165 logmessage(loglevel, "%s: DHCPv6 REPLY: %s", ifp->name, status); 2166 free(sbuf); 2167 state->lerror = code; 2168 errno = 0; 2169 2170 /* RFC 8415 18.2.10 */ 2171 if (code == D6_STATUS_USEMULTICAST) { 2172 logdebugx("%s: server sent USEMULTICAST", ifp->name); 2173 state->unicast = in6addr_any; 2174 } 2175 2176 /* code cannot be D6_STATUS_OK, so there is a failure */ 2177 if (ifp->ctx->options & DHCPCD_TEST) 2178 eloop_exit(ifp->ctx->eloop, EXIT_FAILURE); 2179 2180 return (int)code; 2181 } 2182 2183 const struct ipv6_addr * 2184 dhcp6_iffindaddr(const struct interface *ifp, const struct in6_addr *addr, 2185 unsigned int flags) 2186 { 2187 const struct dhcp6_state *state; 2188 const struct ipv6_addr *ap; 2189 2190 if ((state = D6_STATE(ifp)) != NULL) { 2191 TAILQ_FOREACH(ap, &state->addrs, next) { 2192 if (ipv6_findaddrmatch(ap, addr, flags)) 2193 return ap; 2194 } 2195 } 2196 return NULL; 2197 } 2198 2199 struct ipv6_addr * 2200 dhcp6_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, 2201 unsigned int flags) 2202 { 2203 struct interface *ifp; 2204 struct ipv6_addr *ap; 2205 struct dhcp6_state *state; 2206 2207 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 2208 if ((state = D6_STATE(ifp)) != NULL) { 2209 TAILQ_FOREACH(ap, &state->addrs, next) { 2210 if (ipv6_findaddrmatch(ap, addr, flags)) 2211 return ap; 2212 } 2213 } 2214 } 2215 return NULL; 2216 } 2217 2218 static int 2219 dhcp6_findna(struct interface *ifp, uint16_t ot, const uint8_t *iaid, 2220 uint8_t *d, size_t l, const struct timespec *acquired) 2221 { 2222 struct dhcp6_state *state; 2223 uint8_t *o, *nd; 2224 uint16_t ol; 2225 struct ipv6_addr *a; 2226 int i; 2227 struct dhcp6_ia_addr ia; 2228 2229 i = 0; 2230 state = D6_STATE(ifp); 2231 while ((o = dhcp6_findoption(d, l, D6_OPTION_IA_ADDR, &ol))) { 2232 /* Set d and l first to ensure we find the next option. */ 2233 nd = o + ol; 2234 l -= (size_t)(nd - d); 2235 d = nd; 2236 if (ol < sizeof(ia)) { 2237 errno = EINVAL; 2238 logerrx("%s: IA Address option truncated", ifp->name); 2239 continue; 2240 } 2241 memcpy(&ia, o, sizeof(ia)); 2242 ia.pltime = ntohl(ia.pltime); 2243 ia.vltime = ntohl(ia.vltime); 2244 /* RFC 3315 22.6 */ 2245 if (ia.pltime > ia.vltime) { 2246 errno = EINVAL; 2247 logerr("%s: IA Address pltime %" PRIu32 2248 " > vltime %" PRIu32, 2249 ifp->name, ia.pltime, ia.vltime); 2250 continue; 2251 } 2252 TAILQ_FOREACH(a, &state->addrs, next) { 2253 if (ipv6_findaddrmatch(a, &ia.addr, 0)) 2254 break; 2255 } 2256 if (a == NULL) { 2257 /* 2258 * RFC 5942 Section 5 2259 * We cannot assume any prefix length, nor tie the 2260 * address to an existing one as it could expire 2261 * before the address. 2262 * As such we just give it a 128 prefix. 2263 */ 2264 a = ipv6_newaddr(ifp, &ia.addr, 128, IPV6_AF_ONLINK); 2265 a->dadcallback = dhcp6_dadcallback; 2266 a->ia_type = ot; 2267 memcpy(a->iaid, iaid, sizeof(a->iaid)); 2268 a->created = *acquired; 2269 2270 TAILQ_INSERT_TAIL(&state->addrs, a, next); 2271 } else { 2272 if (!(a->flags & IPV6_AF_ONLINK)) 2273 a->flags |= IPV6_AF_ONLINK | IPV6_AF_NEW; 2274 a->flags &= ~(IPV6_AF_STALE | IPV6_AF_EXTENDED); 2275 } 2276 a->acquired = *acquired; 2277 a->prefix_pltime = ia.pltime; 2278 if (a->prefix_vltime != ia.vltime) { 2279 a->flags |= IPV6_AF_NEW; 2280 a->prefix_vltime = ia.vltime; 2281 } 2282 if (a->prefix_pltime && a->prefix_pltime < state->lowpl) 2283 state->lowpl = a->prefix_pltime; 2284 if (a->prefix_vltime && a->prefix_vltime > state->expire) 2285 state->expire = a->prefix_vltime; 2286 i++; 2287 } 2288 return i; 2289 } 2290 2291 #ifndef SMALL 2292 static int 2293 dhcp6_findpd(struct interface *ifp, const uint8_t *iaid, uint8_t *d, size_t l, 2294 const struct timespec *acquired) 2295 { 2296 struct dhcp6_state *state; 2297 uint8_t *o, *nd; 2298 struct ipv6_addr *a; 2299 int i; 2300 uint8_t nb, *pw; 2301 uint16_t ol; 2302 uint32_t pdp_vltime, pdp_pltime; 2303 uint8_t pdp_plen; 2304 struct in6_addr pdp_prefix; 2305 2306 i = 0; 2307 state = D6_STATE(ifp); 2308 while ((o = dhcp6_findoption(d, l, D6_OPTION_IAPREFIX, &ol))) { 2309 /* Set d and l first to ensure we find the next option. */ 2310 nd = o + ol; 2311 l -= (size_t)(nd - d); 2312 d = nd; 2313 if (ol < DHCP6_PD_ADDR_SIZE) { 2314 errno = EINVAL; 2315 logerrx("%s: IA Prefix option truncated", ifp->name); 2316 continue; 2317 } 2318 2319 memcpy(&pdp_pltime, o, sizeof(pdp_pltime)); 2320 o += sizeof(pdp_pltime); 2321 memcpy(&pdp_vltime, o, sizeof(pdp_vltime)); 2322 o += sizeof(pdp_vltime); 2323 memcpy(&pdp_plen, o, sizeof(pdp_plen)); 2324 o += sizeof(pdp_plen); 2325 2326 if (pdp_plen > 128) { 2327 errno = EINVAL; 2328 logerrx("%s: IA Prefix length %u invalid", ifp->name, 2329 pdp_plen); 2330 continue; 2331 } 2332 2333 pdp_pltime = ntohl(pdp_pltime); 2334 pdp_vltime = ntohl(pdp_vltime); 2335 /* RFC 3315 22.6 */ 2336 if (pdp_pltime > pdp_vltime) { 2337 errno = EINVAL; 2338 logerrx("%s: IA Prefix pltime %" PRIu32 2339 " > vltime %" PRIu32, 2340 ifp->name, pdp_pltime, pdp_vltime); 2341 continue; 2342 } 2343 2344 memcpy(&pdp_prefix, o, sizeof(pdp_prefix)); 2345 o += sizeof(pdp_prefix); 2346 ol = (uint16_t)(ol - sizeof(pdp_pltime) - sizeof(pdp_vltime) - 2347 sizeof(pdp_plen) - sizeof(pdp_prefix)); 2348 2349 TAILQ_FOREACH(a, &state->addrs, next) { 2350 if (IN6_ARE_ADDR_EQUAL(&a->prefix, &pdp_prefix)) 2351 break; 2352 } 2353 2354 if (a == NULL) { 2355 a = ipv6_newaddr(ifp, &pdp_prefix, pdp_plen, 2356 IPV6_AF_PFXDELEGATION); 2357 if (a == NULL) 2358 break; 2359 a->created = *acquired; 2360 a->dadcallback = dhcp6_dadcallback; 2361 a->ia_type = D6_OPTION_IA_PD; 2362 memcpy(a->iaid, iaid, sizeof(a->iaid)); 2363 TAILQ_INSERT_TAIL(&state->addrs, a, next); 2364 } else { 2365 if (!(a->flags & IPV6_AF_PFXDELEGATION)) 2366 a->flags |= IPV6_AF_NEW | IPV6_AF_PFXDELEGATION; 2367 a->flags &= ~(IPV6_AF_STALE | IPV6_AF_EXTENDED); 2368 if (a->prefix_vltime != pdp_vltime) 2369 a->flags |= IPV6_AF_NEW; 2370 } 2371 2372 a->acquired = *acquired; 2373 a->prefix_pltime = pdp_pltime; 2374 a->prefix_vltime = pdp_vltime; 2375 2376 if (a->prefix_pltime && a->prefix_pltime < state->lowpl) 2377 state->lowpl = a->prefix_pltime; 2378 if (a->prefix_vltime && a->prefix_vltime > state->expire) 2379 state->expire = a->prefix_vltime; 2380 i++; 2381 2382 a->prefix_exclude_len = 0; 2383 memset(&a->prefix_exclude, 0, sizeof(a->prefix_exclude)); 2384 o = dhcp6_findoption(o, ol, D6_OPTION_PD_EXCLUDE, &ol); 2385 if (o == NULL) 2386 continue; 2387 2388 /* RFC 6603 4.2 says option length MUST be between 2 and 17. 2389 * This allows 1 octet for prefix length and 16 for the 2390 * subnet ID. */ 2391 if (ol < 2 || ol > 17) { 2392 logerrx("%s: invalid PD Exclude option", ifp->name); 2393 continue; 2394 } 2395 2396 /* RFC 6603 4.2 says prefix length MUST be between the 2397 * length of the IAPREFIX prefix length + 1 and 128. */ 2398 if (*o < a->prefix_len + 1 || *o > 128) { 2399 logerrx("%s: invalid PD Exclude length", ifp->name); 2400 continue; 2401 } 2402 2403 ol--; 2404 /* Check option length matches prefix length. */ 2405 if (((*o - a->prefix_len - 1) / NBBY) + 1 != ol) { 2406 logerrx("%s: PD Exclude length mismatch", ifp->name); 2407 continue; 2408 } 2409 a->prefix_exclude_len = *o++; 2410 2411 memcpy(&a->prefix_exclude, &a->prefix, 2412 sizeof(a->prefix_exclude)); 2413 nb = a->prefix_len % NBBY; 2414 if (nb) 2415 ol--; 2416 pw = a->prefix_exclude.s6_addr + 2417 (a->prefix_exclude_len / NBBY) - 1; 2418 while (ol-- > 0) 2419 *pw-- = *o++; 2420 if (nb) 2421 *pw = (uint8_t)(*pw | (*o >> nb)); 2422 } 2423 return i; 2424 } 2425 #endif 2426 2427 static int 2428 dhcp6_findia(struct interface *ifp, struct dhcp6_message *m, size_t l, 2429 const char *sfrom, const struct timespec *acquired) 2430 { 2431 struct dhcp6_state *state; 2432 const struct if_options *ifo; 2433 struct dhcp6_option o; 2434 uint8_t *d, *p; 2435 struct dhcp6_ia_na ia; 2436 int i, e, error; 2437 size_t j; 2438 uint16_t nl; 2439 uint8_t iaid[4]; 2440 char buf[sizeof(iaid) * 3]; 2441 struct ipv6_addr *ap; 2442 struct if_ia *ifia; 2443 2444 if (l < sizeof(*m)) { 2445 /* Should be impossible with guards at packet in 2446 * and reading leases */ 2447 errno = EINVAL; 2448 return -1; 2449 } 2450 2451 ifo = ifp->options; 2452 i = e = 0; 2453 state = D6_STATE(ifp); 2454 TAILQ_FOREACH(ap, &state->addrs, next) { 2455 /* Anything not from a lease for this interface should be 2456 * marked as stale. */ 2457 if (!(ap->flags & IPV6_AF_DELEGATED)) 2458 ap->flags |= IPV6_AF_STALE; 2459 } 2460 2461 d = (uint8_t *)m + sizeof(*m); 2462 l -= sizeof(*m); 2463 while (l > sizeof(o)) { 2464 memcpy(&o, d, sizeof(o)); 2465 o.len = ntohs(o.len); 2466 if (o.len > l || sizeof(o) + o.len > l) { 2467 errno = EINVAL; 2468 logerrx("%s: option overflow", ifp->name); 2469 break; 2470 } 2471 p = d + sizeof(o); 2472 d = p + o.len; 2473 l -= sizeof(o) + o.len; 2474 2475 o.code = ntohs(o.code); 2476 switch (o.code) { 2477 case D6_OPTION_IA_TA: 2478 nl = 4; 2479 break; 2480 case D6_OPTION_IA_NA: 2481 case D6_OPTION_IA_PD: 2482 nl = 12; 2483 break; 2484 default: 2485 continue; 2486 } 2487 if (o.len < nl) { 2488 errno = EINVAL; 2489 logerrx("%s: IA option truncated", ifp->name); 2490 continue; 2491 } 2492 2493 memcpy(&ia, p, nl); 2494 p += nl; 2495 o.len = (uint16_t)(o.len - nl); 2496 2497 for (j = 0; j < ifo->ia_len; j++) { 2498 ifia = &ifo->ia[j]; 2499 if (ifia->ia_type == o.code && 2500 memcmp(ifia->iaid, ia.iaid, sizeof(ia.iaid)) == 0) 2501 break; 2502 } 2503 if (j == ifo->ia_len && 2504 !(ifo->ia_len == 0 && 2505 ifp->ctx->options & DHCPCD_DUMPLEASE)) { 2506 logdebugx("%s: ignoring unrequested IAID %s", ifp->name, 2507 hwaddr_ntoa(ia.iaid, sizeof(ia.iaid), buf, 2508 sizeof(buf))); 2509 continue; 2510 } 2511 2512 if (o.code != D6_OPTION_IA_TA) { 2513 ia.t1 = ntohl(ia.t1); 2514 ia.t2 = ntohl(ia.t2); 2515 /* RFC 3315 22.4 */ 2516 if (ia.t2 > 0 && ia.t1 > ia.t2) { 2517 logwarnx("%s: IAID %s T1(%d) > T2(%d) from %s", 2518 ifp->name, 2519 hwaddr_ntoa(iaid, sizeof(iaid), buf, 2520 sizeof(buf)), 2521 ia.t1, ia.t2, sfrom); 2522 continue; 2523 } 2524 } else 2525 ia.t1 = ia.t2 = 0; /* appease gcc */ 2526 if ((error = dhcp6_checkstatusok(ifp, NULL, p, o.len)) != 0) { 2527 if (error == D6_STATUS_NOBINDING) 2528 state->has_no_binding = true; 2529 e = 1; 2530 continue; 2531 } 2532 if (o.code == D6_OPTION_IA_PD) { 2533 #ifndef SMALL 2534 if (dhcp6_findpd(ifp, ia.iaid, p, o.len, acquired) == 2535 0) { 2536 logwarnx("%s: %s: DHCPv6 REPLY missing Prefix", 2537 ifp->name, sfrom); 2538 continue; 2539 } 2540 #endif 2541 } else { 2542 if (dhcp6_findna(ifp, o.code, ia.iaid, p, o.len, 2543 acquired) == 0) { 2544 logwarnx("%s: %s: DHCPv6 REPLY missing " 2545 "IA Address", 2546 ifp->name, sfrom); 2547 continue; 2548 } 2549 } 2550 if (o.code != D6_OPTION_IA_TA) { 2551 if (ia.t1 != 0 && 2552 (ia.t1 < state->renew || state->renew == 0)) 2553 state->renew = ia.t1; 2554 if (ia.t2 != 0 && 2555 (ia.t2 < state->rebind || state->rebind == 0)) 2556 state->rebind = ia.t2; 2557 } 2558 i++; 2559 } 2560 2561 if (i == 0 && e) 2562 return -1; 2563 return i; 2564 } 2565 2566 #ifndef SMALL 2567 static bool 2568 dhcp6_deprecatedele(struct ipv6_addr *ia) 2569 { 2570 struct ipv6_addr *da, *dan, *dda; 2571 struct timespec now; 2572 struct dhcp6_state *state; 2573 bool freed = false; 2574 2575 timespecclear(&now); 2576 TAILQ_FOREACH_SAFE(da, &ia->pd_pfxs, pd_next, dan) { 2577 if (ia->prefix_vltime == 0) { 2578 if (da->prefix_vltime != 0) 2579 da->prefix_vltime = 0; 2580 else 2581 continue; 2582 } else if (da->prefix_pltime != 0) 2583 da->prefix_pltime = 0; 2584 else 2585 continue; 2586 2587 if (ipv6_doaddr(da, &now) != -1) 2588 continue; 2589 2590 /* Delegation deleted, forget it. */ 2591 TAILQ_REMOVE(&ia->pd_pfxs, da, pd_next); 2592 2593 /* Delete it from the interface. */ 2594 state = D6_STATE(da->iface); 2595 if (state == NULL) 2596 continue; /* unlikely */ 2597 2598 TAILQ_FOREACH(dda, &state->addrs, next) { 2599 if (IN6_ARE_ADDR_EQUAL(&dda->addr, &da->addr)) 2600 break; 2601 } 2602 if (dda != ia && dda != NULL) { 2603 TAILQ_REMOVE(&state->addrs, dda, next); 2604 ipv6_freeaddr(dda); 2605 freed = true; 2606 } 2607 } 2608 2609 return freed; 2610 } 2611 #endif 2612 2613 static void 2614 dhcp6_deprecateaddrs(struct ipv6_addrhead *addrs) 2615 { 2616 struct ipv6_addr *ia, *ian; 2617 #ifndef SMALL 2618 bool again; 2619 2620 again: 2621 #endif 2622 2623 TAILQ_FOREACH_SAFE(ia, addrs, next, ian) { 2624 if (ia->flags & IPV6_AF_EXTENDED) 2625 ; 2626 else if (ia->flags & IPV6_AF_STALE) { 2627 if (ia->prefix_vltime != 0) 2628 logdebugx("%s: %s: became stale", 2629 ia->iface->name, ia->saddr); 2630 /* Technically this violates RFC 8415 18.2.10.1, 2631 * but we need a mechanism to tell the kernel to 2632 * try and prefer other addresses. */ 2633 ia->prefix_pltime = 0; 2634 } else if (ia->prefix_vltime == 0) 2635 loginfox("%s: %s: no valid lifetime", ia->iface->name, 2636 ia->saddr); 2637 else 2638 continue; 2639 2640 #ifndef SMALL 2641 /* If we delegated from this prefix, deprecate or remove 2642 * the delegations. */ 2643 if (ia->flags & IPV6_AF_PFXDELEGATION) 2644 again = dhcp6_deprecatedele(ia); 2645 else 2646 again = false; 2647 #endif 2648 2649 if (ia->flags & IPV6_AF_REQUEST) { 2650 ia->prefix_vltime = ia->prefix_pltime = 0; 2651 eloop_q_timeout_delete(ia->iface->ctx->eloop, 2652 ELOOP_QUEUE_ALL, NULL, ia); 2653 continue; 2654 } 2655 TAILQ_REMOVE(addrs, ia, next); 2656 if (!(ia->flags & IPV6_AF_EXTENDED)) 2657 ipv6_deleteaddr(ia); 2658 ipv6_freeaddr(ia); 2659 #ifndef SMALL 2660 /* Deletion may invalidate the next pointer so restart */ 2661 if (again) 2662 goto again; 2663 #endif 2664 } 2665 } 2666 2667 static int 2668 dhcp6_validatelease(struct interface *ifp, struct dhcp6_message *m, size_t len, 2669 const char *sfrom, const struct timespec *acquired) 2670 { 2671 struct dhcp6_state *state; 2672 int nia, ok_errno; 2673 struct timespec aq; 2674 2675 if (len <= sizeof(*m)) { 2676 logerrx("%s: DHCPv6 lease truncated", ifp->name); 2677 return -1; 2678 } 2679 2680 state = D6_STATE(ifp); 2681 errno = 0; 2682 if (dhcp6_checkstatusok(ifp, m, NULL, len) != 0) 2683 return -1; 2684 ok_errno = errno; 2685 2686 state->renew = state->rebind = state->expire = 0; 2687 state->lowpl = ND6_INFINITE_LIFETIME; 2688 if (!acquired) { 2689 clock_gettime(CLOCK_MONOTONIC, &aq); 2690 acquired = &aq; 2691 } 2692 state->has_no_binding = false; 2693 nia = dhcp6_findia(ifp, m, len, sfrom, acquired); 2694 if (nia == 0 && state->state == DH6S_CONFIRM && ok_errno == 0 && 2695 state->new && state->new_len) { 2696 state->has_no_binding = false; 2697 nia = dhcp6_findia(ifp, state->new, state->new_len, sfrom, 2698 acquired); 2699 } 2700 if (nia == 0) { 2701 logerrx("%s: no useable IA found in lease", ifp->name); 2702 return -1; 2703 } 2704 return nia; 2705 } 2706 2707 static ssize_t 2708 dhcp6_readlease(struct interface *ifp, int validate) 2709 { 2710 struct dhcp6_message *dhcp6 = NULL; 2711 struct dhcp6_state *state; 2712 ssize_t bytes; 2713 int fd; 2714 time_t mtime, now; 2715 #ifdef AUTH 2716 uint8_t *o; 2717 uint16_t ol; 2718 #endif 2719 2720 state = D6_STATE(ifp); 2721 if (state->leasefile[0] == '\0') 2722 logdebugx("reading standard input"); 2723 else 2724 logdebugx("%s: reading lease: %s", ifp->name, state->leasefile); 2725 bytes = dhcp_readfile(ifp->ctx, state->leasefile, (void **)&dhcp6, 2726 NULL); 2727 if (bytes == -1) 2728 goto ex; 2729 2730 if (ifp->ctx->options & DHCPCD_DUMPLEASE || state->leasefile[0] == '\0') 2731 goto out; 2732 2733 if (bytes == 0) 2734 goto ex; 2735 2736 /* If not validating IA's and if they have expired, 2737 * skip to the auth check. */ 2738 if (!validate) 2739 goto auth; 2740 2741 if (dhcp_filemtime(ifp->ctx, state->leasefile, &mtime) == -1) 2742 goto ex; 2743 clock_gettime(CLOCK_MONOTONIC, &state->acquired); 2744 if ((now = time(NULL)) == -1) 2745 goto ex; 2746 state->acquired.tv_sec -= now - mtime; 2747 2748 /* Check to see if the lease is still valid */ 2749 fd = dhcp6_validatelease(ifp, dhcp6, (size_t)bytes, NULL, 2750 &state->acquired); 2751 if (fd == -1) { 2752 bytes = 0; /* We have already reported the error */ 2753 goto ex; 2754 } 2755 2756 if (state->expire != ND6_INFINITE_LIFETIME && 2757 (time_t)state->expire < now - mtime) { 2758 logdebugx("%s: discarding expired lease", ifp->name); 2759 bytes = 0; 2760 goto ex; 2761 } 2762 2763 auth: 2764 #ifdef AUTH 2765 /* Authenticate the message */ 2766 o = dhcp6_findmoption(dhcp6, (size_t)bytes, D6_OPTION_AUTH, &ol); 2767 if (o) { 2768 if (dhcp_auth_validate(&state->auth, &ifp->options->auth, dhcp6, 2769 (size_t)bytes, 6, dhcp6->type, o, ol) == NULL) { 2770 logerr("%s: authentication failed", ifp->name); 2771 bytes = 0; 2772 goto ex; 2773 } 2774 if (state->auth.token) 2775 logdebugx("%s: validated using 0x%08" PRIu32, ifp->name, 2776 state->auth.token->secretid); 2777 else 2778 loginfox("%s: accepted reconfigure key", ifp->name); 2779 } else if ((ifp->options->auth.options & DHCPCD_AUTH_SENDREQUIRE) == 2780 DHCPCD_AUTH_SENDREQUIRE) { 2781 logerrx("%s: authentication now required", ifp->name); 2782 goto ex; 2783 } 2784 #endif 2785 2786 out: 2787 free(state->new); 2788 state->new = dhcp6; 2789 state->new_len = (size_t)bytes; 2790 return bytes; 2791 2792 ex: 2793 dhcp6_freedrop_addrs(ifp, 0, IPV6_AF_DELEGATED, NULL); 2794 dhcp_unlink(ifp->ctx, state->leasefile); 2795 free(state->new); 2796 state->new = NULL; 2797 state->new_len = 0; 2798 dhcp6_addrequestedaddrs(ifp); 2799 free(dhcp6); 2800 return bytes == 0 ? 0 : -1; 2801 } 2802 2803 static void 2804 dhcp6_startinit(struct interface *ifp) 2805 { 2806 struct dhcp6_state *state; 2807 struct if_options *ifo; 2808 ssize_t r; 2809 uint8_t has_ta, has_non_ta; 2810 size_t i; 2811 2812 state = D6_STATE(ifp); 2813 ifo = ifp->options; 2814 state->expire = ND6_INFINITE_LIFETIME; 2815 state->lowpl = ND6_INFINITE_LIFETIME; 2816 2817 dhcp6_addrequestedaddrs(ifp); 2818 has_ta = has_non_ta = 0; 2819 for (i = 0; i < ifo->ia_len; i++) { 2820 switch (ifo->ia[i].ia_type) { 2821 case D6_OPTION_IA_TA: 2822 has_ta = 1; 2823 break; 2824 default: 2825 has_non_ta = 1; 2826 } 2827 } 2828 2829 if (!(ifp->ctx->options & DHCPCD_TEST) && !(has_ta && !has_non_ta) && 2830 ifo->reboot != 0) { 2831 r = dhcp6_readlease(ifp, 1); 2832 if (r == -1) { 2833 if (errno != ENOENT && errno != ESRCH) 2834 logerr("%s: %s", __func__, state->leasefile); 2835 } else if (r != 0 && !(ifo->options & DHCPCD_ANONYMOUS)) { 2836 /* RFC 3633 section 12.1 */ 2837 #ifndef SMALL 2838 if (state->state == DH6S_MANUALREBIND || 2839 dhcp6_hasprefixdelegation(ifp)) 2840 dhcp6_startrebind(ifp); 2841 else 2842 #endif 2843 dhcp6_startconfirm(ifp); 2844 return; 2845 } 2846 } 2847 dhcp6_startdiscoinform(ifp); 2848 } 2849 2850 #ifndef SMALL 2851 static struct ipv6_addr * 2852 dhcp6_ifdelegateaddr(struct interface *ifp, struct ipv6_addr *prefix, 2853 const struct if_sla *sla, struct if_ia *if_ia) 2854 { 2855 struct dhcp6_state *state; 2856 struct in6_addr addr, daddr; 2857 struct ipv6_addr *ia; 2858 int pfxlen, dadcounter; 2859 uint64_t vl; 2860 2861 /* RFC6603 Section 4.2 */ 2862 if (strcmp(ifp->name, prefix->iface->name) == 0) { 2863 if (prefix->prefix_exclude_len == 0) { 2864 /* Don't spam the log automatically */ 2865 if (sla != NULL) 2866 logwarnx("%s: DHCPv6 server does not support " 2867 "OPTION_PD_EXCLUDE", 2868 ifp->name); 2869 return NULL; 2870 } 2871 pfxlen = prefix->prefix_exclude_len; 2872 memcpy(&addr, &prefix->prefix_exclude, sizeof(addr)); 2873 } else if ((pfxlen = dhcp6_delegateaddr(&addr, ifp, prefix, sla, 2874 if_ia)) == -1) 2875 return NULL; 2876 2877 if (sla != NULL && fls64(sla->suffix) > 128 - pfxlen) { 2878 logerrx("%s: suffix %" PRIu64 " + prefix_len %d > 128", 2879 ifp->name, sla->suffix, pfxlen); 2880 return NULL; 2881 } 2882 2883 /* Add our suffix */ 2884 if (sla != NULL && sla->suffix != 0) { 2885 daddr = addr; 2886 vl = be64dec(addr.s6_addr + 8); 2887 vl |= sla->suffix; 2888 be64enc(daddr.s6_addr + 8, vl); 2889 } else { 2890 dadcounter = ipv6_makeaddr(&daddr, ifp, &addr, pfxlen, 0); 2891 if (dadcounter == -1) { 2892 logerrx("%s: error adding slaac to prefix_len %d", 2893 ifp->name, pfxlen); 2894 return NULL; 2895 } 2896 } 2897 2898 /* Find an existing address */ 2899 state = D6_STATE(ifp); 2900 TAILQ_FOREACH(ia, &state->addrs, next) { 2901 if (IN6_ARE_ADDR_EQUAL(&ia->addr, &daddr)) 2902 break; 2903 } 2904 if (ia == NULL) { 2905 ia = ipv6_newaddr(ifp, &daddr, (uint8_t)pfxlen, IPV6_AF_ONLINK); 2906 if (ia == NULL) 2907 return NULL; 2908 ia->dadcallback = dhcp6_dadcallback; 2909 memcpy(&ia->iaid, &prefix->iaid, sizeof(ia->iaid)); 2910 ia->created = prefix->acquired; 2911 2912 TAILQ_INSERT_TAIL(&state->addrs, ia, next); 2913 TAILQ_INSERT_TAIL(&prefix->pd_pfxs, ia, pd_next); 2914 } 2915 ia->delegating_prefix = prefix; 2916 ia->prefix = addr; 2917 ia->prefix_len = (uint8_t)pfxlen; 2918 ia->acquired = prefix->acquired; 2919 ia->prefix_pltime = prefix->prefix_pltime; 2920 ia->prefix_vltime = prefix->prefix_vltime; 2921 2922 /* If the prefix length hasn't changed, 2923 * don't install a reject route. */ 2924 if (prefix->prefix_len == pfxlen) 2925 prefix->flags |= IPV6_AF_NOREJECT; 2926 else 2927 prefix->flags &= ~IPV6_AF_NOREJECT; 2928 2929 return ia; 2930 } 2931 #endif 2932 2933 static void 2934 dhcp6_script_try_run(struct interface *ifp, int delegated) 2935 { 2936 struct dhcp6_state *state; 2937 struct ipv6_addr *ap; 2938 int completed; 2939 2940 state = D6_STATE(ifp); 2941 completed = 1; 2942 /* If all addresses have completed DAD run the script */ 2943 TAILQ_FOREACH(ap, &state->addrs, next) { 2944 if (!(ap->flags & IPV6_AF_ADDED)) 2945 continue; 2946 if (ap->flags & IPV6_AF_ONLINK) { 2947 if (!(ap->flags & IPV6_AF_DADCOMPLETED) && 2948 ipv6_iffindaddr(ap->iface, &ap->addr, 2949 IN6_IFF_TENTATIVE)) 2950 ap->flags |= IPV6_AF_DADCOMPLETED; 2951 if ((ap->flags & IPV6_AF_DADCOMPLETED) == 0 2952 #ifndef SMALL 2953 && ((delegated && ap->delegating_prefix) || 2954 (!delegated && !ap->delegating_prefix)) 2955 #endif 2956 ) { 2957 completed = 0; 2958 break; 2959 } 2960 } 2961 } 2962 if (completed) { 2963 script_runreason(ifp, delegated ? "DELEGATED6" : state->reason); 2964 if (!delegated) 2965 dhcpcd_daemonise(ifp->ctx); 2966 } else 2967 logdebugx("%s: waiting for DHCPv6 DAD to complete", ifp->name); 2968 } 2969 2970 #ifdef SMALL 2971 size_t 2972 dhcp6_find_delegates(__unused struct interface *ifp) 2973 { 2974 return 0; 2975 } 2976 #else 2977 static void 2978 dhcp6_delegate_prefix(struct interface *ifp) 2979 { 2980 struct if_options *ifo; 2981 struct dhcp6_state *state; 2982 struct ipv6_addr *ap; 2983 size_t i, j, k; 2984 struct if_ia *ia; 2985 struct if_sla *sla; 2986 struct interface *ifd; 2987 bool carrier_warned; 2988 2989 ifo = ifp->options; 2990 state = D6_STATE(ifp); 2991 2992 /* Clear the logged flag. */ 2993 TAILQ_FOREACH(ap, &state->addrs, next) { 2994 ap->flags &= ~IPV6_AF_DELEGATEDLOG; 2995 } 2996 2997 TAILQ_FOREACH(ifd, ifp->ctx->ifaces, next) { 2998 if (!ifd->active) 2999 continue; 3000 if (!(ifd->options->options & DHCPCD_CONFIGURE)) 3001 continue; 3002 k = 0; 3003 carrier_warned = false; 3004 TAILQ_FOREACH(ap, &state->addrs, next) { 3005 if (!(ap->flags & IPV6_AF_PFXDELEGATION)) 3006 continue; 3007 if (!(ap->flags & IPV6_AF_DELEGATEDLOG)) { 3008 int loglevel; 3009 3010 if (ap->flags & IPV6_AF_NEW) 3011 loglevel = LOG_INFO; 3012 else 3013 loglevel = LOG_DEBUG; 3014 /* We only want to log this the once as we loop 3015 * through many interfaces first. */ 3016 ap->flags |= IPV6_AF_DELEGATEDLOG; 3017 logmessage(loglevel, "%s: delegated prefix %s", 3018 ifp->name, ap->saddr); 3019 ap->flags &= ~IPV6_AF_NEW; 3020 } 3021 for (i = 0; i < ifo->ia_len; i++) { 3022 ia = &ifo->ia[i]; 3023 if (ia->ia_type != D6_OPTION_IA_PD) 3024 continue; 3025 if (memcmp(ia->iaid, ap->iaid, 3026 sizeof(ia->iaid))) 3027 continue; 3028 if (ia->sla_len == 0) { 3029 /* no SLA configured, so lets 3030 * automate it */ 3031 if (!if_is_link_up(ifd)) { 3032 logdebugx( 3033 "%s: has no carrier, cannot" 3034 " delegate addresses", 3035 ifd->name); 3036 carrier_warned = true; 3037 break; 3038 } 3039 if (dhcp6_ifdelegateaddr(ifd, ap, NULL, 3040 ia)) 3041 k++; 3042 } 3043 for (j = 0; j < ia->sla_len; j++) { 3044 sla = &ia->sla[j]; 3045 if (strcmp(ifd->name, sla->ifname)) 3046 continue; 3047 if (!if_is_link_up(ifd)) { 3048 logdebugx( 3049 "%s: has no carrier, cannot" 3050 " delegate addresses", 3051 ifd->name); 3052 carrier_warned = true; 3053 break; 3054 } 3055 if (dhcp6_ifdelegateaddr(ifd, ap, sla, 3056 ia)) 3057 k++; 3058 } 3059 if (carrier_warned) 3060 break; 3061 } 3062 if (carrier_warned) 3063 break; 3064 } 3065 if (k && !carrier_warned) { 3066 struct dhcp6_state *s = D6_STATE(ifd); 3067 3068 ipv6_addaddrs(&s->addrs); 3069 dhcp6_script_try_run(ifd, 1); 3070 } 3071 } 3072 3073 /* Now all addresses have been added, rebuild the routing table. */ 3074 rt_build(ifp->ctx, AF_INET6); 3075 } 3076 3077 static void 3078 dhcp6_find_delegates1(void *arg) 3079 { 3080 dhcp6_find_delegates(arg); 3081 } 3082 3083 size_t 3084 dhcp6_find_delegates(struct interface *ifp) 3085 { 3086 struct if_options *ifo; 3087 struct dhcp6_state *state; 3088 struct ipv6_addr *ap; 3089 size_t i, j, k; 3090 struct if_ia *ia; 3091 struct if_sla *sla; 3092 struct interface *ifd; 3093 3094 if (ifp->options != NULL && !(ifp->options->options & DHCPCD_CONFIGURE)) 3095 return 0; 3096 3097 k = 0; 3098 TAILQ_FOREACH(ifd, ifp->ctx->ifaces, next) { 3099 ifo = ifd->options; 3100 state = D6_STATE(ifd); 3101 if (state == NULL || state->state != DH6S_BOUND) 3102 continue; 3103 TAILQ_FOREACH(ap, &state->addrs, next) { 3104 if (!(ap->flags & IPV6_AF_PFXDELEGATION)) 3105 continue; 3106 for (i = 0; i < ifo->ia_len; i++) { 3107 ia = &ifo->ia[i]; 3108 if (ia->ia_type != D6_OPTION_IA_PD) 3109 continue; 3110 if (memcmp(ia->iaid, ap->iaid, 3111 sizeof(ia->iaid))) 3112 continue; 3113 for (j = 0; j < ia->sla_len; j++) { 3114 sla = &ia->sla[j]; 3115 if (strcmp(ifp->name, sla->ifname)) 3116 continue; 3117 if (ipv6_linklocal(ifp) == NULL) { 3118 logdebugx( 3119 "%s: delaying adding" 3120 " delegated addresses for" 3121 " LL address", 3122 ifp->name); 3123 ipv6_addlinklocalcallback(ifp, 3124 dhcp6_find_delegates1, ifp); 3125 return 1; 3126 } 3127 if (dhcp6_ifdelegateaddr(ifp, ap, sla, 3128 ia)) 3129 k++; 3130 } 3131 } 3132 } 3133 } 3134 3135 if (k) { 3136 loginfox("%s: adding delegated prefixes", ifp->name); 3137 state = D6_STATE(ifp); 3138 ipv6_addaddrs(&state->addrs); 3139 rt_build(ifp->ctx, AF_INET6); 3140 dhcp6_script_try_run(ifp, 1); 3141 } 3142 return k; 3143 } 3144 #endif 3145 3146 static void 3147 dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) 3148 { 3149 struct dhcp6_state *state = D6_STATE(ifp); 3150 bool timedout = (op == NULL), confirmed; 3151 struct ipv6_addr *ia; 3152 int loglevel; 3153 struct timespec now; 3154 3155 if (state->state == DH6S_RENEW) { 3156 loglevel = LOG_DEBUG; 3157 TAILQ_FOREACH(ia, &state->addrs, next) { 3158 if (ia->flags & IPV6_AF_NEW) { 3159 loglevel = LOG_INFO; 3160 break; 3161 } 3162 } 3163 } else if (state->state == DH6S_INFORM) 3164 loglevel = state->new_start ? LOG_INFO : LOG_DEBUG; 3165 else 3166 loglevel = LOG_INFO; 3167 state->new_start = false; 3168 3169 if (!timedout) { 3170 logmessage(loglevel, "%s: %s received from %s", ifp->name, op, 3171 sfrom); 3172 #ifndef SMALL 3173 /* If we delegated from an unconfirmed lease we MUST drop 3174 * them now. Hopefully we have new delegations. */ 3175 if (state->reason != NULL && 3176 strcmp(state->reason, "TIMEOUT6") == 0) 3177 dhcp6_delete_delegates(ifp); 3178 #endif 3179 state->reason = NULL; 3180 } else 3181 state->reason = "TIMEOUT6"; 3182 3183 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 3184 clock_gettime(CLOCK_MONOTONIC, &now); 3185 3186 switch (state->state) { 3187 case DH6S_INFORM: { 3188 struct dhcp6_option *o; 3189 uint16_t ol; 3190 3191 if (state->reason == NULL) 3192 state->reason = "INFORM6"; 3193 o = dhcp6_findmoption(state->recv, state->recv_len, 3194 D6_OPTION_INFO_REFRESH_TIME, &ol); 3195 if (o == NULL || ol != sizeof(uint32_t)) 3196 state->renew = IRT_DEFAULT; 3197 else { 3198 memcpy(&state->renew, o, ol); 3199 state->renew = ntohl(state->renew); 3200 if (state->renew < IRT_MINIMUM) 3201 state->renew = IRT_MINIMUM; 3202 } 3203 state->rebind = 0; 3204 state->expire = ND6_INFINITE_LIFETIME; 3205 state->lowpl = ND6_INFINITE_LIFETIME; 3206 } break; 3207 3208 case DH6S_REQUEST: 3209 if (state->reason == NULL) 3210 state->reason = "BOUND6"; 3211 /* FALLTHROUGH */ 3212 case DH6S_RENEW: 3213 if (state->reason == NULL) 3214 state->reason = "RENEW6"; 3215 /* FALLTHROUGH */ 3216 case DH6S_REBIND: 3217 if (state->reason == NULL) 3218 state->reason = "REBIND6"; 3219 /* FALLTHROUGH */ 3220 case DH6S_CONFIRM: 3221 if (state->reason == NULL) 3222 state->reason = "REBOOT6"; 3223 if (state->renew != 0) { 3224 bool all_expired = true; 3225 3226 TAILQ_FOREACH(ia, &state->addrs, next) { 3227 if (ia->flags & IPV6_AF_STALE) 3228 continue; 3229 if (!(state->renew == ND6_INFINITE_LIFETIME && 3230 ia->prefix_vltime == 3231 ND6_INFINITE_LIFETIME) && 3232 ia->prefix_vltime != 0 && 3233 ia->prefix_vltime <= state->renew) 3234 logwarnx( 3235 "%s: %s will expire before renewal", 3236 ifp->name, ia->saddr); 3237 else 3238 all_expired = false; 3239 } 3240 if (all_expired) { 3241 /* All address's vltime happens at or before 3242 * the configured T1 in the IA. 3243 * This is a badly configured server and we 3244 * have to use our own notion of what 3245 * T1 and T2 should be as a result. 3246 * 3247 * Doing this violates RFC 3315 22.4: 3248 * In a message sent by a server to a client, 3249 * the client MUST use the values in the T1 3250 * and T2 fields for the T1 and T2 parameters, 3251 * unless those values in those fields are 0. 3252 */ 3253 logwarnx("%s: ignoring T1 %" PRIu32 3254 " due to address expiry", 3255 ifp->name, state->renew); 3256 state->renew = state->rebind = 0; 3257 } 3258 } 3259 if (state->renew == 0 && state->lowpl != ND6_INFINITE_LIFETIME) 3260 state->renew = (uint32_t)(state->lowpl * 0.5); 3261 if (state->rebind == 0 && state->lowpl != ND6_INFINITE_LIFETIME) 3262 state->rebind = (uint32_t)(state->lowpl * 0.8); 3263 break; 3264 default: 3265 state->reason = "UNKNOWN6"; 3266 break; 3267 } 3268 3269 if (state->state != DH6S_CONFIRM && !timedout) { 3270 state->acquired = now; 3271 free(state->old); 3272 state->old = state->new; 3273 state->old_len = state->new_len; 3274 state->new = state->recv; 3275 state->new_len = state->recv_len; 3276 state->recv = NULL; 3277 state->recv_len = 0; 3278 confirmed = false; 3279 } else { 3280 /* Reduce timers based on when we got the lease. */ 3281 uint32_t elapsed; 3282 3283 elapsed = (uint32_t)eloop_timespec_diff(&now, &state->acquired, 3284 NULL); 3285 if (state->renew && state->renew != ND6_INFINITE_LIFETIME) { 3286 if (state->renew > elapsed) 3287 state->renew -= elapsed; 3288 else 3289 state->renew = 0; 3290 } 3291 if (state->rebind && state->rebind != ND6_INFINITE_LIFETIME) { 3292 if (state->rebind > elapsed) 3293 state->rebind -= elapsed; 3294 else 3295 state->rebind = 0; 3296 } 3297 if (state->expire && state->expire != ND6_INFINITE_LIFETIME) { 3298 if (state->expire > elapsed) 3299 state->expire -= elapsed; 3300 else 3301 state->expire = 0; 3302 } 3303 confirmed = true; 3304 } 3305 3306 if (ifp->ctx->options & DHCPCD_TEST) 3307 script_runreason(ifp, "TEST"); 3308 else { 3309 if (state->state == DH6S_INFORM) 3310 state->state = DH6S_INFORMED; 3311 else 3312 state->state = DH6S_BOUND; 3313 state->failed = false; 3314 3315 /* If we CONFIRM we might need to enter RENEW 3316 * or REBIND right away if the timers have expired */ 3317 if ((state->renew || (state->rebind && confirmed)) && 3318 state->renew != ND6_INFINITE_LIFETIME) 3319 eloop_timeout_add_sec(ifp->ctx->eloop, state->renew, 3320 state->state == DH6S_INFORMED ? dhcp6_startinform : 3321 dhcp6_startrenew, 3322 ifp); 3323 if ((state->rebind || (state->expire && confirmed)) && 3324 state->rebind != ND6_INFINITE_LIFETIME) 3325 eloop_timeout_add_sec(ifp->ctx->eloop, state->rebind, 3326 dhcp6_startrebind, ifp); 3327 if (state->expire != ND6_INFINITE_LIFETIME) 3328 eloop_timeout_add_sec(ifp->ctx->eloop, state->expire, 3329 dhcp6_startexpire, ifp); 3330 3331 if (ifp->options->options & DHCPCD_CONFIGURE) { 3332 ipv6_addaddrs(&state->addrs); 3333 if (!timedout) 3334 dhcp6_deprecateaddrs(&state->addrs); 3335 } 3336 3337 if (state->state == DH6S_INFORMED) 3338 logmessage(loglevel, 3339 "%s: refresh in %" PRIu32 " seconds", ifp->name, 3340 state->renew); 3341 else if (state->renew == ND6_INFINITE_LIFETIME) 3342 logmessage(loglevel, "%s: leased for infinity", 3343 ifp->name); 3344 else if (state->renew || state->rebind) 3345 logmessage(loglevel, 3346 "%s: renew in %" PRIu32 ", " 3347 "rebind in %" PRIu32 ", " 3348 "expire in %" PRIu32 " seconds", 3349 ifp->name, state->renew, state->rebind, 3350 state->expire); 3351 else if (state->expire == 0) 3352 logmessage(loglevel, "%s: will expire", ifp->name); 3353 else 3354 logmessage(loglevel, 3355 "%s: expire in %" PRIu32 " seconds", ifp->name, 3356 state->expire); 3357 rt_build(ifp->ctx, AF_INET6); 3358 if (!confirmed && !timedout) { 3359 logdebugx("%s: writing lease: %s", ifp->name, 3360 state->leasefile); 3361 if (dhcp_writefile(ifp->ctx, state->leasefile, 0640, 3362 state->new, state->new_len) == -1) 3363 logerr("dhcp_writefile: %s", state->leasefile); 3364 } 3365 #ifndef SMALL 3366 dhcp6_delegate_prefix(ifp); 3367 #endif 3368 dhcp6_script_try_run(ifp, 0); 3369 } 3370 3371 if (ifp->ctx->options & DHCPCD_TEST) 3372 eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS); 3373 } 3374 3375 static void 3376 dhcp6_adjust_max_rt(struct interface *ifp, struct dhcp6_message *r, size_t len) 3377 { 3378 struct dhcp6_state *state = D6_STATE(ifp); 3379 uint8_t *o; 3380 uint16_t ol; 3381 3382 /* RFC 8415 */ 3383 o = dhcp6_findmoption(r, len, D6_OPTION_SOL_MAX_RT, &ol); 3384 if (o != NULL && ol == sizeof(uint32_t)) { 3385 uint32_t max_rt; 3386 3387 memcpy(&max_rt, o, sizeof(max_rt)); 3388 max_rt = ntohl(max_rt); 3389 if (max_rt >= 60 && max_rt <= 86400) { 3390 logdebugx("%s: SOL_MAX_RT %llu -> %u", ifp->name, 3391 (unsigned long long)state->sol_max_rt, max_rt); 3392 state->sol_max_rt = max_rt; 3393 } else 3394 logerrx("%s: invalid SOL_MAX_RT %u", ifp->name, max_rt); 3395 } 3396 3397 o = dhcp6_findmoption(r, len, D6_OPTION_INF_MAX_RT, &ol); 3398 if (o != NULL && ol == sizeof(uint32_t)) { 3399 uint32_t max_rt; 3400 3401 memcpy(&max_rt, o, sizeof(max_rt)); 3402 max_rt = ntohl(max_rt); 3403 if (max_rt >= 60 && max_rt <= 86400) { 3404 logdebugx("%s: INF_MAX_RT %llu -> %u", ifp->name, 3405 (unsigned long long)state->inf_max_rt, max_rt); 3406 state->inf_max_rt = max_rt; 3407 } else 3408 logerrx("%s: invalid INF_MAX_RT %u", ifp->name, max_rt); 3409 } 3410 } 3411 3412 struct dhcp6_policy { 3413 const struct dhcpcd_ctx *ctx; 3414 const char *ifname; 3415 const char *sfrom; 3416 struct dhcp6_message *msg; 3417 size_t len; 3418 }; 3419 3420 static int 3421 dhcp6_require(uint32_t option, void *arg) 3422 { 3423 struct dhcp6_policy *ctx = arg; 3424 void *o; 3425 3426 o = dhcp6_findmoption(ctx->msg, ctx->len, (uint16_t)option, NULL); 3427 if (o != NULL) 3428 return 0; 3429 3430 logwarnx("%s: reject DHCPv6 (missing option %u) from %s", ctx->ifname, 3431 option, ctx->sfrom); 3432 return -1; 3433 } 3434 3435 static int 3436 dhcp6_reject(uint32_t option, void *arg) 3437 { 3438 struct dhcp6_policy *ctx = arg; 3439 void *o; 3440 3441 o = dhcp6_findmoption(ctx->msg, ctx->len, (uint16_t)option, NULL); 3442 if (o == NULL) 3443 return 0; 3444 3445 logwarnx("%s: reject DHCPv6 (option %u) from %s", ctx->ifname, option, 3446 ctx->sfrom); 3447 return -1; 3448 } 3449 3450 static void 3451 dhcp6_recvif(struct interface *ifp, const char *sfrom, struct dhcp6_message *r, 3452 size_t len) 3453 { 3454 const char *op; 3455 struct dhcp6_state *state; 3456 struct dhcp6_policy policy = { .sfrom = sfrom, .msg = r, .len = len }; 3457 int err; 3458 uint8_t *o, preference = 0; 3459 uint16_t ol; 3460 const struct if_options *ifo; 3461 const struct dho_policy_group *pg; 3462 bool valid_op; 3463 #ifdef AUTH 3464 uint8_t *auth; 3465 uint16_t auth_len; 3466 #endif 3467 3468 state = D6_STATE(ifp); 3469 if (state == NULL || state->send == NULL) { 3470 logdebugx("%s: DHCPv6 reply received but not running", 3471 ifp->name); 3472 return; 3473 } 3474 3475 /* We're already bound and this message is for another machine */ 3476 /* XXX DELEGATED? */ 3477 if (r->type != DHCP6_RECONFIGURE && 3478 (state->state == DH6S_BOUND || state->state == DH6S_INFORMED)) { 3479 logdebugx("%s: DHCPv6 reply received but already bound", 3480 ifp->name); 3481 return; 3482 } 3483 3484 if (dhcp6_findmoption(r, len, D6_OPTION_SERVERID, NULL) == NULL) { 3485 logdebugx("%s: no DHCPv6 server ID from %s", ifp->name, sfrom); 3486 return; 3487 } 3488 3489 ifo = ifp->options; 3490 pg = &ifo->dhopg_dhcp6; 3491 policy.ifname = ifp->name; 3492 3493 err = dho_policy_check(&pg->dhop_require, dhcp6_require, &policy); 3494 if (err == -1) 3495 return; 3496 3497 err = dho_policy_check(&pg->dhop_reject, dhcp6_reject, &policy); 3498 if (err == -1) 3499 return; 3500 3501 #ifdef AUTH 3502 /* Authenticate the message */ 3503 auth = dhcp6_findmoption(r, len, D6_OPTION_AUTH, &auth_len); 3504 if (auth != NULL) { 3505 if (dhcp_auth_validate(&state->auth, &ifo->auth, (uint8_t *)r, 3506 len, 6, r->type, auth, auth_len) == NULL) { 3507 logerr("%s: authentication failed from %s", ifp->name, 3508 sfrom); 3509 return; 3510 } 3511 if (state->auth.token) 3512 logdebugx("%s: validated using 0x%08" PRIu32, ifp->name, 3513 state->auth.token->secretid); 3514 else 3515 loginfox("%s: accepted reconfigure key", ifp->name); 3516 } else if (ifo->auth.options & DHCPCD_AUTH_SEND) { 3517 if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) { 3518 logerrx("%s: no authentication from %s", ifp->name, 3519 sfrom); 3520 return; 3521 } 3522 logwarnx("%s: no authentication from %s", ifp->name, sfrom); 3523 } 3524 #endif 3525 3526 op = dhcp6_get_op(r->type); 3527 valid_op = op != NULL; 3528 switch (r->type) { 3529 case DHCP6_REPLY: 3530 switch (state->state) { 3531 case DH6S_INFORM: 3532 if (dhcp6_checkstatusok(ifp, r, NULL, len) != 0) 3533 return; 3534 break; 3535 case DH6S_CONFIRM: 3536 if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == 3537 -1) { 3538 dhcp6_startdiscoinform(ifp); 3539 return; 3540 } 3541 break; 3542 case DH6S_DISCOVER: 3543 /* Only accept REPLY in DISCOVER for RAPID_COMMIT. 3544 * Normally we get an ADVERTISE for a DISCOVER. */ 3545 if (!dho_policy_requested(pg, D6_OPTION_RAPID_COMMIT) || 3546 !dhcp6_findmoption(r, len, D6_OPTION_RAPID_COMMIT, 3547 NULL)) { 3548 valid_op = false; 3549 break; 3550 } 3551 /* Validate lease before setting state to REQUEST. */ 3552 /* FALLTHROUGH */ 3553 case DH6S_REQUEST: /* FALLTHROUGH */ 3554 case DH6S_RENEW: /* FALLTHROUGH */ 3555 case DH6S_REBIND: 3556 dhcp6_adjust_max_rt(ifp, r, len); 3557 if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == 3558 -1) { 3559 /* 3560 * If we can't use the lease, fallback to 3561 * DISCOVER and try and get a new one. 3562 * 3563 * This is needed become some servers 3564 * renumber the prefix or address 3565 * and deny the current one before it expires 3566 * rather than sending it back with a zero 3567 * lifetime along with the new prefix or 3568 * address to use. 3569 * This behavior is wrong, but moving to the 3570 * DISCOVER phase works around it. 3571 * 3572 * The currently held lease is still valid 3573 * until a new one is found. 3574 */ 3575 if (state->state != DH6S_DISCOVER) 3576 dhcp6_startdiscoinform(ifp); 3577 return; 3578 } 3579 /* RFC8415 18.2.10.1 */ 3580 if ((state->state == DH6S_RENEW || 3581 state->state == DH6S_REBIND) && 3582 state->has_no_binding) { 3583 dhcp6_startrequest(ifp); 3584 return; 3585 } 3586 if (state->state == DH6S_DISCOVER) 3587 state->state = DH6S_REQUEST; 3588 break; 3589 case DH6S_DECLINE: 3590 /* This isnt really a failure, but an 3591 * acknowledgement of one. */ 3592 loginfox("%s: %s acknowledged DECLINE6", ifp->name, 3593 sfrom); 3594 dhcp6_fail(ifp, true); 3595 return; 3596 case DH6S_RELEASE: 3597 loginfox("%s: %s acknowledged RELEASE6", ifp->name, 3598 sfrom); 3599 dhcp6_finishrelease(ifp); 3600 return; 3601 default: 3602 valid_op = false; 3603 break; 3604 } 3605 break; 3606 case DHCP6_ADVERTISE: 3607 if (state->state != DH6S_DISCOVER) { 3608 valid_op = false; 3609 break; 3610 } 3611 3612 o = dhcp6_findmoption(r, len, D6_OPTION_PREFERENCE, &ol); 3613 if (o && ol == sizeof(uint8_t)) 3614 preference = *o; 3615 3616 /* If we already have an advertisement check that this one 3617 * has a higher preference value. */ 3618 if (state->recv_len && state->recv->type == DHCP6_ADVERTISE) { 3619 o = dhcp6_findmoption(state->recv, state->recv_len, 3620 D6_OPTION_PREFERENCE, &ol); 3621 if (o && ol == sizeof(uint8_t) && *o >= preference) { 3622 logdebugx( 3623 "%s: discarding ADVERTISEMENT from %s (%u)", 3624 ifp->name, sfrom, preference); 3625 return; 3626 } 3627 } 3628 3629 dhcp6_adjust_max_rt(ifp, r, len); 3630 if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == -1) 3631 return; 3632 break; 3633 case DHCP6_RECONFIGURE: 3634 #ifdef AUTH 3635 if (auth == NULL) { 3636 #endif 3637 logerrx("%s: unauthenticated %s from %s", ifp->name, op, 3638 sfrom); 3639 if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) 3640 return; 3641 #ifdef AUTH 3642 } 3643 loginfox("%s: %s from %s", ifp->name, op, sfrom); 3644 o = dhcp6_findmoption(r, len, D6_OPTION_RECONF_MSG, &ol); 3645 if (o == NULL) { 3646 logerrx("%s: missing Reconfigure Message option", 3647 ifp->name); 3648 return; 3649 } 3650 if (ol != 1) { 3651 logerrx("%s: missing Reconfigure Message type", 3652 ifp->name); 3653 return; 3654 } 3655 switch (*o) { 3656 case DHCP6_RENEW: 3657 if (state->state != DH6S_BOUND) { 3658 logerrx("%s: not bound, ignoring %s", ifp->name, 3659 op); 3660 return; 3661 } 3662 dhcp6_startrenew(ifp); 3663 break; 3664 case DHCP6_INFORMATION_REQ: 3665 if (state->state != DH6S_INFORMED) { 3666 logerrx("%s: not informed, ignoring %s", 3667 ifp->name, op); 3668 return; 3669 } 3670 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendinform, 3671 ifp); 3672 dhcp6_startinform(ifp); 3673 break; 3674 default: 3675 logerr("%s: unsupported %s type %d", ifp->name, op, *o); 3676 break; 3677 } 3678 return; 3679 #else 3680 break; 3681 #endif 3682 default: 3683 logerrx("%s: invalid DHCP6 type %s (%d)", ifp->name, op, 3684 r->type); 3685 return; 3686 } 3687 if (!valid_op) { 3688 logwarnx("%s: invalid state for DHCP6 type %s (%d)", ifp->name, 3689 op, r->type); 3690 return; 3691 } 3692 3693 if (state->recv_len < (size_t)len) { 3694 free(state->recv); 3695 state->recv = malloc(len); 3696 if (state->recv == NULL) { 3697 logerr(__func__); 3698 return; 3699 } 3700 } 3701 memcpy(state->recv, r, len); 3702 state->recv_len = len; 3703 3704 if (r->type == DHCP6_ADVERTISE) { 3705 struct ipv6_addr *ia; 3706 3707 TAILQ_FOREACH(ia, &state->addrs, next) { 3708 if (!(ia->flags & (IPV6_AF_STALE | IPV6_AF_REQUEST))) 3709 break; 3710 } 3711 if (ia == NULL) 3712 ia = TAILQ_FIRST(&state->addrs); 3713 if (ia == NULL) 3714 loginfox("%s: ADV (no address) from %s (%u)", ifp->name, 3715 sfrom, preference); 3716 else 3717 loginfox("%s: ADV %s from %s (%u)", ifp->name, 3718 ia->saddr, sfrom, preference); 3719 3720 /* 3721 * RFC 8415 18.2.1 says we must collect until ADVERTISEMENTs 3722 * until we get one with a preference of 255 or 3723 * the initial RT has elpased. 3724 */ 3725 if (preference == 255 || state->RTC > 1) 3726 dhcp6_startrequest(ifp); 3727 return; 3728 } 3729 3730 dhcp6_bind(ifp, op, sfrom); 3731 } 3732 3733 void 3734 dhcp6_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, struct ipv6_addr *ia) 3735 { 3736 struct sockaddr_in6 *from = msg->msg_name; 3737 size_t len = msg->msg_iov[0].iov_len; 3738 char sfrom[INET6_ADDRSTRLEN]; 3739 struct interface *ifp; 3740 struct dhcp6_message *r; 3741 const struct dhcp6_state *state; 3742 uint8_t *o; 3743 uint16_t ol; 3744 3745 inet_ntop(AF_INET6, &from->sin6_addr, sfrom, sizeof(sfrom)); 3746 if (len < sizeof(struct dhcp6_message)) { 3747 logerrx("DHCPv6 packet too short from %s", sfrom); 3748 return; 3749 } 3750 3751 if (ia != NULL) 3752 ifp = ia->iface; 3753 else { 3754 ifp = if_findifpfromcmsg(ctx, msg, NULL); 3755 if (ifp == NULL) { 3756 logerr(__func__); 3757 return; 3758 } 3759 } 3760 3761 r = (struct dhcp6_message *)msg->msg_iov[0].iov_base; 3762 3763 uint8_t duid[DUID_LEN], *dp; 3764 size_t duid_len; 3765 o = dhcp6_findmoption(r, len, D6_OPTION_CLIENTID, &ol); 3766 if (ifp->options->options & DHCPCD_ANONYMOUS) { 3767 duid_len = duid_make(duid, ifp, DUID_LL); 3768 dp = duid; 3769 } else { 3770 duid_len = ctx->duid_len; 3771 dp = ctx->duid; 3772 } 3773 if (o == NULL || ol != duid_len || memcmp(o, dp, ol) != 0) { 3774 logdebugx("%s: incorrect client ID from %s", ifp->name, sfrom); 3775 return; 3776 } 3777 3778 if (dhcp6_findmoption(r, len, D6_OPTION_SERVERID, NULL) == NULL) { 3779 logdebugx("%s: no DHCPv6 server ID from %s", ifp->name, sfrom); 3780 return; 3781 } 3782 3783 if (r->type == DHCP6_RECONFIGURE) { 3784 if (!IN6_IS_ADDR_LINKLOCAL(&from->sin6_addr)) { 3785 logerrx("%s: RECONFIGURE6 recv from %s, not LL", 3786 ifp->name, sfrom); 3787 return; 3788 } 3789 goto recvif; 3790 } 3791 3792 state = D6_CSTATE(ifp); 3793 if (state == NULL || r->xid[0] != state->send->xid[0] || 3794 r->xid[1] != state->send->xid[1] || 3795 r->xid[2] != state->send->xid[2]) { 3796 struct interface *ifp1; 3797 const struct dhcp6_state *state1; 3798 3799 /* Find an interface with a matching xid. */ 3800 TAILQ_FOREACH(ifp1, ctx->ifaces, next) { 3801 state1 = D6_CSTATE(ifp1); 3802 if (state1 == NULL || state1->send == NULL) 3803 continue; 3804 if (r->xid[0] == state1->send->xid[0] && 3805 r->xid[1] == state1->send->xid[1] && 3806 r->xid[2] == state1->send->xid[2]) 3807 break; 3808 } 3809 3810 if (ifp1 == NULL) { 3811 if (state != NULL) 3812 logdebugx("%s: wrong xid 0x%02x%02x%02x" 3813 " (expecting 0x%02x%02x%02x) from %s", 3814 ifp->name, r->xid[0], r->xid[1], r->xid[2], 3815 state->send->xid[0], state->send->xid[1], 3816 state->send->xid[2], sfrom); 3817 return; 3818 } 3819 logdebugx("%s: redirecting DHCP6 message to %s", ifp->name, 3820 ifp1->name); 3821 ifp = ifp1; 3822 } 3823 3824 #if 0 3825 /* 3826 * Handy code to inject raw DHCPv6 packets over responses 3827 * from our server. 3828 * This allows me to take a 3rd party wireshark trace and 3829 * replay it in my code. 3830 */ 3831 static int replyn = 0; 3832 char fname[PATH_MAX], tbuf[UDPLEN_MAX]; 3833 int fd; 3834 ssize_t tlen; 3835 uint8_t *si1, *si2; 3836 uint16_t si_len1, si_len2; 3837 3838 snprintf(fname, sizeof(fname), 3839 "/tmp/dhcp6.reply%d.raw", replyn++); 3840 fd = open(fname, O_RDONLY, 0); 3841 if (fd == -1) { 3842 logerr("%s: open: %s", __func__, fname); 3843 return; 3844 } 3845 tlen = read(fd, tbuf, sizeof(tbuf)); 3846 if (tlen == -1) 3847 logerr("%s: read: %s", __func__, fname); 3848 close(fd); 3849 3850 /* Copy across ServerID so we can work with our own server. */ 3851 si1 = dhcp6_findmoption(r, len, D6_OPTION_SERVERID, &si_len1); 3852 si2 = dhcp6_findmoption(tbuf, (size_t)tlen, 3853 D6_OPTION_SERVERID, &si_len2); 3854 if (si1 != NULL && si2 != NULL && si_len1 == si_len2) 3855 memcpy(si2, si1, si_len2); 3856 r = (struct dhcp6_message *)tbuf; 3857 len = (size_t)tlen; 3858 #endif 3859 3860 recvif: 3861 dhcp6_recvif(ifp, sfrom, r, len); 3862 } 3863 3864 static void 3865 dhcp6_recv(struct dhcpcd_ctx *ctx, struct ipv6_addr *ia, unsigned short events) 3866 { 3867 struct sockaddr_in6 from; 3868 union { 3869 struct dhcp6_message dhcp6; 3870 uint8_t buf[UDPLEN_MAX]; /* Maximum UDP message size */ 3871 } iovbuf; 3872 struct iovec iov = { 3873 .iov_base = iovbuf.buf, 3874 .iov_len = sizeof(iovbuf.buf), 3875 }; 3876 union { 3877 struct cmsghdr hdr; 3878 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; 3879 } cmsgbuf = { .buf = { 0 } }; 3880 struct msghdr msg = { 3881 .msg_name = &from, 3882 .msg_namelen = sizeof(from), 3883 .msg_iov = &iov, 3884 .msg_iovlen = 1, 3885 .msg_control = cmsgbuf.buf, 3886 .msg_controllen = sizeof(cmsgbuf.buf), 3887 }; 3888 int s; 3889 ssize_t bytes; 3890 3891 if (events != ELE_READ) 3892 logerrx("%s: unexpected event 0x%04x", __func__, events); 3893 3894 s = ia != NULL ? ia->dhcp6_fd : ctx->dhcp6_rfd; 3895 bytes = recvmsg(s, &msg, 0); 3896 if (bytes == -1) { 3897 logerr(__func__); 3898 return; 3899 } 3900 3901 iov.iov_len = (size_t)bytes; 3902 dhcp6_recvmsg(ctx, &msg, ia); 3903 } 3904 3905 static void 3906 3907 dhcp6_recvaddr(void *arg, unsigned short events) 3908 { 3909 struct ipv6_addr *ia = arg; 3910 3911 dhcp6_recv(ia->iface->ctx, ia, events); 3912 } 3913 3914 static void 3915 dhcp6_recvctx(void *arg, unsigned short events) 3916 { 3917 struct dhcpcd_ctx *ctx = arg; 3918 3919 dhcp6_recv(ctx, NULL, events); 3920 } 3921 3922 int 3923 dhcp6_openraw(void) 3924 { 3925 int fd, v; 3926 3927 fd = xsocket(PF_INET6, SOCK_RAW | SOCK_CXNB, IPPROTO_UDP); 3928 if (fd == -1) 3929 return -1; 3930 3931 v = 1; 3932 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &v, sizeof(v)) == -1) 3933 goto errexit; 3934 3935 v = offsetof(struct udphdr, uh_sum); 3936 if (setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &v, sizeof(v)) == -1) 3937 goto errexit; 3938 3939 return fd; 3940 3941 errexit: 3942 close(fd); 3943 return -1; 3944 } 3945 3946 int 3947 dhcp6_openudp(unsigned int ifindex, struct in6_addr *ia) 3948 { 3949 struct sockaddr_in6 sa; 3950 int n, s; 3951 3952 s = xsocket(PF_INET6, SOCK_DGRAM | SOCK_CXNB, IPPROTO_UDP); 3953 if (s == -1) 3954 goto errexit; 3955 3956 memset(&sa, 0, sizeof(sa)); 3957 sa.sin6_family = AF_INET6; 3958 sa.sin6_port = htons(DHCP6_CLIENT_PORT); 3959 #ifdef BSD 3960 sa.sin6_len = sizeof(sa); 3961 #endif 3962 3963 if (ia != NULL) { 3964 memcpy(&sa.sin6_addr, ia, sizeof(sa.sin6_addr)); 3965 ipv6_setscope(&sa, ifindex); 3966 } 3967 3968 if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) 3969 goto errexit; 3970 3971 n = 1; 3972 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &n, sizeof(n)) == -1) 3973 goto errexit; 3974 3975 #ifdef SO_RERROR 3976 n = 1; 3977 if (setsockopt(s, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) == -1) 3978 goto errexit; 3979 #endif 3980 3981 return s; 3982 3983 errexit: 3984 logerr(__func__); 3985 if (s != -1) 3986 close(s); 3987 return -1; 3988 } 3989 3990 #ifndef SMALL 3991 static void 3992 dhcp6_activateinterfaces(struct interface *ifp) 3993 { 3994 struct interface *ifd; 3995 size_t i, j; 3996 struct if_ia *ia; 3997 struct if_sla *sla; 3998 3999 for (i = 0; i < ifp->options->ia_len; i++) { 4000 ia = &ifp->options->ia[i]; 4001 if (ia->ia_type != D6_OPTION_IA_PD) 4002 continue; 4003 for (j = 0; j < ia->sla_len; j++) { 4004 sla = &ia->sla[j]; 4005 ifd = if_find(ifp->ctx->ifaces, sla->ifname); 4006 if (ifd == NULL) { 4007 if (*sla->ifname != '-') 4008 logwarn("%s: cannot delegate to %s", 4009 ifp->name, sla->ifname); 4010 continue; 4011 } 4012 if (!ifd->active) { 4013 loginfox("%s: activating for delegation", 4014 sla->ifname); 4015 dhcpcd_activateinterface(ifd, 4016 DHCPCD_IPV6 | DHCPCD_DHCP6); 4017 } 4018 } 4019 } 4020 } 4021 #endif 4022 4023 static void 4024 dhcp6_start1(void *arg) 4025 { 4026 struct interface *ifp = arg; 4027 struct dhcpcd_ctx *ctx = ifp->ctx; 4028 struct if_options *ifo = ifp->options; 4029 struct dho_policy_group *pg = &ifo->dhopg_dhcp6; 4030 struct dhcp6_state *state; 4031 4032 if ((ctx->options & (DHCPCD_MANAGER | DHCPCD_PRIVSEP)) == 4033 DHCPCD_MANAGER && 4034 ctx->dhcp6_rfd == -1) { 4035 ctx->dhcp6_rfd = dhcp6_openudp(0, NULL); 4036 if (ctx->dhcp6_rfd == -1) { 4037 logerr(__func__); 4038 return; 4039 } 4040 if (eloop_event_add(ctx->eloop, ctx->dhcp6_rfd, ELE_READ, 4041 dhcp6_recvctx, ctx) == -1) 4042 logerr("%s: eloop_event_add", __func__); 4043 } 4044 4045 if (!IN_PRIVSEP(ctx) && ctx->dhcp6_wfd == -1) { 4046 ctx->dhcp6_wfd = dhcp6_openraw(); 4047 if (ctx->dhcp6_wfd == -1) { 4048 logerr(__func__); 4049 return; 4050 } 4051 } 4052 4053 state = D6_STATE(ifp); 4054 /* If no DHCPv6 options are configured, 4055 match configured DHCPv4 options to DHCPv6 equivalents. */ 4056 if (pg->dhop_request.dhop_policy_len == 0) { 4057 #ifdef INET 4058 const struct dhcp_compat *dhc; 4059 const struct dho_policy_group *dpg = &ifo->dhopg_dhcp; 4060 #endif 4061 int err; 4062 4063 #ifdef INET 4064 for (dhc = dhcp_compats; dhc->dhcp_opt; dhc++) { 4065 if (!dho_policy_has(&dpg->dhop_request, dhc->dhcp_opt)) 4066 continue; 4067 err = dho_policy_add(&pg->dhop_request, dhc->dhcp6_opt); 4068 if (err == -1) { 4069 logerr(__func__); 4070 return; 4071 } 4072 } 4073 #else 4074 err = dho_policy_add(&pg->dhop_request, D6_OPTION_DNS_SERVERS); 4075 if (err == -1) { 4076 logerr(__func__); 4077 return; 4078 } 4079 err = dho_policy_add(&pg->dhop_request, D6_OPTION_DOMAIN_LIST); 4080 if (err == -1) { 4081 logerr(__func__); 4082 return; 4083 } 4084 #endif 4085 if (ifo->fqdn != FQDN_DISABLE || 4086 ifo->options & DHCPCD_HOSTNAME) { 4087 err = dho_policy_add(&pg->dhop_request, D6_OPTION_FQDN); 4088 if (err == -1) { 4089 logerr(__func__); 4090 return; 4091 } 4092 } 4093 } 4094 4095 #ifndef SMALL 4096 /* Rapid commit won't work with Prefix Delegation Exclusion */ 4097 if (dhcp6_findselfsla(ifp)) 4098 dho_policy_del(&pg->dhop_request, D6_OPTION_RAPID_COMMIT); 4099 #endif 4100 4101 if (state->state == DH6S_INFORM) 4102 dhcp6_startinform(ifp); 4103 else 4104 dhcp6_startinit(ifp); 4105 4106 #ifndef SMALL 4107 dhcp6_activateinterfaces(ifp); 4108 #endif 4109 } 4110 4111 int 4112 dhcp6_start(struct interface *ifp, enum DH6S init_state) 4113 { 4114 struct dhcp6_state *state; 4115 4116 state = D6_STATE(ifp); 4117 if (state != NULL) { 4118 switch (init_state) { 4119 case DH6S_INIT: 4120 goto gogogo; 4121 case DH6S_INFORM: 4122 /* RFC 8415 21.23 4123 * If D6_OPTION_INFO_REFRESH_TIME does not exist 4124 * then we MUST refresh by IRT_DEFAULT seconds 4125 * and should not be influenced by only the 4126 * pl/vl time of the RA changing. */ 4127 if (state->state == DH6S_INIT || 4128 (state->state == DH6S_DISCOVER && 4129 !(ifp->options->options & DHCPCD_IA_FORCED) && 4130 !ipv6nd_hasradhcp(ifp, true))) 4131 dhcp6_startinform(ifp); 4132 break; 4133 case DH6S_REQUEST: 4134 if (ifp->options->options & DHCPCD_DHCP6 && 4135 (state->state == DH6S_INIT || 4136 state->state == DH6S_INFORM || 4137 state->state == DH6S_INFORMED || 4138 state->state == DH6S_DELEGATED)) { 4139 /* Change from stateless to stateful */ 4140 init_state = DH6S_INIT; 4141 goto gogogo; 4142 } 4143 break; 4144 case DH6S_CONFIRM: 4145 /* 4146 * CONFIRM a prior lease from a RA. 4147 * This could be triggered by a roaming interface. 4148 * We could also get here if we are delegated to. 4149 * Now that we don't remove delegated addresses when 4150 * reading the lease file this is the safe path. 4151 */ 4152 if (state->state == DH6S_MANUALREBIND) 4153 init_state = DH6S_MANUALREBIND; 4154 else 4155 init_state = DH6S_INIT; 4156 goto gogogo; 4157 default: 4158 /* Not possible, but sushes some compiler warnings. */ 4159 break; 4160 } 4161 return 0; 4162 } else { 4163 switch (init_state) { 4164 case DH6S_CONFIRM: 4165 /* No DHCPv6 config, no existing state 4166 * so nothing to do. */ 4167 return 0; 4168 case DH6S_INFORM: 4169 break; 4170 default: 4171 init_state = DH6S_INIT; 4172 break; 4173 } 4174 } 4175 4176 if (!(ifp->options->options & DHCPCD_DHCP6)) 4177 return 0; 4178 4179 ifp->if_data[IF_DATA_DHCP6] = calloc(1, sizeof(*state)); 4180 state = D6_STATE(ifp); 4181 if (state == NULL) 4182 return -1; 4183 4184 state->sol_max_rt = SOL_MAX_RT; 4185 state->inf_max_rt = INF_MAX_RT; 4186 TAILQ_INIT(&state->addrs); 4187 4188 gogogo: 4189 state->state = init_state; 4190 state->new_start = true; 4191 state->lerror = 0; 4192 state->failed = false; 4193 dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), AF_INET6, 4194 ifp); 4195 if (ipv6_linklocal(ifp) == NULL) { 4196 logdebugx("%s: delaying DHCPv6 for LL address", ifp->name); 4197 ipv6_addlinklocalcallback(ifp, dhcp6_start1, ifp); 4198 return 0; 4199 } 4200 4201 dhcp6_start1(ifp); 4202 return 0; 4203 } 4204 4205 void 4206 dhcp6_reboot(struct interface *ifp) 4207 { 4208 struct dhcp6_state *state; 4209 4210 state = D6_STATE(ifp); 4211 if (state == NULL) 4212 return; 4213 4214 switch (state->state) { 4215 case DH6S_RENEW: /* FALLTHROUGH */ 4216 case DH6S_BOUND: /* FALLTHROUGH */ 4217 case DH6S_REBIND: 4218 state->state = DH6S_MANUALREBIND; 4219 break; 4220 default: /* Appease compilers */ 4221 break; 4222 } 4223 4224 /* Do nothing. On confirming the next lease we will REBIND instead. */ 4225 } 4226 4227 static void 4228 dhcp6_freedrop(struct interface *ifp, int drop, const char *reason) 4229 { 4230 struct dhcp6_state *state; 4231 struct dhcpcd_ctx *ctx; 4232 unsigned long long options; 4233 4234 if (ifp->options) 4235 options = ifp->options->options; 4236 else 4237 options = ifp->ctx->options; 4238 4239 if (ifp->ctx->eloop) 4240 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 4241 4242 #ifndef SMALL 4243 /* If we're dropping the lease, drop delegated addresses. 4244 * If, for whatever reason, we don't drop them in the future 4245 * then they should at least be marked as deprecated (pltime 0). */ 4246 if (drop && (options & DHCPCD_NODROP) != DHCPCD_NODROP) 4247 dhcp6_delete_delegates(ifp); 4248 #endif 4249 4250 state = D6_STATE(ifp); 4251 if (state) { 4252 /* Failure to send the release may cause this function to 4253 * re-enter */ 4254 if (state->state == DH6S_RELEASE) { 4255 dhcp6_finishrelease(ifp); 4256 return; 4257 } 4258 4259 if (drop && options & DHCPCD_RELEASE && 4260 state->state != DH6S_DELEGATED) { 4261 if (if_is_link_up(ifp) && 4262 state->state != DH6S_RELEASED && 4263 state->state != DH6S_INFORMED) { 4264 dhcp6_startrelease(ifp); 4265 return; 4266 } 4267 dhcp_unlink(ifp->ctx, state->leasefile); 4268 } 4269 #ifdef AUTH 4270 else if (state->auth.reconf != NULL) { 4271 /* 4272 * Drop the lease as the token may only be present 4273 * in the initial reply message and not subsequent 4274 * renewals. 4275 * If dhcpcd is restarted, the token is lost. 4276 * XXX persist this in another file? 4277 */ 4278 dhcp_unlink(ifp->ctx, state->leasefile); 4279 } 4280 #endif 4281 4282 dhcp6_freedrop_addrs(ifp, drop, 0, NULL); 4283 free(state->old); 4284 state->old = state->new; 4285 state->old_len = state->new_len; 4286 state->new = NULL; 4287 state->new_len = 0; 4288 if (drop && state->old && 4289 (options & DHCPCD_NODROP) != DHCPCD_NODROP) { 4290 if (reason == NULL) 4291 reason = "STOP6"; 4292 script_runreason(ifp, reason); 4293 } 4294 free(state->old); 4295 free(state->send); 4296 free(state->recv); 4297 free(state); 4298 ifp->if_data[IF_DATA_DHCP6] = NULL; 4299 } 4300 dhcpcd_dropped(ifp); 4301 4302 /* If we don't have any more DHCP6 enabled interfaces, 4303 * close the global socket and release resources */ 4304 ctx = ifp->ctx; 4305 if (ctx->ifaces) { 4306 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 4307 if (D6_STATE(ifp)) 4308 break; 4309 } 4310 } 4311 if (ifp == NULL && ctx->dhcp6_rfd != -1) { 4312 eloop_event_delete(ctx->eloop, ctx->dhcp6_rfd); 4313 close(ctx->dhcp6_rfd); 4314 ctx->dhcp6_rfd = -1; 4315 } 4316 } 4317 4318 void 4319 dhcp6_drop(struct interface *ifp, const char *reason) 4320 { 4321 dhcp6_freedrop(ifp, 1, reason); 4322 } 4323 4324 void 4325 dhcp6_free(struct interface *ifp) 4326 { 4327 dhcp6_freedrop(ifp, 0, NULL); 4328 } 4329 4330 void 4331 dhcp6_abort(struct interface *ifp) 4332 { 4333 struct dhcp6_state *state; 4334 4335 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_start1, ifp); 4336 state = D6_STATE(ifp); 4337 if (state == NULL) 4338 return; 4339 4340 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_startdiscover, ifp); 4341 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_senddiscover, ifp); 4342 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_startinform, ifp); 4343 eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendinform, ifp); 4344 4345 switch (state->state) { 4346 case DH6S_DISCOVER: /* FALLTHROUGH */ 4347 case DH6S_REQUEST: /* FALLTHROUGH */ 4348 case DH6S_INFORM: 4349 state->state = DH6S_INIT; 4350 break; 4351 default: 4352 break; 4353 } 4354 } 4355 4356 void 4357 dhcp6_handleifa(int cmd, struct ipv6_addr *ia, pid_t pid) 4358 { 4359 struct dhcp6_state *state; 4360 struct interface *ifp = ia->iface; 4361 4362 /* If not running in manager mode, listen to this address */ 4363 if (cmd == RTM_NEWADDR && !(ia->addr_flags & IN6_IFF_NOTUSEABLE) && 4364 ifp->active == IF_ACTIVE_USER && 4365 !(ifp->ctx->options & DHCPCD_MANAGER) && 4366 ifp->options->options & DHCPCD_DHCP6) { 4367 #ifdef PRIVSEP 4368 if (IN_PRIVSEP_SE(ifp->ctx)) { 4369 if (ps_inet_opendhcp6(ia) == -1) 4370 logerr(__func__); 4371 } else 4372 #endif 4373 { 4374 if (ia->dhcp6_fd == -1) 4375 ia->dhcp6_fd = dhcp6_openudp(ia->iface->index, 4376 &ia->addr); 4377 if (ia->dhcp6_fd != -1 && 4378 eloop_event_add(ia->iface->ctx->eloop, ia->dhcp6_fd, 4379 ELE_READ, dhcp6_recvaddr, ia) == -1) 4380 logerr("%s: eloop_event_add", __func__); 4381 } 4382 } 4383 4384 if ((state = D6_STATE(ifp)) != NULL) 4385 ipv6_handleifa_addrs(cmd, &state->addrs, ia, pid); 4386 } 4387 4388 ssize_t 4389 dhcp6_env(FILE *fp, const char *prefix, const struct interface *ifp, 4390 const struct dhcp6_message *m, size_t len) 4391 { 4392 const struct if_options *ifo; 4393 const struct dho_policy_group *pg; 4394 struct dhcp_opt *opt, *vo; 4395 const uint8_t *p; 4396 struct dhcp6_option o; 4397 size_t i; 4398 char *pfx; 4399 uint32_t en; 4400 const struct dhcpcd_ctx *ctx; 4401 #ifndef SMALL 4402 const struct dhcp6_state *state; 4403 const struct ipv6_addr *ap; 4404 bool first; 4405 #endif 4406 4407 if (m == NULL) 4408 goto delegated; 4409 4410 if (len < sizeof(*m)) { 4411 /* Should be impossible with guards at packet in 4412 * and reading leases */ 4413 errno = EINVAL; 4414 return -1; 4415 } 4416 4417 ifo = ifp->options; 4418 pg = &ifo->dhopg_dhcp6; 4419 ctx = ifp->ctx; 4420 4421 /* Zero our indexes */ 4422 for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; i++, opt++) 4423 dhcp_zero_index(opt); 4424 for (i = 0, opt = ifp->options->dhcp6_override; 4425 i < ifp->options->dhcp6_override_len; i++, opt++) 4426 dhcp_zero_index(opt); 4427 for (i = 0, opt = ctx->vivso; i < ctx->vivso_len; i++, opt++) 4428 dhcp_zero_index(opt); 4429 if (asprintf(&pfx, "%s_dhcp6", prefix) == -1) 4430 return -1; 4431 4432 /* Unlike DHCP, DHCPv6 options *may* occur more than once. 4433 * There is also no provision for option concatenation unlike DHCP. */ 4434 p = (const uint8_t *)m + sizeof(*m); 4435 len -= sizeof(*m); 4436 for (; len != 0; p += o.len, len -= o.len) { 4437 if (len < sizeof(o)) { 4438 errno = EINVAL; 4439 break; 4440 } 4441 memcpy(&o, p, sizeof(o)); 4442 p += sizeof(o); 4443 len -= sizeof(o); 4444 o.len = ntohs(o.len); 4445 if (len < o.len) { 4446 errno = EINVAL; 4447 break; 4448 } 4449 o.code = ntohs(o.code); 4450 if (!dho_policy_allowed(pg, o.code)) 4451 continue; 4452 for (i = 0, opt = ifo->dhcp6_override; 4453 i < ifo->dhcp6_override_len; i++, opt++) 4454 if (opt->option == o.code) 4455 break; 4456 if (i == ifo->dhcp6_override_len && 4457 o.code == D6_OPTION_VENDOR_OPTS && o.len > sizeof(en)) { 4458 memcpy(&en, p, sizeof(en)); 4459 en = ntohl(en); 4460 vo = vivso_find(en, ifp); 4461 } else 4462 vo = NULL; 4463 if (i == ifo->dhcp6_override_len) { 4464 for (i = 0, opt = ctx->dhcp6_opts; 4465 i < ctx->dhcp6_opts_len; i++, opt++) 4466 if (opt->option == o.code) 4467 break; 4468 if (i == ctx->dhcp6_opts_len) 4469 opt = NULL; 4470 } 4471 if (opt) { 4472 dhcp_envoption(ifp->ctx, fp, pfx, ifp->name, opt, 4473 dhcp6_getoption, p, o.len); 4474 } 4475 if (vo) { 4476 dhcp_envoption(ifp->ctx, fp, pfx, ifp->name, vo, 4477 dhcp6_getoption, p + sizeof(en), 4478 o.len - sizeof(en)); 4479 } 4480 } 4481 free(pfx); 4482 4483 delegated: 4484 #ifndef SMALL 4485 /* Needed for Delegated Prefixes */ 4486 state = D6_CSTATE(ifp); 4487 TAILQ_FOREACH(ap, &state->addrs, next) { 4488 if (ap->delegating_prefix) 4489 break; 4490 } 4491 if (ap == NULL) 4492 return 1; 4493 if (fprintf(fp, "%s_delegated_dhcp6_prefix=", prefix) == -1) 4494 return -1; 4495 first = true; 4496 TAILQ_FOREACH(ap, &state->addrs, next) { 4497 if (ap->delegating_prefix == NULL) 4498 continue; 4499 if (first) 4500 first = false; 4501 else { 4502 if (fputc(' ', fp) == EOF) 4503 return -1; 4504 } 4505 if (fprintf(fp, "%s", ap->saddr) == -1) 4506 return -1; 4507 } 4508 if (fputc('\0', fp) == EOF) 4509 return -1; 4510 #endif 4511 4512 return 1; 4513 } 4514 #endif 4515 4516 #ifndef SMALL 4517 int 4518 dhcp6_dump(struct interface *ifp) 4519 { 4520 struct dhcp6_state *state; 4521 4522 ifp->if_data[IF_DATA_DHCP6] = state = calloc(1, sizeof(*state)); 4523 if (state == NULL) { 4524 logerr(__func__); 4525 return -1; 4526 } 4527 TAILQ_INIT(&state->addrs); 4528 if (dhcp6_readlease(ifp, 0) == -1) { 4529 logerr("dhcp6_readlease"); 4530 return -1; 4531 } 4532 state->reason = "DUMP6"; 4533 return script_runreason(ifp, state->reason); 4534 } 4535 #endif 4536