Skip to content

Commit

Permalink
net: plumb extack in __dev_change_net_namespace()
Browse files Browse the repository at this point in the history
It could be hard to understand why the netlink command fails. For example,
if dev->netns_immutable is set, the error is "Invalid argument".

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Nicolas Dichtel authored and Paolo Abeni committed Mar 4, 2025
1 parent 4754aff commit 12b6f70
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
5 changes: 3 additions & 2 deletions include/linux/netdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -4191,12 +4191,13 @@ int dev_change_flags(struct net_device *dev, unsigned int flags,
int dev_set_alias(struct net_device *, const char *, size_t);
int dev_get_alias(const struct net_device *, char *, size_t);
int __dev_change_net_namespace(struct net_device *dev, struct net *net,
const char *pat, int new_ifindex);
const char *pat, int new_ifindex,
struct netlink_ext_ack *extack);
static inline
int dev_change_net_namespace(struct net_device *dev, struct net *net,
const char *pat)
{
return __dev_change_net_namespace(dev, net, pat, 0);
return __dev_change_net_namespace(dev, net, pat, 0, NULL);
}
int __dev_set_mtu(struct net_device *, int);
int dev_set_mtu(struct net_device *, int);
Expand Down
43 changes: 34 additions & 9 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -12131,6 +12131,7 @@ EXPORT_SYMBOL(unregister_netdev);
* is already taken in the destination network namespace.
* @new_ifindex: If not zero, specifies device index in the target
* namespace.
* @extack: netlink extended ack
*
* This function shuts down a device interface and moves it
* to a new network namespace. On success 0 is returned, on
Expand All @@ -12140,7 +12141,8 @@ EXPORT_SYMBOL(unregister_netdev);
*/

int __dev_change_net_namespace(struct net_device *dev, struct net *net,
const char *pat, int new_ifindex)
const char *pat, int new_ifindex,
struct netlink_ext_ack *extack)
{
struct netdev_name_node *name_node;
struct net *net_old = dev_net(dev);
Expand All @@ -12151,12 +12153,16 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,

/* Don't allow namespace local devices to be moved. */
err = -EINVAL;
if (dev->netns_immutable)
if (dev->netns_immutable) {
NL_SET_ERR_MSG(extack, "The interface netns is immutable");
goto out;
}

/* Ensure the device has been registered */
if (dev->reg_state != NETREG_REGISTERED)
if (dev->reg_state != NETREG_REGISTERED) {
NL_SET_ERR_MSG(extack, "The interface isn't registered");
goto out;
}

/* Get out if there is nothing todo */
err = 0;
Expand All @@ -12169,30 +12175,49 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,
err = -EEXIST;
if (netdev_name_in_use(net, dev->name)) {
/* We get here if we can't use the current device name */
if (!pat)
if (!pat) {
NL_SET_ERR_MSG(extack,
"An interface with the same name exists in the target netns");
goto out;
}
err = dev_prep_valid_name(net, dev, pat, new_name, EEXIST);
if (err < 0)
if (err < 0) {
NL_SET_ERR_MSG_FMT(extack,
"Unable to use '%s' for the new interface name in the target netns",
pat);
goto out;
}
}
/* Check that none of the altnames conflicts. */
err = -EEXIST;
netdev_for_each_altname(dev, name_node)
if (netdev_name_in_use(net, name_node->name))
netdev_for_each_altname(dev, name_node) {
if (netdev_name_in_use(net, name_node->name)) {
NL_SET_ERR_MSG_FMT(extack,
"An interface with the altname %s exists in the target netns",
name_node->name);
goto out;
}
}

/* Check that new_ifindex isn't used yet. */
if (new_ifindex) {
err = dev_index_reserve(net, new_ifindex);
if (err < 0)
if (err < 0) {
NL_SET_ERR_MSG_FMT(extack,
"The ifindex %d is not available in the target netns",
new_ifindex);
goto out;
}
} else {
/* If there is an ifindex conflict assign a new one */
err = dev_index_reserve(net, dev->ifindex);
if (err == -EBUSY)
err = dev_index_reserve(net, 0);
if (err < 0)
if (err < 0) {
NL_SET_ERR_MSG(extack,
"Unable to allocate a new ifindex in the target netns");
goto out;
}
new_ifindex = err;
}

Expand Down
2 changes: 1 addition & 1 deletion net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -3028,7 +3028,7 @@ static int do_setlink(const struct sk_buff *skb, struct net_device *dev,

new_ifindex = nla_get_s32_default(tb[IFLA_NEW_IFINDEX], 0);

err = __dev_change_net_namespace(dev, tgt_net, pat, new_ifindex);
err = __dev_change_net_namespace(dev, tgt_net, pat, new_ifindex, extack);
if (err)
goto errout;

Expand Down

0 comments on commit 12b6f70

Please sign in to comment.