import PULS_20180308
[GitHub/mt8127/android_kernel_alcatel_ttab.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",
788dfcac 63 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
9d921549 64
788dfcac
FLVC
65 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
66 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-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{
4b9e9796 714 struct ethtool_wolinfo wol;
1da177e4
LT
715
716 if (!dev->ethtool_ops->get_wol)
717 return -EOPNOTSUPP;
718
4b9e9796
S
719 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
720 wol.cmd = ETHTOOL_GWOL;
1da177e4
LT
721 dev->ethtool_ops->get_wol(dev, &wol);
722
723 if (copy_to_user(useraddr, &wol, sizeof(wol)))
724 return -EFAULT;
725 return 0;
726}
727
728static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
729{
730 struct ethtool_wolinfo wol;
731
732 if (!dev->ethtool_ops->set_wol)
733 return -EOPNOTSUPP;
734
735 if (copy_from_user(&wol, useraddr, sizeof(wol)))
736 return -EFAULT;
737
738 return dev->ethtool_ops->set_wol(dev, &wol);
739}
740
80f12ecc
YM
741static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
742{
743 struct ethtool_eee edata;
744 int rc;
745
746 if (!dev->ethtool_ops->get_eee)
747 return -EOPNOTSUPP;
748
749 memset(&edata, 0, sizeof(struct ethtool_eee));
750 edata.cmd = ETHTOOL_GEEE;
751 rc = dev->ethtool_ops->get_eee(dev, &edata);
752
753 if (rc)
754 return rc;
755
756 if (copy_to_user(useraddr, &edata, sizeof(edata)))
757 return -EFAULT;
758
759 return 0;
760}
761
762static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
763{
764 struct ethtool_eee edata;
765
766 if (!dev->ethtool_ops->set_eee)
767 return -EOPNOTSUPP;
768
769 if (copy_from_user(&edata, useraddr, sizeof(edata)))
770 return -EFAULT;
771
772 return dev->ethtool_ops->set_eee(dev, &edata);
773}
774
1da177e4
LT
775static int ethtool_nway_reset(struct net_device *dev)
776{
777 if (!dev->ethtool_ops->nway_reset)
778 return -EOPNOTSUPP;
779
780 return dev->ethtool_ops->nway_reset(dev);
781}
782
e596e6e4
BH
783static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
784{
785 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
786
787 if (!dev->ethtool_ops->get_link)
788 return -EOPNOTSUPP;
789
790 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
791
792 if (copy_to_user(useraddr, &edata, sizeof(edata)))
793 return -EFAULT;
794 return 0;
795}
796
081d094e
BH
797static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
798 int (*getter)(struct net_device *,
799 struct ethtool_eeprom *, u8 *),
800 u32 total_len)
1da177e4
LT
801{
802 struct ethtool_eeprom eeprom;
b131dd5d
MSB
803 void __user *userbuf = useraddr + sizeof(eeprom);
804 u32 bytes_remaining;
1da177e4 805 u8 *data;
b131dd5d 806 int ret = 0;
1da177e4 807
1da177e4
LT
808 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
809 return -EFAULT;
810
811 /* Check for wrap and zero */
812 if (eeprom.offset + eeprom.len <= eeprom.offset)
813 return -EINVAL;
814
815 /* Check for exceeding total eeprom len */
081d094e 816 if (eeprom.offset + eeprom.len > total_len)
1da177e4
LT
817 return -EINVAL;
818
b131dd5d 819 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
820 if (!data)
821 return -ENOMEM;
822
b131dd5d
MSB
823 bytes_remaining = eeprom.len;
824 while (bytes_remaining > 0) {
825 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
826
081d094e 827 ret = getter(dev, &eeprom, data);
b131dd5d
MSB
828 if (ret)
829 break;
830 if (copy_to_user(userbuf, data, eeprom.len)) {
831 ret = -EFAULT;
832 break;
833 }
834 userbuf += eeprom.len;
835 eeprom.offset += eeprom.len;
836 bytes_remaining -= eeprom.len;
837 }
1da177e4 838
c5835df9
MSB
839 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
840 eeprom.offset -= eeprom.len;
841 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
842 ret = -EFAULT;
843
1da177e4
LT
844 kfree(data);
845 return ret;
846}
847
081d094e
BH
848static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
849{
850 const struct ethtool_ops *ops = dev->ethtool_ops;
851
852 if (!ops->get_eeprom || !ops->get_eeprom_len)
853 return -EOPNOTSUPP;
854
855 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
856 ops->get_eeprom_len(dev));
857}
858
1da177e4
LT
859static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
860{
861 struct ethtool_eeprom eeprom;
76fd8593 862 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
863 void __user *userbuf = useraddr + sizeof(eeprom);
864 u32 bytes_remaining;
1da177e4 865 u8 *data;
b131dd5d 866 int ret = 0;
1da177e4
LT
867
868 if (!ops->set_eeprom || !ops->get_eeprom_len)
869 return -EOPNOTSUPP;
870
871 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
872 return -EFAULT;
873
874 /* Check for wrap and zero */
875 if (eeprom.offset + eeprom.len <= eeprom.offset)
876 return -EINVAL;
877
878 /* Check for exceeding total eeprom len */
879 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
880 return -EINVAL;
881
b131dd5d 882 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
883 if (!data)
884 return -ENOMEM;
885
b131dd5d
MSB
886 bytes_remaining = eeprom.len;
887 while (bytes_remaining > 0) {
888 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
889
890 if (copy_from_user(data, userbuf, eeprom.len)) {
891 ret = -EFAULT;
892 break;
893 }
894 ret = ops->set_eeprom(dev, &eeprom, data);
895 if (ret)
896 break;
897 userbuf += eeprom.len;
898 eeprom.offset += eeprom.len;
899 bytes_remaining -= eeprom.len;
900 }
1da177e4 901
1da177e4
LT
902 kfree(data);
903 return ret;
904}
905
97f8aefb 906static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
907 void __user *useraddr)
1da177e4 908{
8e557421 909 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
910
911 if (!dev->ethtool_ops->get_coalesce)
912 return -EOPNOTSUPP;
913
914 dev->ethtool_ops->get_coalesce(dev, &coalesce);
915
916 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
917 return -EFAULT;
918 return 0;
919}
920
97f8aefb 921static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
922 void __user *useraddr)
1da177e4
LT
923{
924 struct ethtool_coalesce coalesce;
925
fa04ae5c 926 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
927 return -EOPNOTSUPP;
928
929 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
930 return -EFAULT;
931
932 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
933}
934
935static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
936{
8e557421 937 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
938
939 if (!dev->ethtool_ops->get_ringparam)
940 return -EOPNOTSUPP;
941
942 dev->ethtool_ops->get_ringparam(dev, &ringparam);
943
944 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
945 return -EFAULT;
946 return 0;
947}
948
949static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
950{
951 struct ethtool_ringparam ringparam;
952
953 if (!dev->ethtool_ops->set_ringparam)
954 return -EOPNOTSUPP;
955
956 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
957 return -EFAULT;
958
959 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
960}
961
8b5933c3 962static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
963 void __user *useraddr)
964{
965 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
966
967 if (!dev->ethtool_ops->get_channels)
968 return -EOPNOTSUPP;
969
970 dev->ethtool_ops->get_channels(dev, &channels);
971
972 if (copy_to_user(useraddr, &channels, sizeof(channels)))
973 return -EFAULT;
974 return 0;
975}
976
977static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
978 void __user *useraddr)
979{
980 struct ethtool_channels channels;
981
982 if (!dev->ethtool_ops->set_channels)
983 return -EOPNOTSUPP;
984
985 if (copy_from_user(&channels, useraddr, sizeof(channels)))
986 return -EFAULT;
987
988 return dev->ethtool_ops->set_channels(dev, &channels);
989}
990
1da177e4
LT
991static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
992{
993 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
994
995 if (!dev->ethtool_ops->get_pauseparam)
996 return -EOPNOTSUPP;
997
998 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
999
1000 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1001 return -EFAULT;
1002 return 0;
1003}
1004
1005static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1006{
1007 struct ethtool_pauseparam pauseparam;
1008
e1b90c41 1009 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
1010 return -EOPNOTSUPP;
1011
1012 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1013 return -EFAULT;
1014
1015 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1016}
1017
1da177e4
LT
1018static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1019{
1020 struct ethtool_test test;
76fd8593 1021 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1022 u64 *data;
ff03d49f 1023 int ret, test_len;
1da177e4 1024
a9828ec6 1025 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
1026 return -EOPNOTSUPP;
1027
a9828ec6 1028 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
1029 if (test_len < 0)
1030 return test_len;
1031 WARN_ON(test_len == 0);
1032
1da177e4
LT
1033 if (copy_from_user(&test, useraddr, sizeof(test)))
1034 return -EFAULT;
1035
ff03d49f
JG
1036 test.len = test_len;
1037 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
1038 if (!data)
1039 return -ENOMEM;
1040
1041 ops->self_test(dev, &test, data);
1042
1043 ret = -EFAULT;
1044 if (copy_to_user(useraddr, &test, sizeof(test)))
1045 goto out;
1046 useraddr += sizeof(test);
1047 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1048 goto out;
1049 ret = 0;
1050
1051 out:
1052 kfree(data);
1053 return ret;
1054}
1055
1056static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1057{
1058 struct ethtool_gstrings gstrings;
1da177e4
LT
1059 u8 *data;
1060 int ret;
1061
1da177e4
LT
1062 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1063 return -EFAULT;
1064
340ae165 1065 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
1066 if (ret < 0)
1067 return ret;
1068
1069 gstrings.len = ret;
1da177e4
LT
1070
1071 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1072 if (!data)
1073 return -ENOMEM;
1074
340ae165 1075 __ethtool_get_strings(dev, gstrings.string_set, data);
1da177e4
LT
1076
1077 ret = -EFAULT;
1078 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1079 goto out;
1080 useraddr += sizeof(gstrings);
1081 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1082 goto out;
1083 ret = 0;
1084
340ae165 1085out:
1da177e4
LT
1086 kfree(data);
1087 return ret;
1088}
1089
1090static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1091{
1092 struct ethtool_value id;
68f512f2 1093 static bool busy;
c03a14e8 1094 const struct ethtool_ops *ops = dev->ethtool_ops;
68f512f2 1095 int rc;
1da177e4 1096
c03a14e8 1097 if (!ops->set_phys_id)
1da177e4
LT
1098 return -EOPNOTSUPP;
1099
68f512f2
BH
1100 if (busy)
1101 return -EBUSY;
1102
1da177e4
LT
1103 if (copy_from_user(&id, useraddr, sizeof(id)))
1104 return -EFAULT;
1105
c03a14e8 1106 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 1107 if (rc < 0)
68f512f2
BH
1108 return rc;
1109
1110 /* Drop the RTNL lock while waiting, but prevent reentry or
1111 * removal of the device.
1112 */
1113 busy = true;
1114 dev_hold(dev);
1115 rtnl_unlock();
1116
1117 if (rc == 0) {
1118 /* Driver will handle this itself */
1119 schedule_timeout_interruptible(
143780c6 1120 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 1121 } else {
fce55922
AB
1122 /* Driver expects to be called at twice the frequency in rc */
1123 int n = rc * 2, i, interval = HZ / n;
1124
1125 /* Count down seconds */
68f512f2 1126 do {
fce55922
AB
1127 /* Count down iterations per second */
1128 i = n;
1129 do {
1130 rtnl_lock();
c03a14e8 1131 rc = ops->set_phys_id(dev,
fce55922
AB
1132 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1133 rtnl_unlock();
1134 if (rc)
1135 break;
1136 schedule_timeout_interruptible(interval);
1137 } while (!signal_pending(current) && --i != 0);
68f512f2
BH
1138 } while (!signal_pending(current) &&
1139 (id.data == 0 || --id.data != 0));
1140 }
1141
1142 rtnl_lock();
1143 dev_put(dev);
1144 busy = false;
1145
c03a14e8 1146 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
68f512f2 1147 return rc;
1da177e4
LT
1148}
1149
1150static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1151{
1152 struct ethtool_stats stats;
76fd8593 1153 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1154 u64 *data;
ff03d49f 1155 int ret, n_stats;
1da177e4 1156
a9828ec6 1157 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1158 return -EOPNOTSUPP;
1159
a9828ec6 1160 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1161 if (n_stats < 0)
1162 return n_stats;
1163 WARN_ON(n_stats == 0);
1164
1da177e4
LT
1165 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1166 return -EFAULT;
1167
ff03d49f
JG
1168 stats.n_stats = n_stats;
1169 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1da177e4
LT
1170 if (!data)
1171 return -ENOMEM;
1172
1173 ops->get_ethtool_stats(dev, &stats, data);
1174
1175 ret = -EFAULT;
1176 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1177 goto out;
1178 useraddr += sizeof(stats);
1179 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1180 goto out;
1181 ret = 0;
1182
1183 out:
1184 kfree(data);
1185 return ret;
1186}
1187
0bf0519d 1188static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
1189{
1190 struct ethtool_perm_addr epaddr;
a6f9a705 1191
313674af 1192 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
1193 return -EFAULT;
1194
313674af
MW
1195 if (epaddr.size < dev->addr_len)
1196 return -ETOOSMALL;
1197 epaddr.size = dev->addr_len;
a6f9a705 1198
a6f9a705 1199 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 1200 return -EFAULT;
a6f9a705 1201 useraddr += sizeof(epaddr);
313674af
MW
1202 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1203 return -EFAULT;
1204 return 0;
a6f9a705
JW
1205}
1206
13c99b24
JG
1207static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1208 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 1209{
8e557421 1210 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 1211
13c99b24 1212 if (!actor)
3ae7c0b2
JG
1213 return -EOPNOTSUPP;
1214
13c99b24 1215 edata.data = actor(dev);
3ae7c0b2
JG
1216
1217 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1218 return -EFAULT;
1219 return 0;
1220}
1221
13c99b24
JG
1222static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1223 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
1224{
1225 struct ethtool_value edata;
1226
13c99b24 1227 if (!actor)
3ae7c0b2
JG
1228 return -EOPNOTSUPP;
1229
1230 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1231 return -EFAULT;
1232
13c99b24 1233 actor(dev, edata.data);
339bf024
JG
1234 return 0;
1235}
1236
13c99b24
JG
1237static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1238 int (*actor)(struct net_device *, u32))
339bf024
JG
1239{
1240 struct ethtool_value edata;
1241
13c99b24 1242 if (!actor)
339bf024
JG
1243 return -EOPNOTSUPP;
1244
1245 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1246 return -EFAULT;
1247
13c99b24 1248 return actor(dev, edata.data);
339bf024
JG
1249}
1250
97f8aefb 1251static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1252 char __user *useraddr)
05c6a8d7
AK
1253{
1254 struct ethtool_flash efl;
1255
1256 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1257 return -EFAULT;
1258
1259 if (!dev->ethtool_ops->flash_device)
1260 return -EOPNOTSUPP;
1261
786f5281
BH
1262 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1263
05c6a8d7
AK
1264 return dev->ethtool_ops->flash_device(dev, &efl);
1265}
1266
29dd54b7
AC
1267static int ethtool_set_dump(struct net_device *dev,
1268 void __user *useraddr)
1269{
1270 struct ethtool_dump dump;
1271
1272 if (!dev->ethtool_ops->set_dump)
1273 return -EOPNOTSUPP;
1274
1275 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1276 return -EFAULT;
1277
1278 return dev->ethtool_ops->set_dump(dev, &dump);
1279}
1280
1281static int ethtool_get_dump_flag(struct net_device *dev,
1282 void __user *useraddr)
1283{
1284 int ret;
1285 struct ethtool_dump dump;
1286 const struct ethtool_ops *ops = dev->ethtool_ops;
1287
c03a14e8 1288 if (!ops->get_dump_flag)
29dd54b7
AC
1289 return -EOPNOTSUPP;
1290
1291 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1292 return -EFAULT;
1293
1294 ret = ops->get_dump_flag(dev, &dump);
1295 if (ret)
1296 return ret;
1297
1298 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1299 return -EFAULT;
1300 return 0;
1301}
1302
1303static int ethtool_get_dump_data(struct net_device *dev,
1304 void __user *useraddr)
1305{
1306 int ret;
1307 __u32 len;
1308 struct ethtool_dump dump, tmp;
1309 const struct ethtool_ops *ops = dev->ethtool_ops;
1310 void *data = NULL;
1311
c03a14e8 1312 if (!ops->get_dump_data || !ops->get_dump_flag)
29dd54b7
AC
1313 return -EOPNOTSUPP;
1314
1315 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1316 return -EFAULT;
1317
1318 memset(&tmp, 0, sizeof(tmp));
1319 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1320 ret = ops->get_dump_flag(dev, &tmp);
1321 if (ret)
1322 return ret;
1323
1324 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1325 if (!len)
1326 return -EFAULT;
1327
1328 data = vzalloc(tmp.len);
1329 if (!data)
1330 return -ENOMEM;
1331 ret = ops->get_dump_data(dev, &dump, data);
1332 if (ret)
1333 goto out;
1334
1335 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1336 ret = -EFAULT;
1337 goto out;
1338 }
1339 useraddr += offsetof(struct ethtool_dump, data);
1340 if (copy_to_user(useraddr, data, len))
1341 ret = -EFAULT;
1342out:
1343 vfree(data);
1344 return ret;
1345}
1346
c8f3a8c3
RC
1347static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1348{
1349 int err = 0;
1350 struct ethtool_ts_info info;
1351 const struct ethtool_ops *ops = dev->ethtool_ops;
1352 struct phy_device *phydev = dev->phydev;
1353
1354 memset(&info, 0, sizeof(info));
1355 info.cmd = ETHTOOL_GET_TS_INFO;
1356
1357 if (phydev && phydev->drv && phydev->drv->ts_info) {
c8f3a8c3 1358 err = phydev->drv->ts_info(phydev, &info);
c03a14e8 1359 } else if (ops->get_ts_info) {
c8f3a8c3 1360 err = ops->get_ts_info(dev, &info);
c8f3a8c3
RC
1361 } else {
1362 info.so_timestamping =
1363 SOF_TIMESTAMPING_RX_SOFTWARE |
1364 SOF_TIMESTAMPING_SOFTWARE;
1365 info.phc_index = -1;
1366 }
1367
1368 if (err)
1369 return err;
1370
1371 if (copy_to_user(useraddr, &info, sizeof(info)))
1372 err = -EFAULT;
1373
1374 return err;
1375}
1376
41c3cb6d
SH
1377static int ethtool_get_module_info(struct net_device *dev,
1378 void __user *useraddr)
1379{
1380 int ret;
1381 struct ethtool_modinfo modinfo;
1382 const struct ethtool_ops *ops = dev->ethtool_ops;
1383
1384 if (!ops->get_module_info)
1385 return -EOPNOTSUPP;
1386
1387 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1388 return -EFAULT;
1389
1390 ret = ops->get_module_info(dev, &modinfo);
1391 if (ret)
1392 return ret;
1393
1394 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1395 return -EFAULT;
1396
1397 return 0;
1398}
1399
1400static int ethtool_get_module_eeprom(struct net_device *dev,
1401 void __user *useraddr)
1402{
1403 int ret;
1404 struct ethtool_modinfo modinfo;
1405 const struct ethtool_ops *ops = dev->ethtool_ops;
1406
1407 if (!ops->get_module_info || !ops->get_module_eeprom)
1408 return -EOPNOTSUPP;
1409
1410 ret = ops->get_module_info(dev, &modinfo);
1411 if (ret)
1412 return ret;
1413
1414 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1415 modinfo.eeprom_len);
1416}
1417
1da177e4
LT
1418/* The main entry point in this file. Called from net/core/dev.c */
1419
881d966b 1420int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 1421{
881d966b 1422 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4
LT
1423 void __user *useraddr = ifr->ifr_data;
1424 u32 ethcmd;
1425 int rc;
b29d3145 1426 netdev_features_t old_features;
1da177e4 1427
1da177e4
LT
1428 if (!dev || !netif_device_present(dev))
1429 return -ENODEV;
1430
97f8aefb 1431 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
1432 return -EFAULT;
1433
75f3123c 1434 /* Allow some commands to be done by anyone */
97f8aefb 1435 switch (ethcmd) {
0fdc100b 1436 case ETHTOOL_GSET:
75f3123c 1437 case ETHTOOL_GDRVINFO:
75f3123c 1438 case ETHTOOL_GMSGLVL:
2da45db2 1439 case ETHTOOL_GLINK:
75f3123c
SH
1440 case ETHTOOL_GCOALESCE:
1441 case ETHTOOL_GRINGPARAM:
1442 case ETHTOOL_GPAUSEPARAM:
1443 case ETHTOOL_GRXCSUM:
1444 case ETHTOOL_GTXCSUM:
1445 case ETHTOOL_GSG:
f80400a2 1446 case ETHTOOL_GSSET_INFO:
75f3123c 1447 case ETHTOOL_GSTRINGS:
2da45db2 1448 case ETHTOOL_GSTATS:
75f3123c
SH
1449 case ETHTOOL_GTSO:
1450 case ETHTOOL_GPERMADDR:
1451 case ETHTOOL_GUFO:
1452 case ETHTOOL_GGSO:
1cab819b 1453 case ETHTOOL_GGRO:
339bf024
JG
1454 case ETHTOOL_GFLAGS:
1455 case ETHTOOL_GPFLAGS:
0853ad66 1456 case ETHTOOL_GRXFH:
59089d8d
SB
1457 case ETHTOOL_GRXRINGS:
1458 case ETHTOOL_GRXCLSRLCNT:
1459 case ETHTOOL_GRXCLSRULE:
1460 case ETHTOOL_GRXCLSRLALL:
2da45db2 1461 case ETHTOOL_GRXFHINDIR:
5455c699 1462 case ETHTOOL_GFEATURES:
2da45db2 1463 case ETHTOOL_GCHANNELS:
c8f3a8c3 1464 case ETHTOOL_GET_TS_INFO:
2da45db2 1465 case ETHTOOL_GEEE:
75f3123c
SH
1466 break;
1467 default:
5e1fccc0 1468 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
75f3123c
SH
1469 return -EPERM;
1470 }
1471
97f8aefb 1472 if (dev->ethtool_ops->begin) {
1473 rc = dev->ethtool_ops->begin(dev);
1474 if (rc < 0)
1da177e4 1475 return rc;
97f8aefb 1476 }
d8a33ac4
SH
1477 old_features = dev->features;
1478
1da177e4
LT
1479 switch (ethcmd) {
1480 case ETHTOOL_GSET:
1481 rc = ethtool_get_settings(dev, useraddr);
1482 break;
1483 case ETHTOOL_SSET:
1484 rc = ethtool_set_settings(dev, useraddr);
1485 break;
1486 case ETHTOOL_GDRVINFO:
1487 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
1488 break;
1489 case ETHTOOL_GREGS:
1490 rc = ethtool_get_regs(dev, useraddr);
1491 break;
1492 case ETHTOOL_GWOL:
1493 rc = ethtool_get_wol(dev, useraddr);
1494 break;
1495 case ETHTOOL_SWOL:
1496 rc = ethtool_set_wol(dev, useraddr);
1497 break;
1498 case ETHTOOL_GMSGLVL:
13c99b24
JG
1499 rc = ethtool_get_value(dev, useraddr, ethcmd,
1500 dev->ethtool_ops->get_msglevel);
1da177e4
LT
1501 break;
1502 case ETHTOOL_SMSGLVL:
13c99b24
JG
1503 rc = ethtool_set_value_void(dev, useraddr,
1504 dev->ethtool_ops->set_msglevel);
1da177e4 1505 break;
80f12ecc
YM
1506 case ETHTOOL_GEEE:
1507 rc = ethtool_get_eee(dev, useraddr);
1508 break;
1509 case ETHTOOL_SEEE:
1510 rc = ethtool_set_eee(dev, useraddr);
1511 break;
1da177e4
LT
1512 case ETHTOOL_NWAY_RST:
1513 rc = ethtool_nway_reset(dev);
1514 break;
1515 case ETHTOOL_GLINK:
e596e6e4 1516 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
1517 break;
1518 case ETHTOOL_GEEPROM:
1519 rc = ethtool_get_eeprom(dev, useraddr);
1520 break;
1521 case ETHTOOL_SEEPROM:
1522 rc = ethtool_set_eeprom(dev, useraddr);
1523 break;
1524 case ETHTOOL_GCOALESCE:
1525 rc = ethtool_get_coalesce(dev, useraddr);
1526 break;
1527 case ETHTOOL_SCOALESCE:
1528 rc = ethtool_set_coalesce(dev, useraddr);
1529 break;
1530 case ETHTOOL_GRINGPARAM:
1531 rc = ethtool_get_ringparam(dev, useraddr);
1532 break;
1533 case ETHTOOL_SRINGPARAM:
1534 rc = ethtool_set_ringparam(dev, useraddr);
1535 break;
1536 case ETHTOOL_GPAUSEPARAM:
1537 rc = ethtool_get_pauseparam(dev, useraddr);
1538 break;
1539 case ETHTOOL_SPAUSEPARAM:
1540 rc = ethtool_set_pauseparam(dev, useraddr);
1541 break;
1da177e4
LT
1542 case ETHTOOL_TEST:
1543 rc = ethtool_self_test(dev, useraddr);
1544 break;
1545 case ETHTOOL_GSTRINGS:
1546 rc = ethtool_get_strings(dev, useraddr);
1547 break;
1548 case ETHTOOL_PHYS_ID:
1549 rc = ethtool_phys_id(dev, useraddr);
1550 break;
1551 case ETHTOOL_GSTATS:
1552 rc = ethtool_get_stats(dev, useraddr);
1553 break;
a6f9a705
JW
1554 case ETHTOOL_GPERMADDR:
1555 rc = ethtool_get_perm_addr(dev, useraddr);
1556 break;
3ae7c0b2 1557 case ETHTOOL_GFLAGS:
13c99b24 1558 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 1559 __ethtool_get_flags);
3ae7c0b2
JG
1560 break;
1561 case ETHTOOL_SFLAGS:
da8ac86c 1562 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 1563 break;
339bf024 1564 case ETHTOOL_GPFLAGS:
13c99b24
JG
1565 rc = ethtool_get_value(dev, useraddr, ethcmd,
1566 dev->ethtool_ops->get_priv_flags);
339bf024
JG
1567 break;
1568 case ETHTOOL_SPFLAGS:
13c99b24
JG
1569 rc = ethtool_set_value(dev, useraddr,
1570 dev->ethtool_ops->set_priv_flags);
339bf024 1571 break;
0853ad66 1572 case ETHTOOL_GRXFH:
59089d8d
SB
1573 case ETHTOOL_GRXRINGS:
1574 case ETHTOOL_GRXCLSRLCNT:
1575 case ETHTOOL_GRXCLSRULE:
1576 case ETHTOOL_GRXCLSRLALL:
bf988435 1577 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
1578 break;
1579 case ETHTOOL_SRXFH:
59089d8d
SB
1580 case ETHTOOL_SRXCLSRLDEL:
1581 case ETHTOOL_SRXCLSRLINS:
bf988435 1582 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 1583 break;
05c6a8d7
AK
1584 case ETHTOOL_FLASHDEV:
1585 rc = ethtool_flash_device(dev, useraddr);
1586 break;
d73d3a8c
BH
1587 case ETHTOOL_RESET:
1588 rc = ethtool_reset(dev, useraddr);
1589 break;
723b2f57
JG
1590 case ETHTOOL_GSSET_INFO:
1591 rc = ethtool_get_sset_info(dev, useraddr);
1592 break;
a5b6ee29
BH
1593 case ETHTOOL_GRXFHINDIR:
1594 rc = ethtool_get_rxfh_indir(dev, useraddr);
1595 break;
1596 case ETHTOOL_SRXFHINDIR:
1597 rc = ethtool_set_rxfh_indir(dev, useraddr);
1598 break;
5455c699
MM
1599 case ETHTOOL_GFEATURES:
1600 rc = ethtool_get_features(dev, useraddr);
1601 break;
1602 case ETHTOOL_SFEATURES:
1603 rc = ethtool_set_features(dev, useraddr);
1604 break;
0a417704 1605 case ETHTOOL_GTXCSUM:
e83d360d 1606 case ETHTOOL_GRXCSUM:
0a417704
MM
1607 case ETHTOOL_GSG:
1608 case ETHTOOL_GTSO:
1609 case ETHTOOL_GUFO:
1610 case ETHTOOL_GGSO:
1611 case ETHTOOL_GGRO:
1612 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1613 break;
1614 case ETHTOOL_STXCSUM:
e83d360d 1615 case ETHTOOL_SRXCSUM:
0a417704
MM
1616 case ETHTOOL_SSG:
1617 case ETHTOOL_STSO:
1618 case ETHTOOL_SUFO:
1619 case ETHTOOL_SGSO:
1620 case ETHTOOL_SGRO:
1621 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1622 break;
8b5933c3 1623 case ETHTOOL_GCHANNELS:
1624 rc = ethtool_get_channels(dev, useraddr);
1625 break;
1626 case ETHTOOL_SCHANNELS:
1627 rc = ethtool_set_channels(dev, useraddr);
1628 break;
29dd54b7
AC
1629 case ETHTOOL_SET_DUMP:
1630 rc = ethtool_set_dump(dev, useraddr);
1631 break;
1632 case ETHTOOL_GET_DUMP_FLAG:
1633 rc = ethtool_get_dump_flag(dev, useraddr);
1634 break;
1635 case ETHTOOL_GET_DUMP_DATA:
1636 rc = ethtool_get_dump_data(dev, useraddr);
1637 break;
c8f3a8c3
RC
1638 case ETHTOOL_GET_TS_INFO:
1639 rc = ethtool_get_ts_info(dev, useraddr);
1640 break;
41c3cb6d
SH
1641 case ETHTOOL_GMODULEINFO:
1642 rc = ethtool_get_module_info(dev, useraddr);
1643 break;
1644 case ETHTOOL_GMODULEEEPROM:
1645 rc = ethtool_get_module_eeprom(dev, useraddr);
1646 break;
1da177e4 1647 default:
61a44b9c 1648 rc = -EOPNOTSUPP;
1da177e4 1649 }
4ec93edb 1650
e71a4783 1651 if (dev->ethtool_ops->complete)
1da177e4 1652 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
1653
1654 if (old_features != dev->features)
1655 netdev_features_change(dev);
1656
1da177e4 1657 return rc;
1da177e4 1658}