]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn-network: also check alternative names
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Jan 2024 01:07:19 +0000 (10:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 19 Jan 2024 10:05:27 +0000 (19:05 +0900)
If the requested new name for a network interface is already assigned as a
alternative name, then it is not necessary to and cannot rename the
interface.

src/nspawn/nspawn-network.c

index 27ae07eba49a3dc6476265f5cf743bb8c48f7a9c..a05a2bc9e0fcc431630ddc068b80b7082d725751 100644 (file)
@@ -578,6 +578,37 @@ static int netns_fork_and_wait(int netns_fd, int *ret_original_netns_fd) {
         return 1;
 }
 
+static int needs_rename(sd_netlink **rtnl, sd_device *dev, const char *name) {
+        int r;
+
+        assert(rtnl);
+        assert(dev);
+        assert(name);
+
+        const char *ifname;
+        r = sd_device_get_sysname(dev, &ifname);
+        if (r < 0)
+                return r;
+
+        if (streq(name, ifname))
+                return false;
+
+        int ifindex;
+        r = sd_device_get_ifindex(dev, &ifindex);
+        if (r < 0)
+                return r;
+
+        _cleanup_strv_free_ char **altnames = NULL;
+        r = rtnl_get_link_alternative_names(rtnl, ifindex, &altnames);
+        if (r == -EOPNOTSUPP)
+                return true; /* alternative interface name is not supported, hence the name is not
+                              * assigned to the interface. */
+        if (r < 0)
+                return r;
+
+        return !strv_contains(altnames, name);
+}
+
 static int move_network_interface_one(sd_netlink **rtnl, int netns_fd, sd_device *dev, const char *name) {
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
         int r;
@@ -606,12 +637,10 @@ static int move_network_interface_one(sd_netlink **rtnl, int netns_fd, sd_device
         if (r < 0)
                 return log_device_error_errno(dev, r, "Failed to append namespace fd to netlink message: %m");
 
-        const char *sysname;
-        r = sd_device_get_sysname(dev, &sysname);
+        r = needs_rename(rtnl, dev, name);
         if (r < 0)
-                return log_device_error_errno(dev, r, "Failed to get sysname: %m");
-
-        if (!streq(name, sysname)) {
+                return log_device_error_errno(dev, r, "Failed to determine if the interface should be renamed to '%s': %m", name);
+        if (r > 0) {
                 r = sd_netlink_message_append_string(m, IFLA_IFNAME, name);
                 if (r < 0)
                         return log_device_error_errno(dev, r, "Failed to add netlink interface name: %m");