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 /* Handlers for each ethtool command */
41 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
43 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
44 [NETIF_F_SG_BIT] = "tx-scatter-gather",
45 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
46 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
47 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
48 [NETIF_F_HIGHDMA_BIT] = "highdma",
49 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
50 [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
52 [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
53 [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
54 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
55 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
56 [NETIF_F_LLTX_BIT] = "tx-lockless",
57 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
58 [NETIF_F_GRO_BIT] = "rx-gro",
59 [NETIF_F_LRO_BIT] = "rx-lro",
61 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
62 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
63 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
64 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
65 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
66 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
68 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
69 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
70 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
71 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
72 [NETIF_F_RXHASH_BIT] = "rx-hashing",
73 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
74 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
75 [NETIF_F_LOOPBACK_BIT] = "loopback",
78 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
80 struct ethtool_gfeatures cmd = {
81 .cmd = ETHTOOL_GFEATURES,
82 .size = ETHTOOL_DEV_FEATURE_WORDS,
84 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
89 /* in case feature bits run out again */
90 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
92 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
93 features[i].available = (u32)(dev->hw_features >> (32 * i));
94 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
95 features[i].active = (u32)(dev->features >> (32 * i));
96 features[i].never_changed =
97 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
100 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
101 if (get_user(copy_size, sizeaddr))
104 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
105 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
107 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
109 useraddr += sizeof(cmd);
110 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
116 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
118 struct ethtool_sfeatures cmd;
119 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
120 netdev_features_t wanted = 0, valid = 0;
123 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
125 useraddr += sizeof(cmd);
127 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
130 if (copy_from_user(features, useraddr, sizeof(features)))
133 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
134 valid |= (netdev_features_t)features[i].valid << (32 * i);
135 wanted |= (netdev_features_t)features[i].requested << (32 * i);
138 if (valid & ~NETIF_F_ETHTOOL_BITS)
141 if (valid & ~dev->hw_features) {
142 valid &= dev->hw_features;
143 ret |= ETHTOOL_F_UNSUPPORTED;
146 dev->wanted_features &= ~valid;
147 dev->wanted_features |= wanted & valid;
148 __netdev_update_features(dev);
150 if ((dev->wanted_features ^ dev->features) & valid)
151 ret |= ETHTOOL_F_WISH;
156 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
158 const struct ethtool_ops *ops = dev->ethtool_ops;
160 if (sset == ETH_SS_FEATURES)
161 return ARRAY_SIZE(netdev_features_strings);
163 if (ops && ops->get_sset_count && ops->get_strings)
164 return ops->get_sset_count(dev, sset);
169 static void __ethtool_get_strings(struct net_device *dev,
170 u32 stringset, u8 *data)
172 const struct ethtool_ops *ops = dev->ethtool_ops;
174 if (stringset == ETH_SS_FEATURES)
175 memcpy(data, netdev_features_strings,
176 sizeof(netdev_features_strings));
178 /* ops->get_strings is valid because checked earlier */
179 ops->get_strings(dev, stringset, data);
182 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
184 /* feature masks of legacy discrete ethtool ops */
187 case ETHTOOL_GTXCSUM:
188 case ETHTOOL_STXCSUM:
189 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
190 case ETHTOOL_GRXCSUM:
191 case ETHTOOL_SRXCSUM:
192 return NETIF_F_RXCSUM;
198 return NETIF_F_ALL_TSO;
213 static int ethtool_get_one_feature(struct net_device *dev,
214 char __user *useraddr, u32 ethcmd)
216 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
217 struct ethtool_value edata = {
219 .data = !!(dev->features & mask),
222 if (copy_to_user(useraddr, &edata, sizeof(edata)))
227 static int ethtool_set_one_feature(struct net_device *dev,
228 void __user *useraddr, u32 ethcmd)
230 struct ethtool_value edata;
231 netdev_features_t mask;
233 if (copy_from_user(&edata, useraddr, sizeof(edata)))
236 mask = ethtool_get_feature_mask(ethcmd);
237 mask &= dev->hw_features;
242 dev->wanted_features |= mask;
244 dev->wanted_features &= ~mask;
246 __netdev_update_features(dev);
251 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
252 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
253 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
254 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
256 static u32 __ethtool_get_flags(struct net_device *dev)
260 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
261 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
262 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
263 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
264 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
269 static int __ethtool_set_flags(struct net_device *dev, u32 data)
271 netdev_features_t features = 0, changed;
273 if (data & ~ETH_ALL_FLAGS)
276 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
277 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
278 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
279 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
280 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
282 /* allow changing only bits set in hw_features */
283 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
284 if (changed & ~dev->hw_features)
285 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
287 dev->wanted_features =
288 (dev->wanted_features & ~changed) | (features & changed);
290 __netdev_update_features(dev);
295 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
299 if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
302 memset(cmd, 0, sizeof(struct ethtool_cmd));
303 cmd->cmd = ETHTOOL_GSET;
304 return dev->ethtool_ops->get_settings(dev, cmd);
306 EXPORT_SYMBOL(__ethtool_get_settings);
308 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
311 struct ethtool_cmd cmd;
313 err = __ethtool_get_settings(dev, &cmd);
317 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
322 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
324 struct ethtool_cmd cmd;
326 if (!dev->ethtool_ops->set_settings)
329 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
332 return dev->ethtool_ops->set_settings(dev, &cmd);
335 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
336 void __user *useraddr)
338 struct ethtool_drvinfo info;
339 const struct ethtool_ops *ops = dev->ethtool_ops;
341 memset(&info, 0, sizeof(info));
342 info.cmd = ETHTOOL_GDRVINFO;
343 if (ops && ops->get_drvinfo) {
344 ops->get_drvinfo(dev, &info);
345 } else if (dev->dev.parent && dev->dev.parent->driver) {
346 strlcpy(info.bus_info, dev_name(dev->dev.parent),
347 sizeof(info.bus_info));
348 strlcpy(info.driver, dev->dev.parent->driver->name,
349 sizeof(info.driver));
355 * this method of obtaining string set info is deprecated;
356 * Use ETHTOOL_GSSET_INFO instead.
358 if (ops && ops->get_sset_count) {
361 rc = ops->get_sset_count(dev, ETH_SS_TEST);
363 info.testinfo_len = rc;
364 rc = ops->get_sset_count(dev, ETH_SS_STATS);
367 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
369 info.n_priv_flags = rc;
371 if (ops && ops->get_regs_len)
372 info.regdump_len = ops->get_regs_len(dev);
373 if (ops && ops->get_eeprom_len)
374 info.eedump_len = ops->get_eeprom_len(dev);
376 if (copy_to_user(useraddr, &info, sizeof(info)))
381 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
382 void __user *useraddr)
384 struct ethtool_sset_info info;
386 int i, idx = 0, n_bits = 0, ret, rc;
387 u32 *info_buf = NULL;
389 if (copy_from_user(&info, useraddr, sizeof(info)))
392 /* store copy of mask, because we zero struct later on */
393 sset_mask = info.sset_mask;
397 /* calculate size of return buffer */
398 n_bits = hweight64(sset_mask);
400 memset(&info, 0, sizeof(info));
401 info.cmd = ETHTOOL_GSSET_INFO;
403 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
408 * fill return buffer based on input bitmask and successful
409 * get_sset_count return
411 for (i = 0; i < 64; i++) {
412 if (!(sset_mask & (1ULL << i)))
415 rc = __ethtool_get_sset_count(dev, i);
417 info.sset_mask |= (1ULL << i);
418 info_buf[idx++] = rc;
423 if (copy_to_user(useraddr, &info, sizeof(info)))
426 useraddr += offsetof(struct ethtool_sset_info, data);
427 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
437 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
438 u32 cmd, void __user *useraddr)
440 struct ethtool_rxnfc info;
441 size_t info_size = sizeof(info);
443 if (!dev->ethtool_ops->set_rxnfc)
446 /* struct ethtool_rxnfc was originally defined for
447 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
448 * members. User-space might still be using that
450 if (cmd == ETHTOOL_SRXFH)
451 info_size = (offsetof(struct ethtool_rxnfc, data) +
454 if (copy_from_user(&info, useraddr, info_size))
457 return dev->ethtool_ops->set_rxnfc(dev, &info);
460 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
461 u32 cmd, void __user *useraddr)
463 struct ethtool_rxnfc info;
464 size_t info_size = sizeof(info);
465 const struct ethtool_ops *ops = dev->ethtool_ops;
467 void *rule_buf = NULL;
472 /* struct ethtool_rxnfc was originally defined for
473 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
474 * members. User-space might still be using that
476 if (cmd == ETHTOOL_GRXFH)
477 info_size = (offsetof(struct ethtool_rxnfc, data) +
480 if (copy_from_user(&info, useraddr, info_size))
483 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
484 if (info.rule_cnt > 0) {
485 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
486 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
493 ret = ops->get_rxnfc(dev, &info, rule_buf);
498 if (copy_to_user(useraddr, &info, info_size))
502 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
503 if (copy_to_user(useraddr, rule_buf,
504 info.rule_cnt * sizeof(u32)))
515 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
516 void __user *useraddr)
518 struct ethtool_rxfh_indir *indir;
523 if (!dev->ethtool_ops->get_rxfh_indir)
526 if (copy_from_user(&table_size,
527 useraddr + offsetof(struct ethtool_rxfh_indir, size),
532 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
534 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
535 indir = kzalloc(full_size, GFP_USER);
539 indir->cmd = ETHTOOL_GRXFHINDIR;
540 indir->size = table_size;
541 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
545 if (copy_to_user(useraddr, indir, full_size))
553 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
554 void __user *useraddr)
556 struct ethtool_rxfh_indir *indir;
561 if (!dev->ethtool_ops->set_rxfh_indir)
564 if (copy_from_user(&table_size,
565 useraddr + offsetof(struct ethtool_rxfh_indir, size),
570 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
572 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
573 indir = kmalloc(full_size, GFP_USER);
577 if (copy_from_user(indir, useraddr, full_size)) {
582 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
590 * ethtool does not (or did not) set masks for flow parameters that are
591 * not specified, so if both value and mask are 0 then this must be
592 * treated as equivalent to a mask with all bits set. Implement that
593 * here rather than in drivers.
595 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
597 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
598 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
600 if (fs->flow_type != TCP_V4_FLOW &&
601 fs->flow_type != UDP_V4_FLOW &&
602 fs->flow_type != SCTP_V4_FLOW)
605 if (!(entry->ip4src | mask->ip4src))
606 mask->ip4src = htonl(0xffffffff);
607 if (!(entry->ip4dst | mask->ip4dst))
608 mask->ip4dst = htonl(0xffffffff);
609 if (!(entry->psrc | mask->psrc))
610 mask->psrc = htons(0xffff);
611 if (!(entry->pdst | mask->pdst))
612 mask->pdst = htons(0xffff);
613 if (!(entry->tos | mask->tos))
615 if (!(fs->vlan_tag | fs->vlan_tag_mask))
616 fs->vlan_tag_mask = 0xffff;
617 if (!(fs->data | fs->data_mask))
618 fs->data_mask = 0xffffffffffffffffULL;
621 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
622 void __user *useraddr)
624 struct ethtool_rx_ntuple cmd;
625 const struct ethtool_ops *ops = dev->ethtool_ops;
627 if (!ops->set_rx_ntuple)
630 if (!(dev->features & NETIF_F_NTUPLE))
633 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
636 rx_ntuple_fix_masks(&cmd.fs);
638 return ops->set_rx_ntuple(dev, &cmd);
641 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
643 struct ethtool_regs regs;
644 const struct ethtool_ops *ops = dev->ethtool_ops;
648 if (!ops->get_regs || !ops->get_regs_len)
651 if (copy_from_user(®s, useraddr, sizeof(regs)))
654 reglen = ops->get_regs_len(dev);
655 if (regs.len > reglen)
658 regbuf = vzalloc(reglen);
659 if (reglen && !regbuf)
662 ops->get_regs(dev, ®s, regbuf);
665 if (copy_to_user(useraddr, ®s, sizeof(regs)))
667 useraddr += offsetof(struct ethtool_regs, data);
668 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
677 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
679 struct ethtool_value reset;
682 if (!dev->ethtool_ops->reset)
685 if (copy_from_user(&reset, useraddr, sizeof(reset)))
688 ret = dev->ethtool_ops->reset(dev, &reset.data);
692 if (copy_to_user(useraddr, &reset, sizeof(reset)))
697 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
699 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
701 if (!dev->ethtool_ops->get_wol)
704 dev->ethtool_ops->get_wol(dev, &wol);
706 if (copy_to_user(useraddr, &wol, sizeof(wol)))
711 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
713 struct ethtool_wolinfo wol;
715 if (!dev->ethtool_ops->set_wol)
718 if (copy_from_user(&wol, useraddr, sizeof(wol)))
721 return dev->ethtool_ops->set_wol(dev, &wol);
724 static int ethtool_nway_reset(struct net_device *dev)
726 if (!dev->ethtool_ops->nway_reset)
729 return dev->ethtool_ops->nway_reset(dev);
732 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
734 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
736 if (!dev->ethtool_ops->get_link)
739 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
741 if (copy_to_user(useraddr, &edata, sizeof(edata)))
746 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
748 struct ethtool_eeprom eeprom;
749 const struct ethtool_ops *ops = dev->ethtool_ops;
750 void __user *userbuf = useraddr + sizeof(eeprom);
755 if (!ops->get_eeprom || !ops->get_eeprom_len)
758 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
761 /* Check for wrap and zero */
762 if (eeprom.offset + eeprom.len <= eeprom.offset)
765 /* Check for exceeding total eeprom len */
766 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
769 data = kmalloc(PAGE_SIZE, GFP_USER);
773 bytes_remaining = eeprom.len;
774 while (bytes_remaining > 0) {
775 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
777 ret = ops->get_eeprom(dev, &eeprom, data);
780 if (copy_to_user(userbuf, data, eeprom.len)) {
784 userbuf += eeprom.len;
785 eeprom.offset += eeprom.len;
786 bytes_remaining -= eeprom.len;
789 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
790 eeprom.offset -= eeprom.len;
791 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
798 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
800 struct ethtool_eeprom eeprom;
801 const struct ethtool_ops *ops = dev->ethtool_ops;
802 void __user *userbuf = useraddr + sizeof(eeprom);
807 if (!ops->set_eeprom || !ops->get_eeprom_len)
810 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
813 /* Check for wrap and zero */
814 if (eeprom.offset + eeprom.len <= eeprom.offset)
817 /* Check for exceeding total eeprom len */
818 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
821 data = kmalloc(PAGE_SIZE, GFP_USER);
825 bytes_remaining = eeprom.len;
826 while (bytes_remaining > 0) {
827 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
829 if (copy_from_user(data, userbuf, eeprom.len)) {
833 ret = ops->set_eeprom(dev, &eeprom, data);
836 userbuf += eeprom.len;
837 eeprom.offset += eeprom.len;
838 bytes_remaining -= eeprom.len;
845 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
846 void __user *useraddr)
848 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
850 if (!dev->ethtool_ops->get_coalesce)
853 dev->ethtool_ops->get_coalesce(dev, &coalesce);
855 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
860 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
861 void __user *useraddr)
863 struct ethtool_coalesce coalesce;
865 if (!dev->ethtool_ops->set_coalesce)
868 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
871 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
874 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
876 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
878 if (!dev->ethtool_ops->get_ringparam)
881 dev->ethtool_ops->get_ringparam(dev, &ringparam);
883 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
888 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
890 struct ethtool_ringparam ringparam;
892 if (!dev->ethtool_ops->set_ringparam)
895 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
898 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
901 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
902 void __user *useraddr)
904 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
906 if (!dev->ethtool_ops->get_channels)
909 dev->ethtool_ops->get_channels(dev, &channels);
911 if (copy_to_user(useraddr, &channels, sizeof(channels)))
916 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
917 void __user *useraddr)
919 struct ethtool_channels channels;
921 if (!dev->ethtool_ops->set_channels)
924 if (copy_from_user(&channels, useraddr, sizeof(channels)))
927 return dev->ethtool_ops->set_channels(dev, &channels);
930 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
932 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
934 if (!dev->ethtool_ops->get_pauseparam)
937 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
939 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
944 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
946 struct ethtool_pauseparam pauseparam;
948 if (!dev->ethtool_ops->set_pauseparam)
951 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
954 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
957 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
959 struct ethtool_test test;
960 const struct ethtool_ops *ops = dev->ethtool_ops;
964 if (!ops->self_test || !ops->get_sset_count)
967 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
970 WARN_ON(test_len == 0);
972 if (copy_from_user(&test, useraddr, sizeof(test)))
976 data = kmalloc(test_len * sizeof(u64), GFP_USER);
980 ops->self_test(dev, &test, data);
983 if (copy_to_user(useraddr, &test, sizeof(test)))
985 useraddr += sizeof(test);
986 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
995 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
997 struct ethtool_gstrings gstrings;
1001 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1004 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1010 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1014 __ethtool_get_strings(dev, gstrings.string_set, data);
1017 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1019 useraddr += sizeof(gstrings);
1020 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1029 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1031 struct ethtool_value id;
1035 if (!dev->ethtool_ops->set_phys_id)
1041 if (copy_from_user(&id, useraddr, sizeof(id)))
1044 rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1048 /* Drop the RTNL lock while waiting, but prevent reentry or
1049 * removal of the device.
1056 /* Driver will handle this itself */
1057 schedule_timeout_interruptible(
1058 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1060 /* Driver expects to be called at twice the frequency in rc */
1061 int n = rc * 2, i, interval = HZ / n;
1063 /* Count down seconds */
1065 /* Count down iterations per second */
1069 rc = dev->ethtool_ops->set_phys_id(dev,
1070 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1074 schedule_timeout_interruptible(interval);
1075 } while (!signal_pending(current) && --i != 0);
1076 } while (!signal_pending(current) &&
1077 (id.data == 0 || --id.data != 0));
1084 (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1088 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1090 struct ethtool_stats stats;
1091 const struct ethtool_ops *ops = dev->ethtool_ops;
1095 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1098 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1101 WARN_ON(n_stats == 0);
1103 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1106 stats.n_stats = n_stats;
1107 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1111 ops->get_ethtool_stats(dev, &stats, data);
1114 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1116 useraddr += sizeof(stats);
1117 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1126 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1128 struct ethtool_perm_addr epaddr;
1130 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1133 if (epaddr.size < dev->addr_len)
1135 epaddr.size = dev->addr_len;
1137 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1139 useraddr += sizeof(epaddr);
1140 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1145 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1146 u32 cmd, u32 (*actor)(struct net_device *))
1148 struct ethtool_value edata = { .cmd = cmd };
1153 edata.data = actor(dev);
1155 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1160 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1161 void (*actor)(struct net_device *, u32))
1163 struct ethtool_value edata;
1168 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1171 actor(dev, edata.data);
1175 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1176 int (*actor)(struct net_device *, u32))
1178 struct ethtool_value edata;
1183 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1186 return actor(dev, edata.data);
1189 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1190 char __user *useraddr)
1192 struct ethtool_flash efl;
1194 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1197 if (!dev->ethtool_ops->flash_device)
1200 return dev->ethtool_ops->flash_device(dev, &efl);
1203 static int ethtool_set_dump(struct net_device *dev,
1204 void __user *useraddr)
1206 struct ethtool_dump dump;
1208 if (!dev->ethtool_ops->set_dump)
1211 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1214 return dev->ethtool_ops->set_dump(dev, &dump);
1217 static int ethtool_get_dump_flag(struct net_device *dev,
1218 void __user *useraddr)
1221 struct ethtool_dump dump;
1222 const struct ethtool_ops *ops = dev->ethtool_ops;
1224 if (!dev->ethtool_ops->get_dump_flag)
1227 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1230 ret = ops->get_dump_flag(dev, &dump);
1234 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1239 static int ethtool_get_dump_data(struct net_device *dev,
1240 void __user *useraddr)
1244 struct ethtool_dump dump, tmp;
1245 const struct ethtool_ops *ops = dev->ethtool_ops;
1248 if (!dev->ethtool_ops->get_dump_data ||
1249 !dev->ethtool_ops->get_dump_flag)
1252 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1255 memset(&tmp, 0, sizeof(tmp));
1256 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1257 ret = ops->get_dump_flag(dev, &tmp);
1261 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1265 data = vzalloc(tmp.len);
1268 ret = ops->get_dump_data(dev, &dump, data);
1272 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1276 useraddr += offsetof(struct ethtool_dump, data);
1277 if (copy_to_user(useraddr, data, len))
1284 /* The main entry point in this file. Called from net/core/dev.c */
1286 int dev_ethtool(struct net *net, struct ifreq *ifr)
1288 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1289 void __user *useraddr = ifr->ifr_data;
1294 if (!dev || !netif_device_present(dev))
1297 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1300 if (!dev->ethtool_ops) {
1301 /* ETHTOOL_GDRVINFO does not require any driver support.
1302 * It is also unprivileged and does not change anything,
1303 * so we can take a shortcut to it. */
1304 if (ethcmd == ETHTOOL_GDRVINFO)
1305 return ethtool_get_drvinfo(dev, useraddr);
1310 /* Allow some commands to be done by anyone */
1313 case ETHTOOL_GDRVINFO:
1314 case ETHTOOL_GMSGLVL:
1315 case ETHTOOL_GCOALESCE:
1316 case ETHTOOL_GRINGPARAM:
1317 case ETHTOOL_GPAUSEPARAM:
1318 case ETHTOOL_GRXCSUM:
1319 case ETHTOOL_GTXCSUM:
1321 case ETHTOOL_GSTRINGS:
1323 case ETHTOOL_GPERMADDR:
1327 case ETHTOOL_GFLAGS:
1328 case ETHTOOL_GPFLAGS:
1330 case ETHTOOL_GRXRINGS:
1331 case ETHTOOL_GRXCLSRLCNT:
1332 case ETHTOOL_GRXCLSRULE:
1333 case ETHTOOL_GRXCLSRLALL:
1334 case ETHTOOL_GFEATURES:
1337 if (!capable(CAP_NET_ADMIN))
1341 if (dev->ethtool_ops->begin) {
1342 rc = dev->ethtool_ops->begin(dev);
1346 old_features = dev->features;
1350 rc = ethtool_get_settings(dev, useraddr);
1353 rc = ethtool_set_settings(dev, useraddr);
1355 case ETHTOOL_GDRVINFO:
1356 rc = ethtool_get_drvinfo(dev, useraddr);
1359 rc = ethtool_get_regs(dev, useraddr);
1362 rc = ethtool_get_wol(dev, useraddr);
1365 rc = ethtool_set_wol(dev, useraddr);
1367 case ETHTOOL_GMSGLVL:
1368 rc = ethtool_get_value(dev, useraddr, ethcmd,
1369 dev->ethtool_ops->get_msglevel);
1371 case ETHTOOL_SMSGLVL:
1372 rc = ethtool_set_value_void(dev, useraddr,
1373 dev->ethtool_ops->set_msglevel);
1375 case ETHTOOL_NWAY_RST:
1376 rc = ethtool_nway_reset(dev);
1379 rc = ethtool_get_link(dev, useraddr);
1381 case ETHTOOL_GEEPROM:
1382 rc = ethtool_get_eeprom(dev, useraddr);
1384 case ETHTOOL_SEEPROM:
1385 rc = ethtool_set_eeprom(dev, useraddr);
1387 case ETHTOOL_GCOALESCE:
1388 rc = ethtool_get_coalesce(dev, useraddr);
1390 case ETHTOOL_SCOALESCE:
1391 rc = ethtool_set_coalesce(dev, useraddr);
1393 case ETHTOOL_GRINGPARAM:
1394 rc = ethtool_get_ringparam(dev, useraddr);
1396 case ETHTOOL_SRINGPARAM:
1397 rc = ethtool_set_ringparam(dev, useraddr);
1399 case ETHTOOL_GPAUSEPARAM:
1400 rc = ethtool_get_pauseparam(dev, useraddr);
1402 case ETHTOOL_SPAUSEPARAM:
1403 rc = ethtool_set_pauseparam(dev, useraddr);
1406 rc = ethtool_self_test(dev, useraddr);
1408 case ETHTOOL_GSTRINGS:
1409 rc = ethtool_get_strings(dev, useraddr);
1411 case ETHTOOL_PHYS_ID:
1412 rc = ethtool_phys_id(dev, useraddr);
1414 case ETHTOOL_GSTATS:
1415 rc = ethtool_get_stats(dev, useraddr);
1417 case ETHTOOL_GPERMADDR:
1418 rc = ethtool_get_perm_addr(dev, useraddr);
1420 case ETHTOOL_GFLAGS:
1421 rc = ethtool_get_value(dev, useraddr, ethcmd,
1422 __ethtool_get_flags);
1424 case ETHTOOL_SFLAGS:
1425 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1427 case ETHTOOL_GPFLAGS:
1428 rc = ethtool_get_value(dev, useraddr, ethcmd,
1429 dev->ethtool_ops->get_priv_flags);
1431 case ETHTOOL_SPFLAGS:
1432 rc = ethtool_set_value(dev, useraddr,
1433 dev->ethtool_ops->set_priv_flags);
1436 case ETHTOOL_GRXRINGS:
1437 case ETHTOOL_GRXCLSRLCNT:
1438 case ETHTOOL_GRXCLSRULE:
1439 case ETHTOOL_GRXCLSRLALL:
1440 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1443 case ETHTOOL_SRXCLSRLDEL:
1444 case ETHTOOL_SRXCLSRLINS:
1445 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1447 case ETHTOOL_FLASHDEV:
1448 rc = ethtool_flash_device(dev, useraddr);
1451 rc = ethtool_reset(dev, useraddr);
1453 case ETHTOOL_SRXNTUPLE:
1454 rc = ethtool_set_rx_ntuple(dev, useraddr);
1456 case ETHTOOL_GSSET_INFO:
1457 rc = ethtool_get_sset_info(dev, useraddr);
1459 case ETHTOOL_GRXFHINDIR:
1460 rc = ethtool_get_rxfh_indir(dev, useraddr);
1462 case ETHTOOL_SRXFHINDIR:
1463 rc = ethtool_set_rxfh_indir(dev, useraddr);
1465 case ETHTOOL_GFEATURES:
1466 rc = ethtool_get_features(dev, useraddr);
1468 case ETHTOOL_SFEATURES:
1469 rc = ethtool_set_features(dev, useraddr);
1471 case ETHTOOL_GTXCSUM:
1472 case ETHTOOL_GRXCSUM:
1478 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1480 case ETHTOOL_STXCSUM:
1481 case ETHTOOL_SRXCSUM:
1487 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1489 case ETHTOOL_GCHANNELS:
1490 rc = ethtool_get_channels(dev, useraddr);
1492 case ETHTOOL_SCHANNELS:
1493 rc = ethtool_set_channels(dev, useraddr);
1495 case ETHTOOL_SET_DUMP:
1496 rc = ethtool_set_dump(dev, useraddr);
1498 case ETHTOOL_GET_DUMP_FLAG:
1499 rc = ethtool_get_dump_flag(dev, useraddr);
1501 case ETHTOOL_GET_DUMP_DATA:
1502 rc = ethtool_get_dump_data(dev, useraddr);
1508 if (dev->ethtool_ops->complete)
1509 dev->ethtool_ops->complete(dev);
1511 if (old_features != dev->features)
1512 netdev_features_change(dev);