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