ethtool: Fix potential user buffer overflow for ETHTOOL_{G, S}RXFH
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / ethtool.c
blob5aef51eb3d1f5fd01c2d6766e3062476f43440da
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 <asm/uaccess.h>
23 * Some useful ethtool_ops methods that're device independent.
24 * If we find that all drivers want to do the same thing here,
25 * we can turn these into dev_() function calls.
28 u32 ethtool_op_get_link(struct net_device *dev)
30 return netif_carrier_ok(dev) ? 1 : 0;
33 u32 ethtool_op_get_rx_csum(struct net_device *dev)
35 return (dev->features & NETIF_F_ALL_CSUM) != 0;
37 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
39 u32 ethtool_op_get_tx_csum(struct net_device *dev)
41 return (dev->features & NETIF_F_ALL_CSUM) != 0;
43 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
45 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
47 if (data)
48 dev->features |= NETIF_F_IP_CSUM;
49 else
50 dev->features &= ~NETIF_F_IP_CSUM;
52 return 0;
55 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
57 if (data)
58 dev->features |= NETIF_F_HW_CSUM;
59 else
60 dev->features &= ~NETIF_F_HW_CSUM;
62 return 0;
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;
75 u32 ethtool_op_get_sg(struct net_device *dev)
77 return (dev->features & NETIF_F_SG) != 0;
80 int ethtool_op_set_sg(struct net_device *dev, u32 data)
82 if (data)
83 dev->features |= NETIF_F_SG;
84 else
85 dev->features &= ~NETIF_F_SG;
87 return 0;
90 u32 ethtool_op_get_tso(struct net_device *dev)
92 return (dev->features & NETIF_F_TSO) != 0;
95 int ethtool_op_set_tso(struct net_device *dev, u32 data)
97 if (data)
98 dev->features |= NETIF_F_TSO;
99 else
100 dev->features &= ~NETIF_F_TSO;
102 return 0;
105 u32 ethtool_op_get_ufo(struct net_device *dev)
107 return (dev->features & NETIF_F_UFO) != 0;
110 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
112 if (data)
113 dev->features |= NETIF_F_UFO;
114 else
115 dev->features &= ~NETIF_F_UFO;
116 return 0;
119 /* the following list of flags are the same as their associated
120 * NETIF_F_xxx values in include/linux/netdevice.h
122 static const u32 flags_dup_features =
123 ETH_FLAG_LRO;
125 u32 ethtool_op_get_flags(struct net_device *dev)
127 /* in the future, this function will probably contain additional
128 * handling for flags which are not so easily handled
129 * by a simple masking operation
132 return dev->features & flags_dup_features;
135 int ethtool_op_set_flags(struct net_device *dev, u32 data)
137 if (data & ETH_FLAG_LRO)
138 dev->features |= NETIF_F_LRO;
139 else
140 dev->features &= ~NETIF_F_LRO;
142 return 0;
145 /* Handlers for each ethtool command */
147 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
149 struct ethtool_cmd cmd = { ETHTOOL_GSET };
150 int err;
152 if (!dev->ethtool_ops->get_settings)
153 return -EOPNOTSUPP;
155 err = dev->ethtool_ops->get_settings(dev, &cmd);
156 if (err < 0)
157 return err;
159 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
160 return -EFAULT;
161 return 0;
164 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
166 struct ethtool_cmd cmd;
168 if (!dev->ethtool_ops->set_settings)
169 return -EOPNOTSUPP;
171 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
172 return -EFAULT;
174 return dev->ethtool_ops->set_settings(dev, &cmd);
177 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
179 struct ethtool_drvinfo info;
180 const struct ethtool_ops *ops = dev->ethtool_ops;
182 if (!ops->get_drvinfo)
183 return -EOPNOTSUPP;
185 memset(&info, 0, sizeof(info));
186 info.cmd = ETHTOOL_GDRVINFO;
187 ops->get_drvinfo(dev, &info);
189 if (ops->get_sset_count) {
190 int rc;
192 rc = ops->get_sset_count(dev, ETH_SS_TEST);
193 if (rc >= 0)
194 info.testinfo_len = rc;
195 rc = ops->get_sset_count(dev, ETH_SS_STATS);
196 if (rc >= 0)
197 info.n_stats = rc;
198 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
199 if (rc >= 0)
200 info.n_priv_flags = rc;
201 } else {
202 /* code path for obsolete hooks */
204 if (ops->self_test_count)
205 info.testinfo_len = ops->self_test_count(dev);
206 if (ops->get_stats_count)
207 info.n_stats = ops->get_stats_count(dev);
209 if (ops->get_regs_len)
210 info.regdump_len = ops->get_regs_len(dev);
211 if (ops->get_eeprom_len)
212 info.eedump_len = ops->get_eeprom_len(dev);
214 if (copy_to_user(useraddr, &info, sizeof(info)))
215 return -EFAULT;
216 return 0;
219 static int ethtool_set_rxnfc(struct net_device *dev,
220 u32 cmd, void __user *useraddr)
222 struct ethtool_rxnfc info;
223 size_t info_size = sizeof(info);
225 if (!dev->ethtool_ops->set_rxnfc)
226 return -EOPNOTSUPP;
228 /* struct ethtool_rxnfc was originally defined for
229 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
230 * members. User-space might still be using that
231 * definition. */
232 if (cmd == ETHTOOL_SRXFH)
233 info_size = (offsetof(struct ethtool_rxnfc, data) +
234 sizeof(info.data));
236 if (copy_from_user(&info, useraddr, info_size))
237 return -EFAULT;
239 return dev->ethtool_ops->set_rxnfc(dev, &info);
242 static int ethtool_get_rxnfc(struct net_device *dev,
243 u32 cmd, void __user *useraddr)
245 struct ethtool_rxnfc info;
246 size_t info_size = sizeof(info);
247 const struct ethtool_ops *ops = dev->ethtool_ops;
248 int ret;
249 void *rule_buf = NULL;
251 if (!ops->get_rxnfc)
252 return -EOPNOTSUPP;
254 /* struct ethtool_rxnfc was originally defined for
255 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
256 * members. User-space might still be using that
257 * definition. */
258 if (cmd == ETHTOOL_GRXFH)
259 info_size = (offsetof(struct ethtool_rxnfc, data) +
260 sizeof(info.data));
262 if (copy_from_user(&info, useraddr, info_size))
263 return -EFAULT;
265 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
266 if (info.rule_cnt > 0) {
267 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
268 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
269 GFP_USER);
270 if (!rule_buf)
271 return -ENOMEM;
275 ret = ops->get_rxnfc(dev, &info, rule_buf);
276 if (ret < 0)
277 goto err_out;
279 ret = -EFAULT;
280 if (copy_to_user(useraddr, &info, info_size))
281 goto err_out;
283 if (rule_buf) {
284 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
285 if (copy_to_user(useraddr, rule_buf,
286 info.rule_cnt * sizeof(u32)))
287 goto err_out;
289 ret = 0;
291 err_out:
292 kfree(rule_buf);
294 return ret;
297 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
299 struct ethtool_regs regs;
300 const struct ethtool_ops *ops = dev->ethtool_ops;
301 void *regbuf;
302 int reglen, ret;
304 if (!ops->get_regs || !ops->get_regs_len)
305 return -EOPNOTSUPP;
307 if (copy_from_user(&regs, useraddr, sizeof(regs)))
308 return -EFAULT;
310 reglen = ops->get_regs_len(dev);
311 if (regs.len > reglen)
312 regs.len = reglen;
314 regbuf = kmalloc(reglen, GFP_USER);
315 if (!regbuf)
316 return -ENOMEM;
318 ops->get_regs(dev, &regs, regbuf);
320 ret = -EFAULT;
321 if (copy_to_user(useraddr, &regs, sizeof(regs)))
322 goto out;
323 useraddr += offsetof(struct ethtool_regs, data);
324 if (copy_to_user(useraddr, regbuf, regs.len))
325 goto out;
326 ret = 0;
328 out:
329 kfree(regbuf);
330 return ret;
333 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
335 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
337 if (!dev->ethtool_ops->get_wol)
338 return -EOPNOTSUPP;
340 dev->ethtool_ops->get_wol(dev, &wol);
342 if (copy_to_user(useraddr, &wol, sizeof(wol)))
343 return -EFAULT;
344 return 0;
347 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
349 struct ethtool_wolinfo wol;
351 if (!dev->ethtool_ops->set_wol)
352 return -EOPNOTSUPP;
354 if (copy_from_user(&wol, useraddr, sizeof(wol)))
355 return -EFAULT;
357 return dev->ethtool_ops->set_wol(dev, &wol);
360 static int ethtool_nway_reset(struct net_device *dev)
362 if (!dev->ethtool_ops->nway_reset)
363 return -EOPNOTSUPP;
365 return dev->ethtool_ops->nway_reset(dev);
368 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
370 struct ethtool_eeprom eeprom;
371 const struct ethtool_ops *ops = dev->ethtool_ops;
372 void __user *userbuf = useraddr + sizeof(eeprom);
373 u32 bytes_remaining;
374 u8 *data;
375 int ret = 0;
377 if (!ops->get_eeprom || !ops->get_eeprom_len)
378 return -EOPNOTSUPP;
380 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
381 return -EFAULT;
383 /* Check for wrap and zero */
384 if (eeprom.offset + eeprom.len <= eeprom.offset)
385 return -EINVAL;
387 /* Check for exceeding total eeprom len */
388 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
389 return -EINVAL;
391 data = kmalloc(PAGE_SIZE, GFP_USER);
392 if (!data)
393 return -ENOMEM;
395 bytes_remaining = eeprom.len;
396 while (bytes_remaining > 0) {
397 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
399 ret = ops->get_eeprom(dev, &eeprom, data);
400 if (ret)
401 break;
402 if (copy_to_user(userbuf, data, eeprom.len)) {
403 ret = -EFAULT;
404 break;
406 userbuf += eeprom.len;
407 eeprom.offset += eeprom.len;
408 bytes_remaining -= eeprom.len;
411 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
412 eeprom.offset -= eeprom.len;
413 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
414 ret = -EFAULT;
416 kfree(data);
417 return ret;
420 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
422 struct ethtool_eeprom eeprom;
423 const struct ethtool_ops *ops = dev->ethtool_ops;
424 void __user *userbuf = useraddr + sizeof(eeprom);
425 u32 bytes_remaining;
426 u8 *data;
427 int ret = 0;
429 if (!ops->set_eeprom || !ops->get_eeprom_len)
430 return -EOPNOTSUPP;
432 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
433 return -EFAULT;
435 /* Check for wrap and zero */
436 if (eeprom.offset + eeprom.len <= eeprom.offset)
437 return -EINVAL;
439 /* Check for exceeding total eeprom len */
440 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
441 return -EINVAL;
443 data = kmalloc(PAGE_SIZE, GFP_USER);
444 if (!data)
445 return -ENOMEM;
447 bytes_remaining = eeprom.len;
448 while (bytes_remaining > 0) {
449 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
451 if (copy_from_user(data, userbuf, eeprom.len)) {
452 ret = -EFAULT;
453 break;
455 ret = ops->set_eeprom(dev, &eeprom, data);
456 if (ret)
457 break;
458 userbuf += eeprom.len;
459 eeprom.offset += eeprom.len;
460 bytes_remaining -= eeprom.len;
463 kfree(data);
464 return ret;
467 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
469 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
471 if (!dev->ethtool_ops->get_coalesce)
472 return -EOPNOTSUPP;
474 dev->ethtool_ops->get_coalesce(dev, &coalesce);
476 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
477 return -EFAULT;
478 return 0;
481 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
483 struct ethtool_coalesce coalesce;
485 if (!dev->ethtool_ops->set_coalesce)
486 return -EOPNOTSUPP;
488 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
489 return -EFAULT;
491 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
494 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
496 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
498 if (!dev->ethtool_ops->get_ringparam)
499 return -EOPNOTSUPP;
501 dev->ethtool_ops->get_ringparam(dev, &ringparam);
503 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
504 return -EFAULT;
505 return 0;
508 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
510 struct ethtool_ringparam ringparam;
512 if (!dev->ethtool_ops->set_ringparam)
513 return -EOPNOTSUPP;
515 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
516 return -EFAULT;
518 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
521 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
523 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
525 if (!dev->ethtool_ops->get_pauseparam)
526 return -EOPNOTSUPP;
528 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
530 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
531 return -EFAULT;
532 return 0;
535 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
537 struct ethtool_pauseparam pauseparam;
539 if (!dev->ethtool_ops->set_pauseparam)
540 return -EOPNOTSUPP;
542 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
543 return -EFAULT;
545 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
548 static int __ethtool_set_sg(struct net_device *dev, u32 data)
550 int err;
552 if (!data && dev->ethtool_ops->set_tso) {
553 err = dev->ethtool_ops->set_tso(dev, 0);
554 if (err)
555 return err;
558 if (!data && dev->ethtool_ops->set_ufo) {
559 err = dev->ethtool_ops->set_ufo(dev, 0);
560 if (err)
561 return err;
563 return dev->ethtool_ops->set_sg(dev, data);
566 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
568 struct ethtool_value edata;
569 int err;
571 if (!dev->ethtool_ops->set_tx_csum)
572 return -EOPNOTSUPP;
574 if (copy_from_user(&edata, useraddr, sizeof(edata)))
575 return -EFAULT;
577 if (!edata.data && dev->ethtool_ops->set_sg) {
578 err = __ethtool_set_sg(dev, 0);
579 if (err)
580 return err;
583 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
586 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
588 struct ethtool_value edata;
590 if (!dev->ethtool_ops->set_rx_csum)
591 return -EOPNOTSUPP;
593 if (copy_from_user(&edata, useraddr, sizeof(edata)))
594 return -EFAULT;
596 if (!edata.data && dev->ethtool_ops->set_sg)
597 dev->features &= ~NETIF_F_GRO;
599 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
602 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
604 struct ethtool_value edata;
606 if (!dev->ethtool_ops->set_sg)
607 return -EOPNOTSUPP;
609 if (copy_from_user(&edata, useraddr, sizeof(edata)))
610 return -EFAULT;
612 if (edata.data &&
613 !(dev->features & NETIF_F_ALL_CSUM))
614 return -EINVAL;
616 return __ethtool_set_sg(dev, edata.data);
619 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
621 struct ethtool_value edata;
623 if (!dev->ethtool_ops->set_tso)
624 return -EOPNOTSUPP;
626 if (copy_from_user(&edata, useraddr, sizeof(edata)))
627 return -EFAULT;
629 if (edata.data && !(dev->features & NETIF_F_SG))
630 return -EINVAL;
632 return dev->ethtool_ops->set_tso(dev, edata.data);
635 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
637 struct ethtool_value edata;
639 if (!dev->ethtool_ops->set_ufo)
640 return -EOPNOTSUPP;
641 if (copy_from_user(&edata, useraddr, sizeof(edata)))
642 return -EFAULT;
643 if (edata.data && !(dev->features & NETIF_F_SG))
644 return -EINVAL;
645 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
646 return -EINVAL;
647 return dev->ethtool_ops->set_ufo(dev, edata.data);
650 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
652 struct ethtool_value edata = { ETHTOOL_GGSO };
654 edata.data = dev->features & NETIF_F_GSO;
655 if (copy_to_user(useraddr, &edata, sizeof(edata)))
656 return -EFAULT;
657 return 0;
660 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
662 struct ethtool_value edata;
664 if (copy_from_user(&edata, useraddr, sizeof(edata)))
665 return -EFAULT;
666 if (edata.data)
667 dev->features |= NETIF_F_GSO;
668 else
669 dev->features &= ~NETIF_F_GSO;
670 return 0;
673 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
675 struct ethtool_value edata = { ETHTOOL_GGRO };
677 edata.data = dev->features & NETIF_F_GRO;
678 if (copy_to_user(useraddr, &edata, sizeof(edata)))
679 return -EFAULT;
680 return 0;
683 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
685 struct ethtool_value edata;
687 if (copy_from_user(&edata, useraddr, sizeof(edata)))
688 return -EFAULT;
690 if (edata.data) {
691 if (!dev->ethtool_ops->get_rx_csum ||
692 !dev->ethtool_ops->get_rx_csum(dev))
693 return -EINVAL;
694 dev->features |= NETIF_F_GRO;
695 } else
696 dev->features &= ~NETIF_F_GRO;
698 return 0;
701 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
703 struct ethtool_test test;
704 const struct ethtool_ops *ops = dev->ethtool_ops;
705 u64 *data;
706 int ret, test_len;
708 if (!ops->self_test)
709 return -EOPNOTSUPP;
710 if (!ops->get_sset_count && !ops->self_test_count)
711 return -EOPNOTSUPP;
713 if (ops->get_sset_count)
714 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
715 else
716 /* code path for obsolete hook */
717 test_len = ops->self_test_count(dev);
718 if (test_len < 0)
719 return test_len;
720 WARN_ON(test_len == 0);
722 if (copy_from_user(&test, useraddr, sizeof(test)))
723 return -EFAULT;
725 test.len = test_len;
726 data = kmalloc(test_len * sizeof(u64), GFP_USER);
727 if (!data)
728 return -ENOMEM;
730 ops->self_test(dev, &test, data);
732 ret = -EFAULT;
733 if (copy_to_user(useraddr, &test, sizeof(test)))
734 goto out;
735 useraddr += sizeof(test);
736 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
737 goto out;
738 ret = 0;
740 out:
741 kfree(data);
742 return ret;
745 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
747 struct ethtool_gstrings gstrings;
748 const struct ethtool_ops *ops = dev->ethtool_ops;
749 u8 *data;
750 int ret;
752 if (!ops->get_strings)
753 return -EOPNOTSUPP;
755 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
756 return -EFAULT;
758 if (ops->get_sset_count) {
759 ret = ops->get_sset_count(dev, gstrings.string_set);
760 if (ret < 0)
761 return ret;
763 gstrings.len = ret;
764 } else {
765 /* code path for obsolete hooks */
767 switch (gstrings.string_set) {
768 case ETH_SS_TEST:
769 if (!ops->self_test_count)
770 return -EOPNOTSUPP;
771 gstrings.len = ops->self_test_count(dev);
772 break;
773 case ETH_SS_STATS:
774 if (!ops->get_stats_count)
775 return -EOPNOTSUPP;
776 gstrings.len = ops->get_stats_count(dev);
777 break;
778 default:
779 return -EINVAL;
783 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
784 if (!data)
785 return -ENOMEM;
787 ops->get_strings(dev, gstrings.string_set, data);
789 ret = -EFAULT;
790 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
791 goto out;
792 useraddr += sizeof(gstrings);
793 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
794 goto out;
795 ret = 0;
797 out:
798 kfree(data);
799 return ret;
802 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
804 struct ethtool_value id;
806 if (!dev->ethtool_ops->phys_id)
807 return -EOPNOTSUPP;
809 if (copy_from_user(&id, useraddr, sizeof(id)))
810 return -EFAULT;
812 return dev->ethtool_ops->phys_id(dev, id.data);
815 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
817 struct ethtool_stats stats;
818 const struct ethtool_ops *ops = dev->ethtool_ops;
819 u64 *data;
820 int ret, n_stats;
822 if (!ops->get_ethtool_stats)
823 return -EOPNOTSUPP;
824 if (!ops->get_sset_count && !ops->get_stats_count)
825 return -EOPNOTSUPP;
827 if (ops->get_sset_count)
828 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
829 else
830 /* code path for obsolete hook */
831 n_stats = ops->get_stats_count(dev);
832 if (n_stats < 0)
833 return n_stats;
834 WARN_ON(n_stats == 0);
836 if (copy_from_user(&stats, useraddr, sizeof(stats)))
837 return -EFAULT;
839 stats.n_stats = n_stats;
840 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
841 if (!data)
842 return -ENOMEM;
844 ops->get_ethtool_stats(dev, &stats, data);
846 ret = -EFAULT;
847 if (copy_to_user(useraddr, &stats, sizeof(stats)))
848 goto out;
849 useraddr += sizeof(stats);
850 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
851 goto out;
852 ret = 0;
854 out:
855 kfree(data);
856 return ret;
859 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
861 struct ethtool_perm_addr epaddr;
863 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
864 return -EFAULT;
866 if (epaddr.size < dev->addr_len)
867 return -ETOOSMALL;
868 epaddr.size = dev->addr_len;
870 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
871 return -EFAULT;
872 useraddr += sizeof(epaddr);
873 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
874 return -EFAULT;
875 return 0;
878 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
879 u32 cmd, u32 (*actor)(struct net_device *))
881 struct ethtool_value edata = { cmd };
883 if (!actor)
884 return -EOPNOTSUPP;
886 edata.data = actor(dev);
888 if (copy_to_user(useraddr, &edata, sizeof(edata)))
889 return -EFAULT;
890 return 0;
893 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
894 void (*actor)(struct net_device *, u32))
896 struct ethtool_value edata;
898 if (!actor)
899 return -EOPNOTSUPP;
901 if (copy_from_user(&edata, useraddr, sizeof(edata)))
902 return -EFAULT;
904 actor(dev, edata.data);
905 return 0;
908 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
909 int (*actor)(struct net_device *, u32))
911 struct ethtool_value edata;
913 if (!actor)
914 return -EOPNOTSUPP;
916 if (copy_from_user(&edata, useraddr, sizeof(edata)))
917 return -EFAULT;
919 return actor(dev, edata.data);
922 static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
924 struct ethtool_flash efl;
926 if (copy_from_user(&efl, useraddr, sizeof(efl)))
927 return -EFAULT;
929 if (!dev->ethtool_ops->flash_device)
930 return -EOPNOTSUPP;
932 return dev->ethtool_ops->flash_device(dev, &efl);
935 /* The main entry point in this file. Called from net/core/dev.c */
937 int dev_ethtool(struct net *net, struct ifreq *ifr)
939 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
940 void __user *useraddr = ifr->ifr_data;
941 u32 ethcmd;
942 int rc;
943 unsigned long old_features;
945 if (!dev || !netif_device_present(dev))
946 return -ENODEV;
948 if (!dev->ethtool_ops)
949 return -EOPNOTSUPP;
951 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
952 return -EFAULT;
954 /* Allow some commands to be done by anyone */
955 switch(ethcmd) {
956 case ETHTOOL_GDRVINFO:
957 case ETHTOOL_GMSGLVL:
958 case ETHTOOL_GCOALESCE:
959 case ETHTOOL_GRINGPARAM:
960 case ETHTOOL_GPAUSEPARAM:
961 case ETHTOOL_GRXCSUM:
962 case ETHTOOL_GTXCSUM:
963 case ETHTOOL_GSG:
964 case ETHTOOL_GSTRINGS:
965 case ETHTOOL_GTSO:
966 case ETHTOOL_GPERMADDR:
967 case ETHTOOL_GUFO:
968 case ETHTOOL_GGSO:
969 case ETHTOOL_GFLAGS:
970 case ETHTOOL_GPFLAGS:
971 case ETHTOOL_GRXFH:
972 case ETHTOOL_GRXRINGS:
973 case ETHTOOL_GRXCLSRLCNT:
974 case ETHTOOL_GRXCLSRULE:
975 case ETHTOOL_GRXCLSRLALL:
976 break;
977 default:
978 if (!capable(CAP_NET_ADMIN))
979 return -EPERM;
982 if (dev->ethtool_ops->begin)
983 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
984 return rc;
986 old_features = dev->features;
988 switch (ethcmd) {
989 case ETHTOOL_GSET:
990 rc = ethtool_get_settings(dev, useraddr);
991 break;
992 case ETHTOOL_SSET:
993 rc = ethtool_set_settings(dev, useraddr);
994 break;
995 case ETHTOOL_GDRVINFO:
996 rc = ethtool_get_drvinfo(dev, useraddr);
997 break;
998 case ETHTOOL_GREGS:
999 rc = ethtool_get_regs(dev, useraddr);
1000 break;
1001 case ETHTOOL_GWOL:
1002 rc = ethtool_get_wol(dev, useraddr);
1003 break;
1004 case ETHTOOL_SWOL:
1005 rc = ethtool_set_wol(dev, useraddr);
1006 break;
1007 case ETHTOOL_GMSGLVL:
1008 rc = ethtool_get_value(dev, useraddr, ethcmd,
1009 dev->ethtool_ops->get_msglevel);
1010 break;
1011 case ETHTOOL_SMSGLVL:
1012 rc = ethtool_set_value_void(dev, useraddr,
1013 dev->ethtool_ops->set_msglevel);
1014 break;
1015 case ETHTOOL_NWAY_RST:
1016 rc = ethtool_nway_reset(dev);
1017 break;
1018 case ETHTOOL_GLINK:
1019 rc = ethtool_get_value(dev, useraddr, ethcmd,
1020 dev->ethtool_ops->get_link);
1021 break;
1022 case ETHTOOL_GEEPROM:
1023 rc = ethtool_get_eeprom(dev, useraddr);
1024 break;
1025 case ETHTOOL_SEEPROM:
1026 rc = ethtool_set_eeprom(dev, useraddr);
1027 break;
1028 case ETHTOOL_GCOALESCE:
1029 rc = ethtool_get_coalesce(dev, useraddr);
1030 break;
1031 case ETHTOOL_SCOALESCE:
1032 rc = ethtool_set_coalesce(dev, useraddr);
1033 break;
1034 case ETHTOOL_GRINGPARAM:
1035 rc = ethtool_get_ringparam(dev, useraddr);
1036 break;
1037 case ETHTOOL_SRINGPARAM:
1038 rc = ethtool_set_ringparam(dev, useraddr);
1039 break;
1040 case ETHTOOL_GPAUSEPARAM:
1041 rc = ethtool_get_pauseparam(dev, useraddr);
1042 break;
1043 case ETHTOOL_SPAUSEPARAM:
1044 rc = ethtool_set_pauseparam(dev, useraddr);
1045 break;
1046 case ETHTOOL_GRXCSUM:
1047 rc = ethtool_get_value(dev, useraddr, ethcmd,
1048 (dev->ethtool_ops->get_rx_csum ?
1049 dev->ethtool_ops->get_rx_csum :
1050 ethtool_op_get_rx_csum));
1051 break;
1052 case ETHTOOL_SRXCSUM:
1053 rc = ethtool_set_rx_csum(dev, useraddr);
1054 break;
1055 case ETHTOOL_GTXCSUM:
1056 rc = ethtool_get_value(dev, useraddr, ethcmd,
1057 (dev->ethtool_ops->get_tx_csum ?
1058 dev->ethtool_ops->get_tx_csum :
1059 ethtool_op_get_tx_csum));
1060 break;
1061 case ETHTOOL_STXCSUM:
1062 rc = ethtool_set_tx_csum(dev, useraddr);
1063 break;
1064 case ETHTOOL_GSG:
1065 rc = ethtool_get_value(dev, useraddr, ethcmd,
1066 (dev->ethtool_ops->get_sg ?
1067 dev->ethtool_ops->get_sg :
1068 ethtool_op_get_sg));
1069 break;
1070 case ETHTOOL_SSG:
1071 rc = ethtool_set_sg(dev, useraddr);
1072 break;
1073 case ETHTOOL_GTSO:
1074 rc = ethtool_get_value(dev, useraddr, ethcmd,
1075 (dev->ethtool_ops->get_tso ?
1076 dev->ethtool_ops->get_tso :
1077 ethtool_op_get_tso));
1078 break;
1079 case ETHTOOL_STSO:
1080 rc = ethtool_set_tso(dev, useraddr);
1081 break;
1082 case ETHTOOL_TEST:
1083 rc = ethtool_self_test(dev, useraddr);
1084 break;
1085 case ETHTOOL_GSTRINGS:
1086 rc = ethtool_get_strings(dev, useraddr);
1087 break;
1088 case ETHTOOL_PHYS_ID:
1089 rc = ethtool_phys_id(dev, useraddr);
1090 break;
1091 case ETHTOOL_GSTATS:
1092 rc = ethtool_get_stats(dev, useraddr);
1093 break;
1094 case ETHTOOL_GPERMADDR:
1095 rc = ethtool_get_perm_addr(dev, useraddr);
1096 break;
1097 case ETHTOOL_GUFO:
1098 rc = ethtool_get_value(dev, useraddr, ethcmd,
1099 (dev->ethtool_ops->get_ufo ?
1100 dev->ethtool_ops->get_ufo :
1101 ethtool_op_get_ufo));
1102 break;
1103 case ETHTOOL_SUFO:
1104 rc = ethtool_set_ufo(dev, useraddr);
1105 break;
1106 case ETHTOOL_GGSO:
1107 rc = ethtool_get_gso(dev, useraddr);
1108 break;
1109 case ETHTOOL_SGSO:
1110 rc = ethtool_set_gso(dev, useraddr);
1111 break;
1112 case ETHTOOL_GFLAGS:
1113 rc = ethtool_get_value(dev, useraddr, ethcmd,
1114 (dev->ethtool_ops->get_flags ?
1115 dev->ethtool_ops->get_flags :
1116 ethtool_op_get_flags));
1117 break;
1118 case ETHTOOL_SFLAGS:
1119 rc = ethtool_set_value(dev, useraddr,
1120 dev->ethtool_ops->set_flags);
1121 break;
1122 case ETHTOOL_GPFLAGS:
1123 rc = ethtool_get_value(dev, useraddr, ethcmd,
1124 dev->ethtool_ops->get_priv_flags);
1125 break;
1126 case ETHTOOL_SPFLAGS:
1127 rc = ethtool_set_value(dev, useraddr,
1128 dev->ethtool_ops->set_priv_flags);
1129 break;
1130 case ETHTOOL_GRXFH:
1131 case ETHTOOL_GRXRINGS:
1132 case ETHTOOL_GRXCLSRLCNT:
1133 case ETHTOOL_GRXCLSRULE:
1134 case ETHTOOL_GRXCLSRLALL:
1135 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1136 break;
1137 case ETHTOOL_SRXFH:
1138 case ETHTOOL_SRXCLSRLDEL:
1139 case ETHTOOL_SRXCLSRLINS:
1140 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1141 break;
1142 case ETHTOOL_GGRO:
1143 rc = ethtool_get_gro(dev, useraddr);
1144 break;
1145 case ETHTOOL_SGRO:
1146 rc = ethtool_set_gro(dev, useraddr);
1147 break;
1148 case ETHTOOL_FLASHDEV:
1149 rc = ethtool_flash_device(dev, useraddr);
1150 break;
1151 default:
1152 rc = -EOPNOTSUPP;
1155 if (dev->ethtool_ops->complete)
1156 dev->ethtool_ops->complete(dev);
1158 if (old_features != dev->features)
1159 netdev_features_change(dev);
1161 return rc;
1164 EXPORT_SYMBOL(ethtool_op_get_link);
1165 EXPORT_SYMBOL(ethtool_op_get_sg);
1166 EXPORT_SYMBOL(ethtool_op_get_tso);
1167 EXPORT_SYMBOL(ethtool_op_set_sg);
1168 EXPORT_SYMBOL(ethtool_op_set_tso);
1169 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1170 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1171 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1172 EXPORT_SYMBOL(ethtool_op_set_ufo);
1173 EXPORT_SYMBOL(ethtool_op_get_ufo);
1174 EXPORT_SYMBOL(ethtool_op_set_flags);
1175 EXPORT_SYMBOL(ethtool_op_get_flags);