chiark / gitweb /
networkd: link - ignore missing MAC address from NEWLINK message
[elogind.git] / src / network / networkd-link.c
index 63d253d94130e99fc6dbe86dc955cb455078af9d..927dbb196001bae83286b677439fab5b0231967e 100644 (file)
@@ -80,10 +80,10 @@ void link_free(Link *link) {
 
         assert(link->manager);
 
-        sd_dhcp_client_free(link->dhcp_client);
+        sd_dhcp_client_unref(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 
-        sd_ipv4ll_free(link->ipv4ll);
+        sd_ipv4ll_unref(link->ipv4ll);
 
         hashmap_remove(link->manager->links, &link->ifindex);
 
@@ -1031,6 +1031,8 @@ static int link_acquire_conf(Link *link) {
 }
 
 static int link_update_flags(Link *link, unsigned flags) {
+        unsigned flags_added, flags_removed, generic_flags;
+        bool carrier_gained, carrier_lost;
         int r;
 
         assert(link);
@@ -1042,50 +1044,78 @@ static int link_update_flags(Link *link, unsigned flags) {
         if (link->flags == flags)
                 return 0;
 
-        log_debug_link(link, "link status updated: %#.8x -> %#.8x",
-                       link->flags, flags);
+        flags_added = (link->flags ^ flags) & flags;
+        flags_removed = (link->flags ^ flags) & link->flags;
+        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_RUNNING);
 
-        if ((link->flags & IFF_UP) != (flags & IFF_UP))
-                log_info_link(link,
-                              "link is %s", flags & IFF_UP ? "up": "down");
+        /* consider link to have carrier when both RUNNING and LOWER_UP, as RUNNING
+           may mean that the oper state is unknown, in which case we should fall back to
+           simply trust LOWER_UP, even thought that is less reliable
+         */
+        carrier_gained = ((flags_added & IFF_LOWER_UP) && (flags & IFF_RUNNING)) ||
+                         ((flags_added & IFF_RUNNING) && (flags & IFF_LOWER_UP));
+        carrier_lost = ((link->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
+                        (IFF_RUNNING | IFF_LOWER_UP)) &&
+                       (flags_removed & (IFF_LOWER_UP | IFF_RUNNING));
 
-        if ((link->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
-                if (flags & IFF_LOWER_UP) {
-                        log_info_link(link, "carrier on");
+        link->flags = flags;
 
-                        if (link->network->dhcp || link->network->ipv4ll) {
-                                r = link_acquire_conf(link);
-                                if (r < 0) {
-                                        log_warning_link(link, "Could not acquire configuration: %s", strerror(-r));
-                                        link_enter_failed(link);
-                                        return r;
-                                }
+        if (flags_added & generic_flags)
+                log_debug_link(link, "link flags gained: %#.8x",
+                               flags_added & generic_flags);
+
+        if (flags_removed & generic_flags)
+                log_debug_link(link, "link flags lost: %#.8x",
+                                flags_removed & generic_flags);
+
+        if (flags_added & IFF_UP)
+                log_info_link(link, "link is up");
+        else if (flags_removed & IFF_UP)
+                log_info_link(link, "link is down");
+
+        if (flags_added & IFF_LOWER_UP)
+                log_info_link(link, "link is lower up");
+        else if (flags_removed & IFF_LOWER_UP)
+                log_info_link(link, "link is lower down");
+
+        if (flags_added & IFF_RUNNING)
+                log_info_link(link, "link is running");
+        else if (flags_removed & IFF_RUNNING)
+                log_info_link(link, "link is not running");
+
+        if (carrier_gained) {
+                log_info_link(link, "gained carrier");
+
+                if (link->network->dhcp || link->network->ipv4ll) {
+                        r = link_acquire_conf(link);
+                        if (r < 0) {
+                                log_warning_link(link, "Could not acquire configuration: %s", strerror(-r));
+                                link_enter_failed(link);
+                                return r;
                         }
-                } else {
-                        log_info_link(link, "carrier off");
+                }
+        } else if (carrier_lost) {
+                log_info_link(link, "lost carrier");
 
-                        if (link->network->dhcp) {
-                                r = sd_dhcp_client_stop(link->dhcp_client);
-                                if (r < 0) {
-                                        log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
-                                        link_enter_failed(link);
-                                        return r;
-                                }
+                if (link->network->dhcp) {
+                        r = sd_dhcp_client_stop(link->dhcp_client);
+                        if (r < 0) {
+                                log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
+                                link_enter_failed(link);
+                                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;
-                                }
+                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;
                         }
                 }
         }
 
-        link->flags = flags;
-
         return 0;
 }
 
@@ -1299,6 +1329,7 @@ static int link_getlink_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
         int r;
 
         assert(link);
+        assert(link->ifname);
 
         if (link->state == LINK_STATE_FAILED)
                 return 1;
@@ -1459,10 +1490,10 @@ int link_update(Link *link, sd_rtnl_message *m) {
                                        PRIu16, link->original_mtu);
         }
 
+        /* The kernel may broadcast NEWLINK messages without the MAC address
+           set, simply ignore them. */
         r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
-        if (r < 0)
-                log_debug_link(link, "Could not get MAC address: %s", strerror(-r));
-        else {
+        if (r >= 0) {
                 if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN)) {
 
                         memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN);
@@ -1510,11 +1541,15 @@ int link_update(Link *link, sd_rtnl_message *m) {
 int link_save(Link *link) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        const char *state;
         int r;
 
         assert(link);
         assert(link->state_file);
 
+        state = link_state_to_string(link->state);
+        assert(state);
+
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
                 goto finish;
@@ -1523,8 +1558,7 @@ int link_save(Link *link) {
 
         fprintf(f,
                 "# This is private data. Do not parse.\n"
-                "STATE=%s\n",
-                link_state_to_string(link->state));
+                "STATE=%s\n", state);
 
         if (link->dhcp_lease) {
                 _cleanup_free_ char *lease_file = NULL;