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