[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <174265429530.356712.918910072525880381.stgit@pro.pro>
Date: Sat, 22 Mar 2025 17:38:15 +0300
From: Kirill Tkhai <tkhai@...ru>
To: netdev@...r.kernel.org,
linux-kernel@...r.kernel.org
Cc: tkhai@...ru
Subject: [PATCH NET-PREV 03/51] net: do_setlink() refactoring: move target_net acquiring to callers
The patch is preparation in rtnetlink code for using nd_lock.
This is a step to move dereference of tb[IFLA_MASTER] up
to where main dev is dereferenced by ifi_index.
Signed-off-by: Kirill Tkhai <tkhai@...ru>
---
net/core/rtnetlink.c | 78 +++++++++++++++++++++++++++++++-------------------
1 file changed, 49 insertions(+), 29 deletions(-)
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 34e35b81cfa6..a5af69af235f 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2774,7 +2774,7 @@ static int do_set_proto_down(struct net_device *dev,
#define DO_SETLINK_MODIFIED 0x01
/* notify flag means notify + modified. */
#define DO_SETLINK_NOTIFY 0x03
-static int do_setlink(const struct sk_buff *skb,
+static int do_setlink(struct net *net, const struct sk_buff *skb,
struct net_device *dev, struct ifinfomsg *ifm,
struct netlink_ext_ack *extack,
struct nlattr **tb, int status)
@@ -2788,25 +2788,16 @@ static int do_setlink(const struct sk_buff *skb,
else
ifname[0] = '\0';
- if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
+ if (net) { /* target net */
const char *pat = ifname[0] ? ifname : NULL;
- struct net *net;
int new_ifindex;
- net = rtnl_link_get_net_capable(skb, dev_net(dev),
- tb, CAP_NET_ADMIN);
- if (IS_ERR(net)) {
- err = PTR_ERR(net);
- goto errout;
- }
-
if (tb[IFLA_NEW_IFINDEX])
new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
else
new_ifindex = 0;
err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
- put_net(net);
if (err)
goto errout;
status |= DO_SETLINK_MODIFIED;
@@ -3171,6 +3162,7 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct net *net = sock_net(skb->sk);
struct ifinfomsg *ifm;
struct net_device *dev;
+ struct net *target_net = NULL;
int err;
struct nlattr *tb[IFLA_MAX+1];
@@ -3183,6 +3175,13 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
if (err < 0)
goto errout;
+ target_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
+ if (IS_ERR(target_net)) {
+ err = PTR_ERR(target_net);
+ target_net = NULL;
+ goto errout;
+ }
+
err = -EINVAL;
ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0)
@@ -3201,8 +3200,10 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
if (err < 0)
goto errout;
- err = do_setlink(skb, dev, ifm, extack, tb, 0);
+ err = do_setlink(target_net, skb, dev, ifm, extack, tb, 0);
errout:
+ if (target_net)
+ put_net(target_net);
return err;
}
@@ -3440,38 +3441,51 @@ static int rtnl_group_changelink(const struct sk_buff *skb,
struct nlattr **tb)
{
struct net_device *dev, *aux;
+ struct net *target_net;
int err;
+ target_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
+ if (IS_ERR(target_net)) {
+ err = PTR_ERR(target_net);
+ target_net = NULL;
+ goto out;
+ }
+
for_each_netdev_safe(net, dev, aux) {
if (dev->group == group) {
err = validate_linkmsg(dev, tb, extack);
if (err < 0)
- return err;
- err = do_setlink(skb, dev, ifm, extack, tb, 0);
+ break;
+ err = do_setlink(target_net, skb, dev, ifm, extack, tb, 0);
if (err < 0)
- return err;
+ break;
}
}
-
- return 0;
+out:
+ if (target_net)
+ put_net(target_net);
+ return err;
}
static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
const struct rtnl_link_ops *ops,
const struct nlmsghdr *nlh,
struct nlattr **tb, struct nlattr **data,
- struct netlink_ext_ack *extack)
+ struct netlink_ext_ack *extack,
+ struct net *dest_net)
{
unsigned char name_assign_type = NET_NAME_USER;
struct net *net = sock_net(skb->sk);
u32 portid = NETLINK_CB(skb).portid;
- struct net *dest_net, *link_net;
+ struct net *link_net;
struct net_device *dev;
char ifname[IFNAMSIZ];
int err;
if (!ops->alloc && !ops->setup)
return -EOPNOTSUPP;
+ if (!dest_net)
+ dest_net = net;
if (tb[IFLA_IFNAME]) {
nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
@@ -3480,11 +3494,6 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
name_assign_type = NET_NAME_ENUM;
}
- dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
- if (IS_ERR(dest_net))
- return PTR_ERR(dest_net);
- dest_net = dest_net ? : get_net(net);
-
if (tb[IFLA_LINK_NETNSID]) {
int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
@@ -3535,7 +3544,6 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
out:
if (link_net)
put_net(link_net);
- put_net(dest_net);
return err;
out_unregister:
if (ops->newlink) {
@@ -3557,7 +3565,8 @@ struct rtnl_newlink_tbs {
static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct rtnl_newlink_tbs *tbs,
- struct netlink_ext_ack *extack)
+ struct netlink_ext_ack *extack,
+ struct net *target_net)
{
struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
struct nlattr ** const tb = tbs->tb;
@@ -3688,7 +3697,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
status |= DO_SETLINK_NOTIFY;
}
- return do_setlink(skb, dev, ifm, extack, tb, status);
+ return do_setlink(target_net, skb, dev, ifm, extack, tb, status);
}
if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
@@ -3722,12 +3731,14 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EOPNOTSUPP;
}
- return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack);
+ return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack, target_net);
}
static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
+ struct net *net = sock_net(skb->sk);
+ struct net *target_net = NULL;
struct rtnl_newlink_tbs *tbs;
struct nlattr **tb;
int ret;
@@ -3746,8 +3757,17 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
if (ret < 0)
goto out;
- ret = __rtnl_newlink(skb, nlh, tbs, extack);
+ target_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
+ if (IS_ERR(target_net)) {
+ ret = PTR_ERR(target_net);
+ target_net = NULL;
+ goto out;
+ }
+
+ ret = __rtnl_newlink(skb, nlh, tbs, extack, target_net);
out:
+ if (target_net)
+ put_net(target_net);
kfree(tbs);
return ret;
}
Powered by blists - more mailing lists