batman-adv: Fix list removal of batadv_hardif_neigh_node
[deliverable/linux.git] / net / core / ethtool.c
CommitLineData
1da177e4
LT
1/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
61a44b9c 6 * the information ethtool needs.
1da177e4 7 *
61a44b9c
MW
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/types.h>
4fc268d2 16#include <linux/capability.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
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>
960fb622 28#include <linux/net.h>
1da177e4 29
4ec93edb 30/*
1da177e4
LT
31 * Some useful ethtool_ops methods that're device independent.
32 * If we find that all drivers want to do the same thing here,
33 * we can turn these into dev_() function calls.
34 */
35
36u32 ethtool_op_get_link(struct net_device *dev)
37{
38 return netif_carrier_ok(dev) ? 1 : 0;
39}
97f8aefb 40EXPORT_SYMBOL(ethtool_op_get_link);
1da177e4 41
02eacbd0
RC
42int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
43{
44 info->so_timestamping =
45 SOF_TIMESTAMPING_TX_SOFTWARE |
46 SOF_TIMESTAMPING_RX_SOFTWARE |
47 SOF_TIMESTAMPING_SOFTWARE;
48 info->phc_index = -1;
49 return 0;
50}
51EXPORT_SYMBOL(ethtool_op_get_ts_info);
52
1da177e4
LT
53/* Handlers for each ethtool command */
54
475414f6 55#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
5455c699 56
9d921549
MM
57static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
58 [NETIF_F_SG_BIT] = "tx-scatter-gather",
59 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
9d921549
MM
60 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
61 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
62 [NETIF_F_HIGHDMA_BIT] = "highdma",
63 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
788dfcac 64 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
9d921549 65
788dfcac
FLVC
66 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
2d6577f1
DM
68 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
69 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
70 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
9d921549
MM
71 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
72 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
73 [NETIF_F_LLTX_BIT] = "tx-lockless",
74 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
75 [NETIF_F_GRO_BIT] = "rx-gro",
76 [NETIF_F_LRO_BIT] = "rx-lro",
77
78 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
79 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
80 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
81 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
82 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
83 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
68c33163 84 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
cb32f511 85 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
61c1db7f 86 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
73136267 87 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
9d921549
MM
88
89 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
53692b1d 90 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
9d921549
MM
91 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
92 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
93 [NETIF_F_RXHASH_BIT] = "rx-hashing",
94 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
95 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
96 [NETIF_F_LOOPBACK_BIT] = "loopback",
36eabda3 97 [NETIF_F_RXFCS_BIT] = "rx-fcs",
5e0c03c8 98 [NETIF_F_RXALL_BIT] = "rx-all",
a6cc0cfa 99 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
d0290214 100 [NETIF_F_BUSY_POLL_BIT] = "busy-poll",
9d921549
MM
101};
102
892311f6
EP
103static const char
104rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
105 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
106 [ETH_RSS_HASH_XOR_BIT] = "xor",
107};
108
a4244b0c
HHZ
109static const char
110tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
111 [ETHTOOL_ID_UNSPEC] = "Unspec",
112 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
113 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
114};
115
5455c699
MM
116static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
117{
118 struct ethtool_gfeatures cmd = {
119 .cmd = ETHTOOL_GFEATURES,
120 .size = ETHTOOL_DEV_FEATURE_WORDS,
121 };
475414f6 122 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
5455c699
MM
123 u32 __user *sizeaddr;
124 u32 copy_size;
475414f6
MM
125 int i;
126
127 /* in case feature bits run out again */
09da71b1 128 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
475414f6
MM
129
130 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
131 features[i].available = (u32)(dev->hw_features >> (32 * i));
132 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
133 features[i].active = (u32)(dev->features >> (32 * i));
134 features[i].never_changed =
135 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
475414f6 136 }
5455c699
MM
137
138 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
139 if (get_user(copy_size, sizeaddr))
140 return -EFAULT;
141
142 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
143 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
144
145 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
146 return -EFAULT;
147 useraddr += sizeof(cmd);
148 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
149 return -EFAULT;
150
151 return 0;
152}
153
154static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
155{
156 struct ethtool_sfeatures cmd;
157 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
475414f6
MM
158 netdev_features_t wanted = 0, valid = 0;
159 int i, ret = 0;
5455c699
MM
160
161 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
162 return -EFAULT;
163 useraddr += sizeof(cmd);
164
165 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
166 return -EINVAL;
167
168 if (copy_from_user(features, useraddr, sizeof(features)))
169 return -EFAULT;
170
475414f6 171 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
172 valid |= (netdev_features_t)features[i].valid << (32 * i);
173 wanted |= (netdev_features_t)features[i].requested << (32 * i);
475414f6
MM
174 }
175
176 if (valid & ~NETIF_F_ETHTOOL_BITS)
5455c699
MM
177 return -EINVAL;
178
475414f6
MM
179 if (valid & ~dev->hw_features) {
180 valid &= dev->hw_features;
5455c699
MM
181 ret |= ETHTOOL_F_UNSUPPORTED;
182 }
183
475414f6
MM
184 dev->wanted_features &= ~valid;
185 dev->wanted_features |= wanted & valid;
6cb6a27c 186 __netdev_update_features(dev);
5455c699 187
475414f6 188 if ((dev->wanted_features ^ dev->features) & valid)
5455c699
MM
189 ret |= ETHTOOL_F_WISH;
190
191 return ret;
192}
193
f3a40945
AL
194static int phy_get_sset_count(struct phy_device *phydev)
195{
196 int ret;
197
198 if (phydev->drv->get_sset_count &&
199 phydev->drv->get_strings &&
200 phydev->drv->get_stats) {
201 mutex_lock(&phydev->lock);
202 ret = phydev->drv->get_sset_count(phydev);
203 mutex_unlock(&phydev->lock);
204
205 return ret;
206 }
207
208 return -EOPNOTSUPP;
209}
210
340ae165
MM
211static int __ethtool_get_sset_count(struct net_device *dev, int sset)
212{
213 const struct ethtool_ops *ops = dev->ethtool_ops;
214
5455c699
MM
215 if (sset == ETH_SS_FEATURES)
216 return ARRAY_SIZE(netdev_features_strings);
217
892311f6
EP
218 if (sset == ETH_SS_RSS_HASH_FUNCS)
219 return ARRAY_SIZE(rss_hash_func_strings);
220
a4244b0c
HHZ
221 if (sset == ETH_SS_TUNABLES)
222 return ARRAY_SIZE(tunable_strings);
223
f3a40945
AL
224 if (sset == ETH_SS_PHY_STATS) {
225 if (dev->phydev)
226 return phy_get_sset_count(dev->phydev);
227 else
228 return -EOPNOTSUPP;
229 }
230
c03a14e8 231 if (ops->get_sset_count && ops->get_strings)
340ae165
MM
232 return ops->get_sset_count(dev, sset);
233 else
234 return -EOPNOTSUPP;
235}
236
237static void __ethtool_get_strings(struct net_device *dev,
238 u32 stringset, u8 *data)
239{
240 const struct ethtool_ops *ops = dev->ethtool_ops;
241
5455c699
MM
242 if (stringset == ETH_SS_FEATURES)
243 memcpy(data, netdev_features_strings,
244 sizeof(netdev_features_strings));
892311f6
EP
245 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
246 memcpy(data, rss_hash_func_strings,
247 sizeof(rss_hash_func_strings));
a4244b0c
HHZ
248 else if (stringset == ETH_SS_TUNABLES)
249 memcpy(data, tunable_strings, sizeof(tunable_strings));
f3a40945
AL
250 else if (stringset == ETH_SS_PHY_STATS) {
251 struct phy_device *phydev = dev->phydev;
252
253 if (phydev) {
254 mutex_lock(&phydev->lock);
255 phydev->drv->get_strings(phydev, data);
256 mutex_unlock(&phydev->lock);
257 } else {
258 return;
259 }
260 } else
5455c699
MM
261 /* ops->get_strings is valid because checked earlier */
262 ops->get_strings(dev, stringset, data);
340ae165
MM
263}
264
c8f44aff 265static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0a417704
MM
266{
267 /* feature masks of legacy discrete ethtool ops */
268
269 switch (eth_cmd) {
270 case ETHTOOL_GTXCSUM:
271 case ETHTOOL_STXCSUM:
a188222b 272 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
e83d360d
MM
273 case ETHTOOL_GRXCSUM:
274 case ETHTOOL_SRXCSUM:
275 return NETIF_F_RXCSUM;
0a417704
MM
276 case ETHTOOL_GSG:
277 case ETHTOOL_SSG:
278 return NETIF_F_SG;
279 case ETHTOOL_GTSO:
280 case ETHTOOL_STSO:
281 return NETIF_F_ALL_TSO;
282 case ETHTOOL_GUFO:
283 case ETHTOOL_SUFO:
284 return NETIF_F_UFO;
285 case ETHTOOL_GGSO:
286 case ETHTOOL_SGSO:
287 return NETIF_F_GSO;
288 case ETHTOOL_GGRO:
289 case ETHTOOL_SGRO:
290 return NETIF_F_GRO;
291 default:
292 BUG();
293 }
294}
295
0a417704
MM
296static int ethtool_get_one_feature(struct net_device *dev,
297 char __user *useraddr, u32 ethcmd)
298{
c8f44aff 299 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0a417704
MM
300 struct ethtool_value edata = {
301 .cmd = ethcmd,
86794881 302 .data = !!(dev->features & mask),
0a417704 303 };
0a417704 304
0a417704
MM
305 if (copy_to_user(useraddr, &edata, sizeof(edata)))
306 return -EFAULT;
307 return 0;
308}
309
0a417704
MM
310static int ethtool_set_one_feature(struct net_device *dev,
311 void __user *useraddr, u32 ethcmd)
312{
313 struct ethtool_value edata;
c8f44aff 314 netdev_features_t mask;
0a417704
MM
315
316 if (copy_from_user(&edata, useraddr, sizeof(edata)))
317 return -EFAULT;
318
86794881
MM
319 mask = ethtool_get_feature_mask(ethcmd);
320 mask &= dev->hw_features;
bc5787c6
MM
321 if (!mask)
322 return -EOPNOTSUPP;
86794881 323
bc5787c6
MM
324 if (edata.data)
325 dev->wanted_features |= mask;
326 else
327 dev->wanted_features &= ~mask;
86794881 328
bc5787c6 329 __netdev_update_features(dev);
86794881 330
bc5787c6
MM
331 return 0;
332}
333
02b3a552
MM
334#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
335 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
f646968f
PM
336#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
337 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
338 NETIF_F_RXHASH)
bc5787c6
MM
339
340static u32 __ethtool_get_flags(struct net_device *dev)
341{
02b3a552
MM
342 u32 flags = 0;
343
8a73125c
DF
344 if (dev->features & NETIF_F_LRO)
345 flags |= ETH_FLAG_LRO;
346 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
347 flags |= ETH_FLAG_RXVLAN;
348 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
349 flags |= ETH_FLAG_TXVLAN;
350 if (dev->features & NETIF_F_NTUPLE)
351 flags |= ETH_FLAG_NTUPLE;
352 if (dev->features & NETIF_F_RXHASH)
353 flags |= ETH_FLAG_RXHASH;
02b3a552
MM
354
355 return flags;
0a417704
MM
356}
357
bc5787c6 358static int __ethtool_set_flags(struct net_device *dev, u32 data)
da8ac86c 359{
c8f44aff 360 netdev_features_t features = 0, changed;
da8ac86c 361
02b3a552 362 if (data & ~ETH_ALL_FLAGS)
da8ac86c
MM
363 return -EINVAL;
364
8a73125c
DF
365 if (data & ETH_FLAG_LRO)
366 features |= NETIF_F_LRO;
367 if (data & ETH_FLAG_RXVLAN)
368 features |= NETIF_F_HW_VLAN_CTAG_RX;
369 if (data & ETH_FLAG_TXVLAN)
370 features |= NETIF_F_HW_VLAN_CTAG_TX;
371 if (data & ETH_FLAG_NTUPLE)
372 features |= NETIF_F_NTUPLE;
373 if (data & ETH_FLAG_RXHASH)
374 features |= NETIF_F_RXHASH;
02b3a552 375
da8ac86c 376 /* allow changing only bits set in hw_features */
02b3a552 377 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
da8ac86c
MM
378 if (changed & ~dev->hw_features)
379 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
380
381 dev->wanted_features =
02b3a552 382 (dev->wanted_features & ~changed) | (features & changed);
da8ac86c 383
6cb6a27c 384 __netdev_update_features(dev);
da8ac86c
MM
385
386 return 0;
387}
388
4bc71cb9 389int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1da177e4 390{
4bc71cb9 391 ASSERT_RTNL();
1da177e4 392
c03a14e8 393 if (!dev->ethtool_ops->get_settings)
1da177e4
LT
394 return -EOPNOTSUPP;
395
4bc71cb9
JP
396 memset(cmd, 0, sizeof(struct ethtool_cmd));
397 cmd->cmd = ETHTOOL_GSET;
398 return dev->ethtool_ops->get_settings(dev, cmd);
399}
400EXPORT_SYMBOL(__ethtool_get_settings);
401
402static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
403{
404 int err;
405 struct ethtool_cmd cmd;
406
18ec898e 407 err = __ethtool_get_settings(dev, &cmd);
1da177e4
LT
408 if (err < 0)
409 return err;
410
411 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
412 return -EFAULT;
413 return 0;
414}
415
416static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
417{
418 struct ethtool_cmd cmd;
419
420 if (!dev->ethtool_ops->set_settings)
421 return -EOPNOTSUPP;
422
423 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
424 return -EFAULT;
425
426 return dev->ethtool_ops->set_settings(dev, &cmd);
427}
428
97f8aefb 429static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
430 void __user *useraddr)
1da177e4
LT
431{
432 struct ethtool_drvinfo info;
76fd8593 433 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 434
1da177e4
LT
435 memset(&info, 0, sizeof(info));
436 info.cmd = ETHTOOL_GDRVINFO;
c03a14e8 437 if (ops->get_drvinfo) {
01414802
BH
438 ops->get_drvinfo(dev, &info);
439 } else if (dev->dev.parent && dev->dev.parent->driver) {
440 strlcpy(info.bus_info, dev_name(dev->dev.parent),
441 sizeof(info.bus_info));
442 strlcpy(info.driver, dev->dev.parent->driver->name,
443 sizeof(info.driver));
444 } else {
445 return -EOPNOTSUPP;
446 }
1da177e4 447
723b2f57
JG
448 /*
449 * this method of obtaining string set info is deprecated;
d17792eb 450 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 451 */
c03a14e8 452 if (ops->get_sset_count) {
ff03d49f
JG
453 int rc;
454
455 rc = ops->get_sset_count(dev, ETH_SS_TEST);
456 if (rc >= 0)
457 info.testinfo_len = rc;
458 rc = ops->get_sset_count(dev, ETH_SS_STATS);
459 if (rc >= 0)
460 info.n_stats = rc;
339bf024
JG
461 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
462 if (rc >= 0)
463 info.n_priv_flags = rc;
ff03d49f 464 }
c03a14e8 465 if (ops->get_regs_len)
1da177e4 466 info.regdump_len = ops->get_regs_len(dev);
c03a14e8 467 if (ops->get_eeprom_len)
1da177e4
LT
468 info.eedump_len = ops->get_eeprom_len(dev);
469
470 if (copy_to_user(useraddr, &info, sizeof(info)))
471 return -EFAULT;
472 return 0;
473}
474
f5c445ed 475static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 476 void __user *useraddr)
723b2f57
JG
477{
478 struct ethtool_sset_info info;
723b2f57
JG
479 u64 sset_mask;
480 int i, idx = 0, n_bits = 0, ret, rc;
481 u32 *info_buf = NULL;
482
723b2f57
JG
483 if (copy_from_user(&info, useraddr, sizeof(info)))
484 return -EFAULT;
485
486 /* store copy of mask, because we zero struct later on */
487 sset_mask = info.sset_mask;
488 if (!sset_mask)
489 return 0;
490
491 /* calculate size of return buffer */
d17792eb 492 n_bits = hweight64(sset_mask);
723b2f57
JG
493
494 memset(&info, 0, sizeof(info));
495 info.cmd = ETHTOOL_GSSET_INFO;
496
497 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
498 if (!info_buf)
499 return -ENOMEM;
500
501 /*
502 * fill return buffer based on input bitmask and successful
503 * get_sset_count return
504 */
505 for (i = 0; i < 64; i++) {
506 if (!(sset_mask & (1ULL << i)))
507 continue;
508
340ae165 509 rc = __ethtool_get_sset_count(dev, i);
723b2f57
JG
510 if (rc >= 0) {
511 info.sset_mask |= (1ULL << i);
512 info_buf[idx++] = rc;
513 }
514 }
515
516 ret = -EFAULT;
517 if (copy_to_user(useraddr, &info, sizeof(info)))
518 goto out;
519
520 useraddr += offsetof(struct ethtool_sset_info, data);
521 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
522 goto out;
523
524 ret = 0;
525
526out:
527 kfree(info_buf);
528 return ret;
529}
530
97f8aefb 531static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
bf988435 532 u32 cmd, void __user *useraddr)
0853ad66 533{
bf988435
BH
534 struct ethtool_rxnfc info;
535 size_t info_size = sizeof(info);
55664f32 536 int rc;
0853ad66 537
59089d8d 538 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
539 return -EOPNOTSUPP;
540
bf988435
BH
541 /* struct ethtool_rxnfc was originally defined for
542 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
543 * members. User-space might still be using that
544 * definition. */
545 if (cmd == ETHTOOL_SRXFH)
546 info_size = (offsetof(struct ethtool_rxnfc, data) +
547 sizeof(info.data));
548
549 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
550 return -EFAULT;
551
55664f32
BH
552 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
553 if (rc)
554 return rc;
555
556 if (cmd == ETHTOOL_SRXCLSRLINS &&
557 copy_to_user(useraddr, &info, info_size))
558 return -EFAULT;
559
560 return 0;
0853ad66
SB
561}
562
97f8aefb 563static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
bf988435 564 u32 cmd, void __user *useraddr)
0853ad66
SB
565{
566 struct ethtool_rxnfc info;
bf988435 567 size_t info_size = sizeof(info);
59089d8d
SB
568 const struct ethtool_ops *ops = dev->ethtool_ops;
569 int ret;
570 void *rule_buf = NULL;
0853ad66 571
59089d8d 572 if (!ops->get_rxnfc)
0853ad66
SB
573 return -EOPNOTSUPP;
574
bf988435
BH
575 /* struct ethtool_rxnfc was originally defined for
576 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
577 * members. User-space might still be using that
578 * definition. */
579 if (cmd == ETHTOOL_GRXFH)
580 info_size = (offsetof(struct ethtool_rxnfc, data) +
581 sizeof(info.data));
582
583 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
584 return -EFAULT;
585
59089d8d
SB
586 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
587 if (info.rule_cnt > 0) {
db048b69 588 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
ae6df5f9 589 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
db048b69 590 GFP_USER);
59089d8d
SB
591 if (!rule_buf)
592 return -ENOMEM;
593 }
594 }
0853ad66 595
59089d8d
SB
596 ret = ops->get_rxnfc(dev, &info, rule_buf);
597 if (ret < 0)
598 goto err_out;
599
600 ret = -EFAULT;
bf988435 601 if (copy_to_user(useraddr, &info, info_size))
59089d8d
SB
602 goto err_out;
603
604 if (rule_buf) {
605 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
606 if (copy_to_user(useraddr, rule_buf,
607 info.rule_cnt * sizeof(u32)))
608 goto err_out;
609 }
610 ret = 0;
611
612err_out:
c9caceca 613 kfree(rule_buf);
59089d8d
SB
614
615 return ret;
0853ad66
SB
616}
617
3de0b592
VD
618static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
619 struct ethtool_rxnfc *rx_rings,
620 u32 size)
621{
fb95cd8d 622 int i;
3de0b592
VD
623
624 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
fb95cd8d 625 return -EFAULT;
3de0b592
VD
626
627 /* Validate ring indices */
fb95cd8d
BH
628 for (i = 0; i < size; i++)
629 if (indir[i] >= rx_rings->data)
630 return -EINVAL;
631
632 return 0;
3de0b592
VD
633}
634
960fb622
ED
635u8 netdev_rss_key[NETDEV_RSS_KEY_LEN];
636
637void netdev_rss_key_fill(void *buffer, size_t len)
638{
639 BUG_ON(len > sizeof(netdev_rss_key));
640 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
641 memcpy(buffer, netdev_rss_key, len);
642}
643EXPORT_SYMBOL(netdev_rss_key_fill);
644
a5b6ee29
BH
645static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
646 void __user *useraddr)
647{
7850f63f
BH
648 u32 user_size, dev_size;
649 u32 *indir;
a5b6ee29
BH
650 int ret;
651
7850f63f 652 if (!dev->ethtool_ops->get_rxfh_indir_size ||
fe62d001 653 !dev->ethtool_ops->get_rxfh)
7850f63f
BH
654 return -EOPNOTSUPP;
655 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
656 if (dev_size == 0)
a5b6ee29
BH
657 return -EOPNOTSUPP;
658
7850f63f 659 if (copy_from_user(&user_size,
a5b6ee29 660 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 661 sizeof(user_size)))
a5b6ee29
BH
662 return -EFAULT;
663
7850f63f
BH
664 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
665 &dev_size, sizeof(dev_size)))
666 return -EFAULT;
667
668 /* If the user buffer size is 0, this is just a query for the
669 * device table size. Otherwise, if it's smaller than the
670 * device table size it's an error.
671 */
672 if (user_size < dev_size)
673 return user_size == 0 ? 0 : -EINVAL;
674
675 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
676 if (!indir)
677 return -ENOMEM;
678
892311f6 679 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
a5b6ee29
BH
680 if (ret)
681 goto out;
682
7850f63f
BH
683 if (copy_to_user(useraddr +
684 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
685 indir, dev_size * sizeof(indir[0])))
a5b6ee29
BH
686 ret = -EFAULT;
687
688out:
689 kfree(indir);
690 return ret;
691}
692
693static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
694 void __user *useraddr)
695{
7850f63f
BH
696 struct ethtool_rxnfc rx_rings;
697 u32 user_size, dev_size, i;
698 u32 *indir;
c03a14e8 699 const struct ethtool_ops *ops = dev->ethtool_ops;
a5b6ee29 700 int ret;
3de0b592 701 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
a5b6ee29 702
fe62d001 703 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
c03a14e8 704 !ops->get_rxnfc)
7850f63f 705 return -EOPNOTSUPP;
c03a14e8
JP
706
707 dev_size = ops->get_rxfh_indir_size(dev);
7850f63f 708 if (dev_size == 0)
a5b6ee29
BH
709 return -EOPNOTSUPP;
710
7850f63f 711 if (copy_from_user(&user_size,
a5b6ee29 712 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 713 sizeof(user_size)))
a5b6ee29
BH
714 return -EFAULT;
715
278bc429 716 if (user_size != 0 && user_size != dev_size)
7850f63f
BH
717 return -EINVAL;
718
719 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
720 if (!indir)
721 return -ENOMEM;
722
7850f63f 723 rx_rings.cmd = ETHTOOL_GRXRINGS;
c03a14e8 724 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
7850f63f
BH
725 if (ret)
726 goto out;
278bc429
BH
727
728 if (user_size == 0) {
729 for (i = 0; i < dev_size; i++)
730 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
731 } else {
3de0b592
VD
732 ret = ethtool_copy_validate_indir(indir,
733 useraddr + ringidx_offset,
734 &rx_rings,
735 dev_size);
736 if (ret)
737 goto out;
738 }
739
892311f6 740 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
3de0b592
VD
741
742out:
743 kfree(indir);
744 return ret;
745}
746
747static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
748 void __user *useraddr)
749{
750 int ret;
751 const struct ethtool_ops *ops = dev->ethtool_ops;
f062a384 752 u32 user_indir_size, user_key_size;
3de0b592 753 u32 dev_indir_size = 0, dev_key_size = 0;
f062a384 754 struct ethtool_rxfh rxfh;
3de0b592 755 u32 total_size;
f062a384 756 u32 indir_bytes;
3de0b592 757 u32 *indir = NULL;
892311f6 758 u8 dev_hfunc = 0;
3de0b592
VD
759 u8 *hkey = NULL;
760 u8 *rss_config;
761
892311f6 762 if (!ops->get_rxfh)
3de0b592
VD
763 return -EOPNOTSUPP;
764
765 if (ops->get_rxfh_indir_size)
766 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592
VD
767 if (ops->get_rxfh_key_size)
768 dev_key_size = ops->get_rxfh_key_size(dev);
769
f062a384 770 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592 771 return -EFAULT;
f062a384
BH
772 user_indir_size = rxfh.indir_size;
773 user_key_size = rxfh.key_size;
3de0b592 774
f062a384 775 /* Check that reserved fields are 0 for now */
892311f6
EP
776 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
777 rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384
BH
778 return -EINVAL;
779
780 rxfh.indir_size = dev_indir_size;
781 rxfh.key_size = dev_key_size;
782 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
3de0b592
VD
783 return -EFAULT;
784
3de0b592
VD
785 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
786 (user_key_size && (user_key_size != dev_key_size)))
787 return -EINVAL;
788
789 indir_bytes = user_indir_size * sizeof(indir[0]);
790 total_size = indir_bytes + user_key_size;
791 rss_config = kzalloc(total_size, GFP_USER);
792 if (!rss_config)
793 return -ENOMEM;
794
795 if (user_indir_size)
796 indir = (u32 *)rss_config;
797
798 if (user_key_size)
799 hkey = rss_config + indir_bytes;
800
892311f6
EP
801 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
802 if (ret)
803 goto out;
3de0b592 804
892311f6
EP
805 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
806 &dev_hfunc, sizeof(rxfh.hfunc))) {
807 ret = -EFAULT;
808 } else if (copy_to_user(useraddr +
809 offsetof(struct ethtool_rxfh, rss_config[0]),
810 rss_config, total_size)) {
811 ret = -EFAULT;
812 }
813out:
3de0b592
VD
814 kfree(rss_config);
815
816 return ret;
817}
818
819static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
820 void __user *useraddr)
821{
822 int ret;
823 const struct ethtool_ops *ops = dev->ethtool_ops;
824 struct ethtool_rxnfc rx_rings;
f062a384
BH
825 struct ethtool_rxfh rxfh;
826 u32 dev_indir_size = 0, dev_key_size = 0, i;
3de0b592
VD
827 u32 *indir = NULL, indir_bytes = 0;
828 u8 *hkey = NULL;
829 u8 *rss_config;
3de0b592
VD
830 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
831
892311f6 832 if (!ops->get_rxnfc || !ops->set_rxfh)
3de0b592
VD
833 return -EOPNOTSUPP;
834
835 if (ops->get_rxfh_indir_size)
836 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592 837 if (ops->get_rxfh_key_size)
d340c862 838 dev_key_size = ops->get_rxfh_key_size(dev);
3de0b592 839
f062a384 840 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592
VD
841 return -EFAULT;
842
f062a384 843 /* Check that reserved fields are 0 for now */
892311f6
EP
844 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
845 rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384
BH
846 return -EINVAL;
847
892311f6
EP
848 /* If either indir, hash key or function is valid, proceed further.
849 * Must request at least one change: indir size, hash key or function.
3de0b592 850 */
f062a384
BH
851 if ((rxfh.indir_size &&
852 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
853 rxfh.indir_size != dev_indir_size) ||
854 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
855 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
892311f6 856 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
3de0b592
VD
857 return -EINVAL;
858
f062a384 859 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
3de0b592
VD
860 indir_bytes = dev_indir_size * sizeof(indir[0]);
861
f062a384 862 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
3de0b592
VD
863 if (!rss_config)
864 return -ENOMEM;
865
866 rx_rings.cmd = ETHTOOL_GRXRINGS;
867 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
868 if (ret)
869 goto out;
870
f062a384
BH
871 /* rxfh.indir_size == 0 means reset the indir table to default.
872 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
3de0b592 873 */
f062a384
BH
874 if (rxfh.indir_size &&
875 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
3de0b592
VD
876 indir = (u32 *)rss_config;
877 ret = ethtool_copy_validate_indir(indir,
878 useraddr + rss_cfg_offset,
879 &rx_rings,
f062a384 880 rxfh.indir_size);
3de0b592 881 if (ret)
7850f63f 882 goto out;
f062a384 883 } else if (rxfh.indir_size == 0) {
3de0b592
VD
884 indir = (u32 *)rss_config;
885 for (i = 0; i < dev_indir_size; i++)
886 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
887 }
278bc429 888
f062a384 889 if (rxfh.key_size) {
3de0b592
VD
890 hkey = rss_config + indir_bytes;
891 if (copy_from_user(hkey,
892 useraddr + rss_cfg_offset + indir_bytes,
f062a384 893 rxfh.key_size)) {
3de0b592
VD
894 ret = -EFAULT;
895 goto out;
278bc429 896 }
7850f63f
BH
897 }
898
892311f6 899 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
a5b6ee29
BH
900
901out:
3de0b592 902 kfree(rss_config);
a5b6ee29
BH
903 return ret;
904}
905
1da177e4
LT
906static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
907{
908 struct ethtool_regs regs;
76fd8593 909 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
910 void *regbuf;
911 int reglen, ret;
912
913 if (!ops->get_regs || !ops->get_regs_len)
914 return -EOPNOTSUPP;
915
916 if (copy_from_user(&regs, useraddr, sizeof(regs)))
917 return -EFAULT;
918
919 reglen = ops->get_regs_len(dev);
920 if (regs.len > reglen)
921 regs.len = reglen;
922
b7c7d01a 923 regbuf = vzalloc(reglen);
67ae7cf1 924 if (reglen && !regbuf)
1da177e4
LT
925 return -ENOMEM;
926
927 ops->get_regs(dev, &regs, regbuf);
928
929 ret = -EFAULT;
930 if (copy_to_user(useraddr, &regs, sizeof(regs)))
931 goto out;
932 useraddr += offsetof(struct ethtool_regs, data);
67ae7cf1 933 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1da177e4
LT
934 goto out;
935 ret = 0;
936
937 out:
a77f5db3 938 vfree(regbuf);
1da177e4
LT
939 return ret;
940}
941
d73d3a8c
BH
942static int ethtool_reset(struct net_device *dev, char __user *useraddr)
943{
944 struct ethtool_value reset;
945 int ret;
946
947 if (!dev->ethtool_ops->reset)
948 return -EOPNOTSUPP;
949
950 if (copy_from_user(&reset, useraddr, sizeof(reset)))
951 return -EFAULT;
952
953 ret = dev->ethtool_ops->reset(dev, &reset.data);
954 if (ret)
955 return ret;
956
957 if (copy_to_user(useraddr, &reset, sizeof(reset)))
958 return -EFAULT;
959 return 0;
960}
961
1da177e4
LT
962static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
963{
8e557421 964 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1da177e4
LT
965
966 if (!dev->ethtool_ops->get_wol)
967 return -EOPNOTSUPP;
968
969 dev->ethtool_ops->get_wol(dev, &wol);
970
971 if (copy_to_user(useraddr, &wol, sizeof(wol)))
972 return -EFAULT;
973 return 0;
974}
975
976static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
977{
978 struct ethtool_wolinfo wol;
979
980 if (!dev->ethtool_ops->set_wol)
981 return -EOPNOTSUPP;
982
983 if (copy_from_user(&wol, useraddr, sizeof(wol)))
984 return -EFAULT;
985
986 return dev->ethtool_ops->set_wol(dev, &wol);
987}
988
80f12ecc
YM
989static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
990{
991 struct ethtool_eee edata;
992 int rc;
993
994 if (!dev->ethtool_ops->get_eee)
995 return -EOPNOTSUPP;
996
997 memset(&edata, 0, sizeof(struct ethtool_eee));
998 edata.cmd = ETHTOOL_GEEE;
999 rc = dev->ethtool_ops->get_eee(dev, &edata);
1000
1001 if (rc)
1002 return rc;
1003
1004 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1005 return -EFAULT;
1006
1007 return 0;
1008}
1009
1010static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1011{
1012 struct ethtool_eee edata;
1013
1014 if (!dev->ethtool_ops->set_eee)
1015 return -EOPNOTSUPP;
1016
1017 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1018 return -EFAULT;
1019
1020 return dev->ethtool_ops->set_eee(dev, &edata);
1021}
1022
1da177e4
LT
1023static int ethtool_nway_reset(struct net_device *dev)
1024{
1025 if (!dev->ethtool_ops->nway_reset)
1026 return -EOPNOTSUPP;
1027
1028 return dev->ethtool_ops->nway_reset(dev);
1029}
1030
e596e6e4
BH
1031static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1032{
1033 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1034
1035 if (!dev->ethtool_ops->get_link)
1036 return -EOPNOTSUPP;
1037
1038 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1039
1040 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1041 return -EFAULT;
1042 return 0;
1043}
1044
081d094e
BH
1045static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1046 int (*getter)(struct net_device *,
1047 struct ethtool_eeprom *, u8 *),
1048 u32 total_len)
1da177e4
LT
1049{
1050 struct ethtool_eeprom eeprom;
b131dd5d
MSB
1051 void __user *userbuf = useraddr + sizeof(eeprom);
1052 u32 bytes_remaining;
1da177e4 1053 u8 *data;
b131dd5d 1054 int ret = 0;
1da177e4 1055
1da177e4
LT
1056 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1057 return -EFAULT;
1058
1059 /* Check for wrap and zero */
1060 if (eeprom.offset + eeprom.len <= eeprom.offset)
1061 return -EINVAL;
1062
1063 /* Check for exceeding total eeprom len */
081d094e 1064 if (eeprom.offset + eeprom.len > total_len)
1da177e4
LT
1065 return -EINVAL;
1066
b131dd5d 1067 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1068 if (!data)
1069 return -ENOMEM;
1070
b131dd5d
MSB
1071 bytes_remaining = eeprom.len;
1072 while (bytes_remaining > 0) {
1073 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1074
081d094e 1075 ret = getter(dev, &eeprom, data);
b131dd5d
MSB
1076 if (ret)
1077 break;
1078 if (copy_to_user(userbuf, data, eeprom.len)) {
1079 ret = -EFAULT;
1080 break;
1081 }
1082 userbuf += eeprom.len;
1083 eeprom.offset += eeprom.len;
1084 bytes_remaining -= eeprom.len;
1085 }
1da177e4 1086
c5835df9
MSB
1087 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1088 eeprom.offset -= eeprom.len;
1089 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1090 ret = -EFAULT;
1091
1da177e4
LT
1092 kfree(data);
1093 return ret;
1094}
1095
081d094e
BH
1096static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1097{
1098 const struct ethtool_ops *ops = dev->ethtool_ops;
1099
e0fb6fb6
GR
1100 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1101 !ops->get_eeprom_len(dev))
081d094e
BH
1102 return -EOPNOTSUPP;
1103
1104 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1105 ops->get_eeprom_len(dev));
1106}
1107
1da177e4
LT
1108static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1109{
1110 struct ethtool_eeprom eeprom;
76fd8593 1111 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
1112 void __user *userbuf = useraddr + sizeof(eeprom);
1113 u32 bytes_remaining;
1da177e4 1114 u8 *data;
b131dd5d 1115 int ret = 0;
1da177e4 1116
e0fb6fb6
GR
1117 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1118 !ops->get_eeprom_len(dev))
1da177e4
LT
1119 return -EOPNOTSUPP;
1120
1121 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1122 return -EFAULT;
1123
1124 /* Check for wrap and zero */
1125 if (eeprom.offset + eeprom.len <= eeprom.offset)
1126 return -EINVAL;
1127
1128 /* Check for exceeding total eeprom len */
1129 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1130 return -EINVAL;
1131
b131dd5d 1132 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1133 if (!data)
1134 return -ENOMEM;
1135
b131dd5d
MSB
1136 bytes_remaining = eeprom.len;
1137 while (bytes_remaining > 0) {
1138 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1139
1140 if (copy_from_user(data, userbuf, eeprom.len)) {
1141 ret = -EFAULT;
1142 break;
1143 }
1144 ret = ops->set_eeprom(dev, &eeprom, data);
1145 if (ret)
1146 break;
1147 userbuf += eeprom.len;
1148 eeprom.offset += eeprom.len;
1149 bytes_remaining -= eeprom.len;
1150 }
1da177e4 1151
1da177e4
LT
1152 kfree(data);
1153 return ret;
1154}
1155
97f8aefb 1156static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1157 void __user *useraddr)
1da177e4 1158{
8e557421 1159 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
1160
1161 if (!dev->ethtool_ops->get_coalesce)
1162 return -EOPNOTSUPP;
1163
1164 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1165
1166 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1167 return -EFAULT;
1168 return 0;
1169}
1170
97f8aefb 1171static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1172 void __user *useraddr)
1da177e4
LT
1173{
1174 struct ethtool_coalesce coalesce;
1175
fa04ae5c 1176 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
1177 return -EOPNOTSUPP;
1178
1179 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1180 return -EFAULT;
1181
1182 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1183}
1184
1185static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1186{
8e557421 1187 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
1188
1189 if (!dev->ethtool_ops->get_ringparam)
1190 return -EOPNOTSUPP;
1191
1192 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1193
1194 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1195 return -EFAULT;
1196 return 0;
1197}
1198
1199static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1200{
1201 struct ethtool_ringparam ringparam;
1202
1203 if (!dev->ethtool_ops->set_ringparam)
1204 return -EOPNOTSUPP;
1205
1206 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1207 return -EFAULT;
1208
1209 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1210}
1211
8b5933c3 1212static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1213 void __user *useraddr)
1214{
1215 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1216
1217 if (!dev->ethtool_ops->get_channels)
1218 return -EOPNOTSUPP;
1219
1220 dev->ethtool_ops->get_channels(dev, &channels);
1221
1222 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1223 return -EFAULT;
1224 return 0;
1225}
1226
1227static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1228 void __user *useraddr)
1229{
1230 struct ethtool_channels channels;
1231
1232 if (!dev->ethtool_ops->set_channels)
1233 return -EOPNOTSUPP;
1234
1235 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1236 return -EFAULT;
1237
1238 return dev->ethtool_ops->set_channels(dev, &channels);
1239}
1240
1da177e4
LT
1241static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1242{
1243 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1244
1245 if (!dev->ethtool_ops->get_pauseparam)
1246 return -EOPNOTSUPP;
1247
1248 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1249
1250 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1251 return -EFAULT;
1252 return 0;
1253}
1254
1255static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1256{
1257 struct ethtool_pauseparam pauseparam;
1258
e1b90c41 1259 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
1260 return -EOPNOTSUPP;
1261
1262 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1263 return -EFAULT;
1264
1265 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1266}
1267
1da177e4
LT
1268static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1269{
1270 struct ethtool_test test;
76fd8593 1271 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1272 u64 *data;
ff03d49f 1273 int ret, test_len;
1da177e4 1274
a9828ec6 1275 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
1276 return -EOPNOTSUPP;
1277
a9828ec6 1278 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
1279 if (test_len < 0)
1280 return test_len;
1281 WARN_ON(test_len == 0);
1282
1da177e4
LT
1283 if (copy_from_user(&test, useraddr, sizeof(test)))
1284 return -EFAULT;
1285
ff03d49f
JG
1286 test.len = test_len;
1287 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
1288 if (!data)
1289 return -ENOMEM;
1290
1291 ops->self_test(dev, &test, data);
1292
1293 ret = -EFAULT;
1294 if (copy_to_user(useraddr, &test, sizeof(test)))
1295 goto out;
1296 useraddr += sizeof(test);
1297 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1298 goto out;
1299 ret = 0;
1300
1301 out:
1302 kfree(data);
1303 return ret;
1304}
1305
1306static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1307{
1308 struct ethtool_gstrings gstrings;
1da177e4
LT
1309 u8 *data;
1310 int ret;
1311
1da177e4
LT
1312 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1313 return -EFAULT;
1314
340ae165 1315 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
1316 if (ret < 0)
1317 return ret;
1318
1319 gstrings.len = ret;
1da177e4 1320
077cb37f 1321 data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
1da177e4
LT
1322 if (!data)
1323 return -ENOMEM;
1324
340ae165 1325 __ethtool_get_strings(dev, gstrings.string_set, data);
1da177e4
LT
1326
1327 ret = -EFAULT;
1328 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1329 goto out;
1330 useraddr += sizeof(gstrings);
1331 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1332 goto out;
1333 ret = 0;
1334
340ae165 1335out:
1da177e4
LT
1336 kfree(data);
1337 return ret;
1338}
1339
1340static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1341{
1342 struct ethtool_value id;
68f512f2 1343 static bool busy;
c03a14e8 1344 const struct ethtool_ops *ops = dev->ethtool_ops;
68f512f2 1345 int rc;
1da177e4 1346
c03a14e8 1347 if (!ops->set_phys_id)
1da177e4
LT
1348 return -EOPNOTSUPP;
1349
68f512f2
BH
1350 if (busy)
1351 return -EBUSY;
1352
1da177e4
LT
1353 if (copy_from_user(&id, useraddr, sizeof(id)))
1354 return -EFAULT;
1355
c03a14e8 1356 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 1357 if (rc < 0)
68f512f2
BH
1358 return rc;
1359
1360 /* Drop the RTNL lock while waiting, but prevent reentry or
1361 * removal of the device.
1362 */
1363 busy = true;
1364 dev_hold(dev);
1365 rtnl_unlock();
1366
1367 if (rc == 0) {
1368 /* Driver will handle this itself */
1369 schedule_timeout_interruptible(
143780c6 1370 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 1371 } else {
fce55922
AB
1372 /* Driver expects to be called at twice the frequency in rc */
1373 int n = rc * 2, i, interval = HZ / n;
1374
1375 /* Count down seconds */
68f512f2 1376 do {
fce55922
AB
1377 /* Count down iterations per second */
1378 i = n;
1379 do {
1380 rtnl_lock();
c03a14e8 1381 rc = ops->set_phys_id(dev,
fce55922
AB
1382 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1383 rtnl_unlock();
1384 if (rc)
1385 break;
1386 schedule_timeout_interruptible(interval);
1387 } while (!signal_pending(current) && --i != 0);
68f512f2
BH
1388 } while (!signal_pending(current) &&
1389 (id.data == 0 || --id.data != 0));
1390 }
1391
1392 rtnl_lock();
1393 dev_put(dev);
1394 busy = false;
1395
c03a14e8 1396 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
68f512f2 1397 return rc;
1da177e4
LT
1398}
1399
1400static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1401{
1402 struct ethtool_stats stats;
76fd8593 1403 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1404 u64 *data;
ff03d49f 1405 int ret, n_stats;
1da177e4 1406
a9828ec6 1407 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1408 return -EOPNOTSUPP;
1409
a9828ec6 1410 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1411 if (n_stats < 0)
1412 return n_stats;
1413 WARN_ON(n_stats == 0);
1414
1da177e4
LT
1415 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1416 return -EFAULT;
1417
ff03d49f
JG
1418 stats.n_stats = n_stats;
1419 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1da177e4
LT
1420 if (!data)
1421 return -ENOMEM;
1422
1423 ops->get_ethtool_stats(dev, &stats, data);
1424
1425 ret = -EFAULT;
1426 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1427 goto out;
1428 useraddr += sizeof(stats);
1429 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1430 goto out;
1431 ret = 0;
1432
1433 out:
1434 kfree(data);
1435 return ret;
1436}
1437
f3a40945
AL
1438static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1439{
1440 struct ethtool_stats stats;
1441 struct phy_device *phydev = dev->phydev;
1442 u64 *data;
1443 int ret, n_stats;
1444
1445 if (!phydev)
1446 return -EOPNOTSUPP;
1447
1448 n_stats = phy_get_sset_count(phydev);
1449
1450 if (n_stats < 0)
1451 return n_stats;
1452 WARN_ON(n_stats == 0);
1453
1454 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1455 return -EFAULT;
1456
1457 stats.n_stats = n_stats;
1458 data = kmalloc_array(n_stats, sizeof(u64), GFP_USER);
1459 if (!data)
1460 return -ENOMEM;
1461
1462 mutex_lock(&phydev->lock);
1463 phydev->drv->get_stats(phydev, &stats, data);
1464 mutex_unlock(&phydev->lock);
1465
1466 ret = -EFAULT;
1467 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1468 goto out;
1469 useraddr += sizeof(stats);
1470 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1471 goto out;
1472 ret = 0;
1473
1474 out:
1475 kfree(data);
1476 return ret;
1477}
1478
0bf0519d 1479static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
1480{
1481 struct ethtool_perm_addr epaddr;
a6f9a705 1482
313674af 1483 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
1484 return -EFAULT;
1485
313674af
MW
1486 if (epaddr.size < dev->addr_len)
1487 return -ETOOSMALL;
1488 epaddr.size = dev->addr_len;
a6f9a705 1489
a6f9a705 1490 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 1491 return -EFAULT;
a6f9a705 1492 useraddr += sizeof(epaddr);
313674af
MW
1493 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1494 return -EFAULT;
1495 return 0;
a6f9a705
JW
1496}
1497
13c99b24
JG
1498static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1499 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 1500{
8e557421 1501 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 1502
13c99b24 1503 if (!actor)
3ae7c0b2
JG
1504 return -EOPNOTSUPP;
1505
13c99b24 1506 edata.data = actor(dev);
3ae7c0b2
JG
1507
1508 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1509 return -EFAULT;
1510 return 0;
1511}
1512
13c99b24
JG
1513static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1514 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
1515{
1516 struct ethtool_value edata;
1517
13c99b24 1518 if (!actor)
3ae7c0b2
JG
1519 return -EOPNOTSUPP;
1520
1521 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1522 return -EFAULT;
1523
13c99b24 1524 actor(dev, edata.data);
339bf024
JG
1525 return 0;
1526}
1527
13c99b24
JG
1528static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1529 int (*actor)(struct net_device *, u32))
339bf024
JG
1530{
1531 struct ethtool_value edata;
1532
13c99b24 1533 if (!actor)
339bf024
JG
1534 return -EOPNOTSUPP;
1535
1536 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1537 return -EFAULT;
1538
13c99b24 1539 return actor(dev, edata.data);
339bf024
JG
1540}
1541
97f8aefb 1542static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1543 char __user *useraddr)
05c6a8d7
AK
1544{
1545 struct ethtool_flash efl;
1546
1547 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1548 return -EFAULT;
1549
1550 if (!dev->ethtool_ops->flash_device)
1551 return -EOPNOTSUPP;
1552
786f5281
BH
1553 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1554
05c6a8d7
AK
1555 return dev->ethtool_ops->flash_device(dev, &efl);
1556}
1557
29dd54b7
AC
1558static int ethtool_set_dump(struct net_device *dev,
1559 void __user *useraddr)
1560{
1561 struct ethtool_dump dump;
1562
1563 if (!dev->ethtool_ops->set_dump)
1564 return -EOPNOTSUPP;
1565
1566 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1567 return -EFAULT;
1568
1569 return dev->ethtool_ops->set_dump(dev, &dump);
1570}
1571
1572static int ethtool_get_dump_flag(struct net_device *dev,
1573 void __user *useraddr)
1574{
1575 int ret;
1576 struct ethtool_dump dump;
1577 const struct ethtool_ops *ops = dev->ethtool_ops;
1578
c03a14e8 1579 if (!ops->get_dump_flag)
29dd54b7
AC
1580 return -EOPNOTSUPP;
1581
1582 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1583 return -EFAULT;
1584
1585 ret = ops->get_dump_flag(dev, &dump);
1586 if (ret)
1587 return ret;
1588
1589 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1590 return -EFAULT;
1591 return 0;
1592}
1593
1594static int ethtool_get_dump_data(struct net_device *dev,
1595 void __user *useraddr)
1596{
1597 int ret;
1598 __u32 len;
1599 struct ethtool_dump dump, tmp;
1600 const struct ethtool_ops *ops = dev->ethtool_ops;
1601 void *data = NULL;
1602
c03a14e8 1603 if (!ops->get_dump_data || !ops->get_dump_flag)
29dd54b7
AC
1604 return -EOPNOTSUPP;
1605
1606 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1607 return -EFAULT;
1608
1609 memset(&tmp, 0, sizeof(tmp));
1610 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1611 ret = ops->get_dump_flag(dev, &tmp);
1612 if (ret)
1613 return ret;
1614
c590b5e2 1615 len = min(tmp.len, dump.len);
29dd54b7
AC
1616 if (!len)
1617 return -EFAULT;
1618
c590b5e2
MS
1619 /* Don't ever let the driver think there's more space available
1620 * than it requested with .get_dump_flag().
1621 */
1622 dump.len = len;
1623
1624 /* Always allocate enough space to hold the whole thing so that the
1625 * driver does not need to check the length and bother with partial
1626 * dumping.
1627 */
29dd54b7
AC
1628 data = vzalloc(tmp.len);
1629 if (!data)
1630 return -ENOMEM;
1631 ret = ops->get_dump_data(dev, &dump, data);
1632 if (ret)
1633 goto out;
1634
c590b5e2
MS
1635 /* There are two sane possibilities:
1636 * 1. The driver's .get_dump_data() does not touch dump.len.
1637 * 2. Or it may set dump.len to how much it really writes, which
1638 * should be tmp.len (or len if it can do a partial dump).
1639 * In any case respond to userspace with the actual length of data
1640 * it's receiving.
1641 */
1642 WARN_ON(dump.len != len && dump.len != tmp.len);
1643 dump.len = len;
1644
29dd54b7
AC
1645 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1646 ret = -EFAULT;
1647 goto out;
1648 }
1649 useraddr += offsetof(struct ethtool_dump, data);
1650 if (copy_to_user(useraddr, data, len))
1651 ret = -EFAULT;
1652out:
1653 vfree(data);
1654 return ret;
1655}
1656
c8f3a8c3
RC
1657static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1658{
1659 int err = 0;
1660 struct ethtool_ts_info info;
1661 const struct ethtool_ops *ops = dev->ethtool_ops;
1662 struct phy_device *phydev = dev->phydev;
1663
1664 memset(&info, 0, sizeof(info));
1665 info.cmd = ETHTOOL_GET_TS_INFO;
1666
1667 if (phydev && phydev->drv && phydev->drv->ts_info) {
c8f3a8c3 1668 err = phydev->drv->ts_info(phydev, &info);
c03a14e8 1669 } else if (ops->get_ts_info) {
c8f3a8c3 1670 err = ops->get_ts_info(dev, &info);
c8f3a8c3
RC
1671 } else {
1672 info.so_timestamping =
1673 SOF_TIMESTAMPING_RX_SOFTWARE |
1674 SOF_TIMESTAMPING_SOFTWARE;
1675 info.phc_index = -1;
1676 }
1677
1678 if (err)
1679 return err;
1680
1681 if (copy_to_user(useraddr, &info, sizeof(info)))
1682 err = -EFAULT;
1683
1684 return err;
1685}
1686
2f438366
ES
1687static int __ethtool_get_module_info(struct net_device *dev,
1688 struct ethtool_modinfo *modinfo)
1689{
1690 const struct ethtool_ops *ops = dev->ethtool_ops;
1691 struct phy_device *phydev = dev->phydev;
1692
1693 if (phydev && phydev->drv && phydev->drv->module_info)
1694 return phydev->drv->module_info(phydev, modinfo);
1695
1696 if (ops->get_module_info)
1697 return ops->get_module_info(dev, modinfo);
1698
1699 return -EOPNOTSUPP;
1700}
1701
41c3cb6d
SH
1702static int ethtool_get_module_info(struct net_device *dev,
1703 void __user *useraddr)
1704{
1705 int ret;
1706 struct ethtool_modinfo modinfo;
41c3cb6d
SH
1707
1708 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1709 return -EFAULT;
1710
2f438366 1711 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
1712 if (ret)
1713 return ret;
1714
1715 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1716 return -EFAULT;
1717
1718 return 0;
1719}
1720
2f438366
ES
1721static int __ethtool_get_module_eeprom(struct net_device *dev,
1722 struct ethtool_eeprom *ee, u8 *data)
1723{
1724 const struct ethtool_ops *ops = dev->ethtool_ops;
1725 struct phy_device *phydev = dev->phydev;
1726
1727 if (phydev && phydev->drv && phydev->drv->module_eeprom)
1728 return phydev->drv->module_eeprom(phydev, ee, data);
1729
1730 if (ops->get_module_eeprom)
1731 return ops->get_module_eeprom(dev, ee, data);
1732
1733 return -EOPNOTSUPP;
1734}
1735
41c3cb6d
SH
1736static int ethtool_get_module_eeprom(struct net_device *dev,
1737 void __user *useraddr)
1738{
1739 int ret;
1740 struct ethtool_modinfo modinfo;
41c3cb6d 1741
2f438366 1742 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
1743 if (ret)
1744 return ret;
1745
2f438366
ES
1746 return ethtool_get_any_eeprom(dev, useraddr,
1747 __ethtool_get_module_eeprom,
41c3cb6d
SH
1748 modinfo.eeprom_len);
1749}
1750
f0db9b07
GV
1751static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
1752{
1753 switch (tuna->id) {
1754 case ETHTOOL_RX_COPYBREAK:
1255a505 1755 case ETHTOOL_TX_COPYBREAK:
f0db9b07
GV
1756 if (tuna->len != sizeof(u32) ||
1757 tuna->type_id != ETHTOOL_TUNABLE_U32)
1758 return -EINVAL;
1759 break;
1760 default:
1761 return -EINVAL;
1762 }
1763
1764 return 0;
1765}
1766
1767static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
1768{
1769 int ret;
1770 struct ethtool_tunable tuna;
1771 const struct ethtool_ops *ops = dev->ethtool_ops;
1772 void *data;
1773
1774 if (!ops->get_tunable)
1775 return -EOPNOTSUPP;
1776 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1777 return -EFAULT;
1778 ret = ethtool_tunable_valid(&tuna);
1779 if (ret)
1780 return ret;
1781 data = kmalloc(tuna.len, GFP_USER);
1782 if (!data)
1783 return -ENOMEM;
1784 ret = ops->get_tunable(dev, &tuna, data);
1785 if (ret)
1786 goto out;
1787 useraddr += sizeof(tuna);
1788 ret = -EFAULT;
1789 if (copy_to_user(useraddr, data, tuna.len))
1790 goto out;
1791 ret = 0;
1792
1793out:
1794 kfree(data);
1795 return ret;
1796}
1797
1798static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
1799{
1800 int ret;
1801 struct ethtool_tunable tuna;
1802 const struct ethtool_ops *ops = dev->ethtool_ops;
1803 void *data;
1804
1805 if (!ops->set_tunable)
1806 return -EOPNOTSUPP;
1807 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1808 return -EFAULT;
1809 ret = ethtool_tunable_valid(&tuna);
1810 if (ret)
1811 return ret;
1812 data = kmalloc(tuna.len, GFP_USER);
1813 if (!data)
1814 return -ENOMEM;
1815 useraddr += sizeof(tuna);
1816 ret = -EFAULT;
1817 if (copy_from_user(data, useraddr, tuna.len))
1818 goto out;
1819 ret = ops->set_tunable(dev, &tuna, data);
1820
1821out:
1822 kfree(data);
1823 return ret;
1824}
1825
600fed5e 1826/* The main entry point in this file. Called from net/core/dev_ioctl.c */
1da177e4 1827
881d966b 1828int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 1829{
881d966b 1830 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4
LT
1831 void __user *useraddr = ifr->ifr_data;
1832 u32 ethcmd;
1833 int rc;
b29d3145 1834 netdev_features_t old_features;
1da177e4 1835
1da177e4
LT
1836 if (!dev || !netif_device_present(dev))
1837 return -ENODEV;
1838
97f8aefb 1839 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
1840 return -EFAULT;
1841
75f3123c 1842 /* Allow some commands to be done by anyone */
97f8aefb 1843 switch (ethcmd) {
0fdc100b 1844 case ETHTOOL_GSET:
75f3123c 1845 case ETHTOOL_GDRVINFO:
75f3123c 1846 case ETHTOOL_GMSGLVL:
2da45db2 1847 case ETHTOOL_GLINK:
75f3123c
SH
1848 case ETHTOOL_GCOALESCE:
1849 case ETHTOOL_GRINGPARAM:
1850 case ETHTOOL_GPAUSEPARAM:
1851 case ETHTOOL_GRXCSUM:
1852 case ETHTOOL_GTXCSUM:
1853 case ETHTOOL_GSG:
f80400a2 1854 case ETHTOOL_GSSET_INFO:
75f3123c 1855 case ETHTOOL_GSTRINGS:
2da45db2 1856 case ETHTOOL_GSTATS:
f3a40945 1857 case ETHTOOL_GPHYSTATS:
75f3123c
SH
1858 case ETHTOOL_GTSO:
1859 case ETHTOOL_GPERMADDR:
1860 case ETHTOOL_GUFO:
1861 case ETHTOOL_GGSO:
1cab819b 1862 case ETHTOOL_GGRO:
339bf024
JG
1863 case ETHTOOL_GFLAGS:
1864 case ETHTOOL_GPFLAGS:
0853ad66 1865 case ETHTOOL_GRXFH:
59089d8d
SB
1866 case ETHTOOL_GRXRINGS:
1867 case ETHTOOL_GRXCLSRLCNT:
1868 case ETHTOOL_GRXCLSRULE:
1869 case ETHTOOL_GRXCLSRLALL:
2da45db2 1870 case ETHTOOL_GRXFHINDIR:
3de0b592 1871 case ETHTOOL_GRSSH:
5455c699 1872 case ETHTOOL_GFEATURES:
2da45db2 1873 case ETHTOOL_GCHANNELS:
c8f3a8c3 1874 case ETHTOOL_GET_TS_INFO:
2da45db2 1875 case ETHTOOL_GEEE:
f0db9b07 1876 case ETHTOOL_GTUNABLE:
75f3123c
SH
1877 break;
1878 default:
5e1fccc0 1879 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
75f3123c
SH
1880 return -EPERM;
1881 }
1882
97f8aefb 1883 if (dev->ethtool_ops->begin) {
1884 rc = dev->ethtool_ops->begin(dev);
1885 if (rc < 0)
1da177e4 1886 return rc;
97f8aefb 1887 }
d8a33ac4
SH
1888 old_features = dev->features;
1889
1da177e4
LT
1890 switch (ethcmd) {
1891 case ETHTOOL_GSET:
1892 rc = ethtool_get_settings(dev, useraddr);
1893 break;
1894 case ETHTOOL_SSET:
1895 rc = ethtool_set_settings(dev, useraddr);
1896 break;
1897 case ETHTOOL_GDRVINFO:
1898 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
1899 break;
1900 case ETHTOOL_GREGS:
1901 rc = ethtool_get_regs(dev, useraddr);
1902 break;
1903 case ETHTOOL_GWOL:
1904 rc = ethtool_get_wol(dev, useraddr);
1905 break;
1906 case ETHTOOL_SWOL:
1907 rc = ethtool_set_wol(dev, useraddr);
1908 break;
1909 case ETHTOOL_GMSGLVL:
13c99b24
JG
1910 rc = ethtool_get_value(dev, useraddr, ethcmd,
1911 dev->ethtool_ops->get_msglevel);
1da177e4
LT
1912 break;
1913 case ETHTOOL_SMSGLVL:
13c99b24
JG
1914 rc = ethtool_set_value_void(dev, useraddr,
1915 dev->ethtool_ops->set_msglevel);
1da177e4 1916 break;
80f12ecc
YM
1917 case ETHTOOL_GEEE:
1918 rc = ethtool_get_eee(dev, useraddr);
1919 break;
1920 case ETHTOOL_SEEE:
1921 rc = ethtool_set_eee(dev, useraddr);
1922 break;
1da177e4
LT
1923 case ETHTOOL_NWAY_RST:
1924 rc = ethtool_nway_reset(dev);
1925 break;
1926 case ETHTOOL_GLINK:
e596e6e4 1927 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
1928 break;
1929 case ETHTOOL_GEEPROM:
1930 rc = ethtool_get_eeprom(dev, useraddr);
1931 break;
1932 case ETHTOOL_SEEPROM:
1933 rc = ethtool_set_eeprom(dev, useraddr);
1934 break;
1935 case ETHTOOL_GCOALESCE:
1936 rc = ethtool_get_coalesce(dev, useraddr);
1937 break;
1938 case ETHTOOL_SCOALESCE:
1939 rc = ethtool_set_coalesce(dev, useraddr);
1940 break;
1941 case ETHTOOL_GRINGPARAM:
1942 rc = ethtool_get_ringparam(dev, useraddr);
1943 break;
1944 case ETHTOOL_SRINGPARAM:
1945 rc = ethtool_set_ringparam(dev, useraddr);
1946 break;
1947 case ETHTOOL_GPAUSEPARAM:
1948 rc = ethtool_get_pauseparam(dev, useraddr);
1949 break;
1950 case ETHTOOL_SPAUSEPARAM:
1951 rc = ethtool_set_pauseparam(dev, useraddr);
1952 break;
1da177e4
LT
1953 case ETHTOOL_TEST:
1954 rc = ethtool_self_test(dev, useraddr);
1955 break;
1956 case ETHTOOL_GSTRINGS:
1957 rc = ethtool_get_strings(dev, useraddr);
1958 break;
1959 case ETHTOOL_PHYS_ID:
1960 rc = ethtool_phys_id(dev, useraddr);
1961 break;
1962 case ETHTOOL_GSTATS:
1963 rc = ethtool_get_stats(dev, useraddr);
1964 break;
a6f9a705
JW
1965 case ETHTOOL_GPERMADDR:
1966 rc = ethtool_get_perm_addr(dev, useraddr);
1967 break;
3ae7c0b2 1968 case ETHTOOL_GFLAGS:
13c99b24 1969 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 1970 __ethtool_get_flags);
3ae7c0b2
JG
1971 break;
1972 case ETHTOOL_SFLAGS:
da8ac86c 1973 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 1974 break;
339bf024 1975 case ETHTOOL_GPFLAGS:
13c99b24
JG
1976 rc = ethtool_get_value(dev, useraddr, ethcmd,
1977 dev->ethtool_ops->get_priv_flags);
339bf024
JG
1978 break;
1979 case ETHTOOL_SPFLAGS:
13c99b24
JG
1980 rc = ethtool_set_value(dev, useraddr,
1981 dev->ethtool_ops->set_priv_flags);
339bf024 1982 break;
0853ad66 1983 case ETHTOOL_GRXFH:
59089d8d
SB
1984 case ETHTOOL_GRXRINGS:
1985 case ETHTOOL_GRXCLSRLCNT:
1986 case ETHTOOL_GRXCLSRULE:
1987 case ETHTOOL_GRXCLSRLALL:
bf988435 1988 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
1989 break;
1990 case ETHTOOL_SRXFH:
59089d8d
SB
1991 case ETHTOOL_SRXCLSRLDEL:
1992 case ETHTOOL_SRXCLSRLINS:
bf988435 1993 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 1994 break;
05c6a8d7
AK
1995 case ETHTOOL_FLASHDEV:
1996 rc = ethtool_flash_device(dev, useraddr);
1997 break;
d73d3a8c
BH
1998 case ETHTOOL_RESET:
1999 rc = ethtool_reset(dev, useraddr);
2000 break;
723b2f57
JG
2001 case ETHTOOL_GSSET_INFO:
2002 rc = ethtool_get_sset_info(dev, useraddr);
2003 break;
a5b6ee29
BH
2004 case ETHTOOL_GRXFHINDIR:
2005 rc = ethtool_get_rxfh_indir(dev, useraddr);
2006 break;
2007 case ETHTOOL_SRXFHINDIR:
2008 rc = ethtool_set_rxfh_indir(dev, useraddr);
2009 break;
3de0b592
VD
2010 case ETHTOOL_GRSSH:
2011 rc = ethtool_get_rxfh(dev, useraddr);
2012 break;
2013 case ETHTOOL_SRSSH:
2014 rc = ethtool_set_rxfh(dev, useraddr);
2015 break;
5455c699
MM
2016 case ETHTOOL_GFEATURES:
2017 rc = ethtool_get_features(dev, useraddr);
2018 break;
2019 case ETHTOOL_SFEATURES:
2020 rc = ethtool_set_features(dev, useraddr);
2021 break;
0a417704 2022 case ETHTOOL_GTXCSUM:
e83d360d 2023 case ETHTOOL_GRXCSUM:
0a417704
MM
2024 case ETHTOOL_GSG:
2025 case ETHTOOL_GTSO:
2026 case ETHTOOL_GUFO:
2027 case ETHTOOL_GGSO:
2028 case ETHTOOL_GGRO:
2029 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2030 break;
2031 case ETHTOOL_STXCSUM:
e83d360d 2032 case ETHTOOL_SRXCSUM:
0a417704
MM
2033 case ETHTOOL_SSG:
2034 case ETHTOOL_STSO:
2035 case ETHTOOL_SUFO:
2036 case ETHTOOL_SGSO:
2037 case ETHTOOL_SGRO:
2038 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2039 break;
8b5933c3 2040 case ETHTOOL_GCHANNELS:
2041 rc = ethtool_get_channels(dev, useraddr);
2042 break;
2043 case ETHTOOL_SCHANNELS:
2044 rc = ethtool_set_channels(dev, useraddr);
2045 break;
29dd54b7
AC
2046 case ETHTOOL_SET_DUMP:
2047 rc = ethtool_set_dump(dev, useraddr);
2048 break;
2049 case ETHTOOL_GET_DUMP_FLAG:
2050 rc = ethtool_get_dump_flag(dev, useraddr);
2051 break;
2052 case ETHTOOL_GET_DUMP_DATA:
2053 rc = ethtool_get_dump_data(dev, useraddr);
2054 break;
c8f3a8c3
RC
2055 case ETHTOOL_GET_TS_INFO:
2056 rc = ethtool_get_ts_info(dev, useraddr);
2057 break;
41c3cb6d
SH
2058 case ETHTOOL_GMODULEINFO:
2059 rc = ethtool_get_module_info(dev, useraddr);
2060 break;
2061 case ETHTOOL_GMODULEEEPROM:
2062 rc = ethtool_get_module_eeprom(dev, useraddr);
2063 break;
f0db9b07
GV
2064 case ETHTOOL_GTUNABLE:
2065 rc = ethtool_get_tunable(dev, useraddr);
2066 break;
2067 case ETHTOOL_STUNABLE:
2068 rc = ethtool_set_tunable(dev, useraddr);
2069 break;
f3a40945
AL
2070 case ETHTOOL_GPHYSTATS:
2071 rc = ethtool_get_phy_stats(dev, useraddr);
2072 break;
1da177e4 2073 default:
61a44b9c 2074 rc = -EOPNOTSUPP;
1da177e4 2075 }
4ec93edb 2076
e71a4783 2077 if (dev->ethtool_ops->complete)
1da177e4 2078 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
2079
2080 if (old_features != dev->features)
2081 netdev_features_change(dev);
2082
1da177e4 2083 return rc;
1da177e4 2084}
This page took 0.956177 seconds and 5 git commands to generate.