1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2023 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 static const char dhcpcd_copyright[] = "Copyright (c) 2006-2023 Roy Marples"; 30 31 #include <sys/file.h> 32 #include <sys/ioctl.h> 33 #include <sys/socket.h> 34 #include <sys/stat.h> 35 #include <sys/time.h> 36 #include <sys/types.h> 37 #include <sys/uio.h> 38 #include <sys/wait.h> 39 40 #include <ctype.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <getopt.h> 44 #include <limits.h> 45 #include <paths.h> 46 #include <signal.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <syslog.h> 51 #include <unistd.h> 52 #include <time.h> 53 54 #include "config.h" 55 #include "arp.h" 56 #include "common.h" 57 #include "control.h" 58 #include "dev.h" 59 #include "dhcp-common.h" 60 #include "dhcpcd.h" 61 #include "dhcp.h" 62 #include "dhcp6.h" 63 #include "duid.h" 64 #include "eloop.h" 65 #include "if.h" 66 #include "if-options.h" 67 #include "ipv4.h" 68 #include "ipv4ll.h" 69 #include "ipv6.h" 70 #include "ipv6nd.h" 71 #include "logerr.h" 72 #include "privsep.h" 73 #include "script.h" 74 75 #ifdef HAVE_CAPSICUM 76 #include <sys/capsicum.h> 77 #endif 78 #ifdef HAVE_UTIL_H 79 #include <util.h> 80 #endif 81 82 #ifdef USE_SIGNALS 83 const int dhcpcd_signals[] = { 84 SIGTERM, 85 SIGINT, 86 SIGALRM, 87 SIGHUP, 88 SIGUSR1, 89 SIGUSR2, 90 SIGCHLD, 91 }; 92 const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals); 93 94 const int dhcpcd_signals_ignore[] = { 95 SIGPIPE, 96 }; 97 const size_t dhcpcd_signals_ignore_len = __arraycount(dhcpcd_signals_ignore); 98 #endif 99 100 const char *dhcpcd_default_script = SCRIPT; 101 102 static void 103 usage(void) 104 { 105 106 printf("usage: "PACKAGE"\t[-146ABbDdEGgHJKLMNPpqTV]\n" 107 "\t\t[-C, --nohook hook] [-c, --script script]\n" 108 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n" 109 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n" 110 "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n" 111 "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n" 112 "\t\t[-O, --nooption option] [-o, --option option]\n" 113 "\t\t[-Q, --require option] [-r, --request address]\n" 114 "\t\t[-S, --static value]\n" 115 "\t\t[-s, --inform address[/cidr[/broadcast_address]]]\n [--inform6]" 116 "\t\t[-t, --timeout seconds] [-u, --userclass class]\n" 117 "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n" 118 "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n" 119 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n" 120 "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n" 121 " "PACKAGE"\t-n, --rebind [interface]\n" 122 " "PACKAGE"\t-k, --release [interface]\n" 123 " "PACKAGE"\t-U, --dumplease interface\n" 124 " "PACKAGE"\t--version\n" 125 " "PACKAGE"\t-x, --exit [interface]\n"); 126 } 127 128 static void 129 free_globals(struct dhcpcd_ctx *ctx) 130 { 131 struct dhcp_opt *opt; 132 133 if (ctx->ifac) { 134 for (; ctx->ifac > 0; ctx->ifac--) 135 free(ctx->ifav[ctx->ifac - 1]); 136 free(ctx->ifav); 137 ctx->ifav = NULL; 138 } 139 if (ctx->ifdc) { 140 for (; ctx->ifdc > 0; ctx->ifdc--) 141 free(ctx->ifdv[ctx->ifdc - 1]); 142 free(ctx->ifdv); 143 ctx->ifdv = NULL; 144 } 145 if (ctx->ifcc) { 146 for (; ctx->ifcc > 0; ctx->ifcc--) 147 free(ctx->ifcv[ctx->ifcc - 1]); 148 free(ctx->ifcv); 149 ctx->ifcv = NULL; 150 } 151 152 #ifdef INET 153 if (ctx->dhcp_opts) { 154 for (opt = ctx->dhcp_opts; 155 ctx->dhcp_opts_len > 0; 156 opt++, ctx->dhcp_opts_len--) 157 free_dhcp_opt_embenc(opt); 158 free(ctx->dhcp_opts); 159 ctx->dhcp_opts = NULL; 160 } 161 #endif 162 #ifdef INET6 163 if (ctx->nd_opts) { 164 for (opt = ctx->nd_opts; 165 ctx->nd_opts_len > 0; 166 opt++, ctx->nd_opts_len--) 167 free_dhcp_opt_embenc(opt); 168 free(ctx->nd_opts); 169 ctx->nd_opts = NULL; 170 } 171 #ifdef DHCP6 172 if (ctx->dhcp6_opts) { 173 for (opt = ctx->dhcp6_opts; 174 ctx->dhcp6_opts_len > 0; 175 opt++, ctx->dhcp6_opts_len--) 176 free_dhcp_opt_embenc(opt); 177 free(ctx->dhcp6_opts); 178 ctx->dhcp6_opts = NULL; 179 } 180 #endif 181 #endif 182 if (ctx->vivso) { 183 for (opt = ctx->vivso; 184 ctx->vivso_len > 0; 185 opt++, ctx->vivso_len--) 186 free_dhcp_opt_embenc(opt); 187 free(ctx->vivso); 188 ctx->vivso = NULL; 189 } 190 } 191 192 static void 193 handle_exit_timeout(void *arg) 194 { 195 struct dhcpcd_ctx *ctx; 196 197 ctx = arg; 198 logerrx("timed out"); 199 if (!(ctx->options & DHCPCD_MANAGER)) { 200 struct interface *ifp; 201 202 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 203 if (ifp->active == IF_ACTIVE_USER) 204 script_runreason(ifp, "STOPPED"); 205 } 206 eloop_exit(ctx->eloop, EXIT_FAILURE); 207 return; 208 } 209 ctx->options |= DHCPCD_NOWAITIP; 210 dhcpcd_daemonise(ctx); 211 } 212 213 static const char * 214 dhcpcd_af(int af) 215 { 216 217 switch (af) { 218 case AF_UNSPEC: 219 return "IP"; 220 case AF_INET: 221 return "IPv4"; 222 case AF_INET6: 223 return "IPv6"; 224 default: 225 return NULL; 226 } 227 } 228 229 int 230 dhcpcd_ifafwaiting(const struct interface *ifp) 231 { 232 unsigned long long opts; 233 bool foundany = false; 234 235 if (ifp->active != IF_ACTIVE_USER) 236 return AF_MAX; 237 238 #define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6) 239 opts = ifp->options->options; 240 #ifdef INET 241 if (opts & DHCPCD_WAITIP4 || 242 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL))) 243 { 244 bool foundaddr = ipv4_hasaddr(ifp); 245 246 if (opts & DHCPCD_WAITIP4 && !foundaddr) 247 return AF_INET; 248 if (foundaddr) 249 foundany = true; 250 } 251 #endif 252 #ifdef INET6 253 if (opts & DHCPCD_WAITIP6 || 254 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL))) 255 { 256 bool foundaddr = ipv6_hasaddr(ifp); 257 258 if (opts & DHCPCD_WAITIP6 && !foundaddr) 259 return AF_INET; 260 if (foundaddr) 261 foundany = true; 262 } 263 #endif 264 265 if (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL) && !foundany) 266 return AF_UNSPEC; 267 return AF_MAX; 268 } 269 270 int 271 dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx) 272 { 273 unsigned long long opts; 274 const struct interface *ifp; 275 int af; 276 277 if (!(ctx->options & DHCPCD_WAITOPTS)) 278 return AF_MAX; 279 280 opts = ctx->options; 281 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 282 #ifdef INET 283 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) && 284 ipv4_hasaddr(ifp)) 285 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4); 286 #endif 287 #ifdef INET6 288 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) && 289 ipv6_hasaddr(ifp)) 290 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6); 291 #endif 292 if (!(opts & DHCPCD_WAITOPTS)) 293 break; 294 } 295 if (opts & DHCPCD_WAITIP) 296 af = AF_UNSPEC; 297 else if (opts & DHCPCD_WAITIP4) 298 af = AF_INET; 299 else if (opts & DHCPCD_WAITIP6) 300 af = AF_INET6; 301 else 302 return AF_MAX; 303 return af; 304 } 305 306 static int 307 dhcpcd_ipwaited(struct dhcpcd_ctx *ctx) 308 { 309 struct interface *ifp; 310 int af; 311 312 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 313 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) { 314 logdebugx("%s: waiting for an %s address", 315 ifp->name, dhcpcd_af(af)); 316 return 0; 317 } 318 } 319 320 if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) { 321 logdebugx("waiting for an %s address", 322 dhcpcd_af(af)); 323 return 0; 324 } 325 326 return 1; 327 } 328 329 /* Returns the pid of the child, otherwise 0. */ 330 void 331 dhcpcd_daemonise(struct dhcpcd_ctx *ctx) 332 { 333 #ifdef THERE_IS_NO_FORK 334 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx); 335 errno = ENOSYS; 336 return; 337 #else 338 int i; 339 unsigned int logopts = loggetopts(); 340 341 if (ctx->options & DHCPCD_DAEMONISE && 342 !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP))) 343 { 344 if (!dhcpcd_ipwaited(ctx)) 345 return; 346 } 347 348 if (ctx->options & DHCPCD_ONESHOT) { 349 loginfox("exiting due to oneshot"); 350 eloop_exit(ctx->eloop, EXIT_SUCCESS); 351 return; 352 } 353 354 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx); 355 if (ctx->options & DHCPCD_DAEMONISED || 356 !(ctx->options & DHCPCD_DAEMONISE)) 357 return; 358 359 /* Don't use loginfo because this makes no sense in a log. */ 360 if (!(logopts & LOGERR_QUIET) && ctx->stderr_valid) 361 (void)fprintf(stderr, 362 "forked to background, child pid %d\n", getpid()); 363 i = EXIT_SUCCESS; 364 if (write(ctx->fork_fd, &i, sizeof(i)) == -1) 365 logerr("write"); 366 ctx->options |= DHCPCD_DAEMONISED; 367 eloop_event_delete(ctx->eloop, ctx->fork_fd); 368 close(ctx->fork_fd); 369 ctx->fork_fd = -1; 370 371 /* 372 * Stop writing to stderr. 373 * On the happy path, only the manager process writes to stderr, 374 * so this just stops wasting fprintf calls to nowhere. 375 * All other calls - ie errors in privsep processes or script output, 376 * will error when printing. 377 * If we *really* want to fix that, then we need to suck 378 * stderr/stdout in the manager process and either disacrd it or pass 379 * it to the launcher process and then to stderr. 380 */ 381 logopts &= ~LOGERR_ERR; 382 logsetopts(logopts); 383 #endif 384 } 385 386 static void 387 dhcpcd_drop(struct interface *ifp, int stop) 388 { 389 390 #ifdef DHCP6 391 dhcp6_drop(ifp, stop ? NULL : "EXPIRE6"); 392 #endif 393 #ifdef INET6 394 ipv6nd_drop(ifp); 395 ipv6_drop(ifp); 396 #endif 397 #ifdef IPV4LL 398 ipv4ll_drop(ifp); 399 #endif 400 #ifdef INET 401 dhcp_drop(ifp, stop ? "STOP" : "EXPIRE"); 402 #endif 403 #ifdef ARP 404 arp_drop(ifp); 405 #endif 406 #if !defined(DHCP6) && !defined(DHCP) 407 UNUSED(stop); 408 #endif 409 } 410 411 static void 412 stop_interface(struct interface *ifp, const char *reason) 413 { 414 struct dhcpcd_ctx *ctx; 415 416 ctx = ifp->ctx; 417 loginfox("%s: removing interface", ifp->name); 418 ifp->options->options |= DHCPCD_STOPPING; 419 420 dhcpcd_drop(ifp, 1); 421 script_runreason(ifp, reason == NULL ? "STOPPED" : reason); 422 423 /* Delete all timeouts for the interfaces */ 424 eloop_q_timeout_delete(ctx->eloop, ELOOP_QUEUE_ALL, NULL, ifp); 425 426 /* De-activate the interface */ 427 ifp->active = IF_INACTIVE; 428 ifp->options->options &= ~DHCPCD_STOPPING; 429 430 if (!(ctx->options & (DHCPCD_MANAGER | DHCPCD_TEST))) 431 eloop_exit(ctx->eloop, EXIT_FAILURE); 432 } 433 434 static void 435 configure_interface1(struct interface *ifp) 436 { 437 struct if_options *ifo = ifp->options; 438 439 /* Do any platform specific configuration */ 440 if_conf(ifp); 441 442 /* If we want to release a lease, we can't really persist the 443 * address either. */ 444 if (ifo->options & DHCPCD_RELEASE) 445 ifo->options &= ~DHCPCD_PERSISTENT; 446 447 if (ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) { 448 ifo->options &= ~DHCPCD_ARP; 449 if (!(ifp->flags & IFF_MULTICAST)) 450 ifo->options &= ~DHCPCD_IPV6RS; 451 if (!(ifo->options & (DHCPCD_INFORM | DHCPCD_WANTDHCP))) 452 ifo->options |= DHCPCD_STATIC; 453 } 454 455 if (ifo->metric != -1) 456 ifp->metric = (unsigned int)ifo->metric; 457 458 #ifdef INET6 459 /* We want to setup INET6 on the interface as soon as possible. */ 460 if (ifp->active == IF_ACTIVE_USER && 461 ifo->options & DHCPCD_IPV6 && 462 !(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) 463 { 464 /* If not doing any DHCP, disable the RDNSS requirement. */ 465 if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6))) 466 ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS; 467 if_setup_inet6(ifp); 468 } 469 #endif 470 471 if (!(ifo->options & DHCPCD_IAID)) { 472 /* 473 * An IAID is for identifying a unqiue interface within 474 * the client. It is 4 bytes long. Working out a default 475 * value is problematic. 476 * 477 * Interface name and number are not stable 478 * between different OS's. Some OS's also cannot make 479 * up their mind what the interface should be called 480 * (yes, udev, I'm looking at you). 481 * Also, the name could be longer than 4 bytes. 482 * Also, with pluggable interfaces the name and index 483 * could easily get swapped per actual interface. 484 * 485 * The MAC address is 6 bytes long, the final 3 486 * being unique to the manufacturer and the initial 3 487 * being unique to the organisation which makes it. 488 * We could use the last 4 bytes of the MAC address 489 * as the IAID as it's the most stable part given the 490 * above, but equally it's not guaranteed to be 491 * unique. 492 * 493 * Given the above, and our need to reliably work 494 * between reboots without persitent storage, 495 * generating the IAID from the MAC address is the only 496 * logical default. 497 * Saying that, if a VLANID has been specified then we 498 * can use that. It's possible that different interfaces 499 * can have the same VLANID, but this is no worse than 500 * generating the IAID from the duplicate MAC address. 501 * 502 * dhclient uses the last 4 bytes of the MAC address. 503 * dibbler uses an increamenting counter. 504 * wide-dhcpv6 uses 0 or a configured value. 505 * odhcp6c uses 1. 506 * Windows 7 uses the first 3 bytes of the MAC address 507 * and an unknown byte. 508 * dhcpcd-6.1.0 and earlier used the interface name, 509 * falling back to interface index if name > 4. 510 */ 511 if (ifp->vlanid != 0) { 512 uint32_t vlanid; 513 514 /* Maximal VLANID is 4095, so prefix with 0xff 515 * so we don't conflict with an interface index. */ 516 vlanid = htonl(ifp->vlanid | 0xff000000); 517 memcpy(ifo->iaid, &vlanid, sizeof(vlanid)); 518 } else if (ifo->options & DHCPCD_ANONYMOUS) 519 memset(ifo->iaid, 0, sizeof(ifo->iaid)); 520 else if (ifp->hwlen >= sizeof(ifo->iaid)) { 521 memcpy(ifo->iaid, 522 ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid), 523 sizeof(ifo->iaid)); 524 } else { 525 uint32_t len; 526 527 len = (uint32_t)strlen(ifp->name); 528 if (len <= sizeof(ifo->iaid)) { 529 memcpy(ifo->iaid, ifp->name, len); 530 if (len < sizeof(ifo->iaid)) 531 memset(ifo->iaid + len, 0, 532 sizeof(ifo->iaid) - len); 533 } else { 534 /* IAID is the same size as a uint32_t */ 535 len = htonl(ifp->index); 536 memcpy(ifo->iaid, &len, sizeof(ifo->iaid)); 537 } 538 } 539 ifo->options |= DHCPCD_IAID; 540 } 541 542 #ifdef DHCP6 543 if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 && 544 ifp->name[0] != '\0') 545 { 546 ifo->ia = malloc(sizeof(*ifo->ia)); 547 if (ifo->ia == NULL) 548 logerr(__func__); 549 else { 550 ifo->ia_len = 1; 551 ifo->ia->ia_type = D6_OPTION_IA_NA; 552 memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid)); 553 memset(&ifo->ia->addr, 0, sizeof(ifo->ia->addr)); 554 #ifndef SMALL 555 ifo->ia->sla = NULL; 556 ifo->ia->sla_len = 0; 557 #endif 558 } 559 } else { 560 size_t i; 561 562 for (i = 0; i < ifo->ia_len; i++) { 563 if (!ifo->ia[i].iaid_set) { 564 memcpy(&ifo->ia[i].iaid, ifo->iaid, 565 sizeof(ifo->ia[i].iaid)); 566 ifo->ia[i].iaid_set = 1; 567 } 568 } 569 } 570 #endif 571 572 /* If root is network mounted, we don't want to kill the connection 573 * if the DHCP server goes the way of the dodo OR dhcpcd is rebooting 574 * and the lease file has expired. */ 575 if (is_root_local() == 0) 576 ifo->options |= DHCPCD_LASTLEASE_EXTEND; 577 } 578 579 int 580 dhcpcd_selectprofile(struct interface *ifp, const char *profile) 581 { 582 struct if_options *ifo; 583 char pssid[PROFILE_LEN]; 584 585 if (ifp->ssid_len) { 586 ssize_t r; 587 588 r = print_string(pssid, sizeof(pssid), OT_ESCSTRING, 589 ifp->ssid, ifp->ssid_len); 590 if (r == -1) { 591 logerr(__func__); 592 pssid[0] = '\0'; 593 } 594 } else 595 pssid[0] = '\0'; 596 ifo = read_config(ifp->ctx, ifp->name, pssid, profile); 597 if (ifo == NULL) { 598 logdebugx("%s: no profile %s", ifp->name, profile); 599 return -1; 600 } 601 if (profile != NULL) { 602 strlcpy(ifp->profile, profile, sizeof(ifp->profile)); 603 loginfox("%s: selected profile %s", ifp->name, profile); 604 } else 605 *ifp->profile = '\0'; 606 607 free_options(ifp->ctx, ifp->options); 608 ifp->options = ifo; 609 if (profile) { 610 add_options(ifp->ctx, ifp->name, ifp->options, 611 ifp->ctx->argc, ifp->ctx->argv); 612 configure_interface1(ifp); 613 } 614 return 1; 615 } 616 617 static void 618 configure_interface(struct interface *ifp, int argc, char **argv, 619 unsigned long long options) 620 { 621 time_t old; 622 623 old = ifp->options ? ifp->options->mtime : 0; 624 dhcpcd_selectprofile(ifp, NULL); 625 if (ifp->options == NULL) { 626 /* dhcpcd cannot continue with this interface. */ 627 ifp->active = IF_INACTIVE; 628 return; 629 } 630 add_options(ifp->ctx, ifp->name, ifp->options, argc, argv); 631 ifp->options->options |= options; 632 configure_interface1(ifp); 633 634 /* If the mtime has changed drop any old lease */ 635 if (old != 0 && ifp->options->mtime != old) { 636 logwarnx("%s: config file changed, expiring leases", 637 ifp->name); 638 dhcpcd_drop(ifp, 0); 639 } 640 } 641 642 static void 643 dhcpcd_initstate2(struct interface *ifp, unsigned long long options) 644 { 645 struct if_options *ifo; 646 647 if (options) { 648 if ((ifo = default_config(ifp->ctx)) == NULL) { 649 logerr(__func__); 650 return; 651 } 652 ifo->options |= options; 653 free(ifp->options); 654 ifp->options = ifo; 655 } else 656 ifo = ifp->options; 657 658 #ifdef INET6 659 if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == -1) { 660 logerr(__func__); 661 ifo->options &= ~DHCPCD_IPV6; 662 } 663 #endif 664 } 665 666 static void 667 dhcpcd_initstate1(struct interface *ifp, int argc, char **argv, 668 unsigned long long options) 669 { 670 671 configure_interface(ifp, argc, argv, options); 672 if (ifp->active) 673 dhcpcd_initstate2(ifp, 0); 674 } 675 676 static void 677 dhcpcd_initstate(struct interface *ifp, unsigned long long options) 678 { 679 680 dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options); 681 } 682 683 static void 684 dhcpcd_reportssid(struct interface *ifp) 685 { 686 char pssid[IF_SSIDLEN * 4]; 687 688 if (print_string(pssid, sizeof(pssid), OT_ESCSTRING, 689 ifp->ssid, ifp->ssid_len) == -1) 690 { 691 logerr(__func__); 692 return; 693 } 694 695 loginfox("%s: connected to Access Point: %s", ifp->name, pssid); 696 } 697 698 static void 699 dhcpcd_nocarrier_roaming(struct interface *ifp) 700 { 701 702 loginfox("%s: carrier lost - roaming", ifp->name); 703 704 #ifdef ARP 705 arp_drop(ifp); 706 #endif 707 #ifdef INET 708 dhcp_abort(ifp); 709 #endif 710 #ifdef DHCP6 711 dhcp6_abort(ifp); 712 #endif 713 714 rt_build(ifp->ctx, AF_UNSPEC); 715 script_runreason(ifp, "NOCARRIER_ROAMING"); 716 } 717 718 void 719 dhcpcd_handlecarrier(struct interface *ifp, int carrier, unsigned int flags) 720 { 721 bool was_link_up = if_is_link_up(ifp); 722 bool was_roaming = if_roaming(ifp); 723 724 ifp->carrier = carrier; 725 ifp->flags = flags; 726 727 if (!if_is_link_up(ifp)) { 728 if (!ifp->active || (!was_link_up && !was_roaming)) 729 return; 730 731 /* 732 * If the interface is roaming (generally on wireless) 733 * then while we are not up, we are not down either. 734 * Preserve the network state until we either disconnect 735 * or re-connect. 736 */ 737 if (!ifp->options->randomise_hwaddr && if_roaming(ifp)) { 738 dhcpcd_nocarrier_roaming(ifp); 739 return; 740 } 741 742 loginfox("%s: carrier lost", ifp->name); 743 script_runreason(ifp, "NOCARRIER"); 744 dhcpcd_drop(ifp, 0); 745 746 if (ifp->options->randomise_hwaddr) { 747 bool is_up = ifp->flags & IFF_UP; 748 749 if (is_up) 750 if_down(ifp); 751 if (if_randomisemac(ifp) == -1 && errno != ENXIO) 752 logerr(__func__); 753 if (is_up) 754 if_up(ifp); 755 } 756 757 return; 758 } 759 760 /* 761 * At this point carrier is NOT DOWN and we have IFF_UP. 762 * We should treat LINK_UNKNOWN as up as the driver may not support 763 * link state changes. 764 * The consideration of any other information about carrier should 765 * be handled in the OS specific if_carrier() function. 766 */ 767 if (was_link_up) 768 return; 769 770 if (ifp->active) { 771 if (carrier == LINK_UNKNOWN) 772 loginfox("%s: carrier unknown, assuming up", ifp->name); 773 else 774 loginfox("%s: carrier acquired", ifp->name); 775 } 776 777 #if !defined(__linux__) && !defined(__NetBSD__) 778 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the 779 * hardware address changes so we have to go 780 * through the disovery process to work it out. */ 781 dhcpcd_handleinterface(ifp->ctx, 0, ifp->name); 782 #endif 783 784 if (ifp->wireless) { 785 uint8_t ossid[IF_SSIDLEN]; 786 size_t olen; 787 788 olen = ifp->ssid_len; 789 memcpy(ossid, ifp->ssid, ifp->ssid_len); 790 if_getssid(ifp); 791 792 /* If we changed SSID network, drop leases */ 793 if ((ifp->ssid_len != olen || 794 memcmp(ifp->ssid, ossid, ifp->ssid_len)) && ifp->active) 795 { 796 dhcpcd_reportssid(ifp); 797 dhcpcd_drop(ifp, 0); 798 #ifdef IPV4LL 799 ipv4ll_reset(ifp); 800 #endif 801 } 802 } 803 804 if (!ifp->active) 805 return; 806 807 dhcpcd_initstate(ifp, 0); 808 script_runreason(ifp, "CARRIER"); 809 810 #ifdef INET6 811 /* Set any IPv6 Routers we remembered to expire faster than they 812 * would normally as we maybe on a new network. */ 813 ipv6nd_startexpire(ifp); 814 #ifdef IPV6_MANAGETEMPADDR 815 /* RFC4941 Section 3.5 */ 816 ipv6_regentempaddrs(ifp); 817 #endif 818 #endif 819 820 dhcpcd_startinterface(ifp); 821 } 822 823 static void 824 warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid) 825 { 826 struct interface *ifn; 827 #ifdef INET6 828 size_t i; 829 struct if_ia *ia; 830 #endif 831 832 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) { 833 if (ifn == ifp || !ifn->active) 834 continue; 835 if (ifn->options->options & DHCPCD_ANONYMOUS) 836 continue; 837 if (ia_type == 0 && 838 memcmp(ifn->options->iaid, iaid, 839 sizeof(ifn->options->iaid)) == 0) 840 break; 841 #ifdef INET6 842 for (i = 0; i < ifn->options->ia_len; i++) { 843 ia = &ifn->options->ia[i]; 844 if (ia->ia_type == ia_type && 845 memcmp(ia->iaid, iaid, sizeof(ia->iaid)) == 0) 846 break; 847 } 848 #endif 849 } 850 851 /* This is only a problem if the interfaces are on the same network. */ 852 if (ifn) 853 logerrx("%s: IAID conflicts with one assigned to %s", 854 ifp->name, ifn->name); 855 } 856 857 static void 858 dhcpcd_initduid(struct dhcpcd_ctx *ctx, struct interface *ifp) 859 { 860 char buf[DUID_LEN * 3]; 861 862 if (ctx->duid != NULL) { 863 if (ifp == NULL) 864 goto log; 865 return; 866 } 867 868 duid_init(ctx, ifp); 869 if (ctx->duid == NULL) 870 return; 871 872 log: 873 loginfox("DUID %s", 874 hwaddr_ntoa(ctx->duid, ctx->duid_len, buf, sizeof(buf))); 875 } 876 877 void 878 dhcpcd_startinterface(void *arg) 879 { 880 struct interface *ifp = arg; 881 struct if_options *ifo = ifp->options; 882 883 if (ifo->options & DHCPCD_LINK && !if_is_link_up(ifp)) { 884 loginfox("%s: waiting for carrier", ifp->name); 885 return; 886 } 887 888 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6) && 889 !(ifo->options & DHCPCD_ANONYMOUS)) 890 { 891 char buf[sizeof(ifo->iaid) * 3]; 892 #ifdef INET6 893 size_t i; 894 struct if_ia *ia; 895 #endif 896 897 /* Try and init DUID from the interface hardware address */ 898 dhcpcd_initduid(ifp->ctx, ifp); 899 900 /* Report IAIDs */ 901 loginfox("%s: IAID %s", ifp->name, 902 hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid), 903 buf, sizeof(buf))); 904 warn_iaid_conflict(ifp, 0, ifo->iaid); 905 906 #ifdef INET6 907 for (i = 0; i < ifo->ia_len; i++) { 908 ia = &ifo->ia[i]; 909 if (memcmp(ifo->iaid, ia->iaid, sizeof(ifo->iaid))) { 910 loginfox("%s: IA type %u IAID %s", 911 ifp->name, ia->ia_type, 912 hwaddr_ntoa(ia->iaid, sizeof(ia->iaid), 913 buf, sizeof(buf))); 914 warn_iaid_conflict(ifp, ia->ia_type, ia->iaid); 915 } 916 } 917 #endif 918 } 919 920 #ifdef INET6 921 if (ifo->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) { 922 logerr("%s: ipv6_start", ifp->name); 923 ifo->options &= ~DHCPCD_IPV6; 924 } 925 926 if (ifo->options & DHCPCD_IPV6) { 927 if (ifp->active == IF_ACTIVE_USER) { 928 ipv6_startstatic(ifp); 929 930 if (ifo->options & DHCPCD_IPV6RS) 931 ipv6nd_startrs(ifp); 932 } 933 934 #ifdef DHCP6 935 /* DHCPv6 could be turned off, but the interface 936 * is still delegated to. */ 937 if (ifp->active) 938 dhcp6_find_delegates(ifp); 939 940 if (ifo->options & DHCPCD_DHCP6) { 941 if (ifp->active == IF_ACTIVE_USER) { 942 enum DH6S d6_state; 943 944 if (ifo->options & DHCPCD_IA_FORCED) 945 d6_state = DH6S_INIT; 946 else if (ifo->options & DHCPCD_INFORM6) 947 d6_state = DH6S_INFORM; 948 else 949 d6_state = DH6S_CONFIRM; 950 if (dhcp6_start(ifp, d6_state) == -1) 951 logerr("%s: dhcp6_start", ifp->name); 952 } 953 } 954 #endif 955 } 956 #endif 957 958 #ifdef INET 959 if (ifo->options & DHCPCD_IPV4 && ifp->active == IF_ACTIVE_USER) { 960 /* Ensure we have an IPv4 state before starting DHCP */ 961 if (ipv4_getstate(ifp) != NULL) 962 dhcp_start(ifp); 963 } 964 #endif 965 } 966 967 static void 968 dhcpcd_prestartinterface(void *arg) 969 { 970 struct interface *ifp = arg; 971 struct dhcpcd_ctx *ctx = ifp->ctx; 972 bool randmac_down; 973 974 if (ifp->carrier <= LINK_DOWN && 975 ifp->options->randomise_hwaddr && 976 ifp->flags & IFF_UP) 977 { 978 if_down(ifp); 979 randmac_down = true; 980 } else 981 randmac_down = false; 982 983 if ((!(ctx->options & DHCPCD_MANAGER) || 984 ifp->options->options & DHCPCD_IF_UP || randmac_down) && 985 !(ifp->flags & IFF_UP)) 986 { 987 if (ifp->options->randomise_hwaddr && 988 if_randomisemac(ifp) == -1) 989 logerr(__func__); 990 if (if_up(ifp) == -1) 991 logerr(__func__); 992 } 993 994 dhcpcd_startinterface(ifp); 995 } 996 997 static void 998 run_preinit(struct interface *ifp) 999 { 1000 1001 if (ifp->ctx->options & DHCPCD_TEST) 1002 return; 1003 1004 script_runreason(ifp, "PREINIT"); 1005 if (ifp->wireless && if_is_link_up(ifp)) 1006 dhcpcd_reportssid(ifp); 1007 if (ifp->options->options & DHCPCD_LINK && ifp->carrier != LINK_UNKNOWN) 1008 script_runreason(ifp, 1009 ifp->carrier == LINK_UP ? "CARRIER" : "NOCARRIER"); 1010 } 1011 1012 void 1013 dhcpcd_activateinterface(struct interface *ifp, unsigned long long options) 1014 { 1015 1016 if (ifp->active) 1017 return; 1018 1019 ifp->active = IF_ACTIVE; 1020 dhcpcd_initstate2(ifp, options); 1021 1022 /* It's possible we might not have been able to load 1023 * a config. */ 1024 if (!ifp->active) 1025 return; 1026 1027 configure_interface1(ifp); 1028 run_preinit(ifp); 1029 dhcpcd_prestartinterface(ifp); 1030 } 1031 1032 int 1033 dhcpcd_handleinterface(void *arg, int action, const char *ifname) 1034 { 1035 struct dhcpcd_ctx *ctx = arg; 1036 struct ifaddrs *ifaddrs; 1037 struct if_head *ifs; 1038 struct interface *ifp, *iff; 1039 const char * const argv[] = { ifname }; 1040 int e; 1041 1042 if (action == -1) { 1043 ifp = if_find(ctx->ifaces, ifname); 1044 if (ifp == NULL) { 1045 errno = ESRCH; 1046 return -1; 1047 } 1048 if (ifp->active) { 1049 logdebugx("%s: interface departed", ifp->name); 1050 stop_interface(ifp, "DEPARTED"); 1051 } 1052 TAILQ_REMOVE(ctx->ifaces, ifp, next); 1053 if_free(ifp); 1054 return 0; 1055 } 1056 1057 ifs = if_discover(ctx, &ifaddrs, -1, UNCONST(argv)); 1058 if (ifs == NULL) { 1059 logerr(__func__); 1060 return -1; 1061 } 1062 1063 ifp = if_find(ifs, ifname); 1064 if (ifp == NULL) { 1065 /* This can happen if an interface is quickly added 1066 * and then removed. */ 1067 errno = ENOENT; 1068 e = -1; 1069 goto out; 1070 } 1071 e = 1; 1072 1073 /* Check if we already have the interface */ 1074 iff = if_find(ctx->ifaces, ifp->name); 1075 1076 if (iff != NULL) { 1077 if (iff->active) 1078 logdebugx("%s: interface updated", iff->name); 1079 /* The flags and hwaddr could have changed */ 1080 iff->flags = ifp->flags; 1081 iff->hwlen = ifp->hwlen; 1082 if (ifp->hwlen != 0) 1083 memcpy(iff->hwaddr, ifp->hwaddr, iff->hwlen); 1084 } else { 1085 TAILQ_REMOVE(ifs, ifp, next); 1086 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next); 1087 if (ifp->active) { 1088 logdebugx("%s: interface added", ifp->name); 1089 dhcpcd_initstate(ifp, 0); 1090 run_preinit(ifp); 1091 } 1092 iff = ifp; 1093 } 1094 1095 if (action > 0) { 1096 if_learnaddrs(ctx, ifs, &ifaddrs); 1097 if (iff->active) 1098 dhcpcd_prestartinterface(iff); 1099 } 1100 1101 out: 1102 /* Free our discovered list */ 1103 while ((ifp = TAILQ_FIRST(ifs))) { 1104 TAILQ_REMOVE(ifs, ifp, next); 1105 if_free(ifp); 1106 } 1107 free(ifs); 1108 if_freeifaddrs(ctx, &ifaddrs); 1109 1110 return e; 1111 } 1112 1113 static void 1114 dhcpcd_handlelink(void *arg, unsigned short events) 1115 { 1116 struct dhcpcd_ctx *ctx = arg; 1117 1118 if (events != ELE_READ) 1119 logerrx("%s: unexpected event 0x%04x", __func__, events); 1120 1121 if (if_handlelink(ctx) == -1) { 1122 if (errno == ENOBUFS || errno == ENOMEM) { 1123 dhcpcd_linkoverflow(ctx); 1124 return; 1125 } 1126 if (errno != ENOTSUP) 1127 logerr(__func__); 1128 } 1129 } 1130 1131 static void 1132 dhcpcd_checkcarrier(void *arg) 1133 { 1134 struct interface *ifp0 = arg, *ifp; 1135 1136 ifp = if_find(ifp0->ctx->ifaces, ifp0->name); 1137 if (ifp == NULL || ifp->carrier == ifp0->carrier) 1138 return; 1139 1140 dhcpcd_handlecarrier(ifp, ifp0->carrier, ifp0->flags); 1141 if_free(ifp0); 1142 } 1143 1144 #ifndef SMALL 1145 static void 1146 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx *ctx) 1147 { 1148 socklen_t socklen; 1149 1150 if (ctx->link_rcvbuf == 0) 1151 return; 1152 1153 logdebugx("setting route socket receive buffer size to %d bytes", 1154 ctx->link_rcvbuf); 1155 1156 socklen = sizeof(ctx->link_rcvbuf); 1157 if (setsockopt(ctx->link_fd, SOL_SOCKET, 1158 SO_RCVBUF, &ctx->link_rcvbuf, socklen) == -1) 1159 logerr(__func__); 1160 } 1161 #endif 1162 1163 static void 1164 dhcpcd_runprestartinterface(void *arg) 1165 { 1166 struct interface *ifp = arg; 1167 1168 run_preinit(ifp); 1169 dhcpcd_prestartinterface(ifp); 1170 } 1171 1172 void 1173 dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx) 1174 { 1175 socklen_t socklen; 1176 int rcvbuflen; 1177 char buf[2048]; 1178 ssize_t rlen; 1179 size_t rcnt; 1180 struct if_head *ifaces; 1181 struct ifaddrs *ifaddrs; 1182 struct interface *ifp, *ifn, *ifp1; 1183 1184 socklen = sizeof(rcvbuflen); 1185 if (getsockopt(ctx->link_fd, SOL_SOCKET, 1186 SO_RCVBUF, &rcvbuflen, &socklen) == -1) { 1187 logerr("%s: getsockopt", __func__); 1188 rcvbuflen = 0; 1189 } 1190 #ifdef __linux__ 1191 else 1192 rcvbuflen /= 2; 1193 #endif 1194 1195 logerrx("route socket overflowed (rcvbuflen %d)" 1196 " - learning interface state", rcvbuflen); 1197 1198 /* Drain the socket. 1199 * We cannot open a new one due to privsep. */ 1200 rcnt = 0; 1201 do { 1202 rlen = read(ctx->link_fd, buf, sizeof(buf)); 1203 if (++rcnt % 1000 == 0) 1204 logwarnx("drained %zu messages", rcnt); 1205 } while (rlen != -1 || errno == ENOBUFS || errno == ENOMEM); 1206 if (rcnt % 1000 != 0) 1207 logwarnx("drained %zu messages", rcnt); 1208 1209 /* Work out the current interfaces. */ 1210 ifaces = if_discover(ctx, &ifaddrs, ctx->ifc, ctx->ifv); 1211 if (ifaces == NULL) { 1212 logerr(__func__); 1213 return; 1214 } 1215 1216 /* Punt departed interfaces */ 1217 TAILQ_FOREACH_SAFE(ifp, ctx->ifaces, next, ifn) { 1218 if (if_find(ifaces, ifp->name) != NULL) 1219 continue; 1220 dhcpcd_handleinterface(ctx, -1, ifp->name); 1221 } 1222 1223 /* Add new interfaces */ 1224 while ((ifp = TAILQ_FIRST(ifaces)) != NULL ) { 1225 TAILQ_REMOVE(ifaces, ifp, next); 1226 ifp1 = if_find(ctx->ifaces, ifp->name); 1227 if (ifp1 != NULL) { 1228 /* If the interface already exists, 1229 * check carrier state. 1230 * dhcpcd_checkcarrier will free ifp. */ 1231 eloop_timeout_add_sec(ctx->eloop, 0, 1232 dhcpcd_checkcarrier, ifp); 1233 continue; 1234 } 1235 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next); 1236 if (ifp->active) { 1237 dhcpcd_initstate(ifp, 0); 1238 eloop_timeout_add_sec(ctx->eloop, 0, 1239 dhcpcd_runprestartinterface, ifp); 1240 } 1241 } 1242 free(ifaces); 1243 1244 /* Update address state. */ 1245 if_markaddrsstale(ctx->ifaces); 1246 if_learnaddrs(ctx, ctx->ifaces, &ifaddrs); 1247 if_deletestaleaddrs(ctx->ifaces); 1248 if_freeifaddrs(ctx, &ifaddrs); 1249 } 1250 1251 void 1252 dhcpcd_handlehwaddr(struct interface *ifp, 1253 uint16_t hwtype, const void *hwaddr, uint8_t hwlen) 1254 { 1255 char buf[sizeof(ifp->hwaddr) * 3]; 1256 1257 if (hwaddr == NULL || !if_valid_hwaddr(hwaddr, hwlen)) 1258 hwlen = 0; 1259 1260 if (hwlen > sizeof(ifp->hwaddr)) { 1261 errno = ENOBUFS; 1262 logerr("%s: %s", __func__, ifp->name); 1263 return; 1264 } 1265 1266 if (ifp->hwtype != hwtype) { 1267 if (ifp->active) 1268 loginfox("%s: hardware address type changed" 1269 " from %d to %d", ifp->name, ifp->hwtype, hwtype); 1270 ifp->hwtype = hwtype; 1271 } 1272 1273 if (ifp->hwlen == hwlen && 1274 (hwlen == 0 || memcmp(ifp->hwaddr, hwaddr, hwlen) == 0)) 1275 return; 1276 1277 if (ifp->active) { 1278 loginfox("%s: old hardware address: %s", ifp->name, 1279 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf))); 1280 loginfox("%s: new hardware address: %s", ifp->name, 1281 hwaddr_ntoa(hwaddr, hwlen, buf, sizeof(buf))); 1282 } 1283 ifp->hwlen = hwlen; 1284 if (hwaddr != NULL) 1285 memcpy(ifp->hwaddr, hwaddr, hwlen); 1286 } 1287 1288 static void 1289 if_reboot(struct interface *ifp, int argc, char **argv) 1290 { 1291 #ifdef INET 1292 unsigned long long oldopts; 1293 1294 oldopts = ifp->options->options; 1295 #endif 1296 script_runreason(ifp, "RECONFIGURE"); 1297 dhcpcd_initstate1(ifp, argc, argv, 0); 1298 #ifdef INET 1299 dhcp_reboot_newopts(ifp, oldopts); 1300 #endif 1301 #ifdef DHCP6 1302 dhcp6_reboot(ifp); 1303 #endif 1304 dhcpcd_prestartinterface(ifp); 1305 } 1306 1307 static void 1308 reload_config(struct dhcpcd_ctx *ctx) 1309 { 1310 struct if_options *ifo; 1311 1312 free_globals(ctx); 1313 if ((ifo = read_config(ctx, NULL, NULL, NULL)) == NULL) 1314 return; 1315 add_options(ctx, NULL, ifo, ctx->argc, ctx->argv); 1316 /* We need to preserve these options. */ 1317 if (ctx->options & DHCPCD_STARTED) 1318 ifo->options |= DHCPCD_STARTED; 1319 if (ctx->options & DHCPCD_MANAGER) 1320 ifo->options |= DHCPCD_MANAGER; 1321 if (ctx->options & DHCPCD_DAEMONISED) 1322 ifo->options |= DHCPCD_DAEMONISED; 1323 if (ctx->options & DHCPCD_PRIVSEP) 1324 ifo->options |= DHCPCD_PRIVSEP; 1325 ctx->options = ifo->options; 1326 free_options(ctx, ifo); 1327 } 1328 1329 static void 1330 reconf_reboot(struct dhcpcd_ctx *ctx, int action, int argc, char **argv, int oi) 1331 { 1332 int i; 1333 struct interface *ifp; 1334 1335 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 1336 for (i = oi; i < argc; i++) { 1337 if (strcmp(ifp->name, argv[i]) == 0) 1338 break; 1339 } 1340 if (oi != argc && i == argc) 1341 continue; 1342 if (ifp->active == IF_ACTIVE_USER) { 1343 if (action) 1344 if_reboot(ifp, argc, argv); 1345 #ifdef INET 1346 else 1347 ipv4_applyaddr(ifp); 1348 #endif 1349 } else if (i != argc) { 1350 ifp->active = IF_ACTIVE_USER; 1351 dhcpcd_initstate1(ifp, argc, argv, 0); 1352 run_preinit(ifp); 1353 dhcpcd_prestartinterface(ifp); 1354 } 1355 } 1356 } 1357 1358 static void 1359 stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts) 1360 { 1361 struct interface *ifp; 1362 1363 ctx->options |= DHCPCD_EXITING; 1364 if (ctx->ifaces == NULL) 1365 return; 1366 1367 /* Drop the last interface first */ 1368 TAILQ_FOREACH_REVERSE(ifp, ctx->ifaces, if_head, next) { 1369 if (!ifp->active) 1370 continue; 1371 ifp->options->options |= opts; 1372 if (ifp->options->options & DHCPCD_RELEASE) 1373 ifp->options->options &= ~DHCPCD_PERSISTENT; 1374 ifp->options->options |= DHCPCD_EXITING; 1375 stop_interface(ifp, NULL); 1376 } 1377 } 1378 1379 static void 1380 dhcpcd_ifrenew(struct interface *ifp) 1381 { 1382 1383 if (!ifp->active) 1384 return; 1385 1386 if (ifp->options->options & DHCPCD_LINK && !if_is_link_up(ifp)) 1387 return; 1388 1389 #ifdef INET 1390 dhcp_renew(ifp); 1391 #endif 1392 #ifdef INET6 1393 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS) 1394 if ((ifp->options->options & DHCPCD_RARENEW) == DHCPCD_RARENEW) 1395 ipv6nd_startrs(ifp); 1396 #endif 1397 #ifdef DHCP6 1398 dhcp6_renew(ifp); 1399 #endif 1400 } 1401 1402 static void 1403 dhcpcd_renew(struct dhcpcd_ctx *ctx) 1404 { 1405 struct interface *ifp; 1406 1407 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 1408 dhcpcd_ifrenew(ifp); 1409 } 1410 } 1411 1412 #ifdef USE_SIGNALS 1413 #define sigmsg "received %s, %s" 1414 void 1415 dhcpcd_signal_cb(int sig, void *arg) 1416 { 1417 struct dhcpcd_ctx *ctx = arg; 1418 unsigned long long opts; 1419 int exit_code; 1420 1421 if (ctx->options & DHCPCD_DUMPLEASE) { 1422 eloop_exit(ctx->eloop, EXIT_FAILURE); 1423 return; 1424 } 1425 1426 if (sig != SIGCHLD && ctx->options & DHCPCD_FORKED) { 1427 if (sig != SIGHUP && 1428 write(ctx->fork_fd, &sig, sizeof(sig)) == -1) 1429 logerr("%s: write", __func__); 1430 return; 1431 } 1432 1433 opts = 0; 1434 exit_code = EXIT_FAILURE; 1435 switch (sig) { 1436 case SIGINT: 1437 loginfox(sigmsg, "SIGINT", "stopping"); 1438 break; 1439 case SIGTERM: 1440 loginfox(sigmsg, "SIGTERM", "stopping"); 1441 exit_code = EXIT_SUCCESS; 1442 break; 1443 case SIGALRM: 1444 loginfox(sigmsg, "SIGALRM", "releasing"); 1445 opts |= DHCPCD_RELEASE; 1446 exit_code = EXIT_SUCCESS; 1447 break; 1448 case SIGHUP: 1449 loginfox(sigmsg, "SIGHUP", "rebinding"); 1450 reload_config(ctx); 1451 /* Preserve any options passed on the commandline 1452 * when we were started. */ 1453 reconf_reboot(ctx, 1, ctx->argc, ctx->argv, 1454 ctx->argc - ctx->ifc); 1455 return; 1456 case SIGUSR1: 1457 loginfox(sigmsg, "SIGUSR1", "renewing"); 1458 dhcpcd_renew(ctx); 1459 return; 1460 case SIGUSR2: 1461 loginfox(sigmsg, "SIGUSR2", "reopening log"); 1462 #ifdef PRIVSEP 1463 if (IN_PRIVSEP(ctx)) { 1464 if (ps_root_logreopen(ctx) == -1) 1465 logerr("ps_root_logreopen"); 1466 return; 1467 } 1468 #endif 1469 if (logopen(ctx->logfile) == -1) 1470 logerr("logopen"); 1471 return; 1472 case SIGCHLD: 1473 #ifdef PRIVSEP 1474 ps_root_signalcb(sig, ctx); 1475 #else 1476 while (waitpid(-1, NULL, WNOHANG) > 0) 1477 ; 1478 #endif 1479 return; 1480 default: 1481 logerrx("received signal %d but don't know what to do with it", 1482 sig); 1483 return; 1484 } 1485 1486 if (!(ctx->options & DHCPCD_TEST)) 1487 stop_all_interfaces(ctx, opts); 1488 eloop_exit(ctx->eloop, exit_code); 1489 } 1490 #endif 1491 1492 int 1493 dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, 1494 int argc, char **argv) 1495 { 1496 struct interface *ifp; 1497 unsigned long long opts; 1498 int opt, oi, do_reboot, do_renew, af = AF_UNSPEC; 1499 size_t len, l, nifaces; 1500 char *tmp, *p; 1501 1502 /* Special commands for our control socket 1503 * as the other end should be blocking until it gets the 1504 * expected reply we should be safely able just to change the 1505 * write callback on the fd */ 1506 /* Make any change here in privsep-control.c as well. */ 1507 if (strcmp(*argv, "--version") == 0) { 1508 return control_queue(fd, UNCONST(VERSION), 1509 strlen(VERSION) + 1); 1510 } else if (strcmp(*argv, "--getconfigfile") == 0) { 1511 return control_queue(fd, UNCONST(fd->ctx->cffile), 1512 strlen(fd->ctx->cffile) + 1); 1513 } else if (strcmp(*argv, "--getinterfaces") == 0) { 1514 optind = argc = 0; 1515 goto dumplease; 1516 } else if (strcmp(*argv, "--listen") == 0) { 1517 fd->flags |= FD_LISTEN; 1518 return 0; 1519 } 1520 1521 /* Log the command */ 1522 len = 1; 1523 for (opt = 0; opt < argc; opt++) 1524 len += strlen(argv[opt]) + 1; 1525 tmp = malloc(len); 1526 if (tmp == NULL) 1527 return -1; 1528 p = tmp; 1529 for (opt = 0; opt < argc; opt++) { 1530 l = strlen(argv[opt]); 1531 strlcpy(p, argv[opt], len); 1532 len -= l + 1; 1533 p += l; 1534 *p++ = ' '; 1535 } 1536 *--p = '\0'; 1537 loginfox("control command: %s", tmp); 1538 free(tmp); 1539 1540 optind = 0; 1541 oi = 0; 1542 opts = 0; 1543 do_reboot = do_renew = 0; 1544 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1) 1545 { 1546 switch (opt) { 1547 case 'g': 1548 /* Assumed if below not set */ 1549 break; 1550 case 'k': 1551 opts |= DHCPCD_RELEASE; 1552 break; 1553 case 'n': 1554 do_reboot = 1; 1555 break; 1556 case 'p': 1557 opts |= DHCPCD_PERSISTENT; 1558 break; 1559 case 'x': 1560 opts |= DHCPCD_EXITING; 1561 break; 1562 case 'N': 1563 do_renew = 1; 1564 break; 1565 case 'U': 1566 opts |= DHCPCD_DUMPLEASE; 1567 break; 1568 case '4': 1569 af = AF_INET; 1570 break; 1571 case '6': 1572 af = AF_INET6; 1573 break; 1574 } 1575 } 1576 1577 if (opts & DHCPCD_DUMPLEASE) { 1578 ctx->options |= DHCPCD_DUMPLEASE; 1579 dumplease: 1580 nifaces = 0; 1581 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 1582 if (!ifp->active) 1583 continue; 1584 for (oi = optind; oi < argc; oi++) { 1585 if (strcmp(ifp->name, argv[oi]) == 0) 1586 break; 1587 } 1588 if (optind == argc || oi < argc) { 1589 opt = send_interface(NULL, ifp, af); 1590 if (opt == -1) 1591 goto dumperr; 1592 nifaces += (size_t)opt; 1593 } 1594 } 1595 if (write(fd->fd, &nifaces, sizeof(nifaces)) != sizeof(nifaces)) 1596 goto dumperr; 1597 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 1598 if (!ifp->active) 1599 continue; 1600 for (oi = optind; oi < argc; oi++) { 1601 if (strcmp(ifp->name, argv[oi]) == 0) 1602 break; 1603 } 1604 if (optind == argc || oi < argc) { 1605 if (send_interface(fd, ifp, af) == -1) 1606 goto dumperr; 1607 } 1608 } 1609 ctx->options &= ~DHCPCD_DUMPLEASE; 1610 return 0; 1611 dumperr: 1612 ctx->options &= ~DHCPCD_DUMPLEASE; 1613 return -1; 1614 } 1615 1616 /* Only privileged users can control dhcpcd via the socket. */ 1617 if (fd->flags & FD_UNPRIV) { 1618 errno = EPERM; 1619 return -1; 1620 } 1621 1622 if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) { 1623 if (optind == argc) { 1624 stop_all_interfaces(ctx, opts); 1625 eloop_exit(ctx->eloop, EXIT_SUCCESS); 1626 return 0; 1627 } 1628 for (oi = optind; oi < argc; oi++) { 1629 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL) 1630 continue; 1631 if (!ifp->active) 1632 continue; 1633 ifp->options->options |= opts; 1634 if (opts & DHCPCD_RELEASE) 1635 ifp->options->options &= ~DHCPCD_PERSISTENT; 1636 stop_interface(ifp, NULL); 1637 } 1638 return 0; 1639 } 1640 1641 if (do_renew) { 1642 if (optind == argc) { 1643 dhcpcd_renew(ctx); 1644 return 0; 1645 } 1646 for (oi = optind; oi < argc; oi++) { 1647 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL) 1648 continue; 1649 dhcpcd_ifrenew(ifp); 1650 } 1651 return 0; 1652 } 1653 1654 reload_config(ctx); 1655 /* XXX: Respect initial commandline options? */ 1656 reconf_reboot(ctx, do_reboot, argc, argv, optind - 1); 1657 return 0; 1658 } 1659 1660 static void dhcpcd_readdump1(void *, unsigned short); 1661 1662 static void 1663 dhcpcd_readdump2(void *arg, unsigned short events) 1664 { 1665 struct dhcpcd_ctx *ctx = arg; 1666 ssize_t len; 1667 int exit_code = EXIT_FAILURE; 1668 1669 if (events != ELE_READ) 1670 logerrx("%s: unexpected event 0x%04x", __func__, events); 1671 1672 len = read(ctx->control_fd, ctx->ctl_buf + ctx->ctl_bufpos, 1673 ctx->ctl_buflen - ctx->ctl_bufpos); 1674 if (len == -1) { 1675 logerr(__func__); 1676 goto finished; 1677 } else if (len == 0) 1678 goto finished; 1679 if ((size_t)len + ctx->ctl_bufpos != ctx->ctl_buflen) { 1680 ctx->ctl_bufpos += (size_t)len; 1681 return; 1682 } 1683 1684 if (ctx->ctl_buf[ctx->ctl_buflen - 1] != '\0') /* unlikely */ 1685 ctx->ctl_buf[ctx->ctl_buflen - 1] = '\0'; 1686 script_dump(ctx->ctl_buf, ctx->ctl_buflen); 1687 fflush(stdout); 1688 if (--ctx->ctl_extra != 0) { 1689 putchar('\n'); 1690 if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, 1691 dhcpcd_readdump1, ctx) == -1) 1692 logerr("%s: eloop_event_add", __func__); 1693 return; 1694 } 1695 exit_code = EXIT_SUCCESS; 1696 1697 finished: 1698 shutdown(ctx->control_fd, SHUT_RDWR); 1699 eloop_exit(ctx->eloop, exit_code); 1700 } 1701 1702 static void 1703 dhcpcd_readdump1(void *arg, unsigned short events) 1704 { 1705 struct dhcpcd_ctx *ctx = arg; 1706 ssize_t len; 1707 1708 if (events != ELE_READ) 1709 logerrx("%s: unexpected event 0x%04x", __func__, events); 1710 1711 len = read(ctx->control_fd, &ctx->ctl_buflen, sizeof(ctx->ctl_buflen)); 1712 if (len != sizeof(ctx->ctl_buflen)) { 1713 if (len != -1) 1714 errno = EINVAL; 1715 goto err; 1716 } 1717 if (ctx->ctl_buflen > SSIZE_MAX) { 1718 errno = ENOBUFS; 1719 goto err; 1720 } 1721 1722 free(ctx->ctl_buf); 1723 ctx->ctl_buf = malloc(ctx->ctl_buflen); 1724 if (ctx->ctl_buf == NULL) 1725 goto err; 1726 1727 ctx->ctl_bufpos = 0; 1728 if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, 1729 dhcpcd_readdump2, ctx) == -1) 1730 logerr("%s: eloop_event_add", __func__); 1731 return; 1732 1733 err: 1734 logerr(__func__); 1735 eloop_exit(ctx->eloop, EXIT_FAILURE); 1736 } 1737 1738 static void 1739 dhcpcd_readdump0(void *arg, unsigned short events) 1740 { 1741 struct dhcpcd_ctx *ctx = arg; 1742 ssize_t len; 1743 1744 if (events != ELE_READ) 1745 logerrx("%s: unexpected event 0x%04x", __func__, events); 1746 1747 len = read(ctx->control_fd, &ctx->ctl_extra, sizeof(ctx->ctl_extra)); 1748 if (len != sizeof(ctx->ctl_extra)) { 1749 if (len != -1) 1750 errno = EINVAL; 1751 logerr(__func__); 1752 eloop_exit(ctx->eloop, EXIT_FAILURE); 1753 return; 1754 } 1755 1756 if (ctx->ctl_extra == 0) { 1757 eloop_exit(ctx->eloop, EXIT_SUCCESS); 1758 return; 1759 } 1760 1761 if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, 1762 dhcpcd_readdump1, ctx) == -1) 1763 logerr("%s: eloop_event_add", __func__); 1764 } 1765 1766 static void 1767 dhcpcd_readdumptimeout(void *arg) 1768 { 1769 struct dhcpcd_ctx *ctx = arg; 1770 1771 logerrx(__func__); 1772 eloop_exit(ctx->eloop, EXIT_FAILURE); 1773 } 1774 1775 static int 1776 dhcpcd_readdump(struct dhcpcd_ctx *ctx) 1777 { 1778 1779 ctx->options |= DHCPCD_FORKED; 1780 if (eloop_timeout_add_sec(ctx->eloop, 5, 1781 dhcpcd_readdumptimeout, ctx) == -1) 1782 return -1; 1783 return eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, 1784 dhcpcd_readdump0, ctx); 1785 } 1786 1787 static void 1788 dhcpcd_fork_cb(void *arg, unsigned short events) 1789 { 1790 struct dhcpcd_ctx *ctx = arg; 1791 int exit_code; 1792 ssize_t len; 1793 1794 if (!(events & ELE_READ)) 1795 logerrx("%s: unexpected event 0x%04x", __func__, events); 1796 1797 len = read(ctx->fork_fd, &exit_code, sizeof(exit_code)); 1798 if (len == -1) { 1799 logerr(__func__); 1800 exit_code = EXIT_FAILURE; 1801 } else if ((size_t)len < sizeof(exit_code)) { 1802 logerrx("%s: truncated read %zd (expected %zu)", 1803 __func__, len, sizeof(exit_code)); 1804 exit_code = EXIT_FAILURE; 1805 } 1806 if (ctx->options & DHCPCD_FORKED) 1807 eloop_exit(ctx->eloop, exit_code); 1808 else 1809 dhcpcd_signal_cb(exit_code, ctx); 1810 } 1811 1812 static void 1813 dhcpcd_stderr_cb(void *arg, unsigned short events) 1814 { 1815 struct dhcpcd_ctx *ctx = arg; 1816 char log[BUFSIZ]; 1817 ssize_t len; 1818 1819 if (events != ELE_READ) 1820 logerrx("%s: unexpected event 0x%04x", __func__, events); 1821 1822 len = read(ctx->stderr_fd, log, sizeof(log)); 1823 if (len == -1) { 1824 if (errno != ECONNRESET) 1825 logerr(__func__); 1826 return; 1827 } 1828 1829 log[len] = '\0'; 1830 fprintf(stderr, "%s", log); 1831 } 1832 1833 static void 1834 dhcpcd_pidfile_timeout(void *arg) 1835 { 1836 struct dhcpcd_ctx *ctx = arg; 1837 pid_t pid; 1838 1839 pid = pidfile_read(ctx->pidfile); 1840 1841 if(pid == -1) 1842 eloop_exit(ctx->eloop, EXIT_SUCCESS); 1843 else if (++ctx->duid_len >= 100) { /* overload duid_len */ 1844 logerrx("pid %d failed to exit", pid); 1845 eloop_exit(ctx->eloop, EXIT_FAILURE); 1846 } else 1847 eloop_timeout_add_msec(ctx->eloop, 100, 1848 dhcpcd_pidfile_timeout, ctx); 1849 } 1850 1851 int 1852 main(int argc, char **argv, char **envp) 1853 { 1854 struct dhcpcd_ctx ctx; 1855 struct ifaddrs *ifaddrs = NULL; 1856 struct if_options *ifo; 1857 struct interface *ifp; 1858 sa_family_t family = AF_UNSPEC; 1859 int opt, oi = 0, i; 1860 unsigned int logopts, t; 1861 ssize_t len; 1862 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK) 1863 pid_t pid; 1864 int fork_fd[2], stderr_fd[2]; 1865 #endif 1866 #ifdef USE_SIGNALS 1867 int sig = 0; 1868 const char *siga = NULL; 1869 size_t si; 1870 #endif 1871 1872 #ifdef SETPROCTITLE_H 1873 setproctitle_init(argc, argv, envp); 1874 #else 1875 UNUSED(envp); 1876 #endif 1877 1878 /* Test for --help and --version */ 1879 if (argc > 1) { 1880 if (strcmp(argv[1], "--help") == 0) { 1881 usage(); 1882 return EXIT_SUCCESS; 1883 } else if (strcmp(argv[1], "--version") == 0) { 1884 printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright); 1885 printf("Compiled in features:" 1886 #ifdef INET 1887 " INET" 1888 #endif 1889 #ifdef ARP 1890 " ARP" 1891 #endif 1892 #ifdef ARPING 1893 " ARPing" 1894 #endif 1895 #ifdef IPV4LL 1896 " IPv4LL" 1897 #endif 1898 #ifdef INET6 1899 " INET6" 1900 #endif 1901 #ifdef DHCP6 1902 " DHCPv6" 1903 #endif 1904 #ifdef AUTH 1905 " AUTH" 1906 #endif 1907 #ifdef PRIVSEP 1908 " PRIVSEP" 1909 #endif 1910 "\n"); 1911 return EXIT_SUCCESS; 1912 } 1913 } 1914 1915 memset(&ctx, 0, sizeof(ctx)); 1916 1917 ifo = NULL; 1918 ctx.cffile = CONFIG; 1919 ctx.script = UNCONST(dhcpcd_default_script); 1920 ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1; 1921 ctx.pf_inet_fd = -1; 1922 #ifdef PF_LINK 1923 ctx.pf_link_fd = -1; 1924 #endif 1925 1926 TAILQ_INIT(&ctx.control_fds); 1927 #ifdef USE_SIGNALS 1928 ctx.fork_fd = -1; 1929 #endif 1930 #ifdef PLUGIN_DEV 1931 ctx.dev_fd = -1; 1932 #endif 1933 #ifdef INET 1934 ctx.udp_rfd = -1; 1935 ctx.udp_wfd = -1; 1936 #endif 1937 #if defined(INET6) && !defined(__sun) 1938 ctx.nd_fd = -1; 1939 #endif 1940 #ifdef DHCP6 1941 ctx.dhcp6_rfd = -1; 1942 ctx.dhcp6_wfd = -1; 1943 #endif 1944 #ifdef PRIVSEP 1945 ctx.ps_log_fd = -1; 1946 TAILQ_INIT(&ctx.ps_processes); 1947 #endif 1948 1949 /* Check our streams for validity */ 1950 ctx.stdin_valid = fcntl(STDIN_FILENO, F_GETFD) != -1; 1951 ctx.stdout_valid = fcntl(STDOUT_FILENO, F_GETFD) != -1; 1952 ctx.stderr_valid = fcntl(STDERR_FILENO, F_GETFD) != -1; 1953 1954 logopts = LOGERR_LOG | LOGERR_LOG_DATE | LOGERR_LOG_PID; 1955 if (ctx.stderr_valid) 1956 logopts |= LOGERR_ERR; 1957 1958 i = 0; 1959 1960 while ((opt = getopt_long(argc, argv, 1961 ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, 1962 cf_options, &oi)) != -1) 1963 { 1964 switch (opt) { 1965 case '4': 1966 family = AF_INET; 1967 break; 1968 case '6': 1969 family = AF_INET6; 1970 break; 1971 case 'f': 1972 ctx.cffile = optarg; 1973 break; 1974 case 'j': 1975 free(ctx.logfile); 1976 ctx.logfile = strdup(optarg); 1977 break; 1978 #ifdef USE_SIGNALS 1979 case 'k': 1980 sig = SIGALRM; 1981 siga = "ALRM"; 1982 break; 1983 case 'n': 1984 sig = SIGHUP; 1985 siga = "HUP"; 1986 break; 1987 case 'q': 1988 /* -qq disables console output entirely. 1989 * This is important for systemd because it logs 1990 * both console AND syslog to the same log 1991 * resulting in untold confusion. */ 1992 if (logopts & LOGERR_QUIET) 1993 logopts &= ~LOGERR_ERR; 1994 else 1995 logopts |= LOGERR_QUIET; 1996 break; 1997 case 'x': 1998 sig = SIGTERM; 1999 siga = "TERM"; 2000 break; 2001 case 'N': 2002 sig = SIGUSR1; 2003 siga = "USR1"; 2004 break; 2005 #endif 2006 case 'P': 2007 ctx.options |= DHCPCD_PRINT_PIDFILE; 2008 logopts &= ~(LOGERR_LOG | LOGERR_ERR); 2009 break; 2010 case 'T': 2011 i = 1; 2012 logopts &= ~LOGERR_LOG; 2013 break; 2014 case 'U': 2015 i = 3; 2016 break; 2017 case 'V': 2018 i = 2; 2019 break; 2020 case '?': 2021 if (ctx.options & DHCPCD_PRINT_PIDFILE) 2022 continue; 2023 usage(); 2024 goto exit_failure; 2025 } 2026 } 2027 2028 if (optind != argc - 1) 2029 ctx.options |= DHCPCD_MANAGER; 2030 2031 logsetopts(logopts); 2032 logopen(ctx.logfile); 2033 2034 ctx.argv = argv; 2035 ctx.argc = argc; 2036 ctx.ifc = argc - optind; 2037 ctx.ifv = argv + optind; 2038 2039 rt_init(&ctx); 2040 2041 ifo = read_config(&ctx, NULL, NULL, NULL); 2042 if (ifo == NULL) { 2043 if (ctx.options & DHCPCD_PRINT_PIDFILE) 2044 goto printpidfile; 2045 goto exit_failure; 2046 } 2047 2048 opt = add_options(&ctx, NULL, ifo, argc, argv); 2049 if (opt != 1) { 2050 if (ctx.options & DHCPCD_PRINT_PIDFILE) 2051 goto printpidfile; 2052 if (opt == 0) 2053 usage(); 2054 goto exit_failure; 2055 } 2056 if (i == 2) { 2057 printf("Interface options:\n"); 2058 if (optind == argc - 1) { 2059 free_options(&ctx, ifo); 2060 ifo = read_config(&ctx, argv[optind], NULL, NULL); 2061 if (ifo == NULL) 2062 goto exit_failure; 2063 add_options(&ctx, NULL, ifo, argc, argv); 2064 } 2065 if_printoptions(); 2066 #ifdef INET 2067 if (family == 0 || family == AF_INET) { 2068 printf("\nDHCPv4 options:\n"); 2069 dhcp_printoptions(&ctx, 2070 ifo->dhcp_override, ifo->dhcp_override_len); 2071 } 2072 #endif 2073 #ifdef INET6 2074 if (family == 0 || family == AF_INET6) { 2075 printf("\nND options:\n"); 2076 ipv6nd_printoptions(&ctx, 2077 ifo->nd_override, ifo->nd_override_len); 2078 #ifdef DHCP6 2079 printf("\nDHCPv6 options:\n"); 2080 dhcp6_printoptions(&ctx, 2081 ifo->dhcp6_override, ifo->dhcp6_override_len); 2082 #endif 2083 } 2084 #endif 2085 goto exit_success; 2086 } 2087 ctx.options |= ifo->options; 2088 2089 if (i == 1 || i == 3) { 2090 if (i == 1) 2091 ctx.options |= DHCPCD_TEST; 2092 else 2093 ctx.options |= DHCPCD_DUMPLEASE; 2094 ctx.options |= DHCPCD_PERSISTENT; 2095 ctx.options &= ~DHCPCD_DAEMONISE; 2096 } 2097 2098 #ifdef THERE_IS_NO_FORK 2099 ctx.options &= ~DHCPCD_DAEMONISE; 2100 #endif 2101 2102 if (ctx.options & DHCPCD_DEBUG) 2103 logsetopts(logopts | LOGERR_DEBUG); 2104 2105 if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) { 2106 printpidfile: 2107 /* If we have any other args, we should run as a single dhcpcd 2108 * instance for that interface. */ 2109 if (optind == argc - 1 && !(ctx.options & DHCPCD_MANAGER)) { 2110 const char *per; 2111 const char *ifname; 2112 2113 ifname = *ctx.ifv; 2114 if (ifname == NULL || strlen(ifname) > IF_NAMESIZE) { 2115 errno = ifname == NULL ? EINVAL : E2BIG; 2116 logerr("%s: ", ifname); 2117 goto exit_failure; 2118 } 2119 /* Allow a dhcpcd interface per address family */ 2120 switch(family) { 2121 case AF_INET: 2122 per = "-4"; 2123 break; 2124 case AF_INET6: 2125 per = "-6"; 2126 break; 2127 default: 2128 per = ""; 2129 } 2130 snprintf(ctx.pidfile, sizeof(ctx.pidfile), 2131 PIDFILE, ifname, per, "."); 2132 } else { 2133 snprintf(ctx.pidfile, sizeof(ctx.pidfile), 2134 PIDFILE, "", "", ""); 2135 ctx.options |= DHCPCD_MANAGER; 2136 2137 /* 2138 * If we are given any interfaces, we 2139 * cannot send a signal as that would impact 2140 * other interfaces. 2141 */ 2142 if (optind != argc) 2143 sig = 0; 2144 } 2145 if (ctx.options & DHCPCD_PRINT_PIDFILE) { 2146 printf("%s\n", ctx.pidfile); 2147 goto exit_success; 2148 } 2149 } 2150 2151 if (chdir("/") == -1) 2152 logerr("%s: chdir: /", __func__); 2153 2154 /* Freeing allocated addresses from dumping leases can trigger 2155 * eloop removals as well, so init here. */ 2156 if ((ctx.eloop = eloop_new()) == NULL) { 2157 logerr("%s: eloop_init", __func__); 2158 goto exit_failure; 2159 } 2160 2161 #ifdef USE_SIGNALS 2162 for (si = 0; si < dhcpcd_signals_ignore_len; si++) 2163 signal(dhcpcd_signals_ignore[si], SIG_IGN); 2164 2165 /* Save signal mask, block and redirect signals to our handler */ 2166 eloop_signal_set_cb(ctx.eloop, 2167 dhcpcd_signals, dhcpcd_signals_len, 2168 dhcpcd_signal_cb, &ctx); 2169 if (eloop_signal_mask(ctx.eloop, &ctx.sigset) == -1) { 2170 logerr("%s: eloop_signal_mask", __func__); 2171 goto exit_failure; 2172 } 2173 2174 if (sig != 0) { 2175 pid = pidfile_read(ctx.pidfile); 2176 if (pid != 0 && pid != -1) 2177 loginfox("sending signal %s to pid %d", siga, pid); 2178 if (pid == 0 || pid == -1 || kill(pid, sig) != 0) { 2179 if (pid != 0 && pid != -1 && errno != ESRCH) { 2180 logerr("kill"); 2181 goto exit_failure; 2182 } 2183 unlink(ctx.pidfile); 2184 /* We can still continue and send the command 2185 * via the control socket. */ 2186 } else { 2187 if (sig == SIGHUP || sig == SIGUSR1) 2188 goto exit_success; 2189 /* Spin until it exits */ 2190 loginfox("waiting for pid %d to exit", pid); 2191 dhcpcd_pidfile_timeout(&ctx); 2192 goto run_loop; 2193 } 2194 } 2195 #endif 2196 2197 #ifdef PRIVSEP 2198 ps_init(&ctx); 2199 #endif 2200 2201 #ifndef SMALL 2202 if (ctx.options & DHCPCD_DUMPLEASE && 2203 ioctl(fileno(stdin), FIONREAD, &i, sizeof(i)) == 0 && 2204 i > 0) 2205 { 2206 ctx.options |= DHCPCD_FORKED; /* pretend child process */ 2207 #ifdef PRIVSEP 2208 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1) 2209 goto exit_failure; 2210 #endif 2211 ifp = calloc(1, sizeof(*ifp)); 2212 if (ifp == NULL) { 2213 logerr(__func__); 2214 goto exit_failure; 2215 } 2216 ifp->ctx = &ctx; 2217 ifp->options = ifo; 2218 switch (family) { 2219 case AF_INET: 2220 #ifdef INET 2221 if (dhcp_dump(ifp) == -1) 2222 goto exit_failure; 2223 break; 2224 #else 2225 logerrx("No DHCP support"); 2226 goto exit_failure; 2227 #endif 2228 case AF_INET6: 2229 #ifdef DHCP6 2230 if (dhcp6_dump(ifp) == -1) 2231 goto exit_failure; 2232 break; 2233 #else 2234 logerrx("No DHCP6 support"); 2235 goto exit_failure; 2236 #endif 2237 default: 2238 logerrx("Family not specified. Please use -4 or -6."); 2239 goto exit_failure; 2240 } 2241 goto exit_success; 2242 } 2243 #endif 2244 2245 /* Try and contact the manager process to send the instruction. */ 2246 if (!(ctx.options & DHCPCD_TEST)) { 2247 ctx.options |= DHCPCD_FORKED; /* avoid socket unlink */ 2248 if (!(ctx.options & DHCPCD_MANAGER)) 2249 ctx.control_fd = control_open(argv[optind], family, 2250 ctx.options & DHCPCD_DUMPLEASE); 2251 if (!(ctx.options & DHCPCD_MANAGER) && ctx.control_fd == -1) 2252 ctx.control_fd = control_open(argv[optind], AF_UNSPEC, 2253 ctx.options & DHCPCD_DUMPLEASE); 2254 if (ctx.control_fd == -1) 2255 ctx.control_fd = control_open(NULL, AF_UNSPEC, 2256 ctx.options & DHCPCD_DUMPLEASE); 2257 if (ctx.control_fd != -1) { 2258 #ifdef PRIVSEP 2259 if (IN_PRIVSEP(&ctx) && 2260 ps_managersandbox(&ctx, NULL) == -1) 2261 goto exit_failure; 2262 #endif 2263 if (!(ctx.options & DHCPCD_DUMPLEASE)) 2264 loginfox("sending commands to dhcpcd process"); 2265 len = control_send(&ctx, argc, argv); 2266 if (len > 0) 2267 logdebugx("send OK"); 2268 else { 2269 logerr("%s: control_send", __func__); 2270 goto exit_failure; 2271 } 2272 if (ctx.options & DHCPCD_DUMPLEASE) { 2273 if (dhcpcd_readdump(&ctx) == -1) { 2274 logerr("%s: dhcpcd_readdump", __func__); 2275 goto exit_failure; 2276 } 2277 goto run_loop; 2278 } 2279 goto exit_success; 2280 } else { 2281 if (errno != ENOENT) 2282 logerr("%s: control_open", __func__); 2283 /* If asking dhcpcd to exit and we failed to 2284 * send a signal or a message then we 2285 * don't proceed past here. */ 2286 if (ctx.options & DHCPCD_DUMPLEASE || 2287 sig == SIGTERM || sig == SIGALRM) 2288 { 2289 if (errno == ENOENT) 2290 logerrx(PACKAGE" is not running"); 2291 goto exit_failure; 2292 } 2293 if (errno == EPERM || errno == EACCES) 2294 goto exit_failure; 2295 } 2296 ctx.options &= ~DHCPCD_FORKED; 2297 } 2298 2299 if (!(ctx.options & DHCPCD_TEST)) { 2300 /* Ensure we have the needed directories */ 2301 if (mkdir(DBDIR, 0750) == -1 && errno != EEXIST) 2302 logerr("%s: mkdir: %s", __func__, DBDIR); 2303 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST) 2304 logerr("%s: mkdir: %s", __func__, RUNDIR); 2305 if ((pid = pidfile_lock(ctx.pidfile)) != 0) { 2306 if (pid == -1) 2307 logerr("%s: pidfile_lock: %s", 2308 __func__, ctx.pidfile); 2309 else 2310 logerrx(PACKAGE 2311 " already running on pid %d (%s)", 2312 pid, ctx.pidfile); 2313 goto exit_failure; 2314 } 2315 } 2316 2317 loginfox(PACKAGE "-" VERSION " starting"); 2318 if (ctx.stdin_valid && freopen(_PATH_DEVNULL, "w", stdin) == NULL) 2319 logwarn("freopen stdin"); 2320 2321 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK) 2322 if (!(ctx.options & DHCPCD_DAEMONISE)) 2323 goto start_manager; 2324 2325 if (xsocketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CXNB, 0, fork_fd) == -1 || 2326 (ctx.stderr_valid && 2327 xsocketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CXNB, 0, stderr_fd) == -1)) 2328 { 2329 logerr("socketpair"); 2330 goto exit_failure; 2331 } 2332 switch (pid = fork()) { 2333 case -1: 2334 logerr("fork"); 2335 goto exit_failure; 2336 case 0: 2337 ctx.fork_fd = fork_fd[1]; 2338 close(fork_fd[0]); 2339 #ifdef PRIVSEP_RIGHTS 2340 if (ps_rights_limit_fd(ctx.fork_fd) == -1) { 2341 logerr("ps_rights_limit_fdpair"); 2342 goto exit_failure; 2343 } 2344 #endif 2345 if (eloop_event_add(ctx.eloop, ctx.fork_fd, ELE_READ, 2346 dhcpcd_fork_cb, &ctx) == -1) 2347 logerr("%s: eloop_event_add", __func__); 2348 2349 /* 2350 * Redirect stderr to the stderr socketpair. 2351 * Redirect stdout as well. 2352 * dhcpcd doesn't output via stdout, but something in 2353 * a called script might. 2354 */ 2355 if (ctx.stderr_valid) { 2356 if (dup2(stderr_fd[1], STDERR_FILENO) == -1 || 2357 (ctx.stdout_valid && 2358 dup2(stderr_fd[1], STDOUT_FILENO) == -1)) 2359 logerr("dup2"); 2360 close(stderr_fd[0]); 2361 close(stderr_fd[1]); 2362 } else if (ctx.stdout_valid) { 2363 if (freopen(_PATH_DEVNULL, "w", stdout) == NULL) 2364 logerr("freopen stdout"); 2365 } 2366 if (setsid() == -1) { 2367 logerr("%s: setsid", __func__); 2368 goto exit_failure; 2369 } 2370 /* Ensure we can never get a controlling terminal */ 2371 switch (pid = fork()) { 2372 case -1: 2373 logerr("fork"); 2374 goto exit_failure; 2375 case 0: 2376 eloop_forked(ctx.eloop); 2377 break; 2378 default: 2379 ctx.options |= DHCPCD_FORKED; /* A lie */ 2380 i = EXIT_SUCCESS; 2381 goto exit1; 2382 } 2383 break; 2384 default: 2385 setproctitle("[launcher]"); 2386 ctx.options |= DHCPCD_FORKED | DHCPCD_LAUNCHER; 2387 ctx.fork_fd = fork_fd[0]; 2388 close(fork_fd[1]); 2389 #ifdef PRIVSEP_RIGHTS 2390 if (ps_rights_limit_fd(ctx.fork_fd) == -1) { 2391 logerr("ps_rights_limit_fd"); 2392 goto exit_failure; 2393 } 2394 #endif 2395 if (eloop_event_add(ctx.eloop, ctx.fork_fd, ELE_READ, 2396 dhcpcd_fork_cb, &ctx) == -1) 2397 logerr("%s: eloop_event_add", __func__); 2398 2399 if (ctx.stderr_valid) { 2400 ctx.stderr_fd = stderr_fd[0]; 2401 close(stderr_fd[1]); 2402 #ifdef PRIVSEP_RIGHTS 2403 if (ps_rights_limit_fd(ctx.stderr_fd) == 1) { 2404 logerr("ps_rights_limit_fd"); 2405 goto exit_failure; 2406 } 2407 #endif 2408 if (eloop_event_add(ctx.eloop, ctx.stderr_fd, ELE_READ, 2409 dhcpcd_stderr_cb, &ctx) == -1) 2410 logerr("%s: eloop_event_add", __func__); 2411 } 2412 #ifdef PRIVSEP 2413 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1) 2414 goto exit_failure; 2415 #endif 2416 goto run_loop; 2417 } 2418 2419 /* We have now forked, setsid, forked once more. 2420 * From this point on, we are the controlling daemon. */ 2421 logdebugx("spawned manager process on PID %d", getpid()); 2422 start_manager: 2423 ctx.options |= DHCPCD_STARTED; 2424 if ((pid = pidfile_lock(ctx.pidfile)) != 0) { 2425 logerr("%s: pidfile_lock %d", __func__, pid); 2426 #ifdef PRIVSEP 2427 /* privsep has not started ... */ 2428 ctx.options &= ~DHCPCD_PRIVSEP; 2429 #endif 2430 goto exit_failure; 2431 } 2432 #endif 2433 2434 os_init(); 2435 2436 #if defined(BSD) && defined(INET6) 2437 /* Disable the kernel RTADV sysctl as early as possible. */ 2438 if (ctx.options & DHCPCD_IPV6 && ctx.options & DHCPCD_IPV6RS) 2439 if_disable_rtadv(); 2440 #endif 2441 2442 #ifdef PRIVSEP 2443 if (IN_PRIVSEP(&ctx) && ps_start(&ctx) == -1) { 2444 logerr("ps_start"); 2445 goto exit_failure; 2446 } 2447 if (ctx.options & DHCPCD_FORKED) 2448 goto run_loop; 2449 #endif 2450 2451 if (!(ctx.options & DHCPCD_TEST)) { 2452 if (control_start(&ctx, 2453 ctx.options & DHCPCD_MANAGER ? 2454 NULL : argv[optind], family) == -1) 2455 { 2456 logerr("%s: control_start", __func__); 2457 goto exit_failure; 2458 } 2459 } 2460 2461 #ifdef PLUGIN_DEV 2462 /* Start any dev listening plugin which may want to 2463 * change the interface name provided by the kernel */ 2464 if (!IN_PRIVSEP(&ctx) && 2465 (ctx.options & (DHCPCD_MANAGER | DHCPCD_DEV)) == 2466 (DHCPCD_MANAGER | DHCPCD_DEV)) 2467 dev_start(&ctx, dhcpcd_handleinterface); 2468 #endif 2469 2470 setproctitle("%s%s%s", 2471 ctx.options & DHCPCD_MANAGER ? "[manager]" : argv[optind], 2472 ctx.options & DHCPCD_IPV4 ? " [ip4]" : "", 2473 ctx.options & DHCPCD_IPV6 ? " [ip6]" : ""); 2474 2475 if (if_opensockets(&ctx) == -1) { 2476 logerr("%s: if_opensockets", __func__); 2477 goto exit_failure; 2478 } 2479 #ifndef SMALL 2480 dhcpcd_setlinkrcvbuf(&ctx); 2481 #endif 2482 2483 /* Try and create DUID from the machine UUID. */ 2484 dhcpcd_initduid(&ctx, NULL); 2485 2486 /* Cache the default vendor option. */ 2487 if (dhcp_vendor(ctx.vendor, sizeof(ctx.vendor)) == -1) 2488 logerr("dhcp_vendor"); 2489 2490 /* Start handling kernel messages for interfaces, addresses and 2491 * routes. */ 2492 if (eloop_event_add(ctx.eloop, ctx.link_fd, ELE_READ, 2493 dhcpcd_handlelink, &ctx) == -1) 2494 logerr("%s: eloop_event_add", __func__); 2495 2496 #ifdef PRIVSEP 2497 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, "stdio route") == -1) 2498 goto exit_failure; 2499 #endif 2500 2501 /* When running dhcpcd against a single interface, we need to retain 2502 * the old behaviour of waiting for an IP address */ 2503 if (ctx.ifc == 1 && !(ctx.options & DHCPCD_BACKGROUND)) 2504 ctx.options |= DHCPCD_WAITIP; 2505 2506 ctx.ifaces = if_discover(&ctx, &ifaddrs, ctx.ifc, ctx.ifv); 2507 if (ctx.ifaces == NULL) { 2508 logerr("%s: if_discover", __func__); 2509 goto exit_failure; 2510 } 2511 for (i = 0; i < ctx.ifc; i++) { 2512 if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL) 2513 logerrx("%s: interface not found", 2514 ctx.ifv[i]); 2515 else if (!ifp->active) 2516 logerrx("%s: interface has an invalid configuration", 2517 ctx.ifv[i]); 2518 } 2519 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 2520 if (ifp->active == IF_ACTIVE_USER) 2521 break; 2522 } 2523 if (ifp == NULL) { 2524 if (ctx.ifc == 0) { 2525 int loglevel; 2526 2527 loglevel = ctx.options & DHCPCD_INACTIVE ? 2528 LOG_DEBUG : LOG_ERR; 2529 logmessage(loglevel, "no valid interfaces found"); 2530 dhcpcd_daemonise(&ctx); 2531 } else 2532 goto exit_failure; 2533 if (!(ctx.options & DHCPCD_LINK)) { 2534 logerrx("aborting as link detection is disabled"); 2535 goto exit_failure; 2536 } 2537 } 2538 2539 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 2540 if (ifp->active) 2541 dhcpcd_initstate1(ifp, argc, argv, 0); 2542 } 2543 if_learnaddrs(&ctx, ctx.ifaces, &ifaddrs); 2544 if_freeifaddrs(&ctx, &ifaddrs); 2545 ifaddrs = NULL; 2546 2547 if (ctx.options & DHCPCD_BACKGROUND) 2548 dhcpcd_daemonise(&ctx); 2549 2550 opt = 0; 2551 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 2552 if (ifp->active) { 2553 run_preinit(ifp); 2554 if (if_is_link_up(ifp)) 2555 opt = 1; 2556 } 2557 } 2558 2559 if (!(ctx.options & DHCPCD_BACKGROUND)) { 2560 if (ctx.options & DHCPCD_MANAGER) 2561 t = ifo->timeout; 2562 else { 2563 t = 0; 2564 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 2565 if (ifp->active) { 2566 t = ifp->options->timeout; 2567 break; 2568 } 2569 } 2570 } 2571 if (opt == 0 && 2572 ctx.options & DHCPCD_LINK && 2573 !(ctx.options & DHCPCD_WAITIP)) 2574 { 2575 int loglevel; 2576 2577 loglevel = ctx.options & DHCPCD_INACTIVE ? 2578 LOG_DEBUG : LOG_WARNING; 2579 logmessage(loglevel, "no interfaces have a carrier"); 2580 dhcpcd_daemonise(&ctx); 2581 } else if (t > 0 && 2582 /* Test mode removes the daemonise bit, so check for both */ 2583 ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST)) 2584 { 2585 eloop_timeout_add_sec(ctx.eloop, t, 2586 handle_exit_timeout, &ctx); 2587 } 2588 } 2589 free_options(&ctx, ifo); 2590 ifo = NULL; 2591 2592 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 2593 if (ifp->active) 2594 eloop_timeout_add_sec(ctx.eloop, 0, 2595 dhcpcd_prestartinterface, ifp); 2596 } 2597 2598 run_loop: 2599 i = eloop_start(ctx.eloop, &ctx.sigset); 2600 if (i < 0) { 2601 logerr("%s: eloop_start", __func__); 2602 goto exit_failure; 2603 } 2604 goto exit1; 2605 2606 exit_success: 2607 i = EXIT_SUCCESS; 2608 goto exit1; 2609 2610 exit_failure: 2611 i = EXIT_FAILURE; 2612 2613 exit1: 2614 if (!(ctx.options & DHCPCD_TEST) && control_stop(&ctx) == -1) 2615 logerr("%s: control_stop", __func__); 2616 if_freeifaddrs(&ctx, &ifaddrs); 2617 #ifdef PRIVSEP 2618 ps_stop(&ctx); 2619 #endif 2620 /* Free memory and close fd's */ 2621 if (ctx.ifaces) { 2622 while ((ifp = TAILQ_FIRST(ctx.ifaces))) { 2623 TAILQ_REMOVE(ctx.ifaces, ifp, next); 2624 if_free(ifp); 2625 } 2626 free(ctx.ifaces); 2627 ctx.ifaces = NULL; 2628 } 2629 free_options(&ctx, ifo); 2630 #ifdef HAVE_OPEN_MEMSTREAM 2631 if (ctx.script_fp) 2632 fclose(ctx.script_fp); 2633 #endif 2634 free(ctx.script_buf); 2635 free(ctx.script_env); 2636 rt_dispose(&ctx); 2637 free(ctx.duid); 2638 if (ctx.link_fd != -1) { 2639 eloop_event_delete(ctx.eloop, ctx.link_fd); 2640 close(ctx.link_fd); 2641 } 2642 if_closesockets(&ctx); 2643 free_globals(&ctx); 2644 #ifdef INET6 2645 ipv6_ctxfree(&ctx); 2646 #endif 2647 #ifdef PLUGIN_DEV 2648 dev_stop(&ctx); 2649 #endif 2650 if (ctx.script != dhcpcd_default_script) 2651 free(ctx.script); 2652 #ifdef PRIVSEP 2653 if (ps_stopwait(&ctx) != EXIT_SUCCESS) 2654 i = EXIT_FAILURE; 2655 #endif 2656 if (ctx.options & DHCPCD_STARTED && !(ctx.options & DHCPCD_FORKED)) 2657 loginfox(PACKAGE " exited"); 2658 #ifdef PRIVSEP 2659 if (ps_root_stop(&ctx) == -1) 2660 i = EXIT_FAILURE; 2661 eloop_free(ctx.ps_eloop); 2662 #endif 2663 eloop_free(ctx.eloop); 2664 logclose(); 2665 free(ctx.logfile); 2666 free(ctx.ctl_buf); 2667 #ifdef SETPROCTITLE_H 2668 setproctitle_fini(); 2669 #endif 2670 #ifdef USE_SIGNALS 2671 if (ctx.options & DHCPCD_STARTED) { 2672 /* Try to detach from the launch process. */ 2673 if (ctx.fork_fd != -1 && 2674 write(ctx.fork_fd, &i, sizeof(i)) == -1) 2675 logerr("%s: write", __func__); 2676 } 2677 if (ctx.options & (DHCPCD_FORKED | DHCPCD_PRIVSEP)) 2678 _exit(i); /* so atexit won't remove our pidfile */ 2679 #endif 2680 return i; 2681 } 2682