chiark / gitweb /
networkd: link - set mac addresses when starting clients
[elogind.git] / src / network / networkd-link.c
index 319f2904c0ea2639d6b1cfe5dbafd2740e372c29..cf22778b423fea0f7ab6bf5a71597dcb755b59af 100644 (file)
@@ -1059,17 +1059,20 @@ static int link_update_flags(Link *link, unsigned flags) {
 
         flags_added = (link->flags ^ flags) & flags;
         flags_removed = (link->flags ^ flags) & link->flags;
-        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_RUNNING);
+        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_DORMANT | IFF_DEBUG |
+                          IFF_MULTICAST | IFF_BROADCAST | IFF_PROMISC |
+                          IFF_NOARP | IFF_MASTER | IFF_SLAVE);
 
-        /* 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
+        /* consider link to have carrier when LOWER_UP and !DORMANT
+
+           TODO: use proper operstates once we start supporting 802.1X
+
+           see Documentation/networking/operstates.txt in the kernel sources
          */
-        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));
+        carrier_gained = (((flags_added & IFF_LOWER_UP) && !(flags & IFF_DORMANT)) ||
+                          ((flags_removed & IFF_DORMANT) && (flags & IFF_LOWER_UP)));
+        carrier_lost = ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) &&
+                       ((flags_removed & IFF_LOWER_UP) || (flags_added & IFF_DORMANT));
 
         link->flags = flags;
 
@@ -1090,18 +1093,54 @@ static int link_update_flags(Link *link, unsigned flags) {
         else if (flags_removed & IFF_LOWER_UP)
                 log_debug_link(link, "link is lower down");
 
-        if (flags_added & IFF_RUNNING)
-                log_debug_link(link, "link is running");
-        else if (flags_removed & IFF_RUNNING)
-                log_debug_link(link, "link is not running");
-
+        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, "ignored link flags gained: %#.8x",
+                log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
                                flags_added & generic_flags);
 
         if (flags_removed & generic_flags)
-                log_debug_link(link, "ignored link flags lost: %#.8x",
-                                flags_removed & generic_flags);
+                log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
+                               flags_removed & generic_flags);
 
         if (carrier_gained) {
                 log_info_link(link, "gained carrier");
@@ -1370,6 +1409,10 @@ static int link_configure(Link *link) {
                 if (r < 0)
                         return r;
 
+                r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
+                if (r < 0)
+                        return r;
+
                 r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
                 if (r < 0)
                         return r;
@@ -1388,6 +1431,10 @@ static int link_configure(Link *link) {
                 if (r < 0)
                         return r;
 
+                r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
+                if (r < 0)
+                        return r;
+
                 r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
                 if (r < 0)
                         return r;
@@ -1487,14 +1534,26 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
 int link_update(Link *link, sd_rtnl_message *m) {
         unsigned flags;
         struct ether_addr mac;
+        char *ifname;
         int r;
 
         assert(link);
+        assert(link->ifname);
         assert(m);
 
         if (link->state == LINK_STATE_FAILED)
                 return 0;
 
+        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);
+
+                free(link->ifname);
+                link->ifname = strdup(ifname);
+                if (!link->ifname)
+                        return -ENOMEM;
+        }
+
         if (!link->original_mtu) {
                 r = sd_rtnl_message_read_u16(m, IFLA_MTU, &link->original_mtu);
                 if (r >= 0)