Linux 6.12-rc1
[linux-block.git] / net / ethtool / ioctl.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * net/core/ethtool.c - Ethtool ioctl handler
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 *
6 * This file is where we call all the ethtool_ops commands to get
61a44b9c 7 * the information ethtool needs.
1da177e4
LT
8 */
9
dd98d289 10#include <linux/compat.h>
b6459415 11#include <linux/etherdevice.h>
1da177e4
LT
12#include <linux/module.h>
13#include <linux/types.h>
4fc268d2 14#include <linux/capability.h>
1da177e4
LT
15#include <linux/errno.h>
16#include <linux/ethtool.h>
17#include <linux/netdevice.h>
c8f3a8c3
RC
18#include <linux/net_tstamp.h>
19#include <linux/phy.h>
d17792eb 20#include <linux/bitops.h>
97f8aefb 21#include <linux/uaccess.h>
73da16c2 22#include <linux/vmalloc.h>
e679c9c1 23#include <linux/sfp.h>
5a0e3ad6 24#include <linux/slab.h>
68f512f2 25#include <linux/rtnetlink.h>
174cd4b1 26#include <linux/sched/signal.h>
960fb622 27#include <linux/net.h>
f32a2137 28#include <linux/pm_runtime.h>
d2efeb52 29#include <linux/utsname.h>
ddb6e99e 30#include <net/devlink.h>
8cdc3223 31#include <net/ipv6.h>
a71506a4 32#include <net/xdp_sock_drv.h>
eca4205f 33#include <net/flow_offload.h>
73286734 34#include <linux/ethtool_netlink.h>
d44e1310
MK
35#include "common.h"
36
095cfcfe
JK
37/* State held across locks and calls for commands which have devlink fallback */
38struct ethtool_devlink_compat {
1af0a094 39 struct devlink *devlink;
095cfcfe
JK
40 union {
41 struct ethtool_flash efl;
42 struct ethtool_drvinfo info;
43 };
44};
45
1af0a094
JK
46static struct devlink *netdev_to_devlink_get(struct net_device *dev)
47{
8eba37f7 48 if (!dev->devlink_port)
1af0a094 49 return NULL;
8eba37f7 50 return devlink_try_get(dev->devlink_port->devlink);
1af0a094
JK
51}
52
4ec93edb 53/*
1da177e4
LT
54 * Some useful ethtool_ops methods that're device independent.
55 * If we find that all drivers want to do the same thing here,
56 * we can turn these into dev_() function calls.
57 */
58
59u32 ethtool_op_get_link(struct net_device *dev)
60{
facd15df
JB
61 /* Synchronize carrier state with link watch, see also rtnl_getlink() */
62 linkwatch_sync_dev(dev);
63
1da177e4
LT
64 return netif_carrier_ok(dev) ? 1 : 0;
65}
97f8aefb 66EXPORT_SYMBOL(ethtool_op_get_link);
1da177e4 67
2111375b
KM
68int ethtool_op_get_ts_info(struct net_device *dev,
69 struct kernel_ethtool_ts_info *info)
02eacbd0
RC
70{
71 info->so_timestamping =
72 SOF_TIMESTAMPING_TX_SOFTWARE |
73 SOF_TIMESTAMPING_RX_SOFTWARE |
74 SOF_TIMESTAMPING_SOFTWARE;
75 info->phc_index = -1;
76 return 0;
77}
78EXPORT_SYMBOL(ethtool_op_get_ts_info);
79
1da177e4
LT
80/* Handlers for each ethtool command */
81
5455c699
MM
82static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
83{
84 struct ethtool_gfeatures cmd = {
85 .cmd = ETHTOOL_GFEATURES,
86 .size = ETHTOOL_DEV_FEATURE_WORDS,
87 };
475414f6 88 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
5455c699
MM
89 u32 __user *sizeaddr;
90 u32 copy_size;
475414f6
MM
91 int i;
92
93 /* in case feature bits run out again */
09da71b1 94 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
475414f6
MM
95
96 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
97 features[i].available = (u32)(dev->hw_features >> (32 * i));
98 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
99 features[i].active = (u32)(dev->features >> (32 * i));
100 features[i].never_changed =
101 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
475414f6 102 }
5455c699
MM
103
104 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
105 if (get_user(copy_size, sizeaddr))
106 return -EFAULT;
107
108 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
109 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
110
111 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
112 return -EFAULT;
113 useraddr += sizeof(cmd);
ed717613
GS
114 if (copy_to_user(useraddr, features,
115 array_size(copy_size, sizeof(*features))))
5455c699
MM
116 return -EFAULT;
117
118 return 0;
119}
120
121static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
122{
123 struct ethtool_sfeatures cmd;
124 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
475414f6
MM
125 netdev_features_t wanted = 0, valid = 0;
126 int i, ret = 0;
5455c699
MM
127
128 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
129 return -EFAULT;
130 useraddr += sizeof(cmd);
131
132 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
133 return -EINVAL;
134
135 if (copy_from_user(features, useraddr, sizeof(features)))
136 return -EFAULT;
137
475414f6 138 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
139 valid |= (netdev_features_t)features[i].valid << (32 * i);
140 wanted |= (netdev_features_t)features[i].requested << (32 * i);
475414f6
MM
141 }
142
143 if (valid & ~NETIF_F_ETHTOOL_BITS)
5455c699
MM
144 return -EINVAL;
145
475414f6
MM
146 if (valid & ~dev->hw_features) {
147 valid &= dev->hw_features;
5455c699
MM
148 ret |= ETHTOOL_F_UNSUPPORTED;
149 }
150
475414f6
MM
151 dev->wanted_features &= ~valid;
152 dev->wanted_features |= wanted & valid;
6cb6a27c 153 __netdev_update_features(dev);
5455c699 154
475414f6 155 if ((dev->wanted_features ^ dev->features) & valid)
5455c699
MM
156 ret |= ETHTOOL_F_WISH;
157
158 return ret;
159}
160
340ae165
MM
161static int __ethtool_get_sset_count(struct net_device *dev, int sset)
162{
17809516 163 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
340ae165
MM
164 const struct ethtool_ops *ops = dev->ethtool_ops;
165
5455c699
MM
166 if (sset == ETH_SS_FEATURES)
167 return ARRAY_SIZE(netdev_features_strings);
168
892311f6
EP
169 if (sset == ETH_SS_RSS_HASH_FUNCS)
170 return ARRAY_SIZE(rss_hash_func_strings);
171
a4244b0c
HHZ
172 if (sset == ETH_SS_TUNABLES)
173 return ARRAY_SIZE(tunable_strings);
174
968ad9da
RL
175 if (sset == ETH_SS_PHY_TUNABLES)
176 return ARRAY_SIZE(phy_tunable_strings);
177
99943382 178 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
17809516
FF
179 !ops->get_ethtool_phy_stats &&
180 phy_ops && phy_ops->get_sset_count)
181 return phy_ops->get_sset_count(dev->phydev);
f3a40945 182
428c122f
MK
183 if (sset == ETH_SS_LINK_MODES)
184 return __ETHTOOL_LINK_MODE_MASK_NBITS;
185
c03a14e8 186 if (ops->get_sset_count && ops->get_strings)
340ae165
MM
187 return ops->get_sset_count(dev, sset);
188 else
189 return -EOPNOTSUPP;
190}
191
192static void __ethtool_get_strings(struct net_device *dev,
193 u32 stringset, u8 *data)
194{
17809516 195 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
340ae165
MM
196 const struct ethtool_ops *ops = dev->ethtool_ops;
197
5455c699
MM
198 if (stringset == ETH_SS_FEATURES)
199 memcpy(data, netdev_features_strings,
200 sizeof(netdev_features_strings));
892311f6
EP
201 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
202 memcpy(data, rss_hash_func_strings,
203 sizeof(rss_hash_func_strings));
a4244b0c
HHZ
204 else if (stringset == ETH_SS_TUNABLES)
205 memcpy(data, tunable_strings, sizeof(tunable_strings));
968ad9da
RL
206 else if (stringset == ETH_SS_PHY_TUNABLES)
207 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
99943382 208 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
17809516
FF
209 !ops->get_ethtool_phy_stats && phy_ops &&
210 phy_ops->get_strings)
211 phy_ops->get_strings(dev->phydev, data);
428c122f
MK
212 else if (stringset == ETH_SS_LINK_MODES)
213 memcpy(data, link_mode_names,
214 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
99943382 215 else
5455c699
MM
216 /* ops->get_strings is valid because checked earlier */
217 ops->get_strings(dev, stringset, data);
340ae165
MM
218}
219
c8f44aff 220static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0a417704
MM
221{
222 /* feature masks of legacy discrete ethtool ops */
223
224 switch (eth_cmd) {
225 case ETHTOOL_GTXCSUM:
226 case ETHTOOL_STXCSUM:
9d648fb5 227 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
f70bb065 228 NETIF_F_SCTP_CRC;
e83d360d
MM
229 case ETHTOOL_GRXCSUM:
230 case ETHTOOL_SRXCSUM:
231 return NETIF_F_RXCSUM;
0a417704
MM
232 case ETHTOOL_GSG:
233 case ETHTOOL_SSG:
f70bb065 234 return NETIF_F_SG | NETIF_F_FRAGLIST;
0a417704
MM
235 case ETHTOOL_GTSO:
236 case ETHTOOL_STSO:
237 return NETIF_F_ALL_TSO;
0a417704
MM
238 case ETHTOOL_GGSO:
239 case ETHTOOL_SGSO:
240 return NETIF_F_GSO;
241 case ETHTOOL_GGRO:
242 case ETHTOOL_SGRO:
243 return NETIF_F_GRO;
244 default:
245 BUG();
246 }
247}
248
0a417704
MM
249static int ethtool_get_one_feature(struct net_device *dev,
250 char __user *useraddr, u32 ethcmd)
251{
c8f44aff 252 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0a417704
MM
253 struct ethtool_value edata = {
254 .cmd = ethcmd,
86794881 255 .data = !!(dev->features & mask),
0a417704 256 };
0a417704 257
0a417704
MM
258 if (copy_to_user(useraddr, &edata, sizeof(edata)))
259 return -EFAULT;
260 return 0;
261}
262
0a417704
MM
263static int ethtool_set_one_feature(struct net_device *dev,
264 void __user *useraddr, u32 ethcmd)
265{
266 struct ethtool_value edata;
c8f44aff 267 netdev_features_t mask;
0a417704
MM
268
269 if (copy_from_user(&edata, useraddr, sizeof(edata)))
270 return -EFAULT;
271
86794881
MM
272 mask = ethtool_get_feature_mask(ethcmd);
273 mask &= dev->hw_features;
bc5787c6
MM
274 if (!mask)
275 return -EOPNOTSUPP;
86794881 276
bc5787c6
MM
277 if (edata.data)
278 dev->wanted_features |= mask;
279 else
280 dev->wanted_features &= ~mask;
86794881 281
bc5787c6 282 __netdev_update_features(dev);
86794881 283
bc5787c6
MM
284 return 0;
285}
286
02b3a552
MM
287#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
288 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
f646968f
PM
289#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
290 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
291 NETIF_F_RXHASH)
bc5787c6
MM
292
293static u32 __ethtool_get_flags(struct net_device *dev)
294{
02b3a552
MM
295 u32 flags = 0;
296
8a73125c
DF
297 if (dev->features & NETIF_F_LRO)
298 flags |= ETH_FLAG_LRO;
299 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
300 flags |= ETH_FLAG_RXVLAN;
301 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
302 flags |= ETH_FLAG_TXVLAN;
303 if (dev->features & NETIF_F_NTUPLE)
304 flags |= ETH_FLAG_NTUPLE;
305 if (dev->features & NETIF_F_RXHASH)
306 flags |= ETH_FLAG_RXHASH;
02b3a552
MM
307
308 return flags;
0a417704
MM
309}
310
bc5787c6 311static int __ethtool_set_flags(struct net_device *dev, u32 data)
da8ac86c 312{
c8f44aff 313 netdev_features_t features = 0, changed;
da8ac86c 314
02b3a552 315 if (data & ~ETH_ALL_FLAGS)
da8ac86c
MM
316 return -EINVAL;
317
8a73125c
DF
318 if (data & ETH_FLAG_LRO)
319 features |= NETIF_F_LRO;
320 if (data & ETH_FLAG_RXVLAN)
321 features |= NETIF_F_HW_VLAN_CTAG_RX;
322 if (data & ETH_FLAG_TXVLAN)
323 features |= NETIF_F_HW_VLAN_CTAG_TX;
324 if (data & ETH_FLAG_NTUPLE)
325 features |= NETIF_F_NTUPLE;
326 if (data & ETH_FLAG_RXHASH)
327 features |= NETIF_F_RXHASH;
02b3a552 328
da8ac86c 329 /* allow changing only bits set in hw_features */
02b3a552 330 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
da8ac86c
MM
331 if (changed & ~dev->hw_features)
332 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
333
334 dev->wanted_features =
02b3a552 335 (dev->wanted_features & ~changed) | (features & changed);
da8ac86c 336
6cb6a27c 337 __netdev_update_features(dev);
da8ac86c
MM
338
339 return 0;
340}
341
5a6cd6de
AB
342/* Given two link masks, AND them together and save the result in dst. */
343void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
344 struct ethtool_link_ksettings *src)
345{
346 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
347 unsigned int idx = 0;
348
349 for (; idx < size; idx++) {
350 dst->link_modes.supported[idx] &=
351 src->link_modes.supported[idx];
352 dst->link_modes.advertising[idx] &=
353 src->link_modes.advertising[idx];
354 }
355}
356EXPORT_SYMBOL(ethtool_intersect_link_masks);
357
6d62b4d5
PR
358void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
359 u32 legacy_u32)
3f1ac7a7 360{
4973056c 361 linkmode_zero(dst);
3f1ac7a7
DD
362 dst[0] = legacy_u32;
363}
6d62b4d5 364EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
3f1ac7a7
DD
365
366/* return false if src had higher bits set. lower bits always updated. */
6d62b4d5
PR
367bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
368 const unsigned long *src)
3f1ac7a7 369{
3f1ac7a7 370 *legacy_u32 = src[0];
19d62f5e
MB
371 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
372 __ETHTOOL_LINK_MODE_MASK_NBITS;
3f1ac7a7 373}
6d62b4d5 374EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
3f1ac7a7 375
3f1ac7a7
DD
376/* return false if ksettings link modes had higher bits
377 * set. legacy_settings always updated (best effort)
378 */
379static bool
380convert_link_ksettings_to_legacy_settings(
381 struct ethtool_cmd *legacy_settings,
382 const struct ethtool_link_ksettings *link_ksettings)
383{
384 bool retval = true;
385
386 memset(legacy_settings, 0, sizeof(*legacy_settings));
387 /* this also clears the deprecated fields in legacy structure:
388 * __u8 transceiver;
389 * __u32 maxtxpkt;
390 * __u32 maxrxpkt;
391 */
392
6d62b4d5 393 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
394 &legacy_settings->supported,
395 link_ksettings->link_modes.supported);
6d62b4d5 396 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
397 &legacy_settings->advertising,
398 link_ksettings->link_modes.advertising);
6d62b4d5 399 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
400 &legacy_settings->lp_advertising,
401 link_ksettings->link_modes.lp_advertising);
402 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
403 legacy_settings->duplex
404 = link_ksettings->base.duplex;
405 legacy_settings->port
406 = link_ksettings->base.port;
407 legacy_settings->phy_address
408 = link_ksettings->base.phy_address;
409 legacy_settings->autoneg
410 = link_ksettings->base.autoneg;
411 legacy_settings->mdio_support
412 = link_ksettings->base.mdio_support;
413 legacy_settings->eth_tp_mdix
414 = link_ksettings->base.eth_tp_mdix;
415 legacy_settings->eth_tp_mdix_ctrl
416 = link_ksettings->base.eth_tp_mdix_ctrl;
19cab887
FF
417 legacy_settings->transceiver
418 = link_ksettings->base.transceiver;
3f1ac7a7
DD
419 return retval;
420}
421
422/* number of 32-bit words to store the user's link mode bitmaps */
423#define __ETHTOOL_LINK_MODE_MASK_NU32 \
424 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
425
426/* layout of the struct passed from/to userland */
427struct ethtool_link_usettings {
428 struct ethtool_link_settings base;
429 struct {
430 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
431 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
432 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
433 } link_modes;
434};
435
9b300495 436/* Internal kernel helper to query a device ethtool_link_settings. */
3f1ac7a7
DD
437int __ethtool_get_link_ksettings(struct net_device *dev,
438 struct ethtool_link_ksettings *link_ksettings)
439{
3f1ac7a7
DD
440 ASSERT_RTNL();
441
9b300495 442 if (!dev->ethtool_ops->get_link_ksettings)
3237fc63
DD
443 return -EOPNOTSUPP;
444
a699781c
JB
445 if (!netif_device_present(dev))
446 return -ENODEV;
447
9b300495 448 memset(link_ksettings, 0, sizeof(*link_ksettings));
a975d7d8 449 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
3f1ac7a7
DD
450}
451EXPORT_SYMBOL(__ethtool_get_link_ksettings);
452
453/* convert ethtool_link_usettings in user space to a kernel internal
454 * ethtool_link_ksettings. return 0 on success, errno on error.
455 */
456static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
457 const void __user *from)
458{
459 struct ethtool_link_usettings link_usettings;
460
461 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
462 return -EFAULT;
463
464 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
3aa56885
YN
465 bitmap_from_arr32(to->link_modes.supported,
466 link_usettings.link_modes.supported,
467 __ETHTOOL_LINK_MODE_MASK_NBITS);
468 bitmap_from_arr32(to->link_modes.advertising,
469 link_usettings.link_modes.advertising,
470 __ETHTOOL_LINK_MODE_MASK_NBITS);
471 bitmap_from_arr32(to->link_modes.lp_advertising,
472 link_usettings.link_modes.lp_advertising,
473 __ETHTOOL_LINK_MODE_MASK_NBITS);
3f1ac7a7
DD
474
475 return 0;
476}
477
70ae1e12
CF
478/* Check if the user is trying to change anything besides speed/duplex */
479bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
480{
481 struct ethtool_link_settings base2 = {};
482
483 base2.speed = cmd->base.speed;
484 base2.port = PORT_OTHER;
485 base2.duplex = cmd->base.duplex;
486 base2.cmd = cmd->base.cmd;
487 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
488
489 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
490 bitmap_empty(cmd->link_modes.supported,
491 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
492 bitmap_empty(cmd->link_modes.lp_advertising,
493 __ETHTOOL_LINK_MODE_MASK_NBITS);
494}
495
3f1ac7a7
DD
496/* convert a kernel internal ethtool_link_ksettings to
497 * ethtool_link_usettings in user space. return 0 on success, errno on
498 * error.
499 */
500static int
501store_link_ksettings_for_user(void __user *to,
502 const struct ethtool_link_ksettings *from)
503{
504 struct ethtool_link_usettings link_usettings;
505
c1d9e34e 506 memcpy(&link_usettings, from, sizeof(link_usettings));
3aa56885
YN
507 bitmap_to_arr32(link_usettings.link_modes.supported,
508 from->link_modes.supported,
509 __ETHTOOL_LINK_MODE_MASK_NBITS);
510 bitmap_to_arr32(link_usettings.link_modes.advertising,
511 from->link_modes.advertising,
512 __ETHTOOL_LINK_MODE_MASK_NBITS);
513 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
514 from->link_modes.lp_advertising,
515 __ETHTOOL_LINK_MODE_MASK_NBITS);
3f1ac7a7
DD
516
517 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
518 return -EFAULT;
519
520 return 0;
521}
522
9b300495 523/* Query device for its ethtool_link_settings. */
3f1ac7a7
DD
524static int ethtool_get_link_ksettings(struct net_device *dev,
525 void __user *useraddr)
526{
527 int err = 0;
528 struct ethtool_link_ksettings link_ksettings;
529
530 ASSERT_RTNL();
3f1ac7a7
DD
531 if (!dev->ethtool_ops->get_link_ksettings)
532 return -EOPNOTSUPP;
533
534 /* handle bitmap nbits handshake */
535 if (copy_from_user(&link_ksettings.base, useraddr,
536 sizeof(link_ksettings.base)))
537 return -EFAULT;
538
539 if (__ETHTOOL_LINK_MODE_MASK_NU32
540 != link_ksettings.base.link_mode_masks_nwords) {
541 /* wrong link mode nbits requested */
542 memset(&link_ksettings, 0, sizeof(link_ksettings));
793cf87d 543 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
3f1ac7a7
DD
544 /* send back number of words required as negative val */
545 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
546 "need too many bits for link modes!");
547 link_ksettings.base.link_mode_masks_nwords
548 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
549
550 /* copy the base fields back to user, not the link
551 * mode bitmaps
552 */
553 if (copy_to_user(useraddr, &link_ksettings.base,
554 sizeof(link_ksettings.base)))
555 return -EFAULT;
556
557 return 0;
558 }
559
560 /* handshake successful: user/kernel agree on
561 * link_mode_masks_nwords
562 */
563
564 memset(&link_ksettings, 0, sizeof(link_ksettings));
565 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
566 if (err < 0)
567 return err;
568
569 /* make sure we tell the right values to user */
570 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
571 link_ksettings.base.link_mode_masks_nwords
572 = __ETHTOOL_LINK_MODE_MASK_NU32;
bdbdac76
OR
573 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
574 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
0c3e10cb 575 link_ksettings.base.rate_matching = RATE_MATCH_NONE;
3f1ac7a7
DD
576
577 return store_link_ksettings_for_user(useraddr, &link_ksettings);
578}
579
9b300495 580/* Update device ethtool_link_settings. */
3f1ac7a7
DD
581static int ethtool_set_link_ksettings(struct net_device *dev,
582 void __user *useraddr)
583{
9ad685db 584 struct ethtool_link_ksettings link_ksettings = {};
3f1ac7a7 585 int err;
3f1ac7a7
DD
586
587 ASSERT_RTNL();
588
589 if (!dev->ethtool_ops->set_link_ksettings)
590 return -EOPNOTSUPP;
591
592 /* make sure nbits field has expected value */
593 if (copy_from_user(&link_ksettings.base, useraddr,
594 sizeof(link_ksettings.base)))
595 return -EFAULT;
596
597 if (__ETHTOOL_LINK_MODE_MASK_NU32
598 != link_ksettings.base.link_mode_masks_nwords)
599 return -EINVAL;
600
601 /* copy the whole structure, now that we know it has expected
602 * format
603 */
604 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
605 if (err)
606 return err;
607
608 /* re-check nwords field, just in case */
609 if (__ETHTOOL_LINK_MODE_MASK_NU32
610 != link_ksettings.base.link_mode_masks_nwords)
611 return -EINVAL;
612
bdbdac76
OR
613 if (link_ksettings.base.master_slave_cfg ||
614 link_ksettings.base.master_slave_state)
615 return -EINVAL;
616
73286734 617 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
1b1b1847 618 if (err >= 0) {
73286734 619 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
1b1b1847
MK
620 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
621 }
73286734 622 return err;
3f1ac7a7
DD
623}
624
70ae1e12
CF
625int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
626 const struct ethtool_link_ksettings *cmd,
627 u32 *dev_speed, u8 *dev_duplex)
628{
629 u32 speed;
630 u8 duplex;
631
632 speed = cmd->base.speed;
633 duplex = cmd->base.duplex;
634 /* don't allow custom speed and duplex */
635 if (!ethtool_validate_speed(speed) ||
636 !ethtool_validate_duplex(duplex) ||
637 !ethtool_virtdev_validate_cmd(cmd))
638 return -EINVAL;
639 *dev_speed = speed;
640 *dev_duplex = duplex;
641
642 return 0;
643}
644EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
645
3f1ac7a7
DD
646/* Query device for its ethtool_cmd settings.
647 *
9b300495
MK
648 * Backward compatibility note: for compatibility with legacy ethtool, this is
649 * now implemented via get_link_ksettings. When driver reports higher link mode
650 * bits, a kernel warning is logged once (with name of 1st driver/device) to
651 * recommend user to upgrade ethtool, but the command is successful (only the
652 * lower link mode bits reported back to user). Deprecated fields from
653 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
3f1ac7a7 654 */
4bc71cb9
JP
655static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
656{
9b300495 657 struct ethtool_link_ksettings link_ksettings;
4bc71cb9 658 struct ethtool_cmd cmd;
9b300495 659 int err;
4bc71cb9 660
3f1ac7a7 661 ASSERT_RTNL();
9b300495
MK
662 if (!dev->ethtool_ops->get_link_ksettings)
663 return -EOPNOTSUPP;
3f1ac7a7 664
caa93b7c 665 if (dev->ethtool->module_fw_flash_in_progress)
31e0aa99
DR
666 return -EBUSY;
667
9b300495
MK
668 memset(&link_ksettings, 0, sizeof(link_ksettings));
669 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
670 if (err < 0)
671 return err;
672 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
3237fc63 673
9b300495
MK
674 /* send a sensible cmd tag back to user */
675 cmd.cmd = ETHTOOL_GSET;
1da177e4
LT
676
677 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
678 return -EFAULT;
3f1ac7a7 679
1da177e4
LT
680 return 0;
681}
682
3f1ac7a7
DD
683/* Update device link settings with given ethtool_cmd.
684 *
9b300495
MK
685 * Backward compatibility note: for compatibility with legacy ethtool, this is
686 * now always implemented via set_link_settings. When user's request updates
687 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
688 * warning is logged once (with name of 1st driver/device) to recommend user to
689 * upgrade ethtool, and the request is rejected.
3f1ac7a7 690 */
1da177e4
LT
691static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
692{
9b300495 693 struct ethtool_link_ksettings link_ksettings;
1da177e4 694 struct ethtool_cmd cmd;
73286734 695 int ret;
1da177e4 696
3f1ac7a7 697 ASSERT_RTNL();
1da177e4
LT
698
699 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
700 return -EFAULT;
9b300495 701 if (!dev->ethtool_ops->set_link_ksettings)
3f1ac7a7
DD
702 return -EOPNOTSUPP;
703
9b300495
MK
704 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
705 return -EINVAL;
706 link_ksettings.base.link_mode_masks_nwords =
707 __ETHTOOL_LINK_MODE_MASK_NU32;
73286734 708 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
1b1b1847 709 if (ret >= 0) {
73286734 710 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
1b1b1847
MK
711 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
712 }
73286734 713 return ret;
1da177e4
LT
714}
715
095cfcfe
JK
716static int
717ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
1da177e4 718{
76fd8593 719 const struct ethtool_ops *ops = dev->ethtool_ops;
edaf5df2 720 struct device *parent = dev->dev.parent;
1da177e4 721
095cfcfe 722 rsp->info.cmd = ETHTOOL_GDRVINFO;
d2efeb52
JH
723 strscpy(rsp->info.version, init_uts_ns.name.release,
724 sizeof(rsp->info.version));
c03a14e8 725 if (ops->get_drvinfo) {
095cfcfe 726 ops->get_drvinfo(dev, &rsp->info);
edaf5df2
VM
727 if (!rsp->info.bus_info[0] && parent)
728 strscpy(rsp->info.bus_info, dev_name(parent),
729 sizeof(rsp->info.bus_info));
730 if (!rsp->info.driver[0] && parent && parent->driver)
731 strscpy(rsp->info.driver, parent->driver->name,
732 sizeof(rsp->info.driver));
733 } else if (parent && parent->driver) {
734 strscpy(rsp->info.bus_info, dev_name(parent),
095cfcfe 735 sizeof(rsp->info.bus_info));
edaf5df2 736 strscpy(rsp->info.driver, parent->driver->name,
095cfcfe 737 sizeof(rsp->info.driver));
bde3b0fd 738 } else if (dev->rtnl_link_ops) {
a71af890 739 strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
bde3b0fd 740 sizeof(rsp->info.driver));
01414802
BH
741 } else {
742 return -EOPNOTSUPP;
743 }
1da177e4 744
723b2f57
JG
745 /*
746 * this method of obtaining string set info is deprecated;
d17792eb 747 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 748 */
c03a14e8 749 if (ops->get_sset_count) {
ff03d49f
JG
750 int rc;
751
752 rc = ops->get_sset_count(dev, ETH_SS_TEST);
753 if (rc >= 0)
095cfcfe 754 rsp->info.testinfo_len = rc;
ff03d49f
JG
755 rc = ops->get_sset_count(dev, ETH_SS_STATS);
756 if (rc >= 0)
095cfcfe 757 rsp->info.n_stats = rc;
339bf024
JG
758 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
759 if (rc >= 0)
095cfcfe 760 rsp->info.n_priv_flags = rc;
ff03d49f 761 }
f9fc54d3
YL
762 if (ops->get_regs_len) {
763 int ret = ops->get_regs_len(dev);
764
765 if (ret > 0)
095cfcfe 766 rsp->info.regdump_len = ret;
f9fc54d3
YL
767 }
768
c03a14e8 769 if (ops->get_eeprom_len)
095cfcfe 770 rsp->info.eedump_len = ops->get_eeprom_len(dev);
ddb6e99e 771
095cfcfe 772 if (!rsp->info.fw_version[0])
1af0a094
JK
773 rsp->devlink = netdev_to_devlink_get(dev);
774
1da177e4
LT
775 return 0;
776}
777
f5c445ed 778static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 779 void __user *useraddr)
723b2f57
JG
780{
781 struct ethtool_sset_info info;
723b2f57
JG
782 u64 sset_mask;
783 int i, idx = 0, n_bits = 0, ret, rc;
784 u32 *info_buf = NULL;
785
723b2f57
JG
786 if (copy_from_user(&info, useraddr, sizeof(info)))
787 return -EFAULT;
788
789 /* store copy of mask, because we zero struct later on */
790 sset_mask = info.sset_mask;
791 if (!sset_mask)
792 return 0;
793
794 /* calculate size of return buffer */
d17792eb 795 n_bits = hweight64(sset_mask);
723b2f57
JG
796
797 memset(&info, 0, sizeof(info));
798 info.cmd = ETHTOOL_GSSET_INFO;
799
6396bb22 800 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
723b2f57
JG
801 if (!info_buf)
802 return -ENOMEM;
803
804 /*
805 * fill return buffer based on input bitmask and successful
806 * get_sset_count return
807 */
808 for (i = 0; i < 64; i++) {
809 if (!(sset_mask & (1ULL << i)))
810 continue;
811
340ae165 812 rc = __ethtool_get_sset_count(dev, i);
723b2f57
JG
813 if (rc >= 0) {
814 info.sset_mask |= (1ULL << i);
815 info_buf[idx++] = rc;
816 }
817 }
818
819 ret = -EFAULT;
820 if (copy_to_user(useraddr, &info, sizeof(info)))
821 goto out;
822
823 useraddr += offsetof(struct ethtool_sset_info, data);
ed717613 824 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
723b2f57
JG
825 goto out;
826
827 ret = 0;
828
829out:
830 kfree(info_buf);
831 return ret;
832}
833
dd98d289
AB
834static noinline_for_stack int
835ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
836 const struct compat_ethtool_rxnfc __user *useraddr,
837 size_t size)
838{
839 struct compat_ethtool_rxnfc crxnfc = {};
840
841 /* We expect there to be holes between fs.m_ext and
842 * fs.ring_cookie and at the end of fs, but nowhere else.
843 * On non-x86, no conversion should be needed.
844 */
845 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
846 sizeof(struct compat_ethtool_rxnfc) !=
847 sizeof(struct ethtool_rxnfc));
848 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
849 sizeof(useraddr->fs.m_ext) !=
850 offsetof(struct ethtool_rxnfc, fs.m_ext) +
851 sizeof(rxnfc->fs.m_ext));
852 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
853 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
854 offsetof(struct ethtool_rxnfc, fs.location) -
855 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
856
857 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
858 return -EFAULT;
859
860 *rxnfc = (struct ethtool_rxnfc) {
861 .cmd = crxnfc.cmd,
862 .flow_type = crxnfc.flow_type,
863 .data = crxnfc.data,
864 .fs = {
865 .flow_type = crxnfc.fs.flow_type,
866 .h_u = crxnfc.fs.h_u,
867 .h_ext = crxnfc.fs.h_ext,
868 .m_u = crxnfc.fs.m_u,
869 .m_ext = crxnfc.fs.m_ext,
870 .ring_cookie = crxnfc.fs.ring_cookie,
871 .location = crxnfc.fs.location,
872 },
873 .rule_cnt = crxnfc.rule_cnt,
874 };
875
876 return 0;
877}
878
879static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
880 const void __user *useraddr,
881 size_t size)
882{
883 if (compat_need_64bit_alignment_fixup())
884 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
885
886 if (copy_from_user(rxnfc, useraddr, size))
887 return -EFAULT;
888
889 return 0;
890}
891
892static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
893 const struct ethtool_rxnfc *rxnfc,
894 size_t size, const u32 *rule_buf)
895{
896 struct compat_ethtool_rxnfc crxnfc;
897
898 memset(&crxnfc, 0, sizeof(crxnfc));
899 crxnfc = (struct compat_ethtool_rxnfc) {
900 .cmd = rxnfc->cmd,
901 .flow_type = rxnfc->flow_type,
902 .data = rxnfc->data,
903 .fs = {
904 .flow_type = rxnfc->fs.flow_type,
905 .h_u = rxnfc->fs.h_u,
906 .h_ext = rxnfc->fs.h_ext,
907 .m_u = rxnfc->fs.m_u,
908 .m_ext = rxnfc->fs.m_ext,
909 .ring_cookie = rxnfc->fs.ring_cookie,
910 .location = rxnfc->fs.location,
911 },
912 .rule_cnt = rxnfc->rule_cnt,
913 };
914
915 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
916 return -EFAULT;
917
918 return 0;
919}
920
801b27e8
JD
921static int ethtool_rxnfc_copy_struct(u32 cmd, struct ethtool_rxnfc *info,
922 size_t *info_size, void __user *useraddr)
923{
924 /* struct ethtool_rxnfc was originally defined for
925 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
926 * members. User-space might still be using that
927 * definition.
928 */
929 if (cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH)
930 *info_size = (offsetof(struct ethtool_rxnfc, data) +
931 sizeof(info->data));
932
933 if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
934 return -EFAULT;
935
936 if ((cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH) && info->flow_type & FLOW_RSS) {
937 *info_size = sizeof(*info);
938 if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
939 return -EFAULT;
940 /* Since malicious users may modify the original data,
941 * we need to check whether FLOW_RSS is still requested.
942 */
943 if (!(info->flow_type & FLOW_RSS))
944 return -EINVAL;
945 }
946
947 if (info->cmd != cmd)
948 return -EINVAL;
949
950 return 0;
951}
952
dd98d289
AB
953static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
954 const struct ethtool_rxnfc *rxnfc,
955 size_t size, const u32 *rule_buf)
956{
957 int ret;
958
959 if (compat_need_64bit_alignment_fixup()) {
960 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
961 rule_buf);
962 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
963 } else {
9b29a161 964 ret = copy_to_user(useraddr, rxnfc, size);
dd98d289
AB
965 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
966 }
967
968 if (ret)
969 return -EFAULT;
970
971 if (rule_buf) {
972 if (copy_to_user(useraddr, rule_buf,
973 rxnfc->rule_cnt * sizeof(u32)))
974 return -EFAULT;
975 }
976
977 return 0;
978}
979
97f8aefb 980static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
bf988435 981 u32 cmd, void __user *useraddr)
0853ad66 982{
13e59344 983 const struct ethtool_ops *ops = dev->ethtool_ops;
bf988435
BH
984 struct ethtool_rxnfc info;
985 size_t info_size = sizeof(info);
55664f32 986 int rc;
0853ad66 987
501869fe 988 if (!ops->set_rxnfc)
0853ad66
SB
989 return -EOPNOTSUPP;
990
801b27e8
JD
991 rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
992 if (rc)
993 return rc;
0853ad66 994
501869fe
GE
995 if (ops->get_rxfh) {
996 struct ethtool_rxfh_param rxfh = {};
13e59344 997
501869fe
GE
998 rc = ops->get_rxfh(dev, &rxfh);
999 if (rc)
1000 return rc;
1001
1002 /* Sanity check: if symmetric-xor is set, then:
1003 * 1 - no other fields besides IP src/dst and/or L4 src/dst
1004 * 2 - If src is set, dst must also be set
1005 */
1006 if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1007 ((info.data & ~(RXH_IP_SRC | RXH_IP_DST |
1008 RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
1009 (!!(info.data & RXH_IP_SRC) ^ !!(info.data & RXH_IP_DST)) ||
1010 (!!(info.data & RXH_L4_B_0_1) ^ !!(info.data & RXH_L4_B_2_3))))
1011 return -EINVAL;
1012 }
13e59344
AZ
1013
1014 rc = ops->set_rxnfc(dev, &info);
55664f32
BH
1015 if (rc)
1016 return rc;
1017
1018 if (cmd == ETHTOOL_SRXCLSRLINS &&
dd98d289 1019 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
55664f32
BH
1020 return -EFAULT;
1021
1022 return 0;
0853ad66
SB
1023}
1024
97f8aefb 1025static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
bf988435 1026 u32 cmd, void __user *useraddr)
0853ad66
SB
1027{
1028 struct ethtool_rxnfc info;
bf988435 1029 size_t info_size = sizeof(info);
59089d8d
SB
1030 const struct ethtool_ops *ops = dev->ethtool_ops;
1031 int ret;
1032 void *rule_buf = NULL;
0853ad66 1033
59089d8d 1034 if (!ops->get_rxnfc)
0853ad66
SB
1035 return -EOPNOTSUPP;
1036
801b27e8
JD
1037 ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1038 if (ret)
1039 return ret;
2bb3207d 1040
59089d8d
SB
1041 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1042 if (info.rule_cnt > 0) {
db048b69 1043 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
6396bb22 1044 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
db048b69 1045 GFP_USER);
59089d8d
SB
1046 if (!rule_buf)
1047 return -ENOMEM;
1048 }
1049 }
0853ad66 1050
59089d8d
SB
1051 ret = ops->get_rxnfc(dev, &info, rule_buf);
1052 if (ret < 0)
1053 goto err_out;
1054
dd98d289 1055 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
59089d8d 1056err_out:
c9caceca 1057 kfree(rule_buf);
59089d8d
SB
1058
1059 return ret;
0853ad66
SB
1060}
1061
3de0b592
VD
1062static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1063 struct ethtool_rxnfc *rx_rings,
1064 u32 size)
1065{
fb95cd8d 1066 int i;
3de0b592 1067
ed717613 1068 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
fb95cd8d 1069 return -EFAULT;
3de0b592
VD
1070
1071 /* Validate ring indices */
fb95cd8d
BH
1072 for (i = 0; i < size; i++)
1073 if (indir[i] >= rx_rings->data)
1074 return -EINVAL;
1075
1076 return 0;
3de0b592
VD
1077}
1078
ba905f5e 1079u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
960fb622
ED
1080
1081void netdev_rss_key_fill(void *buffer, size_t len)
1082{
1083 BUG_ON(len > sizeof(netdev_rss_key));
1084 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1085 memcpy(buffer, netdev_rss_key, len);
1086}
1087EXPORT_SYMBOL(netdev_rss_key_fill);
1088
a5b6ee29
BH
1089static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1090 void __user *useraddr)
1091{
fb6e30a7
AZ
1092 struct ethtool_rxfh_param rxfh = {};
1093 u32 user_size;
a5b6ee29
BH
1094 int ret;
1095
7850f63f 1096 if (!dev->ethtool_ops->get_rxfh_indir_size ||
fe62d001 1097 !dev->ethtool_ops->get_rxfh)
7850f63f 1098 return -EOPNOTSUPP;
fb6e30a7
AZ
1099 rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1100 if (rxfh.indir_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
7850f63f 1108 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
fb6e30a7 1109 &rxfh.indir_size, sizeof(rxfh.indir_size)))
7850f63f
BH
1110 return -EFAULT;
1111
1112 /* If the user buffer size is 0, this is just a query for the
1113 * device table size. Otherwise, if it's smaller than the
1114 * device table size it's an error.
1115 */
fb6e30a7 1116 if (user_size < rxfh.indir_size)
7850f63f
BH
1117 return user_size == 0 ? 0 : -EINVAL;
1118
fb6e30a7
AZ
1119 rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
1120 if (!rxfh.indir)
a5b6ee29
BH
1121 return -ENOMEM;
1122
fb6e30a7 1123 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
a5b6ee29
BH
1124 if (ret)
1125 goto out;
7850f63f
BH
1126 if (copy_to_user(useraddr +
1127 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
fb6e30a7 1128 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
a5b6ee29
BH
1129 ret = -EFAULT;
1130
1131out:
fb6e30a7 1132 kfree(rxfh.indir);
a5b6ee29
BH
1133 return ret;
1134}
1135
1136static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1137 void __user *useraddr)
1138{
c03a14e8 1139 const struct ethtool_ops *ops = dev->ethtool_ops;
fb6e30a7
AZ
1140 struct ethtool_rxfh_param rxfh_dev = {};
1141 struct netlink_ext_ack *extack = NULL;
1142 struct ethtool_rxnfc rx_rings;
1143 u32 user_size, i;
a5b6ee29 1144 int ret;
3de0b592 1145 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
a5b6ee29 1146
fe62d001 1147 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
c03a14e8 1148 !ops->get_rxnfc)
7850f63f 1149 return -EOPNOTSUPP;
c03a14e8 1150
fb6e30a7
AZ
1151 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1152 if (rxfh_dev.indir_size == 0)
a5b6ee29
BH
1153 return -EOPNOTSUPP;
1154
7850f63f 1155 if (copy_from_user(&user_size,
a5b6ee29 1156 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 1157 sizeof(user_size)))
a5b6ee29
BH
1158 return -EFAULT;
1159
fb6e30a7 1160 if (user_size != 0 && user_size != rxfh_dev.indir_size)
7850f63f
BH
1161 return -EINVAL;
1162
fb6e30a7
AZ
1163 rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
1164 sizeof(rxfh_dev.indir[0]), GFP_USER);
1165 if (!rxfh_dev.indir)
a5b6ee29
BH
1166 return -ENOMEM;
1167
7850f63f 1168 rx_rings.cmd = ETHTOOL_GRXRINGS;
c03a14e8 1169 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
7850f63f
BH
1170 if (ret)
1171 goto out;
278bc429
BH
1172
1173 if (user_size == 0) {
fb6e30a7
AZ
1174 u32 *indir = rxfh_dev.indir;
1175
1176 for (i = 0; i < rxfh_dev.indir_size; i++)
278bc429
BH
1177 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1178 } else {
fb6e30a7 1179 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
3de0b592
VD
1180 useraddr + ringidx_offset,
1181 &rx_rings,
fb6e30a7 1182 rxfh_dev.indir_size);
3de0b592
VD
1183 if (ret)
1184 goto out;
1185 }
1186
fb6e30a7
AZ
1187 rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
1188 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
d4ab4286
KJ
1189 if (ret)
1190 goto out;
1191
1192 /* indicate whether rxfh was set to default */
1193 if (user_size == 0)
1194 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1195 else
1196 dev->priv_flags |= IFF_RXFH_CONFIGURED;
3de0b592
VD
1197
1198out:
fb6e30a7 1199 kfree(rxfh_dev.indir);
3de0b592
VD
1200 return ret;
1201}
1202
1203static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1204 void __user *useraddr)
1205{
3de0b592 1206 const struct ethtool_ops *ops = dev->ethtool_ops;
fb6e30a7 1207 struct ethtool_rxfh_param rxfh_dev = {};
f062a384 1208 u32 user_indir_size, user_key_size;
7964e788 1209 struct ethtool_rxfh_context *ctx;
f062a384 1210 struct ethtool_rxfh rxfh;
f062a384 1211 u32 indir_bytes;
3de0b592 1212 u8 *rss_config;
fb6e30a7
AZ
1213 u32 total_size;
1214 int ret;
3de0b592 1215
892311f6 1216 if (!ops->get_rxfh)
3de0b592
VD
1217 return -EOPNOTSUPP;
1218
1219 if (ops->get_rxfh_indir_size)
fb6e30a7 1220 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
3de0b592 1221 if (ops->get_rxfh_key_size)
fb6e30a7 1222 rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
3de0b592 1223
f062a384 1224 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592 1225 return -EFAULT;
f062a384
BH
1226 user_indir_size = rxfh.indir_size;
1227 user_key_size = rxfh.key_size;
3de0b592 1228
f062a384 1229 /* Check that reserved fields are 0 for now */
13e59344 1230 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
f062a384 1231 return -EINVAL;
84a1d9c4 1232 /* Most drivers don't handle rss_context, check it's 0 as well */
ce056504
JK
1233 if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
1234 ops->create_rxfh_context))
84a1d9c4 1235 return -EOPNOTSUPP;
f062a384 1236
fb6e30a7
AZ
1237 rxfh.indir_size = rxfh_dev.indir_size;
1238 rxfh.key_size = rxfh_dev.key_size;
f062a384 1239 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
3de0b592
VD
1240 return -EFAULT;
1241
fb6e30a7
AZ
1242 if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
1243 (user_key_size && user_key_size != rxfh_dev.key_size))
3de0b592
VD
1244 return -EINVAL;
1245
fb6e30a7 1246 indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
3de0b592
VD
1247 total_size = indir_bytes + user_key_size;
1248 rss_config = kzalloc(total_size, GFP_USER);
1249 if (!rss_config)
1250 return -ENOMEM;
1251
1252 if (user_indir_size)
fb6e30a7 1253 rxfh_dev.indir = (u32 *)rss_config;
3de0b592
VD
1254
1255 if (user_key_size)
fb6e30a7 1256 rxfh_dev.key = rss_config + indir_bytes;
3de0b592 1257
7964e788
EC
1258 if (rxfh.rss_context) {
1259 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
1260 if (!ctx) {
1261 ret = -ENOENT;
1262 goto out;
1263 }
1264 if (rxfh_dev.indir)
1265 memcpy(rxfh_dev.indir, ethtool_rxfh_context_indir(ctx),
1266 indir_bytes);
ec6e57be
JK
1267 if (!ops->rxfh_per_ctx_key) {
1268 rxfh_dev.key_size = 0;
1269 } else {
1270 if (rxfh_dev.key)
1271 memcpy(rxfh_dev.key,
1272 ethtool_rxfh_context_key(ctx),
1273 user_key_size);
1274 rxfh_dev.hfunc = ctx->hfunc;
1275 }
7964e788
EC
1276 rxfh_dev.input_xfrm = ctx->input_xfrm;
1277 ret = 0;
1278 } else {
1279 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
1280 if (ret)
1281 goto out;
1282 }
3de0b592 1283
892311f6 1284 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
fb6e30a7 1285 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
892311f6 1286 ret = -EFAULT;
7c402f77
AZ
1287 } else if (copy_to_user(useraddr +
1288 offsetof(struct ethtool_rxfh, input_xfrm),
1289 &rxfh_dev.input_xfrm,
1290 sizeof(rxfh.input_xfrm))) {
1291 ret = -EFAULT;
ec6e57be
JK
1292 } else if (copy_to_user(useraddr +
1293 offsetof(struct ethtool_rxfh, key_size),
1294 &rxfh_dev.key_size,
1295 sizeof(rxfh.key_size))) {
1296 ret = -EFAULT;
892311f6
EP
1297 } else if (copy_to_user(useraddr +
1298 offsetof(struct ethtool_rxfh, rss_config[0]),
1299 rss_config, total_size)) {
1300 ret = -EFAULT;
1301 }
1302out:
3de0b592
VD
1303 kfree(rss_config);
1304
1305 return ret;
1306}
1307
28c8757a
JK
1308static struct ethtool_rxfh_context *
1309ethtool_rxfh_ctx_alloc(const struct ethtool_ops *ops,
1310 u32 indir_size, u32 key_size)
1311{
1312 size_t indir_bytes, flex_len, key_off, size;
1313 struct ethtool_rxfh_context *ctx;
1314 u32 priv_bytes, indir_max;
1315 u16 key_max;
1316
1317 key_max = max(key_size, ops->rxfh_key_space);
1318 indir_max = max(indir_size, ops->rxfh_indir_space);
1319
1320 priv_bytes = ALIGN(ops->rxfh_priv_size, sizeof(u32));
1321 indir_bytes = array_size(indir_max, sizeof(u32));
1322
1323 key_off = size_add(priv_bytes, indir_bytes);
1324 flex_len = size_add(key_off, key_max);
1325 size = struct_size_t(struct ethtool_rxfh_context, data, flex_len);
1326
1327 ctx = kzalloc(size, GFP_KERNEL_ACCOUNT);
1328 if (!ctx)
1329 return NULL;
1330
1331 ctx->indir_size = indir_size;
1332 ctx->key_size = key_size;
1333 ctx->key_off = key_off;
1334 ctx->priv_size = ops->rxfh_priv_size;
1335
1336 ctx->hfunc = ETH_RSS_HASH_NO_CHANGE;
1337 ctx->input_xfrm = RXH_XFRM_NO_CHANGE;
1338
1339 return ctx;
1340}
1341
3de0b592
VD
1342static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1343 void __user *useraddr)
1344{
fb6e30a7 1345 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
3de0b592 1346 const struct ethtool_ops *ops = dev->ethtool_ops;
fb6e30a7 1347 u32 dev_indir_size = 0, dev_key_size = 0, i;
7195f0ef 1348 u32 user_indir_len = 0, indir_bytes = 0;
fb6e30a7 1349 struct ethtool_rxfh_param rxfh_dev = {};
eac9122f 1350 struct ethtool_rxfh_context *ctx = NULL;
fb6e30a7 1351 struct netlink_ext_ack *extack = NULL;
3de0b592 1352 struct ethtool_rxnfc rx_rings;
f062a384 1353 struct ethtool_rxfh rxfh;
87925151 1354 bool locked = false; /* dev->ethtool->rss_lock taken */
eac9122f 1355 bool create = false;
fb6e30a7
AZ
1356 u8 *rss_config;
1357 int ret;
3de0b592 1358
892311f6 1359 if (!ops->get_rxnfc || !ops->set_rxfh)
3de0b592
VD
1360 return -EOPNOTSUPP;
1361
1362 if (ops->get_rxfh_indir_size)
1363 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592 1364 if (ops->get_rxfh_key_size)
d340c862 1365 dev_key_size = ops->get_rxfh_key_size(dev);
3de0b592 1366
f062a384 1367 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592
VD
1368 return -EFAULT;
1369
f062a384 1370 /* Check that reserved fields are 0 for now */
13e59344 1371 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
f062a384 1372 return -EINVAL;
84a1d9c4 1373 /* Most drivers don't handle rss_context, check it's 0 as well */
ce056504
JK
1374 if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
1375 ops->create_rxfh_context))
84a1d9c4 1376 return -EOPNOTSUPP;
13e59344 1377 /* Check input data transformation capabilities */
948f97f9
AZ
1378 if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
1379 rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
1380 return -EINVAL;
503757c8
SM
1381 if (rxfh.input_xfrm != RXH_XFRM_NO_CHANGE &&
1382 (rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
13e59344
AZ
1383 !ops->cap_rss_sym_xor_supported)
1384 return -EOPNOTSUPP;
eac9122f 1385 create = rxfh.rss_context == ETH_RXFH_CONTEXT_ALLOC;
f062a384 1386
f062a384
BH
1387 if ((rxfh.indir_size &&
1388 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1389 rxfh.indir_size != dev_indir_size) ||
4d7c3c1a
GP
1390 (rxfh.key_size && rxfh.key_size != dev_key_size))
1391 return -EINVAL;
1392
1393 /* Must request at least one change: indir size, hash key, function
1394 * or input transformation.
1395 * There's no need for any of it in case of context creation.
1396 */
1397 if (!create &&
f062a384 1398 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
0dd415d1
AZ
1399 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
1400 rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
3de0b592
VD
1401 return -EINVAL;
1402
dc975537 1403 indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
3de0b592 1404
ec6e57be
JK
1405 /* Check settings which may be global rather than per RSS-context */
1406 if (rxfh.rss_context && !ops->rxfh_per_ctx_key)
1407 if (rxfh.key_size ||
1408 (rxfh.hfunc && rxfh.hfunc != ETH_RSS_HASH_NO_CHANGE) ||
1409 (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_NO_CHANGE))
1410 return -EOPNOTSUPP;
1411
dc975537 1412 rss_config = kzalloc(indir_bytes + dev_key_size, GFP_USER);
3de0b592
VD
1413 if (!rss_config)
1414 return -ENOMEM;
1415
1416 rx_rings.cmd = ETHTOOL_GRXRINGS;
1417 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1418 if (ret)
1419 goto out;
1420
84a1d9c4
EC
1421 /* rxfh.indir_size == 0 means reset the indir table to default (master
1422 * context) or delete the context (other RSS contexts).
f062a384 1423 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
3de0b592 1424 */
f062a384
BH
1425 if (rxfh.indir_size &&
1426 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
7195f0ef 1427 user_indir_len = indir_bytes;
fb6e30a7
AZ
1428 rxfh_dev.indir = (u32 *)rss_config;
1429 rxfh_dev.indir_size = dev_indir_size;
1430 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
3de0b592
VD
1431 useraddr + rss_cfg_offset,
1432 &rx_rings,
f062a384 1433 rxfh.indir_size);
3de0b592 1434 if (ret)
7850f63f 1435 goto out;
f062a384 1436 } else if (rxfh.indir_size == 0) {
84a1d9c4 1437 if (rxfh.rss_context == 0) {
fb6e30a7
AZ
1438 u32 *indir;
1439
1440 rxfh_dev.indir = (u32 *)rss_config;
1441 rxfh_dev.indir_size = dev_indir_size;
1442 indir = rxfh_dev.indir;
84a1d9c4
EC
1443 for (i = 0; i < dev_indir_size; i++)
1444 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1445 } else {
dcd8dbf9 1446 rxfh_dev.rss_delete = true;
84a1d9c4 1447 }
3de0b592 1448 }
278bc429 1449
f062a384 1450 if (rxfh.key_size) {
fb6e30a7
AZ
1451 rxfh_dev.key_size = dev_key_size;
1452 rxfh_dev.key = rss_config + indir_bytes;
1453 if (copy_from_user(rxfh_dev.key,
7195f0ef 1454 useraddr + rss_cfg_offset + user_indir_len,
f062a384 1455 rxfh.key_size)) {
3de0b592
VD
1456 ret = -EFAULT;
1457 goto out;
278bc429 1458 }
7850f63f
BH
1459 }
1460
87925151
EC
1461 if (rxfh.rss_context) {
1462 mutex_lock(&dev->ethtool->rss_lock);
1463 locked = true;
1464 }
eac9122f
EC
1465 if (create) {
1466 if (rxfh_dev.rss_delete) {
1467 ret = -EINVAL;
1468 goto out;
1469 }
28c8757a 1470 ctx = ethtool_rxfh_ctx_alloc(ops, dev_indir_size, dev_key_size);
eac9122f
EC
1471 if (!ctx) {
1472 ret = -ENOMEM;
1473 goto out;
1474 }
28c8757a 1475
847a8ab1 1476 if (ops->create_rxfh_context) {
b54de559 1477 u32 limit = ops->rxfh_max_num_contexts ?: U32_MAX;
847a8ab1
EC
1478 u32 ctx_id;
1479
1480 /* driver uses new API, core allocates ID */
1481 ret = xa_alloc(&dev->ethtool->rss_ctx, &ctx_id, ctx,
b54de559
EC
1482 XA_LIMIT(1, limit - 1),
1483 GFP_KERNEL_ACCOUNT);
847a8ab1
EC
1484 if (ret < 0) {
1485 kfree(ctx);
1486 goto out;
1487 }
1488 WARN_ON(!ctx_id); /* can't happen */
1489 rxfh.rss_context = ctx_id;
1490 }
eac9122f
EC
1491 } else if (rxfh.rss_context) {
1492 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
1493 if (!ctx) {
1494 ret = -ENOENT;
1495 goto out;
1496 }
1497 }
fb6e30a7 1498 rxfh_dev.hfunc = rxfh.hfunc;
dcd8dbf9 1499 rxfh_dev.rss_context = rxfh.rss_context;
13e59344 1500 rxfh_dev.input_xfrm = rxfh.input_xfrm;
fb6e30a7 1501
847a8ab1 1502 if (rxfh.rss_context && ops->create_rxfh_context) {
dc975537 1503 if (create) {
30a32cdf
EC
1504 ret = ops->create_rxfh_context(dev, ctx, &rxfh_dev,
1505 extack);
dc975537
JK
1506 /* Make sure driver populates defaults */
1507 WARN_ON_ONCE(!ret && !rxfh_dev.key &&
1508 !memchr_inv(ethtool_rxfh_context_key(ctx),
1509 0, ctx->key_size));
1510 } else if (rxfh_dev.rss_delete) {
847a8ab1 1511 ret = ops->remove_rxfh_context(dev, ctx,
30a32cdf
EC
1512 rxfh.rss_context,
1513 extack);
dc975537 1514 } else {
30a32cdf
EC
1515 ret = ops->modify_rxfh_context(dev, ctx, &rxfh_dev,
1516 extack);
dc975537 1517 }
847a8ab1
EC
1518 } else {
1519 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1520 }
1521 if (ret) {
1522 if (create) {
eac9122f 1523 /* failed to create, free our new tracking entry */
847a8ab1
EC
1524 if (ops->create_rxfh_context)
1525 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
eac9122f 1526 kfree(ctx);
847a8ab1 1527 }
d4ab4286 1528 goto out;
eac9122f 1529 }
d4ab4286 1530
84a1d9c4 1531 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
dcd8dbf9 1532 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
84a1d9c4
EC
1533 ret = -EFAULT;
1534
dcd8dbf9 1535 if (!rxfh_dev.rss_context) {
84a1d9c4
EC
1536 /* indicate whether rxfh was set to default */
1537 if (rxfh.indir_size == 0)
1538 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1539 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1540 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1541 }
eac9122f 1542 /* Update rss_ctx tracking */
847a8ab1
EC
1543 if (create && !ops->create_rxfh_context) {
1544 /* driver uses old API, it chose context ID */
1a16cdf7 1545 if (WARN_ON(xa_load(&dev->ethtool->rss_ctx, rxfh_dev.rss_context))) {
eac9122f
EC
1546 /* context ID reused, our tracking is screwed */
1547 kfree(ctx);
1548 goto out;
1549 }
1550 /* Allocate the exact ID the driver gave us */
1a16cdf7 1551 if (xa_is_err(xa_store(&dev->ethtool->rss_ctx, rxfh_dev.rss_context,
eac9122f
EC
1552 ctx, GFP_KERNEL))) {
1553 kfree(ctx);
1554 goto out;
1555 }
dc975537
JK
1556
1557 /* Fetch the defaults for the old API, in the new API drivers
1558 * should write defaults into ctx themselves.
1559 */
1560 rxfh_dev.indir = (u32 *)rss_config;
1561 rxfh_dev.indir_size = dev_indir_size;
1562
1563 rxfh_dev.key = rss_config + indir_bytes;
1564 rxfh_dev.key_size = dev_key_size;
1565
1566 ret = ops->get_rxfh(dev, &rxfh_dev);
1567 if (WARN_ON(ret)) {
1568 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
1569 kfree(ctx);
1570 goto out;
1571 }
eac9122f
EC
1572 }
1573 if (rxfh_dev.rss_delete) {
1574 WARN_ON(xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context) != ctx);
1575 kfree(ctx);
1576 } else if (ctx) {
1577 if (rxfh_dev.indir) {
1578 for (i = 0; i < dev_indir_size; i++)
1579 ethtool_rxfh_context_indir(ctx)[i] = rxfh_dev.indir[i];
dc975537
JK
1580 ctx->indir_configured =
1581 rxfh.indir_size &&
1582 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE;
eac9122f
EC
1583 }
1584 if (rxfh_dev.key) {
1585 memcpy(ethtool_rxfh_context_key(ctx), rxfh_dev.key,
1586 dev_key_size);
dc975537 1587 ctx->key_configured = !!rxfh.key_size;
eac9122f
EC
1588 }
1589 if (rxfh_dev.hfunc != ETH_RSS_HASH_NO_CHANGE)
1590 ctx->hfunc = rxfh_dev.hfunc;
1591 if (rxfh_dev.input_xfrm != RXH_XFRM_NO_CHANGE)
1592 ctx->input_xfrm = rxfh_dev.input_xfrm;
1593 }
a5b6ee29
BH
1594
1595out:
87925151
EC
1596 if (locked)
1597 mutex_unlock(&dev->ethtool->rss_lock);
3de0b592 1598 kfree(rss_config);
a5b6ee29
BH
1599 return ret;
1600}
1601
1da177e4
LT
1602static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1603{
1604 struct ethtool_regs regs;
76fd8593 1605 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
1606 void *regbuf;
1607 int reglen, ret;
1608
1609 if (!ops->get_regs || !ops->get_regs_len)
1610 return -EOPNOTSUPP;
1611
1612 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1613 return -EFAULT;
1614
1615 reglen = ops->get_regs_len(dev);
f9fc54d3
YL
1616 if (reglen <= 0)
1617 return reglen;
1618
1da177e4
LT
1619 if (regs.len > reglen)
1620 regs.len = reglen;
1621
ef76c77a
DC
1622 regbuf = vzalloc(reglen);
1623 if (!regbuf)
1624 return -ENOMEM;
1da177e4 1625
0ee4e769
VD
1626 if (regs.len < reglen)
1627 reglen = regs.len;
1628
1da177e4
LT
1629 ops->get_regs(dev, &regs, regbuf);
1630
1631 ret = -EFAULT;
1632 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1633 goto out;
1634 useraddr += offsetof(struct ethtool_regs, data);
0ee4e769 1635 if (copy_to_user(useraddr, regbuf, reglen))
1da177e4
LT
1636 goto out;
1637 ret = 0;
1638
1639 out:
a77f5db3 1640 vfree(regbuf);
1da177e4
LT
1641 return ret;
1642}
1643
d73d3a8c
BH
1644static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1645{
1646 struct ethtool_value reset;
1647 int ret;
1648
1649 if (!dev->ethtool_ops->reset)
1650 return -EOPNOTSUPP;
1651
caa93b7c 1652 if (dev->ethtool->module_fw_flash_in_progress)
31e0aa99
DR
1653 return -EBUSY;
1654
d73d3a8c
BH
1655 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1656 return -EFAULT;
1657
1658 ret = dev->ethtool_ops->reset(dev, &reset.data);
1659 if (ret)
1660 return ret;
1661
1662 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1663 return -EFAULT;
1664 return 0;
1665}
1666
1da177e4
LT
1667static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1668{
5ff223e8 1669 struct ethtool_wolinfo wol;
1da177e4
LT
1670
1671 if (!dev->ethtool_ops->get_wol)
1672 return -EOPNOTSUPP;
1673
5ff223e8 1674 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1675 wol.cmd = ETHTOOL_GWOL;
1da177e4
LT
1676 dev->ethtool_ops->get_wol(dev, &wol);
1677
1678 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1679 return -EFAULT;
1680 return 0;
1681}
1682
1683static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1684{
55b24334 1685 struct ethtool_wolinfo wol, cur_wol;
61941143 1686 int ret;
1da177e4 1687
55b24334 1688 if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
1da177e4
LT
1689 return -EOPNOTSUPP;
1690
55b24334
JC
1691 memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
1692 cur_wol.cmd = ETHTOOL_GWOL;
1693 dev->ethtool_ops->get_wol(dev, &cur_wol);
1694
1da177e4
LT
1695 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1696 return -EFAULT;
1697
55b24334
JC
1698 if (wol.wolopts & ~cur_wol.supported)
1699 return -EINVAL;
1700
2bddad9e
JC
1701 if (wol.wolopts == cur_wol.wolopts &&
1702 !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
55b24334
JC
1703 return 0;
1704
61941143
HK
1705 ret = dev->ethtool_ops->set_wol(dev, &wol);
1706 if (ret)
1707 return ret;
1708
3ebbd9f6 1709 dev->ethtool->wol_enabled = !!wol.wolopts;
67bffa79 1710 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
61941143
HK
1711
1712 return 0;
1da177e4
LT
1713}
1714
0b3100bc
HK
1715static void eee_to_keee(struct ethtool_keee *keee,
1716 const struct ethtool_eee *eee)
1717{
1718 memset(keee, 0, sizeof(*keee));
1719
0b3100bc
HK
1720 keee->eee_enabled = eee->eee_enabled;
1721 keee->tx_lpi_enabled = eee->tx_lpi_enabled;
1722 keee->tx_lpi_timer = eee->tx_lpi_timer;
1f069de6 1723
1f069de6
HK
1724 ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
1725 eee->advertised);
0b3100bc
HK
1726}
1727
1728static void keee_to_eee(struct ethtool_eee *eee,
1729 const struct ethtool_keee *keee)
1730{
292fac46
AL
1731 bool overflow;
1732
0b3100bc
HK
1733 memset(eee, 0, sizeof(*eee));
1734
0b3100bc
HK
1735 eee->eee_active = keee->eee_active;
1736 eee->eee_enabled = keee->eee_enabled;
1737 eee->tx_lpi_enabled = keee->tx_lpi_enabled;
1738 eee->tx_lpi_timer = keee->tx_lpi_timer;
1f069de6 1739
292fac46
AL
1740 overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
1741 keee->supported);
1742 ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
1743 keee->advertised);
1744 ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
1745 keee->lp_advertised);
1746 if (overflow)
1747 pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
0b3100bc
HK
1748}
1749
80f12ecc
YM
1750static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1751{
0b3100bc
HK
1752 struct ethtool_keee keee;
1753 struct ethtool_eee eee;
80f12ecc
YM
1754 int rc;
1755
1756 if (!dev->ethtool_ops->get_eee)
1757 return -EOPNOTSUPP;
1758
0b3100bc
HK
1759 memset(&keee, 0, sizeof(keee));
1760 rc = dev->ethtool_ops->get_eee(dev, &keee);
80f12ecc
YM
1761 if (rc)
1762 return rc;
1763
0b3100bc
HK
1764 keee_to_eee(&eee, &keee);
1765 if (copy_to_user(useraddr, &eee, sizeof(eee)))
80f12ecc
YM
1766 return -EFAULT;
1767
1768 return 0;
1769}
1770
1771static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1772{
0b3100bc
HK
1773 struct ethtool_keee keee;
1774 struct ethtool_eee eee;
6c5bc8fe 1775 int ret;
80f12ecc
YM
1776
1777 if (!dev->ethtool_ops->set_eee)
1778 return -EOPNOTSUPP;
1779
0b3100bc 1780 if (copy_from_user(&eee, useraddr, sizeof(eee)))
80f12ecc
YM
1781 return -EFAULT;
1782
0b3100bc
HK
1783 eee_to_keee(&keee, &eee);
1784 ret = dev->ethtool_ops->set_eee(dev, &keee);
6c5bc8fe
MK
1785 if (!ret)
1786 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1787 return ret;
80f12ecc
YM
1788}
1789
1da177e4
LT
1790static int ethtool_nway_reset(struct net_device *dev)
1791{
1792 if (!dev->ethtool_ops->nway_reset)
1793 return -EOPNOTSUPP;
1794
1795 return dev->ethtool_ops->nway_reset(dev);
1796}
1797
e596e6e4
BH
1798static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1799{
1800 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
3d2b847f 1801 int link = __ethtool_get_link(dev);
e596e6e4 1802
3d2b847f
MK
1803 if (link < 0)
1804 return link;
e596e6e4 1805
3d2b847f 1806 edata.data = link;
e596e6e4
BH
1807 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1808 return -EFAULT;
1809 return 0;
1810}
1811
081d094e
BH
1812static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1813 int (*getter)(struct net_device *,
1814 struct ethtool_eeprom *, u8 *),
1815 u32 total_len)
1da177e4
LT
1816{
1817 struct ethtool_eeprom eeprom;
b131dd5d
MSB
1818 void __user *userbuf = useraddr + sizeof(eeprom);
1819 u32 bytes_remaining;
1da177e4 1820 u8 *data;
b131dd5d 1821 int ret = 0;
1da177e4 1822
1da177e4
LT
1823 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1824 return -EFAULT;
1825
1826 /* Check for wrap and zero */
1827 if (eeprom.offset + eeprom.len <= eeprom.offset)
1828 return -EINVAL;
1829
1830 /* Check for exceeding total eeprom len */
081d094e 1831 if (eeprom.offset + eeprom.len > total_len)
1da177e4
LT
1832 return -EINVAL;
1833
80ec82e3 1834 data = kzalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1835 if (!data)
1836 return -ENOMEM;
1837
b131dd5d
MSB
1838 bytes_remaining = eeprom.len;
1839 while (bytes_remaining > 0) {
1840 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1841
081d094e 1842 ret = getter(dev, &eeprom, data);
b131dd5d
MSB
1843 if (ret)
1844 break;
b9bbc4c1
HK
1845 if (!eeprom.len) {
1846 ret = -EIO;
1847 break;
1848 }
b131dd5d
MSB
1849 if (copy_to_user(userbuf, data, eeprom.len)) {
1850 ret = -EFAULT;
1851 break;
1852 }
1853 userbuf += eeprom.len;
1854 eeprom.offset += eeprom.len;
1855 bytes_remaining -= eeprom.len;
1856 }
1da177e4 1857
c5835df9
MSB
1858 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1859 eeprom.offset -= eeprom.len;
1860 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1861 ret = -EFAULT;
1862
1da177e4
LT
1863 kfree(data);
1864 return ret;
1865}
1866
081d094e
BH
1867static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1868{
1869 const struct ethtool_ops *ops = dev->ethtool_ops;
1870
e0fb6fb6
GR
1871 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1872 !ops->get_eeprom_len(dev))
081d094e
BH
1873 return -EOPNOTSUPP;
1874
1875 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1876 ops->get_eeprom_len(dev));
1877}
1878
1da177e4
LT
1879static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1880{
1881 struct ethtool_eeprom eeprom;
76fd8593 1882 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
1883 void __user *userbuf = useraddr + sizeof(eeprom);
1884 u32 bytes_remaining;
1da177e4 1885 u8 *data;
b131dd5d 1886 int ret = 0;
1da177e4 1887
e0fb6fb6
GR
1888 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1889 !ops->get_eeprom_len(dev))
1da177e4
LT
1890 return -EOPNOTSUPP;
1891
1892 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1893 return -EFAULT;
1894
1895 /* Check for wrap and zero */
1896 if (eeprom.offset + eeprom.len <= eeprom.offset)
1897 return -EINVAL;
1898
1899 /* Check for exceeding total eeprom len */
1900 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1901 return -EINVAL;
1902
80ec82e3 1903 data = kzalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1904 if (!data)
1905 return -ENOMEM;
1906
b131dd5d
MSB
1907 bytes_remaining = eeprom.len;
1908 while (bytes_remaining > 0) {
1909 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1910
1911 if (copy_from_user(data, userbuf, eeprom.len)) {
1912 ret = -EFAULT;
1913 break;
1914 }
1915 ret = ops->set_eeprom(dev, &eeprom, data);
1916 if (ret)
1917 break;
1918 userbuf += eeprom.len;
1919 eeprom.offset += eeprom.len;
1920 bytes_remaining -= eeprom.len;
1921 }
1da177e4 1922
1da177e4
LT
1923 kfree(data);
1924 return ret;
1925}
1926
97f8aefb 1927static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1928 void __user *useraddr)
1da177e4 1929{
8e557421 1930 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
f3ccfda1 1931 struct kernel_ethtool_coalesce kernel_coalesce = {};
31610711 1932 int ret;
1da177e4
LT
1933
1934 if (!dev->ethtool_ops->get_coalesce)
1935 return -EOPNOTSUPP;
1936
f3ccfda1
YM
1937 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1938 NULL);
31610711
HK
1939 if (ret)
1940 return ret;
1da177e4
LT
1941
1942 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1943 return -EFAULT;
1944 return 0;
1945}
1946
95cddcb5
JK
1947static bool
1948ethtool_set_coalesce_supported(struct net_device *dev,
1949 struct ethtool_coalesce *coalesce)
1950{
1951 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1952 u32 nonzero_params = 0;
1953
95cddcb5
JK
1954 if (coalesce->rx_coalesce_usecs)
1955 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1956 if (coalesce->rx_max_coalesced_frames)
1957 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1958 if (coalesce->rx_coalesce_usecs_irq)
1959 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1960 if (coalesce->rx_max_coalesced_frames_irq)
1961 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1962 if (coalesce->tx_coalesce_usecs)
1963 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1964 if (coalesce->tx_max_coalesced_frames)
1965 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1966 if (coalesce->tx_coalesce_usecs_irq)
1967 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1968 if (coalesce->tx_max_coalesced_frames_irq)
1969 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1970 if (coalesce->stats_block_coalesce_usecs)
1971 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1972 if (coalesce->use_adaptive_rx_coalesce)
1973 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1974 if (coalesce->use_adaptive_tx_coalesce)
1975 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1976 if (coalesce->pkt_rate_low)
1977 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1978 if (coalesce->rx_coalesce_usecs_low)
1979 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1980 if (coalesce->rx_max_coalesced_frames_low)
1981 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1982 if (coalesce->tx_coalesce_usecs_low)
1983 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1984 if (coalesce->tx_max_coalesced_frames_low)
1985 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1986 if (coalesce->pkt_rate_high)
1987 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1988 if (coalesce->rx_coalesce_usecs_high)
1989 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1990 if (coalesce->rx_max_coalesced_frames_high)
1991 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1992 if (coalesce->tx_coalesce_usecs_high)
1993 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1994 if (coalesce->tx_max_coalesced_frames_high)
1995 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1996 if (coalesce->rate_sample_interval)
1997 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1998
1999 return (supported_params & nonzero_params) == nonzero_params;
2000}
2001
97f8aefb 2002static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
2003 void __user *useraddr)
1da177e4 2004{
f3ccfda1 2005 struct kernel_ethtool_coalesce kernel_coalesce = {};
1da177e4 2006 struct ethtool_coalesce coalesce;
0cf3eac8 2007 int ret;
1da177e4 2008
0276af21 2009 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1da177e4
LT
2010 return -EOPNOTSUPP;
2011
f3ccfda1
YM
2012 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
2013 NULL);
2014 if (ret)
2015 return ret;
2016
1da177e4
LT
2017 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
2018 return -EFAULT;
2019
95cddcb5
JK
2020 if (!ethtool_set_coalesce_supported(dev, &coalesce))
2021 return -EOPNOTSUPP;
2022
f3ccfda1
YM
2023 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
2024 NULL);
0cf3eac8
MK
2025 if (!ret)
2026 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
2027 return ret;
1da177e4
LT
2028}
2029
2030static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
2031{
8e557421 2032 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
74624944 2033 struct kernel_ethtool_ringparam kernel_ringparam = {};
1da177e4
LT
2034
2035 if (!dev->ethtool_ops->get_ringparam)
2036 return -EOPNOTSUPP;
2037
74624944
HC
2038 dev->ethtool_ops->get_ringparam(dev, &ringparam,
2039 &kernel_ringparam, NULL);
1da177e4
LT
2040
2041 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
2042 return -EFAULT;
2043 return 0;
2044}
2045
2046static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
2047{
37e2d99b 2048 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
74624944 2049 struct kernel_ethtool_ringparam kernel_ringparam;
bc9d1c99 2050 int ret;
1da177e4 2051
37e2d99b 2052 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1da177e4
LT
2053 return -EOPNOTSUPP;
2054
2055 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
2056 return -EFAULT;
2057
74624944 2058 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
37e2d99b
EE
2059
2060 /* ensure new ring parameters are within the maximums */
2061 if (ringparam.rx_pending > max.rx_max_pending ||
2062 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
2063 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
2064 ringparam.tx_pending > max.tx_max_pending)
2065 return -EINVAL;
2066
74624944
HC
2067 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
2068 &kernel_ringparam, NULL);
bc9d1c99
MK
2069 if (!ret)
2070 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
2071 return ret;
1da177e4
LT
2072}
2073
8b5933c3 2074static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
2075 void __user *useraddr)
2076{
2077 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
2078
2079 if (!dev->ethtool_ops->get_channels)
2080 return -EOPNOTSUPP;
2081
2082 dev->ethtool_ops->get_channels(dev, &channels);
2083
2084 if (copy_to_user(useraddr, &channels, sizeof(channels)))
2085 return -EFAULT;
2086 return 0;
2087}
2088
2089static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
2090 void __user *useraddr)
2091{
b8c8a2e2 2092 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1661d346 2093 u16 from_channel, to_channel;
1661d346 2094 unsigned int i;
546379b9 2095 int ret;
8b5933c3 2096
8bf36862 2097 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
8b5933c3 2098 return -EOPNOTSUPP;
2099
2100 if (copy_from_user(&channels, useraddr, sizeof(channels)))
2101 return -EFAULT;
2102
b8c8a2e2 2103 dev->ethtool_ops->get_channels(dev, &curr);
8bf36862 2104
75c36dbb
JK
2105 if (channels.rx_count == curr.rx_count &&
2106 channels.tx_count == curr.tx_count &&
2107 channels.combined_count == curr.combined_count &&
2108 channels.other_count == curr.other_count)
2109 return 0;
2110
8bf36862 2111 /* ensure new counts are within the maximums */
b8c8a2e2
JK
2112 if (channels.rx_count > curr.max_rx ||
2113 channels.tx_count > curr.max_tx ||
2114 channels.combined_count > curr.max_combined ||
2115 channels.other_count > curr.max_other)
8bf36862
KJ
2116 return -EINVAL;
2117
7be92514
JK
2118 /* ensure there is at least one RX and one TX channel */
2119 if (!channels.combined_count &&
2120 (!channels.rx_count || !channels.tx_count))
2121 return -EINVAL;
2122
916b7d31
MA
2123 ret = ethtool_check_max_channel(dev, channels, NULL);
2124 if (ret)
2125 return ret;
d4ab4286 2126
1661d346
JK
2127 /* Disabling channels, query zero-copy AF_XDP sockets */
2128 from_channel = channels.combined_count +
2129 min(channels.rx_count, channels.tx_count);
2130 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
2131 for (i = from_channel; i < to_channel; i++)
c4655761 2132 if (xsk_get_pool_from_qid(dev, i))
1661d346
JK
2133 return -EINVAL;
2134
546379b9
MK
2135 ret = dev->ethtool_ops->set_channels(dev, &channels);
2136 if (!ret)
2137 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
2138 return ret;
8b5933c3 2139}
2140
1da177e4
LT
2141static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
2142{
e83887f6 2143 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1da177e4
LT
2144
2145 if (!dev->ethtool_ops->get_pauseparam)
2146 return -EOPNOTSUPP;
2147
2148 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
2149
2150 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
2151 return -EFAULT;
2152 return 0;
2153}
2154
2155static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
2156{
2157 struct ethtool_pauseparam pauseparam;
bf37faa3 2158 int ret;
1da177e4 2159
e1b90c41 2160 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
2161 return -EOPNOTSUPP;
2162
2163 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
2164 return -EFAULT;
2165
bf37faa3
MK
2166 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
2167 if (!ret)
2168 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
2169 return ret;
1da177e4
LT
2170}
2171
1da177e4
LT
2172static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
2173{
2174 struct ethtool_test test;
76fd8593 2175 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 2176 u64 *data;
ff03d49f 2177 int ret, test_len;
1da177e4 2178
a9828ec6 2179 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
2180 return -EOPNOTSUPP;
2181
a9828ec6 2182 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
2183 if (test_len < 0)
2184 return test_len;
2185 WARN_ON(test_len == 0);
2186
1da177e4
LT
2187 if (copy_from_user(&test, useraddr, sizeof(test)))
2188 return -EFAULT;
2189
ff03d49f 2190 test.len = test_len;
80ec82e3 2191 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1da177e4
LT
2192 if (!data)
2193 return -ENOMEM;
2194
77e9b2ab 2195 netif_testing_on(dev);
1da177e4 2196 ops->self_test(dev, &test, data);
77e9b2ab 2197 netif_testing_off(dev);
1da177e4
LT
2198
2199 ret = -EFAULT;
2200 if (copy_to_user(useraddr, &test, sizeof(test)))
2201 goto out;
2202 useraddr += sizeof(test);
ed717613 2203 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
1da177e4
LT
2204 goto out;
2205 ret = 0;
2206
2207 out:
2208 kfree(data);
2209 return ret;
2210}
2211
2212static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
2213{
2214 struct ethtool_gstrings gstrings;
1da177e4
LT
2215 u8 *data;
2216 int ret;
2217
1da177e4
LT
2218 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
2219 return -EFAULT;
2220
340ae165 2221 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
2222 if (ret < 0)
2223 return ret;
4d1ceea8
AS
2224 if (ret > S32_MAX / ETH_GSTRING_LEN)
2225 return -ENOMEM;
2226 WARN_ON_ONCE(!ret);
a9828ec6
BH
2227
2228 gstrings.len = ret;
1da177e4 2229
3d883026
LR
2230 if (gstrings.len) {
2231 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
2232 if (!data)
2233 return -ENOMEM;
2234
2235 __ethtool_get_strings(dev, gstrings.string_set, data);
2236 } else {
2237 data = NULL;
2238 }
1da177e4
LT
2239
2240 ret = -EFAULT;
2241 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
2242 goto out;
2243 useraddr += sizeof(gstrings);
4d1ceea8 2244 if (gstrings.len &&
ed717613
GS
2245 copy_to_user(useraddr, data,
2246 array_size(gstrings.len, ETH_GSTRING_LEN)))
1da177e4
LT
2247 goto out;
2248 ret = 0;
2249
340ae165 2250out:
4d1ceea8 2251 vfree(data);
1da177e4
LT
2252 return ret;
2253}
2254
7888fe53
AD
2255__printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
2256{
2257 va_list args;
2258
2259 va_start(args, fmt);
2260 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
2261 va_end(args);
2262
2263 *data += ETH_GSTRING_LEN;
2264}
2265EXPORT_SYMBOL(ethtool_sprintf);
2266
2a48c635 2267void ethtool_puts(u8 **data, const char *str)
2268{
2269 strscpy(*data, str, ETH_GSTRING_LEN);
2270 *data += ETH_GSTRING_LEN;
2271}
2272EXPORT_SYMBOL(ethtool_puts);
2273
1da177e4
LT
2274static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
2275{
2276 struct ethtool_value id;
68f512f2 2277 static bool busy;
c03a14e8 2278 const struct ethtool_ops *ops = dev->ethtool_ops;
5ae21950 2279 netdevice_tracker dev_tracker;
68f512f2 2280 int rc;
1da177e4 2281
c03a14e8 2282 if (!ops->set_phys_id)
1da177e4
LT
2283 return -EOPNOTSUPP;
2284
68f512f2
BH
2285 if (busy)
2286 return -EBUSY;
2287
1da177e4
LT
2288 if (copy_from_user(&id, useraddr, sizeof(id)))
2289 return -EFAULT;
2290
c03a14e8 2291 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 2292 if (rc < 0)
68f512f2
BH
2293 return rc;
2294
2295 /* Drop the RTNL lock while waiting, but prevent reentry or
2296 * removal of the device.
2297 */
2298 busy = true;
d62607c3 2299 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
68f512f2
BH
2300 rtnl_unlock();
2301
2302 if (rc == 0) {
2303 /* Driver will handle this itself */
2304 schedule_timeout_interruptible(
143780c6 2305 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 2306 } else {
fce55922 2307 /* Driver expects to be called at twice the frequency in rc */
2adc6edc 2308 int n = rc * 2, interval = HZ / n;
64a8f8f7
MK
2309 u64 count = mul_u32_u32(n, id.data);
2310 u64 i = 0;
fce55922 2311
68f512f2 2312 do {
2adc6edc
EC
2313 rtnl_lock();
2314 rc = ops->set_phys_id(dev,
2315 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2316 rtnl_unlock();
2317 if (rc)
2318 break;
2319 schedule_timeout_interruptible(interval);
2320 } while (!signal_pending(current) && (!id.data || i < count));
68f512f2
BH
2321 }
2322
2323 rtnl_lock();
d62607c3 2324 netdev_put(dev, &dev_tracker);
68f512f2
BH
2325 busy = false;
2326
c03a14e8 2327 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
68f512f2 2328 return rc;
1da177e4
LT
2329}
2330
2331static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2332{
2333 struct ethtool_stats stats;
76fd8593 2334 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 2335 u64 *data;
ff03d49f 2336 int ret, n_stats;
1da177e4 2337
a9828ec6 2338 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
2339 return -EOPNOTSUPP;
2340
a9828ec6 2341 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
2342 if (n_stats < 0)
2343 return n_stats;
4d1ceea8
AS
2344 if (n_stats > S32_MAX / sizeof(u64))
2345 return -ENOMEM;
2346 WARN_ON_ONCE(!n_stats);
1da177e4
LT
2347 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2348 return -EFAULT;
2349
ff03d49f 2350 stats.n_stats = n_stats;
1da177e4 2351
3d883026
LR
2352 if (n_stats) {
2353 data = vzalloc(array_size(n_stats, sizeof(u64)));
2354 if (!data)
2355 return -ENOMEM;
2356 ops->get_ethtool_stats(dev, &stats, data);
2357 } else {
2358 data = NULL;
2359 }
1da177e4
LT
2360
2361 ret = -EFAULT;
2362 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2363 goto out;
2364 useraddr += sizeof(stats);
3dd14996 2365 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
1da177e4
LT
2366 goto out;
2367 ret = 0;
2368
2369 out:
4d1ceea8 2370 vfree(data);
1da177e4
LT
2371 return ret;
2372}
2373
201ed315 2374static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
f3a40945 2375{
f3a40945
AL
2376 if (n_stats < 0)
2377 return n_stats;
4d1ceea8
AS
2378 if (n_stats > S32_MAX / sizeof(u64))
2379 return -ENOMEM;
9deb1e9f
DT
2380 if (WARN_ON_ONCE(!n_stats))
2381 return -EOPNOTSUPP;
f3a40945 2382
201ed315
DT
2383 *data = vzalloc(array_size(n_stats, sizeof(u64)));
2384 if (!*data)
2385 return -ENOMEM;
2386
2387 return 0;
2388}
2389
2390static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2391 struct ethtool_stats *stats,
2392 u64 **data)
2393 {
2394 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2395 int n_stats, ret;
2396
2397 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2398 return -EOPNOTSUPP;
2399
2400 n_stats = phy_ops->get_sset_count(phydev);
2401
2402 ret = ethtool_vzalloc_stats_array(n_stats, data);
2403 if (ret)
2404 return ret;
2405
2406 stats->n_stats = n_stats;
2407 return phy_ops->get_stats(phydev, stats, *data);
2408}
2409
2410static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2411 struct ethtool_stats *stats,
2412 u64 **data)
2413{
2414 const struct ethtool_ops *ops = dev->ethtool_ops;
2415 int n_stats, ret;
2416
0dcc53ab 2417 if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats)
201ed315
DT
2418 return -EOPNOTSUPP;
2419
2420 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2421
2422 ret = ethtool_vzalloc_stats_array(n_stats, data);
2423 if (ret)
2424 return ret;
2425
2426 stats->n_stats = n_stats;
2427 ops->get_ethtool_phy_stats(dev, stats, *data);
2428
2429 return 0;
2430}
2431
2432static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2433{
2434 struct phy_device *phydev = dev->phydev;
2435 struct ethtool_stats stats;
2436 u64 *data = NULL;
2437 int ret = -EOPNOTSUPP;
2438
f3a40945
AL
2439 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2440 return -EFAULT;
2441
201ed315
DT
2442 if (phydev)
2443 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
f3a40945 2444
201ed315
DT
2445 if (ret == -EOPNOTSUPP)
2446 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
3d883026 2447
201ed315
DT
2448 if (ret)
2449 goto out;
f3a40945 2450
201ed315
DT
2451 if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2452 ret = -EFAULT;
f3a40945 2453 goto out;
201ed315
DT
2454 }
2455
f3a40945 2456 useraddr += sizeof(stats);
201ed315
DT
2457 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2458 ret = -EFAULT;
f3a40945
AL
2459
2460 out:
4d1ceea8 2461 vfree(data);
f3a40945
AL
2462 return ret;
2463}
2464
0bf0519d 2465static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
2466{
2467 struct ethtool_perm_addr epaddr;
a6f9a705 2468
313674af 2469 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
2470 return -EFAULT;
2471
313674af
MW
2472 if (epaddr.size < dev->addr_len)
2473 return -ETOOSMALL;
2474 epaddr.size = dev->addr_len;
a6f9a705 2475
a6f9a705 2476 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 2477 return -EFAULT;
a6f9a705 2478 useraddr += sizeof(epaddr);
313674af
MW
2479 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2480 return -EFAULT;
2481 return 0;
a6f9a705
JW
2482}
2483
13c99b24
JG
2484static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2485 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 2486{
8e557421 2487 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 2488
13c99b24 2489 if (!actor)
3ae7c0b2
JG
2490 return -EOPNOTSUPP;
2491
13c99b24 2492 edata.data = actor(dev);
3ae7c0b2
JG
2493
2494 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2495 return -EFAULT;
2496 return 0;
2497}
2498
13c99b24
JG
2499static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2500 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
2501{
2502 struct ethtool_value edata;
2503
13c99b24 2504 if (!actor)
3ae7c0b2
JG
2505 return -EOPNOTSUPP;
2506
2507 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2508 return -EFAULT;
2509
13c99b24 2510 actor(dev, edata.data);
339bf024
JG
2511 return 0;
2512}
2513
13c99b24
JG
2514static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2515 int (*actor)(struct net_device *, u32))
339bf024
JG
2516{
2517 struct ethtool_value edata;
2518
13c99b24 2519 if (!actor)
339bf024
JG
2520 return -EOPNOTSUPP;
2521
2522 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2523 return -EFAULT;
2524
13c99b24 2525 return actor(dev, edata.data);
339bf024
JG
2526}
2527
095cfcfe
JK
2528static int
2529ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
05c6a8d7 2530{
1af0a094
JK
2531 if (!dev->ethtool_ops->flash_device) {
2532 req->devlink = netdev_to_devlink_get(dev);
2533 return 0;
2534 }
786f5281 2535
095cfcfe 2536 return dev->ethtool_ops->flash_device(dev, &req->efl);
05c6a8d7
AK
2537}
2538
29dd54b7
AC
2539static int ethtool_set_dump(struct net_device *dev,
2540 void __user *useraddr)
2541{
2542 struct ethtool_dump dump;
2543
2544 if (!dev->ethtool_ops->set_dump)
2545 return -EOPNOTSUPP;
2546
2547 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2548 return -EFAULT;
2549
2550 return dev->ethtool_ops->set_dump(dev, &dump);
2551}
2552
2553static int ethtool_get_dump_flag(struct net_device *dev,
2554 void __user *useraddr)
2555{
2556 int ret;
2557 struct ethtool_dump dump;
2558 const struct ethtool_ops *ops = dev->ethtool_ops;
2559
c03a14e8 2560 if (!ops->get_dump_flag)
29dd54b7
AC
2561 return -EOPNOTSUPP;
2562
2563 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2564 return -EFAULT;
2565
2566 ret = ops->get_dump_flag(dev, &dump);
2567 if (ret)
2568 return ret;
2569
2570 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2571 return -EFAULT;
2572 return 0;
2573}
2574
2575static int ethtool_get_dump_data(struct net_device *dev,
2576 void __user *useraddr)
2577{
2578 int ret;
2579 __u32 len;
2580 struct ethtool_dump dump, tmp;
2581 const struct ethtool_ops *ops = dev->ethtool_ops;
2582 void *data = NULL;
2583
c03a14e8 2584 if (!ops->get_dump_data || !ops->get_dump_flag)
29dd54b7
AC
2585 return -EOPNOTSUPP;
2586
2587 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2588 return -EFAULT;
2589
2590 memset(&tmp, 0, sizeof(tmp));
2591 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2592 ret = ops->get_dump_flag(dev, &tmp);
2593 if (ret)
2594 return ret;
2595
c590b5e2 2596 len = min(tmp.len, dump.len);
29dd54b7
AC
2597 if (!len)
2598 return -EFAULT;
2599
c590b5e2
MS
2600 /* Don't ever let the driver think there's more space available
2601 * than it requested with .get_dump_flag().
2602 */
2603 dump.len = len;
2604
2605 /* Always allocate enough space to hold the whole thing so that the
2606 * driver does not need to check the length and bother with partial
2607 * dumping.
2608 */
29dd54b7
AC
2609 data = vzalloc(tmp.len);
2610 if (!data)
2611 return -ENOMEM;
2612 ret = ops->get_dump_data(dev, &dump, data);
2613 if (ret)
2614 goto out;
2615
c590b5e2
MS
2616 /* There are two sane possibilities:
2617 * 1. The driver's .get_dump_data() does not touch dump.len.
2618 * 2. Or it may set dump.len to how much it really writes, which
2619 * should be tmp.len (or len if it can do a partial dump).
2620 * In any case respond to userspace with the actual length of data
2621 * it's receiving.
2622 */
2623 WARN_ON(dump.len != len && dump.len != tmp.len);
2624 dump.len = len;
2625
29dd54b7
AC
2626 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2627 ret = -EFAULT;
2628 goto out;
2629 }
2630 useraddr += offsetof(struct ethtool_dump, data);
2631 if (copy_to_user(useraddr, data, len))
2632 ret = -EFAULT;
2633out:
2634 vfree(data);
2635 return ret;
2636}
2637
c8f3a8c3
RC
2638static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2639{
2111375b
KM
2640 struct kernel_ethtool_ts_info kernel_info;
2641 struct ethtool_ts_info info = {};
5b071c59 2642 int err;
c8f3a8c3 2643
2111375b 2644 err = __ethtool_get_ts_info(dev, &kernel_info);
c8f3a8c3
RC
2645 if (err)
2646 return err;
2647
2111375b
KM
2648 info.cmd = kernel_info.cmd;
2649 info.so_timestamping = kernel_info.so_timestamping;
2650 info.phc_index = kernel_info.phc_index;
2651 info.tx_types = kernel_info.tx_types;
2652 info.rx_filters = kernel_info.rx_filters;
2653
c8f3a8c3 2654 if (copy_to_user(useraddr, &info, sizeof(info)))
5b071c59 2655 return -EFAULT;
c8f3a8c3 2656
5b071c59 2657 return 0;
c8f3a8c3
RC
2658}
2659
95dfc7ef
AL
2660int ethtool_get_module_info_call(struct net_device *dev,
2661 struct ethtool_modinfo *modinfo)
2f438366
ES
2662{
2663 const struct ethtool_ops *ops = dev->ethtool_ops;
2664 struct phy_device *phydev = dev->phydev;
2665
caa93b7c 2666 if (dev->ethtool->module_fw_flash_in_progress)
31e0aa99
DR
2667 return -EBUSY;
2668
e679c9c1
RK
2669 if (dev->sfp_bus)
2670 return sfp_get_module_info(dev->sfp_bus, modinfo);
2671
2f438366
ES
2672 if (phydev && phydev->drv && phydev->drv->module_info)
2673 return phydev->drv->module_info(phydev, modinfo);
2674
2675 if (ops->get_module_info)
2676 return ops->get_module_info(dev, modinfo);
2677
2678 return -EOPNOTSUPP;
2679}
2680
41c3cb6d
SH
2681static int ethtool_get_module_info(struct net_device *dev,
2682 void __user *useraddr)
2683{
2684 int ret;
2685 struct ethtool_modinfo modinfo;
41c3cb6d
SH
2686
2687 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2688 return -EFAULT;
2689
95dfc7ef 2690 ret = ethtool_get_module_info_call(dev, &modinfo);
41c3cb6d
SH
2691 if (ret)
2692 return ret;
2693
2694 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2695 return -EFAULT;
2696
2697 return 0;
2698}
2699
95dfc7ef
AL
2700int ethtool_get_module_eeprom_call(struct net_device *dev,
2701 struct ethtool_eeprom *ee, u8 *data)
2f438366
ES
2702{
2703 const struct ethtool_ops *ops = dev->ethtool_ops;
2704 struct phy_device *phydev = dev->phydev;
2705
caa93b7c 2706 if (dev->ethtool->module_fw_flash_in_progress)
31e0aa99
DR
2707 return -EBUSY;
2708
e679c9c1
RK
2709 if (dev->sfp_bus)
2710 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2711
2f438366
ES
2712 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2713 return phydev->drv->module_eeprom(phydev, ee, data);
2714
2715 if (ops->get_module_eeprom)
2716 return ops->get_module_eeprom(dev, ee, data);
2717
2718 return -EOPNOTSUPP;
2719}
2720
41c3cb6d
SH
2721static int ethtool_get_module_eeprom(struct net_device *dev,
2722 void __user *useraddr)
2723{
2724 int ret;
2725 struct ethtool_modinfo modinfo;
41c3cb6d 2726
95dfc7ef 2727 ret = ethtool_get_module_info_call(dev, &modinfo);
41c3cb6d
SH
2728 if (ret)
2729 return ret;
2730
2f438366 2731 return ethtool_get_any_eeprom(dev, useraddr,
95dfc7ef 2732 ethtool_get_module_eeprom_call,
41c3cb6d
SH
2733 modinfo.eeprom_len);
2734}
2735
f0db9b07
GV
2736static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2737{
2738 switch (tuna->id) {
2739 case ETHTOOL_RX_COPYBREAK:
1255a505 2740 case ETHTOOL_TX_COPYBREAK:
448f413a 2741 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
f0db9b07
GV
2742 if (tuna->len != sizeof(u32) ||
2743 tuna->type_id != ETHTOOL_TUNABLE_U32)
2744 return -EINVAL;
2745 break;
e1577c1c
IK
2746 case ETHTOOL_PFC_PREVENTION_TOUT:
2747 if (tuna->len != sizeof(u16) ||
2748 tuna->type_id != ETHTOOL_TUNABLE_U16)
2749 return -EINVAL;
2750 break;
f0db9b07
GV
2751 default:
2752 return -EINVAL;
2753 }
2754
2755 return 0;
2756}
2757
2758static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2759{
2760 int ret;
2761 struct ethtool_tunable tuna;
2762 const struct ethtool_ops *ops = dev->ethtool_ops;
2763 void *data;
2764
2765 if (!ops->get_tunable)
2766 return -EOPNOTSUPP;
2767 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2768 return -EFAULT;
2769 ret = ethtool_tunable_valid(&tuna);
2770 if (ret)
2771 return ret;
80ec82e3 2772 data = kzalloc(tuna.len, GFP_USER);
f0db9b07
GV
2773 if (!data)
2774 return -ENOMEM;
2775 ret = ops->get_tunable(dev, &tuna, data);
2776 if (ret)
2777 goto out;
2778 useraddr += sizeof(tuna);
2779 ret = -EFAULT;
2780 if (copy_to_user(useraddr, data, tuna.len))
2781 goto out;
2782 ret = 0;
2783
2784out:
2785 kfree(data);
2786 return ret;
2787}
2788
2789static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2790{
2791 int ret;
2792 struct ethtool_tunable tuna;
2793 const struct ethtool_ops *ops = dev->ethtool_ops;
2794 void *data;
2795
2796 if (!ops->set_tunable)
2797 return -EOPNOTSUPP;
2798 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2799 return -EFAULT;
2800 ret = ethtool_tunable_valid(&tuna);
2801 if (ret)
2802 return ret;
f0db9b07 2803 useraddr += sizeof(tuna);
30e7e3ec
AV
2804 data = memdup_user(useraddr, tuna.len);
2805 if (IS_ERR(data))
2806 return PTR_ERR(data);
f0db9b07
GV
2807 ret = ops->set_tunable(dev, &tuna, data);
2808
f0db9b07
GV
2809 kfree(data);
2810 return ret;
2811}
2812
3499e87e
AB
2813static noinline_for_stack int
2814ethtool_get_per_queue_coalesce(struct net_device *dev,
2815 void __user *useraddr,
2816 struct ethtool_per_queue_op *per_queue_opt)
421797b1
KL
2817{
2818 u32 bit;
2819 int ret;
2820 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2821
2822 if (!dev->ethtool_ops->get_per_queue_coalesce)
2823 return -EOPNOTSUPP;
2824
2825 useraddr += sizeof(*per_queue_opt);
2826
3aa56885
YN
2827 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2828 MAX_NUM_QUEUE);
421797b1
KL
2829
2830 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2831 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2832
2833 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2834 if (ret != 0)
2835 return ret;
2836 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2837 return -EFAULT;
2838 useraddr += sizeof(coalesce);
2839 }
2840
2841 return 0;
2842}
2843
3499e87e
AB
2844static noinline_for_stack int
2845ethtool_set_per_queue_coalesce(struct net_device *dev,
2846 void __user *useraddr,
2847 struct ethtool_per_queue_op *per_queue_opt)
f38d138a
KL
2848{
2849 u32 bit;
2850 int i, ret = 0;
2851 int n_queue;
2852 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2853 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2854
2855 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2856 (!dev->ethtool_ops->get_per_queue_coalesce))
2857 return -EOPNOTSUPP;
2858
2859 useraddr += sizeof(*per_queue_opt);
2860
3aa56885 2861 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
f38d138a
KL
2862 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2863 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2864 if (!backup)
2865 return -ENOMEM;
2866
2867 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2868 struct ethtool_coalesce coalesce;
2869
2870 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2871 if (ret != 0)
2872 goto roll_back;
2873
2874 tmp++;
2875
2876 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2877 ret = -EFAULT;
2878 goto roll_back;
2879 }
2880
95cddcb5
JK
2881 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2882 ret = -EOPNOTSUPP;
2883 goto roll_back;
2884 }
2885
f38d138a
KL
2886 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2887 if (ret != 0)
2888 goto roll_back;
2889
2890 useraddr += sizeof(coalesce);
2891 }
2892
2893roll_back:
2894 if (ret != 0) {
2895 tmp = backup;
2896 for_each_set_bit(i, queue_mask, bit) {
2897 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2898 tmp++;
2899 }
2900 }
2901 kfree(backup);
2902
2903 return ret;
2904}
2905
3499e87e 2906static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
58f5bbe3 2907 void __user *useraddr, u32 sub_cmd)
ac2c7ad0
KL
2908{
2909 struct ethtool_per_queue_op per_queue_opt;
2910
2911 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2912 return -EFAULT;
2913
58f5bbe3
WW
2914 if (per_queue_opt.sub_command != sub_cmd)
2915 return -EINVAL;
2916
ac2c7ad0 2917 switch (per_queue_opt.sub_command) {
421797b1
KL
2918 case ETHTOOL_GCOALESCE:
2919 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
f38d138a
KL
2920 case ETHTOOL_SCOALESCE:
2921 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
ac2c7ad0
KL
2922 default:
2923 return -EOPNOTSUPP;
9d253c02 2924 }
ac2c7ad0
KL
2925}
2926
968ad9da
RL
2927static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2928{
2929 switch (tuna->id) {
65feddd5 2930 case ETHTOOL_PHY_DOWNSHIFT:
3aeb0803 2931 case ETHTOOL_PHY_FAST_LINK_DOWN:
65feddd5
RL
2932 if (tuna->len != sizeof(u8) ||
2933 tuna->type_id != ETHTOOL_TUNABLE_U8)
2934 return -EINVAL;
2935 break;
9f2f13f4
AA
2936 case ETHTOOL_PHY_EDPD:
2937 if (tuna->len != sizeof(u16) ||
2938 tuna->type_id != ETHTOOL_TUNABLE_U16)
2939 return -EINVAL;
2940 break;
968ad9da
RL
2941 default:
2942 return -EINVAL;
2943 }
2944
2945 return 0;
2946}
2947
2948static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2949{
968ad9da 2950 struct phy_device *phydev = dev->phydev;
c6db31ff
IR
2951 struct ethtool_tunable tuna;
2952 bool phy_drv_tunable;
968ad9da 2953 void *data;
c6db31ff 2954 int ret;
968ad9da 2955
c6db31ff
IR
2956 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2957 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
968ad9da 2958 return -EOPNOTSUPP;
968ad9da
RL
2959 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2960 return -EFAULT;
2961 ret = ethtool_phy_tunable_valid(&tuna);
2962 if (ret)
2963 return ret;
80ec82e3 2964 data = kzalloc(tuna.len, GFP_USER);
968ad9da
RL
2965 if (!data)
2966 return -ENOMEM;
c6db31ff
IR
2967 if (phy_drv_tunable) {
2968 mutex_lock(&phydev->lock);
2969 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2970 mutex_unlock(&phydev->lock);
2971 } else {
2972 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2973 }
968ad9da
RL
2974 if (ret)
2975 goto out;
2976 useraddr += sizeof(tuna);
2977 ret = -EFAULT;
2978 if (copy_to_user(useraddr, data, tuna.len))
2979 goto out;
2980 ret = 0;
2981
2982out:
2983 kfree(data);
2984 return ret;
2985}
2986
2987static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2988{
968ad9da 2989 struct phy_device *phydev = dev->phydev;
c6db31ff
IR
2990 struct ethtool_tunable tuna;
2991 bool phy_drv_tunable;
968ad9da 2992 void *data;
c6db31ff 2993 int ret;
968ad9da 2994
c6db31ff
IR
2995 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2996 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
968ad9da
RL
2997 return -EOPNOTSUPP;
2998 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2999 return -EFAULT;
3000 ret = ethtool_phy_tunable_valid(&tuna);
3001 if (ret)
3002 return ret;
968ad9da 3003 useraddr += sizeof(tuna);
30e7e3ec
AV
3004 data = memdup_user(useraddr, tuna.len);
3005 if (IS_ERR(data))
3006 return PTR_ERR(data);
c6db31ff
IR
3007 if (phy_drv_tunable) {
3008 mutex_lock(&phydev->lock);
3009 ret = phydev->drv->set_tunable(phydev, &tuna, data);
3010 mutex_unlock(&phydev->lock);
3011 } else {
3012 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
3013 }
968ad9da 3014
968ad9da
RL
3015 kfree(data);
3016 return ret;
3017}
3018
1a5f3da2
VSR
3019static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
3020{
e83887f6 3021 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
a6d50512 3022 int rc;
1a5f3da2
VSR
3023
3024 if (!dev->ethtool_ops->get_fecparam)
3025 return -EOPNOTSUPP;
3026
a6d50512
EC
3027 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
3028 if (rc)
3029 return rc;
1a5f3da2 3030
240e1144
JK
3031 if (WARN_ON_ONCE(fecparam.reserved))
3032 fecparam.reserved = 0;
3033
1a5f3da2
VSR
3034 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
3035 return -EFAULT;
3036 return 0;
3037}
3038
3039static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
3040{
3041 struct ethtool_fecparam fecparam;
3042
3043 if (!dev->ethtool_ops->set_fecparam)
3044 return -EOPNOTSUPP;
3045
3046 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
3047 return -EFAULT;
3048
cf2cc0bf 3049 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
42ce127d
JK
3050 return -EINVAL;
3051
d3b37fc8 3052 fecparam.active_fec = 0;
240e1144
JK
3053 fecparam.reserved = 0;
3054
1a5f3da2
VSR
3055 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
3056}
3057
600fed5e 3058/* The main entry point in this file. Called from net/core/dev_ioctl.c */
1da177e4 3059
f49deaa6 3060static int
095cfcfe
JK
3061__dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
3062 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
1da177e4 3063{
095cfcfe
JK
3064 struct net_device *dev;
3065 u32 sub_cmd;
1da177e4 3066 int rc;
b29d3145 3067 netdev_features_t old_features;
1da177e4 3068
095cfcfe 3069 dev = __dev_get_by_name(net, ifr->ifr_name);
f32a2137 3070 if (!dev)
1da177e4
LT
3071 return -ENODEV;
3072
ac2c7ad0
KL
3073 if (ethcmd == ETHTOOL_PERQUEUE) {
3074 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
3075 return -EFAULT;
3076 } else {
3077 sub_cmd = ethcmd;
3078 }
75f3123c 3079 /* Allow some commands to be done by anyone */
ac2c7ad0 3080 switch (sub_cmd) {
0fdc100b 3081 case ETHTOOL_GSET:
75f3123c 3082 case ETHTOOL_GDRVINFO:
75f3123c 3083 case ETHTOOL_GMSGLVL:
2da45db2 3084 case ETHTOOL_GLINK:
75f3123c
SH
3085 case ETHTOOL_GCOALESCE:
3086 case ETHTOOL_GRINGPARAM:
3087 case ETHTOOL_GPAUSEPARAM:
3088 case ETHTOOL_GRXCSUM:
3089 case ETHTOOL_GTXCSUM:
3090 case ETHTOOL_GSG:
f80400a2 3091 case ETHTOOL_GSSET_INFO:
75f3123c 3092 case ETHTOOL_GSTRINGS:
2da45db2 3093 case ETHTOOL_GSTATS:
f3a40945 3094 case ETHTOOL_GPHYSTATS:
75f3123c
SH
3095 case ETHTOOL_GTSO:
3096 case ETHTOOL_GPERMADDR:
474ff260 3097 case ETHTOOL_GUFO:
75f3123c 3098 case ETHTOOL_GGSO:
1cab819b 3099 case ETHTOOL_GGRO:
339bf024
JG
3100 case ETHTOOL_GFLAGS:
3101 case ETHTOOL_GPFLAGS:
0853ad66 3102 case ETHTOOL_GRXFH:
59089d8d
SB
3103 case ETHTOOL_GRXRINGS:
3104 case ETHTOOL_GRXCLSRLCNT:
3105 case ETHTOOL_GRXCLSRULE:
3106 case ETHTOOL_GRXCLSRLALL:
2da45db2 3107 case ETHTOOL_GRXFHINDIR:
3de0b592 3108 case ETHTOOL_GRSSH:
5455c699 3109 case ETHTOOL_GFEATURES:
2da45db2 3110 case ETHTOOL_GCHANNELS:
c8f3a8c3 3111 case ETHTOOL_GET_TS_INFO:
2da45db2 3112 case ETHTOOL_GEEE:
f0db9b07 3113 case ETHTOOL_GTUNABLE:
968ad9da 3114 case ETHTOOL_PHY_GTUNABLE:
8006f6bf 3115 case ETHTOOL_GLINKSETTINGS:
1a5f3da2 3116 case ETHTOOL_GFECPARAM:
75f3123c
SH
3117 break;
3118 default:
5e1fccc0 3119 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
75f3123c
SH
3120 return -EPERM;
3121 }
3122
f32a2137
HK
3123 if (dev->dev.parent)
3124 pm_runtime_get_sync(dev->dev.parent);
3125
3126 if (!netif_device_present(dev)) {
3127 rc = -ENODEV;
3128 goto out;
3129 }
3130
97f8aefb 3131 if (dev->ethtool_ops->begin) {
3132 rc = dev->ethtool_ops->begin(dev);
f32a2137
HK
3133 if (rc < 0)
3134 goto out;
97f8aefb 3135 }
d8a33ac4
SH
3136 old_features = dev->features;
3137
1da177e4
LT
3138 switch (ethcmd) {
3139 case ETHTOOL_GSET:
3140 rc = ethtool_get_settings(dev, useraddr);
3141 break;
3142 case ETHTOOL_SSET:
3143 rc = ethtool_set_settings(dev, useraddr);
3144 break;
3145 case ETHTOOL_GDRVINFO:
095cfcfe 3146 rc = ethtool_get_drvinfo(dev, devlink_state);
1da177e4
LT
3147 break;
3148 case ETHTOOL_GREGS:
3149 rc = ethtool_get_regs(dev, useraddr);
3150 break;
3151 case ETHTOOL_GWOL:
3152 rc = ethtool_get_wol(dev, useraddr);
3153 break;
3154 case ETHTOOL_SWOL:
3155 rc = ethtool_set_wol(dev, useraddr);
3156 break;
3157 case ETHTOOL_GMSGLVL:
13c99b24
JG
3158 rc = ethtool_get_value(dev, useraddr, ethcmd,
3159 dev->ethtool_ops->get_msglevel);
1da177e4
LT
3160 break;
3161 case ETHTOOL_SMSGLVL:
13c99b24
JG
3162 rc = ethtool_set_value_void(dev, useraddr,
3163 dev->ethtool_ops->set_msglevel);
0bda7af3
MK
3164 if (!rc)
3165 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
1da177e4 3166 break;
80f12ecc
YM
3167 case ETHTOOL_GEEE:
3168 rc = ethtool_get_eee(dev, useraddr);
3169 break;
3170 case ETHTOOL_SEEE:
3171 rc = ethtool_set_eee(dev, useraddr);
3172 break;
1da177e4
LT
3173 case ETHTOOL_NWAY_RST:
3174 rc = ethtool_nway_reset(dev);
3175 break;
3176 case ETHTOOL_GLINK:
e596e6e4 3177 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
3178 break;
3179 case ETHTOOL_GEEPROM:
3180 rc = ethtool_get_eeprom(dev, useraddr);
3181 break;
3182 case ETHTOOL_SEEPROM:
3183 rc = ethtool_set_eeprom(dev, useraddr);
3184 break;
3185 case ETHTOOL_GCOALESCE:
3186 rc = ethtool_get_coalesce(dev, useraddr);
3187 break;
3188 case ETHTOOL_SCOALESCE:
3189 rc = ethtool_set_coalesce(dev, useraddr);
3190 break;
3191 case ETHTOOL_GRINGPARAM:
3192 rc = ethtool_get_ringparam(dev, useraddr);
3193 break;
3194 case ETHTOOL_SRINGPARAM:
3195 rc = ethtool_set_ringparam(dev, useraddr);
3196 break;
3197 case ETHTOOL_GPAUSEPARAM:
3198 rc = ethtool_get_pauseparam(dev, useraddr);
3199 break;
3200 case ETHTOOL_SPAUSEPARAM:
3201 rc = ethtool_set_pauseparam(dev, useraddr);
3202 break;
1da177e4
LT
3203 case ETHTOOL_TEST:
3204 rc = ethtool_self_test(dev, useraddr);
3205 break;
3206 case ETHTOOL_GSTRINGS:
3207 rc = ethtool_get_strings(dev, useraddr);
3208 break;
3209 case ETHTOOL_PHYS_ID:
3210 rc = ethtool_phys_id(dev, useraddr);
3211 break;
3212 case ETHTOOL_GSTATS:
3213 rc = ethtool_get_stats(dev, useraddr);
3214 break;
a6f9a705
JW
3215 case ETHTOOL_GPERMADDR:
3216 rc = ethtool_get_perm_addr(dev, useraddr);
3217 break;
3ae7c0b2 3218 case ETHTOOL_GFLAGS:
13c99b24 3219 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 3220 __ethtool_get_flags);
3ae7c0b2
JG
3221 break;
3222 case ETHTOOL_SFLAGS:
da8ac86c 3223 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 3224 break;
339bf024 3225 case ETHTOOL_GPFLAGS:
13c99b24
JG
3226 rc = ethtool_get_value(dev, useraddr, ethcmd,
3227 dev->ethtool_ops->get_priv_flags);
111dcba3
MK
3228 if (!rc)
3229 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
339bf024
JG
3230 break;
3231 case ETHTOOL_SPFLAGS:
13c99b24
JG
3232 rc = ethtool_set_value(dev, useraddr,
3233 dev->ethtool_ops->set_priv_flags);
339bf024 3234 break;
0853ad66 3235 case ETHTOOL_GRXFH:
59089d8d
SB
3236 case ETHTOOL_GRXRINGS:
3237 case ETHTOOL_GRXCLSRLCNT:
3238 case ETHTOOL_GRXCLSRULE:
3239 case ETHTOOL_GRXCLSRLALL:
bf988435 3240 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
3241 break;
3242 case ETHTOOL_SRXFH:
59089d8d
SB
3243 case ETHTOOL_SRXCLSRLDEL:
3244 case ETHTOOL_SRXCLSRLINS:
bf988435 3245 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 3246 break;
05c6a8d7 3247 case ETHTOOL_FLASHDEV:
095cfcfe 3248 rc = ethtool_flash_device(dev, devlink_state);
05c6a8d7 3249 break;
d73d3a8c
BH
3250 case ETHTOOL_RESET:
3251 rc = ethtool_reset(dev, useraddr);
3252 break;
723b2f57
JG
3253 case ETHTOOL_GSSET_INFO:
3254 rc = ethtool_get_sset_info(dev, useraddr);
3255 break;
a5b6ee29
BH
3256 case ETHTOOL_GRXFHINDIR:
3257 rc = ethtool_get_rxfh_indir(dev, useraddr);
3258 break;
3259 case ETHTOOL_SRXFHINDIR:
3260 rc = ethtool_set_rxfh_indir(dev, useraddr);
3261 break;
3de0b592
VD
3262 case ETHTOOL_GRSSH:
3263 rc = ethtool_get_rxfh(dev, useraddr);
3264 break;
3265 case ETHTOOL_SRSSH:
3266 rc = ethtool_set_rxfh(dev, useraddr);
3267 break;
5455c699
MM
3268 case ETHTOOL_GFEATURES:
3269 rc = ethtool_get_features(dev, useraddr);
3270 break;
3271 case ETHTOOL_SFEATURES:
3272 rc = ethtool_set_features(dev, useraddr);
3273 break;
0a417704 3274 case ETHTOOL_GTXCSUM:
e83d360d 3275 case ETHTOOL_GRXCSUM:
0a417704
MM
3276 case ETHTOOL_GSG:
3277 case ETHTOOL_GTSO:
0a417704
MM
3278 case ETHTOOL_GGSO:
3279 case ETHTOOL_GGRO:
3280 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
3281 break;
3282 case ETHTOOL_STXCSUM:
e83d360d 3283 case ETHTOOL_SRXCSUM:
0a417704
MM
3284 case ETHTOOL_SSG:
3285 case ETHTOOL_STSO:
0a417704
MM
3286 case ETHTOOL_SGSO:
3287 case ETHTOOL_SGRO:
3288 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
3289 break;
8b5933c3 3290 case ETHTOOL_GCHANNELS:
3291 rc = ethtool_get_channels(dev, useraddr);
3292 break;
3293 case ETHTOOL_SCHANNELS:
3294 rc = ethtool_set_channels(dev, useraddr);
3295 break;
29dd54b7
AC
3296 case ETHTOOL_SET_DUMP:
3297 rc = ethtool_set_dump(dev, useraddr);
3298 break;
3299 case ETHTOOL_GET_DUMP_FLAG:
3300 rc = ethtool_get_dump_flag(dev, useraddr);
3301 break;
3302 case ETHTOOL_GET_DUMP_DATA:
3303 rc = ethtool_get_dump_data(dev, useraddr);
3304 break;
c8f3a8c3
RC
3305 case ETHTOOL_GET_TS_INFO:
3306 rc = ethtool_get_ts_info(dev, useraddr);
3307 break;
41c3cb6d
SH
3308 case ETHTOOL_GMODULEINFO:
3309 rc = ethtool_get_module_info(dev, useraddr);
3310 break;
3311 case ETHTOOL_GMODULEEEPROM:
3312 rc = ethtool_get_module_eeprom(dev, useraddr);
3313 break;
f0db9b07
GV
3314 case ETHTOOL_GTUNABLE:
3315 rc = ethtool_get_tunable(dev, useraddr);
3316 break;
3317 case ETHTOOL_STUNABLE:
3318 rc = ethtool_set_tunable(dev, useraddr);
3319 break;
f3a40945
AL
3320 case ETHTOOL_GPHYSTATS:
3321 rc = ethtool_get_phy_stats(dev, useraddr);
3322 break;
ac2c7ad0 3323 case ETHTOOL_PERQUEUE:
58f5bbe3 3324 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
ac2c7ad0 3325 break;
3f1ac7a7
DD
3326 case ETHTOOL_GLINKSETTINGS:
3327 rc = ethtool_get_link_ksettings(dev, useraddr);
3328 break;
3329 case ETHTOOL_SLINKSETTINGS:
3330 rc = ethtool_set_link_ksettings(dev, useraddr);
3331 break;
968ad9da
RL
3332 case ETHTOOL_PHY_GTUNABLE:
3333 rc = get_phy_tunable(dev, useraddr);
3334 break;
3335 case ETHTOOL_PHY_STUNABLE:
3336 rc = set_phy_tunable(dev, useraddr);
3337 break;
1a5f3da2
VSR
3338 case ETHTOOL_GFECPARAM:
3339 rc = ethtool_get_fecparam(dev, useraddr);
3340 break;
3341 case ETHTOOL_SFECPARAM:
3342 rc = ethtool_set_fecparam(dev, useraddr);
3343 break;
1da177e4 3344 default:
61a44b9c 3345 rc = -EOPNOTSUPP;
1da177e4 3346 }
4ec93edb 3347
e71a4783 3348 if (dev->ethtool_ops->complete)
1da177e4 3349 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
3350
3351 if (old_features != dev->features)
3352 netdev_features_change(dev);
f32a2137
HK
3353out:
3354 if (dev->dev.parent)
3355 pm_runtime_put(dev->dev.parent);
d8a33ac4 3356
1da177e4 3357 return rc;
1da177e4 3358}
eca4205f 3359
f49deaa6
JK
3360int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3361{
095cfcfe
JK
3362 struct ethtool_devlink_compat *state;
3363 u32 ethcmd;
f49deaa6
JK
3364 int rc;
3365
095cfcfe
JK
3366 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3367 return -EFAULT;
3368
3369 state = kzalloc(sizeof(*state), GFP_KERNEL);
3370 if (!state)
3371 return -ENOMEM;
3372
3373 switch (ethcmd) {
3374 case ETHTOOL_FLASHDEV:
3375 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3376 rc = -EFAULT;
3377 goto exit_free;
3378 }
3379 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3380 break;
3381 }
3382
f49deaa6 3383 rtnl_lock();
095cfcfe 3384 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
f49deaa6 3385 rtnl_unlock();
095cfcfe
JK
3386 if (rc)
3387 goto exit_free;
3388
3389 switch (ethcmd) {
1af0a094
JK
3390 case ETHTOOL_FLASHDEV:
3391 if (state->devlink)
3392 rc = devlink_compat_flash_update(state->devlink,
3393 state->efl.data);
3394 break;
095cfcfe 3395 case ETHTOOL_GDRVINFO:
1af0a094
JK
3396 if (state->devlink)
3397 devlink_compat_running_version(state->devlink,
3398 state->info.fw_version,
3399 sizeof(state->info.fw_version));
095cfcfe
JK
3400 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3401 rc = -EFAULT;
3402 goto exit_free;
3403 }
3404 break;
3405 }
f49deaa6 3406
095cfcfe 3407exit_free:
1af0a094
JK
3408 if (state->devlink)
3409 devlink_put(state->devlink);
095cfcfe 3410 kfree(state);
f49deaa6
JK
3411 return rc;
3412}
3413
eca4205f
PNA
3414struct ethtool_rx_flow_key {
3415 struct flow_dissector_key_basic basic;
3416 union {
3417 struct flow_dissector_key_ipv4_addrs ipv4;
3418 struct flow_dissector_key_ipv6_addrs ipv6;
3419 };
3420 struct flow_dissector_key_ports tp;
3421 struct flow_dissector_key_ip ip;
3422 struct flow_dissector_key_vlan vlan;
3423 struct flow_dissector_key_eth_addrs eth_addrs;
3424} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3425
3426struct ethtool_rx_flow_match {
3427 struct flow_dissector dissector;
3428 struct ethtool_rx_flow_key key;
3429 struct ethtool_rx_flow_key mask;
3430};
3431
3432struct ethtool_rx_flow_rule *
3433ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3434{
3435 const struct ethtool_rx_flow_spec *fs = input->fs;
eca4205f
PNA
3436 struct ethtool_rx_flow_match *match;
3437 struct ethtool_rx_flow_rule *flow;
3438 struct flow_action_entry *act;
3439
3440 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3441 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3442 if (!flow)
3443 return ERR_PTR(-ENOMEM);
3444
3445 /* ethtool_rx supports only one single action per rule. */
3446 flow->rule = flow_rule_alloc(1);
3447 if (!flow->rule) {
3448 kfree(flow);
3449 return ERR_PTR(-ENOMEM);
3450 }
3451
3452 match = (struct ethtool_rx_flow_match *)flow->priv;
3453 flow->rule->match.dissector = &match->dissector;
3454 flow->rule->match.mask = &match->mask;
3455 flow->rule->match.key = &match->key;
3456
3457 match->mask.basic.n_proto = htons(0xffff);
3458
3459 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
5b9469a2
MC
3460 case ETHER_FLOW: {
3461 const struct ethhdr *ether_spec, *ether_m_spec;
3462
3463 ether_spec = &fs->h_u.ether_spec;
3464 ether_m_spec = &fs->m_u.ether_spec;
3465
3466 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3467 ether_addr_copy(match->key.eth_addrs.src,
3468 ether_spec->h_source);
3469 ether_addr_copy(match->mask.eth_addrs.src,
3470 ether_m_spec->h_source);
3471 }
3472 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3473 ether_addr_copy(match->key.eth_addrs.dst,
3474 ether_spec->h_dest);
3475 ether_addr_copy(match->mask.eth_addrs.dst,
3476 ether_m_spec->h_dest);
3477 }
3478 if (ether_m_spec->h_proto) {
3479 match->key.basic.n_proto = ether_spec->h_proto;
3480 match->mask.basic.n_proto = ether_m_spec->h_proto;
3481 }
3482 }
3483 break;
eca4205f
PNA
3484 case TCP_V4_FLOW:
3485 case UDP_V4_FLOW: {
3486 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3487
3488 match->key.basic.n_proto = htons(ETH_P_IP);
3489
3490 v4_spec = &fs->h_u.tcp_ip4_spec;
3491 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3492
3493 if (v4_m_spec->ip4src) {
3494 match->key.ipv4.src = v4_spec->ip4src;
3495 match->mask.ipv4.src = v4_m_spec->ip4src;
3496 }
3497 if (v4_m_spec->ip4dst) {
3498 match->key.ipv4.dst = v4_spec->ip4dst;
3499 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3500 }
3501 if (v4_m_spec->ip4src ||
3502 v4_m_spec->ip4dst) {
3503 match->dissector.used_keys |=
2b3082c6 3504 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
eca4205f
PNA
3505 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3506 offsetof(struct ethtool_rx_flow_key, ipv4);
3507 }
3508 if (v4_m_spec->psrc) {
3509 match->key.tp.src = v4_spec->psrc;
3510 match->mask.tp.src = v4_m_spec->psrc;
3511 }
3512 if (v4_m_spec->pdst) {
3513 match->key.tp.dst = v4_spec->pdst;
3514 match->mask.tp.dst = v4_m_spec->pdst;
3515 }
3516 if (v4_m_spec->psrc ||
3517 v4_m_spec->pdst) {
3518 match->dissector.used_keys |=
2b3082c6 3519 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
eca4205f
PNA
3520 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3521 offsetof(struct ethtool_rx_flow_key, tp);
3522 }
3523 if (v4_m_spec->tos) {
3524 match->key.ip.tos = v4_spec->tos;
3525 match->mask.ip.tos = v4_m_spec->tos;
3526 match->dissector.used_keys |=
3527 BIT(FLOW_DISSECTOR_KEY_IP);
3528 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3529 offsetof(struct ethtool_rx_flow_key, ip);
3530 }
3531 }
3532 break;
3533 case TCP_V6_FLOW:
3534 case UDP_V6_FLOW: {
3535 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3536
3537 match->key.basic.n_proto = htons(ETH_P_IPV6);
3538
3539 v6_spec = &fs->h_u.tcp_ip6_spec;
3540 v6_m_spec = &fs->m_u.tcp_ip6_spec;
8cdc3223 3541 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
eca4205f
PNA
3542 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3543 sizeof(match->key.ipv6.src));
3544 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3545 sizeof(match->mask.ipv6.src));
3546 }
8cdc3223 3547 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
eca4205f
PNA
3548 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3549 sizeof(match->key.ipv6.dst));
3550 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3551 sizeof(match->mask.ipv6.dst));
3552 }
8cdc3223
KI
3553 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3554 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
eca4205f 3555 match->dissector.used_keys |=
2b3082c6 3556 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
eca4205f
PNA
3557 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3558 offsetof(struct ethtool_rx_flow_key, ipv6);
3559 }
3560 if (v6_m_spec->psrc) {
3561 match->key.tp.src = v6_spec->psrc;
3562 match->mask.tp.src = v6_m_spec->psrc;
3563 }
3564 if (v6_m_spec->pdst) {
3565 match->key.tp.dst = v6_spec->pdst;
3566 match->mask.tp.dst = v6_m_spec->pdst;
3567 }
3568 if (v6_m_spec->psrc ||
3569 v6_m_spec->pdst) {
3570 match->dissector.used_keys |=
2b3082c6 3571 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
eca4205f
PNA
3572 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3573 offsetof(struct ethtool_rx_flow_key, tp);
3574 }
3575 if (v6_m_spec->tclass) {
3576 match->key.ip.tos = v6_spec->tclass;
3577 match->mask.ip.tos = v6_m_spec->tclass;
3578 match->dissector.used_keys |=
2b3082c6 3579 BIT_ULL(FLOW_DISSECTOR_KEY_IP);
eca4205f
PNA
3580 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3581 offsetof(struct ethtool_rx_flow_key, ip);
3582 }
3583 }
3584 break;
3585 default:
3586 ethtool_rx_flow_rule_destroy(flow);
3587 return ERR_PTR(-EINVAL);
3588 }
3589
3590 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3591 case TCP_V4_FLOW:
3592 case TCP_V6_FLOW:
3593 match->key.basic.ip_proto = IPPROTO_TCP;
d0a84e1f 3594 match->mask.basic.ip_proto = 0xff;
eca4205f
PNA
3595 break;
3596 case UDP_V4_FLOW:
3597 case UDP_V6_FLOW:
3598 match->key.basic.ip_proto = IPPROTO_UDP;
d0a84e1f 3599 match->mask.basic.ip_proto = 0xff;
eca4205f
PNA
3600 break;
3601 }
eca4205f 3602
2b3082c6 3603 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
eca4205f
PNA
3604 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3605 offsetof(struct ethtool_rx_flow_key, basic);
3606
3607 if (fs->flow_type & FLOW_EXT) {
3608 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3609 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3610
b73484b2 3611 if (ext_m_spec->vlan_etype) {
eca4205f
PNA
3612 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3613 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
b73484b2 3614 }
eca4205f 3615
b73484b2 3616 if (ext_m_spec->vlan_tci) {
eca4205f
PNA
3617 match->key.vlan.vlan_id =
3618 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3619 match->mask.vlan.vlan_id =
3620 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3621
f0d2ca15
MC
3622 match->key.vlan.vlan_dei =
3623 !!(ext_h_spec->vlan_tci & htons(0x1000));
3624 match->mask.vlan.vlan_dei =
3625 !!(ext_m_spec->vlan_tci & htons(0x1000));
3626
eca4205f
PNA
3627 match->key.vlan.vlan_priority =
3628 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3629 match->mask.vlan.vlan_priority =
3630 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
b73484b2 3631 }
eca4205f 3632
b73484b2
MC
3633 if (ext_m_spec->vlan_etype ||
3634 ext_m_spec->vlan_tci) {
eca4205f 3635 match->dissector.used_keys |=
2b3082c6 3636 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
eca4205f
PNA
3637 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3638 offsetof(struct ethtool_rx_flow_key, vlan);
3639 }
3640 }
3641 if (fs->flow_type & FLOW_MAC_EXT) {
3642 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3643 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3644
8b34ec65
NC
3645 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3646 ETH_ALEN);
3647 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3648 ETH_ALEN);
3649
3650 match->dissector.used_keys |=
2b3082c6 3651 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
8b34ec65
NC
3652 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3653 offsetof(struct ethtool_rx_flow_key, eth_addrs);
eca4205f
PNA
3654 }
3655
3656 act = &flow->rule->action.entries[0];
3657 switch (fs->ring_cookie) {
3658 case RX_CLS_FLOW_DISC:
3659 act->id = FLOW_ACTION_DROP;
3660 break;
3661 case RX_CLS_FLOW_WAKE:
3662 act->id = FLOW_ACTION_WAKE;
3663 break;
3664 default:
3665 act->id = FLOW_ACTION_QUEUE;
3666 if (fs->flow_type & FLOW_RSS)
3667 act->queue.ctx = input->rss_ctx;
3668
3669 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3670 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3671 break;
3672 }
3673
3674 return flow;
3675}
3676EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3677
3678void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3679{
3680 kfree(flow->rule);
3681 kfree(flow);
3682}
3683EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);