1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Axis Communications AB. All rights reserved.
8 #include <netinet/if_ether.h>
12 #include "sd-ipv4acd.h"
14 #include "alloc-util.h"
16 #include "ether-addr-util.h"
17 #include "event-util.h"
19 #include "in-addr-util.h"
21 #include "random-util.h"
22 #include "siphash24.h"
23 #include "string-util.h"
24 #include "time-util.h"
26 /* Constants from the RFC */
27 #define PROBE_WAIT_USEC (1U * USEC_PER_SEC)
29 #define PROBE_MIN_USEC (1U * USEC_PER_SEC)
30 #define PROBE_MAX_USEC (2U * USEC_PER_SEC)
31 #define ANNOUNCE_WAIT_USEC (2U * USEC_PER_SEC)
32 #define ANNOUNCE_NUM 2U
33 #define ANNOUNCE_INTERVAL_USEC (2U * USEC_PER_SEC)
34 #define MAX_CONFLICTS 10U
35 #define RATE_LIMIT_INTERVAL_USEC (60U * USEC_PER_SEC)
36 #define DEFEND_INTERVAL_USEC (10U * USEC_PER_SEC)
38 typedef enum IPv4ACDState
{
40 IPV4ACD_STATE_STARTED
,
41 IPV4ACD_STATE_WAITING_PROBE
,
42 IPV4ACD_STATE_PROBING
,
43 IPV4ACD_STATE_WAITING_ANNOUNCE
,
44 IPV4ACD_STATE_ANNOUNCING
,
45 IPV4ACD_STATE_RUNNING
,
47 _IPV4ACD_STATE_INVALID
= -1
60 sd_event_source
*receive_message_event_source
;
61 sd_event_source
*timer_event_source
;
67 struct ether_addr mac_addr
;
71 sd_ipv4acd_callback_t callback
;
75 #define log_ipv4acd_errno(acd, error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "IPV4ACD: " fmt, ##__VA_ARGS__)
76 #define log_ipv4acd(acd, fmt, ...) log_ipv4acd_errno(acd, 0, fmt, ##__VA_ARGS__)
78 static void ipv4acd_set_state(sd_ipv4acd
*acd
, IPv4ACDState st
, bool reset_counter
) {
80 assert(st
< _IPV4ACD_STATE_MAX
);
82 if (st
== acd
->state
&& !reset_counter
)
90 static void ipv4acd_reset(sd_ipv4acd
*acd
) {
93 (void) event_source_disable(acd
->timer_event_source
);
94 acd
->receive_message_event_source
= sd_event_source_unref(acd
->receive_message_event_source
);
96 acd
->fd
= safe_close(acd
->fd
);
98 ipv4acd_set_state(acd
, IPV4ACD_STATE_INIT
, true);
101 static sd_ipv4acd
*ipv4acd_free(sd_ipv4acd
*acd
) {
104 acd
->timer_event_source
= sd_event_source_unref(acd
->timer_event_source
);
107 sd_ipv4acd_detach_event(acd
);
112 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4acd
, sd_ipv4acd
, ipv4acd_free
);
114 int sd_ipv4acd_new(sd_ipv4acd
**ret
) {
115 _cleanup_(sd_ipv4acd_unrefp
) sd_ipv4acd
*acd
= NULL
;
117 assert_return(ret
, -EINVAL
);
119 acd
= new(sd_ipv4acd
, 1);
123 *acd
= (sd_ipv4acd
) {
125 .state
= IPV4ACD_STATE_INIT
,
130 *ret
= TAKE_PTR(acd
);
135 static void ipv4acd_client_notify(sd_ipv4acd
*acd
, int event
) {
141 acd
->callback(acd
, event
, acd
->userdata
);
144 int sd_ipv4acd_stop(sd_ipv4acd
*acd
) {
145 IPv4ACDState old_state
;
150 old_state
= acd
->state
;
154 if (old_state
== IPV4ACD_STATE_INIT
)
157 log_ipv4acd(acd
, "STOPPED");
159 ipv4acd_client_notify(acd
, SD_IPV4ACD_EVENT_STOP
);
164 static int ipv4acd_on_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
);
166 static int ipv4acd_set_next_wakeup(sd_ipv4acd
*acd
, usec_t usec
, usec_t random_usec
) {
167 usec_t next_timeout
, time_now
;
174 next_timeout
+= (usec_t
) random_u64() % random_usec
;
176 assert_se(sd_event_now(acd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
178 return event_reset_time(acd
->event
, &acd
->timer_event_source
,
179 clock_boottime_or_monotonic(),
180 time_now
+ next_timeout
, 0,
181 ipv4acd_on_timeout
, acd
,
182 acd
->event_priority
, "ipv4acd-timer", true);
185 static bool ipv4acd_arp_conflict(sd_ipv4acd
*acd
, struct ether_arp
*arp
) {
190 if (memcmp(arp
->arp_spa
, &acd
->address
, sizeof(acd
->address
)) == 0)
193 /* the TPA matched instead of the SPA, this is not a conflict */
197 static int ipv4acd_on_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
198 sd_ipv4acd
*acd
= userdata
;
203 switch (acd
->state
) {
205 case IPV4ACD_STATE_STARTED
:
206 ipv4acd_set_state(acd
, IPV4ACD_STATE_WAITING_PROBE
, true);
208 if (acd
->n_conflict
>= MAX_CONFLICTS
) {
209 char ts
[FORMAT_TIMESPAN_MAX
];
210 log_ipv4acd(acd
, "Max conflicts reached, delaying by %s", format_timespan(ts
, sizeof(ts
), RATE_LIMIT_INTERVAL_USEC
, 0));
212 r
= ipv4acd_set_next_wakeup(acd
, RATE_LIMIT_INTERVAL_USEC
, PROBE_WAIT_USEC
);
216 r
= ipv4acd_set_next_wakeup(acd
, 0, PROBE_WAIT_USEC
);
223 case IPV4ACD_STATE_WAITING_PROBE
:
224 case IPV4ACD_STATE_PROBING
:
226 r
= arp_send_probe(acd
->fd
, acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
228 log_ipv4acd_errno(acd
, r
, "Failed to send ARP probe: %m");
231 _cleanup_free_
char *address
= NULL
;
232 union in_addr_union addr
= { .in
.s_addr
= acd
->address
};
234 (void) in_addr_to_string(AF_INET
, &addr
, &address
);
235 log_ipv4acd(acd
, "Probing %s", strna(address
));
238 if (acd
->n_iteration
< PROBE_NUM
- 2) {
239 ipv4acd_set_state(acd
, IPV4ACD_STATE_PROBING
, false);
241 r
= ipv4acd_set_next_wakeup(acd
, PROBE_MIN_USEC
, (PROBE_MAX_USEC
-PROBE_MIN_USEC
));
245 ipv4acd_set_state(acd
, IPV4ACD_STATE_WAITING_ANNOUNCE
, true);
247 r
= ipv4acd_set_next_wakeup(acd
, ANNOUNCE_WAIT_USEC
, 0);
254 case IPV4ACD_STATE_ANNOUNCING
:
255 if (acd
->n_iteration
>= ANNOUNCE_NUM
- 1) {
256 ipv4acd_set_state(acd
, IPV4ACD_STATE_RUNNING
, false);
261 case IPV4ACD_STATE_WAITING_ANNOUNCE
:
262 /* Send announcement packet */
263 r
= arp_send_announcement(acd
->fd
, acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
265 log_ipv4acd_errno(acd
, r
, "Failed to send ARP announcement: %m");
268 log_ipv4acd(acd
, "ANNOUNCE");
270 ipv4acd_set_state(acd
, IPV4ACD_STATE_ANNOUNCING
, false);
272 r
= ipv4acd_set_next_wakeup(acd
, ANNOUNCE_INTERVAL_USEC
, 0);
276 if (acd
->n_iteration
== 0) {
278 ipv4acd_client_notify(acd
, SD_IPV4ACD_EVENT_BIND
);
284 assert_not_reached("Invalid state.");
290 sd_ipv4acd_stop(acd
);
294 static void ipv4acd_on_conflict(sd_ipv4acd
*acd
) {
295 _cleanup_free_
char *address
= NULL
;
296 union in_addr_union addr
= { .in
.s_addr
= acd
->address
};
302 (void) in_addr_to_string(AF_INET
, &addr
, &address
);
303 log_ipv4acd(acd
, "Conflict on %s (%u)", strna(address
), acd
->n_conflict
);
306 ipv4acd_client_notify(acd
, SD_IPV4ACD_EVENT_CONFLICT
);
309 static int ipv4acd_on_packet(
315 sd_ipv4acd
*acd
= userdata
;
316 struct ether_arp packet
;
324 n
= recv(fd
, &packet
, sizeof(struct ether_arp
), 0);
326 if (IN_SET(errno
, EAGAIN
, EINTR
))
329 log_ipv4acd_errno(acd
, errno
, "Failed to read ARP packet: %m");
332 if ((size_t) n
!= sizeof(struct ether_arp
)) {
333 log_ipv4acd(acd
, "Ignoring too short ARP packet.");
337 switch (acd
->state
) {
339 case IPV4ACD_STATE_ANNOUNCING
:
340 case IPV4ACD_STATE_RUNNING
:
342 if (ipv4acd_arp_conflict(acd
, &packet
)) {
345 assert_se(sd_event_now(acd
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
348 if (ts
> acd
->defend_window
) {
349 acd
->defend_window
= ts
+ DEFEND_INTERVAL_USEC
;
350 r
= arp_send_announcement(acd
->fd
, acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
352 log_ipv4acd_errno(acd
, r
, "Failed to send ARP announcement: %m");
355 log_ipv4acd(acd
, "DEFEND");
358 ipv4acd_on_conflict(acd
);
362 case IPV4ACD_STATE_WAITING_PROBE
:
363 case IPV4ACD_STATE_PROBING
:
364 case IPV4ACD_STATE_WAITING_ANNOUNCE
:
365 /* BPF ensures this packet indicates a conflict */
366 ipv4acd_on_conflict(acd
);
370 assert_not_reached("Invalid state.");
376 sd_ipv4acd_stop(acd
);
380 int sd_ipv4acd_set_ifindex(sd_ipv4acd
*acd
, int ifindex
) {
381 assert_return(acd
, -EINVAL
);
382 assert_return(ifindex
> 0, -EINVAL
);
383 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
385 acd
->ifindex
= ifindex
;
390 int sd_ipv4acd_set_mac(sd_ipv4acd
*acd
, const struct ether_addr
*addr
) {
391 assert_return(acd
, -EINVAL
);
392 assert_return(addr
, -EINVAL
);
393 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
395 acd
->mac_addr
= *addr
;
400 int sd_ipv4acd_detach_event(sd_ipv4acd
*acd
) {
401 assert_return(acd
, -EINVAL
);
403 acd
->event
= sd_event_unref(acd
->event
);
408 int sd_ipv4acd_attach_event(sd_ipv4acd
*acd
, sd_event
*event
, int64_t priority
) {
411 assert_return(acd
, -EINVAL
);
412 assert_return(!acd
->event
, -EBUSY
);
415 acd
->event
= sd_event_ref(event
);
417 r
= sd_event_default(&acd
->event
);
422 acd
->event_priority
= priority
;
427 int sd_ipv4acd_set_callback(sd_ipv4acd
*acd
, sd_ipv4acd_callback_t cb
, void *userdata
) {
428 assert_return(acd
, -EINVAL
);
431 acd
->userdata
= userdata
;
436 int sd_ipv4acd_set_address(sd_ipv4acd
*acd
, const struct in_addr
*address
) {
437 assert_return(acd
, -EINVAL
);
438 assert_return(address
, -EINVAL
);
439 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
441 acd
->address
= address
->s_addr
;
446 int sd_ipv4acd_get_address(sd_ipv4acd
*acd
, struct in_addr
*address
) {
447 assert_return(acd
, -EINVAL
);
448 assert_return(address
, -EINVAL
);
450 address
->s_addr
= acd
->address
;
455 int sd_ipv4acd_is_running(sd_ipv4acd
*acd
) {
456 assert_return(acd
, false);
458 return acd
->state
!= IPV4ACD_STATE_INIT
;
461 int sd_ipv4acd_start(sd_ipv4acd
*acd
, bool reset_conflicts
) {
464 assert_return(acd
, -EINVAL
);
465 assert_return(acd
->event
, -EINVAL
);
466 assert_return(acd
->ifindex
> 0, -EINVAL
);
467 assert_return(acd
->address
!= 0, -EINVAL
);
468 assert_return(!ether_addr_is_null(&acd
->mac_addr
), -EINVAL
);
469 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
471 r
= arp_network_bind_raw_socket(acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
475 CLOSE_AND_REPLACE(acd
->fd
, r
);
476 acd
->defend_window
= 0;
481 r
= sd_event_add_io(acd
->event
, &acd
->receive_message_event_source
, acd
->fd
, EPOLLIN
, ipv4acd_on_packet
, acd
);
485 r
= sd_event_source_set_priority(acd
->receive_message_event_source
, acd
->event_priority
);
489 (void) sd_event_source_set_description(acd
->receive_message_event_source
, "ipv4acd-receive-message");
491 r
= ipv4acd_set_next_wakeup(acd
, 0, 0);
495 ipv4acd_set_state(acd
, IPV4ACD_STATE_STARTED
, true);