2015-08-21 12:50:31 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright (C) 2014 Axis Communications AB. All rights reserved.
|
|
|
|
Copyright (C) 2015 Tom Gundersen
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-ipv4acd.h"
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "arp-util.h"
|
2016-05-22 14:26:06 +02:00
|
|
|
#include "ether-addr-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-08-21 12:50:31 +02:00
|
|
|
#include "in-addr-util.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "random-util.h"
|
|
|
|
#include "siphash24.h"
|
2016-05-23 17:52:08 +02:00
|
|
|
#include "string-util.h"
|
2015-08-21 12:50:31 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
/* Constants from the RFC */
|
2016-05-23 18:18:04 +02:00
|
|
|
#define PROBE_WAIT_USEC (1U * USEC_PER_SEC)
|
|
|
|
#define PROBE_NUM 3U
|
|
|
|
#define PROBE_MIN_USEC (1U * USEC_PER_SEC)
|
|
|
|
#define PROBE_MAX_USEC (2U * USEC_PER_SEC)
|
|
|
|
#define ANNOUNCE_WAIT_USEC (2U * USEC_PER_SEC)
|
|
|
|
#define ANNOUNCE_NUM 2U
|
|
|
|
#define ANNOUNCE_INTERVAL_USEC (2U * USEC_PER_SEC)
|
|
|
|
#define MAX_CONFLICTS 10U
|
|
|
|
#define RATE_LIMIT_INTERVAL_USEC (60U * USEC_PER_SEC)
|
|
|
|
#define DEFEND_INTERVAL_USEC (10U * USEC_PER_SEC)
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
typedef enum IPv4ACDState {
|
|
|
|
IPV4ACD_STATE_INIT,
|
2016-05-23 18:06:33 +02:00
|
|
|
IPV4ACD_STATE_STARTED,
|
2015-08-21 12:50:31 +02:00
|
|
|
IPV4ACD_STATE_WAITING_PROBE,
|
|
|
|
IPV4ACD_STATE_PROBING,
|
|
|
|
IPV4ACD_STATE_WAITING_ANNOUNCE,
|
|
|
|
IPV4ACD_STATE_ANNOUNCING,
|
|
|
|
IPV4ACD_STATE_RUNNING,
|
|
|
|
_IPV4ACD_STATE_MAX,
|
|
|
|
_IPV4ACD_STATE_INVALID = -1
|
|
|
|
} IPv4ACDState;
|
|
|
|
|
|
|
|
struct sd_ipv4acd {
|
2016-05-23 17:17:37 +02:00
|
|
|
unsigned n_ref;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
IPv4ACDState state;
|
2016-05-23 16:13:18 +02:00
|
|
|
int ifindex;
|
2015-08-21 12:50:31 +02:00
|
|
|
int fd;
|
2016-05-23 17:23:40 +02:00
|
|
|
|
|
|
|
unsigned n_iteration;
|
|
|
|
unsigned n_conflict;
|
|
|
|
|
2016-05-23 17:32:18 +02:00
|
|
|
sd_event_source *receive_message_event_source;
|
|
|
|
sd_event_source *timer_event_source;
|
2016-05-23 17:23:40 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
usec_t defend_window;
|
|
|
|
be32_t address;
|
2016-05-23 17:23:40 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
/* External */
|
|
|
|
struct ether_addr mac_addr;
|
2016-05-23 17:23:40 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
sd_event *event;
|
|
|
|
int event_priority;
|
2016-05-23 16:48:56 +02:00
|
|
|
sd_ipv4acd_callback_t callback;
|
2015-08-21 12:50:31 +02:00
|
|
|
void* userdata;
|
|
|
|
};
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
#define log_ipv4acd_errno(acd, error, fmt, ...) log_internal(LOG_DEBUG, error, __FILE__, __LINE__, __func__, "IPV4ACD: " fmt, ##__VA_ARGS__)
|
|
|
|
#define log_ipv4acd(acd, fmt, ...) log_ipv4acd_errno(acd, 0, fmt, ##__VA_ARGS__)
|
2016-05-23 19:35:18 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
static void ipv4acd_set_state(sd_ipv4acd *acd, IPv4ACDState st, bool reset_counter) {
|
|
|
|
assert(acd);
|
2016-05-23 17:52:08 +02:00
|
|
|
assert(st < _IPV4ACD_STATE_MAX);
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (st == acd->state && !reset_counter)
|
|
|
|
acd->n_iteration++;
|
2016-05-23 17:52:08 +02:00
|
|
|
else {
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->state = st;
|
|
|
|
acd->n_iteration = 0;
|
2016-05-23 17:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
static void ipv4acd_reset(sd_ipv4acd *acd) {
|
|
|
|
assert(acd);
|
2016-05-23 17:52:08 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->timer_event_source = sd_event_source_unref(acd->timer_event_source);
|
|
|
|
acd->receive_message_event_source = sd_event_source_unref(acd->receive_message_event_source);
|
2016-05-23 17:52:08 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->fd = safe_close(acd->fd);
|
2016-05-23 17:52:08 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_INIT, true);
|
2016-05-23 17:52:08 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_ipv4acd *sd_ipv4acd_ref(sd_ipv4acd *acd) {
|
|
|
|
if (!acd)
|
2016-05-23 17:17:37 +02:00
|
|
|
return NULL;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_se(acd->n_ref >= 1);
|
|
|
|
acd->n_ref++;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
return acd;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_ipv4acd *sd_ipv4acd_unref(sd_ipv4acd *acd) {
|
|
|
|
if (!acd)
|
2016-05-23 17:17:37 +02:00
|
|
|
return NULL;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_se(acd->n_ref >= 1);
|
|
|
|
acd->n_ref--;
|
2016-05-23 17:17:37 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (acd->n_ref > 0)
|
2015-08-21 12:50:31 +02:00
|
|
|
return NULL;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_reset(acd);
|
|
|
|
sd_ipv4acd_detach_event(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-10-17 00:28:30 +02:00
|
|
|
return mfree(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sd_ipv4acd_new(sd_ipv4acd **ret) {
|
2016-05-23 19:39:44 +02:00
|
|
|
_cleanup_(sd_ipv4acd_unrefp) sd_ipv4acd *acd = NULL;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
assert_return(ret, -EINVAL);
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd = new0(sd_ipv4acd, 1);
|
|
|
|
if (!acd)
|
2015-08-21 12:50:31 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->n_ref = 1;
|
|
|
|
acd->state = IPV4ACD_STATE_INIT;
|
|
|
|
acd->ifindex = -1;
|
|
|
|
acd->fd = -1;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
*ret = acd;
|
|
|
|
acd = NULL;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
static void ipv4acd_client_notify(sd_ipv4acd *acd, int event) {
|
|
|
|
assert(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (!acd->callback)
|
2016-05-22 14:26:36 +02:00
|
|
|
return;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->callback(acd, event, acd->userdata);
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_stop(sd_ipv4acd *acd) {
|
|
|
|
assert_return(acd, -EINVAL);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_reset(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "STOPPED");
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_STOP);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipv4acd_on_timeout(sd_event_source *s, uint64_t usec, void *userdata);
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
static int ipv4acd_set_next_wakeup(sd_ipv4acd *acd, usec_t usec, usec_t random_usec) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_event_source_unrefp) sd_event_source *timer = NULL;
|
2016-05-23 18:18:04 +02:00
|
|
|
usec_t next_timeout, time_now;
|
2015-08-21 12:50:31 +02:00
|
|
|
int r;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 18:18:04 +02:00
|
|
|
next_timeout = usec;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 18:18:04 +02:00
|
|
|
if (random_usec > 0)
|
|
|
|
next_timeout += (usec_t) random_u64() % random_usec;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_se(sd_event_now(acd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = sd_event_add_time(acd->event, &timer, clock_boottime_or_monotonic(), time_now + next_timeout, 0, ipv4acd_on_timeout, acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = sd_event_source_set_priority(timer, acd->event_priority);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2016-05-23 17:32:18 +02:00
|
|
|
(void) sd_event_source_set_description(timer, "ipv4acd-timer");
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_event_source_unref(acd->timer_event_source);
|
|
|
|
acd->timer_event_source = timer;
|
2015-08-21 12:50:31 +02:00
|
|
|
timer = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
static bool ipv4acd_arp_conflict(sd_ipv4acd *acd, struct ether_arp *arp) {
|
|
|
|
assert(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
assert(arp);
|
|
|
|
|
|
|
|
/* see the BPF */
|
2016-05-23 19:39:44 +02:00
|
|
|
if (memcmp(arp->arp_spa, &acd->address, sizeof(acd->address)) == 0)
|
2015-08-21 12:50:31 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* the TPA matched instead of the SPA, this is not a conflict */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipv4acd_on_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_ipv4acd *acd = userdata;
|
2015-08-21 12:50:31 +02:00
|
|
|
int r = 0;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
switch (acd->state) {
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 18:06:33 +02:00
|
|
|
case IPV4ACD_STATE_STARTED:
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_WAITING_PROBE, true);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (acd->n_conflict >= MAX_CONFLICTS) {
|
2016-05-23 18:18:04 +02:00
|
|
|
char ts[FORMAT_TIMESPAN_MAX];
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "Max conflicts reached, delaying by %s", format_timespan(ts, sizeof(ts), RATE_LIMIT_INTERVAL_USEC, 0));
|
2016-05-23 17:18:58 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = ipv4acd_set_next_wakeup(acd, RATE_LIMIT_INTERVAL_USEC, PROBE_WAIT_USEC);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
} else {
|
2016-05-23 19:39:44 +02:00
|
|
|
r = ipv4acd_set_next_wakeup(acd, 0, PROBE_WAIT_USEC);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-05-23 17:32:18 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
case IPV4ACD_STATE_WAITING_PROBE:
|
|
|
|
case IPV4ACD_STATE_PROBING:
|
|
|
|
/* Send a probe */
|
2016-05-23 19:39:44 +02:00
|
|
|
r = arp_send_probe(acd->fd, acd->ifindex, acd->address, &acd->mac_addr);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0) {
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd_errno(acd, r, "Failed to send ARP probe: %m");
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
} else {
|
|
|
|
_cleanup_free_ char *address = NULL;
|
2016-05-23 19:39:44 +02:00
|
|
|
union in_addr_union addr = { .in.s_addr = acd->address };
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 17:52:08 +02:00
|
|
|
(void) in_addr_to_string(AF_INET, &addr, &address);
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "Probing %s", strna(address));
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (acd->n_iteration < PROBE_NUM - 2) {
|
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_PROBING, false);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = ipv4acd_set_next_wakeup(acd, PROBE_MIN_USEC, (PROBE_MAX_USEC-PROBE_MIN_USEC));
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
} else {
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_WAITING_ANNOUNCE, true);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = ipv4acd_set_next_wakeup(acd, ANNOUNCE_WAIT_USEC, 0);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPV4ACD_STATE_ANNOUNCING:
|
2016-05-23 19:39:44 +02:00
|
|
|
if (acd->n_iteration >= ANNOUNCE_NUM - 1) {
|
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_RUNNING, false);
|
2015-08-21 12:50:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-05-23 17:32:18 +02:00
|
|
|
|
|
|
|
/* fall through */
|
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
case IPV4ACD_STATE_WAITING_ANNOUNCE:
|
|
|
|
/* Send announcement packet */
|
2016-05-23 19:39:44 +02:00
|
|
|
r = arp_send_announcement(acd->fd, acd->ifindex, acd->address, &acd->mac_addr);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0) {
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd_errno(acd, r, "Failed to send ARP announcement: %m");
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
} else
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "ANNOUNCE");
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_ANNOUNCING, false);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = ipv4acd_set_next_wakeup(acd, ANNOUNCE_INTERVAL_USEC, 0);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (acd->n_iteration == 0) {
|
|
|
|
acd->n_conflict = 0;
|
|
|
|
ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_BIND);
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-05-23 17:32:18 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
default:
|
|
|
|
assert_not_reached("Invalid state.");
|
|
|
|
}
|
|
|
|
|
2016-05-23 18:26:49 +02:00
|
|
|
return 0;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 18:26:49 +02:00
|
|
|
fail:
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_ipv4acd_stop(acd);
|
2016-05-23 18:26:49 +02:00
|
|
|
return 0;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
static void ipv4acd_on_conflict(sd_ipv4acd *acd) {
|
2015-08-21 12:50:31 +02:00
|
|
|
_cleanup_free_ char *address = NULL;
|
2016-05-23 19:39:44 +02:00
|
|
|
union in_addr_union addr = { .in.s_addr = acd->address };
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->n_conflict++;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 17:52:08 +02:00
|
|
|
(void) in_addr_to_string(AF_INET, &addr, &address);
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "Conflict on %s (%u)", strna(address), acd->n_conflict);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_reset(acd);
|
|
|
|
ipv4acd_client_notify(acd, SD_IPV4ACD_EVENT_CONFLICT);
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 14:26:36 +02:00
|
|
|
static int ipv4acd_on_packet(
|
|
|
|
sd_event_source *s,
|
|
|
|
int fd,
|
|
|
|
uint32_t revents,
|
|
|
|
void *userdata) {
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_ipv4acd *acd = userdata;
|
2015-08-21 12:50:31 +02:00
|
|
|
struct ether_arp packet;
|
2016-05-22 14:26:36 +02:00
|
|
|
ssize_t n;
|
2015-08-21 12:50:31 +02:00
|
|
|
int r;
|
|
|
|
|
2016-05-22 14:26:36 +02:00
|
|
|
assert(s);
|
2016-05-23 19:39:44 +02:00
|
|
|
assert(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
assert(fd >= 0);
|
|
|
|
|
2016-05-22 14:26:36 +02:00
|
|
|
n = recv(fd, &packet, sizeof(struct ether_arp), 0);
|
|
|
|
if (n < 0) {
|
2017-09-29 00:37:23 +02:00
|
|
|
if (IN_SET(errno, EAGAIN, EINTR))
|
2016-05-23 15:56:01 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd_errno(acd, errno, "Failed to read ARP packet: %m");
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2016-05-22 14:26:36 +02:00
|
|
|
}
|
|
|
|
if ((size_t) n != sizeof(struct ether_arp)) {
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "Ignoring too short ARP packet.");
|
2016-05-22 14:26:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
switch (acd->state) {
|
2016-05-22 14:26:36 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
case IPV4ACD_STATE_ANNOUNCING:
|
|
|
|
case IPV4ACD_STATE_RUNNING:
|
2016-05-22 14:26:36 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
if (ipv4acd_arp_conflict(acd, &packet)) {
|
2015-08-21 12:50:31 +02:00
|
|
|
usec_t ts;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_se(sd_event_now(acd->event, clock_boottime_or_monotonic(), &ts) >= 0);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
/* Defend address */
|
2016-05-23 19:39:44 +02:00
|
|
|
if (ts > acd->defend_window) {
|
|
|
|
acd->defend_window = ts + DEFEND_INTERVAL_USEC;
|
|
|
|
r = arp_send_announcement(acd->fd, acd->ifindex, acd->address, &acd->mac_addr);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0) {
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd_errno(acd, r, "Failed to send ARP announcement: %m");
|
2016-05-23 18:26:49 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
} else
|
2016-05-23 19:39:44 +02:00
|
|
|
log_ipv4acd(acd, "DEFEND");
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
} else
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_on_conflict(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-05-22 14:26:36 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
case IPV4ACD_STATE_WAITING_PROBE:
|
|
|
|
case IPV4ACD_STATE_PROBING:
|
|
|
|
case IPV4ACD_STATE_WAITING_ANNOUNCE:
|
|
|
|
/* BPF ensures this packet indicates a conflict */
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_on_conflict(acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
break;
|
2016-05-22 14:26:36 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
default:
|
|
|
|
assert_not_reached("Invalid state.");
|
|
|
|
}
|
|
|
|
|
2016-05-23 18:26:49 +02:00
|
|
|
return 0;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 18:26:49 +02:00
|
|
|
fail:
|
2016-05-23 19:39:44 +02:00
|
|
|
sd_ipv4acd_stop(acd);
|
2016-05-23 18:26:49 +02:00
|
|
|
return 0;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int ifindex) {
|
|
|
|
assert_return(acd, -EINVAL);
|
2016-05-23 16:13:18 +02:00
|
|
|
assert_return(ifindex > 0, -EINVAL);
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->ifindex = ifindex;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) {
|
|
|
|
assert_return(acd, -EINVAL);
|
2015-08-21 12:50:31 +02:00
|
|
|
assert_return(addr, -EINVAL);
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->mac_addr = *addr;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_detach_event(sd_ipv4acd *acd) {
|
|
|
|
assert_return(acd, -EINVAL);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->event = sd_event_unref(acd->event);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_attach_event(sd_ipv4acd *acd, sd_event *event, int64_t priority) {
|
2015-08-21 12:50:31 +02:00
|
|
|
int r;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_return(acd, -EINVAL);
|
|
|
|
assert_return(!acd->event, -EBUSY);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
if (event)
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->event = sd_event_ref(event);
|
2015-08-21 12:50:31 +02:00
|
|
|
else {
|
2016-05-23 19:39:44 +02:00
|
|
|
r = sd_event_default(&acd->event);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->event_priority = priority;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *userdata) {
|
|
|
|
assert_return(acd, -EINVAL);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->callback = cb;
|
|
|
|
acd->userdata = userdata;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address) {
|
|
|
|
assert_return(acd, -EINVAL);
|
2015-08-21 12:50:31 +02:00
|
|
|
assert_return(address, -EINVAL);
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
acd->address = address->s_addr;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_is_running(sd_ipv4acd *acd) {
|
|
|
|
assert_return(acd, false);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
return acd->state != IPV4ACD_STATE_INIT;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
int sd_ipv4acd_start(sd_ipv4acd *acd) {
|
2015-08-21 12:50:31 +02:00
|
|
|
int r;
|
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
assert_return(acd, -EINVAL);
|
|
|
|
assert_return(acd->event, -EINVAL);
|
|
|
|
assert_return(acd->ifindex > 0, -EINVAL);
|
|
|
|
assert_return(acd->address != 0, -EINVAL);
|
|
|
|
assert_return(!ether_addr_is_null(&acd->mac_addr), -EINVAL);
|
|
|
|
assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = arp_network_bind_raw_socket(acd->ifindex, acd->address, &acd->mac_addr);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 17:52:08 +02:00
|
|
|
return r;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
safe_close(acd->fd);
|
|
|
|
acd->fd = r;
|
|
|
|
acd->defend_window = 0;
|
|
|
|
acd->n_conflict = 0;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = sd_event_add_io(acd->event, &acd->receive_message_event_source, acd->fd, EPOLLIN, ipv4acd_on_packet, acd);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 17:52:08 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = sd_event_source_set_priority(acd->receive_message_event_source, acd->event_priority);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 17:52:08 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
(void) sd_event_source_set_description(acd->receive_message_event_source, "ipv4acd-receive-message");
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
r = ipv4acd_set_next_wakeup(acd, 0, 0);
|
2015-08-21 12:50:31 +02:00
|
|
|
if (r < 0)
|
2016-05-23 17:52:08 +02:00
|
|
|
goto fail;
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_set_state(acd, IPV4ACD_STATE_STARTED, true);
|
2015-08-21 12:50:31 +02:00
|
|
|
return 0;
|
2016-05-23 17:52:08 +02:00
|
|
|
|
|
|
|
fail:
|
2016-05-23 19:39:44 +02:00
|
|
|
ipv4acd_reset(acd);
|
2016-05-23 17:52:08 +02:00
|
|
|
return r;
|
2015-08-21 12:50:31 +02:00
|
|
|
}
|