be0c12
From 2faf160d0b8122e0dca603a441db68dc38c1bab6 Mon Sep 17 00:00:00 2001
be0c12
From: Yu Watanabe <watanabe.yu+github@gmail.com>
be0c12
Date: Mon, 16 Dec 2019 23:44:42 +0900
be0c12
Subject: [PATCH] udev: introduce AlternativeNamesPolicy= setting
be0c12
be0c12
(cherry picked from commit ef1d2c07f9567dfea8a4e012d8779a4ded2d9ae6)
be0c12
be0c12
Related: #2005008
be0c12
---
be0c12
 man/systemd.link.xml                 | 11 +++++
be0c12
 src/udev/net/link-config-gperf.gperf |  1 +
be0c12
 src/udev/net/link-config.c           | 62 ++++++++++++++++++++++++++--
be0c12
 src/udev/net/link-config.h           |  5 +++
be0c12
 4 files changed, 76 insertions(+), 3 deletions(-)
be0c12
be0c12
diff --git a/man/systemd.link.xml b/man/systemd.link.xml
be0c12
index c8ebb751ee..13dcce0879 100644
be0c12
--- a/man/systemd.link.xml
be0c12
+++ b/man/systemd.link.xml
be0c12
@@ -343,6 +343,17 @@
be0c12
           </para>
be0c12
         </listitem>
be0c12
       </varlistentry>
be0c12
+      <varlistentry>
be0c12
+        <term><varname>AlternativeNamesPolicy=</varname></term>
be0c12
+        <listitem>
be0c12
+          <para>A space-separated list of policies by which the interface's alternative names
be0c12
+          should be set. Each of the policies may fail, and all successful policies are used. The
be0c12
+          available policies are <literal>database</literal>, <literal>onboard</literal>,
be0c12
+          <literal>slot</literal>, <literal>path</literal>, and <literal>mac</literal>. If the
be0c12
+          kernel does not support the alternative names, then this setting will be ignored.
be0c12
+          </para>
be0c12
+        </listitem>
be0c12
+      </varlistentry>
be0c12
       <varlistentry>
be0c12
         <term><varname>AlternativeName=</varname></term>
be0c12
         <listitem>
be0c12
diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf
be0c12
index 913c754145..df8404e7b8 100644
be0c12
--- a/src/udev/net/link-config-gperf.gperf
be0c12
+++ b/src/udev/net/link-config-gperf.gperf
be0c12
@@ -35,6 +35,7 @@ Link.MACAddress,                 config_parse_hwaddr,        0,
be0c12
 Link.NamePolicy,                 config_parse_name_policy,   0,                             offsetof(link_config, name_policy)
be0c12
 Link.Name,                       config_parse_ifname,        0,                             offsetof(link_config, name)
be0c12
 Link.AlternativeName,            config_parse_ifnames,       1,                             offsetof(link_config, alternative_names)
be0c12
+Link.AlternativeNamesPolicy,     config_parse_alternative_names_policy, 0,                  offsetof(link_config, alternative_names_policy)
be0c12
 Link.Alias,                      config_parse_ifalias,       0,                             offsetof(link_config, alias)
be0c12
 Link.MTUBytes,                   config_parse_mtu,           AF_UNSPEC,                     offsetof(link_config, mtu)
be0c12
 Link.BitsPerSecond,              config_parse_si_size,       0,                             offsetof(link_config, speed)
