]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/core/ethtool.c
Merge branch 'for-linus-4.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.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 26#include <linux/rtnetlink.h>
174cd4b1 27#include <linux/sched/signal.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",
cbc53e08 82 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
9d921549
MM
83 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
84 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
68c33163 85 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
518f213d 86 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
7e13318d
TH
87 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
88 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
73136267 89 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
518f213d 90 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
802ab55a 91 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
90017acc 92 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
c7ef8f0c 93 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
9d921549
MM
94
95 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
53692b1d 96 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
9d921549
MM
97 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
98 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
99 [NETIF_F_RXHASH_BIT] = "rx-hashing",
100 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
101 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
102 [NETIF_F_LOOPBACK_BIT] = "loopback",
36eabda3 103 [NETIF_F_RXFCS_BIT] = "rx-fcs",
5e0c03c8 104 [NETIF_F_RXALL_BIT] = "rx-all",
a6cc0cfa 105 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
1c78c64e 106 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
c7ef8f0c
SK
107 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
108 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
9d921549
MM
109};
110
892311f6
EP
111static const char
112rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
113 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
114 [ETH_RSS_HASH_XOR_BIT] = "xor",
abb521e3 115 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
892311f6
EP
116};
117
a4244b0c
HHZ
118static const char
119tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
120 [ETHTOOL_ID_UNSPEC] = "Unspec",
121 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
122 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
123};
124
968ad9da
RL
125static const char
126phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
127 [ETHTOOL_ID_UNSPEC] = "Unspec",
65feddd5 128 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
968ad9da
RL
129};
130
5455c699
MM
131static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
132{
133 struct ethtool_gfeatures cmd = {
134 .cmd = ETHTOOL_GFEATURES,
135 .size = ETHTOOL_DEV_FEATURE_WORDS,
136 };
475414f6 137 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
5455c699
MM
138 u32 __user *sizeaddr;
139 u32 copy_size;
475414f6
MM
140 int i;
141
142 /* in case feature bits run out again */
09da71b1 143 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
475414f6
MM
144
145 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
146 features[i].available = (u32)(dev->hw_features >> (32 * i));
147 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
148 features[i].active = (u32)(dev->features >> (32 * i));
149 features[i].never_changed =
150 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
475414f6 151 }
5455c699
MM
152
153 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
154 if (get_user(copy_size, sizeaddr))
155 return -EFAULT;
156
157 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
158 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
159
160 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
161 return -EFAULT;
162 useraddr += sizeof(cmd);
163 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
164 return -EFAULT;
165
166 return 0;
167}
168
169static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
170{
171 struct ethtool_sfeatures cmd;
172 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
475414f6
MM
173 netdev_features_t wanted = 0, valid = 0;
174 int i, ret = 0;
5455c699
MM
175
176 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
177 return -EFAULT;
178 useraddr += sizeof(cmd);
179
180 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
181 return -EINVAL;
182
183 if (copy_from_user(features, useraddr, sizeof(features)))
184 return -EFAULT;
185
475414f6 186 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
187 valid |= (netdev_features_t)features[i].valid << (32 * i);
188 wanted |= (netdev_features_t)features[i].requested << (32 * i);
475414f6
MM
189 }
190
191 if (valid & ~NETIF_F_ETHTOOL_BITS)
5455c699
MM
192 return -EINVAL;
193
475414f6
MM
194 if (valid & ~dev->hw_features) {
195 valid &= dev->hw_features;
5455c699
MM
196 ret |= ETHTOOL_F_UNSUPPORTED;
197 }
198
475414f6
MM
199 dev->wanted_features &= ~valid;
200 dev->wanted_features |= wanted & valid;
6cb6a27c 201 __netdev_update_features(dev);
5455c699 202
475414f6 203 if ((dev->wanted_features ^ dev->features) & valid)
5455c699
MM
204 ret |= ETHTOOL_F_WISH;
205
206 return ret;
207}
208
f3a40945
AL
209static int phy_get_sset_count(struct phy_device *phydev)
210{
211 int ret;
212
213 if (phydev->drv->get_sset_count &&
214 phydev->drv->get_strings &&
215 phydev->drv->get_stats) {
216 mutex_lock(&phydev->lock);
217 ret = phydev->drv->get_sset_count(phydev);
218 mutex_unlock(&phydev->lock);
219
220 return ret;
221 }
222
223 return -EOPNOTSUPP;
224}
225
340ae165
MM
226static int __ethtool_get_sset_count(struct net_device *dev, int sset)
227{
228 const struct ethtool_ops *ops = dev->ethtool_ops;
229
5455c699
MM
230 if (sset == ETH_SS_FEATURES)
231 return ARRAY_SIZE(netdev_features_strings);
232
892311f6
EP
233 if (sset == ETH_SS_RSS_HASH_FUNCS)
234 return ARRAY_SIZE(rss_hash_func_strings);
235
a4244b0c
HHZ
236 if (sset == ETH_SS_TUNABLES)
237 return ARRAY_SIZE(tunable_strings);
238
968ad9da
RL
239 if (sset == ETH_SS_PHY_TUNABLES)
240 return ARRAY_SIZE(phy_tunable_strings);
241
f3a40945
AL
242 if (sset == ETH_SS_PHY_STATS) {
243 if (dev->phydev)
244 return phy_get_sset_count(dev->phydev);
245 else
246 return -EOPNOTSUPP;
247 }
248
c03a14e8 249 if (ops->get_sset_count && ops->get_strings)
340ae165
MM
250 return ops->get_sset_count(dev, sset);
251 else
252 return -EOPNOTSUPP;
253}
254
255static void __ethtool_get_strings(struct net_device *dev,
256 u32 stringset, u8 *data)
257{
258 const struct ethtool_ops *ops = dev->ethtool_ops;
259
5455c699
MM
260 if (stringset == ETH_SS_FEATURES)
261 memcpy(data, netdev_features_strings,
262 sizeof(netdev_features_strings));
892311f6
EP
263 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
264 memcpy(data, rss_hash_func_strings,
265 sizeof(rss_hash_func_strings));
a4244b0c
HHZ
266 else if (stringset == ETH_SS_TUNABLES)
267 memcpy(data, tunable_strings, sizeof(tunable_strings));
968ad9da
RL
268 else if (stringset == ETH_SS_PHY_TUNABLES)
269 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
f3a40945
AL
270 else if (stringset == ETH_SS_PHY_STATS) {
271 struct phy_device *phydev = dev->phydev;
272
273 if (phydev) {
274 mutex_lock(&phydev->lock);
275 phydev->drv->get_strings(phydev, data);
276 mutex_unlock(&phydev->lock);
277 } else {
278 return;
279 }
280 } else
5455c699
MM
281 /* ops->get_strings is valid because checked earlier */
282 ops->get_strings(dev, stringset, data);
340ae165
MM
283}
284
c8f44aff 285static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0a417704
MM
286{
287 /* feature masks of legacy discrete ethtool ops */
288
289 switch (eth_cmd) {
290 case ETHTOOL_GTXCSUM:
291 case ETHTOOL_STXCSUM:
a188222b 292 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
e83d360d
MM
293 case ETHTOOL_GRXCSUM:
294 case ETHTOOL_SRXCSUM:
295 return NETIF_F_RXCSUM;
0a417704
MM
296 case ETHTOOL_GSG:
297 case ETHTOOL_SSG:
298 return NETIF_F_SG;
299 case ETHTOOL_GTSO:
300 case ETHTOOL_STSO:
301 return NETIF_F_ALL_TSO;
302 case ETHTOOL_GUFO:
303 case ETHTOOL_SUFO:
304 return NETIF_F_UFO;
305 case ETHTOOL_GGSO:
306 case ETHTOOL_SGSO:
307 return NETIF_F_GSO;
308 case ETHTOOL_GGRO:
309 case ETHTOOL_SGRO:
310 return NETIF_F_GRO;
311 default:
312 BUG();
313 }
314}
315
0a417704
MM
316static int ethtool_get_one_feature(struct net_device *dev,
317 char __user *useraddr, u32 ethcmd)
318{
c8f44aff 319 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0a417704
MM
320 struct ethtool_value edata = {
321 .cmd = ethcmd,
86794881 322 .data = !!(dev->features & mask),
0a417704 323 };
0a417704 324
0a417704
MM
325 if (copy_to_user(useraddr, &edata, sizeof(edata)))
326 return -EFAULT;
327 return 0;
328}
329
0a417704
MM
330static int ethtool_set_one_feature(struct net_device *dev,
331 void __user *useraddr, u32 ethcmd)
332{
333 struct ethtool_value edata;
c8f44aff 334 netdev_features_t mask;
0a417704
MM
335
336 if (copy_from_user(&edata, useraddr, sizeof(edata)))
337 return -EFAULT;
338
86794881
MM
339 mask = ethtool_get_feature_mask(ethcmd);
340 mask &= dev->hw_features;
bc5787c6
MM
341 if (!mask)
342 return -EOPNOTSUPP;
86794881 343
bc5787c6
MM
344 if (edata.data)
345 dev->wanted_features |= mask;
346 else
347 dev->wanted_features &= ~mask;
86794881 348
bc5787c6 349 __netdev_update_features(dev);
86794881 350
bc5787c6
MM
351 return 0;
352}
353
02b3a552
MM
354#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
355 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
f646968f
PM
356#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
357 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
358 NETIF_F_RXHASH)
bc5787c6
MM
359
360static u32 __ethtool_get_flags(struct net_device *dev)
361{
02b3a552
MM
362 u32 flags = 0;
363
8a73125c
DF
364 if (dev->features & NETIF_F_LRO)
365 flags |= ETH_FLAG_LRO;
366 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
367 flags |= ETH_FLAG_RXVLAN;
368 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
369 flags |= ETH_FLAG_TXVLAN;
370 if (dev->features & NETIF_F_NTUPLE)
371 flags |= ETH_FLAG_NTUPLE;
372 if (dev->features & NETIF_F_RXHASH)
373 flags |= ETH_FLAG_RXHASH;
02b3a552
MM
374
375 return flags;
0a417704
MM
376}
377
bc5787c6 378static int __ethtool_set_flags(struct net_device *dev, u32 data)
da8ac86c 379{
c8f44aff 380 netdev_features_t features = 0, changed;
da8ac86c 381
02b3a552 382 if (data & ~ETH_ALL_FLAGS)
da8ac86c
MM
383 return -EINVAL;
384
8a73125c
DF
385 if (data & ETH_FLAG_LRO)
386 features |= NETIF_F_LRO;
387 if (data & ETH_FLAG_RXVLAN)
388 features |= NETIF_F_HW_VLAN_CTAG_RX;
389 if (data & ETH_FLAG_TXVLAN)
390 features |= NETIF_F_HW_VLAN_CTAG_TX;
391 if (data & ETH_FLAG_NTUPLE)
392 features |= NETIF_F_NTUPLE;
393 if (data & ETH_FLAG_RXHASH)
394 features |= NETIF_F_RXHASH;
02b3a552 395
da8ac86c 396 /* allow changing only bits set in hw_features */
02b3a552 397 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
da8ac86c
MM
398 if (changed & ~dev->hw_features)
399 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
400
401 dev->wanted_features =
02b3a552 402 (dev->wanted_features & ~changed) | (features & changed);
da8ac86c 403
6cb6a27c 404 __netdev_update_features(dev);
da8ac86c
MM
405
406 return 0;
407}
408
6d62b4d5
PR
409void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
410 u32 legacy_u32)
3f1ac7a7
DD
411{
412 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
413 dst[0] = legacy_u32;
414}
6d62b4d5 415EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
3f1ac7a7
DD
416
417/* return false if src had higher bits set. lower bits always updated. */
6d62b4d5
PR
418bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
419 const unsigned long *src)
3f1ac7a7
DD
420{
421 bool retval = true;
422
423 /* TODO: following test will soon always be true */
424 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
425 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
426
427 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
428 bitmap_fill(ext, 32);
429 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
430 if (bitmap_intersects(ext, src,
431 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
432 /* src mask goes beyond bit 31 */
433 retval = false;
434 }
435 }
436 *legacy_u32 = src[0];
437 return retval;
438}
6d62b4d5 439EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
3f1ac7a7
DD
440
441/* return false if legacy contained non-0 deprecated fields
442 * transceiver/maxtxpkt/maxrxpkt. rest of ksettings always updated
443 */
444static bool
445convert_legacy_settings_to_link_ksettings(
446 struct ethtool_link_ksettings *link_ksettings,
447 const struct ethtool_cmd *legacy_settings)
448{
449 bool retval = true;
450
451 memset(link_ksettings, 0, sizeof(*link_ksettings));
452
453 /* This is used to tell users that driver is still using these
454 * deprecated legacy fields, and they should not use
455 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
456 */
457 if (legacy_settings->transceiver ||
458 legacy_settings->maxtxpkt ||
459 legacy_settings->maxrxpkt)
460 retval = false;
461
6d62b4d5 462 ethtool_convert_legacy_u32_to_link_mode(
3f1ac7a7
DD
463 link_ksettings->link_modes.supported,
464 legacy_settings->supported);
6d62b4d5 465 ethtool_convert_legacy_u32_to_link_mode(
3f1ac7a7
DD
466 link_ksettings->link_modes.advertising,
467 legacy_settings->advertising);
6d62b4d5 468 ethtool_convert_legacy_u32_to_link_mode(
3f1ac7a7
DD
469 link_ksettings->link_modes.lp_advertising,
470 legacy_settings->lp_advertising);
471 link_ksettings->base.speed
472 = ethtool_cmd_speed(legacy_settings);
473 link_ksettings->base.duplex
474 = legacy_settings->duplex;
475 link_ksettings->base.port
476 = legacy_settings->port;
477 link_ksettings->base.phy_address
478 = legacy_settings->phy_address;
479 link_ksettings->base.autoneg
480 = legacy_settings->autoneg;
481 link_ksettings->base.mdio_support
482 = legacy_settings->mdio_support;
483 link_ksettings->base.eth_tp_mdix
484 = legacy_settings->eth_tp_mdix;
485 link_ksettings->base.eth_tp_mdix_ctrl
486 = legacy_settings->eth_tp_mdix_ctrl;
487 return retval;
488}
489
490/* return false if ksettings link modes had higher bits
491 * set. legacy_settings always updated (best effort)
492 */
493static bool
494convert_link_ksettings_to_legacy_settings(
495 struct ethtool_cmd *legacy_settings,
496 const struct ethtool_link_ksettings *link_ksettings)
497{
498 bool retval = true;
499
500 memset(legacy_settings, 0, sizeof(*legacy_settings));
501 /* this also clears the deprecated fields in legacy structure:
502 * __u8 transceiver;
503 * __u32 maxtxpkt;
504 * __u32 maxrxpkt;
505 */
506
6d62b4d5 507 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
508 &legacy_settings->supported,
509 link_ksettings->link_modes.supported);
6d62b4d5 510 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
511 &legacy_settings->advertising,
512 link_ksettings->link_modes.advertising);
6d62b4d5 513 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
514 &legacy_settings->lp_advertising,
515 link_ksettings->link_modes.lp_advertising);
516 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
517 legacy_settings->duplex
518 = link_ksettings->base.duplex;
519 legacy_settings->port
520 = link_ksettings->base.port;
521 legacy_settings->phy_address
522 = link_ksettings->base.phy_address;
523 legacy_settings->autoneg
524 = link_ksettings->base.autoneg;
525 legacy_settings->mdio_support
526 = link_ksettings->base.mdio_support;
527 legacy_settings->eth_tp_mdix
528 = link_ksettings->base.eth_tp_mdix;
529 legacy_settings->eth_tp_mdix_ctrl
530 = link_ksettings->base.eth_tp_mdix_ctrl;
531 return retval;
532}
533
534/* number of 32-bit words to store the user's link mode bitmaps */
535#define __ETHTOOL_LINK_MODE_MASK_NU32 \
536 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
537
538/* layout of the struct passed from/to userland */
539struct ethtool_link_usettings {
540 struct ethtool_link_settings base;
541 struct {
542 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
543 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
544 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
545 } link_modes;
546};
547
548/* Internal kernel helper to query a device ethtool_link_settings.
549 *
550 * Backward compatibility note: for compatibility with legacy drivers
551 * that implement only the ethtool_cmd API, this has to work with both
552 * drivers implementing get_link_ksettings API and drivers
553 * implementing get_settings API. When drivers implement get_settings
554 * and report ethtool_cmd deprecated fields
555 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
556 * because the resulting struct ethtool_link_settings does not report them.
557 */
558int __ethtool_get_link_ksettings(struct net_device *dev,
559 struct ethtool_link_ksettings *link_ksettings)
560{
561 int err;
562 struct ethtool_cmd cmd;
563
564 ASSERT_RTNL();
565
566 if (dev->ethtool_ops->get_link_ksettings) {
567 memset(link_ksettings, 0, sizeof(*link_ksettings));
568 return dev->ethtool_ops->get_link_ksettings(dev,
569 link_ksettings);
570 }
571
572 /* driver doesn't support %ethtool_link_ksettings API. revert to
573 * legacy %ethtool_cmd API, unless it's not supported either.
574 * TODO: remove when ethtool_ops::get_settings disappears internally
575 */
3237fc63
DD
576 if (!dev->ethtool_ops->get_settings)
577 return -EOPNOTSUPP;
578
579 memset(&cmd, 0, sizeof(cmd));
580 cmd.cmd = ETHTOOL_GSET;
581 err = dev->ethtool_ops->get_settings(dev, &cmd);
3f1ac7a7
DD
582 if (err < 0)
583 return err;
584
585 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
586 */
587 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
588 return err;
589}
590EXPORT_SYMBOL(__ethtool_get_link_ksettings);
591
592/* convert ethtool_link_usettings in user space to a kernel internal
593 * ethtool_link_ksettings. return 0 on success, errno on error.
594 */
595static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
596 const void __user *from)
597{
598 struct ethtool_link_usettings link_usettings;
599
600 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
601 return -EFAULT;
602
603 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
604 bitmap_from_u32array(to->link_modes.supported,
605 __ETHTOOL_LINK_MODE_MASK_NBITS,
606 link_usettings.link_modes.supported,
607 __ETHTOOL_LINK_MODE_MASK_NU32);
608 bitmap_from_u32array(to->link_modes.advertising,
609 __ETHTOOL_LINK_MODE_MASK_NBITS,
610 link_usettings.link_modes.advertising,
611 __ETHTOOL_LINK_MODE_MASK_NU32);
612 bitmap_from_u32array(to->link_modes.lp_advertising,
613 __ETHTOOL_LINK_MODE_MASK_NBITS,
614 link_usettings.link_modes.lp_advertising,
615 __ETHTOOL_LINK_MODE_MASK_NU32);
616
617 return 0;
618}
619
620/* convert a kernel internal ethtool_link_ksettings to
621 * ethtool_link_usettings in user space. return 0 on success, errno on
622 * error.
623 */
624static int
625store_link_ksettings_for_user(void __user *to,
626 const struct ethtool_link_ksettings *from)
627{
628 struct ethtool_link_usettings link_usettings;
629
630 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
631 bitmap_to_u32array(link_usettings.link_modes.supported,
632 __ETHTOOL_LINK_MODE_MASK_NU32,
633 from->link_modes.supported,
634 __ETHTOOL_LINK_MODE_MASK_NBITS);
635 bitmap_to_u32array(link_usettings.link_modes.advertising,
636 __ETHTOOL_LINK_MODE_MASK_NU32,
637 from->link_modes.advertising,
638 __ETHTOOL_LINK_MODE_MASK_NBITS);
639 bitmap_to_u32array(link_usettings.link_modes.lp_advertising,
640 __ETHTOOL_LINK_MODE_MASK_NU32,
641 from->link_modes.lp_advertising,
642 __ETHTOOL_LINK_MODE_MASK_NBITS);
643
644 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
645 return -EFAULT;
646
647 return 0;
648}
649
650/* Query device for its ethtool_link_settings.
651 *
652 * Backward compatibility note: this function must fail when driver
653 * does not implement ethtool::get_link_ksettings, even if legacy
654 * ethtool_ops::get_settings is implemented. This tells new versions
655 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
656 * this driver, so that they can correctly access the ethtool_cmd
657 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
658 * implements ethtool_ops::get_settings anymore.
659 */
660static int ethtool_get_link_ksettings(struct net_device *dev,
661 void __user *useraddr)
662{
663 int err = 0;
664 struct ethtool_link_ksettings link_ksettings;
665
666 ASSERT_RTNL();
667
668 if (!dev->ethtool_ops->get_link_ksettings)
669 return -EOPNOTSUPP;
670
671 /* handle bitmap nbits handshake */
672 if (copy_from_user(&link_ksettings.base, useraddr,
673 sizeof(link_ksettings.base)))
674 return -EFAULT;
675
676 if (__ETHTOOL_LINK_MODE_MASK_NU32
677 != link_ksettings.base.link_mode_masks_nwords) {
678 /* wrong link mode nbits requested */
679 memset(&link_ksettings, 0, sizeof(link_ksettings));
793cf87d 680 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
3f1ac7a7
DD
681 /* send back number of words required as negative val */
682 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
683 "need too many bits for link modes!");
684 link_ksettings.base.link_mode_masks_nwords
685 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
686
687 /* copy the base fields back to user, not the link
688 * mode bitmaps
689 */
690 if (copy_to_user(useraddr, &link_ksettings.base,
691 sizeof(link_ksettings.base)))
692 return -EFAULT;
693
694 return 0;
695 }
696
697 /* handshake successful: user/kernel agree on
698 * link_mode_masks_nwords
699 */
700
701 memset(&link_ksettings, 0, sizeof(link_ksettings));
702 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
703 if (err < 0)
704 return err;
705
706 /* make sure we tell the right values to user */
707 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
708 link_ksettings.base.link_mode_masks_nwords
709 = __ETHTOOL_LINK_MODE_MASK_NU32;
710
711 return store_link_ksettings_for_user(useraddr, &link_ksettings);
712}
713
714/* Update device ethtool_link_settings.
715 *
716 * Backward compatibility note: this function must fail when driver
717 * does not implement ethtool::set_link_ksettings, even if legacy
718 * ethtool_ops::set_settings is implemented. This tells new versions
719 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
720 * this driver, so that they can correctly update the ethtool_cmd
721 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
722 * implements ethtool_ops::get_settings anymore.
723 */
724static int ethtool_set_link_ksettings(struct net_device *dev,
725 void __user *useraddr)
726{
727 int err;
728 struct ethtool_link_ksettings link_ksettings;
729
730 ASSERT_RTNL();
731
732 if (!dev->ethtool_ops->set_link_ksettings)
733 return -EOPNOTSUPP;
734
735 /* make sure nbits field has expected value */
736 if (copy_from_user(&link_ksettings.base, useraddr,
737 sizeof(link_ksettings.base)))
738 return -EFAULT;
739
740 if (__ETHTOOL_LINK_MODE_MASK_NU32
741 != link_ksettings.base.link_mode_masks_nwords)
742 return -EINVAL;
743
744 /* copy the whole structure, now that we know it has expected
745 * format
746 */
747 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
748 if (err)
749 return err;
750
751 /* re-check nwords field, just in case */
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings.base.link_mode_masks_nwords)
754 return -EINVAL;
755
756 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
757}
758
3f1ac7a7
DD
759static void
760warn_incomplete_ethtool_legacy_settings_conversion(const char *details)
761{
762 char name[sizeof(current->comm)];
763
764 pr_info_once("warning: `%s' uses legacy ethtool link settings API, %s\n",
765 get_task_comm(name, current), details);
766}
767
768/* Query device for its ethtool_cmd settings.
769 *
770 * Backward compatibility note: for compatibility with legacy ethtool,
771 * this has to work with both drivers implementing get_link_ksettings
772 * API and drivers implementing get_settings API. When drivers
773 * implement get_link_ksettings and report higher link mode bits, a
774 * kernel warning is logged once (with name of 1st driver/device) to
775 * recommend user to upgrade ethtool, but the command is successful
776 * (only the lower link mode bits reported back to user).
777 */
4bc71cb9
JP
778static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
779{
4bc71cb9
JP
780 struct ethtool_cmd cmd;
781
3f1ac7a7
DD
782 ASSERT_RTNL();
783
784 if (dev->ethtool_ops->get_link_ksettings) {
785 /* First, use link_ksettings API if it is supported */
786 int err;
787 struct ethtool_link_ksettings link_ksettings;
788
789 memset(&link_ksettings, 0, sizeof(link_ksettings));
790 err = dev->ethtool_ops->get_link_ksettings(dev,
791 &link_ksettings);
792 if (err < 0)
793 return err;
794 if (!convert_link_ksettings_to_legacy_settings(&cmd,
795 &link_ksettings))
796 warn_incomplete_ethtool_legacy_settings_conversion(
797 "link modes are only partially reported");
798
799 /* send a sensible cmd tag back to user */
800 cmd.cmd = ETHTOOL_GSET;
801 } else {
3f1ac7a7
DD
802 /* driver doesn't support %ethtool_link_ksettings
803 * API. revert to legacy %ethtool_cmd API, unless it's
804 * not supported either.
805 */
3237fc63
DD
806 int err;
807
808 if (!dev->ethtool_ops->get_settings)
809 return -EOPNOTSUPP;
810
811 memset(&cmd, 0, sizeof(cmd));
812 cmd.cmd = ETHTOOL_GSET;
813 err = dev->ethtool_ops->get_settings(dev, &cmd);
3f1ac7a7
DD
814 if (err < 0)
815 return err;
816 }
1da177e4
LT
817
818 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
819 return -EFAULT;
3f1ac7a7 820
1da177e4
LT
821 return 0;
822}
823
3f1ac7a7
DD
824/* Update device link settings with given ethtool_cmd.
825 *
826 * Backward compatibility note: for compatibility with legacy ethtool,
827 * this has to work with both drivers implementing set_link_ksettings
828 * API and drivers implementing set_settings API. When drivers
829 * implement set_link_ksettings and user's request updates deprecated
830 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
831 * warning is logged once (with name of 1st driver/device) to
832 * recommend user to upgrade ethtool, and the request is rejected.
833 */
1da177e4
LT
834static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
835{
836 struct ethtool_cmd cmd;
837
3f1ac7a7 838 ASSERT_RTNL();
1da177e4
LT
839
840 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
841 return -EFAULT;
842
3f1ac7a7
DD
843 /* first, try new %ethtool_link_ksettings API. */
844 if (dev->ethtool_ops->set_link_ksettings) {
845 struct ethtool_link_ksettings link_ksettings;
846
847 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
848 &cmd))
849 return -EINVAL;
850
851 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
852 link_ksettings.base.link_mode_masks_nwords
853 = __ETHTOOL_LINK_MODE_MASK_NU32;
854 return dev->ethtool_ops->set_link_ksettings(dev,
855 &link_ksettings);
856 }
857
858 /* legacy %ethtool_cmd API */
859
860 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
861 * disappears internally
862 */
863
864 if (!dev->ethtool_ops->set_settings)
865 return -EOPNOTSUPP;
866
1da177e4
LT
867 return dev->ethtool_ops->set_settings(dev, &cmd);
868}
869
97f8aefb 870static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
871 void __user *useraddr)
1da177e4
LT
872{
873 struct ethtool_drvinfo info;
76fd8593 874 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 875
1da177e4
LT
876 memset(&info, 0, sizeof(info));
877 info.cmd = ETHTOOL_GDRVINFO;
c03a14e8 878 if (ops->get_drvinfo) {
01414802
BH
879 ops->get_drvinfo(dev, &info);
880 } else if (dev->dev.parent && dev->dev.parent->driver) {
881 strlcpy(info.bus_info, dev_name(dev->dev.parent),
882 sizeof(info.bus_info));
883 strlcpy(info.driver, dev->dev.parent->driver->name,
884 sizeof(info.driver));
885 } else {
886 return -EOPNOTSUPP;
887 }
1da177e4 888
723b2f57
JG
889 /*
890 * this method of obtaining string set info is deprecated;
d17792eb 891 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 892 */
c03a14e8 893 if (ops->get_sset_count) {
ff03d49f
JG
894 int rc;
895
896 rc = ops->get_sset_count(dev, ETH_SS_TEST);
897 if (rc >= 0)
898 info.testinfo_len = rc;
899 rc = ops->get_sset_count(dev, ETH_SS_STATS);
900 if (rc >= 0)
901 info.n_stats = rc;
339bf024
JG
902 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
903 if (rc >= 0)
904 info.n_priv_flags = rc;
ff03d49f 905 }
c03a14e8 906 if (ops->get_regs_len)
1da177e4 907 info.regdump_len = ops->get_regs_len(dev);
c03a14e8 908 if (ops->get_eeprom_len)
1da177e4
LT
909 info.eedump_len = ops->get_eeprom_len(dev);
910
911 if (copy_to_user(useraddr, &info, sizeof(info)))
912 return -EFAULT;
913 return 0;
914}
915
f5c445ed 916static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 917 void __user *useraddr)
723b2f57
JG
918{
919 struct ethtool_sset_info info;
723b2f57
JG
920 u64 sset_mask;
921 int i, idx = 0, n_bits = 0, ret, rc;
922 u32 *info_buf = NULL;
923
723b2f57
JG
924 if (copy_from_user(&info, useraddr, sizeof(info)))
925 return -EFAULT;
926
927 /* store copy of mask, because we zero struct later on */
928 sset_mask = info.sset_mask;
929 if (!sset_mask)
930 return 0;
931
932 /* calculate size of return buffer */
d17792eb 933 n_bits = hweight64(sset_mask);
723b2f57
JG
934
935 memset(&info, 0, sizeof(info));
936 info.cmd = ETHTOOL_GSSET_INFO;
937
938 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
939 if (!info_buf)
940 return -ENOMEM;
941
942 /*
943 * fill return buffer based on input bitmask and successful
944 * get_sset_count return
945 */
946 for (i = 0; i < 64; i++) {
947 if (!(sset_mask & (1ULL << i)))
948 continue;
949
340ae165 950 rc = __ethtool_get_sset_count(dev, i);
723b2f57
JG
951 if (rc >= 0) {
952 info.sset_mask |= (1ULL << i);
953 info_buf[idx++] = rc;
954 }
955 }
956
957 ret = -EFAULT;
958 if (copy_to_user(useraddr, &info, sizeof(info)))
959 goto out;
960
961 useraddr += offsetof(struct ethtool_sset_info, data);
962 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
963 goto out;
964
965 ret = 0;
966
967out:
968 kfree(info_buf);
969 return ret;
970}
971
97f8aefb 972static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
bf988435 973 u32 cmd, void __user *useraddr)
0853ad66 974{
bf988435
BH
975 struct ethtool_rxnfc info;
976 size_t info_size = sizeof(info);
55664f32 977 int rc;
0853ad66 978
59089d8d 979 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
980 return -EOPNOTSUPP;
981
bf988435
BH
982 /* struct ethtool_rxnfc was originally defined for
983 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
984 * members. User-space might still be using that
985 * definition. */
986 if (cmd == ETHTOOL_SRXFH)
987 info_size = (offsetof(struct ethtool_rxnfc, data) +
988 sizeof(info.data));
989
990 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
991 return -EFAULT;
992
55664f32
BH
993 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
994 if (rc)
995 return rc;
996
997 if (cmd == ETHTOOL_SRXCLSRLINS &&
998 copy_to_user(useraddr, &info, info_size))
999 return -EFAULT;
1000
1001 return 0;
0853ad66
SB
1002}
1003
97f8aefb 1004static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
bf988435 1005 u32 cmd, void __user *useraddr)
0853ad66
SB
1006{
1007 struct ethtool_rxnfc info;
bf988435 1008 size_t info_size = sizeof(info);
59089d8d
SB
1009 const struct ethtool_ops *ops = dev->ethtool_ops;
1010 int ret;
1011 void *rule_buf = NULL;
0853ad66 1012
59089d8d 1013 if (!ops->get_rxnfc)
0853ad66
SB
1014 return -EOPNOTSUPP;
1015
bf988435
BH
1016 /* struct ethtool_rxnfc was originally defined for
1017 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1018 * members. User-space might still be using that
1019 * definition. */
1020 if (cmd == ETHTOOL_GRXFH)
1021 info_size = (offsetof(struct ethtool_rxnfc, data) +
1022 sizeof(info.data));
1023
1024 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
1025 return -EFAULT;
1026
59089d8d
SB
1027 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1028 if (info.rule_cnt > 0) {
db048b69 1029 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
ae6df5f9 1030 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
db048b69 1031 GFP_USER);
59089d8d
SB
1032 if (!rule_buf)
1033 return -ENOMEM;
1034 }
1035 }
0853ad66 1036
59089d8d
SB
1037 ret = ops->get_rxnfc(dev, &info, rule_buf);
1038 if (ret < 0)
1039 goto err_out;
1040
1041 ret = -EFAULT;
bf988435 1042 if (copy_to_user(useraddr, &info, info_size))
59089d8d
SB
1043 goto err_out;
1044
1045 if (rule_buf) {
1046 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1047 if (copy_to_user(useraddr, rule_buf,
1048 info.rule_cnt * sizeof(u32)))
1049 goto err_out;
1050 }
1051 ret = 0;
1052
1053err_out:
c9caceca 1054 kfree(rule_buf);
59089d8d
SB
1055
1056 return ret;
0853ad66
SB
1057}
1058
3de0b592
VD
1059static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1060 struct ethtool_rxnfc *rx_rings,
1061 u32 size)
1062{
fb95cd8d 1063 int i;
3de0b592
VD
1064
1065 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
fb95cd8d 1066 return -EFAULT;
3de0b592
VD
1067
1068 /* Validate ring indices */
fb95cd8d
BH
1069 for (i = 0; i < size; i++)
1070 if (indir[i] >= rx_rings->data)
1071 return -EINVAL;
1072
1073 return 0;
3de0b592
VD
1074}
1075
ba905f5e 1076u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
960fb622
ED
1077
1078void netdev_rss_key_fill(void *buffer, size_t len)
1079{
1080 BUG_ON(len > sizeof(netdev_rss_key));
1081 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1082 memcpy(buffer, netdev_rss_key, len);
1083}
1084EXPORT_SYMBOL(netdev_rss_key_fill);
1085
d4ab4286
KJ
1086static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1087{
1088 u32 dev_size, current_max = 0;
1089 u32 *indir;
1090 int ret;
1091
1092 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1093 !dev->ethtool_ops->get_rxfh)
1094 return -EOPNOTSUPP;
1095 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1096 if (dev_size == 0)
1097 return -EOPNOTSUPP;
1098
1099 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1100 if (!indir)
1101 return -ENOMEM;
1102
1103 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1104 if (ret)
1105 goto out;
1106
1107 while (dev_size--)
1108 current_max = max(current_max, indir[dev_size]);
1109
1110 *max = current_max;
1111
1112out:
1113 kfree(indir);
1114 return ret;
1115}
1116
a5b6ee29
BH
1117static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1118 void __user *useraddr)
1119{
7850f63f
BH
1120 u32 user_size, dev_size;
1121 u32 *indir;
a5b6ee29
BH
1122 int ret;
1123
7850f63f 1124 if (!dev->ethtool_ops->get_rxfh_indir_size ||
fe62d001 1125 !dev->ethtool_ops->get_rxfh)
7850f63f
BH
1126 return -EOPNOTSUPP;
1127 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1128 if (dev_size == 0)
a5b6ee29
BH
1129 return -EOPNOTSUPP;
1130
7850f63f 1131 if (copy_from_user(&user_size,
a5b6ee29 1132 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 1133 sizeof(user_size)))
a5b6ee29
BH
1134 return -EFAULT;
1135
7850f63f
BH
1136 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1137 &dev_size, sizeof(dev_size)))
1138 return -EFAULT;
1139
1140 /* If the user buffer size is 0, this is just a query for the
1141 * device table size. Otherwise, if it's smaller than the
1142 * device table size it's an error.
1143 */
1144 if (user_size < dev_size)
1145 return user_size == 0 ? 0 : -EINVAL;
1146
1147 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
1148 if (!indir)
1149 return -ENOMEM;
1150
892311f6 1151 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
a5b6ee29
BH
1152 if (ret)
1153 goto out;
1154
7850f63f
BH
1155 if (copy_to_user(useraddr +
1156 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1157 indir, dev_size * sizeof(indir[0])))
a5b6ee29
BH
1158 ret = -EFAULT;
1159
1160out:
1161 kfree(indir);
1162 return ret;
1163}
1164
1165static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1166 void __user *useraddr)
1167{
7850f63f
BH
1168 struct ethtool_rxnfc rx_rings;
1169 u32 user_size, dev_size, i;
1170 u32 *indir;
c03a14e8 1171 const struct ethtool_ops *ops = dev->ethtool_ops;
a5b6ee29 1172 int ret;
3de0b592 1173 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
a5b6ee29 1174
fe62d001 1175 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
c03a14e8 1176 !ops->get_rxnfc)
7850f63f 1177 return -EOPNOTSUPP;
c03a14e8
JP
1178
1179 dev_size = ops->get_rxfh_indir_size(dev);
7850f63f 1180 if (dev_size == 0)
a5b6ee29
BH
1181 return -EOPNOTSUPP;
1182
7850f63f 1183 if (copy_from_user(&user_size,
a5b6ee29 1184 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 1185 sizeof(user_size)))
a5b6ee29
BH
1186 return -EFAULT;
1187
278bc429 1188 if (user_size != 0 && user_size != dev_size)
7850f63f
BH
1189 return -EINVAL;
1190
1191 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
1192 if (!indir)
1193 return -ENOMEM;
1194
7850f63f 1195 rx_rings.cmd = ETHTOOL_GRXRINGS;
c03a14e8 1196 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
7850f63f
BH
1197 if (ret)
1198 goto out;
278bc429
BH
1199
1200 if (user_size == 0) {
1201 for (i = 0; i < dev_size; i++)
1202 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1203 } else {
3de0b592
VD
1204 ret = ethtool_copy_validate_indir(indir,
1205 useraddr + ringidx_offset,
1206 &rx_rings,
1207 dev_size);
1208 if (ret)
1209 goto out;
1210 }
1211
892311f6 1212 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
d4ab4286
KJ
1213 if (ret)
1214 goto out;
1215
1216 /* indicate whether rxfh was set to default */
1217 if (user_size == 0)
1218 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1219 else
1220 dev->priv_flags |= IFF_RXFH_CONFIGURED;
3de0b592
VD
1221
1222out:
1223 kfree(indir);
1224 return ret;
1225}
1226
1227static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1228 void __user *useraddr)
1229{
1230 int ret;
1231 const struct ethtool_ops *ops = dev->ethtool_ops;
f062a384 1232 u32 user_indir_size, user_key_size;
3de0b592 1233 u32 dev_indir_size = 0, dev_key_size = 0;
f062a384 1234 struct ethtool_rxfh rxfh;
3de0b592 1235 u32 total_size;
f062a384 1236 u32 indir_bytes;
3de0b592 1237 u32 *indir = NULL;
892311f6 1238 u8 dev_hfunc = 0;
3de0b592
VD
1239 u8 *hkey = NULL;
1240 u8 *rss_config;
1241
892311f6 1242 if (!ops->get_rxfh)
3de0b592
VD
1243 return -EOPNOTSUPP;
1244
1245 if (ops->get_rxfh_indir_size)
1246 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592
VD
1247 if (ops->get_rxfh_key_size)
1248 dev_key_size = ops->get_rxfh_key_size(dev);
1249
f062a384 1250 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592 1251 return -EFAULT;
f062a384
BH
1252 user_indir_size = rxfh.indir_size;
1253 user_key_size = rxfh.key_size;
3de0b592 1254
f062a384 1255 /* Check that reserved fields are 0 for now */
892311f6
EP
1256 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1257 rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384
BH
1258 return -EINVAL;
1259
1260 rxfh.indir_size = dev_indir_size;
1261 rxfh.key_size = dev_key_size;
1262 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
3de0b592
VD
1263 return -EFAULT;
1264
3de0b592
VD
1265 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1266 (user_key_size && (user_key_size != dev_key_size)))
1267 return -EINVAL;
1268
1269 indir_bytes = user_indir_size * sizeof(indir[0]);
1270 total_size = indir_bytes + user_key_size;
1271 rss_config = kzalloc(total_size, GFP_USER);
1272 if (!rss_config)
1273 return -ENOMEM;
1274
1275 if (user_indir_size)
1276 indir = (u32 *)rss_config;
1277
1278 if (user_key_size)
1279 hkey = rss_config + indir_bytes;
1280
892311f6
EP
1281 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1282 if (ret)
1283 goto out;
3de0b592 1284
892311f6
EP
1285 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1286 &dev_hfunc, sizeof(rxfh.hfunc))) {
1287 ret = -EFAULT;
1288 } else if (copy_to_user(useraddr +
1289 offsetof(struct ethtool_rxfh, rss_config[0]),
1290 rss_config, total_size)) {
1291 ret = -EFAULT;
1292 }
1293out:
3de0b592
VD
1294 kfree(rss_config);
1295
1296 return ret;
1297}
1298
1299static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1300 void __user *useraddr)
1301{
1302 int ret;
1303 const struct ethtool_ops *ops = dev->ethtool_ops;
1304 struct ethtool_rxnfc rx_rings;
f062a384
BH
1305 struct ethtool_rxfh rxfh;
1306 u32 dev_indir_size = 0, dev_key_size = 0, i;
3de0b592
VD
1307 u32 *indir = NULL, indir_bytes = 0;
1308 u8 *hkey = NULL;
1309 u8 *rss_config;
3de0b592
VD
1310 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1311
892311f6 1312 if (!ops->get_rxnfc || !ops->set_rxfh)
3de0b592
VD
1313 return -EOPNOTSUPP;
1314
1315 if (ops->get_rxfh_indir_size)
1316 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592 1317 if (ops->get_rxfh_key_size)
d340c862 1318 dev_key_size = ops->get_rxfh_key_size(dev);
3de0b592 1319
f062a384 1320 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592
VD
1321 return -EFAULT;
1322
f062a384 1323 /* Check that reserved fields are 0 for now */
892311f6
EP
1324 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1325 rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384
BH
1326 return -EINVAL;
1327
892311f6
EP
1328 /* If either indir, hash key or function is valid, proceed further.
1329 * Must request at least one change: indir size, hash key or function.
3de0b592 1330 */
f062a384
BH
1331 if ((rxfh.indir_size &&
1332 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1333 rxfh.indir_size != dev_indir_size) ||
1334 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1335 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
892311f6 1336 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
3de0b592
VD
1337 return -EINVAL;
1338
f062a384 1339 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
3de0b592
VD
1340 indir_bytes = dev_indir_size * sizeof(indir[0]);
1341
f062a384 1342 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
3de0b592
VD
1343 if (!rss_config)
1344 return -ENOMEM;
1345
1346 rx_rings.cmd = ETHTOOL_GRXRINGS;
1347 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1348 if (ret)
1349 goto out;
1350
f062a384
BH
1351 /* rxfh.indir_size == 0 means reset the indir table to default.
1352 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
3de0b592 1353 */
f062a384
BH
1354 if (rxfh.indir_size &&
1355 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
3de0b592
VD
1356 indir = (u32 *)rss_config;
1357 ret = ethtool_copy_validate_indir(indir,
1358 useraddr + rss_cfg_offset,
1359 &rx_rings,
f062a384 1360 rxfh.indir_size);
3de0b592 1361 if (ret)
7850f63f 1362 goto out;
f062a384 1363 } else if (rxfh.indir_size == 0) {
3de0b592
VD
1364 indir = (u32 *)rss_config;
1365 for (i = 0; i < dev_indir_size; i++)
1366 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1367 }
278bc429 1368
f062a384 1369 if (rxfh.key_size) {
3de0b592
VD
1370 hkey = rss_config + indir_bytes;
1371 if (copy_from_user(hkey,
1372 useraddr + rss_cfg_offset + indir_bytes,
f062a384 1373 rxfh.key_size)) {
3de0b592
VD
1374 ret = -EFAULT;
1375 goto out;
278bc429 1376 }
7850f63f
BH
1377 }
1378
892311f6 1379 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
d4ab4286
KJ
1380 if (ret)
1381 goto out;
1382
1383 /* indicate whether rxfh was set to default */
1384 if (rxfh.indir_size == 0)
1385 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1386 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1387 dev->priv_flags |= IFF_RXFH_CONFIGURED;
a5b6ee29
BH
1388
1389out:
3de0b592 1390 kfree(rss_config);
a5b6ee29
BH
1391 return ret;
1392}
1393
1da177e4
LT
1394static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1395{
1396 struct ethtool_regs regs;
76fd8593 1397 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
1398 void *regbuf;
1399 int reglen, ret;
1400
1401 if (!ops->get_regs || !ops->get_regs_len)
1402 return -EOPNOTSUPP;
1403
1404 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1405 return -EFAULT;
1406
1407 reglen = ops->get_regs_len(dev);
1408 if (regs.len > reglen)
1409 regs.len = reglen;
1410
3808d348
SG
1411 regbuf = NULL;
1412 if (reglen) {
1413 regbuf = vzalloc(reglen);
1414 if (!regbuf)
1415 return -ENOMEM;
1416 }
1da177e4
LT
1417
1418 ops->get_regs(dev, &regs, regbuf);
1419
1420 ret = -EFAULT;
1421 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1422 goto out;
1423 useraddr += offsetof(struct ethtool_regs, data);
67ae7cf1 1424 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1da177e4
LT
1425 goto out;
1426 ret = 0;
1427
1428 out:
a77f5db3 1429 vfree(regbuf);
1da177e4
LT
1430 return ret;
1431}
1432
d73d3a8c
BH
1433static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1434{
1435 struct ethtool_value reset;
1436 int ret;
1437
1438 if (!dev->ethtool_ops->reset)
1439 return -EOPNOTSUPP;
1440
1441 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1442 return -EFAULT;
1443
1444 ret = dev->ethtool_ops->reset(dev, &reset.data);
1445 if (ret)
1446 return ret;
1447
1448 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1449 return -EFAULT;
1450 return 0;
1451}
1452
1da177e4
LT
1453static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1454{
8e557421 1455 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1da177e4
LT
1456
1457 if (!dev->ethtool_ops->get_wol)
1458 return -EOPNOTSUPP;
1459
1460 dev->ethtool_ops->get_wol(dev, &wol);
1461
1462 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1463 return -EFAULT;
1464 return 0;
1465}
1466
1467static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1468{
1469 struct ethtool_wolinfo wol;
1470
1471 if (!dev->ethtool_ops->set_wol)
1472 return -EOPNOTSUPP;
1473
1474 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1475 return -EFAULT;
1476
1477 return dev->ethtool_ops->set_wol(dev, &wol);
1478}
1479
80f12ecc
YM
1480static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1481{
1482 struct ethtool_eee edata;
1483 int rc;
1484
1485 if (!dev->ethtool_ops->get_eee)
1486 return -EOPNOTSUPP;
1487
1488 memset(&edata, 0, sizeof(struct ethtool_eee));
1489 edata.cmd = ETHTOOL_GEEE;
1490 rc = dev->ethtool_ops->get_eee(dev, &edata);
1491
1492 if (rc)
1493 return rc;
1494
1495 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1496 return -EFAULT;
1497
1498 return 0;
1499}
1500
1501static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1502{
1503 struct ethtool_eee edata;
1504
1505 if (!dev->ethtool_ops->set_eee)
1506 return -EOPNOTSUPP;
1507
1508 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1509 return -EFAULT;
1510
1511 return dev->ethtool_ops->set_eee(dev, &edata);
1512}
1513
1da177e4
LT
1514static int ethtool_nway_reset(struct net_device *dev)
1515{
1516 if (!dev->ethtool_ops->nway_reset)
1517 return -EOPNOTSUPP;
1518
1519 return dev->ethtool_ops->nway_reset(dev);
1520}
1521
e596e6e4
BH
1522static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1523{
1524 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1525
1526 if (!dev->ethtool_ops->get_link)
1527 return -EOPNOTSUPP;
1528
1529 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1530
1531 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1532 return -EFAULT;
1533 return 0;
1534}
1535
081d094e
BH
1536static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1537 int (*getter)(struct net_device *,
1538 struct ethtool_eeprom *, u8 *),
1539 u32 total_len)
1da177e4
LT
1540{
1541 struct ethtool_eeprom eeprom;
b131dd5d
MSB
1542 void __user *userbuf = useraddr + sizeof(eeprom);
1543 u32 bytes_remaining;
1da177e4 1544 u8 *data;
b131dd5d 1545 int ret = 0;
1da177e4 1546
1da177e4
LT
1547 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1548 return -EFAULT;
1549
1550 /* Check for wrap and zero */
1551 if (eeprom.offset + eeprom.len <= eeprom.offset)
1552 return -EINVAL;
1553
1554 /* Check for exceeding total eeprom len */
081d094e 1555 if (eeprom.offset + eeprom.len > total_len)
1da177e4
LT
1556 return -EINVAL;
1557
b131dd5d 1558 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1559 if (!data)
1560 return -ENOMEM;
1561
b131dd5d
MSB
1562 bytes_remaining = eeprom.len;
1563 while (bytes_remaining > 0) {
1564 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1565
081d094e 1566 ret = getter(dev, &eeprom, data);
b131dd5d
MSB
1567 if (ret)
1568 break;
1569 if (copy_to_user(userbuf, data, eeprom.len)) {
1570 ret = -EFAULT;
1571 break;
1572 }
1573 userbuf += eeprom.len;
1574 eeprom.offset += eeprom.len;
1575 bytes_remaining -= eeprom.len;
1576 }
1da177e4 1577
c5835df9
MSB
1578 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1579 eeprom.offset -= eeprom.len;
1580 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1581 ret = -EFAULT;
1582
1da177e4
LT
1583 kfree(data);
1584 return ret;
1585}
1586
081d094e
BH
1587static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1588{
1589 const struct ethtool_ops *ops = dev->ethtool_ops;
1590
e0fb6fb6
GR
1591 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1592 !ops->get_eeprom_len(dev))
081d094e
BH
1593 return -EOPNOTSUPP;
1594
1595 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1596 ops->get_eeprom_len(dev));
1597}
1598
1da177e4
LT
1599static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1600{
1601 struct ethtool_eeprom eeprom;
76fd8593 1602 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
1603 void __user *userbuf = useraddr + sizeof(eeprom);
1604 u32 bytes_remaining;
1da177e4 1605 u8 *data;
b131dd5d 1606 int ret = 0;
1da177e4 1607
e0fb6fb6
GR
1608 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1609 !ops->get_eeprom_len(dev))
1da177e4
LT
1610 return -EOPNOTSUPP;
1611
1612 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1613 return -EFAULT;
1614
1615 /* Check for wrap and zero */
1616 if (eeprom.offset + eeprom.len <= eeprom.offset)
1617 return -EINVAL;
1618
1619 /* Check for exceeding total eeprom len */
1620 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1621 return -EINVAL;
1622
b131dd5d 1623 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1624 if (!data)
1625 return -ENOMEM;
1626
b131dd5d
MSB
1627 bytes_remaining = eeprom.len;
1628 while (bytes_remaining > 0) {
1629 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1630
1631 if (copy_from_user(data, userbuf, eeprom.len)) {
1632 ret = -EFAULT;
1633 break;
1634 }
1635 ret = ops->set_eeprom(dev, &eeprom, data);
1636 if (ret)
1637 break;
1638 userbuf += eeprom.len;
1639 eeprom.offset += eeprom.len;
1640 bytes_remaining -= eeprom.len;
1641 }
1da177e4 1642
1da177e4
LT
1643 kfree(data);
1644 return ret;
1645}
1646
97f8aefb 1647static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1648 void __user *useraddr)
1da177e4 1649{
8e557421 1650 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
1651
1652 if (!dev->ethtool_ops->get_coalesce)
1653 return -EOPNOTSUPP;
1654
1655 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1656
1657 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1658 return -EFAULT;
1659 return 0;
1660}
1661
97f8aefb 1662static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1663 void __user *useraddr)
1da177e4
LT
1664{
1665 struct ethtool_coalesce coalesce;
1666
fa04ae5c 1667 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
1668 return -EOPNOTSUPP;
1669
1670 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1671 return -EFAULT;
1672
1673 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1674}
1675
1676static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1677{
8e557421 1678 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
1679
1680 if (!dev->ethtool_ops->get_ringparam)
1681 return -EOPNOTSUPP;
1682
1683 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1684
1685 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1686 return -EFAULT;
1687 return 0;
1688}
1689
1690static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1691{
1692 struct ethtool_ringparam ringparam;
1693
1694 if (!dev->ethtool_ops->set_ringparam)
1695 return -EOPNOTSUPP;
1696
1697 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1698 return -EFAULT;
1699
1700 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1701}
1702
8b5933c3 1703static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1704 void __user *useraddr)
1705{
1706 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1707
1708 if (!dev->ethtool_ops->get_channels)
1709 return -EOPNOTSUPP;
1710
1711 dev->ethtool_ops->get_channels(dev, &channels);
1712
1713 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1714 return -EFAULT;
1715 return 0;
1716}
1717
1718static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1719 void __user *useraddr)
1720{
31a86d13 1721 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
d4ab4286 1722 u32 max_rx_in_use = 0;
8b5933c3 1723
8bf36862 1724 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
8b5933c3 1725 return -EOPNOTSUPP;
1726
1727 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1728 return -EFAULT;
1729
8bf36862
KJ
1730 dev->ethtool_ops->get_channels(dev, &max);
1731
1732 /* ensure new counts are within the maximums */
1733 if ((channels.rx_count > max.max_rx) ||
1734 (channels.tx_count > max.max_tx) ||
1735 (channels.combined_count > max.max_combined) ||
1736 (channels.other_count > max.max_other))
1737 return -EINVAL;
1738
d4ab4286
KJ
1739 /* ensure the new Rx count fits within the configured Rx flow
1740 * indirection table settings */
1741 if (netif_is_rxfh_configured(dev) &&
1742 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1743 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1744 return -EINVAL;
1745
8b5933c3 1746 return dev->ethtool_ops->set_channels(dev, &channels);
1747}
1748
1da177e4
LT
1749static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1750{
1751 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1752
1753 if (!dev->ethtool_ops->get_pauseparam)
1754 return -EOPNOTSUPP;
1755
1756 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1757
1758 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1759 return -EFAULT;
1760 return 0;
1761}
1762
1763static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1764{
1765 struct ethtool_pauseparam pauseparam;
1766
e1b90c41 1767 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
1768 return -EOPNOTSUPP;
1769
1770 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1771 return -EFAULT;
1772
1773 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1774}
1775
1da177e4
LT
1776static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1777{
1778 struct ethtool_test test;
76fd8593 1779 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1780 u64 *data;
ff03d49f 1781 int ret, test_len;
1da177e4 1782
a9828ec6 1783 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
1784 return -EOPNOTSUPP;
1785
a9828ec6 1786 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
1787 if (test_len < 0)
1788 return test_len;
1789 WARN_ON(test_len == 0);
1790
1da177e4
LT
1791 if (copy_from_user(&test, useraddr, sizeof(test)))
1792 return -EFAULT;
1793
ff03d49f
JG
1794 test.len = test_len;
1795 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
1796 if (!data)
1797 return -ENOMEM;
1798
1799 ops->self_test(dev, &test, data);
1800
1801 ret = -EFAULT;
1802 if (copy_to_user(useraddr, &test, sizeof(test)))
1803 goto out;
1804 useraddr += sizeof(test);
1805 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1806 goto out;
1807 ret = 0;
1808
1809 out:
1810 kfree(data);
1811 return ret;
1812}
1813
1814static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1815{
1816 struct ethtool_gstrings gstrings;
1da177e4
LT
1817 u8 *data;
1818 int ret;
1819
1da177e4
LT
1820 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1821 return -EFAULT;
1822
340ae165 1823 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
1824 if (ret < 0)
1825 return ret;
4d1ceea8
AS
1826 if (ret > S32_MAX / ETH_GSTRING_LEN)
1827 return -ENOMEM;
1828 WARN_ON_ONCE(!ret);
a9828ec6
BH
1829
1830 gstrings.len = ret;
4d1ceea8
AS
1831 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1832 if (gstrings.len && !data)
1da177e4
LT
1833 return -ENOMEM;
1834
340ae165 1835 __ethtool_get_strings(dev, gstrings.string_set, data);
1da177e4
LT
1836
1837 ret = -EFAULT;
1838 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1839 goto out;
1840 useraddr += sizeof(gstrings);
4d1ceea8
AS
1841 if (gstrings.len &&
1842 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1da177e4
LT
1843 goto out;
1844 ret = 0;
1845
340ae165 1846out:
4d1ceea8 1847 vfree(data);
1da177e4
LT
1848 return ret;
1849}
1850
1851static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1852{
1853 struct ethtool_value id;
68f512f2 1854 static bool busy;
c03a14e8 1855 const struct ethtool_ops *ops = dev->ethtool_ops;
68f512f2 1856 int rc;
1da177e4 1857
c03a14e8 1858 if (!ops->set_phys_id)
1da177e4
LT
1859 return -EOPNOTSUPP;
1860
68f512f2
BH
1861 if (busy)
1862 return -EBUSY;
1863
1da177e4
LT
1864 if (copy_from_user(&id, useraddr, sizeof(id)))
1865 return -EFAULT;
1866
c03a14e8 1867 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 1868 if (rc < 0)
68f512f2
BH
1869 return rc;
1870
1871 /* Drop the RTNL lock while waiting, but prevent reentry or
1872 * removal of the device.
1873 */
1874 busy = true;
1875 dev_hold(dev);
1876 rtnl_unlock();
1877
1878 if (rc == 0) {
1879 /* Driver will handle this itself */
1880 schedule_timeout_interruptible(
143780c6 1881 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 1882 } else {
fce55922
AB
1883 /* Driver expects to be called at twice the frequency in rc */
1884 int n = rc * 2, i, interval = HZ / n;
1885
1886 /* Count down seconds */
68f512f2 1887 do {
fce55922
AB
1888 /* Count down iterations per second */
1889 i = n;
1890 do {
1891 rtnl_lock();
c03a14e8 1892 rc = ops->set_phys_id(dev,
fce55922
AB
1893 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1894 rtnl_unlock();
1895 if (rc)
1896 break;
1897 schedule_timeout_interruptible(interval);
1898 } while (!signal_pending(current) && --i != 0);
68f512f2
BH
1899 } while (!signal_pending(current) &&
1900 (id.data == 0 || --id.data != 0));
1901 }
1902
1903 rtnl_lock();
1904 dev_put(dev);
1905 busy = false;
1906
c03a14e8 1907 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
68f512f2 1908 return rc;
1da177e4
LT
1909}
1910
1911static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1912{
1913 struct ethtool_stats stats;
76fd8593 1914 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1915 u64 *data;
ff03d49f 1916 int ret, n_stats;
1da177e4 1917
a9828ec6 1918 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1919 return -EOPNOTSUPP;
1920
a9828ec6 1921 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1922 if (n_stats < 0)
1923 return n_stats;
4d1ceea8
AS
1924 if (n_stats > S32_MAX / sizeof(u64))
1925 return -ENOMEM;
1926 WARN_ON_ONCE(!n_stats);
1da177e4
LT
1927 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1928 return -EFAULT;
1929
ff03d49f 1930 stats.n_stats = n_stats;
4d1ceea8
AS
1931 data = vzalloc(n_stats * sizeof(u64));
1932 if (n_stats && !data)
1da177e4
LT
1933 return -ENOMEM;
1934
1935 ops->get_ethtool_stats(dev, &stats, data);
1936
1937 ret = -EFAULT;
1938 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1939 goto out;
1940 useraddr += sizeof(stats);
4d1ceea8 1941 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1da177e4
LT
1942 goto out;
1943 ret = 0;
1944
1945 out:
4d1ceea8 1946 vfree(data);
1da177e4
LT
1947 return ret;
1948}
1949
f3a40945
AL
1950static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1951{
1952 struct ethtool_stats stats;
1953 struct phy_device *phydev = dev->phydev;
1954 u64 *data;
1955 int ret, n_stats;
1956
1957 if (!phydev)
1958 return -EOPNOTSUPP;
1959
1960 n_stats = phy_get_sset_count(phydev);
f3a40945
AL
1961 if (n_stats < 0)
1962 return n_stats;
4d1ceea8
AS
1963 if (n_stats > S32_MAX / sizeof(u64))
1964 return -ENOMEM;
1965 WARN_ON_ONCE(!n_stats);
f3a40945
AL
1966
1967 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1968 return -EFAULT;
1969
1970 stats.n_stats = n_stats;
4d1ceea8
AS
1971 data = vzalloc(n_stats * sizeof(u64));
1972 if (n_stats && !data)
f3a40945
AL
1973 return -ENOMEM;
1974
1975 mutex_lock(&phydev->lock);
1976 phydev->drv->get_stats(phydev, &stats, data);
1977 mutex_unlock(&phydev->lock);
1978
1979 ret = -EFAULT;
1980 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1981 goto out;
1982 useraddr += sizeof(stats);
4d1ceea8 1983 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
f3a40945
AL
1984 goto out;
1985 ret = 0;
1986
1987 out:
4d1ceea8 1988 vfree(data);
f3a40945
AL
1989 return ret;
1990}
1991
0bf0519d 1992static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
1993{
1994 struct ethtool_perm_addr epaddr;
a6f9a705 1995
313674af 1996 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
1997 return -EFAULT;
1998
313674af
MW
1999 if (epaddr.size < dev->addr_len)
2000 return -ETOOSMALL;
2001 epaddr.size = dev->addr_len;
a6f9a705 2002
a6f9a705 2003 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 2004 return -EFAULT;
a6f9a705 2005 useraddr += sizeof(epaddr);
313674af
MW
2006 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2007 return -EFAULT;
2008 return 0;
a6f9a705
JW
2009}
2010
13c99b24
JG
2011static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2012 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 2013{
8e557421 2014 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 2015
13c99b24 2016 if (!actor)
3ae7c0b2
JG
2017 return -EOPNOTSUPP;
2018
13c99b24 2019 edata.data = actor(dev);
3ae7c0b2
JG
2020
2021 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2022 return -EFAULT;
2023 return 0;
2024}
2025
13c99b24
JG
2026static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2027 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
2028{
2029 struct ethtool_value edata;
2030
13c99b24 2031 if (!actor)
3ae7c0b2
JG
2032 return -EOPNOTSUPP;
2033
2034 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2035 return -EFAULT;
2036
13c99b24 2037 actor(dev, edata.data);
339bf024
JG
2038 return 0;
2039}
2040
13c99b24
JG
2041static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2042 int (*actor)(struct net_device *, u32))
339bf024
JG
2043{
2044 struct ethtool_value edata;
2045
13c99b24 2046 if (!actor)
339bf024
JG
2047 return -EOPNOTSUPP;
2048
2049 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2050 return -EFAULT;
2051
13c99b24 2052 return actor(dev, edata.data);
339bf024
JG
2053}
2054
97f8aefb 2055static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2056 char __user *useraddr)
05c6a8d7
AK
2057{
2058 struct ethtool_flash efl;
2059
2060 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2061 return -EFAULT;
2062
2063 if (!dev->ethtool_ops->flash_device)
2064 return -EOPNOTSUPP;
2065
786f5281
BH
2066 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2067
05c6a8d7
AK
2068 return dev->ethtool_ops->flash_device(dev, &efl);
2069}
2070
29dd54b7
AC
2071static int ethtool_set_dump(struct net_device *dev,
2072 void __user *useraddr)
2073{
2074 struct ethtool_dump dump;
2075
2076 if (!dev->ethtool_ops->set_dump)
2077 return -EOPNOTSUPP;
2078
2079 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2080 return -EFAULT;
2081
2082 return dev->ethtool_ops->set_dump(dev, &dump);
2083}
2084
2085static int ethtool_get_dump_flag(struct net_device *dev,
2086 void __user *useraddr)
2087{
2088 int ret;
2089 struct ethtool_dump dump;
2090 const struct ethtool_ops *ops = dev->ethtool_ops;
2091
c03a14e8 2092 if (!ops->get_dump_flag)
29dd54b7
AC
2093 return -EOPNOTSUPP;
2094
2095 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2096 return -EFAULT;
2097
2098 ret = ops->get_dump_flag(dev, &dump);
2099 if (ret)
2100 return ret;
2101
2102 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2103 return -EFAULT;
2104 return 0;
2105}
2106
2107static int ethtool_get_dump_data(struct net_device *dev,
2108 void __user *useraddr)
2109{
2110 int ret;
2111 __u32 len;
2112 struct ethtool_dump dump, tmp;
2113 const struct ethtool_ops *ops = dev->ethtool_ops;
2114 void *data = NULL;
2115
c03a14e8 2116 if (!ops->get_dump_data || !ops->get_dump_flag)
29dd54b7
AC
2117 return -EOPNOTSUPP;
2118
2119 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2120 return -EFAULT;
2121
2122 memset(&tmp, 0, sizeof(tmp));
2123 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2124 ret = ops->get_dump_flag(dev, &tmp);
2125 if (ret)
2126 return ret;
2127
c590b5e2 2128 len = min(tmp.len, dump.len);
29dd54b7
AC
2129 if (!len)
2130 return -EFAULT;
2131
c590b5e2
MS
2132 /* Don't ever let the driver think there's more space available
2133 * than it requested with .get_dump_flag().
2134 */
2135 dump.len = len;
2136
2137 /* Always allocate enough space to hold the whole thing so that the
2138 * driver does not need to check the length and bother with partial
2139 * dumping.
2140 */
29dd54b7
AC
2141 data = vzalloc(tmp.len);
2142 if (!data)
2143 return -ENOMEM;
2144 ret = ops->get_dump_data(dev, &dump, data);
2145 if (ret)
2146 goto out;
2147
c590b5e2
MS
2148 /* There are two sane possibilities:
2149 * 1. The driver's .get_dump_data() does not touch dump.len.
2150 * 2. Or it may set dump.len to how much it really writes, which
2151 * should be tmp.len (or len if it can do a partial dump).
2152 * In any case respond to userspace with the actual length of data
2153 * it's receiving.
2154 */
2155 WARN_ON(dump.len != len && dump.len != tmp.len);
2156 dump.len = len;
2157
29dd54b7
AC
2158 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2159 ret = -EFAULT;
2160 goto out;
2161 }
2162 useraddr += offsetof(struct ethtool_dump, data);
2163 if (copy_to_user(useraddr, data, len))
2164 ret = -EFAULT;
2165out:
2166 vfree(data);
2167 return ret;
2168}
2169
c8f3a8c3
RC
2170static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2171{
2172 int err = 0;
2173 struct ethtool_ts_info info;
2174 const struct ethtool_ops *ops = dev->ethtool_ops;
2175 struct phy_device *phydev = dev->phydev;
2176
2177 memset(&info, 0, sizeof(info));
2178 info.cmd = ETHTOOL_GET_TS_INFO;
2179
2180 if (phydev && phydev->drv && phydev->drv->ts_info) {
c8f3a8c3 2181 err = phydev->drv->ts_info(phydev, &info);
c03a14e8 2182 } else if (ops->get_ts_info) {
c8f3a8c3 2183 err = ops->get_ts_info(dev, &info);
c8f3a8c3
RC
2184 } else {
2185 info.so_timestamping =
2186 SOF_TIMESTAMPING_RX_SOFTWARE |
2187 SOF_TIMESTAMPING_SOFTWARE;
2188 info.phc_index = -1;
2189 }
2190
2191 if (err)
2192 return err;
2193
2194 if (copy_to_user(useraddr, &info, sizeof(info)))
2195 err = -EFAULT;
2196
2197 return err;
2198}
2199
2f438366
ES
2200static int __ethtool_get_module_info(struct net_device *dev,
2201 struct ethtool_modinfo *modinfo)
2202{
2203 const struct ethtool_ops *ops = dev->ethtool_ops;
2204 struct phy_device *phydev = dev->phydev;
2205
2206 if (phydev && phydev->drv && phydev->drv->module_info)
2207 return phydev->drv->module_info(phydev, modinfo);
2208
2209 if (ops->get_module_info)
2210 return ops->get_module_info(dev, modinfo);
2211
2212 return -EOPNOTSUPP;
2213}
2214
41c3cb6d
SH
2215static int ethtool_get_module_info(struct net_device *dev,
2216 void __user *useraddr)
2217{
2218 int ret;
2219 struct ethtool_modinfo modinfo;
41c3cb6d
SH
2220
2221 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2222 return -EFAULT;
2223
2f438366 2224 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
2225 if (ret)
2226 return ret;
2227
2228 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2229 return -EFAULT;
2230
2231 return 0;
2232}
2233
2f438366
ES
2234static int __ethtool_get_module_eeprom(struct net_device *dev,
2235 struct ethtool_eeprom *ee, u8 *data)
2236{
2237 const struct ethtool_ops *ops = dev->ethtool_ops;
2238 struct phy_device *phydev = dev->phydev;
2239
2240 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2241 return phydev->drv->module_eeprom(phydev, ee, data);
2242
2243 if (ops->get_module_eeprom)
2244 return ops->get_module_eeprom(dev, ee, data);
2245
2246 return -EOPNOTSUPP;
2247}
2248
41c3cb6d
SH
2249static int ethtool_get_module_eeprom(struct net_device *dev,
2250 void __user *useraddr)
2251{
2252 int ret;
2253 struct ethtool_modinfo modinfo;
41c3cb6d 2254
2f438366 2255 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
2256 if (ret)
2257 return ret;
2258
2f438366
ES
2259 return ethtool_get_any_eeprom(dev, useraddr,
2260 __ethtool_get_module_eeprom,
41c3cb6d
SH
2261 modinfo.eeprom_len);
2262}
2263
f0db9b07
GV
2264static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2265{
2266 switch (tuna->id) {
2267 case ETHTOOL_RX_COPYBREAK:
1255a505 2268 case ETHTOOL_TX_COPYBREAK:
f0db9b07
GV
2269 if (tuna->len != sizeof(u32) ||
2270 tuna->type_id != ETHTOOL_TUNABLE_U32)
2271 return -EINVAL;
2272 break;
2273 default:
2274 return -EINVAL;
2275 }
2276
2277 return 0;
2278}
2279
2280static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2281{
2282 int ret;
2283 struct ethtool_tunable tuna;
2284 const struct ethtool_ops *ops = dev->ethtool_ops;
2285 void *data;
2286
2287 if (!ops->get_tunable)
2288 return -EOPNOTSUPP;
2289 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2290 return -EFAULT;
2291 ret = ethtool_tunable_valid(&tuna);
2292 if (ret)
2293 return ret;
2294 data = kmalloc(tuna.len, GFP_USER);
2295 if (!data)
2296 return -ENOMEM;
2297 ret = ops->get_tunable(dev, &tuna, data);
2298 if (ret)
2299 goto out;
2300 useraddr += sizeof(tuna);
2301 ret = -EFAULT;
2302 if (copy_to_user(useraddr, data, tuna.len))
2303 goto out;
2304 ret = 0;
2305
2306out:
2307 kfree(data);
2308 return ret;
2309}
2310
2311static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2312{
2313 int ret;
2314 struct ethtool_tunable tuna;
2315 const struct ethtool_ops *ops = dev->ethtool_ops;
2316 void *data;
2317
2318 if (!ops->set_tunable)
2319 return -EOPNOTSUPP;
2320 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2321 return -EFAULT;
2322 ret = ethtool_tunable_valid(&tuna);
2323 if (ret)
2324 return ret;
2325 data = kmalloc(tuna.len, GFP_USER);
2326 if (!data)
2327 return -ENOMEM;
2328 useraddr += sizeof(tuna);
2329 ret = -EFAULT;
2330 if (copy_from_user(data, useraddr, tuna.len))
2331 goto out;
2332 ret = ops->set_tunable(dev, &tuna, data);
2333
2334out:
2335 kfree(data);
2336 return ret;
2337}
2338
421797b1
KL
2339static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2340 void __user *useraddr,
2341 struct ethtool_per_queue_op *per_queue_opt)
2342{
2343 u32 bit;
2344 int ret;
2345 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2346
2347 if (!dev->ethtool_ops->get_per_queue_coalesce)
2348 return -EOPNOTSUPP;
2349
2350 useraddr += sizeof(*per_queue_opt);
2351
2352 bitmap_from_u32array(queue_mask,
2353 MAX_NUM_QUEUE,
2354 per_queue_opt->queue_mask,
2355 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2356
2357 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2358 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2359
2360 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2361 if (ret != 0)
2362 return ret;
2363 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2364 return -EFAULT;
2365 useraddr += sizeof(coalesce);
2366 }
2367
2368 return 0;
2369}
2370
f38d138a
KL
2371static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2372 void __user *useraddr,
2373 struct ethtool_per_queue_op *per_queue_opt)
2374{
2375 u32 bit;
2376 int i, ret = 0;
2377 int n_queue;
2378 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2379 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2380
2381 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2382 (!dev->ethtool_ops->get_per_queue_coalesce))
2383 return -EOPNOTSUPP;
2384
2385 useraddr += sizeof(*per_queue_opt);
2386
2387 bitmap_from_u32array(queue_mask,
2388 MAX_NUM_QUEUE,
2389 per_queue_opt->queue_mask,
2390 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2391 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2392 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2393 if (!backup)
2394 return -ENOMEM;
2395
2396 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2397 struct ethtool_coalesce coalesce;
2398
2399 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2400 if (ret != 0)
2401 goto roll_back;
2402
2403 tmp++;
2404
2405 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2406 ret = -EFAULT;
2407 goto roll_back;
2408 }
2409
2410 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2411 if (ret != 0)
2412 goto roll_back;
2413
2414 useraddr += sizeof(coalesce);
2415 }
2416
2417roll_back:
2418 if (ret != 0) {
2419 tmp = backup;
2420 for_each_set_bit(i, queue_mask, bit) {
2421 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2422 tmp++;
2423 }
2424 }
2425 kfree(backup);
2426
2427 return ret;
2428}
2429
ac2c7ad0
KL
2430static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2431{
2432 struct ethtool_per_queue_op per_queue_opt;
2433
2434 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2435 return -EFAULT;
2436
2437 switch (per_queue_opt.sub_command) {
421797b1
KL
2438 case ETHTOOL_GCOALESCE:
2439 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
f38d138a
KL
2440 case ETHTOOL_SCOALESCE:
2441 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
ac2c7ad0
KL
2442 default:
2443 return -EOPNOTSUPP;
2444 };
2445}
2446
968ad9da
RL
2447static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2448{
2449 switch (tuna->id) {
65feddd5
RL
2450 case ETHTOOL_PHY_DOWNSHIFT:
2451 if (tuna->len != sizeof(u8) ||
2452 tuna->type_id != ETHTOOL_TUNABLE_U8)
2453 return -EINVAL;
2454 break;
968ad9da
RL
2455 default:
2456 return -EINVAL;
2457 }
2458
2459 return 0;
2460}
2461
2462static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2463{
2464 int ret;
2465 struct ethtool_tunable tuna;
2466 struct phy_device *phydev = dev->phydev;
2467 void *data;
2468
2469 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2470 return -EOPNOTSUPP;
2471
2472 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2473 return -EFAULT;
2474 ret = ethtool_phy_tunable_valid(&tuna);
2475 if (ret)
2476 return ret;
2477 data = kmalloc(tuna.len, GFP_USER);
2478 if (!data)
2479 return -ENOMEM;
4b65246b 2480 mutex_lock(&phydev->lock);
968ad9da 2481 ret = phydev->drv->get_tunable(phydev, &tuna, data);
4b65246b 2482 mutex_unlock(&phydev->lock);
968ad9da
RL
2483 if (ret)
2484 goto out;
2485 useraddr += sizeof(tuna);
2486 ret = -EFAULT;
2487 if (copy_to_user(useraddr, data, tuna.len))
2488 goto out;
2489 ret = 0;
2490
2491out:
2492 kfree(data);
2493 return ret;
2494}
2495
2496static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2497{
2498 int ret;
2499 struct ethtool_tunable tuna;
2500 struct phy_device *phydev = dev->phydev;
2501 void *data;
2502
2503 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2504 return -EOPNOTSUPP;
2505 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2506 return -EFAULT;
2507 ret = ethtool_phy_tunable_valid(&tuna);
2508 if (ret)
2509 return ret;
2510 data = kmalloc(tuna.len, GFP_USER);
2511 if (!data)
2512 return -ENOMEM;
2513 useraddr += sizeof(tuna);
2514 ret = -EFAULT;
2515 if (copy_from_user(data, useraddr, tuna.len))
2516 goto out;
4b65246b 2517 mutex_lock(&phydev->lock);
968ad9da 2518 ret = phydev->drv->set_tunable(phydev, &tuna, data);
4b65246b 2519 mutex_unlock(&phydev->lock);
968ad9da
RL
2520
2521out:
2522 kfree(data);
2523 return ret;
2524}
2525
600fed5e 2526/* The main entry point in this file. Called from net/core/dev_ioctl.c */
1da177e4 2527
881d966b 2528int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 2529{
881d966b 2530 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4 2531 void __user *useraddr = ifr->ifr_data;
ac2c7ad0 2532 u32 ethcmd, sub_cmd;
1da177e4 2533 int rc;
b29d3145 2534 netdev_features_t old_features;
1da177e4 2535
1da177e4
LT
2536 if (!dev || !netif_device_present(dev))
2537 return -ENODEV;
2538
97f8aefb 2539 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
2540 return -EFAULT;
2541
ac2c7ad0
KL
2542 if (ethcmd == ETHTOOL_PERQUEUE) {
2543 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2544 return -EFAULT;
2545 } else {
2546 sub_cmd = ethcmd;
2547 }
75f3123c 2548 /* Allow some commands to be done by anyone */
ac2c7ad0 2549 switch (sub_cmd) {
0fdc100b 2550 case ETHTOOL_GSET:
75f3123c 2551 case ETHTOOL_GDRVINFO:
75f3123c 2552 case ETHTOOL_GMSGLVL:
2da45db2 2553 case ETHTOOL_GLINK:
75f3123c
SH
2554 case ETHTOOL_GCOALESCE:
2555 case ETHTOOL_GRINGPARAM:
2556 case ETHTOOL_GPAUSEPARAM:
2557 case ETHTOOL_GRXCSUM:
2558 case ETHTOOL_GTXCSUM:
2559 case ETHTOOL_GSG:
f80400a2 2560 case ETHTOOL_GSSET_INFO:
75f3123c 2561 case ETHTOOL_GSTRINGS:
2da45db2 2562 case ETHTOOL_GSTATS:
f3a40945 2563 case ETHTOOL_GPHYSTATS:
75f3123c
SH
2564 case ETHTOOL_GTSO:
2565 case ETHTOOL_GPERMADDR:
2566 case ETHTOOL_GUFO:
2567 case ETHTOOL_GGSO:
1cab819b 2568 case ETHTOOL_GGRO:
339bf024
JG
2569 case ETHTOOL_GFLAGS:
2570 case ETHTOOL_GPFLAGS:
0853ad66 2571 case ETHTOOL_GRXFH:
59089d8d
SB
2572 case ETHTOOL_GRXRINGS:
2573 case ETHTOOL_GRXCLSRLCNT:
2574 case ETHTOOL_GRXCLSRULE:
2575 case ETHTOOL_GRXCLSRLALL:
2da45db2 2576 case ETHTOOL_GRXFHINDIR:
3de0b592 2577 case ETHTOOL_GRSSH:
5455c699 2578 case ETHTOOL_GFEATURES:
2da45db2 2579 case ETHTOOL_GCHANNELS:
c8f3a8c3 2580 case ETHTOOL_GET_TS_INFO:
2da45db2 2581 case ETHTOOL_GEEE:
f0db9b07 2582 case ETHTOOL_GTUNABLE:
968ad9da 2583 case ETHTOOL_PHY_GTUNABLE:
8006f6bf 2584 case ETHTOOL_GLINKSETTINGS:
75f3123c
SH
2585 break;
2586 default:
5e1fccc0 2587 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
75f3123c
SH
2588 return -EPERM;
2589 }
2590
97f8aefb 2591 if (dev->ethtool_ops->begin) {
2592 rc = dev->ethtool_ops->begin(dev);
2593 if (rc < 0)
1da177e4 2594 return rc;
97f8aefb 2595 }
d8a33ac4
SH
2596 old_features = dev->features;
2597
1da177e4
LT
2598 switch (ethcmd) {
2599 case ETHTOOL_GSET:
2600 rc = ethtool_get_settings(dev, useraddr);
2601 break;
2602 case ETHTOOL_SSET:
2603 rc = ethtool_set_settings(dev, useraddr);
2604 break;
2605 case ETHTOOL_GDRVINFO:
2606 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
2607 break;
2608 case ETHTOOL_GREGS:
2609 rc = ethtool_get_regs(dev, useraddr);
2610 break;
2611 case ETHTOOL_GWOL:
2612 rc = ethtool_get_wol(dev, useraddr);
2613 break;
2614 case ETHTOOL_SWOL:
2615 rc = ethtool_set_wol(dev, useraddr);
2616 break;
2617 case ETHTOOL_GMSGLVL:
13c99b24
JG
2618 rc = ethtool_get_value(dev, useraddr, ethcmd,
2619 dev->ethtool_ops->get_msglevel);
1da177e4
LT
2620 break;
2621 case ETHTOOL_SMSGLVL:
13c99b24
JG
2622 rc = ethtool_set_value_void(dev, useraddr,
2623 dev->ethtool_ops->set_msglevel);
1da177e4 2624 break;
80f12ecc
YM
2625 case ETHTOOL_GEEE:
2626 rc = ethtool_get_eee(dev, useraddr);
2627 break;
2628 case ETHTOOL_SEEE:
2629 rc = ethtool_set_eee(dev, useraddr);
2630 break;
1da177e4
LT
2631 case ETHTOOL_NWAY_RST:
2632 rc = ethtool_nway_reset(dev);
2633 break;
2634 case ETHTOOL_GLINK:
e596e6e4 2635 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
2636 break;
2637 case ETHTOOL_GEEPROM:
2638 rc = ethtool_get_eeprom(dev, useraddr);
2639 break;
2640 case ETHTOOL_SEEPROM:
2641 rc = ethtool_set_eeprom(dev, useraddr);
2642 break;
2643 case ETHTOOL_GCOALESCE:
2644 rc = ethtool_get_coalesce(dev, useraddr);
2645 break;
2646 case ETHTOOL_SCOALESCE:
2647 rc = ethtool_set_coalesce(dev, useraddr);
2648 break;
2649 case ETHTOOL_GRINGPARAM:
2650 rc = ethtool_get_ringparam(dev, useraddr);
2651 break;
2652 case ETHTOOL_SRINGPARAM:
2653 rc = ethtool_set_ringparam(dev, useraddr);
2654 break;
2655 case ETHTOOL_GPAUSEPARAM:
2656 rc = ethtool_get_pauseparam(dev, useraddr);
2657 break;
2658 case ETHTOOL_SPAUSEPARAM:
2659 rc = ethtool_set_pauseparam(dev, useraddr);
2660 break;
1da177e4
LT
2661 case ETHTOOL_TEST:
2662 rc = ethtool_self_test(dev, useraddr);
2663 break;
2664 case ETHTOOL_GSTRINGS:
2665 rc = ethtool_get_strings(dev, useraddr);
2666 break;
2667 case ETHTOOL_PHYS_ID:
2668 rc = ethtool_phys_id(dev, useraddr);
2669 break;
2670 case ETHTOOL_GSTATS:
2671 rc = ethtool_get_stats(dev, useraddr);
2672 break;
a6f9a705
JW
2673 case ETHTOOL_GPERMADDR:
2674 rc = ethtool_get_perm_addr(dev, useraddr);
2675 break;
3ae7c0b2 2676 case ETHTOOL_GFLAGS:
13c99b24 2677 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 2678 __ethtool_get_flags);
3ae7c0b2
JG
2679 break;
2680 case ETHTOOL_SFLAGS:
da8ac86c 2681 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 2682 break;
339bf024 2683 case ETHTOOL_GPFLAGS:
13c99b24
JG
2684 rc = ethtool_get_value(dev, useraddr, ethcmd,
2685 dev->ethtool_ops->get_priv_flags);
339bf024
JG
2686 break;
2687 case ETHTOOL_SPFLAGS:
13c99b24
JG
2688 rc = ethtool_set_value(dev, useraddr,
2689 dev->ethtool_ops->set_priv_flags);
339bf024 2690 break;
0853ad66 2691 case ETHTOOL_GRXFH:
59089d8d
SB
2692 case ETHTOOL_GRXRINGS:
2693 case ETHTOOL_GRXCLSRLCNT:
2694 case ETHTOOL_GRXCLSRULE:
2695 case ETHTOOL_GRXCLSRLALL:
bf988435 2696 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
2697 break;
2698 case ETHTOOL_SRXFH:
59089d8d
SB
2699 case ETHTOOL_SRXCLSRLDEL:
2700 case ETHTOOL_SRXCLSRLINS:
bf988435 2701 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 2702 break;
05c6a8d7
AK
2703 case ETHTOOL_FLASHDEV:
2704 rc = ethtool_flash_device(dev, useraddr);
2705 break;
d73d3a8c
BH
2706 case ETHTOOL_RESET:
2707 rc = ethtool_reset(dev, useraddr);
2708 break;
723b2f57
JG
2709 case ETHTOOL_GSSET_INFO:
2710 rc = ethtool_get_sset_info(dev, useraddr);
2711 break;
a5b6ee29
BH
2712 case ETHTOOL_GRXFHINDIR:
2713 rc = ethtool_get_rxfh_indir(dev, useraddr);
2714 break;
2715 case ETHTOOL_SRXFHINDIR:
2716 rc = ethtool_set_rxfh_indir(dev, useraddr);
2717 break;
3de0b592
VD
2718 case ETHTOOL_GRSSH:
2719 rc = ethtool_get_rxfh(dev, useraddr);
2720 break;
2721 case ETHTOOL_SRSSH:
2722 rc = ethtool_set_rxfh(dev, useraddr);
2723 break;
5455c699
MM
2724 case ETHTOOL_GFEATURES:
2725 rc = ethtool_get_features(dev, useraddr);
2726 break;
2727 case ETHTOOL_SFEATURES:
2728 rc = ethtool_set_features(dev, useraddr);
2729 break;
0a417704 2730 case ETHTOOL_GTXCSUM:
e83d360d 2731 case ETHTOOL_GRXCSUM:
0a417704
MM
2732 case ETHTOOL_GSG:
2733 case ETHTOOL_GTSO:
2734 case ETHTOOL_GUFO:
2735 case ETHTOOL_GGSO:
2736 case ETHTOOL_GGRO:
2737 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2738 break;
2739 case ETHTOOL_STXCSUM:
e83d360d 2740 case ETHTOOL_SRXCSUM:
0a417704
MM
2741 case ETHTOOL_SSG:
2742 case ETHTOOL_STSO:
2743 case ETHTOOL_SUFO:
2744 case ETHTOOL_SGSO:
2745 case ETHTOOL_SGRO:
2746 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2747 break;
8b5933c3 2748 case ETHTOOL_GCHANNELS:
2749 rc = ethtool_get_channels(dev, useraddr);
2750 break;
2751 case ETHTOOL_SCHANNELS:
2752 rc = ethtool_set_channels(dev, useraddr);
2753 break;
29dd54b7
AC
2754 case ETHTOOL_SET_DUMP:
2755 rc = ethtool_set_dump(dev, useraddr);
2756 break;
2757 case ETHTOOL_GET_DUMP_FLAG:
2758 rc = ethtool_get_dump_flag(dev, useraddr);
2759 break;
2760 case ETHTOOL_GET_DUMP_DATA:
2761 rc = ethtool_get_dump_data(dev, useraddr);
2762 break;
c8f3a8c3
RC
2763 case ETHTOOL_GET_TS_INFO:
2764 rc = ethtool_get_ts_info(dev, useraddr);
2765 break;
41c3cb6d
SH
2766 case ETHTOOL_GMODULEINFO:
2767 rc = ethtool_get_module_info(dev, useraddr);
2768 break;
2769 case ETHTOOL_GMODULEEEPROM:
2770 rc = ethtool_get_module_eeprom(dev, useraddr);
2771 break;
f0db9b07
GV
2772 case ETHTOOL_GTUNABLE:
2773 rc = ethtool_get_tunable(dev, useraddr);
2774 break;
2775 case ETHTOOL_STUNABLE:
2776 rc = ethtool_set_tunable(dev, useraddr);
2777 break;
f3a40945
AL
2778 case ETHTOOL_GPHYSTATS:
2779 rc = ethtool_get_phy_stats(dev, useraddr);
2780 break;
ac2c7ad0
KL
2781 case ETHTOOL_PERQUEUE:
2782 rc = ethtool_set_per_queue(dev, useraddr);
2783 break;
3f1ac7a7
DD
2784 case ETHTOOL_GLINKSETTINGS:
2785 rc = ethtool_get_link_ksettings(dev, useraddr);
2786 break;
2787 case ETHTOOL_SLINKSETTINGS:
2788 rc = ethtool_set_link_ksettings(dev, useraddr);
2789 break;
968ad9da
RL
2790 case ETHTOOL_PHY_GTUNABLE:
2791 rc = get_phy_tunable(dev, useraddr);
2792 break;
2793 case ETHTOOL_PHY_STUNABLE:
2794 rc = set_phy_tunable(dev, useraddr);
2795 break;
1da177e4 2796 default:
61a44b9c 2797 rc = -EOPNOTSUPP;
1da177e4 2798 }
4ec93edb 2799
e71a4783 2800 if (dev->ethtool_ops->complete)
1da177e4 2801 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
2802
2803 if (old_features != dev->features)
2804 netdev_features_change(dev);
2805
1da177e4 2806 return rc;
1da177e4 2807}