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>
24 #include <linux/rtnetlink.h>
25 #include <linux/sched.h>
28 * Some useful ethtool_ops methods that're device independent.
29 * If we find that all drivers want to do the same thing here,
30 * we can turn these into dev_() function calls.
33 u32 ethtool_op_get_link(struct net_device *dev)
35 return netif_carrier_ok(dev) ? 1 : 0;
37 EXPORT_SYMBOL(ethtool_op_get_link);
39 u32 ethtool_op_get_tx_csum(struct net_device *dev)
41 return (dev->features & NETIF_F_ALL_CSUM) != 0;
43 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
45 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
48 dev->features |= NETIF_F_IP_CSUM;
50 dev->features &= ~NETIF_F_IP_CSUM;
54 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
56 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
59 dev->features |= NETIF_F_HW_CSUM;
61 dev->features &= ~NETIF_F_HW_CSUM;
65 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
67 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
70 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
72 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
76 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
78 u32 ethtool_op_get_sg(struct net_device *dev)
80 return (dev->features & NETIF_F_SG) != 0;
82 EXPORT_SYMBOL(ethtool_op_get_sg);
84 int ethtool_op_set_sg(struct net_device *dev, u32 data)
87 dev->features |= NETIF_F_SG;
89 dev->features &= ~NETIF_F_SG;
93 EXPORT_SYMBOL(ethtool_op_set_sg);
95 u32 ethtool_op_get_tso(struct net_device *dev)
97 return (dev->features & NETIF_F_TSO) != 0;
99 EXPORT_SYMBOL(ethtool_op_get_tso);
101 int ethtool_op_set_tso(struct net_device *dev, u32 data)
104 dev->features |= NETIF_F_TSO;
106 dev->features &= ~NETIF_F_TSO;
110 EXPORT_SYMBOL(ethtool_op_set_tso);
112 u32 ethtool_op_get_ufo(struct net_device *dev)
114 return (dev->features & NETIF_F_UFO) != 0;
116 EXPORT_SYMBOL(ethtool_op_get_ufo);
118 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
121 dev->features |= NETIF_F_UFO;
123 dev->features &= ~NETIF_F_UFO;
126 EXPORT_SYMBOL(ethtool_op_set_ufo);
128 /* the following list of flags are the same as their associated
129 * NETIF_F_xxx values in include/linux/netdevice.h
131 static const u32 flags_dup_features =
132 (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
135 u32 ethtool_op_get_flags(struct net_device *dev)
137 /* in the future, this function will probably contain additional
138 * handling for flags which are not so easily handled
139 * by a simple masking operation
142 return dev->features & flags_dup_features;
144 EXPORT_SYMBOL(ethtool_op_get_flags);
146 /* Check if device can enable (or disable) particular feature coded in "data"
147 * argument. Flags "supported" describe features that can be toggled by device.
148 * If feature can not be toggled, it state (enabled or disabled) must match
149 * hardcoded device features state, otherwise flags are marked as invalid.
151 bool ethtool_invalid_flags(struct net_device *dev, u32 data, u32 supported)
153 u32 features = dev->features & flags_dup_features;
154 /* "data" can contain only flags_dup_features bits,
155 * see __ethtool_set_flags */
157 return (features & ~supported) != (data & ~supported);
159 EXPORT_SYMBOL(ethtool_invalid_flags);
161 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
163 if (ethtool_invalid_flags(dev, data, supported))
166 dev->features = ((dev->features & ~flags_dup_features) |
167 (data & flags_dup_features));
170 EXPORT_SYMBOL(ethtool_op_set_flags);
172 void ethtool_ntuple_flush(struct net_device *dev)
174 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
176 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
177 list_del(&fsc->list);
180 dev->ethtool_ntuple_list.count = 0;
182 EXPORT_SYMBOL(ethtool_ntuple_flush);
184 /* Handlers for each ethtool command */
186 #define ETHTOOL_DEV_FEATURE_WORDS 1
188 static void ethtool_get_features_compat(struct net_device *dev,
189 struct ethtool_get_features_block *features)
191 if (!dev->ethtool_ops)
194 /* getting RX checksum */
195 if (dev->ethtool_ops->get_rx_csum)
196 if (dev->ethtool_ops->get_rx_csum(dev))
197 features[0].active |= NETIF_F_RXCSUM;
199 /* mark legacy-changeable features */
200 if (dev->ethtool_ops->set_sg)
201 features[0].available |= NETIF_F_SG;
202 if (dev->ethtool_ops->set_tx_csum)
203 features[0].available |= NETIF_F_ALL_CSUM;
204 if (dev->ethtool_ops->set_tso)
205 features[0].available |= NETIF_F_ALL_TSO;
206 if (dev->ethtool_ops->set_rx_csum)
207 features[0].available |= NETIF_F_RXCSUM;
208 if (dev->ethtool_ops->set_flags)
209 features[0].available |= flags_dup_features;
212 static int ethtool_set_feature_compat(struct net_device *dev,
213 int (*legacy_set)(struct net_device *, u32),
214 struct ethtool_set_features_block *features, u32 mask)
221 if (!(features[0].valid & mask))
224 features[0].valid &= ~mask;
226 do_set = !!(features[0].requested & mask);
228 if (legacy_set(dev, do_set) < 0)
230 "Legacy feature change (%s) failed for 0x%08x\n",
231 do_set ? "set" : "clear", mask);
236 static int ethtool_set_flags_compat(struct net_device *dev,
237 int (*legacy_set)(struct net_device *, u32),
238 struct ethtool_set_features_block *features, u32 mask)
245 if (!(features[0].valid & mask))
248 value = dev->features & ~features[0].valid;
249 value |= features[0].requested;
251 features[0].valid &= ~mask;
253 if (legacy_set(dev, value & mask) < 0)
254 netdev_info(dev, "Legacy flags change failed\n");
259 static int ethtool_set_features_compat(struct net_device *dev,
260 struct ethtool_set_features_block *features)
264 if (!dev->ethtool_ops)
267 compat = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
268 features, NETIF_F_SG);
269 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
270 features, NETIF_F_ALL_CSUM);
271 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
272 features, NETIF_F_ALL_TSO);
273 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
274 features, NETIF_F_RXCSUM);
275 compat |= ethtool_set_flags_compat(dev, dev->ethtool_ops->set_flags,
276 features, flags_dup_features);
281 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
283 struct ethtool_gfeatures cmd = {
284 .cmd = ETHTOOL_GFEATURES,
285 .size = ETHTOOL_DEV_FEATURE_WORDS,
287 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
289 .available = dev->hw_features,
290 .requested = dev->wanted_features,
291 .active = dev->features,
292 .never_changed = NETIF_F_NEVER_CHANGE,
295 u32 __user *sizeaddr;
298 ethtool_get_features_compat(dev, features);
300 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
301 if (get_user(copy_size, sizeaddr))
304 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
305 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
307 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
309 useraddr += sizeof(cmd);
310 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
316 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
318 struct ethtool_sfeatures cmd;
319 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
322 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
324 useraddr += sizeof(cmd);
326 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
329 if (copy_from_user(features, useraddr, sizeof(features)))
332 if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
335 if (ethtool_set_features_compat(dev, features))
336 ret |= ETHTOOL_F_COMPAT;
338 if (features[0].valid & ~dev->hw_features) {
339 features[0].valid &= dev->hw_features;
340 ret |= ETHTOOL_F_UNSUPPORTED;
343 dev->wanted_features &= ~features[0].valid;
344 dev->wanted_features |= features[0].valid & features[0].requested;
345 __netdev_update_features(dev);
347 if ((dev->wanted_features ^ dev->features) & features[0].valid)
348 ret |= ETHTOOL_F_WISH;
353 static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
354 /* NETIF_F_SG */ "tx-scatter-gather",
355 /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
356 /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
357 /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
358 /* NETIF_F_IPV6_CSUM */ "tx-checksum-ipv6",
359 /* NETIF_F_HIGHDMA */ "highdma",
360 /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
361 /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
363 /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
364 /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
365 /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
366 /* NETIF_F_GSO */ "tx-generic-segmentation",
367 /* NETIF_F_LLTX */ "tx-lockless",
368 /* NETIF_F_NETNS_LOCAL */ "netns-local",
369 /* NETIF_F_GRO */ "rx-gro",
370 /* NETIF_F_LRO */ "rx-lro",
372 /* NETIF_F_TSO */ "tx-tcp-segmentation",
373 /* NETIF_F_UFO */ "tx-udp-fragmentation",
374 /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
375 /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
376 /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
377 /* NETIF_F_FSO */ "tx-fcoe-segmentation",
381 /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
382 /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
383 /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
384 /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
385 /* NETIF_F_RXHASH */ "rx-hashing",
386 /* NETIF_F_RXCSUM */ "rx-checksum",
387 /* NETIF_F_NOCACHE_COPY */ "tx-nocache-copy",
388 /* NETIF_F_LOOPBACK */ "loopback",
391 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
393 const struct ethtool_ops *ops = dev->ethtool_ops;
395 if (sset == ETH_SS_FEATURES)
396 return ARRAY_SIZE(netdev_features_strings);
398 if (ops && ops->get_sset_count && ops->get_strings)
399 return ops->get_sset_count(dev, sset);
404 static void __ethtool_get_strings(struct net_device *dev,
405 u32 stringset, u8 *data)
407 const struct ethtool_ops *ops = dev->ethtool_ops;
409 if (stringset == ETH_SS_FEATURES)
410 memcpy(data, netdev_features_strings,
411 sizeof(netdev_features_strings));
413 /* ops->get_strings is valid because checked earlier */
414 ops->get_strings(dev, stringset, data);
417 static u32 ethtool_get_feature_mask(u32 eth_cmd)
419 /* feature masks of legacy discrete ethtool ops */
422 case ETHTOOL_GTXCSUM:
423 case ETHTOOL_STXCSUM:
424 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
425 case ETHTOOL_GRXCSUM:
426 case ETHTOOL_SRXCSUM:
427 return NETIF_F_RXCSUM;
433 return NETIF_F_ALL_TSO;
448 static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
450 const struct ethtool_ops *ops = dev->ethtool_ops;
456 case ETHTOOL_GTXCSUM:
457 return ops->get_tx_csum;
458 case ETHTOOL_GRXCSUM:
459 return ops->get_rx_csum;
471 static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
473 return !!(dev->features & NETIF_F_ALL_CSUM);
476 static int ethtool_get_one_feature(struct net_device *dev,
477 char __user *useraddr, u32 ethcmd)
479 u32 mask = ethtool_get_feature_mask(ethcmd);
480 struct ethtool_value edata = {
482 .data = !!(dev->features & mask),
485 /* compatibility with discrete get_ ops */
486 if (!(dev->hw_features & mask)) {
487 u32 (*actor)(struct net_device *);
489 actor = __ethtool_get_one_feature_actor(dev, ethcmd);
491 /* bug compatibility with old get_rx_csum */
492 if (ethcmd == ETHTOOL_GRXCSUM && !actor)
493 actor = __ethtool_get_rx_csum_oldbug;
496 edata.data = actor(dev);
499 if (copy_to_user(useraddr, &edata, sizeof(edata)))
504 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
505 static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
506 static int __ethtool_set_sg(struct net_device *dev, u32 data);
507 static int __ethtool_set_tso(struct net_device *dev, u32 data);
508 static int __ethtool_set_ufo(struct net_device *dev, u32 data);
510 static int ethtool_set_one_feature(struct net_device *dev,
511 void __user *useraddr, u32 ethcmd)
513 struct ethtool_value edata;
516 if (copy_from_user(&edata, useraddr, sizeof(edata)))
519 mask = ethtool_get_feature_mask(ethcmd);
520 mask &= dev->hw_features;
523 dev->wanted_features |= mask;
525 dev->wanted_features &= ~mask;
527 __netdev_update_features(dev);
531 /* Driver is not converted to ndo_fix_features or does not
532 * support changing this offload. In the latter case it won't
533 * have corresponding ethtool_ops field set.
535 * Following part is to be removed after all drivers advertise
536 * their changeable features in netdev->hw_features and stop
537 * using discrete offload setting ops.
541 case ETHTOOL_STXCSUM:
542 return __ethtool_set_tx_csum(dev, edata.data);
543 case ETHTOOL_SRXCSUM:
544 return __ethtool_set_rx_csum(dev, edata.data);
546 return __ethtool_set_sg(dev, edata.data);
548 return __ethtool_set_tso(dev, edata.data);
550 return __ethtool_set_ufo(dev, edata.data);
556 int __ethtool_set_flags(struct net_device *dev, u32 data)
560 if (data & ~flags_dup_features)
563 /* legacy set_flags() op */
564 if (dev->ethtool_ops->set_flags) {
565 if (unlikely(dev->hw_features & flags_dup_features))
567 "driver BUG: mixed hw_features and set_flags()\n");
568 return dev->ethtool_ops->set_flags(dev, data);
571 /* allow changing only bits set in hw_features */
572 changed = (data ^ dev->features) & flags_dup_features;
573 if (changed & ~dev->hw_features)
574 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
576 dev->wanted_features =
577 (dev->wanted_features & ~changed) | (data & dev->hw_features);
579 __netdev_update_features(dev);
584 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
586 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
589 if (!dev->ethtool_ops->get_settings)
592 err = dev->ethtool_ops->get_settings(dev, &cmd);
596 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
601 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
603 struct ethtool_cmd cmd;
605 if (!dev->ethtool_ops->set_settings)
608 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
611 return dev->ethtool_ops->set_settings(dev, &cmd);
614 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
615 void __user *useraddr)
617 struct ethtool_drvinfo info;
618 const struct ethtool_ops *ops = dev->ethtool_ops;
620 memset(&info, 0, sizeof(info));
621 info.cmd = ETHTOOL_GDRVINFO;
622 if (ops && ops->get_drvinfo) {
623 ops->get_drvinfo(dev, &info);
624 } else if (dev->dev.parent && dev->dev.parent->driver) {
625 strlcpy(info.bus_info, dev_name(dev->dev.parent),
626 sizeof(info.bus_info));
627 strlcpy(info.driver, dev->dev.parent->driver->name,
628 sizeof(info.driver));
634 * this method of obtaining string set info is deprecated;
635 * Use ETHTOOL_GSSET_INFO instead.
637 if (ops && ops->get_sset_count) {
640 rc = ops->get_sset_count(dev, ETH_SS_TEST);
642 info.testinfo_len = rc;
643 rc = ops->get_sset_count(dev, ETH_SS_STATS);
646 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
648 info.n_priv_flags = rc;
650 if (ops && ops->get_regs_len)
651 info.regdump_len = ops->get_regs_len(dev);
652 if (ops && ops->get_eeprom_len)
653 info.eedump_len = ops->get_eeprom_len(dev);
655 if (copy_to_user(useraddr, &info, sizeof(info)))
660 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
661 void __user *useraddr)
663 struct ethtool_sset_info info;
665 int i, idx = 0, n_bits = 0, ret, rc;
666 u32 *info_buf = NULL;
668 if (copy_from_user(&info, useraddr, sizeof(info)))
671 /* store copy of mask, because we zero struct later on */
672 sset_mask = info.sset_mask;
676 /* calculate size of return buffer */
677 n_bits = hweight64(sset_mask);
679 memset(&info, 0, sizeof(info));
680 info.cmd = ETHTOOL_GSSET_INFO;
682 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
687 * fill return buffer based on input bitmask and successful
688 * get_sset_count return
690 for (i = 0; i < 64; i++) {
691 if (!(sset_mask & (1ULL << i)))
694 rc = __ethtool_get_sset_count(dev, i);
696 info.sset_mask |= (1ULL << i);
697 info_buf[idx++] = rc;
702 if (copy_to_user(useraddr, &info, sizeof(info)))
705 useraddr += offsetof(struct ethtool_sset_info, data);
706 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
716 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
717 u32 cmd, void __user *useraddr)
719 struct ethtool_rxnfc info;
720 size_t info_size = sizeof(info);
722 if (!dev->ethtool_ops->set_rxnfc)
725 /* struct ethtool_rxnfc was originally defined for
726 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
727 * members. User-space might still be using that
729 if (cmd == ETHTOOL_SRXFH)
730 info_size = (offsetof(struct ethtool_rxnfc, data) +
733 if (copy_from_user(&info, useraddr, info_size))
736 return dev->ethtool_ops->set_rxnfc(dev, &info);
739 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
740 u32 cmd, void __user *useraddr)
742 struct ethtool_rxnfc info;
743 size_t info_size = sizeof(info);
744 const struct ethtool_ops *ops = dev->ethtool_ops;
746 void *rule_buf = NULL;
751 /* struct ethtool_rxnfc was originally defined for
752 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
753 * members. User-space might still be using that
755 if (cmd == ETHTOOL_GRXFH)
756 info_size = (offsetof(struct ethtool_rxnfc, data) +
759 if (copy_from_user(&info, useraddr, info_size))
762 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
763 if (info.rule_cnt > 0) {
764 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
765 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
772 ret = ops->get_rxnfc(dev, &info, rule_buf);
777 if (copy_to_user(useraddr, &info, info_size))
781 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
782 if (copy_to_user(useraddr, rule_buf,
783 info.rule_cnt * sizeof(u32)))
794 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
795 void __user *useraddr)
797 struct ethtool_rxfh_indir *indir;
802 if (!dev->ethtool_ops->get_rxfh_indir)
805 if (copy_from_user(&table_size,
806 useraddr + offsetof(struct ethtool_rxfh_indir, size),
811 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
813 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
814 indir = kzalloc(full_size, GFP_USER);
818 indir->cmd = ETHTOOL_GRXFHINDIR;
819 indir->size = table_size;
820 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
824 if (copy_to_user(useraddr, indir, full_size))
832 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
833 void __user *useraddr)
835 struct ethtool_rxfh_indir *indir;
840 if (!dev->ethtool_ops->set_rxfh_indir)
843 if (copy_from_user(&table_size,
844 useraddr + offsetof(struct ethtool_rxfh_indir, size),
849 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
851 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
852 indir = kmalloc(full_size, GFP_USER);
856 if (copy_from_user(indir, useraddr, full_size)) {
861 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
868 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
869 struct ethtool_rx_ntuple_flow_spec *spec,
870 struct ethtool_rx_ntuple_flow_spec_container *fsc)
873 /* don't add filters forever */
874 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
875 /* free the container */
880 /* Copy the whole filter over */
881 fsc->fs.flow_type = spec->flow_type;
882 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
883 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
885 fsc->fs.vlan_tag = spec->vlan_tag;
886 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
887 fsc->fs.data = spec->data;
888 fsc->fs.data_mask = spec->data_mask;
889 fsc->fs.action = spec->action;
891 /* add to the list */
892 list_add_tail_rcu(&fsc->list, &list->list);
897 * ethtool does not (or did not) set masks for flow parameters that are
898 * not specified, so if both value and mask are 0 then this must be
899 * treated as equivalent to a mask with all bits set. Implement that
900 * here rather than in drivers.
902 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
904 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
905 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
907 if (fs->flow_type != TCP_V4_FLOW &&
908 fs->flow_type != UDP_V4_FLOW &&
909 fs->flow_type != SCTP_V4_FLOW)
912 if (!(entry->ip4src | mask->ip4src))
913 mask->ip4src = htonl(0xffffffff);
914 if (!(entry->ip4dst | mask->ip4dst))
915 mask->ip4dst = htonl(0xffffffff);
916 if (!(entry->psrc | mask->psrc))
917 mask->psrc = htons(0xffff);
918 if (!(entry->pdst | mask->pdst))
919 mask->pdst = htons(0xffff);
920 if (!(entry->tos | mask->tos))
922 if (!(fs->vlan_tag | fs->vlan_tag_mask))
923 fs->vlan_tag_mask = 0xffff;
924 if (!(fs->data | fs->data_mask))
925 fs->data_mask = 0xffffffffffffffffULL;
928 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
929 void __user *useraddr)
931 struct ethtool_rx_ntuple cmd;
932 const struct ethtool_ops *ops = dev->ethtool_ops;
933 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
936 if (!ops->set_rx_ntuple)
939 if (!(dev->features & NETIF_F_NTUPLE))
942 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
945 rx_ntuple_fix_masks(&cmd.fs);
948 * Cache filter in dev struct for GET operation only if
949 * the underlying driver doesn't have its own GET operation, and
950 * only if the filter was added successfully. First make sure we
951 * can allocate the filter, then continue if successful.
953 if (!ops->get_rx_ntuple) {
954 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
959 ret = ops->set_rx_ntuple(dev, &cmd);
965 if (!ops->get_rx_ntuple)
966 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
971 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
973 struct ethtool_gstrings gstrings;
974 const struct ethtool_ops *ops = dev->ethtool_ops;
975 struct ethtool_rx_ntuple_flow_spec_container *fsc;
978 int ret, i, num_strings = 0;
980 if (!ops->get_sset_count)
983 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
986 ret = ops->get_sset_count(dev, gstrings.string_set);
992 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
996 if (ops->get_rx_ntuple) {
997 /* driver-specific filter grab */
998 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
1002 /* default ethtool filter grab */
1005 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
1006 sprintf(p, "Filter %d:\n", i);
1007 p += ETH_GSTRING_LEN;
1010 switch (fsc->fs.flow_type) {
1012 sprintf(p, "\tFlow Type: TCP\n");
1013 p += ETH_GSTRING_LEN;
1017 sprintf(p, "\tFlow Type: UDP\n");
1018 p += ETH_GSTRING_LEN;
1022 sprintf(p, "\tFlow Type: SCTP\n");
1023 p += ETH_GSTRING_LEN;
1026 case AH_ESP_V4_FLOW:
1027 sprintf(p, "\tFlow Type: AH ESP\n");
1028 p += ETH_GSTRING_LEN;
1032 sprintf(p, "\tFlow Type: ESP\n");
1033 p += ETH_GSTRING_LEN;
1037 sprintf(p, "\tFlow Type: Raw IP\n");
1038 p += ETH_GSTRING_LEN;
1042 sprintf(p, "\tFlow Type: IPv4\n");
1043 p += ETH_GSTRING_LEN;
1047 sprintf(p, "\tFlow Type: Unknown\n");
1048 p += ETH_GSTRING_LEN;
1050 goto unknown_filter;
1053 /* now the rest of the filters */
1054 switch (fsc->fs.flow_type) {
1058 sprintf(p, "\tSrc IP addr: 0x%x\n",
1059 fsc->fs.h_u.tcp_ip4_spec.ip4src);
1060 p += ETH_GSTRING_LEN;
1062 sprintf(p, "\tSrc IP mask: 0x%x\n",
1063 fsc->fs.m_u.tcp_ip4_spec.ip4src);
1064 p += ETH_GSTRING_LEN;
1066 sprintf(p, "\tDest IP addr: 0x%x\n",
1067 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
1068 p += ETH_GSTRING_LEN;
1070 sprintf(p, "\tDest IP mask: 0x%x\n",
1071 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
1072 p += ETH_GSTRING_LEN;
1074 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
1075 fsc->fs.h_u.tcp_ip4_spec.psrc,
1076 fsc->fs.m_u.tcp_ip4_spec.psrc);
1077 p += ETH_GSTRING_LEN;
1079 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
1080 fsc->fs.h_u.tcp_ip4_spec.pdst,
1081 fsc->fs.m_u.tcp_ip4_spec.pdst);
1082 p += ETH_GSTRING_LEN;
1084 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
1085 fsc->fs.h_u.tcp_ip4_spec.tos,
1086 fsc->fs.m_u.tcp_ip4_spec.tos);
1087 p += ETH_GSTRING_LEN;
1090 case AH_ESP_V4_FLOW:
1092 sprintf(p, "\tSrc IP addr: 0x%x\n",
1093 fsc->fs.h_u.ah_ip4_spec.ip4src);
1094 p += ETH_GSTRING_LEN;
1096 sprintf(p, "\tSrc IP mask: 0x%x\n",
1097 fsc->fs.m_u.ah_ip4_spec.ip4src);
1098 p += ETH_GSTRING_LEN;
1100 sprintf(p, "\tDest IP addr: 0x%x\n",
1101 fsc->fs.h_u.ah_ip4_spec.ip4dst);
1102 p += ETH_GSTRING_LEN;
1104 sprintf(p, "\tDest IP mask: 0x%x\n",
1105 fsc->fs.m_u.ah_ip4_spec.ip4dst);
1106 p += ETH_GSTRING_LEN;
1108 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
1109 fsc->fs.h_u.ah_ip4_spec.spi,
1110 fsc->fs.m_u.ah_ip4_spec.spi);
1111 p += ETH_GSTRING_LEN;
1113 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
1114 fsc->fs.h_u.ah_ip4_spec.tos,
1115 fsc->fs.m_u.ah_ip4_spec.tos);
1116 p += ETH_GSTRING_LEN;
1120 sprintf(p, "\tSrc IP addr: 0x%x\n",
1121 fsc->fs.h_u.usr_ip4_spec.ip4src);
1122 p += ETH_GSTRING_LEN;
1124 sprintf(p, "\tSrc IP mask: 0x%x\n",
1125 fsc->fs.m_u.usr_ip4_spec.ip4src);
1126 p += ETH_GSTRING_LEN;
1128 sprintf(p, "\tDest IP addr: 0x%x\n",
1129 fsc->fs.h_u.usr_ip4_spec.ip4dst);
1130 p += ETH_GSTRING_LEN;
1132 sprintf(p, "\tDest IP mask: 0x%x\n",
1133 fsc->fs.m_u.usr_ip4_spec.ip4dst);
1134 p += ETH_GSTRING_LEN;
1138 sprintf(p, "\tSrc IP addr: 0x%x\n",
1139 fsc->fs.h_u.usr_ip4_spec.ip4src);
1140 p += ETH_GSTRING_LEN;
1142 sprintf(p, "\tSrc IP mask: 0x%x\n",
1143 fsc->fs.m_u.usr_ip4_spec.ip4src);
1144 p += ETH_GSTRING_LEN;
1146 sprintf(p, "\tDest IP addr: 0x%x\n",
1147 fsc->fs.h_u.usr_ip4_spec.ip4dst);
1148 p += ETH_GSTRING_LEN;
1150 sprintf(p, "\tDest IP mask: 0x%x\n",
1151 fsc->fs.m_u.usr_ip4_spec.ip4dst);
1152 p += ETH_GSTRING_LEN;
1154 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
1155 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
1156 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
1157 p += ETH_GSTRING_LEN;
1159 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
1160 fsc->fs.h_u.usr_ip4_spec.tos,
1161 fsc->fs.m_u.usr_ip4_spec.tos);
1162 p += ETH_GSTRING_LEN;
1164 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
1165 fsc->fs.h_u.usr_ip4_spec.ip_ver,
1166 fsc->fs.m_u.usr_ip4_spec.ip_ver);
1167 p += ETH_GSTRING_LEN;
1169 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
1170 fsc->fs.h_u.usr_ip4_spec.proto,
1171 fsc->fs.m_u.usr_ip4_spec.proto);
1172 p += ETH_GSTRING_LEN;
1176 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
1177 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
1178 p += ETH_GSTRING_LEN;
1180 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
1181 p += ETH_GSTRING_LEN;
1183 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
1184 p += ETH_GSTRING_LEN;
1186 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
1187 sprintf(p, "\tAction: Drop\n");
1189 sprintf(p, "\tAction: Direct to queue %d\n",
1191 p += ETH_GSTRING_LEN;
1197 /* indicate to userspace how many strings we actually have */
1198 gstrings.len = num_strings;
1200 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1202 useraddr += sizeof(gstrings);
1203 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1212 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1214 struct ethtool_regs regs;
1215 const struct ethtool_ops *ops = dev->ethtool_ops;
1219 if (!ops->get_regs || !ops->get_regs_len)
1222 if (copy_from_user(®s, useraddr, sizeof(regs)))
1225 reglen = ops->get_regs_len(dev);
1226 if (regs.len > reglen)
1229 regbuf = vzalloc(reglen);
1233 ops->get_regs(dev, ®s, regbuf);
1236 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1238 useraddr += offsetof(struct ethtool_regs, data);
1239 if (copy_to_user(useraddr, regbuf, regs.len))
1248 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1250 struct ethtool_value reset;
1253 if (!dev->ethtool_ops->reset)
1256 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1259 ret = dev->ethtool_ops->reset(dev, &reset.data);
1263 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1268 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1270 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1272 if (!dev->ethtool_ops->get_wol)
1275 dev->ethtool_ops->get_wol(dev, &wol);
1277 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1282 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1284 struct ethtool_wolinfo wol;
1286 if (!dev->ethtool_ops->set_wol)
1289 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1292 return dev->ethtool_ops->set_wol(dev, &wol);
1295 static int ethtool_nway_reset(struct net_device *dev)
1297 if (!dev->ethtool_ops->nway_reset)
1300 return dev->ethtool_ops->nway_reset(dev);
1303 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1305 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1307 if (!dev->ethtool_ops->get_link)
1310 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1312 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1317 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1319 struct ethtool_eeprom eeprom;
1320 const struct ethtool_ops *ops = dev->ethtool_ops;
1321 void __user *userbuf = useraddr + sizeof(eeprom);
1322 u32 bytes_remaining;
1326 if (!ops->get_eeprom || !ops->get_eeprom_len)
1329 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1332 /* Check for wrap and zero */
1333 if (eeprom.offset + eeprom.len <= eeprom.offset)
1336 /* Check for exceeding total eeprom len */
1337 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1340 data = kmalloc(PAGE_SIZE, GFP_USER);
1344 bytes_remaining = eeprom.len;
1345 while (bytes_remaining > 0) {
1346 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1348 ret = ops->get_eeprom(dev, &eeprom, data);
1351 if (copy_to_user(userbuf, data, eeprom.len)) {
1355 userbuf += eeprom.len;
1356 eeprom.offset += eeprom.len;
1357 bytes_remaining -= eeprom.len;
1360 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1361 eeprom.offset -= eeprom.len;
1362 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1369 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1371 struct ethtool_eeprom eeprom;
1372 const struct ethtool_ops *ops = dev->ethtool_ops;
1373 void __user *userbuf = useraddr + sizeof(eeprom);
1374 u32 bytes_remaining;
1378 if (!ops->set_eeprom || !ops->get_eeprom_len)
1381 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1384 /* Check for wrap and zero */
1385 if (eeprom.offset + eeprom.len <= eeprom.offset)
1388 /* Check for exceeding total eeprom len */
1389 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1392 data = kmalloc(PAGE_SIZE, GFP_USER);
1396 bytes_remaining = eeprom.len;
1397 while (bytes_remaining > 0) {
1398 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1400 if (copy_from_user(data, userbuf, eeprom.len)) {
1404 ret = ops->set_eeprom(dev, &eeprom, data);
1407 userbuf += eeprom.len;
1408 eeprom.offset += eeprom.len;
1409 bytes_remaining -= eeprom.len;
1416 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1417 void __user *useraddr)
1419 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1421 if (!dev->ethtool_ops->get_coalesce)
1424 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1426 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1431 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1432 void __user *useraddr)
1434 struct ethtool_coalesce coalesce;
1436 if (!dev->ethtool_ops->set_coalesce)
1439 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1442 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1445 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1447 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1449 if (!dev->ethtool_ops->get_ringparam)
1452 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1454 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1459 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1461 struct ethtool_ringparam ringparam;
1463 if (!dev->ethtool_ops->set_ringparam)
1466 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1469 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1472 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1473 void __user *useraddr)
1475 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1477 if (!dev->ethtool_ops->get_channels)
1480 dev->ethtool_ops->get_channels(dev, &channels);
1482 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1487 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1488 void __user *useraddr)
1490 struct ethtool_channels channels;
1492 if (!dev->ethtool_ops->set_channels)
1495 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1498 return dev->ethtool_ops->set_channels(dev, &channels);
1501 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1503 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1505 if (!dev->ethtool_ops->get_pauseparam)
1508 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1510 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1515 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1517 struct ethtool_pauseparam pauseparam;
1519 if (!dev->ethtool_ops->set_pauseparam)
1522 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1525 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1528 static int __ethtool_set_sg(struct net_device *dev, u32 data)
1532 if (!dev->ethtool_ops->set_sg)
1535 if (data && !(dev->features & NETIF_F_ALL_CSUM))
1538 if (!data && dev->ethtool_ops->set_tso) {
1539 err = dev->ethtool_ops->set_tso(dev, 0);
1544 if (!data && dev->ethtool_ops->set_ufo) {
1545 err = dev->ethtool_ops->set_ufo(dev, 0);
1549 return dev->ethtool_ops->set_sg(dev, data);
1552 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
1556 if (!dev->ethtool_ops->set_tx_csum)
1559 if (!data && dev->ethtool_ops->set_sg) {
1560 err = __ethtool_set_sg(dev, 0);
1565 return dev->ethtool_ops->set_tx_csum(dev, data);
1568 static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
1570 if (!dev->ethtool_ops->set_rx_csum)
1574 dev->features &= ~NETIF_F_GRO;
1576 return dev->ethtool_ops->set_rx_csum(dev, data);
1579 static int __ethtool_set_tso(struct net_device *dev, u32 data)
1581 if (!dev->ethtool_ops->set_tso)
1584 if (data && !(dev->features & NETIF_F_SG))
1587 return dev->ethtool_ops->set_tso(dev, data);
1590 static int __ethtool_set_ufo(struct net_device *dev, u32 data)
1592 if (!dev->ethtool_ops->set_ufo)
1594 if (data && !(dev->features & NETIF_F_SG))
1596 if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
1597 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1598 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
1600 return dev->ethtool_ops->set_ufo(dev, data);
1603 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1605 struct ethtool_test test;
1606 const struct ethtool_ops *ops = dev->ethtool_ops;
1610 if (!ops->self_test || !ops->get_sset_count)
1613 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1616 WARN_ON(test_len == 0);
1618 if (copy_from_user(&test, useraddr, sizeof(test)))
1621 test.len = test_len;
1622 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1626 ops->self_test(dev, &test, data);
1629 if (copy_to_user(useraddr, &test, sizeof(test)))
1631 useraddr += sizeof(test);
1632 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1641 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1643 struct ethtool_gstrings gstrings;
1647 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1650 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1656 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1660 __ethtool_get_strings(dev, gstrings.string_set, data);
1663 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1665 useraddr += sizeof(gstrings);
1666 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1675 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1677 struct ethtool_value id;
1681 if (!dev->ethtool_ops->set_phys_id)
1687 if (copy_from_user(&id, useraddr, sizeof(id)))
1690 rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1694 /* Drop the RTNL lock while waiting, but prevent reentry or
1695 * removal of the device.
1702 /* Driver will handle this itself */
1703 schedule_timeout_interruptible(
1704 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1706 /* Driver expects to be called at twice the frequency in rc */
1707 int n = rc * 2, i, interval = HZ / n;
1709 /* Count down seconds */
1711 /* Count down iterations per second */
1715 rc = dev->ethtool_ops->set_phys_id(dev,
1716 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1720 schedule_timeout_interruptible(interval);
1721 } while (!signal_pending(current) && --i != 0);
1722 } while (!signal_pending(current) &&
1723 (id.data == 0 || --id.data != 0));
1730 (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1734 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1736 struct ethtool_stats stats;
1737 const struct ethtool_ops *ops = dev->ethtool_ops;
1741 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1744 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1747 WARN_ON(n_stats == 0);
1749 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1752 stats.n_stats = n_stats;
1753 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1757 ops->get_ethtool_stats(dev, &stats, data);
1760 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1762 useraddr += sizeof(stats);
1763 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1772 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1774 struct ethtool_perm_addr epaddr;
1776 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1779 if (epaddr.size < dev->addr_len)
1781 epaddr.size = dev->addr_len;
1783 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1785 useraddr += sizeof(epaddr);
1786 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1791 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1792 u32 cmd, u32 (*actor)(struct net_device *))
1794 struct ethtool_value edata = { .cmd = cmd };
1799 edata.data = actor(dev);
1801 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1806 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1807 void (*actor)(struct net_device *, u32))
1809 struct ethtool_value edata;
1814 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1817 actor(dev, edata.data);
1821 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1822 int (*actor)(struct net_device *, u32))
1824 struct ethtool_value edata;
1829 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1832 return actor(dev, edata.data);
1835 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1836 char __user *useraddr)
1838 struct ethtool_flash efl;
1840 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1843 if (!dev->ethtool_ops->flash_device)
1846 return dev->ethtool_ops->flash_device(dev, &efl);
1849 static int ethtool_set_dump(struct net_device *dev,
1850 void __user *useraddr)
1852 struct ethtool_dump dump;
1854 if (!dev->ethtool_ops->set_dump)
1857 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1860 return dev->ethtool_ops->set_dump(dev, &dump);
1863 static int ethtool_get_dump_flag(struct net_device *dev,
1864 void __user *useraddr)
1867 struct ethtool_dump dump;
1868 const struct ethtool_ops *ops = dev->ethtool_ops;
1870 if (!dev->ethtool_ops->get_dump_flag)
1873 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1876 ret = ops->get_dump_flag(dev, &dump);
1880 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1885 static int ethtool_get_dump_data(struct net_device *dev,
1886 void __user *useraddr)
1890 struct ethtool_dump dump, tmp;
1891 const struct ethtool_ops *ops = dev->ethtool_ops;
1894 if (!dev->ethtool_ops->get_dump_data ||
1895 !dev->ethtool_ops->get_dump_flag)
1898 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1901 memset(&tmp, 0, sizeof(tmp));
1902 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1903 ret = ops->get_dump_flag(dev, &tmp);
1907 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1911 data = vzalloc(tmp.len);
1914 ret = ops->get_dump_data(dev, &dump, data);
1918 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1922 useraddr += offsetof(struct ethtool_dump, data);
1923 if (copy_to_user(useraddr, data, len))
1930 /* The main entry point in this file. Called from net/core/dev.c */
1932 int dev_ethtool(struct net *net, struct ifreq *ifr)
1934 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1935 void __user *useraddr = ifr->ifr_data;
1940 if (!dev || !netif_device_present(dev))
1943 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1946 if (!dev->ethtool_ops) {
1947 /* ETHTOOL_GDRVINFO does not require any driver support.
1948 * It is also unprivileged and does not change anything,
1949 * so we can take a shortcut to it. */
1950 if (ethcmd == ETHTOOL_GDRVINFO)
1951 return ethtool_get_drvinfo(dev, useraddr);
1956 /* Allow some commands to be done by anyone */
1959 case ETHTOOL_GDRVINFO:
1960 case ETHTOOL_GMSGLVL:
1961 case ETHTOOL_GCOALESCE:
1962 case ETHTOOL_GRINGPARAM:
1963 case ETHTOOL_GPAUSEPARAM:
1964 case ETHTOOL_GRXCSUM:
1965 case ETHTOOL_GTXCSUM:
1967 case ETHTOOL_GSTRINGS:
1969 case ETHTOOL_GPERMADDR:
1973 case ETHTOOL_GFLAGS:
1974 case ETHTOOL_GPFLAGS:
1976 case ETHTOOL_GRXRINGS:
1977 case ETHTOOL_GRXCLSRLCNT:
1978 case ETHTOOL_GRXCLSRULE:
1979 case ETHTOOL_GRXCLSRLALL:
1980 case ETHTOOL_GFEATURES:
1983 if (!capable(CAP_NET_ADMIN))
1987 if (dev->ethtool_ops->begin) {
1988 rc = dev->ethtool_ops->begin(dev);
1992 old_features = dev->features;
1996 rc = ethtool_get_settings(dev, useraddr);
1999 rc = ethtool_set_settings(dev, useraddr);
2001 case ETHTOOL_GDRVINFO:
2002 rc = ethtool_get_drvinfo(dev, useraddr);
2005 rc = ethtool_get_regs(dev, useraddr);
2008 rc = ethtool_get_wol(dev, useraddr);
2011 rc = ethtool_set_wol(dev, useraddr);
2013 case ETHTOOL_GMSGLVL:
2014 rc = ethtool_get_value(dev, useraddr, ethcmd,
2015 dev->ethtool_ops->get_msglevel);
2017 case ETHTOOL_SMSGLVL:
2018 rc = ethtool_set_value_void(dev, useraddr,
2019 dev->ethtool_ops->set_msglevel);
2021 case ETHTOOL_NWAY_RST:
2022 rc = ethtool_nway_reset(dev);
2025 rc = ethtool_get_link(dev, useraddr);
2027 case ETHTOOL_GEEPROM:
2028 rc = ethtool_get_eeprom(dev, useraddr);
2030 case ETHTOOL_SEEPROM:
2031 rc = ethtool_set_eeprom(dev, useraddr);
2033 case ETHTOOL_GCOALESCE:
2034 rc = ethtool_get_coalesce(dev, useraddr);
2036 case ETHTOOL_SCOALESCE:
2037 rc = ethtool_set_coalesce(dev, useraddr);
2039 case ETHTOOL_GRINGPARAM:
2040 rc = ethtool_get_ringparam(dev, useraddr);
2042 case ETHTOOL_SRINGPARAM:
2043 rc = ethtool_set_ringparam(dev, useraddr);
2045 case ETHTOOL_GPAUSEPARAM:
2046 rc = ethtool_get_pauseparam(dev, useraddr);
2048 case ETHTOOL_SPAUSEPARAM:
2049 rc = ethtool_set_pauseparam(dev, useraddr);
2052 rc = ethtool_self_test(dev, useraddr);
2054 case ETHTOOL_GSTRINGS:
2055 rc = ethtool_get_strings(dev, useraddr);
2057 case ETHTOOL_PHYS_ID:
2058 rc = ethtool_phys_id(dev, useraddr);
2060 case ETHTOOL_GSTATS:
2061 rc = ethtool_get_stats(dev, useraddr);
2063 case ETHTOOL_GPERMADDR:
2064 rc = ethtool_get_perm_addr(dev, useraddr);
2066 case ETHTOOL_GFLAGS:
2067 rc = ethtool_get_value(dev, useraddr, ethcmd,
2068 (dev->ethtool_ops->get_flags ?
2069 dev->ethtool_ops->get_flags :
2070 ethtool_op_get_flags));
2072 case ETHTOOL_SFLAGS:
2073 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2075 case ETHTOOL_GPFLAGS:
2076 rc = ethtool_get_value(dev, useraddr, ethcmd,
2077 dev->ethtool_ops->get_priv_flags);
2079 case ETHTOOL_SPFLAGS:
2080 rc = ethtool_set_value(dev, useraddr,
2081 dev->ethtool_ops->set_priv_flags);
2084 case ETHTOOL_GRXRINGS:
2085 case ETHTOOL_GRXCLSRLCNT:
2086 case ETHTOOL_GRXCLSRULE:
2087 case ETHTOOL_GRXCLSRLALL:
2088 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2091 case ETHTOOL_SRXCLSRLDEL:
2092 case ETHTOOL_SRXCLSRLINS:
2093 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2095 case ETHTOOL_FLASHDEV:
2096 rc = ethtool_flash_device(dev, useraddr);
2099 rc = ethtool_reset(dev, useraddr);
2101 case ETHTOOL_SRXNTUPLE:
2102 rc = ethtool_set_rx_ntuple(dev, useraddr);
2104 case ETHTOOL_GRXNTUPLE:
2105 rc = ethtool_get_rx_ntuple(dev, useraddr);
2107 case ETHTOOL_GSSET_INFO:
2108 rc = ethtool_get_sset_info(dev, useraddr);
2110 case ETHTOOL_GRXFHINDIR:
2111 rc = ethtool_get_rxfh_indir(dev, useraddr);
2113 case ETHTOOL_SRXFHINDIR:
2114 rc = ethtool_set_rxfh_indir(dev, useraddr);
2116 case ETHTOOL_GFEATURES:
2117 rc = ethtool_get_features(dev, useraddr);
2119 case ETHTOOL_SFEATURES:
2120 rc = ethtool_set_features(dev, useraddr);
2122 case ETHTOOL_GTXCSUM:
2123 case ETHTOOL_GRXCSUM:
2129 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2131 case ETHTOOL_STXCSUM:
2132 case ETHTOOL_SRXCSUM:
2138 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2140 case ETHTOOL_GCHANNELS:
2141 rc = ethtool_get_channels(dev, useraddr);
2143 case ETHTOOL_SCHANNELS:
2144 rc = ethtool_set_channels(dev, useraddr);
2146 case ETHTOOL_SET_DUMP:
2147 rc = ethtool_set_dump(dev, useraddr);
2149 case ETHTOOL_GET_DUMP_FLAG:
2150 rc = ethtool_get_dump_flag(dev, useraddr);
2152 case ETHTOOL_GET_DUMP_DATA:
2153 rc = ethtool_get_dump_data(dev, useraddr);
2159 if (dev->ethtool_ops->complete)
2160 dev->ethtool_ops->complete(dev);
2162 if (old_features != dev->features)
2163 netdev_features_change(dev);