[PATCH v3 6/6] net: ipv6: allow setting address on interface outside current namespace

From: Jonas Bonn
Date: Thu Nov 07 2019 - 08:28:13 EST


This patch allows an interface outside of the current namespace to be
selected when setting a new IPv6 address for a device. This uses the
IFA_TARGET_NETNSID attribute to select the namespace in which to search
for the interface to act upon.

Signed-off-by: Jonas Bonn <jonas@xxxxxxxxxxx>
---
net/ipv6/addrconf.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index 34ccef18b40e..06a49670fe62 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -4721,6 +4721,7 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
struct net *net = sock_net(skb->sk);
+ struct net *tgt_net;
struct ifaddrmsg *ifm;
struct nlattr *tb[IFA_MAX+1];
struct in6_addr *peer_pfx;
@@ -4758,6 +4759,18 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
cfg.preferred_lft = ci->ifa_prefered;
}

+ if (tb[IFA_TARGET_NETNSID]) {
+ s32 netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
+
+ tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
+ if (IS_ERR(tgt_net)) {
+ NL_SET_ERR_MSG(extack,
+ "ipv6: Invalid target network namespace id");
+ return PTR_ERR(tgt_net);
+ }
+ net = tgt_net;
+ }
+
dev = __dev_get_by_index(net, ifm->ifa_index);
if (!dev)
return -ENODEV;
--
2.20.1