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/slab.h>
25 * Some useful ethtool_ops methods that're device independent.
26 * If we find that all drivers want to do the same thing here,
27 * we can turn these into dev_() function calls.
30 u32 ethtool_op_get_link(struct net_device *dev)
32 return netif_carrier_ok(dev) ? 1 : 0;
34 EXPORT_SYMBOL(ethtool_op_get_link);
36 u32 ethtool_op_get_rx_csum(struct net_device *dev)
38 return (dev->features & NETIF_F_ALL_CSUM) != 0;
40 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
42 u32 ethtool_op_get_tx_csum(struct net_device *dev)
44 return (dev->features & NETIF_F_ALL_CSUM) != 0;
46 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
48 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
51 dev->features |= NETIF_F_IP_CSUM;
53 dev->features &= ~NETIF_F_IP_CSUM;
58 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
61 dev->features |= NETIF_F_HW_CSUM;
63 dev->features &= ~NETIF_F_HW_CSUM;
67 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
69 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
72 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
74 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
78 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
80 u32 ethtool_op_get_sg(struct net_device *dev)
82 return (dev->features & NETIF_F_SG) != 0;
84 EXPORT_SYMBOL(ethtool_op_get_sg);
86 int ethtool_op_set_sg(struct net_device *dev, u32 data)
89 dev->features |= NETIF_F_SG;
91 dev->features &= ~NETIF_F_SG;
95 EXPORT_SYMBOL(ethtool_op_set_sg);
97 u32 ethtool_op_get_tso(struct net_device *dev)
99 return (dev->features & NETIF_F_TSO) != 0;
101 EXPORT_SYMBOL(ethtool_op_get_tso);
103 int ethtool_op_set_tso(struct net_device *dev, u32 data)
106 dev->features |= NETIF_F_TSO;
108 dev->features &= ~NETIF_F_TSO;
112 EXPORT_SYMBOL(ethtool_op_set_tso);
114 u32 ethtool_op_get_ufo(struct net_device *dev)
116 return (dev->features & NETIF_F_UFO) != 0;
118 EXPORT_SYMBOL(ethtool_op_get_ufo);
120 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
123 dev->features |= NETIF_F_UFO;
125 dev->features &= ~NETIF_F_UFO;
128 EXPORT_SYMBOL(ethtool_op_set_ufo);
130 /* the following list of flags are the same as their associated
131 * NETIF_F_xxx values in include/linux/netdevice.h
133 static const u32 flags_dup_features =
134 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
136 u32 ethtool_op_get_flags(struct net_device *dev)
138 /* in the future, this function will probably contain additional
139 * handling for flags which are not so easily handled
140 * by a simple masking operation
143 return dev->features & flags_dup_features;
145 EXPORT_SYMBOL(ethtool_op_get_flags);
147 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
149 if (data & ~supported)
152 dev->features = ((dev->features & ~flags_dup_features) |
153 (data & flags_dup_features));
156 EXPORT_SYMBOL(ethtool_op_set_flags);
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);
202 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
203 void __user *useraddr)
205 struct ethtool_drvinfo info;
206 const struct ethtool_ops *ops = dev->ethtool_ops;
208 memset(&info, 0, sizeof(info));
209 info.cmd = ETHTOOL_GDRVINFO;
210 if (ops && ops->get_drvinfo) {
211 ops->get_drvinfo(dev, &info);
212 } else if (dev->dev.parent && dev->dev.parent->driver) {
213 strlcpy(info.bus_info, dev_name(dev->dev.parent),
214 sizeof(info.bus_info));
215 strlcpy(info.driver, dev->dev.parent->driver->name,
216 sizeof(info.driver));
222 * this method of obtaining string set info is deprecated;
223 * Use ETHTOOL_GSSET_INFO instead.
225 if (ops && ops->get_sset_count) {
228 rc = ops->get_sset_count(dev, ETH_SS_TEST);
230 info.testinfo_len = rc;
231 rc = ops->get_sset_count(dev, ETH_SS_STATS);
234 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
236 info.n_priv_flags = rc;
238 if (ops && ops->get_regs_len)
239 info.regdump_len = ops->get_regs_len(dev);
240 if (ops && ops->get_eeprom_len)
241 info.eedump_len = ops->get_eeprom_len(dev);
243 if (copy_to_user(useraddr, &info, sizeof(info)))
248 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
249 void __user *useraddr)
251 struct ethtool_sset_info info;
252 const struct ethtool_ops *ops = dev->ethtool_ops;
254 int i, idx = 0, n_bits = 0, ret, rc;
255 u32 *info_buf = NULL;
257 if (!ops->get_sset_count)
260 if (copy_from_user(&info, useraddr, sizeof(info)))
263 /* store copy of mask, because we zero struct later on */
264 sset_mask = info.sset_mask;
268 /* calculate size of return buffer */
269 n_bits = hweight64(sset_mask);
271 memset(&info, 0, sizeof(info));
272 info.cmd = ETHTOOL_GSSET_INFO;
274 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
279 * fill return buffer based on input bitmask and successful
280 * get_sset_count return
282 for (i = 0; i < 64; i++) {
283 if (!(sset_mask & (1ULL << i)))
286 rc = ops->get_sset_count(dev, i);
288 info.sset_mask |= (1ULL << i);
289 info_buf[idx++] = rc;
294 if (copy_to_user(useraddr, &info, sizeof(info)))
297 useraddr += offsetof(struct ethtool_sset_info, data);
298 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
308 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
309 u32 cmd, void __user *useraddr)
311 struct ethtool_rxnfc info;
312 size_t info_size = sizeof(info);
314 if (!dev->ethtool_ops->set_rxnfc)
317 /* struct ethtool_rxnfc was originally defined for
318 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
319 * members. User-space might still be using that
321 if (cmd == ETHTOOL_SRXFH)
322 info_size = (offsetof(struct ethtool_rxnfc, data) +
325 if (copy_from_user(&info, useraddr, info_size))
328 return dev->ethtool_ops->set_rxnfc(dev, &info);
331 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
332 u32 cmd, void __user *useraddr)
334 struct ethtool_rxnfc info;
335 size_t info_size = sizeof(info);
336 const struct ethtool_ops *ops = dev->ethtool_ops;
338 void *rule_buf = NULL;
343 /* struct ethtool_rxnfc was originally defined for
344 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
345 * members. User-space might still be using that
347 if (cmd == ETHTOOL_GRXFH)
348 info_size = (offsetof(struct ethtool_rxnfc, data) +
351 if (copy_from_user(&info, useraddr, info_size))
354 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
355 if (info.rule_cnt > 0) {
356 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
357 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
364 ret = ops->get_rxnfc(dev, &info, rule_buf);
369 if (copy_to_user(useraddr, &info, info_size))
373 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
374 if (copy_to_user(useraddr, rule_buf,
375 info.rule_cnt * sizeof(u32)))
386 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
387 void __user *useraddr)
389 struct ethtool_rxfh_indir *indir;
394 if (!dev->ethtool_ops->get_rxfh_indir)
397 if (copy_from_user(&table_size,
398 useraddr + offsetof(struct ethtool_rxfh_indir, size),
403 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
405 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
406 indir = kmalloc(full_size, GFP_USER);
410 indir->cmd = ETHTOOL_GRXFHINDIR;
411 indir->size = table_size;
412 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
416 if (copy_to_user(useraddr, indir, full_size))
424 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
425 void __user *useraddr)
427 struct ethtool_rxfh_indir *indir;
432 if (!dev->ethtool_ops->set_rxfh_indir)
435 if (copy_from_user(&table_size,
436 useraddr + offsetof(struct ethtool_rxfh_indir, size),
441 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
443 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
444 indir = kmalloc(full_size, GFP_USER);
448 if (copy_from_user(indir, useraddr, full_size)) {
453 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
460 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
461 struct ethtool_rx_ntuple_flow_spec *spec,
462 struct ethtool_rx_ntuple_flow_spec_container *fsc)
465 /* don't add filters forever */
466 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
467 /* free the container */
472 /* Copy the whole filter over */
473 fsc->fs.flow_type = spec->flow_type;
474 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
475 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
477 fsc->fs.vlan_tag = spec->vlan_tag;
478 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
479 fsc->fs.data = spec->data;
480 fsc->fs.data_mask = spec->data_mask;
481 fsc->fs.action = spec->action;
483 /* add to the list */
484 list_add_tail_rcu(&fsc->list, &list->list);
488 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
489 void __user *useraddr)
491 struct ethtool_rx_ntuple cmd;
492 const struct ethtool_ops *ops = dev->ethtool_ops;
493 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
496 if (!(dev->features & NETIF_F_NTUPLE))
499 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
503 * Cache filter in dev struct for GET operation only if
504 * the underlying driver doesn't have its own GET operation, and
505 * only if the filter was added successfully. First make sure we
506 * can allocate the filter, then continue if successful.
508 if (!ops->get_rx_ntuple) {
509 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
514 ret = ops->set_rx_ntuple(dev, &cmd);
520 if (!ops->get_rx_ntuple)
521 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
526 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
528 struct ethtool_gstrings gstrings;
529 const struct ethtool_ops *ops = dev->ethtool_ops;
530 struct ethtool_rx_ntuple_flow_spec_container *fsc;
533 int ret, i, num_strings = 0;
535 if (!ops->get_sset_count)
538 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
541 ret = ops->get_sset_count(dev, gstrings.string_set);
547 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
551 if (ops->get_rx_ntuple) {
552 /* driver-specific filter grab */
553 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
557 /* default ethtool filter grab */
560 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
561 sprintf(p, "Filter %d:\n", i);
562 p += ETH_GSTRING_LEN;
565 switch (fsc->fs.flow_type) {
567 sprintf(p, "\tFlow Type: TCP\n");
568 p += ETH_GSTRING_LEN;
572 sprintf(p, "\tFlow Type: UDP\n");
573 p += ETH_GSTRING_LEN;
577 sprintf(p, "\tFlow Type: SCTP\n");
578 p += ETH_GSTRING_LEN;
582 sprintf(p, "\tFlow Type: AH ESP\n");
583 p += ETH_GSTRING_LEN;
587 sprintf(p, "\tFlow Type: ESP\n");
588 p += ETH_GSTRING_LEN;
592 sprintf(p, "\tFlow Type: Raw IP\n");
593 p += ETH_GSTRING_LEN;
597 sprintf(p, "\tFlow Type: IPv4\n");
598 p += ETH_GSTRING_LEN;
602 sprintf(p, "\tFlow Type: Unknown\n");
603 p += ETH_GSTRING_LEN;
608 /* now the rest of the filters */
609 switch (fsc->fs.flow_type) {
613 sprintf(p, "\tSrc IP addr: 0x%x\n",
614 fsc->fs.h_u.tcp_ip4_spec.ip4src);
615 p += ETH_GSTRING_LEN;
617 sprintf(p, "\tSrc IP mask: 0x%x\n",
618 fsc->fs.m_u.tcp_ip4_spec.ip4src);
619 p += ETH_GSTRING_LEN;
621 sprintf(p, "\tDest IP addr: 0x%x\n",
622 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
623 p += ETH_GSTRING_LEN;
625 sprintf(p, "\tDest IP mask: 0x%x\n",
626 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
627 p += ETH_GSTRING_LEN;
629 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
630 fsc->fs.h_u.tcp_ip4_spec.psrc,
631 fsc->fs.m_u.tcp_ip4_spec.psrc);
632 p += ETH_GSTRING_LEN;
634 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
635 fsc->fs.h_u.tcp_ip4_spec.pdst,
636 fsc->fs.m_u.tcp_ip4_spec.pdst);
637 p += ETH_GSTRING_LEN;
639 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
640 fsc->fs.h_u.tcp_ip4_spec.tos,
641 fsc->fs.m_u.tcp_ip4_spec.tos);
642 p += ETH_GSTRING_LEN;
647 sprintf(p, "\tSrc IP addr: 0x%x\n",
648 fsc->fs.h_u.ah_ip4_spec.ip4src);
649 p += ETH_GSTRING_LEN;
651 sprintf(p, "\tSrc IP mask: 0x%x\n",
652 fsc->fs.m_u.ah_ip4_spec.ip4src);
653 p += ETH_GSTRING_LEN;
655 sprintf(p, "\tDest IP addr: 0x%x\n",
656 fsc->fs.h_u.ah_ip4_spec.ip4dst);
657 p += ETH_GSTRING_LEN;
659 sprintf(p, "\tDest IP mask: 0x%x\n",
660 fsc->fs.m_u.ah_ip4_spec.ip4dst);
661 p += ETH_GSTRING_LEN;
663 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
664 fsc->fs.h_u.ah_ip4_spec.spi,
665 fsc->fs.m_u.ah_ip4_spec.spi);
666 p += ETH_GSTRING_LEN;
668 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
669 fsc->fs.h_u.ah_ip4_spec.tos,
670 fsc->fs.m_u.ah_ip4_spec.tos);
671 p += ETH_GSTRING_LEN;
675 sprintf(p, "\tSrc IP addr: 0x%x\n",
676 fsc->fs.h_u.raw_ip4_spec.ip4src);
677 p += ETH_GSTRING_LEN;
679 sprintf(p, "\tSrc IP mask: 0x%x\n",
680 fsc->fs.m_u.raw_ip4_spec.ip4src);
681 p += ETH_GSTRING_LEN;
683 sprintf(p, "\tDest IP addr: 0x%x\n",
684 fsc->fs.h_u.raw_ip4_spec.ip4dst);
685 p += ETH_GSTRING_LEN;
687 sprintf(p, "\tDest IP mask: 0x%x\n",
688 fsc->fs.m_u.raw_ip4_spec.ip4dst);
689 p += ETH_GSTRING_LEN;
693 sprintf(p, "\tSrc IP addr: 0x%x\n",
694 fsc->fs.h_u.usr_ip4_spec.ip4src);
695 p += ETH_GSTRING_LEN;
697 sprintf(p, "\tSrc IP mask: 0x%x\n",
698 fsc->fs.m_u.usr_ip4_spec.ip4src);
699 p += ETH_GSTRING_LEN;
701 sprintf(p, "\tDest IP addr: 0x%x\n",
702 fsc->fs.h_u.usr_ip4_spec.ip4dst);
703 p += ETH_GSTRING_LEN;
705 sprintf(p, "\tDest IP mask: 0x%x\n",
706 fsc->fs.m_u.usr_ip4_spec.ip4dst);
707 p += ETH_GSTRING_LEN;
709 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
710 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
711 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
712 p += ETH_GSTRING_LEN;
714 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
715 fsc->fs.h_u.usr_ip4_spec.tos,
716 fsc->fs.m_u.usr_ip4_spec.tos);
717 p += ETH_GSTRING_LEN;
719 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
720 fsc->fs.h_u.usr_ip4_spec.ip_ver,
721 fsc->fs.m_u.usr_ip4_spec.ip_ver);
722 p += ETH_GSTRING_LEN;
724 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
725 fsc->fs.h_u.usr_ip4_spec.proto,
726 fsc->fs.m_u.usr_ip4_spec.proto);
727 p += ETH_GSTRING_LEN;
731 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
732 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
733 p += ETH_GSTRING_LEN;
735 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
736 p += ETH_GSTRING_LEN;
738 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
739 p += ETH_GSTRING_LEN;
741 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
742 sprintf(p, "\tAction: Drop\n");
744 sprintf(p, "\tAction: Direct to queue %d\n",
746 p += ETH_GSTRING_LEN;
752 /* indicate to userspace how many strings we actually have */
753 gstrings.len = num_strings;
755 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
757 useraddr += sizeof(gstrings);
758 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
767 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
769 struct ethtool_regs regs;
770 const struct ethtool_ops *ops = dev->ethtool_ops;
774 if (!ops->get_regs || !ops->get_regs_len)
777 if (copy_from_user(®s, useraddr, sizeof(regs)))
780 reglen = ops->get_regs_len(dev);
781 if (regs.len > reglen)
784 regbuf = kmalloc(reglen, GFP_USER);
788 ops->get_regs(dev, ®s, regbuf);
791 if (copy_to_user(useraddr, ®s, sizeof(regs)))
793 useraddr += offsetof(struct ethtool_regs, data);
794 if (copy_to_user(useraddr, regbuf, regs.len))
803 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
805 struct ethtool_value reset;
808 if (!dev->ethtool_ops->reset)
811 if (copy_from_user(&reset, useraddr, sizeof(reset)))
814 ret = dev->ethtool_ops->reset(dev, &reset.data);
818 if (copy_to_user(useraddr, &reset, sizeof(reset)))
823 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
825 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
827 if (!dev->ethtool_ops->get_wol)
830 dev->ethtool_ops->get_wol(dev, &wol);
832 if (copy_to_user(useraddr, &wol, sizeof(wol)))
837 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
839 struct ethtool_wolinfo wol;
841 if (!dev->ethtool_ops->set_wol)
844 if (copy_from_user(&wol, useraddr, sizeof(wol)))
847 return dev->ethtool_ops->set_wol(dev, &wol);
850 static int ethtool_nway_reset(struct net_device *dev)
852 if (!dev->ethtool_ops->nway_reset)
855 return dev->ethtool_ops->nway_reset(dev);
858 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
860 struct ethtool_eeprom eeprom;
861 const struct ethtool_ops *ops = dev->ethtool_ops;
862 void __user *userbuf = useraddr + sizeof(eeprom);
867 if (!ops->get_eeprom || !ops->get_eeprom_len)
870 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
873 /* Check for wrap and zero */
874 if (eeprom.offset + eeprom.len <= eeprom.offset)
877 /* Check for exceeding total eeprom len */
878 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
881 data = kmalloc(PAGE_SIZE, GFP_USER);
885 bytes_remaining = eeprom.len;
886 while (bytes_remaining > 0) {
887 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
889 ret = ops->get_eeprom(dev, &eeprom, data);
892 if (copy_to_user(userbuf, data, eeprom.len)) {
896 userbuf += eeprom.len;
897 eeprom.offset += eeprom.len;
898 bytes_remaining -= eeprom.len;
901 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
902 eeprom.offset -= eeprom.len;
903 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
910 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
912 struct ethtool_eeprom eeprom;
913 const struct ethtool_ops *ops = dev->ethtool_ops;
914 void __user *userbuf = useraddr + sizeof(eeprom);
919 if (!ops->set_eeprom || !ops->get_eeprom_len)
922 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
925 /* Check for wrap and zero */
926 if (eeprom.offset + eeprom.len <= eeprom.offset)
929 /* Check for exceeding total eeprom len */
930 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
933 data = kmalloc(PAGE_SIZE, GFP_USER);
937 bytes_remaining = eeprom.len;
938 while (bytes_remaining > 0) {
939 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
941 if (copy_from_user(data, userbuf, eeprom.len)) {
945 ret = ops->set_eeprom(dev, &eeprom, data);
948 userbuf += eeprom.len;
949 eeprom.offset += eeprom.len;
950 bytes_remaining -= eeprom.len;
957 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
958 void __user *useraddr)
960 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
962 if (!dev->ethtool_ops->get_coalesce)
965 dev->ethtool_ops->get_coalesce(dev, &coalesce);
967 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
972 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
973 void __user *useraddr)
975 struct ethtool_coalesce coalesce;
977 if (!dev->ethtool_ops->set_coalesce)
980 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
983 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
986 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
988 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
990 if (!dev->ethtool_ops->get_ringparam)
993 dev->ethtool_ops->get_ringparam(dev, &ringparam);
995 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1000 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1002 struct ethtool_ringparam ringparam;
1004 if (!dev->ethtool_ops->set_ringparam)
1007 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1010 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1013 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1015 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1017 if (!dev->ethtool_ops->get_pauseparam)
1020 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1022 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1027 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1029 struct ethtool_pauseparam pauseparam;
1031 if (!dev->ethtool_ops->set_pauseparam)
1034 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1037 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1040 static int __ethtool_set_sg(struct net_device *dev, u32 data)
1044 if (!data && dev->ethtool_ops->set_tso) {
1045 err = dev->ethtool_ops->set_tso(dev, 0);
1050 if (!data && dev->ethtool_ops->set_ufo) {
1051 err = dev->ethtool_ops->set_ufo(dev, 0);
1055 return dev->ethtool_ops->set_sg(dev, data);
1058 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
1060 struct ethtool_value edata;
1063 if (!dev->ethtool_ops->set_tx_csum)
1066 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1069 if (!edata.data && dev->ethtool_ops->set_sg) {
1070 err = __ethtool_set_sg(dev, 0);
1075 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1077 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1079 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1081 struct ethtool_value edata;
1083 if (!dev->ethtool_ops->set_rx_csum)
1086 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1089 if (!edata.data && dev->ethtool_ops->set_sg)
1090 dev->features &= ~NETIF_F_GRO;
1092 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1095 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1097 struct ethtool_value edata;
1099 if (!dev->ethtool_ops->set_sg)
1102 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1106 !(dev->features & NETIF_F_ALL_CSUM))
1109 return __ethtool_set_sg(dev, edata.data);
1112 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1114 struct ethtool_value edata;
1116 if (!dev->ethtool_ops->set_tso)
1119 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1122 if (edata.data && !(dev->features & NETIF_F_SG))
1125 return dev->ethtool_ops->set_tso(dev, edata.data);
1128 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1130 struct ethtool_value edata;
1132 if (!dev->ethtool_ops->set_ufo)
1134 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1136 if (edata.data && !(dev->features & NETIF_F_SG))
1138 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1140 return dev->ethtool_ops->set_ufo(dev, edata.data);
1143 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1145 struct ethtool_value edata = { ETHTOOL_GGSO };
1147 edata.data = dev->features & NETIF_F_GSO;
1148 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1153 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1155 struct ethtool_value edata;
1157 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1160 dev->features |= NETIF_F_GSO;
1162 dev->features &= ~NETIF_F_GSO;
1166 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1168 struct ethtool_value edata = { ETHTOOL_GGRO };
1170 edata.data = dev->features & NETIF_F_GRO;
1171 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1176 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1178 struct ethtool_value edata;
1180 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1184 if (!dev->ethtool_ops->get_rx_csum ||
1185 !dev->ethtool_ops->get_rx_csum(dev))
1187 dev->features |= NETIF_F_GRO;
1189 dev->features &= ~NETIF_F_GRO;
1194 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1196 struct ethtool_test test;
1197 const struct ethtool_ops *ops = dev->ethtool_ops;
1201 if (!ops->self_test || !ops->get_sset_count)
1204 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1207 WARN_ON(test_len == 0);
1209 if (copy_from_user(&test, useraddr, sizeof(test)))
1212 test.len = test_len;
1213 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1217 ops->self_test(dev, &test, data);
1220 if (copy_to_user(useraddr, &test, sizeof(test)))
1222 useraddr += sizeof(test);
1223 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1232 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1234 struct ethtool_gstrings gstrings;
1235 const struct ethtool_ops *ops = dev->ethtool_ops;
1239 if (!ops->get_strings || !ops->get_sset_count)
1242 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1245 ret = ops->get_sset_count(dev, gstrings.string_set);
1251 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1255 ops->get_strings(dev, gstrings.string_set, data);
1258 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1260 useraddr += sizeof(gstrings);
1261 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1270 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1272 struct ethtool_value id;
1274 if (!dev->ethtool_ops->phys_id)
1277 if (copy_from_user(&id, useraddr, sizeof(id)))
1280 return dev->ethtool_ops->phys_id(dev, id.data);
1283 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1285 struct ethtool_stats stats;
1286 const struct ethtool_ops *ops = dev->ethtool_ops;
1290 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1293 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1296 WARN_ON(n_stats == 0);
1298 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1301 stats.n_stats = n_stats;
1302 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1306 ops->get_ethtool_stats(dev, &stats, data);
1309 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1311 useraddr += sizeof(stats);
1312 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1321 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1323 struct ethtool_perm_addr epaddr;
1325 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1328 if (epaddr.size < dev->addr_len)
1330 epaddr.size = dev->addr_len;
1332 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1334 useraddr += sizeof(epaddr);
1335 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1340 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1341 u32 cmd, u32 (*actor)(struct net_device *))
1343 struct ethtool_value edata = { .cmd = cmd };
1348 edata.data = actor(dev);
1350 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1355 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1356 void (*actor)(struct net_device *, u32))
1358 struct ethtool_value edata;
1363 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1366 actor(dev, edata.data);
1370 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1371 int (*actor)(struct net_device *, u32))
1373 struct ethtool_value edata;
1378 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1381 return actor(dev, edata.data);
1384 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1385 char __user *useraddr)
1387 struct ethtool_flash efl;
1389 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1392 if (!dev->ethtool_ops->flash_device)
1395 return dev->ethtool_ops->flash_device(dev, &efl);
1398 /* The main entry point in this file. Called from net/core/dev.c */
1400 int dev_ethtool(struct net *net, struct ifreq *ifr)
1402 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1403 void __user *useraddr = ifr->ifr_data;
1406 unsigned long old_features;
1408 if (!dev || !netif_device_present(dev))
1411 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1414 if (!dev->ethtool_ops) {
1415 /* ETHTOOL_GDRVINFO does not require any driver support.
1416 * It is also unprivileged and does not change anything,
1417 * so we can take a shortcut to it. */
1418 if (ethcmd == ETHTOOL_GDRVINFO)
1419 return ethtool_get_drvinfo(dev, useraddr);
1424 /* Allow some commands to be done by anyone */
1427 case ETHTOOL_GDRVINFO:
1428 case ETHTOOL_GMSGLVL:
1429 case ETHTOOL_GCOALESCE:
1430 case ETHTOOL_GRINGPARAM:
1431 case ETHTOOL_GPAUSEPARAM:
1432 case ETHTOOL_GRXCSUM:
1433 case ETHTOOL_GTXCSUM:
1435 case ETHTOOL_GSTRINGS:
1437 case ETHTOOL_GPERMADDR:
1441 case ETHTOOL_GFLAGS:
1442 case ETHTOOL_GPFLAGS:
1444 case ETHTOOL_GRXRINGS:
1445 case ETHTOOL_GRXCLSRLCNT:
1446 case ETHTOOL_GRXCLSRULE:
1447 case ETHTOOL_GRXCLSRLALL:
1450 if (!capable(CAP_NET_ADMIN))
1454 if (dev->ethtool_ops->begin) {
1455 rc = dev->ethtool_ops->begin(dev);
1459 old_features = dev->features;
1463 rc = ethtool_get_settings(dev, useraddr);
1466 rc = ethtool_set_settings(dev, useraddr);
1468 case ETHTOOL_GDRVINFO:
1469 rc = ethtool_get_drvinfo(dev, useraddr);
1472 rc = ethtool_get_regs(dev, useraddr);
1475 rc = ethtool_get_wol(dev, useraddr);
1478 rc = ethtool_set_wol(dev, useraddr);
1480 case ETHTOOL_GMSGLVL:
1481 rc = ethtool_get_value(dev, useraddr, ethcmd,
1482 dev->ethtool_ops->get_msglevel);
1484 case ETHTOOL_SMSGLVL:
1485 rc = ethtool_set_value_void(dev, useraddr,
1486 dev->ethtool_ops->set_msglevel);
1488 case ETHTOOL_NWAY_RST:
1489 rc = ethtool_nway_reset(dev);
1492 rc = ethtool_get_value(dev, useraddr, ethcmd,
1493 dev->ethtool_ops->get_link);
1495 case ETHTOOL_GEEPROM:
1496 rc = ethtool_get_eeprom(dev, useraddr);
1498 case ETHTOOL_SEEPROM:
1499 rc = ethtool_set_eeprom(dev, useraddr);
1501 case ETHTOOL_GCOALESCE:
1502 rc = ethtool_get_coalesce(dev, useraddr);
1504 case ETHTOOL_SCOALESCE:
1505 rc = ethtool_set_coalesce(dev, useraddr);
1507 case ETHTOOL_GRINGPARAM:
1508 rc = ethtool_get_ringparam(dev, useraddr);
1510 case ETHTOOL_SRINGPARAM:
1511 rc = ethtool_set_ringparam(dev, useraddr);
1513 case ETHTOOL_GPAUSEPARAM:
1514 rc = ethtool_get_pauseparam(dev, useraddr);
1516 case ETHTOOL_SPAUSEPARAM:
1517 rc = ethtool_set_pauseparam(dev, useraddr);
1519 case ETHTOOL_GRXCSUM:
1520 rc = ethtool_get_value(dev, useraddr, ethcmd,
1521 (dev->ethtool_ops->get_rx_csum ?
1522 dev->ethtool_ops->get_rx_csum :
1523 ethtool_op_get_rx_csum));
1525 case ETHTOOL_SRXCSUM:
1526 rc = ethtool_set_rx_csum(dev, useraddr);
1528 case ETHTOOL_GTXCSUM:
1529 rc = ethtool_get_value(dev, useraddr, ethcmd,
1530 (dev->ethtool_ops->get_tx_csum ?
1531 dev->ethtool_ops->get_tx_csum :
1532 ethtool_op_get_tx_csum));
1534 case ETHTOOL_STXCSUM:
1535 rc = ethtool_set_tx_csum(dev, useraddr);
1538 rc = ethtool_get_value(dev, useraddr, ethcmd,
1539 (dev->ethtool_ops->get_sg ?
1540 dev->ethtool_ops->get_sg :
1541 ethtool_op_get_sg));
1544 rc = ethtool_set_sg(dev, useraddr);
1547 rc = ethtool_get_value(dev, useraddr, ethcmd,
1548 (dev->ethtool_ops->get_tso ?
1549 dev->ethtool_ops->get_tso :
1550 ethtool_op_get_tso));
1553 rc = ethtool_set_tso(dev, useraddr);
1556 rc = ethtool_self_test(dev, useraddr);
1558 case ETHTOOL_GSTRINGS:
1559 rc = ethtool_get_strings(dev, useraddr);
1561 case ETHTOOL_PHYS_ID:
1562 rc = ethtool_phys_id(dev, useraddr);
1564 case ETHTOOL_GSTATS:
1565 rc = ethtool_get_stats(dev, useraddr);
1567 case ETHTOOL_GPERMADDR:
1568 rc = ethtool_get_perm_addr(dev, useraddr);
1571 rc = ethtool_get_value(dev, useraddr, ethcmd,
1572 (dev->ethtool_ops->get_ufo ?
1573 dev->ethtool_ops->get_ufo :
1574 ethtool_op_get_ufo));
1577 rc = ethtool_set_ufo(dev, useraddr);
1580 rc = ethtool_get_gso(dev, useraddr);
1583 rc = ethtool_set_gso(dev, useraddr);
1585 case ETHTOOL_GFLAGS:
1586 rc = ethtool_get_value(dev, useraddr, ethcmd,
1587 (dev->ethtool_ops->get_flags ?
1588 dev->ethtool_ops->get_flags :
1589 ethtool_op_get_flags));
1591 case ETHTOOL_SFLAGS:
1592 rc = ethtool_set_value(dev, useraddr,
1593 dev->ethtool_ops->set_flags);
1595 case ETHTOOL_GPFLAGS:
1596 rc = ethtool_get_value(dev, useraddr, ethcmd,
1597 dev->ethtool_ops->get_priv_flags);
1599 case ETHTOOL_SPFLAGS:
1600 rc = ethtool_set_value(dev, useraddr,
1601 dev->ethtool_ops->set_priv_flags);
1604 case ETHTOOL_GRXRINGS:
1605 case ETHTOOL_GRXCLSRLCNT:
1606 case ETHTOOL_GRXCLSRULE:
1607 case ETHTOOL_GRXCLSRLALL:
1608 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1611 case ETHTOOL_SRXCLSRLDEL:
1612 case ETHTOOL_SRXCLSRLINS:
1613 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1616 rc = ethtool_get_gro(dev, useraddr);
1619 rc = ethtool_set_gro(dev, useraddr);
1621 case ETHTOOL_FLASHDEV:
1622 rc = ethtool_flash_device(dev, useraddr);
1625 rc = ethtool_reset(dev, useraddr);
1627 case ETHTOOL_SRXNTUPLE:
1628 rc = ethtool_set_rx_ntuple(dev, useraddr);
1630 case ETHTOOL_GRXNTUPLE:
1631 rc = ethtool_get_rx_ntuple(dev, useraddr);
1633 case ETHTOOL_GSSET_INFO:
1634 rc = ethtool_get_sset_info(dev, useraddr);
1636 case ETHTOOL_GRXFHINDIR:
1637 rc = ethtool_get_rxfh_indir(dev, useraddr);
1639 case ETHTOOL_SRXFHINDIR:
1640 rc = ethtool_set_rxfh_indir(dev, useraddr);
1646 if (dev->ethtool_ops->complete)
1647 dev->ethtool_ops->complete(dev);
1649 if (old_features != dev->features)
1650 netdev_features_change(dev);