xref: /dragonfly/contrib/dhcpcd/src/ipv4.c (revision c9c5aa9e)
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 	if (ifp->options->options & DHCPCD_LASTLEASE_EXTEND) {
665 		/* We don't want the kernel to expire the address. */
666 		ia->vltime = ia->pltime = DHCP_INFINITE_LIFETIME;
667 	} else {
668 		ia->vltime = vltime;
669 		ia->pltime = pltime;
670 	}
671 #else
672 	UNUSED(vltime);
673 	UNUSED(pltime);
674 #endif
675 	snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d",
676 	    inet_ntoa(*addr), inet_ntocidr(*mask));
677 
678 #ifdef ALIAS_ADDR
679 	blank = (ia->alias[0] == '\0');
680 	if ((replaced = ipv4_aliasaddr(ia, &replaced_ia)) == -1) {
681 		logerr("%s: ipv4_aliasaddr", ifp->name);
682 		free(ia);
683 		return NULL;
684 	}
685 	if (blank)
686 		logdebugx("%s: aliased %s", ia->alias, ia->saddr);
687 #endif
688 
689 	logdebugx("%s: adding IP address %s %s %s",
690 	    ifp->name, ia->saddr,
691 	    ifp->flags & IFF_POINTOPOINT ? "destination" : "broadcast",
692 	    inet_ntoa(*bcast));
693 	if (if_address(RTM_NEWADDR, ia) == -1) {
694 		if (errno != EEXIST)
695 			logerr("%s: if_addaddress",
696 			    __func__);
697 		if (ia->flags & IPV4_AF_NEW)
698 			free(ia);
699 		return NULL;
700 	}
701 
702 #ifdef ALIAS_ADDR
703 	if (replaced) {
704 		TAILQ_REMOVE(&state->addrs, replaced_ia, next);
705 		free(replaced_ia);
706 	}
707 #endif
708 
709 	if (ia->flags & IPV4_AF_NEW)
710 		TAILQ_INSERT_TAIL(&state->addrs, ia, next);
711 	return ia;
712 }
713 
714 static int
715 ipv4_daddaddr(struct interface *ifp, const struct dhcp_lease *lease)
716 {
717 	struct dhcp_state *state;
718 	struct ipv4_addr *ia;
719 
720 	ia = ipv4_addaddr(ifp, &lease->addr, &lease->mask, &lease->brd,
721 	    lease->leasetime, lease->rebindtime);
722 	if (ia == NULL)
723 		return -1;
724 
725 	state = D_STATE(ifp);
726 	state->added = STATE_ADDED;
727 	state->addr = ia;
728 	return 0;
729 }
730 
731 struct ipv4_addr *
732 ipv4_applyaddr(void *arg)
733 {
734 	struct interface *ifp = arg;
735 	struct dhcp_state *state = D_STATE(ifp);
736 	struct dhcp_lease *lease;
737 	struct if_options *ifo = ifp->options;
738 	struct ipv4_addr *ia;
739 
740 	if (state == NULL)
741 		return NULL;
742 
743 	lease = &state->lease;
744 	if (state->new == NULL) {
745 		if ((ifo->options & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
746 		    (DHCPCD_EXITING | DHCPCD_PERSISTENT))
747 		{
748 			if (state->added) {
749 				delete_address(ifp);
750 				rt_build(ifp->ctx, AF_INET);
751 #ifdef ARP
752 				/* Announce the preferred address to
753 				 * kick ARP caches. */
754 				arp_announceaddr(ifp->ctx,&lease->addr);
755 #endif
756 			}
757 			script_runreason(ifp, state->reason);
758 		} else
759 			rt_build(ifp->ctx, AF_INET);
760 		return NULL;
761 	}
762 
763 	ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
764 	/* If the netmask or broadcast is different, re-add the addresss.
765 	 * If IP addresses do not have lifetimes, there is a very real chance
766 	 * that re-adding them will scrub the subnet route temporarily
767 	 * which is a bad thing, so avoid it. */
768 	if (ia != NULL &&
769 	    ia->mask.s_addr == lease->mask.s_addr &&
770 	    ia->brd.s_addr == lease->brd.s_addr)
771 	{
772 #ifndef IP_LIFETIME
773 		logdebugx("%s: IP address %s already exists",
774 		    ifp->name, ia->saddr);
775 #endif
776 	} else {
777 #ifdef __linux__
778 		/* Linux does not change netmask/broadcast address
779 		 * for re-added addresses, so we need to delete the old one
780 		 * first. */
781 		if (ia != NULL)
782 			ipv4_deladdr(ia, 0);
783 #endif
784 #ifndef IP_LIFETIME
785 		if (ipv4_daddaddr(ifp, lease) == -1 && errno != EEXIST)
786 			return NULL;
787 #endif
788 	}
789 #ifdef IP_LIFETIME
790 	if (ipv4_daddaddr(ifp, lease) == -1 && errno != EEXIST)
791 		return NULL;
792 #endif
793 
794 	ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
795 	if (ia == NULL) {
796 		logerrx("%s: added address vanished", ifp->name);
797 		return NULL;
798 	}
799 #if defined(ARP) && defined(IN_IFF_NOTUSEABLE)
800 	if (ia->addr_flags & IN_IFF_NOTUSEABLE)
801 		return NULL;
802 #endif
803 
804 	/* Delete the old address if different */
805 	if (state->addr &&
806 	    state->addr->addr.s_addr != lease->addr.s_addr &&
807 	    ipv4_iffindaddr(ifp, &lease->addr, NULL))
808 		delete_address(ifp);
809 
810 	state->addr = ia;
811 	state->added = STATE_ADDED;
812 
813 	rt_build(ifp->ctx, AF_INET);
814 
815 #ifdef ARP
816 	arp_announceaddr(ifp->ctx, &state->addr->addr);
817 #endif
818 
819 	if (state->state == DHS_BOUND) {
820 		script_runreason(ifp, state->reason);
821 		dhcpcd_daemonise(ifp->ctx);
822 	}
823 	return ia;
824 }
825 
826 void
827 ipv4_markaddrsstale(struct interface *ifp)
828 {
829 	struct ipv4_state *state;
830 	struct ipv4_addr *ia;
831 
832 	state = IPV4_STATE(ifp);
833 	if (state == NULL)
834 		return;
835 
836 	TAILQ_FOREACH(ia, &state->addrs, next) {
837 		ia->flags |= IPV4_AF_STALE;
838 	}
839 }
840 
841 void
842 ipv4_deletestaleaddrs(struct interface *ifp)
843 {
844 	struct ipv4_state *state;
845 	struct ipv4_addr *ia, *ia1;
846 
847 	state = IPV4_STATE(ifp);
848 	if (state == NULL)
849 		return;
850 
851 	TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) {
852 		if (!(ia->flags & IPV4_AF_STALE))
853 			continue;
854 		ipv4_handleifa(ifp->ctx, RTM_DELADDR,
855 		    ifp->ctx->ifaces, ifp->name,
856 		    &ia->addr, &ia->mask, &ia->brd, 0, getpid());
857 	}
858 }
859 
860 void
861 ipv4_handleifa(struct dhcpcd_ctx *ctx,
862     int cmd, struct if_head *ifs, const char *ifname,
863     const struct in_addr *addr, const struct in_addr *mask,
864     const struct in_addr *brd, int addrflags, pid_t pid)
865 {
866 	struct interface *ifp;
867 	struct ipv4_state *state;
868 	struct ipv4_addr *ia;
869 	bool ia_is_new;
870 
871 #if 0
872 	char sbrdbuf[INET_ADDRSTRLEN];
873 	const char *sbrd;
874 
875 	if (brd)
876 		sbrd = inet_ntop(AF_INET, brd, sbrdbuf, sizeof(sbrdbuf));
877 	else
878 		sbrd = NULL;
879 	logdebugx("%s: %s %s/%d %s %d", ifname,
880 	    cmd == RTM_NEWADDR ? "RTM_NEWADDR" :
881 	    cmd == RTM_DELADDR ? "RTM_DELADDR" : "???",
882 	    inet_ntoa(*addr), inet_ntocidr(*mask), sbrd, addrflags);
883 #endif
884 
885 	if (ifs == NULL)
886 		ifs = ctx->ifaces;
887 	if (ifs == NULL) {
888 		errno = ESRCH;
889 		return;
890 	}
891 	if ((ifp = if_find(ifs, ifname)) == NULL)
892 		return;
893 	if ((state = ipv4_getstate(ifp)) == NULL) {
894 		errno = ENOENT;
895 		return;
896 	}
897 
898 	ia = ipv4_iffindaddr(ifp, addr, NULL);
899 	switch (cmd) {
900 	case RTM_NEWADDR:
901 		if (ia == NULL) {
902 			if ((ia = malloc(sizeof(*ia))) == NULL) {
903 				logerr(__func__);
904 				return;
905 			}
906 			ia->iface = ifp;
907 			ia->addr = *addr;
908 			ia->mask = *mask;
909 			ia->flags = 0;
910 			ia_is_new = true;
911 #ifdef ALIAS_ADDR
912 			strlcpy(ia->alias, ifname, sizeof(ia->alias));
913 #endif
914 			TAILQ_INSERT_TAIL(&state->addrs, ia, next);
915 		} else
916 			ia_is_new = false;
917 		/* Mask could have changed */
918 		if (ia_is_new ||
919 		    (mask->s_addr != INADDR_ANY &&
920 		    mask->s_addr != ia->mask.s_addr))
921 		{
922 			ia->mask = *mask;
923 			snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d",
924 			    inet_ntoa(*addr), inet_ntocidr(*mask));
925 		}
926 		if (brd != NULL)
927 			ia->brd = *brd;
928 		else
929 			ia->brd.s_addr = INADDR_ANY;
930 		ia->addr_flags = addrflags;
931 		ia->flags &= ~IPV4_AF_STALE;
932 		break;
933 	case RTM_DELADDR:
934 		if (ia == NULL)
935 			return;
936 		if (mask->s_addr != INADDR_ANY &&
937 		    mask->s_addr != ia->mask.s_addr)
938 			return;
939 		TAILQ_REMOVE(&state->addrs, ia, next);
940 		break;
941 	default:
942 		return;
943 	}
944 
945 	if (addr->s_addr != INADDR_ANY && addr->s_addr != INADDR_BROADCAST) {
946 		ia = dhcp_handleifa(cmd, ia, pid);
947 #ifdef IPV4LL
948 		if (ia != NULL)
949 			ipv4ll_handleifa(cmd, ia, pid);
950 #endif
951 	}
952 
953 	if (cmd == RTM_DELADDR)
954 		free(ia);
955 }
956 
957 void
958 ipv4_free(struct interface *ifp)
959 {
960 	struct ipv4_state *state;
961 	struct ipv4_addr *ia;
962 
963 	if (ifp == NULL || (state = IPV4_STATE(ifp)) == NULL)
964 		return;
965 
966 	while ((ia = TAILQ_FIRST(&state->addrs))) {
967 		TAILQ_REMOVE(&state->addrs, ia, next);
968 		free(ia);
969 	}
970 	free(state);
971 }
972