[systemd-devel] [PATCH] sd-network: IPv4 link-local support [v2]

Umut Tezduyar Lindskog umut.tezduyar at axis.com
Fri Feb 28 07:10:20 PST 2014


Implements IPv4LL with respect to RFC 3927
(http://tools.ietf.org/rfc/rfc3927.txt) and integrates it
with networkd. Majority of the IPv4LL state machine is
taken from avahi (http://avahi.org/) project's autoip.

IPv4LL can be enabled by IPv4LL=yes under [Network]
section of .network file.

IPv4LL works independent of DHCP but if DHCP lease is
aquired, then LL address will be dropped.
---
 Makefile.am                              |    7 +-
 TODO                                     |    5 +
 src/libsystemd-network/ipv4ll-internal.h |   38 +++
 src/libsystemd-network/ipv4ll-network.c  |   57 ++++
 src/libsystemd-network/ipv4ll-packet.c   |   71 ++++
 src/libsystemd-network/sd-ipv4ll.c       |  519 ++++++++++++++++++++++++++++++
 src/libsystemd/sd-rtnl/rtnl-message.c    |   14 +
 src/network/networkd-address.c           |    4 +-
 src/network/networkd-link.c              |  312 ++++++++++++++++--
 src/network/networkd-network-gperf.gperf |    1 +
 src/network/networkd-route.c             |   85 +++++
 src/network/networkd.h                   |    8 +
 src/systemd/sd-ipv4ll.h                  |   50 +++
 src/systemd/sd-rtnl.h                    |    1 +
 14 files changed, 1141 insertions(+), 31 deletions(-)
 create mode 100644 src/libsystemd-network/ipv4ll-internal.h
 create mode 100644 src/libsystemd-network/ipv4ll-network.c
 create mode 100644 src/libsystemd-network/ipv4ll-packet.c
 create mode 100644 src/libsystemd-network/sd-ipv4ll.c
 create mode 100644 src/systemd/sd-ipv4ll.h

diff --git a/Makefile.am b/Makefile.am
index 6be6407..30bfe13 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2405,6 +2405,7 @@ libsystemd_network_la_SOURCES = \
 	src/systemd/sd-network.h \
 	src/systemd/sd-dhcp-client.h \
 	src/systemd/sd-dhcp-lease.h \
+	src/systemd/sd-ipv4ll.h \
 	src/network/sd-network.c \
 	src/libsystemd-network/sd-dhcp-client.c \
 	src/libsystemd-network/dhcp-network.c \
@@ -2413,7 +2414,11 @@ libsystemd_network_la_SOURCES = \
 	src/libsystemd-network/dhcp-internal.h \
 	src/libsystemd-network/dhcp-protocol.h \
 	src/libsystemd-network/dhcp-lease-internal.h \
-	src/libsystemd-network/sd-dhcp-lease.c
+	src/libsystemd-network/sd-dhcp-lease.c \
+	src/libsystemd-network/sd-ipv4ll.c \
+	src/libsystemd-network/ipv4ll-network.c \
+	src/libsystemd-network/ipv4ll-packet.c \
+	src/libsystemd-network/ipv4ll-internal.h
 
 libsystemd_network_la_LIBADD = \
 	libsystemd-internal.la \
diff --git a/TODO b/TODO
index 6cac3e2..711d2ad 100644
--- a/TODO
+++ b/TODO
@@ -679,6 +679,11 @@ Features:
    - add support for more DHCPv4 options (and, longer term, other kinds of dynamic config)
    - add proper initrd support (in particular generate .network/.link files based on /proc/cmdline)
    - add reduced [Link] support to .network files
+   - add IPv4LL tests (inspire by DHCP)
+   - add IPv4LL to man pages. Explain coexistance between DHCP
+   - add Scope= parsing option for [Network]
+   - change LL address generation and make it predictable like get_mac() (link-config.c)
+   - have smooth transition from LL to routable address, without disconnecting clients.
 
 External:
 
diff --git a/src/libsystemd-network/ipv4ll-internal.h b/src/libsystemd-network/ipv4ll-internal.h
new file mode 100644
index 0000000..fe5ef8e
--- /dev/null
+++ b/src/libsystemd-network/ipv4ll-internal.h
@@ -0,0 +1,38 @@
+/*-*- 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_meta(LOG_DEBUG, __FILE__, __LINE__, __func__, "IPv4LL: " fmt, ##__VA_ARGS__)
diff --git a/src/libsystemd-network/ipv4ll-network.c b/src/libsystemd-network/ipv4ll-network.c
new file mode 100644
index 0000000..b325445
--- /dev/null
+++ b/src/libsystemd-network/ipv4ll-network.c
@@ -0,0 +1,57 @@
+/***
+  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 "util.h"
+#include "ipv4ll-internal.h"
+
+int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
+                                        const struct ether_arp *arp) {
+        assert(arp);
+        assert(link);
+        assert(fd >= 0);
+
+        if (sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll)) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
+        int s;
+
+        assert(index > 0);
+        assert(link);
+
+        s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, htons(ETH_P_ARP));
+        if (s < 0)
+                return -errno;
+
+        link->ll.sll_family = AF_PACKET;
+        link->ll.sll_ifindex = index;
+        link->ll.sll_protocol = htons(ETH_P_ARP);
+        link->ll.sll_halen = ETH_ALEN;
+
+        if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
+                close_nointr_nofail(s);
+                return -errno;
+        }
+
+        return s;
+}
+
diff --git a/src/libsystemd-network/ipv4ll-packet.c b/src/libsystemd-network/ipv4ll-packet.c
new file mode 100644
index 0000000..2b6c73a
--- /dev/null
+++ b/src/libsystemd-network/ipv4ll-packet.c
@@ -0,0 +1,71 @@
+/***
+  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;
+}
diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c
new file mode 100644
index 0000000..39f6988
--- /dev/null
+++ b/src/libsystemd-network/sd-ipv4ll.c
@@ -0,0 +1,519 @@
+/***
+  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 "list.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_MAX,
+        _IPV4LL_STATE_INVALID = -1
+} IPv4LLState;
+
+struct sd_ipv4ll {
+        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;
+        /* 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 int ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
+        assert(ll);
+
+        if (ll->cb)
+                ll->cb(ll, event, ll->userdata);
+
+        return 0;
+}
+
+static int ipv4ll_stop(sd_ipv4ll *ll, int event) {
+        assert(ll);
+
+        ll->receive_message = sd_event_source_unref(ll->receive_message);
+        if (ll->fd >= 0)
+                close_nointr_nofail(ll->fd);
+        ll->fd = -1;
+
+        ll->timer = sd_event_source_unref(ll->timer);
+
+        ipv4ll_client_notify(ll, event);
+
+        ll->claimed_address = 0;
+
+        ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
+
+        log_ipv4ll(ll, "STOPPED");
+
+        return 0;
+}
+
+static be32_t ipv4ll_pick_address(sd_ipv4ll *ll) {
+        be32_t addr;
+
+        assert(ll);
+
+        if (ll->address) {
+                do {
+                        uint32_t r = random_u32() & 0x0000FFFF;
+                        addr = htonl(IPV4LL_NETWORK | r);
+                } while (addr == ll->address ||
+                        (ntohl(addr) & IPV4LL_NETMASK) != IPV4LL_NETWORK ||
+                        (ntohl(addr) & 0x0000FF00) == 0x0000 ||
+                        (ntohl(addr) & 0x0000FF00) == 0xFF00);
+        } else {
+                uint32_t a = 1;
+                int i;
+
+                for (i = 0; i < ETH_ALEN; i++)
+                        a += ll->mac_addr.ether_addr_octet[i]*i;
+                a = (a % 0xFE00) + 0x0100;
+                addr = htonl(IPV4LL_NETWORK | (uint32_t) a);
+        }
+
+        return addr;
+}
+
+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_get_now_monotonic(ll->event, &time_now) < 0)
+                time_now = now(CLOCK_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;
+                        r = ipv4ll_client_notify(ll, IPV4LL_EVENT_BIND);
+                        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_get_now_monotonic(ll->event, &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");
+                        r = ipv4ll_client_notify(ll, IPV4LL_EVENT_CONFLICT);
+                        ll->claimed_address = 0;
+
+                        /* Pick a new address */
+                        ll->address = ipv4ll_pick_address(ll);
+                        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_monotonic(ll->event, &ll->timer,
+                                   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;
+        }
+
+out:
+        if (r < 0)
+                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 < 0 || r < 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;
+}
+
+int sd_ipv4ll_set_index(sd_ipv4ll *ll, int interface_index) {
+        assert_return(ll, -EINVAL);
+        assert_return(interface_index >= -1, -EINVAL);
+        assert_return(ll->state == IPV4LL_STATE_INIT, -EBUSY);
+
+        ll->index = interface_index;
+
+        return 0;
+}
+
+int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
+        assert_return(ll, -EINVAL);
+        assert_return(ll->state == IPV4LL_STATE_INIT, -EBUSY);
+
+        memcpy(&ll->mac_addr.ether_addr_octet, addr, ETH_ALEN);
+
+        return 0;
+}
+
+int sd_ipv4ll_detach_event(sd_ipv4ll *ll) {
+        assert_return(ll, -EINVAL);
+
+        ll->event = sd_event_unref(ll->event);
+
+        return 0;
+}
+
+int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int priority) {
+        int r;
+
+        assert_return(ll, -EINVAL);
+        assert_return(!ll->event, -EBUSY);
+
+        if (event)
+                ll->event = sd_event_ref(event);
+        else {
+                r = sd_event_default(&ll->event);
+                if (r < 0) {
+                        ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
+                        return r;
+                }
+        }
+
+        ll->event_priority = priority;
+
+        return 0;
+}
+
+int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_cb_t cb, void *userdata) {
+        assert_return(ll, -EINVAL);
+
+        ll->cb = cb;
+        ll->userdata = userdata;
+
+        return 0;
+}
+
+int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address){
+        assert_return(ll, -EINVAL);
+        assert_return(address, -EINVAL);
+
+        if (ll->claimed_address == 0) {
+                return -ENOENT;
+        }
+
+        address->s_addr = ll->claimed_address;
+        return 0;
+}
+
+int sd_ipv4ll_start (sd_ipv4ll *ll) {
+        int r;
+
+        assert_return(ll, -EINVAL);
+        assert_return(ll->event, -EINVAL);
+        assert_return(ll->index > 0, -EINVAL);
+        assert_return(ll->state == IPV4LL_STATE_INIT, -EBUSY);
+
+        r = arp_network_bind_raw_socket(ll->index, &ll->link);
+
+        if (r < 0)
+                goto out;
+
+        ll->fd = r;
+        ll->conflict = 0;
+        ll->defend_window = 0;
+        ll->claimed_address = 0;
+
+        if (ll->address == 0)
+                ll->address = ipv4ll_pick_address(ll);
+
+        ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
+
+        r = sd_event_add_io(ll->event, &ll->receive_message, ll->fd,
+                            EPOLLIN, ipv4ll_receive_message, ll);
+        if (r < 0)
+                goto out;
+
+        r = sd_event_source_set_priority(ll->receive_message, ll->event_priority);
+        if (r < 0)
+                goto out;
+
+        r = sd_event_add_monotonic(ll->event, &ll->timer, now(CLOCK_MONOTONIC), 0,
+                                   ipv4ll_timer, ll);
+
+        if (r < 0)
+                goto out;
+
+        r = sd_event_source_set_priority(ll->timer, ll->event_priority);
+
+out:
+        if (r < 0)
+                ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
+
+        return 0;
+}
+
+int sd_ipv4ll_stop(sd_ipv4ll *ll) {
+        return ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
+}
+
+void sd_ipv4ll_free (sd_ipv4ll *ll) {
+        if (!ll)
+                return;
+
+        sd_ipv4ll_stop(ll);
+        sd_ipv4ll_detach_event(ll);
+
+        free(ll);
+}
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_ipv4ll*, sd_ipv4ll_free);
+#define _cleanup_ipv4ll_free_ _cleanup_(sd_ipv4ll_freep)
+
+int sd_ipv4ll_new(sd_ipv4ll **ret) {
+        _cleanup_ipv4ll_free_ sd_ipv4ll *ll = NULL;
+
+        assert_return(ret, -EINVAL);
+
+        ll = new0(sd_ipv4ll, 1);
+        if (!ll)
+                return -ENOMEM;
+
+        ll->state = IPV4LL_STATE_INIT;
+        ll->index = -1;
+        ll->fd = -1;
+
+        *ret = ll;
+        ll = NULL;
+
+        return 0;
+}
diff --git a/src/libsystemd/sd-rtnl/rtnl-message.c b/src/libsystemd/sd-rtnl/rtnl-message.c
index 27b6065..95dccc2 100644
--- a/src/libsystemd/sd-rtnl/rtnl-message.c
+++ b/src/libsystemd/sd-rtnl/rtnl-message.c
@@ -85,6 +85,20 @@ int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char pr
         return 0;
 }
 
