From: Jiri Pirko <jiri@mellanox.com>
Date: Mon, 30 Sep 2019 11:48:19 +0200
Subject: net: rtnetlink: introduce helper to get net_device instance by ifname
Patch-mainline: v5.5-rc1
Git-commit: cc6090e985d7d62bf2f1230c507d5fbe9899c9ec
References: bsc#1154353
Introduce helper function rtnl_get_dev() that gets net_device structure
instance pointer according to passed ifname or ifname attribute.
Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Acked-by: Thomas Bogendoerfer <tbogendoerfer@suse.de>
---
net/core/rtnetlink.c | 45 +++++++++++++++++++++++++--------------------
1 file changed, 25 insertions(+), 20 deletions(-)
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2800,6 +2800,23 @@ errout:
return err;
}
+static struct net_device *rtnl_dev_get(struct net *net,
+ struct nlattr *ifname_attr,
+ char *ifname)
+{
+ char buffer[IFNAMSIZ];
+
+ if (!ifname) {
+ ifname = buffer;
+ if (ifname_attr)
+ nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
+ else
+ return NULL;
+ }
+
+ return __dev_get_by_name(net, ifname);
+}
+
static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
@@ -2829,7 +2846,7 @@ static int rtnl_setlink(struct sk_buff *
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);
+ dev = rtnl_dev_get(net, NULL, ifname);
else
goto errout;
@@ -2902,7 +2919,6 @@ static int rtnl_dellink(struct sk_buff *
struct net *tgt_net = net;
struct net_device *dev = NULL;
struct ifinfomsg *ifm;
- char ifname[IFNAMSIZ];
struct nlattr *tb[IFLA_MAX+1];
int err;
int netnsid = -1;
@@ -2916,9 +2932,6 @@ static int rtnl_dellink(struct sk_buff *
if (err < 0)
return err;
- if (tb[IFLA_IFNAME])
- nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
-
if (tb[IFLA_TARGET_NETNSID]) {
netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
@@ -2931,7 +2944,7 @@ static int rtnl_dellink(struct sk_buff *
if (ifm->ifi_index > 0)
dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
else if (tb[IFLA_IFNAME])
- dev = __dev_get_by_name(tgt_net, ifname);
+ dev = rtnl_dev_get(net, tb[IFLA_IFNAME], NULL);
else if (tb[IFLA_GROUP])
err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
else
@@ -3103,7 +3116,7 @@ replay:
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);
+ dev = rtnl_dev_get(net, NULL, ifname);
else
dev = NULL;
@@ -3385,7 +3398,6 @@ static int rtnl_getlink(struct sk_buff *
struct net *net = sock_net(skb->sk);
struct net *tgt_net = net;
struct ifinfomsg *ifm;
- char ifname[IFNAMSIZ];
struct nlattr *tb[IFLA_MAX+1];
struct net_device *dev = NULL;
struct sk_buff *nskb;
@@ -3408,9 +3420,6 @@ static int rtnl_getlink(struct sk_buff *
return PTR_ERR(tgt_net);
}
- if (tb[IFLA_IFNAME])
- nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
-
if (tb[IFLA_EXT_MASK])
ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
@@ -3419,7 +3428,7 @@ static int rtnl_getlink(struct sk_buff *
if (ifm->ifi_index > 0)
dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
else if (tb[IFLA_IFNAME])
- dev = __dev_get_by_name(tgt_net, ifname);
+ dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], NULL);
else
goto out;
@@ -3502,16 +3511,12 @@ static int rtnl_linkprop(int cmd, struct
return err;
ifm = nlmsg_data(nlh);
- if (ifm->ifi_index > 0) {
+ if (ifm->ifi_index > 0)
dev = __dev_get_by_index(net, ifm->ifi_index);
- } else if (tb[IFLA_IFNAME]) {
- char ifname[IFNAMSIZ];
-
- nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
- dev = __dev_get_by_name(net, ifname);
- } else {
+ else if (tb[IFLA_IFNAME])
+ dev = rtnl_dev_get(net, tb[IFLA_IFNAME], NULL);
+ else
return -EINVAL;
- }
if (!dev)
return -ENODEV;