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)
48 dev->features |= NETIF_F_IP_CSUM;
50 dev->features &= ~NETIF_F_IP_CSUM;
55 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
58 dev->features |= NETIF_F_HW_CSUM;
60 dev->features &= ~NETIF_F_HW_CSUM;
65 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
68 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
70 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
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)
83 dev->features |= NETIF_F_SG;
85 dev->features &= ~NETIF_F_SG;
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)
98 dev->features |= NETIF_F_TSO;
100 dev->features &= ~NETIF_F_TSO;
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)
113 dev->features |= NETIF_F_UFO;
115 dev->features &= ~NETIF_F_UFO;
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 | ETH_FLAG_NTUPLE);
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;
140 dev->features &= ~NETIF_F_LRO;
142 if (data & ETH_FLAG_NTUPLE)
143 dev->features |= NETIF_F_NTUPLE;
145 dev->features &= ~NETIF_F_NTUPLE;
150 void ethtool_ntuple_flush(struct net_device *dev)
152 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
154 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
155 list_del(&fsc->list);
158 dev->ethtool_ntuple_list.count = 0;
160 EXPORT_SYMBOL(ethtool_ntuple_flush);
162 /* Handlers for each ethtool command */
164 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
166 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
169 if (!dev->ethtool_ops->get_settings)
172 err = dev->ethtool_ops->get_settings(dev, &cmd);
176 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
181 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
183 struct ethtool_cmd cmd;
185 if (!dev->ethtool_ops->set_settings)
188 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
191 return dev->ethtool_ops->set_settings(dev, &cmd);
194 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
196 struct ethtool_drvinfo info;
197 const struct ethtool_ops *ops = dev->ethtool_ops;
199 if (!ops->get_drvinfo)
202 memset(&info, 0, sizeof(info));
203 info.cmd = ETHTOOL_GDRVINFO;
204 ops->get_drvinfo(dev, &info);
206 if (ops->get_sset_count) {
209 rc = ops->get_sset_count(dev, ETH_SS_TEST);
211 info.testinfo_len = rc;
212 rc = ops->get_sset_count(dev, ETH_SS_STATS);
215 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
217 info.n_priv_flags = rc;
219 if (ops->get_regs_len)
220 info.regdump_len = ops->get_regs_len(dev);
221 if (ops->get_eeprom_len)
222 info.eedump_len = ops->get_eeprom_len(dev);
224 if (copy_to_user(useraddr, &info, sizeof(info)))
229 static int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
231 struct ethtool_rxnfc cmd;
233 if (!dev->ethtool_ops->set_rxnfc)
236 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
239 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
242 static int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
244 struct ethtool_rxnfc info;
245 const struct ethtool_ops *ops = dev->ethtool_ops;
247 void *rule_buf = NULL;
252 if (copy_from_user(&info, useraddr, sizeof(info)))
255 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
256 if (info.rule_cnt > 0) {
257 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
264 ret = ops->get_rxnfc(dev, &info, rule_buf);
269 if (copy_to_user(useraddr, &info, sizeof(info)))
273 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
274 if (copy_to_user(useraddr, rule_buf,
275 info.rule_cnt * sizeof(u32)))
286 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
287 struct ethtool_rx_ntuple_flow_spec *spec,
288 struct ethtool_rx_ntuple_flow_spec_container *fsc)
291 /* don't add filters forever */
292 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
293 /* free the container */
298 /* Copy the whole filter over */
299 fsc->fs.flow_type = spec->flow_type;
300 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
301 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
303 fsc->fs.vlan_tag = spec->vlan_tag;
304 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
305 fsc->fs.data = spec->data;
306 fsc->fs.data_mask = spec->data_mask;
307 fsc->fs.action = spec->action;
309 /* add to the list */
310 list_add_tail_rcu(&fsc->list, &list->list);
314 static int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
316 struct ethtool_rx_ntuple cmd;
317 const struct ethtool_ops *ops = dev->ethtool_ops;
318 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
321 if (!ops->set_rx_ntuple)
324 if (!(dev->features & NETIF_F_NTUPLE))
327 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
331 * Cache filter in dev struct for GET operation only if
332 * the underlying driver doesn't have its own GET operation, and
333 * only if the filter was added successfully. First make sure we
334 * can allocate the filter, then continue if successful.
336 if (!ops->get_rx_ntuple) {
337 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
342 ret = ops->set_rx_ntuple(dev, &cmd);
348 if (!ops->get_rx_ntuple)
349 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
354 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
356 struct ethtool_gstrings gstrings;
357 const struct ethtool_ops *ops = dev->ethtool_ops;
358 struct ethtool_rx_ntuple_flow_spec_container *fsc;
361 int ret, i, num_strings = 0;
363 if (!ops->get_sset_count)
366 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
369 ret = ops->get_sset_count(dev, gstrings.string_set);
375 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
379 if (ops->get_rx_ntuple) {
380 /* driver-specific filter grab */
381 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
385 /* default ethtool filter grab */
388 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
389 sprintf(p, "Filter %d:\n", i);
390 p += ETH_GSTRING_LEN;
393 switch (fsc->fs.flow_type) {
395 sprintf(p, "\tFlow Type: TCP\n");
396 p += ETH_GSTRING_LEN;
400 sprintf(p, "\tFlow Type: UDP\n");
401 p += ETH_GSTRING_LEN;
405 sprintf(p, "\tFlow Type: SCTP\n");
406 p += ETH_GSTRING_LEN;
410 sprintf(p, "\tFlow Type: AH ESP\n");
411 p += ETH_GSTRING_LEN;
415 sprintf(p, "\tFlow Type: ESP\n");
416 p += ETH_GSTRING_LEN;
420 sprintf(p, "\tFlow Type: Raw IP\n");
421 p += ETH_GSTRING_LEN;
425 sprintf(p, "\tFlow Type: IPv4\n");
426 p += ETH_GSTRING_LEN;
430 sprintf(p, "\tFlow Type: Unknown\n");
431 p += ETH_GSTRING_LEN;
436 /* now the rest of the filters */
437 switch (fsc->fs.flow_type) {
441 sprintf(p, "\tSrc IP addr: 0x%x\n",
442 fsc->fs.h_u.tcp_ip4_spec.ip4src);
443 p += ETH_GSTRING_LEN;
445 sprintf(p, "\tSrc IP mask: 0x%x\n",
446 fsc->fs.m_u.tcp_ip4_spec.ip4src);
447 p += ETH_GSTRING_LEN;
449 sprintf(p, "\tDest IP addr: 0x%x\n",
450 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
451 p += ETH_GSTRING_LEN;
453 sprintf(p, "\tDest IP mask: 0x%x\n",
454 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
455 p += ETH_GSTRING_LEN;
457 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
458 fsc->fs.h_u.tcp_ip4_spec.psrc,
459 fsc->fs.m_u.tcp_ip4_spec.psrc);
460 p += ETH_GSTRING_LEN;
462 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
463 fsc->fs.h_u.tcp_ip4_spec.pdst,
464 fsc->fs.m_u.tcp_ip4_spec.pdst);
465 p += ETH_GSTRING_LEN;
467 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
468 fsc->fs.h_u.tcp_ip4_spec.tos,
469 fsc->fs.m_u.tcp_ip4_spec.tos);
470 p += ETH_GSTRING_LEN;
475 sprintf(p, "\tSrc IP addr: 0x%x\n",
476 fsc->fs.h_u.ah_ip4_spec.ip4src);
477 p += ETH_GSTRING_LEN;
479 sprintf(p, "\tSrc IP mask: 0x%x\n",
480 fsc->fs.m_u.ah_ip4_spec.ip4src);
481 p += ETH_GSTRING_LEN;
483 sprintf(p, "\tDest IP addr: 0x%x\n",
484 fsc->fs.h_u.ah_ip4_spec.ip4dst);
485 p += ETH_GSTRING_LEN;
487 sprintf(p, "\tDest IP mask: 0x%x\n",
488 fsc->fs.m_u.ah_ip4_spec.ip4dst);
489 p += ETH_GSTRING_LEN;
491 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
492 fsc->fs.h_u.ah_ip4_spec.spi,
493 fsc->fs.m_u.ah_ip4_spec.spi);
494 p += ETH_GSTRING_LEN;
496 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
497 fsc->fs.h_u.ah_ip4_spec.tos,
498 fsc->fs.m_u.ah_ip4_spec.tos);
499 p += ETH_GSTRING_LEN;
503 sprintf(p, "\tSrc IP addr: 0x%x\n",
504 fsc->fs.h_u.raw_ip4_spec.ip4src);
505 p += ETH_GSTRING_LEN;
507 sprintf(p, "\tSrc IP mask: 0x%x\n",
508 fsc->fs.m_u.raw_ip4_spec.ip4src);
509 p += ETH_GSTRING_LEN;
511 sprintf(p, "\tDest IP addr: 0x%x\n",
512 fsc->fs.h_u.raw_ip4_spec.ip4dst);
513 p += ETH_GSTRING_LEN;
515 sprintf(p, "\tDest IP mask: 0x%x\n",
516 fsc->fs.m_u.raw_ip4_spec.ip4dst);
517 p += ETH_GSTRING_LEN;
521 sprintf(p, "\tSrc IP addr: 0x%x\n",
522 fsc->fs.h_u.usr_ip4_spec.ip4src);
523 p += ETH_GSTRING_LEN;
525 sprintf(p, "\tSrc IP mask: 0x%x\n",
526 fsc->fs.m_u.usr_ip4_spec.ip4src);
527 p += ETH_GSTRING_LEN;
529 sprintf(p, "\tDest IP addr: 0x%x\n",
530 fsc->fs.h_u.usr_ip4_spec.ip4dst);
531 p += ETH_GSTRING_LEN;
533 sprintf(p, "\tDest IP mask: 0x%x\n",
534 fsc->fs.m_u.usr_ip4_spec.ip4dst);
535 p += ETH_GSTRING_LEN;
537 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
538 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
539 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
540 p += ETH_GSTRING_LEN;
542 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
543 fsc->fs.h_u.usr_ip4_spec.tos,
544 fsc->fs.m_u.usr_ip4_spec.tos);
545 p += ETH_GSTRING_LEN;
547 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
548 fsc->fs.h_u.usr_ip4_spec.ip_ver,
549 fsc->fs.m_u.usr_ip4_spec.ip_ver);
550 p += ETH_GSTRING_LEN;
552 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
553 fsc->fs.h_u.usr_ip4_spec.proto,
554 fsc->fs.m_u.usr_ip4_spec.proto);
555 p += ETH_GSTRING_LEN;
559 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
560 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
561 p += ETH_GSTRING_LEN;
563 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
564 p += ETH_GSTRING_LEN;
566 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
567 p += ETH_GSTRING_LEN;
569 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
570 sprintf(p, "\tAction: Drop\n");
572 sprintf(p, "\tAction: Direct to queue %d\n",
574 p += ETH_GSTRING_LEN;
580 /* indicate to userspace how many strings we actually have */
581 gstrings.len = num_strings;
583 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
585 useraddr += sizeof(gstrings);
586 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
595 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
597 struct ethtool_regs regs;
598 const struct ethtool_ops *ops = dev->ethtool_ops;
602 if (!ops->get_regs || !ops->get_regs_len)
605 if (copy_from_user(®s, useraddr, sizeof(regs)))
608 reglen = ops->get_regs_len(dev);
609 if (regs.len > reglen)
612 regbuf = kmalloc(reglen, GFP_USER);
616 ops->get_regs(dev, ®s, regbuf);
619 if (copy_to_user(useraddr, ®s, sizeof(regs)))
621 useraddr += offsetof(struct ethtool_regs, data);
622 if (copy_to_user(useraddr, regbuf, regs.len))
631 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
633 struct ethtool_value reset;
636 if (!dev->ethtool_ops->reset)
639 if (copy_from_user(&reset, useraddr, sizeof(reset)))
642 /* Clear ethtool n-tuple list */
643 ethtool_ntuple_flush(dev);
645 ret = dev->ethtool_ops->reset(dev, &reset.data);
649 if (copy_to_user(useraddr, &reset, sizeof(reset)))
654 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
656 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
658 if (!dev->ethtool_ops->get_wol)
661 dev->ethtool_ops->get_wol(dev, &wol);
663 if (copy_to_user(useraddr, &wol, sizeof(wol)))
668 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
670 struct ethtool_wolinfo wol;
672 if (!dev->ethtool_ops->set_wol)
675 if (copy_from_user(&wol, useraddr, sizeof(wol)))
678 return dev->ethtool_ops->set_wol(dev, &wol);
681 static int ethtool_nway_reset(struct net_device *dev)
683 if (!dev->ethtool_ops->nway_reset)
686 return dev->ethtool_ops->nway_reset(dev);
689 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
691 struct ethtool_eeprom eeprom;
692 const struct ethtool_ops *ops = dev->ethtool_ops;
693 void __user *userbuf = useraddr + sizeof(eeprom);
698 if (!ops->get_eeprom || !ops->get_eeprom_len)
701 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
704 /* Check for wrap and zero */
705 if (eeprom.offset + eeprom.len <= eeprom.offset)
708 /* Check for exceeding total eeprom len */
709 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
712 data = kmalloc(PAGE_SIZE, GFP_USER);
716 bytes_remaining = eeprom.len;
717 while (bytes_remaining > 0) {
718 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
720 ret = ops->get_eeprom(dev, &eeprom, data);
723 if (copy_to_user(userbuf, data, eeprom.len)) {
727 userbuf += eeprom.len;
728 eeprom.offset += eeprom.len;
729 bytes_remaining -= eeprom.len;
732 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
733 eeprom.offset -= eeprom.len;
734 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
741 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
743 struct ethtool_eeprom eeprom;
744 const struct ethtool_ops *ops = dev->ethtool_ops;
745 void __user *userbuf = useraddr + sizeof(eeprom);
750 if (!ops->set_eeprom || !ops->get_eeprom_len)
753 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
756 /* Check for wrap and zero */
757 if (eeprom.offset + eeprom.len <= eeprom.offset)
760 /* Check for exceeding total eeprom len */
761 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
764 data = kmalloc(PAGE_SIZE, GFP_USER);
768 bytes_remaining = eeprom.len;
769 while (bytes_remaining > 0) {
770 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
772 if (copy_from_user(data, userbuf, eeprom.len)) {
776 ret = ops->set_eeprom(dev, &eeprom, data);
779 userbuf += eeprom.len;
780 eeprom.offset += eeprom.len;
781 bytes_remaining -= eeprom.len;
788 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
790 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
792 if (!dev->ethtool_ops->get_coalesce)
795 dev->ethtool_ops->get_coalesce(dev, &coalesce);
797 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
802 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
804 struct ethtool_coalesce coalesce;
806 if (!dev->ethtool_ops->set_coalesce)
809 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
812 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
815 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
817 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
819 if (!dev->ethtool_ops->get_ringparam)
822 dev->ethtool_ops->get_ringparam(dev, &ringparam);
824 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
829 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
831 struct ethtool_ringparam ringparam;
833 if (!dev->ethtool_ops->set_ringparam)
836 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
839 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
842 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
844 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
846 if (!dev->ethtool_ops->get_pauseparam)
849 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
851 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
856 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
858 struct ethtool_pauseparam pauseparam;
860 if (!dev->ethtool_ops->set_pauseparam)
863 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
866 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
869 static int __ethtool_set_sg(struct net_device *dev, u32 data)
873 if (!data && dev->ethtool_ops->set_tso) {
874 err = dev->ethtool_ops->set_tso(dev, 0);
879 if (!data && dev->ethtool_ops->set_ufo) {
880 err = dev->ethtool_ops->set_ufo(dev, 0);
884 return dev->ethtool_ops->set_sg(dev, data);
887 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
889 struct ethtool_value edata;
892 if (!dev->ethtool_ops->set_tx_csum)
895 if (copy_from_user(&edata, useraddr, sizeof(edata)))
898 if (!edata.data && dev->ethtool_ops->set_sg) {
899 err = __ethtool_set_sg(dev, 0);
904 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
907 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
909 struct ethtool_value edata;
911 if (!dev->ethtool_ops->set_rx_csum)
914 if (copy_from_user(&edata, useraddr, sizeof(edata)))
917 if (!edata.data && dev->ethtool_ops->set_sg)
918 dev->features &= ~NETIF_F_GRO;
920 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
923 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
925 struct ethtool_value edata;
927 if (!dev->ethtool_ops->set_sg)
930 if (copy_from_user(&edata, useraddr, sizeof(edata)))
934 !(dev->features & NETIF_F_ALL_CSUM))
937 return __ethtool_set_sg(dev, edata.data);
940 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
942 struct ethtool_value edata;
944 if (!dev->ethtool_ops->set_tso)
947 if (copy_from_user(&edata, useraddr, sizeof(edata)))
950 if (edata.data && !(dev->features & NETIF_F_SG))
953 return dev->ethtool_ops->set_tso(dev, edata.data);
956 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
958 struct ethtool_value edata;
960 if (!dev->ethtool_ops->set_ufo)
962 if (copy_from_user(&edata, useraddr, sizeof(edata)))
964 if (edata.data && !(dev->features & NETIF_F_SG))
966 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
968 return dev->ethtool_ops->set_ufo(dev, edata.data);
971 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
973 struct ethtool_value edata = { ETHTOOL_GGSO };
975 edata.data = dev->features & NETIF_F_GSO;
976 if (copy_to_user(useraddr, &edata, sizeof(edata)))
981 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
983 struct ethtool_value edata;
985 if (copy_from_user(&edata, useraddr, sizeof(edata)))
988 dev->features |= NETIF_F_GSO;
990 dev->features &= ~NETIF_F_GSO;
994 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
996 struct ethtool_value edata = { ETHTOOL_GGRO };
998 edata.data = dev->features & NETIF_F_GRO;
999 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1004 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1006 struct ethtool_value edata;
1008 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1012 if (!dev->ethtool_ops->get_rx_csum ||
1013 !dev->ethtool_ops->get_rx_csum(dev))
1015 dev->features |= NETIF_F_GRO;
1017 dev->features &= ~NETIF_F_GRO;
1022 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1024 struct ethtool_test test;
1025 const struct ethtool_ops *ops = dev->ethtool_ops;
1029 if (!ops->self_test || !ops->get_sset_count)
1032 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1035 WARN_ON(test_len == 0);
1037 if (copy_from_user(&test, useraddr, sizeof(test)))
1040 test.len = test_len;
1041 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1045 ops->self_test(dev, &test, data);
1048 if (copy_to_user(useraddr, &test, sizeof(test)))
1050 useraddr += sizeof(test);
1051 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1060 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1062 struct ethtool_gstrings gstrings;
1063 const struct ethtool_ops *ops = dev->ethtool_ops;
1067 if (!ops->get_strings || !ops->get_sset_count)
1070 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1073 ret = ops->get_sset_count(dev, gstrings.string_set);
1079 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1083 ops->get_strings(dev, gstrings.string_set, data);
1086 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1088 useraddr += sizeof(gstrings);
1089 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1098 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1100 struct ethtool_value id;
1102 if (!dev->ethtool_ops->phys_id)
1105 if (copy_from_user(&id, useraddr, sizeof(id)))
1108 return dev->ethtool_ops->phys_id(dev, id.data);
1111 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1113 struct ethtool_stats stats;
1114 const struct ethtool_ops *ops = dev->ethtool_ops;
1118 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1121 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1124 WARN_ON(n_stats == 0);
1126 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1129 stats.n_stats = n_stats;
1130 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1134 ops->get_ethtool_stats(dev, &stats, data);
1137 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1139 useraddr += sizeof(stats);
1140 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1149 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1151 struct ethtool_perm_addr epaddr;
1153 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1156 if (epaddr.size < dev->addr_len)
1158 epaddr.size = dev->addr_len;
1160 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1162 useraddr += sizeof(epaddr);
1163 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1168 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1169 u32 cmd, u32 (*actor)(struct net_device *))
1171 struct ethtool_value edata = { .cmd = cmd };
1176 edata.data = actor(dev);
1178 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1183 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1184 void (*actor)(struct net_device *, u32))
1186 struct ethtool_value edata;
1191 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1194 actor(dev, edata.data);
1198 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1199 int (*actor)(struct net_device *, u32))
1201 struct ethtool_value edata;
1206 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1209 return actor(dev, edata.data);
1212 static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1214 struct ethtool_flash efl;
1216 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1219 if (!dev->ethtool_ops->flash_device)
1222 return dev->ethtool_ops->flash_device(dev, &efl);
1225 /* The main entry point in this file. Called from net/core/dev.c */
1227 int dev_ethtool(struct net *net, struct ifreq *ifr)
1229 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1230 void __user *useraddr = ifr->ifr_data;
1233 unsigned long old_features;
1235 if (!dev || !netif_device_present(dev))
1238 if (!dev->ethtool_ops)
1241 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1244 /* Allow some commands to be done by anyone */
1246 case ETHTOOL_GDRVINFO:
1247 case ETHTOOL_GMSGLVL:
1248 case ETHTOOL_GCOALESCE:
1249 case ETHTOOL_GRINGPARAM:
1250 case ETHTOOL_GPAUSEPARAM:
1251 case ETHTOOL_GRXCSUM:
1252 case ETHTOOL_GTXCSUM:
1254 case ETHTOOL_GSTRINGS:
1256 case ETHTOOL_GPERMADDR:
1259 case ETHTOOL_GFLAGS:
1260 case ETHTOOL_GPFLAGS:
1262 case ETHTOOL_GRXRINGS:
1263 case ETHTOOL_GRXCLSRLCNT:
1264 case ETHTOOL_GRXCLSRULE:
1265 case ETHTOOL_GRXCLSRLALL:
1268 if (!capable(CAP_NET_ADMIN))
1272 if (dev->ethtool_ops->begin)
1273 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1276 old_features = dev->features;
1280 rc = ethtool_get_settings(dev, useraddr);
1283 rc = ethtool_set_settings(dev, useraddr);
1285 case ETHTOOL_GDRVINFO:
1286 rc = ethtool_get_drvinfo(dev, useraddr);
1289 rc = ethtool_get_regs(dev, useraddr);
1292 rc = ethtool_get_wol(dev, useraddr);
1295 rc = ethtool_set_wol(dev, useraddr);
1297 case ETHTOOL_GMSGLVL:
1298 rc = ethtool_get_value(dev, useraddr, ethcmd,
1299 dev->ethtool_ops->get_msglevel);
1301 case ETHTOOL_SMSGLVL:
1302 rc = ethtool_set_value_void(dev, useraddr,
1303 dev->ethtool_ops->set_msglevel);
1305 case ETHTOOL_NWAY_RST:
1306 rc = ethtool_nway_reset(dev);
1309 rc = ethtool_get_value(dev, useraddr, ethcmd,
1310 dev->ethtool_ops->get_link);
1312 case ETHTOOL_GEEPROM:
1313 rc = ethtool_get_eeprom(dev, useraddr);
1315 case ETHTOOL_SEEPROM:
1316 rc = ethtool_set_eeprom(dev, useraddr);
1318 case ETHTOOL_GCOALESCE:
1319 rc = ethtool_get_coalesce(dev, useraddr);
1321 case ETHTOOL_SCOALESCE:
1322 rc = ethtool_set_coalesce(dev, useraddr);
1324 case ETHTOOL_GRINGPARAM:
1325 rc = ethtool_get_ringparam(dev, useraddr);
1327 case ETHTOOL_SRINGPARAM:
1328 rc = ethtool_set_ringparam(dev, useraddr);
1330 case ETHTOOL_GPAUSEPARAM:
1331 rc = ethtool_get_pauseparam(dev, useraddr);
1333 case ETHTOOL_SPAUSEPARAM:
1334 rc = ethtool_set_pauseparam(dev, useraddr);
1336 case ETHTOOL_GRXCSUM:
1337 rc = ethtool_get_value(dev, useraddr, ethcmd,
1338 (dev->ethtool_ops->get_rx_csum ?
1339 dev->ethtool_ops->get_rx_csum :
1340 ethtool_op_get_rx_csum));
1342 case ETHTOOL_SRXCSUM:
1343 rc = ethtool_set_rx_csum(dev, useraddr);
1345 case ETHTOOL_GTXCSUM:
1346 rc = ethtool_get_value(dev, useraddr, ethcmd,
1347 (dev->ethtool_ops->get_tx_csum ?
1348 dev->ethtool_ops->get_tx_csum :
1349 ethtool_op_get_tx_csum));
1351 case ETHTOOL_STXCSUM:
1352 rc = ethtool_set_tx_csum(dev, useraddr);
1355 rc = ethtool_get_value(dev, useraddr, ethcmd,
1356 (dev->ethtool_ops->get_sg ?
1357 dev->ethtool_ops->get_sg :
1358 ethtool_op_get_sg));
1361 rc = ethtool_set_sg(dev, useraddr);
1364 rc = ethtool_get_value(dev, useraddr, ethcmd,
1365 (dev->ethtool_ops->get_tso ?
1366 dev->ethtool_ops->get_tso :
1367 ethtool_op_get_tso));
1370 rc = ethtool_set_tso(dev, useraddr);
1373 rc = ethtool_self_test(dev, useraddr);
1375 case ETHTOOL_GSTRINGS:
1376 rc = ethtool_get_strings(dev, useraddr);
1378 case ETHTOOL_PHYS_ID:
1379 rc = ethtool_phys_id(dev, useraddr);
1381 case ETHTOOL_GSTATS:
1382 rc = ethtool_get_stats(dev, useraddr);
1384 case ETHTOOL_GPERMADDR:
1385 rc = ethtool_get_perm_addr(dev, useraddr);
1388 rc = ethtool_get_value(dev, useraddr, ethcmd,
1389 (dev->ethtool_ops->get_ufo ?
1390 dev->ethtool_ops->get_ufo :
1391 ethtool_op_get_ufo));
1394 rc = ethtool_set_ufo(dev, useraddr);
1397 rc = ethtool_get_gso(dev, useraddr);
1400 rc = ethtool_set_gso(dev, useraddr);
1402 case ETHTOOL_GFLAGS:
1403 rc = ethtool_get_value(dev, useraddr, ethcmd,
1404 (dev->ethtool_ops->get_flags ?
1405 dev->ethtool_ops->get_flags :
1406 ethtool_op_get_flags));
1408 case ETHTOOL_SFLAGS:
1409 rc = ethtool_set_value(dev, useraddr,
1410 dev->ethtool_ops->set_flags);
1412 case ETHTOOL_GPFLAGS:
1413 rc = ethtool_get_value(dev, useraddr, ethcmd,
1414 dev->ethtool_ops->get_priv_flags);
1416 case ETHTOOL_SPFLAGS:
1417 rc = ethtool_set_value(dev, useraddr,
1418 dev->ethtool_ops->set_priv_flags);
1421 case ETHTOOL_GRXRINGS:
1422 case ETHTOOL_GRXCLSRLCNT:
1423 case ETHTOOL_GRXCLSRULE:
1424 case ETHTOOL_GRXCLSRLALL:
1425 rc = ethtool_get_rxnfc(dev, useraddr);
1428 case ETHTOOL_SRXCLSRLDEL:
1429 case ETHTOOL_SRXCLSRLINS:
1430 rc = ethtool_set_rxnfc(dev, useraddr);
1433 rc = ethtool_get_gro(dev, useraddr);
1436 rc = ethtool_set_gro(dev, useraddr);
1438 case ETHTOOL_FLASHDEV:
1439 rc = ethtool_flash_device(dev, useraddr);
1442 rc = ethtool_reset(dev, useraddr);
1444 case ETHTOOL_SRXNTUPLE:
1445 rc = ethtool_set_rx_ntuple(dev, useraddr);
1447 case ETHTOOL_GRXNTUPLE:
1448 rc = ethtool_get_rx_ntuple(dev, useraddr);
1454 if (dev->ethtool_ops->complete)
1455 dev->ethtool_ops->complete(dev);
1457 if (old_features != dev->features)
1458 netdev_features_change(dev);
1463 EXPORT_SYMBOL(ethtool_op_get_link);
1464 EXPORT_SYMBOL(ethtool_op_get_sg);
1465 EXPORT_SYMBOL(ethtool_op_get_tso);
1466 EXPORT_SYMBOL(ethtool_op_set_sg);
1467 EXPORT_SYMBOL(ethtool_op_set_tso);
1468 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1469 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1470 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1471 EXPORT_SYMBOL(ethtool_op_set_ufo);
1472 EXPORT_SYMBOL(ethtool_op_get_ufo);
1473 EXPORT_SYMBOL(ethtool_op_set_flags);
1474 EXPORT_SYMBOL(ethtool_op_get_flags);