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/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/sched.h>
28 * Some useful ethtool_ops methods that're device independent.
29 * If we find that all drivers want to do the same thing here,
30 * we can turn these into dev_() function calls.
33 u32
ethtool_op_get_link(struct net_device
*dev
)
35 return netif_carrier_ok(dev
) ? 1 : 0;
37 EXPORT_SYMBOL(ethtool_op_get_link
);
39 /* Handlers for each ethtool command */
41 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
43 static const char netdev_features_strings
[NETDEV_FEATURE_COUNT
][ETH_GSTRING_LEN
] = {
44 [NETIF_F_SG_BIT
] = "tx-scatter-gather",
45 [NETIF_F_IP_CSUM_BIT
] = "tx-checksum-ipv4",
46 [NETIF_F_HW_CSUM_BIT
] = "tx-checksum-ip-generic",
47 [NETIF_F_IPV6_CSUM_BIT
] = "tx-checksum-ipv6",
48 [NETIF_F_HIGHDMA_BIT
] = "highdma",
49 [NETIF_F_FRAGLIST_BIT
] = "tx-scatter-gather-fraglist",
50 [NETIF_F_HW_VLAN_TX_BIT
] = "tx-vlan-hw-insert",
52 [NETIF_F_HW_VLAN_RX_BIT
] = "rx-vlan-hw-parse",
53 [NETIF_F_HW_VLAN_FILTER_BIT
] = "rx-vlan-filter",
54 [NETIF_F_VLAN_CHALLENGED_BIT
] = "vlan-challenged",
55 [NETIF_F_GSO_BIT
] = "tx-generic-segmentation",
56 [NETIF_F_LLTX_BIT
] = "tx-lockless",
57 [NETIF_F_NETNS_LOCAL_BIT
] = "netns-local",
58 [NETIF_F_GRO_BIT
] = "rx-gro",
59 [NETIF_F_LRO_BIT
] = "rx-lro",
61 [NETIF_F_TSO_BIT
] = "tx-tcp-segmentation",
62 [NETIF_F_UFO_BIT
] = "tx-udp-fragmentation",
63 [NETIF_F_GSO_ROBUST_BIT
] = "tx-gso-robust",
64 [NETIF_F_TSO_ECN_BIT
] = "tx-tcp-ecn-segmentation",
65 [NETIF_F_TSO6_BIT
] = "tx-tcp6-segmentation",
66 [NETIF_F_FSO_BIT
] = "tx-fcoe-segmentation",
68 [NETIF_F_FCOE_CRC_BIT
] = "tx-checksum-fcoe-crc",
69 [NETIF_F_SCTP_CSUM_BIT
] = "tx-checksum-sctp",
70 [NETIF_F_FCOE_MTU_BIT
] = "fcoe-mtu",
71 [NETIF_F_NTUPLE_BIT
] = "rx-ntuple-filter",
72 [NETIF_F_RXHASH_BIT
] = "rx-hashing",
73 [NETIF_F_RXCSUM_BIT
] = "rx-checksum",
74 [NETIF_F_NOCACHE_COPY_BIT
] = "tx-nocache-copy",
75 [NETIF_F_LOOPBACK_BIT
] = "loopback",
76 [NETIF_F_RXFCS_BIT
] = "rx-fcs",
79 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
81 struct ethtool_gfeatures cmd
= {
82 .cmd
= ETHTOOL_GFEATURES
,
83 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
85 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
90 /* in case feature bits run out again */
91 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
93 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
94 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
95 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
96 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
97 features
[i
].never_changed
=
98 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
101 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
102 if (get_user(copy_size
, sizeaddr
))
105 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
106 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
108 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
110 useraddr
+= sizeof(cmd
);
111 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
117 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
119 struct ethtool_sfeatures cmd
;
120 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
121 netdev_features_t wanted
= 0, valid
= 0;
124 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
126 useraddr
+= sizeof(cmd
);
128 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
131 if (copy_from_user(features
, useraddr
, sizeof(features
)))
134 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
135 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
136 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
139 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
142 if (valid
& ~dev
->hw_features
) {
143 valid
&= dev
->hw_features
;
144 ret
|= ETHTOOL_F_UNSUPPORTED
;
147 dev
->wanted_features
&= ~valid
;
148 dev
->wanted_features
|= wanted
& valid
;
149 __netdev_update_features(dev
);
151 if ((dev
->wanted_features
^ dev
->features
) & valid
)
152 ret
|= ETHTOOL_F_WISH
;
157 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
159 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
161 if (sset
== ETH_SS_FEATURES
)
162 return ARRAY_SIZE(netdev_features_strings
);
164 if (ops
&& ops
->get_sset_count
&& ops
->get_strings
)
165 return ops
->get_sset_count(dev
, sset
);
170 static void __ethtool_get_strings(struct net_device
*dev
,
171 u32 stringset
, u8
*data
)
173 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
175 if (stringset
== ETH_SS_FEATURES
)
176 memcpy(data
, netdev_features_strings
,
177 sizeof(netdev_features_strings
));
179 /* ops->get_strings is valid because checked earlier */
180 ops
->get_strings(dev
, stringset
, data
);
183 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
185 /* feature masks of legacy discrete ethtool ops */
188 case ETHTOOL_GTXCSUM
:
189 case ETHTOOL_STXCSUM
:
190 return NETIF_F_ALL_CSUM
| NETIF_F_SCTP_CSUM
;
191 case ETHTOOL_GRXCSUM
:
192 case ETHTOOL_SRXCSUM
:
193 return NETIF_F_RXCSUM
;
199 return NETIF_F_ALL_TSO
;
214 static int ethtool_get_one_feature(struct net_device
*dev
,
215 char __user
*useraddr
, u32 ethcmd
)
217 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
218 struct ethtool_value edata
= {
220 .data
= !!(dev
->features
& mask
),
223 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
228 static int ethtool_set_one_feature(struct net_device
*dev
,
229 void __user
*useraddr
, u32 ethcmd
)
231 struct ethtool_value edata
;
232 netdev_features_t mask
;
234 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
237 mask
= ethtool_get_feature_mask(ethcmd
);
238 mask
&= dev
->hw_features
;
243 dev
->wanted_features
|= mask
;
245 dev
->wanted_features
&= ~mask
;
247 __netdev_update_features(dev
);
252 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
253 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
254 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
255 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
257 static u32
__ethtool_get_flags(struct net_device
*dev
)
261 if (dev
->features
& NETIF_F_LRO
) flags
|= ETH_FLAG_LRO
;
262 if (dev
->features
& NETIF_F_HW_VLAN_RX
) flags
|= ETH_FLAG_RXVLAN
;
263 if (dev
->features
& NETIF_F_HW_VLAN_TX
) flags
|= ETH_FLAG_TXVLAN
;
264 if (dev
->features
& NETIF_F_NTUPLE
) flags
|= ETH_FLAG_NTUPLE
;
265 if (dev
->features
& NETIF_F_RXHASH
) flags
|= ETH_FLAG_RXHASH
;
270 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
272 netdev_features_t features
= 0, changed
;
274 if (data
& ~ETH_ALL_FLAGS
)
277 if (data
& ETH_FLAG_LRO
) features
|= NETIF_F_LRO
;
278 if (data
& ETH_FLAG_RXVLAN
) features
|= NETIF_F_HW_VLAN_RX
;
279 if (data
& ETH_FLAG_TXVLAN
) features
|= NETIF_F_HW_VLAN_TX
;
280 if (data
& ETH_FLAG_NTUPLE
) features
|= NETIF_F_NTUPLE
;
281 if (data
& ETH_FLAG_RXHASH
) features
|= NETIF_F_RXHASH
;
283 /* allow changing only bits set in hw_features */
284 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
285 if (changed
& ~dev
->hw_features
)
286 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
288 dev
->wanted_features
=
289 (dev
->wanted_features
& ~changed
) | (features
& changed
);
291 __netdev_update_features(dev
);
296 int __ethtool_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
300 if (!dev
->ethtool_ops
|| !dev
->ethtool_ops
->get_settings
)
303 memset(cmd
, 0, sizeof(struct ethtool_cmd
));
304 cmd
->cmd
= ETHTOOL_GSET
;
305 return dev
->ethtool_ops
->get_settings(dev
, cmd
);
307 EXPORT_SYMBOL(__ethtool_get_settings
);
309 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
312 struct ethtool_cmd cmd
;
314 err
= __ethtool_get_settings(dev
, &cmd
);
318 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
323 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
325 struct ethtool_cmd cmd
;
327 if (!dev
->ethtool_ops
->set_settings
)
330 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
333 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
336 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
337 void __user
*useraddr
)
339 struct ethtool_drvinfo info
;
340 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
342 memset(&info
, 0, sizeof(info
));
343 info
.cmd
= ETHTOOL_GDRVINFO
;
344 if (ops
&& ops
->get_drvinfo
) {
345 ops
->get_drvinfo(dev
, &info
);
346 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
347 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
348 sizeof(info
.bus_info
));
349 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
350 sizeof(info
.driver
));
356 * this method of obtaining string set info is deprecated;
357 * Use ETHTOOL_GSSET_INFO instead.
359 if (ops
&& ops
->get_sset_count
) {
362 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
364 info
.testinfo_len
= rc
;
365 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
368 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
370 info
.n_priv_flags
= rc
;
372 if (ops
&& ops
->get_regs_len
)
373 info
.regdump_len
= ops
->get_regs_len(dev
);
374 if (ops
&& ops
->get_eeprom_len
)
375 info
.eedump_len
= ops
->get_eeprom_len(dev
);
377 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
382 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
383 void __user
*useraddr
)
385 struct ethtool_sset_info info
;
387 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
388 u32
*info_buf
= NULL
;
390 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
393 /* store copy of mask, because we zero struct later on */
394 sset_mask
= info
.sset_mask
;
398 /* calculate size of return buffer */
399 n_bits
= hweight64(sset_mask
);
401 memset(&info
, 0, sizeof(info
));
402 info
.cmd
= ETHTOOL_GSSET_INFO
;
404 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
409 * fill return buffer based on input bitmask and successful
410 * get_sset_count return
412 for (i
= 0; i
< 64; i
++) {
413 if (!(sset_mask
& (1ULL << i
)))
416 rc
= __ethtool_get_sset_count(dev
, i
);
418 info
.sset_mask
|= (1ULL << i
);
419 info_buf
[idx
++] = rc
;
424 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
427 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
428 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
438 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
439 u32 cmd
, void __user
*useraddr
)
441 struct ethtool_rxnfc info
;
442 size_t info_size
= sizeof(info
);
445 if (!dev
->ethtool_ops
->set_rxnfc
)
448 /* struct ethtool_rxnfc was originally defined for
449 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
450 * members. User-space might still be using that
452 if (cmd
== ETHTOOL_SRXFH
)
453 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
456 if (copy_from_user(&info
, useraddr
, info_size
))
459 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
463 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
464 copy_to_user(useraddr
, &info
, info_size
))
470 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
471 u32 cmd
, void __user
*useraddr
)
473 struct ethtool_rxnfc info
;
474 size_t info_size
= sizeof(info
);
475 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
477 void *rule_buf
= NULL
;
482 /* struct ethtool_rxnfc was originally defined for
483 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
484 * members. User-space might still be using that
486 if (cmd
== ETHTOOL_GRXFH
)
487 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
490 if (copy_from_user(&info
, useraddr
, info_size
))
493 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
494 if (info
.rule_cnt
> 0) {
495 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
496 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
503 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
508 if (copy_to_user(useraddr
, &info
, info_size
))
512 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
513 if (copy_to_user(useraddr
, rule_buf
,
514 info
.rule_cnt
* sizeof(u32
)))
525 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
526 void __user
*useraddr
)
528 u32 user_size
, dev_size
;
532 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
533 !dev
->ethtool_ops
->get_rxfh_indir
)
535 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
539 if (copy_from_user(&user_size
,
540 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
544 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
545 &dev_size
, sizeof(dev_size
)))
548 /* If the user buffer size is 0, this is just a query for the
549 * device table size. Otherwise, if it's smaller than the
550 * device table size it's an error.
552 if (user_size
< dev_size
)
553 return user_size
== 0 ? 0 : -EINVAL
;
555 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
559 ret
= dev
->ethtool_ops
->get_rxfh_indir(dev
, indir
);
563 if (copy_to_user(useraddr
+
564 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
565 indir
, dev_size
* sizeof(indir
[0])))
573 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
574 void __user
*useraddr
)
576 struct ethtool_rxnfc rx_rings
;
577 u32 user_size
, dev_size
, i
;
581 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
582 !dev
->ethtool_ops
->set_rxfh_indir
||
583 !dev
->ethtool_ops
->get_rxnfc
)
585 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
589 if (copy_from_user(&user_size
,
590 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
594 if (user_size
!= 0 && user_size
!= dev_size
)
597 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
601 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
602 ret
= dev
->ethtool_ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
606 if (user_size
== 0) {
607 for (i
= 0; i
< dev_size
; i
++)
608 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
610 if (copy_from_user(indir
,
612 offsetof(struct ethtool_rxfh_indir
,
614 dev_size
* sizeof(indir
[0]))) {
619 /* Validate ring indices */
620 for (i
= 0; i
< dev_size
; i
++) {
621 if (indir
[i
] >= rx_rings
.data
) {
628 ret
= dev
->ethtool_ops
->set_rxfh_indir(dev
, indir
);
635 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
637 struct ethtool_regs regs
;
638 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
642 if (!ops
->get_regs
|| !ops
->get_regs_len
)
645 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
648 reglen
= ops
->get_regs_len(dev
);
649 if (regs
.len
> reglen
)
652 regbuf
= vzalloc(reglen
);
653 if (reglen
&& !regbuf
)
656 ops
->get_regs(dev
, ®s
, regbuf
);
659 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
661 useraddr
+= offsetof(struct ethtool_regs
, data
);
662 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
671 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
673 struct ethtool_value reset
;
676 if (!dev
->ethtool_ops
->reset
)
679 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
682 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
686 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
691 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
693 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
695 if (!dev
->ethtool_ops
->get_wol
)
698 dev
->ethtool_ops
->get_wol(dev
, &wol
);
700 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
705 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
707 struct ethtool_wolinfo wol
;
709 if (!dev
->ethtool_ops
->set_wol
)
712 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
715 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
718 static int ethtool_nway_reset(struct net_device
*dev
)
720 if (!dev
->ethtool_ops
->nway_reset
)
723 return dev
->ethtool_ops
->nway_reset(dev
);
726 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
728 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
730 if (!dev
->ethtool_ops
->get_link
)
733 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
735 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
740 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
742 struct ethtool_eeprom eeprom
;
743 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
744 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
749 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
752 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
755 /* Check for wrap and zero */
756 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
759 /* Check for exceeding total eeprom len */
760 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
763 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
767 bytes_remaining
= eeprom
.len
;
768 while (bytes_remaining
> 0) {
769 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
771 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
774 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
778 userbuf
+= eeprom
.len
;
779 eeprom
.offset
+= eeprom
.len
;
780 bytes_remaining
-= eeprom
.len
;
783 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
784 eeprom
.offset
-= eeprom
.len
;
785 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
792 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
794 struct ethtool_eeprom eeprom
;
795 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
796 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
801 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
804 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
807 /* Check for wrap and zero */
808 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
811 /* Check for exceeding total eeprom len */
812 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
815 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
819 bytes_remaining
= eeprom
.len
;
820 while (bytes_remaining
> 0) {
821 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
823 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
827 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
830 userbuf
+= eeprom
.len
;
831 eeprom
.offset
+= eeprom
.len
;
832 bytes_remaining
-= eeprom
.len
;
839 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
840 void __user
*useraddr
)
842 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
844 if (!dev
->ethtool_ops
->get_coalesce
)
847 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
849 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
854 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
855 void __user
*useraddr
)
857 struct ethtool_coalesce coalesce
;
859 if (!dev
->ethtool_ops
->set_coalesce
)
862 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
865 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
868 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
870 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
872 if (!dev
->ethtool_ops
->get_ringparam
)
875 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
877 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
882 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
884 struct ethtool_ringparam ringparam
;
886 if (!dev
->ethtool_ops
->set_ringparam
)
889 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
892 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
895 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
896 void __user
*useraddr
)
898 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
900 if (!dev
->ethtool_ops
->get_channels
)
903 dev
->ethtool_ops
->get_channels(dev
, &channels
);
905 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
910 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
911 void __user
*useraddr
)
913 struct ethtool_channels channels
;
915 if (!dev
->ethtool_ops
->set_channels
)
918 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
921 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
924 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
926 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
928 if (!dev
->ethtool_ops
->get_pauseparam
)
931 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
933 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
938 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
940 struct ethtool_pauseparam pauseparam
;
942 if (!dev
->ethtool_ops
->set_pauseparam
)
945 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
948 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
951 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
953 struct ethtool_test test
;
954 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
958 if (!ops
->self_test
|| !ops
->get_sset_count
)
961 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
964 WARN_ON(test_len
== 0);
966 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
970 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
974 ops
->self_test(dev
, &test
, data
);
977 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
979 useraddr
+= sizeof(test
);
980 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
989 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
991 struct ethtool_gstrings gstrings
;
995 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
998 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1004 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1008 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1011 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1013 useraddr
+= sizeof(gstrings
);
1014 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1023 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1025 struct ethtool_value id
;
1029 if (!dev
->ethtool_ops
->set_phys_id
)
1035 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1038 rc
= dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1042 /* Drop the RTNL lock while waiting, but prevent reentry or
1043 * removal of the device.
1050 /* Driver will handle this itself */
1051 schedule_timeout_interruptible(
1052 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1054 /* Driver expects to be called at twice the frequency in rc */
1055 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1057 /* Count down seconds */
1059 /* Count down iterations per second */
1063 rc
= dev
->ethtool_ops
->set_phys_id(dev
,
1064 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1068 schedule_timeout_interruptible(interval
);
1069 } while (!signal_pending(current
) && --i
!= 0);
1070 } while (!signal_pending(current
) &&
1071 (id
.data
== 0 || --id
.data
!= 0));
1078 (void)dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1082 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1084 struct ethtool_stats stats
;
1085 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1089 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1092 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1095 WARN_ON(n_stats
== 0);
1097 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1100 stats
.n_stats
= n_stats
;
1101 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1105 ops
->get_ethtool_stats(dev
, &stats
, data
);
1108 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1110 useraddr
+= sizeof(stats
);
1111 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1120 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1122 struct ethtool_perm_addr epaddr
;
1124 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1127 if (epaddr
.size
< dev
->addr_len
)
1129 epaddr
.size
= dev
->addr_len
;
1131 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1133 useraddr
+= sizeof(epaddr
);
1134 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1139 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1140 u32 cmd
, u32 (*actor
)(struct net_device
*))
1142 struct ethtool_value edata
= { .cmd
= cmd
};
1147 edata
.data
= actor(dev
);
1149 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1154 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1155 void (*actor
)(struct net_device
*, u32
))
1157 struct ethtool_value edata
;
1162 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1165 actor(dev
, edata
.data
);
1169 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1170 int (*actor
)(struct net_device
*, u32
))
1172 struct ethtool_value edata
;
1177 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1180 return actor(dev
, edata
.data
);
1183 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1184 char __user
*useraddr
)
1186 struct ethtool_flash efl
;
1188 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1191 if (!dev
->ethtool_ops
->flash_device
)
1194 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
1196 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1199 static int ethtool_set_dump(struct net_device
*dev
,
1200 void __user
*useraddr
)
1202 struct ethtool_dump dump
;
1204 if (!dev
->ethtool_ops
->set_dump
)
1207 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1210 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
1213 static int ethtool_get_dump_flag(struct net_device
*dev
,
1214 void __user
*useraddr
)
1217 struct ethtool_dump dump
;
1218 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1220 if (!dev
->ethtool_ops
->get_dump_flag
)
1223 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1226 ret
= ops
->get_dump_flag(dev
, &dump
);
1230 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
1235 static int ethtool_get_dump_data(struct net_device
*dev
,
1236 void __user
*useraddr
)
1240 struct ethtool_dump dump
, tmp
;
1241 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1244 if (!dev
->ethtool_ops
->get_dump_data
||
1245 !dev
->ethtool_ops
->get_dump_flag
)
1248 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1251 memset(&tmp
, 0, sizeof(tmp
));
1252 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
1253 ret
= ops
->get_dump_flag(dev
, &tmp
);
1257 len
= (tmp
.len
> dump
.len
) ? dump
.len
: tmp
.len
;
1261 data
= vzalloc(tmp
.len
);
1264 ret
= ops
->get_dump_data(dev
, &dump
, data
);
1268 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
1272 useraddr
+= offsetof(struct ethtool_dump
, data
);
1273 if (copy_to_user(useraddr
, data
, len
))
1280 /* The main entry point in this file. Called from net/core/dev.c */
1282 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1284 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1285 void __user
*useraddr
= ifr
->ifr_data
;
1290 if (!dev
|| !netif_device_present(dev
))
1293 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1296 if (!dev
->ethtool_ops
) {
1297 /* ETHTOOL_GDRVINFO does not require any driver support.
1298 * It is also unprivileged and does not change anything,
1299 * so we can take a shortcut to it. */
1300 if (ethcmd
== ETHTOOL_GDRVINFO
)
1301 return ethtool_get_drvinfo(dev
, useraddr
);
1306 /* Allow some commands to be done by anyone */
1309 case ETHTOOL_GDRVINFO
:
1310 case ETHTOOL_GMSGLVL
:
1311 case ETHTOOL_GCOALESCE
:
1312 case ETHTOOL_GRINGPARAM
:
1313 case ETHTOOL_GPAUSEPARAM
:
1314 case ETHTOOL_GRXCSUM
:
1315 case ETHTOOL_GTXCSUM
:
1317 case ETHTOOL_GSSET_INFO
:
1318 case ETHTOOL_GSTRINGS
:
1320 case ETHTOOL_GPERMADDR
:
1324 case ETHTOOL_GFLAGS
:
1325 case ETHTOOL_GPFLAGS
:
1327 case ETHTOOL_GRXRINGS
:
1328 case ETHTOOL_GRXCLSRLCNT
:
1329 case ETHTOOL_GRXCLSRULE
:
1330 case ETHTOOL_GRXCLSRLALL
:
1331 case ETHTOOL_GFEATURES
:
1334 if (!capable(CAP_NET_ADMIN
))
1338 if (dev
->ethtool_ops
->begin
) {
1339 rc
= dev
->ethtool_ops
->begin(dev
);
1343 old_features
= dev
->features
;
1347 rc
= ethtool_get_settings(dev
, useraddr
);
1350 rc
= ethtool_set_settings(dev
, useraddr
);
1352 case ETHTOOL_GDRVINFO
:
1353 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1356 rc
= ethtool_get_regs(dev
, useraddr
);
1359 rc
= ethtool_get_wol(dev
, useraddr
);
1362 rc
= ethtool_set_wol(dev
, useraddr
);
1364 case ETHTOOL_GMSGLVL
:
1365 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1366 dev
->ethtool_ops
->get_msglevel
);
1368 case ETHTOOL_SMSGLVL
:
1369 rc
= ethtool_set_value_void(dev
, useraddr
,
1370 dev
->ethtool_ops
->set_msglevel
);
1372 case ETHTOOL_NWAY_RST
:
1373 rc
= ethtool_nway_reset(dev
);
1376 rc
= ethtool_get_link(dev
, useraddr
);
1378 case ETHTOOL_GEEPROM
:
1379 rc
= ethtool_get_eeprom(dev
, useraddr
);
1381 case ETHTOOL_SEEPROM
:
1382 rc
= ethtool_set_eeprom(dev
, useraddr
);
1384 case ETHTOOL_GCOALESCE
:
1385 rc
= ethtool_get_coalesce(dev
, useraddr
);
1387 case ETHTOOL_SCOALESCE
:
1388 rc
= ethtool_set_coalesce(dev
, useraddr
);
1390 case ETHTOOL_GRINGPARAM
:
1391 rc
= ethtool_get_ringparam(dev
, useraddr
);
1393 case ETHTOOL_SRINGPARAM
:
1394 rc
= ethtool_set_ringparam(dev
, useraddr
);
1396 case ETHTOOL_GPAUSEPARAM
:
1397 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1399 case ETHTOOL_SPAUSEPARAM
:
1400 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1403 rc
= ethtool_self_test(dev
, useraddr
);
1405 case ETHTOOL_GSTRINGS
:
1406 rc
= ethtool_get_strings(dev
, useraddr
);
1408 case ETHTOOL_PHYS_ID
:
1409 rc
= ethtool_phys_id(dev
, useraddr
);
1411 case ETHTOOL_GSTATS
:
1412 rc
= ethtool_get_stats(dev
, useraddr
);
1414 case ETHTOOL_GPERMADDR
:
1415 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1417 case ETHTOOL_GFLAGS
:
1418 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1419 __ethtool_get_flags
);
1421 case ETHTOOL_SFLAGS
:
1422 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
1424 case ETHTOOL_GPFLAGS
:
1425 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1426 dev
->ethtool_ops
->get_priv_flags
);
1428 case ETHTOOL_SPFLAGS
:
1429 rc
= ethtool_set_value(dev
, useraddr
,
1430 dev
->ethtool_ops
->set_priv_flags
);
1433 case ETHTOOL_GRXRINGS
:
1434 case ETHTOOL_GRXCLSRLCNT
:
1435 case ETHTOOL_GRXCLSRULE
:
1436 case ETHTOOL_GRXCLSRLALL
:
1437 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
1440 case ETHTOOL_SRXCLSRLDEL
:
1441 case ETHTOOL_SRXCLSRLINS
:
1442 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
1444 case ETHTOOL_FLASHDEV
:
1445 rc
= ethtool_flash_device(dev
, useraddr
);
1448 rc
= ethtool_reset(dev
, useraddr
);
1450 case ETHTOOL_GSSET_INFO
:
1451 rc
= ethtool_get_sset_info(dev
, useraddr
);
1453 case ETHTOOL_GRXFHINDIR
:
1454 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
1456 case ETHTOOL_SRXFHINDIR
:
1457 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
1459 case ETHTOOL_GFEATURES
:
1460 rc
= ethtool_get_features(dev
, useraddr
);
1462 case ETHTOOL_SFEATURES
:
1463 rc
= ethtool_set_features(dev
, useraddr
);
1465 case ETHTOOL_GTXCSUM
:
1466 case ETHTOOL_GRXCSUM
:
1472 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
1474 case ETHTOOL_STXCSUM
:
1475 case ETHTOOL_SRXCSUM
:
1481 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
1483 case ETHTOOL_GCHANNELS
:
1484 rc
= ethtool_get_channels(dev
, useraddr
);
1486 case ETHTOOL_SCHANNELS
:
1487 rc
= ethtool_set_channels(dev
, useraddr
);
1489 case ETHTOOL_SET_DUMP
:
1490 rc
= ethtool_set_dump(dev
, useraddr
);
1492 case ETHTOOL_GET_DUMP_FLAG
:
1493 rc
= ethtool_get_dump_flag(dev
, useraddr
);
1495 case ETHTOOL_GET_DUMP_DATA
:
1496 rc
= ethtool_get_dump_data(dev
, useraddr
);
1502 if (dev
->ethtool_ops
->complete
)
1503 dev
->ethtool_ops
->complete(dev
);
1505 if (old_features
!= dev
->features
)
1506 netdev_features_change(dev
);