1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/ethtool.c - Ethtool ioctl handler
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
6 * This file is where we call all the ethtool_ops commands to get
7 * the information ethtool needs.
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/capability.h>
13 #include <linux/errno.h>
14 #include <linux/ethtool.h>
15 #include <linux/netdevice.h>
16 #include <linux/net_tstamp.h>
17 #include <linux/phy.h>
18 #include <linux/bitops.h>
19 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sfp.h>
22 #include <linux/slab.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/sched/signal.h>
25 #include <linux/net.h>
26 #include <net/devlink.h>
27 #include <net/xdp_sock.h>
28 #include <net/flow_offload.h>
33 * Some useful ethtool_ops methods that're device independent.
34 * If we find that all drivers want to do the same thing here,
35 * we can turn these into dev_() function calls.
38 u32 ethtool_op_get_link(struct net_device *dev)
40 return netif_carrier_ok(dev) ? 1 : 0;
42 EXPORT_SYMBOL(ethtool_op_get_link);
44 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
46 info->so_timestamping =
47 SOF_TIMESTAMPING_TX_SOFTWARE |
48 SOF_TIMESTAMPING_RX_SOFTWARE |
49 SOF_TIMESTAMPING_SOFTWARE;
53 EXPORT_SYMBOL(ethtool_op_get_ts_info);
55 /* Handlers for each ethtool command */
57 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
59 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
61 struct ethtool_gfeatures cmd = {
62 .cmd = ETHTOOL_GFEATURES,
63 .size = ETHTOOL_DEV_FEATURE_WORDS,
65 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
70 /* in case feature bits run out again */
71 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
73 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
74 features[i].available = (u32)(dev->hw_features >> (32 * i));
75 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
76 features[i].active = (u32)(dev->features >> (32 * i));
77 features[i].never_changed =
78 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
81 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
82 if (get_user(copy_size, sizeaddr))
85 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
86 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
88 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
90 useraddr += sizeof(cmd);
91 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
97 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
99 struct ethtool_sfeatures cmd;
100 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
101 netdev_features_t wanted = 0, valid = 0;
104 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
106 useraddr += sizeof(cmd);
108 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
111 if (copy_from_user(features, useraddr, sizeof(features)))
114 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
115 valid |= (netdev_features_t)features[i].valid << (32 * i);
116 wanted |= (netdev_features_t)features[i].requested << (32 * i);
119 if (valid & ~NETIF_F_ETHTOOL_BITS)
122 if (valid & ~dev->hw_features) {
123 valid &= dev->hw_features;
124 ret |= ETHTOOL_F_UNSUPPORTED;
127 dev->wanted_features &= ~valid;
128 dev->wanted_features |= wanted & valid;
129 __netdev_update_features(dev);
131 if ((dev->wanted_features ^ dev->features) & valid)
132 ret |= ETHTOOL_F_WISH;
137 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
139 const struct ethtool_ops *ops = dev->ethtool_ops;
141 if (sset == ETH_SS_FEATURES)
142 return ARRAY_SIZE(netdev_features_strings);
144 if (sset == ETH_SS_RSS_HASH_FUNCS)
145 return ARRAY_SIZE(rss_hash_func_strings);
147 if (sset == ETH_SS_TUNABLES)
148 return ARRAY_SIZE(tunable_strings);
150 if (sset == ETH_SS_PHY_TUNABLES)
151 return ARRAY_SIZE(phy_tunable_strings);
153 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
154 !ops->get_ethtool_phy_stats)
155 return phy_ethtool_get_sset_count(dev->phydev);
157 if (sset == ETH_SS_LINK_MODES)
158 return __ETHTOOL_LINK_MODE_MASK_NBITS;
160 if (ops->get_sset_count && ops->get_strings)
161 return ops->get_sset_count(dev, sset);
166 static void __ethtool_get_strings(struct net_device *dev,
167 u32 stringset, u8 *data)
169 const struct ethtool_ops *ops = dev->ethtool_ops;
171 if (stringset == ETH_SS_FEATURES)
172 memcpy(data, netdev_features_strings,
173 sizeof(netdev_features_strings));
174 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
175 memcpy(data, rss_hash_func_strings,
176 sizeof(rss_hash_func_strings));
177 else if (stringset == ETH_SS_TUNABLES)
178 memcpy(data, tunable_strings, sizeof(tunable_strings));
179 else if (stringset == ETH_SS_PHY_TUNABLES)
180 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
181 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
182 !ops->get_ethtool_phy_stats)
183 phy_ethtool_get_strings(dev->phydev, data);
184 else if (stringset == ETH_SS_LINK_MODES)
185 memcpy(data, link_mode_names,
186 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
188 /* ops->get_strings is valid because checked earlier */
189 ops->get_strings(dev, stringset, data);
192 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
194 /* feature masks of legacy discrete ethtool ops */
197 case ETHTOOL_GTXCSUM:
198 case ETHTOOL_STXCSUM:
199 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
200 case ETHTOOL_GRXCSUM:
201 case ETHTOOL_SRXCSUM:
202 return NETIF_F_RXCSUM;
208 return NETIF_F_ALL_TSO;
220 static int ethtool_get_one_feature(struct net_device *dev,
221 char __user *useraddr, u32 ethcmd)
223 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
224 struct ethtool_value edata = {
226 .data = !!(dev->features & mask),
229 if (copy_to_user(useraddr, &edata, sizeof(edata)))
234 static int ethtool_set_one_feature(struct net_device *dev,
235 void __user *useraddr, u32 ethcmd)
237 struct ethtool_value edata;
238 netdev_features_t mask;
240 if (copy_from_user(&edata, useraddr, sizeof(edata)))
243 mask = ethtool_get_feature_mask(ethcmd);
244 mask &= dev->hw_features;
249 dev->wanted_features |= mask;
251 dev->wanted_features &= ~mask;
253 __netdev_update_features(dev);
258 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
259 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
260 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
261 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
264 static u32 __ethtool_get_flags(struct net_device *dev)
268 if (dev->features & NETIF_F_LRO)
269 flags |= ETH_FLAG_LRO;
270 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
271 flags |= ETH_FLAG_RXVLAN;
272 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
273 flags |= ETH_FLAG_TXVLAN;
274 if (dev->features & NETIF_F_NTUPLE)
275 flags |= ETH_FLAG_NTUPLE;
276 if (dev->features & NETIF_F_RXHASH)
277 flags |= ETH_FLAG_RXHASH;
282 static int __ethtool_set_flags(struct net_device *dev, u32 data)
284 netdev_features_t features = 0, changed;
286 if (data & ~ETH_ALL_FLAGS)
289 if (data & ETH_FLAG_LRO)
290 features |= NETIF_F_LRO;
291 if (data & ETH_FLAG_RXVLAN)
292 features |= NETIF_F_HW_VLAN_CTAG_RX;
293 if (data & ETH_FLAG_TXVLAN)
294 features |= NETIF_F_HW_VLAN_CTAG_TX;
295 if (data & ETH_FLAG_NTUPLE)
296 features |= NETIF_F_NTUPLE;
297 if (data & ETH_FLAG_RXHASH)
298 features |= NETIF_F_RXHASH;
300 /* allow changing only bits set in hw_features */
301 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
302 if (changed & ~dev->hw_features)
303 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
305 dev->wanted_features =
306 (dev->wanted_features & ~changed) | (features & changed);
308 __netdev_update_features(dev);
313 /* Given two link masks, AND them together and save the result in dst. */
314 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
315 struct ethtool_link_ksettings *src)
317 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
318 unsigned int idx = 0;
320 for (; idx < size; idx++) {
321 dst->link_modes.supported[idx] &=
322 src->link_modes.supported[idx];
323 dst->link_modes.advertising[idx] &=
324 src->link_modes.advertising[idx];
327 EXPORT_SYMBOL(ethtool_intersect_link_masks);
329 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
332 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
335 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
337 /* return false if src had higher bits set. lower bits always updated. */
338 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
339 const unsigned long *src)
343 /* TODO: following test will soon always be true */
344 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
345 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
347 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
348 bitmap_fill(ext, 32);
349 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
350 if (bitmap_intersects(ext, src,
351 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
352 /* src mask goes beyond bit 31 */
356 *legacy_u32 = src[0];
359 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
361 /* return false if legacy contained non-0 deprecated fields
362 * maxtxpkt/maxrxpkt. rest of ksettings always updated
365 convert_legacy_settings_to_link_ksettings(
366 struct ethtool_link_ksettings *link_ksettings,
367 const struct ethtool_cmd *legacy_settings)
371 memset(link_ksettings, 0, sizeof(*link_ksettings));
373 /* This is used to tell users that driver is still using these
374 * deprecated legacy fields, and they should not use
375 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
377 if (legacy_settings->maxtxpkt ||
378 legacy_settings->maxrxpkt)
381 ethtool_convert_legacy_u32_to_link_mode(
382 link_ksettings->link_modes.supported,
383 legacy_settings->supported);
384 ethtool_convert_legacy_u32_to_link_mode(
385 link_ksettings->link_modes.advertising,
386 legacy_settings->advertising);
387 ethtool_convert_legacy_u32_to_link_mode(
388 link_ksettings->link_modes.lp_advertising,
389 legacy_settings->lp_advertising);
390 link_ksettings->base.speed
391 = ethtool_cmd_speed(legacy_settings);
392 link_ksettings->base.duplex
393 = legacy_settings->duplex;
394 link_ksettings->base.port
395 = legacy_settings->port;
396 link_ksettings->base.phy_address
397 = legacy_settings->phy_address;
398 link_ksettings->base.autoneg
399 = legacy_settings->autoneg;
400 link_ksettings->base.mdio_support
401 = legacy_settings->mdio_support;
402 link_ksettings->base.eth_tp_mdix
403 = legacy_settings->eth_tp_mdix;
404 link_ksettings->base.eth_tp_mdix_ctrl
405 = legacy_settings->eth_tp_mdix_ctrl;
409 /* return false if ksettings link modes had higher bits
410 * set. legacy_settings always updated (best effort)
413 convert_link_ksettings_to_legacy_settings(
414 struct ethtool_cmd *legacy_settings,
415 const struct ethtool_link_ksettings *link_ksettings)
419 memset(legacy_settings, 0, sizeof(*legacy_settings));
420 /* this also clears the deprecated fields in legacy structure:
426 retval &= ethtool_convert_link_mode_to_legacy_u32(
427 &legacy_settings->supported,
428 link_ksettings->link_modes.supported);
429 retval &= ethtool_convert_link_mode_to_legacy_u32(
430 &legacy_settings->advertising,
431 link_ksettings->link_modes.advertising);
432 retval &= ethtool_convert_link_mode_to_legacy_u32(
433 &legacy_settings->lp_advertising,
434 link_ksettings->link_modes.lp_advertising);
435 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
436 legacy_settings->duplex
437 = link_ksettings->base.duplex;
438 legacy_settings->port
439 = link_ksettings->base.port;
440 legacy_settings->phy_address
441 = link_ksettings->base.phy_address;
442 legacy_settings->autoneg
443 = link_ksettings->base.autoneg;
444 legacy_settings->mdio_support
445 = link_ksettings->base.mdio_support;
446 legacy_settings->eth_tp_mdix
447 = link_ksettings->base.eth_tp_mdix;
448 legacy_settings->eth_tp_mdix_ctrl
449 = link_ksettings->base.eth_tp_mdix_ctrl;
450 legacy_settings->transceiver
451 = link_ksettings->base.transceiver;
455 /* number of 32-bit words to store the user's link mode bitmaps */
456 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
457 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
459 /* layout of the struct passed from/to userland */
460 struct ethtool_link_usettings {
461 struct ethtool_link_settings base;
463 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
464 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
465 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
469 /* Internal kernel helper to query a device ethtool_link_settings. */
470 int __ethtool_get_link_ksettings(struct net_device *dev,
471 struct ethtool_link_ksettings *link_ksettings)
475 if (!dev->ethtool_ops->get_link_ksettings)
478 memset(link_ksettings, 0, sizeof(*link_ksettings));
479 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
481 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
483 /* convert ethtool_link_usettings in user space to a kernel internal
484 * ethtool_link_ksettings. return 0 on success, errno on error.
486 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
487 const void __user *from)
489 struct ethtool_link_usettings link_usettings;
491 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
494 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
495 bitmap_from_arr32(to->link_modes.supported,
496 link_usettings.link_modes.supported,
497 __ETHTOOL_LINK_MODE_MASK_NBITS);
498 bitmap_from_arr32(to->link_modes.advertising,
499 link_usettings.link_modes.advertising,
500 __ETHTOOL_LINK_MODE_MASK_NBITS);
501 bitmap_from_arr32(to->link_modes.lp_advertising,
502 link_usettings.link_modes.lp_advertising,
503 __ETHTOOL_LINK_MODE_MASK_NBITS);
508 /* convert a kernel internal ethtool_link_ksettings to
509 * ethtool_link_usettings in user space. return 0 on success, errno on
513 store_link_ksettings_for_user(void __user *to,
514 const struct ethtool_link_ksettings *from)
516 struct ethtool_link_usettings link_usettings;
518 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
519 bitmap_to_arr32(link_usettings.link_modes.supported,
520 from->link_modes.supported,
521 __ETHTOOL_LINK_MODE_MASK_NBITS);
522 bitmap_to_arr32(link_usettings.link_modes.advertising,
523 from->link_modes.advertising,
524 __ETHTOOL_LINK_MODE_MASK_NBITS);
525 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
526 from->link_modes.lp_advertising,
527 __ETHTOOL_LINK_MODE_MASK_NBITS);
529 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
535 /* Query device for its ethtool_link_settings. */
536 static int ethtool_get_link_ksettings(struct net_device *dev,
537 void __user *useraddr)
540 struct ethtool_link_ksettings link_ksettings;
543 if (!dev->ethtool_ops->get_link_ksettings)
546 /* handle bitmap nbits handshake */
547 if (copy_from_user(&link_ksettings.base, useraddr,
548 sizeof(link_ksettings.base)))
551 if (__ETHTOOL_LINK_MODE_MASK_NU32
552 != link_ksettings.base.link_mode_masks_nwords) {
553 /* wrong link mode nbits requested */
554 memset(&link_ksettings, 0, sizeof(link_ksettings));
555 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
556 /* send back number of words required as negative val */
557 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
558 "need too many bits for link modes!");
559 link_ksettings.base.link_mode_masks_nwords
560 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
562 /* copy the base fields back to user, not the link
565 if (copy_to_user(useraddr, &link_ksettings.base,
566 sizeof(link_ksettings.base)))
572 /* handshake successful: user/kernel agree on
573 * link_mode_masks_nwords
576 memset(&link_ksettings, 0, sizeof(link_ksettings));
577 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
581 /* make sure we tell the right values to user */
582 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
583 link_ksettings.base.link_mode_masks_nwords
584 = __ETHTOOL_LINK_MODE_MASK_NU32;
586 return store_link_ksettings_for_user(useraddr, &link_ksettings);
589 /* Update device ethtool_link_settings. */
590 static int ethtool_set_link_ksettings(struct net_device *dev,
591 void __user *useraddr)
594 struct ethtool_link_ksettings link_ksettings;
598 if (!dev->ethtool_ops->set_link_ksettings)
601 /* make sure nbits field has expected value */
602 if (copy_from_user(&link_ksettings.base, useraddr,
603 sizeof(link_ksettings.base)))
606 if (__ETHTOOL_LINK_MODE_MASK_NU32
607 != link_ksettings.base.link_mode_masks_nwords)
610 /* copy the whole structure, now that we know it has expected
613 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
617 /* re-check nwords field, just in case */
618 if (__ETHTOOL_LINK_MODE_MASK_NU32
619 != link_ksettings.base.link_mode_masks_nwords)
622 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
625 /* Query device for its ethtool_cmd settings.
627 * Backward compatibility note: for compatibility with legacy ethtool, this is
628 * now implemented via get_link_ksettings. When driver reports higher link mode
629 * bits, a kernel warning is logged once (with name of 1st driver/device) to
630 * recommend user to upgrade ethtool, but the command is successful (only the
631 * lower link mode bits reported back to user). Deprecated fields from
632 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
634 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
636 struct ethtool_link_ksettings link_ksettings;
637 struct ethtool_cmd cmd;
641 if (!dev->ethtool_ops->get_link_ksettings)
644 memset(&link_ksettings, 0, sizeof(link_ksettings));
645 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
648 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
650 /* send a sensible cmd tag back to user */
651 cmd.cmd = ETHTOOL_GSET;
653 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
659 /* Update device link settings with given ethtool_cmd.
661 * Backward compatibility note: for compatibility with legacy ethtool, this is
662 * now always implemented via set_link_settings. When user's request updates
663 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
664 * warning is logged once (with name of 1st driver/device) to recommend user to
665 * upgrade ethtool, and the request is rejected.
667 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
669 struct ethtool_link_ksettings link_ksettings;
670 struct ethtool_cmd cmd;
674 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
676 if (!dev->ethtool_ops->set_link_ksettings)
679 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
681 link_ksettings.base.link_mode_masks_nwords =
682 __ETHTOOL_LINK_MODE_MASK_NU32;
683 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
686 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
687 void __user *useraddr)
689 struct ethtool_drvinfo info;
690 const struct ethtool_ops *ops = dev->ethtool_ops;
692 memset(&info, 0, sizeof(info));
693 info.cmd = ETHTOOL_GDRVINFO;
694 if (ops->get_drvinfo) {
695 ops->get_drvinfo(dev, &info);
696 } else if (dev->dev.parent && dev->dev.parent->driver) {
697 strlcpy(info.bus_info, dev_name(dev->dev.parent),
698 sizeof(info.bus_info));
699 strlcpy(info.driver, dev->dev.parent->driver->name,
700 sizeof(info.driver));
706 * this method of obtaining string set info is deprecated;
707 * Use ETHTOOL_GSSET_INFO instead.
709 if (ops->get_sset_count) {
712 rc = ops->get_sset_count(dev, ETH_SS_TEST);
714 info.testinfo_len = rc;
715 rc = ops->get_sset_count(dev, ETH_SS_STATS);
718 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
720 info.n_priv_flags = rc;
722 if (ops->get_regs_len) {
723 int ret = ops->get_regs_len(dev);
726 info.regdump_len = ret;
729 if (ops->get_eeprom_len)
730 info.eedump_len = ops->get_eeprom_len(dev);
732 if (!info.fw_version[0])
733 devlink_compat_running_version(dev, info.fw_version,
734 sizeof(info.fw_version));
736 if (copy_to_user(useraddr, &info, sizeof(info)))
741 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
742 void __user *useraddr)
744 struct ethtool_sset_info info;
746 int i, idx = 0, n_bits = 0, ret, rc;
747 u32 *info_buf = NULL;
749 if (copy_from_user(&info, useraddr, sizeof(info)))
752 /* store copy of mask, because we zero struct later on */
753 sset_mask = info.sset_mask;
757 /* calculate size of return buffer */
758 n_bits = hweight64(sset_mask);
760 memset(&info, 0, sizeof(info));
761 info.cmd = ETHTOOL_GSSET_INFO;
763 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
768 * fill return buffer based on input bitmask and successful
769 * get_sset_count return
771 for (i = 0; i < 64; i++) {
772 if (!(sset_mask & (1ULL << i)))
775 rc = __ethtool_get_sset_count(dev, i);
777 info.sset_mask |= (1ULL << i);
778 info_buf[idx++] = rc;
783 if (copy_to_user(useraddr, &info, sizeof(info)))
786 useraddr += offsetof(struct ethtool_sset_info, data);
787 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
797 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
798 u32 cmd, void __user *useraddr)
800 struct ethtool_rxnfc info;
801 size_t info_size = sizeof(info);
804 if (!dev->ethtool_ops->set_rxnfc)
807 /* struct ethtool_rxnfc was originally defined for
808 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
809 * members. User-space might still be using that
811 if (cmd == ETHTOOL_SRXFH)
812 info_size = (offsetof(struct ethtool_rxnfc, data) +
815 if (copy_from_user(&info, useraddr, info_size))
818 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
822 if (cmd == ETHTOOL_SRXCLSRLINS &&
823 copy_to_user(useraddr, &info, info_size))
829 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
830 u32 cmd, void __user *useraddr)
832 struct ethtool_rxnfc info;
833 size_t info_size = sizeof(info);
834 const struct ethtool_ops *ops = dev->ethtool_ops;
836 void *rule_buf = NULL;
841 /* struct ethtool_rxnfc was originally defined for
842 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
843 * members. User-space might still be using that
845 if (cmd == ETHTOOL_GRXFH)
846 info_size = (offsetof(struct ethtool_rxnfc, data) +
849 if (copy_from_user(&info, useraddr, info_size))
852 /* If FLOW_RSS was requested then user-space must be using the
853 * new definition, as FLOW_RSS is newer.
855 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
856 info_size = sizeof(info);
857 if (copy_from_user(&info, useraddr, info_size))
859 /* Since malicious users may modify the original data,
860 * we need to check whether FLOW_RSS is still requested.
862 if (!(info.flow_type & FLOW_RSS))
869 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
870 if (info.rule_cnt > 0) {
871 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
872 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
879 ret = ops->get_rxnfc(dev, &info, rule_buf);
884 if (copy_to_user(useraddr, &info, info_size))
888 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
889 if (copy_to_user(useraddr, rule_buf,
890 info.rule_cnt * sizeof(u32)))
901 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
902 struct ethtool_rxnfc *rx_rings,
907 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
910 /* Validate ring indices */
911 for (i = 0; i < size; i++)
912 if (indir[i] >= rx_rings->data)
918 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
920 void netdev_rss_key_fill(void *buffer, size_t len)
922 BUG_ON(len > sizeof(netdev_rss_key));
923 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
924 memcpy(buffer, netdev_rss_key, len);
926 EXPORT_SYMBOL(netdev_rss_key_fill);
928 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
930 u32 dev_size, current_max = 0;
934 if (!dev->ethtool_ops->get_rxfh_indir_size ||
935 !dev->ethtool_ops->get_rxfh)
937 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
941 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
945 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
950 current_max = max(current_max, indir[dev_size]);
959 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
960 void __user *useraddr)
962 u32 user_size, dev_size;
966 if (!dev->ethtool_ops->get_rxfh_indir_size ||
967 !dev->ethtool_ops->get_rxfh)
969 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
973 if (copy_from_user(&user_size,
974 useraddr + offsetof(struct ethtool_rxfh_indir, size),
978 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
979 &dev_size, sizeof(dev_size)))
982 /* If the user buffer size is 0, this is just a query for the
983 * device table size. Otherwise, if it's smaller than the
984 * device table size it's an error.
986 if (user_size < dev_size)
987 return user_size == 0 ? 0 : -EINVAL;
989 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
993 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
997 if (copy_to_user(useraddr +
998 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
999 indir, dev_size * sizeof(indir[0])))
1007 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1008 void __user *useraddr)
1010 struct ethtool_rxnfc rx_rings;
1011 u32 user_size, dev_size, i;
1013 const struct ethtool_ops *ops = dev->ethtool_ops;
1015 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1017 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1021 dev_size = ops->get_rxfh_indir_size(dev);
1025 if (copy_from_user(&user_size,
1026 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1030 if (user_size != 0 && user_size != dev_size)
1033 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1037 rx_rings.cmd = ETHTOOL_GRXRINGS;
1038 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1042 if (user_size == 0) {
1043 for (i = 0; i < dev_size; i++)
1044 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1046 ret = ethtool_copy_validate_indir(indir,
1047 useraddr + ringidx_offset,
1054 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1058 /* indicate whether rxfh was set to default */
1060 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1062 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1069 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1070 void __user *useraddr)
1073 const struct ethtool_ops *ops = dev->ethtool_ops;
1074 u32 user_indir_size, user_key_size;
1075 u32 dev_indir_size = 0, dev_key_size = 0;
1076 struct ethtool_rxfh rxfh;
1087 if (ops->get_rxfh_indir_size)
1088 dev_indir_size = ops->get_rxfh_indir_size(dev);
1089 if (ops->get_rxfh_key_size)
1090 dev_key_size = ops->get_rxfh_key_size(dev);
1092 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1094 user_indir_size = rxfh.indir_size;
1095 user_key_size = rxfh.key_size;
1097 /* Check that reserved fields are 0 for now */
1098 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1100 /* Most drivers don't handle rss_context, check it's 0 as well */
1101 if (rxfh.rss_context && !ops->get_rxfh_context)
1104 rxfh.indir_size = dev_indir_size;
1105 rxfh.key_size = dev_key_size;
1106 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1109 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1110 (user_key_size && (user_key_size != dev_key_size)))
1113 indir_bytes = user_indir_size * sizeof(indir[0]);
1114 total_size = indir_bytes + user_key_size;
1115 rss_config = kzalloc(total_size, GFP_USER);
1119 if (user_indir_size)
1120 indir = (u32 *)rss_config;
1123 hkey = rss_config + indir_bytes;
1125 if (rxfh.rss_context)
1126 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1130 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1134 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1135 &dev_hfunc, sizeof(rxfh.hfunc))) {
1137 } else if (copy_to_user(useraddr +
1138 offsetof(struct ethtool_rxfh, rss_config[0]),
1139 rss_config, total_size)) {
1148 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1149 void __user *useraddr)
1152 const struct ethtool_ops *ops = dev->ethtool_ops;
1153 struct ethtool_rxnfc rx_rings;
1154 struct ethtool_rxfh rxfh;
1155 u32 dev_indir_size = 0, dev_key_size = 0, i;
1156 u32 *indir = NULL, indir_bytes = 0;
1159 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1160 bool delete = false;
1162 if (!ops->get_rxnfc || !ops->set_rxfh)
1165 if (ops->get_rxfh_indir_size)
1166 dev_indir_size = ops->get_rxfh_indir_size(dev);
1167 if (ops->get_rxfh_key_size)
1168 dev_key_size = ops->get_rxfh_key_size(dev);
1170 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1173 /* Check that reserved fields are 0 for now */
1174 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1176 /* Most drivers don't handle rss_context, check it's 0 as well */
1177 if (rxfh.rss_context && !ops->set_rxfh_context)
1180 /* If either indir, hash key or function is valid, proceed further.
1181 * Must request at least one change: indir size, hash key or function.
1183 if ((rxfh.indir_size &&
1184 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1185 rxfh.indir_size != dev_indir_size) ||
1186 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1187 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1188 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1191 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1192 indir_bytes = dev_indir_size * sizeof(indir[0]);
1194 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1198 rx_rings.cmd = ETHTOOL_GRXRINGS;
1199 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1203 /* rxfh.indir_size == 0 means reset the indir table to default (master
1204 * context) or delete the context (other RSS contexts).
1205 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1207 if (rxfh.indir_size &&
1208 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1209 indir = (u32 *)rss_config;
1210 ret = ethtool_copy_validate_indir(indir,
1211 useraddr + rss_cfg_offset,
1216 } else if (rxfh.indir_size == 0) {
1217 if (rxfh.rss_context == 0) {
1218 indir = (u32 *)rss_config;
1219 for (i = 0; i < dev_indir_size; i++)
1220 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1226 if (rxfh.key_size) {
1227 hkey = rss_config + indir_bytes;
1228 if (copy_from_user(hkey,
1229 useraddr + rss_cfg_offset + indir_bytes,
1236 if (rxfh.rss_context)
1237 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1238 &rxfh.rss_context, delete);
1240 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1244 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1245 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1248 if (!rxfh.rss_context) {
1249 /* indicate whether rxfh was set to default */
1250 if (rxfh.indir_size == 0)
1251 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1252 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1253 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1261 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1263 struct ethtool_regs regs;
1264 const struct ethtool_ops *ops = dev->ethtool_ops;
1268 if (!ops->get_regs || !ops->get_regs_len)
1271 if (copy_from_user(®s, useraddr, sizeof(regs)))
1274 reglen = ops->get_regs_len(dev);
1278 if (regs.len > reglen)
1281 regbuf = vzalloc(reglen);
1285 if (regs.len < reglen)
1288 ops->get_regs(dev, ®s, regbuf);
1291 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1293 useraddr += offsetof(struct ethtool_regs, data);
1294 if (copy_to_user(useraddr, regbuf, reglen))
1303 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1305 struct ethtool_value reset;
1308 if (!dev->ethtool_ops->reset)
1311 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1314 ret = dev->ethtool_ops->reset(dev, &reset.data);
1318 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1323 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1325 struct ethtool_wolinfo wol;
1327 if (!dev->ethtool_ops->get_wol)
1330 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1331 wol.cmd = ETHTOOL_GWOL;
1332 dev->ethtool_ops->get_wol(dev, &wol);
1334 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1339 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1341 struct ethtool_wolinfo wol;
1344 if (!dev->ethtool_ops->set_wol)
1347 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1350 ret = dev->ethtool_ops->set_wol(dev, &wol);
1354 dev->wol_enabled = !!wol.wolopts;
1359 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1361 struct ethtool_eee edata;
1364 if (!dev->ethtool_ops->get_eee)
1367 memset(&edata, 0, sizeof(struct ethtool_eee));
1368 edata.cmd = ETHTOOL_GEEE;
1369 rc = dev->ethtool_ops->get_eee(dev, &edata);
1374 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1380 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1382 struct ethtool_eee edata;
1384 if (!dev->ethtool_ops->set_eee)
1387 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1390 return dev->ethtool_ops->set_eee(dev, &edata);
1393 static int ethtool_nway_reset(struct net_device *dev)
1395 if (!dev->ethtool_ops->nway_reset)
1398 return dev->ethtool_ops->nway_reset(dev);
1401 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1403 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1405 if (!dev->ethtool_ops->get_link)
1408 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1410 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1415 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1416 int (*getter)(struct net_device *,
1417 struct ethtool_eeprom *, u8 *),
1420 struct ethtool_eeprom eeprom;
1421 void __user *userbuf = useraddr + sizeof(eeprom);
1422 u32 bytes_remaining;
1426 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1429 /* Check for wrap and zero */
1430 if (eeprom.offset + eeprom.len <= eeprom.offset)
1433 /* Check for exceeding total eeprom len */
1434 if (eeprom.offset + eeprom.len > total_len)
1437 data = kmalloc(PAGE_SIZE, GFP_USER);
1441 bytes_remaining = eeprom.len;
1442 while (bytes_remaining > 0) {
1443 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1445 ret = getter(dev, &eeprom, data);
1448 if (copy_to_user(userbuf, data, eeprom.len)) {
1452 userbuf += eeprom.len;
1453 eeprom.offset += eeprom.len;
1454 bytes_remaining -= eeprom.len;
1457 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1458 eeprom.offset -= eeprom.len;
1459 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1466 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1468 const struct ethtool_ops *ops = dev->ethtool_ops;
1470 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1471 !ops->get_eeprom_len(dev))
1474 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1475 ops->get_eeprom_len(dev));
1478 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1480 struct ethtool_eeprom eeprom;
1481 const struct ethtool_ops *ops = dev->ethtool_ops;
1482 void __user *userbuf = useraddr + sizeof(eeprom);
1483 u32 bytes_remaining;
1487 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1488 !ops->get_eeprom_len(dev))
1491 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1494 /* Check for wrap and zero */
1495 if (eeprom.offset + eeprom.len <= eeprom.offset)
1498 /* Check for exceeding total eeprom len */
1499 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1502 data = kmalloc(PAGE_SIZE, GFP_USER);
1506 bytes_remaining = eeprom.len;
1507 while (bytes_remaining > 0) {
1508 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1510 if (copy_from_user(data, userbuf, eeprom.len)) {
1514 ret = ops->set_eeprom(dev, &eeprom, data);
1517 userbuf += eeprom.len;
1518 eeprom.offset += eeprom.len;
1519 bytes_remaining -= eeprom.len;
1526 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1527 void __user *useraddr)
1529 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1531 if (!dev->ethtool_ops->get_coalesce)
1534 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1536 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1541 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1542 void __user *useraddr)
1544 struct ethtool_coalesce coalesce;
1546 if (!dev->ethtool_ops->set_coalesce)
1549 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1552 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1555 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1557 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1559 if (!dev->ethtool_ops->get_ringparam)
1562 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1564 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1569 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1571 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1573 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1576 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1579 dev->ethtool_ops->get_ringparam(dev, &max);
1581 /* ensure new ring parameters are within the maximums */
1582 if (ringparam.rx_pending > max.rx_max_pending ||
1583 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1584 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1585 ringparam.tx_pending > max.tx_max_pending)
1588 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1591 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1592 void __user *useraddr)
1594 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1596 if (!dev->ethtool_ops->get_channels)
1599 dev->ethtool_ops->get_channels(dev, &channels);
1601 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1606 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1607 void __user *useraddr)
1609 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1610 u16 from_channel, to_channel;
1611 u32 max_rx_in_use = 0;
1614 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1617 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1620 dev->ethtool_ops->get_channels(dev, &curr);
1622 /* ensure new counts are within the maximums */
1623 if (channels.rx_count > curr.max_rx ||
1624 channels.tx_count > curr.max_tx ||
1625 channels.combined_count > curr.max_combined ||
1626 channels.other_count > curr.max_other)
1629 /* ensure the new Rx count fits within the configured Rx flow
1630 * indirection table settings */
1631 if (netif_is_rxfh_configured(dev) &&
1632 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1633 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1636 /* Disabling channels, query zero-copy AF_XDP sockets */
1637 from_channel = channels.combined_count +
1638 min(channels.rx_count, channels.tx_count);
1639 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1640 for (i = from_channel; i < to_channel; i++)
1641 if (xdp_get_umem_from_qid(dev, i))
1644 return dev->ethtool_ops->set_channels(dev, &channels);
1647 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1649 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1651 if (!dev->ethtool_ops->get_pauseparam)
1654 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1656 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1661 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1663 struct ethtool_pauseparam pauseparam;
1665 if (!dev->ethtool_ops->set_pauseparam)
1668 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1671 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1674 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1676 struct ethtool_test test;
1677 const struct ethtool_ops *ops = dev->ethtool_ops;
1681 if (!ops->self_test || !ops->get_sset_count)
1684 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1687 WARN_ON(test_len == 0);
1689 if (copy_from_user(&test, useraddr, sizeof(test)))
1692 test.len = test_len;
1693 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1697 ops->self_test(dev, &test, data);
1700 if (copy_to_user(useraddr, &test, sizeof(test)))
1702 useraddr += sizeof(test);
1703 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1712 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1714 struct ethtool_gstrings gstrings;
1718 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1721 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1724 if (ret > S32_MAX / ETH_GSTRING_LEN)
1731 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1735 __ethtool_get_strings(dev, gstrings.string_set, data);
1741 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1743 useraddr += sizeof(gstrings);
1745 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1754 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1756 struct ethtool_value id;
1758 const struct ethtool_ops *ops = dev->ethtool_ops;
1761 if (!ops->set_phys_id)
1767 if (copy_from_user(&id, useraddr, sizeof(id)))
1770 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1774 /* Drop the RTNL lock while waiting, but prevent reentry or
1775 * removal of the device.
1782 /* Driver will handle this itself */
1783 schedule_timeout_interruptible(
1784 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1786 /* Driver expects to be called at twice the frequency in rc */
1787 int n = rc * 2, i, interval = HZ / n;
1789 /* Count down seconds */
1791 /* Count down iterations per second */
1795 rc = ops->set_phys_id(dev,
1796 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1800 schedule_timeout_interruptible(interval);
1801 } while (!signal_pending(current) && --i != 0);
1802 } while (!signal_pending(current) &&
1803 (id.data == 0 || --id.data != 0));
1810 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1814 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1816 struct ethtool_stats stats;
1817 const struct ethtool_ops *ops = dev->ethtool_ops;
1821 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1824 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1827 if (n_stats > S32_MAX / sizeof(u64))
1829 WARN_ON_ONCE(!n_stats);
1830 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1833 stats.n_stats = n_stats;
1836 data = vzalloc(array_size(n_stats, sizeof(u64)));
1839 ops->get_ethtool_stats(dev, &stats, data);
1845 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1847 useraddr += sizeof(stats);
1848 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1857 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1859 const struct ethtool_ops *ops = dev->ethtool_ops;
1860 struct phy_device *phydev = dev->phydev;
1861 struct ethtool_stats stats;
1865 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1868 if (dev->phydev && !ops->get_ethtool_phy_stats)
1869 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1871 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1874 if (n_stats > S32_MAX / sizeof(u64))
1876 WARN_ON_ONCE(!n_stats);
1878 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1881 stats.n_stats = n_stats;
1884 data = vzalloc(array_size(n_stats, sizeof(u64)));
1888 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1889 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1893 ops->get_ethtool_phy_stats(dev, &stats, data);
1900 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1902 useraddr += sizeof(stats);
1903 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1912 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1914 struct ethtool_perm_addr epaddr;
1916 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1919 if (epaddr.size < dev->addr_len)
1921 epaddr.size = dev->addr_len;
1923 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1925 useraddr += sizeof(epaddr);
1926 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1931 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1932 u32 cmd, u32 (*actor)(struct net_device *))
1934 struct ethtool_value edata = { .cmd = cmd };
1939 edata.data = actor(dev);
1941 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1946 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1947 void (*actor)(struct net_device *, u32))
1949 struct ethtool_value edata;
1954 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1957 actor(dev, edata.data);
1961 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1962 int (*actor)(struct net_device *, u32))
1964 struct ethtool_value edata;
1969 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1972 return actor(dev, edata.data);
1975 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1976 char __user *useraddr)
1978 struct ethtool_flash efl;
1980 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1982 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1984 if (!dev->ethtool_ops->flash_device)
1985 return devlink_compat_flash_update(dev, efl.data);
1987 return dev->ethtool_ops->flash_device(dev, &efl);
1990 static int ethtool_set_dump(struct net_device *dev,
1991 void __user *useraddr)
1993 struct ethtool_dump dump;
1995 if (!dev->ethtool_ops->set_dump)
1998 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2001 return dev->ethtool_ops->set_dump(dev, &dump);
2004 static int ethtool_get_dump_flag(struct net_device *dev,
2005 void __user *useraddr)
2008 struct ethtool_dump dump;
2009 const struct ethtool_ops *ops = dev->ethtool_ops;
2011 if (!ops->get_dump_flag)
2014 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2017 ret = ops->get_dump_flag(dev, &dump);
2021 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2026 static int ethtool_get_dump_data(struct net_device *dev,
2027 void __user *useraddr)
2031 struct ethtool_dump dump, tmp;
2032 const struct ethtool_ops *ops = dev->ethtool_ops;
2035 if (!ops->get_dump_data || !ops->get_dump_flag)
2038 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2041 memset(&tmp, 0, sizeof(tmp));
2042 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2043 ret = ops->get_dump_flag(dev, &tmp);
2047 len = min(tmp.len, dump.len);
2051 /* Don't ever let the driver think there's more space available
2052 * than it requested with .get_dump_flag().
2056 /* Always allocate enough space to hold the whole thing so that the
2057 * driver does not need to check the length and bother with partial
2060 data = vzalloc(tmp.len);
2063 ret = ops->get_dump_data(dev, &dump, data);
2067 /* There are two sane possibilities:
2068 * 1. The driver's .get_dump_data() does not touch dump.len.
2069 * 2. Or it may set dump.len to how much it really writes, which
2070 * should be tmp.len (or len if it can do a partial dump).
2071 * In any case respond to userspace with the actual length of data
2074 WARN_ON(dump.len != len && dump.len != tmp.len);
2077 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2081 useraddr += offsetof(struct ethtool_dump, data);
2082 if (copy_to_user(useraddr, data, len))
2089 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2092 struct ethtool_ts_info info;
2093 const struct ethtool_ops *ops = dev->ethtool_ops;
2094 struct phy_device *phydev = dev->phydev;
2096 memset(&info, 0, sizeof(info));
2097 info.cmd = ETHTOOL_GET_TS_INFO;
2099 if (phy_has_tsinfo(phydev)) {
2100 err = phy_ts_info(phydev, &info);
2101 } else if (ops->get_ts_info) {
2102 err = ops->get_ts_info(dev, &info);
2104 info.so_timestamping =
2105 SOF_TIMESTAMPING_RX_SOFTWARE |
2106 SOF_TIMESTAMPING_SOFTWARE;
2107 info.phc_index = -1;
2113 if (copy_to_user(useraddr, &info, sizeof(info)))
2119 static int __ethtool_get_module_info(struct net_device *dev,
2120 struct ethtool_modinfo *modinfo)
2122 const struct ethtool_ops *ops = dev->ethtool_ops;
2123 struct phy_device *phydev = dev->phydev;
2126 return sfp_get_module_info(dev->sfp_bus, modinfo);
2128 if (phydev && phydev->drv && phydev->drv->module_info)
2129 return phydev->drv->module_info(phydev, modinfo);
2131 if (ops->get_module_info)
2132 return ops->get_module_info(dev, modinfo);
2137 static int ethtool_get_module_info(struct net_device *dev,
2138 void __user *useraddr)
2141 struct ethtool_modinfo modinfo;
2143 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2146 ret = __ethtool_get_module_info(dev, &modinfo);
2150 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2156 static int __ethtool_get_module_eeprom(struct net_device *dev,
2157 struct ethtool_eeprom *ee, u8 *data)
2159 const struct ethtool_ops *ops = dev->ethtool_ops;
2160 struct phy_device *phydev = dev->phydev;
2163 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2165 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2166 return phydev->drv->module_eeprom(phydev, ee, data);
2168 if (ops->get_module_eeprom)
2169 return ops->get_module_eeprom(dev, ee, data);
2174 static int ethtool_get_module_eeprom(struct net_device *dev,
2175 void __user *useraddr)
2178 struct ethtool_modinfo modinfo;
2180 ret = __ethtool_get_module_info(dev, &modinfo);
2184 return ethtool_get_any_eeprom(dev, useraddr,
2185 __ethtool_get_module_eeprom,
2186 modinfo.eeprom_len);
2189 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2192 case ETHTOOL_RX_COPYBREAK:
2193 case ETHTOOL_TX_COPYBREAK:
2194 if (tuna->len != sizeof(u32) ||
2195 tuna->type_id != ETHTOOL_TUNABLE_U32)
2198 case ETHTOOL_PFC_PREVENTION_TOUT:
2199 if (tuna->len != sizeof(u16) ||
2200 tuna->type_id != ETHTOOL_TUNABLE_U16)
2210 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2213 struct ethtool_tunable tuna;
2214 const struct ethtool_ops *ops = dev->ethtool_ops;
2217 if (!ops->get_tunable)
2219 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2221 ret = ethtool_tunable_valid(&tuna);
2224 data = kmalloc(tuna.len, GFP_USER);
2227 ret = ops->get_tunable(dev, &tuna, data);
2230 useraddr += sizeof(tuna);
2232 if (copy_to_user(useraddr, data, tuna.len))
2241 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2244 struct ethtool_tunable tuna;
2245 const struct ethtool_ops *ops = dev->ethtool_ops;
2248 if (!ops->set_tunable)
2250 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2252 ret = ethtool_tunable_valid(&tuna);
2255 useraddr += sizeof(tuna);
2256 data = memdup_user(useraddr, tuna.len);
2258 return PTR_ERR(data);
2259 ret = ops->set_tunable(dev, &tuna, data);
2265 static noinline_for_stack int
2266 ethtool_get_per_queue_coalesce(struct net_device *dev,
2267 void __user *useraddr,
2268 struct ethtool_per_queue_op *per_queue_opt)
2272 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2274 if (!dev->ethtool_ops->get_per_queue_coalesce)
2277 useraddr += sizeof(*per_queue_opt);
2279 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2282 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2283 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2285 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2288 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2290 useraddr += sizeof(coalesce);
2296 static noinline_for_stack int
2297 ethtool_set_per_queue_coalesce(struct net_device *dev,
2298 void __user *useraddr,
2299 struct ethtool_per_queue_op *per_queue_opt)
2304 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2305 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2307 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2308 (!dev->ethtool_ops->get_per_queue_coalesce))
2311 useraddr += sizeof(*per_queue_opt);
2313 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2314 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2315 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2319 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2320 struct ethtool_coalesce coalesce;
2322 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2328 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2333 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2337 useraddr += sizeof(coalesce);
2343 for_each_set_bit(i, queue_mask, bit) {
2344 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2353 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2354 void __user *useraddr, u32 sub_cmd)
2356 struct ethtool_per_queue_op per_queue_opt;
2358 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2361 if (per_queue_opt.sub_command != sub_cmd)
2364 switch (per_queue_opt.sub_command) {
2365 case ETHTOOL_GCOALESCE:
2366 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2367 case ETHTOOL_SCOALESCE:
2368 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2374 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2377 case ETHTOOL_PHY_DOWNSHIFT:
2378 case ETHTOOL_PHY_FAST_LINK_DOWN:
2379 if (tuna->len != sizeof(u8) ||
2380 tuna->type_id != ETHTOOL_TUNABLE_U8)
2383 case ETHTOOL_PHY_EDPD:
2384 if (tuna->len != sizeof(u16) ||
2385 tuna->type_id != ETHTOOL_TUNABLE_U16)
2395 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2398 struct ethtool_tunable tuna;
2399 struct phy_device *phydev = dev->phydev;
2402 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2405 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2407 ret = ethtool_phy_tunable_valid(&tuna);
2410 data = kmalloc(tuna.len, GFP_USER);
2413 mutex_lock(&phydev->lock);
2414 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2415 mutex_unlock(&phydev->lock);
2418 useraddr += sizeof(tuna);
2420 if (copy_to_user(useraddr, data, tuna.len))
2429 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2432 struct ethtool_tunable tuna;
2433 struct phy_device *phydev = dev->phydev;
2436 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2438 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2440 ret = ethtool_phy_tunable_valid(&tuna);
2443 useraddr += sizeof(tuna);
2444 data = memdup_user(useraddr, tuna.len);
2446 return PTR_ERR(data);
2447 mutex_lock(&phydev->lock);
2448 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2449 mutex_unlock(&phydev->lock);
2455 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2457 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2460 if (!dev->ethtool_ops->get_fecparam)
2463 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2467 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2472 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2474 struct ethtool_fecparam fecparam;
2476 if (!dev->ethtool_ops->set_fecparam)
2479 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2482 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2485 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2487 int dev_ethtool(struct net *net, struct ifreq *ifr)
2489 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2490 void __user *useraddr = ifr->ifr_data;
2491 u32 ethcmd, sub_cmd;
2493 netdev_features_t old_features;
2495 if (!dev || !netif_device_present(dev))
2498 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2501 if (ethcmd == ETHTOOL_PERQUEUE) {
2502 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2507 /* Allow some commands to be done by anyone */
2510 case ETHTOOL_GDRVINFO:
2511 case ETHTOOL_GMSGLVL:
2513 case ETHTOOL_GCOALESCE:
2514 case ETHTOOL_GRINGPARAM:
2515 case ETHTOOL_GPAUSEPARAM:
2516 case ETHTOOL_GRXCSUM:
2517 case ETHTOOL_GTXCSUM:
2519 case ETHTOOL_GSSET_INFO:
2520 case ETHTOOL_GSTRINGS:
2521 case ETHTOOL_GSTATS:
2522 case ETHTOOL_GPHYSTATS:
2524 case ETHTOOL_GPERMADDR:
2528 case ETHTOOL_GFLAGS:
2529 case ETHTOOL_GPFLAGS:
2531 case ETHTOOL_GRXRINGS:
2532 case ETHTOOL_GRXCLSRLCNT:
2533 case ETHTOOL_GRXCLSRULE:
2534 case ETHTOOL_GRXCLSRLALL:
2535 case ETHTOOL_GRXFHINDIR:
2537 case ETHTOOL_GFEATURES:
2538 case ETHTOOL_GCHANNELS:
2539 case ETHTOOL_GET_TS_INFO:
2541 case ETHTOOL_GTUNABLE:
2542 case ETHTOOL_PHY_GTUNABLE:
2543 case ETHTOOL_GLINKSETTINGS:
2544 case ETHTOOL_GFECPARAM:
2547 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2551 if (dev->ethtool_ops->begin) {
2552 rc = dev->ethtool_ops->begin(dev);
2556 old_features = dev->features;
2560 rc = ethtool_get_settings(dev, useraddr);
2563 rc = ethtool_set_settings(dev, useraddr);
2565 case ETHTOOL_GDRVINFO:
2566 rc = ethtool_get_drvinfo(dev, useraddr);
2569 rc = ethtool_get_regs(dev, useraddr);
2572 rc = ethtool_get_wol(dev, useraddr);
2575 rc = ethtool_set_wol(dev, useraddr);
2577 case ETHTOOL_GMSGLVL:
2578 rc = ethtool_get_value(dev, useraddr, ethcmd,
2579 dev->ethtool_ops->get_msglevel);
2581 case ETHTOOL_SMSGLVL:
2582 rc = ethtool_set_value_void(dev, useraddr,
2583 dev->ethtool_ops->set_msglevel);
2586 rc = ethtool_get_eee(dev, useraddr);
2589 rc = ethtool_set_eee(dev, useraddr);
2591 case ETHTOOL_NWAY_RST:
2592 rc = ethtool_nway_reset(dev);
2595 rc = ethtool_get_link(dev, useraddr);
2597 case ETHTOOL_GEEPROM:
2598 rc = ethtool_get_eeprom(dev, useraddr);
2600 case ETHTOOL_SEEPROM:
2601 rc = ethtool_set_eeprom(dev, useraddr);
2603 case ETHTOOL_GCOALESCE:
2604 rc = ethtool_get_coalesce(dev, useraddr);
2606 case ETHTOOL_SCOALESCE:
2607 rc = ethtool_set_coalesce(dev, useraddr);
2609 case ETHTOOL_GRINGPARAM:
2610 rc = ethtool_get_ringparam(dev, useraddr);
2612 case ETHTOOL_SRINGPARAM:
2613 rc = ethtool_set_ringparam(dev, useraddr);
2615 case ETHTOOL_GPAUSEPARAM:
2616 rc = ethtool_get_pauseparam(dev, useraddr);
2618 case ETHTOOL_SPAUSEPARAM:
2619 rc = ethtool_set_pauseparam(dev, useraddr);
2622 rc = ethtool_self_test(dev, useraddr);
2624 case ETHTOOL_GSTRINGS:
2625 rc = ethtool_get_strings(dev, useraddr);
2627 case ETHTOOL_PHYS_ID:
2628 rc = ethtool_phys_id(dev, useraddr);
2630 case ETHTOOL_GSTATS:
2631 rc = ethtool_get_stats(dev, useraddr);
2633 case ETHTOOL_GPERMADDR:
2634 rc = ethtool_get_perm_addr(dev, useraddr);
2636 case ETHTOOL_GFLAGS:
2637 rc = ethtool_get_value(dev, useraddr, ethcmd,
2638 __ethtool_get_flags);
2640 case ETHTOOL_SFLAGS:
2641 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2643 case ETHTOOL_GPFLAGS:
2644 rc = ethtool_get_value(dev, useraddr, ethcmd,
2645 dev->ethtool_ops->get_priv_flags);
2647 case ETHTOOL_SPFLAGS:
2648 rc = ethtool_set_value(dev, useraddr,
2649 dev->ethtool_ops->set_priv_flags);
2652 case ETHTOOL_GRXRINGS:
2653 case ETHTOOL_GRXCLSRLCNT:
2654 case ETHTOOL_GRXCLSRULE:
2655 case ETHTOOL_GRXCLSRLALL:
2656 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2659 case ETHTOOL_SRXCLSRLDEL:
2660 case ETHTOOL_SRXCLSRLINS:
2661 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2663 case ETHTOOL_FLASHDEV:
2664 rc = ethtool_flash_device(dev, useraddr);
2667 rc = ethtool_reset(dev, useraddr);
2669 case ETHTOOL_GSSET_INFO:
2670 rc = ethtool_get_sset_info(dev, useraddr);
2672 case ETHTOOL_GRXFHINDIR:
2673 rc = ethtool_get_rxfh_indir(dev, useraddr);
2675 case ETHTOOL_SRXFHINDIR:
2676 rc = ethtool_set_rxfh_indir(dev, useraddr);
2679 rc = ethtool_get_rxfh(dev, useraddr);
2682 rc = ethtool_set_rxfh(dev, useraddr);
2684 case ETHTOOL_GFEATURES:
2685 rc = ethtool_get_features(dev, useraddr);
2687 case ETHTOOL_SFEATURES:
2688 rc = ethtool_set_features(dev, useraddr);
2690 case ETHTOOL_GTXCSUM:
2691 case ETHTOOL_GRXCSUM:
2696 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2698 case ETHTOOL_STXCSUM:
2699 case ETHTOOL_SRXCSUM:
2704 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2706 case ETHTOOL_GCHANNELS:
2707 rc = ethtool_get_channels(dev, useraddr);
2709 case ETHTOOL_SCHANNELS:
2710 rc = ethtool_set_channels(dev, useraddr);
2712 case ETHTOOL_SET_DUMP:
2713 rc = ethtool_set_dump(dev, useraddr);
2715 case ETHTOOL_GET_DUMP_FLAG:
2716 rc = ethtool_get_dump_flag(dev, useraddr);
2718 case ETHTOOL_GET_DUMP_DATA:
2719 rc = ethtool_get_dump_data(dev, useraddr);
2721 case ETHTOOL_GET_TS_INFO:
2722 rc = ethtool_get_ts_info(dev, useraddr);
2724 case ETHTOOL_GMODULEINFO:
2725 rc = ethtool_get_module_info(dev, useraddr);
2727 case ETHTOOL_GMODULEEEPROM:
2728 rc = ethtool_get_module_eeprom(dev, useraddr);
2730 case ETHTOOL_GTUNABLE:
2731 rc = ethtool_get_tunable(dev, useraddr);
2733 case ETHTOOL_STUNABLE:
2734 rc = ethtool_set_tunable(dev, useraddr);
2736 case ETHTOOL_GPHYSTATS:
2737 rc = ethtool_get_phy_stats(dev, useraddr);
2739 case ETHTOOL_PERQUEUE:
2740 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2742 case ETHTOOL_GLINKSETTINGS:
2743 rc = ethtool_get_link_ksettings(dev, useraddr);
2745 case ETHTOOL_SLINKSETTINGS:
2746 rc = ethtool_set_link_ksettings(dev, useraddr);
2748 case ETHTOOL_PHY_GTUNABLE:
2749 rc = get_phy_tunable(dev, useraddr);
2751 case ETHTOOL_PHY_STUNABLE:
2752 rc = set_phy_tunable(dev, useraddr);
2754 case ETHTOOL_GFECPARAM:
2755 rc = ethtool_get_fecparam(dev, useraddr);
2757 case ETHTOOL_SFECPARAM:
2758 rc = ethtool_set_fecparam(dev, useraddr);
2764 if (dev->ethtool_ops->complete)
2765 dev->ethtool_ops->complete(dev);
2767 if (old_features != dev->features)
2768 netdev_features_change(dev);
2773 struct ethtool_rx_flow_key {
2774 struct flow_dissector_key_basic basic;
2776 struct flow_dissector_key_ipv4_addrs ipv4;
2777 struct flow_dissector_key_ipv6_addrs ipv6;
2779 struct flow_dissector_key_ports tp;
2780 struct flow_dissector_key_ip ip;
2781 struct flow_dissector_key_vlan vlan;
2782 struct flow_dissector_key_eth_addrs eth_addrs;
2783 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2785 struct ethtool_rx_flow_match {
2786 struct flow_dissector dissector;
2787 struct ethtool_rx_flow_key key;
2788 struct ethtool_rx_flow_key mask;
2791 struct ethtool_rx_flow_rule *
2792 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2794 const struct ethtool_rx_flow_spec *fs = input->fs;
2795 static struct in6_addr zero_addr = {};
2796 struct ethtool_rx_flow_match *match;
2797 struct ethtool_rx_flow_rule *flow;
2798 struct flow_action_entry *act;
2800 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2801 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2803 return ERR_PTR(-ENOMEM);
2805 /* ethtool_rx supports only one single action per rule. */
2806 flow->rule = flow_rule_alloc(1);
2809 return ERR_PTR(-ENOMEM);
2812 match = (struct ethtool_rx_flow_match *)flow->priv;
2813 flow->rule->match.dissector = &match->dissector;
2814 flow->rule->match.mask = &match->mask;
2815 flow->rule->match.key = &match->key;
2817 match->mask.basic.n_proto = htons(0xffff);
2819 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2821 const struct ethhdr *ether_spec, *ether_m_spec;
2823 ether_spec = &fs->h_u.ether_spec;
2824 ether_m_spec = &fs->m_u.ether_spec;
2826 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2827 ether_addr_copy(match->key.eth_addrs.src,
2828 ether_spec->h_source);
2829 ether_addr_copy(match->mask.eth_addrs.src,
2830 ether_m_spec->h_source);
2832 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2833 ether_addr_copy(match->key.eth_addrs.dst,
2834 ether_spec->h_dest);
2835 ether_addr_copy(match->mask.eth_addrs.dst,
2836 ether_m_spec->h_dest);
2838 if (ether_m_spec->h_proto) {
2839 match->key.basic.n_proto = ether_spec->h_proto;
2840 match->mask.basic.n_proto = ether_m_spec->h_proto;
2846 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2848 match->key.basic.n_proto = htons(ETH_P_IP);
2850 v4_spec = &fs->h_u.tcp_ip4_spec;
2851 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2853 if (v4_m_spec->ip4src) {
2854 match->key.ipv4.src = v4_spec->ip4src;
2855 match->mask.ipv4.src = v4_m_spec->ip4src;
2857 if (v4_m_spec->ip4dst) {
2858 match->key.ipv4.dst = v4_spec->ip4dst;
2859 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2861 if (v4_m_spec->ip4src ||
2862 v4_m_spec->ip4dst) {
2863 match->dissector.used_keys |=
2864 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2865 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2866 offsetof(struct ethtool_rx_flow_key, ipv4);
2868 if (v4_m_spec->psrc) {
2869 match->key.tp.src = v4_spec->psrc;
2870 match->mask.tp.src = v4_m_spec->psrc;
2872 if (v4_m_spec->pdst) {
2873 match->key.tp.dst = v4_spec->pdst;
2874 match->mask.tp.dst = v4_m_spec->pdst;
2876 if (v4_m_spec->psrc ||
2878 match->dissector.used_keys |=
2879 BIT(FLOW_DISSECTOR_KEY_PORTS);
2880 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2881 offsetof(struct ethtool_rx_flow_key, tp);
2883 if (v4_m_spec->tos) {
2884 match->key.ip.tos = v4_spec->tos;
2885 match->mask.ip.tos = v4_m_spec->tos;
2886 match->dissector.used_keys |=
2887 BIT(FLOW_DISSECTOR_KEY_IP);
2888 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2889 offsetof(struct ethtool_rx_flow_key, ip);
2895 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2897 match->key.basic.n_proto = htons(ETH_P_IPV6);
2899 v6_spec = &fs->h_u.tcp_ip6_spec;
2900 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2901 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2902 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2903 sizeof(match->key.ipv6.src));
2904 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2905 sizeof(match->mask.ipv6.src));
2907 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2908 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2909 sizeof(match->key.ipv6.dst));
2910 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2911 sizeof(match->mask.ipv6.dst));
2913 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2914 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2915 match->dissector.used_keys |=
2916 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2917 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2918 offsetof(struct ethtool_rx_flow_key, ipv6);
2920 if (v6_m_spec->psrc) {
2921 match->key.tp.src = v6_spec->psrc;
2922 match->mask.tp.src = v6_m_spec->psrc;
2924 if (v6_m_spec->pdst) {
2925 match->key.tp.dst = v6_spec->pdst;
2926 match->mask.tp.dst = v6_m_spec->pdst;
2928 if (v6_m_spec->psrc ||
2930 match->dissector.used_keys |=
2931 BIT(FLOW_DISSECTOR_KEY_PORTS);
2932 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2933 offsetof(struct ethtool_rx_flow_key, tp);
2935 if (v6_m_spec->tclass) {
2936 match->key.ip.tos = v6_spec->tclass;
2937 match->mask.ip.tos = v6_m_spec->tclass;
2938 match->dissector.used_keys |=
2939 BIT(FLOW_DISSECTOR_KEY_IP);
2940 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2941 offsetof(struct ethtool_rx_flow_key, ip);
2946 ethtool_rx_flow_rule_destroy(flow);
2947 return ERR_PTR(-EINVAL);
2950 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2953 match->key.basic.ip_proto = IPPROTO_TCP;
2957 match->key.basic.ip_proto = IPPROTO_UDP;
2960 match->mask.basic.ip_proto = 0xff;
2962 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
2963 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
2964 offsetof(struct ethtool_rx_flow_key, basic);
2966 if (fs->flow_type & FLOW_EXT) {
2967 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
2968 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
2970 if (ext_m_spec->vlan_etype) {
2971 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
2972 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
2975 if (ext_m_spec->vlan_tci) {
2976 match->key.vlan.vlan_id =
2977 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
2978 match->mask.vlan.vlan_id =
2979 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
2981 match->key.vlan.vlan_dei =
2982 !!(ext_h_spec->vlan_tci & htons(0x1000));
2983 match->mask.vlan.vlan_dei =
2984 !!(ext_m_spec->vlan_tci & htons(0x1000));
2986 match->key.vlan.vlan_priority =
2987 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
2988 match->mask.vlan.vlan_priority =
2989 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
2992 if (ext_m_spec->vlan_etype ||
2993 ext_m_spec->vlan_tci) {
2994 match->dissector.used_keys |=
2995 BIT(FLOW_DISSECTOR_KEY_VLAN);
2996 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
2997 offsetof(struct ethtool_rx_flow_key, vlan);
3000 if (fs->flow_type & FLOW_MAC_EXT) {
3001 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3002 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3004 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3006 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3009 match->dissector.used_keys |=
3010 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3011 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3012 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3015 act = &flow->rule->action.entries[0];
3016 switch (fs->ring_cookie) {
3017 case RX_CLS_FLOW_DISC:
3018 act->id = FLOW_ACTION_DROP;
3020 case RX_CLS_FLOW_WAKE:
3021 act->id = FLOW_ACTION_WAKE;
3024 act->id = FLOW_ACTION_QUEUE;
3025 if (fs->flow_type & FLOW_RSS)
3026 act->queue.ctx = input->rss_ctx;
3028 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3029 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3035 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3037 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3042 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);