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/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>
36 * Some useful ethtool_ops methods that're device independent.
37 * If we find that all drivers want to do the same thing here,
38 * we can turn these into dev_() function calls.
41 u32 ethtool_op_get_link(struct net_device *dev)
43 return netif_carrier_ok(dev) ? 1 : 0;
45 EXPORT_SYMBOL(ethtool_op_get_link);
47 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
49 info->so_timestamping =
50 SOF_TIMESTAMPING_TX_SOFTWARE |
51 SOF_TIMESTAMPING_RX_SOFTWARE |
52 SOF_TIMESTAMPING_SOFTWARE;
56 EXPORT_SYMBOL(ethtool_op_get_ts_info);
58 /* Handlers for each ethtool command */
60 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
62 struct ethtool_gfeatures cmd = {
63 .cmd = ETHTOOL_GFEATURES,
64 .size = ETHTOOL_DEV_FEATURE_WORDS,
66 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
71 /* in case feature bits run out again */
72 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
74 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
75 features[i].available = (u32)(dev->hw_features >> (32 * i));
76 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
77 features[i].active = (u32)(dev->features >> (32 * i));
78 features[i].never_changed =
79 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
82 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
83 if (get_user(copy_size, sizeaddr))
86 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
87 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
89 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
91 useraddr += sizeof(cmd);
92 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
98 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
100 struct ethtool_sfeatures cmd;
101 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
102 netdev_features_t wanted = 0, valid = 0;
105 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
107 useraddr += sizeof(cmd);
109 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
112 if (copy_from_user(features, useraddr, sizeof(features)))
115 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
116 valid |= (netdev_features_t)features[i].valid << (32 * i);
117 wanted |= (netdev_features_t)features[i].requested << (32 * i);
120 if (valid & ~NETIF_F_ETHTOOL_BITS)
123 if (valid & ~dev->hw_features) {
124 valid &= dev->hw_features;
125 ret |= ETHTOOL_F_UNSUPPORTED;
128 dev->wanted_features &= ~valid;
129 dev->wanted_features |= wanted & valid;
130 __netdev_update_features(dev);
132 if ((dev->wanted_features ^ dev->features) & valid)
133 ret |= ETHTOOL_F_WISH;
138 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
140 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
141 const struct ethtool_ops *ops = dev->ethtool_ops;
143 if (sset == ETH_SS_FEATURES)
144 return ARRAY_SIZE(netdev_features_strings);
146 if (sset == ETH_SS_RSS_HASH_FUNCS)
147 return ARRAY_SIZE(rss_hash_func_strings);
149 if (sset == ETH_SS_TUNABLES)
150 return ARRAY_SIZE(tunable_strings);
152 if (sset == ETH_SS_PHY_TUNABLES)
153 return ARRAY_SIZE(phy_tunable_strings);
155 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
156 !ops->get_ethtool_phy_stats &&
157 phy_ops && phy_ops->get_sset_count)
158 return phy_ops->get_sset_count(dev->phydev);
160 if (sset == ETH_SS_LINK_MODES)
161 return __ETHTOOL_LINK_MODE_MASK_NBITS;
163 if (ops->get_sset_count && ops->get_strings)
164 return ops->get_sset_count(dev, sset);
169 static void __ethtool_get_strings(struct net_device *dev,
170 u32 stringset, u8 *data)
172 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
173 const struct ethtool_ops *ops = dev->ethtool_ops;
175 if (stringset == ETH_SS_FEATURES)
176 memcpy(data, netdev_features_strings,
177 sizeof(netdev_features_strings));
178 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
179 memcpy(data, rss_hash_func_strings,
180 sizeof(rss_hash_func_strings));
181 else if (stringset == ETH_SS_TUNABLES)
182 memcpy(data, tunable_strings, sizeof(tunable_strings));
183 else if (stringset == ETH_SS_PHY_TUNABLES)
184 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
185 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
186 !ops->get_ethtool_phy_stats && phy_ops &&
187 phy_ops->get_strings)
188 phy_ops->get_strings(dev->phydev, data);
189 else if (stringset == ETH_SS_LINK_MODES)
190 memcpy(data, link_mode_names,
191 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
193 /* ops->get_strings is valid because checked earlier */
194 ops->get_strings(dev, stringset, data);
197 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
199 /* feature masks of legacy discrete ethtool ops */
202 case ETHTOOL_GTXCSUM:
203 case ETHTOOL_STXCSUM:
204 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
206 case ETHTOOL_GRXCSUM:
207 case ETHTOOL_SRXCSUM:
208 return NETIF_F_RXCSUM;
211 return NETIF_F_SG | NETIF_F_FRAGLIST;
214 return NETIF_F_ALL_TSO;
226 static int ethtool_get_one_feature(struct net_device *dev,
227 char __user *useraddr, u32 ethcmd)
229 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
230 struct ethtool_value edata = {
232 .data = !!(dev->features & mask),
235 if (copy_to_user(useraddr, &edata, sizeof(edata)))
240 static int ethtool_set_one_feature(struct net_device *dev,
241 void __user *useraddr, u32 ethcmd)
243 struct ethtool_value edata;
244 netdev_features_t mask;
246 if (copy_from_user(&edata, useraddr, sizeof(edata)))
249 mask = ethtool_get_feature_mask(ethcmd);
250 mask &= dev->hw_features;
255 dev->wanted_features |= mask;
257 dev->wanted_features &= ~mask;
259 __netdev_update_features(dev);
264 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
265 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
266 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
267 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
270 static u32 __ethtool_get_flags(struct net_device *dev)
274 if (dev->features & NETIF_F_LRO)
275 flags |= ETH_FLAG_LRO;
276 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
277 flags |= ETH_FLAG_RXVLAN;
278 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
279 flags |= ETH_FLAG_TXVLAN;
280 if (dev->features & NETIF_F_NTUPLE)
281 flags |= ETH_FLAG_NTUPLE;
282 if (dev->features & NETIF_F_RXHASH)
283 flags |= ETH_FLAG_RXHASH;
288 static int __ethtool_set_flags(struct net_device *dev, u32 data)
290 netdev_features_t features = 0, changed;
292 if (data & ~ETH_ALL_FLAGS)
295 if (data & ETH_FLAG_LRO)
296 features |= NETIF_F_LRO;
297 if (data & ETH_FLAG_RXVLAN)
298 features |= NETIF_F_HW_VLAN_CTAG_RX;
299 if (data & ETH_FLAG_TXVLAN)
300 features |= NETIF_F_HW_VLAN_CTAG_TX;
301 if (data & ETH_FLAG_NTUPLE)
302 features |= NETIF_F_NTUPLE;
303 if (data & ETH_FLAG_RXHASH)
304 features |= NETIF_F_RXHASH;
306 /* allow changing only bits set in hw_features */
307 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
308 if (changed & ~dev->hw_features)
309 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
311 dev->wanted_features =
312 (dev->wanted_features & ~changed) | (features & changed);
314 __netdev_update_features(dev);
319 /* Given two link masks, AND them together and save the result in dst. */
320 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
321 struct ethtool_link_ksettings *src)
323 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
324 unsigned int idx = 0;
326 for (; idx < size; idx++) {
327 dst->link_modes.supported[idx] &=
328 src->link_modes.supported[idx];
329 dst->link_modes.advertising[idx] &=
330 src->link_modes.advertising[idx];
333 EXPORT_SYMBOL(ethtool_intersect_link_masks);
335 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
338 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
341 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
343 /* return false if src had higher bits set. lower bits always updated. */
344 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
345 const unsigned long *src)
349 /* TODO: following test will soon always be true */
350 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
351 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
353 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
354 bitmap_fill(ext, 32);
355 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
356 if (bitmap_intersects(ext, src,
357 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
358 /* src mask goes beyond bit 31 */
362 *legacy_u32 = src[0];
365 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
367 /* return false if ksettings link modes had higher bits
368 * set. legacy_settings always updated (best effort)
371 convert_link_ksettings_to_legacy_settings(
372 struct ethtool_cmd *legacy_settings,
373 const struct ethtool_link_ksettings *link_ksettings)
377 memset(legacy_settings, 0, sizeof(*legacy_settings));
378 /* this also clears the deprecated fields in legacy structure:
384 retval &= ethtool_convert_link_mode_to_legacy_u32(
385 &legacy_settings->supported,
386 link_ksettings->link_modes.supported);
387 retval &= ethtool_convert_link_mode_to_legacy_u32(
388 &legacy_settings->advertising,
389 link_ksettings->link_modes.advertising);
390 retval &= ethtool_convert_link_mode_to_legacy_u32(
391 &legacy_settings->lp_advertising,
392 link_ksettings->link_modes.lp_advertising);
393 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
394 legacy_settings->duplex
395 = link_ksettings->base.duplex;
396 legacy_settings->port
397 = link_ksettings->base.port;
398 legacy_settings->phy_address
399 = link_ksettings->base.phy_address;
400 legacy_settings->autoneg
401 = link_ksettings->base.autoneg;
402 legacy_settings->mdio_support
403 = link_ksettings->base.mdio_support;
404 legacy_settings->eth_tp_mdix
405 = link_ksettings->base.eth_tp_mdix;
406 legacy_settings->eth_tp_mdix_ctrl
407 = link_ksettings->base.eth_tp_mdix_ctrl;
408 legacy_settings->transceiver
409 = link_ksettings->base.transceiver;
413 /* number of 32-bit words to store the user's link mode bitmaps */
414 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
415 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
417 /* layout of the struct passed from/to userland */
418 struct ethtool_link_usettings {
419 struct ethtool_link_settings base;
421 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
422 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
423 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
427 /* Internal kernel helper to query a device ethtool_link_settings. */
428 int __ethtool_get_link_ksettings(struct net_device *dev,
429 struct ethtool_link_ksettings *link_ksettings)
433 if (!dev->ethtool_ops->get_link_ksettings)
436 memset(link_ksettings, 0, sizeof(*link_ksettings));
437 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
439 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
441 /* convert ethtool_link_usettings in user space to a kernel internal
442 * ethtool_link_ksettings. return 0 on success, errno on error.
444 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
445 const void __user *from)
447 struct ethtool_link_usettings link_usettings;
449 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
452 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
453 bitmap_from_arr32(to->link_modes.supported,
454 link_usettings.link_modes.supported,
455 __ETHTOOL_LINK_MODE_MASK_NBITS);
456 bitmap_from_arr32(to->link_modes.advertising,
457 link_usettings.link_modes.advertising,
458 __ETHTOOL_LINK_MODE_MASK_NBITS);
459 bitmap_from_arr32(to->link_modes.lp_advertising,
460 link_usettings.link_modes.lp_advertising,
461 __ETHTOOL_LINK_MODE_MASK_NBITS);
466 /* Check if the user is trying to change anything besides speed/duplex */
467 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
469 struct ethtool_link_settings base2 = {};
471 base2.speed = cmd->base.speed;
472 base2.port = PORT_OTHER;
473 base2.duplex = cmd->base.duplex;
474 base2.cmd = cmd->base.cmd;
475 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
477 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
478 bitmap_empty(cmd->link_modes.supported,
479 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
480 bitmap_empty(cmd->link_modes.lp_advertising,
481 __ETHTOOL_LINK_MODE_MASK_NBITS);
484 /* convert a kernel internal ethtool_link_ksettings to
485 * ethtool_link_usettings in user space. return 0 on success, errno on
489 store_link_ksettings_for_user(void __user *to,
490 const struct ethtool_link_ksettings *from)
492 struct ethtool_link_usettings link_usettings;
494 memcpy(&link_usettings, from, sizeof(link_usettings));
495 bitmap_to_arr32(link_usettings.link_modes.supported,
496 from->link_modes.supported,
497 __ETHTOOL_LINK_MODE_MASK_NBITS);
498 bitmap_to_arr32(link_usettings.link_modes.advertising,
499 from->link_modes.advertising,
500 __ETHTOOL_LINK_MODE_MASK_NBITS);
501 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
502 from->link_modes.lp_advertising,
503 __ETHTOOL_LINK_MODE_MASK_NBITS);
505 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
511 /* Query device for its ethtool_link_settings. */
512 static int ethtool_get_link_ksettings(struct net_device *dev,
513 void __user *useraddr)
516 struct ethtool_link_ksettings link_ksettings;
519 if (!dev->ethtool_ops->get_link_ksettings)
522 /* handle bitmap nbits handshake */
523 if (copy_from_user(&link_ksettings.base, useraddr,
524 sizeof(link_ksettings.base)))
527 if (__ETHTOOL_LINK_MODE_MASK_NU32
528 != link_ksettings.base.link_mode_masks_nwords) {
529 /* wrong link mode nbits requested */
530 memset(&link_ksettings, 0, sizeof(link_ksettings));
531 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
532 /* send back number of words required as negative val */
533 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
534 "need too many bits for link modes!");
535 link_ksettings.base.link_mode_masks_nwords
536 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
538 /* copy the base fields back to user, not the link
541 if (copy_to_user(useraddr, &link_ksettings.base,
542 sizeof(link_ksettings.base)))
548 /* handshake successful: user/kernel agree on
549 * link_mode_masks_nwords
552 memset(&link_ksettings, 0, sizeof(link_ksettings));
553 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
557 /* make sure we tell the right values to user */
558 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
559 link_ksettings.base.link_mode_masks_nwords
560 = __ETHTOOL_LINK_MODE_MASK_NU32;
561 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
562 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
564 return store_link_ksettings_for_user(useraddr, &link_ksettings);
567 /* Update device ethtool_link_settings. */
568 static int ethtool_set_link_ksettings(struct net_device *dev,
569 void __user *useraddr)
572 struct ethtool_link_ksettings link_ksettings;
576 if (!dev->ethtool_ops->set_link_ksettings)
579 /* make sure nbits field has expected value */
580 if (copy_from_user(&link_ksettings.base, useraddr,
581 sizeof(link_ksettings.base)))
584 if (__ETHTOOL_LINK_MODE_MASK_NU32
585 != link_ksettings.base.link_mode_masks_nwords)
588 /* copy the whole structure, now that we know it has expected
591 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
595 /* re-check nwords field, just in case */
596 if (__ETHTOOL_LINK_MODE_MASK_NU32
597 != link_ksettings.base.link_mode_masks_nwords)
600 if (link_ksettings.base.master_slave_cfg ||
601 link_ksettings.base.master_slave_state)
604 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
606 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
607 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
612 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
613 const struct ethtool_link_ksettings *cmd,
614 u32 *dev_speed, u8 *dev_duplex)
619 speed = cmd->base.speed;
620 duplex = cmd->base.duplex;
621 /* don't allow custom speed and duplex */
622 if (!ethtool_validate_speed(speed) ||
623 !ethtool_validate_duplex(duplex) ||
624 !ethtool_virtdev_validate_cmd(cmd))
627 *dev_duplex = duplex;
631 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
633 /* Query device for its ethtool_cmd settings.
635 * Backward compatibility note: for compatibility with legacy ethtool, this is
636 * now implemented via get_link_ksettings. When driver reports higher link mode
637 * bits, a kernel warning is logged once (with name of 1st driver/device) to
638 * recommend user to upgrade ethtool, but the command is successful (only the
639 * lower link mode bits reported back to user). Deprecated fields from
640 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
642 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
644 struct ethtool_link_ksettings link_ksettings;
645 struct ethtool_cmd cmd;
649 if (!dev->ethtool_ops->get_link_ksettings)
652 memset(&link_ksettings, 0, sizeof(link_ksettings));
653 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
656 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
658 /* send a sensible cmd tag back to user */
659 cmd.cmd = ETHTOOL_GSET;
661 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
667 /* Update device link settings with given ethtool_cmd.
669 * Backward compatibility note: for compatibility with legacy ethtool, this is
670 * now always implemented via set_link_settings. When user's request updates
671 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
672 * warning is logged once (with name of 1st driver/device) to recommend user to
673 * upgrade ethtool, and the request is rejected.
675 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
677 struct ethtool_link_ksettings link_ksettings;
678 struct ethtool_cmd cmd;
683 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
685 if (!dev->ethtool_ops->set_link_ksettings)
688 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
690 link_ksettings.base.link_mode_masks_nwords =
691 __ETHTOOL_LINK_MODE_MASK_NU32;
692 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
694 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
695 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
700 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
701 void __user *useraddr)
703 struct ethtool_drvinfo info;
704 const struct ethtool_ops *ops = dev->ethtool_ops;
706 memset(&info, 0, sizeof(info));
707 info.cmd = ETHTOOL_GDRVINFO;
708 strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
709 if (ops->get_drvinfo) {
710 ops->get_drvinfo(dev, &info);
711 } else if (dev->dev.parent && dev->dev.parent->driver) {
712 strlcpy(info.bus_info, dev_name(dev->dev.parent),
713 sizeof(info.bus_info));
714 strlcpy(info.driver, dev->dev.parent->driver->name,
715 sizeof(info.driver));
721 * this method of obtaining string set info is deprecated;
722 * Use ETHTOOL_GSSET_INFO instead.
724 if (ops->get_sset_count) {
727 rc = ops->get_sset_count(dev, ETH_SS_TEST);
729 info.testinfo_len = rc;
730 rc = ops->get_sset_count(dev, ETH_SS_STATS);
733 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
735 info.n_priv_flags = rc;
737 if (ops->get_regs_len) {
738 int ret = ops->get_regs_len(dev);
741 info.regdump_len = ret;
744 if (ops->get_eeprom_len)
745 info.eedump_len = ops->get_eeprom_len(dev);
747 if (!info.fw_version[0])
748 devlink_compat_running_version(dev, info.fw_version,
749 sizeof(info.fw_version));
751 if (copy_to_user(useraddr, &info, sizeof(info)))
756 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
757 void __user *useraddr)
759 struct ethtool_sset_info info;
761 int i, idx = 0, n_bits = 0, ret, rc;
762 u32 *info_buf = NULL;
764 if (copy_from_user(&info, useraddr, sizeof(info)))
767 /* store copy of mask, because we zero struct later on */
768 sset_mask = info.sset_mask;
772 /* calculate size of return buffer */
773 n_bits = hweight64(sset_mask);
775 memset(&info, 0, sizeof(info));
776 info.cmd = ETHTOOL_GSSET_INFO;
778 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
783 * fill return buffer based on input bitmask and successful
784 * get_sset_count return
786 for (i = 0; i < 64; i++) {
787 if (!(sset_mask & (1ULL << i)))
790 rc = __ethtool_get_sset_count(dev, i);
792 info.sset_mask |= (1ULL << i);
793 info_buf[idx++] = rc;
798 if (copy_to_user(useraddr, &info, sizeof(info)))
801 useraddr += offsetof(struct ethtool_sset_info, data);
802 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
812 static noinline_for_stack int
813 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
814 const struct compat_ethtool_rxnfc __user *useraddr,
817 struct compat_ethtool_rxnfc crxnfc = {};
819 /* We expect there to be holes between fs.m_ext and
820 * fs.ring_cookie and at the end of fs, but nowhere else.
821 * On non-x86, no conversion should be needed.
823 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
824 sizeof(struct compat_ethtool_rxnfc) !=
825 sizeof(struct ethtool_rxnfc));
826 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
827 sizeof(useraddr->fs.m_ext) !=
828 offsetof(struct ethtool_rxnfc, fs.m_ext) +
829 sizeof(rxnfc->fs.m_ext));
830 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
831 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
832 offsetof(struct ethtool_rxnfc, fs.location) -
833 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
835 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
838 *rxnfc = (struct ethtool_rxnfc) {
840 .flow_type = crxnfc.flow_type,
843 .flow_type = crxnfc.fs.flow_type,
844 .h_u = crxnfc.fs.h_u,
845 .h_ext = crxnfc.fs.h_ext,
846 .m_u = crxnfc.fs.m_u,
847 .m_ext = crxnfc.fs.m_ext,
848 .ring_cookie = crxnfc.fs.ring_cookie,
849 .location = crxnfc.fs.location,
851 .rule_cnt = crxnfc.rule_cnt,
857 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
858 const void __user *useraddr,
861 if (compat_need_64bit_alignment_fixup())
862 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
864 if (copy_from_user(rxnfc, useraddr, size))
870 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
871 const struct ethtool_rxnfc *rxnfc,
872 size_t size, const u32 *rule_buf)
874 struct compat_ethtool_rxnfc crxnfc;
876 memset(&crxnfc, 0, sizeof(crxnfc));
877 crxnfc = (struct compat_ethtool_rxnfc) {
879 .flow_type = rxnfc->flow_type,
882 .flow_type = rxnfc->fs.flow_type,
883 .h_u = rxnfc->fs.h_u,
884 .h_ext = rxnfc->fs.h_ext,
885 .m_u = rxnfc->fs.m_u,
886 .m_ext = rxnfc->fs.m_ext,
887 .ring_cookie = rxnfc->fs.ring_cookie,
888 .location = rxnfc->fs.location,
890 .rule_cnt = rxnfc->rule_cnt,
893 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
899 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
900 const struct ethtool_rxnfc *rxnfc,
901 size_t size, const u32 *rule_buf)
905 if (compat_need_64bit_alignment_fixup()) {
906 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
908 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
910 ret = copy_to_user(useraddr, rxnfc, size);
911 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
918 if (copy_to_user(useraddr, rule_buf,
919 rxnfc->rule_cnt * sizeof(u32)))
926 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
927 u32 cmd, void __user *useraddr)
929 struct ethtool_rxnfc info;
930 size_t info_size = sizeof(info);
933 if (!dev->ethtool_ops->set_rxnfc)
936 /* struct ethtool_rxnfc was originally defined for
937 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
938 * members. User-space might still be using that
940 if (cmd == ETHTOOL_SRXFH)
941 info_size = (offsetof(struct ethtool_rxnfc, data) +
944 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
947 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
951 if (cmd == ETHTOOL_SRXCLSRLINS &&
952 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
958 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
959 u32 cmd, void __user *useraddr)
961 struct ethtool_rxnfc info;
962 size_t info_size = sizeof(info);
963 const struct ethtool_ops *ops = dev->ethtool_ops;
965 void *rule_buf = NULL;
970 /* struct ethtool_rxnfc was originally defined for
971 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
972 * members. User-space might still be using that
974 if (cmd == ETHTOOL_GRXFH)
975 info_size = (offsetof(struct ethtool_rxnfc, data) +
978 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
981 /* If FLOW_RSS was requested then user-space must be using the
982 * new definition, as FLOW_RSS is newer.
984 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
985 info_size = sizeof(info);
986 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
988 /* Since malicious users may modify the original data,
989 * we need to check whether FLOW_RSS is still requested.
991 if (!(info.flow_type & FLOW_RSS))
998 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
999 if (info.rule_cnt > 0) {
1000 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1001 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1008 ret = ops->get_rxnfc(dev, &info, rule_buf);
1012 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1019 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1020 struct ethtool_rxnfc *rx_rings,
1025 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1028 /* Validate ring indices */
1029 for (i = 0; i < size; i++)
1030 if (indir[i] >= rx_rings->data)
1036 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1038 void netdev_rss_key_fill(void *buffer, size_t len)
1040 BUG_ON(len > sizeof(netdev_rss_key));
1041 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1042 memcpy(buffer, netdev_rss_key, len);
1044 EXPORT_SYMBOL(netdev_rss_key_fill);
1046 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1047 void __user *useraddr)
1049 u32 user_size, dev_size;
1053 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1054 !dev->ethtool_ops->get_rxfh)
1056 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1060 if (copy_from_user(&user_size,
1061 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1065 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1066 &dev_size, sizeof(dev_size)))
1069 /* If the user buffer size is 0, this is just a query for the
1070 * device table size. Otherwise, if it's smaller than the
1071 * device table size it's an error.
1073 if (user_size < dev_size)
1074 return user_size == 0 ? 0 : -EINVAL;
1076 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1080 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1084 if (copy_to_user(useraddr +
1085 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1086 indir, dev_size * sizeof(indir[0])))
1094 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1095 void __user *useraddr)
1097 struct ethtool_rxnfc rx_rings;
1098 u32 user_size, dev_size, i;
1100 const struct ethtool_ops *ops = dev->ethtool_ops;
1102 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1104 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1108 dev_size = ops->get_rxfh_indir_size(dev);
1112 if (copy_from_user(&user_size,
1113 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1117 if (user_size != 0 && user_size != dev_size)
1120 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1124 rx_rings.cmd = ETHTOOL_GRXRINGS;
1125 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1129 if (user_size == 0) {
1130 for (i = 0; i < dev_size; i++)
1131 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1133 ret = ethtool_copy_validate_indir(indir,
1134 useraddr + ringidx_offset,
1141 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1145 /* indicate whether rxfh was set to default */
1147 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1149 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1156 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1157 void __user *useraddr)
1160 const struct ethtool_ops *ops = dev->ethtool_ops;
1161 u32 user_indir_size, user_key_size;
1162 u32 dev_indir_size = 0, dev_key_size = 0;
1163 struct ethtool_rxfh rxfh;
1174 if (ops->get_rxfh_indir_size)
1175 dev_indir_size = ops->get_rxfh_indir_size(dev);
1176 if (ops->get_rxfh_key_size)
1177 dev_key_size = ops->get_rxfh_key_size(dev);
1179 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1181 user_indir_size = rxfh.indir_size;
1182 user_key_size = rxfh.key_size;
1184 /* Check that reserved fields are 0 for now */
1185 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1187 /* Most drivers don't handle rss_context, check it's 0 as well */
1188 if (rxfh.rss_context && !ops->get_rxfh_context)
1191 rxfh.indir_size = dev_indir_size;
1192 rxfh.key_size = dev_key_size;
1193 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1196 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1197 (user_key_size && (user_key_size != dev_key_size)))
1200 indir_bytes = user_indir_size * sizeof(indir[0]);
1201 total_size = indir_bytes + user_key_size;
1202 rss_config = kzalloc(total_size, GFP_USER);
1206 if (user_indir_size)
1207 indir = (u32 *)rss_config;
1210 hkey = rss_config + indir_bytes;
1212 if (rxfh.rss_context)
1213 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1217 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1221 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1222 &dev_hfunc, sizeof(rxfh.hfunc))) {
1224 } else if (copy_to_user(useraddr +
1225 offsetof(struct ethtool_rxfh, rss_config[0]),
1226 rss_config, total_size)) {
1235 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1236 void __user *useraddr)
1239 const struct ethtool_ops *ops = dev->ethtool_ops;
1240 struct ethtool_rxnfc rx_rings;
1241 struct ethtool_rxfh rxfh;
1242 u32 dev_indir_size = 0, dev_key_size = 0, i;
1243 u32 *indir = NULL, indir_bytes = 0;
1246 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1247 bool delete = false;
1249 if (!ops->get_rxnfc || !ops->set_rxfh)
1252 if (ops->get_rxfh_indir_size)
1253 dev_indir_size = ops->get_rxfh_indir_size(dev);
1254 if (ops->get_rxfh_key_size)
1255 dev_key_size = ops->get_rxfh_key_size(dev);
1257 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1260 /* Check that reserved fields are 0 for now */
1261 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1263 /* Most drivers don't handle rss_context, check it's 0 as well */
1264 if (rxfh.rss_context && !ops->set_rxfh_context)
1267 /* If either indir, hash key or function is valid, proceed further.
1268 * Must request at least one change: indir size, hash key or function.
1270 if ((rxfh.indir_size &&
1271 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1272 rxfh.indir_size != dev_indir_size) ||
1273 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1274 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1275 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1278 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1279 indir_bytes = dev_indir_size * sizeof(indir[0]);
1281 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1285 rx_rings.cmd = ETHTOOL_GRXRINGS;
1286 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1290 /* rxfh.indir_size == 0 means reset the indir table to default (master
1291 * context) or delete the context (other RSS contexts).
1292 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1294 if (rxfh.indir_size &&
1295 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1296 indir = (u32 *)rss_config;
1297 ret = ethtool_copy_validate_indir(indir,
1298 useraddr + rss_cfg_offset,
1303 } else if (rxfh.indir_size == 0) {
1304 if (rxfh.rss_context == 0) {
1305 indir = (u32 *)rss_config;
1306 for (i = 0; i < dev_indir_size; i++)
1307 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1313 if (rxfh.key_size) {
1314 hkey = rss_config + indir_bytes;
1315 if (copy_from_user(hkey,
1316 useraddr + rss_cfg_offset + indir_bytes,
1323 if (rxfh.rss_context)
1324 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1325 &rxfh.rss_context, delete);
1327 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1331 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1332 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1335 if (!rxfh.rss_context) {
1336 /* indicate whether rxfh was set to default */
1337 if (rxfh.indir_size == 0)
1338 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1339 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1340 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1348 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1350 struct ethtool_regs regs;
1351 const struct ethtool_ops *ops = dev->ethtool_ops;
1355 if (!ops->get_regs || !ops->get_regs_len)
1358 if (copy_from_user(®s, useraddr, sizeof(regs)))
1361 reglen = ops->get_regs_len(dev);
1365 if (regs.len > reglen)
1368 regbuf = vzalloc(reglen);
1372 if (regs.len < reglen)
1375 ops->get_regs(dev, ®s, regbuf);
1378 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1380 useraddr += offsetof(struct ethtool_regs, data);
1381 if (copy_to_user(useraddr, regbuf, reglen))
1390 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1392 struct ethtool_value reset;
1395 if (!dev->ethtool_ops->reset)
1398 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1401 ret = dev->ethtool_ops->reset(dev, &reset.data);
1405 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1410 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1412 struct ethtool_wolinfo wol;
1414 if (!dev->ethtool_ops->get_wol)
1417 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1418 wol.cmd = ETHTOOL_GWOL;
1419 dev->ethtool_ops->get_wol(dev, &wol);
1421 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1426 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1428 struct ethtool_wolinfo wol;
1431 if (!dev->ethtool_ops->set_wol)
1434 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1437 ret = dev->ethtool_ops->set_wol(dev, &wol);
1441 dev->wol_enabled = !!wol.wolopts;
1442 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1447 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1449 struct ethtool_eee edata;
1452 if (!dev->ethtool_ops->get_eee)
1455 memset(&edata, 0, sizeof(struct ethtool_eee));
1456 edata.cmd = ETHTOOL_GEEE;
1457 rc = dev->ethtool_ops->get_eee(dev, &edata);
1462 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1468 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1470 struct ethtool_eee edata;
1473 if (!dev->ethtool_ops->set_eee)
1476 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1479 ret = dev->ethtool_ops->set_eee(dev, &edata);
1481 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1485 static int ethtool_nway_reset(struct net_device *dev)
1487 if (!dev->ethtool_ops->nway_reset)
1490 return dev->ethtool_ops->nway_reset(dev);
1493 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1495 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1496 int link = __ethtool_get_link(dev);
1502 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1507 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1508 int (*getter)(struct net_device *,
1509 struct ethtool_eeprom *, u8 *),
1512 struct ethtool_eeprom eeprom;
1513 void __user *userbuf = useraddr + sizeof(eeprom);
1514 u32 bytes_remaining;
1518 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1521 /* Check for wrap and zero */
1522 if (eeprom.offset + eeprom.len <= eeprom.offset)
1525 /* Check for exceeding total eeprom len */
1526 if (eeprom.offset + eeprom.len > total_len)
1529 data = kzalloc(PAGE_SIZE, GFP_USER);
1533 bytes_remaining = eeprom.len;
1534 while (bytes_remaining > 0) {
1535 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1537 ret = getter(dev, &eeprom, data);
1540 if (copy_to_user(userbuf, data, eeprom.len)) {
1544 userbuf += eeprom.len;
1545 eeprom.offset += eeprom.len;
1546 bytes_remaining -= eeprom.len;
1549 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1550 eeprom.offset -= eeprom.len;
1551 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1558 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1560 const struct ethtool_ops *ops = dev->ethtool_ops;
1562 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1563 !ops->get_eeprom_len(dev))
1566 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1567 ops->get_eeprom_len(dev));
1570 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1572 struct ethtool_eeprom eeprom;
1573 const struct ethtool_ops *ops = dev->ethtool_ops;
1574 void __user *userbuf = useraddr + sizeof(eeprom);
1575 u32 bytes_remaining;
1579 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1580 !ops->get_eeprom_len(dev))
1583 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1586 /* Check for wrap and zero */
1587 if (eeprom.offset + eeprom.len <= eeprom.offset)
1590 /* Check for exceeding total eeprom len */
1591 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1594 data = kzalloc(PAGE_SIZE, GFP_USER);
1598 bytes_remaining = eeprom.len;
1599 while (bytes_remaining > 0) {
1600 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1602 if (copy_from_user(data, userbuf, eeprom.len)) {
1606 ret = ops->set_eeprom(dev, &eeprom, data);
1609 userbuf += eeprom.len;
1610 eeprom.offset += eeprom.len;
1611 bytes_remaining -= eeprom.len;
1618 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1619 void __user *useraddr)
1621 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1622 struct kernel_ethtool_coalesce kernel_coalesce = {};
1625 if (!dev->ethtool_ops->get_coalesce)
1628 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1633 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1639 ethtool_set_coalesce_supported(struct net_device *dev,
1640 struct ethtool_coalesce *coalesce)
1642 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1643 u32 nonzero_params = 0;
1645 if (coalesce->rx_coalesce_usecs)
1646 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1647 if (coalesce->rx_max_coalesced_frames)
1648 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1649 if (coalesce->rx_coalesce_usecs_irq)
1650 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1651 if (coalesce->rx_max_coalesced_frames_irq)
1652 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1653 if (coalesce->tx_coalesce_usecs)
1654 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1655 if (coalesce->tx_max_coalesced_frames)
1656 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1657 if (coalesce->tx_coalesce_usecs_irq)
1658 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1659 if (coalesce->tx_max_coalesced_frames_irq)
1660 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1661 if (coalesce->stats_block_coalesce_usecs)
1662 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1663 if (coalesce->use_adaptive_rx_coalesce)
1664 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1665 if (coalesce->use_adaptive_tx_coalesce)
1666 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1667 if (coalesce->pkt_rate_low)
1668 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1669 if (coalesce->rx_coalesce_usecs_low)
1670 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1671 if (coalesce->rx_max_coalesced_frames_low)
1672 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1673 if (coalesce->tx_coalesce_usecs_low)
1674 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1675 if (coalesce->tx_max_coalesced_frames_low)
1676 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1677 if (coalesce->pkt_rate_high)
1678 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1679 if (coalesce->rx_coalesce_usecs_high)
1680 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1681 if (coalesce->rx_max_coalesced_frames_high)
1682 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1683 if (coalesce->tx_coalesce_usecs_high)
1684 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1685 if (coalesce->tx_max_coalesced_frames_high)
1686 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1687 if (coalesce->rate_sample_interval)
1688 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1690 return (supported_params & nonzero_params) == nonzero_params;
1693 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1694 void __user *useraddr)
1696 struct kernel_ethtool_coalesce kernel_coalesce = {};
1697 struct ethtool_coalesce coalesce;
1700 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1703 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1708 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1711 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1714 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1717 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1721 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1723 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1725 if (!dev->ethtool_ops->get_ringparam)
1728 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1730 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1735 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1737 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1740 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1743 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1746 dev->ethtool_ops->get_ringparam(dev, &max);
1748 /* ensure new ring parameters are within the maximums */
1749 if (ringparam.rx_pending > max.rx_max_pending ||
1750 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1751 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1752 ringparam.tx_pending > max.tx_max_pending)
1755 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1757 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1761 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1762 void __user *useraddr)
1764 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1766 if (!dev->ethtool_ops->get_channels)
1769 dev->ethtool_ops->get_channels(dev, &channels);
1771 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1776 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1777 void __user *useraddr)
1779 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1780 u16 from_channel, to_channel;
1781 u32 max_rx_in_use = 0;
1785 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1788 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1791 dev->ethtool_ops->get_channels(dev, &curr);
1793 if (channels.rx_count == curr.rx_count &&
1794 channels.tx_count == curr.tx_count &&
1795 channels.combined_count == curr.combined_count &&
1796 channels.other_count == curr.other_count)
1799 /* ensure new counts are within the maximums */
1800 if (channels.rx_count > curr.max_rx ||
1801 channels.tx_count > curr.max_tx ||
1802 channels.combined_count > curr.max_combined ||
1803 channels.other_count > curr.max_other)
1806 /* ensure there is at least one RX and one TX channel */
1807 if (!channels.combined_count &&
1808 (!channels.rx_count || !channels.tx_count))
1811 /* ensure the new Rx count fits within the configured Rx flow
1812 * indirection table settings */
1813 if (netif_is_rxfh_configured(dev) &&
1814 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1815 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1818 /* Disabling channels, query zero-copy AF_XDP sockets */
1819 from_channel = channels.combined_count +
1820 min(channels.rx_count, channels.tx_count);
1821 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1822 for (i = from_channel; i < to_channel; i++)
1823 if (xsk_get_pool_from_qid(dev, i))
1826 ret = dev->ethtool_ops->set_channels(dev, &channels);
1828 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1832 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1834 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1836 if (!dev->ethtool_ops->get_pauseparam)
1839 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1841 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1846 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1848 struct ethtool_pauseparam pauseparam;
1851 if (!dev->ethtool_ops->set_pauseparam)
1854 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1857 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1859 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1863 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1865 struct ethtool_test test;
1866 const struct ethtool_ops *ops = dev->ethtool_ops;
1870 if (!ops->self_test || !ops->get_sset_count)
1873 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1876 WARN_ON(test_len == 0);
1878 if (copy_from_user(&test, useraddr, sizeof(test)))
1881 test.len = test_len;
1882 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1886 netif_testing_on(dev);
1887 ops->self_test(dev, &test, data);
1888 netif_testing_off(dev);
1891 if (copy_to_user(useraddr, &test, sizeof(test)))
1893 useraddr += sizeof(test);
1894 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1903 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1905 struct ethtool_gstrings gstrings;
1909 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1912 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1915 if (ret > S32_MAX / ETH_GSTRING_LEN)
1922 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1926 __ethtool_get_strings(dev, gstrings.string_set, data);
1932 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1934 useraddr += sizeof(gstrings);
1936 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1945 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1949 va_start(args, fmt);
1950 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1953 *data += ETH_GSTRING_LEN;
1955 EXPORT_SYMBOL(ethtool_sprintf);
1957 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1959 struct ethtool_value id;
1961 const struct ethtool_ops *ops = dev->ethtool_ops;
1964 if (!ops->set_phys_id)
1970 if (copy_from_user(&id, useraddr, sizeof(id)))
1973 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1977 /* Drop the RTNL lock while waiting, but prevent reentry or
1978 * removal of the device.
1985 /* Driver will handle this itself */
1986 schedule_timeout_interruptible(
1987 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1989 /* Driver expects to be called at twice the frequency in rc */
1990 int n = rc * 2, interval = HZ / n;
1991 u64 count = mul_u32_u32(n, id.data);
1996 rc = ops->set_phys_id(dev,
1997 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2001 schedule_timeout_interruptible(interval);
2002 } while (!signal_pending(current) && (!id.data || i < count));
2009 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2013 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2015 struct ethtool_stats stats;
2016 const struct ethtool_ops *ops = dev->ethtool_ops;
2020 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2023 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2026 if (n_stats > S32_MAX / sizeof(u64))
2028 WARN_ON_ONCE(!n_stats);
2029 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2032 stats.n_stats = n_stats;
2035 data = vzalloc(array_size(n_stats, sizeof(u64)));
2038 ops->get_ethtool_stats(dev, &stats, data);
2044 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2046 useraddr += sizeof(stats);
2047 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2056 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2058 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2059 const struct ethtool_ops *ops = dev->ethtool_ops;
2060 struct phy_device *phydev = dev->phydev;
2061 struct ethtool_stats stats;
2065 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
2068 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2069 phy_ops && phy_ops->get_sset_count)
2070 n_stats = phy_ops->get_sset_count(dev->phydev);
2072 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2075 if (n_stats > S32_MAX / sizeof(u64))
2077 if (WARN_ON_ONCE(!n_stats))
2080 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2083 stats.n_stats = n_stats;
2086 data = vzalloc(array_size(n_stats, sizeof(u64)));
2090 if (dev->phydev && !ops->get_ethtool_phy_stats &&
2091 phy_ops && phy_ops->get_stats) {
2092 ret = phy_ops->get_stats(dev->phydev, &stats, data);
2096 ops->get_ethtool_phy_stats(dev, &stats, data);
2103 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2105 useraddr += sizeof(stats);
2106 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2115 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2117 struct ethtool_perm_addr epaddr;
2119 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2122 if (epaddr.size < dev->addr_len)
2124 epaddr.size = dev->addr_len;
2126 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2128 useraddr += sizeof(epaddr);
2129 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2134 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2135 u32 cmd, u32 (*actor)(struct net_device *))
2137 struct ethtool_value edata = { .cmd = cmd };
2142 edata.data = actor(dev);
2144 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2149 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2150 void (*actor)(struct net_device *, u32))
2152 struct ethtool_value edata;
2157 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2160 actor(dev, edata.data);
2164 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2165 int (*actor)(struct net_device *, u32))
2167 struct ethtool_value edata;
2172 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2175 return actor(dev, edata.data);
2178 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2179 char __user *useraddr)
2181 struct ethtool_flash efl;
2183 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2185 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2187 if (!dev->ethtool_ops->flash_device)
2188 return devlink_compat_flash_update(dev, efl.data);
2190 return dev->ethtool_ops->flash_device(dev, &efl);
2193 static int ethtool_set_dump(struct net_device *dev,
2194 void __user *useraddr)
2196 struct ethtool_dump dump;
2198 if (!dev->ethtool_ops->set_dump)
2201 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2204 return dev->ethtool_ops->set_dump(dev, &dump);
2207 static int ethtool_get_dump_flag(struct net_device *dev,
2208 void __user *useraddr)
2211 struct ethtool_dump dump;
2212 const struct ethtool_ops *ops = dev->ethtool_ops;
2214 if (!ops->get_dump_flag)
2217 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2220 ret = ops->get_dump_flag(dev, &dump);
2224 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2229 static int ethtool_get_dump_data(struct net_device *dev,
2230 void __user *useraddr)
2234 struct ethtool_dump dump, tmp;
2235 const struct ethtool_ops *ops = dev->ethtool_ops;
2238 if (!ops->get_dump_data || !ops->get_dump_flag)
2241 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2244 memset(&tmp, 0, sizeof(tmp));
2245 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2246 ret = ops->get_dump_flag(dev, &tmp);
2250 len = min(tmp.len, dump.len);
2254 /* Don't ever let the driver think there's more space available
2255 * than it requested with .get_dump_flag().
2259 /* Always allocate enough space to hold the whole thing so that the
2260 * driver does not need to check the length and bother with partial
2263 data = vzalloc(tmp.len);
2266 ret = ops->get_dump_data(dev, &dump, data);
2270 /* There are two sane possibilities:
2271 * 1. The driver's .get_dump_data() does not touch dump.len.
2272 * 2. Or it may set dump.len to how much it really writes, which
2273 * should be tmp.len (or len if it can do a partial dump).
2274 * In any case respond to userspace with the actual length of data
2277 WARN_ON(dump.len != len && dump.len != tmp.len);
2280 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2284 useraddr += offsetof(struct ethtool_dump, data);
2285 if (copy_to_user(useraddr, data, len))
2292 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2294 struct ethtool_ts_info info;
2297 err = __ethtool_get_ts_info(dev, &info);
2301 if (copy_to_user(useraddr, &info, sizeof(info)))
2307 int ethtool_get_module_info_call(struct net_device *dev,
2308 struct ethtool_modinfo *modinfo)
2310 const struct ethtool_ops *ops = dev->ethtool_ops;
2311 struct phy_device *phydev = dev->phydev;
2314 return sfp_get_module_info(dev->sfp_bus, modinfo);
2316 if (phydev && phydev->drv && phydev->drv->module_info)
2317 return phydev->drv->module_info(phydev, modinfo);
2319 if (ops->get_module_info)
2320 return ops->get_module_info(dev, modinfo);
2325 static int ethtool_get_module_info(struct net_device *dev,
2326 void __user *useraddr)
2329 struct ethtool_modinfo modinfo;
2331 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2334 ret = ethtool_get_module_info_call(dev, &modinfo);
2338 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2344 int ethtool_get_module_eeprom_call(struct net_device *dev,
2345 struct ethtool_eeprom *ee, u8 *data)
2347 const struct ethtool_ops *ops = dev->ethtool_ops;
2348 struct phy_device *phydev = dev->phydev;
2351 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2353 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2354 return phydev->drv->module_eeprom(phydev, ee, data);
2356 if (ops->get_module_eeprom)
2357 return ops->get_module_eeprom(dev, ee, data);
2362 static int ethtool_get_module_eeprom(struct net_device *dev,
2363 void __user *useraddr)
2366 struct ethtool_modinfo modinfo;
2368 ret = ethtool_get_module_info_call(dev, &modinfo);
2372 return ethtool_get_any_eeprom(dev, useraddr,
2373 ethtool_get_module_eeprom_call,
2374 modinfo.eeprom_len);
2377 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2380 case ETHTOOL_RX_COPYBREAK:
2381 case ETHTOOL_TX_COPYBREAK:
2382 if (tuna->len != sizeof(u32) ||
2383 tuna->type_id != ETHTOOL_TUNABLE_U32)
2386 case ETHTOOL_PFC_PREVENTION_TOUT:
2387 if (tuna->len != sizeof(u16) ||
2388 tuna->type_id != ETHTOOL_TUNABLE_U16)
2398 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2401 struct ethtool_tunable tuna;
2402 const struct ethtool_ops *ops = dev->ethtool_ops;
2405 if (!ops->get_tunable)
2407 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2409 ret = ethtool_tunable_valid(&tuna);
2412 data = kzalloc(tuna.len, GFP_USER);
2415 ret = ops->get_tunable(dev, &tuna, data);
2418 useraddr += sizeof(tuna);
2420 if (copy_to_user(useraddr, data, tuna.len))
2429 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2432 struct ethtool_tunable tuna;
2433 const struct ethtool_ops *ops = dev->ethtool_ops;
2436 if (!ops->set_tunable)
2438 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2440 ret = ethtool_tunable_valid(&tuna);
2443 useraddr += sizeof(tuna);
2444 data = memdup_user(useraddr, tuna.len);
2446 return PTR_ERR(data);
2447 ret = ops->set_tunable(dev, &tuna, data);
2453 static noinline_for_stack int
2454 ethtool_get_per_queue_coalesce(struct net_device *dev,
2455 void __user *useraddr,
2456 struct ethtool_per_queue_op *per_queue_opt)
2460 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2462 if (!dev->ethtool_ops->get_per_queue_coalesce)
2465 useraddr += sizeof(*per_queue_opt);
2467 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2470 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2471 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2473 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2476 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2478 useraddr += sizeof(coalesce);
2484 static noinline_for_stack int
2485 ethtool_set_per_queue_coalesce(struct net_device *dev,
2486 void __user *useraddr,
2487 struct ethtool_per_queue_op *per_queue_opt)
2492 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2493 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2495 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2496 (!dev->ethtool_ops->get_per_queue_coalesce))
2499 useraddr += sizeof(*per_queue_opt);
2501 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2502 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2503 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2507 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2508 struct ethtool_coalesce coalesce;
2510 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2516 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2521 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2526 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2530 useraddr += sizeof(coalesce);
2536 for_each_set_bit(i, queue_mask, bit) {
2537 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2546 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2547 void __user *useraddr, u32 sub_cmd)
2549 struct ethtool_per_queue_op per_queue_opt;
2551 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2554 if (per_queue_opt.sub_command != sub_cmd)
2557 switch (per_queue_opt.sub_command) {
2558 case ETHTOOL_GCOALESCE:
2559 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2560 case ETHTOOL_SCOALESCE:
2561 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2567 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2570 case ETHTOOL_PHY_DOWNSHIFT:
2571 case ETHTOOL_PHY_FAST_LINK_DOWN:
2572 if (tuna->len != sizeof(u8) ||
2573 tuna->type_id != ETHTOOL_TUNABLE_U8)
2576 case ETHTOOL_PHY_EDPD:
2577 if (tuna->len != sizeof(u16) ||
2578 tuna->type_id != ETHTOOL_TUNABLE_U16)
2588 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2590 struct phy_device *phydev = dev->phydev;
2591 struct ethtool_tunable tuna;
2592 bool phy_drv_tunable;
2596 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2597 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2599 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2601 ret = ethtool_phy_tunable_valid(&tuna);
2604 data = kzalloc(tuna.len, GFP_USER);
2607 if (phy_drv_tunable) {
2608 mutex_lock(&phydev->lock);
2609 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2610 mutex_unlock(&phydev->lock);
2612 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2616 useraddr += sizeof(tuna);
2618 if (copy_to_user(useraddr, data, tuna.len))
2627 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2629 struct phy_device *phydev = dev->phydev;
2630 struct ethtool_tunable tuna;
2631 bool phy_drv_tunable;
2635 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2636 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2638 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2640 ret = ethtool_phy_tunable_valid(&tuna);
2643 useraddr += sizeof(tuna);
2644 data = memdup_user(useraddr, tuna.len);
2646 return PTR_ERR(data);
2647 if (phy_drv_tunable) {
2648 mutex_lock(&phydev->lock);
2649 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2650 mutex_unlock(&phydev->lock);
2652 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2659 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2661 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2664 if (!dev->ethtool_ops->get_fecparam)
2667 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2671 if (WARN_ON_ONCE(fecparam.reserved))
2672 fecparam.reserved = 0;
2674 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2679 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2681 struct ethtool_fecparam fecparam;
2683 if (!dev->ethtool_ops->set_fecparam)
2686 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2689 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2692 fecparam.active_fec = 0;
2693 fecparam.reserved = 0;
2695 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2698 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2700 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
2702 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2703 u32 ethcmd, sub_cmd;
2705 netdev_features_t old_features;
2710 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2713 if (ethcmd == ETHTOOL_PERQUEUE) {
2714 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2719 /* Allow some commands to be done by anyone */
2722 case ETHTOOL_GDRVINFO:
2723 case ETHTOOL_GMSGLVL:
2725 case ETHTOOL_GCOALESCE:
2726 case ETHTOOL_GRINGPARAM:
2727 case ETHTOOL_GPAUSEPARAM:
2728 case ETHTOOL_GRXCSUM:
2729 case ETHTOOL_GTXCSUM:
2731 case ETHTOOL_GSSET_INFO:
2732 case ETHTOOL_GSTRINGS:
2733 case ETHTOOL_GSTATS:
2734 case ETHTOOL_GPHYSTATS:
2736 case ETHTOOL_GPERMADDR:
2740 case ETHTOOL_GFLAGS:
2741 case ETHTOOL_GPFLAGS:
2743 case ETHTOOL_GRXRINGS:
2744 case ETHTOOL_GRXCLSRLCNT:
2745 case ETHTOOL_GRXCLSRULE:
2746 case ETHTOOL_GRXCLSRLALL:
2747 case ETHTOOL_GRXFHINDIR:
2749 case ETHTOOL_GFEATURES:
2750 case ETHTOOL_GCHANNELS:
2751 case ETHTOOL_GET_TS_INFO:
2753 case ETHTOOL_GTUNABLE:
2754 case ETHTOOL_PHY_GTUNABLE:
2755 case ETHTOOL_GLINKSETTINGS:
2756 case ETHTOOL_GFECPARAM:
2759 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2763 if (dev->dev.parent)
2764 pm_runtime_get_sync(dev->dev.parent);
2766 if (!netif_device_present(dev)) {
2771 if (dev->ethtool_ops->begin) {
2772 rc = dev->ethtool_ops->begin(dev);
2776 old_features = dev->features;
2780 rc = ethtool_get_settings(dev, useraddr);
2783 rc = ethtool_set_settings(dev, useraddr);
2785 case ETHTOOL_GDRVINFO:
2786 rc = ethtool_get_drvinfo(dev, useraddr);
2789 rc = ethtool_get_regs(dev, useraddr);
2792 rc = ethtool_get_wol(dev, useraddr);
2795 rc = ethtool_set_wol(dev, useraddr);
2797 case ETHTOOL_GMSGLVL:
2798 rc = ethtool_get_value(dev, useraddr, ethcmd,
2799 dev->ethtool_ops->get_msglevel);
2801 case ETHTOOL_SMSGLVL:
2802 rc = ethtool_set_value_void(dev, useraddr,
2803 dev->ethtool_ops->set_msglevel);
2805 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2808 rc = ethtool_get_eee(dev, useraddr);
2811 rc = ethtool_set_eee(dev, useraddr);
2813 case ETHTOOL_NWAY_RST:
2814 rc = ethtool_nway_reset(dev);
2817 rc = ethtool_get_link(dev, useraddr);
2819 case ETHTOOL_GEEPROM:
2820 rc = ethtool_get_eeprom(dev, useraddr);
2822 case ETHTOOL_SEEPROM:
2823 rc = ethtool_set_eeprom(dev, useraddr);
2825 case ETHTOOL_GCOALESCE:
2826 rc = ethtool_get_coalesce(dev, useraddr);
2828 case ETHTOOL_SCOALESCE:
2829 rc = ethtool_set_coalesce(dev, useraddr);
2831 case ETHTOOL_GRINGPARAM:
2832 rc = ethtool_get_ringparam(dev, useraddr);
2834 case ETHTOOL_SRINGPARAM:
2835 rc = ethtool_set_ringparam(dev, useraddr);
2837 case ETHTOOL_GPAUSEPARAM:
2838 rc = ethtool_get_pauseparam(dev, useraddr);
2840 case ETHTOOL_SPAUSEPARAM:
2841 rc = ethtool_set_pauseparam(dev, useraddr);
2844 rc = ethtool_self_test(dev, useraddr);
2846 case ETHTOOL_GSTRINGS:
2847 rc = ethtool_get_strings(dev, useraddr);
2849 case ETHTOOL_PHYS_ID:
2850 rc = ethtool_phys_id(dev, useraddr);
2852 case ETHTOOL_GSTATS:
2853 rc = ethtool_get_stats(dev, useraddr);
2855 case ETHTOOL_GPERMADDR:
2856 rc = ethtool_get_perm_addr(dev, useraddr);
2858 case ETHTOOL_GFLAGS:
2859 rc = ethtool_get_value(dev, useraddr, ethcmd,
2860 __ethtool_get_flags);
2862 case ETHTOOL_SFLAGS:
2863 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2865 case ETHTOOL_GPFLAGS:
2866 rc = ethtool_get_value(dev, useraddr, ethcmd,
2867 dev->ethtool_ops->get_priv_flags);
2869 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2871 case ETHTOOL_SPFLAGS:
2872 rc = ethtool_set_value(dev, useraddr,
2873 dev->ethtool_ops->set_priv_flags);
2876 case ETHTOOL_GRXRINGS:
2877 case ETHTOOL_GRXCLSRLCNT:
2878 case ETHTOOL_GRXCLSRULE:
2879 case ETHTOOL_GRXCLSRLALL:
2880 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2883 case ETHTOOL_SRXCLSRLDEL:
2884 case ETHTOOL_SRXCLSRLINS:
2885 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2887 case ETHTOOL_FLASHDEV:
2888 rc = ethtool_flash_device(dev, useraddr);
2891 rc = ethtool_reset(dev, useraddr);
2893 case ETHTOOL_GSSET_INFO:
2894 rc = ethtool_get_sset_info(dev, useraddr);
2896 case ETHTOOL_GRXFHINDIR:
2897 rc = ethtool_get_rxfh_indir(dev, useraddr);
2899 case ETHTOOL_SRXFHINDIR:
2900 rc = ethtool_set_rxfh_indir(dev, useraddr);
2903 rc = ethtool_get_rxfh(dev, useraddr);
2906 rc = ethtool_set_rxfh(dev, useraddr);
2908 case ETHTOOL_GFEATURES:
2909 rc = ethtool_get_features(dev, useraddr);
2911 case ETHTOOL_SFEATURES:
2912 rc = ethtool_set_features(dev, useraddr);
2914 case ETHTOOL_GTXCSUM:
2915 case ETHTOOL_GRXCSUM:
2920 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2922 case ETHTOOL_STXCSUM:
2923 case ETHTOOL_SRXCSUM:
2928 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2930 case ETHTOOL_GCHANNELS:
2931 rc = ethtool_get_channels(dev, useraddr);
2933 case ETHTOOL_SCHANNELS:
2934 rc = ethtool_set_channels(dev, useraddr);
2936 case ETHTOOL_SET_DUMP:
2937 rc = ethtool_set_dump(dev, useraddr);
2939 case ETHTOOL_GET_DUMP_FLAG:
2940 rc = ethtool_get_dump_flag(dev, useraddr);
2942 case ETHTOOL_GET_DUMP_DATA:
2943 rc = ethtool_get_dump_data(dev, useraddr);
2945 case ETHTOOL_GET_TS_INFO:
2946 rc = ethtool_get_ts_info(dev, useraddr);
2948 case ETHTOOL_GMODULEINFO:
2949 rc = ethtool_get_module_info(dev, useraddr);
2951 case ETHTOOL_GMODULEEEPROM:
2952 rc = ethtool_get_module_eeprom(dev, useraddr);
2954 case ETHTOOL_GTUNABLE:
2955 rc = ethtool_get_tunable(dev, useraddr);
2957 case ETHTOOL_STUNABLE:
2958 rc = ethtool_set_tunable(dev, useraddr);
2960 case ETHTOOL_GPHYSTATS:
2961 rc = ethtool_get_phy_stats(dev, useraddr);
2963 case ETHTOOL_PERQUEUE:
2964 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2966 case ETHTOOL_GLINKSETTINGS:
2967 rc = ethtool_get_link_ksettings(dev, useraddr);
2969 case ETHTOOL_SLINKSETTINGS:
2970 rc = ethtool_set_link_ksettings(dev, useraddr);
2972 case ETHTOOL_PHY_GTUNABLE:
2973 rc = get_phy_tunable(dev, useraddr);
2975 case ETHTOOL_PHY_STUNABLE:
2976 rc = set_phy_tunable(dev, useraddr);
2978 case ETHTOOL_GFECPARAM:
2979 rc = ethtool_get_fecparam(dev, useraddr);
2981 case ETHTOOL_SFECPARAM:
2982 rc = ethtool_set_fecparam(dev, useraddr);
2988 if (dev->ethtool_ops->complete)
2989 dev->ethtool_ops->complete(dev);
2991 if (old_features != dev->features)
2992 netdev_features_change(dev);
2994 if (dev->dev.parent)
2995 pm_runtime_put(dev->dev.parent);
3000 struct ethtool_rx_flow_key {
3001 struct flow_dissector_key_basic basic;
3003 struct flow_dissector_key_ipv4_addrs ipv4;
3004 struct flow_dissector_key_ipv6_addrs ipv6;
3006 struct flow_dissector_key_ports tp;
3007 struct flow_dissector_key_ip ip;
3008 struct flow_dissector_key_vlan vlan;
3009 struct flow_dissector_key_eth_addrs eth_addrs;
3010 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3012 struct ethtool_rx_flow_match {
3013 struct flow_dissector dissector;
3014 struct ethtool_rx_flow_key key;
3015 struct ethtool_rx_flow_key mask;
3018 struct ethtool_rx_flow_rule *
3019 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3021 const struct ethtool_rx_flow_spec *fs = input->fs;
3022 static struct in6_addr zero_addr = {};
3023 struct ethtool_rx_flow_match *match;
3024 struct ethtool_rx_flow_rule *flow;
3025 struct flow_action_entry *act;
3027 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3028 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3030 return ERR_PTR(-ENOMEM);
3032 /* ethtool_rx supports only one single action per rule. */
3033 flow->rule = flow_rule_alloc(1);
3036 return ERR_PTR(-ENOMEM);
3039 match = (struct ethtool_rx_flow_match *)flow->priv;
3040 flow->rule->match.dissector = &match->dissector;
3041 flow->rule->match.mask = &match->mask;
3042 flow->rule->match.key = &match->key;
3044 match->mask.basic.n_proto = htons(0xffff);
3046 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3048 const struct ethhdr *ether_spec, *ether_m_spec;
3050 ether_spec = &fs->h_u.ether_spec;
3051 ether_m_spec = &fs->m_u.ether_spec;
3053 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3054 ether_addr_copy(match->key.eth_addrs.src,
3055 ether_spec->h_source);
3056 ether_addr_copy(match->mask.eth_addrs.src,
3057 ether_m_spec->h_source);
3059 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3060 ether_addr_copy(match->key.eth_addrs.dst,
3061 ether_spec->h_dest);
3062 ether_addr_copy(match->mask.eth_addrs.dst,
3063 ether_m_spec->h_dest);
3065 if (ether_m_spec->h_proto) {
3066 match->key.basic.n_proto = ether_spec->h_proto;
3067 match->mask.basic.n_proto = ether_m_spec->h_proto;
3073 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3075 match->key.basic.n_proto = htons(ETH_P_IP);
3077 v4_spec = &fs->h_u.tcp_ip4_spec;
3078 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3080 if (v4_m_spec->ip4src) {
3081 match->key.ipv4.src = v4_spec->ip4src;
3082 match->mask.ipv4.src = v4_m_spec->ip4src;
3084 if (v4_m_spec->ip4dst) {
3085 match->key.ipv4.dst = v4_spec->ip4dst;
3086 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3088 if (v4_m_spec->ip4src ||
3089 v4_m_spec->ip4dst) {
3090 match->dissector.used_keys |=
3091 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3092 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3093 offsetof(struct ethtool_rx_flow_key, ipv4);
3095 if (v4_m_spec->psrc) {
3096 match->key.tp.src = v4_spec->psrc;
3097 match->mask.tp.src = v4_m_spec->psrc;
3099 if (v4_m_spec->pdst) {
3100 match->key.tp.dst = v4_spec->pdst;
3101 match->mask.tp.dst = v4_m_spec->pdst;
3103 if (v4_m_spec->psrc ||
3105 match->dissector.used_keys |=
3106 BIT(FLOW_DISSECTOR_KEY_PORTS);
3107 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3108 offsetof(struct ethtool_rx_flow_key, tp);
3110 if (v4_m_spec->tos) {
3111 match->key.ip.tos = v4_spec->tos;
3112 match->mask.ip.tos = v4_m_spec->tos;
3113 match->dissector.used_keys |=
3114 BIT(FLOW_DISSECTOR_KEY_IP);
3115 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3116 offsetof(struct ethtool_rx_flow_key, ip);
3122 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3124 match->key.basic.n_proto = htons(ETH_P_IPV6);
3126 v6_spec = &fs->h_u.tcp_ip6_spec;
3127 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3128 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
3129 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3130 sizeof(match->key.ipv6.src));
3131 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3132 sizeof(match->mask.ipv6.src));
3134 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3135 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3136 sizeof(match->key.ipv6.dst));
3137 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3138 sizeof(match->mask.ipv6.dst));
3140 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
3141 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3142 match->dissector.used_keys |=
3143 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3144 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3145 offsetof(struct ethtool_rx_flow_key, ipv6);
3147 if (v6_m_spec->psrc) {
3148 match->key.tp.src = v6_spec->psrc;
3149 match->mask.tp.src = v6_m_spec->psrc;
3151 if (v6_m_spec->pdst) {
3152 match->key.tp.dst = v6_spec->pdst;
3153 match->mask.tp.dst = v6_m_spec->pdst;
3155 if (v6_m_spec->psrc ||
3157 match->dissector.used_keys |=
3158 BIT(FLOW_DISSECTOR_KEY_PORTS);
3159 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3160 offsetof(struct ethtool_rx_flow_key, tp);
3162 if (v6_m_spec->tclass) {
3163 match->key.ip.tos = v6_spec->tclass;
3164 match->mask.ip.tos = v6_m_spec->tclass;
3165 match->dissector.used_keys |=
3166 BIT(FLOW_DISSECTOR_KEY_IP);
3167 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3168 offsetof(struct ethtool_rx_flow_key, ip);
3173 ethtool_rx_flow_rule_destroy(flow);
3174 return ERR_PTR(-EINVAL);
3177 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3180 match->key.basic.ip_proto = IPPROTO_TCP;
3181 match->mask.basic.ip_proto = 0xff;
3185 match->key.basic.ip_proto = IPPROTO_UDP;
3186 match->mask.basic.ip_proto = 0xff;
3190 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3191 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3192 offsetof(struct ethtool_rx_flow_key, basic);
3194 if (fs->flow_type & FLOW_EXT) {
3195 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3196 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3198 if (ext_m_spec->vlan_etype) {
3199 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3200 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3203 if (ext_m_spec->vlan_tci) {
3204 match->key.vlan.vlan_id =
3205 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3206 match->mask.vlan.vlan_id =
3207 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3209 match->key.vlan.vlan_dei =
3210 !!(ext_h_spec->vlan_tci & htons(0x1000));
3211 match->mask.vlan.vlan_dei =
3212 !!(ext_m_spec->vlan_tci & htons(0x1000));
3214 match->key.vlan.vlan_priority =
3215 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3216 match->mask.vlan.vlan_priority =
3217 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3220 if (ext_m_spec->vlan_etype ||
3221 ext_m_spec->vlan_tci) {
3222 match->dissector.used_keys |=
3223 BIT(FLOW_DISSECTOR_KEY_VLAN);
3224 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3225 offsetof(struct ethtool_rx_flow_key, vlan);
3228 if (fs->flow_type & FLOW_MAC_EXT) {
3229 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3230 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3232 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3234 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3237 match->dissector.used_keys |=
3238 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3239 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3240 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3243 act = &flow->rule->action.entries[0];
3244 switch (fs->ring_cookie) {
3245 case RX_CLS_FLOW_DISC:
3246 act->id = FLOW_ACTION_DROP;
3248 case RX_CLS_FLOW_WAKE:
3249 act->id = FLOW_ACTION_WAKE;
3252 act->id = FLOW_ACTION_QUEUE;
3253 if (fs->flow_type & FLOW_RSS)
3254 act->queue.ctx = input->rss_ctx;
3256 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3257 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3263 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3265 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3270 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);