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",
77 [NETIF_F_RXALL_BIT
] = "rx-all",
80 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
82 struct ethtool_gfeatures cmd
= {
83 .cmd
= ETHTOOL_GFEATURES
,
84 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
86 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
91 /* in case feature bits run out again */
92 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
94 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
95 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
96 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
97 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
98 features
[i
].never_changed
=
99 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
102 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
103 if (get_user(copy_size
, sizeaddr
))
106 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
107 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
109 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
111 useraddr
+= sizeof(cmd
);
112 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
118 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
120 struct ethtool_sfeatures cmd
;
121 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
122 netdev_features_t wanted
= 0, valid
= 0;
125 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
127 useraddr
+= sizeof(cmd
);
129 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
132 if (copy_from_user(features
, useraddr
, sizeof(features
)))
135 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
136 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
137 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
140 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
143 if (valid
& ~dev
->hw_features
) {
144 valid
&= dev
->hw_features
;
145 ret
|= ETHTOOL_F_UNSUPPORTED
;
148 dev
->wanted_features
&= ~valid
;
149 dev
->wanted_features
|= wanted
& valid
;
150 __netdev_update_features(dev
);
152 if ((dev
->wanted_features
^ dev
->features
) & valid
)
153 ret
|= ETHTOOL_F_WISH
;
158 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
160 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
162 if (sset
== ETH_SS_FEATURES
)
163 return ARRAY_SIZE(netdev_features_strings
);
165 if (ops
&& ops
->get_sset_count
&& ops
->get_strings
)
166 return ops
->get_sset_count(dev
, sset
);
171 static void __ethtool_get_strings(struct net_device
*dev
,
172 u32 stringset
, u8
*data
)
174 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
176 if (stringset
== ETH_SS_FEATURES
)
177 memcpy(data
, netdev_features_strings
,
178 sizeof(netdev_features_strings
));
180 /* ops->get_strings is valid because checked earlier */
181 ops
->get_strings(dev
, stringset
, data
);
184 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
186 /* feature masks of legacy discrete ethtool ops */
189 case ETHTOOL_GTXCSUM
:
190 case ETHTOOL_STXCSUM
:
191 return NETIF_F_ALL_CSUM
| NETIF_F_SCTP_CSUM
;
192 case ETHTOOL_GRXCSUM
:
193 case ETHTOOL_SRXCSUM
:
194 return NETIF_F_RXCSUM
;
200 return NETIF_F_ALL_TSO
;
215 static int ethtool_get_one_feature(struct net_device
*dev
,
216 char __user
*useraddr
, u32 ethcmd
)
218 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
219 struct ethtool_value edata
= {
221 .data
= !!(dev
->features
& mask
),
224 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
229 static int ethtool_set_one_feature(struct net_device
*dev
,
230 void __user
*useraddr
, u32 ethcmd
)
232 struct ethtool_value edata
;
233 netdev_features_t mask
;
235 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
238 mask
= ethtool_get_feature_mask(ethcmd
);
239 mask
&= dev
->hw_features
;
244 dev
->wanted_features
|= mask
;
246 dev
->wanted_features
&= ~mask
;
248 __netdev_update_features(dev
);
253 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
254 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
255 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
256 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
258 static u32
__ethtool_get_flags(struct net_device
*dev
)
262 if (dev
->features
& NETIF_F_LRO
) flags
|= ETH_FLAG_LRO
;
263 if (dev
->features
& NETIF_F_HW_VLAN_RX
) flags
|= ETH_FLAG_RXVLAN
;
264 if (dev
->features
& NETIF_F_HW_VLAN_TX
) flags
|= ETH_FLAG_TXVLAN
;
265 if (dev
->features
& NETIF_F_NTUPLE
) flags
|= ETH_FLAG_NTUPLE
;
266 if (dev
->features
& NETIF_F_RXHASH
) flags
|= ETH_FLAG_RXHASH
;
271 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
273 netdev_features_t features
= 0, changed
;
275 if (data
& ~ETH_ALL_FLAGS
)
278 if (data
& ETH_FLAG_LRO
) features
|= NETIF_F_LRO
;
279 if (data
& ETH_FLAG_RXVLAN
) features
|= NETIF_F_HW_VLAN_RX
;
280 if (data
& ETH_FLAG_TXVLAN
) features
|= NETIF_F_HW_VLAN_TX
;
281 if (data
& ETH_FLAG_NTUPLE
) features
|= NETIF_F_NTUPLE
;
282 if (data
& ETH_FLAG_RXHASH
) features
|= NETIF_F_RXHASH
;
284 /* allow changing only bits set in hw_features */
285 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
286 if (changed
& ~dev
->hw_features
)
287 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
289 dev
->wanted_features
=
290 (dev
->wanted_features
& ~changed
) | (features
& changed
);
292 __netdev_update_features(dev
);
297 int __ethtool_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
301 if (!dev
->ethtool_ops
|| !dev
->ethtool_ops
->get_settings
)
304 memset(cmd
, 0, sizeof(struct ethtool_cmd
));
305 cmd
->cmd
= ETHTOOL_GSET
;
306 return dev
->ethtool_ops
->get_settings(dev
, cmd
);
308 EXPORT_SYMBOL(__ethtool_get_settings
);
310 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
313 struct ethtool_cmd cmd
;
315 err
= __ethtool_get_settings(dev
, &cmd
);
319 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
324 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
326 struct ethtool_cmd cmd
;
328 if (!dev
->ethtool_ops
->set_settings
)
331 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
334 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
337 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
338 void __user
*useraddr
)
340 struct ethtool_drvinfo info
;
341 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
343 memset(&info
, 0, sizeof(info
));
344 info
.cmd
= ETHTOOL_GDRVINFO
;
345 if (ops
&& ops
->get_drvinfo
) {
346 ops
->get_drvinfo(dev
, &info
);
347 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
348 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
349 sizeof(info
.bus_info
));
350 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
351 sizeof(info
.driver
));
357 * this method of obtaining string set info is deprecated;
358 * Use ETHTOOL_GSSET_INFO instead.
360 if (ops
&& ops
->get_sset_count
) {
363 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
365 info
.testinfo_len
= rc
;
366 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
369 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
371 info
.n_priv_flags
= rc
;
373 if (ops
&& ops
->get_regs_len
)
374 info
.regdump_len
= ops
->get_regs_len(dev
);
375 if (ops
&& ops
->get_eeprom_len
)
376 info
.eedump_len
= ops
->get_eeprom_len(dev
);
378 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
383 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
384 void __user
*useraddr
)
386 struct ethtool_sset_info info
;
388 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
389 u32
*info_buf
= NULL
;
391 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
394 /* store copy of mask, because we zero struct later on */
395 sset_mask
= info
.sset_mask
;
399 /* calculate size of return buffer */
400 n_bits
= hweight64(sset_mask
);
402 memset(&info
, 0, sizeof(info
));
403 info
.cmd
= ETHTOOL_GSSET_INFO
;
405 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
410 * fill return buffer based on input bitmask and successful
411 * get_sset_count return
413 for (i
= 0; i
< 64; i
++) {
414 if (!(sset_mask
& (1ULL << i
)))
417 rc
= __ethtool_get_sset_count(dev
, i
);
419 info
.sset_mask
|= (1ULL << i
);
420 info_buf
[idx
++] = rc
;
425 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
428 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
429 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
439 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
440 u32 cmd
, void __user
*useraddr
)
442 struct ethtool_rxnfc info
;
443 size_t info_size
= sizeof(info
);
446 if (!dev
->ethtool_ops
->set_rxnfc
)
449 /* struct ethtool_rxnfc was originally defined for
450 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
451 * members. User-space might still be using that
453 if (cmd
== ETHTOOL_SRXFH
)
454 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
457 if (copy_from_user(&info
, useraddr
, info_size
))
460 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
464 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
465 copy_to_user(useraddr
, &info
, info_size
))
471 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
472 u32 cmd
, void __user
*useraddr
)
474 struct ethtool_rxnfc info
;
475 size_t info_size
= sizeof(info
);
476 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
478 void *rule_buf
= NULL
;
483 /* struct ethtool_rxnfc was originally defined for
484 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
485 * members. User-space might still be using that
487 if (cmd
== ETHTOOL_GRXFH
)
488 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
491 if (copy_from_user(&info
, useraddr
, info_size
))
494 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
495 if (info
.rule_cnt
> 0) {
496 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
497 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
504 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
509 if (copy_to_user(useraddr
, &info
, info_size
))
513 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
514 if (copy_to_user(useraddr
, rule_buf
,
515 info
.rule_cnt
* sizeof(u32
)))
526 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
527 void __user
*useraddr
)
529 u32 user_size
, dev_size
;
533 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
534 !dev
->ethtool_ops
->get_rxfh_indir
)
536 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
540 if (copy_from_user(&user_size
,
541 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
545 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
546 &dev_size
, sizeof(dev_size
)))
549 /* If the user buffer size is 0, this is just a query for the
550 * device table size. Otherwise, if it's smaller than the
551 * device table size it's an error.
553 if (user_size
< dev_size
)
554 return user_size
== 0 ? 0 : -EINVAL
;
556 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
560 ret
= dev
->ethtool_ops
->get_rxfh_indir(dev
, indir
);
564 if (copy_to_user(useraddr
+
565 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
566 indir
, dev_size
* sizeof(indir
[0])))
574 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
575 void __user
*useraddr
)
577 struct ethtool_rxnfc rx_rings
;
578 u32 user_size
, dev_size
, i
;
582 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
583 !dev
->ethtool_ops
->set_rxfh_indir
||
584 !dev
->ethtool_ops
->get_rxnfc
)
586 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
590 if (copy_from_user(&user_size
,
591 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
595 if (user_size
!= 0 && user_size
!= dev_size
)
598 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
602 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
603 ret
= dev
->ethtool_ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
607 if (user_size
== 0) {
608 for (i
= 0; i
< dev_size
; i
++)
609 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
611 if (copy_from_user(indir
,
613 offsetof(struct ethtool_rxfh_indir
,
615 dev_size
* sizeof(indir
[0]))) {
620 /* Validate ring indices */
621 for (i
= 0; i
< dev_size
; i
++) {
622 if (indir
[i
] >= rx_rings
.data
) {
629 ret
= dev
->ethtool_ops
->set_rxfh_indir(dev
, indir
);
636 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
638 struct ethtool_regs regs
;
639 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
643 if (!ops
->get_regs
|| !ops
->get_regs_len
)
646 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
649 reglen
= ops
->get_regs_len(dev
);
650 if (regs
.len
> reglen
)
653 regbuf
= vzalloc(reglen
);
654 if (reglen
&& !regbuf
)
657 ops
->get_regs(dev
, ®s
, regbuf
);
660 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
662 useraddr
+= offsetof(struct ethtool_regs
, data
);
663 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
672 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
674 struct ethtool_value reset
;
677 if (!dev
->ethtool_ops
->reset
)
680 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
683 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
687 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
692 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
694 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
696 if (!dev
->ethtool_ops
->get_wol
)
699 dev
->ethtool_ops
->get_wol(dev
, &wol
);
701 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
706 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
708 struct ethtool_wolinfo wol
;
710 if (!dev
->ethtool_ops
->set_wol
)
713 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
716 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
719 static int ethtool_nway_reset(struct net_device
*dev
)
721 if (!dev
->ethtool_ops
->nway_reset
)
724 return dev
->ethtool_ops
->nway_reset(dev
);
727 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
729 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
731 if (!dev
->ethtool_ops
->get_link
)
734 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
736 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
741 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
743 struct ethtool_eeprom eeprom
;
744 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
745 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
750 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
753 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
756 /* Check for wrap and zero */
757 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
760 /* Check for exceeding total eeprom len */
761 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
764 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
768 bytes_remaining
= eeprom
.len
;
769 while (bytes_remaining
> 0) {
770 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
772 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
775 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
779 userbuf
+= eeprom
.len
;
780 eeprom
.offset
+= eeprom
.len
;
781 bytes_remaining
-= eeprom
.len
;
784 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
785 eeprom
.offset
-= eeprom
.len
;
786 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
793 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
795 struct ethtool_eeprom eeprom
;
796 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
797 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
802 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
805 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
808 /* Check for wrap and zero */
809 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
812 /* Check for exceeding total eeprom len */
813 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
816 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
820 bytes_remaining
= eeprom
.len
;
821 while (bytes_remaining
> 0) {
822 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
824 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
828 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
831 userbuf
+= eeprom
.len
;
832 eeprom
.offset
+= eeprom
.len
;
833 bytes_remaining
-= eeprom
.len
;
840 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
841 void __user
*useraddr
)
843 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
845 if (!dev
->ethtool_ops
->get_coalesce
)
848 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
850 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
855 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
856 void __user
*useraddr
)
858 struct ethtool_coalesce coalesce
;
860 if (!dev
->ethtool_ops
->set_coalesce
)
863 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
866 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
869 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
871 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
873 if (!dev
->ethtool_ops
->get_ringparam
)
876 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
878 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
883 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
885 struct ethtool_ringparam ringparam
;
887 if (!dev
->ethtool_ops
->set_ringparam
)
890 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
893 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
896 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
897 void __user
*useraddr
)
899 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
901 if (!dev
->ethtool_ops
->get_channels
)
904 dev
->ethtool_ops
->get_channels(dev
, &channels
);
906 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
911 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
912 void __user
*useraddr
)
914 struct ethtool_channels channels
;
916 if (!dev
->ethtool_ops
->set_channels
)
919 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
922 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
925 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
927 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
929 if (!dev
->ethtool_ops
->get_pauseparam
)
932 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
934 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
939 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
941 struct ethtool_pauseparam pauseparam
;
943 if (!dev
->ethtool_ops
->set_pauseparam
)
946 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
949 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
952 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
954 struct ethtool_test test
;
955 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
959 if (!ops
->self_test
|| !ops
->get_sset_count
)
962 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
965 WARN_ON(test_len
== 0);
967 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
971 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
975 ops
->self_test(dev
, &test
, data
);
978 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
980 useraddr
+= sizeof(test
);
981 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
990 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
992 struct ethtool_gstrings gstrings
;
996 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
999 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1005 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1009 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1012 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1014 useraddr
+= sizeof(gstrings
);
1015 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1024 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1026 struct ethtool_value id
;
1030 if (!dev
->ethtool_ops
->set_phys_id
)
1036 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1039 rc
= dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1043 /* Drop the RTNL lock while waiting, but prevent reentry or
1044 * removal of the device.
1051 /* Driver will handle this itself */
1052 schedule_timeout_interruptible(
1053 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1055 /* Driver expects to be called at twice the frequency in rc */
1056 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1058 /* Count down seconds */
1060 /* Count down iterations per second */
1064 rc
= dev
->ethtool_ops
->set_phys_id(dev
,
1065 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1069 schedule_timeout_interruptible(interval
);
1070 } while (!signal_pending(current
) && --i
!= 0);
1071 } while (!signal_pending(current
) &&
1072 (id
.data
== 0 || --id
.data
!= 0));
1079 (void)dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1083 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1085 struct ethtool_stats stats
;
1086 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1090 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1093 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1096 WARN_ON(n_stats
== 0);
1098 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1101 stats
.n_stats
= n_stats
;
1102 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1106 ops
->get_ethtool_stats(dev
, &stats
, data
);
1109 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1111 useraddr
+= sizeof(stats
);
1112 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1121 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1123 struct ethtool_perm_addr epaddr
;
1125 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1128 if (epaddr
.size
< dev
->addr_len
)
1130 epaddr
.size
= dev
->addr_len
;
1132 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1134 useraddr
+= sizeof(epaddr
);
1135 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1140 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1141 u32 cmd
, u32 (*actor
)(struct net_device
*))
1143 struct ethtool_value edata
= { .cmd
= cmd
};
1148 edata
.data
= actor(dev
);
1150 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1155 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1156 void (*actor
)(struct net_device
*, u32
))
1158 struct ethtool_value edata
;
1163 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1166 actor(dev
, edata
.data
);
1170 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1171 int (*actor
)(struct net_device
*, u32
))
1173 struct ethtool_value edata
;
1178 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1181 return actor(dev
, edata
.data
);
1184 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1185 char __user
*useraddr
)
1187 struct ethtool_flash efl
;
1189 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1192 if (!dev
->ethtool_ops
->flash_device
)
1195 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
1197 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1200 static int ethtool_set_dump(struct net_device
*dev
,
1201 void __user
*useraddr
)
1203 struct ethtool_dump dump
;
1205 if (!dev
->ethtool_ops
->set_dump
)
1208 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1211 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
1214 static int ethtool_get_dump_flag(struct net_device
*dev
,
1215 void __user
*useraddr
)
1218 struct ethtool_dump dump
;
1219 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1221 if (!dev
->ethtool_ops
->get_dump_flag
)
1224 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1227 ret
= ops
->get_dump_flag(dev
, &dump
);
1231 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
1236 static int ethtool_get_dump_data(struct net_device
*dev
,
1237 void __user
*useraddr
)
1241 struct ethtool_dump dump
, tmp
;
1242 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1245 if (!dev
->ethtool_ops
->get_dump_data
||
1246 !dev
->ethtool_ops
->get_dump_flag
)
1249 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1252 memset(&tmp
, 0, sizeof(tmp
));
1253 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
1254 ret
= ops
->get_dump_flag(dev
, &tmp
);
1258 len
= (tmp
.len
> dump
.len
) ? dump
.len
: tmp
.len
;
1262 data
= vzalloc(tmp
.len
);
1265 ret
= ops
->get_dump_data(dev
, &dump
, data
);
1269 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
1273 useraddr
+= offsetof(struct ethtool_dump
, data
);
1274 if (copy_to_user(useraddr
, data
, len
))
1281 /* The main entry point in this file. Called from net/core/dev.c */
1283 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1285 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1286 void __user
*useraddr
= ifr
->ifr_data
;
1291 if (!dev
|| !netif_device_present(dev
))
1294 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1297 if (!dev
->ethtool_ops
) {
1298 /* ETHTOOL_GDRVINFO does not require any driver support.
1299 * It is also unprivileged and does not change anything,
1300 * so we can take a shortcut to it. */
1301 if (ethcmd
== ETHTOOL_GDRVINFO
)
1302 return ethtool_get_drvinfo(dev
, useraddr
);
1307 /* Allow some commands to be done by anyone */
1310 case ETHTOOL_GDRVINFO
:
1311 case ETHTOOL_GMSGLVL
:
1312 case ETHTOOL_GCOALESCE
:
1313 case ETHTOOL_GRINGPARAM
:
1314 case ETHTOOL_GPAUSEPARAM
:
1315 case ETHTOOL_GRXCSUM
:
1316 case ETHTOOL_GTXCSUM
:
1318 case ETHTOOL_GSSET_INFO
:
1319 case ETHTOOL_GSTRINGS
:
1321 case ETHTOOL_GPERMADDR
:
1325 case ETHTOOL_GFLAGS
:
1326 case ETHTOOL_GPFLAGS
:
1328 case ETHTOOL_GRXRINGS
:
1329 case ETHTOOL_GRXCLSRLCNT
:
1330 case ETHTOOL_GRXCLSRULE
:
1331 case ETHTOOL_GRXCLSRLALL
:
1332 case ETHTOOL_GFEATURES
:
1335 if (!capable(CAP_NET_ADMIN
))
1339 if (dev
->ethtool_ops
->begin
) {
1340 rc
= dev
->ethtool_ops
->begin(dev
);
1344 old_features
= dev
->features
;
1348 rc
= ethtool_get_settings(dev
, useraddr
);
1351 rc
= ethtool_set_settings(dev
, useraddr
);
1353 case ETHTOOL_GDRVINFO
:
1354 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1357 rc
= ethtool_get_regs(dev
, useraddr
);
1360 rc
= ethtool_get_wol(dev
, useraddr
);
1363 rc
= ethtool_set_wol(dev
, useraddr
);
1365 case ETHTOOL_GMSGLVL
:
1366 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1367 dev
->ethtool_ops
->get_msglevel
);
1369 case ETHTOOL_SMSGLVL
:
1370 rc
= ethtool_set_value_void(dev
, useraddr
,
1371 dev
->ethtool_ops
->set_msglevel
);
1373 case ETHTOOL_NWAY_RST
:
1374 rc
= ethtool_nway_reset(dev
);
1377 rc
= ethtool_get_link(dev
, useraddr
);
1379 case ETHTOOL_GEEPROM
:
1380 rc
= ethtool_get_eeprom(dev
, useraddr
);
1382 case ETHTOOL_SEEPROM
:
1383 rc
= ethtool_set_eeprom(dev
, useraddr
);
1385 case ETHTOOL_GCOALESCE
:
1386 rc
= ethtool_get_coalesce(dev
, useraddr
);
1388 case ETHTOOL_SCOALESCE
:
1389 rc
= ethtool_set_coalesce(dev
, useraddr
);
1391 case ETHTOOL_GRINGPARAM
:
1392 rc
= ethtool_get_ringparam(dev
, useraddr
);
1394 case ETHTOOL_SRINGPARAM
:
1395 rc
= ethtool_set_ringparam(dev
, useraddr
);
1397 case ETHTOOL_GPAUSEPARAM
:
1398 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1400 case ETHTOOL_SPAUSEPARAM
:
1401 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1404 rc
= ethtool_self_test(dev
, useraddr
);
1406 case ETHTOOL_GSTRINGS
:
1407 rc
= ethtool_get_strings(dev
, useraddr
);
1409 case ETHTOOL_PHYS_ID
:
1410 rc
= ethtool_phys_id(dev
, useraddr
);
1412 case ETHTOOL_GSTATS
:
1413 rc
= ethtool_get_stats(dev
, useraddr
);
1415 case ETHTOOL_GPERMADDR
:
1416 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1418 case ETHTOOL_GFLAGS
:
1419 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1420 __ethtool_get_flags
);
1422 case ETHTOOL_SFLAGS
:
1423 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
1425 case ETHTOOL_GPFLAGS
:
1426 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1427 dev
->ethtool_ops
->get_priv_flags
);
1429 case ETHTOOL_SPFLAGS
:
1430 rc
= ethtool_set_value(dev
, useraddr
,
1431 dev
->ethtool_ops
->set_priv_flags
);
1434 case ETHTOOL_GRXRINGS
:
1435 case ETHTOOL_GRXCLSRLCNT
:
1436 case ETHTOOL_GRXCLSRULE
:
1437 case ETHTOOL_GRXCLSRLALL
:
1438 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
1441 case ETHTOOL_SRXCLSRLDEL
:
1442 case ETHTOOL_SRXCLSRLINS
:
1443 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
1445 case ETHTOOL_FLASHDEV
:
1446 rc
= ethtool_flash_device(dev
, useraddr
);
1449 rc
= ethtool_reset(dev
, useraddr
);
1451 case ETHTOOL_GSSET_INFO
:
1452 rc
= ethtool_get_sset_info(dev
, useraddr
);
1454 case ETHTOOL_GRXFHINDIR
:
1455 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
1457 case ETHTOOL_SRXFHINDIR
:
1458 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
1460 case ETHTOOL_GFEATURES
:
1461 rc
= ethtool_get_features(dev
, useraddr
);
1463 case ETHTOOL_SFEATURES
:
1464 rc
= ethtool_set_features(dev
, useraddr
);
1466 case ETHTOOL_GTXCSUM
:
1467 case ETHTOOL_GRXCSUM
:
1473 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
1475 case ETHTOOL_STXCSUM
:
1476 case ETHTOOL_SRXCSUM
:
1482 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
1484 case ETHTOOL_GCHANNELS
:
1485 rc
= ethtool_get_channels(dev
, useraddr
);
1487 case ETHTOOL_SCHANNELS
:
1488 rc
= ethtool_set_channels(dev
, useraddr
);
1490 case ETHTOOL_SET_DUMP
:
1491 rc
= ethtool_set_dump(dev
, useraddr
);
1493 case ETHTOOL_GET_DUMP_FLAG
:
1494 rc
= ethtool_get_dump_flag(dev
, useraddr
);
1496 case ETHTOOL_GET_DUMP_DATA
:
1497 rc
= ethtool_get_dump_data(dev
, useraddr
);
1503 if (dev
->ethtool_ops
->complete
)
1504 dev
->ethtool_ops
->complete(dev
);
1506 if (old_features
!= dev
->features
)
1507 netdev_features_change(dev
);