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>
29 #include <linux/ethtool_netlink.h>
30 #include <generated/utsrelease.h>
34 * Some useful ethtool_ops methods that're device independent.
35 * If we find that all drivers want to do the same thing here,
36 * we can turn these into dev_() function calls.
39 u32 ethtool_op_get_link(struct net_device *dev)
41 return netif_carrier_ok(dev) ? 1 : 0;
43 EXPORT_SYMBOL(ethtool_op_get_link);
45 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
47 info->so_timestamping =
48 SOF_TIMESTAMPING_TX_SOFTWARE |
49 SOF_TIMESTAMPING_RX_SOFTWARE |
50 SOF_TIMESTAMPING_SOFTWARE;
54 EXPORT_SYMBOL(ethtool_op_get_ts_info);
56 /* Handlers for each ethtool command */
58 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
60 struct ethtool_gfeatures cmd = {
61 .cmd = ETHTOOL_GFEATURES,
62 .size = ETHTOOL_DEV_FEATURE_WORDS,
64 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
69 /* in case feature bits run out again */
70 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
72 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
73 features[i].available = (u32)(dev->hw_features >> (32 * i));
74 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
75 features[i].active = (u32)(dev->features >> (32 * i));
76 features[i].never_changed =
77 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
80 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
81 if (get_user(copy_size, sizeaddr))
84 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
85 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
87 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
89 useraddr += sizeof(cmd);
90 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
96 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
98 struct ethtool_sfeatures cmd;
99 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
100 netdev_features_t wanted = 0, valid = 0;
103 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
105 useraddr += sizeof(cmd);
107 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
110 if (copy_from_user(features, useraddr, sizeof(features)))
113 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
114 valid |= (netdev_features_t)features[i].valid << (32 * i);
115 wanted |= (netdev_features_t)features[i].requested << (32 * i);
118 if (valid & ~NETIF_F_ETHTOOL_BITS)
121 if (valid & ~dev->hw_features) {
122 valid &= dev->hw_features;
123 ret |= ETHTOOL_F_UNSUPPORTED;
126 dev->wanted_features &= ~valid;
127 dev->wanted_features |= wanted & valid;
128 __netdev_update_features(dev);
130 if ((dev->wanted_features ^ dev->features) & valid)
131 ret |= ETHTOOL_F_WISH;
136 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
138 const struct ethtool_ops *ops = dev->ethtool_ops;
140 if (sset == ETH_SS_FEATURES)
141 return ARRAY_SIZE(netdev_features_strings);
143 if (sset == ETH_SS_RSS_HASH_FUNCS)
144 return ARRAY_SIZE(rss_hash_func_strings);
146 if (sset == ETH_SS_TUNABLES)
147 return ARRAY_SIZE(tunable_strings);
149 if (sset == ETH_SS_PHY_TUNABLES)
150 return ARRAY_SIZE(phy_tunable_strings);
152 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
153 !ops->get_ethtool_phy_stats)
154 return phy_ethtool_get_sset_count(dev->phydev);
156 if (sset == ETH_SS_LINK_MODES)
157 return __ETHTOOL_LINK_MODE_MASK_NBITS;
159 if (ops->get_sset_count && ops->get_strings)
160 return ops->get_sset_count(dev, sset);
165 static void __ethtool_get_strings(struct net_device *dev,
166 u32 stringset, u8 *data)
168 const struct ethtool_ops *ops = dev->ethtool_ops;
170 if (stringset == ETH_SS_FEATURES)
171 memcpy(data, netdev_features_strings,
172 sizeof(netdev_features_strings));
173 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
174 memcpy(data, rss_hash_func_strings,
175 sizeof(rss_hash_func_strings));
176 else if (stringset == ETH_SS_TUNABLES)
177 memcpy(data, tunable_strings, sizeof(tunable_strings));
178 else if (stringset == ETH_SS_PHY_TUNABLES)
179 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
180 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
181 !ops->get_ethtool_phy_stats)
182 phy_ethtool_get_strings(dev->phydev, data);
183 else if (stringset == ETH_SS_LINK_MODES)
184 memcpy(data, link_mode_names,
185 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
187 /* ops->get_strings is valid because checked earlier */
188 ops->get_strings(dev, stringset, data);
191 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
193 /* feature masks of legacy discrete ethtool ops */
196 case ETHTOOL_GTXCSUM:
197 case ETHTOOL_STXCSUM:
198 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
200 case ETHTOOL_GRXCSUM:
201 case ETHTOOL_SRXCSUM:
202 return NETIF_F_RXCSUM;
205 return NETIF_F_SG | NETIF_F_FRAGLIST;
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 ksettings link modes had higher bits
362 * set. legacy_settings always updated (best effort)
365 convert_link_ksettings_to_legacy_settings(
366 struct ethtool_cmd *legacy_settings,
367 const struct ethtool_link_ksettings *link_ksettings)
371 memset(legacy_settings, 0, sizeof(*legacy_settings));
372 /* this also clears the deprecated fields in legacy structure:
378 retval &= ethtool_convert_link_mode_to_legacy_u32(
379 &legacy_settings->supported,
380 link_ksettings->link_modes.supported);
381 retval &= ethtool_convert_link_mode_to_legacy_u32(
382 &legacy_settings->advertising,
383 link_ksettings->link_modes.advertising);
384 retval &= ethtool_convert_link_mode_to_legacy_u32(
385 &legacy_settings->lp_advertising,
386 link_ksettings->link_modes.lp_advertising);
387 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
388 legacy_settings->duplex
389 = link_ksettings->base.duplex;
390 legacy_settings->port
391 = link_ksettings->base.port;
392 legacy_settings->phy_address
393 = link_ksettings->base.phy_address;
394 legacy_settings->autoneg
395 = link_ksettings->base.autoneg;
396 legacy_settings->mdio_support
397 = link_ksettings->base.mdio_support;
398 legacy_settings->eth_tp_mdix
399 = link_ksettings->base.eth_tp_mdix;
400 legacy_settings->eth_tp_mdix_ctrl
401 = link_ksettings->base.eth_tp_mdix_ctrl;
402 legacy_settings->transceiver
403 = link_ksettings->base.transceiver;
407 /* number of 32-bit words to store the user's link mode bitmaps */
408 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
409 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
411 /* layout of the struct passed from/to userland */
412 struct ethtool_link_usettings {
413 struct ethtool_link_settings base;
415 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
416 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
417 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
421 /* Internal kernel helper to query a device ethtool_link_settings. */
422 int __ethtool_get_link_ksettings(struct net_device *dev,
423 struct ethtool_link_ksettings *link_ksettings)
427 if (!dev->ethtool_ops->get_link_ksettings)
430 memset(link_ksettings, 0, sizeof(*link_ksettings));
431 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
433 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
435 /* convert ethtool_link_usettings in user space to a kernel internal
436 * ethtool_link_ksettings. return 0 on success, errno on error.
438 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
439 const void __user *from)
441 struct ethtool_link_usettings link_usettings;
443 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
446 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
447 bitmap_from_arr32(to->link_modes.supported,
448 link_usettings.link_modes.supported,
449 __ETHTOOL_LINK_MODE_MASK_NBITS);
450 bitmap_from_arr32(to->link_modes.advertising,
451 link_usettings.link_modes.advertising,
452 __ETHTOOL_LINK_MODE_MASK_NBITS);
453 bitmap_from_arr32(to->link_modes.lp_advertising,
454 link_usettings.link_modes.lp_advertising,
455 __ETHTOOL_LINK_MODE_MASK_NBITS);
460 /* Check if the user is trying to change anything besides speed/duplex */
461 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
463 struct ethtool_link_settings base2 = {};
465 base2.speed = cmd->base.speed;
466 base2.port = PORT_OTHER;
467 base2.duplex = cmd->base.duplex;
468 base2.cmd = cmd->base.cmd;
469 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
471 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
472 bitmap_empty(cmd->link_modes.supported,
473 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
474 bitmap_empty(cmd->link_modes.lp_advertising,
475 __ETHTOOL_LINK_MODE_MASK_NBITS);
478 /* convert a kernel internal ethtool_link_ksettings to
479 * ethtool_link_usettings in user space. return 0 on success, errno on
483 store_link_ksettings_for_user(void __user *to,
484 const struct ethtool_link_ksettings *from)
486 struct ethtool_link_usettings link_usettings;
488 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
489 bitmap_to_arr32(link_usettings.link_modes.supported,
490 from->link_modes.supported,
491 __ETHTOOL_LINK_MODE_MASK_NBITS);
492 bitmap_to_arr32(link_usettings.link_modes.advertising,
493 from->link_modes.advertising,
494 __ETHTOOL_LINK_MODE_MASK_NBITS);
495 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
496 from->link_modes.lp_advertising,
497 __ETHTOOL_LINK_MODE_MASK_NBITS);
499 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
505 /* Query device for its ethtool_link_settings. */
506 static int ethtool_get_link_ksettings(struct net_device *dev,
507 void __user *useraddr)
510 struct ethtool_link_ksettings link_ksettings;
513 if (!dev->ethtool_ops->get_link_ksettings)
516 /* handle bitmap nbits handshake */
517 if (copy_from_user(&link_ksettings.base, useraddr,
518 sizeof(link_ksettings.base)))
521 if (__ETHTOOL_LINK_MODE_MASK_NU32
522 != link_ksettings.base.link_mode_masks_nwords) {
523 /* wrong link mode nbits requested */
524 memset(&link_ksettings, 0, sizeof(link_ksettings));
525 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
526 /* send back number of words required as negative val */
527 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
528 "need too many bits for link modes!");
529 link_ksettings.base.link_mode_masks_nwords
530 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
532 /* copy the base fields back to user, not the link
535 if (copy_to_user(useraddr, &link_ksettings.base,
536 sizeof(link_ksettings.base)))
542 /* handshake successful: user/kernel agree on
543 * link_mode_masks_nwords
546 memset(&link_ksettings, 0, sizeof(link_ksettings));
547 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
551 /* make sure we tell the right values to user */
552 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
553 link_ksettings.base.link_mode_masks_nwords
554 = __ETHTOOL_LINK_MODE_MASK_NU32;
556 return store_link_ksettings_for_user(useraddr, &link_ksettings);
559 /* Update device ethtool_link_settings. */
560 static int ethtool_set_link_ksettings(struct net_device *dev,
561 void __user *useraddr)
564 struct ethtool_link_ksettings link_ksettings;
568 if (!dev->ethtool_ops->set_link_ksettings)
571 /* make sure nbits field has expected value */
572 if (copy_from_user(&link_ksettings.base, useraddr,
573 sizeof(link_ksettings.base)))
576 if (__ETHTOOL_LINK_MODE_MASK_NU32
577 != link_ksettings.base.link_mode_masks_nwords)
580 /* copy the whole structure, now that we know it has expected
583 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
587 /* re-check nwords field, just in case */
588 if (__ETHTOOL_LINK_MODE_MASK_NU32
589 != link_ksettings.base.link_mode_masks_nwords)
592 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
594 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
595 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
600 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
601 const struct ethtool_link_ksettings *cmd,
602 u32 *dev_speed, u8 *dev_duplex)
607 speed = cmd->base.speed;
608 duplex = cmd->base.duplex;
609 /* don't allow custom speed and duplex */
610 if (!ethtool_validate_speed(speed) ||
611 !ethtool_validate_duplex(duplex) ||
612 !ethtool_virtdev_validate_cmd(cmd))
615 *dev_duplex = duplex;
619 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
621 /* Query device for its ethtool_cmd settings.
623 * Backward compatibility note: for compatibility with legacy ethtool, this is
624 * now implemented via get_link_ksettings. When driver reports higher link mode
625 * bits, a kernel warning is logged once (with name of 1st driver/device) to
626 * recommend user to upgrade ethtool, but the command is successful (only the
627 * lower link mode bits reported back to user). Deprecated fields from
628 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
630 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
632 struct ethtool_link_ksettings link_ksettings;
633 struct ethtool_cmd cmd;
637 if (!dev->ethtool_ops->get_link_ksettings)
640 memset(&link_ksettings, 0, sizeof(link_ksettings));
641 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
644 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
646 /* send a sensible cmd tag back to user */
647 cmd.cmd = ETHTOOL_GSET;
649 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
655 /* Update device link settings with given ethtool_cmd.
657 * Backward compatibility note: for compatibility with legacy ethtool, this is
658 * now always implemented via set_link_settings. When user's request updates
659 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
660 * warning is logged once (with name of 1st driver/device) to recommend user to
661 * upgrade ethtool, and the request is rejected.
663 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
665 struct ethtool_link_ksettings link_ksettings;
666 struct ethtool_cmd cmd;
671 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
673 if (!dev->ethtool_ops->set_link_ksettings)
676 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
678 link_ksettings.base.link_mode_masks_nwords =
679 __ETHTOOL_LINK_MODE_MASK_NU32;
680 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
682 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
683 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
688 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
689 void __user *useraddr)
691 struct ethtool_drvinfo info;
692 const struct ethtool_ops *ops = dev->ethtool_ops;
694 memset(&info, 0, sizeof(info));
695 info.cmd = ETHTOOL_GDRVINFO;
696 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
697 if (ops->get_drvinfo) {
698 ops->get_drvinfo(dev, &info);
699 } else if (dev->dev.parent && dev->dev.parent->driver) {
700 strlcpy(info.bus_info, dev_name(dev->dev.parent),
701 sizeof(info.bus_info));
702 strlcpy(info.driver, dev->dev.parent->driver->name,
703 sizeof(info.driver));
709 * this method of obtaining string set info is deprecated;
710 * Use ETHTOOL_GSSET_INFO instead.
712 if (ops->get_sset_count) {
715 rc = ops->get_sset_count(dev, ETH_SS_TEST);
717 info.testinfo_len = rc;
718 rc = ops->get_sset_count(dev, ETH_SS_STATS);
721 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
723 info.n_priv_flags = rc;
725 if (ops->get_regs_len) {
726 int ret = ops->get_regs_len(dev);
729 info.regdump_len = ret;
732 if (ops->get_eeprom_len)
733 info.eedump_len = ops->get_eeprom_len(dev);
735 if (!info.fw_version[0])
736 devlink_compat_running_version(dev, info.fw_version,
737 sizeof(info.fw_version));
739 if (copy_to_user(useraddr, &info, sizeof(info)))
744 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
745 void __user *useraddr)
747 struct ethtool_sset_info info;
749 int i, idx = 0, n_bits = 0, ret, rc;
750 u32 *info_buf = NULL;
752 if (copy_from_user(&info, useraddr, sizeof(info)))
755 /* store copy of mask, because we zero struct later on */
756 sset_mask = info.sset_mask;
760 /* calculate size of return buffer */
761 n_bits = hweight64(sset_mask);
763 memset(&info, 0, sizeof(info));
764 info.cmd = ETHTOOL_GSSET_INFO;
766 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
771 * fill return buffer based on input bitmask and successful
772 * get_sset_count return
774 for (i = 0; i < 64; i++) {
775 if (!(sset_mask & (1ULL << i)))
778 rc = __ethtool_get_sset_count(dev, i);
780 info.sset_mask |= (1ULL << i);
781 info_buf[idx++] = rc;
786 if (copy_to_user(useraddr, &info, sizeof(info)))
789 useraddr += offsetof(struct ethtool_sset_info, data);
790 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
800 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
801 u32 cmd, void __user *useraddr)
803 struct ethtool_rxnfc info;
804 size_t info_size = sizeof(info);
807 if (!dev->ethtool_ops->set_rxnfc)
810 /* struct ethtool_rxnfc was originally defined for
811 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
812 * members. User-space might still be using that
814 if (cmd == ETHTOOL_SRXFH)
815 info_size = (offsetof(struct ethtool_rxnfc, data) +
818 if (copy_from_user(&info, useraddr, info_size))
821 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
825 if (cmd == ETHTOOL_SRXCLSRLINS &&
826 copy_to_user(useraddr, &info, info_size))
832 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
833 u32 cmd, void __user *useraddr)
835 struct ethtool_rxnfc info;
836 size_t info_size = sizeof(info);
837 const struct ethtool_ops *ops = dev->ethtool_ops;
839 void *rule_buf = NULL;
844 /* struct ethtool_rxnfc was originally defined for
845 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
846 * members. User-space might still be using that
848 if (cmd == ETHTOOL_GRXFH)
849 info_size = (offsetof(struct ethtool_rxnfc, data) +
852 if (copy_from_user(&info, useraddr, info_size))
855 /* If FLOW_RSS was requested then user-space must be using the
856 * new definition, as FLOW_RSS is newer.
858 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
859 info_size = sizeof(info);
860 if (copy_from_user(&info, useraddr, info_size))
862 /* Since malicious users may modify the original data,
863 * we need to check whether FLOW_RSS is still requested.
865 if (!(info.flow_type & FLOW_RSS))
872 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
873 if (info.rule_cnt > 0) {
874 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
875 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
882 ret = ops->get_rxnfc(dev, &info, rule_buf);
887 if (copy_to_user(useraddr, &info, info_size))
891 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
892 if (copy_to_user(useraddr, rule_buf,
893 info.rule_cnt * sizeof(u32)))
904 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
905 struct ethtool_rxnfc *rx_rings,
910 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
913 /* Validate ring indices */
914 for (i = 0; i < size; i++)
915 if (indir[i] >= rx_rings->data)
921 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
923 void netdev_rss_key_fill(void *buffer, size_t len)
925 BUG_ON(len > sizeof(netdev_rss_key));
926 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
927 memcpy(buffer, netdev_rss_key, len);
929 EXPORT_SYMBOL(netdev_rss_key_fill);
931 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
932 void __user *useraddr)
934 u32 user_size, dev_size;
938 if (!dev->ethtool_ops->get_rxfh_indir_size ||
939 !dev->ethtool_ops->get_rxfh)
941 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
945 if (copy_from_user(&user_size,
946 useraddr + offsetof(struct ethtool_rxfh_indir, size),
950 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
951 &dev_size, sizeof(dev_size)))
954 /* If the user buffer size is 0, this is just a query for the
955 * device table size. Otherwise, if it's smaller than the
956 * device table size it's an error.
958 if (user_size < dev_size)
959 return user_size == 0 ? 0 : -EINVAL;
961 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
965 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
969 if (copy_to_user(useraddr +
970 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
971 indir, dev_size * sizeof(indir[0])))
979 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
980 void __user *useraddr)
982 struct ethtool_rxnfc rx_rings;
983 u32 user_size, dev_size, i;
985 const struct ethtool_ops *ops = dev->ethtool_ops;
987 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
989 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
993 dev_size = ops->get_rxfh_indir_size(dev);
997 if (copy_from_user(&user_size,
998 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1002 if (user_size != 0 && user_size != dev_size)
1005 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1009 rx_rings.cmd = ETHTOOL_GRXRINGS;
1010 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1014 if (user_size == 0) {
1015 for (i = 0; i < dev_size; i++)
1016 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1018 ret = ethtool_copy_validate_indir(indir,
1019 useraddr + ringidx_offset,
1026 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1030 /* indicate whether rxfh was set to default */
1032 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1034 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1041 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1042 void __user *useraddr)
1045 const struct ethtool_ops *ops = dev->ethtool_ops;
1046 u32 user_indir_size, user_key_size;
1047 u32 dev_indir_size = 0, dev_key_size = 0;
1048 struct ethtool_rxfh rxfh;
1059 if (ops->get_rxfh_indir_size)
1060 dev_indir_size = ops->get_rxfh_indir_size(dev);
1061 if (ops->get_rxfh_key_size)
1062 dev_key_size = ops->get_rxfh_key_size(dev);
1064 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1066 user_indir_size = rxfh.indir_size;
1067 user_key_size = rxfh.key_size;
1069 /* Check that reserved fields are 0 for now */
1070 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1072 /* Most drivers don't handle rss_context, check it's 0 as well */
1073 if (rxfh.rss_context && !ops->get_rxfh_context)
1076 rxfh.indir_size = dev_indir_size;
1077 rxfh.key_size = dev_key_size;
1078 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1081 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1082 (user_key_size && (user_key_size != dev_key_size)))
1085 indir_bytes = user_indir_size * sizeof(indir[0]);
1086 total_size = indir_bytes + user_key_size;
1087 rss_config = kzalloc(total_size, GFP_USER);
1091 if (user_indir_size)
1092 indir = (u32 *)rss_config;
1095 hkey = rss_config + indir_bytes;
1097 if (rxfh.rss_context)
1098 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1102 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1106 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1107 &dev_hfunc, sizeof(rxfh.hfunc))) {
1109 } else if (copy_to_user(useraddr +
1110 offsetof(struct ethtool_rxfh, rss_config[0]),
1111 rss_config, total_size)) {
1120 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1121 void __user *useraddr)
1124 const struct ethtool_ops *ops = dev->ethtool_ops;
1125 struct ethtool_rxnfc rx_rings;
1126 struct ethtool_rxfh rxfh;
1127 u32 dev_indir_size = 0, dev_key_size = 0, i;
1128 u32 *indir = NULL, indir_bytes = 0;
1131 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1132 bool delete = false;
1134 if (!ops->get_rxnfc || !ops->set_rxfh)
1137 if (ops->get_rxfh_indir_size)
1138 dev_indir_size = ops->get_rxfh_indir_size(dev);
1139 if (ops->get_rxfh_key_size)
1140 dev_key_size = ops->get_rxfh_key_size(dev);
1142 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1145 /* Check that reserved fields are 0 for now */
1146 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1148 /* Most drivers don't handle rss_context, check it's 0 as well */
1149 if (rxfh.rss_context && !ops->set_rxfh_context)
1152 /* If either indir, hash key or function is valid, proceed further.
1153 * Must request at least one change: indir size, hash key or function.
1155 if ((rxfh.indir_size &&
1156 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1157 rxfh.indir_size != dev_indir_size) ||
1158 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1159 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1160 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1163 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1164 indir_bytes = dev_indir_size * sizeof(indir[0]);
1166 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1170 rx_rings.cmd = ETHTOOL_GRXRINGS;
1171 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1175 /* rxfh.indir_size == 0 means reset the indir table to default (master
1176 * context) or delete the context (other RSS contexts).
1177 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1179 if (rxfh.indir_size &&
1180 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1181 indir = (u32 *)rss_config;
1182 ret = ethtool_copy_validate_indir(indir,
1183 useraddr + rss_cfg_offset,
1188 } else if (rxfh.indir_size == 0) {
1189 if (rxfh.rss_context == 0) {
1190 indir = (u32 *)rss_config;
1191 for (i = 0; i < dev_indir_size; i++)
1192 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1198 if (rxfh.key_size) {
1199 hkey = rss_config + indir_bytes;
1200 if (copy_from_user(hkey,
1201 useraddr + rss_cfg_offset + indir_bytes,
1208 if (rxfh.rss_context)
1209 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1210 &rxfh.rss_context, delete);
1212 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1216 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1217 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1220 if (!rxfh.rss_context) {
1221 /* indicate whether rxfh was set to default */
1222 if (rxfh.indir_size == 0)
1223 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1224 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1225 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1233 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1235 struct ethtool_regs regs;
1236 const struct ethtool_ops *ops = dev->ethtool_ops;
1240 if (!ops->get_regs || !ops->get_regs_len)
1243 if (copy_from_user(®s, useraddr, sizeof(regs)))
1246 reglen = ops->get_regs_len(dev);
1250 if (regs.len > reglen)
1253 regbuf = vzalloc(reglen);
1257 if (regs.len < reglen)
1260 ops->get_regs(dev, ®s, regbuf);
1263 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1265 useraddr += offsetof(struct ethtool_regs, data);
1266 if (copy_to_user(useraddr, regbuf, reglen))
1275 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1277 struct ethtool_value reset;
1280 if (!dev->ethtool_ops->reset)
1283 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1286 ret = dev->ethtool_ops->reset(dev, &reset.data);
1290 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1295 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1297 struct ethtool_wolinfo wol;
1299 if (!dev->ethtool_ops->get_wol)
1302 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1303 wol.cmd = ETHTOOL_GWOL;
1304 dev->ethtool_ops->get_wol(dev, &wol);
1306 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1311 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1313 struct ethtool_wolinfo wol;
1316 if (!dev->ethtool_ops->set_wol)
1319 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1322 ret = dev->ethtool_ops->set_wol(dev, &wol);
1326 dev->wol_enabled = !!wol.wolopts;
1327 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1332 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1334 struct ethtool_eee edata;
1337 if (!dev->ethtool_ops->get_eee)
1340 memset(&edata, 0, sizeof(struct ethtool_eee));
1341 edata.cmd = ETHTOOL_GEEE;
1342 rc = dev->ethtool_ops->get_eee(dev, &edata);
1347 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1353 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1355 struct ethtool_eee edata;
1358 if (!dev->ethtool_ops->set_eee)
1361 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1364 ret = dev->ethtool_ops->set_eee(dev, &edata);
1366 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1370 static int ethtool_nway_reset(struct net_device *dev)
1372 if (!dev->ethtool_ops->nway_reset)
1375 return dev->ethtool_ops->nway_reset(dev);
1378 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1380 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1381 int link = __ethtool_get_link(dev);
1387 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1392 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1393 int (*getter)(struct net_device *,
1394 struct ethtool_eeprom *, u8 *),
1397 struct ethtool_eeprom eeprom;
1398 void __user *userbuf = useraddr + sizeof(eeprom);
1399 u32 bytes_remaining;
1403 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1406 /* Check for wrap and zero */
1407 if (eeprom.offset + eeprom.len <= eeprom.offset)
1410 /* Check for exceeding total eeprom len */
1411 if (eeprom.offset + eeprom.len > total_len)
1414 data = kmalloc(PAGE_SIZE, GFP_USER);
1418 bytes_remaining = eeprom.len;
1419 while (bytes_remaining > 0) {
1420 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1422 ret = getter(dev, &eeprom, data);
1425 if (copy_to_user(userbuf, data, eeprom.len)) {
1429 userbuf += eeprom.len;
1430 eeprom.offset += eeprom.len;
1431 bytes_remaining -= eeprom.len;
1434 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1435 eeprom.offset -= eeprom.len;
1436 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1443 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1445 const struct ethtool_ops *ops = dev->ethtool_ops;
1447 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1448 !ops->get_eeprom_len(dev))
1451 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1452 ops->get_eeprom_len(dev));
1455 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1457 struct ethtool_eeprom eeprom;
1458 const struct ethtool_ops *ops = dev->ethtool_ops;
1459 void __user *userbuf = useraddr + sizeof(eeprom);
1460 u32 bytes_remaining;
1464 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1465 !ops->get_eeprom_len(dev))
1468 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1471 /* Check for wrap and zero */
1472 if (eeprom.offset + eeprom.len <= eeprom.offset)
1475 /* Check for exceeding total eeprom len */
1476 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1479 data = kmalloc(PAGE_SIZE, GFP_USER);
1483 bytes_remaining = eeprom.len;
1484 while (bytes_remaining > 0) {
1485 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1487 if (copy_from_user(data, userbuf, eeprom.len)) {
1491 ret = ops->set_eeprom(dev, &eeprom, data);
1494 userbuf += eeprom.len;
1495 eeprom.offset += eeprom.len;
1496 bytes_remaining -= eeprom.len;
1503 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1504 void __user *useraddr)
1506 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1508 if (!dev->ethtool_ops->get_coalesce)
1511 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1513 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1519 ethtool_set_coalesce_supported(struct net_device *dev,
1520 struct ethtool_coalesce *coalesce)
1522 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1523 u32 nonzero_params = 0;
1525 if (coalesce->rx_coalesce_usecs)
1526 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1527 if (coalesce->rx_max_coalesced_frames)
1528 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1529 if (coalesce->rx_coalesce_usecs_irq)
1530 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1531 if (coalesce->rx_max_coalesced_frames_irq)
1532 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1533 if (coalesce->tx_coalesce_usecs)
1534 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1535 if (coalesce->tx_max_coalesced_frames)
1536 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1537 if (coalesce->tx_coalesce_usecs_irq)
1538 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1539 if (coalesce->tx_max_coalesced_frames_irq)
1540 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1541 if (coalesce->stats_block_coalesce_usecs)
1542 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1543 if (coalesce->use_adaptive_rx_coalesce)
1544 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1545 if (coalesce->use_adaptive_tx_coalesce)
1546 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1547 if (coalesce->pkt_rate_low)
1548 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1549 if (coalesce->rx_coalesce_usecs_low)
1550 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1551 if (coalesce->rx_max_coalesced_frames_low)
1552 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1553 if (coalesce->tx_coalesce_usecs_low)
1554 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1555 if (coalesce->tx_max_coalesced_frames_low)
1556 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1557 if (coalesce->pkt_rate_high)
1558 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1559 if (coalesce->rx_coalesce_usecs_high)
1560 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1561 if (coalesce->rx_max_coalesced_frames_high)
1562 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1563 if (coalesce->tx_coalesce_usecs_high)
1564 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1565 if (coalesce->tx_max_coalesced_frames_high)
1566 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1567 if (coalesce->rate_sample_interval)
1568 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1570 return (supported_params & nonzero_params) == nonzero_params;
1573 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1574 void __user *useraddr)
1576 struct ethtool_coalesce coalesce;
1579 if (!dev->ethtool_ops->set_coalesce)
1582 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1585 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1588 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce);
1590 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1594 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1596 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1598 if (!dev->ethtool_ops->get_ringparam)
1601 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1603 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1608 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1610 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1613 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1616 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1619 dev->ethtool_ops->get_ringparam(dev, &max);
1621 /* ensure new ring parameters are within the maximums */
1622 if (ringparam.rx_pending > max.rx_max_pending ||
1623 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1624 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1625 ringparam.tx_pending > max.tx_max_pending)
1628 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1630 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1634 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1635 void __user *useraddr)
1637 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1639 if (!dev->ethtool_ops->get_channels)
1642 dev->ethtool_ops->get_channels(dev, &channels);
1644 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1649 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1650 void __user *useraddr)
1652 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1653 u16 from_channel, to_channel;
1654 u32 max_rx_in_use = 0;
1658 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1661 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1664 dev->ethtool_ops->get_channels(dev, &curr);
1666 /* ensure new counts are within the maximums */
1667 if (channels.rx_count > curr.max_rx ||
1668 channels.tx_count > curr.max_tx ||
1669 channels.combined_count > curr.max_combined ||
1670 channels.other_count > curr.max_other)
1673 /* ensure the new Rx count fits within the configured Rx flow
1674 * indirection table settings */
1675 if (netif_is_rxfh_configured(dev) &&
1676 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1677 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1680 /* Disabling channels, query zero-copy AF_XDP sockets */
1681 from_channel = channels.combined_count +
1682 min(channels.rx_count, channels.tx_count);
1683 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1684 for (i = from_channel; i < to_channel; i++)
1685 if (xdp_get_umem_from_qid(dev, i))
1688 ret = dev->ethtool_ops->set_channels(dev, &channels);
1690 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1694 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1696 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1698 if (!dev->ethtool_ops->get_pauseparam)
1701 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1703 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1708 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1710 struct ethtool_pauseparam pauseparam;
1713 if (!dev->ethtool_ops->set_pauseparam)
1716 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1719 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1721 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1725 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1727 struct ethtool_test test;
1728 const struct ethtool_ops *ops = dev->ethtool_ops;
1732 if (!ops->self_test || !ops->get_sset_count)
1735 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1738 WARN_ON(test_len == 0);
1740 if (copy_from_user(&test, useraddr, sizeof(test)))
1743 test.len = test_len;
1744 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1748 netif_testing_on(dev);
1749 ops->self_test(dev, &test, data);
1750 netif_testing_off(dev);
1753 if (copy_to_user(useraddr, &test, sizeof(test)))
1755 useraddr += sizeof(test);
1756 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1765 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1767 struct ethtool_gstrings gstrings;
1771 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1774 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1777 if (ret > S32_MAX / ETH_GSTRING_LEN)
1784 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1788 __ethtool_get_strings(dev, gstrings.string_set, data);
1794 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1796 useraddr += sizeof(gstrings);
1798 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1807 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1809 struct ethtool_value id;
1811 const struct ethtool_ops *ops = dev->ethtool_ops;
1814 if (!ops->set_phys_id)
1820 if (copy_from_user(&id, useraddr, sizeof(id)))
1823 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1827 /* Drop the RTNL lock while waiting, but prevent reentry or
1828 * removal of the device.
1835 /* Driver will handle this itself */
1836 schedule_timeout_interruptible(
1837 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1839 /* Driver expects to be called at twice the frequency in rc */
1840 int n = rc * 2, i, interval = HZ / n;
1842 /* Count down seconds */
1844 /* Count down iterations per second */
1848 rc = ops->set_phys_id(dev,
1849 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1853 schedule_timeout_interruptible(interval);
1854 } while (!signal_pending(current) && --i != 0);
1855 } while (!signal_pending(current) &&
1856 (id.data == 0 || --id.data != 0));
1863 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1867 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1869 struct ethtool_stats stats;
1870 const struct ethtool_ops *ops = dev->ethtool_ops;
1874 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1877 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1880 if (n_stats > S32_MAX / sizeof(u64))
1882 WARN_ON_ONCE(!n_stats);
1883 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1886 stats.n_stats = n_stats;
1889 data = vzalloc(array_size(n_stats, sizeof(u64)));
1892 ops->get_ethtool_stats(dev, &stats, data);
1898 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1900 useraddr += sizeof(stats);
1901 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1910 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1912 const struct ethtool_ops *ops = dev->ethtool_ops;
1913 struct phy_device *phydev = dev->phydev;
1914 struct ethtool_stats stats;
1918 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1921 if (dev->phydev && !ops->get_ethtool_phy_stats)
1922 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1924 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1927 if (n_stats > S32_MAX / sizeof(u64))
1929 WARN_ON_ONCE(!n_stats);
1931 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1934 stats.n_stats = n_stats;
1937 data = vzalloc(array_size(n_stats, sizeof(u64)));
1941 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1942 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1946 ops->get_ethtool_phy_stats(dev, &stats, data);
1953 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1955 useraddr += sizeof(stats);
1956 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1965 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1967 struct ethtool_perm_addr epaddr;
1969 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1972 if (epaddr.size < dev->addr_len)
1974 epaddr.size = dev->addr_len;
1976 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1978 useraddr += sizeof(epaddr);
1979 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1984 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1985 u32 cmd, u32 (*actor)(struct net_device *))
1987 struct ethtool_value edata = { .cmd = cmd };
1992 edata.data = actor(dev);
1994 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1999 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2000 void (*actor)(struct net_device *, u32))
2002 struct ethtool_value edata;
2007 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2010 actor(dev, edata.data);
2014 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2015 int (*actor)(struct net_device *, u32))
2017 struct ethtool_value edata;
2022 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2025 return actor(dev, edata.data);
2028 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2029 char __user *useraddr)
2031 struct ethtool_flash efl;
2033 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2035 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2037 if (!dev->ethtool_ops->flash_device)
2038 return devlink_compat_flash_update(dev, efl.data);
2040 return dev->ethtool_ops->flash_device(dev, &efl);
2043 static int ethtool_set_dump(struct net_device *dev,
2044 void __user *useraddr)
2046 struct ethtool_dump dump;
2048 if (!dev->ethtool_ops->set_dump)
2051 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2054 return dev->ethtool_ops->set_dump(dev, &dump);
2057 static int ethtool_get_dump_flag(struct net_device *dev,
2058 void __user *useraddr)
2061 struct ethtool_dump dump;
2062 const struct ethtool_ops *ops = dev->ethtool_ops;
2064 if (!ops->get_dump_flag)
2067 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2070 ret = ops->get_dump_flag(dev, &dump);
2074 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2079 static int ethtool_get_dump_data(struct net_device *dev,
2080 void __user *useraddr)
2084 struct ethtool_dump dump, tmp;
2085 const struct ethtool_ops *ops = dev->ethtool_ops;
2088 if (!ops->get_dump_data || !ops->get_dump_flag)
2091 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2094 memset(&tmp, 0, sizeof(tmp));
2095 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2096 ret = ops->get_dump_flag(dev, &tmp);
2100 len = min(tmp.len, dump.len);
2104 /* Don't ever let the driver think there's more space available
2105 * than it requested with .get_dump_flag().
2109 /* Always allocate enough space to hold the whole thing so that the
2110 * driver does not need to check the length and bother with partial
2113 data = vzalloc(tmp.len);
2116 ret = ops->get_dump_data(dev, &dump, data);
2120 /* There are two sane possibilities:
2121 * 1. The driver's .get_dump_data() does not touch dump.len.
2122 * 2. Or it may set dump.len to how much it really writes, which
2123 * should be tmp.len (or len if it can do a partial dump).
2124 * In any case respond to userspace with the actual length of data
2127 WARN_ON(dump.len != len && dump.len != tmp.len);
2130 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2134 useraddr += offsetof(struct ethtool_dump, data);
2135 if (copy_to_user(useraddr, data, len))
2142 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2144 struct ethtool_ts_info info;
2147 err = __ethtool_get_ts_info(dev, &info);
2151 if (copy_to_user(useraddr, &info, sizeof(info)))
2157 static int __ethtool_get_module_info(struct net_device *dev,
2158 struct ethtool_modinfo *modinfo)
2160 const struct ethtool_ops *ops = dev->ethtool_ops;
2161 struct phy_device *phydev = dev->phydev;
2164 return sfp_get_module_info(dev->sfp_bus, modinfo);
2166 if (phydev && phydev->drv && phydev->drv->module_info)
2167 return phydev->drv->module_info(phydev, modinfo);
2169 if (ops->get_module_info)
2170 return ops->get_module_info(dev, modinfo);
2175 static int ethtool_get_module_info(struct net_device *dev,
2176 void __user *useraddr)
2179 struct ethtool_modinfo modinfo;
2181 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2184 ret = __ethtool_get_module_info(dev, &modinfo);
2188 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2194 static int __ethtool_get_module_eeprom(struct net_device *dev,
2195 struct ethtool_eeprom *ee, u8 *data)
2197 const struct ethtool_ops *ops = dev->ethtool_ops;
2198 struct phy_device *phydev = dev->phydev;
2201 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2203 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2204 return phydev->drv->module_eeprom(phydev, ee, data);
2206 if (ops->get_module_eeprom)
2207 return ops->get_module_eeprom(dev, ee, data);
2212 static int ethtool_get_module_eeprom(struct net_device *dev,
2213 void __user *useraddr)
2216 struct ethtool_modinfo modinfo;
2218 ret = __ethtool_get_module_info(dev, &modinfo);
2222 return ethtool_get_any_eeprom(dev, useraddr,
2223 __ethtool_get_module_eeprom,
2224 modinfo.eeprom_len);
2227 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2230 case ETHTOOL_RX_COPYBREAK:
2231 case ETHTOOL_TX_COPYBREAK:
2232 if (tuna->len != sizeof(u32) ||
2233 tuna->type_id != ETHTOOL_TUNABLE_U32)
2236 case ETHTOOL_PFC_PREVENTION_TOUT:
2237 if (tuna->len != sizeof(u16) ||
2238 tuna->type_id != ETHTOOL_TUNABLE_U16)
2248 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2251 struct ethtool_tunable tuna;
2252 const struct ethtool_ops *ops = dev->ethtool_ops;
2255 if (!ops->get_tunable)
2257 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2259 ret = ethtool_tunable_valid(&tuna);
2262 data = kmalloc(tuna.len, GFP_USER);
2265 ret = ops->get_tunable(dev, &tuna, data);
2268 useraddr += sizeof(tuna);
2270 if (copy_to_user(useraddr, data, tuna.len))
2279 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2282 struct ethtool_tunable tuna;
2283 const struct ethtool_ops *ops = dev->ethtool_ops;
2286 if (!ops->set_tunable)
2288 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2290 ret = ethtool_tunable_valid(&tuna);
2293 useraddr += sizeof(tuna);
2294 data = memdup_user(useraddr, tuna.len);
2296 return PTR_ERR(data);
2297 ret = ops->set_tunable(dev, &tuna, data);
2303 static noinline_for_stack int
2304 ethtool_get_per_queue_coalesce(struct net_device *dev,
2305 void __user *useraddr,
2306 struct ethtool_per_queue_op *per_queue_opt)
2310 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2312 if (!dev->ethtool_ops->get_per_queue_coalesce)
2315 useraddr += sizeof(*per_queue_opt);
2317 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2320 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2321 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2323 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2326 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2328 useraddr += sizeof(coalesce);
2334 static noinline_for_stack int
2335 ethtool_set_per_queue_coalesce(struct net_device *dev,
2336 void __user *useraddr,
2337 struct ethtool_per_queue_op *per_queue_opt)
2342 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2343 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2345 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2346 (!dev->ethtool_ops->get_per_queue_coalesce))
2349 useraddr += sizeof(*per_queue_opt);
2351 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2352 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2353 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2357 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2358 struct ethtool_coalesce coalesce;
2360 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2366 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2371 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2376 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2380 useraddr += sizeof(coalesce);
2386 for_each_set_bit(i, queue_mask, bit) {
2387 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2396 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2397 void __user *useraddr, u32 sub_cmd)
2399 struct ethtool_per_queue_op per_queue_opt;
2401 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2404 if (per_queue_opt.sub_command != sub_cmd)
2407 switch (per_queue_opt.sub_command) {
2408 case ETHTOOL_GCOALESCE:
2409 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2410 case ETHTOOL_SCOALESCE:
2411 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2417 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2420 case ETHTOOL_PHY_DOWNSHIFT:
2421 case ETHTOOL_PHY_FAST_LINK_DOWN:
2422 if (tuna->len != sizeof(u8) ||
2423 tuna->type_id != ETHTOOL_TUNABLE_U8)
2426 case ETHTOOL_PHY_EDPD:
2427 if (tuna->len != sizeof(u16) ||
2428 tuna->type_id != ETHTOOL_TUNABLE_U16)
2438 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2441 struct ethtool_tunable tuna;
2442 struct phy_device *phydev = dev->phydev;
2445 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2448 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2450 ret = ethtool_phy_tunable_valid(&tuna);
2453 data = kmalloc(tuna.len, GFP_USER);
2456 mutex_lock(&phydev->lock);
2457 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2458 mutex_unlock(&phydev->lock);
2461 useraddr += sizeof(tuna);
2463 if (copy_to_user(useraddr, data, tuna.len))
2472 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2475 struct ethtool_tunable tuna;
2476 struct phy_device *phydev = dev->phydev;
2479 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2481 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2483 ret = ethtool_phy_tunable_valid(&tuna);
2486 useraddr += sizeof(tuna);
2487 data = memdup_user(useraddr, tuna.len);
2489 return PTR_ERR(data);
2490 mutex_lock(&phydev->lock);
2491 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2492 mutex_unlock(&phydev->lock);
2498 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2500 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2503 if (!dev->ethtool_ops->get_fecparam)
2506 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2510 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2515 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2517 struct ethtool_fecparam fecparam;
2519 if (!dev->ethtool_ops->set_fecparam)
2522 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2525 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2528 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2530 int dev_ethtool(struct net *net, struct ifreq *ifr)
2532 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2533 void __user *useraddr = ifr->ifr_data;
2534 u32 ethcmd, sub_cmd;
2536 netdev_features_t old_features;
2538 if (!dev || !netif_device_present(dev))
2541 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2544 if (ethcmd == ETHTOOL_PERQUEUE) {
2545 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2550 /* Allow some commands to be done by anyone */
2553 case ETHTOOL_GDRVINFO:
2554 case ETHTOOL_GMSGLVL:
2556 case ETHTOOL_GCOALESCE:
2557 case ETHTOOL_GRINGPARAM:
2558 case ETHTOOL_GPAUSEPARAM:
2559 case ETHTOOL_GRXCSUM:
2560 case ETHTOOL_GTXCSUM:
2562 case ETHTOOL_GSSET_INFO:
2563 case ETHTOOL_GSTRINGS:
2564 case ETHTOOL_GSTATS:
2565 case ETHTOOL_GPHYSTATS:
2567 case ETHTOOL_GPERMADDR:
2571 case ETHTOOL_GFLAGS:
2572 case ETHTOOL_GPFLAGS:
2574 case ETHTOOL_GRXRINGS:
2575 case ETHTOOL_GRXCLSRLCNT:
2576 case ETHTOOL_GRXCLSRULE:
2577 case ETHTOOL_GRXCLSRLALL:
2578 case ETHTOOL_GRXFHINDIR:
2580 case ETHTOOL_GFEATURES:
2581 case ETHTOOL_GCHANNELS:
2582 case ETHTOOL_GET_TS_INFO:
2584 case ETHTOOL_GTUNABLE:
2585 case ETHTOOL_PHY_GTUNABLE:
2586 case ETHTOOL_GLINKSETTINGS:
2587 case ETHTOOL_GFECPARAM:
2590 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2594 if (dev->ethtool_ops->begin) {
2595 rc = dev->ethtool_ops->begin(dev);
2599 old_features = dev->features;
2603 rc = ethtool_get_settings(dev, useraddr);
2606 rc = ethtool_set_settings(dev, useraddr);
2608 case ETHTOOL_GDRVINFO:
2609 rc = ethtool_get_drvinfo(dev, useraddr);
2612 rc = ethtool_get_regs(dev, useraddr);
2615 rc = ethtool_get_wol(dev, useraddr);
2618 rc = ethtool_set_wol(dev, useraddr);
2620 case ETHTOOL_GMSGLVL:
2621 rc = ethtool_get_value(dev, useraddr, ethcmd,
2622 dev->ethtool_ops->get_msglevel);
2624 case ETHTOOL_SMSGLVL:
2625 rc = ethtool_set_value_void(dev, useraddr,
2626 dev->ethtool_ops->set_msglevel);
2628 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2631 rc = ethtool_get_eee(dev, useraddr);
2634 rc = ethtool_set_eee(dev, useraddr);
2636 case ETHTOOL_NWAY_RST:
2637 rc = ethtool_nway_reset(dev);
2640 rc = ethtool_get_link(dev, useraddr);
2642 case ETHTOOL_GEEPROM:
2643 rc = ethtool_get_eeprom(dev, useraddr);
2645 case ETHTOOL_SEEPROM:
2646 rc = ethtool_set_eeprom(dev, useraddr);
2648 case ETHTOOL_GCOALESCE:
2649 rc = ethtool_get_coalesce(dev, useraddr);
2651 case ETHTOOL_SCOALESCE:
2652 rc = ethtool_set_coalesce(dev, useraddr);
2654 case ETHTOOL_GRINGPARAM:
2655 rc = ethtool_get_ringparam(dev, useraddr);
2657 case ETHTOOL_SRINGPARAM:
2658 rc = ethtool_set_ringparam(dev, useraddr);
2660 case ETHTOOL_GPAUSEPARAM:
2661 rc = ethtool_get_pauseparam(dev, useraddr);
2663 case ETHTOOL_SPAUSEPARAM:
2664 rc = ethtool_set_pauseparam(dev, useraddr);
2667 rc = ethtool_self_test(dev, useraddr);
2669 case ETHTOOL_GSTRINGS:
2670 rc = ethtool_get_strings(dev, useraddr);
2672 case ETHTOOL_PHYS_ID:
2673 rc = ethtool_phys_id(dev, useraddr);
2675 case ETHTOOL_GSTATS:
2676 rc = ethtool_get_stats(dev, useraddr);
2678 case ETHTOOL_GPERMADDR:
2679 rc = ethtool_get_perm_addr(dev, useraddr);
2681 case ETHTOOL_GFLAGS:
2682 rc = ethtool_get_value(dev, useraddr, ethcmd,
2683 __ethtool_get_flags);
2685 case ETHTOOL_SFLAGS:
2686 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2688 case ETHTOOL_GPFLAGS:
2689 rc = ethtool_get_value(dev, useraddr, ethcmd,
2690 dev->ethtool_ops->get_priv_flags);
2692 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2694 case ETHTOOL_SPFLAGS:
2695 rc = ethtool_set_value(dev, useraddr,
2696 dev->ethtool_ops->set_priv_flags);
2699 case ETHTOOL_GRXRINGS:
2700 case ETHTOOL_GRXCLSRLCNT:
2701 case ETHTOOL_GRXCLSRULE:
2702 case ETHTOOL_GRXCLSRLALL:
2703 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2706 case ETHTOOL_SRXCLSRLDEL:
2707 case ETHTOOL_SRXCLSRLINS:
2708 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2710 case ETHTOOL_FLASHDEV:
2711 rc = ethtool_flash_device(dev, useraddr);
2714 rc = ethtool_reset(dev, useraddr);
2716 case ETHTOOL_GSSET_INFO:
2717 rc = ethtool_get_sset_info(dev, useraddr);
2719 case ETHTOOL_GRXFHINDIR:
2720 rc = ethtool_get_rxfh_indir(dev, useraddr);
2722 case ETHTOOL_SRXFHINDIR:
2723 rc = ethtool_set_rxfh_indir(dev, useraddr);
2726 rc = ethtool_get_rxfh(dev, useraddr);
2729 rc = ethtool_set_rxfh(dev, useraddr);
2731 case ETHTOOL_GFEATURES:
2732 rc = ethtool_get_features(dev, useraddr);
2734 case ETHTOOL_SFEATURES:
2735 rc = ethtool_set_features(dev, useraddr);
2737 case ETHTOOL_GTXCSUM:
2738 case ETHTOOL_GRXCSUM:
2743 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2745 case ETHTOOL_STXCSUM:
2746 case ETHTOOL_SRXCSUM:
2751 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2753 case ETHTOOL_GCHANNELS:
2754 rc = ethtool_get_channels(dev, useraddr);
2756 case ETHTOOL_SCHANNELS:
2757 rc = ethtool_set_channels(dev, useraddr);
2759 case ETHTOOL_SET_DUMP:
2760 rc = ethtool_set_dump(dev, useraddr);
2762 case ETHTOOL_GET_DUMP_FLAG:
2763 rc = ethtool_get_dump_flag(dev, useraddr);
2765 case ETHTOOL_GET_DUMP_DATA:
2766 rc = ethtool_get_dump_data(dev, useraddr);
2768 case ETHTOOL_GET_TS_INFO:
2769 rc = ethtool_get_ts_info(dev, useraddr);
2771 case ETHTOOL_GMODULEINFO:
2772 rc = ethtool_get_module_info(dev, useraddr);
2774 case ETHTOOL_GMODULEEEPROM:
2775 rc = ethtool_get_module_eeprom(dev, useraddr);
2777 case ETHTOOL_GTUNABLE:
2778 rc = ethtool_get_tunable(dev, useraddr);
2780 case ETHTOOL_STUNABLE:
2781 rc = ethtool_set_tunable(dev, useraddr);
2783 case ETHTOOL_GPHYSTATS:
2784 rc = ethtool_get_phy_stats(dev, useraddr);
2786 case ETHTOOL_PERQUEUE:
2787 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2789 case ETHTOOL_GLINKSETTINGS:
2790 rc = ethtool_get_link_ksettings(dev, useraddr);
2792 case ETHTOOL_SLINKSETTINGS:
2793 rc = ethtool_set_link_ksettings(dev, useraddr);
2795 case ETHTOOL_PHY_GTUNABLE:
2796 rc = get_phy_tunable(dev, useraddr);
2798 case ETHTOOL_PHY_STUNABLE:
2799 rc = set_phy_tunable(dev, useraddr);
2801 case ETHTOOL_GFECPARAM:
2802 rc = ethtool_get_fecparam(dev, useraddr);
2804 case ETHTOOL_SFECPARAM:
2805 rc = ethtool_set_fecparam(dev, useraddr);
2811 if (dev->ethtool_ops->complete)
2812 dev->ethtool_ops->complete(dev);
2814 if (old_features != dev->features)
2815 netdev_features_change(dev);
2820 struct ethtool_rx_flow_key {
2821 struct flow_dissector_key_basic basic;
2823 struct flow_dissector_key_ipv4_addrs ipv4;
2824 struct flow_dissector_key_ipv6_addrs ipv6;
2826 struct flow_dissector_key_ports tp;
2827 struct flow_dissector_key_ip ip;
2828 struct flow_dissector_key_vlan vlan;
2829 struct flow_dissector_key_eth_addrs eth_addrs;
2830 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2832 struct ethtool_rx_flow_match {
2833 struct flow_dissector dissector;
2834 struct ethtool_rx_flow_key key;
2835 struct ethtool_rx_flow_key mask;
2838 struct ethtool_rx_flow_rule *
2839 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2841 const struct ethtool_rx_flow_spec *fs = input->fs;
2842 static struct in6_addr zero_addr = {};
2843 struct ethtool_rx_flow_match *match;
2844 struct ethtool_rx_flow_rule *flow;
2845 struct flow_action_entry *act;
2847 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2848 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2850 return ERR_PTR(-ENOMEM);
2852 /* ethtool_rx supports only one single action per rule. */
2853 flow->rule = flow_rule_alloc(1);
2856 return ERR_PTR(-ENOMEM);
2859 match = (struct ethtool_rx_flow_match *)flow->priv;
2860 flow->rule->match.dissector = &match->dissector;
2861 flow->rule->match.mask = &match->mask;
2862 flow->rule->match.key = &match->key;
2864 match->mask.basic.n_proto = htons(0xffff);
2866 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2868 const struct ethhdr *ether_spec, *ether_m_spec;
2870 ether_spec = &fs->h_u.ether_spec;
2871 ether_m_spec = &fs->m_u.ether_spec;
2873 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2874 ether_addr_copy(match->key.eth_addrs.src,
2875 ether_spec->h_source);
2876 ether_addr_copy(match->mask.eth_addrs.src,
2877 ether_m_spec->h_source);
2879 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2880 ether_addr_copy(match->key.eth_addrs.dst,
2881 ether_spec->h_dest);
2882 ether_addr_copy(match->mask.eth_addrs.dst,
2883 ether_m_spec->h_dest);
2885 if (ether_m_spec->h_proto) {
2886 match->key.basic.n_proto = ether_spec->h_proto;
2887 match->mask.basic.n_proto = ether_m_spec->h_proto;
2893 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2895 match->key.basic.n_proto = htons(ETH_P_IP);
2897 v4_spec = &fs->h_u.tcp_ip4_spec;
2898 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2900 if (v4_m_spec->ip4src) {
2901 match->key.ipv4.src = v4_spec->ip4src;
2902 match->mask.ipv4.src = v4_m_spec->ip4src;
2904 if (v4_m_spec->ip4dst) {
2905 match->key.ipv4.dst = v4_spec->ip4dst;
2906 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2908 if (v4_m_spec->ip4src ||
2909 v4_m_spec->ip4dst) {
2910 match->dissector.used_keys |=
2911 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2912 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2913 offsetof(struct ethtool_rx_flow_key, ipv4);
2915 if (v4_m_spec->psrc) {
2916 match->key.tp.src = v4_spec->psrc;
2917 match->mask.tp.src = v4_m_spec->psrc;
2919 if (v4_m_spec->pdst) {
2920 match->key.tp.dst = v4_spec->pdst;
2921 match->mask.tp.dst = v4_m_spec->pdst;
2923 if (v4_m_spec->psrc ||
2925 match->dissector.used_keys |=
2926 BIT(FLOW_DISSECTOR_KEY_PORTS);
2927 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2928 offsetof(struct ethtool_rx_flow_key, tp);
2930 if (v4_m_spec->tos) {
2931 match->key.ip.tos = v4_spec->tos;
2932 match->mask.ip.tos = v4_m_spec->tos;
2933 match->dissector.used_keys |=
2934 BIT(FLOW_DISSECTOR_KEY_IP);
2935 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2936 offsetof(struct ethtool_rx_flow_key, ip);
2942 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2944 match->key.basic.n_proto = htons(ETH_P_IPV6);
2946 v6_spec = &fs->h_u.tcp_ip6_spec;
2947 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2948 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2949 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2950 sizeof(match->key.ipv6.src));
2951 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2952 sizeof(match->mask.ipv6.src));
2954 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2955 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2956 sizeof(match->key.ipv6.dst));
2957 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2958 sizeof(match->mask.ipv6.dst));
2960 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2961 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2962 match->dissector.used_keys |=
2963 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2964 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2965 offsetof(struct ethtool_rx_flow_key, ipv6);
2967 if (v6_m_spec->psrc) {
2968 match->key.tp.src = v6_spec->psrc;
2969 match->mask.tp.src = v6_m_spec->psrc;
2971 if (v6_m_spec->pdst) {
2972 match->key.tp.dst = v6_spec->pdst;
2973 match->mask.tp.dst = v6_m_spec->pdst;
2975 if (v6_m_spec->psrc ||
2977 match->dissector.used_keys |=
2978 BIT(FLOW_DISSECTOR_KEY_PORTS);
2979 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2980 offsetof(struct ethtool_rx_flow_key, tp);
2982 if (v6_m_spec->tclass) {
2983 match->key.ip.tos = v6_spec->tclass;
2984 match->mask.ip.tos = v6_m_spec->tclass;
2985 match->dissector.used_keys |=
2986 BIT(FLOW_DISSECTOR_KEY_IP);
2987 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2988 offsetof(struct ethtool_rx_flow_key, ip);
2993 ethtool_rx_flow_rule_destroy(flow);
2994 return ERR_PTR(-EINVAL);
2997 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3000 match->key.basic.ip_proto = IPPROTO_TCP;
3004 match->key.basic.ip_proto = IPPROTO_UDP;
3007 match->mask.basic.ip_proto = 0xff;
3009 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3010 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3011 offsetof(struct ethtool_rx_flow_key, basic);
3013 if (fs->flow_type & FLOW_EXT) {
3014 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3015 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3017 if (ext_m_spec->vlan_etype) {
3018 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3019 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3022 if (ext_m_spec->vlan_tci) {
3023 match->key.vlan.vlan_id =
3024 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3025 match->mask.vlan.vlan_id =
3026 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3028 match->key.vlan.vlan_dei =
3029 !!(ext_h_spec->vlan_tci & htons(0x1000));
3030 match->mask.vlan.vlan_dei =
3031 !!(ext_m_spec->vlan_tci & htons(0x1000));
3033 match->key.vlan.vlan_priority =
3034 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3035 match->mask.vlan.vlan_priority =
3036 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3039 if (ext_m_spec->vlan_etype ||
3040 ext_m_spec->vlan_tci) {
3041 match->dissector.used_keys |=
3042 BIT(FLOW_DISSECTOR_KEY_VLAN);
3043 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3044 offsetof(struct ethtool_rx_flow_key, vlan);
3047 if (fs->flow_type & FLOW_MAC_EXT) {
3048 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3049 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3051 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3053 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3056 match->dissector.used_keys |=
3057 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3058 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3059 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3062 act = &flow->rule->action.entries[0];
3063 switch (fs->ring_cookie) {
3064 case RX_CLS_FLOW_DISC:
3065 act->id = FLOW_ACTION_DROP;
3067 case RX_CLS_FLOW_WAKE:
3068 act->id = FLOW_ACTION_WAKE;
3071 act->id = FLOW_ACTION_QUEUE;
3072 if (fs->flow_type & FLOW_RSS)
3073 act->queue.ctx = input->rss_ctx;
3075 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3076 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3082 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3084 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3089 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);