Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / net / core / ethtool.c
blob17cb912793fa5ef0d221abda0dfb447b216b4268
1 /*
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;
47 info->phc_index = -1;
48 return 0;
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_SIT_BIT] = "tx-sit-segmentation",
86 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
87 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
89 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
90 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
91 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
92 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
93 [NETIF_F_RXHASH_BIT] = "rx-hashing",
94 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
95 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
96 [NETIF_F_LOOPBACK_BIT] = "loopback",
97 [NETIF_F_RXFCS_BIT] = "rx-fcs",
98 [NETIF_F_RXALL_BIT] = "rx-all",
99 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
100 [NETIF_F_BUSY_POLL_BIT] = "busy-poll",
103 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
105 struct ethtool_gfeatures cmd = {
106 .cmd = ETHTOOL_GFEATURES,
107 .size = ETHTOOL_DEV_FEATURE_WORDS,
109 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
110 u32 __user *sizeaddr;
111 u32 copy_size;
112 int i;
114 /* in case feature bits run out again */
115 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
117 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
118 features[i].available = (u32)(dev->hw_features >> (32 * i));
119 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
120 features[i].active = (u32)(dev->features >> (32 * i));
121 features[i].never_changed =
122 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
125 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
126 if (get_user(copy_size, sizeaddr))
127 return -EFAULT;
129 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
130 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
132 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
133 return -EFAULT;
134 useraddr += sizeof(cmd);
135 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
136 return -EFAULT;
138 return 0;
141 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
143 struct ethtool_sfeatures cmd;
144 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
145 netdev_features_t wanted = 0, valid = 0;
146 int i, ret = 0;
148 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
149 return -EFAULT;
150 useraddr += sizeof(cmd);
152 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
153 return -EINVAL;
155 if (copy_from_user(features, useraddr, sizeof(features)))
156 return -EFAULT;
158 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
159 valid |= (netdev_features_t)features[i].valid << (32 * i);
160 wanted |= (netdev_features_t)features[i].requested << (32 * i);
163 if (valid & ~NETIF_F_ETHTOOL_BITS)
164 return -EINVAL;
166 if (valid & ~dev->hw_features) {
167 valid &= dev->hw_features;
168 ret |= ETHTOOL_F_UNSUPPORTED;
171 dev->wanted_features &= ~valid;
172 dev->wanted_features |= wanted & valid;
173 __netdev_update_features(dev);
175 if ((dev->wanted_features ^ dev->features) & valid)
176 ret |= ETHTOOL_F_WISH;
178 return ret;
181 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
183 const struct ethtool_ops *ops = dev->ethtool_ops;
185 if (sset == ETH_SS_FEATURES)
186 return ARRAY_SIZE(netdev_features_strings);
188 if (ops->get_sset_count && ops->get_strings)
189 return ops->get_sset_count(dev, sset);
190 else
191 return -EOPNOTSUPP;
194 static void __ethtool_get_strings(struct net_device *dev,
195 u32 stringset, u8 *data)
197 const struct ethtool_ops *ops = dev->ethtool_ops;
199 if (stringset == ETH_SS_FEATURES)
200 memcpy(data, netdev_features_strings,
201 sizeof(netdev_features_strings));
202 else
203 /* ops->get_strings is valid because checked earlier */
204 ops->get_strings(dev, stringset, data);
207 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
209 /* feature masks of legacy discrete ethtool ops */
211 switch (eth_cmd) {
212 case ETHTOOL_GTXCSUM:
213 case ETHTOOL_STXCSUM:
214 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
215 case ETHTOOL_GRXCSUM:
216 case ETHTOOL_SRXCSUM:
217 return NETIF_F_RXCSUM;
218 case ETHTOOL_GSG:
219 case ETHTOOL_SSG:
220 return NETIF_F_SG;
221 case ETHTOOL_GTSO:
222 case ETHTOOL_STSO:
223 return NETIF_F_ALL_TSO;
224 case ETHTOOL_GUFO:
225 case ETHTOOL_SUFO:
226 return NETIF_F_UFO;
227 case ETHTOOL_GGSO:
228 case ETHTOOL_SGSO:
229 return NETIF_F_GSO;
230 case ETHTOOL_GGRO:
231 case ETHTOOL_SGRO:
232 return NETIF_F_GRO;
233 default:
234 BUG();
238 static int ethtool_get_one_feature(struct net_device *dev,
239 char __user *useraddr, u32 ethcmd)
241 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
242 struct ethtool_value edata = {
243 .cmd = ethcmd,
244 .data = !!(dev->features & mask),
247 if (copy_to_user(useraddr, &edata, sizeof(edata)))
248 return -EFAULT;
249 return 0;
252 static int ethtool_set_one_feature(struct net_device *dev,
253 void __user *useraddr, u32 ethcmd)
255 struct ethtool_value edata;
256 netdev_features_t mask;
258 if (copy_from_user(&edata, useraddr, sizeof(edata)))
259 return -EFAULT;
261 mask = ethtool_get_feature_mask(ethcmd);
262 mask &= dev->hw_features;
263 if (!mask)
264 return -EOPNOTSUPP;
266 if (edata.data)
267 dev->wanted_features |= mask;
268 else
269 dev->wanted_features &= ~mask;
271 __netdev_update_features(dev);
273 return 0;
276 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
277 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
278 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
279 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
280 NETIF_F_RXHASH)
282 static u32 __ethtool_get_flags(struct net_device *dev)
284 u32 flags = 0;
286 if (dev->features & NETIF_F_LRO)
287 flags |= ETH_FLAG_LRO;
288 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
289 flags |= ETH_FLAG_RXVLAN;
290 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
291 flags |= ETH_FLAG_TXVLAN;
292 if (dev->features & NETIF_F_NTUPLE)
293 flags |= ETH_FLAG_NTUPLE;
294 if (dev->features & NETIF_F_RXHASH)
295 flags |= ETH_FLAG_RXHASH;
297 return flags;
300 static int __ethtool_set_flags(struct net_device *dev, u32 data)
302 netdev_features_t features = 0, changed;
304 if (data & ~ETH_ALL_FLAGS)
305 return -EINVAL;
307 if (data & ETH_FLAG_LRO)
308 features |= NETIF_F_LRO;
309 if (data & ETH_FLAG_RXVLAN)
310 features |= NETIF_F_HW_VLAN_CTAG_RX;
311 if (data & ETH_FLAG_TXVLAN)
312 features |= NETIF_F_HW_VLAN_CTAG_TX;
313 if (data & ETH_FLAG_NTUPLE)
314 features |= NETIF_F_NTUPLE;
315 if (data & ETH_FLAG_RXHASH)
316 features |= NETIF_F_RXHASH;
318 /* allow changing only bits set in hw_features */
319 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
320 if (changed & ~dev->hw_features)
321 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
323 dev->wanted_features =
324 (dev->wanted_features & ~changed) | (features & changed);
326 __netdev_update_features(dev);
328 return 0;
331 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
333 ASSERT_RTNL();
335 if (!dev->ethtool_ops->get_settings)
336 return -EOPNOTSUPP;
338 memset(cmd, 0, sizeof(struct ethtool_cmd));
339 cmd->cmd = ETHTOOL_GSET;
340 return dev->ethtool_ops->get_settings(dev, cmd);
342 EXPORT_SYMBOL(__ethtool_get_settings);
344 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
346 int err;
347 struct ethtool_cmd cmd;
349 err = __ethtool_get_settings(dev, &cmd);
350 if (err < 0)
351 return err;
353 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
354 return -EFAULT;
355 return 0;
358 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
360 struct ethtool_cmd cmd;
362 if (!dev->ethtool_ops->set_settings)
363 return -EOPNOTSUPP;
365 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
366 return -EFAULT;
368 return dev->ethtool_ops->set_settings(dev, &cmd);
371 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
372 void __user *useraddr)
374 struct ethtool_drvinfo info;
375 const struct ethtool_ops *ops = dev->ethtool_ops;
377 memset(&info, 0, sizeof(info));
378 info.cmd = ETHTOOL_GDRVINFO;
379 if (ops->get_drvinfo) {
380 ops->get_drvinfo(dev, &info);
381 } else if (dev->dev.parent && dev->dev.parent->driver) {
382 strlcpy(info.bus_info, dev_name(dev->dev.parent),
383 sizeof(info.bus_info));
384 strlcpy(info.driver, dev->dev.parent->driver->name,
385 sizeof(info.driver));
386 } else {
387 return -EOPNOTSUPP;
391 * this method of obtaining string set info is deprecated;
392 * Use ETHTOOL_GSSET_INFO instead.
394 if (ops->get_sset_count) {
395 int rc;
397 rc = ops->get_sset_count(dev, ETH_SS_TEST);
398 if (rc >= 0)
399 info.testinfo_len = rc;
400 rc = ops->get_sset_count(dev, ETH_SS_STATS);
401 if (rc >= 0)
402 info.n_stats = rc;
403 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
404 if (rc >= 0)
405 info.n_priv_flags = rc;
407 if (ops->get_regs_len)
408 info.regdump_len = ops->get_regs_len(dev);
409 if (ops->get_eeprom_len)
410 info.eedump_len = ops->get_eeprom_len(dev);
412 if (copy_to_user(useraddr, &info, sizeof(info)))
413 return -EFAULT;
414 return 0;
417 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
418 void __user *useraddr)
420 struct ethtool_sset_info info;
421 u64 sset_mask;
422 int i, idx = 0, n_bits = 0, ret, rc;
423 u32 *info_buf = NULL;
425 if (copy_from_user(&info, useraddr, sizeof(info)))
426 return -EFAULT;
428 /* store copy of mask, because we zero struct later on */
429 sset_mask = info.sset_mask;
430 if (!sset_mask)
431 return 0;
433 /* calculate size of return buffer */
434 n_bits = hweight64(sset_mask);
436 memset(&info, 0, sizeof(info));
437 info.cmd = ETHTOOL_GSSET_INFO;
439 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
440 if (!info_buf)
441 return -ENOMEM;
444 * fill return buffer based on input bitmask and successful
445 * get_sset_count return
447 for (i = 0; i < 64; i++) {
448 if (!(sset_mask & (1ULL << i)))
449 continue;
451 rc = __ethtool_get_sset_count(dev, i);
452 if (rc >= 0) {
453 info.sset_mask |= (1ULL << i);
454 info_buf[idx++] = rc;
458 ret = -EFAULT;
459 if (copy_to_user(useraddr, &info, sizeof(info)))
460 goto out;
462 useraddr += offsetof(struct ethtool_sset_info, data);
463 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
464 goto out;
466 ret = 0;
468 out:
469 kfree(info_buf);
470 return ret;
473 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
474 u32 cmd, void __user *useraddr)
476 struct ethtool_rxnfc info;
477 size_t info_size = sizeof(info);
478 int rc;
480 if (!dev->ethtool_ops->set_rxnfc)
481 return -EOPNOTSUPP;
483 /* struct ethtool_rxnfc was originally defined for
484 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
485 * members. User-space might still be using that
486 * definition. */
487 if (cmd == ETHTOOL_SRXFH)
488 info_size = (offsetof(struct ethtool_rxnfc, data) +
489 sizeof(info.data));
491 if (copy_from_user(&info, useraddr, info_size))
492 return -EFAULT;
494 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
495 if (rc)
496 return rc;
498 if (cmd == ETHTOOL_SRXCLSRLINS &&
499 copy_to_user(useraddr, &info, info_size))
500 return -EFAULT;
502 return 0;
505 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
506 u32 cmd, void __user *useraddr)
508 struct ethtool_rxnfc info;
509 size_t info_size = sizeof(info);
510 const struct ethtool_ops *ops = dev->ethtool_ops;
511 int ret;
512 void *rule_buf = NULL;
514 if (!ops->get_rxnfc)
515 return -EOPNOTSUPP;
517 /* struct ethtool_rxnfc was originally defined for
518 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
519 * members. User-space might still be using that
520 * definition. */
521 if (cmd == ETHTOOL_GRXFH)
522 info_size = (offsetof(struct ethtool_rxnfc, data) +
523 sizeof(info.data));
525 if (copy_from_user(&info, useraddr, info_size))
526 return -EFAULT;
528 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
529 if (info.rule_cnt > 0) {
530 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
531 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
532 GFP_USER);
533 if (!rule_buf)
534 return -ENOMEM;
538 ret = ops->get_rxnfc(dev, &info, rule_buf);
539 if (ret < 0)
540 goto err_out;
542 ret = -EFAULT;
543 if (copy_to_user(useraddr, &info, info_size))
544 goto err_out;
546 if (rule_buf) {
547 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
548 if (copy_to_user(useraddr, rule_buf,
549 info.rule_cnt * sizeof(u32)))
550 goto err_out;
552 ret = 0;
554 err_out:
555 kfree(rule_buf);
557 return ret;
560 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
561 struct ethtool_rxnfc *rx_rings,
562 u32 size)
564 int i;
566 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
567 return -EFAULT;
569 /* Validate ring indices */
570 for (i = 0; i < size; i++)
571 if (indir[i] >= rx_rings->data)
572 return -EINVAL;
574 return 0;
577 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
578 void __user *useraddr)
580 u32 user_size, dev_size;
581 u32 *indir;
582 int ret;
584 if (!dev->ethtool_ops->get_rxfh_indir_size ||
585 !dev->ethtool_ops->get_rxfh)
586 return -EOPNOTSUPP;
587 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
588 if (dev_size == 0)
589 return -EOPNOTSUPP;
591 if (copy_from_user(&user_size,
592 useraddr + offsetof(struct ethtool_rxfh_indir, size),
593 sizeof(user_size)))
594 return -EFAULT;
596 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
597 &dev_size, sizeof(dev_size)))
598 return -EFAULT;
600 /* If the user buffer size is 0, this is just a query for the
601 * device table size. Otherwise, if it's smaller than the
602 * device table size it's an error.
604 if (user_size < dev_size)
605 return user_size == 0 ? 0 : -EINVAL;
607 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
608 if (!indir)
609 return -ENOMEM;
611 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL);
612 if (ret)
613 goto out;
615 if (copy_to_user(useraddr +
616 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
617 indir, dev_size * sizeof(indir[0])))
618 ret = -EFAULT;
620 out:
621 kfree(indir);
622 return ret;
625 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
626 void __user *useraddr)
628 struct ethtool_rxnfc rx_rings;
629 u32 user_size, dev_size, i;
630 u32 *indir;
631 const struct ethtool_ops *ops = dev->ethtool_ops;
632 int ret;
633 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
635 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
636 !ops->get_rxnfc)
637 return -EOPNOTSUPP;
639 dev_size = ops->get_rxfh_indir_size(dev);
640 if (dev_size == 0)
641 return -EOPNOTSUPP;
643 if (copy_from_user(&user_size,
644 useraddr + offsetof(struct ethtool_rxfh_indir, size),
645 sizeof(user_size)))
646 return -EFAULT;
648 if (user_size != 0 && user_size != dev_size)
649 return -EINVAL;
651 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
652 if (!indir)
653 return -ENOMEM;
655 rx_rings.cmd = ETHTOOL_GRXRINGS;
656 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
657 if (ret)
658 goto out;
660 if (user_size == 0) {
661 for (i = 0; i < dev_size; i++)
662 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
663 } else {
664 ret = ethtool_copy_validate_indir(indir,
665 useraddr + ringidx_offset,
666 &rx_rings,
667 dev_size);
668 if (ret)
669 goto out;
672 ret = ops->set_rxfh(dev, indir, NULL);
674 out:
675 kfree(indir);
676 return ret;
679 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
680 void __user *useraddr)
682 int ret;
683 const struct ethtool_ops *ops = dev->ethtool_ops;
684 u32 user_indir_size, user_key_size;
685 u32 dev_indir_size = 0, dev_key_size = 0;
686 struct ethtool_rxfh rxfh;
687 u32 total_size;
688 u32 indir_bytes;
689 u32 *indir = NULL;
690 u8 *hkey = NULL;
691 u8 *rss_config;
693 if (!(dev->ethtool_ops->get_rxfh_indir_size ||
694 dev->ethtool_ops->get_rxfh_key_size) ||
695 !dev->ethtool_ops->get_rxfh)
696 return -EOPNOTSUPP;
698 if (ops->get_rxfh_indir_size)
699 dev_indir_size = ops->get_rxfh_indir_size(dev);
700 if (ops->get_rxfh_key_size)
701 dev_key_size = ops->get_rxfh_key_size(dev);
703 if ((dev_key_size + dev_indir_size) == 0)
704 return -EOPNOTSUPP;
706 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
707 return -EFAULT;
708 user_indir_size = rxfh.indir_size;
709 user_key_size = rxfh.key_size;
711 /* Check that reserved fields are 0 for now */
712 if (rxfh.rss_context || rxfh.rsvd[0] || rxfh.rsvd[1])
713 return -EINVAL;
715 rxfh.indir_size = dev_indir_size;
716 rxfh.key_size = dev_key_size;
717 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
718 return -EFAULT;
720 /* If the user buffer size is 0, this is just a query for the
721 * device table size and key size. Otherwise, if the User size is
722 * not equal to device table size or key size it's an error.
724 if (!user_indir_size && !user_key_size)
725 return 0;
727 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
728 (user_key_size && (user_key_size != dev_key_size)))
729 return -EINVAL;
731 indir_bytes = user_indir_size * sizeof(indir[0]);
732 total_size = indir_bytes + user_key_size;
733 rss_config = kzalloc(total_size, GFP_USER);
734 if (!rss_config)
735 return -ENOMEM;
737 if (user_indir_size)
738 indir = (u32 *)rss_config;
740 if (user_key_size)
741 hkey = rss_config + indir_bytes;
743 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey);
744 if (!ret) {
745 if (copy_to_user(useraddr +
746 offsetof(struct ethtool_rxfh, rss_config[0]),
747 rss_config, total_size))
748 ret = -EFAULT;
751 kfree(rss_config);
753 return ret;
756 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
757 void __user *useraddr)
759 int ret;
760 const struct ethtool_ops *ops = dev->ethtool_ops;
761 struct ethtool_rxnfc rx_rings;
762 struct ethtool_rxfh rxfh;
763 u32 dev_indir_size = 0, dev_key_size = 0, i;
764 u32 *indir = NULL, indir_bytes = 0;
765 u8 *hkey = NULL;
766 u8 *rss_config;
767 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
769 if (!(ops->get_rxfh_indir_size || ops->get_rxfh_key_size) ||
770 !ops->get_rxnfc || !ops->set_rxfh)
771 return -EOPNOTSUPP;
773 if (ops->get_rxfh_indir_size)
774 dev_indir_size = ops->get_rxfh_indir_size(dev);
775 if (ops->get_rxfh_key_size)
776 dev_key_size = dev->ethtool_ops->get_rxfh_key_size(dev);
777 if ((dev_key_size + dev_indir_size) == 0)
778 return -EOPNOTSUPP;
780 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
781 return -EFAULT;
783 /* Check that reserved fields are 0 for now */
784 if (rxfh.rss_context || rxfh.rsvd[0] || rxfh.rsvd[1])
785 return -EINVAL;
787 /* If either indir or hash key is valid, proceed further.
788 * It is not valid to request that both be unchanged.
790 if ((rxfh.indir_size &&
791 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
792 rxfh.indir_size != dev_indir_size) ||
793 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
794 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
795 rxfh.key_size == 0))
796 return -EINVAL;
798 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
799 indir_bytes = dev_indir_size * sizeof(indir[0]);
801 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
802 if (!rss_config)
803 return -ENOMEM;
805 rx_rings.cmd = ETHTOOL_GRXRINGS;
806 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
807 if (ret)
808 goto out;
810 /* rxfh.indir_size == 0 means reset the indir table to default.
811 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
813 if (rxfh.indir_size &&
814 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
815 indir = (u32 *)rss_config;
816 ret = ethtool_copy_validate_indir(indir,
817 useraddr + rss_cfg_offset,
818 &rx_rings,
819 rxfh.indir_size);
820 if (ret)
821 goto out;
822 } else if (rxfh.indir_size == 0) {
823 indir = (u32 *)rss_config;
824 for (i = 0; i < dev_indir_size; i++)
825 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
828 if (rxfh.key_size) {
829 hkey = rss_config + indir_bytes;
830 if (copy_from_user(hkey,
831 useraddr + rss_cfg_offset + indir_bytes,
832 rxfh.key_size)) {
833 ret = -EFAULT;
834 goto out;
838 ret = ops->set_rxfh(dev, indir, hkey);
840 out:
841 kfree(rss_config);
842 return ret;
845 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
847 struct ethtool_regs regs;
848 const struct ethtool_ops *ops = dev->ethtool_ops;
849 void *regbuf;
850 int reglen, ret;
852 if (!ops->get_regs || !ops->get_regs_len)
853 return -EOPNOTSUPP;
855 if (copy_from_user(&regs, useraddr, sizeof(regs)))
856 return -EFAULT;
858 reglen = ops->get_regs_len(dev);
859 if (regs.len > reglen)
860 regs.len = reglen;
862 regbuf = vzalloc(reglen);
863 if (reglen && !regbuf)
864 return -ENOMEM;
866 ops->get_regs(dev, &regs, regbuf);
868 ret = -EFAULT;
869 if (copy_to_user(useraddr, &regs, sizeof(regs)))
870 goto out;
871 useraddr += offsetof(struct ethtool_regs, data);
872 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
873 goto out;
874 ret = 0;
876 out:
877 vfree(regbuf);
878 return ret;
881 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
883 struct ethtool_value reset;
884 int ret;
886 if (!dev->ethtool_ops->reset)
887 return -EOPNOTSUPP;
889 if (copy_from_user(&reset, useraddr, sizeof(reset)))
890 return -EFAULT;
892 ret = dev->ethtool_ops->reset(dev, &reset.data);
893 if (ret)
894 return ret;
896 if (copy_to_user(useraddr, &reset, sizeof(reset)))
897 return -EFAULT;
898 return 0;
901 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
903 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
905 if (!dev->ethtool_ops->get_wol)
906 return -EOPNOTSUPP;
908 dev->ethtool_ops->get_wol(dev, &wol);
910 if (copy_to_user(useraddr, &wol, sizeof(wol)))
911 return -EFAULT;
912 return 0;
915 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
917 struct ethtool_wolinfo wol;
919 if (!dev->ethtool_ops->set_wol)
920 return -EOPNOTSUPP;
922 if (copy_from_user(&wol, useraddr, sizeof(wol)))
923 return -EFAULT;
925 return dev->ethtool_ops->set_wol(dev, &wol);
928 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
930 struct ethtool_eee edata;
931 int rc;
933 if (!dev->ethtool_ops->get_eee)
934 return -EOPNOTSUPP;
936 memset(&edata, 0, sizeof(struct ethtool_eee));
937 edata.cmd = ETHTOOL_GEEE;
938 rc = dev->ethtool_ops->get_eee(dev, &edata);
940 if (rc)
941 return rc;
943 if (copy_to_user(useraddr, &edata, sizeof(edata)))
944 return -EFAULT;
946 return 0;
949 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
951 struct ethtool_eee edata;
953 if (!dev->ethtool_ops->set_eee)
954 return -EOPNOTSUPP;
956 if (copy_from_user(&edata, useraddr, sizeof(edata)))
957 return -EFAULT;
959 return dev->ethtool_ops->set_eee(dev, &edata);
962 static int ethtool_nway_reset(struct net_device *dev)
964 if (!dev->ethtool_ops->nway_reset)
965 return -EOPNOTSUPP;
967 return dev->ethtool_ops->nway_reset(dev);
970 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
972 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
974 if (!dev->ethtool_ops->get_link)
975 return -EOPNOTSUPP;
977 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
979 if (copy_to_user(useraddr, &edata, sizeof(edata)))
980 return -EFAULT;
981 return 0;
984 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
985 int (*getter)(struct net_device *,
986 struct ethtool_eeprom *, u8 *),
987 u32 total_len)
989 struct ethtool_eeprom eeprom;
990 void __user *userbuf = useraddr + sizeof(eeprom);
991 u32 bytes_remaining;
992 u8 *data;
993 int ret = 0;
995 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
996 return -EFAULT;
998 /* Check for wrap and zero */
999 if (eeprom.offset + eeprom.len <= eeprom.offset)
1000 return -EINVAL;
1002 /* Check for exceeding total eeprom len */
1003 if (eeprom.offset + eeprom.len > total_len)
1004 return -EINVAL;
1006 data = kmalloc(PAGE_SIZE, GFP_USER);
1007 if (!data)
1008 return -ENOMEM;
1010 bytes_remaining = eeprom.len;
1011 while (bytes_remaining > 0) {
1012 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1014 ret = getter(dev, &eeprom, data);
1015 if (ret)
1016 break;
1017 if (copy_to_user(userbuf, data, eeprom.len)) {
1018 ret = -EFAULT;
1019 break;
1021 userbuf += eeprom.len;
1022 eeprom.offset += eeprom.len;
1023 bytes_remaining -= eeprom.len;
1026 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1027 eeprom.offset -= eeprom.len;
1028 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1029 ret = -EFAULT;
1031 kfree(data);
1032 return ret;
1035 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1037 const struct ethtool_ops *ops = dev->ethtool_ops;
1039 if (!ops->get_eeprom || !ops->get_eeprom_len)
1040 return -EOPNOTSUPP;
1042 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1043 ops->get_eeprom_len(dev));
1046 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1048 struct ethtool_eeprom eeprom;
1049 const struct ethtool_ops *ops = dev->ethtool_ops;
1050 void __user *userbuf = useraddr + sizeof(eeprom);
1051 u32 bytes_remaining;
1052 u8 *data;
1053 int ret = 0;
1055 if (!ops->set_eeprom || !ops->get_eeprom_len)
1056 return -EOPNOTSUPP;
1058 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1059 return -EFAULT;
1061 /* Check for wrap and zero */
1062 if (eeprom.offset + eeprom.len <= eeprom.offset)
1063 return -EINVAL;
1065 /* Check for exceeding total eeprom len */
1066 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1067 return -EINVAL;
1069 data = kmalloc(PAGE_SIZE, GFP_USER);
1070 if (!data)
1071 return -ENOMEM;
1073 bytes_remaining = eeprom.len;
1074 while (bytes_remaining > 0) {
1075 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1077 if (copy_from_user(data, userbuf, eeprom.len)) {
1078 ret = -EFAULT;
1079 break;
1081 ret = ops->set_eeprom(dev, &eeprom, data);
1082 if (ret)
1083 break;
1084 userbuf += eeprom.len;
1085 eeprom.offset += eeprom.len;
1086 bytes_remaining -= eeprom.len;
1089 kfree(data);
1090 return ret;
1093 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1094 void __user *useraddr)
1096 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1098 if (!dev->ethtool_ops->get_coalesce)
1099 return -EOPNOTSUPP;
1101 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1103 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1104 return -EFAULT;
1105 return 0;
1108 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1109 void __user *useraddr)
1111 struct ethtool_coalesce coalesce;
1113 if (!dev->ethtool_ops->set_coalesce)
1114 return -EOPNOTSUPP;
1116 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1117 return -EFAULT;
1119 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1122 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1124 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1126 if (!dev->ethtool_ops->get_ringparam)
1127 return -EOPNOTSUPP;
1129 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1131 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1132 return -EFAULT;
1133 return 0;
1136 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1138 struct ethtool_ringparam ringparam;
1140 if (!dev->ethtool_ops->set_ringparam)
1141 return -EOPNOTSUPP;
1143 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1144 return -EFAULT;
1146 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1149 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1150 void __user *useraddr)
1152 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1154 if (!dev->ethtool_ops->get_channels)
1155 return -EOPNOTSUPP;
1157 dev->ethtool_ops->get_channels(dev, &channels);
1159 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1160 return -EFAULT;
1161 return 0;
1164 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1165 void __user *useraddr)
1167 struct ethtool_channels channels;
1169 if (!dev->ethtool_ops->set_channels)
1170 return -EOPNOTSUPP;
1172 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1173 return -EFAULT;
1175 return dev->ethtool_ops->set_channels(dev, &channels);
1178 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1180 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1182 if (!dev->ethtool_ops->get_pauseparam)
1183 return -EOPNOTSUPP;
1185 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1187 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1188 return -EFAULT;
1189 return 0;
1192 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1194 struct ethtool_pauseparam pauseparam;
1196 if (!dev->ethtool_ops->set_pauseparam)
1197 return -EOPNOTSUPP;
1199 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1200 return -EFAULT;
1202 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1205 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1207 struct ethtool_test test;
1208 const struct ethtool_ops *ops = dev->ethtool_ops;
1209 u64 *data;
1210 int ret, test_len;
1212 if (!ops->self_test || !ops->get_sset_count)
1213 return -EOPNOTSUPP;
1215 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1216 if (test_len < 0)
1217 return test_len;
1218 WARN_ON(test_len == 0);
1220 if (copy_from_user(&test, useraddr, sizeof(test)))
1221 return -EFAULT;
1223 test.len = test_len;
1224 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1225 if (!data)
1226 return -ENOMEM;
1228 ops->self_test(dev, &test, data);
1230 ret = -EFAULT;
1231 if (copy_to_user(useraddr, &test, sizeof(test)))
1232 goto out;
1233 useraddr += sizeof(test);
1234 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1235 goto out;
1236 ret = 0;
1238 out:
1239 kfree(data);
1240 return ret;
1243 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1245 struct ethtool_gstrings gstrings;
1246 u8 *data;
1247 int ret;
1249 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1250 return -EFAULT;
1252 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1253 if (ret < 0)
1254 return ret;
1256 gstrings.len = ret;
1258 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1259 if (!data)
1260 return -ENOMEM;
1262 __ethtool_get_strings(dev, gstrings.string_set, data);
1264 ret = -EFAULT;
1265 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1266 goto out;
1267 useraddr += sizeof(gstrings);
1268 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1269 goto out;
1270 ret = 0;
1272 out:
1273 kfree(data);
1274 return ret;
1277 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1279 struct ethtool_value id;
1280 static bool busy;
1281 const struct ethtool_ops *ops = dev->ethtool_ops;
1282 int rc;
1284 if (!ops->set_phys_id)
1285 return -EOPNOTSUPP;
1287 if (busy)
1288 return -EBUSY;
1290 if (copy_from_user(&id, useraddr, sizeof(id)))
1291 return -EFAULT;
1293 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1294 if (rc < 0)
1295 return rc;
1297 /* Drop the RTNL lock while waiting, but prevent reentry or
1298 * removal of the device.
1300 busy = true;
1301 dev_hold(dev);
1302 rtnl_unlock();
1304 if (rc == 0) {
1305 /* Driver will handle this itself */
1306 schedule_timeout_interruptible(
1307 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1308 } else {
1309 /* Driver expects to be called at twice the frequency in rc */
1310 int n = rc * 2, i, interval = HZ / n;
1312 /* Count down seconds */
1313 do {
1314 /* Count down iterations per second */
1315 i = n;
1316 do {
1317 rtnl_lock();
1318 rc = ops->set_phys_id(dev,
1319 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1320 rtnl_unlock();
1321 if (rc)
1322 break;
1323 schedule_timeout_interruptible(interval);
1324 } while (!signal_pending(current) && --i != 0);
1325 } while (!signal_pending(current) &&
1326 (id.data == 0 || --id.data != 0));
1329 rtnl_lock();
1330 dev_put(dev);
1331 busy = false;
1333 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1334 return rc;
1337 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1339 struct ethtool_stats stats;
1340 const struct ethtool_ops *ops = dev->ethtool_ops;
1341 u64 *data;
1342 int ret, n_stats;
1344 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1345 return -EOPNOTSUPP;
1347 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1348 if (n_stats < 0)
1349 return n_stats;
1350 WARN_ON(n_stats == 0);
1352 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1353 return -EFAULT;
1355 stats.n_stats = n_stats;
1356 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1357 if (!data)
1358 return -ENOMEM;
1360 ops->get_ethtool_stats(dev, &stats, data);
1362 ret = -EFAULT;
1363 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1364 goto out;
1365 useraddr += sizeof(stats);
1366 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1367 goto out;
1368 ret = 0;
1370 out:
1371 kfree(data);
1372 return ret;
1375 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1377 struct ethtool_perm_addr epaddr;
1379 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1380 return -EFAULT;
1382 if (epaddr.size < dev->addr_len)
1383 return -ETOOSMALL;
1384 epaddr.size = dev->addr_len;
1386 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1387 return -EFAULT;
1388 useraddr += sizeof(epaddr);
1389 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1390 return -EFAULT;
1391 return 0;
1394 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1395 u32 cmd, u32 (*actor)(struct net_device *))
1397 struct ethtool_value edata = { .cmd = cmd };
1399 if (!actor)
1400 return -EOPNOTSUPP;
1402 edata.data = actor(dev);
1404 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1405 return -EFAULT;
1406 return 0;
1409 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1410 void (*actor)(struct net_device *, u32))
1412 struct ethtool_value edata;
1414 if (!actor)
1415 return -EOPNOTSUPP;
1417 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1418 return -EFAULT;
1420 actor(dev, edata.data);
1421 return 0;
1424 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1425 int (*actor)(struct net_device *, u32))
1427 struct ethtool_value edata;
1429 if (!actor)
1430 return -EOPNOTSUPP;
1432 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1433 return -EFAULT;
1435 return actor(dev, edata.data);
1438 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1439 char __user *useraddr)
1441 struct ethtool_flash efl;
1443 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1444 return -EFAULT;
1446 if (!dev->ethtool_ops->flash_device)
1447 return -EOPNOTSUPP;
1449 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1451 return dev->ethtool_ops->flash_device(dev, &efl);
1454 static int ethtool_set_dump(struct net_device *dev,
1455 void __user *useraddr)
1457 struct ethtool_dump dump;
1459 if (!dev->ethtool_ops->set_dump)
1460 return -EOPNOTSUPP;
1462 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1463 return -EFAULT;
1465 return dev->ethtool_ops->set_dump(dev, &dump);
1468 static int ethtool_get_dump_flag(struct net_device *dev,
1469 void __user *useraddr)
1471 int ret;
1472 struct ethtool_dump dump;
1473 const struct ethtool_ops *ops = dev->ethtool_ops;
1475 if (!ops->get_dump_flag)
1476 return -EOPNOTSUPP;
1478 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1479 return -EFAULT;
1481 ret = ops->get_dump_flag(dev, &dump);
1482 if (ret)
1483 return ret;
1485 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1486 return -EFAULT;
1487 return 0;
1490 static int ethtool_get_dump_data(struct net_device *dev,
1491 void __user *useraddr)
1493 int ret;
1494 __u32 len;
1495 struct ethtool_dump dump, tmp;
1496 const struct ethtool_ops *ops = dev->ethtool_ops;
1497 void *data = NULL;
1499 if (!ops->get_dump_data || !ops->get_dump_flag)
1500 return -EOPNOTSUPP;
1502 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1503 return -EFAULT;
1505 memset(&tmp, 0, sizeof(tmp));
1506 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1507 ret = ops->get_dump_flag(dev, &tmp);
1508 if (ret)
1509 return ret;
1511 len = min(tmp.len, dump.len);
1512 if (!len)
1513 return -EFAULT;
1515 /* Don't ever let the driver think there's more space available
1516 * than it requested with .get_dump_flag().
1518 dump.len = len;
1520 /* Always allocate enough space to hold the whole thing so that the
1521 * driver does not need to check the length and bother with partial
1522 * dumping.
1524 data = vzalloc(tmp.len);
1525 if (!data)
1526 return -ENOMEM;
1527 ret = ops->get_dump_data(dev, &dump, data);
1528 if (ret)
1529 goto out;
1531 /* There are two sane possibilities:
1532 * 1. The driver's .get_dump_data() does not touch dump.len.
1533 * 2. Or it may set dump.len to how much it really writes, which
1534 * should be tmp.len (or len if it can do a partial dump).
1535 * In any case respond to userspace with the actual length of data
1536 * it's receiving.
1538 WARN_ON(dump.len != len && dump.len != tmp.len);
1539 dump.len = len;
1541 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1542 ret = -EFAULT;
1543 goto out;
1545 useraddr += offsetof(struct ethtool_dump, data);
1546 if (copy_to_user(useraddr, data, len))
1547 ret = -EFAULT;
1548 out:
1549 vfree(data);
1550 return ret;
1553 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1555 int err = 0;
1556 struct ethtool_ts_info info;
1557 const struct ethtool_ops *ops = dev->ethtool_ops;
1558 struct phy_device *phydev = dev->phydev;
1560 memset(&info, 0, sizeof(info));
1561 info.cmd = ETHTOOL_GET_TS_INFO;
1563 if (phydev && phydev->drv && phydev->drv->ts_info) {
1564 err = phydev->drv->ts_info(phydev, &info);
1565 } else if (ops->get_ts_info) {
1566 err = ops->get_ts_info(dev, &info);
1567 } else {
1568 info.so_timestamping =
1569 SOF_TIMESTAMPING_RX_SOFTWARE |
1570 SOF_TIMESTAMPING_SOFTWARE;
1571 info.phc_index = -1;
1574 if (err)
1575 return err;
1577 if (copy_to_user(useraddr, &info, sizeof(info)))
1578 err = -EFAULT;
1580 return err;
1583 static int ethtool_get_module_info(struct net_device *dev,
1584 void __user *useraddr)
1586 int ret;
1587 struct ethtool_modinfo modinfo;
1588 const struct ethtool_ops *ops = dev->ethtool_ops;
1590 if (!ops->get_module_info)
1591 return -EOPNOTSUPP;
1593 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1594 return -EFAULT;
1596 ret = ops->get_module_info(dev, &modinfo);
1597 if (ret)
1598 return ret;
1600 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1601 return -EFAULT;
1603 return 0;
1606 static int ethtool_get_module_eeprom(struct net_device *dev,
1607 void __user *useraddr)
1609 int ret;
1610 struct ethtool_modinfo modinfo;
1611 const struct ethtool_ops *ops = dev->ethtool_ops;
1613 if (!ops->get_module_info || !ops->get_module_eeprom)
1614 return -EOPNOTSUPP;
1616 ret = ops->get_module_info(dev, &modinfo);
1617 if (ret)
1618 return ret;
1620 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1621 modinfo.eeprom_len);
1624 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
1626 int dev_ethtool(struct net *net, struct ifreq *ifr)
1628 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1629 void __user *useraddr = ifr->ifr_data;
1630 u32 ethcmd;
1631 int rc;
1632 netdev_features_t old_features;
1634 if (!dev || !netif_device_present(dev))
1635 return -ENODEV;
1637 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1638 return -EFAULT;
1640 /* Allow some commands to be done by anyone */
1641 switch (ethcmd) {
1642 case ETHTOOL_GSET:
1643 case ETHTOOL_GDRVINFO:
1644 case ETHTOOL_GMSGLVL:
1645 case ETHTOOL_GLINK:
1646 case ETHTOOL_GCOALESCE:
1647 case ETHTOOL_GRINGPARAM:
1648 case ETHTOOL_GPAUSEPARAM:
1649 case ETHTOOL_GRXCSUM:
1650 case ETHTOOL_GTXCSUM:
1651 case ETHTOOL_GSG:
1652 case ETHTOOL_GSSET_INFO:
1653 case ETHTOOL_GSTRINGS:
1654 case ETHTOOL_GSTATS:
1655 case ETHTOOL_GTSO:
1656 case ETHTOOL_GPERMADDR:
1657 case ETHTOOL_GUFO:
1658 case ETHTOOL_GGSO:
1659 case ETHTOOL_GGRO:
1660 case ETHTOOL_GFLAGS:
1661 case ETHTOOL_GPFLAGS:
1662 case ETHTOOL_GRXFH:
1663 case ETHTOOL_GRXRINGS:
1664 case ETHTOOL_GRXCLSRLCNT:
1665 case ETHTOOL_GRXCLSRULE:
1666 case ETHTOOL_GRXCLSRLALL:
1667 case ETHTOOL_GRXFHINDIR:
1668 case ETHTOOL_GRSSH:
1669 case ETHTOOL_GFEATURES:
1670 case ETHTOOL_GCHANNELS:
1671 case ETHTOOL_GET_TS_INFO:
1672 case ETHTOOL_GEEE:
1673 break;
1674 default:
1675 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1676 return -EPERM;
1679 if (dev->ethtool_ops->begin) {
1680 rc = dev->ethtool_ops->begin(dev);
1681 if (rc < 0)
1682 return rc;
1684 old_features = dev->features;
1686 switch (ethcmd) {
1687 case ETHTOOL_GSET:
1688 rc = ethtool_get_settings(dev, useraddr);
1689 break;
1690 case ETHTOOL_SSET:
1691 rc = ethtool_set_settings(dev, useraddr);
1692 break;
1693 case ETHTOOL_GDRVINFO:
1694 rc = ethtool_get_drvinfo(dev, useraddr);
1695 break;
1696 case ETHTOOL_GREGS:
1697 rc = ethtool_get_regs(dev, useraddr);
1698 break;
1699 case ETHTOOL_GWOL:
1700 rc = ethtool_get_wol(dev, useraddr);
1701 break;
1702 case ETHTOOL_SWOL:
1703 rc = ethtool_set_wol(dev, useraddr);
1704 break;
1705 case ETHTOOL_GMSGLVL:
1706 rc = ethtool_get_value(dev, useraddr, ethcmd,
1707 dev->ethtool_ops->get_msglevel);
1708 break;
1709 case ETHTOOL_SMSGLVL:
1710 rc = ethtool_set_value_void(dev, useraddr,
1711 dev->ethtool_ops->set_msglevel);
1712 break;
1713 case ETHTOOL_GEEE:
1714 rc = ethtool_get_eee(dev, useraddr);
1715 break;
1716 case ETHTOOL_SEEE:
1717 rc = ethtool_set_eee(dev, useraddr);
1718 break;
1719 case ETHTOOL_NWAY_RST:
1720 rc = ethtool_nway_reset(dev);
1721 break;
1722 case ETHTOOL_GLINK:
1723 rc = ethtool_get_link(dev, useraddr);
1724 break;
1725 case ETHTOOL_GEEPROM:
1726 rc = ethtool_get_eeprom(dev, useraddr);
1727 break;
1728 case ETHTOOL_SEEPROM:
1729 rc = ethtool_set_eeprom(dev, useraddr);
1730 break;
1731 case ETHTOOL_GCOALESCE:
1732 rc = ethtool_get_coalesce(dev, useraddr);
1733 break;
1734 case ETHTOOL_SCOALESCE:
1735 rc = ethtool_set_coalesce(dev, useraddr);
1736 break;
1737 case ETHTOOL_GRINGPARAM:
1738 rc = ethtool_get_ringparam(dev, useraddr);
1739 break;
1740 case ETHTOOL_SRINGPARAM:
1741 rc = ethtool_set_ringparam(dev, useraddr);
1742 break;
1743 case ETHTOOL_GPAUSEPARAM:
1744 rc = ethtool_get_pauseparam(dev, useraddr);
1745 break;
1746 case ETHTOOL_SPAUSEPARAM:
1747 rc = ethtool_set_pauseparam(dev, useraddr);
1748 break;
1749 case ETHTOOL_TEST:
1750 rc = ethtool_self_test(dev, useraddr);
1751 break;
1752 case ETHTOOL_GSTRINGS:
1753 rc = ethtool_get_strings(dev, useraddr);
1754 break;
1755 case ETHTOOL_PHYS_ID:
1756 rc = ethtool_phys_id(dev, useraddr);
1757 break;
1758 case ETHTOOL_GSTATS:
1759 rc = ethtool_get_stats(dev, useraddr);
1760 break;
1761 case ETHTOOL_GPERMADDR:
1762 rc = ethtool_get_perm_addr(dev, useraddr);
1763 break;
1764 case ETHTOOL_GFLAGS:
1765 rc = ethtool_get_value(dev, useraddr, ethcmd,
1766 __ethtool_get_flags);
1767 break;
1768 case ETHTOOL_SFLAGS:
1769 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1770 break;
1771 case ETHTOOL_GPFLAGS:
1772 rc = ethtool_get_value(dev, useraddr, ethcmd,
1773 dev->ethtool_ops->get_priv_flags);
1774 break;
1775 case ETHTOOL_SPFLAGS:
1776 rc = ethtool_set_value(dev, useraddr,
1777 dev->ethtool_ops->set_priv_flags);
1778 break;
1779 case ETHTOOL_GRXFH:
1780 case ETHTOOL_GRXRINGS:
1781 case ETHTOOL_GRXCLSRLCNT:
1782 case ETHTOOL_GRXCLSRULE:
1783 case ETHTOOL_GRXCLSRLALL:
1784 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1785 break;
1786 case ETHTOOL_SRXFH:
1787 case ETHTOOL_SRXCLSRLDEL:
1788 case ETHTOOL_SRXCLSRLINS:
1789 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1790 break;
1791 case ETHTOOL_FLASHDEV:
1792 rc = ethtool_flash_device(dev, useraddr);
1793 break;
1794 case ETHTOOL_RESET:
1795 rc = ethtool_reset(dev, useraddr);
1796 break;
1797 case ETHTOOL_GSSET_INFO:
1798 rc = ethtool_get_sset_info(dev, useraddr);
1799 break;
1800 case ETHTOOL_GRXFHINDIR:
1801 rc = ethtool_get_rxfh_indir(dev, useraddr);
1802 break;
1803 case ETHTOOL_SRXFHINDIR:
1804 rc = ethtool_set_rxfh_indir(dev, useraddr);
1805 break;
1806 case ETHTOOL_GRSSH:
1807 rc = ethtool_get_rxfh(dev, useraddr);
1808 break;
1809 case ETHTOOL_SRSSH:
1810 rc = ethtool_set_rxfh(dev, useraddr);
1811 break;
1812 case ETHTOOL_GFEATURES:
1813 rc = ethtool_get_features(dev, useraddr);
1814 break;
1815 case ETHTOOL_SFEATURES:
1816 rc = ethtool_set_features(dev, useraddr);
1817 break;
1818 case ETHTOOL_GTXCSUM:
1819 case ETHTOOL_GRXCSUM:
1820 case ETHTOOL_GSG:
1821 case ETHTOOL_GTSO:
1822 case ETHTOOL_GUFO:
1823 case ETHTOOL_GGSO:
1824 case ETHTOOL_GGRO:
1825 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1826 break;
1827 case ETHTOOL_STXCSUM:
1828 case ETHTOOL_SRXCSUM:
1829 case ETHTOOL_SSG:
1830 case ETHTOOL_STSO:
1831 case ETHTOOL_SUFO:
1832 case ETHTOOL_SGSO:
1833 case ETHTOOL_SGRO:
1834 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1835 break;
1836 case ETHTOOL_GCHANNELS:
1837 rc = ethtool_get_channels(dev, useraddr);
1838 break;
1839 case ETHTOOL_SCHANNELS:
1840 rc = ethtool_set_channels(dev, useraddr);
1841 break;
1842 case ETHTOOL_SET_DUMP:
1843 rc = ethtool_set_dump(dev, useraddr);
1844 break;
1845 case ETHTOOL_GET_DUMP_FLAG:
1846 rc = ethtool_get_dump_flag(dev, useraddr);
1847 break;
1848 case ETHTOOL_GET_DUMP_DATA:
1849 rc = ethtool_get_dump_data(dev, useraddr);
1850 break;
1851 case ETHTOOL_GET_TS_INFO:
1852 rc = ethtool_get_ts_info(dev, useraddr);
1853 break;
1854 case ETHTOOL_GMODULEINFO:
1855 rc = ethtool_get_module_info(dev, useraddr);
1856 break;
1857 case ETHTOOL_GMODULEEEPROM:
1858 rc = ethtool_get_module_eeprom(dev, useraddr);
1859 break;
1860 default:
1861 rc = -EOPNOTSUPP;
1864 if (dev->ethtool_ops->complete)
1865 dev->ethtool_ops->complete(dev);
1867 if (old_features != dev->features)
1868 netdev_features_change(dev);
1870 return rc;