[PATCH 2/5] rtnetlink: skip namespace change if already effect

From: Jonas Bonn
Date: Tue Nov 05 2019 - 03:11:46 EST


RTM_SETLINK uses IFA_TARGET_NETNSID both as a selector for the device to
act upon and as a selection of the namespace to move a device in the
current namespace to. As such, one ends up in the code path for setting
the namespace every time one calls setlink on a device outside the
current namespace. This has the unfortunate side effect of setting the
'modified' flag on the device for every pass, resulting in Netlink
notifications even when nothing was changed.

This patch just makes the namespace switch dependent upon the namespace
the device currently resides in.

Signed-off-by: Jonas Bonn <jonas@xxxxxxxxxxx>
---
net/core/rtnetlink.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 269d1afefceb..a6ec1b4ff7cd 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2393,11 +2393,15 @@ static int do_setlink(const struct sk_buff *skb,
goto errout;
}

- err = dev_change_net_namespace(dev, net, ifname);
- put_net(net);
- if (err)
- goto errout;
- status |= DO_SETLINK_MODIFIED;
+ if (!net_eq(dev_net(dev), net)) {
+ err = dev_change_net_namespace(dev, net, ifname);
+ put_net(net);
+ if (err)
+ goto errout;
+ status |= DO_SETLINK_MODIFIED;
+ } else {
+ put_net(net);
+ }
}

if (tb[IFLA_MAP]) {
--
2.20.1