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