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