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 <asm/uaccess.h>
23 * Some useful ethtool_ops methods that're device independent.
24 * If we find that all drivers want to do the same thing here,
25 * we can turn these into dev_() function calls.
28 u32 ethtool_op_get_link(struct net_device *dev)
30 return netif_carrier_ok(dev) ? 1 : 0;
33 u32 ethtool_op_get_rx_csum(struct net_device *dev)
35 return (dev->features & NETIF_F_ALL_CSUM) != 0;
37 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
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;
55 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
58 dev->features |= NETIF_F_HW_CSUM;
60 dev->features &= ~NETIF_F_HW_CSUM;
65 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
68 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
70 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
75 u32 ethtool_op_get_sg(struct net_device *dev)
77 return (dev->features & NETIF_F_SG) != 0;
80 int ethtool_op_set_sg(struct net_device *dev, u32 data)
83 dev->features |= NETIF_F_SG;
85 dev->features &= ~NETIF_F_SG;
90 u32 ethtool_op_get_tso(struct net_device *dev)
92 return (dev->features & NETIF_F_TSO) != 0;
95 int ethtool_op_set_tso(struct net_device *dev, u32 data)
98 dev->features |= NETIF_F_TSO;
100 dev->features &= ~NETIF_F_TSO;
105 u32 ethtool_op_get_ufo(struct net_device *dev)
107 return (dev->features & NETIF_F_UFO) != 0;
110 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
113 dev->features |= NETIF_F_UFO;
115 dev->features &= ~NETIF_F_UFO;
119 /* the following list of flags are the same as their associated
120 * NETIF_F_xxx values in include/linux/netdevice.h
122 static const u32 flags_dup_features =
123 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE);
125 u32 ethtool_op_get_flags(struct net_device *dev)
127 /* in the future, this function will probably contain additional
128 * handling for flags which are not so easily handled
129 * by a simple masking operation
132 return dev->features & flags_dup_features;
135 int ethtool_op_set_flags(struct net_device *dev, u32 data)
137 if (data & ETH_FLAG_LRO)
138 dev->features |= NETIF_F_LRO;
140 dev->features &= ~NETIF_F_LRO;
142 if (data & ETH_FLAG_NTUPLE)
143 dev->features |= NETIF_F_NTUPLE;
145 dev->features &= ~NETIF_F_NTUPLE;
150 void ethtool_ntuple_flush(struct net_device *dev)
152 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
154 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
155 list_del(&fsc->list);
158 dev->ethtool_ntuple_list.count = 0;
160 EXPORT_SYMBOL(ethtool_ntuple_flush);
162 /* Handlers for each ethtool command */
164 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
166 struct ethtool_cmd cmd = { ETHTOOL_GSET };
169 if (!dev->ethtool_ops->get_settings)
172 err = dev->ethtool_ops->get_settings(dev, &cmd);
176 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
181 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
183 struct ethtool_cmd cmd;
185 if (!dev->ethtool_ops->set_settings)
188 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
191 return dev->ethtool_ops->set_settings(dev, &cmd);
194 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
196 struct ethtool_drvinfo info;
197 const struct ethtool_ops *ops = dev->ethtool_ops;
199 if (!ops->get_drvinfo)
202 memset(&info, 0, sizeof(info));
203 info.cmd = ETHTOOL_GDRVINFO;
204 ops->get_drvinfo(dev, &info);
206 if (ops->get_sset_count) {
209 rc = ops->get_sset_count(dev, ETH_SS_TEST);
211 info.testinfo_len = rc;
212 rc = ops->get_sset_count(dev, ETH_SS_STATS);
215 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
217 info.n_priv_flags = rc;
219 if (ops->get_regs_len)
220 info.regdump_len = ops->get_regs_len(dev);
221 if (ops->get_eeprom_len)
222 info.eedump_len = ops->get_eeprom_len(dev);
224 if (copy_to_user(useraddr, &info, sizeof(info)))
229 static int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
231 struct ethtool_rxnfc cmd;
233 if (!dev->ethtool_ops->set_rxnfc)
236 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
239 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
242 static int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
244 struct ethtool_rxnfc info;
245 const struct ethtool_ops *ops = dev->ethtool_ops;
247 void *rule_buf = NULL;
252 if (copy_from_user(&info, useraddr, sizeof(info)))
255 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
256 if (info.rule_cnt > 0) {
257 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
264 ret = ops->get_rxnfc(dev, &info, rule_buf);
269 if (copy_to_user(useraddr, &info, sizeof(info)))
273 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
274 if (copy_to_user(useraddr, rule_buf,
275 info.rule_cnt * sizeof(u32)))
286 static int __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
287 struct ethtool_rx_ntuple_flow_spec *spec)
289 struct ethtool_rx_ntuple_flow_spec_container *fsc;
291 /* don't add filters forever */
292 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY)
295 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
299 /* Copy the whole filter over */
300 fsc->fs.flow_type = spec->flow_type;
301 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
302 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
304 fsc->fs.vlan_tag = spec->vlan_tag;
305 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
306 fsc->fs.data = spec->data;
307 fsc->fs.data_mask = spec->data_mask;
308 fsc->fs.action = spec->action;
310 /* add to the list */
311 list_add_tail_rcu(&fsc->list, &list->list);
317 static int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
319 struct ethtool_rx_ntuple cmd;
320 const struct ethtool_ops *ops = dev->ethtool_ops;
323 if (!ops->set_rx_ntuple)
326 if (!(dev->features & NETIF_F_NTUPLE))
329 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
332 ret = ops->set_rx_ntuple(dev, &cmd);
335 * Cache filter in dev struct for GET operation only if
336 * the underlying driver doesn't have its own GET operation, and
337 * only if the filter was added successfully.
339 if (!ops->get_rx_ntuple && !ret)
340 if (__rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs))
346 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
348 struct ethtool_gstrings gstrings;
349 const struct ethtool_ops *ops = dev->ethtool_ops;
350 struct ethtool_rx_ntuple_flow_spec_container *fsc;
353 int ret, i, num_strings = 0;
355 if (!ops->get_sset_count)
358 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
361 ret = ops->get_sset_count(dev, gstrings.string_set);
367 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
371 if (ops->get_rx_ntuple) {
372 /* driver-specific filter grab */
373 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
377 /* default ethtool filter grab */
380 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
381 sprintf(p, "Filter %d:\n", i);
382 p += ETH_GSTRING_LEN;
385 switch (fsc->fs.flow_type) {
387 sprintf(p, "\tFlow Type: TCP\n");
388 p += ETH_GSTRING_LEN;
392 sprintf(p, "\tFlow Type: UDP\n");
393 p += ETH_GSTRING_LEN;
397 sprintf(p, "\tFlow Type: SCTP\n");
398 p += ETH_GSTRING_LEN;
402 sprintf(p, "\tFlow Type: AH ESP\n");
403 p += ETH_GSTRING_LEN;
407 sprintf(p, "\tFlow Type: ESP\n");
408 p += ETH_GSTRING_LEN;
412 sprintf(p, "\tFlow Type: Raw IP\n");
413 p += ETH_GSTRING_LEN;
417 sprintf(p, "\tFlow Type: IPv4\n");
418 p += ETH_GSTRING_LEN;
422 sprintf(p, "\tFlow Type: Unknown\n");
423 p += ETH_GSTRING_LEN;
428 /* now the rest of the filters */
429 switch (fsc->fs.flow_type) {
433 sprintf(p, "\tSrc IP addr: 0x%x\n",
434 fsc->fs.h_u.tcp_ip4_spec.ip4src);
435 p += ETH_GSTRING_LEN;
437 sprintf(p, "\tSrc IP mask: 0x%x\n",
438 fsc->fs.m_u.tcp_ip4_spec.ip4src);
439 p += ETH_GSTRING_LEN;
441 sprintf(p, "\tDest IP addr: 0x%x\n",
442 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
443 p += ETH_GSTRING_LEN;
445 sprintf(p, "\tDest IP mask: 0x%x\n",
446 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
447 p += ETH_GSTRING_LEN;
449 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
450 fsc->fs.h_u.tcp_ip4_spec.psrc,
451 fsc->fs.m_u.tcp_ip4_spec.psrc);
452 p += ETH_GSTRING_LEN;
454 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
455 fsc->fs.h_u.tcp_ip4_spec.pdst,
456 fsc->fs.m_u.tcp_ip4_spec.pdst);
457 p += ETH_GSTRING_LEN;
459 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
460 fsc->fs.h_u.tcp_ip4_spec.tos,
461 fsc->fs.m_u.tcp_ip4_spec.tos);
462 p += ETH_GSTRING_LEN;
467 sprintf(p, "\tSrc IP addr: 0x%x\n",
468 fsc->fs.h_u.ah_ip4_spec.ip4src);
469 p += ETH_GSTRING_LEN;
471 sprintf(p, "\tSrc IP mask: 0x%x\n",
472 fsc->fs.m_u.ah_ip4_spec.ip4src);
473 p += ETH_GSTRING_LEN;
475 sprintf(p, "\tDest IP addr: 0x%x\n",
476 fsc->fs.h_u.ah_ip4_spec.ip4dst);
477 p += ETH_GSTRING_LEN;
479 sprintf(p, "\tDest IP mask: 0x%x\n",
480 fsc->fs.m_u.ah_ip4_spec.ip4dst);
481 p += ETH_GSTRING_LEN;
483 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
484 fsc->fs.h_u.ah_ip4_spec.spi,
485 fsc->fs.m_u.ah_ip4_spec.spi);
486 p += ETH_GSTRING_LEN;
488 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
489 fsc->fs.h_u.ah_ip4_spec.tos,
490 fsc->fs.m_u.ah_ip4_spec.tos);
491 p += ETH_GSTRING_LEN;
495 sprintf(p, "\tSrc IP addr: 0x%x\n",
496 fsc->fs.h_u.raw_ip4_spec.ip4src);
497 p += ETH_GSTRING_LEN;
499 sprintf(p, "\tSrc IP mask: 0x%x\n",
500 fsc->fs.m_u.raw_ip4_spec.ip4src);
501 p += ETH_GSTRING_LEN;
503 sprintf(p, "\tDest IP addr: 0x%x\n",
504 fsc->fs.h_u.raw_ip4_spec.ip4dst);
505 p += ETH_GSTRING_LEN;
507 sprintf(p, "\tDest IP mask: 0x%x\n",
508 fsc->fs.m_u.raw_ip4_spec.ip4dst);
509 p += ETH_GSTRING_LEN;
513 sprintf(p, "\tSrc IP addr: 0x%x\n",
514 fsc->fs.h_u.usr_ip4_spec.ip4src);
515 p += ETH_GSTRING_LEN;
517 sprintf(p, "\tSrc IP mask: 0x%x\n",
518 fsc->fs.m_u.usr_ip4_spec.ip4src);
519 p += ETH_GSTRING_LEN;
521 sprintf(p, "\tDest IP addr: 0x%x\n",
522 fsc->fs.h_u.usr_ip4_spec.ip4dst);
523 p += ETH_GSTRING_LEN;
525 sprintf(p, "\tDest IP mask: 0x%x\n",
526 fsc->fs.m_u.usr_ip4_spec.ip4dst);
527 p += ETH_GSTRING_LEN;
529 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
530 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
531 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
532 p += ETH_GSTRING_LEN;
534 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
535 fsc->fs.h_u.usr_ip4_spec.tos,
536 fsc->fs.m_u.usr_ip4_spec.tos);
537 p += ETH_GSTRING_LEN;
539 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
540 fsc->fs.h_u.usr_ip4_spec.ip_ver,
541 fsc->fs.m_u.usr_ip4_spec.ip_ver);
542 p += ETH_GSTRING_LEN;
544 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
545 fsc->fs.h_u.usr_ip4_spec.proto,
546 fsc->fs.m_u.usr_ip4_spec.proto);
547 p += ETH_GSTRING_LEN;
551 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
552 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
553 p += ETH_GSTRING_LEN;
555 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
556 p += ETH_GSTRING_LEN;
558 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
559 p += ETH_GSTRING_LEN;
561 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
562 sprintf(p, "\tAction: Drop\n");
564 sprintf(p, "\tAction: Direct to queue %d\n",
566 p += ETH_GSTRING_LEN;
572 /* indicate to userspace how many strings we actually have */
573 gstrings.len = num_strings;
575 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
577 useraddr += sizeof(gstrings);
578 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
587 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
589 struct ethtool_regs regs;
590 const struct ethtool_ops *ops = dev->ethtool_ops;
594 if (!ops->get_regs || !ops->get_regs_len)
597 if (copy_from_user(®s, useraddr, sizeof(regs)))
600 reglen = ops->get_regs_len(dev);
601 if (regs.len > reglen)
604 regbuf = kmalloc(reglen, GFP_USER);
608 ops->get_regs(dev, ®s, regbuf);
611 if (copy_to_user(useraddr, ®s, sizeof(regs)))
613 useraddr += offsetof(struct ethtool_regs, data);
614 if (copy_to_user(useraddr, regbuf, regs.len))
623 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
625 struct ethtool_value reset;
628 if (!dev->ethtool_ops->reset)
631 if (copy_from_user(&reset, useraddr, sizeof(reset)))
634 /* Clear ethtool n-tuple list */
635 ethtool_ntuple_flush(dev);
637 ret = dev->ethtool_ops->reset(dev, &reset.data);
641 if (copy_to_user(useraddr, &reset, sizeof(reset)))
646 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
648 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
650 if (!dev->ethtool_ops->get_wol)
653 dev->ethtool_ops->get_wol(dev, &wol);
655 if (copy_to_user(useraddr, &wol, sizeof(wol)))
660 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
662 struct ethtool_wolinfo wol;
664 if (!dev->ethtool_ops->set_wol)
667 if (copy_from_user(&wol, useraddr, sizeof(wol)))
670 return dev->ethtool_ops->set_wol(dev, &wol);
673 static int ethtool_nway_reset(struct net_device *dev)
675 if (!dev->ethtool_ops->nway_reset)
678 return dev->ethtool_ops->nway_reset(dev);
681 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
683 struct ethtool_eeprom eeprom;
684 const struct ethtool_ops *ops = dev->ethtool_ops;
685 void __user *userbuf = useraddr + sizeof(eeprom);
690 if (!ops->get_eeprom || !ops->get_eeprom_len)
693 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
696 /* Check for wrap and zero */
697 if (eeprom.offset + eeprom.len <= eeprom.offset)
700 /* Check for exceeding total eeprom len */
701 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
704 data = kmalloc(PAGE_SIZE, GFP_USER);
708 bytes_remaining = eeprom.len;
709 while (bytes_remaining > 0) {
710 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
712 ret = ops->get_eeprom(dev, &eeprom, data);
715 if (copy_to_user(userbuf, data, eeprom.len)) {
719 userbuf += eeprom.len;
720 eeprom.offset += eeprom.len;
721 bytes_remaining -= eeprom.len;
724 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
725 eeprom.offset -= eeprom.len;
726 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
733 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
735 struct ethtool_eeprom eeprom;
736 const struct ethtool_ops *ops = dev->ethtool_ops;
737 void __user *userbuf = useraddr + sizeof(eeprom);
742 if (!ops->set_eeprom || !ops->get_eeprom_len)
745 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
748 /* Check for wrap and zero */
749 if (eeprom.offset + eeprom.len <= eeprom.offset)
752 /* Check for exceeding total eeprom len */
753 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
756 data = kmalloc(PAGE_SIZE, GFP_USER);
760 bytes_remaining = eeprom.len;
761 while (bytes_remaining > 0) {
762 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
764 if (copy_from_user(data, userbuf, eeprom.len)) {
768 ret = ops->set_eeprom(dev, &eeprom, data);
771 userbuf += eeprom.len;
772 eeprom.offset += eeprom.len;
773 bytes_remaining -= eeprom.len;
780 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
782 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
784 if (!dev->ethtool_ops->get_coalesce)
787 dev->ethtool_ops->get_coalesce(dev, &coalesce);
789 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
794 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
796 struct ethtool_coalesce coalesce;
798 if (!dev->ethtool_ops->set_coalesce)
801 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
804 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
807 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
809 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
811 if (!dev->ethtool_ops->get_ringparam)
814 dev->ethtool_ops->get_ringparam(dev, &ringparam);
816 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
821 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
823 struct ethtool_ringparam ringparam;
825 if (!dev->ethtool_ops->set_ringparam)
828 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
831 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
834 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
836 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
838 if (!dev->ethtool_ops->get_pauseparam)
841 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
843 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
848 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
850 struct ethtool_pauseparam pauseparam;
852 if (!dev->ethtool_ops->set_pauseparam)
855 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
858 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
861 static int __ethtool_set_sg(struct net_device *dev, u32 data)
865 if (!data && dev->ethtool_ops->set_tso) {
866 err = dev->ethtool_ops->set_tso(dev, 0);
871 if (!data && dev->ethtool_ops->set_ufo) {
872 err = dev->ethtool_ops->set_ufo(dev, 0);
876 return dev->ethtool_ops->set_sg(dev, data);
879 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
881 struct ethtool_value edata;
884 if (!dev->ethtool_ops->set_tx_csum)
887 if (copy_from_user(&edata, useraddr, sizeof(edata)))
890 if (!edata.data && dev->ethtool_ops->set_sg) {
891 err = __ethtool_set_sg(dev, 0);
896 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
899 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
901 struct ethtool_value edata;
903 if (!dev->ethtool_ops->set_rx_csum)
906 if (copy_from_user(&edata, useraddr, sizeof(edata)))
909 if (!edata.data && dev->ethtool_ops->set_sg)
910 dev->features &= ~NETIF_F_GRO;
912 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
915 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
917 struct ethtool_value edata;
919 if (!dev->ethtool_ops->set_sg)
922 if (copy_from_user(&edata, useraddr, sizeof(edata)))
926 !(dev->features & NETIF_F_ALL_CSUM))
929 return __ethtool_set_sg(dev, edata.data);
932 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
934 struct ethtool_value edata;
936 if (!dev->ethtool_ops->set_tso)
939 if (copy_from_user(&edata, useraddr, sizeof(edata)))
942 if (edata.data && !(dev->features & NETIF_F_SG))
945 return dev->ethtool_ops->set_tso(dev, edata.data);
948 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
950 struct ethtool_value edata;
952 if (!dev->ethtool_ops->set_ufo)
954 if (copy_from_user(&edata, useraddr, sizeof(edata)))
956 if (edata.data && !(dev->features & NETIF_F_SG))
958 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
960 return dev->ethtool_ops->set_ufo(dev, edata.data);
963 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
965 struct ethtool_value edata = { ETHTOOL_GGSO };
967 edata.data = dev->features & NETIF_F_GSO;
968 if (copy_to_user(useraddr, &edata, sizeof(edata)))
973 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
975 struct ethtool_value edata;
977 if (copy_from_user(&edata, useraddr, sizeof(edata)))
980 dev->features |= NETIF_F_GSO;
982 dev->features &= ~NETIF_F_GSO;
986 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
988 struct ethtool_value edata = { ETHTOOL_GGRO };
990 edata.data = dev->features & NETIF_F_GRO;
991 if (copy_to_user(useraddr, &edata, sizeof(edata)))
996 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
998 struct ethtool_value edata;
1000 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1004 if (!dev->ethtool_ops->get_rx_csum ||
1005 !dev->ethtool_ops->get_rx_csum(dev))
1007 dev->features |= NETIF_F_GRO;
1009 dev->features &= ~NETIF_F_GRO;
1014 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1016 struct ethtool_test test;
1017 const struct ethtool_ops *ops = dev->ethtool_ops;
1021 if (!ops->self_test || !ops->get_sset_count)
1024 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1027 WARN_ON(test_len == 0);
1029 if (copy_from_user(&test, useraddr, sizeof(test)))
1032 test.len = test_len;
1033 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1037 ops->self_test(dev, &test, data);
1040 if (copy_to_user(useraddr, &test, sizeof(test)))
1042 useraddr += sizeof(test);
1043 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1052 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1054 struct ethtool_gstrings gstrings;
1055 const struct ethtool_ops *ops = dev->ethtool_ops;
1059 if (!ops->get_strings || !ops->get_sset_count)
1062 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1065 ret = ops->get_sset_count(dev, gstrings.string_set);
1071 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1075 ops->get_strings(dev, gstrings.string_set, data);
1078 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1080 useraddr += sizeof(gstrings);
1081 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1090 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1092 struct ethtool_value id;
1094 if (!dev->ethtool_ops->phys_id)
1097 if (copy_from_user(&id, useraddr, sizeof(id)))
1100 return dev->ethtool_ops->phys_id(dev, id.data);
1103 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1105 struct ethtool_stats stats;
1106 const struct ethtool_ops *ops = dev->ethtool_ops;
1110 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1113 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1116 WARN_ON(n_stats == 0);
1118 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1121 stats.n_stats = n_stats;
1122 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1126 ops->get_ethtool_stats(dev, &stats, data);
1129 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1131 useraddr += sizeof(stats);
1132 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1141 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1143 struct ethtool_perm_addr epaddr;
1145 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1148 if (epaddr.size < dev->addr_len)
1150 epaddr.size = dev->addr_len;
1152 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1154 useraddr += sizeof(epaddr);
1155 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1160 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1161 u32 cmd, u32 (*actor)(struct net_device *))
1163 struct ethtool_value edata = { cmd };
1168 edata.data = actor(dev);
1170 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1175 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1176 void (*actor)(struct net_device *, u32))
1178 struct ethtool_value edata;
1183 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1186 actor(dev, edata.data);
1190 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1191 int (*actor)(struct net_device *, u32))
1193 struct ethtool_value edata;
1198 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1201 return actor(dev, edata.data);
1204 static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1206 struct ethtool_flash efl;
1208 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1211 if (!dev->ethtool_ops->flash_device)
1214 return dev->ethtool_ops->flash_device(dev, &efl);
1217 /* The main entry point in this file. Called from net/core/dev.c */
1219 int dev_ethtool(struct net *net, struct ifreq *ifr)
1221 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1222 void __user *useraddr = ifr->ifr_data;
1225 unsigned long old_features;
1227 if (!dev || !netif_device_present(dev))
1230 if (!dev->ethtool_ops)
1233 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1236 /* Allow some commands to be done by anyone */
1238 case ETHTOOL_GDRVINFO:
1239 case ETHTOOL_GMSGLVL:
1240 case ETHTOOL_GCOALESCE:
1241 case ETHTOOL_GRINGPARAM:
1242 case ETHTOOL_GPAUSEPARAM:
1243 case ETHTOOL_GRXCSUM:
1244 case ETHTOOL_GTXCSUM:
1246 case ETHTOOL_GSTRINGS:
1248 case ETHTOOL_GPERMADDR:
1251 case ETHTOOL_GFLAGS:
1252 case ETHTOOL_GPFLAGS:
1254 case ETHTOOL_GRXRINGS:
1255 case ETHTOOL_GRXCLSRLCNT:
1256 case ETHTOOL_GRXCLSRULE:
1257 case ETHTOOL_GRXCLSRLALL:
1260 if (!capable(CAP_NET_ADMIN))
1264 if (dev->ethtool_ops->begin)
1265 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1268 old_features = dev->features;
1272 rc = ethtool_get_settings(dev, useraddr);
1275 rc = ethtool_set_settings(dev, useraddr);
1277 case ETHTOOL_GDRVINFO:
1278 rc = ethtool_get_drvinfo(dev, useraddr);
1281 rc = ethtool_get_regs(dev, useraddr);
1284 rc = ethtool_get_wol(dev, useraddr);
1287 rc = ethtool_set_wol(dev, useraddr);
1289 case ETHTOOL_GMSGLVL:
1290 rc = ethtool_get_value(dev, useraddr, ethcmd,
1291 dev->ethtool_ops->get_msglevel);
1293 case ETHTOOL_SMSGLVL:
1294 rc = ethtool_set_value_void(dev, useraddr,
1295 dev->ethtool_ops->set_msglevel);
1297 case ETHTOOL_NWAY_RST:
1298 rc = ethtool_nway_reset(dev);
1301 rc = ethtool_get_value(dev, useraddr, ethcmd,
1302 dev->ethtool_ops->get_link);
1304 case ETHTOOL_GEEPROM:
1305 rc = ethtool_get_eeprom(dev, useraddr);
1307 case ETHTOOL_SEEPROM:
1308 rc = ethtool_set_eeprom(dev, useraddr);
1310 case ETHTOOL_GCOALESCE:
1311 rc = ethtool_get_coalesce(dev, useraddr);
1313 case ETHTOOL_SCOALESCE:
1314 rc = ethtool_set_coalesce(dev, useraddr);
1316 case ETHTOOL_GRINGPARAM:
1317 rc = ethtool_get_ringparam(dev, useraddr);
1319 case ETHTOOL_SRINGPARAM:
1320 rc = ethtool_set_ringparam(dev, useraddr);
1322 case ETHTOOL_GPAUSEPARAM:
1323 rc = ethtool_get_pauseparam(dev, useraddr);
1325 case ETHTOOL_SPAUSEPARAM:
1326 rc = ethtool_set_pauseparam(dev, useraddr);
1328 case ETHTOOL_GRXCSUM:
1329 rc = ethtool_get_value(dev, useraddr, ethcmd,
1330 (dev->ethtool_ops->get_rx_csum ?
1331 dev->ethtool_ops->get_rx_csum :
1332 ethtool_op_get_rx_csum));
1334 case ETHTOOL_SRXCSUM:
1335 rc = ethtool_set_rx_csum(dev, useraddr);
1337 case ETHTOOL_GTXCSUM:
1338 rc = ethtool_get_value(dev, useraddr, ethcmd,
1339 (dev->ethtool_ops->get_tx_csum ?
1340 dev->ethtool_ops->get_tx_csum :
1341 ethtool_op_get_tx_csum));
1343 case ETHTOOL_STXCSUM:
1344 rc = ethtool_set_tx_csum(dev, useraddr);
1347 rc = ethtool_get_value(dev, useraddr, ethcmd,
1348 (dev->ethtool_ops->get_sg ?
1349 dev->ethtool_ops->get_sg :
1350 ethtool_op_get_sg));
1353 rc = ethtool_set_sg(dev, useraddr);
1356 rc = ethtool_get_value(dev, useraddr, ethcmd,
1357 (dev->ethtool_ops->get_tso ?
1358 dev->ethtool_ops->get_tso :
1359 ethtool_op_get_tso));
1362 rc = ethtool_set_tso(dev, useraddr);
1365 rc = ethtool_self_test(dev, useraddr);
1367 case ETHTOOL_GSTRINGS:
1368 rc = ethtool_get_strings(dev, useraddr);
1370 case ETHTOOL_PHYS_ID:
1371 rc = ethtool_phys_id(dev, useraddr);
1373 case ETHTOOL_GSTATS:
1374 rc = ethtool_get_stats(dev, useraddr);
1376 case ETHTOOL_GPERMADDR:
1377 rc = ethtool_get_perm_addr(dev, useraddr);
1380 rc = ethtool_get_value(dev, useraddr, ethcmd,
1381 (dev->ethtool_ops->get_ufo ?
1382 dev->ethtool_ops->get_ufo :
1383 ethtool_op_get_ufo));
1386 rc = ethtool_set_ufo(dev, useraddr);
1389 rc = ethtool_get_gso(dev, useraddr);
1392 rc = ethtool_set_gso(dev, useraddr);
1394 case ETHTOOL_GFLAGS:
1395 rc = ethtool_get_value(dev, useraddr, ethcmd,
1396 (dev->ethtool_ops->get_flags ?
1397 dev->ethtool_ops->get_flags :
1398 ethtool_op_get_flags));
1400 case ETHTOOL_SFLAGS:
1401 rc = ethtool_set_value(dev, useraddr,
1402 dev->ethtool_ops->set_flags);
1404 case ETHTOOL_GPFLAGS:
1405 rc = ethtool_get_value(dev, useraddr, ethcmd,
1406 dev->ethtool_ops->get_priv_flags);
1408 case ETHTOOL_SPFLAGS:
1409 rc = ethtool_set_value(dev, useraddr,
1410 dev->ethtool_ops->set_priv_flags);
1413 case ETHTOOL_GRXRINGS:
1414 case ETHTOOL_GRXCLSRLCNT:
1415 case ETHTOOL_GRXCLSRULE:
1416 case ETHTOOL_GRXCLSRLALL:
1417 rc = ethtool_get_rxnfc(dev, useraddr);
1420 case ETHTOOL_SRXCLSRLDEL:
1421 case ETHTOOL_SRXCLSRLINS:
1422 rc = ethtool_set_rxnfc(dev, useraddr);
1425 rc = ethtool_get_gro(dev, useraddr);
1428 rc = ethtool_set_gro(dev, useraddr);
1430 case ETHTOOL_FLASHDEV:
1431 rc = ethtool_flash_device(dev, useraddr);
1434 rc = ethtool_reset(dev, useraddr);
1436 case ETHTOOL_SRXNTUPLE:
1437 rc = ethtool_set_rx_ntuple(dev, useraddr);
1439 case ETHTOOL_GRXNTUPLE:
1440 rc = ethtool_get_rx_ntuple(dev, useraddr);
1446 if (dev->ethtool_ops->complete)
1447 dev->ethtool_ops->complete(dev);
1449 if (old_features != dev->features)
1450 netdev_features_change(dev);
1455 EXPORT_SYMBOL(ethtool_op_get_link);
1456 EXPORT_SYMBOL(ethtool_op_get_sg);
1457 EXPORT_SYMBOL(ethtool_op_get_tso);
1458 EXPORT_SYMBOL(ethtool_op_set_sg);
1459 EXPORT_SYMBOL(ethtool_op_set_tso);
1460 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1461 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1462 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1463 EXPORT_SYMBOL(ethtool_op_set_ufo);
1464 EXPORT_SYMBOL(ethtool_op_get_ufo);
1465 EXPORT_SYMBOL(ethtool_op_set_flags);
1466 EXPORT_SYMBOL(ethtool_op_get_flags);