be0c12
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
be0c12
index 8e88c8e5c4..6ceb4c698e 100644
be0c12
--- a/src/udev/net/link-config.c
be0c12
+++ b/src/udev/net/link-config.c
be0c12
@@ -69,6 +69,7 @@ static void link_config_free(link_config *link) {
be0c12
         free(link->name_policy);
be0c12
         free(link->name);
be0c12
         strv_free(link->alternative_names);
be0c12
+        free(link->alternative_names_policy);
be0c12
         free(link->alias);
be0c12
 
be0c12
         free(link);
be0c12
@@ -349,6 +350,7 @@ static int get_mac(struct udev_device *device, bool want_random,
be0c12
 
be0c12
 int link_config_apply(link_config_ctx *ctx, link_config *config,
be0c12
                       struct udev_device *device, const char **name) {
be0c12
+        _cleanup_strv_free_ char **altnames = NULL;
be0c12
         bool respect_predictable = false;
be0c12
         struct ether_addr generated_mac;
be0c12
         struct ether_addr *mac = NULL;
be0c12
@@ -473,11 +475,52 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
be0c12
         if (r < 0)
be0c12
                 return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name);
be0c12
 
be0c12
-        r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, config->alternative_names);
be0c12
+        if (config->alternative_names) {
be0c12
+                altnames = strv_copy(config->alternative_names);
be0c12
+                if (!altnames)
be0c12
+                        return log_oom();
be0c12
+        }
be0c12
+
be0c12
+        if (config->alternative_names_policy)
be0c12
+                for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) {
be0c12
+                        const char *n;
be0c12
+
be0c12
+                        switch (*p) {
be0c12
+                        case NAMEPOLICY_DATABASE:
be0c12
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
be0c12
+                                break;
be0c12
+                        case NAMEPOLICY_ONBOARD:
be0c12
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
be0c12
+                                break;
be0c12
+                        case NAMEPOLICY_SLOT:
be0c12
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
be0c12
+                                break;
be0c12
+                        case NAMEPOLICY_PATH:
be0c12
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
be0c12
+                                break;
be0c12
+                        case NAMEPOLICY_MAC:
be0c12
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
be0c12
+                                break;
be0c12
+                        default:
be0c12
+                                assert_not_reached("invalid policy");
be0c12
+                        }
be0c12
+                        if (!isempty(n)) {
be0c12
+                                r = strv_extend(&altnames, n);
be0c12
+                                if (r < 0)
be0c12
+                                        return log_oom();
be0c12
+                        }
be0c12
+                }
be0c12
+
be0c12
+        if (new_name)
be0c12
+                strv_remove(altnames, new_name);
be0c12
+        strv_remove(altnames, old_name);
be0c12
+        strv_uniq(altnames);
be0c12
+
be0c12
+        r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, altnames);
be0c12
         if (r == -EOPNOTSUPP)
be0c12
-                log_debug_errno(r, "Could not set AlternativeName= on %s, ignoring: %m", old_name);
be0c12
+                log_debug_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s, ignoring: %m", old_name);
be0c12
         else if (r < 0)
be0c12
-                return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name);
be0c12
+                return log_warning_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s: %m", old_name);
be0c12
 
be0c12
         *name = new_name;
be0c12
 
be0c12
@@ -524,3 +567,16 @@ DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
be0c12
 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
be0c12
                           _NAMEPOLICY_INVALID,
be0c12
                           "Failed to parse interface name policy");
be0c12
+
be0c12
+static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
be0c12
+        [NAMEPOLICY_DATABASE] = "database",
be0c12
+        [NAMEPOLICY_ONBOARD] = "onboard",
be0c12
+        [NAMEPOLICY_SLOT] = "slot",
be0c12
+        [NAMEPOLICY_PATH] = "path",
be0c12
+        [NAMEPOLICY_MAC] = "mac",
be0c12
+};
be0c12
+
be0c12
+DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
be0c12
+DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy,
be0c12
+                          _NAMEPOLICY_INVALID,
be0c12
+                          "Failed to parse alternative names policy");
be0c12
diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h
be0c12
index 93d5fdce59..634bd2ec54 100644
be0c12
--- a/src/udev/net/link-config.h
be0c12
+++ b/src/udev/net/link-config.h
be0c12
@@ -49,6 +49,7 @@ struct link_config {
be0c12
         struct ether_addr *mac;
be0c12
         MACPolicy mac_policy;
be0c12
         NamePolicy *name_policy;
be0c12
+        NamePolicy *alternative_names_policy;
be0c12
         char *name;
be0c12
         char **alternative_names;
be0c12
         char *alias;
be0c12
@@ -78,6 +79,9 @@ int link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret
be0c12
 const char *name_policy_to_string(NamePolicy p) _const_;
be0c12
 NamePolicy name_policy_from_string(const char *p) _pure_;
be0c12
 
be0c12
+const char *alternative_names_policy_to_string(NamePolicy p) _const_;
be0c12
+NamePolicy alternative_names_policy_from_string(const char *p) _pure_;
be0c12
+
be0c12
 const char *mac_policy_to_string(MACPolicy p) _const_;
be0c12
 MACPolicy mac_policy_from_string(const char *p) _pure_;
be0c12
 
be0c12
@@ -86,3 +90,4 @@ const struct ConfigPerfItem* link_config_gperf_lookup(const char *key, GPERF_LEN
be0c12
 
be0c12
 int config_parse_mac_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
be0c12
 int config_parse_name_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
be0c12
+int config_parse_alternative_names_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);