2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
26 * Some useful ethtool_ops methods that're device independent.
27 * If we find that all drivers want to do the same thing here,
28 * we can turn these into dev_() function calls.
31 u32 ethtool_op_get_link(struct net_device *dev)
33 return netif_carrier_ok(dev) ? 1 : 0;
35 EXPORT_SYMBOL(ethtool_op_get_link);
37 u32 ethtool_op_get_rx_csum(struct net_device *dev)
39 return (dev->features & NETIF_F_ALL_CSUM) != 0;
41 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
43 u32 ethtool_op_get_tx_csum(struct net_device *dev)
45 return (dev->features & NETIF_F_ALL_CSUM) != 0;
47 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
49 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
52 dev->features |= NETIF_F_IP_CSUM;
54 dev->features &= ~NETIF_F_IP_CSUM;
58 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
60 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
63 dev->features |= NETIF_F_HW_CSUM;
65 dev->features &= ~NETIF_F_HW_CSUM;
69 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
71 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
74 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
76 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
80 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
82 u32 ethtool_op_get_sg(struct net_device *dev)
84 return (dev->features & NETIF_F_SG) != 0;
86 EXPORT_SYMBOL(ethtool_op_get_sg);
88 int ethtool_op_set_sg(struct net_device *dev, u32 data)
91 dev->features |= NETIF_F_SG;
93 dev->features &= ~NETIF_F_SG;
97 EXPORT_SYMBOL(ethtool_op_set_sg);
99 u32 ethtool_op_get_tso(struct net_device *dev)
101 return (dev->features & NETIF_F_TSO) != 0;
103 EXPORT_SYMBOL(ethtool_op_get_tso);
105 int ethtool_op_set_tso(struct net_device *dev, u32 data)
108 dev->features |= NETIF_F_TSO;
110 dev->features &= ~NETIF_F_TSO;
114 EXPORT_SYMBOL(ethtool_op_set_tso);
116 u32 ethtool_op_get_ufo(struct net_device *dev)
118 return (dev->features & NETIF_F_UFO) != 0;
120 EXPORT_SYMBOL(ethtool_op_get_ufo);
122 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
125 dev->features |= NETIF_F_UFO;
127 dev->features &= ~NETIF_F_UFO;
130 EXPORT_SYMBOL(ethtool_op_set_ufo);
132 /* the following list of flags are the same as their associated
133 * NETIF_F_xxx values in include/linux/netdevice.h
135 static const u32 flags_dup_features =
136 (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
139 u32 ethtool_op_get_flags(struct net_device *dev)
141 /* in the future, this function will probably contain additional
142 * handling for flags which are not so easily handled
143 * by a simple masking operation
146 return dev->features & flags_dup_features;
148 EXPORT_SYMBOL(ethtool_op_get_flags);
150 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
152 if (data & ~supported)
155 dev->features = ((dev->features & ~flags_dup_features) |
156 (data & flags_dup_features));
159 EXPORT_SYMBOL(ethtool_op_set_flags);
161 void ethtool_ntuple_flush(struct net_device *dev)
163 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
165 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
166 list_del(&fsc->list);
169 dev->ethtool_ntuple_list.count = 0;
171 EXPORT_SYMBOL(ethtool_ntuple_flush);
173 /* Handlers for each ethtool command */
175 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
177 const struct ethtool_ops *ops = dev->ethtool_ops;
179 if (ops && ops->get_sset_count && ops->get_strings)
180 return ops->get_sset_count(dev, sset);
185 static void __ethtool_get_strings(struct net_device *dev,
186 u32 stringset, u8 *data)
188 const struct ethtool_ops *ops = dev->ethtool_ops;
190 /* ops->get_strings is valid because checked earlier */
191 ops->get_strings(dev, stringset, data);
194 static u32 ethtool_get_feature_mask(u32 eth_cmd)
196 /* feature masks of legacy discrete ethtool ops */
199 case ETHTOOL_GTXCSUM:
200 case ETHTOOL_STXCSUM:
201 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
207 return NETIF_F_ALL_TSO;
222 static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
224 const struct ethtool_ops *ops = dev->ethtool_ops;
230 case ETHTOOL_GTXCSUM:
231 return ops->get_tx_csum;
243 static int ethtool_get_one_feature(struct net_device *dev,
244 char __user *useraddr, u32 ethcmd)
246 struct ethtool_value edata = {
248 .data = !!(dev->features & ethtool_get_feature_mask(ethcmd)),
250 u32 (*actor)(struct net_device *);
252 actor = __ethtool_get_one_feature_actor(dev, ethcmd);
254 edata.data = actor(dev);
256 if (copy_to_user(useraddr, &edata, sizeof(edata)))
261 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
262 static int __ethtool_set_sg(struct net_device *dev, u32 data);
263 static int __ethtool_set_tso(struct net_device *dev, u32 data);
264 static int __ethtool_set_ufo(struct net_device *dev, u32 data);
266 static int ethtool_set_one_feature(struct net_device *dev,
267 void __user *useraddr, u32 ethcmd)
269 struct ethtool_value edata;
272 if (copy_from_user(&edata, useraddr, sizeof(edata)))
276 case ETHTOOL_STXCSUM:
277 return __ethtool_set_tx_csum(dev, edata.data);
279 return __ethtool_set_sg(dev, edata.data);
281 return __ethtool_set_tso(dev, edata.data);
283 return __ethtool_set_ufo(dev, edata.data);
286 mask = ethtool_get_feature_mask(ethcmd);
288 dev->features |= mask;
290 dev->features &= ~mask;
297 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
299 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
302 if (!dev->ethtool_ops->get_settings)
305 err = dev->ethtool_ops->get_settings(dev, &cmd);
309 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
314 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
316 struct ethtool_cmd cmd;
318 if (!dev->ethtool_ops->set_settings)
321 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
324 return dev->ethtool_ops->set_settings(dev, &cmd);
327 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
328 void __user *useraddr)
330 struct ethtool_drvinfo info;
331 const struct ethtool_ops *ops = dev->ethtool_ops;
333 memset(&info, 0, sizeof(info));
334 info.cmd = ETHTOOL_GDRVINFO;
335 if (ops && ops->get_drvinfo) {
336 ops->get_drvinfo(dev, &info);
337 } else if (dev->dev.parent && dev->dev.parent->driver) {
338 strlcpy(info.bus_info, dev_name(dev->dev.parent),
339 sizeof(info.bus_info));
340 strlcpy(info.driver, dev->dev.parent->driver->name,
341 sizeof(info.driver));
347 * this method of obtaining string set info is deprecated;
348 * Use ETHTOOL_GSSET_INFO instead.
350 if (ops && ops->get_sset_count) {
353 rc = ops->get_sset_count(dev, ETH_SS_TEST);
355 info.testinfo_len = rc;
356 rc = ops->get_sset_count(dev, ETH_SS_STATS);
359 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
361 info.n_priv_flags = rc;
363 if (ops && ops->get_regs_len)
364 info.regdump_len = ops->get_regs_len(dev);
365 if (ops && ops->get_eeprom_len)
366 info.eedump_len = ops->get_eeprom_len(dev);
368 if (copy_to_user(useraddr, &info, sizeof(info)))
373 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
374 void __user *useraddr)
376 struct ethtool_sset_info info;
378 int i, idx = 0, n_bits = 0, ret, rc;
379 u32 *info_buf = NULL;
381 if (copy_from_user(&info, useraddr, sizeof(info)))
384 /* store copy of mask, because we zero struct later on */
385 sset_mask = info.sset_mask;
389 /* calculate size of return buffer */
390 n_bits = hweight64(sset_mask);
392 memset(&info, 0, sizeof(info));
393 info.cmd = ETHTOOL_GSSET_INFO;
395 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
400 * fill return buffer based on input bitmask and successful
401 * get_sset_count return
403 for (i = 0; i < 64; i++) {
404 if (!(sset_mask & (1ULL << i)))
407 rc = __ethtool_get_sset_count(dev, i);
409 info.sset_mask |= (1ULL << i);
410 info_buf[idx++] = rc;
415 if (copy_to_user(useraddr, &info, sizeof(info)))
418 useraddr += offsetof(struct ethtool_sset_info, data);
419 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
429 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
430 u32 cmd, void __user *useraddr)
432 struct ethtool_rxnfc info;
433 size_t info_size = sizeof(info);
435 if (!dev->ethtool_ops->set_rxnfc)
438 /* struct ethtool_rxnfc was originally defined for
439 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
440 * members. User-space might still be using that
442 if (cmd == ETHTOOL_SRXFH)
443 info_size = (offsetof(struct ethtool_rxnfc, data) +
446 if (copy_from_user(&info, useraddr, info_size))
449 return dev->ethtool_ops->set_rxnfc(dev, &info);
452 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
453 u32 cmd, void __user *useraddr)
455 struct ethtool_rxnfc info;
456 size_t info_size = sizeof(info);
457 const struct ethtool_ops *ops = dev->ethtool_ops;
459 void *rule_buf = NULL;
464 /* struct ethtool_rxnfc was originally defined for
465 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
466 * members. User-space might still be using that
468 if (cmd == ETHTOOL_GRXFH)
469 info_size = (offsetof(struct ethtool_rxnfc, data) +
472 if (copy_from_user(&info, useraddr, info_size))
475 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
476 if (info.rule_cnt > 0) {
477 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
478 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
485 ret = ops->get_rxnfc(dev, &info, rule_buf);
490 if (copy_to_user(useraddr, &info, info_size))
494 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
495 if (copy_to_user(useraddr, rule_buf,
496 info.rule_cnt * sizeof(u32)))
507 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
508 void __user *useraddr)
510 struct ethtool_rxfh_indir *indir;
515 if (!dev->ethtool_ops->get_rxfh_indir)
518 if (copy_from_user(&table_size,
519 useraddr + offsetof(struct ethtool_rxfh_indir, size),
524 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
526 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
527 indir = kzalloc(full_size, GFP_USER);
531 indir->cmd = ETHTOOL_GRXFHINDIR;
532 indir->size = table_size;
533 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
537 if (copy_to_user(useraddr, indir, full_size))
545 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
546 void __user *useraddr)
548 struct ethtool_rxfh_indir *indir;
553 if (!dev->ethtool_ops->set_rxfh_indir)
556 if (copy_from_user(&table_size,
557 useraddr + offsetof(struct ethtool_rxfh_indir, size),
562 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
564 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
565 indir = kmalloc(full_size, GFP_USER);
569 if (copy_from_user(indir, useraddr, full_size)) {
574 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
581 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
582 struct ethtool_rx_ntuple_flow_spec *spec,
583 struct ethtool_rx_ntuple_flow_spec_container *fsc)
586 /* don't add filters forever */
587 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
588 /* free the container */
593 /* Copy the whole filter over */
594 fsc->fs.flow_type = spec->flow_type;
595 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
596 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
598 fsc->fs.vlan_tag = spec->vlan_tag;
599 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
600 fsc->fs.data = spec->data;
601 fsc->fs.data_mask = spec->data_mask;
602 fsc->fs.action = spec->action;
604 /* add to the list */
605 list_add_tail_rcu(&fsc->list, &list->list);
610 * ethtool does not (or did not) set masks for flow parameters that are
611 * not specified, so if both value and mask are 0 then this must be
612 * treated as equivalent to a mask with all bits set. Implement that
613 * here rather than in drivers.
615 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
617 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
618 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
620 if (fs->flow_type != TCP_V4_FLOW &&
621 fs->flow_type != UDP_V4_FLOW &&
622 fs->flow_type != SCTP_V4_FLOW)
625 if (!(entry->ip4src | mask->ip4src))
626 mask->ip4src = htonl(0xffffffff);
627 if (!(entry->ip4dst | mask->ip4dst))
628 mask->ip4dst = htonl(0xffffffff);
629 if (!(entry->psrc | mask->psrc))
630 mask->psrc = htons(0xffff);
631 if (!(entry->pdst | mask->pdst))
632 mask->pdst = htons(0xffff);
633 if (!(entry->tos | mask->tos))
635 if (!(fs->vlan_tag | fs->vlan_tag_mask))
636 fs->vlan_tag_mask = 0xffff;
637 if (!(fs->data | fs->data_mask))
638 fs->data_mask = 0xffffffffffffffffULL;
641 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
642 void __user *useraddr)
644 struct ethtool_rx_ntuple cmd;
645 const struct ethtool_ops *ops = dev->ethtool_ops;
646 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
649 if (!(dev->features & NETIF_F_NTUPLE))
652 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
655 rx_ntuple_fix_masks(&cmd.fs);
658 * Cache filter in dev struct for GET operation only if
659 * the underlying driver doesn't have its own GET operation, and
660 * only if the filter was added successfully. First make sure we
661 * can allocate the filter, then continue if successful.
663 if (!ops->get_rx_ntuple) {
664 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
669 ret = ops->set_rx_ntuple(dev, &cmd);
675 if (!ops->get_rx_ntuple)
676 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
681 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
683 struct ethtool_gstrings gstrings;
684 const struct ethtool_ops *ops = dev->ethtool_ops;
685 struct ethtool_rx_ntuple_flow_spec_container *fsc;
688 int ret, i, num_strings = 0;
690 if (!ops->get_sset_count)
693 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
696 ret = ops->get_sset_count(dev, gstrings.string_set);
702 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
706 if (ops->get_rx_ntuple) {
707 /* driver-specific filter grab */
708 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
712 /* default ethtool filter grab */
715 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
716 sprintf(p, "Filter %d:\n", i);
717 p += ETH_GSTRING_LEN;
720 switch (fsc->fs.flow_type) {
722 sprintf(p, "\tFlow Type: TCP\n");
723 p += ETH_GSTRING_LEN;
727 sprintf(p, "\tFlow Type: UDP\n");
728 p += ETH_GSTRING_LEN;
732 sprintf(p, "\tFlow Type: SCTP\n");
733 p += ETH_GSTRING_LEN;
737 sprintf(p, "\tFlow Type: AH ESP\n");
738 p += ETH_GSTRING_LEN;
742 sprintf(p, "\tFlow Type: ESP\n");
743 p += ETH_GSTRING_LEN;
747 sprintf(p, "\tFlow Type: Raw IP\n");
748 p += ETH_GSTRING_LEN;
752 sprintf(p, "\tFlow Type: IPv4\n");
753 p += ETH_GSTRING_LEN;
757 sprintf(p, "\tFlow Type: Unknown\n");
758 p += ETH_GSTRING_LEN;
763 /* now the rest of the filters */
764 switch (fsc->fs.flow_type) {
768 sprintf(p, "\tSrc IP addr: 0x%x\n",
769 fsc->fs.h_u.tcp_ip4_spec.ip4src);
770 p += ETH_GSTRING_LEN;
772 sprintf(p, "\tSrc IP mask: 0x%x\n",
773 fsc->fs.m_u.tcp_ip4_spec.ip4src);
774 p += ETH_GSTRING_LEN;
776 sprintf(p, "\tDest IP addr: 0x%x\n",
777 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
778 p += ETH_GSTRING_LEN;
780 sprintf(p, "\tDest IP mask: 0x%x\n",
781 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
782 p += ETH_GSTRING_LEN;
784 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
785 fsc->fs.h_u.tcp_ip4_spec.psrc,
786 fsc->fs.m_u.tcp_ip4_spec.psrc);
787 p += ETH_GSTRING_LEN;
789 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
790 fsc->fs.h_u.tcp_ip4_spec.pdst,
791 fsc->fs.m_u.tcp_ip4_spec.pdst);
792 p += ETH_GSTRING_LEN;
794 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
795 fsc->fs.h_u.tcp_ip4_spec.tos,
796 fsc->fs.m_u.tcp_ip4_spec.tos);
797 p += ETH_GSTRING_LEN;
802 sprintf(p, "\tSrc IP addr: 0x%x\n",
803 fsc->fs.h_u.ah_ip4_spec.ip4src);
804 p += ETH_GSTRING_LEN;
806 sprintf(p, "\tSrc IP mask: 0x%x\n",
807 fsc->fs.m_u.ah_ip4_spec.ip4src);
808 p += ETH_GSTRING_LEN;
810 sprintf(p, "\tDest IP addr: 0x%x\n",
811 fsc->fs.h_u.ah_ip4_spec.ip4dst);
812 p += ETH_GSTRING_LEN;
814 sprintf(p, "\tDest IP mask: 0x%x\n",
815 fsc->fs.m_u.ah_ip4_spec.ip4dst);
816 p += ETH_GSTRING_LEN;
818 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
819 fsc->fs.h_u.ah_ip4_spec.spi,
820 fsc->fs.m_u.ah_ip4_spec.spi);
821 p += ETH_GSTRING_LEN;
823 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
824 fsc->fs.h_u.ah_ip4_spec.tos,
825 fsc->fs.m_u.ah_ip4_spec.tos);
826 p += ETH_GSTRING_LEN;
830 sprintf(p, "\tSrc IP addr: 0x%x\n",
831 fsc->fs.h_u.usr_ip4_spec.ip4src);
832 p += ETH_GSTRING_LEN;
834 sprintf(p, "\tSrc IP mask: 0x%x\n",
835 fsc->fs.m_u.usr_ip4_spec.ip4src);
836 p += ETH_GSTRING_LEN;
838 sprintf(p, "\tDest IP addr: 0x%x\n",
839 fsc->fs.h_u.usr_ip4_spec.ip4dst);
840 p += ETH_GSTRING_LEN;
842 sprintf(p, "\tDest IP mask: 0x%x\n",
843 fsc->fs.m_u.usr_ip4_spec.ip4dst);
844 p += ETH_GSTRING_LEN;
848 sprintf(p, "\tSrc IP addr: 0x%x\n",
849 fsc->fs.h_u.usr_ip4_spec.ip4src);
850 p += ETH_GSTRING_LEN;
852 sprintf(p, "\tSrc IP mask: 0x%x\n",
853 fsc->fs.m_u.usr_ip4_spec.ip4src);
854 p += ETH_GSTRING_LEN;
856 sprintf(p, "\tDest IP addr: 0x%x\n",
857 fsc->fs.h_u.usr_ip4_spec.ip4dst);
858 p += ETH_GSTRING_LEN;
860 sprintf(p, "\tDest IP mask: 0x%x\n",
861 fsc->fs.m_u.usr_ip4_spec.ip4dst);
862 p += ETH_GSTRING_LEN;
864 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
865 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
866 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
867 p += ETH_GSTRING_LEN;
869 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
870 fsc->fs.h_u.usr_ip4_spec.tos,
871 fsc->fs.m_u.usr_ip4_spec.tos);
872 p += ETH_GSTRING_LEN;
874 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
875 fsc->fs.h_u.usr_ip4_spec.ip_ver,
876 fsc->fs.m_u.usr_ip4_spec.ip_ver);
877 p += ETH_GSTRING_LEN;
879 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
880 fsc->fs.h_u.usr_ip4_spec.proto,
881 fsc->fs.m_u.usr_ip4_spec.proto);
882 p += ETH_GSTRING_LEN;
886 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
887 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
888 p += ETH_GSTRING_LEN;
890 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
891 p += ETH_GSTRING_LEN;
893 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
894 p += ETH_GSTRING_LEN;
896 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
897 sprintf(p, "\tAction: Drop\n");
899 sprintf(p, "\tAction: Direct to queue %d\n",
901 p += ETH_GSTRING_LEN;
907 /* indicate to userspace how many strings we actually have */
908 gstrings.len = num_strings;
910 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
912 useraddr += sizeof(gstrings);
913 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
922 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
924 struct ethtool_regs regs;
925 const struct ethtool_ops *ops = dev->ethtool_ops;
929 if (!ops->get_regs || !ops->get_regs_len)
932 if (copy_from_user(®s, useraddr, sizeof(regs)))
935 reglen = ops->get_regs_len(dev);
936 if (regs.len > reglen)
939 regbuf = vzalloc(reglen);
943 ops->get_regs(dev, ®s, regbuf);
946 if (copy_to_user(useraddr, ®s, sizeof(regs)))
948 useraddr += offsetof(struct ethtool_regs, data);
949 if (copy_to_user(useraddr, regbuf, regs.len))
958 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
960 struct ethtool_value reset;
963 if (!dev->ethtool_ops->reset)
966 if (copy_from_user(&reset, useraddr, sizeof(reset)))
969 ret = dev->ethtool_ops->reset(dev, &reset.data);
973 if (copy_to_user(useraddr, &reset, sizeof(reset)))
978 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
980 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
982 if (!dev->ethtool_ops->get_wol)
985 dev->ethtool_ops->get_wol(dev, &wol);
987 if (copy_to_user(useraddr, &wol, sizeof(wol)))
992 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
994 struct ethtool_wolinfo wol;
996 if (!dev->ethtool_ops->set_wol)
999 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1002 return dev->ethtool_ops->set_wol(dev, &wol);
1005 static int ethtool_nway_reset(struct net_device *dev)
1007 if (!dev->ethtool_ops->nway_reset)
1010 return dev->ethtool_ops->nway_reset(dev);
1013 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1015 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1017 if (!dev->ethtool_ops->get_link)
1020 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1022 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1027 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1029 struct ethtool_eeprom eeprom;
1030 const struct ethtool_ops *ops = dev->ethtool_ops;
1031 void __user *userbuf = useraddr + sizeof(eeprom);
1032 u32 bytes_remaining;
1036 if (!ops->get_eeprom || !ops->get_eeprom_len)
1039 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1042 /* Check for wrap and zero */
1043 if (eeprom.offset + eeprom.len <= eeprom.offset)
1046 /* Check for exceeding total eeprom len */
1047 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1050 data = kmalloc(PAGE_SIZE, GFP_USER);
1054 bytes_remaining = eeprom.len;
1055 while (bytes_remaining > 0) {
1056 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1058 ret = ops->get_eeprom(dev, &eeprom, data);
1061 if (copy_to_user(userbuf, data, eeprom.len)) {
1065 userbuf += eeprom.len;
1066 eeprom.offset += eeprom.len;
1067 bytes_remaining -= eeprom.len;
1070 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1071 eeprom.offset -= eeprom.len;
1072 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1079 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1081 struct ethtool_eeprom eeprom;
1082 const struct ethtool_ops *ops = dev->ethtool_ops;
1083 void __user *userbuf = useraddr + sizeof(eeprom);
1084 u32 bytes_remaining;
1088 if (!ops->set_eeprom || !ops->get_eeprom_len)
1091 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1094 /* Check for wrap and zero */
1095 if (eeprom.offset + eeprom.len <= eeprom.offset)
1098 /* Check for exceeding total eeprom len */
1099 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1102 data = kmalloc(PAGE_SIZE, GFP_USER);
1106 bytes_remaining = eeprom.len;
1107 while (bytes_remaining > 0) {
1108 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1110 if (copy_from_user(data, userbuf, eeprom.len)) {
1114 ret = ops->set_eeprom(dev, &eeprom, data);
1117 userbuf += eeprom.len;
1118 eeprom.offset += eeprom.len;
1119 bytes_remaining -= eeprom.len;
1126 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1127 void __user *useraddr)
1129 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1131 if (!dev->ethtool_ops->get_coalesce)
1134 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1136 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1141 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1142 void __user *useraddr)
1144 struct ethtool_coalesce coalesce;
1146 if (!dev->ethtool_ops->set_coalesce)
1149 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1152 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1155 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1157 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1159 if (!dev->ethtool_ops->get_ringparam)
1162 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1164 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1169 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1171 struct ethtool_ringparam ringparam;
1173 if (!dev->ethtool_ops->set_ringparam)
1176 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1179 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1182 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1184 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1186 if (!dev->ethtool_ops->get_pauseparam)
1189 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1191 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1196 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1198 struct ethtool_pauseparam pauseparam;
1200 if (!dev->ethtool_ops->set_pauseparam)
1203 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1206 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1209 static int __ethtool_set_sg(struct net_device *dev, u32 data)
1213 if (data && !(dev->features & NETIF_F_ALL_CSUM))
1216 if (!data && dev->ethtool_ops->set_tso) {
1217 err = dev->ethtool_ops->set_tso(dev, 0);
1222 if (!data && dev->ethtool_ops->set_ufo) {
1223 err = dev->ethtool_ops->set_ufo(dev, 0);
1227 return dev->ethtool_ops->set_sg(dev, data);
1230 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
1234 if (!dev->ethtool_ops->set_tx_csum)
1237 if (!data && dev->ethtool_ops->set_sg) {
1238 err = __ethtool_set_sg(dev, 0);
1243 return dev->ethtool_ops->set_tx_csum(dev, data);
1246 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1248 struct ethtool_value edata;
1250 if (!dev->ethtool_ops->set_rx_csum)
1253 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1256 if (!edata.data && dev->ethtool_ops->set_sg)
1257 dev->features &= ~NETIF_F_GRO;
1259 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1262 static int __ethtool_set_tso(struct net_device *dev, u32 data)
1264 if (!dev->ethtool_ops->set_tso)
1267 if (data && !(dev->features & NETIF_F_SG))
1270 return dev->ethtool_ops->set_tso(dev, data);
1273 static int __ethtool_set_ufo(struct net_device *dev, u32 data)
1275 if (!dev->ethtool_ops->set_ufo)
1277 if (data && !(dev->features & NETIF_F_SG))
1279 if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
1280 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1281 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
1283 return dev->ethtool_ops->set_ufo(dev, data);
1286 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1288 struct ethtool_test test;
1289 const struct ethtool_ops *ops = dev->ethtool_ops;
1293 if (!ops->self_test || !ops->get_sset_count)
1296 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1299 WARN_ON(test_len == 0);
1301 if (copy_from_user(&test, useraddr, sizeof(test)))
1304 test.len = test_len;
1305 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1309 ops->self_test(dev, &test, data);
1312 if (copy_to_user(useraddr, &test, sizeof(test)))
1314 useraddr += sizeof(test);
1315 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1324 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1326 struct ethtool_gstrings gstrings;
1330 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1333 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1339 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1343 __ethtool_get_strings(dev, gstrings.string_set, data);
1346 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1348 useraddr += sizeof(gstrings);
1349 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1358 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1360 struct ethtool_value id;
1362 if (!dev->ethtool_ops->phys_id)
1365 if (copy_from_user(&id, useraddr, sizeof(id)))
1368 return dev->ethtool_ops->phys_id(dev, id.data);
1371 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1373 struct ethtool_stats stats;
1374 const struct ethtool_ops *ops = dev->ethtool_ops;
1378 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1381 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1384 WARN_ON(n_stats == 0);
1386 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1389 stats.n_stats = n_stats;
1390 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1394 ops->get_ethtool_stats(dev, &stats, data);
1397 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1399 useraddr += sizeof(stats);
1400 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1409 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1411 struct ethtool_perm_addr epaddr;
1413 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1416 if (epaddr.size < dev->addr_len)
1418 epaddr.size = dev->addr_len;
1420 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1422 useraddr += sizeof(epaddr);
1423 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1428 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1429 u32 cmd, u32 (*actor)(struct net_device *))
1431 struct ethtool_value edata = { .cmd = cmd };
1436 edata.data = actor(dev);
1438 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1443 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1444 void (*actor)(struct net_device *, u32))
1446 struct ethtool_value edata;
1451 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1454 actor(dev, edata.data);
1458 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1459 int (*actor)(struct net_device *, u32))
1461 struct ethtool_value edata;
1466 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1469 return actor(dev, edata.data);
1472 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1473 char __user *useraddr)
1475 struct ethtool_flash efl;
1477 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1480 if (!dev->ethtool_ops->flash_device)
1483 return dev->ethtool_ops->flash_device(dev, &efl);
1486 /* The main entry point in this file. Called from net/core/dev.c */
1488 int dev_ethtool(struct net *net, struct ifreq *ifr)
1490 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1491 void __user *useraddr = ifr->ifr_data;
1496 if (!dev || !netif_device_present(dev))
1499 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1502 if (!dev->ethtool_ops) {
1503 /* ETHTOOL_GDRVINFO does not require any driver support.
1504 * It is also unprivileged and does not change anything,
1505 * so we can take a shortcut to it. */
1506 if (ethcmd == ETHTOOL_GDRVINFO)
1507 return ethtool_get_drvinfo(dev, useraddr);
1512 /* Allow some commands to be done by anyone */
1515 case ETHTOOL_GDRVINFO:
1516 case ETHTOOL_GMSGLVL:
1517 case ETHTOOL_GCOALESCE:
1518 case ETHTOOL_GRINGPARAM:
1519 case ETHTOOL_GPAUSEPARAM:
1520 case ETHTOOL_GRXCSUM:
1521 case ETHTOOL_GTXCSUM:
1523 case ETHTOOL_GSTRINGS:
1525 case ETHTOOL_GPERMADDR:
1529 case ETHTOOL_GFLAGS:
1530 case ETHTOOL_GPFLAGS:
1532 case ETHTOOL_GRXRINGS:
1533 case ETHTOOL_GRXCLSRLCNT:
1534 case ETHTOOL_GRXCLSRULE:
1535 case ETHTOOL_GRXCLSRLALL:
1538 if (!capable(CAP_NET_ADMIN))
1542 if (dev->ethtool_ops->begin) {
1543 rc = dev->ethtool_ops->begin(dev);
1547 old_features = dev->features;
1551 rc = ethtool_get_settings(dev, useraddr);
1554 rc = ethtool_set_settings(dev, useraddr);
1556 case ETHTOOL_GDRVINFO:
1557 rc = ethtool_get_drvinfo(dev, useraddr);
1560 rc = ethtool_get_regs(dev, useraddr);
1563 rc = ethtool_get_wol(dev, useraddr);
1566 rc = ethtool_set_wol(dev, useraddr);
1568 case ETHTOOL_GMSGLVL:
1569 rc = ethtool_get_value(dev, useraddr, ethcmd,
1570 dev->ethtool_ops->get_msglevel);
1572 case ETHTOOL_SMSGLVL:
1573 rc = ethtool_set_value_void(dev, useraddr,
1574 dev->ethtool_ops->set_msglevel);
1576 case ETHTOOL_NWAY_RST:
1577 rc = ethtool_nway_reset(dev);
1580 rc = ethtool_get_link(dev, useraddr);
1582 case ETHTOOL_GEEPROM:
1583 rc = ethtool_get_eeprom(dev, useraddr);
1585 case ETHTOOL_SEEPROM:
1586 rc = ethtool_set_eeprom(dev, useraddr);
1588 case ETHTOOL_GCOALESCE:
1589 rc = ethtool_get_coalesce(dev, useraddr);
1591 case ETHTOOL_SCOALESCE:
1592 rc = ethtool_set_coalesce(dev, useraddr);
1594 case ETHTOOL_GRINGPARAM:
1595 rc = ethtool_get_ringparam(dev, useraddr);
1597 case ETHTOOL_SRINGPARAM:
1598 rc = ethtool_set_ringparam(dev, useraddr);
1600 case ETHTOOL_GPAUSEPARAM:
1601 rc = ethtool_get_pauseparam(dev, useraddr);
1603 case ETHTOOL_SPAUSEPARAM:
1604 rc = ethtool_set_pauseparam(dev, useraddr);
1606 case ETHTOOL_GRXCSUM:
1607 rc = ethtool_get_value(dev, useraddr, ethcmd,
1608 (dev->ethtool_ops->get_rx_csum ?
1609 dev->ethtool_ops->get_rx_csum :
1610 ethtool_op_get_rx_csum));
1612 case ETHTOOL_SRXCSUM:
1613 rc = ethtool_set_rx_csum(dev, useraddr);
1616 rc = ethtool_self_test(dev, useraddr);
1618 case ETHTOOL_GSTRINGS:
1619 rc = ethtool_get_strings(dev, useraddr);
1621 case ETHTOOL_PHYS_ID:
1622 rc = ethtool_phys_id(dev, useraddr);
1624 case ETHTOOL_GSTATS:
1625 rc = ethtool_get_stats(dev, useraddr);
1627 case ETHTOOL_GPERMADDR:
1628 rc = ethtool_get_perm_addr(dev, useraddr);
1630 case ETHTOOL_GFLAGS:
1631 rc = ethtool_get_value(dev, useraddr, ethcmd,
1632 (dev->ethtool_ops->get_flags ?
1633 dev->ethtool_ops->get_flags :
1634 ethtool_op_get_flags));
1636 case ETHTOOL_SFLAGS:
1637 rc = ethtool_set_value(dev, useraddr,
1638 dev->ethtool_ops->set_flags);
1640 case ETHTOOL_GPFLAGS:
1641 rc = ethtool_get_value(dev, useraddr, ethcmd,
1642 dev->ethtool_ops->get_priv_flags);
1644 case ETHTOOL_SPFLAGS:
1645 rc = ethtool_set_value(dev, useraddr,
1646 dev->ethtool_ops->set_priv_flags);
1649 case ETHTOOL_GRXRINGS:
1650 case ETHTOOL_GRXCLSRLCNT:
1651 case ETHTOOL_GRXCLSRULE:
1652 case ETHTOOL_GRXCLSRLALL:
1653 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1656 case ETHTOOL_SRXCLSRLDEL:
1657 case ETHTOOL_SRXCLSRLINS:
1658 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1660 case ETHTOOL_FLASHDEV:
1661 rc = ethtool_flash_device(dev, useraddr);
1664 rc = ethtool_reset(dev, useraddr);
1666 case ETHTOOL_SRXNTUPLE:
1667 rc = ethtool_set_rx_ntuple(dev, useraddr);
1669 case ETHTOOL_GRXNTUPLE:
1670 rc = ethtool_get_rx_ntuple(dev, useraddr);
1672 case ETHTOOL_GSSET_INFO:
1673 rc = ethtool_get_sset_info(dev, useraddr);
1675 case ETHTOOL_GRXFHINDIR:
1676 rc = ethtool_get_rxfh_indir(dev, useraddr);
1678 case ETHTOOL_SRXFHINDIR:
1679 rc = ethtool_set_rxfh_indir(dev, useraddr);
1681 case ETHTOOL_GTXCSUM:
1687 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1689 case ETHTOOL_STXCSUM:
1695 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1701 if (dev->ethtool_ops->complete)
1702 dev->ethtool_ops->complete(dev);
1704 if (old_features != dev->features)
1705 netdev_features_change(dev);