1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/ethtool.c - Ethtool ioctl handler
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_drv.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_phy_ops *phy_ops = ethtool_phy_ops;
139 const struct ethtool_ops *ops = dev->ethtool_ops;
141 if (sset == ETH_SS_FEATURES)
142 return ARRAY_SIZE(netdev_features_strings);
144 if (sset == ETH_SS_RSS_HASH_FUNCS)
145 return ARRAY_SIZE(rss_hash_func_strings);
147 if (sset == ETH_SS_TUNABLES)
148 return ARRAY_SIZE(tunable_strings);
150 if (sset == ETH_SS_PHY_TUNABLES)
151 return ARRAY_SIZE(phy_tunable_strings);
153 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
154 !ops->get_ethtool_phy_stats &&
155 phy_ops && phy_ops->get_sset_count)
156 return phy_ops->get_sset_count(dev->phydev);
158 if (sset == ETH_SS_LINK_MODES)
159 return __ETHTOOL_LINK_MODE_MASK_NBITS;
161 if (ops->get_sset_count && ops->get_strings)
162 return ops->get_sset_count(dev, sset);
167 static void __ethtool_get_strings(struct net_device *dev,
168 u32 stringset, u8 *data)
170 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
171 const struct ethtool_ops *ops = dev->ethtool_ops;
173 if (stringset == ETH_SS_FEATURES)
174 memcpy(data, netdev_features_strings,
175 sizeof(netdev_features_strings));
176 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
177 memcpy(data, rss_hash_func_strings,
178 sizeof(rss_hash_func_strings));
179 else if (stringset == ETH_SS_TUNABLES)
180 memcpy(data, tunable_strings, sizeof(tunable_strings));
181 else if (stringset == ETH_SS_PHY_TUNABLES)
182 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
183 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
184 !ops->get_ethtool_phy_stats && phy_ops &&
185 phy_ops->get_strings)
186 phy_ops->get_strings(dev->phydev, data);
187 else if (stringset == ETH_SS_LINK_MODES)
188 memcpy(data, link_mode_names,
189 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
191 /* ops->get_strings is valid because checked earlier */
192 ops->get_strings(dev, stringset, data);
195 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
197 /* feature masks of legacy discrete ethtool ops */
200 case ETHTOOL_GTXCSUM:
201 case ETHTOOL_STXCSUM:
202 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
204 case ETHTOOL_GRXCSUM:
205 case ETHTOOL_SRXCSUM:
206 return NETIF_F_RXCSUM;
209 return NETIF_F_SG | NETIF_F_FRAGLIST;
212 return NETIF_F_ALL_TSO;
224 static int ethtool_get_one_feature(struct net_device *dev,
225 char __user *useraddr, u32 ethcmd)
227 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
228 struct ethtool_value edata = {
230 .data = !!(dev->features & mask),
233 if (copy_to_user(useraddr, &edata, sizeof(edata)))
238 static int ethtool_set_one_feature(struct net_device *dev,
239 void __user *useraddr, u32 ethcmd)
241 struct ethtool_value edata;
242 netdev_features_t mask;
244 if (copy_from_user(&edata, useraddr, sizeof(edata)))
247 mask = ethtool_get_feature_mask(ethcmd);
248 mask &= dev->hw_features;
253 dev->wanted_features |= mask;
255 dev->wanted_features &= ~mask;
257 __netdev_update_features(dev);
262 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
263 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
264 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
265 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
268 static u32 __ethtool_get_flags(struct net_device *dev)
272 if (dev->features & NETIF_F_LRO)
273 flags |= ETH_FLAG_LRO;
274 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
275 flags |= ETH_FLAG_RXVLAN;
276 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
277 flags |= ETH_FLAG_TXVLAN;
278 if (dev->features & NETIF_F_NTUPLE)
279 flags |= ETH_FLAG_NTUPLE;
280 if (dev->features & NETIF_F_RXHASH)
281 flags |= ETH_FLAG_RXHASH;
286 static int __ethtool_set_flags(struct net_device *dev, u32 data)
288 netdev_features_t features = 0, changed;
290 if (data & ~ETH_ALL_FLAGS)
293 if (data & ETH_FLAG_LRO)
294 features |= NETIF_F_LRO;
295 if (data & ETH_FLAG_RXVLAN)
296 features |= NETIF_F_HW_VLAN_CTAG_RX;
297 if (data & ETH_FLAG_TXVLAN)
298 features |= NETIF_F_HW_VLAN_CTAG_TX;
299 if (data & ETH_FLAG_NTUPLE)
300 features |= NETIF_F_NTUPLE;
301 if (data & ETH_FLAG_RXHASH)
302 features |= NETIF_F_RXHASH;
304 /* allow changing only bits set in hw_features */
305 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
306 if (changed & ~dev->hw_features)
307 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
309 dev->wanted_features =
310 (dev->wanted_features & ~changed) | (features & changed);
312 __netdev_update_features(dev);
317 /* Given two link masks, AND them together and save the result in dst. */
318 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
319 struct ethtool_link_ksettings *src)
321 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
322 unsigned int idx = 0;
324 for (; idx < size; idx++) {
325 dst->link_modes.supported[idx] &=
326 src->link_modes.supported[idx];
327 dst->link_modes.advertising[idx] &=
328 src->link_modes.advertising[idx];
331 EXPORT_SYMBOL(ethtool_intersect_link_masks);
333 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
336 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
339 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
341 /* return false if src had higher bits set. lower bits always updated. */
342 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
343 const unsigned long *src)
347 /* TODO: following test will soon always be true */
348 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
349 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
351 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
352 bitmap_fill(ext, 32);
353 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
354 if (bitmap_intersects(ext, src,
355 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
356 /* src mask goes beyond bit 31 */
360 *legacy_u32 = src[0];
363 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
365 /* return false if ksettings link modes had higher bits
366 * set. legacy_settings always updated (best effort)
369 convert_link_ksettings_to_legacy_settings(
370 struct ethtool_cmd *legacy_settings,
371 const struct ethtool_link_ksettings *link_ksettings)
375 memset(legacy_settings, 0, sizeof(*legacy_settings));
376 /* this also clears the deprecated fields in legacy structure:
382 retval &= ethtool_convert_link_mode_to_legacy_u32(
383 &legacy_settings->supported,
384 link_ksettings->link_modes.supported);
385 retval &= ethtool_convert_link_mode_to_legacy_u32(
386 &legacy_settings->advertising,
387 link_ksettings->link_modes.advertising);
388 retval &= ethtool_convert_link_mode_to_legacy_u32(
389 &legacy_settings->lp_advertising,
390 link_ksettings->link_modes.lp_advertising);
391 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
392 legacy_settings->duplex
393 = link_ksettings->base.duplex;
394 legacy_settings->port
395 = link_ksettings->base.port;
396 legacy_settings->phy_address
397 = link_ksettings->base.phy_address;
398 legacy_settings->autoneg
399 = link_ksettings->base.autoneg;
400 legacy_settings->mdio_support
401 = link_ksettings->base.mdio_support;
402 legacy_settings->eth_tp_mdix
403 = link_ksettings->base.eth_tp_mdix;
404 legacy_settings->eth_tp_mdix_ctrl
405 = link_ksettings->base.eth_tp_mdix_ctrl;
406 legacy_settings->transceiver
407 = link_ksettings->base.transceiver;
411 /* number of 32-bit words to store the user's link mode bitmaps */
412 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
413 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
415 /* layout of the struct passed from/to userland */
416 struct ethtool_link_usettings {
417 struct ethtool_link_settings base;
419 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
420 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
421 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
425 /* Internal kernel helper to query a device ethtool_link_settings. */
426 int __ethtool_get_link_ksettings(struct net_device *dev,
427 struct ethtool_link_ksettings *link_ksettings)
429 const struct link_mode_info *link_info;
434 if (!dev->ethtool_ops->get_link_ksettings)
437 memset(link_ksettings, 0, sizeof(*link_ksettings));
439 link_ksettings->link_mode = -1;
440 err = dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
444 if (link_ksettings->link_mode != -1) {
445 link_info = &link_mode_params[link_ksettings->link_mode];
446 link_ksettings->base.speed = link_info->speed;
447 link_ksettings->lanes = link_info->lanes;
448 link_ksettings->base.duplex = link_info->duplex;
453 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
455 /* convert ethtool_link_usettings in user space to a kernel internal
456 * ethtool_link_ksettings. return 0 on success, errno on error.
458 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
459 const void __user *from)
461 struct ethtool_link_usettings link_usettings;
463 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
466 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
467 bitmap_from_arr32(to->link_modes.supported,
468 link_usettings.link_modes.supported,
469 __ETHTOOL_LINK_MODE_MASK_NBITS);
470 bitmap_from_arr32(to->link_modes.advertising,
471 link_usettings.link_modes.advertising,
472 __ETHTOOL_LINK_MODE_MASK_NBITS);
473 bitmap_from_arr32(to->link_modes.lp_advertising,
474 link_usettings.link_modes.lp_advertising,
475 __ETHTOOL_LINK_MODE_MASK_NBITS);
480 /* Check if the user is trying to change anything besides speed/duplex */
481 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
483 struct ethtool_link_settings base2 = {};
485 base2.speed = cmd->base.speed;
486 base2.port = PORT_OTHER;
487 base2.duplex = cmd->base.duplex;
488 base2.cmd = cmd->base.cmd;
489 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
491 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
492 bitmap_empty(cmd->link_modes.supported,
493 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
494 bitmap_empty(cmd->link_modes.lp_advertising,
495 __ETHTOOL_LINK_MODE_MASK_NBITS);
498 /* convert a kernel internal ethtool_link_ksettings to
499 * ethtool_link_usettings in user space. return 0 on success, errno on
503 store_link_ksettings_for_user(void __user *to,
504 const struct ethtool_link_ksettings *from)
506 struct ethtool_link_usettings link_usettings;
508 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
509 bitmap_to_arr32(link_usettings.link_modes.supported,
510 from->link_modes.supported,
511 __ETHTOOL_LINK_MODE_MASK_NBITS);
512 bitmap_to_arr32(link_usettings.link_modes.advertising,
513 from->link_modes.advertising,
514 __ETHTOOL_LINK_MODE_MASK_NBITS);
515 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
516 from->link_modes.lp_advertising,
517 __ETHTOOL_LINK_MODE_MASK_NBITS);
519 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
525 /* Query device for its ethtool_link_settings. */
526 static int ethtool_get_link_ksettings(struct net_device *dev,
527 void __user *useraddr)
530 struct ethtool_link_ksettings link_ksettings;
533 if (!dev->ethtool_ops->get_link_ksettings)
536 /* handle bitmap nbits handshake */
537 if (copy_from_user(&link_ksettings.base, useraddr,
538 sizeof(link_ksettings.base)))
541 if (__ETHTOOL_LINK_MODE_MASK_NU32
542 != link_ksettings.base.link_mode_masks_nwords) {
543 /* wrong link mode nbits requested */
544 memset(&link_ksettings, 0, sizeof(link_ksettings));
545 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
546 /* send back number of words required as negative val */
547 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
548 "need too many bits for link modes!");
549 link_ksettings.base.link_mode_masks_nwords
550 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
552 /* copy the base fields back to user, not the link
555 if (copy_to_user(useraddr, &link_ksettings.base,
556 sizeof(link_ksettings.base)))
562 /* handshake successful: user/kernel agree on
563 * link_mode_masks_nwords
566 memset(&link_ksettings, 0, sizeof(link_ksettings));
567 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
571 /* make sure we tell the right values to user */
572 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
573 link_ksettings.base.link_mode_masks_nwords
574 = __ETHTOOL_LINK_MODE_MASK_NU32;
575 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
576 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
578 return store_link_ksettings_for_user(useraddr, &link_ksettings);
581 /* Update device ethtool_link_settings. */
582 static int ethtool_set_link_ksettings(struct net_device *dev,
583 void __user *useraddr)
586 struct ethtool_link_ksettings link_ksettings;
590 if (!dev->ethtool_ops->set_link_ksettings)
593 /* make sure nbits field has expected value */
594 if (copy_from_user(&link_ksettings.base, useraddr,
595 sizeof(link_ksettings.base)))
598 if (__ETHTOOL_LINK_MODE_MASK_NU32
599 != link_ksettings.base.link_mode_masks_nwords)
602 /* copy the whole structure, now that we know it has expected
605 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
609 /* re-check nwords field, just in case */
610 if (__ETHTOOL_LINK_MODE_MASK_NU32
611 != link_ksettings.base.link_mode_masks_nwords)
614 if (link_ksettings.base.master_slave_cfg ||
615 link_ksettings.base.master_slave_state)
618 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
620 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
621 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
626 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
627 const struct ethtool_link_ksettings *cmd,
628 u32 *dev_speed, u8 *dev_duplex)
633 speed = cmd->base.speed;
634 duplex = cmd->base.duplex;
635 /* don't allow custom speed and duplex */
636 if (!ethtool_validate_speed(speed) ||
637 !ethtool_validate_duplex(duplex) ||
638 !ethtool_virtdev_validate_cmd(cmd))
641 *dev_duplex = duplex;
645 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
647 /* Query device for its ethtool_cmd settings.
649 * Backward compatibility note: for compatibility with legacy ethtool, this is
650 * now implemented via get_link_ksettings. When driver reports higher link mode
651 * bits, a kernel warning is logged once (with name of 1st driver/device) to
652 * recommend user to upgrade ethtool, but the command is successful (only the
653 * lower link mode bits reported back to user). Deprecated fields from
654 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
656 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
658 struct ethtool_link_ksettings link_ksettings;
659 struct ethtool_cmd cmd;
663 if (!dev->ethtool_ops->get_link_ksettings)
666 memset(&link_ksettings, 0, sizeof(link_ksettings));
667 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
670 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
672 /* send a sensible cmd tag back to user */
673 cmd.cmd = ETHTOOL_GSET;
675 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
681 /* Update device link settings with given ethtool_cmd.
683 * Backward compatibility note: for compatibility with legacy ethtool, this is
684 * now always implemented via set_link_settings. When user's request updates
685 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
686 * warning is logged once (with name of 1st driver/device) to recommend user to
687 * upgrade ethtool, and the request is rejected.
689 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
691 struct ethtool_link_ksettings link_ksettings;
692 struct ethtool_cmd cmd;
697 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
699 if (!dev->ethtool_ops->set_link_ksettings)
702 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
704 link_ksettings.base.link_mode_masks_nwords =
705 __ETHTOOL_LINK_MODE_MASK_NU32;
706 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
708 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
709 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
714 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
715 void __user *useraddr)
717 struct ethtool_drvinfo info;
718 const struct ethtool_ops *ops = dev->ethtool_ops;
720 memset(&info, 0, sizeof(info));
721 info.cmd = ETHTOOL_GDRVINFO;
722 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
723 if (ops->get_drvinfo) {
724 ops->get_drvinfo(dev, &info);
725 } else if (dev->dev.parent && dev->dev.parent->driver) {
726 strlcpy(info.bus_info, dev_name(dev->dev.parent),
727 sizeof(info.bus_info));
728 strlcpy(info.driver, dev->dev.parent->driver->name,
729 sizeof(info.driver));
735 * this method of obtaining string set info is deprecated;
736 * Use ETHTOOL_GSSET_INFO instead.
738 if (ops->get_sset_count) {
741 rc = ops->get_sset_count(dev, ETH_SS_TEST);
743 info.testinfo_len = rc;
744 rc = ops->get_sset_count(dev, ETH_SS_STATS);
747 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
749 info.n_priv_flags = rc;
751 if (ops->get_regs_len) {
752 int ret = ops->get_regs_len(dev);
755 info.regdump_len = ret;
758 if (ops->get_eeprom_len)
759 info.eedump_len = ops->get_eeprom_len(dev);
761 if (!info.fw_version[0])
762 devlink_compat_running_version(dev, info.fw_version,
763 sizeof(info.fw_version));
765 if (copy_to_user(useraddr, &info, sizeof(info)))
770 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
771 void __user *useraddr)
773 struct ethtool_sset_info info;
775 int i, idx = 0, n_bits = 0, ret, rc;
776 u32 *info_buf = NULL;
778 if (copy_from_user(&info, useraddr, sizeof(info)))
781 /* store copy of mask, because we zero struct later on */
782 sset_mask = info.sset_mask;
786 /* calculate size of return buffer */
787 n_bits = hweight64(sset_mask);
789 memset(&info, 0, sizeof(info));
790 info.cmd = ETHTOOL_GSSET_INFO;
792 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
797 * fill return buffer based on input bitmask and successful
798 * get_sset_count return
800 for (i = 0; i < 64; i++) {
801 if (!(sset_mask & (1ULL << i)))
804 rc = __ethtool_get_sset_count(dev, i);
806 info.sset_mask |= (1ULL << i);
807 info_buf[idx++] = rc;
812 if (copy_to_user(useraddr, &info, sizeof(info)))
815 useraddr += offsetof(struct ethtool_sset_info, data);
816 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
826 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
827 u32 cmd, void __user *useraddr)
829 struct ethtool_rxnfc info;
830 size_t info_size = sizeof(info);
833 if (!dev->ethtool_ops->set_rxnfc)
836 /* struct ethtool_rxnfc was originally defined for
837 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
838 * members. User-space might still be using that
840 if (cmd == ETHTOOL_SRXFH)
841 info_size = (offsetof(struct ethtool_rxnfc, data) +
844 if (copy_from_user(&info, useraddr, info_size))
847 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
851 if (cmd == ETHTOOL_SRXCLSRLINS &&
852 copy_to_user(useraddr, &info, info_size))
858 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
859 u32 cmd, void __user *useraddr)
861 struct ethtool_rxnfc info;
862 size_t info_size = sizeof(info);
863 const struct ethtool_ops *ops = dev->ethtool_ops;
865 void *rule_buf = NULL;
870 /* struct ethtool_rxnfc was originally defined for
871 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
872 * members. User-space might still be using that
874 if (cmd == ETHTOOL_GRXFH)
875 info_size = (offsetof(struct ethtool_rxnfc, data) +
878 if (copy_from_user(&info, useraddr, info_size))
881 /* If FLOW_RSS was requested then user-space must be using the
882 * new definition, as FLOW_RSS is newer.
884 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
885 info_size = sizeof(info);
886 if (copy_from_user(&info, useraddr, info_size))
888 /* Since malicious users may modify the original data,
889 * we need to check whether FLOW_RSS is still requested.
891 if (!(info.flow_type & FLOW_RSS))
898 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
899 if (info.rule_cnt > 0) {
900 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
901 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
908 ret = ops->get_rxnfc(dev, &info, rule_buf);
913 if (copy_to_user(useraddr, &info, info_size))
917 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
918 if (copy_to_user(useraddr, rule_buf,
919 info.rule_cnt * sizeof(u32)))
930 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
931 struct ethtool_rxnfc *rx_rings,
936 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
939 /* Validate ring indices */
940 for (i = 0; i < size; i++)
941 if (indir[i] >= rx_rings->data)
947 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
949 void netdev_rss_key_fill(void *buffer, size_t len)
951 BUG_ON(len > sizeof(netdev_rss_key));
952 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
953 memcpy(buffer, netdev_rss_key, len);
955 EXPORT_SYMBOL(netdev_rss_key_fill);
957 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
958 void __user *useraddr)
960 u32 user_size, dev_size;
964 if (!dev->ethtool_ops->get_rxfh_indir_size ||
965 !dev->ethtool_ops->get_rxfh)
967 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
971 if (copy_from_user(&user_size,
972 useraddr + offsetof(struct ethtool_rxfh_indir, size),
976 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
977 &dev_size, sizeof(dev_size)))
980 /* If the user buffer size is 0, this is just a query for the
981 * device table size. Otherwise, if it's smaller than the
982 * device table size it's an error.
984 if (user_size < dev_size)
985 return user_size == 0 ? 0 : -EINVAL;
987 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
991 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
995 if (copy_to_user(useraddr +
996 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
997 indir, dev_size * sizeof(indir[0])))
1005 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1006 void __user *useraddr)
1008 struct ethtool_rxnfc rx_rings;
1009 u32 user_size, dev_size, i;
1011 const struct ethtool_ops *ops = dev->ethtool_ops;
1013 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1015 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1019 dev_size = ops->get_rxfh_indir_size(dev);
1023 if (copy_from_user(&user_size,
1024 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1028 if (user_size != 0 && user_size != dev_size)
1031 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1035 rx_rings.cmd = ETHTOOL_GRXRINGS;
1036 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1040 if (user_size == 0) {
1041 for (i = 0; i < dev_size; i++)
1042 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1044 ret = ethtool_copy_validate_indir(indir,
1045 useraddr + ringidx_offset,
1052 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1056 /* indicate whether rxfh was set to default */
1058 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1060 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1067 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1068 void __user *useraddr)
1071 const struct ethtool_ops *ops = dev->ethtool_ops;
1072 u32 user_indir_size, user_key_size;
1073 u32 dev_indir_size = 0, dev_key_size = 0;
1074 struct ethtool_rxfh rxfh;
1085 if (ops->get_rxfh_indir_size)
1086 dev_indir_size = ops->get_rxfh_indir_size(dev);
1087 if (ops->get_rxfh_key_size)
1088 dev_key_size = ops->get_rxfh_key_size(dev);
1090 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1092 user_indir_size = rxfh.indir_size;
1093 user_key_size = rxfh.key_size;
1095 /* Check that reserved fields are 0 for now */
1096 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1098 /* Most drivers don't handle rss_context, check it's 0 as well */
1099 if (rxfh.rss_context && !ops->get_rxfh_context)
1102 rxfh.indir_size = dev_indir_size;
1103 rxfh.key_size = dev_key_size;
1104 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1107 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1108 (user_key_size && (user_key_size != dev_key_size)))
1111 indir_bytes = user_indir_size * sizeof(indir[0]);
1112 total_size = indir_bytes + user_key_size;
1113 rss_config = kzalloc(total_size, GFP_USER);
1117 if (user_indir_size)
1118 indir = (u32 *)rss_config;
1121 hkey = rss_config + indir_bytes;
1123 if (rxfh.rss_context)
1124 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1128 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1132 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1133 &dev_hfunc, sizeof(rxfh.hfunc))) {
1135 } else if (copy_to_user(useraddr +
1136 offsetof(struct ethtool_rxfh, rss_config[0]),
1137 rss_config, total_size)) {
1146 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1147 void __user *useraddr)
1150 const struct ethtool_ops *ops = dev->ethtool_ops;
1151 struct ethtool_rxnfc rx_rings;
1152 struct ethtool_rxfh rxfh;
1153 u32 dev_indir_size = 0, dev_key_size = 0, i;
1154 u32 *indir = NULL, indir_bytes = 0;
1157 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1158 bool delete = false;
1160 if (!ops->get_rxnfc || !ops->set_rxfh)
1163 if (ops->get_rxfh_indir_size)
1164 dev_indir_size = ops->get_rxfh_indir_size(dev);
1165 if (ops->get_rxfh_key_size)
1166 dev_key_size = ops->get_rxfh_key_size(dev);
1168 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1171 /* Check that reserved fields are 0 for now */
1172 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1174 /* Most drivers don't handle rss_context, check it's 0 as well */
1175 if (rxfh.rss_context && !ops->set_rxfh_context)
1178 /* If either indir, hash key or function is valid, proceed further.
1179 * Must request at least one change: indir size, hash key or function.
1181 if ((rxfh.indir_size &&
1182 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1183 rxfh.indir_size != dev_indir_size) ||
1184 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1185 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1186 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1189 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1190 indir_bytes = dev_indir_size * sizeof(indir[0]);
1192 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1196 rx_rings.cmd = ETHTOOL_GRXRINGS;
1197 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1201 /* rxfh.indir_size == 0 means reset the indir table to default (master
1202 * context) or delete the context (other RSS contexts).
1203 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1205 if (rxfh.indir_size &&
1206 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1207 indir = (u32 *)rss_config;
1208 ret = ethtool_copy_validate_indir(indir,
1209 useraddr + rss_cfg_offset,
1214 } else if (rxfh.indir_size == 0) {
1215 if (rxfh.rss_context == 0) {
1216 indir = (u32 *)rss_config;
1217 for (i = 0; i < dev_indir_size; i++)
1218 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1224 if (rxfh.key_size) {
1225 hkey = rss_config + indir_bytes;
1226 if (copy_from_user(hkey,
1227 useraddr + rss_cfg_offset + indir_bytes,
1234 if (rxfh.rss_context)
1235 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1236 &rxfh.rss_context, delete);
1238 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1242 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1243 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1246 if (!rxfh.rss_context) {
1247 /* indicate whether rxfh was set to default */
1248 if (rxfh.indir_size == 0)
1249 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1250 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1251 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1259 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1261 struct ethtool_regs regs;
1262 const struct ethtool_ops *ops = dev->ethtool_ops;
1266 if (!ops->get_regs || !ops->get_regs_len)
1269 if (copy_from_user(®s, useraddr, sizeof(regs)))
1272 reglen = ops->get_regs_len(dev);
1276 if (regs.len > reglen)
1279 regbuf = vzalloc(reglen);
1283 if (regs.len < reglen)
1286 ops->get_regs(dev, ®s, regbuf);
1289 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1291 useraddr += offsetof(struct ethtool_regs, data);
1292 if (copy_to_user(useraddr, regbuf, reglen))
1301 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1303 struct ethtool_value reset;
1306 if (!dev->ethtool_ops->reset)
1309 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1312 ret = dev->ethtool_ops->reset(dev, &reset.data);
1316 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1321 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1323 struct ethtool_wolinfo wol;
1325 if (!dev->ethtool_ops->get_wol)
1328 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1329 wol.cmd = ETHTOOL_GWOL;
1330 dev->ethtool_ops->get_wol(dev, &wol);
1332 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1337 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1339 struct ethtool_wolinfo wol;
1342 if (!dev->ethtool_ops->set_wol)
1345 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1348 ret = dev->ethtool_ops->set_wol(dev, &wol);
1352 dev->wol_enabled = !!wol.wolopts;
1353 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1358 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1360 struct ethtool_eee edata;
1363 if (!dev->ethtool_ops->get_eee)
1366 memset(&edata, 0, sizeof(struct ethtool_eee));
1367 edata.cmd = ETHTOOL_GEEE;
1368 rc = dev->ethtool_ops->get_eee(dev, &edata);
1373 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1379 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1381 struct ethtool_eee edata;
1384 if (!dev->ethtool_ops->set_eee)
1387 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1390 ret = dev->ethtool_ops->set_eee(dev, &edata);
1392 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1396 static int ethtool_nway_reset(struct net_device *dev)
1398 if (!dev->ethtool_ops->nway_reset)
1401 return dev->ethtool_ops->nway_reset(dev);
1404 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1406 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1407 int link = __ethtool_get_link(dev);
1413 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1418 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1419 int (*getter)(struct net_device *,
1420 struct ethtool_eeprom *, u8 *),
1423 struct ethtool_eeprom eeprom;
1424 void __user *userbuf = useraddr + sizeof(eeprom);
1425 u32 bytes_remaining;
1429 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1432 /* Check for wrap and zero */
1433 if (eeprom.offset + eeprom.len <= eeprom.offset)
1436 /* Check for exceeding total eeprom len */
1437 if (eeprom.offset + eeprom.len > total_len)
1440 data = kmalloc(PAGE_SIZE, GFP_USER);
1444 bytes_remaining = eeprom.len;
1445 while (bytes_remaining > 0) {
1446 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1448 ret = getter(dev, &eeprom, data);
1451 if (copy_to_user(userbuf, data, eeprom.len)) {
1455 userbuf += eeprom.len;
1456 eeprom.offset += eeprom.len;
1457 bytes_remaining -= eeprom.len;
1460 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1461 eeprom.offset -= eeprom.len;
1462 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1469 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1471 const struct ethtool_ops *ops = dev->ethtool_ops;
1473 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1474 !ops->get_eeprom_len(dev))
1477 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1478 ops->get_eeprom_len(dev));
1481 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1483 struct ethtool_eeprom eeprom;
1484 const struct ethtool_ops *ops = dev->ethtool_ops;
1485 void __user *userbuf = useraddr + sizeof(eeprom);
1486 u32 bytes_remaining;
1490 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1491 !ops->get_eeprom_len(dev))
1494 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1497 /* Check for wrap and zero */
1498 if (eeprom.offset + eeprom.len <= eeprom.offset)
1501 /* Check for exceeding total eeprom len */
1502 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1505 data = kmalloc(PAGE_SIZE, GFP_USER);
1509 bytes_remaining = eeprom.len;
1510 while (bytes_remaining > 0) {
1511 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1513 if (copy_from_user(data, userbuf, eeprom.len)) {
1517 ret = ops->set_eeprom(dev, &eeprom, data);
1520 userbuf += eeprom.len;
1521 eeprom.offset += eeprom.len;
1522 bytes_remaining -= eeprom.len;
1529 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1530 void __user *useraddr)
1532 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1535 if (!dev->ethtool_ops->get_coalesce)
1538 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce);
1542 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1548 ethtool_set_coalesce_supported(struct net_device *dev,
1549 struct ethtool_coalesce *coalesce)
1551 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1552 u32 nonzero_params = 0;
1554 if (coalesce->rx_coalesce_usecs)
1555 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1556 if (coalesce->rx_max_coalesced_frames)
1557 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1558 if (coalesce->rx_coalesce_usecs_irq)
1559 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1560 if (coalesce->rx_max_coalesced_frames_irq)
1561 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1562 if (coalesce->tx_coalesce_usecs)
1563 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1564 if (coalesce->tx_max_coalesced_frames)
1565 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1566 if (coalesce->tx_coalesce_usecs_irq)
1567 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1568 if (coalesce->tx_max_coalesced_frames_irq)
1569 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1570 if (coalesce->stats_block_coalesce_usecs)
1571 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1572 if (coalesce->use_adaptive_rx_coalesce)
1573 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1574 if (coalesce->use_adaptive_tx_coalesce)
1575 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1576 if (coalesce->pkt_rate_low)
1577 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1578 if (coalesce->rx_coalesce_usecs_low)
1579 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1580 if (coalesce->rx_max_coalesced_frames_low)
1581 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1582 if (coalesce->tx_coalesce_usecs_low)
1583 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1584 if (coalesce->tx_max_coalesced_frames_low)
1585 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1586 if (coalesce->pkt_rate_high)
1587 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1588 if (coalesce->rx_coalesce_usecs_high)
1589 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1590 if (coalesce->rx_max_coalesced_frames_high)
1591 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1592 if (coalesce->tx_coalesce_usecs_high)
1593 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1594 if (coalesce->tx_max_coalesced_frames_high)
1595 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1596 if (coalesce->rate_sample_interval)
1597 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1599 return (supported_params & nonzero_params) == nonzero_params;
1602 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1603 void __user *useraddr)
1605 struct ethtool_coalesce coalesce;
1608 if (!dev->ethtool_ops->set_coalesce)
1611 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1614 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1617 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce);
1619 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1623 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1625 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1627 if (!dev->ethtool_ops->get_ringparam)
1630 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1632 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1637 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1639 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1642 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1645 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1648 dev->ethtool_ops->get_ringparam(dev, &max);
1650 /* ensure new ring parameters are within the maximums */
1651 if (ringparam.rx_pending > max.rx_max_pending ||
1652 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1653 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1654 ringparam.tx_pending > max.tx_max_pending)
1657 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1659 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1663 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1664 void __user *useraddr)
1666 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1668 if (!dev->ethtool_ops->get_channels)
1671 dev->ethtool_ops->get_channels(dev, &channels);
1673 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1678 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1679 void __user *useraddr)
1681 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1682 u16 from_channel, to_channel;
1683 u32 max_rx_in_use = 0;
1687 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1690 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1693 dev->ethtool_ops->get_channels(dev, &curr);
1695 if (channels.rx_count == curr.rx_count &&
1696 channels.tx_count == curr.tx_count &&
1697 channels.combined_count == curr.combined_count &&
1698 channels.other_count == curr.other_count)
1701 /* ensure new counts are within the maximums */
1702 if (channels.rx_count > curr.max_rx ||
1703 channels.tx_count > curr.max_tx ||
1704 channels.combined_count > curr.max_combined ||
1705 channels.other_count > curr.max_other)
1708 /* ensure there is at least one RX and one TX channel */
1709 if (!channels.combined_count &&
1710 (!channels.rx_count || !channels.tx_count))
1713 /* ensure the new Rx count fits within the configured Rx flow
1714 * indirection table settings */
1715 if (netif_is_rxfh_configured(dev) &&
1716 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1717 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1720 /* Disabling channels, query zero-copy AF_XDP sockets */
1721 from_channel = channels.combined_count +
1722 min(channels.rx_count, channels.tx_count);
1723 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1724 for (i = from_channel; i < to_channel; i++)
1725 if (xsk_get_pool_from_qid(dev, i))
1728 ret = dev->ethtool_ops->set_channels(dev, &channels);
1730 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1734 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1736 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1738 if (!dev->ethtool_ops->get_pauseparam)
1741 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1743 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1748 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1750 struct ethtool_pauseparam pauseparam;
1753 if (!dev->ethtool_ops->set_pauseparam)
1756 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1759 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1761 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1765 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1767 struct ethtool_test test;
1768 const struct ethtool_ops *ops = dev->ethtool_ops;
1772 if (!ops->self_test || !ops->get_sset_count)
1775 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1778 WARN_ON(test_len == 0);
1780 if (copy_from_user(&test, useraddr, sizeof(test)))
1783 test.len = test_len;
1784 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1788 netif_testing_on(dev);
1789 ops->self_test(dev, &test, data);
1790 netif_testing_off(dev);
1793 if (copy_to_user(useraddr, &test, sizeof(test)))
1795 useraddr += sizeof(test);
1796 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1805 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1807 struct ethtool_gstrings gstrings;
1811 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1814 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1817 if (ret > S32_MAX / ETH_GSTRING_LEN)
1824 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1828 __ethtool_get_strings(dev, gstrings.string_set, data);
1834 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1836 useraddr += sizeof(gstrings);
1838 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1847 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1849 struct ethtool_value id;
1851 const struct ethtool_ops *ops = dev->ethtool_ops;
1854 if (!ops->set_phys_id)
1860 if (copy_from_user(&id, useraddr, sizeof(id)))
1863 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1867 /* Drop the RTNL lock while waiting, but prevent reentry or
1868 * removal of the device.
1875 /* Driver will handle this itself */
1876 schedule_timeout_interruptible(
1877 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1879 /* Driver expects to be called at twice the frequency in rc */
1880 int n = rc * 2, interval = HZ / n;
1881 u64 count = n * id.data, i = 0;
1885 rc = ops->set_phys_id(dev,
1886 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1890 schedule_timeout_interruptible(interval);
1891 } while (!signal_pending(current) && (!id.data || i < count));
1898 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1902 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1904 struct ethtool_stats stats;
1905 const struct ethtool_ops *ops = dev->ethtool_ops;
1909 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1912 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1915 if (n_stats > S32_MAX / sizeof(u64))
1917 WARN_ON_ONCE(!n_stats);
1918 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1921 stats.n_stats = n_stats;
1924 data = vzalloc(array_size(n_stats, sizeof(u64)));
1927 ops->get_ethtool_stats(dev, &stats, data);
1933 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1935 useraddr += sizeof(stats);
1936 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
1945 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1947 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
1948 const struct ethtool_ops *ops = dev->ethtool_ops;
1949 struct phy_device *phydev = dev->phydev;
1950 struct ethtool_stats stats;
1954 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1957 if (dev->phydev && !ops->get_ethtool_phy_stats &&
1958 phy_ops && phy_ops->get_sset_count)
1959 n_stats = phy_ops->get_sset_count(dev->phydev);
1961 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1964 if (n_stats > S32_MAX / sizeof(u64))
1966 WARN_ON_ONCE(!n_stats);
1968 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1971 stats.n_stats = n_stats;
1974 data = vzalloc(array_size(n_stats, sizeof(u64)));
1978 if (dev->phydev && !ops->get_ethtool_phy_stats &&
1979 phy_ops && phy_ops->get_stats) {
1980 ret = phy_ops->get_stats(dev->phydev, &stats, data);
1984 ops->get_ethtool_phy_stats(dev, &stats, data);
1991 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1993 useraddr += sizeof(stats);
1994 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2003 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2005 struct ethtool_perm_addr epaddr;
2007 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2010 if (epaddr.size < dev->addr_len)
2012 epaddr.size = dev->addr_len;
2014 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2016 useraddr += sizeof(epaddr);
2017 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2022 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2023 u32 cmd, u32 (*actor)(struct net_device *))
2025 struct ethtool_value edata = { .cmd = cmd };
2030 edata.data = actor(dev);
2032 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2037 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2038 void (*actor)(struct net_device *, u32))
2040 struct ethtool_value edata;
2045 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2048 actor(dev, edata.data);
2052 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2053 int (*actor)(struct net_device *, u32))
2055 struct ethtool_value edata;
2060 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2063 return actor(dev, edata.data);
2066 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2067 char __user *useraddr)
2069 struct ethtool_flash efl;
2071 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2073 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2075 if (!dev->ethtool_ops->flash_device)
2076 return devlink_compat_flash_update(dev, efl.data);
2078 return dev->ethtool_ops->flash_device(dev, &efl);
2081 static int ethtool_set_dump(struct net_device *dev,
2082 void __user *useraddr)
2084 struct ethtool_dump dump;
2086 if (!dev->ethtool_ops->set_dump)
2089 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2092 return dev->ethtool_ops->set_dump(dev, &dump);
2095 static int ethtool_get_dump_flag(struct net_device *dev,
2096 void __user *useraddr)
2099 struct ethtool_dump dump;
2100 const struct ethtool_ops *ops = dev->ethtool_ops;
2102 if (!ops->get_dump_flag)
2105 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2108 ret = ops->get_dump_flag(dev, &dump);
2112 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2117 static int ethtool_get_dump_data(struct net_device *dev,
2118 void __user *useraddr)
2122 struct ethtool_dump dump, tmp;
2123 const struct ethtool_ops *ops = dev->ethtool_ops;
2126 if (!ops->get_dump_data || !ops->get_dump_flag)
2129 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2132 memset(&tmp, 0, sizeof(tmp));
2133 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2134 ret = ops->get_dump_flag(dev, &tmp);
2138 len = min(tmp.len, dump.len);
2142 /* Don't ever let the driver think there's more space available
2143 * than it requested with .get_dump_flag().
2147 /* Always allocate enough space to hold the whole thing so that the
2148 * driver does not need to check the length and bother with partial
2151 data = vzalloc(tmp.len);
2154 ret = ops->get_dump_data(dev, &dump, data);
2158 /* There are two sane possibilities:
2159 * 1. The driver's .get_dump_data() does not touch dump.len.
2160 * 2. Or it may set dump.len to how much it really writes, which
2161 * should be tmp.len (or len if it can do a partial dump).
2162 * In any case respond to userspace with the actual length of data
2165 WARN_ON(dump.len != len && dump.len != tmp.len);
2168 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2172 useraddr += offsetof(struct ethtool_dump, data);
2173 if (copy_to_user(useraddr, data, len))
2180 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2182 struct ethtool_ts_info info;
2185 err = __ethtool_get_ts_info(dev, &info);
2189 if (copy_to_user(useraddr, &info, sizeof(info)))
2195 static int __ethtool_get_module_info(struct net_device *dev,
2196 struct ethtool_modinfo *modinfo)
2198 const struct ethtool_ops *ops = dev->ethtool_ops;
2199 struct phy_device *phydev = dev->phydev;
2202 return sfp_get_module_info(dev->sfp_bus, modinfo);
2204 if (phydev && phydev->drv && phydev->drv->module_info)
2205 return phydev->drv->module_info(phydev, modinfo);
2207 if (ops->get_module_info)
2208 return ops->get_module_info(dev, modinfo);
2213 static int ethtool_get_module_info(struct net_device *dev,
2214 void __user *useraddr)
2217 struct ethtool_modinfo modinfo;
2219 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2222 ret = __ethtool_get_module_info(dev, &modinfo);
2226 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2232 static int __ethtool_get_module_eeprom(struct net_device *dev,
2233 struct ethtool_eeprom *ee, u8 *data)
2235 const struct ethtool_ops *ops = dev->ethtool_ops;
2236 struct phy_device *phydev = dev->phydev;
2239 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2241 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2242 return phydev->drv->module_eeprom(phydev, ee, data);
2244 if (ops->get_module_eeprom)
2245 return ops->get_module_eeprom(dev, ee, data);
2250 static int ethtool_get_module_eeprom(struct net_device *dev,
2251 void __user *useraddr)
2254 struct ethtool_modinfo modinfo;
2256 ret = __ethtool_get_module_info(dev, &modinfo);
2260 return ethtool_get_any_eeprom(dev, useraddr,
2261 __ethtool_get_module_eeprom,
2262 modinfo.eeprom_len);
2265 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2268 case ETHTOOL_RX_COPYBREAK:
2269 case ETHTOOL_TX_COPYBREAK:
2270 if (tuna->len != sizeof(u32) ||
2271 tuna->type_id != ETHTOOL_TUNABLE_U32)
2274 case ETHTOOL_PFC_PREVENTION_TOUT:
2275 if (tuna->len != sizeof(u16) ||
2276 tuna->type_id != ETHTOOL_TUNABLE_U16)
2286 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2289 struct ethtool_tunable tuna;
2290 const struct ethtool_ops *ops = dev->ethtool_ops;
2293 if (!ops->get_tunable)
2295 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2297 ret = ethtool_tunable_valid(&tuna);
2300 data = kmalloc(tuna.len, GFP_USER);
2303 ret = ops->get_tunable(dev, &tuna, data);
2306 useraddr += sizeof(tuna);
2308 if (copy_to_user(useraddr, data, tuna.len))
2317 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2320 struct ethtool_tunable tuna;
2321 const struct ethtool_ops *ops = dev->ethtool_ops;
2324 if (!ops->set_tunable)
2326 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2328 ret = ethtool_tunable_valid(&tuna);
2331 useraddr += sizeof(tuna);
2332 data = memdup_user(useraddr, tuna.len);
2334 return PTR_ERR(data);
2335 ret = ops->set_tunable(dev, &tuna, data);
2341 static noinline_for_stack int
2342 ethtool_get_per_queue_coalesce(struct net_device *dev,
2343 void __user *useraddr,
2344 struct ethtool_per_queue_op *per_queue_opt)
2348 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2350 if (!dev->ethtool_ops->get_per_queue_coalesce)
2353 useraddr += sizeof(*per_queue_opt);
2355 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2358 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2359 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2361 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2364 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2366 useraddr += sizeof(coalesce);
2372 static noinline_for_stack int
2373 ethtool_set_per_queue_coalesce(struct net_device *dev,
2374 void __user *useraddr,
2375 struct ethtool_per_queue_op *per_queue_opt)
2380 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2381 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2383 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2384 (!dev->ethtool_ops->get_per_queue_coalesce))
2387 useraddr += sizeof(*per_queue_opt);
2389 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2390 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2391 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2395 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2396 struct ethtool_coalesce coalesce;
2398 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2404 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2409 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2414 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2418 useraddr += sizeof(coalesce);
2424 for_each_set_bit(i, queue_mask, bit) {
2425 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2434 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2435 void __user *useraddr, u32 sub_cmd)
2437 struct ethtool_per_queue_op per_queue_opt;
2439 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2442 if (per_queue_opt.sub_command != sub_cmd)
2445 switch (per_queue_opt.sub_command) {
2446 case ETHTOOL_GCOALESCE:
2447 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2448 case ETHTOOL_SCOALESCE:
2449 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2455 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2458 case ETHTOOL_PHY_DOWNSHIFT:
2459 case ETHTOOL_PHY_FAST_LINK_DOWN:
2460 if (tuna->len != sizeof(u8) ||
2461 tuna->type_id != ETHTOOL_TUNABLE_U8)
2464 case ETHTOOL_PHY_EDPD:
2465 if (tuna->len != sizeof(u16) ||
2466 tuna->type_id != ETHTOOL_TUNABLE_U16)
2476 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2478 struct phy_device *phydev = dev->phydev;
2479 struct ethtool_tunable tuna;
2480 bool phy_drv_tunable;
2484 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2485 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2487 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2489 ret = ethtool_phy_tunable_valid(&tuna);
2492 data = kmalloc(tuna.len, GFP_USER);
2495 if (phy_drv_tunable) {
2496 mutex_lock(&phydev->lock);
2497 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2498 mutex_unlock(&phydev->lock);
2500 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2504 useraddr += sizeof(tuna);
2506 if (copy_to_user(useraddr, data, tuna.len))
2515 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2517 struct phy_device *phydev = dev->phydev;
2518 struct ethtool_tunable tuna;
2519 bool phy_drv_tunable;
2523 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2524 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2526 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2528 ret = ethtool_phy_tunable_valid(&tuna);
2531 useraddr += sizeof(tuna);
2532 data = memdup_user(useraddr, tuna.len);
2534 return PTR_ERR(data);
2535 if (phy_drv_tunable) {
2536 mutex_lock(&phydev->lock);
2537 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2538 mutex_unlock(&phydev->lock);
2540 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2547 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2549 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2552 if (!dev->ethtool_ops->get_fecparam)
2555 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2559 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2564 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2566 struct ethtool_fecparam fecparam;
2568 if (!dev->ethtool_ops->set_fecparam)
2571 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2574 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2577 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2579 int dev_ethtool(struct net *net, struct ifreq *ifr)
2581 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2582 void __user *useraddr = ifr->ifr_data;
2583 u32 ethcmd, sub_cmd;
2585 netdev_features_t old_features;
2587 if (!dev || !netif_device_present(dev))
2590 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2593 if (ethcmd == ETHTOOL_PERQUEUE) {
2594 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2599 /* Allow some commands to be done by anyone */
2602 case ETHTOOL_GDRVINFO:
2603 case ETHTOOL_GMSGLVL:
2605 case ETHTOOL_GCOALESCE:
2606 case ETHTOOL_GRINGPARAM:
2607 case ETHTOOL_GPAUSEPARAM:
2608 case ETHTOOL_GRXCSUM:
2609 case ETHTOOL_GTXCSUM:
2611 case ETHTOOL_GSSET_INFO:
2612 case ETHTOOL_GSTRINGS:
2613 case ETHTOOL_GSTATS:
2614 case ETHTOOL_GPHYSTATS:
2616 case ETHTOOL_GPERMADDR:
2620 case ETHTOOL_GFLAGS:
2621 case ETHTOOL_GPFLAGS:
2623 case ETHTOOL_GRXRINGS:
2624 case ETHTOOL_GRXCLSRLCNT:
2625 case ETHTOOL_GRXCLSRULE:
2626 case ETHTOOL_GRXCLSRLALL:
2627 case ETHTOOL_GRXFHINDIR:
2629 case ETHTOOL_GFEATURES:
2630 case ETHTOOL_GCHANNELS:
2631 case ETHTOOL_GET_TS_INFO:
2633 case ETHTOOL_GTUNABLE:
2634 case ETHTOOL_PHY_GTUNABLE:
2635 case ETHTOOL_GLINKSETTINGS:
2636 case ETHTOOL_GFECPARAM:
2639 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2643 if (dev->ethtool_ops->begin) {
2644 rc = dev->ethtool_ops->begin(dev);
2648 old_features = dev->features;
2652 rc = ethtool_get_settings(dev, useraddr);
2655 rc = ethtool_set_settings(dev, useraddr);
2657 case ETHTOOL_GDRVINFO:
2658 rc = ethtool_get_drvinfo(dev, useraddr);
2661 rc = ethtool_get_regs(dev, useraddr);
2664 rc = ethtool_get_wol(dev, useraddr);
2667 rc = ethtool_set_wol(dev, useraddr);
2669 case ETHTOOL_GMSGLVL:
2670 rc = ethtool_get_value(dev, useraddr, ethcmd,
2671 dev->ethtool_ops->get_msglevel);
2673 case ETHTOOL_SMSGLVL:
2674 rc = ethtool_set_value_void(dev, useraddr,
2675 dev->ethtool_ops->set_msglevel);
2677 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2680 rc = ethtool_get_eee(dev, useraddr);
2683 rc = ethtool_set_eee(dev, useraddr);
2685 case ETHTOOL_NWAY_RST:
2686 rc = ethtool_nway_reset(dev);
2689 rc = ethtool_get_link(dev, useraddr);
2691 case ETHTOOL_GEEPROM:
2692 rc = ethtool_get_eeprom(dev, useraddr);
2694 case ETHTOOL_SEEPROM:
2695 rc = ethtool_set_eeprom(dev, useraddr);
2697 case ETHTOOL_GCOALESCE:
2698 rc = ethtool_get_coalesce(dev, useraddr);
2700 case ETHTOOL_SCOALESCE:
2701 rc = ethtool_set_coalesce(dev, useraddr);
2703 case ETHTOOL_GRINGPARAM:
2704 rc = ethtool_get_ringparam(dev, useraddr);
2706 case ETHTOOL_SRINGPARAM:
2707 rc = ethtool_set_ringparam(dev, useraddr);
2709 case ETHTOOL_GPAUSEPARAM:
2710 rc = ethtool_get_pauseparam(dev, useraddr);
2712 case ETHTOOL_SPAUSEPARAM:
2713 rc = ethtool_set_pauseparam(dev, useraddr);
2716 rc = ethtool_self_test(dev, useraddr);
2718 case ETHTOOL_GSTRINGS:
2719 rc = ethtool_get_strings(dev, useraddr);
2721 case ETHTOOL_PHYS_ID:
2722 rc = ethtool_phys_id(dev, useraddr);
2724 case ETHTOOL_GSTATS:
2725 rc = ethtool_get_stats(dev, useraddr);
2727 case ETHTOOL_GPERMADDR:
2728 rc = ethtool_get_perm_addr(dev, useraddr);
2730 case ETHTOOL_GFLAGS:
2731 rc = ethtool_get_value(dev, useraddr, ethcmd,
2732 __ethtool_get_flags);
2734 case ETHTOOL_SFLAGS:
2735 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2737 case ETHTOOL_GPFLAGS:
2738 rc = ethtool_get_value(dev, useraddr, ethcmd,
2739 dev->ethtool_ops->get_priv_flags);
2741 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2743 case ETHTOOL_SPFLAGS:
2744 rc = ethtool_set_value(dev, useraddr,
2745 dev->ethtool_ops->set_priv_flags);
2748 case ETHTOOL_GRXRINGS:
2749 case ETHTOOL_GRXCLSRLCNT:
2750 case ETHTOOL_GRXCLSRULE:
2751 case ETHTOOL_GRXCLSRLALL:
2752 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2755 case ETHTOOL_SRXCLSRLDEL:
2756 case ETHTOOL_SRXCLSRLINS:
2757 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2759 case ETHTOOL_FLASHDEV:
2760 rc = ethtool_flash_device(dev, useraddr);
2763 rc = ethtool_reset(dev, useraddr);
2765 case ETHTOOL_GSSET_INFO:
2766 rc = ethtool_get_sset_info(dev, useraddr);
2768 case ETHTOOL_GRXFHINDIR:
2769 rc = ethtool_get_rxfh_indir(dev, useraddr);
2771 case ETHTOOL_SRXFHINDIR:
2772 rc = ethtool_set_rxfh_indir(dev, useraddr);
2775 rc = ethtool_get_rxfh(dev, useraddr);
2778 rc = ethtool_set_rxfh(dev, useraddr);
2780 case ETHTOOL_GFEATURES:
2781 rc = ethtool_get_features(dev, useraddr);
2783 case ETHTOOL_SFEATURES:
2784 rc = ethtool_set_features(dev, useraddr);
2786 case ETHTOOL_GTXCSUM:
2787 case ETHTOOL_GRXCSUM:
2792 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2794 case ETHTOOL_STXCSUM:
2795 case ETHTOOL_SRXCSUM:
2800 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2802 case ETHTOOL_GCHANNELS:
2803 rc = ethtool_get_channels(dev, useraddr);
2805 case ETHTOOL_SCHANNELS:
2806 rc = ethtool_set_channels(dev, useraddr);
2808 case ETHTOOL_SET_DUMP:
2809 rc = ethtool_set_dump(dev, useraddr);
2811 case ETHTOOL_GET_DUMP_FLAG:
2812 rc = ethtool_get_dump_flag(dev, useraddr);
2814 case ETHTOOL_GET_DUMP_DATA:
2815 rc = ethtool_get_dump_data(dev, useraddr);
2817 case ETHTOOL_GET_TS_INFO:
2818 rc = ethtool_get_ts_info(dev, useraddr);
2820 case ETHTOOL_GMODULEINFO:
2821 rc = ethtool_get_module_info(dev, useraddr);
2823 case ETHTOOL_GMODULEEEPROM:
2824 rc = ethtool_get_module_eeprom(dev, useraddr);
2826 case ETHTOOL_GTUNABLE:
2827 rc = ethtool_get_tunable(dev, useraddr);
2829 case ETHTOOL_STUNABLE:
2830 rc = ethtool_set_tunable(dev, useraddr);
2832 case ETHTOOL_GPHYSTATS:
2833 rc = ethtool_get_phy_stats(dev, useraddr);
2835 case ETHTOOL_PERQUEUE:
2836 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2838 case ETHTOOL_GLINKSETTINGS:
2839 rc = ethtool_get_link_ksettings(dev, useraddr);
2841 case ETHTOOL_SLINKSETTINGS:
2842 rc = ethtool_set_link_ksettings(dev, useraddr);
2844 case ETHTOOL_PHY_GTUNABLE:
2845 rc = get_phy_tunable(dev, useraddr);
2847 case ETHTOOL_PHY_STUNABLE:
2848 rc = set_phy_tunable(dev, useraddr);
2850 case ETHTOOL_GFECPARAM:
2851 rc = ethtool_get_fecparam(dev, useraddr);
2853 case ETHTOOL_SFECPARAM:
2854 rc = ethtool_set_fecparam(dev, useraddr);
2860 if (dev->ethtool_ops->complete)
2861 dev->ethtool_ops->complete(dev);
2863 if (old_features != dev->features)
2864 netdev_features_change(dev);
2869 struct ethtool_rx_flow_key {
2870 struct flow_dissector_key_basic basic;
2872 struct flow_dissector_key_ipv4_addrs ipv4;
2873 struct flow_dissector_key_ipv6_addrs ipv6;
2875 struct flow_dissector_key_ports tp;
2876 struct flow_dissector_key_ip ip;
2877 struct flow_dissector_key_vlan vlan;
2878 struct flow_dissector_key_eth_addrs eth_addrs;
2879 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2881 struct ethtool_rx_flow_match {
2882 struct flow_dissector dissector;
2883 struct ethtool_rx_flow_key key;
2884 struct ethtool_rx_flow_key mask;
2887 struct ethtool_rx_flow_rule *
2888 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2890 const struct ethtool_rx_flow_spec *fs = input->fs;
2891 static struct in6_addr zero_addr = {};
2892 struct ethtool_rx_flow_match *match;
2893 struct ethtool_rx_flow_rule *flow;
2894 struct flow_action_entry *act;
2896 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2897 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2899 return ERR_PTR(-ENOMEM);
2901 /* ethtool_rx supports only one single action per rule. */
2902 flow->rule = flow_rule_alloc(1);
2905 return ERR_PTR(-ENOMEM);
2908 match = (struct ethtool_rx_flow_match *)flow->priv;
2909 flow->rule->match.dissector = &match->dissector;
2910 flow->rule->match.mask = &match->mask;
2911 flow->rule->match.key = &match->key;
2913 match->mask.basic.n_proto = htons(0xffff);
2915 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2917 const struct ethhdr *ether_spec, *ether_m_spec;
2919 ether_spec = &fs->h_u.ether_spec;
2920 ether_m_spec = &fs->m_u.ether_spec;
2922 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2923 ether_addr_copy(match->key.eth_addrs.src,
2924 ether_spec->h_source);
2925 ether_addr_copy(match->mask.eth_addrs.src,
2926 ether_m_spec->h_source);
2928 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2929 ether_addr_copy(match->key.eth_addrs.dst,
2930 ether_spec->h_dest);
2931 ether_addr_copy(match->mask.eth_addrs.dst,
2932 ether_m_spec->h_dest);
2934 if (ether_m_spec->h_proto) {
2935 match->key.basic.n_proto = ether_spec->h_proto;
2936 match->mask.basic.n_proto = ether_m_spec->h_proto;
2942 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2944 match->key.basic.n_proto = htons(ETH_P_IP);
2946 v4_spec = &fs->h_u.tcp_ip4_spec;
2947 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2949 if (v4_m_spec->ip4src) {
2950 match->key.ipv4.src = v4_spec->ip4src;
2951 match->mask.ipv4.src = v4_m_spec->ip4src;
2953 if (v4_m_spec->ip4dst) {
2954 match->key.ipv4.dst = v4_spec->ip4dst;
2955 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2957 if (v4_m_spec->ip4src ||
2958 v4_m_spec->ip4dst) {
2959 match->dissector.used_keys |=
2960 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2961 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2962 offsetof(struct ethtool_rx_flow_key, ipv4);
2964 if (v4_m_spec->psrc) {
2965 match->key.tp.src = v4_spec->psrc;
2966 match->mask.tp.src = v4_m_spec->psrc;
2968 if (v4_m_spec->pdst) {
2969 match->key.tp.dst = v4_spec->pdst;
2970 match->mask.tp.dst = v4_m_spec->pdst;
2972 if (v4_m_spec->psrc ||
2974 match->dissector.used_keys |=
2975 BIT(FLOW_DISSECTOR_KEY_PORTS);
2976 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2977 offsetof(struct ethtool_rx_flow_key, tp);
2979 if (v4_m_spec->tos) {
2980 match->key.ip.tos = v4_spec->tos;
2981 match->mask.ip.tos = v4_m_spec->tos;
2982 match->dissector.used_keys |=
2983 BIT(FLOW_DISSECTOR_KEY_IP);
2984 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2985 offsetof(struct ethtool_rx_flow_key, ip);
2991 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2993 match->key.basic.n_proto = htons(ETH_P_IPV6);
2995 v6_spec = &fs->h_u.tcp_ip6_spec;
2996 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2997 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2998 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2999 sizeof(match->key.ipv6.src));
3000 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3001 sizeof(match->mask.ipv6.src));
3003 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3004 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3005 sizeof(match->key.ipv6.dst));
3006 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3007 sizeof(match->mask.ipv6.dst));
3009 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
3010 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3011 match->dissector.used_keys |=
3012 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3013 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3014 offsetof(struct ethtool_rx_flow_key, ipv6);
3016 if (v6_m_spec->psrc) {
3017 match->key.tp.src = v6_spec->psrc;
3018 match->mask.tp.src = v6_m_spec->psrc;
3020 if (v6_m_spec->pdst) {
3021 match->key.tp.dst = v6_spec->pdst;
3022 match->mask.tp.dst = v6_m_spec->pdst;
3024 if (v6_m_spec->psrc ||
3026 match->dissector.used_keys |=
3027 BIT(FLOW_DISSECTOR_KEY_PORTS);
3028 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3029 offsetof(struct ethtool_rx_flow_key, tp);
3031 if (v6_m_spec->tclass) {
3032 match->key.ip.tos = v6_spec->tclass;
3033 match->mask.ip.tos = v6_m_spec->tclass;
3034 match->dissector.used_keys |=
3035 BIT(FLOW_DISSECTOR_KEY_IP);
3036 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3037 offsetof(struct ethtool_rx_flow_key, ip);
3042 ethtool_rx_flow_rule_destroy(flow);
3043 return ERR_PTR(-EINVAL);
3046 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3049 match->key.basic.ip_proto = IPPROTO_TCP;
3050 match->mask.basic.ip_proto = 0xff;
3054 match->key.basic.ip_proto = IPPROTO_UDP;
3055 match->mask.basic.ip_proto = 0xff;
3059 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3060 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3061 offsetof(struct ethtool_rx_flow_key, basic);
3063 if (fs->flow_type & FLOW_EXT) {
3064 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3065 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3067 if (ext_m_spec->vlan_etype) {
3068 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3069 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3072 if (ext_m_spec->vlan_tci) {
3073 match->key.vlan.vlan_id =
3074 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3075 match->mask.vlan.vlan_id =
3076 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3078 match->key.vlan.vlan_dei =
3079 !!(ext_h_spec->vlan_tci & htons(0x1000));
3080 match->mask.vlan.vlan_dei =
3081 !!(ext_m_spec->vlan_tci & htons(0x1000));
3083 match->key.vlan.vlan_priority =
3084 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3085 match->mask.vlan.vlan_priority =
3086 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3089 if (ext_m_spec->vlan_etype ||
3090 ext_m_spec->vlan_tci) {
3091 match->dissector.used_keys |=
3092 BIT(FLOW_DISSECTOR_KEY_VLAN);
3093 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3094 offsetof(struct ethtool_rx_flow_key, vlan);
3097 if (fs->flow_type & FLOW_MAC_EXT) {
3098 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3099 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3101 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3103 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3106 match->dissector.used_keys |=
3107 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3108 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3109 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3112 act = &flow->rule->action.entries[0];
3113 switch (fs->ring_cookie) {
3114 case RX_CLS_FLOW_DISC:
3115 act->id = FLOW_ACTION_DROP;
3117 case RX_CLS_FLOW_WAKE:
3118 act->id = FLOW_ACTION_WAKE;
3121 act->id = FLOW_ACTION_QUEUE;
3122 if (fs->flow_type & FLOW_RSS)
3123 act->queue.ctx = input->rss_ctx;
3125 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3126 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3132 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3134 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3139 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);