Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtnetlink: Lookup device in target netns when creating link

When creating link, lookup for existing device in target net namespace
instead of current one.
For example, two links created by:

# ip link add dummy1 type dummy
# ip link add netns ns1 dummy1 type dummy

should have no conflict since they are in different namespaces.

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Link: https://patch.msgid.link/20250219125039.18024-2-shaw.leon@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Xiao Liang and committed by
Jakub Kicinski
ec061546 4fe67dd2

+8 -2
+8 -2
net/core/rtnetlink.c
··· 3867 3867 { 3868 3868 struct nlattr ** const tb = tbs->tb; 3869 3869 struct net *net = sock_net(skb->sk); 3870 + struct net *device_net; 3870 3871 struct net_device *dev; 3871 3872 struct ifinfomsg *ifm; 3872 3873 bool link_specified; 3873 3874 3875 + /* When creating, lookup for existing device in target net namespace */ 3876 + device_net = (nlh->nlmsg_flags & NLM_F_CREATE) && 3877 + (nlh->nlmsg_flags & NLM_F_EXCL) ? 3878 + tgt_net : net; 3879 + 3874 3880 ifm = nlmsg_data(nlh); 3875 3881 if (ifm->ifi_index > 0) { 3876 3882 link_specified = true; 3877 - dev = __dev_get_by_index(net, ifm->ifi_index); 3883 + dev = __dev_get_by_index(device_net, ifm->ifi_index); 3878 3884 } else if (ifm->ifi_index < 0) { 3879 3885 NL_SET_ERR_MSG(extack, "ifindex can't be negative"); 3880 3886 return -EINVAL; 3881 3887 } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { 3882 3888 link_specified = true; 3883 - dev = rtnl_dev_get(net, tb); 3889 + dev = rtnl_dev_get(device_net, tb); 3884 3890 } else { 3885 3891 link_specified = false; 3886 3892 dev = NULL;