teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0712-udev-support-AlternativeName-setting-in-.link-file.patch

da2bf9
From a29790ac578540ccb4264367603aba9bc41d1bf7 Mon Sep 17 00:00:00 2001
da2bf9
From: Yu Watanabe <watanabe.yu+github@gmail.com>
da2bf9
Date: Sun, 15 Dec 2019 23:21:18 +0900
da2bf9
Subject: [PATCH] udev: support AlternativeName= setting in .link file
da2bf9
da2bf9
(cherry picked from commit a5053a158b43c5ddee90f4915b9fc603e0191d6d)
da2bf9
da2bf9
Related: #2005008
da2bf9
---
da2bf9
 man/systemd.link.xml                     |  8 ++++
da2bf9
 src/libsystemd/sd-netlink/netlink-util.c | 40 ++++++++++++++++
da2bf9
 src/libsystemd/sd-netlink/netlink-util.h |  1 +
da2bf9
 src/shared/conf-parser.c                 | 60 ++++++++++++++++++++++++
da2bf9
 src/shared/conf-parser.h                 |  1 +
da2bf9
 src/udev/net/link-config-gperf.gperf     |  1 +
da2bf9
 src/udev/net/link-config.c               |  5 ++
da2bf9
 src/udev/net/link-config.h               |  1 +
da2bf9
 8 files changed, 117 insertions(+)
da2bf9
da2bf9
diff --git a/man/systemd.link.xml b/man/systemd.link.xml
da2bf9
index 32657308d0..0b0d83349d 100644
da2bf9
--- a/man/systemd.link.xml
da2bf9
+++ b/man/systemd.link.xml
da2bf9
@@ -343,6 +343,14 @@
da2bf9
           </para>
da2bf9
         </listitem>
da2bf9
       </varlistentry>
da2bf9
+      <varlistentry>
da2bf9
+        <term><varname>AlternativeName=</varname></term>
da2bf9
+        <listitem>
da2bf9
+          <para>The alternative interface name to use. This option can be specified multiple times.
da2bf9
+          If the empty string is assigned to this option, the list is reset, and all prior assignments
da2bf9
+          have no effect.</para>
da2bf9
+        </listitem>
da2bf9
+      </varlistentry>
da2bf9
       <varlistentry>
da2bf9
         <term><varname>MTUBytes=</varname></term>
da2bf9
         <listitem>
da2bf9
diff --git a/src/libsystemd/sd-netlink/netlink-util.c b/src/libsystemd/sd-netlink/netlink-util.c
da2bf9
index 3928dfbabf..c1c306f121 100644
da2bf9
--- a/src/libsystemd/sd-netlink/netlink-util.c
da2bf9
+++ b/src/libsystemd/sd-netlink/netlink-util.c
da2bf9
@@ -4,6 +4,7 @@
da2bf9
 
da2bf9
 #include "netlink-internal.h"
da2bf9
 #include "netlink-util.h"
da2bf9
+#include "strv.h"
da2bf9
 
da2bf9
 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
da2bf9
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
da2bf9
@@ -80,6 +81,45 @@ int rtnl_set_link_properties(sd_netlink **rtnl, int ifindex, const char *alias,
da2bf9
         return 0;
da2bf9
 }
da2bf9
 
