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_IPIP_BIT] = "tx-ipip-segmentation",
85 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
86 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
88 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
89 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
90 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
91 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
92 [NETIF_F_RXHASH_BIT] = "rx-hashing",
93 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
94 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
95 [NETIF_F_LOOPBACK_BIT] = "loopback",
96 [NETIF_F_RXFCS_BIT] = "rx-fcs",
97 [NETIF_F_RXALL_BIT] = "rx-all",
100 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
102 struct ethtool_gfeatures cmd = {
103 .cmd = ETHTOOL_GFEATURES,
104 .size = ETHTOOL_DEV_FEATURE_WORDS,
106 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
107 u32 __user *sizeaddr;
111 /* in case feature bits run out again */
112 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
114 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
115 features[i].available = (u32)(dev->hw_features >> (32 * i));
116 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
117 features[i].active = (u32)(dev->features >> (32 * i));
118 features[i].never_changed =
119 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
122 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
123 if (get_user(copy_size, sizeaddr))
126 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
127 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
129 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
131 useraddr += sizeof(cmd);
132 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
138 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
140 struct ethtool_sfeatures cmd;
141 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
142 netdev_features_t wanted = 0, valid = 0;
145 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
147 useraddr += sizeof(cmd);
149 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
152 if (copy_from_user(features, useraddr, sizeof(features)))
155 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
156 valid |= (netdev_features_t)features[i].valid << (32 * i);
157 wanted |= (netdev_features_t)features[i].requested << (32 * i);
160 if (valid & ~NETIF_F_ETHTOOL_BITS)
163 if (valid & ~dev->hw_features) {
164 valid &= dev->hw_features;
165 ret |= ETHTOOL_F_UNSUPPORTED;
168 dev->wanted_features &= ~valid;
169 dev->wanted_features |= wanted & valid;
170 __netdev_update_features(dev);
172 if ((dev->wanted_features ^ dev->features) & valid)
173 ret |= ETHTOOL_F_WISH;
178 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
180 const struct ethtool_ops *ops = dev->ethtool_ops;
182 if (sset == ETH_SS_FEATURES)
183 return ARRAY_SIZE(netdev_features_strings);
185 if (ops->get_sset_count && ops->get_strings)
186 return ops->get_sset_count(dev, sset);
191 static void __ethtool_get_strings(struct net_device *dev,
192 u32 stringset, u8 *data)
194 const struct ethtool_ops *ops = dev->ethtool_ops;
196 if (stringset == ETH_SS_FEATURES)
197 memcpy(data, netdev_features_strings,
198 sizeof(netdev_features_strings));
200 /* ops->get_strings is valid because checked earlier */
201 ops->get_strings(dev, stringset, data);
204 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
206 /* feature masks of legacy discrete ethtool ops */
209 case ETHTOOL_GTXCSUM:
210 case ETHTOOL_STXCSUM:
211 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
212 case ETHTOOL_GRXCSUM:
213 case ETHTOOL_SRXCSUM:
214 return NETIF_F_RXCSUM;
220 return NETIF_F_ALL_TSO;
235 static int ethtool_get_one_feature(struct net_device *dev,
236 char __user *useraddr, u32 ethcmd)
238 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
239 struct ethtool_value edata = {
241 .data = !!(dev->features & mask),
244 if (copy_to_user(useraddr, &edata, sizeof(edata)))
249 static int ethtool_set_one_feature(struct net_device *dev,
250 void __user *useraddr, u32 ethcmd)
252 struct ethtool_value edata;
253 netdev_features_t mask;
255 if (copy_from_user(&edata, useraddr, sizeof(edata)))
258 mask = ethtool_get_feature_mask(ethcmd);
259 mask &= dev->hw_features;
264 dev->wanted_features |= mask;
266 dev->wanted_features &= ~mask;
268 __netdev_update_features(dev);
273 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
274 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
275 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
276 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
279 static u32 __ethtool_get_flags(struct net_device *dev)
283 if (dev->features & NETIF_F_LRO)
284 flags |= ETH_FLAG_LRO;
285 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
286 flags |= ETH_FLAG_RXVLAN;
287 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
288 flags |= ETH_FLAG_TXVLAN;
289 if (dev->features & NETIF_F_NTUPLE)
290 flags |= ETH_FLAG_NTUPLE;
291 if (dev->features & NETIF_F_RXHASH)
292 flags |= ETH_FLAG_RXHASH;
297 static int __ethtool_set_flags(struct net_device *dev, u32 data)
299 netdev_features_t features = 0, changed;
301 if (data & ~ETH_ALL_FLAGS)
304 if (data & ETH_FLAG_LRO)
305 features |= NETIF_F_LRO;
306 if (data & ETH_FLAG_RXVLAN)
307 features |= NETIF_F_HW_VLAN_CTAG_RX;
308 if (data & ETH_FLAG_TXVLAN)
309 features |= NETIF_F_HW_VLAN_CTAG_TX;
310 if (data & ETH_FLAG_NTUPLE)
311 features |= NETIF_F_NTUPLE;
312 if (data & ETH_FLAG_RXHASH)
313 features |= NETIF_F_RXHASH;
315 /* allow changing only bits set in hw_features */
316 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
317 if (changed & ~dev->hw_features)
318 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
320 dev->wanted_features =
321 (dev->wanted_features & ~changed) | (features & changed);
323 __netdev_update_features(dev);
328 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
332 if (!dev->ethtool_ops->get_settings)
335 memset(cmd, 0, sizeof(struct ethtool_cmd));
336 cmd->cmd = ETHTOOL_GSET;
337 return dev->ethtool_ops->get_settings(dev, cmd);
339 EXPORT_SYMBOL(__ethtool_get_settings);
341 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
344 struct ethtool_cmd cmd;
346 err = __ethtool_get_settings(dev, &cmd);
350 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
355 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
357 struct ethtool_cmd cmd;
359 if (!dev->ethtool_ops->set_settings)
362 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
365 return dev->ethtool_ops->set_settings(dev, &cmd);
368 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
369 void __user *useraddr)
371 struct ethtool_drvinfo info;
372 const struct ethtool_ops *ops = dev->ethtool_ops;
374 memset(&info, 0, sizeof(info));
375 info.cmd = ETHTOOL_GDRVINFO;
376 if (ops->get_drvinfo) {
377 ops->get_drvinfo(dev, &info);
378 } else if (dev->dev.parent && dev->dev.parent->driver) {
379 strlcpy(info.bus_info, dev_name(dev->dev.parent),
380 sizeof(info.bus_info));
381 strlcpy(info.driver, dev->dev.parent->driver->name,
382 sizeof(info.driver));
388 * this method of obtaining string set info is deprecated;
389 * Use ETHTOOL_GSSET_INFO instead.
391 if (ops->get_sset_count) {
394 rc = ops->get_sset_count(dev, ETH_SS_TEST);
396 info.testinfo_len = rc;
397 rc = ops->get_sset_count(dev, ETH_SS_STATS);
400 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
402 info.n_priv_flags = rc;
404 if (ops->get_regs_len)
405 info.regdump_len = ops->get_regs_len(dev);
406 if (ops->get_eeprom_len)
407 info.eedump_len = ops->get_eeprom_len(dev);
409 if (copy_to_user(useraddr, &info, sizeof(info)))
414 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
415 void __user *useraddr)
417 struct ethtool_sset_info info;
419 int i, idx = 0, n_bits = 0, ret, rc;
420 u32 *info_buf = NULL;
422 if (copy_from_user(&info, useraddr, sizeof(info)))
425 /* store copy of mask, because we zero struct later on */
426 sset_mask = info.sset_mask;
430 /* calculate size of return buffer */
431 n_bits = hweight64(sset_mask);
433 memset(&info, 0, sizeof(info));
434 info.cmd = ETHTOOL_GSSET_INFO;
436 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
441 * fill return buffer based on input bitmask and successful
442 * get_sset_count return
444 for (i = 0; i < 64; i++) {
445 if (!(sset_mask & (1ULL << i)))
448 rc = __ethtool_get_sset_count(dev, i);
450 info.sset_mask |= (1ULL << i);
451 info_buf[idx++] = rc;
456 if (copy_to_user(useraddr, &info, sizeof(info)))
459 useraddr += offsetof(struct ethtool_sset_info, data);
460 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
470 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
471 u32 cmd, void __user *useraddr)
473 struct ethtool_rxnfc info;
474 size_t info_size = sizeof(info);
477 if (!dev->ethtool_ops->set_rxnfc)
480 /* struct ethtool_rxnfc was originally defined for
481 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
482 * members. User-space might still be using that
484 if (cmd == ETHTOOL_SRXFH)
485 info_size = (offsetof(struct ethtool_rxnfc, data) +
488 if (copy_from_user(&info, useraddr, info_size))
491 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
495 if (cmd == ETHTOOL_SRXCLSRLINS &&
496 copy_to_user(useraddr, &info, info_size))
502 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
503 u32 cmd, void __user *useraddr)
505 struct ethtool_rxnfc info;
506 size_t info_size = sizeof(info);
507 const struct ethtool_ops *ops = dev->ethtool_ops;
509 void *rule_buf = NULL;
514 /* struct ethtool_rxnfc was originally defined for
515 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
516 * members. User-space might still be using that
518 if (cmd == ETHTOOL_GRXFH)
519 info_size = (offsetof(struct ethtool_rxnfc, data) +
522 if (copy_from_user(&info, useraddr, info_size))
525 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
526 if (info.rule_cnt > 0) {
527 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
528 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
535 ret = ops->get_rxnfc(dev, &info, rule_buf);
540 if (copy_to_user(useraddr, &info, info_size))
544 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
545 if (copy_to_user(useraddr, rule_buf,
546 info.rule_cnt * sizeof(u32)))
557 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
558 void __user *useraddr)
560 u32 user_size, dev_size;
564 if (!dev->ethtool_ops->get_rxfh_indir_size ||
565 !dev->ethtool_ops->get_rxfh_indir)
567 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
571 if (copy_from_user(&user_size,
572 useraddr + offsetof(struct ethtool_rxfh_indir, size),
576 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
577 &dev_size, sizeof(dev_size)))
580 /* If the user buffer size is 0, this is just a query for the
581 * device table size. Otherwise, if it's smaller than the
582 * device table size it's an error.
584 if (user_size < dev_size)
585 return user_size == 0 ? 0 : -EINVAL;
587 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
591 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
595 if (copy_to_user(useraddr +
596 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
597 indir, dev_size * sizeof(indir[0])))
605 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
606 void __user *useraddr)
608 struct ethtool_rxnfc rx_rings;
609 u32 user_size, dev_size, i;
611 const struct ethtool_ops *ops = dev->ethtool_ops;
614 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
618 dev_size = ops->get_rxfh_indir_size(dev);
622 if (copy_from_user(&user_size,
623 useraddr + offsetof(struct ethtool_rxfh_indir, size),
627 if (user_size != 0 && user_size != dev_size)
630 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
634 rx_rings.cmd = ETHTOOL_GRXRINGS;
635 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
639 if (user_size == 0) {
640 for (i = 0; i < dev_size; i++)
641 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
643 if (copy_from_user(indir,
645 offsetof(struct ethtool_rxfh_indir,
647 dev_size * sizeof(indir[0]))) {
652 /* Validate ring indices */
653 for (i = 0; i < dev_size; i++) {
654 if (indir[i] >= rx_rings.data) {
661 ret = ops->set_rxfh_indir(dev, indir);
668 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
670 struct ethtool_regs regs;
671 const struct ethtool_ops *ops = dev->ethtool_ops;
675 if (!ops->get_regs || !ops->get_regs_len)
678 if (copy_from_user(®s, useraddr, sizeof(regs)))
681 reglen = ops->get_regs_len(dev);
682 if (regs.len > reglen)
685 regbuf = vzalloc(reglen);
686 if (reglen && !regbuf)
689 ops->get_regs(dev, ®s, regbuf);
692 if (copy_to_user(useraddr, ®s, sizeof(regs)))
694 useraddr += offsetof(struct ethtool_regs, data);
695 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
704 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
706 struct ethtool_value reset;
709 if (!dev->ethtool_ops->reset)
712 if (copy_from_user(&reset, useraddr, sizeof(reset)))
715 ret = dev->ethtool_ops->reset(dev, &reset.data);
719 if (copy_to_user(useraddr, &reset, sizeof(reset)))
724 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
726 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
728 if (!dev->ethtool_ops->get_wol)
731 dev->ethtool_ops->get_wol(dev, &wol);
733 if (copy_to_user(useraddr, &wol, sizeof(wol)))
738 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
740 struct ethtool_wolinfo wol;
742 if (!dev->ethtool_ops->set_wol)
745 if (copy_from_user(&wol, useraddr, sizeof(wol)))
748 return dev->ethtool_ops->set_wol(dev, &wol);
751 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
753 struct ethtool_eee edata;
756 if (!dev->ethtool_ops->get_eee)
759 memset(&edata, 0, sizeof(struct ethtool_eee));
760 edata.cmd = ETHTOOL_GEEE;
761 rc = dev->ethtool_ops->get_eee(dev, &edata);
766 if (copy_to_user(useraddr, &edata, sizeof(edata)))
772 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
774 struct ethtool_eee edata;
776 if (!dev->ethtool_ops->set_eee)
779 if (copy_from_user(&edata, useraddr, sizeof(edata)))
782 return dev->ethtool_ops->set_eee(dev, &edata);
785 static int ethtool_nway_reset(struct net_device *dev)
787 if (!dev->ethtool_ops->nway_reset)
790 return dev->ethtool_ops->nway_reset(dev);
793 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
795 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
797 if (!dev->ethtool_ops->get_link)
800 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
802 if (copy_to_user(useraddr, &edata, sizeof(edata)))
807 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
808 int (*getter)(struct net_device *,
809 struct ethtool_eeprom *, u8 *),
812 struct ethtool_eeprom eeprom;
813 void __user *userbuf = useraddr + sizeof(eeprom);
818 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
821 /* Check for wrap and zero */
822 if (eeprom.offset + eeprom.len <= eeprom.offset)
825 /* Check for exceeding total eeprom len */
826 if (eeprom.offset + eeprom.len > total_len)
829 data = kmalloc(PAGE_SIZE, GFP_USER);
833 bytes_remaining = eeprom.len;
834 while (bytes_remaining > 0) {
835 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
837 ret = getter(dev, &eeprom, data);
840 if (copy_to_user(userbuf, data, eeprom.len)) {
844 userbuf += eeprom.len;
845 eeprom.offset += eeprom.len;
846 bytes_remaining -= eeprom.len;
849 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
850 eeprom.offset -= eeprom.len;
851 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
858 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
860 const struct ethtool_ops *ops = dev->ethtool_ops;
862 if (!ops->get_eeprom || !ops->get_eeprom_len)
865 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
866 ops->get_eeprom_len(dev));
869 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
871 struct ethtool_eeprom eeprom;
872 const struct ethtool_ops *ops = dev->ethtool_ops;
873 void __user *userbuf = useraddr + sizeof(eeprom);
878 if (!ops->set_eeprom || !ops->get_eeprom_len)
881 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
884 /* Check for wrap and zero */
885 if (eeprom.offset + eeprom.len <= eeprom.offset)
888 /* Check for exceeding total eeprom len */
889 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
892 data = kmalloc(PAGE_SIZE, GFP_USER);
896 bytes_remaining = eeprom.len;
897 while (bytes_remaining > 0) {
898 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
900 if (copy_from_user(data, userbuf, eeprom.len)) {
904 ret = ops->set_eeprom(dev, &eeprom, data);
907 userbuf += eeprom.len;
908 eeprom.offset += eeprom.len;
909 bytes_remaining -= eeprom.len;
916 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
917 void __user *useraddr)
919 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
921 if (!dev->ethtool_ops->get_coalesce)
924 dev->ethtool_ops->get_coalesce(dev, &coalesce);
926 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
931 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
932 void __user *useraddr)
934 struct ethtool_coalesce coalesce;
936 if (!dev->ethtool_ops->set_coalesce)
939 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
942 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
945 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
947 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
949 if (!dev->ethtool_ops->get_ringparam)
952 dev->ethtool_ops->get_ringparam(dev, &ringparam);
954 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
959 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
961 struct ethtool_ringparam ringparam;
963 if (!dev->ethtool_ops->set_ringparam)
966 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
969 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
972 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
973 void __user *useraddr)
975 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
977 if (!dev->ethtool_ops->get_channels)
980 dev->ethtool_ops->get_channels(dev, &channels);
982 if (copy_to_user(useraddr, &channels, sizeof(channels)))
987 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
988 void __user *useraddr)
990 struct ethtool_channels channels;
992 if (!dev->ethtool_ops->set_channels)
995 if (copy_from_user(&channels, useraddr, sizeof(channels)))
998 return dev->ethtool_ops->set_channels(dev, &channels);
1001 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1003 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1005 if (!dev->ethtool_ops->get_pauseparam)
1008 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1010 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1015 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1017 struct ethtool_pauseparam pauseparam;
1019 if (!dev->ethtool_ops->set_pauseparam)
1022 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1025 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1028 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1030 struct ethtool_test test;
1031 const struct ethtool_ops *ops = dev->ethtool_ops;
1035 if (!ops->self_test || !ops->get_sset_count)
1038 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1041 WARN_ON(test_len == 0);
1043 if (copy_from_user(&test, useraddr, sizeof(test)))
1046 test.len = test_len;
1047 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1051 ops->self_test(dev, &test, data);
1054 if (copy_to_user(useraddr, &test, sizeof(test)))
1056 useraddr += sizeof(test);
1057 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1066 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1068 struct ethtool_gstrings gstrings;
1072 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1075 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1081 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1085 __ethtool_get_strings(dev, gstrings.string_set, data);
1088 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1090 useraddr += sizeof(gstrings);
1091 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1100 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1102 struct ethtool_value id;
1104 const struct ethtool_ops *ops = dev->ethtool_ops;
1107 if (!ops->set_phys_id)
1113 if (copy_from_user(&id, useraddr, sizeof(id)))
1116 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1120 /* Drop the RTNL lock while waiting, but prevent reentry or
1121 * removal of the device.
1128 /* Driver will handle this itself */
1129 schedule_timeout_interruptible(
1130 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1132 /* Driver expects to be called at twice the frequency in rc */
1133 int n = rc * 2, i, interval = HZ / n;
1135 /* Count down seconds */
1137 /* Count down iterations per second */
1141 rc = ops->set_phys_id(dev,
1142 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1146 schedule_timeout_interruptible(interval);
1147 } while (!signal_pending(current) && --i != 0);
1148 } while (!signal_pending(current) &&
1149 (id.data == 0 || --id.data != 0));
1156 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1160 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1162 struct ethtool_stats stats;
1163 const struct ethtool_ops *ops = dev->ethtool_ops;
1167 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1170 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1173 WARN_ON(n_stats == 0);
1175 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1178 stats.n_stats = n_stats;
1179 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1183 ops->get_ethtool_stats(dev, &stats, data);
1186 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1188 useraddr += sizeof(stats);
1189 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1198 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1200 struct ethtool_perm_addr epaddr;
1202 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1205 if (epaddr.size < dev->addr_len)
1207 epaddr.size = dev->addr_len;
1209 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1211 useraddr += sizeof(epaddr);
1212 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1217 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1218 u32 cmd, u32 (*actor)(struct net_device *))
1220 struct ethtool_value edata = { .cmd = cmd };
1225 edata.data = actor(dev);
1227 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1232 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1233 void (*actor)(struct net_device *, u32))
1235 struct ethtool_value edata;
1240 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1243 actor(dev, edata.data);
1247 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1248 int (*actor)(struct net_device *, u32))
1250 struct ethtool_value edata;
1255 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1258 return actor(dev, edata.data);
1261 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1262 char __user *useraddr)
1264 struct ethtool_flash efl;
1266 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1269 if (!dev->ethtool_ops->flash_device)
1272 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1274 return dev->ethtool_ops->flash_device(dev, &efl);
1277 static int ethtool_set_dump(struct net_device *dev,
1278 void __user *useraddr)
1280 struct ethtool_dump dump;
1282 if (!dev->ethtool_ops->set_dump)
1285 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1288 return dev->ethtool_ops->set_dump(dev, &dump);
1291 static int ethtool_get_dump_flag(struct net_device *dev,
1292 void __user *useraddr)
1295 struct ethtool_dump dump;
1296 const struct ethtool_ops *ops = dev->ethtool_ops;
1298 if (!ops->get_dump_flag)
1301 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1304 ret = ops->get_dump_flag(dev, &dump);
1308 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1313 static int ethtool_get_dump_data(struct net_device *dev,
1314 void __user *useraddr)
1318 struct ethtool_dump dump, tmp;
1319 const struct ethtool_ops *ops = dev->ethtool_ops;
1322 if (!ops->get_dump_data || !ops->get_dump_flag)
1325 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1328 memset(&tmp, 0, sizeof(tmp));
1329 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1330 ret = ops->get_dump_flag(dev, &tmp);
1334 len = min(tmp.len, dump.len);
1338 /* Don't ever let the driver think there's more space available
1339 * than it requested with .get_dump_flag().
1343 /* Always allocate enough space to hold the whole thing so that the
1344 * driver does not need to check the length and bother with partial
1347 data = vzalloc(tmp.len);
1350 ret = ops->get_dump_data(dev, &dump, data);
1354 /* There are two sane possibilities:
1355 * 1. The driver's .get_dump_data() does not touch dump.len.
1356 * 2. Or it may set dump.len to how much it really writes, which
1357 * should be tmp.len (or len if it can do a partial dump).
1358 * In any case respond to userspace with the actual length of data
1361 WARN_ON(dump.len != len && dump.len != tmp.len);
1364 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1368 useraddr += offsetof(struct ethtool_dump, data);
1369 if (copy_to_user(useraddr, data, len))
1376 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1379 struct ethtool_ts_info info;
1380 const struct ethtool_ops *ops = dev->ethtool_ops;
1381 struct phy_device *phydev = dev->phydev;
1383 memset(&info, 0, sizeof(info));
1384 info.cmd = ETHTOOL_GET_TS_INFO;
1386 if (phydev && phydev->drv && phydev->drv->ts_info) {
1387 err = phydev->drv->ts_info(phydev, &info);
1388 } else if (ops->get_ts_info) {
1389 err = ops->get_ts_info(dev, &info);
1391 info.so_timestamping =
1392 SOF_TIMESTAMPING_RX_SOFTWARE |
1393 SOF_TIMESTAMPING_SOFTWARE;
1394 info.phc_index = -1;
1400 if (copy_to_user(useraddr, &info, sizeof(info)))
1406 static int ethtool_get_module_info(struct net_device *dev,
1407 void __user *useraddr)
1410 struct ethtool_modinfo modinfo;
1411 const struct ethtool_ops *ops = dev->ethtool_ops;
1413 if (!ops->get_module_info)
1416 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1419 ret = ops->get_module_info(dev, &modinfo);
1423 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1429 static int ethtool_get_module_eeprom(struct net_device *dev,
1430 void __user *useraddr)
1433 struct ethtool_modinfo modinfo;
1434 const struct ethtool_ops *ops = dev->ethtool_ops;
1436 if (!ops->get_module_info || !ops->get_module_eeprom)
1439 ret = ops->get_module_info(dev, &modinfo);
1443 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1444 modinfo.eeprom_len);
1447 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
1449 int dev_ethtool(struct net *net, struct ifreq *ifr)
1451 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1452 void __user *useraddr = ifr->ifr_data;
1455 netdev_features_t old_features;
1457 if (!dev || !netif_device_present(dev))
1460 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1463 /* Allow some commands to be done by anyone */
1466 case ETHTOOL_GDRVINFO:
1467 case ETHTOOL_GMSGLVL:
1469 case ETHTOOL_GCOALESCE:
1470 case ETHTOOL_GRINGPARAM:
1471 case ETHTOOL_GPAUSEPARAM:
1472 case ETHTOOL_GRXCSUM:
1473 case ETHTOOL_GTXCSUM:
1475 case ETHTOOL_GSSET_INFO:
1476 case ETHTOOL_GSTRINGS:
1477 case ETHTOOL_GSTATS:
1479 case ETHTOOL_GPERMADDR:
1483 case ETHTOOL_GFLAGS:
1484 case ETHTOOL_GPFLAGS:
1486 case ETHTOOL_GRXRINGS:
1487 case ETHTOOL_GRXCLSRLCNT:
1488 case ETHTOOL_GRXCLSRULE:
1489 case ETHTOOL_GRXCLSRLALL:
1490 case ETHTOOL_GRXFHINDIR:
1491 case ETHTOOL_GFEATURES:
1492 case ETHTOOL_GCHANNELS:
1493 case ETHTOOL_GET_TS_INFO:
1497 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1501 if (dev->ethtool_ops->begin) {
1502 rc = dev->ethtool_ops->begin(dev);
1506 old_features = dev->features;
1510 rc = ethtool_get_settings(dev, useraddr);
1513 rc = ethtool_set_settings(dev, useraddr);
1515 case ETHTOOL_GDRVINFO:
1516 rc = ethtool_get_drvinfo(dev, useraddr);
1519 rc = ethtool_get_regs(dev, useraddr);
1522 rc = ethtool_get_wol(dev, useraddr);
1525 rc = ethtool_set_wol(dev, useraddr);
1527 case ETHTOOL_GMSGLVL:
1528 rc = ethtool_get_value(dev, useraddr, ethcmd,
1529 dev->ethtool_ops->get_msglevel);
1531 case ETHTOOL_SMSGLVL:
1532 rc = ethtool_set_value_void(dev, useraddr,
1533 dev->ethtool_ops->set_msglevel);
1536 rc = ethtool_get_eee(dev, useraddr);
1539 rc = ethtool_set_eee(dev, useraddr);
1541 case ETHTOOL_NWAY_RST:
1542 rc = ethtool_nway_reset(dev);
1545 rc = ethtool_get_link(dev, useraddr);
1547 case ETHTOOL_GEEPROM:
1548 rc = ethtool_get_eeprom(dev, useraddr);
1550 case ETHTOOL_SEEPROM:
1551 rc = ethtool_set_eeprom(dev, useraddr);
1553 case ETHTOOL_GCOALESCE:
1554 rc = ethtool_get_coalesce(dev, useraddr);
1556 case ETHTOOL_SCOALESCE:
1557 rc = ethtool_set_coalesce(dev, useraddr);
1559 case ETHTOOL_GRINGPARAM:
1560 rc = ethtool_get_ringparam(dev, useraddr);
1562 case ETHTOOL_SRINGPARAM:
1563 rc = ethtool_set_ringparam(dev, useraddr);
1565 case ETHTOOL_GPAUSEPARAM:
1566 rc = ethtool_get_pauseparam(dev, useraddr);
1568 case ETHTOOL_SPAUSEPARAM:
1569 rc = ethtool_set_pauseparam(dev, useraddr);
1572 rc = ethtool_self_test(dev, useraddr);
1574 case ETHTOOL_GSTRINGS:
1575 rc = ethtool_get_strings(dev, useraddr);
1577 case ETHTOOL_PHYS_ID:
1578 rc = ethtool_phys_id(dev, useraddr);
1580 case ETHTOOL_GSTATS:
1581 rc = ethtool_get_stats(dev, useraddr);
1583 case ETHTOOL_GPERMADDR:
1584 rc = ethtool_get_perm_addr(dev, useraddr);
1586 case ETHTOOL_GFLAGS:
1587 rc = ethtool_get_value(dev, useraddr, ethcmd,
1588 __ethtool_get_flags);
1590 case ETHTOOL_SFLAGS:
1591 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1593 case ETHTOOL_GPFLAGS:
1594 rc = ethtool_get_value(dev, useraddr, ethcmd,
1595 dev->ethtool_ops->get_priv_flags);
1597 case ETHTOOL_SPFLAGS:
1598 rc = ethtool_set_value(dev, useraddr,
1599 dev->ethtool_ops->set_priv_flags);
1602 case ETHTOOL_GRXRINGS:
1603 case ETHTOOL_GRXCLSRLCNT:
1604 case ETHTOOL_GRXCLSRULE:
1605 case ETHTOOL_GRXCLSRLALL:
1606 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1609 case ETHTOOL_SRXCLSRLDEL:
1610 case ETHTOOL_SRXCLSRLINS:
1611 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1613 case ETHTOOL_FLASHDEV:
1614 rc = ethtool_flash_device(dev, useraddr);
1617 rc = ethtool_reset(dev, useraddr);
1619 case ETHTOOL_GSSET_INFO:
1620 rc = ethtool_get_sset_info(dev, useraddr);
1622 case ETHTOOL_GRXFHINDIR:
1623 rc = ethtool_get_rxfh_indir(dev, useraddr);
1625 case ETHTOOL_SRXFHINDIR:
1626 rc = ethtool_set_rxfh_indir(dev, useraddr);
1628 case ETHTOOL_GFEATURES:
1629 rc = ethtool_get_features(dev, useraddr);
1631 case ETHTOOL_SFEATURES:
1632 rc = ethtool_set_features(dev, useraddr);
1634 case ETHTOOL_GTXCSUM:
1635 case ETHTOOL_GRXCSUM:
1641 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1643 case ETHTOOL_STXCSUM:
1644 case ETHTOOL_SRXCSUM:
1650 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1652 case ETHTOOL_GCHANNELS:
1653 rc = ethtool_get_channels(dev, useraddr);
1655 case ETHTOOL_SCHANNELS:
1656 rc = ethtool_set_channels(dev, useraddr);
1658 case ETHTOOL_SET_DUMP:
1659 rc = ethtool_set_dump(dev, useraddr);
1661 case ETHTOOL_GET_DUMP_FLAG:
1662 rc = ethtool_get_dump_flag(dev, useraddr);
1664 case ETHTOOL_GET_DUMP_DATA:
1665 rc = ethtool_get_dump_data(dev, useraddr);
1667 case ETHTOOL_GET_TS_INFO:
1668 rc = ethtool_get_ts_info(dev, useraddr);
1670 case ETHTOOL_GMODULEINFO:
1671 rc = ethtool_get_module_info(dev, useraddr);
1673 case ETHTOOL_GMODULEEEPROM:
1674 rc = ethtool_get_module_eeprom(dev, useraddr);
1680 if (dev->ethtool_ops->complete)
1681 dev->ethtool_ops->complete(dev);
1683 if (old_features != dev->features)
1684 netdev_features_change(dev);