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