b43: use bcma_chipco_gpio_control()
[linux/fpc-iii.git] / net / core / ethtool.c
blob3e9b2c3e30f063cb3e59545f2af31176ea7b0151
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_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",
82 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
83 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
84 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
85 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
86 [NETIF_F_RXHASH_BIT] = "rx-hashing",
87 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
88 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
89 [NETIF_F_LOOPBACK_BIT] = "loopback",
90 [NETIF_F_RXFCS_BIT] = "rx-fcs",
91 [NETIF_F_RXALL_BIT] = "rx-all",
94 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
96 struct ethtool_gfeatures cmd = {
97 .cmd = ETHTOOL_GFEATURES,
98 .size = ETHTOOL_DEV_FEATURE_WORDS,
100 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
101 u32 __user *sizeaddr;
102 u32 copy_size;
103 int i;
105 /* in case feature bits run out again */
106 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
108 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
109 features[i].available = (u32)(dev->hw_features >> (32 * i));
110 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
111 features[i].active = (u32)(dev->features >> (32 * i));
112 features[i].never_changed =
113 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
116 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
117 if (get_user(copy_size, sizeaddr))
118 return -EFAULT;
120 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
121 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
123 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
124 return -EFAULT;
125 useraddr += sizeof(cmd);
126 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
127 return -EFAULT;
129 return 0;
132 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
134 struct ethtool_sfeatures cmd;
135 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
136 netdev_features_t wanted = 0, valid = 0;
137 int i, ret = 0;
139 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
140 return -EFAULT;
141 useraddr += sizeof(cmd);
143 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
144 return -EINVAL;
146 if (copy_from_user(features, useraddr, sizeof(features)))
147 return -EFAULT;
149 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
150 valid |= (netdev_features_t)features[i].valid << (32 * i);
151 wanted |= (netdev_features_t)features[i].requested << (32 * i);
154 if (valid & ~NETIF_F_ETHTOOL_BITS)
155 return -EINVAL;
157 if (valid & ~dev->hw_features) {
158 valid &= dev->hw_features;
159 ret |= ETHTOOL_F_UNSUPPORTED;
162 dev->wanted_features &= ~valid;
163 dev->wanted_features |= wanted & valid;
164 __netdev_update_features(dev);
166 if ((dev->wanted_features ^ dev->features) & valid)
167 ret |= ETHTOOL_F_WISH;
169 return ret;
172 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
174 const struct ethtool_ops *ops = dev->ethtool_ops;
176 if (sset == ETH_SS_FEATURES)
177 return ARRAY_SIZE(netdev_features_strings);
179 if (ops->get_sset_count && ops->get_strings)
180 return ops->get_sset_count(dev, sset);
181 else
182 return -EOPNOTSUPP;
185 static void __ethtool_get_strings(struct net_device *dev,
186 u32 stringset, u8 *data)
188 const struct ethtool_ops *ops = dev->ethtool_ops;
190 if (stringset == ETH_SS_FEATURES)
191 memcpy(data, netdev_features_strings,
192 sizeof(netdev_features_strings));
193 else
194 /* ops->get_strings is valid because checked earlier */
195 ops->get_strings(dev, stringset, data);
198 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
200 /* feature masks of legacy discrete ethtool ops */
202 switch (eth_cmd) {
203 case ETHTOOL_GTXCSUM:
204 case ETHTOOL_STXCSUM:
205 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
206 case ETHTOOL_GRXCSUM:
207 case ETHTOOL_SRXCSUM:
208 return NETIF_F_RXCSUM;
209 case ETHTOOL_GSG:
210 case ETHTOOL_SSG:
211 return NETIF_F_SG;
212 case ETHTOOL_GTSO:
213 case ETHTOOL_STSO:
214 return NETIF_F_ALL_TSO;
215 case ETHTOOL_GUFO:
216 case ETHTOOL_SUFO:
217 return NETIF_F_UFO;
218 case ETHTOOL_GGSO:
219 case ETHTOOL_SGSO:
220 return NETIF_F_GSO;
221 case ETHTOOL_GGRO:
222 case ETHTOOL_SGRO:
223 return NETIF_F_GRO;
224 default:
225 BUG();
229 static int ethtool_get_one_feature(struct net_device *dev,
230 char __user *useraddr, u32 ethcmd)
232 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
233 struct ethtool_value edata = {
234 .cmd = ethcmd,
235 .data = !!(dev->features & mask),
238 if (copy_to_user(useraddr, &edata, sizeof(edata)))
239 return -EFAULT;
240 return 0;
243 static int ethtool_set_one_feature(struct net_device *dev,
244 void __user *useraddr, u32 ethcmd)
246 struct ethtool_value edata;
247 netdev_features_t mask;
249 if (copy_from_user(&edata, useraddr, sizeof(edata)))
250 return -EFAULT;
252 mask = ethtool_get_feature_mask(ethcmd);
253 mask &= dev->hw_features;
254 if (!mask)
255 return -EOPNOTSUPP;
257 if (edata.data)
258 dev->wanted_features |= mask;
259 else
260 dev->wanted_features &= ~mask;
262 __netdev_update_features(dev);
264 return 0;
267 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
268 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
269 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
270 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
272 static u32 __ethtool_get_flags(struct net_device *dev)
274 u32 flags = 0;
276 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
277 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
278 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
279 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
280 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
282 return flags;
285 static int __ethtool_set_flags(struct net_device *dev, u32 data)
287 netdev_features_t features = 0, changed;
289 if (data & ~ETH_ALL_FLAGS)
290 return -EINVAL;
292 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
293 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
294 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
295 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
296 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
298 /* allow changing only bits set in hw_features */
299 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
300 if (changed & ~dev->hw_features)
301 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
303 dev->wanted_features =
304 (dev->wanted_features & ~changed) | (features & changed);
306 __netdev_update_features(dev);
308 return 0;
311 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
313 ASSERT_RTNL();
315 if (!dev->ethtool_ops->get_settings)
316 return -EOPNOTSUPP;
318 memset(cmd, 0, sizeof(struct ethtool_cmd));
319 cmd->cmd = ETHTOOL_GSET;
320 return dev->ethtool_ops->get_settings(dev, cmd);
322 EXPORT_SYMBOL(__ethtool_get_settings);
324 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
326 int err;
327 struct ethtool_cmd cmd;
329 err = __ethtool_get_settings(dev, &cmd);
330 if (err < 0)
331 return err;
333 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
334 return -EFAULT;
335 return 0;
338 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
340 struct ethtool_cmd cmd;
342 if (!dev->ethtool_ops->set_settings)
343 return -EOPNOTSUPP;
345 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
346 return -EFAULT;
348 return dev->ethtool_ops->set_settings(dev, &cmd);
351 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
352 void __user *useraddr)
354 struct ethtool_drvinfo info;
355 const struct ethtool_ops *ops = dev->ethtool_ops;
357 memset(&info, 0, sizeof(info));
358 info.cmd = ETHTOOL_GDRVINFO;
359 if (ops->get_drvinfo) {
360 ops->get_drvinfo(dev, &info);
361 } else if (dev->dev.parent && dev->dev.parent->driver) {
362 strlcpy(info.bus_info, dev_name(dev->dev.parent),
363 sizeof(info.bus_info));
364 strlcpy(info.driver, dev->dev.parent->driver->name,
365 sizeof(info.driver));
366 } else {
367 return -EOPNOTSUPP;
371 * this method of obtaining string set info is deprecated;
372 * Use ETHTOOL_GSSET_INFO instead.
374 if (ops->get_sset_count) {
375 int rc;
377 rc = ops->get_sset_count(dev, ETH_SS_TEST);
378 if (rc >= 0)
379 info.testinfo_len = rc;
380 rc = ops->get_sset_count(dev, ETH_SS_STATS);
381 if (rc >= 0)
382 info.n_stats = rc;
383 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
384 if (rc >= 0)
385 info.n_priv_flags = rc;
387 if (ops->get_regs_len)
388 info.regdump_len = ops->get_regs_len(dev);
389 if (ops->get_eeprom_len)
390 info.eedump_len = ops->get_eeprom_len(dev);
392 if (copy_to_user(useraddr, &info, sizeof(info)))
393 return -EFAULT;
394 return 0;
397 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
398 void __user *useraddr)
400 struct ethtool_sset_info info;
401 u64 sset_mask;
402 int i, idx = 0, n_bits = 0, ret, rc;
403 u32 *info_buf = NULL;
405 if (copy_from_user(&info, useraddr, sizeof(info)))
406 return -EFAULT;
408 /* store copy of mask, because we zero struct later on */
409 sset_mask = info.sset_mask;
410 if (!sset_mask)
411 return 0;
413 /* calculate size of return buffer */
414 n_bits = hweight64(sset_mask);
416 memset(&info, 0, sizeof(info));
417 info.cmd = ETHTOOL_GSSET_INFO;
419 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
420 if (!info_buf)
421 return -ENOMEM;
424 * fill return buffer based on input bitmask and successful
425 * get_sset_count return
427 for (i = 0; i < 64; i++) {
428 if (!(sset_mask & (1ULL << i)))
429 continue;
431 rc = __ethtool_get_sset_count(dev, i);
432 if (rc >= 0) {
433 info.sset_mask |= (1ULL << i);
434 info_buf[idx++] = rc;
438 ret = -EFAULT;
439 if (copy_to_user(useraddr, &info, sizeof(info)))
440 goto out;
442 useraddr += offsetof(struct ethtool_sset_info, data);
443 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
444 goto out;
446 ret = 0;
448 out:
449 kfree(info_buf);
450 return ret;
453 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
454 u32 cmd, void __user *useraddr)
456 struct ethtool_rxnfc info;
457 size_t info_size = sizeof(info);
458 int rc;
460 if (!dev->ethtool_ops->set_rxnfc)
461 return -EOPNOTSUPP;
463 /* struct ethtool_rxnfc was originally defined for
464 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
465 * members. User-space might still be using that
466 * definition. */
467 if (cmd == ETHTOOL_SRXFH)
468 info_size = (offsetof(struct ethtool_rxnfc, data) +
469 sizeof(info.data));
471 if (copy_from_user(&info, useraddr, info_size))
472 return -EFAULT;
474 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
475 if (rc)
476 return rc;
478 if (cmd == ETHTOOL_SRXCLSRLINS &&
479 copy_to_user(useraddr, &info, info_size))
480 return -EFAULT;
482 return 0;
485 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
486 u32 cmd, void __user *useraddr)
488 struct ethtool_rxnfc info;
489 size_t info_size = sizeof(info);
490 const struct ethtool_ops *ops = dev->ethtool_ops;
491 int ret;
492 void *rule_buf = NULL;
494 if (!ops->get_rxnfc)
495 return -EOPNOTSUPP;
497 /* struct ethtool_rxnfc was originally defined for
498 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
499 * members. User-space might still be using that
500 * definition. */
501 if (cmd == ETHTOOL_GRXFH)
502 info_size = (offsetof(struct ethtool_rxnfc, data) +
503 sizeof(info.data));
505 if (copy_from_user(&info, useraddr, info_size))
506 return -EFAULT;
508 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
509 if (info.rule_cnt > 0) {
510 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
511 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
512 GFP_USER);
513 if (!rule_buf)
514 return -ENOMEM;
518 ret = ops->get_rxnfc(dev, &info, rule_buf);
519 if (ret < 0)
520 goto err_out;
522 ret = -EFAULT;
523 if (copy_to_user(useraddr, &info, info_size))
524 goto err_out;
526 if (rule_buf) {
527 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
528 if (copy_to_user(useraddr, rule_buf,
529 info.rule_cnt * sizeof(u32)))
530 goto err_out;
532 ret = 0;
534 err_out:
535 kfree(rule_buf);
537 return ret;
540 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
541 void __user *useraddr)
543 u32 user_size, dev_size;
544 u32 *indir;
545 int ret;
547 if (!dev->ethtool_ops->get_rxfh_indir_size ||
548 !dev->ethtool_ops->get_rxfh_indir)
549 return -EOPNOTSUPP;
550 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
551 if (dev_size == 0)
552 return -EOPNOTSUPP;
554 if (copy_from_user(&user_size,
555 useraddr + offsetof(struct ethtool_rxfh_indir, size),
556 sizeof(user_size)))
557 return -EFAULT;
559 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
560 &dev_size, sizeof(dev_size)))
561 return -EFAULT;
563 /* If the user buffer size is 0, this is just a query for the
564 * device table size. Otherwise, if it's smaller than the
565 * device table size it's an error.
567 if (user_size < dev_size)
568 return user_size == 0 ? 0 : -EINVAL;
570 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
571 if (!indir)
572 return -ENOMEM;
574 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
575 if (ret)
576 goto out;
578 if (copy_to_user(useraddr +
579 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
580 indir, dev_size * sizeof(indir[0])))
581 ret = -EFAULT;
583 out:
584 kfree(indir);
585 return ret;
588 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
589 void __user *useraddr)
591 struct ethtool_rxnfc rx_rings;
592 u32 user_size, dev_size, i;
593 u32 *indir;
594 const struct ethtool_ops *ops = dev->ethtool_ops;
595 int ret;
597 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
598 !ops->get_rxnfc)
599 return -EOPNOTSUPP;
601 dev_size = ops->get_rxfh_indir_size(dev);
602 if (dev_size == 0)
603 return -EOPNOTSUPP;
605 if (copy_from_user(&user_size,
606 useraddr + offsetof(struct ethtool_rxfh_indir, size),
607 sizeof(user_size)))
608 return -EFAULT;
610 if (user_size != 0 && user_size != dev_size)
611 return -EINVAL;
613 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
614 if (!indir)
615 return -ENOMEM;
617 rx_rings.cmd = ETHTOOL_GRXRINGS;
618 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
619 if (ret)
620 goto out;
622 if (user_size == 0) {
623 for (i = 0; i < dev_size; i++)
624 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
625 } else {
626 if (copy_from_user(indir,
627 useraddr +
628 offsetof(struct ethtool_rxfh_indir,
629 ring_index[0]),
630 dev_size * sizeof(indir[0]))) {
631 ret = -EFAULT;
632 goto out;
635 /* Validate ring indices */
636 for (i = 0; i < dev_size; i++) {
637 if (indir[i] >= rx_rings.data) {
638 ret = -EINVAL;
639 goto out;
644 ret = ops->set_rxfh_indir(dev, indir);
646 out:
647 kfree(indir);
648 return ret;
651 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
653 struct ethtool_regs regs;
654 const struct ethtool_ops *ops = dev->ethtool_ops;
655 void *regbuf;
656 int reglen, ret;
658 if (!ops->get_regs || !ops->get_regs_len)
659 return -EOPNOTSUPP;
661 if (copy_from_user(&regs, useraddr, sizeof(regs)))
662 return -EFAULT;
664 reglen = ops->get_regs_len(dev);
665 if (regs.len > reglen)
666 regs.len = reglen;
668 regbuf = vzalloc(reglen);
669 if (reglen && !regbuf)
670 return -ENOMEM;
672 ops->get_regs(dev, &regs, regbuf);
674 ret = -EFAULT;
675 if (copy_to_user(useraddr, &regs, sizeof(regs)))
676 goto out;
677 useraddr += offsetof(struct ethtool_regs, data);
678 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
679 goto out;
680 ret = 0;
682 out:
683 vfree(regbuf);
684 return ret;
687 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
689 struct ethtool_value reset;
690 int ret;
692 if (!dev->ethtool_ops->reset)
693 return -EOPNOTSUPP;
695 if (copy_from_user(&reset, useraddr, sizeof(reset)))
696 return -EFAULT;
698 ret = dev->ethtool_ops->reset(dev, &reset.data);
699 if (ret)
700 return ret;
702 if (copy_to_user(useraddr, &reset, sizeof(reset)))
703 return -EFAULT;
704 return 0;
707 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
709 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
711 if (!dev->ethtool_ops->get_wol)
712 return -EOPNOTSUPP;
714 dev->ethtool_ops->get_wol(dev, &wol);
716 if (copy_to_user(useraddr, &wol, sizeof(wol)))
717 return -EFAULT;
718 return 0;
721 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
723 struct ethtool_wolinfo wol;
725 if (!dev->ethtool_ops->set_wol)
726 return -EOPNOTSUPP;
728 if (copy_from_user(&wol, useraddr, sizeof(wol)))
729 return -EFAULT;
731 return dev->ethtool_ops->set_wol(dev, &wol);
734 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
736 struct ethtool_eee edata;
737 int rc;
739 if (!dev->ethtool_ops->get_eee)
740 return -EOPNOTSUPP;
742 memset(&edata, 0, sizeof(struct ethtool_eee));
743 edata.cmd = ETHTOOL_GEEE;
744 rc = dev->ethtool_ops->get_eee(dev, &edata);
746 if (rc)
747 return rc;
749 if (copy_to_user(useraddr, &edata, sizeof(edata)))
750 return -EFAULT;
752 return 0;
755 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
757 struct ethtool_eee edata;
759 if (!dev->ethtool_ops->set_eee)
760 return -EOPNOTSUPP;
762 if (copy_from_user(&edata, useraddr, sizeof(edata)))
763 return -EFAULT;
765 return dev->ethtool_ops->set_eee(dev, &edata);
768 static int ethtool_nway_reset(struct net_device *dev)
770 if (!dev->ethtool_ops->nway_reset)
771 return -EOPNOTSUPP;
773 return dev->ethtool_ops->nway_reset(dev);
776 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
778 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
780 if (!dev->ethtool_ops->get_link)
781 return -EOPNOTSUPP;
783 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
785 if (copy_to_user(useraddr, &edata, sizeof(edata)))
786 return -EFAULT;
787 return 0;
790 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
791 int (*getter)(struct net_device *,
792 struct ethtool_eeprom *, u8 *),
793 u32 total_len)
795 struct ethtool_eeprom eeprom;
796 void __user *userbuf = useraddr + sizeof(eeprom);
797 u32 bytes_remaining;
798 u8 *data;
799 int ret = 0;
801 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
802 return -EFAULT;
804 /* Check for wrap and zero */
805 if (eeprom.offset + eeprom.len <= eeprom.offset)
806 return -EINVAL;
808 /* Check for exceeding total eeprom len */
809 if (eeprom.offset + eeprom.len > total_len)
810 return -EINVAL;
812 data = kmalloc(PAGE_SIZE, GFP_USER);
813 if (!data)
814 return -ENOMEM;
816 bytes_remaining = eeprom.len;
817 while (bytes_remaining > 0) {
818 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
820 ret = getter(dev, &eeprom, data);
821 if (ret)
822 break;
823 if (copy_to_user(userbuf, data, eeprom.len)) {
824 ret = -EFAULT;
825 break;
827 userbuf += eeprom.len;
828 eeprom.offset += eeprom.len;
829 bytes_remaining -= eeprom.len;
832 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
833 eeprom.offset -= eeprom.len;
834 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
835 ret = -EFAULT;
837 kfree(data);
838 return ret;
841 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
843 const struct ethtool_ops *ops = dev->ethtool_ops;
845 if (!ops->get_eeprom || !ops->get_eeprom_len)
846 return -EOPNOTSUPP;
848 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
849 ops->get_eeprom_len(dev));
852 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
854 struct ethtool_eeprom eeprom;
855 const struct ethtool_ops *ops = dev->ethtool_ops;
856 void __user *userbuf = useraddr + sizeof(eeprom);
857 u32 bytes_remaining;
858 u8 *data;
859 int ret = 0;
861 if (!ops->set_eeprom || !ops->get_eeprom_len)
862 return -EOPNOTSUPP;
864 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
865 return -EFAULT;
867 /* Check for wrap and zero */
868 if (eeprom.offset + eeprom.len <= eeprom.offset)
869 return -EINVAL;
871 /* Check for exceeding total eeprom len */
872 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
873 return -EINVAL;
875 data = kmalloc(PAGE_SIZE, GFP_USER);
876 if (!data)
877 return -ENOMEM;
879 bytes_remaining = eeprom.len;
880 while (bytes_remaining > 0) {
881 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
883 if (copy_from_user(data, userbuf, eeprom.len)) {
884 ret = -EFAULT;
885 break;
887 ret = ops->set_eeprom(dev, &eeprom, data);
888 if (ret)
889 break;
890 userbuf += eeprom.len;
891 eeprom.offset += eeprom.len;
892 bytes_remaining -= eeprom.len;
895 kfree(data);
896 return ret;
899 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
900 void __user *useraddr)
902 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
904 if (!dev->ethtool_ops->get_coalesce)
905 return -EOPNOTSUPP;
907 dev->ethtool_ops->get_coalesce(dev, &coalesce);
909 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
910 return -EFAULT;
911 return 0;
914 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
915 void __user *useraddr)
917 struct ethtool_coalesce coalesce;
919 if (!dev->ethtool_ops->set_coalesce)
920 return -EOPNOTSUPP;
922 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
923 return -EFAULT;
925 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
928 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
930 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
932 if (!dev->ethtool_ops->get_ringparam)
933 return -EOPNOTSUPP;
935 dev->ethtool_ops->get_ringparam(dev, &ringparam);
937 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
938 return -EFAULT;
939 return 0;
942 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
944 struct ethtool_ringparam ringparam;
946 if (!dev->ethtool_ops->set_ringparam)
947 return -EOPNOTSUPP;
949 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
950 return -EFAULT;
952 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
955 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
956 void __user *useraddr)
958 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
960 if (!dev->ethtool_ops->get_channels)
961 return -EOPNOTSUPP;
963 dev->ethtool_ops->get_channels(dev, &channels);
965 if (copy_to_user(useraddr, &channels, sizeof(channels)))
966 return -EFAULT;
967 return 0;
970 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
971 void __user *useraddr)
973 struct ethtool_channels channels;
975 if (!dev->ethtool_ops->set_channels)
976 return -EOPNOTSUPP;
978 if (copy_from_user(&channels, useraddr, sizeof(channels)))
979 return -EFAULT;
981 return dev->ethtool_ops->set_channels(dev, &channels);
984 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
986 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
988 if (!dev->ethtool_ops->get_pauseparam)
989 return -EOPNOTSUPP;
991 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
993 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
994 return -EFAULT;
995 return 0;
998 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1000 struct ethtool_pauseparam pauseparam;
1002 if (!dev->ethtool_ops->set_pauseparam)
1003 return -EOPNOTSUPP;
1005 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1006 return -EFAULT;
1008 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1011 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1013 struct ethtool_test test;
1014 const struct ethtool_ops *ops = dev->ethtool_ops;
1015 u64 *data;
1016 int ret, test_len;
1018 if (!ops->self_test || !ops->get_sset_count)
1019 return -EOPNOTSUPP;
1021 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1022 if (test_len < 0)
1023 return test_len;
1024 WARN_ON(test_len == 0);
1026 if (copy_from_user(&test, useraddr, sizeof(test)))
1027 return -EFAULT;
1029 test.len = test_len;
1030 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1031 if (!data)
1032 return -ENOMEM;
1034 ops->self_test(dev, &test, data);
1036 ret = -EFAULT;
1037 if (copy_to_user(useraddr, &test, sizeof(test)))
1038 goto out;
1039 useraddr += sizeof(test);
1040 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1041 goto out;
1042 ret = 0;
1044 out:
1045 kfree(data);
1046 return ret;
1049 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1051 struct ethtool_gstrings gstrings;
1052 u8 *data;
1053 int ret;
1055 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1056 return -EFAULT;
1058 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1059 if (ret < 0)
1060 return ret;
1062 gstrings.len = ret;
1064 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1065 if (!data)
1066 return -ENOMEM;
1068 __ethtool_get_strings(dev, gstrings.string_set, data);
1070 ret = -EFAULT;
1071 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1072 goto out;
1073 useraddr += sizeof(gstrings);
1074 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1075 goto out;
1076 ret = 0;
1078 out:
1079 kfree(data);
1080 return ret;
1083 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1085 struct ethtool_value id;
1086 static bool busy;
1087 const struct ethtool_ops *ops = dev->ethtool_ops;
1088 int rc;
1090 if (!ops->set_phys_id)
1091 return -EOPNOTSUPP;
1093 if (busy)
1094 return -EBUSY;
1096 if (copy_from_user(&id, useraddr, sizeof(id)))
1097 return -EFAULT;
1099 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1100 if (rc < 0)
1101 return rc;
1103 /* Drop the RTNL lock while waiting, but prevent reentry or
1104 * removal of the device.
1106 busy = true;
1107 dev_hold(dev);
1108 rtnl_unlock();
1110 if (rc == 0) {
1111 /* Driver will handle this itself */
1112 schedule_timeout_interruptible(
1113 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1114 } else {
1115 /* Driver expects to be called at twice the frequency in rc */
1116 int n = rc * 2, i, interval = HZ / n;
1118 /* Count down seconds */
1119 do {
1120 /* Count down iterations per second */
1121 i = n;
1122 do {
1123 rtnl_lock();
1124 rc = ops->set_phys_id(dev,
1125 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1126 rtnl_unlock();
1127 if (rc)
1128 break;
1129 schedule_timeout_interruptible(interval);
1130 } while (!signal_pending(current) && --i != 0);
1131 } while (!signal_pending(current) &&
1132 (id.data == 0 || --id.data != 0));
1135 rtnl_lock();
1136 dev_put(dev);
1137 busy = false;
1139 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1140 return rc;
1143 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1145 struct ethtool_stats stats;
1146 const struct ethtool_ops *ops = dev->ethtool_ops;
1147 u64 *data;
1148 int ret, n_stats;
1150 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1151 return -EOPNOTSUPP;
1153 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1154 if (n_stats < 0)
1155 return n_stats;
1156 WARN_ON(n_stats == 0);
1158 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1159 return -EFAULT;
1161 stats.n_stats = n_stats;
1162 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1163 if (!data)
1164 return -ENOMEM;
1166 ops->get_ethtool_stats(dev, &stats, data);
1168 ret = -EFAULT;
1169 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1170 goto out;
1171 useraddr += sizeof(stats);
1172 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1173 goto out;
1174 ret = 0;
1176 out:
1177 kfree(data);
1178 return ret;
1181 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1183 struct ethtool_perm_addr epaddr;
1185 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1186 return -EFAULT;
1188 if (epaddr.size < dev->addr_len)
1189 return -ETOOSMALL;
1190 epaddr.size = dev->addr_len;
1192 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1193 return -EFAULT;
1194 useraddr += sizeof(epaddr);
1195 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1196 return -EFAULT;
1197 return 0;
1200 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1201 u32 cmd, u32 (*actor)(struct net_device *))
1203 struct ethtool_value edata = { .cmd = cmd };
1205 if (!actor)
1206 return -EOPNOTSUPP;
1208 edata.data = actor(dev);
1210 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1211 return -EFAULT;
1212 return 0;
1215 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1216 void (*actor)(struct net_device *, u32))
1218 struct ethtool_value edata;
1220 if (!actor)
1221 return -EOPNOTSUPP;
1223 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1224 return -EFAULT;
1226 actor(dev, edata.data);
1227 return 0;
1230 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1231 int (*actor)(struct net_device *, u32))
1233 struct ethtool_value edata;
1235 if (!actor)
1236 return -EOPNOTSUPP;
1238 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1239 return -EFAULT;
1241 return actor(dev, edata.data);
1244 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1245 char __user *useraddr)
1247 struct ethtool_flash efl;
1249 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1250 return -EFAULT;
1252 if (!dev->ethtool_ops->flash_device)
1253 return -EOPNOTSUPP;
1255 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1257 return dev->ethtool_ops->flash_device(dev, &efl);
1260 static int ethtool_set_dump(struct net_device *dev,
1261 void __user *useraddr)
1263 struct ethtool_dump dump;
1265 if (!dev->ethtool_ops->set_dump)
1266 return -EOPNOTSUPP;
1268 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1269 return -EFAULT;
1271 return dev->ethtool_ops->set_dump(dev, &dump);
1274 static int ethtool_get_dump_flag(struct net_device *dev,
1275 void __user *useraddr)
1277 int ret;
1278 struct ethtool_dump dump;
1279 const struct ethtool_ops *ops = dev->ethtool_ops;
1281 if (!ops->get_dump_flag)
1282 return -EOPNOTSUPP;
1284 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1285 return -EFAULT;
1287 ret = ops->get_dump_flag(dev, &dump);
1288 if (ret)
1289 return ret;
1291 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1292 return -EFAULT;
1293 return 0;
1296 static int ethtool_get_dump_data(struct net_device *dev,
1297 void __user *useraddr)
1299 int ret;
1300 __u32 len;
1301 struct ethtool_dump dump, tmp;
1302 const struct ethtool_ops *ops = dev->ethtool_ops;
1303 void *data = NULL;
1305 if (!ops->get_dump_data || !ops->get_dump_flag)
1306 return -EOPNOTSUPP;
1308 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1309 return -EFAULT;
1311 memset(&tmp, 0, sizeof(tmp));
1312 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1313 ret = ops->get_dump_flag(dev, &tmp);
1314 if (ret)
1315 return ret;
1317 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1318 if (!len)
1319 return -EFAULT;
1321 data = vzalloc(tmp.len);
1322 if (!data)
1323 return -ENOMEM;
1324 ret = ops->get_dump_data(dev, &dump, data);
1325 if (ret)
1326 goto out;
1328 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1329 ret = -EFAULT;
1330 goto out;
1332 useraddr += offsetof(struct ethtool_dump, data);
1333 if (copy_to_user(useraddr, data, len))
1334 ret = -EFAULT;
1335 out:
1336 vfree(data);
1337 return ret;
1340 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1342 int err = 0;
1343 struct ethtool_ts_info info;
1344 const struct ethtool_ops *ops = dev->ethtool_ops;
1345 struct phy_device *phydev = dev->phydev;
1347 memset(&info, 0, sizeof(info));
1348 info.cmd = ETHTOOL_GET_TS_INFO;
1350 if (phydev && phydev->drv && phydev->drv->ts_info) {
1351 err = phydev->drv->ts_info(phydev, &info);
1352 } else if (ops->get_ts_info) {
1353 err = ops->get_ts_info(dev, &info);
1354 } else {
1355 info.so_timestamping =
1356 SOF_TIMESTAMPING_RX_SOFTWARE |
1357 SOF_TIMESTAMPING_SOFTWARE;
1358 info.phc_index = -1;
1361 if (err)
1362 return err;
1364 if (copy_to_user(useraddr, &info, sizeof(info)))
1365 err = -EFAULT;
1367 return err;
1370 static int ethtool_get_module_info(struct net_device *dev,
1371 void __user *useraddr)
1373 int ret;
1374 struct ethtool_modinfo modinfo;
1375 const struct ethtool_ops *ops = dev->ethtool_ops;
1377 if (!ops->get_module_info)
1378 return -EOPNOTSUPP;
1380 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1381 return -EFAULT;
1383 ret = ops->get_module_info(dev, &modinfo);
1384 if (ret)
1385 return ret;
1387 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1388 return -EFAULT;
1390 return 0;
1393 static int ethtool_get_module_eeprom(struct net_device *dev,
1394 void __user *useraddr)
1396 int ret;
1397 struct ethtool_modinfo modinfo;
1398 const struct ethtool_ops *ops = dev->ethtool_ops;
1400 if (!ops->get_module_info || !ops->get_module_eeprom)
1401 return -EOPNOTSUPP;
1403 ret = ops->get_module_info(dev, &modinfo);
1404 if (ret)
1405 return ret;
1407 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1408 modinfo.eeprom_len);
1411 /* The main entry point in this file. Called from net/core/dev.c */
1413 int dev_ethtool(struct net *net, struct ifreq *ifr)
1415 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1416 void __user *useraddr = ifr->ifr_data;
1417 u32 ethcmd;
1418 int rc;
1419 u32 old_features;
1421 if (!dev || !netif_device_present(dev))
1422 return -ENODEV;
1424 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1425 return -EFAULT;
1427 /* Allow some commands to be done by anyone */
1428 switch (ethcmd) {
1429 case ETHTOOL_GSET:
1430 case ETHTOOL_GDRVINFO:
1431 case ETHTOOL_GMSGLVL:
1432 case ETHTOOL_GLINK:
1433 case ETHTOOL_GCOALESCE:
1434 case ETHTOOL_GRINGPARAM:
1435 case ETHTOOL_GPAUSEPARAM:
1436 case ETHTOOL_GRXCSUM:
1437 case ETHTOOL_GTXCSUM:
1438 case ETHTOOL_GSG:
1439 case ETHTOOL_GSSET_INFO:
1440 case ETHTOOL_GSTRINGS:
1441 case ETHTOOL_GSTATS:
1442 case ETHTOOL_GTSO:
1443 case ETHTOOL_GPERMADDR:
1444 case ETHTOOL_GUFO:
1445 case ETHTOOL_GGSO:
1446 case ETHTOOL_GGRO:
1447 case ETHTOOL_GFLAGS:
1448 case ETHTOOL_GPFLAGS:
1449 case ETHTOOL_GRXFH:
1450 case ETHTOOL_GRXRINGS:
1451 case ETHTOOL_GRXCLSRLCNT:
1452 case ETHTOOL_GRXCLSRULE:
1453 case ETHTOOL_GRXCLSRLALL:
1454 case ETHTOOL_GRXFHINDIR:
1455 case ETHTOOL_GFEATURES:
1456 case ETHTOOL_GCHANNELS:
1457 case ETHTOOL_GET_TS_INFO:
1458 case ETHTOOL_GEEE:
1459 break;
1460 default:
1461 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1462 return -EPERM;
1465 if (dev->ethtool_ops->begin) {
1466 rc = dev->ethtool_ops->begin(dev);
1467 if (rc < 0)
1468 return rc;
1470 old_features = dev->features;
1472 switch (ethcmd) {
1473 case ETHTOOL_GSET:
1474 rc = ethtool_get_settings(dev, useraddr);
1475 break;
1476 case ETHTOOL_SSET:
1477 rc = ethtool_set_settings(dev, useraddr);
1478 break;
1479 case ETHTOOL_GDRVINFO:
1480 rc = ethtool_get_drvinfo(dev, useraddr);
1481 break;
1482 case ETHTOOL_GREGS:
1483 rc = ethtool_get_regs(dev, useraddr);
1484 break;
1485 case ETHTOOL_GWOL:
1486 rc = ethtool_get_wol(dev, useraddr);
1487 break;
1488 case ETHTOOL_SWOL:
1489 rc = ethtool_set_wol(dev, useraddr);
1490 break;
1491 case ETHTOOL_GMSGLVL:
1492 rc = ethtool_get_value(dev, useraddr, ethcmd,
1493 dev->ethtool_ops->get_msglevel);
1494 break;
1495 case ETHTOOL_SMSGLVL:
1496 rc = ethtool_set_value_void(dev, useraddr,
1497 dev->ethtool_ops->set_msglevel);
1498 break;
1499 case ETHTOOL_GEEE:
1500 rc = ethtool_get_eee(dev, useraddr);
1501 break;
1502 case ETHTOOL_SEEE:
1503 rc = ethtool_set_eee(dev, useraddr);
1504 break;
1505 case ETHTOOL_NWAY_RST:
1506 rc = ethtool_nway_reset(dev);
1507 break;
1508 case ETHTOOL_GLINK:
1509 rc = ethtool_get_link(dev, useraddr);
1510 break;
1511 case ETHTOOL_GEEPROM:
1512 rc = ethtool_get_eeprom(dev, useraddr);
1513 break;
1514 case ETHTOOL_SEEPROM:
1515 rc = ethtool_set_eeprom(dev, useraddr);
1516 break;
1517 case ETHTOOL_GCOALESCE:
1518 rc = ethtool_get_coalesce(dev, useraddr);
1519 break;
1520 case ETHTOOL_SCOALESCE:
1521 rc = ethtool_set_coalesce(dev, useraddr);
1522 break;
1523 case ETHTOOL_GRINGPARAM:
1524 rc = ethtool_get_ringparam(dev, useraddr);
1525 break;
1526 case ETHTOOL_SRINGPARAM:
1527 rc = ethtool_set_ringparam(dev, useraddr);
1528 break;
1529 case ETHTOOL_GPAUSEPARAM:
1530 rc = ethtool_get_pauseparam(dev, useraddr);
1531 break;
1532 case ETHTOOL_SPAUSEPARAM:
1533 rc = ethtool_set_pauseparam(dev, useraddr);
1534 break;
1535 case ETHTOOL_TEST:
1536 rc = ethtool_self_test(dev, useraddr);
1537 break;
1538 case ETHTOOL_GSTRINGS:
1539 rc = ethtool_get_strings(dev, useraddr);
1540 break;
1541 case ETHTOOL_PHYS_ID:
1542 rc = ethtool_phys_id(dev, useraddr);
1543 break;
1544 case ETHTOOL_GSTATS:
1545 rc = ethtool_get_stats(dev, useraddr);
1546 break;
1547 case ETHTOOL_GPERMADDR:
1548 rc = ethtool_get_perm_addr(dev, useraddr);
1549 break;
1550 case ETHTOOL_GFLAGS:
1551 rc = ethtool_get_value(dev, useraddr, ethcmd,
1552 __ethtool_get_flags);
1553 break;
1554 case ETHTOOL_SFLAGS:
1555 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1556 break;
1557 case ETHTOOL_GPFLAGS:
1558 rc = ethtool_get_value(dev, useraddr, ethcmd,
1559 dev->ethtool_ops->get_priv_flags);
1560 break;
1561 case ETHTOOL_SPFLAGS:
1562 rc = ethtool_set_value(dev, useraddr,
1563 dev->ethtool_ops->set_priv_flags);
1564 break;
1565 case ETHTOOL_GRXFH:
1566 case ETHTOOL_GRXRINGS:
1567 case ETHTOOL_GRXCLSRLCNT:
1568 case ETHTOOL_GRXCLSRULE:
1569 case ETHTOOL_GRXCLSRLALL:
1570 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1571 break;
1572 case ETHTOOL_SRXFH:
1573 case ETHTOOL_SRXCLSRLDEL:
1574 case ETHTOOL_SRXCLSRLINS:
1575 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1576 break;
1577 case ETHTOOL_FLASHDEV:
1578 rc = ethtool_flash_device(dev, useraddr);
1579 break;
1580 case ETHTOOL_RESET:
1581 rc = ethtool_reset(dev, useraddr);
1582 break;
1583 case ETHTOOL_GSSET_INFO:
1584 rc = ethtool_get_sset_info(dev, useraddr);
1585 break;
1586 case ETHTOOL_GRXFHINDIR:
1587 rc = ethtool_get_rxfh_indir(dev, useraddr);
1588 break;
1589 case ETHTOOL_SRXFHINDIR:
1590 rc = ethtool_set_rxfh_indir(dev, useraddr);
1591 break;
1592 case ETHTOOL_GFEATURES:
1593 rc = ethtool_get_features(dev, useraddr);
1594 break;
1595 case ETHTOOL_SFEATURES:
1596 rc = ethtool_set_features(dev, useraddr);
1597 break;
1598 case ETHTOOL_GTXCSUM:
1599 case ETHTOOL_GRXCSUM:
1600 case ETHTOOL_GSG:
1601 case ETHTOOL_GTSO:
1602 case ETHTOOL_GUFO:
1603 case ETHTOOL_GGSO:
1604 case ETHTOOL_GGRO:
1605 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1606 break;
1607 case ETHTOOL_STXCSUM:
1608 case ETHTOOL_SRXCSUM:
1609 case ETHTOOL_SSG:
1610 case ETHTOOL_STSO:
1611 case ETHTOOL_SUFO:
1612 case ETHTOOL_SGSO:
1613 case ETHTOOL_SGRO:
1614 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1615 break;
1616 case ETHTOOL_GCHANNELS:
1617 rc = ethtool_get_channels(dev, useraddr);
1618 break;
1619 case ETHTOOL_SCHANNELS:
1620 rc = ethtool_set_channels(dev, useraddr);
1621 break;
1622 case ETHTOOL_SET_DUMP:
1623 rc = ethtool_set_dump(dev, useraddr);
1624 break;
1625 case ETHTOOL_GET_DUMP_FLAG:
1626 rc = ethtool_get_dump_flag(dev, useraddr);
1627 break;
1628 case ETHTOOL_GET_DUMP_DATA:
1629 rc = ethtool_get_dump_data(dev, useraddr);
1630 break;
1631 case ETHTOOL_GET_TS_INFO:
1632 rc = ethtool_get_ts_info(dev, useraddr);
1633 break;
1634 case ETHTOOL_GMODULEINFO:
1635 rc = ethtool_get_module_info(dev, useraddr);
1636 break;
1637 case ETHTOOL_GMODULEEEPROM:
1638 rc = ethtool_get_module_eeprom(dev, useraddr);
1639 break;
1640 default:
1641 rc = -EOPNOTSUPP;
1644 if (dev->ethtool_ops->complete)
1645 dev->ethtool_ops->complete(dev);
1647 if (old_features != dev->features)
1648 netdev_features_change(dev);
1650 return rc;