1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/ethtool.c - Ethtool ioctl handler
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
6 * This file is where we call all the ethtool_ops commands to get
7 * the information ethtool needs.
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/capability.h>
13 #include <linux/errno.h>
14 #include <linux/ethtool.h>
15 #include <linux/netdevice.h>
16 #include <linux/net_tstamp.h>
17 #include <linux/phy.h>
18 #include <linux/bitops.h>
19 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sfp.h>
22 #include <linux/slab.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/sched/signal.h>
25 #include <linux/net.h>
26 #include <net/devlink.h>
27 #include <net/xdp_sock.h>
28 #include <net/flow_offload.h>
29 #include <linux/ethtool_netlink.h>
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 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
60 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
62 struct ethtool_gfeatures cmd = {
63 .cmd = ETHTOOL_GFEATURES,
64 .size = ETHTOOL_DEV_FEATURE_WORDS,
66 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
71 /* in case feature bits run out again */
72 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
74 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
75 features[i].available = (u32)(dev->hw_features >> (32 * i));
76 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
77 features[i].active = (u32)(dev->features >> (32 * i));
78 features[i].never_changed =
79 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
82 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
83 if (get_user(copy_size, sizeaddr))
86 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
87 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
89 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
91 useraddr += sizeof(cmd);
92 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
98 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
100 struct ethtool_sfeatures cmd;
101 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
102 netdev_features_t wanted = 0, valid = 0;
105 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
107 useraddr += sizeof(cmd);
109 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
112 if (copy_from_user(features, useraddr, sizeof(features)))
115 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
116 valid |= (netdev_features_t)features[i].valid << (32 * i);
117 wanted |= (netdev_features_t)features[i].requested << (32 * i);
120 if (valid & ~NETIF_F_ETHTOOL_BITS)
123 if (valid & ~dev->hw_features) {
124 valid &= dev->hw_features;
125 ret |= ETHTOOL_F_UNSUPPORTED;
128 dev->wanted_features &= ~valid;
129 dev->wanted_features |= wanted & valid;
130 __netdev_update_features(dev);
132 if ((dev->wanted_features ^ dev->features) & valid)
133 ret |= ETHTOOL_F_WISH;
138 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
140 const struct ethtool_ops *ops = dev->ethtool_ops;
142 if (sset == ETH_SS_FEATURES)
143 return ARRAY_SIZE(netdev_features_strings);
145 if (sset == ETH_SS_RSS_HASH_FUNCS)
146 return ARRAY_SIZE(rss_hash_func_strings);
148 if (sset == ETH_SS_TUNABLES)
149 return ARRAY_SIZE(tunable_strings);
151 if (sset == ETH_SS_PHY_TUNABLES)
152 return ARRAY_SIZE(phy_tunable_strings);
154 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
155 !ops->get_ethtool_phy_stats)
156 return phy_ethtool_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_ops *ops = dev->ethtool_ops;
172 if (stringset == ETH_SS_FEATURES)
173 memcpy(data, netdev_features_strings,
174 sizeof(netdev_features_strings));
175 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
176 memcpy(data, rss_hash_func_strings,
177 sizeof(rss_hash_func_strings));
178 else if (stringset == ETH_SS_TUNABLES)
179 memcpy(data, tunable_strings, sizeof(tunable_strings));
180 else if (stringset == ETH_SS_PHY_TUNABLES)
181 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
182 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
183 !ops->get_ethtool_phy_stats)
184 phy_ethtool_get_strings(dev->phydev, data);
185 else if (stringset == ETH_SS_LINK_MODES)
186 memcpy(data, link_mode_names,
187 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
189 /* ops->get_strings is valid because checked earlier */
190 ops->get_strings(dev, stringset, data);
193 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
195 /* feature masks of legacy discrete ethtool ops */
198 case ETHTOOL_GTXCSUM:
199 case ETHTOOL_STXCSUM:
200 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
201 case ETHTOOL_GRXCSUM:
202 case ETHTOOL_SRXCSUM:
203 return NETIF_F_RXCSUM;
209 return NETIF_F_ALL_TSO;
221 static int ethtool_get_one_feature(struct net_device *dev,
222 char __user *useraddr, u32 ethcmd)
224 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
225 struct ethtool_value edata = {
227 .data = !!(dev->features & mask),
230 if (copy_to_user(useraddr, &edata, sizeof(edata)))
235 static int ethtool_set_one_feature(struct net_device *dev,
236 void __user *useraddr, u32 ethcmd)
238 struct ethtool_value edata;
239 netdev_features_t mask;
241 if (copy_from_user(&edata, useraddr, sizeof(edata)))
244 mask = ethtool_get_feature_mask(ethcmd);
245 mask &= dev->hw_features;
250 dev->wanted_features |= mask;
252 dev->wanted_features &= ~mask;
254 __netdev_update_features(dev);
259 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
260 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
261 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
262 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
265 static u32 __ethtool_get_flags(struct net_device *dev)
269 if (dev->features & NETIF_F_LRO)
270 flags |= ETH_FLAG_LRO;
271 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
272 flags |= ETH_FLAG_RXVLAN;
273 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
274 flags |= ETH_FLAG_TXVLAN;
275 if (dev->features & NETIF_F_NTUPLE)
276 flags |= ETH_FLAG_NTUPLE;
277 if (dev->features & NETIF_F_RXHASH)
278 flags |= ETH_FLAG_RXHASH;
283 static int __ethtool_set_flags(struct net_device *dev, u32 data)
285 netdev_features_t features = 0, changed;
287 if (data & ~ETH_ALL_FLAGS)
290 if (data & ETH_FLAG_LRO)
291 features |= NETIF_F_LRO;
292 if (data & ETH_FLAG_RXVLAN)
293 features |= NETIF_F_HW_VLAN_CTAG_RX;
294 if (data & ETH_FLAG_TXVLAN)
295 features |= NETIF_F_HW_VLAN_CTAG_TX;
296 if (data & ETH_FLAG_NTUPLE)
297 features |= NETIF_F_NTUPLE;
298 if (data & ETH_FLAG_RXHASH)
299 features |= NETIF_F_RXHASH;
301 /* allow changing only bits set in hw_features */
302 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
303 if (changed & ~dev->hw_features)
304 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
306 dev->wanted_features =
307 (dev->wanted_features & ~changed) | (features & changed);
309 __netdev_update_features(dev);
314 /* Given two link masks, AND them together and save the result in dst. */
315 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
316 struct ethtool_link_ksettings *src)
318 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
319 unsigned int idx = 0;
321 for (; idx < size; idx++) {
322 dst->link_modes.supported[idx] &=
323 src->link_modes.supported[idx];
324 dst->link_modes.advertising[idx] &=
325 src->link_modes.advertising[idx];
328 EXPORT_SYMBOL(ethtool_intersect_link_masks);
330 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
333 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
336 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
338 /* return false if src had higher bits set. lower bits always updated. */
339 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
340 const unsigned long *src)
344 /* TODO: following test will soon always be true */
345 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
346 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
348 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
349 bitmap_fill(ext, 32);
350 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
351 if (bitmap_intersects(ext, src,
352 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
353 /* src mask goes beyond bit 31 */
357 *legacy_u32 = src[0];
360 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
362 /* return false if ksettings link modes had higher bits
363 * set. legacy_settings always updated (best effort)
366 convert_link_ksettings_to_legacy_settings(
367 struct ethtool_cmd *legacy_settings,
368 const struct ethtool_link_ksettings *link_ksettings)
372 memset(legacy_settings, 0, sizeof(*legacy_settings));
373 /* this also clears the deprecated fields in legacy structure:
379 retval &= ethtool_convert_link_mode_to_legacy_u32(
380 &legacy_settings->supported,
381 link_ksettings->link_modes.supported);
382 retval &= ethtool_convert_link_mode_to_legacy_u32(
383 &legacy_settings->advertising,
384 link_ksettings->link_modes.advertising);
385 retval &= ethtool_convert_link_mode_to_legacy_u32(
386 &legacy_settings->lp_advertising,
387 link_ksettings->link_modes.lp_advertising);
388 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
389 legacy_settings->duplex
390 = link_ksettings->base.duplex;
391 legacy_settings->port
392 = link_ksettings->base.port;
393 legacy_settings->phy_address
394 = link_ksettings->base.phy_address;
395 legacy_settings->autoneg
396 = link_ksettings->base.autoneg;
397 legacy_settings->mdio_support
398 = link_ksettings->base.mdio_support;
399 legacy_settings->eth_tp_mdix
400 = link_ksettings->base.eth_tp_mdix;
401 legacy_settings->eth_tp_mdix_ctrl
402 = link_ksettings->base.eth_tp_mdix_ctrl;
403 legacy_settings->transceiver
404 = link_ksettings->base.transceiver;
408 /* number of 32-bit words to store the user's link mode bitmaps */
409 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
410 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
412 /* layout of the struct passed from/to userland */
413 struct ethtool_link_usettings {
414 struct ethtool_link_settings base;
416 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
417 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
418 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
422 /* Internal kernel helper to query a device ethtool_link_settings. */
423 int __ethtool_get_link_ksettings(struct net_device *dev,
424 struct ethtool_link_ksettings *link_ksettings)
428 if (!dev->ethtool_ops->get_link_ksettings)
431 memset(link_ksettings, 0, sizeof(*link_ksettings));
432 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
434 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
436 /* convert ethtool_link_usettings in user space to a kernel internal
437 * ethtool_link_ksettings. return 0 on success, errno on error.
439 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
440 const void __user *from)
442 struct ethtool_link_usettings link_usettings;
444 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
447 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
448 bitmap_from_arr32(to->link_modes.supported,
449 link_usettings.link_modes.supported,
450 __ETHTOOL_LINK_MODE_MASK_NBITS);
451 bitmap_from_arr32(to->link_modes.advertising,
452 link_usettings.link_modes.advertising,
453 __ETHTOOL_LINK_MODE_MASK_NBITS);
454 bitmap_from_arr32(to->link_modes.lp_advertising,
455 link_usettings.link_modes.lp_advertising,
456 __ETHTOOL_LINK_MODE_MASK_NBITS);
461 /* convert a kernel internal ethtool_link_ksettings to
462 * ethtool_link_usettings in user space. return 0 on success, errno on
466 store_link_ksettings_for_user(void __user *to,
467 const struct ethtool_link_ksettings *from)
469 struct ethtool_link_usettings link_usettings;
471 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
472 bitmap_to_arr32(link_usettings.link_modes.supported,
473 from->link_modes.supported,
474 __ETHTOOL_LINK_MODE_MASK_NBITS);
475 bitmap_to_arr32(link_usettings.link_modes.advertising,
476 from->link_modes.advertising,
477 __ETHTOOL_LINK_MODE_MASK_NBITS);
478 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
479 from->link_modes.lp_advertising,
480 __ETHTOOL_LINK_MODE_MASK_NBITS);
482 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
488 /* Query device for its ethtool_link_settings. */
489 static int ethtool_get_link_ksettings(struct net_device *dev,
490 void __user *useraddr)
493 struct ethtool_link_ksettings link_ksettings;
496 if (!dev->ethtool_ops->get_link_ksettings)
499 /* handle bitmap nbits handshake */
500 if (copy_from_user(&link_ksettings.base, useraddr,
501 sizeof(link_ksettings.base)))
504 if (__ETHTOOL_LINK_MODE_MASK_NU32
505 != link_ksettings.base.link_mode_masks_nwords) {
506 /* wrong link mode nbits requested */
507 memset(&link_ksettings, 0, sizeof(link_ksettings));
508 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
509 /* send back number of words required as negative val */
510 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
511 "need too many bits for link modes!");
512 link_ksettings.base.link_mode_masks_nwords
513 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
515 /* copy the base fields back to user, not the link
518 if (copy_to_user(useraddr, &link_ksettings.base,
519 sizeof(link_ksettings.base)))
525 /* handshake successful: user/kernel agree on
526 * link_mode_masks_nwords
529 memset(&link_ksettings, 0, sizeof(link_ksettings));
530 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
534 /* make sure we tell the right values to user */
535 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
536 link_ksettings.base.link_mode_masks_nwords
537 = __ETHTOOL_LINK_MODE_MASK_NU32;
539 return store_link_ksettings_for_user(useraddr, &link_ksettings);
542 /* Update device ethtool_link_settings. */
543 static int ethtool_set_link_ksettings(struct net_device *dev,
544 void __user *useraddr)
547 struct ethtool_link_ksettings link_ksettings;
551 if (!dev->ethtool_ops->set_link_ksettings)
554 /* make sure nbits field has expected value */
555 if (copy_from_user(&link_ksettings.base, useraddr,
556 sizeof(link_ksettings.base)))
559 if (__ETHTOOL_LINK_MODE_MASK_NU32
560 != link_ksettings.base.link_mode_masks_nwords)
563 /* copy the whole structure, now that we know it has expected
566 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
570 /* re-check nwords field, just in case */
571 if (__ETHTOOL_LINK_MODE_MASK_NU32
572 != link_ksettings.base.link_mode_masks_nwords)
575 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
577 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
578 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
583 /* Query device for its ethtool_cmd settings.
585 * Backward compatibility note: for compatibility with legacy ethtool, this is
586 * now implemented via get_link_ksettings. When driver reports higher link mode
587 * bits, a kernel warning is logged once (with name of 1st driver/device) to
588 * recommend user to upgrade ethtool, but the command is successful (only the
589 * lower link mode bits reported back to user). Deprecated fields from
590 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
592 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
594 struct ethtool_link_ksettings link_ksettings;
595 struct ethtool_cmd cmd;
599 if (!dev->ethtool_ops->get_link_ksettings)
602 memset(&link_ksettings, 0, sizeof(link_ksettings));
603 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
606 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
608 /* send a sensible cmd tag back to user */
609 cmd.cmd = ETHTOOL_GSET;
611 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
617 /* Update device link settings with given ethtool_cmd.
619 * Backward compatibility note: for compatibility with legacy ethtool, this is
620 * now always implemented via set_link_settings. When user's request updates
621 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
622 * warning is logged once (with name of 1st driver/device) to recommend user to
623 * upgrade ethtool, and the request is rejected.
625 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
627 struct ethtool_link_ksettings link_ksettings;
628 struct ethtool_cmd cmd;
633 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
635 if (!dev->ethtool_ops->set_link_ksettings)
638 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
640 link_ksettings.base.link_mode_masks_nwords =
641 __ETHTOOL_LINK_MODE_MASK_NU32;
642 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
644 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
645 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
650 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
651 void __user *useraddr)
653 struct ethtool_drvinfo info;
654 const struct ethtool_ops *ops = dev->ethtool_ops;
656 memset(&info, 0, sizeof(info));
657 info.cmd = ETHTOOL_GDRVINFO;
658 if (ops->get_drvinfo) {
659 ops->get_drvinfo(dev, &info);
660 } else if (dev->dev.parent && dev->dev.parent->driver) {
661 strlcpy(info.bus_info, dev_name(dev->dev.parent),
662 sizeof(info.bus_info));
663 strlcpy(info.driver, dev->dev.parent->driver->name,
664 sizeof(info.driver));
670 * this method of obtaining string set info is deprecated;
671 * Use ETHTOOL_GSSET_INFO instead.
673 if (ops->get_sset_count) {
676 rc = ops->get_sset_count(dev, ETH_SS_TEST);
678 info.testinfo_len = rc;
679 rc = ops->get_sset_count(dev, ETH_SS_STATS);
682 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
684 info.n_priv_flags = rc;
686 if (ops->get_regs_len) {
687 int ret = ops->get_regs_len(dev);
690 info.regdump_len = ret;
693 if (ops->get_eeprom_len)
694 info.eedump_len = ops->get_eeprom_len(dev);
696 if (!info.fw_version[0])
697 devlink_compat_running_version(dev, info.fw_version,
698 sizeof(info.fw_version));
700 if (copy_to_user(useraddr, &info, sizeof(info)))
705 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
706 void __user *useraddr)
708 struct ethtool_sset_info info;
710 int i, idx = 0, n_bits = 0, ret, rc;
711 u32 *info_buf = NULL;
713 if (copy_from_user(&info, useraddr, sizeof(info)))
716 /* store copy of mask, because we zero struct later on */
717 sset_mask = info.sset_mask;
721 /* calculate size of return buffer */
722 n_bits = hweight64(sset_mask);
724 memset(&info, 0, sizeof(info));
725 info.cmd = ETHTOOL_GSSET_INFO;
727 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
732 * fill return buffer based on input bitmask and successful
733 * get_sset_count return
735 for (i = 0; i < 64; i++) {
736 if (!(sset_mask & (1ULL << i)))
739 rc = __ethtool_get_sset_count(dev, i);
741 info.sset_mask |= (1ULL << i);
742 info_buf[idx++] = rc;
747 if (copy_to_user(useraddr, &info, sizeof(info)))
750 useraddr += offsetof(struct ethtool_sset_info, data);
751 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
761 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
762 u32 cmd, void __user *useraddr)
764 struct ethtool_rxnfc info;
765 size_t info_size = sizeof(info);
768 if (!dev->ethtool_ops->set_rxnfc)
771 /* struct ethtool_rxnfc was originally defined for
772 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
773 * members. User-space might still be using that
775 if (cmd == ETHTOOL_SRXFH)
776 info_size = (offsetof(struct ethtool_rxnfc, data) +
779 if (copy_from_user(&info, useraddr, info_size))
782 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
786 if (cmd == ETHTOOL_SRXCLSRLINS &&
787 copy_to_user(useraddr, &info, info_size))
793 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
794 u32 cmd, void __user *useraddr)
796 struct ethtool_rxnfc info;
797 size_t info_size = sizeof(info);
798 const struct ethtool_ops *ops = dev->ethtool_ops;
800 void *rule_buf = NULL;
805 /* struct ethtool_rxnfc was originally defined for
806 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
807 * members. User-space might still be using that
809 if (cmd == ETHTOOL_GRXFH)
810 info_size = (offsetof(struct ethtool_rxnfc, data) +
813 if (copy_from_user(&info, useraddr, info_size))
816 /* If FLOW_RSS was requested then user-space must be using the
817 * new definition, as FLOW_RSS is newer.
819 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
820 info_size = sizeof(info);
821 if (copy_from_user(&info, useraddr, info_size))
823 /* Since malicious users may modify the original data,
824 * we need to check whether FLOW_RSS is still requested.
826 if (!(info.flow_type & FLOW_RSS))
833 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
834 if (info.rule_cnt > 0) {
835 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
836 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
843 ret = ops->get_rxnfc(dev, &info, rule_buf);
848 if (copy_to_user(useraddr, &info, info_size))
852 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
853 if (copy_to_user(useraddr, rule_buf,
854 info.rule_cnt * sizeof(u32)))
865 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
866 struct ethtool_rxnfc *rx_rings,
871 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
874 /* Validate ring indices */
875 for (i = 0; i < size; i++)
876 if (indir[i] >= rx_rings->data)
882 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
884 void netdev_rss_key_fill(void *buffer, size_t len)
886 BUG_ON(len > sizeof(netdev_rss_key));
887 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
888 memcpy(buffer, netdev_rss_key, len);
890 EXPORT_SYMBOL(netdev_rss_key_fill);
892 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
894 u32 dev_size, current_max = 0;
898 if (!dev->ethtool_ops->get_rxfh_indir_size ||
899 !dev->ethtool_ops->get_rxfh)
901 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
905 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
909 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
914 current_max = max(current_max, indir[dev_size]);
923 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
924 void __user *useraddr)
926 u32 user_size, dev_size;
930 if (!dev->ethtool_ops->get_rxfh_indir_size ||
931 !dev->ethtool_ops->get_rxfh)
933 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
937 if (copy_from_user(&user_size,
938 useraddr + offsetof(struct ethtool_rxfh_indir, size),
942 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
943 &dev_size, sizeof(dev_size)))
946 /* If the user buffer size is 0, this is just a query for the
947 * device table size. Otherwise, if it's smaller than the
948 * device table size it's an error.
950 if (user_size < dev_size)
951 return user_size == 0 ? 0 : -EINVAL;
953 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
957 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
961 if (copy_to_user(useraddr +
962 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
963 indir, dev_size * sizeof(indir[0])))
971 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
972 void __user *useraddr)
974 struct ethtool_rxnfc rx_rings;
975 u32 user_size, dev_size, i;
977 const struct ethtool_ops *ops = dev->ethtool_ops;
979 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
981 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
985 dev_size = ops->get_rxfh_indir_size(dev);
989 if (copy_from_user(&user_size,
990 useraddr + offsetof(struct ethtool_rxfh_indir, size),
994 if (user_size != 0 && user_size != dev_size)
997 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1001 rx_rings.cmd = ETHTOOL_GRXRINGS;
1002 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1006 if (user_size == 0) {
1007 for (i = 0; i < dev_size; i++)
1008 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1010 ret = ethtool_copy_validate_indir(indir,
1011 useraddr + ringidx_offset,
1018 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1022 /* indicate whether rxfh was set to default */
1024 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1026 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1033 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1034 void __user *useraddr)
1037 const struct ethtool_ops *ops = dev->ethtool_ops;
1038 u32 user_indir_size, user_key_size;
1039 u32 dev_indir_size = 0, dev_key_size = 0;
1040 struct ethtool_rxfh rxfh;
1051 if (ops->get_rxfh_indir_size)
1052 dev_indir_size = ops->get_rxfh_indir_size(dev);
1053 if (ops->get_rxfh_key_size)
1054 dev_key_size = ops->get_rxfh_key_size(dev);
1056 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1058 user_indir_size = rxfh.indir_size;
1059 user_key_size = rxfh.key_size;
1061 /* Check that reserved fields are 0 for now */
1062 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1064 /* Most drivers don't handle rss_context, check it's 0 as well */
1065 if (rxfh.rss_context && !ops->get_rxfh_context)
1068 rxfh.indir_size = dev_indir_size;
1069 rxfh.key_size = dev_key_size;
1070 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1073 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1074 (user_key_size && (user_key_size != dev_key_size)))
1077 indir_bytes = user_indir_size * sizeof(indir[0]);
1078 total_size = indir_bytes + user_key_size;
1079 rss_config = kzalloc(total_size, GFP_USER);
1083 if (user_indir_size)
1084 indir = (u32 *)rss_config;
1087 hkey = rss_config + indir_bytes;
1089 if (rxfh.rss_context)
1090 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1094 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1098 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1099 &dev_hfunc, sizeof(rxfh.hfunc))) {
1101 } else if (copy_to_user(useraddr +
1102 offsetof(struct ethtool_rxfh, rss_config[0]),
1103 rss_config, total_size)) {
1112 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1113 void __user *useraddr)
1116 const struct ethtool_ops *ops = dev->ethtool_ops;
1117 struct ethtool_rxnfc rx_rings;
1118 struct ethtool_rxfh rxfh;
1119 u32 dev_indir_size = 0, dev_key_size = 0, i;
1120 u32 *indir = NULL, indir_bytes = 0;
1123 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1124 bool delete = false;
1126 if (!ops->get_rxnfc || !ops->set_rxfh)
1129 if (ops->get_rxfh_indir_size)
1130 dev_indir_size = ops->get_rxfh_indir_size(dev);
1131 if (ops->get_rxfh_key_size)
1132 dev_key_size = ops->get_rxfh_key_size(dev);
1134 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1137 /* Check that reserved fields are 0 for now */
1138 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1140 /* Most drivers don't handle rss_context, check it's 0 as well */
1141 if (rxfh.rss_context && !ops->set_rxfh_context)
1144 /* If either indir, hash key or function is valid, proceed further.
1145 * Must request at least one change: indir size, hash key or function.
1147 if ((rxfh.indir_size &&
1148 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1149 rxfh.indir_size != dev_indir_size) ||
1150 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1151 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1152 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1155 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1156 indir_bytes = dev_indir_size * sizeof(indir[0]);
1158 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1162 rx_rings.cmd = ETHTOOL_GRXRINGS;
1163 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1167 /* rxfh.indir_size == 0 means reset the indir table to default (master
1168 * context) or delete the context (other RSS contexts).
1169 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1171 if (rxfh.indir_size &&
1172 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1173 indir = (u32 *)rss_config;
1174 ret = ethtool_copy_validate_indir(indir,
1175 useraddr + rss_cfg_offset,
1180 } else if (rxfh.indir_size == 0) {
1181 if (rxfh.rss_context == 0) {
1182 indir = (u32 *)rss_config;
1183 for (i = 0; i < dev_indir_size; i++)
1184 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1190 if (rxfh.key_size) {
1191 hkey = rss_config + indir_bytes;
1192 if (copy_from_user(hkey,
1193 useraddr + rss_cfg_offset + indir_bytes,
1200 if (rxfh.rss_context)
1201 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1202 &rxfh.rss_context, delete);
1204 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1208 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1209 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1212 if (!rxfh.rss_context) {
1213 /* indicate whether rxfh was set to default */
1214 if (rxfh.indir_size == 0)
1215 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1216 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1217 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1225 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1227 struct ethtool_regs regs;
1228 const struct ethtool_ops *ops = dev->ethtool_ops;
1232 if (!ops->get_regs || !ops->get_regs_len)
1235 if (copy_from_user(®s, useraddr, sizeof(regs)))
1238 reglen = ops->get_regs_len(dev);
1242 if (regs.len > reglen)
1245 regbuf = vzalloc(reglen);
1249 if (regs.len < reglen)
1252 ops->get_regs(dev, ®s, regbuf);
1255 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1257 useraddr += offsetof(struct ethtool_regs, data);
1258 if (copy_to_user(useraddr, regbuf, reglen))
1267 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1269 struct ethtool_value reset;
1272 if (!dev->ethtool_ops->reset)
1275 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1278 ret = dev->ethtool_ops->reset(dev, &reset.data);
1282 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1287 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1289 struct ethtool_wolinfo wol;
1291 if (!dev->ethtool_ops->get_wol)
1294 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1295 wol.cmd = ETHTOOL_GWOL;
1296 dev->ethtool_ops->get_wol(dev, &wol);
1298 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1303 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1305 struct ethtool_wolinfo wol;
1308 if (!dev->ethtool_ops->set_wol)
1311 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1314 ret = dev->ethtool_ops->set_wol(dev, &wol);
1318 dev->wol_enabled = !!wol.wolopts;
1323 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1325 struct ethtool_eee edata;
1328 if (!dev->ethtool_ops->get_eee)
1331 memset(&edata, 0, sizeof(struct ethtool_eee));
1332 edata.cmd = ETHTOOL_GEEE;
1333 rc = dev->ethtool_ops->get_eee(dev, &edata);
1338 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1344 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1346 struct ethtool_eee edata;
1348 if (!dev->ethtool_ops->set_eee)
1351 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1354 return dev->ethtool_ops->set_eee(dev, &edata);
1357 static int ethtool_nway_reset(struct net_device *dev)
1359 if (!dev->ethtool_ops->nway_reset)
1362 return dev->ethtool_ops->nway_reset(dev);
1365 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1367 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1369 if (!dev->ethtool_ops->get_link)
1372 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1374 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1379 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1380 int (*getter)(struct net_device *,
1381 struct ethtool_eeprom *, u8 *),
1384 struct ethtool_eeprom eeprom;
1385 void __user *userbuf = useraddr + sizeof(eeprom);
1386 u32 bytes_remaining;
1390 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1393 /* Check for wrap and zero */
1394 if (eeprom.offset + eeprom.len <= eeprom.offset)
1397 /* Check for exceeding total eeprom len */
1398 if (eeprom.offset + eeprom.len > total_len)
1401 data = kmalloc(PAGE_SIZE, GFP_USER);
1405 bytes_remaining = eeprom.len;
1406 while (bytes_remaining > 0) {
1407 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1409 ret = getter(dev, &eeprom, data);
1412 if (copy_to_user(userbuf, data, eeprom.len)) {
1416 userbuf += eeprom.len;
1417 eeprom.offset += eeprom.len;
1418 bytes_remaining -= eeprom.len;
1421 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1422 eeprom.offset -= eeprom.len;
1423 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1430 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1432 const struct ethtool_ops *ops = dev->ethtool_ops;
1434 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1435 !ops->get_eeprom_len(dev))
1438 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1439 ops->get_eeprom_len(dev));
1442 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1444 struct ethtool_eeprom eeprom;
1445 const struct ethtool_ops *ops = dev->ethtool_ops;
1446 void __user *userbuf = useraddr + sizeof(eeprom);
1447 u32 bytes_remaining;
1451 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1452 !ops->get_eeprom_len(dev))
1455 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1458 /* Check for wrap and zero */
1459 if (eeprom.offset + eeprom.len <= eeprom.offset)
1462 /* Check for exceeding total eeprom len */
1463 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1466 data = kmalloc(PAGE_SIZE, GFP_USER);
1470 bytes_remaining = eeprom.len;
1471 while (bytes_remaining > 0) {
1472 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1474 if (copy_from_user(data, userbuf, eeprom.len)) {
1478 ret = ops->set_eeprom(dev, &eeprom, data);
1481 userbuf += eeprom.len;
1482 eeprom.offset += eeprom.len;
1483 bytes_remaining -= eeprom.len;
1490 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1491 void __user *useraddr)
1493 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1495 if (!dev->ethtool_ops->get_coalesce)
1498 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1500 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1505 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1506 void __user *useraddr)
1508 struct ethtool_coalesce coalesce;
1510 if (!dev->ethtool_ops->set_coalesce)
1513 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1516 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1519 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1521 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1523 if (!dev->ethtool_ops->get_ringparam)
1526 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1528 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1533 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1535 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1537 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1540 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1543 dev->ethtool_ops->get_ringparam(dev, &max);
1545 /* ensure new ring parameters are within the maximums */
1546 if (ringparam.rx_pending > max.rx_max_pending ||
1547 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1548 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1549 ringparam.tx_pending > max.tx_max_pending)
1552 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1555 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1556 void __user *useraddr)
1558 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1560 if (!dev->ethtool_ops->get_channels)
1563 dev->ethtool_ops->get_channels(dev, &channels);
1565 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1570 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1571 void __user *useraddr)
1573 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1574 u16 from_channel, to_channel;
1575 u32 max_rx_in_use = 0;
1578 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1581 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1584 dev->ethtool_ops->get_channels(dev, &curr);
1586 /* ensure new counts are within the maximums */
1587 if (channels.rx_count > curr.max_rx ||
1588 channels.tx_count > curr.max_tx ||
1589 channels.combined_count > curr.max_combined ||
1590 channels.other_count > curr.max_other)
1593 /* ensure the new Rx count fits within the configured Rx flow
1594 * indirection table settings */
1595 if (netif_is_rxfh_configured(dev) &&
1596 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1597 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1600 /* Disabling channels, query zero-copy AF_XDP sockets */
1601 from_channel = channels.combined_count +
1602 min(channels.rx_count, channels.tx_count);
1603 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1604 for (i = from_channel; i < to_channel; i++)
1605 if (xdp_get_umem_from_qid(dev, i))
1608 return dev->ethtool_ops->set_channels(dev, &channels);
1611 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1613 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1615 if (!dev->ethtool_ops->get_pauseparam)
1618 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1620 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1625 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1627 struct ethtool_pauseparam pauseparam;
1629 if (!dev->ethtool_ops->set_pauseparam)
1632 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1635 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1638 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1640 struct ethtool_test test;
1641 const struct ethtool_ops *ops = dev->ethtool_ops;
1645 if (!ops->self_test || !ops->get_sset_count)
1648 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1651 WARN_ON(test_len == 0);
1653 if (copy_from_user(&test, useraddr, sizeof(test)))
1656 test.len = test_len;
1657 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1661 ops->self_test(dev, &test, data);
1664 if (copy_to_user(useraddr, &test, sizeof(test)))
1666 useraddr += sizeof(test);
1667 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1676 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1678 struct ethtool_gstrings gstrings;
1682 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1685 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1688 if (ret > S32_MAX / ETH_GSTRING_LEN)
1695 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1699 __ethtool_get_strings(dev, gstrings.string_set, data);
1705 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1707 useraddr += sizeof(gstrings);
1709 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1718 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1720 struct ethtool_value id;
1722 const struct ethtool_ops *ops = dev->ethtool_ops;
1725 if (!ops->set_phys_id)
1731 if (copy_from_user(&id, useraddr, sizeof(id)))
1734 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1738 /* Drop the RTNL lock while waiting, but prevent reentry or
1739 * removal of the device.
1746 /* Driver will handle this itself */
1747 schedule_timeout_interruptible(
1748 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1750 /* Driver expects to be called at twice the frequency in rc */
1751 int n = rc * 2, i, interval = HZ / n;
1753 /* Count down seconds */
1755 /* Count down iterations per second */
1759 rc = ops->set_phys_id(dev,
1760 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1764 schedule_timeout_interruptible(interval);
1765 } while (!signal_pending(current) && --i != 0);
1766 } while (!signal_pending(current) &&
1767 (id.data == 0 || --id.data != 0));
1774 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1778 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1780 struct ethtool_stats stats;
1781 const struct ethtool_ops *ops = dev->ethtool_ops;
1785 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1788 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1791 if (n_stats > S32_MAX / sizeof(u64))
1793 WARN_ON_ONCE(!n_stats);
1794 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1797 stats.n_stats = n_stats;
1800 data = vzalloc(array_size(n_stats, sizeof(u64)));
1803 ops->get_ethtool_stats(dev, &stats, data);
1809 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1811 useraddr += sizeof(stats);
1812 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1821 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1823 const struct ethtool_ops *ops = dev->ethtool_ops;
1824 struct phy_device *phydev = dev->phydev;
1825 struct ethtool_stats stats;
1829 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1832 if (dev->phydev && !ops->get_ethtool_phy_stats)
1833 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1835 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1838 if (n_stats > S32_MAX / sizeof(u64))
1840 WARN_ON_ONCE(!n_stats);
1842 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1845 stats.n_stats = n_stats;
1848 data = vzalloc(array_size(n_stats, sizeof(u64)));
1852 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1853 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1857 ops->get_ethtool_phy_stats(dev, &stats, data);
1864 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1866 useraddr += sizeof(stats);
1867 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1876 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1878 struct ethtool_perm_addr epaddr;
1880 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1883 if (epaddr.size < dev->addr_len)
1885 epaddr.size = dev->addr_len;
1887 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1889 useraddr += sizeof(epaddr);
1890 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1895 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1896 u32 cmd, u32 (*actor)(struct net_device *))
1898 struct ethtool_value edata = { .cmd = cmd };
1903 edata.data = actor(dev);
1905 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1910 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1911 void (*actor)(struct net_device *, u32))
1913 struct ethtool_value edata;
1918 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1921 actor(dev, edata.data);
1925 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1926 int (*actor)(struct net_device *, u32))
1928 struct ethtool_value edata;
1933 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1936 return actor(dev, edata.data);
1939 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1940 char __user *useraddr)
1942 struct ethtool_flash efl;
1944 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1946 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1948 if (!dev->ethtool_ops->flash_device)
1949 return devlink_compat_flash_update(dev, efl.data);
1951 return dev->ethtool_ops->flash_device(dev, &efl);
1954 static int ethtool_set_dump(struct net_device *dev,
1955 void __user *useraddr)
1957 struct ethtool_dump dump;
1959 if (!dev->ethtool_ops->set_dump)
1962 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1965 return dev->ethtool_ops->set_dump(dev, &dump);
1968 static int ethtool_get_dump_flag(struct net_device *dev,
1969 void __user *useraddr)
1972 struct ethtool_dump dump;
1973 const struct ethtool_ops *ops = dev->ethtool_ops;
1975 if (!ops->get_dump_flag)
1978 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1981 ret = ops->get_dump_flag(dev, &dump);
1985 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1990 static int ethtool_get_dump_data(struct net_device *dev,
1991 void __user *useraddr)
1995 struct ethtool_dump dump, tmp;
1996 const struct ethtool_ops *ops = dev->ethtool_ops;
1999 if (!ops->get_dump_data || !ops->get_dump_flag)
2002 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2005 memset(&tmp, 0, sizeof(tmp));
2006 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2007 ret = ops->get_dump_flag(dev, &tmp);
2011 len = min(tmp.len, dump.len);
2015 /* Don't ever let the driver think there's more space available
2016 * than it requested with .get_dump_flag().
2020 /* Always allocate enough space to hold the whole thing so that the
2021 * driver does not need to check the length and bother with partial
2024 data = vzalloc(tmp.len);
2027 ret = ops->get_dump_data(dev, &dump, data);
2031 /* There are two sane possibilities:
2032 * 1. The driver's .get_dump_data() does not touch dump.len.
2033 * 2. Or it may set dump.len to how much it really writes, which
2034 * should be tmp.len (or len if it can do a partial dump).
2035 * In any case respond to userspace with the actual length of data
2038 WARN_ON(dump.len != len && dump.len != tmp.len);
2041 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2045 useraddr += offsetof(struct ethtool_dump, data);
2046 if (copy_to_user(useraddr, data, len))
2053 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2056 struct ethtool_ts_info info;
2057 const struct ethtool_ops *ops = dev->ethtool_ops;
2058 struct phy_device *phydev = dev->phydev;
2060 memset(&info, 0, sizeof(info));
2061 info.cmd = ETHTOOL_GET_TS_INFO;
2063 if (phy_has_tsinfo(phydev)) {
2064 err = phy_ts_info(phydev, &info);
2065 } else if (ops->get_ts_info) {
2066 err = ops->get_ts_info(dev, &info);
2068 info.so_timestamping =
2069 SOF_TIMESTAMPING_RX_SOFTWARE |
2070 SOF_TIMESTAMPING_SOFTWARE;
2071 info.phc_index = -1;
2077 if (copy_to_user(useraddr, &info, sizeof(info)))
2083 static int __ethtool_get_module_info(struct net_device *dev,
2084 struct ethtool_modinfo *modinfo)
2086 const struct ethtool_ops *ops = dev->ethtool_ops;
2087 struct phy_device *phydev = dev->phydev;
2090 return sfp_get_module_info(dev->sfp_bus, modinfo);
2092 if (phydev && phydev->drv && phydev->drv->module_info)
2093 return phydev->drv->module_info(phydev, modinfo);
2095 if (ops->get_module_info)
2096 return ops->get_module_info(dev, modinfo);
2101 static int ethtool_get_module_info(struct net_device *dev,
2102 void __user *useraddr)
2105 struct ethtool_modinfo modinfo;
2107 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2110 ret = __ethtool_get_module_info(dev, &modinfo);
2114 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2120 static int __ethtool_get_module_eeprom(struct net_device *dev,
2121 struct ethtool_eeprom *ee, u8 *data)
2123 const struct ethtool_ops *ops = dev->ethtool_ops;
2124 struct phy_device *phydev = dev->phydev;
2127 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2129 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2130 return phydev->drv->module_eeprom(phydev, ee, data);
2132 if (ops->get_module_eeprom)
2133 return ops->get_module_eeprom(dev, ee, data);
2138 static int ethtool_get_module_eeprom(struct net_device *dev,
2139 void __user *useraddr)
2142 struct ethtool_modinfo modinfo;
2144 ret = __ethtool_get_module_info(dev, &modinfo);
2148 return ethtool_get_any_eeprom(dev, useraddr,
2149 __ethtool_get_module_eeprom,
2150 modinfo.eeprom_len);
2153 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2156 case ETHTOOL_RX_COPYBREAK:
2157 case ETHTOOL_TX_COPYBREAK:
2158 if (tuna->len != sizeof(u32) ||
2159 tuna->type_id != ETHTOOL_TUNABLE_U32)
2162 case ETHTOOL_PFC_PREVENTION_TOUT:
2163 if (tuna->len != sizeof(u16) ||
2164 tuna->type_id != ETHTOOL_TUNABLE_U16)
2174 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2177 struct ethtool_tunable tuna;
2178 const struct ethtool_ops *ops = dev->ethtool_ops;
2181 if (!ops->get_tunable)
2183 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2185 ret = ethtool_tunable_valid(&tuna);
2188 data = kmalloc(tuna.len, GFP_USER);
2191 ret = ops->get_tunable(dev, &tuna, data);
2194 useraddr += sizeof(tuna);
2196 if (copy_to_user(useraddr, data, tuna.len))
2205 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2208 struct ethtool_tunable tuna;
2209 const struct ethtool_ops *ops = dev->ethtool_ops;
2212 if (!ops->set_tunable)
2214 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2216 ret = ethtool_tunable_valid(&tuna);
2219 useraddr += sizeof(tuna);
2220 data = memdup_user(useraddr, tuna.len);
2222 return PTR_ERR(data);
2223 ret = ops->set_tunable(dev, &tuna, data);
2229 static noinline_for_stack int
2230 ethtool_get_per_queue_coalesce(struct net_device *dev,
2231 void __user *useraddr,
2232 struct ethtool_per_queue_op *per_queue_opt)
2236 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2238 if (!dev->ethtool_ops->get_per_queue_coalesce)
2241 useraddr += sizeof(*per_queue_opt);
2243 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2246 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2247 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2249 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2252 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2254 useraddr += sizeof(coalesce);
2260 static noinline_for_stack int
2261 ethtool_set_per_queue_coalesce(struct net_device *dev,
2262 void __user *useraddr,
2263 struct ethtool_per_queue_op *per_queue_opt)
2268 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2269 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2271 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2272 (!dev->ethtool_ops->get_per_queue_coalesce))
2275 useraddr += sizeof(*per_queue_opt);
2277 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2278 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2279 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2283 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2284 struct ethtool_coalesce coalesce;
2286 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2292 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2297 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2301 useraddr += sizeof(coalesce);
2307 for_each_set_bit(i, queue_mask, bit) {
2308 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2317 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2318 void __user *useraddr, u32 sub_cmd)
2320 struct ethtool_per_queue_op per_queue_opt;
2322 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2325 if (per_queue_opt.sub_command != sub_cmd)
2328 switch (per_queue_opt.sub_command) {
2329 case ETHTOOL_GCOALESCE:
2330 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2331 case ETHTOOL_SCOALESCE:
2332 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2338 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2341 case ETHTOOL_PHY_DOWNSHIFT:
2342 case ETHTOOL_PHY_FAST_LINK_DOWN:
2343 if (tuna->len != sizeof(u8) ||
2344 tuna->type_id != ETHTOOL_TUNABLE_U8)
2347 case ETHTOOL_PHY_EDPD:
2348 if (tuna->len != sizeof(u16) ||
2349 tuna->type_id != ETHTOOL_TUNABLE_U16)
2359 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2362 struct ethtool_tunable tuna;
2363 struct phy_device *phydev = dev->phydev;
2366 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2369 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2371 ret = ethtool_phy_tunable_valid(&tuna);
2374 data = kmalloc(tuna.len, GFP_USER);
2377 mutex_lock(&phydev->lock);
2378 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2379 mutex_unlock(&phydev->lock);
2382 useraddr += sizeof(tuna);
2384 if (copy_to_user(useraddr, data, tuna.len))
2393 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2396 struct ethtool_tunable tuna;
2397 struct phy_device *phydev = dev->phydev;
2400 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2402 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2404 ret = ethtool_phy_tunable_valid(&tuna);
2407 useraddr += sizeof(tuna);
2408 data = memdup_user(useraddr, tuna.len);
2410 return PTR_ERR(data);
2411 mutex_lock(&phydev->lock);
2412 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2413 mutex_unlock(&phydev->lock);
2419 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2421 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2424 if (!dev->ethtool_ops->get_fecparam)
2427 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2431 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2436 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2438 struct ethtool_fecparam fecparam;
2440 if (!dev->ethtool_ops->set_fecparam)
2443 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2446 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2449 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2451 int dev_ethtool(struct net *net, struct ifreq *ifr)
2453 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2454 void __user *useraddr = ifr->ifr_data;
2455 u32 ethcmd, sub_cmd;
2457 netdev_features_t old_features;
2459 if (!dev || !netif_device_present(dev))
2462 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2465 if (ethcmd == ETHTOOL_PERQUEUE) {
2466 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2471 /* Allow some commands to be done by anyone */
2474 case ETHTOOL_GDRVINFO:
2475 case ETHTOOL_GMSGLVL:
2477 case ETHTOOL_GCOALESCE:
2478 case ETHTOOL_GRINGPARAM:
2479 case ETHTOOL_GPAUSEPARAM:
2480 case ETHTOOL_GRXCSUM:
2481 case ETHTOOL_GTXCSUM:
2483 case ETHTOOL_GSSET_INFO:
2484 case ETHTOOL_GSTRINGS:
2485 case ETHTOOL_GSTATS:
2486 case ETHTOOL_GPHYSTATS:
2488 case ETHTOOL_GPERMADDR:
2492 case ETHTOOL_GFLAGS:
2493 case ETHTOOL_GPFLAGS:
2495 case ETHTOOL_GRXRINGS:
2496 case ETHTOOL_GRXCLSRLCNT:
2497 case ETHTOOL_GRXCLSRULE:
2498 case ETHTOOL_GRXCLSRLALL:
2499 case ETHTOOL_GRXFHINDIR:
2501 case ETHTOOL_GFEATURES:
2502 case ETHTOOL_GCHANNELS:
2503 case ETHTOOL_GET_TS_INFO:
2505 case ETHTOOL_GTUNABLE:
2506 case ETHTOOL_PHY_GTUNABLE:
2507 case ETHTOOL_GLINKSETTINGS:
2508 case ETHTOOL_GFECPARAM:
2511 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2515 if (dev->ethtool_ops->begin) {
2516 rc = dev->ethtool_ops->begin(dev);
2520 old_features = dev->features;
2524 rc = ethtool_get_settings(dev, useraddr);
2527 rc = ethtool_set_settings(dev, useraddr);
2529 case ETHTOOL_GDRVINFO:
2530 rc = ethtool_get_drvinfo(dev, useraddr);
2533 rc = ethtool_get_regs(dev, useraddr);
2536 rc = ethtool_get_wol(dev, useraddr);
2539 rc = ethtool_set_wol(dev, useraddr);
2541 case ETHTOOL_GMSGLVL:
2542 rc = ethtool_get_value(dev, useraddr, ethcmd,
2543 dev->ethtool_ops->get_msglevel);
2545 case ETHTOOL_SMSGLVL:
2546 rc = ethtool_set_value_void(dev, useraddr,
2547 dev->ethtool_ops->set_msglevel);
2550 rc = ethtool_get_eee(dev, useraddr);
2553 rc = ethtool_set_eee(dev, useraddr);
2555 case ETHTOOL_NWAY_RST:
2556 rc = ethtool_nway_reset(dev);
2559 rc = ethtool_get_link(dev, useraddr);
2561 case ETHTOOL_GEEPROM:
2562 rc = ethtool_get_eeprom(dev, useraddr);
2564 case ETHTOOL_SEEPROM:
2565 rc = ethtool_set_eeprom(dev, useraddr);
2567 case ETHTOOL_GCOALESCE:
2568 rc = ethtool_get_coalesce(dev, useraddr);
2570 case ETHTOOL_SCOALESCE:
2571 rc = ethtool_set_coalesce(dev, useraddr);
2573 case ETHTOOL_GRINGPARAM:
2574 rc = ethtool_get_ringparam(dev, useraddr);
2576 case ETHTOOL_SRINGPARAM:
2577 rc = ethtool_set_ringparam(dev, useraddr);
2579 case ETHTOOL_GPAUSEPARAM:
2580 rc = ethtool_get_pauseparam(dev, useraddr);
2582 case ETHTOOL_SPAUSEPARAM:
2583 rc = ethtool_set_pauseparam(dev, useraddr);
2586 rc = ethtool_self_test(dev, useraddr);
2588 case ETHTOOL_GSTRINGS:
2589 rc = ethtool_get_strings(dev, useraddr);
2591 case ETHTOOL_PHYS_ID:
2592 rc = ethtool_phys_id(dev, useraddr);
2594 case ETHTOOL_GSTATS:
2595 rc = ethtool_get_stats(dev, useraddr);
2597 case ETHTOOL_GPERMADDR:
2598 rc = ethtool_get_perm_addr(dev, useraddr);
2600 case ETHTOOL_GFLAGS:
2601 rc = ethtool_get_value(dev, useraddr, ethcmd,
2602 __ethtool_get_flags);
2604 case ETHTOOL_SFLAGS:
2605 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2607 case ETHTOOL_GPFLAGS:
2608 rc = ethtool_get_value(dev, useraddr, ethcmd,
2609 dev->ethtool_ops->get_priv_flags);
2611 case ETHTOOL_SPFLAGS:
2612 rc = ethtool_set_value(dev, useraddr,
2613 dev->ethtool_ops->set_priv_flags);
2616 case ETHTOOL_GRXRINGS:
2617 case ETHTOOL_GRXCLSRLCNT:
2618 case ETHTOOL_GRXCLSRULE:
2619 case ETHTOOL_GRXCLSRLALL:
2620 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2623 case ETHTOOL_SRXCLSRLDEL:
2624 case ETHTOOL_SRXCLSRLINS:
2625 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2627 case ETHTOOL_FLASHDEV:
2628 rc = ethtool_flash_device(dev, useraddr);
2631 rc = ethtool_reset(dev, useraddr);
2633 case ETHTOOL_GSSET_INFO:
2634 rc = ethtool_get_sset_info(dev, useraddr);
2636 case ETHTOOL_GRXFHINDIR:
2637 rc = ethtool_get_rxfh_indir(dev, useraddr);
2639 case ETHTOOL_SRXFHINDIR:
2640 rc = ethtool_set_rxfh_indir(dev, useraddr);
2643 rc = ethtool_get_rxfh(dev, useraddr);
2646 rc = ethtool_set_rxfh(dev, useraddr);
2648 case ETHTOOL_GFEATURES:
2649 rc = ethtool_get_features(dev, useraddr);
2651 case ETHTOOL_SFEATURES:
2652 rc = ethtool_set_features(dev, useraddr);
2654 case ETHTOOL_GTXCSUM:
2655 case ETHTOOL_GRXCSUM:
2660 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2662 case ETHTOOL_STXCSUM:
2663 case ETHTOOL_SRXCSUM:
2668 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2670 case ETHTOOL_GCHANNELS:
2671 rc = ethtool_get_channels(dev, useraddr);
2673 case ETHTOOL_SCHANNELS:
2674 rc = ethtool_set_channels(dev, useraddr);
2676 case ETHTOOL_SET_DUMP:
2677 rc = ethtool_set_dump(dev, useraddr);
2679 case ETHTOOL_GET_DUMP_FLAG:
2680 rc = ethtool_get_dump_flag(dev, useraddr);
2682 case ETHTOOL_GET_DUMP_DATA:
2683 rc = ethtool_get_dump_data(dev, useraddr);
2685 case ETHTOOL_GET_TS_INFO:
2686 rc = ethtool_get_ts_info(dev, useraddr);
2688 case ETHTOOL_GMODULEINFO:
2689 rc = ethtool_get_module_info(dev, useraddr);
2691 case ETHTOOL_GMODULEEEPROM:
2692 rc = ethtool_get_module_eeprom(dev, useraddr);
2694 case ETHTOOL_GTUNABLE:
2695 rc = ethtool_get_tunable(dev, useraddr);
2697 case ETHTOOL_STUNABLE:
2698 rc = ethtool_set_tunable(dev, useraddr);
2700 case ETHTOOL_GPHYSTATS:
2701 rc = ethtool_get_phy_stats(dev, useraddr);
2703 case ETHTOOL_PERQUEUE:
2704 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2706 case ETHTOOL_GLINKSETTINGS:
2707 rc = ethtool_get_link_ksettings(dev, useraddr);
2709 case ETHTOOL_SLINKSETTINGS:
2710 rc = ethtool_set_link_ksettings(dev, useraddr);
2712 case ETHTOOL_PHY_GTUNABLE:
2713 rc = get_phy_tunable(dev, useraddr);
2715 case ETHTOOL_PHY_STUNABLE:
2716 rc = set_phy_tunable(dev, useraddr);
2718 case ETHTOOL_GFECPARAM:
2719 rc = ethtool_get_fecparam(dev, useraddr);
2721 case ETHTOOL_SFECPARAM:
2722 rc = ethtool_set_fecparam(dev, useraddr);
2728 if (dev->ethtool_ops->complete)
2729 dev->ethtool_ops->complete(dev);
2731 if (old_features != dev->features)
2732 netdev_features_change(dev);
2737 struct ethtool_rx_flow_key {
2738 struct flow_dissector_key_basic basic;
2740 struct flow_dissector_key_ipv4_addrs ipv4;
2741 struct flow_dissector_key_ipv6_addrs ipv6;
2743 struct flow_dissector_key_ports tp;
2744 struct flow_dissector_key_ip ip;
2745 struct flow_dissector_key_vlan vlan;
2746 struct flow_dissector_key_eth_addrs eth_addrs;
2747 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2749 struct ethtool_rx_flow_match {
2750 struct flow_dissector dissector;
2751 struct ethtool_rx_flow_key key;
2752 struct ethtool_rx_flow_key mask;
2755 struct ethtool_rx_flow_rule *
2756 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2758 const struct ethtool_rx_flow_spec *fs = input->fs;
2759 static struct in6_addr zero_addr = {};
2760 struct ethtool_rx_flow_match *match;
2761 struct ethtool_rx_flow_rule *flow;
2762 struct flow_action_entry *act;
2764 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2765 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2767 return ERR_PTR(-ENOMEM);
2769 /* ethtool_rx supports only one single action per rule. */
2770 flow->rule = flow_rule_alloc(1);
2773 return ERR_PTR(-ENOMEM);
2776 match = (struct ethtool_rx_flow_match *)flow->priv;
2777 flow->rule->match.dissector = &match->dissector;
2778 flow->rule->match.mask = &match->mask;
2779 flow->rule->match.key = &match->key;
2781 match->mask.basic.n_proto = htons(0xffff);
2783 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2785 const struct ethhdr *ether_spec, *ether_m_spec;
2787 ether_spec = &fs->h_u.ether_spec;
2788 ether_m_spec = &fs->m_u.ether_spec;
2790 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2791 ether_addr_copy(match->key.eth_addrs.src,
2792 ether_spec->h_source);
2793 ether_addr_copy(match->mask.eth_addrs.src,
2794 ether_m_spec->h_source);
2796 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2797 ether_addr_copy(match->key.eth_addrs.dst,
2798 ether_spec->h_dest);
2799 ether_addr_copy(match->mask.eth_addrs.dst,
2800 ether_m_spec->h_dest);
2802 if (ether_m_spec->h_proto) {
2803 match->key.basic.n_proto = ether_spec->h_proto;
2804 match->mask.basic.n_proto = ether_m_spec->h_proto;
2810 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2812 match->key.basic.n_proto = htons(ETH_P_IP);
2814 v4_spec = &fs->h_u.tcp_ip4_spec;
2815 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2817 if (v4_m_spec->ip4src) {
2818 match->key.ipv4.src = v4_spec->ip4src;
2819 match->mask.ipv4.src = v4_m_spec->ip4src;
2821 if (v4_m_spec->ip4dst) {
2822 match->key.ipv4.dst = v4_spec->ip4dst;
2823 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2825 if (v4_m_spec->ip4src ||
2826 v4_m_spec->ip4dst) {
2827 match->dissector.used_keys |=
2828 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2829 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2830 offsetof(struct ethtool_rx_flow_key, ipv4);
2832 if (v4_m_spec->psrc) {
2833 match->key.tp.src = v4_spec->psrc;
2834 match->mask.tp.src = v4_m_spec->psrc;
2836 if (v4_m_spec->pdst) {
2837 match->key.tp.dst = v4_spec->pdst;
2838 match->mask.tp.dst = v4_m_spec->pdst;
2840 if (v4_m_spec->psrc ||
2842 match->dissector.used_keys |=
2843 BIT(FLOW_DISSECTOR_KEY_PORTS);
2844 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2845 offsetof(struct ethtool_rx_flow_key, tp);
2847 if (v4_m_spec->tos) {
2848 match->key.ip.tos = v4_spec->tos;
2849 match->mask.ip.tos = v4_m_spec->tos;
2850 match->dissector.used_keys |=
2851 BIT(FLOW_DISSECTOR_KEY_IP);
2852 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2853 offsetof(struct ethtool_rx_flow_key, ip);
2859 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2861 match->key.basic.n_proto = htons(ETH_P_IPV6);
2863 v6_spec = &fs->h_u.tcp_ip6_spec;
2864 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2865 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2866 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2867 sizeof(match->key.ipv6.src));
2868 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2869 sizeof(match->mask.ipv6.src));
2871 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2872 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2873 sizeof(match->key.ipv6.dst));
2874 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2875 sizeof(match->mask.ipv6.dst));
2877 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2878 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2879 match->dissector.used_keys |=
2880 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2881 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2882 offsetof(struct ethtool_rx_flow_key, ipv6);
2884 if (v6_m_spec->psrc) {
2885 match->key.tp.src = v6_spec->psrc;
2886 match->mask.tp.src = v6_m_spec->psrc;
2888 if (v6_m_spec->pdst) {
2889 match->key.tp.dst = v6_spec->pdst;
2890 match->mask.tp.dst = v6_m_spec->pdst;
2892 if (v6_m_spec->psrc ||
2894 match->dissector.used_keys |=
2895 BIT(FLOW_DISSECTOR_KEY_PORTS);
2896 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2897 offsetof(struct ethtool_rx_flow_key, tp);
2899 if (v6_m_spec->tclass) {
2900 match->key.ip.tos = v6_spec->tclass;
2901 match->mask.ip.tos = v6_m_spec->tclass;
2902 match->dissector.used_keys |=
2903 BIT(FLOW_DISSECTOR_KEY_IP);
2904 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2905 offsetof(struct ethtool_rx_flow_key, ip);
2910 ethtool_rx_flow_rule_destroy(flow);
2911 return ERR_PTR(-EINVAL);
2914 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2917 match->key.basic.ip_proto = IPPROTO_TCP;
2921 match->key.basic.ip_proto = IPPROTO_UDP;
2924 match->mask.basic.ip_proto = 0xff;
2926 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
2927 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
2928 offsetof(struct ethtool_rx_flow_key, basic);
2930 if (fs->flow_type & FLOW_EXT) {
2931 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
2932 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
2934 if (ext_m_spec->vlan_etype) {
2935 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
2936 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
2939 if (ext_m_spec->vlan_tci) {
2940 match->key.vlan.vlan_id =
2941 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
2942 match->mask.vlan.vlan_id =
2943 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
2945 match->key.vlan.vlan_dei =
2946 !!(ext_h_spec->vlan_tci & htons(0x1000));
2947 match->mask.vlan.vlan_dei =
2948 !!(ext_m_spec->vlan_tci & htons(0x1000));
2950 match->key.vlan.vlan_priority =
2951 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
2952 match->mask.vlan.vlan_priority =
2953 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
2956 if (ext_m_spec->vlan_etype ||
2957 ext_m_spec->vlan_tci) {
2958 match->dissector.used_keys |=
2959 BIT(FLOW_DISSECTOR_KEY_VLAN);
2960 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
2961 offsetof(struct ethtool_rx_flow_key, vlan);
2964 if (fs->flow_type & FLOW_MAC_EXT) {
2965 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
2966 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
2968 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
2970 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
2973 match->dissector.used_keys |=
2974 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
2975 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
2976 offsetof(struct ethtool_rx_flow_key, eth_addrs);
2979 act = &flow->rule->action.entries[0];
2980 switch (fs->ring_cookie) {
2981 case RX_CLS_FLOW_DISC:
2982 act->id = FLOW_ACTION_DROP;
2984 case RX_CLS_FLOW_WAKE:
2985 act->id = FLOW_ACTION_WAKE;
2988 act->id = FLOW_ACTION_QUEUE;
2989 if (fs->flow_type & FLOW_RSS)
2990 act->queue.ctx = input->rss_ctx;
2992 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
2993 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
2999 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3001 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3006 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);