1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <arpa/inet.h>
39 #define ELOOP_QUEUE IPV4LL
46 #include "if-options.h"
53 static const struct in_addr inaddr_llmask
= {
54 .s_addr
= HTONL(LINKLOCAL_MASK
)
56 static const struct in_addr inaddr_llbcast
= {
57 .s_addr
= HTONL(LINKLOCAL_BCAST
)
61 ipv4ll_pickaddr(struct interface
*ifp
)
63 struct in_addr addr
= { .s_addr
= 0 };
64 struct ipv4ll_state
*state
;
66 state
= IPV4LL_STATE(ifp
);
67 setstate(state
->randomstate
);
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] */
78 addr
.s_addr
= ntohl(LINKLOCAL_ADDR
|
79 ((uint32_t)(r
% 0xFD00) + 0x0100));
81 /* No point using a failed address */
82 if (IN_ARE_ADDR_EQUAL(&addr
, &state
->pickedaddr
))
84 /* Ensure we don't have the address on another interface */
85 } while (ipv4_findaddr(ifp
->ctx
, &addr
) != NULL
);
87 /* Restore the original random state */
88 setstate(ifp
->ctx
->randomstate
);
89 state
->pickedaddr
= addr
;
93 ipv4ll_subnetroute(rb_tree_t
*routes
, struct interface
*ifp
)
95 struct ipv4ll_state
*state
;
100 if ((state
= IPV4LL_STATE(ifp
)) == NULL
||
104 if ((rt
= rt_new(ifp
)) == NULL
)
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 rt
->rt_dflags
|= RTDF_IPV4LL
;
115 return rt_proto_add(routes
, rt
) ? 1 : 0;
119 ipv4ll_defaultroute(rb_tree_t
*routes
, struct interface
*ifp
)
121 struct ipv4ll_state
*state
;
126 if ((state
= IPV4LL_STATE(ifp
)) == NULL
||
130 if ((rt
= rt_new(ifp
)) == NULL
)
133 in
.s_addr
= INADDR_ANY
;
134 sa_in_init(&rt
->rt_dest
, &in
);
135 sa_in_init(&rt
->rt_netmask
, &in
);
136 sa_in_init(&rt
->rt_gateway
, &in
);
137 sa_in_init(&rt
->rt_ifa
, &state
->addr
->addr
);
138 rt
->rt_dflags
|= RTDF_IPV4LL
;
139 #ifdef HAVE_ROUTE_METRIC
140 rt
->rt_metric
+= 10000;
142 return rt_proto_add(routes
, rt
) ? 1 : 0;
146 ipv4ll_env(FILE *fp
, const char *prefix
, const struct interface
*ifp
)
148 const struct ipv4ll_state
*state
;
149 const char *pf
= prefix
== NULL
? "" : "_";
150 struct in_addr netnum
;
153 if ((state
= IPV4LL_CSTATE(ifp
)) == NULL
|| state
->addr
== NULL
)
156 /* Emulate a DHCP environment */
157 if (efprintf(fp
, "%s%sip_address=%s",
158 prefix
, pf
, inet_ntoa(state
->addr
->addr
)) == -1)
160 if (efprintf(fp
, "%s%ssubnet_mask=%s",
161 prefix
, pf
, inet_ntoa(state
->addr
->mask
)) == -1)
163 if (efprintf(fp
, "%s%ssubnet_cidr=%d",
164 prefix
, pf
, inet_ntocidr(state
->addr
->mask
)) == -1)
166 if (efprintf(fp
, "%s%sbroadcast_address=%s",
167 prefix
, pf
, inet_ntoa(state
->addr
->brd
)) == -1)
169 netnum
.s_addr
= state
->addr
->addr
.s_addr
& state
->addr
->mask
.s_addr
;
170 if (efprintf(fp
, "%s%snetwork_number=%s",
171 prefix
, pf
, inet_ntoa(netnum
)) == -1)
177 ipv4ll_announced_arp(struct arp_state
*astate
)
179 struct ipv4ll_state
*state
= IPV4LL_STATE(astate
->iface
);
181 state
->conflicts
= 0;
182 #ifdef KERNEL_RFC5227
187 #ifndef KERNEL_RFC5227
188 /* This is the callback by ARP freeing */
190 ipv4ll_free_arp(struct arp_state
*astate
)
192 struct ipv4ll_state
*state
;
194 state
= IPV4LL_STATE(astate
->iface
);
195 if (state
!= NULL
&& state
->arp
== astate
)
199 /* This is us freeing any ARP state */
201 ipv4ll_freearp(struct interface
*ifp
)
203 struct ipv4ll_state
*state
;
205 state
= IPV4LL_STATE(ifp
);
206 if (state
== NULL
|| state
->arp
== NULL
)
209 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, state
->arp
);
210 arp_free(state
->arp
);
214 #define ipv4ll_freearp(ifp)
218 ipv4ll_not_found(struct interface
*ifp
)
220 struct ipv4ll_state
*state
;
221 struct ipv4_addr
*ia
;
222 struct arp_state
*astate
;
224 state
= IPV4LL_STATE(ifp
);
225 ia
= ipv4_iffindaddr(ifp
, &state
->pickedaddr
, &inaddr_llmask
);
226 #ifdef IN_IFF_NOTREADY
227 if (ia
== NULL
|| ia
->addr_flags
& IN_IFF_NOTREADY
)
229 loginfox("%s: using IPv4LL address %s",
230 ifp
->name
, inet_ntoa(state
->pickedaddr
));
231 if (!(ifp
->options
->options
& DHCPCD_CONFIGURE
))
234 if (ifp
->ctx
->options
& DHCPCD_TEST
)
236 ia
= ipv4_addaddr(ifp
, &state
->pickedaddr
,
237 &inaddr_llmask
, &inaddr_llbcast
,
238 DHCP_INFINITE_LIFETIME
, DHCP_INFINITE_LIFETIME
);
242 #ifdef IN_IFF_NOTREADY
243 if (ia
->addr_flags
& IN_IFF_NOTREADY
)
245 logdebugx("%s: DAD completed for %s", ifp
->name
, ia
->saddr
);
251 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
252 script_runreason(ifp
, "TEST");
253 eloop_exit(ifp
->ctx
->eloop
, EXIT_SUCCESS
);
256 rt_build(ifp
->ctx
, AF_INET
);
258 astate
= arp_announceaddr(ifp
->ctx
, &ia
->addr
);
260 astate
->announced_cb
= ipv4ll_announced_arp
;
261 script_runreason(ifp
, "IPV4LL");
262 dhcpcd_daemonise(ifp
->ctx
);
266 ipv4ll_found(struct interface
*ifp
)
268 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
271 if (++state
->conflicts
== MAX_CONFLICTS
)
272 logerrx("%s: failed to acquire an IPv4LL address",
274 ipv4ll_pickaddr(ifp
);
275 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
276 state
->conflicts
>= MAX_CONFLICTS
?
277 RATE_LIMIT_INTERVAL
: PROBE_WAIT
,
282 ipv4ll_defend_failed(struct interface
*ifp
)
284 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
287 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
288 ipv4_deladdr(state
->addr
, 1);
290 rt_build(ifp
->ctx
, AF_INET
);
291 script_runreason(ifp
, "IPV4LL");
292 ipv4ll_pickaddr(ifp
);
296 #ifndef KERNEL_RFC5227
298 ipv4ll_not_found_arp(struct arp_state
*astate
)
301 ipv4ll_not_found(astate
->iface
);
305 ipv4ll_found_arp(struct arp_state
*astate
, __unused
const struct arp_msg
*amsg
)
308 ipv4ll_found(astate
->iface
);
312 ipv4ll_defend_failed_arp(struct arp_state
*astate
)
315 ipv4ll_defend_failed(astate
->iface
);
320 ipv4ll_start(void *arg
)
322 struct interface
*ifp
= arg
;
323 struct ipv4ll_state
*state
;
324 struct ipv4_addr
*ia
;
326 #ifndef KERNEL_RFC5227
327 struct arp_state
*astate
;
330 if ((state
= IPV4LL_STATE(ifp
)) == NULL
) {
331 ifp
->if_data
[IF_DATA_IPV4LL
] = calloc(1, sizeof(*state
));
332 if ((state
= IPV4LL_STATE(ifp
)) == NULL
) {
338 /* RFC 3927 Section 2.1 states that the random number generator
339 * SHOULD be seeded with a value derived from persistent information
340 * such as the IEEE 802 MAC address so that it usually picks
341 * the same address without persistent storage. */
342 if (!state
->seeded
) {
346 if (sizeof(seed
) > ifp
->hwlen
) {
348 memcpy(&seed
, ifp
->hwaddr
, ifp
->hwlen
);
350 memcpy(&seed
, ifp
->hwaddr
+ ifp
->hwlen
- sizeof(seed
),
352 /* coverity[dont_call] */
353 orig
= initstate(seed
,
354 state
->randomstate
, sizeof(state
->randomstate
));
356 /* Save the original state. */
357 if (ifp
->ctx
->randomstate
== NULL
)
358 ifp
->ctx
->randomstate
= orig
;
360 /* Set back the original state until we need the seeded one. */
361 setstate(ifp
->ctx
->randomstate
);
362 state
->seeded
= true;
365 /* Find the previosuly used address. */
366 if (state
->pickedaddr
.s_addr
!= INADDR_ANY
)
367 ia
= ipv4_iffindaddr(ifp
, &state
->pickedaddr
, NULL
);
371 /* Find an existing IPv4LL address and ensure we can work with it. */
373 ia
= ipv4_iffindlladdr(ifp
);
376 #ifdef IN_IFF_DUPLICATED
377 if (ia
!= NULL
&& ia
->addr_flags
& IN_IFF_DUPLICATED
) {
378 state
->pickedaddr
= ia
->addr
; /* So it's not picked again. */
380 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
389 state
->pickedaddr
= ia
->addr
;
390 #ifdef IN_IFF_TENTATIVE
391 if (ia
->addr_flags
& (IN_IFF_TENTATIVE
| IN_IFF_DETACHED
)) {
392 loginfox("%s: waiting for DAD to complete on %s",
393 ifp
->name
, inet_ntoa(ia
->addr
));
397 #ifdef IN_IFF_DUPLICATED
398 loginfox("%s: using IPv4LL address %s", ifp
->name
, ia
->saddr
);
401 loginfox("%s: probing for an IPv4LL address", ifp
->name
);
402 if (repick
|| state
->pickedaddr
.s_addr
== INADDR_ANY
)
403 ipv4ll_pickaddr(ifp
);
406 #ifdef KERNEL_RFC5227
407 ipv4ll_not_found(ifp
);
410 state
->arp
= astate
= arp_new(ifp
, &state
->pickedaddr
);
411 if (state
->arp
== NULL
)
414 astate
->found_cb
= ipv4ll_found_arp
;
415 astate
->not_found_cb
= ipv4ll_not_found_arp
;
416 astate
->announced_cb
= ipv4ll_announced_arp
;
417 astate
->defend_failed_cb
= ipv4ll_defend_failed_arp
;
418 astate
->free_cb
= ipv4ll_free_arp
;
424 ipv4ll_drop(struct interface
*ifp
)
426 struct ipv4ll_state
*state
;
427 bool dropped
= false;
428 struct ipv4_state
*istate
;
434 if ((ifp
->options
->options
& DHCPCD_NODROP
) == DHCPCD_NODROP
)
437 state
= IPV4LL_STATE(ifp
);
438 if (state
&& state
->addr
!= NULL
) {
439 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
440 ipv4_deladdr(state
->addr
, 1);
445 /* Free any other link local addresses that might exist. */
446 if ((istate
= IPV4_STATE(ifp
)) != NULL
) {
447 struct ipv4_addr
*ia
, *ian
;
449 TAILQ_FOREACH_SAFE(ia
, &istate
->addrs
, next
, ian
) {
450 if (IN_LINKLOCAL(ntohl(ia
->addr
.s_addr
))) {
451 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
459 rt_build(ifp
->ctx
, AF_INET
);
460 script_runreason(ifp
, "IPV4LL");
465 ipv4ll_reset(struct interface
*ifp
)
467 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
472 state
->pickedaddr
.s_addr
= INADDR_ANY
;
473 state
->seeded
= false;
477 ipv4ll_free(struct interface
*ifp
)
483 free(IPV4LL_STATE(ifp
));
484 ifp
->if_data
[IF_DATA_IPV4LL
] = NULL
;
487 /* This may cause issues in BSD systems, where running as a single dhcpcd
488 * daemon would solve this issue easily. */
489 #ifdef HAVE_ROUTE_METRIC
491 ipv4ll_recvrt(__unused
int cmd
, const struct rt
*rt
)
493 struct dhcpcd_ctx
*ctx
;
494 struct interface
*ifp
;
496 /* Only interested in default route changes. */
497 if (sa_is_unspecified(&rt
->rt_dest
))
500 /* If any interface is running IPv4LL, rebuild our routing table. */
501 ctx
= rt
->rt_ifp
->ctx
;
502 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
503 if (IPV4LL_STATE_RUNNING(ifp
)) {
504 rt_build(ctx
, AF_INET
);
514 ipv4ll_handleifa(int cmd
, struct ipv4_addr
*ia
, pid_t pid
)
516 struct interface
*ifp
;
517 struct ipv4ll_state
*state
;
520 state
= IPV4LL_STATE(ifp
);
524 if (cmd
== RTM_DELADDR
&&
525 state
->addr
!= NULL
&&
526 IN_ARE_ADDR_EQUAL(&state
->addr
->addr
, &ia
->addr
))
528 loginfox("%s: pid %d deleted IP address %s",
529 ifp
->name
, pid
, ia
->saddr
);
530 ipv4ll_defend_failed(ifp
);
534 #ifdef IN_IFF_DUPLICATED
535 if (cmd
!= RTM_NEWADDR
)
537 if (!IN_ARE_ADDR_EQUAL(&state
->pickedaddr
, &ia
->addr
))
539 if (!(ia
->addr_flags
& IN_IFF_NOTUSEABLE
))
540 ipv4ll_not_found(ifp
);
541 else if (ia
->addr_flags
& IN_IFF_DUPLICATED
) {
542 logerrx("%s: DAD detected %s", ifp
->name
, ia
->saddr
);
544 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
547 rt_build(ifp
->ctx
, AF_INET
);