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