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