chiark / gitweb /
networkd: add dhcp server support
[elogind.git] / src / network / networkd-link.c
index 7cb9ee675418101ac966b5926f0e2bba365434d9..4b59af4ab093b5f8343e047605e0d9e5bc8fdc80 100644 (file)
@@ -75,12 +75,16 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (!link->ifname)
                 return -ENOMEM;
 
-        r = asprintf(&link->state_file, "/run/systemd/network/links/%"PRIu64,
+        r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
+        if (r < 0)
+                return r;
+
+        r = asprintf(&link->state_file, "/run/systemd/netif/links/%"PRIu64,
                      link->ifindex);
         if (r < 0)
                 return -ENOMEM;
 
-        r = asprintf(&link->lease_file, "/run/systemd/network/leases/%"PRIu64,
+        r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%"PRIu64,
                      link->ifindex);
         if (r < 0)
                 return -ENOMEM;
@@ -96,11 +100,18 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
 }
 
 static void link_free(Link *link) {
+        Address *address;
+
         if (!link)
                 return;
 
         assert(link->manager);
 
+        while ((address = link->addresses)) {
+                LIST_REMOVE(addresses, link->addresses, address);
+                address_free(address);
+        }
+
         sd_dhcp_client_unref(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 
@@ -160,26 +171,13 @@ void link_drop(Link *link) {
 
         link->state = LINK_STATE_LINGER;
 
-        log_debug_link(link, "dropped");
+        log_debug_link(link, "link removed");
 
         link_unref(link);
 
         return;
 }
 
-static int link_enter_configured(Link *link) {
-        assert(link);
-        assert(link->state == LINK_STATE_SETTING_ROUTES);
-
-        log_info_link(link, "link configured");
-
-        link->state = LINK_STATE_CONFIGURED;
-
-        link_save(link);
-
-        return 0;
-}
-
 static void link_enter_unmanaged(Link *link) {
         assert(link);
 
@@ -220,6 +218,16 @@ static int link_stop_clients(Link *link) {
                 }
         }
 
+        if (link->network->dhcp_server) {
+                assert(link->dhcp_server);
+
+                k = sd_dhcp_server_stop(link->dhcp_server);
+                if (k < 0) {
+                        log_warning_link(link, "Could not stop DHCPv4 server: %s", strerror(-r));
+                        r = k;
+                }
+        }
+
         return r;
 }
 
@@ -238,6 +246,37 @@ static void link_enter_failed(Link *link) {
         link_save(link);
 }
 
+static int link_enter_configured(Link *link) {
+        int r;
+
+        assert(link);
+        assert(link->network);
+        assert(link->state == LINK_STATE_SETTING_ROUTES);
+
+
+        if (link->network->dhcp_server) {
+                log_debug_link(link, "offering DHCPv4 leases");
+
+                r = sd_dhcp_server_start(link->dhcp_server);
+                if (r < 0) {
+                        log_warning_link(link, "could not start DHCPv4 server "
+                                         "instance: %s", strerror(-r));
+
+                        link_enter_failed(link);
+
+                        return 0;
+                }
+        }
+
+        log_info_link(link, "link configured");
+
+        link->state = LINK_STATE_CONFIGURED;
+
+        link_save(link);
+
+        return 0;
+}
+
 static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         Link *link = userdata;
         int r;
@@ -257,7 +296,8 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST)
                 log_struct_link(LOG_WARNING, link,
-                                "MESSAGE=%s: could not set route: %s",
+                                "MESSAGE=%*s: could not set route: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -290,7 +330,7 @@ static int link_enter_set_routes(Link *link) {
 
         log_debug_link(link, "setting routes");
 
-        LIST_FOREACH(static_routes, rt, link->network->static_routes) {
+        LIST_FOREACH(routes, rt, link->network->static_routes) {
                 r = route_configure(rt, link, &route_handler);
                 if (r < 0) {
                         log_warning_link(link,
@@ -420,9 +460,10 @@ static int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata)
         }
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0 && r != -ENOENT)
+        if (r < 0 && r != -ESRCH)
                 log_struct_link(LOG_WARNING, link,
-                                "MESSAGE=%s: could not drop route: %s",
+                                "MESSAGE=%*s: could not drop route: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -453,7 +494,8 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST)
                 log_struct_link(LOG_WARNING, link,
-                                "MESSAGE=%s: could not set address: %s",
+                                "MESSAGE=%*s: could not set address: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -484,7 +526,7 @@ static int link_enter_set_addresses(Link *link) {
 
         log_debug_link(link, "setting addresses");
 
-        LIST_FOREACH(static_addresses, ad, link->network->static_addresses) {
+        LIST_FOREACH(addresses, ad, link->network->static_addresses) {
                 r = address_configure(ad, link, &address_handler);
                 if (r < 0) {
                         log_warning_link(link,
@@ -599,7 +641,8 @@ static int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userd
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -ENOENT)
                 log_struct_link(LOG_WARNING, link,
-                                "MESSAGE=%s: could not update address: %s",
+                                "MESSAGE=%*s: could not update address: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -623,9 +666,10 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat
         }
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0 && r != -ENOENT)
+        if (r < 0 && r != -EADDRNOTAVAIL)
                 log_struct_link(LOG_WARNING, link,
-                                "MESSAGE=%s: could not drop address: %s",
+                                "MESSAGE=%*s: could not drop address: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -648,7 +692,7 @@ static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata,
 
         r = sd_bus_message_get_errno(m);
         if (r < 0)
-                log_warning("Could not set hostname: %s", strerror(-r));
+                log_warning_link(link, "Could not set hostname: %s", strerror(-r));
 
         link_unref(link);
 
@@ -684,7 +728,7 @@ static int link_set_hostname(Link *link, const char *hostname) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_call_async(link->manager->bus, m, set_hostname_handler, link, 0, NULL);
+        r = sd_bus_call_async(link->manager->bus, NULL, m, set_hostname_handler, link, 0);
         if (r < 0)
                 log_error_link(link, "Could not set transient hostname: %s", strerror(-r));
 
@@ -827,7 +871,7 @@ static int dhcp_lease_lost(Link *link) {
                 if (r >= 0 && hostname) {
                         r = link_set_hostname(link, "");
                         if (r < 0)
-                                log_error("Failed to reset transient hostname");
+                                log_error_link(link, "Failed to reset transient hostname");
                 }
         }
 
@@ -842,8 +886,6 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
         struct in_addr netmask;
         struct in_addr gateway;
         unsigned prefixlen;
-        struct in_addr *nameservers;
-        size_t nameservers_size;
         int r;
 
         assert(client);
@@ -880,7 +922,8 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
 
         if (r >= 0)
                 log_struct_link(LOG_INFO, link,
-                                "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
+                                "MESSAGE=%*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
+                                 IFNAMSIZ,
                                  link->ifname,
                                  ADDRESS_FMT_VAL(address),
                                  prefixlen,
@@ -894,7 +937,8 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                                  NULL);
         else
                 log_struct_link(LOG_INFO, link,
-                                "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u",
+                                "MESSAGE=%*s: DHCPv4 address %u.%u.%u.%u/%u",
+                                 IFNAMSIZ,
                                  link->ifname,
                                  ADDRESS_FMT_VAL(address),
                                  prefixlen,
@@ -906,15 +950,6 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
 
         link->dhcp_lease = lease;
 
-        if (link->network->dhcp_dns) {
-                r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
-                if (r >= 0) {
-                        r = manager_update_resolv_conf(link->manager);
-                        if (r < 0)
-                                log_error("Failed to update resolv.conf");
-                }
-        }
-
         if (link->network->dhcp_mtu) {
                 uint16_t mtu;
 
@@ -934,7 +969,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                 if (r >= 0) {
                         r = link_set_hostname(link, hostname);
                         if (r < 0)
-                                log_error("Failed to set transient hostname "
+                                log_error_link(link, "Failed to set transient hostname "
                                           "to '%s'", hostname);
                 }
         }
@@ -1129,7 +1164,8 @@ static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
                 return r;
 
         log_struct_link(LOG_INFO, link,
-                        "MESSAGE=%s: IPv4 link-local address %u.%u.%u.%u",
+                        "MESSAGE=%*s: IPv4 link-local address %u.%u.%u.%u",
+                        IFNAMSIZ,
                         link->ifname,
                         ADDRESS_FMT_VAL(address),
                         NULL);
@@ -1226,8 +1262,13 @@ bool link_has_carrier(unsigned flags, uint8_t operstate) {
         return false;
 }
 
+#define FLAG_STRING(string, flag, old, new) \
+        (((old ^ new) & flag) \
+                ? ((old & flag) ? (" -" string) : (" +" string)) \
+                : "")
+
 static int link_update_flags(Link *link, sd_rtnl_message *m) {
-        unsigned flags, flags_added, flags_removed, generic_flags;
+        unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
         uint8_t operstate;
         bool carrier_gained = false, carrier_lost = false;
         int r;
@@ -1244,83 +1285,59 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
         if (r < 0)
                 /* if we got a message without operstate, take it to mean
                    the state was unchanged */
-                operstate = link->operstate;
+                operstate = link->kernel_operstate;
 
-        if ((link->flags == flags) && (link->operstate == operstate))
+        if ((link->flags == flags) && (link->kernel_operstate == operstate))
                 return 0;
 
-        flags_added = (link->flags ^ flags) & flags;
-        flags_removed = (link->flags ^ flags) & link->flags;
-        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_DORMANT | IFF_DEBUG |
-                          IFF_MULTICAST | IFF_BROADCAST | IFF_PROMISC |
-                          IFF_NOARP | IFF_MASTER | IFF_SLAVE | IFF_RUNNING);
-
-        if (flags_added & IFF_UP)
-                log_debug_link(link, "link is up");
-        else if (flags_removed & IFF_UP)
-                log_debug_link(link, "link is down");
-
-        if (flags_added & IFF_LOWER_UP)
-                log_debug_link(link, "link is lower up");
-        else if (flags_removed & IFF_LOWER_UP)
-                log_debug_link(link, "link is lower down");
-
-        if (flags_added & IFF_DORMANT)
-                log_debug_link(link, "link is dormant");
-        else if (flags_removed & IFF_DORMANT)
-                log_debug_link(link, "link is not dormant");
-
-        if (flags_added & IFF_DEBUG)
-                log_debug_link(link, "debugging enabled in the kernel");
-        else if (flags_removed & IFF_DEBUG)
-                log_debug_link(link, "debugging disabled in the kernel");
-
-        if (flags_added & IFF_MULTICAST)
-                log_debug_link(link, "multicast enabled");
-        else if (flags_removed & IFF_MULTICAST)
-                log_debug_link(link, "multicast disabled");
-
-        if (flags_added & IFF_BROADCAST)
-                log_debug_link(link, "broadcast enabled");
-        else if (flags_removed & IFF_BROADCAST)
-                log_debug_link(link, "broadcast disabled");
-
-        if (flags_added & IFF_PROMISC)
-                log_debug_link(link, "promiscuous mode enabled");
-        else if (flags_removed & IFF_PROMISC)
-                log_debug_link(link, "promiscuous mode disabled");
-
-        if (flags_added & IFF_NOARP)
-                log_debug_link(link, "ARP protocol disabled");
-        else if (flags_removed & IFF_NOARP)
-                log_debug_link(link, "ARP protocol enabled");
-
-        if (flags_added & IFF_MASTER)
-                log_debug_link(link, "link is master");
-        else if (flags_removed & IFF_MASTER)
-                log_debug_link(link, "link is no longer master");
-
-        if (flags_added & IFF_SLAVE)
-                log_debug_link(link, "link is slave");
-        else if (flags_removed & IFF_SLAVE)
-                log_debug_link(link, "link is no longer slave");
-
-        /* link flags are currently at most 18 bits, let's default to printing 20 */
-        if (flags_added & generic_flags)
-                log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
-                               flags_added & generic_flags);
-
-        if (flags_removed & generic_flags)
-                log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
-                               flags_removed & generic_flags);
-
-        carrier_gained = !link_has_carrier(link->flags, link->operstate) &&
+        if (link->flags != flags) {
+                log_debug_link(link, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
+                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
+                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
+                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
+                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
+                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
+                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
+                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
+                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
+                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
+                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
+                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
+                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
+                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
+                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
+                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
+                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
+                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
+                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
+
+                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
+                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
+                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
+                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
+                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
+                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
+                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
+                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
+
+                /* link flags are currently at most 18 bits, let's align to printing 20 */
+                if (unknown_flags_added)
+                        log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
+                                       unknown_flags_added);
+
+                if (unknown_flags_removed)
+                        log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
+                                       unknown_flags_removed);
+        }
+
+        carrier_gained = !link_has_carrier(link->flags, link->kernel_operstate) &&
                        link_has_carrier(flags, operstate);
-        carrier_lost = link_has_carrier(link->flags, link->operstate) &&
+        carrier_lost = link_has_carrier(link->flags, link->kernel_operstate) &&
                          !link_has_carrier(flags, operstate);
 
         link->flags = flags;
-        link->operstate = operstate;
+        link->kernel_operstate = operstate;
 
         link_save(link);
 
@@ -1367,7 +1384,8 @@ static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 /* we warn but don't fail the link, as it may
                    be brought up later */
                 log_struct_link(LOG_WARNING, link,
-                                "MESSAGE=%s: could not bring up interface: %s",
+                                "MESSAGE=%*s: could not bring up interface: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -1453,7 +1471,8 @@ static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         r = sd_rtnl_message_get_errno(m);
         if (r < 0) {
                 log_struct_link(LOG_ERR, link,
-                                "MESSAGE=%s: could not enslave: %s",
+                                "MESSAGE=%*s: could not enslave: %s",
+                                IFNAMSIZ,
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
@@ -1485,23 +1504,27 @@ static int link_enter_enslave(Link *link) {
 
         link_save(link);
 
-        if (!link->network->bridge && !link->network->bond &&
+        if (!link->network->bridge &&
+            !link->network->bond &&
+            !link->network->tunnel &&
             hashmap_isempty(link->network->vlans) &&
             hashmap_isempty(link->network->macvlans))
                 return link_enslaved(link);
 
         if (link->network->bond) {
                 log_struct_link(LOG_DEBUG, link,
-                                "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, link->network->bond->name,
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, link->network->bond->ifname,
                                 NETDEV(link->network->bond),
                                 NULL);
 
                 r = netdev_enslave(link->network->bond, link, &enslave_handler);
                 if (r < 0) {
                         log_struct_link(LOG_WARNING, link,
-                                        "MESSAGE=%s: could not enslave by '%s': %s",
-                                        link->ifname, link->network->bond->name, strerror(-r),
+                                        "MESSAGE=%*s: could not enslave by '%s': %s",
+                                        IFNAMSIZ,
+                                        link->ifname, link->network->bond->ifname, strerror(-r),
                                         NETDEV(link->network->bond),
                                         NULL);
                         link_enter_failed(link);
@@ -1514,16 +1537,18 @@ static int link_enter_enslave(Link *link) {
 
         if (link->network->bridge) {
                 log_struct_link(LOG_DEBUG, link,
-                                "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, link->network->bridge->name,
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, link->network->bridge->ifname,
                                 NETDEV(link->network->bridge),
                                 NULL);
 
                 r = netdev_enslave(link->network->bridge, link, &enslave_handler);
                 if (r < 0) {
                         log_struct_link(LOG_WARNING, link,
-                                        "MESSAGE=%s: could not enslave by '%s': %s",
-                                        link->ifname, link->network->bridge->name, strerror(-r),
+                                        "MESSAGE=%*s: could not enslave by '%s': %s",
+                                        IFNAMSIZ,
+                                        link->ifname, link->network->bridge->ifname, strerror(-r),
                                         NETDEV(link->network->bridge),
                                         NULL);
                         link_enter_failed(link);
@@ -1534,16 +1559,42 @@ static int link_enter_enslave(Link *link) {
                 link->enslaving ++;
         }
 
+        if (link->network->tunnel) {
+                log_struct_link(LOG_DEBUG, link,
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, link->network->tunnel->ifname,
+                                NETDEV(link->network->tunnel),
+                                NULL);
+
+                r = netdev_enslave(link->network->tunnel, link, &enslave_handler);
+                if (r < 0) {
+                        log_struct_link(LOG_WARNING, link,
+                                        "MESSAGE=%*s: could not enslave by '%s': %s",
+                                        IFNAMSIZ,
+                                        link->ifname, link->network->tunnel->ifname, strerror(-r),
+                                        NETDEV(link->network->tunnel),
+                                        NULL);
+                        link_enter_failed(link);
+                        return r;
+                }
+
+                link_ref(link);
+                link->enslaving ++;
+        }
+
         HASHMAP_FOREACH(vlan, link->network->vlans, i) {
                 log_struct_link(LOG_DEBUG, link,
-                                "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, vlan->name, NETDEV(vlan), NULL);
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, vlan->ifname, NETDEV(vlan), NULL);
 
                 r = netdev_enslave(vlan, link, &enslave_handler);
                 if (r < 0) {
                         log_struct_link(LOG_WARNING, link,
-                                        "MESSAGE=%s: could not enslave by '%s': %s",
-                                        link->ifname, vlan->name, strerror(-r),
+                                        "MESSAGE=%*s: could not enslave by '%s': %s",
+                                        IFNAMSIZ,
+                                        link->ifname, vlan->ifname, strerror(-r),
                                         NETDEV(vlan), NULL);
                         link_enter_failed(link);
                         return r;
@@ -1555,14 +1606,16 @@ static int link_enter_enslave(Link *link) {
 
         HASHMAP_FOREACH(macvlan, link->network->macvlans, i) {
                 log_struct_link(LOG_DEBUG, link,
-                                "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, macvlan->name, NETDEV(macvlan), NULL);
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, macvlan->ifname, NETDEV(macvlan), NULL);
 
                 r = netdev_enslave(macvlan, link, &enslave_handler);
                 if (r < 0) {
                         log_struct_link(LOG_WARNING, link,
-                                        "MESSAGE=%s: could not enslave by '%s': %s",
-                                        link->ifname, macvlan->name, strerror(-r),
+                                        "MESSAGE=%*s: could not enslave by '%s': %s",
+                                        IFNAMSIZ,
+                                        link->ifname, macvlan->ifname, strerror(-r),
                                         NETDEV(macvlan), NULL);
                         link_enter_failed(link);
                         return r;
@@ -1642,6 +1695,51 @@ static int link_configure(Link *link) {
                 }
         }
 
+        if (link->network->dhcp_server) {
+                Address *address;
+
+                r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0);
+                if (r < 0)
+                        return r;
+
+                LIST_FOREACH(addresses, address,
+                             link->network->static_addresses) {
+                        struct in_addr pool_start;
+
+                        if (address->family != AF_INET)
+                                continue;
+
+                        /* currently this is picked essentially at random */
+                        r = sd_dhcp_server_set_address(link->dhcp_server,
+                                                       &address->in_addr.in);
+                        if (r < 0)
+                                return r;
+
+                        /* offer 32 addresses starting from the address following the server address */
+                        pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1);
+                        r = sd_dhcp_server_set_lease_pool(link->dhcp_server,
+                                                          &pool_start, 32);
+
+                        break;
+                }
+
+                /* TODO:
+                r = sd_dhcp_server_set_router(link->dhcp_server,
+                                              &main_address->in_addr.in);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_server_set_prefixlen(link->dhcp_server,
+                                                 main_address->prefixlen);
+                if (r < 0)
+                        return r;
+                */
+        }
+
         if (link_has_carrier(link->flags, link->operstate)) {
                 r = link_acquire_conf(link);
                 if (r < 0)
@@ -1665,7 +1763,7 @@ int link_initialized(Link *link, struct udev_device *device) {
         if (device)
                 link->udev_device = udev_device_ref(device);
 
-        log_debug_link(link, "link initialized");
+        log_debug_link(link, "udev initialized link");
 
         r = network_get(link->manager, device, link->ifname, &link->mac, &network);
         if (r == -ENOENT) {
@@ -1685,13 +1783,160 @@ int link_initialized(Link *link, struct udev_device *device) {
         return 0;
 }
 
+int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
+        Manager *m = userdata;
+        Link *link = NULL;
+        uint16_t type;
+        _cleanup_address_free_ Address *address = NULL;
+        Address *ad;
+        char buf[INET6_ADDRSTRLEN];
+        bool address_dropped = false;
+        int r, ifindex;
+
+        assert(rtnl);
+        assert(message);
+        assert(m);
+
+        r = sd_rtnl_message_get_type(message, &type);
+        if (r < 0) {
+                log_warning("rtnl: could not get message type");
+                return 0;
+        }
+
+        r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
+        if (r < 0 || ifindex <= 0) {
+                log_warning("rtnl: received address message without valid ifindex, ignoring");
+                return 0;
+        } else {
+                r = link_get(m, ifindex, &link);
+                if (r < 0 || !link) {
+                        log_warning("rtnl: received address for a nonexistent link, ignoring");
+                        return 0;
+                }
+        }
+
+        r = address_new_dynamic(&address);
+        if (r < 0)
+                return 0;
+
+        r = sd_rtnl_message_addr_get_family(message, &address->family);
+        if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) {
+                log_warning_link(link, "rtnl: received address with invalid family, ignoring");
+                return 0;
+        }
+
+        r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
+        if (r < 0) {
+                log_warning_link(link, "rtnl: received address with invalid prefixlen, ignoring");
+                return 0;
+        }
+
+        r = sd_rtnl_message_addr_get_scope(message, &address->scope);
+        if (r < 0) {
+                log_warning_link(link, "rtnl: received address with invalid scope, ignoring");
+                return 0;
+        }
+
+        switch (address->family) {
+        case AF_INET:
+                r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in);
+                if (r < 0) {
+                        log_warning_link(link, "rtnl: received address without valid address, ignoring");
+                        return 0;
+                }
+
+                break;
+
+        case AF_INET6:
+                r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6);
+                if (r < 0) {
+                        log_warning_link(link, "rtnl: received address without valid address, ignoring");
+                        return 0;
+                }
+
+                break;
+
+        default:
+                assert_not_reached("invalid address family");
+        }
+
+        if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) {
+                log_warning_link(link, "could not print address");
+                return 0;
+        }
+
+        LIST_FOREACH(addresses, ad, link->addresses) {
+                if (address_equal(ad, address)) {
+                        LIST_REMOVE(addresses, link->addresses, ad);
+
+                        address_free(ad);
+
+                        address_dropped = true;
+
+                        break;
+                }
+        }
+
+        switch (type) {
+        case RTM_NEWADDR:
+                if (!address_dropped)
+                        log_debug_link(link, "added address: %s/%u", buf,
+                                      address->prefixlen);
+
+                LIST_PREPEND(addresses, link->addresses, address);
+                address = NULL;
+
+                link_save(link);
+
+                break;
+        case RTM_DELADDR:
+                if (address_dropped) {
+                        log_debug_link(link, "removed address: %s/%u", buf,
+                                      address->prefixlen);
+
+                        link_save(link);
+                }
+
+                break;
+        default:
+                assert_not_reached("Received invalid RTNL message type");
+        }
+
+        return 1;
+}
+
+static int link_get_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Link *link = userdata;
+        int r;
+
+        assert(rtnl);
+        assert(m);
+        assert(link);
+
+        for (; m; m = sd_rtnl_message_next(m)) {
+                r = sd_rtnl_message_get_errno(m);
+                if (r < 0) {
+                        log_debug_link(link, "getting address failed: %s", strerror(-r));
+                        continue;
+                }
+
+                r = link_rtnl_process_address(rtnl, m, link->manager);
+                if (r < 0)
+                        log_warning_link(link, "could not process address: %s", strerror(-r));
+        }
+
+        return 1;
+}
+
 int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
         Link *link;
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
         char ifindex_str[2 + DECIMAL_STR_MAX(int)];
         int r;
 
         assert(m);
+        assert(m->rtnl);
         assert(message);
         assert(ret);
 
@@ -1701,7 +1946,15 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
 
         link = *ret;
 
-        log_debug_link(link, "link added");
+        log_debug_link(link, "link %"PRIu64" added", link->ifindex);
+
+        r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, link->ifindex, 0);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_call_async(m->rtnl, req, link_get_address_handler, link, 0, NULL);
+        if (r < 0)
+                return r;
 
         if (detect_container(NULL) <= 0) {
                 /* not in a container, udev will be around */
@@ -1712,9 +1965,11 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
                         return -errno;
                 }
 
-                if (udev_device_get_is_initialized(device) <= 0)
+                if (udev_device_get_is_initialized(device) <= 0) {
                         /* not yet ready */
+                        log_debug_link(link, "udev initializing link...");
                         return 0;
+                }
         }
 
         r = link_initialized(link, device);
@@ -1733,6 +1988,12 @@ int link_update(Link *link, sd_rtnl_message *m) {
         assert(link->ifname);
         assert(m);
 
+        if (link->state == LINK_STATE_LINGER) {
+                link_ref(link);
+                log_info_link(link, "link readded");
+                link->state = LINK_STATE_ENSLAVING;
+        }
+
         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
         if (r >= 0 && !streq(ifname, link->ifname)) {
                 log_info_link(link, "renamed to %s", ifname);
@@ -1792,10 +2053,60 @@ int link_update(Link *link, sd_rtnl_message *m) {
         return link_update_flags(link, m);
 }
 
+static void serialize_addresses(FILE *f, const char *key, Address *address) {
+        Address *ad;
+
+        assert(f);
+        assert(key);
+
+        if (!address)
+                return;
+
+        fprintf(f, "%s=", key);
+
+        LIST_FOREACH(addresses, ad, address) {
+                char buf[INET6_ADDRSTRLEN];
+
+                if (inet_ntop(ad->family, &ad->in_addr, buf, INET6_ADDRSTRLEN))
+                        fprintf(f, "%s%s", buf, (ad->addresses_next) ? " ": "");
+        }
+
+        fputs("\n", f);
+}
+
+static void link_update_operstate(Link *link) {
+
+        assert(link);
+
+        if (link->kernel_operstate == IF_OPER_DORMANT)
+                link->operstate = LINK_OPERSTATE_DORMANT;
+        else if (link_has_carrier(link->flags, link->kernel_operstate)) {
+                Address *address;
+                uint8_t scope = RT_SCOPE_NOWHERE;
+
+                /* if we have carrier, check what addresses we have */
+                LIST_FOREACH(addresses, address, link->addresses) {
+                        if (address->scope < scope)
+                                scope = address->scope;
+                }
+
+                if (scope < RT_SCOPE_SITE)
+                        /* universally accessible addresses found */
+                        link->operstate = LINK_OPERSTATE_ROUTABLE;
+                else if (scope < RT_SCOPE_HOST)
+                        /* only link or site local addresses found */
+                        link->operstate = LINK_OPERSTATE_DEGRADED;
+                else
+                        /* no useful addresses found */
+                        link->operstate = LINK_OPERSTATE_CARRIER;
+        } else
+                link->operstate = LINK_OPERSTATE_UNKNOWN;
+}
+
 int link_save(Link *link) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        const char *admin_state, *oper_state = "unknown";
+        const char *admin_state, *oper_state;
         int r;
 
         assert(link);
@@ -1803,6 +2114,8 @@ int link_save(Link *link) {
         assert(link->lease_file);
         assert(link->manager);
 
+        link_update_operstate(link);
+
         r = manager_save(link->manager);
         if (r < 0)
                 return r;
@@ -1815,10 +2128,8 @@ int link_save(Link *link) {
         admin_state = link_state_to_string(link->state);
         assert(admin_state);
 
-        if (link->operstate == IF_OPER_DORMANT)
-                oper_state = "dormant";
-        else if (link_has_carrier(link->flags, link->operstate))
-                oper_state = "carrier";
+        oper_state = link_operstate_to_string(link->operstate);
+        assert(oper_state);
 
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
@@ -1833,12 +2144,23 @@ int link_save(Link *link) {
                 "FLAGS=%u\n",
                 admin_state, oper_state, link->flags);
 
+        if (link->network) {
+                serialize_addresses(f, "DNS", link->network->dns);
+                serialize_addresses(f, "NTP", link->network->ntp);
+        }
+
         if (link->dhcp_lease) {
                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
                 if (r < 0)
                         goto finish;
 
-                fprintf(f, "DHCP_LEASE=%s\n", link->lease_file);
+                fprintf(f,
+                        "DHCP_LEASE=%s\n"
+                        "DHCP_USE_DNS=%s\n"
+                        "DHCP_USE_NTP=%s\n",
+                        link->lease_file,
+                        yes_no(link->network->dhcp_dns),
+                        yes_no(link->network->dhcp_ntp));
         } else
                 unlink(link->lease_file);
 
@@ -1852,7 +2174,7 @@ int link_save(Link *link) {
 
 finish:
         if (r < 0)
-                log_error("Failed to save link data to %s: %s", link->state_file, strerror(-r));
+                log_error_link(link, "Failed to save link data to %s: %s", link->state_file, strerror(-r));
 
         return r;
 }
@@ -1869,3 +2191,13 @@ static const char* const link_state_table[_LINK_STATE_MAX] = {
 };
 
 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
+
+static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
+        [LINK_OPERSTATE_UNKNOWN] = "unknown",
+        [LINK_OPERSTATE_DORMANT] = "dormant",
+        [LINK_OPERSTATE_CARRIER] = "carrier",
+        [LINK_OPERSTATE_DEGRADED] = "degraded",
+        [LINK_OPERSTATE_ROUTABLE] = "routable",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);