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