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>
30 #include <net/xdp_sock_drv.h>
31 #include <net/flow_offload.h>
32 #include <linux/ethtool_netlink.h>
33 #include <generated/utsrelease.h>
36 /* State held across locks and calls for commands which have devlink fallback */
37 struct ethtool_devlink_compat {
38 struct devlink *devlink;
40 struct ethtool_flash efl;
41 struct ethtool_drvinfo info;
45 static struct devlink *netdev_to_devlink_get(struct net_device *dev)
47 if (!dev->devlink_port)
49 return devlink_try_get(dev->devlink_port->devlink);
53 * Some useful ethtool_ops methods that're device independent.
54 * If we find that all drivers want to do the same thing here,
55 * we can turn these into dev_() function calls.
58 u32 ethtool_op_get_link(struct net_device *dev)
60 return netif_carrier_ok(dev) ? 1 : 0;
62 EXPORT_SYMBOL(ethtool_op_get_link);
64 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
66 info->so_timestamping =
67 SOF_TIMESTAMPING_TX_SOFTWARE |
68 SOF_TIMESTAMPING_RX_SOFTWARE |
69 SOF_TIMESTAMPING_SOFTWARE;
73 EXPORT_SYMBOL(ethtool_op_get_ts_info);
75 /* Handlers for each ethtool command */
77 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
79 struct ethtool_gfeatures cmd = {
80 .cmd = ETHTOOL_GFEATURES,
81 .size = ETHTOOL_DEV_FEATURE_WORDS,
83 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
88 /* in case feature bits run out again */
89 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
91 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
92 features[i].available = (u32)(dev->hw_features >> (32 * i));
93 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
94 features[i].active = (u32)(dev->features >> (32 * i));
95 features[i].never_changed =
96 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
99 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
100 if (get_user(copy_size, sizeaddr))
103 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
104 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
106 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
108 useraddr += sizeof(cmd);
109 if (copy_to_user(useraddr, features,
110 array_size(copy_size, sizeof(*features))))
116 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
118 struct ethtool_sfeatures cmd;
119 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
120 netdev_features_t wanted = 0, valid = 0;
123 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
125 useraddr += sizeof(cmd);
127 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
130 if (copy_from_user(features, useraddr, sizeof(features)))
133 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
134 valid |= (netdev_features_t)features[i].valid << (32 * i);
135 wanted |= (netdev_features_t)features[i].requested << (32 * i);
138 if (valid & ~NETIF_F_ETHTOOL_BITS)
141 if (valid & ~dev->hw_features) {
142 valid &= dev->hw_features;
143 ret |= ETHTOOL_F_UNSUPPORTED;
146 dev->wanted_features &= ~valid;
147 dev->wanted_features |= wanted & valid;
148 __netdev_update_features(dev);
150 if ((dev->wanted_features ^ dev->features) & valid)
151 ret |= ETHTOOL_F_WISH;
156 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
158 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
159 const struct ethtool_ops *ops = dev->ethtool_ops;
161 if (sset == ETH_SS_FEATURES)
162 return ARRAY_SIZE(netdev_features_strings);
164 if (sset == ETH_SS_RSS_HASH_FUNCS)
165 return ARRAY_SIZE(rss_hash_func_strings);
167 if (sset == ETH_SS_TUNABLES)
168 return ARRAY_SIZE(tunable_strings);
170 if (sset == ETH_SS_PHY_TUNABLES)
171 return ARRAY_SIZE(phy_tunable_strings);
173 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
174 !ops->get_ethtool_phy_stats &&
175 phy_ops && phy_ops->get_sset_count)
176 return phy_ops->get_sset_count(dev->phydev);
178 if (sset == ETH_SS_LINK_MODES)
179 return __ETHTOOL_LINK_MODE_MASK_NBITS;
181 if (ops->get_sset_count && ops->get_strings)
182 return ops->get_sset_count(dev, sset);
187 static void __ethtool_get_strings(struct net_device *dev,
188 u32 stringset, u8 *data)
190 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
191 const struct ethtool_ops *ops = dev->ethtool_ops;
193 if (stringset == ETH_SS_FEATURES)
194 memcpy(data, netdev_features_strings,
195 sizeof(netdev_features_strings));
196 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
197 memcpy(data, rss_hash_func_strings,
198 sizeof(rss_hash_func_strings));
199 else if (stringset == ETH_SS_TUNABLES)
200 memcpy(data, tunable_strings, sizeof(tunable_strings));
201 else if (stringset == ETH_SS_PHY_TUNABLES)
202 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
203 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
204 !ops->get_ethtool_phy_stats && phy_ops &&
205 phy_ops->get_strings)
206 phy_ops->get_strings(dev->phydev, data);
207 else if (stringset == ETH_SS_LINK_MODES)
208 memcpy(data, link_mode_names,
209 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
211 /* ops->get_strings is valid because checked earlier */
212 ops->get_strings(dev, stringset, data);
215 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
217 /* feature masks of legacy discrete ethtool ops */
220 case ETHTOOL_GTXCSUM:
221 case ETHTOOL_STXCSUM:
222 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
224 case ETHTOOL_GRXCSUM:
225 case ETHTOOL_SRXCSUM:
226 return NETIF_F_RXCSUM;
229 return NETIF_F_SG | NETIF_F_FRAGLIST;
232 return NETIF_F_ALL_TSO;
244 static int ethtool_get_one_feature(struct net_device *dev,
245 char __user *useraddr, u32 ethcmd)
247 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
248 struct ethtool_value edata = {
250 .data = !!(dev->features & mask),
253 if (copy_to_user(useraddr, &edata, sizeof(edata)))
258 static int ethtool_set_one_feature(struct net_device *dev,
259 void __user *useraddr, u32 ethcmd)
261 struct ethtool_value edata;
262 netdev_features_t mask;
264 if (copy_from_user(&edata, useraddr, sizeof(edata)))
267 mask = ethtool_get_feature_mask(ethcmd);
268 mask &= dev->hw_features;
273 dev->wanted_features |= mask;
275 dev->wanted_features &= ~mask;
277 __netdev_update_features(dev);
282 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
283 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
284 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
285 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
288 static u32 __ethtool_get_flags(struct net_device *dev)
292 if (dev->features & NETIF_F_LRO)
293 flags |= ETH_FLAG_LRO;
294 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
295 flags |= ETH_FLAG_RXVLAN;
296 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
297 flags |= ETH_FLAG_TXVLAN;
298 if (dev->features & NETIF_F_NTUPLE)
299 flags |= ETH_FLAG_NTUPLE;
300 if (dev->features & NETIF_F_RXHASH)
301 flags |= ETH_FLAG_RXHASH;
306 static int __ethtool_set_flags(struct net_device *dev, u32 data)
308 netdev_features_t features = 0, changed;
310 if (data & ~ETH_ALL_FLAGS)
313 if (data & ETH_FLAG_LRO)
314 features |= NETIF_F_LRO;
315 if (data & ETH_FLAG_RXVLAN)
316 features |= NETIF_F_HW_VLAN_CTAG_RX;
317 if (data & ETH_FLAG_TXVLAN)
318 features |= NETIF_F_HW_VLAN_CTAG_TX;
319 if (data & ETH_FLAG_NTUPLE)
320 features |= NETIF_F_NTUPLE;
321 if (data & ETH_FLAG_RXHASH)
322 features |= NETIF_F_RXHASH;
324 /* allow changing only bits set in hw_features */
325 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
326 if (changed & ~dev->hw_features)
327 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
329 dev->wanted_features =
330 (dev->wanted_features & ~changed) | (features & changed);
332 __netdev_update_features(dev);
337 /* Given two link masks, AND them together and save the result in dst. */
338 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
339 struct ethtool_link_ksettings *src)
341 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
342 unsigned int idx = 0;
344 for (; idx < size; idx++) {
345 dst->link_modes.supported[idx] &=
346 src->link_modes.supported[idx];
347 dst->link_modes.advertising[idx] &=
348 src->link_modes.advertising[idx];
351 EXPORT_SYMBOL(ethtool_intersect_link_masks);
353 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
359 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
361 /* return false if src had higher bits set. lower bits always updated. */
362 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
363 const unsigned long *src)
365 *legacy_u32 = src[0];
366 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
367 __ETHTOOL_LINK_MODE_MASK_NBITS;
369 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
371 /* return false if ksettings link modes had higher bits
372 * set. legacy_settings always updated (best effort)
375 convert_link_ksettings_to_legacy_settings(
376 struct ethtool_cmd *legacy_settings,
377 const struct ethtool_link_ksettings *link_ksettings)
381 memset(legacy_settings, 0, sizeof(*legacy_settings));
382 /* this also clears the deprecated fields in legacy structure:
388 retval &= ethtool_convert_link_mode_to_legacy_u32(
389 &legacy_settings->supported,
390 link_ksettings->link_modes.supported);
391 retval &= ethtool_convert_link_mode_to_legacy_u32(
392 &legacy_settings->advertising,
393 link_ksettings->link_modes.advertising);
394 retval &= ethtool_convert_link_mode_to_legacy_u32(
395 &legacy_settings->lp_advertising,
396 link_ksettings->link_modes.lp_advertising);
397 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
398 legacy_settings->duplex
399 = link_ksettings->base.duplex;
400 legacy_settings->port
401 = link_ksettings->base.port;
402 legacy_settings->phy_address
403 = link_ksettings->base.phy_address;
404 legacy_settings->autoneg
405 = link_ksettings->base.autoneg;
406 legacy_settings->mdio_support
407 = link_ksettings->base.mdio_support;
408 legacy_settings->eth_tp_mdix
409 = link_ksettings->base.eth_tp_mdix;
410 legacy_settings->eth_tp_mdix_ctrl
411 = link_ksettings->base.eth_tp_mdix_ctrl;
412 legacy_settings->transceiver
413 = link_ksettings->base.transceiver;
417 /* number of 32-bit words to store the user's link mode bitmaps */
418 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
419 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
421 /* layout of the struct passed from/to userland */
422 struct ethtool_link_usettings {
423 struct ethtool_link_settings base;
425 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
426 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
427 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
431 /* Internal kernel helper to query a device ethtool_link_settings. */
432 int __ethtool_get_link_ksettings(struct net_device *dev,
433 struct ethtool_link_ksettings *link_ksettings)
437 if (!dev->ethtool_ops->get_link_ksettings)
440 memset(link_ksettings, 0, sizeof(*link_ksettings));
441 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
443 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
445 /* convert ethtool_link_usettings in user space to a kernel internal
446 * ethtool_link_ksettings. return 0 on success, errno on error.
448 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
449 const void __user *from)
451 struct ethtool_link_usettings link_usettings;
453 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
456 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
457 bitmap_from_arr32(to->link_modes.supported,
458 link_usettings.link_modes.supported,
459 __ETHTOOL_LINK_MODE_MASK_NBITS);
460 bitmap_from_arr32(to->link_modes.advertising,
461 link_usettings.link_modes.advertising,
462 __ETHTOOL_LINK_MODE_MASK_NBITS);
463 bitmap_from_arr32(to->link_modes.lp_advertising,
464 link_usettings.link_modes.lp_advertising,
465 __ETHTOOL_LINK_MODE_MASK_NBITS);
470 /* Check if the user is trying to change anything besides speed/duplex */
471 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
473 struct ethtool_link_settings base2 = {};
475 base2.speed = cmd->base.speed;
476 base2.port = PORT_OTHER;
477 base2.duplex = cmd->base.duplex;
478 base2.cmd = cmd->base.cmd;
479 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
481 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
482 bitmap_empty(cmd->link_modes.supported,
483 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
484 bitmap_empty(cmd->link_modes.lp_advertising,
485 __ETHTOOL_LINK_MODE_MASK_NBITS);
488 /* convert a kernel internal ethtool_link_ksettings to
489 * ethtool_link_usettings in user space. return 0 on success, errno on
493 store_link_ksettings_for_user(void __user *to,
494 const struct ethtool_link_ksettings *from)
496 struct ethtool_link_usettings link_usettings;
498 memcpy(&link_usettings, from, sizeof(link_usettings));
499 bitmap_to_arr32(link_usettings.link_modes.supported,
500 from->link_modes.supported,
501 __ETHTOOL_LINK_MODE_MASK_NBITS);
502 bitmap_to_arr32(link_usettings.link_modes.advertising,
503 from->link_modes.advertising,
504 __ETHTOOL_LINK_MODE_MASK_NBITS);
505 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
506 from->link_modes.lp_advertising,
507 __ETHTOOL_LINK_MODE_MASK_NBITS);
509 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
515 /* Query device for its ethtool_link_settings. */
516 static int ethtool_get_link_ksettings(struct net_device *dev,
517 void __user *useraddr)
520 struct ethtool_link_ksettings link_ksettings;
523 if (!dev->ethtool_ops->get_link_ksettings)
526 /* handle bitmap nbits handshake */
527 if (copy_from_user(&link_ksettings.base, useraddr,
528 sizeof(link_ksettings.base)))
531 if (__ETHTOOL_LINK_MODE_MASK_NU32
532 != link_ksettings.base.link_mode_masks_nwords) {
533 /* wrong link mode nbits requested */
534 memset(&link_ksettings, 0, sizeof(link_ksettings));
535 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
536 /* send back number of words required as negative val */
537 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
538 "need too many bits for link modes!");
539 link_ksettings.base.link_mode_masks_nwords
540 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
542 /* copy the base fields back to user, not the link
545 if (copy_to_user(useraddr, &link_ksettings.base,
546 sizeof(link_ksettings.base)))
552 /* handshake successful: user/kernel agree on
553 * link_mode_masks_nwords
556 memset(&link_ksettings, 0, sizeof(link_ksettings));
557 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
561 /* make sure we tell the right values to user */
562 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
563 link_ksettings.base.link_mode_masks_nwords
564 = __ETHTOOL_LINK_MODE_MASK_NU32;
565 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
566 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
567 link_ksettings.base.rate_matching = RATE_MATCH_NONE;
569 return store_link_ksettings_for_user(useraddr, &link_ksettings);
572 /* Update device ethtool_link_settings. */
573 static int ethtool_set_link_ksettings(struct net_device *dev,
574 void __user *useraddr)
577 struct ethtool_link_ksettings link_ksettings;
581 if (!dev->ethtool_ops->set_link_ksettings)
584 /* make sure nbits field has expected value */
585 if (copy_from_user(&link_ksettings.base, useraddr,
586 sizeof(link_ksettings.base)))
589 if (__ETHTOOL_LINK_MODE_MASK_NU32
590 != link_ksettings.base.link_mode_masks_nwords)
593 /* copy the whole structure, now that we know it has expected
596 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
600 /* re-check nwords field, just in case */
601 if (__ETHTOOL_LINK_MODE_MASK_NU32
602 != link_ksettings.base.link_mode_masks_nwords)
605 if (link_ksettings.base.master_slave_cfg ||
606 link_ksettings.base.master_slave_state)
609 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
611 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
612 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
617 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
618 const struct ethtool_link_ksettings *cmd,
619 u32 *dev_speed, u8 *dev_duplex)
624 speed = cmd->base.speed;
625 duplex = cmd->base.duplex;
626 /* don't allow custom speed and duplex */
627 if (!ethtool_validate_speed(speed) ||
628 !ethtool_validate_duplex(duplex) ||
629 !ethtool_virtdev_validate_cmd(cmd))
632 *dev_duplex = duplex;
636 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
638 /* Query device for its ethtool_cmd settings.
640 * Backward compatibility note: for compatibility with legacy ethtool, this is
641 * now implemented via get_link_ksettings. When driver reports higher link mode
642 * bits, a kernel warning is logged once (with name of 1st driver/device) to
643 * recommend user to upgrade ethtool, but the command is successful (only the
644 * lower link mode bits reported back to user). Deprecated fields from
645 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
647 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
649 struct ethtool_link_ksettings link_ksettings;
650 struct ethtool_cmd cmd;
654 if (!dev->ethtool_ops->get_link_ksettings)
657 memset(&link_ksettings, 0, sizeof(link_ksettings));
658 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
661 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
663 /* send a sensible cmd tag back to user */
664 cmd.cmd = ETHTOOL_GSET;
666 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
672 /* Update device link settings with given ethtool_cmd.
674 * Backward compatibility note: for compatibility with legacy ethtool, this is
675 * now always implemented via set_link_settings. When user's request updates
676 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
677 * warning is logged once (with name of 1st driver/device) to recommend user to
678 * upgrade ethtool, and the request is rejected.
680 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
682 struct ethtool_link_ksettings link_ksettings;
683 struct ethtool_cmd cmd;
688 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
690 if (!dev->ethtool_ops->set_link_ksettings)
693 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
695 link_ksettings.base.link_mode_masks_nwords =
696 __ETHTOOL_LINK_MODE_MASK_NU32;
697 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
699 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
700 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
706 ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
708 const struct ethtool_ops *ops = dev->ethtool_ops;
709 struct device *parent = dev->dev.parent;
711 rsp->info.cmd = ETHTOOL_GDRVINFO;
712 strscpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
713 if (ops->get_drvinfo) {
714 ops->get_drvinfo(dev, &rsp->info);
715 if (!rsp->info.bus_info[0] && parent)
716 strscpy(rsp->info.bus_info, dev_name(parent),
717 sizeof(rsp->info.bus_info));
718 if (!rsp->info.driver[0] && parent && parent->driver)
719 strscpy(rsp->info.driver, parent->driver->name,
720 sizeof(rsp->info.driver));
721 } else if (parent && parent->driver) {
722 strscpy(rsp->info.bus_info, dev_name(parent),
723 sizeof(rsp->info.bus_info));
724 strscpy(rsp->info.driver, parent->driver->name,
725 sizeof(rsp->info.driver));
726 } else if (dev->rtnl_link_ops) {
727 strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
728 sizeof(rsp->info.driver));
734 * this method of obtaining string set info is deprecated;
735 * Use ETHTOOL_GSSET_INFO instead.
737 if (ops->get_sset_count) {
740 rc = ops->get_sset_count(dev, ETH_SS_TEST);
742 rsp->info.testinfo_len = rc;
743 rc = ops->get_sset_count(dev, ETH_SS_STATS);
745 rsp->info.n_stats = rc;
746 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
748 rsp->info.n_priv_flags = rc;
750 if (ops->get_regs_len) {
751 int ret = ops->get_regs_len(dev);
754 rsp->info.regdump_len = ret;
757 if (ops->get_eeprom_len)
758 rsp->info.eedump_len = ops->get_eeprom_len(dev);
760 if (!rsp->info.fw_version[0])
761 rsp->devlink = netdev_to_devlink_get(dev);
766 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
767 void __user *useraddr)
769 struct ethtool_sset_info info;
771 int i, idx = 0, n_bits = 0, ret, rc;
772 u32 *info_buf = NULL;
774 if (copy_from_user(&info, useraddr, sizeof(info)))
777 /* store copy of mask, because we zero struct later on */
778 sset_mask = info.sset_mask;
782 /* calculate size of return buffer */
783 n_bits = hweight64(sset_mask);
785 memset(&info, 0, sizeof(info));
786 info.cmd = ETHTOOL_GSSET_INFO;
788 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
793 * fill return buffer based on input bitmask and successful
794 * get_sset_count return
796 for (i = 0; i < 64; i++) {
797 if (!(sset_mask & (1ULL << i)))
800 rc = __ethtool_get_sset_count(dev, i);
802 info.sset_mask |= (1ULL << i);
803 info_buf[idx++] = rc;
808 if (copy_to_user(useraddr, &info, sizeof(info)))
811 useraddr += offsetof(struct ethtool_sset_info, data);
812 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
822 static noinline_for_stack int
823 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
824 const struct compat_ethtool_rxnfc __user *useraddr,
827 struct compat_ethtool_rxnfc crxnfc = {};
829 /* We expect there to be holes between fs.m_ext and
830 * fs.ring_cookie and at the end of fs, but nowhere else.
831 * On non-x86, no conversion should be needed.
833 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
834 sizeof(struct compat_ethtool_rxnfc) !=
835 sizeof(struct ethtool_rxnfc));
836 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
837 sizeof(useraddr->fs.m_ext) !=
838 offsetof(struct ethtool_rxnfc, fs.m_ext) +
839 sizeof(rxnfc->fs.m_ext));
840 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
841 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
842 offsetof(struct ethtool_rxnfc, fs.location) -
843 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
845 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
848 *rxnfc = (struct ethtool_rxnfc) {
850 .flow_type = crxnfc.flow_type,
853 .flow_type = crxnfc.fs.flow_type,
854 .h_u = crxnfc.fs.h_u,
855 .h_ext = crxnfc.fs.h_ext,
856 .m_u = crxnfc.fs.m_u,
857 .m_ext = crxnfc.fs.m_ext,
858 .ring_cookie = crxnfc.fs.ring_cookie,
859 .location = crxnfc.fs.location,
861 .rule_cnt = crxnfc.rule_cnt,
867 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
868 const void __user *useraddr,
871 if (compat_need_64bit_alignment_fixup())
872 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
874 if (copy_from_user(rxnfc, useraddr, size))
880 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
881 const struct ethtool_rxnfc *rxnfc,
882 size_t size, const u32 *rule_buf)
884 struct compat_ethtool_rxnfc crxnfc;
886 memset(&crxnfc, 0, sizeof(crxnfc));
887 crxnfc = (struct compat_ethtool_rxnfc) {
889 .flow_type = rxnfc->flow_type,
892 .flow_type = rxnfc->fs.flow_type,
893 .h_u = rxnfc->fs.h_u,
894 .h_ext = rxnfc->fs.h_ext,
895 .m_u = rxnfc->fs.m_u,
896 .m_ext = rxnfc->fs.m_ext,
897 .ring_cookie = rxnfc->fs.ring_cookie,
898 .location = rxnfc->fs.location,
900 .rule_cnt = rxnfc->rule_cnt,
903 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
909 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
910 const struct ethtool_rxnfc *rxnfc,
911 size_t size, const u32 *rule_buf)
915 if (compat_need_64bit_alignment_fixup()) {
916 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
918 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
920 ret = copy_to_user(useraddr, rxnfc, size);
921 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
928 if (copy_to_user(useraddr, rule_buf,
929 rxnfc->rule_cnt * sizeof(u32)))
936 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
937 u32 cmd, void __user *useraddr)
939 struct ethtool_rxnfc info;
940 size_t info_size = sizeof(info);
943 if (!dev->ethtool_ops->set_rxnfc)
946 /* struct ethtool_rxnfc was originally defined for
947 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
948 * members. User-space might still be using that
950 if (cmd == ETHTOOL_SRXFH)
951 info_size = (offsetof(struct ethtool_rxnfc, data) +
954 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
957 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
961 if (cmd == ETHTOOL_SRXCLSRLINS &&
962 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
968 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
969 u32 cmd, void __user *useraddr)
971 struct ethtool_rxnfc info;
972 size_t info_size = sizeof(info);
973 const struct ethtool_ops *ops = dev->ethtool_ops;
975 void *rule_buf = NULL;
980 /* struct ethtool_rxnfc was originally defined for
981 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
982 * members. User-space might still be using that
984 if (cmd == ETHTOOL_GRXFH)
985 info_size = (offsetof(struct ethtool_rxnfc, data) +
988 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
991 /* If FLOW_RSS was requested then user-space must be using the
992 * new definition, as FLOW_RSS is newer.
994 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
995 info_size = sizeof(info);
996 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
998 /* Since malicious users may modify the original data,
999 * we need to check whether FLOW_RSS is still requested.
1001 if (!(info.flow_type & FLOW_RSS))
1005 if (info.cmd != cmd)
1008 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1009 if (info.rule_cnt > 0) {
1010 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1011 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1018 ret = ops->get_rxnfc(dev, &info, rule_buf);
1022 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1029 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1030 struct ethtool_rxnfc *rx_rings,
1035 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1038 /* Validate ring indices */
1039 for (i = 0; i < size; i++)
1040 if (indir[i] >= rx_rings->data)
1046 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1048 void netdev_rss_key_fill(void *buffer, size_t len)
1050 BUG_ON(len > sizeof(netdev_rss_key));
1051 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1052 memcpy(buffer, netdev_rss_key, len);
1054 EXPORT_SYMBOL(netdev_rss_key_fill);
1056 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1057 void __user *useraddr)
1059 u32 user_size, dev_size;
1063 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1064 !dev->ethtool_ops->get_rxfh)
1066 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1070 if (copy_from_user(&user_size,
1071 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1075 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1076 &dev_size, sizeof(dev_size)))
1079 /* If the user buffer size is 0, this is just a query for the
1080 * device table size. Otherwise, if it's smaller than the
1081 * device table size it's an error.
1083 if (user_size < dev_size)
1084 return user_size == 0 ? 0 : -EINVAL;
1086 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1090 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1094 if (copy_to_user(useraddr +
1095 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1096 indir, dev_size * sizeof(indir[0])))
1104 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1105 void __user *useraddr)
1107 struct ethtool_rxnfc rx_rings;
1108 u32 user_size, dev_size, i;
1110 const struct ethtool_ops *ops = dev->ethtool_ops;
1112 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1114 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1118 dev_size = ops->get_rxfh_indir_size(dev);
1122 if (copy_from_user(&user_size,
1123 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1127 if (user_size != 0 && user_size != dev_size)
1130 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1134 rx_rings.cmd = ETHTOOL_GRXRINGS;
1135 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1139 if (user_size == 0) {
1140 for (i = 0; i < dev_size; i++)
1141 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1143 ret = ethtool_copy_validate_indir(indir,
1144 useraddr + ringidx_offset,
1151 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1155 /* indicate whether rxfh was set to default */
1157 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1159 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1166 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1167 void __user *useraddr)
1170 const struct ethtool_ops *ops = dev->ethtool_ops;
1171 u32 user_indir_size, user_key_size;
1172 u32 dev_indir_size = 0, dev_key_size = 0;
1173 struct ethtool_rxfh rxfh;
1184 if (ops->get_rxfh_indir_size)
1185 dev_indir_size = ops->get_rxfh_indir_size(dev);
1186 if (ops->get_rxfh_key_size)
1187 dev_key_size = ops->get_rxfh_key_size(dev);
1189 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1191 user_indir_size = rxfh.indir_size;
1192 user_key_size = rxfh.key_size;
1194 /* Check that reserved fields are 0 for now */
1195 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1197 /* Most drivers don't handle rss_context, check it's 0 as well */
1198 if (rxfh.rss_context && !ops->get_rxfh_context)
1201 rxfh.indir_size = dev_indir_size;
1202 rxfh.key_size = dev_key_size;
1203 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1206 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1207 (user_key_size && (user_key_size != dev_key_size)))
1210 indir_bytes = user_indir_size * sizeof(indir[0]);
1211 total_size = indir_bytes + user_key_size;
1212 rss_config = kzalloc(total_size, GFP_USER);
1216 if (user_indir_size)
1217 indir = (u32 *)rss_config;
1220 hkey = rss_config + indir_bytes;
1222 if (rxfh.rss_context)
1223 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1227 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1231 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1232 &dev_hfunc, sizeof(rxfh.hfunc))) {
1234 } else if (copy_to_user(useraddr +
1235 offsetof(struct ethtool_rxfh, rss_config[0]),
1236 rss_config, total_size)) {
1245 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1246 void __user *useraddr)
1249 const struct ethtool_ops *ops = dev->ethtool_ops;
1250 struct ethtool_rxnfc rx_rings;
1251 struct ethtool_rxfh rxfh;
1252 u32 dev_indir_size = 0, dev_key_size = 0, i;
1253 u32 *indir = NULL, indir_bytes = 0;
1256 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1257 bool delete = false;
1259 if (!ops->get_rxnfc || !ops->set_rxfh)
1262 if (ops->get_rxfh_indir_size)
1263 dev_indir_size = ops->get_rxfh_indir_size(dev);
1264 if (ops->get_rxfh_key_size)
1265 dev_key_size = ops->get_rxfh_key_size(dev);
1267 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1270 /* Check that reserved fields are 0 for now */
1271 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1273 /* Most drivers don't handle rss_context, check it's 0 as well */
1274 if (rxfh.rss_context && !ops->set_rxfh_context)
1277 /* If either indir, hash key or function is valid, proceed further.
1278 * Must request at least one change: indir size, hash key or function.
1280 if ((rxfh.indir_size &&
1281 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1282 rxfh.indir_size != dev_indir_size) ||
1283 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1284 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1285 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1288 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1289 indir_bytes = dev_indir_size * sizeof(indir[0]);
1291 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1295 rx_rings.cmd = ETHTOOL_GRXRINGS;
1296 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1300 /* rxfh.indir_size == 0 means reset the indir table to default (master
1301 * context) or delete the context (other RSS contexts).
1302 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1304 if (rxfh.indir_size &&
1305 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1306 indir = (u32 *)rss_config;
1307 ret = ethtool_copy_validate_indir(indir,
1308 useraddr + rss_cfg_offset,
1313 } else if (rxfh.indir_size == 0) {
1314 if (rxfh.rss_context == 0) {
1315 indir = (u32 *)rss_config;
1316 for (i = 0; i < dev_indir_size; i++)
1317 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1323 if (rxfh.key_size) {
1324 hkey = rss_config + indir_bytes;
1325 if (copy_from_user(hkey,
1326 useraddr + rss_cfg_offset + indir_bytes,
1333 if (rxfh.rss_context)
1334 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1335 &rxfh.rss_context, delete);
1337 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1341 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1342 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1345 if (!rxfh.rss_context) {
1346 /* indicate whether rxfh was set to default */
1347 if (rxfh.indir_size == 0)
1348 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1349 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1350 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1358 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1360 struct ethtool_regs regs;
1361 const struct ethtool_ops *ops = dev->ethtool_ops;
1365 if (!ops->get_regs || !ops->get_regs_len)
1368 if (copy_from_user(®s, useraddr, sizeof(regs)))
1371 reglen = ops->get_regs_len(dev);
1375 if (regs.len > reglen)
1378 regbuf = vzalloc(reglen);
1382 if (regs.len < reglen)
1385 ops->get_regs(dev, ®s, regbuf);
1388 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1390 useraddr += offsetof(struct ethtool_regs, data);
1391 if (copy_to_user(useraddr, regbuf, reglen))
1400 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1402 struct ethtool_value reset;
1405 if (!dev->ethtool_ops->reset)
1408 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1411 ret = dev->ethtool_ops->reset(dev, &reset.data);
1415 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1420 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1422 struct ethtool_wolinfo wol;
1424 if (!dev->ethtool_ops->get_wol)
1427 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1428 wol.cmd = ETHTOOL_GWOL;
1429 dev->ethtool_ops->get_wol(dev, &wol);
1431 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1436 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1438 struct ethtool_wolinfo wol;
1441 if (!dev->ethtool_ops->set_wol)
1444 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1447 ret = dev->ethtool_ops->set_wol(dev, &wol);
1451 dev->wol_enabled = !!wol.wolopts;
1452 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1457 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1459 struct ethtool_eee edata;
1462 if (!dev->ethtool_ops->get_eee)
1465 memset(&edata, 0, sizeof(struct ethtool_eee));
1466 edata.cmd = ETHTOOL_GEEE;
1467 rc = dev->ethtool_ops->get_eee(dev, &edata);
1472 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1478 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1480 struct ethtool_eee edata;
1483 if (!dev->ethtool_ops->set_eee)
1486 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1489 ret = dev->ethtool_ops->set_eee(dev, &edata);
1491 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1495 static int ethtool_nway_reset(struct net_device *dev)
1497 if (!dev->ethtool_ops->nway_reset)
1500 return dev->ethtool_ops->nway_reset(dev);
1503 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1505 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1506 int link = __ethtool_get_link(dev);
1512 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1517 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1518 int (*getter)(struct net_device *,
1519 struct ethtool_eeprom *, u8 *),
1522 struct ethtool_eeprom eeprom;
1523 void __user *userbuf = useraddr + sizeof(eeprom);
1524 u32 bytes_remaining;
1528 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1531 /* Check for wrap and zero */
1532 if (eeprom.offset + eeprom.len <= eeprom.offset)
1535 /* Check for exceeding total eeprom len */
1536 if (eeprom.offset + eeprom.len > total_len)
1539 data = kzalloc(PAGE_SIZE, GFP_USER);
1543 bytes_remaining = eeprom.len;
1544 while (bytes_remaining > 0) {
1545 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1547 ret = getter(dev, &eeprom, data);
1554 if (copy_to_user(userbuf, data, eeprom.len)) {
1558 userbuf += eeprom.len;
1559 eeprom.offset += eeprom.len;
1560 bytes_remaining -= eeprom.len;
1563 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1564 eeprom.offset -= eeprom.len;
1565 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1572 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1574 const struct ethtool_ops *ops = dev->ethtool_ops;
1576 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1577 !ops->get_eeprom_len(dev))
1580 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1581 ops->get_eeprom_len(dev));
1584 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1586 struct ethtool_eeprom eeprom;
1587 const struct ethtool_ops *ops = dev->ethtool_ops;
1588 void __user *userbuf = useraddr + sizeof(eeprom);
1589 u32 bytes_remaining;
1593 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1594 !ops->get_eeprom_len(dev))
1597 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1600 /* Check for wrap and zero */
1601 if (eeprom.offset + eeprom.len <= eeprom.offset)
1604 /* Check for exceeding total eeprom len */
1605 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1608 data = kzalloc(PAGE_SIZE, GFP_USER);
1612 bytes_remaining = eeprom.len;
1613 while (bytes_remaining > 0) {
1614 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1616 if (copy_from_user(data, userbuf, eeprom.len)) {
1620 ret = ops->set_eeprom(dev, &eeprom, data);
1623 userbuf += eeprom.len;
1624 eeprom.offset += eeprom.len;
1625 bytes_remaining -= eeprom.len;
1632 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1633 void __user *useraddr)
1635 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1636 struct kernel_ethtool_coalesce kernel_coalesce = {};
1639 if (!dev->ethtool_ops->get_coalesce)
1642 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1647 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1653 ethtool_set_coalesce_supported(struct net_device *dev,
1654 struct ethtool_coalesce *coalesce)
1656 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1657 u32 nonzero_params = 0;
1659 if (coalesce->rx_coalesce_usecs)
1660 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1661 if (coalesce->rx_max_coalesced_frames)
1662 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1663 if (coalesce->rx_coalesce_usecs_irq)
1664 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1665 if (coalesce->rx_max_coalesced_frames_irq)
1666 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1667 if (coalesce->tx_coalesce_usecs)
1668 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1669 if (coalesce->tx_max_coalesced_frames)
1670 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1671 if (coalesce->tx_coalesce_usecs_irq)
1672 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1673 if (coalesce->tx_max_coalesced_frames_irq)
1674 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1675 if (coalesce->stats_block_coalesce_usecs)
1676 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1677 if (coalesce->use_adaptive_rx_coalesce)
1678 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1679 if (coalesce->use_adaptive_tx_coalesce)
1680 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1681 if (coalesce->pkt_rate_low)
1682 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1683 if (coalesce->rx_coalesce_usecs_low)
1684 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1685 if (coalesce->rx_max_coalesced_frames_low)
1686 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1687 if (coalesce->tx_coalesce_usecs_low)
1688 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1689 if (coalesce->tx_max_coalesced_frames_low)
1690 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1691 if (coalesce->pkt_rate_high)
1692 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1693 if (coalesce->rx_coalesce_usecs_high)
1694 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1695 if (coalesce->rx_max_coalesced_frames_high)
1696 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1697 if (coalesce->tx_coalesce_usecs_high)
1698 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1699 if (coalesce->tx_max_coalesced_frames_high)
1700 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1701 if (coalesce->rate_sample_interval)
1702 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1704 return (supported_params & nonzero_params) == nonzero_params;
1707 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1708 void __user *useraddr)
1710 struct kernel_ethtool_coalesce kernel_coalesce = {};
1711 struct ethtool_coalesce coalesce;
1714 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1717 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1722 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1725 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1728 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1731 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1735 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1737 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1738 struct kernel_ethtool_ringparam kernel_ringparam = {};
1740 if (!dev->ethtool_ops->get_ringparam)
1743 dev->ethtool_ops->get_ringparam(dev, &ringparam,
1744 &kernel_ringparam, NULL);
1746 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1751 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1753 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1754 struct kernel_ethtool_ringparam kernel_ringparam;
1757 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1760 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1763 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
1765 /* ensure new ring parameters are within the maximums */
1766 if (ringparam.rx_pending > max.rx_max_pending ||
1767 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1768 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1769 ringparam.tx_pending > max.tx_max_pending)
1772 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1773 &kernel_ringparam, NULL);
1775 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1779 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1780 void __user *useraddr)
1782 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1784 if (!dev->ethtool_ops->get_channels)
1787 dev->ethtool_ops->get_channels(dev, &channels);
1789 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1794 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1795 void __user *useraddr)
1797 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1798 u16 from_channel, to_channel;
1799 u64 max_rxnfc_in_use;
1800 u32 max_rxfh_in_use;
1804 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1807 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1810 dev->ethtool_ops->get_channels(dev, &curr);
1812 if (channels.rx_count == curr.rx_count &&
1813 channels.tx_count == curr.tx_count &&
1814 channels.combined_count == curr.combined_count &&
1815 channels.other_count == curr.other_count)
1818 /* ensure new counts are within the maximums */
1819 if (channels.rx_count > curr.max_rx ||
1820 channels.tx_count > curr.max_tx ||
1821 channels.combined_count > curr.max_combined ||
1822 channels.other_count > curr.max_other)
1825 /* ensure there is at least one RX and one TX channel */
1826 if (!channels.combined_count &&
1827 (!channels.rx_count || !channels.tx_count))
1830 /* ensure the new Rx count fits within the configured Rx flow
1831 * indirection table/rxnfc settings */
1832 if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
1833 max_rxnfc_in_use = 0;
1834 if (!netif_is_rxfh_configured(dev) ||
1835 ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use))
1836 max_rxfh_in_use = 0;
1837 if (channels.combined_count + channels.rx_count <=
1838 max_t(u64, max_rxnfc_in_use, max_rxfh_in_use))
1841 /* Disabling channels, query zero-copy AF_XDP sockets */
1842 from_channel = channels.combined_count +
1843 min(channels.rx_count, channels.tx_count);
1844 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1845 for (i = from_channel; i < to_channel; i++)
1846 if (xsk_get_pool_from_qid(dev, i))
1849 ret = dev->ethtool_ops->set_channels(dev, &channels);
1851 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1855 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1857 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1859 if (!dev->ethtool_ops->get_pauseparam)
1862 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1864 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1869 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1871 struct ethtool_pauseparam pauseparam;
1874 if (!dev->ethtool_ops->set_pauseparam)
1877 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1880 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1882 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1886 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1888 struct ethtool_test test;
1889 const struct ethtool_ops *ops = dev->ethtool_ops;
1893 if (!ops->self_test || !ops->get_sset_count)
1896 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1899 WARN_ON(test_len == 0);
1901 if (copy_from_user(&test, useraddr, sizeof(test)))
1904 test.len = test_len;
1905 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1909 netif_testing_on(dev);
1910 ops->self_test(dev, &test, data);
1911 netif_testing_off(dev);
1914 if (copy_to_user(useraddr, &test, sizeof(test)))
1916 useraddr += sizeof(test);
1917 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
1926 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1928 struct ethtool_gstrings gstrings;
1932 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1935 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1938 if (ret > S32_MAX / ETH_GSTRING_LEN)
1945 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1949 __ethtool_get_strings(dev, gstrings.string_set, data);
1955 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1957 useraddr += sizeof(gstrings);
1959 copy_to_user(useraddr, data,
1960 array_size(gstrings.len, ETH_GSTRING_LEN)))
1969 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1973 va_start(args, fmt);
1974 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1977 *data += ETH_GSTRING_LEN;
1979 EXPORT_SYMBOL(ethtool_sprintf);
1981 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1983 struct ethtool_value id;
1985 const struct ethtool_ops *ops = dev->ethtool_ops;
1986 netdevice_tracker dev_tracker;
1989 if (!ops->set_phys_id)
1995 if (copy_from_user(&id, useraddr, sizeof(id)))
1998 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2002 /* Drop the RTNL lock while waiting, but prevent reentry or
2003 * removal of the device.
2006 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2010 /* Driver will handle this itself */
2011 schedule_timeout_interruptible(
2012 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2014 /* Driver expects to be called at twice the frequency in rc */
2015 int n = rc * 2, interval = HZ / n;
2016 u64 count = mul_u32_u32(n, id.data);
2021 rc = ops->set_phys_id(dev,
2022 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2026 schedule_timeout_interruptible(interval);
2027 } while (!signal_pending(current) && (!id.data || i < count));
2031 netdev_put(dev, &dev_tracker);
2034 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2038 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2040 struct ethtool_stats stats;
2041 const struct ethtool_ops *ops = dev->ethtool_ops;
2045 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2048 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2051 if (n_stats > S32_MAX / sizeof(u64))
2053 WARN_ON_ONCE(!n_stats);
2054 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2057 stats.n_stats = n_stats;
2060 data = vzalloc(array_size(n_stats, sizeof(u64)));
2063 ops->get_ethtool_stats(dev, &stats, data);
2069 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2071 useraddr += sizeof(stats);
2072 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2081 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
2085 if (n_stats > S32_MAX / sizeof(u64))
2087 if (WARN_ON_ONCE(!n_stats))
2090 *data = vzalloc(array_size(n_stats, sizeof(u64)));
2097 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2098 struct ethtool_stats *stats,
2101 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2104 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2107 n_stats = phy_ops->get_sset_count(phydev);
2109 ret = ethtool_vzalloc_stats_array(n_stats, data);
2113 stats->n_stats = n_stats;
2114 return phy_ops->get_stats(phydev, stats, *data);
2117 static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2118 struct ethtool_stats *stats,
2121 const struct ethtool_ops *ops = dev->ethtool_ops;
2124 if (!ops || !ops->get_sset_count || ops->get_ethtool_phy_stats)
2127 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2129 ret = ethtool_vzalloc_stats_array(n_stats, data);
2133 stats->n_stats = n_stats;
2134 ops->get_ethtool_phy_stats(dev, stats, *data);
2139 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2141 struct phy_device *phydev = dev->phydev;
2142 struct ethtool_stats stats;
2144 int ret = -EOPNOTSUPP;
2146 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2150 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
2152 if (ret == -EOPNOTSUPP)
2153 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
2158 if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2163 useraddr += sizeof(stats);
2164 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2172 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2174 struct ethtool_perm_addr epaddr;
2176 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2179 if (epaddr.size < dev->addr_len)
2181 epaddr.size = dev->addr_len;
2183 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2185 useraddr += sizeof(epaddr);
2186 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2191 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2192 u32 cmd, u32 (*actor)(struct net_device *))
2194 struct ethtool_value edata = { .cmd = cmd };
2199 edata.data = actor(dev);
2201 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2206 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2207 void (*actor)(struct net_device *, u32))
2209 struct ethtool_value edata;
2214 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2217 actor(dev, edata.data);
2221 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2222 int (*actor)(struct net_device *, u32))
2224 struct ethtool_value edata;
2229 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2232 return actor(dev, edata.data);
2236 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2238 if (!dev->ethtool_ops->flash_device) {
2239 req->devlink = netdev_to_devlink_get(dev);
2243 return dev->ethtool_ops->flash_device(dev, &req->efl);
2246 static int ethtool_set_dump(struct net_device *dev,
2247 void __user *useraddr)
2249 struct ethtool_dump dump;
2251 if (!dev->ethtool_ops->set_dump)
2254 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2257 return dev->ethtool_ops->set_dump(dev, &dump);
2260 static int ethtool_get_dump_flag(struct net_device *dev,
2261 void __user *useraddr)
2264 struct ethtool_dump dump;
2265 const struct ethtool_ops *ops = dev->ethtool_ops;
2267 if (!ops->get_dump_flag)
2270 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2273 ret = ops->get_dump_flag(dev, &dump);
2277 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2282 static int ethtool_get_dump_data(struct net_device *dev,
2283 void __user *useraddr)
2287 struct ethtool_dump dump, tmp;
2288 const struct ethtool_ops *ops = dev->ethtool_ops;
2291 if (!ops->get_dump_data || !ops->get_dump_flag)
2294 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2297 memset(&tmp, 0, sizeof(tmp));
2298 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2299 ret = ops->get_dump_flag(dev, &tmp);
2303 len = min(tmp.len, dump.len);
2307 /* Don't ever let the driver think there's more space available
2308 * than it requested with .get_dump_flag().
2312 /* Always allocate enough space to hold the whole thing so that the
2313 * driver does not need to check the length and bother with partial
2316 data = vzalloc(tmp.len);
2319 ret = ops->get_dump_data(dev, &dump, data);
2323 /* There are two sane possibilities:
2324 * 1. The driver's .get_dump_data() does not touch dump.len.
2325 * 2. Or it may set dump.len to how much it really writes, which
2326 * should be tmp.len (or len if it can do a partial dump).
2327 * In any case respond to userspace with the actual length of data
2330 WARN_ON(dump.len != len && dump.len != tmp.len);
2333 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2337 useraddr += offsetof(struct ethtool_dump, data);
2338 if (copy_to_user(useraddr, data, len))
2345 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2347 struct ethtool_ts_info info;
2350 err = __ethtool_get_ts_info(dev, &info);
2354 if (copy_to_user(useraddr, &info, sizeof(info)))
2360 int ethtool_get_module_info_call(struct net_device *dev,
2361 struct ethtool_modinfo *modinfo)
2363 const struct ethtool_ops *ops = dev->ethtool_ops;
2364 struct phy_device *phydev = dev->phydev;
2367 return sfp_get_module_info(dev->sfp_bus, modinfo);
2369 if (phydev && phydev->drv && phydev->drv->module_info)
2370 return phydev->drv->module_info(phydev, modinfo);
2372 if (ops->get_module_info)
2373 return ops->get_module_info(dev, modinfo);
2378 static int ethtool_get_module_info(struct net_device *dev,
2379 void __user *useraddr)
2382 struct ethtool_modinfo modinfo;
2384 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2387 ret = ethtool_get_module_info_call(dev, &modinfo);
2391 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2397 int ethtool_get_module_eeprom_call(struct net_device *dev,
2398 struct ethtool_eeprom *ee, u8 *data)
2400 const struct ethtool_ops *ops = dev->ethtool_ops;
2401 struct phy_device *phydev = dev->phydev;
2404 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2406 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2407 return phydev->drv->module_eeprom(phydev, ee, data);
2409 if (ops->get_module_eeprom)
2410 return ops->get_module_eeprom(dev, ee, data);
2415 static int ethtool_get_module_eeprom(struct net_device *dev,
2416 void __user *useraddr)
2419 struct ethtool_modinfo modinfo;
2421 ret = ethtool_get_module_info_call(dev, &modinfo);
2425 return ethtool_get_any_eeprom(dev, useraddr,
2426 ethtool_get_module_eeprom_call,
2427 modinfo.eeprom_len);
2430 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2433 case ETHTOOL_RX_COPYBREAK:
2434 case ETHTOOL_TX_COPYBREAK:
2435 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2436 if (tuna->len != sizeof(u32) ||
2437 tuna->type_id != ETHTOOL_TUNABLE_U32)
2440 case ETHTOOL_PFC_PREVENTION_TOUT:
2441 if (tuna->len != sizeof(u16) ||
2442 tuna->type_id != ETHTOOL_TUNABLE_U16)
2452 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2455 struct ethtool_tunable tuna;
2456 const struct ethtool_ops *ops = dev->ethtool_ops;
2459 if (!ops->get_tunable)
2461 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2463 ret = ethtool_tunable_valid(&tuna);
2466 data = kzalloc(tuna.len, GFP_USER);
2469 ret = ops->get_tunable(dev, &tuna, data);
2472 useraddr += sizeof(tuna);
2474 if (copy_to_user(useraddr, data, tuna.len))
2483 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2486 struct ethtool_tunable tuna;
2487 const struct ethtool_ops *ops = dev->ethtool_ops;
2490 if (!ops->set_tunable)
2492 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2494 ret = ethtool_tunable_valid(&tuna);
2497 useraddr += sizeof(tuna);
2498 data = memdup_user(useraddr, tuna.len);
2500 return PTR_ERR(data);
2501 ret = ops->set_tunable(dev, &tuna, data);
2507 static noinline_for_stack int
2508 ethtool_get_per_queue_coalesce(struct net_device *dev,
2509 void __user *useraddr,
2510 struct ethtool_per_queue_op *per_queue_opt)
2514 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2516 if (!dev->ethtool_ops->get_per_queue_coalesce)
2519 useraddr += sizeof(*per_queue_opt);
2521 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2524 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2525 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2527 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2530 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2532 useraddr += sizeof(coalesce);
2538 static noinline_for_stack int
2539 ethtool_set_per_queue_coalesce(struct net_device *dev,
2540 void __user *useraddr,
2541 struct ethtool_per_queue_op *per_queue_opt)
2546 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2547 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2549 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2550 (!dev->ethtool_ops->get_per_queue_coalesce))
2553 useraddr += sizeof(*per_queue_opt);
2555 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2556 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2557 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2561 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2562 struct ethtool_coalesce coalesce;
2564 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2570 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2575 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2580 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2584 useraddr += sizeof(coalesce);
2590 for_each_set_bit(i, queue_mask, bit) {
2591 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2600 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2601 void __user *useraddr, u32 sub_cmd)
2603 struct ethtool_per_queue_op per_queue_opt;
2605 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2608 if (per_queue_opt.sub_command != sub_cmd)
2611 switch (per_queue_opt.sub_command) {
2612 case ETHTOOL_GCOALESCE:
2613 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2614 case ETHTOOL_SCOALESCE:
2615 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2621 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2624 case ETHTOOL_PHY_DOWNSHIFT:
2625 case ETHTOOL_PHY_FAST_LINK_DOWN:
2626 if (tuna->len != sizeof(u8) ||
2627 tuna->type_id != ETHTOOL_TUNABLE_U8)
2630 case ETHTOOL_PHY_EDPD:
2631 if (tuna->len != sizeof(u16) ||
2632 tuna->type_id != ETHTOOL_TUNABLE_U16)
2642 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2644 struct phy_device *phydev = dev->phydev;
2645 struct ethtool_tunable tuna;
2646 bool phy_drv_tunable;
2650 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2651 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2653 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2655 ret = ethtool_phy_tunable_valid(&tuna);
2658 data = kzalloc(tuna.len, GFP_USER);
2661 if (phy_drv_tunable) {
2662 mutex_lock(&phydev->lock);
2663 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2664 mutex_unlock(&phydev->lock);
2666 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2670 useraddr += sizeof(tuna);
2672 if (copy_to_user(useraddr, data, tuna.len))
2681 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2683 struct phy_device *phydev = dev->phydev;
2684 struct ethtool_tunable tuna;
2685 bool phy_drv_tunable;
2689 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2690 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2692 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2694 ret = ethtool_phy_tunable_valid(&tuna);
2697 useraddr += sizeof(tuna);
2698 data = memdup_user(useraddr, tuna.len);
2700 return PTR_ERR(data);
2701 if (phy_drv_tunable) {
2702 mutex_lock(&phydev->lock);
2703 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2704 mutex_unlock(&phydev->lock);
2706 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2713 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2715 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2718 if (!dev->ethtool_ops->get_fecparam)
2721 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2725 if (WARN_ON_ONCE(fecparam.reserved))
2726 fecparam.reserved = 0;
2728 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2733 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2735 struct ethtool_fecparam fecparam;
2737 if (!dev->ethtool_ops->set_fecparam)
2740 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2743 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2746 fecparam.active_fec = 0;
2747 fecparam.reserved = 0;
2749 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2752 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2755 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2756 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2758 struct net_device *dev;
2761 netdev_features_t old_features;
2763 dev = __dev_get_by_name(net, ifr->ifr_name);
2767 if (ethcmd == ETHTOOL_PERQUEUE) {
2768 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2773 /* Allow some commands to be done by anyone */
2776 case ETHTOOL_GDRVINFO:
2777 case ETHTOOL_GMSGLVL:
2779 case ETHTOOL_GCOALESCE:
2780 case ETHTOOL_GRINGPARAM:
2781 case ETHTOOL_GPAUSEPARAM:
2782 case ETHTOOL_GRXCSUM:
2783 case ETHTOOL_GTXCSUM:
2785 case ETHTOOL_GSSET_INFO:
2786 case ETHTOOL_GSTRINGS:
2787 case ETHTOOL_GSTATS:
2788 case ETHTOOL_GPHYSTATS:
2790 case ETHTOOL_GPERMADDR:
2794 case ETHTOOL_GFLAGS:
2795 case ETHTOOL_GPFLAGS:
2797 case ETHTOOL_GRXRINGS:
2798 case ETHTOOL_GRXCLSRLCNT:
2799 case ETHTOOL_GRXCLSRULE:
2800 case ETHTOOL_GRXCLSRLALL:
2801 case ETHTOOL_GRXFHINDIR:
2803 case ETHTOOL_GFEATURES:
2804 case ETHTOOL_GCHANNELS:
2805 case ETHTOOL_GET_TS_INFO:
2807 case ETHTOOL_GTUNABLE:
2808 case ETHTOOL_PHY_GTUNABLE:
2809 case ETHTOOL_GLINKSETTINGS:
2810 case ETHTOOL_GFECPARAM:
2813 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2817 if (dev->dev.parent)
2818 pm_runtime_get_sync(dev->dev.parent);
2820 if (!netif_device_present(dev)) {
2825 if (dev->ethtool_ops->begin) {
2826 rc = dev->ethtool_ops->begin(dev);
2830 old_features = dev->features;
2834 rc = ethtool_get_settings(dev, useraddr);
2837 rc = ethtool_set_settings(dev, useraddr);
2839 case ETHTOOL_GDRVINFO:
2840 rc = ethtool_get_drvinfo(dev, devlink_state);
2843 rc = ethtool_get_regs(dev, useraddr);
2846 rc = ethtool_get_wol(dev, useraddr);
2849 rc = ethtool_set_wol(dev, useraddr);
2851 case ETHTOOL_GMSGLVL:
2852 rc = ethtool_get_value(dev, useraddr, ethcmd,
2853 dev->ethtool_ops->get_msglevel);
2855 case ETHTOOL_SMSGLVL:
2856 rc = ethtool_set_value_void(dev, useraddr,
2857 dev->ethtool_ops->set_msglevel);
2859 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2862 rc = ethtool_get_eee(dev, useraddr);
2865 rc = ethtool_set_eee(dev, useraddr);
2867 case ETHTOOL_NWAY_RST:
2868 rc = ethtool_nway_reset(dev);
2871 rc = ethtool_get_link(dev, useraddr);
2873 case ETHTOOL_GEEPROM:
2874 rc = ethtool_get_eeprom(dev, useraddr);
2876 case ETHTOOL_SEEPROM:
2877 rc = ethtool_set_eeprom(dev, useraddr);
2879 case ETHTOOL_GCOALESCE:
2880 rc = ethtool_get_coalesce(dev, useraddr);
2882 case ETHTOOL_SCOALESCE:
2883 rc = ethtool_set_coalesce(dev, useraddr);
2885 case ETHTOOL_GRINGPARAM:
2886 rc = ethtool_get_ringparam(dev, useraddr);
2888 case ETHTOOL_SRINGPARAM:
2889 rc = ethtool_set_ringparam(dev, useraddr);
2891 case ETHTOOL_GPAUSEPARAM:
2892 rc = ethtool_get_pauseparam(dev, useraddr);
2894 case ETHTOOL_SPAUSEPARAM:
2895 rc = ethtool_set_pauseparam(dev, useraddr);
2898 rc = ethtool_self_test(dev, useraddr);
2900 case ETHTOOL_GSTRINGS:
2901 rc = ethtool_get_strings(dev, useraddr);
2903 case ETHTOOL_PHYS_ID:
2904 rc = ethtool_phys_id(dev, useraddr);
2906 case ETHTOOL_GSTATS:
2907 rc = ethtool_get_stats(dev, useraddr);
2909 case ETHTOOL_GPERMADDR:
2910 rc = ethtool_get_perm_addr(dev, useraddr);
2912 case ETHTOOL_GFLAGS:
2913 rc = ethtool_get_value(dev, useraddr, ethcmd,
2914 __ethtool_get_flags);
2916 case ETHTOOL_SFLAGS:
2917 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2919 case ETHTOOL_GPFLAGS:
2920 rc = ethtool_get_value(dev, useraddr, ethcmd,
2921 dev->ethtool_ops->get_priv_flags);
2923 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2925 case ETHTOOL_SPFLAGS:
2926 rc = ethtool_set_value(dev, useraddr,
2927 dev->ethtool_ops->set_priv_flags);
2930 case ETHTOOL_GRXRINGS:
2931 case ETHTOOL_GRXCLSRLCNT:
2932 case ETHTOOL_GRXCLSRULE:
2933 case ETHTOOL_GRXCLSRLALL:
2934 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2937 case ETHTOOL_SRXCLSRLDEL:
2938 case ETHTOOL_SRXCLSRLINS:
2939 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2941 case ETHTOOL_FLASHDEV:
2942 rc = ethtool_flash_device(dev, devlink_state);
2945 rc = ethtool_reset(dev, useraddr);
2947 case ETHTOOL_GSSET_INFO:
2948 rc = ethtool_get_sset_info(dev, useraddr);
2950 case ETHTOOL_GRXFHINDIR:
2951 rc = ethtool_get_rxfh_indir(dev, useraddr);
2953 case ETHTOOL_SRXFHINDIR:
2954 rc = ethtool_set_rxfh_indir(dev, useraddr);
2957 rc = ethtool_get_rxfh(dev, useraddr);
2960 rc = ethtool_set_rxfh(dev, useraddr);
2962 case ETHTOOL_GFEATURES:
2963 rc = ethtool_get_features(dev, useraddr);
2965 case ETHTOOL_SFEATURES:
2966 rc = ethtool_set_features(dev, useraddr);
2968 case ETHTOOL_GTXCSUM:
2969 case ETHTOOL_GRXCSUM:
2974 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2976 case ETHTOOL_STXCSUM:
2977 case ETHTOOL_SRXCSUM:
2982 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2984 case ETHTOOL_GCHANNELS:
2985 rc = ethtool_get_channels(dev, useraddr);
2987 case ETHTOOL_SCHANNELS:
2988 rc = ethtool_set_channels(dev, useraddr);
2990 case ETHTOOL_SET_DUMP:
2991 rc = ethtool_set_dump(dev, useraddr);
2993 case ETHTOOL_GET_DUMP_FLAG:
2994 rc = ethtool_get_dump_flag(dev, useraddr);
2996 case ETHTOOL_GET_DUMP_DATA:
2997 rc = ethtool_get_dump_data(dev, useraddr);
2999 case ETHTOOL_GET_TS_INFO:
3000 rc = ethtool_get_ts_info(dev, useraddr);
3002 case ETHTOOL_GMODULEINFO:
3003 rc = ethtool_get_module_info(dev, useraddr);
3005 case ETHTOOL_GMODULEEEPROM:
3006 rc = ethtool_get_module_eeprom(dev, useraddr);
3008 case ETHTOOL_GTUNABLE:
3009 rc = ethtool_get_tunable(dev, useraddr);
3011 case ETHTOOL_STUNABLE:
3012 rc = ethtool_set_tunable(dev, useraddr);
3014 case ETHTOOL_GPHYSTATS:
3015 rc = ethtool_get_phy_stats(dev, useraddr);
3017 case ETHTOOL_PERQUEUE:
3018 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
3020 case ETHTOOL_GLINKSETTINGS:
3021 rc = ethtool_get_link_ksettings(dev, useraddr);
3023 case ETHTOOL_SLINKSETTINGS:
3024 rc = ethtool_set_link_ksettings(dev, useraddr);
3026 case ETHTOOL_PHY_GTUNABLE:
3027 rc = get_phy_tunable(dev, useraddr);
3029 case ETHTOOL_PHY_STUNABLE:
3030 rc = set_phy_tunable(dev, useraddr);
3032 case ETHTOOL_GFECPARAM:
3033 rc = ethtool_get_fecparam(dev, useraddr);
3035 case ETHTOOL_SFECPARAM:
3036 rc = ethtool_set_fecparam(dev, useraddr);
3042 if (dev->ethtool_ops->complete)
3043 dev->ethtool_ops->complete(dev);
3045 if (old_features != dev->features)
3046 netdev_features_change(dev);
3048 if (dev->dev.parent)
3049 pm_runtime_put(dev->dev.parent);
3054 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3056 struct ethtool_devlink_compat *state;
3060 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3063 state = kzalloc(sizeof(*state), GFP_KERNEL);
3068 case ETHTOOL_FLASHDEV:
3069 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3073 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3078 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3084 case ETHTOOL_FLASHDEV:
3086 rc = devlink_compat_flash_update(state->devlink,
3089 case ETHTOOL_GDRVINFO:
3091 devlink_compat_running_version(state->devlink,
3092 state->info.fw_version,
3093 sizeof(state->info.fw_version));
3094 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3103 devlink_put(state->devlink);
3108 struct ethtool_rx_flow_key {
3109 struct flow_dissector_key_basic basic;
3111 struct flow_dissector_key_ipv4_addrs ipv4;
3112 struct flow_dissector_key_ipv6_addrs ipv6;
3114 struct flow_dissector_key_ports tp;
3115 struct flow_dissector_key_ip ip;
3116 struct flow_dissector_key_vlan vlan;
3117 struct flow_dissector_key_eth_addrs eth_addrs;
3118 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3120 struct ethtool_rx_flow_match {
3121 struct flow_dissector dissector;
3122 struct ethtool_rx_flow_key key;
3123 struct ethtool_rx_flow_key mask;
3126 struct ethtool_rx_flow_rule *
3127 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3129 const struct ethtool_rx_flow_spec *fs = input->fs;
3130 static struct in6_addr zero_addr = {};
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 (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
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 (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
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 (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
3249 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
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);