Commit eab389bd authored by Beniamino Galvani's avatar Beniamino Galvani
Browse files

build: update systemd code

This is a direct dump from systemd git on 2015-04-08, git commit
431c3b6bab9c; it updates the existing files and imports systemd IPv4LL
implementation.

    SYSTEMD_DIR=../systemd
    COMMIT=431c3b6bab9ceb54cd144d8df24d764a8a5f8fcc

    (
       cd "$SYSTEMD_DIR"
       git checkout "$COMMIT"
       git reset --hard
       git clean -fdx
    )
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd/sd-id128/sd-id128.c ./src/systemd/src/libsystemd/sd-id128/sd-id128.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-identifier.c ./src/systemd/src/libsystemd-network/dhcp-identifier.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-identifier.h ./src/systemd/src/libsystemd-network/dhcp-identifier.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-internal.h ./src/systemd/src/libsystemd-network/dhcp-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-lease-internal.h ./src/systemd/src/libsystemd-network/dhcp-lease-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-network.c ./src/systemd/src/libsystemd-network/dhcp-network.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-option.c ./src/systemd/src/libsystemd-network/dhcp-option.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-packet.c ./src/systemd/src/libsystemd-network/dhcp-packet.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp-protocol.h ./src/systemd/src/libsystemd-network/dhcp-protocol.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp6-internal.h ./src/systemd/src/libsystemd-network/dhcp6-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp6-lease-internal.h ./src/systemd/src/libsystemd-network/dhcp6-lease-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp6-network.c ./src/systemd/src/libsystemd-network/dhcp6-network.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp6-option.c ./src/systemd/src/libsystemd-network/dhcp6-option.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/dhcp6-protocol.h ./src/systemd/src/libsystemd-network/dhcp6-protocol.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/network-internal.c ./src/systemd/src/libsystemd-network/network-internal.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/network-internal.h ./src/systemd/src/libsystemd-network/network-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/sd-dhcp-client.c ./src/systemd/src/libsystemd-network/sd-dhcp-client.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/sd-dhcp-lease.c ./src/systemd/src/libsystemd-network/sd-dhcp-lease.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/sd-dhcp6-client.c ./src/systemd/src/libsystemd-network/sd-dhcp6-client.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/sd-dhcp6-lease.c ./src/systemd/src/libsystemd-network/sd-dhcp6-lease.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/ipv4ll-internal.h ./src/systemd/src/libsystemd-network/ipv4ll-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/ipv4ll-internal.h ./src/systemd/src/libsystemd-network/ipv4ll-internal.h
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/ipv4ll-network.c ./src/systemd/src/libsystemd-network/ipv4ll-network.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network//ipv4ll-packet.c ./src/systemd/src/libsystemd-network//ipv4ll-packet.c
    /bin/cp "$SYSTEMD_DIR"/src/libsystemd-network/sd-ipv4ll.c ./src/systemd/src/libsystemd-network/sd-ipv4ll.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/async.h ./src/systemd/src/shared/async.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/fileio.c ./src/systemd/src/shared/fileio.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/fileio.h ./src/systemd/src/shared/fileio.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/list.h ./src/systemd/src/shared/list.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/log.h ./src/systemd/src/shared/log.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/macro.h ./src/systemd/src/shared/macro.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/path-util.c ./src/systemd/src/shared/path-util.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/path-util.h ./src/systemd/src/shared/path-util.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/refcnt.h ./src/systemd/src/shared/refcnt.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/siphash24.c ./src/systemd/src/shared/siphash24.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/siphash24.h ./src/systemd/src/shared/siphash24.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/socket-util.h ./src/systemd/src/shared/socket-util.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/sparse-endian.h ./src/systemd/src/shared/sparse-endian.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/strv.c ./src/systemd/src/shared/strv.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/strv.h ./src/systemd/src/shared/strv.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/time-util.c ./src/systemd/src/shared/time-util.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/time-util.h ./src/systemd/src/shared/time-util.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/utf8.c ./src/systemd/src/shared/utf8.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/utf8.h ./src/systemd/src/shared/utf8.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/util.c ./src/systemd/src/shared/util.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/util.h ./src/systemd/src/shared/util.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/unaligned.h ./src/systemd/src/shared/unaligned.h
    /bin/cp "$SYSTEMD_DIR"/src/shared/in-addr-util.c ./src/systemd/src/shared/in-addr-util.c
    /bin/cp "$SYSTEMD_DIR"/src/shared/in-addr-util.h ./src/systemd/src/shared/in-addr-util.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/_sd-common.h ./src/systemd/src/systemd/_sd-common.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-dhcp-client.h ./src/systemd/src/systemd/sd-dhcp-client.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-dhcp-lease.h ./src/systemd/src/systemd/sd-dhcp-lease.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-dhcp6-client.h ./src/systemd/src/systemd/sd-dhcp6-client.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-dhcp6-lease.h ./src/systemd/src/systemd/sd-dhcp6-lease.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-event.h ./src/systemd/src/systemd/sd-event.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-id128.h ./src/systemd/src/systemd/sd-id128.h
    /bin/cp "$SYSTEMD_DIR"/src/systemd/sd-ipv4ll.h ./src/systemd/src/systemd/sd-ipv4ll.h
