chiark / gitweb /
resolved: add daemon to manage resolv.conf
[elogind.git] / src / network / networkd-link.c
index 058fbc24f11212a782912c1c70f67762373aff7c..5a7472ba24a09dcdb2337fbf4f8b2d37de0cb493 100644 (file)
@@ -37,7 +37,7 @@ static int ipv4ll_address_update(Link *link, bool deprecate);
 static bool ipv4ll_is_bound(sd_ipv4ll *ll);
 
 static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
-        _cleanup_link_free_ Link *link = NULL;
+        _cleanup_link_unref_ Link *link = NULL;
         uint16_t type;
         char *ifname;
         int r, ifindex;
@@ -67,6 +67,7 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (!link)
                 return -ENOMEM;
 
+        link->n_ref = 1;
         link->manager = manager;
         link->state = LINK_STATE_INITIALIZING;
         link->ifindex = ifindex;
@@ -79,6 +80,11 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (r < 0)
                 return -ENOMEM;
 
+        r = asprintf(&link->lease_file, "/run/systemd/network/leases/%"PRIu64,
+                     link->ifindex);
+        if (r < 0)
+                return -ENOMEM;
+
         r = hashmap_put(manager->links, &link->ifindex, link);
         if (r < 0)
                 return r;
@@ -89,20 +95,32 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         return 0;
 }
 
-void link_free(Link *link) {
+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);
 
+        unlink(link->lease_file);
+        free(link->lease_file);
+
         sd_ipv4ll_unref(link->ipv4ll);
 
         hashmap_remove(link->manager->links, &link->ifindex);
 
         free(link->ifname);
+
+        unlink(link->state_file);
         free(link->state_file);
 
         udev_device_unref(link->udev_device);
@@ -110,6 +128,20 @@ void link_free(Link *link) {
         free(link);
 }
 
+Link *link_unref(Link *link) {
+        if (link && (-- link->n_ref <= 0))
+                link_free(link);
+
+        return NULL;
+}
+
+Link *link_ref(Link *link) {
+        if (link)
+                assert_se(++ link->n_ref >= 2);
+
+        return link;
+}
+
 int link_get(Manager *m, int ifindex, Link **ret) {
         Link *link;
         uint64_t ifindex_64;
@@ -129,6 +161,19 @@ int link_get(Manager *m, int ifindex, Link **ret) {
         return 0;
 }
 
+void link_drop(Link *link) {
+        if (!link || link->state == LINK_STATE_LINGER)
+                return;
+
+        link->state = LINK_STATE_LINGER;
+
+        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);
@@ -188,7 +233,7 @@ static int link_stop_clients(Link *link) {
 static void link_enter_failed(Link *link) {
         assert(link);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                 return;
 
         log_warning_link(link, "failed");
@@ -205,19 +250,22 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         int r;
 
         assert(link->route_messages > 0);
-        assert(link->state == LINK_STATE_SETTING_ADDRESSES ||
-               link->state == LINK_STATE_SETTING_ROUTES ||
-               link->state == LINK_STATE_FAILED);
+        assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES,
+                      LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED,
+                      LINK_STATE_LINGER));
 
         link->route_messages --;
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         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);
@@ -229,6 +277,8 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 link_enter_configured(link);
         }
 
+        link_unref(link);
+
         return 1;
 }
 
@@ -243,12 +293,12 @@ static int link_enter_set_routes(Link *link) {
         link->state = LINK_STATE_SETTING_ROUTES;
 
         if (!link->network->static_routes && !link->dhcp_lease &&
-                (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
+            (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
                 return link_enter_configured(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,
@@ -257,6 +307,7 @@ static int link_enter_set_routes(Link *link) {
                         return r;
                 }
 
+                link_ref(link);
                 link->route_messages ++;
         }
 
@@ -291,6 +342,7 @@ static int link_enter_set_routes(Link *link) {
                                 return r;
                         }
 
+                        link_ref(link);
                         link->route_messages ++;
                 }
         }
@@ -336,6 +388,7 @@ static int link_enter_set_routes(Link *link) {
                                 return r;
                         }
 
+                        link_ref(link);
                         link->route_messages ++;
 
                         route->family = AF_INET;
@@ -349,6 +402,7 @@ static int link_enter_set_routes(Link *link) {
                                 return r;
                         }
 
+                        link_ref(link);
                         link->route_messages ++;
                 }
         }
@@ -368,17 +422,22 @@ static int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata)
         assert(link);
         assert(link->ifname);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         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);
 
+        link_unref(link);
+
         return 0;
 }
 
