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/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sched.h>
28 #include <linux/net.h>
31 * Some useful ethtool_ops methods that're device independent.
32 * If we find that all drivers want to do the same thing here,
33 * we can turn these into dev_() function calls.
36 u32
ethtool_op_get_link(struct net_device
*dev
)
38 return netif_carrier_ok(dev
) ? 1 : 0;
40 EXPORT_SYMBOL(ethtool_op_get_link
);
42 int ethtool_op_get_ts_info(struct net_device
*dev
, struct ethtool_ts_info
*info
)
44 info
->so_timestamping
=
45 SOF_TIMESTAMPING_TX_SOFTWARE
|
46 SOF_TIMESTAMPING_RX_SOFTWARE
|
47 SOF_TIMESTAMPING_SOFTWARE
;
51 EXPORT_SYMBOL(ethtool_op_get_ts_info
);
53 /* Handlers for each ethtool command */
55 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
57 static const char netdev_features_strings
[NETDEV_FEATURE_COUNT
][ETH_GSTRING_LEN
] = {
58 [NETIF_F_SG_BIT
] = "tx-scatter-gather",
59 [NETIF_F_IP_CSUM_BIT
] = "tx-checksum-ipv4",
60 [NETIF_F_HW_CSUM_BIT
] = "tx-checksum-ip-generic",
61 [NETIF_F_IPV6_CSUM_BIT
] = "tx-checksum-ipv6",
62 [NETIF_F_HIGHDMA_BIT
] = "highdma",
63 [NETIF_F_FRAGLIST_BIT
] = "tx-scatter-gather-fraglist",
64 [NETIF_F_HW_VLAN_CTAG_TX_BIT
] = "tx-vlan-hw-insert",
66 [NETIF_F_HW_VLAN_CTAG_RX_BIT
] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT
] = "rx-vlan-filter",
68 [NETIF_F_HW_VLAN_STAG_TX_BIT
] = "tx-vlan-stag-hw-insert",
69 [NETIF_F_HW_VLAN_STAG_RX_BIT
] = "rx-vlan-stag-hw-parse",
70 [NETIF_F_HW_VLAN_STAG_FILTER_BIT
] = "rx-vlan-stag-filter",
71 [NETIF_F_VLAN_CHALLENGED_BIT
] = "vlan-challenged",
72 [NETIF_F_GSO_BIT
] = "tx-generic-segmentation",
73 [NETIF_F_LLTX_BIT
] = "tx-lockless",
74 [NETIF_F_NETNS_LOCAL_BIT
] = "netns-local",
75 [NETIF_F_GRO_BIT
] = "rx-gro",
76 [NETIF_F_LRO_BIT
] = "rx-lro",
78 [NETIF_F_TSO_BIT
] = "tx-tcp-segmentation",
79 [NETIF_F_UFO_BIT
] = "tx-udp-fragmentation",
80 [NETIF_F_GSO_ROBUST_BIT
] = "tx-gso-robust",
81 [NETIF_F_TSO_ECN_BIT
] = "tx-tcp-ecn-segmentation",
82 [NETIF_F_TSO6_BIT
] = "tx-tcp6-segmentation",
83 [NETIF_F_FSO_BIT
] = "tx-fcoe-segmentation",
84 [NETIF_F_GSO_GRE_BIT
] = "tx-gre-segmentation",
85 [NETIF_F_GSO_IPIP_BIT
] = "tx-ipip-segmentation",
86 [NETIF_F_GSO_SIT_BIT
] = "tx-sit-segmentation",
87 [NETIF_F_GSO_UDP_TUNNEL_BIT
] = "tx-udp_tnl-segmentation",
89 [NETIF_F_FCOE_CRC_BIT
] = "tx-checksum-fcoe-crc",
90 [NETIF_F_SCTP_CRC_BIT
] = "tx-checksum-sctp",
91 [NETIF_F_FCOE_MTU_BIT
] = "fcoe-mtu",
92 [NETIF_F_NTUPLE_BIT
] = "rx-ntuple-filter",
93 [NETIF_F_RXHASH_BIT
] = "rx-hashing",
94 [NETIF_F_RXCSUM_BIT
] = "rx-checksum",
95 [NETIF_F_NOCACHE_COPY_BIT
] = "tx-nocache-copy",
96 [NETIF_F_LOOPBACK_BIT
] = "loopback",
97 [NETIF_F_RXFCS_BIT
] = "rx-fcs",
98 [NETIF_F_RXALL_BIT
] = "rx-all",
99 [NETIF_F_HW_L2FW_DOFFLOAD_BIT
] = "l2-fwd-offload",
100 [NETIF_F_BUSY_POLL_BIT
] = "busy-poll",
104 rss_hash_func_strings
[ETH_RSS_HASH_FUNCS_COUNT
][ETH_GSTRING_LEN
] = {
105 [ETH_RSS_HASH_TOP_BIT
] = "toeplitz",
106 [ETH_RSS_HASH_XOR_BIT
] = "xor",
110 tunable_strings
[__ETHTOOL_TUNABLE_COUNT
][ETH_GSTRING_LEN
] = {
111 [ETHTOOL_ID_UNSPEC
] = "Unspec",
112 [ETHTOOL_RX_COPYBREAK
] = "rx-copybreak",
113 [ETHTOOL_TX_COPYBREAK
] = "tx-copybreak",
116 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
118 struct ethtool_gfeatures cmd
= {
119 .cmd
= ETHTOOL_GFEATURES
,
120 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
122 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
123 u32 __user
*sizeaddr
;
127 /* in case feature bits run out again */
128 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
130 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
131 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
132 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
133 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
134 features
[i
].never_changed
=
135 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
138 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
139 if (get_user(copy_size
, sizeaddr
))
142 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
143 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
145 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
147 useraddr
+= sizeof(cmd
);
148 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
154 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
156 struct ethtool_sfeatures cmd
;
157 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
158 netdev_features_t wanted
= 0, valid
= 0;
161 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
163 useraddr
+= sizeof(cmd
);
165 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
168 if (copy_from_user(features
, useraddr
, sizeof(features
)))
171 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
172 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
173 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
176 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
179 if (valid
& ~dev
->hw_features
) {
180 valid
&= dev
->hw_features
;
181 ret
|= ETHTOOL_F_UNSUPPORTED
;
184 dev
->wanted_features
&= ~valid
;
185 dev
->wanted_features
|= wanted
& valid
;
186 __netdev_update_features(dev
);
188 if ((dev
->wanted_features
^ dev
->features
) & valid
)
189 ret
|= ETHTOOL_F_WISH
;
194 static int phy_get_sset_count(struct phy_device
*phydev
)
198 if (phydev
->drv
->get_sset_count
&&
199 phydev
->drv
->get_strings
&&
200 phydev
->drv
->get_stats
) {
201 mutex_lock(&phydev
->lock
);
202 ret
= phydev
->drv
->get_sset_count(phydev
);
203 mutex_unlock(&phydev
->lock
);
211 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
213 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
215 if (sset
== ETH_SS_FEATURES
)
216 return ARRAY_SIZE(netdev_features_strings
);
218 if (sset
== ETH_SS_RSS_HASH_FUNCS
)
219 return ARRAY_SIZE(rss_hash_func_strings
);
221 if (sset
== ETH_SS_TUNABLES
)
222 return ARRAY_SIZE(tunable_strings
);
224 if (sset
== ETH_SS_PHY_STATS
) {
226 return phy_get_sset_count(dev
->phydev
);
231 if (ops
->get_sset_count
&& ops
->get_strings
)
232 return ops
->get_sset_count(dev
, sset
);
237 static void __ethtool_get_strings(struct net_device
*dev
,
238 u32 stringset
, u8
*data
)
240 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
242 if (stringset
== ETH_SS_FEATURES
)
243 memcpy(data
, netdev_features_strings
,
244 sizeof(netdev_features_strings
));
245 else if (stringset
== ETH_SS_RSS_HASH_FUNCS
)
246 memcpy(data
, rss_hash_func_strings
,
247 sizeof(rss_hash_func_strings
));
248 else if (stringset
== ETH_SS_TUNABLES
)
249 memcpy(data
, tunable_strings
, sizeof(tunable_strings
));
250 else if (stringset
== ETH_SS_PHY_STATS
) {
251 struct phy_device
*phydev
= dev
->phydev
;
254 mutex_lock(&phydev
->lock
);
255 phydev
->drv
->get_strings(phydev
, data
);
256 mutex_unlock(&phydev
->lock
);
261 /* ops->get_strings is valid because checked earlier */
262 ops
->get_strings(dev
, stringset
, data
);
265 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
267 /* feature masks of legacy discrete ethtool ops */
270 case ETHTOOL_GTXCSUM
:
271 case ETHTOOL_STXCSUM
:
272 return NETIF_F_CSUM_MASK
| NETIF_F_SCTP_CRC
;
273 case ETHTOOL_GRXCSUM
:
274 case ETHTOOL_SRXCSUM
:
275 return NETIF_F_RXCSUM
;
281 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 int __ethtool_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
393 if (!dev
->ethtool_ops
->get_settings
)
396 memset(cmd
, 0, sizeof(struct ethtool_cmd
));
397 cmd
->cmd
= ETHTOOL_GSET
;
398 return dev
->ethtool_ops
->get_settings(dev
, cmd
);
400 EXPORT_SYMBOL(__ethtool_get_settings
);
402 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
405 struct ethtool_cmd cmd
;
407 err
= __ethtool_get_settings(dev
, &cmd
);
411 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
416 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
418 struct ethtool_cmd cmd
;
420 if (!dev
->ethtool_ops
->set_settings
)
423 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
426 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
429 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
430 void __user
*useraddr
)
432 struct ethtool_drvinfo info
;
433 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
435 memset(&info
, 0, sizeof(info
));
436 info
.cmd
= ETHTOOL_GDRVINFO
;
437 if (ops
->get_drvinfo
) {
438 ops
->get_drvinfo(dev
, &info
);
439 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
440 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
441 sizeof(info
.bus_info
));
442 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
443 sizeof(info
.driver
));
449 * this method of obtaining string set info is deprecated;
450 * Use ETHTOOL_GSSET_INFO instead.
452 if (ops
->get_sset_count
) {
455 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
457 info
.testinfo_len
= rc
;
458 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
461 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
463 info
.n_priv_flags
= rc
;
465 if (ops
->get_regs_len
)
466 info
.regdump_len
= ops
->get_regs_len(dev
);
467 if (ops
->get_eeprom_len
)
468 info
.eedump_len
= ops
->get_eeprom_len(dev
);
470 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
475 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
476 void __user
*useraddr
)
478 struct ethtool_sset_info info
;
480 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
481 u32
*info_buf
= NULL
;
483 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
486 /* store copy of mask, because we zero struct later on */
487 sset_mask
= info
.sset_mask
;
491 /* calculate size of return buffer */
492 n_bits
= hweight64(sset_mask
);
494 memset(&info
, 0, sizeof(info
));
495 info
.cmd
= ETHTOOL_GSSET_INFO
;
497 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
502 * fill return buffer based on input bitmask and successful
503 * get_sset_count return
505 for (i
= 0; i
< 64; i
++) {
506 if (!(sset_mask
& (1ULL << i
)))
509 rc
= __ethtool_get_sset_count(dev
, i
);
511 info
.sset_mask
|= (1ULL << i
);
512 info_buf
[idx
++] = rc
;
517 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
520 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
521 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
531 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
532 u32 cmd
, void __user
*useraddr
)
534 struct ethtool_rxnfc info
;
535 size_t info_size
= sizeof(info
);
538 if (!dev
->ethtool_ops
->set_rxnfc
)
541 /* struct ethtool_rxnfc was originally defined for
542 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
543 * members. User-space might still be using that
545 if (cmd
== ETHTOOL_SRXFH
)
546 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
549 if (copy_from_user(&info
, useraddr
, info_size
))
552 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
556 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
557 copy_to_user(useraddr
, &info
, info_size
))
563 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
564 u32 cmd
, void __user
*useraddr
)
566 struct ethtool_rxnfc info
;
567 size_t info_size
= sizeof(info
);
568 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
570 void *rule_buf
= NULL
;
575 /* struct ethtool_rxnfc was originally defined for
576 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
577 * members. User-space might still be using that
579 if (cmd
== ETHTOOL_GRXFH
)
580 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
583 if (copy_from_user(&info
, useraddr
, info_size
))
586 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
587 if (info
.rule_cnt
> 0) {
588 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
589 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
596 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
601 if (copy_to_user(useraddr
, &info
, info_size
))
605 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
606 if (copy_to_user(useraddr
, rule_buf
,
607 info
.rule_cnt
* sizeof(u32
)))
618 static int ethtool_copy_validate_indir(u32
*indir
, void __user
*useraddr
,
619 struct ethtool_rxnfc
*rx_rings
,
624 if (copy_from_user(indir
, useraddr
, size
* sizeof(indir
[0])))
627 /* Validate ring indices */
628 for (i
= 0; i
< size
; i
++)
629 if (indir
[i
] >= rx_rings
->data
)
635 u8 netdev_rss_key
[NETDEV_RSS_KEY_LEN
] __read_mostly
;
637 void netdev_rss_key_fill(void *buffer
, size_t len
)
639 BUG_ON(len
> sizeof(netdev_rss_key
));
640 net_get_random_once(netdev_rss_key
, sizeof(netdev_rss_key
));
641 memcpy(buffer
, netdev_rss_key
, len
);
643 EXPORT_SYMBOL(netdev_rss_key_fill
);
645 static int ethtool_get_max_rxfh_channel(struct net_device
*dev
, u32
*max
)
647 u32 dev_size
, current_max
= 0;
651 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
652 !dev
->ethtool_ops
->get_rxfh
)
654 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
658 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
662 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
667 current_max
= max(current_max
, indir
[dev_size
]);
676 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
677 void __user
*useraddr
)
679 u32 user_size
, dev_size
;
683 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
684 !dev
->ethtool_ops
->get_rxfh
)
686 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
690 if (copy_from_user(&user_size
,
691 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
695 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
696 &dev_size
, sizeof(dev_size
)))
699 /* If the user buffer size is 0, this is just a query for the
700 * device table size. Otherwise, if it's smaller than the
701 * device table size it's an error.
703 if (user_size
< dev_size
)
704 return user_size
== 0 ? 0 : -EINVAL
;
706 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
710 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
714 if (copy_to_user(useraddr
+
715 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
716 indir
, dev_size
* sizeof(indir
[0])))
724 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
725 void __user
*useraddr
)
727 struct ethtool_rxnfc rx_rings
;
728 u32 user_size
, dev_size
, i
;
730 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
732 u32 ringidx_offset
= offsetof(struct ethtool_rxfh_indir
, ring_index
[0]);
734 if (!ops
->get_rxfh_indir_size
|| !ops
->set_rxfh
||
738 dev_size
= ops
->get_rxfh_indir_size(dev
);
742 if (copy_from_user(&user_size
,
743 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
747 if (user_size
!= 0 && user_size
!= dev_size
)
750 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
754 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
755 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
759 if (user_size
== 0) {
760 for (i
= 0; i
< dev_size
; i
++)
761 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
763 ret
= ethtool_copy_validate_indir(indir
,
764 useraddr
+ ringidx_offset
,
771 ret
= ops
->set_rxfh(dev
, indir
, NULL
, ETH_RSS_HASH_NO_CHANGE
);
775 /* indicate whether rxfh was set to default */
777 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
779 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
786 static noinline_for_stack
int ethtool_get_rxfh(struct net_device
*dev
,
787 void __user
*useraddr
)
790 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
791 u32 user_indir_size
, user_key_size
;
792 u32 dev_indir_size
= 0, dev_key_size
= 0;
793 struct ethtool_rxfh rxfh
;
804 if (ops
->get_rxfh_indir_size
)
805 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
806 if (ops
->get_rxfh_key_size
)
807 dev_key_size
= ops
->get_rxfh_key_size(dev
);
809 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
811 user_indir_size
= rxfh
.indir_size
;
812 user_key_size
= rxfh
.key_size
;
814 /* Check that reserved fields are 0 for now */
815 if (rxfh
.rss_context
|| rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] ||
816 rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
819 rxfh
.indir_size
= dev_indir_size
;
820 rxfh
.key_size
= dev_key_size
;
821 if (copy_to_user(useraddr
, &rxfh
, sizeof(rxfh
)))
824 if ((user_indir_size
&& (user_indir_size
!= dev_indir_size
)) ||
825 (user_key_size
&& (user_key_size
!= dev_key_size
)))
828 indir_bytes
= user_indir_size
* sizeof(indir
[0]);
829 total_size
= indir_bytes
+ user_key_size
;
830 rss_config
= kzalloc(total_size
, GFP_USER
);
835 indir
= (u32
*)rss_config
;
838 hkey
= rss_config
+ indir_bytes
;
840 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, hkey
, &dev_hfunc
);
844 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, hfunc
),
845 &dev_hfunc
, sizeof(rxfh
.hfunc
))) {
847 } else if (copy_to_user(useraddr
+
848 offsetof(struct ethtool_rxfh
, rss_config
[0]),
849 rss_config
, total_size
)) {
858 static noinline_for_stack
int ethtool_set_rxfh(struct net_device
*dev
,
859 void __user
*useraddr
)
862 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
863 struct ethtool_rxnfc rx_rings
;
864 struct ethtool_rxfh rxfh
;
865 u32 dev_indir_size
= 0, dev_key_size
= 0, i
;
866 u32
*indir
= NULL
, indir_bytes
= 0;
869 u32 rss_cfg_offset
= offsetof(struct ethtool_rxfh
, rss_config
[0]);
871 if (!ops
->get_rxnfc
|| !ops
->set_rxfh
)
874 if (ops
->get_rxfh_indir_size
)
875 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
876 if (ops
->get_rxfh_key_size
)
877 dev_key_size
= ops
->get_rxfh_key_size(dev
);
879 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
882 /* Check that reserved fields are 0 for now */
883 if (rxfh
.rss_context
|| rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] ||
884 rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
887 /* If either indir, hash key or function is valid, proceed further.
888 * Must request at least one change: indir size, hash key or function.
890 if ((rxfh
.indir_size
&&
891 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
&&
892 rxfh
.indir_size
!= dev_indir_size
) ||
893 (rxfh
.key_size
&& (rxfh
.key_size
!= dev_key_size
)) ||
894 (rxfh
.indir_size
== ETH_RXFH_INDIR_NO_CHANGE
&&
895 rxfh
.key_size
== 0 && rxfh
.hfunc
== ETH_RSS_HASH_NO_CHANGE
))
898 if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
899 indir_bytes
= dev_indir_size
* sizeof(indir
[0]);
901 rss_config
= kzalloc(indir_bytes
+ rxfh
.key_size
, GFP_USER
);
905 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
906 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
910 /* rxfh.indir_size == 0 means reset the indir table to default.
911 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
913 if (rxfh
.indir_size
&&
914 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
) {
915 indir
= (u32
*)rss_config
;
916 ret
= ethtool_copy_validate_indir(indir
,
917 useraddr
+ rss_cfg_offset
,
922 } else if (rxfh
.indir_size
== 0) {
923 indir
= (u32
*)rss_config
;
924 for (i
= 0; i
< dev_indir_size
; i
++)
925 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
929 hkey
= rss_config
+ indir_bytes
;
930 if (copy_from_user(hkey
,
931 useraddr
+ rss_cfg_offset
+ indir_bytes
,
938 ret
= ops
->set_rxfh(dev
, indir
, hkey
, rxfh
.hfunc
);
942 /* indicate whether rxfh was set to default */
943 if (rxfh
.indir_size
== 0)
944 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
945 else if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
946 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
953 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
955 struct ethtool_regs regs
;
956 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
960 if (!ops
->get_regs
|| !ops
->get_regs_len
)
963 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
966 reglen
= ops
->get_regs_len(dev
);
967 if (regs
.len
> reglen
)
970 regbuf
= vzalloc(reglen
);
971 if (reglen
&& !regbuf
)
974 ops
->get_regs(dev
, ®s
, regbuf
);
977 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
979 useraddr
+= offsetof(struct ethtool_regs
, data
);
980 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
989 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
991 struct ethtool_value reset
;
994 if (!dev
->ethtool_ops
->reset
)
997 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
1000 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
1004 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
1009 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
1011 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
1013 if (!dev
->ethtool_ops
->get_wol
)
1016 dev
->ethtool_ops
->get_wol(dev
, &wol
);
1018 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
1023 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
1025 struct ethtool_wolinfo wol
;
1027 if (!dev
->ethtool_ops
->set_wol
)
1030 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
1033 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
1036 static int ethtool_get_eee(struct net_device
*dev
, char __user
*useraddr
)
1038 struct ethtool_eee edata
;
1041 if (!dev
->ethtool_ops
->get_eee
)
1044 memset(&edata
, 0, sizeof(struct ethtool_eee
));
1045 edata
.cmd
= ETHTOOL_GEEE
;
1046 rc
= dev
->ethtool_ops
->get_eee(dev
, &edata
);
1051 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1057 static int ethtool_set_eee(struct net_device
*dev
, char __user
*useraddr
)
1059 struct ethtool_eee edata
;
1061 if (!dev
->ethtool_ops
->set_eee
)
1064 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1067 return dev
->ethtool_ops
->set_eee(dev
, &edata
);
1070 static int ethtool_nway_reset(struct net_device
*dev
)
1072 if (!dev
->ethtool_ops
->nway_reset
)
1075 return dev
->ethtool_ops
->nway_reset(dev
);
1078 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
1080 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
1082 if (!dev
->ethtool_ops
->get_link
)
1085 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
1087 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1092 static int ethtool_get_any_eeprom(struct net_device
*dev
, void __user
*useraddr
,
1093 int (*getter
)(struct net_device
*,
1094 struct ethtool_eeprom
*, u8
*),
1097 struct ethtool_eeprom eeprom
;
1098 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1099 u32 bytes_remaining
;
1103 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1106 /* Check for wrap and zero */
1107 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1110 /* Check for exceeding total eeprom len */
1111 if (eeprom
.offset
+ eeprom
.len
> total_len
)
1114 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1118 bytes_remaining
= eeprom
.len
;
1119 while (bytes_remaining
> 0) {
1120 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1122 ret
= getter(dev
, &eeprom
, data
);
1125 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
1129 userbuf
+= eeprom
.len
;
1130 eeprom
.offset
+= eeprom
.len
;
1131 bytes_remaining
-= eeprom
.len
;
1134 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
1135 eeprom
.offset
-= eeprom
.len
;
1136 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
1143 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1145 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1147 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
||
1148 !ops
->get_eeprom_len(dev
))
1151 return ethtool_get_any_eeprom(dev
, useraddr
, ops
->get_eeprom
,
1152 ops
->get_eeprom_len(dev
));
1155 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1157 struct ethtool_eeprom eeprom
;
1158 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1159 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1160 u32 bytes_remaining
;
1164 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
||
1165 !ops
->get_eeprom_len(dev
))
1168 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1171 /* Check for wrap and zero */
1172 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1175 /* Check for exceeding total eeprom len */
1176 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1179 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1183 bytes_remaining
= eeprom
.len
;
1184 while (bytes_remaining
> 0) {
1185 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1187 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
1191 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
1194 userbuf
+= eeprom
.len
;
1195 eeprom
.offset
+= eeprom
.len
;
1196 bytes_remaining
-= eeprom
.len
;
1203 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
1204 void __user
*useraddr
)
1206 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
1208 if (!dev
->ethtool_ops
->get_coalesce
)
1211 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
1213 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
1218 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
1219 void __user
*useraddr
)
1221 struct ethtool_coalesce coalesce
;
1223 if (!dev
->ethtool_ops
->set_coalesce
)
1226 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
1229 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
1232 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1234 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
1236 if (!dev
->ethtool_ops
->get_ringparam
)
1239 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
1241 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
1246 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1248 struct ethtool_ringparam ringparam
;
1250 if (!dev
->ethtool_ops
->set_ringparam
)
1253 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
1256 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
1259 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
1260 void __user
*useraddr
)
1262 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
1264 if (!dev
->ethtool_ops
->get_channels
)
1267 dev
->ethtool_ops
->get_channels(dev
, &channels
);
1269 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
1274 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
1275 void __user
*useraddr
)
1277 struct ethtool_channels channels
, max
;
1278 u32 max_rx_in_use
= 0;
1280 if (!dev
->ethtool_ops
->set_channels
|| !dev
->ethtool_ops
->get_channels
)
1283 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
1286 dev
->ethtool_ops
->get_channels(dev
, &max
);
1288 /* ensure new counts are within the maximums */
1289 if ((channels
.rx_count
> max
.max_rx
) ||
1290 (channels
.tx_count
> max
.max_tx
) ||
1291 (channels
.combined_count
> max
.max_combined
) ||
1292 (channels
.other_count
> max
.max_other
))
1295 /* ensure the new Rx count fits within the configured Rx flow
1296 * indirection table settings */
1297 if (netif_is_rxfh_configured(dev
) &&
1298 !ethtool_get_max_rxfh_channel(dev
, &max_rx_in_use
) &&
1299 (channels
.combined_count
+ channels
.rx_count
) <= max_rx_in_use
)
1302 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
1305 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1307 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
1309 if (!dev
->ethtool_ops
->get_pauseparam
)
1312 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
1314 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
1319 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1321 struct ethtool_pauseparam pauseparam
;
1323 if (!dev
->ethtool_ops
->set_pauseparam
)
1326 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
1329 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
1332 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1334 struct ethtool_test test
;
1335 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1339 if (!ops
->self_test
|| !ops
->get_sset_count
)
1342 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1345 WARN_ON(test_len
== 0);
1347 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1350 test
.len
= test_len
;
1351 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1355 ops
->self_test(dev
, &test
, data
);
1358 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1360 useraddr
+= sizeof(test
);
1361 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1370 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1372 struct ethtool_gstrings gstrings
;
1376 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1379 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1385 data
= kcalloc(gstrings
.len
, ETH_GSTRING_LEN
, GFP_USER
);
1389 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1392 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1394 useraddr
+= sizeof(gstrings
);
1395 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1404 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1406 struct ethtool_value id
;
1408 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1411 if (!ops
->set_phys_id
)
1417 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1420 rc
= ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1424 /* Drop the RTNL lock while waiting, but prevent reentry or
1425 * removal of the device.
1432 /* Driver will handle this itself */
1433 schedule_timeout_interruptible(
1434 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1436 /* Driver expects to be called at twice the frequency in rc */
1437 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1439 /* Count down seconds */
1441 /* Count down iterations per second */
1445 rc
= ops
->set_phys_id(dev
,
1446 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1450 schedule_timeout_interruptible(interval
);
1451 } while (!signal_pending(current
) && --i
!= 0);
1452 } while (!signal_pending(current
) &&
1453 (id
.data
== 0 || --id
.data
!= 0));
1460 (void) ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1464 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1466 struct ethtool_stats stats
;
1467 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1471 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1474 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1477 WARN_ON(n_stats
== 0);
1479 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1482 stats
.n_stats
= n_stats
;
1483 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1487 ops
->get_ethtool_stats(dev
, &stats
, data
);
1490 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1492 useraddr
+= sizeof(stats
);
1493 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1502 static int ethtool_get_phy_stats(struct net_device
*dev
, void __user
*useraddr
)
1504 struct ethtool_stats stats
;
1505 struct phy_device
*phydev
= dev
->phydev
;
1512 n_stats
= phy_get_sset_count(phydev
);
1516 WARN_ON(n_stats
== 0);
1518 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1521 stats
.n_stats
= n_stats
;
1522 data
= kmalloc_array(n_stats
, sizeof(u64
), GFP_USER
);
1526 mutex_lock(&phydev
->lock
);
1527 phydev
->drv
->get_stats(phydev
, &stats
, data
);
1528 mutex_unlock(&phydev
->lock
);
1531 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1533 useraddr
+= sizeof(stats
);
1534 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1543 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1545 struct ethtool_perm_addr epaddr
;
1547 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1550 if (epaddr
.size
< dev
->addr_len
)
1552 epaddr
.size
= dev
->addr_len
;
1554 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1556 useraddr
+= sizeof(epaddr
);
1557 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1562 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1563 u32 cmd
, u32 (*actor
)(struct net_device
*))
1565 struct ethtool_value edata
= { .cmd
= cmd
};
1570 edata
.data
= actor(dev
);
1572 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1577 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1578 void (*actor
)(struct net_device
*, u32
))
1580 struct ethtool_value edata
;
1585 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1588 actor(dev
, edata
.data
);
1592 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1593 int (*actor
)(struct net_device
*, u32
))
1595 struct ethtool_value edata
;
1600 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1603 return actor(dev
, edata
.data
);
1606 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1607 char __user
*useraddr
)
1609 struct ethtool_flash efl
;
1611 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1614 if (!dev
->ethtool_ops
->flash_device
)
1617 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
1619 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1622 static int ethtool_set_dump(struct net_device
*dev
,
1623 void __user
*useraddr
)
1625 struct ethtool_dump dump
;
1627 if (!dev
->ethtool_ops
->set_dump
)
1630 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1633 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
1636 static int ethtool_get_dump_flag(struct net_device
*dev
,
1637 void __user
*useraddr
)
1640 struct ethtool_dump dump
;
1641 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1643 if (!ops
->get_dump_flag
)
1646 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1649 ret
= ops
->get_dump_flag(dev
, &dump
);
1653 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
1658 static int ethtool_get_dump_data(struct net_device
*dev
,
1659 void __user
*useraddr
)
1663 struct ethtool_dump dump
, tmp
;
1664 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1667 if (!ops
->get_dump_data
|| !ops
->get_dump_flag
)
1670 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1673 memset(&tmp
, 0, sizeof(tmp
));
1674 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
1675 ret
= ops
->get_dump_flag(dev
, &tmp
);
1679 len
= min(tmp
.len
, dump
.len
);
1683 /* Don't ever let the driver think there's more space available
1684 * than it requested with .get_dump_flag().
1688 /* Always allocate enough space to hold the whole thing so that the
1689 * driver does not need to check the length and bother with partial
1692 data
= vzalloc(tmp
.len
);
1695 ret
= ops
->get_dump_data(dev
, &dump
, data
);
1699 /* There are two sane possibilities:
1700 * 1. The driver's .get_dump_data() does not touch dump.len.
1701 * 2. Or it may set dump.len to how much it really writes, which
1702 * should be tmp.len (or len if it can do a partial dump).
1703 * In any case respond to userspace with the actual length of data
1706 WARN_ON(dump
.len
!= len
&& dump
.len
!= tmp
.len
);
1709 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
1713 useraddr
+= offsetof(struct ethtool_dump
, data
);
1714 if (copy_to_user(useraddr
, data
, len
))
1721 static int ethtool_get_ts_info(struct net_device
*dev
, void __user
*useraddr
)
1724 struct ethtool_ts_info info
;
1725 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1726 struct phy_device
*phydev
= dev
->phydev
;
1728 memset(&info
, 0, sizeof(info
));
1729 info
.cmd
= ETHTOOL_GET_TS_INFO
;
1731 if (phydev
&& phydev
->drv
&& phydev
->drv
->ts_info
) {
1732 err
= phydev
->drv
->ts_info(phydev
, &info
);
1733 } else if (ops
->get_ts_info
) {
1734 err
= ops
->get_ts_info(dev
, &info
);
1736 info
.so_timestamping
=
1737 SOF_TIMESTAMPING_RX_SOFTWARE
|
1738 SOF_TIMESTAMPING_SOFTWARE
;
1739 info
.phc_index
= -1;
1745 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
1751 static int __ethtool_get_module_info(struct net_device
*dev
,
1752 struct ethtool_modinfo
*modinfo
)
1754 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1755 struct phy_device
*phydev
= dev
->phydev
;
1757 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_info
)
1758 return phydev
->drv
->module_info(phydev
, modinfo
);
1760 if (ops
->get_module_info
)
1761 return ops
->get_module_info(dev
, modinfo
);
1766 static int ethtool_get_module_info(struct net_device
*dev
,
1767 void __user
*useraddr
)
1770 struct ethtool_modinfo modinfo
;
1772 if (copy_from_user(&modinfo
, useraddr
, sizeof(modinfo
)))
1775 ret
= __ethtool_get_module_info(dev
, &modinfo
);
1779 if (copy_to_user(useraddr
, &modinfo
, sizeof(modinfo
)))
1785 static int __ethtool_get_module_eeprom(struct net_device
*dev
,
1786 struct ethtool_eeprom
*ee
, u8
*data
)
1788 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1789 struct phy_device
*phydev
= dev
->phydev
;
1791 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_eeprom
)
1792 return phydev
->drv
->module_eeprom(phydev
, ee
, data
);
1794 if (ops
->get_module_eeprom
)
1795 return ops
->get_module_eeprom(dev
, ee
, data
);
1800 static int ethtool_get_module_eeprom(struct net_device
*dev
,
1801 void __user
*useraddr
)
1804 struct ethtool_modinfo modinfo
;
1806 ret
= __ethtool_get_module_info(dev
, &modinfo
);
1810 return ethtool_get_any_eeprom(dev
, useraddr
,
1811 __ethtool_get_module_eeprom
,
1812 modinfo
.eeprom_len
);
1815 static int ethtool_tunable_valid(const struct ethtool_tunable
*tuna
)
1818 case ETHTOOL_RX_COPYBREAK
:
1819 case ETHTOOL_TX_COPYBREAK
:
1820 if (tuna
->len
!= sizeof(u32
) ||
1821 tuna
->type_id
!= ETHTOOL_TUNABLE_U32
)
1831 static int ethtool_get_tunable(struct net_device
*dev
, void __user
*useraddr
)
1834 struct ethtool_tunable tuna
;
1835 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1838 if (!ops
->get_tunable
)
1840 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
1842 ret
= ethtool_tunable_valid(&tuna
);
1845 data
= kmalloc(tuna
.len
, GFP_USER
);
1848 ret
= ops
->get_tunable(dev
, &tuna
, data
);
1851 useraddr
+= sizeof(tuna
);
1853 if (copy_to_user(useraddr
, data
, tuna
.len
))
1862 static int ethtool_set_tunable(struct net_device
*dev
, void __user
*useraddr
)
1865 struct ethtool_tunable tuna
;
1866 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1869 if (!ops
->set_tunable
)
1871 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
1873 ret
= ethtool_tunable_valid(&tuna
);
1876 data
= kmalloc(tuna
.len
, GFP_USER
);
1879 useraddr
+= sizeof(tuna
);
1881 if (copy_from_user(data
, useraddr
, tuna
.len
))
1883 ret
= ops
->set_tunable(dev
, &tuna
, data
);
1890 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
1892 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1894 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1895 void __user
*useraddr
= ifr
->ifr_data
;
1898 netdev_features_t old_features
;
1900 if (!dev
|| !netif_device_present(dev
))
1903 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1906 /* Allow some commands to be done by anyone */
1909 case ETHTOOL_GDRVINFO
:
1910 case ETHTOOL_GMSGLVL
:
1912 case ETHTOOL_GCOALESCE
:
1913 case ETHTOOL_GRINGPARAM
:
1914 case ETHTOOL_GPAUSEPARAM
:
1915 case ETHTOOL_GRXCSUM
:
1916 case ETHTOOL_GTXCSUM
:
1918 case ETHTOOL_GSSET_INFO
:
1919 case ETHTOOL_GSTRINGS
:
1920 case ETHTOOL_GSTATS
:
1921 case ETHTOOL_GPHYSTATS
:
1923 case ETHTOOL_GPERMADDR
:
1927 case ETHTOOL_GFLAGS
:
1928 case ETHTOOL_GPFLAGS
:
1930 case ETHTOOL_GRXRINGS
:
1931 case ETHTOOL_GRXCLSRLCNT
:
1932 case ETHTOOL_GRXCLSRULE
:
1933 case ETHTOOL_GRXCLSRLALL
:
1934 case ETHTOOL_GRXFHINDIR
:
1936 case ETHTOOL_GFEATURES
:
1937 case ETHTOOL_GCHANNELS
:
1938 case ETHTOOL_GET_TS_INFO
:
1940 case ETHTOOL_GTUNABLE
:
1943 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1947 if (dev
->ethtool_ops
->begin
) {
1948 rc
= dev
->ethtool_ops
->begin(dev
);
1952 old_features
= dev
->features
;
1956 rc
= ethtool_get_settings(dev
, useraddr
);
1959 rc
= ethtool_set_settings(dev
, useraddr
);
1961 case ETHTOOL_GDRVINFO
:
1962 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1965 rc
= ethtool_get_regs(dev
, useraddr
);
1968 rc
= ethtool_get_wol(dev
, useraddr
);
1971 rc
= ethtool_set_wol(dev
, useraddr
);
1973 case ETHTOOL_GMSGLVL
:
1974 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1975 dev
->ethtool_ops
->get_msglevel
);
1977 case ETHTOOL_SMSGLVL
:
1978 rc
= ethtool_set_value_void(dev
, useraddr
,
1979 dev
->ethtool_ops
->set_msglevel
);
1982 rc
= ethtool_get_eee(dev
, useraddr
);
1985 rc
= ethtool_set_eee(dev
, useraddr
);
1987 case ETHTOOL_NWAY_RST
:
1988 rc
= ethtool_nway_reset(dev
);
1991 rc
= ethtool_get_link(dev
, useraddr
);
1993 case ETHTOOL_GEEPROM
:
1994 rc
= ethtool_get_eeprom(dev
, useraddr
);
1996 case ETHTOOL_SEEPROM
:
1997 rc
= ethtool_set_eeprom(dev
, useraddr
);
1999 case ETHTOOL_GCOALESCE
:
2000 rc
= ethtool_get_coalesce(dev
, useraddr
);
2002 case ETHTOOL_SCOALESCE
:
2003 rc
= ethtool_set_coalesce(dev
, useraddr
);
2005 case ETHTOOL_GRINGPARAM
:
2006 rc
= ethtool_get_ringparam(dev
, useraddr
);
2008 case ETHTOOL_SRINGPARAM
:
2009 rc
= ethtool_set_ringparam(dev
, useraddr
);
2011 case ETHTOOL_GPAUSEPARAM
:
2012 rc
= ethtool_get_pauseparam(dev
, useraddr
);
2014 case ETHTOOL_SPAUSEPARAM
:
2015 rc
= ethtool_set_pauseparam(dev
, useraddr
);
2018 rc
= ethtool_self_test(dev
, useraddr
);
2020 case ETHTOOL_GSTRINGS
:
2021 rc
= ethtool_get_strings(dev
, useraddr
);
2023 case ETHTOOL_PHYS_ID
:
2024 rc
= ethtool_phys_id(dev
, useraddr
);
2026 case ETHTOOL_GSTATS
:
2027 rc
= ethtool_get_stats(dev
, useraddr
);
2029 case ETHTOOL_GPERMADDR
:
2030 rc
= ethtool_get_perm_addr(dev
, useraddr
);
2032 case ETHTOOL_GFLAGS
:
2033 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2034 __ethtool_get_flags
);
2036 case ETHTOOL_SFLAGS
:
2037 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
2039 case ETHTOOL_GPFLAGS
:
2040 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2041 dev
->ethtool_ops
->get_priv_flags
);
2043 case ETHTOOL_SPFLAGS
:
2044 rc
= ethtool_set_value(dev
, useraddr
,
2045 dev
->ethtool_ops
->set_priv_flags
);
2048 case ETHTOOL_GRXRINGS
:
2049 case ETHTOOL_GRXCLSRLCNT
:
2050 case ETHTOOL_GRXCLSRULE
:
2051 case ETHTOOL_GRXCLSRLALL
:
2052 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
2055 case ETHTOOL_SRXCLSRLDEL
:
2056 case ETHTOOL_SRXCLSRLINS
:
2057 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
2059 case ETHTOOL_FLASHDEV
:
2060 rc
= ethtool_flash_device(dev
, useraddr
);
2063 rc
= ethtool_reset(dev
, useraddr
);
2065 case ETHTOOL_GSSET_INFO
:
2066 rc
= ethtool_get_sset_info(dev
, useraddr
);
2068 case ETHTOOL_GRXFHINDIR
:
2069 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2071 case ETHTOOL_SRXFHINDIR
:
2072 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2075 rc
= ethtool_get_rxfh(dev
, useraddr
);
2078 rc
= ethtool_set_rxfh(dev
, useraddr
);
2080 case ETHTOOL_GFEATURES
:
2081 rc
= ethtool_get_features(dev
, useraddr
);
2083 case ETHTOOL_SFEATURES
:
2084 rc
= ethtool_set_features(dev
, useraddr
);
2086 case ETHTOOL_GTXCSUM
:
2087 case ETHTOOL_GRXCSUM
:
2093 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2095 case ETHTOOL_STXCSUM
:
2096 case ETHTOOL_SRXCSUM
:
2102 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2104 case ETHTOOL_GCHANNELS
:
2105 rc
= ethtool_get_channels(dev
, useraddr
);
2107 case ETHTOOL_SCHANNELS
:
2108 rc
= ethtool_set_channels(dev
, useraddr
);
2110 case ETHTOOL_SET_DUMP
:
2111 rc
= ethtool_set_dump(dev
, useraddr
);
2113 case ETHTOOL_GET_DUMP_FLAG
:
2114 rc
= ethtool_get_dump_flag(dev
, useraddr
);
2116 case ETHTOOL_GET_DUMP_DATA
:
2117 rc
= ethtool_get_dump_data(dev
, useraddr
);
2119 case ETHTOOL_GET_TS_INFO
:
2120 rc
= ethtool_get_ts_info(dev
, useraddr
);
2122 case ETHTOOL_GMODULEINFO
:
2123 rc
= ethtool_get_module_info(dev
, useraddr
);
2125 case ETHTOOL_GMODULEEEPROM
:
2126 rc
= ethtool_get_module_eeprom(dev
, useraddr
);
2128 case ETHTOOL_GTUNABLE
:
2129 rc
= ethtool_get_tunable(dev
, useraddr
);
2131 case ETHTOOL_STUNABLE
:
2132 rc
= ethtool_set_tunable(dev
, useraddr
);
2134 case ETHTOOL_GPHYSTATS
:
2135 rc
= ethtool_get_phy_stats(dev
, useraddr
);
2141 if (dev
->ethtool_ops
->complete
)
2142 dev
->ethtool_ops
->complete(dev
);
2144 if (old_features
!= dev
->features
)
2145 netdev_features_change(dev
);