]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/core/ethtool.c
net/ethtool: support get coalesce per queue
[mirror_ubuntu-zesty-kernel.git] / net / core / ethtool.c
CommitLineData
1da177e4
LT
1/*
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
61a44b9c 6 * the information ethtool needs.
1da177e4 7 *
61a44b9c
MW
8 * 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.
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/types.h>
4fc268d2 16#include <linux/capability.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
c8f3a8c3
RC
20#include <linux/net_tstamp.h>
21#include <linux/phy.h>
d17792eb 22#include <linux/bitops.h>
97f8aefb 23#include <linux/uaccess.h>
73da16c2 24#include <linux/vmalloc.h>
5a0e3ad6 25#include <linux/slab.h>
68f512f2
BH
26#include <linux/rtnetlink.h>
27#include <linux/sched.h>
960fb622 28#include <linux/net.h>
1da177e4 29
4ec93edb 30/*
1da177e4
LT
31 * 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}
97f8aefb 40EXPORT_SYMBOL(ethtool_op_get_link);
1da177e4 41
02eacbd0
RC
42int 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
1da177e4
LT
53/* Handlers for each ethtool command */
54
475414f6 55#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
5455c699 56
9d921549
MM
57static 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",
9d921549
MM
60 [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",
788dfcac 64 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
9d921549 65
788dfcac
FLVC
66 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
2d6577f1
DM
68 [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",
9d921549
MM
71 [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",
68c33163 84 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
cb32f511 85 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
61c1db7f 86 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
73136267 87 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
9d921549
MM
88
89 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
53692b1d 90 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
9d921549
MM
91 [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",
36eabda3 97 [NETIF_F_RXFCS_BIT] = "rx-fcs",
5e0c03c8 98 [NETIF_F_RXALL_BIT] = "rx-all",
a6cc0cfa 99 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
d0290214 100 [NETIF_F_BUSY_POLL_BIT] = "busy-poll",
1c78c64e 101 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
9d921549
MM
102};
103
892311f6
EP
104static 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
a4244b0c
HHZ
110static 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
5455c699
MM
117static 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 };
475414f6 123 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
5455c699
MM
124 u32 __user *sizeaddr;
125 u32 copy_size;
475414f6
MM
126 int i;
127
128 /* in case feature bits run out again */
09da71b1 129 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
475414f6
MM
130
131 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
132 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));
475414f6 137 }
5455c699
MM
138
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];
475414f6
MM
159 netdev_features_t wanted = 0, valid = 0;
160 int i, ret = 0;
5455c699
MM
161
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
475414f6 172 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
173 valid |= (netdev_features_t)features[i].valid << (32 * i);
174 wanted |= (netdev_features_t)features[i].requested << (32 * i);
475414f6
MM
175 }
176
177 if (valid & ~NETIF_F_ETHTOOL_BITS)
5455c699
MM
178 return -EINVAL;
179
475414f6
MM
180 if (valid & ~dev->hw_features) {
181 valid &= dev->hw_features;
5455c699
MM
182 ret |= ETHTOOL_F_UNSUPPORTED;
183 }
184
475414f6
MM
185 dev->wanted_features &= ~valid;
186 dev->wanted_features |= wanted & valid;
6cb6a27c 187 __netdev_update_features(dev);
5455c699 188
475414f6 189 if ((dev->wanted_features ^ dev->features) & valid)
5455c699
MM
190 ret |= ETHTOOL_F_WISH;
191
192 return ret;
193}
194
f3a40945
AL
195static 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
340ae165
MM
212static int __ethtool_get_sset_count(struct net_device *dev, int sset)
213{
214 const struct ethtool_ops *ops = dev->ethtool_ops;
215
5455c699
MM
216 if (sset == ETH_SS_FEATURES)
217 return ARRAY_SIZE(netdev_features_strings);
218
892311f6
EP
219 if (sset == ETH_SS_RSS_HASH_FUNCS)
220 return ARRAY_SIZE(rss_hash_func_strings);
221
a4244b0c
HHZ
222 if (sset == ETH_SS_TUNABLES)
223 return ARRAY_SIZE(tunable_strings);
224
f3a40945
AL
225 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
c03a14e8 232 if (ops->get_sset_count && ops->get_strings)
340ae165
MM
233 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
5455c699
MM
243 if (stringset == ETH_SS_FEATURES)
244 memcpy(data, netdev_features_strings,
245 sizeof(netdev_features_strings));
892311f6
EP
246 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
247 memcpy(data, rss_hash_func_strings,
248 sizeof(rss_hash_func_strings));
a4244b0c
HHZ
249 else if (stringset == ETH_SS_TUNABLES)
250 memcpy(data, tunable_strings, sizeof(tunable_strings));
f3a40945
AL
251 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
5455c699
MM
262 /* ops->get_strings is valid because checked earlier */
263 ops->get_strings(dev, stringset, data);
340ae165
MM
264}
265
c8f44aff 266static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0a417704
MM
267{
268 /* feature masks of legacy discrete ethtool ops */
269
270 switch (eth_cmd) {
271 case ETHTOOL_GTXCSUM:
272 case ETHTOOL_STXCSUM:
a188222b 273 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
e83d360d
MM
274 case ETHTOOL_GRXCSUM:
275 case ETHTOOL_SRXCSUM:
276 return NETIF_F_RXCSUM;
0a417704
MM
277 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
0a417704
MM
297static int ethtool_get_one_feature(struct net_device *dev,
298 char __user *useraddr, u32 ethcmd)
299{
c8f44aff 300 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0a417704
MM
301 struct ethtool_value edata = {
302 .cmd = ethcmd,
86794881 303 .data = !!(dev->features & mask),
0a417704 304 };
0a417704 305
0a417704
MM
306 if (copy_to_user(useraddr, &edata, sizeof(edata)))
307 return -EFAULT;
308 return 0;
309}
310
0a417704
MM
311static int ethtool_set_one_feature(struct net_device *dev,
312 void __user *useraddr, u32 ethcmd)
313{
314 struct ethtool_value edata;
c8f44aff 315 netdev_features_t mask;
0a417704
MM
316
317 if (copy_from_user(&edata, useraddr, sizeof(edata)))
318 return -EFAULT;
319
86794881
MM
320 mask = ethtool_get_feature_mask(ethcmd);
321 mask &= dev->hw_features;
bc5787c6
MM
322 if (!mask)
323 return -EOPNOTSUPP;
86794881 324
bc5787c6
MM
325 if (edata.data)
326 dev->wanted_features |= mask;
327 else
328 dev->wanted_features &= ~mask;
86794881 329
bc5787c6 330 __netdev_update_features(dev);
86794881 331
bc5787c6
MM
332 return 0;
333}
334
02b3a552
MM
335#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
336 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
f646968f
PM
337#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)
bc5787c6
MM
340
341static u32 __ethtool_get_flags(struct net_device *dev)
342{
02b3a552
MM
343 u32 flags = 0;
344
8a73125c
DF
345 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;
02b3a552
MM
355
356 return flags;
0a417704
MM
357}
358
bc5787c6 359static int __ethtool_set_flags(struct net_device *dev, u32 data)
da8ac86c 360{
c8f44aff 361 netdev_features_t features = 0, changed;
da8ac86c 362
02b3a552 363 if (data & ~ETH_ALL_FLAGS)
da8ac86c
MM
364 return -EINVAL;
365
8a73125c
DF
366 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;
02b3a552 376
da8ac86c 377 /* allow changing only bits set in hw_features */
02b3a552 378 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
da8ac86c
MM
379 if (changed & ~dev->hw_features)
380 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
381
382 dev->wanted_features =
02b3a552 383 (dev->wanted_features & ~changed) | (features & changed);
da8ac86c 384
6cb6a27c 385 __netdev_update_features(dev);
da8ac86c
MM
386
387 return 0;
388}
389
4bc71cb9 390int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1da177e4 391{
4bc71cb9 392 ASSERT_RTNL();
1da177e4 393
c03a14e8 394 if (!dev->ethtool_ops->get_settings)
1da177e4
LT
395 return -EOPNOTSUPP;
396
4bc71cb9
JP
397 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
18ec898e 408 err = __ethtool_get_settings(dev, &cmd);
1da177e4
LT
409 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
97f8aefb 430static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
431 void __user *useraddr)
1da177e4
LT
432{
433 struct ethtool_drvinfo info;
76fd8593 434 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 435
1da177e4
LT
436 memset(&info, 0, sizeof(info));
437 info.cmd = ETHTOOL_GDRVINFO;
c03a14e8 438 if (ops->get_drvinfo) {
01414802
BH
439 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 }
1da177e4 448
723b2f57
JG
449 /*
450 * this method of obtaining string set info is deprecated;
d17792eb 451 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 452 */
c03a14e8 453 if (ops->get_sset_count) {
ff03d49f
JG
454 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;
339bf024
JG
462 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
463 if (rc >= 0)
464 info.n_priv_flags = rc;
ff03d49f 465 }
c03a14e8 466 if (ops->get_regs_len)
1da177e4 467 info.regdump_len = ops->get_regs_len(dev);
c03a14e8 468 if (ops->get_eeprom_len)
1da177e4
LT
469 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
f5c445ed 476static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 477 void __user *useraddr)
723b2f57
JG
478{
479 struct ethtool_sset_info info;
723b2f57
JG
480 u64 sset_mask;
481 int i, idx = 0, n_bits = 0, ret, rc;
482 u32 *info_buf = NULL;
483
723b2f57
JG
484 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 */
d17792eb 493 n_bits = hweight64(sset_mask);
723b2f57
JG
494
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
340ae165 510 rc = __ethtool_get_sset_count(dev, i);
723b2f57
JG
511 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
97f8aefb 532static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
bf988435 533 u32 cmd, void __user *useraddr)
0853ad66 534{
bf988435
BH
535 struct ethtool_rxnfc info;
536 size_t info_size = sizeof(info);
55664f32 537 int rc;
0853ad66 538
59089d8d 539 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
540 return -EOPNOTSUPP;
541
bf988435
BH
542 /* 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))
0853ad66
SB
551 return -EFAULT;
552
55664f32
BH
553 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;
0853ad66
SB
562}
563
97f8aefb 564static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
bf988435 565 u32 cmd, void __user *useraddr)
0853ad66
SB
566{
567 struct ethtool_rxnfc info;
bf988435 568 size_t info_size = sizeof(info);
59089d8d
SB
569 const struct ethtool_ops *ops = dev->ethtool_ops;
570 int ret;
571 void *rule_buf = NULL;
0853ad66 572
59089d8d 573 if (!ops->get_rxnfc)
0853ad66
SB
574 return -EOPNOTSUPP;
575
bf988435
BH
576 /* 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))
0853ad66
SB
585 return -EFAULT;
586
59089d8d
SB
587 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
588 if (info.rule_cnt > 0) {
db048b69 589 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
ae6df5f9 590 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
db048b69 591 GFP_USER);
59089d8d
SB
592 if (!rule_buf)
593 return -ENOMEM;
594 }
595 }
0853ad66 596
59089d8d
SB
597 ret = ops->get_rxnfc(dev, &info, rule_buf);
598 if (ret < 0)
599 goto err_out;
600
601 ret = -EFAULT;
bf988435 602 if (copy_to_user(useraddr, &info, info_size))
59089d8d
SB
603 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:
c9caceca 614 kfree(rule_buf);
59089d8d
SB
615
616 return ret;
0853ad66
SB
617}
618
3de0b592
VD
619static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
620 struct ethtool_rxnfc *rx_rings,
621 u32 size)
622{
fb95cd8d 623 int i;
3de0b592
VD
624
625 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
fb95cd8d 626 return -EFAULT;
3de0b592
VD
627
628 /* Validate ring indices */
fb95cd8d
BH
629 for (i = 0; i < size; i++)
630 if (indir[i] >= rx_rings->data)
631 return -EINVAL;
632
633 return 0;
3de0b592
VD
634}
635
ba905f5e 636u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
960fb622
ED
637
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
d4ab4286
KJ
646static 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
a5b6ee29
BH
677static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
678 void __user *useraddr)
679{
7850f63f
BH
680 u32 user_size, dev_size;
681 u32 *indir;
a5b6ee29
BH
682 int ret;
683
7850f63f 684 if (!dev->ethtool_ops->get_rxfh_indir_size ||
fe62d001 685 !dev->ethtool_ops->get_rxfh)
7850f63f
BH
686 return -EOPNOTSUPP;
687 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
688 if (dev_size == 0)
a5b6ee29
BH
689 return -EOPNOTSUPP;
690
7850f63f 691 if (copy_from_user(&user_size,
a5b6ee29 692 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 693 sizeof(user_size)))
a5b6ee29
BH
694 return -EFAULT;
695
7850f63f
BH
696 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);
a5b6ee29
BH
708 if (!indir)
709 return -ENOMEM;
710
892311f6 711 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
a5b6ee29
BH
712 if (ret)
713 goto out;
714
7850f63f
BH
715 if (copy_to_user(useraddr +
716 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
717 indir, dev_size * sizeof(indir[0])))
a5b6ee29
BH
718 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{
7850f63f
BH
728 struct ethtool_rxnfc rx_rings;
729 u32 user_size, dev_size, i;
730 u32 *indir;
c03a14e8 731 const struct ethtool_ops *ops = dev->ethtool_ops;
a5b6ee29 732 int ret;
3de0b592 733 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
a5b6ee29 734
fe62d001 735 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
c03a14e8 736 !ops->get_rxnfc)
7850f63f 737 return -EOPNOTSUPP;
c03a14e8
JP
738
739 dev_size = ops->get_rxfh_indir_size(dev);
7850f63f 740 if (dev_size == 0)
a5b6ee29
BH
741 return -EOPNOTSUPP;
742
7850f63f 743 if (copy_from_user(&user_size,
a5b6ee29 744 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 745 sizeof(user_size)))
a5b6ee29
BH
746 return -EFAULT;
747
278bc429 748 if (user_size != 0 && user_size != dev_size)
7850f63f
BH
749 return -EINVAL;
750
751 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
752 if (!indir)
753 return -ENOMEM;
754
7850f63f 755 rx_rings.cmd = ETHTOOL_GRXRINGS;
c03a14e8 756 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
7850f63f
BH
757 if (ret)
758 goto out;
278bc429
BH
759
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 {
3de0b592
VD
764 ret = ethtool_copy_validate_indir(indir,
765 useraddr + ringidx_offset,
766 &rx_rings,
767 dev_size);
768 if (ret)
769 goto out;
770 }
771
892311f6 772 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
d4ab4286
KJ
773 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;
3de0b592
VD
781
782out:
783 kfree(indir);
784 return ret;
785}
786
787static 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;
f062a384 792 u32 user_indir_size, user_key_size;
3de0b592 793 u32 dev_indir_size = 0, dev_key_size = 0;
f062a384 794 struct ethtool_rxfh rxfh;
3de0b592 795 u32 total_size;
f062a384 796 u32 indir_bytes;
3de0b592 797 u32 *indir = NULL;
892311f6 798 u8 dev_hfunc = 0;
3de0b592
VD
799 u8 *hkey = NULL;
800 u8 *rss_config;
801
892311f6 802 if (!ops->get_rxfh)
3de0b592
VD
803 return -EOPNOTSUPP;
804
805 if (ops->get_rxfh_indir_size)
806 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592
VD
807 if (ops->get_rxfh_key_size)
808 dev_key_size = ops->get_rxfh_key_size(dev);
809
f062a384 810 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592 811 return -EFAULT;
f062a384
BH
812 user_indir_size = rxfh.indir_size;
813 user_key_size = rxfh.key_size;
3de0b592 814
f062a384 815 /* Check that reserved fields are 0 for now */
892311f6
EP
816 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
817 rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384
BH
818 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)))
3de0b592
VD
823 return -EFAULT;
824
3de0b592
VD
825 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
892311f6
EP
841 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
842 if (ret)
843 goto out;
3de0b592 844
892311f6
EP
845 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:
3de0b592
VD
854 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;
f062a384
BH
865 struct ethtool_rxfh rxfh;
866 u32 dev_indir_size = 0, dev_key_size = 0, i;
3de0b592
VD
867 u32 *indir = NULL, indir_bytes = 0;
868 u8 *hkey = NULL;
869 u8 *rss_config;
3de0b592
VD
870 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
871
892311f6 872 if (!ops->get_rxnfc || !ops->set_rxfh)
3de0b592
VD
873 return -EOPNOTSUPP;
874
875 if (ops->get_rxfh_indir_size)
876 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592 877 if (ops->get_rxfh_key_size)
d340c862 878 dev_key_size = ops->get_rxfh_key_size(dev);
3de0b592 879
f062a384 880 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592
VD
881 return -EFAULT;
882
f062a384 883 /* Check that reserved fields are 0 for now */
892311f6
EP
884 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
885 rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384
BH
886 return -EINVAL;
887
892311f6
EP
888 /* If either indir, hash key or function is valid, proceed further.
889 * Must request at least one change: indir size, hash key or function.
3de0b592 890 */
f062a384
BH
891 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 &&
892311f6 896 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
3de0b592
VD
897 return -EINVAL;
898
f062a384 899 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
3de0b592
VD
900 indir_bytes = dev_indir_size * sizeof(indir[0]);
901
f062a384 902 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
3de0b592
VD
903 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
f062a384
BH
911 /* rxfh.indir_size == 0 means reset the indir table to default.
912 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
3de0b592 913 */
f062a384
BH
914 if (rxfh.indir_size &&
915 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
3de0b592
VD
916 indir = (u32 *)rss_config;
917 ret = ethtool_copy_validate_indir(indir,
918 useraddr + rss_cfg_offset,
919 &rx_rings,
f062a384 920 rxfh.indir_size);
3de0b592 921 if (ret)
7850f63f 922 goto out;
f062a384 923 } else if (rxfh.indir_size == 0) {
3de0b592
VD
924 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 }
278bc429 928
f062a384 929 if (rxfh.key_size) {
3de0b592
VD
930 hkey = rss_config + indir_bytes;
931 if (copy_from_user(hkey,
932 useraddr + rss_cfg_offset + indir_bytes,
f062a384 933 rxfh.key_size)) {
3de0b592
VD
934 ret = -EFAULT;
935 goto out;
278bc429 936 }
7850f63f
BH
937 }
938
892311f6 939 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
d4ab4286
KJ
940 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;
a5b6ee29
BH
948
949out:
3de0b592 950 kfree(rss_config);
a5b6ee29
BH
951 return ret;
952}
953
1da177e4
LT
954static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
955{
956 struct ethtool_regs regs;
76fd8593 957 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
958 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
b7c7d01a 971 regbuf = vzalloc(reglen);
67ae7cf1 972 if (reglen && !regbuf)
1da177e4
LT
973 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);
67ae7cf1 981 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1da177e4
LT
982 goto out;
983 ret = 0;
984
985 out:
a77f5db3 986 vfree(regbuf);
1da177e4
LT
987 return ret;
988}
989
d73d3a8c
BH
990static 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
1da177e4
LT
1010static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1011{
8e557421 1012 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1da177e4
LT
1013
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
80f12ecc
YM
1037static 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
1da177e4
LT
1071static 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
e596e6e4
BH
1079static 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
081d094e
BH
1093static 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)
1da177e4
LT
1097{
1098 struct ethtool_eeprom eeprom;
b131dd5d
MSB
1099 void __user *userbuf = useraddr + sizeof(eeprom);
1100 u32 bytes_remaining;
1da177e4 1101 u8 *data;
b131dd5d 1102 int ret = 0;
1da177e4 1103
1da177e4
LT
1104 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 */
081d094e 1112 if (eeprom.offset + eeprom.len > total_len)
1da177e4
LT
1113 return -EINVAL;
1114
b131dd5d 1115 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1116 if (!data)
1117 return -ENOMEM;
1118
b131dd5d
MSB
1119 bytes_remaining = eeprom.len;
1120 while (bytes_remaining > 0) {
1121 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1122
081d094e 1123 ret = getter(dev, &eeprom, data);
b131dd5d
MSB
1124 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 }
1da177e4 1134
c5835df9
MSB
1135 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1136 eeprom.offset -= eeprom.len;
1137 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1138 ret = -EFAULT;
1139
1da177e4
LT
1140 kfree(data);
1141 return ret;
1142}
1143
081d094e
BH
1144static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1145{
1146 const struct ethtool_ops *ops = dev->ethtool_ops;
1147
e0fb6fb6
GR
1148 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1149 !ops->get_eeprom_len(dev))
081d094e
BH
1150 return -EOPNOTSUPP;
1151
1152 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1153 ops->get_eeprom_len(dev));
1154}
1155
1da177e4
LT
1156static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1157{
1158 struct ethtool_eeprom eeprom;
76fd8593 1159 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
1160 void __user *userbuf = useraddr + sizeof(eeprom);
1161 u32 bytes_remaining;
1da177e4 1162 u8 *data;
b131dd5d 1163 int ret = 0;
1da177e4 1164
e0fb6fb6
GR
1165 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1166 !ops->get_eeprom_len(dev))
1da177e4
LT
1167 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
b131dd5d 1180 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1181 if (!data)
1182 return -ENOMEM;
1183
b131dd5d
MSB
1184 bytes_remaining = eeprom.len;
1185 while (bytes_remaining > 0) {
1186 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1187
1188 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 }
1da177e4 1199
1da177e4
LT
1200 kfree(data);
1201 return ret;
1202}
1203
97f8aefb 1204static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1205 void __user *useraddr)
1da177e4 1206{
8e557421 1207 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
1208
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
97f8aefb 1219static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1220 void __user *useraddr)
1da177e4
LT
1221{
1222 struct ethtool_coalesce coalesce;
1223
fa04ae5c 1224 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
1225 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{
8e557421 1235 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
1236
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
8b5933c3 1260static 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{
8bf36862 1278 struct ethtool_channels channels, max;
d4ab4286 1279 u32 max_rx_in_use = 0;
8b5933c3 1280
8bf36862 1281 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
8b5933c3 1282 return -EOPNOTSUPP;
1283
1284 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1285 return -EFAULT;
1286
8bf36862
KJ
1287 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
d4ab4286
KJ
1296 /* 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
8b5933c3 1303 return dev->ethtool_ops->set_channels(dev, &channels);
1304}
1305
1da177e4
LT
1306static 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
e1b90c41 1324 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
1325 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
1da177e4
LT
1333static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1334{
1335 struct ethtool_test test;
76fd8593 1336 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1337 u64 *data;
ff03d49f 1338 int ret, test_len;
1da177e4 1339
a9828ec6 1340 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
1341 return -EOPNOTSUPP;
1342
a9828ec6 1343 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
1344 if (test_len < 0)
1345 return test_len;
1346 WARN_ON(test_len == 0);
1347
1da177e4
LT
1348 if (copy_from_user(&test, useraddr, sizeof(test)))
1349 return -EFAULT;
1350
ff03d49f
JG
1351 test.len = test_len;
1352 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
1353 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;
1da177e4
LT
1374 u8 *data;
1375 int ret;
1376
1da177e4
LT
1377 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1378 return -EFAULT;
1379
340ae165 1380 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
1381 if (ret < 0)
1382 return ret;
1383
1384 gstrings.len = ret;
1da177e4 1385
077cb37f 1386 data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
1da177e4
LT
1387 if (!data)
1388 return -ENOMEM;
1389
340ae165 1390 __ethtool_get_strings(dev, gstrings.string_set, data);
1da177e4
LT
1391
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
340ae165 1400out:
1da177e4
LT
1401 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;
68f512f2 1408 static bool busy;
c03a14e8 1409 const struct ethtool_ops *ops = dev->ethtool_ops;
68f512f2 1410 int rc;
1da177e4 1411
c03a14e8 1412 if (!ops->set_phys_id)
1da177e4
LT
1413 return -EOPNOTSUPP;
1414
68f512f2
BH
1415 if (busy)
1416 return -EBUSY;
1417
1da177e4
LT
1418 if (copy_from_user(&id, useraddr, sizeof(id)))
1419 return -EFAULT;
1420
c03a14e8 1421 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 1422 if (rc < 0)
68f512f2
BH
1423 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(
143780c6 1435 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 1436 } else {
fce55922
AB
1437 /* Driver expects to be called at twice the frequency in rc */
1438 int n = rc * 2, i, interval = HZ / n;
1439
1440 /* Count down seconds */
68f512f2 1441 do {
fce55922
AB
1442 /* Count down iterations per second */
1443 i = n;
1444 do {
1445 rtnl_lock();
c03a14e8 1446 rc = ops->set_phys_id(dev,
fce55922
AB
1447 (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);
68f512f2
BH
1453 } while (!signal_pending(current) &&
1454 (id.data == 0 || --id.data != 0));
1455 }
1456
1457 rtnl_lock();
1458 dev_put(dev);
1459 busy = false;
1460
c03a14e8 1461 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
68f512f2 1462 return rc;
1da177e4
LT
1463}
1464
1465static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1466{
1467 struct ethtool_stats stats;
76fd8593 1468 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1469 u64 *data;
ff03d49f 1470 int ret, n_stats;
1da177e4 1471
a9828ec6 1472 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1473 return -EOPNOTSUPP;
1474
a9828ec6 1475 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1476 if (n_stats < 0)
1477 return n_stats;
1478 WARN_ON(n_stats == 0);
1479
1da177e4
LT
1480 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1481 return -EFAULT;
1482
ff03d49f
JG
1483 stats.n_stats = n_stats;
1484 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1da177e4
LT
1485 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
f3a40945
AL
1503static 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
0bf0519d 1544static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
1545{
1546 struct ethtool_perm_addr epaddr;
a6f9a705 1547
313674af 1548 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
1549 return -EFAULT;
1550
313674af
MW
1551 if (epaddr.size < dev->addr_len)
1552 return -ETOOSMALL;
1553 epaddr.size = dev->addr_len;
a6f9a705 1554
a6f9a705 1555 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 1556 return -EFAULT;
a6f9a705 1557 useraddr += sizeof(epaddr);
313674af
MW
1558 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1559 return -EFAULT;
1560 return 0;
a6f9a705
JW
1561}
1562
13c99b24
JG
1563static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1564 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 1565{
8e557421 1566 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 1567
13c99b24 1568 if (!actor)
3ae7c0b2
JG
1569 return -EOPNOTSUPP;
1570
13c99b24 1571 edata.data = actor(dev);
3ae7c0b2
JG
1572
1573 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1574 return -EFAULT;
1575 return 0;
1576}
1577
13c99b24
JG
1578static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1579 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
1580{
1581 struct ethtool_value edata;
1582
13c99b24 1583 if (!actor)
3ae7c0b2
JG
1584 return -EOPNOTSUPP;
1585
1586 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1587 return -EFAULT;
1588
13c99b24 1589 actor(dev, edata.data);
339bf024
JG
1590 return 0;
1591}
1592
13c99b24
JG
1593static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1594 int (*actor)(struct net_device *, u32))
339bf024
JG
1595{
1596 struct ethtool_value edata;
1597
13c99b24 1598 if (!actor)
339bf024
JG
1599 return -EOPNOTSUPP;
1600
1601 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1602 return -EFAULT;
1603
13c99b24 1604 return actor(dev, edata.data);
339bf024
JG
1605}
1606
97f8aefb 1607static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1608 char __user *useraddr)
05c6a8d7
AK
1609{
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
786f5281
BH
1618 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1619
05c6a8d7
AK
1620 return dev->ethtool_ops->flash_device(dev, &efl);
1621}
1622
29dd54b7
AC
1623static 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
c03a14e8 1644 if (!ops->get_dump_flag)
29dd54b7
AC
1645 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
c03a14e8 1668 if (!ops->get_dump_data || !ops->get_dump_flag)
29dd54b7
AC
1669 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
c590b5e2 1680 len = min(tmp.len, dump.len);
29dd54b7
AC
1681 if (!len)
1682 return -EFAULT;
1683
c590b5e2
MS
1684 /* 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 */
29dd54b7
AC
1693 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
c590b5e2
MS
1700 /* 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
29dd54b7
AC
1710 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
c8f3a8c3
RC
1722static 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) {
c8f3a8c3 1733 err = phydev->drv->ts_info(phydev, &info);
c03a14e8 1734 } else if (ops->get_ts_info) {
c8f3a8c3 1735 err = ops->get_ts_info(dev, &info);
c8f3a8c3
RC
1736 } 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
2f438366
ES
1752static 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
41c3cb6d
SH
1767static int ethtool_get_module_info(struct net_device *dev,
1768 void __user *useraddr)
1769{
1770 int ret;
1771 struct ethtool_modinfo modinfo;
41c3cb6d
SH
1772
1773 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1774 return -EFAULT;
1775
2f438366 1776 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
1777 if (ret)
1778 return ret;
1779
1780 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1781 return -EFAULT;
1782
1783 return 0;
1784}
1785
2f438366
ES
1786static 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
41c3cb6d
SH
1801static int ethtool_get_module_eeprom(struct net_device *dev,
1802 void __user *useraddr)
1803{
1804 int ret;
1805 struct ethtool_modinfo modinfo;
41c3cb6d 1806
2f438366 1807 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
1808 if (ret)
1809 return ret;
1810
2f438366
ES
1811 return ethtool_get_any_eeprom(dev, useraddr,
1812 __ethtool_get_module_eeprom,
41c3cb6d
SH
1813 modinfo.eeprom_len);
1814}
1815
f0db9b07
GV
1816static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
1817{
1818 switch (tuna->id) {
1819 case ETHTOOL_RX_COPYBREAK:
1255a505 1820 case ETHTOOL_TX_COPYBREAK:
f0db9b07
GV
1821 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
421797b1
KL
1891static int ethtool_get_per_queue_coalesce(struct net_device *dev,
1892 void __user *useraddr,
1893 struct ethtool_per_queue_op *per_queue_opt)
1894{
1895 u32 bit;
1896 int ret;
1897 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
1898
1899 if (!dev->ethtool_ops->get_per_queue_coalesce)
1900 return -EOPNOTSUPP;
1901
1902 useraddr += sizeof(*per_queue_opt);
1903
1904 bitmap_from_u32array(queue_mask,
1905 MAX_NUM_QUEUE,
1906 per_queue_opt->queue_mask,
1907 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
1908
1909 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
1910 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1911
1912 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
1913 if (ret != 0)
1914 return ret;
1915 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1916 return -EFAULT;
1917 useraddr += sizeof(coalesce);
1918 }
1919
1920 return 0;
1921}
1922
ac2c7ad0
KL
1923static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
1924{
1925 struct ethtool_per_queue_op per_queue_opt;
1926
1927 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
1928 return -EFAULT;
1929
1930 switch (per_queue_opt.sub_command) {
421797b1
KL
1931 case ETHTOOL_GCOALESCE:
1932 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
ac2c7ad0
KL
1933 default:
1934 return -EOPNOTSUPP;
1935 };
1936}
1937
600fed5e 1938/* The main entry point in this file. Called from net/core/dev_ioctl.c */
1da177e4 1939
881d966b 1940int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 1941{
881d966b 1942 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4 1943 void __user *useraddr = ifr->ifr_data;
ac2c7ad0 1944 u32 ethcmd, sub_cmd;
1da177e4 1945 int rc;
b29d3145 1946 netdev_features_t old_features;
1da177e4 1947
1da177e4
LT
1948 if (!dev || !netif_device_present(dev))
1949 return -ENODEV;
1950
97f8aefb 1951 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
1952 return -EFAULT;
1953
ac2c7ad0
KL
1954 if (ethcmd == ETHTOOL_PERQUEUE) {
1955 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
1956 return -EFAULT;
1957 } else {
1958 sub_cmd = ethcmd;
1959 }
75f3123c 1960 /* Allow some commands to be done by anyone */
ac2c7ad0 1961 switch (sub_cmd) {
0fdc100b 1962 case ETHTOOL_GSET:
75f3123c 1963 case ETHTOOL_GDRVINFO:
75f3123c 1964 case ETHTOOL_GMSGLVL:
2da45db2 1965 case ETHTOOL_GLINK:
75f3123c
SH
1966 case ETHTOOL_GCOALESCE:
1967 case ETHTOOL_GRINGPARAM:
1968 case ETHTOOL_GPAUSEPARAM:
1969 case ETHTOOL_GRXCSUM:
1970 case ETHTOOL_GTXCSUM:
1971 case ETHTOOL_GSG:
f80400a2 1972 case ETHTOOL_GSSET_INFO:
75f3123c 1973 case ETHTOOL_GSTRINGS:
2da45db2 1974 case ETHTOOL_GSTATS:
f3a40945 1975 case ETHTOOL_GPHYSTATS:
75f3123c
SH
1976 case ETHTOOL_GTSO:
1977 case ETHTOOL_GPERMADDR:
1978 case ETHTOOL_GUFO:
1979 case ETHTOOL_GGSO:
1cab819b 1980 case ETHTOOL_GGRO:
339bf024
JG
1981 case ETHTOOL_GFLAGS:
1982 case ETHTOOL_GPFLAGS:
0853ad66 1983 case ETHTOOL_GRXFH:
59089d8d
SB
1984 case ETHTOOL_GRXRINGS:
1985 case ETHTOOL_GRXCLSRLCNT:
1986 case ETHTOOL_GRXCLSRULE:
1987 case ETHTOOL_GRXCLSRLALL:
2da45db2 1988 case ETHTOOL_GRXFHINDIR:
3de0b592 1989 case ETHTOOL_GRSSH:
5455c699 1990 case ETHTOOL_GFEATURES:
2da45db2 1991 case ETHTOOL_GCHANNELS:
c8f3a8c3 1992 case ETHTOOL_GET_TS_INFO:
2da45db2 1993 case ETHTOOL_GEEE:
f0db9b07 1994 case ETHTOOL_GTUNABLE:
75f3123c
SH
1995 break;
1996 default:
5e1fccc0 1997 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
75f3123c
SH
1998 return -EPERM;
1999 }
2000
97f8aefb 2001 if (dev->ethtool_ops->begin) {
2002 rc = dev->ethtool_ops->begin(dev);
2003 if (rc < 0)
1da177e4 2004 return rc;
97f8aefb 2005 }
d8a33ac4
SH
2006 old_features = dev->features;
2007
1da177e4
LT
2008 switch (ethcmd) {
2009 case ETHTOOL_GSET:
2010 rc = ethtool_get_settings(dev, useraddr);
2011 break;
2012 case ETHTOOL_SSET:
2013 rc = ethtool_set_settings(dev, useraddr);
2014 break;
2015 case ETHTOOL_GDRVINFO:
2016 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
2017 break;
2018 case ETHTOOL_GREGS:
2019 rc = ethtool_get_regs(dev, useraddr);
2020 break;
2021 case ETHTOOL_GWOL:
2022 rc = ethtool_get_wol(dev, useraddr);
2023 break;
2024 case ETHTOOL_SWOL:
2025 rc = ethtool_set_wol(dev, useraddr);
2026 break;
2027 case ETHTOOL_GMSGLVL:
13c99b24
JG
2028 rc = ethtool_get_value(dev, useraddr, ethcmd,
2029 dev->ethtool_ops->get_msglevel);
1da177e4
LT
2030 break;
2031 case ETHTOOL_SMSGLVL:
13c99b24
JG
2032 rc = ethtool_set_value_void(dev, useraddr,
2033 dev->ethtool_ops->set_msglevel);
1da177e4 2034 break;
80f12ecc
YM
2035 case ETHTOOL_GEEE:
2036 rc = ethtool_get_eee(dev, useraddr);
2037 break;
2038 case ETHTOOL_SEEE:
2039 rc = ethtool_set_eee(dev, useraddr);
2040 break;
1da177e4
LT
2041 case ETHTOOL_NWAY_RST:
2042 rc = ethtool_nway_reset(dev);
2043 break;
2044 case ETHTOOL_GLINK:
e596e6e4 2045 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
2046 break;
2047 case ETHTOOL_GEEPROM:
2048 rc = ethtool_get_eeprom(dev, useraddr);
2049 break;
2050 case ETHTOOL_SEEPROM:
2051 rc = ethtool_set_eeprom(dev, useraddr);
2052 break;
2053 case ETHTOOL_GCOALESCE:
2054 rc = ethtool_get_coalesce(dev, useraddr);
2055 break;
2056 case ETHTOOL_SCOALESCE:
2057 rc = ethtool_set_coalesce(dev, useraddr);
2058 break;
2059 case ETHTOOL_GRINGPARAM:
2060 rc = ethtool_get_ringparam(dev, useraddr);
2061 break;
2062 case ETHTOOL_SRINGPARAM:
2063 rc = ethtool_set_ringparam(dev, useraddr);
2064 break;
2065 case ETHTOOL_GPAUSEPARAM:
2066 rc = ethtool_get_pauseparam(dev, useraddr);
2067 break;
2068 case ETHTOOL_SPAUSEPARAM:
2069 rc = ethtool_set_pauseparam(dev, useraddr);
2070 break;
1da177e4
LT
2071 case ETHTOOL_TEST:
2072 rc = ethtool_self_test(dev, useraddr);
2073 break;
2074 case ETHTOOL_GSTRINGS:
2075 rc = ethtool_get_strings(dev, useraddr);
2076 break;
2077 case ETHTOOL_PHYS_ID:
2078 rc = ethtool_phys_id(dev, useraddr);
2079 break;
2080 case ETHTOOL_GSTATS:
2081 rc = ethtool_get_stats(dev, useraddr);
2082 break;
a6f9a705
JW
2083 case ETHTOOL_GPERMADDR:
2084 rc = ethtool_get_perm_addr(dev, useraddr);
2085 break;
3ae7c0b2 2086 case ETHTOOL_GFLAGS:
13c99b24 2087 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 2088 __ethtool_get_flags);
3ae7c0b2
JG
2089 break;
2090 case ETHTOOL_SFLAGS:
da8ac86c 2091 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 2092 break;
339bf024 2093 case ETHTOOL_GPFLAGS:
13c99b24
JG
2094 rc = ethtool_get_value(dev, useraddr, ethcmd,
2095 dev->ethtool_ops->get_priv_flags);
339bf024
JG
2096 break;
2097 case ETHTOOL_SPFLAGS:
13c99b24
JG
2098 rc = ethtool_set_value(dev, useraddr,
2099 dev->ethtool_ops->set_priv_flags);
339bf024 2100 break;
0853ad66 2101 case ETHTOOL_GRXFH:
59089d8d
SB
2102 case ETHTOOL_GRXRINGS:
2103 case ETHTOOL_GRXCLSRLCNT:
2104 case ETHTOOL_GRXCLSRULE:
2105 case ETHTOOL_GRXCLSRLALL:
bf988435 2106 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
2107 break;
2108 case ETHTOOL_SRXFH:
59089d8d
SB
2109 case ETHTOOL_SRXCLSRLDEL:
2110 case ETHTOOL_SRXCLSRLINS:
bf988435 2111 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 2112 break;
05c6a8d7
AK
2113 case ETHTOOL_FLASHDEV:
2114 rc = ethtool_flash_device(dev, useraddr);
2115 break;
d73d3a8c
BH
2116 case ETHTOOL_RESET:
2117 rc = ethtool_reset(dev, useraddr);
2118 break;
723b2f57
JG
2119 case ETHTOOL_GSSET_INFO:
2120 rc = ethtool_get_sset_info(dev, useraddr);
2121 break;
a5b6ee29
BH
2122 case ETHTOOL_GRXFHINDIR:
2123 rc = ethtool_get_rxfh_indir(dev, useraddr);
2124 break;
2125 case ETHTOOL_SRXFHINDIR:
2126 rc = ethtool_set_rxfh_indir(dev, useraddr);
2127 break;
3de0b592
VD
2128 case ETHTOOL_GRSSH:
2129 rc = ethtool_get_rxfh(dev, useraddr);
2130 break;
2131 case ETHTOOL_SRSSH:
2132 rc = ethtool_set_rxfh(dev, useraddr);
2133 break;
5455c699
MM
2134 case ETHTOOL_GFEATURES:
2135 rc = ethtool_get_features(dev, useraddr);
2136 break;
2137 case ETHTOOL_SFEATURES:
2138 rc = ethtool_set_features(dev, useraddr);
2139 break;
0a417704 2140 case ETHTOOL_GTXCSUM:
e83d360d 2141 case ETHTOOL_GRXCSUM:
0a417704
MM
2142 case ETHTOOL_GSG:
2143 case ETHTOOL_GTSO:
2144 case ETHTOOL_GUFO:
2145 case ETHTOOL_GGSO:
2146 case ETHTOOL_GGRO:
2147 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2148 break;
2149 case ETHTOOL_STXCSUM:
e83d360d 2150 case ETHTOOL_SRXCSUM:
0a417704
MM
2151 case ETHTOOL_SSG:
2152 case ETHTOOL_STSO:
2153 case ETHTOOL_SUFO:
2154 case ETHTOOL_SGSO:
2155 case ETHTOOL_SGRO:
2156 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2157 break;
8b5933c3 2158 case ETHTOOL_GCHANNELS:
2159 rc = ethtool_get_channels(dev, useraddr);
2160 break;
2161 case ETHTOOL_SCHANNELS:
2162 rc = ethtool_set_channels(dev, useraddr);
2163 break;
29dd54b7
AC
2164 case ETHTOOL_SET_DUMP:
2165 rc = ethtool_set_dump(dev, useraddr);
2166 break;
2167 case ETHTOOL_GET_DUMP_FLAG:
2168 rc = ethtool_get_dump_flag(dev, useraddr);
2169 break;
2170 case ETHTOOL_GET_DUMP_DATA:
2171 rc = ethtool_get_dump_data(dev, useraddr);
2172 break;
c8f3a8c3
RC
2173 case ETHTOOL_GET_TS_INFO:
2174 rc = ethtool_get_ts_info(dev, useraddr);
2175 break;
41c3cb6d
SH
2176 case ETHTOOL_GMODULEINFO:
2177 rc = ethtool_get_module_info(dev, useraddr);
2178 break;
2179 case ETHTOOL_GMODULEEEPROM:
2180 rc = ethtool_get_module_eeprom(dev, useraddr);
2181 break;
f0db9b07
GV
2182 case ETHTOOL_GTUNABLE:
2183 rc = ethtool_get_tunable(dev, useraddr);
2184 break;
2185 case ETHTOOL_STUNABLE:
2186 rc = ethtool_set_tunable(dev, useraddr);
2187 break;
f3a40945
AL
2188 case ETHTOOL_GPHYSTATS:
2189 rc = ethtool_get_phy_stats(dev, useraddr);
2190 break;
ac2c7ad0
KL
2191 case ETHTOOL_PERQUEUE:
2192 rc = ethtool_set_per_queue(dev, useraddr);
2193 break;
1da177e4 2194 default:
61a44b9c 2195 rc = -EOPNOTSUPP;
1da177e4 2196 }
4ec93edb 2197
e71a4783 2198 if (dev->ethtool_ops->complete)
1da177e4 2199 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
2200
2201 if (old_features != dev->features)
2202 netdev_features_change(dev);
2203
1da177e4 2204 return rc;
1da177e4 2205}