@@ -390,17 +449,21 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         assert(link);
         assert(link->ifname);
         assert(link->addr_messages > 0);
-        assert(link->state == LINK_STATE_SETTING_ADDRESSES || link->state == LINK_STATE_FAILED);
+        assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES,
+               LINK_STATE_FAILED, LINK_STATE_LINGER));
 
         link->addr_messages --;
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         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);
@@ -410,6 +473,8 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 link_enter_set_routes(link);
         }
 
+        link_unref(link);
+
         return 1;
 }
 
@@ -429,7 +494,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,
@@ -438,6 +503,7 @@ static int link_enter_set_addresses(Link *link) {
                         return r;
                 }
 
+                link_ref(link);
                 link->addr_messages ++;
         }
 
@@ -473,6 +539,7 @@ static int link_enter_set_addresses(Link *link) {
                                 return r;
                         }
 
+                        link_ref(link);
                         link->addr_messages ++;
                 }
         }
@@ -519,6 +586,7 @@ static int link_enter_set_addresses(Link *link) {
                         return r;
                 }
 
+                link_ref(link);
                 link->addr_messages ++;
         }
 
@@ -533,17 +601,22 @@ static int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userd
         assert(link);
         assert(link->ifname);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         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);
 
+        link_unref(link);
+
         return 0;
 }
 
@@ -555,45 +628,62 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat
         assert(link);
         assert(link->ifname);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         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);
 
+        link_unref(link);
+
         return 0;
 }
 
 static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
+        Link *link = userdata;
         int r;
 
+        assert(link);
+
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
+                return 1;
+        }
+
         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);
 
         return 1;
 }
 
-static int set_hostname(sd_bus *bus, const char *hostname) {
+static int link_set_hostname(Link *link, const char *hostname) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         int r = 0;
 
+        assert(link);
+        assert(link->manager);
         assert(hostname);
 
-        log_debug("Setting transient hostname: '%s'", hostname);
+        log_debug_link(link, "Setting transient hostname: '%s'", hostname);
 
-        if (!bus) { /* TODO: replace by assert when we can rely on kdbus */
-                log_info("Not connected to system bus, ignoring transient hostname.");
+        if (!link->manager->bus) { /* TODO: replace by assert when we can rely on kdbus */
+                log_info_link(link, "Not connected to system bus, ignoring transient hostname.");
                 return 0;
         }
 
         r = sd_bus_message_new_method_call(
-                        bus,
+                        link->manager->bus,
                         &m,
                         "org.freedesktop.hostname1",
                         "/org/freedesktop/hostname1",
@@ -606,9 +696,11 @@ static int set_hostname(sd_bus *bus, const char *hostname) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_call_async(bus, m, set_hostname_handler, NULL, 0, NULL);
+        r = sd_bus_call_async(link->manager->bus, NULL, m, set_hostname_handler, link, 0);
         if (r < 0)
-                log_error("Could not set transient hostname: %s", strerror(-r));
+                log_error_link(link, "Could not set transient hostname: %s", strerror(-r));
+
+        link_ref(link);
 
         return r;
 }
@@ -621,8 +713,10 @@ static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         assert(link);
         assert(link->ifname);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0)
@@ -632,6 +726,8 @@ static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                                 "ERRNO=%d", -r,
                                 NULL);
 
+        link_unref(link);
+
         return 1;
 }
 
@@ -665,6 +761,8 @@ static int link_set_mtu(Link *link, uint32_t mtu) {
                 return r;
         }
 
+        link_unref(link);
+
         return 0;
 }
 
@@ -695,6 +793,7 @@ static int dhcp_lease_lost(Link *link) {
                                 route_gw->scope = RT_SCOPE_LINK;
 
                                 route_drop(route_gw, link, &route_drop_handler);
+                                link_ref(link);
                         }
 
                         r = route_new_dynamic(&route);
@@ -703,6 +802,7 @@ static int dhcp_lease_lost(Link *link) {
                                 route->in_addr.in = gateway;
 
                                 route_drop(route, link, &route_drop_handler);
+                                link_ref(link);
                         }
                 }
 
@@ -715,6 +815,7 @@ static int dhcp_lease_lost(Link *link) {
                 address->prefixlen = prefixlen;
 
                 address_drop(address, link, &address_drop_handler);
+                link_ref(link);
         }
 
         if (link->network->dhcp_mtu) {
@@ -736,9 +837,9 @@ static int dhcp_lease_lost(Link *link) {
 
                 r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
                 if (r >= 0 && hostname) {
-                        r = set_hostname(link->manager->bus, "");
+                        r = link_set_hostname(link, "");
                         if (r < 0)
-                                log_error("Failed to reset transient hostname");
+                                log_error_link(link, "Failed to reset transient hostname");
                 }
         }
 
@@ -753,8 +854,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);
@@ -791,7 +890,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,
@@ -805,7 +905,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,
@@ -817,15 +918,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;
 
