Skip to content

Commit cc6090e

Browse files
jpirkodavem330
authored andcommitted
net: rtnetlink: introduce helper to get net_device instance by ifname
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>
1 parent 7af12cb commit cc6090e

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

net/core/rtnetlink.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,23 @@ static int do_setlink(const struct sk_buff *skb,
27782778
return err;
27792779
}
27802780

2781+
static struct net_device *rtnl_dev_get(struct net *net,
2782+
struct nlattr *ifname_attr,
2783+
char *ifname)
2784+
{
2785+
char buffer[IFNAMSIZ];
2786+
2787+
if (!ifname) {
2788+
ifname = buffer;
2789+
if (ifname_attr)
2790+
nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
2791+
else
2792+
return NULL;
2793+
}
2794+
2795+
return __dev_get_by_name(net, ifname);
2796+
}
2797+
27812798
static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
27822799
struct netlink_ext_ack *extack)
27832800
{
@@ -2807,7 +2824,7 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
28072824
if (ifm->ifi_index > 0)
28082825
dev = __dev_get_by_index(net, ifm->ifi_index);
28092826
else if (tb[IFLA_IFNAME])
2810-
dev = __dev_get_by_name(net, ifname);
2827+
dev = rtnl_dev_get(net, NULL, ifname);
28112828
else
28122829
goto errout;
28132830

@@ -2880,7 +2897,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
28802897
struct net *tgt_net = net;
28812898
struct net_device *dev = NULL;
28822899
struct ifinfomsg *ifm;
2883-
char ifname[IFNAMSIZ];
28842900
struct nlattr *tb[IFLA_MAX+1];
28852901
int err;
28862902
int netnsid = -1;
@@ -2894,9 +2910,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
28942910
if (err < 0)
28952911
return err;
28962912

2897-
if (tb[IFLA_IFNAME])
2898-
nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2899-
29002913
if (tb[IFLA_TARGET_NETNSID]) {
29012914
netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
29022915
tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
@@ -2909,7 +2922,7 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
29092922
if (ifm->ifi_index > 0)
29102923
dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
29112924
else if (tb[IFLA_IFNAME])
2912-
dev = __dev_get_by_name(tgt_net, ifname);
2925+
dev = rtnl_dev_get(net, tb[IFLA_IFNAME], NULL);
29132926
else if (tb[IFLA_GROUP])
29142927
err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
29152928
else
@@ -3081,7 +3094,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
30813094
if (ifm->ifi_index > 0)
30823095
dev = __dev_get_by_index(net, ifm->ifi_index);
30833096
else if (tb[IFLA_IFNAME])
3084-
dev = __dev_get_by_name(net, ifname);
3097+
dev = rtnl_dev_get(net, NULL, ifname);
30853098
else
30863099
dev = NULL;
30873100

@@ -3363,7 +3376,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
33633376
struct net *net = sock_net(skb->sk);
33643377
struct net *tgt_net = net;
33653378
struct ifinfomsg *ifm;
3366-
char ifname[IFNAMSIZ];
33673379
struct nlattr *tb[IFLA_MAX+1];
33683380
struct net_device *dev = NULL;
33693381
struct sk_buff *nskb;
@@ -3386,9 +3398,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
33863398
return PTR_ERR(tgt_net);
33873399
}
33883400

3389-
if (tb[IFLA_IFNAME])
3390-
nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3391-
33923401
if (tb[IFLA_EXT_MASK])
33933402
ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
33943403

@@ -3397,7 +3406,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
33973406
if (ifm->ifi_index > 0)
33983407
dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
33993408
else if (tb[IFLA_IFNAME])
3400-
dev = __dev_get_by_name(tgt_net, ifname);
3409+
dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], NULL);
34013410
else
34023411
goto out;
34033412

@@ -3480,16 +3489,12 @@ static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
34803489
return err;
34813490

34823491
ifm = nlmsg_data(nlh);
3483-
if (ifm->ifi_index > 0) {
3492+
if (ifm->ifi_index > 0)
34843493
dev = __dev_get_by_index(net, ifm->ifi_index);
3485-
} else if (tb[IFLA_IFNAME]) {
3486-
char ifname[IFNAMSIZ];
3487-
3488-
nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3489-
dev = __dev_get_by_name(net, ifname);
3490-
} else {
3494+
else if (tb[IFLA_IFNAME])
3495+
dev = rtnl_dev_get(net, tb[IFLA_IFNAME], NULL);
3496+
else
34913497
return -EINVAL;
3492-
}
34933498

34943499
if (!dev)
34953500
return -ENODEV;

0 commit comments

Comments
 (0)