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 <linux/utsname.h>
30 #include <net/devlink.h>
32 #include <net/xdp_sock_drv.h>
33 #include <net/flow_offload.h>
34 #include <linux/ethtool_netlink.h>
37 /* State held across locks and calls for commands which have devlink fallback */
38 struct ethtool_devlink_compat {
39 struct devlink *devlink;
41 struct ethtool_flash efl;
42 struct ethtool_drvinfo info;
46 static struct devlink *netdev_to_devlink_get(struct net_device *dev)
48 if (!dev->devlink_port)
50 return devlink_try_get(dev->devlink_port->devlink);
54 * Some useful ethtool_ops methods that're device independent.
55 * If we find that all drivers want to do the same thing here,
56 * we can turn these into dev_() function calls.
59 u32 ethtool_op_get_link(struct net_device *dev)
61 /* Synchronize carrier state with link watch, see also rtnl_getlink() */
62 linkwatch_sync_dev(dev);
64 return netif_carrier_ok(dev) ? 1 : 0;
66 EXPORT_SYMBOL(ethtool_op_get_link);
68 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
70 info->so_timestamping =
71 SOF_TIMESTAMPING_TX_SOFTWARE |
72 SOF_TIMESTAMPING_RX_SOFTWARE |
73 SOF_TIMESTAMPING_SOFTWARE;
77 EXPORT_SYMBOL(ethtool_op_get_ts_info);
79 /* Handlers for each ethtool command */
81 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
83 struct ethtool_gfeatures cmd = {
84 .cmd = ETHTOOL_GFEATURES,
85 .size = ETHTOOL_DEV_FEATURE_WORDS,
87 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
92 /* in case feature bits run out again */
93 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
95 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
96 features[i].available = (u32)(dev->hw_features >> (32 * i));
97 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
98 features[i].active = (u32)(dev->features >> (32 * i));
99 features[i].never_changed =
100 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
103 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
104 if (get_user(copy_size, sizeaddr))
107 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
108 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
110 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
112 useraddr += sizeof(cmd);
113 if (copy_to_user(useraddr, features,
114 array_size(copy_size, sizeof(*features))))
120 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
122 struct ethtool_sfeatures cmd;
123 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
124 netdev_features_t wanted = 0, valid = 0;
127 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
129 useraddr += sizeof(cmd);
131 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
134 if (copy_from_user(features, useraddr, sizeof(features)))
137 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
138 valid |= (netdev_features_t)features[i].valid << (32 * i);
139 wanted |= (netdev_features_t)features[i].requested << (32 * i);
142 if (valid & ~NETIF_F_ETHTOOL_BITS)
145 if (valid & ~dev->hw_features) {
146 valid &= dev->hw_features;
147 ret |= ETHTOOL_F_UNSUPPORTED;
150 dev->wanted_features &= ~valid;
151 dev->wanted_features |= wanted & valid;
152 __netdev_update_features(dev);
154 if ((dev->wanted_features ^ dev->features) & valid)
155 ret |= ETHTOOL_F_WISH;
160 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
162 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
163 const struct ethtool_ops *ops = dev->ethtool_ops;
165 if (sset == ETH_SS_FEATURES)
166 return ARRAY_SIZE(netdev_features_strings);
168 if (sset == ETH_SS_RSS_HASH_FUNCS)
169 return ARRAY_SIZE(rss_hash_func_strings);
171 if (sset == ETH_SS_TUNABLES)
172 return ARRAY_SIZE(tunable_strings);
174 if (sset == ETH_SS_PHY_TUNABLES)
175 return ARRAY_SIZE(phy_tunable_strings);
177 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
178 !ops->get_ethtool_phy_stats &&
179 phy_ops && phy_ops->get_sset_count)
180 return phy_ops->get_sset_count(dev->phydev);
182 if (sset == ETH_SS_LINK_MODES)
183 return __ETHTOOL_LINK_MODE_MASK_NBITS;
185 if (ops->get_sset_count && ops->get_strings)
186 return ops->get_sset_count(dev, sset);
191 static void __ethtool_get_strings(struct net_device *dev,
192 u32 stringset, u8 *data)
194 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
195 const struct ethtool_ops *ops = dev->ethtool_ops;
197 if (stringset == ETH_SS_FEATURES)
198 memcpy(data, netdev_features_strings,
199 sizeof(netdev_features_strings));
200 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
201 memcpy(data, rss_hash_func_strings,
202 sizeof(rss_hash_func_strings));
203 else if (stringset == ETH_SS_TUNABLES)
204 memcpy(data, tunable_strings, sizeof(tunable_strings));
205 else if (stringset == ETH_SS_PHY_TUNABLES)
206 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
207 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
208 !ops->get_ethtool_phy_stats && phy_ops &&
209 phy_ops->get_strings)
210 phy_ops->get_strings(dev->phydev, data);
211 else if (stringset == ETH_SS_LINK_MODES)
212 memcpy(data, link_mode_names,
213 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
215 /* ops->get_strings is valid because checked earlier */
216 ops->get_strings(dev, stringset, data);
219 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
221 /* feature masks of legacy discrete ethtool ops */
224 case ETHTOOL_GTXCSUM:
225 case ETHTOOL_STXCSUM:
226 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
228 case ETHTOOL_GRXCSUM:
229 case ETHTOOL_SRXCSUM:
230 return NETIF_F_RXCSUM;
233 return NETIF_F_SG | NETIF_F_FRAGLIST;
236 return NETIF_F_ALL_TSO;
248 static int ethtool_get_one_feature(struct net_device *dev,
249 char __user *useraddr, u32 ethcmd)
251 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
252 struct ethtool_value edata = {
254 .data = !!(dev->features & mask),
257 if (copy_to_user(useraddr, &edata, sizeof(edata)))
262 static int ethtool_set_one_feature(struct net_device *dev,
263 void __user *useraddr, u32 ethcmd)
265 struct ethtool_value edata;
266 netdev_features_t mask;
268 if (copy_from_user(&edata, useraddr, sizeof(edata)))
271 mask = ethtool_get_feature_mask(ethcmd);
272 mask &= dev->hw_features;
277 dev->wanted_features |= mask;
279 dev->wanted_features &= ~mask;
281 __netdev_update_features(dev);
286 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
287 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
288 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
289 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
292 static u32 __ethtool_get_flags(struct net_device *dev)
296 if (dev->features & NETIF_F_LRO)
297 flags |= ETH_FLAG_LRO;
298 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
299 flags |= ETH_FLAG_RXVLAN;
300 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
301 flags |= ETH_FLAG_TXVLAN;
302 if (dev->features & NETIF_F_NTUPLE)
303 flags |= ETH_FLAG_NTUPLE;
304 if (dev->features & NETIF_F_RXHASH)
305 flags |= ETH_FLAG_RXHASH;
310 static int __ethtool_set_flags(struct net_device *dev, u32 data)
312 netdev_features_t features = 0, changed;
314 if (data & ~ETH_ALL_FLAGS)
317 if (data & ETH_FLAG_LRO)
318 features |= NETIF_F_LRO;
319 if (data & ETH_FLAG_RXVLAN)
320 features |= NETIF_F_HW_VLAN_CTAG_RX;
321 if (data & ETH_FLAG_TXVLAN)
322 features |= NETIF_F_HW_VLAN_CTAG_TX;
323 if (data & ETH_FLAG_NTUPLE)
324 features |= NETIF_F_NTUPLE;
325 if (data & ETH_FLAG_RXHASH)
326 features |= NETIF_F_RXHASH;
328 /* allow changing only bits set in hw_features */
329 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
330 if (changed & ~dev->hw_features)
331 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
333 dev->wanted_features =
334 (dev->wanted_features & ~changed) | (features & changed);
336 __netdev_update_features(dev);
341 /* Given two link masks, AND them together and save the result in dst. */
342 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
343 struct ethtool_link_ksettings *src)
345 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
346 unsigned int idx = 0;
348 for (; idx < size; idx++) {
349 dst->link_modes.supported[idx] &=
350 src->link_modes.supported[idx];
351 dst->link_modes.advertising[idx] &=
352 src->link_modes.advertising[idx];
355 EXPORT_SYMBOL(ethtool_intersect_link_masks);
357 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
363 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
365 /* return false if src had higher bits set. lower bits always updated. */
366 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
367 const unsigned long *src)
369 *legacy_u32 = src[0];
370 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
371 __ETHTOOL_LINK_MODE_MASK_NBITS;
373 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
375 /* return false if ksettings link modes had higher bits
376 * set. legacy_settings always updated (best effort)
379 convert_link_ksettings_to_legacy_settings(
380 struct ethtool_cmd *legacy_settings,
381 const struct ethtool_link_ksettings *link_ksettings)
385 memset(legacy_settings, 0, sizeof(*legacy_settings));
386 /* this also clears the deprecated fields in legacy structure:
392 retval &= ethtool_convert_link_mode_to_legacy_u32(
393 &legacy_settings->supported,
394 link_ksettings->link_modes.supported);
395 retval &= ethtool_convert_link_mode_to_legacy_u32(
396 &legacy_settings->advertising,
397 link_ksettings->link_modes.advertising);
398 retval &= ethtool_convert_link_mode_to_legacy_u32(
399 &legacy_settings->lp_advertising,
400 link_ksettings->link_modes.lp_advertising);
401 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
402 legacy_settings->duplex
403 = link_ksettings->base.duplex;
404 legacy_settings->port
405 = link_ksettings->base.port;
406 legacy_settings->phy_address
407 = link_ksettings->base.phy_address;
408 legacy_settings->autoneg
409 = link_ksettings->base.autoneg;
410 legacy_settings->mdio_support
411 = link_ksettings->base.mdio_support;
412 legacy_settings->eth_tp_mdix
413 = link_ksettings->base.eth_tp_mdix;
414 legacy_settings->eth_tp_mdix_ctrl
415 = link_ksettings->base.eth_tp_mdix_ctrl;
416 legacy_settings->transceiver
417 = link_ksettings->base.transceiver;
421 /* number of 32-bit words to store the user's link mode bitmaps */
422 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
423 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
425 /* layout of the struct passed from/to userland */
426 struct ethtool_link_usettings {
427 struct ethtool_link_settings base;
429 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
430 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
431 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
435 /* Internal kernel helper to query a device ethtool_link_settings. */
436 int __ethtool_get_link_ksettings(struct net_device *dev,
437 struct ethtool_link_ksettings *link_ksettings)
441 if (!dev->ethtool_ops->get_link_ksettings)
444 memset(link_ksettings, 0, sizeof(*link_ksettings));
445 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
447 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
449 /* convert ethtool_link_usettings in user space to a kernel internal
450 * ethtool_link_ksettings. return 0 on success, errno on error.
452 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
453 const void __user *from)
455 struct ethtool_link_usettings link_usettings;
457 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
460 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
461 bitmap_from_arr32(to->link_modes.supported,
462 link_usettings.link_modes.supported,
463 __ETHTOOL_LINK_MODE_MASK_NBITS);
464 bitmap_from_arr32(to->link_modes.advertising,
465 link_usettings.link_modes.advertising,
466 __ETHTOOL_LINK_MODE_MASK_NBITS);
467 bitmap_from_arr32(to->link_modes.lp_advertising,
468 link_usettings.link_modes.lp_advertising,
469 __ETHTOOL_LINK_MODE_MASK_NBITS);
474 /* Check if the user is trying to change anything besides speed/duplex */
475 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
477 struct ethtool_link_settings base2 = {};
479 base2.speed = cmd->base.speed;
480 base2.port = PORT_OTHER;
481 base2.duplex = cmd->base.duplex;
482 base2.cmd = cmd->base.cmd;
483 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
485 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
486 bitmap_empty(cmd->link_modes.supported,
487 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
488 bitmap_empty(cmd->link_modes.lp_advertising,
489 __ETHTOOL_LINK_MODE_MASK_NBITS);
492 /* convert a kernel internal ethtool_link_ksettings to
493 * ethtool_link_usettings in user space. return 0 on success, errno on
497 store_link_ksettings_for_user(void __user *to,
498 const struct ethtool_link_ksettings *from)
500 struct ethtool_link_usettings link_usettings;
502 memcpy(&link_usettings, from, sizeof(link_usettings));
503 bitmap_to_arr32(link_usettings.link_modes.supported,
504 from->link_modes.supported,
505 __ETHTOOL_LINK_MODE_MASK_NBITS);
506 bitmap_to_arr32(link_usettings.link_modes.advertising,
507 from->link_modes.advertising,
508 __ETHTOOL_LINK_MODE_MASK_NBITS);
509 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
510 from->link_modes.lp_advertising,
511 __ETHTOOL_LINK_MODE_MASK_NBITS);
513 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
519 /* Query device for its ethtool_link_settings. */
520 static int ethtool_get_link_ksettings(struct net_device *dev,
521 void __user *useraddr)
524 struct ethtool_link_ksettings link_ksettings;
527 if (!dev->ethtool_ops->get_link_ksettings)
530 /* handle bitmap nbits handshake */
531 if (copy_from_user(&link_ksettings.base, useraddr,
532 sizeof(link_ksettings.base)))
535 if (__ETHTOOL_LINK_MODE_MASK_NU32
536 != link_ksettings.base.link_mode_masks_nwords) {
537 /* wrong link mode nbits requested */
538 memset(&link_ksettings, 0, sizeof(link_ksettings));
539 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
540 /* send back number of words required as negative val */
541 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
542 "need too many bits for link modes!");
543 link_ksettings.base.link_mode_masks_nwords
544 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
546 /* copy the base fields back to user, not the link
549 if (copy_to_user(useraddr, &link_ksettings.base,
550 sizeof(link_ksettings.base)))
556 /* handshake successful: user/kernel agree on
557 * link_mode_masks_nwords
560 memset(&link_ksettings, 0, sizeof(link_ksettings));
561 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
565 /* make sure we tell the right values to user */
566 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
567 link_ksettings.base.link_mode_masks_nwords
568 = __ETHTOOL_LINK_MODE_MASK_NU32;
569 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
570 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
571 link_ksettings.base.rate_matching = RATE_MATCH_NONE;
573 return store_link_ksettings_for_user(useraddr, &link_ksettings);
576 /* Update device ethtool_link_settings. */
577 static int ethtool_set_link_ksettings(struct net_device *dev,
578 void __user *useraddr)
580 struct ethtool_link_ksettings link_ksettings = {};
585 if (!dev->ethtool_ops->set_link_ksettings)
588 /* make sure nbits field has expected value */
589 if (copy_from_user(&link_ksettings.base, useraddr,
590 sizeof(link_ksettings.base)))
593 if (__ETHTOOL_LINK_MODE_MASK_NU32
594 != link_ksettings.base.link_mode_masks_nwords)
597 /* copy the whole structure, now that we know it has expected
600 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
604 /* re-check nwords field, just in case */
605 if (__ETHTOOL_LINK_MODE_MASK_NU32
606 != link_ksettings.base.link_mode_masks_nwords)
609 if (link_ksettings.base.master_slave_cfg ||
610 link_ksettings.base.master_slave_state)
613 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
615 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
616 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
621 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
622 const struct ethtool_link_ksettings *cmd,
623 u32 *dev_speed, u8 *dev_duplex)
628 speed = cmd->base.speed;
629 duplex = cmd->base.duplex;
630 /* don't allow custom speed and duplex */
631 if (!ethtool_validate_speed(speed) ||
632 !ethtool_validate_duplex(duplex) ||
633 !ethtool_virtdev_validate_cmd(cmd))
636 *dev_duplex = duplex;
640 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
642 /* Query device for its ethtool_cmd settings.
644 * Backward compatibility note: for compatibility with legacy ethtool, this is
645 * now implemented via get_link_ksettings. When driver reports higher link mode
646 * bits, a kernel warning is logged once (with name of 1st driver/device) to
647 * recommend user to upgrade ethtool, but the command is successful (only the
648 * lower link mode bits reported back to user). Deprecated fields from
649 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
651 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
653 struct ethtool_link_ksettings link_ksettings;
654 struct ethtool_cmd cmd;
658 if (!dev->ethtool_ops->get_link_ksettings)
661 memset(&link_ksettings, 0, sizeof(link_ksettings));
662 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
665 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
667 /* send a sensible cmd tag back to user */
668 cmd.cmd = ETHTOOL_GSET;
670 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
676 /* Update device link settings with given ethtool_cmd.
678 * Backward compatibility note: for compatibility with legacy ethtool, this is
679 * now always implemented via set_link_settings. When user's request updates
680 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
681 * warning is logged once (with name of 1st driver/device) to recommend user to
682 * upgrade ethtool, and the request is rejected.
684 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
686 struct ethtool_link_ksettings link_ksettings;
687 struct ethtool_cmd cmd;
692 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
694 if (!dev->ethtool_ops->set_link_ksettings)
697 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
699 link_ksettings.base.link_mode_masks_nwords =
700 __ETHTOOL_LINK_MODE_MASK_NU32;
701 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
703 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
704 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
710 ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
712 const struct ethtool_ops *ops = dev->ethtool_ops;
713 struct device *parent = dev->dev.parent;
715 rsp->info.cmd = ETHTOOL_GDRVINFO;
716 strscpy(rsp->info.version, init_uts_ns.name.release,
717 sizeof(rsp->info.version));
718 if (ops->get_drvinfo) {
719 ops->get_drvinfo(dev, &rsp->info);
720 if (!rsp->info.bus_info[0] && parent)
721 strscpy(rsp->info.bus_info, dev_name(parent),
722 sizeof(rsp->info.bus_info));
723 if (!rsp->info.driver[0] && parent && parent->driver)
724 strscpy(rsp->info.driver, parent->driver->name,
725 sizeof(rsp->info.driver));
726 } else if (parent && parent->driver) {
727 strscpy(rsp->info.bus_info, dev_name(parent),
728 sizeof(rsp->info.bus_info));
729 strscpy(rsp->info.driver, parent->driver->name,
730 sizeof(rsp->info.driver));
731 } else if (dev->rtnl_link_ops) {
732 strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
733 sizeof(rsp->info.driver));
739 * this method of obtaining string set info is deprecated;
740 * Use ETHTOOL_GSSET_INFO instead.
742 if (ops->get_sset_count) {
745 rc = ops->get_sset_count(dev, ETH_SS_TEST);
747 rsp->info.testinfo_len = rc;
748 rc = ops->get_sset_count(dev, ETH_SS_STATS);
750 rsp->info.n_stats = rc;
751 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
753 rsp->info.n_priv_flags = rc;
755 if (ops->get_regs_len) {
756 int ret = ops->get_regs_len(dev);
759 rsp->info.regdump_len = ret;
762 if (ops->get_eeprom_len)
763 rsp->info.eedump_len = ops->get_eeprom_len(dev);
765 if (!rsp->info.fw_version[0])
766 rsp->devlink = netdev_to_devlink_get(dev);
771 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
772 void __user *useraddr)
774 struct ethtool_sset_info info;
776 int i, idx = 0, n_bits = 0, ret, rc;
777 u32 *info_buf = NULL;
779 if (copy_from_user(&info, useraddr, sizeof(info)))
782 /* store copy of mask, because we zero struct later on */
783 sset_mask = info.sset_mask;
787 /* calculate size of return buffer */
788 n_bits = hweight64(sset_mask);
790 memset(&info, 0, sizeof(info));
791 info.cmd = ETHTOOL_GSSET_INFO;
793 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
798 * fill return buffer based on input bitmask and successful
799 * get_sset_count return
801 for (i = 0; i < 64; i++) {
802 if (!(sset_mask & (1ULL << i)))
805 rc = __ethtool_get_sset_count(dev, i);
807 info.sset_mask |= (1ULL << i);
808 info_buf[idx++] = rc;
813 if (copy_to_user(useraddr, &info, sizeof(info)))
816 useraddr += offsetof(struct ethtool_sset_info, data);
817 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
827 static noinline_for_stack int
828 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
829 const struct compat_ethtool_rxnfc __user *useraddr,
832 struct compat_ethtool_rxnfc crxnfc = {};
834 /* We expect there to be holes between fs.m_ext and
835 * fs.ring_cookie and at the end of fs, but nowhere else.
836 * On non-x86, no conversion should be needed.
838 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
839 sizeof(struct compat_ethtool_rxnfc) !=
840 sizeof(struct ethtool_rxnfc));
841 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
842 sizeof(useraddr->fs.m_ext) !=
843 offsetof(struct ethtool_rxnfc, fs.m_ext) +
844 sizeof(rxnfc->fs.m_ext));
845 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
846 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
847 offsetof(struct ethtool_rxnfc, fs.location) -
848 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
850 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
853 *rxnfc = (struct ethtool_rxnfc) {
855 .flow_type = crxnfc.flow_type,
858 .flow_type = crxnfc.fs.flow_type,
859 .h_u = crxnfc.fs.h_u,
860 .h_ext = crxnfc.fs.h_ext,
861 .m_u = crxnfc.fs.m_u,
862 .m_ext = crxnfc.fs.m_ext,
863 .ring_cookie = crxnfc.fs.ring_cookie,
864 .location = crxnfc.fs.location,
866 .rule_cnt = crxnfc.rule_cnt,
872 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
873 const void __user *useraddr,
876 if (compat_need_64bit_alignment_fixup())
877 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
879 if (copy_from_user(rxnfc, useraddr, size))
885 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
886 const struct ethtool_rxnfc *rxnfc,
887 size_t size, const u32 *rule_buf)
889 struct compat_ethtool_rxnfc crxnfc;
891 memset(&crxnfc, 0, sizeof(crxnfc));
892 crxnfc = (struct compat_ethtool_rxnfc) {
894 .flow_type = rxnfc->flow_type,
897 .flow_type = rxnfc->fs.flow_type,
898 .h_u = rxnfc->fs.h_u,
899 .h_ext = rxnfc->fs.h_ext,
900 .m_u = rxnfc->fs.m_u,
901 .m_ext = rxnfc->fs.m_ext,
902 .ring_cookie = rxnfc->fs.ring_cookie,
903 .location = rxnfc->fs.location,
905 .rule_cnt = rxnfc->rule_cnt,
908 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
914 static int ethtool_rxnfc_copy_struct(u32 cmd, struct ethtool_rxnfc *info,
915 size_t *info_size, void __user *useraddr)
917 /* struct ethtool_rxnfc was originally defined for
918 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
919 * members. User-space might still be using that
922 if (cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH)
923 *info_size = (offsetof(struct ethtool_rxnfc, data) +
926 if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
929 if ((cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH) && info->flow_type & FLOW_RSS) {
930 *info_size = sizeof(*info);
931 if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
933 /* Since malicious users may modify the original data,
934 * we need to check whether FLOW_RSS is still requested.
936 if (!(info->flow_type & FLOW_RSS))
940 if (info->cmd != cmd)
946 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
947 const struct ethtool_rxnfc *rxnfc,
948 size_t size, const u32 *rule_buf)
952 if (compat_need_64bit_alignment_fixup()) {
953 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
955 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
957 ret = copy_to_user(useraddr, rxnfc, size);
958 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
965 if (copy_to_user(useraddr, rule_buf,
966 rxnfc->rule_cnt * sizeof(u32)))
973 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
974 u32 cmd, void __user *useraddr)
976 const struct ethtool_ops *ops = dev->ethtool_ops;
977 struct ethtool_rxnfc info;
978 size_t info_size = sizeof(info);
984 rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
989 struct ethtool_rxfh_param rxfh = {};
991 rc = ops->get_rxfh(dev, &rxfh);
995 /* Sanity check: if symmetric-xor is set, then:
996 * 1 - no other fields besides IP src/dst and/or L4 src/dst
997 * 2 - If src is set, dst must also be set
999 if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1000 ((info.data & ~(RXH_IP_SRC | RXH_IP_DST |
1001 RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
1002 (!!(info.data & RXH_IP_SRC) ^ !!(info.data & RXH_IP_DST)) ||
1003 (!!(info.data & RXH_L4_B_0_1) ^ !!(info.data & RXH_L4_B_2_3))))
1007 rc = ops->set_rxnfc(dev, &info);
1011 if (cmd == ETHTOOL_SRXCLSRLINS &&
1012 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
1018 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1019 u32 cmd, void __user *useraddr)
1021 struct ethtool_rxnfc info;
1022 size_t info_size = sizeof(info);
1023 const struct ethtool_ops *ops = dev->ethtool_ops;
1025 void *rule_buf = NULL;
1027 if (!ops->get_rxnfc)
1030 ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1034 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1035 if (info.rule_cnt > 0) {
1036 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1037 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1044 ret = ops->get_rxnfc(dev, &info, rule_buf);
1048 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1055 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1056 struct ethtool_rxnfc *rx_rings,
1061 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1064 /* Validate ring indices */
1065 for (i = 0; i < size; i++)
1066 if (indir[i] >= rx_rings->data)
1072 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1074 void netdev_rss_key_fill(void *buffer, size_t len)
1076 BUG_ON(len > sizeof(netdev_rss_key));
1077 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1078 memcpy(buffer, netdev_rss_key, len);
1080 EXPORT_SYMBOL(netdev_rss_key_fill);
1082 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1083 void __user *useraddr)
1085 struct ethtool_rxfh_param rxfh = {};
1089 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1090 !dev->ethtool_ops->get_rxfh)
1092 rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1093 if (rxfh.indir_size == 0)
1096 if (copy_from_user(&user_size,
1097 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1101 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1102 &rxfh.indir_size, sizeof(rxfh.indir_size)))
1105 /* If the user buffer size is 0, this is just a query for the
1106 * device table size. Otherwise, if it's smaller than the
1107 * device table size it's an error.
1109 if (user_size < rxfh.indir_size)
1110 return user_size == 0 ? 0 : -EINVAL;
1112 rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
1116 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
1119 if (copy_to_user(useraddr +
1120 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1121 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
1129 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1130 void __user *useraddr)
1132 const struct ethtool_ops *ops = dev->ethtool_ops;
1133 struct ethtool_rxfh_param rxfh_dev = {};
1134 struct netlink_ext_ack *extack = NULL;
1135 struct ethtool_rxnfc rx_rings;
1138 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1140 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1144 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1145 if (rxfh_dev.indir_size == 0)
1148 if (copy_from_user(&user_size,
1149 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1153 if (user_size != 0 && user_size != rxfh_dev.indir_size)
1156 rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
1157 sizeof(rxfh_dev.indir[0]), GFP_USER);
1158 if (!rxfh_dev.indir)
1161 rx_rings.cmd = ETHTOOL_GRXRINGS;
1162 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1166 if (user_size == 0) {
1167 u32 *indir = rxfh_dev.indir;
1169 for (i = 0; i < rxfh_dev.indir_size; i++)
1170 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1172 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1173 useraddr + ringidx_offset,
1175 rxfh_dev.indir_size);
1180 rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
1181 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1185 /* indicate whether rxfh was set to default */
1187 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1189 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1192 kfree(rxfh_dev.indir);
1196 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1197 void __user *useraddr)
1199 const struct ethtool_ops *ops = dev->ethtool_ops;
1200 struct ethtool_rxfh_param rxfh_dev = {};
1201 u32 user_indir_size, user_key_size;
1202 struct ethtool_rxfh rxfh;
1211 if (ops->get_rxfh_indir_size)
1212 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1213 if (ops->get_rxfh_key_size)
1214 rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
1216 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1218 user_indir_size = rxfh.indir_size;
1219 user_key_size = rxfh.key_size;
1221 /* Check that reserved fields are 0 for now */
1222 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1224 /* Most drivers don't handle rss_context, check it's 0 as well */
1225 if (rxfh.rss_context && !ops->cap_rss_ctx_supported)
1228 rxfh.indir_size = rxfh_dev.indir_size;
1229 rxfh.key_size = rxfh_dev.key_size;
1230 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1233 if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
1234 (user_key_size && user_key_size != rxfh_dev.key_size))
1237 indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
1238 total_size = indir_bytes + user_key_size;
1239 rss_config = kzalloc(total_size, GFP_USER);
1243 if (user_indir_size)
1244 rxfh_dev.indir = (u32 *)rss_config;
1247 rxfh_dev.key = rss_config + indir_bytes;
1249 rxfh_dev.rss_context = rxfh.rss_context;
1251 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
1255 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1256 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
1258 } else if (copy_to_user(useraddr +
1259 offsetof(struct ethtool_rxfh, input_xfrm),
1260 &rxfh_dev.input_xfrm,
1261 sizeof(rxfh.input_xfrm))) {
1263 } else if (copy_to_user(useraddr +
1264 offsetof(struct ethtool_rxfh, rss_config[0]),
1265 rss_config, total_size)) {
1274 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1275 void __user *useraddr)
1277 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1278 const struct ethtool_ops *ops = dev->ethtool_ops;
1279 u32 dev_indir_size = 0, dev_key_size = 0, i;
1280 struct ethtool_rxfh_param rxfh_dev = {};
1281 struct netlink_ext_ack *extack = NULL;
1282 struct ethtool_rxnfc rx_rings;
1283 struct ethtool_rxfh rxfh;
1284 u32 indir_bytes = 0;
1288 if (!ops->get_rxnfc || !ops->set_rxfh)
1291 if (ops->get_rxfh_indir_size)
1292 dev_indir_size = ops->get_rxfh_indir_size(dev);
1293 if (ops->get_rxfh_key_size)
1294 dev_key_size = ops->get_rxfh_key_size(dev);
1296 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1299 /* Check that reserved fields are 0 for now */
1300 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1302 /* Most drivers don't handle rss_context, check it's 0 as well */
1303 if (rxfh.rss_context && !ops->cap_rss_ctx_supported)
1305 /* Check input data transformation capabilities */
1306 if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
1307 rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
1309 if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1310 !ops->cap_rss_sym_xor_supported)
1313 /* If either indir, hash key or function is valid, proceed further.
1314 * Must request at least one change: indir size, hash key, function
1315 * or input transformation.
1317 if ((rxfh.indir_size &&
1318 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1319 rxfh.indir_size != dev_indir_size) ||
1320 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1321 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1322 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
1323 rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
1326 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1327 indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
1329 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1333 rx_rings.cmd = ETHTOOL_GRXRINGS;
1334 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1338 /* rxfh.indir_size == 0 means reset the indir table to default (master
1339 * context) or delete the context (other RSS contexts).
1340 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1342 if (rxfh.indir_size &&
1343 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1344 rxfh_dev.indir = (u32 *)rss_config;
1345 rxfh_dev.indir_size = dev_indir_size;
1346 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1347 useraddr + rss_cfg_offset,
1352 } else if (rxfh.indir_size == 0) {
1353 if (rxfh.rss_context == 0) {
1356 rxfh_dev.indir = (u32 *)rss_config;
1357 rxfh_dev.indir_size = dev_indir_size;
1358 indir = rxfh_dev.indir;
1359 for (i = 0; i < dev_indir_size; i++)
1360 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1362 rxfh_dev.rss_delete = true;
1366 if (rxfh.key_size) {
1367 rxfh_dev.key_size = dev_key_size;
1368 rxfh_dev.key = rss_config + indir_bytes;
1369 if (copy_from_user(rxfh_dev.key,
1370 useraddr + rss_cfg_offset + indir_bytes,
1377 rxfh_dev.hfunc = rxfh.hfunc;
1378 rxfh_dev.rss_context = rxfh.rss_context;
1379 rxfh_dev.input_xfrm = rxfh.input_xfrm;
1381 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1385 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1386 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
1389 if (!rxfh_dev.rss_context) {
1390 /* indicate whether rxfh was set to default */
1391 if (rxfh.indir_size == 0)
1392 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1393 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1394 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1402 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1404 struct ethtool_regs regs;
1405 const struct ethtool_ops *ops = dev->ethtool_ops;
1409 if (!ops->get_regs || !ops->get_regs_len)
1412 if (copy_from_user(®s, useraddr, sizeof(regs)))
1415 reglen = ops->get_regs_len(dev);
1419 if (regs.len > reglen)
1422 regbuf = vzalloc(reglen);
1426 if (regs.len < reglen)
1429 ops->get_regs(dev, ®s, regbuf);
1432 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1434 useraddr += offsetof(struct ethtool_regs, data);
1435 if (copy_to_user(useraddr, regbuf, reglen))
1444 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1446 struct ethtool_value reset;
1449 if (!dev->ethtool_ops->reset)
1452 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1455 ret = dev->ethtool_ops->reset(dev, &reset.data);
1459 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1464 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1466 struct ethtool_wolinfo wol;
1468 if (!dev->ethtool_ops->get_wol)
1471 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1472 wol.cmd = ETHTOOL_GWOL;
1473 dev->ethtool_ops->get_wol(dev, &wol);
1475 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1480 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1482 struct ethtool_wolinfo wol, cur_wol;
1485 if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
1488 memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
1489 cur_wol.cmd = ETHTOOL_GWOL;
1490 dev->ethtool_ops->get_wol(dev, &cur_wol);
1492 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1495 if (wol.wolopts & ~cur_wol.supported)
1498 if (wol.wolopts == cur_wol.wolopts &&
1499 !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
1502 ret = dev->ethtool_ops->set_wol(dev, &wol);
1506 dev->wol_enabled = !!wol.wolopts;
1507 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1512 static void eee_to_keee(struct ethtool_keee *keee,
1513 const struct ethtool_eee *eee)
1515 memset(keee, 0, sizeof(*keee));
1517 keee->eee_enabled = eee->eee_enabled;
1518 keee->tx_lpi_enabled = eee->tx_lpi_enabled;
1519 keee->tx_lpi_timer = eee->tx_lpi_timer;
1521 ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
1525 static void keee_to_eee(struct ethtool_eee *eee,
1526 const struct ethtool_keee *keee)
1530 memset(eee, 0, sizeof(*eee));
1532 eee->eee_active = keee->eee_active;
1533 eee->eee_enabled = keee->eee_enabled;
1534 eee->tx_lpi_enabled = keee->tx_lpi_enabled;
1535 eee->tx_lpi_timer = keee->tx_lpi_timer;
1537 overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
1539 ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
1541 ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
1542 keee->lp_advertised);
1544 pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
1547 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1549 struct ethtool_keee keee;
1550 struct ethtool_eee eee;
1553 if (!dev->ethtool_ops->get_eee)
1556 memset(&keee, 0, sizeof(keee));
1557 rc = dev->ethtool_ops->get_eee(dev, &keee);
1561 keee_to_eee(&eee, &keee);
1562 if (copy_to_user(useraddr, &eee, sizeof(eee)))
1568 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1570 struct ethtool_keee keee;
1571 struct ethtool_eee eee;
1574 if (!dev->ethtool_ops->set_eee)
1577 if (copy_from_user(&eee, useraddr, sizeof(eee)))
1580 eee_to_keee(&keee, &eee);
1581 ret = dev->ethtool_ops->set_eee(dev, &keee);
1583 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1587 static int ethtool_nway_reset(struct net_device *dev)
1589 if (!dev->ethtool_ops->nway_reset)
1592 return dev->ethtool_ops->nway_reset(dev);
1595 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1597 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1598 int link = __ethtool_get_link(dev);
1604 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1609 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1610 int (*getter)(struct net_device *,
1611 struct ethtool_eeprom *, u8 *),
1614 struct ethtool_eeprom eeprom;
1615 void __user *userbuf = useraddr + sizeof(eeprom);
1616 u32 bytes_remaining;
1620 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1623 /* Check for wrap and zero */
1624 if (eeprom.offset + eeprom.len <= eeprom.offset)
1627 /* Check for exceeding total eeprom len */
1628 if (eeprom.offset + eeprom.len > total_len)
1631 data = kzalloc(PAGE_SIZE, GFP_USER);
1635 bytes_remaining = eeprom.len;
1636 while (bytes_remaining > 0) {
1637 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1639 ret = getter(dev, &eeprom, data);
1646 if (copy_to_user(userbuf, data, eeprom.len)) {
1650 userbuf += eeprom.len;
1651 eeprom.offset += eeprom.len;
1652 bytes_remaining -= eeprom.len;
1655 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1656 eeprom.offset -= eeprom.len;
1657 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1664 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1666 const struct ethtool_ops *ops = dev->ethtool_ops;
1668 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1669 !ops->get_eeprom_len(dev))
1672 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1673 ops->get_eeprom_len(dev));
1676 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1678 struct ethtool_eeprom eeprom;
1679 const struct ethtool_ops *ops = dev->ethtool_ops;
1680 void __user *userbuf = useraddr + sizeof(eeprom);
1681 u32 bytes_remaining;
1685 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1686 !ops->get_eeprom_len(dev))
1689 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1692 /* Check for wrap and zero */
1693 if (eeprom.offset + eeprom.len <= eeprom.offset)
1696 /* Check for exceeding total eeprom len */
1697 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1700 data = kzalloc(PAGE_SIZE, GFP_USER);
1704 bytes_remaining = eeprom.len;
1705 while (bytes_remaining > 0) {
1706 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1708 if (copy_from_user(data, userbuf, eeprom.len)) {
1712 ret = ops->set_eeprom(dev, &eeprom, data);
1715 userbuf += eeprom.len;
1716 eeprom.offset += eeprom.len;
1717 bytes_remaining -= eeprom.len;
1724 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1725 void __user *useraddr)
1727 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1728 struct kernel_ethtool_coalesce kernel_coalesce = {};
1731 if (!dev->ethtool_ops->get_coalesce)
1734 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1739 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1745 ethtool_set_coalesce_supported(struct net_device *dev,
1746 struct ethtool_coalesce *coalesce)
1748 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1749 u32 nonzero_params = 0;
1751 if (coalesce->rx_coalesce_usecs)
1752 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1753 if (coalesce->rx_max_coalesced_frames)
1754 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1755 if (coalesce->rx_coalesce_usecs_irq)
1756 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1757 if (coalesce->rx_max_coalesced_frames_irq)
1758 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1759 if (coalesce->tx_coalesce_usecs)
1760 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1761 if (coalesce->tx_max_coalesced_frames)
1762 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1763 if (coalesce->tx_coalesce_usecs_irq)
1764 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1765 if (coalesce->tx_max_coalesced_frames_irq)
1766 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1767 if (coalesce->stats_block_coalesce_usecs)
1768 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1769 if (coalesce->use_adaptive_rx_coalesce)
1770 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1771 if (coalesce->use_adaptive_tx_coalesce)
1772 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1773 if (coalesce->pkt_rate_low)
1774 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1775 if (coalesce->rx_coalesce_usecs_low)
1776 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1777 if (coalesce->rx_max_coalesced_frames_low)
1778 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1779 if (coalesce->tx_coalesce_usecs_low)
1780 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1781 if (coalesce->tx_max_coalesced_frames_low)
1782 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1783 if (coalesce->pkt_rate_high)
1784 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1785 if (coalesce->rx_coalesce_usecs_high)
1786 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1787 if (coalesce->rx_max_coalesced_frames_high)
1788 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1789 if (coalesce->tx_coalesce_usecs_high)
1790 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1791 if (coalesce->tx_max_coalesced_frames_high)
1792 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1793 if (coalesce->rate_sample_interval)
1794 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1796 return (supported_params & nonzero_params) == nonzero_params;
1799 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1800 void __user *useraddr)
1802 struct kernel_ethtool_coalesce kernel_coalesce = {};
1803 struct ethtool_coalesce coalesce;
1806 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1809 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1814 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1817 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1820 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1823 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1827 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1829 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1830 struct kernel_ethtool_ringparam kernel_ringparam = {};
1832 if (!dev->ethtool_ops->get_ringparam)
1835 dev->ethtool_ops->get_ringparam(dev, &ringparam,
1836 &kernel_ringparam, NULL);
1838 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1843 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1845 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1846 struct kernel_ethtool_ringparam kernel_ringparam;
1849 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1852 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1855 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
1857 /* ensure new ring parameters are within the maximums */
1858 if (ringparam.rx_pending > max.rx_max_pending ||
1859 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1860 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1861 ringparam.tx_pending > max.tx_max_pending)
1864 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1865 &kernel_ringparam, NULL);
1867 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1871 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1872 void __user *useraddr)
1874 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1876 if (!dev->ethtool_ops->get_channels)
1879 dev->ethtool_ops->get_channels(dev, &channels);
1881 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1886 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1887 void __user *useraddr)
1889 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1890 u16 from_channel, to_channel;
1891 u64 max_rxnfc_in_use;
1892 u32 max_rxfh_in_use;
1896 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1899 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1902 dev->ethtool_ops->get_channels(dev, &curr);
1904 if (channels.rx_count == curr.rx_count &&
1905 channels.tx_count == curr.tx_count &&
1906 channels.combined_count == curr.combined_count &&
1907 channels.other_count == curr.other_count)
1910 /* ensure new counts are within the maximums */
1911 if (channels.rx_count > curr.max_rx ||
1912 channels.tx_count > curr.max_tx ||
1913 channels.combined_count > curr.max_combined ||
1914 channels.other_count > curr.max_other)
1917 /* ensure there is at least one RX and one TX channel */
1918 if (!channels.combined_count &&
1919 (!channels.rx_count || !channels.tx_count))
1922 /* ensure the new Rx count fits within the configured Rx flow
1923 * indirection table/rxnfc settings */
1924 if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
1925 max_rxnfc_in_use = 0;
1926 if (!netif_is_rxfh_configured(dev) ||
1927 ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use))
1928 max_rxfh_in_use = 0;
1929 if (channels.combined_count + channels.rx_count <=
1930 max_t(u64, max_rxnfc_in_use, max_rxfh_in_use))
1933 /* Disabling channels, query zero-copy AF_XDP sockets */
1934 from_channel = channels.combined_count +
1935 min(channels.rx_count, channels.tx_count);
1936 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1937 for (i = from_channel; i < to_channel; i++)
1938 if (xsk_get_pool_from_qid(dev, i))
1941 ret = dev->ethtool_ops->set_channels(dev, &channels);
1943 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1947 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1949 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1951 if (!dev->ethtool_ops->get_pauseparam)
1954 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1956 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1961 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1963 struct ethtool_pauseparam pauseparam;
1966 if (!dev->ethtool_ops->set_pauseparam)
1969 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1972 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1974 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1978 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1980 struct ethtool_test test;
1981 const struct ethtool_ops *ops = dev->ethtool_ops;
1985 if (!ops->self_test || !ops->get_sset_count)
1988 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1991 WARN_ON(test_len == 0);
1993 if (copy_from_user(&test, useraddr, sizeof(test)))
1996 test.len = test_len;
1997 data = kcalloc(test_len, sizeof(u64), GFP_USER);
2001 netif_testing_on(dev);
2002 ops->self_test(dev, &test, data);
2003 netif_testing_off(dev);
2006 if (copy_to_user(useraddr, &test, sizeof(test)))
2008 useraddr += sizeof(test);
2009 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
2018 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
2020 struct ethtool_gstrings gstrings;
2024 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
2027 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
2030 if (ret > S32_MAX / ETH_GSTRING_LEN)
2037 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
2041 __ethtool_get_strings(dev, gstrings.string_set, data);
2047 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
2049 useraddr += sizeof(gstrings);
2051 copy_to_user(useraddr, data,
2052 array_size(gstrings.len, ETH_GSTRING_LEN)))
2061 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
2065 va_start(args, fmt);
2066 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
2069 *data += ETH_GSTRING_LEN;
2071 EXPORT_SYMBOL(ethtool_sprintf);
2073 void ethtool_puts(u8 **data, const char *str)
2075 strscpy(*data, str, ETH_GSTRING_LEN);
2076 *data += ETH_GSTRING_LEN;
2078 EXPORT_SYMBOL(ethtool_puts);
2080 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
2082 struct ethtool_value id;
2084 const struct ethtool_ops *ops = dev->ethtool_ops;
2085 netdevice_tracker dev_tracker;
2088 if (!ops->set_phys_id)
2094 if (copy_from_user(&id, useraddr, sizeof(id)))
2097 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2101 /* Drop the RTNL lock while waiting, but prevent reentry or
2102 * removal of the device.
2105 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2109 /* Driver will handle this itself */
2110 schedule_timeout_interruptible(
2111 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2113 /* Driver expects to be called at twice the frequency in rc */
2114 int n = rc * 2, interval = HZ / n;
2115 u64 count = mul_u32_u32(n, id.data);
2120 rc = ops->set_phys_id(dev,
2121 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2125 schedule_timeout_interruptible(interval);
2126 } while (!signal_pending(current) && (!id.data || i < count));
2130 netdev_put(dev, &dev_tracker);
2133 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2137 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2139 struct ethtool_stats stats;
2140 const struct ethtool_ops *ops = dev->ethtool_ops;
2144 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2147 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2150 if (n_stats > S32_MAX / sizeof(u64))
2152 WARN_ON_ONCE(!n_stats);
2153 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2156 stats.n_stats = n_stats;
2159 data = vzalloc(array_size(n_stats, sizeof(u64)));
2162 ops->get_ethtool_stats(dev, &stats, data);
2168 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2170 useraddr += sizeof(stats);
2171 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2180 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
2184 if (n_stats > S32_MAX / sizeof(u64))
2186 if (WARN_ON_ONCE(!n_stats))
2189 *data = vzalloc(array_size(n_stats, sizeof(u64)));
2196 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2197 struct ethtool_stats *stats,
2200 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2203 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2206 n_stats = phy_ops->get_sset_count(phydev);
2208 ret = ethtool_vzalloc_stats_array(n_stats, data);
2212 stats->n_stats = n_stats;
2213 return phy_ops->get_stats(phydev, stats, *data);
2216 static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2217 struct ethtool_stats *stats,
2220 const struct ethtool_ops *ops = dev->ethtool_ops;
2223 if (!ops || !ops->get_sset_count || ops->get_ethtool_phy_stats)
2226 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2228 ret = ethtool_vzalloc_stats_array(n_stats, data);
2232 stats->n_stats = n_stats;
2233 ops->get_ethtool_phy_stats(dev, stats, *data);
2238 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2240 struct phy_device *phydev = dev->phydev;
2241 struct ethtool_stats stats;
2243 int ret = -EOPNOTSUPP;
2245 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2249 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
2251 if (ret == -EOPNOTSUPP)
2252 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
2257 if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2262 useraddr += sizeof(stats);
2263 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2271 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2273 struct ethtool_perm_addr epaddr;
2275 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2278 if (epaddr.size < dev->addr_len)
2280 epaddr.size = dev->addr_len;
2282 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2284 useraddr += sizeof(epaddr);
2285 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2290 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2291 u32 cmd, u32 (*actor)(struct net_device *))
2293 struct ethtool_value edata = { .cmd = cmd };
2298 edata.data = actor(dev);
2300 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2305 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2306 void (*actor)(struct net_device *, u32))
2308 struct ethtool_value edata;
2313 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2316 actor(dev, edata.data);
2320 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2321 int (*actor)(struct net_device *, u32))
2323 struct ethtool_value edata;
2328 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2331 return actor(dev, edata.data);
2335 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2337 if (!dev->ethtool_ops->flash_device) {
2338 req->devlink = netdev_to_devlink_get(dev);
2342 return dev->ethtool_ops->flash_device(dev, &req->efl);
2345 static int ethtool_set_dump(struct net_device *dev,
2346 void __user *useraddr)
2348 struct ethtool_dump dump;
2350 if (!dev->ethtool_ops->set_dump)
2353 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2356 return dev->ethtool_ops->set_dump(dev, &dump);
2359 static int ethtool_get_dump_flag(struct net_device *dev,
2360 void __user *useraddr)
2363 struct ethtool_dump dump;
2364 const struct ethtool_ops *ops = dev->ethtool_ops;
2366 if (!ops->get_dump_flag)
2369 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2372 ret = ops->get_dump_flag(dev, &dump);
2376 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2381 static int ethtool_get_dump_data(struct net_device *dev,
2382 void __user *useraddr)
2386 struct ethtool_dump dump, tmp;
2387 const struct ethtool_ops *ops = dev->ethtool_ops;
2390 if (!ops->get_dump_data || !ops->get_dump_flag)
2393 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2396 memset(&tmp, 0, sizeof(tmp));
2397 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2398 ret = ops->get_dump_flag(dev, &tmp);
2402 len = min(tmp.len, dump.len);
2406 /* Don't ever let the driver think there's more space available
2407 * than it requested with .get_dump_flag().
2411 /* Always allocate enough space to hold the whole thing so that the
2412 * driver does not need to check the length and bother with partial
2415 data = vzalloc(tmp.len);
2418 ret = ops->get_dump_data(dev, &dump, data);
2422 /* There are two sane possibilities:
2423 * 1. The driver's .get_dump_data() does not touch dump.len.
2424 * 2. Or it may set dump.len to how much it really writes, which
2425 * should be tmp.len (or len if it can do a partial dump).
2426 * In any case respond to userspace with the actual length of data
2429 WARN_ON(dump.len != len && dump.len != tmp.len);
2432 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2436 useraddr += offsetof(struct ethtool_dump, data);
2437 if (copy_to_user(useraddr, data, len))
2444 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2446 struct ethtool_ts_info info;
2449 err = __ethtool_get_ts_info(dev, &info);
2453 if (copy_to_user(useraddr, &info, sizeof(info)))
2459 int ethtool_get_module_info_call(struct net_device *dev,
2460 struct ethtool_modinfo *modinfo)
2462 const struct ethtool_ops *ops = dev->ethtool_ops;
2463 struct phy_device *phydev = dev->phydev;
2466 return sfp_get_module_info(dev->sfp_bus, modinfo);
2468 if (phydev && phydev->drv && phydev->drv->module_info)
2469 return phydev->drv->module_info(phydev, modinfo);
2471 if (ops->get_module_info)
2472 return ops->get_module_info(dev, modinfo);
2477 static int ethtool_get_module_info(struct net_device *dev,
2478 void __user *useraddr)
2481 struct ethtool_modinfo modinfo;
2483 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2486 ret = ethtool_get_module_info_call(dev, &modinfo);
2490 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2496 int ethtool_get_module_eeprom_call(struct net_device *dev,
2497 struct ethtool_eeprom *ee, u8 *data)
2499 const struct ethtool_ops *ops = dev->ethtool_ops;
2500 struct phy_device *phydev = dev->phydev;
2503 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2505 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2506 return phydev->drv->module_eeprom(phydev, ee, data);
2508 if (ops->get_module_eeprom)
2509 return ops->get_module_eeprom(dev, ee, data);
2514 static int ethtool_get_module_eeprom(struct net_device *dev,
2515 void __user *useraddr)
2518 struct ethtool_modinfo modinfo;
2520 ret = ethtool_get_module_info_call(dev, &modinfo);
2524 return ethtool_get_any_eeprom(dev, useraddr,
2525 ethtool_get_module_eeprom_call,
2526 modinfo.eeprom_len);
2529 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2532 case ETHTOOL_RX_COPYBREAK:
2533 case ETHTOOL_TX_COPYBREAK:
2534 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2535 if (tuna->len != sizeof(u32) ||
2536 tuna->type_id != ETHTOOL_TUNABLE_U32)
2539 case ETHTOOL_PFC_PREVENTION_TOUT:
2540 if (tuna->len != sizeof(u16) ||
2541 tuna->type_id != ETHTOOL_TUNABLE_U16)
2551 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2554 struct ethtool_tunable tuna;
2555 const struct ethtool_ops *ops = dev->ethtool_ops;
2558 if (!ops->get_tunable)
2560 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2562 ret = ethtool_tunable_valid(&tuna);
2565 data = kzalloc(tuna.len, GFP_USER);
2568 ret = ops->get_tunable(dev, &tuna, data);
2571 useraddr += sizeof(tuna);
2573 if (copy_to_user(useraddr, data, tuna.len))
2582 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2585 struct ethtool_tunable tuna;
2586 const struct ethtool_ops *ops = dev->ethtool_ops;
2589 if (!ops->set_tunable)
2591 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2593 ret = ethtool_tunable_valid(&tuna);
2596 useraddr += sizeof(tuna);
2597 data = memdup_user(useraddr, tuna.len);
2599 return PTR_ERR(data);
2600 ret = ops->set_tunable(dev, &tuna, data);
2606 static noinline_for_stack int
2607 ethtool_get_per_queue_coalesce(struct net_device *dev,
2608 void __user *useraddr,
2609 struct ethtool_per_queue_op *per_queue_opt)
2613 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2615 if (!dev->ethtool_ops->get_per_queue_coalesce)
2618 useraddr += sizeof(*per_queue_opt);
2620 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2623 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2624 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2626 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2629 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2631 useraddr += sizeof(coalesce);
2637 static noinline_for_stack int
2638 ethtool_set_per_queue_coalesce(struct net_device *dev,
2639 void __user *useraddr,
2640 struct ethtool_per_queue_op *per_queue_opt)
2645 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2646 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2648 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2649 (!dev->ethtool_ops->get_per_queue_coalesce))
2652 useraddr += sizeof(*per_queue_opt);
2654 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2655 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2656 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2660 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2661 struct ethtool_coalesce coalesce;
2663 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2669 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2674 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2679 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2683 useraddr += sizeof(coalesce);
2689 for_each_set_bit(i, queue_mask, bit) {
2690 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2699 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2700 void __user *useraddr, u32 sub_cmd)
2702 struct ethtool_per_queue_op per_queue_opt;
2704 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2707 if (per_queue_opt.sub_command != sub_cmd)
2710 switch (per_queue_opt.sub_command) {
2711 case ETHTOOL_GCOALESCE:
2712 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2713 case ETHTOOL_SCOALESCE:
2714 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2720 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2723 case ETHTOOL_PHY_DOWNSHIFT:
2724 case ETHTOOL_PHY_FAST_LINK_DOWN:
2725 if (tuna->len != sizeof(u8) ||
2726 tuna->type_id != ETHTOOL_TUNABLE_U8)
2729 case ETHTOOL_PHY_EDPD:
2730 if (tuna->len != sizeof(u16) ||
2731 tuna->type_id != ETHTOOL_TUNABLE_U16)
2741 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2743 struct phy_device *phydev = dev->phydev;
2744 struct ethtool_tunable tuna;
2745 bool phy_drv_tunable;
2749 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2750 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2752 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2754 ret = ethtool_phy_tunable_valid(&tuna);
2757 data = kzalloc(tuna.len, GFP_USER);
2760 if (phy_drv_tunable) {
2761 mutex_lock(&phydev->lock);
2762 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2763 mutex_unlock(&phydev->lock);
2765 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2769 useraddr += sizeof(tuna);
2771 if (copy_to_user(useraddr, data, tuna.len))
2780 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2782 struct phy_device *phydev = dev->phydev;
2783 struct ethtool_tunable tuna;
2784 bool phy_drv_tunable;
2788 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2789 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2791 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2793 ret = ethtool_phy_tunable_valid(&tuna);
2796 useraddr += sizeof(tuna);
2797 data = memdup_user(useraddr, tuna.len);
2799 return PTR_ERR(data);
2800 if (phy_drv_tunable) {
2801 mutex_lock(&phydev->lock);
2802 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2803 mutex_unlock(&phydev->lock);
2805 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2812 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2814 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2817 if (!dev->ethtool_ops->get_fecparam)
2820 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2824 if (WARN_ON_ONCE(fecparam.reserved))
2825 fecparam.reserved = 0;
2827 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2832 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2834 struct ethtool_fecparam fecparam;
2836 if (!dev->ethtool_ops->set_fecparam)
2839 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2842 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2845 fecparam.active_fec = 0;
2846 fecparam.reserved = 0;
2848 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2851 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2854 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2855 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2857 struct net_device *dev;
2860 netdev_features_t old_features;
2862 dev = __dev_get_by_name(net, ifr->ifr_name);
2866 if (ethcmd == ETHTOOL_PERQUEUE) {
2867 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2872 /* Allow some commands to be done by anyone */
2875 case ETHTOOL_GDRVINFO:
2876 case ETHTOOL_GMSGLVL:
2878 case ETHTOOL_GCOALESCE:
2879 case ETHTOOL_GRINGPARAM:
2880 case ETHTOOL_GPAUSEPARAM:
2881 case ETHTOOL_GRXCSUM:
2882 case ETHTOOL_GTXCSUM:
2884 case ETHTOOL_GSSET_INFO:
2885 case ETHTOOL_GSTRINGS:
2886 case ETHTOOL_GSTATS:
2887 case ETHTOOL_GPHYSTATS:
2889 case ETHTOOL_GPERMADDR:
2893 case ETHTOOL_GFLAGS:
2894 case ETHTOOL_GPFLAGS:
2896 case ETHTOOL_GRXRINGS:
2897 case ETHTOOL_GRXCLSRLCNT:
2898 case ETHTOOL_GRXCLSRULE:
2899 case ETHTOOL_GRXCLSRLALL:
2900 case ETHTOOL_GRXFHINDIR:
2902 case ETHTOOL_GFEATURES:
2903 case ETHTOOL_GCHANNELS:
2904 case ETHTOOL_GET_TS_INFO:
2906 case ETHTOOL_GTUNABLE:
2907 case ETHTOOL_PHY_GTUNABLE:
2908 case ETHTOOL_GLINKSETTINGS:
2909 case ETHTOOL_GFECPARAM:
2912 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2916 if (dev->dev.parent)
2917 pm_runtime_get_sync(dev->dev.parent);
2919 if (!netif_device_present(dev)) {
2924 if (dev->ethtool_ops->begin) {
2925 rc = dev->ethtool_ops->begin(dev);
2929 old_features = dev->features;
2933 rc = ethtool_get_settings(dev, useraddr);
2936 rc = ethtool_set_settings(dev, useraddr);
2938 case ETHTOOL_GDRVINFO:
2939 rc = ethtool_get_drvinfo(dev, devlink_state);
2942 rc = ethtool_get_regs(dev, useraddr);
2945 rc = ethtool_get_wol(dev, useraddr);
2948 rc = ethtool_set_wol(dev, useraddr);
2950 case ETHTOOL_GMSGLVL:
2951 rc = ethtool_get_value(dev, useraddr, ethcmd,
2952 dev->ethtool_ops->get_msglevel);
2954 case ETHTOOL_SMSGLVL:
2955 rc = ethtool_set_value_void(dev, useraddr,
2956 dev->ethtool_ops->set_msglevel);
2958 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2961 rc = ethtool_get_eee(dev, useraddr);
2964 rc = ethtool_set_eee(dev, useraddr);
2966 case ETHTOOL_NWAY_RST:
2967 rc = ethtool_nway_reset(dev);
2970 rc = ethtool_get_link(dev, useraddr);
2972 case ETHTOOL_GEEPROM:
2973 rc = ethtool_get_eeprom(dev, useraddr);
2975 case ETHTOOL_SEEPROM:
2976 rc = ethtool_set_eeprom(dev, useraddr);
2978 case ETHTOOL_GCOALESCE:
2979 rc = ethtool_get_coalesce(dev, useraddr);
2981 case ETHTOOL_SCOALESCE:
2982 rc = ethtool_set_coalesce(dev, useraddr);
2984 case ETHTOOL_GRINGPARAM:
2985 rc = ethtool_get_ringparam(dev, useraddr);
2987 case ETHTOOL_SRINGPARAM:
2988 rc = ethtool_set_ringparam(dev, useraddr);
2990 case ETHTOOL_GPAUSEPARAM:
2991 rc = ethtool_get_pauseparam(dev, useraddr);
2993 case ETHTOOL_SPAUSEPARAM:
2994 rc = ethtool_set_pauseparam(dev, useraddr);
2997 rc = ethtool_self_test(dev, useraddr);
2999 case ETHTOOL_GSTRINGS:
3000 rc = ethtool_get_strings(dev, useraddr);
3002 case ETHTOOL_PHYS_ID:
3003 rc = ethtool_phys_id(dev, useraddr);
3005 case ETHTOOL_GSTATS:
3006 rc = ethtool_get_stats(dev, useraddr);
3008 case ETHTOOL_GPERMADDR:
3009 rc = ethtool_get_perm_addr(dev, useraddr);
3011 case ETHTOOL_GFLAGS:
3012 rc = ethtool_get_value(dev, useraddr, ethcmd,
3013 __ethtool_get_flags);
3015 case ETHTOOL_SFLAGS:
3016 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3018 case ETHTOOL_GPFLAGS:
3019 rc = ethtool_get_value(dev, useraddr, ethcmd,
3020 dev->ethtool_ops->get_priv_flags);
3022 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
3024 case ETHTOOL_SPFLAGS:
3025 rc = ethtool_set_value(dev, useraddr,
3026 dev->ethtool_ops->set_priv_flags);
3029 case ETHTOOL_GRXRINGS:
3030 case ETHTOOL_GRXCLSRLCNT:
3031 case ETHTOOL_GRXCLSRULE:
3032 case ETHTOOL_GRXCLSRLALL:
3033 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
3036 case ETHTOOL_SRXCLSRLDEL:
3037 case ETHTOOL_SRXCLSRLINS:
3038 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
3040 case ETHTOOL_FLASHDEV:
3041 rc = ethtool_flash_device(dev, devlink_state);
3044 rc = ethtool_reset(dev, useraddr);
3046 case ETHTOOL_GSSET_INFO:
3047 rc = ethtool_get_sset_info(dev, useraddr);
3049 case ETHTOOL_GRXFHINDIR:
3050 rc = ethtool_get_rxfh_indir(dev, useraddr);
3052 case ETHTOOL_SRXFHINDIR:
3053 rc = ethtool_set_rxfh_indir(dev, useraddr);
3056 rc = ethtool_get_rxfh(dev, useraddr);
3059 rc = ethtool_set_rxfh(dev, useraddr);
3061 case ETHTOOL_GFEATURES:
3062 rc = ethtool_get_features(dev, useraddr);
3064 case ETHTOOL_SFEATURES:
3065 rc = ethtool_set_features(dev, useraddr);
3067 case ETHTOOL_GTXCSUM:
3068 case ETHTOOL_GRXCSUM:
3073 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
3075 case ETHTOOL_STXCSUM:
3076 case ETHTOOL_SRXCSUM:
3081 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
3083 case ETHTOOL_GCHANNELS:
3084 rc = ethtool_get_channels(dev, useraddr);
3086 case ETHTOOL_SCHANNELS:
3087 rc = ethtool_set_channels(dev, useraddr);
3089 case ETHTOOL_SET_DUMP:
3090 rc = ethtool_set_dump(dev, useraddr);
3092 case ETHTOOL_GET_DUMP_FLAG:
3093 rc = ethtool_get_dump_flag(dev, useraddr);
3095 case ETHTOOL_GET_DUMP_DATA:
3096 rc = ethtool_get_dump_data(dev, useraddr);
3098 case ETHTOOL_GET_TS_INFO:
3099 rc = ethtool_get_ts_info(dev, useraddr);
3101 case ETHTOOL_GMODULEINFO:
3102 rc = ethtool_get_module_info(dev, useraddr);
3104 case ETHTOOL_GMODULEEEPROM:
3105 rc = ethtool_get_module_eeprom(dev, useraddr);
3107 case ETHTOOL_GTUNABLE:
3108 rc = ethtool_get_tunable(dev, useraddr);
3110 case ETHTOOL_STUNABLE:
3111 rc = ethtool_set_tunable(dev, useraddr);
3113 case ETHTOOL_GPHYSTATS:
3114 rc = ethtool_get_phy_stats(dev, useraddr);
3116 case ETHTOOL_PERQUEUE:
3117 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
3119 case ETHTOOL_GLINKSETTINGS:
3120 rc = ethtool_get_link_ksettings(dev, useraddr);
3122 case ETHTOOL_SLINKSETTINGS:
3123 rc = ethtool_set_link_ksettings(dev, useraddr);
3125 case ETHTOOL_PHY_GTUNABLE:
3126 rc = get_phy_tunable(dev, useraddr);
3128 case ETHTOOL_PHY_STUNABLE:
3129 rc = set_phy_tunable(dev, useraddr);
3131 case ETHTOOL_GFECPARAM:
3132 rc = ethtool_get_fecparam(dev, useraddr);
3134 case ETHTOOL_SFECPARAM:
3135 rc = ethtool_set_fecparam(dev, useraddr);
3141 if (dev->ethtool_ops->complete)
3142 dev->ethtool_ops->complete(dev);
3144 if (old_features != dev->features)
3145 netdev_features_change(dev);
3147 if (dev->dev.parent)
3148 pm_runtime_put(dev->dev.parent);
3153 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3155 struct ethtool_devlink_compat *state;
3159 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3162 state = kzalloc(sizeof(*state), GFP_KERNEL);
3167 case ETHTOOL_FLASHDEV:
3168 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3172 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3177 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3183 case ETHTOOL_FLASHDEV:
3185 rc = devlink_compat_flash_update(state->devlink,
3188 case ETHTOOL_GDRVINFO:
3190 devlink_compat_running_version(state->devlink,
3191 state->info.fw_version,
3192 sizeof(state->info.fw_version));
3193 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3202 devlink_put(state->devlink);
3207 struct ethtool_rx_flow_key {
3208 struct flow_dissector_key_basic basic;
3210 struct flow_dissector_key_ipv4_addrs ipv4;
3211 struct flow_dissector_key_ipv6_addrs ipv6;
3213 struct flow_dissector_key_ports tp;
3214 struct flow_dissector_key_ip ip;
3215 struct flow_dissector_key_vlan vlan;
3216 struct flow_dissector_key_eth_addrs eth_addrs;
3217 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3219 struct ethtool_rx_flow_match {
3220 struct flow_dissector dissector;
3221 struct ethtool_rx_flow_key key;
3222 struct ethtool_rx_flow_key mask;
3225 struct ethtool_rx_flow_rule *
3226 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3228 const struct ethtool_rx_flow_spec *fs = input->fs;
3229 struct ethtool_rx_flow_match *match;
3230 struct ethtool_rx_flow_rule *flow;
3231 struct flow_action_entry *act;
3233 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3234 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3236 return ERR_PTR(-ENOMEM);
3238 /* ethtool_rx supports only one single action per rule. */
3239 flow->rule = flow_rule_alloc(1);
3242 return ERR_PTR(-ENOMEM);
3245 match = (struct ethtool_rx_flow_match *)flow->priv;
3246 flow->rule->match.dissector = &match->dissector;
3247 flow->rule->match.mask = &match->mask;
3248 flow->rule->match.key = &match->key;
3250 match->mask.basic.n_proto = htons(0xffff);
3252 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3254 const struct ethhdr *ether_spec, *ether_m_spec;
3256 ether_spec = &fs->h_u.ether_spec;
3257 ether_m_spec = &fs->m_u.ether_spec;
3259 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3260 ether_addr_copy(match->key.eth_addrs.src,
3261 ether_spec->h_source);
3262 ether_addr_copy(match->mask.eth_addrs.src,
3263 ether_m_spec->h_source);
3265 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3266 ether_addr_copy(match->key.eth_addrs.dst,
3267 ether_spec->h_dest);
3268 ether_addr_copy(match->mask.eth_addrs.dst,
3269 ether_m_spec->h_dest);
3271 if (ether_m_spec->h_proto) {
3272 match->key.basic.n_proto = ether_spec->h_proto;
3273 match->mask.basic.n_proto = ether_m_spec->h_proto;
3279 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3281 match->key.basic.n_proto = htons(ETH_P_IP);
3283 v4_spec = &fs->h_u.tcp_ip4_spec;
3284 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3286 if (v4_m_spec->ip4src) {
3287 match->key.ipv4.src = v4_spec->ip4src;
3288 match->mask.ipv4.src = v4_m_spec->ip4src;
3290 if (v4_m_spec->ip4dst) {
3291 match->key.ipv4.dst = v4_spec->ip4dst;
3292 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3294 if (v4_m_spec->ip4src ||
3295 v4_m_spec->ip4dst) {
3296 match->dissector.used_keys |=
3297 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3298 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3299 offsetof(struct ethtool_rx_flow_key, ipv4);
3301 if (v4_m_spec->psrc) {
3302 match->key.tp.src = v4_spec->psrc;
3303 match->mask.tp.src = v4_m_spec->psrc;
3305 if (v4_m_spec->pdst) {
3306 match->key.tp.dst = v4_spec->pdst;
3307 match->mask.tp.dst = v4_m_spec->pdst;
3309 if (v4_m_spec->psrc ||
3311 match->dissector.used_keys |=
3312 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3313 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3314 offsetof(struct ethtool_rx_flow_key, tp);
3316 if (v4_m_spec->tos) {
3317 match->key.ip.tos = v4_spec->tos;
3318 match->mask.ip.tos = v4_m_spec->tos;
3319 match->dissector.used_keys |=
3320 BIT(FLOW_DISSECTOR_KEY_IP);
3321 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3322 offsetof(struct ethtool_rx_flow_key, ip);
3328 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3330 match->key.basic.n_proto = htons(ETH_P_IPV6);
3332 v6_spec = &fs->h_u.tcp_ip6_spec;
3333 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3334 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
3335 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3336 sizeof(match->key.ipv6.src));
3337 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3338 sizeof(match->mask.ipv6.src));
3340 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3341 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3342 sizeof(match->key.ipv6.dst));
3343 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3344 sizeof(match->mask.ipv6.dst));
3346 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3347 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3348 match->dissector.used_keys |=
3349 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3350 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3351 offsetof(struct ethtool_rx_flow_key, ipv6);
3353 if (v6_m_spec->psrc) {
3354 match->key.tp.src = v6_spec->psrc;
3355 match->mask.tp.src = v6_m_spec->psrc;
3357 if (v6_m_spec->pdst) {
3358 match->key.tp.dst = v6_spec->pdst;
3359 match->mask.tp.dst = v6_m_spec->pdst;
3361 if (v6_m_spec->psrc ||
3363 match->dissector.used_keys |=
3364 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3365 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3366 offsetof(struct ethtool_rx_flow_key, tp);
3368 if (v6_m_spec->tclass) {
3369 match->key.ip.tos = v6_spec->tclass;
3370 match->mask.ip.tos = v6_m_spec->tclass;
3371 match->dissector.used_keys |=
3372 BIT_ULL(FLOW_DISSECTOR_KEY_IP);
3373 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3374 offsetof(struct ethtool_rx_flow_key, ip);
3379 ethtool_rx_flow_rule_destroy(flow);
3380 return ERR_PTR(-EINVAL);
3383 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3386 match->key.basic.ip_proto = IPPROTO_TCP;
3387 match->mask.basic.ip_proto = 0xff;
3391 match->key.basic.ip_proto = IPPROTO_UDP;
3392 match->mask.basic.ip_proto = 0xff;
3396 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
3397 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3398 offsetof(struct ethtool_rx_flow_key, basic);
3400 if (fs->flow_type & FLOW_EXT) {
3401 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3402 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3404 if (ext_m_spec->vlan_etype) {
3405 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3406 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3409 if (ext_m_spec->vlan_tci) {
3410 match->key.vlan.vlan_id =
3411 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3412 match->mask.vlan.vlan_id =
3413 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3415 match->key.vlan.vlan_dei =
3416 !!(ext_h_spec->vlan_tci & htons(0x1000));
3417 match->mask.vlan.vlan_dei =
3418 !!(ext_m_spec->vlan_tci & htons(0x1000));
3420 match->key.vlan.vlan_priority =
3421 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3422 match->mask.vlan.vlan_priority =
3423 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3426 if (ext_m_spec->vlan_etype ||
3427 ext_m_spec->vlan_tci) {
3428 match->dissector.used_keys |=
3429 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
3430 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3431 offsetof(struct ethtool_rx_flow_key, vlan);
3434 if (fs->flow_type & FLOW_MAC_EXT) {
3435 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3436 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3438 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3440 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3443 match->dissector.used_keys |=
3444 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3445 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3446 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3449 act = &flow->rule->action.entries[0];
3450 switch (fs->ring_cookie) {
3451 case RX_CLS_FLOW_DISC:
3452 act->id = FLOW_ACTION_DROP;
3454 case RX_CLS_FLOW_WAKE:
3455 act->id = FLOW_ACTION_WAKE;
3458 act->id = FLOW_ACTION_QUEUE;
3459 if (fs->flow_type & FLOW_RSS)
3460 act->queue.ctx = input->rss_ctx;
3462 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3463 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3469 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3471 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3476 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);