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