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