2 This file is part of systemd.
4 Copyright (C) 2014 Axis Communications AB. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <arpa/inet.h>
27 #include "siphash24.h"
31 #include "ipv4ll-internal.h"
32 #include "sd-ipv4ll.h"
34 /* Constants from the RFC */
39 #define ANNOUNCE_WAIT 2
40 #define ANNOUNCE_NUM 2
41 #define ANNOUNCE_INTERVAL 2
42 #define MAX_CONFLICTS 10
43 #define RATE_LIMIT_INTERVAL 60
44 #define DEFEND_INTERVAL 10
46 #define IPV4LL_NETWORK 0xA9FE0000L
47 #define IPV4LL_NETMASK 0xFFFF0000L
49 typedef enum IPv4LLTrigger{
51 IPV4LL_TRIGGER_PACKET,
52 IPV4LL_TRIGGER_TIMEOUT,
54 _IPV4LL_TRIGGER_INVALID = -1
57 typedef enum IPv4LLState {
59 IPV4LL_STATE_WAITING_PROBE,
61 IPV4LL_STATE_WAITING_ANNOUNCE,
62 IPV4LL_STATE_ANNOUNCING,
66 _IPV4LL_STATE_INVALID = -1
75 union sockaddr_union link;
78 sd_event_source *receive_message;
79 sd_event_source *timer;
82 int next_wakeup_valid;
84 struct random_data *random_data;
85 char *random_data_state;
87 be32_t claimed_address;
88 struct ether_addr mac_addr;
95 static void ipv4ll_run_state_machine(sd_ipv4ll *ll, IPv4LLTrigger trigger, void *trigger_data);
97 static void ipv4ll_set_state(sd_ipv4ll *ll, IPv4LLState st, int reset_counter) {
100 assert(st < _IPV4LL_STATE_MAX);
102 if (st == ll->state && !reset_counter) {
110 static sd_ipv4ll *ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
114 ll = sd_ipv4ll_ref(ll);
115 ll->cb(ll, event, ll->userdata);
116 ll = sd_ipv4ll_unref(ll);
122 static sd_ipv4ll *ipv4ll_stop(sd_ipv4ll *ll, int event) {
125 ll->receive_message = sd_event_source_unref(ll->receive_message);
126 ll->fd = safe_close(ll->fd);
128 ll->timer = sd_event_source_unref(ll->timer);
130 log_ipv4ll(ll, "STOPPED");
132 ll = ipv4ll_client_notify(ll, event);
135 ll->claimed_address = 0;
136 ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
142 static int ipv4ll_pick_address(sd_ipv4ll *ll, be32_t *address) {
149 assert(ll->random_data);
152 r = random_r(ll->random_data, &random);
155 addr = htonl((random & 0x0000FFFF) | IPV4LL_NETWORK);
156 } while (addr == ll->address ||
157 (ntohl(addr) & IPV4LL_NETMASK) != IPV4LL_NETWORK ||
158 (ntohl(addr) & 0x0000FF00) == 0x0000 ||
159 (ntohl(addr) & 0x0000FF00) == 0xFF00);
165 static int ipv4ll_timer(sd_event_source *s, uint64_t usec, void *userdata) {
166 sd_ipv4ll *ll = (sd_ipv4ll*)userdata;
170 ll->next_wakeup_valid = 0;
171 ipv4ll_run_state_machine(ll, IPV4LL_TRIGGER_TIMEOUT, NULL);
176 static void ipv4ll_set_next_wakeup(sd_ipv4ll *ll, int sec, int random_sec) {
177 usec_t next_timeout = 0;
181 assert(random_sec >= 0);
184 next_timeout = sec * USEC_PER_SEC;
187 next_timeout += random_u32() % (random_sec * USEC_PER_SEC);
189 if (sd_event_now(ll->event, clock_boottime_or_monotonic(), &time_now) < 0)
190 time_now = now(clock_boottime_or_monotonic());
192 ll->next_wakeup = time_now + next_timeout;
193 ll->next_wakeup_valid = 1;
196 static bool ipv4ll_arp_conflict (sd_ipv4ll *ll, struct ether_arp *arp) {
200 if (memcmp(arp->arp_spa, &ll->address, sizeof(ll->address)) == 0 &&
201 memcmp(arp->arp_sha, &ll->mac_addr, ETH_ALEN) != 0)
207 static bool ipv4ll_arp_probe_conflict (sd_ipv4ll *ll, struct ether_arp *arp) {
211 if (ipv4ll_arp_conflict(ll, arp))
214 if (memcmp(arp->arp_tpa, &ll->address, sizeof(ll->address)) == 0 &&
215 memcmp(arp->arp_sha, &ll->mac_addr, ETH_ALEN))
221 static void ipv4ll_run_state_machine(sd_ipv4ll *ll, IPv4LLTrigger trigger, void *trigger_data) {
222 struct ether_arp out_packet;
223 int out_packet_ready = 0;
227 assert(trigger < _IPV4LL_TRIGGER_MAX);
229 if (ll->state == IPV4LL_STATE_INIT) {
231 log_ipv4ll(ll, "PROBE");
232 ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_PROBE, 1);
233 ipv4ll_set_next_wakeup(ll, 0, PROBE_WAIT);
235 } else if ((ll->state == IPV4LL_STATE_WAITING_PROBE && trigger == IPV4LL_TRIGGER_TIMEOUT) ||
236 (ll->state == IPV4LL_STATE_PROBING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration < PROBE_NUM-2)) {
239 arp_packet_probe(&out_packet, ll->address, &ll->mac_addr);
240 out_packet_ready = 1;
241 ipv4ll_set_state(ll, IPV4LL_STATE_PROBING, 0);
243 ipv4ll_set_next_wakeup(ll, PROBE_MIN, (PROBE_MAX-PROBE_MIN));
245 } else if (ll->state == IPV4LL_STATE_PROBING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration >= PROBE_NUM-2) {
247 /* Send the last probe */
248 arp_packet_probe(&out_packet, ll->address, &ll->mac_addr);
249 out_packet_ready = 1;
250 ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_ANNOUNCE, 1);
252 ipv4ll_set_next_wakeup(ll, ANNOUNCE_WAIT, 0);
254 } else if ((ll->state == IPV4LL_STATE_WAITING_ANNOUNCE && trigger == IPV4LL_TRIGGER_TIMEOUT) ||
255 (ll->state == IPV4LL_STATE_ANNOUNCING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration < ANNOUNCE_NUM-1)) {
257 /* Send announcement packet */
258 arp_packet_announcement(&out_packet, ll->address, &ll->mac_addr);
259 out_packet_ready = 1;
260 ipv4ll_set_state(ll, IPV4LL_STATE_ANNOUNCING, 0);
262 ipv4ll_set_next_wakeup(ll, ANNOUNCE_INTERVAL, 0);
264 if (ll->iteration == 0) {
265 log_ipv4ll(ll, "ANNOUNCE");
266 ll->claimed_address = ll->address;
267 ll = ipv4ll_client_notify(ll, IPV4LL_EVENT_BIND);
268 if (!ll || ll->state == IPV4LL_STATE_STOPPED)
274 } else if ((ll->state == IPV4LL_STATE_ANNOUNCING && trigger == IPV4LL_TRIGGER_TIMEOUT &&
275 ll->iteration >= ANNOUNCE_NUM-1)) {
277 ipv4ll_set_state(ll, IPV4LL_STATE_RUNNING, 0);
278 ll->next_wakeup_valid = 0;
280 } else if (trigger == IPV4LL_TRIGGER_PACKET) {
284 struct ether_arp* in_packet = (struct ether_arp*)trigger_data;
288 if (IN_SET(ll->state, IPV4LL_STATE_ANNOUNCING, IPV4LL_STATE_RUNNING)) {
290 if (ipv4ll_arp_conflict(ll, in_packet)) {
292 r = sd_event_now(ll->event, clock_boottime_or_monotonic(), &time_now);
297 if (time_now > ll->defend_window) {
298 ll->defend_window = time_now + DEFEND_INTERVAL * USEC_PER_SEC;
299 arp_packet_announcement(&out_packet, ll->address, &ll->mac_addr);
300 out_packet_ready = 1;
305 } else if (IN_SET(ll->state, IPV4LL_STATE_WAITING_PROBE,
306 IPV4LL_STATE_PROBING,
307 IPV4LL_STATE_WAITING_ANNOUNCE)) {
309 conflicted = ipv4ll_arp_probe_conflict(ll, in_packet);
313 log_ipv4ll(ll, "CONFLICT");
314 ll = ipv4ll_client_notify(ll, IPV4LL_EVENT_CONFLICT);
315 if (!ll || ll->state == IPV4LL_STATE_STOPPED)
318 ll->claimed_address = 0;
320 /* Pick a new address */
321 r = ipv4ll_pick_address(ll, &ll->address);
325 ll->defend_window = 0;
326 ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_PROBE, 1);
328 if (ll->conflict >= MAX_CONFLICTS) {
329 log_ipv4ll(ll, "MAX_CONFLICTS");
330 ipv4ll_set_next_wakeup(ll, RATE_LIMIT_INTERVAL, PROBE_WAIT);
332 ipv4ll_set_next_wakeup(ll, 0, PROBE_WAIT);
337 if (out_packet_ready) {
338 r = arp_network_send_raw_socket(ll->fd, &ll->link, &out_packet);
340 log_ipv4ll(ll, "failed to send arp packet out");
345 if (ll->next_wakeup_valid) {
346 ll->timer = sd_event_source_unref(ll->timer);
347 r = sd_event_add_time(ll->event, &ll->timer, clock_boottime_or_monotonic(),
348 ll->next_wakeup, 0, ipv4ll_timer, ll);
352 r = sd_event_source_set_priority(ll->timer, ll->event_priority);
356 r = sd_event_source_set_description(ll->timer, "ipv4ll-timer");
366 static int ipv4ll_receive_message(sd_event_source *s, int fd,
367 uint32_t revents, void *userdata) {
369 struct ether_arp arp;
370 sd_ipv4ll *ll = (sd_ipv4ll*)userdata;
374 r = read(fd, &arp, sizeof(struct ether_arp));
375 if (r < (int) sizeof(struct ether_arp))
378 r = arp_packet_verify_headers(&arp);
382 ipv4ll_run_state_machine(ll, IPV4LL_TRIGGER_PACKET, &arp);
387 int sd_ipv4ll_set_index(sd_ipv4ll *ll, int interface_index) {
388 assert_return(ll, -EINVAL);
389 assert_return(interface_index > 0, -EINVAL);
390 assert_return(IN_SET(ll->state, IPV4LL_STATE_INIT,
391 IPV4LL_STATE_STOPPED), -EBUSY);
393 ll->index = interface_index;
398 int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
399 bool need_restart = false;
401 assert_return(ll, -EINVAL);
402 assert_return(addr, -EINVAL);
404 if (memcmp(&ll->mac_addr, addr, ETH_ALEN) == 0)
407 if (!IN_SET(ll->state, IPV4LL_STATE_INIT, IPV4LL_STATE_STOPPED)) {
408 log_ipv4ll(ll, "Changing MAC address on running IPv4LL "
409 "client, restarting");
410 ll = ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
417 memcpy(&ll->mac_addr, addr, ETH_ALEN);
425 int sd_ipv4ll_detach_event(sd_ipv4ll *ll) {
426 assert_return(ll, -EINVAL);
428 ll->event = sd_event_unref(ll->event);
433 int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int priority) {
436 assert_return(ll, -EINVAL);
437 assert_return(!ll->event, -EBUSY);
440 ll->event = sd_event_ref(event);
442 r = sd_event_default(&ll->event);
444 ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
449 ll->event_priority = priority;
454 int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_cb_t cb, void *userdata) {
455 assert_return(ll, -EINVAL);
458 ll->userdata = userdata;
463 int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address){
464 assert_return(ll, -EINVAL);
465 assert_return(address, -EINVAL);
467 if (ll->claimed_address == 0) {
471 address->s_addr = ll->claimed_address;
475 int sd_ipv4ll_set_address_seed (sd_ipv4ll *ll, uint8_t seed[8]) {
476 unsigned int entropy;
479 assert_return(ll, -EINVAL);
480 assert_return(seed, -EINVAL);
484 free(ll->random_data);
485 free(ll->random_data_state);
487 ll->random_data = new0(struct random_data, 1);
488 ll->random_data_state = new0(char, 128);
490 if (!ll->random_data || !ll->random_data_state) {
495 r = initstate_r((unsigned int)entropy, ll->random_data_state, 128, ll->random_data);
501 free(ll->random_data);
502 free(ll->random_data_state);
503 ll->random_data = NULL;
504 ll->random_data_state = NULL;
509 bool sd_ipv4ll_is_running(sd_ipv4ll *ll) {
510 assert_return(ll, -EINVAL);
512 return !IN_SET(ll->state, IPV4LL_STATE_INIT, IPV4LL_STATE_STOPPED);
515 #define HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
517 int sd_ipv4ll_start (sd_ipv4ll *ll) {
520 assert_return(ll, -EINVAL);
521 assert_return(ll->event, -EINVAL);
522 assert_return(ll->index > 0, -EINVAL);
523 assert_return(IN_SET(ll->state, IPV4LL_STATE_INIT,
524 IPV4LL_STATE_STOPPED), -EBUSY);
526 ll->state = IPV4LL_STATE_INIT;
528 r = arp_network_bind_raw_socket(ll->index, &ll->link);
535 ll->defend_window = 0;
536 ll->claimed_address = 0;
538 if (!ll->random_data) {
541 /* Fallback to mac */
542 siphash24(seed, &ll->mac_addr.ether_addr_octet,
543 ETH_ALEN, HASH_KEY.bytes);
545 r = sd_ipv4ll_set_address_seed(ll, seed);
550 if (ll->address == 0) {
551 r = ipv4ll_pick_address(ll, &ll->address);
556 ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
558 r = sd_event_add_io(ll->event, &ll->receive_message, ll->fd,
559 EPOLLIN, ipv4ll_receive_message, ll);
563 r = sd_event_source_set_priority(ll->receive_message, ll->event_priority);
567 r = sd_event_source_set_description(ll->receive_message, "ipv4ll-receive-message");
571 r = sd_event_add_time(ll->event,
573 clock_boottime_or_monotonic(),
574 now(clock_boottime_or_monotonic()), 0,
580 r = sd_event_source_set_priority(ll->timer, ll->event_priority);
584 r = sd_event_source_set_description(ll->timer, "ipv4ll-timer");
587 ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
592 int sd_ipv4ll_stop(sd_ipv4ll *ll) {
593 ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
595 ipv4ll_set_state(ll, IPV4LL_STATE_STOPPED, 1);
600 sd_ipv4ll *sd_ipv4ll_ref(sd_ipv4ll *ll) {
602 assert_se(REFCNT_INC(ll->n_ref) >= 2);
607 sd_ipv4ll *sd_ipv4ll_unref(sd_ipv4ll *ll) {
608 if (ll && REFCNT_DEC(ll->n_ref) == 0) {
609 ll->receive_message =
610 sd_event_source_unref(ll->receive_message);
611 ll->fd = safe_close(ll->fd);
613 ll->timer = sd_event_source_unref(ll->timer);
615 sd_ipv4ll_detach_event(ll);
617 free(ll->random_data);
618 free(ll->random_data_state);
627 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_ipv4ll*, sd_ipv4ll_unref);
628 #define _cleanup_ipv4ll_free_ _cleanup_(sd_ipv4ll_unrefp)
630 int sd_ipv4ll_new(sd_ipv4ll **ret) {
631 _cleanup_ipv4ll_free_ sd_ipv4ll *ll = NULL;
633 assert_return(ret, -EINVAL);
635 ll = new0(sd_ipv4ll, 1);
639 ll->n_ref = REFCNT_INIT;
640 ll->state = IPV4LL_STATE_INIT;