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 const struct ethtool_ops *ops = dev->ethtool_ops;
139 if (data & ETH_FLAG_LRO)
140 dev->features |= NETIF_F_LRO;
142 dev->features &= ~NETIF_F_LRO;
144 if (data & ETH_FLAG_NTUPLE) {
145 if (!ops->set_rx_ntuple)
147 dev->features |= NETIF_F_NTUPLE;
149 /* safe to clear regardless */
150 dev->features &= ~NETIF_F_NTUPLE;
156 void ethtool_ntuple_flush(struct net_device *dev)
158 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
160 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
161 list_del(&fsc->list);
164 dev->ethtool_ntuple_list.count = 0;
166 EXPORT_SYMBOL(ethtool_ntuple_flush);
168 /* Handlers for each ethtool command */
170 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
172 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
175 if (!dev->ethtool_ops->get_settings)
178 err = dev->ethtool_ops->get_settings(dev, &cmd);
182 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
187 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
189 struct ethtool_cmd cmd;
191 if (!dev->ethtool_ops->set_settings)
194 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
197 return dev->ethtool_ops->set_settings(dev, &cmd);
201 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
203 static noinline int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
205 struct ethtool_drvinfo info;
206 const struct ethtool_ops *ops = dev->ethtool_ops;
208 if (!ops->get_drvinfo)
211 memset(&info, 0, sizeof(info));
212 info.cmd = ETHTOOL_GDRVINFO;
213 ops->get_drvinfo(dev, &info);
215 if (ops->get_sset_count) {
218 rc = ops->get_sset_count(dev, ETH_SS_TEST);
220 info.testinfo_len = rc;
221 rc = ops->get_sset_count(dev, ETH_SS_STATS);
224 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
226 info.n_priv_flags = rc;
228 if (ops->get_regs_len)
229 info.regdump_len = ops->get_regs_len(dev);
230 if (ops->get_eeprom_len)
231 info.eedump_len = ops->get_eeprom_len(dev);
233 if (copy_to_user(useraddr, &info, sizeof(info)))
239 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
241 static noinline int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
243 struct ethtool_rxnfc cmd;
245 if (!dev->ethtool_ops->set_rxnfc)
248 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
251 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
255 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
257 static noinline int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
259 struct ethtool_rxnfc info;
260 const struct ethtool_ops *ops = dev->ethtool_ops;
262 void *rule_buf = NULL;
267 if (copy_from_user(&info, useraddr, sizeof(info)))
270 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
271 if (info.rule_cnt > 0) {
272 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
279 ret = ops->get_rxnfc(dev, &info, rule_buf);
284 if (copy_to_user(useraddr, &info, sizeof(info)))
288 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
289 if (copy_to_user(useraddr, rule_buf,
290 info.rule_cnt * sizeof(u32)))
301 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
302 struct ethtool_rx_ntuple_flow_spec *spec,
303 struct ethtool_rx_ntuple_flow_spec_container *fsc)
306 /* don't add filters forever */
307 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
308 /* free the container */
313 /* Copy the whole filter over */
314 fsc->fs.flow_type = spec->flow_type;
315 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
316 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
318 fsc->fs.vlan_tag = spec->vlan_tag;
319 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
320 fsc->fs.data = spec->data;
321 fsc->fs.data_mask = spec->data_mask;
322 fsc->fs.action = spec->action;
324 /* add to the list */
325 list_add_tail_rcu(&fsc->list, &list->list);
330 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
332 static noinline int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
334 struct ethtool_rx_ntuple cmd;
335 const struct ethtool_ops *ops = dev->ethtool_ops;
336 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
339 if (!(dev->features & NETIF_F_NTUPLE))
342 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
346 * Cache filter in dev struct for GET operation only if
347 * the underlying driver doesn't have its own GET operation, and
348 * only if the filter was added successfully. First make sure we
349 * can allocate the filter, then continue if successful.
351 if (!ops->get_rx_ntuple) {
352 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
357 ret = ops->set_rx_ntuple(dev, &cmd);
363 if (!ops->get_rx_ntuple)
364 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
369 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
371 struct ethtool_gstrings gstrings;
372 const struct ethtool_ops *ops = dev->ethtool_ops;
373 struct ethtool_rx_ntuple_flow_spec_container *fsc;
376 int ret, i, num_strings = 0;
378 if (!ops->get_sset_count)
381 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
384 ret = ops->get_sset_count(dev, gstrings.string_set);
390 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
394 if (ops->get_rx_ntuple) {
395 /* driver-specific filter grab */
396 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
400 /* default ethtool filter grab */
403 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
404 sprintf(p, "Filter %d:\n", i);
405 p += ETH_GSTRING_LEN;
408 switch (fsc->fs.flow_type) {
410 sprintf(p, "\tFlow Type: TCP\n");
411 p += ETH_GSTRING_LEN;
415 sprintf(p, "\tFlow Type: UDP\n");
416 p += ETH_GSTRING_LEN;
420 sprintf(p, "\tFlow Type: SCTP\n");
421 p += ETH_GSTRING_LEN;
425 sprintf(p, "\tFlow Type: AH ESP\n");
426 p += ETH_GSTRING_LEN;
430 sprintf(p, "\tFlow Type: ESP\n");
431 p += ETH_GSTRING_LEN;
435 sprintf(p, "\tFlow Type: Raw IP\n");
436 p += ETH_GSTRING_LEN;
440 sprintf(p, "\tFlow Type: IPv4\n");
441 p += ETH_GSTRING_LEN;
445 sprintf(p, "\tFlow Type: Unknown\n");
446 p += ETH_GSTRING_LEN;
451 /* now the rest of the filters */
452 switch (fsc->fs.flow_type) {
456 sprintf(p, "\tSrc IP addr: 0x%x\n",
457 fsc->fs.h_u.tcp_ip4_spec.ip4src);
458 p += ETH_GSTRING_LEN;
460 sprintf(p, "\tSrc IP mask: 0x%x\n",
461 fsc->fs.m_u.tcp_ip4_spec.ip4src);
462 p += ETH_GSTRING_LEN;
464 sprintf(p, "\tDest IP addr: 0x%x\n",
465 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
466 p += ETH_GSTRING_LEN;
468 sprintf(p, "\tDest IP mask: 0x%x\n",
469 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
470 p += ETH_GSTRING_LEN;
472 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
473 fsc->fs.h_u.tcp_ip4_spec.psrc,
474 fsc->fs.m_u.tcp_ip4_spec.psrc);
475 p += ETH_GSTRING_LEN;
477 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
478 fsc->fs.h_u.tcp_ip4_spec.pdst,
479 fsc->fs.m_u.tcp_ip4_spec.pdst);
480 p += ETH_GSTRING_LEN;
482 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
483 fsc->fs.h_u.tcp_ip4_spec.tos,
484 fsc->fs.m_u.tcp_ip4_spec.tos);
485 p += ETH_GSTRING_LEN;
490 sprintf(p, "\tSrc IP addr: 0x%x\n",
491 fsc->fs.h_u.ah_ip4_spec.ip4src);
492 p += ETH_GSTRING_LEN;
494 sprintf(p, "\tSrc IP mask: 0x%x\n",
495 fsc->fs.m_u.ah_ip4_spec.ip4src);
496 p += ETH_GSTRING_LEN;
498 sprintf(p, "\tDest IP addr: 0x%x\n",
499 fsc->fs.h_u.ah_ip4_spec.ip4dst);
500 p += ETH_GSTRING_LEN;
502 sprintf(p, "\tDest IP mask: 0x%x\n",
503 fsc->fs.m_u.ah_ip4_spec.ip4dst);
504 p += ETH_GSTRING_LEN;
506 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
507 fsc->fs.h_u.ah_ip4_spec.spi,
508 fsc->fs.m_u.ah_ip4_spec.spi);
509 p += ETH_GSTRING_LEN;
511 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
512 fsc->fs.h_u.ah_ip4_spec.tos,
513 fsc->fs.m_u.ah_ip4_spec.tos);
514 p += ETH_GSTRING_LEN;
518 sprintf(p, "\tSrc IP addr: 0x%x\n",
519 fsc->fs.h_u.raw_ip4_spec.ip4src);
520 p += ETH_GSTRING_LEN;
522 sprintf(p, "\tSrc IP mask: 0x%x\n",
523 fsc->fs.m_u.raw_ip4_spec.ip4src);
524 p += ETH_GSTRING_LEN;
526 sprintf(p, "\tDest IP addr: 0x%x\n",
527 fsc->fs.h_u.raw_ip4_spec.ip4dst);
528 p += ETH_GSTRING_LEN;
530 sprintf(p, "\tDest IP mask: 0x%x\n",
531 fsc->fs.m_u.raw_ip4_spec.ip4dst);
532 p += ETH_GSTRING_LEN;
536 sprintf(p, "\tSrc IP addr: 0x%x\n",
537 fsc->fs.h_u.usr_ip4_spec.ip4src);
538 p += ETH_GSTRING_LEN;
540 sprintf(p, "\tSrc IP mask: 0x%x\n",
541 fsc->fs.m_u.usr_ip4_spec.ip4src);
542 p += ETH_GSTRING_LEN;
544 sprintf(p, "\tDest IP addr: 0x%x\n",
545 fsc->fs.h_u.usr_ip4_spec.ip4dst);
546 p += ETH_GSTRING_LEN;
548 sprintf(p, "\tDest IP mask: 0x%x\n",
549 fsc->fs.m_u.usr_ip4_spec.ip4dst);
550 p += ETH_GSTRING_LEN;
552 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
553 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
554 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
555 p += ETH_GSTRING_LEN;
557 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
558 fsc->fs.h_u.usr_ip4_spec.tos,
559 fsc->fs.m_u.usr_ip4_spec.tos);
560 p += ETH_GSTRING_LEN;
562 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
563 fsc->fs.h_u.usr_ip4_spec.ip_ver,
564 fsc->fs.m_u.usr_ip4_spec.ip_ver);
565 p += ETH_GSTRING_LEN;
567 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
568 fsc->fs.h_u.usr_ip4_spec.proto,
569 fsc->fs.m_u.usr_ip4_spec.proto);
570 p += ETH_GSTRING_LEN;
574 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
575 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
576 p += ETH_GSTRING_LEN;
578 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
579 p += ETH_GSTRING_LEN;
581 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
582 p += ETH_GSTRING_LEN;
584 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
585 sprintf(p, "\tAction: Drop\n");
587 sprintf(p, "\tAction: Direct to queue %d\n",
589 p += ETH_GSTRING_LEN;
595 /* indicate to userspace how many strings we actually have */
596 gstrings.len = num_strings;
598 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
600 useraddr += sizeof(gstrings);
601 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
610 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
612 struct ethtool_regs regs;
613 const struct ethtool_ops *ops = dev->ethtool_ops;
617 if (!ops->get_regs || !ops->get_regs_len)
620 if (copy_from_user(®s, useraddr, sizeof(regs)))
623 reglen = ops->get_regs_len(dev);
624 if (regs.len > reglen)
627 regbuf = kmalloc(reglen, GFP_USER);
631 ops->get_regs(dev, ®s, regbuf);
634 if (copy_to_user(useraddr, ®s, sizeof(regs)))
636 useraddr += offsetof(struct ethtool_regs, data);
637 if (copy_to_user(useraddr, regbuf, regs.len))
646 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
648 struct ethtool_value reset;
651 if (!dev->ethtool_ops->reset)
654 if (copy_from_user(&reset, useraddr, sizeof(reset)))
657 /* Clear ethtool n-tuple list */
658 ethtool_ntuple_flush(dev);
660 ret = dev->ethtool_ops->reset(dev, &reset.data);
664 if (copy_to_user(useraddr, &reset, sizeof(reset)))
669 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
671 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
673 if (!dev->ethtool_ops->get_wol)
676 dev->ethtool_ops->get_wol(dev, &wol);
678 if (copy_to_user(useraddr, &wol, sizeof(wol)))
683 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
685 struct ethtool_wolinfo wol;
687 if (!dev->ethtool_ops->set_wol)
690 if (copy_from_user(&wol, useraddr, sizeof(wol)))
693 return dev->ethtool_ops->set_wol(dev, &wol);
696 static int ethtool_nway_reset(struct net_device *dev)
698 if (!dev->ethtool_ops->nway_reset)
701 return dev->ethtool_ops->nway_reset(dev);
704 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
706 struct ethtool_eeprom eeprom;
707 const struct ethtool_ops *ops = dev->ethtool_ops;
708 void __user *userbuf = useraddr + sizeof(eeprom);
713 if (!ops->get_eeprom || !ops->get_eeprom_len)
716 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
719 /* Check for wrap and zero */
720 if (eeprom.offset + eeprom.len <= eeprom.offset)
723 /* Check for exceeding total eeprom len */
724 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
727 data = kmalloc(PAGE_SIZE, GFP_USER);
731 bytes_remaining = eeprom.len;
732 while (bytes_remaining > 0) {
733 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
735 ret = ops->get_eeprom(dev, &eeprom, data);
738 if (copy_to_user(userbuf, data, eeprom.len)) {
742 userbuf += eeprom.len;
743 eeprom.offset += eeprom.len;
744 bytes_remaining -= eeprom.len;
747 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
748 eeprom.offset -= eeprom.len;
749 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
756 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
758 struct ethtool_eeprom eeprom;
759 const struct ethtool_ops *ops = dev->ethtool_ops;
760 void __user *userbuf = useraddr + sizeof(eeprom);
765 if (!ops->set_eeprom || !ops->get_eeprom_len)
768 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
771 /* Check for wrap and zero */
772 if (eeprom.offset + eeprom.len <= eeprom.offset)
775 /* Check for exceeding total eeprom len */
776 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
779 data = kmalloc(PAGE_SIZE, GFP_USER);
783 bytes_remaining = eeprom.len;
784 while (bytes_remaining > 0) {
785 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
787 if (copy_from_user(data, userbuf, eeprom.len)) {
791 ret = ops->set_eeprom(dev, &eeprom, data);
794 userbuf += eeprom.len;
795 eeprom.offset += eeprom.len;
796 bytes_remaining -= eeprom.len;
804 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
806 static noinline int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
808 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
810 if (!dev->ethtool_ops->get_coalesce)
813 dev->ethtool_ops->get_coalesce(dev, &coalesce);
815 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
821 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
823 static noinline int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
825 struct ethtool_coalesce coalesce;
827 if (!dev->ethtool_ops->set_coalesce)
830 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
833 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
836 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
838 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
840 if (!dev->ethtool_ops->get_ringparam)
843 dev->ethtool_ops->get_ringparam(dev, &ringparam);
845 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
850 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
852 struct ethtool_ringparam ringparam;
854 if (!dev->ethtool_ops->set_ringparam)
857 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
860 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
863 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
865 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
867 if (!dev->ethtool_ops->get_pauseparam)
870 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
872 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
877 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
879 struct ethtool_pauseparam pauseparam;
881 if (!dev->ethtool_ops->set_pauseparam)
884 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
887 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
890 static int __ethtool_set_sg(struct net_device *dev, u32 data)
894 if (!data && dev->ethtool_ops->set_tso) {
895 err = dev->ethtool_ops->set_tso(dev, 0);
900 if (!data && dev->ethtool_ops->set_ufo) {
901 err = dev->ethtool_ops->set_ufo(dev, 0);
905 return dev->ethtool_ops->set_sg(dev, data);
908 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
910 struct ethtool_value edata;
913 if (!dev->ethtool_ops->set_tx_csum)
916 if (copy_from_user(&edata, useraddr, sizeof(edata)))
919 if (!edata.data && dev->ethtool_ops->set_sg) {
920 err = __ethtool_set_sg(dev, 0);
925 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
928 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
930 struct ethtool_value edata;
932 if (!dev->ethtool_ops->set_rx_csum)
935 if (copy_from_user(&edata, useraddr, sizeof(edata)))
938 if (!edata.data && dev->ethtool_ops->set_sg)
939 dev->features &= ~NETIF_F_GRO;
941 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
944 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
946 struct ethtool_value edata;
948 if (!dev->ethtool_ops->set_sg)
951 if (copy_from_user(&edata, useraddr, sizeof(edata)))
955 !(dev->features & NETIF_F_ALL_CSUM))
958 return __ethtool_set_sg(dev, edata.data);
961 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
963 struct ethtool_value edata;
965 if (!dev->ethtool_ops->set_tso)
968 if (copy_from_user(&edata, useraddr, sizeof(edata)))
971 if (edata.data && !(dev->features & NETIF_F_SG))
974 return dev->ethtool_ops->set_tso(dev, edata.data);
977 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
979 struct ethtool_value edata;
981 if (!dev->ethtool_ops->set_ufo)
983 if (copy_from_user(&edata, useraddr, sizeof(edata)))
985 if (edata.data && !(dev->features & NETIF_F_SG))
987 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
989 return dev->ethtool_ops->set_ufo(dev, edata.data);
992 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
994 struct ethtool_value edata = { ETHTOOL_GGSO };
996 edata.data = dev->features & NETIF_F_GSO;
997 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1002 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1004 struct ethtool_value edata;
1006 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1009 dev->features |= NETIF_F_GSO;
1011 dev->features &= ~NETIF_F_GSO;
1015 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1017 struct ethtool_value edata = { ETHTOOL_GGRO };
1019 edata.data = dev->features & NETIF_F_GRO;
1020 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1025 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1027 struct ethtool_value edata;
1029 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1033 if (!dev->ethtool_ops->get_rx_csum ||
1034 !dev->ethtool_ops->get_rx_csum(dev))
1036 dev->features |= NETIF_F_GRO;
1038 dev->features &= ~NETIF_F_GRO;
1043 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1045 struct ethtool_test test;
1046 const struct ethtool_ops *ops = dev->ethtool_ops;
1050 if (!ops->self_test || !ops->get_sset_count)
1053 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1056 WARN_ON(test_len == 0);
1058 if (copy_from_user(&test, useraddr, sizeof(test)))
1061 test.len = test_len;
1062 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1066 ops->self_test(dev, &test, data);
1069 if (copy_to_user(useraddr, &test, sizeof(test)))
1071 useraddr += sizeof(test);
1072 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1081 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1083 struct ethtool_gstrings gstrings;
1084 const struct ethtool_ops *ops = dev->ethtool_ops;
1088 if (!ops->get_strings || !ops->get_sset_count)
1091 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1094 ret = ops->get_sset_count(dev, gstrings.string_set);
1100 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1104 ops->get_strings(dev, gstrings.string_set, data);
1107 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1109 useraddr += sizeof(gstrings);
1110 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1119 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1121 struct ethtool_value id;
1123 if (!dev->ethtool_ops->phys_id)
1126 if (copy_from_user(&id, useraddr, sizeof(id)))
1129 return dev->ethtool_ops->phys_id(dev, id.data);
1132 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1134 struct ethtool_stats stats;
1135 const struct ethtool_ops *ops = dev->ethtool_ops;
1139 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1142 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1145 WARN_ON(n_stats == 0);
1147 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1150 stats.n_stats = n_stats;
1151 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1155 ops->get_ethtool_stats(dev, &stats, data);
1158 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1160 useraddr += sizeof(stats);
1161 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1170 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1172 struct ethtool_perm_addr epaddr;
1174 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1177 if (epaddr.size < dev->addr_len)
1179 epaddr.size = dev->addr_len;
1181 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1183 useraddr += sizeof(epaddr);
1184 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1189 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1190 u32 cmd, u32 (*actor)(struct net_device *))
1192 struct ethtool_value edata = { .cmd = cmd };
1197 edata.data = actor(dev);
1199 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1204 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1205 void (*actor)(struct net_device *, u32))
1207 struct ethtool_value edata;
1212 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1215 actor(dev, edata.data);
1219 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1220 int (*actor)(struct net_device *, u32))
1222 struct ethtool_value edata;
1227 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1230 return actor(dev, edata.data);
1234 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
1236 static noinline int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1238 struct ethtool_flash efl;
1240 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1243 if (!dev->ethtool_ops->flash_device)
1246 return dev->ethtool_ops->flash_device(dev, &efl);
1249 /* The main entry point in this file. Called from net/core/dev.c */
1251 int dev_ethtool(struct net *net, struct ifreq *ifr)
1253 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1254 void __user *useraddr = ifr->ifr_data;
1257 unsigned long old_features;
1259 if (!dev || !netif_device_present(dev))
1262 if (!dev->ethtool_ops)
1265 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1268 /* Allow some commands to be done by anyone */
1270 case ETHTOOL_GDRVINFO:
1271 case ETHTOOL_GMSGLVL:
1272 case ETHTOOL_GCOALESCE:
1273 case ETHTOOL_GRINGPARAM:
1274 case ETHTOOL_GPAUSEPARAM:
1275 case ETHTOOL_GRXCSUM:
1276 case ETHTOOL_GTXCSUM:
1278 case ETHTOOL_GSTRINGS:
1280 case ETHTOOL_GPERMADDR:
1284 case ETHTOOL_GFLAGS:
1285 case ETHTOOL_GPFLAGS:
1287 case ETHTOOL_GRXRINGS:
1288 case ETHTOOL_GRXCLSRLCNT:
1289 case ETHTOOL_GRXCLSRULE:
1290 case ETHTOOL_GRXCLSRLALL:
1293 if (!capable(CAP_NET_ADMIN))
1297 if (dev->ethtool_ops->begin)
1298 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1301 old_features = dev->features;
1305 rc = ethtool_get_settings(dev, useraddr);
1308 rc = ethtool_set_settings(dev, useraddr);
1310 case ETHTOOL_GDRVINFO:
1311 rc = ethtool_get_drvinfo(dev, useraddr);
1314 rc = ethtool_get_regs(dev, useraddr);
1317 rc = ethtool_get_wol(dev, useraddr);
1320 rc = ethtool_set_wol(dev, useraddr);
1322 case ETHTOOL_GMSGLVL:
1323 rc = ethtool_get_value(dev, useraddr, ethcmd,
1324 dev->ethtool_ops->get_msglevel);
1326 case ETHTOOL_SMSGLVL:
1327 rc = ethtool_set_value_void(dev, useraddr,
1328 dev->ethtool_ops->set_msglevel);
1330 case ETHTOOL_NWAY_RST:
1331 rc = ethtool_nway_reset(dev);
1334 rc = ethtool_get_value(dev, useraddr, ethcmd,
1335 dev->ethtool_ops->get_link);
1337 case ETHTOOL_GEEPROM:
1338 rc = ethtool_get_eeprom(dev, useraddr);
1340 case ETHTOOL_SEEPROM:
1341 rc = ethtool_set_eeprom(dev, useraddr);
1343 case ETHTOOL_GCOALESCE:
1344 rc = ethtool_get_coalesce(dev, useraddr);
1346 case ETHTOOL_SCOALESCE:
1347 rc = ethtool_set_coalesce(dev, useraddr);
1349 case ETHTOOL_GRINGPARAM:
1350 rc = ethtool_get_ringparam(dev, useraddr);
1352 case ETHTOOL_SRINGPARAM:
1353 rc = ethtool_set_ringparam(dev, useraddr);
1355 case ETHTOOL_GPAUSEPARAM:
1356 rc = ethtool_get_pauseparam(dev, useraddr);
1358 case ETHTOOL_SPAUSEPARAM:
1359 rc = ethtool_set_pauseparam(dev, useraddr);
1361 case ETHTOOL_GRXCSUM:
1362 rc = ethtool_get_value(dev, useraddr, ethcmd,
1363 (dev->ethtool_ops->get_rx_csum ?
1364 dev->ethtool_ops->get_rx_csum :
1365 ethtool_op_get_rx_csum));
1367 case ETHTOOL_SRXCSUM:
1368 rc = ethtool_set_rx_csum(dev, useraddr);
1370 case ETHTOOL_GTXCSUM:
1371 rc = ethtool_get_value(dev, useraddr, ethcmd,
1372 (dev->ethtool_ops->get_tx_csum ?
1373 dev->ethtool_ops->get_tx_csum :
1374 ethtool_op_get_tx_csum));
1376 case ETHTOOL_STXCSUM:
1377 rc = ethtool_set_tx_csum(dev, useraddr);
1380 rc = ethtool_get_value(dev, useraddr, ethcmd,
1381 (dev->ethtool_ops->get_sg ?
1382 dev->ethtool_ops->get_sg :
1383 ethtool_op_get_sg));
1386 rc = ethtool_set_sg(dev, useraddr);
1389 rc = ethtool_get_value(dev, useraddr, ethcmd,
1390 (dev->ethtool_ops->get_tso ?
1391 dev->ethtool_ops->get_tso :
1392 ethtool_op_get_tso));
1395 rc = ethtool_set_tso(dev, useraddr);
1398 rc = ethtool_self_test(dev, useraddr);
1400 case ETHTOOL_GSTRINGS:
1401 rc = ethtool_get_strings(dev, useraddr);
1403 case ETHTOOL_PHYS_ID:
1404 rc = ethtool_phys_id(dev, useraddr);
1406 case ETHTOOL_GSTATS:
1407 rc = ethtool_get_stats(dev, useraddr);
1409 case ETHTOOL_GPERMADDR:
1410 rc = ethtool_get_perm_addr(dev, useraddr);
1413 rc = ethtool_get_value(dev, useraddr, ethcmd,
1414 (dev->ethtool_ops->get_ufo ?
1415 dev->ethtool_ops->get_ufo :
1416 ethtool_op_get_ufo));
1419 rc = ethtool_set_ufo(dev, useraddr);
1422 rc = ethtool_get_gso(dev, useraddr);
1425 rc = ethtool_set_gso(dev, useraddr);
1427 case ETHTOOL_GFLAGS:
1428 rc = ethtool_get_value(dev, useraddr, ethcmd,
1429 (dev->ethtool_ops->get_flags ?
1430 dev->ethtool_ops->get_flags :
1431 ethtool_op_get_flags));
1433 case ETHTOOL_SFLAGS:
1434 rc = ethtool_set_value(dev, useraddr,
1435 dev->ethtool_ops->set_flags);
1437 case ETHTOOL_GPFLAGS:
1438 rc = ethtool_get_value(dev, useraddr, ethcmd,
1439 dev->ethtool_ops->get_priv_flags);
1441 case ETHTOOL_SPFLAGS:
1442 rc = ethtool_set_value(dev, useraddr,
1443 dev->ethtool_ops->set_priv_flags);
1446 case ETHTOOL_GRXRINGS:
1447 case ETHTOOL_GRXCLSRLCNT:
1448 case ETHTOOL_GRXCLSRULE:
1449 case ETHTOOL_GRXCLSRLALL:
1450 rc = ethtool_get_rxnfc(dev, useraddr);
1453 case ETHTOOL_SRXCLSRLDEL:
1454 case ETHTOOL_SRXCLSRLINS:
1455 rc = ethtool_set_rxnfc(dev, useraddr);
1458 rc = ethtool_get_gro(dev, useraddr);
1461 rc = ethtool_set_gro(dev, useraddr);
1463 case ETHTOOL_FLASHDEV:
1464 rc = ethtool_flash_device(dev, useraddr);
1467 rc = ethtool_reset(dev, useraddr);
1469 case ETHTOOL_SRXNTUPLE:
1470 rc = ethtool_set_rx_ntuple(dev, useraddr);
1472 case ETHTOOL_GRXNTUPLE:
1473 rc = ethtool_get_rx_ntuple(dev, useraddr);
1479 if (dev->ethtool_ops->complete)
1480 dev->ethtool_ops->complete(dev);
1482 if (old_features != dev->features)
1483 netdev_features_change(dev);
1488 EXPORT_SYMBOL(ethtool_op_get_link);
1489 EXPORT_SYMBOL(ethtool_op_get_sg);
1490 EXPORT_SYMBOL(ethtool_op_get_tso);
1491 EXPORT_SYMBOL(ethtool_op_set_sg);
1492 EXPORT_SYMBOL(ethtool_op_set_tso);
1493 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1494 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1495 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1496 EXPORT_SYMBOL(ethtool_op_set_ufo);
1497 EXPORT_SYMBOL(ethtool_op_get_ufo);
1498 EXPORT_SYMBOL(ethtool_op_set_flags);
1499 EXPORT_SYMBOL(ethtool_op_get_flags);