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/vermagic.h>
21 #include <linux/vmalloc.h>
22 #include <linux/sfp.h>
23 #include <linux/slab.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/sched/signal.h>
26 #include <linux/net.h>
27 #include <net/devlink.h>
28 #include <net/xdp_sock.h>
29 #include <net/flow_offload.h>
30 #include <linux/ethtool_netlink.h>
35 * Some useful ethtool_ops methods that're device independent.
36 * If we find that all drivers want to do the same thing here,
37 * we can turn these into dev_() function calls.
40 u32 ethtool_op_get_link(struct net_device *dev)
42 return netif_carrier_ok(dev) ? 1 : 0;
44 EXPORT_SYMBOL(ethtool_op_get_link);
46 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
48 info->so_timestamping =
49 SOF_TIMESTAMPING_TX_SOFTWARE |
50 SOF_TIMESTAMPING_RX_SOFTWARE |
51 SOF_TIMESTAMPING_SOFTWARE;
55 EXPORT_SYMBOL(ethtool_op_get_ts_info);
57 /* Handlers for each ethtool command */
59 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
61 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
63 struct ethtool_gfeatures cmd = {
64 .cmd = ETHTOOL_GFEATURES,
65 .size = ETHTOOL_DEV_FEATURE_WORDS,
67 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
72 /* in case feature bits run out again */
73 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
75 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
76 features[i].available = (u32)(dev->hw_features >> (32 * i));
77 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
78 features[i].active = (u32)(dev->features >> (32 * i));
79 features[i].never_changed =
80 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
83 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
84 if (get_user(copy_size, sizeaddr))
87 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
88 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
90 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
92 useraddr += sizeof(cmd);
93 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
99 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
101 struct ethtool_sfeatures cmd;
102 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
103 netdev_features_t wanted = 0, valid = 0;
106 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
108 useraddr += sizeof(cmd);
110 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
113 if (copy_from_user(features, useraddr, sizeof(features)))
116 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
117 valid |= (netdev_features_t)features[i].valid << (32 * i);
118 wanted |= (netdev_features_t)features[i].requested << (32 * i);
121 if (valid & ~NETIF_F_ETHTOOL_BITS)
124 if (valid & ~dev->hw_features) {
125 valid &= dev->hw_features;
126 ret |= ETHTOOL_F_UNSUPPORTED;
129 dev->wanted_features &= ~valid;
130 dev->wanted_features |= wanted & valid;
131 __netdev_update_features(dev);
133 if ((dev->wanted_features ^ dev->features) & valid)
134 ret |= ETHTOOL_F_WISH;
139 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
141 const struct ethtool_ops *ops = dev->ethtool_ops;
143 if (sset == ETH_SS_FEATURES)
144 return ARRAY_SIZE(netdev_features_strings);
146 if (sset == ETH_SS_RSS_HASH_FUNCS)
147 return ARRAY_SIZE(rss_hash_func_strings);
149 if (sset == ETH_SS_TUNABLES)
150 return ARRAY_SIZE(tunable_strings);
152 if (sset == ETH_SS_PHY_TUNABLES)
153 return ARRAY_SIZE(phy_tunable_strings);
155 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
156 !ops->get_ethtool_phy_stats)
157 return phy_ethtool_get_sset_count(dev->phydev);
159 if (sset == ETH_SS_LINK_MODES)
160 return __ETHTOOL_LINK_MODE_MASK_NBITS;
162 if (ops->get_sset_count && ops->get_strings)
163 return ops->get_sset_count(dev, sset);
168 static void __ethtool_get_strings(struct net_device *dev,
169 u32 stringset, u8 *data)
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)
185 phy_ethtool_get_strings(dev->phydev, data);
186 else if (stringset == ETH_SS_LINK_MODES)
187 memcpy(data, link_mode_names,
188 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
190 /* ops->get_strings is valid because checked earlier */
191 ops->get_strings(dev, stringset, data);
194 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
196 /* feature masks of legacy discrete ethtool ops */
199 case ETHTOOL_GTXCSUM:
200 case ETHTOOL_STXCSUM:
201 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
202 case ETHTOOL_GRXCSUM:
203 case ETHTOOL_SRXCSUM:
204 return NETIF_F_RXCSUM;
210 return NETIF_F_ALL_TSO;
222 static int ethtool_get_one_feature(struct net_device *dev,
223 char __user *useraddr, u32 ethcmd)
225 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
226 struct ethtool_value edata = {
228 .data = !!(dev->features & mask),
231 if (copy_to_user(useraddr, &edata, sizeof(edata)))
236 static int ethtool_set_one_feature(struct net_device *dev,
237 void __user *useraddr, u32 ethcmd)
239 struct ethtool_value edata;
240 netdev_features_t mask;
242 if (copy_from_user(&edata, useraddr, sizeof(edata)))
245 mask = ethtool_get_feature_mask(ethcmd);
246 mask &= dev->hw_features;
251 dev->wanted_features |= mask;
253 dev->wanted_features &= ~mask;
255 __netdev_update_features(dev);
260 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
261 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
262 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
263 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
266 static u32 __ethtool_get_flags(struct net_device *dev)
270 if (dev->features & NETIF_F_LRO)
271 flags |= ETH_FLAG_LRO;
272 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
273 flags |= ETH_FLAG_RXVLAN;
274 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
275 flags |= ETH_FLAG_TXVLAN;
276 if (dev->features & NETIF_F_NTUPLE)
277 flags |= ETH_FLAG_NTUPLE;
278 if (dev->features & NETIF_F_RXHASH)
279 flags |= ETH_FLAG_RXHASH;
284 static int __ethtool_set_flags(struct net_device *dev, u32 data)
286 netdev_features_t features = 0, changed;
288 if (data & ~ETH_ALL_FLAGS)
291 if (data & ETH_FLAG_LRO)
292 features |= NETIF_F_LRO;
293 if (data & ETH_FLAG_RXVLAN)
294 features |= NETIF_F_HW_VLAN_CTAG_RX;
295 if (data & ETH_FLAG_TXVLAN)
296 features |= NETIF_F_HW_VLAN_CTAG_TX;
297 if (data & ETH_FLAG_NTUPLE)
298 features |= NETIF_F_NTUPLE;
299 if (data & ETH_FLAG_RXHASH)
300 features |= NETIF_F_RXHASH;
302 /* allow changing only bits set in hw_features */
303 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
304 if (changed & ~dev->hw_features)
305 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
307 dev->wanted_features =
308 (dev->wanted_features & ~changed) | (features & changed);
310 __netdev_update_features(dev);
315 /* Given two link masks, AND them together and save the result in dst. */
316 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
317 struct ethtool_link_ksettings *src)
319 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
320 unsigned int idx = 0;
322 for (; idx < size; idx++) {
323 dst->link_modes.supported[idx] &=
324 src->link_modes.supported[idx];
325 dst->link_modes.advertising[idx] &=
326 src->link_modes.advertising[idx];
329 EXPORT_SYMBOL(ethtool_intersect_link_masks);
331 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
334 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
337 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
339 /* return false if src had higher bits set. lower bits always updated. */
340 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
341 const unsigned long *src)
345 /* TODO: following test will soon always be true */
346 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
347 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
349 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
350 bitmap_fill(ext, 32);
351 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
352 if (bitmap_intersects(ext, src,
353 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
354 /* src mask goes beyond bit 31 */
358 *legacy_u32 = src[0];
361 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
363 /* return false if ksettings link modes had higher bits
364 * set. legacy_settings always updated (best effort)
367 convert_link_ksettings_to_legacy_settings(
368 struct ethtool_cmd *legacy_settings,
369 const struct ethtool_link_ksettings *link_ksettings)
373 memset(legacy_settings, 0, sizeof(*legacy_settings));
374 /* this also clears the deprecated fields in legacy structure:
380 retval &= ethtool_convert_link_mode_to_legacy_u32(
381 &legacy_settings->supported,
382 link_ksettings->link_modes.supported);
383 retval &= ethtool_convert_link_mode_to_legacy_u32(
384 &legacy_settings->advertising,
385 link_ksettings->link_modes.advertising);
386 retval &= ethtool_convert_link_mode_to_legacy_u32(
387 &legacy_settings->lp_advertising,
388 link_ksettings->link_modes.lp_advertising);
389 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
390 legacy_settings->duplex
391 = link_ksettings->base.duplex;
392 legacy_settings->port
393 = link_ksettings->base.port;
394 legacy_settings->phy_address
395 = link_ksettings->base.phy_address;
396 legacy_settings->autoneg
397 = link_ksettings->base.autoneg;
398 legacy_settings->mdio_support
399 = link_ksettings->base.mdio_support;
400 legacy_settings->eth_tp_mdix
401 = link_ksettings->base.eth_tp_mdix;
402 legacy_settings->eth_tp_mdix_ctrl
403 = link_ksettings->base.eth_tp_mdix_ctrl;
404 legacy_settings->transceiver
405 = link_ksettings->base.transceiver;
409 /* number of 32-bit words to store the user's link mode bitmaps */
410 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
411 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
413 /* layout of the struct passed from/to userland */
414 struct ethtool_link_usettings {
415 struct ethtool_link_settings base;
417 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
418 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
419 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
423 /* Internal kernel helper to query a device ethtool_link_settings. */
424 int __ethtool_get_link_ksettings(struct net_device *dev,
425 struct ethtool_link_ksettings *link_ksettings)
429 if (!dev->ethtool_ops->get_link_ksettings)
432 memset(link_ksettings, 0, sizeof(*link_ksettings));
433 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
435 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
437 /* convert ethtool_link_usettings in user space to a kernel internal
438 * ethtool_link_ksettings. return 0 on success, errno on error.
440 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
441 const void __user *from)
443 struct ethtool_link_usettings link_usettings;
445 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
448 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
449 bitmap_from_arr32(to->link_modes.supported,
450 link_usettings.link_modes.supported,
451 __ETHTOOL_LINK_MODE_MASK_NBITS);
452 bitmap_from_arr32(to->link_modes.advertising,
453 link_usettings.link_modes.advertising,
454 __ETHTOOL_LINK_MODE_MASK_NBITS);
455 bitmap_from_arr32(to->link_modes.lp_advertising,
456 link_usettings.link_modes.lp_advertising,
457 __ETHTOOL_LINK_MODE_MASK_NBITS);
462 /* convert a kernel internal ethtool_link_ksettings to
463 * ethtool_link_usettings in user space. return 0 on success, errno on
467 store_link_ksettings_for_user(void __user *to,
468 const struct ethtool_link_ksettings *from)
470 struct ethtool_link_usettings link_usettings;
472 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
473 bitmap_to_arr32(link_usettings.link_modes.supported,
474 from->link_modes.supported,
475 __ETHTOOL_LINK_MODE_MASK_NBITS);
476 bitmap_to_arr32(link_usettings.link_modes.advertising,
477 from->link_modes.advertising,
478 __ETHTOOL_LINK_MODE_MASK_NBITS);
479 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
480 from->link_modes.lp_advertising,
481 __ETHTOOL_LINK_MODE_MASK_NBITS);
483 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
489 /* Query device for its ethtool_link_settings. */
490 static int ethtool_get_link_ksettings(struct net_device *dev,
491 void __user *useraddr)
494 struct ethtool_link_ksettings link_ksettings;
497 if (!dev->ethtool_ops->get_link_ksettings)
500 /* handle bitmap nbits handshake */
501 if (copy_from_user(&link_ksettings.base, useraddr,
502 sizeof(link_ksettings.base)))
505 if (__ETHTOOL_LINK_MODE_MASK_NU32
506 != link_ksettings.base.link_mode_masks_nwords) {
507 /* wrong link mode nbits requested */
508 memset(&link_ksettings, 0, sizeof(link_ksettings));
509 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
510 /* send back number of words required as negative val */
511 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
512 "need too many bits for link modes!");
513 link_ksettings.base.link_mode_masks_nwords
514 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
516 /* copy the base fields back to user, not the link
519 if (copy_to_user(useraddr, &link_ksettings.base,
520 sizeof(link_ksettings.base)))
526 /* handshake successful: user/kernel agree on
527 * link_mode_masks_nwords
530 memset(&link_ksettings, 0, sizeof(link_ksettings));
531 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
535 /* make sure we tell the right values to user */
536 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
537 link_ksettings.base.link_mode_masks_nwords
538 = __ETHTOOL_LINK_MODE_MASK_NU32;
540 return store_link_ksettings_for_user(useraddr, &link_ksettings);
543 /* Update device ethtool_link_settings. */
544 static int ethtool_set_link_ksettings(struct net_device *dev,
545 void __user *useraddr)
548 struct ethtool_link_ksettings link_ksettings;
552 if (!dev->ethtool_ops->set_link_ksettings)
555 /* make sure nbits field has expected value */
556 if (copy_from_user(&link_ksettings.base, useraddr,
557 sizeof(link_ksettings.base)))
560 if (__ETHTOOL_LINK_MODE_MASK_NU32
561 != link_ksettings.base.link_mode_masks_nwords)
564 /* copy the whole structure, now that we know it has expected
567 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
571 /* re-check nwords field, just in case */
572 if (__ETHTOOL_LINK_MODE_MASK_NU32
573 != link_ksettings.base.link_mode_masks_nwords)
576 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
578 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
579 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
584 /* Query device for its ethtool_cmd settings.
586 * Backward compatibility note: for compatibility with legacy ethtool, this is
587 * now implemented via get_link_ksettings. When driver reports higher link mode
588 * bits, a kernel warning is logged once (with name of 1st driver/device) to
589 * recommend user to upgrade ethtool, but the command is successful (only the
590 * lower link mode bits reported back to user). Deprecated fields from
591 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
593 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
595 struct ethtool_link_ksettings link_ksettings;
596 struct ethtool_cmd cmd;
600 if (!dev->ethtool_ops->get_link_ksettings)
603 memset(&link_ksettings, 0, sizeof(link_ksettings));
604 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
607 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
609 /* send a sensible cmd tag back to user */
610 cmd.cmd = ETHTOOL_GSET;
612 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
618 /* Update device link settings with given ethtool_cmd.
620 * Backward compatibility note: for compatibility with legacy ethtool, this is
621 * now always implemented via set_link_settings. When user's request updates
622 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
623 * warning is logged once (with name of 1st driver/device) to recommend user to
624 * upgrade ethtool, and the request is rejected.
626 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
628 struct ethtool_link_ksettings link_ksettings;
629 struct ethtool_cmd cmd;
634 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
636 if (!dev->ethtool_ops->set_link_ksettings)
639 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
641 link_ksettings.base.link_mode_masks_nwords =
642 __ETHTOOL_LINK_MODE_MASK_NU32;
643 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
645 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
646 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
651 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
652 void __user *useraddr)
654 struct ethtool_drvinfo info;
655 const struct ethtool_ops *ops = dev->ethtool_ops;
657 memset(&info, 0, sizeof(info));
658 info.cmd = ETHTOOL_GDRVINFO;
659 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
660 if (ops->get_drvinfo) {
661 ops->get_drvinfo(dev, &info);
662 } else if (dev->dev.parent && dev->dev.parent->driver) {
663 strlcpy(info.bus_info, dev_name(dev->dev.parent),
664 sizeof(info.bus_info));
665 strlcpy(info.driver, dev->dev.parent->driver->name,
666 sizeof(info.driver));
672 * this method of obtaining string set info is deprecated;
673 * Use ETHTOOL_GSSET_INFO instead.
675 if (ops->get_sset_count) {
678 rc = ops->get_sset_count(dev, ETH_SS_TEST);
680 info.testinfo_len = rc;
681 rc = ops->get_sset_count(dev, ETH_SS_STATS);
684 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
686 info.n_priv_flags = rc;
688 if (ops->get_regs_len) {
689 int ret = ops->get_regs_len(dev);
692 info.regdump_len = ret;
695 if (ops->get_eeprom_len)
696 info.eedump_len = ops->get_eeprom_len(dev);
698 if (!info.fw_version[0])
699 devlink_compat_running_version(dev, info.fw_version,
700 sizeof(info.fw_version));
702 if (copy_to_user(useraddr, &info, sizeof(info)))
707 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
708 void __user *useraddr)
710 struct ethtool_sset_info info;
712 int i, idx = 0, n_bits = 0, ret, rc;
713 u32 *info_buf = NULL;
715 if (copy_from_user(&info, useraddr, sizeof(info)))
718 /* store copy of mask, because we zero struct later on */
719 sset_mask = info.sset_mask;
723 /* calculate size of return buffer */
724 n_bits = hweight64(sset_mask);
726 memset(&info, 0, sizeof(info));
727 info.cmd = ETHTOOL_GSSET_INFO;
729 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
734 * fill return buffer based on input bitmask and successful
735 * get_sset_count return
737 for (i = 0; i < 64; i++) {
738 if (!(sset_mask & (1ULL << i)))
741 rc = __ethtool_get_sset_count(dev, i);
743 info.sset_mask |= (1ULL << i);
744 info_buf[idx++] = rc;
749 if (copy_to_user(useraddr, &info, sizeof(info)))
752 useraddr += offsetof(struct ethtool_sset_info, data);
753 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
763 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
764 u32 cmd, void __user *useraddr)
766 struct ethtool_rxnfc info;
767 size_t info_size = sizeof(info);
770 if (!dev->ethtool_ops->set_rxnfc)
773 /* struct ethtool_rxnfc was originally defined for
774 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
775 * members. User-space might still be using that
777 if (cmd == ETHTOOL_SRXFH)
778 info_size = (offsetof(struct ethtool_rxnfc, data) +
781 if (copy_from_user(&info, useraddr, info_size))
784 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
788 if (cmd == ETHTOOL_SRXCLSRLINS &&
789 copy_to_user(useraddr, &info, info_size))
795 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
796 u32 cmd, void __user *useraddr)
798 struct ethtool_rxnfc info;
799 size_t info_size = sizeof(info);
800 const struct ethtool_ops *ops = dev->ethtool_ops;
802 void *rule_buf = NULL;
807 /* struct ethtool_rxnfc was originally defined for
808 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
809 * members. User-space might still be using that
811 if (cmd == ETHTOOL_GRXFH)
812 info_size = (offsetof(struct ethtool_rxnfc, data) +
815 if (copy_from_user(&info, useraddr, info_size))
818 /* If FLOW_RSS was requested then user-space must be using the
819 * new definition, as FLOW_RSS is newer.
821 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
822 info_size = sizeof(info);
823 if (copy_from_user(&info, useraddr, info_size))
825 /* Since malicious users may modify the original data,
826 * we need to check whether FLOW_RSS is still requested.
828 if (!(info.flow_type & FLOW_RSS))
835 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
836 if (info.rule_cnt > 0) {
837 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
838 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
845 ret = ops->get_rxnfc(dev, &info, rule_buf);
850 if (copy_to_user(useraddr, &info, info_size))
854 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
855 if (copy_to_user(useraddr, rule_buf,
856 info.rule_cnt * sizeof(u32)))
867 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
868 struct ethtool_rxnfc *rx_rings,
873 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
876 /* Validate ring indices */
877 for (i = 0; i < size; i++)
878 if (indir[i] >= rx_rings->data)
884 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
886 void netdev_rss_key_fill(void *buffer, size_t len)
888 BUG_ON(len > sizeof(netdev_rss_key));
889 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
890 memcpy(buffer, netdev_rss_key, len);
892 EXPORT_SYMBOL(netdev_rss_key_fill);
894 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
896 u32 dev_size, current_max = 0;
900 if (!dev->ethtool_ops->get_rxfh_indir_size ||
901 !dev->ethtool_ops->get_rxfh)
903 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
907 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
911 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
916 current_max = max(current_max, indir[dev_size]);
925 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
926 void __user *useraddr)
928 u32 user_size, dev_size;
932 if (!dev->ethtool_ops->get_rxfh_indir_size ||
933 !dev->ethtool_ops->get_rxfh)
935 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
939 if (copy_from_user(&user_size,
940 useraddr + offsetof(struct ethtool_rxfh_indir, size),
944 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
945 &dev_size, sizeof(dev_size)))
948 /* If the user buffer size is 0, this is just a query for the
949 * device table size. Otherwise, if it's smaller than the
950 * device table size it's an error.
952 if (user_size < dev_size)
953 return user_size == 0 ? 0 : -EINVAL;
955 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
959 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
963 if (copy_to_user(useraddr +
964 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
965 indir, dev_size * sizeof(indir[0])))
973 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
974 void __user *useraddr)
976 struct ethtool_rxnfc rx_rings;
977 u32 user_size, dev_size, i;
979 const struct ethtool_ops *ops = dev->ethtool_ops;
981 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
983 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
987 dev_size = ops->get_rxfh_indir_size(dev);
991 if (copy_from_user(&user_size,
992 useraddr + offsetof(struct ethtool_rxfh_indir, size),
996 if (user_size != 0 && user_size != dev_size)
999 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1003 rx_rings.cmd = ETHTOOL_GRXRINGS;
1004 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1008 if (user_size == 0) {
1009 for (i = 0; i < dev_size; i++)
1010 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1012 ret = ethtool_copy_validate_indir(indir,
1013 useraddr + ringidx_offset,
1020 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1024 /* indicate whether rxfh was set to default */
1026 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1028 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1035 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1036 void __user *useraddr)
1039 const struct ethtool_ops *ops = dev->ethtool_ops;
1040 u32 user_indir_size, user_key_size;
1041 u32 dev_indir_size = 0, dev_key_size = 0;
1042 struct ethtool_rxfh rxfh;
1053 if (ops->get_rxfh_indir_size)
1054 dev_indir_size = ops->get_rxfh_indir_size(dev);
1055 if (ops->get_rxfh_key_size)
1056 dev_key_size = ops->get_rxfh_key_size(dev);
1058 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1060 user_indir_size = rxfh.indir_size;
1061 user_key_size = rxfh.key_size;
1063 /* Check that reserved fields are 0 for now */
1064 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1066 /* Most drivers don't handle rss_context, check it's 0 as well */
1067 if (rxfh.rss_context && !ops->get_rxfh_context)
1070 rxfh.indir_size = dev_indir_size;
1071 rxfh.key_size = dev_key_size;
1072 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1075 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1076 (user_key_size && (user_key_size != dev_key_size)))
1079 indir_bytes = user_indir_size * sizeof(indir[0]);
1080 total_size = indir_bytes + user_key_size;
1081 rss_config = kzalloc(total_size, GFP_USER);
1085 if (user_indir_size)
1086 indir = (u32 *)rss_config;
1089 hkey = rss_config + indir_bytes;
1091 if (rxfh.rss_context)
1092 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1096 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1100 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1101 &dev_hfunc, sizeof(rxfh.hfunc))) {
1103 } else if (copy_to_user(useraddr +
1104 offsetof(struct ethtool_rxfh, rss_config[0]),
1105 rss_config, total_size)) {
1114 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1115 void __user *useraddr)
1118 const struct ethtool_ops *ops = dev->ethtool_ops;
1119 struct ethtool_rxnfc rx_rings;
1120 struct ethtool_rxfh rxfh;
1121 u32 dev_indir_size = 0, dev_key_size = 0, i;
1122 u32 *indir = NULL, indir_bytes = 0;
1125 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1126 bool delete = false;
1128 if (!ops->get_rxnfc || !ops->set_rxfh)
1131 if (ops->get_rxfh_indir_size)
1132 dev_indir_size = ops->get_rxfh_indir_size(dev);
1133 if (ops->get_rxfh_key_size)
1134 dev_key_size = ops->get_rxfh_key_size(dev);
1136 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1139 /* Check that reserved fields are 0 for now */
1140 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1142 /* Most drivers don't handle rss_context, check it's 0 as well */
1143 if (rxfh.rss_context && !ops->set_rxfh_context)
1146 /* If either indir, hash key or function is valid, proceed further.
1147 * Must request at least one change: indir size, hash key or function.
1149 if ((rxfh.indir_size &&
1150 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1151 rxfh.indir_size != dev_indir_size) ||
1152 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1153 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1154 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1157 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1158 indir_bytes = dev_indir_size * sizeof(indir[0]);
1160 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1164 rx_rings.cmd = ETHTOOL_GRXRINGS;
1165 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1169 /* rxfh.indir_size == 0 means reset the indir table to default (master
1170 * context) or delete the context (other RSS contexts).
1171 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1173 if (rxfh.indir_size &&
1174 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1175 indir = (u32 *)rss_config;
1176 ret = ethtool_copy_validate_indir(indir,
1177 useraddr + rss_cfg_offset,
1182 } else if (rxfh.indir_size == 0) {
1183 if (rxfh.rss_context == 0) {
1184 indir = (u32 *)rss_config;
1185 for (i = 0; i < dev_indir_size; i++)
1186 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1192 if (rxfh.key_size) {
1193 hkey = rss_config + indir_bytes;
1194 if (copy_from_user(hkey,
1195 useraddr + rss_cfg_offset + indir_bytes,
1202 if (rxfh.rss_context)
1203 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1204 &rxfh.rss_context, delete);
1206 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1210 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1211 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1214 if (!rxfh.rss_context) {
1215 /* indicate whether rxfh was set to default */
1216 if (rxfh.indir_size == 0)
1217 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1218 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1219 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1227 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1229 struct ethtool_regs regs;
1230 const struct ethtool_ops *ops = dev->ethtool_ops;
1234 if (!ops->get_regs || !ops->get_regs_len)
1237 if (copy_from_user(®s, useraddr, sizeof(regs)))
1240 reglen = ops->get_regs_len(dev);
1244 if (regs.len > reglen)
1247 regbuf = vzalloc(reglen);
1251 if (regs.len < reglen)
1254 ops->get_regs(dev, ®s, regbuf);
1257 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1259 useraddr += offsetof(struct ethtool_regs, data);
1260 if (copy_to_user(useraddr, regbuf, reglen))
1269 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1271 struct ethtool_value reset;
1274 if (!dev->ethtool_ops->reset)
1277 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1280 ret = dev->ethtool_ops->reset(dev, &reset.data);
1284 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1289 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1291 struct ethtool_wolinfo wol;
1293 if (!dev->ethtool_ops->get_wol)
1296 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1297 wol.cmd = ETHTOOL_GWOL;
1298 dev->ethtool_ops->get_wol(dev, &wol);
1300 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1305 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1307 struct ethtool_wolinfo wol;
1310 if (!dev->ethtool_ops->set_wol)
1313 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1316 ret = dev->ethtool_ops->set_wol(dev, &wol);
1320 dev->wol_enabled = !!wol.wolopts;
1321 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1326 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1328 struct ethtool_eee edata;
1331 if (!dev->ethtool_ops->get_eee)
1334 memset(&edata, 0, sizeof(struct ethtool_eee));
1335 edata.cmd = ETHTOOL_GEEE;
1336 rc = dev->ethtool_ops->get_eee(dev, &edata);
1341 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1347 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1349 struct ethtool_eee edata;
1351 if (!dev->ethtool_ops->set_eee)
1354 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1357 return dev->ethtool_ops->set_eee(dev, &edata);
1360 static int ethtool_nway_reset(struct net_device *dev)
1362 if (!dev->ethtool_ops->nway_reset)
1365 return dev->ethtool_ops->nway_reset(dev);
1368 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1370 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1371 int link = __ethtool_get_link(dev);
1377 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1382 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1383 int (*getter)(struct net_device *,
1384 struct ethtool_eeprom *, u8 *),
1387 struct ethtool_eeprom eeprom;
1388 void __user *userbuf = useraddr + sizeof(eeprom);
1389 u32 bytes_remaining;
1393 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1396 /* Check for wrap and zero */
1397 if (eeprom.offset + eeprom.len <= eeprom.offset)
1400 /* Check for exceeding total eeprom len */
1401 if (eeprom.offset + eeprom.len > total_len)
1404 data = kmalloc(PAGE_SIZE, GFP_USER);
1408 bytes_remaining = eeprom.len;
1409 while (bytes_remaining > 0) {
1410 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1412 ret = getter(dev, &eeprom, data);
1415 if (copy_to_user(userbuf, data, eeprom.len)) {
1419 userbuf += eeprom.len;
1420 eeprom.offset += eeprom.len;
1421 bytes_remaining -= eeprom.len;
1424 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1425 eeprom.offset -= eeprom.len;
1426 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1433 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1435 const struct ethtool_ops *ops = dev->ethtool_ops;
1437 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1438 !ops->get_eeprom_len(dev))
1441 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1442 ops->get_eeprom_len(dev));
1445 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1447 struct ethtool_eeprom eeprom;
1448 const struct ethtool_ops *ops = dev->ethtool_ops;
1449 void __user *userbuf = useraddr + sizeof(eeprom);
1450 u32 bytes_remaining;
1454 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1455 !ops->get_eeprom_len(dev))
1458 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1461 /* Check for wrap and zero */
1462 if (eeprom.offset + eeprom.len <= eeprom.offset)
1465 /* Check for exceeding total eeprom len */
1466 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1469 data = kmalloc(PAGE_SIZE, GFP_USER);
1473 bytes_remaining = eeprom.len;
1474 while (bytes_remaining > 0) {
1475 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1477 if (copy_from_user(data, userbuf, eeprom.len)) {
1481 ret = ops->set_eeprom(dev, &eeprom, data);
1484 userbuf += eeprom.len;
1485 eeprom.offset += eeprom.len;
1486 bytes_remaining -= eeprom.len;
1493 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1494 void __user *useraddr)
1496 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1498 if (!dev->ethtool_ops->get_coalesce)
1501 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1503 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1508 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1509 void __user *useraddr)
1511 struct ethtool_coalesce coalesce;
1513 if (!dev->ethtool_ops->set_coalesce)
1516 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1519 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1522 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1524 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1526 if (!dev->ethtool_ops->get_ringparam)
1529 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1531 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1536 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1538 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1540 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1543 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1546 dev->ethtool_ops->get_ringparam(dev, &max);
1548 /* ensure new ring parameters are within the maximums */
1549 if (ringparam.rx_pending > max.rx_max_pending ||
1550 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1551 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1552 ringparam.tx_pending > max.tx_max_pending)
1555 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1558 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1559 void __user *useraddr)
1561 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1563 if (!dev->ethtool_ops->get_channels)
1566 dev->ethtool_ops->get_channels(dev, &channels);
1568 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1573 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1574 void __user *useraddr)
1576 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1577 u16 from_channel, to_channel;
1578 u32 max_rx_in_use = 0;
1581 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1584 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1587 dev->ethtool_ops->get_channels(dev, &curr);
1589 /* ensure new counts are within the maximums */
1590 if (channels.rx_count > curr.max_rx ||
1591 channels.tx_count > curr.max_tx ||
1592 channels.combined_count > curr.max_combined ||
1593 channels.other_count > curr.max_other)
1596 /* ensure the new Rx count fits within the configured Rx flow
1597 * indirection table settings */
1598 if (netif_is_rxfh_configured(dev) &&
1599 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1600 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1603 /* Disabling channels, query zero-copy AF_XDP sockets */
1604 from_channel = channels.combined_count +
1605 min(channels.rx_count, channels.tx_count);
1606 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1607 for (i = from_channel; i < to_channel; i++)
1608 if (xdp_get_umem_from_qid(dev, i))
1611 return dev->ethtool_ops->set_channels(dev, &channels);
1614 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1616 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1618 if (!dev->ethtool_ops->get_pauseparam)
1621 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1623 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1628 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1630 struct ethtool_pauseparam pauseparam;
1632 if (!dev->ethtool_ops->set_pauseparam)
1635 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1638 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1641 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1643 struct ethtool_test test;
1644 const struct ethtool_ops *ops = dev->ethtool_ops;
1648 if (!ops->self_test || !ops->get_sset_count)
1651 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1654 WARN_ON(test_len == 0);
1656 if (copy_from_user(&test, useraddr, sizeof(test)))
1659 test.len = test_len;
1660 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1664 ops->self_test(dev, &test, data);
1667 if (copy_to_user(useraddr, &test, sizeof(test)))
1669 useraddr += sizeof(test);
1670 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1679 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1681 struct ethtool_gstrings gstrings;
1685 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1688 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1691 if (ret > S32_MAX / ETH_GSTRING_LEN)
1698 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1702 __ethtool_get_strings(dev, gstrings.string_set, data);
1708 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1710 useraddr += sizeof(gstrings);
1712 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1721 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1723 struct ethtool_value id;
1725 const struct ethtool_ops *ops = dev->ethtool_ops;
1728 if (!ops->set_phys_id)
1734 if (copy_from_user(&id, useraddr, sizeof(id)))
1737 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1741 /* Drop the RTNL lock while waiting, but prevent reentry or
1742 * removal of the device.
1749 /* Driver will handle this itself */
1750 schedule_timeout_interruptible(
1751 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1753 /* Driver expects to be called at twice the frequency in rc */
1754 int n = rc * 2, i, interval = HZ / n;
1756 /* Count down seconds */
1758 /* Count down iterations per second */
1762 rc = ops->set_phys_id(dev,
1763 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1767 schedule_timeout_interruptible(interval);
1768 } while (!signal_pending(current) && --i != 0);
1769 } while (!signal_pending(current) &&
1770 (id.data == 0 || --id.data != 0));
1777 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1781 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1783 struct ethtool_stats stats;
1784 const struct ethtool_ops *ops = dev->ethtool_ops;
1788 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1791 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1794 if (n_stats > S32_MAX / sizeof(u64))
1796 WARN_ON_ONCE(!n_stats);
1797 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1800 stats.n_stats = n_stats;
1803 data = vzalloc(array_size(n_stats, sizeof(u64)));
1806 ops->get_ethtool_stats(dev, &stats, data);
1812 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1814 useraddr += sizeof(stats);
1815 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1824 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1826 const struct ethtool_ops *ops = dev->ethtool_ops;
1827 struct phy_device *phydev = dev->phydev;
1828 struct ethtool_stats stats;
1832 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1835 if (dev->phydev && !ops->get_ethtool_phy_stats)
1836 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1838 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1841 if (n_stats > S32_MAX / sizeof(u64))
1843 WARN_ON_ONCE(!n_stats);
1845 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1848 stats.n_stats = n_stats;
1851 data = vzalloc(array_size(n_stats, sizeof(u64)));
1855 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1856 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1860 ops->get_ethtool_phy_stats(dev, &stats, data);
1867 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1869 useraddr += sizeof(stats);
1870 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1879 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1881 struct ethtool_perm_addr epaddr;
1883 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1886 if (epaddr.size < dev->addr_len)
1888 epaddr.size = dev->addr_len;
1890 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1892 useraddr += sizeof(epaddr);
1893 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1898 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1899 u32 cmd, u32 (*actor)(struct net_device *))
1901 struct ethtool_value edata = { .cmd = cmd };
1906 edata.data = actor(dev);
1908 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1913 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1914 void (*actor)(struct net_device *, u32))
1916 struct ethtool_value edata;
1921 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1924 actor(dev, edata.data);
1928 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1929 int (*actor)(struct net_device *, u32))
1931 struct ethtool_value edata;
1936 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1939 return actor(dev, edata.data);
1942 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1943 char __user *useraddr)
1945 struct ethtool_flash efl;
1947 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1949 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1951 if (!dev->ethtool_ops->flash_device)
1952 return devlink_compat_flash_update(dev, efl.data);
1954 return dev->ethtool_ops->flash_device(dev, &efl);
1957 static int ethtool_set_dump(struct net_device *dev,
1958 void __user *useraddr)
1960 struct ethtool_dump dump;
1962 if (!dev->ethtool_ops->set_dump)
1965 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1968 return dev->ethtool_ops->set_dump(dev, &dump);
1971 static int ethtool_get_dump_flag(struct net_device *dev,
1972 void __user *useraddr)
1975 struct ethtool_dump dump;
1976 const struct ethtool_ops *ops = dev->ethtool_ops;
1978 if (!ops->get_dump_flag)
1981 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1984 ret = ops->get_dump_flag(dev, &dump);
1988 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1993 static int ethtool_get_dump_data(struct net_device *dev,
1994 void __user *useraddr)
1998 struct ethtool_dump dump, tmp;
1999 const struct ethtool_ops *ops = dev->ethtool_ops;
2002 if (!ops->get_dump_data || !ops->get_dump_flag)
2005 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2008 memset(&tmp, 0, sizeof(tmp));
2009 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2010 ret = ops->get_dump_flag(dev, &tmp);
2014 len = min(tmp.len, dump.len);
2018 /* Don't ever let the driver think there's more space available
2019 * than it requested with .get_dump_flag().
2023 /* Always allocate enough space to hold the whole thing so that the
2024 * driver does not need to check the length and bother with partial
2027 data = vzalloc(tmp.len);
2030 ret = ops->get_dump_data(dev, &dump, data);
2034 /* There are two sane possibilities:
2035 * 1. The driver's .get_dump_data() does not touch dump.len.
2036 * 2. Or it may set dump.len to how much it really writes, which
2037 * should be tmp.len (or len if it can do a partial dump).
2038 * In any case respond to userspace with the actual length of data
2041 WARN_ON(dump.len != len && dump.len != tmp.len);
2044 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2048 useraddr += offsetof(struct ethtool_dump, data);
2049 if (copy_to_user(useraddr, data, len))
2056 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2059 struct ethtool_ts_info info;
2060 const struct ethtool_ops *ops = dev->ethtool_ops;
2061 struct phy_device *phydev = dev->phydev;
2063 memset(&info, 0, sizeof(info));
2064 info.cmd = ETHTOOL_GET_TS_INFO;
2066 if (phy_has_tsinfo(phydev)) {
2067 err = phy_ts_info(phydev, &info);
2068 } else if (ops->get_ts_info) {
2069 err = ops->get_ts_info(dev, &info);
2071 info.so_timestamping =
2072 SOF_TIMESTAMPING_RX_SOFTWARE |
2073 SOF_TIMESTAMPING_SOFTWARE;
2074 info.phc_index = -1;
2080 if (copy_to_user(useraddr, &info, sizeof(info)))
2086 static int __ethtool_get_module_info(struct net_device *dev,
2087 struct ethtool_modinfo *modinfo)
2089 const struct ethtool_ops *ops = dev->ethtool_ops;
2090 struct phy_device *phydev = dev->phydev;
2093 return sfp_get_module_info(dev->sfp_bus, modinfo);
2095 if (phydev && phydev->drv && phydev->drv->module_info)
2096 return phydev->drv->module_info(phydev, modinfo);
2098 if (ops->get_module_info)
2099 return ops->get_module_info(dev, modinfo);
2104 static int ethtool_get_module_info(struct net_device *dev,
2105 void __user *useraddr)
2108 struct ethtool_modinfo modinfo;
2110 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2113 ret = __ethtool_get_module_info(dev, &modinfo);
2117 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2123 static int __ethtool_get_module_eeprom(struct net_device *dev,
2124 struct ethtool_eeprom *ee, u8 *data)
2126 const struct ethtool_ops *ops = dev->ethtool_ops;
2127 struct phy_device *phydev = dev->phydev;
2130 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2132 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2133 return phydev->drv->module_eeprom(phydev, ee, data);
2135 if (ops->get_module_eeprom)
2136 return ops->get_module_eeprom(dev, ee, data);
2141 static int ethtool_get_module_eeprom(struct net_device *dev,
2142 void __user *useraddr)
2145 struct ethtool_modinfo modinfo;
2147 ret = __ethtool_get_module_info(dev, &modinfo);
2151 return ethtool_get_any_eeprom(dev, useraddr,
2152 __ethtool_get_module_eeprom,
2153 modinfo.eeprom_len);
2156 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2159 case ETHTOOL_RX_COPYBREAK:
2160 case ETHTOOL_TX_COPYBREAK:
2161 if (tuna->len != sizeof(u32) ||
2162 tuna->type_id != ETHTOOL_TUNABLE_U32)
2165 case ETHTOOL_PFC_PREVENTION_TOUT:
2166 if (tuna->len != sizeof(u16) ||
2167 tuna->type_id != ETHTOOL_TUNABLE_U16)
2177 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2180 struct ethtool_tunable tuna;
2181 const struct ethtool_ops *ops = dev->ethtool_ops;
2184 if (!ops->get_tunable)
2186 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2188 ret = ethtool_tunable_valid(&tuna);
2191 data = kmalloc(tuna.len, GFP_USER);
2194 ret = ops->get_tunable(dev, &tuna, data);
2197 useraddr += sizeof(tuna);
2199 if (copy_to_user(useraddr, data, tuna.len))
2208 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2211 struct ethtool_tunable tuna;
2212 const struct ethtool_ops *ops = dev->ethtool_ops;
2215 if (!ops->set_tunable)
2217 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2219 ret = ethtool_tunable_valid(&tuna);
2222 useraddr += sizeof(tuna);
2223 data = memdup_user(useraddr, tuna.len);
2225 return PTR_ERR(data);
2226 ret = ops->set_tunable(dev, &tuna, data);
2232 static noinline_for_stack int
2233 ethtool_get_per_queue_coalesce(struct net_device *dev,
2234 void __user *useraddr,
2235 struct ethtool_per_queue_op *per_queue_opt)
2239 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2241 if (!dev->ethtool_ops->get_per_queue_coalesce)
2244 useraddr += sizeof(*per_queue_opt);
2246 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2249 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2250 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2252 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2255 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2257 useraddr += sizeof(coalesce);
2263 static noinline_for_stack int
2264 ethtool_set_per_queue_coalesce(struct net_device *dev,
2265 void __user *useraddr,
2266 struct ethtool_per_queue_op *per_queue_opt)
2271 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2272 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2274 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2275 (!dev->ethtool_ops->get_per_queue_coalesce))
2278 useraddr += sizeof(*per_queue_opt);
2280 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2281 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2282 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2286 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2287 struct ethtool_coalesce coalesce;
2289 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2295 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2300 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2304 useraddr += sizeof(coalesce);
2310 for_each_set_bit(i, queue_mask, bit) {
2311 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2320 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2321 void __user *useraddr, u32 sub_cmd)
2323 struct ethtool_per_queue_op per_queue_opt;
2325 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2328 if (per_queue_opt.sub_command != sub_cmd)
2331 switch (per_queue_opt.sub_command) {
2332 case ETHTOOL_GCOALESCE:
2333 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2334 case ETHTOOL_SCOALESCE:
2335 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2341 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2344 case ETHTOOL_PHY_DOWNSHIFT:
2345 case ETHTOOL_PHY_FAST_LINK_DOWN:
2346 if (tuna->len != sizeof(u8) ||
2347 tuna->type_id != ETHTOOL_TUNABLE_U8)
2350 case ETHTOOL_PHY_EDPD:
2351 if (tuna->len != sizeof(u16) ||
2352 tuna->type_id != ETHTOOL_TUNABLE_U16)
2362 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2365 struct ethtool_tunable tuna;
2366 struct phy_device *phydev = dev->phydev;
2369 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2372 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2374 ret = ethtool_phy_tunable_valid(&tuna);
2377 data = kmalloc(tuna.len, GFP_USER);
2380 mutex_lock(&phydev->lock);
2381 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2382 mutex_unlock(&phydev->lock);
2385 useraddr += sizeof(tuna);
2387 if (copy_to_user(useraddr, data, tuna.len))
2396 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2399 struct ethtool_tunable tuna;
2400 struct phy_device *phydev = dev->phydev;
2403 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2405 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2407 ret = ethtool_phy_tunable_valid(&tuna);
2410 useraddr += sizeof(tuna);
2411 data = memdup_user(useraddr, tuna.len);
2413 return PTR_ERR(data);
2414 mutex_lock(&phydev->lock);
2415 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2416 mutex_unlock(&phydev->lock);
2422 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2424 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2427 if (!dev->ethtool_ops->get_fecparam)
2430 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2434 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2439 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2441 struct ethtool_fecparam fecparam;
2443 if (!dev->ethtool_ops->set_fecparam)
2446 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2449 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2452 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2454 int dev_ethtool(struct net *net, struct ifreq *ifr)
2456 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2457 void __user *useraddr = ifr->ifr_data;
2458 u32 ethcmd, sub_cmd;
2460 netdev_features_t old_features;
2462 if (!dev || !netif_device_present(dev))
2465 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2468 if (ethcmd == ETHTOOL_PERQUEUE) {
2469 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2474 /* Allow some commands to be done by anyone */
2477 case ETHTOOL_GDRVINFO:
2478 case ETHTOOL_GMSGLVL:
2480 case ETHTOOL_GCOALESCE:
2481 case ETHTOOL_GRINGPARAM:
2482 case ETHTOOL_GPAUSEPARAM:
2483 case ETHTOOL_GRXCSUM:
2484 case ETHTOOL_GTXCSUM:
2486 case ETHTOOL_GSSET_INFO:
2487 case ETHTOOL_GSTRINGS:
2488 case ETHTOOL_GSTATS:
2489 case ETHTOOL_GPHYSTATS:
2491 case ETHTOOL_GPERMADDR:
2495 case ETHTOOL_GFLAGS:
2496 case ETHTOOL_GPFLAGS:
2498 case ETHTOOL_GRXRINGS:
2499 case ETHTOOL_GRXCLSRLCNT:
2500 case ETHTOOL_GRXCLSRULE:
2501 case ETHTOOL_GRXCLSRLALL:
2502 case ETHTOOL_GRXFHINDIR:
2504 case ETHTOOL_GFEATURES:
2505 case ETHTOOL_GCHANNELS:
2506 case ETHTOOL_GET_TS_INFO:
2508 case ETHTOOL_GTUNABLE:
2509 case ETHTOOL_PHY_GTUNABLE:
2510 case ETHTOOL_GLINKSETTINGS:
2511 case ETHTOOL_GFECPARAM:
2514 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2518 if (dev->ethtool_ops->begin) {
2519 rc = dev->ethtool_ops->begin(dev);
2523 old_features = dev->features;
2527 rc = ethtool_get_settings(dev, useraddr);
2530 rc = ethtool_set_settings(dev, useraddr);
2532 case ETHTOOL_GDRVINFO:
2533 rc = ethtool_get_drvinfo(dev, useraddr);
2536 rc = ethtool_get_regs(dev, useraddr);
2539 rc = ethtool_get_wol(dev, useraddr);
2542 rc = ethtool_set_wol(dev, useraddr);
2544 case ETHTOOL_GMSGLVL:
2545 rc = ethtool_get_value(dev, useraddr, ethcmd,
2546 dev->ethtool_ops->get_msglevel);
2548 case ETHTOOL_SMSGLVL:
2549 rc = ethtool_set_value_void(dev, useraddr,
2550 dev->ethtool_ops->set_msglevel);
2552 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2555 rc = ethtool_get_eee(dev, useraddr);
2558 rc = ethtool_set_eee(dev, useraddr);
2560 case ETHTOOL_NWAY_RST:
2561 rc = ethtool_nway_reset(dev);
2564 rc = ethtool_get_link(dev, useraddr);
2566 case ETHTOOL_GEEPROM:
2567 rc = ethtool_get_eeprom(dev, useraddr);
2569 case ETHTOOL_SEEPROM:
2570 rc = ethtool_set_eeprom(dev, useraddr);
2572 case ETHTOOL_GCOALESCE:
2573 rc = ethtool_get_coalesce(dev, useraddr);
2575 case ETHTOOL_SCOALESCE:
2576 rc = ethtool_set_coalesce(dev, useraddr);
2578 case ETHTOOL_GRINGPARAM:
2579 rc = ethtool_get_ringparam(dev, useraddr);
2581 case ETHTOOL_SRINGPARAM:
2582 rc = ethtool_set_ringparam(dev, useraddr);
2584 case ETHTOOL_GPAUSEPARAM:
2585 rc = ethtool_get_pauseparam(dev, useraddr);
2587 case ETHTOOL_SPAUSEPARAM:
2588 rc = ethtool_set_pauseparam(dev, useraddr);
2591 rc = ethtool_self_test(dev, useraddr);
2593 case ETHTOOL_GSTRINGS:
2594 rc = ethtool_get_strings(dev, useraddr);
2596 case ETHTOOL_PHYS_ID:
2597 rc = ethtool_phys_id(dev, useraddr);
2599 case ETHTOOL_GSTATS:
2600 rc = ethtool_get_stats(dev, useraddr);
2602 case ETHTOOL_GPERMADDR:
2603 rc = ethtool_get_perm_addr(dev, useraddr);
2605 case ETHTOOL_GFLAGS:
2606 rc = ethtool_get_value(dev, useraddr, ethcmd,
2607 __ethtool_get_flags);
2609 case ETHTOOL_SFLAGS:
2610 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2612 case ETHTOOL_GPFLAGS:
2613 rc = ethtool_get_value(dev, useraddr, ethcmd,
2614 dev->ethtool_ops->get_priv_flags);
2616 case ETHTOOL_SPFLAGS:
2617 rc = ethtool_set_value(dev, useraddr,
2618 dev->ethtool_ops->set_priv_flags);
2621 case ETHTOOL_GRXRINGS:
2622 case ETHTOOL_GRXCLSRLCNT:
2623 case ETHTOOL_GRXCLSRULE:
2624 case ETHTOOL_GRXCLSRLALL:
2625 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2628 case ETHTOOL_SRXCLSRLDEL:
2629 case ETHTOOL_SRXCLSRLINS:
2630 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2632 case ETHTOOL_FLASHDEV:
2633 rc = ethtool_flash_device(dev, useraddr);
2636 rc = ethtool_reset(dev, useraddr);
2638 case ETHTOOL_GSSET_INFO:
2639 rc = ethtool_get_sset_info(dev, useraddr);
2641 case ETHTOOL_GRXFHINDIR:
2642 rc = ethtool_get_rxfh_indir(dev, useraddr);
2644 case ETHTOOL_SRXFHINDIR:
2645 rc = ethtool_set_rxfh_indir(dev, useraddr);
2648 rc = ethtool_get_rxfh(dev, useraddr);
2651 rc = ethtool_set_rxfh(dev, useraddr);
2653 case ETHTOOL_GFEATURES:
2654 rc = ethtool_get_features(dev, useraddr);
2656 case ETHTOOL_SFEATURES:
2657 rc = ethtool_set_features(dev, useraddr);
2659 case ETHTOOL_GTXCSUM:
2660 case ETHTOOL_GRXCSUM:
2665 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2667 case ETHTOOL_STXCSUM:
2668 case ETHTOOL_SRXCSUM:
2673 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2675 case ETHTOOL_GCHANNELS:
2676 rc = ethtool_get_channels(dev, useraddr);
2678 case ETHTOOL_SCHANNELS:
2679 rc = ethtool_set_channels(dev, useraddr);
2681 case ETHTOOL_SET_DUMP:
2682 rc = ethtool_set_dump(dev, useraddr);
2684 case ETHTOOL_GET_DUMP_FLAG:
2685 rc = ethtool_get_dump_flag(dev, useraddr);
2687 case ETHTOOL_GET_DUMP_DATA:
2688 rc = ethtool_get_dump_data(dev, useraddr);
2690 case ETHTOOL_GET_TS_INFO:
2691 rc = ethtool_get_ts_info(dev, useraddr);
2693 case ETHTOOL_GMODULEINFO:
2694 rc = ethtool_get_module_info(dev, useraddr);
2696 case ETHTOOL_GMODULEEEPROM:
2697 rc = ethtool_get_module_eeprom(dev, useraddr);
2699 case ETHTOOL_GTUNABLE:
2700 rc = ethtool_get_tunable(dev, useraddr);
2702 case ETHTOOL_STUNABLE:
2703 rc = ethtool_set_tunable(dev, useraddr);
2705 case ETHTOOL_GPHYSTATS:
2706 rc = ethtool_get_phy_stats(dev, useraddr);
2708 case ETHTOOL_PERQUEUE:
2709 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2711 case ETHTOOL_GLINKSETTINGS:
2712 rc = ethtool_get_link_ksettings(dev, useraddr);
2714 case ETHTOOL_SLINKSETTINGS:
2715 rc = ethtool_set_link_ksettings(dev, useraddr);
2717 case ETHTOOL_PHY_GTUNABLE:
2718 rc = get_phy_tunable(dev, useraddr);
2720 case ETHTOOL_PHY_STUNABLE:
2721 rc = set_phy_tunable(dev, useraddr);
2723 case ETHTOOL_GFECPARAM:
2724 rc = ethtool_get_fecparam(dev, useraddr);
2726 case ETHTOOL_SFECPARAM:
2727 rc = ethtool_set_fecparam(dev, useraddr);
2733 if (dev->ethtool_ops->complete)
2734 dev->ethtool_ops->complete(dev);
2736 if (old_features != dev->features)
2737 netdev_features_change(dev);
2742 struct ethtool_rx_flow_key {
2743 struct flow_dissector_key_basic basic;
2745 struct flow_dissector_key_ipv4_addrs ipv4;
2746 struct flow_dissector_key_ipv6_addrs ipv6;
2748 struct flow_dissector_key_ports tp;
2749 struct flow_dissector_key_ip ip;
2750 struct flow_dissector_key_vlan vlan;
2751 struct flow_dissector_key_eth_addrs eth_addrs;
2752 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2754 struct ethtool_rx_flow_match {
2755 struct flow_dissector dissector;
2756 struct ethtool_rx_flow_key key;
2757 struct ethtool_rx_flow_key mask;
2760 struct ethtool_rx_flow_rule *
2761 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2763 const struct ethtool_rx_flow_spec *fs = input->fs;
2764 static struct in6_addr zero_addr = {};
2765 struct ethtool_rx_flow_match *match;
2766 struct ethtool_rx_flow_rule *flow;
2767 struct flow_action_entry *act;
2769 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2770 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2772 return ERR_PTR(-ENOMEM);
2774 /* ethtool_rx supports only one single action per rule. */
2775 flow->rule = flow_rule_alloc(1);
2778 return ERR_PTR(-ENOMEM);
2781 match = (struct ethtool_rx_flow_match *)flow->priv;
2782 flow->rule->match.dissector = &match->dissector;
2783 flow->rule->match.mask = &match->mask;
2784 flow->rule->match.key = &match->key;
2786 match->mask.basic.n_proto = htons(0xffff);
2788 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2790 const struct ethhdr *ether_spec, *ether_m_spec;
2792 ether_spec = &fs->h_u.ether_spec;
2793 ether_m_spec = &fs->m_u.ether_spec;
2795 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2796 ether_addr_copy(match->key.eth_addrs.src,
2797 ether_spec->h_source);
2798 ether_addr_copy(match->mask.eth_addrs.src,
2799 ether_m_spec->h_source);
2801 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2802 ether_addr_copy(match->key.eth_addrs.dst,
2803 ether_spec->h_dest);
2804 ether_addr_copy(match->mask.eth_addrs.dst,
2805 ether_m_spec->h_dest);
2807 if (ether_m_spec->h_proto) {
2808 match->key.basic.n_proto = ether_spec->h_proto;
2809 match->mask.basic.n_proto = ether_m_spec->h_proto;
2815 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2817 match->key.basic.n_proto = htons(ETH_P_IP);
2819 v4_spec = &fs->h_u.tcp_ip4_spec;
2820 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2822 if (v4_m_spec->ip4src) {
2823 match->key.ipv4.src = v4_spec->ip4src;
2824 match->mask.ipv4.src = v4_m_spec->ip4src;
2826 if (v4_m_spec->ip4dst) {
2827 match->key.ipv4.dst = v4_spec->ip4dst;
2828 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2830 if (v4_m_spec->ip4src ||
2831 v4_m_spec->ip4dst) {
2832 match->dissector.used_keys |=
2833 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2834 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2835 offsetof(struct ethtool_rx_flow_key, ipv4);
2837 if (v4_m_spec->psrc) {
2838 match->key.tp.src = v4_spec->psrc;
2839 match->mask.tp.src = v4_m_spec->psrc;
2841 if (v4_m_spec->pdst) {
2842 match->key.tp.dst = v4_spec->pdst;
2843 match->mask.tp.dst = v4_m_spec->pdst;
2845 if (v4_m_spec->psrc ||
2847 match->dissector.used_keys |=
2848 BIT(FLOW_DISSECTOR_KEY_PORTS);
2849 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2850 offsetof(struct ethtool_rx_flow_key, tp);
2852 if (v4_m_spec->tos) {
2853 match->key.ip.tos = v4_spec->tos;
2854 match->mask.ip.tos = v4_m_spec->tos;
2855 match->dissector.used_keys |=
2856 BIT(FLOW_DISSECTOR_KEY_IP);
2857 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2858 offsetof(struct ethtool_rx_flow_key, ip);
2864 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2866 match->key.basic.n_proto = htons(ETH_P_IPV6);
2868 v6_spec = &fs->h_u.tcp_ip6_spec;
2869 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2870 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2871 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2872 sizeof(match->key.ipv6.src));
2873 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2874 sizeof(match->mask.ipv6.src));
2876 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2877 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2878 sizeof(match->key.ipv6.dst));
2879 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2880 sizeof(match->mask.ipv6.dst));
2882 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2883 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2884 match->dissector.used_keys |=
2885 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2886 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2887 offsetof(struct ethtool_rx_flow_key, ipv6);
2889 if (v6_m_spec->psrc) {
2890 match->key.tp.src = v6_spec->psrc;
2891 match->mask.tp.src = v6_m_spec->psrc;
2893 if (v6_m_spec->pdst) {
2894 match->key.tp.dst = v6_spec->pdst;
2895 match->mask.tp.dst = v6_m_spec->pdst;
2897 if (v6_m_spec->psrc ||
2899 match->dissector.used_keys |=
2900 BIT(FLOW_DISSECTOR_KEY_PORTS);
2901 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2902 offsetof(struct ethtool_rx_flow_key, tp);
2904 if (v6_m_spec->tclass) {
2905 match->key.ip.tos = v6_spec->tclass;
2906 match->mask.ip.tos = v6_m_spec->tclass;
2907 match->dissector.used_keys |=
2908 BIT(FLOW_DISSECTOR_KEY_IP);
2909 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2910 offsetof(struct ethtool_rx_flow_key, ip);
2915 ethtool_rx_flow_rule_destroy(flow);
2916 return ERR_PTR(-EINVAL);
2919 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2922 match->key.basic.ip_proto = IPPROTO_TCP;
2926 match->key.basic.ip_proto = IPPROTO_UDP;
2929 match->mask.basic.ip_proto = 0xff;
2931 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
2932 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
2933 offsetof(struct ethtool_rx_flow_key, basic);
2935 if (fs->flow_type & FLOW_EXT) {
2936 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
2937 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
2939 if (ext_m_spec->vlan_etype) {
2940 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
2941 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
2944 if (ext_m_spec->vlan_tci) {
2945 match->key.vlan.vlan_id =
2946 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
2947 match->mask.vlan.vlan_id =
2948 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
2950 match->key.vlan.vlan_dei =
2951 !!(ext_h_spec->vlan_tci & htons(0x1000));
2952 match->mask.vlan.vlan_dei =
2953 !!(ext_m_spec->vlan_tci & htons(0x1000));
2955 match->key.vlan.vlan_priority =
2956 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
2957 match->mask.vlan.vlan_priority =
2958 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
2961 if (ext_m_spec->vlan_etype ||
2962 ext_m_spec->vlan_tci) {
2963 match->dissector.used_keys |=
2964 BIT(FLOW_DISSECTOR_KEY_VLAN);
2965 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
2966 offsetof(struct ethtool_rx_flow_key, vlan);
2969 if (fs->flow_type & FLOW_MAC_EXT) {
2970 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
2971 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
2973 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
2975 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
2978 match->dissector.used_keys |=
2979 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
2980 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
2981 offsetof(struct ethtool_rx_flow_key, eth_addrs);
2984 act = &flow->rule->action.entries[0];
2985 switch (fs->ring_cookie) {
2986 case RX_CLS_FLOW_DISC:
2987 act->id = FLOW_ACTION_DROP;
2989 case RX_CLS_FLOW_WAKE:
2990 act->id = FLOW_ACTION_WAKE;
2993 act->id = FLOW_ACTION_QUEUE;
2994 if (fs->flow_type & FLOW_RSS)
2995 act->queue.ctx = input->rss_ctx;
2997 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
2998 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3004 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3006 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3011 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);