chiark / gitweb /
udev: link-config - move naming policy from udev rules
[elogind.git] / src / udev / net / link-config.c
index 902ed0955d23bbd2c17549caeac4be181849e2e5..9c75b62b735eb78858d7e16f378601ce97c5d0e9 100644 (file)
@@ -20,6 +20,7 @@
 ***/
 
 #include <netinet/ether.h>
+#include <net/if.h>
 
 #include "link-config.h"
 
@@ -33,6 +34,7 @@
 #include "path-util.h"
 #include "conf-parser.h"
 #include "conf-files.h"
+#include "fileio.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -252,6 +254,8 @@ int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_confi
 
 static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const char *mac, unsigned int mtu) {
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
+        char new_name[IFNAMSIZ];
+        struct ether_addr new_mac;
         bool need_update;
         int r;
 
@@ -263,7 +267,8 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
                 return r;
 
         if (name) {
-                r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
+                strscpy(new_name, IFNAMSIZ, name);
+                r = sd_rtnl_message_append(message, IFLA_IFNAME, new_name);
                 if (r < 0)
                         return r;
 
@@ -271,7 +276,16 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
         }
 
         if (mac) {
-                r = sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac));
+                r = sscanf(mac, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+                           &new_mac.ether_addr_octet[0],
+                           &new_mac.ether_addr_octet[1],
+                           &new_mac.ether_addr_octet[2],
+                           &new_mac.ether_addr_octet[3],
+                           &new_mac.ether_addr_octet[4],
+                           &new_mac.ether_addr_octet[5]);
+                if (r != 6)
+                        return -EINVAL;
+                r = sd_rtnl_message_append(message, IFLA_ADDRESS, &new_mac);
                 if (r < 0)
                         return r;
 
@@ -295,8 +309,27 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
         return 0;
 }
 
+static bool enable_name_policy(void) {
+        _cleanup_free_ char *line;
+        char *w, *state;
+        int r;
+        size_t l;
+
+        r = read_one_line_file("/proc/cmdline", &line);
+        if (r < 0) {
+                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+                return true; /* something is very wrong, let's not make it worse */
+        }
+
+        FOREACH_WORD_QUOTED(w, l, line, state)
+                if (strneq(w, "net.ifnames=0", l))
+                        return false;
+
+        return true;
+}
+
 int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
-        const char *name;
+        const char *name, *new_name = NULL;
         int r, ifindex;
 
         name = udev_device_get_sysname(device);
@@ -342,7 +375,35 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                 return -ENODEV;
         }
 
-        r = rtnl_set_properties(ctx->rtnl, ifindex, config->name, config->mac, config->mtu);
+        if (config->name_policy && enable_name_policy()) {
+                char **policy;
+
+                STRV_FOREACH(policy, config->name_policy) {
+                        if (streq(*policy, "onboard")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
+                                if (new_name)
+                                        break;
+                        } else if (streq(*policy, "slot")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
+                                if (new_name)
+                                        break;
+                        } else if (streq(*policy, "path")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
+                                if (new_name)
+                                        break;
+                        } else if (streq(*policy, "mac")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
+                                if (new_name)
+                                        break;
+                        } else
+                                log_warning("Invalid link naming policy '%s', ignoring.", *policy);
+                }
+        }
+
+        if (!new_name && config->name)
+                new_name = config->name;
+
+        r = rtnl_set_properties(ctx->rtnl, ifindex, new_name, config->mac, config->mtu);
         if (r < 0) {
                 log_warning("Could not set Name, MACAddress or MTU on %s", name);
                 return r;