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