1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Axis Communications AB. All rights reserved.
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"
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, __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 assert_return(acd
, -EINVAL
);
149 log_ipv4acd(acd
, "STOPPED");
151 ipv4acd_client_notify(acd
, SD_IPV4ACD_EVENT_STOP
);
156 static int ipv4acd_on_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
);
158 static int ipv4acd_set_next_wakeup(sd_ipv4acd
*acd
, usec_t usec
, usec_t random_usec
) {
159 usec_t next_timeout
, time_now
;
166 next_timeout
+= (usec_t
) random_u64() % random_usec
;
168 assert_se(sd_event_now(acd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
170 return event_reset_time(acd
->event
, &acd
->timer_event_source
,
171 clock_boottime_or_monotonic(),
172 time_now
+ next_timeout
, 0,
173 ipv4acd_on_timeout
, acd
,
174 acd
->event_priority
, "ipv4acd-timer", true);
177 static bool ipv4acd_arp_conflict(sd_ipv4acd
*acd
, struct ether_arp
*arp
) {
182 if (memcmp(arp
->arp_spa
, &acd
->address
, sizeof(acd
->address
)) == 0)
185 /* the TPA matched instead of the SPA, this is not a conflict */
189 static int ipv4acd_on_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
190 sd_ipv4acd
*acd
= userdata
;
195 switch (acd
->state
) {
197 case IPV4ACD_STATE_STARTED
:
198 ipv4acd_set_state(acd
, IPV4ACD_STATE_WAITING_PROBE
, true);
200 if (acd
->n_conflict
>= MAX_CONFLICTS
) {
201 char ts
[FORMAT_TIMESPAN_MAX
];
202 log_ipv4acd(acd
, "Max conflicts reached, delaying by %s", format_timespan(ts
, sizeof(ts
), RATE_LIMIT_INTERVAL_USEC
, 0));
204 r
= ipv4acd_set_next_wakeup(acd
, RATE_LIMIT_INTERVAL_USEC
, PROBE_WAIT_USEC
);
208 r
= ipv4acd_set_next_wakeup(acd
, 0, PROBE_WAIT_USEC
);
215 case IPV4ACD_STATE_WAITING_PROBE
:
216 case IPV4ACD_STATE_PROBING
:
218 r
= arp_send_probe(acd
->fd
, acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
220 log_ipv4acd_errno(acd
, r
, "Failed to send ARP probe: %m");
223 _cleanup_free_
char *address
= NULL
;
224 union in_addr_union addr
= { .in
.s_addr
= acd
->address
};
226 (void) in_addr_to_string(AF_INET
, &addr
, &address
);
227 log_ipv4acd(acd
, "Probing %s", strna(address
));
230 if (acd
->n_iteration
< PROBE_NUM
- 2) {
231 ipv4acd_set_state(acd
, IPV4ACD_STATE_PROBING
, false);
233 r
= ipv4acd_set_next_wakeup(acd
, PROBE_MIN_USEC
, (PROBE_MAX_USEC
-PROBE_MIN_USEC
));
237 ipv4acd_set_state(acd
, IPV4ACD_STATE_WAITING_ANNOUNCE
, true);
239 r
= ipv4acd_set_next_wakeup(acd
, ANNOUNCE_WAIT_USEC
, 0);
246 case IPV4ACD_STATE_ANNOUNCING
:
247 if (acd
->n_iteration
>= ANNOUNCE_NUM
- 1) {
248 ipv4acd_set_state(acd
, IPV4ACD_STATE_RUNNING
, false);
253 case IPV4ACD_STATE_WAITING_ANNOUNCE
:
254 /* Send announcement packet */
255 r
= arp_send_announcement(acd
->fd
, acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
257 log_ipv4acd_errno(acd
, r
, "Failed to send ARP announcement: %m");
260 log_ipv4acd(acd
, "ANNOUNCE");
262 ipv4acd_set_state(acd
, IPV4ACD_STATE_ANNOUNCING
, false);
264 r
= ipv4acd_set_next_wakeup(acd
, ANNOUNCE_INTERVAL_USEC
, 0);
268 if (acd
->n_iteration
== 0) {
270 ipv4acd_client_notify(acd
, SD_IPV4ACD_EVENT_BIND
);
276 assert_not_reached("Invalid state.");
282 sd_ipv4acd_stop(acd
);
286 static void ipv4acd_on_conflict(sd_ipv4acd
*acd
) {
287 _cleanup_free_
char *address
= NULL
;
288 union in_addr_union addr
= { .in
.s_addr
= acd
->address
};
294 (void) in_addr_to_string(AF_INET
, &addr
, &address
);
295 log_ipv4acd(acd
, "Conflict on %s (%u)", strna(address
), acd
->n_conflict
);
298 ipv4acd_client_notify(acd
, SD_IPV4ACD_EVENT_CONFLICT
);
301 static int ipv4acd_on_packet(
307 sd_ipv4acd
*acd
= userdata
;
308 struct ether_arp packet
;
316 n
= recv(fd
, &packet
, sizeof(struct ether_arp
), 0);
318 if (IN_SET(errno
, EAGAIN
, EINTR
))
321 log_ipv4acd_errno(acd
, errno
, "Failed to read ARP packet: %m");
324 if ((size_t) n
!= sizeof(struct ether_arp
)) {
325 log_ipv4acd(acd
, "Ignoring too short ARP packet.");
329 switch (acd
->state
) {
331 case IPV4ACD_STATE_ANNOUNCING
:
332 case IPV4ACD_STATE_RUNNING
:
334 if (ipv4acd_arp_conflict(acd
, &packet
)) {
337 assert_se(sd_event_now(acd
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
340 if (ts
> acd
->defend_window
) {
341 acd
->defend_window
= ts
+ DEFEND_INTERVAL_USEC
;
342 r
= arp_send_announcement(acd
->fd
, acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
344 log_ipv4acd_errno(acd
, r
, "Failed to send ARP announcement: %m");
347 log_ipv4acd(acd
, "DEFEND");
350 ipv4acd_on_conflict(acd
);
354 case IPV4ACD_STATE_WAITING_PROBE
:
355 case IPV4ACD_STATE_PROBING
:
356 case IPV4ACD_STATE_WAITING_ANNOUNCE
:
357 /* BPF ensures this packet indicates a conflict */
358 ipv4acd_on_conflict(acd
);
362 assert_not_reached("Invalid state.");
368 sd_ipv4acd_stop(acd
);
372 int sd_ipv4acd_set_ifindex(sd_ipv4acd
*acd
, int ifindex
) {
373 assert_return(acd
, -EINVAL
);
374 assert_return(ifindex
> 0, -EINVAL
);
375 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
377 acd
->ifindex
= ifindex
;
382 int sd_ipv4acd_set_mac(sd_ipv4acd
*acd
, const struct ether_addr
*addr
) {
383 assert_return(acd
, -EINVAL
);
384 assert_return(addr
, -EINVAL
);
385 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
387 acd
->mac_addr
= *addr
;
392 int sd_ipv4acd_detach_event(sd_ipv4acd
*acd
) {
393 assert_return(acd
, -EINVAL
);
395 acd
->event
= sd_event_unref(acd
->event
);
400 int sd_ipv4acd_attach_event(sd_ipv4acd
*acd
, sd_event
*event
, int64_t priority
) {
403 assert_return(acd
, -EINVAL
);
404 assert_return(!acd
->event
, -EBUSY
);
407 acd
->event
= sd_event_ref(event
);
409 r
= sd_event_default(&acd
->event
);
414 acd
->event_priority
= priority
;
419 int sd_ipv4acd_set_callback(sd_ipv4acd
*acd
, sd_ipv4acd_callback_t cb
, void *userdata
) {
420 assert_return(acd
, -EINVAL
);
423 acd
->userdata
= userdata
;
428 int sd_ipv4acd_set_address(sd_ipv4acd
*acd
, const struct in_addr
*address
) {
429 assert_return(acd
, -EINVAL
);
430 assert_return(address
, -EINVAL
);
431 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
433 acd
->address
= address
->s_addr
;
438 int sd_ipv4acd_is_running(sd_ipv4acd
*acd
) {
439 assert_return(acd
, false);
441 return acd
->state
!= IPV4ACD_STATE_INIT
;
444 int sd_ipv4acd_start(sd_ipv4acd
*acd
) {
447 assert_return(acd
, -EINVAL
);
448 assert_return(acd
->event
, -EINVAL
);
449 assert_return(acd
->ifindex
> 0, -EINVAL
);
450 assert_return(acd
->address
!= 0, -EINVAL
);
451 assert_return(!ether_addr_is_null(&acd
->mac_addr
), -EINVAL
);
452 assert_return(acd
->state
== IPV4ACD_STATE_INIT
, -EBUSY
);
454 r
= arp_network_bind_raw_socket(acd
->ifindex
, acd
->address
, &acd
->mac_addr
);
460 acd
->defend_window
= 0;
463 r
= sd_event_add_io(acd
->event
, &acd
->receive_message_event_source
, acd
->fd
, EPOLLIN
, ipv4acd_on_packet
, acd
);
467 r
= sd_event_source_set_priority(acd
->receive_message_event_source
, acd
->event_priority
);
471 (void) sd_event_source_set_description(acd
->receive_message_event_source
, "ipv4acd-receive-message");
473 r
= ipv4acd_set_next_wakeup(acd
, 0, 0);
477 ipv4acd_set_state(acd
, IPV4ACD_STATE_STARTED
, true);