2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
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
)
1800 data
= vzalloc(array_size(gstrings
.len
, ETH_GSTRING_LEN
));
1801 if (gstrings
.len
&& !data
)
1804 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1807 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1809 useraddr
+= sizeof(gstrings
);
1811 copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1820 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1822 struct ethtool_value id
;
1824 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1827 if (!ops
->set_phys_id
)
1833 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1836 rc
= ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1840 /* Drop the RTNL lock while waiting, but prevent reentry or
1841 * removal of the device.
1848 /* Driver will handle this itself */
1849 schedule_timeout_interruptible(
1850 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1852 /* Driver expects to be called at twice the frequency in rc */
1853 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1855 /* Count down seconds */
1857 /* Count down iterations per second */
1861 rc
= ops
->set_phys_id(dev
,
1862 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1866 schedule_timeout_interruptible(interval
);
1867 } while (!signal_pending(current
) && --i
!= 0);
1868 } while (!signal_pending(current
) &&
1869 (id
.data
== 0 || --id
.data
!= 0));
1876 (void) ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1880 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1882 struct ethtool_stats stats
;
1883 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1887 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1890 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1893 if (n_stats
> S32_MAX
/ sizeof(u64
))
1895 WARN_ON_ONCE(!n_stats
);
1896 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1899 stats
.n_stats
= n_stats
;
1900 data
= vzalloc(array_size(n_stats
, sizeof(u64
)));
1901 if (n_stats
&& !data
)
1904 ops
->get_ethtool_stats(dev
, &stats
, data
);
1907 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1909 useraddr
+= sizeof(stats
);
1910 if (n_stats
&& copy_to_user(useraddr
, data
, n_stats
* sizeof(u64
)))
1919 static int ethtool_get_phy_stats(struct net_device
*dev
, void __user
*useraddr
)
1921 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1922 struct phy_device
*phydev
= dev
->phydev
;
1923 struct ethtool_stats stats
;
1927 if (!phydev
&& (!ops
->get_ethtool_phy_stats
|| !ops
->get_sset_count
))
1930 if (dev
->phydev
&& !ops
->get_ethtool_phy_stats
)
1931 n_stats
= phy_ethtool_get_sset_count(dev
->phydev
);
1933 n_stats
= ops
->get_sset_count(dev
, ETH_SS_PHY_STATS
);
1936 if (n_stats
> S32_MAX
/ sizeof(u64
))
1938 WARN_ON_ONCE(!n_stats
);
1940 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1943 stats
.n_stats
= n_stats
;
1944 data
= vzalloc(array_size(n_stats
, sizeof(u64
)));
1945 if (n_stats
&& !data
)
1948 if (dev
->phydev
&& !ops
->get_ethtool_phy_stats
) {
1949 ret
= phy_ethtool_get_stats(dev
->phydev
, &stats
, data
);
1953 ops
->get_ethtool_phy_stats(dev
, &stats
, data
);
1957 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1959 useraddr
+= sizeof(stats
);
1960 if (n_stats
&& copy_to_user(useraddr
, data
, n_stats
* sizeof(u64
)))
1969 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1971 struct ethtool_perm_addr epaddr
;
1973 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1976 if (epaddr
.size
< dev
->addr_len
)
1978 epaddr
.size
= dev
->addr_len
;
1980 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1982 useraddr
+= sizeof(epaddr
);
1983 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1988 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1989 u32 cmd
, u32 (*actor
)(struct net_device
*))
1991 struct ethtool_value edata
= { .cmd
= cmd
};
1996 edata
.data
= actor(dev
);
1998 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
2003 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
2004 void (*actor
)(struct net_device
*, u32
))
2006 struct ethtool_value edata
;
2011 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2014 actor(dev
, edata
.data
);
2018 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
2019 int (*actor
)(struct net_device
*, u32
))
2021 struct ethtool_value edata
;
2026 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2029 return actor(dev
, edata
.data
);
2032 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
2033 char __user
*useraddr
)
2035 struct ethtool_flash efl
;
2037 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
2039 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
2041 if (!dev
->ethtool_ops
->flash_device
)
2042 return devlink_compat_flash_update(dev
, efl
.data
);
2044 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
2047 static int ethtool_set_dump(struct net_device
*dev
,
2048 void __user
*useraddr
)
2050 struct ethtool_dump dump
;
2052 if (!dev
->ethtool_ops
->set_dump
)
2055 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2058 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
2061 static int ethtool_get_dump_flag(struct net_device
*dev
,
2062 void __user
*useraddr
)
2065 struct ethtool_dump dump
;
2066 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2068 if (!ops
->get_dump_flag
)
2071 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2074 ret
= ops
->get_dump_flag(dev
, &dump
);
2078 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
2083 static int ethtool_get_dump_data(struct net_device
*dev
,
2084 void __user
*useraddr
)
2088 struct ethtool_dump dump
, tmp
;
2089 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2092 if (!ops
->get_dump_data
|| !ops
->get_dump_flag
)
2095 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2098 memset(&tmp
, 0, sizeof(tmp
));
2099 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
2100 ret
= ops
->get_dump_flag(dev
, &tmp
);
2104 len
= min(tmp
.len
, dump
.len
);
2108 /* Don't ever let the driver think there's more space available
2109 * than it requested with .get_dump_flag().
2113 /* Always allocate enough space to hold the whole thing so that the
2114 * driver does not need to check the length and bother with partial
2117 data
= vzalloc(tmp
.len
);
2120 ret
= ops
->get_dump_data(dev
, &dump
, data
);
2124 /* There are two sane possibilities:
2125 * 1. The driver's .get_dump_data() does not touch dump.len.
2126 * 2. Or it may set dump.len to how much it really writes, which
2127 * should be tmp.len (or len if it can do a partial dump).
2128 * In any case respond to userspace with the actual length of data
2131 WARN_ON(dump
.len
!= len
&& dump
.len
!= tmp
.len
);
2134 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
2138 useraddr
+= offsetof(struct ethtool_dump
, data
);
2139 if (copy_to_user(useraddr
, data
, len
))
2146 static int ethtool_get_ts_info(struct net_device
*dev
, void __user
*useraddr
)
2149 struct ethtool_ts_info info
;
2150 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2151 struct phy_device
*phydev
= dev
->phydev
;
2153 memset(&info
, 0, sizeof(info
));
2154 info
.cmd
= ETHTOOL_GET_TS_INFO
;
2156 if (phydev
&& phydev
->drv
&& phydev
->drv
->ts_info
) {
2157 err
= phydev
->drv
->ts_info(phydev
, &info
);
2158 } else if (ops
->get_ts_info
) {
2159 err
= ops
->get_ts_info(dev
, &info
);
2161 info
.so_timestamping
=
2162 SOF_TIMESTAMPING_RX_SOFTWARE
|
2163 SOF_TIMESTAMPING_SOFTWARE
;
2164 info
.phc_index
= -1;
2170 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
2176 static int __ethtool_get_module_info(struct net_device
*dev
,
2177 struct ethtool_modinfo
*modinfo
)
2179 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2180 struct phy_device
*phydev
= dev
->phydev
;
2183 return sfp_get_module_info(dev
->sfp_bus
, modinfo
);
2185 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_info
)
2186 return phydev
->drv
->module_info(phydev
, modinfo
);
2188 if (ops
->get_module_info
)
2189 return ops
->get_module_info(dev
, modinfo
);
2194 static int ethtool_get_module_info(struct net_device
*dev
,
2195 void __user
*useraddr
)
2198 struct ethtool_modinfo modinfo
;
2200 if (copy_from_user(&modinfo
, useraddr
, sizeof(modinfo
)))
2203 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2207 if (copy_to_user(useraddr
, &modinfo
, sizeof(modinfo
)))
2213 static int __ethtool_get_module_eeprom(struct net_device
*dev
,
2214 struct ethtool_eeprom
*ee
, u8
*data
)
2216 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2217 struct phy_device
*phydev
= dev
->phydev
;
2220 return sfp_get_module_eeprom(dev
->sfp_bus
, ee
, data
);
2222 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_eeprom
)
2223 return phydev
->drv
->module_eeprom(phydev
, ee
, data
);
2225 if (ops
->get_module_eeprom
)
2226 return ops
->get_module_eeprom(dev
, ee
, data
);
2231 static int ethtool_get_module_eeprom(struct net_device
*dev
,
2232 void __user
*useraddr
)
2235 struct ethtool_modinfo modinfo
;
2237 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2241 return ethtool_get_any_eeprom(dev
, useraddr
,
2242 __ethtool_get_module_eeprom
,
2243 modinfo
.eeprom_len
);
2246 static int ethtool_tunable_valid(const struct ethtool_tunable
*tuna
)
2249 case ETHTOOL_RX_COPYBREAK
:
2250 case ETHTOOL_TX_COPYBREAK
:
2251 if (tuna
->len
!= sizeof(u32
) ||
2252 tuna
->type_id
!= ETHTOOL_TUNABLE_U32
)
2255 case ETHTOOL_PFC_PREVENTION_TOUT
:
2256 if (tuna
->len
!= sizeof(u16
) ||
2257 tuna
->type_id
!= ETHTOOL_TUNABLE_U16
)
2267 static int ethtool_get_tunable(struct net_device
*dev
, void __user
*useraddr
)
2270 struct ethtool_tunable tuna
;
2271 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2274 if (!ops
->get_tunable
)
2276 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2278 ret
= ethtool_tunable_valid(&tuna
);
2281 data
= kmalloc(tuna
.len
, GFP_USER
);
2284 ret
= ops
->get_tunable(dev
, &tuna
, data
);
2287 useraddr
+= sizeof(tuna
);
2289 if (copy_to_user(useraddr
, data
, tuna
.len
))
2298 static int ethtool_set_tunable(struct net_device
*dev
, void __user
*useraddr
)
2301 struct ethtool_tunable tuna
;
2302 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2305 if (!ops
->set_tunable
)
2307 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2309 ret
= ethtool_tunable_valid(&tuna
);
2312 useraddr
+= sizeof(tuna
);
2313 data
= memdup_user(useraddr
, tuna
.len
);
2315 return PTR_ERR(data
);
2316 ret
= ops
->set_tunable(dev
, &tuna
, data
);
2322 static int ethtool_get_per_queue_coalesce(struct net_device
*dev
,
2323 void __user
*useraddr
,
2324 struct ethtool_per_queue_op
*per_queue_opt
)
2328 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2330 if (!dev
->ethtool_ops
->get_per_queue_coalesce
)
2333 useraddr
+= sizeof(*per_queue_opt
);
2335 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
,
2338 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2339 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
2341 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, &coalesce
);
2344 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
2346 useraddr
+= sizeof(coalesce
);
2352 static int ethtool_set_per_queue_coalesce(struct net_device
*dev
,
2353 void __user
*useraddr
,
2354 struct ethtool_per_queue_op
*per_queue_opt
)
2359 struct ethtool_coalesce
*backup
= NULL
, *tmp
= NULL
;
2360 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2362 if ((!dev
->ethtool_ops
->set_per_queue_coalesce
) ||
2363 (!dev
->ethtool_ops
->get_per_queue_coalesce
))
2366 useraddr
+= sizeof(*per_queue_opt
);
2368 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
, MAX_NUM_QUEUE
);
2369 n_queue
= bitmap_weight(queue_mask
, MAX_NUM_QUEUE
);
2370 tmp
= backup
= kmalloc_array(n_queue
, sizeof(*backup
), GFP_KERNEL
);
2374 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2375 struct ethtool_coalesce coalesce
;
2377 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, tmp
);
2383 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
))) {
2388 ret
= dev
->ethtool_ops
->set_per_queue_coalesce(dev
, bit
, &coalesce
);
2392 useraddr
+= sizeof(coalesce
);
2398 for_each_set_bit(i
, queue_mask
, bit
) {
2399 dev
->ethtool_ops
->set_per_queue_coalesce(dev
, i
, tmp
);
2408 static int ethtool_set_per_queue(struct net_device
*dev
,
2409 void __user
*useraddr
, u32 sub_cmd
)
2411 struct ethtool_per_queue_op per_queue_opt
;
2413 if (copy_from_user(&per_queue_opt
, useraddr
, sizeof(per_queue_opt
)))
2416 if (per_queue_opt
.sub_command
!= sub_cmd
)
2419 switch (per_queue_opt
.sub_command
) {
2420 case ETHTOOL_GCOALESCE
:
2421 return ethtool_get_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2422 case ETHTOOL_SCOALESCE
:
2423 return ethtool_set_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2429 static int ethtool_phy_tunable_valid(const struct ethtool_tunable
*tuna
)
2432 case ETHTOOL_PHY_DOWNSHIFT
:
2433 if (tuna
->len
!= sizeof(u8
) ||
2434 tuna
->type_id
!= ETHTOOL_TUNABLE_U8
)
2444 static int get_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2447 struct ethtool_tunable tuna
;
2448 struct phy_device
*phydev
= dev
->phydev
;
2451 if (!(phydev
&& phydev
->drv
&& phydev
->drv
->get_tunable
))
2454 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2456 ret
= ethtool_phy_tunable_valid(&tuna
);
2459 data
= kmalloc(tuna
.len
, GFP_USER
);
2462 mutex_lock(&phydev
->lock
);
2463 ret
= phydev
->drv
->get_tunable(phydev
, &tuna
, data
);
2464 mutex_unlock(&phydev
->lock
);
2467 useraddr
+= sizeof(tuna
);
2469 if (copy_to_user(useraddr
, data
, tuna
.len
))
2478 static int set_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2481 struct ethtool_tunable tuna
;
2482 struct phy_device
*phydev
= dev
->phydev
;
2485 if (!(phydev
&& phydev
->drv
&& phydev
->drv
->set_tunable
))
2487 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2489 ret
= ethtool_phy_tunable_valid(&tuna
);
2492 useraddr
+= sizeof(tuna
);
2493 data
= memdup_user(useraddr
, tuna
.len
);
2495 return PTR_ERR(data
);
2496 mutex_lock(&phydev
->lock
);
2497 ret
= phydev
->drv
->set_tunable(phydev
, &tuna
, data
);
2498 mutex_unlock(&phydev
->lock
);
2504 static int ethtool_get_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2506 struct ethtool_fecparam fecparam
= { .cmd
= ETHTOOL_GFECPARAM
};
2509 if (!dev
->ethtool_ops
->get_fecparam
)
2512 rc
= dev
->ethtool_ops
->get_fecparam(dev
, &fecparam
);
2516 if (copy_to_user(useraddr
, &fecparam
, sizeof(fecparam
)))
2521 static int ethtool_set_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2523 struct ethtool_fecparam fecparam
;
2525 if (!dev
->ethtool_ops
->set_fecparam
)
2528 if (copy_from_user(&fecparam
, useraddr
, sizeof(fecparam
)))
2531 return dev
->ethtool_ops
->set_fecparam(dev
, &fecparam
);
2534 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2536 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
2538 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
2539 void __user
*useraddr
= ifr
->ifr_data
;
2540 u32 ethcmd
, sub_cmd
;
2542 netdev_features_t old_features
;
2544 if (!dev
|| !netif_device_present(dev
))
2547 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
2550 if (ethcmd
== ETHTOOL_PERQUEUE
) {
2551 if (copy_from_user(&sub_cmd
, useraddr
+ sizeof(ethcmd
), sizeof(sub_cmd
)))
2556 /* Allow some commands to be done by anyone */
2559 case ETHTOOL_GDRVINFO
:
2560 case ETHTOOL_GMSGLVL
:
2562 case ETHTOOL_GCOALESCE
:
2563 case ETHTOOL_GRINGPARAM
:
2564 case ETHTOOL_GPAUSEPARAM
:
2565 case ETHTOOL_GRXCSUM
:
2566 case ETHTOOL_GTXCSUM
:
2568 case ETHTOOL_GSSET_INFO
:
2569 case ETHTOOL_GSTRINGS
:
2570 case ETHTOOL_GSTATS
:
2571 case ETHTOOL_GPHYSTATS
:
2573 case ETHTOOL_GPERMADDR
:
2577 case ETHTOOL_GFLAGS
:
2578 case ETHTOOL_GPFLAGS
:
2580 case ETHTOOL_GRXRINGS
:
2581 case ETHTOOL_GRXCLSRLCNT
:
2582 case ETHTOOL_GRXCLSRULE
:
2583 case ETHTOOL_GRXCLSRLALL
:
2584 case ETHTOOL_GRXFHINDIR
:
2586 case ETHTOOL_GFEATURES
:
2587 case ETHTOOL_GCHANNELS
:
2588 case ETHTOOL_GET_TS_INFO
:
2590 case ETHTOOL_GTUNABLE
:
2591 case ETHTOOL_PHY_GTUNABLE
:
2592 case ETHTOOL_GLINKSETTINGS
:
2593 case ETHTOOL_GFECPARAM
:
2596 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2600 if (dev
->ethtool_ops
->begin
) {
2601 rc
= dev
->ethtool_ops
->begin(dev
);
2605 old_features
= dev
->features
;
2609 rc
= ethtool_get_settings(dev
, useraddr
);
2612 rc
= ethtool_set_settings(dev
, useraddr
);
2614 case ETHTOOL_GDRVINFO
:
2615 rc
= ethtool_get_drvinfo(dev
, useraddr
);
2618 rc
= ethtool_get_regs(dev
, useraddr
);
2621 rc
= ethtool_get_wol(dev
, useraddr
);
2624 rc
= ethtool_set_wol(dev
, useraddr
);
2626 case ETHTOOL_GMSGLVL
:
2627 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2628 dev
->ethtool_ops
->get_msglevel
);
2630 case ETHTOOL_SMSGLVL
:
2631 rc
= ethtool_set_value_void(dev
, useraddr
,
2632 dev
->ethtool_ops
->set_msglevel
);
2635 rc
= ethtool_get_eee(dev
, useraddr
);
2638 rc
= ethtool_set_eee(dev
, useraddr
);
2640 case ETHTOOL_NWAY_RST
:
2641 rc
= ethtool_nway_reset(dev
);
2644 rc
= ethtool_get_link(dev
, useraddr
);
2646 case ETHTOOL_GEEPROM
:
2647 rc
= ethtool_get_eeprom(dev
, useraddr
);
2649 case ETHTOOL_SEEPROM
:
2650 rc
= ethtool_set_eeprom(dev
, useraddr
);
2652 case ETHTOOL_GCOALESCE
:
2653 rc
= ethtool_get_coalesce(dev
, useraddr
);
2655 case ETHTOOL_SCOALESCE
:
2656 rc
= ethtool_set_coalesce(dev
, useraddr
);
2658 case ETHTOOL_GRINGPARAM
:
2659 rc
= ethtool_get_ringparam(dev
, useraddr
);
2661 case ETHTOOL_SRINGPARAM
:
2662 rc
= ethtool_set_ringparam(dev
, useraddr
);
2664 case ETHTOOL_GPAUSEPARAM
:
2665 rc
= ethtool_get_pauseparam(dev
, useraddr
);
2667 case ETHTOOL_SPAUSEPARAM
:
2668 rc
= ethtool_set_pauseparam(dev
, useraddr
);
2671 rc
= ethtool_self_test(dev
, useraddr
);
2673 case ETHTOOL_GSTRINGS
:
2674 rc
= ethtool_get_strings(dev
, useraddr
);
2676 case ETHTOOL_PHYS_ID
:
2677 rc
= ethtool_phys_id(dev
, useraddr
);
2679 case ETHTOOL_GSTATS
:
2680 rc
= ethtool_get_stats(dev
, useraddr
);
2682 case ETHTOOL_GPERMADDR
:
2683 rc
= ethtool_get_perm_addr(dev
, useraddr
);
2685 case ETHTOOL_GFLAGS
:
2686 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2687 __ethtool_get_flags
);
2689 case ETHTOOL_SFLAGS
:
2690 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
2692 case ETHTOOL_GPFLAGS
:
2693 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2694 dev
->ethtool_ops
->get_priv_flags
);
2696 case ETHTOOL_SPFLAGS
:
2697 rc
= ethtool_set_value(dev
, useraddr
,
2698 dev
->ethtool_ops
->set_priv_flags
);
2701 case ETHTOOL_GRXRINGS
:
2702 case ETHTOOL_GRXCLSRLCNT
:
2703 case ETHTOOL_GRXCLSRULE
:
2704 case ETHTOOL_GRXCLSRLALL
:
2705 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
2708 case ETHTOOL_SRXCLSRLDEL
:
2709 case ETHTOOL_SRXCLSRLINS
:
2710 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
2712 case ETHTOOL_FLASHDEV
:
2713 rc
= ethtool_flash_device(dev
, useraddr
);
2716 rc
= ethtool_reset(dev
, useraddr
);
2718 case ETHTOOL_GSSET_INFO
:
2719 rc
= ethtool_get_sset_info(dev
, useraddr
);
2721 case ETHTOOL_GRXFHINDIR
:
2722 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2724 case ETHTOOL_SRXFHINDIR
:
2725 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2728 rc
= ethtool_get_rxfh(dev
, useraddr
);
2731 rc
= ethtool_set_rxfh(dev
, useraddr
);
2733 case ETHTOOL_GFEATURES
:
2734 rc
= ethtool_get_features(dev
, useraddr
);
2736 case ETHTOOL_SFEATURES
:
2737 rc
= ethtool_set_features(dev
, useraddr
);
2739 case ETHTOOL_GTXCSUM
:
2740 case ETHTOOL_GRXCSUM
:
2745 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2747 case ETHTOOL_STXCSUM
:
2748 case ETHTOOL_SRXCSUM
:
2753 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2755 case ETHTOOL_GCHANNELS
:
2756 rc
= ethtool_get_channels(dev
, useraddr
);
2758 case ETHTOOL_SCHANNELS
:
2759 rc
= ethtool_set_channels(dev
, useraddr
);
2761 case ETHTOOL_SET_DUMP
:
2762 rc
= ethtool_set_dump(dev
, useraddr
);
2764 case ETHTOOL_GET_DUMP_FLAG
:
2765 rc
= ethtool_get_dump_flag(dev
, useraddr
);
2767 case ETHTOOL_GET_DUMP_DATA
:
2768 rc
= ethtool_get_dump_data(dev
, useraddr
);
2770 case ETHTOOL_GET_TS_INFO
:
2771 rc
= ethtool_get_ts_info(dev
, useraddr
);
2773 case ETHTOOL_GMODULEINFO
:
2774 rc
= ethtool_get_module_info(dev
, useraddr
);
2776 case ETHTOOL_GMODULEEEPROM
:
2777 rc
= ethtool_get_module_eeprom(dev
, useraddr
);
2779 case ETHTOOL_GTUNABLE
:
2780 rc
= ethtool_get_tunable(dev
, useraddr
);
2782 case ETHTOOL_STUNABLE
:
2783 rc
= ethtool_set_tunable(dev
, useraddr
);
2785 case ETHTOOL_GPHYSTATS
:
2786 rc
= ethtool_get_phy_stats(dev
, useraddr
);
2788 case ETHTOOL_PERQUEUE
:
2789 rc
= ethtool_set_per_queue(dev
, useraddr
, sub_cmd
);
2791 case ETHTOOL_GLINKSETTINGS
:
2792 rc
= ethtool_get_link_ksettings(dev
, useraddr
);
2794 case ETHTOOL_SLINKSETTINGS
:
2795 rc
= ethtool_set_link_ksettings(dev
, useraddr
);
2797 case ETHTOOL_PHY_GTUNABLE
:
2798 rc
= get_phy_tunable(dev
, useraddr
);
2800 case ETHTOOL_PHY_STUNABLE
:
2801 rc
= set_phy_tunable(dev
, useraddr
);
2803 case ETHTOOL_GFECPARAM
:
2804 rc
= ethtool_get_fecparam(dev
, useraddr
);
2806 case ETHTOOL_SFECPARAM
:
2807 rc
= ethtool_set_fecparam(dev
, useraddr
);
2813 if (dev
->ethtool_ops
->complete
)
2814 dev
->ethtool_ops
->complete(dev
);
2816 if (old_features
!= dev
->features
)
2817 netdev_features_change(dev
);
2822 struct ethtool_rx_flow_key
{
2823 struct flow_dissector_key_basic basic
;
2825 struct flow_dissector_key_ipv4_addrs ipv4
;
2826 struct flow_dissector_key_ipv6_addrs ipv6
;
2828 struct flow_dissector_key_ports tp
;
2829 struct flow_dissector_key_ip ip
;
2830 struct flow_dissector_key_vlan vlan
;
2831 struct flow_dissector_key_eth_addrs eth_addrs
;
2832 } __aligned(BITS_PER_LONG
/ 8); /* Ensure that we can do comparisons as longs. */
2834 struct ethtool_rx_flow_match
{
2835 struct flow_dissector dissector
;
2836 struct ethtool_rx_flow_key key
;
2837 struct ethtool_rx_flow_key mask
;
2840 struct ethtool_rx_flow_rule
*
2841 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input
*input
)
2843 const struct ethtool_rx_flow_spec
*fs
= input
->fs
;
2844 static struct in6_addr zero_addr
= {};
2845 struct ethtool_rx_flow_match
*match
;
2846 struct ethtool_rx_flow_rule
*flow
;
2847 struct flow_action_entry
*act
;
2849 flow
= kzalloc(sizeof(struct ethtool_rx_flow_rule
) +
2850 sizeof(struct ethtool_rx_flow_match
), GFP_KERNEL
);
2852 return ERR_PTR(-ENOMEM
);
2854 /* ethtool_rx supports only one single action per rule. */
2855 flow
->rule
= flow_rule_alloc(1);
2858 return ERR_PTR(-ENOMEM
);
2861 match
= (struct ethtool_rx_flow_match
*)flow
->priv
;
2862 flow
->rule
->match
.dissector
= &match
->dissector
;
2863 flow
->rule
->match
.mask
= &match
->mask
;
2864 flow
->rule
->match
.key
= &match
->key
;
2866 match
->mask
.basic
.n_proto
= htons(0xffff);
2868 switch (fs
->flow_type
& ~(FLOW_EXT
| FLOW_MAC_EXT
| FLOW_RSS
)) {
2871 const struct ethtool_tcpip4_spec
*v4_spec
, *v4_m_spec
;
2873 match
->key
.basic
.n_proto
= htons(ETH_P_IP
);
2875 v4_spec
= &fs
->h_u
.tcp_ip4_spec
;
2876 v4_m_spec
= &fs
->m_u
.tcp_ip4_spec
;
2878 if (v4_m_spec
->ip4src
) {
2879 match
->key
.ipv4
.src
= v4_spec
->ip4src
;
2880 match
->mask
.ipv4
.src
= v4_m_spec
->ip4src
;
2882 if (v4_m_spec
->ip4dst
) {
2883 match
->key
.ipv4
.dst
= v4_spec
->ip4dst
;
2884 match
->mask
.ipv4
.dst
= v4_m_spec
->ip4dst
;
2886 if (v4_m_spec
->ip4src
||
2887 v4_m_spec
->ip4dst
) {
2888 match
->dissector
.used_keys
|=
2889 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS
);
2890 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IPV4_ADDRS
] =
2891 offsetof(struct ethtool_rx_flow_key
, ipv4
);
2893 if (v4_m_spec
->psrc
) {
2894 match
->key
.tp
.src
= v4_spec
->psrc
;
2895 match
->mask
.tp
.src
= v4_m_spec
->psrc
;
2897 if (v4_m_spec
->pdst
) {
2898 match
->key
.tp
.dst
= v4_spec
->pdst
;
2899 match
->mask
.tp
.dst
= v4_m_spec
->pdst
;
2901 if (v4_m_spec
->psrc
||
2903 match
->dissector
.used_keys
|=
2904 BIT(FLOW_DISSECTOR_KEY_PORTS
);
2905 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_PORTS
] =
2906 offsetof(struct ethtool_rx_flow_key
, tp
);
2908 if (v4_m_spec
->tos
) {
2909 match
->key
.ip
.tos
= v4_spec
->tos
;
2910 match
->mask
.ip
.tos
= v4_m_spec
->tos
;
2911 match
->dissector
.used_keys
|=
2912 BIT(FLOW_DISSECTOR_KEY_IP
);
2913 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IP
] =
2914 offsetof(struct ethtool_rx_flow_key
, ip
);
2920 const struct ethtool_tcpip6_spec
*v6_spec
, *v6_m_spec
;
2922 match
->key
.basic
.n_proto
= htons(ETH_P_IPV6
);
2924 v6_spec
= &fs
->h_u
.tcp_ip6_spec
;
2925 v6_m_spec
= &fs
->m_u
.tcp_ip6_spec
;
2926 if (memcmp(v6_m_spec
->ip6src
, &zero_addr
, sizeof(zero_addr
))) {
2927 memcpy(&match
->key
.ipv6
.src
, v6_spec
->ip6src
,
2928 sizeof(match
->key
.ipv6
.src
));
2929 memcpy(&match
->mask
.ipv6
.src
, v6_m_spec
->ip6src
,
2930 sizeof(match
->mask
.ipv6
.src
));
2932 if (memcmp(v6_m_spec
->ip6dst
, &zero_addr
, sizeof(zero_addr
))) {
2933 memcpy(&match
->key
.ipv6
.dst
, v6_spec
->ip6dst
,
2934 sizeof(match
->key
.ipv6
.dst
));
2935 memcpy(&match
->mask
.ipv6
.dst
, v6_m_spec
->ip6dst
,
2936 sizeof(match
->mask
.ipv6
.dst
));
2938 if (memcmp(v6_m_spec
->ip6src
, &zero_addr
, sizeof(zero_addr
)) ||
2939 memcmp(v6_m_spec
->ip6src
, &zero_addr
, sizeof(zero_addr
))) {
2940 match
->dissector
.used_keys
|=
2941 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS
);
2942 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IPV6_ADDRS
] =
2943 offsetof(struct ethtool_rx_flow_key
, ipv6
);
2945 if (v6_m_spec
->psrc
) {
2946 match
->key
.tp
.src
= v6_spec
->psrc
;
2947 match
->mask
.tp
.src
= v6_m_spec
->psrc
;
2949 if (v6_m_spec
->pdst
) {
2950 match
->key
.tp
.dst
= v6_spec
->pdst
;
2951 match
->mask
.tp
.dst
= v6_m_spec
->pdst
;
2953 if (v6_m_spec
->psrc
||
2955 match
->dissector
.used_keys
|=
2956 BIT(FLOW_DISSECTOR_KEY_PORTS
);
2957 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_PORTS
] =
2958 offsetof(struct ethtool_rx_flow_key
, tp
);
2960 if (v6_m_spec
->tclass
) {
2961 match
->key
.ip
.tos
= v6_spec
->tclass
;
2962 match
->mask
.ip
.tos
= v6_m_spec
->tclass
;
2963 match
->dissector
.used_keys
|=
2964 BIT(FLOW_DISSECTOR_KEY_IP
);
2965 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IP
] =
2966 offsetof(struct ethtool_rx_flow_key
, ip
);
2971 ethtool_rx_flow_rule_destroy(flow
);
2972 return ERR_PTR(-EINVAL
);
2975 switch (fs
->flow_type
& ~(FLOW_EXT
| FLOW_MAC_EXT
| FLOW_RSS
)) {
2978 match
->key
.basic
.ip_proto
= IPPROTO_TCP
;
2982 match
->key
.basic
.ip_proto
= IPPROTO_UDP
;
2985 match
->mask
.basic
.ip_proto
= 0xff;
2987 match
->dissector
.used_keys
|= BIT(FLOW_DISSECTOR_KEY_BASIC
);
2988 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_BASIC
] =
2989 offsetof(struct ethtool_rx_flow_key
, basic
);
2991 if (fs
->flow_type
& FLOW_EXT
) {
2992 const struct ethtool_flow_ext
*ext_h_spec
= &fs
->h_ext
;
2993 const struct ethtool_flow_ext
*ext_m_spec
= &fs
->m_ext
;
2995 if (ext_m_spec
->vlan_etype
&&
2996 ext_m_spec
->vlan_tci
) {
2997 match
->key
.vlan
.vlan_tpid
= ext_h_spec
->vlan_etype
;
2998 match
->mask
.vlan
.vlan_tpid
= ext_m_spec
->vlan_etype
;
3000 match
->key
.vlan
.vlan_id
=
3001 ntohs(ext_h_spec
->vlan_tci
) & 0x0fff;
3002 match
->mask
.vlan
.vlan_id
=
3003 ntohs(ext_m_spec
->vlan_tci
) & 0x0fff;
3005 match
->key
.vlan
.vlan_priority
=
3006 (ntohs(ext_h_spec
->vlan_tci
) & 0xe000) >> 13;
3007 match
->mask
.vlan
.vlan_priority
=
3008 (ntohs(ext_m_spec
->vlan_tci
) & 0xe000) >> 13;
3010 match
->dissector
.used_keys
|=
3011 BIT(FLOW_DISSECTOR_KEY_VLAN
);
3012 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_VLAN
] =
3013 offsetof(struct ethtool_rx_flow_key
, vlan
);
3016 if (fs
->flow_type
& FLOW_MAC_EXT
) {
3017 const struct ethtool_flow_ext
*ext_h_spec
= &fs
->h_ext
;
3018 const struct ethtool_flow_ext
*ext_m_spec
= &fs
->m_ext
;
3020 memcpy(match
->key
.eth_addrs
.dst
, ext_h_spec
->h_dest
,
3022 memcpy(match
->mask
.eth_addrs
.dst
, ext_m_spec
->h_dest
,
3025 match
->dissector
.used_keys
|=
3026 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS
);
3027 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_ETH_ADDRS
] =
3028 offsetof(struct ethtool_rx_flow_key
, eth_addrs
);
3031 act
= &flow
->rule
->action
.entries
[0];
3032 switch (fs
->ring_cookie
) {
3033 case RX_CLS_FLOW_DISC
:
3034 act
->id
= FLOW_ACTION_DROP
;
3036 case RX_CLS_FLOW_WAKE
:
3037 act
->id
= FLOW_ACTION_WAKE
;
3040 act
->id
= FLOW_ACTION_QUEUE
;
3041 if (fs
->flow_type
& FLOW_RSS
)
3042 act
->queue
.ctx
= input
->rss_ctx
;
3044 act
->queue
.vf
= ethtool_get_flow_spec_ring_vf(fs
->ring_cookie
);
3045 act
->queue
.index
= ethtool_get_flow_spec_ring(fs
->ring_cookie
);
3051 EXPORT_SYMBOL(ethtool_rx_flow_rule_create
);
3053 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule
*flow
)
3058 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy
);