1 /* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2006-2018 Roy Marples <roy@marples.name> 4 * All rights reserved 5 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 const char dhcpcd_copyright[] = "Copyright (c) 2006-2018 Roy Marples"; 29 30 #include <sys/file.h> 31 #include <sys/socket.h> 32 #include <sys/stat.h> 33 #include <sys/time.h> 34 #include <sys/types.h> 35 #include <sys/uio.h> 36 37 #include <ctype.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <getopt.h> 41 #include <limits.h> 42 #include <paths.h> 43 #include <signal.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <time.h> 49 50 #include "config.h" 51 #include "arp.h" 52 #include "common.h" 53 #include "control.h" 54 #include "dev.h" 55 #include "dhcpcd.h" 56 #include "dhcp6.h" 57 #include "duid.h" 58 #include "eloop.h" 59 #include "if.h" 60 #include "if-options.h" 61 #include "ipv4.h" 62 #include "ipv4ll.h" 63 #include "ipv6.h" 64 #include "ipv6nd.h" 65 #include "logerr.h" 66 #include "script.h" 67 68 #ifdef HAVE_UTIL_H 69 #include <util.h> 70 #endif 71 72 #ifdef USE_SIGNALS 73 const int dhcpcd_signals[] = { 74 SIGTERM, 75 SIGINT, 76 SIGALRM, 77 SIGHUP, 78 SIGUSR1, 79 SIGUSR2, 80 SIGPIPE 81 }; 82 const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals); 83 #endif 84 85 static void 86 usage(void) 87 { 88 89 printf("usage: "PACKAGE"\t[-46ABbDdEGgHJKkLnPpqTVw]\n" 90 "\t\t[-C, --nohook hook] [-c, --script script]\n" 91 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n" 92 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n" 93 "\t\t[-i, --vendorclassid vendorclassid] [-l, --leasetime seconds]\n" 94 "\t\t[-m, --metric metric] [-O, --nooption option]\n" 95 "\t\t[-o, --option option] [-Q, --require option]\n" 96 "\t\t[-r, --request address] [-S, --static value]\n" 97 "\t\t[-s, --inform address[/cidr]] [-t, --timeout seconds]\n" 98 "\t\t[-u, --userclass class] [-v, --vendor code, value]\n" 99 "\t\t[-W, --whitelist address[/cidr]] [-y, --reboot seconds]\n" 100 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n" 101 "\t\t[-z, --allowinterfaces pattern] [interface] [...]\n" 102 " "PACKAGE"\t-k, --release [interface]\n" 103 " "PACKAGE"\t-U, --dumplease interface\n" 104 " "PACKAGE"\t--version\n" 105 " "PACKAGE"\t-x, --exit [interface]\n"); 106 } 107 108 static void 109 free_globals(struct dhcpcd_ctx *ctx) 110 { 111 struct dhcp_opt *opt; 112 113 if (ctx->ifac) { 114 for (; ctx->ifac > 0; ctx->ifac--) 115 free(ctx->ifav[ctx->ifac - 1]); 116 free(ctx->ifav); 117 ctx->ifav = NULL; 118 } 119 if (ctx->ifdc) { 120 for (; ctx->ifdc > 0; ctx->ifdc--) 121 free(ctx->ifdv[ctx->ifdc - 1]); 122 free(ctx->ifdv); 123 ctx->ifdv = NULL; 124 } 125 if (ctx->ifcc) { 126 for (; ctx->ifcc > 0; ctx->ifcc--) 127 free(ctx->ifcv[ctx->ifcc - 1]); 128 free(ctx->ifcv); 129 ctx->ifcv = NULL; 130 } 131 132 #ifdef INET 133 if (ctx->dhcp_opts) { 134 for (opt = ctx->dhcp_opts; 135 ctx->dhcp_opts_len > 0; 136 opt++, ctx->dhcp_opts_len--) 137 free_dhcp_opt_embenc(opt); 138 free(ctx->dhcp_opts); 139 ctx->dhcp_opts = NULL; 140 } 141 #endif 142 #ifdef INET6 143 if (ctx->nd_opts) { 144 for (opt = ctx->nd_opts; 145 ctx->nd_opts_len > 0; 146 opt++, ctx->nd_opts_len--) 147 free_dhcp_opt_embenc(opt); 148 free(ctx->nd_opts); 149 ctx->nd_opts = NULL; 150 } 151 if (ctx->dhcp6_opts) { 152 for (opt = ctx->dhcp6_opts; 153 ctx->dhcp6_opts_len > 0; 154 opt++, ctx->dhcp6_opts_len--) 155 free_dhcp_opt_embenc(opt); 156 free(ctx->dhcp6_opts); 157 ctx->dhcp6_opts = NULL; 158 } 159 #endif 160 if (ctx->vivso) { 161 for (opt = ctx->vivso; 162 ctx->vivso_len > 0; 163 opt++, ctx->vivso_len--) 164 free_dhcp_opt_embenc(opt); 165 free(ctx->vivso); 166 ctx->vivso = NULL; 167 } 168 } 169 170 static void 171 handle_exit_timeout(void *arg) 172 { 173 struct dhcpcd_ctx *ctx; 174 175 ctx = arg; 176 logerrx("timed out"); 177 if (!(ctx->options & DHCPCD_MASTER)) { 178 eloop_exit(ctx->eloop, EXIT_FAILURE); 179 return; 180 } 181 ctx->options |= DHCPCD_NOWAITIP; 182 dhcpcd_daemonise(ctx); 183 } 184 185 static const char * 186 dhcpcd_af(int af) 187 { 188 189 switch (af) { 190 case AF_UNSPEC: 191 return "IP"; 192 case AF_INET: 193 return "IPv4"; 194 case AF_INET6: 195 return "IPv6"; 196 default: 197 return NULL; 198 } 199 } 200 201 int 202 dhcpcd_ifafwaiting(const struct interface *ifp) 203 { 204 unsigned long long opts; 205 206 if (ifp->active != IF_ACTIVE_USER) 207 return AF_MAX; 208 209 opts = ifp->options->options; 210 if (opts & DHCPCD_WAITIP4 && !ipv4_hasaddr(ifp)) 211 return AF_INET; 212 if (opts & DHCPCD_WAITIP6 && !ipv6_hasaddr(ifp)) 213 return AF_INET6; 214 if (opts & DHCPCD_WAITIP && 215 !(opts & (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)) && 216 !ipv4_hasaddr(ifp) && !ipv6_hasaddr(ifp)) 217 return AF_UNSPEC; 218 return AF_MAX; 219 } 220 221 int 222 dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx) 223 { 224 unsigned long long opts; 225 const struct interface *ifp; 226 int af; 227 228 if (!(ctx->options & DHCPCD_WAITOPTS)) 229 return AF_MAX; 230 231 opts = ctx->options; 232 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 233 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) && 234 ipv4_hasaddr(ifp)) 235 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4); 236 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) && 237 ipv6_hasaddr(ifp)) 238 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6); 239 if (!(opts & DHCPCD_WAITOPTS)) 240 break; 241 } 242 if (opts & DHCPCD_WAITIP) 243 af = AF_UNSPEC; 244 else if (opts & DHCPCD_WAITIP4) 245 af = AF_INET; 246 else if (opts & DHCPCD_WAITIP6) 247 af = AF_INET6; 248 else 249 return AF_MAX; 250 return af; 251 } 252 253 static int 254 dhcpcd_ipwaited(struct dhcpcd_ctx *ctx) 255 { 256 struct interface *ifp; 257 int af; 258 259 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 260 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) { 261 logdebugx("%s: waiting for an %s address", 262 ifp->name, dhcpcd_af(af)); 263 return 0; 264 } 265 } 266 267 if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) { 268 logdebugx("waiting for an %s address", 269 dhcpcd_af(af)); 270 return 0; 271 } 272 273 return 1; 274 } 275 276 /* Returns the pid of the child, otherwise 0. */ 277 pid_t 278 dhcpcd_daemonise(struct dhcpcd_ctx *ctx) 279 { 280 #ifdef THERE_IS_NO_FORK 281 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx); 282 errno = ENOSYS; 283 return 0; 284 #else 285 pid_t pid, lpid; 286 char buf = '\0'; 287 int sidpipe[2], fd; 288 289 if (ctx->options & DHCPCD_DAEMONISE && 290 !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP))) 291 { 292 if (!dhcpcd_ipwaited(ctx)) 293 return 0; 294 } 295 296 if (ctx->options & DHCPCD_ONESHOT) { 297 loginfox("exiting due to oneshot"); 298 eloop_exit(ctx->eloop, EXIT_SUCCESS); 299 return 0; 300 } 301 302 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx); 303 if (ctx->options & DHCPCD_DAEMONISED || 304 !(ctx->options & DHCPCD_DAEMONISE)) 305 return 0; 306 logdebugx("forking to background"); 307 308 /* Setup a signal pipe so parent knows when to exit. */ 309 if (pipe(sidpipe) == -1) { 310 logerr("%s: pipe", __func__); 311 return 0; 312 } 313 314 /* Store the pid and routing message seq number so we can identify 315 * the last message successfully sent to the kernel. 316 * This allows us to ignore all messages we sent after forking 317 * and detaching. */ 318 ctx->ppid = getpid(); 319 ctx->pseq = ctx->sseq; 320 321 switch (pid = fork()) { 322 case -1: 323 logerr("%s: fork", __func__); 324 return 0; 325 case 0: 326 if ((lpid = pidfile_lock(ctx->pidfile)) != 0) 327 logerr("%s: pidfile_lock %d", __func__, lpid); 328 setsid(); 329 /* Notify parent it's safe to exit as we've detached. */ 330 close(sidpipe[0]); 331 if (write(sidpipe[1], &buf, 1) == -1) 332 logerr("%s: write", __func__); 333 close(sidpipe[1]); 334 /* Some polling methods don't survive after forking, 335 * so ensure we can requeue all our events. */ 336 if (eloop_requeue(ctx->eloop) == -1) { 337 logerr("%s: eloop_requeue", __func__); 338 eloop_exit(ctx->eloop, EXIT_FAILURE); 339 } 340 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 341 dup2(fd, STDIN_FILENO); 342 dup2(fd, STDOUT_FILENO); 343 dup2(fd, STDERR_FILENO); 344 close(fd); 345 } 346 ctx->options |= DHCPCD_DAEMONISED; 347 return 0; 348 default: 349 /* Wait for child to detach */ 350 close(sidpipe[1]); 351 if (read(sidpipe[0], &buf, 1) == -1) 352 logerr("%s: read", __func__); 353 close(sidpipe[0]); 354 loginfox("forked to background, child pid %d", pid); 355 ctx->options |= DHCPCD_FORKED; 356 eloop_exit(ctx->eloop, EXIT_SUCCESS); 357 return pid; 358 } 359 #endif 360 } 361 362 static void 363 dhcpcd_drop(struct interface *ifp, int stop) 364 { 365 366 #ifdef DHCP6 367 dhcp6_drop(ifp, stop ? NULL : "EXPIRE6"); 368 #endif 369 #ifdef INET6 370 ipv6nd_drop(ifp); 371 ipv6_drop(ifp); 372 #endif 373 #ifdef IPV4LL 374 ipv4ll_drop(ifp); 375 #endif 376 #ifdef INET 377 dhcp_drop(ifp, stop ? "STOP" : "EXPIRE"); 378 #endif 379 #ifdef ARP 380 arp_drop(ifp); 381 #endif 382 #if !defined(DHCP6) && !defined(DHCP) 383 UNUSED(stop); 384 #endif 385 } 386 387 static void 388 stop_interface(struct interface *ifp) 389 { 390 struct dhcpcd_ctx *ctx; 391 392 ctx = ifp->ctx; 393 loginfox("%s: removing interface", ifp->name); 394 ifp->options->options |= DHCPCD_STOPPING; 395 396 dhcpcd_drop(ifp, 1); 397 if (ifp->options->options & DHCPCD_DEPARTED) 398 script_runreason(ifp, "DEPARTED"); 399 else 400 script_runreason(ifp, "STOPPED"); 401 402 /* Delete all timeouts for the interfaces */ 403 eloop_q_timeout_delete(ctx->eloop, 0, NULL, ifp); 404 405 /* De-activate the interface */ 406 ifp->active = IF_INACTIVE; 407 ifp->options->options &= ~DHCPCD_STOPPING; 408 /* Set the link state to unknown as we're no longer tracking it. */ 409 ifp->carrier = LINK_UNKNOWN; 410 411 if (!(ctx->options & (DHCPCD_MASTER | DHCPCD_TEST))) 412 eloop_exit(ctx->eloop, EXIT_FAILURE); 413 } 414 415 static void 416 configure_interface1(struct interface *ifp) 417 { 418 struct if_options *ifo = ifp->options; 419 420 /* Do any platform specific configuration */ 421 if_conf(ifp); 422 423 /* If we want to release a lease, we can't really persist the 424 * address either. */ 425 if (ifo->options & DHCPCD_RELEASE) 426 ifo->options &= ~DHCPCD_PERSISTENT; 427 428 if (ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) { 429 ifo->options &= ~DHCPCD_ARP; 430 if (!(ifp->flags & IFF_MULTICAST)) 431 ifo->options &= ~DHCPCD_IPV6RS; 432 if (!(ifo->options & DHCPCD_INFORM)) 433 ifo->options |= DHCPCD_STATIC; 434 } 435 if (ifp->flags & IFF_NOARP || 436 !(ifo->options & DHCPCD_ARP) || 437 ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) 438 ifo->options &= ~DHCPCD_IPV4LL; 439 440 if (ifo->metric != -1) 441 ifp->metric = (unsigned int)ifo->metric; 442 443 if (!(ifo->options & DHCPCD_IPV4)) 444 ifo->options &= ~(DHCPCD_DHCP | DHCPCD_IPV4LL | DHCPCD_WAITIP4); 445 446 #ifdef INET6 447 if (!(ifo->options & DHCPCD_IPV6)) 448 ifo->options &= 449 ~(DHCPCD_IPV6RS | DHCPCD_DHCP6 | DHCPCD_WAITIP6); 450 451 /* We want to setup INET6 on the interface as soon as possible. */ 452 if (ifp->active == IF_ACTIVE_USER && 453 ifo->options & DHCPCD_IPV6 && 454 !(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) 455 { 456 /* If not doing any DHCP, disable the RDNSS requirement. */ 457 if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6))) 458 ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS; 459 if_setup_inet6(ifp); 460 } 461 #endif 462 463 if (!(ifo->options & DHCPCD_IAID)) { 464 /* 465 * An IAID is for identifying a unqiue interface within 466 * the client. It is 4 bytes long. Working out a default 467 * value is problematic. 468 * 469 * Interface name and number are not stable 470 * between different OS's. Some OS's also cannot make 471 * up their mind what the interface should be called 472 * (yes, udev, I'm looking at you). 473 * Also, the name could be longer than 4 bytes. 474 * Also, with pluggable interfaces the name and index 475 * could easily get swapped per actual interface. 476 * 477 * The MAC address is 6 bytes long, the final 3 478 * being unique to the manufacturer and the initial 3 479 * being unique to the organisation which makes it. 480 * We could use the last 4 bytes of the MAC address 481 * as the IAID as it's the most stable part given the 482 * above, but equally it's not guaranteed to be 483 * unique. 484 * 485 * Given the above, and our need to reliably work 486 * between reboots without persitent storage, 487 * generating the IAID from the MAC address is the only 488 * logical default. 489 * Saying that, if a VLANID has been specified then we 490 * can use that. It's possible that different interfaces 491 * can have the same VLANID, but this is no worse than 492 * generating the IAID from the duplicate MAC address. 493 * 494 * dhclient uses the last 4 bytes of the MAC address. 495 * dibbler uses an increamenting counter. 496 * wide-dhcpv6 uses 0 or a configured value. 497 * odhcp6c uses 1. 498 * Windows 7 uses the first 3 bytes of the MAC address 499 * and an unknown byte. 500 * dhcpcd-6.1.0 and earlier used the interface name, 501 * falling back to interface index if name > 4. 502 */ 503 if (ifp->vlanid != 0) { 504 uint32_t vlanid; 505 506 /* Maximal VLANID is 4095, so prefix with 0xff 507 * so we don't conflict with an interface index. */ 508 vlanid = htonl(ifp->vlanid | 0xff000000); 509 memcpy(ifo->iaid, &vlanid, sizeof(vlanid)); 510 } else if (ifp->hwlen >= sizeof(ifo->iaid)) { 511 memcpy(ifo->iaid, 512 ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid), 513 sizeof(ifo->iaid)); 514 } else { 515 uint32_t len; 516 517 len = (uint32_t)strlen(ifp->name); 518 if (len <= sizeof(ifo->iaid)) { 519 memcpy(ifo->iaid, ifp->name, len); 520 if (len < sizeof(ifo->iaid)) 521 memset(ifo->iaid + len, 0, 522 sizeof(ifo->iaid) - len); 523 } else { 524 /* IAID is the same size as a uint32_t */ 525 len = htonl(ifp->index); 526 memcpy(ifo->iaid, &len, sizeof(ifo->iaid)); 527 } 528 } 529 ifo->options |= DHCPCD_IAID; 530 } 531 532 #ifdef INET6 533 if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 && 534 ifp->name[0] != '\0') 535 { 536 ifo->ia = malloc(sizeof(*ifo->ia)); 537 if (ifo->ia == NULL) 538 logerr(__func__); 539 else { 540 ifo->ia_len = 1; 541 ifo->ia->ia_type = D6_OPTION_IA_NA; 542 memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid)); 543 memset(&ifo->ia->addr, 0, sizeof(ifo->ia->addr)); 544 #ifndef SMALL 545 ifo->ia->sla = NULL; 546 ifo->ia->sla_len = 0; 547 #endif 548 } 549 } else { 550 size_t i; 551 552 for (i = 0; i < ifo->ia_len; i++) { 553 if (!ifo->ia[i].iaid_set) { 554 memcpy(&ifo->ia[i].iaid, ifo->iaid, 555 sizeof(ifo->ia[i].iaid)); 556 ifo->ia[i].iaid_set = 1; 557 } 558 } 559 } 560 #endif 561 } 562 563 int 564 dhcpcd_selectprofile(struct interface *ifp, const char *profile) 565 { 566 struct if_options *ifo; 567 char pssid[PROFILE_LEN]; 568 569 if (ifp->ssid_len) { 570 ssize_t r; 571 572 r = print_string(pssid, sizeof(pssid), OT_ESCSTRING, 573 ifp->ssid, ifp->ssid_len); 574 if (r == -1) { 575 logerr(__func__); 576 pssid[0] = '\0'; 577 } 578 } else 579 pssid[0] = '\0'; 580 ifo = read_config(ifp->ctx, ifp->name, pssid, profile); 581 if (ifo == NULL) { 582 logdebugx("%s: no profile %s", ifp->name, profile); 583 return -1; 584 } 585 if (profile != NULL) { 586 strlcpy(ifp->profile, profile, sizeof(ifp->profile)); 587 loginfox("%s: selected profile %s", ifp->name, profile); 588 } else 589 *ifp->profile = '\0'; 590 591 free_options(ifp->ctx, ifp->options); 592 ifp->options = ifo; 593 if (profile) { 594 add_options(ifp->ctx, ifp->name, ifp->options, 595 ifp->ctx->argc, ifp->ctx->argv); 596 configure_interface1(ifp); 597 } 598 return 1; 599 } 600 601 static void 602 configure_interface(struct interface *ifp, int argc, char **argv, 603 unsigned long long options) 604 { 605 time_t old; 606 607 old = ifp->options ? ifp->options->mtime : 0; 608 dhcpcd_selectprofile(ifp, NULL); 609 if (ifp->options == NULL) { 610 /* dhcpcd cannot continue with this interface. */ 611 ifp->active = IF_INACTIVE; 612 return; 613 } 614 add_options(ifp->ctx, ifp->name, ifp->options, argc, argv); 615 ifp->options->options |= options; 616 configure_interface1(ifp); 617 618 /* If the mtime has changed drop any old lease */ 619 if (old != 0 && ifp->options->mtime != old) { 620 logwarnx("%s: confile file changed, expiring leases", 621 ifp->name); 622 dhcpcd_drop(ifp, 0); 623 } 624 } 625 626 static void 627 dhcpcd_pollup(void *arg) 628 { 629 struct interface *ifp = arg; 630 int carrier; 631 632 carrier = if_carrier(ifp); /* will set ifp->flags */ 633 if (carrier == LINK_UP && !(ifp->flags & IFF_UP)) { 634 struct timespec tv; 635 636 tv.tv_sec = 0; 637 tv.tv_nsec = IF_POLL_UP * NSEC_PER_MSEC; 638 eloop_timeout_add_tv(ifp->ctx->eloop, &tv, dhcpcd_pollup, ifp); 639 return; 640 } 641 642 dhcpcd_handlecarrier(ifp->ctx, carrier, ifp->flags, ifp->name); 643 } 644 645 static void 646 dhcpcd_initstate2(struct interface *ifp, unsigned long long options) 647 { 648 struct if_options *ifo; 649 650 if (options) { 651 if ((ifo = default_config(ifp->ctx)) == NULL) { 652 logerr(__func__); 653 return; 654 } 655 ifo->options |= options; 656 free(ifp->options); 657 ifp->options = ifo; 658 } else 659 ifo = ifp->options; 660 661 #ifdef INET6 662 if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == -1) { 663 logerr(__func__); 664 ifo->options &= ~DHCPCD_IPV6; 665 } 666 #endif 667 } 668 669 static void 670 dhcpcd_initstate1(struct interface *ifp, int argc, char **argv, 671 unsigned long long options) 672 { 673 674 configure_interface(ifp, argc, argv, options); 675 if (ifp->active) 676 dhcpcd_initstate2(ifp, 0); 677 } 678 679 static void 680 dhcpcd_initstate(struct interface *ifp, unsigned long long options) 681 { 682 683 dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options); 684 } 685 686 void 687 dhcpcd_handlecarrier(struct dhcpcd_ctx *ctx, int carrier, unsigned int flags, 688 const char *ifname) 689 { 690 struct interface *ifp; 691 692 ifp = if_find(ctx->ifaces, ifname); 693 if (ifp == NULL || 694 ifp->options == NULL || !(ifp->options->options & DHCPCD_LINK) || 695 !ifp->active) 696 return; 697 698 switch(carrier) { 699 case LINK_UNKNOWN: 700 carrier = if_carrier(ifp); /* will set ifp->flags */ 701 break; 702 case LINK_UP: 703 /* we have a carrier! Still need to check for IFF_UP */ 704 if (flags & IFF_UP) 705 ifp->flags = flags; 706 else { 707 /* So we need to poll for IFF_UP as there is no 708 * kernel notification when it's set. */ 709 dhcpcd_pollup(ifp); 710 return; 711 } 712 break; 713 default: 714 ifp->flags = flags; 715 } 716 717 /* If we here, we don't need to poll for IFF_UP any longer 718 * if generated by a kernel event. */ 719 eloop_timeout_delete(ifp->ctx->eloop, dhcpcd_pollup, ifp); 720 721 if (carrier == LINK_UNKNOWN) { 722 if (errno != ENOTTY && errno != ENXIO) { 723 /* Don't log an error if interface departed */ 724 logerr("%s: %s", ifp->name, __func__); 725 } 726 } else if (carrier == LINK_DOWN || (ifp->flags & IFF_UP) == 0) { 727 if (ifp->carrier != LINK_DOWN) { 728 if (ifp->carrier == LINK_UP) 729 loginfox("%s: carrier lost", ifp->name); 730 ifp->carrier = LINK_DOWN; 731 script_runreason(ifp, "NOCARRIER"); 732 #ifdef NOCARRIER_PRESERVE_IP 733 #ifdef ARP 734 arp_drop(ifp); 735 #endif 736 dhcp_abort(ifp); 737 ipv6nd_expire(ifp, 0); 738 #else 739 dhcpcd_drop(ifp, 0); 740 #endif 741 } 742 } else if (carrier == LINK_UP && ifp->flags & IFF_UP) { 743 if (ifp->carrier != LINK_UP) { 744 loginfox("%s: carrier acquired", ifp->name); 745 ifp->carrier = LINK_UP; 746 #if !defined(__linux__) && !defined(__NetBSD__) 747 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the 748 * hardware address changes so we have to go 749 * through the disovery process to work it out. */ 750 dhcpcd_handleinterface(ctx, 0, ifp->name); 751 #endif 752 if (ifp->wireless) { 753 uint8_t ossid[IF_SSIDLEN]; 754 #ifdef NOCARRIER_PRESERVE_IP 755 size_t olen; 756 757 olen = ifp->ssid_len; 758 #endif 759 memcpy(ossid, ifp->ssid, ifp->ssid_len); 760 if_getssid(ifp); 761 #ifdef NOCARRIER_PRESERVE_IP 762 /* If we changed SSID network, drop leases */ 763 if (ifp->ssid_len != olen || 764 memcmp(ifp->ssid, ossid, ifp->ssid_len)) 765 dhcpcd_drop(ifp, 0); 766 #endif 767 } 768 dhcpcd_initstate(ifp, 0); 769 script_runreason(ifp, "CARRIER"); 770 #ifdef NOCARRIER_PRESERVE_IP 771 /* Set any IPv6 Routers we remembered to expire 772 * faster than they would normally as we 773 * maybe on a new network. */ 774 ipv6nd_expire(ifp, RTR_CARRIER_EXPIRE); 775 #endif 776 /* RFC4941 Section 3.5 */ 777 ipv6_gentempifid(ifp); 778 dhcpcd_startinterface(ifp); 779 } 780 } 781 } 782 783 static void 784 warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid) 785 { 786 struct interface *ifn; 787 #ifdef INET6 788 size_t i; 789 struct if_ia *ia; 790 #endif 791 792 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) { 793 if (ifn == ifp || !ifn->active) 794 continue; 795 if (ia_type == 0 && 796 memcmp(ifn->options->iaid, iaid, 797 sizeof(ifn->options->iaid)) == 0) 798 break; 799 #ifdef INET6 800 for (i = 0; i < ifn->options->ia_len; i++) { 801 ia = &ifn->options->ia[i]; 802 if (ia->ia_type == ia_type && 803 memcmp(ia->iaid, iaid, sizeof(ia->iaid)) == 0) 804 break; 805 } 806 #endif 807 } 808 809 /* This is only a problem if the interfaces are on the same network. */ 810 if (ifn) 811 logerrx("%s: IAID conflicts with one assigned to %s", 812 ifp->name, ifn->name); 813 } 814 815 void 816 dhcpcd_startinterface(void *arg) 817 { 818 struct interface *ifp = arg; 819 struct if_options *ifo = ifp->options; 820 char buf[DUID_LEN * 3]; 821 int carrier; 822 struct timespec tv; 823 824 if (ifo->options & DHCPCD_LINK) { 825 switch (ifp->carrier) { 826 case LINK_UP: 827 break; 828 case LINK_DOWN: 829 loginfox("%s: waiting for carrier", ifp->name); 830 return; 831 case LINK_UNKNOWN: 832 /* No media state available. 833 * Loop until both IFF_UP and IFF_RUNNING are set */ 834 if ((carrier = if_carrier(ifp)) == LINK_UNKNOWN) { 835 tv.tv_sec = 0; 836 tv.tv_nsec = IF_POLL_UP * NSEC_PER_MSEC; 837 eloop_timeout_add_tv(ifp->ctx->eloop, 838 &tv, dhcpcd_startinterface, ifp); 839 } else 840 dhcpcd_handlecarrier(ifp->ctx, carrier, 841 ifp->flags, ifp->name); 842 return; 843 } 844 } 845 846 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) { 847 /* Report client DUID */ 848 if (ifp->ctx->duid == NULL) { 849 if (duid_init(ifp) == 0) 850 return; 851 loginfox("DUID %s", 852 hwaddr_ntoa(ifp->ctx->duid, 853 ifp->ctx->duid_len, 854 buf, sizeof(buf))); 855 } 856 } 857 858 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) { 859 #ifdef INET6 860 size_t i; 861 struct if_ia *ia; 862 #endif 863 864 /* Report IAIDs */ 865 loginfox("%s: IAID %s", ifp->name, 866 hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid), 867 buf, sizeof(buf))); 868 warn_iaid_conflict(ifp, 0, ifo->iaid); 869 #ifdef INET6 870 for (i = 0; i < ifo->ia_len; i++) { 871 ia = &ifo->ia[i]; 872 if (memcmp(ifo->iaid, ia->iaid, sizeof(ifo->iaid))) { 873 loginfox("%s: IA type %u IAID %s", 874 ifp->name, ia->ia_type, 875 hwaddr_ntoa(ia->iaid, sizeof(ia->iaid), 876 buf, sizeof(buf))); 877 warn_iaid_conflict(ifp, ia->ia_type, ia->iaid); 878 } 879 } 880 #endif 881 } 882 883 if (ifo->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) { 884 logerr("%s: ipv6_start", ifp->name); 885 ifo->options &= ~DHCPCD_IPV6; 886 } 887 if (ifo->options & DHCPCD_IPV6) { 888 if (ifp->active == IF_ACTIVE_USER) { 889 ipv6_startstatic(ifp); 890 891 if (ifo->options & DHCPCD_IPV6RS) 892 ipv6nd_startrs(ifp); 893 } 894 895 896 if (ifo->options & DHCPCD_DHCP6) { 897 dhcp6_find_delegates(ifp); 898 899 if (ifp->active == IF_ACTIVE_USER) { 900 enum DH6S d6_state; 901 902 if (ifo->options & DHCPCD_IA_FORCED) 903 d6_state = DH6S_INIT; 904 else if (ifo->options & DHCPCD_INFORM6) 905 d6_state = DH6S_INFORM; 906 else 907 d6_state = DH6S_CONFIRM; 908 if (dhcp6_start(ifp, d6_state) == -1) 909 logerr("%s: dhcp6_start", ifp->name); 910 } 911 } 912 } 913 914 #ifdef INET 915 if (ifo->options & DHCPCD_IPV4 && ifp->active == IF_ACTIVE_USER) { 916 /* Ensure we have an IPv4 state before starting DHCP */ 917 if (ipv4_getstate(ifp) != NULL) 918 dhcp_start(ifp); 919 } 920 #endif 921 } 922 923 static void 924 dhcpcd_prestartinterface(void *arg) 925 { 926 struct interface *ifp = arg; 927 928 if ((!(ifp->ctx->options & DHCPCD_MASTER) || 929 ifp->options->options & DHCPCD_IF_UP) && 930 if_up(ifp) == -1) 931 logerr("%s: %s", __func__, ifp->name); 932 933 if (ifp->options->options & DHCPCD_LINK && 934 ifp->carrier == LINK_UNKNOWN) 935 { 936 int carrier; 937 938 if ((carrier = if_carrier(ifp)) != LINK_UNKNOWN) { 939 dhcpcd_handlecarrier(ifp->ctx, carrier, 940 ifp->flags, ifp->name); 941 return; 942 } 943 loginfox("%s: unknown carrier, waiting for interface flags", 944 ifp->name); 945 } 946 947 dhcpcd_startinterface(ifp); 948 } 949 950 static void 951 run_preinit(struct interface *ifp) 952 { 953 954 if (ifp->ctx->options & DHCPCD_TEST) 955 return; 956 957 script_runreason(ifp, "PREINIT"); 958 959 if (ifp->options->options & DHCPCD_LINK && ifp->carrier != LINK_UNKNOWN) 960 script_runreason(ifp, 961 ifp->carrier == LINK_UP ? "CARRIER" : "NOCARRIER"); 962 } 963 964 void 965 dhcpcd_activateinterface(struct interface *ifp, unsigned long long options) 966 { 967 968 if (!ifp->active) { 969 ifp->active = IF_ACTIVE; 970 dhcpcd_initstate2(ifp, options); 971 /* It's possible we might not have been able to load 972 * a config. */ 973 if (ifp->active) { 974 configure_interface1(ifp); 975 run_preinit(ifp); 976 dhcpcd_prestartinterface(ifp); 977 } 978 } 979 } 980 981 int 982 dhcpcd_handleinterface(void *arg, int action, const char *ifname) 983 { 984 struct dhcpcd_ctx *ctx; 985 struct ifaddrs *ifaddrs; 986 struct if_head *ifs; 987 struct interface *ifp, *iff; 988 const char * const argv[] = { ifname }; 989 990 ctx = arg; 991 if (action == -1) { 992 ifp = if_find(ctx->ifaces, ifname); 993 if (ifp == NULL) { 994 errno = ESRCH; 995 return -1; 996 } 997 if (ifp->active) { 998 logdebugx("%s: interface departed", ifp->name); 999 ifp->options->options |= DHCPCD_DEPARTED; 1000 stop_interface(ifp); 1001 } 1002 TAILQ_REMOVE(ctx->ifaces, ifp, next); 1003 if_free(ifp); 1004 return 0; 1005 } 1006 1007 ifs = if_discover(ctx, &ifaddrs, -1, UNCONST(argv)); 1008 if (ifs == NULL) { 1009 logerr(__func__); 1010 return -1; 1011 } 1012 ifp = if_find(ifs, ifname); 1013 if (ifp == NULL) { 1014 /* This can happen if an interface is quickly added 1015 * and then removed. */ 1016 errno = ENOENT; 1017 return -1; 1018 } 1019 /* Check if we already have the interface */ 1020 iff = if_find(ctx->ifaces, ifp->name); 1021 1022 if (iff != NULL) { 1023 if (iff->active) 1024 logdebugx("%s: interface updated", iff->name); 1025 /* The flags and hwaddr could have changed */ 1026 iff->flags = ifp->flags; 1027 iff->hwlen = ifp->hwlen; 1028 if (ifp->hwlen != 0) 1029 memcpy(iff->hwaddr, ifp->hwaddr, iff->hwlen); 1030 } else { 1031 TAILQ_REMOVE(ifs, ifp, next); 1032 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next); 1033 if (ifp->active) { 1034 logdebugx("%s: interface added", ifp->name); 1035 dhcpcd_initstate(ifp, 0); 1036 run_preinit(ifp); 1037 } 1038 iff = ifp; 1039 } 1040 1041 if (action > 0) { 1042 if_learnaddrs(ctx, ifs, &ifaddrs); 1043 if (iff->active) 1044 dhcpcd_prestartinterface(iff); 1045 } 1046 1047 /* Free our discovered list */ 1048 while ((ifp = TAILQ_FIRST(ifs))) { 1049 TAILQ_REMOVE(ifs, ifp, next); 1050 if_free(ifp); 1051 } 1052 free(ifs); 1053 1054 return 1; 1055 } 1056 1057 static void 1058 dhcpcd_handlelink(void *arg) 1059 { 1060 struct dhcpcd_ctx *ctx = arg; 1061 1062 if (if_handlelink(ctx) == -1) { 1063 if (errno == ENOBUFS || errno == ENOMEM) { 1064 dhcpcd_linkoverflow(ctx); 1065 return; 1066 } 1067 logerr(__func__); 1068 } 1069 } 1070 1071 static void 1072 dhcpcd_checkcarrier(void *arg) 1073 { 1074 struct interface *ifp = arg; 1075 1076 dhcpcd_handlecarrier(ifp->ctx, LINK_UNKNOWN, ifp->flags, ifp->name); 1077 } 1078 1079 void 1080 dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx) 1081 { 1082 struct if_head *ifaces; 1083 struct ifaddrs *ifaddrs; 1084 struct interface *ifp, *ifn, *ifp1; 1085 1086 logerrx("route socket overflowed - learning interface state"); 1087 1088 /* Close the existing socket and open a new one. 1089 * This is easier than draining the kernel buffer of an 1090 * in-determinate size. */ 1091 eloop_event_delete(ctx->eloop, ctx->link_fd); 1092 close(ctx->link_fd); 1093 if_closesockets_os(ctx); 1094 if (if_opensockets_os(ctx) == -1) { 1095 logerr("%s: if_opensockets", __func__); 1096 eloop_exit(ctx->eloop, EXIT_FAILURE); 1097 return; 1098 } 1099 eloop_event_add(ctx->eloop, ctx->link_fd, dhcpcd_handlelink, ctx); 1100 1101 /* Work out the current interfaces. */ 1102 ifaces = if_discover(ctx, &ifaddrs, ctx->ifc, ctx->ifv); 1103 1104 /* Punt departed interfaces */ 1105 TAILQ_FOREACH_SAFE(ifp, ctx->ifaces, next, ifn) { 1106 if (if_find(ifaces, ifp->name) != NULL) 1107 continue; 1108 dhcpcd_handleinterface(ctx, -1, ifp->name); 1109 } 1110 1111 /* Add new interfaces */ 1112 TAILQ_FOREACH_SAFE(ifp, ifaces, next, ifn) { 1113 ifp1 = if_find(ctx->ifaces, ifp->name); 1114 if (ifp1 != NULL) { 1115 /* If the interface already exists, 1116 * check carrier state. */ 1117 eloop_timeout_add_sec(ctx->eloop, 0, 1118 dhcpcd_checkcarrier, ifp1); 1119 continue; 1120 } 1121 TAILQ_REMOVE(ifaces, ifp, next); 1122 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next); 1123 if (ifp->active) 1124 eloop_timeout_add_sec(ctx->eloop, 0, 1125 dhcpcd_prestartinterface, ifp); 1126 } 1127 1128 /* Update address state. */ 1129 if_markaddrsstale(ctx->ifaces); 1130 if_learnaddrs(ctx, ctx->ifaces, &ifaddrs); 1131 if_deletestaleaddrs(ctx->ifaces); 1132 } 1133 1134 void 1135 dhcpcd_handlehwaddr(struct dhcpcd_ctx *ctx, const char *ifname, 1136 const void *hwaddr, uint8_t hwlen) 1137 { 1138 struct interface *ifp; 1139 char buf[sizeof(ifp->hwaddr) * 3]; 1140 1141 ifp = if_find(ctx->ifaces, ifname); 1142 if (ifp == NULL) 1143 return; 1144 1145 if (!if_valid_hwaddr(hwaddr, hwlen)) 1146 hwlen = 0; 1147 1148 if (hwlen > sizeof(ifp->hwaddr)) { 1149 errno = ENOBUFS; 1150 logerr("%s: %s", __func__, ifp->name); 1151 return; 1152 } 1153 1154 if (ifp->hwlen == hwlen && memcmp(ifp->hwaddr, hwaddr, hwlen) == 0) 1155 return; 1156 1157 loginfox("%s: new hardware address: %s", ifp->name, 1158 hwaddr_ntoa(hwaddr, hwlen, buf, sizeof(buf))); 1159 ifp->hwlen = hwlen; 1160 memcpy(ifp->hwaddr, hwaddr, hwlen); 1161 } 1162 1163 static void 1164 if_reboot(struct interface *ifp, int argc, char **argv) 1165 { 1166 unsigned long long oldopts; 1167 1168 oldopts = ifp->options->options; 1169 script_runreason(ifp, "RECONFIGURE"); 1170 dhcpcd_initstate1(ifp, argc, argv, 0); 1171 dhcp_reboot_newopts(ifp, oldopts); 1172 dhcp6_reboot(ifp); 1173 dhcpcd_prestartinterface(ifp); 1174 } 1175 1176 static void 1177 reload_config(struct dhcpcd_ctx *ctx) 1178 { 1179 struct if_options *ifo; 1180 1181 free_globals(ctx); 1182 if ((ifo = read_config(ctx, NULL, NULL, NULL)) == NULL) 1183 return; 1184 add_options(ctx, NULL, ifo, ctx->argc, ctx->argv); 1185 /* We need to preserve these two options. */ 1186 if (ctx->options & DHCPCD_MASTER) 1187 ifo->options |= DHCPCD_MASTER; 1188 if (ctx->options & DHCPCD_DAEMONISED) 1189 ifo->options |= DHCPCD_DAEMONISED; 1190 ctx->options = ifo->options; 1191 free_options(ctx, ifo); 1192 } 1193 1194 static void 1195 reconf_reboot(struct dhcpcd_ctx *ctx, int action, int argc, char **argv, int oi) 1196 { 1197 int i; 1198 struct interface *ifp; 1199 1200 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 1201 for (i = oi; i < argc; i++) { 1202 if (strcmp(ifp->name, argv[i]) == 0) 1203 break; 1204 } 1205 if (oi != argc && i == argc) 1206 continue; 1207 if (ifp->active == IF_ACTIVE_USER) { 1208 if (action) 1209 if_reboot(ifp, argc, argv); 1210 else 1211 ipv4_applyaddr(ifp); 1212 } else if (i != argc) { 1213 ifp->active = IF_ACTIVE_USER; 1214 dhcpcd_initstate1(ifp, argc, argv, 0); 1215 run_preinit(ifp); 1216 dhcpcd_prestartinterface(ifp); 1217 } 1218 } 1219 } 1220 1221 static void 1222 stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts) 1223 { 1224 struct interface *ifp; 1225 1226 ctx->options |= DHCPCD_EXITING; 1227 /* Drop the last interface first */ 1228 TAILQ_FOREACH_REVERSE(ifp, ctx->ifaces, if_head, next) { 1229 if (ifp->active) { 1230 ifp->options->options |= opts; 1231 if (ifp->options->options & DHCPCD_RELEASE) 1232 ifp->options->options &= ~DHCPCD_PERSISTENT; 1233 ifp->options->options |= DHCPCD_EXITING; 1234 stop_interface(ifp); 1235 } 1236 } 1237 } 1238 1239 static void 1240 dhcpcd_ifrenew(struct interface *ifp) 1241 { 1242 1243 if (!ifp->active) 1244 return; 1245 1246 if (ifp->options->options & DHCPCD_LINK && 1247 ifp->carrier == LINK_DOWN) 1248 return; 1249 1250 dhcp_renew(ifp); 1251 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS) 1252 if ((ifp->options->options & DHCPCD_RARENEW) == DHCPCD_RARENEW) 1253 ipv6nd_startrs(ifp); 1254 dhcp6_renew(ifp); 1255 } 1256 1257 static void 1258 dhcpcd_renew(struct dhcpcd_ctx *ctx) 1259 { 1260 struct interface *ifp; 1261 1262 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 1263 dhcpcd_ifrenew(ifp); 1264 } 1265 } 1266 1267 #ifdef USE_SIGNALS 1268 #define sigmsg "received %s, %s" 1269 static void 1270 signal_cb(int sig, void *arg) 1271 { 1272 struct dhcpcd_ctx *ctx = arg; 1273 unsigned long long opts; 1274 int exit_code; 1275 1276 opts = 0; 1277 exit_code = EXIT_FAILURE; 1278 switch (sig) { 1279 case SIGINT: 1280 loginfox(sigmsg, "SIGINT", "stopping"); 1281 break; 1282 case SIGTERM: 1283 loginfox(sigmsg, "SIGTERM", "stopping"); 1284 exit_code = EXIT_SUCCESS; 1285 break; 1286 case SIGALRM: 1287 loginfox(sigmsg, "SIGALRM", "releasing"); 1288 opts |= DHCPCD_RELEASE; 1289 exit_code = EXIT_SUCCESS; 1290 break; 1291 case SIGHUP: 1292 loginfox(sigmsg, "SIGHUP", "rebinding"); 1293 reload_config(ctx); 1294 /* Preserve any options passed on the commandline 1295 * when we were started. */ 1296 reconf_reboot(ctx, 1, ctx->argc, ctx->argv, 1297 ctx->argc - ctx->ifc); 1298 return; 1299 case SIGUSR1: 1300 loginfox(sigmsg, "SIGUSR1", "renewing"); 1301 dhcpcd_renew(ctx); 1302 return; 1303 case SIGUSR2: 1304 loginfox(sigmsg, "SIGUSR2", "reopening log"); 1305 logclose(); 1306 if (logopen(ctx->logfile) == -1) 1307 logerr(__func__); 1308 return; 1309 case SIGPIPE: 1310 logwarnx("received SIGPIPE"); 1311 return; 1312 default: 1313 logerrx("received signal %d but don't know what to do with it", 1314 sig); 1315 return; 1316 } 1317 1318 if (!(ctx->options & DHCPCD_TEST)) 1319 stop_all_interfaces(ctx, opts); 1320 eloop_exit(ctx->eloop, exit_code); 1321 } 1322 #endif 1323 1324 static void 1325 dhcpcd_getinterfaces(void *arg) 1326 { 1327 struct fd_list *fd = arg; 1328 struct interface *ifp; 1329 size_t len; 1330 1331 len = 0; 1332 TAILQ_FOREACH(ifp, fd->ctx->ifaces, next) { 1333 if (!ifp->active) 1334 continue; 1335 len++; 1336 if (D_STATE_RUNNING(ifp)) 1337 len++; 1338 if (IPV4LL_STATE_RUNNING(ifp)) 1339 len++; 1340 if (IPV6_STATE_RUNNING(ifp)) 1341 len++; 1342 if (RS_STATE_RUNNING(ifp)) 1343 len++; 1344 if (D6_STATE_RUNNING(ifp)) 1345 len++; 1346 } 1347 if (write(fd->fd, &len, sizeof(len)) != sizeof(len)) 1348 return; 1349 eloop_event_remove_writecb(fd->ctx->eloop, fd->fd); 1350 TAILQ_FOREACH(ifp, fd->ctx->ifaces, next) { 1351 if (!ifp->active) 1352 continue; 1353 if (send_interface(fd, ifp) == -1) 1354 logerr(__func__); 1355 } 1356 } 1357 1358 int 1359 dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, 1360 int argc, char **argv) 1361 { 1362 struct interface *ifp; 1363 unsigned long long opts; 1364 int opt, oi, do_reboot, do_renew; 1365 size_t len, l; 1366 char *tmp, *p; 1367 1368 /* Special commands for our control socket 1369 * as the other end should be blocking until it gets the 1370 * expected reply we should be safely able just to change the 1371 * write callback on the fd */ 1372 if (strcmp(*argv, "--version") == 0) { 1373 return control_queue(fd, UNCONST(VERSION), 1374 strlen(VERSION) + 1, 0); 1375 } else if (strcmp(*argv, "--getconfigfile") == 0) { 1376 return control_queue(fd, UNCONST(fd->ctx->cffile), 1377 strlen(fd->ctx->cffile) + 1, 0); 1378 } else if (strcmp(*argv, "--getinterfaces") == 0) { 1379 eloop_event_add_w(fd->ctx->eloop, fd->fd, 1380 dhcpcd_getinterfaces, fd); 1381 return 0; 1382 } else if (strcmp(*argv, "--listen") == 0) { 1383 fd->flags |= FD_LISTEN; 1384 return 0; 1385 } 1386 1387 /* Only priviledged users can control dhcpcd via the socket. */ 1388 if (fd->flags & FD_UNPRIV) { 1389 errno = EPERM; 1390 return -1; 1391 } 1392 1393 /* Log the command */ 1394 len = 1; 1395 for (opt = 0; opt < argc; opt++) 1396 len += strlen(argv[opt]) + 1; 1397 tmp = malloc(len); 1398 if (tmp == NULL) 1399 return -1; 1400 p = tmp; 1401 for (opt = 0; opt < argc; opt++) { 1402 l = strlen(argv[opt]); 1403 strlcpy(p, argv[opt], len); 1404 len -= l + 1; 1405 p += l; 1406 *p++ = ' '; 1407 } 1408 *--p = '\0'; 1409 loginfox("control command: %s", tmp); 1410 free(tmp); 1411 1412 optind = 0; 1413 oi = 0; 1414 opts = 0; 1415 do_reboot = do_renew = 0; 1416 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1) 1417 { 1418 switch (opt) { 1419 case 'g': 1420 /* Assumed if below not set */ 1421 break; 1422 case 'k': 1423 opts |= DHCPCD_RELEASE; 1424 break; 1425 case 'n': 1426 do_reboot = 1; 1427 break; 1428 case 'p': 1429 opts |= DHCPCD_PERSISTENT; 1430 break; 1431 case 'x': 1432 opts |= DHCPCD_EXITING; 1433 break; 1434 case 'N': 1435 do_renew = 1; 1436 break; 1437 } 1438 } 1439 1440 if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) { 1441 if (optind == argc) { 1442 stop_all_interfaces(ctx, opts); 1443 eloop_exit(ctx->eloop, EXIT_SUCCESS); 1444 return 0; 1445 } 1446 for (oi = optind; oi < argc; oi++) { 1447 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL) 1448 continue; 1449 if (!ifp->active) 1450 continue; 1451 ifp->options->options |= opts; 1452 if (opts & DHCPCD_RELEASE) 1453 ifp->options->options &= ~DHCPCD_PERSISTENT; 1454 stop_interface(ifp); 1455 } 1456 return 0; 1457 } 1458 1459 if (do_renew) { 1460 if (optind == argc) { 1461 dhcpcd_renew(ctx); 1462 return 0; 1463 } 1464 for (oi = optind; oi < argc; oi++) { 1465 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL) 1466 continue; 1467 dhcpcd_ifrenew(ifp); 1468 } 1469 return 0; 1470 } 1471 1472 reload_config(ctx); 1473 /* XXX: Respect initial commandline options? */ 1474 reconf_reboot(ctx, do_reboot, argc, argv, optind - 1); 1475 return 0; 1476 } 1477 1478 int 1479 main(int argc, char **argv) 1480 { 1481 struct dhcpcd_ctx ctx; 1482 struct ifaddrs *ifaddrs = NULL; 1483 struct if_options *ifo; 1484 struct interface *ifp; 1485 uint16_t family = 0; 1486 int opt, oi = 0, i; 1487 unsigned int logopts; 1488 time_t t; 1489 ssize_t len; 1490 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK) 1491 pid_t pid; 1492 #endif 1493 #ifdef USE_SIGNALS 1494 int sig = 0; 1495 const char *siga = NULL; 1496 #endif 1497 1498 /* Test for --help and --version */ 1499 if (argc > 1) { 1500 if (strcmp(argv[1], "--help") == 0) { 1501 usage(); 1502 return EXIT_SUCCESS; 1503 } else if (strcmp(argv[1], "--version") == 0) { 1504 printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright); 1505 printf("Compiled in features:" 1506 #ifdef INET 1507 " INET" 1508 #endif 1509 #ifdef ARP 1510 " ARP" 1511 #endif 1512 #ifdef ARPING 1513 " ARPing" 1514 #endif 1515 #ifdef IPV4LL 1516 " IPv4LL" 1517 #endif 1518 #ifdef INET6 1519 " INET6" 1520 #endif 1521 #ifdef DHCP6 1522 " DHCPv6" 1523 #endif 1524 #ifdef AUTH 1525 " AUTH" 1526 #endif 1527 "\n"); 1528 return EXIT_SUCCESS; 1529 } 1530 } 1531 1532 memset(&ctx, 0, sizeof(ctx)); 1533 1534 ifo = NULL; 1535 ctx.cffile = CONFIG; 1536 ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1; 1537 ctx.pf_inet_fd = -1; 1538 #ifdef IFLR_ACTIVE 1539 ctx.pf_link_fd = -1; 1540 #endif 1541 1542 TAILQ_INIT(&ctx.control_fds); 1543 #ifdef PLUGIN_DEV 1544 ctx.dev_fd = -1; 1545 #endif 1546 #ifdef INET 1547 ctx.udp_fd = -1; 1548 #endif 1549 rt_init(&ctx); 1550 1551 logopts = LOGERR_ERR|LOGERR_LOG|LOGERR_LOG_DATE|LOGERR_LOG_PID; 1552 i = 0; 1553 while ((opt = getopt_long(argc, argv, 1554 ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, 1555 cf_options, &oi)) != -1) 1556 { 1557 switch (opt) { 1558 case '4': 1559 family = AF_INET; 1560 break; 1561 case '6': 1562 family = AF_INET6; 1563 break; 1564 case 'f': 1565 ctx.cffile = optarg; 1566 break; 1567 case 'j': 1568 free(ctx.logfile); 1569 ctx.logfile = strdup(optarg); 1570 break; 1571 #ifdef USE_SIGNALS 1572 case 'k': 1573 sig = SIGALRM; 1574 siga = "ALRM"; 1575 break; 1576 case 'n': 1577 sig = SIGHUP; 1578 siga = "HUP"; 1579 break; 1580 case 'g': 1581 case 'p': 1582 /* Force going via command socket as we're 1583 * out of user definable signals. */ 1584 i = 4; 1585 break; 1586 case 'q': 1587 logopts |= LOGERR_QUIET; 1588 break; 1589 case 'x': 1590 sig = SIGTERM; 1591 siga = "TERM"; 1592 break; 1593 case 'N': 1594 sig = SIGUSR1; 1595 siga = "USR1"; 1596 break; 1597 #endif 1598 case 'P': 1599 ctx.options |= DHCPCD_PRINT_PIDFILE; 1600 logopts &= ~(LOGERR_LOG | LOGERR_ERR); 1601 break; 1602 case 'T': 1603 i = 1; 1604 logopts &= ~LOGERR_LOG; 1605 break; 1606 case 'U': 1607 i = 3; 1608 break; 1609 case 'V': 1610 i = 2; 1611 break; 1612 case '?': 1613 if (ctx.options & DHCPCD_PRINT_PIDFILE) 1614 continue; 1615 usage(); 1616 goto exit_failure; 1617 } 1618 } 1619 1620 logsetopts(logopts); 1621 logopen(ctx.logfile); 1622 1623 ctx.argv = argv; 1624 ctx.argc = argc; 1625 ctx.ifc = argc - optind; 1626 ctx.ifv = argv + optind; 1627 1628 ifo = read_config(&ctx, NULL, NULL, NULL); 1629 if (ifo == NULL) { 1630 if (ctx.options & DHCPCD_PRINT_PIDFILE) 1631 goto printpidfile; 1632 goto exit_failure; 1633 } 1634 opt = add_options(&ctx, NULL, ifo, argc, argv); 1635 if (opt != 1) { 1636 if (ctx.options & DHCPCD_PRINT_PIDFILE) 1637 goto printpidfile; 1638 if (opt == 0) 1639 usage(); 1640 goto exit_failure; 1641 } 1642 if (i == 2) { 1643 printf("Interface options:\n"); 1644 if (optind == argc - 1) { 1645 free_options(&ctx, ifo); 1646 ifo = read_config(&ctx, argv[optind], NULL, NULL); 1647 if (ifo == NULL) 1648 goto exit_failure; 1649 add_options(&ctx, NULL, ifo, argc, argv); 1650 } 1651 if_printoptions(); 1652 #ifdef INET 1653 if (family == 0 || family == AF_INET) { 1654 printf("\nDHCPv4 options:\n"); 1655 dhcp_printoptions(&ctx, 1656 ifo->dhcp_override, ifo->dhcp_override_len); 1657 } 1658 #endif 1659 #ifdef INET6 1660 if (family == 0 || family == AF_INET6) { 1661 printf("\nND options:\n"); 1662 ipv6nd_printoptions(&ctx, 1663 ifo->nd_override, ifo->nd_override_len); 1664 printf("\nDHCPv6 options:\n"); 1665 dhcp6_printoptions(&ctx, 1666 ifo->dhcp6_override, ifo->dhcp6_override_len); 1667 } 1668 #endif 1669 goto exit_success; 1670 } 1671 ctx.options |= ifo->options; 1672 if (i == 1 || i == 3) { 1673 if (i == 1) 1674 ctx.options |= DHCPCD_TEST; 1675 else 1676 ctx.options |= DHCPCD_DUMPLEASE; 1677 ctx.options |= DHCPCD_PERSISTENT; 1678 ctx.options &= ~DHCPCD_DAEMONISE; 1679 } 1680 1681 #ifdef THERE_IS_NO_FORK 1682 ctx.options &= ~DHCPCD_DAEMONISE; 1683 #endif 1684 1685 if (ctx.options & DHCPCD_DEBUG) 1686 logsetopts(logopts | LOGERR_DEBUG); 1687 1688 if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) { 1689 printpidfile: 1690 /* If we have any other args, we should run as a single dhcpcd 1691 * instance for that interface. */ 1692 if (optind == argc - 1 && !(ctx.options & DHCPCD_MASTER)) { 1693 const char *per; 1694 const char *ifname; 1695 1696 ifname = *ctx.ifv; 1697 if (ifname == NULL || strlen(ifname) > IF_NAMESIZE) { 1698 errno = ifname == NULL ? EINVAL : E2BIG; 1699 logerr("%s: ", ifname); 1700 goto exit_failure; 1701 } 1702 /* Allow a dhcpcd interface per address family */ 1703 switch(family) { 1704 case AF_INET: 1705 per = "-4"; 1706 break; 1707 case AF_INET6: 1708 per = "-6"; 1709 break; 1710 default: 1711 per = ""; 1712 } 1713 snprintf(ctx.pidfile, sizeof(ctx.pidfile), 1714 PIDFILE, "-", ifname, per); 1715 } else { 1716 snprintf(ctx.pidfile, sizeof(ctx.pidfile), 1717 PIDFILE, "", "", ""); 1718 ctx.options |= DHCPCD_MASTER; 1719 } 1720 if (ctx.options & DHCPCD_PRINT_PIDFILE) { 1721 printf("%s\n", ctx.pidfile); 1722 goto exit_success; 1723 } 1724 } 1725 1726 if (chdir("/") == -1) 1727 logerr("%s: chdir `/'", __func__); 1728 1729 /* Freeing allocated addresses from dumping leases can trigger 1730 * eloop removals as well, so init here. */ 1731 if ((ctx.eloop = eloop_new()) == NULL) { 1732 logerr("%s: eloop_init", __func__); 1733 goto exit_failure; 1734 } 1735 #ifdef USE_SIGNALS 1736 /* Save signal mask, block and redirect signals to our handler */ 1737 if (eloop_signal_set_cb(ctx.eloop, 1738 dhcpcd_signals, dhcpcd_signals_len, 1739 signal_cb, &ctx) == -1) 1740 { 1741 logerr("%s: eloop_signal_set_cb", __func__); 1742 goto exit_failure; 1743 } 1744 if (eloop_signal_mask(ctx.eloop, &ctx.sigset) == -1) { 1745 logerr("%s: eloop_signal_mask", __func__); 1746 goto exit_failure; 1747 } 1748 #endif 1749 1750 if (ctx.options & DHCPCD_DUMPLEASE) { 1751 /* Open sockets so we can dump something about 1752 * valid interfaces. */ 1753 if (if_opensockets(&ctx) == -1) { 1754 logerr("%s: if_opensockets", __func__); 1755 goto exit_failure; 1756 } 1757 if (optind != argc) { 1758 /* We need to try and find the interface so we can load 1759 * the hardware address to compare automated IAID */ 1760 ctx.ifaces = if_discover(&ctx, &ifaddrs, 1761 argc - optind, argv + optind); 1762 } else { 1763 if ((ctx.ifaces = malloc(sizeof(*ctx.ifaces))) != NULL) 1764 TAILQ_INIT(ctx.ifaces); 1765 } 1766 if (ctx.ifaces == NULL) { 1767 logerr("%s: if_discover", __func__); 1768 goto exit_failure; 1769 } 1770 ifp = if_find(ctx.ifaces, argv[optind]); 1771 if (ifp == NULL) { 1772 ifp = calloc(1, sizeof(*ifp)); 1773 if (ifp == NULL) { 1774 logerr(__func__); 1775 goto exit_failure; 1776 } 1777 if (optind != argc) 1778 strlcpy(ctx.pidfile, argv[optind], 1779 sizeof(ctx.pidfile)); 1780 ifp->ctx = &ctx; 1781 TAILQ_INSERT_HEAD(ctx.ifaces, ifp, next); 1782 if (family == 0) { 1783 if (ctx.pidfile[0] != '\0' && 1784 ctx.pidfile[strlen(ctx.pidfile) - 1] == '6') 1785 family = AF_INET6; 1786 else 1787 family = AF_INET; 1788 } 1789 } 1790 configure_interface(ifp, ctx.argc, ctx.argv, 0); 1791 i = 0; 1792 if (family == 0 || family == AF_INET) { 1793 if (dhcp_dump(ifp) == -1) 1794 i = -1; 1795 } 1796 if (family == 0 || family == AF_INET6) { 1797 if (dhcp6_dump(ifp) == -1) 1798 i = -1; 1799 } 1800 if (i == -1) 1801 goto exit_failure; 1802 goto exit_success; 1803 } 1804 1805 #ifdef USE_SIGNALS 1806 /* Test against siga instead of sig to avoid gcc 1807 * warning about a bogus potential signed overflow. 1808 * The end result will be the same. */ 1809 if ((siga == NULL || i == 4 || ctx.ifc != 0) && 1810 !(ctx.options & DHCPCD_TEST)) 1811 { 1812 #endif 1813 if (!(ctx.options & DHCPCD_MASTER)) 1814 ctx.control_fd = control_open(argv[optind]); 1815 if (ctx.control_fd == -1) 1816 ctx.control_fd = control_open(NULL); 1817 if (ctx.control_fd != -1) { 1818 loginfox("sending commands to master dhcpcd process"); 1819 len = control_send(&ctx, argc, argv); 1820 control_close(&ctx); 1821 if (len > 0) { 1822 logdebugx("send OK"); 1823 goto exit_success; 1824 } else { 1825 logerr("%s: control_send", __func__); 1826 goto exit_failure; 1827 } 1828 } else { 1829 if (errno != ENOENT) 1830 logerr("%s: control_open", __func__); 1831 } 1832 #ifdef USE_SIGNALS 1833 } 1834 #endif 1835 1836 #ifdef USE_SIGNALS 1837 if (sig != 0) { 1838 pid = pidfile_read(ctx.pidfile); 1839 if (pid != 0 && pid != -1) 1840 loginfox("sending signal %s to pid %d", siga, pid); 1841 if (pid == 0 || pid == -1 || kill(pid, sig) != 0) { 1842 if (sig != SIGHUP && sig != SIGUSR1 && errno != EPERM) 1843 logerrx(PACKAGE" not running"); 1844 if (pid != 0 && pid != -1 && errno != ESRCH) { 1845 logerr("kill"); 1846 goto exit_failure; 1847 } 1848 unlink(ctx.pidfile); 1849 if (sig != SIGHUP && sig != SIGUSR1) 1850 goto exit_failure; 1851 } else { 1852 struct timespec ts; 1853 1854 if (sig == SIGHUP || sig == SIGUSR1) 1855 goto exit_success; 1856 /* Spin until it exits */ 1857 loginfox("waiting for pid %d to exit", pid); 1858 ts.tv_sec = 0; 1859 ts.tv_nsec = 100000000; /* 10th of a second */ 1860 for(i = 0; i < 100; i++) { 1861 nanosleep(&ts, NULL); 1862 if (pidfile_read(ctx.pidfile) == -1) 1863 goto exit_success; 1864 } 1865 logerrx("pid %d failed to exit", pid); 1866 goto exit_failure; 1867 } 1868 } 1869 1870 if (!(ctx.options & DHCPCD_TEST)) { 1871 /* Ensure we have the needed directories */ 1872 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST) 1873 logerr("%s: mkdir `%s'", __func__, RUNDIR); 1874 if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST) 1875 logerr("%s: mkdir `%s'", __func__, DBDIR); 1876 1877 if ((pid = pidfile_lock(ctx.pidfile)) != 0) { 1878 if (pid == -1) 1879 logerr("%s: pidfile_lock", __func__); 1880 else 1881 logerrx(PACKAGE 1882 " already running on pid %d (%s)", 1883 pid, ctx.pidfile); 1884 goto exit_failure; 1885 } 1886 } 1887 1888 if (ctx.options & DHCPCD_MASTER) { 1889 if (control_start(&ctx, NULL) == -1) 1890 logerr("%s: control_start", __func__); 1891 } 1892 #else 1893 if (control_start(&ctx, 1894 ctx.options & DHCPCD_MASTER ? NULL : argv[optind]) == -1) 1895 { 1896 logerr("%s: control_start", __func__); 1897 goto exit_failure; 1898 } 1899 #endif 1900 1901 logdebugx(PACKAGE "-" VERSION " starting"); 1902 ctx.options |= DHCPCD_STARTED; 1903 1904 #ifdef HAVE_SETPROCTITLE 1905 setproctitle("%s%s%s", 1906 ctx.options & DHCPCD_MASTER ? "[master]" : argv[optind], 1907 ctx.options & DHCPCD_IPV4 ? " [ip4]" : "", 1908 ctx.options & DHCPCD_IPV6 ? " [ip6]" : ""); 1909 #endif 1910 1911 if (if_opensockets(&ctx) == -1) { 1912 logerr("%s: if_opensockets", __func__); 1913 goto exit_failure; 1914 } 1915 1916 /* When running dhcpcd against a single interface, we need to retain 1917 * the old behaviour of waiting for an IP address */ 1918 if (ctx.ifc == 1 && !(ctx.options & DHCPCD_BACKGROUND)) 1919 ctx.options |= DHCPCD_WAITIP; 1920 1921 /* Start handling kernel messages for interfaces, addresses and 1922 * routes. */ 1923 eloop_event_add(ctx.eloop, ctx.link_fd, dhcpcd_handlelink, &ctx); 1924 1925 /* Start any dev listening plugin which may want to 1926 * change the interface name provided by the kernel */ 1927 if ((ctx.options & (DHCPCD_MASTER | DHCPCD_DEV)) == 1928 (DHCPCD_MASTER | DHCPCD_DEV)) 1929 dev_start(&ctx); 1930 1931 ctx.ifaces = if_discover(&ctx, &ifaddrs, ctx.ifc, ctx.ifv); 1932 if (ctx.ifaces == NULL) { 1933 logerr("%s: if_discover", __func__); 1934 goto exit_failure; 1935 } 1936 for (i = 0; i < ctx.ifc; i++) { 1937 if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL || 1938 !ifp->active) 1939 logerrx("%s: interface not found or invalid", 1940 ctx.ifv[i]); 1941 } 1942 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 1943 if (ifp->active == IF_ACTIVE_USER) 1944 break; 1945 } 1946 if (ifp == NULL) { 1947 if (ctx.ifc == 0) { 1948 logfunc_t *logfunc; 1949 1950 logfunc = ctx.options & DHCPCD_INACTIVE ? 1951 logdebugx : logerrx; 1952 logfunc("no valid interfaces found"); 1953 } else 1954 goto exit_failure; 1955 if (!(ctx.options & DHCPCD_LINK)) { 1956 logerrx("aborting as link detection is disabled"); 1957 goto exit_failure; 1958 } 1959 } 1960 1961 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 1962 if (ifp->active) 1963 dhcpcd_initstate1(ifp, argc, argv, 0); 1964 } 1965 if_learnaddrs(&ctx, ctx.ifaces, &ifaddrs); 1966 1967 if (ctx.options & DHCPCD_BACKGROUND && dhcpcd_daemonise(&ctx)) 1968 goto exit_success; 1969 1970 opt = 0; 1971 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 1972 if (ifp->active) { 1973 run_preinit(ifp); 1974 if (!(ifp->options->options & DHCPCD_LINK) || 1975 ifp->carrier != LINK_DOWN) 1976 opt = 1; 1977 } 1978 } 1979 1980 if (!(ctx.options & DHCPCD_BACKGROUND)) { 1981 if (ctx.options & DHCPCD_MASTER) 1982 t = ifo->timeout; 1983 else { 1984 t = 0; 1985 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 1986 if (ifp->active) { 1987 t = ifp->options->timeout; 1988 break; 1989 } 1990 } 1991 } 1992 if (opt == 0 && 1993 ctx.options & DHCPCD_LINK && 1994 !(ctx.options & DHCPCD_WAITIP)) 1995 { 1996 logfunc_t *logfunc; 1997 1998 logfunc = ctx.options & DHCPCD_INACTIVE ? 1999 logdebugx : logwarnx; 2000 logfunc("no interfaces have a carrier"); 2001 if (dhcpcd_daemonise(&ctx)) 2002 goto exit_success; 2003 } else if (t > 0 && 2004 /* Test mode removes the daemonise bit, so check for both */ 2005 ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST)) 2006 { 2007 eloop_timeout_add_sec(ctx.eloop, t, 2008 handle_exit_timeout, &ctx); 2009 } 2010 } 2011 free_options(&ctx, ifo); 2012 ifo = NULL; 2013 2014 if_sortinterfaces(&ctx); 2015 TAILQ_FOREACH(ifp, ctx.ifaces, next) { 2016 if (ifp->active) 2017 eloop_timeout_add_sec(ctx.eloop, 0, 2018 dhcpcd_prestartinterface, ifp); 2019 } 2020 2021 i = eloop_start(ctx.eloop, &ctx.sigset); 2022 if (i < 0) { 2023 logerr("%s: eloop_start", __func__); 2024 goto exit_failure; 2025 } 2026 goto exit1; 2027 2028 exit_success: 2029 i = EXIT_SUCCESS; 2030 goto exit1; 2031 2032 exit_failure: 2033 i = EXIT_FAILURE; 2034 2035 exit1: 2036 if (ifaddrs != NULL) 2037 freeifaddrs(ifaddrs); 2038 if (control_stop(&ctx) == -1) 2039 logerr("%s: control_stop", __func__); 2040 /* Free memory and close fd's */ 2041 if (ctx.ifaces) { 2042 while ((ifp = TAILQ_FIRST(ctx.ifaces))) { 2043 TAILQ_REMOVE(ctx.ifaces, ifp, next); 2044 if_free(ifp); 2045 } 2046 free(ctx.ifaces); 2047 } 2048 free_options(&ctx, ifo); 2049 rt_dispose(&ctx); 2050 free(ctx.duid); 2051 if (ctx.link_fd != -1) { 2052 eloop_event_delete(ctx.eloop, ctx.link_fd); 2053 close(ctx.link_fd); 2054 } 2055 if_closesockets(&ctx); 2056 free_globals(&ctx); 2057 ipv6_ctxfree(&ctx); 2058 dev_stop(&ctx); 2059 eloop_free(ctx.eloop); 2060 free(ctx.iov[0].iov_base); 2061 2062 if (ctx.options & DHCPCD_STARTED && !(ctx.options & DHCPCD_FORKED)) 2063 loginfox(PACKAGE " exited"); 2064 logclose(); 2065 free(ctx.logfile); 2066 #ifdef USE_SIGNALS 2067 if (ctx.options & DHCPCD_FORKED) 2068 _exit(i); /* so atexit won't remove our pidfile */ 2069 #endif 2070 return i; 2071 } 2072