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/etherdevice.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/ethtool.h>
17 #include <linux/netdevice.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/phy.h>
20 #include <linux/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/sfp.h>
24 #include <linux/slab.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/sched/signal.h>
27 #include <linux/net.h>
28 #include <linux/pm_runtime.h>
29 #include <net/devlink.h>
30 #include <net/xdp_sock_drv.h>
31 #include <net/flow_offload.h>
32 #include <linux/ethtool_netlink.h>
33 #include <generated/utsrelease.h>
36 /* State held across locks and calls for commands which have devlink fallback */
37 struct ethtool_devlink_compat {
38 struct devlink *devlink;
40 struct ethtool_flash efl;
41 struct ethtool_drvinfo info;
45 static struct devlink *netdev_to_devlink_get(struct net_device *dev)
47 struct devlink_port *devlink_port;
49 if (!dev->netdev_ops->ndo_get_devlink_port)
52 devlink_port = dev->netdev_ops->ndo_get_devlink_port(dev);
56 return devlink_try_get(devlink_port->devlink);
60 * Some useful ethtool_ops methods that're device independent.
61 * If we find that all drivers want to do the same thing here,
62 * we can turn these into dev_() function calls.
65 u32 ethtool_op_get_link(struct net_device *dev)
67 return netif_carrier_ok(dev) ? 1 : 0;
69 EXPORT_SYMBOL(ethtool_op_get_link);
71 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
73 info->so_timestamping =
74 SOF_TIMESTAMPING_TX_SOFTWARE |
75 SOF_TIMESTAMPING_RX_SOFTWARE |
76 SOF_TIMESTAMPING_SOFTWARE;
80 EXPORT_SYMBOL(ethtool_op_get_ts_info);
82 /* Handlers for each ethtool command */
84 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
86 struct ethtool_gfeatures cmd = {
87 .cmd = ETHTOOL_GFEATURES,
88 .size = ETHTOOL_DEV_FEATURE_WORDS,
90 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
95 /* in case feature bits run out again */
96 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
98 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
99 features[i].available = (u32)(dev->hw_features >> (32 * i));
100 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
101 features[i].active = (u32)(dev->features >> (32 * i));
102 features[i].never_changed =
103 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
106 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
107 if (get_user(copy_size, sizeaddr))
110 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
111 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
113 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
115 useraddr += sizeof(cmd);
116 if (copy_to_user(useraddr, features,
117 array_size(copy_size, sizeof(*features))))
123 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
125 struct ethtool_sfeatures cmd;
126 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
127 netdev_features_t wanted = 0, valid = 0;
130 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
132 useraddr += sizeof(cmd);
134 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
137 if (copy_from_user(features, useraddr, sizeof(features)))
140 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
141 valid |= (netdev_features_t)features[i].valid << (32 * i);
142 wanted |= (netdev_features_t)features[i].requested << (32 * i);
145 if (valid & ~NETIF_F_ETHTOOL_BITS)
148 if (valid & ~dev->hw_features) {
149 valid &= dev->hw_features;
150 ret |= ETHTOOL_F_UNSUPPORTED;
153 dev->wanted_features &= ~valid;
154 dev->wanted_features |= wanted & valid;
155 __netdev_update_features(dev);
157 if ((dev->wanted_features ^ dev->features) & valid)
158 ret |= ETHTOOL_F_WISH;
163 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
165 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
166 const struct ethtool_ops *ops = dev->ethtool_ops;
168 if (sset == ETH_SS_FEATURES)
169 return ARRAY_SIZE(netdev_features_strings);
171 if (sset == ETH_SS_RSS_HASH_FUNCS)
172 return ARRAY_SIZE(rss_hash_func_strings);
174 if (sset == ETH_SS_TUNABLES)
175 return ARRAY_SIZE(tunable_strings);
177 if (sset == ETH_SS_PHY_TUNABLES)
178 return ARRAY_SIZE(phy_tunable_strings);
180 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
181 !ops->get_ethtool_phy_stats &&
182 phy_ops && phy_ops->get_sset_count)
183 return phy_ops->get_sset_count(dev->phydev);
185 if (sset == ETH_SS_LINK_MODES)
186 return __ETHTOOL_LINK_MODE_MASK_NBITS;
188 if (ops->get_sset_count && ops->get_strings)
189 return ops->get_sset_count(dev, sset);
194 static void __ethtool_get_strings(struct net_device *dev,
195 u32 stringset, u8 *data)
197 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
198 const struct ethtool_ops *ops = dev->ethtool_ops;
200 if (stringset == ETH_SS_FEATURES)
201 memcpy(data, netdev_features_strings,
202 sizeof(netdev_features_strings));
203 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
204 memcpy(data, rss_hash_func_strings,
205 sizeof(rss_hash_func_strings));
206 else if (stringset == ETH_SS_TUNABLES)
207 memcpy(data, tunable_strings, sizeof(tunable_strings));
208 else if (stringset == ETH_SS_PHY_TUNABLES)
209 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
210 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
211 !ops->get_ethtool_phy_stats && phy_ops &&
212 phy_ops->get_strings)
213 phy_ops->get_strings(dev->phydev, data);
214 else if (stringset == ETH_SS_LINK_MODES)
215 memcpy(data, link_mode_names,
216 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
218 /* ops->get_strings is valid because checked earlier */
219 ops->get_strings(dev, stringset, data);
222 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
224 /* feature masks of legacy discrete ethtool ops */
227 case ETHTOOL_GTXCSUM:
228 case ETHTOOL_STXCSUM:
229 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
231 case ETHTOOL_GRXCSUM:
232 case ETHTOOL_SRXCSUM:
233 return NETIF_F_RXCSUM;
236 return NETIF_F_SG | NETIF_F_FRAGLIST;
239 return NETIF_F_ALL_TSO;
251 static int ethtool_get_one_feature(struct net_device *dev,
252 char __user *useraddr, u32 ethcmd)
254 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
255 struct ethtool_value edata = {
257 .data = !!(dev->features & mask),
260 if (copy_to_user(useraddr, &edata, sizeof(edata)))
265 static int ethtool_set_one_feature(struct net_device *dev,
266 void __user *useraddr, u32 ethcmd)
268 struct ethtool_value edata;
269 netdev_features_t mask;
271 if (copy_from_user(&edata, useraddr, sizeof(edata)))
274 mask = ethtool_get_feature_mask(ethcmd);
275 mask &= dev->hw_features;
280 dev->wanted_features |= mask;
282 dev->wanted_features &= ~mask;
284 __netdev_update_features(dev);
289 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
290 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
291 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
292 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
295 static u32 __ethtool_get_flags(struct net_device *dev)
299 if (dev->features & NETIF_F_LRO)
300 flags |= ETH_FLAG_LRO;
301 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
302 flags |= ETH_FLAG_RXVLAN;
303 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
304 flags |= ETH_FLAG_TXVLAN;
305 if (dev->features & NETIF_F_NTUPLE)
306 flags |= ETH_FLAG_NTUPLE;
307 if (dev->features & NETIF_F_RXHASH)
308 flags |= ETH_FLAG_RXHASH;
313 static int __ethtool_set_flags(struct net_device *dev, u32 data)
315 netdev_features_t features = 0, changed;
317 if (data & ~ETH_ALL_FLAGS)
320 if (data & ETH_FLAG_LRO)
321 features |= NETIF_F_LRO;
322 if (data & ETH_FLAG_RXVLAN)
323 features |= NETIF_F_HW_VLAN_CTAG_RX;
324 if (data & ETH_FLAG_TXVLAN)
325 features |= NETIF_F_HW_VLAN_CTAG_TX;
326 if (data & ETH_FLAG_NTUPLE)
327 features |= NETIF_F_NTUPLE;
328 if (data & ETH_FLAG_RXHASH)
329 features |= NETIF_F_RXHASH;
331 /* allow changing only bits set in hw_features */
332 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
333 if (changed & ~dev->hw_features)
334 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
336 dev->wanted_features =
337 (dev->wanted_features & ~changed) | (features & changed);
339 __netdev_update_features(dev);
344 /* Given two link masks, AND them together and save the result in dst. */
345 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
346 struct ethtool_link_ksettings *src)
348 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
349 unsigned int idx = 0;
351 for (; idx < size; idx++) {
352 dst->link_modes.supported[idx] &=
353 src->link_modes.supported[idx];
354 dst->link_modes.advertising[idx] &=
355 src->link_modes.advertising[idx];
358 EXPORT_SYMBOL(ethtool_intersect_link_masks);
360 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
366 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
368 /* return false if src had higher bits set. lower bits always updated. */
369 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
370 const unsigned long *src)
372 *legacy_u32 = src[0];
373 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
374 __ETHTOOL_LINK_MODE_MASK_NBITS;
376 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
378 /* return false if ksettings link modes had higher bits
379 * set. legacy_settings always updated (best effort)
382 convert_link_ksettings_to_legacy_settings(
383 struct ethtool_cmd *legacy_settings,
384 const struct ethtool_link_ksettings *link_ksettings)
388 memset(legacy_settings, 0, sizeof(*legacy_settings));
389 /* this also clears the deprecated fields in legacy structure:
395 retval &= ethtool_convert_link_mode_to_legacy_u32(
396 &legacy_settings->supported,
397 link_ksettings->link_modes.supported);
398 retval &= ethtool_convert_link_mode_to_legacy_u32(
399 &legacy_settings->advertising,
400 link_ksettings->link_modes.advertising);
401 retval &= ethtool_convert_link_mode_to_legacy_u32(
402 &legacy_settings->lp_advertising,
403 link_ksettings->link_modes.lp_advertising);
404 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
405 legacy_settings->duplex
406 = link_ksettings->base.duplex;
407 legacy_settings->port
408 = link_ksettings->base.port;
409 legacy_settings->phy_address
410 = link_ksettings->base.phy_address;
411 legacy_settings->autoneg
412 = link_ksettings->base.autoneg;
413 legacy_settings->mdio_support
414 = link_ksettings->base.mdio_support;
415 legacy_settings->eth_tp_mdix
416 = link_ksettings->base.eth_tp_mdix;
417 legacy_settings->eth_tp_mdix_ctrl
418 = link_ksettings->base.eth_tp_mdix_ctrl;
419 legacy_settings->transceiver
420 = link_ksettings->base.transceiver;
424 /* number of 32-bit words to store the user's link mode bitmaps */
425 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
426 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
428 /* layout of the struct passed from/to userland */
429 struct ethtool_link_usettings {
430 struct ethtool_link_settings base;
432 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
433 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
434 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
438 /* Internal kernel helper to query a device ethtool_link_settings. */
439 int __ethtool_get_link_ksettings(struct net_device *dev,
440 struct ethtool_link_ksettings *link_ksettings)
444 if (!dev->ethtool_ops->get_link_ksettings)
447 memset(link_ksettings, 0, sizeof(*link_ksettings));
448 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
450 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
452 /* convert ethtool_link_usettings in user space to a kernel internal
453 * ethtool_link_ksettings. return 0 on success, errno on error.
455 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
456 const void __user *from)
458 struct ethtool_link_usettings link_usettings;
460 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
463 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
464 bitmap_from_arr32(to->link_modes.supported,
465 link_usettings.link_modes.supported,
466 __ETHTOOL_LINK_MODE_MASK_NBITS);
467 bitmap_from_arr32(to->link_modes.advertising,
468 link_usettings.link_modes.advertising,
469 __ETHTOOL_LINK_MODE_MASK_NBITS);
470 bitmap_from_arr32(to->link_modes.lp_advertising,
471 link_usettings.link_modes.lp_advertising,
472 __ETHTOOL_LINK_MODE_MASK_NBITS);
477 /* Check if the user is trying to change anything besides speed/duplex */
478 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
480 struct ethtool_link_settings base2 = {};
482 base2.speed = cmd->base.speed;
483 base2.port = PORT_OTHER;
484 base2.duplex = cmd->base.duplex;
485 base2.cmd = cmd->base.cmd;
486 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
488 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
489 bitmap_empty(cmd->link_modes.supported,
490 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
491 bitmap_empty(cmd->link_modes.lp_advertising,
492 __ETHTOOL_LINK_MODE_MASK_NBITS);
495 /* convert a kernel internal ethtool_link_ksettings to
496 * ethtool_link_usettings in user space. return 0 on success, errno on
500 store_link_ksettings_for_user(void __user *to,
501 const struct ethtool_link_ksettings *from)
503 struct ethtool_link_usettings link_usettings;
505 memcpy(&link_usettings, from, sizeof(link_usettings));
506 bitmap_to_arr32(link_usettings.link_modes.supported,
507 from->link_modes.supported,
508 __ETHTOOL_LINK_MODE_MASK_NBITS);
509 bitmap_to_arr32(link_usettings.link_modes.advertising,
510 from->link_modes.advertising,
511 __ETHTOOL_LINK_MODE_MASK_NBITS);
512 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
513 from->link_modes.lp_advertising,
514 __ETHTOOL_LINK_MODE_MASK_NBITS);
516 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
522 /* Query device for its ethtool_link_settings. */
523 static int ethtool_get_link_ksettings(struct net_device *dev,
524 void __user *useraddr)
527 struct ethtool_link_ksettings link_ksettings;
530 if (!dev->ethtool_ops->get_link_ksettings)
533 /* handle bitmap nbits handshake */
534 if (copy_from_user(&link_ksettings.base, useraddr,
535 sizeof(link_ksettings.base)))
538 if (__ETHTOOL_LINK_MODE_MASK_NU32
539 != link_ksettings.base.link_mode_masks_nwords) {
540 /* wrong link mode nbits requested */
541 memset(&link_ksettings, 0, sizeof(link_ksettings));
542 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
543 /* send back number of words required as negative val */
544 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
545 "need too many bits for link modes!");
546 link_ksettings.base.link_mode_masks_nwords
547 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
549 /* copy the base fields back to user, not the link
552 if (copy_to_user(useraddr, &link_ksettings.base,
553 sizeof(link_ksettings.base)))
559 /* handshake successful: user/kernel agree on
560 * link_mode_masks_nwords
563 memset(&link_ksettings, 0, sizeof(link_ksettings));
564 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
568 /* make sure we tell the right values to user */
569 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
570 link_ksettings.base.link_mode_masks_nwords
571 = __ETHTOOL_LINK_MODE_MASK_NU32;
572 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
573 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
575 return store_link_ksettings_for_user(useraddr, &link_ksettings);
578 /* Update device ethtool_link_settings. */
579 static int ethtool_set_link_ksettings(struct net_device *dev,
580 void __user *useraddr)
583 struct ethtool_link_ksettings link_ksettings;
587 if (!dev->ethtool_ops->set_link_ksettings)
590 /* make sure nbits field has expected value */
591 if (copy_from_user(&link_ksettings.base, useraddr,
592 sizeof(link_ksettings.base)))
595 if (__ETHTOOL_LINK_MODE_MASK_NU32
596 != link_ksettings.base.link_mode_masks_nwords)
599 /* copy the whole structure, now that we know it has expected
602 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
606 /* re-check nwords field, just in case */
607 if (__ETHTOOL_LINK_MODE_MASK_NU32
608 != link_ksettings.base.link_mode_masks_nwords)
611 if (link_ksettings.base.master_slave_cfg ||
612 link_ksettings.base.master_slave_state)
615 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
617 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
618 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
623 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
624 const struct ethtool_link_ksettings *cmd,
625 u32 *dev_speed, u8 *dev_duplex)
630 speed = cmd->base.speed;
631 duplex = cmd->base.duplex;
632 /* don't allow custom speed and duplex */
633 if (!ethtool_validate_speed(speed) ||
634 !ethtool_validate_duplex(duplex) ||
635 !ethtool_virtdev_validate_cmd(cmd))
638 *dev_duplex = duplex;
642 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
644 /* Query device for its ethtool_cmd settings.
646 * Backward compatibility note: for compatibility with legacy ethtool, this is
647 * now implemented via get_link_ksettings. When driver reports higher link mode
648 * bits, a kernel warning is logged once (with name of 1st driver/device) to
649 * recommend user to upgrade ethtool, but the command is successful (only the
650 * lower link mode bits reported back to user). Deprecated fields from
651 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
653 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
655 struct ethtool_link_ksettings link_ksettings;
656 struct ethtool_cmd cmd;
660 if (!dev->ethtool_ops->get_link_ksettings)
663 memset(&link_ksettings, 0, sizeof(link_ksettings));
664 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
667 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
669 /* send a sensible cmd tag back to user */
670 cmd.cmd = ETHTOOL_GSET;
672 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
678 /* Update device link settings with given ethtool_cmd.
680 * Backward compatibility note: for compatibility with legacy ethtool, this is
681 * now always implemented via set_link_settings. When user's request updates
682 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
683 * warning is logged once (with name of 1st driver/device) to recommend user to
684 * upgrade ethtool, and the request is rejected.
686 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
688 struct ethtool_link_ksettings link_ksettings;
689 struct ethtool_cmd cmd;
694 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
696 if (!dev->ethtool_ops->set_link_ksettings)
699 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
701 link_ksettings.base.link_mode_masks_nwords =
702 __ETHTOOL_LINK_MODE_MASK_NU32;
703 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
705 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
706 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
712 ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
714 const struct ethtool_ops *ops = dev->ethtool_ops;
716 rsp->info.cmd = ETHTOOL_GDRVINFO;
717 strlcpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
718 if (ops->get_drvinfo) {
719 ops->get_drvinfo(dev, &rsp->info);
720 } else if (dev->dev.parent && dev->dev.parent->driver) {
721 strlcpy(rsp->info.bus_info, dev_name(dev->dev.parent),
722 sizeof(rsp->info.bus_info));
723 strlcpy(rsp->info.driver, dev->dev.parent->driver->name,
724 sizeof(rsp->info.driver));
725 } else if (dev->rtnl_link_ops) {
726 strlcpy(rsp->info.driver, dev->rtnl_link_ops->kind,
727 sizeof(rsp->info.driver));
733 * this method of obtaining string set info is deprecated;
734 * Use ETHTOOL_GSSET_INFO instead.
736 if (ops->get_sset_count) {
739 rc = ops->get_sset_count(dev, ETH_SS_TEST);
741 rsp->info.testinfo_len = rc;
742 rc = ops->get_sset_count(dev, ETH_SS_STATS);
744 rsp->info.n_stats = rc;
745 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
747 rsp->info.n_priv_flags = rc;
749 if (ops->get_regs_len) {
750 int ret = ops->get_regs_len(dev);
753 rsp->info.regdump_len = ret;
756 if (ops->get_eeprom_len)
757 rsp->info.eedump_len = ops->get_eeprom_len(dev);
759 if (!rsp->info.fw_version[0])
760 rsp->devlink = netdev_to_devlink_get(dev);
765 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
766 void __user *useraddr)
768 struct ethtool_sset_info info;
770 int i, idx = 0, n_bits = 0, ret, rc;
771 u32 *info_buf = NULL;
773 if (copy_from_user(&info, useraddr, sizeof(info)))
776 /* store copy of mask, because we zero struct later on */
777 sset_mask = info.sset_mask;
781 /* calculate size of return buffer */
782 n_bits = hweight64(sset_mask);
784 memset(&info, 0, sizeof(info));
785 info.cmd = ETHTOOL_GSSET_INFO;
787 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
792 * fill return buffer based on input bitmask and successful
793 * get_sset_count return
795 for (i = 0; i < 64; i++) {
796 if (!(sset_mask & (1ULL << i)))
799 rc = __ethtool_get_sset_count(dev, i);
801 info.sset_mask |= (1ULL << i);
802 info_buf[idx++] = rc;
807 if (copy_to_user(useraddr, &info, sizeof(info)))
810 useraddr += offsetof(struct ethtool_sset_info, data);
811 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
821 static noinline_for_stack int
822 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
823 const struct compat_ethtool_rxnfc __user *useraddr,
826 struct compat_ethtool_rxnfc crxnfc = {};
828 /* We expect there to be holes between fs.m_ext and
829 * fs.ring_cookie and at the end of fs, but nowhere else.
830 * On non-x86, no conversion should be needed.
832 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
833 sizeof(struct compat_ethtool_rxnfc) !=
834 sizeof(struct ethtool_rxnfc));
835 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
836 sizeof(useraddr->fs.m_ext) !=
837 offsetof(struct ethtool_rxnfc, fs.m_ext) +
838 sizeof(rxnfc->fs.m_ext));
839 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
840 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
841 offsetof(struct ethtool_rxnfc, fs.location) -
842 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
844 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
847 *rxnfc = (struct ethtool_rxnfc) {
849 .flow_type = crxnfc.flow_type,
852 .flow_type = crxnfc.fs.flow_type,
853 .h_u = crxnfc.fs.h_u,
854 .h_ext = crxnfc.fs.h_ext,
855 .m_u = crxnfc.fs.m_u,
856 .m_ext = crxnfc.fs.m_ext,
857 .ring_cookie = crxnfc.fs.ring_cookie,
858 .location = crxnfc.fs.location,
860 .rule_cnt = crxnfc.rule_cnt,
866 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
867 const void __user *useraddr,
870 if (compat_need_64bit_alignment_fixup())
871 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
873 if (copy_from_user(rxnfc, useraddr, size))
879 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
880 const struct ethtool_rxnfc *rxnfc,
881 size_t size, const u32 *rule_buf)
883 struct compat_ethtool_rxnfc crxnfc;
885 memset(&crxnfc, 0, sizeof(crxnfc));
886 crxnfc = (struct compat_ethtool_rxnfc) {
888 .flow_type = rxnfc->flow_type,
891 .flow_type = rxnfc->fs.flow_type,
892 .h_u = rxnfc->fs.h_u,
893 .h_ext = rxnfc->fs.h_ext,
894 .m_u = rxnfc->fs.m_u,
895 .m_ext = rxnfc->fs.m_ext,
896 .ring_cookie = rxnfc->fs.ring_cookie,
897 .location = rxnfc->fs.location,
899 .rule_cnt = rxnfc->rule_cnt,
902 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
908 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
909 const struct ethtool_rxnfc *rxnfc,
910 size_t size, const u32 *rule_buf)
914 if (compat_need_64bit_alignment_fixup()) {
915 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
917 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
919 ret = copy_to_user(useraddr, rxnfc, size);
920 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
927 if (copy_to_user(useraddr, rule_buf,
928 rxnfc->rule_cnt * sizeof(u32)))
935 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
936 u32 cmd, void __user *useraddr)
938 struct ethtool_rxnfc info;
939 size_t info_size = sizeof(info);
942 if (!dev->ethtool_ops->set_rxnfc)
945 /* struct ethtool_rxnfc was originally defined for
946 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
947 * members. User-space might still be using that
949 if (cmd == ETHTOOL_SRXFH)
950 info_size = (offsetof(struct ethtool_rxnfc, data) +
953 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
956 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
960 if (cmd == ETHTOOL_SRXCLSRLINS &&
961 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
967 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
968 u32 cmd, void __user *useraddr)
970 struct ethtool_rxnfc info;
971 size_t info_size = sizeof(info);
972 const struct ethtool_ops *ops = dev->ethtool_ops;
974 void *rule_buf = NULL;
979 /* struct ethtool_rxnfc was originally defined for
980 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
981 * members. User-space might still be using that
983 if (cmd == ETHTOOL_GRXFH)
984 info_size = (offsetof(struct ethtool_rxnfc, data) +
987 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
990 /* If FLOW_RSS was requested then user-space must be using the
991 * new definition, as FLOW_RSS is newer.
993 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
994 info_size = sizeof(info);
995 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
997 /* Since malicious users may modify the original data,
998 * we need to check whether FLOW_RSS is still requested.
1000 if (!(info.flow_type & FLOW_RSS))
1004 if (info.cmd != cmd)
1007 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1008 if (info.rule_cnt > 0) {
1009 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1010 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1017 ret = ops->get_rxnfc(dev, &info, rule_buf);
1021 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1028 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1029 struct ethtool_rxnfc *rx_rings,
1034 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1037 /* Validate ring indices */
1038 for (i = 0; i < size; i++)
1039 if (indir[i] >= rx_rings->data)
1045 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1047 void netdev_rss_key_fill(void *buffer, size_t len)
1049 BUG_ON(len > sizeof(netdev_rss_key));
1050 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1051 memcpy(buffer, netdev_rss_key, len);
1053 EXPORT_SYMBOL(netdev_rss_key_fill);
1055 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1056 void __user *useraddr)
1058 u32 user_size, dev_size;
1062 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1063 !dev->ethtool_ops->get_rxfh)
1065 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1069 if (copy_from_user(&user_size,
1070 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1074 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1075 &dev_size, sizeof(dev_size)))
1078 /* If the user buffer size is 0, this is just a query for the
1079 * device table size. Otherwise, if it's smaller than the
1080 * device table size it's an error.
1082 if (user_size < dev_size)
1083 return user_size == 0 ? 0 : -EINVAL;
1085 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1089 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1093 if (copy_to_user(useraddr +
1094 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1095 indir, dev_size * sizeof(indir[0])))
1103 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1104 void __user *useraddr)
1106 struct ethtool_rxnfc rx_rings;
1107 u32 user_size, dev_size, i;
1109 const struct ethtool_ops *ops = dev->ethtool_ops;
1111 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1113 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1117 dev_size = ops->get_rxfh_indir_size(dev);
1121 if (copy_from_user(&user_size,
1122 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1126 if (user_size != 0 && user_size != dev_size)
1129 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1133 rx_rings.cmd = ETHTOOL_GRXRINGS;
1134 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1138 if (user_size == 0) {
1139 for (i = 0; i < dev_size; i++)
1140 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1142 ret = ethtool_copy_validate_indir(indir,
1143 useraddr + ringidx_offset,
1150 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1154 /* indicate whether rxfh was set to default */
1156 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1158 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1165 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1166 void __user *useraddr)
1169 const struct ethtool_ops *ops = dev->ethtool_ops;
1170 u32 user_indir_size, user_key_size;
1171 u32 dev_indir_size = 0, dev_key_size = 0;
1172 struct ethtool_rxfh rxfh;
1183 if (ops->get_rxfh_indir_size)
1184 dev_indir_size = ops->get_rxfh_indir_size(dev);
1185 if (ops->get_rxfh_key_size)
1186 dev_key_size = ops->get_rxfh_key_size(dev);
1188 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1190 user_indir_size = rxfh.indir_size;
1191 user_key_size = rxfh.key_size;
1193 /* Check that reserved fields are 0 for now */
1194 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1196 /* Most drivers don't handle rss_context, check it's 0 as well */
1197 if (rxfh.rss_context && !ops->get_rxfh_context)
1200 rxfh.indir_size = dev_indir_size;
1201 rxfh.key_size = dev_key_size;
1202 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1205 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1206 (user_key_size && (user_key_size != dev_key_size)))
1209 indir_bytes = user_indir_size * sizeof(indir[0]);
1210 total_size = indir_bytes + user_key_size;
1211 rss_config = kzalloc(total_size, GFP_USER);
1215 if (user_indir_size)
1216 indir = (u32 *)rss_config;
1219 hkey = rss_config + indir_bytes;
1221 if (rxfh.rss_context)
1222 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1226 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1230 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1231 &dev_hfunc, sizeof(rxfh.hfunc))) {
1233 } else if (copy_to_user(useraddr +
1234 offsetof(struct ethtool_rxfh, rss_config[0]),
1235 rss_config, total_size)) {
1244 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1245 void __user *useraddr)
1248 const struct ethtool_ops *ops = dev->ethtool_ops;
1249 struct ethtool_rxnfc rx_rings;
1250 struct ethtool_rxfh rxfh;
1251 u32 dev_indir_size = 0, dev_key_size = 0, i;
1252 u32 *indir = NULL, indir_bytes = 0;
1255 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1256 bool delete = false;
1258 if (!ops->get_rxnfc || !ops->set_rxfh)
1261 if (ops->get_rxfh_indir_size)
1262 dev_indir_size = ops->get_rxfh_indir_size(dev);
1263 if (ops->get_rxfh_key_size)
1264 dev_key_size = ops->get_rxfh_key_size(dev);
1266 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1269 /* Check that reserved fields are 0 for now */
1270 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1272 /* Most drivers don't handle rss_context, check it's 0 as well */
1273 if (rxfh.rss_context && !ops->set_rxfh_context)
1276 /* If either indir, hash key or function is valid, proceed further.
1277 * Must request at least one change: indir size, hash key or function.
1279 if ((rxfh.indir_size &&
1280 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1281 rxfh.indir_size != dev_indir_size) ||
1282 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1283 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1284 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1287 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1288 indir_bytes = dev_indir_size * sizeof(indir[0]);
1290 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1294 rx_rings.cmd = ETHTOOL_GRXRINGS;
1295 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1299 /* rxfh.indir_size == 0 means reset the indir table to default (master
1300 * context) or delete the context (other RSS contexts).
1301 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1303 if (rxfh.indir_size &&
1304 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1305 indir = (u32 *)rss_config;
1306 ret = ethtool_copy_validate_indir(indir,
1307 useraddr + rss_cfg_offset,
1312 } else if (rxfh.indir_size == 0) {
1313 if (rxfh.rss_context == 0) {
1314 indir = (u32 *)rss_config;
1315 for (i = 0; i < dev_indir_size; i++)
1316 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1322 if (rxfh.key_size) {
1323 hkey = rss_config + indir_bytes;
1324 if (copy_from_user(hkey,
1325 useraddr + rss_cfg_offset + indir_bytes,
1332 if (rxfh.rss_context)
1333 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1334 &rxfh.rss_context, delete);
1336 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1340 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1341 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1344 if (!rxfh.rss_context) {
1345 /* indicate whether rxfh was set to default */
1346 if (rxfh.indir_size == 0)
1347 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1348 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1349 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1357 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1359 struct ethtool_regs regs;
1360 const struct ethtool_ops *ops = dev->ethtool_ops;
1364 if (!ops->get_regs || !ops->get_regs_len)
1367 if (copy_from_user(®s, useraddr, sizeof(regs)))
1370 reglen = ops->get_regs_len(dev);
1374 if (regs.len > reglen)
1377 regbuf = vzalloc(reglen);
1381 if (regs.len < reglen)
1384 ops->get_regs(dev, ®s, regbuf);
1387 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1389 useraddr += offsetof(struct ethtool_regs, data);
1390 if (copy_to_user(useraddr, regbuf, reglen))
1399 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1401 struct ethtool_value reset;
1404 if (!dev->ethtool_ops->reset)
1407 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1410 ret = dev->ethtool_ops->reset(dev, &reset.data);
1414 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1419 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1421 struct ethtool_wolinfo wol;
1423 if (!dev->ethtool_ops->get_wol)
1426 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1427 wol.cmd = ETHTOOL_GWOL;
1428 dev->ethtool_ops->get_wol(dev, &wol);
1430 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1435 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1437 struct ethtool_wolinfo wol;
1440 if (!dev->ethtool_ops->set_wol)
1443 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1446 ret = dev->ethtool_ops->set_wol(dev, &wol);
1450 dev->wol_enabled = !!wol.wolopts;
1451 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1456 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1458 struct ethtool_eee edata;
1461 if (!dev->ethtool_ops->get_eee)
1464 memset(&edata, 0, sizeof(struct ethtool_eee));
1465 edata.cmd = ETHTOOL_GEEE;
1466 rc = dev->ethtool_ops->get_eee(dev, &edata);
1471 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1477 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1479 struct ethtool_eee edata;
1482 if (!dev->ethtool_ops->set_eee)
1485 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1488 ret = dev->ethtool_ops->set_eee(dev, &edata);
1490 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1494 static int ethtool_nway_reset(struct net_device *dev)
1496 if (!dev->ethtool_ops->nway_reset)
1499 return dev->ethtool_ops->nway_reset(dev);
1502 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1504 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1505 int link = __ethtool_get_link(dev);
1511 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1516 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1517 int (*getter)(struct net_device *,
1518 struct ethtool_eeprom *, u8 *),
1521 struct ethtool_eeprom eeprom;
1522 void __user *userbuf = useraddr + sizeof(eeprom);
1523 u32 bytes_remaining;
1527 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1530 /* Check for wrap and zero */
1531 if (eeprom.offset + eeprom.len <= eeprom.offset)
1534 /* Check for exceeding total eeprom len */
1535 if (eeprom.offset + eeprom.len > total_len)
1538 data = kzalloc(PAGE_SIZE, GFP_USER);
1542 bytes_remaining = eeprom.len;
1543 while (bytes_remaining > 0) {
1544 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1546 ret = getter(dev, &eeprom, data);
1553 if (copy_to_user(userbuf, data, eeprom.len)) {
1557 userbuf += eeprom.len;
1558 eeprom.offset += eeprom.len;
1559 bytes_remaining -= eeprom.len;
1562 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1563 eeprom.offset -= eeprom.len;
1564 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1571 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1573 const struct ethtool_ops *ops = dev->ethtool_ops;
1575 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1576 !ops->get_eeprom_len(dev))
1579 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1580 ops->get_eeprom_len(dev));
1583 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1585 struct ethtool_eeprom eeprom;
1586 const struct ethtool_ops *ops = dev->ethtool_ops;
1587 void __user *userbuf = useraddr + sizeof(eeprom);
1588 u32 bytes_remaining;
1592 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1593 !ops->get_eeprom_len(dev))
1596 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1599 /* Check for wrap and zero */
1600 if (eeprom.offset + eeprom.len <= eeprom.offset)
1603 /* Check for exceeding total eeprom len */
1604 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1607 data = kzalloc(PAGE_SIZE, GFP_USER);
1611 bytes_remaining = eeprom.len;
1612 while (bytes_remaining > 0) {
1613 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1615 if (copy_from_user(data, userbuf, eeprom.len)) {
1619 ret = ops->set_eeprom(dev, &eeprom, data);
1622 userbuf += eeprom.len;
1623 eeprom.offset += eeprom.len;
1624 bytes_remaining -= eeprom.len;
1631 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1632 void __user *useraddr)
1634 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1635 struct kernel_ethtool_coalesce kernel_coalesce = {};
1638 if (!dev->ethtool_ops->get_coalesce)
1641 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1646 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1652 ethtool_set_coalesce_supported(struct net_device *dev,
1653 struct ethtool_coalesce *coalesce)
1655 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1656 u32 nonzero_params = 0;
1658 if (coalesce->rx_coalesce_usecs)
1659 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1660 if (coalesce->rx_max_coalesced_frames)
1661 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1662 if (coalesce->rx_coalesce_usecs_irq)
1663 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1664 if (coalesce->rx_max_coalesced_frames_irq)
1665 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1666 if (coalesce->tx_coalesce_usecs)
1667 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1668 if (coalesce->tx_max_coalesced_frames)
1669 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1670 if (coalesce->tx_coalesce_usecs_irq)
1671 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1672 if (coalesce->tx_max_coalesced_frames_irq)
1673 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1674 if (coalesce->stats_block_coalesce_usecs)
1675 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1676 if (coalesce->use_adaptive_rx_coalesce)
1677 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1678 if (coalesce->use_adaptive_tx_coalesce)
1679 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1680 if (coalesce->pkt_rate_low)
1681 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1682 if (coalesce->rx_coalesce_usecs_low)
1683 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1684 if (coalesce->rx_max_coalesced_frames_low)
1685 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1686 if (coalesce->tx_coalesce_usecs_low)
1687 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1688 if (coalesce->tx_max_coalesced_frames_low)
1689 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1690 if (coalesce->pkt_rate_high)
1691 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1692 if (coalesce->rx_coalesce_usecs_high)
1693 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1694 if (coalesce->rx_max_coalesced_frames_high)
1695 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1696 if (coalesce->tx_coalesce_usecs_high)
1697 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1698 if (coalesce->tx_max_coalesced_frames_high)
1699 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1700 if (coalesce->rate_sample_interval)
1701 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1703 return (supported_params & nonzero_params) == nonzero_params;
1706 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1707 void __user *useraddr)
1709 struct kernel_ethtool_coalesce kernel_coalesce = {};
1710 struct ethtool_coalesce coalesce;
1713 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1716 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1721 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1724 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1727 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1730 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1734 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1736 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1737 struct kernel_ethtool_ringparam kernel_ringparam = {};
1739 if (!dev->ethtool_ops->get_ringparam)
1742 dev->ethtool_ops->get_ringparam(dev, &ringparam,
1743 &kernel_ringparam, NULL);
1745 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1750 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1752 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1753 struct kernel_ethtool_ringparam kernel_ringparam;
1756 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1759 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1762 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
1764 /* ensure new ring parameters are within the maximums */
1765 if (ringparam.rx_pending > max.rx_max_pending ||
1766 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1767 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1768 ringparam.tx_pending > max.tx_max_pending)
1771 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1772 &kernel_ringparam, NULL);
1774 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1778 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1779 void __user *useraddr)
1781 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1783 if (!dev->ethtool_ops->get_channels)
1786 dev->ethtool_ops->get_channels(dev, &channels);
1788 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1793 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1794 void __user *useraddr)
1796 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1797 u16 from_channel, to_channel;
1798 u32 max_rx_in_use = 0;
1802 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1805 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1808 dev->ethtool_ops->get_channels(dev, &curr);
1810 if (channels.rx_count == curr.rx_count &&
1811 channels.tx_count == curr.tx_count &&
1812 channels.combined_count == curr.combined_count &&
1813 channels.other_count == curr.other_count)
1816 /* ensure new counts are within the maximums */
1817 if (channels.rx_count > curr.max_rx ||
1818 channels.tx_count > curr.max_tx ||
1819 channels.combined_count > curr.max_combined ||
1820 channels.other_count > curr.max_other)
1823 /* ensure there is at least one RX and one TX channel */
1824 if (!channels.combined_count &&
1825 (!channels.rx_count || !channels.tx_count))
1828 /* ensure the new Rx count fits within the configured Rx flow
1829 * indirection table settings */
1830 if (netif_is_rxfh_configured(dev) &&
1831 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1832 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1835 /* Disabling channels, query zero-copy AF_XDP sockets */
1836 from_channel = channels.combined_count +
1837 min(channels.rx_count, channels.tx_count);
1838 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1839 for (i = from_channel; i < to_channel; i++)
1840 if (xsk_get_pool_from_qid(dev, i))
1843 ret = dev->ethtool_ops->set_channels(dev, &channels);
1845 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1849 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1851 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1853 if (!dev->ethtool_ops->get_pauseparam)
1856 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1858 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1863 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1865 struct ethtool_pauseparam pauseparam;
1868 if (!dev->ethtool_ops->set_pauseparam)
1871 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1874 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1876 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1880 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1882 struct ethtool_test test;
1883 const struct ethtool_ops *ops = dev->ethtool_ops;
1887 if (!ops->self_test || !ops->get_sset_count)
1890 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1893 WARN_ON(test_len == 0);
1895 if (copy_from_user(&test, useraddr, sizeof(test)))
1898 test.len = test_len;
1899 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1903 netif_testing_on(dev);
1904 ops->self_test(dev, &test, data);
1905 netif_testing_off(dev);
1908 if (copy_to_user(useraddr, &test, sizeof(test)))
1910 useraddr += sizeof(test);
1911 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
1920 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1922 struct ethtool_gstrings gstrings;
1926 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1929 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1932 if (ret > S32_MAX / ETH_GSTRING_LEN)
1939 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1943 __ethtool_get_strings(dev, gstrings.string_set, data);
1949 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1951 useraddr += sizeof(gstrings);
1953 copy_to_user(useraddr, data,
1954 array_size(gstrings.len, ETH_GSTRING_LEN)))
1963 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1967 va_start(args, fmt);
1968 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1971 *data += ETH_GSTRING_LEN;
1973 EXPORT_SYMBOL(ethtool_sprintf);
1975 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1977 struct ethtool_value id;
1979 const struct ethtool_ops *ops = dev->ethtool_ops;
1980 netdevice_tracker dev_tracker;
1983 if (!ops->set_phys_id)
1989 if (copy_from_user(&id, useraddr, sizeof(id)))
1992 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1996 /* Drop the RTNL lock while waiting, but prevent reentry or
1997 * removal of the device.
2000 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2004 /* Driver will handle this itself */
2005 schedule_timeout_interruptible(
2006 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2008 /* Driver expects to be called at twice the frequency in rc */
2009 int n = rc * 2, interval = HZ / n;
2010 u64 count = n * id.data, i = 0;
2014 rc = ops->set_phys_id(dev,
2015 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2019 schedule_timeout_interruptible(interval);
2020 } while (!signal_pending(current) && (!id.data || i < count));
2024 netdev_put(dev, &dev_tracker);
2027 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2031 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2033 struct ethtool_stats stats;
2034 const struct ethtool_ops *ops = dev->ethtool_ops;
2038 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2041 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2044 if (n_stats > S32_MAX / sizeof(u64))
2046 WARN_ON_ONCE(!n_stats);
2047 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2050 stats.n_stats = n_stats;
2053 data = vzalloc(array_size(n_stats, sizeof(u64)));
2056 ops->get_ethtool_stats(dev, &stats, data);
2062 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2064 useraddr += sizeof(stats);
2065 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2074 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2076 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2077 const struct ethtool_ops *ops = dev->ethtool_ops;
2078 struct phy_device *phydev = dev->phydev;
2079 struct ethtool_stats stats;
2083 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
2086 if (phydev && !ops->get_ethtool_phy_stats &&
2087 phy_ops && phy_ops->get_sset_count)
2088 n_stats = phy_ops->get_sset_count(phydev);
2090 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2093 if (n_stats > S32_MAX / sizeof(u64))
2095 WARN_ON_ONCE(!n_stats);
2097 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2100 stats.n_stats = n_stats;
2103 data = vzalloc(array_size(n_stats, sizeof(u64)));
2107 if (phydev && !ops->get_ethtool_phy_stats &&
2108 phy_ops && phy_ops->get_stats) {
2109 ret = phy_ops->get_stats(phydev, &stats, data);
2113 ops->get_ethtool_phy_stats(dev, &stats, data);
2120 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2122 useraddr += sizeof(stats);
2123 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2132 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2134 struct ethtool_perm_addr epaddr;
2136 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2139 if (epaddr.size < dev->addr_len)
2141 epaddr.size = dev->addr_len;
2143 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2145 useraddr += sizeof(epaddr);
2146 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2151 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2152 u32 cmd, u32 (*actor)(struct net_device *))
2154 struct ethtool_value edata = { .cmd = cmd };
2159 edata.data = actor(dev);
2161 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2166 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2167 void (*actor)(struct net_device *, u32))
2169 struct ethtool_value edata;
2174 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2177 actor(dev, edata.data);
2181 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2182 int (*actor)(struct net_device *, u32))
2184 struct ethtool_value edata;
2189 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2192 return actor(dev, edata.data);
2196 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2198 if (!dev->ethtool_ops->flash_device) {
2199 req->devlink = netdev_to_devlink_get(dev);
2203 return dev->ethtool_ops->flash_device(dev, &req->efl);
2206 static int ethtool_set_dump(struct net_device *dev,
2207 void __user *useraddr)
2209 struct ethtool_dump dump;
2211 if (!dev->ethtool_ops->set_dump)
2214 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2217 return dev->ethtool_ops->set_dump(dev, &dump);
2220 static int ethtool_get_dump_flag(struct net_device *dev,
2221 void __user *useraddr)
2224 struct ethtool_dump dump;
2225 const struct ethtool_ops *ops = dev->ethtool_ops;
2227 if (!ops->get_dump_flag)
2230 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2233 ret = ops->get_dump_flag(dev, &dump);
2237 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2242 static int ethtool_get_dump_data(struct net_device *dev,
2243 void __user *useraddr)
2247 struct ethtool_dump dump, tmp;
2248 const struct ethtool_ops *ops = dev->ethtool_ops;
2251 if (!ops->get_dump_data || !ops->get_dump_flag)
2254 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2257 memset(&tmp, 0, sizeof(tmp));
2258 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2259 ret = ops->get_dump_flag(dev, &tmp);
2263 len = min(tmp.len, dump.len);
2267 /* Don't ever let the driver think there's more space available
2268 * than it requested with .get_dump_flag().
2272 /* Always allocate enough space to hold the whole thing so that the
2273 * driver does not need to check the length and bother with partial
2276 data = vzalloc(tmp.len);
2279 ret = ops->get_dump_data(dev, &dump, data);
2283 /* There are two sane possibilities:
2284 * 1. The driver's .get_dump_data() does not touch dump.len.
2285 * 2. Or it may set dump.len to how much it really writes, which
2286 * should be tmp.len (or len if it can do a partial dump).
2287 * In any case respond to userspace with the actual length of data
2290 WARN_ON(dump.len != len && dump.len != tmp.len);
2293 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2297 useraddr += offsetof(struct ethtool_dump, data);
2298 if (copy_to_user(useraddr, data, len))
2305 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2307 struct ethtool_ts_info info;
2310 err = __ethtool_get_ts_info(dev, &info);
2314 if (copy_to_user(useraddr, &info, sizeof(info)))
2320 int ethtool_get_module_info_call(struct net_device *dev,
2321 struct ethtool_modinfo *modinfo)
2323 const struct ethtool_ops *ops = dev->ethtool_ops;
2324 struct phy_device *phydev = dev->phydev;
2327 return sfp_get_module_info(dev->sfp_bus, modinfo);
2329 if (phydev && phydev->drv && phydev->drv->module_info)
2330 return phydev->drv->module_info(phydev, modinfo);
2332 if (ops->get_module_info)
2333 return ops->get_module_info(dev, modinfo);
2338 static int ethtool_get_module_info(struct net_device *dev,
2339 void __user *useraddr)
2342 struct ethtool_modinfo modinfo;
2344 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2347 ret = ethtool_get_module_info_call(dev, &modinfo);
2351 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2357 int ethtool_get_module_eeprom_call(struct net_device *dev,
2358 struct ethtool_eeprom *ee, u8 *data)
2360 const struct ethtool_ops *ops = dev->ethtool_ops;
2361 struct phy_device *phydev = dev->phydev;
2364 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2366 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2367 return phydev->drv->module_eeprom(phydev, ee, data);
2369 if (ops->get_module_eeprom)
2370 return ops->get_module_eeprom(dev, ee, data);
2375 static int ethtool_get_module_eeprom(struct net_device *dev,
2376 void __user *useraddr)
2379 struct ethtool_modinfo modinfo;
2381 ret = ethtool_get_module_info_call(dev, &modinfo);
2385 return ethtool_get_any_eeprom(dev, useraddr,
2386 ethtool_get_module_eeprom_call,
2387 modinfo.eeprom_len);
2390 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2393 case ETHTOOL_RX_COPYBREAK:
2394 case ETHTOOL_TX_COPYBREAK:
2395 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2396 if (tuna->len != sizeof(u32) ||
2397 tuna->type_id != ETHTOOL_TUNABLE_U32)
2400 case ETHTOOL_PFC_PREVENTION_TOUT:
2401 if (tuna->len != sizeof(u16) ||
2402 tuna->type_id != ETHTOOL_TUNABLE_U16)
2412 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2415 struct ethtool_tunable tuna;
2416 const struct ethtool_ops *ops = dev->ethtool_ops;
2419 if (!ops->get_tunable)
2421 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2423 ret = ethtool_tunable_valid(&tuna);
2426 data = kzalloc(tuna.len, GFP_USER);
2429 ret = ops->get_tunable(dev, &tuna, data);
2432 useraddr += sizeof(tuna);
2434 if (copy_to_user(useraddr, data, tuna.len))
2443 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2446 struct ethtool_tunable tuna;
2447 const struct ethtool_ops *ops = dev->ethtool_ops;
2450 if (!ops->set_tunable)
2452 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2454 ret = ethtool_tunable_valid(&tuna);
2457 useraddr += sizeof(tuna);
2458 data = memdup_user(useraddr, tuna.len);
2460 return PTR_ERR(data);
2461 ret = ops->set_tunable(dev, &tuna, data);
2467 static noinline_for_stack int
2468 ethtool_get_per_queue_coalesce(struct net_device *dev,
2469 void __user *useraddr,
2470 struct ethtool_per_queue_op *per_queue_opt)
2474 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2476 if (!dev->ethtool_ops->get_per_queue_coalesce)
2479 useraddr += sizeof(*per_queue_opt);
2481 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2484 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2485 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2487 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2490 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2492 useraddr += sizeof(coalesce);
2498 static noinline_for_stack int
2499 ethtool_set_per_queue_coalesce(struct net_device *dev,
2500 void __user *useraddr,
2501 struct ethtool_per_queue_op *per_queue_opt)
2506 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2507 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2509 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2510 (!dev->ethtool_ops->get_per_queue_coalesce))
2513 useraddr += sizeof(*per_queue_opt);
2515 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2516 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2517 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2521 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2522 struct ethtool_coalesce coalesce;
2524 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2530 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2535 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2540 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2544 useraddr += sizeof(coalesce);
2550 for_each_set_bit(i, queue_mask, bit) {
2551 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2560 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2561 void __user *useraddr, u32 sub_cmd)
2563 struct ethtool_per_queue_op per_queue_opt;
2565 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2568 if (per_queue_opt.sub_command != sub_cmd)
2571 switch (per_queue_opt.sub_command) {
2572 case ETHTOOL_GCOALESCE:
2573 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2574 case ETHTOOL_SCOALESCE:
2575 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2581 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2584 case ETHTOOL_PHY_DOWNSHIFT:
2585 case ETHTOOL_PHY_FAST_LINK_DOWN:
2586 if (tuna->len != sizeof(u8) ||
2587 tuna->type_id != ETHTOOL_TUNABLE_U8)
2590 case ETHTOOL_PHY_EDPD:
2591 if (tuna->len != sizeof(u16) ||
2592 tuna->type_id != ETHTOOL_TUNABLE_U16)
2602 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2604 struct phy_device *phydev = dev->phydev;
2605 struct ethtool_tunable tuna;
2606 bool phy_drv_tunable;
2610 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2611 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2613 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2615 ret = ethtool_phy_tunable_valid(&tuna);
2618 data = kzalloc(tuna.len, GFP_USER);
2621 if (phy_drv_tunable) {
2622 mutex_lock(&phydev->lock);
2623 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2624 mutex_unlock(&phydev->lock);
2626 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2630 useraddr += sizeof(tuna);
2632 if (copy_to_user(useraddr, data, tuna.len))
2641 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2643 struct phy_device *phydev = dev->phydev;
2644 struct ethtool_tunable tuna;
2645 bool phy_drv_tunable;
2649 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2650 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2652 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2654 ret = ethtool_phy_tunable_valid(&tuna);
2657 useraddr += sizeof(tuna);
2658 data = memdup_user(useraddr, tuna.len);
2660 return PTR_ERR(data);
2661 if (phy_drv_tunable) {
2662 mutex_lock(&phydev->lock);
2663 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2664 mutex_unlock(&phydev->lock);
2666 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2673 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2675 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2678 if (!dev->ethtool_ops->get_fecparam)
2681 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2685 if (WARN_ON_ONCE(fecparam.reserved))
2686 fecparam.reserved = 0;
2688 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2693 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2695 struct ethtool_fecparam fecparam;
2697 if (!dev->ethtool_ops->set_fecparam)
2700 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2703 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2706 fecparam.active_fec = 0;
2707 fecparam.reserved = 0;
2709 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2712 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2715 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2716 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2718 struct net_device *dev;
2721 netdev_features_t old_features;
2723 dev = __dev_get_by_name(net, ifr->ifr_name);
2727 if (ethcmd == ETHTOOL_PERQUEUE) {
2728 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2733 /* Allow some commands to be done by anyone */
2736 case ETHTOOL_GDRVINFO:
2737 case ETHTOOL_GMSGLVL:
2739 case ETHTOOL_GCOALESCE:
2740 case ETHTOOL_GRINGPARAM:
2741 case ETHTOOL_GPAUSEPARAM:
2742 case ETHTOOL_GRXCSUM:
2743 case ETHTOOL_GTXCSUM:
2745 case ETHTOOL_GSSET_INFO:
2746 case ETHTOOL_GSTRINGS:
2747 case ETHTOOL_GSTATS:
2748 case ETHTOOL_GPHYSTATS:
2750 case ETHTOOL_GPERMADDR:
2754 case ETHTOOL_GFLAGS:
2755 case ETHTOOL_GPFLAGS:
2757 case ETHTOOL_GRXRINGS:
2758 case ETHTOOL_GRXCLSRLCNT:
2759 case ETHTOOL_GRXCLSRULE:
2760 case ETHTOOL_GRXCLSRLALL:
2761 case ETHTOOL_GRXFHINDIR:
2763 case ETHTOOL_GFEATURES:
2764 case ETHTOOL_GCHANNELS:
2765 case ETHTOOL_GET_TS_INFO:
2767 case ETHTOOL_GTUNABLE:
2768 case ETHTOOL_PHY_GTUNABLE:
2769 case ETHTOOL_GLINKSETTINGS:
2770 case ETHTOOL_GFECPARAM:
2773 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2777 if (dev->dev.parent)
2778 pm_runtime_get_sync(dev->dev.parent);
2780 if (!netif_device_present(dev)) {
2785 if (dev->ethtool_ops->begin) {
2786 rc = dev->ethtool_ops->begin(dev);
2790 old_features = dev->features;
2794 rc = ethtool_get_settings(dev, useraddr);
2797 rc = ethtool_set_settings(dev, useraddr);
2799 case ETHTOOL_GDRVINFO:
2800 rc = ethtool_get_drvinfo(dev, devlink_state);
2803 rc = ethtool_get_regs(dev, useraddr);
2806 rc = ethtool_get_wol(dev, useraddr);
2809 rc = ethtool_set_wol(dev, useraddr);
2811 case ETHTOOL_GMSGLVL:
2812 rc = ethtool_get_value(dev, useraddr, ethcmd,
2813 dev->ethtool_ops->get_msglevel);
2815 case ETHTOOL_SMSGLVL:
2816 rc = ethtool_set_value_void(dev, useraddr,
2817 dev->ethtool_ops->set_msglevel);
2819 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2822 rc = ethtool_get_eee(dev, useraddr);
2825 rc = ethtool_set_eee(dev, useraddr);
2827 case ETHTOOL_NWAY_RST:
2828 rc = ethtool_nway_reset(dev);
2831 rc = ethtool_get_link(dev, useraddr);
2833 case ETHTOOL_GEEPROM:
2834 rc = ethtool_get_eeprom(dev, useraddr);
2836 case ETHTOOL_SEEPROM:
2837 rc = ethtool_set_eeprom(dev, useraddr);
2839 case ETHTOOL_GCOALESCE:
2840 rc = ethtool_get_coalesce(dev, useraddr);
2842 case ETHTOOL_SCOALESCE:
2843 rc = ethtool_set_coalesce(dev, useraddr);
2845 case ETHTOOL_GRINGPARAM:
2846 rc = ethtool_get_ringparam(dev, useraddr);
2848 case ETHTOOL_SRINGPARAM:
2849 rc = ethtool_set_ringparam(dev, useraddr);
2851 case ETHTOOL_GPAUSEPARAM:
2852 rc = ethtool_get_pauseparam(dev, useraddr);
2854 case ETHTOOL_SPAUSEPARAM:
2855 rc = ethtool_set_pauseparam(dev, useraddr);
2858 rc = ethtool_self_test(dev, useraddr);
2860 case ETHTOOL_GSTRINGS:
2861 rc = ethtool_get_strings(dev, useraddr);
2863 case ETHTOOL_PHYS_ID:
2864 rc = ethtool_phys_id(dev, useraddr);
2866 case ETHTOOL_GSTATS:
2867 rc = ethtool_get_stats(dev, useraddr);
2869 case ETHTOOL_GPERMADDR:
2870 rc = ethtool_get_perm_addr(dev, useraddr);
2872 case ETHTOOL_GFLAGS:
2873 rc = ethtool_get_value(dev, useraddr, ethcmd,
2874 __ethtool_get_flags);
2876 case ETHTOOL_SFLAGS:
2877 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2879 case ETHTOOL_GPFLAGS:
2880 rc = ethtool_get_value(dev, useraddr, ethcmd,
2881 dev->ethtool_ops->get_priv_flags);
2883 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2885 case ETHTOOL_SPFLAGS:
2886 rc = ethtool_set_value(dev, useraddr,
2887 dev->ethtool_ops->set_priv_flags);
2890 case ETHTOOL_GRXRINGS:
2891 case ETHTOOL_GRXCLSRLCNT:
2892 case ETHTOOL_GRXCLSRULE:
2893 case ETHTOOL_GRXCLSRLALL:
2894 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2897 case ETHTOOL_SRXCLSRLDEL:
2898 case ETHTOOL_SRXCLSRLINS:
2899 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2901 case ETHTOOL_FLASHDEV:
2902 rc = ethtool_flash_device(dev, devlink_state);
2905 rc = ethtool_reset(dev, useraddr);
2907 case ETHTOOL_GSSET_INFO:
2908 rc = ethtool_get_sset_info(dev, useraddr);
2910 case ETHTOOL_GRXFHINDIR:
2911 rc = ethtool_get_rxfh_indir(dev, useraddr);
2913 case ETHTOOL_SRXFHINDIR:
2914 rc = ethtool_set_rxfh_indir(dev, useraddr);
2917 rc = ethtool_get_rxfh(dev, useraddr);
2920 rc = ethtool_set_rxfh(dev, useraddr);
2922 case ETHTOOL_GFEATURES:
2923 rc = ethtool_get_features(dev, useraddr);
2925 case ETHTOOL_SFEATURES:
2926 rc = ethtool_set_features(dev, useraddr);
2928 case ETHTOOL_GTXCSUM:
2929 case ETHTOOL_GRXCSUM:
2934 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2936 case ETHTOOL_STXCSUM:
2937 case ETHTOOL_SRXCSUM:
2942 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2944 case ETHTOOL_GCHANNELS:
2945 rc = ethtool_get_channels(dev, useraddr);
2947 case ETHTOOL_SCHANNELS:
2948 rc = ethtool_set_channels(dev, useraddr);
2950 case ETHTOOL_SET_DUMP:
2951 rc = ethtool_set_dump(dev, useraddr);
2953 case ETHTOOL_GET_DUMP_FLAG:
2954 rc = ethtool_get_dump_flag(dev, useraddr);
2956 case ETHTOOL_GET_DUMP_DATA:
2957 rc = ethtool_get_dump_data(dev, useraddr);
2959 case ETHTOOL_GET_TS_INFO:
2960 rc = ethtool_get_ts_info(dev, useraddr);
2962 case ETHTOOL_GMODULEINFO:
2963 rc = ethtool_get_module_info(dev, useraddr);
2965 case ETHTOOL_GMODULEEEPROM:
2966 rc = ethtool_get_module_eeprom(dev, useraddr);
2968 case ETHTOOL_GTUNABLE:
2969 rc = ethtool_get_tunable(dev, useraddr);
2971 case ETHTOOL_STUNABLE:
2972 rc = ethtool_set_tunable(dev, useraddr);
2974 case ETHTOOL_GPHYSTATS:
2975 rc = ethtool_get_phy_stats(dev, useraddr);
2977 case ETHTOOL_PERQUEUE:
2978 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2980 case ETHTOOL_GLINKSETTINGS:
2981 rc = ethtool_get_link_ksettings(dev, useraddr);
2983 case ETHTOOL_SLINKSETTINGS:
2984 rc = ethtool_set_link_ksettings(dev, useraddr);
2986 case ETHTOOL_PHY_GTUNABLE:
2987 rc = get_phy_tunable(dev, useraddr);
2989 case ETHTOOL_PHY_STUNABLE:
2990 rc = set_phy_tunable(dev, useraddr);
2992 case ETHTOOL_GFECPARAM:
2993 rc = ethtool_get_fecparam(dev, useraddr);
2995 case ETHTOOL_SFECPARAM:
2996 rc = ethtool_set_fecparam(dev, useraddr);
3002 if (dev->ethtool_ops->complete)
3003 dev->ethtool_ops->complete(dev);
3005 if (old_features != dev->features)
3006 netdev_features_change(dev);
3008 if (dev->dev.parent)
3009 pm_runtime_put(dev->dev.parent);
3014 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3016 struct ethtool_devlink_compat *state;
3020 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3023 state = kzalloc(sizeof(*state), GFP_KERNEL);
3028 case ETHTOOL_FLASHDEV:
3029 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3033 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3038 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3044 case ETHTOOL_FLASHDEV:
3046 rc = devlink_compat_flash_update(state->devlink,
3049 case ETHTOOL_GDRVINFO:
3051 devlink_compat_running_version(state->devlink,
3052 state->info.fw_version,
3053 sizeof(state->info.fw_version));
3054 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3063 devlink_put(state->devlink);
3068 struct ethtool_rx_flow_key {
3069 struct flow_dissector_key_basic basic;
3071 struct flow_dissector_key_ipv4_addrs ipv4;
3072 struct flow_dissector_key_ipv6_addrs ipv6;
3074 struct flow_dissector_key_ports tp;
3075 struct flow_dissector_key_ip ip;
3076 struct flow_dissector_key_vlan vlan;
3077 struct flow_dissector_key_eth_addrs eth_addrs;
3078 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3080 struct ethtool_rx_flow_match {
3081 struct flow_dissector dissector;
3082 struct ethtool_rx_flow_key key;
3083 struct ethtool_rx_flow_key mask;
3086 struct ethtool_rx_flow_rule *
3087 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3089 const struct ethtool_rx_flow_spec *fs = input->fs;
3090 static struct in6_addr zero_addr = {};
3091 struct ethtool_rx_flow_match *match;
3092 struct ethtool_rx_flow_rule *flow;
3093 struct flow_action_entry *act;
3095 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3096 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3098 return ERR_PTR(-ENOMEM);
3100 /* ethtool_rx supports only one single action per rule. */
3101 flow->rule = flow_rule_alloc(1);
3104 return ERR_PTR(-ENOMEM);
3107 match = (struct ethtool_rx_flow_match *)flow->priv;
3108 flow->rule->match.dissector = &match->dissector;
3109 flow->rule->match.mask = &match->mask;
3110 flow->rule->match.key = &match->key;
3112 match->mask.basic.n_proto = htons(0xffff);
3114 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3116 const struct ethhdr *ether_spec, *ether_m_spec;
3118 ether_spec = &fs->h_u.ether_spec;
3119 ether_m_spec = &fs->m_u.ether_spec;
3121 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3122 ether_addr_copy(match->key.eth_addrs.src,
3123 ether_spec->h_source);
3124 ether_addr_copy(match->mask.eth_addrs.src,
3125 ether_m_spec->h_source);
3127 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3128 ether_addr_copy(match->key.eth_addrs.dst,
3129 ether_spec->h_dest);
3130 ether_addr_copy(match->mask.eth_addrs.dst,
3131 ether_m_spec->h_dest);
3133 if (ether_m_spec->h_proto) {
3134 match->key.basic.n_proto = ether_spec->h_proto;
3135 match->mask.basic.n_proto = ether_m_spec->h_proto;
3141 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3143 match->key.basic.n_proto = htons(ETH_P_IP);
3145 v4_spec = &fs->h_u.tcp_ip4_spec;
3146 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3148 if (v4_m_spec->ip4src) {
3149 match->key.ipv4.src = v4_spec->ip4src;
3150 match->mask.ipv4.src = v4_m_spec->ip4src;
3152 if (v4_m_spec->ip4dst) {
3153 match->key.ipv4.dst = v4_spec->ip4dst;
3154 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3156 if (v4_m_spec->ip4src ||
3157 v4_m_spec->ip4dst) {
3158 match->dissector.used_keys |=
3159 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3160 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3161 offsetof(struct ethtool_rx_flow_key, ipv4);
3163 if (v4_m_spec->psrc) {
3164 match->key.tp.src = v4_spec->psrc;
3165 match->mask.tp.src = v4_m_spec->psrc;
3167 if (v4_m_spec->pdst) {
3168 match->key.tp.dst = v4_spec->pdst;
3169 match->mask.tp.dst = v4_m_spec->pdst;
3171 if (v4_m_spec->psrc ||
3173 match->dissector.used_keys |=
3174 BIT(FLOW_DISSECTOR_KEY_PORTS);
3175 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3176 offsetof(struct ethtool_rx_flow_key, tp);
3178 if (v4_m_spec->tos) {
3179 match->key.ip.tos = v4_spec->tos;
3180 match->mask.ip.tos = v4_m_spec->tos;
3181 match->dissector.used_keys |=
3182 BIT(FLOW_DISSECTOR_KEY_IP);
3183 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3184 offsetof(struct ethtool_rx_flow_key, ip);
3190 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3192 match->key.basic.n_proto = htons(ETH_P_IPV6);
3194 v6_spec = &fs->h_u.tcp_ip6_spec;
3195 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3196 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
3197 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3198 sizeof(match->key.ipv6.src));
3199 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3200 sizeof(match->mask.ipv6.src));
3202 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3203 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3204 sizeof(match->key.ipv6.dst));
3205 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3206 sizeof(match->mask.ipv6.dst));
3208 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
3209 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3210 match->dissector.used_keys |=
3211 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3212 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3213 offsetof(struct ethtool_rx_flow_key, ipv6);
3215 if (v6_m_spec->psrc) {
3216 match->key.tp.src = v6_spec->psrc;
3217 match->mask.tp.src = v6_m_spec->psrc;
3219 if (v6_m_spec->pdst) {
3220 match->key.tp.dst = v6_spec->pdst;
3221 match->mask.tp.dst = v6_m_spec->pdst;
3223 if (v6_m_spec->psrc ||
3225 match->dissector.used_keys |=
3226 BIT(FLOW_DISSECTOR_KEY_PORTS);
3227 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3228 offsetof(struct ethtool_rx_flow_key, tp);
3230 if (v6_m_spec->tclass) {
3231 match->key.ip.tos = v6_spec->tclass;
3232 match->mask.ip.tos = v6_m_spec->tclass;
3233 match->dissector.used_keys |=
3234 BIT(FLOW_DISSECTOR_KEY_IP);
3235 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3236 offsetof(struct ethtool_rx_flow_key, ip);
3241 ethtool_rx_flow_rule_destroy(flow);
3242 return ERR_PTR(-EINVAL);
3245 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3248 match->key.basic.ip_proto = IPPROTO_TCP;
3249 match->mask.basic.ip_proto = 0xff;
3253 match->key.basic.ip_proto = IPPROTO_UDP;
3254 match->mask.basic.ip_proto = 0xff;
3258 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3259 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3260 offsetof(struct ethtool_rx_flow_key, basic);
3262 if (fs->flow_type & FLOW_EXT) {
3263 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3264 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3266 if (ext_m_spec->vlan_etype) {
3267 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3268 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3271 if (ext_m_spec->vlan_tci) {
3272 match->key.vlan.vlan_id =
3273 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3274 match->mask.vlan.vlan_id =
3275 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3277 match->key.vlan.vlan_dei =
3278 !!(ext_h_spec->vlan_tci & htons(0x1000));
3279 match->mask.vlan.vlan_dei =
3280 !!(ext_m_spec->vlan_tci & htons(0x1000));
3282 match->key.vlan.vlan_priority =
3283 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3284 match->mask.vlan.vlan_priority =
3285 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3288 if (ext_m_spec->vlan_etype ||
3289 ext_m_spec->vlan_tci) {
3290 match->dissector.used_keys |=
3291 BIT(FLOW_DISSECTOR_KEY_VLAN);
3292 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3293 offsetof(struct ethtool_rx_flow_key, vlan);
3296 if (fs->flow_type & FLOW_MAC_EXT) {
3297 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3298 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3300 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3302 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3305 match->dissector.used_keys |=
3306 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3307 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3308 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3311 act = &flow->rule->action.entries[0];
3312 switch (fs->ring_cookie) {
3313 case RX_CLS_FLOW_DISC:
3314 act->id = FLOW_ACTION_DROP;
3316 case RX_CLS_FLOW_WAKE:
3317 act->id = FLOW_ACTION_WAKE;
3320 act->id = FLOW_ACTION_QUEUE;
3321 if (fs->flow_type & FLOW_RSS)
3322 act->queue.ctx = input->rss_ctx;
3324 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3325 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3331 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3333 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3338 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);