xhci: Always set urb->status to zero for isoc endpoints.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / ethtool.c
blob76ed64563a2ebab4d9f2a7382dc75d0f7fe65299
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/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
26 * Some useful ethtool_ops methods that're device independent.
27 * If we find that all drivers want to do the same thing here,
28 * we can turn these into dev_() function calls.
31 u32 ethtool_op_get_link(struct net_device *dev)
33 return netif_carrier_ok(dev) ? 1 : 0;
35 EXPORT_SYMBOL(ethtool_op_get_link);
37 u32 ethtool_op_get_tx_csum(struct net_device *dev)
39 return (dev->features & NETIF_F_ALL_CSUM) != 0;
41 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
43 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
45 if (data)
46 dev->features |= NETIF_F_IP_CSUM;
47 else
48 dev->features &= ~NETIF_F_IP_CSUM;
50 return 0;
52 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
54 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
56 if (data)
57 dev->features |= NETIF_F_HW_CSUM;
58 else
59 dev->features &= ~NETIF_F_HW_CSUM;
61 return 0;
63 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
65 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
67 if (data)
68 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
69 else
70 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
72 return 0;
74 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
76 u32 ethtool_op_get_sg(struct net_device *dev)
78 return (dev->features & NETIF_F_SG) != 0;
80 EXPORT_SYMBOL(ethtool_op_get_sg);
82 int ethtool_op_set_sg(struct net_device *dev, u32 data)
84 if (data)
85 dev->features |= NETIF_F_SG;
86 else
87 dev->features &= ~NETIF_F_SG;
89 return 0;
91 EXPORT_SYMBOL(ethtool_op_set_sg);
93 u32 ethtool_op_get_tso(struct net_device *dev)
95 return (dev->features & NETIF_F_TSO) != 0;
97 EXPORT_SYMBOL(ethtool_op_get_tso);
99 int ethtool_op_set_tso(struct net_device *dev, u32 data)
101 if (data)
102 dev->features |= NETIF_F_TSO;
103 else
104 dev->features &= ~NETIF_F_TSO;
106 return 0;
108 EXPORT_SYMBOL(ethtool_op_set_tso);
110 u32 ethtool_op_get_ufo(struct net_device *dev)
112 return (dev->features & NETIF_F_UFO) != 0;
114 EXPORT_SYMBOL(ethtool_op_get_ufo);
116 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
118 if (data)
119 dev->features |= NETIF_F_UFO;
120 else
121 dev->features &= ~NETIF_F_UFO;
122 return 0;
124 EXPORT_SYMBOL(ethtool_op_set_ufo);
126 /* the following list of flags are the same as their associated
127 * NETIF_F_xxx values in include/linux/netdevice.h
129 static const u32 flags_dup_features =
130 (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
131 ETH_FLAG_RXHASH);
133 u32 ethtool_op_get_flags(struct net_device *dev)
135 /* in the future, this function will probably contain additional
136 * handling for flags which are not so easily handled
137 * by a simple masking operation
140 return dev->features & flags_dup_features;
142 EXPORT_SYMBOL(ethtool_op_get_flags);
144 /* Check if device can enable (or disable) particular feature coded in "data"
145 * argument. Flags "supported" describe features that can be toggled by device.
146 * If feature can not be toggled, it state (enabled or disabled) must match
147 * hardcoded device features state, otherwise flags are marked as invalid.
149 bool ethtool_invalid_flags(struct net_device *dev, u32 data, u32 supported)
151 u32 features = dev->features & flags_dup_features;
152 /* "data" can contain only flags_dup_features bits,
153 * see __ethtool_set_flags */
155 return (features & ~supported) != (data & ~supported);
157 EXPORT_SYMBOL(ethtool_invalid_flags);
159 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
161 if (ethtool_invalid_flags(dev, data, supported))
162 return -EINVAL;
164 dev->features = ((dev->features & ~flags_dup_features) |
165 (data & flags_dup_features));
166 return 0;
168 EXPORT_SYMBOL(ethtool_op_set_flags);
170 void ethtool_ntuple_flush(struct net_device *dev)
172 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
174 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
175 list_del(&fsc->list);
176 kfree(fsc);
178 dev->ethtool_ntuple_list.count = 0;
180 EXPORT_SYMBOL(ethtool_ntuple_flush);
182 /* Handlers for each ethtool command */
184 #define ETHTOOL_DEV_FEATURE_WORDS 1
186 static void ethtool_get_features_compat(struct net_device *dev,
187 struct ethtool_get_features_block *features)
189 if (!dev->ethtool_ops)
190 return;
192 /* getting RX checksum */
193 if (dev->ethtool_ops->get_rx_csum)
194 if (dev->ethtool_ops->get_rx_csum(dev))
195 features[0].active |= NETIF_F_RXCSUM;
197 /* mark legacy-changeable features */
198 if (dev->ethtool_ops->set_sg)
199 features[0].available |= NETIF_F_SG;
200 if (dev->ethtool_ops->set_tx_csum)
201 features[0].available |= NETIF_F_ALL_CSUM;
202 if (dev->ethtool_ops->set_tso)
203 features[0].available |= NETIF_F_ALL_TSO;
204 if (dev->ethtool_ops->set_rx_csum)
205 features[0].available |= NETIF_F_RXCSUM;
206 if (dev->ethtool_ops->set_flags)
207 features[0].available |= flags_dup_features;
210 static int ethtool_set_feature_compat(struct net_device *dev,
211 int (*legacy_set)(struct net_device *, u32),
212 struct ethtool_set_features_block *features, u32 mask)
214 u32 do_set;
216 if (!legacy_set)
217 return 0;
219 if (!(features[0].valid & mask))
220 return 0;
222 features[0].valid &= ~mask;
224 do_set = !!(features[0].requested & mask);
226 if (legacy_set(dev, do_set) < 0)
227 netdev_info(dev,
228 "Legacy feature change (%s) failed for 0x%08x\n",
229 do_set ? "set" : "clear", mask);
231 return 1;
234 static int ethtool_set_flags_compat(struct net_device *dev,
235 int (*legacy_set)(struct net_device *, u32),
236 struct ethtool_set_features_block *features, u32 mask)
238 u32 value;
240 if (!legacy_set)
241 return 0;
243 if (!(features[0].valid & mask))
244 return 0;
246 value = dev->features & ~features[0].valid;
247 value |= features[0].requested;
249 features[0].valid &= ~mask;
251 if (legacy_set(dev, value & mask) < 0)
252 netdev_info(dev, "Legacy flags change failed\n");
254 return 1;
257 static int ethtool_set_features_compat(struct net_device *dev,
258 struct ethtool_set_features_block *features)
260 int compat;
262 if (!dev->ethtool_ops)
263 return 0;
265 compat = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
266 features, NETIF_F_SG);
267 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
268 features, NETIF_F_ALL_CSUM);
269 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
270 features, NETIF_F_ALL_TSO);
271 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
272 features, NETIF_F_RXCSUM);
273 compat |= ethtool_set_flags_compat(dev, dev->ethtool_ops->set_flags,
274 features, flags_dup_features);
276 return compat;
279 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
281 struct ethtool_gfeatures cmd = {
282 .cmd = ETHTOOL_GFEATURES,
283 .size = ETHTOOL_DEV_FEATURE_WORDS,
285 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
287 .available = dev->hw_features,
288 .requested = dev->wanted_features,
289 .active = dev->features,
290 .never_changed = NETIF_F_NEVER_CHANGE,
293 u32 __user *sizeaddr;
294 u32 copy_size;
296 ethtool_get_features_compat(dev, features);
298 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
299 if (get_user(copy_size, sizeaddr))
300 return -EFAULT;
302 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
303 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
305 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
306 return -EFAULT;
307 useraddr += sizeof(cmd);
308 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
309 return -EFAULT;
311 return 0;
314 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
316 struct ethtool_sfeatures cmd;
317 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
318 int ret = 0;
320 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
321 return -EFAULT;
322 useraddr += sizeof(cmd);
324 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
325 return -EINVAL;
327 if (copy_from_user(features, useraddr, sizeof(features)))
328 return -EFAULT;
330 if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
331 return -EINVAL;
333 if (ethtool_set_features_compat(dev, features))
334 ret |= ETHTOOL_F_COMPAT;
336 if (features[0].valid & ~dev->hw_features) {
337 features[0].valid &= dev->hw_features;
338 ret |= ETHTOOL_F_UNSUPPORTED;
341 dev->wanted_features &= ~features[0].valid;
342 dev->wanted_features |= features[0].valid & features[0].requested;
343 netdev_update_features(dev);
345 if ((dev->wanted_features ^ dev->features) & features[0].valid)
346 ret |= ETHTOOL_F_WISH;
348 return ret;
351 static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
352 /* NETIF_F_SG */ "tx-scatter-gather",
353 /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
354 /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
355 /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
356 /* NETIF_F_IPV6_CSUM */ "tx-checksum-ipv6",
357 /* NETIF_F_HIGHDMA */ "highdma",
358 /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
359 /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
361 /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
362 /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
363 /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
364 /* NETIF_F_GSO */ "tx-generic-segmentation",
365 /* NETIF_F_LLTX */ "tx-lockless",
366 /* NETIF_F_NETNS_LOCAL */ "netns-local",
367 /* NETIF_F_GRO */ "rx-gro",
368 /* NETIF_F_LRO */ "rx-lro",
370 /* NETIF_F_TSO */ "tx-tcp-segmentation",
371 /* NETIF_F_UFO */ "tx-udp-fragmentation",
372 /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
373 /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
374 /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
375 /* NETIF_F_FSO */ "tx-fcoe-segmentation",
379 /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
380 /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
381 /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
382 /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
383 /* NETIF_F_RXHASH */ "rx-hashing",
384 /* NETIF_F_RXCSUM */ "rx-checksum",
389 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
391 const struct ethtool_ops *ops = dev->ethtool_ops;
393 if (sset == ETH_SS_FEATURES)
394 return ARRAY_SIZE(netdev_features_strings);
396 if (ops && ops->get_sset_count && ops->get_strings)
397 return ops->get_sset_count(dev, sset);
398 else
399 return -EOPNOTSUPP;
402 static void __ethtool_get_strings(struct net_device *dev,
403 u32 stringset, u8 *data)
405 const struct ethtool_ops *ops = dev->ethtool_ops;
407 if (stringset == ETH_SS_FEATURES)
408 memcpy(data, netdev_features_strings,
409 sizeof(netdev_features_strings));
410 else
411 /* ops->get_strings is valid because checked earlier */
412 ops->get_strings(dev, stringset, data);
415 static u32 ethtool_get_feature_mask(u32 eth_cmd)
417 /* feature masks of legacy discrete ethtool ops */
419 switch (eth_cmd) {
420 case ETHTOOL_GTXCSUM:
421 case ETHTOOL_STXCSUM:
422 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
423 case ETHTOOL_GRXCSUM:
424 case ETHTOOL_SRXCSUM:
425 return NETIF_F_RXCSUM;
426 case ETHTOOL_GSG:
427 case ETHTOOL_SSG:
428 return NETIF_F_SG;
429 case ETHTOOL_GTSO:
430 case ETHTOOL_STSO:
431 return NETIF_F_ALL_TSO;
432 case ETHTOOL_GUFO:
433 case ETHTOOL_SUFO:
434 return NETIF_F_UFO;
435 case ETHTOOL_GGSO:
436 case ETHTOOL_SGSO:
437 return NETIF_F_GSO;
438 case ETHTOOL_GGRO:
439 case ETHTOOL_SGRO:
440 return NETIF_F_GRO;
441 default:
442 BUG();
446 static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
448 const struct ethtool_ops *ops = dev->ethtool_ops;
450 if (!ops)
451 return NULL;
453 switch (ethcmd) {
454 case ETHTOOL_GTXCSUM:
455 return ops->get_tx_csum;
456 case ETHTOOL_GRXCSUM:
457 return ops->get_rx_csum;
458 case ETHTOOL_SSG:
459 return ops->get_sg;
460 case ETHTOOL_STSO:
461 return ops->get_tso;
462 case ETHTOOL_SUFO:
463 return ops->get_ufo;
464 default:
465 return NULL;
469 static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
471 return !!(dev->features & NETIF_F_ALL_CSUM);
474 static int ethtool_get_one_feature(struct net_device *dev,
475 char __user *useraddr, u32 ethcmd)
477 u32 mask = ethtool_get_feature_mask(ethcmd);
478 struct ethtool_value edata = {
479 .cmd = ethcmd,
480 .data = !!(dev->features & mask),
483 /* compatibility with discrete get_ ops */
484 if (!(dev->hw_features & mask)) {
485 u32 (*actor)(struct net_device *);
487 actor = __ethtool_get_one_feature_actor(dev, ethcmd);
489 /* bug compatibility with old get_rx_csum */
490 if (ethcmd == ETHTOOL_GRXCSUM && !actor)
491 actor = __ethtool_get_rx_csum_oldbug;
493 if (actor)
494 edata.data = actor(dev);
497 if (copy_to_user(useraddr, &edata, sizeof(edata)))
498 return -EFAULT;
499 return 0;
502 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
503 static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
504 static int __ethtool_set_sg(struct net_device *dev, u32 data);
505 static int __ethtool_set_tso(struct net_device *dev, u32 data);
506 static int __ethtool_set_ufo(struct net_device *dev, u32 data);
508 static int ethtool_set_one_feature(struct net_device *dev,
509 void __user *useraddr, u32 ethcmd)
511 struct ethtool_value edata;
512 u32 mask;
514 if (copy_from_user(&edata, useraddr, sizeof(edata)))
515 return -EFAULT;
517 mask = ethtool_get_feature_mask(ethcmd);
518 mask &= dev->hw_features;
519 if (mask) {
520 if (edata.data)
521 dev->wanted_features |= mask;
522 else
523 dev->wanted_features &= ~mask;
525 netdev_update_features(dev);
526 return 0;
529 /* Driver is not converted to ndo_fix_features or does not
530 * support changing this offload. In the latter case it won't
531 * have corresponding ethtool_ops field set.
533 * Following part is to be removed after all drivers advertise
534 * their changeable features in netdev->hw_features and stop
535 * using discrete offload setting ops.
538 switch (ethcmd) {
539 case ETHTOOL_STXCSUM:
540 return __ethtool_set_tx_csum(dev, edata.data);
541 case ETHTOOL_SRXCSUM:
542 return __ethtool_set_rx_csum(dev, edata.data);
543 case ETHTOOL_SSG:
544 return __ethtool_set_sg(dev, edata.data);
545 case ETHTOOL_STSO:
546 return __ethtool_set_tso(dev, edata.data);
547 case ETHTOOL_SUFO:
548 return __ethtool_set_ufo(dev, edata.data);
549 default:
550 return -EOPNOTSUPP;
554 int __ethtool_set_flags(struct net_device *dev, u32 data)
556 u32 changed;
558 if (data & ~flags_dup_features)
559 return -EINVAL;
561 /* legacy set_flags() op */
562 if (dev->ethtool_ops->set_flags) {
563 if (unlikely(dev->hw_features & flags_dup_features))
564 netdev_warn(dev,
565 "driver BUG: mixed hw_features and set_flags()\n");
566 return dev->ethtool_ops->set_flags(dev, data);
569 /* allow changing only bits set in hw_features */
570 changed = (data ^ dev->wanted_features) & flags_dup_features;
571 if (changed & ~dev->hw_features)
572 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
574 dev->wanted_features =
575 (dev->wanted_features & ~changed) | data;
577 netdev_update_features(dev);
579 return 0;
582 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
584 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
585 int err;
587 if (!dev->ethtool_ops->get_settings)
588 return -EOPNOTSUPP;
590 err = dev->ethtool_ops->get_settings(dev, &cmd);
591 if (err < 0)
592 return err;
594 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
595 return -EFAULT;
596 return 0;
599 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
601 struct ethtool_cmd cmd;
603 if (!dev->ethtool_ops->set_settings)
604 return -EOPNOTSUPP;
606 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
607 return -EFAULT;
609 return dev->ethtool_ops->set_settings(dev, &cmd);
612 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
613 void __user *useraddr)
615 struct ethtool_drvinfo info;
616 const struct ethtool_ops *ops = dev->ethtool_ops;
618 memset(&info, 0, sizeof(info));
619 info.cmd = ETHTOOL_GDRVINFO;
620 if (ops && ops->get_drvinfo) {
621 ops->get_drvinfo(dev, &info);
622 } else if (dev->dev.parent && dev->dev.parent->driver) {
623 strlcpy(info.bus_info, dev_name(dev->dev.parent),
624 sizeof(info.bus_info));
625 strlcpy(info.driver, dev->dev.parent->driver->name,
626 sizeof(info.driver));
627 } else {
628 return -EOPNOTSUPP;
632 * this method of obtaining string set info is deprecated;
633 * Use ETHTOOL_GSSET_INFO instead.
635 if (ops && ops->get_sset_count) {
636 int rc;
638 rc = ops->get_sset_count(dev, ETH_SS_TEST);
639 if (rc >= 0)
640 info.testinfo_len = rc;
641 rc = ops->get_sset_count(dev, ETH_SS_STATS);
642 if (rc >= 0)
643 info.n_stats = rc;
644 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
645 if (rc >= 0)
646 info.n_priv_flags = rc;
648 if (ops && ops->get_regs_len)
649 info.regdump_len = ops->get_regs_len(dev);
650 if (ops && ops->get_eeprom_len)
651 info.eedump_len = ops->get_eeprom_len(dev);
653 if (copy_to_user(useraddr, &info, sizeof(info)))
654 return -EFAULT;
655 return 0;
658 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
659 void __user *useraddr)
661 struct ethtool_sset_info info;
662 u64 sset_mask;
663 int i, idx = 0, n_bits = 0, ret, rc;
664 u32 *info_buf = NULL;
666 if (copy_from_user(&info, useraddr, sizeof(info)))
667 return -EFAULT;
669 /* store copy of mask, because we zero struct later on */
670 sset_mask = info.sset_mask;
671 if (!sset_mask)
672 return 0;
674 /* calculate size of return buffer */
675 n_bits = hweight64(sset_mask);
677 memset(&info, 0, sizeof(info));
678 info.cmd = ETHTOOL_GSSET_INFO;
680 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
681 if (!info_buf)
682 return -ENOMEM;
685 * fill return buffer based on input bitmask and successful
686 * get_sset_count return
688 for (i = 0; i < 64; i++) {
689 if (!(sset_mask & (1ULL << i)))
690 continue;
692 rc = __ethtool_get_sset_count(dev, i);
693 if (rc >= 0) {
694 info.sset_mask |= (1ULL << i);
695 info_buf[idx++] = rc;
699 ret = -EFAULT;
700 if (copy_to_user(useraddr, &info, sizeof(info)))
701 goto out;
703 useraddr += offsetof(struct ethtool_sset_info, data);
704 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
705 goto out;
707 ret = 0;
709 out:
710 kfree(info_buf);
711 return ret;
714 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
715 u32 cmd, void __user *useraddr)
717 struct ethtool_rxnfc info;
718 size_t info_size = sizeof(info);
720 if (!dev->ethtool_ops->set_rxnfc)
721 return -EOPNOTSUPP;
723 /* struct ethtool_rxnfc was originally defined for
724 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
725 * members. User-space might still be using that
726 * definition. */
727 if (cmd == ETHTOOL_SRXFH)
728 info_size = (offsetof(struct ethtool_rxnfc, data) +
729 sizeof(info.data));
731 if (copy_from_user(&info, useraddr, info_size))
732 return -EFAULT;
734 return dev->ethtool_ops->set_rxnfc(dev, &info);
737 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
738 u32 cmd, void __user *useraddr)
740 struct ethtool_rxnfc info;
741 size_t info_size = sizeof(info);
742 const struct ethtool_ops *ops = dev->ethtool_ops;
743 int ret;
744 void *rule_buf = NULL;
746 if (!ops->get_rxnfc)
747 return -EOPNOTSUPP;
749 /* struct ethtool_rxnfc was originally defined for
750 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
751 * members. User-space might still be using that
752 * definition. */
753 if (cmd == ETHTOOL_GRXFH)
754 info_size = (offsetof(struct ethtool_rxnfc, data) +
755 sizeof(info.data));
757 if (copy_from_user(&info, useraddr, info_size))
758 return -EFAULT;
760 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
761 if (info.rule_cnt > 0) {
762 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
763 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
764 GFP_USER);
765 if (!rule_buf)
766 return -ENOMEM;
770 ret = ops->get_rxnfc(dev, &info, rule_buf);
771 if (ret < 0)
772 goto err_out;
774 ret = -EFAULT;
775 if (copy_to_user(useraddr, &info, info_size))
776 goto err_out;
778 if (rule_buf) {
779 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
780 if (copy_to_user(useraddr, rule_buf,
781 info.rule_cnt * sizeof(u32)))
782 goto err_out;
784 ret = 0;
786 err_out:
787 kfree(rule_buf);
789 return ret;
792 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
793 void __user *useraddr)
795 struct ethtool_rxfh_indir *indir;
796 u32 table_size;
797 size_t full_size;
798 int ret;
800 if (!dev->ethtool_ops->get_rxfh_indir)
801 return -EOPNOTSUPP;
803 if (copy_from_user(&table_size,
804 useraddr + offsetof(struct ethtool_rxfh_indir, size),
805 sizeof(table_size)))
806 return -EFAULT;
808 if (table_size >
809 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
810 return -ENOMEM;
811 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
812 indir = kzalloc(full_size, GFP_USER);
813 if (!indir)
814 return -ENOMEM;
816 indir->cmd = ETHTOOL_GRXFHINDIR;
817 indir->size = table_size;
818 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
819 if (ret)
820 goto out;
822 if (copy_to_user(useraddr, indir, full_size))
823 ret = -EFAULT;
825 out:
826 kfree(indir);
827 return ret;
830 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
831 void __user *useraddr)
833 struct ethtool_rxfh_indir *indir;
834 u32 table_size;
835 size_t full_size;
836 int ret;
838 if (!dev->ethtool_ops->set_rxfh_indir)
839 return -EOPNOTSUPP;
841 if (copy_from_user(&table_size,
842 useraddr + offsetof(struct ethtool_rxfh_indir, size),
843 sizeof(table_size)))
844 return -EFAULT;
846 if (table_size >
847 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
848 return -ENOMEM;
849 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
850 indir = kmalloc(full_size, GFP_USER);
851 if (!indir)
852 return -ENOMEM;
854 if (copy_from_user(indir, useraddr, full_size)) {
855 ret = -EFAULT;
856 goto out;
859 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
861 out:
862 kfree(indir);
863 return ret;
866 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
867 struct ethtool_rx_ntuple_flow_spec *spec,
868 struct ethtool_rx_ntuple_flow_spec_container *fsc)
871 /* don't add filters forever */
872 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
873 /* free the container */
874 kfree(fsc);
875 return;
878 /* Copy the whole filter over */
879 fsc->fs.flow_type = spec->flow_type;
880 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
881 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
883 fsc->fs.vlan_tag = spec->vlan_tag;
884 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
885 fsc->fs.data = spec->data;
886 fsc->fs.data_mask = spec->data_mask;
887 fsc->fs.action = spec->action;
889 /* add to the list */
890 list_add_tail_rcu(&fsc->list, &list->list);
891 list->count++;
895 * ethtool does not (or did not) set masks for flow parameters that are
896 * not specified, so if both value and mask are 0 then this must be
897 * treated as equivalent to a mask with all bits set. Implement that
898 * here rather than in drivers.
900 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
902 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
903 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
905 if (fs->flow_type != TCP_V4_FLOW &&
906 fs->flow_type != UDP_V4_FLOW &&
907 fs->flow_type != SCTP_V4_FLOW)
908 return;
910 if (!(entry->ip4src | mask->ip4src))
911 mask->ip4src = htonl(0xffffffff);
912 if (!(entry->ip4dst | mask->ip4dst))
913 mask->ip4dst = htonl(0xffffffff);
914 if (!(entry->psrc | mask->psrc))
915 mask->psrc = htons(0xffff);
916 if (!(entry->pdst | mask->pdst))
917 mask->pdst = htons(0xffff);
918 if (!(entry->tos | mask->tos))
919 mask->tos = 0xff;
920 if (!(fs->vlan_tag | fs->vlan_tag_mask))
921 fs->vlan_tag_mask = 0xffff;
922 if (!(fs->data | fs->data_mask))
923 fs->data_mask = 0xffffffffffffffffULL;
926 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
927 void __user *useraddr)
929 struct ethtool_rx_ntuple cmd;
930 const struct ethtool_ops *ops = dev->ethtool_ops;
931 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
932 int ret;
934 if (!(dev->features & NETIF_F_NTUPLE))
935 return -EINVAL;
937 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
938 return -EFAULT;
940 rx_ntuple_fix_masks(&cmd.fs);
943 * Cache filter in dev struct for GET operation only if
944 * the underlying driver doesn't have its own GET operation, and
945 * only if the filter was added successfully. First make sure we
946 * can allocate the filter, then continue if successful.
948 if (!ops->get_rx_ntuple) {
949 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
950 if (!fsc)
951 return -ENOMEM;
954 ret = ops->set_rx_ntuple(dev, &cmd);
955 if (ret) {
956 kfree(fsc);
957 return ret;
960 if (!ops->get_rx_ntuple)
961 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
963 return ret;
966 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
968 struct ethtool_gstrings gstrings;
969 const struct ethtool_ops *ops = dev->ethtool_ops;
970 struct ethtool_rx_ntuple_flow_spec_container *fsc;
971 u8 *data;
972 char *p;
973 int ret, i, num_strings = 0;
975 if (!ops->get_sset_count)
976 return -EOPNOTSUPP;
978 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
979 return -EFAULT;
981 ret = ops->get_sset_count(dev, gstrings.string_set);
982 if (ret < 0)
983 return ret;
985 gstrings.len = ret;
987 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
988 if (!data)
989 return -ENOMEM;
991 if (ops->get_rx_ntuple) {
992 /* driver-specific filter grab */
993 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
994 goto copy;
997 /* default ethtool filter grab */
998 i = 0;
999 p = (char *)data;
1000 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
1001 sprintf(p, "Filter %d:\n", i);
1002 p += ETH_GSTRING_LEN;
1003 num_strings++;
1005 switch (fsc->fs.flow_type) {
1006 case TCP_V4_FLOW:
1007 sprintf(p, "\tFlow Type: TCP\n");
1008 p += ETH_GSTRING_LEN;
1009 num_strings++;
1010 break;
1011 case UDP_V4_FLOW:
1012 sprintf(p, "\tFlow Type: UDP\n");
1013 p += ETH_GSTRING_LEN;
1014 num_strings++;
1015 break;
1016 case SCTP_V4_FLOW:
1017 sprintf(p, "\tFlow Type: SCTP\n");
1018 p += ETH_GSTRING_LEN;
1019 num_strings++;
1020 break;
1021 case AH_ESP_V4_FLOW:
1022 sprintf(p, "\tFlow Type: AH ESP\n");
1023 p += ETH_GSTRING_LEN;
1024 num_strings++;
1025 break;
1026 case ESP_V4_FLOW:
1027 sprintf(p, "\tFlow Type: ESP\n");
1028 p += ETH_GSTRING_LEN;
1029 num_strings++;
1030 break;
1031 case IP_USER_FLOW:
1032 sprintf(p, "\tFlow Type: Raw IP\n");
1033 p += ETH_GSTRING_LEN;
1034 num_strings++;
1035 break;
1036 case IPV4_FLOW:
1037 sprintf(p, "\tFlow Type: IPv4\n");
1038 p += ETH_GSTRING_LEN;
1039 num_strings++;
1040 break;
1041 default:
1042 sprintf(p, "\tFlow Type: Unknown\n");
1043 p += ETH_GSTRING_LEN;
1044 num_strings++;
1045 goto unknown_filter;
1048 /* now the rest of the filters */
1049 switch (fsc->fs.flow_type) {
1050 case TCP_V4_FLOW:
1051 case UDP_V4_FLOW:
1052 case SCTP_V4_FLOW:
1053 sprintf(p, "\tSrc IP addr: 0x%x\n",
1054 fsc->fs.h_u.tcp_ip4_spec.ip4src);
1055 p += ETH_GSTRING_LEN;
1056 num_strings++;
1057 sprintf(p, "\tSrc IP mask: 0x%x\n",
1058 fsc->fs.m_u.tcp_ip4_spec.ip4src);
1059 p += ETH_GSTRING_LEN;
1060 num_strings++;
1061 sprintf(p, "\tDest IP addr: 0x%x\n",
1062 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
1063 p += ETH_GSTRING_LEN;
1064 num_strings++;
1065 sprintf(p, "\tDest IP mask: 0x%x\n",
1066 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
1067 p += ETH_GSTRING_LEN;
1068 num_strings++;
1069 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
1070 fsc->fs.h_u.tcp_ip4_spec.psrc,
1071 fsc->fs.m_u.tcp_ip4_spec.psrc);
1072 p += ETH_GSTRING_LEN;
1073 num_strings++;
1074 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
1075 fsc->fs.h_u.tcp_ip4_spec.pdst,
1076 fsc->fs.m_u.tcp_ip4_spec.pdst);
1077 p += ETH_GSTRING_LEN;
1078 num_strings++;
1079 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
1080 fsc->fs.h_u.tcp_ip4_spec.tos,
1081 fsc->fs.m_u.tcp_ip4_spec.tos);
1082 p += ETH_GSTRING_LEN;
1083 num_strings++;
1084 break;
1085 case AH_ESP_V4_FLOW:
1086 case ESP_V4_FLOW:
1087 sprintf(p, "\tSrc IP addr: 0x%x\n",
1088 fsc->fs.h_u.ah_ip4_spec.ip4src);
1089 p += ETH_GSTRING_LEN;
1090 num_strings++;
1091 sprintf(p, "\tSrc IP mask: 0x%x\n",
1092 fsc->fs.m_u.ah_ip4_spec.ip4src);
1093 p += ETH_GSTRING_LEN;
1094 num_strings++;
1095 sprintf(p, "\tDest IP addr: 0x%x\n",
1096 fsc->fs.h_u.ah_ip4_spec.ip4dst);
1097 p += ETH_GSTRING_LEN;
1098 num_strings++;
1099 sprintf(p, "\tDest IP mask: 0x%x\n",
1100 fsc->fs.m_u.ah_ip4_spec.ip4dst);
1101 p += ETH_GSTRING_LEN;
1102 num_strings++;
1103 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
1104 fsc->fs.h_u.ah_ip4_spec.spi,
1105 fsc->fs.m_u.ah_ip4_spec.spi);
1106 p += ETH_GSTRING_LEN;
1107 num_strings++;
1108 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
1109 fsc->fs.h_u.ah_ip4_spec.tos,
1110 fsc->fs.m_u.ah_ip4_spec.tos);
1111 p += ETH_GSTRING_LEN;
1112 num_strings++;
1113 break;
1114 case IP_USER_FLOW:
1115 sprintf(p, "\tSrc IP addr: 0x%x\n",
1116 fsc->fs.h_u.usr_ip4_spec.ip4src);
1117 p += ETH_GSTRING_LEN;
1118 num_strings++;
1119 sprintf(p, "\tSrc IP mask: 0x%x\n",
1120 fsc->fs.m_u.usr_ip4_spec.ip4src);
1121 p += ETH_GSTRING_LEN;
1122 num_strings++;
1123 sprintf(p, "\tDest IP addr: 0x%x\n",
1124 fsc->fs.h_u.usr_ip4_spec.ip4dst);
1125 p += ETH_GSTRING_LEN;
1126 num_strings++;
1127 sprintf(p, "\tDest IP mask: 0x%x\n",
1128 fsc->fs.m_u.usr_ip4_spec.ip4dst);
1129 p += ETH_GSTRING_LEN;
1130 num_strings++;
1131 break;
1132 case IPV4_FLOW:
1133 sprintf(p, "\tSrc IP addr: 0x%x\n",
1134 fsc->fs.h_u.usr_ip4_spec.ip4src);
1135 p += ETH_GSTRING_LEN;
1136 num_strings++;
1137 sprintf(p, "\tSrc IP mask: 0x%x\n",
1138 fsc->fs.m_u.usr_ip4_spec.ip4src);
1139 p += ETH_GSTRING_LEN;
1140 num_strings++;
1141 sprintf(p, "\tDest IP addr: 0x%x\n",
1142 fsc->fs.h_u.usr_ip4_spec.ip4dst);
1143 p += ETH_GSTRING_LEN;
1144 num_strings++;
1145 sprintf(p, "\tDest IP mask: 0x%x\n",
1146 fsc->fs.m_u.usr_ip4_spec.ip4dst);
1147 p += ETH_GSTRING_LEN;
1148 num_strings++;
1149 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
1150 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
1151 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
1152 p += ETH_GSTRING_LEN;
1153 num_strings++;
1154 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
1155 fsc->fs.h_u.usr_ip4_spec.tos,
1156 fsc->fs.m_u.usr_ip4_spec.tos);
1157 p += ETH_GSTRING_LEN;
1158 num_strings++;
1159 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
1160 fsc->fs.h_u.usr_ip4_spec.ip_ver,
1161 fsc->fs.m_u.usr_ip4_spec.ip_ver);
1162 p += ETH_GSTRING_LEN;
1163 num_strings++;
1164 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
1165 fsc->fs.h_u.usr_ip4_spec.proto,
1166 fsc->fs.m_u.usr_ip4_spec.proto);
1167 p += ETH_GSTRING_LEN;
1168 num_strings++;
1169 break;
1171 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
1172 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
1173 p += ETH_GSTRING_LEN;
1174 num_strings++;
1175 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
1176 p += ETH_GSTRING_LEN;
1177 num_strings++;
1178 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
1179 p += ETH_GSTRING_LEN;
1180 num_strings++;
1181 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
1182 sprintf(p, "\tAction: Drop\n");
1183 else
1184 sprintf(p, "\tAction: Direct to queue %d\n",
1185 fsc->fs.action);
1186 p += ETH_GSTRING_LEN;
1187 num_strings++;
1188 unknown_filter:
1189 i++;
1191 copy:
1192 /* indicate to userspace how many strings we actually have */
1193 gstrings.len = num_strings;
1194 ret = -EFAULT;
1195 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1196 goto out;
1197 useraddr += sizeof(gstrings);
1198 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1199 goto out;
1200 ret = 0;
1202 out:
1203 kfree(data);
1204 return ret;
1207 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1209 struct ethtool_regs regs;
1210 const struct ethtool_ops *ops = dev->ethtool_ops;
1211 void *regbuf;
1212 int reglen, ret;
1214 if (!ops->get_regs || !ops->get_regs_len)
1215 return -EOPNOTSUPP;
1217 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1218 return -EFAULT;
1220 reglen = ops->get_regs_len(dev);
1221 if (regs.len > reglen)
1222 regs.len = reglen;
1224 regbuf = vzalloc(reglen);
1225 if (!regbuf)
1226 return -ENOMEM;
1228 ops->get_regs(dev, &regs, regbuf);
1230 ret = -EFAULT;
1231 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1232 goto out;
1233 useraddr += offsetof(struct ethtool_regs, data);
1234 if (copy_to_user(useraddr, regbuf, regs.len))
1235 goto out;
1236 ret = 0;
1238 out:
1239 vfree(regbuf);
1240 return ret;
1243 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1245 struct ethtool_value reset;
1246 int ret;
1248 if (!dev->ethtool_ops->reset)
1249 return -EOPNOTSUPP;
1251 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1252 return -EFAULT;
1254 ret = dev->ethtool_ops->reset(dev, &reset.data);
1255 if (ret)
1256 return ret;
1258 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1259 return -EFAULT;
1260 return 0;
1263 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1265 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1267 if (!dev->ethtool_ops->get_wol)
1268 return -EOPNOTSUPP;
1270 dev->ethtool_ops->get_wol(dev, &wol);
1272 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1273 return -EFAULT;
1274 return 0;
1277 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1279 struct ethtool_wolinfo wol;
1281 if (!dev->ethtool_ops->set_wol)
1282 return -EOPNOTSUPP;
1284 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1285 return -EFAULT;
1287 return dev->ethtool_ops->set_wol(dev, &wol);
1290 static int ethtool_nway_reset(struct net_device *dev)
1292 if (!dev->ethtool_ops->nway_reset)
1293 return -EOPNOTSUPP;
1295 return dev->ethtool_ops->nway_reset(dev);
1298 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1300 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1302 if (!dev->ethtool_ops->get_link)
1303 return -EOPNOTSUPP;
1305 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1307 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1308 return -EFAULT;
1309 return 0;
1312 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1314 struct ethtool_eeprom eeprom;
1315 const struct ethtool_ops *ops = dev->ethtool_ops;
1316 void __user *userbuf = useraddr + sizeof(eeprom);
1317 u32 bytes_remaining;
1318 u8 *data;
1319 int ret = 0;
1321 if (!ops->get_eeprom || !ops->get_eeprom_len)
1322 return -EOPNOTSUPP;
1324 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1325 return -EFAULT;
1327 /* Check for wrap and zero */
1328 if (eeprom.offset + eeprom.len <= eeprom.offset)
1329 return -EINVAL;
1331 /* Check for exceeding total eeprom len */
1332 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1333 return -EINVAL;
1335 data = kmalloc(PAGE_SIZE, GFP_USER);
1336 if (!data)
1337 return -ENOMEM;
1339 bytes_remaining = eeprom.len;
1340 while (bytes_remaining > 0) {
1341 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1343 ret = ops->get_eeprom(dev, &eeprom, data);
1344 if (ret)
1345 break;
1346 if (copy_to_user(userbuf, data, eeprom.len)) {
1347 ret = -EFAULT;
1348 break;
1350 userbuf += eeprom.len;
1351 eeprom.offset += eeprom.len;
1352 bytes_remaining -= eeprom.len;
1355 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1356 eeprom.offset -= eeprom.len;
1357 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1358 ret = -EFAULT;
1360 kfree(data);
1361 return ret;
1364 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1366 struct ethtool_eeprom eeprom;
1367 const struct ethtool_ops *ops = dev->ethtool_ops;
1368 void __user *userbuf = useraddr + sizeof(eeprom);
1369 u32 bytes_remaining;
1370 u8 *data;
1371 int ret = 0;
1373 if (!ops->set_eeprom || !ops->get_eeprom_len)
1374 return -EOPNOTSUPP;
1376 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1377 return -EFAULT;
1379 /* Check for wrap and zero */
1380 if (eeprom.offset + eeprom.len <= eeprom.offset)
1381 return -EINVAL;
1383 /* Check for exceeding total eeprom len */
1384 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1385 return -EINVAL;
1387 data = kmalloc(PAGE_SIZE, GFP_USER);
1388 if (!data)
1389 return -ENOMEM;
1391 bytes_remaining = eeprom.len;
1392 while (bytes_remaining > 0) {
1393 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1395 if (copy_from_user(data, userbuf, eeprom.len)) {
1396 ret = -EFAULT;
1397 break;
1399 ret = ops->set_eeprom(dev, &eeprom, data);
1400 if (ret)
1401 break;
1402 userbuf += eeprom.len;
1403 eeprom.offset += eeprom.len;
1404 bytes_remaining -= eeprom.len;
1407 kfree(data);
1408 return ret;
1411 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1412 void __user *useraddr)
1414 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1416 if (!dev->ethtool_ops->get_coalesce)
1417 return -EOPNOTSUPP;
1419 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1421 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1422 return -EFAULT;
1423 return 0;
1426 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1427 void __user *useraddr)
1429 struct ethtool_coalesce coalesce;
1431 if (!dev->ethtool_ops->set_coalesce)
1432 return -EOPNOTSUPP;
1434 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1435 return -EFAULT;
1437 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1440 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1442 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1444 if (!dev->ethtool_ops->get_ringparam)
1445 return -EOPNOTSUPP;
1447 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1449 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1450 return -EFAULT;
1451 return 0;
1454 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1456 struct ethtool_ringparam ringparam;
1458 if (!dev->ethtool_ops->set_ringparam)
1459 return -EOPNOTSUPP;
1461 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1462 return -EFAULT;
1464 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1467 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1469 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1471 if (!dev->ethtool_ops->get_pauseparam)
1472 return -EOPNOTSUPP;
1474 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1476 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1477 return -EFAULT;
1478 return 0;
1481 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1483 struct ethtool_pauseparam pauseparam;
1485 if (!dev->ethtool_ops->set_pauseparam)
1486 return -EOPNOTSUPP;
1488 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1489 return -EFAULT;
1491 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1494 static int __ethtool_set_sg(struct net_device *dev, u32 data)
1496 int err;
1498 if (!dev->ethtool_ops->set_sg)
1499 return -EOPNOTSUPP;
1501 if (data && !(dev->features & NETIF_F_ALL_CSUM))
1502 return -EINVAL;
1504 if (!data && dev->ethtool_ops->set_tso) {
1505 err = dev->ethtool_ops->set_tso(dev, 0);
1506 if (err)
1507 return err;
1510 if (!data && dev->ethtool_ops->set_ufo) {
1511 err = dev->ethtool_ops->set_ufo(dev, 0);
1512 if (err)
1513 return err;
1515 return dev->ethtool_ops->set_sg(dev, data);
1518 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
1520 int err;
1522 if (!dev->ethtool_ops->set_tx_csum)
1523 return -EOPNOTSUPP;
1525 if (!data && dev->ethtool_ops->set_sg) {
1526 err = __ethtool_set_sg(dev, 0);
1527 if (err)
1528 return err;
1531 return dev->ethtool_ops->set_tx_csum(dev, data);
1534 static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
1536 if (!dev->ethtool_ops->set_rx_csum)
1537 return -EOPNOTSUPP;
1539 if (!data)
1540 dev->features &= ~NETIF_F_GRO;
1542 return dev->ethtool_ops->set_rx_csum(dev, data);
1545 static int __ethtool_set_tso(struct net_device *dev, u32 data)
1547 if (!dev->ethtool_ops->set_tso)
1548 return -EOPNOTSUPP;
1550 if (data && !(dev->features & NETIF_F_SG))
1551 return -EINVAL;
1553 return dev->ethtool_ops->set_tso(dev, data);
1556 static int __ethtool_set_ufo(struct net_device *dev, u32 data)
1558 if (!dev->ethtool_ops->set_ufo)
1559 return -EOPNOTSUPP;
1560 if (data && !(dev->features & NETIF_F_SG))
1561 return -EINVAL;
1562 if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
1563 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1564 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
1565 return -EINVAL;
1566 return dev->ethtool_ops->set_ufo(dev, data);
1569 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1571 struct ethtool_test test;
1572 const struct ethtool_ops *ops = dev->ethtool_ops;
1573 u64 *data;
1574 int ret, test_len;
1576 if (!ops->self_test || !ops->get_sset_count)
1577 return -EOPNOTSUPP;
1579 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1580 if (test_len < 0)
1581 return test_len;
1582 WARN_ON(test_len == 0);
1584 if (copy_from_user(&test, useraddr, sizeof(test)))
1585 return -EFAULT;
1587 test.len = test_len;
1588 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1589 if (!data)
1590 return -ENOMEM;
1592 ops->self_test(dev, &test, data);
1594 ret = -EFAULT;
1595 if (copy_to_user(useraddr, &test, sizeof(test)))
1596 goto out;
1597 useraddr += sizeof(test);
1598 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1599 goto out;
1600 ret = 0;
1602 out:
1603 kfree(data);
1604 return ret;
1607 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1609 struct ethtool_gstrings gstrings;
1610 u8 *data;
1611 int ret;
1613 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1614 return -EFAULT;
1616 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1617 if (ret < 0)
1618 return ret;
1620 gstrings.len = ret;
1622 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1623 if (!data)
1624 return -ENOMEM;
1626 __ethtool_get_strings(dev, gstrings.string_set, data);
1628 ret = -EFAULT;
1629 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1630 goto out;
1631 useraddr += sizeof(gstrings);
1632 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1633 goto out;
1634 ret = 0;
1636 out:
1637 kfree(data);
1638 return ret;
1641 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1643 struct ethtool_value id;
1645 if (!dev->ethtool_ops->phys_id)
1646 return -EOPNOTSUPP;
1648 if (copy_from_user(&id, useraddr, sizeof(id)))
1649 return -EFAULT;
1651 return dev->ethtool_ops->phys_id(dev, id.data);
1654 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1656 struct ethtool_stats stats;
1657 const struct ethtool_ops *ops = dev->ethtool_ops;
1658 u64 *data;
1659 int ret, n_stats;
1661 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1662 return -EOPNOTSUPP;
1664 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1665 if (n_stats < 0)
1666 return n_stats;
1667 WARN_ON(n_stats == 0);
1669 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1670 return -EFAULT;
1672 stats.n_stats = n_stats;
1673 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1674 if (!data)
1675 return -ENOMEM;
1677 ops->get_ethtool_stats(dev, &stats, data);
1679 ret = -EFAULT;
1680 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1681 goto out;
1682 useraddr += sizeof(stats);
1683 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1684 goto out;
1685 ret = 0;
1687 out:
1688 kfree(data);
1689 return ret;
1692 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1694 struct ethtool_perm_addr epaddr;
1696 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1697 return -EFAULT;
1699 if (epaddr.size < dev->addr_len)
1700 return -ETOOSMALL;
1701 epaddr.size = dev->addr_len;
1703 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1704 return -EFAULT;
1705 useraddr += sizeof(epaddr);
1706 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1707 return -EFAULT;
1708 return 0;
1711 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1712 u32 cmd, u32 (*actor)(struct net_device *))
1714 struct ethtool_value edata = { .cmd = cmd };
1716 if (!actor)
1717 return -EOPNOTSUPP;
1719 edata.data = actor(dev);
1721 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1722 return -EFAULT;
1723 return 0;
1726 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1727 void (*actor)(struct net_device *, u32))
1729 struct ethtool_value edata;
1731 if (!actor)
1732 return -EOPNOTSUPP;
1734 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1735 return -EFAULT;
1737 actor(dev, edata.data);
1738 return 0;
1741 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1742 int (*actor)(struct net_device *, u32))
1744 struct ethtool_value edata;
1746 if (!actor)
1747 return -EOPNOTSUPP;
1749 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1750 return -EFAULT;
1752 return actor(dev, edata.data);
1755 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1756 char __user *useraddr)
1758 struct ethtool_flash efl;
1760 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1761 return -EFAULT;
1763 if (!dev->ethtool_ops->flash_device)
1764 return -EOPNOTSUPP;
1766 return dev->ethtool_ops->flash_device(dev, &efl);
1769 /* The main entry point in this file. Called from net/core/dev.c */
1771 int dev_ethtool(struct net *net, struct ifreq *ifr)
1773 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1774 void __user *useraddr = ifr->ifr_data;
1775 u32 ethcmd;
1776 int rc;
1777 u32 old_features;
1779 if (!dev || !netif_device_present(dev))
1780 return -ENODEV;
1782 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1783 return -EFAULT;
1785 if (!dev->ethtool_ops) {
1786 /* ETHTOOL_GDRVINFO does not require any driver support.
1787 * It is also unprivileged and does not change anything,
1788 * so we can take a shortcut to it. */
1789 if (ethcmd == ETHTOOL_GDRVINFO)
1790 return ethtool_get_drvinfo(dev, useraddr);
1791 else
1792 return -EOPNOTSUPP;
1795 /* Allow some commands to be done by anyone */
1796 switch (ethcmd) {
1797 case ETHTOOL_GSET:
1798 case ETHTOOL_GDRVINFO:
1799 case ETHTOOL_GMSGLVL:
1800 case ETHTOOL_GCOALESCE:
1801 case ETHTOOL_GRINGPARAM:
1802 case ETHTOOL_GPAUSEPARAM:
1803 case ETHTOOL_GRXCSUM:
1804 case ETHTOOL_GTXCSUM:
1805 case ETHTOOL_GSG:
1806 case ETHTOOL_GSTRINGS:
1807 case ETHTOOL_GTSO:
1808 case ETHTOOL_GPERMADDR:
1809 case ETHTOOL_GUFO:
1810 case ETHTOOL_GGSO:
1811 case ETHTOOL_GGRO:
1812 case ETHTOOL_GFLAGS:
1813 case ETHTOOL_GPFLAGS:
1814 case ETHTOOL_GRXFH:
1815 case ETHTOOL_GRXRINGS:
1816 case ETHTOOL_GRXCLSRLCNT:
1817 case ETHTOOL_GRXCLSRULE:
1818 case ETHTOOL_GRXCLSRLALL:
1819 case ETHTOOL_GFEATURES:
1820 break;
1821 default:
1822 if (!capable(CAP_NET_ADMIN))
1823 return -EPERM;
1826 if (dev->ethtool_ops->begin) {
1827 rc = dev->ethtool_ops->begin(dev);
1828 if (rc < 0)
1829 return rc;
1831 old_features = dev->features;
1833 switch (ethcmd) {
1834 case ETHTOOL_GSET:
1835 rc = ethtool_get_settings(dev, useraddr);
1836 break;
1837 case ETHTOOL_SSET:
1838 rc = ethtool_set_settings(dev, useraddr);
1839 break;
1840 case ETHTOOL_GDRVINFO:
1841 rc = ethtool_get_drvinfo(dev, useraddr);
1842 break;
1843 case ETHTOOL_GREGS:
1844 rc = ethtool_get_regs(dev, useraddr);
1845 break;
1846 case ETHTOOL_GWOL:
1847 rc = ethtool_get_wol(dev, useraddr);
1848 break;
1849 case ETHTOOL_SWOL:
1850 rc = ethtool_set_wol(dev, useraddr);
1851 break;
1852 case ETHTOOL_GMSGLVL:
1853 rc = ethtool_get_value(dev, useraddr, ethcmd,
1854 dev->ethtool_ops->get_msglevel);
1855 break;
1856 case ETHTOOL_SMSGLVL:
1857 rc = ethtool_set_value_void(dev, useraddr,
1858 dev->ethtool_ops->set_msglevel);
1859 break;
1860 case ETHTOOL_NWAY_RST:
1861 rc = ethtool_nway_reset(dev);
1862 break;
1863 case ETHTOOL_GLINK:
1864 rc = ethtool_get_link(dev, useraddr);
1865 break;
1866 case ETHTOOL_GEEPROM:
1867 rc = ethtool_get_eeprom(dev, useraddr);
1868 break;
1869 case ETHTOOL_SEEPROM:
1870 rc = ethtool_set_eeprom(dev, useraddr);
1871 break;
1872 case ETHTOOL_GCOALESCE:
1873 rc = ethtool_get_coalesce(dev, useraddr);
1874 break;
1875 case ETHTOOL_SCOALESCE:
1876 rc = ethtool_set_coalesce(dev, useraddr);
1877 break;
1878 case ETHTOOL_GRINGPARAM:
1879 rc = ethtool_get_ringparam(dev, useraddr);
1880 break;
1881 case ETHTOOL_SRINGPARAM:
1882 rc = ethtool_set_ringparam(dev, useraddr);
1883 break;
1884 case ETHTOOL_GPAUSEPARAM:
1885 rc = ethtool_get_pauseparam(dev, useraddr);
1886 break;
1887 case ETHTOOL_SPAUSEPARAM:
1888 rc = ethtool_set_pauseparam(dev, useraddr);
1889 break;
1890 case ETHTOOL_TEST:
1891 rc = ethtool_self_test(dev, useraddr);
1892 break;
1893 case ETHTOOL_GSTRINGS:
1894 rc = ethtool_get_strings(dev, useraddr);
1895 break;
1896 case ETHTOOL_PHYS_ID:
1897 rc = ethtool_phys_id(dev, useraddr);
1898 break;
1899 case ETHTOOL_GSTATS:
1900 rc = ethtool_get_stats(dev, useraddr);
1901 break;
1902 case ETHTOOL_GPERMADDR:
1903 rc = ethtool_get_perm_addr(dev, useraddr);
1904 break;
1905 case ETHTOOL_GFLAGS:
1906 rc = ethtool_get_value(dev, useraddr, ethcmd,
1907 (dev->ethtool_ops->get_flags ?
1908 dev->ethtool_ops->get_flags :
1909 ethtool_op_get_flags));
1910 break;
1911 case ETHTOOL_SFLAGS:
1912 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1913 break;
1914 case ETHTOOL_GPFLAGS:
1915 rc = ethtool_get_value(dev, useraddr, ethcmd,
1916 dev->ethtool_ops->get_priv_flags);
1917 break;
1918 case ETHTOOL_SPFLAGS:
1919 rc = ethtool_set_value(dev, useraddr,
1920 dev->ethtool_ops->set_priv_flags);
1921 break;
1922 case ETHTOOL_GRXFH:
1923 case ETHTOOL_GRXRINGS:
1924 case ETHTOOL_GRXCLSRLCNT:
1925 case ETHTOOL_GRXCLSRULE:
1926 case ETHTOOL_GRXCLSRLALL:
1927 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1928 break;
1929 case ETHTOOL_SRXFH:
1930 case ETHTOOL_SRXCLSRLDEL:
1931 case ETHTOOL_SRXCLSRLINS:
1932 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1933 break;
1934 case ETHTOOL_FLASHDEV:
1935 rc = ethtool_flash_device(dev, useraddr);
1936 break;
1937 case ETHTOOL_RESET:
1938 rc = ethtool_reset(dev, useraddr);
1939 break;
1940 case ETHTOOL_SRXNTUPLE:
1941 rc = ethtool_set_rx_ntuple(dev, useraddr);
1942 break;
1943 case ETHTOOL_GRXNTUPLE:
1944 rc = ethtool_get_rx_ntuple(dev, useraddr);
1945 break;
1946 case ETHTOOL_GSSET_INFO:
1947 rc = ethtool_get_sset_info(dev, useraddr);
1948 break;
1949 case ETHTOOL_GRXFHINDIR:
1950 rc = ethtool_get_rxfh_indir(dev, useraddr);
1951 break;
1952 case ETHTOOL_SRXFHINDIR:
1953 rc = ethtool_set_rxfh_indir(dev, useraddr);
1954 break;
1955 case ETHTOOL_GFEATURES:
1956 rc = ethtool_get_features(dev, useraddr);
1957 break;
1958 case ETHTOOL_SFEATURES:
1959 rc = ethtool_set_features(dev, useraddr);
1960 break;
1961 case ETHTOOL_GTXCSUM:
1962 case ETHTOOL_GRXCSUM:
1963 case ETHTOOL_GSG:
1964 case ETHTOOL_GTSO:
1965 case ETHTOOL_GUFO:
1966 case ETHTOOL_GGSO:
1967 case ETHTOOL_GGRO:
1968 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1969 break;
1970 case ETHTOOL_STXCSUM:
1971 case ETHTOOL_SRXCSUM:
1972 case ETHTOOL_SSG:
1973 case ETHTOOL_STSO:
1974 case ETHTOOL_SUFO:
1975 case ETHTOOL_SGSO:
1976 case ETHTOOL_SGRO:
1977 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1978 break;
1979 default:
1980 rc = -EOPNOTSUPP;
1983 if (dev->ethtool_ops->complete)
1984 dev->ethtool_ops->complete(dev);
1986 if (old_features != dev->features)
1987 netdev_features_change(dev);
1989 return rc;