bluetooth: hci: Fix type of "enable_hs" to bool.
[linux-2.6-block.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>
d17792eb 20#include <linux/bitops.h>
97f8aefb 21#include <linux/uaccess.h>
73da16c2 22#include <linux/vmalloc.h>
5a0e3ad6 23#include <linux/slab.h>
68f512f2
BH
24#include <linux/rtnetlink.h>
25#include <linux/sched.h>
1da177e4 26
4ec93edb 27/*
1da177e4
LT
28 * Some useful ethtool_ops methods that're device independent.
29 * If we find that all drivers want to do the same thing here,
30 * we can turn these into dev_() function calls.
31 */
32
33u32 ethtool_op_get_link(struct net_device *dev)
34{
35 return netif_carrier_ok(dev) ? 1 : 0;
36}
97f8aefb 37EXPORT_SYMBOL(ethtool_op_get_link);
1da177e4 38
1da177e4
LT
39/* Handlers for each ethtool command */
40
475414f6 41#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
5455c699 42
9d921549
MM
43static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
44 [NETIF_F_SG_BIT] = "tx-scatter-gather",
45 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
9d921549
MM
46 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
47 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
48 [NETIF_F_HIGHDMA_BIT] = "highdma",
49 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
50 [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
51
52 [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
53 [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
54 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
55 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
56 [NETIF_F_LLTX_BIT] = "tx-lockless",
57 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
58 [NETIF_F_GRO_BIT] = "rx-gro",
59 [NETIF_F_LRO_BIT] = "rx-lro",
60
61 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
62 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
63 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
64 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
65 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
66 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
67
68 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
69 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
70 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
71 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
72 [NETIF_F_RXHASH_BIT] = "rx-hashing",
73 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
74 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
75 [NETIF_F_LOOPBACK_BIT] = "loopback",
76};
77
5455c699
MM
78static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
79{
80 struct ethtool_gfeatures cmd = {
81 .cmd = ETHTOOL_GFEATURES,
82 .size = ETHTOOL_DEV_FEATURE_WORDS,
83 };
475414f6 84 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
5455c699
MM
85 u32 __user *sizeaddr;
86 u32 copy_size;
475414f6
MM
87 int i;
88
89 /* in case feature bits run out again */
09da71b1 90 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
475414f6
MM
91
92 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
93 features[i].available = (u32)(dev->hw_features >> (32 * i));
94 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
95 features[i].active = (u32)(dev->features >> (32 * i));
96 features[i].never_changed =
97 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
475414f6 98 }
5455c699
MM
99
100 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
101 if (get_user(copy_size, sizeaddr))
102 return -EFAULT;
103
104 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
105 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
106
107 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
108 return -EFAULT;
109 useraddr += sizeof(cmd);
110 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
111 return -EFAULT;
112
113 return 0;
114}
115
116static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
117{
118 struct ethtool_sfeatures cmd;
119 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
475414f6
MM
120 netdev_features_t wanted = 0, valid = 0;
121 int i, ret = 0;
5455c699
MM
122
123 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
124 return -EFAULT;
125 useraddr += sizeof(cmd);
126
127 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
128 return -EINVAL;
129
130 if (copy_from_user(features, useraddr, sizeof(features)))
131 return -EFAULT;
132
475414f6 133 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
134 valid |= (netdev_features_t)features[i].valid << (32 * i);
135 wanted |= (netdev_features_t)features[i].requested << (32 * i);
475414f6
MM
136 }
137
138 if (valid & ~NETIF_F_ETHTOOL_BITS)
5455c699
MM
139 return -EINVAL;
140
475414f6
MM
141 if (valid & ~dev->hw_features) {
142 valid &= dev->hw_features;
5455c699
MM
143 ret |= ETHTOOL_F_UNSUPPORTED;
144 }
145
475414f6
MM
146 dev->wanted_features &= ~valid;
147 dev->wanted_features |= wanted & valid;
6cb6a27c 148 __netdev_update_features(dev);
5455c699 149
475414f6 150 if ((dev->wanted_features ^ dev->features) & valid)
5455c699
MM
151 ret |= ETHTOOL_F_WISH;
152
153 return ret;
154}
155
340ae165
MM
156static int __ethtool_get_sset_count(struct net_device *dev, int sset)
157{
158 const struct ethtool_ops *ops = dev->ethtool_ops;
159
5455c699
MM
160 if (sset == ETH_SS_FEATURES)
161 return ARRAY_SIZE(netdev_features_strings);
162
340ae165
MM
163 if (ops && ops->get_sset_count && ops->get_strings)
164 return ops->get_sset_count(dev, sset);
165 else
166 return -EOPNOTSUPP;
167}
168
169static void __ethtool_get_strings(struct net_device *dev,
170 u32 stringset, u8 *data)
171{
172 const struct ethtool_ops *ops = dev->ethtool_ops;
173
5455c699
MM
174 if (stringset == ETH_SS_FEATURES)
175 memcpy(data, netdev_features_strings,
176 sizeof(netdev_features_strings));
177 else
178 /* ops->get_strings is valid because checked earlier */
179 ops->get_strings(dev, stringset, data);
340ae165
MM
180}
181
c8f44aff 182static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0a417704
MM
183{
184 /* feature masks of legacy discrete ethtool ops */
185
186 switch (eth_cmd) {
187 case ETHTOOL_GTXCSUM:
188 case ETHTOOL_STXCSUM:
189 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
e83d360d
MM
190 case ETHTOOL_GRXCSUM:
191 case ETHTOOL_SRXCSUM:
192 return NETIF_F_RXCSUM;
0a417704
MM
193 case ETHTOOL_GSG:
194 case ETHTOOL_SSG:
195 return NETIF_F_SG;
196 case ETHTOOL_GTSO:
197 case ETHTOOL_STSO:
198 return NETIF_F_ALL_TSO;
199 case ETHTOOL_GUFO:
200 case ETHTOOL_SUFO:
201 return NETIF_F_UFO;
202 case ETHTOOL_GGSO:
203 case ETHTOOL_SGSO:
204 return NETIF_F_GSO;
205 case ETHTOOL_GGRO:
206 case ETHTOOL_SGRO:
207 return NETIF_F_GRO;
208 default:
209 BUG();
210 }
211}
212
0a417704
MM
213static int ethtool_get_one_feature(struct net_device *dev,
214 char __user *useraddr, u32 ethcmd)
215{
c8f44aff 216 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0a417704
MM
217 struct ethtool_value edata = {
218 .cmd = ethcmd,
86794881 219 .data = !!(dev->features & mask),
0a417704 220 };
0a417704 221
0a417704
MM
222 if (copy_to_user(useraddr, &edata, sizeof(edata)))
223 return -EFAULT;
224 return 0;
225}
226
0a417704
MM
227static int ethtool_set_one_feature(struct net_device *dev,
228 void __user *useraddr, u32 ethcmd)
229{
230 struct ethtool_value edata;
c8f44aff 231 netdev_features_t mask;
0a417704
MM
232
233 if (copy_from_user(&edata, useraddr, sizeof(edata)))
234 return -EFAULT;
235
86794881
MM
236 mask = ethtool_get_feature_mask(ethcmd);
237 mask &= dev->hw_features;
bc5787c6
MM
238 if (!mask)
239 return -EOPNOTSUPP;
86794881 240
bc5787c6
MM
241 if (edata.data)
242 dev->wanted_features |= mask;
243 else
244 dev->wanted_features &= ~mask;
86794881 245
bc5787c6 246 __netdev_update_features(dev);
86794881 247
bc5787c6
MM
248 return 0;
249}
250
02b3a552
MM
251#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
252 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
253#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
254 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
bc5787c6
MM
255
256static u32 __ethtool_get_flags(struct net_device *dev)
257{
02b3a552
MM
258 u32 flags = 0;
259
260 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
261 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
262 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
263 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
264 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
265
266 return flags;
0a417704
MM
267}
268
bc5787c6 269static int __ethtool_set_flags(struct net_device *dev, u32 data)
da8ac86c 270{
c8f44aff 271 netdev_features_t features = 0, changed;
da8ac86c 272
02b3a552 273 if (data & ~ETH_ALL_FLAGS)
da8ac86c
MM
274 return -EINVAL;
275
02b3a552
MM
276 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
277 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
278 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
279 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
280 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
281
da8ac86c 282 /* allow changing only bits set in hw_features */
02b3a552 283 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
da8ac86c
MM
284 if (changed & ~dev->hw_features)
285 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
286
287 dev->wanted_features =
02b3a552 288 (dev->wanted_features & ~changed) | (features & changed);
da8ac86c 289
6cb6a27c 290 __netdev_update_features(dev);
da8ac86c
MM
291
292 return 0;
293}
294
4bc71cb9 295int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1da177e4 296{
4bc71cb9 297 ASSERT_RTNL();
1da177e4 298
4bc71cb9 299 if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
1da177e4
LT
300 return -EOPNOTSUPP;
301
4bc71cb9
JP
302 memset(cmd, 0, sizeof(struct ethtool_cmd));
303 cmd->cmd = ETHTOOL_GSET;
304 return dev->ethtool_ops->get_settings(dev, cmd);
305}
306EXPORT_SYMBOL(__ethtool_get_settings);
307
308static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
309{
310 int err;
311 struct ethtool_cmd cmd;
312
313 err = __ethtool_get_settings(dev, &cmd);
1da177e4
LT
314 if (err < 0)
315 return err;
316
317 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
318 return -EFAULT;
319 return 0;
320}
321
322static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
323{
324 struct ethtool_cmd cmd;
325
326 if (!dev->ethtool_ops->set_settings)
327 return -EOPNOTSUPP;
328
329 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
330 return -EFAULT;
331
332 return dev->ethtool_ops->set_settings(dev, &cmd);
333}
334
97f8aefb 335static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
336 void __user *useraddr)
1da177e4
LT
337{
338 struct ethtool_drvinfo info;
76fd8593 339 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 340
1da177e4
LT
341 memset(&info, 0, sizeof(info));
342 info.cmd = ETHTOOL_GDRVINFO;
01414802
BH
343 if (ops && ops->get_drvinfo) {
344 ops->get_drvinfo(dev, &info);
345 } else if (dev->dev.parent && dev->dev.parent->driver) {
346 strlcpy(info.bus_info, dev_name(dev->dev.parent),
347 sizeof(info.bus_info));
348 strlcpy(info.driver, dev->dev.parent->driver->name,
349 sizeof(info.driver));
350 } else {
351 return -EOPNOTSUPP;
352 }
1da177e4 353
723b2f57
JG
354 /*
355 * this method of obtaining string set info is deprecated;
d17792eb 356 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 357 */
01414802 358 if (ops && ops->get_sset_count) {
ff03d49f
JG
359 int rc;
360
361 rc = ops->get_sset_count(dev, ETH_SS_TEST);
362 if (rc >= 0)
363 info.testinfo_len = rc;
364 rc = ops->get_sset_count(dev, ETH_SS_STATS);
365 if (rc >= 0)
366 info.n_stats = rc;
339bf024
JG
367 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
368 if (rc >= 0)
369 info.n_priv_flags = rc;
ff03d49f 370 }
01414802 371 if (ops && ops->get_regs_len)
1da177e4 372 info.regdump_len = ops->get_regs_len(dev);
01414802 373 if (ops && ops->get_eeprom_len)
1da177e4
LT
374 info.eedump_len = ops->get_eeprom_len(dev);
375
376 if (copy_to_user(useraddr, &info, sizeof(info)))
377 return -EFAULT;
378 return 0;
379}
380
f5c445ed 381static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 382 void __user *useraddr)
723b2f57
JG
383{
384 struct ethtool_sset_info info;
723b2f57
JG
385 u64 sset_mask;
386 int i, idx = 0, n_bits = 0, ret, rc;
387 u32 *info_buf = NULL;
388
723b2f57
JG
389 if (copy_from_user(&info, useraddr, sizeof(info)))
390 return -EFAULT;
391
392 /* store copy of mask, because we zero struct later on */
393 sset_mask = info.sset_mask;
394 if (!sset_mask)
395 return 0;
396
397 /* calculate size of return buffer */
d17792eb 398 n_bits = hweight64(sset_mask);
723b2f57
JG
399
400 memset(&info, 0, sizeof(info));
401 info.cmd = ETHTOOL_GSSET_INFO;
402
403 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
404 if (!info_buf)
405 return -ENOMEM;
406
407 /*
408 * fill return buffer based on input bitmask and successful
409 * get_sset_count return
410 */
411 for (i = 0; i < 64; i++) {
412 if (!(sset_mask & (1ULL << i)))
413 continue;
414
340ae165 415 rc = __ethtool_get_sset_count(dev, i);
723b2f57
JG
416 if (rc >= 0) {
417 info.sset_mask |= (1ULL << i);
418 info_buf[idx++] = rc;
419 }
420 }
421
422 ret = -EFAULT;
423 if (copy_to_user(useraddr, &info, sizeof(info)))
424 goto out;
425
426 useraddr += offsetof(struct ethtool_sset_info, data);
427 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
428 goto out;
429
430 ret = 0;
431
432out:
433 kfree(info_buf);
434 return ret;
435}
436
97f8aefb 437static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
bf988435 438 u32 cmd, void __user *useraddr)
0853ad66 439{
bf988435
BH
440 struct ethtool_rxnfc info;
441 size_t info_size = sizeof(info);
55664f32 442 int rc;
0853ad66 443
59089d8d 444 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
445 return -EOPNOTSUPP;
446
bf988435
BH
447 /* struct ethtool_rxnfc was originally defined for
448 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
449 * members. User-space might still be using that
450 * definition. */
451 if (cmd == ETHTOOL_SRXFH)
452 info_size = (offsetof(struct ethtool_rxnfc, data) +
453 sizeof(info.data));
454
455 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
456 return -EFAULT;
457
55664f32
BH
458 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
459 if (rc)
460 return rc;
461
462 if (cmd == ETHTOOL_SRXCLSRLINS &&
463 copy_to_user(useraddr, &info, info_size))
464 return -EFAULT;
465
466 return 0;
0853ad66
SB
467}
468
97f8aefb 469static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
bf988435 470 u32 cmd, void __user *useraddr)
0853ad66
SB
471{
472 struct ethtool_rxnfc info;
bf988435 473 size_t info_size = sizeof(info);
59089d8d
SB
474 const struct ethtool_ops *ops = dev->ethtool_ops;
475 int ret;
476 void *rule_buf = NULL;
0853ad66 477
59089d8d 478 if (!ops->get_rxnfc)
0853ad66
SB
479 return -EOPNOTSUPP;
480
bf988435
BH
481 /* struct ethtool_rxnfc was originally defined for
482 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
483 * members. User-space might still be using that
484 * definition. */
485 if (cmd == ETHTOOL_GRXFH)
486 info_size = (offsetof(struct ethtool_rxnfc, data) +
487 sizeof(info.data));
488
489 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
490 return -EFAULT;
491
59089d8d
SB
492 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
493 if (info.rule_cnt > 0) {
db048b69 494 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
ae6df5f9 495 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
db048b69 496 GFP_USER);
59089d8d
SB
497 if (!rule_buf)
498 return -ENOMEM;
499 }
500 }
0853ad66 501
59089d8d
SB
502 ret = ops->get_rxnfc(dev, &info, rule_buf);
503 if (ret < 0)
504 goto err_out;
505
506 ret = -EFAULT;
bf988435 507 if (copy_to_user(useraddr, &info, info_size))
59089d8d
SB
508 goto err_out;
509
510 if (rule_buf) {
511 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
512 if (copy_to_user(useraddr, rule_buf,
513 info.rule_cnt * sizeof(u32)))
514 goto err_out;
515 }
516 ret = 0;
517
518err_out:
c9caceca 519 kfree(rule_buf);
59089d8d
SB
520
521 return ret;
0853ad66
SB
522}
523
a5b6ee29
BH
524static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
525 void __user *useraddr)
526{
7850f63f
BH
527 u32 user_size, dev_size;
528 u32 *indir;
a5b6ee29
BH
529 int ret;
530
7850f63f
BH
531 if (!dev->ethtool_ops->get_rxfh_indir_size ||
532 !dev->ethtool_ops->get_rxfh_indir)
533 return -EOPNOTSUPP;
534 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
535 if (dev_size == 0)
a5b6ee29
BH
536 return -EOPNOTSUPP;
537
7850f63f 538 if (copy_from_user(&user_size,
a5b6ee29 539 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 540 sizeof(user_size)))
a5b6ee29
BH
541 return -EFAULT;
542
7850f63f
BH
543 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
544 &dev_size, sizeof(dev_size)))
545 return -EFAULT;
546
547 /* If the user buffer size is 0, this is just a query for the
548 * device table size. Otherwise, if it's smaller than the
549 * device table size it's an error.
550 */
551 if (user_size < dev_size)
552 return user_size == 0 ? 0 : -EINVAL;
553
554 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
555 if (!indir)
556 return -ENOMEM;
557
a5b6ee29
BH
558 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
559 if (ret)
560 goto out;
561
7850f63f
BH
562 if (copy_to_user(useraddr +
563 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
564 indir, dev_size * sizeof(indir[0])))
a5b6ee29
BH
565 ret = -EFAULT;
566
567out:
568 kfree(indir);
569 return ret;
570}
571
572static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
573 void __user *useraddr)
574{
7850f63f
BH
575 struct ethtool_rxnfc rx_rings;
576 u32 user_size, dev_size, i;
577 u32 *indir;
a5b6ee29
BH
578 int ret;
579
7850f63f
BH
580 if (!dev->ethtool_ops->get_rxfh_indir_size ||
581 !dev->ethtool_ops->set_rxfh_indir ||
582 !dev->ethtool_ops->get_rxnfc)
583 return -EOPNOTSUPP;
584 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
585 if (dev_size == 0)
a5b6ee29
BH
586 return -EOPNOTSUPP;
587
7850f63f 588 if (copy_from_user(&user_size,
a5b6ee29 589 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 590 sizeof(user_size)))
a5b6ee29
BH
591 return -EFAULT;
592
278bc429 593 if (user_size != 0 && user_size != dev_size)
7850f63f
BH
594 return -EINVAL;
595
596 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
597 if (!indir)
598 return -ENOMEM;
599
7850f63f
BH
600 rx_rings.cmd = ETHTOOL_GRXRINGS;
601 ret = dev->ethtool_ops->get_rxnfc(dev, &rx_rings, NULL);
602 if (ret)
603 goto out;
278bc429
BH
604
605 if (user_size == 0) {
606 for (i = 0; i < dev_size; i++)
607 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
608 } else {
609 if (copy_from_user(indir,
610 useraddr +
611 offsetof(struct ethtool_rxfh_indir,
612 ring_index[0]),
613 dev_size * sizeof(indir[0]))) {
614 ret = -EFAULT;
7850f63f
BH
615 goto out;
616 }
278bc429
BH
617
618 /* Validate ring indices */
619 for (i = 0; i < dev_size; i++) {
620 if (indir[i] >= rx_rings.data) {
621 ret = -EINVAL;
622 goto out;
623 }
624 }
7850f63f
BH
625 }
626
a5b6ee29
BH
627 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
628
629out:
630 kfree(indir);
631 return ret;
632}
633
1da177e4
LT
634static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
635{
636 struct ethtool_regs regs;
76fd8593 637 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
638 void *regbuf;
639 int reglen, ret;
640
641 if (!ops->get_regs || !ops->get_regs_len)
642 return -EOPNOTSUPP;
643
644 if (copy_from_user(&regs, useraddr, sizeof(regs)))
645 return -EFAULT;
646
647 reglen = ops->get_regs_len(dev);
648 if (regs.len > reglen)
649 regs.len = reglen;
650
b7c7d01a 651 regbuf = vzalloc(reglen);
67ae7cf1 652 if (reglen && !regbuf)
1da177e4
LT
653 return -ENOMEM;
654
655 ops->get_regs(dev, &regs, regbuf);
656
657 ret = -EFAULT;
658 if (copy_to_user(useraddr, &regs, sizeof(regs)))
659 goto out;
660 useraddr += offsetof(struct ethtool_regs, data);
67ae7cf1 661 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1da177e4
LT
662 goto out;
663 ret = 0;
664
665 out:
a77f5db3 666 vfree(regbuf);
1da177e4
LT
667 return ret;
668}
669
d73d3a8c
BH
670static int ethtool_reset(struct net_device *dev, char __user *useraddr)
671{
672 struct ethtool_value reset;
673 int ret;
674
675 if (!dev->ethtool_ops->reset)
676 return -EOPNOTSUPP;
677
678 if (copy_from_user(&reset, useraddr, sizeof(reset)))
679 return -EFAULT;
680
681 ret = dev->ethtool_ops->reset(dev, &reset.data);
682 if (ret)
683 return ret;
684
685 if (copy_to_user(useraddr, &reset, sizeof(reset)))
686 return -EFAULT;
687 return 0;
688}
689
1da177e4
LT
690static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
691{
8e557421 692 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1da177e4
LT
693
694 if (!dev->ethtool_ops->get_wol)
695 return -EOPNOTSUPP;
696
697 dev->ethtool_ops->get_wol(dev, &wol);
698
699 if (copy_to_user(useraddr, &wol, sizeof(wol)))
700 return -EFAULT;
701 return 0;
702}
703
704static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
705{
706 struct ethtool_wolinfo wol;
707
708 if (!dev->ethtool_ops->set_wol)
709 return -EOPNOTSUPP;
710
711 if (copy_from_user(&wol, useraddr, sizeof(wol)))
712 return -EFAULT;
713
714 return dev->ethtool_ops->set_wol(dev, &wol);
715}
716
1da177e4
LT
717static int ethtool_nway_reset(struct net_device *dev)
718{
719 if (!dev->ethtool_ops->nway_reset)
720 return -EOPNOTSUPP;
721
722 return dev->ethtool_ops->nway_reset(dev);
723}
724
e596e6e4
BH
725static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
726{
727 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
728
729 if (!dev->ethtool_ops->get_link)
730 return -EOPNOTSUPP;
731
732 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
733
734 if (copy_to_user(useraddr, &edata, sizeof(edata)))
735 return -EFAULT;
736 return 0;
737}
738
1da177e4
LT
739static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
740{
741 struct ethtool_eeprom eeprom;
76fd8593 742 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
743 void __user *userbuf = useraddr + sizeof(eeprom);
744 u32 bytes_remaining;
1da177e4 745 u8 *data;
b131dd5d 746 int ret = 0;
1da177e4
LT
747
748 if (!ops->get_eeprom || !ops->get_eeprom_len)
749 return -EOPNOTSUPP;
750
751 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
752 return -EFAULT;
753
754 /* Check for wrap and zero */
755 if (eeprom.offset + eeprom.len <= eeprom.offset)
756 return -EINVAL;
757
758 /* Check for exceeding total eeprom len */
759 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
760 return -EINVAL;
761
b131dd5d 762 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
763 if (!data)
764 return -ENOMEM;
765
b131dd5d
MSB
766 bytes_remaining = eeprom.len;
767 while (bytes_remaining > 0) {
768 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
769
770 ret = ops->get_eeprom(dev, &eeprom, data);
771 if (ret)
772 break;
773 if (copy_to_user(userbuf, data, eeprom.len)) {
774 ret = -EFAULT;
775 break;
776 }
777 userbuf += eeprom.len;
778 eeprom.offset += eeprom.len;
779 bytes_remaining -= eeprom.len;
780 }
1da177e4 781
c5835df9
MSB
782 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
783 eeprom.offset -= eeprom.len;
784 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
785 ret = -EFAULT;
786
1da177e4
LT
787 kfree(data);
788 return ret;
789}
790
791static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
792{
793 struct ethtool_eeprom eeprom;
76fd8593 794 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
795 void __user *userbuf = useraddr + sizeof(eeprom);
796 u32 bytes_remaining;
1da177e4 797 u8 *data;
b131dd5d 798 int ret = 0;
1da177e4
LT
799
800 if (!ops->set_eeprom || !ops->get_eeprom_len)
801 return -EOPNOTSUPP;
802
803 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
804 return -EFAULT;
805
806 /* Check for wrap and zero */
807 if (eeprom.offset + eeprom.len <= eeprom.offset)
808 return -EINVAL;
809
810 /* Check for exceeding total eeprom len */
811 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
812 return -EINVAL;
813
b131dd5d 814 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
815 if (!data)
816 return -ENOMEM;
817
b131dd5d
MSB
818 bytes_remaining = eeprom.len;
819 while (bytes_remaining > 0) {
820 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
821
822 if (copy_from_user(data, userbuf, eeprom.len)) {
823 ret = -EFAULT;
824 break;
825 }
826 ret = ops->set_eeprom(dev, &eeprom, data);
827 if (ret)
828 break;
829 userbuf += eeprom.len;
830 eeprom.offset += eeprom.len;
831 bytes_remaining -= eeprom.len;
832 }
1da177e4 833
1da177e4
LT
834 kfree(data);
835 return ret;
836}
837
97f8aefb 838static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
839 void __user *useraddr)
1da177e4 840{
8e557421 841 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
842
843 if (!dev->ethtool_ops->get_coalesce)
844 return -EOPNOTSUPP;
845
846 dev->ethtool_ops->get_coalesce(dev, &coalesce);
847
848 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
849 return -EFAULT;
850 return 0;
851}
852
97f8aefb 853static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
854 void __user *useraddr)
1da177e4
LT
855{
856 struct ethtool_coalesce coalesce;
857
fa04ae5c 858 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
859 return -EOPNOTSUPP;
860
861 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
862 return -EFAULT;
863
864 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
865}
866
867static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
868{
8e557421 869 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
870
871 if (!dev->ethtool_ops->get_ringparam)
872 return -EOPNOTSUPP;
873
874 dev->ethtool_ops->get_ringparam(dev, &ringparam);
875
876 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
877 return -EFAULT;
878 return 0;
879}
880
881static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
882{
883 struct ethtool_ringparam ringparam;
884
885 if (!dev->ethtool_ops->set_ringparam)
886 return -EOPNOTSUPP;
887
888 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
889 return -EFAULT;
890
891 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
892}
893
8b5933c3 894static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
895 void __user *useraddr)
896{
897 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
898
899 if (!dev->ethtool_ops->get_channels)
900 return -EOPNOTSUPP;
901
902 dev->ethtool_ops->get_channels(dev, &channels);
903
904 if (copy_to_user(useraddr, &channels, sizeof(channels)))
905 return -EFAULT;
906 return 0;
907}
908
909static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
910 void __user *useraddr)
911{
912 struct ethtool_channels channels;
913
914 if (!dev->ethtool_ops->set_channels)
915 return -EOPNOTSUPP;
916
917 if (copy_from_user(&channels, useraddr, sizeof(channels)))
918 return -EFAULT;
919
920 return dev->ethtool_ops->set_channels(dev, &channels);
921}
922
1da177e4
LT
923static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
924{
925 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
926
927 if (!dev->ethtool_ops->get_pauseparam)
928 return -EOPNOTSUPP;
929
930 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
931
932 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
933 return -EFAULT;
934 return 0;
935}
936
937static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
938{
939 struct ethtool_pauseparam pauseparam;
940
e1b90c41 941 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
942 return -EOPNOTSUPP;
943
944 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
945 return -EFAULT;
946
947 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
948}
949
1da177e4
LT
950static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
951{
952 struct ethtool_test test;
76fd8593 953 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 954 u64 *data;
ff03d49f 955 int ret, test_len;
1da177e4 956
a9828ec6 957 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
958 return -EOPNOTSUPP;
959
a9828ec6 960 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
961 if (test_len < 0)
962 return test_len;
963 WARN_ON(test_len == 0);
964
1da177e4
LT
965 if (copy_from_user(&test, useraddr, sizeof(test)))
966 return -EFAULT;
967
ff03d49f
JG
968 test.len = test_len;
969 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
970 if (!data)
971 return -ENOMEM;
972
973 ops->self_test(dev, &test, data);
974
975 ret = -EFAULT;
976 if (copy_to_user(useraddr, &test, sizeof(test)))
977 goto out;
978 useraddr += sizeof(test);
979 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
980 goto out;
981 ret = 0;
982
983 out:
984 kfree(data);
985 return ret;
986}
987
988static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
989{
990 struct ethtool_gstrings gstrings;
1da177e4
LT
991 u8 *data;
992 int ret;
993
1da177e4
LT
994 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
995 return -EFAULT;
996
340ae165 997 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
998 if (ret < 0)
999 return ret;
1000
1001 gstrings.len = ret;
1da177e4
LT
1002
1003 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1004 if (!data)
1005 return -ENOMEM;
1006
340ae165 1007 __ethtool_get_strings(dev, gstrings.string_set, data);
1da177e4
LT
1008
1009 ret = -EFAULT;
1010 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1011 goto out;
1012 useraddr += sizeof(gstrings);
1013 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1014 goto out;
1015 ret = 0;
1016
340ae165 1017out:
1da177e4
LT
1018 kfree(data);
1019 return ret;
1020}
1021
1022static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1023{
1024 struct ethtool_value id;
68f512f2
BH
1025 static bool busy;
1026 int rc;
1da177e4 1027
1ab7b6ac 1028 if (!dev->ethtool_ops->set_phys_id)
1da177e4
LT
1029 return -EOPNOTSUPP;
1030
68f512f2
BH
1031 if (busy)
1032 return -EBUSY;
1033
1da177e4
LT
1034 if (copy_from_user(&id, useraddr, sizeof(id)))
1035 return -EFAULT;
1036
68f512f2 1037 rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 1038 if (rc < 0)
68f512f2
BH
1039 return rc;
1040
1041 /* Drop the RTNL lock while waiting, but prevent reentry or
1042 * removal of the device.
1043 */
1044 busy = true;
1045 dev_hold(dev);
1046 rtnl_unlock();
1047
1048 if (rc == 0) {
1049 /* Driver will handle this itself */
1050 schedule_timeout_interruptible(
143780c6 1051 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 1052 } else {
fce55922
AB
1053 /* Driver expects to be called at twice the frequency in rc */
1054 int n = rc * 2, i, interval = HZ / n;
1055
1056 /* Count down seconds */
68f512f2 1057 do {
fce55922
AB
1058 /* Count down iterations per second */
1059 i = n;
1060 do {
1061 rtnl_lock();
1062 rc = dev->ethtool_ops->set_phys_id(dev,
1063 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1064 rtnl_unlock();
1065 if (rc)
1066 break;
1067 schedule_timeout_interruptible(interval);
1068 } while (!signal_pending(current) && --i != 0);
68f512f2
BH
1069 } while (!signal_pending(current) &&
1070 (id.data == 0 || --id.data != 0));
1071 }
1072
1073 rtnl_lock();
1074 dev_put(dev);
1075 busy = false;
1076
1077 (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1078 return rc;
1da177e4
LT
1079}
1080
1081static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1082{
1083 struct ethtool_stats stats;
76fd8593 1084 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1085 u64 *data;
ff03d49f 1086 int ret, n_stats;
1da177e4 1087
a9828ec6 1088 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1089 return -EOPNOTSUPP;
1090
a9828ec6 1091 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1092 if (n_stats < 0)
1093 return n_stats;
1094 WARN_ON(n_stats == 0);
1095
1da177e4
LT
1096 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1097 return -EFAULT;
1098
ff03d49f
JG
1099 stats.n_stats = n_stats;
1100 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1da177e4
LT
1101 if (!data)
1102 return -ENOMEM;
1103
1104 ops->get_ethtool_stats(dev, &stats, data);
1105
1106 ret = -EFAULT;
1107 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1108 goto out;
1109 useraddr += sizeof(stats);
1110 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1111 goto out;
1112 ret = 0;
1113
1114 out:
1115 kfree(data);
1116 return ret;
1117}
1118
0bf0519d 1119static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
1120{
1121 struct ethtool_perm_addr epaddr;
a6f9a705 1122
313674af 1123 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
1124 return -EFAULT;
1125
313674af
MW
1126 if (epaddr.size < dev->addr_len)
1127 return -ETOOSMALL;
1128 epaddr.size = dev->addr_len;
a6f9a705 1129
a6f9a705 1130 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 1131 return -EFAULT;
a6f9a705 1132 useraddr += sizeof(epaddr);
313674af
MW
1133 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1134 return -EFAULT;
1135 return 0;
a6f9a705
JW
1136}
1137
13c99b24
JG
1138static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1139 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 1140{
8e557421 1141 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 1142
13c99b24 1143 if (!actor)
3ae7c0b2
JG
1144 return -EOPNOTSUPP;
1145
13c99b24 1146 edata.data = actor(dev);
3ae7c0b2
JG
1147
1148 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1149 return -EFAULT;
1150 return 0;
1151}
1152
13c99b24
JG
1153static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1154 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
1155{
1156 struct ethtool_value edata;
1157
13c99b24 1158 if (!actor)
3ae7c0b2
JG
1159 return -EOPNOTSUPP;
1160
1161 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1162 return -EFAULT;
1163
13c99b24 1164 actor(dev, edata.data);
339bf024
JG
1165 return 0;
1166}
1167
13c99b24
JG
1168static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1169 int (*actor)(struct net_device *, u32))
339bf024
JG
1170{
1171 struct ethtool_value edata;
1172
13c99b24 1173 if (!actor)
339bf024
JG
1174 return -EOPNOTSUPP;
1175
1176 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1177 return -EFAULT;
1178
13c99b24 1179 return actor(dev, edata.data);
339bf024
JG
1180}
1181
97f8aefb 1182static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1183 char __user *useraddr)
05c6a8d7
AK
1184{
1185 struct ethtool_flash efl;
1186
1187 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1188 return -EFAULT;
1189
1190 if (!dev->ethtool_ops->flash_device)
1191 return -EOPNOTSUPP;
1192
1193 return dev->ethtool_ops->flash_device(dev, &efl);
1194}
1195
29dd54b7
AC
1196static int ethtool_set_dump(struct net_device *dev,
1197 void __user *useraddr)
1198{
1199 struct ethtool_dump dump;
1200
1201 if (!dev->ethtool_ops->set_dump)
1202 return -EOPNOTSUPP;
1203
1204 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1205 return -EFAULT;
1206
1207 return dev->ethtool_ops->set_dump(dev, &dump);
1208}
1209
1210static int ethtool_get_dump_flag(struct net_device *dev,
1211 void __user *useraddr)
1212{
1213 int ret;
1214 struct ethtool_dump dump;
1215 const struct ethtool_ops *ops = dev->ethtool_ops;
1216
1217 if (!dev->ethtool_ops->get_dump_flag)
1218 return -EOPNOTSUPP;
1219
1220 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1221 return -EFAULT;
1222
1223 ret = ops->get_dump_flag(dev, &dump);
1224 if (ret)
1225 return ret;
1226
1227 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1228 return -EFAULT;
1229 return 0;
1230}
1231
1232static int ethtool_get_dump_data(struct net_device *dev,
1233 void __user *useraddr)
1234{
1235 int ret;
1236 __u32 len;
1237 struct ethtool_dump dump, tmp;
1238 const struct ethtool_ops *ops = dev->ethtool_ops;
1239 void *data = NULL;
1240
1241 if (!dev->ethtool_ops->get_dump_data ||
1242 !dev->ethtool_ops->get_dump_flag)
1243 return -EOPNOTSUPP;
1244
1245 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1246 return -EFAULT;
1247
1248 memset(&tmp, 0, sizeof(tmp));
1249 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1250 ret = ops->get_dump_flag(dev, &tmp);
1251 if (ret)
1252 return ret;
1253
1254 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1255 if (!len)
1256 return -EFAULT;
1257
1258 data = vzalloc(tmp.len);
1259 if (!data)
1260 return -ENOMEM;
1261 ret = ops->get_dump_data(dev, &dump, data);
1262 if (ret)
1263 goto out;
1264
1265 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1266 ret = -EFAULT;
1267 goto out;
1268 }
1269 useraddr += offsetof(struct ethtool_dump, data);
1270 if (copy_to_user(useraddr, data, len))
1271 ret = -EFAULT;
1272out:
1273 vfree(data);
1274 return ret;
1275}
1276
1da177e4
LT
1277/* The main entry point in this file. Called from net/core/dev.c */
1278
881d966b 1279int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 1280{
881d966b 1281 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4
LT
1282 void __user *useraddr = ifr->ifr_data;
1283 u32 ethcmd;
1284 int rc;
04ed3e74 1285 u32 old_features;
1da177e4 1286
1da177e4
LT
1287 if (!dev || !netif_device_present(dev))
1288 return -ENODEV;
1289
97f8aefb 1290 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
1291 return -EFAULT;
1292
01414802
BH
1293 if (!dev->ethtool_ops) {
1294 /* ETHTOOL_GDRVINFO does not require any driver support.
1295 * It is also unprivileged and does not change anything,
1296 * so we can take a shortcut to it. */
1297 if (ethcmd == ETHTOOL_GDRVINFO)
1298 return ethtool_get_drvinfo(dev, useraddr);
1299 else
1300 return -EOPNOTSUPP;
1301 }
1302
75f3123c 1303 /* Allow some commands to be done by anyone */
97f8aefb 1304 switch (ethcmd) {
0fdc100b 1305 case ETHTOOL_GSET:
75f3123c 1306 case ETHTOOL_GDRVINFO:
75f3123c 1307 case ETHTOOL_GMSGLVL:
75f3123c
SH
1308 case ETHTOOL_GCOALESCE:
1309 case ETHTOOL_GRINGPARAM:
1310 case ETHTOOL_GPAUSEPARAM:
1311 case ETHTOOL_GRXCSUM:
1312 case ETHTOOL_GTXCSUM:
1313 case ETHTOOL_GSG:
1314 case ETHTOOL_GSTRINGS:
75f3123c
SH
1315 case ETHTOOL_GTSO:
1316 case ETHTOOL_GPERMADDR:
1317 case ETHTOOL_GUFO:
1318 case ETHTOOL_GGSO:
1cab819b 1319 case ETHTOOL_GGRO:
339bf024
JG
1320 case ETHTOOL_GFLAGS:
1321 case ETHTOOL_GPFLAGS:
0853ad66 1322 case ETHTOOL_GRXFH:
59089d8d
SB
1323 case ETHTOOL_GRXRINGS:
1324 case ETHTOOL_GRXCLSRLCNT:
1325 case ETHTOOL_GRXCLSRULE:
1326 case ETHTOOL_GRXCLSRLALL:
5455c699 1327 case ETHTOOL_GFEATURES:
75f3123c
SH
1328 break;
1329 default:
1330 if (!capable(CAP_NET_ADMIN))
1331 return -EPERM;
1332 }
1333
97f8aefb 1334 if (dev->ethtool_ops->begin) {
1335 rc = dev->ethtool_ops->begin(dev);
1336 if (rc < 0)
1da177e4 1337 return rc;
97f8aefb 1338 }
d8a33ac4
SH
1339 old_features = dev->features;
1340
1da177e4
LT
1341 switch (ethcmd) {
1342 case ETHTOOL_GSET:
1343 rc = ethtool_get_settings(dev, useraddr);
1344 break;
1345 case ETHTOOL_SSET:
1346 rc = ethtool_set_settings(dev, useraddr);
1347 break;
1348 case ETHTOOL_GDRVINFO:
1349 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
1350 break;
1351 case ETHTOOL_GREGS:
1352 rc = ethtool_get_regs(dev, useraddr);
1353 break;
1354 case ETHTOOL_GWOL:
1355 rc = ethtool_get_wol(dev, useraddr);
1356 break;
1357 case ETHTOOL_SWOL:
1358 rc = ethtool_set_wol(dev, useraddr);
1359 break;
1360 case ETHTOOL_GMSGLVL:
13c99b24
JG
1361 rc = ethtool_get_value(dev, useraddr, ethcmd,
1362 dev->ethtool_ops->get_msglevel);
1da177e4
LT
1363 break;
1364 case ETHTOOL_SMSGLVL:
13c99b24
JG
1365 rc = ethtool_set_value_void(dev, useraddr,
1366 dev->ethtool_ops->set_msglevel);
1da177e4
LT
1367 break;
1368 case ETHTOOL_NWAY_RST:
1369 rc = ethtool_nway_reset(dev);
1370 break;
1371 case ETHTOOL_GLINK:
e596e6e4 1372 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
1373 break;
1374 case ETHTOOL_GEEPROM:
1375 rc = ethtool_get_eeprom(dev, useraddr);
1376 break;
1377 case ETHTOOL_SEEPROM:
1378 rc = ethtool_set_eeprom(dev, useraddr);
1379 break;
1380 case ETHTOOL_GCOALESCE:
1381 rc = ethtool_get_coalesce(dev, useraddr);
1382 break;
1383 case ETHTOOL_SCOALESCE:
1384 rc = ethtool_set_coalesce(dev, useraddr);
1385 break;
1386 case ETHTOOL_GRINGPARAM:
1387 rc = ethtool_get_ringparam(dev, useraddr);
1388 break;
1389 case ETHTOOL_SRINGPARAM:
1390 rc = ethtool_set_ringparam(dev, useraddr);
1391 break;
1392 case ETHTOOL_GPAUSEPARAM:
1393 rc = ethtool_get_pauseparam(dev, useraddr);
1394 break;
1395 case ETHTOOL_SPAUSEPARAM:
1396 rc = ethtool_set_pauseparam(dev, useraddr);
1397 break;
1da177e4
LT
1398 case ETHTOOL_TEST:
1399 rc = ethtool_self_test(dev, useraddr);
1400 break;
1401 case ETHTOOL_GSTRINGS:
1402 rc = ethtool_get_strings(dev, useraddr);
1403 break;
1404 case ETHTOOL_PHYS_ID:
1405 rc = ethtool_phys_id(dev, useraddr);
1406 break;
1407 case ETHTOOL_GSTATS:
1408 rc = ethtool_get_stats(dev, useraddr);
1409 break;
a6f9a705
JW
1410 case ETHTOOL_GPERMADDR:
1411 rc = ethtool_get_perm_addr(dev, useraddr);
1412 break;
3ae7c0b2 1413 case ETHTOOL_GFLAGS:
13c99b24 1414 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 1415 __ethtool_get_flags);
3ae7c0b2
JG
1416 break;
1417 case ETHTOOL_SFLAGS:
da8ac86c 1418 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 1419 break;
339bf024 1420 case ETHTOOL_GPFLAGS:
13c99b24
JG
1421 rc = ethtool_get_value(dev, useraddr, ethcmd,
1422 dev->ethtool_ops->get_priv_flags);
339bf024
JG
1423 break;
1424 case ETHTOOL_SPFLAGS:
13c99b24
JG
1425 rc = ethtool_set_value(dev, useraddr,
1426 dev->ethtool_ops->set_priv_flags);
339bf024 1427 break;
0853ad66 1428 case ETHTOOL_GRXFH:
59089d8d
SB
1429 case ETHTOOL_GRXRINGS:
1430 case ETHTOOL_GRXCLSRLCNT:
1431 case ETHTOOL_GRXCLSRULE:
1432 case ETHTOOL_GRXCLSRLALL:
bf988435 1433 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
1434 break;
1435 case ETHTOOL_SRXFH:
59089d8d
SB
1436 case ETHTOOL_SRXCLSRLDEL:
1437 case ETHTOOL_SRXCLSRLINS:
bf988435 1438 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 1439 break;
05c6a8d7
AK
1440 case ETHTOOL_FLASHDEV:
1441 rc = ethtool_flash_device(dev, useraddr);
1442 break;
d73d3a8c
BH
1443 case ETHTOOL_RESET:
1444 rc = ethtool_reset(dev, useraddr);
1445 break;
723b2f57
JG
1446 case ETHTOOL_GSSET_INFO:
1447 rc = ethtool_get_sset_info(dev, useraddr);
1448 break;
a5b6ee29
BH
1449 case ETHTOOL_GRXFHINDIR:
1450 rc = ethtool_get_rxfh_indir(dev, useraddr);
1451 break;
1452 case ETHTOOL_SRXFHINDIR:
1453 rc = ethtool_set_rxfh_indir(dev, useraddr);
1454 break;
5455c699
MM
1455 case ETHTOOL_GFEATURES:
1456 rc = ethtool_get_features(dev, useraddr);
1457 break;
1458 case ETHTOOL_SFEATURES:
1459 rc = ethtool_set_features(dev, useraddr);
1460 break;
0a417704 1461 case ETHTOOL_GTXCSUM:
e83d360d 1462 case ETHTOOL_GRXCSUM:
0a417704
MM
1463 case ETHTOOL_GSG:
1464 case ETHTOOL_GTSO:
1465 case ETHTOOL_GUFO:
1466 case ETHTOOL_GGSO:
1467 case ETHTOOL_GGRO:
1468 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1469 break;
1470 case ETHTOOL_STXCSUM:
e83d360d 1471 case ETHTOOL_SRXCSUM:
0a417704
MM
1472 case ETHTOOL_SSG:
1473 case ETHTOOL_STSO:
1474 case ETHTOOL_SUFO:
1475 case ETHTOOL_SGSO:
1476 case ETHTOOL_SGRO:
1477 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1478 break;
8b5933c3 1479 case ETHTOOL_GCHANNELS:
1480 rc = ethtool_get_channels(dev, useraddr);
1481 break;
1482 case ETHTOOL_SCHANNELS:
1483 rc = ethtool_set_channels(dev, useraddr);
1484 break;
29dd54b7
AC
1485 case ETHTOOL_SET_DUMP:
1486 rc = ethtool_set_dump(dev, useraddr);
1487 break;
1488 case ETHTOOL_GET_DUMP_FLAG:
1489 rc = ethtool_get_dump_flag(dev, useraddr);
1490 break;
1491 case ETHTOOL_GET_DUMP_DATA:
1492 rc = ethtool_get_dump_data(dev, useraddr);
1493 break;
1da177e4 1494 default:
61a44b9c 1495 rc = -EOPNOTSUPP;
1da177e4 1496 }
4ec93edb 1497
e71a4783 1498 if (dev->ethtool_ops->complete)
1da177e4 1499 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
1500
1501 if (old_features != dev->features)
1502 netdev_features_change(dev);
1503
1da177e4 1504 return rc;
1da177e4 1505}