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