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/slab.h>
22 #include <asm/uaccess.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;
35 u32
ethtool_op_get_rx_csum(struct net_device
*dev
)
37 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
39 EXPORT_SYMBOL(ethtool_op_get_rx_csum
);
41 u32
ethtool_op_get_tx_csum(struct net_device
*dev
)
43 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
45 EXPORT_SYMBOL(ethtool_op_get_tx_csum
);
47 int ethtool_op_set_tx_csum(struct net_device
*dev
, u32 data
)
50 dev
->features
|= NETIF_F_IP_CSUM
;
52 dev
->features
&= ~NETIF_F_IP_CSUM
;
57 int ethtool_op_set_tx_hw_csum(struct net_device
*dev
, u32 data
)
60 dev
->features
|= NETIF_F_HW_CSUM
;
62 dev
->features
&= ~NETIF_F_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
);
77 u32
ethtool_op_get_sg(struct net_device
*dev
)
79 return (dev
->features
& NETIF_F_SG
) != 0;
82 int ethtool_op_set_sg(struct net_device
*dev
, u32 data
)
85 dev
->features
|= NETIF_F_SG
;
87 dev
->features
&= ~NETIF_F_SG
;
92 u32
ethtool_op_get_tso(struct net_device
*dev
)
94 return (dev
->features
& NETIF_F_TSO
) != 0;
97 int ethtool_op_set_tso(struct net_device
*dev
, u32 data
)
100 dev
->features
|= NETIF_F_TSO
;
102 dev
->features
&= ~NETIF_F_TSO
;
107 u32
ethtool_op_get_ufo(struct net_device
*dev
)
109 return (dev
->features
& NETIF_F_UFO
) != 0;
112 int ethtool_op_set_ufo(struct net_device
*dev
, u32 data
)
115 dev
->features
|= NETIF_F_UFO
;
117 dev
->features
&= ~NETIF_F_UFO
;
121 /* the following list of flags are the same as their associated
122 * NETIF_F_xxx values in include/linux/netdevice.h
124 static const u32 flags_dup_features
=
125 (ETH_FLAG_LRO
| ETH_FLAG_NTUPLE
);
127 u32
ethtool_op_get_flags(struct net_device
*dev
)
129 /* in the future, this function will probably contain additional
130 * handling for flags which are not so easily handled
131 * by a simple masking operation
134 return dev
->features
& flags_dup_features
;
137 int ethtool_op_set_flags(struct net_device
*dev
, u32 data
)
139 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
140 unsigned long features
= dev
->features
;
142 if (data
& ETH_FLAG_LRO
)
143 features
|= NETIF_F_LRO
;
145 features
&= ~NETIF_F_LRO
;
147 if (data
& ETH_FLAG_NTUPLE
) {
148 if (!ops
->set_rx_ntuple
)
150 features
|= NETIF_F_NTUPLE
;
152 /* safe to clear regardless */
153 features
&= ~NETIF_F_NTUPLE
;
156 dev
->features
= features
;
160 void ethtool_ntuple_flush(struct net_device
*dev
)
162 struct ethtool_rx_ntuple_flow_spec_container
*fsc
, *f
;
164 list_for_each_entry_safe(fsc
, f
, &dev
->ethtool_ntuple_list
.list
, list
) {
165 list_del(&fsc
->list
);
168 dev
->ethtool_ntuple_list
.count
= 0;
170 EXPORT_SYMBOL(ethtool_ntuple_flush
);
172 /* Handlers for each ethtool command */
174 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
176 struct ethtool_cmd cmd
= { .cmd
= ETHTOOL_GSET
};
179 if (!dev
->ethtool_ops
->get_settings
)
182 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
186 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
191 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
193 struct ethtool_cmd cmd
;
195 if (!dev
->ethtool_ops
->set_settings
)
198 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
201 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
204 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
, void __user
*useraddr
)
206 struct ethtool_drvinfo info
;
207 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
209 if (!ops
->get_drvinfo
)
212 memset(&info
, 0, sizeof(info
));
213 info
.cmd
= ETHTOOL_GDRVINFO
;
214 ops
->get_drvinfo(dev
, &info
);
217 * this method of obtaining string set info is deprecated;
218 * Use ETHTOOL_GSSET_INFO instead.
220 if (ops
->get_sset_count
) {
223 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
225 info
.testinfo_len
= rc
;
226 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
229 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
231 info
.n_priv_flags
= rc
;
233 if (ops
->get_regs_len
)
234 info
.regdump_len
= ops
->get_regs_len(dev
);
235 if (ops
->get_eeprom_len
)
236 info
.eedump_len
= ops
->get_eeprom_len(dev
);
238 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
243 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
244 void __user
*useraddr
)
246 struct ethtool_sset_info info
;
247 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
249 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
250 u32
*info_buf
= NULL
;
252 if (!ops
->get_sset_count
)
255 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
258 /* store copy of mask, because we zero struct later on */
259 sset_mask
= info
.sset_mask
;
263 /* calculate size of return buffer */
264 n_bits
= hweight64(sset_mask
);
266 memset(&info
, 0, sizeof(info
));
267 info
.cmd
= ETHTOOL_GSSET_INFO
;
269 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
274 * fill return buffer based on input bitmask and successful
275 * get_sset_count return
277 for (i
= 0; i
< 64; i
++) {
278 if (!(sset_mask
& (1ULL << i
)))
281 rc
= ops
->get_sset_count(dev
, i
);
283 info
.sset_mask
|= (1ULL << i
);
284 info_buf
[idx
++] = rc
;
289 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
292 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
293 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
303 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
, void __user
*useraddr
)
305 struct ethtool_rxnfc cmd
;
307 if (!dev
->ethtool_ops
->set_rxnfc
)
310 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
313 return dev
->ethtool_ops
->set_rxnfc(dev
, &cmd
);
316 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
, void __user
*useraddr
)
318 struct ethtool_rxnfc info
;
319 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
321 void *rule_buf
= NULL
;
326 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
329 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
330 if (info
.rule_cnt
> 0) {
331 rule_buf
= kmalloc(info
.rule_cnt
* sizeof(u32
),
338 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
343 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
347 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
348 if (copy_to_user(useraddr
, rule_buf
,
349 info
.rule_cnt
* sizeof(u32
)))
360 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list
*list
,
361 struct ethtool_rx_ntuple_flow_spec
*spec
,
362 struct ethtool_rx_ntuple_flow_spec_container
*fsc
)
365 /* don't add filters forever */
366 if (list
->count
>= ETHTOOL_MAX_NTUPLE_LIST_ENTRY
) {
367 /* free the container */
372 /* Copy the whole filter over */
373 fsc
->fs
.flow_type
= spec
->flow_type
;
374 memcpy(&fsc
->fs
.h_u
, &spec
->h_u
, sizeof(spec
->h_u
));
375 memcpy(&fsc
->fs
.m_u
, &spec
->m_u
, sizeof(spec
->m_u
));
377 fsc
->fs
.vlan_tag
= spec
->vlan_tag
;
378 fsc
->fs
.vlan_tag_mask
= spec
->vlan_tag_mask
;
379 fsc
->fs
.data
= spec
->data
;
380 fsc
->fs
.data_mask
= spec
->data_mask
;
381 fsc
->fs
.action
= spec
->action
;
383 /* add to the list */
384 list_add_tail_rcu(&fsc
->list
, &list
->list
);
388 static noinline_for_stack
int ethtool_set_rx_ntuple(struct net_device
*dev
, void __user
*useraddr
)
390 struct ethtool_rx_ntuple cmd
;
391 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
392 struct ethtool_rx_ntuple_flow_spec_container
*fsc
= NULL
;
395 if (!(dev
->features
& NETIF_F_NTUPLE
))
398 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
402 * Cache filter in dev struct for GET operation only if
403 * the underlying driver doesn't have its own GET operation, and
404 * only if the filter was added successfully. First make sure we
405 * can allocate the filter, then continue if successful.
407 if (!ops
->get_rx_ntuple
) {
408 fsc
= kmalloc(sizeof(*fsc
), GFP_ATOMIC
);
413 ret
= ops
->set_rx_ntuple(dev
, &cmd
);
419 if (!ops
->get_rx_ntuple
)
420 __rx_ntuple_filter_add(&dev
->ethtool_ntuple_list
, &cmd
.fs
, fsc
);
425 static int ethtool_get_rx_ntuple(struct net_device
*dev
, void __user
*useraddr
)
427 struct ethtool_gstrings gstrings
;
428 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
429 struct ethtool_rx_ntuple_flow_spec_container
*fsc
;
432 int ret
, i
, num_strings
= 0;
434 if (!ops
->get_sset_count
)
437 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
440 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
446 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
450 if (ops
->get_rx_ntuple
) {
451 /* driver-specific filter grab */
452 ret
= ops
->get_rx_ntuple(dev
, gstrings
.string_set
, data
);
456 /* default ethtool filter grab */
459 list_for_each_entry(fsc
, &dev
->ethtool_ntuple_list
.list
, list
) {
460 sprintf(p
, "Filter %d:\n", i
);
461 p
+= ETH_GSTRING_LEN
;
464 switch (fsc
->fs
.flow_type
) {
466 sprintf(p
, "\tFlow Type: TCP\n");
467 p
+= ETH_GSTRING_LEN
;
471 sprintf(p
, "\tFlow Type: UDP\n");
472 p
+= ETH_GSTRING_LEN
;
476 sprintf(p
, "\tFlow Type: SCTP\n");
477 p
+= ETH_GSTRING_LEN
;
481 sprintf(p
, "\tFlow Type: AH ESP\n");
482 p
+= ETH_GSTRING_LEN
;
486 sprintf(p
, "\tFlow Type: ESP\n");
487 p
+= ETH_GSTRING_LEN
;
491 sprintf(p
, "\tFlow Type: Raw IP\n");
492 p
+= ETH_GSTRING_LEN
;
496 sprintf(p
, "\tFlow Type: IPv4\n");
497 p
+= ETH_GSTRING_LEN
;
501 sprintf(p
, "\tFlow Type: Unknown\n");
502 p
+= ETH_GSTRING_LEN
;
507 /* now the rest of the filters */
508 switch (fsc
->fs
.flow_type
) {
512 sprintf(p
, "\tSrc IP addr: 0x%x\n",
513 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4src
);
514 p
+= ETH_GSTRING_LEN
;
516 sprintf(p
, "\tSrc IP mask: 0x%x\n",
517 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4src
);
518 p
+= ETH_GSTRING_LEN
;
520 sprintf(p
, "\tDest IP addr: 0x%x\n",
521 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4dst
);
522 p
+= ETH_GSTRING_LEN
;
524 sprintf(p
, "\tDest IP mask: 0x%x\n",
525 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4dst
);
526 p
+= ETH_GSTRING_LEN
;
528 sprintf(p
, "\tSrc Port: %d, mask: 0x%x\n",
529 fsc
->fs
.h_u
.tcp_ip4_spec
.psrc
,
530 fsc
->fs
.m_u
.tcp_ip4_spec
.psrc
);
531 p
+= ETH_GSTRING_LEN
;
533 sprintf(p
, "\tDest Port: %d, mask: 0x%x\n",
534 fsc
->fs
.h_u
.tcp_ip4_spec
.pdst
,
535 fsc
->fs
.m_u
.tcp_ip4_spec
.pdst
);
536 p
+= ETH_GSTRING_LEN
;
538 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
539 fsc
->fs
.h_u
.tcp_ip4_spec
.tos
,
540 fsc
->fs
.m_u
.tcp_ip4_spec
.tos
);
541 p
+= ETH_GSTRING_LEN
;
546 sprintf(p
, "\tSrc IP addr: 0x%x\n",
547 fsc
->fs
.h_u
.ah_ip4_spec
.ip4src
);
548 p
+= ETH_GSTRING_LEN
;
550 sprintf(p
, "\tSrc IP mask: 0x%x\n",
551 fsc
->fs
.m_u
.ah_ip4_spec
.ip4src
);
552 p
+= ETH_GSTRING_LEN
;
554 sprintf(p
, "\tDest IP addr: 0x%x\n",
555 fsc
->fs
.h_u
.ah_ip4_spec
.ip4dst
);
556 p
+= ETH_GSTRING_LEN
;
558 sprintf(p
, "\tDest IP mask: 0x%x\n",
559 fsc
->fs
.m_u
.ah_ip4_spec
.ip4dst
);
560 p
+= ETH_GSTRING_LEN
;
562 sprintf(p
, "\tSPI: %d, mask: 0x%x\n",
563 fsc
->fs
.h_u
.ah_ip4_spec
.spi
,
564 fsc
->fs
.m_u
.ah_ip4_spec
.spi
);
565 p
+= ETH_GSTRING_LEN
;
567 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
568 fsc
->fs
.h_u
.ah_ip4_spec
.tos
,
569 fsc
->fs
.m_u
.ah_ip4_spec
.tos
);
570 p
+= ETH_GSTRING_LEN
;
574 sprintf(p
, "\tSrc IP addr: 0x%x\n",
575 fsc
->fs
.h_u
.raw_ip4_spec
.ip4src
);
576 p
+= ETH_GSTRING_LEN
;
578 sprintf(p
, "\tSrc IP mask: 0x%x\n",
579 fsc
->fs
.m_u
.raw_ip4_spec
.ip4src
);
580 p
+= ETH_GSTRING_LEN
;
582 sprintf(p
, "\tDest IP addr: 0x%x\n",
583 fsc
->fs
.h_u
.raw_ip4_spec
.ip4dst
);
584 p
+= ETH_GSTRING_LEN
;
586 sprintf(p
, "\tDest IP mask: 0x%x\n",
587 fsc
->fs
.m_u
.raw_ip4_spec
.ip4dst
);
588 p
+= ETH_GSTRING_LEN
;
592 sprintf(p
, "\tSrc IP addr: 0x%x\n",
593 fsc
->fs
.h_u
.usr_ip4_spec
.ip4src
);
594 p
+= ETH_GSTRING_LEN
;
596 sprintf(p
, "\tSrc IP mask: 0x%x\n",
597 fsc
->fs
.m_u
.usr_ip4_spec
.ip4src
);
598 p
+= ETH_GSTRING_LEN
;
600 sprintf(p
, "\tDest IP addr: 0x%x\n",
601 fsc
->fs
.h_u
.usr_ip4_spec
.ip4dst
);
602 p
+= ETH_GSTRING_LEN
;
604 sprintf(p
, "\tDest IP mask: 0x%x\n",
605 fsc
->fs
.m_u
.usr_ip4_spec
.ip4dst
);
606 p
+= ETH_GSTRING_LEN
;
608 sprintf(p
, "\tL4 bytes: 0x%x, mask: 0x%x\n",
609 fsc
->fs
.h_u
.usr_ip4_spec
.l4_4_bytes
,
610 fsc
->fs
.m_u
.usr_ip4_spec
.l4_4_bytes
);
611 p
+= ETH_GSTRING_LEN
;
613 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
614 fsc
->fs
.h_u
.usr_ip4_spec
.tos
,
615 fsc
->fs
.m_u
.usr_ip4_spec
.tos
);
616 p
+= ETH_GSTRING_LEN
;
618 sprintf(p
, "\tIP Version: %d, mask: 0x%x\n",
619 fsc
->fs
.h_u
.usr_ip4_spec
.ip_ver
,
620 fsc
->fs
.m_u
.usr_ip4_spec
.ip_ver
);
621 p
+= ETH_GSTRING_LEN
;
623 sprintf(p
, "\tProtocol: %d, mask: 0x%x\n",
624 fsc
->fs
.h_u
.usr_ip4_spec
.proto
,
625 fsc
->fs
.m_u
.usr_ip4_spec
.proto
);
626 p
+= ETH_GSTRING_LEN
;
630 sprintf(p
, "\tVLAN: %d, mask: 0x%x\n",
631 fsc
->fs
.vlan_tag
, fsc
->fs
.vlan_tag_mask
);
632 p
+= ETH_GSTRING_LEN
;
634 sprintf(p
, "\tUser-defined: 0x%Lx\n", fsc
->fs
.data
);
635 p
+= ETH_GSTRING_LEN
;
637 sprintf(p
, "\tUser-defined mask: 0x%Lx\n", fsc
->fs
.data_mask
);
638 p
+= ETH_GSTRING_LEN
;
640 if (fsc
->fs
.action
== ETHTOOL_RXNTUPLE_ACTION_DROP
)
641 sprintf(p
, "\tAction: Drop\n");
643 sprintf(p
, "\tAction: Direct to queue %d\n",
645 p
+= ETH_GSTRING_LEN
;
651 /* indicate to userspace how many strings we actually have */
652 gstrings
.len
= num_strings
;
654 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
656 useraddr
+= sizeof(gstrings
);
657 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
666 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
668 struct ethtool_regs regs
;
669 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
673 if (!ops
->get_regs
|| !ops
->get_regs_len
)
676 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
679 reglen
= ops
->get_regs_len(dev
);
680 if (regs
.len
> reglen
)
683 regbuf
= kmalloc(reglen
, GFP_USER
);
687 ops
->get_regs(dev
, ®s
, regbuf
);
690 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
692 useraddr
+= offsetof(struct ethtool_regs
, data
);
693 if (copy_to_user(useraddr
, regbuf
, regs
.len
))
702 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
704 struct ethtool_value reset
;
707 if (!dev
->ethtool_ops
->reset
)
710 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
713 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
717 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
722 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
724 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
726 if (!dev
->ethtool_ops
->get_wol
)
729 dev
->ethtool_ops
->get_wol(dev
, &wol
);
731 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
736 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
738 struct ethtool_wolinfo wol
;
740 if (!dev
->ethtool_ops
->set_wol
)
743 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
746 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
749 static int ethtool_nway_reset(struct net_device
*dev
)
751 if (!dev
->ethtool_ops
->nway_reset
)
754 return dev
->ethtool_ops
->nway_reset(dev
);
757 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
759 struct ethtool_eeprom eeprom
;
760 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
761 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
766 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
769 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
772 /* Check for wrap and zero */
773 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
776 /* Check for exceeding total eeprom len */
777 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
780 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
784 bytes_remaining
= eeprom
.len
;
785 while (bytes_remaining
> 0) {
786 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
788 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
791 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
795 userbuf
+= eeprom
.len
;
796 eeprom
.offset
+= eeprom
.len
;
797 bytes_remaining
-= eeprom
.len
;
800 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
801 eeprom
.offset
-= eeprom
.len
;
802 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
809 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
811 struct ethtool_eeprom eeprom
;
812 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
813 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
818 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
821 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
824 /* Check for wrap and zero */
825 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
828 /* Check for exceeding total eeprom len */
829 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
832 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
836 bytes_remaining
= eeprom
.len
;
837 while (bytes_remaining
> 0) {
838 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
840 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
844 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
847 userbuf
+= eeprom
.len
;
848 eeprom
.offset
+= eeprom
.len
;
849 bytes_remaining
-= eeprom
.len
;
856 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
, void __user
*useraddr
)
858 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
860 if (!dev
->ethtool_ops
->get_coalesce
)
863 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
865 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
870 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
, void __user
*useraddr
)
872 struct ethtool_coalesce coalesce
;
874 if (!dev
->ethtool_ops
->set_coalesce
)
877 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
880 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
883 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
885 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
887 if (!dev
->ethtool_ops
->get_ringparam
)
890 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
892 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
897 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
899 struct ethtool_ringparam ringparam
;
901 if (!dev
->ethtool_ops
->set_ringparam
)
904 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
907 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
910 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
912 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
914 if (!dev
->ethtool_ops
->get_pauseparam
)
917 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
919 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
924 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
926 struct ethtool_pauseparam pauseparam
;
928 if (!dev
->ethtool_ops
->set_pauseparam
)
931 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
934 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
937 static int __ethtool_set_sg(struct net_device
*dev
, u32 data
)
941 if (!data
&& dev
->ethtool_ops
->set_tso
) {
942 err
= dev
->ethtool_ops
->set_tso(dev
, 0);
947 if (!data
&& dev
->ethtool_ops
->set_ufo
) {
948 err
= dev
->ethtool_ops
->set_ufo(dev
, 0);
952 return dev
->ethtool_ops
->set_sg(dev
, data
);
955 static int ethtool_set_tx_csum(struct net_device
*dev
, char __user
*useraddr
)
957 struct ethtool_value edata
;
960 if (!dev
->ethtool_ops
->set_tx_csum
)
963 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
966 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
) {
967 err
= __ethtool_set_sg(dev
, 0);
972 return dev
->ethtool_ops
->set_tx_csum(dev
, edata
.data
);
975 static int ethtool_set_rx_csum(struct net_device
*dev
, char __user
*useraddr
)
977 struct ethtool_value edata
;
979 if (!dev
->ethtool_ops
->set_rx_csum
)
982 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
985 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
)
986 dev
->features
&= ~NETIF_F_GRO
;
988 return dev
->ethtool_ops
->set_rx_csum(dev
, edata
.data
);
991 static int ethtool_set_sg(struct net_device
*dev
, char __user
*useraddr
)
993 struct ethtool_value edata
;
995 if (!dev
->ethtool_ops
->set_sg
)
998 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1002 !(dev
->features
& NETIF_F_ALL_CSUM
))
1005 return __ethtool_set_sg(dev
, edata
.data
);
1008 static int ethtool_set_tso(struct net_device
*dev
, char __user
*useraddr
)
1010 struct ethtool_value edata
;
1012 if (!dev
->ethtool_ops
->set_tso
)
1015 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1018 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
1021 return dev
->ethtool_ops
->set_tso(dev
, edata
.data
);
1024 static int ethtool_set_ufo(struct net_device
*dev
, char __user
*useraddr
)
1026 struct ethtool_value edata
;
1028 if (!dev
->ethtool_ops
->set_ufo
)
1030 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1032 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
1034 if (edata
.data
&& !(dev
->features
& NETIF_F_HW_CSUM
))
1036 return dev
->ethtool_ops
->set_ufo(dev
, edata
.data
);
1039 static int ethtool_get_gso(struct net_device
*dev
, char __user
*useraddr
)
1041 struct ethtool_value edata
= { ETHTOOL_GGSO
};
1043 edata
.data
= dev
->features
& NETIF_F_GSO
;
1044 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1049 static int ethtool_set_gso(struct net_device
*dev
, char __user
*useraddr
)
1051 struct ethtool_value edata
;
1053 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1056 dev
->features
|= NETIF_F_GSO
;
1058 dev
->features
&= ~NETIF_F_GSO
;
1062 static int ethtool_get_gro(struct net_device
*dev
, char __user
*useraddr
)
1064 struct ethtool_value edata
= { ETHTOOL_GGRO
};
1066 edata
.data
= dev
->features
& NETIF_F_GRO
;
1067 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1072 static int ethtool_set_gro(struct net_device
*dev
, char __user
*useraddr
)
1074 struct ethtool_value edata
;
1076 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1080 if (!dev
->ethtool_ops
->get_rx_csum
||
1081 !dev
->ethtool_ops
->get_rx_csum(dev
))
1083 dev
->features
|= NETIF_F_GRO
;
1085 dev
->features
&= ~NETIF_F_GRO
;
1090 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1092 struct ethtool_test test
;
1093 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1097 if (!ops
->self_test
|| !ops
->get_sset_count
)
1100 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1103 WARN_ON(test_len
== 0);
1105 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1108 test
.len
= test_len
;
1109 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1113 ops
->self_test(dev
, &test
, data
);
1116 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1118 useraddr
+= sizeof(test
);
1119 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1128 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1130 struct ethtool_gstrings gstrings
;
1131 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1135 if (!ops
->get_strings
|| !ops
->get_sset_count
)
1138 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1141 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
1147 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1151 ops
->get_strings(dev
, gstrings
.string_set
, data
);
1154 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1156 useraddr
+= sizeof(gstrings
);
1157 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1166 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1168 struct ethtool_value id
;
1170 if (!dev
->ethtool_ops
->phys_id
)
1173 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1176 return dev
->ethtool_ops
->phys_id(dev
, id
.data
);
1179 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1181 struct ethtool_stats stats
;
1182 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1186 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1189 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1192 WARN_ON(n_stats
== 0);
1194 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1197 stats
.n_stats
= n_stats
;
1198 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1202 ops
->get_ethtool_stats(dev
, &stats
, data
);
1205 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1207 useraddr
+= sizeof(stats
);
1208 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1217 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1219 struct ethtool_perm_addr epaddr
;
1221 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1224 if (epaddr
.size
< dev
->addr_len
)
1226 epaddr
.size
= dev
->addr_len
;
1228 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1230 useraddr
+= sizeof(epaddr
);
1231 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1236 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1237 u32 cmd
, u32 (*actor
)(struct net_device
*))
1239 struct ethtool_value edata
= { .cmd
= cmd
};
1244 edata
.data
= actor(dev
);
1246 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1251 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1252 void (*actor
)(struct net_device
*, u32
))
1254 struct ethtool_value edata
;
1259 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1262 actor(dev
, edata
.data
);
1266 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1267 int (*actor
)(struct net_device
*, u32
))
1269 struct ethtool_value edata
;
1274 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1277 return actor(dev
, edata
.data
);
1280 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
, char __user
*useraddr
)
1282 struct ethtool_flash efl
;
1284 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1287 if (!dev
->ethtool_ops
->flash_device
)
1290 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1293 /* The main entry point in this file. Called from net/core/dev.c */
1295 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1297 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1298 void __user
*useraddr
= ifr
->ifr_data
;
1301 unsigned long old_features
;
1303 if (!dev
|| !netif_device_present(dev
))
1306 if (!dev
->ethtool_ops
)
1309 if (copy_from_user(ðcmd
, useraddr
, sizeof (ethcmd
)))
1312 /* Allow some commands to be done by anyone */
1314 case ETHTOOL_GDRVINFO
:
1315 case ETHTOOL_GMSGLVL
:
1316 case ETHTOOL_GCOALESCE
:
1317 case ETHTOOL_GRINGPARAM
:
1318 case ETHTOOL_GPAUSEPARAM
:
1319 case ETHTOOL_GRXCSUM
:
1320 case ETHTOOL_GTXCSUM
:
1322 case ETHTOOL_GSTRINGS
:
1324 case ETHTOOL_GPERMADDR
:
1328 case ETHTOOL_GFLAGS
:
1329 case ETHTOOL_GPFLAGS
:
1331 case ETHTOOL_GRXRINGS
:
1332 case ETHTOOL_GRXCLSRLCNT
:
1333 case ETHTOOL_GRXCLSRULE
:
1334 case ETHTOOL_GRXCLSRLALL
:
1337 if (!capable(CAP_NET_ADMIN
))
1341 if (dev
->ethtool_ops
->begin
)
1342 if ((rc
= dev
->ethtool_ops
->begin(dev
)) < 0)
1345 old_features
= dev
->features
;
1349 rc
= ethtool_get_settings(dev
, useraddr
);
1352 rc
= ethtool_set_settings(dev
, useraddr
);
1354 case ETHTOOL_GDRVINFO
:
1355 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1358 rc
= ethtool_get_regs(dev
, useraddr
);
1361 rc
= ethtool_get_wol(dev
, useraddr
);
1364 rc
= ethtool_set_wol(dev
, useraddr
);
1366 case ETHTOOL_GMSGLVL
:
1367 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1368 dev
->ethtool_ops
->get_msglevel
);
1370 case ETHTOOL_SMSGLVL
:
1371 rc
= ethtool_set_value_void(dev
, useraddr
,
1372 dev
->ethtool_ops
->set_msglevel
);
1374 case ETHTOOL_NWAY_RST
:
1375 rc
= ethtool_nway_reset(dev
);
1378 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1379 dev
->ethtool_ops
->get_link
);
1381 case ETHTOOL_GEEPROM
:
1382 rc
= ethtool_get_eeprom(dev
, useraddr
);
1384 case ETHTOOL_SEEPROM
:
1385 rc
= ethtool_set_eeprom(dev
, useraddr
);
1387 case ETHTOOL_GCOALESCE
:
1388 rc
= ethtool_get_coalesce(dev
, useraddr
);
1390 case ETHTOOL_SCOALESCE
:
1391 rc
= ethtool_set_coalesce(dev
, useraddr
);
1393 case ETHTOOL_GRINGPARAM
:
1394 rc
= ethtool_get_ringparam(dev
, useraddr
);
1396 case ETHTOOL_SRINGPARAM
:
1397 rc
= ethtool_set_ringparam(dev
, useraddr
);
1399 case ETHTOOL_GPAUSEPARAM
:
1400 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1402 case ETHTOOL_SPAUSEPARAM
:
1403 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1405 case ETHTOOL_GRXCSUM
:
1406 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1407 (dev
->ethtool_ops
->get_rx_csum
?
1408 dev
->ethtool_ops
->get_rx_csum
:
1409 ethtool_op_get_rx_csum
));
1411 case ETHTOOL_SRXCSUM
:
1412 rc
= ethtool_set_rx_csum(dev
, useraddr
);
1414 case ETHTOOL_GTXCSUM
:
1415 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1416 (dev
->ethtool_ops
->get_tx_csum
?
1417 dev
->ethtool_ops
->get_tx_csum
:
1418 ethtool_op_get_tx_csum
));
1420 case ETHTOOL_STXCSUM
:
1421 rc
= ethtool_set_tx_csum(dev
, useraddr
);
1424 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1425 (dev
->ethtool_ops
->get_sg
?
1426 dev
->ethtool_ops
->get_sg
:
1427 ethtool_op_get_sg
));
1430 rc
= ethtool_set_sg(dev
, useraddr
);
1433 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1434 (dev
->ethtool_ops
->get_tso
?
1435 dev
->ethtool_ops
->get_tso
:
1436 ethtool_op_get_tso
));
1439 rc
= ethtool_set_tso(dev
, useraddr
);
1442 rc
= ethtool_self_test(dev
, useraddr
);
1444 case ETHTOOL_GSTRINGS
:
1445 rc
= ethtool_get_strings(dev
, useraddr
);
1447 case ETHTOOL_PHYS_ID
:
1448 rc
= ethtool_phys_id(dev
, useraddr
);
1450 case ETHTOOL_GSTATS
:
1451 rc
= ethtool_get_stats(dev
, useraddr
);
1453 case ETHTOOL_GPERMADDR
:
1454 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1457 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1458 (dev
->ethtool_ops
->get_ufo
?
1459 dev
->ethtool_ops
->get_ufo
:
1460 ethtool_op_get_ufo
));
1463 rc
= ethtool_set_ufo(dev
, useraddr
);
1466 rc
= ethtool_get_gso(dev
, useraddr
);
1469 rc
= ethtool_set_gso(dev
, useraddr
);
1471 case ETHTOOL_GFLAGS
:
1472 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1473 (dev
->ethtool_ops
->get_flags
?
1474 dev
->ethtool_ops
->get_flags
:
1475 ethtool_op_get_flags
));
1477 case ETHTOOL_SFLAGS
:
1478 rc
= ethtool_set_value(dev
, useraddr
,
1479 dev
->ethtool_ops
->set_flags
);
1481 case ETHTOOL_GPFLAGS
:
1482 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1483 dev
->ethtool_ops
->get_priv_flags
);
1485 case ETHTOOL_SPFLAGS
:
1486 rc
= ethtool_set_value(dev
, useraddr
,
1487 dev
->ethtool_ops
->set_priv_flags
);
1490 case ETHTOOL_GRXRINGS
:
1491 case ETHTOOL_GRXCLSRLCNT
:
1492 case ETHTOOL_GRXCLSRULE
:
1493 case ETHTOOL_GRXCLSRLALL
:
1494 rc
= ethtool_get_rxnfc(dev
, useraddr
);
1497 case ETHTOOL_SRXCLSRLDEL
:
1498 case ETHTOOL_SRXCLSRLINS
:
1499 rc
= ethtool_set_rxnfc(dev
, useraddr
);
1502 rc
= ethtool_get_gro(dev
, useraddr
);
1505 rc
= ethtool_set_gro(dev
, useraddr
);
1507 case ETHTOOL_FLASHDEV
:
1508 rc
= ethtool_flash_device(dev
, useraddr
);
1511 rc
= ethtool_reset(dev
, useraddr
);
1513 case ETHTOOL_SRXNTUPLE
:
1514 rc
= ethtool_set_rx_ntuple(dev
, useraddr
);
1516 case ETHTOOL_GRXNTUPLE
:
1517 rc
= ethtool_get_rx_ntuple(dev
, useraddr
);
1519 case ETHTOOL_GSSET_INFO
:
1520 rc
= ethtool_get_sset_info(dev
, useraddr
);
1526 if (dev
->ethtool_ops
->complete
)
1527 dev
->ethtool_ops
->complete(dev
);
1529 if (old_features
!= dev
->features
)
1530 netdev_features_change(dev
);
1535 EXPORT_SYMBOL(ethtool_op_get_link
);
1536 EXPORT_SYMBOL(ethtool_op_get_sg
);
1537 EXPORT_SYMBOL(ethtool_op_get_tso
);
1538 EXPORT_SYMBOL(ethtool_op_set_sg
);
1539 EXPORT_SYMBOL(ethtool_op_set_tso
);
1540 EXPORT_SYMBOL(ethtool_op_set_tx_csum
);
1541 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum
);
1542 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum
);
1543 EXPORT_SYMBOL(ethtool_op_set_ufo
);
1544 EXPORT_SYMBOL(ethtool_op_get_ufo
);
1545 EXPORT_SYMBOL(ethtool_op_set_flags
);
1546 EXPORT_SYMBOL(ethtool_op_get_flags
);