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