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;
59 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
62 dev->features |= NETIF_F_HW_CSUM;
64 dev->features &= ~NETIF_F_HW_CSUM;
68 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
70 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
73 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
75 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
79 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
81 u32 ethtool_op_get_sg(struct net_device *dev)
83 return (dev->features & NETIF_F_SG) != 0;
85 EXPORT_SYMBOL(ethtool_op_get_sg);
87 int ethtool_op_set_sg(struct net_device *dev, u32 data)
90 dev->features |= NETIF_F_SG;
92 dev->features &= ~NETIF_F_SG;
96 EXPORT_SYMBOL(ethtool_op_set_sg);
98 u32 ethtool_op_get_tso(struct net_device *dev)
100 return (dev->features & NETIF_F_TSO) != 0;
102 EXPORT_SYMBOL(ethtool_op_get_tso);
104 int ethtool_op_set_tso(struct net_device *dev, u32 data)
107 dev->features |= NETIF_F_TSO;
109 dev->features &= ~NETIF_F_TSO;
113 EXPORT_SYMBOL(ethtool_op_set_tso);
115 u32 ethtool_op_get_ufo(struct net_device *dev)
117 return (dev->features & NETIF_F_UFO) != 0;
119 EXPORT_SYMBOL(ethtool_op_get_ufo);
121 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
124 dev->features |= NETIF_F_UFO;
126 dev->features &= ~NETIF_F_UFO;
129 EXPORT_SYMBOL(ethtool_op_set_ufo);
131 /* the following list of flags are the same as their associated
132 * NETIF_F_xxx values in include/linux/netdevice.h
134 static const u32 flags_dup_features =
135 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
137 u32 ethtool_op_get_flags(struct net_device *dev)
139 /* in the future, this function will probably contain additional
140 * handling for flags which are not so easily handled
141 * by a simple masking operation
144 return dev->features & flags_dup_features;
146 EXPORT_SYMBOL(ethtool_op_get_flags);
148 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
150 if (data & ~supported)
153 dev->features = ((dev->features & ~flags_dup_features) |
154 (data & flags_dup_features));
157 EXPORT_SYMBOL(ethtool_op_set_flags);
159 void ethtool_ntuple_flush(struct net_device *dev)
161 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
163 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
164 list_del(&fsc->list);
167 dev->ethtool_ntuple_list.count = 0;
169 EXPORT_SYMBOL(ethtool_ntuple_flush);
171 /* Handlers for each ethtool command */
173 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
175 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
178 if (!dev->ethtool_ops->get_settings)
181 err = dev->ethtool_ops->get_settings(dev, &cmd);
185 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
190 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
192 struct ethtool_cmd cmd;
194 if (!dev->ethtool_ops->set_settings)
197 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
200 return dev->ethtool_ops->set_settings(dev, &cmd);
203 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
204 void __user *useraddr)
206 struct ethtool_drvinfo info;
207 const struct ethtool_ops *ops = dev->ethtool_ops;
209 memset(&info, 0, sizeof(info));
210 info.cmd = ETHTOOL_GDRVINFO;
211 if (ops && ops->get_drvinfo) {
212 ops->get_drvinfo(dev, &info);
213 } else if (dev->dev.parent && dev->dev.parent->driver) {
214 strlcpy(info.bus_info, dev_name(dev->dev.parent),
215 sizeof(info.bus_info));
216 strlcpy(info.driver, dev->dev.parent->driver->name,
217 sizeof(info.driver));
223 * this method of obtaining string set info is deprecated;
224 * Use ETHTOOL_GSSET_INFO instead.
226 if (ops && ops->get_sset_count) {
229 rc = ops->get_sset_count(dev, ETH_SS_TEST);
231 info.testinfo_len = rc;
232 rc = ops->get_sset_count(dev, ETH_SS_STATS);
235 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
237 info.n_priv_flags = rc;
239 if (ops && ops->get_regs_len)
240 info.regdump_len = ops->get_regs_len(dev);
241 if (ops && ops->get_eeprom_len)
242 info.eedump_len = ops->get_eeprom_len(dev);
244 if (copy_to_user(useraddr, &info, sizeof(info)))
249 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
250 void __user *useraddr)
252 struct ethtool_sset_info info;
253 const struct ethtool_ops *ops = dev->ethtool_ops;
255 int i, idx = 0, n_bits = 0, ret, rc;
256 u32 *info_buf = NULL;
258 if (!ops->get_sset_count)
261 if (copy_from_user(&info, useraddr, sizeof(info)))
264 /* store copy of mask, because we zero struct later on */
265 sset_mask = info.sset_mask;
269 /* calculate size of return buffer */
270 n_bits = hweight64(sset_mask);
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)))
309 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
310 u32 cmd, void __user *useraddr)
312 struct ethtool_rxnfc info;
313 size_t info_size = sizeof(info);
315 if (!dev->ethtool_ops->set_rxnfc)
318 /* struct ethtool_rxnfc was originally defined for
319 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
320 * members. User-space might still be using that
322 if (cmd == ETHTOOL_SRXFH)
323 info_size = (offsetof(struct ethtool_rxnfc, data) +
326 if (copy_from_user(&info, useraddr, info_size))
329 return dev->ethtool_ops->set_rxnfc(dev, &info);
332 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
333 u32 cmd, void __user *useraddr)
335 struct ethtool_rxnfc info;
336 size_t info_size = sizeof(info);
337 const struct ethtool_ops *ops = dev->ethtool_ops;
339 void *rule_buf = NULL;
344 /* struct ethtool_rxnfc was originally defined for
345 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
346 * members. User-space might still be using that
348 if (cmd == ETHTOOL_GRXFH)
349 info_size = (offsetof(struct ethtool_rxnfc, data) +
352 if (copy_from_user(&info, useraddr, info_size))
355 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
356 if (info.rule_cnt > 0) {
357 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
358 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
365 ret = ops->get_rxnfc(dev, &info, rule_buf);
370 if (copy_to_user(useraddr, &info, info_size))
374 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
375 if (copy_to_user(useraddr, rule_buf,
376 info.rule_cnt * sizeof(u32)))
387 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
388 void __user *useraddr)
390 struct ethtool_rxfh_indir *indir;
395 if (!dev->ethtool_ops->get_rxfh_indir)
398 if (copy_from_user(&table_size,
399 useraddr + offsetof(struct ethtool_rxfh_indir, size),
404 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
406 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
407 indir = kzalloc(full_size, GFP_USER);
411 indir->cmd = ETHTOOL_GRXFHINDIR;
412 indir->size = table_size;
413 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
417 if (copy_to_user(useraddr, indir, full_size))
425 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
426 void __user *useraddr)
428 struct ethtool_rxfh_indir *indir;
433 if (!dev->ethtool_ops->set_rxfh_indir)
436 if (copy_from_user(&table_size,
437 useraddr + offsetof(struct ethtool_rxfh_indir, size),
442 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
444 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
445 indir = kmalloc(full_size, GFP_USER);
449 if (copy_from_user(indir, useraddr, full_size)) {
454 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
461 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
462 struct ethtool_rx_ntuple_flow_spec *spec,
463 struct ethtool_rx_ntuple_flow_spec_container *fsc)
466 /* don't add filters forever */
467 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
468 /* free the container */
473 /* Copy the whole filter over */
474 fsc->fs.flow_type = spec->flow_type;
475 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
476 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
478 fsc->fs.vlan_tag = spec->vlan_tag;
479 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
480 fsc->fs.data = spec->data;
481 fsc->fs.data_mask = spec->data_mask;
482 fsc->fs.action = spec->action;
484 /* add to the list */
485 list_add_tail_rcu(&fsc->list, &list->list);
490 * ethtool does not (or did not) set masks for flow parameters that are
491 * not specified, so if both value and mask are 0 then this must be
492 * treated as equivalent to a mask with all bits set. Implement that
493 * here rather than in drivers.
495 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
497 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
498 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
500 if (fs->flow_type != TCP_V4_FLOW &&
501 fs->flow_type != UDP_V4_FLOW &&
502 fs->flow_type != SCTP_V4_FLOW)
505 if (!(entry->ip4src | mask->ip4src))
506 mask->ip4src = htonl(0xffffffff);
507 if (!(entry->ip4dst | mask->ip4dst))
508 mask->ip4dst = htonl(0xffffffff);
509 if (!(entry->psrc | mask->psrc))
510 mask->psrc = htons(0xffff);
511 if (!(entry->pdst | mask->pdst))
512 mask->pdst = htons(0xffff);
513 if (!(entry->tos | mask->tos))
515 if (!(fs->vlan_tag | fs->vlan_tag_mask))
516 fs->vlan_tag_mask = 0xffff;
517 if (!(fs->data | fs->data_mask))
518 fs->data_mask = 0xffffffffffffffffULL;
521 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
522 void __user *useraddr)
524 struct ethtool_rx_ntuple cmd;
525 const struct ethtool_ops *ops = dev->ethtool_ops;
526 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
529 if (!(dev->features & NETIF_F_NTUPLE))
532 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
535 rx_ntuple_fix_masks(&cmd.fs);
538 * Cache filter in dev struct for GET operation only if
539 * the underlying driver doesn't have its own GET operation, and
540 * only if the filter was added successfully. First make sure we
541 * can allocate the filter, then continue if successful.
543 if (!ops->get_rx_ntuple) {
544 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
549 ret = ops->set_rx_ntuple(dev, &cmd);
555 if (!ops->get_rx_ntuple)
556 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
561 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
563 struct ethtool_gstrings gstrings;
564 const struct ethtool_ops *ops = dev->ethtool_ops;
565 struct ethtool_rx_ntuple_flow_spec_container *fsc;
568 int ret, i, num_strings = 0;
570 if (!ops->get_sset_count)
573 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
576 ret = ops->get_sset_count(dev, gstrings.string_set);
582 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
586 if (ops->get_rx_ntuple) {
587 /* driver-specific filter grab */
588 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
592 /* default ethtool filter grab */
595 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
596 sprintf(p, "Filter %d:\n", i);
597 p += ETH_GSTRING_LEN;
600 switch (fsc->fs.flow_type) {
602 sprintf(p, "\tFlow Type: TCP\n");
603 p += ETH_GSTRING_LEN;
607 sprintf(p, "\tFlow Type: UDP\n");
608 p += ETH_GSTRING_LEN;
612 sprintf(p, "\tFlow Type: SCTP\n");
613 p += ETH_GSTRING_LEN;
617 sprintf(p, "\tFlow Type: AH ESP\n");
618 p += ETH_GSTRING_LEN;
622 sprintf(p, "\tFlow Type: ESP\n");
623 p += ETH_GSTRING_LEN;
627 sprintf(p, "\tFlow Type: Raw IP\n");
628 p += ETH_GSTRING_LEN;
632 sprintf(p, "\tFlow Type: IPv4\n");
633 p += ETH_GSTRING_LEN;
637 sprintf(p, "\tFlow Type: Unknown\n");
638 p += ETH_GSTRING_LEN;
643 /* now the rest of the filters */
644 switch (fsc->fs.flow_type) {
648 sprintf(p, "\tSrc IP addr: 0x%x\n",
649 fsc->fs.h_u.tcp_ip4_spec.ip4src);
650 p += ETH_GSTRING_LEN;
652 sprintf(p, "\tSrc IP mask: 0x%x\n",
653 fsc->fs.m_u.tcp_ip4_spec.ip4src);
654 p += ETH_GSTRING_LEN;
656 sprintf(p, "\tDest IP addr: 0x%x\n",
657 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
658 p += ETH_GSTRING_LEN;
660 sprintf(p, "\tDest IP mask: 0x%x\n",
661 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
662 p += ETH_GSTRING_LEN;
664 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
665 fsc->fs.h_u.tcp_ip4_spec.psrc,
666 fsc->fs.m_u.tcp_ip4_spec.psrc);
667 p += ETH_GSTRING_LEN;
669 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
670 fsc->fs.h_u.tcp_ip4_spec.pdst,
671 fsc->fs.m_u.tcp_ip4_spec.pdst);
672 p += ETH_GSTRING_LEN;
674 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
675 fsc->fs.h_u.tcp_ip4_spec.tos,
676 fsc->fs.m_u.tcp_ip4_spec.tos);
677 p += ETH_GSTRING_LEN;
682 sprintf(p, "\tSrc IP addr: 0x%x\n",
683 fsc->fs.h_u.ah_ip4_spec.ip4src);
684 p += ETH_GSTRING_LEN;
686 sprintf(p, "\tSrc IP mask: 0x%x\n",
687 fsc->fs.m_u.ah_ip4_spec.ip4src);
688 p += ETH_GSTRING_LEN;
690 sprintf(p, "\tDest IP addr: 0x%x\n",
691 fsc->fs.h_u.ah_ip4_spec.ip4dst);
692 p += ETH_GSTRING_LEN;
694 sprintf(p, "\tDest IP mask: 0x%x\n",
695 fsc->fs.m_u.ah_ip4_spec.ip4dst);
696 p += ETH_GSTRING_LEN;
698 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
699 fsc->fs.h_u.ah_ip4_spec.spi,
700 fsc->fs.m_u.ah_ip4_spec.spi);
701 p += ETH_GSTRING_LEN;
703 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
704 fsc->fs.h_u.ah_ip4_spec.tos,
705 fsc->fs.m_u.ah_ip4_spec.tos);
706 p += ETH_GSTRING_LEN;
710 sprintf(p, "\tSrc IP addr: 0x%x\n",
711 fsc->fs.h_u.usr_ip4_spec.ip4src);
712 p += ETH_GSTRING_LEN;
714 sprintf(p, "\tSrc IP mask: 0x%x\n",
715 fsc->fs.m_u.usr_ip4_spec.ip4src);
716 p += ETH_GSTRING_LEN;
718 sprintf(p, "\tDest IP addr: 0x%x\n",
719 fsc->fs.h_u.usr_ip4_spec.ip4dst);
720 p += ETH_GSTRING_LEN;
722 sprintf(p, "\tDest IP mask: 0x%x\n",
723 fsc->fs.m_u.usr_ip4_spec.ip4dst);
724 p += ETH_GSTRING_LEN;
728 sprintf(p, "\tSrc IP addr: 0x%x\n",
729 fsc->fs.h_u.usr_ip4_spec.ip4src);
730 p += ETH_GSTRING_LEN;
732 sprintf(p, "\tSrc IP mask: 0x%x\n",
733 fsc->fs.m_u.usr_ip4_spec.ip4src);
734 p += ETH_GSTRING_LEN;
736 sprintf(p, "\tDest IP addr: 0x%x\n",
737 fsc->fs.h_u.usr_ip4_spec.ip4dst);
738 p += ETH_GSTRING_LEN;
740 sprintf(p, "\tDest IP mask: 0x%x\n",
741 fsc->fs.m_u.usr_ip4_spec.ip4dst);
742 p += ETH_GSTRING_LEN;
744 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
745 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
746 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
747 p += ETH_GSTRING_LEN;
749 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
750 fsc->fs.h_u.usr_ip4_spec.tos,
751 fsc->fs.m_u.usr_ip4_spec.tos);
752 p += ETH_GSTRING_LEN;
754 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
755 fsc->fs.h_u.usr_ip4_spec.ip_ver,
756 fsc->fs.m_u.usr_ip4_spec.ip_ver);
757 p += ETH_GSTRING_LEN;
759 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
760 fsc->fs.h_u.usr_ip4_spec.proto,
761 fsc->fs.m_u.usr_ip4_spec.proto);
762 p += ETH_GSTRING_LEN;
766 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
767 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
768 p += ETH_GSTRING_LEN;
770 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
771 p += ETH_GSTRING_LEN;
773 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
774 p += ETH_GSTRING_LEN;
776 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
777 sprintf(p, "\tAction: Drop\n");
779 sprintf(p, "\tAction: Direct to queue %d\n",
781 p += ETH_GSTRING_LEN;
787 /* indicate to userspace how many strings we actually have */
788 gstrings.len = num_strings;
790 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
792 useraddr += sizeof(gstrings);
793 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
802 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
804 struct ethtool_regs regs;
805 const struct ethtool_ops *ops = dev->ethtool_ops;
809 if (!ops->get_regs || !ops->get_regs_len)
812 if (copy_from_user(®s, useraddr, sizeof(regs)))
815 reglen = ops->get_regs_len(dev);
816 if (regs.len > reglen)
819 regbuf = vmalloc(reglen);
823 ops->get_regs(dev, ®s, regbuf);
826 if (copy_to_user(useraddr, ®s, sizeof(regs)))
828 useraddr += offsetof(struct ethtool_regs, data);
829 if (copy_to_user(useraddr, regbuf, regs.len))
838 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
840 struct ethtool_value reset;
843 if (!dev->ethtool_ops->reset)
846 if (copy_from_user(&reset, useraddr, sizeof(reset)))
849 ret = dev->ethtool_ops->reset(dev, &reset.data);
853 if (copy_to_user(useraddr, &reset, sizeof(reset)))
858 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
860 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
862 if (!dev->ethtool_ops->get_wol)
865 dev->ethtool_ops->get_wol(dev, &wol);
867 if (copy_to_user(useraddr, &wol, sizeof(wol)))
872 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
874 struct ethtool_wolinfo wol;
876 if (!dev->ethtool_ops->set_wol)
879 if (copy_from_user(&wol, useraddr, sizeof(wol)))
882 return dev->ethtool_ops->set_wol(dev, &wol);
885 static int ethtool_nway_reset(struct net_device *dev)
887 if (!dev->ethtool_ops->nway_reset)
890 return dev->ethtool_ops->nway_reset(dev);
893 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
895 struct ethtool_eeprom eeprom;
896 const struct ethtool_ops *ops = dev->ethtool_ops;
897 void __user *userbuf = useraddr + sizeof(eeprom);
902 if (!ops->get_eeprom || !ops->get_eeprom_len)
905 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
908 /* Check for wrap and zero */
909 if (eeprom.offset + eeprom.len <= eeprom.offset)
912 /* Check for exceeding total eeprom len */
913 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
916 data = kmalloc(PAGE_SIZE, GFP_USER);
920 bytes_remaining = eeprom.len;
921 while (bytes_remaining > 0) {
922 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
924 ret = ops->get_eeprom(dev, &eeprom, data);
927 if (copy_to_user(userbuf, data, eeprom.len)) {
931 userbuf += eeprom.len;
932 eeprom.offset += eeprom.len;
933 bytes_remaining -= eeprom.len;
936 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
937 eeprom.offset -= eeprom.len;
938 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
945 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
947 struct ethtool_eeprom eeprom;
948 const struct ethtool_ops *ops = dev->ethtool_ops;
949 void __user *userbuf = useraddr + sizeof(eeprom);
954 if (!ops->set_eeprom || !ops->get_eeprom_len)
957 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
960 /* Check for wrap and zero */
961 if (eeprom.offset + eeprom.len <= eeprom.offset)
964 /* Check for exceeding total eeprom len */
965 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
968 data = kmalloc(PAGE_SIZE, GFP_USER);
972 bytes_remaining = eeprom.len;
973 while (bytes_remaining > 0) {
974 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
976 if (copy_from_user(data, userbuf, eeprom.len)) {
980 ret = ops->set_eeprom(dev, &eeprom, data);
983 userbuf += eeprom.len;
984 eeprom.offset += eeprom.len;
985 bytes_remaining -= eeprom.len;
992 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
993 void __user *useraddr)
995 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
997 if (!dev->ethtool_ops->get_coalesce)
1000 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1002 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1007 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1008 void __user *useraddr)
1010 struct ethtool_coalesce coalesce;
1012 if (!dev->ethtool_ops->set_coalesce)
1015 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1018 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1021 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1023 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1025 if (!dev->ethtool_ops->get_ringparam)
1028 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1030 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1035 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1037 struct ethtool_ringparam ringparam;
1039 if (!dev->ethtool_ops->set_ringparam)
1042 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1045 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1048 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1050 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1052 if (!dev->ethtool_ops->get_pauseparam)
1055 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1057 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1062 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1064 struct ethtool_pauseparam pauseparam;
1066 if (!dev->ethtool_ops->set_pauseparam)
1069 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1072 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1075 static int __ethtool_set_sg(struct net_device *dev, u32 data)
1079 if (!data && dev->ethtool_ops->set_tso) {
1080 err = dev->ethtool_ops->set_tso(dev, 0);
1085 if (!data && dev->ethtool_ops->set_ufo) {
1086 err = dev->ethtool_ops->set_ufo(dev, 0);
1090 return dev->ethtool_ops->set_sg(dev, data);
1093 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
1095 struct ethtool_value edata;
1098 if (!dev->ethtool_ops->set_tx_csum)
1101 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1104 if (!edata.data && dev->ethtool_ops->set_sg) {
1105 err = __ethtool_set_sg(dev, 0);
1110 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1112 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1114 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1116 struct ethtool_value edata;
1118 if (!dev->ethtool_ops->set_rx_csum)
1121 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1124 if (!edata.data && dev->ethtool_ops->set_sg)
1125 dev->features &= ~NETIF_F_GRO;
1127 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1130 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1132 struct ethtool_value edata;
1134 if (!dev->ethtool_ops->set_sg)
1137 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1141 !(dev->features & NETIF_F_ALL_CSUM))
1144 return __ethtool_set_sg(dev, edata.data);
1147 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1149 struct ethtool_value edata;
1151 if (!dev->ethtool_ops->set_tso)
1154 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1157 if (edata.data && !(dev->features & NETIF_F_SG))
1160 return dev->ethtool_ops->set_tso(dev, edata.data);
1163 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1165 struct ethtool_value edata;
1167 if (!dev->ethtool_ops->set_ufo)
1169 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1171 if (edata.data && !(dev->features & NETIF_F_SG))
1173 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1175 return dev->ethtool_ops->set_ufo(dev, edata.data);
1178 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1180 struct ethtool_value edata = { ETHTOOL_GGSO };
1182 edata.data = dev->features & NETIF_F_GSO;
1183 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1188 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1190 struct ethtool_value edata;
1192 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1195 dev->features |= NETIF_F_GSO;
1197 dev->features &= ~NETIF_F_GSO;
1201 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1203 struct ethtool_value edata = { ETHTOOL_GGRO };
1205 edata.data = dev->features & NETIF_F_GRO;
1206 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1211 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1213 struct ethtool_value edata;
1215 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1219 u32 rxcsum = dev->ethtool_ops->get_rx_csum ?
1220 dev->ethtool_ops->get_rx_csum(dev) :
1221 ethtool_op_get_rx_csum(dev);
1225 dev->features |= NETIF_F_GRO;
1227 dev->features &= ~NETIF_F_GRO;
1232 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1234 struct ethtool_test test;
1235 const struct ethtool_ops *ops = dev->ethtool_ops;
1239 if (!ops->self_test || !ops->get_sset_count)
1242 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1245 WARN_ON(test_len == 0);
1247 if (copy_from_user(&test, useraddr, sizeof(test)))
1250 test.len = test_len;
1251 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1255 ops->self_test(dev, &test, data);
1258 if (copy_to_user(useraddr, &test, sizeof(test)))
1260 useraddr += sizeof(test);
1261 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1270 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1272 struct ethtool_gstrings gstrings;
1273 const struct ethtool_ops *ops = dev->ethtool_ops;
1277 if (!ops->get_strings || !ops->get_sset_count)
1280 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1283 ret = ops->get_sset_count(dev, gstrings.string_set);
1289 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1293 ops->get_strings(dev, gstrings.string_set, data);
1296 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1298 useraddr += sizeof(gstrings);
1299 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1308 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1310 struct ethtool_value id;
1312 if (!dev->ethtool_ops->phys_id)
1315 if (copy_from_user(&id, useraddr, sizeof(id)))
1318 return dev->ethtool_ops->phys_id(dev, id.data);
1321 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1323 struct ethtool_stats stats;
1324 const struct ethtool_ops *ops = dev->ethtool_ops;
1328 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1331 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1334 WARN_ON(n_stats == 0);
1336 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1339 stats.n_stats = n_stats;
1340 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1344 ops->get_ethtool_stats(dev, &stats, data);
1347 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1349 useraddr += sizeof(stats);
1350 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1359 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1361 struct ethtool_perm_addr epaddr;
1363 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1366 if (epaddr.size < dev->addr_len)
1368 epaddr.size = dev->addr_len;
1370 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1372 useraddr += sizeof(epaddr);
1373 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1378 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1379 u32 cmd, u32 (*actor)(struct net_device *))
1381 struct ethtool_value edata = { .cmd = cmd };
1386 edata.data = actor(dev);
1388 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1393 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1394 void (*actor)(struct net_device *, u32))
1396 struct ethtool_value edata;
1401 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1404 actor(dev, edata.data);
1408 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1409 int (*actor)(struct net_device *, u32))
1411 struct ethtool_value edata;
1416 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1419 return actor(dev, edata.data);
1422 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1423 char __user *useraddr)
1425 struct ethtool_flash efl;
1427 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1430 if (!dev->ethtool_ops->flash_device)
1433 return dev->ethtool_ops->flash_device(dev, &efl);
1436 /* The main entry point in this file. Called from net/core/dev.c */
1438 int dev_ethtool(struct net *net, struct ifreq *ifr)
1440 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1441 void __user *useraddr = ifr->ifr_data;
1444 unsigned long old_features;
1446 if (!dev || !netif_device_present(dev))
1449 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1452 if (!dev->ethtool_ops) {
1453 /* ETHTOOL_GDRVINFO does not require any driver support.
1454 * It is also unprivileged and does not change anything,
1455 * so we can take a shortcut to it. */
1456 if (ethcmd == ETHTOOL_GDRVINFO)
1457 return ethtool_get_drvinfo(dev, useraddr);
1462 /* Allow some commands to be done by anyone */
1465 case ETHTOOL_GDRVINFO:
1466 case ETHTOOL_GMSGLVL:
1467 case ETHTOOL_GCOALESCE:
1468 case ETHTOOL_GRINGPARAM:
1469 case ETHTOOL_GPAUSEPARAM:
1470 case ETHTOOL_GRXCSUM:
1471 case ETHTOOL_GTXCSUM:
1473 case ETHTOOL_GSTRINGS:
1475 case ETHTOOL_GPERMADDR:
1479 case ETHTOOL_GFLAGS:
1480 case ETHTOOL_GPFLAGS:
1482 case ETHTOOL_GRXRINGS:
1483 case ETHTOOL_GRXCLSRLCNT:
1484 case ETHTOOL_GRXCLSRULE:
1485 case ETHTOOL_GRXCLSRLALL:
1488 if (!capable(CAP_NET_ADMIN))
1492 if (dev->ethtool_ops->begin) {
1493 rc = dev->ethtool_ops->begin(dev);
1497 old_features = dev->features;
1501 rc = ethtool_get_settings(dev, useraddr);
1504 rc = ethtool_set_settings(dev, useraddr);
1506 case ETHTOOL_GDRVINFO:
1507 rc = ethtool_get_drvinfo(dev, useraddr);
1510 rc = ethtool_get_regs(dev, useraddr);
1513 rc = ethtool_get_wol(dev, useraddr);
1516 rc = ethtool_set_wol(dev, useraddr);
1518 case ETHTOOL_GMSGLVL:
1519 rc = ethtool_get_value(dev, useraddr, ethcmd,
1520 dev->ethtool_ops->get_msglevel);
1522 case ETHTOOL_SMSGLVL:
1523 rc = ethtool_set_value_void(dev, useraddr,
1524 dev->ethtool_ops->set_msglevel);
1526 case ETHTOOL_NWAY_RST:
1527 rc = ethtool_nway_reset(dev);
1530 rc = ethtool_get_value(dev, useraddr, ethcmd,
1531 dev->ethtool_ops->get_link);
1533 case ETHTOOL_GEEPROM:
1534 rc = ethtool_get_eeprom(dev, useraddr);
1536 case ETHTOOL_SEEPROM:
1537 rc = ethtool_set_eeprom(dev, useraddr);
1539 case ETHTOOL_GCOALESCE:
1540 rc = ethtool_get_coalesce(dev, useraddr);
1542 case ETHTOOL_SCOALESCE:
1543 rc = ethtool_set_coalesce(dev, useraddr);
1545 case ETHTOOL_GRINGPARAM:
1546 rc = ethtool_get_ringparam(dev, useraddr);
1548 case ETHTOOL_SRINGPARAM:
1549 rc = ethtool_set_ringparam(dev, useraddr);
1551 case ETHTOOL_GPAUSEPARAM:
1552 rc = ethtool_get_pauseparam(dev, useraddr);
1554 case ETHTOOL_SPAUSEPARAM:
1555 rc = ethtool_set_pauseparam(dev, useraddr);
1557 case ETHTOOL_GRXCSUM:
1558 rc = ethtool_get_value(dev, useraddr, ethcmd,
1559 (dev->ethtool_ops->get_rx_csum ?
1560 dev->ethtool_ops->get_rx_csum :
1561 ethtool_op_get_rx_csum));
1563 case ETHTOOL_SRXCSUM:
1564 rc = ethtool_set_rx_csum(dev, useraddr);
1566 case ETHTOOL_GTXCSUM:
1567 rc = ethtool_get_value(dev, useraddr, ethcmd,
1568 (dev->ethtool_ops->get_tx_csum ?
1569 dev->ethtool_ops->get_tx_csum :
1570 ethtool_op_get_tx_csum));
1572 case ETHTOOL_STXCSUM:
1573 rc = ethtool_set_tx_csum(dev, useraddr);
1576 rc = ethtool_get_value(dev, useraddr, ethcmd,
1577 (dev->ethtool_ops->get_sg ?
1578 dev->ethtool_ops->get_sg :
1579 ethtool_op_get_sg));
1582 rc = ethtool_set_sg(dev, useraddr);
1585 rc = ethtool_get_value(dev, useraddr, ethcmd,
1586 (dev->ethtool_ops->get_tso ?
1587 dev->ethtool_ops->get_tso :
1588 ethtool_op_get_tso));
1591 rc = ethtool_set_tso(dev, useraddr);
1594 rc = ethtool_self_test(dev, useraddr);
1596 case ETHTOOL_GSTRINGS:
1597 rc = ethtool_get_strings(dev, useraddr);
1599 case ETHTOOL_PHYS_ID:
1600 rc = ethtool_phys_id(dev, useraddr);
1602 case ETHTOOL_GSTATS:
1603 rc = ethtool_get_stats(dev, useraddr);
1605 case ETHTOOL_GPERMADDR:
1606 rc = ethtool_get_perm_addr(dev, useraddr);
1609 rc = ethtool_get_value(dev, useraddr, ethcmd,
1610 (dev->ethtool_ops->get_ufo ?
1611 dev->ethtool_ops->get_ufo :
1612 ethtool_op_get_ufo));
1615 rc = ethtool_set_ufo(dev, useraddr);
1618 rc = ethtool_get_gso(dev, useraddr);
1621 rc = ethtool_set_gso(dev, useraddr);
1623 case ETHTOOL_GFLAGS:
1624 rc = ethtool_get_value(dev, useraddr, ethcmd,
1625 (dev->ethtool_ops->get_flags ?
1626 dev->ethtool_ops->get_flags :
1627 ethtool_op_get_flags));
1629 case ETHTOOL_SFLAGS:
1630 rc = ethtool_set_value(dev, useraddr,
1631 dev->ethtool_ops->set_flags);
1633 case ETHTOOL_GPFLAGS:
1634 rc = ethtool_get_value(dev, useraddr, ethcmd,
1635 dev->ethtool_ops->get_priv_flags);
1637 case ETHTOOL_SPFLAGS:
1638 rc = ethtool_set_value(dev, useraddr,
1639 dev->ethtool_ops->set_priv_flags);
1642 case ETHTOOL_GRXRINGS:
1643 case ETHTOOL_GRXCLSRLCNT:
1644 case ETHTOOL_GRXCLSRULE:
1645 case ETHTOOL_GRXCLSRLALL:
1646 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1649 case ETHTOOL_SRXCLSRLDEL:
1650 case ETHTOOL_SRXCLSRLINS:
1651 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1654 rc = ethtool_get_gro(dev, useraddr);
1657 rc = ethtool_set_gro(dev, useraddr);
1659 case ETHTOOL_FLASHDEV:
1660 rc = ethtool_flash_device(dev, useraddr);
1663 rc = ethtool_reset(dev, useraddr);
1665 case ETHTOOL_SRXNTUPLE:
1666 rc = ethtool_set_rx_ntuple(dev, useraddr);
1668 case ETHTOOL_GRXNTUPLE:
1669 rc = ethtool_get_rx_ntuple(dev, useraddr);
1671 case ETHTOOL_GSSET_INFO:
1672 rc = ethtool_get_sset_info(dev, useraddr);
1674 case ETHTOOL_GRXFHINDIR:
1675 rc = ethtool_get_rxfh_indir(dev, useraddr);
1677 case ETHTOOL_SRXFHINDIR:
1678 rc = ethtool_set_rxfh_indir(dev, useraddr);
1684 if (dev->ethtool_ops->complete)
1685 dev->ethtool_ops->complete(dev);
1687 if (old_features != dev->features)
1688 netdev_features_change(dev);