]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ipv4acd.c
Merge pull request #17185 from yuwata/ethtool-update
[thirdparty/systemd.git] / src / libsystemd-network / sd-ipv4acd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Axis Communications AB. All rights reserved.
4 ***/
5
6 #include <arpa/inet.h>
7 #include <errno.h>
8 #include <netinet/if_ether.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "sd-ipv4acd.h"
13
14 #include "alloc-util.h"
15 #include "arp-util.h"
16 #include "ether-addr-util.h"
17 #include "event-util.h"
18 #include "fd-util.h"
19 #include "in-addr-util.h"
20 #include "list.h"
21 #include "random-util.h"
22 #include "siphash24.h"
23 #include "string-util.h"
24 #include "time-util.h"
25
26 /* Constants from the RFC */
27 #define PROBE_WAIT_USEC (1U * USEC_PER_SEC)
28 #define PROBE_NUM 3U
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)
37
38 typedef enum IPv4ACDState {
39 IPV4ACD_STATE_INIT,
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,
46 _IPV4ACD_STATE_MAX,
47 _IPV4ACD_STATE_INVALID = -1
48 } IPv4ACDState;
49
50 struct sd_ipv4acd {
51 unsigned n_ref;
52
53 IPv4ACDState state;
54 int ifindex;
55 int fd;
56
57 unsigned n_iteration;
58 unsigned n_conflict;
59
60 sd_event_source *receive_message_event_source;
61 sd_event_source *timer_event_source;
62
63 usec_t defend_window;
64 be32_t address;
65
66 /* External */
67 struct ether_addr mac_addr;
68
69 sd_event *event;
70 int event_priority;
71 sd_ipv4acd_callback_t callback;
72 void* userdata;
73 };
74
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__)
77
78 static void ipv4acd_set_state(sd_ipv4acd *acd, IPv4ACDState st, bool reset_counter) {
79 assert(acd);
80 assert(st < _IPV4ACD_STATE_MAX);
81
82 if (st == acd->state && !reset_counter)
83 acd->n_iteration++;
84 else {
85 acd->state = st;
86 acd->n_iteration = 0;
87 }
88 }
89
90 static void ipv4acd_reset(sd_ipv4acd *acd) {
91 assert(acd);
92
93 (void) event_source_disable(acd->timer_event_source);
94 acd->receive_message_event_source = sd_event_source_unref(acd->receive_message_event_source);
95
96 acd->fd = safe_close(acd->fd);
97
98 ipv4acd_set_state(acd, IPV4ACD_STATE_INIT, true);
99 }
100
101 static sd_ipv4acd *ipv4acd_free(sd_ipv4acd *acd) {
102 assert(acd);
103
104 acd->timer_event_source = sd_event_source_unref(acd->timer_event_source);
105
106 ipv4acd_reset(acd);
107 sd_ipv4acd_detach_event(acd);
108
109 return mfree(acd);
110 }
111
112 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4acd, sd_ipv4acd, ipv4acd_free);
113
114 int sd_ipv4acd_new(sd_ipv4acd **ret) {
115 _cleanup_(sd_ipv4acd_unrefp) sd_ipv4acd *acd = NULL;
116
117 assert_return(ret, -EINVAL);
118
119 acd = new(sd_ipv4acd, 1);
120 if (!acd)
121 return -ENOMEM;
122
123 *acd = (sd_ipv4acd) {
124 .n_ref = 1,
125 .state = IPV4ACD_STATE_INIT,
126 .ifindex = -1,
127 .fd = -1,
128 };
129
130 *ret = TAKE_PTR(acd);
131
132 return 0;
133 }
134
135 static void ipv4acd_client_notify(sd_ipv4acd *acd, int event) {
136 assert(acd);
137
138 if (!acd->callback)
139 return;
140
141 acd->callback(acd, event, acd->userdata);
142 }
143
144 int sd_ipv4acd_stop(sd_ipv4acd *acd) {
145 IPv4ACDState old_state;
146
147 if (!acd)
148 return 0;
149
150 old_state = acd->state;
151
152 ipv4acd_reset(acd);
153
154 if (old_state == IPV4ACD_STATE_INIT)
155 return 0;
156
157 log_ipv4acd(acd, "STOPPED");
158
159 ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_STOP);
160
161 return 0;
162 }
163
164 static int ipv4acd_on_timeout(sd_event_source *s, uint64_t usec, void *userdata);
165
166 static int ipv4acd_set_next_wakeup(sd_ipv4acd *acd, usec_t usec, usec_t random_usec) {
167 usec_t next_timeout, time_now;
168
169 assert(acd);
170
171 next_timeout = usec;
172
173 if (random_usec > 0)
174 next_timeout += (usec_t) random_u64() % random_usec;
175
176 assert_se(sd_event_now(acd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
177
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);
183 }
184
185 static bool ipv4acd_arp_conflict(sd_ipv4acd *acd, struct ether_arp *arp) {
186 assert(acd);
187 assert(arp);
188
189 /* see the BPF */
190 if (memcmp(arp->arp_spa, &acd->address, sizeof(acd->address)) == 0)
191 return true;
192
193 /* the TPA matched instead of the SPA, this is not a conflict */
194 return false;
195 }
196
197 static int ipv4acd_on_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
198 sd_ipv4acd *acd = userdata;
199 int r = 0;
200
201 assert(acd);
202
203 switch (acd->state) {
204
205 case IPV4ACD_STATE_STARTED:
206 ipv4acd_set_state(acd, IPV4ACD_STATE_WAITING_PROBE, true);
207
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));
211
212 r = ipv4acd_set_next_wakeup(acd, RATE_LIMIT_INTERVAL_USEC, PROBE_WAIT_USEC);
213 if (r < 0)
214 goto fail;
215 } else {
216 r = ipv4acd_set_next_wakeup(acd, 0, PROBE_WAIT_USEC);
217 if (r < 0)
218 goto fail;
219 }
220
221 break;
222
223 case IPV4ACD_STATE_WAITING_PROBE:
224 case IPV4ACD_STATE_PROBING:
225 /* Send a probe */
226 r = arp_send_probe(acd->fd, acd->ifindex, acd->address, &acd->mac_addr);
227 if (r < 0) {
228 log_ipv4acd_errno(acd, r, "Failed to send ARP probe: %m");
229 goto fail;
230 } else {
231 _cleanup_free_ char *address = NULL;
232 union in_addr_union addr = { .in.s_addr = acd->address };
233
234 (void) in_addr_to_string(AF_INET, &addr, &address);
235 log_ipv4acd(acd, "Probing %s", strna(address));
236 }
237
238 if (acd->n_iteration < PROBE_NUM - 2) {
239 ipv4acd_set_state(acd, IPV4ACD_STATE_PROBING, false);
240
241 r = ipv4acd_set_next_wakeup(acd, PROBE_MIN_USEC, (PROBE_MAX_USEC-PROBE_MIN_USEC));
242 if (r < 0)
243 goto fail;
244 } else {
245 ipv4acd_set_state(acd, IPV4ACD_STATE_WAITING_ANNOUNCE, true);
246
247 r = ipv4acd_set_next_wakeup(acd, ANNOUNCE_WAIT_USEC, 0);
248 if (r < 0)
249 goto fail;
250 }
251
252 break;
253
254 case IPV4ACD_STATE_ANNOUNCING:
255 if (acd->n_iteration >= ANNOUNCE_NUM - 1) {
256 ipv4acd_set_state(acd, IPV4ACD_STATE_RUNNING, false);
257 break;
258 }
259
260 _fallthrough_;
261 case IPV4ACD_STATE_WAITING_ANNOUNCE:
262 /* Send announcement packet */
263 r = arp_send_announcement(acd->fd, acd->ifindex, acd->address, &acd->mac_addr);
264 if (r < 0) {
265 log_ipv4acd_errno(acd, r, "Failed to send ARP announcement: %m");
266 goto fail;
267 } else
268 log_ipv4acd(acd, "ANNOUNCE");
269
270 ipv4acd_set_state(acd, IPV4ACD_STATE_ANNOUNCING, false);
271
272 r = ipv4acd_set_next_wakeup(acd, ANNOUNCE_INTERVAL_USEC, 0);
273 if (r < 0)
274 goto fail;
275
276 if (acd->n_iteration == 0) {
277 acd->n_conflict = 0;
278 ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_BIND);
279 }
280
281 break;
282
283 default:
284 assert_not_reached("Invalid state.");
285 }
286
287 return 0;
288
289 fail:
290 sd_ipv4acd_stop(acd);
291 return 0;
292 }
293
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 };
297
298 assert(acd);
299
300 acd->n_conflict++;
301
302 (void) in_addr_to_string(AF_INET, &addr, &address);
303 log_ipv4acd(acd, "Conflict on %s (%u)", strna(address), acd->n_conflict);
304
305 ipv4acd_reset(acd);
306 ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_CONFLICT);
307 }
308
309 static int ipv4acd_on_packet(
310 sd_event_source *s,
311 int fd,
312 uint32_t revents,
313 void *userdata) {
314
315 sd_ipv4acd *acd = userdata;
316 struct ether_arp packet;
317 ssize_t n;
318 int r;
319
320 assert(s);
321 assert(acd);
322 assert(fd >= 0);
323
324 n = recv(fd, &packet, sizeof(struct ether_arp), 0);
325 if (n < 0) {
326 if (IN_SET(errno, EAGAIN, EINTR))
327 return 0;
328
329 log_ipv4acd_errno(acd, errno, "Failed to read ARP packet: %m");
330 goto fail;
331 }
332 if ((size_t) n != sizeof(struct ether_arp)) {
333 log_ipv4acd(acd, "Ignoring too short ARP packet.");
334 return 0;
335 }
336
337 switch (acd->state) {
338
339 case IPV4ACD_STATE_ANNOUNCING:
340 case IPV4ACD_STATE_RUNNING:
341
342 if (ipv4acd_arp_conflict(acd, &packet)) {
343 usec_t ts;
344
345 assert_se(sd_event_now(acd->event, clock_boottime_or_monotonic(), &ts) >= 0);
346
347 /* Defend address */
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);
351 if (r < 0) {
352 log_ipv4acd_errno(acd, r, "Failed to send ARP announcement: %m");
353 goto fail;
354 } else
355 log_ipv4acd(acd, "DEFEND");
356
357 } else
358 ipv4acd_on_conflict(acd);
359 }
360 break;
361
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);
367 break;
368
369 default:
370 assert_not_reached("Invalid state.");
371 }
372
373 return 0;
374
375 fail:
376 sd_ipv4acd_stop(acd);
377 return 0;
378 }
379
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);
384
385 acd->ifindex = ifindex;
386
387 return 0;
388 }
389
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);
394
395 acd->mac_addr = *addr;
396
397 return 0;
398 }
399
400 int sd_ipv4acd_detach_event(sd_ipv4acd *acd) {
401 assert_return(acd, -EINVAL);
402
403 acd->event = sd_event_unref(acd->event);
404
405 return 0;
406 }
407
408 int sd_ipv4acd_attach_event(sd_ipv4acd *acd, sd_event *event, int64_t priority) {
409 int r;
410
411 assert_return(acd, -EINVAL);
412 assert_return(!acd->event, -EBUSY);
413
414 if (event)
415 acd->event = sd_event_ref(event);
416 else {
417 r = sd_event_default(&acd->event);
418 if (r < 0)
419 return r;
420 }
421
422 acd->event_priority = priority;
423
424 return 0;
425 }
426
427 int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *userdata) {
428 assert_return(acd, -EINVAL);
429
430 acd->callback = cb;
431 acd->userdata = userdata;
432
433 return 0;
434 }
435
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);
440
441 acd->address = address->s_addr;
442
443 return 0;
444 }
445
446 int sd_ipv4acd_get_address(sd_ipv4acd *acd, struct in_addr *address) {
447 assert_return(acd, -EINVAL);
448 assert_return(address, -EINVAL);
449
450 address->s_addr = acd->address;
451
452 return 0;
453 }
454
455 int sd_ipv4acd_is_running(sd_ipv4acd *acd) {
456 assert_return(acd, false);
457
458 return acd->state != IPV4ACD_STATE_INIT;
459 }
460
461 int sd_ipv4acd_start(sd_ipv4acd *acd, bool reset_conflicts) {
462 int r;
463
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);
470
471 r = arp_network_bind_raw_socket(acd->ifindex, acd->address, &acd->mac_addr);
472 if (r < 0)
473 return r;
474
475 CLOSE_AND_REPLACE(acd->fd, r);
476 acd->defend_window = 0;
477
478 if (reset_conflicts)
479 acd->n_conflict = 0;
480
481 r = sd_event_add_io(acd->event, &acd->receive_message_event_source, acd->fd, EPOLLIN, ipv4acd_on_packet, acd);
482 if (r < 0)
483 goto fail;
484
485 r = sd_event_source_set_priority(acd->receive_message_event_source, acd->event_priority);
486 if (r < 0)
487 goto fail;
488
489 (void) sd_event_source_set_description(acd->receive_message_event_source, "ipv4acd-receive-message");
490
491 r = ipv4acd_set_next_wakeup(acd, 0, 0);
492 if (r < 0)
493 goto fail;
494
495 ipv4acd_set_state(acd, IPV4ACD_STATE_STARTED, true);
496 return 0;
497
498 fail:
499 ipv4acd_reset(acd);
500 return r;
501 }