[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20191105081112.16656-4-jonas@norrbonn.se>
Date: Tue, 5 Nov 2019 09:11:10 +0100
From: Jonas Bonn <jonas@...rbonn.se>
To: nicolas.dichtel@...nd.com, netdev@...r.kernel.org,
linux-kernel@...r.kernel.org
Cc: davem@...emloft.net, Jonas Bonn <jonas@...rbonn.se>
Subject: [PATCH 3/5] rtnetlink: allow RTM_NEWLINK to act upon interfaces in arbitrary namespaces
RTM_NEWLINK can be used mostly interchangeably with RTM_SETLINK for
modifying device configuration. As such, this method requires the same
logic as RTM_SETLINK for finding the device to act on.
With this patch, the IFLA_TARGET_NETNSID selects the namespace in which
to search for the interface to act upon. This allows, for example, to
set the namespace of an interface outside the current namespace by
selecting it with the (IFLA_TARGET_NETNSID,ifi->ifi_index) pair and
specifying the namespace with one of IFLA_NET_NS_[PID|FD].
Since rtnl_newlink branches off into do_setlink, we need to provide the
same backwards compatibility check as we do for RTM_SETLINK: if the
device is not found in the namespace given by IFLA_TARGET_NETNSID then
we search for it in the current namespace. If found there, it's
namespace will be changed, as before.
Signed-off-by: Jonas Bonn <jonas@...rbonn.se>
---
net/core/rtnetlink.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index a6ec1b4ff7cd..3aba9e9d2c32 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -3019,6 +3019,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
const struct rtnl_link_ops *m_ops = NULL;
struct net_device *master_dev = NULL;
struct net *net = sock_net(skb->sk);
+ struct net *tgt_net = NULL;
const struct rtnl_link_ops *ops;
struct nlattr *tb[IFLA_MAX + 1];
struct net *dest_net, *link_net;
@@ -3047,6 +3048,15 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
else
ifname[0] = '\0';
+ if (tb[IFLA_TARGET_NETNSID]) {
+ int32_t netnsid;
+ netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
+ tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
+ if (IS_ERR(tgt_net))
+ return PTR_ERR(tgt_net);
+ net = tgt_net;
+ }
+
ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0)
dev = __dev_get_by_index(net, ifm->ifi_index);
@@ -3057,6 +3067,23 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
dev = NULL;
}
+ /* A hack to preserve kernel<->userspace interface.
+ * It was previously allowed to pass the IFLA_TARGET_NETNSID
+ * attribute as a way to _set_ the network namespace. In this
+ * case, the device interface was assumed to be in the _current_
+ * namespace.
+ * If the device cannot be found in the target namespace then we
+ * assume that the request is to set the device in the current
+ * namespace and thus we attempt to find the device there.
+ */
+ if (!dev && tgt_net) {
+ net = sock_net(skb->sk);
+ if (ifm->ifi_index > 0)
+ dev = __dev_get_by_index(net, ifm->ifi_index);
+ else if (tb[IFLA_IFNAME])
+ dev = __dev_get_by_name(net, ifname);
+ }
+
if (dev) {
master_dev = netdev_master_upper_dev_get(dev);
if (master_dev)
@@ -3251,6 +3278,8 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
goto out_unregister;
}
out:
+ if (tgt_net)
+ put_net(tgt_net);
if (link_net)
put_net(link_net);
put_net(dest_net);
--
2.20.1
Powered by blists - more mailing lists