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_TX_BIT] = "tx-vlan-hw-insert",
65 [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
66 [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
67 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
68 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
69 [NETIF_F_LLTX_BIT] = "tx-lockless",
70 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
71 [NETIF_F_GRO_BIT] = "rx-gro",
72 [NETIF_F_LRO_BIT] = "rx-lro",
74 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
75 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
76 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
77 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
78 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
79 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
80 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
81 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
83 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
84 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
85 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
86 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
87 [NETIF_F_RXHASH_BIT] = "rx-hashing",
88 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
89 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
90 [NETIF_F_LOOPBACK_BIT] = "loopback",
91 [NETIF_F_RXFCS_BIT] = "rx-fcs",
92 [NETIF_F_RXALL_BIT] = "rx-all",
95 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
97 struct ethtool_gfeatures cmd = {
98 .cmd = ETHTOOL_GFEATURES,
99 .size = ETHTOOL_DEV_FEATURE_WORDS,
101 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
102 u32 __user *sizeaddr;
106 /* in case feature bits run out again */
107 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
109 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
110 features[i].available = (u32)(dev->hw_features >> (32 * i));
111 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
112 features[i].active = (u32)(dev->features >> (32 * i));
113 features[i].never_changed =
114 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
117 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
118 if (get_user(copy_size, sizeaddr))
121 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
122 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
124 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
126 useraddr += sizeof(cmd);
127 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
133 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
135 struct ethtool_sfeatures cmd;
136 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
137 netdev_features_t wanted = 0, valid = 0;
140 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
142 useraddr += sizeof(cmd);
144 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
147 if (copy_from_user(features, useraddr, sizeof(features)))
150 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
151 valid |= (netdev_features_t)features[i].valid << (32 * i);
152 wanted |= (netdev_features_t)features[i].requested << (32 * i);
155 if (valid & ~NETIF_F_ETHTOOL_BITS)
158 if (valid & ~dev->hw_features) {
159 valid &= dev->hw_features;
160 ret |= ETHTOOL_F_UNSUPPORTED;
163 dev->wanted_features &= ~valid;
164 dev->wanted_features |= wanted & valid;
165 __netdev_update_features(dev);
167 if ((dev->wanted_features ^ dev->features) & valid)
168 ret |= ETHTOOL_F_WISH;
173 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
175 const struct ethtool_ops *ops = dev->ethtool_ops;
177 if (sset == ETH_SS_FEATURES)
178 return ARRAY_SIZE(netdev_features_strings);
180 if (ops->get_sset_count && ops->get_strings)
181 return ops->get_sset_count(dev, sset);
186 static void __ethtool_get_strings(struct net_device *dev,
187 u32 stringset, u8 *data)
189 const struct ethtool_ops *ops = dev->ethtool_ops;
191 if (stringset == ETH_SS_FEATURES)
192 memcpy(data, netdev_features_strings,
193 sizeof(netdev_features_strings));
195 /* ops->get_strings is valid because checked earlier */
196 ops->get_strings(dev, stringset, data);
199 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
201 /* feature masks of legacy discrete ethtool ops */
204 case ETHTOOL_GTXCSUM:
205 case ETHTOOL_STXCSUM:
206 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
207 case ETHTOOL_GRXCSUM:
208 case ETHTOOL_SRXCSUM:
209 return NETIF_F_RXCSUM;
215 return NETIF_F_ALL_TSO;
230 static int ethtool_get_one_feature(struct net_device *dev,
231 char __user *useraddr, u32 ethcmd)
233 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
234 struct ethtool_value edata = {
236 .data = !!(dev->features & mask),
239 if (copy_to_user(useraddr, &edata, sizeof(edata)))
244 static int ethtool_set_one_feature(struct net_device *dev,
245 void __user *useraddr, u32 ethcmd)
247 struct ethtool_value edata;
248 netdev_features_t mask;
250 if (copy_from_user(&edata, useraddr, sizeof(edata)))
253 mask = ethtool_get_feature_mask(ethcmd);
254 mask &= dev->hw_features;
259 dev->wanted_features |= mask;
261 dev->wanted_features &= ~mask;
263 __netdev_update_features(dev);
268 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
269 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
270 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
271 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
273 static u32 __ethtool_get_flags(struct net_device *dev)
277 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
278 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
279 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
280 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
281 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
286 static int __ethtool_set_flags(struct net_device *dev, u32 data)
288 netdev_features_t features = 0, changed;
290 if (data & ~ETH_ALL_FLAGS)
293 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
294 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
295 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
296 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
297 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
299 /* allow changing only bits set in hw_features */
300 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
301 if (changed & ~dev->hw_features)
302 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
304 dev->wanted_features =
305 (dev->wanted_features & ~changed) | (features & changed);
307 __netdev_update_features(dev);
312 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
316 if (!dev->ethtool_ops->get_settings)
319 memset(cmd, 0, sizeof(struct ethtool_cmd));
320 cmd->cmd = ETHTOOL_GSET;
321 return dev->ethtool_ops->get_settings(dev, cmd);
323 EXPORT_SYMBOL(__ethtool_get_settings);
325 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
328 struct ethtool_cmd cmd;
330 err = __ethtool_get_settings(dev, &cmd);
334 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
339 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
341 struct ethtool_cmd cmd;
343 if (!dev->ethtool_ops->set_settings)
346 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
349 return dev->ethtool_ops->set_settings(dev, &cmd);
352 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
353 void __user *useraddr)
355 struct ethtool_drvinfo info;
356 const struct ethtool_ops *ops = dev->ethtool_ops;
358 memset(&info, 0, sizeof(info));
359 info.cmd = ETHTOOL_GDRVINFO;
360 if (ops->get_drvinfo) {
361 ops->get_drvinfo(dev, &info);
362 } else if (dev->dev.parent && dev->dev.parent->driver) {
363 strlcpy(info.bus_info, dev_name(dev->dev.parent),
364 sizeof(info.bus_info));
365 strlcpy(info.driver, dev->dev.parent->driver->name,
366 sizeof(info.driver));
372 * this method of obtaining string set info is deprecated;
373 * Use ETHTOOL_GSSET_INFO instead.
375 if (ops->get_sset_count) {
378 rc = ops->get_sset_count(dev, ETH_SS_TEST);
380 info.testinfo_len = rc;
381 rc = ops->get_sset_count(dev, ETH_SS_STATS);
384 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
386 info.n_priv_flags = rc;
388 if (ops->get_regs_len)
389 info.regdump_len = ops->get_regs_len(dev);
390 if (ops->get_eeprom_len)
391 info.eedump_len = ops->get_eeprom_len(dev);
393 if (copy_to_user(useraddr, &info, sizeof(info)))
398 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
399 void __user *useraddr)
401 struct ethtool_sset_info info;
403 int i, idx = 0, n_bits = 0, ret, rc;
404 u32 *info_buf = NULL;
406 if (copy_from_user(&info, useraddr, sizeof(info)))
409 /* store copy of mask, because we zero struct later on */
410 sset_mask = info.sset_mask;
414 /* calculate size of return buffer */
415 n_bits = hweight64(sset_mask);
417 memset(&info, 0, sizeof(info));
418 info.cmd = ETHTOOL_GSSET_INFO;
420 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
425 * fill return buffer based on input bitmask and successful
426 * get_sset_count return
428 for (i = 0; i < 64; i++) {
429 if (!(sset_mask & (1ULL << i)))
432 rc = __ethtool_get_sset_count(dev, i);
434 info.sset_mask |= (1ULL << i);
435 info_buf[idx++] = rc;
440 if (copy_to_user(useraddr, &info, sizeof(info)))
443 useraddr += offsetof(struct ethtool_sset_info, data);
444 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
454 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
455 u32 cmd, void __user *useraddr)
457 struct ethtool_rxnfc info;
458 size_t info_size = sizeof(info);
461 if (!dev->ethtool_ops->set_rxnfc)
464 /* struct ethtool_rxnfc was originally defined for
465 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
466 * members. User-space might still be using that
468 if (cmd == ETHTOOL_SRXFH)
469 info_size = (offsetof(struct ethtool_rxnfc, data) +
472 if (copy_from_user(&info, useraddr, info_size))
475 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
479 if (cmd == ETHTOOL_SRXCLSRLINS &&
480 copy_to_user(useraddr, &info, info_size))
486 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
487 u32 cmd, void __user *useraddr)
489 struct ethtool_rxnfc info;
490 size_t info_size = sizeof(info);
491 const struct ethtool_ops *ops = dev->ethtool_ops;
493 void *rule_buf = NULL;
498 /* struct ethtool_rxnfc was originally defined for
499 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
500 * members. User-space might still be using that
502 if (cmd == ETHTOOL_GRXFH)
503 info_size = (offsetof(struct ethtool_rxnfc, data) +
506 if (copy_from_user(&info, useraddr, info_size))
509 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
510 if (info.rule_cnt > 0) {
511 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
512 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
519 ret = ops->get_rxnfc(dev, &info, rule_buf);
524 if (copy_to_user(useraddr, &info, info_size))
528 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
529 if (copy_to_user(useraddr, rule_buf,
530 info.rule_cnt * sizeof(u32)))
541 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
542 void __user *useraddr)
544 u32 user_size, dev_size;
548 if (!dev->ethtool_ops->get_rxfh_indir_size ||
549 !dev->ethtool_ops->get_rxfh_indir)
551 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
555 if (copy_from_user(&user_size,
556 useraddr + offsetof(struct ethtool_rxfh_indir, size),
560 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
561 &dev_size, sizeof(dev_size)))
564 /* If the user buffer size is 0, this is just a query for the
565 * device table size. Otherwise, if it's smaller than the
566 * device table size it's an error.
568 if (user_size < dev_size)
569 return user_size == 0 ? 0 : -EINVAL;
571 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
575 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
579 if (copy_to_user(useraddr +
580 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
581 indir, dev_size * sizeof(indir[0])))
589 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
590 void __user *useraddr)
592 struct ethtool_rxnfc rx_rings;
593 u32 user_size, dev_size, i;
595 const struct ethtool_ops *ops = dev->ethtool_ops;
598 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
602 dev_size = ops->get_rxfh_indir_size(dev);
606 if (copy_from_user(&user_size,
607 useraddr + offsetof(struct ethtool_rxfh_indir, size),
611 if (user_size != 0 && user_size != dev_size)
614 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
618 rx_rings.cmd = ETHTOOL_GRXRINGS;
619 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
623 if (user_size == 0) {
624 for (i = 0; i < dev_size; i++)
625 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
627 if (copy_from_user(indir,
629 offsetof(struct ethtool_rxfh_indir,
631 dev_size * sizeof(indir[0]))) {
636 /* Validate ring indices */
637 for (i = 0; i < dev_size; i++) {
638 if (indir[i] >= rx_rings.data) {
645 ret = ops->set_rxfh_indir(dev, indir);
652 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
654 struct ethtool_regs regs;
655 const struct ethtool_ops *ops = dev->ethtool_ops;
659 if (!ops->get_regs || !ops->get_regs_len)
662 if (copy_from_user(®s, useraddr, sizeof(regs)))
665 reglen = ops->get_regs_len(dev);
666 if (regs.len > reglen)
669 regbuf = vzalloc(reglen);
670 if (reglen && !regbuf)
673 ops->get_regs(dev, ®s, regbuf);
676 if (copy_to_user(useraddr, ®s, sizeof(regs)))
678 useraddr += offsetof(struct ethtool_regs, data);
679 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
688 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
690 struct ethtool_value reset;
693 if (!dev->ethtool_ops->reset)
696 if (copy_from_user(&reset, useraddr, sizeof(reset)))
699 ret = dev->ethtool_ops->reset(dev, &reset.data);
703 if (copy_to_user(useraddr, &reset, sizeof(reset)))
708 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
710 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
712 if (!dev->ethtool_ops->get_wol)
715 dev->ethtool_ops->get_wol(dev, &wol);
717 if (copy_to_user(useraddr, &wol, sizeof(wol)))
722 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
724 struct ethtool_wolinfo wol;
726 if (!dev->ethtool_ops->set_wol)
729 if (copy_from_user(&wol, useraddr, sizeof(wol)))
732 return dev->ethtool_ops->set_wol(dev, &wol);
735 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
737 struct ethtool_eee edata;
740 if (!dev->ethtool_ops->get_eee)
743 memset(&edata, 0, sizeof(struct ethtool_eee));
744 edata.cmd = ETHTOOL_GEEE;
745 rc = dev->ethtool_ops->get_eee(dev, &edata);
750 if (copy_to_user(useraddr, &edata, sizeof(edata)))
756 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
758 struct ethtool_eee edata;
760 if (!dev->ethtool_ops->set_eee)
763 if (copy_from_user(&edata, useraddr, sizeof(edata)))
766 return dev->ethtool_ops->set_eee(dev, &edata);
769 static int ethtool_nway_reset(struct net_device *dev)
771 if (!dev->ethtool_ops->nway_reset)
774 return dev->ethtool_ops->nway_reset(dev);
777 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
779 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
781 if (!dev->ethtool_ops->get_link)
784 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
786 if (copy_to_user(useraddr, &edata, sizeof(edata)))
791 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
792 int (*getter)(struct net_device *,
793 struct ethtool_eeprom *, u8 *),
796 struct ethtool_eeprom eeprom;
797 void __user *userbuf = useraddr + sizeof(eeprom);
802 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
805 /* Check for wrap and zero */
806 if (eeprom.offset + eeprom.len <= eeprom.offset)
809 /* Check for exceeding total eeprom len */
810 if (eeprom.offset + eeprom.len > total_len)
813 data = kmalloc(PAGE_SIZE, GFP_USER);
817 bytes_remaining = eeprom.len;
818 while (bytes_remaining > 0) {
819 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
821 ret = getter(dev, &eeprom, data);
824 if (copy_to_user(userbuf, data, eeprom.len)) {
828 userbuf += eeprom.len;
829 eeprom.offset += eeprom.len;
830 bytes_remaining -= eeprom.len;
833 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
834 eeprom.offset -= eeprom.len;
835 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
842 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
844 const struct ethtool_ops *ops = dev->ethtool_ops;
846 if (!ops->get_eeprom || !ops->get_eeprom_len)
849 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
850 ops->get_eeprom_len(dev));
853 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
855 struct ethtool_eeprom eeprom;
856 const struct ethtool_ops *ops = dev->ethtool_ops;
857 void __user *userbuf = useraddr + sizeof(eeprom);
862 if (!ops->set_eeprom || !ops->get_eeprom_len)
865 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
868 /* Check for wrap and zero */
869 if (eeprom.offset + eeprom.len <= eeprom.offset)
872 /* Check for exceeding total eeprom len */
873 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
876 data = kmalloc(PAGE_SIZE, GFP_USER);
880 bytes_remaining = eeprom.len;
881 while (bytes_remaining > 0) {
882 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
884 if (copy_from_user(data, userbuf, eeprom.len)) {
888 ret = ops->set_eeprom(dev, &eeprom, data);
891 userbuf += eeprom.len;
892 eeprom.offset += eeprom.len;
893 bytes_remaining -= eeprom.len;
900 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
901 void __user *useraddr)
903 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
905 if (!dev->ethtool_ops->get_coalesce)
908 dev->ethtool_ops->get_coalesce(dev, &coalesce);
910 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
915 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
916 void __user *useraddr)
918 struct ethtool_coalesce coalesce;
920 if (!dev->ethtool_ops->set_coalesce)
923 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
926 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
929 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
931 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
933 if (!dev->ethtool_ops->get_ringparam)
936 dev->ethtool_ops->get_ringparam(dev, &ringparam);
938 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
943 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
945 struct ethtool_ringparam ringparam;
947 if (!dev->ethtool_ops->set_ringparam)
950 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
953 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
956 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
957 void __user *useraddr)
959 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
961 if (!dev->ethtool_ops->get_channels)
964 dev->ethtool_ops->get_channels(dev, &channels);
966 if (copy_to_user(useraddr, &channels, sizeof(channels)))
971 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
972 void __user *useraddr)
974 struct ethtool_channels channels;
976 if (!dev->ethtool_ops->set_channels)
979 if (copy_from_user(&channels, useraddr, sizeof(channels)))
982 return dev->ethtool_ops->set_channels(dev, &channels);
985 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
987 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
989 if (!dev->ethtool_ops->get_pauseparam)
992 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
994 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
999 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1001 struct ethtool_pauseparam pauseparam;
1003 if (!dev->ethtool_ops->set_pauseparam)
1006 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1009 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1012 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1014 struct ethtool_test test;
1015 const struct ethtool_ops *ops = dev->ethtool_ops;
1019 if (!ops->self_test || !ops->get_sset_count)
1022 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1025 WARN_ON(test_len == 0);
1027 if (copy_from_user(&test, useraddr, sizeof(test)))
1030 test.len = test_len;
1031 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1035 ops->self_test(dev, &test, data);
1038 if (copy_to_user(useraddr, &test, sizeof(test)))
1040 useraddr += sizeof(test);
1041 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1050 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1052 struct ethtool_gstrings gstrings;
1056 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1059 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1065 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1069 __ethtool_get_strings(dev, gstrings.string_set, data);
1072 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1074 useraddr += sizeof(gstrings);
1075 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1084 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1086 struct ethtool_value id;
1088 const struct ethtool_ops *ops = dev->ethtool_ops;
1091 if (!ops->set_phys_id)
1097 if (copy_from_user(&id, useraddr, sizeof(id)))
1100 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1104 /* Drop the RTNL lock while waiting, but prevent reentry or
1105 * removal of the device.
1112 /* Driver will handle this itself */
1113 schedule_timeout_interruptible(
1114 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1116 /* Driver expects to be called at twice the frequency in rc */
1117 int n = rc * 2, i, interval = HZ / n;
1119 /* Count down seconds */
1121 /* Count down iterations per second */
1125 rc = ops->set_phys_id(dev,
1126 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1130 schedule_timeout_interruptible(interval);
1131 } while (!signal_pending(current) && --i != 0);
1132 } while (!signal_pending(current) &&
1133 (id.data == 0 || --id.data != 0));
1140 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1144 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1146 struct ethtool_stats stats;
1147 const struct ethtool_ops *ops = dev->ethtool_ops;
1151 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1154 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1157 WARN_ON(n_stats == 0);
1159 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1162 stats.n_stats = n_stats;
1163 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1167 ops->get_ethtool_stats(dev, &stats, data);
1170 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1172 useraddr += sizeof(stats);
1173 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1182 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1184 struct ethtool_perm_addr epaddr;
1186 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1189 if (epaddr.size < dev->addr_len)
1191 epaddr.size = dev->addr_len;
1193 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1195 useraddr += sizeof(epaddr);
1196 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1201 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1202 u32 cmd, u32 (*actor)(struct net_device *))
1204 struct ethtool_value edata = { .cmd = cmd };
1209 edata.data = actor(dev);
1211 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1216 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1217 void (*actor)(struct net_device *, u32))
1219 struct ethtool_value edata;
1224 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1227 actor(dev, edata.data);
1231 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1232 int (*actor)(struct net_device *, u32))
1234 struct ethtool_value edata;
1239 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1242 return actor(dev, edata.data);
1245 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1246 char __user *useraddr)
1248 struct ethtool_flash efl;
1250 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1253 if (!dev->ethtool_ops->flash_device)
1256 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1258 return dev->ethtool_ops->flash_device(dev, &efl);
1261 static int ethtool_set_dump(struct net_device *dev,
1262 void __user *useraddr)
1264 struct ethtool_dump dump;
1266 if (!dev->ethtool_ops->set_dump)
1269 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1272 return dev->ethtool_ops->set_dump(dev, &dump);
1275 static int ethtool_get_dump_flag(struct net_device *dev,
1276 void __user *useraddr)
1279 struct ethtool_dump dump;
1280 const struct ethtool_ops *ops = dev->ethtool_ops;
1282 if (!ops->get_dump_flag)
1285 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1288 ret = ops->get_dump_flag(dev, &dump);
1292 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1297 static int ethtool_get_dump_data(struct net_device *dev,
1298 void __user *useraddr)
1302 struct ethtool_dump dump, tmp;
1303 const struct ethtool_ops *ops = dev->ethtool_ops;
1306 if (!ops->get_dump_data || !ops->get_dump_flag)
1309 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1312 memset(&tmp, 0, sizeof(tmp));
1313 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1314 ret = ops->get_dump_flag(dev, &tmp);
1318 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1322 data = vzalloc(tmp.len);
1325 ret = ops->get_dump_data(dev, &dump, data);
1329 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1333 useraddr += offsetof(struct ethtool_dump, data);
1334 if (copy_to_user(useraddr, data, len))
1341 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1344 struct ethtool_ts_info info;
1345 const struct ethtool_ops *ops = dev->ethtool_ops;
1346 struct phy_device *phydev = dev->phydev;
1348 memset(&info, 0, sizeof(info));
1349 info.cmd = ETHTOOL_GET_TS_INFO;
1351 if (phydev && phydev->drv && phydev->drv->ts_info) {
1352 err = phydev->drv->ts_info(phydev, &info);
1353 } else if (ops->get_ts_info) {
1354 err = ops->get_ts_info(dev, &info);
1356 info.so_timestamping =
1357 SOF_TIMESTAMPING_RX_SOFTWARE |
1358 SOF_TIMESTAMPING_SOFTWARE;
1359 info.phc_index = -1;
1365 if (copy_to_user(useraddr, &info, sizeof(info)))
1371 static int ethtool_get_module_info(struct net_device *dev,
1372 void __user *useraddr)
1375 struct ethtool_modinfo modinfo;
1376 const struct ethtool_ops *ops = dev->ethtool_ops;
1378 if (!ops->get_module_info)
1381 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1384 ret = ops->get_module_info(dev, &modinfo);
1388 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1394 static int ethtool_get_module_eeprom(struct net_device *dev,
1395 void __user *useraddr)
1398 struct ethtool_modinfo modinfo;
1399 const struct ethtool_ops *ops = dev->ethtool_ops;
1401 if (!ops->get_module_info || !ops->get_module_eeprom)
1404 ret = ops->get_module_info(dev, &modinfo);
1408 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1409 modinfo.eeprom_len);
1412 /* The main entry point in this file. Called from net/core/dev.c */
1414 int dev_ethtool(struct net *net, struct ifreq *ifr)
1416 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1417 void __user *useraddr = ifr->ifr_data;
1422 if (!dev || !netif_device_present(dev))
1425 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1428 /* Allow some commands to be done by anyone */
1431 case ETHTOOL_GDRVINFO:
1432 case ETHTOOL_GMSGLVL:
1434 case ETHTOOL_GCOALESCE:
1435 case ETHTOOL_GRINGPARAM:
1436 case ETHTOOL_GPAUSEPARAM:
1437 case ETHTOOL_GRXCSUM:
1438 case ETHTOOL_GTXCSUM:
1440 case ETHTOOL_GSSET_INFO:
1441 case ETHTOOL_GSTRINGS:
1442 case ETHTOOL_GSTATS:
1444 case ETHTOOL_GPERMADDR:
1448 case ETHTOOL_GFLAGS:
1449 case ETHTOOL_GPFLAGS:
1451 case ETHTOOL_GRXRINGS:
1452 case ETHTOOL_GRXCLSRLCNT:
1453 case ETHTOOL_GRXCLSRULE:
1454 case ETHTOOL_GRXCLSRLALL:
1455 case ETHTOOL_GRXFHINDIR:
1456 case ETHTOOL_GFEATURES:
1457 case ETHTOOL_GCHANNELS:
1458 case ETHTOOL_GET_TS_INFO:
1462 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1466 if (dev->ethtool_ops->begin) {
1467 rc = dev->ethtool_ops->begin(dev);
1471 old_features = dev->features;
1475 rc = ethtool_get_settings(dev, useraddr);
1478 rc = ethtool_set_settings(dev, useraddr);
1480 case ETHTOOL_GDRVINFO:
1481 rc = ethtool_get_drvinfo(dev, useraddr);
1484 rc = ethtool_get_regs(dev, useraddr);
1487 rc = ethtool_get_wol(dev, useraddr);
1490 rc = ethtool_set_wol(dev, useraddr);
1492 case ETHTOOL_GMSGLVL:
1493 rc = ethtool_get_value(dev, useraddr, ethcmd,
1494 dev->ethtool_ops->get_msglevel);
1496 case ETHTOOL_SMSGLVL:
1497 rc = ethtool_set_value_void(dev, useraddr,
1498 dev->ethtool_ops->set_msglevel);
1501 rc = ethtool_get_eee(dev, useraddr);
1504 rc = ethtool_set_eee(dev, useraddr);
1506 case ETHTOOL_NWAY_RST:
1507 rc = ethtool_nway_reset(dev);
1510 rc = ethtool_get_link(dev, useraddr);
1512 case ETHTOOL_GEEPROM:
1513 rc = ethtool_get_eeprom(dev, useraddr);
1515 case ETHTOOL_SEEPROM:
1516 rc = ethtool_set_eeprom(dev, useraddr);
1518 case ETHTOOL_GCOALESCE:
1519 rc = ethtool_get_coalesce(dev, useraddr);
1521 case ETHTOOL_SCOALESCE:
1522 rc = ethtool_set_coalesce(dev, useraddr);
1524 case ETHTOOL_GRINGPARAM:
1525 rc = ethtool_get_ringparam(dev, useraddr);
1527 case ETHTOOL_SRINGPARAM:
1528 rc = ethtool_set_ringparam(dev, useraddr);
1530 case ETHTOOL_GPAUSEPARAM:
1531 rc = ethtool_get_pauseparam(dev, useraddr);
1533 case ETHTOOL_SPAUSEPARAM:
1534 rc = ethtool_set_pauseparam(dev, useraddr);
1537 rc = ethtool_self_test(dev, useraddr);
1539 case ETHTOOL_GSTRINGS:
1540 rc = ethtool_get_strings(dev, useraddr);
1542 case ETHTOOL_PHYS_ID:
1543 rc = ethtool_phys_id(dev, useraddr);
1545 case ETHTOOL_GSTATS:
1546 rc = ethtool_get_stats(dev, useraddr);
1548 case ETHTOOL_GPERMADDR:
1549 rc = ethtool_get_perm_addr(dev, useraddr);
1551 case ETHTOOL_GFLAGS:
1552 rc = ethtool_get_value(dev, useraddr, ethcmd,
1553 __ethtool_get_flags);
1555 case ETHTOOL_SFLAGS:
1556 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1558 case ETHTOOL_GPFLAGS:
1559 rc = ethtool_get_value(dev, useraddr, ethcmd,
1560 dev->ethtool_ops->get_priv_flags);
1562 case ETHTOOL_SPFLAGS:
1563 rc = ethtool_set_value(dev, useraddr,
1564 dev->ethtool_ops->set_priv_flags);
1567 case ETHTOOL_GRXRINGS:
1568 case ETHTOOL_GRXCLSRLCNT:
1569 case ETHTOOL_GRXCLSRULE:
1570 case ETHTOOL_GRXCLSRLALL:
1571 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1574 case ETHTOOL_SRXCLSRLDEL:
1575 case ETHTOOL_SRXCLSRLINS:
1576 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1578 case ETHTOOL_FLASHDEV:
1579 rc = ethtool_flash_device(dev, useraddr);
1582 rc = ethtool_reset(dev, useraddr);
1584 case ETHTOOL_GSSET_INFO:
1585 rc = ethtool_get_sset_info(dev, useraddr);
1587 case ETHTOOL_GRXFHINDIR:
1588 rc = ethtool_get_rxfh_indir(dev, useraddr);
1590 case ETHTOOL_SRXFHINDIR:
1591 rc = ethtool_set_rxfh_indir(dev, useraddr);
1593 case ETHTOOL_GFEATURES:
1594 rc = ethtool_get_features(dev, useraddr);
1596 case ETHTOOL_SFEATURES:
1597 rc = ethtool_set_features(dev, useraddr);
1599 case ETHTOOL_GTXCSUM:
1600 case ETHTOOL_GRXCSUM:
1606 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1608 case ETHTOOL_STXCSUM:
1609 case ETHTOOL_SRXCSUM:
1615 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1617 case ETHTOOL_GCHANNELS:
1618 rc = ethtool_get_channels(dev, useraddr);
1620 case ETHTOOL_SCHANNELS:
1621 rc = ethtool_set_channels(dev, useraddr);
1623 case ETHTOOL_SET_DUMP:
1624 rc = ethtool_set_dump(dev, useraddr);
1626 case ETHTOOL_GET_DUMP_FLAG:
1627 rc = ethtool_get_dump_flag(dev, useraddr);
1629 case ETHTOOL_GET_DUMP_DATA:
1630 rc = ethtool_get_dump_data(dev, useraddr);
1632 case ETHTOOL_GET_TS_INFO:
1633 rc = ethtool_get_ts_info(dev, useraddr);
1635 case ETHTOOL_GMODULEINFO:
1636 rc = ethtool_get_module_info(dev, useraddr);
1638 case ETHTOOL_GMODULEEEPROM:
1639 rc = ethtool_get_module_eeprom(dev, useraddr);
1645 if (dev->ethtool_ops->complete)
1646 dev->ethtool_ops->complete(dev);
1648 if (old_features != dev->features)
1649 netdev_features_change(dev);