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 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
61 struct ethtool_gfeatures cmd = {
62 .cmd = ETHTOOL_GFEATURES,
63 .size = ETHTOOL_DEV_FEATURE_WORDS,
65 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
70 /* in case feature bits run out again */
71 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
73 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
74 features[i].available = (u32)(dev->hw_features >> (32 * i));
75 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
76 features[i].active = (u32)(dev->features >> (32 * i));
77 features[i].never_changed =
78 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
81 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
82 if (get_user(copy_size, sizeaddr))
85 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
86 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
88 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
90 useraddr += sizeof(cmd);
91 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
97 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
99 struct ethtool_sfeatures cmd;
100 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
101 netdev_features_t wanted = 0, valid = 0;
104 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
106 useraddr += sizeof(cmd);
108 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
111 if (copy_from_user(features, useraddr, sizeof(features)))
114 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
115 valid |= (netdev_features_t)features[i].valid << (32 * i);
116 wanted |= (netdev_features_t)features[i].requested << (32 * i);
119 if (valid & ~NETIF_F_ETHTOOL_BITS)
122 if (valid & ~dev->hw_features) {
123 valid &= dev->hw_features;
124 ret |= ETHTOOL_F_UNSUPPORTED;
127 dev->wanted_features &= ~valid;
128 dev->wanted_features |= wanted & valid;
129 __netdev_update_features(dev);
131 if ((dev->wanted_features ^ dev->features) & valid)
132 ret |= ETHTOOL_F_WISH;
137 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
139 const struct ethtool_ops *ops = dev->ethtool_ops;
141 if (sset == ETH_SS_FEATURES)
142 return ARRAY_SIZE(netdev_features_strings);
144 if (sset == ETH_SS_RSS_HASH_FUNCS)
145 return ARRAY_SIZE(rss_hash_func_strings);
147 if (sset == ETH_SS_TUNABLES)
148 return ARRAY_SIZE(tunable_strings);
150 if (sset == ETH_SS_PHY_TUNABLES)
151 return ARRAY_SIZE(phy_tunable_strings);
153 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
154 !ops->get_ethtool_phy_stats)
155 return phy_ethtool_get_sset_count(dev->phydev);
157 if (sset == ETH_SS_LINK_MODES)
158 return __ETHTOOL_LINK_MODE_MASK_NBITS;
160 if (ops->get_sset_count && ops->get_strings)
161 return ops->get_sset_count(dev, sset);
166 static void __ethtool_get_strings(struct net_device *dev,
167 u32 stringset, u8 *data)
169 const struct ethtool_ops *ops = dev->ethtool_ops;
171 if (stringset == ETH_SS_FEATURES)
172 memcpy(data, netdev_features_strings,
173 sizeof(netdev_features_strings));
174 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
175 memcpy(data, rss_hash_func_strings,
176 sizeof(rss_hash_func_strings));
177 else if (stringset == ETH_SS_TUNABLES)
178 memcpy(data, tunable_strings, sizeof(tunable_strings));
179 else if (stringset == ETH_SS_PHY_TUNABLES)
180 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
181 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
182 !ops->get_ethtool_phy_stats)
183 phy_ethtool_get_strings(dev->phydev, data);
184 else if (stringset == ETH_SS_LINK_MODES)
185 memcpy(data, link_mode_names,
186 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
188 /* ops->get_strings is valid because checked earlier */
189 ops->get_strings(dev, stringset, data);
192 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
194 /* feature masks of legacy discrete ethtool ops */
197 case ETHTOOL_GTXCSUM:
198 case ETHTOOL_STXCSUM:
199 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
201 case ETHTOOL_GRXCSUM:
202 case ETHTOOL_SRXCSUM:
203 return NETIF_F_RXCSUM;
206 return NETIF_F_SG | NETIF_F_FRAGLIST;
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 /* Check if the user is trying to change anything besides speed/duplex */
462 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
464 struct ethtool_link_settings base2 = {};
466 base2.speed = cmd->base.speed;
467 base2.port = PORT_OTHER;
468 base2.duplex = cmd->base.duplex;
469 base2.cmd = cmd->base.cmd;
470 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
472 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
473 bitmap_empty(cmd->link_modes.supported,
474 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
475 bitmap_empty(cmd->link_modes.lp_advertising,
476 __ETHTOOL_LINK_MODE_MASK_NBITS);
479 /* convert a kernel internal ethtool_link_ksettings to
480 * ethtool_link_usettings in user space. return 0 on success, errno on
484 store_link_ksettings_for_user(void __user *to,
485 const struct ethtool_link_ksettings *from)
487 struct ethtool_link_usettings link_usettings;
489 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
490 bitmap_to_arr32(link_usettings.link_modes.supported,
491 from->link_modes.supported,
492 __ETHTOOL_LINK_MODE_MASK_NBITS);
493 bitmap_to_arr32(link_usettings.link_modes.advertising,
494 from->link_modes.advertising,
495 __ETHTOOL_LINK_MODE_MASK_NBITS);
496 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
497 from->link_modes.lp_advertising,
498 __ETHTOOL_LINK_MODE_MASK_NBITS);
500 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
506 /* Query device for its ethtool_link_settings. */
507 static int ethtool_get_link_ksettings(struct net_device *dev,
508 void __user *useraddr)
511 struct ethtool_link_ksettings link_ksettings;
514 if (!dev->ethtool_ops->get_link_ksettings)
517 /* handle bitmap nbits handshake */
518 if (copy_from_user(&link_ksettings.base, useraddr,
519 sizeof(link_ksettings.base)))
522 if (__ETHTOOL_LINK_MODE_MASK_NU32
523 != link_ksettings.base.link_mode_masks_nwords) {
524 /* wrong link mode nbits requested */
525 memset(&link_ksettings, 0, sizeof(link_ksettings));
526 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
527 /* send back number of words required as negative val */
528 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
529 "need too many bits for link modes!");
530 link_ksettings.base.link_mode_masks_nwords
531 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
533 /* copy the base fields back to user, not the link
536 if (copy_to_user(useraddr, &link_ksettings.base,
537 sizeof(link_ksettings.base)))
543 /* handshake successful: user/kernel agree on
544 * link_mode_masks_nwords
547 memset(&link_ksettings, 0, sizeof(link_ksettings));
548 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
552 /* make sure we tell the right values to user */
553 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
554 link_ksettings.base.link_mode_masks_nwords
555 = __ETHTOOL_LINK_MODE_MASK_NU32;
557 return store_link_ksettings_for_user(useraddr, &link_ksettings);
560 /* Update device ethtool_link_settings. */
561 static int ethtool_set_link_ksettings(struct net_device *dev,
562 void __user *useraddr)
565 struct ethtool_link_ksettings link_ksettings;
569 if (!dev->ethtool_ops->set_link_ksettings)
572 /* make sure nbits field has expected value */
573 if (copy_from_user(&link_ksettings.base, useraddr,
574 sizeof(link_ksettings.base)))
577 if (__ETHTOOL_LINK_MODE_MASK_NU32
578 != link_ksettings.base.link_mode_masks_nwords)
581 /* copy the whole structure, now that we know it has expected
584 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
588 /* re-check nwords field, just in case */
589 if (__ETHTOOL_LINK_MODE_MASK_NU32
590 != link_ksettings.base.link_mode_masks_nwords)
593 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
595 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
596 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
601 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
602 const struct ethtool_link_ksettings *cmd,
603 u32 *dev_speed, u8 *dev_duplex)
608 speed = cmd->base.speed;
609 duplex = cmd->base.duplex;
610 /* don't allow custom speed and duplex */
611 if (!ethtool_validate_speed(speed) ||
612 !ethtool_validate_duplex(duplex) ||
613 !ethtool_virtdev_validate_cmd(cmd))
616 *dev_duplex = duplex;
620 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
622 /* Query device for its ethtool_cmd settings.
624 * Backward compatibility note: for compatibility with legacy ethtool, this is
625 * now implemented via get_link_ksettings. When driver reports higher link mode
626 * bits, a kernel warning is logged once (with name of 1st driver/device) to
627 * recommend user to upgrade ethtool, but the command is successful (only the
628 * lower link mode bits reported back to user). Deprecated fields from
629 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
631 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
633 struct ethtool_link_ksettings link_ksettings;
634 struct ethtool_cmd cmd;
638 if (!dev->ethtool_ops->get_link_ksettings)
641 memset(&link_ksettings, 0, sizeof(link_ksettings));
642 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
645 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
647 /* send a sensible cmd tag back to user */
648 cmd.cmd = ETHTOOL_GSET;
650 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
656 /* Update device link settings with given ethtool_cmd.
658 * Backward compatibility note: for compatibility with legacy ethtool, this is
659 * now always implemented via set_link_settings. When user's request updates
660 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
661 * warning is logged once (with name of 1st driver/device) to recommend user to
662 * upgrade ethtool, and the request is rejected.
664 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
666 struct ethtool_link_ksettings link_ksettings;
667 struct ethtool_cmd cmd;
672 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
674 if (!dev->ethtool_ops->set_link_ksettings)
677 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
679 link_ksettings.base.link_mode_masks_nwords =
680 __ETHTOOL_LINK_MODE_MASK_NU32;
681 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
683 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
684 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
689 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
690 void __user *useraddr)
692 struct ethtool_drvinfo info;
693 const struct ethtool_ops *ops = dev->ethtool_ops;
695 memset(&info, 0, sizeof(info));
696 info.cmd = ETHTOOL_GDRVINFO;
697 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
698 if (ops->get_drvinfo) {
699 ops->get_drvinfo(dev, &info);
700 } else if (dev->dev.parent && dev->dev.parent->driver) {
701 strlcpy(info.bus_info, dev_name(dev->dev.parent),
702 sizeof(info.bus_info));
703 strlcpy(info.driver, dev->dev.parent->driver->name,
704 sizeof(info.driver));
710 * this method of obtaining string set info is deprecated;
711 * Use ETHTOOL_GSSET_INFO instead.
713 if (ops->get_sset_count) {
716 rc = ops->get_sset_count(dev, ETH_SS_TEST);
718 info.testinfo_len = rc;
719 rc = ops->get_sset_count(dev, ETH_SS_STATS);
722 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
724 info.n_priv_flags = rc;
726 if (ops->get_regs_len) {
727 int ret = ops->get_regs_len(dev);
730 info.regdump_len = ret;
733 if (ops->get_eeprom_len)
734 info.eedump_len = ops->get_eeprom_len(dev);
736 if (!info.fw_version[0])
737 devlink_compat_running_version(dev, info.fw_version,
738 sizeof(info.fw_version));
740 if (copy_to_user(useraddr, &info, sizeof(info)))
745 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
746 void __user *useraddr)
748 struct ethtool_sset_info info;
750 int i, idx = 0, n_bits = 0, ret, rc;
751 u32 *info_buf = NULL;
753 if (copy_from_user(&info, useraddr, sizeof(info)))
756 /* store copy of mask, because we zero struct later on */
757 sset_mask = info.sset_mask;
761 /* calculate size of return buffer */
762 n_bits = hweight64(sset_mask);
764 memset(&info, 0, sizeof(info));
765 info.cmd = ETHTOOL_GSSET_INFO;
767 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
772 * fill return buffer based on input bitmask and successful
773 * get_sset_count return
775 for (i = 0; i < 64; i++) {
776 if (!(sset_mask & (1ULL << i)))
779 rc = __ethtool_get_sset_count(dev, i);
781 info.sset_mask |= (1ULL << i);
782 info_buf[idx++] = rc;
787 if (copy_to_user(useraddr, &info, sizeof(info)))
790 useraddr += offsetof(struct ethtool_sset_info, data);
791 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
801 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
802 u32 cmd, void __user *useraddr)
804 struct ethtool_rxnfc info;
805 size_t info_size = sizeof(info);
808 if (!dev->ethtool_ops->set_rxnfc)
811 /* struct ethtool_rxnfc was originally defined for
812 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
813 * members. User-space might still be using that
815 if (cmd == ETHTOOL_SRXFH)
816 info_size = (offsetof(struct ethtool_rxnfc, data) +
819 if (copy_from_user(&info, useraddr, info_size))
822 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
826 if (cmd == ETHTOOL_SRXCLSRLINS &&
827 copy_to_user(useraddr, &info, info_size))
833 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
834 u32 cmd, void __user *useraddr)
836 struct ethtool_rxnfc info;
837 size_t info_size = sizeof(info);
838 const struct ethtool_ops *ops = dev->ethtool_ops;
840 void *rule_buf = NULL;
845 /* struct ethtool_rxnfc was originally defined for
846 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
847 * members. User-space might still be using that
849 if (cmd == ETHTOOL_GRXFH)
850 info_size = (offsetof(struct ethtool_rxnfc, data) +
853 if (copy_from_user(&info, useraddr, info_size))
856 /* If FLOW_RSS was requested then user-space must be using the
857 * new definition, as FLOW_RSS is newer.
859 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
860 info_size = sizeof(info);
861 if (copy_from_user(&info, useraddr, info_size))
863 /* Since malicious users may modify the original data,
864 * we need to check whether FLOW_RSS is still requested.
866 if (!(info.flow_type & FLOW_RSS))
873 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
874 if (info.rule_cnt > 0) {
875 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
876 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
883 ret = ops->get_rxnfc(dev, &info, rule_buf);
888 if (copy_to_user(useraddr, &info, info_size))
892 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
893 if (copy_to_user(useraddr, rule_buf,
894 info.rule_cnt * sizeof(u32)))
905 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
906 struct ethtool_rxnfc *rx_rings,
911 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
914 /* Validate ring indices */
915 for (i = 0; i < size; i++)
916 if (indir[i] >= rx_rings->data)
922 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
924 void netdev_rss_key_fill(void *buffer, size_t len)
926 BUG_ON(len > sizeof(netdev_rss_key));
927 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
928 memcpy(buffer, netdev_rss_key, len);
930 EXPORT_SYMBOL(netdev_rss_key_fill);
932 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
933 void __user *useraddr)
935 u32 user_size, dev_size;
939 if (!dev->ethtool_ops->get_rxfh_indir_size ||
940 !dev->ethtool_ops->get_rxfh)
942 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
946 if (copy_from_user(&user_size,
947 useraddr + offsetof(struct ethtool_rxfh_indir, size),
951 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
952 &dev_size, sizeof(dev_size)))
955 /* If the user buffer size is 0, this is just a query for the
956 * device table size. Otherwise, if it's smaller than the
957 * device table size it's an error.
959 if (user_size < dev_size)
960 return user_size == 0 ? 0 : -EINVAL;
962 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
966 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
970 if (copy_to_user(useraddr +
971 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
972 indir, dev_size * sizeof(indir[0])))
980 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
981 void __user *useraddr)
983 struct ethtool_rxnfc rx_rings;
984 u32 user_size, dev_size, i;
986 const struct ethtool_ops *ops = dev->ethtool_ops;
988 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
990 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
994 dev_size = ops->get_rxfh_indir_size(dev);
998 if (copy_from_user(&user_size,
999 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1003 if (user_size != 0 && user_size != dev_size)
1006 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1010 rx_rings.cmd = ETHTOOL_GRXRINGS;
1011 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1015 if (user_size == 0) {
1016 for (i = 0; i < dev_size; i++)
1017 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1019 ret = ethtool_copy_validate_indir(indir,
1020 useraddr + ringidx_offset,
1027 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1031 /* indicate whether rxfh was set to default */
1033 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1035 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1042 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1043 void __user *useraddr)
1046 const struct ethtool_ops *ops = dev->ethtool_ops;
1047 u32 user_indir_size, user_key_size;
1048 u32 dev_indir_size = 0, dev_key_size = 0;
1049 struct ethtool_rxfh rxfh;
1060 if (ops->get_rxfh_indir_size)
1061 dev_indir_size = ops->get_rxfh_indir_size(dev);
1062 if (ops->get_rxfh_key_size)
1063 dev_key_size = ops->get_rxfh_key_size(dev);
1065 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1067 user_indir_size = rxfh.indir_size;
1068 user_key_size = rxfh.key_size;
1070 /* Check that reserved fields are 0 for now */
1071 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1073 /* Most drivers don't handle rss_context, check it's 0 as well */
1074 if (rxfh.rss_context && !ops->get_rxfh_context)
1077 rxfh.indir_size = dev_indir_size;
1078 rxfh.key_size = dev_key_size;
1079 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1082 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1083 (user_key_size && (user_key_size != dev_key_size)))
1086 indir_bytes = user_indir_size * sizeof(indir[0]);
1087 total_size = indir_bytes + user_key_size;
1088 rss_config = kzalloc(total_size, GFP_USER);
1092 if (user_indir_size)
1093 indir = (u32 *)rss_config;
1096 hkey = rss_config + indir_bytes;
1098 if (rxfh.rss_context)
1099 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1103 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1107 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1108 &dev_hfunc, sizeof(rxfh.hfunc))) {
1110 } else if (copy_to_user(useraddr +
1111 offsetof(struct ethtool_rxfh, rss_config[0]),
1112 rss_config, total_size)) {
1121 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1122 void __user *useraddr)
1125 const struct ethtool_ops *ops = dev->ethtool_ops;
1126 struct ethtool_rxnfc rx_rings;
1127 struct ethtool_rxfh rxfh;
1128 u32 dev_indir_size = 0, dev_key_size = 0, i;
1129 u32 *indir = NULL, indir_bytes = 0;
1132 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1133 bool delete = false;
1135 if (!ops->get_rxnfc || !ops->set_rxfh)
1138 if (ops->get_rxfh_indir_size)
1139 dev_indir_size = ops->get_rxfh_indir_size(dev);
1140 if (ops->get_rxfh_key_size)
1141 dev_key_size = ops->get_rxfh_key_size(dev);
1143 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1146 /* Check that reserved fields are 0 for now */
1147 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1149 /* Most drivers don't handle rss_context, check it's 0 as well */
1150 if (rxfh.rss_context && !ops->set_rxfh_context)
1153 /* If either indir, hash key or function is valid, proceed further.
1154 * Must request at least one change: indir size, hash key or function.
1156 if ((rxfh.indir_size &&
1157 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1158 rxfh.indir_size != dev_indir_size) ||
1159 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1160 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1161 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1164 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1165 indir_bytes = dev_indir_size * sizeof(indir[0]);
1167 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1171 rx_rings.cmd = ETHTOOL_GRXRINGS;
1172 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1176 /* rxfh.indir_size == 0 means reset the indir table to default (master
1177 * context) or delete the context (other RSS contexts).
1178 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1180 if (rxfh.indir_size &&
1181 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1182 indir = (u32 *)rss_config;
1183 ret = ethtool_copy_validate_indir(indir,
1184 useraddr + rss_cfg_offset,
1189 } else if (rxfh.indir_size == 0) {
1190 if (rxfh.rss_context == 0) {
1191 indir = (u32 *)rss_config;
1192 for (i = 0; i < dev_indir_size; i++)
1193 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1199 if (rxfh.key_size) {
1200 hkey = rss_config + indir_bytes;
1201 if (copy_from_user(hkey,
1202 useraddr + rss_cfg_offset + indir_bytes,
1209 if (rxfh.rss_context)
1210 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1211 &rxfh.rss_context, delete);
1213 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1217 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1218 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1221 if (!rxfh.rss_context) {
1222 /* indicate whether rxfh was set to default */
1223 if (rxfh.indir_size == 0)
1224 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1225 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1226 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1234 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1236 struct ethtool_regs regs;
1237 const struct ethtool_ops *ops = dev->ethtool_ops;
1241 if (!ops->get_regs || !ops->get_regs_len)
1244 if (copy_from_user(®s, useraddr, sizeof(regs)))
1247 reglen = ops->get_regs_len(dev);
1251 if (regs.len > reglen)
1254 regbuf = vzalloc(reglen);
1258 if (regs.len < reglen)
1261 ops->get_regs(dev, ®s, regbuf);
1264 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1266 useraddr += offsetof(struct ethtool_regs, data);
1267 if (copy_to_user(useraddr, regbuf, reglen))
1276 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1278 struct ethtool_value reset;
1281 if (!dev->ethtool_ops->reset)
1284 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1287 ret = dev->ethtool_ops->reset(dev, &reset.data);
1291 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1296 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1298 struct ethtool_wolinfo wol;
1300 if (!dev->ethtool_ops->get_wol)
1303 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1304 wol.cmd = ETHTOOL_GWOL;
1305 dev->ethtool_ops->get_wol(dev, &wol);
1307 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1312 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1314 struct ethtool_wolinfo wol;
1317 if (!dev->ethtool_ops->set_wol)
1320 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1323 ret = dev->ethtool_ops->set_wol(dev, &wol);
1327 dev->wol_enabled = !!wol.wolopts;
1328 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1333 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1335 struct ethtool_eee edata;
1338 if (!dev->ethtool_ops->get_eee)
1341 memset(&edata, 0, sizeof(struct ethtool_eee));
1342 edata.cmd = ETHTOOL_GEEE;
1343 rc = dev->ethtool_ops->get_eee(dev, &edata);
1348 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1354 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1356 struct ethtool_eee edata;
1359 if (!dev->ethtool_ops->set_eee)
1362 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1365 ret = dev->ethtool_ops->set_eee(dev, &edata);
1367 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1371 static int ethtool_nway_reset(struct net_device *dev)
1373 if (!dev->ethtool_ops->nway_reset)
1376 return dev->ethtool_ops->nway_reset(dev);
1379 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1381 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1382 int link = __ethtool_get_link(dev);
1388 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1393 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1394 int (*getter)(struct net_device *,
1395 struct ethtool_eeprom *, u8 *),
1398 struct ethtool_eeprom eeprom;
1399 void __user *userbuf = useraddr + sizeof(eeprom);
1400 u32 bytes_remaining;
1404 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1407 /* Check for wrap and zero */
1408 if (eeprom.offset + eeprom.len <= eeprom.offset)
1411 /* Check for exceeding total eeprom len */
1412 if (eeprom.offset + eeprom.len > total_len)
1415 data = kmalloc(PAGE_SIZE, GFP_USER);
1419 bytes_remaining = eeprom.len;
1420 while (bytes_remaining > 0) {
1421 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1423 ret = getter(dev, &eeprom, data);
1426 if (copy_to_user(userbuf, data, eeprom.len)) {
1430 userbuf += eeprom.len;
1431 eeprom.offset += eeprom.len;
1432 bytes_remaining -= eeprom.len;
1435 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1436 eeprom.offset -= eeprom.len;
1437 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1444 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1446 const struct ethtool_ops *ops = dev->ethtool_ops;
1448 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1449 !ops->get_eeprom_len(dev))
1452 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1453 ops->get_eeprom_len(dev));
1456 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1458 struct ethtool_eeprom eeprom;
1459 const struct ethtool_ops *ops = dev->ethtool_ops;
1460 void __user *userbuf = useraddr + sizeof(eeprom);
1461 u32 bytes_remaining;
1465 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1466 !ops->get_eeprom_len(dev))
1469 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1472 /* Check for wrap and zero */
1473 if (eeprom.offset + eeprom.len <= eeprom.offset)
1476 /* Check for exceeding total eeprom len */
1477 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1480 data = kmalloc(PAGE_SIZE, GFP_USER);
1484 bytes_remaining = eeprom.len;
1485 while (bytes_remaining > 0) {
1486 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1488 if (copy_from_user(data, userbuf, eeprom.len)) {
1492 ret = ops->set_eeprom(dev, &eeprom, data);
1495 userbuf += eeprom.len;
1496 eeprom.offset += eeprom.len;
1497 bytes_remaining -= eeprom.len;
1504 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1505 void __user *useraddr)
1507 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1509 if (!dev->ethtool_ops->get_coalesce)
1512 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1514 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1520 ethtool_set_coalesce_supported(struct net_device *dev,
1521 struct ethtool_coalesce *coalesce)
1523 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1524 u32 nonzero_params = 0;
1526 if (coalesce->rx_coalesce_usecs)
1527 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1528 if (coalesce->rx_max_coalesced_frames)
1529 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1530 if (coalesce->rx_coalesce_usecs_irq)
1531 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1532 if (coalesce->rx_max_coalesced_frames_irq)
1533 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1534 if (coalesce->tx_coalesce_usecs)
1535 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1536 if (coalesce->tx_max_coalesced_frames)
1537 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1538 if (coalesce->tx_coalesce_usecs_irq)
1539 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1540 if (coalesce->tx_max_coalesced_frames_irq)
1541 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1542 if (coalesce->stats_block_coalesce_usecs)
1543 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1544 if (coalesce->use_adaptive_rx_coalesce)
1545 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1546 if (coalesce->use_adaptive_tx_coalesce)
1547 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1548 if (coalesce->pkt_rate_low)
1549 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1550 if (coalesce->rx_coalesce_usecs_low)
1551 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1552 if (coalesce->rx_max_coalesced_frames_low)
1553 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1554 if (coalesce->tx_coalesce_usecs_low)
1555 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1556 if (coalesce->tx_max_coalesced_frames_low)
1557 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1558 if (coalesce->pkt_rate_high)
1559 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1560 if (coalesce->rx_coalesce_usecs_high)
1561 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1562 if (coalesce->rx_max_coalesced_frames_high)
1563 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1564 if (coalesce->tx_coalesce_usecs_high)
1565 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1566 if (coalesce->tx_max_coalesced_frames_high)
1567 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1568 if (coalesce->rate_sample_interval)
1569 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1571 return (supported_params & nonzero_params) == nonzero_params;
1574 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1575 void __user *useraddr)
1577 struct ethtool_coalesce coalesce;
1580 if (!dev->ethtool_ops->set_coalesce)
1583 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1586 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1589 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce);
1591 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1595 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1597 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1599 if (!dev->ethtool_ops->get_ringparam)
1602 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1604 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1609 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1611 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1614 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1617 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1620 dev->ethtool_ops->get_ringparam(dev, &max);
1622 /* ensure new ring parameters are within the maximums */
1623 if (ringparam.rx_pending > max.rx_max_pending ||
1624 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1625 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1626 ringparam.tx_pending > max.tx_max_pending)
1629 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1631 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1635 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1636 void __user *useraddr)
1638 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1640 if (!dev->ethtool_ops->get_channels)
1643 dev->ethtool_ops->get_channels(dev, &channels);
1645 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1650 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1651 void __user *useraddr)
1653 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1654 u16 from_channel, to_channel;
1655 u32 max_rx_in_use = 0;
1659 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1662 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1665 dev->ethtool_ops->get_channels(dev, &curr);
1667 /* ensure new counts are within the maximums */
1668 if (channels.rx_count > curr.max_rx ||
1669 channels.tx_count > curr.max_tx ||
1670 channels.combined_count > curr.max_combined ||
1671 channels.other_count > curr.max_other)
1674 /* ensure the new Rx count fits within the configured Rx flow
1675 * indirection table settings */
1676 if (netif_is_rxfh_configured(dev) &&
1677 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1678 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1681 /* Disabling channels, query zero-copy AF_XDP sockets */
1682 from_channel = channels.combined_count +
1683 min(channels.rx_count, channels.tx_count);
1684 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1685 for (i = from_channel; i < to_channel; i++)
1686 if (xdp_get_umem_from_qid(dev, i))
1689 ret = dev->ethtool_ops->set_channels(dev, &channels);
1691 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1695 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1697 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1699 if (!dev->ethtool_ops->get_pauseparam)
1702 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1704 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1709 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1711 struct ethtool_pauseparam pauseparam;
1714 if (!dev->ethtool_ops->set_pauseparam)
1717 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1720 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1722 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1726 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1728 struct ethtool_test test;
1729 const struct ethtool_ops *ops = dev->ethtool_ops;
1733 if (!ops->self_test || !ops->get_sset_count)
1736 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1739 WARN_ON(test_len == 0);
1741 if (copy_from_user(&test, useraddr, sizeof(test)))
1744 test.len = test_len;
1745 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1749 ops->self_test(dev, &test, data);
1752 if (copy_to_user(useraddr, &test, sizeof(test)))
1754 useraddr += sizeof(test);
1755 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1764 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1766 struct ethtool_gstrings gstrings;
1770 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1773 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1776 if (ret > S32_MAX / ETH_GSTRING_LEN)
1783 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1787 __ethtool_get_strings(dev, gstrings.string_set, data);
1793 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1795 useraddr += sizeof(gstrings);
1797 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1806 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1808 struct ethtool_value id;
1810 const struct ethtool_ops *ops = dev->ethtool_ops;
1813 if (!ops->set_phys_id)
1819 if (copy_from_user(&id, useraddr, sizeof(id)))
1822 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1826 /* Drop the RTNL lock while waiting, but prevent reentry or
1827 * removal of the device.
1834 /* Driver will handle this itself */
1835 schedule_timeout_interruptible(
1836 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1838 /* Driver expects to be called at twice the frequency in rc */
1839 int n = rc * 2, i, interval = HZ / n;
1841 /* Count down seconds */
1843 /* Count down iterations per second */
1847 rc = ops->set_phys_id(dev,
1848 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1852 schedule_timeout_interruptible(interval);
1853 } while (!signal_pending(current) && --i != 0);
1854 } while (!signal_pending(current) &&
1855 (id.data == 0 || --id.data != 0));
1862 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1866 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1868 struct ethtool_stats stats;
1869 const struct ethtool_ops *ops = dev->ethtool_ops;
1873 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1876 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1879 if (n_stats > S32_MAX / sizeof(u64))
1881 WARN_ON_ONCE(!n_stats);
1882 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1885 stats.n_stats = n_stats;
1888 data = vzalloc(array_size(n_stats, sizeof(u64)));
1891 ops->get_ethtool_stats(dev, &stats, data);
1897 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1899 useraddr += sizeof(stats);
1900 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1909 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1911 const struct ethtool_ops *ops = dev->ethtool_ops;
1912 struct phy_device *phydev = dev->phydev;
1913 struct ethtool_stats stats;
1917 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1920 if (dev->phydev && !ops->get_ethtool_phy_stats)
1921 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1923 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1926 if (n_stats > S32_MAX / sizeof(u64))
1928 WARN_ON_ONCE(!n_stats);
1930 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1933 stats.n_stats = n_stats;
1936 data = vzalloc(array_size(n_stats, sizeof(u64)));
1940 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1941 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1945 ops->get_ethtool_phy_stats(dev, &stats, data);
1952 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1954 useraddr += sizeof(stats);
1955 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1964 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1966 struct ethtool_perm_addr epaddr;
1968 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1971 if (epaddr.size < dev->addr_len)
1973 epaddr.size = dev->addr_len;
1975 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1977 useraddr += sizeof(epaddr);
1978 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1983 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1984 u32 cmd, u32 (*actor)(struct net_device *))
1986 struct ethtool_value edata = { .cmd = cmd };
1991 edata.data = actor(dev);
1993 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1998 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1999 void (*actor)(struct net_device *, u32))
2001 struct ethtool_value edata;
2006 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2009 actor(dev, edata.data);
2013 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2014 int (*actor)(struct net_device *, u32))
2016 struct ethtool_value edata;
2021 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2024 return actor(dev, edata.data);
2027 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2028 char __user *useraddr)
2030 struct ethtool_flash efl;
2032 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2034 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2036 if (!dev->ethtool_ops->flash_device)
2037 return devlink_compat_flash_update(dev, efl.data);
2039 return dev->ethtool_ops->flash_device(dev, &efl);
2042 static int ethtool_set_dump(struct net_device *dev,
2043 void __user *useraddr)
2045 struct ethtool_dump dump;
2047 if (!dev->ethtool_ops->set_dump)
2050 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2053 return dev->ethtool_ops->set_dump(dev, &dump);
2056 static int ethtool_get_dump_flag(struct net_device *dev,
2057 void __user *useraddr)
2060 struct ethtool_dump dump;
2061 const struct ethtool_ops *ops = dev->ethtool_ops;
2063 if (!ops->get_dump_flag)
2066 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2069 ret = ops->get_dump_flag(dev, &dump);
2073 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2078 static int ethtool_get_dump_data(struct net_device *dev,
2079 void __user *useraddr)
2083 struct ethtool_dump dump, tmp;
2084 const struct ethtool_ops *ops = dev->ethtool_ops;
2087 if (!ops->get_dump_data || !ops->get_dump_flag)
2090 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2093 memset(&tmp, 0, sizeof(tmp));
2094 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2095 ret = ops->get_dump_flag(dev, &tmp);
2099 len = min(tmp.len, dump.len);
2103 /* Don't ever let the driver think there's more space available
2104 * than it requested with .get_dump_flag().
2108 /* Always allocate enough space to hold the whole thing so that the
2109 * driver does not need to check the length and bother with partial
2112 data = vzalloc(tmp.len);
2115 ret = ops->get_dump_data(dev, &dump, data);
2119 /* There are two sane possibilities:
2120 * 1. The driver's .get_dump_data() does not touch dump.len.
2121 * 2. Or it may set dump.len to how much it really writes, which
2122 * should be tmp.len (or len if it can do a partial dump).
2123 * In any case respond to userspace with the actual length of data
2126 WARN_ON(dump.len != len && dump.len != tmp.len);
2129 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2133 useraddr += offsetof(struct ethtool_dump, data);
2134 if (copy_to_user(useraddr, data, len))
2141 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2143 struct ethtool_ts_info info;
2146 err = __ethtool_get_ts_info(dev, &info);
2150 if (copy_to_user(useraddr, &info, sizeof(info)))
2156 static int __ethtool_get_module_info(struct net_device *dev,
2157 struct ethtool_modinfo *modinfo)
2159 const struct ethtool_ops *ops = dev->ethtool_ops;
2160 struct phy_device *phydev = dev->phydev;
2163 return sfp_get_module_info(dev->sfp_bus, modinfo);
2165 if (phydev && phydev->drv && phydev->drv->module_info)
2166 return phydev->drv->module_info(phydev, modinfo);
2168 if (ops->get_module_info)
2169 return ops->get_module_info(dev, modinfo);
2174 static int ethtool_get_module_info(struct net_device *dev,
2175 void __user *useraddr)
2178 struct ethtool_modinfo modinfo;
2180 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2183 ret = __ethtool_get_module_info(dev, &modinfo);
2187 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2193 static int __ethtool_get_module_eeprom(struct net_device *dev,
2194 struct ethtool_eeprom *ee, u8 *data)
2196 const struct ethtool_ops *ops = dev->ethtool_ops;
2197 struct phy_device *phydev = dev->phydev;
2200 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2202 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2203 return phydev->drv->module_eeprom(phydev, ee, data);
2205 if (ops->get_module_eeprom)
2206 return ops->get_module_eeprom(dev, ee, data);
2211 static int ethtool_get_module_eeprom(struct net_device *dev,
2212 void __user *useraddr)
2215 struct ethtool_modinfo modinfo;
2217 ret = __ethtool_get_module_info(dev, &modinfo);
2221 return ethtool_get_any_eeprom(dev, useraddr,
2222 __ethtool_get_module_eeprom,
2223 modinfo.eeprom_len);
2226 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2229 case ETHTOOL_RX_COPYBREAK:
2230 case ETHTOOL_TX_COPYBREAK:
2231 if (tuna->len != sizeof(u32) ||
2232 tuna->type_id != ETHTOOL_TUNABLE_U32)
2235 case ETHTOOL_PFC_PREVENTION_TOUT:
2236 if (tuna->len != sizeof(u16) ||
2237 tuna->type_id != ETHTOOL_TUNABLE_U16)
2247 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2250 struct ethtool_tunable tuna;
2251 const struct ethtool_ops *ops = dev->ethtool_ops;
2254 if (!ops->get_tunable)
2256 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2258 ret = ethtool_tunable_valid(&tuna);
2261 data = kmalloc(tuna.len, GFP_USER);
2264 ret = ops->get_tunable(dev, &tuna, data);
2267 useraddr += sizeof(tuna);
2269 if (copy_to_user(useraddr, data, tuna.len))
2278 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2281 struct ethtool_tunable tuna;
2282 const struct ethtool_ops *ops = dev->ethtool_ops;
2285 if (!ops->set_tunable)
2287 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2289 ret = ethtool_tunable_valid(&tuna);
2292 useraddr += sizeof(tuna);
2293 data = memdup_user(useraddr, tuna.len);
2295 return PTR_ERR(data);
2296 ret = ops->set_tunable(dev, &tuna, data);
2302 static noinline_for_stack int
2303 ethtool_get_per_queue_coalesce(struct net_device *dev,
2304 void __user *useraddr,
2305 struct ethtool_per_queue_op *per_queue_opt)
2309 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2311 if (!dev->ethtool_ops->get_per_queue_coalesce)
2314 useraddr += sizeof(*per_queue_opt);
2316 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2319 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2320 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2322 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2325 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2327 useraddr += sizeof(coalesce);
2333 static noinline_for_stack int
2334 ethtool_set_per_queue_coalesce(struct net_device *dev,
2335 void __user *useraddr,
2336 struct ethtool_per_queue_op *per_queue_opt)
2341 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2342 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2344 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2345 (!dev->ethtool_ops->get_per_queue_coalesce))
2348 useraddr += sizeof(*per_queue_opt);
2350 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2351 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2352 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2356 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2357 struct ethtool_coalesce coalesce;
2359 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2365 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2370 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2375 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2379 useraddr += sizeof(coalesce);
2385 for_each_set_bit(i, queue_mask, bit) {
2386 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2395 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2396 void __user *useraddr, u32 sub_cmd)
2398 struct ethtool_per_queue_op per_queue_opt;
2400 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2403 if (per_queue_opt.sub_command != sub_cmd)
2406 switch (per_queue_opt.sub_command) {
2407 case ETHTOOL_GCOALESCE:
2408 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2409 case ETHTOOL_SCOALESCE:
2410 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2416 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2419 case ETHTOOL_PHY_DOWNSHIFT:
2420 case ETHTOOL_PHY_FAST_LINK_DOWN:
2421 if (tuna->len != sizeof(u8) ||
2422 tuna->type_id != ETHTOOL_TUNABLE_U8)
2425 case ETHTOOL_PHY_EDPD:
2426 if (tuna->len != sizeof(u16) ||
2427 tuna->type_id != ETHTOOL_TUNABLE_U16)
2437 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2440 struct ethtool_tunable tuna;
2441 struct phy_device *phydev = dev->phydev;
2444 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2447 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2449 ret = ethtool_phy_tunable_valid(&tuna);
2452 data = kmalloc(tuna.len, GFP_USER);
2455 mutex_lock(&phydev->lock);
2456 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2457 mutex_unlock(&phydev->lock);
2460 useraddr += sizeof(tuna);
2462 if (copy_to_user(useraddr, data, tuna.len))
2471 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2474 struct ethtool_tunable tuna;
2475 struct phy_device *phydev = dev->phydev;
2478 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2480 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2482 ret = ethtool_phy_tunable_valid(&tuna);
2485 useraddr += sizeof(tuna);
2486 data = memdup_user(useraddr, tuna.len);
2488 return PTR_ERR(data);
2489 mutex_lock(&phydev->lock);
2490 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2491 mutex_unlock(&phydev->lock);
2497 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2499 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2502 if (!dev->ethtool_ops->get_fecparam)
2505 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2509 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2514 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2516 struct ethtool_fecparam fecparam;
2518 if (!dev->ethtool_ops->set_fecparam)
2521 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2524 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2527 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2529 int dev_ethtool(struct net *net, struct ifreq *ifr)
2531 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2532 void __user *useraddr = ifr->ifr_data;
2533 u32 ethcmd, sub_cmd;
2535 netdev_features_t old_features;
2537 if (!dev || !netif_device_present(dev))
2540 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2543 if (ethcmd == ETHTOOL_PERQUEUE) {
2544 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2549 /* Allow some commands to be done by anyone */
2552 case ETHTOOL_GDRVINFO:
2553 case ETHTOOL_GMSGLVL:
2555 case ETHTOOL_GCOALESCE:
2556 case ETHTOOL_GRINGPARAM:
2557 case ETHTOOL_GPAUSEPARAM:
2558 case ETHTOOL_GRXCSUM:
2559 case ETHTOOL_GTXCSUM:
2561 case ETHTOOL_GSSET_INFO:
2562 case ETHTOOL_GSTRINGS:
2563 case ETHTOOL_GSTATS:
2564 case ETHTOOL_GPHYSTATS:
2566 case ETHTOOL_GPERMADDR:
2570 case ETHTOOL_GFLAGS:
2571 case ETHTOOL_GPFLAGS:
2573 case ETHTOOL_GRXRINGS:
2574 case ETHTOOL_GRXCLSRLCNT:
2575 case ETHTOOL_GRXCLSRULE:
2576 case ETHTOOL_GRXCLSRLALL:
2577 case ETHTOOL_GRXFHINDIR:
2579 case ETHTOOL_GFEATURES:
2580 case ETHTOOL_GCHANNELS:
2581 case ETHTOOL_GET_TS_INFO:
2583 case ETHTOOL_GTUNABLE:
2584 case ETHTOOL_PHY_GTUNABLE:
2585 case ETHTOOL_GLINKSETTINGS:
2586 case ETHTOOL_GFECPARAM:
2589 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2593 if (dev->ethtool_ops->begin) {
2594 rc = dev->ethtool_ops->begin(dev);
2598 old_features = dev->features;
2602 rc = ethtool_get_settings(dev, useraddr);
2605 rc = ethtool_set_settings(dev, useraddr);
2607 case ETHTOOL_GDRVINFO:
2608 rc = ethtool_get_drvinfo(dev, useraddr);
2611 rc = ethtool_get_regs(dev, useraddr);
2614 rc = ethtool_get_wol(dev, useraddr);
2617 rc = ethtool_set_wol(dev, useraddr);
2619 case ETHTOOL_GMSGLVL:
2620 rc = ethtool_get_value(dev, useraddr, ethcmd,
2621 dev->ethtool_ops->get_msglevel);
2623 case ETHTOOL_SMSGLVL:
2624 rc = ethtool_set_value_void(dev, useraddr,
2625 dev->ethtool_ops->set_msglevel);
2627 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2630 rc = ethtool_get_eee(dev, useraddr);
2633 rc = ethtool_set_eee(dev, useraddr);
2635 case ETHTOOL_NWAY_RST:
2636 rc = ethtool_nway_reset(dev);
2639 rc = ethtool_get_link(dev, useraddr);
2641 case ETHTOOL_GEEPROM:
2642 rc = ethtool_get_eeprom(dev, useraddr);
2644 case ETHTOOL_SEEPROM:
2645 rc = ethtool_set_eeprom(dev, useraddr);
2647 case ETHTOOL_GCOALESCE:
2648 rc = ethtool_get_coalesce(dev, useraddr);
2650 case ETHTOOL_SCOALESCE:
2651 rc = ethtool_set_coalesce(dev, useraddr);
2653 case ETHTOOL_GRINGPARAM:
2654 rc = ethtool_get_ringparam(dev, useraddr);
2656 case ETHTOOL_SRINGPARAM:
2657 rc = ethtool_set_ringparam(dev, useraddr);
2659 case ETHTOOL_GPAUSEPARAM:
2660 rc = ethtool_get_pauseparam(dev, useraddr);
2662 case ETHTOOL_SPAUSEPARAM:
2663 rc = ethtool_set_pauseparam(dev, useraddr);
2666 rc = ethtool_self_test(dev, useraddr);
2668 case ETHTOOL_GSTRINGS:
2669 rc = ethtool_get_strings(dev, useraddr);
2671 case ETHTOOL_PHYS_ID:
2672 rc = ethtool_phys_id(dev, useraddr);
2674 case ETHTOOL_GSTATS:
2675 rc = ethtool_get_stats(dev, useraddr);
2677 case ETHTOOL_GPERMADDR:
2678 rc = ethtool_get_perm_addr(dev, useraddr);
2680 case ETHTOOL_GFLAGS:
2681 rc = ethtool_get_value(dev, useraddr, ethcmd,
2682 __ethtool_get_flags);
2684 case ETHTOOL_SFLAGS:
2685 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2687 case ETHTOOL_GPFLAGS:
2688 rc = ethtool_get_value(dev, useraddr, ethcmd,
2689 dev->ethtool_ops->get_priv_flags);
2691 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2693 case ETHTOOL_SPFLAGS:
2694 rc = ethtool_set_value(dev, useraddr,
2695 dev->ethtool_ops->set_priv_flags);
2698 case ETHTOOL_GRXRINGS:
2699 case ETHTOOL_GRXCLSRLCNT:
2700 case ETHTOOL_GRXCLSRULE:
2701 case ETHTOOL_GRXCLSRLALL:
2702 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2705 case ETHTOOL_SRXCLSRLDEL:
2706 case ETHTOOL_SRXCLSRLINS:
2707 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2709 case ETHTOOL_FLASHDEV:
2710 rc = ethtool_flash_device(dev, useraddr);
2713 rc = ethtool_reset(dev, useraddr);
2715 case ETHTOOL_GSSET_INFO:
2716 rc = ethtool_get_sset_info(dev, useraddr);
2718 case ETHTOOL_GRXFHINDIR:
2719 rc = ethtool_get_rxfh_indir(dev, useraddr);
2721 case ETHTOOL_SRXFHINDIR:
2722 rc = ethtool_set_rxfh_indir(dev, useraddr);
2725 rc = ethtool_get_rxfh(dev, useraddr);
2728 rc = ethtool_set_rxfh(dev, useraddr);
2730 case ETHTOOL_GFEATURES:
2731 rc = ethtool_get_features(dev, useraddr);
2733 case ETHTOOL_SFEATURES:
2734 rc = ethtool_set_features(dev, useraddr);
2736 case ETHTOOL_GTXCSUM:
2737 case ETHTOOL_GRXCSUM:
2742 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2744 case ETHTOOL_STXCSUM:
2745 case ETHTOOL_SRXCSUM:
2750 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2752 case ETHTOOL_GCHANNELS:
2753 rc = ethtool_get_channels(dev, useraddr);
2755 case ETHTOOL_SCHANNELS:
2756 rc = ethtool_set_channels(dev, useraddr);
2758 case ETHTOOL_SET_DUMP:
2759 rc = ethtool_set_dump(dev, useraddr);
2761 case ETHTOOL_GET_DUMP_FLAG:
2762 rc = ethtool_get_dump_flag(dev, useraddr);
2764 case ETHTOOL_GET_DUMP_DATA:
2765 rc = ethtool_get_dump_data(dev, useraddr);
2767 case ETHTOOL_GET_TS_INFO:
2768 rc = ethtool_get_ts_info(dev, useraddr);
2770 case ETHTOOL_GMODULEINFO:
2771 rc = ethtool_get_module_info(dev, useraddr);
2773 case ETHTOOL_GMODULEEEPROM:
2774 rc = ethtool_get_module_eeprom(dev, useraddr);
2776 case ETHTOOL_GTUNABLE:
2777 rc = ethtool_get_tunable(dev, useraddr);
2779 case ETHTOOL_STUNABLE:
2780 rc = ethtool_set_tunable(dev, useraddr);
2782 case ETHTOOL_GPHYSTATS:
2783 rc = ethtool_get_phy_stats(dev, useraddr);
2785 case ETHTOOL_PERQUEUE:
2786 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2788 case ETHTOOL_GLINKSETTINGS:
2789 rc = ethtool_get_link_ksettings(dev, useraddr);
2791 case ETHTOOL_SLINKSETTINGS:
2792 rc = ethtool_set_link_ksettings(dev, useraddr);
2794 case ETHTOOL_PHY_GTUNABLE:
2795 rc = get_phy_tunable(dev, useraddr);
2797 case ETHTOOL_PHY_STUNABLE:
2798 rc = set_phy_tunable(dev, useraddr);
2800 case ETHTOOL_GFECPARAM:
2801 rc = ethtool_get_fecparam(dev, useraddr);
2803 case ETHTOOL_SFECPARAM:
2804 rc = ethtool_set_fecparam(dev, useraddr);
2810 if (dev->ethtool_ops->complete)
2811 dev->ethtool_ops->complete(dev);
2813 if (old_features != dev->features)
2814 netdev_features_change(dev);
2819 struct ethtool_rx_flow_key {
2820 struct flow_dissector_key_basic basic;
2822 struct flow_dissector_key_ipv4_addrs ipv4;
2823 struct flow_dissector_key_ipv6_addrs ipv6;
2825 struct flow_dissector_key_ports tp;
2826 struct flow_dissector_key_ip ip;
2827 struct flow_dissector_key_vlan vlan;
2828 struct flow_dissector_key_eth_addrs eth_addrs;
2829 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2831 struct ethtool_rx_flow_match {
2832 struct flow_dissector dissector;
2833 struct ethtool_rx_flow_key key;
2834 struct ethtool_rx_flow_key mask;
2837 struct ethtool_rx_flow_rule *
2838 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2840 const struct ethtool_rx_flow_spec *fs = input->fs;
2841 static struct in6_addr zero_addr = {};
2842 struct ethtool_rx_flow_match *match;
2843 struct ethtool_rx_flow_rule *flow;
2844 struct flow_action_entry *act;
2846 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2847 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2849 return ERR_PTR(-ENOMEM);
2851 /* ethtool_rx supports only one single action per rule. */
2852 flow->rule = flow_rule_alloc(1);
2855 return ERR_PTR(-ENOMEM);
2858 match = (struct ethtool_rx_flow_match *)flow->priv;
2859 flow->rule->match.dissector = &match->dissector;
2860 flow->rule->match.mask = &match->mask;
2861 flow->rule->match.key = &match->key;
2863 match->mask.basic.n_proto = htons(0xffff);
2865 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2867 const struct ethhdr *ether_spec, *ether_m_spec;
2869 ether_spec = &fs->h_u.ether_spec;
2870 ether_m_spec = &fs->m_u.ether_spec;
2872 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
2873 ether_addr_copy(match->key.eth_addrs.src,
2874 ether_spec->h_source);
2875 ether_addr_copy(match->mask.eth_addrs.src,
2876 ether_m_spec->h_source);
2878 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
2879 ether_addr_copy(match->key.eth_addrs.dst,
2880 ether_spec->h_dest);
2881 ether_addr_copy(match->mask.eth_addrs.dst,
2882 ether_m_spec->h_dest);
2884 if (ether_m_spec->h_proto) {
2885 match->key.basic.n_proto = ether_spec->h_proto;
2886 match->mask.basic.n_proto = ether_m_spec->h_proto;
2892 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2894 match->key.basic.n_proto = htons(ETH_P_IP);
2896 v4_spec = &fs->h_u.tcp_ip4_spec;
2897 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2899 if (v4_m_spec->ip4src) {
2900 match->key.ipv4.src = v4_spec->ip4src;
2901 match->mask.ipv4.src = v4_m_spec->ip4src;
2903 if (v4_m_spec->ip4dst) {
2904 match->key.ipv4.dst = v4_spec->ip4dst;
2905 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2907 if (v4_m_spec->ip4src ||
2908 v4_m_spec->ip4dst) {
2909 match->dissector.used_keys |=
2910 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2911 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2912 offsetof(struct ethtool_rx_flow_key, ipv4);
2914 if (v4_m_spec->psrc) {
2915 match->key.tp.src = v4_spec->psrc;
2916 match->mask.tp.src = v4_m_spec->psrc;
2918 if (v4_m_spec->pdst) {
2919 match->key.tp.dst = v4_spec->pdst;
2920 match->mask.tp.dst = v4_m_spec->pdst;
2922 if (v4_m_spec->psrc ||
2924 match->dissector.used_keys |=
2925 BIT(FLOW_DISSECTOR_KEY_PORTS);
2926 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2927 offsetof(struct ethtool_rx_flow_key, tp);
2929 if (v4_m_spec->tos) {
2930 match->key.ip.tos = v4_spec->tos;
2931 match->mask.ip.tos = v4_m_spec->tos;
2932 match->dissector.used_keys |=
2933 BIT(FLOW_DISSECTOR_KEY_IP);
2934 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2935 offsetof(struct ethtool_rx_flow_key, ip);
2941 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2943 match->key.basic.n_proto = htons(ETH_P_IPV6);
2945 v6_spec = &fs->h_u.tcp_ip6_spec;
2946 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2947 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2948 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2949 sizeof(match->key.ipv6.src));
2950 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2951 sizeof(match->mask.ipv6.src));
2953 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2954 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2955 sizeof(match->key.ipv6.dst));
2956 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2957 sizeof(match->mask.ipv6.dst));
2959 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2960 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2961 match->dissector.used_keys |=
2962 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2963 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2964 offsetof(struct ethtool_rx_flow_key, ipv6);
2966 if (v6_m_spec->psrc) {
2967 match->key.tp.src = v6_spec->psrc;
2968 match->mask.tp.src = v6_m_spec->psrc;
2970 if (v6_m_spec->pdst) {
2971 match->key.tp.dst = v6_spec->pdst;
2972 match->mask.tp.dst = v6_m_spec->pdst;
2974 if (v6_m_spec->psrc ||
2976 match->dissector.used_keys |=
2977 BIT(FLOW_DISSECTOR_KEY_PORTS);
2978 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2979 offsetof(struct ethtool_rx_flow_key, tp);
2981 if (v6_m_spec->tclass) {
2982 match->key.ip.tos = v6_spec->tclass;
2983 match->mask.ip.tos = v6_m_spec->tclass;
2984 match->dissector.used_keys |=
2985 BIT(FLOW_DISSECTOR_KEY_IP);
2986 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2987 offsetof(struct ethtool_rx_flow_key, ip);
2992 ethtool_rx_flow_rule_destroy(flow);
2993 return ERR_PTR(-EINVAL);
2996 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2999 match->key.basic.ip_proto = IPPROTO_TCP;
3003 match->key.basic.ip_proto = IPPROTO_UDP;
3006 match->mask.basic.ip_proto = 0xff;
3008 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3009 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3010 offsetof(struct ethtool_rx_flow_key, basic);
3012 if (fs->flow_type & FLOW_EXT) {
3013 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3014 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3016 if (ext_m_spec->vlan_etype) {
3017 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3018 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3021 if (ext_m_spec->vlan_tci) {
3022 match->key.vlan.vlan_id =
3023 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3024 match->mask.vlan.vlan_id =
3025 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3027 match->key.vlan.vlan_dei =
3028 !!(ext_h_spec->vlan_tci & htons(0x1000));
3029 match->mask.vlan.vlan_dei =
3030 !!(ext_m_spec->vlan_tci & htons(0x1000));
3032 match->key.vlan.vlan_priority =
3033 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3034 match->mask.vlan.vlan_priority =
3035 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3038 if (ext_m_spec->vlan_etype ||
3039 ext_m_spec->vlan_tci) {
3040 match->dissector.used_keys |=
3041 BIT(FLOW_DISSECTOR_KEY_VLAN);
3042 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3043 offsetof(struct ethtool_rx_flow_key, vlan);
3046 if (fs->flow_type & FLOW_MAC_EXT) {
3047 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3048 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3050 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3052 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3055 match->dissector.used_keys |=
3056 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3057 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3058 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3061 act = &flow->rule->action.entries[0];
3062 switch (fs->ring_cookie) {
3063 case RX_CLS_FLOW_DISC:
3064 act->id = FLOW_ACTION_DROP;
3066 case RX_CLS_FLOW_WAKE:
3067 act->id = FLOW_ACTION_WAKE;
3070 act->id = FLOW_ACTION_QUEUE;
3071 if (fs->flow_type & FLOW_RSS)
3072 act->queue.ctx = input->rss_ctx;
3074 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3075 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3081 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3083 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3088 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);