[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20190719110029.29466-7-jiri@resnulli.us>
Date: Fri, 19 Jul 2019 13:00:28 +0200
From: Jiri Pirko <jiri@...nulli.us>
To: netdev@...r.kernel.org
Cc: davem@...emloft.net, jakub.kicinski@...ronome.com,
sthemmin@...rosoft.com, dsahern@...il.com, dcbw@...hat.com,
mkubecek@...e.cz, andrew@...n.ch, parav@...lanox.com,
saeedm@...lanox.com, mlxsw@...lanox.com
Subject: [patch net-next rfc 6/7] net: rtnetlink: introduce helper to get net_device instance by ifname
From: Jiri Pirko <jiri@...lanox.com>
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@...lanox.com>
---
net/core/rtnetlink.c | 57 ++++++++++++++++++++++----------------------
1 file changed, 29 insertions(+), 28 deletions(-)
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 8994dc858ae0..1fa30d514e3f 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2765,6 +2765,23 @@ static int do_setlink(const struct sk_buff *skb,
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)
{
@@ -2794,7 +2811,7 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
@@ -2867,7 +2884,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
@@ -2881,9 +2897,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
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);
@@ -2896,7 +2909,7 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
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
@@ -3068,7 +3081,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
@@ -3350,7 +3363,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
@@ -3373,9 +3385,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
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]);
@@ -3384,7 +3393,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
@@ -3433,16 +3442,12 @@ static int rtnl_newaltifname(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
@@ -3484,16 +3489,12 @@ static int rtnl_delaltifname(struct sk_buff *skb, struct nlmsghdr *nlh,
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;
--
2.21.0
Powered by blists - more mailing lists