1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2019 Roy Marples <roy@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 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <sys/stat.h> 32 33 #include <arpa/inet.h> 34 #include <net/if.h> 35 #include <net/route.h> 36 #include <netinet/if_ether.h> 37 #include <netinet/in_systm.h> 38 #include <netinet/in.h> 39 #include <netinet/ip.h> 40 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */ 41 #include <netinet/udp.h> 42 #undef __FAVOR_BSD 43 44 #ifdef AF_LINK 45 # include <net/if_dl.h> 46 #endif 47 48 #include <assert.h> 49 #include <ctype.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <inttypes.h> 53 #include <stdbool.h> 54 #include <stddef.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 #define ELOOP_QUEUE 2 61 #include "config.h" 62 #include "arp.h" 63 #include "bpf.h" 64 #include "common.h" 65 #include "dhcp.h" 66 #include "dhcpcd.h" 67 #include "dhcp-common.h" 68 #include "duid.h" 69 #include "eloop.h" 70 #include "if.h" 71 #include "ipv4.h" 72 #include "ipv4ll.h" 73 #include "logerr.h" 74 #include "sa.h" 75 #include "script.h" 76 77 #define DAD "Duplicate address detected" 78 #define DHCP_MIN_LEASE 20 79 80 #define IPV4A ADDRIPV4 | ARRAY 81 #define IPV4R ADDRIPV4 | REQUEST 82 83 /* We should define a maximum for the NAK exponential backoff */ 84 #define NAKOFF_MAX 60 85 86 /* Wait N nanoseconds between sending a RELEASE and dropping the address. 87 * This gives the kernel enough time to actually send it. */ 88 #define RELEASE_DELAY_S 0 89 #define RELEASE_DELAY_NS 10000000 90 91 #ifndef IPDEFTTL 92 #define IPDEFTTL 64 /* RFC1340 */ 93 #endif 94 95 /* Support older systems with different defines */ 96 #if !defined(IP_RECVPKTINFO) && defined(IP_PKTINFO) 97 #define IP_RECVPKTINFO IP_PKTINFO 98 #endif 99 100 /* Assert the correct structure size for on wire */ 101 __CTASSERT(sizeof(struct ip) == 20); 102 __CTASSERT(sizeof(struct udphdr) == 8); 103 __CTASSERT(sizeof(struct bootp) == 300); 104 105 struct dhcp_op { 106 uint8_t value; 107 const char *name; 108 }; 109 110 static const struct dhcp_op dhcp_ops[] = { 111 { DHCP_DISCOVER, "DISCOVER" }, 112 { DHCP_OFFER, "OFFER" }, 113 { DHCP_REQUEST, "REQUEST" }, 114 { DHCP_DECLINE, "DECLINE" }, 115 { DHCP_ACK, "ACK" }, 116 { DHCP_NAK, "NAK" }, 117 { DHCP_RELEASE, "RELEASE" }, 118 { DHCP_INFORM, "INFORM" }, 119 { DHCP_FORCERENEW, "FORCERENEW"}, 120 { 0, NULL } 121 }; 122 123 static const char * const dhcp_params[] = { 124 "ip_address", 125 "subnet_cidr", 126 "network_number", 127 "filename", 128 "server_name", 129 NULL 130 }; 131 132 static int dhcp_openbpf(struct interface *); 133 static void dhcp_start1(void *); 134 #if defined(ARP) && (!defined(KERNEL_RFC5227) || defined(ARPING)) 135 static void dhcp_arp_found(struct arp_state *, const struct arp_msg *); 136 #endif 137 static void dhcp_handledhcp(struct interface *, struct bootp *, size_t, 138 const struct in_addr *); 139 static void dhcp_handleifudp(void *); 140 static int dhcp_initstate(struct interface *); 141 142 void 143 dhcp_printoptions(const struct dhcpcd_ctx *ctx, 144 const struct dhcp_opt *opts, size_t opts_len) 145 { 146 const char * const *p; 147 size_t i, j; 148 const struct dhcp_opt *opt, *opt2; 149 int cols; 150 151 for (p = dhcp_params; *p; p++) 152 printf(" %s\n", *p); 153 154 for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++) { 155 for (j = 0, opt2 = opts; j < opts_len; j++, opt2++) 156 if (opt->option == opt2->option) 157 break; 158 if (j == opts_len) { 159 cols = printf("%03d %s", opt->option, opt->var); 160 dhcp_print_option_encoding(opt, cols); 161 } 162 } 163 for (i = 0, opt = opts; i < opts_len; i++, opt++) { 164 cols = printf("%03d %s", opt->option, opt->var); 165 dhcp_print_option_encoding(opt, cols); 166 } 167 } 168 169 static const uint8_t * 170 get_option(struct dhcpcd_ctx *ctx, 171 const struct bootp *bootp, size_t bootp_len, 172 unsigned int opt, size_t *opt_len) 173 { 174 const uint8_t *p, *e; 175 uint8_t l, o, ol, overl, *bp; 176 const uint8_t *op; 177 size_t bl; 178 179 if (bootp == NULL || bootp_len < DHCP_MIN_LEN) { 180 errno = EINVAL; 181 return NULL; 182 } 183 184 /* Check we have the magic cookie */ 185 if (!IS_DHCP(bootp)) { 186 errno = ENOTSUP; 187 return NULL; 188 } 189 190 p = bootp->vend + 4; /* options after the 4 byte cookie */ 191 e = (const uint8_t *)bootp + bootp_len; 192 ol = o = overl = 0; 193 bp = NULL; 194 op = NULL; 195 bl = 0; 196 while (p < e) { 197 o = *p++; 198 switch (o) { 199 case DHO_PAD: 200 /* No length to read */ 201 continue; 202 case DHO_END: 203 if (overl & 1) { 204 /* bit 1 set means parse boot file */ 205 overl = (uint8_t)(overl & ~1); 206 p = bootp->file; 207 e = p + sizeof(bootp->file); 208 } else if (overl & 2) { 209 /* bit 2 set means parse server name */ 210 overl = (uint8_t)(overl & ~2); 211 p = bootp->sname; 212 e = p + sizeof(bootp->sname); 213 } else 214 goto exit; 215 /* No length to read */ 216 continue; 217 } 218 219 /* Check we can read the length */ 220 if (p == e) { 221 errno = EINVAL; 222 return NULL; 223 } 224 l = *p++; 225 226 /* Check we can read the option data, if present */ 227 if (p + l > e) { 228 errno = EINVAL; 229 return NULL; 230 } 231 232 if (o == DHO_OPTSOVERLOADED) { 233 /* Ensure we only get this option once by setting 234 * the last bit as well as the value. 235 * This is valid because only the first two bits 236 * actually mean anything in RFC2132 Section 9.3 */ 237 if (l == 1 && !overl) 238 overl = 0x80 | p[0]; 239 } 240 241 if (o == opt) { 242 if (op) { 243 /* We must concatonate the options. */ 244 if (bl + l > ctx->opt_buffer_len) { 245 size_t pos; 246 uint8_t *nb; 247 248 if (bp) 249 pos = (size_t) 250 (bp - ctx->opt_buffer); 251 else 252 pos = 0; 253 nb = realloc(ctx->opt_buffer, bl + l); 254 if (nb == NULL) 255 return NULL; 256 ctx->opt_buffer = nb; 257 ctx->opt_buffer_len = bl + l; 258 bp = ctx->opt_buffer + pos; 259 } 260 if (bp == NULL) 261 bp = ctx->opt_buffer; 262 memcpy(bp, op, ol); 263 bp += ol; 264 } 265 ol = l; 266 op = p; 267 bl += ol; 268 } 269 p += l; 270 } 271 272 exit: 273 if (opt_len) 274 *opt_len = bl; 275 if (bp) { 276 memcpy(bp, op, ol); 277 return (const uint8_t *)ctx->opt_buffer; 278 } 279 if (op) 280 return op; 281 errno = ENOENT; 282 return NULL; 283 } 284 285 static int 286 get_option_addr(struct dhcpcd_ctx *ctx, 287 struct in_addr *a, const struct bootp *bootp, size_t bootp_len, 288 uint8_t option) 289 { 290 const uint8_t *p; 291 size_t len; 292 293 p = get_option(ctx, bootp, bootp_len, option, &len); 294 if (!p || len < (ssize_t)sizeof(a->s_addr)) 295 return -1; 296 memcpy(&a->s_addr, p, sizeof(a->s_addr)); 297 return 0; 298 } 299 300 static int 301 get_option_uint32(struct dhcpcd_ctx *ctx, 302 uint32_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option) 303 { 304 const uint8_t *p; 305 size_t len; 306 uint32_t d; 307 308 p = get_option(ctx, bootp, bootp_len, option, &len); 309 if (!p || len < (ssize_t)sizeof(d)) 310 return -1; 311 memcpy(&d, p, sizeof(d)); 312 if (i) 313 *i = ntohl(d); 314 return 0; 315 } 316 317 static int 318 get_option_uint16(struct dhcpcd_ctx *ctx, 319 uint16_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option) 320 { 321 const uint8_t *p; 322 size_t len; 323 uint16_t d; 324 325 p = get_option(ctx, bootp, bootp_len, option, &len); 326 if (!p || len < (ssize_t)sizeof(d)) 327 return -1; 328 memcpy(&d, p, sizeof(d)); 329 if (i) 330 *i = ntohs(d); 331 return 0; 332 } 333 334 static int 335 get_option_uint8(struct dhcpcd_ctx *ctx, 336 uint8_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option) 337 { 338 const uint8_t *p; 339 size_t len; 340 341 p = get_option(ctx, bootp, bootp_len, option, &len); 342 if (!p || len < (ssize_t)sizeof(*p)) 343 return -1; 344 if (i) 345 *i = *(p); 346 return 0; 347 } 348 349 ssize_t 350 print_rfc3442(FILE *fp, const uint8_t *data, size_t data_len) 351 { 352 const uint8_t *p = data, *e; 353 size_t ocets; 354 uint8_t cidr; 355 struct in_addr addr; 356 357 /* Minimum is 5 -first is CIDR and a router length of 4 */ 358 if (data_len < 5) { 359 errno = EINVAL; 360 return -1; 361 } 362 363 e = p + data_len; 364 while (p < e) { 365 if (p != data) { 366 if (fputc(' ', fp) == EOF) 367 return -1; 368 } 369 cidr = *p++; 370 if (cidr > 32) { 371 errno = EINVAL; 372 return -1; 373 } 374 ocets = (size_t)(cidr + 7) / NBBY; 375 if (p + 4 + ocets > e) { 376 errno = ERANGE; 377 return -1; 378 } 379 /* If we have ocets then we have a destination and netmask */ 380 addr.s_addr = 0; 381 if (ocets > 0) { 382 memcpy(&addr.s_addr, p, ocets); 383 p += ocets; 384 } 385 if (fprintf(fp, "%s/%d", inet_ntoa(addr), cidr) == -1) 386 return -1; 387 388 /* Finally, snag the router */ 389 memcpy(&addr.s_addr, p, 4); 390 p += 4; 391 if (fprintf(fp, " %s", inet_ntoa(addr)) == -1) 392 return -1; 393 } 394 395 if (fputc('\0', fp) == EOF) 396 return -1; 397 return 1; 398 } 399 400 static int 401 decode_rfc3442_rt(rb_tree_t *routes, struct interface *ifp, 402 const uint8_t *data, size_t dl, const struct bootp *bootp) 403 { 404 const uint8_t *p = data; 405 const uint8_t *e; 406 uint8_t cidr; 407 size_t ocets; 408 struct rt *rt = NULL; 409 struct in_addr dest, netmask, gateway; 410 int n; 411 412 /* Minimum is 5 -first is CIDR and a router length of 4 */ 413 if (dl < 5) { 414 errno = EINVAL; 415 return -1; 416 } 417 418 n = 0; 419 e = p + dl; 420 while (p < e) { 421 cidr = *p++; 422 if (cidr > 32) { 423 errno = EINVAL; 424 return -1; 425 } 426 427 ocets = (size_t)(cidr + 7) / NBBY; 428 if (p + 4 + ocets > e) { 429 errno = ERANGE; 430 return -1; 431 } 432 433 if ((rt = rt_new(ifp)) == NULL) 434 return -1; 435 436 /* If we have ocets then we have a destination and netmask */ 437 dest.s_addr = 0; 438 if (ocets > 0) { 439 memcpy(&dest.s_addr, p, ocets); 440 p += ocets; 441 netmask.s_addr = htonl(~0U << (32 - cidr)); 442 } else 443 netmask.s_addr = 0; 444 445 /* Finally, snag the router */ 446 memcpy(&gateway.s_addr, p, 4); 447 p += 4; 448 449 /* An on-link host route is normally set by having the 450 * gateway match the destination or assigned address */ 451 if (gateway.s_addr == dest.s_addr || 452 (gateway.s_addr == bootp->yiaddr || 453 gateway.s_addr == bootp->ciaddr)) 454 { 455 gateway.s_addr = INADDR_ANY; 456 netmask.s_addr = INADDR_BROADCAST; 457 } 458 if (netmask.s_addr == INADDR_BROADCAST) 459 rt->rt_flags = RTF_HOST; 460 461 sa_in_init(&rt->rt_dest, &dest); 462 sa_in_init(&rt->rt_netmask, &netmask); 463 sa_in_init(&rt->rt_gateway, &gateway); 464 if (rt_proto_add(routes, rt)) 465 n = 1; 466 } 467 return n; 468 } 469 470 ssize_t 471 print_rfc3361(FILE *fp, const uint8_t *data, size_t dl) 472 { 473 uint8_t enc; 474 char sip[NS_MAXDNAME]; 475 struct in_addr addr; 476 477 if (dl < 2) { 478 errno = EINVAL; 479 return 0; 480 } 481 482 enc = *data++; 483 dl--; 484 switch (enc) { 485 case 0: 486 if (decode_rfc1035(sip, sizeof(sip), data, dl) == -1) 487 return -1; 488 if (efprintf(fp, "%s", sip) == -1) 489 return -1; 490 break; 491 case 1: 492 if (dl % 4 != 0) { 493 errno = EINVAL; 494 break; 495 } 496 addr.s_addr = INADDR_BROADCAST; 497 for (; 498 dl != 0; 499 data += sizeof(addr.s_addr), dl -= sizeof(addr.s_addr)) 500 { 501 memcpy(&addr.s_addr, data, sizeof(addr.s_addr)); 502 if (fprintf(fp, "%s", inet_ntoa(addr)) == -1) 503 return -1; 504 if (dl != 0) { 505 if (fputc(' ', fp) == EOF) 506 return -1; 507 } 508 } 509 if (fputc('\0', fp) == EOF) 510 return -1; 511 break; 512 default: 513 errno = EINVAL; 514 return 0; 515 } 516 517 return 1; 518 } 519 520 static char * 521 get_option_string(struct dhcpcd_ctx *ctx, 522 const struct bootp *bootp, size_t bootp_len, uint8_t option) 523 { 524 size_t len; 525 const uint8_t *p; 526 char *s; 527 528 p = get_option(ctx, bootp, bootp_len, option, &len); 529 if (!p || len == 0 || *p == '\0') 530 return NULL; 531 532 s = malloc(sizeof(char) * (len + 1)); 533 if (s) { 534 memcpy(s, p, len); 535 s[len] = '\0'; 536 } 537 return s; 538 } 539 540 /* This calculates the netmask that we should use for static routes. 541 * This IS different from the calculation used to calculate the netmask 542 * for an interface address. */ 543 static uint32_t 544 route_netmask(uint32_t ip_in) 545 { 546 /* used to be unsigned long - check if error */ 547 uint32_t p = ntohl(ip_in); 548 uint32_t t; 549 550 if (IN_CLASSA(p)) 551 t = ~IN_CLASSA_NET; 552 else { 553 if (IN_CLASSB(p)) 554 t = ~IN_CLASSB_NET; 555 else { 556 if (IN_CLASSC(p)) 557 t = ~IN_CLASSC_NET; 558 else 559 t = 0; 560 } 561 } 562 563 while (t & p) 564 t >>= 1; 565 566 return (htonl(~t)); 567 } 568 569 /* We need to obey routing options. 570 * If we have a CSR then we only use that. 571 * Otherwise we add static routes and then routers. */ 572 static int 573 get_option_routes(rb_tree_t *routes, struct interface *ifp, 574 const struct bootp *bootp, size_t bootp_len) 575 { 576 struct if_options *ifo = ifp->options; 577 const uint8_t *p; 578 const uint8_t *e; 579 struct rt *rt = NULL; 580 struct in_addr dest, netmask, gateway; 581 size_t len; 582 const char *csr = ""; 583 int n; 584 585 /* If we have CSR's then we MUST use these only */ 586 if (!has_option_mask(ifo->nomask, DHO_CSR)) 587 p = get_option(ifp->ctx, bootp, bootp_len, DHO_CSR, &len); 588 else 589 p = NULL; 590 /* Check for crappy MS option */ 591 if (!p && !has_option_mask(ifo->nomask, DHO_MSCSR)) { 592 p = get_option(ifp->ctx, bootp, bootp_len, DHO_MSCSR, &len); 593 if (p) 594 csr = "MS "; 595 } 596 if (p && (n = decode_rfc3442_rt(routes, ifp, p, len, bootp)) != -1) { 597 const struct dhcp_state *state; 598 599 state = D_CSTATE(ifp); 600 if (!(ifo->options & DHCPCD_CSR_WARNED) && 601 !(state->added & STATE_FAKE)) 602 { 603 logdebugx("%s: using %sClassless Static Routes", 604 ifp->name, csr); 605 ifo->options |= DHCPCD_CSR_WARNED; 606 } 607 return n; 608 } 609 610 n = 0; 611 /* OK, get our static routes first. */ 612 if (!has_option_mask(ifo->nomask, DHO_STATICROUTE)) 613 p = get_option(ifp->ctx, bootp, bootp_len, 614 DHO_STATICROUTE, &len); 615 else 616 p = NULL; 617 /* RFC 2131 Section 5.8 states length MUST be in multiples of 8 */ 618 if (p && len % 8 == 0) { 619 e = p + len; 620 while (p < e) { 621 memcpy(&dest.s_addr, p, sizeof(dest.s_addr)); 622 p += 4; 623 memcpy(&gateway.s_addr, p, sizeof(gateway.s_addr)); 624 p += 4; 625 /* RFC 2131 Section 5.8 states default route is 626 * illegal */ 627 if (gateway.s_addr == INADDR_ANY) 628 continue; 629 if ((rt = rt_new(ifp)) == NULL) 630 return -1; 631 632 /* A on-link host route is normally set by having the 633 * gateway match the destination or assigned address */ 634 if (gateway.s_addr == dest.s_addr || 635 (gateway.s_addr == bootp->yiaddr || 636 gateway.s_addr == bootp->ciaddr)) 637 { 638 gateway.s_addr = INADDR_ANY; 639 netmask.s_addr = INADDR_BROADCAST; 640 } else 641 netmask.s_addr = route_netmask(dest.s_addr); 642 if (netmask.s_addr == INADDR_BROADCAST) 643 rt->rt_flags = RTF_HOST; 644 645 sa_in_init(&rt->rt_dest, &dest); 646 sa_in_init(&rt->rt_netmask, &netmask); 647 sa_in_init(&rt->rt_gateway, &gateway); 648 if (rt_proto_add(routes, rt)) 649 n++; 650 } 651 } 652 653 /* Now grab our routers */ 654 if (!has_option_mask(ifo->nomask, DHO_ROUTER)) 655 p = get_option(ifp->ctx, bootp, bootp_len, DHO_ROUTER, &len); 656 else 657 p = NULL; 658 if (p && len % 4 == 0) { 659 e = p + len; 660 dest.s_addr = INADDR_ANY; 661 netmask.s_addr = INADDR_ANY; 662 while (p < e) { 663 if ((rt = rt_new(ifp)) == NULL) 664 return -1; 665 memcpy(&gateway.s_addr, p, sizeof(gateway.s_addr)); 666 p += 4; 667 sa_in_init(&rt->rt_dest, &dest); 668 sa_in_init(&rt->rt_netmask, &netmask); 669 sa_in_init(&rt->rt_gateway, &gateway); 670 if (rt_proto_add(routes, rt)) 671 n++; 672 } 673 } 674 675 return n; 676 } 677 678 uint16_t 679 dhcp_get_mtu(const struct interface *ifp) 680 { 681 const struct dhcp_state *state; 682 uint16_t mtu; 683 684 if (ifp->options->mtu) 685 return (uint16_t)ifp->options->mtu; 686 mtu = 0; /* bogus gcc warning */ 687 if ((state = D_CSTATE(ifp)) == NULL || 688 has_option_mask(ifp->options->nomask, DHO_MTU) || 689 get_option_uint16(ifp->ctx, &mtu, 690 state->new, state->new_len, DHO_MTU) == -1) 691 return 0; 692 return mtu; 693 } 694 695 /* Grab our routers from the DHCP message and apply any MTU value 696 * the message contains */ 697 int 698 dhcp_get_routes(rb_tree_t *routes, struct interface *ifp) 699 { 700 const struct dhcp_state *state; 701 702 if ((state = D_CSTATE(ifp)) == NULL || !(state->added & STATE_ADDED)) 703 return 0; 704 return get_option_routes(routes, ifp, state->new, state->new_len); 705 } 706 707 /* Assumes DHCP options */ 708 static int 709 dhcp_message_add_addr(struct bootp *bootp, 710 uint8_t type, struct in_addr addr) 711 { 712 uint8_t *p; 713 size_t len; 714 715 p = bootp->vend; 716 while (*p != DHO_END) { 717 p++; 718 p += *p + 1; 719 } 720 721 len = (size_t)(p - bootp->vend); 722 if (len + 6 > sizeof(bootp->vend)) { 723 errno = ENOMEM; 724 return -1; 725 } 726 727 *p++ = type; 728 *p++ = 4; 729 memcpy(p, &addr.s_addr, 4); 730 p += 4; 731 *p = DHO_END; 732 return 0; 733 } 734 735 static ssize_t 736 make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) 737 { 738 struct bootp *bootp; 739 uint8_t *lp, *p, *e; 740 uint8_t *n_params = NULL; 741 uint32_t ul; 742 uint16_t sz; 743 size_t len, i; 744 const struct dhcp_opt *opt; 745 struct if_options *ifo = ifp->options; 746 const struct dhcp_state *state = D_CSTATE(ifp); 747 const struct dhcp_lease *lease = &state->lease; 748 char hbuf[HOSTNAME_MAX_LEN + 1]; 749 const char *hostname; 750 const struct vivco *vivco; 751 int mtu; 752 #ifdef AUTH 753 uint8_t *auth, auth_len; 754 #endif 755 756 if ((mtu = if_getmtu(ifp)) == -1) 757 logerr("%s: if_getmtu", ifp->name); 758 else if (mtu < MTU_MIN) { 759 if (if_setmtu(ifp, MTU_MIN) == -1) 760 logerr("%s: if_setmtu", ifp->name); 761 mtu = MTU_MIN; 762 } 763 764 if (ifo->options & DHCPCD_BOOTP) 765 bootp = calloc(1, sizeof (*bootp)); 766 else 767 /* Make the maximal message we could send */ 768 bootp = calloc(1, (size_t)(mtu - IP_UDP_SIZE)); 769 770 if (bootp == NULL) 771 return -1; 772 *bootpm = bootp; 773 774 if (state->addr != NULL && 775 (type == DHCP_INFORM || type == DHCP_RELEASE || 776 (type == DHCP_REQUEST && 777 state->addr->mask.s_addr == lease->mask.s_addr && 778 (state->new == NULL || IS_DHCP(state->new)) && 779 !(state->added & STATE_FAKE)))) 780 bootp->ciaddr = state->addr->addr.s_addr; 781 782 bootp->op = BOOTREQUEST; 783 bootp->htype = (uint8_t)ifp->family; 784 switch (ifp->family) { 785 case ARPHRD_ETHER: 786 case ARPHRD_IEEE802: 787 bootp->hlen = (uint8_t)ifp->hwlen; 788 memcpy(&bootp->chaddr, &ifp->hwaddr, ifp->hwlen); 789 break; 790 } 791 792 if (ifo->options & DHCPCD_BROADCAST && 793 bootp->ciaddr == 0 && 794 type != DHCP_DECLINE && 795 type != DHCP_RELEASE) 796 bootp->flags = htons(BROADCAST_FLAG); 797 798 if (type != DHCP_DECLINE && type != DHCP_RELEASE) { 799 struct timespec tv; 800 801 clock_gettime(CLOCK_MONOTONIC, &tv); 802 timespecsub(&tv, &state->started, &tv); 803 if (tv.tv_sec < 0 || tv.tv_sec > (time_t)UINT16_MAX) 804 bootp->secs = htons((uint16_t)UINT16_MAX); 805 else 806 bootp->secs = htons((uint16_t)tv.tv_sec); 807 } 808 809 bootp->xid = htonl(state->xid); 810 811 if (ifo->options & DHCPCD_BOOTP) 812 return sizeof(*bootp); 813 814 p = bootp->vend; 815 e = (uint8_t *)bootp + (mtu - IP_UDP_SIZE) - 1; /* -1 for DHO_END */ 816 817 ul = htonl(MAGIC_COOKIE); 818 memcpy(p, &ul, sizeof(ul)); 819 p += sizeof(ul); 820 821 *p++ = DHO_MESSAGETYPE; 822 *p++ = 1; 823 *p++ = type; 824 825 #define AREA_LEFT (size_t)(e - p) 826 #define AREA_FIT(s) if ((s) > AREA_LEFT) goto toobig 827 #define AREA_CHECK(s) if ((s) + 2UL > AREA_LEFT) goto toobig 828 #define PUT_ADDR(o, a) do { \ 829 AREA_CHECK(4); \ 830 *p++ = (o); \ 831 *p++ = 4; \ 832 memcpy(p, &(a)->s_addr, 4); \ 833 p += 4; \ 834 } while (0 /* CONSTCOND */) 835 836 if (state->clientid) { 837 AREA_CHECK(state->clientid[0]); 838 *p++ = DHO_CLIENTID; 839 memcpy(p, state->clientid, (size_t)state->clientid[0] + 1); 840 p += state->clientid[0] + 1; 841 } 842 843 if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) { 844 if (type == DHCP_DECLINE || 845 (type == DHCP_REQUEST && 846 (state->addr == NULL || 847 state->added & STATE_FAKE || 848 lease->addr.s_addr != state->addr->addr.s_addr))) 849 { 850 PUT_ADDR(DHO_IPADDRESS, &lease->addr); 851 if (lease->server.s_addr) 852 PUT_ADDR(DHO_SERVERID, &lease->server); 853 } 854 855 if (type == DHCP_RELEASE) { 856 if (lease->server.s_addr) 857 PUT_ADDR(DHO_SERVERID, &lease->server); 858 } 859 } 860 861 if (type == DHCP_DECLINE) { 862 len = strlen(DAD); 863 if (len > AREA_LEFT) { 864 *p++ = DHO_MESSAGE; 865 *p++ = (uint8_t)len; 866 memcpy(p, DAD, len); 867 p += len; 868 } 869 } 870 871 if (type == DHCP_DISCOVER && 872 !(ifp->ctx->options & DHCPCD_TEST) && 873 has_option_mask(ifo->requestmask, DHO_RAPIDCOMMIT)) 874 { 875 /* RFC 4039 Section 3 */ 876 AREA_CHECK(0); 877 *p++ = DHO_RAPIDCOMMIT; 878 *p++ = 0; 879 } 880 881 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_REQUEST) 882 PUT_ADDR(DHO_IPADDRESS, &ifo->req_addr); 883 884 /* RFC 2563 Auto Configure */ 885 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_IPV4LL) { 886 AREA_CHECK(1); 887 *p++ = DHO_AUTOCONFIGURE; 888 *p++ = 1; 889 *p++ = 1; 890 } 891 892 if (type == DHCP_DISCOVER || 893 type == DHCP_INFORM || 894 type == DHCP_REQUEST) 895 { 896 if (mtu != -1) { 897 AREA_CHECK(2); 898 *p++ = DHO_MAXMESSAGESIZE; 899 *p++ = 2; 900 sz = htons((uint16_t)(mtu - IP_UDP_SIZE)); 901 memcpy(p, &sz, 2); 902 p += 2; 903 } 904 905 if (ifo->userclass[0]) { 906 AREA_CHECK(ifo->userclass[0]); 907 *p++ = DHO_USERCLASS; 908 memcpy(p, ifo->userclass, 909 (size_t)ifo->userclass[0] + 1); 910 p += ifo->userclass[0] + 1; 911 } 912 913 if (ifo->vendorclassid[0]) { 914 AREA_CHECK(ifo->vendorclassid[0]); 915 *p++ = DHO_VENDORCLASSID; 916 memcpy(p, ifo->vendorclassid, 917 (size_t)ifo->vendorclassid[0] + 1); 918 p += ifo->vendorclassid[0] + 1; 919 } 920 921 if (ifo->mudurl[0]) { 922 AREA_CHECK(ifo->mudurl[0]); 923 *p++ = DHO_MUDURL; 924 memcpy(p, ifo->mudurl, (size_t)ifo->mudurl[0] + 1); 925 p += ifo->mudurl[0] + 1; 926 } 927 928 if (type != DHCP_INFORM) { 929 if (ifo->leasetime != 0) { 930 AREA_CHECK(4); 931 *p++ = DHO_LEASETIME; 932 *p++ = 4; 933 ul = htonl(ifo->leasetime); 934 memcpy(p, &ul, 4); 935 p += 4; 936 } 937 } 938 939 hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo); 940 941 /* 942 * RFC4702 3.1 States that if we send the Client FQDN option 943 * then we MUST NOT also send the Host Name option. 944 * Technically we could, but that is not RFC conformant and 945 * also seems to break some DHCP server implemetations such as 946 * Windows. On the other hand, ISC dhcpd is just as non RFC 947 * conformant by not accepting a partially qualified FQDN. 948 */ 949 if (ifo->fqdn != FQDN_DISABLE) { 950 /* IETF DHC-FQDN option (81), RFC4702 */ 951 i = 3; 952 if (hostname) 953 i += encode_rfc1035(hostname, NULL); 954 AREA_CHECK(i); 955 *p++ = DHO_FQDN; 956 *p++ = (uint8_t)i; 957 /* 958 * Flags: 0000NEOS 959 * S: 1 => Client requests Server to update 960 * a RR in DNS as well as PTR 961 * O: 1 => Server indicates to client that 962 * DNS has been updated 963 * E: 1 => Name data is DNS format 964 * N: 1 => Client requests Server to not 965 * update DNS 966 */ 967 if (hostname) 968 *p++ = (uint8_t)((ifo->fqdn & 0x09) | 0x04); 969 else 970 *p++ = (FQDN_NONE & 0x09) | 0x04; 971 *p++ = 0; /* from server for PTR RR */ 972 *p++ = 0; /* from server for A RR if S=1 */ 973 if (hostname) { 974 i = encode_rfc1035(hostname, p); 975 p += i; 976 } 977 } else if (ifo->options & DHCPCD_HOSTNAME && hostname) { 978 len = strlen(hostname); 979 AREA_CHECK(len); 980 *p++ = DHO_HOSTNAME; 981 *p++ = (uint8_t)len; 982 memcpy(p, hostname, len); 983 p += len; 984 } 985 986 /* vendor is already encoded correctly, so just add it */ 987 if (ifo->vendor[0]) { 988 AREA_CHECK(ifo->vendor[0]); 989 *p++ = DHO_VENDOR; 990 memcpy(p, ifo->vendor, (size_t)ifo->vendor[0] + 1); 991 p += ifo->vendor[0] + 1; 992 } 993 994 #ifdef AUTH 995 if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) != 996 DHCPCD_AUTH_SENDREQUIRE && 997 !has_option_mask(ifo->nomask, DHO_FORCERENEW_NONCE)) 998 { 999 /* We support HMAC-MD5 */ 1000 AREA_CHECK(1); 1001 *p++ = DHO_FORCERENEW_NONCE; 1002 *p++ = 1; 1003 *p++ = AUTH_ALG_HMAC_MD5; 1004 } 1005 #endif 1006 1007 if (ifo->vivco_len) { 1008 AREA_CHECK(sizeof(ul)); 1009 *p++ = DHO_VIVCO; 1010 lp = p++; 1011 *lp = sizeof(ul); 1012 ul = htonl(ifo->vivco_en); 1013 memcpy(p, &ul, sizeof(ul)); 1014 p += sizeof(ul); 1015 for (i = 0, vivco = ifo->vivco; 1016 i < ifo->vivco_len; 1017 i++, vivco++) 1018 { 1019 AREA_FIT(vivco->len); 1020 if (vivco->len + 2 + *lp > 255) { 1021 logerrx("%s: VIVCO option too big", 1022 ifp->name); 1023 free(bootp); 1024 return -1; 1025 } 1026 *p++ = (uint8_t)vivco->len; 1027 memcpy(p, vivco->data, vivco->len); 1028 p += vivco->len; 1029 *lp = (uint8_t)(*lp + vivco->len + 1); 1030 } 1031 } 1032 1033 AREA_CHECK(0); 1034 *p++ = DHO_PARAMETERREQUESTLIST; 1035 n_params = p; 1036 *p++ = 0; 1037 for (i = 0, opt = ifp->ctx->dhcp_opts; 1038 i < ifp->ctx->dhcp_opts_len; 1039 i++, opt++) 1040 { 1041 if (!DHC_REQOPT(opt, ifo->requestmask, ifo->nomask)) 1042 continue; 1043 if (type == DHCP_INFORM && 1044 (opt->option == DHO_RENEWALTIME || 1045 opt->option == DHO_REBINDTIME)) 1046 continue; 1047 AREA_FIT(1); 1048 *p++ = (uint8_t)opt->option; 1049 } 1050 for (i = 0, opt = ifo->dhcp_override; 1051 i < ifo->dhcp_override_len; 1052 i++, opt++) 1053 { 1054 /* Check if added above */ 1055 for (lp = n_params + 1; lp < p; lp++) 1056 if (*lp == (uint8_t)opt->option) 1057 break; 1058 if (lp < p) 1059 continue; 1060 if (!DHC_REQOPT(opt, ifo->requestmask, ifo->nomask)) 1061 continue; 1062 if (type == DHCP_INFORM && 1063 (opt->option == DHO_RENEWALTIME || 1064 opt->option == DHO_REBINDTIME)) 1065 continue; 1066 AREA_FIT(1); 1067 *p++ = (uint8_t)opt->option; 1068 } 1069 *n_params = (uint8_t)(p - n_params - 1); 1070 } 1071 1072 #ifdef AUTH 1073 auth = NULL; /* appease GCC */ 1074 auth_len = 0; 1075 if (ifo->auth.options & DHCPCD_AUTH_SEND) { 1076 ssize_t alen = dhcp_auth_encode(&ifo->auth, 1077 state->auth.token, 1078 NULL, 0, 4, type, NULL, 0); 1079 if (alen != -1 && alen > UINT8_MAX) { 1080 errno = ERANGE; 1081 alen = -1; 1082 } 1083 if (alen == -1) 1084 logerr("%s: dhcp_auth_encode", ifp->name); 1085 else if (alen != 0) { 1086 auth_len = (uint8_t)alen; 1087 AREA_CHECK(auth_len); 1088 *p++ = DHO_AUTHENTICATION; 1089 *p++ = auth_len; 1090 auth = p; 1091 p += auth_len; 1092 } 1093 } 1094 #endif 1095 1096 *p++ = DHO_END; 1097 len = (size_t)(p - (uint8_t *)bootp); 1098 1099 /* Pad out to the BOOTP message length. 1100 * Even if we send a DHCP packet with a variable length vendor area, 1101 * some servers / relay agents don't like packets smaller than 1102 * a BOOTP message which is fine because that's stipulated 1103 * in RFC1542 section 2.1. */ 1104 while (len < sizeof(*bootp)) { 1105 *p++ = DHO_PAD; 1106 len++; 1107 } 1108 1109 #ifdef AUTH 1110 if (ifo->auth.options & DHCPCD_AUTH_SEND && auth_len != 0) 1111 dhcp_auth_encode(&ifo->auth, state->auth.token, 1112 (uint8_t *)bootp, len, 4, type, auth, auth_len); 1113 #endif 1114 1115 return (ssize_t)len; 1116 1117 toobig: 1118 logerrx("%s: DHCP message too big", ifp->name); 1119 free(bootp); 1120 return -1; 1121 } 1122 1123 static ssize_t 1124 write_lease(const struct interface *ifp, const struct bootp *bootp, size_t len) 1125 { 1126 int fd; 1127 ssize_t bytes; 1128 const struct dhcp_state *state = D_CSTATE(ifp); 1129 1130 logdebugx("%s: writing lease `%s'", ifp->name, state->leasefile); 1131 1132 fd = open(state->leasefile, O_WRONLY | O_CREAT | O_TRUNC, 0644); 1133 if (fd == -1) 1134 return -1; 1135 bytes = write(fd, bootp, len); 1136 close(fd); 1137 return bytes; 1138 } 1139 1140 static size_t 1141 read_lease(struct interface *ifp, struct bootp **bootp) 1142 { 1143 int fd; 1144 bool fd_opened; 1145 struct dhcp_state *state = D_STATE(ifp); 1146 struct bootp *lease; 1147 size_t bytes; 1148 uint8_t type; 1149 #ifdef AUTH 1150 const uint8_t *auth; 1151 size_t auth_len; 1152 #endif 1153 1154 /* Safety */ 1155 *bootp = NULL; 1156 1157 if (state->leasefile[0] == '\0') { 1158 fd = fileno(stdin); 1159 fd_opened = false; 1160 } else { 1161 fd = open(state->leasefile, O_RDONLY); 1162 fd_opened = true; 1163 } 1164 if (fd == -1) { 1165 if (errno != ENOENT) 1166 logerr("%s: open `%s'", 1167 ifp->name, state->leasefile); 1168 return 0; 1169 } 1170 if (state->leasefile[0] == '\0') 1171 logdebugx("reading standard input"); 1172 else 1173 logdebugx("%s: reading lease `%s'", 1174 ifp->name, state->leasefile); 1175 1176 bytes = dhcp_read_lease_fd(fd, (void **)&lease); 1177 if (fd_opened) 1178 close(fd); 1179 if (bytes == 0) 1180 return 0; 1181 1182 /* Ensure the packet is at lease BOOTP sized 1183 * with a vendor area of 4 octets 1184 * (it should be more, and our read packet enforces this so this 1185 * code should not be needed, but of course people could 1186 * scribble whatever in the stored lease file. */ 1187 if (bytes < DHCP_MIN_LEN) { 1188 free(lease); 1189 logerrx("%s: %s: truncated lease", ifp->name, __func__); 1190 return 0; 1191 } 1192 1193 if (ifp->ctx->options & DHCPCD_DUMPLEASE) 1194 goto out; 1195 1196 /* We may have found a BOOTP server */ 1197 if (get_option_uint8(ifp->ctx, &type, (struct bootp *)lease, bytes, 1198 DHO_MESSAGETYPE) == -1) 1199 type = 0; 1200 1201 #ifdef AUTH 1202 /* Authenticate the message */ 1203 auth = get_option(ifp->ctx, (struct bootp *)lease, bytes, 1204 DHO_AUTHENTICATION, &auth_len); 1205 if (auth) { 1206 if (dhcp_auth_validate(&state->auth, &ifp->options->auth, 1207 lease, bytes, 4, type, auth, auth_len) == NULL) 1208 { 1209 logerr("%s: authentication failed", ifp->name); 1210 free(lease); 1211 return 0; 1212 } 1213 if (state->auth.token) 1214 logdebugx("%s: validated using 0x%08" PRIu32, 1215 ifp->name, state->auth.token->secretid); 1216 else 1217 logdebugx("%s: accepted reconfigure key", ifp->name); 1218 } else if ((ifp->options->auth.options & DHCPCD_AUTH_SENDREQUIRE) == 1219 DHCPCD_AUTH_SENDREQUIRE) 1220 { 1221 logerrx("%s: authentication now required", ifp->name); 1222 free(lease); 1223 return 0; 1224 } 1225 #endif 1226 1227 out: 1228 *bootp = (struct bootp *)lease; 1229 return bytes; 1230 } 1231 1232 static const struct dhcp_opt * 1233 dhcp_getoverride(const struct if_options *ifo, unsigned int o) 1234 { 1235 size_t i; 1236 const struct dhcp_opt *opt; 1237 1238 for (i = 0, opt = ifo->dhcp_override; 1239 i < ifo->dhcp_override_len; 1240 i++, opt++) 1241 { 1242 if (opt->option == o) 1243 return opt; 1244 } 1245 return NULL; 1246 } 1247 1248 static const uint8_t * 1249 dhcp_getoption(struct dhcpcd_ctx *ctx, 1250 size_t *os, unsigned int *code, size_t *len, 1251 const uint8_t *od, size_t ol, struct dhcp_opt **oopt) 1252 { 1253 size_t i; 1254 struct dhcp_opt *opt; 1255 1256 if (od) { 1257 if (ol < 2) { 1258 errno = EINVAL; 1259 return NULL; 1260 } 1261 *os = 2; /* code + len */ 1262 *code = (unsigned int)*od++; 1263 *len = (size_t)*od++; 1264 if (*len > ol - *os) { 1265 errno = ERANGE; 1266 return NULL; 1267 } 1268 } 1269 1270 *oopt = NULL; 1271 for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++) { 1272 if (opt->option == *code) { 1273 *oopt = opt; 1274 break; 1275 } 1276 } 1277 1278 return od; 1279 } 1280 1281 ssize_t 1282 dhcp_env(FILE *fenv, const char *prefix, const struct interface *ifp, 1283 const struct bootp *bootp, size_t bootp_len) 1284 { 1285 const struct if_options *ifo; 1286 const uint8_t *p; 1287 struct in_addr addr; 1288 struct in_addr net; 1289 struct in_addr brd; 1290 struct dhcp_opt *opt, *vo; 1291 size_t i, pl; 1292 char safe[(BOOTP_FILE_LEN * 4) + 1]; 1293 uint8_t overl = 0; 1294 uint32_t en; 1295 1296 ifo = ifp->options; 1297 if (get_option_uint8(ifp->ctx, &overl, bootp, bootp_len, 1298 DHO_OPTSOVERLOADED) == -1) 1299 overl = 0; 1300 1301 if (bootp->yiaddr || bootp->ciaddr) { 1302 /* Set some useful variables that we derive from the DHCP 1303 * message but are not necessarily in the options */ 1304 addr.s_addr = bootp->yiaddr ? bootp->yiaddr : bootp->ciaddr; 1305 if (efprintf(fenv, "%s_ip_address=%s", 1306 prefix, inet_ntoa(addr)) == -1) 1307 return -1; 1308 if (get_option_addr(ifp->ctx, &net, 1309 bootp, bootp_len, DHO_SUBNETMASK) == -1) { 1310 net.s_addr = ipv4_getnetmask(addr.s_addr); 1311 if (efprintf(fenv, "%s_subnet_mask=%s", 1312 prefix, inet_ntoa(net)) == -1) 1313 return -1; 1314 } 1315 if (efprintf(fenv, "%s_subnet_cidr=%d", 1316 prefix, inet_ntocidr(net))== -1) 1317 return -1; 1318 if (get_option_addr(ifp->ctx, &brd, 1319 bootp, bootp_len, DHO_BROADCAST) == -1) 1320 { 1321 brd.s_addr = addr.s_addr | ~net.s_addr; 1322 if (efprintf(fenv, "%s_broadcast_address=%s", 1323 prefix, inet_ntoa(brd)) == -1) 1324 return -1; 1325 } 1326 addr.s_addr = bootp->yiaddr & net.s_addr; 1327 if (efprintf(fenv, "%s_network_number=%s", 1328 prefix, inet_ntoa(addr)) == -1) 1329 return -1; 1330 } 1331 1332 if (*bootp->file && !(overl & 1)) { 1333 print_string(safe, sizeof(safe), OT_STRING, 1334 bootp->file, sizeof(bootp->file)); 1335 if (efprintf(fenv, "%s_filename=%s", prefix, safe) == -1) 1336 return -1; 1337 } 1338 if (*bootp->sname && !(overl & 2)) { 1339 print_string(safe, sizeof(safe), OT_STRING | OT_DOMAIN, 1340 bootp->sname, sizeof(bootp->sname)); 1341 if (efprintf(fenv, "%s_server_name=%s", prefix, safe) == -1) 1342 return -1; 1343 } 1344 1345 /* Zero our indexes */ 1346 for (i = 0, opt = ifp->ctx->dhcp_opts; 1347 i < ifp->ctx->dhcp_opts_len; 1348 i++, opt++) 1349 dhcp_zero_index(opt); 1350 for (i = 0, opt = ifp->options->dhcp_override; 1351 i < ifp->options->dhcp_override_len; 1352 i++, opt++) 1353 dhcp_zero_index(opt); 1354 for (i = 0, opt = ifp->ctx->vivso; 1355 i < ifp->ctx->vivso_len; 1356 i++, opt++) 1357 dhcp_zero_index(opt); 1358 1359 for (i = 0, opt = ifp->ctx->dhcp_opts; 1360 i < ifp->ctx->dhcp_opts_len; 1361 i++, opt++) 1362 { 1363 if (has_option_mask(ifo->nomask, opt->option)) 1364 continue; 1365 if (dhcp_getoverride(ifo, opt->option)) 1366 continue; 1367 p = get_option(ifp->ctx, bootp, bootp_len, opt->option, &pl); 1368 if (p == NULL) 1369 continue; 1370 dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, 1371 opt, dhcp_getoption, p, pl); 1372 1373 if (opt->option != DHO_VIVSO || pl <= (int)sizeof(uint32_t)) 1374 continue; 1375 memcpy(&en, p, sizeof(en)); 1376 en = ntohl(en); 1377 vo = vivso_find(en, ifp); 1378 if (vo == NULL) 1379 continue; 1380 /* Skip over en + total size */ 1381 p += sizeof(en) + 1; 1382 pl -= sizeof(en) + 1; 1383 dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, 1384 vo, dhcp_getoption, p, pl); 1385 } 1386 1387 for (i = 0, opt = ifo->dhcp_override; 1388 i < ifo->dhcp_override_len; 1389 i++, opt++) 1390 { 1391 if (has_option_mask(ifo->nomask, opt->option)) 1392 continue; 1393 p = get_option(ifp->ctx, bootp, bootp_len, opt->option, &pl); 1394 if (p == NULL) 1395 continue; 1396 dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, 1397 opt, dhcp_getoption, p, pl); 1398 } 1399 1400 return 1; 1401 } 1402 1403 static void 1404 get_lease(struct interface *ifp, 1405 struct dhcp_lease *lease, const struct bootp *bootp, size_t len) 1406 { 1407 struct dhcpcd_ctx *ctx; 1408 1409 assert(bootp != NULL); 1410 1411 memcpy(&lease->cookie, bootp->vend, sizeof(lease->cookie)); 1412 /* BOOTP does not set yiaddr for replies when ciaddr is set. */ 1413 lease->addr.s_addr = bootp->yiaddr ? bootp->yiaddr : bootp->ciaddr; 1414 ctx = ifp->ctx; 1415 if (ifp->options->options & (DHCPCD_STATIC | DHCPCD_INFORM)) { 1416 if (ifp->options->req_addr.s_addr != INADDR_ANY) { 1417 lease->mask = ifp->options->req_mask; 1418 if (ifp->options->req_brd.s_addr != INADDR_ANY) 1419 lease->brd = ifp->options->req_brd; 1420 else 1421 lease->brd.s_addr = 1422 lease->addr.s_addr | ~lease->mask.s_addr; 1423 } else { 1424 const struct ipv4_addr *ia; 1425 1426 ia = ipv4_iffindaddr(ifp, &lease->addr, NULL); 1427 assert(ia != NULL); 1428 lease->mask = ia->mask; 1429 lease->brd = ia->brd; 1430 } 1431 } else { 1432 if (get_option_addr(ctx, &lease->mask, bootp, len, 1433 DHO_SUBNETMASK) == -1) 1434 lease->mask.s_addr = 1435 ipv4_getnetmask(lease->addr.s_addr); 1436 if (get_option_addr(ctx, &lease->brd, bootp, len, 1437 DHO_BROADCAST) == -1) 1438 lease->brd.s_addr = 1439 lease->addr.s_addr | ~lease->mask.s_addr; 1440 } 1441 if (get_option_uint32(ctx, &lease->leasetime, 1442 bootp, len, DHO_LEASETIME) != 0) 1443 lease->leasetime = DHCP_INFINITE_LIFETIME; 1444 if (get_option_uint32(ctx, &lease->renewaltime, 1445 bootp, len, DHO_RENEWALTIME) != 0) 1446 lease->renewaltime = 0; 1447 if (get_option_uint32(ctx, &lease->rebindtime, 1448 bootp, len, DHO_REBINDTIME) != 0) 1449 lease->rebindtime = 0; 1450 if (get_option_addr(ctx, &lease->server, bootp, len, DHO_SERVERID) != 0) 1451 lease->server.s_addr = INADDR_ANY; 1452 } 1453 1454 static const char * 1455 get_dhcp_op(uint8_t type) 1456 { 1457 const struct dhcp_op *d; 1458 1459 for (d = dhcp_ops; d->name; d++) 1460 if (d->value == type) 1461 return d->name; 1462 return NULL; 1463 } 1464 1465 static void 1466 dhcp_fallback(void *arg) 1467 { 1468 struct interface *iface; 1469 1470 iface = (struct interface *)arg; 1471 dhcpcd_selectprofile(iface, iface->options->fallback); 1472 dhcpcd_startinterface(iface); 1473 } 1474 1475 static void 1476 dhcp_new_xid(struct interface *ifp) 1477 { 1478 struct dhcp_state *state; 1479 const struct interface *ifp1; 1480 const struct dhcp_state *state1; 1481 1482 state = D_STATE(ifp); 1483 if (ifp->options->options & DHCPCD_XID_HWADDR && 1484 ifp->hwlen >= sizeof(state->xid)) 1485 /* The lower bits are probably more unique on the network */ 1486 memcpy(&state->xid, 1487 (ifp->hwaddr + ifp->hwlen) - sizeof(state->xid), 1488 sizeof(state->xid)); 1489 else { 1490 again: 1491 state->xid = arc4random(); 1492 } 1493 1494 /* Ensure it's unique */ 1495 TAILQ_FOREACH(ifp1, ifp->ctx->ifaces, next) { 1496 if (ifp == ifp1) 1497 continue; 1498 if ((state1 = D_CSTATE(ifp1)) == NULL) 1499 continue; 1500 if (state1->xid == state->xid) 1501 break; 1502 } 1503 if (ifp1 != NULL) { 1504 if (ifp->options->options & DHCPCD_XID_HWADDR && 1505 ifp->hwlen >= sizeof(state->xid)) 1506 { 1507 logerrx("%s: duplicate xid on %s", 1508 ifp->name, ifp1->name); 1509 return; 1510 } 1511 goto again; 1512 } 1513 1514 /* We can't do this when sharing leases across interfaes */ 1515 #if 0 1516 /* As the XID changes, re-apply the filter. */ 1517 if (state->bpf_fd != -1) { 1518 if (bpf_bootp(ifp, state->bpf_fd) == -1) 1519 logerr(__func__); /* try to continue */ 1520 } 1521 #endif 1522 } 1523 1524 void 1525 dhcp_close(struct interface *ifp) 1526 { 1527 struct dhcp_state *state = D_STATE(ifp); 1528 1529 if (state == NULL) 1530 return; 1531 1532 if (state->bpf_fd != -1) { 1533 eloop_event_delete(ifp->ctx->eloop, state->bpf_fd); 1534 bpf_close(ifp, state->bpf_fd); 1535 state->bpf_fd = -1; 1536 state->bpf_flags |= BPF_EOF; 1537 } 1538 if (state->udp_fd != -1) { 1539 eloop_event_delete(ifp->ctx->eloop, state->udp_fd); 1540 close(state->udp_fd); 1541 state->udp_fd = -1; 1542 } 1543 1544 state->interval = 0; 1545 } 1546 1547 static int 1548 dhcp_openudp(struct in_addr *ia) 1549 { 1550 int s; 1551 struct sockaddr_in sin; 1552 int n; 1553 1554 if ((s = xsocket(PF_INET, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP)) == -1) 1555 return -1; 1556 1557 n = 1; 1558 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) 1559 goto errexit; 1560 #ifdef IP_RECVIF 1561 if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &n, sizeof(n)) == -1) 1562 goto errexit; 1563 #else 1564 if (setsockopt(s, IPPROTO_IP, IP_RECVPKTINFO, &n, sizeof(n)) == -1) 1565 goto errexit; 1566 #endif 1567 memset(&sin, 0, sizeof(sin)); 1568 sin.sin_family = AF_INET; 1569 sin.sin_port = htons(BOOTPC); 1570 if (ia != NULL) 1571 sin.sin_addr = *ia; 1572 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) 1573 goto errexit; 1574 1575 return s; 1576 1577 errexit: 1578 close(s); 1579 return -1; 1580 } 1581 1582 static uint16_t 1583 in_cksum(const void *data, size_t len, uint32_t *isum) 1584 { 1585 const uint16_t *word = data; 1586 uint32_t sum = isum != NULL ? *isum : 0; 1587 1588 for (; len > 1; len -= sizeof(*word)) 1589 sum += *word++; 1590 1591 if (len == 1) 1592 sum += htons((uint16_t)(*(const uint8_t *)word << 8)); 1593 1594 if (isum != NULL) 1595 *isum = sum; 1596 1597 sum = (sum >> 16) + (sum & 0xffff); 1598 sum += (sum >> 16); 1599 1600 return (uint16_t)~sum; 1601 } 1602 1603 static struct bootp_pkt * 1604 dhcp_makeudppacket(size_t *sz, const uint8_t *data, size_t length, 1605 struct in_addr source, struct in_addr dest) 1606 { 1607 struct bootp_pkt *udpp; 1608 struct ip *ip; 1609 struct udphdr *udp; 1610 1611 if ((udpp = calloc(1, sizeof(*ip) + sizeof(*udp) + length)) == NULL) 1612 return NULL; 1613 ip = &udpp->ip; 1614 udp = &udpp->udp; 1615 1616 /* OK, this is important :) 1617 * We copy the data to our packet and then create a small part of the 1618 * ip structure and an invalid ip_len (basically udp length). 1619 * We then fill the udp structure and put the checksum 1620 * of the whole packet into the udp checksum. 1621 * Finally we complete the ip structure and ip checksum. 1622 * If we don't do the ordering like so then the udp checksum will be 1623 * broken, so find another way of doing it! */ 1624 1625 memcpy(&udpp->bootp, data, length); 1626 1627 ip->ip_p = IPPROTO_UDP; 1628 ip->ip_src.s_addr = source.s_addr; 1629 if (dest.s_addr == 0) 1630 ip->ip_dst.s_addr = INADDR_BROADCAST; 1631 else 1632 ip->ip_dst.s_addr = dest.s_addr; 1633 1634 udp->uh_sport = htons(BOOTPC); 1635 udp->uh_dport = htons(BOOTPS); 1636 udp->uh_ulen = htons((uint16_t)(sizeof(*udp) + length)); 1637 ip->ip_len = udp->uh_ulen; 1638 udp->uh_sum = in_cksum(udpp, sizeof(*ip) + sizeof(*udp) + length, NULL); 1639 1640 ip->ip_v = IPVERSION; 1641 ip->ip_hl = sizeof(*ip) >> 2; 1642 ip->ip_id = (uint16_t)arc4random_uniform(UINT16_MAX); 1643 ip->ip_ttl = IPDEFTTL; 1644 ip->ip_len = htons((uint16_t)(sizeof(*ip) + sizeof(*udp) + length)); 1645 ip->ip_sum = in_cksum(ip, sizeof(*ip), NULL); 1646 if (ip->ip_sum == 0) 1647 ip->ip_sum = 0xffff; /* RFC 768 */ 1648 1649 *sz = sizeof(*ip) + sizeof(*udp) + length; 1650 return udpp; 1651 } 1652 1653 static ssize_t 1654 dhcp_sendudp(struct interface *ifp, struct in_addr *to, void *data, size_t len) 1655 { 1656 struct sockaddr_in sin = { 1657 .sin_family = AF_INET, 1658 .sin_addr = *to, 1659 .sin_port = htons(BOOTPS), 1660 #ifdef HAVE_SA_LEN 1661 .sin_len = sizeof(sin), 1662 #endif 1663 }; 1664 struct iovec iov[] = { 1665 { .iov_base = data, .iov_len = len } 1666 }; 1667 struct msghdr msg = { 1668 .msg_name = (void *)&sin, 1669 .msg_namelen = sizeof(sin), 1670 .msg_iov = iov, 1671 .msg_iovlen = 1, 1672 }; 1673 struct dhcp_state *state = D_STATE(ifp); 1674 ssize_t r; 1675 int fd; 1676 1677 fd = state->udp_fd; 1678 if (fd == -1) { 1679 fd = dhcp_openudp(state->addr != NULL ?&state->addr->addr:NULL); 1680 if (fd == -1) 1681 return -1; 1682 } 1683 r = sendmsg(fd, &msg, 0); 1684 if (state->udp_fd == -1) 1685 close(fd); 1686 return r; 1687 } 1688 1689 static void 1690 send_message(struct interface *ifp, uint8_t type, 1691 void (*callback)(void *)) 1692 { 1693 struct dhcp_state *state = D_STATE(ifp); 1694 struct if_options *ifo = ifp->options; 1695 struct bootp *bootp; 1696 struct bootp_pkt *udp; 1697 size_t len, ulen; 1698 ssize_t r; 1699 struct in_addr from, to; 1700 struct timespec tv; 1701 1702 if (!callback) { 1703 /* No carrier? Don't bother sending the packet. */ 1704 if (ifp->carrier <= LINK_DOWN) 1705 return; 1706 logdebugx("%s: sending %s with xid 0x%x", 1707 ifp->name, 1708 ifo->options & DHCPCD_BOOTP ? "BOOTP" : get_dhcp_op(type), 1709 state->xid); 1710 } else { 1711 if (state->interval == 0) 1712 state->interval = 4; 1713 else { 1714 state->interval *= 2; 1715 if (state->interval > 64) 1716 state->interval = 64; 1717 } 1718 tv.tv_sec = state->interval + DHCP_RAND_MIN; 1719 tv.tv_nsec = (suseconds_t)arc4random_uniform( 1720 (DHCP_RAND_MAX - DHCP_RAND_MIN) * NSEC_PER_SEC); 1721 timespecnorm(&tv); 1722 /* No carrier? Don't bother sending the packet. 1723 * However, we do need to advance the timeout. */ 1724 if (ifp->carrier <= LINK_DOWN) 1725 goto fail; 1726 logdebugx("%s: sending %s (xid 0x%x), next in %0.1f seconds", 1727 ifp->name, 1728 ifo->options & DHCPCD_BOOTP ? "BOOTP" : get_dhcp_op(type), 1729 state->xid, 1730 timespec_to_double(&tv)); 1731 } 1732 1733 r = make_message(&bootp, ifp, type); 1734 if (r == -1) 1735 goto fail; 1736 len = (size_t)r; 1737 1738 if (!(state->added & STATE_FAKE) && 1739 state->addr != NULL && 1740 ipv4_iffindaddr(ifp, &state->lease.addr, NULL) != NULL) 1741 from.s_addr = state->lease.addr.s_addr; 1742 else 1743 from.s_addr = INADDR_ANY; 1744 if (from.s_addr != INADDR_ANY && 1745 state->lease.server.s_addr != INADDR_ANY) 1746 to.s_addr = state->lease.server.s_addr; 1747 else 1748 to.s_addr = INADDR_BROADCAST; 1749 1750 /* 1751 * If not listening on the unspecified address we can 1752 * only receive broadcast messages via BPF. 1753 * Sockets bound to an address cannot receive broadcast messages 1754 * even if they are setup to send them. 1755 * Broadcasting from UDP is only an optimisation for rebinding 1756 * and on BSD, at least, is reliant on the subnet route being 1757 * correctly configured to receive the unicast reply. 1758 * As such, we always broadcast and receive the reply to it via BPF. 1759 * This also guarantees we have a DHCP server attached to the 1760 * interface we want to configure because we can't dictate the 1761 * interface via IP_PKTINFO unlike for IPv6. 1762 */ 1763 if (to.s_addr != INADDR_BROADCAST) 1764 { 1765 if (dhcp_sendudp(ifp, &to, bootp, len) != -1) 1766 goto out; 1767 logerr("%s: dhcp_sendudp", ifp->name); 1768 } 1769 1770 if (dhcp_openbpf(ifp) == -1) 1771 goto out; 1772 1773 udp = dhcp_makeudppacket(&ulen, (uint8_t *)bootp, len, from, to); 1774 if (udp == NULL) { 1775 logerr("%s: dhcp_makeudppacket", ifp->name); 1776 r = 0; 1777 } else { 1778 r = bpf_send(ifp, state->bpf_fd, 1779 ETHERTYPE_IP, (uint8_t *)udp, ulen); 1780 free(udp); 1781 } 1782 /* If we failed to send a raw packet this normally means 1783 * we don't have the ability to work beneath the IP layer 1784 * for this interface. 1785 * As such we remove it from consideration without actually 1786 * stopping the interface. */ 1787 if (r == -1) { 1788 logerr("%s: bpf_send", ifp->name); 1789 switch(errno) { 1790 case ENETDOWN: 1791 case ENETRESET: 1792 case ENETUNREACH: 1793 case ENOBUFS: 1794 break; 1795 default: 1796 if (!(ifp->ctx->options & DHCPCD_TEST)) 1797 dhcp_drop(ifp, "FAIL"); 1798 eloop_timeout_delete(ifp->ctx->eloop, 1799 NULL, ifp); 1800 callback = NULL; 1801 } 1802 } 1803 1804 out: 1805 free(bootp); 1806 1807 fail: 1808 /* Even if we fail to send a packet we should continue as we are 1809 * as our failure timeouts will change out codepath when needed. */ 1810 if (callback) 1811 eloop_timeout_add_tv(ifp->ctx->eloop, &tv, callback, ifp); 1812 } 1813 1814 static void 1815 send_inform(void *arg) 1816 { 1817 1818 send_message((struct interface *)arg, DHCP_INFORM, send_inform); 1819 } 1820 1821 static void 1822 send_discover(void *arg) 1823 { 1824 1825 send_message((struct interface *)arg, DHCP_DISCOVER, send_discover); 1826 } 1827 1828 static void 1829 send_request(void *arg) 1830 { 1831 1832 send_message((struct interface *)arg, DHCP_REQUEST, send_request); 1833 } 1834 1835 static void 1836 send_renew(void *arg) 1837 { 1838 1839 send_message((struct interface *)arg, DHCP_REQUEST, send_renew); 1840 } 1841 1842 static void 1843 send_rebind(void *arg) 1844 { 1845 1846 send_message((struct interface *)arg, DHCP_REQUEST, send_rebind); 1847 } 1848 1849 void 1850 dhcp_discover(void *arg) 1851 { 1852 struct interface *ifp = arg; 1853 struct dhcp_state *state = D_STATE(ifp); 1854 struct if_options *ifo = ifp->options; 1855 1856 state->state = DHS_DISCOVER; 1857 dhcp_new_xid(ifp); 1858 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 1859 if (ifo->fallback) 1860 eloop_timeout_add_sec(ifp->ctx->eloop, 1861 ifo->reboot, dhcp_fallback, ifp); 1862 #ifdef IPV4LL 1863 else if (ifo->options & DHCPCD_IPV4LL) 1864 eloop_timeout_add_sec(ifp->ctx->eloop, 1865 ifo->reboot, ipv4ll_start, ifp); 1866 #endif 1867 if (ifo->options & DHCPCD_REQUEST) 1868 loginfox("%s: soliciting a DHCP lease (requesting %s)", 1869 ifp->name, inet_ntoa(ifo->req_addr)); 1870 else 1871 loginfox("%s: soliciting a %s lease", 1872 ifp->name, ifo->options & DHCPCD_BOOTP ? "BOOTP" : "DHCP"); 1873 send_discover(ifp); 1874 } 1875 1876 static void 1877 dhcp_request(void *arg) 1878 { 1879 struct interface *ifp = arg; 1880 struct dhcp_state *state = D_STATE(ifp); 1881 1882 state->state = DHS_REQUEST; 1883 send_request(ifp); 1884 } 1885 1886 static void 1887 dhcp_expire1(struct interface *ifp) 1888 { 1889 struct dhcp_state *state = D_STATE(ifp); 1890 1891 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 1892 dhcp_drop(ifp, "EXPIRE"); 1893 unlink(state->leasefile); 1894 state->interval = 0; 1895 if (!(ifp->options->options & DHCPCD_LINK) || ifp->carrier > LINK_DOWN) 1896 dhcp_discover(ifp); 1897 } 1898 1899 static void 1900 dhcp_expire(void *arg) 1901 { 1902 struct interface *ifp = arg; 1903 1904 if (ifp->options->options & DHCPCD_LASTLEASE_EXTEND) { 1905 logwarnx("%s: DHCP lease expired, extending lease", ifp->name); 1906 return; 1907 } 1908 1909 logerrx("%s: DHCP lease expired", ifp->name); 1910 dhcp_expire1(ifp); 1911 } 1912 1913 #if defined(ARP) || defined(IN_IFF_DUPLICATED) 1914 static void 1915 dhcp_decline(struct interface *ifp) 1916 { 1917 1918 send_message(ifp, DHCP_DECLINE, NULL); 1919 } 1920 #endif 1921 1922 static void 1923 dhcp_startrenew(void *arg) 1924 { 1925 struct interface *ifp = arg; 1926 struct dhcp_state *state; 1927 struct dhcp_lease *lease; 1928 1929 if ((state = D_STATE(ifp)) == NULL) 1930 return; 1931 1932 /* Only renew in the bound or renew states */ 1933 if (state->state != DHS_BOUND && 1934 state->state != DHS_RENEW) 1935 return; 1936 1937 /* Remove the timeout as the renew may have been forced. */ 1938 eloop_timeout_delete(ifp->ctx->eloop, dhcp_startrenew, ifp); 1939 1940 lease = &state->lease; 1941 logdebugx("%s: renewing lease of %s", ifp->name, 1942 inet_ntoa(lease->addr)); 1943 state->state = DHS_RENEW; 1944 dhcp_new_xid(ifp); 1945 state->interval = 0; 1946 send_renew(ifp); 1947 } 1948 1949 void 1950 dhcp_renew(struct interface *ifp) 1951 { 1952 1953 dhcp_startrenew(ifp); 1954 } 1955 1956 static void 1957 dhcp_rebind(void *arg) 1958 { 1959 struct interface *ifp = arg; 1960 struct dhcp_state *state = D_STATE(ifp); 1961 struct dhcp_lease *lease = &state->lease; 1962 1963 logwarnx("%s: failed to renew DHCP, rebinding", ifp->name); 1964 logdebugx("%s: expire in %"PRIu32" seconds", 1965 ifp->name, lease->leasetime - lease->rebindtime); 1966 state->state = DHS_REBIND; 1967 eloop_timeout_delete(ifp->ctx->eloop, send_renew, ifp); 1968 state->lease.server.s_addr = INADDR_ANY; 1969 state->interval = 0; 1970 ifp->options->options &= ~(DHCPCD_CSR_WARNED | 1971 DHCPCD_ROUTER_HOST_ROUTE_WARNED); 1972 send_rebind(ifp); 1973 } 1974 1975 #if defined(ARP) || defined(IN_IFF_DUPLICATED) 1976 static void 1977 dhcp_finish_dad(struct interface *ifp, struct in_addr *ia) 1978 { 1979 struct dhcp_state *state = D_STATE(ifp); 1980 1981 if (state->state != DHS_PROBE) 1982 return; 1983 if (state->offer == NULL || state->offer->yiaddr != ia->s_addr) 1984 return; 1985 1986 logdebugx("%s: DAD completed for %s", ifp->name, inet_ntoa(*ia)); 1987 if (!(ifp->options->options & DHCPCD_INFORM)) 1988 dhcp_bind(ifp); 1989 #ifndef IN_IFF_DUPLICATED 1990 else { 1991 struct bootp *bootp; 1992 size_t len; 1993 1994 bootp = state->new; 1995 len = state->new_len; 1996 state->new = state->offer; 1997 state->new_len = state->offer_len; 1998 get_lease(ifp, &state->lease, state->new, state->new_len); 1999 ipv4_applyaddr(ifp); 2000 state->new = bootp; 2001 state->new_len = len; 2002 } 2003 #endif 2004 2005 /* If we forked, stop here. */ 2006 if (ifp->ctx->options & DHCPCD_FORKED) 2007 return; 2008 2009 #ifdef IPV4LL 2010 /* Stop IPv4LL now we have a working DHCP address */ 2011 ipv4ll_drop(ifp); 2012 #endif 2013 2014 if (ifp->options->options & DHCPCD_INFORM) 2015 dhcp_inform(ifp); 2016 } 2017 2018 2019 static bool 2020 dhcp_addr_duplicated(struct interface *ifp, struct in_addr *ia) 2021 { 2022 struct dhcp_state *state = D_STATE(ifp); 2023 unsigned long long opts = ifp->options->options; 2024 struct dhcpcd_ctx *ctx = ifp->ctx; 2025 bool deleted = false; 2026 #ifdef IN_IFF_DUPLICATED 2027 struct ipv4_addr *iap; 2028 #endif 2029 2030 if ((state->offer == NULL || state->offer->yiaddr != ia->s_addr) && 2031 !IN_ARE_ADDR_EQUAL(ia, &state->lease.addr)) 2032 return deleted; 2033 2034 /* RFC 2131 3.1.5, Client-server interaction */ 2035 logerrx("%s: DAD detected %s", ifp->name, inet_ntoa(*ia)); 2036 unlink(state->leasefile); 2037 if (!(opts & DHCPCD_STATIC) && !state->lease.frominfo) 2038 dhcp_decline(ifp); 2039 #ifdef IN_IFF_DUPLICATED 2040 if ((iap = ipv4_iffindaddr(ifp, ia, NULL)) != NULL) { 2041 ipv4_deladdr(iap, 0); 2042 deleted = true; 2043 } 2044 #endif 2045 eloop_timeout_delete(ctx->eloop, NULL, ifp); 2046 if (opts & (DHCPCD_STATIC | DHCPCD_INFORM)) { 2047 state->reason = "EXPIRE"; 2048 script_runreason(ifp, state->reason); 2049 #define NOT_ONLY_SELF (DHCPCD_MASTER | DHCPCD_IPV6RS | DHCPCD_DHCP6) 2050 if (!(ctx->options & NOT_ONLY_SELF)) 2051 eloop_exit(ifp->ctx->eloop, EXIT_FAILURE); 2052 return deleted; 2053 } 2054 eloop_timeout_add_sec(ifp->ctx->eloop, 2055 DHCP_RAND_MAX, dhcp_discover, ifp); 2056 return deleted; 2057 } 2058 #endif 2059 2060 #if defined(ARP) && (!defined(KERNEL_RFC5227) || defined(ARPING)) 2061 static void 2062 dhcp_arp_not_found(struct arp_state *astate) 2063 { 2064 struct interface *ifp; 2065 #ifdef ARPING 2066 struct dhcp_state *state; 2067 struct if_options *ifo; 2068 #endif 2069 2070 ifp = astate->iface; 2071 #ifdef ARPING 2072 state = D_STATE(ifp); 2073 ifo = ifp->options; 2074 if (ifo->arping_len && state->arping_index < ifo->arping_len) { 2075 /* We didn't find a profile for this 2076 * address or hwaddr, so move to the next 2077 * arping profile */ 2078 if (++state->arping_index < ifo->arping_len) { 2079 astate->addr.s_addr = 2080 ifo->arping[state->arping_index]; 2081 arp_probe(astate); 2082 return; 2083 } 2084 arp_free(astate); 2085 dhcpcd_startinterface(ifp); 2086 return; 2087 } 2088 #endif 2089 2090 dhcp_finish_dad(ifp, &astate->addr); 2091 } 2092 2093 static void 2094 dhcp_arp_found(struct arp_state *astate, const struct arp_msg *amsg) 2095 { 2096 struct in_addr addr; 2097 struct interface *ifp = astate->iface; 2098 #ifdef ARPING 2099 struct dhcp_state *state; 2100 struct if_options *ifo; 2101 2102 state = D_STATE(ifp); 2103 2104 ifo = ifp->options; 2105 if (state->arping_index != -1 && 2106 state->arping_index < ifo->arping_len && 2107 amsg && 2108 amsg->sip.s_addr == ifo->arping[state->arping_index]) 2109 { 2110 char buf[HWADDR_LEN * 3]; 2111 2112 hwaddr_ntoa(amsg->sha, ifp->hwlen, buf, sizeof(buf)); 2113 if (dhcpcd_selectprofile(ifp, buf) == -1 && 2114 dhcpcd_selectprofile(ifp, inet_ntoa(amsg->sip)) == -1) 2115 { 2116 /* We didn't find a profile for this 2117 * address or hwaddr, so move to the next 2118 * arping profile */ 2119 dhcp_arp_not_found(astate); 2120 return; 2121 } 2122 arp_free(astate); 2123 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 2124 dhcpcd_startinterface(ifp); 2125 return; 2126 } 2127 #else 2128 UNUSED(amsg); 2129 #endif 2130 2131 addr = astate->addr; 2132 arp_free(astate); 2133 dhcp_addr_duplicated(ifp, &addr); 2134 } 2135 2136 #ifdef KERNEL_RFC5227 2137 static void 2138 dhcp_arp_announced(struct arp_state *state) 2139 { 2140 2141 arp_free(state); 2142 } 2143 #endif /* KERNEL_RFC5227 */ 2144 #endif /* ARP */ 2145 2146 void 2147 dhcp_bind(struct interface *ifp) 2148 { 2149 struct dhcpcd_ctx *ctx = ifp->ctx; 2150 struct dhcp_state *state = D_STATE(ifp); 2151 struct if_options *ifo = ifp->options; 2152 struct dhcp_lease *lease = &state->lease; 2153 2154 state->reason = NULL; 2155 /* If we don't have an offer, we are re-binding a lease on preference, 2156 * normally when two interfaces have a lease matching IP addresses. */ 2157 if (state->offer) { 2158 free(state->old); 2159 state->old = state->new; 2160 state->old_len = state->new_len; 2161 state->new = state->offer; 2162 state->new_len = state->offer_len; 2163 state->offer = NULL; 2164 state->offer_len = 0; 2165 } 2166 get_lease(ifp, lease, state->new, state->new_len); 2167 if (ifo->options & DHCPCD_STATIC) { 2168 loginfox("%s: using static address %s/%d", 2169 ifp->name, inet_ntoa(lease->addr), 2170 inet_ntocidr(lease->mask)); 2171 lease->leasetime = DHCP_INFINITE_LIFETIME; 2172 state->reason = "STATIC"; 2173 } else if (ifo->options & DHCPCD_INFORM) { 2174 loginfox("%s: received approval for %s", 2175 ifp->name, inet_ntoa(lease->addr)); 2176 lease->leasetime = DHCP_INFINITE_LIFETIME; 2177 state->reason = "INFORM"; 2178 } else { 2179 if (lease->frominfo) 2180 state->reason = "TIMEOUT"; 2181 if (lease->leasetime == DHCP_INFINITE_LIFETIME) { 2182 lease->renewaltime = 2183 lease->rebindtime = 2184 lease->leasetime; 2185 loginfox("%s: leased %s for infinity", 2186 ifp->name, inet_ntoa(lease->addr)); 2187 } else { 2188 if (lease->leasetime < DHCP_MIN_LEASE) { 2189 logwarnx("%s: minimum lease is %d seconds", 2190 ifp->name, DHCP_MIN_LEASE); 2191 lease->leasetime = DHCP_MIN_LEASE; 2192 } 2193 if (lease->rebindtime == 0) 2194 lease->rebindtime = 2195 (uint32_t)(lease->leasetime * T2); 2196 else if (lease->rebindtime >= lease->leasetime) { 2197 lease->rebindtime = 2198 (uint32_t)(lease->leasetime * T2); 2199 logwarnx("%s: rebind time greater than lease " 2200 "time, forcing to %"PRIu32" seconds", 2201 ifp->name, lease->rebindtime); 2202 } 2203 if (lease->renewaltime == 0) 2204 lease->renewaltime = 2205 (uint32_t)(lease->leasetime * T1); 2206 else if (lease->renewaltime > lease->rebindtime) { 2207 lease->renewaltime = 2208 (uint32_t)(lease->leasetime * T1); 2209 logwarnx("%s: renewal time greater than " 2210 "rebind time, forcing to %"PRIu32" seconds", 2211 ifp->name, lease->renewaltime); 2212 } 2213 if (state->addr && 2214 lease->addr.s_addr == state->addr->addr.s_addr && 2215 !(state->added & STATE_FAKE)) 2216 logdebugx("%s: leased %s for %"PRIu32" seconds", 2217 ifp->name, inet_ntoa(lease->addr), 2218 lease->leasetime); 2219 else 2220 loginfox("%s: leased %s for %"PRIu32" seconds", 2221 ifp->name, inet_ntoa(lease->addr), 2222 lease->leasetime); 2223 } 2224 } 2225 if (ctx->options & DHCPCD_TEST) { 2226 state->reason = "TEST"; 2227 script_runreason(ifp, state->reason); 2228 eloop_exit(ctx->eloop, EXIT_SUCCESS); 2229 return; 2230 } 2231 if (state->reason == NULL) { 2232 if (state->old && !(state->added & STATE_FAKE)) { 2233 if (state->old->yiaddr == state->new->yiaddr && 2234 lease->server.s_addr && 2235 state->state != DHS_REBIND) 2236 state->reason = "RENEW"; 2237 else 2238 state->reason = "REBIND"; 2239 } else if (state->state == DHS_REBOOT) 2240 state->reason = "REBOOT"; 2241 else 2242 state->reason = "BOUND"; 2243 } 2244 if (lease->leasetime == DHCP_INFINITE_LIFETIME) 2245 lease->renewaltime = lease->rebindtime = lease->leasetime; 2246 else { 2247 eloop_timeout_add_sec(ctx->eloop, 2248 (time_t)lease->renewaltime, dhcp_startrenew, ifp); 2249 eloop_timeout_add_sec(ctx->eloop, 2250 (time_t)lease->rebindtime, dhcp_rebind, ifp); 2251 eloop_timeout_add_sec(ctx->eloop, 2252 (time_t)lease->leasetime, dhcp_expire, ifp); 2253 logdebugx("%s: renew in %"PRIu32" seconds, rebind in %"PRIu32 2254 " seconds", 2255 ifp->name, lease->renewaltime, lease->rebindtime); 2256 } 2257 state->state = DHS_BOUND; 2258 if (!state->lease.frominfo && 2259 !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))) 2260 if (write_lease(ifp, state->new, state->new_len) == -1) 2261 logerr(__func__); 2262 2263 ipv4_applyaddr(ifp); 2264 2265 /* Close the BPF filter as we can now receive DHCP messages 2266 * on a UDP socket. */ 2267 if (!(state->udp_fd == -1 || 2268 (state->old != NULL && state->old->yiaddr != state->new->yiaddr))) 2269 return; 2270 dhcp_close(ifp); 2271 2272 2273 /* If not in master mode, open an address specific socket. */ 2274 if (ctx->udp_fd != -1) 2275 return; 2276 state->udp_fd = dhcp_openudp(&state->addr->addr); 2277 if (state->udp_fd == -1) { 2278 logerr(__func__); 2279 /* Address sharing without master mode is not supported. 2280 * It's also possible another DHCP client could be running, 2281 * which is even worse. 2282 * We still need to work, so re-open BPF. */ 2283 dhcp_openbpf(ifp); 2284 return; 2285 } 2286 eloop_event_add(ctx->eloop, state->udp_fd, dhcp_handleifudp, ifp); 2287 } 2288 2289 static void 2290 dhcp_lastlease(void *arg) 2291 { 2292 struct interface *ifp = arg; 2293 struct dhcp_state *state = D_STATE(ifp); 2294 2295 loginfox("%s: timed out contacting a DHCP server, using last lease", 2296 ifp->name); 2297 dhcp_bind(ifp); 2298 /* If we forked, stop here. */ 2299 if (ifp->ctx->options & DHCPCD_FORKED) 2300 return; 2301 state->interval = 0; 2302 dhcp_discover(ifp); 2303 } 2304 2305 static size_t 2306 dhcp_message_new(struct bootp **bootp, 2307 const struct in_addr *addr, const struct in_addr *mask) 2308 { 2309 uint8_t *p; 2310 uint32_t cookie; 2311 2312 if ((*bootp = calloc(1, sizeof(**bootp))) == NULL) 2313 return 0; 2314 2315 (*bootp)->yiaddr = addr->s_addr; 2316 p = (*bootp)->vend; 2317 2318 cookie = htonl(MAGIC_COOKIE); 2319 memcpy(p, &cookie, sizeof(cookie)); 2320 p += sizeof(cookie); 2321 2322 if (mask->s_addr != INADDR_ANY) { 2323 *p++ = DHO_SUBNETMASK; 2324 *p++ = sizeof(mask->s_addr); 2325 memcpy(p, &mask->s_addr, sizeof(mask->s_addr)); 2326 p+= sizeof(mask->s_addr); 2327 } 2328 2329 *p = DHO_END; 2330 return sizeof(**bootp); 2331 } 2332 2333 #ifdef ARP 2334 #ifndef KERNEL_RFC5227 2335 static void 2336 dhcp_arp_defend_failed(struct arp_state *astate) 2337 { 2338 2339 dhcp_drop(astate->iface, "EXPIRED"); 2340 dhcp_start1(astate->iface); 2341 } 2342 #endif 2343 2344 #if !defined(KERNEL_RFC5227) || defined(ARPING) 2345 static struct arp_state * 2346 dhcp_arp_new(struct interface *ifp, struct in_addr *addr) 2347 { 2348 struct arp_state *astate; 2349 2350 astate = arp_new(ifp, addr); 2351 if (astate == NULL) 2352 return NULL; 2353 2354 astate->found_cb = dhcp_arp_found; 2355 astate->not_found_cb = dhcp_arp_not_found; 2356 #ifdef KERNEL_RFC5227 2357 astate->announced_cb = dhcp_arp_announced; 2358 #else 2359 astate->announced_cb = NULL; 2360 astate->defend_failed_cb = dhcp_arp_defend_failed; 2361 #endif 2362 return astate; 2363 } 2364 #endif 2365 #endif /* ARP */ 2366 2367 #if defined(ARP) || defined(KERNEL_RFC5227) 2368 static int 2369 dhcp_arp_address(struct interface *ifp) 2370 { 2371 struct dhcp_state *state; 2372 struct in_addr addr; 2373 struct ipv4_addr *ia; 2374 2375 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 2376 2377 state = D_STATE(ifp); 2378 addr.s_addr = state->offer->yiaddr == INADDR_ANY ? 2379 state->offer->ciaddr : state->offer->yiaddr; 2380 /* If the interface already has the address configured 2381 * then we can't ARP for duplicate detection. */ 2382 ia = ipv4_iffindaddr(ifp, &addr, NULL); 2383 #ifdef IN_IFF_NOTUSEABLE 2384 if (ia == NULL || ia->addr_flags & IN_IFF_NOTUSEABLE) { 2385 state->state = DHS_PROBE; 2386 if (ia == NULL) { 2387 struct dhcp_lease l; 2388 2389 get_lease(ifp, &l, state->offer, state->offer_len); 2390 /* Add the address now, let the kernel handle DAD. */ 2391 ipv4_addaddr(ifp, &l.addr, &l.mask, &l.brd, 2392 l.leasetime, l.rebindtime); 2393 } else if (ia->addr_flags & IN_IFF_DUPLICATED) 2394 dhcp_addr_duplicated(ifp, &ia->addr); 2395 else 2396 loginfox("%s: waiting for DAD on %s", 2397 ifp->name, inet_ntoa(addr)); 2398 return 0; 2399 } 2400 #else 2401 if (!(ifp->flags & IFF_NOARP) && 2402 ifp->options->options & DHCPCD_ARP && 2403 ia == NULL) 2404 { 2405 struct arp_state *astate; 2406 struct dhcp_lease l; 2407 2408 astate = dhcp_arp_new(ifp, &addr); 2409 if (astate == NULL) 2410 return -1; 2411 2412 state->state = DHS_PROBE; 2413 get_lease(ifp, &l, state->offer, state->offer_len); 2414 loginfox("%s: probing address %s/%d", 2415 ifp->name, inet_ntoa(l.addr), inet_ntocidr(l.mask)); 2416 /* We need to handle DAD. */ 2417 arp_probe(astate); 2418 return 0; 2419 } 2420 #endif 2421 2422 return 1; 2423 } 2424 2425 static void 2426 dhcp_arp_bind(struct interface *ifp) 2427 { 2428 2429 if (ifp->ctx->options & DHCPCD_TEST || 2430 dhcp_arp_address(ifp) == 1) 2431 dhcp_bind(ifp); 2432 } 2433 #endif 2434 2435 static void 2436 dhcp_static(struct interface *ifp) 2437 { 2438 struct if_options *ifo; 2439 struct dhcp_state *state; 2440 struct ipv4_addr *ia; 2441 2442 state = D_STATE(ifp); 2443 ifo = ifp->options; 2444 2445 ia = NULL; 2446 if (ifo->req_addr.s_addr == INADDR_ANY && 2447 (ia = ipv4_iffindaddr(ifp, NULL, NULL)) == NULL) 2448 { 2449 loginfox("%s: waiting for 3rd party to " 2450 "configure IP address", ifp->name); 2451 state->reason = "3RDPARTY"; 2452 script_runreason(ifp, state->reason); 2453 return; 2454 } 2455 2456 state->offer_len = dhcp_message_new(&state->offer, 2457 ia ? &ia->addr : &ifo->req_addr, 2458 ia ? &ia->mask : &ifo->req_mask); 2459 if (state->offer_len) 2460 #if defined(ARP) || defined(KERNEL_RFC5227) 2461 dhcp_arp_bind(ifp); 2462 #else 2463 dhcp_bind(ifp); 2464 #endif 2465 } 2466 2467 void 2468 dhcp_inform(struct interface *ifp) 2469 { 2470 struct dhcp_state *state; 2471 struct if_options *ifo; 2472 struct ipv4_addr *ia; 2473 2474 state = D_STATE(ifp); 2475 ifo = ifp->options; 2476 2477 state->state = DHS_INFORM; 2478 free(state->offer); 2479 state->offer = NULL; 2480 state->offer_len = 0; 2481 2482 if (ifo->req_addr.s_addr == INADDR_ANY) { 2483 ia = ipv4_iffindaddr(ifp, NULL, NULL); 2484 if (ia == NULL) { 2485 loginfox("%s: waiting for 3rd party to " 2486 "configure IP address", 2487 ifp->name); 2488 if (!(ifp->ctx->options & DHCPCD_TEST)) { 2489 state->reason = "3RDPARTY"; 2490 script_runreason(ifp, state->reason); 2491 } 2492 return; 2493 } 2494 } else { 2495 ia = ipv4_iffindaddr(ifp, &ifo->req_addr, &ifo->req_mask); 2496 if (ia == NULL) { 2497 if (ifp->ctx->options & DHCPCD_TEST) { 2498 logerrx("%s: cannot add IP address in test mode", 2499 ifp->name); 2500 return; 2501 } 2502 ia = ipv4_iffindaddr(ifp, &ifo->req_addr, NULL); 2503 if (ia != NULL) 2504 /* Netmask must be different, delete it. */ 2505 ipv4_deladdr(ia, 1); 2506 state->offer_len = dhcp_message_new(&state->offer, 2507 &ifo->req_addr, &ifo->req_mask); 2508 #ifdef ARP 2509 if (dhcp_arp_address(ifp) != 1) 2510 return; 2511 #endif 2512 ia = ipv4_iffindaddr(ifp, 2513 &ifo->req_addr, &ifo->req_mask); 2514 assert(ia != NULL); 2515 } 2516 } 2517 2518 state->addr = ia; 2519 state->offer_len = dhcp_message_new(&state->offer, 2520 &ia->addr, &ia->mask); 2521 if (state->offer_len) { 2522 dhcp_new_xid(ifp); 2523 get_lease(ifp, &state->lease, state->offer, state->offer_len); 2524 send_inform(ifp); 2525 } 2526 } 2527 2528 void 2529 dhcp_reboot_newopts(struct interface *ifp, unsigned long long oldopts) 2530 { 2531 struct if_options *ifo; 2532 struct dhcp_state *state = D_STATE(ifp); 2533 2534 if (state == NULL || state->state == DHS_NONE) 2535 return; 2536 ifo = ifp->options; 2537 if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) && 2538 (state->addr == NULL || 2539 state->addr->addr.s_addr != ifo->req_addr.s_addr)) || 2540 (oldopts & (DHCPCD_INFORM | DHCPCD_STATIC) && 2541 !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))) 2542 { 2543 dhcp_drop(ifp, "EXPIRE"); 2544 } 2545 } 2546 2547 #ifdef ARP 2548 static int 2549 dhcp_activeaddr(const struct interface *ifp, const struct in_addr *addr) 2550 { 2551 const struct interface *ifp1; 2552 const struct dhcp_state *state; 2553 2554 TAILQ_FOREACH(ifp1, ifp->ctx->ifaces, next) { 2555 if (ifp1 == ifp) 2556 continue; 2557 if ((state = D_CSTATE(ifp1)) == NULL) 2558 continue; 2559 switch(state->state) { 2560 case DHS_REBOOT: 2561 case DHS_RENEW: 2562 case DHS_REBIND: 2563 case DHS_BOUND: 2564 case DHS_INFORM: 2565 break; 2566 default: 2567 continue; 2568 } 2569 if (state->lease.addr.s_addr == addr->s_addr) 2570 return 1; 2571 } 2572 return 0; 2573 } 2574 #endif 2575 2576 static void 2577 dhcp_reboot(struct interface *ifp) 2578 { 2579 struct if_options *ifo; 2580 struct dhcp_state *state = D_STATE(ifp); 2581 #ifdef ARP 2582 struct ipv4_addr *ia; 2583 #endif 2584 2585 if (state == NULL || state->state == DHS_NONE) 2586 return; 2587 ifo = ifp->options; 2588 state->state = DHS_REBOOT; 2589 state->interval = 0; 2590 2591 if (ifo->options & DHCPCD_LINK && ifp->carrier <= LINK_DOWN) { 2592 loginfox("%s: waiting for carrier", ifp->name); 2593 return; 2594 } 2595 if (ifo->options & DHCPCD_STATIC) { 2596 dhcp_static(ifp); 2597 return; 2598 } 2599 if (ifo->options & DHCPCD_INFORM) { 2600 loginfox("%s: informing address of %s", 2601 ifp->name, inet_ntoa(state->lease.addr)); 2602 dhcp_inform(ifp); 2603 return; 2604 } 2605 if (ifo->reboot == 0 || state->offer == NULL) { 2606 dhcp_discover(ifp); 2607 return; 2608 } 2609 if (!IS_DHCP(state->offer)) 2610 return; 2611 2612 loginfox("%s: rebinding lease of %s", 2613 ifp->name, inet_ntoa(state->lease.addr)); 2614 2615 #ifdef ARP 2616 #ifndef KERNEL_RFC5227 2617 /* Create the DHCP ARP state so we can defend it. */ 2618 (void)dhcp_arp_new(ifp, &state->lease.addr); 2619 #endif 2620 2621 /* If the address exists on the interface and no other interface 2622 * is currently using it then announce it to ensure this 2623 * interface gets the reply. */ 2624 ia = ipv4_iffindaddr(ifp, &state->lease.addr, NULL); 2625 if (ia != NULL && 2626 !(ifp->ctx->options & DHCPCD_TEST) && 2627 #ifdef IN_IFF_NOTUSEABLE 2628 !(ia->addr_flags & IN_IFF_NOTUSEABLE) && 2629 #endif 2630 dhcp_activeaddr(ifp, &state->lease.addr) == 0) 2631 arp_ifannounceaddr(ifp, &state->lease.addr); 2632 #endif 2633 2634 dhcp_new_xid(ifp); 2635 state->lease.server.s_addr = INADDR_ANY; 2636 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 2637 2638 #ifdef IPV4LL 2639 /* Need to add this before dhcp_expire and friends. */ 2640 if (!ifo->fallback && ifo->options & DHCPCD_IPV4LL) 2641 eloop_timeout_add_sec(ifp->ctx->eloop, 2642 ifo->reboot, ipv4ll_start, ifp); 2643 #endif 2644 2645 if (ifo->options & DHCPCD_LASTLEASE && state->lease.frominfo) 2646 eloop_timeout_add_sec(ifp->ctx->eloop, 2647 ifo->reboot, dhcp_lastlease, ifp); 2648 else if (!(ifo->options & DHCPCD_INFORM)) 2649 eloop_timeout_add_sec(ifp->ctx->eloop, 2650 ifo->reboot, dhcp_expire, ifp); 2651 2652 /* Don't bother ARP checking as the server could NAK us first. 2653 * Don't call dhcp_request as that would change the state */ 2654 send_request(ifp); 2655 } 2656 2657 void 2658 dhcp_drop(struct interface *ifp, const char *reason) 2659 { 2660 struct dhcp_state *state; 2661 #ifdef RELEASE_SLOW 2662 struct timespec ts; 2663 #endif 2664 2665 state = D_STATE(ifp); 2666 /* dhcp_start may just have been called and we don't yet have a state 2667 * but we do have a timeout, so punt it. */ 2668 if (state == NULL || state->state == DHS_NONE) { 2669 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 2670 return; 2671 } 2672 2673 #ifdef ARP 2674 if (state->addr != NULL) 2675 arp_freeaddr(ifp, &state->addr->addr); 2676 #endif 2677 #ifdef ARPING 2678 state->arping_index = -1; 2679 #endif 2680 2681 if (ifp->options->options & DHCPCD_RELEASE && 2682 !(ifp->options->options & DHCPCD_INFORM)) 2683 { 2684 /* Failure to send the release may cause this function to 2685 * re-enter so guard by setting the state. */ 2686 if (state->state == DHS_RELEASE) 2687 return; 2688 state->state = DHS_RELEASE; 2689 2690 unlink(state->leasefile); 2691 if (ifp->carrier > LINK_DOWN && 2692 state->new != NULL && 2693 state->lease.server.s_addr != INADDR_ANY) 2694 { 2695 loginfox("%s: releasing lease of %s", 2696 ifp->name, inet_ntoa(state->lease.addr)); 2697 dhcp_new_xid(ifp); 2698 send_message(ifp, DHCP_RELEASE, NULL); 2699 #ifdef RELEASE_SLOW 2700 /* Give the packet a chance to go */ 2701 ts.tv_sec = RELEASE_DELAY_S; 2702 ts.tv_nsec = RELEASE_DELAY_NS; 2703 nanosleep(&ts, NULL); 2704 #endif 2705 } 2706 } 2707 2708 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 2709 #ifdef AUTH 2710 dhcp_auth_reset(&state->auth); 2711 #endif 2712 2713 state->state = DHS_NONE; 2714 free(state->offer); 2715 state->offer = NULL; 2716 state->offer_len = 0; 2717 free(state->old); 2718 state->old = state->new; 2719 state->old_len = state->new_len; 2720 state->new = NULL; 2721 state->new_len = 0; 2722 state->reason = reason; 2723 ipv4_applyaddr(ifp); 2724 free(state->old); 2725 state->old = NULL; 2726 state->old_len = 0; 2727 state->lease.addr.s_addr = 0; 2728 ifp->options->options &= ~(DHCPCD_CSR_WARNED | 2729 DHCPCD_ROUTER_HOST_ROUTE_WARNED); 2730 } 2731 2732 static int 2733 blacklisted_ip(const struct if_options *ifo, in_addr_t addr) 2734 { 2735 size_t i; 2736 2737 for (i = 0; i < ifo->blacklist_len; i += 2) 2738 if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1])) 2739 return 1; 2740 return 0; 2741 } 2742 2743 #define WHTLST_NONE 0 2744 #define WHTLST_MATCH 1 2745 #define WHTLST_NOMATCH 2 2746 static unsigned int 2747 whitelisted_ip(const struct if_options *ifo, in_addr_t addr) 2748 { 2749 size_t i; 2750 2751 if (ifo->whitelist_len == 0) 2752 return WHTLST_NONE; 2753 for (i = 0; i < ifo->whitelist_len; i += 2) 2754 if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1])) 2755 return WHTLST_MATCH; 2756 return WHTLST_NOMATCH; 2757 } 2758 2759 static void 2760 log_dhcp(logfunc_t *logfunc, const char *msg, 2761 const struct interface *ifp, const struct bootp *bootp, size_t bootp_len, 2762 const struct in_addr *from, int ad) 2763 { 2764 const char *tfrom; 2765 char *a, sname[sizeof(bootp->sname) * 4]; 2766 struct in_addr addr; 2767 int r; 2768 uint8_t overl; 2769 2770 if (strcmp(msg, "NAK:") == 0) { 2771 a = get_option_string(ifp->ctx, bootp, bootp_len, DHO_MESSAGE); 2772 if (a) { 2773 char *tmp; 2774 size_t al, tmpl; 2775 2776 al = strlen(a); 2777 tmpl = (al * 4) + 1; 2778 tmp = malloc(tmpl); 2779 if (tmp == NULL) { 2780 logerr(__func__); 2781 free(a); 2782 return; 2783 } 2784 print_string(tmp, tmpl, OT_STRING, (uint8_t *)a, al); 2785 free(a); 2786 a = tmp; 2787 } 2788 } else if (ad && bootp->yiaddr != 0) { 2789 addr.s_addr = bootp->yiaddr; 2790 a = strdup(inet_ntoa(addr)); 2791 if (a == NULL) { 2792 logerr(__func__); 2793 return; 2794 } 2795 } else 2796 a = NULL; 2797 2798 tfrom = "from"; 2799 r = get_option_addr(ifp->ctx, &addr, bootp, bootp_len, DHO_SERVERID); 2800 if (get_option_uint8(ifp->ctx, &overl, bootp, bootp_len, 2801 DHO_OPTSOVERLOADED) == -1) 2802 overl = 0; 2803 if (bootp->sname[0] && r == 0 && !(overl & 2)) { 2804 print_string(sname, sizeof(sname), OT_STRING | OT_DOMAIN, 2805 bootp->sname, sizeof(bootp->sname)); 2806 if (a == NULL) 2807 logfunc("%s: %s %s %s `%s'", 2808 ifp->name, msg, tfrom, inet_ntoa(addr), sname); 2809 else 2810 logfunc("%s: %s %s %s %s `%s'", 2811 ifp->name, msg, a, tfrom, inet_ntoa(addr), sname); 2812 } else { 2813 if (r != 0) { 2814 tfrom = "via"; 2815 addr = *from; 2816 } 2817 if (a == NULL) 2818 logfunc("%s: %s %s %s", 2819 ifp->name, msg, tfrom, inet_ntoa(addr)); 2820 else 2821 logfunc("%s: %s %s %s %s", 2822 ifp->name, msg, a, tfrom, inet_ntoa(addr)); 2823 } 2824 free(a); 2825 } 2826 2827 /* If we're sharing the same IP address with another interface on the 2828 * same network, we may receive the DHCP reply on the wrong interface. 2829 * Try and re-direct it here. */ 2830 static void 2831 dhcp_redirect_dhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, 2832 const struct in_addr *from) 2833 { 2834 struct interface *ifn; 2835 const struct dhcp_state *state; 2836 uint32_t xid; 2837 2838 xid = ntohl(bootp->xid); 2839 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) { 2840 state = D_CSTATE(ifn); 2841 if (state == NULL || state->state == DHS_NONE) 2842 continue; 2843 if (state->xid != xid) 2844 continue; 2845 if (ifn->hwlen <= sizeof(bootp->chaddr) && 2846 memcmp(bootp->chaddr, ifn->hwaddr, ifn->hwlen)) 2847 continue; 2848 logdebugx("%s: redirecting DHCP message to %s", 2849 ifp->name, ifn->name); 2850 dhcp_handledhcp(ifn, bootp, bootp_len, from); 2851 } 2852 } 2853 2854 static void 2855 dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, 2856 const struct in_addr *from) 2857 { 2858 struct dhcp_state *state = D_STATE(ifp); 2859 struct if_options *ifo = ifp->options; 2860 struct dhcp_lease *lease = &state->lease; 2861 uint8_t type, tmp; 2862 struct in_addr addr; 2863 unsigned int i; 2864 char *msg; 2865 bool bootp_copied; 2866 #ifdef AUTH 2867 const uint8_t *auth; 2868 size_t auth_len; 2869 #endif 2870 #ifdef IN_IFF_DUPLICATED 2871 struct ipv4_addr *ia; 2872 #endif 2873 2874 #define LOGDHCP0(l, m) \ 2875 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 0) 2876 #define LOGDHCP(l, m) \ 2877 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 1) 2878 2879 #define IS_STATE_ACTIVE(s) ((s)-state != DHS_NONE && \ 2880 (s)->state != DHS_INIT && (s)->state != DHS_BOUND) 2881 2882 if (bootp->op != BOOTREPLY) { 2883 if (IS_STATE_ACTIVE(state)) 2884 logdebugx("%s: op (%d) is not BOOTREPLY", 2885 ifp->name, bootp->op); 2886 return; 2887 } 2888 2889 if (state->xid != ntohl(bootp->xid)) { 2890 if (IS_STATE_ACTIVE(state)) 2891 logdebugx("%s: wrong xid 0x%x (expecting 0x%x) from %s", 2892 ifp->name, ntohl(bootp->xid), state->xid, 2893 inet_ntoa(*from)); 2894 dhcp_redirect_dhcp(ifp, bootp, bootp_len, from); 2895 return; 2896 } 2897 2898 if (ifp->hwlen <= sizeof(bootp->chaddr) && 2899 memcmp(bootp->chaddr, ifp->hwaddr, ifp->hwlen)) 2900 { 2901 if (IS_STATE_ACTIVE(state)) { 2902 char buf[sizeof(bootp->chaddr) * 3]; 2903 2904 logdebugx("%s: xid 0x%x is for hwaddr %s", 2905 ifp->name, ntohl(bootp->xid), 2906 hwaddr_ntoa(bootp->chaddr, sizeof(bootp->chaddr), 2907 buf, sizeof(buf))); 2908 } 2909 dhcp_redirect_dhcp(ifp, bootp, bootp_len, from); 2910 return; 2911 } 2912 2913 if (!ifp->active) 2914 return; 2915 2916 i = whitelisted_ip(ifp->options, from->s_addr); 2917 switch (i) { 2918 case WHTLST_NOMATCH: 2919 logwarnx("%s: non whitelisted DHCP packet from %s", 2920 ifp->name, inet_ntoa(*from)); 2921 return; 2922 case WHTLST_MATCH: 2923 break; 2924 case WHTLST_NONE: 2925 if (blacklisted_ip(ifp->options, from->s_addr) == 1) { 2926 logwarnx("%s: blacklisted DHCP packet from %s", 2927 ifp->name, inet_ntoa(*from)); 2928 return; 2929 } 2930 } 2931 2932 /* We may have found a BOOTP server */ 2933 if (get_option_uint8(ifp->ctx, &type, 2934 bootp, bootp_len, DHO_MESSAGETYPE) == -1) 2935 type = 0; 2936 else if (ifo->options & DHCPCD_BOOTP) { 2937 logdebugx("%s: ignoring DHCP reply (expecting BOOTP)", 2938 ifp->name); 2939 return; 2940 } 2941 2942 #ifdef AUTH 2943 /* Authenticate the message */ 2944 auth = get_option(ifp->ctx, bootp, bootp_len, 2945 DHO_AUTHENTICATION, &auth_len); 2946 if (auth) { 2947 if (dhcp_auth_validate(&state->auth, &ifo->auth, 2948 (uint8_t *)bootp, bootp_len, 4, type, 2949 auth, auth_len) == NULL) 2950 { 2951 LOGDHCP0(logerrx, "authentication failed"); 2952 return; 2953 } 2954 if (state->auth.token) 2955 logdebugx("%s: validated using 0x%08" PRIu32, 2956 ifp->name, state->auth.token->secretid); 2957 else 2958 loginfox("%s: accepted reconfigure key", ifp->name); 2959 } else if (ifo->auth.options & DHCPCD_AUTH_SEND) { 2960 if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) { 2961 LOGDHCP0(logerrx, "no authentication"); 2962 return; 2963 } 2964 LOGDHCP0(logwarnx, "no authentication"); 2965 } 2966 #endif 2967 2968 /* RFC 3203 */ 2969 if (type == DHCP_FORCERENEW) { 2970 if (from->s_addr == INADDR_ANY || 2971 from->s_addr == INADDR_BROADCAST) 2972 { 2973 LOGDHCP(logerrx, "discarding Force Renew"); 2974 return; 2975 } 2976 #ifdef AUTH 2977 if (auth == NULL) { 2978 LOGDHCP(logerrx, "unauthenticated Force Renew"); 2979 if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) 2980 return; 2981 } 2982 if (state->state != DHS_BOUND && state->state != DHS_INFORM) { 2983 LOGDHCP(logdebugx, "not bound, ignoring Force Renew"); 2984 return; 2985 } 2986 LOGDHCP(loginfox, "Force Renew from"); 2987 /* The rebind and expire timings are still the same, we just 2988 * enter the renew state early */ 2989 if (state->state == DHS_BOUND) 2990 dhcp_renew(ifp); 2991 else { 2992 eloop_timeout_delete(ifp->ctx->eloop, 2993 send_inform, ifp); 2994 dhcp_inform(ifp); 2995 } 2996 #else 2997 LOGDHCP(logerrx, "unauthenticated Force Renew"); 2998 #endif 2999 return; 3000 } 3001 3002 if (state->state == DHS_BOUND) { 3003 LOGDHCP(logdebugx, "bound, ignoring"); 3004 return; 3005 } 3006 3007 if (state->state == DHS_PROBE) { 3008 /* Ignore any DHCP messages whilst probing a lease to bind. */ 3009 LOGDHCP(logdebugx, "probing, ignoring"); 3010 return; 3011 } 3012 3013 /* reset the message counter */ 3014 state->interval = 0; 3015 3016 /* Ensure that no reject options are present */ 3017 for (i = 1; i < 255; i++) { 3018 if (has_option_mask(ifo->rejectmask, i) && 3019 get_option_uint8(ifp->ctx, &tmp, 3020 bootp, bootp_len, (uint8_t)i) == 0) 3021 { 3022 LOGDHCP(logwarnx, "reject DHCP"); 3023 return; 3024 } 3025 } 3026 3027 if (type == DHCP_NAK) { 3028 /* For NAK, only check if we require the ServerID */ 3029 if (has_option_mask(ifo->requiremask, DHO_SERVERID) && 3030 get_option_addr(ifp->ctx, &addr, 3031 bootp, bootp_len, DHO_SERVERID) == -1) 3032 { 3033 LOGDHCP(logwarnx, "reject NAK"); 3034 return; 3035 } 3036 3037 /* We should restart on a NAK */ 3038 LOGDHCP(logwarnx, "NAK:"); 3039 if ((msg = get_option_string(ifp->ctx, 3040 bootp, bootp_len, DHO_MESSAGE))) 3041 { 3042 logwarnx("%s: message: %s", ifp->name, msg); 3043 free(msg); 3044 } 3045 if (state->state == DHS_INFORM) /* INFORM should not be NAKed */ 3046 return; 3047 if (!(ifp->ctx->options & DHCPCD_TEST)) { 3048 dhcp_drop(ifp, "NAK"); 3049 unlink(state->leasefile); 3050 } 3051 3052 /* If we constantly get NAKS then we should slowly back off */ 3053 eloop_timeout_add_sec(ifp->ctx->eloop, 3054 state->nakoff, dhcp_discover, ifp); 3055 if (state->nakoff == 0) 3056 state->nakoff = 1; 3057 else { 3058 state->nakoff *= 2; 3059 if (state->nakoff > NAKOFF_MAX) 3060 state->nakoff = NAKOFF_MAX; 3061 } 3062 return; 3063 } 3064 3065 /* Ensure that all required options are present */ 3066 for (i = 1; i < 255; i++) { 3067 if (has_option_mask(ifo->requiremask, i) && 3068 get_option_uint8(ifp->ctx, &tmp, 3069 bootp, bootp_len, (uint8_t)i) != 0) 3070 { 3071 /* If we are BOOTP, then ignore the need for serverid. 3072 * To ignore BOOTP, require dhcp_message_type. 3073 * However, nothing really stops BOOTP from providing 3074 * DHCP style options as well so the above isn't 3075 * always true. */ 3076 if (type == 0 && i == DHO_SERVERID) 3077 continue; 3078 LOGDHCP(logwarnx, "reject DHCP"); 3079 return; 3080 } 3081 } 3082 3083 /* DHCP Auto-Configure, RFC 2563 */ 3084 if (type == DHCP_OFFER && bootp->yiaddr == 0) { 3085 LOGDHCP(logwarnx, "no address given"); 3086 if ((msg = get_option_string(ifp->ctx, 3087 bootp, bootp_len, DHO_MESSAGE))) 3088 { 3089 logwarnx("%s: message: %s", ifp->name, msg); 3090 free(msg); 3091 } 3092 #ifdef IPV4LL 3093 if (state->state == DHS_DISCOVER && 3094 get_option_uint8(ifp->ctx, &tmp, bootp, bootp_len, 3095 DHO_AUTOCONFIGURE) == 0) 3096 { 3097 switch (tmp) { 3098 case 0: 3099 LOGDHCP(logwarnx, "IPv4LL disabled from"); 3100 ipv4ll_drop(ifp); 3101 #ifdef ARP 3102 arp_drop(ifp); 3103 #endif 3104 break; 3105 case 1: 3106 LOGDHCP(logwarnx, "IPv4LL enabled from"); 3107 ipv4ll_start(ifp); 3108 break; 3109 default: 3110 logerrx("%s: unknown auto configuration " 3111 "option %d", 3112 ifp->name, tmp); 3113 break; 3114 } 3115 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 3116 eloop_timeout_add_sec(ifp->ctx->eloop, 3117 DHCP_MAX, dhcp_discover, ifp); 3118 } 3119 #endif 3120 return; 3121 } 3122 3123 /* Ensure that the address offered is valid */ 3124 if ((type == 0 || type == DHCP_OFFER || type == DHCP_ACK) && 3125 (bootp->ciaddr == INADDR_ANY || bootp->ciaddr == INADDR_BROADCAST) 3126 && 3127 (bootp->yiaddr == INADDR_ANY || bootp->yiaddr == INADDR_BROADCAST)) 3128 { 3129 LOGDHCP(logwarnx, "reject invalid address"); 3130 return; 3131 } 3132 3133 #ifdef IN_IFF_DUPLICATED 3134 ia = ipv4_iffindaddr(ifp, &lease->addr, NULL); 3135 if (ia && ia->addr_flags & IN_IFF_DUPLICATED) { 3136 LOGDHCP(logwarnx, "declined duplicate address"); 3137 if (type) 3138 dhcp_decline(ifp); 3139 ipv4_deladdr(ia, 0); 3140 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 3141 eloop_timeout_add_sec(ifp->ctx->eloop, 3142 DHCP_RAND_MAX, dhcp_discover, ifp); 3143 return; 3144 } 3145 #endif 3146 3147 bootp_copied = false; 3148 if ((type == 0 || type == DHCP_OFFER) && state->state == DHS_DISCOVER) { 3149 lease->frominfo = 0; 3150 lease->addr.s_addr = bootp->yiaddr; 3151 memcpy(&lease->cookie, bootp->vend, sizeof(lease->cookie)); 3152 if (type == 0 || 3153 get_option_addr(ifp->ctx, 3154 &lease->server, bootp, bootp_len, DHO_SERVERID) != 0) 3155 lease->server.s_addr = INADDR_ANY; 3156 3157 /* Test for rapid commit in the OFFER */ 3158 if (!(ifp->ctx->options & DHCPCD_TEST) && 3159 has_option_mask(ifo->requestmask, DHO_RAPIDCOMMIT) && 3160 get_option(ifp->ctx, bootp, bootp_len, 3161 DHO_RAPIDCOMMIT, NULL)) 3162 { 3163 state->state = DHS_REQUEST; 3164 goto rapidcommit; 3165 } 3166 3167 LOGDHCP(loginfox, "offered"); 3168 if (state->offer_len < bootp_len) { 3169 free(state->offer); 3170 if ((state->offer = malloc(bootp_len)) == NULL) { 3171 logerr(__func__); 3172 state->offer_len = 0; 3173 return; 3174 } 3175 } 3176 state->offer_len = bootp_len; 3177 memcpy(state->offer, bootp, bootp_len); 3178 bootp_copied = true; 3179 if (ifp->ctx->options & DHCPCD_TEST) { 3180 free(state->old); 3181 state->old = state->new; 3182 state->old_len = state->new_len; 3183 state->new = state->offer; 3184 state->new_len = state->offer_len; 3185 state->offer = NULL; 3186 state->offer_len = 0; 3187 state->reason = "TEST"; 3188 script_runreason(ifp, state->reason); 3189 eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS); 3190 state->bpf_flags |= BPF_EOF; 3191 return; 3192 } 3193 eloop_timeout_delete(ifp->ctx->eloop, send_discover, ifp); 3194 /* We don't request BOOTP addresses */ 3195 if (type) { 3196 /* We used to ARP check here, but that seems to be in 3197 * violation of RFC2131 where it only describes 3198 * DECLINE after REQUEST. 3199 * It also seems that some MS DHCP servers actually 3200 * ignore DECLINE if no REQUEST, ie we decline a 3201 * DISCOVER. */ 3202 dhcp_request(ifp); 3203 return; 3204 } 3205 } 3206 3207 if (type) { 3208 if (type == DHCP_OFFER) { 3209 LOGDHCP(logwarnx, "ignoring offer of"); 3210 return; 3211 } 3212 3213 /* We should only be dealing with acks */ 3214 if (type != DHCP_ACK) { 3215 LOGDHCP(logerr, "not ACK or OFFER"); 3216 return; 3217 } 3218 3219 if (state->state == DHS_DISCOVER) { 3220 /* We only allow ACK of rapid commit DISCOVER. */ 3221 if (has_option_mask(ifo->requestmask, 3222 DHO_RAPIDCOMMIT) && 3223 get_option(ifp->ctx, bootp, bootp_len, 3224 DHO_RAPIDCOMMIT, NULL)) 3225 state->state = DHS_REQUEST; 3226 else { 3227 LOGDHCP(logdebugx, "ignoring ack of"); 3228 return; 3229 } 3230 } 3231 3232 rapidcommit: 3233 if (!(ifo->options & DHCPCD_INFORM)) 3234 LOGDHCP(logdebugx, "acknowledged"); 3235 else 3236 ifo->options &= ~DHCPCD_STATIC; 3237 } 3238 3239 /* No NAK, so reset the backoff 3240 * We don't reset on an OFFER message because the server could 3241 * potentially NAK the REQUEST. */ 3242 state->nakoff = 0; 3243 3244 /* BOOTP could have already assigned this above. */ 3245 if (!bootp_copied) { 3246 if (state->offer_len < bootp_len) { 3247 free(state->offer); 3248 if ((state->offer = malloc(bootp_len)) == NULL) { 3249 logerr(__func__); 3250 state->offer_len = 0; 3251 return; 3252 } 3253 } 3254 state->offer_len = bootp_len; 3255 memcpy(state->offer, bootp, bootp_len); 3256 } 3257 3258 lease->frominfo = 0; 3259 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); 3260 3261 #if defined(ARP) || defined(KERNEL_RFC5227) 3262 dhcp_arp_bind(ifp); 3263 #else 3264 dhcp_bind(ifp); 3265 #endif 3266 } 3267 3268 static void * 3269 get_udp_data(void *packet, size_t *len) 3270 { 3271 const struct ip *ip = packet; 3272 size_t ip_hl = (size_t)ip->ip_hl * 4; 3273 char *p = packet; 3274 3275 p += ip_hl + sizeof(struct udphdr); 3276 *len = (size_t)ntohs(ip->ip_len) - sizeof(struct udphdr) - ip_hl; 3277 return p; 3278 } 3279 3280 static bool 3281 is_packet_udp_bootp(void *packet, size_t plen) 3282 { 3283 struct ip *ip = packet; 3284 size_t ip_hlen; 3285 struct udphdr udp; 3286 3287 if (plen < sizeof(*ip)) 3288 return false; 3289 3290 if (ip->ip_v != IPVERSION || ip->ip_p != IPPROTO_UDP) 3291 return false; 3292 3293 /* Sanity. */ 3294 if (ntohs(ip->ip_len) > plen) 3295 return false; 3296 3297 ip_hlen = (size_t)ip->ip_hl * 4; 3298 if (ip_hlen < sizeof(*ip)) 3299 return false; 3300 3301 /* Check we have a UDP header and BOOTP. */ 3302 if (ip_hlen + sizeof(udp) + offsetof(struct bootp, vend) > plen) 3303 return false; 3304 3305 /* Sanity. */ 3306 memcpy(&udp, (char *)ip + ip_hlen, sizeof(udp)); 3307 if (ntohs(udp.uh_ulen) < sizeof(udp)) 3308 return false; 3309 if (ip_hlen + ntohs(udp.uh_ulen) > plen) 3310 return false; 3311 3312 /* Check it's to and from the right ports. */ 3313 if (udp.uh_dport != htons(BOOTPC) || udp.uh_sport != htons(BOOTPS)) 3314 return false; 3315 3316 return true; 3317 } 3318 3319 /* Lengths have already been checked. */ 3320 static bool 3321 checksums_valid(void *packet, 3322 struct in_addr *from, unsigned int flags) 3323 { 3324 struct ip *ip = packet; 3325 union pip { 3326 struct ip ip; 3327 uint16_t w[sizeof(struct ip) / 2]; 3328 } pip = { 3329 .ip.ip_p = IPPROTO_UDP, 3330 .ip.ip_src = ip->ip_src, 3331 .ip.ip_dst = ip->ip_dst, 3332 }; 3333 size_t ip_hlen; 3334 struct udphdr udp; 3335 char *udpp, *uh_sump; 3336 uint32_t csum; 3337 3338 if (from != NULL) 3339 from->s_addr = ip->ip_src.s_addr; 3340 3341 ip_hlen = (size_t)ip->ip_hl * 4; 3342 if (in_cksum(ip, ip_hlen, NULL) != 0) 3343 return false; 3344 3345 if (flags & BPF_PARTIALCSUM) 3346 return true; 3347 3348 udpp = (char *)ip + ip_hlen; 3349 memcpy(&udp, udpp, sizeof(udp)); 3350 if (udp.uh_sum == 0) 3351 return true; 3352 3353 /* UDP checksum is based on a pseudo IP header alongside 3354 * the UDP header and payload. */ 3355 pip.ip.ip_len = udp.uh_ulen; 3356 csum = 0; 3357 3358 /* Need to zero the UDP sum in the packet for the checksum to work. */ 3359 uh_sump = udpp + offsetof(struct udphdr, uh_sum); 3360 memset(uh_sump, 0, sizeof(udp.uh_sum)); 3361 3362 /* Checksum pseudo header and then UDP + payload. */ 3363 in_cksum(pip.w, sizeof(pip.w), &csum); 3364 csum = in_cksum(udpp, ntohs(udp.uh_ulen), &csum); 3365 3366 #if 0 /* Not needed, just here for completeness. */ 3367 /* Put the checksum back. */ 3368 memcpy(uh_sump, &udp.uh_sum, sizeof(udp.uh_sum)); 3369 #endif 3370 3371 return csum == udp.uh_sum; 3372 } 3373 3374 static void 3375 dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len, 3376 struct in_addr *from) 3377 { 3378 size_t v; 3379 3380 if (len < offsetof(struct bootp, vend)) { 3381 logerrx("%s: truncated packet (%zu) from %s", 3382 ifp->name, len, inet_ntoa(*from)); 3383 return; 3384 } 3385 3386 /* To make our IS_DHCP macro easy, ensure the vendor 3387 * area has at least 4 octets. */ 3388 v = len - offsetof(struct bootp, vend); 3389 while (v < 4) { 3390 bootp->vend[v++] = '\0'; 3391 len++; 3392 } 3393 3394 dhcp_handledhcp(ifp, bootp, len, from); 3395 } 3396 3397 static void 3398 dhcp_packet(struct interface *ifp, uint8_t *data, size_t len) 3399 { 3400 struct bootp *bootp; 3401 struct in_addr from; 3402 size_t udp_len; 3403 const struct dhcp_state *state = D_CSTATE(ifp); 3404 3405 /* Validate filter. */ 3406 if (!is_packet_udp_bootp(data, len)) { 3407 #ifdef BPF_DEBUG 3408 logerrx("%s: DHCP BPF validation failure", ifp->name); 3409 #endif 3410 return; 3411 } 3412 3413 if (!checksums_valid(data, &from, state->bpf_flags)) { 3414 logerrx("%s: checksum failure from %s", 3415 ifp->name, inet_ntoa(from)); 3416 return; 3417 } 3418 3419 /* 3420 * DHCP has a variable option area rather than a fixed vendor area. 3421 * Because DHCP uses the BOOTP protocol it should still send BOOTP 3422 * sized packets to be RFC compliant. 3423 * However some servers send a truncated vendor area. 3424 * dhcpcd can work fine without the vendor area being sent. 3425 */ 3426 bootp = get_udp_data(data, &udp_len); 3427 dhcp_handlebootp(ifp, bootp, udp_len, &from); 3428 } 3429 3430 static void 3431 dhcp_readbpf(void *arg) 3432 { 3433 struct interface *ifp = arg; 3434 uint8_t buf[MTU_MAX]; 3435 ssize_t bytes; 3436 struct dhcp_state *state = D_STATE(ifp); 3437 3438 /* Some RAW mechanisms are generic file descriptors, not sockets. 3439 * This means we have no kernel call to just get one packet, 3440 * so we have to process the entire buffer. */ 3441 state->bpf_flags &= ~BPF_EOF; 3442 state->bpf_flags |= BPF_READING; 3443 while (!(state->bpf_flags & BPF_EOF)) { 3444 bytes = bpf_read(ifp, state->bpf_fd, buf, sizeof(buf), 3445 &state->bpf_flags); 3446 if (bytes == -1) { 3447 if (state->state != DHS_NONE) { 3448 logerr("%s: %s", __func__, ifp->name); 3449 dhcp_close(ifp); 3450 } 3451 break; 3452 } 3453 dhcp_packet(ifp, buf, (size_t)bytes); 3454 /* Check we still have a state after processing. */ 3455 if ((state = D_STATE(ifp)) == NULL) 3456 break; 3457 } 3458 if (state != NULL) 3459 state->bpf_flags &= ~BPF_READING; 3460 } 3461 3462 static void 3463 dhcp_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg) 3464 { 3465 struct sockaddr_in *from = (struct sockaddr_in *)msg->msg_name; 3466 struct iovec *iov = &msg->msg_iov[0]; 3467 struct interface *ifp; 3468 const struct dhcp_state *state; 3469 3470 ifp = if_findifpfromcmsg(ctx, msg, NULL); 3471 if (ifp == NULL) { 3472 logerr(__func__); 3473 return; 3474 } 3475 state = D_CSTATE(ifp); 3476 if (state == NULL) { 3477 logdebugx("%s: received BOOTP for inactive interface", 3478 ifp->name); 3479 return; 3480 } 3481 3482 if (state->bpf_fd != -1) { 3483 /* Avoid a duplicate read if BPF is open for the interface. */ 3484 return; 3485 } 3486 3487 dhcp_handlebootp(ifp, (struct bootp *)iov->iov_base, iov->iov_len, 3488 &from->sin_addr); 3489 } 3490 3491 static void 3492 dhcp_readudp(struct dhcpcd_ctx *ctx, struct interface *ifp) 3493 { 3494 const struct dhcp_state *state; 3495 struct sockaddr_in from; 3496 unsigned char buf[10 * 1024]; /* Maximum MTU */ 3497 struct iovec iov = { 3498 .iov_base = buf, 3499 .iov_len = sizeof(buf), 3500 }; 3501 #ifdef IP_RECVIF 3502 unsigned char ctl[CMSG_SPACE(sizeof(struct sockaddr_dl))] = { 0 }; 3503 #else 3504 unsigned char ctl[CMSG_SPACE(sizeof(struct in_pktinfo))] = { 0 }; 3505 #endif 3506 struct msghdr msg = { 3507 .msg_name = &from, .msg_namelen = sizeof(from), 3508 .msg_iov = &iov, .msg_iovlen = 1, 3509 .msg_control = ctl, .msg_controllen = sizeof(ctl), 3510 }; 3511 int s; 3512 ssize_t bytes; 3513 3514 if (ifp != NULL) { 3515 state = D_CSTATE(ifp); 3516 s = state->udp_fd; 3517 } else 3518 s = ctx->udp_fd; 3519 3520 bytes = recvmsg(s, &msg, 0); 3521 if (bytes == -1) { 3522 logerr(__func__); 3523 return; 3524 } 3525 3526 iov.iov_len = (size_t)bytes; 3527 dhcp_recvmsg(ctx, &msg); 3528 } 3529 3530 static void 3531 dhcp_handleudp(void *arg) 3532 { 3533 struct dhcpcd_ctx *ctx = arg; 3534 3535 dhcp_readudp(ctx, NULL); 3536 } 3537 3538 static void 3539 dhcp_handleifudp(void *arg) 3540 { 3541 struct interface *ifp = arg; 3542 3543 dhcp_readudp(ifp->ctx, ifp); 3544 } 3545 3546 static int 3547 dhcp_open(struct dhcpcd_ctx *ctx) 3548 { 3549 3550 if (ctx->udp_fd != -1 || (ctx->udp_fd = dhcp_openudp(NULL)) == -1) 3551 return ctx->udp_fd; 3552 3553 eloop_event_add(ctx->eloop, ctx->udp_fd, dhcp_handleudp, ctx); 3554 return ctx->udp_fd; 3555 } 3556 3557 static int 3558 dhcp_openbpf(struct interface *ifp) 3559 { 3560 struct dhcp_state *state; 3561 3562 state = D_STATE(ifp); 3563 if (state->bpf_fd != -1) 3564 return 0; 3565 3566 state->bpf_fd = bpf_open(ifp, bpf_bootp); 3567 if (state->bpf_fd == -1) { 3568 if (errno == ENOENT) { 3569 logerrx("%s not found", bpf_name); 3570 /* May as well disable IPv4 entirely at 3571 * this point as we really need it. */ 3572 ifp->options->options &= ~DHCPCD_IPV4; 3573 } else 3574 logerr("%s: %s", __func__, ifp->name); 3575 return -1; 3576 } 3577 3578 eloop_event_add(ifp->ctx->eloop, 3579 state->bpf_fd, dhcp_readbpf, ifp); 3580 return 0; 3581 } 3582 3583 int 3584 dhcp_dump(struct interface *ifp) 3585 { 3586 struct dhcp_state *state; 3587 3588 ifp->if_data[IF_DATA_DHCP] = state = calloc(1, sizeof(*state)); 3589 if (state == NULL) 3590 goto eexit; 3591 state->bpf_fd = -1; 3592 dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), 3593 AF_INET, ifp); 3594 state->new_len = read_lease(ifp, &state->new); 3595 if (state->new == NULL) { 3596 logerr("%s: %s", 3597 *ifp->name ? ifp->name : state->leasefile, __func__); 3598 return -1; 3599 } 3600 state->reason = "DUMP"; 3601 return script_runreason(ifp, state->reason); 3602 3603 eexit: 3604 logerr(__func__); 3605 return -1; 3606 } 3607 3608 void 3609 dhcp_free(struct interface *ifp) 3610 { 3611 struct dhcp_state *state = D_STATE(ifp); 3612 struct dhcpcd_ctx *ctx; 3613 3614 dhcp_close(ifp); 3615 #ifdef ARP 3616 arp_drop(ifp); 3617 #endif 3618 if (state) { 3619 state->state = DHS_NONE; 3620 free(state->old); 3621 free(state->new); 3622 free(state->offer); 3623 free(state->clientid); 3624 free(state); 3625 } 3626 3627 ctx = ifp->ctx; 3628 /* If we don't have any more DHCP enabled interfaces, 3629 * close the global socket and release resources */ 3630 if (ctx->ifaces) { 3631 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 3632 state = D_STATE(ifp); 3633 if (state != NULL && state->state != DHS_NONE) 3634 break; 3635 } 3636 } 3637 if (ifp == NULL) { 3638 if (ctx->udp_fd != -1) { 3639 eloop_event_delete(ctx->eloop, ctx->udp_fd); 3640 close(ctx->udp_fd); 3641 ctx->udp_fd = -1; 3642 } 3643 3644 free(ctx->opt_buffer); 3645 ctx->opt_buffer = NULL; 3646 } 3647 } 3648 3649 static int 3650 dhcp_initstate(struct interface *ifp) 3651 { 3652 struct dhcp_state *state; 3653 3654 state = D_STATE(ifp); 3655 if (state != NULL) 3656 return 0; 3657 3658 ifp->if_data[IF_DATA_DHCP] = calloc(1, sizeof(*state)); 3659 state = D_STATE(ifp); 3660 if (state == NULL) 3661 return -1; 3662 3663 state->state = DHS_NONE; 3664 /* 0 is a valid fd, so init to -1 */ 3665 state->bpf_fd = -1; 3666 state->udp_fd = -1; 3667 #ifdef ARPING 3668 state->arping_index = -1; 3669 #endif 3670 return 1; 3671 } 3672 3673 static int 3674 dhcp_init(struct interface *ifp) 3675 { 3676 struct dhcp_state *state; 3677 const struct if_options *ifo; 3678 uint8_t len; 3679 char buf[(sizeof(ifo->clientid) - 1) * 3]; 3680 3681 if (dhcp_initstate(ifp) == -1) 3682 return -1; 3683 3684 state = D_STATE(ifp); 3685 state->state = DHS_INIT; 3686 state->reason = "PREINIT"; 3687 state->nakoff = 0; 3688 dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), 3689 AF_INET, ifp); 3690 3691 ifo = ifp->options; 3692 /* We need to drop the leasefile so that dhcp_start 3693 * doesn't load it. */ 3694 if (ifo->options & DHCPCD_REQUEST) 3695 unlink(state->leasefile); 3696 3697 free(state->clientid); 3698 state->clientid = NULL; 3699 3700 if (*ifo->clientid) { 3701 state->clientid = malloc((size_t)(ifo->clientid[0] + 1)); 3702 if (state->clientid == NULL) 3703 goto eexit; 3704 memcpy(state->clientid, ifo->clientid, 3705 (size_t)(ifo->clientid[0]) + 1); 3706 } else if (ifo->options & DHCPCD_CLIENTID) { 3707 if (ifo->options & DHCPCD_DUID) { 3708 state->clientid = malloc(ifp->ctx->duid_len + 6); 3709 if (state->clientid == NULL) 3710 goto eexit; 3711 state->clientid[0] =(uint8_t)(ifp->ctx->duid_len + 5); 3712 state->clientid[1] = 255; /* RFC 4361 */ 3713 memcpy(state->clientid + 2, ifo->iaid, 4); 3714 memcpy(state->clientid + 6, ifp->ctx->duid, 3715 ifp->ctx->duid_len); 3716 } else { 3717 len = (uint8_t)(ifp->hwlen + 1); 3718 state->clientid = malloc((size_t)len + 1); 3719 if (state->clientid == NULL) 3720 goto eexit; 3721 state->clientid[0] = len; 3722 state->clientid[1] = (uint8_t)ifp->family; 3723 memcpy(state->clientid + 2, ifp->hwaddr, 3724 ifp->hwlen); 3725 } 3726 } 3727 3728 if (ifo->options & DHCPCD_DUID) 3729 /* Don't bother logging as DUID and IAID are reported 3730 * at device start. */ 3731 return 0; 3732 3733 if (ifo->options & DHCPCD_CLIENTID) 3734 logdebugx("%s: using ClientID %s", ifp->name, 3735 hwaddr_ntoa(state->clientid + 1, state->clientid[0], 3736 buf, sizeof(buf))); 3737 else if (ifp->hwlen) 3738 logdebugx("%s: using hwaddr %s", ifp->name, 3739 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf))); 3740 return 0; 3741 3742 eexit: 3743 logerr(__func__); 3744 return -1; 3745 } 3746 3747 static void 3748 dhcp_start1(void *arg) 3749 { 3750 struct interface *ifp = arg; 3751 struct dhcpcd_ctx *ctx = ifp->ctx; 3752 struct if_options *ifo = ifp->options; 3753 struct dhcp_state *state; 3754 struct stat st; 3755 uint32_t l; 3756 int nolease; 3757 3758 if (!(ifo->options & DHCPCD_IPV4)) 3759 return; 3760 3761 /* Listen on *.*.*.*:bootpc so that the kernel never sends an 3762 * ICMP port unreachable message back to the DHCP server. 3763 * Only do this in master mode so we don't swallow messages 3764 * for dhcpcd running on another interface. */ 3765 if (ctx->options & DHCPCD_MASTER) { 3766 if (dhcp_open(ctx) == -1) { 3767 /* Don't log an error if some other process 3768 * is handling this. */ 3769 if (errno != EADDRINUSE) 3770 logerr("%s: dhcp_open", __func__); 3771 } 3772 } 3773 3774 if (dhcp_init(ifp) == -1) { 3775 logerr("%s: dhcp_init", ifp->name); 3776 return; 3777 } 3778 3779 state = D_STATE(ifp); 3780 clock_gettime(CLOCK_MONOTONIC, &state->started); 3781 state->interval = 0; 3782 free(state->offer); 3783 state->offer = NULL; 3784 state->offer_len = 0; 3785 3786 #ifdef ARPING 3787 if (ifo->arping_len && state->arping_index < ifo->arping_len) { 3788 struct arp_state *astate; 3789 3790 astate = dhcp_arp_new(ifp, NULL); 3791 if (astate) 3792 dhcp_arp_not_found(astate); 3793 return; 3794 } 3795 #endif 3796 3797 if (ifo->options & DHCPCD_STATIC) { 3798 dhcp_static(ifp); 3799 return; 3800 } 3801 3802 if (ifo->options & DHCPCD_INFORM) { 3803 dhcp_inform(ifp); 3804 return; 3805 } 3806 3807 /* We don't want to read the old lease if we NAK an old test */ 3808 nolease = state->offer && ifp->ctx->options & DHCPCD_TEST; 3809 if (!nolease && ifo->options & DHCPCD_DHCP) { 3810 state->offer_len = read_lease(ifp, &state->offer); 3811 /* Check the saved lease matches the type we want */ 3812 if (state->offer) { 3813 #ifdef IN_IFF_DUPLICATED 3814 struct in_addr addr; 3815 struct ipv4_addr *ia; 3816 3817 addr.s_addr = state->offer->yiaddr; 3818 ia = ipv4_iffindaddr(ifp, &addr, NULL); 3819 #endif 3820 3821 if ((!IS_DHCP(state->offer) && 3822 !(ifo->options & DHCPCD_BOOTP)) || 3823 #ifdef IN_IFF_DUPLICATED 3824 (ia && ia->addr_flags & IN_IFF_DUPLICATED) || 3825 #endif 3826 (IS_DHCP(state->offer) && 3827 ifo->options & DHCPCD_BOOTP)) 3828 { 3829 free(state->offer); 3830 state->offer = NULL; 3831 state->offer_len = 0; 3832 } 3833 } 3834 } 3835 if (state->offer) { 3836 struct ipv4_addr *ia; 3837 3838 get_lease(ifp, &state->lease, state->offer, state->offer_len); 3839 state->lease.frominfo = 1; 3840 if (state->new == NULL && 3841 (ia = ipv4_iffindaddr(ifp, 3842 &state->lease.addr, &state->lease.mask)) != NULL) 3843 { 3844 /* We still have the IP address from the last lease. 3845 * Fake add the address and routes from it so the lease 3846 * can be cleaned up. */ 3847 state->new = malloc(state->offer_len); 3848 if (state->new) { 3849 memcpy(state->new, 3850 state->offer, state->offer_len); 3851 state->new_len = state->offer_len; 3852 state->addr = ia; 3853 state->added |= STATE_ADDED | STATE_FAKE; 3854 rt_build(ifp->ctx, AF_INET); 3855 } else 3856 logerr(__func__); 3857 } 3858 if (!IS_DHCP(state->offer)) { 3859 free(state->offer); 3860 state->offer = NULL; 3861 state->offer_len = 0; 3862 } else if (!(ifo->options & DHCPCD_LASTLEASE_EXTEND) && 3863 state->lease.leasetime != DHCP_INFINITE_LIFETIME && 3864 stat(state->leasefile, &st) == 0) 3865 { 3866 time_t now; 3867 3868 /* Offset lease times and check expiry */ 3869 now = time(NULL); 3870 if (now == -1 || 3871 (time_t)state->lease.leasetime < now - st.st_mtime) 3872 { 3873 logdebugx("%s: discarding expired lease", 3874 ifp->name); 3875 free(state->offer); 3876 state->offer = NULL; 3877 state->offer_len = 0; 3878 state->lease.addr.s_addr = 0; 3879 /* Technically we should discard the lease 3880 * as it's expired, just as DHCPv6 addresses 3881 * would be by the kernel. 3882 * However, this may violate POLA so 3883 * we currently leave it be. 3884 * If we get a totally different lease from 3885 * the DHCP server we'll drop it anyway, as 3886 * we will on any other event which would 3887 * trigger a lease drop. 3888 * This should only happen if dhcpcd stops 3889 * running and the lease expires before 3890 * dhcpcd starts again. */ 3891 #if 0 3892 if (state->new) 3893 dhcp_drop(ifp, "EXPIRE"); 3894 #endif 3895 } else { 3896 l = (uint32_t)(now - st.st_mtime); 3897 state->lease.leasetime -= l; 3898 state->lease.renewaltime -= l; 3899 state->lease.rebindtime -= l; 3900 } 3901 } 3902 } 3903 3904 #ifdef IPV4LL 3905 if (!(ifo->options & DHCPCD_DHCP)) { 3906 if (ifo->options & DHCPCD_IPV4LL) 3907 ipv4ll_start(ifp); 3908 return; 3909 } 3910 #endif 3911 3912 if (state->offer == NULL || !IS_DHCP(state->offer)) 3913 dhcp_discover(ifp); 3914 else 3915 dhcp_reboot(ifp); 3916 } 3917 3918 void 3919 dhcp_start(struct interface *ifp) 3920 { 3921 struct timespec tv; 3922 #ifdef ARPING 3923 const struct dhcp_state *state; 3924 #endif 3925 3926 if (!(ifp->options->options & DHCPCD_IPV4)) 3927 return; 3928 3929 /* If we haven't been given a netmask for our requested address, 3930 * set it now. */ 3931 if (ifp->options->req_addr.s_addr != INADDR_ANY && 3932 ifp->options->req_mask.s_addr == INADDR_ANY) 3933 ifp->options->req_mask.s_addr = 3934 ipv4_getnetmask(ifp->options->req_addr.s_addr); 3935 3936 /* If we haven't specified a ClientID and our hardware address 3937 * length is greater than BOOTP CHADDR then we enforce a ClientID 3938 * of the hardware address family and the hardware address. 3939 * If there is no hardware address and no ClientID set, 3940 * force a DUID based ClientID. */ 3941 if (ifp->hwlen > 16) 3942 ifp->options->options |= DHCPCD_CLIENTID; 3943 else if (ifp->hwlen == 0 && !(ifp->options->options & DHCPCD_CLIENTID)) 3944 ifp->options->options |= DHCPCD_CLIENTID | DHCPCD_DUID; 3945 3946 /* Firewire and InfiniBand interfaces require ClientID and 3947 * the broadcast option being set. */ 3948 switch (ifp->family) { 3949 case ARPHRD_IEEE1394: /* FALLTHROUGH */ 3950 case ARPHRD_INFINIBAND: 3951 ifp->options->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST; 3952 break; 3953 } 3954 3955 /* If we violate RFC2131 section 3.7 then require ARP 3956 * to detect if any other client wants our address. */ 3957 if (ifp->options->options & DHCPCD_LASTLEASE_EXTEND) 3958 ifp->options->options |= DHCPCD_ARP; 3959 3960 /* No point in delaying a static configuration */ 3961 if (ifp->options->options & DHCPCD_STATIC || 3962 !(ifp->options->options & DHCPCD_INITIAL_DELAY)) 3963 { 3964 dhcp_start1(ifp); 3965 return; 3966 } 3967 3968 #ifdef ARPING 3969 /* If we have arpinged then we have already delayed. */ 3970 state = D_CSTATE(ifp); 3971 if (state != NULL && state->arping_index != -1) { 3972 dhcp_start1(ifp); 3973 return; 3974 } 3975 #endif 3976 3977 tv.tv_sec = DHCP_MIN_DELAY; 3978 tv.tv_nsec = (suseconds_t)arc4random_uniform( 3979 (DHCP_MAX_DELAY - DHCP_MIN_DELAY) * NSEC_PER_SEC); 3980 timespecnorm(&tv); 3981 logdebugx("%s: delaying IPv4 for %0.1f seconds", 3982 ifp->name, timespec_to_double(&tv)); 3983 3984 eloop_timeout_add_tv(ifp->ctx->eloop, &tv, dhcp_start1, ifp); 3985 } 3986 3987 void 3988 dhcp_abort(struct interface *ifp) 3989 { 3990 struct dhcp_state *state; 3991 3992 state = D_STATE(ifp); 3993 #ifdef ARPING 3994 if (state != NULL) 3995 state->arping_index = -1; 3996 #endif 3997 3998 eloop_timeout_delete(ifp->ctx->eloop, dhcp_start1, ifp); 3999 4000 if (state != NULL && state->added) { 4001 rt_build(ifp->ctx, AF_INET); 4002 #ifdef ARP 4003 arp_announceaddr(ifp->ctx, &state->addr->addr); 4004 #endif 4005 } 4006 } 4007 4008 struct ipv4_addr * 4009 dhcp_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) 4010 { 4011 struct interface *ifp; 4012 struct dhcp_state *state; 4013 struct if_options *ifo; 4014 uint8_t i; 4015 4016 ifp = ia->iface; 4017 state = D_STATE(ifp); 4018 if (state == NULL || state->state == DHS_NONE) 4019 return ia; 4020 4021 if (cmd == RTM_DELADDR) { 4022 if (state->addr == ia) { 4023 loginfox("%s: pid %d deleted IP address %s", 4024 ifp->name, pid, ia->saddr); 4025 state->addr = NULL; 4026 /* Don't clear the added state as we need 4027 * to drop the lease. */ 4028 dhcp_drop(ifp, "EXPIRE"); 4029 dhcp_start1(ifp); 4030 return ia; 4031 } 4032 } 4033 4034 if (cmd != RTM_NEWADDR) 4035 return ia; 4036 4037 #ifdef IN_IFF_NOTUSEABLE 4038 if (!(ia->addr_flags & IN_IFF_NOTUSEABLE)) 4039 dhcp_finish_dad(ifp, &ia->addr); 4040 else if (ia->addr_flags & IN_IFF_DUPLICATED) 4041 return dhcp_addr_duplicated(ifp, &ia->addr) ? NULL : ia; 4042 #endif 4043 4044 ifo = ifp->options; 4045 if (ifo->options & DHCPCD_INFORM) { 4046 if (state->state != DHS_INFORM) 4047 dhcp_inform(ifp); 4048 return ia; 4049 } 4050 4051 if (!(ifo->options & DHCPCD_STATIC)) 4052 return ia; 4053 if (ifo->req_addr.s_addr != INADDR_ANY) 4054 return ia; 4055 4056 free(state->old); 4057 state->old = state->new; 4058 state->new_len = dhcp_message_new(&state->new, &ia->addr, &ia->mask); 4059 if (state->new == NULL) 4060 return ia; 4061 if (ifp->flags & IFF_POINTOPOINT) { 4062 for (i = 1; i < 255; i++) 4063 if (i != DHO_ROUTER && has_option_mask(ifo->dstmask,i)) 4064 dhcp_message_add_addr(state->new, i, ia->brd); 4065 } 4066 state->reason = "STATIC"; 4067 rt_build(ifp->ctx, AF_INET); 4068 script_runreason(ifp, state->reason); 4069 if (ifo->options & DHCPCD_INFORM) { 4070 state->state = DHS_INFORM; 4071 dhcp_new_xid(ifp); 4072 state->lease.server.s_addr = INADDR_ANY; 4073 state->addr = ia; 4074 dhcp_inform(ifp); 4075 } 4076 4077 return ia; 4078 } 4079