+int sd_rtnl_message_route_set_scope(sd_rtnl_message *m, unsigned char scope) {
+        struct rtmsg *rtm;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->hdr, -EINVAL);
+        assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
+
+        rtm = NLMSG_DATA(m->hdr);
+
+        rtm->rtm_scope = scope;
+
+        return 0;
+}
+
 int sd_rtnl_message_new_route(sd_rtnl *rtnl, sd_rtnl_message **ret,
                               uint16_t nlmsg_type, unsigned char rtm_family) {
         struct rtmsg *rtm;
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index a637739..c924189 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -47,6 +47,7 @@ int address_new_static(Network *network, unsigned section, Address **ret) {
                 return -ENOMEM;
 
         address->family = AF_UNSPEC;
+        address->scope = RT_SCOPE_UNIVERSE;
 
         address->network = network;
 
@@ -71,6 +72,7 @@ int address_new_dynamic(Address **ret) {
                 return -ENOMEM;
 
         address->family = AF_UNSPEC;
+        address->scope = RT_SCOPE_UNIVERSE;
 
         *ret = address;
         address = NULL;
@@ -170,7 +172,7 @@ int address_configure(Address *address, Link *link,
                 return r;
         }
 
-        r = sd_rtnl_message_addr_set_scope(req, RT_SCOPE_UNIVERSE);
+        r = sd_rtnl_message_addr_set_scope(req, address->scope);
         if (r < 0) {
                 log_error("Could not set scope: %s", strerror(-r));
                 return r;
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index f09fb75..c10d947 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -87,6 +87,8 @@ void link_free(Link *link) {
         sd_dhcp_client_free(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 
+        sd_ipv4ll_free(link->ipv4ll);
+
         hashmap_remove(link->manager->links, &link->ifindex);
 
         free(link->ifname);
@@ -196,6 +198,7 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
 static int link_enter_set_routes(Link *link) {
         Route *rt;
+        struct in_addr a;
         int r;
 
         assert(link);
@@ -204,7 +207,8 @@ static int link_enter_set_routes(Link *link) {
 
         link->state = LINK_STATE_SETTING_ROUTES;
 
-        if (!link->network->static_routes && !link->dhcp_lease)
+        if (!link->network->static_routes && !link->dhcp_lease &&
+                (!link->ipv4ll || sd_ipv4ll_get_address(link->ipv4ll, &a) < 0))
                 return link_enter_configured(link);
 
         log_debug_link(link, "setting routes");
@@ -221,6 +225,41 @@ static int link_enter_set_routes(Link *link) {
                 link->route_messages ++;
         }
 
+        if (link->ipv4ll && !link->dhcp_lease) {
+                _cleanup_route_free_ Route *route = NULL;
+                struct in_addr addr;
+
+                r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
+                if (r < 0 && r != -ENOENT) {
+                        log_warning_link(link, "IPV4LL error: no address: %s",
+                                        strerror(-r));
+                        return r;
+                }
+
+                if (r != -ENOENT) {
+                        r = route_new_dynamic(&route);
+                        if (r < 0) {
+                                log_error_link(link, "Could not allocate route: %s",
+                                               strerror(-r));
+                                return r;
+                        }
+
+                        route->family = AF_INET;
+                        route->scope = RT_SCOPE_LINK;
+                        route->metrics = 99;
+
+                        r = route_configure(route, link, &route_handler);
+                        if (r < 0) {
+                                log_warning_link(link,
+                                                 "could not set routes: %s", strerror(-r));
+                                link_enter_failed(link);
+                                return r;
+                        }
+
+                        link->route_messages ++;
+                }
+        }
+
         if (link->dhcp_lease) {
                 _cleanup_route_free_ Route *route = NULL;
                 struct in_addr gateway;
@@ -256,6 +295,28 @@ static int link_enter_set_routes(Link *link) {
         return 0;
 }
 
+static int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Link *link = userdata;
+        int r;
+
+        assert(m);
+        assert(link);
+        assert(link->ifname);
+
+        if (link->state == LINK_STATE_FAILED)
+                return 1;
+
+        r = sd_rtnl_message_get_errno(m);
+        if (r < 0 && r != -ENOENT)
+                log_struct_link(LOG_WARNING, link,
+                                "MESSAGE=%s: could not drop route: %s",
+                                link->ifname, strerror(-r),
+                                "ERRNO=%d", -r,
+                                NULL);
+
+        return 0;
+}
+
 static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         Link *link = userdata;
         int r;
@@ -289,6 +350,7 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
 static int link_enter_set_addresses(Link *link) {
         Address *ad;
+        struct in_addr a;
         int r;
 
         assert(link);
@@ -297,7 +359,8 @@ static int link_enter_set_addresses(Link *link) {
 
         link->state = LINK_STATE_SETTING_ADDRESSES;
 
-        if (!link->network->static_addresses && !link->dhcp_lease)
+        if (!link->network->static_addresses && !link->dhcp_lease &&
+                (!link->ipv4ll || sd_ipv4ll_get_address(link->ipv4ll, &a) < 0))
                 return link_enter_set_routes(link);
 
         log_debug_link(link, "setting addresses");
@@ -314,6 +377,42 @@ static int link_enter_set_addresses(Link *link) {
                 link->addr_messages ++;
         }
 
+        if (link->ipv4ll && !link->dhcp_lease) {
+                _cleanup_address_free_ Address *ll_addr = NULL;
+                struct in_addr addr;
+
+                r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
+                if (r < 0 && r != -ENOENT) {
+                        log_warning_link(link, "IPV4LL error: no address: %s",
+                                        strerror(-r));
+                        return r;
+                }
+
+                if (r != -ENOENT) {
+                        r = address_new_dynamic(&ll_addr);
+                        if (r < 0) {
+                                log_error_link(link, "Could not allocate address: %s", strerror(-r));
+                                return r;
+                        }
+
+                        ll_addr->family = AF_INET;
+                        ll_addr->in_addr.in = addr;
+                        ll_addr->prefixlen = 16;
+                        ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen);
+                        ll_addr->scope = RT_SCOPE_LINK;
+
+                        r = address_configure(ll_addr, link, &address_handler);
+                        if (r < 0) {
+                                log_warning_link(link,
+                                         "could not set addresses: %s", strerror(-r));
+                                link_enter_failed(link);
+                                return r;
+                        }
+
+                        link->addr_messages ++;
+                }
+        }
+
         if (link->dhcp_lease) {
                 _cleanup_address_free_ Address *address = NULL;
                 struct in_addr addr;
@@ -381,7 +480,7 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat
                                 "ERRNO=%d", -r,
                                 NULL);
 
-        return 1;
+        return 0;
 }
 
 static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
@@ -505,7 +604,7 @@ static int dhcp_lease_lost(Link *link) {
                 address->in_addr.in = addr;
                 address->prefixlen = prefixlen;
 
-                address_drop(address, link, address_drop_handler);
+                address_drop(address, link, &address_drop_handler);
         }
 
         if (link->network->dhcp_mtu) {
@@ -675,6 +774,14 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                                 }
                         }
 
+                        if (event == DHCP_EVENT_EXPIRED && link->network->ipv4ll) {
+                                r = sd_ipv4ll_start (link->ipv4ll);
+                                if (r < 0) {
+                                        link_enter_failed(link);
+                                        return;
+                                }
+                        }
+
                         break;
                 case DHCP_EVENT_IP_ACQUIRE:
                         r = dhcp_lease_acquired(client, link);
@@ -682,6 +789,13 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                                 link_enter_failed(link);
                                 return;
                         }
+                        if (link->ipv4ll) {
+                                r = sd_ipv4ll_stop(link->ipv4ll);
+                                if (r < 0) {
+                                        link_enter_failed(link);
+                                        return;
+                                }
+                        }
                         break;
                 default:
                         if (event < 0)
@@ -694,48 +808,179 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
         return;
 }
 
+static int ipv4ll_address_lost(sd_ipv4ll *ll, Link *link) {
+        int r;
+        struct in_addr addr;
+
+        assert(ll);
+        assert(link);
+
+        r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
+        if (r >= 0) {
+                _cleanup_address_free_ Address *address = NULL;
+                _cleanup_route_free_ Route *route = NULL;
+
+                log_debug_link(link, "IPv4 link-local release %u.%u.%u.%u",
+                                ADDRESS_FMT_VAL(addr));
+
+                r = address_new_dynamic(&address);
+                if (r < 0) {
+                        log_error_link(link, "Could not allocate address: %s", strerror(-r));
+                        return r;
+                }
+
+                address->family = AF_INET;
+                address->in_addr.in = addr;
+                address->prefixlen = 16;
+                address->scope = RT_SCOPE_LINK;
+
+                address_drop(address, link, &address_drop_handler);
+
+                r = route_new_dynamic(&route);
+                if (r < 0) {
+                        log_error_link(link, "Could not allocate route: %s",
+                                       strerror(-r));
+                        return r;
+                }
+
+                route->family = AF_INET;
+                route->scope = RT_SCOPE_LINK;
+                route->metrics = 99;
+
+                route_drop(route, link, &route_drop_handler);
+        }
+
+        return 0;
+}
+
+static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
+        struct in_addr address;
+        int r;
+
+        assert(ll);
+        assert(link);
+
+        r = sd_ipv4ll_get_address(ll, &address);
+        if (r < 0)
+                return r;
+
+        log_struct_link(LOG_INFO, link,
+                        "MESSAGE=%s: IPv4 link-local address %u.%u.%u.%u",
+                        link->ifname,
+                        ADDRESS_FMT_VAL(address),
+                        NULL);
+
+       link_enter_set_addresses(link);
+
+       return 0;
+}
+
+static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
+        Link *link = userdata;
+        int r;
+
+        assert(link);
+        assert(link->network);
+        assert(link->manager);
+
+        switch(event) {
+                case IPV4LL_EVENT_STOP:
+                case IPV4LL_EVENT_CONFLICT:
+                        r = ipv4ll_address_lost(ll, link);
+                        if (r < 0) {
+                                link_enter_failed(link);
+                                return;
+                        }
+                        break;
+                case IPV4LL_EVENT_BIND:
+                        r = ipv4ll_address_claimed(ll, link);
+                        if (r < 0) {
+                                link_enter_failed(link);
+                                return;
+                        }
+                        break;
+                default:
+                        if (event < 0)
+                                log_warning_link(link, "IPv4 link-local error: %s", strerror(-event));
+                        else
+                                log_warning_link(link, "IPv4 link-local unknown event: %d", event);
+                        break;
+        }
+}
+
 static int link_acquire_conf(Link *link) {
         int r;
 
         assert(link);
         assert(link->network);
-        assert(link->network->dhcp);
         assert(link->manager);
         assert(link->manager->event);
 
-        if (!link->dhcp_client) {
-                r = sd_dhcp_client_new(&link->dhcp_client);
-                if (r < 0)
-                        return r;
+        if (link->network->ipv4ll) {
+                if (!link->ipv4ll) {
+                        r = sd_ipv4ll_new(&link->ipv4ll);
+                        if (r < 0)
+                                return r;
 
-                r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
-                if (r < 0)
-                        return r;
+                        r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
+                        if (r < 0)
+                                return r;
 
-                r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
-                if (r < 0)
-                        return r;
+                        r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
+                        if (r < 0)
+                                return r;
 
-                r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
-                if (r < 0)
-                        return r;
+                        r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
+                        if (r < 0)
+                                return r;
+                }
 
-                r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link);
+                log_debug_link(link, "acquiring IPv4 link-local address");
+
+                r = sd_ipv4ll_start(link->ipv4ll);
                 if (r < 0)
                         return r;
+        }
+
+        if (link->network->dhcp) {
+                if (!link->dhcp_client) {
+                        r = sd_dhcp_client_new(&link->dhcp_client);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
+                        if (r < 0)
+                                return r;
 
-                if (link->network->dhcp_mtu) {
-                        r = sd_dhcp_client_set_request_option(link->dhcp_client, 26);
+                        r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link);
                         if (r < 0)
                                 return r;
+
+                        if (link->network->dhcp_mtu) {
+                                r = sd_dhcp_client_set_request_option(link->dhcp_client, 26);
+                                if (r < 0)
+                                        return r;
+                        }
                 }
-        }
 
-        log_debug_link(link, "acquiring DHCPv4 lease");
+                log_debug_link(link, "acquiring DHCPv4 lease");
 
-        r = sd_dhcp_client_start(link->dhcp_client);
-        if (r < 0)
-                return r;
+                r = sd_dhcp_client_start(link->dhcp_client);
+                if (r < 0)
+                        return r;
+        }
 
         return 0;
 }
@@ -762,10 +1007,10 @@ static int link_update_flags(Link *link, unsigned flags) {
                 if (flags & IFF_LOWER_UP) {
                         log_info_link(link, "carrier on");
 
-                        if (link->network->dhcp) {
+                        if (link->network->dhcp || link->network->ipv4ll) {
                                 r = link_acquire_conf(link);
                                 if (r < 0) {
-                                        log_warning_link(link, "Could not acquire DHCPv4 lease: %s", strerror(-r));
+                                        log_warning_link(link, "Could not acquire configuration: %s", strerror(-r));
                                         link_enter_failed(link);
                                         return r;
                                 }
@@ -781,6 +1026,15 @@ static int link_update_flags(Link *link, unsigned flags) {
                                         return r;
                                 }
                         }
+
+                        if (link->network->ipv4ll) {
+                                r = sd_ipv4ll_stop(link->ipv4ll);
+                                if (r < 0) {
+                                        log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r));
+                                        link_enter_failed(link);
+                                        return r;
+                                }
+                        }
                 }
         }
 
@@ -863,7 +1117,7 @@ static int link_enslaved(Link *link) {
                 return r;
         }
 
-        if (!link->network->dhcp)
+        if (!link->network->dhcp && !link->network->ipv4ll)
                 return link_enter_set_addresses(link);
 
         return 0;
diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf
index 6cc186f..7e3829a 100644
--- a/src/network/networkd-network-gperf.gperf
+++ b/src/network/networkd-network-gperf.gperf
@@ -30,6 +30,7 @@ Network.Bond,                config_parse_bond,                  0,
 Network.VLAN,                config_parse_vlan,                  0,                             offsetof(Network, vlans)
 Network.MACVLAN,             config_parse_macvlan,               0,                             offsetof(Network, macvlans)
 Network.DHCP,                config_parse_bool,                  0,                             offsetof(Network, dhcp)
+Network.IPv4LL,              config_parse_bool,                  0,                             offsetof(Network, ipv4ll)
 Network.Address,             config_parse_address,               0,                             0
 Network.Gateway,             config_parse_gateway,               0,                             0
 Network.DNS,                 config_parse_dns,                   0,                             offsetof(Network, dns)
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index 46a4537..0cb7239 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -48,6 +48,7 @@ int route_new_static(Network *network, unsigned section, Route **ret) {
                 return -ENOMEM;
 
         route->family = AF_UNSPEC;
+        route->scope = RT_SCOPE_UNIVERSE;
 
         route->network = network;
 
@@ -72,6 +73,7 @@ int route_new_dynamic(Route **ret) {
                 return -ENOMEM;
 
         route->family = AF_UNSPEC;
+        route->scope = RT_SCOPE_UNIVERSE;
 
         *ret = route;
         route = NULL;
@@ -94,6 +96,77 @@ void route_free(Route *route) {
         free(route);
 }
 
+int route_drop(Route *route, Link *link,
+               sd_rtnl_message_handler_t callback) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(link);
+        assert(link->manager);
+        assert(link->manager->rtnl);
+        assert(link->ifindex > 0);
+        assert(route->family == AF_INET || route->family == AF_INET6);
+
+        r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
+                                      RTM_DELROUTE, route->family);
+        if (r < 0) {
+                log_error("Could not create RTM_DELROUTE message: %s", strerror(-r));
+                return r;
+        }
+
+        if (route->family == AF_INET)
+                r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &route->in_addr.in);
+        else if (route->family == AF_INET6)
+                r = sd_rtnl_message_append_in6_addr(req, RTA_GATEWAY, &route->in_addr.in6);
+        if (r < 0) {
+                log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
+                return r;
+        }
+
+        if (route->dst_prefixlen) {
+                if (route->family == AF_INET)
+                        r = sd_rtnl_message_append_in_addr(req, RTA_DST, &route->dst_addr.in);
+                else if (route->family == AF_INET6)
+                        r = sd_rtnl_message_append_in6_addr(req, RTA_DST, &route->dst_addr.in6);
+                if (r < 0) {
+                        log_error("Could not append RTA_DST attribute: %s", strerror(-r));
+                        return r;
+                }
+
+                r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
+                if (r < 0) {
+                        log_error("Could not set destination prefix length: %s", strerror(-r));
+                        return r;
+                }
+        }
+
+        r = sd_rtnl_message_route_set_scope(req, route->scope);
+        if (r < 0) {
+                log_error("Could not set scope: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append_u32(req, RTA_PRIORITY, route->metrics);
+        if (r < 0) {
+                log_error("Could not append RTA_PRIORITY attribute: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append_u32(req, RTA_OIF, link->ifindex);
+        if (r < 0) {
+                log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
+        if (r < 0) {
+                log_error("Could not send rtnetlink message: %s", strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
+
 int route_configure(Route *route, Link *link,
                     sd_rtnl_message_handler_t callback) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
@@ -138,6 +211,18 @@ int route_configure(Route *route, Link *link,
                 }
         }
 
+        r = sd_rtnl_message_route_set_scope(req, route->scope);
+        if (r < 0) {
+                log_error("Could not set scope: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append_u32(req, RTA_PRIORITY, route->metrics);
+        if (r < 0) {
+                log_error("Could not append RTA_PRIORITY attribute: %s", strerror(-r));
+                return r;
+        }
+
         r = sd_rtnl_message_append_u32(req, RTA_OIF, link->ifindex);
         if (r < 0) {
                 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
diff --git a/src/network/networkd.h b/src/network/networkd.h
index e9c0dd5..d8cd7ec 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -27,6 +27,7 @@
 #include "sd-rtnl.h"
 #include "sd-bus.h"
 #include "sd-dhcp-client.h"
+#include "sd-ipv4ll.h"
 #include "udev.h"
 
 #include "rtnl-util.h"
@@ -125,6 +126,7 @@ struct Network {
         bool dhcp_hostname;
         bool dhcp_domainname;
         bool dhcp_critical;
+        bool ipv4ll;
 
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
@@ -142,6 +144,7 @@ struct Address {
 
         unsigned char family;
         unsigned char prefixlen;
+        unsigned char scope;
         char *label;
 
         struct in_addr broadcast;
@@ -160,6 +163,8 @@ struct Route {
 
         unsigned char family;
         unsigned char dst_prefixlen;
+        unsigned char scope;
+        uint32_t metrics;
 
         union {
                 struct in_addr in;
@@ -205,6 +210,7 @@ struct Link {
         sd_dhcp_client *dhcp_client;
         sd_dhcp_lease *dhcp_lease;
         uint16_t original_mtu;
+        sd_ipv4ll *ipv4ll;
 };
 
 struct Manager {
@@ -307,6 +313,8 @@ int route_new_static(Network *network, unsigned section, Route **ret);
 int route_new_dynamic(Route **ret);
 void route_free(Route *route);
 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
+int route_drop(Route *route, Link *link, sd_rtnl_message_handler_t callback);
+
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
 #define _cleanup_route_free_ _cleanup_(route_freep)
diff --git a/src/systemd/sd-ipv4ll.h b/src/systemd/sd-ipv4ll.h
new file mode 100644
index 0000000..0207006
--- /dev/null
+++ b/src/systemd/sd-ipv4ll.h
@@ -0,0 +1,50 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foosdipv4llfoo
+#define foosdipv4llfoo
+
+/***
+  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/in.h>
+#include <net/ethernet.h>
+
+#include "sd-event.h"
+
+enum {
+        IPV4LL_EVENT_STOP           = 0,
+        IPV4LL_EVENT_BIND           = 1,
+        IPV4LL_EVENT_CONFLICT       = 2,
+};
+
+typedef struct sd_ipv4ll sd_ipv4ll;
+typedef void (*sd_ipv4ll_cb_t)(sd_ipv4ll *ll, int event, void *userdata);
+
+int sd_ipv4ll_detach_event(sd_ipv4ll *ll);
+int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int priority);
+int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address);
+int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_cb_t cb, void *userdata);
+int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr);
+int sd_ipv4ll_set_index(sd_ipv4ll *ll, int interface_index);
+int sd_ipv4ll_start (sd_ipv4ll *ll);
+int sd_ipv4ll_stop (sd_ipv4ll *ll);
+void sd_ipv4ll_free (sd_ipv4ll *ll);
+int sd_ipv4ll_new (sd_ipv4ll **ret);
+
+#endif
diff --git a/src/systemd/sd-rtnl.h b/src/systemd/sd-rtnl.h
index f2b3a53..d56c14e 100644
--- a/src/systemd/sd-rtnl.h
+++ b/src/systemd/sd-rtnl.h
@@ -89,6 +89,7 @@ int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex);
 int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags);
 
 int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen);
+int sd_rtnl_message_route_set_scope(sd_rtnl_message *m, unsigned char scope);
 
 int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data);
 int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data);
-- 
1.7.10.4



More information about the systemd-devel mailing list