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