Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * dhcpcd - DHCP client daemon
      3  * SPDX-License-Identifier: BSD-2-Clause
      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/types.h>
     30 #include <sys/socket.h>
     31 
     32 #include <net/if.h>
     33 #include <net/route.h>
     34 #include <netinet/in.h>
     35 #include <netinet/if_ether.h>
     36 
     37 #include <arpa/inet.h>
     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" // IWYU pragma: keep
     48 #include "arp.h"
     49 #include "common.h"
     50 #include "dhcp.h"
     51 #include "dhcpcd.h"
     52 #include "eloop.h"
     53 #include "if-options.h"
     54 #include "if.h"
     55 #include "ipv4.h"
     56 #include "ipv4ll.h"
     57 #include "logerr.h"
     58 #include "route.h"
     59 #include "sa.h"
     60 #include "script.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, const struct in_addr *addr,
    124     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 && dstate->added == STATE_ADDED && dstate->addr != NULL);
    244 }
    245 
    246 /* Interface comparer for working out ordering. */
    247 int
    248 ipv4_ifcmp(const struct interface *si, const struct interface *ti)
    249 {
    250 	const struct dhcp_state *sis, *tis;
    251 
    252 	sis = D_CSTATE(si);
    253 	tis = D_CSTATE(ti);
    254 	if (sis && !tis)
    255 		return -1;
    256 	if (!sis && tis)
    257 		return 1;
    258 	if (!sis && !tis)
    259 		return 0;
    260 	/* If one has a lease and the other not, it takes precedence. */
    261 	if (sis->new && !tis->new)
    262 		return -1;
    263 	if (!sis->new && tis->new)
    264 		return 1;
    265 	/* Always prefer proper leases */
    266 	if (!(sis->added & STATE_FAKE) && (tis->added & STATE_FAKE))
    267 		return -1;
    268 	if ((sis->added & STATE_FAKE) && !(tis->added & STATE_FAKE))
    269 		return 1;
    270 	/* If we are either, they neither have a lease, or they both have.
    271 	 * We need to check for IPv4LL and make it non-preferred. */
    272 	if (sis->new && tis->new) {
    273 		if (IS_DHCP(sis->new) && !IS_DHCP(tis->new))
    274 			return -1;
    275 		if (!IS_DHCP(sis->new) && IS_DHCP(tis->new))
    276 			return 1;
    277 	}
    278 	return 0;
    279 }
    280 
    281 static int
    282 inet_dhcproutes(rb_tree_t *routes, struct interface *ifp, bool *have_default)
    283 {
    284 	const struct dhcp_state *state;
    285 	rb_tree_t nroutes;
    286 	struct rt *rt, *r = NULL;
    287 	struct in_addr in;
    288 	uint16_t mtu;
    289 	int n;
    290 
    291 	state = D_CSTATE(ifp);
    292 	if (state == NULL || !(state->added & STATE_ADDED))
    293 		return 0;
    294 
    295 	/* An address does have to exist. */
    296 	assert(state->addr);
    297 
    298 	rb_tree_init(&nroutes, &rt_compare_proto_ops);
    299 
    300 	/* First, add a subnet route. */
    301 	if (state->addr->mask.s_addr != INADDR_ANY
    302 #ifndef BSD
    303 	    /* BSD adds a route in this instance */
    304 	    && state->addr->mask.s_addr != INADDR_BROADCAST
    305 #endif
    306 	) {
    307 		if ((rt = rt_new(ifp)) == NULL)
    308 			return -1;
    309 		rt->rt_dflags |= RTDF_IFA_ROUTE;
    310 		in.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr;
    311 		sa_in_init(rt->rt_dest, &in);
    312 		in.s_addr = state->addr->mask.s_addr;
    313 		sa_in_init(rt->rt_netmask, &in);
    314 		// in.s_addr = INADDR_ANY;
    315 		// sa_in_init(rt->rt_gateway, &in);
    316 		rt->rt_gateway->sa_family = AF_UNSPEC;
    317 		rt_proto_add(&nroutes, rt);
    318 	}
    319 
    320 	/* If any set routes, grab them, otherwise DHCP routes. */
    321 	if (RB_TREE_MIN(&ifp->options->routes)) {
    322 		RB_TREE_FOREACH(r, &ifp->options->routes)
    323 		{
    324 			if (sa_is_unspecified(r->rt_gateway))
    325 				break;
    326 			if ((rt = rt_new0(ifp->ctx)) == NULL)
    327 				return -1;
    328 			rt_copy(rt, r);
    329 			rt_setif(rt, ifp);
    330 			rt->rt_dflags = RTDF_STATIC;
    331 			rt_proto_add(&nroutes, rt);
    332 		}
    333 	} else {
    334 		if (dhcp_get_routes(&nroutes, ifp) == -1)
    335 			return -1;
    336 	}
    337 
    338 	/* If configured, install a gateway to the desintion
    339 	 * for P2P interfaces. */
    340 	if (ifp->flags & IFF_POINTOPOINT &&
    341 	    dho_policy_has(&ifp->options->dhop_destination, DHO_ROUTER)) {
    342 		if ((rt = rt_new(ifp)) == NULL)
    343 			return -1;
    344 		in.s_addr = INADDR_ANY;
    345 		sa_in_init(rt->rt_dest, &in);
    346 		sa_in_init(rt->rt_netmask, &in);
    347 		sa_in_init(rt->rt_gateway, &state->addr->brd);
    348 		sa_in_init(rt->rt_ifa, &state->addr->addr);
    349 		rt_proto_add(&nroutes, rt);
    350 	}
    351 
    352 	/* Copy our address as the source address and set mtu */
    353 	mtu = dhcp_get_mtu(ifp);
    354 	n = 0;
    355 	while ((rt = RB_TREE_MIN(&nroutes)) != NULL) {
    356 		rb_tree_remove_node(&nroutes, rt);
    357 		rt->rt_mtu = mtu;
    358 		if (!(rt->rt_dflags & RTDF_STATIC))
    359 			rt->rt_dflags |= RTDF_DHCP;
    360 		if (state->added & STATE_FAKE)
    361 			rt->rt_dflags |= RTDF_FAKE;
    362 		sa_in_init(rt->rt_ifa, &state->addr->addr);
    363 		if (rb_tree_insert_node(routes, rt) != rt) {
    364 			rt_free(rt);
    365 			continue;
    366 		}
    367 		if (rt_is_default(rt))
    368 			*have_default = true;
    369 		n = 1;
    370 	}
    371 
    372 	return n;
    373 }
    374 
    375 /* We should check to ensure the routers are on the same subnet
    376  * OR supply a host route. If not, warn and add a host route. */
    377 static int
    378 inet_routerhostroute(rb_tree_t *routes, struct interface *ifp)
    379 {
    380 	struct rt *rt, *rth, *rtp;
    381 	struct sockaddr_in *dest, *netmask, *gateway;
    382 	const char *cp, *cp2, *cp3, *cplim;
    383 	struct if_options *ifo;
    384 	const struct dhcp_state *state;
    385 	struct in_addr in;
    386 	rb_tree_t troutes;
    387 
    388 	/* Don't add a host route for these interfaces. */
    389 	if (ifp->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
    390 		return 0;
    391 
    392 	rb_tree_init(&troutes, &rt_compare_proto_ops);
    393 
    394 	RB_TREE_FOREACH(rt, routes)
    395 	{
    396 		if (rt->rt_dest->sa_family != AF_INET)
    397 			continue;
    398 		if (!sa_is_unspecified(rt->rt_dest) ||
    399 		    sa_is_unspecified(rt->rt_gateway))
    400 			continue;
    401 		gateway = satosin(rt->rt_gateway);
    402 		/* Scan for a route to match */
    403 		RB_TREE_FOREACH(rth, routes)
    404 		{
    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 				char buf[INET_MAX_ADDRSTRLEN];
    434 
    435 				ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED;
    436 				logwarnx("%s: forcing router %s through "
    437 					 "interface",
    438 				    ifp->name,
    439 				    sa_addrtop(rt->rt_gateway, buf,
    440 					sizeof(buf)));
    441 			}
    442 			gateway->sin_addr.s_addr = INADDR_ANY;
    443 			continue;
    444 		}
    445 		if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) &&
    446 		    !(state->added & STATE_FAKE)) {
    447 			char buf[INET_MAX_ADDRSTRLEN];
    448 
    449 			ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED;
    450 			logwarnx("%s: router %s requires a host route",
    451 			    ifp->name,
    452 			    sa_addrtop(rt->rt_gateway, buf, sizeof(buf)));
    453 		}
    454 
    455 		if ((rth = rt_new(ifp)) == NULL)
    456 			return -1;
    457 		rth->rt_flags |= RTF_HOST;
    458 		sa_in_init(rth->rt_dest, &gateway->sin_addr);
    459 		in.s_addr = INADDR_BROADCAST;
    460 		sa_in_init(rth->rt_netmask, &in);
    461 		in.s_addr = INADDR_ANY;
    462 		sa_in_init(rth->rt_gateway, &in);
    463 		rth->rt_mtu = dhcp_get_mtu(ifp);
    464 		if (state->addr != NULL)
    465 			sa_in_init(rth->rt_ifa, &state->addr->addr);
    466 		else
    467 			rth->rt_ifa->sa_family = AF_UNSPEC;
    468 
    469 		/* We need to insert the host route just before the router. */
    470 		while ((rtp = RB_TREE_MAX(routes)) != NULL) {
    471 			rb_tree_remove_node(routes, rtp);
    472 			rt_proto_add(&troutes, rtp);
    473 			if (rtp == rt)
    474 				break;
    475 		}
    476 		rt_proto_add(routes, rth);
    477 		/* troutes is now reversed, so add backwards again. */
    478 		while ((rtp = RB_TREE_MAX(&troutes)) != NULL) {
    479 			rb_tree_remove_node(&troutes, rtp);
    480 			rt_proto_add(routes, rtp);
    481 		}
    482 	}
    483 	return 0;
    484 }
    485 
    486 bool
    487 inet_getroutes(struct dhcpcd_ctx *ctx, rb_tree_t *routes)
    488 {
    489 	struct interface *ifp;
    490 	bool have_default = false;
    491 
    492 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
    493 		if (!ifp->active)
    494 			continue;
    495 		if (inet_dhcproutes(routes, ifp, &have_default) == -1)
    496 			return false;
    497 #ifdef IPV4LL
    498 		if (ipv4ll_subnetroute(routes, ifp) == -1)
    499 			return false;
    500 #endif
    501 		if (inet_routerhostroute(routes, ifp) == -1)
    502 			return false;
    503 	}
    504 
    505 #ifdef IPV4LL
    506 	/* If there is no default route, see if we can use an IPv4LL one. */
    507 	if (have_default)
    508 		return true;
    509 
    510 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
    511 		if (ifp->active && ipv4ll_defaultroute(routes, ifp) == 1)
    512 			break;
    513 	}
    514 #endif
    515 
    516 	return true;
    517 }
    518 
    519 int
    520 ipv4_deladdr(struct ipv4_addr *addr, int keeparp)
    521 {
    522 	int r;
    523 	struct ipv4_state *state;
    524 	struct ipv4_addr *ap;
    525 
    526 	assert(addr != NULL);
    527 	logdebugx("%s: deleting IP address %s", addr->iface->name, addr->saddr);
    528 
    529 	r = if_address(RTM_DELADDR, addr);
    530 	if (r == -1 && errno != EADDRNOTAVAIL && errno != ESRCH &&
    531 	    errno != ENXIO && errno != ENODEV)
    532 		logerr("%s: %s", addr->iface->name, __func__);
    533 
    534 #ifdef ARP
    535 	if (!keeparp)
    536 		arp_freeaddr(addr->iface, &addr->addr);
    537 #else
    538 	UNUSED(keeparp);
    539 #endif
    540 
    541 	state = IPV4_STATE(addr->iface);
    542 	TAILQ_FOREACH(ap, &state->addrs, next) {
    543 		if (IPV4_MASK_EQ(ap, addr)) {
    544 			struct dhcp_state *dstate;
    545 
    546 			dstate = D_STATE(ap->iface);
    547 			TAILQ_REMOVE(&state->addrs, ap, next);
    548 			free(ap);
    549 
    550 			if (dstate && dstate->addr == ap) {
    551 				dstate->added = 0;
    552 				dstate->addr = NULL;
    553 			}
    554 			break;
    555 		}
    556 	}
    557 
    558 	return r;
    559 }
    560 
    561 struct ipv4_state *
    562 ipv4_getstate(struct interface *ifp)
    563 {
    564 	struct ipv4_state *state;
    565 
    566 	state = IPV4_STATE(ifp);
    567 	if (state == NULL) {
    568 		ifp->if_data[IF_DATA_IPV4] = malloc(sizeof(*state));
    569 		state = IPV4_STATE(ifp);
    570 		if (state == NULL) {
    571 			logerr(__func__);
    572 			return NULL;
    573 		}
    574 		TAILQ_INIT(&state->addrs);
    575 	}
    576 	return state;
    577 }
    578 
    579 #ifdef ALIAS_ADDR
    580 /* Find the next logical aliase address we can use. */
    581 static int
    582 ipv4_aliasaddr(struct ipv4_addr *ia, struct ipv4_addr **repl)
    583 {
    584 	struct ipv4_state *state;
    585 	struct ipv4_addr *iap;
    586 	unsigned int lun;
    587 	char alias[IF_NAMESIZE];
    588 
    589 	if (ia->alias[0] != '\0')
    590 		return 0;
    591 
    592 	lun = 0;
    593 	state = IPV4_STATE(ia->iface);
    594 find_lun:
    595 	if (if_makealias(alias, IF_NAMESIZE, ia->iface->name, lun) >=
    596 	    IF_NAMESIZE) {
    597 		errno = ENOMEM;
    598 		return -1;
    599 	}
    600 	TAILQ_FOREACH(iap, &state->addrs, next) {
    601 		if (iap->alias[0] != '\0' && iap->addr.s_addr == INADDR_ANY) {
    602 			/* No address assigned? Lets use it. */
    603 			strlcpy(ia->alias, iap->alias, sizeof(ia->alias));
    604 			if (repl)
    605 				*repl = iap;
    606 			return 1;
    607 		}
    608 		if (strcmp(iap->alias, alias) == 0)
    609 			break;
    610 	}
    611 
    612 	if (iap != NULL) {
    613 		if (lun == UINT_MAX) {
    614 			errno = ERANGE;
    615 			return -1;
    616 		}
    617 		lun++;
    618 		goto find_lun;
    619 	}
    620 
    621 	strlcpy(ia->alias, alias, sizeof(ia->alias));
    622 	return 0;
    623 }
    624 #endif
    625 
    626 struct ipv4_addr *
    627 ipv4_addaddr(struct interface *ifp, const struct in_addr *addr,
    628     const struct in_addr *mask, const struct in_addr *bcast, uint32_t vltime,
    629     uint32_t pltime)
    630 {
    631 	struct ipv4_state *state;
    632 	struct ipv4_addr *ia;
    633 #ifdef ALIAS_ADDR
    634 	int replaced, blank;
    635 	struct ipv4_addr *replaced_ia;
    636 #endif
    637 
    638 	if ((state = ipv4_getstate(ifp)) == NULL) {
    639 		logerr(__func__);
    640 		return NULL;
    641 	}
    642 	if (ifp->options->options & DHCPCD_NOALIAS) {
    643 		struct ipv4_addr *ian;
    644 
    645 		TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ian) {
    646 			if (ia->addr.s_addr != addr->s_addr)
    647 				ipv4_deladdr(ia, 0);
    648 		}
    649 	}
    650 
    651 	ia = ipv4_iffindaddr(ifp, addr, NULL);
    652 	if (ia == NULL) {
    653 		ia = malloc(sizeof(*ia));
    654 		if (ia == NULL) {
    655 			logerr(__func__);
    656 			return NULL;
    657 		}
    658 		ia->iface = ifp;
    659 		ia->addr = *addr;
    660 #ifdef IN_IFF_TENTATIVE
    661 		ia->addr_flags = IN_IFF_TENTATIVE;
    662 #endif
    663 		ia->flags = IPV4_AF_NEW;
    664 	} else
    665 		ia->flags &= ~IPV4_AF_NEW;
    666 
    667 	ia->mask = *mask;
    668 	ia->brd = *bcast;
    669 #ifdef IP_LIFETIME
    670 	if (ifp->options->options & DHCPCD_LASTLEASE_EXTEND) {
    671 		/* We don't want the kernel to expire the address. */
    672 		ia->vltime = ia->pltime = DHCP_INFINITE_LIFETIME;
    673 	} else {
    674 		ia->vltime = vltime;
    675 		ia->pltime = pltime;
    676 	}
    677 #else
    678 	UNUSED(vltime);
    679 	UNUSED(pltime);
    680 #endif
    681 	snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", inet_ntoa(*addr),
    682 	    inet_ntocidr(*mask));
    683 
    684 #ifdef ALIAS_ADDR
    685 	blank = (ia->alias[0] == '\0');
    686 	if ((replaced = ipv4_aliasaddr(ia, &replaced_ia)) == -1) {
    687 		logerr("%s: ipv4_aliasaddr", ifp->name);
    688 		free(ia);
    689 		return NULL;
    690 	}
    691 	if (blank)
    692 		logdebugx("%s: aliased %s", ia->alias, ia->saddr);
    693 #endif
    694 
    695 	logdebugx("%s: adding IP address %s %s %s", ifp->name, ia->saddr,
    696 	    ifp->flags & IFF_POINTOPOINT ? "destination" : "broadcast",
    697 	    inet_ntoa(*bcast));
    698 	if (if_address(RTM_NEWADDR, ia) == -1) {
    699 		if (errno != EEXIST)
    700 			logerr("%s: if_addaddress", __func__);
    701 		if (ia->flags & IPV4_AF_NEW)
    702 			free(ia);
    703 		return NULL;
    704 	}
    705 
    706 #ifdef ALIAS_ADDR
    707 	if (replaced) {
    708 		TAILQ_REMOVE(&state->addrs, replaced_ia, next);
    709 		free(replaced_ia);
    710 	}
    711 #endif
    712 
    713 	if (ia->flags & IPV4_AF_NEW) {
    714 		TAILQ_INSERT_TAIL(&state->addrs, ia, next);
    715 #if defined(ARP) && !defined(KERNEL_RFC5227)
    716 		arp_ifannounceaddr(ifp, &ia->addr);
    717 #endif
    718 	}
    719 
    720 	return ia;
    721 }
    722 
    723 static int
    724 ipv4_daddaddr(struct interface *ifp, const struct dhcp_lease *lease)
    725 {
    726 	struct dhcp_state *state;
    727 	struct ipv4_addr *ia;
    728 
    729 	ia = ipv4_addaddr(ifp, &lease->addr, &lease->mask, &lease->brd,
    730 	    lease->leasetime, lease->rebindtime);
    731 	if (ia == NULL)
    732 		return -1;
    733 
    734 	state = D_STATE(ifp);
    735 	state->added = STATE_ADDED;
    736 	state->addr = ia;
    737 	return 0;
    738 }
    739 
    740 struct ipv4_addr *
    741 ipv4_applyaddr(void *arg)
    742 {
    743 	struct interface *ifp = arg;
    744 	struct dhcp_state *state = D_STATE(ifp);
    745 	struct dhcp_lease *lease;
    746 	struct if_options *ifo = ifp->options;
    747 	struct ipv4_addr *ia, *old_ia;
    748 
    749 	if (state == NULL)
    750 		return NULL;
    751 
    752 	lease = &state->lease;
    753 	if (state->new == NULL) {
    754 		if ((ifo->options & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
    755 		    (DHCPCD_EXITING | DHCPCD_PERSISTENT)) {
    756 			if (state->added) {
    757 				/* Someone might have deleted our address */
    758 				if (state->addr != NULL)
    759 					ipv4_deladdr(state->addr, 0);
    760 				/* No address, ensure state is cleared  */
    761 				state->added = 0;
    762 				rt_build(ifp->ctx, AF_INET);
    763 			}
    764 			script_runreason(ifp, state->reason);
    765 		} else
    766 			rt_build(ifp->ctx, AF_INET);
    767 		return NULL;
    768 	}
    769 
    770 	/* ipv4_dadaddr() will overwrite this, we need it to purge later */
    771 	old_ia = ifp->options->options & DHCPCD_NOALIAS ? NULL : state->addr;
    772 
    773 	ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
    774 	/* If the netmask or broadcast is different, re-add the addresss.
    775 	 * If IP addresses do not have lifetimes, there is a very real chance
    776 	 * that re-adding them will scrub the subnet route temporarily
    777 	 * which is a bad thing, so avoid it. */
    778 	if (ia != NULL && ia->mask.s_addr == lease->mask.s_addr &&
    779 	    ia->brd.s_addr == lease->brd.s_addr) {
    780 #ifndef IP_LIFETIME
    781 		logdebugx("%s: IP address %s already exists", ifp->name,
    782 		    ia->saddr);
    783 #endif
    784 	} else {
    785 #ifdef __linux__
    786 		/* Linux does not change netmask/broadcast address
    787 		 * for re-added addresses, so we need to delete the old one
    788 		 * first. */
    789 		if (ia != NULL) {
    790 			ipv4_deladdr(ia, 0);
    791 			if (ia == old_ia)
    792 				old_ia = NULL;
    793 		}
    794 #endif
    795 #ifndef IP_LIFETIME
    796 		if (ipv4_daddaddr(ifp, lease) == -1 && errno != EEXIST)
    797 			return NULL;
    798 #endif
    799 	}
    800 #ifdef IP_LIFETIME
    801 	if (ipv4_daddaddr(ifp, lease) == -1 && errno != EEXIST)
    802 		return NULL;
    803 #endif
    804 
    805 	ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
    806 	if (ia == NULL) {
    807 		logerrx("%s: added address vanished", ifp->name);
    808 		return NULL;
    809 	}
    810 #if defined(ARP) && defined(IN_IFF_NOTUSEABLE)
    811 	if (ia->addr_flags & IN_IFF_NOTUSEABLE)
    812 		return NULL;
    813 #endif
    814 
    815 	/* Delete the old address if different */
    816 	if (old_ia && old_ia->addr.s_addr != lease->addr.s_addr)
    817 		ipv4_deladdr(old_ia, 0);
    818 
    819 	state->addr = ia;
    820 	state->added = STATE_ADDED;
    821 
    822 	rt_build(ifp->ctx, AF_INET);
    823 
    824 	if (state->state == DHS_BOUND) {
    825 		script_runreason(ifp, state->reason);
    826 		dhcpcd_daemonise(ifp->ctx);
    827 	}
    828 	return ia;
    829 }
    830 
    831 void
    832 ipv4_markaddrsstale(struct interface *ifp)
    833 {
    834 	struct ipv4_state *state;
    835 	struct ipv4_addr *ia;
    836 
    837 	state = IPV4_STATE(ifp);
    838 	if (state == NULL)
    839 		return;
    840 
    841 	TAILQ_FOREACH(ia, &state->addrs, next) {
    842 		ia->flags |= IPV4_AF_STALE;
    843 	}
    844 }
    845 
    846 void
    847 ipv4_deletestaleaddrs(struct interface *ifp)
    848 {
    849 	struct ipv4_state *state;
    850 	struct ipv4_addr *ia, *ia1;
    851 
    852 	state = IPV4_STATE(ifp);
    853 	if (state == NULL)
    854 		return;
    855 
    856 	TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) {
    857 		if (!(ia->flags & IPV4_AF_STALE))
    858 			continue;
    859 		ipv4_handleifa(ifp->ctx, RTM_DELADDR, ifp->ctx->ifaces,
    860 		    ifp->name, &ia->addr, &ia->mask, &ia->brd, 0, getpid());
    861 	}
    862 }
    863 
    864 void
    865 ipv4_handleifa(struct dhcpcd_ctx *ctx, int cmd, struct if_head *ifs,
    866     const char *ifname, const struct in_addr *addr, const struct in_addr *mask,
    867     const struct in_addr *brd, int addrflags, pid_t pid)
    868 {
    869 	struct interface *ifp;
    870 	struct ipv4_state *state;
    871 	struct ipv4_addr *ia;
    872 	bool ia_is_new;
    873 
    874 #if 0
    875 	char sbrdbuf[INET_ADDRSTRLEN];
    876 	const char *sbrd;
    877 
    878 	if (brd)
    879 		sbrd = inet_ntop(AF_INET, brd, sbrdbuf, sizeof(sbrdbuf));
    880 	else
    881 		sbrd = NULL;
    882 	logdebugx("%s: %s %s/%d %s %d", ifname,
    883 	    cmd == RTM_NEWADDR ? "RTM_NEWADDR" :
    884 	    cmd == RTM_DELADDR ? "RTM_DELADDR" : "???",
    885 	    inet_ntoa(*addr), inet_ntocidr(*mask), sbrd, addrflags);
    886 #endif
    887 
    888 	if (ifs == NULL)
    889 		ifs = ctx->ifaces;
    890 	if (ifs == NULL) {
    891 		errno = ESRCH;
    892 		return;
    893 	}
    894 	if ((ifp = if_find(ifs, ifname)) == NULL)
    895 		return;
    896 	if ((state = ipv4_getstate(ifp)) == NULL) {
    897 		errno = ENOENT;
    898 		return;
    899 	}
    900 
    901 	ia = ipv4_iffindaddr(ifp, addr, NULL);
    902 	switch (cmd) {
    903 	case RTM_NEWADDR:
    904 		if (ia == NULL) {
    905 			if ((ia = malloc(sizeof(*ia))) == NULL) {
    906 				logerr(__func__);
    907 				return;
    908 			}
    909 			ia->iface = ifp;
    910 			ia->addr = *addr;
    911 			ia->mask = *mask;
    912 			ia->flags = 0;
    913 			ia_is_new = true;
    914 #ifdef ALIAS_ADDR
    915 			strlcpy(ia->alias, ifname, sizeof(ia->alias));
    916 #endif
    917 			TAILQ_INSERT_TAIL(&state->addrs, ia, next);
    918 		} else
    919 			ia_is_new = false;
    920 		/* Mask could have changed */
    921 		if (ia_is_new ||
    922 		    (mask->s_addr != INADDR_ANY &&
    923 			mask->s_addr != ia->mask.s_addr)) {
    924 			ia->mask = *mask;
    925 			snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d",
    926 			    inet_ntoa(*addr), inet_ntocidr(*mask));
    927 		}
    928 		if (brd != NULL)
    929 			ia->brd = *brd;
    930 		else
    931 			ia->brd.s_addr = INADDR_ANY;
    932 		ia->addr_flags = addrflags;
    933 		ia->flags &= ~IPV4_AF_STALE;
    934 		break;
    935 	case RTM_DELADDR:
    936 		if (ia == NULL)
    937 			return;
    938 		if (mask->s_addr != INADDR_ANY &&
    939 		    mask->s_addr != ia->mask.s_addr)
    940 			return;
    941 		ia->addr_flags = addrflags;
    942 		TAILQ_REMOVE(&state->addrs, ia, next);
    943 		break;
    944 	default:
    945 		return;
    946 	}
    947 
    948 	if (addr->s_addr != INADDR_ANY && addr->s_addr != INADDR_BROADCAST) {
    949 		ia = dhcp_handleifa(cmd, ia, pid);
    950 #ifdef IPV4LL
    951 		if (ia != NULL)
    952 			ipv4ll_handleifa(cmd, ia, pid);
    953 #endif
    954 	}
    955 
    956 	if (cmd == RTM_DELADDR)
    957 		free(ia);
    958 }
    959 
    960 void
    961 ipv4_free(struct interface *ifp)
    962 {
    963 	struct ipv4_state *state;
    964 	struct ipv4_addr *ia;
    965 
    966 	if (ifp == NULL || (state = IPV4_STATE(ifp)) == NULL)
    967 		return;
    968 
    969 	while ((ia = TAILQ_FIRST(&state->addrs))) {
    970 		TAILQ_REMOVE(&state->addrs, ia, next);
    971 		free(ia);
    972 	}
    973 	free(state);
    974 }
    975