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