da2bf9
From 73ff88cdb6bd1991d75323c6c364bcc9bce7efda Mon Sep 17 00:00:00 2001
da2bf9
From: Yu Watanabe <watanabe.yu+github@gmail.com>
da2bf9
Date: Fri, 17 Jul 2020 21:31:24 +0900
da2bf9
Subject: [PATCH] netlink: do not fail when new interface name is already used
da2bf9
 as an alternative name
da2bf9
da2bf9
When renaming a network interface, the new name may be used as an
da2bf9
alternative name. In that case, let's swap the current name and the
da2bf9
alternative name. That is, first drop the new name from the list of
da2bf9
alternative names, then rename the interface, finally set the old name
da2bf9
as an alternative name.
da2bf9
da2bf9
(cherry picked from commit 434a34838034347f45fb9a47df55b1a36e5addfd)
da2bf9
da2bf9
Related: #2005008
da2bf9
---
da2bf9
 src/libsystemd/sd-netlink/netlink-util.c | 30 +++++++++++++++++++++---
da2bf9
 1 file changed, 27 insertions(+), 3 deletions(-)
da2bf9
da2bf9
diff --git a/src/libsystemd/sd-netlink/netlink-util.c b/src/libsystemd/sd-netlink/netlink-util.c
da2bf9
index 7f09261981..4e42ef9e26 100644
da2bf9
--- a/src/libsystemd/sd-netlink/netlink-util.c
da2bf9
+++ b/src/libsystemd/sd-netlink/netlink-util.c
da2bf9
@@ -1,23 +1,40 @@
da2bf9
 /* SPDX-License-Identifier: LGPL-2.1+ */
da2bf9
 
da2bf9
+#include <net/if.h>
da2bf9
+
da2bf9
 #include "sd-netlink.h"
da2bf9
 
da2bf9
 #include "netlink-internal.h"
da2bf9
 #include "netlink-util.h"
da2bf9
+#include "socket-util.h"
da2bf9
+#include "string-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
+        _cleanup_strv_free_ char **alternative_names = NULL;
da2bf9
+        char old_name[IF_NAMESIZE + 1] = {};
da2bf9
         int r;
da2bf9
 
da2bf9
         assert(rtnl);
da2bf9
         assert(ifindex > 0);
da2bf9
         assert(name);
da2bf9
 
da2bf9
-        if (!*rtnl) {
da2bf9
-                r = sd_netlink_open(rtnl);
da2bf9
+        if (!ifname_valid(name))
da2bf9
+                return -EINVAL;
da2bf9
+
da2bf9
+        r = rtnl_get_link_alternative_names(rtnl, ifindex, &alternative_names);
da2bf9
+        if (r < 0)
da2bf9
+                log_debug_errno(r, "Failed to get alternative names on network interface %i, ignoring: %m",
da2bf9
+                                ifindex);
da2bf9
+
da2bf9
+        if (strv_contains(alternative_names, name)) {
da2bf9
+                r = rtnl_delete_link_alternative_names(rtnl, ifindex, STRV_MAKE(name));
da2bf9
                 if (r < 0)
da2bf9
-                        return r;
da2bf9
+                        return log_debug_errno(r, "Failed to remove '%s' from alternative names on network interface %i: %m",
da2bf9
+                                               name, ifindex);
da2bf9
+
da2bf9
+                if_indextoname(ifindex, old_name);
da2bf9
         }
da2bf9
 
da2bf9
         r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
da2bf9
@@ -32,6 +49,13 @@ int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
da2bf9
         if (r < 0)
da2bf9
                 return r;
da2bf9
 
da2bf9
+        if (!isempty(old_name)) {
da2bf9
+                r = rtnl_set_link_alternative_names(rtnl, ifindex, STRV_MAKE(old_name));
da2bf9
+                if (r < 0)
da2bf9
+                        log_debug_errno(r, "Failed to set '%s' as an alternative name on network interface %i, ignoring: %m",
da2bf9
+                                        old_name, ifindex);
da2bf9
+        }
da2bf9
+
da2bf9
         return 0;
da2bf9
 }
da2bf9