1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/ethtool.c - Ethtool ioctl handler
6 * This file is where we call all the ethtool_ops commands to get
7 * the information ethtool needs.
10 #include <linux/compat.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/ethtool.h>
16 #include <linux/netdevice.h>
17 #include <linux/net_tstamp.h>
18 #include <linux/phy.h>
19 #include <linux/bitops.h>
20 #include <linux/uaccess.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 <linux/pm_runtime.h>
28 #include <net/devlink.h>
29 #include <net/xdp_sock_drv.h>
30 #include <net/flow_offload.h>
31 #include <linux/ethtool_netlink.h>
32 #include <generated/utsrelease.h>
35 /* State held across locks and calls for commands which have devlink fallback */
36 struct ethtool_devlink_compat {
37 struct devlink *devlink;
39 struct ethtool_flash efl;
40 struct ethtool_drvinfo info;
44 static struct devlink *netdev_to_devlink_get(struct net_device *dev)
46 struct devlink_port *devlink_port;
48 if (!dev->netdev_ops->ndo_get_devlink_port)
51 devlink_port = dev->netdev_ops->ndo_get_devlink_port(dev);
55 return devlink_try_get(devlink_port->devlink);
59 * Some useful ethtool_ops methods that're device independent.
60 * If we find that all drivers want to do the same thing here,
61 * we can turn these into dev_() function calls.
64 u32 ethtool_op_get_link(struct net_device *dev)
66 return netif_carrier_ok(dev) ? 1 : 0;
68 EXPORT_SYMBOL(ethtool_op_get_link);
70 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
72 info->so_timestamping =
73 SOF_TIMESTAMPING_TX_SOFTWARE |
74 SOF_TIMESTAMPING_RX_SOFTWARE |
75 SOF_TIMESTAMPING_SOFTWARE;
79 EXPORT_SYMBOL(ethtool_op_get_ts_info);
81 /* Handlers for each ethtool command */
83 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
85 struct ethtool_gfeatures cmd = {
86 .cmd = ETHTOOL_GFEATURES,
87 .size = ETHTOOL_DEV_FEATURE_WORDS,
89 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
94 /* in case feature bits run out again */
95 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
97 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
98 features[i].available = (u32)(dev->hw_features >> (32 * i));
99 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
100 features[i].active = (u32)(dev->features >> (32 * i));
101 features[i].never_changed =
102 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
105 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
106 if (get_user(copy_size, sizeaddr))
109 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
110 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
112 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
114 useraddr += sizeof(cmd);
115 if (copy_to_user(useraddr, features,
116 array_size(copy_size, sizeof(*features))))
122 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
124 struct ethtool_sfeatures cmd;
125 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
126 netdev_features_t wanted = 0, valid = 0;
129 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
131 useraddr += sizeof(cmd);
133 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
136 if (copy_from_user(features, useraddr, sizeof(features)))
139 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
140 valid |= (netdev_features_t)features[i].valid << (32 * i);
141 wanted |= (netdev_features_t)features[i].requested << (32 * i);
144 if (valid & ~NETIF_F_ETHTOOL_BITS)
147 if (valid & ~dev->hw_features) {
148 valid &= dev->hw_features;
149 ret |= ETHTOOL_F_UNSUPPORTED;
152 dev->wanted_features &= ~valid;
153 dev->wanted_features |= wanted & valid;
154 __netdev_update_features(dev);
156 if ((dev->wanted_features ^ dev->features) & valid)
157 ret |= ETHTOOL_F_WISH;
162 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
164 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
165 const struct ethtool_ops *ops = dev->ethtool_ops;
167 if (sset == ETH_SS_FEATURES)
168 return ARRAY_SIZE(netdev_features_strings);
170 if (sset == ETH_SS_RSS_HASH_FUNCS)
171 return ARRAY_SIZE(rss_hash_func_strings);
173 if (sset == ETH_SS_TUNABLES)
174 return ARRAY_SIZE(tunable_strings);
176 if (sset == ETH_SS_PHY_TUNABLES)
177 return ARRAY_SIZE(phy_tunable_strings);
179 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
180 !ops->get_ethtool_phy_stats &&
181 phy_ops && phy_ops->get_sset_count)
182 return phy_ops->get_sset_count(dev->phydev);
184 if (sset == ETH_SS_LINK_MODES)
185 return __ETHTOOL_LINK_MODE_MASK_NBITS;
187 if (ops->get_sset_count && ops->get_strings)
188 return ops->get_sset_count(dev, sset);
193 static void __ethtool_get_strings(struct net_device *dev,
194 u32 stringset, u8 *data)
196 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
197 const struct ethtool_ops *ops = dev->ethtool_ops;
199 if (stringset == ETH_SS_FEATURES)
200 memcpy(data, netdev_features_strings,
201 sizeof(netdev_features_strings));
202 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
203 memcpy(data, rss_hash_func_strings,
204 sizeof(rss_hash_func_strings));
205 else if (stringset == ETH_SS_TUNABLES)
206 memcpy(data, tunable_strings, sizeof(tunable_strings));
207 else if (stringset == ETH_SS_PHY_TUNABLES)
208 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
209 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
210 !ops->get_ethtool_phy_stats && phy_ops &&
211 phy_ops->get_strings)
212 phy_ops->get_strings(dev->phydev, data);
213 else if (stringset == ETH_SS_LINK_MODES)
214 memcpy(data, link_mode_names,
215 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
217 /* ops->get_strings is valid because checked earlier */
218 ops->get_strings(dev, stringset, data);
221 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
223 /* feature masks of legacy discrete ethtool ops */
226 case ETHTOOL_GTXCSUM:
227 case ETHTOOL_STXCSUM:
228 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
230 case ETHTOOL_GRXCSUM:
231 case ETHTOOL_SRXCSUM:
232 return NETIF_F_RXCSUM;
235 return NETIF_F_SG | NETIF_F_FRAGLIST;
238 return NETIF_F_ALL_TSO;
250 static int ethtool_get_one_feature(struct net_device *dev,
251 char __user *useraddr, u32 ethcmd)
253 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
254 struct ethtool_value edata = {
256 .data = !!(dev->features & mask),
259 if (copy_to_user(useraddr, &edata, sizeof(edata)))
264 static int ethtool_set_one_feature(struct net_device *dev,
265 void __user *useraddr, u32 ethcmd)
267 struct ethtool_value edata;
268 netdev_features_t mask;
270 if (copy_from_user(&edata, useraddr, sizeof(edata)))
273 mask = ethtool_get_feature_mask(ethcmd);
274 mask &= dev->hw_features;
279 dev->wanted_features |= mask;
281 dev->wanted_features &= ~mask;
283 __netdev_update_features(dev);
288 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
289 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
290 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
291 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
294 static u32 __ethtool_get_flags(struct net_device *dev)
298 if (dev->features & NETIF_F_LRO)
299 flags |= ETH_FLAG_LRO;
300 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
301 flags |= ETH_FLAG_RXVLAN;
302 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
303 flags |= ETH_FLAG_TXVLAN;
304 if (dev->features & NETIF_F_NTUPLE)
305 flags |= ETH_FLAG_NTUPLE;
306 if (dev->features & NETIF_F_RXHASH)
307 flags |= ETH_FLAG_RXHASH;
312 static int __ethtool_set_flags(struct net_device *dev, u32 data)
314 netdev_features_t features = 0, changed;
316 if (data & ~ETH_ALL_FLAGS)
319 if (data & ETH_FLAG_LRO)
320 features |= NETIF_F_LRO;
321 if (data & ETH_FLAG_RXVLAN)
322 features |= NETIF_F_HW_VLAN_CTAG_RX;
323 if (data & ETH_FLAG_TXVLAN)
324 features |= NETIF_F_HW_VLAN_CTAG_TX;
325 if (data & ETH_FLAG_NTUPLE)
326 features |= NETIF_F_NTUPLE;
327 if (data & ETH_FLAG_RXHASH)
328 features |= NETIF_F_RXHASH;
330 /* allow changing only bits set in hw_features */
331 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
332 if (changed & ~dev->hw_features)
333 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
335 dev->wanted_features =
336 (dev->wanted_features & ~changed) | (features & changed);
338 __netdev_update_features(dev);
343 /* Given two link masks, AND them together and save the result in dst. */
344 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
345 struct ethtool_link_ksettings *src)
347 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
348 unsigned int idx = 0;
350 for (; idx < size; idx++) {
351 dst->link_modes.supported[idx] &=
352 src->link_modes.supported[idx];
353 dst->link_modes.advertising[idx] &=
354 src->link_modes.advertising[idx];
357 EXPORT_SYMBOL(ethtool_intersect_link_masks);
359 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
365 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
367 /* return false if src had higher bits set. lower bits always updated. */
368 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
369 const unsigned long *src)
373 /* TODO: following test will soon always be true */
374 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
375 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
378 bitmap_fill(ext, 32);
379 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
380 if (linkmode_intersects(ext, src)) {
381 /* src mask goes beyond bit 31 */
385 *legacy_u32 = src[0];
388 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
390 /* return false if ksettings link modes had higher bits
391 * set. legacy_settings always updated (best effort)
394 convert_link_ksettings_to_legacy_settings(
395 struct ethtool_cmd *legacy_settings,
396 const struct ethtool_link_ksettings *link_ksettings)
400 memset(legacy_settings, 0, sizeof(*legacy_settings));
401 /* this also clears the deprecated fields in legacy structure:
407 retval &= ethtool_convert_link_mode_to_legacy_u32(
408 &legacy_settings->supported,
409 link_ksettings->link_modes.supported);
410 retval &= ethtool_convert_link_mode_to_legacy_u32(
411 &legacy_settings->advertising,
412 link_ksettings->link_modes.advertising);
413 retval &= ethtool_convert_link_mode_to_legacy_u32(
414 &legacy_settings->lp_advertising,
415 link_ksettings->link_modes.lp_advertising);
416 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
417 legacy_settings->duplex
418 = link_ksettings->base.duplex;
419 legacy_settings->port
420 = link_ksettings->base.port;
421 legacy_settings->phy_address
422 = link_ksettings->base.phy_address;
423 legacy_settings->autoneg
424 = link_ksettings->base.autoneg;
425 legacy_settings->mdio_support
426 = link_ksettings->base.mdio_support;
427 legacy_settings->eth_tp_mdix
428 = link_ksettings->base.eth_tp_mdix;
429 legacy_settings->eth_tp_mdix_ctrl
430 = link_ksettings->base.eth_tp_mdix_ctrl;
431 legacy_settings->transceiver
432 = link_ksettings->base.transceiver;
436 /* number of 32-bit words to store the user's link mode bitmaps */
437 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
438 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
440 /* layout of the struct passed from/to userland */
441 struct ethtool_link_usettings {
442 struct ethtool_link_settings base;
444 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
445 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
446 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
450 /* Internal kernel helper to query a device ethtool_link_settings. */
451 int __ethtool_get_link_ksettings(struct net_device *dev,
452 struct ethtool_link_ksettings *link_ksettings)
456 if (!dev->ethtool_ops->get_link_ksettings)
459 memset(link_ksettings, 0, sizeof(*link_ksettings));
460 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
462 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
464 /* convert ethtool_link_usettings in user space to a kernel internal
465 * ethtool_link_ksettings. return 0 on success, errno on error.
467 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
468 const void __user *from)
470 struct ethtool_link_usettings link_usettings;
472 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
475 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
476 bitmap_from_arr32(to->link_modes.supported,
477 link_usettings.link_modes.supported,
478 __ETHTOOL_LINK_MODE_MASK_NBITS);
479 bitmap_from_arr32(to->link_modes.advertising,
480 link_usettings.link_modes.advertising,
481 __ETHTOOL_LINK_MODE_MASK_NBITS);
482 bitmap_from_arr32(to->link_modes.lp_advertising,
483 link_usettings.link_modes.lp_advertising,
484 __ETHTOOL_LINK_MODE_MASK_NBITS);
489 /* Check if the user is trying to change anything besides speed/duplex */
490 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
492 struct ethtool_link_settings base2 = {};
494 base2.speed = cmd->base.speed;
495 base2.port = PORT_OTHER;
496 base2.duplex = cmd->base.duplex;
497 base2.cmd = cmd->base.cmd;
498 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
500 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
501 bitmap_empty(cmd->link_modes.supported,
502 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
503 bitmap_empty(cmd->link_modes.lp_advertising,
504 __ETHTOOL_LINK_MODE_MASK_NBITS);
507 /* convert a kernel internal ethtool_link_ksettings to
508 * ethtool_link_usettings in user space. return 0 on success, errno on
512 store_link_ksettings_for_user(void __user *to,
513 const struct ethtool_link_ksettings *from)
515 struct ethtool_link_usettings link_usettings;
517 memcpy(&link_usettings, from, sizeof(link_usettings));
518 bitmap_to_arr32(link_usettings.link_modes.supported,
519 from->link_modes.supported,
520 __ETHTOOL_LINK_MODE_MASK_NBITS);
521 bitmap_to_arr32(link_usettings.link_modes.advertising,
522 from->link_modes.advertising,
523 __ETHTOOL_LINK_MODE_MASK_NBITS);
524 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
525 from->link_modes.lp_advertising,
526 __ETHTOOL_LINK_MODE_MASK_NBITS);
528 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
534 /* Query device for its ethtool_link_settings. */
535 static int ethtool_get_link_ksettings(struct net_device *dev,
536 void __user *useraddr)
539 struct ethtool_link_ksettings link_ksettings;
542 if (!dev->ethtool_ops->get_link_ksettings)
545 /* handle bitmap nbits handshake */
546 if (copy_from_user(&link_ksettings.base, useraddr,
547 sizeof(link_ksettings.base)))
550 if (__ETHTOOL_LINK_MODE_MASK_NU32
551 != link_ksettings.base.link_mode_masks_nwords) {
552 /* wrong link mode nbits requested */
553 memset(&link_ksettings, 0, sizeof(link_ksettings));
554 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
555 /* send back number of words required as negative val */
556 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
557 "need too many bits for link modes!");
558 link_ksettings.base.link_mode_masks_nwords
559 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
561 /* copy the base fields back to user, not the link
564 if (copy_to_user(useraddr, &link_ksettings.base,
565 sizeof(link_ksettings.base)))
571 /* handshake successful: user/kernel agree on
572 * link_mode_masks_nwords
575 memset(&link_ksettings, 0, sizeof(link_ksettings));
576 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
580 /* make sure we tell the right values to user */
581 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
582 link_ksettings.base.link_mode_masks_nwords
583 = __ETHTOOL_LINK_MODE_MASK_NU32;
584 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
585 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
587 return store_link_ksettings_for_user(useraddr, &link_ksettings);
590 /* Update device ethtool_link_settings. */
591 static int ethtool_set_link_ksettings(struct net_device *dev,
592 void __user *useraddr)
595 struct ethtool_link_ksettings link_ksettings;
599 if (!dev->ethtool_ops->set_link_ksettings)
602 /* make sure nbits field has expected value */
603 if (copy_from_user(&link_ksettings.base, useraddr,
604 sizeof(link_ksettings.base)))
607 if (__ETHTOOL_LINK_MODE_MASK_NU32
608 != link_ksettings.base.link_mode_masks_nwords)
611 /* copy the whole structure, now that we know it has expected
614 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
618 /* re-check nwords field, just in case */
619 if (__ETHTOOL_LINK_MODE_MASK_NU32
620 != link_ksettings.base.link_mode_masks_nwords)
623 if (link_ksettings.base.master_slave_cfg ||
624 link_ksettings.base.master_slave_state)
627 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
629 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
630 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
635 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
636 const struct ethtool_link_ksettings *cmd,
637 u32 *dev_speed, u8 *dev_duplex)
642 speed = cmd->base.speed;
643 duplex = cmd->base.duplex;
644 /* don't allow custom speed and duplex */
645 if (!ethtool_validate_speed(speed) ||
646 !ethtool_validate_duplex(duplex) ||
647 !ethtool_virtdev_validate_cmd(cmd))
650 *dev_duplex = duplex;
654 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
656 /* Query device for its ethtool_cmd settings.
658 * Backward compatibility note: for compatibility with legacy ethtool, this is
659 * now implemented via get_link_ksettings. When driver reports higher link mode
660 * bits, a kernel warning is logged once (with name of 1st driver/device) to
661 * recommend user to upgrade ethtool, but the command is successful (only the
662 * lower link mode bits reported back to user). Deprecated fields from
663 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
665 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
667 struct ethtool_link_ksettings link_ksettings;
668 struct ethtool_cmd cmd;
672 if (!dev->ethtool_ops->get_link_ksettings)
675 memset(&link_ksettings, 0, sizeof(link_ksettings));
676 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
679 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
681 /* send a sensible cmd tag back to user */
682 cmd.cmd = ETHTOOL_GSET;
684 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
690 /* Update device link settings with given ethtool_cmd.
692 * Backward compatibility note: for compatibility with legacy ethtool, this is
693 * now always implemented via set_link_settings. When user's request updates
694 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
695 * warning is logged once (with name of 1st driver/device) to recommend user to
696 * upgrade ethtool, and the request is rejected.
698 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
700 struct ethtool_link_ksettings link_ksettings;
701 struct ethtool_cmd cmd;
706 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
708 if (!dev->ethtool_ops->set_link_ksettings)
711 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
713 link_ksettings.base.link_mode_masks_nwords =
714 __ETHTOOL_LINK_MODE_MASK_NU32;
715 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
717 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
718 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
724 ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
726 const struct ethtool_ops *ops = dev->ethtool_ops;
728 rsp->info.cmd = ETHTOOL_GDRVINFO;
729 strlcpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
730 if (ops->get_drvinfo) {
731 ops->get_drvinfo(dev, &rsp->info);
732 } else if (dev->dev.parent && dev->dev.parent->driver) {
733 strlcpy(rsp->info.bus_info, dev_name(dev->dev.parent),
734 sizeof(rsp->info.bus_info));
735 strlcpy(rsp->info.driver, dev->dev.parent->driver->name,
736 sizeof(rsp->info.driver));
742 * this method of obtaining string set info is deprecated;
743 * Use ETHTOOL_GSSET_INFO instead.
745 if (ops->get_sset_count) {
748 rc = ops->get_sset_count(dev, ETH_SS_TEST);
750 rsp->info.testinfo_len = rc;
751 rc = ops->get_sset_count(dev, ETH_SS_STATS);
753 rsp->info.n_stats = rc;
754 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
756 rsp->info.n_priv_flags = rc;
758 if (ops->get_regs_len) {
759 int ret = ops->get_regs_len(dev);
762 rsp->info.regdump_len = ret;
765 if (ops->get_eeprom_len)
766 rsp->info.eedump_len = ops->get_eeprom_len(dev);
768 if (!rsp->info.fw_version[0])
769 rsp->devlink = netdev_to_devlink_get(dev);
774 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
775 void __user *useraddr)
777 struct ethtool_sset_info info;
779 int i, idx = 0, n_bits = 0, ret, rc;
780 u32 *info_buf = NULL;
782 if (copy_from_user(&info, useraddr, sizeof(info)))
785 /* store copy of mask, because we zero struct later on */
786 sset_mask = info.sset_mask;
790 /* calculate size of return buffer */
791 n_bits = hweight64(sset_mask);
793 memset(&info, 0, sizeof(info));
794 info.cmd = ETHTOOL_GSSET_INFO;
796 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
801 * fill return buffer based on input bitmask and successful
802 * get_sset_count return
804 for (i = 0; i < 64; i++) {
805 if (!(sset_mask & (1ULL << i)))
808 rc = __ethtool_get_sset_count(dev, i);
810 info.sset_mask |= (1ULL << i);
811 info_buf[idx++] = rc;
816 if (copy_to_user(useraddr, &info, sizeof(info)))
819 useraddr += offsetof(struct ethtool_sset_info, data);
820 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
830 static noinline_for_stack int
831 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
832 const struct compat_ethtool_rxnfc __user *useraddr,
835 struct compat_ethtool_rxnfc crxnfc = {};
837 /* We expect there to be holes between fs.m_ext and
838 * fs.ring_cookie and at the end of fs, but nowhere else.
839 * On non-x86, no conversion should be needed.
841 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
842 sizeof(struct compat_ethtool_rxnfc) !=
843 sizeof(struct ethtool_rxnfc));
844 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
845 sizeof(useraddr->fs.m_ext) !=
846 offsetof(struct ethtool_rxnfc, fs.m_ext) +
847 sizeof(rxnfc->fs.m_ext));
848 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
849 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
850 offsetof(struct ethtool_rxnfc, fs.location) -
851 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
853 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
856 *rxnfc = (struct ethtool_rxnfc) {
858 .flow_type = crxnfc.flow_type,
861 .flow_type = crxnfc.fs.flow_type,
862 .h_u = crxnfc.fs.h_u,
863 .h_ext = crxnfc.fs.h_ext,
864 .m_u = crxnfc.fs.m_u,
865 .m_ext = crxnfc.fs.m_ext,
866 .ring_cookie = crxnfc.fs.ring_cookie,
867 .location = crxnfc.fs.location,
869 .rule_cnt = crxnfc.rule_cnt,
875 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
876 const void __user *useraddr,
879 if (compat_need_64bit_alignment_fixup())
880 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
882 if (copy_from_user(rxnfc, useraddr, size))
888 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
889 const struct ethtool_rxnfc *rxnfc,
890 size_t size, const u32 *rule_buf)
892 struct compat_ethtool_rxnfc crxnfc;
894 memset(&crxnfc, 0, sizeof(crxnfc));
895 crxnfc = (struct compat_ethtool_rxnfc) {
897 .flow_type = rxnfc->flow_type,
900 .flow_type = rxnfc->fs.flow_type,
901 .h_u = rxnfc->fs.h_u,
902 .h_ext = rxnfc->fs.h_ext,
903 .m_u = rxnfc->fs.m_u,
904 .m_ext = rxnfc->fs.m_ext,
905 .ring_cookie = rxnfc->fs.ring_cookie,
906 .location = rxnfc->fs.location,
908 .rule_cnt = rxnfc->rule_cnt,
911 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
917 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
918 const struct ethtool_rxnfc *rxnfc,
919 size_t size, const u32 *rule_buf)
923 if (compat_need_64bit_alignment_fixup()) {
924 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
926 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
928 ret = copy_to_user(useraddr, rxnfc, size);
929 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
936 if (copy_to_user(useraddr, rule_buf,
937 rxnfc->rule_cnt * sizeof(u32)))
944 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
945 u32 cmd, void __user *useraddr)
947 struct ethtool_rxnfc info;
948 size_t info_size = sizeof(info);
951 if (!dev->ethtool_ops->set_rxnfc)
954 /* struct ethtool_rxnfc was originally defined for
955 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
956 * members. User-space might still be using that
958 if (cmd == ETHTOOL_SRXFH)
959 info_size = (offsetof(struct ethtool_rxnfc, data) +
962 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
965 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
969 if (cmd == ETHTOOL_SRXCLSRLINS &&
970 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
976 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
977 u32 cmd, void __user *useraddr)
979 struct ethtool_rxnfc info;
980 size_t info_size = sizeof(info);
981 const struct ethtool_ops *ops = dev->ethtool_ops;
983 void *rule_buf = NULL;
988 /* struct ethtool_rxnfc was originally defined for
989 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
990 * members. User-space might still be using that
992 if (cmd == ETHTOOL_GRXFH)
993 info_size = (offsetof(struct ethtool_rxnfc, data) +
996 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
999 /* If FLOW_RSS was requested then user-space must be using the
1000 * new definition, as FLOW_RSS is newer.
1002 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1003 info_size = sizeof(info);
1004 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
1006 /* Since malicious users may modify the original data,
1007 * we need to check whether FLOW_RSS is still requested.
1009 if (!(info.flow_type & FLOW_RSS))
1013 if (info.cmd != cmd)
1016 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1017 if (info.rule_cnt > 0) {
1018 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1019 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1026 ret = ops->get_rxnfc(dev, &info, rule_buf);
1030 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1037 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1038 struct ethtool_rxnfc *rx_rings,
1043 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1046 /* Validate ring indices */
1047 for (i = 0; i < size; i++)
1048 if (indir[i] >= rx_rings->data)
1054 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1056 void netdev_rss_key_fill(void *buffer, size_t len)
1058 BUG_ON(len > sizeof(netdev_rss_key));
1059 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1060 memcpy(buffer, netdev_rss_key, len);
1062 EXPORT_SYMBOL(netdev_rss_key_fill);
1064 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1065 void __user *useraddr)
1067 u32 user_size, dev_size;
1071 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1072 !dev->ethtool_ops->get_rxfh)
1074 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1078 if (copy_from_user(&user_size,
1079 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1083 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1084 &dev_size, sizeof(dev_size)))
1087 /* If the user buffer size is 0, this is just a query for the
1088 * device table size. Otherwise, if it's smaller than the
1089 * device table size it's an error.
1091 if (user_size < dev_size)
1092 return user_size == 0 ? 0 : -EINVAL;
1094 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1098 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1102 if (copy_to_user(useraddr +
1103 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1104 indir, dev_size * sizeof(indir[0])))
1112 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1113 void __user *useraddr)
1115 struct ethtool_rxnfc rx_rings;
1116 u32 user_size, dev_size, i;
1118 const struct ethtool_ops *ops = dev->ethtool_ops;
1120 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1122 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1126 dev_size = ops->get_rxfh_indir_size(dev);
1130 if (copy_from_user(&user_size,
1131 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1135 if (user_size != 0 && user_size != dev_size)
1138 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1142 rx_rings.cmd = ETHTOOL_GRXRINGS;
1143 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1147 if (user_size == 0) {
1148 for (i = 0; i < dev_size; i++)
1149 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1151 ret = ethtool_copy_validate_indir(indir,
1152 useraddr + ringidx_offset,
1159 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1163 /* indicate whether rxfh was set to default */
1165 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1167 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1174 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1175 void __user *useraddr)
1178 const struct ethtool_ops *ops = dev->ethtool_ops;
1179 u32 user_indir_size, user_key_size;
1180 u32 dev_indir_size = 0, dev_key_size = 0;
1181 struct ethtool_rxfh rxfh;
1192 if (ops->get_rxfh_indir_size)
1193 dev_indir_size = ops->get_rxfh_indir_size(dev);
1194 if (ops->get_rxfh_key_size)
1195 dev_key_size = ops->get_rxfh_key_size(dev);
1197 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1199 user_indir_size = rxfh.indir_size;
1200 user_key_size = rxfh.key_size;
1202 /* Check that reserved fields are 0 for now */
1203 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1205 /* Most drivers don't handle rss_context, check it's 0 as well */
1206 if (rxfh.rss_context && !ops->get_rxfh_context)
1209 rxfh.indir_size = dev_indir_size;
1210 rxfh.key_size = dev_key_size;
1211 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1214 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1215 (user_key_size && (user_key_size != dev_key_size)))
1218 indir_bytes = user_indir_size * sizeof(indir[0]);
1219 total_size = indir_bytes + user_key_size;
1220 rss_config = kzalloc(total_size, GFP_USER);
1224 if (user_indir_size)
1225 indir = (u32 *)rss_config;
1228 hkey = rss_config + indir_bytes;
1230 if (rxfh.rss_context)
1231 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1235 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1239 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1240 &dev_hfunc, sizeof(rxfh.hfunc))) {
1242 } else if (copy_to_user(useraddr +
1243 offsetof(struct ethtool_rxfh, rss_config[0]),
1244 rss_config, total_size)) {
1253 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1254 void __user *useraddr)
1257 const struct ethtool_ops *ops = dev->ethtool_ops;
1258 struct ethtool_rxnfc rx_rings;
1259 struct ethtool_rxfh rxfh;
1260 u32 dev_indir_size = 0, dev_key_size = 0, i;
1261 u32 *indir = NULL, indir_bytes = 0;
1264 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1265 bool delete = false;
1267 if (!ops->get_rxnfc || !ops->set_rxfh)
1270 if (ops->get_rxfh_indir_size)
1271 dev_indir_size = ops->get_rxfh_indir_size(dev);
1272 if (ops->get_rxfh_key_size)
1273 dev_key_size = ops->get_rxfh_key_size(dev);
1275 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1278 /* Check that reserved fields are 0 for now */
1279 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1281 /* Most drivers don't handle rss_context, check it's 0 as well */
1282 if (rxfh.rss_context && !ops->set_rxfh_context)
1285 /* If either indir, hash key or function is valid, proceed further.
1286 * Must request at least one change: indir size, hash key or function.
1288 if ((rxfh.indir_size &&
1289 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1290 rxfh.indir_size != dev_indir_size) ||
1291 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1292 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1293 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1296 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1297 indir_bytes = dev_indir_size * sizeof(indir[0]);
1299 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1303 rx_rings.cmd = ETHTOOL_GRXRINGS;
1304 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1308 /* rxfh.indir_size == 0 means reset the indir table to default (master
1309 * context) or delete the context (other RSS contexts).
1310 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1312 if (rxfh.indir_size &&
1313 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1314 indir = (u32 *)rss_config;
1315 ret = ethtool_copy_validate_indir(indir,
1316 useraddr + rss_cfg_offset,
1321 } else if (rxfh.indir_size == 0) {
1322 if (rxfh.rss_context == 0) {
1323 indir = (u32 *)rss_config;
1324 for (i = 0; i < dev_indir_size; i++)
1325 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1331 if (rxfh.key_size) {
1332 hkey = rss_config + indir_bytes;
1333 if (copy_from_user(hkey,
1334 useraddr + rss_cfg_offset + indir_bytes,
1341 if (rxfh.rss_context)
1342 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1343 &rxfh.rss_context, delete);
1345 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1349 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1350 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1353 if (!rxfh.rss_context) {
1354 /* indicate whether rxfh was set to default */
1355 if (rxfh.indir_size == 0)
1356 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1357 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1358 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1366 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1368 struct ethtool_regs regs;
1369 const struct ethtool_ops *ops = dev->ethtool_ops;
1373 if (!ops->get_regs || !ops->get_regs_len)
1376 if (copy_from_user(®s, useraddr, sizeof(regs)))
1379 reglen = ops->get_regs_len(dev);
1383 if (regs.len > reglen)
1386 regbuf = vzalloc(reglen);
1390 if (regs.len < reglen)
1393 ops->get_regs(dev, ®s, regbuf);
1396 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1398 useraddr += offsetof(struct ethtool_regs, data);
1399 if (copy_to_user(useraddr, regbuf, reglen))
1408 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1410 struct ethtool_value reset;
1413 if (!dev->ethtool_ops->reset)
1416 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1419 ret = dev->ethtool_ops->reset(dev, &reset.data);
1423 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1428 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1430 struct ethtool_wolinfo wol;
1432 if (!dev->ethtool_ops->get_wol)
1435 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1436 wol.cmd = ETHTOOL_GWOL;
1437 dev->ethtool_ops->get_wol(dev, &wol);
1439 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1444 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1446 struct ethtool_wolinfo wol;
1449 if (!dev->ethtool_ops->set_wol)
1452 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1455 ret = dev->ethtool_ops->set_wol(dev, &wol);
1459 dev->wol_enabled = !!wol.wolopts;
1460 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1465 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1467 struct ethtool_eee edata;
1470 if (!dev->ethtool_ops->get_eee)
1473 memset(&edata, 0, sizeof(struct ethtool_eee));
1474 edata.cmd = ETHTOOL_GEEE;
1475 rc = dev->ethtool_ops->get_eee(dev, &edata);
1480 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1486 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1488 struct ethtool_eee edata;
1491 if (!dev->ethtool_ops->set_eee)
1494 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1497 ret = dev->ethtool_ops->set_eee(dev, &edata);
1499 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1503 static int ethtool_nway_reset(struct net_device *dev)
1505 if (!dev->ethtool_ops->nway_reset)
1508 return dev->ethtool_ops->nway_reset(dev);
1511 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1513 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1514 int link = __ethtool_get_link(dev);
1520 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1525 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1526 int (*getter)(struct net_device *,
1527 struct ethtool_eeprom *, u8 *),
1530 struct ethtool_eeprom eeprom;
1531 void __user *userbuf = useraddr + sizeof(eeprom);
1532 u32 bytes_remaining;
1536 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1539 /* Check for wrap and zero */
1540 if (eeprom.offset + eeprom.len <= eeprom.offset)
1543 /* Check for exceeding total eeprom len */
1544 if (eeprom.offset + eeprom.len > total_len)
1547 data = kzalloc(PAGE_SIZE, GFP_USER);
1551 bytes_remaining = eeprom.len;
1552 while (bytes_remaining > 0) {
1553 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1555 ret = getter(dev, &eeprom, data);
1562 if (copy_to_user(userbuf, data, eeprom.len)) {
1566 userbuf += eeprom.len;
1567 eeprom.offset += eeprom.len;
1568 bytes_remaining -= eeprom.len;
1571 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1572 eeprom.offset -= eeprom.len;
1573 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1580 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1582 const struct ethtool_ops *ops = dev->ethtool_ops;
1584 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1585 !ops->get_eeprom_len(dev))
1588 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1589 ops->get_eeprom_len(dev));
1592 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1594 struct ethtool_eeprom eeprom;
1595 const struct ethtool_ops *ops = dev->ethtool_ops;
1596 void __user *userbuf = useraddr + sizeof(eeprom);
1597 u32 bytes_remaining;
1601 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1602 !ops->get_eeprom_len(dev))
1605 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1608 /* Check for wrap and zero */
1609 if (eeprom.offset + eeprom.len <= eeprom.offset)
1612 /* Check for exceeding total eeprom len */
1613 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1616 data = kzalloc(PAGE_SIZE, GFP_USER);
1620 bytes_remaining = eeprom.len;
1621 while (bytes_remaining > 0) {
1622 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1624 if (copy_from_user(data, userbuf, eeprom.len)) {
1628 ret = ops->set_eeprom(dev, &eeprom, data);
1631 userbuf += eeprom.len;
1632 eeprom.offset += eeprom.len;
1633 bytes_remaining -= eeprom.len;
1640 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1641 void __user *useraddr)
1643 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1644 struct kernel_ethtool_coalesce kernel_coalesce = {};
1647 if (!dev->ethtool_ops->get_coalesce)
1650 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1655 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1661 ethtool_set_coalesce_supported(struct net_device *dev,
1662 struct ethtool_coalesce *coalesce)
1664 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1665 u32 nonzero_params = 0;
1667 if (coalesce->rx_coalesce_usecs)
1668 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1669 if (coalesce->rx_max_coalesced_frames)
1670 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1671 if (coalesce->rx_coalesce_usecs_irq)
1672 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1673 if (coalesce->rx_max_coalesced_frames_irq)
1674 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1675 if (coalesce->tx_coalesce_usecs)
1676 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1677 if (coalesce->tx_max_coalesced_frames)
1678 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1679 if (coalesce->tx_coalesce_usecs_irq)
1680 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1681 if (coalesce->tx_max_coalesced_frames_irq)
1682 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1683 if (coalesce->stats_block_coalesce_usecs)
1684 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1685 if (coalesce->use_adaptive_rx_coalesce)
1686 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1687 if (coalesce->use_adaptive_tx_coalesce)
1688 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1689 if (coalesce->pkt_rate_low)
1690 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1691 if (coalesce->rx_coalesce_usecs_low)
1692 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1693 if (coalesce->rx_max_coalesced_frames_low)
1694 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1695 if (coalesce->tx_coalesce_usecs_low)
1696 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1697 if (coalesce->tx_max_coalesced_frames_low)
1698 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1699 if (coalesce->pkt_rate_high)
1700 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1701 if (coalesce->rx_coalesce_usecs_high)
1702 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1703 if (coalesce->rx_max_coalesced_frames_high)
1704 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1705 if (coalesce->tx_coalesce_usecs_high)
1706 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1707 if (coalesce->tx_max_coalesced_frames_high)
1708 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1709 if (coalesce->rate_sample_interval)
1710 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1712 return (supported_params & nonzero_params) == nonzero_params;
1715 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1716 void __user *useraddr)
1718 struct kernel_ethtool_coalesce kernel_coalesce = {};
1719 struct ethtool_coalesce coalesce;
1722 if (!dev->ethtool_ops->set_coalesce && !dev->ethtool_ops->get_coalesce)
1725 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1730 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1733 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1736 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1739 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1743 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1745 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1747 if (!dev->ethtool_ops->get_ringparam)
1750 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1752 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1757 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1759 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1762 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1765 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1768 dev->ethtool_ops->get_ringparam(dev, &max);
1770 /* ensure new ring parameters are within the maximums */
1771 if (ringparam.rx_pending > max.rx_max_pending ||
1772 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1773 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1774 ringparam.tx_pending > max.tx_max_pending)
1777 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1779 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1783 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1784 void __user *useraddr)
1786 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1788 if (!dev->ethtool_ops->get_channels)
1791 dev->ethtool_ops->get_channels(dev, &channels);
1793 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1798 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1799 void __user *useraddr)
1801 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1802 u16 from_channel, to_channel;
1803 u32 max_rx_in_use = 0;
1807 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1810 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1813 dev->ethtool_ops->get_channels(dev, &curr);
1815 if (channels.rx_count == curr.rx_count &&
1816 channels.tx_count == curr.tx_count &&
1817 channels.combined_count == curr.combined_count &&
1818 channels.other_count == curr.other_count)
1821 /* ensure new counts are within the maximums */
1822 if (channels.rx_count > curr.max_rx ||
1823 channels.tx_count > curr.max_tx ||
1824 channels.combined_count > curr.max_combined ||
1825 channels.other_count > curr.max_other)
1828 /* ensure there is at least one RX and one TX channel */
1829 if (!channels.combined_count &&
1830 (!channels.rx_count || !channels.tx_count))
1833 /* ensure the new Rx count fits within the configured Rx flow
1834 * indirection table settings */
1835 if (netif_is_rxfh_configured(dev) &&
1836 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1837 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1840 /* Disabling channels, query zero-copy AF_XDP sockets */
1841 from_channel = channels.combined_count +
1842 min(channels.rx_count, channels.tx_count);
1843 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1844 for (i = from_channel; i < to_channel; i++)
1845 if (xsk_get_pool_from_qid(dev, i))
1848 ret = dev->ethtool_ops->set_channels(dev, &channels);
1850 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1854 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1856 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1858 if (!dev->ethtool_ops->get_pauseparam)
1861 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1863 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1868 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1870 struct ethtool_pauseparam pauseparam;
1873 if (!dev->ethtool_ops->set_pauseparam)
1876 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1879 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1881 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1885 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1887 struct ethtool_test test;
1888 const struct ethtool_ops *ops = dev->ethtool_ops;
1892 if (!ops->self_test || !ops->get_sset_count)
1895 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1898 WARN_ON(test_len == 0);
1900 if (copy_from_user(&test, useraddr, sizeof(test)))
1903 test.len = test_len;
1904 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1908 netif_testing_on(dev);
1909 ops->self_test(dev, &test, data);
1910 netif_testing_off(dev);
1913 if (copy_to_user(useraddr, &test, sizeof(test)))
1915 useraddr += sizeof(test);
1916 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
1925 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1927 struct ethtool_gstrings gstrings;
1931 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1934 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1937 if (ret > S32_MAX / ETH_GSTRING_LEN)
1944 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1948 __ethtool_get_strings(dev, gstrings.string_set, data);
1954 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1956 useraddr += sizeof(gstrings);
1958 copy_to_user(useraddr, data,
1959 array_size(gstrings.len, ETH_GSTRING_LEN)))
1968 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1972 va_start(args, fmt);
1973 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1976 *data += ETH_GSTRING_LEN;
1978 EXPORT_SYMBOL(ethtool_sprintf);
1980 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1982 struct ethtool_value id;
1984 const struct ethtool_ops *ops = dev->ethtool_ops;
1987 if (!ops->set_phys_id)
1993 if (copy_from_user(&id, useraddr, sizeof(id)))
1996 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2000 /* Drop the RTNL lock while waiting, but prevent reentry or
2001 * removal of the device.
2008 /* Driver will handle this itself */
2009 schedule_timeout_interruptible(
2010 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2012 /* Driver expects to be called at twice the frequency in rc */
2013 int n = rc * 2, interval = HZ / n;
2014 u64 count = n * id.data, i = 0;
2018 rc = ops->set_phys_id(dev,
2019 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2023 schedule_timeout_interruptible(interval);
2024 } while (!signal_pending(current) && (!id.data || i < count));
2031 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2035 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2037 struct ethtool_stats stats;
2038 const struct ethtool_ops *ops = dev->ethtool_ops;
2042 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2045 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2048 if (n_stats > S32_MAX / sizeof(u64))
2050 WARN_ON_ONCE(!n_stats);
2051 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2054 stats.n_stats = n_stats;
2057 data = vzalloc(array_size(n_stats, sizeof(u64)));
2060 ops->get_ethtool_stats(dev, &stats, data);
2066 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2068 useraddr += sizeof(stats);
2069 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2078 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2080 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2081 const struct ethtool_ops *ops = dev->ethtool_ops;
2082 struct phy_device *phydev = dev->phydev;
2083 struct ethtool_stats stats;
2087 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
2090 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2091 phy_ops && phy_ops->get_sset_count)
2092 n_stats = phy_ops->get_sset_count(dev->phydev);
2094 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2097 if (n_stats > S32_MAX / sizeof(u64))
2099 WARN_ON_ONCE(!n_stats);
2101 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2104 stats.n_stats = n_stats;
2107 data = vzalloc(array_size(n_stats, sizeof(u64)));
2111 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2112 phy_ops && phy_ops->get_stats) {
2113 ret = phy_ops->get_stats(dev->phydev, &stats, data);
2117 ops->get_ethtool_phy_stats(dev, &stats, data);
2124 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2126 useraddr += sizeof(stats);
2127 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2136 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2138 struct ethtool_perm_addr epaddr;
2140 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2143 if (epaddr.size < dev->addr_len)
2145 epaddr.size = dev->addr_len;
2147 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2149 useraddr += sizeof(epaddr);
2150 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2155 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2156 u32 cmd, u32 (*actor)(struct net_device *))
2158 struct ethtool_value edata = { .cmd = cmd };
2163 edata.data = actor(dev);
2165 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2170 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2171 void (*actor)(struct net_device *, u32))
2173 struct ethtool_value edata;
2178 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2181 actor(dev, edata.data);
2185 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2186 int (*actor)(struct net_device *, u32))
2188 struct ethtool_value edata;
2193 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2196 return actor(dev, edata.data);
2200 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2202 if (!dev->ethtool_ops->flash_device) {
2203 req->devlink = netdev_to_devlink_get(dev);
2207 return dev->ethtool_ops->flash_device(dev, &req->efl);
2210 static int ethtool_set_dump(struct net_device *dev,
2211 void __user *useraddr)
2213 struct ethtool_dump dump;
2215 if (!dev->ethtool_ops->set_dump)
2218 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2221 return dev->ethtool_ops->set_dump(dev, &dump);
2224 static int ethtool_get_dump_flag(struct net_device *dev,
2225 void __user *useraddr)
2228 struct ethtool_dump dump;
2229 const struct ethtool_ops *ops = dev->ethtool_ops;
2231 if (!ops->get_dump_flag)
2234 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2237 ret = ops->get_dump_flag(dev, &dump);
2241 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2246 static int ethtool_get_dump_data(struct net_device *dev,
2247 void __user *useraddr)
2251 struct ethtool_dump dump, tmp;
2252 const struct ethtool_ops *ops = dev->ethtool_ops;
2255 if (!ops->get_dump_data || !ops->get_dump_flag)
2258 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2261 memset(&tmp, 0, sizeof(tmp));
2262 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2263 ret = ops->get_dump_flag(dev, &tmp);
2267 len = min(tmp.len, dump.len);
2271 /* Don't ever let the driver think there's more space available
2272 * than it requested with .get_dump_flag().
2276 /* Always allocate enough space to hold the whole thing so that the
2277 * driver does not need to check the length and bother with partial
2280 data = vzalloc(tmp.len);
2283 ret = ops->get_dump_data(dev, &dump, data);
2287 /* There are two sane possibilities:
2288 * 1. The driver's .get_dump_data() does not touch dump.len.
2289 * 2. Or it may set dump.len to how much it really writes, which
2290 * should be tmp.len (or len if it can do a partial dump).
2291 * In any case respond to userspace with the actual length of data
2294 WARN_ON(dump.len != len && dump.len != tmp.len);
2297 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2301 useraddr += offsetof(struct ethtool_dump, data);
2302 if (copy_to_user(useraddr, data, len))
2309 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2311 struct ethtool_ts_info info;
2314 err = __ethtool_get_ts_info(dev, &info);
2318 if (copy_to_user(useraddr, &info, sizeof(info)))
2324 int ethtool_get_module_info_call(struct net_device *dev,
2325 struct ethtool_modinfo *modinfo)
2327 const struct ethtool_ops *ops = dev->ethtool_ops;
2328 struct phy_device *phydev = dev->phydev;
2331 return sfp_get_module_info(dev->sfp_bus, modinfo);
2333 if (phydev && phydev->drv && phydev->drv->module_info)
2334 return phydev->drv->module_info(phydev, modinfo);
2336 if (ops->get_module_info)
2337 return ops->get_module_info(dev, modinfo);
2342 static int ethtool_get_module_info(struct net_device *dev,
2343 void __user *useraddr)
2346 struct ethtool_modinfo modinfo;
2348 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2351 ret = ethtool_get_module_info_call(dev, &modinfo);
2355 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2361 int ethtool_get_module_eeprom_call(struct net_device *dev,
2362 struct ethtool_eeprom *ee, u8 *data)
2364 const struct ethtool_ops *ops = dev->ethtool_ops;
2365 struct phy_device *phydev = dev->phydev;
2368 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2370 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2371 return phydev->drv->module_eeprom(phydev, ee, data);
2373 if (ops->get_module_eeprom)
2374 return ops->get_module_eeprom(dev, ee, data);
2379 static int ethtool_get_module_eeprom(struct net_device *dev,
2380 void __user *useraddr)
2383 struct ethtool_modinfo modinfo;
2385 ret = ethtool_get_module_info_call(dev, &modinfo);
2389 return ethtool_get_any_eeprom(dev, useraddr,
2390 ethtool_get_module_eeprom_call,
2391 modinfo.eeprom_len);
2394 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2397 case ETHTOOL_RX_COPYBREAK:
2398 case ETHTOOL_TX_COPYBREAK:
2399 if (tuna->len != sizeof(u32) ||
2400 tuna->type_id != ETHTOOL_TUNABLE_U32)
2403 case ETHTOOL_PFC_PREVENTION_TOUT:
2404 if (tuna->len != sizeof(u16) ||
2405 tuna->type_id != ETHTOOL_TUNABLE_U16)
2415 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2418 struct ethtool_tunable tuna;
2419 const struct ethtool_ops *ops = dev->ethtool_ops;
2422 if (!ops->get_tunable)
2424 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2426 ret = ethtool_tunable_valid(&tuna);
2429 data = kzalloc(tuna.len, GFP_USER);
2432 ret = ops->get_tunable(dev, &tuna, data);
2435 useraddr += sizeof(tuna);
2437 if (copy_to_user(useraddr, data, tuna.len))
2446 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2449 struct ethtool_tunable tuna;
2450 const struct ethtool_ops *ops = dev->ethtool_ops;
2453 if (!ops->set_tunable)
2455 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2457 ret = ethtool_tunable_valid(&tuna);
2460 useraddr += sizeof(tuna);
2461 data = memdup_user(useraddr, tuna.len);
2463 return PTR_ERR(data);
2464 ret = ops->set_tunable(dev, &tuna, data);
2470 static noinline_for_stack int
2471 ethtool_get_per_queue_coalesce(struct net_device *dev,
2472 void __user *useraddr,
2473 struct ethtool_per_queue_op *per_queue_opt)
2477 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2479 if (!dev->ethtool_ops->get_per_queue_coalesce)
2482 useraddr += sizeof(*per_queue_opt);
2484 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2487 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2488 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2490 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2493 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2495 useraddr += sizeof(coalesce);
2501 static noinline_for_stack int
2502 ethtool_set_per_queue_coalesce(struct net_device *dev,
2503 void __user *useraddr,
2504 struct ethtool_per_queue_op *per_queue_opt)
2509 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2510 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2512 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2513 (!dev->ethtool_ops->get_per_queue_coalesce))
2516 useraddr += sizeof(*per_queue_opt);
2518 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2519 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2520 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2524 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2525 struct ethtool_coalesce coalesce;
2527 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2533 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2538 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2543 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2547 useraddr += sizeof(coalesce);
2553 for_each_set_bit(i, queue_mask, bit) {
2554 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2563 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2564 void __user *useraddr, u32 sub_cmd)
2566 struct ethtool_per_queue_op per_queue_opt;
2568 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2571 if (per_queue_opt.sub_command != sub_cmd)
2574 switch (per_queue_opt.sub_command) {
2575 case ETHTOOL_GCOALESCE:
2576 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2577 case ETHTOOL_SCOALESCE:
2578 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2584 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2587 case ETHTOOL_PHY_DOWNSHIFT:
2588 case ETHTOOL_PHY_FAST_LINK_DOWN:
2589 if (tuna->len != sizeof(u8) ||
2590 tuna->type_id != ETHTOOL_TUNABLE_U8)
2593 case ETHTOOL_PHY_EDPD:
2594 if (tuna->len != sizeof(u16) ||
2595 tuna->type_id != ETHTOOL_TUNABLE_U16)
2605 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2607 struct phy_device *phydev = dev->phydev;
2608 struct ethtool_tunable tuna;
2609 bool phy_drv_tunable;
2613 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2614 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2616 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2618 ret = ethtool_phy_tunable_valid(&tuna);
2621 data = kzalloc(tuna.len, GFP_USER);
2624 if (phy_drv_tunable) {
2625 mutex_lock(&phydev->lock);
2626 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2627 mutex_unlock(&phydev->lock);
2629 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2633 useraddr += sizeof(tuna);
2635 if (copy_to_user(useraddr, data, tuna.len))
2644 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2646 struct phy_device *phydev = dev->phydev;
2647 struct ethtool_tunable tuna;
2648 bool phy_drv_tunable;
2652 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2653 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2655 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2657 ret = ethtool_phy_tunable_valid(&tuna);
2660 useraddr += sizeof(tuna);
2661 data = memdup_user(useraddr, tuna.len);
2663 return PTR_ERR(data);
2664 if (phy_drv_tunable) {
2665 mutex_lock(&phydev->lock);
2666 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2667 mutex_unlock(&phydev->lock);
2669 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2676 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2678 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2681 if (!dev->ethtool_ops->get_fecparam)
2684 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2688 if (WARN_ON_ONCE(fecparam.reserved))
2689 fecparam.reserved = 0;
2691 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2696 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2698 struct ethtool_fecparam fecparam;
2700 if (!dev->ethtool_ops->set_fecparam)
2703 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2706 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2709 fecparam.active_fec = 0;
2710 fecparam.reserved = 0;
2712 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2715 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2718 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2719 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2721 struct net_device *dev;
2724 netdev_features_t old_features;
2726 dev = __dev_get_by_name(net, ifr->ifr_name);
2730 if (ethcmd == ETHTOOL_PERQUEUE) {
2731 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2736 /* Allow some commands to be done by anyone */
2739 case ETHTOOL_GDRVINFO:
2740 case ETHTOOL_GMSGLVL:
2742 case ETHTOOL_GCOALESCE:
2743 case ETHTOOL_GRINGPARAM:
2744 case ETHTOOL_GPAUSEPARAM:
2745 case ETHTOOL_GRXCSUM:
2746 case ETHTOOL_GTXCSUM:
2748 case ETHTOOL_GSSET_INFO:
2749 case ETHTOOL_GSTRINGS:
2750 case ETHTOOL_GSTATS:
2751 case ETHTOOL_GPHYSTATS:
2753 case ETHTOOL_GPERMADDR:
2757 case ETHTOOL_GFLAGS:
2758 case ETHTOOL_GPFLAGS:
2760 case ETHTOOL_GRXRINGS:
2761 case ETHTOOL_GRXCLSRLCNT:
2762 case ETHTOOL_GRXCLSRULE:
2763 case ETHTOOL_GRXCLSRLALL:
2764 case ETHTOOL_GRXFHINDIR:
2766 case ETHTOOL_GFEATURES:
2767 case ETHTOOL_GCHANNELS:
2768 case ETHTOOL_GET_TS_INFO:
2770 case ETHTOOL_GTUNABLE:
2771 case ETHTOOL_PHY_GTUNABLE:
2772 case ETHTOOL_GLINKSETTINGS:
2773 case ETHTOOL_GFECPARAM:
2776 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2780 if (dev->dev.parent)
2781 pm_runtime_get_sync(dev->dev.parent);
2783 if (!netif_device_present(dev)) {
2788 if (dev->ethtool_ops->begin) {
2789 rc = dev->ethtool_ops->begin(dev);
2793 old_features = dev->features;
2797 rc = ethtool_get_settings(dev, useraddr);
2800 rc = ethtool_set_settings(dev, useraddr);
2802 case ETHTOOL_GDRVINFO:
2803 rc = ethtool_get_drvinfo(dev, devlink_state);
2806 rc = ethtool_get_regs(dev, useraddr);
2809 rc = ethtool_get_wol(dev, useraddr);
2812 rc = ethtool_set_wol(dev, useraddr);
2814 case ETHTOOL_GMSGLVL:
2815 rc = ethtool_get_value(dev, useraddr, ethcmd,
2816 dev->ethtool_ops->get_msglevel);
2818 case ETHTOOL_SMSGLVL:
2819 rc = ethtool_set_value_void(dev, useraddr,
2820 dev->ethtool_ops->set_msglevel);
2822 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2825 rc = ethtool_get_eee(dev, useraddr);
2828 rc = ethtool_set_eee(dev, useraddr);
2830 case ETHTOOL_NWAY_RST:
2831 rc = ethtool_nway_reset(dev);
2834 rc = ethtool_get_link(dev, useraddr);
2836 case ETHTOOL_GEEPROM:
2837 rc = ethtool_get_eeprom(dev, useraddr);
2839 case ETHTOOL_SEEPROM:
2840 rc = ethtool_set_eeprom(dev, useraddr);
2842 case ETHTOOL_GCOALESCE:
2843 rc = ethtool_get_coalesce(dev, useraddr);
2845 case ETHTOOL_SCOALESCE:
2846 rc = ethtool_set_coalesce(dev, useraddr);
2848 case ETHTOOL_GRINGPARAM:
2849 rc = ethtool_get_ringparam(dev, useraddr);
2851 case ETHTOOL_SRINGPARAM:
2852 rc = ethtool_set_ringparam(dev, useraddr);
2854 case ETHTOOL_GPAUSEPARAM:
2855 rc = ethtool_get_pauseparam(dev, useraddr);
2857 case ETHTOOL_SPAUSEPARAM:
2858 rc = ethtool_set_pauseparam(dev, useraddr);
2861 rc = ethtool_self_test(dev, useraddr);
2863 case ETHTOOL_GSTRINGS:
2864 rc = ethtool_get_strings(dev, useraddr);
2866 case ETHTOOL_PHYS_ID:
2867 rc = ethtool_phys_id(dev, useraddr);
2869 case ETHTOOL_GSTATS:
2870 rc = ethtool_get_stats(dev, useraddr);
2872 case ETHTOOL_GPERMADDR:
2873 rc = ethtool_get_perm_addr(dev, useraddr);
2875 case ETHTOOL_GFLAGS:
2876 rc = ethtool_get_value(dev, useraddr, ethcmd,
2877 __ethtool_get_flags);
2879 case ETHTOOL_SFLAGS:
2880 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2882 case ETHTOOL_GPFLAGS:
2883 rc = ethtool_get_value(dev, useraddr, ethcmd,
2884 dev->ethtool_ops->get_priv_flags);
2886 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2888 case ETHTOOL_SPFLAGS:
2889 rc = ethtool_set_value(dev, useraddr,
2890 dev->ethtool_ops->set_priv_flags);
2893 case ETHTOOL_GRXRINGS:
2894 case ETHTOOL_GRXCLSRLCNT:
2895 case ETHTOOL_GRXCLSRULE:
2896 case ETHTOOL_GRXCLSRLALL:
2897 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2900 case ETHTOOL_SRXCLSRLDEL:
2901 case ETHTOOL_SRXCLSRLINS:
2902 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2904 case ETHTOOL_FLASHDEV:
2905 rc = ethtool_flash_device(dev, devlink_state);
2908 rc = ethtool_reset(dev, useraddr);
2910 case ETHTOOL_GSSET_INFO:
2911 rc = ethtool_get_sset_info(dev, useraddr);
2913 case ETHTOOL_GRXFHINDIR:
2914 rc = ethtool_get_rxfh_indir(dev, useraddr);
2916 case ETHTOOL_SRXFHINDIR:
2917 rc = ethtool_set_rxfh_indir(dev, useraddr);
2920 rc = ethtool_get_rxfh(dev, useraddr);
2923 rc = ethtool_set_rxfh(dev, useraddr);
2925 case ETHTOOL_GFEATURES:
2926 rc = ethtool_get_features(dev, useraddr);
2928 case ETHTOOL_SFEATURES:
2929 rc = ethtool_set_features(dev, useraddr);
2931 case ETHTOOL_GTXCSUM:
2932 case ETHTOOL_GRXCSUM:
2937 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2939 case ETHTOOL_STXCSUM:
2940 case ETHTOOL_SRXCSUM:
2945 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2947 case ETHTOOL_GCHANNELS:
2948 rc = ethtool_get_channels(dev, useraddr);
2950 case ETHTOOL_SCHANNELS:
2951 rc = ethtool_set_channels(dev, useraddr);
2953 case ETHTOOL_SET_DUMP:
2954 rc = ethtool_set_dump(dev, useraddr);
2956 case ETHTOOL_GET_DUMP_FLAG:
2957 rc = ethtool_get_dump_flag(dev, useraddr);
2959 case ETHTOOL_GET_DUMP_DATA:
2960 rc = ethtool_get_dump_data(dev, useraddr);
2962 case ETHTOOL_GET_TS_INFO:
2963 rc = ethtool_get_ts_info(dev, useraddr);
2965 case ETHTOOL_GMODULEINFO:
2966 rc = ethtool_get_module_info(dev, useraddr);
2968 case ETHTOOL_GMODULEEEPROM:
2969 rc = ethtool_get_module_eeprom(dev, useraddr);
2971 case ETHTOOL_GTUNABLE:
2972 rc = ethtool_get_tunable(dev, useraddr);
2974 case ETHTOOL_STUNABLE:
2975 rc = ethtool_set_tunable(dev, useraddr);
2977 case ETHTOOL_GPHYSTATS:
2978 rc = ethtool_get_phy_stats(dev, useraddr);
2980 case ETHTOOL_PERQUEUE:
2981 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2983 case ETHTOOL_GLINKSETTINGS:
2984 rc = ethtool_get_link_ksettings(dev, useraddr);
2986 case ETHTOOL_SLINKSETTINGS:
2987 rc = ethtool_set_link_ksettings(dev, useraddr);
2989 case ETHTOOL_PHY_GTUNABLE:
2990 rc = get_phy_tunable(dev, useraddr);
2992 case ETHTOOL_PHY_STUNABLE:
2993 rc = set_phy_tunable(dev, useraddr);
2995 case ETHTOOL_GFECPARAM:
2996 rc = ethtool_get_fecparam(dev, useraddr);
2998 case ETHTOOL_SFECPARAM:
2999 rc = ethtool_set_fecparam(dev, useraddr);
3005 if (dev->ethtool_ops->complete)
3006 dev->ethtool_ops->complete(dev);
3008 if (old_features != dev->features)
3009 netdev_features_change(dev);
3011 if (dev->dev.parent)
3012 pm_runtime_put(dev->dev.parent);
3017 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3019 struct ethtool_devlink_compat *state;
3023 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3026 state = kzalloc(sizeof(*state), GFP_KERNEL);
3031 case ETHTOOL_FLASHDEV:
3032 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3036 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3041 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3047 case ETHTOOL_FLASHDEV:
3049 rc = devlink_compat_flash_update(state->devlink,
3052 case ETHTOOL_GDRVINFO:
3054 devlink_compat_running_version(state->devlink,
3055 state->info.fw_version,
3056 sizeof(state->info.fw_version));
3057 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3066 devlink_put(state->devlink);
3071 struct ethtool_rx_flow_key {
3072 struct flow_dissector_key_basic basic;
3074 struct flow_dissector_key_ipv4_addrs ipv4;
3075 struct flow_dissector_key_ipv6_addrs ipv6;
3077 struct flow_dissector_key_ports tp;
3078 struct flow_dissector_key_ip ip;
3079 struct flow_dissector_key_vlan vlan;
3080 struct flow_dissector_key_eth_addrs eth_addrs;
3081 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3083 struct ethtool_rx_flow_match {
3084 struct flow_dissector dissector;
3085 struct ethtool_rx_flow_key key;
3086 struct ethtool_rx_flow_key mask;
3089 struct ethtool_rx_flow_rule *
3090 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3092 const struct ethtool_rx_flow_spec *fs = input->fs;
3093 static struct in6_addr zero_addr = {};
3094 struct ethtool_rx_flow_match *match;
3095 struct ethtool_rx_flow_rule *flow;
3096 struct flow_action_entry *act;
3098 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3099 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3101 return ERR_PTR(-ENOMEM);
3103 /* ethtool_rx supports only one single action per rule. */
3104 flow->rule = flow_rule_alloc(1);
3107 return ERR_PTR(-ENOMEM);
3110 match = (struct ethtool_rx_flow_match *)flow->priv;
3111 flow->rule->match.dissector = &match->dissector;
3112 flow->rule->match.mask = &match->mask;
3113 flow->rule->match.key = &match->key;
3115 match->mask.basic.n_proto = htons(0xffff);
3117 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3119 const struct ethhdr *ether_spec, *ether_m_spec;
3121 ether_spec = &fs->h_u.ether_spec;
3122 ether_m_spec = &fs->m_u.ether_spec;
3124 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3125 ether_addr_copy(match->key.eth_addrs.src,
3126 ether_spec->h_source);
3127 ether_addr_copy(match->mask.eth_addrs.src,
3128 ether_m_spec->h_source);
3130 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3131 ether_addr_copy(match->key.eth_addrs.dst,
3132 ether_spec->h_dest);
3133 ether_addr_copy(match->mask.eth_addrs.dst,
3134 ether_m_spec->h_dest);
3136 if (ether_m_spec->h_proto) {
3137 match->key.basic.n_proto = ether_spec->h_proto;
3138 match->mask.basic.n_proto = ether_m_spec->h_proto;
3144 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3146 match->key.basic.n_proto = htons(ETH_P_IP);
3148 v4_spec = &fs->h_u.tcp_ip4_spec;
3149 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3151 if (v4_m_spec->ip4src) {
3152 match->key.ipv4.src = v4_spec->ip4src;
3153 match->mask.ipv4.src = v4_m_spec->ip4src;
3155 if (v4_m_spec->ip4dst) {
3156 match->key.ipv4.dst = v4_spec->ip4dst;
3157 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3159 if (v4_m_spec->ip4src ||
3160 v4_m_spec->ip4dst) {
3161 match->dissector.used_keys |=
3162 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3163 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3164 offsetof(struct ethtool_rx_flow_key, ipv4);
3166 if (v4_m_spec->psrc) {
3167 match->key.tp.src = v4_spec->psrc;
3168 match->mask.tp.src = v4_m_spec->psrc;
3170 if (v4_m_spec->pdst) {
3171 match->key.tp.dst = v4_spec->pdst;
3172 match->mask.tp.dst = v4_m_spec->pdst;
3174 if (v4_m_spec->psrc ||
3176 match->dissector.used_keys |=
3177 BIT(FLOW_DISSECTOR_KEY_PORTS);
3178 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3179 offsetof(struct ethtool_rx_flow_key, tp);
3181 if (v4_m_spec->tos) {
3182 match->key.ip.tos = v4_spec->tos;
3183 match->mask.ip.tos = v4_m_spec->tos;
3184 match->dissector.used_keys |=
3185 BIT(FLOW_DISSECTOR_KEY_IP);
3186 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3187 offsetof(struct ethtool_rx_flow_key, ip);
3193 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3195 match->key.basic.n_proto = htons(ETH_P_IPV6);
3197 v6_spec = &fs->h_u.tcp_ip6_spec;
3198 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3199 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
3200 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3201 sizeof(match->key.ipv6.src));
3202 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3203 sizeof(match->mask.ipv6.src));
3205 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3206 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3207 sizeof(match->key.ipv6.dst));
3208 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3209 sizeof(match->mask.ipv6.dst));
3211 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
3212 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3213 match->dissector.used_keys |=
3214 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3215 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3216 offsetof(struct ethtool_rx_flow_key, ipv6);
3218 if (v6_m_spec->psrc) {
3219 match->key.tp.src = v6_spec->psrc;
3220 match->mask.tp.src = v6_m_spec->psrc;
3222 if (v6_m_spec->pdst) {
3223 match->key.tp.dst = v6_spec->pdst;
3224 match->mask.tp.dst = v6_m_spec->pdst;
3226 if (v6_m_spec->psrc ||
3228 match->dissector.used_keys |=
3229 BIT(FLOW_DISSECTOR_KEY_PORTS);
3230 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3231 offsetof(struct ethtool_rx_flow_key, tp);
3233 if (v6_m_spec->tclass) {
3234 match->key.ip.tos = v6_spec->tclass;
3235 match->mask.ip.tos = v6_m_spec->tclass;
3236 match->dissector.used_keys |=
3237 BIT(FLOW_DISSECTOR_KEY_IP);
3238 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3239 offsetof(struct ethtool_rx_flow_key, ip);
3244 ethtool_rx_flow_rule_destroy(flow);
3245 return ERR_PTR(-EINVAL);
3248 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3251 match->key.basic.ip_proto = IPPROTO_TCP;
3252 match->mask.basic.ip_proto = 0xff;
3256 match->key.basic.ip_proto = IPPROTO_UDP;
3257 match->mask.basic.ip_proto = 0xff;
3261 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3262 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3263 offsetof(struct ethtool_rx_flow_key, basic);
3265 if (fs->flow_type & FLOW_EXT) {
3266 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3267 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3269 if (ext_m_spec->vlan_etype) {
3270 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3271 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3274 if (ext_m_spec->vlan_tci) {
3275 match->key.vlan.vlan_id =
3276 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3277 match->mask.vlan.vlan_id =
3278 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3280 match->key.vlan.vlan_dei =
3281 !!(ext_h_spec->vlan_tci & htons(0x1000));
3282 match->mask.vlan.vlan_dei =
3283 !!(ext_m_spec->vlan_tci & htons(0x1000));
3285 match->key.vlan.vlan_priority =
3286 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3287 match->mask.vlan.vlan_priority =
3288 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3291 if (ext_m_spec->vlan_etype ||
3292 ext_m_spec->vlan_tci) {
3293 match->dissector.used_keys |=
3294 BIT(FLOW_DISSECTOR_KEY_VLAN);
3295 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3296 offsetof(struct ethtool_rx_flow_key, vlan);
3299 if (fs->flow_type & FLOW_MAC_EXT) {
3300 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3301 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3303 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3305 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3308 match->dissector.used_keys |=
3309 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3310 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3311 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3314 act = &flow->rule->action.entries[0];
3315 switch (fs->ring_cookie) {
3316 case RX_CLS_FLOW_DISC:
3317 act->id = FLOW_ACTION_DROP;
3319 case RX_CLS_FLOW_WAKE:
3320 act->id = FLOW_ACTION_WAKE;
3323 act->id = FLOW_ACTION_QUEUE;
3324 if (fs->flow_type & FLOW_RSS)
3325 act->queue.ctx = input->rss_ctx;
3327 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3328 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3334 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3336 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3341 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);