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