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;
555 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
556 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
558 return store_link_ksettings_for_user(useraddr, &link_ksettings);
561 /* Update device ethtool_link_settings. */
562 static int ethtool_set_link_ksettings(struct net_device *dev,
563 void __user *useraddr)
566 struct ethtool_link_ksettings link_ksettings;
570 if (!dev->ethtool_ops->set_link_ksettings)
573 /* make sure nbits field has expected value */
574 if (copy_from_user(&link_ksettings.base, useraddr,
575 sizeof(link_ksettings.base)))
578 if (__ETHTOOL_LINK_MODE_MASK_NU32
579 != link_ksettings.base.link_mode_masks_nwords)
582 /* copy the whole structure, now that we know it has expected
585 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
589 /* re-check nwords field, just in case */
590 if (__ETHTOOL_LINK_MODE_MASK_NU32
591 != link_ksettings.base.link_mode_masks_nwords)
594 if (link_ksettings.base.master_slave_cfg ||
595 link_ksettings.base.master_slave_state)
598 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
600 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
601 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
606 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
607 const struct ethtool_link_ksettings *cmd,
608 u32 *dev_speed, u8 *dev_duplex)
613 speed = cmd->base.speed;
614 duplex = cmd->base.duplex;
615 /* don't allow custom speed and duplex */
616 if (!ethtool_validate_speed(speed) ||
617 !ethtool_validate_duplex(duplex) ||
618 !ethtool_virtdev_validate_cmd(cmd))
621 *dev_duplex = duplex;
625 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
627 /* Query device for its ethtool_cmd settings.
629 * Backward compatibility note: for compatibility with legacy ethtool, this is
630 * now implemented via get_link_ksettings. When driver reports higher link mode
631 * bits, a kernel warning is logged once (with name of 1st driver/device) to
632 * recommend user to upgrade ethtool, but the command is successful (only the
633 * lower link mode bits reported back to user). Deprecated fields from
634 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
636 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
638 struct ethtool_link_ksettings link_ksettings;
639 struct ethtool_cmd cmd;
643 if (!dev->ethtool_ops->get_link_ksettings)
646 memset(&link_ksettings, 0, sizeof(link_ksettings));
647 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
650 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
652 /* send a sensible cmd tag back to user */
653 cmd.cmd = ETHTOOL_GSET;
655 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
661 /* Update device link settings with given ethtool_cmd.
663 * Backward compatibility note: for compatibility with legacy ethtool, this is
664 * now always implemented via set_link_settings. When user's request updates
665 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
666 * warning is logged once (with name of 1st driver/device) to recommend user to
667 * upgrade ethtool, and the request is rejected.
669 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
671 struct ethtool_link_ksettings link_ksettings;
672 struct ethtool_cmd cmd;
677 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
679 if (!dev->ethtool_ops->set_link_ksettings)
682 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
684 link_ksettings.base.link_mode_masks_nwords =
685 __ETHTOOL_LINK_MODE_MASK_NU32;
686 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
688 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
689 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
694 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
695 void __user *useraddr)
697 struct ethtool_drvinfo info;
698 const struct ethtool_ops *ops = dev->ethtool_ops;
700 memset(&info, 0, sizeof(info));
701 info.cmd = ETHTOOL_GDRVINFO;
702 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
703 if (ops->get_drvinfo) {
704 ops->get_drvinfo(dev, &info);
705 } else if (dev->dev.parent && dev->dev.parent->driver) {
706 strlcpy(info.bus_info, dev_name(dev->dev.parent),
707 sizeof(info.bus_info));
708 strlcpy(info.driver, dev->dev.parent->driver->name,
709 sizeof(info.driver));
715 * this method of obtaining string set info is deprecated;
716 * Use ETHTOOL_GSSET_INFO instead.
718 if (ops->get_sset_count) {
721 rc = ops->get_sset_count(dev, ETH_SS_TEST);
723 info.testinfo_len = rc;
724 rc = ops->get_sset_count(dev, ETH_SS_STATS);
727 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
729 info.n_priv_flags = rc;
731 if (ops->get_regs_len) {
732 int ret = ops->get_regs_len(dev);
735 info.regdump_len = ret;
738 if (ops->get_eeprom_len)
739 info.eedump_len = ops->get_eeprom_len(dev);
741 if (!info.fw_version[0])
742 devlink_compat_running_version(dev, info.fw_version,
743 sizeof(info.fw_version));
745 if (copy_to_user(useraddr, &info, sizeof(info)))
750 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
751 void __user *useraddr)
753 struct ethtool_sset_info info;
755 int i, idx = 0, n_bits = 0, ret, rc;
756 u32 *info_buf = NULL;
758 if (copy_from_user(&info, useraddr, sizeof(info)))
761 /* store copy of mask, because we zero struct later on */
762 sset_mask = info.sset_mask;
766 /* calculate size of return buffer */
767 n_bits = hweight64(sset_mask);
769 memset(&info, 0, sizeof(info));
770 info.cmd = ETHTOOL_GSSET_INFO;
772 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
777 * fill return buffer based on input bitmask and successful
778 * get_sset_count return
780 for (i = 0; i < 64; i++) {
781 if (!(sset_mask & (1ULL << i)))
784 rc = __ethtool_get_sset_count(dev, i);
786 info.sset_mask |= (1ULL << i);
787 info_buf[idx++] = rc;
792 if (copy_to_user(useraddr, &info, sizeof(info)))
795 useraddr += offsetof(struct ethtool_sset_info, data);
796 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
806 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
807 u32 cmd, void __user *useraddr)
809 struct ethtool_rxnfc info;
810 size_t info_size = sizeof(info);
813 if (!dev->ethtool_ops->set_rxnfc)
816 /* struct ethtool_rxnfc was originally defined for
817 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
818 * members. User-space might still be using that
820 if (cmd == ETHTOOL_SRXFH)
821 info_size = (offsetof(struct ethtool_rxnfc, data) +
824 if (copy_from_user(&info, useraddr, info_size))
827 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
831 if (cmd == ETHTOOL_SRXCLSRLINS &&
832 copy_to_user(useraddr, &info, info_size))
838 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
839 u32 cmd, void __user *useraddr)
841 struct ethtool_rxnfc info;
842 size_t info_size = sizeof(info);
843 const struct ethtool_ops *ops = dev->ethtool_ops;
845 void *rule_buf = NULL;
850 /* struct ethtool_rxnfc was originally defined for
851 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
852 * members. User-space might still be using that
854 if (cmd == ETHTOOL_GRXFH)
855 info_size = (offsetof(struct ethtool_rxnfc, data) +
858 if (copy_from_user(&info, useraddr, info_size))
861 /* If FLOW_RSS was requested then user-space must be using the
862 * new definition, as FLOW_RSS is newer.
864 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
865 info_size = sizeof(info);
866 if (copy_from_user(&info, useraddr, info_size))
868 /* Since malicious users may modify the original data,
869 * we need to check whether FLOW_RSS is still requested.
871 if (!(info.flow_type & FLOW_RSS))
878 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
879 if (info.rule_cnt > 0) {
880 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
881 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
888 ret = ops->get_rxnfc(dev, &info, rule_buf);
893 if (copy_to_user(useraddr, &info, info_size))
897 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
898 if (copy_to_user(useraddr, rule_buf,
899 info.rule_cnt * sizeof(u32)))
910 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
911 struct ethtool_rxnfc *rx_rings,
916 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
919 /* Validate ring indices */
920 for (i = 0; i < size; i++)
921 if (indir[i] >= rx_rings->data)
927 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
929 void netdev_rss_key_fill(void *buffer, size_t len)
931 BUG_ON(len > sizeof(netdev_rss_key));
932 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
933 memcpy(buffer, netdev_rss_key, len);
935 EXPORT_SYMBOL(netdev_rss_key_fill);
937 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
938 void __user *useraddr)
940 u32 user_size, dev_size;
944 if (!dev->ethtool_ops->get_rxfh_indir_size ||
945 !dev->ethtool_ops->get_rxfh)
947 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
951 if (copy_from_user(&user_size,
952 useraddr + offsetof(struct ethtool_rxfh_indir, size),
956 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
957 &dev_size, sizeof(dev_size)))
960 /* If the user buffer size is 0, this is just a query for the
961 * device table size. Otherwise, if it's smaller than the
962 * device table size it's an error.
964 if (user_size < dev_size)
965 return user_size == 0 ? 0 : -EINVAL;
967 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
971 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
975 if (copy_to_user(useraddr +
976 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
977 indir, dev_size * sizeof(indir[0])))
985 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
986 void __user *useraddr)
988 struct ethtool_rxnfc rx_rings;
989 u32 user_size, dev_size, i;
991 const struct ethtool_ops *ops = dev->ethtool_ops;
993 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
995 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
999 dev_size = ops->get_rxfh_indir_size(dev);
1003 if (copy_from_user(&user_size,
1004 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1008 if (user_size != 0 && user_size != dev_size)
1011 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1015 rx_rings.cmd = ETHTOOL_GRXRINGS;
1016 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1020 if (user_size == 0) {
1021 for (i = 0; i < dev_size; i++)
1022 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1024 ret = ethtool_copy_validate_indir(indir,
1025 useraddr + ringidx_offset,
1032 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1036 /* indicate whether rxfh was set to default */
1038 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1040 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1047 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1048 void __user *useraddr)
1051 const struct ethtool_ops *ops = dev->ethtool_ops;
1052 u32 user_indir_size, user_key_size;
1053 u32 dev_indir_size = 0, dev_key_size = 0;
1054 struct ethtool_rxfh rxfh;
1065 if (ops->get_rxfh_indir_size)
1066 dev_indir_size = ops->get_rxfh_indir_size(dev);
1067 if (ops->get_rxfh_key_size)
1068 dev_key_size = ops->get_rxfh_key_size(dev);
1070 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1072 user_indir_size = rxfh.indir_size;
1073 user_key_size = rxfh.key_size;
1075 /* Check that reserved fields are 0 for now */
1076 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1078 /* Most drivers don't handle rss_context, check it's 0 as well */
1079 if (rxfh.rss_context && !ops->get_rxfh_context)
1082 rxfh.indir_size = dev_indir_size;
1083 rxfh.key_size = dev_key_size;
1084 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1087 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1088 (user_key_size && (user_key_size != dev_key_size)))
1091 indir_bytes = user_indir_size * sizeof(indir[0]);
1092 total_size = indir_bytes + user_key_size;
1093 rss_config = kzalloc(total_size, GFP_USER);
1097 if (user_indir_size)
1098 indir = (u32 *)rss_config;
1101 hkey = rss_config + indir_bytes;
1103 if (rxfh.rss_context)
1104 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1108 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1112 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1113 &dev_hfunc, sizeof(rxfh.hfunc))) {
1115 } else if (copy_to_user(useraddr +
1116 offsetof(struct ethtool_rxfh, rss_config[0]),
1117 rss_config, total_size)) {
1126 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1127 void __user *useraddr)
1130 const struct ethtool_ops *ops = dev->ethtool_ops;
1131 struct ethtool_rxnfc rx_rings;
1132 struct ethtool_rxfh rxfh;
1133 u32 dev_indir_size = 0, dev_key_size = 0, i;
1134 u32 *indir = NULL, indir_bytes = 0;
1137 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1138 bool delete = false;
1140 if (!ops->get_rxnfc || !ops->set_rxfh)
1143 if (ops->get_rxfh_indir_size)
1144 dev_indir_size = ops->get_rxfh_indir_size(dev);
1145 if (ops->get_rxfh_key_size)
1146 dev_key_size = ops->get_rxfh_key_size(dev);
1148 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1151 /* Check that reserved fields are 0 for now */
1152 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1154 /* Most drivers don't handle rss_context, check it's 0 as well */
1155 if (rxfh.rss_context && !ops->set_rxfh_context)
1158 /* If either indir, hash key or function is valid, proceed further.
1159 * Must request at least one change: indir size, hash key or function.
1161 if ((rxfh.indir_size &&
1162 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1163 rxfh.indir_size != dev_indir_size) ||
1164 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1165 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1166 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1169 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1170 indir_bytes = dev_indir_size * sizeof(indir[0]);
1172 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1176 rx_rings.cmd = ETHTOOL_GRXRINGS;
1177 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1181 /* rxfh.indir_size == 0 means reset the indir table to default (master
1182 * context) or delete the context (other RSS contexts).
1183 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1185 if (rxfh.indir_size &&
1186 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1187 indir = (u32 *)rss_config;
1188 ret = ethtool_copy_validate_indir(indir,
1189 useraddr + rss_cfg_offset,
1194 } else if (rxfh.indir_size == 0) {
1195 if (rxfh.rss_context == 0) {
1196 indir = (u32 *)rss_config;
1197 for (i = 0; i < dev_indir_size; i++)
1198 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1204 if (rxfh.key_size) {
1205 hkey = rss_config + indir_bytes;
1206 if (copy_from_user(hkey,
1207 useraddr + rss_cfg_offset + indir_bytes,
1214 if (rxfh.rss_context)
1215 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1216 &rxfh.rss_context, delete);
1218 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1222 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1223 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1226 if (!rxfh.rss_context) {
1227 /* indicate whether rxfh was set to default */
1228 if (rxfh.indir_size == 0)
1229 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1230 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1231 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1239 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1241 struct ethtool_regs regs;
1242 const struct ethtool_ops *ops = dev->ethtool_ops;
1246 if (!ops->get_regs || !ops->get_regs_len)
1249 if (copy_from_user(®s, useraddr, sizeof(regs)))
1252 reglen = ops->get_regs_len(dev);
1256 if (regs.len > reglen)
1259 regbuf = vzalloc(reglen);
1263 if (regs.len < reglen)
1266 ops->get_regs(dev, ®s, regbuf);
1269 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1271 useraddr += offsetof(struct ethtool_regs, data);
1272 if (copy_to_user(useraddr, regbuf, reglen))
1281 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1283 struct ethtool_value reset;
1286 if (!dev->ethtool_ops->reset)
1289 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1292 ret = dev->ethtool_ops->reset(dev, &reset.data);
1296 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1301 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1303 struct ethtool_wolinfo wol;
1305 if (!dev->ethtool_ops->get_wol)
1308 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1309 wol.cmd = ETHTOOL_GWOL;
1310 dev->ethtool_ops->get_wol(dev, &wol);
1312 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1317 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1319 struct ethtool_wolinfo wol;
1322 if (!dev->ethtool_ops->set_wol)
1325 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1328 ret = dev->ethtool_ops->set_wol(dev, &wol);
1332 dev->wol_enabled = !!wol.wolopts;
1333 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1338 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1340 struct ethtool_eee edata;
1343 if (!dev->ethtool_ops->get_eee)
1346 memset(&edata, 0, sizeof(struct ethtool_eee));
1347 edata.cmd = ETHTOOL_GEEE;
1348 rc = dev->ethtool_ops->get_eee(dev, &edata);
1353 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1359 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1361 struct ethtool_eee edata;
1364 if (!dev->ethtool_ops->set_eee)
1367 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1370 ret = dev->ethtool_ops->set_eee(dev, &edata);
1372 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1376 static int ethtool_nway_reset(struct net_device *dev)
1378 if (!dev->ethtool_ops->nway_reset)
1381 return dev->ethtool_ops->nway_reset(dev);
1384 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1386 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1387 int link = __ethtool_get_link(dev);
1393 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1398 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1399 int (*getter)(struct net_device *,
1400 struct ethtool_eeprom *, u8 *),
1403 struct ethtool_eeprom eeprom;
1404 void __user *userbuf = useraddr + sizeof(eeprom);
1405 u32 bytes_remaining;
1409 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1412 /* Check for wrap and zero */
1413 if (eeprom.offset + eeprom.len <= eeprom.offset)
1416 /* Check for exceeding total eeprom len */
1417 if (eeprom.offset + eeprom.len > total_len)
1420 data = kmalloc(PAGE_SIZE, GFP_USER);
1424 bytes_remaining = eeprom.len;
1425 while (bytes_remaining > 0) {
1426 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1428 ret = getter(dev, &eeprom, data);
1431 if (copy_to_user(userbuf, data, eeprom.len)) {
1435 userbuf += eeprom.len;
1436 eeprom.offset += eeprom.len;
1437 bytes_remaining -= eeprom.len;
1440 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1441 eeprom.offset -= eeprom.len;
1442 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1449 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1451 const struct ethtool_ops *ops = dev->ethtool_ops;
1453 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1454 !ops->get_eeprom_len(dev))
1457 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1458 ops->get_eeprom_len(dev));
1461 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1463 struct ethtool_eeprom eeprom;
1464 const struct ethtool_ops *ops = dev->ethtool_ops;
1465 void __user *userbuf = useraddr + sizeof(eeprom);
1466 u32 bytes_remaining;
1470 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1471 !ops->get_eeprom_len(dev))
1474 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1477 /* Check for wrap and zero */
1478 if (eeprom.offset + eeprom.len <= eeprom.offset)
1481 /* Check for exceeding total eeprom len */
1482 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1485 data = kmalloc(PAGE_SIZE, GFP_USER);
1489 bytes_remaining = eeprom.len;
1490 while (bytes_remaining > 0) {
1491 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1493 if (copy_from_user(data, userbuf, eeprom.len)) {
1497 ret = ops->set_eeprom(dev, &eeprom, data);
1500 userbuf += eeprom.len;
1501 eeprom.offset += eeprom.len;
1502 bytes_remaining -= eeprom.len;
1509 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1510 void __user *useraddr)
1512 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1514 if (!dev->ethtool_ops->get_coalesce)
1517 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1519 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1525 ethtool_set_coalesce_supported(struct net_device *dev,
1526 struct ethtool_coalesce *coalesce)
1528 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1529 u32 nonzero_params = 0;
1531 if (coalesce->rx_coalesce_usecs)
1532 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1533 if (coalesce->rx_max_coalesced_frames)
1534 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1535 if (coalesce->rx_coalesce_usecs_irq)
1536 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1537 if (coalesce->rx_max_coalesced_frames_irq)
1538 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1539 if (coalesce->tx_coalesce_usecs)
1540 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1541 if (coalesce->tx_max_coalesced_frames)
1542 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1543 if (coalesce->tx_coalesce_usecs_irq)
1544 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1545 if (coalesce->tx_max_coalesced_frames_irq)
1546 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1547 if (coalesce->stats_block_coalesce_usecs)
1548 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1549 if (coalesce->use_adaptive_rx_coalesce)
1550 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1551 if (coalesce->use_adaptive_tx_coalesce)
1552 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1553 if (coalesce->pkt_rate_low)
1554 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1555 if (coalesce->rx_coalesce_usecs_low)
1556 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1557 if (coalesce->rx_max_coalesced_frames_low)
1558 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1559 if (coalesce->tx_coalesce_usecs_low)
1560 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1561 if (coalesce->tx_max_coalesced_frames_low)
1562 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1563 if (coalesce->pkt_rate_high)
1564 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1565 if (coalesce->rx_coalesce_usecs_high)
1566 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1567 if (coalesce->rx_max_coalesced_frames_high)
1568 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1569 if (coalesce->tx_coalesce_usecs_high)
1570 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1571 if (coalesce->tx_max_coalesced_frames_high)
1572 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1573 if (coalesce->rate_sample_interval)
1574 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1576 return (supported_params & nonzero_params) == nonzero_params;
1579 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1580 void __user *useraddr)
1582 struct ethtool_coalesce coalesce;
1585 if (!dev->ethtool_ops->set_coalesce)
1588 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1591 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1594 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce);
1596 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1600 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1602 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1604 if (!dev->ethtool_ops->get_ringparam)
1607 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1609 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1614 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1616 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1619 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1622 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1625 dev->ethtool_ops->get_ringparam(dev, &max);
1627 /* ensure new ring parameters are within the maximums */
1628 if (ringparam.rx_pending > max.rx_max_pending ||
1629 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1630 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1631 ringparam.tx_pending > max.tx_max_pending)
1634 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1636 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1640 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1641 void __user *useraddr)
1643 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1645 if (!dev->ethtool_ops->get_channels)
1648 dev->ethtool_ops->get_channels(dev, &channels);
1650 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1655 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1656 void __user *useraddr)
1658 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1659 u16 from_channel, to_channel;
1660 u32 max_rx_in_use = 0;
1664 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1667 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1670 dev->ethtool_ops->get_channels(dev, &curr);
1672 /* ensure new counts are within the maximums */
1673 if (channels.rx_count > curr.max_rx ||
1674 channels.tx_count > curr.max_tx ||
1675 channels.combined_count > curr.max_combined ||
1676 channels.other_count > curr.max_other)
1679 /* ensure the new Rx count fits within the configured Rx flow
1680 * indirection table settings */
1681 if (netif_is_rxfh_configured(dev) &&
1682 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1683 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1686 /* Disabling channels, query zero-copy AF_XDP sockets */
1687 from_channel = channels.combined_count +
1688 min(channels.rx_count, channels.tx_count);
1689 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1690 for (i = from_channel; i < to_channel; i++)
1691 if (xdp_get_umem_from_qid(dev, i))
1694 ret = dev->ethtool_ops->set_channels(dev, &channels);
1696 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1700 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1702 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1704 if (!dev->ethtool_ops->get_pauseparam)
1707 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1709 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1714 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1716 struct ethtool_pauseparam pauseparam;
1719 if (!dev->ethtool_ops->set_pauseparam)
1722 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1725 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1727 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1731 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1733 struct ethtool_test test;
1734 const struct ethtool_ops *ops = dev->ethtool_ops;
1738 if (!ops->self_test || !ops->get_sset_count)
1741 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1744 WARN_ON(test_len == 0);
1746 if (copy_from_user(&test, useraddr, sizeof(test)))
1749 test.len = test_len;
1750 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1754 netif_testing_on(dev);
1755 ops->self_test(dev, &test, data);
1756 netif_testing_off(dev);
1759 if (copy_to_user(useraddr, &test, sizeof(test)))
1761 useraddr += sizeof(test);
1762 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1771 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1773 struct ethtool_gstrings gstrings;
1777 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1780 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1783 if (ret > S32_MAX / ETH_GSTRING_LEN)
1790 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1794 __ethtool_get_strings(dev, gstrings.string_set, data);
1800 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1802 useraddr += sizeof(gstrings);
1804 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1813 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1815 struct ethtool_value id;
1817 const struct ethtool_ops *ops = dev->ethtool_ops;
1820 if (!ops->set_phys_id)
1826 if (copy_from_user(&id, useraddr, sizeof(id)))
1829 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1833 /* Drop the RTNL lock while waiting, but prevent reentry or
1834 * removal of the device.
1841 /* Driver will handle this itself */
1842 schedule_timeout_interruptible(
1843 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1845 /* Driver expects to be called at twice the frequency in rc */
1846 int n = rc * 2, i, interval = HZ / n;
1848 /* Count down seconds */
1850 /* Count down iterations per second */
1854 rc = ops->set_phys_id(dev,
1855 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1859 schedule_timeout_interruptible(interval);
1860 } while (!signal_pending(current) && --i != 0);
1861 } while (!signal_pending(current) &&
1862 (id.data == 0 || --id.data != 0));
1869 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1873 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1875 struct ethtool_stats stats;
1876 const struct ethtool_ops *ops = dev->ethtool_ops;
1880 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1883 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1886 if (n_stats > S32_MAX / sizeof(u64))
1888 WARN_ON_ONCE(!n_stats);
1889 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1892 stats.n_stats = n_stats;
1895 data = vzalloc(array_size(n_stats, sizeof(u64)));
1898 ops->get_ethtool_stats(dev, &stats, data);
1904 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1906 useraddr += sizeof(stats);
1907 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1916 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1918 const struct ethtool_ops *ops = dev->ethtool_ops;
1919 struct phy_device *phydev = dev->phydev;
1920 struct ethtool_stats stats;
1924 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1927 if (dev->phydev && !ops->get_ethtool_phy_stats)
1928 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1930 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1933 if (n_stats > S32_MAX / sizeof(u64))
1935 WARN_ON_ONCE(!n_stats);
1937 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1940 stats.n_stats = n_stats;
1943 data = vzalloc(array_size(n_stats, sizeof(u64)));
1947 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1948 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1952 ops->get_ethtool_phy_stats(dev, &stats, data);
1959 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1961 useraddr += sizeof(stats);
1962 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1971 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1973 struct ethtool_perm_addr epaddr;
1975 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1978 if (epaddr.size < dev->addr_len)
1980 epaddr.size = dev->addr_len;
1982 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1984 useraddr += sizeof(epaddr);
1985 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1990 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1991 u32 cmd, u32 (*actor)(struct net_device *))
1993 struct ethtool_value edata = { .cmd = cmd };
1998 edata.data = actor(dev);
2000 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2005 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2006 void (*actor)(struct net_device *, u32))
2008 struct ethtool_value edata;
2013 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2016 actor(dev, edata.data);
2020 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2021 int (*actor)(struct net_device *, u32))
2023 struct ethtool_value edata;
2028 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2031 return actor(dev, edata.data);
2034 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2035 char __user *useraddr)
2037 struct ethtool_flash efl;
2039 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2041 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2043 if (!dev->ethtool_ops->flash_device)
2044 return devlink_compat_flash_update(dev, efl.data);
2046 return dev->ethtool_ops->flash_device(dev, &efl);
2049 static int ethtool_set_dump(struct net_device *dev,
2050 void __user *useraddr)
2052 struct ethtool_dump dump;
2054 if (!dev->ethtool_ops->set_dump)
2057 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2060 return dev->ethtool_ops->set_dump(dev, &dump);
2063 static int ethtool_get_dump_flag(struct net_device *dev,
2064 void __user *useraddr)
2067 struct ethtool_dump dump;
2068 const struct ethtool_ops *ops = dev->ethtool_ops;
2070 if (!ops->get_dump_flag)
2073 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2076 ret = ops->get_dump_flag(dev, &dump);
2080 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2085 static int ethtool_get_dump_data(struct net_device *dev,
2086 void __user *useraddr)
2090 struct ethtool_dump dump, tmp;
2091 const struct ethtool_ops *ops = dev->ethtool_ops;
2094 if (!ops->get_dump_data || !ops->get_dump_flag)
2097 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2100 memset(&tmp, 0, sizeof(tmp));
2101 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2102 ret = ops->get_dump_flag(dev, &tmp);
2106 len = min(tmp.len, dump.len);
2110 /* Don't ever let the driver think there's more space available
2111 * than it requested with .get_dump_flag().
2115 /* Always allocate enough space to hold the whole thing so that the
2116 * driver does not need to check the length and bother with partial
2119 data = vzalloc(tmp.len);
2122 ret = ops->get_dump_data(dev, &dump, data);
2126 /* There are two sane possibilities:
2127 * 1. The driver's .get_dump_data() does not touch dump.len.
2128 * 2. Or it may set dump.len to how much it really writes, which
2129 * should be tmp.len (or len if it can do a partial dump).
2130 * In any case respond to userspace with the actual length of data
2133 WARN_ON(dump.len != len && dump.len != tmp.len);
2136 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2140 useraddr += offsetof(struct ethtool_dump, data);
2141 if (copy_to_user(useraddr, data, len))
2148 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2150 struct ethtool_ts_info info;
2153 err = __ethtool_get_ts_info(dev, &info);
2157 if (copy_to_user(useraddr, &info, sizeof(info)))
2163 static int __ethtool_get_module_info(struct net_device *dev,
2164 struct ethtool_modinfo *modinfo)
2166 const struct ethtool_ops *ops = dev->ethtool_ops;
2167 struct phy_device *phydev = dev->phydev;
2170 return sfp_get_module_info(dev->sfp_bus, modinfo);
2172 if (phydev && phydev->drv && phydev->drv->module_info)
2173 return phydev->drv->module_info(phydev, modinfo);
2175 if (ops->get_module_info)
2176 return ops->get_module_info(dev, modinfo);
2181 static int ethtool_get_module_info(struct net_device *dev,
2182 void __user *useraddr)
2185 struct ethtool_modinfo modinfo;
2187 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2190 ret = __ethtool_get_module_info(dev, &modinfo);
2194 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2200 static int __ethtool_get_module_eeprom(struct net_device *dev,
2201 struct ethtool_eeprom *ee, u8 *data)
2203 const struct ethtool_ops *ops = dev->ethtool_ops;
2204 struct phy_device *phydev = dev->phydev;
2207 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2209 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2210 return phydev->drv->module_eeprom(phydev, ee, data);
2212 if (ops->get_module_eeprom)
2213 return ops->get_module_eeprom(dev, ee, data);
2218 static int ethtool_get_module_eeprom(struct net_device *dev,
2219 void __user *useraddr)
2222 struct ethtool_modinfo modinfo;
2224 ret = __ethtool_get_module_info(dev, &modinfo);
2228 return ethtool_get_any_eeprom(dev, useraddr,
2229 __ethtool_get_module_eeprom,
2230 modinfo.eeprom_len);
2233 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2236 case ETHTOOL_RX_COPYBREAK:
2237 case ETHTOOL_TX_COPYBREAK:
2238 if (tuna->len != sizeof(u32) ||
2239 tuna->type_id != ETHTOOL_TUNABLE_U32)
2242 case ETHTOOL_PFC_PREVENTION_TOUT:
2243 if (tuna->len != sizeof(u16) ||
2244 tuna->type_id != ETHTOOL_TUNABLE_U16)
2254 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2257 struct ethtool_tunable tuna;
2258 const struct ethtool_ops *ops = dev->ethtool_ops;
2261 if (!ops->get_tunable)
2263 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2265 ret = ethtool_tunable_valid(&tuna);
2268 data = kmalloc(tuna.len, GFP_USER);
2271 ret = ops->get_tunable(dev, &tuna, data);
2274 useraddr += sizeof(tuna);
2276 if (copy_to_user(useraddr, data, tuna.len))
2285 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2288 struct ethtool_tunable tuna;
2289 const struct ethtool_ops *ops = dev->ethtool_ops;
2292 if (!ops->set_tunable)
2294 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2296 ret = ethtool_tunable_valid(&tuna);
2299 useraddr += sizeof(tuna);
2300 data = memdup_user(useraddr, tuna.len);
2302 return PTR_ERR(data);
2303 ret = ops->set_tunable(dev, &tuna, data);
2309 static noinline_for_stack int
2310 ethtool_get_per_queue_coalesce(struct net_device *dev,
2311 void __user *useraddr,
2312 struct ethtool_per_queue_op *per_queue_opt)
2316 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2318 if (!dev->ethtool_ops->get_per_queue_coalesce)
2321 useraddr += sizeof(*per_queue_opt);
2323 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2326 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2327 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2329 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2332 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2334 useraddr += sizeof(coalesce);
2340 static noinline_for_stack int
2341 ethtool_set_per_queue_coalesce(struct net_device *dev,
2342 void __user *useraddr,
2343 struct ethtool_per_queue_op *per_queue_opt)
2348 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2349 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2351 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2352 (!dev->ethtool_ops->get_per_queue_coalesce))
2355 useraddr += sizeof(*per_queue_opt);
2357 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2358 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2359 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2363 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2364 struct ethtool_coalesce coalesce;
2366 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2372 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2377 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2382 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2386 useraddr += sizeof(coalesce);
2392 for_each_set_bit(i, queue_mask, bit) {
2393 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2402 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2403 void __user *useraddr, u32 sub_cmd)
2405 struct ethtool_per_queue_op per_queue_opt;
2407 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2410 if (per_queue_opt.sub_command != sub_cmd)
2413 switch (per_queue_opt.sub_command) {
2414 case ETHTOOL_GCOALESCE:
2415 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2416 case ETHTOOL_SCOALESCE:
2417 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2423 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2426 case ETHTOOL_PHY_DOWNSHIFT:
2427 case ETHTOOL_PHY_FAST_LINK_DOWN:
2428 if (tuna->len != sizeof(u8) ||
2429 tuna->type_id != ETHTOOL_TUNABLE_U8)
2432 case ETHTOOL_PHY_EDPD:
2433 if (tuna->len != sizeof(u16) ||
2434 tuna->type_id != ETHTOOL_TUNABLE_U16)
2444 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2447 struct ethtool_tunable tuna;
2448 struct phy_device *phydev = dev->phydev;
2451 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2454 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2456 ret = ethtool_phy_tunable_valid(&tuna);
2459 data = kmalloc(tuna.len, GFP_USER);
2462 mutex_lock(&phydev->lock);
2463 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2464 mutex_unlock(&phydev->lock);
2467 useraddr += sizeof(tuna);
2469 if (copy_to_user(useraddr, data, tuna.len))
2478 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2481 struct ethtool_tunable tuna;
2482 struct phy_device *phydev = dev->phydev;
2485 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2487 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2489 ret = ethtool_phy_tunable_valid(&tuna);
2492 useraddr += sizeof(tuna);
2493 data = memdup_user(useraddr, tuna.len);
2495 return PTR_ERR(data);
2496 mutex_lock(&phydev->lock);
2497 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2498 mutex_unlock(&phydev->lock);
2504 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2506 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2509 if (!dev->ethtool_ops->get_fecparam)
2512 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2516 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2521 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2523 struct ethtool_fecparam fecparam;
2525 if (!dev->ethtool_ops->set_fecparam)
2528 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2531 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2534 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2536 int dev_ethtool(struct net *net, struct ifreq *ifr)
2538 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2539 void __user *useraddr = ifr->ifr_data;
2540 u32 ethcmd, sub_cmd;
2542 netdev_features_t old_features;
2544 if (!dev || !netif_device_present(dev))
2547 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2550 if (ethcmd == ETHTOOL_PERQUEUE) {
2551 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2556 /* Allow some commands to be done by anyone */
2559 case ETHTOOL_GDRVINFO:
2560 case ETHTOOL_GMSGLVL:
2562 case ETHTOOL_GCOALESCE:
2563 case ETHTOOL_GRINGPARAM:
2564 case ETHTOOL_GPAUSEPARAM:
2565 case ETHTOOL_GRXCSUM:
2566 case ETHTOOL_GTXCSUM:
2568 case ETHTOOL_GSSET_INFO:
2569 case ETHTOOL_GSTRINGS:
2570 case ETHTOOL_GSTATS:
2571 case ETHTOOL_GPHYSTATS:
2573 case ETHTOOL_GPERMADDR:
2577 case ETHTOOL_GFLAGS:
2578 case ETHTOOL_GPFLAGS:
2580 case ETHTOOL_GRXRINGS:
2581 case ETHTOOL_GRXCLSRLCNT:
2582 case ETHTOOL_GRXCLSRULE:
2583 case ETHTOOL_GRXCLSRLALL:
2584 case ETHTOOL_GRXFHINDIR:
2586 case ETHTOOL_GFEATURES:
2587 case ETHTOOL_GCHANNELS:
2588 case ETHTOOL_GET_TS_INFO:
2590 case ETHTOOL_GTUNABLE:
2591 case ETHTOOL_PHY_GTUNABLE:
2592 case ETHTOOL_GLINKSETTINGS:
2593 case ETHTOOL_GFECPARAM:
2596 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2600 if (dev->ethtool_ops->begin) {
2601 rc = dev->ethtool_ops->begin(dev);
2605 old_features = dev->features;
2609 rc = ethtool_get_settings(dev, useraddr);
2612 rc = ethtool_set_settings(dev, useraddr);
2614 case ETHTOOL_GDRVINFO:
2615 rc = ethtool_get_drvinfo(dev, useraddr);
2618 rc = ethtool_get_regs(dev, useraddr);
2621 rc = ethtool_get_wol(dev, useraddr);
2624 rc = ethtool_set_wol(dev, useraddr);
2626 case ETHTOOL_GMSGLVL:
2627 rc = ethtool_get_value(dev, useraddr, ethcmd,
2628 dev->ethtool_ops->get_msglevel);
2630 case ETHTOOL_SMSGLVL:
2631 rc = ethtool_set_value_void(dev, useraddr,
2632 dev->ethtool_ops->set_msglevel);
2634 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2637 rc = ethtool_get_eee(dev, useraddr);
2640 rc = ethtool_set_eee(dev, useraddr);
2642 case ETHTOOL_NWAY_RST:
2643 rc = ethtool_nway_reset(dev);
2646 rc = ethtool_get_link(dev, useraddr);
2648 case ETHTOOL_GEEPROM:
2649 rc = ethtool_get_eeprom(dev, useraddr);
2651 case ETHTOOL_SEEPROM:
2652 rc = ethtool_set_eeprom(dev, useraddr);
2654 case ETHTOOL_GCOALESCE:
2655 rc = ethtool_get_coalesce(dev, useraddr);
2657 case ETHTOOL_SCOALESCE:
2658 rc = ethtool_set_coalesce(dev, useraddr);
2660 case ETHTOOL_GRINGPARAM:
2661 rc = ethtool_get_ringparam(dev, useraddr);
2663 case ETHTOOL_SRINGPARAM:
2664 rc = ethtool_set_ringparam(dev, useraddr);
2666 case ETHTOOL_GPAUSEPARAM:
2667 rc = ethtool_get_pauseparam(dev, useraddr);
2669 case ETHTOOL_SPAUSEPARAM:
2670 rc = ethtool_set_pauseparam(dev, useraddr);
2673 rc = ethtool_self_test(dev, useraddr);
2675 case ETHTOOL_GSTRINGS:
2676 rc = ethtool_get_strings(dev, useraddr);
2678 case ETHTOOL_PHYS_ID:
2679 rc = ethtool_phys_id(dev, useraddr);
2681 case ETHTOOL_GSTATS:
2682 rc = ethtool_get_stats(dev, useraddr);
2684 case ETHTOOL_GPERMADDR:
2685 rc = ethtool_get_perm_addr(dev, useraddr);
2687 case ETHTOOL_GFLAGS:
2688 rc = ethtool_get_value(dev, useraddr, ethcmd,
2689 __ethtool_get_flags);
2691 case ETHTOOL_SFLAGS:
2692 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2694 case ETHTOOL_GPFLAGS:
2695 rc = ethtool_get_value(dev, useraddr, ethcmd,
2696 dev->ethtool_ops->get_priv_flags);
2698 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2700 case ETHTOOL_SPFLAGS:
2701 rc = ethtool_set_value(dev, useraddr,
2702 dev->ethtool_ops->set_priv_flags);
2705 case ETHTOOL_GRXRINGS:
2706 case ETHTOOL_GRXCLSRLCNT:
2707 case ETHTOOL_GRXCLSRULE:
2708 case ETHTOOL_GRXCLSRLALL:
2709 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2712 case ETHTOOL_SRXCLSRLDEL:
2713 case ETHTOOL_SRXCLSRLINS:
2714 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2716 case ETHTOOL_FLASHDEV:
2717 rc = ethtool_flash_device(dev, useraddr);
2720 rc = ethtool_reset(dev, useraddr);
2722 case ETHTOOL_GSSET_INFO:
2723 rc = ethtool_get_sset_info(dev, useraddr);
2725 case ETHTOOL_GRXFHINDIR:
2726 rc = ethtool_get_rxfh_indir(dev, useraddr);
2728 case ETHTOOL_SRXFHINDIR:
2729 rc = ethtool_set_rxfh_indir(dev, useraddr);
2732 rc = ethtool_get_rxfh(dev, useraddr);
2735 rc = ethtool_set_rxfh(dev, useraddr);
2737 case ETHTOOL_GFEATURES:
2738 rc = ethtool_get_features(dev, useraddr);
2740 case ETHTOOL_SFEATURES:
2741 rc = ethtool_set_features(dev, useraddr);
2743 case ETHTOOL_GTXCSUM:
2744 case ETHTOOL_GRXCSUM:
2749 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2751 case ETHTOOL_STXCSUM:
2752 case ETHTOOL_SRXCSUM:
2757 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2759 case ETHTOOL_GCHANNELS:
2760 rc = ethtool_get_channels(dev, useraddr);
2762 case ETHTOOL_SCHANNELS:
2763 rc = ethtool_set_channels(dev, useraddr);
2765 case ETHTOOL_SET_DUMP:
2766 rc = ethtool_set_dump(dev, useraddr);
2768 case ETHTOOL_GET_DUMP_FLAG:
2769 rc = ethtool_get_dump_flag(dev, useraddr);
2771 case ETHTOOL_GET_DUMP_DATA:
2772 rc = ethtool_get_dump_data(dev, useraddr);
2774 case ETHTOOL_GET_TS_INFO:
2775 rc = ethtool_get_ts_info(dev, useraddr);
2777 case ETHTOOL_GMODULEINFO:
2778 rc = ethtool_get_module_info(dev, useraddr);
2780 case ETHTOOL_GMODULEEEPROM:
2781 rc = ethtool_get_module_eeprom(dev, useraddr);
2783 case ETHTOOL_GTUNABLE:
2784 rc = ethtool_get_tunable(dev, useraddr);
2786 case ETHTOOL_STUNABLE:
2787 rc = ethtool_set_tunable(dev, useraddr);
2789 case ETHTOOL_GPHYSTATS:
2790 rc = ethtool_get_phy_stats(dev, useraddr);
2792 case ETHTOOL_PERQUEUE:
2793 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2795 case ETHTOOL_GLINKSETTINGS:
2796 rc = ethtool_get_link_ksettings(dev, useraddr);
2798 case ETHTOOL_SLINKSETTINGS:
2799 rc = ethtool_set_link_ksettings(dev, useraddr);
2801 case ETHTOOL_PHY_GTUNABLE:
2802 rc = get_phy_tunable(dev, useraddr);
2804 case ETHTOOL_PHY_STUNABLE:
2805 rc = set_phy_tunable(dev, useraddr);
2807 case ETHTOOL_GFECPARAM:
2808 rc = ethtool_get_fecparam(dev, useraddr);
2810 case ETHTOOL_SFECPARAM:
2811 rc = ethtool_set_fecparam(dev, useraddr);
2817 if (dev->ethtool_ops->complete)
2818 dev->ethtool_ops->complete(dev);
2820 if (old_features != dev->features)
2821 netdev_features_change(dev);
2826 struct ethtool_rx_flow_key {
2827 struct flow_dissector_key_basic basic;
2829 struct flow_dissector_key_ipv4_addrs ipv4;
2830 struct flow_dissector_key_ipv6_addrs ipv6;
2832 struct flow_dissector_key_ports tp;
2833 struct flow_dissector_key_ip ip;
2834 struct flow_dissector_key_vlan vlan;
2835 struct flow_dissector_key_eth_addrs eth_addrs;
2836 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2838 struct ethtool_rx_flow_match {
2839 struct flow_dissector dissector;
2840 struct ethtool_rx_flow_key key;
2841 struct ethtool_rx_flow_key mask;
2844 struct ethtool_rx_flow_rule *
2845 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2847 const struct ethtool_rx_flow_spec *fs = input->fs;
2848 static struct in6_addr zero_addr = {};
2849 struct ethtool_rx_flow_match *match;
2850 struct ethtool_rx_flow_rule *flow;
2851 struct flow_action_entry *act;
2853 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2854 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2856 return ERR_PTR(-ENOMEM);
2858 /* ethtool_rx supports only one single action per rule. */
2859 flow->rule = flow_rule_alloc(1);
2862 return ERR_PTR(-ENOMEM);
2865 match = (struct ethtool_rx_flow_match *)flow->priv;
2866 flow->rule->match.dissector = &match->dissector;
2867 flow->rule->match.mask = &match->mask;
2868 flow->rule->match.key = &match->key;
2870 match->mask.basic.n_proto = htons(0xffff);
2872 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2874 const struct ethhdr *ether_spec, *ether_m_spec;
2876 ether_spec = &fs->h_u.ether_spec;
2877 ether_m_spec = &fs->m_u.ether_spec;
2879 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2880 ether_addr_copy(match->key.eth_addrs.src,
2881 ether_spec->h_source);
2882 ether_addr_copy(match->mask.eth_addrs.src,
2883 ether_m_spec->h_source);
2885 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2886 ether_addr_copy(match->key.eth_addrs.dst,
2887 ether_spec->h_dest);
2888 ether_addr_copy(match->mask.eth_addrs.dst,
2889 ether_m_spec->h_dest);
2891 if (ether_m_spec->h_proto) {
2892 match->key.basic.n_proto = ether_spec->h_proto;
2893 match->mask.basic.n_proto = ether_m_spec->h_proto;
2899 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2901 match->key.basic.n_proto = htons(ETH_P_IP);
2903 v4_spec = &fs->h_u.tcp_ip4_spec;
2904 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2906 if (v4_m_spec->ip4src) {
2907 match->key.ipv4.src = v4_spec->ip4src;
2908 match->mask.ipv4.src = v4_m_spec->ip4src;
2910 if (v4_m_spec->ip4dst) {
2911 match->key.ipv4.dst = v4_spec->ip4dst;
2912 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2914 if (v4_m_spec->ip4src ||
2915 v4_m_spec->ip4dst) {
2916 match->dissector.used_keys |=
2917 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2918 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2919 offsetof(struct ethtool_rx_flow_key, ipv4);
2921 if (v4_m_spec->psrc) {
2922 match->key.tp.src = v4_spec->psrc;
2923 match->mask.tp.src = v4_m_spec->psrc;
2925 if (v4_m_spec->pdst) {
2926 match->key.tp.dst = v4_spec->pdst;
2927 match->mask.tp.dst = v4_m_spec->pdst;
2929 if (v4_m_spec->psrc ||
2931 match->dissector.used_keys |=
2932 BIT(FLOW_DISSECTOR_KEY_PORTS);
2933 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2934 offsetof(struct ethtool_rx_flow_key, tp);
2936 if (v4_m_spec->tos) {
2937 match->key.ip.tos = v4_spec->tos;
2938 match->mask.ip.tos = v4_m_spec->tos;
2939 match->dissector.used_keys |=
2940 BIT(FLOW_DISSECTOR_KEY_IP);
2941 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2942 offsetof(struct ethtool_rx_flow_key, ip);
2948 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2950 match->key.basic.n_proto = htons(ETH_P_IPV6);
2952 v6_spec = &fs->h_u.tcp_ip6_spec;
2953 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2954 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2955 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2956 sizeof(match->key.ipv6.src));
2957 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2958 sizeof(match->mask.ipv6.src));
2960 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2961 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2962 sizeof(match->key.ipv6.dst));
2963 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2964 sizeof(match->mask.ipv6.dst));
2966 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2967 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2968 match->dissector.used_keys |=
2969 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2970 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2971 offsetof(struct ethtool_rx_flow_key, ipv6);
2973 if (v6_m_spec->psrc) {
2974 match->key.tp.src = v6_spec->psrc;
2975 match->mask.tp.src = v6_m_spec->psrc;
2977 if (v6_m_spec->pdst) {
2978 match->key.tp.dst = v6_spec->pdst;
2979 match->mask.tp.dst = v6_m_spec->pdst;
2981 if (v6_m_spec->psrc ||
2983 match->dissector.used_keys |=
2984 BIT(FLOW_DISSECTOR_KEY_PORTS);
2985 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2986 offsetof(struct ethtool_rx_flow_key, tp);
2988 if (v6_m_spec->tclass) {
2989 match->key.ip.tos = v6_spec->tclass;
2990 match->mask.ip.tos = v6_m_spec->tclass;
2991 match->dissector.used_keys |=
2992 BIT(FLOW_DISSECTOR_KEY_IP);
2993 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2994 offsetof(struct ethtool_rx_flow_key, ip);
2999 ethtool_rx_flow_rule_destroy(flow);
3000 return ERR_PTR(-EINVAL);
3003 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3006 match->key.basic.ip_proto = IPPROTO_TCP;
3010 match->key.basic.ip_proto = IPPROTO_UDP;
3013 match->mask.basic.ip_proto = 0xff;
3015 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3016 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3017 offsetof(struct ethtool_rx_flow_key, basic);
3019 if (fs->flow_type & FLOW_EXT) {
3020 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3021 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3023 if (ext_m_spec->vlan_etype) {
3024 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3025 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3028 if (ext_m_spec->vlan_tci) {
3029 match->key.vlan.vlan_id =
3030 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3031 match->mask.vlan.vlan_id =
3032 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3034 match->key.vlan.vlan_dei =
3035 !!(ext_h_spec->vlan_tci & htons(0x1000));
3036 match->mask.vlan.vlan_dei =
3037 !!(ext_m_spec->vlan_tci & htons(0x1000));
3039 match->key.vlan.vlan_priority =
3040 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3041 match->mask.vlan.vlan_priority =
3042 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3045 if (ext_m_spec->vlan_etype ||
3046 ext_m_spec->vlan_tci) {
3047 match->dissector.used_keys |=
3048 BIT(FLOW_DISSECTOR_KEY_VLAN);
3049 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3050 offsetof(struct ethtool_rx_flow_key, vlan);
3053 if (fs->flow_type & FLOW_MAC_EXT) {
3054 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3055 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3057 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3059 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3062 match->dissector.used_keys |=
3063 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3064 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3065 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3068 act = &flow->rule->action.entries[0];
3069 switch (fs->ring_cookie) {
3070 case RX_CLS_FLOW_DISC:
3071 act->id = FLOW_ACTION_DROP;
3073 case RX_CLS_FLOW_WAKE:
3074 act->id = FLOW_ACTION_WAKE;
3077 act->id = FLOW_ACTION_QUEUE;
3078 if (fs->flow_type & FLOW_RSS)
3079 act->queue.ctx = input->rss_ctx;
3081 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3082 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3088 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3090 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3095 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);