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. We fall back to calling do_ioctl()
7 * for drivers which haven't been converted to ethtool_ops yet.
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/ethtool.h>
17 #include <linux/netdevice.h>
18 #include <asm/uaccess.h>
21 * Some useful ethtool_ops methods that're device independent.
22 * If we find that all drivers want to do the same thing here,
23 * we can turn these into dev_() function calls.
26 u32 ethtool_op_get_link(struct net_device *dev)
28 return netif_carrier_ok(dev) ? 1 : 0;
31 u32 ethtool_op_get_tx_csum(struct net_device *dev)
33 return (dev->features & NETIF_F_ALL_CSUM) != 0;
36 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
39 dev->features |= NETIF_F_IP_CSUM;
41 dev->features &= ~NETIF_F_IP_CSUM;
46 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
49 dev->features |= NETIF_F_HW_CSUM;
51 dev->features &= ~NETIF_F_HW_CSUM;
56 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
59 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
61 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
66 u32 ethtool_op_get_sg(struct net_device *dev)
68 return (dev->features & NETIF_F_SG) != 0;
71 int ethtool_op_set_sg(struct net_device *dev, u32 data)
74 dev->features |= NETIF_F_SG;
76 dev->features &= ~NETIF_F_SG;
81 u32 ethtool_op_get_tso(struct net_device *dev)
83 return (dev->features & NETIF_F_TSO) != 0;
86 int ethtool_op_set_tso(struct net_device *dev, u32 data)
89 dev->features |= NETIF_F_TSO;
91 dev->features &= ~NETIF_F_TSO;
96 int ethtool_op_get_perm_addr(struct net_device *dev, struct ethtool_perm_addr *addr, u8 *data)
98 unsigned char len = dev->addr_len;
99 if ( addr->size < len )
103 memcpy(data, dev->perm_addr, len);
108 u32 ethtool_op_get_ufo(struct net_device *dev)
110 return (dev->features & NETIF_F_UFO) != 0;
113 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
116 dev->features |= NETIF_F_UFO;
118 dev->features &= ~NETIF_F_UFO;
122 /* Handlers for each ethtool command */
124 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
126 struct ethtool_cmd cmd = { ETHTOOL_GSET };
129 if (!dev->ethtool_ops->get_settings)
132 err = dev->ethtool_ops->get_settings(dev, &cmd);
136 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
141 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
143 struct ethtool_cmd cmd;
145 if (!dev->ethtool_ops->set_settings)
148 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
151 return dev->ethtool_ops->set_settings(dev, &cmd);
154 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
156 struct ethtool_drvinfo info;
157 const struct ethtool_ops *ops = dev->ethtool_ops;
159 if (!ops->get_drvinfo)
162 memset(&info, 0, sizeof(info));
163 info.cmd = ETHTOOL_GDRVINFO;
164 ops->get_drvinfo(dev, &info);
166 if (ops->self_test_count)
167 info.testinfo_len = ops->self_test_count(dev);
168 if (ops->get_stats_count)
169 info.n_stats = ops->get_stats_count(dev);
170 if (ops->get_regs_len)
171 info.regdump_len = ops->get_regs_len(dev);
172 if (ops->get_eeprom_len)
173 info.eedump_len = ops->get_eeprom_len(dev);
175 if (copy_to_user(useraddr, &info, sizeof(info)))
180 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
182 struct ethtool_regs regs;
183 const struct ethtool_ops *ops = dev->ethtool_ops;
187 if (!ops->get_regs || !ops->get_regs_len)
190 if (copy_from_user(®s, useraddr, sizeof(regs)))
193 reglen = ops->get_regs_len(dev);
194 if (regs.len > reglen)
197 regbuf = kmalloc(reglen, GFP_USER);
201 ops->get_regs(dev, ®s, regbuf);
204 if (copy_to_user(useraddr, ®s, sizeof(regs)))
206 useraddr += offsetof(struct ethtool_regs, data);
207 if (copy_to_user(useraddr, regbuf, regs.len))
216 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
218 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
220 if (!dev->ethtool_ops->get_wol)
223 dev->ethtool_ops->get_wol(dev, &wol);
225 if (copy_to_user(useraddr, &wol, sizeof(wol)))
230 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
232 struct ethtool_wolinfo wol;
234 if (!dev->ethtool_ops->set_wol)
237 if (copy_from_user(&wol, useraddr, sizeof(wol)))
240 return dev->ethtool_ops->set_wol(dev, &wol);
243 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
245 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
247 if (!dev->ethtool_ops->get_msglevel)
250 edata.data = dev->ethtool_ops->get_msglevel(dev);
252 if (copy_to_user(useraddr, &edata, sizeof(edata)))
257 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
259 struct ethtool_value edata;
261 if (!dev->ethtool_ops->set_msglevel)
264 if (copy_from_user(&edata, useraddr, sizeof(edata)))
267 dev->ethtool_ops->set_msglevel(dev, edata.data);
271 static int ethtool_nway_reset(struct net_device *dev)
273 if (!dev->ethtool_ops->nway_reset)
276 return dev->ethtool_ops->nway_reset(dev);
279 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
281 struct ethtool_value edata = { ETHTOOL_GLINK };
283 if (!dev->ethtool_ops->get_link)
286 edata.data = dev->ethtool_ops->get_link(dev);
288 if (copy_to_user(useraddr, &edata, sizeof(edata)))
293 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
295 struct ethtool_eeprom eeprom;
296 const struct ethtool_ops *ops = dev->ethtool_ops;
300 if (!ops->get_eeprom || !ops->get_eeprom_len)
303 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
306 /* Check for wrap and zero */
307 if (eeprom.offset + eeprom.len <= eeprom.offset)
310 /* Check for exceeding total eeprom len */
311 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
314 data = kmalloc(eeprom.len, GFP_USER);
319 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
322 ret = ops->get_eeprom(dev, &eeprom, data);
327 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
329 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
338 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
340 struct ethtool_eeprom eeprom;
341 const struct ethtool_ops *ops = dev->ethtool_ops;
345 if (!ops->set_eeprom || !ops->get_eeprom_len)
348 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
351 /* Check for wrap and zero */
352 if (eeprom.offset + eeprom.len <= eeprom.offset)
355 /* Check for exceeding total eeprom len */
356 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
359 data = kmalloc(eeprom.len, GFP_USER);
364 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
367 ret = ops->set_eeprom(dev, &eeprom, data);
371 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
379 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
381 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
383 if (!dev->ethtool_ops->get_coalesce)
386 dev->ethtool_ops->get_coalesce(dev, &coalesce);
388 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
393 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
395 struct ethtool_coalesce coalesce;
397 if (!dev->ethtool_ops->set_coalesce)
400 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
403 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
406 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
408 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
410 if (!dev->ethtool_ops->get_ringparam)
413 dev->ethtool_ops->get_ringparam(dev, &ringparam);
415 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
420 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
422 struct ethtool_ringparam ringparam;
424 if (!dev->ethtool_ops->set_ringparam)
427 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
430 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
433 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
435 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
437 if (!dev->ethtool_ops->get_pauseparam)
440 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
442 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
447 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
449 struct ethtool_pauseparam pauseparam;
451 if (!dev->ethtool_ops->set_pauseparam)
454 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
457 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
460 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
462 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
464 if (!dev->ethtool_ops->get_rx_csum)
467 edata.data = dev->ethtool_ops->get_rx_csum(dev);
469 if (copy_to_user(useraddr, &edata, sizeof(edata)))
474 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
476 struct ethtool_value edata;
478 if (!dev->ethtool_ops->set_rx_csum)
481 if (copy_from_user(&edata, useraddr, sizeof(edata)))
484 dev->ethtool_ops->set_rx_csum(dev, edata.data);
488 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
490 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
492 if (!dev->ethtool_ops->get_tx_csum)
495 edata.data = dev->ethtool_ops->get_tx_csum(dev);
497 if (copy_to_user(useraddr, &edata, sizeof(edata)))
502 static int __ethtool_set_sg(struct net_device *dev, u32 data)
506 if (!data && dev->ethtool_ops->set_tso) {
507 err = dev->ethtool_ops->set_tso(dev, 0);
512 if (!data && dev->ethtool_ops->set_ufo) {
513 err = dev->ethtool_ops->set_ufo(dev, 0);
517 return dev->ethtool_ops->set_sg(dev, data);
520 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
522 struct ethtool_value edata;
525 if (!dev->ethtool_ops->set_tx_csum)
528 if (copy_from_user(&edata, useraddr, sizeof(edata)))
531 if (!edata.data && dev->ethtool_ops->set_sg) {
532 err = __ethtool_set_sg(dev, 0);
537 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
540 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
542 struct ethtool_value edata = { ETHTOOL_GSG };
544 if (!dev->ethtool_ops->get_sg)
547 edata.data = dev->ethtool_ops->get_sg(dev);
549 if (copy_to_user(useraddr, &edata, sizeof(edata)))
554 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
556 struct ethtool_value edata;
558 if (!dev->ethtool_ops->set_sg)
561 if (copy_from_user(&edata, useraddr, sizeof(edata)))
565 !(dev->features & NETIF_F_ALL_CSUM))
568 return __ethtool_set_sg(dev, edata.data);
571 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
573 struct ethtool_value edata = { ETHTOOL_GTSO };
575 if (!dev->ethtool_ops->get_tso)
578 edata.data = dev->ethtool_ops->get_tso(dev);
580 if (copy_to_user(useraddr, &edata, sizeof(edata)))
585 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
587 struct ethtool_value edata;
589 if (!dev->ethtool_ops->set_tso)
592 if (copy_from_user(&edata, useraddr, sizeof(edata)))
595 if (edata.data && !(dev->features & NETIF_F_SG))
598 return dev->ethtool_ops->set_tso(dev, edata.data);
601 static int ethtool_get_ufo(struct net_device *dev, char __user *useraddr)
603 struct ethtool_value edata = { ETHTOOL_GUFO };
605 if (!dev->ethtool_ops->get_ufo)
607 edata.data = dev->ethtool_ops->get_ufo(dev);
608 if (copy_to_user(useraddr, &edata, sizeof(edata)))
613 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
615 struct ethtool_value edata;
617 if (!dev->ethtool_ops->set_ufo)
619 if (copy_from_user(&edata, useraddr, sizeof(edata)))
621 if (edata.data && !(dev->features & NETIF_F_SG))
623 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
625 return dev->ethtool_ops->set_ufo(dev, edata.data);
628 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
630 struct ethtool_value edata = { ETHTOOL_GGSO };
632 edata.data = dev->features & NETIF_F_GSO;
633 if (copy_to_user(useraddr, &edata, sizeof(edata)))
638 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
640 struct ethtool_value edata;
642 if (copy_from_user(&edata, useraddr, sizeof(edata)))
645 dev->features |= NETIF_F_GSO;
647 dev->features &= ~NETIF_F_GSO;
651 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
653 struct ethtool_test test;
654 const struct ethtool_ops *ops = dev->ethtool_ops;
658 if (!ops->self_test || !ops->self_test_count)
661 if (copy_from_user(&test, useraddr, sizeof(test)))
664 test.len = ops->self_test_count(dev);
665 data = kmalloc(test.len * sizeof(u64), GFP_USER);
669 ops->self_test(dev, &test, data);
672 if (copy_to_user(useraddr, &test, sizeof(test)))
674 useraddr += sizeof(test);
675 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
684 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
686 struct ethtool_gstrings gstrings;
687 const struct ethtool_ops *ops = dev->ethtool_ops;
691 if (!ops->get_strings)
694 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
697 switch (gstrings.string_set) {
699 if (!ops->self_test_count)
701 gstrings.len = ops->self_test_count(dev);
704 if (!ops->get_stats_count)
706 gstrings.len = ops->get_stats_count(dev);
712 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
716 ops->get_strings(dev, gstrings.string_set, data);
719 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
721 useraddr += sizeof(gstrings);
722 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
731 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
733 struct ethtool_value id;
735 if (!dev->ethtool_ops->phys_id)
738 if (copy_from_user(&id, useraddr, sizeof(id)))
741 return dev->ethtool_ops->phys_id(dev, id.data);
744 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
746 struct ethtool_stats stats;
747 const struct ethtool_ops *ops = dev->ethtool_ops;
751 if (!ops->get_ethtool_stats || !ops->get_stats_count)
754 if (copy_from_user(&stats, useraddr, sizeof(stats)))
757 stats.n_stats = ops->get_stats_count(dev);
758 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
762 ops->get_ethtool_stats(dev, &stats, data);
765 if (copy_to_user(useraddr, &stats, sizeof(stats)))
767 useraddr += sizeof(stats);
768 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
777 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
779 struct ethtool_perm_addr epaddr;
783 if (!dev->ethtool_ops->get_perm_addr)
786 if (copy_from_user(&epaddr,useraddr,sizeof(epaddr)))
789 data = kmalloc(epaddr.size, GFP_USER);
793 ret = dev->ethtool_ops->get_perm_addr(dev,&epaddr,data);
798 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
800 useraddr += sizeof(epaddr);
801 if (copy_to_user(useraddr, data, epaddr.size))
810 /* The main entry point in this file. Called from net/core/dev.c */
812 int dev_ethtool(struct ifreq *ifr)
814 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
815 void __user *useraddr = ifr->ifr_data;
818 unsigned long old_features;
820 if (!dev || !netif_device_present(dev))
823 if (!dev->ethtool_ops)
826 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
829 /* Allow some commands to be done by anyone */
831 case ETHTOOL_GDRVINFO:
832 case ETHTOOL_GMSGLVL:
833 case ETHTOOL_GCOALESCE:
834 case ETHTOOL_GRINGPARAM:
835 case ETHTOOL_GPAUSEPARAM:
836 case ETHTOOL_GRXCSUM:
837 case ETHTOOL_GTXCSUM:
839 case ETHTOOL_GSTRINGS:
841 case ETHTOOL_GPERMADDR:
846 if (!capable(CAP_NET_ADMIN))
850 if (dev->ethtool_ops->begin)
851 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
854 old_features = dev->features;
858 rc = ethtool_get_settings(dev, useraddr);
861 rc = ethtool_set_settings(dev, useraddr);
863 case ETHTOOL_GDRVINFO:
864 rc = ethtool_get_drvinfo(dev, useraddr);
867 rc = ethtool_get_regs(dev, useraddr);
870 rc = ethtool_get_wol(dev, useraddr);
873 rc = ethtool_set_wol(dev, useraddr);
875 case ETHTOOL_GMSGLVL:
876 rc = ethtool_get_msglevel(dev, useraddr);
878 case ETHTOOL_SMSGLVL:
879 rc = ethtool_set_msglevel(dev, useraddr);
881 case ETHTOOL_NWAY_RST:
882 rc = ethtool_nway_reset(dev);
885 rc = ethtool_get_link(dev, useraddr);
887 case ETHTOOL_GEEPROM:
888 rc = ethtool_get_eeprom(dev, useraddr);
890 case ETHTOOL_SEEPROM:
891 rc = ethtool_set_eeprom(dev, useraddr);
893 case ETHTOOL_GCOALESCE:
894 rc = ethtool_get_coalesce(dev, useraddr);
896 case ETHTOOL_SCOALESCE:
897 rc = ethtool_set_coalesce(dev, useraddr);
899 case ETHTOOL_GRINGPARAM:
900 rc = ethtool_get_ringparam(dev, useraddr);
902 case ETHTOOL_SRINGPARAM:
903 rc = ethtool_set_ringparam(dev, useraddr);
905 case ETHTOOL_GPAUSEPARAM:
906 rc = ethtool_get_pauseparam(dev, useraddr);
908 case ETHTOOL_SPAUSEPARAM:
909 rc = ethtool_set_pauseparam(dev, useraddr);
911 case ETHTOOL_GRXCSUM:
912 rc = ethtool_get_rx_csum(dev, useraddr);
914 case ETHTOOL_SRXCSUM:
915 rc = ethtool_set_rx_csum(dev, useraddr);
917 case ETHTOOL_GTXCSUM:
918 rc = ethtool_get_tx_csum(dev, useraddr);
920 case ETHTOOL_STXCSUM:
921 rc = ethtool_set_tx_csum(dev, useraddr);
924 rc = ethtool_get_sg(dev, useraddr);
927 rc = ethtool_set_sg(dev, useraddr);
930 rc = ethtool_get_tso(dev, useraddr);
933 rc = ethtool_set_tso(dev, useraddr);
936 rc = ethtool_self_test(dev, useraddr);
938 case ETHTOOL_GSTRINGS:
939 rc = ethtool_get_strings(dev, useraddr);
941 case ETHTOOL_PHYS_ID:
942 rc = ethtool_phys_id(dev, useraddr);
945 rc = ethtool_get_stats(dev, useraddr);
947 case ETHTOOL_GPERMADDR:
948 rc = ethtool_get_perm_addr(dev, useraddr);
951 rc = ethtool_get_ufo(dev, useraddr);
954 rc = ethtool_set_ufo(dev, useraddr);
957 rc = ethtool_get_gso(dev, useraddr);
960 rc = ethtool_set_gso(dev, useraddr);
966 if (dev->ethtool_ops->complete)
967 dev->ethtool_ops->complete(dev);
969 if (old_features != dev->features)
970 netdev_features_change(dev);
975 /* Keep existing behaviour for the moment. */
976 if (!capable(CAP_NET_ADMIN))
980 return dev->do_ioctl(dev, ifr, SIOCETHTOOL);
984 EXPORT_SYMBOL(dev_ethtool);
985 EXPORT_SYMBOL(ethtool_op_get_link);
986 EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr);
987 EXPORT_SYMBOL(ethtool_op_get_sg);
988 EXPORT_SYMBOL(ethtool_op_get_tso);
989 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
990 EXPORT_SYMBOL(ethtool_op_set_sg);
991 EXPORT_SYMBOL(ethtool_op_set_tso);
992 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
993 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
994 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
995 EXPORT_SYMBOL(ethtool_op_set_ufo);
996 EXPORT_SYMBOL(ethtool_op_get_ufo);