@@ -843,9 +935,9 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
 
                 r = sd_dhcp_lease_get_hostname(lease, &hostname);
                 if (r >= 0) {
-                        r = set_hostname(link->manager->bus, hostname);
+                        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);
                 }
         }
@@ -863,7 +955,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
         assert(link->network);
         assert(link->manager);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                 return;
 
         switch (event) {
@@ -963,6 +1055,7 @@ static int ipv4ll_address_update(Link *link, bool deprecate) {
                 address->broadcast.s_addr = address->in_addr.in.s_addr | htonl(0xfffffffflu >> address->prefixlen);
 
                 address_update(address, link, &address_update_handler);
+                link_ref(link);
         }
 
         return 0;
@@ -995,6 +1088,7 @@ static int ipv4ll_address_lost(Link *link) {
                 address->scope = RT_SCOPE_LINK;
 
                 address_drop(address, link, &address_drop_handler);
+                link_ref(link);
 
                 r = route_new_dynamic(&route);
                 if (r < 0) {
@@ -1008,6 +1102,7 @@ static int ipv4ll_address_lost(Link *link) {
                 route->metrics = 99;
 
                 route_drop(route, link, &route_drop_handler);
+                link_ref(link);
         }
 
         return 0;
@@ -1037,7 +1132,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);
@@ -1055,6 +1151,9 @@ static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
         assert(link->network);
         assert(link->manager);
 
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
+                return;
+
         switch(event) {
                 case IPV4LL_EVENT_STOP:
                 case IPV4LL_EVENT_CONFLICT:
@@ -1117,17 +1216,33 @@ static int link_acquire_conf(Link *link) {
         return 0;
 }
 
+bool link_has_carrier(unsigned flags, uint8_t operstate) {
+        /* see Documentation/networking/operstates.txt in the kernel sources */
+
+        if (operstate == IF_OPER_UP)
+                return true;
+
+        if (operstate == IF_OPER_UNKNOWN)
+                /* operstate may not be implemented, so fall back to flags */
+                if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT))
+                        return true;
+
+        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;
 
         assert(link);
 
-        if (link->state == LINK_STATE_FAILED)
-                return 0;
-
         r = sd_rtnl_message_link_get_flags(m, &flags);
         if (r < 0) {
                 log_warning_link(link, "Could not get link flags");
@@ -1143,70 +1258,46 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
         if ((link->flags == flags) && (link->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);
+        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->operstate) &&
                        link_has_carrier(flags, operstate);
@@ -1216,6 +1307,12 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
         link->flags = flags;
         link->operstate = operstate;
 
+        link_save(link);
+
+        if (link->state == LINK_STATE_FAILED ||
+            link->state == LINK_STATE_UNMANAGED)
+                return 0;
+
         if (carrier_gained) {
                 log_info_link(link, "gained carrier");
 
@@ -1245,20 +1342,25 @@ static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 
         assert(link);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0) {
                 /* 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);
         }
 
+        link_unref(link);
+
         return 1;
 }
 
@@ -1292,6 +1394,8 @@ static int link_up(Link *link) {
                 return r;
         }
 
+        link_ref(link);
+
         return 0;
 }
 
@@ -1321,22 +1425,27 @@ static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         int r;
 
         assert(link);
-        assert(link->state == LINK_STATE_ENSLAVING || link->state == LINK_STATE_FAILED);
+        assert(IN_SET(link->state, LINK_STATE_ENSLAVING, LINK_STATE_FAILED,
+                      LINK_STATE_LINGER));
         assert(link->network);
 
         link->enslaving --;
 
-        if (link->state == LINK_STATE_FAILED)
+        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
+                link_unref(link);
                 return 1;
+        }
 
         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);
                 link_enter_failed(link);
+                link_unref(link);
                 return 1;
         }
 
@@ -1345,6 +1454,8 @@ static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         if (link->enslaving == 0)
                 link_enslaved(link);
 
+        link_unref(link);
+
         return 1;
 }
 
@@ -1361,86 +1472,124 @@ 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'",
+                                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",
+                                        IFNAMSIZ,
+                                        link->ifname, link->network->bond->ifname, strerror(-r),
+                                        NETDEV(link->network->bond),
+                                        NULL);
+                        link_enter_failed(link);
+                        return r;
+                }
+
+                link_ref(link);
+                link->enslaving ++;
+        }
+
         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);
                         return r;
                 }
 
+                link_ref(link);
                 link->enslaving ++;
         }
 
-        if (link->network->bond) {
+        if (link->network->tunnel) {
                 log_struct_link(LOG_DEBUG, link,
-                                "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, link->network->bond->name,
-                                NETDEV(link->network->bond),
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, link->network->tunnel->ifname,
+                                NETDEV(link->network->tunnel),
                                 NULL);
 
-                r = netdev_enslave(link->network->bond, link, &enslave_handler);
+                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",
-                                        link->ifname, link->network->bond->name, strerror(-r),
-                                        NETDEV(link->network->bond),
+                                        "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;
                 }
 
+                link_ref(link);
                 link->enslaving ++;
         }
 
         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;
                 }
 
+                link_ref(link);
                 link->enslaving ++;
         }
 
@@ -1514,8 +1663,7 @@ static int link_configure(Link *link) {
                 }
         }
 
-        if (link_has_carrier(link->flags, link->operstate))
-        {
+        if (link_has_carrier(link->flags, link->operstate)) {
                 r = link_acquire_conf(link);
                 if (r < 0)
                         return r;
@@ -1538,7 +1686,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) {
@@ -1558,13 +1706,149 @@ 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: recevied address with invalid prefixlen, 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;
+
+                break;
+        case RTM_DELADDR:
+                if (address_dropped)
+                        log_debug_link(link, "removed address: %s/%u", buf,
+                                      address->prefixlen);
+
+                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);
 
@@ -1574,7 +1858,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 */
@@ -1585,9 +1877,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);
@@ -1606,8 +1900,11 @@ int link_update(Link *link, sd_rtnl_message *m) {
         assert(link->ifname);
         assert(m);
 
-        if (link->state == LINK_STATE_FAILED || link->state == LINK_STATE_UNMANAGED)
-                return 0;
+        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)) {
@@ -1668,17 +1965,54 @@ 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(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN))
+                        fprintf(f, "%s%s", buf, (ad->addresses_next) ? " ": "");
+        }
+
+        fputs("\n", f);
+}
+
 int link_save(Link *link) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        const char *state;
