1 /* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2006-2018 Roy Marples <roy@marples.name> 4 * All rights reserved 5 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/socket.h> 29 #include <sys/types.h> 30 31 #include <arpa/inet.h> 32 #include <net/if.h> 33 #include <net/route.h> 34 #include <netinet/if_ether.h> 35 #include <netinet/in.h> 36 37 #include <assert.h> 38 #include <ctype.h> 39 #include <errno.h> 40 #include <stdbool.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "config.h" 46 #include "arp.h" 47 #include "common.h" 48 #include "dhcpcd.h" 49 #include "dhcp.h" 50 #include "if.h" 51 #include "if-options.h" 52 #include "ipv4.h" 53 #include "ipv4ll.h" 54 #include "logerr.h" 55 #include "route.h" 56 #include "script.h" 57 #include "sa.h" 58 59 #define IPV4_LOOPBACK_ROUTE 60 #if defined(__linux__) || defined(__sun) || (defined(BSD) && defined(RTF_LOCAL)) 61 /* Linux has had loopback routes in the local table since 2.2 62 * Solaris does not seem to support loopback routes. */ 63 #undef IPV4_LOOPBACK_ROUTE 64 #endif 65 66 uint8_t 67 inet_ntocidr(struct in_addr address) 68 { 69 uint8_t cidr = 0; 70 uint32_t mask = htonl(address.s_addr); 71 72 while (mask) { 73 cidr++; 74 mask <<= 1; 75 } 76 return cidr; 77 } 78 79 int 80 inet_cidrtoaddr(int cidr, struct in_addr *addr) 81 { 82 int ocets; 83 84 if (cidr < 1 || cidr > 32) { 85 errno = EINVAL; 86 return -1; 87 } 88 ocets = (cidr + 7) / NBBY; 89 90 addr->s_addr = 0; 91 if (ocets > 0) { 92 memset(&addr->s_addr, 255, (size_t)ocets - 1); 93 memset((unsigned char *)&addr->s_addr + (ocets - 1), 94 (256 - (1 << (32 - cidr) % NBBY)), 1); 95 } 96 97 return 0; 98 } 99 100 uint32_t 101 ipv4_getnetmask(uint32_t addr) 102 { 103 uint32_t dst; 104 105 if (addr == 0) 106 return 0; 107 108 dst = htonl(addr); 109 if (IN_CLASSA(dst)) 110 return ntohl(IN_CLASSA_NET); 111 if (IN_CLASSB(dst)) 112 return ntohl(IN_CLASSB_NET); 113 if (IN_CLASSC(dst)) 114 return ntohl(IN_CLASSC_NET); 115 116 return 0; 117 } 118 119 struct ipv4_addr * 120 ipv4_iffindaddr(struct interface *ifp, 121 const struct in_addr *addr, const struct in_addr *mask) 122 { 123 struct ipv4_state *state; 124 struct ipv4_addr *ap; 125 126 state = IPV4_STATE(ifp); 127 if (state) { 128 TAILQ_FOREACH(ap, &state->addrs, next) { 129 if ((addr == NULL || ap->addr.s_addr == addr->s_addr) && 130 (mask == NULL || ap->mask.s_addr == mask->s_addr)) 131 return ap; 132 } 133 } 134 return NULL; 135 } 136 137 struct ipv4_addr * 138 ipv4_iffindlladdr(struct interface *ifp) 139 { 140 struct ipv4_state *state; 141 struct ipv4_addr *ap; 142 143 state = IPV4_STATE(ifp); 144 if (state) { 145 TAILQ_FOREACH(ap, &state->addrs, next) { 146 if (IN_LINKLOCAL(htonl(ap->addr.s_addr))) 147 return ap; 148 } 149 } 150 return NULL; 151 } 152 153 static struct ipv4_addr * 154 ipv4_iffindmaskaddr(struct interface *ifp, const struct in_addr *addr) 155 { 156 struct ipv4_state *state; 157 struct ipv4_addr *ap; 158 159 state = IPV4_STATE(ifp); 160 if (state) { 161 TAILQ_FOREACH (ap, &state->addrs, next) { 162 if ((ap->addr.s_addr & ap->mask.s_addr) == 163 (addr->s_addr & ap->mask.s_addr)) 164 return ap; 165 } 166 } 167 return NULL; 168 } 169 170 struct ipv4_addr * 171 ipv4_findaddr(struct dhcpcd_ctx *ctx, const struct in_addr *addr) 172 { 173 struct interface *ifp; 174 struct ipv4_addr *ap; 175 176 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 177 ap = ipv4_iffindaddr(ifp, addr, NULL); 178 if (ap) 179 return ap; 180 } 181 return NULL; 182 } 183 184 struct ipv4_addr * 185 ipv4_findmaskaddr(struct dhcpcd_ctx *ctx, const struct in_addr *addr) 186 { 187 struct interface *ifp; 188 struct ipv4_addr *ap; 189 190 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 191 ap = ipv4_iffindmaskaddr(ifp, addr); 192 if (ap) 193 return ap; 194 } 195 return NULL; 196 } 197 198 int 199 ipv4_hasaddr(const struct interface *ifp) 200 { 201 const struct dhcp_state *dstate; 202 203 if (IPV4LL_STATE_RUNNING(ifp)) 204 return 1; 205 206 dstate = D_CSTATE(ifp); 207 return (dstate && 208 dstate->added == STATE_ADDED && 209 dstate->addr != NULL); 210 } 211 212 /* Interface comparer for working out ordering. */ 213 int 214 ipv4_ifcmp(const struct interface *si, const struct interface *ti) 215 { 216 const struct dhcp_state *sis, *tis; 217 218 sis = D_CSTATE(si); 219 tis = D_CSTATE(ti); 220 if (sis && !tis) 221 return -1; 222 if (!sis && tis) 223 return 1; 224 if (!sis && !tis) 225 return 0; 226 /* If one has a lease and the other not, it takes precedence. */ 227 if (sis->new && !tis->new) 228 return -1; 229 if (!sis->new && tis->new) 230 return 1; 231 /* Always prefer proper leases */ 232 if (!(sis->added & STATE_FAKE) && (tis->added & STATE_FAKE)) 233 return -1; 234 if ((sis->added & STATE_FAKE) && !(tis->added & STATE_FAKE)) 235 return 1; 236 /* If we are either, they neither have a lease, or they both have. 237 * We need to check for IPv4LL and make it non-preferred. */ 238 if (sis->new && tis->new) { 239 if (IS_DHCP(sis->new) && !IS_DHCP(tis->new)) 240 return -1; 241 if (!IS_DHCP(sis->new) && IS_DHCP(tis->new)) 242 return 1; 243 } 244 return 0; 245 } 246 247 static int 248 inet_dhcproutes(struct rt_head *routes, struct interface *ifp) 249 { 250 const struct dhcp_state *state; 251 struct rt_head nroutes; 252 struct rt *rt, *r = NULL; 253 struct in_addr in; 254 uint16_t mtu; 255 int n; 256 257 state = D_CSTATE(ifp); 258 if (state == NULL || state->state != DHS_BOUND || !state->added) 259 return 0; 260 261 /* An address does have to exist. */ 262 assert(state->addr); 263 264 TAILQ_INIT(&nroutes); 265 266 /* First, add a subnet route. */ 267 if (!(ifp->flags & IFF_POINTOPOINT) && 268 #ifndef BSD 269 /* BSD adds a route in this instance */ 270 state->addr->mask.s_addr != INADDR_BROADCAST && 271 #endif 272 state->addr->mask.s_addr != INADDR_ANY) 273 { 274 if ((rt = rt_new(ifp)) == NULL) 275 return -1; 276 rt->rt_dflags |= RTDF_IFA_ROUTE; 277 in.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr; 278 sa_in_init(&rt->rt_dest, &in); 279 in.s_addr = state->addr->mask.s_addr; 280 sa_in_init(&rt->rt_netmask, &in); 281 //in.s_addr = INADDR_ANY; 282 //sa_in_init(&rt->rt_gateway, &in); 283 rt->rt_gateway.sa_family = AF_UNSPEC; 284 TAILQ_INSERT_HEAD(&nroutes, rt, rt_next); 285 } 286 287 /* If any set routes, grab them, otherwise DHCP routes. */ 288 if (TAILQ_FIRST(&ifp->options->routes)) { 289 TAILQ_FOREACH(r, &ifp->options->routes, rt_next) { 290 if (sa_is_unspecified(&r->rt_gateway)) 291 break; 292 if ((rt = rt_new0(ifp->ctx)) == NULL) 293 return -1; 294 memcpy(rt, r, sizeof(*rt)); 295 rt_setif(rt, ifp); 296 rt->rt_dflags = RTDF_STATIC; 297 TAILQ_INSERT_TAIL(&nroutes, rt, rt_next); 298 } 299 } else { 300 if (dhcp_get_routes(&nroutes, ifp) == -1) 301 return -1; 302 } 303 304 /* If configured, Install a gateway to the desintion 305 * for P2P interfaces. */ 306 if (ifp->flags & IFF_POINTOPOINT && 307 has_option_mask(ifp->options->dstmask, DHO_ROUTER)) 308 { 309 if ((rt = rt_new(ifp)) == NULL) 310 return -1; 311 in.s_addr = INADDR_ANY; 312 sa_in_init(&rt->rt_dest, &in); 313 sa_in_init(&rt->rt_netmask, &in); 314 sa_in_init(&rt->rt_gateway, &state->addr->brd); 315 sa_in_init(&rt->rt_ifa, &state->addr->addr); 316 TAILQ_INSERT_HEAD(routes, rt, rt_next); 317 } 318 319 /* Copy our address as the source address and set mtu */ 320 mtu = dhcp_get_mtu(ifp); 321 n = 0; 322 TAILQ_FOREACH(rt, &nroutes, rt_next) { 323 rt->rt_mtu = mtu; 324 if (!(rt->rt_dflags & RTDF_STATIC)) 325 rt->rt_dflags |= RTDF_DHCP; 326 sa_in_init(&rt->rt_ifa, &state->addr->addr); 327 n++; 328 } 329 TAILQ_CONCAT(routes, &nroutes, rt_next); 330 331 return n; 332 } 333 334 /* We should check to ensure the routers are on the same subnet 335 * OR supply a host route. If not, warn and add a host route. */ 336 static int 337 inet_routerhostroute(struct rt_head *routes, struct interface *ifp) 338 { 339 struct rt *rt, *rth; 340 struct sockaddr_in *dest, *netmask, *gateway; 341 const char *cp, *cp2, *cp3, *cplim; 342 struct if_options *ifo; 343 const struct dhcp_state *state; 344 struct in_addr in; 345 346 /* Don't add a host route for these interfaces. */ 347 if (ifp->flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) 348 return 0; 349 350 TAILQ_FOREACH(rt, routes, rt_next) { 351 if (rt->rt_dest.sa_family != AF_INET) 352 continue; 353 if (!sa_is_unspecified(&rt->rt_dest) || 354 sa_is_unspecified(&rt->rt_gateway)) 355 continue; 356 gateway = satosin(&rt->rt_gateway); 357 /* Scan for a route to match */ 358 TAILQ_FOREACH(rth, routes, rt_next) { 359 if (rth == rt) 360 break; 361 /* match host */ 362 if (sa_cmp(&rth->rt_dest, &rt->rt_gateway) == 0) 363 break; 364 /* match subnet */ 365 cp = (const char *)&gateway->sin_addr.s_addr; 366 dest = satosin(&rth->rt_dest); 367 cp2 = (const char *)&dest->sin_addr.s_addr; 368 netmask = satosin(&rth->rt_netmask); 369 cp3 = (const char *)&netmask->sin_addr.s_addr; 370 cplim = cp3 + sizeof(netmask->sin_addr.s_addr); 371 while (cp3 < cplim) { 372 if ((*cp++ ^ *cp2++) & *cp3++) 373 break; 374 } 375 if (cp3 == cplim) 376 break; 377 } 378 if (rth != rt) 379 continue; 380 if ((state = D_CSTATE(ifp)) == NULL) 381 continue; 382 ifo = ifp->options; 383 if (ifp->flags & IFF_NOARP) { 384 if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) && 385 !(state->added & STATE_FAKE)) 386 { 387 char buf[INET_MAX_ADDRSTRLEN]; 388 389 ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED; 390 logwarnx("%s: forcing router %s through " 391 "interface", 392 ifp->name, 393 sa_addrtop(&rt->rt_gateway, 394 buf, sizeof(buf))); 395 } 396 gateway->sin_addr.s_addr = INADDR_ANY; 397 continue; 398 } 399 if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) && 400 !(state->added & STATE_FAKE)) 401 { 402 char buf[INET_MAX_ADDRSTRLEN]; 403 404 ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED; 405 logwarnx("%s: router %s requires a host route", 406 ifp->name, 407 sa_addrtop(&rt->rt_gateway, buf, sizeof(buf))); 408 } 409 if ((rth = rt_new(ifp)) == NULL) 410 return -1; 411 rth->rt_flags |= RTF_HOST; 412 sa_in_init(&rth->rt_dest, &gateway->sin_addr); 413 in.s_addr = INADDR_BROADCAST; 414 sa_in_init(&rth->rt_netmask, &in); 415 in.s_addr = INADDR_ANY; 416 sa_in_init(&rth->rt_gateway, &in); 417 rth->rt_mtu = dhcp_get_mtu(ifp); 418 sa_in_init(&rth->rt_ifa, &state->addr->addr); 419 TAILQ_INSERT_BEFORE(rt, rth, rt_next); 420 } 421 return 0; 422 } 423 424 bool 425 inet_getroutes(struct dhcpcd_ctx *ctx, struct rt_head *routes) 426 { 427 struct interface *ifp; 428 struct rt def; 429 bool have_default; 430 431 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 432 if (!ifp->active) 433 continue; 434 if (inet_dhcproutes(routes, ifp) == -1) 435 return false; 436 if (ipv4ll_subnetroute(routes, ifp) == -1) 437 return false; 438 if (inet_routerhostroute(routes, ifp) == -1) 439 return false; 440 } 441 442 /* If there is no default route, see if we can use an IPv4LL one. */ 443 memset(&def, 0, sizeof(def)); 444 def.rt_dest.sa_family = AF_INET; 445 have_default = (rt_find(routes, &def) != NULL); 446 if (!have_default) { 447 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 448 if (ifp->active && 449 ipv4ll_defaultroute(routes, ifp) == 1) 450 break; 451 } 452 } 453 454 return true; 455 } 456 457 int 458 ipv4_deladdr(struct ipv4_addr *addr, int keeparp) 459 { 460 int r; 461 struct ipv4_state *state; 462 struct ipv4_addr *ap; 463 #ifdef ARP 464 struct arp_state *astate; 465 #else 466 UNUSED(keeparp); 467 #endif 468 469 logdebugx("%s: deleting IP address %s", 470 addr->iface->name, addr->saddr); 471 472 r = if_address(RTM_DELADDR, addr); 473 if (r == -1 && 474 errno != EADDRNOTAVAIL && errno != ESRCH && 475 errno != ENXIO && errno != ENODEV) 476 logerr("%s: %s", addr->iface->name, __func__); 477 478 #ifdef ARP 479 if (!keeparp && (astate = arp_find(addr->iface, &addr->addr)) != NULL) 480 arp_free(astate); 481 #endif 482 483 state = IPV4_STATE(addr->iface); 484 TAILQ_FOREACH(ap, &state->addrs, next) { 485 if (IPV4_MASK_EQ(ap, addr)) { 486 struct dhcp_state *dstate; 487 488 dstate = D_STATE(ap->iface); 489 TAILQ_REMOVE(&state->addrs, ap, next); 490 free(ap); 491 492 if (dstate && dstate->addr == ap) { 493 dstate->added = 0; 494 dstate->addr = NULL; 495 } 496 break; 497 } 498 } 499 500 return r; 501 } 502 503 static int 504 delete_address(struct interface *ifp) 505 { 506 int r; 507 struct if_options *ifo; 508 struct dhcp_state *state; 509 510 state = D_STATE(ifp); 511 ifo = ifp->options; 512 /* The lease could have been added, but the address deleted 513 * by a 3rd party. */ 514 if (state->addr == NULL || 515 ifo->options & DHCPCD_INFORM || 516 (ifo->options & DHCPCD_STATIC && ifo->req_addr.s_addr == 0)) 517 return 0; 518 r = ipv4_deladdr(state->addr, 0); 519 return r; 520 } 521 522 struct ipv4_state * 523 ipv4_getstate(struct interface *ifp) 524 { 525 struct ipv4_state *state; 526 527 state = IPV4_STATE(ifp); 528 if (state == NULL) { 529 ifp->if_data[IF_DATA_IPV4] = malloc(sizeof(*state)); 530 state = IPV4_STATE(ifp); 531 if (state == NULL) { 532 logerr(__func__); 533 return NULL; 534 } 535 TAILQ_INIT(&state->addrs); 536 state->buffer_size = state->buffer_len = state->buffer_pos = 0; 537 state->buffer = NULL; 538 } 539 return state; 540 } 541 542 #ifdef ALIAS_ADDR 543 /* Find the next logical aliase address we can use. */ 544 static int 545 ipv4_aliasaddr(struct ipv4_addr *ia, struct ipv4_addr **repl) 546 { 547 struct ipv4_state *state; 548 struct ipv4_addr *iap; 549 unsigned int lun; 550 char alias[IF_NAMESIZE]; 551 552 if (ia->alias[0] != '\0') 553 return 0; 554 555 lun = 0; 556 state = IPV4_STATE(ia->iface); 557 find_lun: 558 if (lun == 0) 559 strlcpy(alias, ia->iface->name, sizeof(alias)); 560 else 561 snprintf(alias, sizeof(alias), "%s:%u", ia->iface->name, lun); 562 TAILQ_FOREACH(iap, &state->addrs, next) { 563 if (iap->alias[0] != '\0' && iap->addr.s_addr == INADDR_ANY) { 564 /* No address assigned? Lets use it. */ 565 strlcpy(ia->alias, iap->alias, sizeof(ia->alias)); 566 if (repl) 567 *repl = iap; 568 return 1; 569 } 570 if (strcmp(iap->alias, alias) == 0) 571 break; 572 } 573 574 if (iap != NULL) { 575 if (lun == UINT_MAX) { 576 errno = ERANGE; 577 return -1; 578 } 579 lun++; 580 goto find_lun; 581 } 582 583 strlcpy(ia->alias, alias, sizeof(ia->alias)); 584 return 0; 585 } 586 #endif 587 588 struct ipv4_addr * 589 ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, 590 const struct in_addr *mask, const struct in_addr *bcast) 591 { 592 struct ipv4_state *state; 593 struct ipv4_addr *ia; 594 bool is_new = false; 595 #ifdef ALIAS_ADDR 596 int replaced, blank; 597 struct ipv4_addr *replaced_ia; 598 #endif 599 600 if ((state = ipv4_getstate(ifp)) == NULL) { 601 logerr(__func__); 602 return NULL; 603 } 604 if (ifp->options->options & DHCPCD_NOALIAS) { 605 struct ipv4_addr *ian; 606 607 TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ian) { 608 if (ia->addr.s_addr != addr->s_addr) 609 ipv4_deladdr(ia, 0); 610 } 611 } 612 613 ia = ipv4_iffindaddr(ifp, addr, NULL); 614 if (ia == NULL) { 615 ia = malloc(sizeof(*ia)); 616 if (ia == NULL) { 617 logerr(__func__); 618 return NULL; 619 } 620 ia->iface = ifp; 621 ia->addr = *addr; 622 #ifdef IN_IFF_TENTATIVE 623 ia->addr_flags = IN_IFF_TENTATIVE; 624 #endif 625 is_new = true; 626 } 627 628 ia->mask = *mask; 629 ia->brd = *bcast; 630 snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", 631 inet_ntoa(*addr), inet_ntocidr(*mask)); 632 633 #ifdef ALIAS_ADDR 634 blank = (ia->alias[0] == '\0'); 635 if ((replaced = ipv4_aliasaddr(ia, &replaced_ia)) == -1) { 636 logerr("%s: ipv4_aliasaddr", ifp->name); 637 free(ia); 638 return NULL; 639 } 640 if (blank) 641 logdebugx("%s: aliased %s", ia->alias, ia->saddr); 642 #endif 643 644 logdebugx("%s: adding IP address %s broadcast %s", 645 ifp->name, ia->saddr, inet_ntoa(*bcast)); 646 if (if_address(RTM_NEWADDR, ia) == -1) { 647 if (errno != EEXIST) 648 logerr("%s: if_addaddress", 649 __func__); 650 free(ia); 651 return NULL; 652 } 653 654 #ifdef ALIAS_ADDR 655 if (replaced) { 656 TAILQ_REMOVE(&state->addrs, replaced_ia, next); 657 free(replaced_ia); 658 } 659 #endif 660 661 if (is_new) 662 TAILQ_INSERT_TAIL(&state->addrs, ia, next); 663 return ia; 664 } 665 666 static int 667 ipv4_daddaddr(struct interface *ifp, const struct dhcp_lease *lease) 668 { 669 struct dhcp_state *state; 670 struct ipv4_addr *ia; 671 672 ia = ipv4_addaddr(ifp, &lease->addr, &lease->mask, &lease->brd); 673 if (ia == NULL) 674 return -1; 675 676 state = D_STATE(ifp); 677 state->added = STATE_ADDED; 678 state->addr = ia; 679 return 0; 680 } 681 682 void 683 ipv4_applyaddr(void *arg) 684 { 685 struct interface *ifp = arg; 686 struct dhcp_state *state = D_STATE(ifp); 687 struct dhcp_lease *lease; 688 struct if_options *ifo = ifp->options; 689 struct ipv4_addr *ia; 690 int r; 691 692 if (state == NULL) 693 return; 694 695 lease = &state->lease; 696 if (state->new == NULL) { 697 if ((ifo->options & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) != 698 (DHCPCD_EXITING | DHCPCD_PERSISTENT)) 699 { 700 if (state->added) { 701 struct in_addr addr; 702 703 addr = lease->addr; 704 delete_address(ifp); 705 rt_build(ifp->ctx, AF_INET); 706 #ifdef ARP 707 /* Announce the preferred address to 708 * kick ARP caches. */ 709 arp_announceaddr(ifp->ctx, &addr); 710 #endif 711 } 712 script_runreason(ifp, state->reason); 713 } else 714 rt_build(ifp->ctx, AF_INET); 715 return; 716 } 717 718 /* If the netmask or broadcast is different, re-add the addresss */ 719 ia = ipv4_iffindaddr(ifp, &lease->addr, NULL); 720 if (ia && 721 ia->mask.s_addr == lease->mask.s_addr && 722 ia->brd.s_addr == lease->brd.s_addr) 723 logdebugx("%s: IP address %s already exists", 724 ifp->name, ia->saddr); 725 else { 726 #if __linux__ 727 /* Linux does not change netmask/broadcast address 728 * for re-added addresses, so we need to delete the old one 729 * first. */ 730 if (ia != NULL) 731 ipv4_deladdr(ia, 0); 732 #endif 733 r = ipv4_daddaddr(ifp, lease); 734 if (r == -1 && errno != EEXIST) 735 return; 736 } 737 738 ia = ipv4_iffindaddr(ifp, &lease->addr, NULL); 739 if (ia == NULL) { 740 logerrx("%s: added address vanished", ifp->name); 741 return; 742 } 743 #if defined(ARP) && defined(IN_IFF_NOTUSEABLE) 744 if (ia->addr_flags & IN_IFF_NOTUSEABLE) 745 return; 746 #endif 747 748 /* Delete the old address if different */ 749 if (state->addr && 750 state->addr->addr.s_addr != lease->addr.s_addr && 751 ipv4_iffindaddr(ifp, &lease->addr, NULL)) 752 delete_address(ifp); 753 754 state->addr = ia; 755 state->added = STATE_ADDED; 756 757 /* Find any freshly added routes, such as the subnet route. 758 * We do this because we cannot rely on recieving the kernel 759 * notification right now via our link socket. */ 760 if_initrt(ifp->ctx, AF_INET); 761 rt_build(ifp->ctx, AF_INET); 762 763 #ifdef ARP 764 arp_announceaddr(ifp->ctx, &state->addr->addr); 765 #endif 766 767 if (state->state == DHS_BOUND) { 768 script_runreason(ifp, state->reason); 769 dhcpcd_daemonise(ifp->ctx); 770 } 771 } 772 773 void 774 ipv4_markaddrsstale(struct interface *ifp) 775 { 776 struct ipv4_state *state; 777 struct ipv4_addr *ia; 778 779 state = IPV4_STATE(ifp); 780 if (state == NULL) 781 return; 782 783 TAILQ_FOREACH(ia, &state->addrs, next) { 784 ia->flags |= IPV4_AF_STALE; 785 } 786 } 787 788 void 789 ipv4_deletestaleaddrs(struct interface *ifp) 790 { 791 struct ipv4_state *state; 792 struct ipv4_addr *ia, *ia1; 793 794 state = IPV4_STATE(ifp); 795 if (state == NULL) 796 return; 797 798 TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) { 799 if (!(ia->flags & IPV4_AF_STALE)) 800 continue; 801 ipv4_handleifa(ifp->ctx, RTM_DELADDR, 802 ifp->ctx->ifaces, ifp->name, 803 &ia->addr, &ia->mask, &ia->brd, 0, getpid()); 804 } 805 } 806 807 void 808 ipv4_handleifa(struct dhcpcd_ctx *ctx, 809 int cmd, struct if_head *ifs, const char *ifname, 810 const struct in_addr *addr, const struct in_addr *mask, 811 const struct in_addr *brd, int addrflags, pid_t pid) 812 { 813 struct interface *ifp; 814 struct ipv4_state *state; 815 struct ipv4_addr *ia; 816 bool ia_is_new; 817 818 #if 0 819 char sbrdbuf[INET_ADDRSTRLEN]; 820 const char *sbrd; 821 822 if (brd) 823 sbrd = inet_ntop(AF_INET, brd, sbrdbuf, sizeof(sbrdbuf)); 824 else 825 sbrd = NULL; 826 logdebugx("%s: %s %s/%d %s %d", ifname, 827 cmd == RTM_NEWADDR ? "RTM_NEWADDR" : 828 cmd == RTM_DELADDR ? "RTM_DELADDR" : "???", 829 inet_ntoa(*addr), inet_ntocidr(*mask), sbrd, addrflags); 830 #endif 831 832 if (ifs == NULL) 833 ifs = ctx->ifaces; 834 if (ifs == NULL) { 835 errno = ESRCH; 836 return; 837 } 838 if ((ifp = if_find(ifs, ifname)) == NULL) 839 return; 840 if ((state = ipv4_getstate(ifp)) == NULL) { 841 errno = ENOENT; 842 return; 843 } 844 845 ia = ipv4_iffindaddr(ifp, addr, NULL); 846 switch (cmd) { 847 case RTM_NEWADDR: 848 if (ia == NULL) { 849 if ((ia = malloc(sizeof(*ia))) == NULL) { 850 logerr(__func__); 851 return; 852 } 853 ia->iface = ifp; 854 ia->addr = *addr; 855 ia->mask = *mask; 856 ia->flags = 0; 857 ia_is_new = true; 858 #ifdef ALIAS_ADDR 859 strlcpy(ia->alias, ifname, sizeof(ia->alias)); 860 #endif 861 TAILQ_INSERT_TAIL(&state->addrs, ia, next); 862 } else 863 ia_is_new = false; 864 /* Mask could have changed */ 865 if (ia_is_new || 866 (mask->s_addr != INADDR_ANY && 867 mask->s_addr != ia->mask.s_addr)) 868 { 869 ia->mask = *mask; 870 snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", 871 inet_ntoa(*addr), inet_ntocidr(*mask)); 872 } 873 if (brd != NULL) 874 ia->brd = *brd; 875 else 876 ia->brd.s_addr = INADDR_ANY; 877 ia->addr_flags = addrflags; 878 ia->flags &= ~IPV4_AF_STALE; 879 break; 880 case RTM_DELADDR: 881 if (ia == NULL) 882 return; 883 TAILQ_REMOVE(&state->addrs, ia, next); 884 break; 885 default: 886 return; 887 } 888 889 if (addr->s_addr != INADDR_ANY && addr->s_addr != INADDR_BROADCAST) { 890 #ifdef ARP 891 arp_handleifa(cmd, ia); 892 #endif 893 dhcp_handleifa(cmd, ia, pid); 894 } 895 896 if (cmd == RTM_DELADDR) 897 free(ia); 898 } 899 900 void 901 ipv4_free(struct interface *ifp) 902 { 903 struct ipv4_state *state; 904 struct ipv4_addr *ia; 905 906 if (ifp) { 907 state = IPV4_STATE(ifp); 908 if (state) { 909 while ((ia = TAILQ_FIRST(&state->addrs))) { 910 TAILQ_REMOVE(&state->addrs, ia, next); 911 free(ia); 912 } 913 free(state->buffer); 914 free(state); 915 } 916 } 917 } 918