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/slab.h>
25 * Some useful ethtool_ops methods that're device independent.
26 * If we find that all drivers want to do the same thing here,
27 * we can turn these into dev_() function calls.
30 u32
ethtool_op_get_link(struct net_device
*dev
)
32 return netif_carrier_ok(dev
) ? 1 : 0;
34 EXPORT_SYMBOL(ethtool_op_get_link
);
36 u32
ethtool_op_get_rx_csum(struct net_device
*dev
)
38 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
40 EXPORT_SYMBOL(ethtool_op_get_rx_csum
);
42 u32
ethtool_op_get_tx_csum(struct net_device
*dev
)
44 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
46 EXPORT_SYMBOL(ethtool_op_get_tx_csum
);
48 int ethtool_op_set_tx_csum(struct net_device
*dev
, u32 data
)
51 dev
->features
|= NETIF_F_IP_CSUM
;
53 dev
->features
&= ~NETIF_F_IP_CSUM
;
58 int ethtool_op_set_tx_hw_csum(struct net_device
*dev
, u32 data
)
61 dev
->features
|= NETIF_F_HW_CSUM
;
63 dev
->features
&= ~NETIF_F_HW_CSUM
;
67 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum
);
69 int ethtool_op_set_tx_ipv6_csum(struct net_device
*dev
, u32 data
)
72 dev
->features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
74 dev
->features
&= ~(NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
);
78 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum
);
80 u32
ethtool_op_get_sg(struct net_device
*dev
)
82 return (dev
->features
& NETIF_F_SG
) != 0;
84 EXPORT_SYMBOL(ethtool_op_get_sg
);
86 int ethtool_op_set_sg(struct net_device
*dev
, u32 data
)
89 dev
->features
|= NETIF_F_SG
;
91 dev
->features
&= ~NETIF_F_SG
;
95 EXPORT_SYMBOL(ethtool_op_set_sg
);
97 u32
ethtool_op_get_tso(struct net_device
*dev
)
99 return (dev
->features
& NETIF_F_TSO
) != 0;
101 EXPORT_SYMBOL(ethtool_op_get_tso
);
103 int ethtool_op_set_tso(struct net_device
*dev
, u32 data
)
106 dev
->features
|= NETIF_F_TSO
;
108 dev
->features
&= ~NETIF_F_TSO
;
112 EXPORT_SYMBOL(ethtool_op_set_tso
);
114 u32
ethtool_op_get_ufo(struct net_device
*dev
)
116 return (dev
->features
& NETIF_F_UFO
) != 0;
118 EXPORT_SYMBOL(ethtool_op_get_ufo
);
120 int ethtool_op_set_ufo(struct net_device
*dev
, u32 data
)
123 dev
->features
|= NETIF_F_UFO
;
125 dev
->features
&= ~NETIF_F_UFO
;
128 EXPORT_SYMBOL(ethtool_op_set_ufo
);
130 /* the following list of flags are the same as their associated
131 * NETIF_F_xxx values in include/linux/netdevice.h
133 static const u32 flags_dup_features
=
134 (ETH_FLAG_LRO
| ETH_FLAG_NTUPLE
| ETH_FLAG_RXHASH
);
136 u32
ethtool_op_get_flags(struct net_device
*dev
)
138 /* in the future, this function will probably contain additional
139 * handling for flags which are not so easily handled
140 * by a simple masking operation
143 return dev
->features
& flags_dup_features
;
145 EXPORT_SYMBOL(ethtool_op_get_flags
);
147 int ethtool_op_set_flags(struct net_device
*dev
, u32 data
)
149 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
150 unsigned long features
= dev
->features
;
152 if (data
& ETH_FLAG_LRO
)
153 features
|= NETIF_F_LRO
;
155 features
&= ~NETIF_F_LRO
;
157 if (data
& ETH_FLAG_NTUPLE
) {
158 if (!ops
->set_rx_ntuple
)
160 features
|= NETIF_F_NTUPLE
;
162 /* safe to clear regardless */
163 features
&= ~NETIF_F_NTUPLE
;
166 if (data
& ETH_FLAG_RXHASH
)
167 features
|= NETIF_F_RXHASH
;
169 features
&= ~NETIF_F_RXHASH
;
171 dev
->features
= features
;
174 EXPORT_SYMBOL(ethtool_op_set_flags
);
176 void ethtool_ntuple_flush(struct net_device
*dev
)
178 struct ethtool_rx_ntuple_flow_spec_container
*fsc
, *f
;
180 list_for_each_entry_safe(fsc
, f
, &dev
->ethtool_ntuple_list
.list
, list
) {
181 list_del(&fsc
->list
);
184 dev
->ethtool_ntuple_list
.count
= 0;
186 EXPORT_SYMBOL(ethtool_ntuple_flush
);
188 /* Handlers for each ethtool command */
190 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
192 struct ethtool_cmd cmd
= { .cmd
= ETHTOOL_GSET
};
195 if (!dev
->ethtool_ops
->get_settings
)
198 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
202 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
207 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
209 struct ethtool_cmd cmd
;
211 if (!dev
->ethtool_ops
->set_settings
)
214 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
217 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
220 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
221 void __user
*useraddr
)
223 struct ethtool_drvinfo info
;
224 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
226 if (!ops
->get_drvinfo
)
229 memset(&info
, 0, sizeof(info
));
230 info
.cmd
= ETHTOOL_GDRVINFO
;
231 ops
->get_drvinfo(dev
, &info
);
234 * this method of obtaining string set info is deprecated;
235 * Use ETHTOOL_GSSET_INFO instead.
237 if (ops
->get_sset_count
) {
240 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
242 info
.testinfo_len
= rc
;
243 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
246 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
248 info
.n_priv_flags
= rc
;
250 if (ops
->get_regs_len
)
251 info
.regdump_len
= ops
->get_regs_len(dev
);
252 if (ops
->get_eeprom_len
)
253 info
.eedump_len
= ops
->get_eeprom_len(dev
);
255 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
260 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
261 void __user
*useraddr
)
263 struct ethtool_sset_info info
;
264 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
266 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
267 u32
*info_buf
= NULL
;
269 if (!ops
->get_sset_count
)
272 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
275 /* store copy of mask, because we zero struct later on */
276 sset_mask
= info
.sset_mask
;
280 /* calculate size of return buffer */
281 n_bits
= hweight64(sset_mask
);
283 memset(&info
, 0, sizeof(info
));
284 info
.cmd
= ETHTOOL_GSSET_INFO
;
286 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
291 * fill return buffer based on input bitmask and successful
292 * get_sset_count return
294 for (i
= 0; i
< 64; i
++) {
295 if (!(sset_mask
& (1ULL << i
)))
298 rc
= ops
->get_sset_count(dev
, i
);
300 info
.sset_mask
|= (1ULL << i
);
301 info_buf
[idx
++] = rc
;
306 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
309 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
310 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
320 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
321 void __user
*useraddr
)
323 struct ethtool_rxnfc cmd
;
325 if (!dev
->ethtool_ops
->set_rxnfc
)
328 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
331 return dev
->ethtool_ops
->set_rxnfc(dev
, &cmd
);
334 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
335 void __user
*useraddr
)
337 struct ethtool_rxnfc info
;
338 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
340 void *rule_buf
= NULL
;
345 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
348 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
349 if (info
.rule_cnt
> 0) {
350 rule_buf
= kmalloc(info
.rule_cnt
* sizeof(u32
),
357 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
362 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
366 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
367 if (copy_to_user(useraddr
, rule_buf
,
368 info
.rule_cnt
* sizeof(u32
)))
379 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list
*list
,
380 struct ethtool_rx_ntuple_flow_spec
*spec
,
381 struct ethtool_rx_ntuple_flow_spec_container
*fsc
)
384 /* don't add filters forever */
385 if (list
->count
>= ETHTOOL_MAX_NTUPLE_LIST_ENTRY
) {
386 /* free the container */
391 /* Copy the whole filter over */
392 fsc
->fs
.flow_type
= spec
->flow_type
;
393 memcpy(&fsc
->fs
.h_u
, &spec
->h_u
, sizeof(spec
->h_u
));
394 memcpy(&fsc
->fs
.m_u
, &spec
->m_u
, sizeof(spec
->m_u
));
396 fsc
->fs
.vlan_tag
= spec
->vlan_tag
;
397 fsc
->fs
.vlan_tag_mask
= spec
->vlan_tag_mask
;
398 fsc
->fs
.data
= spec
->data
;
399 fsc
->fs
.data_mask
= spec
->data_mask
;
400 fsc
->fs
.action
= spec
->action
;
402 /* add to the list */
403 list_add_tail_rcu(&fsc
->list
, &list
->list
);
407 static noinline_for_stack
int ethtool_set_rx_ntuple(struct net_device
*dev
,
408 void __user
*useraddr
)
410 struct ethtool_rx_ntuple cmd
;
411 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
412 struct ethtool_rx_ntuple_flow_spec_container
*fsc
= NULL
;
415 if (!(dev
->features
& NETIF_F_NTUPLE
))
418 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
422 * Cache filter in dev struct for GET operation only if
423 * the underlying driver doesn't have its own GET operation, and
424 * only if the filter was added successfully. First make sure we
425 * can allocate the filter, then continue if successful.
427 if (!ops
->get_rx_ntuple
) {
428 fsc
= kmalloc(sizeof(*fsc
), GFP_ATOMIC
);
433 ret
= ops
->set_rx_ntuple(dev
, &cmd
);
439 if (!ops
->get_rx_ntuple
)
440 __rx_ntuple_filter_add(&dev
->ethtool_ntuple_list
, &cmd
.fs
, fsc
);
445 static int ethtool_get_rx_ntuple(struct net_device
*dev
, void __user
*useraddr
)
447 struct ethtool_gstrings gstrings
;
448 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
449 struct ethtool_rx_ntuple_flow_spec_container
*fsc
;
452 int ret
, i
, num_strings
= 0;
454 if (!ops
->get_sset_count
)
457 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
460 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
466 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
470 if (ops
->get_rx_ntuple
) {
471 /* driver-specific filter grab */
472 ret
= ops
->get_rx_ntuple(dev
, gstrings
.string_set
, data
);
476 /* default ethtool filter grab */
479 list_for_each_entry(fsc
, &dev
->ethtool_ntuple_list
.list
, list
) {
480 sprintf(p
, "Filter %d:\n", i
);
481 p
+= ETH_GSTRING_LEN
;
484 switch (fsc
->fs
.flow_type
) {
486 sprintf(p
, "\tFlow Type: TCP\n");
487 p
+= ETH_GSTRING_LEN
;
491 sprintf(p
, "\tFlow Type: UDP\n");
492 p
+= ETH_GSTRING_LEN
;
496 sprintf(p
, "\tFlow Type: SCTP\n");
497 p
+= ETH_GSTRING_LEN
;
501 sprintf(p
, "\tFlow Type: AH ESP\n");
502 p
+= ETH_GSTRING_LEN
;
506 sprintf(p
, "\tFlow Type: ESP\n");
507 p
+= ETH_GSTRING_LEN
;
511 sprintf(p
, "\tFlow Type: Raw IP\n");
512 p
+= ETH_GSTRING_LEN
;
516 sprintf(p
, "\tFlow Type: IPv4\n");
517 p
+= ETH_GSTRING_LEN
;
521 sprintf(p
, "\tFlow Type: Unknown\n");
522 p
+= ETH_GSTRING_LEN
;
527 /* now the rest of the filters */
528 switch (fsc
->fs
.flow_type
) {
532 sprintf(p
, "\tSrc IP addr: 0x%x\n",
533 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4src
);
534 p
+= ETH_GSTRING_LEN
;
536 sprintf(p
, "\tSrc IP mask: 0x%x\n",
537 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4src
);
538 p
+= ETH_GSTRING_LEN
;
540 sprintf(p
, "\tDest IP addr: 0x%x\n",
541 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4dst
);
542 p
+= ETH_GSTRING_LEN
;
544 sprintf(p
, "\tDest IP mask: 0x%x\n",
545 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4dst
);
546 p
+= ETH_GSTRING_LEN
;
548 sprintf(p
, "\tSrc Port: %d, mask: 0x%x\n",
549 fsc
->fs
.h_u
.tcp_ip4_spec
.psrc
,
550 fsc
->fs
.m_u
.tcp_ip4_spec
.psrc
);
551 p
+= ETH_GSTRING_LEN
;
553 sprintf(p
, "\tDest Port: %d, mask: 0x%x\n",
554 fsc
->fs
.h_u
.tcp_ip4_spec
.pdst
,
555 fsc
->fs
.m_u
.tcp_ip4_spec
.pdst
);
556 p
+= ETH_GSTRING_LEN
;
558 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
559 fsc
->fs
.h_u
.tcp_ip4_spec
.tos
,
560 fsc
->fs
.m_u
.tcp_ip4_spec
.tos
);
561 p
+= ETH_GSTRING_LEN
;
566 sprintf(p
, "\tSrc IP addr: 0x%x\n",
567 fsc
->fs
.h_u
.ah_ip4_spec
.ip4src
);
568 p
+= ETH_GSTRING_LEN
;
570 sprintf(p
, "\tSrc IP mask: 0x%x\n",
571 fsc
->fs
.m_u
.ah_ip4_spec
.ip4src
);
572 p
+= ETH_GSTRING_LEN
;
574 sprintf(p
, "\tDest IP addr: 0x%x\n",
575 fsc
->fs
.h_u
.ah_ip4_spec
.ip4dst
);
576 p
+= ETH_GSTRING_LEN
;
578 sprintf(p
, "\tDest IP mask: 0x%x\n",
579 fsc
->fs
.m_u
.ah_ip4_spec
.ip4dst
);
580 p
+= ETH_GSTRING_LEN
;
582 sprintf(p
, "\tSPI: %d, mask: 0x%x\n",
583 fsc
->fs
.h_u
.ah_ip4_spec
.spi
,
584 fsc
->fs
.m_u
.ah_ip4_spec
.spi
);
585 p
+= ETH_GSTRING_LEN
;
587 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
588 fsc
->fs
.h_u
.ah_ip4_spec
.tos
,
589 fsc
->fs
.m_u
.ah_ip4_spec
.tos
);
590 p
+= ETH_GSTRING_LEN
;
594 sprintf(p
, "\tSrc IP addr: 0x%x\n",
595 fsc
->fs
.h_u
.raw_ip4_spec
.ip4src
);
596 p
+= ETH_GSTRING_LEN
;
598 sprintf(p
, "\tSrc IP mask: 0x%x\n",
599 fsc
->fs
.m_u
.raw_ip4_spec
.ip4src
);
600 p
+= ETH_GSTRING_LEN
;
602 sprintf(p
, "\tDest IP addr: 0x%x\n",
603 fsc
->fs
.h_u
.raw_ip4_spec
.ip4dst
);
604 p
+= ETH_GSTRING_LEN
;
606 sprintf(p
, "\tDest IP mask: 0x%x\n",
607 fsc
->fs
.m_u
.raw_ip4_spec
.ip4dst
);
608 p
+= ETH_GSTRING_LEN
;
612 sprintf(p
, "\tSrc IP addr: 0x%x\n",
613 fsc
->fs
.h_u
.usr_ip4_spec
.ip4src
);
614 p
+= ETH_GSTRING_LEN
;
616 sprintf(p
, "\tSrc IP mask: 0x%x\n",
617 fsc
->fs
.m_u
.usr_ip4_spec
.ip4src
);
618 p
+= ETH_GSTRING_LEN
;
620 sprintf(p
, "\tDest IP addr: 0x%x\n",
621 fsc
->fs
.h_u
.usr_ip4_spec
.ip4dst
);
622 p
+= ETH_GSTRING_LEN
;
624 sprintf(p
, "\tDest IP mask: 0x%x\n",
625 fsc
->fs
.m_u
.usr_ip4_spec
.ip4dst
);
626 p
+= ETH_GSTRING_LEN
;
628 sprintf(p
, "\tL4 bytes: 0x%x, mask: 0x%x\n",
629 fsc
->fs
.h_u
.usr_ip4_spec
.l4_4_bytes
,
630 fsc
->fs
.m_u
.usr_ip4_spec
.l4_4_bytes
);
631 p
+= ETH_GSTRING_LEN
;
633 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
634 fsc
->fs
.h_u
.usr_ip4_spec
.tos
,
635 fsc
->fs
.m_u
.usr_ip4_spec
.tos
);
636 p
+= ETH_GSTRING_LEN
;
638 sprintf(p
, "\tIP Version: %d, mask: 0x%x\n",
639 fsc
->fs
.h_u
.usr_ip4_spec
.ip_ver
,
640 fsc
->fs
.m_u
.usr_ip4_spec
.ip_ver
);
641 p
+= ETH_GSTRING_LEN
;
643 sprintf(p
, "\tProtocol: %d, mask: 0x%x\n",
644 fsc
->fs
.h_u
.usr_ip4_spec
.proto
,
645 fsc
->fs
.m_u
.usr_ip4_spec
.proto
);
646 p
+= ETH_GSTRING_LEN
;
650 sprintf(p
, "\tVLAN: %d, mask: 0x%x\n",
651 fsc
->fs
.vlan_tag
, fsc
->fs
.vlan_tag_mask
);
652 p
+= ETH_GSTRING_LEN
;
654 sprintf(p
, "\tUser-defined: 0x%Lx\n", fsc
->fs
.data
);
655 p
+= ETH_GSTRING_LEN
;
657 sprintf(p
, "\tUser-defined mask: 0x%Lx\n", fsc
->fs
.data_mask
);
658 p
+= ETH_GSTRING_LEN
;
660 if (fsc
->fs
.action
== ETHTOOL_RXNTUPLE_ACTION_DROP
)
661 sprintf(p
, "\tAction: Drop\n");
663 sprintf(p
, "\tAction: Direct to queue %d\n",
665 p
+= ETH_GSTRING_LEN
;
671 /* indicate to userspace how many strings we actually have */
672 gstrings
.len
= num_strings
;
674 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
676 useraddr
+= sizeof(gstrings
);
677 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
686 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
688 struct ethtool_regs regs
;
689 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
693 if (!ops
->get_regs
|| !ops
->get_regs_len
)
696 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
699 reglen
= ops
->get_regs_len(dev
);
700 if (regs
.len
> reglen
)
703 regbuf
= kmalloc(reglen
, GFP_USER
);
707 ops
->get_regs(dev
, ®s
, regbuf
);
710 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
712 useraddr
+= offsetof(struct ethtool_regs
, data
);
713 if (copy_to_user(useraddr
, regbuf
, regs
.len
))
722 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
724 struct ethtool_value reset
;
727 if (!dev
->ethtool_ops
->reset
)
730 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
733 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
737 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
742 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
744 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
746 if (!dev
->ethtool_ops
->get_wol
)
749 dev
->ethtool_ops
->get_wol(dev
, &wol
);
751 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
756 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
758 struct ethtool_wolinfo wol
;
760 if (!dev
->ethtool_ops
->set_wol
)
763 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
766 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
769 static int ethtool_nway_reset(struct net_device
*dev
)
771 if (!dev
->ethtool_ops
->nway_reset
)
774 return dev
->ethtool_ops
->nway_reset(dev
);
777 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
779 struct ethtool_eeprom eeprom
;
780 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
781 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
786 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
789 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
792 /* Check for wrap and zero */
793 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
796 /* Check for exceeding total eeprom len */
797 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
800 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
804 bytes_remaining
= eeprom
.len
;
805 while (bytes_remaining
> 0) {
806 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
808 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
811 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
815 userbuf
+= eeprom
.len
;
816 eeprom
.offset
+= eeprom
.len
;
817 bytes_remaining
-= eeprom
.len
;
820 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
821 eeprom
.offset
-= eeprom
.len
;
822 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
829 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
831 struct ethtool_eeprom eeprom
;
832 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
833 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
838 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
841 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
844 /* Check for wrap and zero */
845 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
848 /* Check for exceeding total eeprom len */
849 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
852 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
856 bytes_remaining
= eeprom
.len
;
857 while (bytes_remaining
> 0) {
858 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
860 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
864 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
867 userbuf
+= eeprom
.len
;
868 eeprom
.offset
+= eeprom
.len
;
869 bytes_remaining
-= eeprom
.len
;
876 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
877 void __user
*useraddr
)
879 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
881 if (!dev
->ethtool_ops
->get_coalesce
)
884 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
886 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
891 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
892 void __user
*useraddr
)
894 struct ethtool_coalesce coalesce
;
896 if (!dev
->ethtool_ops
->set_coalesce
)
899 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
902 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
905 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
907 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
909 if (!dev
->ethtool_ops
->get_ringparam
)
912 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
914 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
919 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
921 struct ethtool_ringparam ringparam
;
923 if (!dev
->ethtool_ops
->set_ringparam
)
926 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
929 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
932 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
934 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
936 if (!dev
->ethtool_ops
->get_pauseparam
)
939 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
941 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
946 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
948 struct ethtool_pauseparam pauseparam
;
950 if (!dev
->ethtool_ops
->set_pauseparam
)
953 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
956 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
959 static int __ethtool_set_sg(struct net_device
*dev
, u32 data
)
963 if (!data
&& dev
->ethtool_ops
->set_tso
) {
964 err
= dev
->ethtool_ops
->set_tso(dev
, 0);
969 if (!data
&& dev
->ethtool_ops
->set_ufo
) {
970 err
= dev
->ethtool_ops
->set_ufo(dev
, 0);
974 return dev
->ethtool_ops
->set_sg(dev
, data
);
977 static int ethtool_set_tx_csum(struct net_device
*dev
, char __user
*useraddr
)
979 struct ethtool_value edata
;
982 if (!dev
->ethtool_ops
->set_tx_csum
)
985 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
988 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
) {
989 err
= __ethtool_set_sg(dev
, 0);
994 return dev
->ethtool_ops
->set_tx_csum(dev
, edata
.data
);
996 EXPORT_SYMBOL(ethtool_op_set_tx_csum
);
998 static int ethtool_set_rx_csum(struct net_device
*dev
, char __user
*useraddr
)
1000 struct ethtool_value edata
;
1002 if (!dev
->ethtool_ops
->set_rx_csum
)
1005 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1008 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
)
1009 dev
->features
&= ~NETIF_F_GRO
;
1011 return dev
->ethtool_ops
->set_rx_csum(dev
, edata
.data
);
1014 static int ethtool_set_sg(struct net_device
*dev
, char __user
*useraddr
)
1016 struct ethtool_value edata
;
1018 if (!dev
->ethtool_ops
->set_sg
)
1021 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1025 !(dev
->features
& NETIF_F_ALL_CSUM
))
1028 return __ethtool_set_sg(dev
, edata
.data
);
1031 static int ethtool_set_tso(struct net_device
*dev
, char __user
*useraddr
)
1033 struct ethtool_value edata
;
1035 if (!dev
->ethtool_ops
->set_tso
)
1038 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1041 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
1044 return dev
->ethtool_ops
->set_tso(dev
, edata
.data
);
1047 static int ethtool_set_ufo(struct net_device
*dev
, char __user
*useraddr
)
1049 struct ethtool_value edata
;
1051 if (!dev
->ethtool_ops
->set_ufo
)
1053 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1055 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
1057 if (edata
.data
&& !(dev
->features
& NETIF_F_HW_CSUM
))
1059 return dev
->ethtool_ops
->set_ufo(dev
, edata
.data
);
1062 static int ethtool_get_gso(struct net_device
*dev
, char __user
*useraddr
)
1064 struct ethtool_value edata
= { ETHTOOL_GGSO
};
1066 edata
.data
= dev
->features
& NETIF_F_GSO
;
1067 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1072 static int ethtool_set_gso(struct net_device
*dev
, char __user
*useraddr
)
1074 struct ethtool_value edata
;
1076 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1079 dev
->features
|= NETIF_F_GSO
;
1081 dev
->features
&= ~NETIF_F_GSO
;
1085 static int ethtool_get_gro(struct net_device
*dev
, char __user
*useraddr
)
1087 struct ethtool_value edata
= { ETHTOOL_GGRO
};
1089 edata
.data
= dev
->features
& NETIF_F_GRO
;
1090 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1095 static int ethtool_set_gro(struct net_device
*dev
, char __user
*useraddr
)
1097 struct ethtool_value edata
;
1099 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1103 if (!dev
->ethtool_ops
->get_rx_csum
||
1104 !dev
->ethtool_ops
->get_rx_csum(dev
))
1106 dev
->features
|= NETIF_F_GRO
;
1108 dev
->features
&= ~NETIF_F_GRO
;
1113 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1115 struct ethtool_test test
;
1116 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1120 if (!ops
->self_test
|| !ops
->get_sset_count
)
1123 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1126 WARN_ON(test_len
== 0);
1128 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1131 test
.len
= test_len
;
1132 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1136 ops
->self_test(dev
, &test
, data
);
1139 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1141 useraddr
+= sizeof(test
);
1142 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1151 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1153 struct ethtool_gstrings gstrings
;
1154 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1158 if (!ops
->get_strings
|| !ops
->get_sset_count
)
1161 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1164 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
1170 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1174 ops
->get_strings(dev
, gstrings
.string_set
, data
);
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_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1191 struct ethtool_value id
;
1193 if (!dev
->ethtool_ops
->phys_id
)
1196 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1199 return dev
->ethtool_ops
->phys_id(dev
, id
.data
);
1202 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1204 struct ethtool_stats stats
;
1205 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1209 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1212 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1215 WARN_ON(n_stats
== 0);
1217 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1220 stats
.n_stats
= n_stats
;
1221 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1225 ops
->get_ethtool_stats(dev
, &stats
, data
);
1228 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1230 useraddr
+= sizeof(stats
);
1231 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1240 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1242 struct ethtool_perm_addr epaddr
;
1244 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1247 if (epaddr
.size
< dev
->addr_len
)
1249 epaddr
.size
= dev
->addr_len
;
1251 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1253 useraddr
+= sizeof(epaddr
);
1254 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1259 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1260 u32 cmd
, u32 (*actor
)(struct net_device
*))
1262 struct ethtool_value edata
= { .cmd
= cmd
};
1267 edata
.data
= actor(dev
);
1269 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1274 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1275 void (*actor
)(struct net_device
*, u32
))
1277 struct ethtool_value edata
;
1282 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1285 actor(dev
, edata
.data
);
1289 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1290 int (*actor
)(struct net_device
*, u32
))
1292 struct ethtool_value edata
;
1297 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1300 return actor(dev
, edata
.data
);
1303 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1304 char __user
*useraddr
)
1306 struct ethtool_flash efl
;
1308 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1311 if (!dev
->ethtool_ops
->flash_device
)
1314 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1317 /* The main entry point in this file. Called from net/core/dev.c */
1319 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1321 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1322 void __user
*useraddr
= ifr
->ifr_data
;
1325 unsigned long old_features
;
1327 if (!dev
|| !netif_device_present(dev
))
1330 if (!dev
->ethtool_ops
)
1333 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1336 /* Allow some commands to be done by anyone */
1338 case ETHTOOL_GDRVINFO
:
1339 case ETHTOOL_GMSGLVL
:
1340 case ETHTOOL_GCOALESCE
:
1341 case ETHTOOL_GRINGPARAM
:
1342 case ETHTOOL_GPAUSEPARAM
:
1343 case ETHTOOL_GRXCSUM
:
1344 case ETHTOOL_GTXCSUM
:
1346 case ETHTOOL_GSTRINGS
:
1348 case ETHTOOL_GPERMADDR
:
1352 case ETHTOOL_GFLAGS
:
1353 case ETHTOOL_GPFLAGS
:
1355 case ETHTOOL_GRXRINGS
:
1356 case ETHTOOL_GRXCLSRLCNT
:
1357 case ETHTOOL_GRXCLSRULE
:
1358 case ETHTOOL_GRXCLSRLALL
:
1361 if (!capable(CAP_NET_ADMIN
))
1365 if (dev
->ethtool_ops
->begin
) {
1366 rc
= dev
->ethtool_ops
->begin(dev
);
1370 old_features
= dev
->features
;
1374 rc
= ethtool_get_settings(dev
, useraddr
);
1377 rc
= ethtool_set_settings(dev
, useraddr
);
1379 case ETHTOOL_GDRVINFO
:
1380 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1383 rc
= ethtool_get_regs(dev
, useraddr
);
1386 rc
= ethtool_get_wol(dev
, useraddr
);
1389 rc
= ethtool_set_wol(dev
, useraddr
);
1391 case ETHTOOL_GMSGLVL
:
1392 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1393 dev
->ethtool_ops
->get_msglevel
);
1395 case ETHTOOL_SMSGLVL
:
1396 rc
= ethtool_set_value_void(dev
, useraddr
,
1397 dev
->ethtool_ops
->set_msglevel
);
1399 case ETHTOOL_NWAY_RST
:
1400 rc
= ethtool_nway_reset(dev
);
1403 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1404 dev
->ethtool_ops
->get_link
);
1406 case ETHTOOL_GEEPROM
:
1407 rc
= ethtool_get_eeprom(dev
, useraddr
);
1409 case ETHTOOL_SEEPROM
:
1410 rc
= ethtool_set_eeprom(dev
, useraddr
);
1412 case ETHTOOL_GCOALESCE
:
1413 rc
= ethtool_get_coalesce(dev
, useraddr
);
1415 case ETHTOOL_SCOALESCE
:
1416 rc
= ethtool_set_coalesce(dev
, useraddr
);
1418 case ETHTOOL_GRINGPARAM
:
1419 rc
= ethtool_get_ringparam(dev
, useraddr
);
1421 case ETHTOOL_SRINGPARAM
:
1422 rc
= ethtool_set_ringparam(dev
, useraddr
);
1424 case ETHTOOL_GPAUSEPARAM
:
1425 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1427 case ETHTOOL_SPAUSEPARAM
:
1428 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1430 case ETHTOOL_GRXCSUM
:
1431 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1432 (dev
->ethtool_ops
->get_rx_csum
?
1433 dev
->ethtool_ops
->get_rx_csum
:
1434 ethtool_op_get_rx_csum
));
1436 case ETHTOOL_SRXCSUM
:
1437 rc
= ethtool_set_rx_csum(dev
, useraddr
);
1439 case ETHTOOL_GTXCSUM
:
1440 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1441 (dev
->ethtool_ops
->get_tx_csum
?
1442 dev
->ethtool_ops
->get_tx_csum
:
1443 ethtool_op_get_tx_csum
));
1445 case ETHTOOL_STXCSUM
:
1446 rc
= ethtool_set_tx_csum(dev
, useraddr
);
1449 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1450 (dev
->ethtool_ops
->get_sg
?
1451 dev
->ethtool_ops
->get_sg
:
1452 ethtool_op_get_sg
));
1455 rc
= ethtool_set_sg(dev
, useraddr
);
1458 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1459 (dev
->ethtool_ops
->get_tso
?
1460 dev
->ethtool_ops
->get_tso
:
1461 ethtool_op_get_tso
));
1464 rc
= ethtool_set_tso(dev
, useraddr
);
1467 rc
= ethtool_self_test(dev
, useraddr
);
1469 case ETHTOOL_GSTRINGS
:
1470 rc
= ethtool_get_strings(dev
, useraddr
);
1472 case ETHTOOL_PHYS_ID
:
1473 rc
= ethtool_phys_id(dev
, useraddr
);
1475 case ETHTOOL_GSTATS
:
1476 rc
= ethtool_get_stats(dev
, useraddr
);
1478 case ETHTOOL_GPERMADDR
:
1479 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1482 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1483 (dev
->ethtool_ops
->get_ufo
?
1484 dev
->ethtool_ops
->get_ufo
:
1485 ethtool_op_get_ufo
));
1488 rc
= ethtool_set_ufo(dev
, useraddr
);
1491 rc
= ethtool_get_gso(dev
, useraddr
);
1494 rc
= ethtool_set_gso(dev
, useraddr
);
1496 case ETHTOOL_GFLAGS
:
1497 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1498 (dev
->ethtool_ops
->get_flags
?
1499 dev
->ethtool_ops
->get_flags
:
1500 ethtool_op_get_flags
));
1502 case ETHTOOL_SFLAGS
:
1503 rc
= ethtool_set_value(dev
, useraddr
,
1504 dev
->ethtool_ops
->set_flags
);
1506 case ETHTOOL_GPFLAGS
:
1507 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1508 dev
->ethtool_ops
->get_priv_flags
);
1510 case ETHTOOL_SPFLAGS
:
1511 rc
= ethtool_set_value(dev
, useraddr
,
1512 dev
->ethtool_ops
->set_priv_flags
);
1515 case ETHTOOL_GRXRINGS
:
1516 case ETHTOOL_GRXCLSRLCNT
:
1517 case ETHTOOL_GRXCLSRULE
:
1518 case ETHTOOL_GRXCLSRLALL
:
1519 rc
= ethtool_get_rxnfc(dev
, useraddr
);
1522 case ETHTOOL_SRXCLSRLDEL
:
1523 case ETHTOOL_SRXCLSRLINS
:
1524 rc
= ethtool_set_rxnfc(dev
, useraddr
);
1527 rc
= ethtool_get_gro(dev
, useraddr
);
1530 rc
= ethtool_set_gro(dev
, useraddr
);
1532 case ETHTOOL_FLASHDEV
:
1533 rc
= ethtool_flash_device(dev
, useraddr
);
1536 rc
= ethtool_reset(dev
, useraddr
);
1538 case ETHTOOL_SRXNTUPLE
:
1539 rc
= ethtool_set_rx_ntuple(dev
, useraddr
);
1541 case ETHTOOL_GRXNTUPLE
:
1542 rc
= ethtool_get_rx_ntuple(dev
, useraddr
);
1544 case ETHTOOL_GSSET_INFO
:
1545 rc
= ethtool_get_sset_info(dev
, useraddr
);
1551 if (dev
->ethtool_ops
->complete
)
1552 dev
->ethtool_ops
->complete(dev
);
1554 if (old_features
!= dev
->features
)
1555 netdev_features_change(dev
);