blob: d640ecf71e7447226a3fd236f9f835a48ecc55bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010026#include <linux/rtnetlink.h>
27#include <linux/sched.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080028#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * Some useful ethtool_ops methods that're device independent.
32 * If we find that all drivers want to do the same thing here,
33 * we can turn these into dev_() function calls.
34 */
35
36u32 ethtool_op_get_link(struct net_device *dev)
37{
38 return netif_carrier_ok(dev) ? 1 : 0;
39}
chavey97f8aef2010-04-07 21:54:42 -070040EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Richard Cochran02eacbd2012-04-03 22:59:22 +000042int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
43{
44 info->so_timestamping =
45 SOF_TIMESTAMPING_TX_SOFTWARE |
46 SOF_TIMESTAMPING_RX_SOFTWARE |
47 SOF_TIMESTAMPING_SOFTWARE;
48 info->phc_index = -1;
49 return 0;
50}
51EXPORT_SYMBOL(ethtool_op_get_ts_info);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Handlers for each ethtool command */
54
Michał Mirosław475414f2011-11-15 15:29:55 +000055#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000056
Michał Mirosław9d921542011-11-15 15:29:55 +000057static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
58 [NETIF_F_SG_BIT] = "tx-scatter-gather",
59 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000060 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
61 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
62 [NETIF_F_HIGHDMA_BIT] = "highdma",
63 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090064 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000065
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090066 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040068 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
69 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
70 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000071 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
72 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
73 [NETIF_F_LLTX_BIT] = "tx-lockless",
74 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
75 [NETIF_F_GRO_BIT] = "rx-gro",
76 [NETIF_F_LRO_BIT] = "rx-lro",
77
78 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
79 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
80 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
81 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
82 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
83 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000084 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Eric Dumazetcb32f512013-10-19 11:42:57 -070085 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
Eric Dumazet61c1db72013-10-20 20:47:30 -070086 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000087 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000088
89 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080090 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000091 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
92 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
93 [NETIF_F_RXHASH_BIT] = "rx-hashing",
94 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
95 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
96 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000097 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000098 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -080099 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
Jiri Pirkod0290212014-04-02 23:09:31 +0200100 [NETIF_F_BUSY_POLL_BIT] = "busy-poll",
John Fastabend1c78c642016-02-16 21:17:37 -0800101 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000102};
103
Eyal Perry892311f2014-12-02 18:12:10 +0200104static const char
105rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
106 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
107 [ETH_RSS_HASH_XOR_BIT] = "xor",
108};
109
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300110static const char
111tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
112 [ETHTOOL_ID_UNSPEC] = "Unspec",
113 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
114 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
115};
116
Michał Mirosław5455c692011-02-15 16:59:17 +0000117static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
118{
119 struct ethtool_gfeatures cmd = {
120 .cmd = ETHTOOL_GFEATURES,
121 .size = ETHTOOL_DEV_FEATURE_WORDS,
122 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000123 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000124 u32 __user *sizeaddr;
125 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000126 int i;
127
128 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000129 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000130
131 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000132 features[i].available = (u32)(dev->hw_features >> (32 * i));
133 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
134 features[i].active = (u32)(dev->features >> (32 * i));
135 features[i].never_changed =
136 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000137 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000138
139 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
140 if (get_user(copy_size, sizeaddr))
141 return -EFAULT;
142
143 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
144 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
145
146 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
147 return -EFAULT;
148 useraddr += sizeof(cmd);
149 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
150 return -EFAULT;
151
152 return 0;
153}
154
155static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
156{
157 struct ethtool_sfeatures cmd;
158 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000159 netdev_features_t wanted = 0, valid = 0;
160 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000161
162 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
163 return -EFAULT;
164 useraddr += sizeof(cmd);
165
166 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
167 return -EINVAL;
168
169 if (copy_from_user(features, useraddr, sizeof(features)))
170 return -EFAULT;
171
Michał Mirosław475414f2011-11-15 15:29:55 +0000172 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000173 valid |= (netdev_features_t)features[i].valid << (32 * i);
174 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000175 }
176
177 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000178 return -EINVAL;
179
Michał Mirosław475414f2011-11-15 15:29:55 +0000180 if (valid & ~dev->hw_features) {
181 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000182 ret |= ETHTOOL_F_UNSUPPORTED;
183 }
184
Michał Mirosław475414f2011-11-15 15:29:55 +0000185 dev->wanted_features &= ~valid;
186 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700187 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000188
Michał Mirosław475414f2011-11-15 15:29:55 +0000189 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000190 ret |= ETHTOOL_F_WISH;
191
192 return ret;
193}
194
Andrew Lunnf3a40942015-12-30 16:28:25 +0100195static int phy_get_sset_count(struct phy_device *phydev)
196{
197 int ret;
198
199 if (phydev->drv->get_sset_count &&
200 phydev->drv->get_strings &&
201 phydev->drv->get_stats) {
202 mutex_lock(&phydev->lock);
203 ret = phydev->drv->get_sset_count(phydev);
204 mutex_unlock(&phydev->lock);
205
206 return ret;
207 }
208
209 return -EOPNOTSUPP;
210}
211
Michał Mirosław340ae162011-02-15 16:59:16 +0000212static int __ethtool_get_sset_count(struct net_device *dev, int sset)
213{
214 const struct ethtool_ops *ops = dev->ethtool_ops;
215
Michał Mirosław5455c692011-02-15 16:59:17 +0000216 if (sset == ETH_SS_FEATURES)
217 return ARRAY_SIZE(netdev_features_strings);
218
Eyal Perry892311f2014-12-02 18:12:10 +0200219 if (sset == ETH_SS_RSS_HASH_FUNCS)
220 return ARRAY_SIZE(rss_hash_func_strings);
221
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300222 if (sset == ETH_SS_TUNABLES)
223 return ARRAY_SIZE(tunable_strings);
224
Andrew Lunnf3a40942015-12-30 16:28:25 +0100225 if (sset == ETH_SS_PHY_STATS) {
226 if (dev->phydev)
227 return phy_get_sset_count(dev->phydev);
228 else
229 return -EOPNOTSUPP;
230 }
231
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000232 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000233 return ops->get_sset_count(dev, sset);
234 else
235 return -EOPNOTSUPP;
236}
237
238static void __ethtool_get_strings(struct net_device *dev,
239 u32 stringset, u8 *data)
240{
241 const struct ethtool_ops *ops = dev->ethtool_ops;
242
Michał Mirosław5455c692011-02-15 16:59:17 +0000243 if (stringset == ETH_SS_FEATURES)
244 memcpy(data, netdev_features_strings,
245 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200246 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
247 memcpy(data, rss_hash_func_strings,
248 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300249 else if (stringset == ETH_SS_TUNABLES)
250 memcpy(data, tunable_strings, sizeof(tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100251 else if (stringset == ETH_SS_PHY_STATS) {
252 struct phy_device *phydev = dev->phydev;
253
254 if (phydev) {
255 mutex_lock(&phydev->lock);
256 phydev->drv->get_strings(phydev, data);
257 mutex_unlock(&phydev->lock);
258 } else {
259 return;
260 }
261 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000262 /* ops->get_strings is valid because checked earlier */
263 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000264}
265
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000266static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000267{
268 /* feature masks of legacy discrete ethtool ops */
269
270 switch (eth_cmd) {
271 case ETHTOOL_GTXCSUM:
272 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800273 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000274 case ETHTOOL_GRXCSUM:
275 case ETHTOOL_SRXCSUM:
276 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000277 case ETHTOOL_GSG:
278 case ETHTOOL_SSG:
279 return NETIF_F_SG;
280 case ETHTOOL_GTSO:
281 case ETHTOOL_STSO:
282 return NETIF_F_ALL_TSO;
283 case ETHTOOL_GUFO:
284 case ETHTOOL_SUFO:
285 return NETIF_F_UFO;
286 case ETHTOOL_GGSO:
287 case ETHTOOL_SGSO:
288 return NETIF_F_GSO;
289 case ETHTOOL_GGRO:
290 case ETHTOOL_SGRO:
291 return NETIF_F_GRO;
292 default:
293 BUG();
294 }
295}
296
Michał Mirosław0a417702011-02-15 16:59:17 +0000297static int ethtool_get_one_feature(struct net_device *dev,
298 char __user *useraddr, u32 ethcmd)
299{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000300 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000301 struct ethtool_value edata = {
302 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000303 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000304 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000305
Michał Mirosław0a417702011-02-15 16:59:17 +0000306 if (copy_to_user(useraddr, &edata, sizeof(edata)))
307 return -EFAULT;
308 return 0;
309}
310
Michał Mirosław0a417702011-02-15 16:59:17 +0000311static int ethtool_set_one_feature(struct net_device *dev,
312 void __user *useraddr, u32 ethcmd)
313{
314 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000315 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000316
317 if (copy_from_user(&edata, useraddr, sizeof(edata)))
318 return -EFAULT;
319
Michał Mirosław86794882011-02-15 16:59:17 +0000320 mask = ethtool_get_feature_mask(ethcmd);
321 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000322 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000323 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000324
325 if (edata.data)
326 dev->wanted_features |= mask;
327 else
328 dev->wanted_features &= ~mask;
329
330 __netdev_update_features(dev);
331
332 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000333}
334
Michał Mirosław02b3a552011-11-15 15:29:55 +0000335#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
336 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000337#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
338 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
339 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000340
341static u32 __ethtool_get_flags(struct net_device *dev)
342{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000343 u32 flags = 0;
344
Dragos Foianu8a731252013-07-13 14:43:00 +0100345 if (dev->features & NETIF_F_LRO)
346 flags |= ETH_FLAG_LRO;
347 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
348 flags |= ETH_FLAG_RXVLAN;
349 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
350 flags |= ETH_FLAG_TXVLAN;
351 if (dev->features & NETIF_F_NTUPLE)
352 flags |= ETH_FLAG_NTUPLE;
353 if (dev->features & NETIF_F_RXHASH)
354 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000355
356 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000357}
358
359static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000360{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000361 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000362
Michał Mirosław02b3a552011-11-15 15:29:55 +0000363 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000364 return -EINVAL;
365
Dragos Foianu8a731252013-07-13 14:43:00 +0100366 if (data & ETH_FLAG_LRO)
367 features |= NETIF_F_LRO;
368 if (data & ETH_FLAG_RXVLAN)
369 features |= NETIF_F_HW_VLAN_CTAG_RX;
370 if (data & ETH_FLAG_TXVLAN)
371 features |= NETIF_F_HW_VLAN_CTAG_TX;
372 if (data & ETH_FLAG_NTUPLE)
373 features |= NETIF_F_NTUPLE;
374 if (data & ETH_FLAG_RXHASH)
375 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000376
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000377 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000378 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000379 if (changed & ~dev->hw_features)
380 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
381
382 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000383 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000384
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700385 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000386
387 return 0;
388}
389
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000390int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000392 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000394 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EOPNOTSUPP;
396
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000397 memset(cmd, 0, sizeof(struct ethtool_cmd));
398 cmd->cmd = ETHTOOL_GSET;
399 return dev->ethtool_ops->get_settings(dev, cmd);
400}
401EXPORT_SYMBOL(__ethtool_get_settings);
402
403static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
404{
405 int err;
406 struct ethtool_cmd cmd;
407
David S. Miller18ec8982015-06-01 14:43:50 -0700408 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (err < 0)
410 return err;
411
412 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
413 return -EFAULT;
414 return 0;
415}
416
417static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
418{
419 struct ethtool_cmd cmd;
420
421 if (!dev->ethtool_ops->set_settings)
422 return -EOPNOTSUPP;
423
424 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
425 return -EFAULT;
426
427 return dev->ethtool_ops->set_settings(dev, &cmd);
428}
429
chavey97f8aef2010-04-07 21:54:42 -0700430static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
431 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700434 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 memset(&info, 0, sizeof(info));
437 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000438 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700439 ops->get_drvinfo(dev, &info);
440 } else if (dev->dev.parent && dev->dev.parent->driver) {
441 strlcpy(info.bus_info, dev_name(dev->dev.parent),
442 sizeof(info.bus_info));
443 strlcpy(info.driver, dev->dev.parent->driver->name,
444 sizeof(info.driver));
445 } else {
446 return -EOPNOTSUPP;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Jeff Garzik723b2f52010-03-03 22:51:50 +0000449 /*
450 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000451 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000452 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000453 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700454 int rc;
455
456 rc = ops->get_sset_count(dev, ETH_SS_TEST);
457 if (rc >= 0)
458 info.testinfo_len = rc;
459 rc = ops->get_sset_count(dev, ETH_SS_STATS);
460 if (rc >= 0)
461 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700462 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
463 if (rc >= 0)
464 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700465 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000466 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000468 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 info.eedump_len = ops->get_eeprom_len(dev);
470
471 if (copy_to_user(useraddr, &info, sizeof(info)))
472 return -EFAULT;
473 return 0;
474}
475
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800476static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700477 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000478{
479 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000480 u64 sset_mask;
481 int i, idx = 0, n_bits = 0, ret, rc;
482 u32 *info_buf = NULL;
483
Jeff Garzik723b2f52010-03-03 22:51:50 +0000484 if (copy_from_user(&info, useraddr, sizeof(info)))
485 return -EFAULT;
486
487 /* store copy of mask, because we zero struct later on */
488 sset_mask = info.sset_mask;
489 if (!sset_mask)
490 return 0;
491
492 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000493 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000494
495 memset(&info, 0, sizeof(info));
496 info.cmd = ETHTOOL_GSSET_INFO;
497
498 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
499 if (!info_buf)
500 return -ENOMEM;
501
502 /*
503 * fill return buffer based on input bitmask and successful
504 * get_sset_count return
505 */
506 for (i = 0; i < 64; i++) {
507 if (!(sset_mask & (1ULL << i)))
508 continue;
509
Michał Mirosław340ae162011-02-15 16:59:16 +0000510 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000511 if (rc >= 0) {
512 info.sset_mask |= (1ULL << i);
513 info_buf[idx++] = rc;
514 }
515 }
516
517 ret = -EFAULT;
518 if (copy_to_user(useraddr, &info, sizeof(info)))
519 goto out;
520
521 useraddr += offsetof(struct ethtool_sset_info, data);
522 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
523 goto out;
524
525 ret = 0;
526
527out:
528 kfree(info_buf);
529 return ret;
530}
531
chavey97f8aef2010-04-07 21:54:42 -0700532static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000533 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700534{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000535 struct ethtool_rxnfc info;
536 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000537 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700538
Santwona Behera59089d82009-02-20 00:58:13 -0800539 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700540 return -EOPNOTSUPP;
541
Ben Hutchingsbf988432010-06-28 08:45:58 +0000542 /* struct ethtool_rxnfc was originally defined for
543 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
544 * members. User-space might still be using that
545 * definition. */
546 if (cmd == ETHTOOL_SRXFH)
547 info_size = (offsetof(struct ethtool_rxnfc, data) +
548 sizeof(info.data));
549
550 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700551 return -EFAULT;
552
Ben Hutchings55664f32012-01-03 12:04:51 +0000553 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
554 if (rc)
555 return rc;
556
557 if (cmd == ETHTOOL_SRXCLSRLINS &&
558 copy_to_user(useraddr, &info, info_size))
559 return -EFAULT;
560
561 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700562}
563
chavey97f8aef2010-04-07 21:54:42 -0700564static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000565 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700566{
567 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000568 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800569 const struct ethtool_ops *ops = dev->ethtool_ops;
570 int ret;
571 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700572
Santwona Behera59089d82009-02-20 00:58:13 -0800573 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700574 return -EOPNOTSUPP;
575
Ben Hutchingsbf988432010-06-28 08:45:58 +0000576 /* struct ethtool_rxnfc was originally defined for
577 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
578 * members. User-space might still be using that
579 * definition. */
580 if (cmd == ETHTOOL_GRXFH)
581 info_size = (offsetof(struct ethtool_rxnfc, data) +
582 sizeof(info.data));
583
584 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700585 return -EFAULT;
586
Santwona Behera59089d82009-02-20 00:58:13 -0800587 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
588 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000589 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000590 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000591 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800592 if (!rule_buf)
593 return -ENOMEM;
594 }
595 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700596
Santwona Behera59089d82009-02-20 00:58:13 -0800597 ret = ops->get_rxnfc(dev, &info, rule_buf);
598 if (ret < 0)
599 goto err_out;
600
601 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000602 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800603 goto err_out;
604
605 if (rule_buf) {
606 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
607 if (copy_to_user(useraddr, rule_buf,
608 info.rule_cnt * sizeof(u32)))
609 goto err_out;
610 }
611 ret = 0;
612
613err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700614 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800615
616 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700617}
618
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530619static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
620 struct ethtool_rxnfc *rx_rings,
621 u32 size)
622{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100623 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530624
625 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100626 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530627
628 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100629 for (i = 0; i < size; i++)
630 if (indir[i] >= rx_rings->data)
631 return -EINVAL;
632
633 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530634}
635
Kim Jonesba905f52016-02-02 03:51:16 +0000636u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -0800637
638void netdev_rss_key_fill(void *buffer, size_t len)
639{
640 BUG_ON(len > sizeof(netdev_rss_key));
641 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
642 memcpy(buffer, netdev_rss_key, len);
643}
644EXPORT_SYMBOL(netdev_rss_key_fill);
645
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -0800646static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
647{
648 u32 dev_size, current_max = 0;
649 u32 *indir;
650 int ret;
651
652 if (!dev->ethtool_ops->get_rxfh_indir_size ||
653 !dev->ethtool_ops->get_rxfh)
654 return -EOPNOTSUPP;
655 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
656 if (dev_size == 0)
657 return -EOPNOTSUPP;
658
659 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
660 if (!indir)
661 return -ENOMEM;
662
663 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
664 if (ret)
665 goto out;
666
667 while (dev_size--)
668 current_max = max(current_max, indir[dev_size]);
669
670 *max = current_max;
671
672out:
673 kfree(indir);
674 return ret;
675}
676
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000677static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
678 void __user *useraddr)
679{
Ben Hutchings7850f632011-12-15 13:55:01 +0000680 u32 user_size, dev_size;
681 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000682 int ret;
683
Ben Hutchings7850f632011-12-15 13:55:01 +0000684 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100685 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +0000686 return -EOPNOTSUPP;
687 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
688 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000689 return -EOPNOTSUPP;
690
Ben Hutchings7850f632011-12-15 13:55:01 +0000691 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000692 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000693 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000694 return -EFAULT;
695
Ben Hutchings7850f632011-12-15 13:55:01 +0000696 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
697 &dev_size, sizeof(dev_size)))
698 return -EFAULT;
699
700 /* If the user buffer size is 0, this is just a query for the
701 * device table size. Otherwise, if it's smaller than the
702 * device table size it's an error.
703 */
704 if (user_size < dev_size)
705 return user_size == 0 ? 0 : -EINVAL;
706
707 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000708 if (!indir)
709 return -ENOMEM;
710
Eyal Perry892311f2014-12-02 18:12:10 +0200711 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000712 if (ret)
713 goto out;
714
Ben Hutchings7850f632011-12-15 13:55:01 +0000715 if (copy_to_user(useraddr +
716 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
717 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000718 ret = -EFAULT;
719
720out:
721 kfree(indir);
722 return ret;
723}
724
725static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
726 void __user *useraddr)
727{
Ben Hutchings7850f632011-12-15 13:55:01 +0000728 struct ethtool_rxnfc rx_rings;
729 u32 user_size, dev_size, i;
730 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000731 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000732 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530733 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000734
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100735 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000736 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000737 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000738
739 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000740 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000741 return -EOPNOTSUPP;
742
Ben Hutchings7850f632011-12-15 13:55:01 +0000743 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000744 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000745 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000746 return -EFAULT;
747
Ben Hutchings278bc422011-12-15 13:56:49 +0000748 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000749 return -EINVAL;
750
751 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000752 if (!indir)
753 return -ENOMEM;
754
Ben Hutchings7850f632011-12-15 13:55:01 +0000755 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000756 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000757 if (ret)
758 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000759
760 if (user_size == 0) {
761 for (i = 0; i < dev_size; i++)
762 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
763 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530764 ret = ethtool_copy_validate_indir(indir,
765 useraddr + ringidx_offset,
766 &rx_rings,
767 dev_size);
768 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +0000769 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +0000770 }
771
Eyal Perry892311f2014-12-02 18:12:10 +0200772 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -0800773 if (ret)
774 goto out;
775
776 /* indicate whether rxfh was set to default */
777 if (user_size == 0)
778 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
779 else
780 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000781
782out:
783 kfree(indir);
784 return ret;
785}
786
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530787static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
788 void __user *useraddr)
789{
790 int ret;
791 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100792 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530793 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100794 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530795 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100796 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530797 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +0200798 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530799 u8 *hkey = NULL;
800 u8 *rss_config;
801
Eyal Perry892311f2014-12-02 18:12:10 +0200802 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530803 return -EOPNOTSUPP;
804
805 if (ops->get_rxfh_indir_size)
806 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530807 if (ops->get_rxfh_key_size)
808 dev_key_size = ops->get_rxfh_key_size(dev);
809
Ben Hutchingsf062a382014-05-15 16:28:07 +0100810 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530811 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100812 user_indir_size = rxfh.indir_size;
813 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530814
Ben Hutchingsf062a382014-05-15 16:28:07 +0100815 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +0200816 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
817 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +0100818 return -EINVAL;
819
820 rxfh.indir_size = dev_indir_size;
821 rxfh.key_size = dev_key_size;
822 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530823 return -EFAULT;
824
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530825 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
826 (user_key_size && (user_key_size != dev_key_size)))
827 return -EINVAL;
828
829 indir_bytes = user_indir_size * sizeof(indir[0]);
830 total_size = indir_bytes + user_key_size;
831 rss_config = kzalloc(total_size, GFP_USER);
832 if (!rss_config)
833 return -ENOMEM;
834
835 if (user_indir_size)
836 indir = (u32 *)rss_config;
837
838 if (user_key_size)
839 hkey = rss_config + indir_bytes;
840
Eyal Perry892311f2014-12-02 18:12:10 +0200841 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
842 if (ret)
843 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530844
Eyal Perry892311f2014-12-02 18:12:10 +0200845 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
846 &dev_hfunc, sizeof(rxfh.hfunc))) {
847 ret = -EFAULT;
848 } else if (copy_to_user(useraddr +
849 offsetof(struct ethtool_rxfh, rss_config[0]),
850 rss_config, total_size)) {
851 ret = -EFAULT;
852 }
853out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530854 kfree(rss_config);
855
856 return ret;
857}
858
859static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
860 void __user *useraddr)
861{
862 int ret;
863 const struct ethtool_ops *ops = dev->ethtool_ops;
864 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100865 struct ethtool_rxfh rxfh;
866 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530867 u32 *indir = NULL, indir_bytes = 0;
868 u8 *hkey = NULL;
869 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530870 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
871
Eyal Perry892311f2014-12-02 18:12:10 +0200872 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530873 return -EOPNOTSUPP;
874
875 if (ops->get_rxfh_indir_size)
876 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530877 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +0300878 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530879
Ben Hutchingsf062a382014-05-15 16:28:07 +0100880 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530881 return -EFAULT;
882
Ben Hutchingsf062a382014-05-15 16:28:07 +0100883 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +0200884 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
885 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +0100886 return -EINVAL;
887
Eyal Perry892311f2014-12-02 18:12:10 +0200888 /* If either indir, hash key or function is valid, proceed further.
889 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530890 */
Ben Hutchingsf062a382014-05-15 16:28:07 +0100891 if ((rxfh.indir_size &&
892 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
893 rxfh.indir_size != dev_indir_size) ||
894 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
895 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +0200896 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530897 return -EINVAL;
898
Ben Hutchingsf062a382014-05-15 16:28:07 +0100899 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530900 indir_bytes = dev_indir_size * sizeof(indir[0]);
901
Ben Hutchingsf062a382014-05-15 16:28:07 +0100902 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530903 if (!rss_config)
904 return -ENOMEM;
905
906 rx_rings.cmd = ETHTOOL_GRXRINGS;
907 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
908 if (ret)
909 goto out;
910
Ben Hutchingsf062a382014-05-15 16:28:07 +0100911 /* rxfh.indir_size == 0 means reset the indir table to default.
912 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530913 */
Ben Hutchingsf062a382014-05-15 16:28:07 +0100914 if (rxfh.indir_size &&
915 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530916 indir = (u32 *)rss_config;
917 ret = ethtool_copy_validate_indir(indir,
918 useraddr + rss_cfg_offset,
919 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +0100920 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530921 if (ret)
922 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100923 } else if (rxfh.indir_size == 0) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530924 indir = (u32 *)rss_config;
925 for (i = 0; i < dev_indir_size; i++)
926 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
927 }
928
Ben Hutchingsf062a382014-05-15 16:28:07 +0100929 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530930 hkey = rss_config + indir_bytes;
931 if (copy_from_user(hkey,
932 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +0100933 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530934 ret = -EFAULT;
935 goto out;
936 }
937 }
938
Eyal Perry892311f2014-12-02 18:12:10 +0200939 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -0800940 if (ret)
941 goto out;
942
943 /* indicate whether rxfh was set to default */
944 if (rxfh.indir_size == 0)
945 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
946 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
947 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530948
949out:
950 kfree(rss_config);
951 return ret;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
955{
956 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700957 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 void *regbuf;
959 int reglen, ret;
960
961 if (!ops->get_regs || !ops->get_regs_len)
962 return -EOPNOTSUPP;
963
964 if (copy_from_user(&regs, useraddr, sizeof(regs)))
965 return -EFAULT;
966
967 reglen = ops->get_regs_len(dev);
968 if (regs.len > reglen)
969 regs.len = reglen;
970
Eugene Teob7c7d012011-01-24 21:05:17 -0800971 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700972 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -ENOMEM;
974
975 ops->get_regs(dev, &regs, regbuf);
976
977 ret = -EFAULT;
978 if (copy_to_user(useraddr, &regs, sizeof(regs)))
979 goto out;
980 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700981 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto out;
983 ret = 0;
984
985 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000986 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return ret;
988}
989
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000990static int ethtool_reset(struct net_device *dev, char __user *useraddr)
991{
992 struct ethtool_value reset;
993 int ret;
994
995 if (!dev->ethtool_ops->reset)
996 return -EOPNOTSUPP;
997
998 if (copy_from_user(&reset, useraddr, sizeof(reset)))
999 return -EFAULT;
1000
1001 ret = dev->ethtool_ops->reset(dev, &reset.data);
1002 if (ret)
1003 return ret;
1004
1005 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1006 return -EFAULT;
1007 return 0;
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1011{
Roland Dreier8e557422010-02-11 12:14:23 -08001012 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 if (!dev->ethtool_ops->get_wol)
1015 return -EOPNOTSUPP;
1016
1017 dev->ethtool_ops->get_wol(dev, &wol);
1018
1019 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1020 return -EFAULT;
1021 return 0;
1022}
1023
1024static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1025{
1026 struct ethtool_wolinfo wol;
1027
1028 if (!dev->ethtool_ops->set_wol)
1029 return -EOPNOTSUPP;
1030
1031 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1032 return -EFAULT;
1033
1034 return dev->ethtool_ops->set_wol(dev, &wol);
1035}
1036
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001037static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1038{
1039 struct ethtool_eee edata;
1040 int rc;
1041
1042 if (!dev->ethtool_ops->get_eee)
1043 return -EOPNOTSUPP;
1044
1045 memset(&edata, 0, sizeof(struct ethtool_eee));
1046 edata.cmd = ETHTOOL_GEEE;
1047 rc = dev->ethtool_ops->get_eee(dev, &edata);
1048
1049 if (rc)
1050 return rc;
1051
1052 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1053 return -EFAULT;
1054
1055 return 0;
1056}
1057
1058static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1059{
1060 struct ethtool_eee edata;
1061
1062 if (!dev->ethtool_ops->set_eee)
1063 return -EOPNOTSUPP;
1064
1065 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1066 return -EFAULT;
1067
1068 return dev->ethtool_ops->set_eee(dev, &edata);
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static int ethtool_nway_reset(struct net_device *dev)
1072{
1073 if (!dev->ethtool_ops->nway_reset)
1074 return -EOPNOTSUPP;
1075
1076 return dev->ethtool_ops->nway_reset(dev);
1077}
1078
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001079static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1080{
1081 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1082
1083 if (!dev->ethtool_ops->get_link)
1084 return -EOPNOTSUPP;
1085
1086 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1087
1088 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1089 return -EFAULT;
1090 return 0;
1091}
1092
Ben Hutchings081d0942012-04-12 00:42:12 +00001093static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1094 int (*getter)(struct net_device *,
1095 struct ethtool_eeprom *, u8 *),
1096 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001099 void __user *userbuf = useraddr + sizeof(eeprom);
1100 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001102 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1105 return -EFAULT;
1106
1107 /* Check for wrap and zero */
1108 if (eeprom.offset + eeprom.len <= eeprom.offset)
1109 return -EINVAL;
1110
1111 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001112 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return -EINVAL;
1114
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001115 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!data)
1117 return -ENOMEM;
1118
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001119 bytes_remaining = eeprom.len;
1120 while (bytes_remaining > 0) {
1121 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Ben Hutchings081d0942012-04-12 00:42:12 +00001123 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001124 if (ret)
1125 break;
1126 if (copy_to_user(userbuf, data, eeprom.len)) {
1127 ret = -EFAULT;
1128 break;
1129 }
1130 userbuf += eeprom.len;
1131 eeprom.offset += eeprom.len;
1132 bytes_remaining -= eeprom.len;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001135 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1136 eeprom.offset -= eeprom.len;
1137 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1138 ret = -EFAULT;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 kfree(data);
1141 return ret;
1142}
1143
Ben Hutchings081d0942012-04-12 00:42:12 +00001144static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1145{
1146 const struct ethtool_ops *ops = dev->ethtool_ops;
1147
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001148 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1149 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001150 return -EOPNOTSUPP;
1151
1152 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1153 ops->get_eeprom_len(dev));
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1157{
1158 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001159 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001160 void __user *userbuf = useraddr + sizeof(eeprom);
1161 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001163 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001165 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1166 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -EOPNOTSUPP;
1168
1169 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1170 return -EFAULT;
1171
1172 /* Check for wrap and zero */
1173 if (eeprom.offset + eeprom.len <= eeprom.offset)
1174 return -EINVAL;
1175
1176 /* Check for exceeding total eeprom len */
1177 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1178 return -EINVAL;
1179
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001180 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!data)
1182 return -ENOMEM;
1183
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001184 bytes_remaining = eeprom.len;
1185 while (bytes_remaining > 0) {
1186 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001188 if (copy_from_user(data, userbuf, eeprom.len)) {
1189 ret = -EFAULT;
1190 break;
1191 }
1192 ret = ops->set_eeprom(dev, &eeprom, data);
1193 if (ret)
1194 break;
1195 userbuf += eeprom.len;
1196 eeprom.offset += eeprom.len;
1197 bytes_remaining -= eeprom.len;
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 kfree(data);
1201 return ret;
1202}
1203
chavey97f8aef2010-04-07 21:54:42 -07001204static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1205 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Roland Dreier8e557422010-02-11 12:14:23 -08001207 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (!dev->ethtool_ops->get_coalesce)
1210 return -EOPNOTSUPP;
1211
1212 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1213
1214 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1215 return -EFAULT;
1216 return 0;
1217}
1218
chavey97f8aef2010-04-07 21:54:42 -07001219static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1220 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct ethtool_coalesce coalesce;
1223
David S. Millerfa04ae52005-06-06 15:07:19 -07001224 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return -EOPNOTSUPP;
1226
1227 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1228 return -EFAULT;
1229
1230 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1231}
1232
1233static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1234{
Roland Dreier8e557422010-02-11 12:14:23 -08001235 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (!dev->ethtool_ops->get_ringparam)
1238 return -EOPNOTSUPP;
1239
1240 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1241
1242 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1243 return -EFAULT;
1244 return 0;
1245}
1246
1247static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1248{
1249 struct ethtool_ringparam ringparam;
1250
1251 if (!dev->ethtool_ops->set_ringparam)
1252 return -EOPNOTSUPP;
1253
1254 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1255 return -EFAULT;
1256
1257 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1258}
1259
amit salecha8b5933c2011-04-07 01:58:42 +00001260static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1261 void __user *useraddr)
1262{
1263 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1264
1265 if (!dev->ethtool_ops->get_channels)
1266 return -EOPNOTSUPP;
1267
1268 dev->ethtool_ops->get_channels(dev, &channels);
1269
1270 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1271 return -EFAULT;
1272 return 0;
1273}
1274
1275static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1276 void __user *useraddr)
1277{
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001278 struct ethtool_channels channels, max;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001279 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001280
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001281 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001282 return -EOPNOTSUPP;
1283
1284 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1285 return -EFAULT;
1286
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001287 dev->ethtool_ops->get_channels(dev, &max);
1288
1289 /* ensure new counts are within the maximums */
1290 if ((channels.rx_count > max.max_rx) ||
1291 (channels.tx_count > max.max_tx) ||
1292 (channels.combined_count > max.max_combined) ||
1293 (channels.other_count > max.max_other))
1294 return -EINVAL;
1295
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001296 /* ensure the new Rx count fits within the configured Rx flow
1297 * indirection table settings */
1298 if (netif_is_rxfh_configured(dev) &&
1299 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1300 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1301 return -EINVAL;
1302
amit salecha8b5933c2011-04-07 01:58:42 +00001303 return dev->ethtool_ops->set_channels(dev, &channels);
1304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1307{
1308 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1309
1310 if (!dev->ethtool_ops->get_pauseparam)
1311 return -EOPNOTSUPP;
1312
1313 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1314
1315 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1316 return -EFAULT;
1317 return 0;
1318}
1319
1320static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1321{
1322 struct ethtool_pauseparam pauseparam;
1323
Jeff Garzike1b90c42006-07-17 12:54:40 -04001324 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EOPNOTSUPP;
1326
1327 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1328 return -EFAULT;
1329
1330 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1334{
1335 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001336 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001338 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001340 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001341 return -EOPNOTSUPP;
1342
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001343 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001344 if (test_len < 0)
1345 return test_len;
1346 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 if (copy_from_user(&test, useraddr, sizeof(test)))
1349 return -EFAULT;
1350
Jeff Garzikff03d492007-08-15 16:01:08 -07001351 test.len = test_len;
1352 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (!data)
1354 return -ENOMEM;
1355
1356 ops->self_test(dev, &test, data);
1357
1358 ret = -EFAULT;
1359 if (copy_to_user(useraddr, &test, sizeof(test)))
1360 goto out;
1361 useraddr += sizeof(test);
1362 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1363 goto out;
1364 ret = 0;
1365
1366 out:
1367 kfree(data);
1368 return ret;
1369}
1370
1371static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1372{
1373 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 u8 *data;
1375 int ret;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1378 return -EFAULT;
1379
Michał Mirosław340ae162011-02-15 16:59:16 +00001380 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001381 if (ret < 0)
1382 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001383
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001384 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Joe Perches077cb372015-10-14 01:09:40 -07001386 data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (!data)
1388 return -ENOMEM;
1389
Michał Mirosław340ae162011-02-15 16:59:16 +00001390 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 ret = -EFAULT;
1393 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1394 goto out;
1395 useraddr += sizeof(gstrings);
1396 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1397 goto out;
1398 ret = 0;
1399
Michał Mirosław340ae162011-02-15 16:59:16 +00001400out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 kfree(data);
1402 return ret;
1403}
1404
1405static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1406{
1407 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001408 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001409 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001410 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001412 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EOPNOTSUPP;
1414
Ben Hutchings68f512f2011-04-02 00:35:15 +01001415 if (busy)
1416 return -EBUSY;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (copy_from_user(&id, useraddr, sizeof(id)))
1419 return -EFAULT;
1420
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001421 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001422 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001423 return rc;
1424
1425 /* Drop the RTNL lock while waiting, but prevent reentry or
1426 * removal of the device.
1427 */
1428 busy = true;
1429 dev_hold(dev);
1430 rtnl_unlock();
1431
1432 if (rc == 0) {
1433 /* Driver will handle this itself */
1434 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001435 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001436 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001437 /* Driver expects to be called at twice the frequency in rc */
1438 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001439
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001440 /* Count down seconds */
1441 do {
1442 /* Count down iterations per second */
1443 i = n;
1444 do {
1445 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001446 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001447 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1448 rtnl_unlock();
1449 if (rc)
1450 break;
1451 schedule_timeout_interruptible(interval);
1452 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001453 } while (!signal_pending(current) &&
1454 (id.data == 0 || --id.data != 0));
1455 }
1456
1457 rtnl_lock();
1458 dev_put(dev);
1459 busy = false;
1460
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001461 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001462 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
1465static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1466{
1467 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001468 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001470 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001472 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001473 return -EOPNOTSUPP;
1474
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001475 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001476 if (n_stats < 0)
1477 return n_stats;
1478 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1481 return -EFAULT;
1482
Jeff Garzikff03d492007-08-15 16:01:08 -07001483 stats.n_stats = n_stats;
1484 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (!data)
1486 return -ENOMEM;
1487
1488 ops->get_ethtool_stats(dev, &stats, data);
1489
1490 ret = -EFAULT;
1491 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1492 goto out;
1493 useraddr += sizeof(stats);
1494 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1495 goto out;
1496 ret = 0;
1497
1498 out:
1499 kfree(data);
1500 return ret;
1501}
1502
Andrew Lunnf3a40942015-12-30 16:28:25 +01001503static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1504{
1505 struct ethtool_stats stats;
1506 struct phy_device *phydev = dev->phydev;
1507 u64 *data;
1508 int ret, n_stats;
1509
1510 if (!phydev)
1511 return -EOPNOTSUPP;
1512
1513 n_stats = phy_get_sset_count(phydev);
1514
1515 if (n_stats < 0)
1516 return n_stats;
1517 WARN_ON(n_stats == 0);
1518
1519 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1520 return -EFAULT;
1521
1522 stats.n_stats = n_stats;
1523 data = kmalloc_array(n_stats, sizeof(u64), GFP_USER);
1524 if (!data)
1525 return -ENOMEM;
1526
1527 mutex_lock(&phydev->lock);
1528 phydev->drv->get_stats(phydev, &stats, data);
1529 mutex_unlock(&phydev->lock);
1530
1531 ret = -EFAULT;
1532 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1533 goto out;
1534 useraddr += sizeof(stats);
1535 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1536 goto out;
1537 ret = 0;
1538
1539 out:
1540 kfree(data);
1541 return ret;
1542}
1543
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001544static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001545{
1546 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001547
Matthew Wilcox313674a2007-07-31 14:00:29 -07001548 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001549 return -EFAULT;
1550
Matthew Wilcox313674a2007-07-31 14:00:29 -07001551 if (epaddr.size < dev->addr_len)
1552 return -ETOOSMALL;
1553 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001554
Jon Wetzela6f9a702005-08-20 17:15:54 -07001555 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001556 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001557 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001558 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1559 return -EFAULT;
1560 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001561}
1562
Jeff Garzik13c99b22007-08-15 16:01:56 -07001563static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1564 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001565{
Roland Dreier8e557422010-02-11 12:14:23 -08001566 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001567
Jeff Garzik13c99b22007-08-15 16:01:56 -07001568 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001569 return -EOPNOTSUPP;
1570
Jeff Garzik13c99b22007-08-15 16:01:56 -07001571 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001572
1573 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1574 return -EFAULT;
1575 return 0;
1576}
1577
Jeff Garzik13c99b22007-08-15 16:01:56 -07001578static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1579 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001580{
1581 struct ethtool_value edata;
1582
Jeff Garzik13c99b22007-08-15 16:01:56 -07001583 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001584 return -EOPNOTSUPP;
1585
1586 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1587 return -EFAULT;
1588
Jeff Garzik13c99b22007-08-15 16:01:56 -07001589 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001590 return 0;
1591}
1592
Jeff Garzik13c99b22007-08-15 16:01:56 -07001593static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1594 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001595{
1596 struct ethtool_value edata;
1597
Jeff Garzik13c99b22007-08-15 16:01:56 -07001598 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001599 return -EOPNOTSUPP;
1600
1601 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1602 return -EFAULT;
1603
Jeff Garzik13c99b22007-08-15 16:01:56 -07001604 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001605}
1606
chavey97f8aef2010-04-07 21:54:42 -07001607static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1608 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001609{
1610 struct ethtool_flash efl;
1611
1612 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1613 return -EFAULT;
1614
1615 if (!dev->ethtool_ops->flash_device)
1616 return -EOPNOTSUPP;
1617
Ben Hutchings786f5282012-02-01 09:32:25 +00001618 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1619
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001620 return dev->ethtool_ops->flash_device(dev, &efl);
1621}
1622
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001623static int ethtool_set_dump(struct net_device *dev,
1624 void __user *useraddr)
1625{
1626 struct ethtool_dump dump;
1627
1628 if (!dev->ethtool_ops->set_dump)
1629 return -EOPNOTSUPP;
1630
1631 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1632 return -EFAULT;
1633
1634 return dev->ethtool_ops->set_dump(dev, &dump);
1635}
1636
1637static int ethtool_get_dump_flag(struct net_device *dev,
1638 void __user *useraddr)
1639{
1640 int ret;
1641 struct ethtool_dump dump;
1642 const struct ethtool_ops *ops = dev->ethtool_ops;
1643
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001644 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001645 return -EOPNOTSUPP;
1646
1647 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1648 return -EFAULT;
1649
1650 ret = ops->get_dump_flag(dev, &dump);
1651 if (ret)
1652 return ret;
1653
1654 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1655 return -EFAULT;
1656 return 0;
1657}
1658
1659static int ethtool_get_dump_data(struct net_device *dev,
1660 void __user *useraddr)
1661{
1662 int ret;
1663 __u32 len;
1664 struct ethtool_dump dump, tmp;
1665 const struct ethtool_ops *ops = dev->ethtool_ops;
1666 void *data = NULL;
1667
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001668 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001669 return -EOPNOTSUPP;
1670
1671 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1672 return -EFAULT;
1673
1674 memset(&tmp, 0, sizeof(tmp));
1675 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1676 ret = ops->get_dump_flag(dev, &tmp);
1677 if (ret)
1678 return ret;
1679
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001680 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001681 if (!len)
1682 return -EFAULT;
1683
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001684 /* Don't ever let the driver think there's more space available
1685 * than it requested with .get_dump_flag().
1686 */
1687 dump.len = len;
1688
1689 /* Always allocate enough space to hold the whole thing so that the
1690 * driver does not need to check the length and bother with partial
1691 * dumping.
1692 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001693 data = vzalloc(tmp.len);
1694 if (!data)
1695 return -ENOMEM;
1696 ret = ops->get_dump_data(dev, &dump, data);
1697 if (ret)
1698 goto out;
1699
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001700 /* There are two sane possibilities:
1701 * 1. The driver's .get_dump_data() does not touch dump.len.
1702 * 2. Or it may set dump.len to how much it really writes, which
1703 * should be tmp.len (or len if it can do a partial dump).
1704 * In any case respond to userspace with the actual length of data
1705 * it's receiving.
1706 */
1707 WARN_ON(dump.len != len && dump.len != tmp.len);
1708 dump.len = len;
1709
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001710 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1711 ret = -EFAULT;
1712 goto out;
1713 }
1714 useraddr += offsetof(struct ethtool_dump, data);
1715 if (copy_to_user(useraddr, data, len))
1716 ret = -EFAULT;
1717out:
1718 vfree(data);
1719 return ret;
1720}
1721
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001722static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1723{
1724 int err = 0;
1725 struct ethtool_ts_info info;
1726 const struct ethtool_ops *ops = dev->ethtool_ops;
1727 struct phy_device *phydev = dev->phydev;
1728
1729 memset(&info, 0, sizeof(info));
1730 info.cmd = ETHTOOL_GET_TS_INFO;
1731
1732 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001733 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001734 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001735 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001736 } else {
1737 info.so_timestamping =
1738 SOF_TIMESTAMPING_RX_SOFTWARE |
1739 SOF_TIMESTAMPING_SOFTWARE;
1740 info.phc_index = -1;
1741 }
1742
1743 if (err)
1744 return err;
1745
1746 if (copy_to_user(useraddr, &info, sizeof(info)))
1747 err = -EFAULT;
1748
1749 return err;
1750}
1751
Ed Swierk2f438362015-01-02 17:27:56 -08001752static int __ethtool_get_module_info(struct net_device *dev,
1753 struct ethtool_modinfo *modinfo)
1754{
1755 const struct ethtool_ops *ops = dev->ethtool_ops;
1756 struct phy_device *phydev = dev->phydev;
1757
1758 if (phydev && phydev->drv && phydev->drv->module_info)
1759 return phydev->drv->module_info(phydev, modinfo);
1760
1761 if (ops->get_module_info)
1762 return ops->get_module_info(dev, modinfo);
1763
1764 return -EOPNOTSUPP;
1765}
1766
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001767static int ethtool_get_module_info(struct net_device *dev,
1768 void __user *useraddr)
1769{
1770 int ret;
1771 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001772
1773 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1774 return -EFAULT;
1775
Ed Swierk2f438362015-01-02 17:27:56 -08001776 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001777 if (ret)
1778 return ret;
1779
1780 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1781 return -EFAULT;
1782
1783 return 0;
1784}
1785
Ed Swierk2f438362015-01-02 17:27:56 -08001786static int __ethtool_get_module_eeprom(struct net_device *dev,
1787 struct ethtool_eeprom *ee, u8 *data)
1788{
1789 const struct ethtool_ops *ops = dev->ethtool_ops;
1790 struct phy_device *phydev = dev->phydev;
1791
1792 if (phydev && phydev->drv && phydev->drv->module_eeprom)
1793 return phydev->drv->module_eeprom(phydev, ee, data);
1794
1795 if (ops->get_module_eeprom)
1796 return ops->get_module_eeprom(dev, ee, data);
1797
1798 return -EOPNOTSUPP;
1799}
1800
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001801static int ethtool_get_module_eeprom(struct net_device *dev,
1802 void __user *useraddr)
1803{
1804 int ret;
1805 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001806
Ed Swierk2f438362015-01-02 17:27:56 -08001807 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001808 if (ret)
1809 return ret;
1810
Ed Swierk2f438362015-01-02 17:27:56 -08001811 return ethtool_get_any_eeprom(dev, useraddr,
1812 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001813 modinfo.eeprom_len);
1814}
1815
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301816static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
1817{
1818 switch (tuna->id) {
1819 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03001820 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301821 if (tuna->len != sizeof(u32) ||
1822 tuna->type_id != ETHTOOL_TUNABLE_U32)
1823 return -EINVAL;
1824 break;
1825 default:
1826 return -EINVAL;
1827 }
1828
1829 return 0;
1830}
1831
1832static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
1833{
1834 int ret;
1835 struct ethtool_tunable tuna;
1836 const struct ethtool_ops *ops = dev->ethtool_ops;
1837 void *data;
1838
1839 if (!ops->get_tunable)
1840 return -EOPNOTSUPP;
1841 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1842 return -EFAULT;
1843 ret = ethtool_tunable_valid(&tuna);
1844 if (ret)
1845 return ret;
1846 data = kmalloc(tuna.len, GFP_USER);
1847 if (!data)
1848 return -ENOMEM;
1849 ret = ops->get_tunable(dev, &tuna, data);
1850 if (ret)
1851 goto out;
1852 useraddr += sizeof(tuna);
1853 ret = -EFAULT;
1854 if (copy_to_user(useraddr, data, tuna.len))
1855 goto out;
1856 ret = 0;
1857
1858out:
1859 kfree(data);
1860 return ret;
1861}
1862
1863static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
1864{
1865 int ret;
1866 struct ethtool_tunable tuna;
1867 const struct ethtool_ops *ops = dev->ethtool_ops;
1868 void *data;
1869
1870 if (!ops->set_tunable)
1871 return -EOPNOTSUPP;
1872 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1873 return -EFAULT;
1874 ret = ethtool_tunable_valid(&tuna);
1875 if (ret)
1876 return ret;
1877 data = kmalloc(tuna.len, GFP_USER);
1878 if (!data)
1879 return -ENOMEM;
1880 useraddr += sizeof(tuna);
1881 ret = -EFAULT;
1882 if (copy_from_user(data, useraddr, tuna.len))
1883 goto out;
1884 ret = ops->set_tunable(dev, &tuna, data);
1885
1886out:
1887 kfree(data);
1888 return ret;
1889}
1890
Kan Liangac2c7ad2016-02-19 09:24:01 -05001891static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
1892{
1893 struct ethtool_per_queue_op per_queue_opt;
1894
1895 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
1896 return -EFAULT;
1897
1898 switch (per_queue_opt.sub_command) {
1899
1900 default:
1901 return -EOPNOTSUPP;
1902 };
1903}
1904
Yan Burman600fed52013-06-03 02:03:34 +00001905/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Eric W. Biederman881d9662007-09-17 11:56:21 -07001907int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001909 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05001911 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00001913 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (!dev || !netif_device_present(dev))
1916 return -ENODEV;
1917
chavey97f8aef2010-04-07 21:54:42 -07001918 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return -EFAULT;
1920
Kan Liangac2c7ad2016-02-19 09:24:01 -05001921 if (ethcmd == ETHTOOL_PERQUEUE) {
1922 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
1923 return -EFAULT;
1924 } else {
1925 sub_cmd = ethcmd;
1926 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07001927 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05001928 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001929 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001930 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001931 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001932 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001933 case ETHTOOL_GCOALESCE:
1934 case ETHTOOL_GRINGPARAM:
1935 case ETHTOOL_GPAUSEPARAM:
1936 case ETHTOOL_GRXCSUM:
1937 case ETHTOOL_GTXCSUM:
1938 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001939 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001940 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001941 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01001942 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001943 case ETHTOOL_GTSO:
1944 case ETHTOOL_GPERMADDR:
1945 case ETHTOOL_GUFO:
1946 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001947 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001948 case ETHTOOL_GFLAGS:
1949 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001950 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001951 case ETHTOOL_GRXRINGS:
1952 case ETHTOOL_GRXCLSRLCNT:
1953 case ETHTOOL_GRXCLSRULE:
1954 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001955 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301956 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00001957 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001958 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001959 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001960 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301961 case ETHTOOL_GTUNABLE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001962 break;
1963 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001964 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001965 return -EPERM;
1966 }
1967
chavey97f8aef2010-04-07 21:54:42 -07001968 if (dev->ethtool_ops->begin) {
1969 rc = dev->ethtool_ops->begin(dev);
1970 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001972 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001973 old_features = dev->features;
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 switch (ethcmd) {
1976 case ETHTOOL_GSET:
1977 rc = ethtool_get_settings(dev, useraddr);
1978 break;
1979 case ETHTOOL_SSET:
1980 rc = ethtool_set_settings(dev, useraddr);
1981 break;
1982 case ETHTOOL_GDRVINFO:
1983 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
1985 case ETHTOOL_GREGS:
1986 rc = ethtool_get_regs(dev, useraddr);
1987 break;
1988 case ETHTOOL_GWOL:
1989 rc = ethtool_get_wol(dev, useraddr);
1990 break;
1991 case ETHTOOL_SWOL:
1992 rc = ethtool_set_wol(dev, useraddr);
1993 break;
1994 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001995 rc = ethtool_get_value(dev, useraddr, ethcmd,
1996 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001999 rc = ethtool_set_value_void(dev, useraddr,
2000 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002002 case ETHTOOL_GEEE:
2003 rc = ethtool_get_eee(dev, useraddr);
2004 break;
2005 case ETHTOOL_SEEE:
2006 rc = ethtool_set_eee(dev, useraddr);
2007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 case ETHTOOL_NWAY_RST:
2009 rc = ethtool_nway_reset(dev);
2010 break;
2011 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002012 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 case ETHTOOL_GEEPROM:
2015 rc = ethtool_get_eeprom(dev, useraddr);
2016 break;
2017 case ETHTOOL_SEEPROM:
2018 rc = ethtool_set_eeprom(dev, useraddr);
2019 break;
2020 case ETHTOOL_GCOALESCE:
2021 rc = ethtool_get_coalesce(dev, useraddr);
2022 break;
2023 case ETHTOOL_SCOALESCE:
2024 rc = ethtool_set_coalesce(dev, useraddr);
2025 break;
2026 case ETHTOOL_GRINGPARAM:
2027 rc = ethtool_get_ringparam(dev, useraddr);
2028 break;
2029 case ETHTOOL_SRINGPARAM:
2030 rc = ethtool_set_ringparam(dev, useraddr);
2031 break;
2032 case ETHTOOL_GPAUSEPARAM:
2033 rc = ethtool_get_pauseparam(dev, useraddr);
2034 break;
2035 case ETHTOOL_SPAUSEPARAM:
2036 rc = ethtool_set_pauseparam(dev, useraddr);
2037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 case ETHTOOL_TEST:
2039 rc = ethtool_self_test(dev, useraddr);
2040 break;
2041 case ETHTOOL_GSTRINGS:
2042 rc = ethtool_get_strings(dev, useraddr);
2043 break;
2044 case ETHTOOL_PHYS_ID:
2045 rc = ethtool_phys_id(dev, useraddr);
2046 break;
2047 case ETHTOOL_GSTATS:
2048 rc = ethtool_get_stats(dev, useraddr);
2049 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002050 case ETHTOOL_GPERMADDR:
2051 rc = ethtool_get_perm_addr(dev, useraddr);
2052 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002053 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002054 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002055 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002056 break;
2057 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002058 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002059 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002060 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002061 rc = ethtool_get_value(dev, useraddr, ethcmd,
2062 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002063 break;
2064 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002065 rc = ethtool_set_value(dev, useraddr,
2066 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002067 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002068 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002069 case ETHTOOL_GRXRINGS:
2070 case ETHTOOL_GRXCLSRLCNT:
2071 case ETHTOOL_GRXCLSRULE:
2072 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002073 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002074 break;
2075 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002076 case ETHTOOL_SRXCLSRLDEL:
2077 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002078 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002079 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002080 case ETHTOOL_FLASHDEV:
2081 rc = ethtool_flash_device(dev, useraddr);
2082 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002083 case ETHTOOL_RESET:
2084 rc = ethtool_reset(dev, useraddr);
2085 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002086 case ETHTOOL_GSSET_INFO:
2087 rc = ethtool_get_sset_info(dev, useraddr);
2088 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002089 case ETHTOOL_GRXFHINDIR:
2090 rc = ethtool_get_rxfh_indir(dev, useraddr);
2091 break;
2092 case ETHTOOL_SRXFHINDIR:
2093 rc = ethtool_set_rxfh_indir(dev, useraddr);
2094 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302095 case ETHTOOL_GRSSH:
2096 rc = ethtool_get_rxfh(dev, useraddr);
2097 break;
2098 case ETHTOOL_SRSSH:
2099 rc = ethtool_set_rxfh(dev, useraddr);
2100 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002101 case ETHTOOL_GFEATURES:
2102 rc = ethtool_get_features(dev, useraddr);
2103 break;
2104 case ETHTOOL_SFEATURES:
2105 rc = ethtool_set_features(dev, useraddr);
2106 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002107 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002108 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002109 case ETHTOOL_GSG:
2110 case ETHTOOL_GTSO:
2111 case ETHTOOL_GUFO:
2112 case ETHTOOL_GGSO:
2113 case ETHTOOL_GGRO:
2114 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2115 break;
2116 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002117 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002118 case ETHTOOL_SSG:
2119 case ETHTOOL_STSO:
2120 case ETHTOOL_SUFO:
2121 case ETHTOOL_SGSO:
2122 case ETHTOOL_SGRO:
2123 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2124 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002125 case ETHTOOL_GCHANNELS:
2126 rc = ethtool_get_channels(dev, useraddr);
2127 break;
2128 case ETHTOOL_SCHANNELS:
2129 rc = ethtool_set_channels(dev, useraddr);
2130 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002131 case ETHTOOL_SET_DUMP:
2132 rc = ethtool_set_dump(dev, useraddr);
2133 break;
2134 case ETHTOOL_GET_DUMP_FLAG:
2135 rc = ethtool_get_dump_flag(dev, useraddr);
2136 break;
2137 case ETHTOOL_GET_DUMP_DATA:
2138 rc = ethtool_get_dump_data(dev, useraddr);
2139 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002140 case ETHTOOL_GET_TS_INFO:
2141 rc = ethtool_get_ts_info(dev, useraddr);
2142 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002143 case ETHTOOL_GMODULEINFO:
2144 rc = ethtool_get_module_info(dev, useraddr);
2145 break;
2146 case ETHTOOL_GMODULEEEPROM:
2147 rc = ethtool_get_module_eeprom(dev, useraddr);
2148 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302149 case ETHTOOL_GTUNABLE:
2150 rc = ethtool_get_tunable(dev, useraddr);
2151 break;
2152 case ETHTOOL_STUNABLE:
2153 rc = ethtool_set_tunable(dev, useraddr);
2154 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002155 case ETHTOOL_GPHYSTATS:
2156 rc = ethtool_get_phy_stats(dev, useraddr);
2157 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002158 case ETHTOOL_PERQUEUE:
2159 rc = ethtool_set_per_queue(dev, useraddr);
2160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002162 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002164
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002165 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002167
2168 if (old_features != dev->features)
2169 netdev_features_change(dev);
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}