1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/ethtool.c - Ethtool ioctl handler
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
6 * This file is where we call all the ethtool_ops commands to get
7 * the information ethtool needs.
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/capability.h>
13 #include <linux/errno.h>
14 #include <linux/ethtool.h>
15 #include <linux/netdevice.h>
16 #include <linux/net_tstamp.h>
17 #include <linux/phy.h>
18 #include <linux/bitops.h>
19 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sfp.h>
22 #include <linux/slab.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/sched/signal.h>
25 #include <linux/net.h>
26 #include <net/devlink.h>
27 #include <net/xdp_sock_drv.h>
28 #include <net/flow_offload.h>
29 #include <linux/ethtool_netlink.h>
30 #include <generated/utsrelease.h>
34 * Some useful ethtool_ops methods that're device independent.
35 * If we find that all drivers want to do the same thing here,
36 * we can turn these into dev_() function calls.
39 u32
ethtool_op_get_link(struct net_device
*dev
)
41 return netif_carrier_ok(dev
) ? 1 : 0;
43 EXPORT_SYMBOL(ethtool_op_get_link
);
45 int ethtool_op_get_ts_info(struct net_device
*dev
, struct ethtool_ts_info
*info
)
47 info
->so_timestamping
=
48 SOF_TIMESTAMPING_TX_SOFTWARE
|
49 SOF_TIMESTAMPING_RX_SOFTWARE
|
50 SOF_TIMESTAMPING_SOFTWARE
;
54 EXPORT_SYMBOL(ethtool_op_get_ts_info
);
56 /* Handlers for each ethtool command */
58 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
60 struct ethtool_gfeatures cmd
= {
61 .cmd
= ETHTOOL_GFEATURES
,
62 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
64 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
69 /* in case feature bits run out again */
70 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
72 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
73 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
74 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
75 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
76 features
[i
].never_changed
=
77 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
80 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
81 if (get_user(copy_size
, sizeaddr
))
84 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
85 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
87 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
89 useraddr
+= sizeof(cmd
);
90 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
96 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
98 struct ethtool_sfeatures cmd
;
99 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
100 netdev_features_t wanted
= 0, valid
= 0;
103 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
105 useraddr
+= sizeof(cmd
);
107 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
110 if (copy_from_user(features
, useraddr
, sizeof(features
)))
113 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
114 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
115 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
118 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
121 if (valid
& ~dev
->hw_features
) {
122 valid
&= dev
->hw_features
;
123 ret
|= ETHTOOL_F_UNSUPPORTED
;
126 dev
->wanted_features
&= ~valid
;
127 dev
->wanted_features
|= wanted
& valid
;
128 __netdev_update_features(dev
);
130 if ((dev
->wanted_features
^ dev
->features
) & valid
)
131 ret
|= ETHTOOL_F_WISH
;
136 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
138 const struct ethtool_phy_ops
*phy_ops
= ethtool_phy_ops
;
139 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
141 if (sset
== ETH_SS_FEATURES
)
142 return ARRAY_SIZE(netdev_features_strings
);
144 if (sset
== ETH_SS_RSS_HASH_FUNCS
)
145 return ARRAY_SIZE(rss_hash_func_strings
);
147 if (sset
== ETH_SS_TUNABLES
)
148 return ARRAY_SIZE(tunable_strings
);
150 if (sset
== ETH_SS_PHY_TUNABLES
)
151 return ARRAY_SIZE(phy_tunable_strings
);
153 if (sset
== ETH_SS_PHY_STATS
&& dev
->phydev
&&
154 !ops
->get_ethtool_phy_stats
&&
155 phy_ops
&& phy_ops
->get_sset_count
)
156 return phy_ops
->get_sset_count(dev
->phydev
);
158 if (sset
== ETH_SS_LINK_MODES
)
159 return __ETHTOOL_LINK_MODE_MASK_NBITS
;
161 if (ops
->get_sset_count
&& ops
->get_strings
)
162 return ops
->get_sset_count(dev
, sset
);
167 static void __ethtool_get_strings(struct net_device
*dev
,
168 u32 stringset
, u8
*data
)
170 const struct ethtool_phy_ops
*phy_ops
= ethtool_phy_ops
;
171 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
173 if (stringset
== ETH_SS_FEATURES
)
174 memcpy(data
, netdev_features_strings
,
175 sizeof(netdev_features_strings
));
176 else if (stringset
== ETH_SS_RSS_HASH_FUNCS
)
177 memcpy(data
, rss_hash_func_strings
,
178 sizeof(rss_hash_func_strings
));
179 else if (stringset
== ETH_SS_TUNABLES
)
180 memcpy(data
, tunable_strings
, sizeof(tunable_strings
));
181 else if (stringset
== ETH_SS_PHY_TUNABLES
)
182 memcpy(data
, phy_tunable_strings
, sizeof(phy_tunable_strings
));
183 else if (stringset
== ETH_SS_PHY_STATS
&& dev
->phydev
&&
184 !ops
->get_ethtool_phy_stats
&& phy_ops
&&
185 phy_ops
->get_strings
)
186 phy_ops
->get_strings(dev
->phydev
, data
);
187 else if (stringset
== ETH_SS_LINK_MODES
)
188 memcpy(data
, link_mode_names
,
189 __ETHTOOL_LINK_MODE_MASK_NBITS
* ETH_GSTRING_LEN
);
191 /* ops->get_strings is valid because checked earlier */
192 ops
->get_strings(dev
, stringset
, data
);
195 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
197 /* feature masks of legacy discrete ethtool ops */
200 case ETHTOOL_GTXCSUM
:
201 case ETHTOOL_STXCSUM
:
202 return NETIF_F_CSUM_MASK
| NETIF_F_FCOE_CRC
|
204 case ETHTOOL_GRXCSUM
:
205 case ETHTOOL_SRXCSUM
:
206 return NETIF_F_RXCSUM
;
209 return NETIF_F_SG
| NETIF_F_FRAGLIST
;
212 return NETIF_F_ALL_TSO
;
224 static int ethtool_get_one_feature(struct net_device
*dev
,
225 char __user
*useraddr
, u32 ethcmd
)
227 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
228 struct ethtool_value edata
= {
230 .data
= !!(dev
->features
& mask
),
233 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
238 static int ethtool_set_one_feature(struct net_device
*dev
,
239 void __user
*useraddr
, u32 ethcmd
)
241 struct ethtool_value edata
;
242 netdev_features_t mask
;
244 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
247 mask
= ethtool_get_feature_mask(ethcmd
);
248 mask
&= dev
->hw_features
;
253 dev
->wanted_features
|= mask
;
255 dev
->wanted_features
&= ~mask
;
257 __netdev_update_features(dev
);
262 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
263 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
264 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
265 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
268 static u32
__ethtool_get_flags(struct net_device
*dev
)
272 if (dev
->features
& NETIF_F_LRO
)
273 flags
|= ETH_FLAG_LRO
;
274 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_RX
)
275 flags
|= ETH_FLAG_RXVLAN
;
276 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_TX
)
277 flags
|= ETH_FLAG_TXVLAN
;
278 if (dev
->features
& NETIF_F_NTUPLE
)
279 flags
|= ETH_FLAG_NTUPLE
;
280 if (dev
->features
& NETIF_F_RXHASH
)
281 flags
|= ETH_FLAG_RXHASH
;
286 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
288 netdev_features_t features
= 0, changed
;
290 if (data
& ~ETH_ALL_FLAGS
)
293 if (data
& ETH_FLAG_LRO
)
294 features
|= NETIF_F_LRO
;
295 if (data
& ETH_FLAG_RXVLAN
)
296 features
|= NETIF_F_HW_VLAN_CTAG_RX
;
297 if (data
& ETH_FLAG_TXVLAN
)
298 features
|= NETIF_F_HW_VLAN_CTAG_TX
;
299 if (data
& ETH_FLAG_NTUPLE
)
300 features
|= NETIF_F_NTUPLE
;
301 if (data
& ETH_FLAG_RXHASH
)
302 features
|= NETIF_F_RXHASH
;
304 /* allow changing only bits set in hw_features */
305 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
306 if (changed
& ~dev
->hw_features
)
307 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
309 dev
->wanted_features
=
310 (dev
->wanted_features
& ~changed
) | (features
& changed
);
312 __netdev_update_features(dev
);
317 /* Given two link masks, AND them together and save the result in dst. */
318 void ethtool_intersect_link_masks(struct ethtool_link_ksettings
*dst
,
319 struct ethtool_link_ksettings
*src
)
321 unsigned int size
= BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS
);
322 unsigned int idx
= 0;
324 for (; idx
< size
; idx
++) {
325 dst
->link_modes
.supported
[idx
] &=
326 src
->link_modes
.supported
[idx
];
327 dst
->link_modes
.advertising
[idx
] &=
328 src
->link_modes
.advertising
[idx
];
331 EXPORT_SYMBOL(ethtool_intersect_link_masks
);
333 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst
,
336 bitmap_zero(dst
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
339 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode
);
341 /* return false if src had higher bits set. lower bits always updated. */
342 bool ethtool_convert_link_mode_to_legacy_u32(u32
*legacy_u32
,
343 const unsigned long *src
)
347 /* TODO: following test will soon always be true */
348 if (__ETHTOOL_LINK_MODE_MASK_NBITS
> 32) {
349 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext
);
351 bitmap_zero(ext
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
352 bitmap_fill(ext
, 32);
353 bitmap_complement(ext
, ext
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
354 if (bitmap_intersects(ext
, src
,
355 __ETHTOOL_LINK_MODE_MASK_NBITS
)) {
356 /* src mask goes beyond bit 31 */
360 *legacy_u32
= src
[0];
363 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32
);
365 /* return false if ksettings link modes had higher bits
366 * set. legacy_settings always updated (best effort)
369 convert_link_ksettings_to_legacy_settings(
370 struct ethtool_cmd
*legacy_settings
,
371 const struct ethtool_link_ksettings
*link_ksettings
)
375 memset(legacy_settings
, 0, sizeof(*legacy_settings
));
376 /* this also clears the deprecated fields in legacy structure:
382 retval
&= ethtool_convert_link_mode_to_legacy_u32(
383 &legacy_settings
->supported
,
384 link_ksettings
->link_modes
.supported
);
385 retval
&= ethtool_convert_link_mode_to_legacy_u32(
386 &legacy_settings
->advertising
,
387 link_ksettings
->link_modes
.advertising
);
388 retval
&= ethtool_convert_link_mode_to_legacy_u32(
389 &legacy_settings
->lp_advertising
,
390 link_ksettings
->link_modes
.lp_advertising
);
391 ethtool_cmd_speed_set(legacy_settings
, link_ksettings
->base
.speed
);
392 legacy_settings
->duplex
393 = link_ksettings
->base
.duplex
;
394 legacy_settings
->port
395 = link_ksettings
->base
.port
;
396 legacy_settings
->phy_address
397 = link_ksettings
->base
.phy_address
;
398 legacy_settings
->autoneg
399 = link_ksettings
->base
.autoneg
;
400 legacy_settings
->mdio_support
401 = link_ksettings
->base
.mdio_support
;
402 legacy_settings
->eth_tp_mdix
403 = link_ksettings
->base
.eth_tp_mdix
;
404 legacy_settings
->eth_tp_mdix_ctrl
405 = link_ksettings
->base
.eth_tp_mdix_ctrl
;
406 legacy_settings
->transceiver
407 = link_ksettings
->base
.transceiver
;
411 /* number of 32-bit words to store the user's link mode bitmaps */
412 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
413 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
415 /* layout of the struct passed from/to userland */
416 struct ethtool_link_usettings
{
417 struct ethtool_link_settings base
;
419 __u32 supported
[__ETHTOOL_LINK_MODE_MASK_NU32
];
420 __u32 advertising
[__ETHTOOL_LINK_MODE_MASK_NU32
];
421 __u32 lp_advertising
[__ETHTOOL_LINK_MODE_MASK_NU32
];
425 /* Internal kernel helper to query a device ethtool_link_settings. */
426 int __ethtool_get_link_ksettings(struct net_device
*dev
,
427 struct ethtool_link_ksettings
*link_ksettings
)
431 if (!dev
->ethtool_ops
->get_link_ksettings
)
434 memset(link_ksettings
, 0, sizeof(*link_ksettings
));
435 return dev
->ethtool_ops
->get_link_ksettings(dev
, link_ksettings
);
437 EXPORT_SYMBOL(__ethtool_get_link_ksettings
);
439 /* convert ethtool_link_usettings in user space to a kernel internal
440 * ethtool_link_ksettings. return 0 on success, errno on error.
442 static int load_link_ksettings_from_user(struct ethtool_link_ksettings
*to
,
443 const void __user
*from
)
445 struct ethtool_link_usettings link_usettings
;
447 if (copy_from_user(&link_usettings
, from
, sizeof(link_usettings
)))
450 memcpy(&to
->base
, &link_usettings
.base
, sizeof(to
->base
));
451 bitmap_from_arr32(to
->link_modes
.supported
,
452 link_usettings
.link_modes
.supported
,
453 __ETHTOOL_LINK_MODE_MASK_NBITS
);
454 bitmap_from_arr32(to
->link_modes
.advertising
,
455 link_usettings
.link_modes
.advertising
,
456 __ETHTOOL_LINK_MODE_MASK_NBITS
);
457 bitmap_from_arr32(to
->link_modes
.lp_advertising
,
458 link_usettings
.link_modes
.lp_advertising
,
459 __ETHTOOL_LINK_MODE_MASK_NBITS
);
464 /* Check if the user is trying to change anything besides speed/duplex */
465 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings
*cmd
)
467 struct ethtool_link_settings base2
= {};
469 base2
.speed
= cmd
->base
.speed
;
470 base2
.port
= PORT_OTHER
;
471 base2
.duplex
= cmd
->base
.duplex
;
472 base2
.cmd
= cmd
->base
.cmd
;
473 base2
.link_mode_masks_nwords
= cmd
->base
.link_mode_masks_nwords
;
475 return !memcmp(&base2
, &cmd
->base
, sizeof(base2
)) &&
476 bitmap_empty(cmd
->link_modes
.supported
,
477 __ETHTOOL_LINK_MODE_MASK_NBITS
) &&
478 bitmap_empty(cmd
->link_modes
.lp_advertising
,
479 __ETHTOOL_LINK_MODE_MASK_NBITS
);
482 /* convert a kernel internal ethtool_link_ksettings to
483 * ethtool_link_usettings in user space. return 0 on success, errno on
487 store_link_ksettings_for_user(void __user
*to
,
488 const struct ethtool_link_ksettings
*from
)
490 struct ethtool_link_usettings link_usettings
;
492 memcpy(&link_usettings
.base
, &from
->base
, sizeof(link_usettings
));
493 bitmap_to_arr32(link_usettings
.link_modes
.supported
,
494 from
->link_modes
.supported
,
495 __ETHTOOL_LINK_MODE_MASK_NBITS
);
496 bitmap_to_arr32(link_usettings
.link_modes
.advertising
,
497 from
->link_modes
.advertising
,
498 __ETHTOOL_LINK_MODE_MASK_NBITS
);
499 bitmap_to_arr32(link_usettings
.link_modes
.lp_advertising
,
500 from
->link_modes
.lp_advertising
,
501 __ETHTOOL_LINK_MODE_MASK_NBITS
);
503 if (copy_to_user(to
, &link_usettings
, sizeof(link_usettings
)))
509 /* Query device for its ethtool_link_settings. */
510 static int ethtool_get_link_ksettings(struct net_device
*dev
,
511 void __user
*useraddr
)
514 struct ethtool_link_ksettings link_ksettings
;
517 if (!dev
->ethtool_ops
->get_link_ksettings
)
520 /* handle bitmap nbits handshake */
521 if (copy_from_user(&link_ksettings
.base
, useraddr
,
522 sizeof(link_ksettings
.base
)))
525 if (__ETHTOOL_LINK_MODE_MASK_NU32
526 != link_ksettings
.base
.link_mode_masks_nwords
) {
527 /* wrong link mode nbits requested */
528 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
529 link_ksettings
.base
.cmd
= ETHTOOL_GLINKSETTINGS
;
530 /* send back number of words required as negative val */
531 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32
<= S8_MAX
,
532 "need too many bits for link modes!");
533 link_ksettings
.base
.link_mode_masks_nwords
534 = -((s8
)__ETHTOOL_LINK_MODE_MASK_NU32
);
536 /* copy the base fields back to user, not the link
539 if (copy_to_user(useraddr
, &link_ksettings
.base
,
540 sizeof(link_ksettings
.base
)))
546 /* handshake successful: user/kernel agree on
547 * link_mode_masks_nwords
550 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
551 err
= dev
->ethtool_ops
->get_link_ksettings(dev
, &link_ksettings
);
555 /* make sure we tell the right values to user */
556 link_ksettings
.base
.cmd
= ETHTOOL_GLINKSETTINGS
;
557 link_ksettings
.base
.link_mode_masks_nwords
558 = __ETHTOOL_LINK_MODE_MASK_NU32
;
559 link_ksettings
.base
.master_slave_cfg
= MASTER_SLAVE_CFG_UNSUPPORTED
;
560 link_ksettings
.base
.master_slave_state
= MASTER_SLAVE_STATE_UNSUPPORTED
;
562 return store_link_ksettings_for_user(useraddr
, &link_ksettings
);
565 /* Update device ethtool_link_settings. */
566 static int ethtool_set_link_ksettings(struct net_device
*dev
,
567 void __user
*useraddr
)
570 struct ethtool_link_ksettings link_ksettings
;
574 if (!dev
->ethtool_ops
->set_link_ksettings
)
577 /* make sure nbits field has expected value */
578 if (copy_from_user(&link_ksettings
.base
, useraddr
,
579 sizeof(link_ksettings
.base
)))
582 if (__ETHTOOL_LINK_MODE_MASK_NU32
583 != link_ksettings
.base
.link_mode_masks_nwords
)
586 /* copy the whole structure, now that we know it has expected
589 err
= load_link_ksettings_from_user(&link_ksettings
, useraddr
);
593 /* re-check nwords field, just in case */
594 if (__ETHTOOL_LINK_MODE_MASK_NU32
595 != link_ksettings
.base
.link_mode_masks_nwords
)
598 if (link_ksettings
.base
.master_slave_cfg
||
599 link_ksettings
.base
.master_slave_state
)
602 err
= dev
->ethtool_ops
->set_link_ksettings(dev
, &link_ksettings
);
604 ethtool_notify(dev
, ETHTOOL_MSG_LINKINFO_NTF
, NULL
);
605 ethtool_notify(dev
, ETHTOOL_MSG_LINKMODES_NTF
, NULL
);
610 int ethtool_virtdev_set_link_ksettings(struct net_device
*dev
,
611 const struct ethtool_link_ksettings
*cmd
,
612 u32
*dev_speed
, u8
*dev_duplex
)
617 speed
= cmd
->base
.speed
;
618 duplex
= cmd
->base
.duplex
;
619 /* don't allow custom speed and duplex */
620 if (!ethtool_validate_speed(speed
) ||
621 !ethtool_validate_duplex(duplex
) ||
622 !ethtool_virtdev_validate_cmd(cmd
))
625 *dev_duplex
= duplex
;
629 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings
);
631 /* Query device for its ethtool_cmd settings.
633 * Backward compatibility note: for compatibility with legacy ethtool, this is
634 * now implemented via get_link_ksettings. When driver reports higher link mode
635 * bits, a kernel warning is logged once (with name of 1st driver/device) to
636 * recommend user to upgrade ethtool, but the command is successful (only the
637 * lower link mode bits reported back to user). Deprecated fields from
638 * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
640 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
642 struct ethtool_link_ksettings link_ksettings
;
643 struct ethtool_cmd cmd
;
647 if (!dev
->ethtool_ops
->get_link_ksettings
)
650 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
651 err
= dev
->ethtool_ops
->get_link_ksettings(dev
, &link_ksettings
);
654 convert_link_ksettings_to_legacy_settings(&cmd
, &link_ksettings
);
656 /* send a sensible cmd tag back to user */
657 cmd
.cmd
= ETHTOOL_GSET
;
659 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
665 /* Update device link settings with given ethtool_cmd.
667 * Backward compatibility note: for compatibility with legacy ethtool, this is
668 * now always implemented via set_link_settings. When user's request updates
669 * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
670 * warning is logged once (with name of 1st driver/device) to recommend user to
671 * upgrade ethtool, and the request is rejected.
673 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
675 struct ethtool_link_ksettings link_ksettings
;
676 struct ethtool_cmd cmd
;
681 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
683 if (!dev
->ethtool_ops
->set_link_ksettings
)
686 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings
, &cmd
))
688 link_ksettings
.base
.link_mode_masks_nwords
=
689 __ETHTOOL_LINK_MODE_MASK_NU32
;
690 ret
= dev
->ethtool_ops
->set_link_ksettings(dev
, &link_ksettings
);
692 ethtool_notify(dev
, ETHTOOL_MSG_LINKINFO_NTF
, NULL
);
693 ethtool_notify(dev
, ETHTOOL_MSG_LINKMODES_NTF
, NULL
);
698 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
699 void __user
*useraddr
)
701 struct ethtool_drvinfo info
;
702 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
704 memset(&info
, 0, sizeof(info
));
705 info
.cmd
= ETHTOOL_GDRVINFO
;
706 strlcpy(info
.version
, UTS_RELEASE
, sizeof(info
.version
));
707 if (ops
->get_drvinfo
) {
708 ops
->get_drvinfo(dev
, &info
);
709 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
710 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
711 sizeof(info
.bus_info
));
712 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
713 sizeof(info
.driver
));
719 * this method of obtaining string set info is deprecated;
720 * Use ETHTOOL_GSSET_INFO instead.
722 if (ops
->get_sset_count
) {
725 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
727 info
.testinfo_len
= rc
;
728 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
731 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
733 info
.n_priv_flags
= rc
;
735 if (ops
->get_regs_len
) {
736 int ret
= ops
->get_regs_len(dev
);
739 info
.regdump_len
= ret
;
742 if (ops
->get_eeprom_len
)
743 info
.eedump_len
= ops
->get_eeprom_len(dev
);
745 if (!info
.fw_version
[0])
746 devlink_compat_running_version(dev
, info
.fw_version
,
747 sizeof(info
.fw_version
));
749 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
754 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
755 void __user
*useraddr
)
757 struct ethtool_sset_info info
;
759 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
760 u32
*info_buf
= NULL
;
762 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
765 /* store copy of mask, because we zero struct later on */
766 sset_mask
= info
.sset_mask
;
770 /* calculate size of return buffer */
771 n_bits
= hweight64(sset_mask
);
773 memset(&info
, 0, sizeof(info
));
774 info
.cmd
= ETHTOOL_GSSET_INFO
;
776 info_buf
= kcalloc(n_bits
, sizeof(u32
), GFP_USER
);
781 * fill return buffer based on input bitmask and successful
782 * get_sset_count return
784 for (i
= 0; i
< 64; i
++) {
785 if (!(sset_mask
& (1ULL << i
)))
788 rc
= __ethtool_get_sset_count(dev
, i
);
790 info
.sset_mask
|= (1ULL << i
);
791 info_buf
[idx
++] = rc
;
796 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
799 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
800 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
810 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
811 u32 cmd
, void __user
*useraddr
)
813 struct ethtool_rxnfc info
;
814 size_t info_size
= sizeof(info
);
817 if (!dev
->ethtool_ops
->set_rxnfc
)
820 /* struct ethtool_rxnfc was originally defined for
821 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
822 * members. User-space might still be using that
824 if (cmd
== ETHTOOL_SRXFH
)
825 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
828 if (copy_from_user(&info
, useraddr
, info_size
))
831 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
835 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
836 copy_to_user(useraddr
, &info
, info_size
))
842 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
843 u32 cmd
, void __user
*useraddr
)
845 struct ethtool_rxnfc info
;
846 size_t info_size
= sizeof(info
);
847 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
849 void *rule_buf
= NULL
;
854 /* struct ethtool_rxnfc was originally defined for
855 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
856 * members. User-space might still be using that
858 if (cmd
== ETHTOOL_GRXFH
)
859 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
862 if (copy_from_user(&info
, useraddr
, info_size
))
865 /* If FLOW_RSS was requested then user-space must be using the
866 * new definition, as FLOW_RSS is newer.
868 if (cmd
== ETHTOOL_GRXFH
&& info
.flow_type
& FLOW_RSS
) {
869 info_size
= sizeof(info
);
870 if (copy_from_user(&info
, useraddr
, info_size
))
872 /* Since malicious users may modify the original data,
873 * we need to check whether FLOW_RSS is still requested.
875 if (!(info
.flow_type
& FLOW_RSS
))
882 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
883 if (info
.rule_cnt
> 0) {
884 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
885 rule_buf
= kcalloc(info
.rule_cnt
, sizeof(u32
),
892 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
897 if (copy_to_user(useraddr
, &info
, info_size
))
901 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
902 if (copy_to_user(useraddr
, rule_buf
,
903 info
.rule_cnt
* sizeof(u32
)))
914 static int ethtool_copy_validate_indir(u32
*indir
, void __user
*useraddr
,
915 struct ethtool_rxnfc
*rx_rings
,
920 if (copy_from_user(indir
, useraddr
, size
* sizeof(indir
[0])))
923 /* Validate ring indices */
924 for (i
= 0; i
< size
; i
++)
925 if (indir
[i
] >= rx_rings
->data
)
931 u8 netdev_rss_key
[NETDEV_RSS_KEY_LEN
] __read_mostly
;
933 void netdev_rss_key_fill(void *buffer
, size_t len
)
935 BUG_ON(len
> sizeof(netdev_rss_key
));
936 net_get_random_once(netdev_rss_key
, sizeof(netdev_rss_key
));
937 memcpy(buffer
, netdev_rss_key
, len
);
939 EXPORT_SYMBOL(netdev_rss_key_fill
);
941 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
942 void __user
*useraddr
)
944 u32 user_size
, dev_size
;
948 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
949 !dev
->ethtool_ops
->get_rxfh
)
951 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
955 if (copy_from_user(&user_size
,
956 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
960 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
961 &dev_size
, sizeof(dev_size
)))
964 /* If the user buffer size is 0, this is just a query for the
965 * device table size. Otherwise, if it's smaller than the
966 * device table size it's an error.
968 if (user_size
< dev_size
)
969 return user_size
== 0 ? 0 : -EINVAL
;
971 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
975 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
979 if (copy_to_user(useraddr
+
980 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
981 indir
, dev_size
* sizeof(indir
[0])))
989 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
990 void __user
*useraddr
)
992 struct ethtool_rxnfc rx_rings
;
993 u32 user_size
, dev_size
, i
;
995 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
997 u32 ringidx_offset
= offsetof(struct ethtool_rxfh_indir
, ring_index
[0]);
999 if (!ops
->get_rxfh_indir_size
|| !ops
->set_rxfh
||
1003 dev_size
= ops
->get_rxfh_indir_size(dev
);
1007 if (copy_from_user(&user_size
,
1008 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1012 if (user_size
!= 0 && user_size
!= dev_size
)
1015 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1019 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
1020 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
1024 if (user_size
== 0) {
1025 for (i
= 0; i
< dev_size
; i
++)
1026 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
1028 ret
= ethtool_copy_validate_indir(indir
,
1029 useraddr
+ ringidx_offset
,
1036 ret
= ops
->set_rxfh(dev
, indir
, NULL
, ETH_RSS_HASH_NO_CHANGE
);
1040 /* indicate whether rxfh was set to default */
1042 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
1044 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
1051 static noinline_for_stack
int ethtool_get_rxfh(struct net_device
*dev
,
1052 void __user
*useraddr
)
1055 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1056 u32 user_indir_size
, user_key_size
;
1057 u32 dev_indir_size
= 0, dev_key_size
= 0;
1058 struct ethtool_rxfh rxfh
;
1069 if (ops
->get_rxfh_indir_size
)
1070 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
1071 if (ops
->get_rxfh_key_size
)
1072 dev_key_size
= ops
->get_rxfh_key_size(dev
);
1074 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
1076 user_indir_size
= rxfh
.indir_size
;
1077 user_key_size
= rxfh
.key_size
;
1079 /* Check that reserved fields are 0 for now */
1080 if (rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] || rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
1082 /* Most drivers don't handle rss_context, check it's 0 as well */
1083 if (rxfh
.rss_context
&& !ops
->get_rxfh_context
)
1086 rxfh
.indir_size
= dev_indir_size
;
1087 rxfh
.key_size
= dev_key_size
;
1088 if (copy_to_user(useraddr
, &rxfh
, sizeof(rxfh
)))
1091 if ((user_indir_size
&& (user_indir_size
!= dev_indir_size
)) ||
1092 (user_key_size
&& (user_key_size
!= dev_key_size
)))
1095 indir_bytes
= user_indir_size
* sizeof(indir
[0]);
1096 total_size
= indir_bytes
+ user_key_size
;
1097 rss_config
= kzalloc(total_size
, GFP_USER
);
1101 if (user_indir_size
)
1102 indir
= (u32
*)rss_config
;
1105 hkey
= rss_config
+ indir_bytes
;
1107 if (rxfh
.rss_context
)
1108 ret
= dev
->ethtool_ops
->get_rxfh_context(dev
, indir
, hkey
,
1112 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, hkey
, &dev_hfunc
);
1116 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, hfunc
),
1117 &dev_hfunc
, sizeof(rxfh
.hfunc
))) {
1119 } else if (copy_to_user(useraddr
+
1120 offsetof(struct ethtool_rxfh
, rss_config
[0]),
1121 rss_config
, total_size
)) {
1130 static noinline_for_stack
int ethtool_set_rxfh(struct net_device
*dev
,
1131 void __user
*useraddr
)
1134 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1135 struct ethtool_rxnfc rx_rings
;
1136 struct ethtool_rxfh rxfh
;
1137 u32 dev_indir_size
= 0, dev_key_size
= 0, i
;
1138 u32
*indir
= NULL
, indir_bytes
= 0;
1141 u32 rss_cfg_offset
= offsetof(struct ethtool_rxfh
, rss_config
[0]);
1142 bool delete = false;
1144 if (!ops
->get_rxnfc
|| !ops
->set_rxfh
)
1147 if (ops
->get_rxfh_indir_size
)
1148 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
1149 if (ops
->get_rxfh_key_size
)
1150 dev_key_size
= ops
->get_rxfh_key_size(dev
);
1152 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
1155 /* Check that reserved fields are 0 for now */
1156 if (rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] || rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
1158 /* Most drivers don't handle rss_context, check it's 0 as well */
1159 if (rxfh
.rss_context
&& !ops
->set_rxfh_context
)
1162 /* If either indir, hash key or function is valid, proceed further.
1163 * Must request at least one change: indir size, hash key or function.
1165 if ((rxfh
.indir_size
&&
1166 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
&&
1167 rxfh
.indir_size
!= dev_indir_size
) ||
1168 (rxfh
.key_size
&& (rxfh
.key_size
!= dev_key_size
)) ||
1169 (rxfh
.indir_size
== ETH_RXFH_INDIR_NO_CHANGE
&&
1170 rxfh
.key_size
== 0 && rxfh
.hfunc
== ETH_RSS_HASH_NO_CHANGE
))
1173 if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
1174 indir_bytes
= dev_indir_size
* sizeof(indir
[0]);
1176 rss_config
= kzalloc(indir_bytes
+ rxfh
.key_size
, GFP_USER
);
1180 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
1181 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
1185 /* rxfh.indir_size == 0 means reset the indir table to default (master
1186 * context) or delete the context (other RSS contexts).
1187 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1189 if (rxfh
.indir_size
&&
1190 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
) {
1191 indir
= (u32
*)rss_config
;
1192 ret
= ethtool_copy_validate_indir(indir
,
1193 useraddr
+ rss_cfg_offset
,
1198 } else if (rxfh
.indir_size
== 0) {
1199 if (rxfh
.rss_context
== 0) {
1200 indir
= (u32
*)rss_config
;
1201 for (i
= 0; i
< dev_indir_size
; i
++)
1202 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
1208 if (rxfh
.key_size
) {
1209 hkey
= rss_config
+ indir_bytes
;
1210 if (copy_from_user(hkey
,
1211 useraddr
+ rss_cfg_offset
+ indir_bytes
,
1218 if (rxfh
.rss_context
)
1219 ret
= ops
->set_rxfh_context(dev
, indir
, hkey
, rxfh
.hfunc
,
1220 &rxfh
.rss_context
, delete);
1222 ret
= ops
->set_rxfh(dev
, indir
, hkey
, rxfh
.hfunc
);
1226 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, rss_context
),
1227 &rxfh
.rss_context
, sizeof(rxfh
.rss_context
)))
1230 if (!rxfh
.rss_context
) {
1231 /* indicate whether rxfh was set to default */
1232 if (rxfh
.indir_size
== 0)
1233 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
1234 else if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
1235 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
1243 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
1245 struct ethtool_regs regs
;
1246 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1250 if (!ops
->get_regs
|| !ops
->get_regs_len
)
1253 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
1256 reglen
= ops
->get_regs_len(dev
);
1260 if (regs
.len
> reglen
)
1263 regbuf
= vzalloc(reglen
);
1267 if (regs
.len
< reglen
)
1270 ops
->get_regs(dev
, ®s
, regbuf
);
1273 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
1275 useraddr
+= offsetof(struct ethtool_regs
, data
);
1276 if (copy_to_user(useraddr
, regbuf
, reglen
))
1285 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
1287 struct ethtool_value reset
;
1290 if (!dev
->ethtool_ops
->reset
)
1293 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
1296 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
1300 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
1305 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
1307 struct ethtool_wolinfo wol
;
1309 if (!dev
->ethtool_ops
->get_wol
)
1312 memset(&wol
, 0, sizeof(struct ethtool_wolinfo
));
1313 wol
.cmd
= ETHTOOL_GWOL
;
1314 dev
->ethtool_ops
->get_wol(dev
, &wol
);
1316 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
1321 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
1323 struct ethtool_wolinfo wol
;
1326 if (!dev
->ethtool_ops
->set_wol
)
1329 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
1332 ret
= dev
->ethtool_ops
->set_wol(dev
, &wol
);
1336 dev
->wol_enabled
= !!wol
.wolopts
;
1337 ethtool_notify(dev
, ETHTOOL_MSG_WOL_NTF
, NULL
);
1342 static int ethtool_get_eee(struct net_device
*dev
, char __user
*useraddr
)
1344 struct ethtool_eee edata
;
1347 if (!dev
->ethtool_ops
->get_eee
)
1350 memset(&edata
, 0, sizeof(struct ethtool_eee
));
1351 edata
.cmd
= ETHTOOL_GEEE
;
1352 rc
= dev
->ethtool_ops
->get_eee(dev
, &edata
);
1357 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1363 static int ethtool_set_eee(struct net_device
*dev
, char __user
*useraddr
)
1365 struct ethtool_eee edata
;
1368 if (!dev
->ethtool_ops
->set_eee
)
1371 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1374 ret
= dev
->ethtool_ops
->set_eee(dev
, &edata
);
1376 ethtool_notify(dev
, ETHTOOL_MSG_EEE_NTF
, NULL
);
1380 static int ethtool_nway_reset(struct net_device
*dev
)
1382 if (!dev
->ethtool_ops
->nway_reset
)
1385 return dev
->ethtool_ops
->nway_reset(dev
);
1388 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
1390 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
1391 int link
= __ethtool_get_link(dev
);
1397 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1402 static int ethtool_get_any_eeprom(struct net_device
*dev
, void __user
*useraddr
,
1403 int (*getter
)(struct net_device
*,
1404 struct ethtool_eeprom
*, u8
*),
1407 struct ethtool_eeprom eeprom
;
1408 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1409 u32 bytes_remaining
;
1413 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1416 /* Check for wrap and zero */
1417 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1420 /* Check for exceeding total eeprom len */
1421 if (eeprom
.offset
+ eeprom
.len
> total_len
)
1424 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1428 bytes_remaining
= eeprom
.len
;
1429 while (bytes_remaining
> 0) {
1430 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1432 ret
= getter(dev
, &eeprom
, data
);
1435 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
1439 userbuf
+= eeprom
.len
;
1440 eeprom
.offset
+= eeprom
.len
;
1441 bytes_remaining
-= eeprom
.len
;
1444 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
1445 eeprom
.offset
-= eeprom
.len
;
1446 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
1453 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1455 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1457 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
||
1458 !ops
->get_eeprom_len(dev
))
1461 return ethtool_get_any_eeprom(dev
, useraddr
, ops
->get_eeprom
,
1462 ops
->get_eeprom_len(dev
));
1465 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1467 struct ethtool_eeprom eeprom
;
1468 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1469 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1470 u32 bytes_remaining
;
1474 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
||
1475 !ops
->get_eeprom_len(dev
))
1478 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1481 /* Check for wrap and zero */
1482 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1485 /* Check for exceeding total eeprom len */
1486 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1489 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1493 bytes_remaining
= eeprom
.len
;
1494 while (bytes_remaining
> 0) {
1495 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1497 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
1501 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
1504 userbuf
+= eeprom
.len
;
1505 eeprom
.offset
+= eeprom
.len
;
1506 bytes_remaining
-= eeprom
.len
;
1513 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
1514 void __user
*useraddr
)
1516 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
1519 if (!dev
->ethtool_ops
->get_coalesce
)
1522 ret
= dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
1526 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
1532 ethtool_set_coalesce_supported(struct net_device
*dev
,
1533 struct ethtool_coalesce
*coalesce
)
1535 u32 supported_params
= dev
->ethtool_ops
->supported_coalesce_params
;
1536 u32 nonzero_params
= 0;
1538 if (coalesce
->rx_coalesce_usecs
)
1539 nonzero_params
|= ETHTOOL_COALESCE_RX_USECS
;
1540 if (coalesce
->rx_max_coalesced_frames
)
1541 nonzero_params
|= ETHTOOL_COALESCE_RX_MAX_FRAMES
;
1542 if (coalesce
->rx_coalesce_usecs_irq
)
1543 nonzero_params
|= ETHTOOL_COALESCE_RX_USECS_IRQ
;
1544 if (coalesce
->rx_max_coalesced_frames_irq
)
1545 nonzero_params
|= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ
;
1546 if (coalesce
->tx_coalesce_usecs
)
1547 nonzero_params
|= ETHTOOL_COALESCE_TX_USECS
;
1548 if (coalesce
->tx_max_coalesced_frames
)
1549 nonzero_params
|= ETHTOOL_COALESCE_TX_MAX_FRAMES
;
1550 if (coalesce
->tx_coalesce_usecs_irq
)
1551 nonzero_params
|= ETHTOOL_COALESCE_TX_USECS_IRQ
;
1552 if (coalesce
->tx_max_coalesced_frames_irq
)
1553 nonzero_params
|= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ
;
1554 if (coalesce
->stats_block_coalesce_usecs
)
1555 nonzero_params
|= ETHTOOL_COALESCE_STATS_BLOCK_USECS
;
1556 if (coalesce
->use_adaptive_rx_coalesce
)
1557 nonzero_params
|= ETHTOOL_COALESCE_USE_ADAPTIVE_RX
;
1558 if (coalesce
->use_adaptive_tx_coalesce
)
1559 nonzero_params
|= ETHTOOL_COALESCE_USE_ADAPTIVE_TX
;
1560 if (coalesce
->pkt_rate_low
)
1561 nonzero_params
|= ETHTOOL_COALESCE_PKT_RATE_LOW
;
1562 if (coalesce
->rx_coalesce_usecs_low
)
1563 nonzero_params
|= ETHTOOL_COALESCE_RX_USECS_LOW
;
1564 if (coalesce
->rx_max_coalesced_frames_low
)
1565 nonzero_params
|= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW
;
1566 if (coalesce
->tx_coalesce_usecs_low
)
1567 nonzero_params
|= ETHTOOL_COALESCE_TX_USECS_LOW
;
1568 if (coalesce
->tx_max_coalesced_frames_low
)
1569 nonzero_params
|= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW
;
1570 if (coalesce
->pkt_rate_high
)
1571 nonzero_params
|= ETHTOOL_COALESCE_PKT_RATE_HIGH
;
1572 if (coalesce
->rx_coalesce_usecs_high
)
1573 nonzero_params
|= ETHTOOL_COALESCE_RX_USECS_HIGH
;
1574 if (coalesce
->rx_max_coalesced_frames_high
)
1575 nonzero_params
|= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH
;
1576 if (coalesce
->tx_coalesce_usecs_high
)
1577 nonzero_params
|= ETHTOOL_COALESCE_TX_USECS_HIGH
;
1578 if (coalesce
->tx_max_coalesced_frames_high
)
1579 nonzero_params
|= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH
;
1580 if (coalesce
->rate_sample_interval
)
1581 nonzero_params
|= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL
;
1583 return (supported_params
& nonzero_params
) == nonzero_params
;
1586 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
1587 void __user
*useraddr
)
1589 struct ethtool_coalesce coalesce
;
1592 if (!dev
->ethtool_ops
->set_coalesce
)
1595 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
1598 if (!ethtool_set_coalesce_supported(dev
, &coalesce
))
1601 ret
= dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
1603 ethtool_notify(dev
, ETHTOOL_MSG_COALESCE_NTF
, NULL
);
1607 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1609 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
1611 if (!dev
->ethtool_ops
->get_ringparam
)
1614 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
1616 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
1621 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1623 struct ethtool_ringparam ringparam
, max
= { .cmd
= ETHTOOL_GRINGPARAM
};
1626 if (!dev
->ethtool_ops
->set_ringparam
|| !dev
->ethtool_ops
->get_ringparam
)
1629 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
1632 dev
->ethtool_ops
->get_ringparam(dev
, &max
);
1634 /* ensure new ring parameters are within the maximums */
1635 if (ringparam
.rx_pending
> max
.rx_max_pending
||
1636 ringparam
.rx_mini_pending
> max
.rx_mini_max_pending
||
1637 ringparam
.rx_jumbo_pending
> max
.rx_jumbo_max_pending
||
1638 ringparam
.tx_pending
> max
.tx_max_pending
)
1641 ret
= dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
1643 ethtool_notify(dev
, ETHTOOL_MSG_RINGS_NTF
, NULL
);
1647 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
1648 void __user
*useraddr
)
1650 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
1652 if (!dev
->ethtool_ops
->get_channels
)
1655 dev
->ethtool_ops
->get_channels(dev
, &channels
);
1657 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
1662 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
1663 void __user
*useraddr
)
1665 struct ethtool_channels channels
, curr
= { .cmd
= ETHTOOL_GCHANNELS
};
1666 u16 from_channel
, to_channel
;
1667 u32 max_rx_in_use
= 0;
1671 if (!dev
->ethtool_ops
->set_channels
|| !dev
->ethtool_ops
->get_channels
)
1674 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
1677 dev
->ethtool_ops
->get_channels(dev
, &curr
);
1679 if (channels
.rx_count
== curr
.rx_count
&&
1680 channels
.tx_count
== curr
.tx_count
&&
1681 channels
.combined_count
== curr
.combined_count
&&
1682 channels
.other_count
== curr
.other_count
)
1685 /* ensure new counts are within the maximums */
1686 if (channels
.rx_count
> curr
.max_rx
||
1687 channels
.tx_count
> curr
.max_tx
||
1688 channels
.combined_count
> curr
.max_combined
||
1689 channels
.other_count
> curr
.max_other
)
1692 /* ensure there is at least one RX and one TX channel */
1693 if (!channels
.combined_count
&&
1694 (!channels
.rx_count
|| !channels
.tx_count
))
1697 /* ensure the new Rx count fits within the configured Rx flow
1698 * indirection table settings */
1699 if (netif_is_rxfh_configured(dev
) &&
1700 !ethtool_get_max_rxfh_channel(dev
, &max_rx_in_use
) &&
1701 (channels
.combined_count
+ channels
.rx_count
) <= max_rx_in_use
)
1704 /* Disabling channels, query zero-copy AF_XDP sockets */
1705 from_channel
= channels
.combined_count
+
1706 min(channels
.rx_count
, channels
.tx_count
);
1707 to_channel
= curr
.combined_count
+ max(curr
.rx_count
, curr
.tx_count
);
1708 for (i
= from_channel
; i
< to_channel
; i
++)
1709 if (xsk_get_pool_from_qid(dev
, i
))
1712 ret
= dev
->ethtool_ops
->set_channels(dev
, &channels
);
1714 ethtool_notify(dev
, ETHTOOL_MSG_CHANNELS_NTF
, NULL
);
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
;
1737 if (!dev
->ethtool_ops
->set_pauseparam
)
1740 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
1743 ret
= dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
1745 ethtool_notify(dev
, ETHTOOL_MSG_PAUSE_NTF
, NULL
);
1749 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1751 struct ethtool_test test
;
1752 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1756 if (!ops
->self_test
|| !ops
->get_sset_count
)
1759 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1762 WARN_ON(test_len
== 0);
1764 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1767 test
.len
= test_len
;
1768 data
= kmalloc_array(test_len
, sizeof(u64
), GFP_USER
);
1772 netif_testing_on(dev
);
1773 ops
->self_test(dev
, &test
, data
);
1774 netif_testing_off(dev
);
1777 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1779 useraddr
+= sizeof(test
);
1780 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1789 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1791 struct ethtool_gstrings gstrings
;
1795 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1798 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1801 if (ret
> S32_MAX
/ ETH_GSTRING_LEN
)
1808 data
= vzalloc(array_size(gstrings
.len
, ETH_GSTRING_LEN
));
1812 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1818 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1820 useraddr
+= sizeof(gstrings
);
1822 copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1831 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1833 struct ethtool_value id
;
1835 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1838 if (!ops
->set_phys_id
)
1844 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1847 rc
= ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1851 /* Drop the RTNL lock while waiting, but prevent reentry or
1852 * removal of the device.
1859 /* Driver will handle this itself */
1860 schedule_timeout_interruptible(
1861 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1863 /* Driver expects to be called at twice the frequency in rc */
1864 int n
= rc
* 2, interval
= HZ
/ n
;
1865 u64 count
= n
* id
.data
, i
= 0;
1869 rc
= ops
->set_phys_id(dev
,
1870 (i
++ & 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1874 schedule_timeout_interruptible(interval
);
1875 } while (!signal_pending(current
) && (!id
.data
|| i
< count
));
1882 (void) ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1886 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1888 struct ethtool_stats stats
;
1889 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1893 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1896 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1899 if (n_stats
> S32_MAX
/ sizeof(u64
))
1901 WARN_ON_ONCE(!n_stats
);
1902 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1905 stats
.n_stats
= n_stats
;
1908 data
= vzalloc(array_size(n_stats
, sizeof(u64
)));
1911 ops
->get_ethtool_stats(dev
, &stats
, data
);
1917 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1919 useraddr
+= sizeof(stats
);
1920 if (n_stats
&& copy_to_user(useraddr
, data
, array_size(n_stats
, sizeof(u64
))))
1929 static int ethtool_get_phy_stats(struct net_device
*dev
, void __user
*useraddr
)
1931 const struct ethtool_phy_ops
*phy_ops
= ethtool_phy_ops
;
1932 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1933 struct phy_device
*phydev
= dev
->phydev
;
1934 struct ethtool_stats stats
;
1938 if (!phydev
&& (!ops
->get_ethtool_phy_stats
|| !ops
->get_sset_count
))
1941 if (dev
->phydev
&& !ops
->get_ethtool_phy_stats
&&
1942 phy_ops
&& phy_ops
->get_sset_count
)
1943 n_stats
= phy_ops
->get_sset_count(dev
->phydev
);
1945 n_stats
= ops
->get_sset_count(dev
, ETH_SS_PHY_STATS
);
1948 if (n_stats
> S32_MAX
/ sizeof(u64
))
1950 WARN_ON_ONCE(!n_stats
);
1952 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1955 stats
.n_stats
= n_stats
;
1958 data
= vzalloc(array_size(n_stats
, sizeof(u64
)));
1962 if (dev
->phydev
&& !ops
->get_ethtool_phy_stats
&&
1963 phy_ops
&& phy_ops
->get_stats
) {
1964 ret
= phy_ops
->get_stats(dev
->phydev
, &stats
, data
);
1968 ops
->get_ethtool_phy_stats(dev
, &stats
, data
);
1975 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1977 useraddr
+= sizeof(stats
);
1978 if (n_stats
&& copy_to_user(useraddr
, data
, array_size(n_stats
, sizeof(u64
))))
1987 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1989 struct ethtool_perm_addr epaddr
;
1991 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1994 if (epaddr
.size
< dev
->addr_len
)
1996 epaddr
.size
= dev
->addr_len
;
1998 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
2000 useraddr
+= sizeof(epaddr
);
2001 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
2006 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
2007 u32 cmd
, u32 (*actor
)(struct net_device
*))
2009 struct ethtool_value edata
= { .cmd
= cmd
};
2014 edata
.data
= actor(dev
);
2016 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
2021 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
2022 void (*actor
)(struct net_device
*, u32
))
2024 struct ethtool_value edata
;
2029 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2032 actor(dev
, edata
.data
);
2036 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
2037 int (*actor
)(struct net_device
*, u32
))
2039 struct ethtool_value edata
;
2044 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2047 return actor(dev
, edata
.data
);
2050 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
2051 char __user
*useraddr
)
2053 struct ethtool_flash efl
;
2055 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
2057 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
2059 if (!dev
->ethtool_ops
->flash_device
)
2060 return devlink_compat_flash_update(dev
, efl
.data
);
2062 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
2065 static int ethtool_set_dump(struct net_device
*dev
,
2066 void __user
*useraddr
)
2068 struct ethtool_dump dump
;
2070 if (!dev
->ethtool_ops
->set_dump
)
2073 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2076 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
2079 static int ethtool_get_dump_flag(struct net_device
*dev
,
2080 void __user
*useraddr
)
2083 struct ethtool_dump dump
;
2084 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2086 if (!ops
->get_dump_flag
)
2089 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2092 ret
= ops
->get_dump_flag(dev
, &dump
);
2096 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
2101 static int ethtool_get_dump_data(struct net_device
*dev
,
2102 void __user
*useraddr
)
2106 struct ethtool_dump dump
, tmp
;
2107 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2110 if (!ops
->get_dump_data
|| !ops
->get_dump_flag
)
2113 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2116 memset(&tmp
, 0, sizeof(tmp
));
2117 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
2118 ret
= ops
->get_dump_flag(dev
, &tmp
);
2122 len
= min(tmp
.len
, dump
.len
);
2126 /* Don't ever let the driver think there's more space available
2127 * than it requested with .get_dump_flag().
2131 /* Always allocate enough space to hold the whole thing so that the
2132 * driver does not need to check the length and bother with partial
2135 data
= vzalloc(tmp
.len
);
2138 ret
= ops
->get_dump_data(dev
, &dump
, data
);
2142 /* There are two sane possibilities:
2143 * 1. The driver's .get_dump_data() does not touch dump.len.
2144 * 2. Or it may set dump.len to how much it really writes, which
2145 * should be tmp.len (or len if it can do a partial dump).
2146 * In any case respond to userspace with the actual length of data
2149 WARN_ON(dump
.len
!= len
&& dump
.len
!= tmp
.len
);
2152 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
2156 useraddr
+= offsetof(struct ethtool_dump
, data
);
2157 if (copy_to_user(useraddr
, data
, len
))
2164 static int ethtool_get_ts_info(struct net_device
*dev
, void __user
*useraddr
)
2166 struct ethtool_ts_info info
;
2169 err
= __ethtool_get_ts_info(dev
, &info
);
2173 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
2179 static int __ethtool_get_module_info(struct net_device
*dev
,
2180 struct ethtool_modinfo
*modinfo
)
2182 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2183 struct phy_device
*phydev
= dev
->phydev
;
2186 return sfp_get_module_info(dev
->sfp_bus
, modinfo
);
2188 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_info
)
2189 return phydev
->drv
->module_info(phydev
, modinfo
);
2191 if (ops
->get_module_info
)
2192 return ops
->get_module_info(dev
, modinfo
);
2197 static int ethtool_get_module_info(struct net_device
*dev
,
2198 void __user
*useraddr
)
2201 struct ethtool_modinfo modinfo
;
2203 if (copy_from_user(&modinfo
, useraddr
, sizeof(modinfo
)))
2206 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2210 if (copy_to_user(useraddr
, &modinfo
, sizeof(modinfo
)))
2216 static int __ethtool_get_module_eeprom(struct net_device
*dev
,
2217 struct ethtool_eeprom
*ee
, u8
*data
)
2219 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2220 struct phy_device
*phydev
= dev
->phydev
;
2223 return sfp_get_module_eeprom(dev
->sfp_bus
, ee
, data
);
2225 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_eeprom
)
2226 return phydev
->drv
->module_eeprom(phydev
, ee
, data
);
2228 if (ops
->get_module_eeprom
)
2229 return ops
->get_module_eeprom(dev
, ee
, data
);
2234 static int ethtool_get_module_eeprom(struct net_device
*dev
,
2235 void __user
*useraddr
)
2238 struct ethtool_modinfo modinfo
;
2240 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2244 return ethtool_get_any_eeprom(dev
, useraddr
,
2245 __ethtool_get_module_eeprom
,
2246 modinfo
.eeprom_len
);
2249 static int ethtool_tunable_valid(const struct ethtool_tunable
*tuna
)
2252 case ETHTOOL_RX_COPYBREAK
:
2253 case ETHTOOL_TX_COPYBREAK
:
2254 if (tuna
->len
!= sizeof(u32
) ||
2255 tuna
->type_id
!= ETHTOOL_TUNABLE_U32
)
2258 case ETHTOOL_PFC_PREVENTION_TOUT
:
2259 if (tuna
->len
!= sizeof(u16
) ||
2260 tuna
->type_id
!= ETHTOOL_TUNABLE_U16
)
2270 static int ethtool_get_tunable(struct net_device
*dev
, void __user
*useraddr
)
2273 struct ethtool_tunable tuna
;
2274 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2277 if (!ops
->get_tunable
)
2279 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2281 ret
= ethtool_tunable_valid(&tuna
);
2284 data
= kmalloc(tuna
.len
, GFP_USER
);
2287 ret
= ops
->get_tunable(dev
, &tuna
, data
);
2290 useraddr
+= sizeof(tuna
);
2292 if (copy_to_user(useraddr
, data
, tuna
.len
))
2301 static int ethtool_set_tunable(struct net_device
*dev
, void __user
*useraddr
)
2304 struct ethtool_tunable tuna
;
2305 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2308 if (!ops
->set_tunable
)
2310 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2312 ret
= ethtool_tunable_valid(&tuna
);
2315 useraddr
+= sizeof(tuna
);
2316 data
= memdup_user(useraddr
, tuna
.len
);
2318 return PTR_ERR(data
);
2319 ret
= ops
->set_tunable(dev
, &tuna
, data
);
2325 static noinline_for_stack
int
2326 ethtool_get_per_queue_coalesce(struct net_device
*dev
,
2327 void __user
*useraddr
,
2328 struct ethtool_per_queue_op
*per_queue_opt
)
2332 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2334 if (!dev
->ethtool_ops
->get_per_queue_coalesce
)
2337 useraddr
+= sizeof(*per_queue_opt
);
2339 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
,
2342 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2343 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
2345 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, &coalesce
);
2348 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
2350 useraddr
+= sizeof(coalesce
);
2356 static noinline_for_stack
int
2357 ethtool_set_per_queue_coalesce(struct net_device
*dev
,
2358 void __user
*useraddr
,
2359 struct ethtool_per_queue_op
*per_queue_opt
)
2364 struct ethtool_coalesce
*backup
= NULL
, *tmp
= NULL
;
2365 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2367 if ((!dev
->ethtool_ops
->set_per_queue_coalesce
) ||
2368 (!dev
->ethtool_ops
->get_per_queue_coalesce
))
2371 useraddr
+= sizeof(*per_queue_opt
);
2373 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
, MAX_NUM_QUEUE
);
2374 n_queue
= bitmap_weight(queue_mask
, MAX_NUM_QUEUE
);
2375 tmp
= backup
= kmalloc_array(n_queue
, sizeof(*backup
), GFP_KERNEL
);
2379 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2380 struct ethtool_coalesce coalesce
;
2382 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, tmp
);
2388 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
))) {
2393 if (!ethtool_set_coalesce_supported(dev
, &coalesce
)) {
2398 ret
= dev
->ethtool_ops
->set_per_queue_coalesce(dev
, bit
, &coalesce
);
2402 useraddr
+= sizeof(coalesce
);
2408 for_each_set_bit(i
, queue_mask
, bit
) {
2409 dev
->ethtool_ops
->set_per_queue_coalesce(dev
, i
, tmp
);
2418 static int noinline_for_stack
ethtool_set_per_queue(struct net_device
*dev
,
2419 void __user
*useraddr
, u32 sub_cmd
)
2421 struct ethtool_per_queue_op per_queue_opt
;
2423 if (copy_from_user(&per_queue_opt
, useraddr
, sizeof(per_queue_opt
)))
2426 if (per_queue_opt
.sub_command
!= sub_cmd
)
2429 switch (per_queue_opt
.sub_command
) {
2430 case ETHTOOL_GCOALESCE
:
2431 return ethtool_get_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2432 case ETHTOOL_SCOALESCE
:
2433 return ethtool_set_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2439 static int ethtool_phy_tunable_valid(const struct ethtool_tunable
*tuna
)
2442 case ETHTOOL_PHY_DOWNSHIFT
:
2443 case ETHTOOL_PHY_FAST_LINK_DOWN
:
2444 if (tuna
->len
!= sizeof(u8
) ||
2445 tuna
->type_id
!= ETHTOOL_TUNABLE_U8
)
2448 case ETHTOOL_PHY_EDPD
:
2449 if (tuna
->len
!= sizeof(u16
) ||
2450 tuna
->type_id
!= ETHTOOL_TUNABLE_U16
)
2460 static int get_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2462 struct phy_device
*phydev
= dev
->phydev
;
2463 struct ethtool_tunable tuna
;
2464 bool phy_drv_tunable
;
2468 phy_drv_tunable
= phydev
&& phydev
->drv
&& phydev
->drv
->get_tunable
;
2469 if (!phy_drv_tunable
&& !dev
->ethtool_ops
->get_phy_tunable
)
2471 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2473 ret
= ethtool_phy_tunable_valid(&tuna
);
2476 data
= kmalloc(tuna
.len
, GFP_USER
);
2479 if (phy_drv_tunable
) {
2480 mutex_lock(&phydev
->lock
);
2481 ret
= phydev
->drv
->get_tunable(phydev
, &tuna
, data
);
2482 mutex_unlock(&phydev
->lock
);
2484 ret
= dev
->ethtool_ops
->get_phy_tunable(dev
, &tuna
, data
);
2488 useraddr
+= sizeof(tuna
);
2490 if (copy_to_user(useraddr
, data
, tuna
.len
))
2499 static int set_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2501 struct phy_device
*phydev
= dev
->phydev
;
2502 struct ethtool_tunable tuna
;
2503 bool phy_drv_tunable
;
2507 phy_drv_tunable
= phydev
&& phydev
->drv
&& phydev
->drv
->get_tunable
;
2508 if (!phy_drv_tunable
&& !dev
->ethtool_ops
->set_phy_tunable
)
2510 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2512 ret
= ethtool_phy_tunable_valid(&tuna
);
2515 useraddr
+= sizeof(tuna
);
2516 data
= memdup_user(useraddr
, tuna
.len
);
2518 return PTR_ERR(data
);
2519 if (phy_drv_tunable
) {
2520 mutex_lock(&phydev
->lock
);
2521 ret
= phydev
->drv
->set_tunable(phydev
, &tuna
, data
);
2522 mutex_unlock(&phydev
->lock
);
2524 ret
= dev
->ethtool_ops
->set_phy_tunable(dev
, &tuna
, data
);
2531 static int ethtool_get_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2533 struct ethtool_fecparam fecparam
= { .cmd
= ETHTOOL_GFECPARAM
};
2536 if (!dev
->ethtool_ops
->get_fecparam
)
2539 rc
= dev
->ethtool_ops
->get_fecparam(dev
, &fecparam
);
2543 if (copy_to_user(useraddr
, &fecparam
, sizeof(fecparam
)))
2548 static int ethtool_set_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2550 struct ethtool_fecparam fecparam
;
2552 if (!dev
->ethtool_ops
->set_fecparam
)
2555 if (copy_from_user(&fecparam
, useraddr
, sizeof(fecparam
)))
2558 return dev
->ethtool_ops
->set_fecparam(dev
, &fecparam
);
2561 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2563 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
2565 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
2566 void __user
*useraddr
= ifr
->ifr_data
;
2567 u32 ethcmd
, sub_cmd
;
2569 netdev_features_t old_features
;
2571 if (!dev
|| !netif_device_present(dev
))
2574 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
2577 if (ethcmd
== ETHTOOL_PERQUEUE
) {
2578 if (copy_from_user(&sub_cmd
, useraddr
+ sizeof(ethcmd
), sizeof(sub_cmd
)))
2583 /* Allow some commands to be done by anyone */
2586 case ETHTOOL_GDRVINFO
:
2587 case ETHTOOL_GMSGLVL
:
2589 case ETHTOOL_GCOALESCE
:
2590 case ETHTOOL_GRINGPARAM
:
2591 case ETHTOOL_GPAUSEPARAM
:
2592 case ETHTOOL_GRXCSUM
:
2593 case ETHTOOL_GTXCSUM
:
2595 case ETHTOOL_GSSET_INFO
:
2596 case ETHTOOL_GSTRINGS
:
2597 case ETHTOOL_GSTATS
:
2598 case ETHTOOL_GPHYSTATS
:
2600 case ETHTOOL_GPERMADDR
:
2604 case ETHTOOL_GFLAGS
:
2605 case ETHTOOL_GPFLAGS
:
2607 case ETHTOOL_GRXRINGS
:
2608 case ETHTOOL_GRXCLSRLCNT
:
2609 case ETHTOOL_GRXCLSRULE
:
2610 case ETHTOOL_GRXCLSRLALL
:
2611 case ETHTOOL_GRXFHINDIR
:
2613 case ETHTOOL_GFEATURES
:
2614 case ETHTOOL_GCHANNELS
:
2615 case ETHTOOL_GET_TS_INFO
:
2617 case ETHTOOL_GTUNABLE
:
2618 case ETHTOOL_PHY_GTUNABLE
:
2619 case ETHTOOL_GLINKSETTINGS
:
2620 case ETHTOOL_GFECPARAM
:
2623 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2627 if (dev
->ethtool_ops
->begin
) {
2628 rc
= dev
->ethtool_ops
->begin(dev
);
2632 old_features
= dev
->features
;
2636 rc
= ethtool_get_settings(dev
, useraddr
);
2639 rc
= ethtool_set_settings(dev
, useraddr
);
2641 case ETHTOOL_GDRVINFO
:
2642 rc
= ethtool_get_drvinfo(dev
, useraddr
);
2645 rc
= ethtool_get_regs(dev
, useraddr
);
2648 rc
= ethtool_get_wol(dev
, useraddr
);
2651 rc
= ethtool_set_wol(dev
, useraddr
);
2653 case ETHTOOL_GMSGLVL
:
2654 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2655 dev
->ethtool_ops
->get_msglevel
);
2657 case ETHTOOL_SMSGLVL
:
2658 rc
= ethtool_set_value_void(dev
, useraddr
,
2659 dev
->ethtool_ops
->set_msglevel
);
2661 ethtool_notify(dev
, ETHTOOL_MSG_DEBUG_NTF
, NULL
);
2664 rc
= ethtool_get_eee(dev
, useraddr
);
2667 rc
= ethtool_set_eee(dev
, useraddr
);
2669 case ETHTOOL_NWAY_RST
:
2670 rc
= ethtool_nway_reset(dev
);
2673 rc
= ethtool_get_link(dev
, useraddr
);
2675 case ETHTOOL_GEEPROM
:
2676 rc
= ethtool_get_eeprom(dev
, useraddr
);
2678 case ETHTOOL_SEEPROM
:
2679 rc
= ethtool_set_eeprom(dev
, useraddr
);
2681 case ETHTOOL_GCOALESCE
:
2682 rc
= ethtool_get_coalesce(dev
, useraddr
);
2684 case ETHTOOL_SCOALESCE
:
2685 rc
= ethtool_set_coalesce(dev
, useraddr
);
2687 case ETHTOOL_GRINGPARAM
:
2688 rc
= ethtool_get_ringparam(dev
, useraddr
);
2690 case ETHTOOL_SRINGPARAM
:
2691 rc
= ethtool_set_ringparam(dev
, useraddr
);
2693 case ETHTOOL_GPAUSEPARAM
:
2694 rc
= ethtool_get_pauseparam(dev
, useraddr
);
2696 case ETHTOOL_SPAUSEPARAM
:
2697 rc
= ethtool_set_pauseparam(dev
, useraddr
);
2700 rc
= ethtool_self_test(dev
, useraddr
);
2702 case ETHTOOL_GSTRINGS
:
2703 rc
= ethtool_get_strings(dev
, useraddr
);
2705 case ETHTOOL_PHYS_ID
:
2706 rc
= ethtool_phys_id(dev
, useraddr
);
2708 case ETHTOOL_GSTATS
:
2709 rc
= ethtool_get_stats(dev
, useraddr
);
2711 case ETHTOOL_GPERMADDR
:
2712 rc
= ethtool_get_perm_addr(dev
, useraddr
);
2714 case ETHTOOL_GFLAGS
:
2715 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2716 __ethtool_get_flags
);
2718 case ETHTOOL_SFLAGS
:
2719 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
2721 case ETHTOOL_GPFLAGS
:
2722 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2723 dev
->ethtool_ops
->get_priv_flags
);
2725 ethtool_notify(dev
, ETHTOOL_MSG_PRIVFLAGS_NTF
, NULL
);
2727 case ETHTOOL_SPFLAGS
:
2728 rc
= ethtool_set_value(dev
, useraddr
,
2729 dev
->ethtool_ops
->set_priv_flags
);
2732 case ETHTOOL_GRXRINGS
:
2733 case ETHTOOL_GRXCLSRLCNT
:
2734 case ETHTOOL_GRXCLSRULE
:
2735 case ETHTOOL_GRXCLSRLALL
:
2736 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
2739 case ETHTOOL_SRXCLSRLDEL
:
2740 case ETHTOOL_SRXCLSRLINS
:
2741 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
2743 case ETHTOOL_FLASHDEV
:
2744 rc
= ethtool_flash_device(dev
, useraddr
);
2747 rc
= ethtool_reset(dev
, useraddr
);
2749 case ETHTOOL_GSSET_INFO
:
2750 rc
= ethtool_get_sset_info(dev
, useraddr
);
2752 case ETHTOOL_GRXFHINDIR
:
2753 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2755 case ETHTOOL_SRXFHINDIR
:
2756 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2759 rc
= ethtool_get_rxfh(dev
, useraddr
);
2762 rc
= ethtool_set_rxfh(dev
, useraddr
);
2764 case ETHTOOL_GFEATURES
:
2765 rc
= ethtool_get_features(dev
, useraddr
);
2767 case ETHTOOL_SFEATURES
:
2768 rc
= ethtool_set_features(dev
, useraddr
);
2770 case ETHTOOL_GTXCSUM
:
2771 case ETHTOOL_GRXCSUM
:
2776 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2778 case ETHTOOL_STXCSUM
:
2779 case ETHTOOL_SRXCSUM
:
2784 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2786 case ETHTOOL_GCHANNELS
:
2787 rc
= ethtool_get_channels(dev
, useraddr
);
2789 case ETHTOOL_SCHANNELS
:
2790 rc
= ethtool_set_channels(dev
, useraddr
);
2792 case ETHTOOL_SET_DUMP
:
2793 rc
= ethtool_set_dump(dev
, useraddr
);
2795 case ETHTOOL_GET_DUMP_FLAG
:
2796 rc
= ethtool_get_dump_flag(dev
, useraddr
);
2798 case ETHTOOL_GET_DUMP_DATA
:
2799 rc
= ethtool_get_dump_data(dev
, useraddr
);
2801 case ETHTOOL_GET_TS_INFO
:
2802 rc
= ethtool_get_ts_info(dev
, useraddr
);
2804 case ETHTOOL_GMODULEINFO
:
2805 rc
= ethtool_get_module_info(dev
, useraddr
);
2807 case ETHTOOL_GMODULEEEPROM
:
2808 rc
= ethtool_get_module_eeprom(dev
, useraddr
);
2810 case ETHTOOL_GTUNABLE
:
2811 rc
= ethtool_get_tunable(dev
, useraddr
);
2813 case ETHTOOL_STUNABLE
:
2814 rc
= ethtool_set_tunable(dev
, useraddr
);
2816 case ETHTOOL_GPHYSTATS
:
2817 rc
= ethtool_get_phy_stats(dev
, useraddr
);
2819 case ETHTOOL_PERQUEUE
:
2820 rc
= ethtool_set_per_queue(dev
, useraddr
, sub_cmd
);
2822 case ETHTOOL_GLINKSETTINGS
:
2823 rc
= ethtool_get_link_ksettings(dev
, useraddr
);
2825 case ETHTOOL_SLINKSETTINGS
:
2826 rc
= ethtool_set_link_ksettings(dev
, useraddr
);
2828 case ETHTOOL_PHY_GTUNABLE
:
2829 rc
= get_phy_tunable(dev
, useraddr
);
2831 case ETHTOOL_PHY_STUNABLE
:
2832 rc
= set_phy_tunable(dev
, useraddr
);
2834 case ETHTOOL_GFECPARAM
:
2835 rc
= ethtool_get_fecparam(dev
, useraddr
);
2837 case ETHTOOL_SFECPARAM
:
2838 rc
= ethtool_set_fecparam(dev
, useraddr
);
2844 if (dev
->ethtool_ops
->complete
)
2845 dev
->ethtool_ops
->complete(dev
);
2847 if (old_features
!= dev
->features
)
2848 netdev_features_change(dev
);
2853 struct ethtool_rx_flow_key
{
2854 struct flow_dissector_key_basic basic
;
2856 struct flow_dissector_key_ipv4_addrs ipv4
;
2857 struct flow_dissector_key_ipv6_addrs ipv6
;
2859 struct flow_dissector_key_ports tp
;
2860 struct flow_dissector_key_ip ip
;
2861 struct flow_dissector_key_vlan vlan
;
2862 struct flow_dissector_key_eth_addrs eth_addrs
;
2863 } __aligned(BITS_PER_LONG
/ 8); /* Ensure that we can do comparisons as longs. */
2865 struct ethtool_rx_flow_match
{
2866 struct flow_dissector dissector
;
2867 struct ethtool_rx_flow_key key
;
2868 struct ethtool_rx_flow_key mask
;
2871 struct ethtool_rx_flow_rule
*
2872 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input
*input
)
2874 const struct ethtool_rx_flow_spec
*fs
= input
->fs
;
2875 static struct in6_addr zero_addr
= {};
2876 struct ethtool_rx_flow_match
*match
;
2877 struct ethtool_rx_flow_rule
*flow
;
2878 struct flow_action_entry
*act
;
2880 flow
= kzalloc(sizeof(struct ethtool_rx_flow_rule
) +
2881 sizeof(struct ethtool_rx_flow_match
), GFP_KERNEL
);
2883 return ERR_PTR(-ENOMEM
);
2885 /* ethtool_rx supports only one single action per rule. */
2886 flow
->rule
= flow_rule_alloc(1);
2889 return ERR_PTR(-ENOMEM
);
2892 match
= (struct ethtool_rx_flow_match
*)flow
->priv
;
2893 flow
->rule
->match
.dissector
= &match
->dissector
;
2894 flow
->rule
->match
.mask
= &match
->mask
;
2895 flow
->rule
->match
.key
= &match
->key
;
2897 match
->mask
.basic
.n_proto
= htons(0xffff);
2899 switch (fs
->flow_type
& ~(FLOW_EXT
| FLOW_MAC_EXT
| FLOW_RSS
)) {
2901 const struct ethhdr
*ether_spec
, *ether_m_spec
;
2903 ether_spec
= &fs
->h_u
.ether_spec
;
2904 ether_m_spec
= &fs
->m_u
.ether_spec
;
2906 if (!is_zero_ether_addr(ether_m_spec
->h_source
)) {
2907 ether_addr_copy(match
->key
.eth_addrs
.src
,
2908 ether_spec
->h_source
);
2909 ether_addr_copy(match
->mask
.eth_addrs
.src
,
2910 ether_m_spec
->h_source
);
2912 if (!is_zero_ether_addr(ether_m_spec
->h_dest
)) {
2913 ether_addr_copy(match
->key
.eth_addrs
.dst
,
2914 ether_spec
->h_dest
);
2915 ether_addr_copy(match
->mask
.eth_addrs
.dst
,
2916 ether_m_spec
->h_dest
);
2918 if (ether_m_spec
->h_proto
) {
2919 match
->key
.basic
.n_proto
= ether_spec
->h_proto
;
2920 match
->mask
.basic
.n_proto
= ether_m_spec
->h_proto
;
2926 const struct ethtool_tcpip4_spec
*v4_spec
, *v4_m_spec
;
2928 match
->key
.basic
.n_proto
= htons(ETH_P_IP
);
2930 v4_spec
= &fs
->h_u
.tcp_ip4_spec
;
2931 v4_m_spec
= &fs
->m_u
.tcp_ip4_spec
;
2933 if (v4_m_spec
->ip4src
) {
2934 match
->key
.ipv4
.src
= v4_spec
->ip4src
;
2935 match
->mask
.ipv4
.src
= v4_m_spec
->ip4src
;
2937 if (v4_m_spec
->ip4dst
) {
2938 match
->key
.ipv4
.dst
= v4_spec
->ip4dst
;
2939 match
->mask
.ipv4
.dst
= v4_m_spec
->ip4dst
;
2941 if (v4_m_spec
->ip4src
||
2942 v4_m_spec
->ip4dst
) {
2943 match
->dissector
.used_keys
|=
2944 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS
);
2945 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IPV4_ADDRS
] =
2946 offsetof(struct ethtool_rx_flow_key
, ipv4
);
2948 if (v4_m_spec
->psrc
) {
2949 match
->key
.tp
.src
= v4_spec
->psrc
;
2950 match
->mask
.tp
.src
= v4_m_spec
->psrc
;
2952 if (v4_m_spec
->pdst
) {
2953 match
->key
.tp
.dst
= v4_spec
->pdst
;
2954 match
->mask
.tp
.dst
= v4_m_spec
->pdst
;
2956 if (v4_m_spec
->psrc
||
2958 match
->dissector
.used_keys
|=
2959 BIT(FLOW_DISSECTOR_KEY_PORTS
);
2960 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_PORTS
] =
2961 offsetof(struct ethtool_rx_flow_key
, tp
);
2963 if (v4_m_spec
->tos
) {
2964 match
->key
.ip
.tos
= v4_spec
->tos
;
2965 match
->mask
.ip
.tos
= v4_m_spec
->tos
;
2966 match
->dissector
.used_keys
|=
2967 BIT(FLOW_DISSECTOR_KEY_IP
);
2968 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IP
] =
2969 offsetof(struct ethtool_rx_flow_key
, ip
);
2975 const struct ethtool_tcpip6_spec
*v6_spec
, *v6_m_spec
;
2977 match
->key
.basic
.n_proto
= htons(ETH_P_IPV6
);
2979 v6_spec
= &fs
->h_u
.tcp_ip6_spec
;
2980 v6_m_spec
= &fs
->m_u
.tcp_ip6_spec
;
2981 if (memcmp(v6_m_spec
->ip6src
, &zero_addr
, sizeof(zero_addr
))) {
2982 memcpy(&match
->key
.ipv6
.src
, v6_spec
->ip6src
,
2983 sizeof(match
->key
.ipv6
.src
));
2984 memcpy(&match
->mask
.ipv6
.src
, v6_m_spec
->ip6src
,
2985 sizeof(match
->mask
.ipv6
.src
));
2987 if (memcmp(v6_m_spec
->ip6dst
, &zero_addr
, sizeof(zero_addr
))) {
2988 memcpy(&match
->key
.ipv6
.dst
, v6_spec
->ip6dst
,
2989 sizeof(match
->key
.ipv6
.dst
));
2990 memcpy(&match
->mask
.ipv6
.dst
, v6_m_spec
->ip6dst
,
2991 sizeof(match
->mask
.ipv6
.dst
));
2993 if (memcmp(v6_m_spec
->ip6src
, &zero_addr
, sizeof(zero_addr
)) ||
2994 memcmp(v6_m_spec
->ip6dst
, &zero_addr
, sizeof(zero_addr
))) {
2995 match
->dissector
.used_keys
|=
2996 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS
);
2997 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IPV6_ADDRS
] =
2998 offsetof(struct ethtool_rx_flow_key
, ipv6
);
3000 if (v6_m_spec
->psrc
) {
3001 match
->key
.tp
.src
= v6_spec
->psrc
;
3002 match
->mask
.tp
.src
= v6_m_spec
->psrc
;
3004 if (v6_m_spec
->pdst
) {
3005 match
->key
.tp
.dst
= v6_spec
->pdst
;
3006 match
->mask
.tp
.dst
= v6_m_spec
->pdst
;
3008 if (v6_m_spec
->psrc
||
3010 match
->dissector
.used_keys
|=
3011 BIT(FLOW_DISSECTOR_KEY_PORTS
);
3012 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_PORTS
] =
3013 offsetof(struct ethtool_rx_flow_key
, tp
);
3015 if (v6_m_spec
->tclass
) {
3016 match
->key
.ip
.tos
= v6_spec
->tclass
;
3017 match
->mask
.ip
.tos
= v6_m_spec
->tclass
;
3018 match
->dissector
.used_keys
|=
3019 BIT(FLOW_DISSECTOR_KEY_IP
);
3020 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_IP
] =
3021 offsetof(struct ethtool_rx_flow_key
, ip
);
3026 ethtool_rx_flow_rule_destroy(flow
);
3027 return ERR_PTR(-EINVAL
);
3030 switch (fs
->flow_type
& ~(FLOW_EXT
| FLOW_MAC_EXT
| FLOW_RSS
)) {
3033 match
->key
.basic
.ip_proto
= IPPROTO_TCP
;
3034 match
->mask
.basic
.ip_proto
= 0xff;
3038 match
->key
.basic
.ip_proto
= IPPROTO_UDP
;
3039 match
->mask
.basic
.ip_proto
= 0xff;
3043 match
->dissector
.used_keys
|= BIT(FLOW_DISSECTOR_KEY_BASIC
);
3044 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_BASIC
] =
3045 offsetof(struct ethtool_rx_flow_key
, basic
);
3047 if (fs
->flow_type
& FLOW_EXT
) {
3048 const struct ethtool_flow_ext
*ext_h_spec
= &fs
->h_ext
;
3049 const struct ethtool_flow_ext
*ext_m_spec
= &fs
->m_ext
;
3051 if (ext_m_spec
->vlan_etype
) {
3052 match
->key
.vlan
.vlan_tpid
= ext_h_spec
->vlan_etype
;
3053 match
->mask
.vlan
.vlan_tpid
= ext_m_spec
->vlan_etype
;
3056 if (ext_m_spec
->vlan_tci
) {
3057 match
->key
.vlan
.vlan_id
=
3058 ntohs(ext_h_spec
->vlan_tci
) & 0x0fff;
3059 match
->mask
.vlan
.vlan_id
=
3060 ntohs(ext_m_spec
->vlan_tci
) & 0x0fff;
3062 match
->key
.vlan
.vlan_dei
=
3063 !!(ext_h_spec
->vlan_tci
& htons(0x1000));
3064 match
->mask
.vlan
.vlan_dei
=
3065 !!(ext_m_spec
->vlan_tci
& htons(0x1000));
3067 match
->key
.vlan
.vlan_priority
=
3068 (ntohs(ext_h_spec
->vlan_tci
) & 0xe000) >> 13;
3069 match
->mask
.vlan
.vlan_priority
=
3070 (ntohs(ext_m_spec
->vlan_tci
) & 0xe000) >> 13;
3073 if (ext_m_spec
->vlan_etype
||
3074 ext_m_spec
->vlan_tci
) {
3075 match
->dissector
.used_keys
|=
3076 BIT(FLOW_DISSECTOR_KEY_VLAN
);
3077 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_VLAN
] =
3078 offsetof(struct ethtool_rx_flow_key
, vlan
);
3081 if (fs
->flow_type
& FLOW_MAC_EXT
) {
3082 const struct ethtool_flow_ext
*ext_h_spec
= &fs
->h_ext
;
3083 const struct ethtool_flow_ext
*ext_m_spec
= &fs
->m_ext
;
3085 memcpy(match
->key
.eth_addrs
.dst
, ext_h_spec
->h_dest
,
3087 memcpy(match
->mask
.eth_addrs
.dst
, ext_m_spec
->h_dest
,
3090 match
->dissector
.used_keys
|=
3091 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS
);
3092 match
->dissector
.offset
[FLOW_DISSECTOR_KEY_ETH_ADDRS
] =
3093 offsetof(struct ethtool_rx_flow_key
, eth_addrs
);
3096 act
= &flow
->rule
->action
.entries
[0];
3097 switch (fs
->ring_cookie
) {
3098 case RX_CLS_FLOW_DISC
:
3099 act
->id
= FLOW_ACTION_DROP
;
3101 case RX_CLS_FLOW_WAKE
:
3102 act
->id
= FLOW_ACTION_WAKE
;
3105 act
->id
= FLOW_ACTION_QUEUE
;
3106 if (fs
->flow_type
& FLOW_RSS
)
3107 act
->queue
.ctx
= input
->rss_ctx
;
3109 act
->queue
.vf
= ethtool_get_flow_spec_ring_vf(fs
->ring_cookie
);
3110 act
->queue
.index
= ethtool_get_flow_spec_ring(fs
->ring_cookie
);
3116 EXPORT_SYMBOL(ethtool_rx_flow_rule_create
);
3118 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule
*flow
)
3123 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy
);