1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <net/route.h>
34 
35 #include <ctype.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "config.h"
42 #include "arp.h"
43 #include "common.h"
44 #include "dhcpcd.h"
45 #include "dhcp.h"
46 #include "if.h"
47 #include "if-options.h"
48 #include "ipv4.h"
49 #include "script.h"
50 #ifdef PASSIVE_MODE
51 #include "rpc-interface.h"
52 #endif
53 
54 #define IPV4_LOOPBACK_ROUTE
55 #if defined(__linux__) || (defined(BSD) && defined(RTF_LOCAL))
56 /* Linux has had loopback routes in the local table since 2.2 */
57 #undef IPV4_LOOPBACK_ROUTE
58 #endif
59 
60 uint8_t
inet_ntocidr(struct in_addr address)61 inet_ntocidr(struct in_addr address)
62 {
63 	uint8_t cidr = 0;
64 	uint32_t mask = htonl(address.s_addr);
65 
66 	while (mask) {
67 		cidr++;
68 		mask <<= 1;
69 	}
70 	return cidr;
71 }
72 
73 int
inet_cidrtoaddr(int cidr,struct in_addr * addr)74 inet_cidrtoaddr(int cidr, struct in_addr *addr)
75 {
76 	int ocets;
77 
78 	if (cidr < 1 || cidr > 32) {
79 		errno = EINVAL;
80 		return -1;
81 	}
82 	ocets = (cidr + 7) / NBBY;
83 
84 	addr->s_addr = 0;
85 	if (ocets > 0) {
86 		memset(&addr->s_addr, 255, (size_t)ocets - 1);
87 		memset((unsigned char *)&addr->s_addr + (ocets - 1),
88 		    (256 - (1 << (32 - cidr) % NBBY)), 1);
89 	}
90 
91 	return 0;
92 }
93 
94 uint32_t
ipv4_getnetmask(uint32_t addr)95 ipv4_getnetmask(uint32_t addr)
96 {
97 	uint32_t dst;
98 
99 	if (addr == 0)
100 		return 0;
101 
102 	dst = htonl(addr);
103 	if (IN_CLASSA(dst))
104 		return ntohl(IN_CLASSA_NET);
105 	if (IN_CLASSB(dst))
106 		return ntohl(IN_CLASSB_NET);
107 	if (IN_CLASSC(dst))
108 		return ntohl(IN_CLASSC_NET);
109 
110 	return 0;
111 }
112 
113 struct ipv4_addr *
ipv4_iffindaddr(struct interface * ifp,const struct in_addr * addr,const struct in_addr * net)114 ipv4_iffindaddr(struct interface *ifp,
115     const struct in_addr *addr, const struct in_addr *net)
116 {
117 	struct ipv4_state *state;
118 	struct ipv4_addr *ap;
119 
120 	state = IPV4_STATE(ifp);
121 	if (state) {
122 		TAILQ_FOREACH(ap, &state->addrs, next) {
123 			if ((addr == NULL || ap->addr.s_addr == addr->s_addr) &&
124 			    (net == NULL || ap->net.s_addr == net->s_addr))
125 				return ap;
126 		}
127 	}
128 	return NULL;
129 }
130 
131 struct ipv4_addr *
ipv4_iffindlladdr(struct interface * ifp)132 ipv4_iffindlladdr(struct interface *ifp)
133 {
134 	struct ipv4_state *state;
135 	struct ipv4_addr *ap;
136 
137 	state = IPV4_STATE(ifp);
138 	if (state) {
139 		TAILQ_FOREACH(ap, &state->addrs, next) {
140 			if (IN_LINKLOCAL(htonl(ap->addr.s_addr)))
141 				return ap;
142 		}
143 	}
144 	return NULL;
145 }
146 
147 struct ipv4_addr *
ipv4_findaddr(struct dhcpcd_ctx * ctx,const struct in_addr * addr)148 ipv4_findaddr(struct dhcpcd_ctx *ctx, const struct in_addr *addr)
149 {
150 	struct interface *ifp;
151 	struct ipv4_addr *ap;
152 
153 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
154 		ap = ipv4_iffindaddr(ifp, addr, NULL);
155 		if (ap)
156 			return ap;
157 	}
158 	return NULL;
159 }
160 
161 int
ipv4_addrexists(struct dhcpcd_ctx * ctx,const struct in_addr * addr)162 ipv4_addrexists(struct dhcpcd_ctx *ctx, const struct in_addr *addr)
163 {
164 	struct interface *ifp;
165 	struct dhcp_state *state;
166 
167 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
168 		state = D_STATE(ifp);
169 		if (state) {
170 			if (addr == NULL) {
171 				if (state->addr.s_addr != INADDR_ANY)
172 					return 1;
173 			} else if (addr->s_addr == state->addr.s_addr)
174 				return 1;
175 		}
176 		if (addr != NULL && ipv4_iffindaddr(ifp, addr, NULL))
177 			return 1;
178 	}
179 	return 0;
180 }
181 
182 void
ipv4_freeroutes(struct rt_head * rts)183 ipv4_freeroutes(struct rt_head *rts)
184 {
185 
186 	if (rts) {
187 		ipv4_freerts(rts);
188 		free(rts);
189 	}
190 }
191 
192 int
ipv4_init(struct dhcpcd_ctx * ctx)193 ipv4_init(struct dhcpcd_ctx *ctx)
194 {
195 
196 	if (ctx->ipv4_routes == NULL) {
197 		ctx->ipv4_routes = malloc(sizeof(*ctx->ipv4_routes));
198 		if (ctx->ipv4_routes == NULL)
199 			return -1;
200 		TAILQ_INIT(ctx->ipv4_routes);
201 	}
202 	if (ctx->ipv4_kroutes == NULL) {
203 		ctx->ipv4_kroutes = malloc(sizeof(*ctx->ipv4_kroutes));
204 		if (ctx->ipv4_kroutes == NULL)
205 			return -1;
206 		TAILQ_INIT(ctx->ipv4_kroutes);
207 	}
208 	return 0;
209 }
210 
211 /* Interface comparer for working out ordering. */
212 int
ipv4_ifcmp(const struct interface * si,const struct interface * ti)213 ipv4_ifcmp(const struct interface *si, const struct interface *ti)
214 {
215 	const struct dhcp_state *sis, *tis;
216 
217 	sis = D_CSTATE(si);
218 	tis = D_CSTATE(ti);
219 	if (sis && !tis)
220 		return -1;
221 	if (!sis && tis)
222 		return 1;
223 	if (!sis && !tis)
224 		return 0;
225 	/* If one has a lease and the other not, it takes precedence. */
226 	if (sis->new && !tis->new)
227 		return -1;
228 	if (!sis->new && tis->new)
229 		return 1;
230 	/* Always prefer proper leases */
231 	if (!(sis->added & STATE_FAKE) && (sis->added & STATE_FAKE))
232 		return -1;
233 	if ((sis->added & STATE_FAKE) && !(sis->added & STATE_FAKE))
234 		return 1;
235 	/* If we are either, they neither have a lease, or they both have.
236 	 * We need to check for IPv4LL and make it non-preferred. */
237 	if (sis->new && tis->new) {
238 		int sill = (sis->new->cookie == htonl(MAGIC_COOKIE));
239 		int till = (tis->new->cookie == htonl(MAGIC_COOKIE));
240 		if (sill && !till)
241 			return -1;
242 		if (!sill && till)
243 			return 1;
244 	}
245 	return 0;
246 }
247 
248 static struct rt *
find_route(struct rt_head * rts,const struct rt * r,const struct rt * srt)249 find_route(struct rt_head *rts, const struct rt *r, const struct rt *srt)
250 {
251 	struct rt *rt;
252 
253 	if (rts == NULL)
254 		return NULL;
255 	TAILQ_FOREACH(rt, rts, next) {
256 		if (rt->dest.s_addr == r->dest.s_addr &&
257 #ifdef HAVE_ROUTE_METRIC
258 		    (srt || (r->iface == NULL || rt->iface == NULL ||
259 		    rt->iface->metric == r->iface->metric)) &&
260 #endif
261                     (!srt || srt != rt) &&
262 		    rt->net.s_addr == r->net.s_addr)
263 			return rt;
264 	}
265 	return NULL;
266 }
267 
268 static void
desc_route(const char * cmd,const struct rt * rt)269 desc_route(const char *cmd, const struct rt *rt)
270 {
271 	char addr[sizeof("000.000.000.000") + 1];
272 	struct dhcpcd_ctx *ctx = rt->iface ? rt->iface->ctx : NULL;
273 	const char *ifname = rt->iface ? rt->iface->name : NULL;
274 
275 	strlcpy(addr, inet_ntoa(rt->dest), sizeof(addr));
276 	if (rt->net.s_addr == htonl(INADDR_BROADCAST) &&
277 	    rt->gate.s_addr == htonl(INADDR_ANY))
278 		logger(ctx, LOG_INFO, "%s: %s host route to %s",
279 		    ifname, cmd, addr);
280 	else if (rt->net.s_addr == htonl(INADDR_BROADCAST))
281 		logger(ctx, LOG_INFO, "%s: %s host route to %s via %s",
282 		    ifname, cmd, addr, inet_ntoa(rt->gate));
283 	else if (rt->gate.s_addr == htonl(INADDR_ANY))
284 		logger(ctx, LOG_INFO, "%s: %s route to %s/%d",
285 		    ifname, cmd, addr, inet_ntocidr(rt->net));
286 	else if (rt->dest.s_addr == htonl(INADDR_ANY) &&
287 	    rt->net.s_addr == htonl(INADDR_ANY))
288 		logger(ctx, LOG_INFO, "%s: %s default route via %s",
289 		    ifname, cmd, inet_ntoa(rt->gate));
290 	else
291 		logger(ctx, LOG_INFO, "%s: %s route to %s/%d via %s",
292 		    ifname, cmd, addr, inet_ntocidr(rt->net),
293 		    inet_ntoa(rt->gate));
294 }
295 
296 static struct rt *
ipv4_findrt(struct dhcpcd_ctx * ctx,const struct rt * rt,int flags)297 ipv4_findrt(struct dhcpcd_ctx *ctx, const struct rt *rt, int flags)
298 {
299 	struct rt *r;
300 
301 	if (ctx->ipv4_kroutes == NULL)
302 		return NULL;
303 	TAILQ_FOREACH(r, ctx->ipv4_kroutes, next) {
304 		if (rt->dest.s_addr == r->dest.s_addr &&
305 #ifdef HAVE_ROUTE_METRIC
306 		    rt->iface == r->iface &&
307 		    (!flags || rt->metric == r->metric) &&
308 #else
309 		    (!flags || rt->iface == r->iface) &&
310 #endif
311 		    rt->net.s_addr == r->net.s_addr)
312 			return r;
313 	}
314 	return NULL;
315 }
316 
317 void
ipv4_freerts(struct rt_head * routes)318 ipv4_freerts(struct rt_head *routes)
319 {
320 	struct rt *rt;
321 
322 	while ((rt = TAILQ_FIRST(routes))) {
323 		TAILQ_REMOVE(routes, rt, next);
324 		free(rt);
325 	}
326 }
327 
328 /* If something other than dhcpcd removes a route,
329  * we need to remove it from our internal table. */
330 int
ipv4_handlert(struct dhcpcd_ctx * ctx,int cmd,struct rt * rt)331 ipv4_handlert(struct dhcpcd_ctx *ctx, int cmd, struct rt *rt)
332 {
333 	struct rt *f;
334 
335 	if (ctx->ipv4_kroutes == NULL)
336 		return 0;
337 
338 	f = ipv4_findrt(ctx, rt, 1);
339 	switch (cmd) {
340 	case RTM_ADD:
341 		if (f == NULL) {
342 			if ((f = malloc(sizeof(*f))) == NULL)
343 				return -1;
344 			*f = *rt;
345 			TAILQ_INSERT_TAIL(ctx->ipv4_kroutes, f, next);
346 		}
347 		break;
348 	case RTM_DELETE:
349 		if (f) {
350 			TAILQ_REMOVE(ctx->ipv4_kroutes, f, next);
351 			free(f);
352 		}
353 
354 		/* If we manage the route, remove it */
355 		if ((f = find_route(ctx->ipv4_routes, rt, NULL))) {
356 			desc_route("removing", f);
357 			TAILQ_REMOVE(ctx->ipv4_routes, f, next);
358 			free(f);
359 		}
360 		break;
361 	}
362 	return 0;
363 }
364 
365 #define n_route(a)	 nc_route(NULL, a)
366 #define c_route(a, b)	 nc_route(a, b)
367 static int
nc_route(struct rt * ort,struct rt * nrt)368 nc_route(struct rt *ort, struct rt *nrt)
369 {
370 
371 	/* Don't set default routes if not asked to */
372 	if (nrt->dest.s_addr == 0 &&
373 	    nrt->net.s_addr == 0 &&
374 	    !(nrt->iface->options->options & DHCPCD_GATEWAY))
375 		return -1;
376 
377 	desc_route(ort == NULL ? "adding" : "changing", nrt);
378 
379 	if (ort == NULL) {
380 		ort = ipv4_findrt(nrt->iface->ctx, nrt, 0);
381 		if (ort &&
382 		    ((ort->flags & RTF_REJECT && nrt->flags & RTF_REJECT) ||
383 		     (ort->iface == nrt->iface &&
384 #ifdef HAVE_ROUTE_METRIC
385 		    ort->metric == nrt->metric &&
386 #endif
387 		    ort->gate.s_addr == nrt->gate.s_addr)))
388 			return 0;
389 	} else if (ort->flags & STATE_FAKE && !(nrt->flags & STATE_FAKE) &&
390 	    ort->iface == nrt->iface &&
391 #ifdef HAVE_ROUTE_METRIC
392 	    ort->metric == nrt->metric &&
393 #endif
394 	    ort->dest.s_addr == nrt->dest.s_addr &&
395 	    ort->net.s_addr ==  nrt->net.s_addr &&
396 	    ort->gate.s_addr == nrt->gate.s_addr)
397 		return 0;
398 
399 #ifdef HAVE_ROUTE_METRIC
400 	/* With route metrics, we can safely add the new route before
401 	 * deleting the old route. */
402 	if (if_route(RTM_ADD, nrt)  == 0) {
403 		if (ort && if_route(RTM_DELETE, ort) == -1 && errno != ESRCH)
404 			logger(nrt->iface->ctx, LOG_ERR, "if_route (DEL): %m");
405 		return 0;
406 	}
407 
408 	/* If the kernel claims the route exists we need to rip out the
409 	 * old one first. */
410 	if (errno != EEXIST || ort == NULL)
411 		goto logerr;
412 #endif
413 
414 	/* No route metrics, we need to delete the old route before
415 	 * adding the new one. */
416 	if (ort && if_route(RTM_DELETE, ort) == -1 && errno != ESRCH)
417 		logger(nrt->iface->ctx, LOG_ERR, "if_route (DEL): %m");
418 	if (if_route(RTM_ADD, nrt) == 0)
419 		return 0;
420 #ifdef HAVE_ROUTE_METRIC
421 logerr:
422 #endif
423 	logger(nrt->iface->ctx, LOG_ERR, "if_route (ADD): %m");
424 	return -1;
425 }
426 
427 static int
d_route(struct rt * rt)428 d_route(struct rt *rt)
429 {
430 	int retval;
431 
432 	desc_route("deleting", rt);
433 	retval = if_route(RTM_DELETE, rt);
434 	if (retval != 0 && errno != ENOENT && errno != ESRCH)
435 		logger(rt->iface->ctx, LOG_ERR,
436 		    "%s: if_delroute: %m", rt->iface->name);
437 	return retval;
438 }
439 
440 static struct rt_head *
add_subnet_route(struct rt_head * rt,const struct interface * ifp)441 add_subnet_route(struct rt_head *rt, const struct interface *ifp)
442 {
443 	const struct dhcp_state *s;
444 	struct rt *r;
445 
446 	if (rt == NULL) /* earlier malloc failed */
447 		return NULL;
448 
449 	s = D_CSTATE(ifp);
450 	/* Don't create a subnet route for these addresses */
451 	if (s->net.s_addr == INADDR_ANY)
452 		return rt;
453 #ifndef BSD
454 	/* BSD adds a route in this instance */
455 	if (s->net.s_addr == INADDR_BROADCAST)
456 		return rt;
457 #endif
458 
459 	if ((r = malloc(sizeof(*r))) == NULL) {
460 		logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
461 		ipv4_freeroutes(rt);
462 		return NULL;
463 	}
464 	r->dest.s_addr = s->addr.s_addr & s->net.s_addr;
465 	r->net.s_addr = s->net.s_addr;
466 	r->gate.s_addr = INADDR_ANY;
467 
468 	TAILQ_INSERT_HEAD(rt, r, next);
469 	return rt;
470 }
471 
472 #ifdef IPV4_LOOPBACK_ROUTE
473 static struct rt_head *
add_loopback_route(struct rt_head * rt,const struct interface * ifp)474 add_loopback_route(struct rt_head *rt, const struct interface *ifp)
475 {
476 	struct rt *r;
477 	const struct dhcp_state *s;
478 
479 	if (rt == NULL) /* earlier malloc failed */
480 		return NULL;
481 
482 	s = D_CSTATE(ifp);
483 	if (s->addr.s_addr == INADDR_ANY)
484 		return rt;
485 
486 	r = malloc(sizeof(*r));
487 	if (r == NULL) {
488 		logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
489 		ipv4_freeroutes(rt);
490 		return NULL;
491 	}
492 	r->dest.s_addr = s->addr.s_addr;
493 	r->net.s_addr = INADDR_BROADCAST;
494 	r->gate.s_addr = htonl(INADDR_LOOPBACK);
495 	TAILQ_INSERT_HEAD(rt, r, next);
496 	return rt;
497 }
498 #endif
499 
500 static struct rt_head *
get_routes(struct interface * ifp)501 get_routes(struct interface *ifp)
502 {
503 	struct rt_head *nrt;
504 	struct rt *rt, *r = NULL;
505 
506 	if (ifp->options->routes && TAILQ_FIRST(ifp->options->routes)) {
507 		nrt = malloc(sizeof(*nrt));
508 		TAILQ_INIT(nrt);
509 		TAILQ_FOREACH(rt, ifp->options->routes, next) {
510 			if (rt->gate.s_addr == 0)
511 				break;
512 			r = malloc(sizeof(*r));
513 			if (r == NULL) {
514 				logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
515 				ipv4_freeroutes(nrt);
516 				return NULL;
517 			}
518 			memcpy(r, rt, sizeof(*r));
519 			TAILQ_INSERT_TAIL(nrt, r, next);
520 		}
521 		return nrt;
522 	}
523 
524 	return get_option_routes(ifp, D_STATE(ifp)->new);
525 }
526 
527 /* Some DHCP servers add set host routes by setting the gateway
528  * to the assigned IP address or the destination address.
529  * We need to change this. */
530 static struct rt_head *
massage_host_routes(struct rt_head * rt,const struct interface * ifp)531 massage_host_routes(struct rt_head *rt, const struct interface *ifp)
532 {
533 	struct rt *r;
534 
535 	if (rt) {
536 		TAILQ_FOREACH(r, rt, next) {
537 			if (r->gate.s_addr == D_CSTATE(ifp)->addr.s_addr ||
538 			    r->gate.s_addr == r->dest.s_addr)
539 			{
540 				r->gate.s_addr = htonl(INADDR_ANY);
541 				r->net.s_addr = htonl(INADDR_BROADCAST);
542 			}
543 		}
544 	}
545 	return rt;
546 }
547 
548 
549 static struct rt_head *
add_destination_route(struct rt_head * rt,const struct interface * ifp)550 add_destination_route(struct rt_head *rt, const struct interface *ifp)
551 {
552 	struct rt *r;
553 
554 	if (rt == NULL || /* failed a malloc earlier probably */
555 	    !(ifp->flags & IFF_POINTOPOINT) ||
556 	    !has_option_mask(ifp->options->dstmask, DHO_ROUTER))
557 		return rt;
558 
559 	r = malloc(sizeof(*r));
560 	if (r == NULL) {
561 		logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
562 		ipv4_freeroutes(rt);
563 		return NULL;
564 	}
565 	r->dest.s_addr = INADDR_ANY;
566 	r->net.s_addr = INADDR_ANY;
567 	r->gate.s_addr = D_CSTATE(ifp)->dst.s_addr;
568 	TAILQ_INSERT_HEAD(rt, r, next);
569 	return rt;
570 }
571 
572 /* We should check to ensure the routers are on the same subnet
573  * OR supply a host route. If not, warn and add a host route. */
574 static struct rt_head *
add_router_host_route(struct rt_head * rt,const struct interface * ifp)575 add_router_host_route(struct rt_head *rt, const struct interface *ifp)
576 {
577 	struct rt *rtp, *rtn;
578 	const char *cp, *cp2, *cp3, *cplim;
579 	struct if_options *ifo;
580 	const struct dhcp_state *state;
581 
582 	if (rt == NULL) /* earlier malloc failed */
583 		return NULL;
584 
585 	TAILQ_FOREACH(rtp, rt, next) {
586 		if (rtp->dest.s_addr != INADDR_ANY)
587 			continue;
588 		/* Scan for a route to match */
589 		TAILQ_FOREACH(rtn, rt, next) {
590 			if (rtn == rtp)
591 				break;
592 			/* match host */
593 			if (rtn->dest.s_addr == rtp->gate.s_addr)
594 				break;
595 			/* match subnet */
596 			cp = (const char *)&rtp->gate.s_addr;
597 			cp2 = (const char *)&rtn->dest.s_addr;
598 			cp3 = (const char *)&rtn->net.s_addr;
599 			cplim = cp3 + sizeof(rtn->net.s_addr);
600 			while (cp3 < cplim) {
601 				if ((*cp++ ^ *cp2++) & *cp3++)
602 					break;
603 			}
604 			if (cp3 == cplim)
605 				break;
606 		}
607 		if (rtn != rtp)
608 			continue;
609 		state = D_CSTATE(ifp);
610 		ifo = ifp->options;
611 		if (ifp->flags & IFF_NOARP) {
612 			if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) &&
613 			    !(state->added & STATE_FAKE))
614 			{
615 				ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED;
616 				logger(ifp->ctx, LOG_WARNING,
617 				    "%s: forcing router %s through interface",
618 				    ifp->name, inet_ntoa(rtp->gate));
619 			}
620 			rtp->gate.s_addr = 0;
621 			continue;
622 		}
623 		if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) &&
624 		    !(state->added & STATE_FAKE))
625 		{
626 			ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED;
627 			logger(ifp->ctx, LOG_WARNING,
628 			    "%s: router %s requires a host route",
629 			    ifp->name, inet_ntoa(rtp->gate));
630 		}
631 		rtn = malloc(sizeof(*rtn));
632 		if (rtn == NULL) {
633 			logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
634 			ipv4_freeroutes(rt);
635 			return NULL;
636 		}
637 		rtn->dest.s_addr = rtp->gate.s_addr;
638 		rtn->net.s_addr = htonl(INADDR_BROADCAST);
639 		rtn->gate.s_addr = htonl(INADDR_ANY);
640 		TAILQ_INSERT_BEFORE(rtp, rtn, next);
641 	}
642 	return rt;
643 }
644 
645 void
ipv4_buildroutes(struct dhcpcd_ctx * ctx)646 ipv4_buildroutes(struct dhcpcd_ctx *ctx)
647 {
648 /* Do not modify route table when running in passive mode. */
649 #ifndef PASSIVE_MODE
650 	struct rt_head *nrs, *dnr;
651 	struct rt *or, *rt, *rtn;
652 	struct interface *ifp;
653 	const struct dhcp_state *state;
654 
655 	/* We need to have the interfaces in the correct order to ensure
656 	 * our routes are managed correctly. */
657 	if_sortinterfaces(ctx);
658 
659 	nrs = malloc(sizeof(*nrs));
660 	if (nrs == NULL) {
661 		logger(ctx, LOG_ERR, "%s: %m", __func__);
662 		return;
663 	}
664 	TAILQ_INIT(nrs);
665 
666 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
667 		state = D_CSTATE(ifp);
668 		if (state == NULL || state->new == NULL || !state->added)
669 			continue;
670 		dnr = get_routes(ifp);
671 		dnr = massage_host_routes(dnr, ifp);
672 		dnr = add_subnet_route(dnr, ifp);
673 #ifdef IPV4_LOOPBACK_ROUTE
674 		dnr = add_loopback_route(dnr, ifp);
675 #endif
676 		if (ifp->options->options & DHCPCD_GATEWAY) {
677 			dnr = add_router_host_route(dnr, ifp);
678 			dnr = add_destination_route(dnr, ifp);
679 		}
680 		if (dnr == NULL) /* failed to malloc all new routes */
681 			continue;
682 		TAILQ_FOREACH_SAFE(rt, dnr, next, rtn) {
683 			rt->iface = ifp;
684 #ifdef HAVE_ROUTE_METRIC
685 			rt->metric = ifp->metric;
686 #endif
687 			rt->flags = state->added & STATE_FAKE;
688 			/* Is this route already in our table? */
689 			if ((find_route(nrs, rt, NULL)) != NULL)
690 				continue;
691 			rt->src.s_addr = state->addr.s_addr;
692 			/* Do we already manage it? */
693 			if ((or = find_route(ctx->ipv4_routes, rt, NULL))) {
694 				if (state->added & STATE_FAKE)
695 					continue;
696 				if (or->flags & STATE_FAKE ||
697 				    or->iface != ifp ||
698 #ifdef HAVE_ROUTE_METRIC
699 				    rt->metric != or->metric ||
700 #endif
701 				    or->src.s_addr != state->addr.s_addr ||
702 				    rt->gate.s_addr != or->gate.s_addr)
703 				{
704 					if (c_route(or, rt) != 0)
705 						continue;
706 				}
707 				TAILQ_REMOVE(ctx->ipv4_routes, or, next);
708 				free(or);
709 			} else {
710 				if (state->added & STATE_FAKE) {
711 					or = ipv4_findrt(ctx, rt, 1);
712 					if (or == NULL ||
713 					    or->gate.s_addr != rt->gate.s_addr)
714  						continue;
715 				} else {
716 					if (n_route(rt) != 0)
717 						continue;
718 				}
719 			}
720 			rt->flags |= STATE_ADDED;
721 			TAILQ_REMOVE(dnr, rt, next);
722 			TAILQ_INSERT_TAIL(nrs, rt, next);
723 		}
724 		ipv4_freeroutes(dnr);
725 	}
726 
727 	/* Remove old routes we used to manage */
728 	if (ctx->ipv4_routes) {
729 		TAILQ_FOREACH(rt, ctx->ipv4_routes, next) {
730 			if (find_route(nrs, rt, NULL) == NULL &&
731 			    (rt->iface->options->options &
732 			    (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
733 			    (DHCPCD_EXITING | DHCPCD_PERSISTENT))
734 				d_route(rt);
735 		}
736 	}
737 	ipv4_freeroutes(ctx->ipv4_routes);
738 	ctx->ipv4_routes = nrs;
739 #endif
740 }
741 
742 int
ipv4_deladdr(struct interface * ifp,const struct in_addr * addr,const struct in_addr * net)743 ipv4_deladdr(struct interface *ifp,
744     const struct in_addr *addr, const struct in_addr *net)
745 {
746 	int r = 0;
747 #ifndef PASSIVE_MODE
748 	struct dhcp_state *dstate;
749 	struct ipv4_state *state;
750 	struct ipv4_addr *ap;
751 
752 	logger(ifp->ctx, LOG_DEBUG, "%s: deleting IP address %s/%d",
753 	    ifp->name, inet_ntoa(*addr), inet_ntocidr(*net));
754 
755 	r = if_deladdress(ifp, addr, net);
756 	if (r == -1 && errno != EADDRNOTAVAIL && errno != ENXIO &&
757 	    errno != ENODEV)
758 		logger(ifp->ctx, LOG_ERR, "%s: %s: %m", ifp->name, __func__);
759 
760 	dstate = D_STATE(ifp);
761 	if (dstate->addr.s_addr == addr->s_addr &&
762 	    dstate->net.s_addr == net->s_addr)
763 	{
764 		dstate->added = 0;
765 		dstate->addr.s_addr = 0;
766 		dstate->net.s_addr = 0;
767 	}
768 
769 	state = IPV4_STATE(ifp);
770 	TAILQ_FOREACH(ap, &state->addrs, next) {
771 		if (ap->addr.s_addr == addr->s_addr &&
772 		    ap->net.s_addr == net->s_addr)
773 		{
774 			TAILQ_REMOVE(&state->addrs, ap, next);
775 			free(ap);
776 			break;
777 		}
778 	}
779 #endif
780 	return r;
781 }
782 
783 static int
delete_address(struct interface * ifp)784 delete_address(struct interface *ifp)
785 {
786 	int r;
787 	struct if_options *ifo;
788 	struct dhcp_state *state;
789 
790 	state = D_STATE(ifp);
791 	ifo = ifp->options;
792 	if (ifo->options & DHCPCD_INFORM ||
793 	    (ifo->options & DHCPCD_STATIC && ifo->req_addr.s_addr == 0))
794 		return 0;
795 	r = ipv4_deladdr(ifp, &state->addr, &state->net);
796 	return r;
797 }
798 
799 static struct ipv4_state *
ipv4_getstate(struct interface * ifp)800 ipv4_getstate(struct interface *ifp)
801 {
802 	struct ipv4_state *state;
803 
804 	state = IPV4_STATE(ifp);
805 	if (state == NULL) {
806 	        ifp->if_data[IF_DATA_IPV4] = malloc(sizeof(*state));
807 		state = IPV4_STATE(ifp);
808 		if (state == NULL) {
809 			logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
810 			return NULL;
811 		}
812 		TAILQ_INIT(&state->addrs);
813 		TAILQ_INIT(&state->routes);
814 	}
815 	return state;
816 }
817 
818 static int
ipv4_addaddr(struct interface * ifp,const struct dhcp_lease * lease)819 ipv4_addaddr(struct interface *ifp, const struct dhcp_lease *lease)
820 {
821 	struct ipv4_state *state;
822 	struct ipv4_addr *ia;
823 
824 	if ((state = ipv4_getstate(ifp)) == NULL) {
825 		logger(ifp->ctx, LOG_ERR, "%s: ipv4_getstate: %m", __func__);
826 		return -1;
827 	}
828 	if (ifp->options->options & DHCPCD_NOALIAS) {
829 		struct ipv4_addr *ian;
830 
831 		TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ian) {
832 			if (ia->addr.s_addr != lease->addr.s_addr)
833 				ipv4_deladdr(ifp, &ia->addr, &ia->net);
834 		}
835 	}
836 
837 	if ((ia = malloc(sizeof(*ia))) == NULL) {
838 		logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
839 		return -1;
840 	}
841 
842 	logger(ifp->ctx, LOG_DEBUG, "%s: adding IP address %s/%d",
843 	    ifp->name, inet_ntoa(lease->addr),
844 	    inet_ntocidr(lease->net));
845 	if (if_addaddress(ifp, &lease->addr, &lease->net, &lease->brd) == -1) {
846 		if (errno != EEXIST)
847 			logger(ifp->ctx, LOG_ERR, "%s: if_addaddress: %m",
848 			    __func__);
849 		free(ia);
850 		return -1;
851 	}
852 
853 	ia->iface = ifp;
854 	ia->addr = lease->addr;
855 	ia->net = lease->net;
856 #ifdef IN_IFF_TENTATIVE
857 	ia->addr_flags = IN_IFF_TENTATIVE;
858 #endif
859 	TAILQ_INSERT_TAIL(&state->addrs, ia, next);
860 	return 0;
861 }
862 
863 static void
ipv4_finalisert(struct interface * ifp)864 ipv4_finalisert(struct interface *ifp)
865 {
866 	const struct dhcp_state *state = D_CSTATE(ifp);
867 
868 	/* Find any freshly added routes, such as the subnet route.
869 	 * We do this because we cannot rely on recieving the kernel
870 	 * notification right now via our link socket. */
871 	if_initrt(ifp);
872 	ipv4_buildroutes(ifp->ctx);
873 	script_runreason(ifp, state->reason);
874 
875 	dhcpcd_daemonise(ifp->ctx);
876 }
877 
878 void
ipv4_finaliseaddr(struct interface * ifp)879 ipv4_finaliseaddr(struct interface *ifp)
880 {
881 #ifndef PASSIVE_MODE
882 	struct dhcp_state *state = D_STATE(ifp);
883 	struct dhcp_lease *lease;
884 
885 	lease = &state->lease;
886 
887 	/* Delete the old address if different */
888 	if (state->addr.s_addr != lease->addr.s_addr &&
889 	    state->addr.s_addr != 0 &&
890 	    ipv4_iffindaddr(ifp, &lease->addr, NULL))
891 		delete_address(ifp);
892 
893 	state->added = STATE_ADDED;
894 	state->defend = 0;
895 	state->addr.s_addr = lease->addr.s_addr;
896 	state->net.s_addr = lease->net.s_addr;
897 	ipv4_finalisert(ifp);
898 #endif
899 }
900 
901 void
ipv4_applyaddr(void * arg)902 ipv4_applyaddr(void *arg)
903 {
904 #ifdef PASSIVE_MODE
905 	rpc_update_ipv4(arg);
906 #else
907 	struct interface *ifp = arg, *ifn;
908 	struct dhcp_state *state = D_STATE(ifp), *nstate;
909 	struct dhcp_message *dhcp;
910 	struct dhcp_lease *lease;
911 	struct if_options *ifo = ifp->options;
912 	struct ipv4_addr *ap;
913 	int r;
914 
915 	if (state == NULL)
916 		return;
917 	dhcp = state->new;
918 	lease = &state->lease;
919 
920 	if (dhcp == NULL) {
921 		if ((ifo->options & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
922 		    (DHCPCD_EXITING | DHCPCD_PERSISTENT))
923 		{
924 			if (state->added) {
925 				struct in_addr addr;
926 
927 				addr = state->addr;
928 				delete_address(ifp);
929 				TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
930 					if (ifn == ifp ||
931 					    strcmp(ifn->name, ifp->name) == 0)
932 						continue;
933 					nstate = D_STATE(ifn);
934 					if (nstate && !nstate->added &&
935 					    nstate->addr.s_addr == addr.s_addr)
936 					{
937 						if (ifn->options->options &
938 						    DHCPCD_ARP)
939 						{
940 							dhcp_bind(ifn, NULL);
941 						} else {
942 							ipv4_addaddr(ifn,
943 							    &nstate->lease);
944 							nstate->added =
945 							    STATE_ADDED;
946 						}
947 						break;
948 					}
949 				}
950 			}
951 			ipv4_buildroutes(ifp->ctx);
952 			script_runreason(ifp, state->reason);
953 		} else
954 			ipv4_buildroutes(ifp->ctx);
955 		return;
956 	}
957 
958 	/* Ensure only one interface has the address */
959 	TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
960 		if (ifn == ifp || strcmp(ifn->name, ifp->name) == 0)
961 			continue;
962 		nstate = D_STATE(ifn);
963 		if (nstate && nstate->added &&
964 		    nstate->addr.s_addr == lease->addr.s_addr)
965 		{
966 			if (ifn->metric <= ifp->metric) {
967 				logger(ifp->ctx, LOG_INFO,
968 				    "%s: preferring %s on %s",
969 				    ifp->name,
970 				    inet_ntoa(lease->addr),
971 				    ifn->name);
972 				state->addr.s_addr = lease->addr.s_addr;
973 				state->net.s_addr = lease->net.s_addr;
974 				ipv4_finalisert(ifp);
975 			}
976 			logger(ifp->ctx, LOG_INFO, "%s: preferring %s on %s",
977 			    ifn->name,
978 			    inet_ntoa(lease->addr),
979 			    ifp->name);
980 			ipv4_deladdr(ifn, &nstate->addr, &nstate->net);
981 			nstate->added = 0;
982 			break;
983 		}
984 	}
985 
986 	/* Does another interface already have the address from a prior boot? */
987 	if (ifn == NULL) {
988 		TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
989 			if (ifn == ifp || strcmp(ifn->name, ifp->name) == 0)
990 				continue;
991 			ap = ipv4_iffindaddr(ifn, &lease->addr, NULL);
992 			if (ap)
993 				ipv4_deladdr(ifn, &ap->addr, &ap->net);
994 		}
995 	}
996 
997 	/* If the netmask is different, delete the addresss */
998 	ap = ipv4_iffindaddr(ifp, &lease->addr, NULL);
999 	if (ap && ap->net.s_addr != lease->net.s_addr)
1000 		ipv4_deladdr(ifp, &ap->addr, &ap->net);
1001 
1002 	if (ipv4_iffindaddr(ifp, &lease->addr, &lease->net))
1003 		logger(ifp->ctx, LOG_DEBUG,
1004 		    "%s: IP address %s/%d already exists",
1005 		    ifp->name, inet_ntoa(lease->addr),
1006 		    inet_ntocidr(lease->net));
1007 	else {
1008 		r = ipv4_addaddr(ifp, lease);
1009 		if (r == -1 && errno != EEXIST)
1010 			return;
1011 	}
1012 
1013 #ifdef IN_IFF_NOTUSEABLE
1014 	ap = ipv4_iffindaddr(ifp, &lease->addr, NULL);
1015 	if (ap == NULL) {
1016 		logger(ifp->ctx, LOG_ERR, "%s: added address vanished",
1017 		    ifp->name);
1018 		return;
1019 	} else if (ap->addr_flags & IN_IFF_NOTUSEABLE)
1020 		return;
1021 #endif
1022 
1023 	ipv4_finaliseaddr(ifp);
1024 
1025 #endif  /* PASSIVE_MODE */
1026 }
1027 
1028 void
ipv4_handleifa(struct dhcpcd_ctx * ctx,int cmd,struct if_head * ifs,const char * ifname,const struct in_addr * addr,const struct in_addr * net,const struct in_addr * dst,int flags)1029 ipv4_handleifa(struct dhcpcd_ctx *ctx,
1030     int cmd, struct if_head *ifs, const char *ifname,
1031     const struct in_addr *addr, const struct in_addr *net,
1032     const struct in_addr *dst, int flags)
1033 {
1034 	struct interface *ifp;
1035 	struct ipv4_state *state;
1036 	struct ipv4_addr *ap;
1037 
1038 	if (ifs == NULL)
1039 		ifs = ctx->ifaces;
1040 	if (ifs == NULL) {
1041 		errno = ESRCH;
1042 		return;
1043 	}
1044 	if (addr->s_addr == INADDR_ANY) {
1045 		errno = EINVAL;
1046 		return;
1047 	}
1048 	if ((ifp = if_find(ifs, ifname)) == NULL)
1049 		return;
1050 	if ((state = ipv4_getstate(ifp)) == NULL) {
1051 		errno = ENOENT;
1052 		return;
1053 	}
1054 
1055 	ap = ipv4_iffindaddr(ifp, addr, net);
1056 	if (cmd == RTM_NEWADDR) {
1057 		if (ap == NULL) {
1058 			if ((ap = malloc(sizeof(*ap))) == NULL) {
1059 				logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
1060 				return;
1061 			}
1062 			ap->iface = ifp;
1063 			ap->addr = *addr;
1064 			ap->net = *net;
1065 			if (dst)
1066 				ap->dst.s_addr = dst->s_addr;
1067 			else
1068 				ap->dst.s_addr = INADDR_ANY;
1069 			TAILQ_INSERT_TAIL(&state->addrs, ap, next);
1070 		}
1071 		ap->addr_flags = flags;
1072 	} else if (cmd == RTM_DELADDR) {
1073 		if (ap) {
1074 			TAILQ_REMOVE(&state->addrs, ap, next);
1075 			free(ap);
1076 		}
1077 	}
1078 
1079 	dhcp_handleifa(cmd, ifp, addr, net, dst, flags);
1080 	arp_handleifa(cmd, ifp, addr, flags);
1081 }
1082 
1083 void
ipv4_free(struct interface * ifp)1084 ipv4_free(struct interface *ifp)
1085 {
1086 	struct ipv4_state *state;
1087 	struct ipv4_addr *addr;
1088 
1089 	if (ifp) {
1090 		state = IPV4_STATE(ifp);
1091 		if (state) {
1092 		        while ((addr = TAILQ_FIRST(&state->addrs))) {
1093 				TAILQ_REMOVE(&state->addrs, addr, next);
1094 				free(addr);
1095 			}
1096 			ipv4_freerts(&state->routes);
1097 			free(state);
1098 		}
1099 	}
1100 }
1101 
1102 void
ipv4_ctxfree(struct dhcpcd_ctx * ctx)1103 ipv4_ctxfree(struct dhcpcd_ctx *ctx)
1104 {
1105 
1106 	ipv4_freeroutes(ctx->ipv4_routes);
1107 	ipv4_freeroutes(ctx->ipv4_kroutes);
1108 }
1109