xref: /dragonfly/contrib/dhcpcd/src/ipv4ll.c (revision 88ed2a5c)
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 <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 6
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 #endif
202 
203 	state = IPV4LL_STATE(ifp);
204 	assert(state != NULL);
205 
206 	ia = ipv4_iffindaddr(ifp, &state->pickedaddr, &inaddr_llmask);
207 #ifdef IN_IFF_NOTREADY
208 	if (ia == NULL || ia->addr_flags & IN_IFF_NOTREADY)
209 #endif
210 		loginfox("%s: using IPv4LL address %s",
211 		  ifp->name, inet_ntoa(state->pickedaddr));
212 	if (ia == NULL) {
213 		if (ifp->ctx->options & DHCPCD_TEST)
214 			goto test;
215 		ia = ipv4_addaddr(ifp, &state->pickedaddr,
216 		    &inaddr_llmask, &inaddr_llbcast,
217 		    DHCP_INFINITE_LIFETIME, DHCP_INFINITE_LIFETIME);
218 	}
219 	if (ia == NULL)
220 		return;
221 #ifdef IN_IFF_NOTREADY
222 	if (ia->addr_flags & IN_IFF_NOTREADY)
223 		return;
224 	logdebugx("%s: DAD completed for %s", ifp->name, ia->saddr);
225 #endif
226 test:
227 	state->addr = ia;
228 	state->down = false;
229 	if (ifp->ctx->options & DHCPCD_TEST) {
230 		script_runreason(ifp, "TEST");
231 		eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS);
232 		return;
233 	}
234 	rt_build(ifp->ctx, AF_INET);
235 #ifdef KERNEL_RFC5227
236 	astate = arp_new(ifp, &ia->addr);
237 	if (astate != NULL) {
238 		astate->announced_cb = ipv4ll_announced_arp;
239 		astate->free_cb = ipv4ll_arpfree;
240 		arp_announce(astate);
241 	}
242 #else
243 	arp_announce(state->arp);
244 #endif
245 	script_runreason(ifp, "IPV4LL");
246 	dhcpcd_daemonise(ifp->ctx);
247 }
248 
249 static void
250 ipv4ll_startifp(void *arg)
251 {
252 	struct interface *ifp = arg;
253 	struct ipv4ll_state *state;
254 
255 	state = IPV4LL_STATE(ifp);
256 	ipv4ll_start1(ifp, state->arp);
257 }
258 
259 static void
260 ipv4ll_found(struct interface *ifp)
261 {
262 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
263 
264 	arp_cancel(state->arp);
265 	if (++state->conflicts == MAX_CONFLICTS)
266 		logerr("%s: failed to acquire an IPv4LL address",
267 		    ifp->name);
268 	eloop_timeout_add_sec(ifp->ctx->eloop,
269 	    state->conflicts >= MAX_CONFLICTS ?
270 	    RATE_LIMIT_INTERVAL : PROBE_WAIT,
271 	    ipv4ll_startifp, ifp);
272 }
273 
274 static void
275 ipv4ll_defend_failed(struct interface *ifp)
276 {
277 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
278 
279 	ipv4_deladdr(state->addr, 1);
280 	state->down = true;
281 	state->addr = NULL;
282 	rt_build(ifp->ctx, AF_INET);
283 	script_runreason(ifp, "IPV4LL");
284 	ipv4ll_start1(ifp, state->arp);
285 }
286 
287 #ifndef KERNEL_RFC5227
288 static void
289 ipv4ll_not_found_arp(struct arp_state *astate)
290 {
291 	struct interface *ifp;
292 	struct ipv4ll_state *state;
293 
294 	assert(astate != NULL);
295 	assert(astate->iface != NULL);
296 
297 	ifp = astate->iface;
298 	state = IPV4LL_STATE(ifp);
299 	assert(state != NULL);
300 	assert(state->arp == astate);
301 	ipv4ll_not_found(ifp);
302 }
303 
304 static void
305 ipv4ll_found_arp(struct arp_state *astate, __unused const struct arp_msg *amsg)
306 {
307 	struct interface *ifp = astate->iface;
308 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
309 
310 	assert(state->arp == astate);
311 	ipv4ll_found(ifp);
312 }
313 
314 static void
315 ipv4ll_defend_failed_arp(struct arp_state *astate)
316 {
317 	struct ipv4ll_state *state = IPV4LL_STATE(astate->iface);
318 
319 	assert(state->arp == astate);
320 	ipv4ll_defend_failed(astate->iface);
321 }
322 #endif
323 
324 static void
325 ipv4ll_start1(struct interface *ifp, struct arp_state *astate)
326 {
327 	struct ipv4ll_state *state;
328 	struct ipv4_addr *ia;
329 	bool repick;
330 
331 	assert(ifp != NULL);
332 	if ((state = IPV4LL_STATE(ifp)) == NULL) {
333 		ifp->if_data[IF_DATA_IPV4LL] = calloc(1, sizeof(*state));
334 		if ((state = IPV4LL_STATE(ifp)) == NULL) {
335 			logerr(__func__);
336 			return;
337 		}
338 	}
339 
340 	/* RFC 3927 Section 2.1 states that the random number generator
341 	 * SHOULD be seeded with a value derived from persistent information
342 	 * such as the IEEE 802 MAC address so that it usually picks
343 	 * the same address without persistent storage. */
344 	if (!state->seeded) {
345 		unsigned int seed;
346 		char *orig;
347 
348 		if (sizeof(seed) > ifp->hwlen) {
349 			seed = 0;
350 			memcpy(&seed, ifp->hwaddr, ifp->hwlen);
351 		} else
352 			memcpy(&seed, ifp->hwaddr + ifp->hwlen - sizeof(seed),
353 			    sizeof(seed));
354 		/* coverity[dont_call] */
355 		orig = initstate(seed,
356 		    state->randomstate, sizeof(state->randomstate));
357 
358 		/* Save the original state. */
359 		if (ifp->ctx->randomstate == NULL)
360 			ifp->ctx->randomstate = orig;
361 
362 		/* Set back the original state until we need the seeded one. */
363 		setstate(ifp->ctx->randomstate);
364 		state->seeded = true;
365 	}
366 
367 #ifndef KERNEL_RFC5227
368 	if (astate == NULL) {
369 		if (state->arp != NULL)
370 			return;
371 		if ((astate = arp_new(ifp, NULL)) == NULL)
372 			return;
373 		astate->found_cb = ipv4ll_found_arp;
374 		astate->not_found_cb = ipv4ll_not_found_arp;
375 		astate->announced_cb = ipv4ll_announced_arp;
376 		astate->defend_failed_cb = ipv4ll_defend_failed_arp;
377 		astate->free_cb = ipv4ll_arpfree;
378 		state->arp = astate;
379 	} else
380 		assert(state->arp == astate);
381 #else
382 	UNUSED(astate);
383 #endif
384 
385 	state->down = true;
386 
387 	/* Find the previosuly used address. */
388 	if (state->pickedaddr.s_addr != INADDR_ANY)
389 		ia = ipv4_iffindaddr(ifp, &state->pickedaddr, NULL);
390 	else
391 		ia = NULL;
392 
393 	/* Find an existing IPv4LL address and ensure we can work with it. */
394 	if (ia == NULL)
395 		ia = ipv4_iffindlladdr(ifp);
396 
397 	repick = false;
398 #ifdef IN_IFF_DUPLICATED
399 	if (ia != NULL && ia->addr_flags & IN_IFF_DUPLICATED) {
400 		state->pickedaddr = ia->addr; /* So it's not picked again. */
401 		repick = true;
402 		ipv4_deladdr(ia, 0);
403 		ia = NULL;
404 	}
405 #endif
406 
407 	state->addr = ia;
408 	if (ia != NULL) {
409 		state->pickedaddr = ia->addr;
410 #ifndef KERNEL_RFC5227
411 		astate->addr = ia->addr;
412 #endif
413 #ifdef IN_IFF_TENTATIVE
414 		if (ia->addr_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) {
415 			loginfox("%s: waiting for DAD to complete on %s",
416 			    ifp->name, inet_ntoa(ia->addr));
417 			return;
418 		}
419 #endif
420 #ifdef IN_IFF_DUPLICATED
421 		loginfox("%s: using IPv4LL address %s", ifp->name, ia->saddr);
422 #endif
423 		ipv4ll_not_found(ifp);
424 		return;
425 	}
426 
427 	loginfox("%s: probing for an IPv4LL address", ifp->name);
428 	if (repick || state->pickedaddr.s_addr == INADDR_ANY)
429 		state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp);
430 #ifndef KERNEL_RFC5227
431 	astate->addr = state->pickedaddr;
432 #endif
433 #ifdef IN_IFF_DUPLICATED
434 	ipv4ll_not_found(ifp);
435 #else
436 	arp_probe(astate);
437 #endif
438 }
439 
440 void
441 ipv4ll_start(void *arg)
442 {
443 
444 	ipv4ll_start1(arg, NULL);
445 }
446 
447 static void
448 ipv4ll_freearp(struct interface *ifp)
449 {
450 	struct ipv4ll_state *state;
451 
452 	state = IPV4LL_STATE(ifp);
453 	if (state == NULL || state->arp == NULL)
454 		return;
455 
456 	eloop_timeout_delete(ifp->ctx->eloop, NULL, state->arp);
457 	arp_free(state->arp);
458 }
459 
460 void
461 ipv4ll_drop(struct interface *ifp)
462 {
463 	struct ipv4ll_state *state;
464 	bool dropped = false;
465 	struct ipv4_state *istate;
466 
467 	assert(ifp != NULL);
468 
469 	ipv4ll_freearp(ifp);
470 
471 	if ((ifp->options->options & DHCPCD_NODROP) == DHCPCD_NODROP)
472 		return;
473 
474 	state = IPV4LL_STATE(ifp);
475 	if (state && state->addr != NULL) {
476 		ipv4_deladdr(state->addr, 1);
477 		state->addr = NULL;
478 		dropped = true;
479 	}
480 
481 	/* Free any other link local addresses that might exist. */
482 	if ((istate = IPV4_STATE(ifp)) != NULL) {
483 		struct ipv4_addr *ia, *ian;
484 
485 		TAILQ_FOREACH_SAFE(ia, &istate->addrs, next, ian) {
486 			if (IN_LINKLOCAL(ntohl(ia->addr.s_addr))) {
487 				ipv4_deladdr(ia, 0);
488 				dropped = true;
489 			}
490 		}
491 	}
492 
493 	if (dropped) {
494 		rt_build(ifp->ctx, AF_INET);
495 		script_runreason(ifp, "IPV4LL");
496 	}
497 }
498 
499 void
500 ipv4ll_reset(struct interface *ifp)
501 {
502 	struct ipv4ll_state *state = IPV4LL_STATE(ifp);
503 
504 	if (state == NULL)
505 		return;
506 	state->pickedaddr.s_addr = INADDR_ANY;
507 	state->seeded = false;
508 }
509 
510 void
511 ipv4ll_free(struct interface *ifp)
512 {
513 
514 	assert(ifp != NULL);
515 
516 	ipv4ll_freearp(ifp);
517 	free(IPV4LL_STATE(ifp));
518 	ifp->if_data[IF_DATA_IPV4LL] = NULL;
519 }
520 
521 /* This may cause issues in BSD systems, where running as a single dhcpcd
522  * daemon would solve this issue easily. */
523 #ifdef HAVE_ROUTE_METRIC
524 int
525 ipv4ll_recvrt(__unused int cmd, const struct rt *rt)
526 {
527 	struct dhcpcd_ctx *ctx;
528 	struct interface *ifp;
529 
530 	/* Only interested in default route changes. */
531 	if (sa_is_unspecified(&rt->rt_dest))
532 		return 0;
533 
534 	/* If any interface is running IPv4LL, rebuild our routing table. */
535 	ctx = rt->rt_ifp->ctx;
536 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
537 		if (IPV4LL_STATE_RUNNING(ifp)) {
538 			rt_build(ctx, AF_INET);
539 			break;
540 		}
541 	}
542 
543 	return 0;
544 }
545 #endif
546 
547 void
548 ipv4ll_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid)
549 {
550 	struct interface *ifp;
551 	struct ipv4ll_state *state;
552 
553 	ifp = ia->iface;
554 	state = IPV4LL_STATE(ifp);
555 	if (state == NULL)
556 		return;
557 
558 	if (cmd == RTM_DELADDR &&
559 	    state->addr != NULL &&
560 	    IN_ARE_ADDR_EQUAL(&state->addr->addr, &ia->addr))
561 	{
562 		loginfox("%s: pid %d deleted IP address %s",
563 		    ifp->name, pid, ia->saddr);
564 		ipv4ll_defend_failed(ifp);
565 		return;
566 	}
567 
568 #ifdef IN_IFF_DUPLICATED
569 	if (cmd != RTM_NEWADDR)
570 		return;
571 	if (!IN_ARE_ADDR_EQUAL(&state->pickedaddr, &ia->addr))
572 		return;
573 	if (!(ia->addr_flags & IN_IFF_NOTUSEABLE))
574 		ipv4ll_not_found(ifp);
575 	else if (ia->addr_flags & IN_IFF_DUPLICATED) {
576 		logerrx("%s: DAD detected %s", ifp->name, ia->saddr);
577 		ipv4_deladdr(state->addr, 1);
578 		ipv4ll_found(ifp);
579 	}
580 #endif
581 }
582