xref: /dragonfly/contrib/dhcpcd/src/ipv4ll.c (revision 6a3cbbc2)
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 <arpa/inet.h>
30 
31 #include <assert.h>
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #define ELOOP_QUEUE	IPV4LL
40 #include "config.h"
41 #include "arp.h"
42 #include "common.h"
43 #include "dhcp.h"
44 #include "eloop.h"
45 #include "if.h"
46 #include "if-options.h"
47 #include "ipv4.h"
48 #include "ipv4ll.h"
49 #include "logerr.h"
50 #include "sa.h"
51 #include "script.h"
52 
53 static const struct in_addr inaddr_llmask = {
54 	.s_addr = HTONL(LINKLOCAL_MASK)
55 };
56 static const struct in_addr inaddr_llbcast = {
57 	.s_addr = HTONL(LINKLOCAL_BCAST)
58 };
59 
60 static void ipv4ll_start1(struct interface *, struct arp_state *);
61 
62 static in_addr_t
63 ipv4ll_pickaddr(struct interface *ifp)
64 {
65 	struct in_addr addr;
66 	struct ipv4ll_state *state;
67 
68 	state = IPV4LL_STATE(ifp);
69 	setstate(state->randomstate);
70 
71 	do {
72 		long r;
73 
74 again:
75 		/* RFC 3927 Section 2.1 states that the first 256 and
76 		 * last 256 addresses are reserved for future use.
77 		 * See ipv4ll_start for why we don't use arc4random. */
78 		/* coverity[dont_call] */
79 		r = random();
80 		addr.s_addr = ntohl(LINKLOCAL_ADDR |
81 		    ((uint32_t)(r % 0xFD00) + 0x0100));
82 
83 		/* No point using a failed address */
84 		if (IN_ARE_ADDR_EQUAL(&addr, &state->pickedaddr))
85 			goto again;
86 		/* Ensure we don't have the address on another interface */
87 	} while (ipv4_findaddr(ifp->ctx, &addr) != NULL);
88 
89 	/* Restore the original random state */
90 	setstate(ifp->ctx->randomstate);
91 	return addr.s_addr;
92 }
93 
94 int
95 ipv4ll_subnetroute(rb_tree_t *routes, struct interface *ifp)
96 {
97 	struct ipv4ll_state *state;
98 	struct rt *rt;
99 	struct in_addr in;
100 
101 	assert(ifp != NULL);
102 	if ((state = IPV4LL_STATE(ifp)) == NULL ||
103 	    state->addr == NULL)
104 		return 0;
105 
106 	if ((rt = rt_new(ifp)) == NULL)
107 		return -1;
108 
109 	in.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr;
110 	sa_in_init(&rt->rt_dest, &in);
111 	in.s_addr = state->addr->mask.s_addr;
112 	sa_in_init(&rt->rt_netmask, &in);
113 	in.s_addr = INADDR_ANY;
114 	sa_in_init(&rt->rt_gateway, &in);
115 	sa_in_init(&rt->rt_ifa, &state->addr->addr);
116 	return rt_proto_add(routes, rt) ? 1 : 0;
117 }
118 
119 int
120 ipv4ll_defaultroute(rb_tree_t *routes, struct interface *ifp)
121 {
122 	struct ipv4ll_state *state;
123 	struct rt *rt;
124 	struct in_addr in;
125 
126 	assert(ifp != NULL);
127 	if ((state = IPV4LL_STATE(ifp)) == NULL ||
128 	    state->addr == NULL)
129 		return 0;
130 
131 	if ((rt = rt_new(ifp)) == NULL)
132 		return -1;
133 
134 	in.s_addr = INADDR_ANY;
135 	sa_in_init(&rt->rt_dest, &in);
136 	sa_in_init(&rt->rt_netmask, &in);
137 	sa_in_init(&rt->rt_gateway, &in);
138 	sa_in_init(&rt->rt_ifa, &state->addr->addr);
139 	return rt_proto_add(routes, rt) ? 1 : 0;
140 }
141 
142 ssize_t
143 ipv4ll_env(FILE *fp, const char *prefix, const struct interface *ifp)
144 {
145 	const struct ipv4ll_state *state;
146 	const char *pf = prefix == NULL ? "" : "_";
147 	struct in_addr netnum;
148 
149 	assert(ifp != NULL);
150 	if ((state = IPV4LL_CSTATE(ifp)) == NULL || state->addr == NULL)
151 		return 0;
152 
153 	/* Emulate a DHCP environment */
154 	if (efprintf(fp, "%s%sip_address=%s",
155 	    prefix, pf, inet_ntoa(state->addr->addr)) == -1)
156 		return -1;
157 	if (efprintf(fp, "%s%ssubnet_mask=%s",
158 	    prefix, pf, inet_ntoa(state->addr->mask)) == -1)
159 		return -1;
160 	if (efprintf(fp, "%s%ssubnet_cidr=%d",
161 	    prefix, pf, inet_ntocidr(state->addr->mask)) == -1)
162 		return -1;
163 	if (efprintf(fp, "%s%sbroadcast_address=%s",
164 	    prefix, pf, inet_ntoa(state->addr->brd)) == -1)
165 		return -1;
166 	netnum.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr;
167 	if (efprintf(fp, "%s%snetwork_number=%s",
168 	    prefix, pf, inet_ntoa(netnum)) == -1)
169 		return -1;
170 	return 5;
171 }
172 
173 static void
174 ipv4ll_announced_arp(struct arp_state *astate)
175 {
176 	struct ipv4ll_state *state = IPV4LL_STATE(astate->iface);
177 
178 	state->conflicts = 0;
179 #ifdef KERNEL_RFC5227
180 	arp_free(astate);
181 #endif
182 }
183 
184 static void
185 ipv4ll_arpfree(struct arp_state *astate)
186 {
187 	struct ipv4ll_state *state;
188 
189 	state = IPV4LL_STATE(astate->iface);
190 	if (state != NULL && state->arp == astate)
191 		state->arp = NULL;
192 }
193 
194 static void
195 ipv4ll_not_found(struct interface *ifp)
196 {
197 	struct ipv4ll_state *state;
198 	struct ipv4_addr *ia;
199 #ifdef KERNEL_RFC5227
200 	struct arp_state *astate;
201 	bool new_addr;
202 #endif
203 
204 	state = IPV4LL_STATE(ifp);
205 	assert(state != NULL);
206 
207 	ia = ipv4_iffindaddr(ifp, &state->pickedaddr, &inaddr_llmask);
208 #ifdef KERNEL_RFC5227
209 	new_addr = ia == NULL;
210 #endif
211 #ifdef IN_IFF_NOTREADY
212 	if (ia == NULL || ia->addr_flags & IN_IFF_NOTREADY)
213 #endif
214 		loginfox("%s: using IPv4LL address %s",
215 		  ifp->name, inet_ntoa(state->pickedaddr));
216 	if (ia == NULL) {
217 		if (ifp->ctx->options & DHCPCD_TEST)
218 			goto test;
219 		ia = ipv4_addaddr(ifp, &state->pickedaddr,
220 		    &inaddr_llmask, &inaddr_llbcast,
221 		    DHCP_INFINITE_LIFETIME, DHCP_INFINITE_LIFETIME);
222 	}
223 	if (ia == NULL)
224 		return;
225 #ifdef IN_IFF_NOTREADY
226 	if (ia->addr_flags & IN_IFF_NOTREADY)
227 		return;
228 	logdebugx("%s: DAD completed for %s", ifp->name, ia->saddr);
229 #endif
230 test:
231 	state->addr = ia;
232 	state->down = false;
233 	if (ifp->ctx->options & DHCPCD_TEST) {
234 		script_runreason(ifp, "TEST");
235 		eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS);
236 		return;
237 	}
238 	rt_build(ifp->ctx, AF_INET);
239 #ifdef KERNEL_RFC5227
240 	if (!new_addr) {
241 		astate = arp_new(ifp, &ia->addr);
242 		if (ifp->ctx->options & DHCPCD_FORKED)
243 			return;
244 		if (astate != NULL) {
245 			astate->announced_cb = ipv4ll_announced_arp;
246 			astate->free_cb = ipv4ll_arpfree;
247 			arp_announce(astate);
248 		}
249 	}
250 #else
251 	arp_announce(state->arp);
252 #endif
253 	script_runreason(ifp, "IPV4LL");
254 	dhcpcd_daemonise(ifp->ctx);
255 }
256 
257 static void
258 ipv4ll_startifp(void *arg)
259 {
260 	struct interface *ifp = arg;
261 	struct ipv4ll_state *state;
262 
263 	state = IPV4LL_STATE(ifp);
264 	ipv4ll_start1(ifp, state->arp);
265 }
266 
267 static void
268 ipv4ll_found(struct interface *ifp)
269 {
270 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
271 
272 	if (state->arp != NULL)
273 		arp_cancel(state->arp);
274 	if (++state->conflicts == MAX_CONFLICTS)
275 		logerrx("%s: failed to acquire an IPv4LL address",
276 		    ifp->name);
277 	state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp);
278 	eloop_timeout_add_sec(ifp->ctx->eloop,
279 	    state->conflicts >= MAX_CONFLICTS ?
280 	    RATE_LIMIT_INTERVAL : PROBE_WAIT,
281 	    ipv4ll_startifp, ifp);
282 }
283 
284 static void
285 ipv4ll_defend_failed(struct interface *ifp)
286 {
287 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
288 
289 	if (state->arp != NULL)
290 		arp_cancel(state->arp);
291 	ipv4_deladdr(state->addr, 1);
292 	state->down = true;
293 	state->addr = NULL;
294 	rt_build(ifp->ctx, AF_INET);
295 	script_runreason(ifp, "IPV4LL");
296 	state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp);
297 	ipv4ll_start1(ifp, state->arp);
298 }
299 
300 #ifndef KERNEL_RFC5227
301 static void
302 ipv4ll_not_found_arp(struct arp_state *astate)
303 {
304 	struct interface *ifp;
305 	struct ipv4ll_state *state;
306 
307 	assert(astate != NULL);
308 	assert(astate->iface != NULL);
309 
310 	ifp = astate->iface;
311 	state = IPV4LL_STATE(ifp);
312 	assert(state != NULL);
313 	assert(state->arp == astate);
314 	ipv4ll_not_found(ifp);
315 }
316 
317 static void
318 ipv4ll_found_arp(struct arp_state *astate, __unused const struct arp_msg *amsg)
319 {
320 	struct interface *ifp = astate->iface;
321 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
322 
323 	assert(state->arp == astate);
324 	ipv4ll_found(ifp);
325 }
326 
327 static void
328 ipv4ll_defend_failed_arp(struct arp_state *astate)
329 {
330 	struct ipv4ll_state *state = IPV4LL_STATE(astate->iface);
331 
332 	assert(state->arp == astate);
333 	ipv4ll_defend_failed(astate->iface);
334 }
335 #endif
336 
337 static void
338 ipv4ll_start1(struct interface *ifp, struct arp_state *astate)
339 {
340 	struct ipv4ll_state *state;
341 	struct ipv4_addr *ia;
342 	bool repick;
343 
344 	assert(ifp != NULL);
345 	if ((state = IPV4LL_STATE(ifp)) == NULL) {
346 		ifp->if_data[IF_DATA_IPV4LL] = calloc(1, sizeof(*state));
347 		if ((state = IPV4LL_STATE(ifp)) == NULL) {
348 			logerr(__func__);
349 			return;
350 		}
351 	}
352 
353 	/* RFC 3927 Section 2.1 states that the random number generator
354 	 * SHOULD be seeded with a value derived from persistent information
355 	 * such as the IEEE 802 MAC address so that it usually picks
356 	 * the same address without persistent storage. */
357 	if (!state->seeded) {
358 		unsigned int seed;
359 		char *orig;
360 
361 		if (sizeof(seed) > ifp->hwlen) {
362 			seed = 0;
363 			memcpy(&seed, ifp->hwaddr, ifp->hwlen);
364 		} else
365 			memcpy(&seed, ifp->hwaddr + ifp->hwlen - sizeof(seed),
366 			    sizeof(seed));
367 		/* coverity[dont_call] */
368 		orig = initstate(seed,
369 		    state->randomstate, sizeof(state->randomstate));
370 
371 		/* Save the original state. */
372 		if (ifp->ctx->randomstate == NULL)
373 			ifp->ctx->randomstate = orig;
374 
375 		/* Set back the original state until we need the seeded one. */
376 		setstate(ifp->ctx->randomstate);
377 		state->seeded = true;
378 	}
379 
380 #ifndef KERNEL_RFC5227
381 	if (astate == NULL) {
382 		if (state->arp != NULL)
383 			return;
384 		if ((astate = arp_new(ifp, NULL)) == NULL)
385 			return;
386 		astate->found_cb = ipv4ll_found_arp;
387 		astate->not_found_cb = ipv4ll_not_found_arp;
388 		astate->announced_cb = ipv4ll_announced_arp;
389 		astate->defend_failed_cb = ipv4ll_defend_failed_arp;
390 		astate->free_cb = ipv4ll_arpfree;
391 		state->arp = astate;
392 	} else
393 		assert(state->arp == astate);
394 #else
395 	UNUSED(astate);
396 #endif
397 
398 	state->down = true;
399 
400 	/* Find the previosuly used address. */
401 	if (state->pickedaddr.s_addr != INADDR_ANY)
402 		ia = ipv4_iffindaddr(ifp, &state->pickedaddr, NULL);
403 	else
404 		ia = NULL;
405 
406 	/* Find an existing IPv4LL address and ensure we can work with it. */
407 	if (ia == NULL)
408 		ia = ipv4_iffindlladdr(ifp);
409 
410 	repick = false;
411 #ifdef IN_IFF_DUPLICATED
412 	if (ia != NULL && ia->addr_flags & IN_IFF_DUPLICATED) {
413 		state->pickedaddr = ia->addr; /* So it's not picked again. */
414 		repick = true;
415 		ipv4_deladdr(ia, 0);
416 		ia = NULL;
417 	}
418 #endif
419 
420 	state->addr = ia;
421 	if (ia != NULL) {
422 		state->pickedaddr = ia->addr;
423 #ifndef KERNEL_RFC5227
424 		astate->addr = ia->addr;
425 #endif
426 #ifdef IN_IFF_TENTATIVE
427 		if (ia->addr_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) {
428 			loginfox("%s: waiting for DAD to complete on %s",
429 			    ifp->name, inet_ntoa(ia->addr));
430 			return;
431 		}
432 #endif
433 #ifdef IN_IFF_DUPLICATED
434 		loginfox("%s: using IPv4LL address %s", ifp->name, ia->saddr);
435 #endif
436 		ipv4ll_not_found(ifp);
437 		return;
438 	}
439 
440 	loginfox("%s: probing for an IPv4LL address", ifp->name);
441 	if (repick || state->pickedaddr.s_addr == INADDR_ANY)
442 		state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp);
443 #ifndef KERNEL_RFC5227
444 	astate->addr = state->pickedaddr;
445 #endif
446 #ifdef IN_IFF_DUPLICATED
447 	ipv4ll_not_found(ifp);
448 #else
449 	arp_probe(astate);
450 #endif
451 }
452 
453 void
454 ipv4ll_start(void *arg)
455 {
456 
457 	ipv4ll_start1(arg, NULL);
458 }
459 
460 static void
461 ipv4ll_freearp(struct interface *ifp)
462 {
463 	struct ipv4ll_state *state;
464 
465 	state = IPV4LL_STATE(ifp);
466 	if (state == NULL || state->arp == NULL)
467 		return;
468 
469 	eloop_timeout_delete(ifp->ctx->eloop, NULL, state->arp);
470 	arp_free(state->arp);
471 }
472 
473 void
474 ipv4ll_drop(struct interface *ifp)
475 {
476 	struct ipv4ll_state *state;
477 	bool dropped = false;
478 	struct ipv4_state *istate;
479 
480 	assert(ifp != NULL);
481 
482 	ipv4ll_freearp(ifp);
483 
484 	if ((ifp->options->options & DHCPCD_NODROP) == DHCPCD_NODROP)
485 		return;
486 
487 	state = IPV4LL_STATE(ifp);
488 	if (state && state->addr != NULL) {
489 		ipv4_deladdr(state->addr, 1);
490 		state->addr = NULL;
491 		dropped = true;
492 	}
493 
494 	/* Free any other link local addresses that might exist. */
495 	if ((istate = IPV4_STATE(ifp)) != NULL) {
496 		struct ipv4_addr *ia, *ian;
497 
498 		TAILQ_FOREACH_SAFE(ia, &istate->addrs, next, ian) {
499 			if (IN_LINKLOCAL(ntohl(ia->addr.s_addr))) {
500 				ipv4_deladdr(ia, 0);
501 				dropped = true;
502 			}
503 		}
504 	}
505 
506 	if (dropped) {
507 		rt_build(ifp->ctx, AF_INET);
508 		script_runreason(ifp, "IPV4LL");
509 	}
510 }
511 
512 void
513 ipv4ll_reset(struct interface *ifp)
514 {
515 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
516 
517 	if (state == NULL)
518 		return;
519 	state->pickedaddr.s_addr = INADDR_ANY;
520 	state->seeded = false;
521 }
522 
523 void
524 ipv4ll_free(struct interface *ifp)
525 {
526 
527 	assert(ifp != NULL);
528 
529 	ipv4ll_freearp(ifp);
530 	free(IPV4LL_STATE(ifp));
531 	ifp->if_data[IF_DATA_IPV4LL] = NULL;
532 }
533 
534 /* This may cause issues in BSD systems, where running as a single dhcpcd
535  * daemon would solve this issue easily. */
536 #ifdef HAVE_ROUTE_METRIC
537 int
538 ipv4ll_recvrt(__unused int cmd, const struct rt *rt)
539 {
540 	struct dhcpcd_ctx *ctx;
541 	struct interface *ifp;
542 
543 	/* Only interested in default route changes. */
544 	if (sa_is_unspecified(&rt->rt_dest))
545 		return 0;
546 
547 	/* If any interface is running IPv4LL, rebuild our routing table. */
548 	ctx = rt->rt_ifp->ctx;
549 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
550 		if (IPV4LL_STATE_RUNNING(ifp)) {
551 			rt_build(ctx, AF_INET);
552 			break;
553 		}
554 	}
555 
556 	return 0;
557 }
558 #endif
559 
560 struct ipv4_addr *
561 ipv4ll_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid)
562 {
563 	struct interface *ifp;
564 	struct ipv4ll_state *state;
565 
566 	ifp = ia->iface;
567 	state = IPV4LL_STATE(ifp);
568 	if (state == NULL)
569 		return ia;
570 
571 	if (cmd == RTM_DELADDR &&
572 	    state->addr != NULL &&
573 	    IN_ARE_ADDR_EQUAL(&state->addr->addr, &ia->addr))
574 	{
575 		loginfox("%s: pid %d deleted IP address %s",
576 		    ifp->name, pid, ia->saddr);
577 		ipv4ll_defend_failed(ifp);
578 		return ia;
579 	}
580 
581 #ifdef IN_IFF_DUPLICATED
582 	if (cmd != RTM_NEWADDR)
583 		return ia;
584 	if (!IN_ARE_ADDR_EQUAL(&state->pickedaddr, &ia->addr))
585 		return ia;
586 	if (!(ia->addr_flags & IN_IFF_NOTUSEABLE))
587 		ipv4ll_not_found(ifp);
588 	else if (ia->addr_flags & IN_IFF_DUPLICATED) {
589 		logerrx("%s: DAD detected %s", ifp->name, ia->saddr);
590 #ifdef KERNEL_RFC5227
591 		arp_freeaddr(ifp, &ia->addr);
592 #endif
593 		ipv4_deladdr(ia, 1);
594 		state->addr = NULL;
595 		rt_build(ifp->ctx, AF_INET);
596 		ipv4ll_found(ifp);
597 		return NULL;
598 	}
599 #endif
600 
601 	return ia;
602 }
603