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/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sched.h>
30 * Some useful ethtool_ops methods that're device independent.
31 * If we find that all drivers want to do the same thing here,
32 * we can turn these into dev_() function calls.
35 u32 ethtool_op_get_link(struct net_device *dev)
37 return netif_carrier_ok(dev) ? 1 : 0;
39 EXPORT_SYMBOL(ethtool_op_get_link);
41 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
43 info->so_timestamping =
44 SOF_TIMESTAMPING_TX_SOFTWARE |
45 SOF_TIMESTAMPING_RX_SOFTWARE |
46 SOF_TIMESTAMPING_SOFTWARE;
50 EXPORT_SYMBOL(ethtool_op_get_ts_info);
52 /* Handlers for each ethtool command */
54 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
56 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
57 [NETIF_F_SG_BIT] = "tx-scatter-gather",
58 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
59 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
60 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
61 [NETIF_F_HIGHDMA_BIT] = "highdma",
62 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
63 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
65 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
66 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
67 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
68 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
69 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
70 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
71 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
72 [NETIF_F_LLTX_BIT] = "tx-lockless",
73 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
74 [NETIF_F_GRO_BIT] = "rx-gro",
75 [NETIF_F_LRO_BIT] = "rx-lro",
77 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
78 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
79 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
80 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
81 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
82 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
83 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
84 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
85 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
87 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
88 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
89 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
90 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
91 [NETIF_F_RXHASH_BIT] = "rx-hashing",
92 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
93 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
94 [NETIF_F_LOOPBACK_BIT] = "loopback",
95 [NETIF_F_RXFCS_BIT] = "rx-fcs",
96 [NETIF_F_RXALL_BIT] = "rx-all",
99 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
101 struct ethtool_gfeatures cmd = {
102 .cmd = ETHTOOL_GFEATURES,
103 .size = ETHTOOL_DEV_FEATURE_WORDS,
105 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
106 u32 __user *sizeaddr;
110 /* in case feature bits run out again */
111 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
113 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
114 features[i].available = (u32)(dev->hw_features >> (32 * i));
115 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
116 features[i].active = (u32)(dev->features >> (32 * i));
117 features[i].never_changed =
118 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
121 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
122 if (get_user(copy_size, sizeaddr))
125 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
126 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
128 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
130 useraddr += sizeof(cmd);
131 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
137 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
139 struct ethtool_sfeatures cmd;
140 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
141 netdev_features_t wanted = 0, valid = 0;
144 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
146 useraddr += sizeof(cmd);
148 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
151 if (copy_from_user(features, useraddr, sizeof(features)))
154 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
155 valid |= (netdev_features_t)features[i].valid << (32 * i);
156 wanted |= (netdev_features_t)features[i].requested << (32 * i);
159 if (valid & ~NETIF_F_ETHTOOL_BITS)
162 if (valid & ~dev->hw_features) {
163 valid &= dev->hw_features;
164 ret |= ETHTOOL_F_UNSUPPORTED;
167 dev->wanted_features &= ~valid;
168 dev->wanted_features |= wanted & valid;
169 __netdev_update_features(dev);
171 if ((dev->wanted_features ^ dev->features) & valid)
172 ret |= ETHTOOL_F_WISH;
177 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
179 const struct ethtool_ops *ops = dev->ethtool_ops;
181 if (sset == ETH_SS_FEATURES)
182 return ARRAY_SIZE(netdev_features_strings);
184 if (ops->get_sset_count && ops->get_strings)
185 return ops->get_sset_count(dev, sset);
190 static void __ethtool_get_strings(struct net_device *dev,
191 u32 stringset, u8 *data)
193 const struct ethtool_ops *ops = dev->ethtool_ops;
195 if (stringset == ETH_SS_FEATURES)
196 memcpy(data, netdev_features_strings,
197 sizeof(netdev_features_strings));
199 /* ops->get_strings is valid because checked earlier */
200 ops->get_strings(dev, stringset, data);
203 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
205 /* feature masks of legacy discrete ethtool ops */
208 case ETHTOOL_GTXCSUM:
209 case ETHTOOL_STXCSUM:
210 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
211 case ETHTOOL_GRXCSUM:
212 case ETHTOOL_SRXCSUM:
213 return NETIF_F_RXCSUM;
219 return NETIF_F_ALL_TSO;
234 static int ethtool_get_one_feature(struct net_device *dev,
235 char __user *useraddr, u32 ethcmd)
237 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
238 struct ethtool_value edata = {
240 .data = !!(dev->features & mask),
243 if (copy_to_user(useraddr, &edata, sizeof(edata)))
248 static int ethtool_set_one_feature(struct net_device *dev,
249 void __user *useraddr, u32 ethcmd)
251 struct ethtool_value edata;
252 netdev_features_t mask;
254 if (copy_from_user(&edata, useraddr, sizeof(edata)))
257 mask = ethtool_get_feature_mask(ethcmd);
258 mask &= dev->hw_features;
263 dev->wanted_features |= mask;
265 dev->wanted_features &= ~mask;
267 __netdev_update_features(dev);
272 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
273 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
274 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
275 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
278 static u32 __ethtool_get_flags(struct net_device *dev)
282 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
283 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) flags |= ETH_FLAG_RXVLAN;
284 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) flags |= ETH_FLAG_TXVLAN;
285 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
286 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
291 static int __ethtool_set_flags(struct net_device *dev, u32 data)
293 netdev_features_t features = 0, changed;
295 if (data & ~ETH_ALL_FLAGS)
298 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
299 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_CTAG_RX;
300 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_CTAG_TX;
301 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
302 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
304 /* allow changing only bits set in hw_features */
305 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
306 if (changed & ~dev->hw_features)
307 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
309 dev->wanted_features =
310 (dev->wanted_features & ~changed) | (features & changed);
312 __netdev_update_features(dev);
317 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
321 if (!dev->ethtool_ops->get_settings)
324 memset(cmd, 0, sizeof(struct ethtool_cmd));
325 cmd->cmd = ETHTOOL_GSET;
326 return dev->ethtool_ops->get_settings(dev, cmd);
328 EXPORT_SYMBOL(__ethtool_get_settings);
330 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
333 struct ethtool_cmd cmd;
335 err = __ethtool_get_settings(dev, &cmd);
339 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
344 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
346 struct ethtool_cmd cmd;
348 if (!dev->ethtool_ops->set_settings)
351 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
354 return dev->ethtool_ops->set_settings(dev, &cmd);
357 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
358 void __user *useraddr)
360 struct ethtool_drvinfo info;
361 const struct ethtool_ops *ops = dev->ethtool_ops;
363 memset(&info, 0, sizeof(info));
364 info.cmd = ETHTOOL_GDRVINFO;
365 if (ops->get_drvinfo) {
366 ops->get_drvinfo(dev, &info);
367 } else if (dev->dev.parent && dev->dev.parent->driver) {
368 strlcpy(info.bus_info, dev_name(dev->dev.parent),
369 sizeof(info.bus_info));
370 strlcpy(info.driver, dev->dev.parent->driver->name,
371 sizeof(info.driver));
377 * this method of obtaining string set info is deprecated;
378 * Use ETHTOOL_GSSET_INFO instead.
380 if (ops->get_sset_count) {
383 rc = ops->get_sset_count(dev, ETH_SS_TEST);
385 info.testinfo_len = rc;
386 rc = ops->get_sset_count(dev, ETH_SS_STATS);
389 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
391 info.n_priv_flags = rc;
393 if (ops->get_regs_len)
394 info.regdump_len = ops->get_regs_len(dev);
395 if (ops->get_eeprom_len)
396 info.eedump_len = ops->get_eeprom_len(dev);
398 if (copy_to_user(useraddr, &info, sizeof(info)))
403 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
404 void __user *useraddr)
406 struct ethtool_sset_info info;
408 int i, idx = 0, n_bits = 0, ret, rc;
409 u32 *info_buf = NULL;
411 if (copy_from_user(&info, useraddr, sizeof(info)))
414 /* store copy of mask, because we zero struct later on */
415 sset_mask = info.sset_mask;
419 /* calculate size of return buffer */
420 n_bits = hweight64(sset_mask);
422 memset(&info, 0, sizeof(info));
423 info.cmd = ETHTOOL_GSSET_INFO;
425 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
430 * fill return buffer based on input bitmask and successful
431 * get_sset_count return
433 for (i = 0; i < 64; i++) {
434 if (!(sset_mask & (1ULL << i)))
437 rc = __ethtool_get_sset_count(dev, i);
439 info.sset_mask |= (1ULL << i);
440 info_buf[idx++] = rc;
445 if (copy_to_user(useraddr, &info, sizeof(info)))
448 useraddr += offsetof(struct ethtool_sset_info, data);
449 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
459 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
460 u32 cmd, void __user *useraddr)
462 struct ethtool_rxnfc info;
463 size_t info_size = sizeof(info);
466 if (!dev->ethtool_ops->set_rxnfc)
469 /* struct ethtool_rxnfc was originally defined for
470 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
471 * members. User-space might still be using that
473 if (cmd == ETHTOOL_SRXFH)
474 info_size = (offsetof(struct ethtool_rxnfc, data) +
477 if (copy_from_user(&info, useraddr, info_size))
480 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
484 if (cmd == ETHTOOL_SRXCLSRLINS &&
485 copy_to_user(useraddr, &info, info_size))
491 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
492 u32 cmd, void __user *useraddr)
494 struct ethtool_rxnfc info;
495 size_t info_size = sizeof(info);
496 const struct ethtool_ops *ops = dev->ethtool_ops;
498 void *rule_buf = NULL;
503 /* struct ethtool_rxnfc was originally defined for
504 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
505 * members. User-space might still be using that
507 if (cmd == ETHTOOL_GRXFH)
508 info_size = (offsetof(struct ethtool_rxnfc, data) +
511 if (copy_from_user(&info, useraddr, info_size))
514 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
515 if (info.rule_cnt > 0) {
516 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
517 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
524 ret = ops->get_rxnfc(dev, &info, rule_buf);
529 if (copy_to_user(useraddr, &info, info_size))
533 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
534 if (copy_to_user(useraddr, rule_buf,
535 info.rule_cnt * sizeof(u32)))
546 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
547 void __user *useraddr)
549 u32 user_size, dev_size;
553 if (!dev->ethtool_ops->get_rxfh_indir_size ||
554 !dev->ethtool_ops->get_rxfh_indir)
556 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
560 if (copy_from_user(&user_size,
561 useraddr + offsetof(struct ethtool_rxfh_indir, size),
565 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
566 &dev_size, sizeof(dev_size)))
569 /* If the user buffer size is 0, this is just a query for the
570 * device table size. Otherwise, if it's smaller than the
571 * device table size it's an error.
573 if (user_size < dev_size)
574 return user_size == 0 ? 0 : -EINVAL;
576 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
580 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
584 if (copy_to_user(useraddr +
585 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
586 indir, dev_size * sizeof(indir[0])))
594 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
595 void __user *useraddr)
597 struct ethtool_rxnfc rx_rings;
598 u32 user_size, dev_size, i;
600 const struct ethtool_ops *ops = dev->ethtool_ops;
603 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
607 dev_size = ops->get_rxfh_indir_size(dev);
611 if (copy_from_user(&user_size,
612 useraddr + offsetof(struct ethtool_rxfh_indir, size),
616 if (user_size != 0 && user_size != dev_size)
619 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
623 rx_rings.cmd = ETHTOOL_GRXRINGS;
624 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
628 if (user_size == 0) {
629 for (i = 0; i < dev_size; i++)
630 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
632 if (copy_from_user(indir,
634 offsetof(struct ethtool_rxfh_indir,
636 dev_size * sizeof(indir[0]))) {
641 /* Validate ring indices */
642 for (i = 0; i < dev_size; i++) {
643 if (indir[i] >= rx_rings.data) {
650 ret = ops->set_rxfh_indir(dev, indir);
657 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
659 struct ethtool_regs regs;
660 const struct ethtool_ops *ops = dev->ethtool_ops;
664 if (!ops->get_regs || !ops->get_regs_len)
667 if (copy_from_user(®s, useraddr, sizeof(regs)))
670 reglen = ops->get_regs_len(dev);
671 if (regs.len > reglen)
674 regbuf = vzalloc(reglen);
675 if (reglen && !regbuf)
678 ops->get_regs(dev, ®s, regbuf);
681 if (copy_to_user(useraddr, ®s, sizeof(regs)))
683 useraddr += offsetof(struct ethtool_regs, data);
684 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
693 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
695 struct ethtool_value reset;
698 if (!dev->ethtool_ops->reset)
701 if (copy_from_user(&reset, useraddr, sizeof(reset)))
704 ret = dev->ethtool_ops->reset(dev, &reset.data);
708 if (copy_to_user(useraddr, &reset, sizeof(reset)))
713 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
715 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
717 if (!dev->ethtool_ops->get_wol)
720 dev->ethtool_ops->get_wol(dev, &wol);
722 if (copy_to_user(useraddr, &wol, sizeof(wol)))
727 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
729 struct ethtool_wolinfo wol;
731 if (!dev->ethtool_ops->set_wol)
734 if (copy_from_user(&wol, useraddr, sizeof(wol)))
737 return dev->ethtool_ops->set_wol(dev, &wol);
740 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
742 struct ethtool_eee edata;
745 if (!dev->ethtool_ops->get_eee)
748 memset(&edata, 0, sizeof(struct ethtool_eee));
749 edata.cmd = ETHTOOL_GEEE;
750 rc = dev->ethtool_ops->get_eee(dev, &edata);
755 if (copy_to_user(useraddr, &edata, sizeof(edata)))
761 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
763 struct ethtool_eee edata;
765 if (!dev->ethtool_ops->set_eee)
768 if (copy_from_user(&edata, useraddr, sizeof(edata)))
771 return dev->ethtool_ops->set_eee(dev, &edata);
774 static int ethtool_nway_reset(struct net_device *dev)
776 if (!dev->ethtool_ops->nway_reset)
779 return dev->ethtool_ops->nway_reset(dev);
782 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
784 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
786 if (!dev->ethtool_ops->get_link)
789 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
791 if (copy_to_user(useraddr, &edata, sizeof(edata)))
796 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
797 int (*getter)(struct net_device *,
798 struct ethtool_eeprom *, u8 *),
801 struct ethtool_eeprom eeprom;
802 void __user *userbuf = useraddr + sizeof(eeprom);
807 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
810 /* Check for wrap and zero */
811 if (eeprom.offset + eeprom.len <= eeprom.offset)
814 /* Check for exceeding total eeprom len */
815 if (eeprom.offset + eeprom.len > total_len)
818 data = kmalloc(PAGE_SIZE, GFP_USER);
822 bytes_remaining = eeprom.len;
823 while (bytes_remaining > 0) {
824 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
826 ret = getter(dev, &eeprom, data);
829 if (copy_to_user(userbuf, data, eeprom.len)) {
833 userbuf += eeprom.len;
834 eeprom.offset += eeprom.len;
835 bytes_remaining -= eeprom.len;
838 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
839 eeprom.offset -= eeprom.len;
840 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
847 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
849 const struct ethtool_ops *ops = dev->ethtool_ops;
851 if (!ops->get_eeprom || !ops->get_eeprom_len)
854 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
855 ops->get_eeprom_len(dev));
858 static int ethtool_set_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->set_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 if (copy_from_user(data, userbuf, eeprom.len)) {
893 ret = ops->set_eeprom(dev, &eeprom, data);
896 userbuf += eeprom.len;
897 eeprom.offset += eeprom.len;
898 bytes_remaining -= eeprom.len;
905 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
906 void __user *useraddr)
908 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
910 if (!dev->ethtool_ops->get_coalesce)
913 dev->ethtool_ops->get_coalesce(dev, &coalesce);
915 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
920 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
921 void __user *useraddr)
923 struct ethtool_coalesce coalesce;
925 if (!dev->ethtool_ops->set_coalesce)
928 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
931 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
934 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
936 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
938 if (!dev->ethtool_ops->get_ringparam)
941 dev->ethtool_ops->get_ringparam(dev, &ringparam);
943 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
948 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
950 struct ethtool_ringparam ringparam;
952 if (!dev->ethtool_ops->set_ringparam)
955 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
958 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
961 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
962 void __user *useraddr)
964 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
966 if (!dev->ethtool_ops->get_channels)
969 dev->ethtool_ops->get_channels(dev, &channels);
971 if (copy_to_user(useraddr, &channels, sizeof(channels)))
976 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
977 void __user *useraddr)
979 struct ethtool_channels channels;
981 if (!dev->ethtool_ops->set_channels)
984 if (copy_from_user(&channels, useraddr, sizeof(channels)))
987 return dev->ethtool_ops->set_channels(dev, &channels);
990 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
992 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
994 if (!dev->ethtool_ops->get_pauseparam)
997 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
999 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1004 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1006 struct ethtool_pauseparam pauseparam;
1008 if (!dev->ethtool_ops->set_pauseparam)
1011 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1014 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1017 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1019 struct ethtool_test test;
1020 const struct ethtool_ops *ops = dev->ethtool_ops;
1024 if (!ops->self_test || !ops->get_sset_count)
1027 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1030 WARN_ON(test_len == 0);
1032 if (copy_from_user(&test, useraddr, sizeof(test)))
1035 test.len = test_len;
1036 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1040 ops->self_test(dev, &test, data);
1043 if (copy_to_user(useraddr, &test, sizeof(test)))
1045 useraddr += sizeof(test);
1046 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1055 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1057 struct ethtool_gstrings gstrings;
1061 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1064 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1070 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1074 __ethtool_get_strings(dev, gstrings.string_set, data);
1077 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1079 useraddr += sizeof(gstrings);
1080 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1089 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1091 struct ethtool_value id;
1093 const struct ethtool_ops *ops = dev->ethtool_ops;
1096 if (!ops->set_phys_id)
1102 if (copy_from_user(&id, useraddr, sizeof(id)))
1105 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1109 /* Drop the RTNL lock while waiting, but prevent reentry or
1110 * removal of the device.
1117 /* Driver will handle this itself */
1118 schedule_timeout_interruptible(
1119 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1121 /* Driver expects to be called at twice the frequency in rc */
1122 int n = rc * 2, i, interval = HZ / n;
1124 /* Count down seconds */
1126 /* Count down iterations per second */
1130 rc = ops->set_phys_id(dev,
1131 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1135 schedule_timeout_interruptible(interval);
1136 } while (!signal_pending(current) && --i != 0);
1137 } while (!signal_pending(current) &&
1138 (id.data == 0 || --id.data != 0));
1145 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1149 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1151 struct ethtool_stats stats;
1152 const struct ethtool_ops *ops = dev->ethtool_ops;
1156 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1159 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1162 WARN_ON(n_stats == 0);
1164 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1167 stats.n_stats = n_stats;
1168 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1172 ops->get_ethtool_stats(dev, &stats, data);
1175 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1177 useraddr += sizeof(stats);
1178 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1187 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1189 struct ethtool_perm_addr epaddr;
1191 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1194 if (epaddr.size < dev->addr_len)
1196 epaddr.size = dev->addr_len;
1198 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1200 useraddr += sizeof(epaddr);
1201 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1206 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1207 u32 cmd, u32 (*actor)(struct net_device *))
1209 struct ethtool_value edata = { .cmd = cmd };
1214 edata.data = actor(dev);
1216 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1221 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1222 void (*actor)(struct net_device *, u32))
1224 struct ethtool_value edata;
1229 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1232 actor(dev, edata.data);
1236 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1237 int (*actor)(struct net_device *, u32))
1239 struct ethtool_value edata;
1244 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1247 return actor(dev, edata.data);
1250 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1251 char __user *useraddr)
1253 struct ethtool_flash efl;
1255 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1258 if (!dev->ethtool_ops->flash_device)
1261 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1263 return dev->ethtool_ops->flash_device(dev, &efl);
1266 static int ethtool_set_dump(struct net_device *dev,
1267 void __user *useraddr)
1269 struct ethtool_dump dump;
1271 if (!dev->ethtool_ops->set_dump)
1274 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1277 return dev->ethtool_ops->set_dump(dev, &dump);
1280 static int ethtool_get_dump_flag(struct net_device *dev,
1281 void __user *useraddr)
1284 struct ethtool_dump dump;
1285 const struct ethtool_ops *ops = dev->ethtool_ops;
1287 if (!ops->get_dump_flag)
1290 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1293 ret = ops->get_dump_flag(dev, &dump);
1297 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1302 static int ethtool_get_dump_data(struct net_device *dev,
1303 void __user *useraddr)
1307 struct ethtool_dump dump, tmp;
1308 const struct ethtool_ops *ops = dev->ethtool_ops;
1311 if (!ops->get_dump_data || !ops->get_dump_flag)
1314 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1317 memset(&tmp, 0, sizeof(tmp));
1318 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1319 ret = ops->get_dump_flag(dev, &tmp);
1323 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1327 data = vzalloc(tmp.len);
1330 ret = ops->get_dump_data(dev, &dump, data);
1334 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1338 useraddr += offsetof(struct ethtool_dump, data);
1339 if (copy_to_user(useraddr, data, len))
1346 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1349 struct ethtool_ts_info info;
1350 const struct ethtool_ops *ops = dev->ethtool_ops;
1351 struct phy_device *phydev = dev->phydev;
1353 memset(&info, 0, sizeof(info));
1354 info.cmd = ETHTOOL_GET_TS_INFO;
1356 if (phydev && phydev->drv && phydev->drv->ts_info) {
1357 err = phydev->drv->ts_info(phydev, &info);
1358 } else if (ops->get_ts_info) {
1359 err = ops->get_ts_info(dev, &info);
1361 info.so_timestamping =
1362 SOF_TIMESTAMPING_RX_SOFTWARE |
1363 SOF_TIMESTAMPING_SOFTWARE;
1364 info.phc_index = -1;
1370 if (copy_to_user(useraddr, &info, sizeof(info)))
1376 static int ethtool_get_module_info(struct net_device *dev,
1377 void __user *useraddr)
1380 struct ethtool_modinfo modinfo;
1381 const struct ethtool_ops *ops = dev->ethtool_ops;
1383 if (!ops->get_module_info)
1386 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1389 ret = ops->get_module_info(dev, &modinfo);
1393 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1399 static int ethtool_get_module_eeprom(struct net_device *dev,
1400 void __user *useraddr)
1403 struct ethtool_modinfo modinfo;
1404 const struct ethtool_ops *ops = dev->ethtool_ops;
1406 if (!ops->get_module_info || !ops->get_module_eeprom)
1409 ret = ops->get_module_info(dev, &modinfo);
1413 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1414 modinfo.eeprom_len);
1417 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
1419 int dev_ethtool(struct net *net, struct ifreq *ifr)
1421 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1422 void __user *useraddr = ifr->ifr_data;
1425 netdev_features_t old_features;
1427 if (!dev || !netif_device_present(dev))
1430 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1433 /* Allow some commands to be done by anyone */
1436 case ETHTOOL_GDRVINFO:
1437 case ETHTOOL_GMSGLVL:
1439 case ETHTOOL_GCOALESCE:
1440 case ETHTOOL_GRINGPARAM:
1441 case ETHTOOL_GPAUSEPARAM:
1442 case ETHTOOL_GRXCSUM:
1443 case ETHTOOL_GTXCSUM:
1445 case ETHTOOL_GSSET_INFO:
1446 case ETHTOOL_GSTRINGS:
1447 case ETHTOOL_GSTATS:
1449 case ETHTOOL_GPERMADDR:
1453 case ETHTOOL_GFLAGS:
1454 case ETHTOOL_GPFLAGS:
1456 case ETHTOOL_GRXRINGS:
1457 case ETHTOOL_GRXCLSRLCNT:
1458 case ETHTOOL_GRXCLSRULE:
1459 case ETHTOOL_GRXCLSRLALL:
1460 case ETHTOOL_GRXFHINDIR:
1461 case ETHTOOL_GFEATURES:
1462 case ETHTOOL_GCHANNELS:
1463 case ETHTOOL_GET_TS_INFO:
1467 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1471 if (dev->ethtool_ops->begin) {
1472 rc = dev->ethtool_ops->begin(dev);
1476 old_features = dev->features;
1480 rc = ethtool_get_settings(dev, useraddr);
1483 rc = ethtool_set_settings(dev, useraddr);
1485 case ETHTOOL_GDRVINFO:
1486 rc = ethtool_get_drvinfo(dev, useraddr);
1489 rc = ethtool_get_regs(dev, useraddr);
1492 rc = ethtool_get_wol(dev, useraddr);
1495 rc = ethtool_set_wol(dev, useraddr);
1497 case ETHTOOL_GMSGLVL:
1498 rc = ethtool_get_value(dev, useraddr, ethcmd,
1499 dev->ethtool_ops->get_msglevel);
1501 case ETHTOOL_SMSGLVL:
1502 rc = ethtool_set_value_void(dev, useraddr,
1503 dev->ethtool_ops->set_msglevel);
1506 rc = ethtool_get_eee(dev, useraddr);
1509 rc = ethtool_set_eee(dev, useraddr);
1511 case ETHTOOL_NWAY_RST:
1512 rc = ethtool_nway_reset(dev);
1515 rc = ethtool_get_link(dev, useraddr);
1517 case ETHTOOL_GEEPROM:
1518 rc = ethtool_get_eeprom(dev, useraddr);
1520 case ETHTOOL_SEEPROM:
1521 rc = ethtool_set_eeprom(dev, useraddr);
1523 case ETHTOOL_GCOALESCE:
1524 rc = ethtool_get_coalesce(dev, useraddr);
1526 case ETHTOOL_SCOALESCE:
1527 rc = ethtool_set_coalesce(dev, useraddr);
1529 case ETHTOOL_GRINGPARAM:
1530 rc = ethtool_get_ringparam(dev, useraddr);
1532 case ETHTOOL_SRINGPARAM:
1533 rc = ethtool_set_ringparam(dev, useraddr);
1535 case ETHTOOL_GPAUSEPARAM:
1536 rc = ethtool_get_pauseparam(dev, useraddr);
1538 case ETHTOOL_SPAUSEPARAM:
1539 rc = ethtool_set_pauseparam(dev, useraddr);
1542 rc = ethtool_self_test(dev, useraddr);
1544 case ETHTOOL_GSTRINGS:
1545 rc = ethtool_get_strings(dev, useraddr);
1547 case ETHTOOL_PHYS_ID:
1548 rc = ethtool_phys_id(dev, useraddr);
1550 case ETHTOOL_GSTATS:
1551 rc = ethtool_get_stats(dev, useraddr);
1553 case ETHTOOL_GPERMADDR:
1554 rc = ethtool_get_perm_addr(dev, useraddr);
1556 case ETHTOOL_GFLAGS:
1557 rc = ethtool_get_value(dev, useraddr, ethcmd,
1558 __ethtool_get_flags);
1560 case ETHTOOL_SFLAGS:
1561 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1563 case ETHTOOL_GPFLAGS:
1564 rc = ethtool_get_value(dev, useraddr, ethcmd,
1565 dev->ethtool_ops->get_priv_flags);
1567 case ETHTOOL_SPFLAGS:
1568 rc = ethtool_set_value(dev, useraddr,
1569 dev->ethtool_ops->set_priv_flags);
1572 case ETHTOOL_GRXRINGS:
1573 case ETHTOOL_GRXCLSRLCNT:
1574 case ETHTOOL_GRXCLSRULE:
1575 case ETHTOOL_GRXCLSRLALL:
1576 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1579 case ETHTOOL_SRXCLSRLDEL:
1580 case ETHTOOL_SRXCLSRLINS:
1581 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1583 case ETHTOOL_FLASHDEV:
1584 rc = ethtool_flash_device(dev, useraddr);
1587 rc = ethtool_reset(dev, useraddr);
1589 case ETHTOOL_GSSET_INFO:
1590 rc = ethtool_get_sset_info(dev, useraddr);
1592 case ETHTOOL_GRXFHINDIR:
1593 rc = ethtool_get_rxfh_indir(dev, useraddr);
1595 case ETHTOOL_SRXFHINDIR:
1596 rc = ethtool_set_rxfh_indir(dev, useraddr);
1598 case ETHTOOL_GFEATURES:
1599 rc = ethtool_get_features(dev, useraddr);
1601 case ETHTOOL_SFEATURES:
1602 rc = ethtool_set_features(dev, useraddr);
1604 case ETHTOOL_GTXCSUM:
1605 case ETHTOOL_GRXCSUM:
1611 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1613 case ETHTOOL_STXCSUM:
1614 case ETHTOOL_SRXCSUM:
1620 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1622 case ETHTOOL_GCHANNELS:
1623 rc = ethtool_get_channels(dev, useraddr);
1625 case ETHTOOL_SCHANNELS:
1626 rc = ethtool_set_channels(dev, useraddr);
1628 case ETHTOOL_SET_DUMP:
1629 rc = ethtool_set_dump(dev, useraddr);
1631 case ETHTOOL_GET_DUMP_FLAG:
1632 rc = ethtool_get_dump_flag(dev, useraddr);
1634 case ETHTOOL_GET_DUMP_DATA:
1635 rc = ethtool_get_dump_data(dev, useraddr);
1637 case ETHTOOL_GET_TS_INFO:
1638 rc = ethtool_get_ts_info(dev, useraddr);
1640 case ETHTOOL_GMODULEINFO:
1641 rc = ethtool_get_module_info(dev, useraddr);
1643 case ETHTOOL_GMODULEEEPROM:
1644 rc = ethtool_get_module_eeprom(dev, useraddr);
1650 if (dev->ethtool_ops->complete)
1651 dev->ethtool_ops->complete(dev);
1653 if (old_features != dev->features)
1654 netdev_features_change(dev);