dhcpcd.c revision e86eee143ed21592f88a46623a81f71002430459
1/* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2006-2010 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 copyright[] = "Copyright (c) 2006-2010 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 <arpa/inet.h> 38#include <net/route.h> 39 40#ifdef __linux__ 41# include <asm/types.h> /* for systems with broken headers */ 42# include <linux/rtnetlink.h> 43#endif 44 45#include <ctype.h> 46#include <errno.h> 47#include <getopt.h> 48#include <limits.h> 49#include <paths.h> 50#include <signal.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <syslog.h> 55#include <unistd.h> 56#include <time.h> 57 58#include "arp.h" 59#include "bind.h" 60#include "config.h" 61#include "common.h" 62#include "configure.h" 63#include "control.h" 64#include "dhcpcd.h" 65#include "duid.h" 66#include "eloop.h" 67#include "if-options.h" 68#include "if-pref.h" 69#include "ipv4ll.h" 70#include "net.h" 71#include "signals.h" 72 73/* We should define a maximum for the NAK exponential backoff */ 74#define NAKOFF_MAX 60 75 76/* Wait N nanoseconds between sending a RELEASE and dropping the address. 77 * This gives the kernel enough time to actually send it. */ 78#define RELEASE_DELAY_S 0 79#define RELEASE_DELAY_NS 10000000 80 81int options = 0; 82int pidfd = -1; 83struct interface *ifaces = NULL; 84int ifac = 0; 85char **ifav = NULL; 86int ifdc = 0; 87char **ifdv = NULL; 88 89static char **margv; 90static int margc; 91static struct if_options *if_options; 92static char **ifv; 93static int ifc; 94static char *cffile; 95static char *pidfile; 96static int linkfd = -1; 97 98struct dhcp_op { 99 uint8_t value; 100 const char *name; 101}; 102 103static const struct dhcp_op dhcp_ops[] = { 104 { DHCP_DISCOVER, "DISCOVER" }, 105 { DHCP_OFFER, "OFFER" }, 106 { DHCP_REQUEST, "REQUEST" }, 107 { DHCP_DECLINE, "DECLINE" }, 108 { DHCP_ACK, "ACK" }, 109 { DHCP_NAK, "NAK" }, 110 { DHCP_RELEASE, "RELEASE" }, 111 { DHCP_INFORM, "INFORM" }, 112 { 0, NULL } 113}; 114 115static void send_release(struct interface *); 116 117static const char * 118get_dhcp_op(uint8_t type) 119{ 120 const struct dhcp_op *d; 121 122 for (d = dhcp_ops; d->name; d++) 123 if (d->value == type) 124 return d->name; 125 return NULL; 126} 127 128static pid_t 129read_pid(void) 130{ 131 FILE *fp; 132 pid_t pid; 133 134 if ((fp = fopen(pidfile, "r")) == NULL) { 135 errno = ENOENT; 136 return 0; 137 } 138 if (fscanf(fp, "%d", &pid) != 1) 139 pid = 0; 140 fclose(fp); 141 return pid; 142} 143 144static void 145usage(void) 146{ 147 printf("usage: "PACKAGE" [-dgknpqwxyADEGHJKLOTV] [-c script] [-f file]" 148 " [-e var=val]\n" 149 " [-h hostname] [-i classID ] [-l leasetime]" 150 " [-m metric] [-o option]\n" 151 " [-r ipaddr] [-s ipaddr] [-t timeout]" 152 " [-u userclass]\n" 153 " [-F none|ptr|both] [-I clientID] [-C hookscript]" 154 " [-Q option]\n" 155 " [-X ipaddr] <interface>\n"); 156} 157 158static void 159cleanup(void) 160{ 161#ifdef DEBUG_MEMORY 162 struct interface *iface; 163 int i; 164 165 free_options(if_options); 166 167 while (ifaces) { 168 iface = ifaces; 169 ifaces = iface->next; 170 free_interface(iface); 171 } 172 173 for (i = 0; i < ifac; i++) 174 free(ifav[i]); 175 free(ifav); 176 for (i = 0; i < ifdc; i++) 177 free(ifdv[i]); 178 free(ifdv); 179#endif 180 181 if (linkfd != -1) 182 close(linkfd); 183 if (pidfd > -1) { 184 if (options & DHCPCD_MASTER) { 185 if (stop_control() == -1) 186 syslog(LOG_ERR, "stop_control: %m"); 187 } 188 close(pidfd); 189 unlink(pidfile); 190 } 191#ifdef DEBUG_MEMORY 192 free(pidfile); 193#endif 194} 195 196/* ARGSUSED */ 197void 198handle_exit_timeout(_unused void *arg) 199{ 200 int timeout; 201 202 syslog(LOG_ERR, "timed out"); 203 if (!(options & DHCPCD_TIMEOUT_IPV4LL)) { 204 if (options & DHCPCD_MASTER) { 205 daemonise(); 206 return; 207 } else 208 exit(EXIT_FAILURE); 209 } 210 options &= ~DHCPCD_TIMEOUT_IPV4LL; 211 timeout = (PROBE_NUM * PROBE_MAX) + PROBE_WAIT + 1; 212 syslog(LOG_WARNING, "allowing %d seconds for IPv4LL timeout", timeout); 213 add_timeout_sec(timeout, handle_exit_timeout, NULL); 214} 215 216void 217drop_config(struct interface *iface, const char *reason) 218{ 219 free(iface->state->old); 220 iface->state->old = iface->state->new; 221 iface->state->new = NULL; 222 iface->state->reason = reason; 223 configure(iface); 224 free(iface->state->old); 225 iface->state->old = NULL; 226 iface->state->lease.addr.s_addr = 0; 227} 228 229struct interface * 230find_interface(const char *ifname) 231{ 232 struct interface *ifp; 233 234 for (ifp = ifaces; ifp; ifp = ifp->next) 235 if (strcmp(ifp->name, ifname) == 0) 236 return ifp; 237 return NULL; 238} 239 240static void 241stop_interface(struct interface *iface) 242{ 243 struct interface *ifp, *ifl = NULL; 244 245 syslog(LOG_INFO, "%s: removing interface", iface->name); 246 if (strcmp(iface->state->reason, "RELEASE") != 0) 247 drop_config(iface, "STOP"); 248 close_sockets(iface); 249 delete_timeout(NULL, iface); 250 for (ifp = ifaces; ifp; ifp = ifp->next) { 251 if (ifp == iface) 252 break; 253 ifl = ifp; 254 } 255 if (ifl) 256 ifl->next = ifp->next; 257 else 258 ifaces = ifp->next; 259 free_interface(ifp); 260 if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) 261 exit(EXIT_FAILURE); 262} 263 264static uint32_t 265dhcp_xid(struct interface *iface) 266{ 267 uint32_t xid; 268 269 if (iface->state->options->options & DHCPCD_XID_HWADDR && 270 iface->hwlen >= sizeof(xid)) 271 /* The lower bits are probably more unique on the network */ 272 memcpy(&xid, (iface->hwaddr + iface->hwlen) - sizeof(xid), 273 sizeof(xid)); 274 else 275 xid = arc4random(); 276 277 return xid; 278} 279 280static void 281send_message(struct interface *iface, int type, 282 void (*callback)(void *)) 283{ 284 struct if_state *state = iface->state; 285 struct if_options *ifo = state->options; 286 struct dhcp_message *dhcp; 287 uint8_t *udp; 288 ssize_t len, r; 289 struct in_addr from, to; 290 in_addr_t a = 0; 291 struct timeval tv; 292 293 if (!callback) 294 syslog(LOG_DEBUG, "%s: sending %s with xid 0x%x", 295 iface->name, get_dhcp_op(type), state->xid); 296 else { 297 if (state->interval == 0) 298 state->interval = 4; 299 else { 300 state->interval *= 2; 301 if (state->interval > 64) 302 state->interval = 64; 303 } 304 tv.tv_sec = state->interval + DHCP_RAND_MIN; 305 tv.tv_usec = arc4random() % (DHCP_RAND_MAX_U - DHCP_RAND_MIN_U); 306 syslog(LOG_DEBUG, 307 "%s: sending %s (xid 0x%x), next in %0.2f seconds", 308 iface->name, get_dhcp_op(type), state->xid, 309 timeval_to_double(&tv)); 310 } 311 312 /* Ensure sockets are open. */ 313 open_sockets(iface); 314 315 /* If we couldn't open a UDP port for our IP address 316 * then we cannot renew. 317 * This could happen if our IP was pulled out from underneath us. 318 * Also, we should not unicast from a BOOTP lease. */ 319 if (iface->udp_fd == -1 || 320 (!(ifo->options & DHCPCD_INFORM) && is_bootp(iface->state->new))) 321 { 322 a = iface->addr.s_addr; 323 iface->addr.s_addr = 0; 324 } 325 len = make_message(&dhcp, iface, type); 326 if (a) 327 iface->addr.s_addr = a; 328 from.s_addr = dhcp->ciaddr; 329 if (from.s_addr) 330 to.s_addr = state->lease.server.s_addr; 331 else 332 to.s_addr = 0; 333 if (to.s_addr && to.s_addr != INADDR_BROADCAST) { 334 r = send_packet(iface, to, (uint8_t *)dhcp, len); 335 if (r == -1) { 336 syslog(LOG_ERR, "%s: send_packet: %m", iface->name); 337 close_sockets(iface); 338 } 339 } else { 340 len = make_udp_packet(&udp, (uint8_t *)dhcp, len, from, to); 341 r = send_raw_packet(iface, ETHERTYPE_IP, udp, len); 342 free(udp); 343 /* If we failed to send a raw packet this normally means 344 * we don't have the ability to work beneath the IP layer 345 * for this interface. 346 * As such we remove it from consideration without actually 347 * stopping the interface. */ 348 if (r == -1) { 349 syslog(LOG_ERR, "%s: send_raw_packet: %m", iface->name); 350 if (!(options & DHCPCD_TEST)) 351 drop_config(iface, "FAIL"); 352 close_sockets(iface); 353 delete_timeout(NULL, iface); 354 callback = NULL; 355 } 356 } 357 free(dhcp); 358 359 /* Even if we fail to send a packet we should continue as we are 360 * as our failure timeouts will change out codepath when needed. */ 361 if (callback) 362 add_timeout_tv(&tv, callback, iface); 363} 364 365static void 366send_inform(void *arg) 367{ 368 send_message((struct interface *)arg, DHCP_INFORM, send_inform); 369} 370 371static void 372send_discover(void *arg) 373{ 374 send_message((struct interface *)arg, DHCP_DISCOVER, send_discover); 375} 376 377static void 378send_request(void *arg) 379{ 380 send_message((struct interface *)arg, DHCP_REQUEST, send_request); 381} 382 383static void 384send_renew(void *arg) 385{ 386 send_message((struct interface *)arg, DHCP_REQUEST, send_renew); 387} 388 389static void 390send_rebind(void *arg) 391{ 392 send_message((struct interface *)arg, DHCP_REQUEST, send_rebind); 393} 394 395void 396start_expire(void *arg) 397{ 398 struct interface *iface = arg; 399 400 iface->state->interval = 0; 401 if (iface->addr.s_addr == 0) { 402 /* We failed to reboot, so enter discovery. */ 403 iface->state->lease.addr.s_addr = 0; 404 start_discover(iface); 405 return; 406 } 407 408 syslog(LOG_ERR, "%s: lease expired", iface->name); 409 delete_timeout(NULL, iface); 410 drop_config(iface, "EXPIRE"); 411 unlink(iface->leasefile); 412 if (iface->carrier != LINK_DOWN) 413 start_interface(iface); 414} 415 416static void 417log_dhcp(int lvl, const char *msg, 418 const struct interface *iface, const struct dhcp_message *dhcp, 419 const struct in_addr *from) 420{ 421 const char *tfrom; 422 char *a; 423 struct in_addr addr; 424 int r; 425 426 if (strcmp(msg, "NAK:") == 0) 427 a = get_option_string(dhcp, DHO_MESSAGE); 428 else if (dhcp->yiaddr != 0) { 429 addr.s_addr = dhcp->yiaddr; 430 a = xstrdup(inet_ntoa(addr)); 431 } else 432 a = NULL; 433 434 tfrom = "from"; 435 r = get_option_addr(&addr, dhcp, DHO_SERVERID); 436 if (dhcp->servername[0] && r == 0) 437 syslog(lvl, "%s: %s %s %s %s `%s'", iface->name, msg, a, 438 tfrom, inet_ntoa(addr), dhcp->servername); 439 else { 440 if (r != 0) { 441 tfrom = "via"; 442 addr = *from; 443 } 444 if (a == NULL) 445 syslog(lvl, "%s: %s %s %s", 446 iface->name, msg, tfrom, inet_ntoa(addr)); 447 else 448 syslog(lvl, "%s: %s %s %s %s", 449 iface->name, msg, a, tfrom, inet_ntoa(addr)); 450 } 451 free(a); 452} 453 454static int 455blacklisted_ip(const struct if_options *ifo, in_addr_t addr) 456{ 457 size_t i; 458 459 for (i = 0; i < ifo->blacklist_len; i += 2) 460 if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1])) 461 return 1; 462 return 0; 463} 464 465static int 466whitelisted_ip(const struct if_options *ifo, in_addr_t addr) 467{ 468 size_t i; 469 470 if (ifo->whitelist_len == 0) 471 return -1; 472 for (i = 0; i < ifo->whitelist_len; i += 2) 473 if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1])) 474 return 1; 475 return 0; 476} 477 478static void 479handle_dhcp(struct interface *iface, struct dhcp_message **dhcpp, const struct in_addr *from) 480{ 481 struct if_state *state = iface->state; 482 struct if_options *ifo = state->options; 483 struct dhcp_message *dhcp = *dhcpp; 484 struct dhcp_lease *lease = &state->lease; 485 uint8_t type, tmp; 486 struct in_addr addr; 487 size_t i; 488 489 /* reset the message counter */ 490 state->interval = 0; 491 492 /* We may have found a BOOTP server */ 493 if (get_option_uint8(&type, dhcp, DHO_MESSAGETYPE) == -1) 494 type = 0; 495 496 if (type == DHCP_NAK) { 497 /* For NAK, only check if we require the ServerID */ 498 if (has_option_mask(ifo->requiremask, DHO_SERVERID) && 499 get_option_addr(&addr, dhcp, DHO_SERVERID) == -1) 500 { 501 log_dhcp(LOG_WARNING, "reject NAK", iface, dhcp, from); 502 return; 503 } 504 /* We should restart on a NAK */ 505 log_dhcp(LOG_WARNING, "NAK:", iface, dhcp, from); 506 if (!(options & DHCPCD_TEST)) { 507 drop_config(iface, "NAK"); 508 unlink(iface->leasefile); 509 } 510 close_sockets(iface); 511 /* If we constantly get NAKS then we should slowly back off */ 512 add_timeout_sec(state->nakoff, start_interface, iface); 513 state->nakoff *= 2; 514 if (state->nakoff > NAKOFF_MAX) 515 state->nakoff = NAKOFF_MAX; 516 return; 517 } 518 519 /* Ensure that all required options are present */ 520 for (i = 1; i < 255; i++) { 521 if (has_option_mask(ifo->requiremask, i) && 522 get_option_uint8(&tmp, dhcp, i) != 0) 523 { 524 /* If we are bootp, then ignore the need for serverid. 525 * To ignore bootp, require dhcp_message_type instead. */ 526 if (type == 0 && i == DHO_SERVERID) 527 continue; 528 log_dhcp(LOG_WARNING, "reject DHCP", iface, dhcp, from); 529 return; 530 } 531 } 532 533 /* No NAK, so reset the backoff */ 534 state->nakoff = 1; 535 536 if ((type == 0 || type == DHCP_OFFER) && 537 state->state == DHS_DISCOVER) 538 { 539 lease->frominfo = 0; 540 lease->addr.s_addr = dhcp->yiaddr; 541 lease->cookie = dhcp->cookie; 542 if (type == 0 || 543 get_option_addr(&lease->server, dhcp, DHO_SERVERID) != 0) 544 lease->server.s_addr = INADDR_ANY; 545 log_dhcp(LOG_INFO, "offered", iface, dhcp, from); 546 free(state->offer); 547 state->offer = dhcp; 548 *dhcpp = NULL; 549 if (options & DHCPCD_TEST) { 550 free(state->old); 551 state->old = state->new; 552 state->new = state->offer; 553 state->offer = NULL; 554 state->reason = "TEST"; 555 run_script(iface); 556 exit(EXIT_SUCCESS); 557 } 558 delete_timeout(send_discover, iface); 559 /* We don't request BOOTP addresses */ 560 if (type) { 561 /* We used to ARP check here, but that seems to be in 562 * violation of RFC2131 where it only describes 563 * DECLINE after REQUEST. 564 * It also seems that some MS DHCP servers actually 565 * ignore DECLINE if no REQUEST, ie we decline a 566 * DISCOVER. */ 567 start_request(iface); 568 return; 569 } 570 } 571 572 if (type) { 573 if (type == DHCP_OFFER) { 574 log_dhcp(LOG_INFO, "ignoring offer of", 575 iface, dhcp, from); 576 return; 577 } 578 579 /* We should only be dealing with acks */ 580 if (type != DHCP_ACK) { 581 log_dhcp(LOG_ERR, "not ACK or OFFER", 582 iface, dhcp, from); 583 return; 584 } 585 586 if (!(ifo->options & DHCPCD_INFORM)) 587 log_dhcp(LOG_INFO, "acknowledged", iface, dhcp, from); 588 } 589 590 /* BOOTP could have already assigned this above, so check we still 591 * have a pointer. */ 592 if (*dhcpp) { 593 free(state->offer); 594 state->offer = dhcp; 595 *dhcpp = NULL; 596 } 597 598 lease->frominfo = 0; 599 delete_timeout(NULL, iface); 600 601 /* We now have an offer, so close the DHCP sockets. 602 * This allows us to safely ARP when broken DHCP servers send an ACK 603 * follows by an invalid NAK. */ 604 close_sockets(iface); 605 606 if (ifo->options & DHCPCD_ARP && 607 iface->addr.s_addr != state->offer->yiaddr) 608 { 609 /* If the interface already has the address configured 610 * then we can't ARP for duplicate detection. */ 611 addr.s_addr = state->offer->yiaddr; 612 if (has_address(iface->name, &addr, NULL) != 1) { 613 state->claims = 0; 614 state->probes = 0; 615 state->conflicts = 0; 616 state->state = DHS_PROBE; 617 send_arp_probe(iface); 618 return; 619 } 620 } 621 622 bind_interface(iface); 623} 624 625static void 626handle_dhcp_packet(void *arg) 627{ 628 struct interface *iface = arg; 629 uint8_t *packet; 630 struct dhcp_message *dhcp = NULL; 631 const uint8_t *pp; 632 ssize_t bytes; 633 struct in_addr from; 634 int i; 635 636 /* We loop through until our buffer is empty. 637 * The benefit is that if we get >1 DHCP packet in our buffer and 638 * the first one fails for any reason, we can use the next. */ 639 packet = xmalloc(udp_dhcp_len); 640 for(;;) { 641 bytes = get_raw_packet(iface, ETHERTYPE_IP, 642 packet, udp_dhcp_len); 643 if (bytes == 0 || bytes == -1) 644 break; 645 if (valid_udp_packet(packet, bytes, &from) == -1) { 646 syslog(LOG_ERR, "%s: invalid UDP packet from %s", 647 iface->name, inet_ntoa(from)); 648 continue; 649 } 650 i = whitelisted_ip(iface->state->options, from.s_addr); 651 if (i == 0) { 652 syslog(LOG_WARNING, 653 "%s: non whitelisted DHCP packet from %s", 654 iface->name, inet_ntoa(from)); 655 continue; 656 } else if (i != 1 && 657 blacklisted_ip(iface->state->options, from.s_addr) == 1) 658 { 659 syslog(LOG_WARNING, 660 "%s: blacklisted DHCP packet from %s", 661 iface->name, inet_ntoa(from)); 662 continue; 663 } 664 if (iface->flags & IFF_POINTOPOINT && 665 iface->dst.s_addr != from.s_addr) 666 { 667 syslog(LOG_WARNING, 668 "%s: server %s is not destination", 669 iface->name, inet_ntoa(from)); 670 } 671 bytes = get_udp_data(&pp, packet); 672 if ((size_t)bytes > sizeof(*dhcp)) { 673 syslog(LOG_ERR, 674 "%s: packet greater than DHCP size from %s", 675 iface->name, inet_ntoa(from)); 676 continue; 677 } 678 if (!dhcp) 679 dhcp = xzalloc(sizeof(*dhcp)); 680 memcpy(dhcp, pp, bytes); 681 if (dhcp->cookie != htonl(MAGIC_COOKIE)) { 682 syslog(LOG_DEBUG, "%s: bogus cookie from %s", 683 iface->name, inet_ntoa(from)); 684 continue; 685 } 686 /* Ensure it's the right transaction */ 687 if (iface->state->xid != dhcp->xid) { 688 syslog(LOG_DEBUG, 689 "%s: wrong xid 0x%x (expecting 0x%x) from %s", 690 iface->name, dhcp->xid, iface->state->xid, 691 inet_ntoa(from)); 692 continue; 693 } 694 /* Ensure packet is for us */ 695 if (iface->hwlen <= sizeof(dhcp->chaddr) && 696 memcmp(dhcp->chaddr, iface->hwaddr, iface->hwlen)) 697 { 698 syslog(LOG_DEBUG, "%s: xid 0x%x is not for hwaddr %s", 699 iface->name, dhcp->xid, 700 hwaddr_ntoa(dhcp->chaddr, sizeof(dhcp->chaddr))); 701 continue; 702 } 703 handle_dhcp(iface, &dhcp, &from); 704 if (iface->raw_fd == -1) 705 break; 706 } 707 free(packet); 708 free(dhcp); 709} 710 711static void 712send_release(struct interface *iface) 713{ 714 struct timespec ts; 715 716 if (iface->state->new != NULL && 717 iface->state->new->cookie == htonl(MAGIC_COOKIE)) 718 { 719 syslog(LOG_INFO, "%s: releasing lease of %s", 720 iface->name, inet_ntoa(iface->state->lease.addr)); 721 iface->state->xid = dhcp_xid(iface); 722 send_message(iface, DHCP_RELEASE, NULL); 723 /* Give the packet a chance to go before dropping the ip */ 724 ts.tv_sec = RELEASE_DELAY_S; 725 ts.tv_nsec = RELEASE_DELAY_NS; 726 nanosleep(&ts, NULL); 727 drop_config(iface, "RELEASE"); 728 } 729 unlink(iface->leasefile); 730} 731 732void 733send_decline(struct interface *iface) 734{ 735 send_message(iface, DHCP_DECLINE, NULL); 736} 737 738static void 739configure_interface1(struct interface *iface) 740{ 741 struct if_state *ifs = iface->state; 742 struct if_options *ifo = ifs->options; 743 uint8_t *duid; 744 size_t len = 0, ifl; 745 746 /* Do any platform specific configuration */ 747 if_conf(iface); 748 749 if (iface->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM)) 750 ifo->options |= DHCPCD_STATIC; 751 if (iface->flags & IFF_NOARP || 752 ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) 753 ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL); 754 if (ifo->options & DHCPCD_LINK && carrier_status(iface) == -1) 755 ifo->options &= ~DHCPCD_LINK; 756 757 if (ifo->metric != -1) 758 iface->metric = ifo->metric; 759 760 /* If we haven't specified a ClientID and our hardware address 761 * length is greater than DHCP_CHADDR_LEN then we enforce a ClientID 762 * of the hardware address family and the hardware address. */ 763 if (iface->hwlen > DHCP_CHADDR_LEN) 764 ifo->options |= DHCPCD_CLIENTID; 765 766 /* Firewire and InfiniBand interfaces require ClientID and 767 * the broadcast option being set. */ 768 switch (iface->family) { 769 case ARPHRD_IEEE1394: /* FALLTHROUGH */ 770 case ARPHRD_INFINIBAND: 771 ifo->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST; 772 break; 773 } 774 775 free(iface->clientid); 776 iface->clientid = NULL; 777 if (*ifo->clientid) { 778 iface->clientid = xmalloc(ifo->clientid[0] + 1); 779 memcpy(iface->clientid, ifo->clientid, ifo->clientid[0] + 1); 780 } else if (ifo->options & DHCPCD_CLIENTID) { 781 if (ifo->options & DHCPCD_DUID) { 782 duid = xmalloc(DUID_LEN); 783 if ((len = get_duid(duid, iface)) == 0) 784 syslog(LOG_ERR, "get_duid: %m"); 785 } 786 if (len > 0) { 787 iface->clientid = xmalloc(len + 6); 788 iface->clientid[0] = len + 5; 789 iface->clientid[1] = 255; /* RFC 4361 */ 790 ifl = strlen(iface->name); 791 if (ifl < 5) { 792 memcpy(iface->clientid + 2, iface->name, ifl); 793 if (ifl < 4) 794 memset(iface->clientid + 2 + ifl, 795 0, 4 - ifl); 796 } else { 797 ifl = htonl(if_nametoindex(iface->name)); 798 memcpy(iface->clientid + 2, &ifl, 4); 799 } 800 } else if (len == 0) { 801 len = iface->hwlen + 1; 802 iface->clientid = xmalloc(len + 1); 803 iface->clientid[0] = len; 804 iface->clientid[1] = iface->family; 805 memcpy(iface->clientid + 2, iface->hwaddr, 806 iface->hwlen); 807 } 808 } 809 if (ifo->options & DHCPCD_CLIENTID) 810 syslog(LOG_DEBUG, "%s: using ClientID %s", iface->name, 811 hwaddr_ntoa(iface->clientid + 1, *iface->clientid)); 812 else 813 syslog(LOG_DEBUG, "%s: using hwaddr %s", iface->name, 814 hwaddr_ntoa(iface->hwaddr, iface->hwlen)); 815} 816 817int 818select_profile(struct interface *iface, const char *profile) 819{ 820 struct if_options *ifo; 821 int ret; 822 823 ret = 0; 824 ifo = read_config(cffile, iface->name, iface->ssid, profile); 825 if (ifo == NULL) { 826 syslog(LOG_DEBUG, "%s: no profile %s", iface->name, profile); 827 ret = -1; 828 goto exit; 829 } 830 if (profile != NULL) { 831 strlcpy(iface->state->profile, profile, 832 sizeof(iface->state->profile)); 833 syslog(LOG_INFO, "%s: selected profile %s", 834 iface->name, profile); 835 } else 836 *iface->state->profile = '\0'; 837 free_options(iface->state->options); 838 iface->state->options = ifo; 839 840exit: 841 if (profile) 842 configure_interface1(iface); 843 return ret; 844} 845 846static void 847start_fallback(void *arg) 848{ 849 struct interface *iface; 850 851 iface = (struct interface *)arg; 852 select_profile(iface, iface->state->options->fallback); 853 start_interface(iface); 854} 855 856static void 857configure_interface(struct interface *iface, int argc, char **argv) 858{ 859 select_profile(iface, NULL); 860 add_options(iface->state->options, argc, argv); 861 configure_interface1(iface); 862} 863 864static void 865handle_carrier(const char *ifname) 866{ 867 struct interface *iface; 868 int carrier; 869 870 if (!(options & DHCPCD_LINK)) 871 return; 872 for (iface = ifaces; iface; iface = iface->next) 873 if (strcmp(iface->name, ifname) == 0) 874 break; 875 if (!iface || !(iface->state->options->options & DHCPCD_LINK)) 876 return; 877 carrier = carrier_status(iface); 878 if (carrier == -1) 879 syslog(LOG_ERR, "%s: carrier_status: %m", ifname); 880 else if (carrier == 0 || !(iface->flags & IFF_RUNNING)) { 881 if (iface->carrier != LINK_DOWN) { 882 iface->carrier = LINK_DOWN; 883 syslog(LOG_INFO, "%s: carrier lost", iface->name); 884 close_sockets(iface); 885 delete_timeouts(iface, start_expire, NULL); 886 drop_config(iface, "NOCARRIER"); 887 } 888 } else if (carrier == 1 && (iface->flags & IFF_RUNNING)) { 889 if (iface->carrier != LINK_UP) { 890 iface->carrier = LINK_UP; 891 syslog(LOG_INFO, "%s: carrier acquired", iface->name); 892 if (iface->wireless) 893 getifssid(iface->name, iface->ssid); 894 configure_interface(iface, margc, margv); 895 iface->state->interval = 0; 896 iface->state->reason = "CARRIER"; 897 run_script(iface); 898 start_interface(iface); 899 } 900 } 901} 902 903void 904start_discover(void *arg) 905{ 906 struct interface *iface = arg; 907 struct if_options *ifo = iface->state->options; 908 909 iface->state->state = DHS_DISCOVER; 910 iface->state->xid = dhcp_xid(iface); 911 delete_timeout(NULL, iface); 912 if (ifo->fallback) 913 add_timeout_sec(ifo->timeout, start_fallback, iface); 914 else if (ifo->options & DHCPCD_IPV4LL && 915 !IN_LINKLOCAL(htonl(iface->addr.s_addr))) 916 { 917 if (IN_LINKLOCAL(htonl(iface->state->fail.s_addr))) 918 add_timeout_sec(RATE_LIMIT_INTERVAL, start_ipv4ll, iface); 919 else 920 add_timeout_sec(ifo->timeout, start_ipv4ll, iface); 921 } 922 syslog(LOG_INFO, "%s: broadcasting for a lease", iface->name); 923 send_discover(iface); 924} 925 926void 927start_request(void *arg) 928{ 929 struct interface *iface = arg; 930 931 iface->state->state = DHS_REQUEST; 932 send_request(iface); 933} 934 935void 936start_renew(void *arg) 937{ 938 struct interface *iface = arg; 939 940 syslog(LOG_INFO, "%s: renewing lease of %s", 941 iface->name, inet_ntoa(iface->state->lease.addr)); 942 iface->state->state = DHS_RENEW; 943 iface->state->xid = dhcp_xid(iface); 944 send_renew(iface); 945} 946 947void 948start_rebind(void *arg) 949{ 950 struct interface *iface = arg; 951 952 syslog(LOG_ERR, "%s: failed to renew, attempting to rebind", 953 iface->name); 954 iface->state->state = DHS_REBIND; 955 delete_timeout(send_renew, iface); 956 iface->state->lease.server.s_addr = 0; 957 send_rebind(iface); 958} 959 960static void 961start_timeout(void *arg) 962{ 963 struct interface *iface = arg; 964 965 bind_interface(iface); 966 iface->state->interval = 0; 967 start_discover(iface); 968} 969 970static struct dhcp_message * 971dhcp_message_new(struct in_addr *addr, struct in_addr *mask) 972{ 973 struct dhcp_message *dhcp; 974 uint8_t *p; 975 976 dhcp = xzalloc(sizeof(*dhcp)); 977 dhcp->yiaddr = addr->s_addr; 978 p = dhcp->options; 979 if (mask && mask->s_addr != INADDR_ANY) { 980 *p++ = DHO_SUBNETMASK; 981 *p++ = sizeof(mask->s_addr); 982 memcpy(p, &mask->s_addr, sizeof(mask->s_addr)); 983 p+= sizeof(mask->s_addr); 984 } 985 *p++ = DHO_END; 986 return dhcp; 987} 988 989static int 990handle_3rdparty(struct interface *iface) 991{ 992 struct if_options *ifo; 993 struct in_addr addr, net, dst; 994 995 ifo = iface->state->options; 996 if (ifo->req_addr.s_addr != INADDR_ANY) 997 return 0; 998 999 if (get_address(iface->name, &addr, &net, &dst) == 1) 1000 handle_ifa(RTM_NEWADDR, iface->name, &addr, &net, &dst); 1001 else { 1002 syslog(LOG_INFO, 1003 "%s: waiting for 3rd party to configure IP address", 1004 iface->name); 1005 iface->state->reason = "3RDPARTY"; 1006 run_script(iface); 1007 } 1008 return 1; 1009} 1010 1011static void 1012start_static(struct interface *iface) 1013{ 1014 struct if_options *ifo; 1015 1016 if (handle_3rdparty(iface)) 1017 return; 1018 ifo = iface->state->options; 1019 iface->state->offer = 1020 dhcp_message_new(&ifo->req_addr, &ifo->req_mask); 1021 delete_timeout(NULL, iface); 1022 bind_interface(iface); 1023} 1024 1025static void 1026start_inform(struct interface *iface) 1027{ 1028 if (handle_3rdparty(iface)) 1029 return; 1030 1031 if (options & DHCPCD_TEST) { 1032 iface->addr.s_addr = iface->state->options->req_addr.s_addr; 1033 iface->net.s_addr = iface->state->options->req_mask.s_addr; 1034 } else { 1035 iface->state->options->options |= DHCPCD_STATIC; 1036 start_static(iface); 1037 } 1038 1039 iface->state->state = DHS_INFORM; 1040 iface->state->xid = dhcp_xid(iface); 1041 send_inform(iface); 1042} 1043 1044void 1045start_reboot(struct interface *iface) 1046{ 1047 struct if_options *ifo = iface->state->options; 1048 1049 if (ifo->options & DHCPCD_LINK && iface->carrier == LINK_DOWN) { 1050 syslog(LOG_INFO, "%s: waiting for carrier", iface->name); 1051 return; 1052 } 1053 if (ifo->options & DHCPCD_STATIC) { 1054 start_static(iface); 1055 return; 1056 } 1057 if (ifo->reboot == 0 || iface->state->offer == NULL) { 1058 start_discover(iface); 1059 return; 1060 } 1061 if (ifo->options & DHCPCD_INFORM) { 1062 syslog(LOG_INFO, "%s: informing address of %s", 1063 iface->name, inet_ntoa(iface->state->lease.addr)); 1064 } else if (iface->state->offer->cookie == 0) { 1065 if (ifo->options & DHCPCD_IPV4LL) { 1066 iface->state->claims = 0; 1067 send_arp_announce(iface); 1068 } else 1069 start_discover(iface); 1070 return; 1071 } else { 1072 syslog(LOG_INFO, "%s: rebinding lease of %s", 1073 iface->name, inet_ntoa(iface->state->lease.addr)); 1074 } 1075 iface->state->state = DHS_REBOOT; 1076 iface->state->xid = dhcp_xid(iface); 1077 iface->state->lease.server.s_addr = 0; 1078 delete_timeout(NULL, iface); 1079 if (ifo->fallback) 1080 add_timeout_sec(ifo->reboot, start_fallback, iface); 1081 else if (ifo->options & DHCPCD_LASTLEASE && 1082 iface->state->lease.frominfo) 1083 add_timeout_sec(ifo->reboot, start_timeout, iface); 1084 else if (!(ifo->options & DHCPCD_INFORM && 1085 options & (DHCPCD_MASTER | DHCPCD_DAEMONISED))) 1086 add_timeout_sec(ifo->reboot, start_expire, iface); 1087 /* Don't bother ARP checking as the server could NAK us first. */ 1088 if (ifo->options & DHCPCD_INFORM) 1089 send_inform(iface); 1090 else 1091 send_request(iface); 1092} 1093 1094void 1095start_interface(void *arg) 1096{ 1097 struct interface *iface = arg; 1098 struct if_options *ifo = iface->state->options; 1099 struct stat st; 1100 struct timeval now; 1101 uint32_t l; 1102 int nolease; 1103 1104 handle_carrier(iface->name); 1105 if (iface->carrier == LINK_DOWN) { 1106 syslog(LOG_INFO, "%s: waiting for carrier", iface->name); 1107 return; 1108 } 1109 1110 iface->start_uptime = uptime(); 1111 free(iface->state->offer); 1112 iface->state->offer = NULL; 1113 1114 if (iface->state->arping_index < ifo->arping_len) { 1115 start_arping(iface); 1116 return; 1117 } 1118 if (ifo->options & DHCPCD_STATIC) { 1119 start_static(iface); 1120 return; 1121 } 1122 if (ifo->options & DHCPCD_INFORM) { 1123 start_inform(iface); 1124 return; 1125 } 1126 if (iface->hwlen == 0 && ifo->clientid[0] == '\0') { 1127 syslog(LOG_WARNING, "%s: needs a clientid to configure", 1128 iface->name); 1129 drop_config(iface, "FAIL"); 1130 close_sockets(iface); 1131 delete_timeout(NULL, iface); 1132 return; 1133 } 1134 /* We don't want to read the old lease if we NAK an old test */ 1135 nolease = iface->state->offer && options & DHCPCD_TEST; 1136 if (!nolease) 1137 iface->state->offer = read_lease(iface); 1138 if (iface->state->offer) { 1139 get_lease(&iface->state->lease, iface->state->offer); 1140 iface->state->lease.frominfo = 1; 1141 if (iface->state->offer->cookie == 0) { 1142 if (iface->state->offer->yiaddr == 1143 iface->addr.s_addr) 1144 { 1145 free(iface->state->offer); 1146 iface->state->offer = NULL; 1147 } 1148 } else if (iface->state->lease.leasetime != ~0U && 1149 stat(iface->leasefile, &st) == 0) 1150 { 1151 /* Offset lease times and check expiry */ 1152 gettimeofday(&now, NULL); 1153 if ((time_t)iface->state->lease.leasetime < 1154 now.tv_sec - st.st_mtime) 1155 { 1156 syslog(LOG_DEBUG, 1157 "%s: discarding expired lease", 1158 iface->name); 1159 free(iface->state->offer); 1160 iface->state->offer = NULL; 1161 iface->state->lease.addr.s_addr = 0; 1162 } else { 1163 l = now.tv_sec - st.st_mtime; 1164 iface->state->lease.leasetime -= l; 1165 iface->state->lease.renewaltime -= l; 1166 iface->state->lease.rebindtime -= l; 1167 } 1168 } 1169 } 1170 if (iface->state->offer == NULL) 1171 start_discover(iface); 1172 else if (iface->state->offer->cookie == 0 && 1173 iface->state->options->options & DHCPCD_IPV4LL) 1174 start_ipv4ll(iface); 1175 else 1176 start_reboot(iface); 1177} 1178 1179static void 1180init_state(struct interface *iface, int argc, char **argv) 1181{ 1182 struct if_state *ifs; 1183 1184 if (iface->state) 1185 ifs = iface->state; 1186 else 1187 ifs = iface->state = xzalloc(sizeof(*ifs)); 1188 1189 ifs->state = DHS_INIT; 1190 ifs->reason = "PREINIT"; 1191 ifs->nakoff = 1; 1192 configure_interface(iface, argc, argv); 1193 if (!(options & DHCPCD_TEST)) 1194 run_script(iface); 1195 /* We need to drop the leasefile so that start_interface 1196 * doesn't load it. */ 1197 if (ifs->options->options & DHCPCD_REQUEST) 1198 unlink(iface->leasefile); 1199 1200 if (ifs->options->options & DHCPCD_LINK) { 1201 switch (carrier_status(iface)) { 1202 case 0: 1203 iface->carrier = LINK_DOWN; 1204 ifs->reason = "NOCARRIER"; 1205 break; 1206 case 1: 1207 iface->carrier = LINK_UP; 1208 ifs->reason = "CARRIER"; 1209 break; 1210 default: 1211 iface->carrier = LINK_UNKNOWN; 1212 return; 1213 } 1214 if (!(options & DHCPCD_TEST)) 1215 run_script(iface); 1216 } else 1217 iface->carrier = LINK_UNKNOWN; 1218} 1219 1220void 1221handle_interface(int action, const char *ifname) 1222{ 1223 struct interface *ifs, *ifp, *ifn, *ifl = NULL; 1224 const char * const argv[] = { ifname }; 1225 int i; 1226 1227 if (action == -1) { 1228 ifp = find_interface(ifname); 1229 if (ifp != NULL) 1230 stop_interface(ifp); 1231 return; 1232 } else if (action == 0) { 1233 handle_carrier(ifname); 1234 return; 1235 } 1236 1237 /* If running off an interface list, check it's in it. */ 1238 if (ifc) { 1239 for (i = 0; i < ifc; i++) 1240 if (strcmp(ifv[i], ifname) == 0) 1241 break; 1242 if (i >= ifc) 1243 return; 1244 } 1245 1246 ifs = discover_interfaces(-1, UNCONST(argv)); 1247 for (ifp = ifs; ifp; ifp = ifp->next) { 1248 if (strcmp(ifp->name, ifname) != 0) 1249 continue; 1250 /* Check if we already have the interface */ 1251 for (ifn = ifaces; ifn; ifn = ifn->next) { 1252 if (strcmp(ifn->name, ifp->name) == 0) 1253 break; 1254 ifl = ifn; 1255 } 1256 if (ifn) { 1257 /* The flags and hwaddr could have changed */ 1258 ifn->flags = ifp->flags; 1259 ifn->hwlen = ifp->hwlen; 1260 if (ifp->hwlen != 0) 1261 memcpy(ifn->hwaddr, ifp->hwaddr, ifn->hwlen); 1262 } else { 1263 if (ifl) 1264 ifl->next = ifp; 1265 else 1266 ifaces = ifp; 1267 } 1268 init_state(ifp, 0, NULL); 1269 start_interface(ifp); 1270 } 1271} 1272 1273#ifdef RTM_CHGADDR 1274void 1275handle_hwaddr(const char *ifname, unsigned char *hwaddr, size_t hwlen) 1276{ 1277 struct interface *ifp; 1278 struct if_options *ifo; 1279 1280 for (ifp = ifaces; ifp; ifp = ifp->next) 1281 if (strcmp(ifp->name, ifname) == 0 && ifp->hwlen <= hwlen) { 1282 ifo = ifp->state->options; 1283 if (!(ifo->options & 1284 (DHCPCD_INFORM | DHCPCD_STATIC | DHCPCD_CLIENTID)) 1285 && ifp->state->new != NULL && 1286 ifp->state->new->cookie == htonl(MAGIC_COOKIE)) 1287 { 1288 syslog(LOG_INFO, 1289 "%s: expiring for new hardware address", 1290 ifp->name); 1291 drop_config(ifp, "EXPIRE"); 1292 } 1293 memcpy(ifp->hwaddr, hwaddr, hwlen); 1294 ifp->hwlen = hwlen; 1295 if (!(ifo->options & 1296 (DHCPCD_INFORM | DHCPCD_STATIC | DHCPCD_CLIENTID))) 1297 { 1298 syslog(LOG_DEBUG, "%s: using hwaddr %s", 1299 ifp->name, 1300 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen)); 1301 ifp->state->interval = 0; 1302 ifp->state->nakoff = 1; 1303 start_interface(ifp); 1304 } 1305 } 1306 free(hwaddr); 1307} 1308#endif 1309 1310void 1311handle_ifa(int type, const char *ifname, 1312 struct in_addr *addr, struct in_addr *net, struct in_addr *dst) 1313{ 1314 struct interface *ifp; 1315 struct if_options *ifo; 1316 int i; 1317 1318 if (addr->s_addr == INADDR_ANY) 1319 return; 1320 for (ifp = ifaces; ifp; ifp = ifp->next) 1321 if (strcmp(ifp->name, ifname) == 0) 1322 break; 1323 if (ifp == NULL) 1324 return; 1325 ifo = ifp->state->options; 1326 if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) == 0 || 1327 ifo->req_addr.s_addr != INADDR_ANY) 1328 return; 1329 1330 switch (type) { 1331 case RTM_DELADDR: 1332 if (ifp->state->new && 1333 ifp->state->new->yiaddr == addr->s_addr) 1334 drop_config(ifp, "EXPIRE"); 1335 break; 1336 case RTM_NEWADDR: 1337 free(ifp->state->old); 1338 ifp->state->old = ifp->state->new; 1339 ifp->state->new = dhcp_message_new(addr, net); 1340 ifp->dst.s_addr = dst ? dst->s_addr : INADDR_ANY; 1341 if (dst) { 1342 for (i = 1; i < 255; i++) 1343 if (i != DHO_ROUTER && 1344 has_option_mask(ifo->dstmask, i)) 1345 dhcp_message_add_addr( 1346 ifp->state->new, 1347 i, *dst); 1348 } 1349 ifp->state->reason = "STATIC"; 1350 build_routes(); 1351 run_script(ifp); 1352 if (ifo->options & DHCPCD_INFORM) { 1353 ifp->state->state = DHS_INFORM; 1354 ifp->state->xid = dhcp_xid(ifp); 1355 ifp->state->lease.server.s_addr = 1356 dst ? dst->s_addr : INADDR_ANY; 1357 ifp->addr = *addr; 1358 ifp->net = *net; 1359 send_inform(ifp); 1360 } 1361 break; 1362 } 1363} 1364 1365/* ARGSUSED */ 1366static void 1367handle_link(_unused void *arg) 1368{ 1369 if (manage_link(linkfd) == -1) 1370 syslog(LOG_ERR, "manage_link: %m"); 1371} 1372 1373static void 1374if_reboot(struct interface *iface, int argc, char **argv) 1375{ 1376 const struct if_options *ifo; 1377 int opt; 1378 1379 ifo = iface->state->options; 1380 opt = ifo->options; 1381 configure_interface(iface, argc, argv); 1382 ifo = iface->state->options; 1383 iface->state->interval = 0; 1384 if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) && 1385 iface->addr.s_addr != ifo->req_addr.s_addr) || 1386 (opt & (DHCPCD_INFORM | DHCPCD_STATIC) && 1387 !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))) 1388 { 1389 drop_config(iface, "EXPIRE"); 1390 } else { 1391 free(iface->state->offer); 1392 iface->state->offer = NULL; 1393 } 1394 start_interface(iface); 1395} 1396 1397static void 1398reconf_reboot(int action, int argc, char **argv, int oi) 1399{ 1400 struct interface *ifl, *ifn, *ifp, *ifs, *ift; 1401 1402 ifs = discover_interfaces(argc - oi, argv + oi); 1403 if (ifs == NULL) 1404 return; 1405 1406 /* Remove any old interfaces */ 1407 if (ifaces) { 1408 for (ifl = NULL; ifl != ifaces;) { 1409 /* Work our way backwards */ 1410 for (ifp = ifaces; ifp; ifp = ifp->next) 1411 if (ifp->next == ifl) { 1412 ifl = ifp; 1413 break; 1414 } 1415 for (ifn = ifs; ifn; ifn = ifn->next) 1416 if (strcmp(ifn->name, ifp->name) == 0) 1417 break; 1418 if (ifn == NULL) { 1419 ifl = ifp->next; 1420 stop_interface(ifp); 1421 } 1422 } 1423 } 1424 1425 for (ifp = ifs; ifp && (ift = ifp->next, 1); ifp = ift) { 1426 ifl = NULL; 1427 for (ifn = ifaces; ifn; ifn = ifn->next) { 1428 if (strcmp(ifn->name, ifp->name) == 0) 1429 break; 1430 ifl = ifn; 1431 } 1432 if (ifn) { 1433 if (action) 1434 if_reboot(ifn, argc, argv); 1435 else if (ifn->state->new) 1436 configure(ifn); 1437 free_interface(ifp); 1438 } else { 1439 ifp->next = NULL; 1440 init_state(ifp, argc, argv); 1441 start_interface(ifp); 1442 if (ifl) 1443 ifl->next = ifp; 1444 else 1445 ifaces = ifp; 1446 } 1447 } 1448 1449 sort_interfaces(); 1450} 1451 1452/* ARGSUSED */ 1453static void 1454handle_signal(_unused void *arg) 1455{ 1456 struct interface *ifp, *ifl; 1457 struct if_options *ifo; 1458 int sig = signal_read(); 1459 int do_release, do_rebind, i; 1460 1461 do_rebind = do_release = 0; 1462 switch (sig) { 1463 case SIGINT: 1464 syslog(LOG_INFO, "received SIGINT, stopping"); 1465 break; 1466 case SIGTERM: 1467 syslog(LOG_INFO, "received SIGTERM, stopping"); 1468 break; 1469 case SIGALRM: 1470 syslog(LOG_INFO, "received SIGALRM, rebinding"); 1471 for (i = 0; i < ifac; i++) 1472 free(ifav[i]); 1473 free(ifav); 1474 ifav = NULL; 1475 ifac = 0; 1476 for (i = 0; i < ifdc; i++) 1477 free(ifdv[i]); 1478 free(ifdv); 1479 ifdc = 0; 1480 ifdv = NULL; 1481 ifo = read_config(cffile, NULL, NULL, NULL); 1482 add_options(ifo, margc, margv); 1483 /* We need to preserve these two options. */ 1484 if (options & DHCPCD_MASTER) 1485 ifo->options |= DHCPCD_MASTER; 1486 if (options & DHCPCD_DAEMONISED) 1487 ifo->options |= DHCPCD_DAEMONISED; 1488 options = ifo->options; 1489 free_options(ifo); 1490 reconf_reboot(1, 0, NULL, 0); 1491 return; 1492 case SIGHUP: 1493 syslog(LOG_INFO, "received SIGHUP, releasing"); 1494 do_release = 1; 1495 break; 1496 case SIGUSR1: 1497 syslog(LOG_INFO, "received SIGUSR, reconfiguring"); 1498 for (ifp = ifaces; ifp; ifp = ifp->next) 1499 if (ifp->state->new) 1500 configure(ifp); 1501 return; 1502 case SIGPIPE: 1503 syslog(LOG_WARNING, "received SIGPIPE"); 1504 return; 1505 default: 1506 syslog(LOG_ERR, 1507 "received signal %d, but don't know what to do with it", 1508 sig); 1509 return; 1510 } 1511 1512 if (options & DHCPCD_TEST) 1513 exit(EXIT_FAILURE); 1514 1515 /* As drop_config could re-arrange the order, we do it like this. */ 1516 for (;;) { 1517 /* Be sane and drop the last config first */ 1518 ifl = NULL; 1519 for (ifp = ifaces; ifp; ifp = ifp->next) { 1520 if (ifp->next == NULL) 1521 break; 1522 ifl = ifp; 1523 } 1524 if (ifp == NULL) 1525 break; 1526 if (ifp->carrier != LINK_DOWN && 1527 (do_release || 1528 ifp->state->options->options & DHCPCD_RELEASE)) 1529 send_release(ifp); 1530 stop_interface(ifp); 1531 } 1532 exit(EXIT_FAILURE); 1533} 1534 1535int 1536handle_args(struct fd_list *fd, int argc, char **argv) 1537{ 1538 struct interface *ifp; 1539 int do_exit = 0, do_release = 0, do_reboot = 0, do_reconf = 0; 1540 int opt, oi = 0; 1541 ssize_t len; 1542 size_t l; 1543 struct iovec iov[2]; 1544 char *tmp, *p; 1545 1546 if (fd != NULL) { 1547 /* Special commands for our control socket */ 1548 if (strcmp(*argv, "--version") == 0) { 1549 len = strlen(VERSION) + 1; 1550 iov[0].iov_base = &len; 1551 iov[0].iov_len = sizeof(ssize_t); 1552 iov[1].iov_base = UNCONST(VERSION); 1553 iov[1].iov_len = len; 1554 if (writev(fd->fd, iov, 2) == -1) { 1555 syslog(LOG_ERR, "writev: %m"); 1556 return -1; 1557 } 1558 return 0; 1559 } else if (strcmp(*argv, "--getconfigfile") == 0) { 1560 len = strlen(cffile ? cffile : CONFIG) + 1; 1561 iov[0].iov_base = &len; 1562 iov[0].iov_len = sizeof(ssize_t); 1563 iov[1].iov_base = cffile ? cffile : UNCONST(CONFIG); 1564 iov[1].iov_len = len; 1565 if (writev(fd->fd, iov, 2) == -1) { 1566 syslog(LOG_ERR, "writev: %m"); 1567 return -1; 1568 } 1569 return 0; 1570 } else if (strcmp(*argv, "--getinterfaces") == 0) { 1571 len = 0; 1572 if (argc == 1) { 1573 for (ifp = ifaces; ifp; ifp = ifp->next) 1574 len++; 1575 len = write(fd->fd, &len, sizeof(len)); 1576 if (len != sizeof(len)) 1577 return -1; 1578 for (ifp = ifaces; ifp; ifp = ifp->next) 1579 send_interface(fd->fd, ifp); 1580 return 0; 1581 } 1582 opt = 0; 1583 while (argv[++opt] != NULL) { 1584 for (ifp = ifaces; ifp; ifp = ifp->next) 1585 if (strcmp(argv[opt], ifp->name) == 0) 1586 len++; 1587 } 1588 len = write(fd->fd, &len, sizeof(len)); 1589 if (len != sizeof(len)) 1590 return -1; 1591 opt = 0; 1592 while (argv[++opt] != NULL) { 1593 for (ifp = ifaces; ifp; ifp = ifp->next) 1594 if (strcmp(argv[opt], ifp->name) == 0) 1595 send_interface(fd->fd, ifp); 1596 } 1597 return 0; 1598 } else if (strcmp(*argv, "--listen") == 0) { 1599 fd->listener = 1; 1600 return 0; 1601 } 1602 } 1603 1604 /* Log the command */ 1605 len = 0; 1606 for (opt = 0; opt < argc; opt++) 1607 len += strlen(argv[opt]) + 1; 1608 tmp = p = xmalloc(len + 1); 1609 for (opt = 0; opt < argc; opt++) { 1610 l = strlen(argv[opt]); 1611 strlcpy(p, argv[opt], l + 1); 1612 p += l; 1613 *p++ = ' '; 1614 } 1615 *--p = '\0'; 1616 syslog(LOG_INFO, "control command: %s", tmp); 1617 free(tmp); 1618 1619 optind = 0; 1620 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1) 1621 { 1622 switch (opt) { 1623 case 'g': 1624 do_reconf = 1; 1625 break; 1626 case 'k': 1627 do_release = 1; 1628 break; 1629 case 'n': 1630 do_reboot = 1; 1631 break; 1632 case 'x': 1633 do_exit = 1; 1634 break; 1635 } 1636 } 1637 1638 /* We need at least one interface */ 1639 if (optind == argc) { 1640 syslog(LOG_ERR, "handle_args: no interface"); 1641 return -1; 1642 } 1643 1644 if (do_release || do_exit) { 1645 for (oi = optind; oi < argc; oi++) { 1646 for (ifp = ifaces; ifp; ifp = ifp->next) 1647 if (strcmp(ifp->name, argv[oi]) == 0) 1648 break; 1649 if (!ifp) 1650 continue; 1651 if (do_release) 1652 ifp->state->options->options |= DHCPCD_RELEASE; 1653 if (ifp->state->options->options & DHCPCD_RELEASE && 1654 ifp->carrier != LINK_DOWN) 1655 send_release(ifp); 1656 stop_interface(ifp); 1657 } 1658 return 0; 1659 } 1660 1661 reconf_reboot(do_reboot, argc, argv, optind); 1662 return 0; 1663} 1664 1665void 1666open_sockets(struct interface *iface) 1667{ 1668 if (iface->raw_fd == -1) { 1669 if (open_socket(iface, ETHERTYPE_IP) == -1) 1670 syslog(LOG_ERR, "%s: open_socket: %m", iface->name); 1671 else 1672 add_event(iface->raw_fd, handle_dhcp_packet, iface); 1673 } 1674 if (iface->udp_fd == -1 && 1675 iface->addr.s_addr != 0 && 1676 iface->state->new != NULL && 1677 (iface->state->new->cookie == htonl(MAGIC_COOKIE) || 1678 iface->state->options->options & DHCPCD_INFORM)) 1679 { 1680 if (open_udp_socket(iface) == -1 && errno != EADDRINUSE) 1681 syslog(LOG_ERR, "%s: open_udp_socket: %m", iface->name); 1682 } 1683} 1684 1685void 1686close_sockets(struct interface *iface) 1687{ 1688 if (iface->arp_fd != -1) { 1689 delete_event(iface->arp_fd); 1690 close(iface->arp_fd); 1691 iface->arp_fd = -1; 1692 } 1693 if (iface->raw_fd != -1) { 1694 delete_event(iface->raw_fd); 1695 close(iface->raw_fd); 1696 iface->raw_fd = -1; 1697 } 1698 if (iface->udp_fd != -1) { 1699 /* we don't listen to events on the udp */ 1700 close(iface->udp_fd); 1701 iface->udp_fd = -1; 1702 } 1703} 1704 1705int 1706main(int argc, char **argv) 1707{ 1708 struct interface *iface; 1709 int opt, oi = 0, signal_fd, sig = 0, i, control_fd; 1710 size_t len; 1711 pid_t pid; 1712 struct timespec ts; 1713 1714 closefrom(3); 1715 openlog(PACKAGE, LOG_PERROR | LOG_PID, LOG_DAEMON); 1716 setlogmask(LOG_UPTO(LOG_INFO)); 1717 1718 /* Test for --help and --version */ 1719 if (argc > 1) { 1720 if (strcmp(argv[1], "--help") == 0) { 1721 usage(); 1722 exit(EXIT_SUCCESS); 1723 } else if (strcmp(argv[1], "--version") == 0) { 1724 printf(""PACKAGE" "VERSION"\n%s\n", copyright); 1725 exit(EXIT_SUCCESS); 1726 } 1727 } 1728 1729 i = 0; 1730 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1) 1731 { 1732 switch (opt) { 1733 case 'f': 1734 cffile = optarg; 1735 break; 1736 case 'g': 1737 sig = SIGUSR1; 1738 break; 1739 case 'k': 1740 sig = SIGHUP; 1741 break; 1742 case 'n': 1743 sig = SIGALRM; 1744 break; 1745 case 'x': 1746 sig = SIGTERM; 1747 break; 1748 case 'T': 1749 i = 1; 1750 break; 1751 case 'U': 1752 i = 2; 1753 break; 1754 case 'V': 1755 print_options(); 1756 exit(EXIT_SUCCESS); 1757 case '?': 1758 usage(); 1759 exit(EXIT_FAILURE); 1760 } 1761 } 1762 1763 margv = argv; 1764 margc = argc; 1765 if_options = read_config(cffile, NULL, NULL, NULL); 1766 opt = add_options(if_options, argc, argv); 1767 if (opt != 1) { 1768 if (opt == 0) 1769 usage(); 1770 exit(EXIT_FAILURE); 1771 } 1772 options = if_options->options; 1773 if (i != 0) { 1774 if (i == 1) 1775 options |= DHCPCD_TEST; 1776 else 1777 options |= DHCPCD_DUMPLEASE; 1778 options |= DHCPCD_PERSISTENT; 1779 options &= ~DHCPCD_DAEMONISE; 1780 } 1781 1782#ifdef THERE_IS_NO_FORK 1783 options &= ~DHCPCD_DAEMONISE; 1784#endif 1785 1786 if (options & DHCPCD_DEBUG) 1787 setlogmask(LOG_UPTO(LOG_DEBUG)); 1788 if (options & DHCPCD_QUIET) 1789 close(STDERR_FILENO); 1790 1791 if (!(options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) { 1792 /* If we have any other args, we should run as a single dhcpcd 1793 * instance for that interface. */ 1794 len = strlen(PIDFILE) + IF_NAMESIZE + 2; 1795 pidfile = xmalloc(len); 1796 if (optind == argc - 1) 1797 snprintf(pidfile, len, PIDFILE, "-", argv[optind]); 1798 else { 1799 snprintf(pidfile, len, PIDFILE, "", ""); 1800 options |= DHCPCD_MASTER; 1801 } 1802 } 1803 1804 if (chdir("/") == -1) 1805 syslog(LOG_ERR, "chdir `/': %m"); 1806 atexit(cleanup); 1807 1808 if (options & DHCPCD_DUMPLEASE) { 1809 if (optind != argc - 1) { 1810 syslog(LOG_ERR, "dumplease requires an interface"); 1811 exit(EXIT_FAILURE); 1812 } 1813 ifaces = iface = xzalloc(sizeof(*iface)); 1814 strlcpy(iface->name, argv[optind], sizeof(iface->name)); 1815 snprintf(iface->leasefile, sizeof(iface->leasefile), 1816 LEASEFILE, iface->name); 1817 iface->state = xzalloc(sizeof(*iface->state)); 1818 iface->state->options = xzalloc(sizeof(*iface->state->options)); 1819 strlcpy(iface->state->options->script, if_options->script, 1820 sizeof(iface->state->options->script)); 1821 iface->state->new = read_lease(iface); 1822 if (iface->state->new == NULL && errno == ENOENT) { 1823 strlcpy(iface->leasefile, argv[optind], 1824 sizeof(iface->leasefile)); 1825 iface->state->new = read_lease(iface); 1826 } 1827 if (iface->state->new == NULL) { 1828 if (errno == ENOENT) 1829 syslog(LOG_ERR, "%s: no lease to dump", 1830 iface->name); 1831 exit(EXIT_FAILURE); 1832 } 1833 iface->state->reason = "DUMP"; 1834 run_script(iface); 1835 exit(EXIT_SUCCESS); 1836 } 1837 1838 if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) { 1839 control_fd = open_control(); 1840 if (control_fd != -1) { 1841 syslog(LOG_INFO, 1842 "sending commands to master dhcpcd process"); 1843 i = send_control(argc, argv); 1844 if (i > 0) { 1845 syslog(LOG_DEBUG, "send OK"); 1846 exit(EXIT_SUCCESS); 1847 } else { 1848 syslog(LOG_ERR, "failed to send commands"); 1849 exit(EXIT_FAILURE); 1850 } 1851 } else { 1852 if (errno != ENOENT) 1853 syslog(LOG_ERR, "open_control: %m"); 1854 } 1855 } 1856 1857 if (geteuid()) 1858 syslog(LOG_WARNING, 1859 PACKAGE " will not work correctly unless run as root"); 1860 1861 if (sig != 0) { 1862 pid = read_pid(); 1863 if (pid != 0) 1864 syslog(LOG_INFO, "sending signal %d to pid %d", 1865 sig, pid); 1866 if (pid == 0 || kill(pid, sig) != 0) { 1867 if (sig != SIGALRM) 1868 syslog(LOG_ERR, ""PACKAGE" not running"); 1869 if (pid != 0 && errno != ESRCH) { 1870 syslog(LOG_ERR, "kill: %m"); 1871 exit(EXIT_FAILURE); 1872 } 1873 unlink(pidfile); 1874 if (sig != SIGALRM) 1875 exit(EXIT_FAILURE); 1876 } else { 1877 if (sig == SIGALRM) 1878 exit(EXIT_SUCCESS); 1879 /* Spin until it exits */ 1880 syslog(LOG_INFO, "waiting for pid %d to exit", pid); 1881 ts.tv_sec = 0; 1882 ts.tv_nsec = 100000000; /* 10th of a second */ 1883 for(i = 0; i < 100; i++) { 1884 nanosleep(&ts, NULL); 1885 if (read_pid() == 0) 1886 exit(EXIT_SUCCESS); 1887 } 1888 syslog(LOG_ERR, "pid %d failed to exit", pid); 1889 exit(EXIT_FAILURE); 1890 } 1891 } 1892 1893 if (!(options & DHCPCD_TEST)) { 1894 if ((pid = read_pid()) > 0 && 1895 kill(pid, 0) == 0) 1896 { 1897 syslog(LOG_ERR, ""PACKAGE 1898 " already running on pid %d (%s)", 1899 pid, pidfile); 1900 exit(EXIT_FAILURE); 1901 } 1902 1903 /* Ensure we have the needed directories */ 1904 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST) { 1905 syslog(LOG_ERR, "mkdir `%s': %m", RUNDIR); 1906 exit(EXIT_FAILURE); 1907 } 1908 if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST) { 1909 syslog(LOG_ERR, "mkdir `%s': %m", DBDIR); 1910 exit(EXIT_FAILURE); 1911 } 1912 1913 pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664); 1914 if (pidfd == -1) { 1915 syslog(LOG_ERR, "open `%s': %m", pidfile); 1916 exit(EXIT_FAILURE); 1917 } 1918 /* Lock the file so that only one instance of dhcpcd runs 1919 * on an interface */ 1920 if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) { 1921 syslog(LOG_ERR, "flock `%s': %m", pidfile); 1922 exit(EXIT_FAILURE); 1923 } 1924 if (set_cloexec(pidfd) == -1) 1925 exit(EXIT_FAILURE); 1926 writepid(pidfd, getpid()); 1927 } 1928 1929 syslog(LOG_INFO, "version " VERSION " starting"); 1930 1931 if ((signal_fd = signal_init()) == -1) 1932 exit(EXIT_FAILURE); 1933 if (signal_setup() == -1) 1934 exit(EXIT_FAILURE); 1935 add_event(signal_fd, handle_signal, NULL); 1936 1937 if (options & DHCPCD_MASTER) { 1938 if (start_control() == -1) { 1939 syslog(LOG_ERR, "start_control: %m"); 1940 exit(EXIT_FAILURE); 1941 } 1942 } 1943 1944 if (init_sockets() == -1) { 1945 syslog(LOG_ERR, "init_socket: %m"); 1946 exit(EXIT_FAILURE); 1947 } 1948 if (if_options->options & DHCPCD_LINK) { 1949 linkfd = open_link_socket(); 1950 if (linkfd == -1) 1951 syslog(LOG_ERR, "open_link_socket: %m"); 1952 else 1953 add_event(linkfd, handle_link, NULL); 1954 } 1955 1956 ifc = argc - optind; 1957 ifv = argv + optind; 1958 1959 /* When running dhcpcd against a single interface, we need to retain 1960 * the old behaviour of waiting for an IP address */ 1961 if (ifc == 1) 1962 options |= DHCPCD_WAITIP; 1963 1964 ifaces = discover_interfaces(ifc, ifv); 1965 for (i = 0; i < ifc; i++) { 1966 for (iface = ifaces; iface; iface = iface->next) 1967 if (strcmp(iface->name, ifv[i]) == 0) 1968 break; 1969 if (!iface) 1970 syslog(LOG_ERR, "%s: interface not found or invalid", 1971 ifv[i]); 1972 } 1973 if (!ifaces) { 1974 if (ifc == 0) 1975 syslog(LOG_ERR, "no valid interfaces found"); 1976 else 1977 exit(EXIT_FAILURE); 1978 if (!(options & DHCPCD_LINK)) { 1979 syslog(LOG_ERR, 1980 "aborting as link detection is disabled"); 1981 exit(EXIT_FAILURE); 1982 } 1983 } 1984 1985 if (options & DHCPCD_BACKGROUND) 1986 daemonise(); 1987 1988 opt = 0; 1989 for (iface = ifaces; iface; iface = iface->next) { 1990 init_state(iface, argc, argv); 1991 if (iface->carrier != LINK_DOWN) 1992 opt = 1; 1993 } 1994 1995 if (!(options & DHCPCD_BACKGROUND)) { 1996 /* If we don't have a carrier, we may have to wait for a second 1997 * before one becomes available if we brought an interface up. */ 1998 if (opt == 0 && 1999 options & DHCPCD_LINK && 2000 options & DHCPCD_WAITUP && 2001 !(options & DHCPCD_WAITIP)) 2002 { 2003 ts.tv_sec = 1; 2004 ts.tv_nsec = 0; 2005 nanosleep(&ts, NULL); 2006 for (iface = ifaces; iface; iface = iface->next) { 2007 handle_carrier(iface->name); 2008 if (iface->carrier != LINK_DOWN) { 2009 opt = 1; 2010 break; 2011 } 2012 } 2013 } 2014 if (opt == 0 && 2015 options & DHCPCD_LINK && 2016 !(options & DHCPCD_WAITIP)) 2017 { 2018 syslog(LOG_WARNING, "no interfaces have a carrier"); 2019 daemonise(); 2020 } else if (if_options->timeout > 0) { 2021 if (options & DHCPCD_IPV4LL) 2022 options |= DHCPCD_TIMEOUT_IPV4LL; 2023 add_timeout_sec(if_options->timeout, 2024 handle_exit_timeout, NULL); 2025 } 2026 } 2027 free_options(if_options); 2028 if_options = NULL; 2029 2030 sort_interfaces(); 2031 for (iface = ifaces; iface; iface = iface->next) 2032 add_timeout_sec(0, start_interface, iface); 2033 2034 start_eloop(); 2035 exit(EXIT_SUCCESS); 2036} 2037