1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/ethtool.c - Ethtool ioctl handler
6 * This file is where we call all the ethtool_ops commands to get
7 * the information ethtool needs.
10 #include <linux/compat.h>
11 #include <linux/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>
31 #include <net/xdp_sock_drv.h>
32 #include <net/flow_offload.h>
33 #include <linux/ethtool_netlink.h>
34 #include <generated/utsrelease.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 return netif_carrier_ok(dev) ? 1 : 0;
63 EXPORT_SYMBOL(ethtool_op_get_link);
65 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
67 info->so_timestamping =
68 SOF_TIMESTAMPING_TX_SOFTWARE |
69 SOF_TIMESTAMPING_RX_SOFTWARE |
70 SOF_TIMESTAMPING_SOFTWARE;
74 EXPORT_SYMBOL(ethtool_op_get_ts_info);
76 /* Handlers for each ethtool command */
78 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
80 struct ethtool_gfeatures cmd = {
81 .cmd = ETHTOOL_GFEATURES,
82 .size = ETHTOOL_DEV_FEATURE_WORDS,
84 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
89 /* in case feature bits run out again */
90 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
92 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
93 features[i].available = (u32)(dev->hw_features >> (32 * i));
94 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
95 features[i].active = (u32)(dev->features >> (32 * i));
96 features[i].never_changed =
97 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
100 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
101 if (get_user(copy_size, sizeaddr))
104 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
105 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
107 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
109 useraddr += sizeof(cmd);
110 if (copy_to_user(useraddr, features,
111 array_size(copy_size, sizeof(*features))))
117 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
119 struct ethtool_sfeatures cmd;
120 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
121 netdev_features_t wanted = 0, valid = 0;
124 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
126 useraddr += sizeof(cmd);
128 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
131 if (copy_from_user(features, useraddr, sizeof(features)))
134 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
135 valid |= (netdev_features_t)features[i].valid << (32 * i);
136 wanted |= (netdev_features_t)features[i].requested << (32 * i);
139 if (valid & ~NETIF_F_ETHTOOL_BITS)
142 if (valid & ~dev->hw_features) {
143 valid &= dev->hw_features;
144 ret |= ETHTOOL_F_UNSUPPORTED;
147 dev->wanted_features &= ~valid;
148 dev->wanted_features |= wanted & valid;
149 __netdev_update_features(dev);
151 if ((dev->wanted_features ^ dev->features) & valid)
152 ret |= ETHTOOL_F_WISH;
157 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
159 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
160 const struct ethtool_ops *ops = dev->ethtool_ops;
162 if (sset == ETH_SS_FEATURES)
163 return ARRAY_SIZE(netdev_features_strings);
165 if (sset == ETH_SS_RSS_HASH_FUNCS)
166 return ARRAY_SIZE(rss_hash_func_strings);
168 if (sset == ETH_SS_TUNABLES)
169 return ARRAY_SIZE(tunable_strings);
171 if (sset == ETH_SS_PHY_TUNABLES)
172 return ARRAY_SIZE(phy_tunable_strings);
174 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
175 !ops->get_ethtool_phy_stats &&
176 phy_ops && phy_ops->get_sset_count)
177 return phy_ops->get_sset_count(dev->phydev);
179 if (sset == ETH_SS_LINK_MODES)
180 return __ETHTOOL_LINK_MODE_MASK_NBITS;
182 if (ops->get_sset_count && ops->get_strings)
183 return ops->get_sset_count(dev, sset);
188 static void __ethtool_get_strings(struct net_device *dev,
189 u32 stringset, u8 *data)
191 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
192 const struct ethtool_ops *ops = dev->ethtool_ops;
194 if (stringset == ETH_SS_FEATURES)
195 memcpy(data, netdev_features_strings,
196 sizeof(netdev_features_strings));
197 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
198 memcpy(data, rss_hash_func_strings,
199 sizeof(rss_hash_func_strings));
200 else if (stringset == ETH_SS_TUNABLES)
201 memcpy(data, tunable_strings, sizeof(tunable_strings));
202 else if (stringset == ETH_SS_PHY_TUNABLES)
203 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
204 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
205 !ops->get_ethtool_phy_stats && phy_ops &&
206 phy_ops->get_strings)
207 phy_ops->get_strings(dev->phydev, data);
208 else if (stringset == ETH_SS_LINK_MODES)
209 memcpy(data, link_mode_names,
210 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
212 /* ops->get_strings is valid because checked earlier */
213 ops->get_strings(dev, stringset, data);
216 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
218 /* feature masks of legacy discrete ethtool ops */
221 case ETHTOOL_GTXCSUM:
222 case ETHTOOL_STXCSUM:
223 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
225 case ETHTOOL_GRXCSUM:
226 case ETHTOOL_SRXCSUM:
227 return NETIF_F_RXCSUM;
230 return NETIF_F_SG | NETIF_F_FRAGLIST;
233 return NETIF_F_ALL_TSO;
245 static int ethtool_get_one_feature(struct net_device *dev,
246 char __user *useraddr, u32 ethcmd)
248 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
249 struct ethtool_value edata = {
251 .data = !!(dev->features & mask),
254 if (copy_to_user(useraddr, &edata, sizeof(edata)))
259 static int ethtool_set_one_feature(struct net_device *dev,
260 void __user *useraddr, u32 ethcmd)
262 struct ethtool_value edata;
263 netdev_features_t mask;
265 if (copy_from_user(&edata, useraddr, sizeof(edata)))
268 mask = ethtool_get_feature_mask(ethcmd);
269 mask &= dev->hw_features;
274 dev->wanted_features |= mask;
276 dev->wanted_features &= ~mask;
278 __netdev_update_features(dev);
283 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
284 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
285 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
286 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
289 static u32 __ethtool_get_flags(struct net_device *dev)
293 if (dev->features & NETIF_F_LRO)
294 flags |= ETH_FLAG_LRO;
295 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
296 flags |= ETH_FLAG_RXVLAN;
297 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
298 flags |= ETH_FLAG_TXVLAN;
299 if (dev->features & NETIF_F_NTUPLE)
300 flags |= ETH_FLAG_NTUPLE;
301 if (dev->features & NETIF_F_RXHASH)
302 flags |= ETH_FLAG_RXHASH;
307 static int __ethtool_set_flags(struct net_device *dev, u32 data)
309 netdev_features_t features = 0, changed;
311 if (data & ~ETH_ALL_FLAGS)
314 if (data & ETH_FLAG_LRO)
315 features |= NETIF_F_LRO;
316 if (data & ETH_FLAG_RXVLAN)
317 features |= NETIF_F_HW_VLAN_CTAG_RX;
318 if (data & ETH_FLAG_TXVLAN)
319 features |= NETIF_F_HW_VLAN_CTAG_TX;
320 if (data & ETH_FLAG_NTUPLE)
321 features |= NETIF_F_NTUPLE;
322 if (data & ETH_FLAG_RXHASH)
323 features |= NETIF_F_RXHASH;
325 /* allow changing only bits set in hw_features */
326 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
327 if (changed & ~dev->hw_features)
328 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
330 dev->wanted_features =
331 (dev->wanted_features & ~changed) | (features & changed);
333 __netdev_update_features(dev);
338 /* Given two link masks, AND them together and save the result in dst. */
339 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
340 struct ethtool_link_ksettings *src)
342 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
343 unsigned int idx = 0;
345 for (; idx < size; idx++) {
346 dst->link_modes.supported[idx] &=
347 src->link_modes.supported[idx];
348 dst->link_modes.advertising[idx] &=
349 src->link_modes.advertising[idx];
352 EXPORT_SYMBOL(ethtool_intersect_link_masks);
354 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
360 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
362 /* return false if src had higher bits set. lower bits always updated. */
363 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
364 const unsigned long *src)
366 *legacy_u32 = src[0];
367 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
368 __ETHTOOL_LINK_MODE_MASK_NBITS;
370 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
372 /* return false if ksettings link modes had higher bits
373 * set. legacy_settings always updated (best effort)
376 convert_link_ksettings_to_legacy_settings(
377 struct ethtool_cmd *legacy_settings,
378 const struct ethtool_link_ksettings *link_ksettings)
382 memset(legacy_settings, 0, sizeof(*legacy_settings));
383 /* this also clears the deprecated fields in legacy structure:
389 retval &= ethtool_convert_link_mode_to_legacy_u32(
390 &legacy_settings->supported,
391 link_ksettings->link_modes.supported);
392 retval &= ethtool_convert_link_mode_to_legacy_u32(
393 &legacy_settings->advertising,
394 link_ksettings->link_modes.advertising);
395 retval &= ethtool_convert_link_mode_to_legacy_u32(
396 &legacy_settings->lp_advertising,
397 link_ksettings->link_modes.lp_advertising);
398 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
399 legacy_settings->duplex
400 = link_ksettings->base.duplex;
401 legacy_settings->port
402 = link_ksettings->base.port;
403 legacy_settings->phy_address
404 = link_ksettings->base.phy_address;
405 legacy_settings->autoneg
406 = link_ksettings->base.autoneg;
407 legacy_settings->mdio_support
408 = link_ksettings->base.mdio_support;
409 legacy_settings->eth_tp_mdix
410 = link_ksettings->base.eth_tp_mdix;
411 legacy_settings->eth_tp_mdix_ctrl
412 = link_ksettings->base.eth_tp_mdix_ctrl;
413 legacy_settings->transceiver
414 = link_ksettings->base.transceiver;
418 /* number of 32-bit words to store the user's link mode bitmaps */
419 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
420 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
422 /* layout of the struct passed from/to userland */
423 struct ethtool_link_usettings {
424 struct ethtool_link_settings base;
426 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
427 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
428 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
432 /* Internal kernel helper to query a device ethtool_link_settings. */
433 int __ethtool_get_link_ksettings(struct net_device *dev,
434 struct ethtool_link_ksettings *link_ksettings)
438 if (!dev->ethtool_ops->get_link_ksettings)
441 memset(link_ksettings, 0, sizeof(*link_ksettings));
442 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
444 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
446 /* convert ethtool_link_usettings in user space to a kernel internal
447 * ethtool_link_ksettings. return 0 on success, errno on error.
449 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
450 const void __user *from)
452 struct ethtool_link_usettings link_usettings;
454 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
457 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
458 bitmap_from_arr32(to->link_modes.supported,
459 link_usettings.link_modes.supported,
460 __ETHTOOL_LINK_MODE_MASK_NBITS);
461 bitmap_from_arr32(to->link_modes.advertising,
462 link_usettings.link_modes.advertising,
463 __ETHTOOL_LINK_MODE_MASK_NBITS);
464 bitmap_from_arr32(to->link_modes.lp_advertising,
465 link_usettings.link_modes.lp_advertising,
466 __ETHTOOL_LINK_MODE_MASK_NBITS);
471 /* Check if the user is trying to change anything besides speed/duplex */
472 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
474 struct ethtool_link_settings base2 = {};
476 base2.speed = cmd->base.speed;
477 base2.port = PORT_OTHER;
478 base2.duplex = cmd->base.duplex;
479 base2.cmd = cmd->base.cmd;
480 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
482 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
483 bitmap_empty(cmd->link_modes.supported,
484 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
485 bitmap_empty(cmd->link_modes.lp_advertising,
486 __ETHTOOL_LINK_MODE_MASK_NBITS);
489 /* convert a kernel internal ethtool_link_ksettings to
490 * ethtool_link_usettings in user space. return 0 on success, errno on
494 store_link_ksettings_for_user(void __user *to,
495 const struct ethtool_link_ksettings *from)
497 struct ethtool_link_usettings link_usettings;
499 memcpy(&link_usettings, from, sizeof(link_usettings));
500 bitmap_to_arr32(link_usettings.link_modes.supported,
501 from->link_modes.supported,
502 __ETHTOOL_LINK_MODE_MASK_NBITS);
503 bitmap_to_arr32(link_usettings.link_modes.advertising,
504 from->link_modes.advertising,
505 __ETHTOOL_LINK_MODE_MASK_NBITS);
506 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
507 from->link_modes.lp_advertising,
508 __ETHTOOL_LINK_MODE_MASK_NBITS);
510 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
516 /* Query device for its ethtool_link_settings. */
517 static int ethtool_get_link_ksettings(struct net_device *dev,
518 void __user *useraddr)
521 struct ethtool_link_ksettings link_ksettings;
524 if (!dev->ethtool_ops->get_link_ksettings)
527 /* handle bitmap nbits handshake */
528 if (copy_from_user(&link_ksettings.base, useraddr,
529 sizeof(link_ksettings.base)))
532 if (__ETHTOOL_LINK_MODE_MASK_NU32
533 != link_ksettings.base.link_mode_masks_nwords) {
534 /* wrong link mode nbits requested */
535 memset(&link_ksettings, 0, sizeof(link_ksettings));
536 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
537 /* send back number of words required as negative val */
538 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
539 "need too many bits for link modes!");
540 link_ksettings.base.link_mode_masks_nwords
541 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
543 /* copy the base fields back to user, not the link
546 if (copy_to_user(useraddr, &link_ksettings.base,
547 sizeof(link_ksettings.base)))
553 /* handshake successful: user/kernel agree on
554 * link_mode_masks_nwords
557 memset(&link_ksettings, 0, sizeof(link_ksettings));
558 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
562 /* make sure we tell the right values to user */
563 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
564 link_ksettings.base.link_mode_masks_nwords
565 = __ETHTOOL_LINK_MODE_MASK_NU32;
566 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
567 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
568 link_ksettings.base.rate_matching = RATE_MATCH_NONE;
570 return store_link_ksettings_for_user(useraddr, &link_ksettings);
573 /* Update device ethtool_link_settings. */
574 static int ethtool_set_link_ksettings(struct net_device *dev,
575 void __user *useraddr)
577 struct ethtool_link_ksettings link_ksettings = {};
582 if (!dev->ethtool_ops->set_link_ksettings)
585 /* make sure nbits field has expected value */
586 if (copy_from_user(&link_ksettings.base, useraddr,
587 sizeof(link_ksettings.base)))
590 if (__ETHTOOL_LINK_MODE_MASK_NU32
591 != link_ksettings.base.link_mode_masks_nwords)
594 /* copy the whole structure, now that we know it has expected
597 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
601 /* re-check nwords field, just in case */
602 if (__ETHTOOL_LINK_MODE_MASK_NU32
603 != link_ksettings.base.link_mode_masks_nwords)
606 if (link_ksettings.base.master_slave_cfg ||
607 link_ksettings.base.master_slave_state)
610 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
612 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
613 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
618 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
619 const struct ethtool_link_ksettings *cmd,
620 u32 *dev_speed, u8 *dev_duplex)
625 speed = cmd->base.speed;
626 duplex = cmd->base.duplex;
627 /* don't allow custom speed and duplex */
628 if (!ethtool_validate_speed(speed) ||
629 !ethtool_validate_duplex(duplex) ||
630 !ethtool_virtdev_validate_cmd(cmd))
633 *dev_duplex = duplex;
637 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
639 /* Query device for its ethtool_cmd settings.
641 * Backward compatibility note: for compatibility with legacy ethtool, this is
642 * now implemented via get_link_ksettings. When driver reports higher link mode
643 * bits, a kernel warning is logged once (with name of 1st driver/device) to
644 * recommend user to upgrade ethtool, but the command is successful (only the
645 * lower link mode bits reported back to user). Deprecated fields from
646 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
648 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
650 struct ethtool_link_ksettings link_ksettings;
651 struct ethtool_cmd cmd;
655 if (!dev->ethtool_ops->get_link_ksettings)
658 memset(&link_ksettings, 0, sizeof(link_ksettings));
659 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
662 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
664 /* send a sensible cmd tag back to user */
665 cmd.cmd = ETHTOOL_GSET;
667 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
673 /* Update device link settings with given ethtool_cmd.
675 * Backward compatibility note: for compatibility with legacy ethtool, this is
676 * now always implemented via set_link_settings. When user's request updates
677 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
678 * warning is logged once (with name of 1st driver/device) to recommend user to
679 * upgrade ethtool, and the request is rejected.
681 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
683 struct ethtool_link_ksettings link_ksettings;
684 struct ethtool_cmd cmd;
689 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
691 if (!dev->ethtool_ops->set_link_ksettings)
694 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
696 link_ksettings.base.link_mode_masks_nwords =
697 __ETHTOOL_LINK_MODE_MASK_NU32;
698 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
700 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
701 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
707 ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
709 const struct ethtool_ops *ops = dev->ethtool_ops;
710 struct device *parent = dev->dev.parent;
712 rsp->info.cmd = ETHTOOL_GDRVINFO;
713 strscpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
714 if (ops->get_drvinfo) {
715 ops->get_drvinfo(dev, &rsp->info);
716 if (!rsp->info.bus_info[0] && parent)
717 strscpy(rsp->info.bus_info, dev_name(parent),
718 sizeof(rsp->info.bus_info));
719 if (!rsp->info.driver[0] && parent && parent->driver)
720 strscpy(rsp->info.driver, parent->driver->name,
721 sizeof(rsp->info.driver));
722 } else if (parent && parent->driver) {
723 strscpy(rsp->info.bus_info, dev_name(parent),
724 sizeof(rsp->info.bus_info));
725 strscpy(rsp->info.driver, parent->driver->name,
726 sizeof(rsp->info.driver));
727 } else if (dev->rtnl_link_ops) {
728 strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
729 sizeof(rsp->info.driver));
735 * this method of obtaining string set info is deprecated;
736 * Use ETHTOOL_GSSET_INFO instead.
738 if (ops->get_sset_count) {
741 rc = ops->get_sset_count(dev, ETH_SS_TEST);
743 rsp->info.testinfo_len = rc;
744 rc = ops->get_sset_count(dev, ETH_SS_STATS);
746 rsp->info.n_stats = rc;
747 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
749 rsp->info.n_priv_flags = rc;
751 if (ops->get_regs_len) {
752 int ret = ops->get_regs_len(dev);
755 rsp->info.regdump_len = ret;
758 if (ops->get_eeprom_len)
759 rsp->info.eedump_len = ops->get_eeprom_len(dev);
761 if (!rsp->info.fw_version[0])
762 rsp->devlink = netdev_to_devlink_get(dev);
767 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
768 void __user *useraddr)
770 struct ethtool_sset_info info;
772 int i, idx = 0, n_bits = 0, ret, rc;
773 u32 *info_buf = NULL;
775 if (copy_from_user(&info, useraddr, sizeof(info)))
778 /* store copy of mask, because we zero struct later on */
779 sset_mask = info.sset_mask;
783 /* calculate size of return buffer */
784 n_bits = hweight64(sset_mask);
786 memset(&info, 0, sizeof(info));
787 info.cmd = ETHTOOL_GSSET_INFO;
789 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
794 * fill return buffer based on input bitmask and successful
795 * get_sset_count return
797 for (i = 0; i < 64; i++) {
798 if (!(sset_mask & (1ULL << i)))
801 rc = __ethtool_get_sset_count(dev, i);
803 info.sset_mask |= (1ULL << i);
804 info_buf[idx++] = rc;
809 if (copy_to_user(useraddr, &info, sizeof(info)))
812 useraddr += offsetof(struct ethtool_sset_info, data);
813 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
823 static noinline_for_stack int
824 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
825 const struct compat_ethtool_rxnfc __user *useraddr,
828 struct compat_ethtool_rxnfc crxnfc = {};
830 /* We expect there to be holes between fs.m_ext and
831 * fs.ring_cookie and at the end of fs, but nowhere else.
832 * On non-x86, no conversion should be needed.
834 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
835 sizeof(struct compat_ethtool_rxnfc) !=
836 sizeof(struct ethtool_rxnfc));
837 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
838 sizeof(useraddr->fs.m_ext) !=
839 offsetof(struct ethtool_rxnfc, fs.m_ext) +
840 sizeof(rxnfc->fs.m_ext));
841 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
842 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
843 offsetof(struct ethtool_rxnfc, fs.location) -
844 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
846 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
849 *rxnfc = (struct ethtool_rxnfc) {
851 .flow_type = crxnfc.flow_type,
854 .flow_type = crxnfc.fs.flow_type,
855 .h_u = crxnfc.fs.h_u,
856 .h_ext = crxnfc.fs.h_ext,
857 .m_u = crxnfc.fs.m_u,
858 .m_ext = crxnfc.fs.m_ext,
859 .ring_cookie = crxnfc.fs.ring_cookie,
860 .location = crxnfc.fs.location,
862 .rule_cnt = crxnfc.rule_cnt,
868 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
869 const void __user *useraddr,
872 if (compat_need_64bit_alignment_fixup())
873 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
875 if (copy_from_user(rxnfc, useraddr, size))
881 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
882 const struct ethtool_rxnfc *rxnfc,
883 size_t size, const u32 *rule_buf)
885 struct compat_ethtool_rxnfc crxnfc;
887 memset(&crxnfc, 0, sizeof(crxnfc));
888 crxnfc = (struct compat_ethtool_rxnfc) {
890 .flow_type = rxnfc->flow_type,
893 .flow_type = rxnfc->fs.flow_type,
894 .h_u = rxnfc->fs.h_u,
895 .h_ext = rxnfc->fs.h_ext,
896 .m_u = rxnfc->fs.m_u,
897 .m_ext = rxnfc->fs.m_ext,
898 .ring_cookie = rxnfc->fs.ring_cookie,
899 .location = rxnfc->fs.location,
901 .rule_cnt = rxnfc->rule_cnt,
904 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
910 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
911 const struct ethtool_rxnfc *rxnfc,
912 size_t size, const u32 *rule_buf)
916 if (compat_need_64bit_alignment_fixup()) {
917 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
919 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
921 ret = copy_to_user(useraddr, rxnfc, size);
922 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
929 if (copy_to_user(useraddr, rule_buf,
930 rxnfc->rule_cnt * sizeof(u32)))
937 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
938 u32 cmd, void __user *useraddr)
940 struct ethtool_rxnfc info;
941 size_t info_size = sizeof(info);
944 if (!dev->ethtool_ops->set_rxnfc)
947 /* struct ethtool_rxnfc was originally defined for
948 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
949 * members. User-space might still be using that
951 if (cmd == ETHTOOL_SRXFH)
952 info_size = (offsetof(struct ethtool_rxnfc, data) +
955 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
958 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
962 if (cmd == ETHTOOL_SRXCLSRLINS &&
963 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
969 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
970 u32 cmd, void __user *useraddr)
972 struct ethtool_rxnfc info;
973 size_t info_size = sizeof(info);
974 const struct ethtool_ops *ops = dev->ethtool_ops;
976 void *rule_buf = NULL;
981 /* struct ethtool_rxnfc was originally defined for
982 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
983 * members. User-space might still be using that
985 if (cmd == ETHTOOL_GRXFH)
986 info_size = (offsetof(struct ethtool_rxnfc, data) +
989 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
992 /* If FLOW_RSS was requested then user-space must be using the
993 * new definition, as FLOW_RSS is newer.
995 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
996 info_size = sizeof(info);
997 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
999 /* Since malicious users may modify the original data,
1000 * we need to check whether FLOW_RSS is still requested.
1002 if (!(info.flow_type & FLOW_RSS))
1006 if (info.cmd != cmd)
1009 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1010 if (info.rule_cnt > 0) {
1011 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1012 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1019 ret = ops->get_rxnfc(dev, &info, rule_buf);
1023 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1030 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1031 struct ethtool_rxnfc *rx_rings,
1036 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1039 /* Validate ring indices */
1040 for (i = 0; i < size; i++)
1041 if (indir[i] >= rx_rings->data)
1047 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1049 void netdev_rss_key_fill(void *buffer, size_t len)
1051 BUG_ON(len > sizeof(netdev_rss_key));
1052 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1053 memcpy(buffer, netdev_rss_key, len);
1055 EXPORT_SYMBOL(netdev_rss_key_fill);
1057 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1058 void __user *useraddr)
1060 u32 user_size, dev_size;
1064 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1065 !dev->ethtool_ops->get_rxfh)
1067 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1071 if (copy_from_user(&user_size,
1072 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1076 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1077 &dev_size, sizeof(dev_size)))
1080 /* If the user buffer size is 0, this is just a query for the
1081 * device table size. Otherwise, if it's smaller than the
1082 * device table size it's an error.
1084 if (user_size < dev_size)
1085 return user_size == 0 ? 0 : -EINVAL;
1087 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1091 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1095 if (copy_to_user(useraddr +
1096 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1097 indir, dev_size * sizeof(indir[0])))
1105 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1106 void __user *useraddr)
1108 struct ethtool_rxnfc rx_rings;
1109 u32 user_size, dev_size, i;
1111 const struct ethtool_ops *ops = dev->ethtool_ops;
1113 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1115 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1119 dev_size = ops->get_rxfh_indir_size(dev);
1123 if (copy_from_user(&user_size,
1124 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1128 if (user_size != 0 && user_size != dev_size)
1131 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1135 rx_rings.cmd = ETHTOOL_GRXRINGS;
1136 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1140 if (user_size == 0) {
1141 for (i = 0; i < dev_size; i++)
1142 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1144 ret = ethtool_copy_validate_indir(indir,
1145 useraddr + ringidx_offset,
1152 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1156 /* indicate whether rxfh was set to default */
1158 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1160 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1167 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1168 void __user *useraddr)
1171 const struct ethtool_ops *ops = dev->ethtool_ops;
1172 u32 user_indir_size, user_key_size;
1173 u32 dev_indir_size = 0, dev_key_size = 0;
1174 struct ethtool_rxfh rxfh;
1185 if (ops->get_rxfh_indir_size)
1186 dev_indir_size = ops->get_rxfh_indir_size(dev);
1187 if (ops->get_rxfh_key_size)
1188 dev_key_size = ops->get_rxfh_key_size(dev);
1190 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1192 user_indir_size = rxfh.indir_size;
1193 user_key_size = rxfh.key_size;
1195 /* Check that reserved fields are 0 for now */
1196 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1198 /* Most drivers don't handle rss_context, check it's 0 as well */
1199 if (rxfh.rss_context && !ops->get_rxfh_context)
1202 rxfh.indir_size = dev_indir_size;
1203 rxfh.key_size = dev_key_size;
1204 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1207 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1208 (user_key_size && (user_key_size != dev_key_size)))
1211 indir_bytes = user_indir_size * sizeof(indir[0]);
1212 total_size = indir_bytes + user_key_size;
1213 rss_config = kzalloc(total_size, GFP_USER);
1217 if (user_indir_size)
1218 indir = (u32 *)rss_config;
1221 hkey = rss_config + indir_bytes;
1223 if (rxfh.rss_context)
1224 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1228 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1232 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1233 &dev_hfunc, sizeof(rxfh.hfunc))) {
1235 } else if (copy_to_user(useraddr +
1236 offsetof(struct ethtool_rxfh, rss_config[0]),
1237 rss_config, total_size)) {
1246 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1247 void __user *useraddr)
1250 const struct ethtool_ops *ops = dev->ethtool_ops;
1251 struct ethtool_rxnfc rx_rings;
1252 struct ethtool_rxfh rxfh;
1253 u32 dev_indir_size = 0, dev_key_size = 0, i;
1254 u32 *indir = NULL, indir_bytes = 0;
1257 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1258 bool delete = false;
1260 if (!ops->get_rxnfc || !ops->set_rxfh)
1263 if (ops->get_rxfh_indir_size)
1264 dev_indir_size = ops->get_rxfh_indir_size(dev);
1265 if (ops->get_rxfh_key_size)
1266 dev_key_size = ops->get_rxfh_key_size(dev);
1268 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1271 /* Check that reserved fields are 0 for now */
1272 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1274 /* Most drivers don't handle rss_context, check it's 0 as well */
1275 if (rxfh.rss_context && !ops->set_rxfh_context)
1278 /* If either indir, hash key or function is valid, proceed further.
1279 * Must request at least one change: indir size, hash key or function.
1281 if ((rxfh.indir_size &&
1282 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1283 rxfh.indir_size != dev_indir_size) ||
1284 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1285 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1286 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1289 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1290 indir_bytes = dev_indir_size * sizeof(indir[0]);
1292 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1296 rx_rings.cmd = ETHTOOL_GRXRINGS;
1297 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1301 /* rxfh.indir_size == 0 means reset the indir table to default (master
1302 * context) or delete the context (other RSS contexts).
1303 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1305 if (rxfh.indir_size &&
1306 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1307 indir = (u32 *)rss_config;
1308 ret = ethtool_copy_validate_indir(indir,
1309 useraddr + rss_cfg_offset,
1314 } else if (rxfh.indir_size == 0) {
1315 if (rxfh.rss_context == 0) {
1316 indir = (u32 *)rss_config;
1317 for (i = 0; i < dev_indir_size; i++)
1318 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1324 if (rxfh.key_size) {
1325 hkey = rss_config + indir_bytes;
1326 if (copy_from_user(hkey,
1327 useraddr + rss_cfg_offset + indir_bytes,
1334 if (rxfh.rss_context)
1335 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1336 &rxfh.rss_context, delete);
1338 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1342 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1343 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1346 if (!rxfh.rss_context) {
1347 /* indicate whether rxfh was set to default */
1348 if (rxfh.indir_size == 0)
1349 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1350 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1351 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1359 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1361 struct ethtool_regs regs;
1362 const struct ethtool_ops *ops = dev->ethtool_ops;
1366 if (!ops->get_regs || !ops->get_regs_len)
1369 if (copy_from_user(®s, useraddr, sizeof(regs)))
1372 reglen = ops->get_regs_len(dev);
1376 if (regs.len > reglen)
1379 regbuf = vzalloc(reglen);
1383 if (regs.len < reglen)
1386 ops->get_regs(dev, ®s, regbuf);
1389 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1391 useraddr += offsetof(struct ethtool_regs, data);
1392 if (copy_to_user(useraddr, regbuf, reglen))
1401 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1403 struct ethtool_value reset;
1406 if (!dev->ethtool_ops->reset)
1409 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1412 ret = dev->ethtool_ops->reset(dev, &reset.data);
1416 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1421 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1423 struct ethtool_wolinfo wol;
1425 if (!dev->ethtool_ops->get_wol)
1428 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1429 wol.cmd = ETHTOOL_GWOL;
1430 dev->ethtool_ops->get_wol(dev, &wol);
1432 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1437 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1439 struct ethtool_wolinfo wol;
1442 if (!dev->ethtool_ops->set_wol)
1445 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1448 ret = dev->ethtool_ops->set_wol(dev, &wol);
1452 dev->wol_enabled = !!wol.wolopts;
1453 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1458 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1460 struct ethtool_eee edata;
1463 if (!dev->ethtool_ops->get_eee)
1466 memset(&edata, 0, sizeof(struct ethtool_eee));
1467 edata.cmd = ETHTOOL_GEEE;
1468 rc = dev->ethtool_ops->get_eee(dev, &edata);
1473 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1479 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1481 struct ethtool_eee edata;
1484 if (!dev->ethtool_ops->set_eee)
1487 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1490 ret = dev->ethtool_ops->set_eee(dev, &edata);
1492 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1496 static int ethtool_nway_reset(struct net_device *dev)
1498 if (!dev->ethtool_ops->nway_reset)
1501 return dev->ethtool_ops->nway_reset(dev);
1504 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1506 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1507 int link = __ethtool_get_link(dev);
1513 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1518 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1519 int (*getter)(struct net_device *,
1520 struct ethtool_eeprom *, u8 *),
1523 struct ethtool_eeprom eeprom;
1524 void __user *userbuf = useraddr + sizeof(eeprom);
1525 u32 bytes_remaining;
1529 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1532 /* Check for wrap and zero */
1533 if (eeprom.offset + eeprom.len <= eeprom.offset)
1536 /* Check for exceeding total eeprom len */
1537 if (eeprom.offset + eeprom.len > total_len)
1540 data = kzalloc(PAGE_SIZE, GFP_USER);
1544 bytes_remaining = eeprom.len;
1545 while (bytes_remaining > 0) {
1546 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1548 ret = getter(dev, &eeprom, data);
1555 if (copy_to_user(userbuf, data, eeprom.len)) {
1559 userbuf += eeprom.len;
1560 eeprom.offset += eeprom.len;
1561 bytes_remaining -= eeprom.len;
1564 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1565 eeprom.offset -= eeprom.len;
1566 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1573 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1575 const struct ethtool_ops *ops = dev->ethtool_ops;
1577 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1578 !ops->get_eeprom_len(dev))
1581 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1582 ops->get_eeprom_len(dev));
1585 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1587 struct ethtool_eeprom eeprom;
1588 const struct ethtool_ops *ops = dev->ethtool_ops;
1589 void __user *userbuf = useraddr + sizeof(eeprom);
1590 u32 bytes_remaining;
1594 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1595 !ops->get_eeprom_len(dev))
1598 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1601 /* Check for wrap and zero */
1602 if (eeprom.offset + eeprom.len <= eeprom.offset)
1605 /* Check for exceeding total eeprom len */
1606 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1609 data = kzalloc(PAGE_SIZE, GFP_USER);
1613 bytes_remaining = eeprom.len;
1614 while (bytes_remaining > 0) {
1615 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1617 if (copy_from_user(data, userbuf, eeprom.len)) {
1621 ret = ops->set_eeprom(dev, &eeprom, data);
1624 userbuf += eeprom.len;
1625 eeprom.offset += eeprom.len;
1626 bytes_remaining -= eeprom.len;
1633 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1634 void __user *useraddr)
1636 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1637 struct kernel_ethtool_coalesce kernel_coalesce = {};
1640 if (!dev->ethtool_ops->get_coalesce)
1643 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1648 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1654 ethtool_set_coalesce_supported(struct net_device *dev,
1655 struct ethtool_coalesce *coalesce)
1657 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1658 u32 nonzero_params = 0;
1660 if (coalesce->rx_coalesce_usecs)
1661 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1662 if (coalesce->rx_max_coalesced_frames)
1663 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1664 if (coalesce->rx_coalesce_usecs_irq)
1665 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1666 if (coalesce->rx_max_coalesced_frames_irq)
1667 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1668 if (coalesce->tx_coalesce_usecs)
1669 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1670 if (coalesce->tx_max_coalesced_frames)
1671 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1672 if (coalesce->tx_coalesce_usecs_irq)
1673 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1674 if (coalesce->tx_max_coalesced_frames_irq)
1675 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1676 if (coalesce->stats_block_coalesce_usecs)
1677 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1678 if (coalesce->use_adaptive_rx_coalesce)
1679 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1680 if (coalesce->use_adaptive_tx_coalesce)
1681 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1682 if (coalesce->pkt_rate_low)
1683 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1684 if (coalesce->rx_coalesce_usecs_low)
1685 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1686 if (coalesce->rx_max_coalesced_frames_low)
1687 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1688 if (coalesce->tx_coalesce_usecs_low)
1689 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1690 if (coalesce->tx_max_coalesced_frames_low)
1691 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1692 if (coalesce->pkt_rate_high)
1693 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1694 if (coalesce->rx_coalesce_usecs_high)
1695 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1696 if (coalesce->rx_max_coalesced_frames_high)
1697 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1698 if (coalesce->tx_coalesce_usecs_high)
1699 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1700 if (coalesce->tx_max_coalesced_frames_high)
1701 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1702 if (coalesce->rate_sample_interval)
1703 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1705 return (supported_params & nonzero_params) == nonzero_params;
1708 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1709 void __user *useraddr)
1711 struct kernel_ethtool_coalesce kernel_coalesce = {};
1712 struct ethtool_coalesce coalesce;
1715 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1718 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1723 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1726 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1729 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1732 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1736 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1738 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1739 struct kernel_ethtool_ringparam kernel_ringparam = {};
1741 if (!dev->ethtool_ops->get_ringparam)
1744 dev->ethtool_ops->get_ringparam(dev, &ringparam,
1745 &kernel_ringparam, NULL);
1747 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1752 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1754 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1755 struct kernel_ethtool_ringparam kernel_ringparam;
1758 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1761 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1764 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
1766 /* ensure new ring parameters are within the maximums */
1767 if (ringparam.rx_pending > max.rx_max_pending ||
1768 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1769 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1770 ringparam.tx_pending > max.tx_max_pending)
1773 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1774 &kernel_ringparam, NULL);
1776 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1780 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1781 void __user *useraddr)
1783 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1785 if (!dev->ethtool_ops->get_channels)
1788 dev->ethtool_ops->get_channels(dev, &channels);
1790 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1795 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1796 void __user *useraddr)
1798 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1799 u16 from_channel, to_channel;
1800 u64 max_rxnfc_in_use;
1801 u32 max_rxfh_in_use;
1805 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1808 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1811 dev->ethtool_ops->get_channels(dev, &curr);
1813 if (channels.rx_count == curr.rx_count &&
1814 channels.tx_count == curr.tx_count &&
1815 channels.combined_count == curr.combined_count &&
1816 channels.other_count == curr.other_count)
1819 /* ensure new counts are within the maximums */
1820 if (channels.rx_count > curr.max_rx ||
1821 channels.tx_count > curr.max_tx ||
1822 channels.combined_count > curr.max_combined ||
1823 channels.other_count > curr.max_other)
1826 /* ensure there is at least one RX and one TX channel */
1827 if (!channels.combined_count &&
1828 (!channels.rx_count || !channels.tx_count))
1831 /* ensure the new Rx count fits within the configured Rx flow
1832 * indirection table/rxnfc settings */
1833 if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
1834 max_rxnfc_in_use = 0;
1835 if (!netif_is_rxfh_configured(dev) ||
1836 ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use))
1837 max_rxfh_in_use = 0;
1838 if (channels.combined_count + channels.rx_count <=
1839 max_t(u64, max_rxnfc_in_use, max_rxfh_in_use))
1842 /* Disabling channels, query zero-copy AF_XDP sockets */
1843 from_channel = channels.combined_count +
1844 min(channels.rx_count, channels.tx_count);
1845 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1846 for (i = from_channel; i < to_channel; i++)
1847 if (xsk_get_pool_from_qid(dev, i))
1850 ret = dev->ethtool_ops->set_channels(dev, &channels);
1852 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1856 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1858 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1860 if (!dev->ethtool_ops->get_pauseparam)
1863 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1865 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1870 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1872 struct ethtool_pauseparam pauseparam;
1875 if (!dev->ethtool_ops->set_pauseparam)
1878 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1881 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1883 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1887 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1889 struct ethtool_test test;
1890 const struct ethtool_ops *ops = dev->ethtool_ops;
1894 if (!ops->self_test || !ops->get_sset_count)
1897 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1900 WARN_ON(test_len == 0);
1902 if (copy_from_user(&test, useraddr, sizeof(test)))
1905 test.len = test_len;
1906 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1910 netif_testing_on(dev);
1911 ops->self_test(dev, &test, data);
1912 netif_testing_off(dev);
1915 if (copy_to_user(useraddr, &test, sizeof(test)))
1917 useraddr += sizeof(test);
1918 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
1927 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1929 struct ethtool_gstrings gstrings;
1933 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1936 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1939 if (ret > S32_MAX / ETH_GSTRING_LEN)
1946 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1950 __ethtool_get_strings(dev, gstrings.string_set, data);
1956 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1958 useraddr += sizeof(gstrings);
1960 copy_to_user(useraddr, data,
1961 array_size(gstrings.len, ETH_GSTRING_LEN)))
1970 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1974 va_start(args, fmt);
1975 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1978 *data += ETH_GSTRING_LEN;
1980 EXPORT_SYMBOL(ethtool_sprintf);
1982 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1984 struct ethtool_value id;
1986 const struct ethtool_ops *ops = dev->ethtool_ops;
1987 netdevice_tracker dev_tracker;
1990 if (!ops->set_phys_id)
1996 if (copy_from_user(&id, useraddr, sizeof(id)))
1999 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2003 /* Drop the RTNL lock while waiting, but prevent reentry or
2004 * removal of the device.
2007 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2011 /* Driver will handle this itself */
2012 schedule_timeout_interruptible(
2013 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2015 /* Driver expects to be called at twice the frequency in rc */
2016 int n = rc * 2, interval = HZ / n;
2017 u64 count = mul_u32_u32(n, id.data);
2022 rc = ops->set_phys_id(dev,
2023 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2027 schedule_timeout_interruptible(interval);
2028 } while (!signal_pending(current) && (!id.data || i < count));
2032 netdev_put(dev, &dev_tracker);
2035 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2039 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2041 struct ethtool_stats stats;
2042 const struct ethtool_ops *ops = dev->ethtool_ops;
2046 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2049 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2052 if (n_stats > S32_MAX / sizeof(u64))
2054 WARN_ON_ONCE(!n_stats);
2055 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2058 stats.n_stats = n_stats;
2061 data = vzalloc(array_size(n_stats, sizeof(u64)));
2064 ops->get_ethtool_stats(dev, &stats, data);
2070 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2072 useraddr += sizeof(stats);
2073 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2082 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
2086 if (n_stats > S32_MAX / sizeof(u64))
2088 if (WARN_ON_ONCE(!n_stats))
2091 *data = vzalloc(array_size(n_stats, sizeof(u64)));
2098 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2099 struct ethtool_stats *stats,
2102 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2105 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2108 n_stats = phy_ops->get_sset_count(phydev);
2110 ret = ethtool_vzalloc_stats_array(n_stats, data);
2114 stats->n_stats = n_stats;
2115 return phy_ops->get_stats(phydev, stats, *data);
2118 static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2119 struct ethtool_stats *stats,
2122 const struct ethtool_ops *ops = dev->ethtool_ops;
2125 if (!ops || !ops->get_sset_count || ops->get_ethtool_phy_stats)
2128 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2130 ret = ethtool_vzalloc_stats_array(n_stats, data);
2134 stats->n_stats = n_stats;
2135 ops->get_ethtool_phy_stats(dev, stats, *data);
2140 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2142 struct phy_device *phydev = dev->phydev;
2143 struct ethtool_stats stats;
2145 int ret = -EOPNOTSUPP;
2147 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2151 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
2153 if (ret == -EOPNOTSUPP)
2154 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
2159 if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2164 useraddr += sizeof(stats);
2165 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2173 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2175 struct ethtool_perm_addr epaddr;
2177 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2180 if (epaddr.size < dev->addr_len)
2182 epaddr.size = dev->addr_len;
2184 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2186 useraddr += sizeof(epaddr);
2187 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2192 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2193 u32 cmd, u32 (*actor)(struct net_device *))
2195 struct ethtool_value edata = { .cmd = cmd };
2200 edata.data = actor(dev);
2202 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2207 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2208 void (*actor)(struct net_device *, u32))
2210 struct ethtool_value edata;
2215 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2218 actor(dev, edata.data);
2222 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2223 int (*actor)(struct net_device *, u32))
2225 struct ethtool_value edata;
2230 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2233 return actor(dev, edata.data);
2237 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2239 if (!dev->ethtool_ops->flash_device) {
2240 req->devlink = netdev_to_devlink_get(dev);
2244 return dev->ethtool_ops->flash_device(dev, &req->efl);
2247 static int ethtool_set_dump(struct net_device *dev,
2248 void __user *useraddr)
2250 struct ethtool_dump dump;
2252 if (!dev->ethtool_ops->set_dump)
2255 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2258 return dev->ethtool_ops->set_dump(dev, &dump);
2261 static int ethtool_get_dump_flag(struct net_device *dev,
2262 void __user *useraddr)
2265 struct ethtool_dump dump;
2266 const struct ethtool_ops *ops = dev->ethtool_ops;
2268 if (!ops->get_dump_flag)
2271 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2274 ret = ops->get_dump_flag(dev, &dump);
2278 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2283 static int ethtool_get_dump_data(struct net_device *dev,
2284 void __user *useraddr)
2288 struct ethtool_dump dump, tmp;
2289 const struct ethtool_ops *ops = dev->ethtool_ops;
2292 if (!ops->get_dump_data || !ops->get_dump_flag)
2295 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2298 memset(&tmp, 0, sizeof(tmp));
2299 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2300 ret = ops->get_dump_flag(dev, &tmp);
2304 len = min(tmp.len, dump.len);
2308 /* Don't ever let the driver think there's more space available
2309 * than it requested with .get_dump_flag().
2313 /* Always allocate enough space to hold the whole thing so that the
2314 * driver does not need to check the length and bother with partial
2317 data = vzalloc(tmp.len);
2320 ret = ops->get_dump_data(dev, &dump, data);
2324 /* There are two sane possibilities:
2325 * 1. The driver's .get_dump_data() does not touch dump.len.
2326 * 2. Or it may set dump.len to how much it really writes, which
2327 * should be tmp.len (or len if it can do a partial dump).
2328 * In any case respond to userspace with the actual length of data
2331 WARN_ON(dump.len != len && dump.len != tmp.len);
2334 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2338 useraddr += offsetof(struct ethtool_dump, data);
2339 if (copy_to_user(useraddr, data, len))
2346 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2348 struct ethtool_ts_info info;
2351 err = __ethtool_get_ts_info(dev, &info);
2355 if (copy_to_user(useraddr, &info, sizeof(info)))
2361 int ethtool_get_module_info_call(struct net_device *dev,
2362 struct ethtool_modinfo *modinfo)
2364 const struct ethtool_ops *ops = dev->ethtool_ops;
2365 struct phy_device *phydev = dev->phydev;
2368 return sfp_get_module_info(dev->sfp_bus, modinfo);
2370 if (phydev && phydev->drv && phydev->drv->module_info)
2371 return phydev->drv->module_info(phydev, modinfo);
2373 if (ops->get_module_info)
2374 return ops->get_module_info(dev, modinfo);
2379 static int ethtool_get_module_info(struct net_device *dev,
2380 void __user *useraddr)
2383 struct ethtool_modinfo modinfo;
2385 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2388 ret = ethtool_get_module_info_call(dev, &modinfo);
2392 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2398 int ethtool_get_module_eeprom_call(struct net_device *dev,
2399 struct ethtool_eeprom *ee, u8 *data)
2401 const struct ethtool_ops *ops = dev->ethtool_ops;
2402 struct phy_device *phydev = dev->phydev;
2405 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2407 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2408 return phydev->drv->module_eeprom(phydev, ee, data);
2410 if (ops->get_module_eeprom)
2411 return ops->get_module_eeprom(dev, ee, data);
2416 static int ethtool_get_module_eeprom(struct net_device *dev,
2417 void __user *useraddr)
2420 struct ethtool_modinfo modinfo;
2422 ret = ethtool_get_module_info_call(dev, &modinfo);
2426 return ethtool_get_any_eeprom(dev, useraddr,
2427 ethtool_get_module_eeprom_call,
2428 modinfo.eeprom_len);
2431 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2434 case ETHTOOL_RX_COPYBREAK:
2435 case ETHTOOL_TX_COPYBREAK:
2436 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2437 if (tuna->len != sizeof(u32) ||
2438 tuna->type_id != ETHTOOL_TUNABLE_U32)
2441 case ETHTOOL_PFC_PREVENTION_TOUT:
2442 if (tuna->len != sizeof(u16) ||
2443 tuna->type_id != ETHTOOL_TUNABLE_U16)
2453 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2456 struct ethtool_tunable tuna;
2457 const struct ethtool_ops *ops = dev->ethtool_ops;
2460 if (!ops->get_tunable)
2462 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2464 ret = ethtool_tunable_valid(&tuna);
2467 data = kzalloc(tuna.len, GFP_USER);
2470 ret = ops->get_tunable(dev, &tuna, data);
2473 useraddr += sizeof(tuna);
2475 if (copy_to_user(useraddr, data, tuna.len))
2484 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2487 struct ethtool_tunable tuna;
2488 const struct ethtool_ops *ops = dev->ethtool_ops;
2491 if (!ops->set_tunable)
2493 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2495 ret = ethtool_tunable_valid(&tuna);
2498 useraddr += sizeof(tuna);
2499 data = memdup_user(useraddr, tuna.len);
2501 return PTR_ERR(data);
2502 ret = ops->set_tunable(dev, &tuna, data);
2508 static noinline_for_stack int
2509 ethtool_get_per_queue_coalesce(struct net_device *dev,
2510 void __user *useraddr,
2511 struct ethtool_per_queue_op *per_queue_opt)
2515 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2517 if (!dev->ethtool_ops->get_per_queue_coalesce)
2520 useraddr += sizeof(*per_queue_opt);
2522 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2525 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2526 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2528 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2531 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2533 useraddr += sizeof(coalesce);
2539 static noinline_for_stack int
2540 ethtool_set_per_queue_coalesce(struct net_device *dev,
2541 void __user *useraddr,
2542 struct ethtool_per_queue_op *per_queue_opt)
2547 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2548 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2550 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2551 (!dev->ethtool_ops->get_per_queue_coalesce))
2554 useraddr += sizeof(*per_queue_opt);
2556 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2557 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2558 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2562 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2563 struct ethtool_coalesce coalesce;
2565 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2571 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2576 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2581 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2585 useraddr += sizeof(coalesce);
2591 for_each_set_bit(i, queue_mask, bit) {
2592 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2601 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2602 void __user *useraddr, u32 sub_cmd)
2604 struct ethtool_per_queue_op per_queue_opt;
2606 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2609 if (per_queue_opt.sub_command != sub_cmd)
2612 switch (per_queue_opt.sub_command) {
2613 case ETHTOOL_GCOALESCE:
2614 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2615 case ETHTOOL_SCOALESCE:
2616 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2622 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2625 case ETHTOOL_PHY_DOWNSHIFT:
2626 case ETHTOOL_PHY_FAST_LINK_DOWN:
2627 if (tuna->len != sizeof(u8) ||
2628 tuna->type_id != ETHTOOL_TUNABLE_U8)
2631 case ETHTOOL_PHY_EDPD:
2632 if (tuna->len != sizeof(u16) ||
2633 tuna->type_id != ETHTOOL_TUNABLE_U16)
2643 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2645 struct phy_device *phydev = dev->phydev;
2646 struct ethtool_tunable tuna;
2647 bool phy_drv_tunable;
2651 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2652 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2654 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2656 ret = ethtool_phy_tunable_valid(&tuna);
2659 data = kzalloc(tuna.len, GFP_USER);
2662 if (phy_drv_tunable) {
2663 mutex_lock(&phydev->lock);
2664 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2665 mutex_unlock(&phydev->lock);
2667 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2671 useraddr += sizeof(tuna);
2673 if (copy_to_user(useraddr, data, tuna.len))
2682 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2684 struct phy_device *phydev = dev->phydev;
2685 struct ethtool_tunable tuna;
2686 bool phy_drv_tunable;
2690 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2691 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2693 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2695 ret = ethtool_phy_tunable_valid(&tuna);
2698 useraddr += sizeof(tuna);
2699 data = memdup_user(useraddr, tuna.len);
2701 return PTR_ERR(data);
2702 if (phy_drv_tunable) {
2703 mutex_lock(&phydev->lock);
2704 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2705 mutex_unlock(&phydev->lock);
2707 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2714 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2716 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2719 if (!dev->ethtool_ops->get_fecparam)
2722 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2726 if (WARN_ON_ONCE(fecparam.reserved))
2727 fecparam.reserved = 0;
2729 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2734 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2736 struct ethtool_fecparam fecparam;
2738 if (!dev->ethtool_ops->set_fecparam)
2741 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2744 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2747 fecparam.active_fec = 0;
2748 fecparam.reserved = 0;
2750 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2753 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2756 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2757 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2759 struct net_device *dev;
2762 netdev_features_t old_features;
2764 dev = __dev_get_by_name(net, ifr->ifr_name);
2768 if (ethcmd == ETHTOOL_PERQUEUE) {
2769 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2774 /* Allow some commands to be done by anyone */
2777 case ETHTOOL_GDRVINFO:
2778 case ETHTOOL_GMSGLVL:
2780 case ETHTOOL_GCOALESCE:
2781 case ETHTOOL_GRINGPARAM:
2782 case ETHTOOL_GPAUSEPARAM:
2783 case ETHTOOL_GRXCSUM:
2784 case ETHTOOL_GTXCSUM:
2786 case ETHTOOL_GSSET_INFO:
2787 case ETHTOOL_GSTRINGS:
2788 case ETHTOOL_GSTATS:
2789 case ETHTOOL_GPHYSTATS:
2791 case ETHTOOL_GPERMADDR:
2795 case ETHTOOL_GFLAGS:
2796 case ETHTOOL_GPFLAGS:
2798 case ETHTOOL_GRXRINGS:
2799 case ETHTOOL_GRXCLSRLCNT:
2800 case ETHTOOL_GRXCLSRULE:
2801 case ETHTOOL_GRXCLSRLALL:
2802 case ETHTOOL_GRXFHINDIR:
2804 case ETHTOOL_GFEATURES:
2805 case ETHTOOL_GCHANNELS:
2806 case ETHTOOL_GET_TS_INFO:
2808 case ETHTOOL_GTUNABLE:
2809 case ETHTOOL_PHY_GTUNABLE:
2810 case ETHTOOL_GLINKSETTINGS:
2811 case ETHTOOL_GFECPARAM:
2814 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2818 if (dev->dev.parent)
2819 pm_runtime_get_sync(dev->dev.parent);
2821 if (!netif_device_present(dev)) {
2826 if (dev->ethtool_ops->begin) {
2827 rc = dev->ethtool_ops->begin(dev);
2831 old_features = dev->features;
2835 rc = ethtool_get_settings(dev, useraddr);
2838 rc = ethtool_set_settings(dev, useraddr);
2840 case ETHTOOL_GDRVINFO:
2841 rc = ethtool_get_drvinfo(dev, devlink_state);
2844 rc = ethtool_get_regs(dev, useraddr);
2847 rc = ethtool_get_wol(dev, useraddr);
2850 rc = ethtool_set_wol(dev, useraddr);
2852 case ETHTOOL_GMSGLVL:
2853 rc = ethtool_get_value(dev, useraddr, ethcmd,
2854 dev->ethtool_ops->get_msglevel);
2856 case ETHTOOL_SMSGLVL:
2857 rc = ethtool_set_value_void(dev, useraddr,
2858 dev->ethtool_ops->set_msglevel);
2860 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2863 rc = ethtool_get_eee(dev, useraddr);
2866 rc = ethtool_set_eee(dev, useraddr);
2868 case ETHTOOL_NWAY_RST:
2869 rc = ethtool_nway_reset(dev);
2872 rc = ethtool_get_link(dev, useraddr);
2874 case ETHTOOL_GEEPROM:
2875 rc = ethtool_get_eeprom(dev, useraddr);
2877 case ETHTOOL_SEEPROM:
2878 rc = ethtool_set_eeprom(dev, useraddr);
2880 case ETHTOOL_GCOALESCE:
2881 rc = ethtool_get_coalesce(dev, useraddr);
2883 case ETHTOOL_SCOALESCE:
2884 rc = ethtool_set_coalesce(dev, useraddr);
2886 case ETHTOOL_GRINGPARAM:
2887 rc = ethtool_get_ringparam(dev, useraddr);
2889 case ETHTOOL_SRINGPARAM:
2890 rc = ethtool_set_ringparam(dev, useraddr);
2892 case ETHTOOL_GPAUSEPARAM:
2893 rc = ethtool_get_pauseparam(dev, useraddr);
2895 case ETHTOOL_SPAUSEPARAM:
2896 rc = ethtool_set_pauseparam(dev, useraddr);
2899 rc = ethtool_self_test(dev, useraddr);
2901 case ETHTOOL_GSTRINGS:
2902 rc = ethtool_get_strings(dev, useraddr);
2904 case ETHTOOL_PHYS_ID:
2905 rc = ethtool_phys_id(dev, useraddr);
2907 case ETHTOOL_GSTATS:
2908 rc = ethtool_get_stats(dev, useraddr);
2910 case ETHTOOL_GPERMADDR:
2911 rc = ethtool_get_perm_addr(dev, useraddr);
2913 case ETHTOOL_GFLAGS:
2914 rc = ethtool_get_value(dev, useraddr, ethcmd,
2915 __ethtool_get_flags);
2917 case ETHTOOL_SFLAGS:
2918 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2920 case ETHTOOL_GPFLAGS:
2921 rc = ethtool_get_value(dev, useraddr, ethcmd,
2922 dev->ethtool_ops->get_priv_flags);
2924 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2926 case ETHTOOL_SPFLAGS:
2927 rc = ethtool_set_value(dev, useraddr,
2928 dev->ethtool_ops->set_priv_flags);
2931 case ETHTOOL_GRXRINGS:
2932 case ETHTOOL_GRXCLSRLCNT:
2933 case ETHTOOL_GRXCLSRULE:
2934 case ETHTOOL_GRXCLSRLALL:
2935 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2938 case ETHTOOL_SRXCLSRLDEL:
2939 case ETHTOOL_SRXCLSRLINS:
2940 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2942 case ETHTOOL_FLASHDEV:
2943 rc = ethtool_flash_device(dev, devlink_state);
2946 rc = ethtool_reset(dev, useraddr);
2948 case ETHTOOL_GSSET_INFO:
2949 rc = ethtool_get_sset_info(dev, useraddr);
2951 case ETHTOOL_GRXFHINDIR:
2952 rc = ethtool_get_rxfh_indir(dev, useraddr);
2954 case ETHTOOL_SRXFHINDIR:
2955 rc = ethtool_set_rxfh_indir(dev, useraddr);
2958 rc = ethtool_get_rxfh(dev, useraddr);
2961 rc = ethtool_set_rxfh(dev, useraddr);
2963 case ETHTOOL_GFEATURES:
2964 rc = ethtool_get_features(dev, useraddr);
2966 case ETHTOOL_SFEATURES:
2967 rc = ethtool_set_features(dev, useraddr);
2969 case ETHTOOL_GTXCSUM:
2970 case ETHTOOL_GRXCSUM:
2975 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2977 case ETHTOOL_STXCSUM:
2978 case ETHTOOL_SRXCSUM:
2983 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2985 case ETHTOOL_GCHANNELS:
2986 rc = ethtool_get_channels(dev, useraddr);
2988 case ETHTOOL_SCHANNELS:
2989 rc = ethtool_set_channels(dev, useraddr);
2991 case ETHTOOL_SET_DUMP:
2992 rc = ethtool_set_dump(dev, useraddr);
2994 case ETHTOOL_GET_DUMP_FLAG:
2995 rc = ethtool_get_dump_flag(dev, useraddr);
2997 case ETHTOOL_GET_DUMP_DATA:
2998 rc = ethtool_get_dump_data(dev, useraddr);
3000 case ETHTOOL_GET_TS_INFO:
3001 rc = ethtool_get_ts_info(dev, useraddr);
3003 case ETHTOOL_GMODULEINFO:
3004 rc = ethtool_get_module_info(dev, useraddr);
3006 case ETHTOOL_GMODULEEEPROM:
3007 rc = ethtool_get_module_eeprom(dev, useraddr);
3009 case ETHTOOL_GTUNABLE:
3010 rc = ethtool_get_tunable(dev, useraddr);
3012 case ETHTOOL_STUNABLE:
3013 rc = ethtool_set_tunable(dev, useraddr);
3015 case ETHTOOL_GPHYSTATS:
3016 rc = ethtool_get_phy_stats(dev, useraddr);
3018 case ETHTOOL_PERQUEUE:
3019 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
3021 case ETHTOOL_GLINKSETTINGS:
3022 rc = ethtool_get_link_ksettings(dev, useraddr);
3024 case ETHTOOL_SLINKSETTINGS:
3025 rc = ethtool_set_link_ksettings(dev, useraddr);
3027 case ETHTOOL_PHY_GTUNABLE:
3028 rc = get_phy_tunable(dev, useraddr);
3030 case ETHTOOL_PHY_STUNABLE:
3031 rc = set_phy_tunable(dev, useraddr);
3033 case ETHTOOL_GFECPARAM:
3034 rc = ethtool_get_fecparam(dev, useraddr);
3036 case ETHTOOL_SFECPARAM:
3037 rc = ethtool_set_fecparam(dev, useraddr);
3043 if (dev->ethtool_ops->complete)
3044 dev->ethtool_ops->complete(dev);
3046 if (old_features != dev->features)
3047 netdev_features_change(dev);
3049 if (dev->dev.parent)
3050 pm_runtime_put(dev->dev.parent);
3055 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3057 struct ethtool_devlink_compat *state;
3061 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3064 state = kzalloc(sizeof(*state), GFP_KERNEL);
3069 case ETHTOOL_FLASHDEV:
3070 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3074 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3079 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3085 case ETHTOOL_FLASHDEV:
3087 rc = devlink_compat_flash_update(state->devlink,
3090 case ETHTOOL_GDRVINFO:
3092 devlink_compat_running_version(state->devlink,
3093 state->info.fw_version,
3094 sizeof(state->info.fw_version));
3095 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3104 devlink_put(state->devlink);
3109 struct ethtool_rx_flow_key {
3110 struct flow_dissector_key_basic basic;
3112 struct flow_dissector_key_ipv4_addrs ipv4;
3113 struct flow_dissector_key_ipv6_addrs ipv6;
3115 struct flow_dissector_key_ports tp;
3116 struct flow_dissector_key_ip ip;
3117 struct flow_dissector_key_vlan vlan;
3118 struct flow_dissector_key_eth_addrs eth_addrs;
3119 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3121 struct ethtool_rx_flow_match {
3122 struct flow_dissector dissector;
3123 struct ethtool_rx_flow_key key;
3124 struct ethtool_rx_flow_key mask;
3127 struct ethtool_rx_flow_rule *
3128 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3130 const struct ethtool_rx_flow_spec *fs = input->fs;
3131 struct ethtool_rx_flow_match *match;
3132 struct ethtool_rx_flow_rule *flow;
3133 struct flow_action_entry *act;
3135 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3136 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3138 return ERR_PTR(-ENOMEM);
3140 /* ethtool_rx supports only one single action per rule. */
3141 flow->rule = flow_rule_alloc(1);
3144 return ERR_PTR(-ENOMEM);
3147 match = (struct ethtool_rx_flow_match *)flow->priv;
3148 flow->rule->match.dissector = &match->dissector;
3149 flow->rule->match.mask = &match->mask;
3150 flow->rule->match.key = &match->key;
3152 match->mask.basic.n_proto = htons(0xffff);
3154 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3156 const struct ethhdr *ether_spec, *ether_m_spec;
3158 ether_spec = &fs->h_u.ether_spec;
3159 ether_m_spec = &fs->m_u.ether_spec;
3161 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3162 ether_addr_copy(match->key.eth_addrs.src,
3163 ether_spec->h_source);
3164 ether_addr_copy(match->mask.eth_addrs.src,
3165 ether_m_spec->h_source);
3167 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3168 ether_addr_copy(match->key.eth_addrs.dst,
3169 ether_spec->h_dest);
3170 ether_addr_copy(match->mask.eth_addrs.dst,
3171 ether_m_spec->h_dest);
3173 if (ether_m_spec->h_proto) {
3174 match->key.basic.n_proto = ether_spec->h_proto;
3175 match->mask.basic.n_proto = ether_m_spec->h_proto;
3181 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3183 match->key.basic.n_proto = htons(ETH_P_IP);
3185 v4_spec = &fs->h_u.tcp_ip4_spec;
3186 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3188 if (v4_m_spec->ip4src) {
3189 match->key.ipv4.src = v4_spec->ip4src;
3190 match->mask.ipv4.src = v4_m_spec->ip4src;
3192 if (v4_m_spec->ip4dst) {
3193 match->key.ipv4.dst = v4_spec->ip4dst;
3194 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3196 if (v4_m_spec->ip4src ||
3197 v4_m_spec->ip4dst) {
3198 match->dissector.used_keys |=
3199 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3200 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3201 offsetof(struct ethtool_rx_flow_key, ipv4);
3203 if (v4_m_spec->psrc) {
3204 match->key.tp.src = v4_spec->psrc;
3205 match->mask.tp.src = v4_m_spec->psrc;
3207 if (v4_m_spec->pdst) {
3208 match->key.tp.dst = v4_spec->pdst;
3209 match->mask.tp.dst = v4_m_spec->pdst;
3211 if (v4_m_spec->psrc ||
3213 match->dissector.used_keys |=
3214 BIT(FLOW_DISSECTOR_KEY_PORTS);
3215 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3216 offsetof(struct ethtool_rx_flow_key, tp);
3218 if (v4_m_spec->tos) {
3219 match->key.ip.tos = v4_spec->tos;
3220 match->mask.ip.tos = v4_m_spec->tos;
3221 match->dissector.used_keys |=
3222 BIT(FLOW_DISSECTOR_KEY_IP);
3223 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3224 offsetof(struct ethtool_rx_flow_key, ip);
3230 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3232 match->key.basic.n_proto = htons(ETH_P_IPV6);
3234 v6_spec = &fs->h_u.tcp_ip6_spec;
3235 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3236 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
3237 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3238 sizeof(match->key.ipv6.src));
3239 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3240 sizeof(match->mask.ipv6.src));
3242 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3243 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3244 sizeof(match->key.ipv6.dst));
3245 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3246 sizeof(match->mask.ipv6.dst));
3248 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3249 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3250 match->dissector.used_keys |=
3251 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3252 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3253 offsetof(struct ethtool_rx_flow_key, ipv6);
3255 if (v6_m_spec->psrc) {
3256 match->key.tp.src = v6_spec->psrc;
3257 match->mask.tp.src = v6_m_spec->psrc;
3259 if (v6_m_spec->pdst) {
3260 match->key.tp.dst = v6_spec->pdst;
3261 match->mask.tp.dst = v6_m_spec->pdst;
3263 if (v6_m_spec->psrc ||
3265 match->dissector.used_keys |=
3266 BIT(FLOW_DISSECTOR_KEY_PORTS);
3267 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3268 offsetof(struct ethtool_rx_flow_key, tp);
3270 if (v6_m_spec->tclass) {
3271 match->key.ip.tos = v6_spec->tclass;
3272 match->mask.ip.tos = v6_m_spec->tclass;
3273 match->dissector.used_keys |=
3274 BIT(FLOW_DISSECTOR_KEY_IP);
3275 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3276 offsetof(struct ethtool_rx_flow_key, ip);
3281 ethtool_rx_flow_rule_destroy(flow);
3282 return ERR_PTR(-EINVAL);
3285 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3288 match->key.basic.ip_proto = IPPROTO_TCP;
3289 match->mask.basic.ip_proto = 0xff;
3293 match->key.basic.ip_proto = IPPROTO_UDP;
3294 match->mask.basic.ip_proto = 0xff;
3298 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3299 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3300 offsetof(struct ethtool_rx_flow_key, basic);
3302 if (fs->flow_type & FLOW_EXT) {
3303 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3304 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3306 if (ext_m_spec->vlan_etype) {
3307 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3308 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3311 if (ext_m_spec->vlan_tci) {
3312 match->key.vlan.vlan_id =
3313 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3314 match->mask.vlan.vlan_id =
3315 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3317 match->key.vlan.vlan_dei =
3318 !!(ext_h_spec->vlan_tci & htons(0x1000));
3319 match->mask.vlan.vlan_dei =
3320 !!(ext_m_spec->vlan_tci & htons(0x1000));
3322 match->key.vlan.vlan_priority =
3323 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3324 match->mask.vlan.vlan_priority =
3325 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3328 if (ext_m_spec->vlan_etype ||
3329 ext_m_spec->vlan_tci) {
3330 match->dissector.used_keys |=
3331 BIT(FLOW_DISSECTOR_KEY_VLAN);
3332 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3333 offsetof(struct ethtool_rx_flow_key, vlan);
3336 if (fs->flow_type & FLOW_MAC_EXT) {
3337 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3338 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3340 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3342 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3345 match->dissector.used_keys |=
3346 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3347 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3348 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3351 act = &flow->rule->action.entries[0];
3352 switch (fs->ring_cookie) {
3353 case RX_CLS_FLOW_DISC:
3354 act->id = FLOW_ACTION_DROP;
3356 case RX_CLS_FLOW_WAKE:
3357 act->id = FLOW_ACTION_WAKE;
3360 act->id = FLOW_ACTION_QUEUE;
3361 if (fs->flow_type & FLOW_RSS)
3362 act->queue.ctx = input->rss_ctx;
3364 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3365 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3371 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3373 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3378 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);