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