+        const char *admin_state, *oper_state = "unknown";
         int r;
 
         assert(link);
         assert(link->state_file);
+        assert(link->lease_file);
+        assert(link->manager);
 
-        state = link_state_to_string(link->state);
-        assert(state);
+        r = manager_save(link->manager);
+        if (r < 0)
+                return r;
+
+        if (link->state == LINK_STATE_LINGER) {
+                unlink(link->state_file);
+                return 0;
+        }
+
+        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";
 
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
@@ -1688,22 +2022,25 @@ int link_save(Link *link) {
 
         fprintf(f,
                 "# This is private data. Do not parse.\n"
-                "STATE=%s\n", state);
+                "ADMIN_STATE=%s\n"
+                "OPER_STATE=%s\n"
+                "FLAGS=%u\n",
+                admin_state, oper_state, link->flags);
 
-        if (link->dhcp_lease) {
-                _cleanup_free_ char *lease_file = NULL;
-
-                r = asprintf(&lease_file, "/run/systemd/network/leases/%"PRIu64,
-                             link->ifindex);
-                if (r < 0)
-                        return -ENOMEM;
+        if (link->network)
+                serialize_addresses(f, "DNS", link->network->dns);
 
-                r = dhcp_lease_save(link->dhcp_lease, lease_file);
+        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", lease_file);
-        }
+                fprintf(f,
+                        "DHCP_LEASE=%s\n"
+                        "DHCP_USE_DNS=%s\n",
+                        link->lease_file, yes_no(link->network->dhcp_dns));
+        } else
+                unlink(link->lease_file);
 
         fflush(f);
 
@@ -1715,19 +2052,20 @@ int link_save(Link *link) {
 
 finish:
         if (r < 0)
-                log_error("Failed to save link data %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;
 }
 
 static const char* const link_state_table[_LINK_STATE_MAX] = {
-        [LINK_STATE_INITIALIZING] = "configuring",
+        [LINK_STATE_INITIALIZING] = "initializing",
         [LINK_STATE_ENSLAVING] = "configuring",
         [LINK_STATE_SETTING_ADDRESSES] = "configuring",
         [LINK_STATE_SETTING_ROUTES] = "configuring",
         [LINK_STATE_CONFIGURED] = "configured",
         [LINK_STATE_UNMANAGED] = "unmanaged",
         [LINK_STATE_FAILED] = "failed",
+        [LINK_STATE_LINGER] = "linger",
 };
 
 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);