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",
78 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
80 struct ethtool_gfeatures cmd
= {
81 .cmd
= ETHTOOL_GFEATURES
,
82 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
84 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
89 /* in case feature bits run out again */
90 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
92 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
93 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
94 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
95 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
96 features
[i
].never_changed
=
97 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
100 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
101 if (get_user(copy_size
, sizeaddr
))
104 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
105 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
107 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
109 useraddr
+= sizeof(cmd
);
110 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
116 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
118 struct ethtool_sfeatures cmd
;
119 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
120 netdev_features_t wanted
= 0, valid
= 0;
123 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
125 useraddr
+= sizeof(cmd
);
127 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
130 if (copy_from_user(features
, useraddr
, sizeof(features
)))
133 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
134 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
135 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
138 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
141 if (valid
& ~dev
->hw_features
) {
142 valid
&= dev
->hw_features
;
143 ret
|= ETHTOOL_F_UNSUPPORTED
;
146 dev
->wanted_features
&= ~valid
;
147 dev
->wanted_features
|= wanted
& valid
;
148 __netdev_update_features(dev
);
150 if ((dev
->wanted_features
^ dev
->features
) & valid
)
151 ret
|= ETHTOOL_F_WISH
;
156 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
158 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
160 if (sset
== ETH_SS_FEATURES
)
161 return ARRAY_SIZE(netdev_features_strings
);
163 if (ops
&& ops
->get_sset_count
&& ops
->get_strings
)
164 return ops
->get_sset_count(dev
, sset
);
169 static void __ethtool_get_strings(struct net_device
*dev
,
170 u32 stringset
, u8
*data
)
172 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
174 if (stringset
== ETH_SS_FEATURES
)
175 memcpy(data
, netdev_features_strings
,
176 sizeof(netdev_features_strings
));
178 /* ops->get_strings is valid because checked earlier */
179 ops
->get_strings(dev
, stringset
, data
);
182 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
184 /* feature masks of legacy discrete ethtool ops */
187 case ETHTOOL_GTXCSUM
:
188 case ETHTOOL_STXCSUM
:
189 return NETIF_F_ALL_CSUM
| NETIF_F_SCTP_CSUM
;
190 case ETHTOOL_GRXCSUM
:
191 case ETHTOOL_SRXCSUM
:
192 return NETIF_F_RXCSUM
;
198 return NETIF_F_ALL_TSO
;
213 static int ethtool_get_one_feature(struct net_device
*dev
,
214 char __user
*useraddr
, u32 ethcmd
)
216 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
217 struct ethtool_value edata
= {
219 .data
= !!(dev
->features
& mask
),
222 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
227 static int ethtool_set_one_feature(struct net_device
*dev
,
228 void __user
*useraddr
, u32 ethcmd
)
230 struct ethtool_value edata
;
231 netdev_features_t mask
;
233 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
236 mask
= ethtool_get_feature_mask(ethcmd
);
237 mask
&= dev
->hw_features
;
242 dev
->wanted_features
|= mask
;
244 dev
->wanted_features
&= ~mask
;
246 __netdev_update_features(dev
);
251 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
252 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
253 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
254 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
256 static u32
__ethtool_get_flags(struct net_device
*dev
)
260 if (dev
->features
& NETIF_F_LRO
) flags
|= ETH_FLAG_LRO
;
261 if (dev
->features
& NETIF_F_HW_VLAN_RX
) flags
|= ETH_FLAG_RXVLAN
;
262 if (dev
->features
& NETIF_F_HW_VLAN_TX
) flags
|= ETH_FLAG_TXVLAN
;
263 if (dev
->features
& NETIF_F_NTUPLE
) flags
|= ETH_FLAG_NTUPLE
;
264 if (dev
->features
& NETIF_F_RXHASH
) flags
|= ETH_FLAG_RXHASH
;
269 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
271 netdev_features_t features
= 0, changed
;
273 if (data
& ~ETH_ALL_FLAGS
)
276 if (data
& ETH_FLAG_LRO
) features
|= NETIF_F_LRO
;
277 if (data
& ETH_FLAG_RXVLAN
) features
|= NETIF_F_HW_VLAN_RX
;
278 if (data
& ETH_FLAG_TXVLAN
) features
|= NETIF_F_HW_VLAN_TX
;
279 if (data
& ETH_FLAG_NTUPLE
) features
|= NETIF_F_NTUPLE
;
280 if (data
& ETH_FLAG_RXHASH
) features
|= NETIF_F_RXHASH
;
282 /* allow changing only bits set in hw_features */
283 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
284 if (changed
& ~dev
->hw_features
)
285 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
287 dev
->wanted_features
=
288 (dev
->wanted_features
& ~changed
) | (features
& changed
);
290 __netdev_update_features(dev
);
295 int __ethtool_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
299 if (!dev
->ethtool_ops
|| !dev
->ethtool_ops
->get_settings
)
302 memset(cmd
, 0, sizeof(struct ethtool_cmd
));
303 cmd
->cmd
= ETHTOOL_GSET
;
304 return dev
->ethtool_ops
->get_settings(dev
, cmd
);
306 EXPORT_SYMBOL(__ethtool_get_settings
);
308 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
311 struct ethtool_cmd cmd
;
313 err
= __ethtool_get_settings(dev
, &cmd
);
317 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
322 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
324 struct ethtool_cmd cmd
;
326 if (!dev
->ethtool_ops
->set_settings
)
329 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
332 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
335 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
336 void __user
*useraddr
)
338 struct ethtool_drvinfo info
;
339 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
341 memset(&info
, 0, sizeof(info
));
342 info
.cmd
= ETHTOOL_GDRVINFO
;
343 if (ops
&& ops
->get_drvinfo
) {
344 ops
->get_drvinfo(dev
, &info
);
345 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
346 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
347 sizeof(info
.bus_info
));
348 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
349 sizeof(info
.driver
));
355 * this method of obtaining string set info is deprecated;
356 * Use ETHTOOL_GSSET_INFO instead.
358 if (ops
&& ops
->get_sset_count
) {
361 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
363 info
.testinfo_len
= rc
;
364 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
367 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
369 info
.n_priv_flags
= rc
;
371 if (ops
&& ops
->get_regs_len
)
372 info
.regdump_len
= ops
->get_regs_len(dev
);
373 if (ops
&& ops
->get_eeprom_len
)
374 info
.eedump_len
= ops
->get_eeprom_len(dev
);
376 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
381 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
382 void __user
*useraddr
)
384 struct ethtool_sset_info info
;
386 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
387 u32
*info_buf
= NULL
;
389 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
392 /* store copy of mask, because we zero struct later on */
393 sset_mask
= info
.sset_mask
;
397 /* calculate size of return buffer */
398 n_bits
= hweight64(sset_mask
);
400 memset(&info
, 0, sizeof(info
));
401 info
.cmd
= ETHTOOL_GSSET_INFO
;
403 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
408 * fill return buffer based on input bitmask and successful
409 * get_sset_count return
411 for (i
= 0; i
< 64; i
++) {
412 if (!(sset_mask
& (1ULL << i
)))
415 rc
= __ethtool_get_sset_count(dev
, i
);
417 info
.sset_mask
|= (1ULL << i
);
418 info_buf
[idx
++] = rc
;
423 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
426 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
427 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
437 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
438 u32 cmd
, void __user
*useraddr
)
440 struct ethtool_rxnfc info
;
441 size_t info_size
= sizeof(info
);
444 if (!dev
->ethtool_ops
->set_rxnfc
)
447 /* struct ethtool_rxnfc was originally defined for
448 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
449 * members. User-space might still be using that
451 if (cmd
== ETHTOOL_SRXFH
)
452 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
455 if (copy_from_user(&info
, useraddr
, info_size
))
458 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
462 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
463 copy_to_user(useraddr
, &info
, info_size
))
469 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
470 u32 cmd
, void __user
*useraddr
)
472 struct ethtool_rxnfc info
;
473 size_t info_size
= sizeof(info
);
474 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
476 void *rule_buf
= NULL
;
481 /* struct ethtool_rxnfc was originally defined for
482 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
483 * members. User-space might still be using that
485 if (cmd
== ETHTOOL_GRXFH
)
486 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
489 if (copy_from_user(&info
, useraddr
, info_size
))
492 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
493 if (info
.rule_cnt
> 0) {
494 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
495 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
502 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
507 if (copy_to_user(useraddr
, &info
, info_size
))
511 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
512 if (copy_to_user(useraddr
, rule_buf
,
513 info
.rule_cnt
* sizeof(u32
)))
524 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
525 void __user
*useraddr
)
527 u32 user_size
, dev_size
;
531 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
532 !dev
->ethtool_ops
->get_rxfh_indir
)
534 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
538 if (copy_from_user(&user_size
,
539 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
543 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
544 &dev_size
, sizeof(dev_size
)))
547 /* If the user buffer size is 0, this is just a query for the
548 * device table size. Otherwise, if it's smaller than the
549 * device table size it's an error.
551 if (user_size
< dev_size
)
552 return user_size
== 0 ? 0 : -EINVAL
;
554 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
558 ret
= dev
->ethtool_ops
->get_rxfh_indir(dev
, indir
);
562 if (copy_to_user(useraddr
+
563 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
564 indir
, dev_size
* sizeof(indir
[0])))
572 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
573 void __user
*useraddr
)
575 struct ethtool_rxnfc rx_rings
;
576 u32 user_size
, dev_size
, i
;
580 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
581 !dev
->ethtool_ops
->set_rxfh_indir
||
582 !dev
->ethtool_ops
->get_rxnfc
)
584 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
588 if (copy_from_user(&user_size
,
589 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
593 if (user_size
!= 0 && user_size
!= dev_size
)
596 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
600 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
601 ret
= dev
->ethtool_ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
605 if (user_size
== 0) {
606 for (i
= 0; i
< dev_size
; i
++)
607 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
609 if (copy_from_user(indir
,
611 offsetof(struct ethtool_rxfh_indir
,
613 dev_size
* sizeof(indir
[0]))) {
618 /* Validate ring indices */
619 for (i
= 0; i
< dev_size
; i
++) {
620 if (indir
[i
] >= rx_rings
.data
) {
627 ret
= dev
->ethtool_ops
->set_rxfh_indir(dev
, indir
);
634 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
636 struct ethtool_regs regs
;
637 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
641 if (!ops
->get_regs
|| !ops
->get_regs_len
)
644 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
647 reglen
= ops
->get_regs_len(dev
);
648 if (regs
.len
> reglen
)
651 regbuf
= vzalloc(reglen
);
652 if (reglen
&& !regbuf
)
655 ops
->get_regs(dev
, ®s
, regbuf
);
658 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
660 useraddr
+= offsetof(struct ethtool_regs
, data
);
661 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
670 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
672 struct ethtool_value reset
;
675 if (!dev
->ethtool_ops
->reset
)
678 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
681 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
685 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
690 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
692 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
694 if (!dev
->ethtool_ops
->get_wol
)
697 dev
->ethtool_ops
->get_wol(dev
, &wol
);
699 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
704 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
706 struct ethtool_wolinfo wol
;
708 if (!dev
->ethtool_ops
->set_wol
)
711 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
714 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
717 static int ethtool_nway_reset(struct net_device
*dev
)
719 if (!dev
->ethtool_ops
->nway_reset
)
722 return dev
->ethtool_ops
->nway_reset(dev
);
725 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
727 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
729 if (!dev
->ethtool_ops
->get_link
)
732 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
734 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
739 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
741 struct ethtool_eeprom eeprom
;
742 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
743 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
748 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
751 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
754 /* Check for wrap and zero */
755 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
758 /* Check for exceeding total eeprom len */
759 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
762 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
766 bytes_remaining
= eeprom
.len
;
767 while (bytes_remaining
> 0) {
768 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
770 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
773 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
777 userbuf
+= eeprom
.len
;
778 eeprom
.offset
+= eeprom
.len
;
779 bytes_remaining
-= eeprom
.len
;
782 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
783 eeprom
.offset
-= eeprom
.len
;
784 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
791 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
793 struct ethtool_eeprom eeprom
;
794 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
795 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
800 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
803 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
806 /* Check for wrap and zero */
807 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
810 /* Check for exceeding total eeprom len */
811 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
814 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
818 bytes_remaining
= eeprom
.len
;
819 while (bytes_remaining
> 0) {
820 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
822 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
826 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
829 userbuf
+= eeprom
.len
;
830 eeprom
.offset
+= eeprom
.len
;
831 bytes_remaining
-= eeprom
.len
;
838 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
839 void __user
*useraddr
)
841 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
843 if (!dev
->ethtool_ops
->get_coalesce
)
846 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
848 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
853 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
854 void __user
*useraddr
)
856 struct ethtool_coalesce coalesce
;
858 if (!dev
->ethtool_ops
->set_coalesce
)
861 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
864 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
867 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
869 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
871 if (!dev
->ethtool_ops
->get_ringparam
)
874 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
876 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
881 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
883 struct ethtool_ringparam ringparam
;
885 if (!dev
->ethtool_ops
->set_ringparam
)
888 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
891 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
894 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
895 void __user
*useraddr
)
897 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
899 if (!dev
->ethtool_ops
->get_channels
)
902 dev
->ethtool_ops
->get_channels(dev
, &channels
);
904 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
909 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
910 void __user
*useraddr
)
912 struct ethtool_channels channels
;
914 if (!dev
->ethtool_ops
->set_channels
)
917 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
920 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
923 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
925 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
927 if (!dev
->ethtool_ops
->get_pauseparam
)
930 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
932 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
937 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
939 struct ethtool_pauseparam pauseparam
;
941 if (!dev
->ethtool_ops
->set_pauseparam
)
944 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
947 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
950 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
952 struct ethtool_test test
;
953 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
957 if (!ops
->self_test
|| !ops
->get_sset_count
)
960 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
963 WARN_ON(test_len
== 0);
965 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
969 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
973 ops
->self_test(dev
, &test
, data
);
976 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
978 useraddr
+= sizeof(test
);
979 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
988 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
990 struct ethtool_gstrings gstrings
;
994 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
997 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1003 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1007 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1010 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1012 useraddr
+= sizeof(gstrings
);
1013 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1022 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1024 struct ethtool_value id
;
1028 if (!dev
->ethtool_ops
->set_phys_id
)
1034 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1037 rc
= dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1041 /* Drop the RTNL lock while waiting, but prevent reentry or
1042 * removal of the device.
1049 /* Driver will handle this itself */
1050 schedule_timeout_interruptible(
1051 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1053 /* Driver expects to be called at twice the frequency in rc */
1054 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1056 /* Count down seconds */
1058 /* Count down iterations per second */
1062 rc
= dev
->ethtool_ops
->set_phys_id(dev
,
1063 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1067 schedule_timeout_interruptible(interval
);
1068 } while (!signal_pending(current
) && --i
!= 0);
1069 } while (!signal_pending(current
) &&
1070 (id
.data
== 0 || --id
.data
!= 0));
1077 (void)dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1081 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1083 struct ethtool_stats stats
;
1084 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1088 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1091 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1094 WARN_ON(n_stats
== 0);
1096 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1099 stats
.n_stats
= n_stats
;
1100 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1104 ops
->get_ethtool_stats(dev
, &stats
, data
);
1107 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1109 useraddr
+= sizeof(stats
);
1110 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1119 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1121 struct ethtool_perm_addr epaddr
;
1123 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1126 if (epaddr
.size
< dev
->addr_len
)
1128 epaddr
.size
= dev
->addr_len
;
1130 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1132 useraddr
+= sizeof(epaddr
);
1133 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1138 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1139 u32 cmd
, u32 (*actor
)(struct net_device
*))
1141 struct ethtool_value edata
= { .cmd
= cmd
};
1146 edata
.data
= actor(dev
);
1148 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1153 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1154 void (*actor
)(struct net_device
*, u32
))
1156 struct ethtool_value edata
;
1161 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1164 actor(dev
, edata
.data
);
1168 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1169 int (*actor
)(struct net_device
*, u32
))
1171 struct ethtool_value edata
;
1176 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1179 return actor(dev
, edata
.data
);
1182 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1183 char __user
*useraddr
)
1185 struct ethtool_flash efl
;
1187 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1190 if (!dev
->ethtool_ops
->flash_device
)
1193 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1196 static int ethtool_set_dump(struct net_device
*dev
,
1197 void __user
*useraddr
)
1199 struct ethtool_dump dump
;
1201 if (!dev
->ethtool_ops
->set_dump
)
1204 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1207 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
1210 static int ethtool_get_dump_flag(struct net_device
*dev
,
1211 void __user
*useraddr
)
1214 struct ethtool_dump dump
;
1215 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1217 if (!dev
->ethtool_ops
->get_dump_flag
)
1220 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1223 ret
= ops
->get_dump_flag(dev
, &dump
);
1227 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
1232 static int ethtool_get_dump_data(struct net_device
*dev
,
1233 void __user
*useraddr
)
1237 struct ethtool_dump dump
, tmp
;
1238 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1241 if (!dev
->ethtool_ops
->get_dump_data
||
1242 !dev
->ethtool_ops
->get_dump_flag
)
1245 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1248 memset(&tmp
, 0, sizeof(tmp
));
1249 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
1250 ret
= ops
->get_dump_flag(dev
, &tmp
);
1254 len
= (tmp
.len
> dump
.len
) ? dump
.len
: tmp
.len
;
1258 data
= vzalloc(tmp
.len
);
1261 ret
= ops
->get_dump_data(dev
, &dump
, data
);
1265 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
1269 useraddr
+= offsetof(struct ethtool_dump
, data
);
1270 if (copy_to_user(useraddr
, data
, len
))
1277 /* The main entry point in this file. Called from net/core/dev.c */
1279 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1281 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1282 void __user
*useraddr
= ifr
->ifr_data
;
1287 if (!dev
|| !netif_device_present(dev
))
1290 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1293 if (!dev
->ethtool_ops
) {
1294 /* ETHTOOL_GDRVINFO does not require any driver support.
1295 * It is also unprivileged and does not change anything,
1296 * so we can take a shortcut to it. */
1297 if (ethcmd
== ETHTOOL_GDRVINFO
)
1298 return ethtool_get_drvinfo(dev
, useraddr
);
1303 /* Allow some commands to be done by anyone */
1306 case ETHTOOL_GDRVINFO
:
1307 case ETHTOOL_GMSGLVL
:
1308 case ETHTOOL_GCOALESCE
:
1309 case ETHTOOL_GRINGPARAM
:
1310 case ETHTOOL_GPAUSEPARAM
:
1311 case ETHTOOL_GRXCSUM
:
1312 case ETHTOOL_GTXCSUM
:
1314 case ETHTOOL_GSTRINGS
:
1316 case ETHTOOL_GPERMADDR
:
1320 case ETHTOOL_GFLAGS
:
1321 case ETHTOOL_GPFLAGS
:
1323 case ETHTOOL_GRXRINGS
:
1324 case ETHTOOL_GRXCLSRLCNT
:
1325 case ETHTOOL_GRXCLSRULE
:
1326 case ETHTOOL_GRXCLSRLALL
:
1327 case ETHTOOL_GFEATURES
:
1330 if (!capable(CAP_NET_ADMIN
))
1334 if (dev
->ethtool_ops
->begin
) {
1335 rc
= dev
->ethtool_ops
->begin(dev
);
1339 old_features
= dev
->features
;
1343 rc
= ethtool_get_settings(dev
, useraddr
);
1346 rc
= ethtool_set_settings(dev
, useraddr
);
1348 case ETHTOOL_GDRVINFO
:
1349 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1352 rc
= ethtool_get_regs(dev
, useraddr
);
1355 rc
= ethtool_get_wol(dev
, useraddr
);
1358 rc
= ethtool_set_wol(dev
, useraddr
);
1360 case ETHTOOL_GMSGLVL
:
1361 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1362 dev
->ethtool_ops
->get_msglevel
);
1364 case ETHTOOL_SMSGLVL
:
1365 rc
= ethtool_set_value_void(dev
, useraddr
,
1366 dev
->ethtool_ops
->set_msglevel
);
1368 case ETHTOOL_NWAY_RST
:
1369 rc
= ethtool_nway_reset(dev
);
1372 rc
= ethtool_get_link(dev
, useraddr
);
1374 case ETHTOOL_GEEPROM
:
1375 rc
= ethtool_get_eeprom(dev
, useraddr
);
1377 case ETHTOOL_SEEPROM
:
1378 rc
= ethtool_set_eeprom(dev
, useraddr
);
1380 case ETHTOOL_GCOALESCE
:
1381 rc
= ethtool_get_coalesce(dev
, useraddr
);
1383 case ETHTOOL_SCOALESCE
:
1384 rc
= ethtool_set_coalesce(dev
, useraddr
);
1386 case ETHTOOL_GRINGPARAM
:
1387 rc
= ethtool_get_ringparam(dev
, useraddr
);
1389 case ETHTOOL_SRINGPARAM
:
1390 rc
= ethtool_set_ringparam(dev
, useraddr
);
1392 case ETHTOOL_GPAUSEPARAM
:
1393 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1395 case ETHTOOL_SPAUSEPARAM
:
1396 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1399 rc
= ethtool_self_test(dev
, useraddr
);
1401 case ETHTOOL_GSTRINGS
:
1402 rc
= ethtool_get_strings(dev
, useraddr
);
1404 case ETHTOOL_PHYS_ID
:
1405 rc
= ethtool_phys_id(dev
, useraddr
);
1407 case ETHTOOL_GSTATS
:
1408 rc
= ethtool_get_stats(dev
, useraddr
);
1410 case ETHTOOL_GPERMADDR
:
1411 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1413 case ETHTOOL_GFLAGS
:
1414 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1415 __ethtool_get_flags
);
1417 case ETHTOOL_SFLAGS
:
1418 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
1420 case ETHTOOL_GPFLAGS
:
1421 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1422 dev
->ethtool_ops
->get_priv_flags
);
1424 case ETHTOOL_SPFLAGS
:
1425 rc
= ethtool_set_value(dev
, useraddr
,
1426 dev
->ethtool_ops
->set_priv_flags
);
1429 case ETHTOOL_GRXRINGS
:
1430 case ETHTOOL_GRXCLSRLCNT
:
1431 case ETHTOOL_GRXCLSRULE
:
1432 case ETHTOOL_GRXCLSRLALL
:
1433 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
1436 case ETHTOOL_SRXCLSRLDEL
:
1437 case ETHTOOL_SRXCLSRLINS
:
1438 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
1440 case ETHTOOL_FLASHDEV
:
1441 rc
= ethtool_flash_device(dev
, useraddr
);
1444 rc
= ethtool_reset(dev
, useraddr
);
1446 case ETHTOOL_GSSET_INFO
:
1447 rc
= ethtool_get_sset_info(dev
, useraddr
);
1449 case ETHTOOL_GRXFHINDIR
:
1450 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
1452 case ETHTOOL_SRXFHINDIR
:
1453 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
1455 case ETHTOOL_GFEATURES
:
1456 rc
= ethtool_get_features(dev
, useraddr
);
1458 case ETHTOOL_SFEATURES
:
1459 rc
= ethtool_set_features(dev
, useraddr
);
1461 case ETHTOOL_GTXCSUM
:
1462 case ETHTOOL_GRXCSUM
:
1468 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
1470 case ETHTOOL_STXCSUM
:
1471 case ETHTOOL_SRXCSUM
:
1477 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
1479 case ETHTOOL_GCHANNELS
:
1480 rc
= ethtool_get_channels(dev
, useraddr
);
1482 case ETHTOOL_SCHANNELS
:
1483 rc
= ethtool_set_channels(dev
, useraddr
);
1485 case ETHTOOL_SET_DUMP
:
1486 rc
= ethtool_set_dump(dev
, useraddr
);
1488 case ETHTOOL_GET_DUMP_FLAG
:
1489 rc
= ethtool_get_dump_flag(dev
, useraddr
);
1491 case ETHTOOL_GET_DUMP_DATA
:
1492 rc
= ethtool_get_dump_data(dev
, useraddr
);
1498 if (dev
->ethtool_ops
->complete
)
1499 dev
->ethtool_ops
->complete(dev
);
1501 if (old_features
!= dev
->features
)
1502 netdev_features_change(dev
);