1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2019 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>
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
)
60 static void ipv4ll_start1(struct interface
*, struct arp_state
*);
63 ipv4ll_pickaddr(struct interface
*ifp
)
66 struct ipv4ll_state
*state
;
68 state
= IPV4LL_STATE(ifp
);
69 setstate(state
->randomstate
);
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] */
80 addr
.s_addr
= ntohl(LINKLOCAL_ADDR
|
81 ((uint32_t)(r
% 0xFD00) + 0x0100));
83 /* No point using a failed address */
84 if (IN_ARE_ADDR_EQUAL(&addr
, &state
->pickedaddr
))
86 /* Ensure we don't have the address on another interface */
87 } while (ipv4_findaddr(ifp
->ctx
, &addr
) != NULL
);
89 /* Restore the original random state */
90 setstate(ifp
->ctx
->randomstate
);
95 ipv4ll_subnetroute(rb_tree_t
*routes
, struct interface
*ifp
)
97 struct ipv4ll_state
*state
;
102 if ((state
= IPV4LL_STATE(ifp
)) == NULL
||
106 if ((rt
= rt_new(ifp
)) == NULL
)
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;
120 ipv4ll_defaultroute(rb_tree_t
*routes
, struct interface
*ifp
)
122 struct ipv4ll_state
*state
;
127 if ((state
= IPV4LL_STATE(ifp
)) == NULL
||
131 if ((rt
= rt_new(ifp
)) == NULL
)
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;
143 ipv4ll_env(FILE *fp
, const char *prefix
, const struct interface
*ifp
)
145 const struct ipv4ll_state
*state
;
146 const char *pf
= prefix
== NULL
? "" : "_";
147 struct in_addr netnum
;
150 if ((state
= IPV4LL_CSTATE(ifp
)) == NULL
|| state
->addr
== NULL
)
153 /* Emulate a DHCP environment */
154 if (efprintf(fp
, "%s%sip_address=%s",
155 prefix
, pf
, inet_ntoa(state
->addr
->addr
)) == -1)
157 if (efprintf(fp
, "%s%ssubnet_mask=%s",
158 prefix
, pf
, inet_ntoa(state
->addr
->mask
)) == -1)
160 if (efprintf(fp
, "%s%ssubnet_cidr=%d",
161 prefix
, pf
, inet_ntocidr(state
->addr
->mask
)) == -1)
163 if (efprintf(fp
, "%s%sbroadcast_address=%s",
164 prefix
, pf
, inet_ntoa(state
->addr
->brd
)) == -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)
174 ipv4ll_announced_arp(struct arp_state
*astate
)
176 struct ipv4ll_state
*state
= IPV4LL_STATE(astate
->iface
);
178 state
->conflicts
= 0;
179 #ifdef KERNEL_RFC5227
185 ipv4ll_arpfree(struct arp_state
*astate
)
187 struct ipv4ll_state
*state
;
189 state
= IPV4LL_STATE(astate
->iface
);
190 if (state
!= NULL
&& state
->arp
== astate
)
195 ipv4ll_not_found(struct interface
*ifp
)
197 struct ipv4ll_state
*state
;
198 struct ipv4_addr
*ia
;
199 #ifdef KERNEL_RFC5227
200 struct arp_state
*astate
;
203 state
= IPV4LL_STATE(ifp
);
204 assert(state
!= NULL
);
206 ia
= ipv4_iffindaddr(ifp
, &state
->pickedaddr
, &inaddr_llmask
);
207 #ifdef IN_IFF_NOTREADY
208 if (ia
== NULL
|| ia
->addr_flags
& IN_IFF_NOTREADY
)
210 loginfox("%s: using IPv4LL address %s",
211 ifp
->name
, inet_ntoa(state
->pickedaddr
));
213 if (ifp
->ctx
->options
& DHCPCD_TEST
)
215 ia
= ipv4_addaddr(ifp
, &state
->pickedaddr
,
216 &inaddr_llmask
, &inaddr_llbcast
,
217 DHCP_INFINITE_LIFETIME
, DHCP_INFINITE_LIFETIME
);
221 #ifdef IN_IFF_NOTREADY
222 if (ia
->addr_flags
& IN_IFF_NOTREADY
)
224 logdebugx("%s: DAD completed for %s", ifp
->name
, ia
->saddr
);
229 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
230 script_runreason(ifp
, "TEST");
231 eloop_exit(ifp
->ctx
->eloop
, EXIT_SUCCESS
);
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
);
243 arp_announce(state
->arp
);
245 script_runreason(ifp
, "IPV4LL");
246 dhcpcd_daemonise(ifp
->ctx
);
250 ipv4ll_startifp(void *arg
)
252 struct interface
*ifp
= arg
;
253 struct ipv4ll_state
*state
;
255 state
= IPV4LL_STATE(ifp
);
256 ipv4ll_start1(ifp
, state
->arp
);
260 ipv4ll_found(struct interface
*ifp
)
262 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
264 arp_cancel(state
->arp
);
265 if (++state
->conflicts
== MAX_CONFLICTS
)
266 logerr("%s: failed to acquire an IPv4LL address",
268 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
269 state
->conflicts
>= MAX_CONFLICTS
?
270 RATE_LIMIT_INTERVAL
: PROBE_WAIT
,
271 ipv4ll_startifp
, ifp
);
275 ipv4ll_defend_failed(struct interface
*ifp
)
277 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
279 ipv4_deladdr(state
->addr
, 1);
282 rt_build(ifp
->ctx
, AF_INET
);
283 script_runreason(ifp
, "IPV4LL");
284 ipv4ll_start1(ifp
, state
->arp
);
287 #ifndef KERNEL_RFC5227
289 ipv4ll_not_found_arp(struct arp_state
*astate
)
291 struct interface
*ifp
;
292 struct ipv4ll_state
*state
;
294 assert(astate
!= NULL
);
295 assert(astate
->iface
!= NULL
);
298 state
= IPV4LL_STATE(ifp
);
299 assert(state
!= NULL
);
300 assert(state
->arp
== astate
);
301 ipv4ll_not_found(ifp
);
305 ipv4ll_found_arp(struct arp_state
*astate
, __unused
const struct arp_msg
*amsg
)
307 struct interface
*ifp
= astate
->iface
;
308 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
310 assert(state
->arp
== astate
);
315 ipv4ll_defend_failed_arp(struct arp_state
*astate
)
317 struct ipv4ll_state
*state
= IPV4LL_STATE(astate
->iface
);
319 assert(state
->arp
== astate
);
320 ipv4ll_defend_failed(astate
->iface
);
325 ipv4ll_start1(struct interface
*ifp
, struct arp_state
*astate
)
327 struct ipv4ll_state
*state
;
328 struct ipv4_addr
*ia
;
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
) {
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
) {
348 if (sizeof(seed
) > ifp
->hwlen
) {
350 memcpy(&seed
, ifp
->hwaddr
, ifp
->hwlen
);
352 memcpy(&seed
, ifp
->hwaddr
+ ifp
->hwlen
- sizeof(seed
),
354 /* coverity[dont_call] */
355 orig
= initstate(seed
,
356 state
->randomstate
, sizeof(state
->randomstate
));
358 /* Save the original state. */
359 if (ifp
->ctx
->randomstate
== NULL
)
360 ifp
->ctx
->randomstate
= orig
;
362 /* Set back the original state until we need the seeded one. */
363 setstate(ifp
->ctx
->randomstate
);
364 state
->seeded
= true;
367 #ifndef KERNEL_RFC5227
368 if (astate
== NULL
) {
369 if (state
->arp
!= NULL
)
371 if ((astate
= arp_new(ifp
, NULL
)) == NULL
)
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
;
380 assert(state
->arp
== astate
);
387 /* Find the previosuly used address. */
388 if (state
->pickedaddr
.s_addr
!= INADDR_ANY
)
389 ia
= ipv4_iffindaddr(ifp
, &state
->pickedaddr
, NULL
);
393 /* Find an existing IPv4LL address and ensure we can work with it. */
395 ia
= ipv4_iffindlladdr(ifp
);
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. */
409 state
->pickedaddr
= ia
->addr
;
410 #ifndef KERNEL_RFC5227
411 astate
->addr
= ia
->addr
;
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
));
420 #ifdef IN_IFF_DUPLICATED
421 loginfox("%s: using IPv4LL address %s", ifp
->name
, ia
->saddr
);
423 ipv4ll_not_found(ifp
);
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
;
433 #ifdef IN_IFF_DUPLICATED
434 ipv4ll_not_found(ifp
);
441 ipv4ll_start(void *arg
)
444 ipv4ll_start1(arg
, NULL
);
448 ipv4ll_freearp(struct interface
*ifp
)
450 struct ipv4ll_state
*state
;
452 state
= IPV4LL_STATE(ifp
);
453 if (state
== NULL
|| state
->arp
== NULL
)
456 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, state
->arp
);
457 arp_free(state
->arp
);
461 ipv4ll_drop(struct interface
*ifp
)
463 struct ipv4ll_state
*state
;
464 bool dropped
= false;
465 struct ipv4_state
*istate
;
471 if ((ifp
->options
->options
& DHCPCD_NODROP
) == DHCPCD_NODROP
)
474 state
= IPV4LL_STATE(ifp
);
475 if (state
&& state
->addr
!= NULL
) {
476 ipv4_deladdr(state
->addr
, 1);
481 /* Free any other link local addresses that might exist. */
482 if ((istate
= IPV4_STATE(ifp
)) != NULL
) {
483 struct ipv4_addr
*ia
, *ian
;
485 TAILQ_FOREACH_SAFE(ia
, &istate
->addrs
, next
, ian
) {
486 if (IN_LINKLOCAL(ntohl(ia
->addr
.s_addr
))) {
494 rt_build(ifp
->ctx
, AF_INET
);
495 script_runreason(ifp
, "IPV4LL");
500 ipv4ll_reset(struct interface
*ifp
)
502 struct ipv4ll_state
*state
= IPV4LL_STATE(ifp
);
506 state
->pickedaddr
.s_addr
= INADDR_ANY
;
507 state
->seeded
= false;
511 ipv4ll_free(struct interface
*ifp
)
517 free(IPV4LL_STATE(ifp
));
518 ifp
->if_data
[IF_DATA_IPV4LL
] = NULL
;
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
525 ipv4ll_recvrt(__unused
int cmd
, const struct rt
*rt
)
527 struct dhcpcd_ctx
*ctx
;
528 struct interface
*ifp
;
530 /* Only interested in default route changes. */
531 if (sa_is_unspecified(&rt
->rt_dest
))
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
);
548 ipv4ll_handleifa(int cmd
, struct ipv4_addr
*ia
, pid_t pid
)
550 struct interface
*ifp
;
551 struct ipv4ll_state
*state
;
554 state
= IPV4LL_STATE(ifp
);
558 if (cmd
== RTM_DELADDR
&&
559 state
->addr
!= NULL
&&
560 IN_ARE_ADDR_EQUAL(&state
->addr
->addr
, &ia
->addr
))
562 loginfox("%s: pid %d deleted IP address %s",
563 ifp
->name
, pid
, ia
->saddr
);
564 ipv4ll_defend_failed(ifp
);
568 #ifdef IN_IFF_DUPLICATED
569 if (cmd
!= RTM_NEWADDR
)
571 if (!IN_ARE_ADDR_EQUAL(&state
->pickedaddr
, &ia
->addr
))
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);