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