parent 8af18182
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright (C) 2014 Axis Communications AB. All rights reserved.
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 <netinet/if_ether.h>
#include "sparse-endian.h"
#include "socket-util.h"
int arp_network_bind_raw_socket(int index, union sockaddr_union *link);
int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
const struct ether_arp *arp);
void arp_packet_init(struct ether_arp *arp);
void arp_packet_probe(struct ether_arp *arp, be32_t pa, const struct ether_addr *ha);
void arp_packet_announcement(struct ether_arp *arp, be32_t pa, const struct ether_addr *ha);
int arp_packet_verify_headers(struct ether_arp *arp);
#define log_ipv4ll(ll, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "IPv4LL: " fmt, ##__VA_ARGS__)
/***
This file is part of systemd.
Copyright (C) 2014 Axis Communications AB. All rights reserved.
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 <linux/filter.h>
#include "util.h"
#include "ipv4ll-internal.h"
int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
const struct ether_arp *arp) {
int r;
assert(arp);
assert(link);
assert(fd >= 0);
r = sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll));
if (r < 0)
return -errno;
return 0;
}
int arp_network_bind_raw_socket(int ifindex, union sockaddr_union *link) {
static const struct sock_filter filter[] = {
BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct ether_arp), 1, 0), /* packet >= arp packet ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hrd)), /* A <- header */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPHRD_ETHER, 1, 0), /* header == ethernet ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pro)), /* A <- protocol */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), /* protocol == IP ? */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_op)), /* A <- operation */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 0, 1), /* protocol == request ? */
BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), /* protocol == reply ? */
BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
};
struct sock_fprog fprog = {
.len = ELEMENTSOF(filter),
.filter = (struct sock_filter*) filter
};
_cleanup_close_ int s = -1;
int r;
assert(ifindex > 0);
assert(link);
s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (s < 0)
return -errno;
r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
if (r < 0)
return -errno;
link->ll.sll_family = AF_PACKET;
link->ll.sll_protocol = htons(ETH_P_ARP);
link->ll.sll_ifindex = ifindex;
link->ll.sll_halen = ETH_ALEN;
memset(link->ll.sll_addr, 0xff, ETH_ALEN);
r = bind(s, &link->sa, sizeof(link->ll));
if (r < 0)
return -errno;
r = s;
s = -1;
return r;
}
/***
This file is part of systemd.
Copyright (C) 2014 Axis Communications AB. All rights reserved.
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 "util.h"
#include "ipv4ll-internal.h"
void arp_packet_init(struct ether_arp *arp) {
assert(arp);
memzero(arp, sizeof(struct ether_arp));
/* Header */
arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); /* HTYPE */
arp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); /* PTYPE */
arp->ea_hdr.ar_hln = ETH_ALEN; /* HLEN */
arp->ea_hdr.ar_pln = sizeof arp->arp_spa; /* PLEN */
arp->ea_hdr.ar_op = htons(ARPOP_REQUEST); /* REQUEST */
}
void arp_packet_probe(struct ether_arp *arp, be32_t pa, const struct ether_addr *ha) {
assert(ha);
arp_packet_init(arp);
memcpy(arp->arp_sha, ha, ETH_ALEN);
memcpy(arp->arp_tpa, &pa, sizeof(pa));
}
void arp_packet_announcement(struct ether_arp *arp, be32_t pa, const struct ether_addr *ha) {
assert(ha);
arp_packet_init(arp);
memcpy(arp->arp_sha, ha, ETH_ALEN);
memcpy(arp->arp_tpa, &pa, sizeof(pa));
memcpy(arp->arp_spa, &pa, sizeof(pa));
}
int arp_packet_verify_headers(struct ether_arp *arp) {
assert(arp);
if (arp->ea_hdr.ar_hrd != htons(ARPHRD_ETHER)) {
log_ipv4ll(NULL, "ignoring packet: header is not ARPHRD_ETHER");
return -EINVAL;
}
if (arp->ea_hdr.ar_pro != htons(ETHERTYPE_IP)) {
log_ipv4ll(NULL, "ignoring packet: protocol is not ETHERTYPE_IP");
return -EINVAL;
}
if (arp->ea_hdr.ar_op != htons(ARPOP_REQUEST) &&
arp->ea_hdr.ar_op != htons(ARPOP_REPLY)) {
log_ipv4ll(NULL, "ignoring packet: operation is not ARPOP_REQUEST or ARPOP_REPLY");
return -EINVAL;
}
return 0;
}
/***
This file is part of systemd.
Copyright (C) 2014 Axis Communications AB. All rights reserved.
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 <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include "util.h"
#include "siphash24.h"
#include "list.h"
#include "refcnt.h"
#include "ipv4ll-internal.h"
#include "sd-ipv4ll.h"
/* Constants from the RFC */
#define PROBE_WAIT 1
#define PROBE_NUM 3
#define PROBE_MIN 1
#define PROBE_MAX 2
#define ANNOUNCE_WAIT 2
#define ANNOUNCE_NUM 2
#define ANNOUNCE_INTERVAL 2
#define MAX_CONFLICTS 10
#define RATE_LIMIT_INTERVAL 60
#define DEFEND_INTERVAL 10
#define IPV4LL_NETWORK 0xA9FE0000L
#define IPV4LL_NETMASK 0xFFFF0000L
typedef enum IPv4LLTrigger{
IPV4LL_TRIGGER_NULL,
IPV4LL_TRIGGER_PACKET,
IPV4LL_TRIGGER_TIMEOUT,
_IPV4LL_TRIGGER_MAX,
_IPV4LL_TRIGGER_INVALID = -1
} IPv4LLTrigger;
typedef enum IPv4LLState {
IPV4LL_STATE_INIT,
IPV4LL_STATE_WAITING_PROBE,
IPV4LL_STATE_PROBING,
IPV4LL_STATE_WAITING_ANNOUNCE,
IPV4LL_STATE_ANNOUNCING,
IPV4LL_STATE_RUNNING,
IPV4LL_STATE_STOPPED,
_IPV4LL_STATE_MAX,
_IPV4LL_STATE_INVALID = -1
} IPv4LLState;
struct sd_ipv4ll {
RefCount n_ref;
IPv4LLState state;
int index;
int fd;
union sockaddr_union link;
int iteration;
int conflict;
sd_event_source *receive_message;
sd_event_source *timer;
usec_t next_wakeup;
usec_t defend_window;
int next_wakeup_valid;
be32_t address;
struct random_data *random_data;
char *random_data_state;
/* External */
be32_t claimed_address;
struct ether_addr mac_addr;
sd_event *event;
int event_priority;
sd_ipv4ll_cb_t cb;
void* userdata;
};
static void ipv4ll_run_state_machine(sd_ipv4ll *ll, IPv4LLTrigger trigger, void *trigger_data);
static void ipv4ll_set_state(sd_ipv4ll *ll, IPv4LLState st, int reset_counter) {
assert(ll);
assert(st < _IPV4LL_STATE_MAX);
if (st == ll->state && !reset_counter) {
ll->iteration++;
} else {
ll->state = st;
ll->iteration = 0;
}
}
static sd_ipv4ll *ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
assert(ll);
if (ll->cb) {
ll = sd_ipv4ll_ref(ll);
ll->cb(ll, event, ll->userdata);
ll = sd_ipv4ll_unref(ll);
}
return ll;
}
static sd_ipv4ll *ipv4ll_stop(sd_ipv4ll *ll, int event) {
assert(ll);
ll->receive_message = sd_event_source_unref(ll->receive_message);
ll->fd = safe_close(ll->fd);
ll->timer = sd_event_source_unref(ll->timer);
log_ipv4ll(ll, "STOPPED");
ll = ipv4ll_client_notify(ll, event);
if (ll) {
ll->claimed_address = 0;
ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
}
return ll;
}
static int ipv4ll_pick_address(sd_ipv4ll *ll, be32_t *address) {
be32_t addr;
int r;
int32_t random;
assert(ll);
assert(address);
assert(ll->random_data);
do {
r = random_r(ll->random_data, &random);
if (r < 0)
return r;
addr = htonl((random & 0x0000FFFF) | IPV4LL_NETWORK);
} while (addr == ll->address ||
(ntohl(addr) & IPV4LL_NETMASK) != IPV4LL_NETWORK ||
(ntohl(addr) & 0x0000FF00) == 0x0000 ||
(ntohl(addr) & 0x0000FF00) == 0xFF00);
*address = addr;
return 0;
}
static int ipv4ll_timer(sd_event_source *s, uint64_t usec, void *userdata) {
sd_ipv4ll *ll = (sd_ipv4ll*)userdata;
assert(ll);
ll->next_wakeup_valid = 0;
ipv4ll_run_state_machine(ll, IPV4LL_TRIGGER_TIMEOUT, NULL);
return 0;
}
static void ipv4ll_set_next_wakeup(sd_ipv4ll *ll, int sec, int random_sec) {
usec_t next_timeout = 0;
usec_t time_now = 0;
assert(sec >= 0);
assert(random_sec >= 0);
assert(ll);
next_timeout = sec * USEC_PER_SEC;
if (random_sec)
next_timeout += random_u32() % (random_sec * USEC_PER_SEC);
if (sd_event_now(ll->event, clock_boottime_or_monotonic(), &time_now) < 0)
time_now = now(clock_boottime_or_monotonic());
ll->next_wakeup = time_now + next_timeout;
ll->next_wakeup_valid = 1;
}
static bool ipv4ll_arp_conflict (sd_ipv4ll *ll, struct ether_arp *arp) {
assert(ll);
assert(arp);
if (memcmp(arp->arp_spa, &ll->address, sizeof(ll->address)) == 0 &&
memcmp(arp->arp_sha, &ll->mac_addr, ETH_ALEN) != 0)
return true;
return false;
}
static bool ipv4ll_arp_probe_conflict (sd_ipv4ll *ll, struct ether_arp *arp) {
assert(ll);
assert(arp);
if (ipv4ll_arp_conflict(ll, arp))
return true;
if (memcmp(arp->arp_tpa, &ll->address, sizeof(ll->address)) == 0 &&
memcmp(arp->arp_sha, &ll->mac_addr, ETH_ALEN))
return true;
return false;
}
static void ipv4ll_run_state_machine(sd_ipv4ll *ll, IPv4LLTrigger trigger, void *trigger_data) {
struct ether_arp out_packet;
int out_packet_ready = 0;
int r = 0;
assert(ll);
assert(trigger < _IPV4LL_TRIGGER_MAX);
if (ll->state == IPV4LL_STATE_INIT) {
log_ipv4ll(ll, "PROBE");
ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_PROBE, 1);
ipv4ll_set_next_wakeup(ll, 0, PROBE_WAIT);
} else if ((ll->state == IPV4LL_STATE_WAITING_PROBE && trigger == IPV4LL_TRIGGER_TIMEOUT) ||
(ll->state == IPV4LL_STATE_PROBING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration < PROBE_NUM-2)) {
/* Send a probe */
arp_packet_probe(&out_packet, ll->address, &ll->mac_addr);
out_packet_ready = 1;
ipv4ll_set_state(ll, IPV4LL_STATE_PROBING, 0);
ipv4ll_set_next_wakeup(ll, PROBE_MIN, (PROBE_MAX-PROBE_MIN));
} else if (ll->state == IPV4LL_STATE_PROBING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration >= PROBE_NUM-2) {
/* Send the last probe */
arp_packet_probe(&out_packet, ll->address, &ll->mac_addr);
out_packet_ready = 1;
ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_ANNOUNCE, 1);
ipv4ll_set_next_wakeup(ll, ANNOUNCE_WAIT, 0);
} else if ((ll->state == IPV4LL_STATE_WAITING_ANNOUNCE && trigger == IPV4LL_TRIGGER_TIMEOUT) ||
(ll->state == IPV4LL_STATE_ANNOUNCING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration < ANNOUNCE_NUM-1)) {
/* Send announcement packet */
arp_packet_announcement(&out_packet, ll->address, &ll->mac_addr);
out_packet_ready = 1;
ipv4ll_set_state(ll, IPV4LL_STATE_ANNOUNCING, 0);
ipv4ll_set_next_wakeup(ll, ANNOUNCE_INTERVAL, 0);
if (ll->iteration == 0) {
log_ipv4ll(ll, "ANNOUNCE");
ll->claimed_address = ll->address;
ll = ipv4ll_client_notify(ll, IPV4LL_EVENT_BIND);
if (!ll || ll->state == IPV4LL_STATE_STOPPED)
goto out;
ll->conflict = 0;
}
} else if ((ll->state == IPV4LL_STATE_ANNOUNCING && trigger == IPV4LL_TRIGGER_TIMEOUT &&
ll->iteration >= ANNOUNCE_NUM-1)) {
ipv4ll_set_state(ll, IPV4LL_STATE_RUNNING, 0);
ll->next_wakeup_valid = 0;
} else if (trigger == IPV4LL_TRIGGER_PACKET) {
int conflicted = 0;
usec_t time_now;
struct ether_arp* in_packet = (struct ether_arp*)trigger_data;
assert(in_packet);
if (IN_SET(ll->state, IPV4LL_STATE_ANNOUNCING, IPV4LL_STATE_RUNNING)) {
if (ipv4ll_arp_conflict(ll, in_packet)) {
r = sd_event_now(ll->event, clock_boottime_or_monotonic(), &time_now);
if (r < 0)
goto out;
/* Defend address */
if (time_now > ll->defend_window) {
ll->defend_window = time_now + DEFEND_INTERVAL * USEC_PER_SEC;
arp_packet_announcement(&out_packet, ll->address, &ll->mac_addr);
out_packet_ready = 1;
} else
conflicted = 1;
}
} else if (IN_SET(ll->state, IPV4LL_STATE_WAITING_PROBE,
IPV4LL_STATE_PROBING,
IPV4LL_STATE_WAITING_ANNOUNCE)) {
conflicted = ipv4ll_arp_probe_conflict(ll, in_packet);
}
if (conflicted) {
log_ipv4ll(ll, "CONFLICT");
ll = ipv4ll_client_notify(ll, IPV4LL_EVENT_CONFLICT);
if (!ll || ll->state == IPV4LL_STATE_STOPPED)
goto out;
ll->claimed_address = 0;
/* Pick a new address */
r = ipv4ll_pick_address(ll, &ll->address);
if (r < 0)
goto out;
ll->conflict++;
ll->defend_window = 0;
ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_PROBE, 1);
if (ll->conflict >= MAX_CONFLICTS) {
log_ipv4ll(ll, "MAX_CONFLICTS");
ipv4ll_set_next_wakeup(ll, RATE_LIMIT_INTERVAL, PROBE_WAIT);
} else
ipv4ll_set_next_wakeup(ll, 0, PROBE_WAIT);
}
}
if (out_packet_ready) {
r = arp_network_send_raw_socket(ll->fd, &ll->link, &out_packet);
if (r < 0) {
log_ipv4ll(ll, "failed to send arp packet out");
goto out;
}
}
if (ll->next_wakeup_valid) {
ll->timer = sd_event_source_unref(ll->timer);
r = sd_event_add_time(ll->event, &ll->timer, clock_boottime_or_monotonic(),
ll->next_wakeup, 0, ipv4ll_timer, ll);
if (r < 0)
goto out;
r = sd_event_source_set_priority(ll->timer, ll->event_priority);
if (r < 0)
goto out;
r = sd_event_source_set_description(ll->timer, "ipv4ll-timer");
if (r < 0)
goto out;
}
out:
if (r < 0 && ll)
ipv4ll_stop(ll, r);
}
static int ipv4ll_receive_message(sd_event_source *s, int fd,
uint32_t revents, void *userdata) {
int r;
struct ether_arp arp;
sd_ipv4ll *ll = (sd_ipv4ll*)userdata;
assert(ll);
r = read(fd, &arp, sizeof(struct ether_arp));
if (r < (int) sizeof(struct ether_arp))
return 0;
r = arp_packet_verify_headers(&arp);
if (r < 0)
return 0;
ipv4ll_run_state_machine(ll, IPV4LL_TRIGGER_PACKET, &arp);
return 0;