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 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);
218 * this method of obtaining string set info is deprecated;
219 * consider using ETHTOOL_GSSET_INFO instead
221 if (ops->get_sset_count) {
224 rc = ops->get_sset_count(dev, ETH_SS_TEST);
226 info.testinfo_len = rc;
227 rc = ops->get_sset_count(dev, ETH_SS_STATS);
230 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
232 info.n_priv_flags = rc;
234 if (ops->get_regs_len)
235 info.regdump_len = ops->get_regs_len(dev);
236 if (ops->get_eeprom_len)
237 info.eedump_len = ops->get_eeprom_len(dev);
239 if (copy_to_user(useraddr, &info, sizeof(info)))
245 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
247 static noinline int ethtool_get_sset_info(struct net_device *dev,
248 void __user *useraddr)
250 struct ethtool_sset_info info;
251 const struct ethtool_ops *ops = dev->ethtool_ops;
253 int i, idx = 0, n_bits = 0, ret, rc;
254 u32 *info_buf = NULL;
256 if (!ops->get_sset_count)
259 if (copy_from_user(&info, useraddr, sizeof(info)))
262 /* store copy of mask, because we zero struct later on */
263 sset_mask = info.sset_mask;
267 /* calculate size of return buffer */
268 for (i = 0; i < 64; i++)
269 if (sset_mask & (1ULL << i))
272 memset(&info, 0, sizeof(info));
273 info.cmd = ETHTOOL_GSSET_INFO;
275 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
280 * fill return buffer based on input bitmask and successful
281 * get_sset_count return
283 for (i = 0; i < 64; i++) {
284 if (!(sset_mask & (1ULL << i)))
287 rc = ops->get_sset_count(dev, i);
289 info.sset_mask |= (1ULL << i);
290 info_buf[idx++] = rc;
295 if (copy_to_user(useraddr, &info, sizeof(info)))
298 useraddr += offsetof(struct ethtool_sset_info, data);
299 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
310 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
312 static noinline int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
314 struct ethtool_rxnfc cmd;
316 if (!dev->ethtool_ops->set_rxnfc)
319 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
322 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
326 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
328 static noinline int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
330 struct ethtool_rxnfc info;
331 const struct ethtool_ops *ops = dev->ethtool_ops;
333 void *rule_buf = NULL;
338 if (copy_from_user(&info, useraddr, sizeof(info)))
341 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
342 if (info.rule_cnt > 0) {
343 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
350 ret = ops->get_rxnfc(dev, &info, rule_buf);
355 if (copy_to_user(useraddr, &info, sizeof(info)))
359 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
360 if (copy_to_user(useraddr, rule_buf,
361 info.rule_cnt * sizeof(u32)))
372 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
373 struct ethtool_rx_ntuple_flow_spec *spec,
374 struct ethtool_rx_ntuple_flow_spec_container *fsc)
377 /* don't add filters forever */
378 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
379 /* free the container */
384 /* Copy the whole filter over */
385 fsc->fs.flow_type = spec->flow_type;
386 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
387 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
389 fsc->fs.vlan_tag = spec->vlan_tag;
390 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
391 fsc->fs.data = spec->data;
392 fsc->fs.data_mask = spec->data_mask;
393 fsc->fs.action = spec->action;
395 /* add to the list */
396 list_add_tail_rcu(&fsc->list, &list->list);
401 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
403 static noinline int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
405 struct ethtool_rx_ntuple cmd;
406 const struct ethtool_ops *ops = dev->ethtool_ops;
407 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
410 if (!(dev->features & NETIF_F_NTUPLE))
413 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
417 * Cache filter in dev struct for GET operation only if
418 * the underlying driver doesn't have its own GET operation, and
419 * only if the filter was added successfully. First make sure we
420 * can allocate the filter, then continue if successful.
422 if (!ops->get_rx_ntuple) {
423 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
428 ret = ops->set_rx_ntuple(dev, &cmd);
434 if (!ops->get_rx_ntuple)
435 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
440 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
442 struct ethtool_gstrings gstrings;
443 const struct ethtool_ops *ops = dev->ethtool_ops;
444 struct ethtool_rx_ntuple_flow_spec_container *fsc;
447 int ret, i, num_strings = 0;
449 if (!ops->get_sset_count)
452 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
455 ret = ops->get_sset_count(dev, gstrings.string_set);
461 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
465 if (ops->get_rx_ntuple) {
466 /* driver-specific filter grab */
467 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
471 /* default ethtool filter grab */
474 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
475 sprintf(p, "Filter %d:\n", i);
476 p += ETH_GSTRING_LEN;
479 switch (fsc->fs.flow_type) {
481 sprintf(p, "\tFlow Type: TCP\n");
482 p += ETH_GSTRING_LEN;
486 sprintf(p, "\tFlow Type: UDP\n");
487 p += ETH_GSTRING_LEN;
491 sprintf(p, "\tFlow Type: SCTP\n");
492 p += ETH_GSTRING_LEN;
496 sprintf(p, "\tFlow Type: AH ESP\n");
497 p += ETH_GSTRING_LEN;
501 sprintf(p, "\tFlow Type: ESP\n");
502 p += ETH_GSTRING_LEN;
506 sprintf(p, "\tFlow Type: Raw IP\n");
507 p += ETH_GSTRING_LEN;
511 sprintf(p, "\tFlow Type: IPv4\n");
512 p += ETH_GSTRING_LEN;
516 sprintf(p, "\tFlow Type: Unknown\n");
517 p += ETH_GSTRING_LEN;
522 /* now the rest of the filters */
523 switch (fsc->fs.flow_type) {
527 sprintf(p, "\tSrc IP addr: 0x%x\n",
528 fsc->fs.h_u.tcp_ip4_spec.ip4src);
529 p += ETH_GSTRING_LEN;
531 sprintf(p, "\tSrc IP mask: 0x%x\n",
532 fsc->fs.m_u.tcp_ip4_spec.ip4src);
533 p += ETH_GSTRING_LEN;
535 sprintf(p, "\tDest IP addr: 0x%x\n",
536 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
537 p += ETH_GSTRING_LEN;
539 sprintf(p, "\tDest IP mask: 0x%x\n",
540 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
541 p += ETH_GSTRING_LEN;
543 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
544 fsc->fs.h_u.tcp_ip4_spec.psrc,
545 fsc->fs.m_u.tcp_ip4_spec.psrc);
546 p += ETH_GSTRING_LEN;
548 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
549 fsc->fs.h_u.tcp_ip4_spec.pdst,
550 fsc->fs.m_u.tcp_ip4_spec.pdst);
551 p += ETH_GSTRING_LEN;
553 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
554 fsc->fs.h_u.tcp_ip4_spec.tos,
555 fsc->fs.m_u.tcp_ip4_spec.tos);
556 p += ETH_GSTRING_LEN;
561 sprintf(p, "\tSrc IP addr: 0x%x\n",
562 fsc->fs.h_u.ah_ip4_spec.ip4src);
563 p += ETH_GSTRING_LEN;
565 sprintf(p, "\tSrc IP mask: 0x%x\n",
566 fsc->fs.m_u.ah_ip4_spec.ip4src);
567 p += ETH_GSTRING_LEN;
569 sprintf(p, "\tDest IP addr: 0x%x\n",
570 fsc->fs.h_u.ah_ip4_spec.ip4dst);
571 p += ETH_GSTRING_LEN;
573 sprintf(p, "\tDest IP mask: 0x%x\n",
574 fsc->fs.m_u.ah_ip4_spec.ip4dst);
575 p += ETH_GSTRING_LEN;
577 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
578 fsc->fs.h_u.ah_ip4_spec.spi,
579 fsc->fs.m_u.ah_ip4_spec.spi);
580 p += ETH_GSTRING_LEN;
582 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
583 fsc->fs.h_u.ah_ip4_spec.tos,
584 fsc->fs.m_u.ah_ip4_spec.tos);
585 p += ETH_GSTRING_LEN;
589 sprintf(p, "\tSrc IP addr: 0x%x\n",
590 fsc->fs.h_u.raw_ip4_spec.ip4src);
591 p += ETH_GSTRING_LEN;
593 sprintf(p, "\tSrc IP mask: 0x%x\n",
594 fsc->fs.m_u.raw_ip4_spec.ip4src);
595 p += ETH_GSTRING_LEN;
597 sprintf(p, "\tDest IP addr: 0x%x\n",
598 fsc->fs.h_u.raw_ip4_spec.ip4dst);
599 p += ETH_GSTRING_LEN;
601 sprintf(p, "\tDest IP mask: 0x%x\n",
602 fsc->fs.m_u.raw_ip4_spec.ip4dst);
603 p += ETH_GSTRING_LEN;
607 sprintf(p, "\tSrc IP addr: 0x%x\n",
608 fsc->fs.h_u.usr_ip4_spec.ip4src);
609 p += ETH_GSTRING_LEN;
611 sprintf(p, "\tSrc IP mask: 0x%x\n",
612 fsc->fs.m_u.usr_ip4_spec.ip4src);
613 p += ETH_GSTRING_LEN;
615 sprintf(p, "\tDest IP addr: 0x%x\n",
616 fsc->fs.h_u.usr_ip4_spec.ip4dst);
617 p += ETH_GSTRING_LEN;
619 sprintf(p, "\tDest IP mask: 0x%x\n",
620 fsc->fs.m_u.usr_ip4_spec.ip4dst);
621 p += ETH_GSTRING_LEN;
623 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
624 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
625 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
626 p += ETH_GSTRING_LEN;
628 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
629 fsc->fs.h_u.usr_ip4_spec.tos,
630 fsc->fs.m_u.usr_ip4_spec.tos);
631 p += ETH_GSTRING_LEN;
633 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
634 fsc->fs.h_u.usr_ip4_spec.ip_ver,
635 fsc->fs.m_u.usr_ip4_spec.ip_ver);
636 p += ETH_GSTRING_LEN;
638 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
639 fsc->fs.h_u.usr_ip4_spec.proto,
640 fsc->fs.m_u.usr_ip4_spec.proto);
641 p += ETH_GSTRING_LEN;
645 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
646 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
647 p += ETH_GSTRING_LEN;
649 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
650 p += ETH_GSTRING_LEN;
652 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
653 p += ETH_GSTRING_LEN;
655 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
656 sprintf(p, "\tAction: Drop\n");
658 sprintf(p, "\tAction: Direct to queue %d\n",
660 p += ETH_GSTRING_LEN;
666 /* indicate to userspace how many strings we actually have */
667 gstrings.len = num_strings;
669 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
671 useraddr += sizeof(gstrings);
672 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
681 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
683 struct ethtool_regs regs;
684 const struct ethtool_ops *ops = dev->ethtool_ops;
688 if (!ops->get_regs || !ops->get_regs_len)
691 if (copy_from_user(®s, useraddr, sizeof(regs)))
694 reglen = ops->get_regs_len(dev);
695 if (regs.len > reglen)
698 regbuf = kmalloc(reglen, GFP_USER);
702 ops->get_regs(dev, ®s, regbuf);
705 if (copy_to_user(useraddr, ®s, sizeof(regs)))
707 useraddr += offsetof(struct ethtool_regs, data);
708 if (copy_to_user(useraddr, regbuf, regs.len))
717 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
719 struct ethtool_value reset;
722 if (!dev->ethtool_ops->reset)
725 if (copy_from_user(&reset, useraddr, sizeof(reset)))
728 ret = dev->ethtool_ops->reset(dev, &reset.data);
732 if (copy_to_user(useraddr, &reset, sizeof(reset)))
737 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
739 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
741 if (!dev->ethtool_ops->get_wol)
744 dev->ethtool_ops->get_wol(dev, &wol);
746 if (copy_to_user(useraddr, &wol, sizeof(wol)))
751 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
753 struct ethtool_wolinfo wol;
755 if (!dev->ethtool_ops->set_wol)
758 if (copy_from_user(&wol, useraddr, sizeof(wol)))
761 return dev->ethtool_ops->set_wol(dev, &wol);
764 static int ethtool_nway_reset(struct net_device *dev)
766 if (!dev->ethtool_ops->nway_reset)
769 return dev->ethtool_ops->nway_reset(dev);
772 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
774 struct ethtool_eeprom eeprom;
775 const struct ethtool_ops *ops = dev->ethtool_ops;
776 void __user *userbuf = useraddr + sizeof(eeprom);
781 if (!ops->get_eeprom || !ops->get_eeprom_len)
784 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
787 /* Check for wrap and zero */
788 if (eeprom.offset + eeprom.len <= eeprom.offset)
791 /* Check for exceeding total eeprom len */
792 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
795 data = kmalloc(PAGE_SIZE, GFP_USER);
799 bytes_remaining = eeprom.len;
800 while (bytes_remaining > 0) {
801 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
803 ret = ops->get_eeprom(dev, &eeprom, data);
806 if (copy_to_user(userbuf, data, eeprom.len)) {
810 userbuf += eeprom.len;
811 eeprom.offset += eeprom.len;
812 bytes_remaining -= eeprom.len;
815 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
816 eeprom.offset -= eeprom.len;
817 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
824 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
826 struct ethtool_eeprom eeprom;
827 const struct ethtool_ops *ops = dev->ethtool_ops;
828 void __user *userbuf = useraddr + sizeof(eeprom);
833 if (!ops->set_eeprom || !ops->get_eeprom_len)
836 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
839 /* Check for wrap and zero */
840 if (eeprom.offset + eeprom.len <= eeprom.offset)
843 /* Check for exceeding total eeprom len */
844 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
847 data = kmalloc(PAGE_SIZE, GFP_USER);
851 bytes_remaining = eeprom.len;
852 while (bytes_remaining > 0) {
853 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
855 if (copy_from_user(data, userbuf, eeprom.len)) {
859 ret = ops->set_eeprom(dev, &eeprom, data);
862 userbuf += eeprom.len;
863 eeprom.offset += eeprom.len;
864 bytes_remaining -= eeprom.len;
872 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
874 static noinline int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
876 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
878 if (!dev->ethtool_ops->get_coalesce)
881 dev->ethtool_ops->get_coalesce(dev, &coalesce);
883 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
889 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
891 static noinline int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
893 struct ethtool_coalesce coalesce;
895 if (!dev->ethtool_ops->set_coalesce)
898 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
901 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
904 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
906 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
908 if (!dev->ethtool_ops->get_ringparam)
911 dev->ethtool_ops->get_ringparam(dev, &ringparam);
913 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
918 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
920 struct ethtool_ringparam ringparam;
922 if (!dev->ethtool_ops->set_ringparam)
925 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
928 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
931 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
933 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
935 if (!dev->ethtool_ops->get_pauseparam)
938 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
940 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
945 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
947 struct ethtool_pauseparam pauseparam;
949 if (!dev->ethtool_ops->set_pauseparam)
952 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
955 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
958 static int __ethtool_set_sg(struct net_device *dev, u32 data)
962 if (!data && dev->ethtool_ops->set_tso) {
963 err = dev->ethtool_ops->set_tso(dev, 0);
968 if (!data && dev->ethtool_ops->set_ufo) {
969 err = dev->ethtool_ops->set_ufo(dev, 0);
973 return dev->ethtool_ops->set_sg(dev, data);
976 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
978 struct ethtool_value edata;
981 if (!dev->ethtool_ops->set_tx_csum)
984 if (copy_from_user(&edata, useraddr, sizeof(edata)))
987 if (!edata.data && dev->ethtool_ops->set_sg) {
988 err = __ethtool_set_sg(dev, 0);
993 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
996 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
998 struct ethtool_value edata;
1000 if (!dev->ethtool_ops->set_rx_csum)
1003 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1006 if (!edata.data && dev->ethtool_ops->set_sg)
1007 dev->features &= ~NETIF_F_GRO;
1009 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1012 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1014 struct ethtool_value edata;
1016 if (!dev->ethtool_ops->set_sg)
1019 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1023 !(dev->features & NETIF_F_ALL_CSUM))
1026 return __ethtool_set_sg(dev, edata.data);
1029 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1031 struct ethtool_value edata;
1033 if (!dev->ethtool_ops->set_tso)
1036 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1039 if (edata.data && !(dev->features & NETIF_F_SG))
1042 return dev->ethtool_ops->set_tso(dev, edata.data);
1045 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1047 struct ethtool_value edata;
1049 if (!dev->ethtool_ops->set_ufo)
1051 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1053 if (edata.data && !(dev->features & NETIF_F_SG))
1055 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1057 return dev->ethtool_ops->set_ufo(dev, edata.data);
1060 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1062 struct ethtool_value edata = { ETHTOOL_GGSO };
1064 edata.data = dev->features & NETIF_F_GSO;
1065 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1070 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1072 struct ethtool_value edata;
1074 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1077 dev->features |= NETIF_F_GSO;
1079 dev->features &= ~NETIF_F_GSO;
1083 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1085 struct ethtool_value edata = { ETHTOOL_GGRO };
1087 edata.data = dev->features & NETIF_F_GRO;
1088 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1093 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1095 struct ethtool_value edata;
1097 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1101 if (!dev->ethtool_ops->get_rx_csum ||
1102 !dev->ethtool_ops->get_rx_csum(dev))
1104 dev->features |= NETIF_F_GRO;
1106 dev->features &= ~NETIF_F_GRO;
1111 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1113 struct ethtool_test test;
1114 const struct ethtool_ops *ops = dev->ethtool_ops;
1118 if (!ops->self_test || !ops->get_sset_count)
1121 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1124 WARN_ON(test_len == 0);
1126 if (copy_from_user(&test, useraddr, sizeof(test)))
1129 test.len = test_len;
1130 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1134 ops->self_test(dev, &test, data);
1137 if (copy_to_user(useraddr, &test, sizeof(test)))
1139 useraddr += sizeof(test);
1140 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1149 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1151 struct ethtool_gstrings gstrings;
1152 const struct ethtool_ops *ops = dev->ethtool_ops;
1156 if (!ops->get_strings || !ops->get_sset_count)
1159 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1162 ret = ops->get_sset_count(dev, gstrings.string_set);
1168 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1172 ops->get_strings(dev, gstrings.string_set, data);
1175 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1177 useraddr += sizeof(gstrings);
1178 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1187 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1189 struct ethtool_value id;
1191 if (!dev->ethtool_ops->phys_id)
1194 if (copy_from_user(&id, useraddr, sizeof(id)))
1197 return dev->ethtool_ops->phys_id(dev, id.data);
1200 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1202 struct ethtool_stats stats;
1203 const struct ethtool_ops *ops = dev->ethtool_ops;
1207 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1210 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1213 WARN_ON(n_stats == 0);
1215 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1218 stats.n_stats = n_stats;
1219 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1223 ops->get_ethtool_stats(dev, &stats, data);
1226 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1228 useraddr += sizeof(stats);
1229 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1238 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1240 struct ethtool_perm_addr epaddr;
1242 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1245 if (epaddr.size < dev->addr_len)
1247 epaddr.size = dev->addr_len;
1249 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1251 useraddr += sizeof(epaddr);
1252 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1257 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1258 u32 cmd, u32 (*actor)(struct net_device *))
1260 struct ethtool_value edata = { .cmd = cmd };
1265 edata.data = actor(dev);
1267 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1272 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1273 void (*actor)(struct net_device *, u32))
1275 struct ethtool_value edata;
1280 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1283 actor(dev, edata.data);
1287 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1288 int (*actor)(struct net_device *, u32))
1290 struct ethtool_value edata;
1295 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1298 return actor(dev, edata.data);
1302 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
1304 static noinline int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1306 struct ethtool_flash efl;
1308 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1311 if (!dev->ethtool_ops->flash_device)
1314 return dev->ethtool_ops->flash_device(dev, &efl);
1317 /* The main entry point in this file. Called from net/core/dev.c */
1319 int dev_ethtool(struct net *net, struct ifreq *ifr)
1321 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1322 void __user *useraddr = ifr->ifr_data;
1325 unsigned long old_features;
1327 if (!dev || !netif_device_present(dev))
1330 if (!dev->ethtool_ops)
1333 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1336 /* Allow some commands to be done by anyone */
1338 case ETHTOOL_GDRVINFO:
1339 case ETHTOOL_GMSGLVL:
1340 case ETHTOOL_GCOALESCE:
1341 case ETHTOOL_GRINGPARAM:
1342 case ETHTOOL_GPAUSEPARAM:
1343 case ETHTOOL_GRXCSUM:
1344 case ETHTOOL_GTXCSUM:
1346 case ETHTOOL_GSTRINGS:
1348 case ETHTOOL_GPERMADDR:
1352 case ETHTOOL_GFLAGS:
1353 case ETHTOOL_GPFLAGS:
1355 case ETHTOOL_GRXRINGS:
1356 case ETHTOOL_GRXCLSRLCNT:
1357 case ETHTOOL_GRXCLSRULE:
1358 case ETHTOOL_GRXCLSRLALL:
1361 if (!capable(CAP_NET_ADMIN))
1365 if (dev->ethtool_ops->begin)
1366 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1369 old_features = dev->features;
1373 rc = ethtool_get_settings(dev, useraddr);
1376 rc = ethtool_set_settings(dev, useraddr);
1378 case ETHTOOL_GDRVINFO:
1379 rc = ethtool_get_drvinfo(dev, useraddr);
1382 rc = ethtool_get_regs(dev, useraddr);
1385 rc = ethtool_get_wol(dev, useraddr);
1388 rc = ethtool_set_wol(dev, useraddr);
1390 case ETHTOOL_GMSGLVL:
1391 rc = ethtool_get_value(dev, useraddr, ethcmd,
1392 dev->ethtool_ops->get_msglevel);
1394 case ETHTOOL_SMSGLVL:
1395 rc = ethtool_set_value_void(dev, useraddr,
1396 dev->ethtool_ops->set_msglevel);
1398 case ETHTOOL_NWAY_RST:
1399 rc = ethtool_nway_reset(dev);
1402 rc = ethtool_get_value(dev, useraddr, ethcmd,
1403 dev->ethtool_ops->get_link);
1405 case ETHTOOL_GEEPROM:
1406 rc = ethtool_get_eeprom(dev, useraddr);
1408 case ETHTOOL_SEEPROM:
1409 rc = ethtool_set_eeprom(dev, useraddr);
1411 case ETHTOOL_GCOALESCE:
1412 rc = ethtool_get_coalesce(dev, useraddr);
1414 case ETHTOOL_SCOALESCE:
1415 rc = ethtool_set_coalesce(dev, useraddr);
1417 case ETHTOOL_GRINGPARAM:
1418 rc = ethtool_get_ringparam(dev, useraddr);
1420 case ETHTOOL_SRINGPARAM:
1421 rc = ethtool_set_ringparam(dev, useraddr);
1423 case ETHTOOL_GPAUSEPARAM:
1424 rc = ethtool_get_pauseparam(dev, useraddr);
1426 case ETHTOOL_SPAUSEPARAM:
1427 rc = ethtool_set_pauseparam(dev, useraddr);
1429 case ETHTOOL_GRXCSUM:
1430 rc = ethtool_get_value(dev, useraddr, ethcmd,
1431 (dev->ethtool_ops->get_rx_csum ?
1432 dev->ethtool_ops->get_rx_csum :
1433 ethtool_op_get_rx_csum));
1435 case ETHTOOL_SRXCSUM:
1436 rc = ethtool_set_rx_csum(dev, useraddr);
1438 case ETHTOOL_GTXCSUM:
1439 rc = ethtool_get_value(dev, useraddr, ethcmd,
1440 (dev->ethtool_ops->get_tx_csum ?
1441 dev->ethtool_ops->get_tx_csum :
1442 ethtool_op_get_tx_csum));
1444 case ETHTOOL_STXCSUM:
1445 rc = ethtool_set_tx_csum(dev, useraddr);
1448 rc = ethtool_get_value(dev, useraddr, ethcmd,
1449 (dev->ethtool_ops->get_sg ?
1450 dev->ethtool_ops->get_sg :
1451 ethtool_op_get_sg));
1454 rc = ethtool_set_sg(dev, useraddr);
1457 rc = ethtool_get_value(dev, useraddr, ethcmd,
1458 (dev->ethtool_ops->get_tso ?
1459 dev->ethtool_ops->get_tso :
1460 ethtool_op_get_tso));
1463 rc = ethtool_set_tso(dev, useraddr);
1466 rc = ethtool_self_test(dev, useraddr);
1468 case ETHTOOL_GSTRINGS:
1469 rc = ethtool_get_strings(dev, useraddr);
1471 case ETHTOOL_PHYS_ID:
1472 rc = ethtool_phys_id(dev, useraddr);
1474 case ETHTOOL_GSTATS:
1475 rc = ethtool_get_stats(dev, useraddr);
1477 case ETHTOOL_GPERMADDR:
1478 rc = ethtool_get_perm_addr(dev, useraddr);
1481 rc = ethtool_get_value(dev, useraddr, ethcmd,
1482 (dev->ethtool_ops->get_ufo ?
1483 dev->ethtool_ops->get_ufo :
1484 ethtool_op_get_ufo));
1487 rc = ethtool_set_ufo(dev, useraddr);
1490 rc = ethtool_get_gso(dev, useraddr);
1493 rc = ethtool_set_gso(dev, useraddr);
1495 case ETHTOOL_GFLAGS:
1496 rc = ethtool_get_value(dev, useraddr, ethcmd,
1497 (dev->ethtool_ops->get_flags ?
1498 dev->ethtool_ops->get_flags :
1499 ethtool_op_get_flags));
1501 case ETHTOOL_SFLAGS:
1502 rc = ethtool_set_value(dev, useraddr,
1503 dev->ethtool_ops->set_flags);
1505 case ETHTOOL_GPFLAGS:
1506 rc = ethtool_get_value(dev, useraddr, ethcmd,
1507 dev->ethtool_ops->get_priv_flags);
1509 case ETHTOOL_SPFLAGS:
1510 rc = ethtool_set_value(dev, useraddr,
1511 dev->ethtool_ops->set_priv_flags);
1514 case ETHTOOL_GRXRINGS:
1515 case ETHTOOL_GRXCLSRLCNT:
1516 case ETHTOOL_GRXCLSRULE:
1517 case ETHTOOL_GRXCLSRLALL:
1518 rc = ethtool_get_rxnfc(dev, useraddr);
1521 case ETHTOOL_SRXCLSRLDEL:
1522 case ETHTOOL_SRXCLSRLINS:
1523 rc = ethtool_set_rxnfc(dev, useraddr);
1526 rc = ethtool_get_gro(dev, useraddr);
1529 rc = ethtool_set_gro(dev, useraddr);
1531 case ETHTOOL_FLASHDEV:
1532 rc = ethtool_flash_device(dev, useraddr);
1535 rc = ethtool_reset(dev, useraddr);
1537 case ETHTOOL_SRXNTUPLE:
1538 rc = ethtool_set_rx_ntuple(dev, useraddr);
1540 case ETHTOOL_GRXNTUPLE:
1541 rc = ethtool_get_rx_ntuple(dev, useraddr);
1543 case ETHTOOL_GSSET_INFO:
1544 rc = ethtool_get_sset_info(dev, useraddr);
1550 if (dev->ethtool_ops->complete)
1551 dev->ethtool_ops->complete(dev);
1553 if (old_features != dev->features)
1554 netdev_features_change(dev);
1559 EXPORT_SYMBOL(ethtool_op_get_link);
1560 EXPORT_SYMBOL(ethtool_op_get_sg);
1561 EXPORT_SYMBOL(ethtool_op_get_tso);
1562 EXPORT_SYMBOL(ethtool_op_set_sg);
1563 EXPORT_SYMBOL(ethtool_op_set_tso);
1564 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1565 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1566 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1567 EXPORT_SYMBOL(ethtool_op_set_ufo);
1568 EXPORT_SYMBOL(ethtool_op_get_ufo);
1569 EXPORT_SYMBOL(ethtool_op_set_flags);
1570 EXPORT_SYMBOL(ethtool_op_get_flags);