]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e3dca008 | 2 | /*** |
810adae9 | 3 | Copyright © 2014 Axis Communications AB. All rights reserved. |
e3dca008 TG |
4 | ***/ |
5 | ||
8f815e8b | 6 | #include <netinet/if_ether.h> |
e3dca008 | 7 | #include <stdio.h> |
e3dca008 | 8 | |
07630cea LP |
9 | #include "sd-ipv4acd.h" |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
07630cea | 12 | #include "arp-util.h" |
5cdf13c7 | 13 | #include "errno-util.h" |
e78f9587 | 14 | #include "ether-addr-util.h" |
32ab66c5 | 15 | #include "event-util.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
e3dca008 | 17 | #include "in-addr-util.h" |
7f77917c | 18 | #include "memory-util.h" |
61a9fa8f | 19 | #include "network-common.h" |
e3dca008 | 20 | #include "random-util.h" |
5cdf13c7 | 21 | #include "socket-util.h" |
878c035a | 22 | #include "string-table.h" |
d246e77a | 23 | #include "string-util.h" |
ca78ad1d | 24 | #include "time-util.h" |
e3dca008 | 25 | |
c2691d8e BG |
26 | /* Intervals from the RFC in seconds, need to be multiplied by the time unit */ |
27 | #define PROBE_WAIT 1U | |
28 | #define PROBE_MIN 1U | |
29 | #define PROBE_MAX 2U | |
30 | #define ANNOUNCE_WAIT 2U | |
31 | #define TOTAL_TIME_UNITS 7U | |
32 | ||
33 | /* Intervals from the RFC not adjusted to the time unit */ | |
e3f4eedb | 34 | #define ANNOUNCE_INTERVAL_USEC (2U * USEC_PER_SEC) |
e3f4eedb LP |
35 | #define RATE_LIMIT_INTERVAL_USEC (60U * USEC_PER_SEC) |
36 | #define DEFEND_INTERVAL_USEC (10U * USEC_PER_SEC) | |
e3dca008 | 37 | |
c2691d8e BG |
38 | /* Other constants from the RFC */ |
39 | #define PROBE_NUM 3U | |
40 | #define ANNOUNCE_NUM 2U | |
41 | #define MAX_CONFLICTS 10U | |
42 | ||
43 | /* Default timeout from the RFC */ | |
2451cd25 | 44 | #define DEFAULT_ACD_TIMEOUT_USEC (200 * USEC_PER_MSEC) |
c2691d8e | 45 | |
e3dca008 TG |
46 | typedef enum IPv4ACDState { |
47 | IPV4ACD_STATE_INIT, | |
c9e458a4 | 48 | IPV4ACD_STATE_STARTED, |
e3dca008 TG |
49 | IPV4ACD_STATE_WAITING_PROBE, |
50 | IPV4ACD_STATE_PROBING, | |
51 | IPV4ACD_STATE_WAITING_ANNOUNCE, | |
52 | IPV4ACD_STATE_ANNOUNCING, | |
53 | IPV4ACD_STATE_RUNNING, | |
54 | _IPV4ACD_STATE_MAX, | |
2d93c20e | 55 | _IPV4ACD_STATE_INVALID = -EINVAL, |
e3dca008 TG |
56 | } IPv4ACDState; |
57 | ||
58 | struct sd_ipv4acd { | |
c116f526 | 59 | unsigned n_ref; |
e3dca008 TG |
60 | |
61 | IPv4ACDState state; | |
2f8e7633 | 62 | int ifindex; |
e3dca008 | 63 | int fd; |
784cdc2d | 64 | |
61a9fa8f | 65 | char *ifname; |
784cdc2d LP |
66 | unsigned n_iteration; |
67 | unsigned n_conflict; | |
68 | ||
c2691d8e BG |
69 | /* Indicates the duration of a "time unit", i.e. one second in the RFC but scaled to the |
70 | * chosen total duration. Represents 1/7 of the total conflict detection timeout. */ | |
71 | usec_t time_unit_usec; | |
72 | ||
4dbf7b3a LP |
73 | sd_event_source *receive_message_event_source; |
74 | sd_event_source *timer_event_source; | |
784cdc2d | 75 | |
e3dca008 | 76 | usec_t defend_window; |
9be2ba5e | 77 | struct in_addr address; |
784cdc2d | 78 | |
e3dca008 TG |
79 | /* External */ |
80 | struct ether_addr mac_addr; | |
784cdc2d | 81 | |
e3dca008 TG |
82 | sd_event *event; |
83 | int event_priority; | |
45aa74c7 | 84 | sd_ipv4acd_callback_t callback; |
7f77917c YW |
85 | void *userdata; |
86 | sd_ipv4acd_check_mac_callback_t check_mac_callback; | |
87 | void *check_mac_userdata; | |
e3dca008 TG |
88 | }; |
89 | ||
a0c2541b | 90 | #define log_ipv4acd_errno(acd, error, fmt, ...) \ |
00dd6d77 | 91 | log_interface_prefix_full_errno( \ |
a0c2541b | 92 | "IPv4ACD: ", \ |
5977b71f | 93 | sd_ipv4acd, acd, \ |
a0c2541b | 94 | error, fmt, ##__VA_ARGS__) |
3f2c0d85 | 95 | #define log_ipv4acd(acd, fmt, ...) \ |
00dd6d77 ZJS |
96 | log_interface_prefix_full_errno_zerook( \ |
97 | "IPv4ACD: ", \ | |
5977b71f | 98 | sd_ipv4acd, acd, \ |
00dd6d77 | 99 | 0, fmt, ##__VA_ARGS__) |
3aacc173 | 100 | |
878c035a YW |
101 | static const char * const ipv4acd_state_table[_IPV4ACD_STATE_MAX] = { |
102 | [IPV4ACD_STATE_INIT] = "init", | |
103 | [IPV4ACD_STATE_STARTED] = "started", | |
104 | [IPV4ACD_STATE_WAITING_PROBE] = "waiting-probe", | |
105 | [IPV4ACD_STATE_PROBING] = "probing", | |
106 | [IPV4ACD_STATE_WAITING_ANNOUNCE] = "waiting-announce", | |
107 | [IPV4ACD_STATE_ANNOUNCING] = "announcing", | |
108 | [IPV4ACD_STATE_RUNNING] = "running", | |
109 | }; | |
110 | ||
111 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(ipv4acd_state, IPv4ACDState); | |
112 | ||
b24ef049 LP |
113 | static void ipv4acd_set_state(sd_ipv4acd *acd, IPv4ACDState st, bool reset_counter) { |
114 | assert(acd); | |
d246e77a LP |
115 | assert(st < _IPV4ACD_STATE_MAX); |
116 | ||
878c035a YW |
117 | if (st != acd->state) |
118 | log_ipv4acd(acd, "%s -> %s", ipv4acd_state_to_string(acd->state), ipv4acd_state_to_string(st)); | |
119 | ||
b24ef049 LP |
120 | if (st == acd->state && !reset_counter) |
121 | acd->n_iteration++; | |
d246e77a | 122 | else { |
b24ef049 LP |
123 | acd->state = st; |
124 | acd->n_iteration = 0; | |
d246e77a LP |
125 | } |
126 | } | |
127 | ||
b24ef049 LP |
128 | static void ipv4acd_reset(sd_ipv4acd *acd) { |
129 | assert(acd); | |
d246e77a | 130 | |
32ab66c5 | 131 | (void) event_source_disable(acd->timer_event_source); |
eb2f7502 | 132 | acd->receive_message_event_source = sd_event_source_disable_unref(acd->receive_message_event_source); |
d246e77a | 133 | |
b24ef049 | 134 | acd->fd = safe_close(acd->fd); |
d246e77a | 135 | |
b24ef049 | 136 | ipv4acd_set_state(acd, IPV4ACD_STATE_INIT, true); |
d246e77a LP |
137 | } |
138 | ||
8301aa0b YW |
139 | static sd_ipv4acd *ipv4acd_free(sd_ipv4acd *acd) { |
140 | assert(acd); | |
e3dca008 | 141 | |
b24ef049 | 142 | ipv4acd_reset(acd); |
eb2f7502 | 143 | sd_event_source_unref(acd->timer_event_source); |
b24ef049 | 144 | sd_ipv4acd_detach_event(acd); |
61a9fa8f | 145 | free(acd->ifname); |
6b430fdb | 146 | return mfree(acd); |
e3dca008 TG |
147 | } |
148 | ||
8301aa0b YW |
149 | DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4acd, sd_ipv4acd, ipv4acd_free); |
150 | ||
e3dca008 | 151 | int sd_ipv4acd_new(sd_ipv4acd **ret) { |
b24ef049 | 152 | _cleanup_(sd_ipv4acd_unrefp) sd_ipv4acd *acd = NULL; |
e3dca008 TG |
153 | |
154 | assert_return(ret, -EINVAL); | |
155 | ||
4ca5acb3 | 156 | acd = new(sd_ipv4acd, 1); |
b24ef049 | 157 | if (!acd) |
e3dca008 TG |
158 | return -ENOMEM; |
159 | ||
4ca5acb3 YW |
160 | *acd = (sd_ipv4acd) { |
161 | .n_ref = 1, | |
162 | .state = IPV4ACD_STATE_INIT, | |
c2691d8e | 163 | .time_unit_usec = DEFAULT_ACD_TIMEOUT_USEC / TOTAL_TIME_UNITS, |
4ca5acb3 | 164 | .ifindex = -1, |
254d1313 | 165 | .fd = -EBADF, |
4ca5acb3 | 166 | }; |
e3dca008 | 167 | |
1cc6c93a | 168 | *ret = TAKE_PTR(acd); |
e3dca008 TG |
169 | |
170 | return 0; | |
171 | } | |
172 | ||
b24ef049 LP |
173 | static void ipv4acd_client_notify(sd_ipv4acd *acd, int event) { |
174 | assert(acd); | |
e3dca008 | 175 | |
b24ef049 | 176 | if (!acd->callback) |
e095f51d LP |
177 | return; |
178 | ||
b24ef049 | 179 | acd->callback(acd, event, acd->userdata); |
e3dca008 TG |
180 | } |
181 | ||
b24ef049 | 182 | int sd_ipv4acd_stop(sd_ipv4acd *acd) { |
6a776e3a YW |
183 | IPv4ACDState old_state; |
184 | ||
c8bae363 YW |
185 | if (!acd) |
186 | return 0; | |
e3dca008 | 187 | |
6a776e3a YW |
188 | old_state = acd->state; |
189 | ||
b24ef049 | 190 | ipv4acd_reset(acd); |
e3dca008 | 191 | |
6a776e3a YW |
192 | if (old_state == IPV4ACD_STATE_INIT) |
193 | return 0; | |
194 | ||
b24ef049 | 195 | log_ipv4acd(acd, "STOPPED"); |
e3dca008 | 196 | |
b24ef049 | 197 | ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_STOP); |
e3dca008 TG |
198 | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static int ipv4acd_on_timeout(sd_event_source *s, uint64_t usec, void *userdata); | |
203 | ||
b24ef049 | 204 | static int ipv4acd_set_next_wakeup(sd_ipv4acd *acd, usec_t usec, usec_t random_usec) { |
e3f4eedb | 205 | usec_t next_timeout, time_now; |
e3dca008 | 206 | |
b24ef049 | 207 | assert(acd); |
e3dca008 | 208 | |
e3f4eedb | 209 | next_timeout = usec; |
e3dca008 | 210 | |
e3f4eedb LP |
211 | if (random_usec > 0) |
212 | next_timeout += (usec_t) random_u64() % random_usec; | |
e3dca008 | 213 | |
ba4e0427 | 214 | assert_se(sd_event_now(acd->event, CLOCK_BOOTTIME, &time_now) >= 0); |
e3dca008 | 215 | |
32ab66c5 | 216 | return event_reset_time(acd->event, &acd->timer_event_source, |
ba4e0427 | 217 | CLOCK_BOOTTIME, |
32ab66c5 YW |
218 | time_now + next_timeout, 0, |
219 | ipv4acd_on_timeout, acd, | |
220 | acd->event_priority, "ipv4acd-timer", true); | |
e3dca008 TG |
221 | } |
222 | ||
e3dca008 | 223 | static int ipv4acd_on_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
99534007 | 224 | sd_ipv4acd *acd = ASSERT_PTR(userdata); |
e3dca008 TG |
225 | int r = 0; |
226 | ||
b24ef049 | 227 | switch (acd->state) { |
e3dca008 | 228 | |
c9e458a4 | 229 | case IPV4ACD_STATE_STARTED: |
3d817902 YW |
230 | acd->defend_window = 0; |
231 | ||
c2691d8e BG |
232 | log_ipv4acd(acd, |
233 | "Started on address " IPV4_ADDRESS_FMT_STR " with a max timeout of %s", | |
234 | IPV4_ADDRESS_FMT_VAL(acd->address), | |
235 | FORMAT_TIMESPAN(TOTAL_TIME_UNITS * acd->time_unit_usec, USEC_PER_MSEC)); | |
236 | ||
b24ef049 | 237 | ipv4acd_set_state(acd, IPV4ACD_STATE_WAITING_PROBE, true); |
e3dca008 | 238 | |
b24ef049 | 239 | if (acd->n_conflict >= MAX_CONFLICTS) { |
3d817902 | 240 | log_ipv4acd(acd, "Max conflicts reached, delaying by %s", |
5291f26d | 241 | FORMAT_TIMESPAN(RATE_LIMIT_INTERVAL_USEC, 0)); |
c2691d8e BG |
242 | r = ipv4acd_set_next_wakeup( |
243 | acd, RATE_LIMIT_INTERVAL_USEC, PROBE_WAIT * acd->time_unit_usec); | |
3d817902 | 244 | } else |
c2691d8e | 245 | r = ipv4acd_set_next_wakeup(acd, 0, PROBE_WAIT * acd->time_unit_usec); |
3d817902 YW |
246 | if (r < 0) |
247 | goto fail; | |
e3dca008 TG |
248 | |
249 | break; | |
4dbf7b3a | 250 | |
e3dca008 TG |
251 | case IPV4ACD_STATE_WAITING_PROBE: |
252 | case IPV4ACD_STATE_PROBING: | |
253 | /* Send a probe */ | |
ecad63f8 | 254 | r = arp_send_probe(acd->fd, acd->ifindex, &acd->address, &acd->mac_addr); |
e3dca008 | 255 | if (r < 0) { |
b24ef049 | 256 | log_ipv4acd_errno(acd, r, "Failed to send ARP probe: %m"); |
ff0c5ebd | 257 | goto fail; |
e3dca008 TG |
258 | } |
259 | ||
9be2ba5e YW |
260 | log_ipv4acd(acd, "Probing "IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(acd->address)); |
261 | ||
b24ef049 LP |
262 | if (acd->n_iteration < PROBE_NUM - 2) { |
263 | ipv4acd_set_state(acd, IPV4ACD_STATE_PROBING, false); | |
e3dca008 | 264 | |
c2691d8e BG |
265 | r = ipv4acd_set_next_wakeup( |
266 | acd, | |
267 | PROBE_MIN * acd->time_unit_usec, | |
268 | (PROBE_MAX - PROBE_MIN) * acd->time_unit_usec); | |
e3dca008 | 269 | if (r < 0) |
ff0c5ebd | 270 | goto fail; |
e3dca008 | 271 | } else { |
b24ef049 | 272 | ipv4acd_set_state(acd, IPV4ACD_STATE_WAITING_ANNOUNCE, true); |
e3dca008 | 273 | |
c2691d8e | 274 | r = ipv4acd_set_next_wakeup(acd, ANNOUNCE_WAIT * acd->time_unit_usec, 0); |
e3dca008 | 275 | if (r < 0) |
ff0c5ebd | 276 | goto fail; |
e3dca008 TG |
277 | } |
278 | ||
279 | break; | |
280 | ||
281 | case IPV4ACD_STATE_ANNOUNCING: | |
b24ef049 LP |
282 | if (acd->n_iteration >= ANNOUNCE_NUM - 1) { |
283 | ipv4acd_set_state(acd, IPV4ACD_STATE_RUNNING, false); | |
e3dca008 TG |
284 | break; |
285 | } | |
4dbf7b3a | 286 | |
4831981d | 287 | _fallthrough_; |
e3dca008 TG |
288 | case IPV4ACD_STATE_WAITING_ANNOUNCE: |
289 | /* Send announcement packet */ | |
ecad63f8 | 290 | r = arp_send_announcement(acd->fd, acd->ifindex, &acd->address, &acd->mac_addr); |
e3dca008 | 291 | if (r < 0) { |
b24ef049 | 292 | log_ipv4acd_errno(acd, r, "Failed to send ARP announcement: %m"); |
ff0c5ebd | 293 | goto fail; |
265b6d4e YW |
294 | } |
295 | ||
296 | log_ipv4acd(acd, "Announcing "IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(acd->address)); | |
e3dca008 | 297 | |
b24ef049 | 298 | ipv4acd_set_state(acd, IPV4ACD_STATE_ANNOUNCING, false); |
e3dca008 | 299 | |
b24ef049 | 300 | r = ipv4acd_set_next_wakeup(acd, ANNOUNCE_INTERVAL_USEC, 0); |
e3dca008 | 301 | if (r < 0) |
ff0c5ebd | 302 | goto fail; |
e3dca008 | 303 | |
b24ef049 LP |
304 | if (acd->n_iteration == 0) { |
305 | acd->n_conflict = 0; | |
306 | ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_BIND); | |
e3dca008 TG |
307 | } |
308 | ||
309 | break; | |
4dbf7b3a | 310 | |
e3dca008 | 311 | default: |
04499a70 | 312 | assert_not_reached(); |
e3dca008 TG |
313 | } |
314 | ||
ff0c5ebd | 315 | return 0; |
e3dca008 | 316 | |
ff0c5ebd | 317 | fail: |
b24ef049 | 318 | sd_ipv4acd_stop(acd); |
ff0c5ebd | 319 | return 0; |
e3dca008 TG |
320 | } |
321 | ||
7f77917c YW |
322 | static bool ipv4acd_arp_conflict(sd_ipv4acd *acd, const struct ether_arp *arp, bool announced) { |
323 | assert(acd); | |
324 | assert(arp); | |
325 | ||
326 | /* RFC 5227 section 2.1.1. | |
327 | * "the host receives any ARP packet (Request *or* Reply) on the interface where the probe is | |
328 | * being performed, where the packet's 'sender IP address' is the address being probed for, | |
329 | * then the host MUST treat this address as being in use by some other host" */ | |
330 | if (memcmp(arp->arp_spa, &acd->address, sizeof(struct in_addr)) == 0) | |
331 | return true; | |
332 | ||
333 | if (announced) | |
334 | /* the TPA matched instead of SPA, this is not a conflict */ | |
335 | return false; | |
336 | ||
337 | /* "any ARP Probe where the packet's 'target IP address' is the address being probed for, and | |
338 | * the packet's 'sender hardware address' is not the hardware address of any of the host's | |
339 | * interfaces, then the host SHOULD similarly treat this as an address conflict" */ | |
340 | if (arp->ea_hdr.ar_op != htobe16(ARPOP_REQUEST)) | |
341 | return false; /* not ARP Request, ignoring. */ | |
342 | if (memeqzero(arp->arp_spa, sizeof(struct in_addr)) == 0) | |
343 | return false; /* not ARP Probe, ignoring. */ | |
344 | if (memcmp(arp->arp_tpa, &acd->address, sizeof(struct in_addr)) != 0) | |
345 | return false; /* target IP address does not match, BPF code is broken? */ | |
346 | ||
347 | if (acd->check_mac_callback && | |
348 | acd->check_mac_callback(acd, (const struct ether_addr*) arp->arp_sha, acd->check_mac_userdata) > 0) | |
349 | /* sender hardware is one of the host's interfaces, ignoring. */ | |
239adf03 | 350 | return false; |
7f77917c YW |
351 | |
352 | return true; /* conflict! */ | |
353 | } | |
354 | ||
b24ef049 | 355 | static void ipv4acd_on_conflict(sd_ipv4acd *acd) { |
b24ef049 | 356 | assert(acd); |
e3dca008 | 357 | |
b24ef049 | 358 | acd->n_conflict++; |
e3dca008 | 359 | |
9be2ba5e | 360 | log_ipv4acd(acd, "Conflict on "IPV4_ADDRESS_FMT_STR" (%u)", IPV4_ADDRESS_FMT_VAL(acd->address), acd->n_conflict); |
e3dca008 | 361 | |
b24ef049 LP |
362 | ipv4acd_reset(acd); |
363 | ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_CONFLICT); | |
e3dca008 TG |
364 | } |
365 | ||
e095f51d LP |
366 | static int ipv4acd_on_packet( |
367 | sd_event_source *s, | |
368 | int fd, | |
369 | uint32_t revents, | |
370 | void *userdata) { | |
371 | ||
99534007 | 372 | sd_ipv4acd *acd = ASSERT_PTR(userdata); |
e3dca008 | 373 | struct ether_arp packet; |
e095f51d | 374 | ssize_t n; |
e3dca008 TG |
375 | int r; |
376 | ||
e095f51d | 377 | assert(s); |
e3dca008 TG |
378 | assert(fd >= 0); |
379 | ||
e095f51d LP |
380 | n = recv(fd, &packet, sizeof(struct ether_arp), 0); |
381 | if (n < 0) { | |
ab8a8a4e | 382 | if (ERRNO_IS_TRANSIENT(errno) || ERRNO_IS_DISCONNECT(errno)) |
004845d1 LP |
383 | return 0; |
384 | ||
b24ef049 | 385 | log_ipv4acd_errno(acd, errno, "Failed to read ARP packet: %m"); |
ff0c5ebd | 386 | goto fail; |
e095f51d LP |
387 | } |
388 | if ((size_t) n != sizeof(struct ether_arp)) { | |
b24ef049 | 389 | log_ipv4acd(acd, "Ignoring too short ARP packet."); |
e095f51d LP |
390 | return 0; |
391 | } | |
e3dca008 | 392 | |
b24ef049 | 393 | switch (acd->state) { |
e095f51d | 394 | |
e3dca008 TG |
395 | case IPV4ACD_STATE_ANNOUNCING: |
396 | case IPV4ACD_STATE_RUNNING: | |
e095f51d | 397 | |
7f77917c | 398 | if (ipv4acd_arp_conflict(acd, &packet, true)) { |
e3dca008 TG |
399 | usec_t ts; |
400 | ||
ba4e0427 | 401 | assert_se(sd_event_now(acd->event, CLOCK_BOOTTIME, &ts) >= 0); |
e3dca008 TG |
402 | |
403 | /* Defend address */ | |
b24ef049 LP |
404 | if (ts > acd->defend_window) { |
405 | acd->defend_window = ts + DEFEND_INTERVAL_USEC; | |
ecad63f8 | 406 | r = arp_send_announcement(acd->fd, acd->ifindex, &acd->address, &acd->mac_addr); |
e3dca008 | 407 | if (r < 0) { |
b24ef049 | 408 | log_ipv4acd_errno(acd, r, "Failed to send ARP announcement: %m"); |
ff0c5ebd | 409 | goto fail; |
265b6d4e YW |
410 | } |
411 | ||
412 | log_ipv4acd(acd, "Defending "IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(acd->address)); | |
e3dca008 TG |
413 | |
414 | } else | |
b24ef049 | 415 | ipv4acd_on_conflict(acd); |
e3dca008 | 416 | } |
e3dca008 | 417 | break; |
e095f51d | 418 | |
146b44d0 | 419 | case IPV4ACD_STATE_STARTED: |
e3dca008 TG |
420 | case IPV4ACD_STATE_WAITING_PROBE: |
421 | case IPV4ACD_STATE_PROBING: | |
422 | case IPV4ACD_STATE_WAITING_ANNOUNCE: | |
7f77917c YW |
423 | if (ipv4acd_arp_conflict(acd, &packet, false)) |
424 | ipv4acd_on_conflict(acd); | |
e3dca008 | 425 | break; |
e095f51d | 426 | |
e3dca008 | 427 | default: |
04499a70 | 428 | assert_not_reached(); |
e3dca008 TG |
429 | } |
430 | ||
ff0c5ebd | 431 | return 0; |
e3dca008 | 432 | |
ff0c5ebd | 433 | fail: |
b24ef049 | 434 | sd_ipv4acd_stop(acd); |
ff0c5ebd | 435 | return 0; |
e3dca008 TG |
436 | } |
437 | ||
b24ef049 LP |
438 | int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int ifindex) { |
439 | assert_return(acd, -EINVAL); | |
2f8e7633 | 440 | assert_return(ifindex > 0, -EINVAL); |
b24ef049 | 441 | assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY); |
e3dca008 | 442 | |
b24ef049 | 443 | acd->ifindex = ifindex; |
e3dca008 TG |
444 | |
445 | return 0; | |
446 | } | |
447 | ||
99b06a2f YW |
448 | int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd) { |
449 | if (!acd) | |
450 | return -EINVAL; | |
451 | ||
452 | return acd->ifindex; | |
453 | } | |
454 | ||
61a9fa8f YW |
455 | int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *ifname) { |
456 | assert_return(acd, -EINVAL); | |
457 | assert_return(ifname, -EINVAL); | |
458 | ||
459 | if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) | |
460 | return -EINVAL; | |
461 | ||
462 | return free_and_strdup(&acd->ifname, ifname); | |
463 | } | |
464 | ||
c2691d8e BG |
465 | int sd_ipv4acd_set_timeout(sd_ipv4acd *acd, uint64_t usec) { |
466 | assert_return(acd, -EINVAL); | |
467 | ||
468 | if (usec == 0) | |
469 | usec = DEFAULT_ACD_TIMEOUT_USEC; | |
470 | ||
471 | /* Clamp the total duration to a value between 1ms and 1 minute */ | |
472 | acd->time_unit_usec = DIV_ROUND_UP( | |
473 | CLAMP(usec, 1U * USEC_PER_MSEC, 1U * USEC_PER_MINUTE), TOTAL_TIME_UNITS); | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
5977b71f YW |
478 | int sd_ipv4acd_get_ifname(sd_ipv4acd *acd, const char **ret) { |
479 | int r; | |
480 | ||
481 | assert_return(acd, -EINVAL); | |
99b06a2f | 482 | |
5977b71f YW |
483 | r = get_ifname(acd->ifindex, &acd->ifname); |
484 | if (r < 0) | |
485 | return r; | |
486 | ||
487 | if (ret) | |
488 | *ret = acd->ifname; | |
489 | ||
490 | return 0; | |
99b06a2f YW |
491 | } |
492 | ||
b24ef049 | 493 | int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) { |
fcb73459 YW |
494 | int r; |
495 | ||
b24ef049 | 496 | assert_return(acd, -EINVAL); |
e3dca008 | 497 | assert_return(addr, -EINVAL); |
fcb73459 | 498 | assert_return(!ether_addr_is_null(addr), -EINVAL); |
e3dca008 | 499 | |
b24ef049 | 500 | acd->mac_addr = *addr; |
e3dca008 | 501 | |
fcb73459 YW |
502 | if (!sd_ipv4acd_is_running(acd)) |
503 | return 0; | |
504 | ||
505 | assert(acd->fd >= 0); | |
506 | r = arp_update_filter(acd->fd, &acd->address, &acd->mac_addr); | |
507 | if (r < 0) { | |
508 | ipv4acd_reset(acd); | |
509 | return r; | |
510 | } | |
511 | ||
e3dca008 TG |
512 | return 0; |
513 | } | |
514 | ||
b24ef049 LP |
515 | int sd_ipv4acd_detach_event(sd_ipv4acd *acd) { |
516 | assert_return(acd, -EINVAL); | |
e3dca008 | 517 | |
b24ef049 | 518 | acd->event = sd_event_unref(acd->event); |
e3dca008 TG |
519 | |
520 | return 0; | |
521 | } | |
522 | ||
b24ef049 | 523 | int sd_ipv4acd_attach_event(sd_ipv4acd *acd, sd_event *event, int64_t priority) { |
e3dca008 TG |
524 | int r; |
525 | ||
b24ef049 LP |
526 | assert_return(acd, -EINVAL); |
527 | assert_return(!acd->event, -EBUSY); | |
e3dca008 TG |
528 | |
529 | if (event) | |
b24ef049 | 530 | acd->event = sd_event_ref(event); |
e3dca008 | 531 | else { |
b24ef049 | 532 | r = sd_event_default(&acd->event); |
e3dca008 TG |
533 | if (r < 0) |
534 | return r; | |
535 | } | |
536 | ||
b24ef049 | 537 | acd->event_priority = priority; |
e3dca008 TG |
538 | |
539 | return 0; | |
540 | } | |
541 | ||
b24ef049 LP |
542 | int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *userdata) { |
543 | assert_return(acd, -EINVAL); | |
e3dca008 | 544 | |
b24ef049 LP |
545 | acd->callback = cb; |
546 | acd->userdata = userdata; | |
e3dca008 TG |
547 | |
548 | return 0; | |
549 | } | |
550 | ||
7f77917c YW |
551 | int sd_ipv4acd_set_check_mac_callback(sd_ipv4acd *acd, sd_ipv4acd_check_mac_callback_t cb, void *userdata) { |
552 | assert_return(acd, -EINVAL); | |
553 | ||
554 | acd->check_mac_callback = cb; | |
555 | acd->check_mac_userdata = userdata; | |
556 | return 0; | |
557 | } | |
558 | ||
b24ef049 | 559 | int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address) { |
5c35c13a YW |
560 | int r; |
561 | ||
b24ef049 | 562 | assert_return(acd, -EINVAL); |
e3dca008 | 563 | assert_return(address, -EINVAL); |
9be2ba5e | 564 | assert_return(in4_addr_is_set(address), -EINVAL); |
5c35c13a YW |
565 | |
566 | if (in4_addr_equal(&acd->address, address)) | |
567 | return 0; | |
e3dca008 | 568 | |
9be2ba5e | 569 | acd->address = *address; |
e3dca008 | 570 | |
5c35c13a YW |
571 | if (!sd_ipv4acd_is_running(acd)) |
572 | return 0; | |
573 | ||
574 | assert(acd->fd >= 0); | |
575 | r = arp_update_filter(acd->fd, &acd->address, &acd->mac_addr); | |
576 | if (r < 0) | |
577 | goto fail; | |
578 | ||
579 | r = ipv4acd_set_next_wakeup(acd, 0, 0); | |
580 | if (r < 0) | |
581 | goto fail; | |
582 | ||
583 | ipv4acd_set_state(acd, IPV4ACD_STATE_STARTED, true); | |
e3dca008 | 584 | return 0; |
5c35c13a YW |
585 | |
586 | fail: | |
587 | ipv4acd_reset(acd); | |
588 | return r; | |
e3dca008 TG |
589 | } |
590 | ||
4dd6a3aa YW |
591 | int sd_ipv4acd_get_address(sd_ipv4acd *acd, struct in_addr *address) { |
592 | assert_return(acd, -EINVAL); | |
593 | assert_return(address, -EINVAL); | |
594 | ||
9be2ba5e | 595 | *address = acd->address; |
4dd6a3aa YW |
596 | |
597 | return 0; | |
598 | } | |
599 | ||
b24ef049 | 600 | int sd_ipv4acd_is_running(sd_ipv4acd *acd) { |
8e91738f YW |
601 | if (!acd) |
602 | return false; | |
e3dca008 | 603 | |
b24ef049 | 604 | return acd->state != IPV4ACD_STATE_INIT; |
e3dca008 TG |
605 | } |
606 | ||
626d653a YW |
607 | int sd_ipv4acd_is_bound(sd_ipv4acd *acd) { |
608 | assert_return(acd, false); | |
609 | ||
610 | return IN_SET(acd->state, IPV4ACD_STATE_ANNOUNCING, IPV4ACD_STATE_RUNNING); | |
611 | } | |
612 | ||
e92b60b2 | 613 | int sd_ipv4acd_start(sd_ipv4acd *acd, bool reset_conflicts) { |
e3dca008 TG |
614 | int r; |
615 | ||
b24ef049 LP |
616 | assert_return(acd, -EINVAL); |
617 | assert_return(acd->event, -EINVAL); | |
618 | assert_return(acd->ifindex > 0, -EINVAL); | |
9be2ba5e | 619 | assert_return(in4_addr_is_set(&acd->address), -EINVAL); |
b24ef049 LP |
620 | assert_return(!ether_addr_is_null(&acd->mac_addr), -EINVAL); |
621 | assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY); | |
e3dca008 | 622 | |
f67208a1 YW |
623 | r = sd_event_get_state(acd->event); |
624 | if (r < 0) | |
625 | return r; | |
626 | if (r == SD_EVENT_FINISHED) | |
627 | return -ESTALE; | |
628 | ||
ecad63f8 | 629 | r = arp_network_bind_raw_socket(acd->ifindex, &acd->address, &acd->mac_addr); |
e3dca008 | 630 | if (r < 0) |
d246e77a | 631 | return r; |
e3dca008 | 632 | |
ee3455cf | 633 | close_and_replace(acd->fd, r); |
e92b60b2 AB |
634 | |
635 | if (reset_conflicts) | |
636 | acd->n_conflict = 0; | |
e3dca008 | 637 | |
b24ef049 | 638 | r = sd_event_add_io(acd->event, &acd->receive_message_event_source, acd->fd, EPOLLIN, ipv4acd_on_packet, acd); |
e3dca008 | 639 | if (r < 0) |
d246e77a | 640 | goto fail; |
e3dca008 | 641 | |
b24ef049 | 642 | r = sd_event_source_set_priority(acd->receive_message_event_source, acd->event_priority); |
e3dca008 | 643 | if (r < 0) |
d246e77a | 644 | goto fail; |
e3dca008 | 645 | |
b24ef049 | 646 | (void) sd_event_source_set_description(acd->receive_message_event_source, "ipv4acd-receive-message"); |
e3dca008 | 647 | |
b24ef049 | 648 | r = ipv4acd_set_next_wakeup(acd, 0, 0); |
e3dca008 | 649 | if (r < 0) |
d246e77a | 650 | goto fail; |
e3dca008 | 651 | |
b24ef049 | 652 | ipv4acd_set_state(acd, IPV4ACD_STATE_STARTED, true); |
e3dca008 | 653 | return 0; |
d246e77a LP |
654 | |
655 | fail: | |
b24ef049 | 656 | ipv4acd_reset(acd); |
d246e77a | 657 | return r; |
e3dca008 | 658 | } |