da2bf9
+int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names) {
da2bf9
+        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
da2bf9
+        int r;
da2bf9
+
da2bf9
+        assert(rtnl);
da2bf9
+        assert(ifindex > 0);
da2bf9
+
da2bf9
+        if (strv_isempty(alternative_names))
da2bf9
+                return 0;
da2bf9
+
da2bf9
+        if (!*rtnl) {
da2bf9
+                r = sd_netlink_open(rtnl);
da2bf9
+                if (r < 0)
da2bf9
+                        return r;
da2bf9
+        }
da2bf9
+
da2bf9
+        r = sd_rtnl_message_new_link(*rtnl, &message, RTM_NEWLINKPROP, ifindex);
da2bf9
+        if (r < 0)
da2bf9
+                return r;
da2bf9
+
da2bf9
+        r = sd_netlink_message_open_container(message, IFLA_PROP_LIST);
da2bf9
+        if (r < 0)
da2bf9
+                return r;
da2bf9
+
da2bf9
+        r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, alternative_names);
da2bf9
+        if (r < 0)
da2bf9
+                return r;
da2bf9
+
da2bf9
+        r = sd_netlink_message_close_container(message);
da2bf9
+        if (r < 0)
da2bf9
+                return r;
da2bf9
+
da2bf9
+        r = sd_netlink_call(*rtnl, message, 0, NULL);
da2bf9
+        if (r < 0)
da2bf9
+                return r;
da2bf9
+
da2bf9
+        return 0;
da2bf9
+}
da2bf9
+
da2bf9
 int rtnl_message_new_synthetic_error(sd_netlink *rtnl, int error, uint32_t serial, sd_netlink_message **ret) {
da2bf9
         struct nlmsgerr *err;
da2bf9
         int r;
da2bf9
diff --git a/src/libsystemd/sd-netlink/netlink-util.h b/src/libsystemd/sd-netlink/netlink-util.h
da2bf9
index 882a616310..92de19c092 100644
da2bf9
--- a/src/libsystemd/sd-netlink/netlink-util.h
da2bf9
+++ b/src/libsystemd/sd-netlink/netlink-util.h
da2bf9
@@ -38,6 +38,7 @@ static inline bool rtnl_message_type_is_routing_policy_rule(uint16_t type) {
da2bf9
 
da2bf9
 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name);
da2bf9
 int rtnl_set_link_properties(sd_netlink **rtnl, int ifindex, const char *alias, const struct ether_addr *mac, uint32_t mtu);
da2bf9
+int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
da2bf9
 
da2bf9
 int rtnl_log_parse_error(int r);
da2bf9
 int rtnl_log_create_error(int r);
da2bf9
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
da2bf9
index 246b7431e4..1f40f00c72 100644
da2bf9
--- a/src/shared/conf-parser.c
da2bf9
+++ b/src/shared/conf-parser.c
da2bf9
@@ -970,6 +970,66 @@ int config_parse_ifname(
da2bf9
         return 0;
da2bf9
 }
da2bf9
 
da2bf9
+int config_parse_ifnames(
da2bf9
+                const char *unit,
da2bf9
+                const char *filename,
da2bf9
+                unsigned line,
da2bf9
+                const char *section,
da2bf9
+                unsigned section_line,
da2bf9
+                const char *lvalue,
da2bf9
+                int ltype,
da2bf9
+                const char *rvalue,
da2bf9
+                void *data,
da2bf9
+                void *userdata) {
da2bf9
+
da2bf9
+        _cleanup_strv_free_ char **names = NULL;
da2bf9
+        char ***s = data;
da2bf9
+        const char *p;
da2bf9
+        int r;
da2bf9
+
da2bf9
+        assert(filename);
da2bf9
+        assert(lvalue);
da2bf9
+        assert(rvalue);
da2bf9
+        assert(data);
da2bf9
+
da2bf9
+        if (isempty(rvalue)) {
da2bf9
+                *s = strv_free(*s);
da2bf9
+                return 0;
da2bf9
+        }
da2bf9
+
da2bf9
+        p = rvalue;
da2bf9
+        for (;;) {
da2bf9
+                _cleanup_free_ char *word = NULL;
da2bf9
+
da2bf9
+                r = extract_first_word(&p, &word, NULL, 0);
da2bf9
+                if (r < 0) {
da2bf9
+                        log_syntax(unit, LOG_ERR, filename, line, r,
da2bf9
+                                   "Failed to extract interface name, ignoring assignment: %s",
da2bf9
+                                   rvalue);
da2bf9
+                        return 0;
da2bf9
+                }
da2bf9
+                if (r == 0)
da2bf9
+                        break;
da2bf9
+
da2bf9
+                if (!ifname_valid_full(word, ltype)) {
da2bf9
+                        log_syntax(unit, LOG_ERR, filename, line, 0,
da2bf9
+                                   "Interface name is not valid or too long, ignoring assignment: %s",
da2bf9
+                                   word);
da2bf9
+                        continue;
da2bf9
+                }
da2bf9
+
da2bf9
+                r = strv_consume(&names, TAKE_PTR(word));
da2bf9
+                if (r < 0)
da2bf9
+                        return log_oom();
da2bf9
+        }
da2bf9
+
da2bf9
+        r = strv_extend_strv(s, names, true);
da2bf9
+        if (r < 0)
da2bf9
+                return log_oom();
da2bf9
+
da2bf9
+        return 0;
da2bf9
+}
da2bf9
+
da2bf9
 int config_parse_ip_port(
da2bf9
                 const char *unit,
da2bf9
                 const char *filename,
da2bf9
diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
da2bf9
index a0a5c89c27..375b2e5a74 100644
da2bf9
--- a/src/shared/conf-parser.h
da2bf9
+++ b/src/shared/conf-parser.h
da2bf9
@@ -137,6 +137,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_signal);
da2bf9
 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
da2bf9
 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
da2bf9
 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
da2bf9
+CONFIG_PARSER_PROTOTYPE(config_parse_ifnames);
da2bf9
 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
da2bf9
 CONFIG_PARSER_PROTOTYPE(config_parse_join_controllers);
da2bf9
 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
da2bf9
diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf
da2bf9
index b37836d852..913c754145 100644
da2bf9
--- a/src/udev/net/link-config-gperf.gperf
da2bf9
+++ b/src/udev/net/link-config-gperf.gperf
da2bf9
@@ -34,6 +34,7 @@ Link.MACAddressPolicy,           config_parse_mac_policy,    0,
da2bf9
 Link.MACAddress,                 config_parse_hwaddr,        0,                             offsetof(link_config, mac)
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.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 5113586457..d07a1a1874 100644
da2bf9
--- a/src/udev/net/link-config.c
da2bf9
+++ b/src/udev/net/link-config.c
da2bf9
@@ -67,6 +67,7 @@ static void link_config_free(link_config *link) {
da2bf9
         free(link->mac);
da2bf9
         free(link->name_policy);
da2bf9
         free(link->name);
da2bf9
+        strv_free(link->alternative_names);
da2bf9
         free(link->alias);
da2bf9
 
da2bf9
         free(link);
da2bf9
@@ -468,6 +469,10 @@ 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 (r < 0)
da2bf9
+                return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name);
da2bf9
+
da2bf9
         *name = new_name;
da2bf9
 
da2bf9
         return 0;
da2bf9
diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h
da2bf9
index 4798bb101c..93d5fdce59 100644
da2bf9
--- a/src/udev/net/link-config.h
da2bf9
+++ b/src/udev/net/link-config.h
da2bf9
@@ -50,6 +50,7 @@ struct link_config {
da2bf9
         MACPolicy mac_policy;
da2bf9
         NamePolicy *name_policy;
da2bf9
         char *name;
da2bf9
+        char **alternative_names;
da2bf9
         char *alias;
da2bf9
         uint32_t mtu;
da2bf9
         size_t speed;