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 u32
ethtool_op_get_tx_csum(struct net_device
*dev
)
41 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
43 EXPORT_SYMBOL(ethtool_op_get_tx_csum
);
45 int ethtool_op_set_tx_csum(struct net_device
*dev
, u32 data
)
48 dev
->features
|= NETIF_F_IP_CSUM
;
50 dev
->features
&= ~NETIF_F_IP_CSUM
;
54 EXPORT_SYMBOL(ethtool_op_set_tx_csum
);
56 int ethtool_op_set_tx_hw_csum(struct net_device
*dev
, u32 data
)
59 dev
->features
|= NETIF_F_HW_CSUM
;
61 dev
->features
&= ~NETIF_F_HW_CSUM
;
65 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum
);
67 int ethtool_op_set_tx_ipv6_csum(struct net_device
*dev
, u32 data
)
70 dev
->features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
72 dev
->features
&= ~(NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
);
76 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum
);
78 u32
ethtool_op_get_sg(struct net_device
*dev
)
80 return (dev
->features
& NETIF_F_SG
) != 0;
82 EXPORT_SYMBOL(ethtool_op_get_sg
);
84 int ethtool_op_set_sg(struct net_device
*dev
, u32 data
)
87 dev
->features
|= NETIF_F_SG
;
89 dev
->features
&= ~NETIF_F_SG
;
93 EXPORT_SYMBOL(ethtool_op_set_sg
);
95 u32
ethtool_op_get_tso(struct net_device
*dev
)
97 return (dev
->features
& NETIF_F_TSO
) != 0;
99 EXPORT_SYMBOL(ethtool_op_get_tso
);
101 int ethtool_op_set_tso(struct net_device
*dev
, u32 data
)
104 dev
->features
|= NETIF_F_TSO
;
106 dev
->features
&= ~NETIF_F_TSO
;
110 EXPORT_SYMBOL(ethtool_op_set_tso
);
112 u32
ethtool_op_get_ufo(struct net_device
*dev
)
114 return (dev
->features
& NETIF_F_UFO
) != 0;
116 EXPORT_SYMBOL(ethtool_op_get_ufo
);
118 int ethtool_op_set_ufo(struct net_device
*dev
, u32 data
)
121 dev
->features
|= NETIF_F_UFO
;
123 dev
->features
&= ~NETIF_F_UFO
;
126 EXPORT_SYMBOL(ethtool_op_set_ufo
);
128 /* the following list of flags are the same as their associated
129 * NETIF_F_xxx values in include/linux/netdevice.h
131 static const u32 flags_dup_features
=
132 (ETH_FLAG_LRO
| ETH_FLAG_RXVLAN
| ETH_FLAG_TXVLAN
| ETH_FLAG_NTUPLE
|
135 u32
ethtool_op_get_flags(struct net_device
*dev
)
137 /* in the future, this function will probably contain additional
138 * handling for flags which are not so easily handled
139 * by a simple masking operation
142 return dev
->features
& flags_dup_features
;
144 EXPORT_SYMBOL(ethtool_op_get_flags
);
146 /* Check if device can enable (or disable) particular feature coded in "data"
147 * argument. Flags "supported" describe features that can be toggled by device.
148 * If feature can not be toggled, it state (enabled or disabled) must match
149 * hardcoded device features state, otherwise flags are marked as invalid.
151 bool ethtool_invalid_flags(struct net_device
*dev
, u32 data
, u32 supported
)
153 u32 features
= dev
->features
& flags_dup_features
;
154 /* "data" can contain only flags_dup_features bits,
155 * see __ethtool_set_flags */
157 return (features
& ~supported
) != (data
& ~supported
);
159 EXPORT_SYMBOL(ethtool_invalid_flags
);
161 int ethtool_op_set_flags(struct net_device
*dev
, u32 data
, u32 supported
)
163 if (ethtool_invalid_flags(dev
, data
, supported
))
166 dev
->features
= ((dev
->features
& ~flags_dup_features
) |
167 (data
& flags_dup_features
));
170 EXPORT_SYMBOL(ethtool_op_set_flags
);
172 void ethtool_ntuple_flush(struct net_device
*dev
)
174 struct ethtool_rx_ntuple_flow_spec_container
*fsc
, *f
;
176 list_for_each_entry_safe(fsc
, f
, &dev
->ethtool_ntuple_list
.list
, list
) {
177 list_del(&fsc
->list
);
180 dev
->ethtool_ntuple_list
.count
= 0;
182 EXPORT_SYMBOL(ethtool_ntuple_flush
);
184 /* Handlers for each ethtool command */
186 #define ETHTOOL_DEV_FEATURE_WORDS 1
188 static void ethtool_get_features_compat(struct net_device
*dev
,
189 struct ethtool_get_features_block
*features
)
191 if (!dev
->ethtool_ops
)
194 /* getting RX checksum */
195 if (dev
->ethtool_ops
->get_rx_csum
)
196 if (dev
->ethtool_ops
->get_rx_csum(dev
))
197 features
[0].active
|= NETIF_F_RXCSUM
;
199 /* mark legacy-changeable features */
200 if (dev
->ethtool_ops
->set_sg
)
201 features
[0].available
|= NETIF_F_SG
;
202 if (dev
->ethtool_ops
->set_tx_csum
)
203 features
[0].available
|= NETIF_F_ALL_CSUM
;
204 if (dev
->ethtool_ops
->set_tso
)
205 features
[0].available
|= NETIF_F_ALL_TSO
;
206 if (dev
->ethtool_ops
->set_rx_csum
)
207 features
[0].available
|= NETIF_F_RXCSUM
;
208 if (dev
->ethtool_ops
->set_flags
)
209 features
[0].available
|= flags_dup_features
;
212 static int ethtool_set_feature_compat(struct net_device
*dev
,
213 int (*legacy_set
)(struct net_device
*, u32
),
214 struct ethtool_set_features_block
*features
, u32 mask
)
221 if (!(features
[0].valid
& mask
))
224 features
[0].valid
&= ~mask
;
226 do_set
= !!(features
[0].requested
& mask
);
228 if (legacy_set(dev
, do_set
) < 0)
230 "Legacy feature change (%s) failed for 0x%08x\n",
231 do_set
? "set" : "clear", mask
);
236 static int ethtool_set_features_compat(struct net_device
*dev
,
237 struct ethtool_set_features_block
*features
)
241 if (!dev
->ethtool_ops
)
244 compat
= ethtool_set_feature_compat(dev
, dev
->ethtool_ops
->set_sg
,
245 features
, NETIF_F_SG
);
246 compat
|= ethtool_set_feature_compat(dev
, dev
->ethtool_ops
->set_tx_csum
,
247 features
, NETIF_F_ALL_CSUM
);
248 compat
|= ethtool_set_feature_compat(dev
, dev
->ethtool_ops
->set_tso
,
249 features
, NETIF_F_ALL_TSO
);
250 compat
|= ethtool_set_feature_compat(dev
, dev
->ethtool_ops
->set_rx_csum
,
251 features
, NETIF_F_RXCSUM
);
252 compat
|= ethtool_set_feature_compat(dev
, dev
->ethtool_ops
->set_flags
,
253 features
, flags_dup_features
);
258 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
260 struct ethtool_gfeatures cmd
= {
261 .cmd
= ETHTOOL_GFEATURES
,
262 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
264 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
] = {
266 .available
= dev
->hw_features
,
267 .requested
= dev
->wanted_features
,
268 .active
= dev
->features
,
269 .never_changed
= NETIF_F_NEVER_CHANGE
,
272 u32 __user
*sizeaddr
;
275 ethtool_get_features_compat(dev
, features
);
277 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
278 if (get_user(copy_size
, sizeaddr
))
281 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
282 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
284 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
286 useraddr
+= sizeof(cmd
);
287 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
293 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
295 struct ethtool_sfeatures cmd
;
296 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
299 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
301 useraddr
+= sizeof(cmd
);
303 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
306 if (copy_from_user(features
, useraddr
, sizeof(features
)))
309 if (features
[0].valid
& ~NETIF_F_ETHTOOL_BITS
)
312 if (ethtool_set_features_compat(dev
, features
))
313 ret
|= ETHTOOL_F_COMPAT
;
315 if (features
[0].valid
& ~dev
->hw_features
) {
316 features
[0].valid
&= dev
->hw_features
;
317 ret
|= ETHTOOL_F_UNSUPPORTED
;
320 dev
->wanted_features
&= ~features
[0].valid
;
321 dev
->wanted_features
|= features
[0].valid
& features
[0].requested
;
322 __netdev_update_features(dev
);
324 if ((dev
->wanted_features
^ dev
->features
) & features
[0].valid
)
325 ret
|= ETHTOOL_F_WISH
;
330 static const char netdev_features_strings
[ETHTOOL_DEV_FEATURE_WORDS
* 32][ETH_GSTRING_LEN
] = {
331 /* NETIF_F_SG */ "tx-scatter-gather",
332 /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
333 /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
334 /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
335 /* NETIF_F_IPV6_CSUM */ "tx_checksum-ipv6",
336 /* NETIF_F_HIGHDMA */ "highdma",
337 /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
338 /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
340 /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
341 /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
342 /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
343 /* NETIF_F_GSO */ "tx-generic-segmentation",
344 /* NETIF_F_LLTX */ "tx-lockless",
345 /* NETIF_F_NETNS_LOCAL */ "netns-local",
346 /* NETIF_F_GRO */ "rx-gro",
347 /* NETIF_F_LRO */ "rx-lro",
349 /* NETIF_F_TSO */ "tx-tcp-segmentation",
350 /* NETIF_F_UFO */ "tx-udp-fragmentation",
351 /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
352 /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
353 /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
354 /* NETIF_F_FSO */ "tx-fcoe-segmentation",
358 /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
359 /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
360 /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
361 /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
362 /* NETIF_F_RXHASH */ "rx-hashing",
363 /* NETIF_F_RXCSUM */ "rx-checksum",
364 /* NETIF_F_NOCACHE_COPY */ "tx-nocache-copy"
365 /* NETIF_F_LOOPBACK */ "loopback",
368 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
370 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
372 if (sset
== ETH_SS_FEATURES
)
373 return ARRAY_SIZE(netdev_features_strings
);
375 if (ops
&& ops
->get_sset_count
&& ops
->get_strings
)
376 return ops
->get_sset_count(dev
, sset
);
381 static void __ethtool_get_strings(struct net_device
*dev
,
382 u32 stringset
, u8
*data
)
384 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
386 if (stringset
== ETH_SS_FEATURES
)
387 memcpy(data
, netdev_features_strings
,
388 sizeof(netdev_features_strings
));
390 /* ops->get_strings is valid because checked earlier */
391 ops
->get_strings(dev
, stringset
, data
);
394 static u32
ethtool_get_feature_mask(u32 eth_cmd
)
396 /* feature masks of legacy discrete ethtool ops */
399 case ETHTOOL_GTXCSUM
:
400 case ETHTOOL_STXCSUM
:
401 return NETIF_F_ALL_CSUM
| NETIF_F_SCTP_CSUM
;
402 case ETHTOOL_GRXCSUM
:
403 case ETHTOOL_SRXCSUM
:
404 return NETIF_F_RXCSUM
;
410 return NETIF_F_ALL_TSO
;
425 static void *__ethtool_get_one_feature_actor(struct net_device
*dev
, u32 ethcmd
)
427 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
433 case ETHTOOL_GTXCSUM
:
434 return ops
->get_tx_csum
;
435 case ETHTOOL_GRXCSUM
:
436 return ops
->get_rx_csum
;
448 static u32
__ethtool_get_rx_csum_oldbug(struct net_device
*dev
)
450 return !!(dev
->features
& NETIF_F_ALL_CSUM
);
453 static int ethtool_get_one_feature(struct net_device
*dev
,
454 char __user
*useraddr
, u32 ethcmd
)
456 u32 mask
= ethtool_get_feature_mask(ethcmd
);
457 struct ethtool_value edata
= {
459 .data
= !!(dev
->features
& mask
),
462 /* compatibility with discrete get_ ops */
463 if (!(dev
->hw_features
& mask
)) {
464 u32 (*actor
)(struct net_device
*);
466 actor
= __ethtool_get_one_feature_actor(dev
, ethcmd
);
468 /* bug compatibility with old get_rx_csum */
469 if (ethcmd
== ETHTOOL_GRXCSUM
&& !actor
)
470 actor
= __ethtool_get_rx_csum_oldbug
;
473 edata
.data
= actor(dev
);
476 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
481 static int __ethtool_set_tx_csum(struct net_device
*dev
, u32 data
);
482 static int __ethtool_set_rx_csum(struct net_device
*dev
, u32 data
);
483 static int __ethtool_set_sg(struct net_device
*dev
, u32 data
);
484 static int __ethtool_set_tso(struct net_device
*dev
, u32 data
);
485 static int __ethtool_set_ufo(struct net_device
*dev
, u32 data
);
487 static int ethtool_set_one_feature(struct net_device
*dev
,
488 void __user
*useraddr
, u32 ethcmd
)
490 struct ethtool_value edata
;
493 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
496 mask
= ethtool_get_feature_mask(ethcmd
);
497 mask
&= dev
->hw_features
;
500 dev
->wanted_features
|= mask
;
502 dev
->wanted_features
&= ~mask
;
504 __netdev_update_features(dev
);
508 /* Driver is not converted to ndo_fix_features or does not
509 * support changing this offload. In the latter case it won't
510 * have corresponding ethtool_ops field set.
512 * Following part is to be removed after all drivers advertise
513 * their changeable features in netdev->hw_features and stop
514 * using discrete offload setting ops.
518 case ETHTOOL_STXCSUM
:
519 return __ethtool_set_tx_csum(dev
, edata
.data
);
520 case ETHTOOL_SRXCSUM
:
521 return __ethtool_set_rx_csum(dev
, edata
.data
);
523 return __ethtool_set_sg(dev
, edata
.data
);
525 return __ethtool_set_tso(dev
, edata
.data
);
527 return __ethtool_set_ufo(dev
, edata
.data
);
533 int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
537 if (data
& ~flags_dup_features
)
540 /* legacy set_flags() op */
541 if (dev
->ethtool_ops
->set_flags
) {
542 if (unlikely(dev
->hw_features
& flags_dup_features
))
544 "driver BUG: mixed hw_features and set_flags()\n");
545 return dev
->ethtool_ops
->set_flags(dev
, data
);
548 /* allow changing only bits set in hw_features */
549 changed
= (data
^ dev
->features
) & flags_dup_features
;
550 if (changed
& ~dev
->hw_features
)
551 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
553 dev
->wanted_features
=
554 (dev
->wanted_features
& ~changed
) | (data
& dev
->hw_features
);
556 __netdev_update_features(dev
);
561 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
563 struct ethtool_cmd cmd
= { .cmd
= ETHTOOL_GSET
};
566 if (!dev
->ethtool_ops
->get_settings
)
569 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
573 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
578 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
580 struct ethtool_cmd cmd
;
582 if (!dev
->ethtool_ops
->set_settings
)
585 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
588 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
591 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
592 void __user
*useraddr
)
594 struct ethtool_drvinfo info
;
595 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
597 memset(&info
, 0, sizeof(info
));
598 info
.cmd
= ETHTOOL_GDRVINFO
;
599 if (ops
&& ops
->get_drvinfo
) {
600 ops
->get_drvinfo(dev
, &info
);
601 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
602 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
603 sizeof(info
.bus_info
));
604 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
605 sizeof(info
.driver
));
611 * this method of obtaining string set info is deprecated;
612 * Use ETHTOOL_GSSET_INFO instead.
614 if (ops
&& ops
->get_sset_count
) {
617 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
619 info
.testinfo_len
= rc
;
620 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
623 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
625 info
.n_priv_flags
= rc
;
627 if (ops
&& ops
->get_regs_len
)
628 info
.regdump_len
= ops
->get_regs_len(dev
);
629 if (ops
&& ops
->get_eeprom_len
)
630 info
.eedump_len
= ops
->get_eeprom_len(dev
);
632 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
637 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
638 void __user
*useraddr
)
640 struct ethtool_sset_info info
;
642 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
643 u32
*info_buf
= NULL
;
645 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
648 /* store copy of mask, because we zero struct later on */
649 sset_mask
= info
.sset_mask
;
653 /* calculate size of return buffer */
654 n_bits
= hweight64(sset_mask
);
656 memset(&info
, 0, sizeof(info
));
657 info
.cmd
= ETHTOOL_GSSET_INFO
;
659 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
664 * fill return buffer based on input bitmask and successful
665 * get_sset_count return
667 for (i
= 0; i
< 64; i
++) {
668 if (!(sset_mask
& (1ULL << i
)))
671 rc
= __ethtool_get_sset_count(dev
, i
);
673 info
.sset_mask
|= (1ULL << i
);
674 info_buf
[idx
++] = rc
;
679 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
682 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
683 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
693 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
694 u32 cmd
, void __user
*useraddr
)
696 struct ethtool_rxnfc info
;
697 size_t info_size
= sizeof(info
);
699 if (!dev
->ethtool_ops
->set_rxnfc
)
702 /* struct ethtool_rxnfc was originally defined for
703 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
704 * members. User-space might still be using that
706 if (cmd
== ETHTOOL_SRXFH
)
707 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
710 if (copy_from_user(&info
, useraddr
, info_size
))
713 return dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
716 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
717 u32 cmd
, void __user
*useraddr
)
719 struct ethtool_rxnfc info
;
720 size_t info_size
= sizeof(info
);
721 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
723 void *rule_buf
= NULL
;
728 /* struct ethtool_rxnfc was originally defined for
729 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
730 * members. User-space might still be using that
732 if (cmd
== ETHTOOL_GRXFH
)
733 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
736 if (copy_from_user(&info
, useraddr
, info_size
))
739 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
740 if (info
.rule_cnt
> 0) {
741 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
742 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
749 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
754 if (copy_to_user(useraddr
, &info
, info_size
))
758 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
759 if (copy_to_user(useraddr
, rule_buf
,
760 info
.rule_cnt
* sizeof(u32
)))
771 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
772 void __user
*useraddr
)
774 struct ethtool_rxfh_indir
*indir
;
779 if (!dev
->ethtool_ops
->get_rxfh_indir
)
782 if (copy_from_user(&table_size
,
783 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
788 (KMALLOC_MAX_SIZE
- sizeof(*indir
)) / sizeof(*indir
->ring_index
))
790 full_size
= sizeof(*indir
) + sizeof(*indir
->ring_index
) * table_size
;
791 indir
= kzalloc(full_size
, GFP_USER
);
795 indir
->cmd
= ETHTOOL_GRXFHINDIR
;
796 indir
->size
= table_size
;
797 ret
= dev
->ethtool_ops
->get_rxfh_indir(dev
, indir
);
801 if (copy_to_user(useraddr
, indir
, full_size
))
809 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
810 void __user
*useraddr
)
812 struct ethtool_rxfh_indir
*indir
;
817 if (!dev
->ethtool_ops
->set_rxfh_indir
)
820 if (copy_from_user(&table_size
,
821 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
826 (KMALLOC_MAX_SIZE
- sizeof(*indir
)) / sizeof(*indir
->ring_index
))
828 full_size
= sizeof(*indir
) + sizeof(*indir
->ring_index
) * table_size
;
829 indir
= kmalloc(full_size
, GFP_USER
);
833 if (copy_from_user(indir
, useraddr
, full_size
)) {
838 ret
= dev
->ethtool_ops
->set_rxfh_indir(dev
, indir
);
845 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list
*list
,
846 struct ethtool_rx_ntuple_flow_spec
*spec
,
847 struct ethtool_rx_ntuple_flow_spec_container
*fsc
)
850 /* don't add filters forever */
851 if (list
->count
>= ETHTOOL_MAX_NTUPLE_LIST_ENTRY
) {
852 /* free the container */
857 /* Copy the whole filter over */
858 fsc
->fs
.flow_type
= spec
->flow_type
;
859 memcpy(&fsc
->fs
.h_u
, &spec
->h_u
, sizeof(spec
->h_u
));
860 memcpy(&fsc
->fs
.m_u
, &spec
->m_u
, sizeof(spec
->m_u
));
862 fsc
->fs
.vlan_tag
= spec
->vlan_tag
;
863 fsc
->fs
.vlan_tag_mask
= spec
->vlan_tag_mask
;
864 fsc
->fs
.data
= spec
->data
;
865 fsc
->fs
.data_mask
= spec
->data_mask
;
866 fsc
->fs
.action
= spec
->action
;
868 /* add to the list */
869 list_add_tail_rcu(&fsc
->list
, &list
->list
);
874 * ethtool does not (or did not) set masks for flow parameters that are
875 * not specified, so if both value and mask are 0 then this must be
876 * treated as equivalent to a mask with all bits set. Implement that
877 * here rather than in drivers.
879 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec
*fs
)
881 struct ethtool_tcpip4_spec
*entry
= &fs
->h_u
.tcp_ip4_spec
;
882 struct ethtool_tcpip4_spec
*mask
= &fs
->m_u
.tcp_ip4_spec
;
884 if (fs
->flow_type
!= TCP_V4_FLOW
&&
885 fs
->flow_type
!= UDP_V4_FLOW
&&
886 fs
->flow_type
!= SCTP_V4_FLOW
)
889 if (!(entry
->ip4src
| mask
->ip4src
))
890 mask
->ip4src
= htonl(0xffffffff);
891 if (!(entry
->ip4dst
| mask
->ip4dst
))
892 mask
->ip4dst
= htonl(0xffffffff);
893 if (!(entry
->psrc
| mask
->psrc
))
894 mask
->psrc
= htons(0xffff);
895 if (!(entry
->pdst
| mask
->pdst
))
896 mask
->pdst
= htons(0xffff);
897 if (!(entry
->tos
| mask
->tos
))
899 if (!(fs
->vlan_tag
| fs
->vlan_tag_mask
))
900 fs
->vlan_tag_mask
= 0xffff;
901 if (!(fs
->data
| fs
->data_mask
))
902 fs
->data_mask
= 0xffffffffffffffffULL
;
905 static noinline_for_stack
int ethtool_set_rx_ntuple(struct net_device
*dev
,
906 void __user
*useraddr
)
908 struct ethtool_rx_ntuple cmd
;
909 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
910 struct ethtool_rx_ntuple_flow_spec_container
*fsc
= NULL
;
913 if (!ops
->set_rx_ntuple
)
916 if (!(dev
->features
& NETIF_F_NTUPLE
))
919 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
922 rx_ntuple_fix_masks(&cmd
.fs
);
925 * Cache filter in dev struct for GET operation only if
926 * the underlying driver doesn't have its own GET operation, and
927 * only if the filter was added successfully. First make sure we
928 * can allocate the filter, then continue if successful.
930 if (!ops
->get_rx_ntuple
) {
931 fsc
= kmalloc(sizeof(*fsc
), GFP_ATOMIC
);
936 ret
= ops
->set_rx_ntuple(dev
, &cmd
);
942 if (!ops
->get_rx_ntuple
)
943 __rx_ntuple_filter_add(&dev
->ethtool_ntuple_list
, &cmd
.fs
, fsc
);
948 static int ethtool_get_rx_ntuple(struct net_device
*dev
, void __user
*useraddr
)
950 struct ethtool_gstrings gstrings
;
951 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
952 struct ethtool_rx_ntuple_flow_spec_container
*fsc
;
955 int ret
, i
, num_strings
= 0;
957 if (!ops
->get_sset_count
)
960 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
963 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
969 data
= kzalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
973 if (ops
->get_rx_ntuple
) {
974 /* driver-specific filter grab */
975 ret
= ops
->get_rx_ntuple(dev
, gstrings
.string_set
, data
);
979 /* default ethtool filter grab */
982 list_for_each_entry(fsc
, &dev
->ethtool_ntuple_list
.list
, list
) {
983 sprintf(p
, "Filter %d:\n", i
);
984 p
+= ETH_GSTRING_LEN
;
987 switch (fsc
->fs
.flow_type
) {
989 sprintf(p
, "\tFlow Type: TCP\n");
990 p
+= ETH_GSTRING_LEN
;
994 sprintf(p
, "\tFlow Type: UDP\n");
995 p
+= ETH_GSTRING_LEN
;
999 sprintf(p
, "\tFlow Type: SCTP\n");
1000 p
+= ETH_GSTRING_LEN
;
1003 case AH_ESP_V4_FLOW
:
1004 sprintf(p
, "\tFlow Type: AH ESP\n");
1005 p
+= ETH_GSTRING_LEN
;
1009 sprintf(p
, "\tFlow Type: ESP\n");
1010 p
+= ETH_GSTRING_LEN
;
1014 sprintf(p
, "\tFlow Type: Raw IP\n");
1015 p
+= ETH_GSTRING_LEN
;
1019 sprintf(p
, "\tFlow Type: IPv4\n");
1020 p
+= ETH_GSTRING_LEN
;
1024 sprintf(p
, "\tFlow Type: Unknown\n");
1025 p
+= ETH_GSTRING_LEN
;
1027 goto unknown_filter
;
1030 /* now the rest of the filters */
1031 switch (fsc
->fs
.flow_type
) {
1035 sprintf(p
, "\tSrc IP addr: 0x%x\n",
1036 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4src
);
1037 p
+= ETH_GSTRING_LEN
;
1039 sprintf(p
, "\tSrc IP mask: 0x%x\n",
1040 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4src
);
1041 p
+= ETH_GSTRING_LEN
;
1043 sprintf(p
, "\tDest IP addr: 0x%x\n",
1044 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4dst
);
1045 p
+= ETH_GSTRING_LEN
;
1047 sprintf(p
, "\tDest IP mask: 0x%x\n",
1048 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4dst
);
1049 p
+= ETH_GSTRING_LEN
;
1051 sprintf(p
, "\tSrc Port: %d, mask: 0x%x\n",
1052 fsc
->fs
.h_u
.tcp_ip4_spec
.psrc
,
1053 fsc
->fs
.m_u
.tcp_ip4_spec
.psrc
);
1054 p
+= ETH_GSTRING_LEN
;
1056 sprintf(p
, "\tDest Port: %d, mask: 0x%x\n",
1057 fsc
->fs
.h_u
.tcp_ip4_spec
.pdst
,
1058 fsc
->fs
.m_u
.tcp_ip4_spec
.pdst
);
1059 p
+= ETH_GSTRING_LEN
;
1061 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
1062 fsc
->fs
.h_u
.tcp_ip4_spec
.tos
,
1063 fsc
->fs
.m_u
.tcp_ip4_spec
.tos
);
1064 p
+= ETH_GSTRING_LEN
;
1067 case AH_ESP_V4_FLOW
:
1069 sprintf(p
, "\tSrc IP addr: 0x%x\n",
1070 fsc
->fs
.h_u
.ah_ip4_spec
.ip4src
);
1071 p
+= ETH_GSTRING_LEN
;
1073 sprintf(p
, "\tSrc IP mask: 0x%x\n",
1074 fsc
->fs
.m_u
.ah_ip4_spec
.ip4src
);
1075 p
+= ETH_GSTRING_LEN
;
1077 sprintf(p
, "\tDest IP addr: 0x%x\n",
1078 fsc
->fs
.h_u
.ah_ip4_spec
.ip4dst
);
1079 p
+= ETH_GSTRING_LEN
;
1081 sprintf(p
, "\tDest IP mask: 0x%x\n",
1082 fsc
->fs
.m_u
.ah_ip4_spec
.ip4dst
);
1083 p
+= ETH_GSTRING_LEN
;
1085 sprintf(p
, "\tSPI: %d, mask: 0x%x\n",
1086 fsc
->fs
.h_u
.ah_ip4_spec
.spi
,
1087 fsc
->fs
.m_u
.ah_ip4_spec
.spi
);
1088 p
+= ETH_GSTRING_LEN
;
1090 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
1091 fsc
->fs
.h_u
.ah_ip4_spec
.tos
,
1092 fsc
->fs
.m_u
.ah_ip4_spec
.tos
);
1093 p
+= ETH_GSTRING_LEN
;
1097 sprintf(p
, "\tSrc IP addr: 0x%x\n",
1098 fsc
->fs
.h_u
.usr_ip4_spec
.ip4src
);
1099 p
+= ETH_GSTRING_LEN
;
1101 sprintf(p
, "\tSrc IP mask: 0x%x\n",
1102 fsc
->fs
.m_u
.usr_ip4_spec
.ip4src
);
1103 p
+= ETH_GSTRING_LEN
;
1105 sprintf(p
, "\tDest IP addr: 0x%x\n",
1106 fsc
->fs
.h_u
.usr_ip4_spec
.ip4dst
);
1107 p
+= ETH_GSTRING_LEN
;
1109 sprintf(p
, "\tDest IP mask: 0x%x\n",
1110 fsc
->fs
.m_u
.usr_ip4_spec
.ip4dst
);
1111 p
+= ETH_GSTRING_LEN
;
1115 sprintf(p
, "\tSrc IP addr: 0x%x\n",
1116 fsc
->fs
.h_u
.usr_ip4_spec
.ip4src
);
1117 p
+= ETH_GSTRING_LEN
;
1119 sprintf(p
, "\tSrc IP mask: 0x%x\n",
1120 fsc
->fs
.m_u
.usr_ip4_spec
.ip4src
);
1121 p
+= ETH_GSTRING_LEN
;
1123 sprintf(p
, "\tDest IP addr: 0x%x\n",
1124 fsc
->fs
.h_u
.usr_ip4_spec
.ip4dst
);
1125 p
+= ETH_GSTRING_LEN
;
1127 sprintf(p
, "\tDest IP mask: 0x%x\n",
1128 fsc
->fs
.m_u
.usr_ip4_spec
.ip4dst
);
1129 p
+= ETH_GSTRING_LEN
;
1131 sprintf(p
, "\tL4 bytes: 0x%x, mask: 0x%x\n",
1132 fsc
->fs
.h_u
.usr_ip4_spec
.l4_4_bytes
,
1133 fsc
->fs
.m_u
.usr_ip4_spec
.l4_4_bytes
);
1134 p
+= ETH_GSTRING_LEN
;
1136 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
1137 fsc
->fs
.h_u
.usr_ip4_spec
.tos
,
1138 fsc
->fs
.m_u
.usr_ip4_spec
.tos
);
1139 p
+= ETH_GSTRING_LEN
;
1141 sprintf(p
, "\tIP Version: %d, mask: 0x%x\n",
1142 fsc
->fs
.h_u
.usr_ip4_spec
.ip_ver
,
1143 fsc
->fs
.m_u
.usr_ip4_spec
.ip_ver
);
1144 p
+= ETH_GSTRING_LEN
;
1146 sprintf(p
, "\tProtocol: %d, mask: 0x%x\n",
1147 fsc
->fs
.h_u
.usr_ip4_spec
.proto
,
1148 fsc
->fs
.m_u
.usr_ip4_spec
.proto
);
1149 p
+= ETH_GSTRING_LEN
;
1153 sprintf(p
, "\tVLAN: %d, mask: 0x%x\n",
1154 fsc
->fs
.vlan_tag
, fsc
->fs
.vlan_tag_mask
);
1155 p
+= ETH_GSTRING_LEN
;
1157 sprintf(p
, "\tUser-defined: 0x%Lx\n", fsc
->fs
.data
);
1158 p
+= ETH_GSTRING_LEN
;
1160 sprintf(p
, "\tUser-defined mask: 0x%Lx\n", fsc
->fs
.data_mask
);
1161 p
+= ETH_GSTRING_LEN
;
1163 if (fsc
->fs
.action
== ETHTOOL_RXNTUPLE_ACTION_DROP
)
1164 sprintf(p
, "\tAction: Drop\n");
1166 sprintf(p
, "\tAction: Direct to queue %d\n",
1168 p
+= ETH_GSTRING_LEN
;
1174 /* indicate to userspace how many strings we actually have */
1175 gstrings
.len
= num_strings
;
1177 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1179 useraddr
+= sizeof(gstrings
);
1180 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1189 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
1191 struct ethtool_regs regs
;
1192 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1196 if (!ops
->get_regs
|| !ops
->get_regs_len
)
1199 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
1202 reglen
= ops
->get_regs_len(dev
);
1203 if (regs
.len
> reglen
)
1206 regbuf
= vzalloc(reglen
);
1210 ops
->get_regs(dev
, ®s
, regbuf
);
1213 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
1215 useraddr
+= offsetof(struct ethtool_regs
, data
);
1216 if (copy_to_user(useraddr
, regbuf
, regs
.len
))
1225 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
1227 struct ethtool_value reset
;
1230 if (!dev
->ethtool_ops
->reset
)
1233 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
1236 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
1240 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
1245 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
1247 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
1249 if (!dev
->ethtool_ops
->get_wol
)
1252 dev
->ethtool_ops
->get_wol(dev
, &wol
);
1254 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
1259 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
1261 struct ethtool_wolinfo wol
;
1263 if (!dev
->ethtool_ops
->set_wol
)
1266 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
1269 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
1272 static int ethtool_nway_reset(struct net_device
*dev
)
1274 if (!dev
->ethtool_ops
->nway_reset
)
1277 return dev
->ethtool_ops
->nway_reset(dev
);
1280 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
1282 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
1284 if (!dev
->ethtool_ops
->get_link
)
1287 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
1289 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1294 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1296 struct ethtool_eeprom eeprom
;
1297 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1298 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1299 u32 bytes_remaining
;
1303 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
1306 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1309 /* Check for wrap and zero */
1310 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1313 /* Check for exceeding total eeprom len */
1314 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1317 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1321 bytes_remaining
= eeprom
.len
;
1322 while (bytes_remaining
> 0) {
1323 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1325 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
1328 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
1332 userbuf
+= eeprom
.len
;
1333 eeprom
.offset
+= eeprom
.len
;
1334 bytes_remaining
-= eeprom
.len
;
1337 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
1338 eeprom
.offset
-= eeprom
.len
;
1339 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
1346 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1348 struct ethtool_eeprom eeprom
;
1349 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1350 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1351 u32 bytes_remaining
;
1355 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
1358 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1361 /* Check for wrap and zero */
1362 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1365 /* Check for exceeding total eeprom len */
1366 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1369 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1373 bytes_remaining
= eeprom
.len
;
1374 while (bytes_remaining
> 0) {
1375 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1377 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
1381 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
1384 userbuf
+= eeprom
.len
;
1385 eeprom
.offset
+= eeprom
.len
;
1386 bytes_remaining
-= eeprom
.len
;
1393 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
1394 void __user
*useraddr
)
1396 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
1398 if (!dev
->ethtool_ops
->get_coalesce
)
1401 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
1403 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
1408 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
1409 void __user
*useraddr
)
1411 struct ethtool_coalesce coalesce
;
1413 if (!dev
->ethtool_ops
->set_coalesce
)
1416 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
1419 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
1422 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1424 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
1426 if (!dev
->ethtool_ops
->get_ringparam
)
1429 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
1431 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
1436 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1438 struct ethtool_ringparam ringparam
;
1440 if (!dev
->ethtool_ops
->set_ringparam
)
1443 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
1446 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
1449 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
1450 void __user
*useraddr
)
1452 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
1454 if (!dev
->ethtool_ops
->get_channels
)
1457 dev
->ethtool_ops
->get_channels(dev
, &channels
);
1459 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
1464 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
1465 void __user
*useraddr
)
1467 struct ethtool_channels channels
;
1469 if (!dev
->ethtool_ops
->set_channels
)
1472 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
1475 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
1478 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1480 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
1482 if (!dev
->ethtool_ops
->get_pauseparam
)
1485 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
1487 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
1492 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1494 struct ethtool_pauseparam pauseparam
;
1496 if (!dev
->ethtool_ops
->set_pauseparam
)
1499 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
1502 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
1505 static int __ethtool_set_sg(struct net_device
*dev
, u32 data
)
1509 if (!dev
->ethtool_ops
->set_sg
)
1512 if (data
&& !(dev
->features
& NETIF_F_ALL_CSUM
))
1515 if (!data
&& dev
->ethtool_ops
->set_tso
) {
1516 err
= dev
->ethtool_ops
->set_tso(dev
, 0);
1521 if (!data
&& dev
->ethtool_ops
->set_ufo
) {
1522 err
= dev
->ethtool_ops
->set_ufo(dev
, 0);
1526 return dev
->ethtool_ops
->set_sg(dev
, data
);
1529 static int __ethtool_set_tx_csum(struct net_device
*dev
, u32 data
)
1533 if (!dev
->ethtool_ops
->set_tx_csum
)
1536 if (!data
&& dev
->ethtool_ops
->set_sg
) {
1537 err
= __ethtool_set_sg(dev
, 0);
1542 return dev
->ethtool_ops
->set_tx_csum(dev
, data
);
1545 static int __ethtool_set_rx_csum(struct net_device
*dev
, u32 data
)
1547 if (!dev
->ethtool_ops
->set_rx_csum
)
1551 dev
->features
&= ~NETIF_F_GRO
;
1553 return dev
->ethtool_ops
->set_rx_csum(dev
, data
);
1556 static int __ethtool_set_tso(struct net_device
*dev
, u32 data
)
1558 if (!dev
->ethtool_ops
->set_tso
)
1561 if (data
&& !(dev
->features
& NETIF_F_SG
))
1564 return dev
->ethtool_ops
->set_tso(dev
, data
);
1567 static int __ethtool_set_ufo(struct net_device
*dev
, u32 data
)
1569 if (!dev
->ethtool_ops
->set_ufo
)
1571 if (data
&& !(dev
->features
& NETIF_F_SG
))
1573 if (data
&& !((dev
->features
& NETIF_F_GEN_CSUM
) ||
1574 (dev
->features
& (NETIF_F_IP_CSUM
|NETIF_F_IPV6_CSUM
))
1575 == (NETIF_F_IP_CSUM
|NETIF_F_IPV6_CSUM
)))
1577 return dev
->ethtool_ops
->set_ufo(dev
, data
);
1580 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1582 struct ethtool_test test
;
1583 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1587 if (!ops
->self_test
|| !ops
->get_sset_count
)
1590 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1593 WARN_ON(test_len
== 0);
1595 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1598 test
.len
= test_len
;
1599 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1603 ops
->self_test(dev
, &test
, data
);
1606 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1608 useraddr
+= sizeof(test
);
1609 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1618 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1620 struct ethtool_gstrings gstrings
;
1624 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1627 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1633 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1637 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1640 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1642 useraddr
+= sizeof(gstrings
);
1643 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1652 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1654 struct ethtool_value id
;
1658 if (!dev
->ethtool_ops
->set_phys_id
)
1664 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1667 rc
= dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1671 /* Drop the RTNL lock while waiting, but prevent reentry or
1672 * removal of the device.
1679 /* Driver will handle this itself */
1680 schedule_timeout_interruptible(
1681 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1683 /* Driver expects to be called at twice the frequency in rc */
1684 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1686 /* Count down seconds */
1688 /* Count down iterations per second */
1692 rc
= dev
->ethtool_ops
->set_phys_id(dev
,
1693 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1697 schedule_timeout_interruptible(interval
);
1698 } while (!signal_pending(current
) && --i
!= 0);
1699 } while (!signal_pending(current
) &&
1700 (id
.data
== 0 || --id
.data
!= 0));
1707 (void)dev
->ethtool_ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1711 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1713 struct ethtool_stats stats
;
1714 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1718 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1721 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1724 WARN_ON(n_stats
== 0);
1726 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1729 stats
.n_stats
= n_stats
;
1730 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1734 ops
->get_ethtool_stats(dev
, &stats
, data
);
1737 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1739 useraddr
+= sizeof(stats
);
1740 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1749 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1751 struct ethtool_perm_addr epaddr
;
1753 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1756 if (epaddr
.size
< dev
->addr_len
)
1758 epaddr
.size
= dev
->addr_len
;
1760 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1762 useraddr
+= sizeof(epaddr
);
1763 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1768 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1769 u32 cmd
, u32 (*actor
)(struct net_device
*))
1771 struct ethtool_value edata
= { .cmd
= cmd
};
1776 edata
.data
= actor(dev
);
1778 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1783 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1784 void (*actor
)(struct net_device
*, u32
))
1786 struct ethtool_value edata
;
1791 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1794 actor(dev
, edata
.data
);
1798 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1799 int (*actor
)(struct net_device
*, u32
))
1801 struct ethtool_value edata
;
1806 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1809 return actor(dev
, edata
.data
);
1812 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1813 char __user
*useraddr
)
1815 struct ethtool_flash efl
;
1817 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1820 if (!dev
->ethtool_ops
->flash_device
)
1823 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1826 /* The main entry point in this file. Called from net/core/dev.c */
1828 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1830 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1831 void __user
*useraddr
= ifr
->ifr_data
;
1836 if (!dev
|| !netif_device_present(dev
))
1839 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1842 if (!dev
->ethtool_ops
) {
1843 /* ETHTOOL_GDRVINFO does not require any driver support.
1844 * It is also unprivileged and does not change anything,
1845 * so we can take a shortcut to it. */
1846 if (ethcmd
== ETHTOOL_GDRVINFO
)
1847 return ethtool_get_drvinfo(dev
, useraddr
);
1852 /* Allow some commands to be done by anyone */
1855 case ETHTOOL_GDRVINFO
:
1856 case ETHTOOL_GMSGLVL
:
1857 case ETHTOOL_GCOALESCE
:
1858 case ETHTOOL_GRINGPARAM
:
1859 case ETHTOOL_GPAUSEPARAM
:
1860 case ETHTOOL_GRXCSUM
:
1861 case ETHTOOL_GTXCSUM
:
1863 case ETHTOOL_GSTRINGS
:
1865 case ETHTOOL_GPERMADDR
:
1869 case ETHTOOL_GFLAGS
:
1870 case ETHTOOL_GPFLAGS
:
1872 case ETHTOOL_GRXRINGS
:
1873 case ETHTOOL_GRXCLSRLCNT
:
1874 case ETHTOOL_GRXCLSRULE
:
1875 case ETHTOOL_GRXCLSRLALL
:
1876 case ETHTOOL_GFEATURES
:
1879 if (!capable(CAP_NET_ADMIN
))
1883 if (dev
->ethtool_ops
->begin
) {
1884 rc
= dev
->ethtool_ops
->begin(dev
);
1888 old_features
= dev
->features
;
1892 rc
= ethtool_get_settings(dev
, useraddr
);
1895 rc
= ethtool_set_settings(dev
, useraddr
);
1897 case ETHTOOL_GDRVINFO
:
1898 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1901 rc
= ethtool_get_regs(dev
, useraddr
);
1904 rc
= ethtool_get_wol(dev
, useraddr
);
1907 rc
= ethtool_set_wol(dev
, useraddr
);
1909 case ETHTOOL_GMSGLVL
:
1910 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1911 dev
->ethtool_ops
->get_msglevel
);
1913 case ETHTOOL_SMSGLVL
:
1914 rc
= ethtool_set_value_void(dev
, useraddr
,
1915 dev
->ethtool_ops
->set_msglevel
);
1917 case ETHTOOL_NWAY_RST
:
1918 rc
= ethtool_nway_reset(dev
);
1921 rc
= ethtool_get_link(dev
, useraddr
);
1923 case ETHTOOL_GEEPROM
:
1924 rc
= ethtool_get_eeprom(dev
, useraddr
);
1926 case ETHTOOL_SEEPROM
:
1927 rc
= ethtool_set_eeprom(dev
, useraddr
);
1929 case ETHTOOL_GCOALESCE
:
1930 rc
= ethtool_get_coalesce(dev
, useraddr
);
1932 case ETHTOOL_SCOALESCE
:
1933 rc
= ethtool_set_coalesce(dev
, useraddr
);
1935 case ETHTOOL_GRINGPARAM
:
1936 rc
= ethtool_get_ringparam(dev
, useraddr
);
1938 case ETHTOOL_SRINGPARAM
:
1939 rc
= ethtool_set_ringparam(dev
, useraddr
);
1941 case ETHTOOL_GPAUSEPARAM
:
1942 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1944 case ETHTOOL_SPAUSEPARAM
:
1945 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1948 rc
= ethtool_self_test(dev
, useraddr
);
1950 case ETHTOOL_GSTRINGS
:
1951 rc
= ethtool_get_strings(dev
, useraddr
);
1953 case ETHTOOL_PHYS_ID
:
1954 rc
= ethtool_phys_id(dev
, useraddr
);
1956 case ETHTOOL_GSTATS
:
1957 rc
= ethtool_get_stats(dev
, useraddr
);
1959 case ETHTOOL_GPERMADDR
:
1960 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1962 case ETHTOOL_GFLAGS
:
1963 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1964 (dev
->ethtool_ops
->get_flags
?
1965 dev
->ethtool_ops
->get_flags
:
1966 ethtool_op_get_flags
));
1968 case ETHTOOL_SFLAGS
:
1969 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
1971 case ETHTOOL_GPFLAGS
:
1972 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1973 dev
->ethtool_ops
->get_priv_flags
);
1975 case ETHTOOL_SPFLAGS
:
1976 rc
= ethtool_set_value(dev
, useraddr
,
1977 dev
->ethtool_ops
->set_priv_flags
);
1980 case ETHTOOL_GRXRINGS
:
1981 case ETHTOOL_GRXCLSRLCNT
:
1982 case ETHTOOL_GRXCLSRULE
:
1983 case ETHTOOL_GRXCLSRLALL
:
1984 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
1987 case ETHTOOL_SRXCLSRLDEL
:
1988 case ETHTOOL_SRXCLSRLINS
:
1989 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
1991 case ETHTOOL_FLASHDEV
:
1992 rc
= ethtool_flash_device(dev
, useraddr
);
1995 rc
= ethtool_reset(dev
, useraddr
);
1997 case ETHTOOL_SRXNTUPLE
:
1998 rc
= ethtool_set_rx_ntuple(dev
, useraddr
);
2000 case ETHTOOL_GRXNTUPLE
:
2001 rc
= ethtool_get_rx_ntuple(dev
, useraddr
);
2003 case ETHTOOL_GSSET_INFO
:
2004 rc
= ethtool_get_sset_info(dev
, useraddr
);
2006 case ETHTOOL_GRXFHINDIR
:
2007 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2009 case ETHTOOL_SRXFHINDIR
:
2010 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2012 case ETHTOOL_GFEATURES
:
2013 rc
= ethtool_get_features(dev
, useraddr
);
2015 case ETHTOOL_SFEATURES
:
2016 rc
= ethtool_set_features(dev
, useraddr
);
2018 case ETHTOOL_GTXCSUM
:
2019 case ETHTOOL_GRXCSUM
:
2025 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2027 case ETHTOOL_STXCSUM
:
2028 case ETHTOOL_SRXCSUM
:
2034 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2036 case ETHTOOL_GCHANNELS
:
2037 rc
= ethtool_get_channels(dev
, useraddr
);
2039 case ETHTOOL_SCHANNELS
:
2040 rc
= ethtool_set_channels(dev
, useraddr
);
2046 if (dev
->ethtool_ops
->complete
)
2047 dev
->ethtool_ops
->complete(dev
);
2049 if (old_features
!= dev
->features
)
2050 netdev_features_change(dev
);