2 * net/core/ethtool.c - Ethtool ioctl handler
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/sfp.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sched/signal.h>
29 #include <linux/net.h>
30 #include <net/devlink.h>
31 #include <net/xdp_sock.h>
32 #include <net/flow_offload.h>
35 * Some useful ethtool_ops methods that're device independent.
36 * If we find that all drivers want to do the same thing here,
37 * we can turn these into dev_() function calls.
40 u32 ethtool_op_get_link(struct net_device *dev)
42 return netif_carrier_ok(dev) ? 1 : 0;
44 EXPORT_SYMBOL(ethtool_op_get_link);
46 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
48 info->so_timestamping =
49 SOF_TIMESTAMPING_TX_SOFTWARE |
50 SOF_TIMESTAMPING_RX_SOFTWARE |
51 SOF_TIMESTAMPING_SOFTWARE;
55 EXPORT_SYMBOL(ethtool_op_get_ts_info);
57 /* Handlers for each ethtool command */
59 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
61 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
62 [NETIF_F_SG_BIT] = "tx-scatter-gather",
63 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
64 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
65 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
66 [NETIF_F_HIGHDMA_BIT] = "highdma",
67 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
68 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
70 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
71 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
72 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
73 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
74 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
75 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
76 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
77 [NETIF_F_LLTX_BIT] = "tx-lockless",
78 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
79 [NETIF_F_GRO_BIT] = "rx-gro",
80 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
81 [NETIF_F_LRO_BIT] = "rx-lro",
83 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
84 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
85 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
86 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
87 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
88 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
89 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
90 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
91 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
92 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
93 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
94 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
95 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
96 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
97 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
98 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation",
100 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
101 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
102 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
103 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
104 [NETIF_F_RXHASH_BIT] = "rx-hashing",
105 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
106 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
107 [NETIF_F_LOOPBACK_BIT] = "loopback",
108 [NETIF_F_RXFCS_BIT] = "rx-fcs",
109 [NETIF_F_RXALL_BIT] = "rx-all",
110 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
111 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
112 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
113 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
114 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
115 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
116 [NETIF_F_HW_TLS_TX_BIT] = "tls-hw-tx-offload",
117 [NETIF_F_HW_TLS_RX_BIT] = "tls-hw-rx-offload",
121 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
122 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
123 [ETH_RSS_HASH_XOR_BIT] = "xor",
124 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
128 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
129 [ETHTOOL_ID_UNSPEC] = "Unspec",
130 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
131 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
132 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
136 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
137 [ETHTOOL_ID_UNSPEC] = "Unspec",
138 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
141 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
143 struct ethtool_gfeatures cmd = {
144 .cmd = ETHTOOL_GFEATURES,
145 .size = ETHTOOL_DEV_FEATURE_WORDS,
147 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
148 u32 __user *sizeaddr;
152 /* in case feature bits run out again */
153 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
155 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
156 features[i].available = (u32)(dev->hw_features >> (32 * i));
157 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
158 features[i].active = (u32)(dev->features >> (32 * i));
159 features[i].never_changed =
160 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
163 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
164 if (get_user(copy_size, sizeaddr))
167 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
168 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
170 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
172 useraddr += sizeof(cmd);
173 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
179 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
181 struct ethtool_sfeatures cmd;
182 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
183 netdev_features_t wanted = 0, valid = 0;
186 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
188 useraddr += sizeof(cmd);
190 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
193 if (copy_from_user(features, useraddr, sizeof(features)))
196 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
197 valid |= (netdev_features_t)features[i].valid << (32 * i);
198 wanted |= (netdev_features_t)features[i].requested << (32 * i);
201 if (valid & ~NETIF_F_ETHTOOL_BITS)
204 if (valid & ~dev->hw_features) {
205 valid &= dev->hw_features;
206 ret |= ETHTOOL_F_UNSUPPORTED;
209 dev->wanted_features &= ~valid;
210 dev->wanted_features |= wanted & valid;
211 __netdev_update_features(dev);
213 if ((dev->wanted_features ^ dev->features) & valid)
214 ret |= ETHTOOL_F_WISH;
219 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
221 const struct ethtool_ops *ops = dev->ethtool_ops;
223 if (sset == ETH_SS_FEATURES)
224 return ARRAY_SIZE(netdev_features_strings);
226 if (sset == ETH_SS_RSS_HASH_FUNCS)
227 return ARRAY_SIZE(rss_hash_func_strings);
229 if (sset == ETH_SS_TUNABLES)
230 return ARRAY_SIZE(tunable_strings);
232 if (sset == ETH_SS_PHY_TUNABLES)
233 return ARRAY_SIZE(phy_tunable_strings);
235 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
236 !ops->get_ethtool_phy_stats)
237 return phy_ethtool_get_sset_count(dev->phydev);
239 if (ops->get_sset_count && ops->get_strings)
240 return ops->get_sset_count(dev, sset);
245 static void __ethtool_get_strings(struct net_device *dev,
246 u32 stringset, u8 *data)
248 const struct ethtool_ops *ops = dev->ethtool_ops;
250 if (stringset == ETH_SS_FEATURES)
251 memcpy(data, netdev_features_strings,
252 sizeof(netdev_features_strings));
253 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
254 memcpy(data, rss_hash_func_strings,
255 sizeof(rss_hash_func_strings));
256 else if (stringset == ETH_SS_TUNABLES)
257 memcpy(data, tunable_strings, sizeof(tunable_strings));
258 else if (stringset == ETH_SS_PHY_TUNABLES)
259 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
260 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
261 !ops->get_ethtool_phy_stats)
262 phy_ethtool_get_strings(dev->phydev, data);
264 /* ops->get_strings is valid because checked earlier */
265 ops->get_strings(dev, stringset, data);
268 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
270 /* feature masks of legacy discrete ethtool ops */
273 case ETHTOOL_GTXCSUM:
274 case ETHTOOL_STXCSUM:
275 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
276 case ETHTOOL_GRXCSUM:
277 case ETHTOOL_SRXCSUM:
278 return NETIF_F_RXCSUM;
284 return NETIF_F_ALL_TSO;
296 static int ethtool_get_one_feature(struct net_device *dev,
297 char __user *useraddr, u32 ethcmd)
299 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
300 struct ethtool_value edata = {
302 .data = !!(dev->features & mask),
305 if (copy_to_user(useraddr, &edata, sizeof(edata)))
310 static int ethtool_set_one_feature(struct net_device *dev,
311 void __user *useraddr, u32 ethcmd)
313 struct ethtool_value edata;
314 netdev_features_t mask;
316 if (copy_from_user(&edata, useraddr, sizeof(edata)))
319 mask = ethtool_get_feature_mask(ethcmd);
320 mask &= dev->hw_features;
325 dev->wanted_features |= mask;
327 dev->wanted_features &= ~mask;
329 __netdev_update_features(dev);
334 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
335 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
336 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
337 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
340 static u32 __ethtool_get_flags(struct net_device *dev)
344 if (dev->features & NETIF_F_LRO)
345 flags |= ETH_FLAG_LRO;
346 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
347 flags |= ETH_FLAG_RXVLAN;
348 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
349 flags |= ETH_FLAG_TXVLAN;
350 if (dev->features & NETIF_F_NTUPLE)
351 flags |= ETH_FLAG_NTUPLE;
352 if (dev->features & NETIF_F_RXHASH)
353 flags |= ETH_FLAG_RXHASH;
358 static int __ethtool_set_flags(struct net_device *dev, u32 data)
360 netdev_features_t features = 0, changed;
362 if (data & ~ETH_ALL_FLAGS)
365 if (data & ETH_FLAG_LRO)
366 features |= NETIF_F_LRO;
367 if (data & ETH_FLAG_RXVLAN)
368 features |= NETIF_F_HW_VLAN_CTAG_RX;
369 if (data & ETH_FLAG_TXVLAN)
370 features |= NETIF_F_HW_VLAN_CTAG_TX;
371 if (data & ETH_FLAG_NTUPLE)
372 features |= NETIF_F_NTUPLE;
373 if (data & ETH_FLAG_RXHASH)
374 features |= NETIF_F_RXHASH;
376 /* allow changing only bits set in hw_features */
377 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
378 if (changed & ~dev->hw_features)
379 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
381 dev->wanted_features =
382 (dev->wanted_features & ~changed) | (features & changed);
384 __netdev_update_features(dev);
389 /* Given two link masks, AND them together and save the result in dst. */
390 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
391 struct ethtool_link_ksettings *src)
393 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
394 unsigned int idx = 0;
396 for (; idx < size; idx++) {
397 dst->link_modes.supported[idx] &=
398 src->link_modes.supported[idx];
399 dst->link_modes.advertising[idx] &=
400 src->link_modes.advertising[idx];
403 EXPORT_SYMBOL(ethtool_intersect_link_masks);
405 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
408 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
411 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
413 /* return false if src had higher bits set. lower bits always updated. */
414 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
415 const unsigned long *src)
419 /* TODO: following test will soon always be true */
420 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
421 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
423 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
424 bitmap_fill(ext, 32);
425 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
426 if (bitmap_intersects(ext, src,
427 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
428 /* src mask goes beyond bit 31 */
432 *legacy_u32 = src[0];
435 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
437 /* return false if legacy contained non-0 deprecated fields
438 * maxtxpkt/maxrxpkt. rest of ksettings always updated
441 convert_legacy_settings_to_link_ksettings(
442 struct ethtool_link_ksettings *link_ksettings,
443 const struct ethtool_cmd *legacy_settings)
447 memset(link_ksettings, 0, sizeof(*link_ksettings));
449 /* This is used to tell users that driver is still using these
450 * deprecated legacy fields, and they should not use
451 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
453 if (legacy_settings->maxtxpkt ||
454 legacy_settings->maxrxpkt)
457 ethtool_convert_legacy_u32_to_link_mode(
458 link_ksettings->link_modes.supported,
459 legacy_settings->supported);
460 ethtool_convert_legacy_u32_to_link_mode(
461 link_ksettings->link_modes.advertising,
462 legacy_settings->advertising);
463 ethtool_convert_legacy_u32_to_link_mode(
464 link_ksettings->link_modes.lp_advertising,
465 legacy_settings->lp_advertising);
466 link_ksettings->base.speed
467 = ethtool_cmd_speed(legacy_settings);
468 link_ksettings->base.duplex
469 = legacy_settings->duplex;
470 link_ksettings->base.port
471 = legacy_settings->port;
472 link_ksettings->base.phy_address
473 = legacy_settings->phy_address;
474 link_ksettings->base.autoneg
475 = legacy_settings->autoneg;
476 link_ksettings->base.mdio_support
477 = legacy_settings->mdio_support;
478 link_ksettings->base.eth_tp_mdix
479 = legacy_settings->eth_tp_mdix;
480 link_ksettings->base.eth_tp_mdix_ctrl
481 = legacy_settings->eth_tp_mdix_ctrl;
485 /* return false if ksettings link modes had higher bits
486 * set. legacy_settings always updated (best effort)
489 convert_link_ksettings_to_legacy_settings(
490 struct ethtool_cmd *legacy_settings,
491 const struct ethtool_link_ksettings *link_ksettings)
495 memset(legacy_settings, 0, sizeof(*legacy_settings));
496 /* this also clears the deprecated fields in legacy structure:
502 retval &= ethtool_convert_link_mode_to_legacy_u32(
503 &legacy_settings->supported,
504 link_ksettings->link_modes.supported);
505 retval &= ethtool_convert_link_mode_to_legacy_u32(
506 &legacy_settings->advertising,
507 link_ksettings->link_modes.advertising);
508 retval &= ethtool_convert_link_mode_to_legacy_u32(
509 &legacy_settings->lp_advertising,
510 link_ksettings->link_modes.lp_advertising);
511 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
512 legacy_settings->duplex
513 = link_ksettings->base.duplex;
514 legacy_settings->port
515 = link_ksettings->base.port;
516 legacy_settings->phy_address
517 = link_ksettings->base.phy_address;
518 legacy_settings->autoneg
519 = link_ksettings->base.autoneg;
520 legacy_settings->mdio_support
521 = link_ksettings->base.mdio_support;
522 legacy_settings->eth_tp_mdix
523 = link_ksettings->base.eth_tp_mdix;
524 legacy_settings->eth_tp_mdix_ctrl
525 = link_ksettings->base.eth_tp_mdix_ctrl;
526 legacy_settings->transceiver
527 = link_ksettings->base.transceiver;
531 /* number of 32-bit words to store the user's link mode bitmaps */
532 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
533 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
535 /* layout of the struct passed from/to userland */
536 struct ethtool_link_usettings {
537 struct ethtool_link_settings base;
539 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
540 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
541 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
545 /* Internal kernel helper to query a device ethtool_link_settings. */
546 int __ethtool_get_link_ksettings(struct net_device *dev,
547 struct ethtool_link_ksettings *link_ksettings)
551 if (!dev->ethtool_ops->get_link_ksettings)
554 memset(link_ksettings, 0, sizeof(*link_ksettings));
555 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
557 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
559 /* convert ethtool_link_usettings in user space to a kernel internal
560 * ethtool_link_ksettings. return 0 on success, errno on error.
562 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
563 const void __user *from)
565 struct ethtool_link_usettings link_usettings;
567 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
570 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
571 bitmap_from_arr32(to->link_modes.supported,
572 link_usettings.link_modes.supported,
573 __ETHTOOL_LINK_MODE_MASK_NBITS);
574 bitmap_from_arr32(to->link_modes.advertising,
575 link_usettings.link_modes.advertising,
576 __ETHTOOL_LINK_MODE_MASK_NBITS);
577 bitmap_from_arr32(to->link_modes.lp_advertising,
578 link_usettings.link_modes.lp_advertising,
579 __ETHTOOL_LINK_MODE_MASK_NBITS);
584 /* convert a kernel internal ethtool_link_ksettings to
585 * ethtool_link_usettings in user space. return 0 on success, errno on
589 store_link_ksettings_for_user(void __user *to,
590 const struct ethtool_link_ksettings *from)
592 struct ethtool_link_usettings link_usettings;
594 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
595 bitmap_to_arr32(link_usettings.link_modes.supported,
596 from->link_modes.supported,
597 __ETHTOOL_LINK_MODE_MASK_NBITS);
598 bitmap_to_arr32(link_usettings.link_modes.advertising,
599 from->link_modes.advertising,
600 __ETHTOOL_LINK_MODE_MASK_NBITS);
601 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
602 from->link_modes.lp_advertising,
603 __ETHTOOL_LINK_MODE_MASK_NBITS);
605 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
611 /* Query device for its ethtool_link_settings. */
612 static int ethtool_get_link_ksettings(struct net_device *dev,
613 void __user *useraddr)
616 struct ethtool_link_ksettings link_ksettings;
619 if (!dev->ethtool_ops->get_link_ksettings)
622 /* handle bitmap nbits handshake */
623 if (copy_from_user(&link_ksettings.base, useraddr,
624 sizeof(link_ksettings.base)))
627 if (__ETHTOOL_LINK_MODE_MASK_NU32
628 != link_ksettings.base.link_mode_masks_nwords) {
629 /* wrong link mode nbits requested */
630 memset(&link_ksettings, 0, sizeof(link_ksettings));
631 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
632 /* send back number of words required as negative val */
633 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
634 "need too many bits for link modes!");
635 link_ksettings.base.link_mode_masks_nwords
636 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
638 /* copy the base fields back to user, not the link
641 if (copy_to_user(useraddr, &link_ksettings.base,
642 sizeof(link_ksettings.base)))
648 /* handshake successful: user/kernel agree on
649 * link_mode_masks_nwords
652 memset(&link_ksettings, 0, sizeof(link_ksettings));
653 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
657 /* make sure we tell the right values to user */
658 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
659 link_ksettings.base.link_mode_masks_nwords
660 = __ETHTOOL_LINK_MODE_MASK_NU32;
662 return store_link_ksettings_for_user(useraddr, &link_ksettings);
665 /* Update device ethtool_link_settings. */
666 static int ethtool_set_link_ksettings(struct net_device *dev,
667 void __user *useraddr)
670 struct ethtool_link_ksettings link_ksettings;
674 if (!dev->ethtool_ops->set_link_ksettings)
677 /* make sure nbits field has expected value */
678 if (copy_from_user(&link_ksettings.base, useraddr,
679 sizeof(link_ksettings.base)))
682 if (__ETHTOOL_LINK_MODE_MASK_NU32
683 != link_ksettings.base.link_mode_masks_nwords)
686 /* copy the whole structure, now that we know it has expected
689 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
693 /* re-check nwords field, just in case */
694 if (__ETHTOOL_LINK_MODE_MASK_NU32
695 != link_ksettings.base.link_mode_masks_nwords)
698 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
701 /* Query device for its ethtool_cmd settings.
703 * Backward compatibility note: for compatibility with legacy ethtool, this is
704 * now implemented via get_link_ksettings. When driver reports higher link mode
705 * bits, a kernel warning is logged once (with name of 1st driver/device) to
706 * recommend user to upgrade ethtool, but the command is successful (only the
707 * lower link mode bits reported back to user). Deprecated fields from
708 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
710 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
712 struct ethtool_link_ksettings link_ksettings;
713 struct ethtool_cmd cmd;
717 if (!dev->ethtool_ops->get_link_ksettings)
720 memset(&link_ksettings, 0, sizeof(link_ksettings));
721 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
724 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
726 /* send a sensible cmd tag back to user */
727 cmd.cmd = ETHTOOL_GSET;
729 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
735 /* Update device link settings with given ethtool_cmd.
737 * Backward compatibility note: for compatibility with legacy ethtool, this is
738 * now always implemented via set_link_settings. When user's request updates
739 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
740 * warning is logged once (with name of 1st driver/device) to recommend user to
741 * upgrade ethtool, and the request is rejected.
743 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
745 struct ethtool_link_ksettings link_ksettings;
746 struct ethtool_cmd cmd;
750 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
752 if (!dev->ethtool_ops->set_link_ksettings)
755 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
757 link_ksettings.base.link_mode_masks_nwords =
758 __ETHTOOL_LINK_MODE_MASK_NU32;
759 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
762 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
763 void __user *useraddr)
765 struct ethtool_drvinfo info;
766 const struct ethtool_ops *ops = dev->ethtool_ops;
768 memset(&info, 0, sizeof(info));
769 info.cmd = ETHTOOL_GDRVINFO;
770 if (ops->get_drvinfo) {
771 ops->get_drvinfo(dev, &info);
772 } else if (dev->dev.parent && dev->dev.parent->driver) {
773 strlcpy(info.bus_info, dev_name(dev->dev.parent),
774 sizeof(info.bus_info));
775 strlcpy(info.driver, dev->dev.parent->driver->name,
776 sizeof(info.driver));
782 * this method of obtaining string set info is deprecated;
783 * Use ETHTOOL_GSSET_INFO instead.
785 if (ops->get_sset_count) {
788 rc = ops->get_sset_count(dev, ETH_SS_TEST);
790 info.testinfo_len = rc;
791 rc = ops->get_sset_count(dev, ETH_SS_STATS);
794 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
796 info.n_priv_flags = rc;
798 if (ops->get_regs_len) {
799 int ret = ops->get_regs_len(dev);
802 info.regdump_len = ret;
805 if (ops->get_eeprom_len)
806 info.eedump_len = ops->get_eeprom_len(dev);
808 if (!info.fw_version[0])
809 devlink_compat_running_version(dev, info.fw_version,
810 sizeof(info.fw_version));
812 if (copy_to_user(useraddr, &info, sizeof(info)))
817 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
818 void __user *useraddr)
820 struct ethtool_sset_info info;
822 int i, idx = 0, n_bits = 0, ret, rc;
823 u32 *info_buf = NULL;
825 if (copy_from_user(&info, useraddr, sizeof(info)))
828 /* store copy of mask, because we zero struct later on */
829 sset_mask = info.sset_mask;
833 /* calculate size of return buffer */
834 n_bits = hweight64(sset_mask);
836 memset(&info, 0, sizeof(info));
837 info.cmd = ETHTOOL_GSSET_INFO;
839 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
844 * fill return buffer based on input bitmask and successful
845 * get_sset_count return
847 for (i = 0; i < 64; i++) {
848 if (!(sset_mask & (1ULL << i)))
851 rc = __ethtool_get_sset_count(dev, i);
853 info.sset_mask |= (1ULL << i);
854 info_buf[idx++] = rc;
859 if (copy_to_user(useraddr, &info, sizeof(info)))
862 useraddr += offsetof(struct ethtool_sset_info, data);
863 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
873 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
874 u32 cmd, void __user *useraddr)
876 struct ethtool_rxnfc info;
877 size_t info_size = sizeof(info);
880 if (!dev->ethtool_ops->set_rxnfc)
883 /* struct ethtool_rxnfc was originally defined for
884 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
885 * members. User-space might still be using that
887 if (cmd == ETHTOOL_SRXFH)
888 info_size = (offsetof(struct ethtool_rxnfc, data) +
891 if (copy_from_user(&info, useraddr, info_size))
894 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
898 if (cmd == ETHTOOL_SRXCLSRLINS &&
899 copy_to_user(useraddr, &info, info_size))
905 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
906 u32 cmd, void __user *useraddr)
908 struct ethtool_rxnfc info;
909 size_t info_size = sizeof(info);
910 const struct ethtool_ops *ops = dev->ethtool_ops;
912 void *rule_buf = NULL;
917 /* struct ethtool_rxnfc was originally defined for
918 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
919 * members. User-space might still be using that
921 if (cmd == ETHTOOL_GRXFH)
922 info_size = (offsetof(struct ethtool_rxnfc, data) +
925 if (copy_from_user(&info, useraddr, info_size))
928 /* If FLOW_RSS was requested then user-space must be using the
929 * new definition, as FLOW_RSS is newer.
931 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
932 info_size = sizeof(info);
933 if (copy_from_user(&info, useraddr, info_size))
935 /* Since malicious users may modify the original data,
936 * we need to check whether FLOW_RSS is still requested.
938 if (!(info.flow_type & FLOW_RSS))
945 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
946 if (info.rule_cnt > 0) {
947 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
948 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
955 ret = ops->get_rxnfc(dev, &info, rule_buf);
960 if (copy_to_user(useraddr, &info, info_size))
964 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
965 if (copy_to_user(useraddr, rule_buf,
966 info.rule_cnt * sizeof(u32)))
977 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
978 struct ethtool_rxnfc *rx_rings,
983 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
986 /* Validate ring indices */
987 for (i = 0; i < size; i++)
988 if (indir[i] >= rx_rings->data)
994 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
996 void netdev_rss_key_fill(void *buffer, size_t len)
998 BUG_ON(len > sizeof(netdev_rss_key));
999 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1000 memcpy(buffer, netdev_rss_key, len);
1002 EXPORT_SYMBOL(netdev_rss_key_fill);
1004 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1006 u32 dev_size, current_max = 0;
1010 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1011 !dev->ethtool_ops->get_rxfh)
1013 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1017 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1021 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1026 current_max = max(current_max, indir[dev_size]);
1035 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1036 void __user *useraddr)
1038 u32 user_size, dev_size;
1042 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1043 !dev->ethtool_ops->get_rxfh)
1045 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1049 if (copy_from_user(&user_size,
1050 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1054 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1055 &dev_size, sizeof(dev_size)))
1058 /* If the user buffer size is 0, this is just a query for the
1059 * device table size. Otherwise, if it's smaller than the
1060 * device table size it's an error.
1062 if (user_size < dev_size)
1063 return user_size == 0 ? 0 : -EINVAL;
1065 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1069 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1073 if (copy_to_user(useraddr +
1074 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1075 indir, dev_size * sizeof(indir[0])))
1083 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1084 void __user *useraddr)
1086 struct ethtool_rxnfc rx_rings;
1087 u32 user_size, dev_size, i;
1089 const struct ethtool_ops *ops = dev->ethtool_ops;
1091 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1093 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1097 dev_size = ops->get_rxfh_indir_size(dev);
1101 if (copy_from_user(&user_size,
1102 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1106 if (user_size != 0 && user_size != dev_size)
1109 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1113 rx_rings.cmd = ETHTOOL_GRXRINGS;
1114 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1118 if (user_size == 0) {
1119 for (i = 0; i < dev_size; i++)
1120 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1122 ret = ethtool_copy_validate_indir(indir,
1123 useraddr + ringidx_offset,
1130 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1134 /* indicate whether rxfh was set to default */
1136 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1138 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1145 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1146 void __user *useraddr)
1149 const struct ethtool_ops *ops = dev->ethtool_ops;
1150 u32 user_indir_size, user_key_size;
1151 u32 dev_indir_size = 0, dev_key_size = 0;
1152 struct ethtool_rxfh rxfh;
1163 if (ops->get_rxfh_indir_size)
1164 dev_indir_size = ops->get_rxfh_indir_size(dev);
1165 if (ops->get_rxfh_key_size)
1166 dev_key_size = ops->get_rxfh_key_size(dev);
1168 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1170 user_indir_size = rxfh.indir_size;
1171 user_key_size = rxfh.key_size;
1173 /* Check that reserved fields are 0 for now */
1174 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1176 /* Most drivers don't handle rss_context, check it's 0 as well */
1177 if (rxfh.rss_context && !ops->get_rxfh_context)
1180 rxfh.indir_size = dev_indir_size;
1181 rxfh.key_size = dev_key_size;
1182 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1185 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1186 (user_key_size && (user_key_size != dev_key_size)))
1189 indir_bytes = user_indir_size * sizeof(indir[0]);
1190 total_size = indir_bytes + user_key_size;
1191 rss_config = kzalloc(total_size, GFP_USER);
1195 if (user_indir_size)
1196 indir = (u32 *)rss_config;
1199 hkey = rss_config + indir_bytes;
1201 if (rxfh.rss_context)
1202 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1206 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1210 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1211 &dev_hfunc, sizeof(rxfh.hfunc))) {
1213 } else if (copy_to_user(useraddr +
1214 offsetof(struct ethtool_rxfh, rss_config[0]),
1215 rss_config, total_size)) {
1224 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1225 void __user *useraddr)
1228 const struct ethtool_ops *ops = dev->ethtool_ops;
1229 struct ethtool_rxnfc rx_rings;
1230 struct ethtool_rxfh rxfh;
1231 u32 dev_indir_size = 0, dev_key_size = 0, i;
1232 u32 *indir = NULL, indir_bytes = 0;
1235 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1236 bool delete = false;
1238 if (!ops->get_rxnfc || !ops->set_rxfh)
1241 if (ops->get_rxfh_indir_size)
1242 dev_indir_size = ops->get_rxfh_indir_size(dev);
1243 if (ops->get_rxfh_key_size)
1244 dev_key_size = ops->get_rxfh_key_size(dev);
1246 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1249 /* Check that reserved fields are 0 for now */
1250 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1252 /* Most drivers don't handle rss_context, check it's 0 as well */
1253 if (rxfh.rss_context && !ops->set_rxfh_context)
1256 /* If either indir, hash key or function is valid, proceed further.
1257 * Must request at least one change: indir size, hash key or function.
1259 if ((rxfh.indir_size &&
1260 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1261 rxfh.indir_size != dev_indir_size) ||
1262 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1263 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1264 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1267 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1268 indir_bytes = dev_indir_size * sizeof(indir[0]);
1270 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1274 rx_rings.cmd = ETHTOOL_GRXRINGS;
1275 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1279 /* rxfh.indir_size == 0 means reset the indir table to default (master
1280 * context) or delete the context (other RSS contexts).
1281 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1283 if (rxfh.indir_size &&
1284 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1285 indir = (u32 *)rss_config;
1286 ret = ethtool_copy_validate_indir(indir,
1287 useraddr + rss_cfg_offset,
1292 } else if (rxfh.indir_size == 0) {
1293 if (rxfh.rss_context == 0) {
1294 indir = (u32 *)rss_config;
1295 for (i = 0; i < dev_indir_size; i++)
1296 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1302 if (rxfh.key_size) {
1303 hkey = rss_config + indir_bytes;
1304 if (copy_from_user(hkey,
1305 useraddr + rss_cfg_offset + indir_bytes,
1312 if (rxfh.rss_context)
1313 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1314 &rxfh.rss_context, delete);
1316 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1320 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1321 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1324 if (!rxfh.rss_context) {
1325 /* indicate whether rxfh was set to default */
1326 if (rxfh.indir_size == 0)
1327 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1328 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1329 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1337 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1339 struct ethtool_regs regs;
1340 const struct ethtool_ops *ops = dev->ethtool_ops;
1344 if (!ops->get_regs || !ops->get_regs_len)
1347 if (copy_from_user(®s, useraddr, sizeof(regs)))
1350 reglen = ops->get_regs_len(dev);
1354 if (regs.len > reglen)
1357 regbuf = vzalloc(reglen);
1361 ops->get_regs(dev, ®s, regbuf);
1364 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1366 useraddr += offsetof(struct ethtool_regs, data);
1367 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1376 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1378 struct ethtool_value reset;
1381 if (!dev->ethtool_ops->reset)
1384 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1387 ret = dev->ethtool_ops->reset(dev, &reset.data);
1391 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1396 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1398 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1400 if (!dev->ethtool_ops->get_wol)
1403 dev->ethtool_ops->get_wol(dev, &wol);
1405 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1410 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1412 struct ethtool_wolinfo wol;
1415 if (!dev->ethtool_ops->set_wol)
1418 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1421 ret = dev->ethtool_ops->set_wol(dev, &wol);
1425 dev->wol_enabled = !!wol.wolopts;
1430 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1432 struct ethtool_eee edata;
1435 if (!dev->ethtool_ops->get_eee)
1438 memset(&edata, 0, sizeof(struct ethtool_eee));
1439 edata.cmd = ETHTOOL_GEEE;
1440 rc = dev->ethtool_ops->get_eee(dev, &edata);
1445 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1451 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1453 struct ethtool_eee edata;
1455 if (!dev->ethtool_ops->set_eee)
1458 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1461 return dev->ethtool_ops->set_eee(dev, &edata);
1464 static int ethtool_nway_reset(struct net_device *dev)
1466 if (!dev->ethtool_ops->nway_reset)
1469 return dev->ethtool_ops->nway_reset(dev);
1472 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1474 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1476 if (!dev->ethtool_ops->get_link)
1479 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1481 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1486 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1487 int (*getter)(struct net_device *,
1488 struct ethtool_eeprom *, u8 *),
1491 struct ethtool_eeprom eeprom;
1492 void __user *userbuf = useraddr + sizeof(eeprom);
1493 u32 bytes_remaining;
1497 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1500 /* Check for wrap and zero */
1501 if (eeprom.offset + eeprom.len <= eeprom.offset)
1504 /* Check for exceeding total eeprom len */
1505 if (eeprom.offset + eeprom.len > total_len)
1508 data = kmalloc(PAGE_SIZE, GFP_USER);
1512 bytes_remaining = eeprom.len;
1513 while (bytes_remaining > 0) {
1514 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1516 ret = getter(dev, &eeprom, data);
1519 if (copy_to_user(userbuf, data, eeprom.len)) {
1523 userbuf += eeprom.len;
1524 eeprom.offset += eeprom.len;
1525 bytes_remaining -= eeprom.len;
1528 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1529 eeprom.offset -= eeprom.len;
1530 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1537 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1539 const struct ethtool_ops *ops = dev->ethtool_ops;
1541 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1542 !ops->get_eeprom_len(dev))
1545 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1546 ops->get_eeprom_len(dev));
1549 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1551 struct ethtool_eeprom eeprom;
1552 const struct ethtool_ops *ops = dev->ethtool_ops;
1553 void __user *userbuf = useraddr + sizeof(eeprom);
1554 u32 bytes_remaining;
1558 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1559 !ops->get_eeprom_len(dev))
1562 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1565 /* Check for wrap and zero */
1566 if (eeprom.offset + eeprom.len <= eeprom.offset)
1569 /* Check for exceeding total eeprom len */
1570 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1573 data = kmalloc(PAGE_SIZE, GFP_USER);
1577 bytes_remaining = eeprom.len;
1578 while (bytes_remaining > 0) {
1579 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1581 if (copy_from_user(data, userbuf, eeprom.len)) {
1585 ret = ops->set_eeprom(dev, &eeprom, data);
1588 userbuf += eeprom.len;
1589 eeprom.offset += eeprom.len;
1590 bytes_remaining -= eeprom.len;
1597 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1598 void __user *useraddr)
1600 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1602 if (!dev->ethtool_ops->get_coalesce)
1605 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1607 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1612 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1613 void __user *useraddr)
1615 struct ethtool_coalesce coalesce;
1617 if (!dev->ethtool_ops->set_coalesce)
1620 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1623 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1626 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1628 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1630 if (!dev->ethtool_ops->get_ringparam)
1633 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1635 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1640 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1642 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1644 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1647 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1650 dev->ethtool_ops->get_ringparam(dev, &max);
1652 /* ensure new ring parameters are within the maximums */
1653 if (ringparam.rx_pending > max.rx_max_pending ||
1654 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1655 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1656 ringparam.tx_pending > max.tx_max_pending)
1659 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1662 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1663 void __user *useraddr)
1665 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1667 if (!dev->ethtool_ops->get_channels)
1670 dev->ethtool_ops->get_channels(dev, &channels);
1672 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1677 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1678 void __user *useraddr)
1680 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1681 u16 from_channel, to_channel;
1682 u32 max_rx_in_use = 0;
1685 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1688 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1691 dev->ethtool_ops->get_channels(dev, &curr);
1693 /* ensure new counts are within the maximums */
1694 if (channels.rx_count > curr.max_rx ||
1695 channels.tx_count > curr.max_tx ||
1696 channels.combined_count > curr.max_combined ||
1697 channels.other_count > curr.max_other)
1700 /* ensure the new Rx count fits within the configured Rx flow
1701 * indirection table settings */
1702 if (netif_is_rxfh_configured(dev) &&
1703 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1704 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1707 /* Disabling channels, query zero-copy AF_XDP sockets */
1708 from_channel = channels.combined_count +
1709 min(channels.rx_count, channels.tx_count);
1710 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1711 for (i = from_channel; i < to_channel; i++)
1712 if (xdp_get_umem_from_qid(dev, i))
1715 return dev->ethtool_ops->set_channels(dev, &channels);
1718 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1720 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1722 if (!dev->ethtool_ops->get_pauseparam)
1725 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1727 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1732 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1734 struct ethtool_pauseparam pauseparam;
1736 if (!dev->ethtool_ops->set_pauseparam)
1739 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1742 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1745 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1747 struct ethtool_test test;
1748 const struct ethtool_ops *ops = dev->ethtool_ops;
1752 if (!ops->self_test || !ops->get_sset_count)
1755 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1758 WARN_ON(test_len == 0);
1760 if (copy_from_user(&test, useraddr, sizeof(test)))
1763 test.len = test_len;
1764 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1768 ops->self_test(dev, &test, data);
1771 if (copy_to_user(useraddr, &test, sizeof(test)))
1773 useraddr += sizeof(test);
1774 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1783 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1785 struct ethtool_gstrings gstrings;
1789 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1792 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1795 if (ret > S32_MAX / ETH_GSTRING_LEN)
1802 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1806 __ethtool_get_strings(dev, gstrings.string_set, data);
1812 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1814 useraddr += sizeof(gstrings);
1816 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1825 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1827 struct ethtool_value id;
1829 const struct ethtool_ops *ops = dev->ethtool_ops;
1832 if (!ops->set_phys_id)
1838 if (copy_from_user(&id, useraddr, sizeof(id)))
1841 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1845 /* Drop the RTNL lock while waiting, but prevent reentry or
1846 * removal of the device.
1853 /* Driver will handle this itself */
1854 schedule_timeout_interruptible(
1855 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1857 /* Driver expects to be called at twice the frequency in rc */
1858 int n = rc * 2, i, interval = HZ / n;
1860 /* Count down seconds */
1862 /* Count down iterations per second */
1866 rc = ops->set_phys_id(dev,
1867 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1871 schedule_timeout_interruptible(interval);
1872 } while (!signal_pending(current) && --i != 0);
1873 } while (!signal_pending(current) &&
1874 (id.data == 0 || --id.data != 0));
1881 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1885 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1887 struct ethtool_stats stats;
1888 const struct ethtool_ops *ops = dev->ethtool_ops;
1892 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1895 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1898 if (n_stats > S32_MAX / sizeof(u64))
1900 WARN_ON_ONCE(!n_stats);
1901 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1904 stats.n_stats = n_stats;
1907 data = vzalloc(array_size(n_stats, sizeof(u64)));
1910 ops->get_ethtool_stats(dev, &stats, data);
1916 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1918 useraddr += sizeof(stats);
1919 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1928 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1930 const struct ethtool_ops *ops = dev->ethtool_ops;
1931 struct phy_device *phydev = dev->phydev;
1932 struct ethtool_stats stats;
1936 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1939 if (dev->phydev && !ops->get_ethtool_phy_stats)
1940 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1942 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1945 if (n_stats > S32_MAX / sizeof(u64))
1947 WARN_ON_ONCE(!n_stats);
1949 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1952 stats.n_stats = n_stats;
1955 data = vzalloc(array_size(n_stats, sizeof(u64)));
1959 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1960 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1964 ops->get_ethtool_phy_stats(dev, &stats, data);
1971 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1973 useraddr += sizeof(stats);
1974 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1983 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1985 struct ethtool_perm_addr epaddr;
1987 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1990 if (epaddr.size < dev->addr_len)
1992 epaddr.size = dev->addr_len;
1994 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1996 useraddr += sizeof(epaddr);
1997 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2002 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2003 u32 cmd, u32 (*actor)(struct net_device *))
2005 struct ethtool_value edata = { .cmd = cmd };
2010 edata.data = actor(dev);
2012 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2017 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2018 void (*actor)(struct net_device *, u32))
2020 struct ethtool_value edata;
2025 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2028 actor(dev, edata.data);
2032 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2033 int (*actor)(struct net_device *, u32))
2035 struct ethtool_value edata;
2040 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2043 return actor(dev, edata.data);
2046 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2047 char __user *useraddr)
2049 struct ethtool_flash efl;
2051 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2053 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2055 if (!dev->ethtool_ops->flash_device)
2056 return devlink_compat_flash_update(dev, efl.data);
2058 return dev->ethtool_ops->flash_device(dev, &efl);
2061 static int ethtool_set_dump(struct net_device *dev,
2062 void __user *useraddr)
2064 struct ethtool_dump dump;
2066 if (!dev->ethtool_ops->set_dump)
2069 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2072 return dev->ethtool_ops->set_dump(dev, &dump);
2075 static int ethtool_get_dump_flag(struct net_device *dev,
2076 void __user *useraddr)
2079 struct ethtool_dump dump;
2080 const struct ethtool_ops *ops = dev->ethtool_ops;
2082 if (!ops->get_dump_flag)
2085 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2088 ret = ops->get_dump_flag(dev, &dump);
2092 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2097 static int ethtool_get_dump_data(struct net_device *dev,
2098 void __user *useraddr)
2102 struct ethtool_dump dump, tmp;
2103 const struct ethtool_ops *ops = dev->ethtool_ops;
2106 if (!ops->get_dump_data || !ops->get_dump_flag)
2109 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2112 memset(&tmp, 0, sizeof(tmp));
2113 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2114 ret = ops->get_dump_flag(dev, &tmp);
2118 len = min(tmp.len, dump.len);
2122 /* Don't ever let the driver think there's more space available
2123 * than it requested with .get_dump_flag().
2127 /* Always allocate enough space to hold the whole thing so that the
2128 * driver does not need to check the length and bother with partial
2131 data = vzalloc(tmp.len);
2134 ret = ops->get_dump_data(dev, &dump, data);
2138 /* There are two sane possibilities:
2139 * 1. The driver's .get_dump_data() does not touch dump.len.
2140 * 2. Or it may set dump.len to how much it really writes, which
2141 * should be tmp.len (or len if it can do a partial dump).
2142 * In any case respond to userspace with the actual length of data
2145 WARN_ON(dump.len != len && dump.len != tmp.len);
2148 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2152 useraddr += offsetof(struct ethtool_dump, data);
2153 if (copy_to_user(useraddr, data, len))
2160 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2163 struct ethtool_ts_info info;
2164 const struct ethtool_ops *ops = dev->ethtool_ops;
2165 struct phy_device *phydev = dev->phydev;
2167 memset(&info, 0, sizeof(info));
2168 info.cmd = ETHTOOL_GET_TS_INFO;
2170 if (phydev && phydev->drv && phydev->drv->ts_info) {
2171 err = phydev->drv->ts_info(phydev, &info);
2172 } else if (ops->get_ts_info) {
2173 err = ops->get_ts_info(dev, &info);
2175 info.so_timestamping =
2176 SOF_TIMESTAMPING_RX_SOFTWARE |
2177 SOF_TIMESTAMPING_SOFTWARE;
2178 info.phc_index = -1;
2184 if (copy_to_user(useraddr, &info, sizeof(info)))
2190 static int __ethtool_get_module_info(struct net_device *dev,
2191 struct ethtool_modinfo *modinfo)
2193 const struct ethtool_ops *ops = dev->ethtool_ops;
2194 struct phy_device *phydev = dev->phydev;
2197 return sfp_get_module_info(dev->sfp_bus, modinfo);
2199 if (phydev && phydev->drv && phydev->drv->module_info)
2200 return phydev->drv->module_info(phydev, modinfo);
2202 if (ops->get_module_info)
2203 return ops->get_module_info(dev, modinfo);
2208 static int ethtool_get_module_info(struct net_device *dev,
2209 void __user *useraddr)
2212 struct ethtool_modinfo modinfo;
2214 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2217 ret = __ethtool_get_module_info(dev, &modinfo);
2221 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2227 static int __ethtool_get_module_eeprom(struct net_device *dev,
2228 struct ethtool_eeprom *ee, u8 *data)
2230 const struct ethtool_ops *ops = dev->ethtool_ops;
2231 struct phy_device *phydev = dev->phydev;
2234 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2236 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2237 return phydev->drv->module_eeprom(phydev, ee, data);
2239 if (ops->get_module_eeprom)
2240 return ops->get_module_eeprom(dev, ee, data);
2245 static int ethtool_get_module_eeprom(struct net_device *dev,
2246 void __user *useraddr)
2249 struct ethtool_modinfo modinfo;
2251 ret = __ethtool_get_module_info(dev, &modinfo);
2255 return ethtool_get_any_eeprom(dev, useraddr,
2256 __ethtool_get_module_eeprom,
2257 modinfo.eeprom_len);
2260 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2263 case ETHTOOL_RX_COPYBREAK:
2264 case ETHTOOL_TX_COPYBREAK:
2265 if (tuna->len != sizeof(u32) ||
2266 tuna->type_id != ETHTOOL_TUNABLE_U32)
2269 case ETHTOOL_PFC_PREVENTION_TOUT:
2270 if (tuna->len != sizeof(u16) ||
2271 tuna->type_id != ETHTOOL_TUNABLE_U16)
2281 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2284 struct ethtool_tunable tuna;
2285 const struct ethtool_ops *ops = dev->ethtool_ops;
2288 if (!ops->get_tunable)
2290 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2292 ret = ethtool_tunable_valid(&tuna);
2295 data = kmalloc(tuna.len, GFP_USER);
2298 ret = ops->get_tunable(dev, &tuna, data);
2301 useraddr += sizeof(tuna);
2303 if (copy_to_user(useraddr, data, tuna.len))
2312 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2315 struct ethtool_tunable tuna;
2316 const struct ethtool_ops *ops = dev->ethtool_ops;
2319 if (!ops->set_tunable)
2321 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2323 ret = ethtool_tunable_valid(&tuna);
2326 useraddr += sizeof(tuna);
2327 data = memdup_user(useraddr, tuna.len);
2329 return PTR_ERR(data);
2330 ret = ops->set_tunable(dev, &tuna, data);
2336 static noinline_for_stack int
2337 ethtool_get_per_queue_coalesce(struct net_device *dev,
2338 void __user *useraddr,
2339 struct ethtool_per_queue_op *per_queue_opt)
2343 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2345 if (!dev->ethtool_ops->get_per_queue_coalesce)
2348 useraddr += sizeof(*per_queue_opt);
2350 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2353 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2354 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2356 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2359 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2361 useraddr += sizeof(coalesce);
2367 static noinline_for_stack int
2368 ethtool_set_per_queue_coalesce(struct net_device *dev,
2369 void __user *useraddr,
2370 struct ethtool_per_queue_op *per_queue_opt)
2375 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2376 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2378 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2379 (!dev->ethtool_ops->get_per_queue_coalesce))
2382 useraddr += sizeof(*per_queue_opt);
2384 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2385 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2386 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2390 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2391 struct ethtool_coalesce coalesce;
2393 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2399 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2404 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2408 useraddr += sizeof(coalesce);
2414 for_each_set_bit(i, queue_mask, bit) {
2415 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2424 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2425 void __user *useraddr, u32 sub_cmd)
2427 struct ethtool_per_queue_op per_queue_opt;
2429 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2432 if (per_queue_opt.sub_command != sub_cmd)
2435 switch (per_queue_opt.sub_command) {
2436 case ETHTOOL_GCOALESCE:
2437 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2438 case ETHTOOL_SCOALESCE:
2439 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2445 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2448 case ETHTOOL_PHY_DOWNSHIFT:
2449 if (tuna->len != sizeof(u8) ||
2450 tuna->type_id != ETHTOOL_TUNABLE_U8)
2460 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2463 struct ethtool_tunable tuna;
2464 struct phy_device *phydev = dev->phydev;
2467 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2470 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2472 ret = ethtool_phy_tunable_valid(&tuna);
2475 data = kmalloc(tuna.len, GFP_USER);
2478 mutex_lock(&phydev->lock);
2479 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2480 mutex_unlock(&phydev->lock);
2483 useraddr += sizeof(tuna);
2485 if (copy_to_user(useraddr, data, tuna.len))
2494 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2497 struct ethtool_tunable tuna;
2498 struct phy_device *phydev = dev->phydev;
2501 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2503 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2505 ret = ethtool_phy_tunable_valid(&tuna);
2508 useraddr += sizeof(tuna);
2509 data = memdup_user(useraddr, tuna.len);
2511 return PTR_ERR(data);
2512 mutex_lock(&phydev->lock);
2513 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2514 mutex_unlock(&phydev->lock);
2520 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2522 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2525 if (!dev->ethtool_ops->get_fecparam)
2528 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2532 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2537 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2539 struct ethtool_fecparam fecparam;
2541 if (!dev->ethtool_ops->set_fecparam)
2544 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2547 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2550 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2552 int dev_ethtool(struct net *net, struct ifreq *ifr)
2554 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2555 void __user *useraddr = ifr->ifr_data;
2556 u32 ethcmd, sub_cmd;
2558 netdev_features_t old_features;
2560 if (!dev || !netif_device_present(dev))
2563 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2566 if (ethcmd == ETHTOOL_PERQUEUE) {
2567 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2572 /* Allow some commands to be done by anyone */
2575 case ETHTOOL_GDRVINFO:
2576 case ETHTOOL_GMSGLVL:
2578 case ETHTOOL_GCOALESCE:
2579 case ETHTOOL_GRINGPARAM:
2580 case ETHTOOL_GPAUSEPARAM:
2581 case ETHTOOL_GRXCSUM:
2582 case ETHTOOL_GTXCSUM:
2584 case ETHTOOL_GSSET_INFO:
2585 case ETHTOOL_GSTRINGS:
2586 case ETHTOOL_GSTATS:
2587 case ETHTOOL_GPHYSTATS:
2589 case ETHTOOL_GPERMADDR:
2593 case ETHTOOL_GFLAGS:
2594 case ETHTOOL_GPFLAGS:
2596 case ETHTOOL_GRXRINGS:
2597 case ETHTOOL_GRXCLSRLCNT:
2598 case ETHTOOL_GRXCLSRULE:
2599 case ETHTOOL_GRXCLSRLALL:
2600 case ETHTOOL_GRXFHINDIR:
2602 case ETHTOOL_GFEATURES:
2603 case ETHTOOL_GCHANNELS:
2604 case ETHTOOL_GET_TS_INFO:
2606 case ETHTOOL_GTUNABLE:
2607 case ETHTOOL_PHY_GTUNABLE:
2608 case ETHTOOL_GLINKSETTINGS:
2609 case ETHTOOL_GFECPARAM:
2612 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2616 if (dev->ethtool_ops->begin) {
2617 rc = dev->ethtool_ops->begin(dev);
2621 old_features = dev->features;
2625 rc = ethtool_get_settings(dev, useraddr);
2628 rc = ethtool_set_settings(dev, useraddr);
2630 case ETHTOOL_GDRVINFO:
2631 rc = ethtool_get_drvinfo(dev, useraddr);
2634 rc = ethtool_get_regs(dev, useraddr);
2637 rc = ethtool_get_wol(dev, useraddr);
2640 rc = ethtool_set_wol(dev, useraddr);
2642 case ETHTOOL_GMSGLVL:
2643 rc = ethtool_get_value(dev, useraddr, ethcmd,
2644 dev->ethtool_ops->get_msglevel);
2646 case ETHTOOL_SMSGLVL:
2647 rc = ethtool_set_value_void(dev, useraddr,
2648 dev->ethtool_ops->set_msglevel);
2651 rc = ethtool_get_eee(dev, useraddr);
2654 rc = ethtool_set_eee(dev, useraddr);
2656 case ETHTOOL_NWAY_RST:
2657 rc = ethtool_nway_reset(dev);
2660 rc = ethtool_get_link(dev, useraddr);
2662 case ETHTOOL_GEEPROM:
2663 rc = ethtool_get_eeprom(dev, useraddr);
2665 case ETHTOOL_SEEPROM:
2666 rc = ethtool_set_eeprom(dev, useraddr);
2668 case ETHTOOL_GCOALESCE:
2669 rc = ethtool_get_coalesce(dev, useraddr);
2671 case ETHTOOL_SCOALESCE:
2672 rc = ethtool_set_coalesce(dev, useraddr);
2674 case ETHTOOL_GRINGPARAM:
2675 rc = ethtool_get_ringparam(dev, useraddr);
2677 case ETHTOOL_SRINGPARAM:
2678 rc = ethtool_set_ringparam(dev, useraddr);
2680 case ETHTOOL_GPAUSEPARAM:
2681 rc = ethtool_get_pauseparam(dev, useraddr);
2683 case ETHTOOL_SPAUSEPARAM:
2684 rc = ethtool_set_pauseparam(dev, useraddr);
2687 rc = ethtool_self_test(dev, useraddr);
2689 case ETHTOOL_GSTRINGS:
2690 rc = ethtool_get_strings(dev, useraddr);
2692 case ETHTOOL_PHYS_ID:
2693 rc = ethtool_phys_id(dev, useraddr);
2695 case ETHTOOL_GSTATS:
2696 rc = ethtool_get_stats(dev, useraddr);
2698 case ETHTOOL_GPERMADDR:
2699 rc = ethtool_get_perm_addr(dev, useraddr);
2701 case ETHTOOL_GFLAGS:
2702 rc = ethtool_get_value(dev, useraddr, ethcmd,
2703 __ethtool_get_flags);
2705 case ETHTOOL_SFLAGS:
2706 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2708 case ETHTOOL_GPFLAGS:
2709 rc = ethtool_get_value(dev, useraddr, ethcmd,
2710 dev->ethtool_ops->get_priv_flags);
2712 case ETHTOOL_SPFLAGS:
2713 rc = ethtool_set_value(dev, useraddr,
2714 dev->ethtool_ops->set_priv_flags);
2717 case ETHTOOL_GRXRINGS:
2718 case ETHTOOL_GRXCLSRLCNT:
2719 case ETHTOOL_GRXCLSRULE:
2720 case ETHTOOL_GRXCLSRLALL:
2721 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2724 case ETHTOOL_SRXCLSRLDEL:
2725 case ETHTOOL_SRXCLSRLINS:
2726 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2728 case ETHTOOL_FLASHDEV:
2729 rc = ethtool_flash_device(dev, useraddr);
2732 rc = ethtool_reset(dev, useraddr);
2734 case ETHTOOL_GSSET_INFO:
2735 rc = ethtool_get_sset_info(dev, useraddr);
2737 case ETHTOOL_GRXFHINDIR:
2738 rc = ethtool_get_rxfh_indir(dev, useraddr);
2740 case ETHTOOL_SRXFHINDIR:
2741 rc = ethtool_set_rxfh_indir(dev, useraddr);
2744 rc = ethtool_get_rxfh(dev, useraddr);
2747 rc = ethtool_set_rxfh(dev, useraddr);
2749 case ETHTOOL_GFEATURES:
2750 rc = ethtool_get_features(dev, useraddr);
2752 case ETHTOOL_SFEATURES:
2753 rc = ethtool_set_features(dev, useraddr);
2755 case ETHTOOL_GTXCSUM:
2756 case ETHTOOL_GRXCSUM:
2761 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2763 case ETHTOOL_STXCSUM:
2764 case ETHTOOL_SRXCSUM:
2769 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2771 case ETHTOOL_GCHANNELS:
2772 rc = ethtool_get_channels(dev, useraddr);
2774 case ETHTOOL_SCHANNELS:
2775 rc = ethtool_set_channels(dev, useraddr);
2777 case ETHTOOL_SET_DUMP:
2778 rc = ethtool_set_dump(dev, useraddr);
2780 case ETHTOOL_GET_DUMP_FLAG:
2781 rc = ethtool_get_dump_flag(dev, useraddr);
2783 case ETHTOOL_GET_DUMP_DATA:
2784 rc = ethtool_get_dump_data(dev, useraddr);
2786 case ETHTOOL_GET_TS_INFO:
2787 rc = ethtool_get_ts_info(dev, useraddr);
2789 case ETHTOOL_GMODULEINFO:
2790 rc = ethtool_get_module_info(dev, useraddr);
2792 case ETHTOOL_GMODULEEEPROM:
2793 rc = ethtool_get_module_eeprom(dev, useraddr);
2795 case ETHTOOL_GTUNABLE:
2796 rc = ethtool_get_tunable(dev, useraddr);
2798 case ETHTOOL_STUNABLE:
2799 rc = ethtool_set_tunable(dev, useraddr);
2801 case ETHTOOL_GPHYSTATS:
2802 rc = ethtool_get_phy_stats(dev, useraddr);
2804 case ETHTOOL_PERQUEUE:
2805 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2807 case ETHTOOL_GLINKSETTINGS:
2808 rc = ethtool_get_link_ksettings(dev, useraddr);
2810 case ETHTOOL_SLINKSETTINGS:
2811 rc = ethtool_set_link_ksettings(dev, useraddr);
2813 case ETHTOOL_PHY_GTUNABLE:
2814 rc = get_phy_tunable(dev, useraddr);
2816 case ETHTOOL_PHY_STUNABLE:
2817 rc = set_phy_tunable(dev, useraddr);
2819 case ETHTOOL_GFECPARAM:
2820 rc = ethtool_get_fecparam(dev, useraddr);
2822 case ETHTOOL_SFECPARAM:
2823 rc = ethtool_set_fecparam(dev, useraddr);
2829 if (dev->ethtool_ops->complete)
2830 dev->ethtool_ops->complete(dev);
2832 if (old_features != dev->features)
2833 netdev_features_change(dev);
2838 struct ethtool_rx_flow_key {
2839 struct flow_dissector_key_basic basic;
2841 struct flow_dissector_key_ipv4_addrs ipv4;
2842 struct flow_dissector_key_ipv6_addrs ipv6;
2844 struct flow_dissector_key_ports tp;
2845 struct flow_dissector_key_ip ip;
2846 struct flow_dissector_key_vlan vlan;
2847 struct flow_dissector_key_eth_addrs eth_addrs;
2848 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2850 struct ethtool_rx_flow_match {
2851 struct flow_dissector dissector;
2852 struct ethtool_rx_flow_key key;
2853 struct ethtool_rx_flow_key mask;
2856 struct ethtool_rx_flow_rule *
2857 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2859 const struct ethtool_rx_flow_spec *fs = input->fs;
2860 static struct in6_addr zero_addr = {};
2861 struct ethtool_rx_flow_match *match;
2862 struct ethtool_rx_flow_rule *flow;
2863 struct flow_action_entry *act;
2865 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2866 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2868 return ERR_PTR(-ENOMEM);
2870 /* ethtool_rx supports only one single action per rule. */
2871 flow->rule = flow_rule_alloc(1);
2874 return ERR_PTR(-ENOMEM);
2877 match = (struct ethtool_rx_flow_match *)flow->priv;
2878 flow->rule->match.dissector = &match->dissector;
2879 flow->rule->match.mask = &match->mask;
2880 flow->rule->match.key = &match->key;
2882 match->mask.basic.n_proto = htons(0xffff);
2884 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2887 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2889 match->key.basic.n_proto = htons(ETH_P_IP);
2891 v4_spec = &fs->h_u.tcp_ip4_spec;
2892 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2894 if (v4_m_spec->ip4src) {
2895 match->key.ipv4.src = v4_spec->ip4src;
2896 match->mask.ipv4.src = v4_m_spec->ip4src;
2898 if (v4_m_spec->ip4dst) {
2899 match->key.ipv4.dst = v4_spec->ip4dst;
2900 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2902 if (v4_m_spec->ip4src ||
2903 v4_m_spec->ip4dst) {
2904 match->dissector.used_keys |=
2905 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2906 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2907 offsetof(struct ethtool_rx_flow_key, ipv4);
2909 if (v4_m_spec->psrc) {
2910 match->key.tp.src = v4_spec->psrc;
2911 match->mask.tp.src = v4_m_spec->psrc;
2913 if (v4_m_spec->pdst) {
2914 match->key.tp.dst = v4_spec->pdst;
2915 match->mask.tp.dst = v4_m_spec->pdst;
2917 if (v4_m_spec->psrc ||
2919 match->dissector.used_keys |=
2920 BIT(FLOW_DISSECTOR_KEY_PORTS);
2921 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2922 offsetof(struct ethtool_rx_flow_key, tp);
2924 if (v4_m_spec->tos) {
2925 match->key.ip.tos = v4_spec->tos;
2926 match->mask.ip.tos = v4_m_spec->tos;
2927 match->dissector.used_keys |=
2928 BIT(FLOW_DISSECTOR_KEY_IP);
2929 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2930 offsetof(struct ethtool_rx_flow_key, ip);
2936 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2938 match->key.basic.n_proto = htons(ETH_P_IPV6);
2940 v6_spec = &fs->h_u.tcp_ip6_spec;
2941 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2942 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2943 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2944 sizeof(match->key.ipv6.src));
2945 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2946 sizeof(match->mask.ipv6.src));
2948 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2949 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2950 sizeof(match->key.ipv6.dst));
2951 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2952 sizeof(match->mask.ipv6.dst));
2954 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2955 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2956 match->dissector.used_keys |=
2957 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2958 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2959 offsetof(struct ethtool_rx_flow_key, ipv6);
2961 if (v6_m_spec->psrc) {
2962 match->key.tp.src = v6_spec->psrc;
2963 match->mask.tp.src = v6_m_spec->psrc;
2965 if (v6_m_spec->pdst) {
2966 match->key.tp.dst = v6_spec->pdst;
2967 match->mask.tp.dst = v6_m_spec->pdst;
2969 if (v6_m_spec->psrc ||
2971 match->dissector.used_keys |=
2972 BIT(FLOW_DISSECTOR_KEY_PORTS);
2973 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2974 offsetof(struct ethtool_rx_flow_key, tp);
2976 if (v6_m_spec->tclass) {
2977 match->key.ip.tos = v6_spec->tclass;
2978 match->mask.ip.tos = v6_m_spec->tclass;
2979 match->dissector.used_keys |=
2980 BIT(FLOW_DISSECTOR_KEY_IP);
2981 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2982 offsetof(struct ethtool_rx_flow_key, ip);
2987 ethtool_rx_flow_rule_destroy(flow);
2988 return ERR_PTR(-EINVAL);
2991 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2994 match->key.basic.ip_proto = IPPROTO_TCP;
2998 match->key.basic.ip_proto = IPPROTO_UDP;
3001 match->mask.basic.ip_proto = 0xff;
3003 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3004 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3005 offsetof(struct ethtool_rx_flow_key, basic);
3007 if (fs->flow_type & FLOW_EXT) {
3008 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3009 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3011 if (ext_m_spec->vlan_etype &&
3012 ext_m_spec->vlan_tci) {
3013 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3014 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3016 match->key.vlan.vlan_id =
3017 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3018 match->mask.vlan.vlan_id =
3019 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3021 match->key.vlan.vlan_priority =
3022 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3023 match->mask.vlan.vlan_priority =
3024 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3026 match->dissector.used_keys |=
3027 BIT(FLOW_DISSECTOR_KEY_VLAN);
3028 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3029 offsetof(struct ethtool_rx_flow_key, vlan);
3032 if (fs->flow_type & FLOW_MAC_EXT) {
3033 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3034 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3036 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3038 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3041 match->dissector.used_keys |=
3042 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3043 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3044 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3047 act = &flow->rule->action.entries[0];
3048 switch (fs->ring_cookie) {
3049 case RX_CLS_FLOW_DISC:
3050 act->id = FLOW_ACTION_DROP;
3052 case RX_CLS_FLOW_WAKE:
3053 act->id = FLOW_ACTION_WAKE;
3056 act->id = FLOW_ACTION_QUEUE;
3057 if (fs->flow_type & FLOW_RSS)
3058 act->queue.ctx = input->rss_ctx;
3060 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3061 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3067 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3069 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3074 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);