2 * net/core/ethtool.c - Ethtool ioctl handler
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 const struct ethtool_ops *ops = dev->ethtool_ops;
138 unsigned long features = dev->features;
140 if (data & ETH_FLAG_LRO)
141 features |= NETIF_F_LRO;
143 features &= ~NETIF_F_LRO;
145 if (data & ETH_FLAG_NTUPLE) {
146 if (!ops->set_rx_ntuple)
148 features |= NETIF_F_NTUPLE;
150 /* safe to clear regardless */
151 features &= ~NETIF_F_NTUPLE;
154 dev->features = features;
158 void ethtool_ntuple_flush(struct net_device *dev)
160 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
162 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
163 list_del(&fsc->list);
166 dev->ethtool_ntuple_list.count = 0;
168 EXPORT_SYMBOL(ethtool_ntuple_flush);
170 /* Handlers for each ethtool command */
172 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
174 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
177 if (!dev->ethtool_ops->get_settings)
180 err = dev->ethtool_ops->get_settings(dev, &cmd);
184 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
189 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
191 struct ethtool_cmd cmd;
193 if (!dev->ethtool_ops->set_settings)
196 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
199 return dev->ethtool_ops->set_settings(dev, &cmd);
203 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
205 static noinline int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
207 struct ethtool_drvinfo info;
208 const struct ethtool_ops *ops = dev->ethtool_ops;
210 if (!ops->get_drvinfo)
213 memset(&info, 0, sizeof(info));
214 info.cmd = ETHTOOL_GDRVINFO;
215 ops->get_drvinfo(dev, &info);
217 if (ops->get_sset_count) {
220 rc = ops->get_sset_count(dev, ETH_SS_TEST);
222 info.testinfo_len = rc;
223 rc = ops->get_sset_count(dev, ETH_SS_STATS);
226 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
228 info.n_priv_flags = rc;
230 if (ops->get_regs_len)
231 info.regdump_len = ops->get_regs_len(dev);
232 if (ops->get_eeprom_len)
233 info.eedump_len = ops->get_eeprom_len(dev);
235 if (copy_to_user(useraddr, &info, sizeof(info)))
241 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
243 static noinline int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
245 struct ethtool_rxnfc cmd;
247 if (!dev->ethtool_ops->set_rxnfc)
250 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
253 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
257 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
259 static noinline int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
261 struct ethtool_rxnfc info;
262 const struct ethtool_ops *ops = dev->ethtool_ops;
264 void *rule_buf = NULL;
269 if (copy_from_user(&info, useraddr, sizeof(info)))
272 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
273 if (info.rule_cnt > 0) {
274 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
281 ret = ops->get_rxnfc(dev, &info, rule_buf);
286 if (copy_to_user(useraddr, &info, sizeof(info)))
290 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
291 if (copy_to_user(useraddr, rule_buf,
292 info.rule_cnt * sizeof(u32)))
303 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
304 struct ethtool_rx_ntuple_flow_spec *spec,
305 struct ethtool_rx_ntuple_flow_spec_container *fsc)
308 /* don't add filters forever */
309 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
310 /* free the container */
315 /* Copy the whole filter over */
316 fsc->fs.flow_type = spec->flow_type;
317 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
318 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
320 fsc->fs.vlan_tag = spec->vlan_tag;
321 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
322 fsc->fs.data = spec->data;
323 fsc->fs.data_mask = spec->data_mask;
324 fsc->fs.action = spec->action;
326 /* add to the list */
327 list_add_tail_rcu(&fsc->list, &list->list);
332 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
334 static noinline int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
336 struct ethtool_rx_ntuple cmd;
337 const struct ethtool_ops *ops = dev->ethtool_ops;
338 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
341 if (!(dev->features & NETIF_F_NTUPLE))
344 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
348 * Cache filter in dev struct for GET operation only if
349 * the underlying driver doesn't have its own GET operation, and
350 * only if the filter was added successfully. First make sure we
351 * can allocate the filter, then continue if successful.
353 if (!ops->get_rx_ntuple) {
354 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
359 ret = ops->set_rx_ntuple(dev, &cmd);
365 if (!ops->get_rx_ntuple)
366 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
371 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
373 struct ethtool_gstrings gstrings;
374 const struct ethtool_ops *ops = dev->ethtool_ops;
375 struct ethtool_rx_ntuple_flow_spec_container *fsc;
378 int ret, i, num_strings = 0;
380 if (!ops->get_sset_count)
383 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
386 ret = ops->get_sset_count(dev, gstrings.string_set);
392 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
396 if (ops->get_rx_ntuple) {
397 /* driver-specific filter grab */
398 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
402 /* default ethtool filter grab */
405 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
406 sprintf(p, "Filter %d:\n", i);
407 p += ETH_GSTRING_LEN;
410 switch (fsc->fs.flow_type) {
412 sprintf(p, "\tFlow Type: TCP\n");
413 p += ETH_GSTRING_LEN;
417 sprintf(p, "\tFlow Type: UDP\n");
418 p += ETH_GSTRING_LEN;
422 sprintf(p, "\tFlow Type: SCTP\n");
423 p += ETH_GSTRING_LEN;
427 sprintf(p, "\tFlow Type: AH ESP\n");
428 p += ETH_GSTRING_LEN;
432 sprintf(p, "\tFlow Type: ESP\n");
433 p += ETH_GSTRING_LEN;
437 sprintf(p, "\tFlow Type: Raw IP\n");
438 p += ETH_GSTRING_LEN;
442 sprintf(p, "\tFlow Type: IPv4\n");
443 p += ETH_GSTRING_LEN;
447 sprintf(p, "\tFlow Type: Unknown\n");
448 p += ETH_GSTRING_LEN;
453 /* now the rest of the filters */
454 switch (fsc->fs.flow_type) {
458 sprintf(p, "\tSrc IP addr: 0x%x\n",
459 fsc->fs.h_u.tcp_ip4_spec.ip4src);
460 p += ETH_GSTRING_LEN;
462 sprintf(p, "\tSrc IP mask: 0x%x\n",
463 fsc->fs.m_u.tcp_ip4_spec.ip4src);
464 p += ETH_GSTRING_LEN;
466 sprintf(p, "\tDest IP addr: 0x%x\n",
467 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
468 p += ETH_GSTRING_LEN;
470 sprintf(p, "\tDest IP mask: 0x%x\n",
471 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
472 p += ETH_GSTRING_LEN;
474 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
475 fsc->fs.h_u.tcp_ip4_spec.psrc,
476 fsc->fs.m_u.tcp_ip4_spec.psrc);
477 p += ETH_GSTRING_LEN;
479 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
480 fsc->fs.h_u.tcp_ip4_spec.pdst,
481 fsc->fs.m_u.tcp_ip4_spec.pdst);
482 p += ETH_GSTRING_LEN;
484 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
485 fsc->fs.h_u.tcp_ip4_spec.tos,
486 fsc->fs.m_u.tcp_ip4_spec.tos);
487 p += ETH_GSTRING_LEN;
492 sprintf(p, "\tSrc IP addr: 0x%x\n",
493 fsc->fs.h_u.ah_ip4_spec.ip4src);
494 p += ETH_GSTRING_LEN;
496 sprintf(p, "\tSrc IP mask: 0x%x\n",
497 fsc->fs.m_u.ah_ip4_spec.ip4src);
498 p += ETH_GSTRING_LEN;
500 sprintf(p, "\tDest IP addr: 0x%x\n",
501 fsc->fs.h_u.ah_ip4_spec.ip4dst);
502 p += ETH_GSTRING_LEN;
504 sprintf(p, "\tDest IP mask: 0x%x\n",
505 fsc->fs.m_u.ah_ip4_spec.ip4dst);
506 p += ETH_GSTRING_LEN;
508 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
509 fsc->fs.h_u.ah_ip4_spec.spi,
510 fsc->fs.m_u.ah_ip4_spec.spi);
511 p += ETH_GSTRING_LEN;
513 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
514 fsc->fs.h_u.ah_ip4_spec.tos,
515 fsc->fs.m_u.ah_ip4_spec.tos);
516 p += ETH_GSTRING_LEN;
520 sprintf(p, "\tSrc IP addr: 0x%x\n",
521 fsc->fs.h_u.raw_ip4_spec.ip4src);
522 p += ETH_GSTRING_LEN;
524 sprintf(p, "\tSrc IP mask: 0x%x\n",
525 fsc->fs.m_u.raw_ip4_spec.ip4src);
526 p += ETH_GSTRING_LEN;
528 sprintf(p, "\tDest IP addr: 0x%x\n",
529 fsc->fs.h_u.raw_ip4_spec.ip4dst);
530 p += ETH_GSTRING_LEN;
532 sprintf(p, "\tDest IP mask: 0x%x\n",
533 fsc->fs.m_u.raw_ip4_spec.ip4dst);
534 p += ETH_GSTRING_LEN;
538 sprintf(p, "\tSrc IP addr: 0x%x\n",
539 fsc->fs.h_u.usr_ip4_spec.ip4src);
540 p += ETH_GSTRING_LEN;
542 sprintf(p, "\tSrc IP mask: 0x%x\n",
543 fsc->fs.m_u.usr_ip4_spec.ip4src);
544 p += ETH_GSTRING_LEN;
546 sprintf(p, "\tDest IP addr: 0x%x\n",
547 fsc->fs.h_u.usr_ip4_spec.ip4dst);
548 p += ETH_GSTRING_LEN;
550 sprintf(p, "\tDest IP mask: 0x%x\n",
551 fsc->fs.m_u.usr_ip4_spec.ip4dst);
552 p += ETH_GSTRING_LEN;
554 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
555 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
556 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
557 p += ETH_GSTRING_LEN;
559 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
560 fsc->fs.h_u.usr_ip4_spec.tos,
561 fsc->fs.m_u.usr_ip4_spec.tos);
562 p += ETH_GSTRING_LEN;
564 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
565 fsc->fs.h_u.usr_ip4_spec.ip_ver,
566 fsc->fs.m_u.usr_ip4_spec.ip_ver);
567 p += ETH_GSTRING_LEN;
569 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
570 fsc->fs.h_u.usr_ip4_spec.proto,
571 fsc->fs.m_u.usr_ip4_spec.proto);
572 p += ETH_GSTRING_LEN;
576 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
577 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
578 p += ETH_GSTRING_LEN;
580 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
581 p += ETH_GSTRING_LEN;
583 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
584 p += ETH_GSTRING_LEN;
586 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
587 sprintf(p, "\tAction: Drop\n");
589 sprintf(p, "\tAction: Direct to queue %d\n",
591 p += ETH_GSTRING_LEN;
597 /* indicate to userspace how many strings we actually have */
598 gstrings.len = num_strings;
600 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
602 useraddr += sizeof(gstrings);
603 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
612 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
614 struct ethtool_regs regs;
615 const struct ethtool_ops *ops = dev->ethtool_ops;
619 if (!ops->get_regs || !ops->get_regs_len)
622 if (copy_from_user(®s, useraddr, sizeof(regs)))
625 reglen = ops->get_regs_len(dev);
626 if (regs.len > reglen)
629 regbuf = kmalloc(reglen, GFP_USER);
633 ops->get_regs(dev, ®s, regbuf);
636 if (copy_to_user(useraddr, ®s, sizeof(regs)))
638 useraddr += offsetof(struct ethtool_regs, data);
639 if (copy_to_user(useraddr, regbuf, regs.len))
648 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
650 struct ethtool_value reset;
653 if (!dev->ethtool_ops->reset)
656 if (copy_from_user(&reset, useraddr, sizeof(reset)))
659 ret = dev->ethtool_ops->reset(dev, &reset.data);
663 if (copy_to_user(useraddr, &reset, sizeof(reset)))
668 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
670 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
672 if (!dev->ethtool_ops->get_wol)
675 dev->ethtool_ops->get_wol(dev, &wol);
677 if (copy_to_user(useraddr, &wol, sizeof(wol)))
682 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
684 struct ethtool_wolinfo wol;
686 if (!dev->ethtool_ops->set_wol)
689 if (copy_from_user(&wol, useraddr, sizeof(wol)))
692 return dev->ethtool_ops->set_wol(dev, &wol);
695 static int ethtool_nway_reset(struct net_device *dev)
697 if (!dev->ethtool_ops->nway_reset)
700 return dev->ethtool_ops->nway_reset(dev);
703 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
705 struct ethtool_eeprom eeprom;
706 const struct ethtool_ops *ops = dev->ethtool_ops;
707 void __user *userbuf = useraddr + sizeof(eeprom);
712 if (!ops->get_eeprom || !ops->get_eeprom_len)
715 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
718 /* Check for wrap and zero */
719 if (eeprom.offset + eeprom.len <= eeprom.offset)
722 /* Check for exceeding total eeprom len */
723 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
726 data = kmalloc(PAGE_SIZE, GFP_USER);
730 bytes_remaining = eeprom.len;
731 while (bytes_remaining > 0) {
732 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
734 ret = ops->get_eeprom(dev, &eeprom, data);
737 if (copy_to_user(userbuf, data, eeprom.len)) {
741 userbuf += eeprom.len;
742 eeprom.offset += eeprom.len;
743 bytes_remaining -= eeprom.len;
746 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
747 eeprom.offset -= eeprom.len;
748 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
755 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
757 struct ethtool_eeprom eeprom;
758 const struct ethtool_ops *ops = dev->ethtool_ops;
759 void __user *userbuf = useraddr + sizeof(eeprom);
764 if (!ops->set_eeprom || !ops->get_eeprom_len)
767 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
770 /* Check for wrap and zero */
771 if (eeprom.offset + eeprom.len <= eeprom.offset)
774 /* Check for exceeding total eeprom len */
775 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
778 data = kmalloc(PAGE_SIZE, GFP_USER);
782 bytes_remaining = eeprom.len;
783 while (bytes_remaining > 0) {
784 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
786 if (copy_from_user(data, userbuf, eeprom.len)) {
790 ret = ops->set_eeprom(dev, &eeprom, data);
793 userbuf += eeprom.len;
794 eeprom.offset += eeprom.len;
795 bytes_remaining -= eeprom.len;
803 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
805 static noinline int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
807 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
809 if (!dev->ethtool_ops->get_coalesce)
812 dev->ethtool_ops->get_coalesce(dev, &coalesce);
814 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
820 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
822 static noinline int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
824 struct ethtool_coalesce coalesce;
826 if (!dev->ethtool_ops->set_coalesce)
829 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
832 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
835 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
837 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
839 if (!dev->ethtool_ops->get_ringparam)
842 dev->ethtool_ops->get_ringparam(dev, &ringparam);
844 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
849 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
851 struct ethtool_ringparam ringparam;
853 if (!dev->ethtool_ops->set_ringparam)
856 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
859 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
862 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
864 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
866 if (!dev->ethtool_ops->get_pauseparam)
869 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
871 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
876 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
878 struct ethtool_pauseparam pauseparam;
880 if (!dev->ethtool_ops->set_pauseparam)
883 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
886 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
889 static int __ethtool_set_sg(struct net_device *dev, u32 data)
893 if (!data && dev->ethtool_ops->set_tso) {
894 err = dev->ethtool_ops->set_tso(dev, 0);
899 if (!data && dev->ethtool_ops->set_ufo) {
900 err = dev->ethtool_ops->set_ufo(dev, 0);
904 return dev->ethtool_ops->set_sg(dev, data);
907 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
909 struct ethtool_value edata;
912 if (!dev->ethtool_ops->set_tx_csum)
915 if (copy_from_user(&edata, useraddr, sizeof(edata)))
918 if (!edata.data && dev->ethtool_ops->set_sg) {
919 err = __ethtool_set_sg(dev, 0);
924 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
927 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
929 struct ethtool_value edata;
931 if (!dev->ethtool_ops->set_rx_csum)
934 if (copy_from_user(&edata, useraddr, sizeof(edata)))
937 if (!edata.data && dev->ethtool_ops->set_sg)
938 dev->features &= ~NETIF_F_GRO;
940 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
943 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
945 struct ethtool_value edata;
947 if (!dev->ethtool_ops->set_sg)
950 if (copy_from_user(&edata, useraddr, sizeof(edata)))
954 !(dev->features & NETIF_F_ALL_CSUM))
957 return __ethtool_set_sg(dev, edata.data);
960 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
962 struct ethtool_value edata;
964 if (!dev->ethtool_ops->set_tso)
967 if (copy_from_user(&edata, useraddr, sizeof(edata)))
970 if (edata.data && !(dev->features & NETIF_F_SG))
973 return dev->ethtool_ops->set_tso(dev, edata.data);
976 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
978 struct ethtool_value edata;
980 if (!dev->ethtool_ops->set_ufo)
982 if (copy_from_user(&edata, useraddr, sizeof(edata)))
984 if (edata.data && !(dev->features & NETIF_F_SG))
986 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
988 return dev->ethtool_ops->set_ufo(dev, edata.data);
991 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
993 struct ethtool_value edata = { ETHTOOL_GGSO };
995 edata.data = dev->features & NETIF_F_GSO;
996 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1001 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1003 struct ethtool_value edata;
1005 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1008 dev->features |= NETIF_F_GSO;
1010 dev->features &= ~NETIF_F_GSO;
1014 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1016 struct ethtool_value edata = { ETHTOOL_GGRO };
1018 edata.data = dev->features & NETIF_F_GRO;
1019 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1024 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1026 struct ethtool_value edata;
1028 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1032 if (!dev->ethtool_ops->get_rx_csum ||
1033 !dev->ethtool_ops->get_rx_csum(dev))
1035 dev->features |= NETIF_F_GRO;
1037 dev->features &= ~NETIF_F_GRO;
1042 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1044 struct ethtool_test test;
1045 const struct ethtool_ops *ops = dev->ethtool_ops;
1049 if (!ops->self_test || !ops->get_sset_count)
1052 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1055 WARN_ON(test_len == 0);
1057 if (copy_from_user(&test, useraddr, sizeof(test)))
1060 test.len = test_len;
1061 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1065 ops->self_test(dev, &test, data);
1068 if (copy_to_user(useraddr, &test, sizeof(test)))
1070 useraddr += sizeof(test);
1071 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1080 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1082 struct ethtool_gstrings gstrings;
1083 const struct ethtool_ops *ops = dev->ethtool_ops;
1087 if (!ops->get_strings || !ops->get_sset_count)
1090 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1093 ret = ops->get_sset_count(dev, gstrings.string_set);
1099 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1103 ops->get_strings(dev, gstrings.string_set, data);
1106 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1108 useraddr += sizeof(gstrings);
1109 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1118 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1120 struct ethtool_value id;
1122 if (!dev->ethtool_ops->phys_id)
1125 if (copy_from_user(&id, useraddr, sizeof(id)))
1128 return dev->ethtool_ops->phys_id(dev, id.data);
1131 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1133 struct ethtool_stats stats;
1134 const struct ethtool_ops *ops = dev->ethtool_ops;
1138 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1141 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1144 WARN_ON(n_stats == 0);
1146 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1149 stats.n_stats = n_stats;
1150 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1154 ops->get_ethtool_stats(dev, &stats, data);
1157 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1159 useraddr += sizeof(stats);
1160 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1169 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1171 struct ethtool_perm_addr epaddr;
1173 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1176 if (epaddr.size < dev->addr_len)
1178 epaddr.size = dev->addr_len;
1180 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1182 useraddr += sizeof(epaddr);
1183 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1188 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1189 u32 cmd, u32 (*actor)(struct net_device *))
1191 struct ethtool_value edata = { .cmd = cmd };
1196 edata.data = actor(dev);
1198 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1203 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1204 void (*actor)(struct net_device *, u32))
1206 struct ethtool_value edata;
1211 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1214 actor(dev, edata.data);
1218 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1219 int (*actor)(struct net_device *, u32))
1221 struct ethtool_value edata;
1226 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1229 return actor(dev, edata.data);
1233 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
1235 static noinline int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1237 struct ethtool_flash efl;
1239 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1242 if (!dev->ethtool_ops->flash_device)
1245 return dev->ethtool_ops->flash_device(dev, &efl);
1248 /* The main entry point in this file. Called from net/core/dev.c */
1250 int dev_ethtool(struct net *net, struct ifreq *ifr)
1252 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1253 void __user *useraddr = ifr->ifr_data;
1256 unsigned long old_features;
1258 if (!dev || !netif_device_present(dev))
1261 if (!dev->ethtool_ops)
1264 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1267 /* Allow some commands to be done by anyone */
1269 case ETHTOOL_GDRVINFO:
1270 case ETHTOOL_GMSGLVL:
1271 case ETHTOOL_GCOALESCE:
1272 case ETHTOOL_GRINGPARAM:
1273 case ETHTOOL_GPAUSEPARAM:
1274 case ETHTOOL_GRXCSUM:
1275 case ETHTOOL_GTXCSUM:
1277 case ETHTOOL_GSTRINGS:
1279 case ETHTOOL_GPERMADDR:
1283 case ETHTOOL_GFLAGS:
1284 case ETHTOOL_GPFLAGS:
1286 case ETHTOOL_GRXRINGS:
1287 case ETHTOOL_GRXCLSRLCNT:
1288 case ETHTOOL_GRXCLSRULE:
1289 case ETHTOOL_GRXCLSRLALL:
1292 if (!capable(CAP_NET_ADMIN))
1296 if (dev->ethtool_ops->begin)
1297 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1300 old_features = dev->features;
1304 rc = ethtool_get_settings(dev, useraddr);
1307 rc = ethtool_set_settings(dev, useraddr);
1309 case ETHTOOL_GDRVINFO:
1310 rc = ethtool_get_drvinfo(dev, useraddr);
1313 rc = ethtool_get_regs(dev, useraddr);
1316 rc = ethtool_get_wol(dev, useraddr);
1319 rc = ethtool_set_wol(dev, useraddr);
1321 case ETHTOOL_GMSGLVL:
1322 rc = ethtool_get_value(dev, useraddr, ethcmd,
1323 dev->ethtool_ops->get_msglevel);
1325 case ETHTOOL_SMSGLVL:
1326 rc = ethtool_set_value_void(dev, useraddr,
1327 dev->ethtool_ops->set_msglevel);
1329 case ETHTOOL_NWAY_RST:
1330 rc = ethtool_nway_reset(dev);
1333 rc = ethtool_get_value(dev, useraddr, ethcmd,
1334 dev->ethtool_ops->get_link);
1336 case ETHTOOL_GEEPROM:
1337 rc = ethtool_get_eeprom(dev, useraddr);
1339 case ETHTOOL_SEEPROM:
1340 rc = ethtool_set_eeprom(dev, useraddr);
1342 case ETHTOOL_GCOALESCE:
1343 rc = ethtool_get_coalesce(dev, useraddr);
1345 case ETHTOOL_SCOALESCE:
1346 rc = ethtool_set_coalesce(dev, useraddr);
1348 case ETHTOOL_GRINGPARAM:
1349 rc = ethtool_get_ringparam(dev, useraddr);
1351 case ETHTOOL_SRINGPARAM:
1352 rc = ethtool_set_ringparam(dev, useraddr);
1354 case ETHTOOL_GPAUSEPARAM:
1355 rc = ethtool_get_pauseparam(dev, useraddr);
1357 case ETHTOOL_SPAUSEPARAM:
1358 rc = ethtool_set_pauseparam(dev, useraddr);
1360 case ETHTOOL_GRXCSUM:
1361 rc = ethtool_get_value(dev, useraddr, ethcmd,
1362 (dev->ethtool_ops->get_rx_csum ?
1363 dev->ethtool_ops->get_rx_csum :
1364 ethtool_op_get_rx_csum));
1366 case ETHTOOL_SRXCSUM:
1367 rc = ethtool_set_rx_csum(dev, useraddr);
1369 case ETHTOOL_GTXCSUM:
1370 rc = ethtool_get_value(dev, useraddr, ethcmd,
1371 (dev->ethtool_ops->get_tx_csum ?
1372 dev->ethtool_ops->get_tx_csum :
1373 ethtool_op_get_tx_csum));
1375 case ETHTOOL_STXCSUM:
1376 rc = ethtool_set_tx_csum(dev, useraddr);
1379 rc = ethtool_get_value(dev, useraddr, ethcmd,
1380 (dev->ethtool_ops->get_sg ?
1381 dev->ethtool_ops->get_sg :
1382 ethtool_op_get_sg));
1385 rc = ethtool_set_sg(dev, useraddr);
1388 rc = ethtool_get_value(dev, useraddr, ethcmd,
1389 (dev->ethtool_ops->get_tso ?
1390 dev->ethtool_ops->get_tso :
1391 ethtool_op_get_tso));
1394 rc = ethtool_set_tso(dev, useraddr);
1397 rc = ethtool_self_test(dev, useraddr);
1399 case ETHTOOL_GSTRINGS:
1400 rc = ethtool_get_strings(dev, useraddr);
1402 case ETHTOOL_PHYS_ID:
1403 rc = ethtool_phys_id(dev, useraddr);
1405 case ETHTOOL_GSTATS:
1406 rc = ethtool_get_stats(dev, useraddr);
1408 case ETHTOOL_GPERMADDR:
1409 rc = ethtool_get_perm_addr(dev, useraddr);
1412 rc = ethtool_get_value(dev, useraddr, ethcmd,
1413 (dev->ethtool_ops->get_ufo ?
1414 dev->ethtool_ops->get_ufo :
1415 ethtool_op_get_ufo));
1418 rc = ethtool_set_ufo(dev, useraddr);
1421 rc = ethtool_get_gso(dev, useraddr);
1424 rc = ethtool_set_gso(dev, useraddr);
1426 case ETHTOOL_GFLAGS:
1427 rc = ethtool_get_value(dev, useraddr, ethcmd,
1428 (dev->ethtool_ops->get_flags ?
1429 dev->ethtool_ops->get_flags :
1430 ethtool_op_get_flags));
1432 case ETHTOOL_SFLAGS:
1433 rc = ethtool_set_value(dev, useraddr,
1434 dev->ethtool_ops->set_flags);
1436 case ETHTOOL_GPFLAGS:
1437 rc = ethtool_get_value(dev, useraddr, ethcmd,
1438 dev->ethtool_ops->get_priv_flags);
1440 case ETHTOOL_SPFLAGS:
1441 rc = ethtool_set_value(dev, useraddr,
1442 dev->ethtool_ops->set_priv_flags);
1445 case ETHTOOL_GRXRINGS:
1446 case ETHTOOL_GRXCLSRLCNT:
1447 case ETHTOOL_GRXCLSRULE:
1448 case ETHTOOL_GRXCLSRLALL:
1449 rc = ethtool_get_rxnfc(dev, useraddr);
1452 case ETHTOOL_SRXCLSRLDEL:
1453 case ETHTOOL_SRXCLSRLINS:
1454 rc = ethtool_set_rxnfc(dev, useraddr);
1457 rc = ethtool_get_gro(dev, useraddr);
1460 rc = ethtool_set_gro(dev, useraddr);
1462 case ETHTOOL_FLASHDEV:
1463 rc = ethtool_flash_device(dev, useraddr);
1466 rc = ethtool_reset(dev, useraddr);
1468 case ETHTOOL_SRXNTUPLE:
1469 rc = ethtool_set_rx_ntuple(dev, useraddr);
1471 case ETHTOOL_GRXNTUPLE:
1472 rc = ethtool_get_rx_ntuple(dev, useraddr);
1478 if (dev->ethtool_ops->complete)
1479 dev->ethtool_ops->complete(dev);
1481 if (old_features != dev->features)
1482 netdev_features_change(dev);
1487 EXPORT_SYMBOL(ethtool_op_get_link);
1488 EXPORT_SYMBOL(ethtool_op_get_sg);
1489 EXPORT_SYMBOL(ethtool_op_get_tso);
1490 EXPORT_SYMBOL(ethtool_op_set_sg);
1491 EXPORT_SYMBOL(ethtool_op_set_tso);
1492 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1493 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1494 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1495 EXPORT_SYMBOL(ethtool_op_set_ufo);
1496 EXPORT_SYMBOL(ethtool_op_get_ufo);
1497 EXPORT_SYMBOL(ethtool_op_set_flags);
1498 EXPORT_SYMBOL(ethtool_op_get_flags);