From: Jonas Bonn <jonas@norrbonn.se>
To: nicolas.dichtel@6wind.com, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: davem@davemloft.net, Jonas Bonn <jonas@norrbonn.se>
Subject: [PATCH v3 4/6] net: ipv4: allow setting address on interface outside current namespace
Date: Thu, 7 Nov 2019 14:27:53 +0100 [thread overview]
Message-ID: <20191107132755.8517-5-jonas@norrbonn.se> (raw)
In-Reply-To: <20191107132755.8517-1-jonas@norrbonn.se>
This patch allows an interface outside of the current namespace to be
selected when setting a new IPv4 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@norrbonn.se>
---
net/ipv4/devinet.c | 61 ++++++++++++++++++++++++++++++++++------------
1 file changed, 45 insertions(+), 16 deletions(-)
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index a4b5bd4d2c89..eecd1b0bb7d2 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -813,22 +813,17 @@ static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
ifa->ifa_cstamp = ifa->ifa_tstamp;
}
-static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
+static struct in_ifaddr *rtm_to_ifaddr(struct nlattr *tb[],
+ struct net *net, struct nlmsghdr *nlh,
__u32 *pvalid_lft, __u32 *pprefered_lft,
struct netlink_ext_ack *extack)
{
- struct nlattr *tb[IFA_MAX+1];
struct in_ifaddr *ifa;
struct ifaddrmsg *ifm;
struct net_device *dev;
struct in_device *in_dev;
int err;
- err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
- ifa_ipv4_policy, extack);
- if (err < 0)
- goto errout;
-
ifm = nlmsg_data(nlh);
err = -EINVAL;
if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
@@ -922,16 +917,38 @@ static int inet_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 = NULL;
struct in_ifaddr *ifa;
struct in_ifaddr *ifa_existing;
__u32 valid_lft = INFINITY_LIFE_TIME;
__u32 prefered_lft = INFINITY_LIFE_TIME;
+ struct nlattr *tb[IFA_MAX + 1];
+ int err;
ASSERT_RTNL();
- ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
- if (IS_ERR(ifa))
- return PTR_ERR(ifa);
+ err = nlmsg_parse_deprecated(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX,
+ ifa_ipv4_policy, extack);
+ if (err < 0)
+ return err;
+
+ 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,
+ "ipv4: Invalid target network namespace id");
+ return PTR_ERR(tgt_net);
+ }
+ net = tgt_net;
+ }
+
+ ifa = rtm_to_ifaddr(tb, net, nlh, &valid_lft, &prefered_lft, extack);
+ if (IS_ERR(ifa)) {
+ err = PTR_ERR(ifa);
+ goto out;
+ }
ifa_existing = find_matching_ifa(ifa);
if (!ifa_existing) {
@@ -945,19 +962,24 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
if (ret < 0) {
inet_free_ifa(ifa);
- return ret;
+ err = ret;
+ goto out;
}
}
- return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
- extack);
+ err = __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
+ extack);
+ if (err < 0)
+ goto out;
} else {
u32 new_metric = ifa->ifa_rt_priority;
inet_free_ifa(ifa);
if (nlh->nlmsg_flags & NLM_F_EXCL ||
- !(nlh->nlmsg_flags & NLM_F_REPLACE))
- return -EEXIST;
+ !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
+ err = -EEXIST;
+ goto out;
+ }
ifa = ifa_existing;
if (ifa->ifa_rt_priority != new_metric) {
@@ -971,7 +993,14 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
&check_lifetime_work, 0);
rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
}
- return 0;
+
+ err = 0;
+
+out:
+ if (tgt_net)
+ put_net(tgt_net);
+
+ return err;
}
/*
--
2.20.1
next prev parent reply other threads:[~2019-11-07 13:28 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-07 13:27 [PATCH v3 0/6] Add namespace awareness to Netlink methods Jonas Bonn
2019-11-07 13:27 ` [PATCH v3 1/6] rtnetlink: allow RTM_SETLINK to reference other namespaces Jonas Bonn
2019-11-07 20:36 ` Mahesh Bandewar (महेश बंडेवार)
2019-11-08 8:20 ` Jonas Bonn
2019-11-08 18:55 ` Mahesh Bandewar (महेश बंडेवार)
2019-11-09 14:17 ` Jonas Bonn
2019-11-12 1:29 ` Mahesh Bandewar (महेश बंडेवार)
2019-11-07 13:27 ` [PATCH v3 2/6] rtnetlink: skip namespace change if already effect Jonas Bonn
2019-11-07 13:27 ` [PATCH v3 3/6] rtnetlink: allow RTM_NEWLINK to act upon interfaces in arbitrary namespaces Jonas Bonn
2019-11-07 13:27 ` Jonas Bonn [this message]
2019-11-07 13:27 ` [PATCH v3 5/6] net: namespace: allow setting NSIDs outside current namespace Jonas Bonn
2019-11-07 13:27 ` [PATCH v3 6/6] net: ipv6: allow setting address on interface " Jonas Bonn
2019-11-07 13:56 ` [PATCH v3 1/1] " Jonas Bonn
2019-11-07 16:58 ` Nicolas Dichtel
2019-11-07 18:37 ` [PATCH v3 0/6] Add namespace awareness to Netlink methods David Miller
2019-11-07 20:40 ` Mahesh Bandewar (महेश बंडेवार)
2019-11-07 21:11 ` David Ahern
2019-11-08 15:36 ` Jonas Bonn
2019-11-08 18:59 ` Mahesh Bandewar (महेश बंडेवार)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191107132755.8517-5-jonas@norrbonn.se \
--to=jonas@norrbonn.se \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nicolas.dichtel@6wind.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).