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