2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/bitops.h>
21 #include <asm/uaccess.h>
24 * Some useful ethtool_ops methods that're device independent.
25 * If we find that all drivers want to do the same thing here,
26 * we can turn these into dev_() function calls.
29 u32 ethtool_op_get_link(struct net_device *dev)
31 return netif_carrier_ok(dev) ? 1 : 0;
34 u32 ethtool_op_get_rx_csum(struct net_device *dev)
36 return (dev->features & NETIF_F_ALL_CSUM) != 0;
38 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
40 u32 ethtool_op_get_tx_csum(struct net_device *dev)
42 return (dev->features & NETIF_F_ALL_CSUM) != 0;
44 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
46 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
49 dev->features |= NETIF_F_IP_CSUM;
51 dev->features &= ~NETIF_F_IP_CSUM;
56 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
59 dev->features |= NETIF_F_HW_CSUM;
61 dev->features &= ~NETIF_F_HW_CSUM;
66 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
69 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
71 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
76 u32 ethtool_op_get_sg(struct net_device *dev)
78 return (dev->features & NETIF_F_SG) != 0;
81 int ethtool_op_set_sg(struct net_device *dev, u32 data)
84 dev->features |= NETIF_F_SG;
86 dev->features &= ~NETIF_F_SG;
91 u32 ethtool_op_get_tso(struct net_device *dev)
93 return (dev->features & NETIF_F_TSO) != 0;
96 int ethtool_op_set_tso(struct net_device *dev, u32 data)
99 dev->features |= NETIF_F_TSO;
101 dev->features &= ~NETIF_F_TSO;
106 u32 ethtool_op_get_ufo(struct net_device *dev)
108 return (dev->features & NETIF_F_UFO) != 0;
111 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
114 dev->features |= NETIF_F_UFO;
116 dev->features &= ~NETIF_F_UFO;
120 /* the following list of flags are the same as their associated
121 * NETIF_F_xxx values in include/linux/netdevice.h
123 static const u32 flags_dup_features =
124 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE);
126 u32 ethtool_op_get_flags(struct net_device *dev)
128 /* in the future, this function will probably contain additional
129 * handling for flags which are not so easily handled
130 * by a simple masking operation
133 return dev->features & flags_dup_features;
136 int ethtool_op_set_flags(struct net_device *dev, u32 data)
138 const struct ethtool_ops *ops = dev->ethtool_ops;
139 unsigned long features = dev->features;
141 if (data & ETH_FLAG_LRO)
142 features |= NETIF_F_LRO;
144 features &= ~NETIF_F_LRO;
146 if (data & ETH_FLAG_NTUPLE) {
147 if (!ops->set_rx_ntuple)
149 features |= NETIF_F_NTUPLE;
151 /* safe to clear regardless */
152 features &= ~NETIF_F_NTUPLE;
155 dev->features = features;
159 void ethtool_ntuple_flush(struct net_device *dev)
161 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
163 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
164 list_del(&fsc->list);
167 dev->ethtool_ntuple_list.count = 0;
169 EXPORT_SYMBOL(ethtool_ntuple_flush);
171 /* Handlers for each ethtool command */
173 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
175 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
178 if (!dev->ethtool_ops->get_settings)
181 err = dev->ethtool_ops->get_settings(dev, &cmd);
185 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
190 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
192 struct ethtool_cmd cmd;
194 if (!dev->ethtool_ops->set_settings)
197 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
200 return dev->ethtool_ops->set_settings(dev, &cmd);
204 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
206 static noinline int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
208 struct ethtool_drvinfo info;
209 const struct ethtool_ops *ops = dev->ethtool_ops;
211 if (!ops->get_drvinfo)
214 memset(&info, 0, sizeof(info));
215 info.cmd = ETHTOOL_GDRVINFO;
216 ops->get_drvinfo(dev, &info);
219 * this method of obtaining string set info is deprecated;
220 * Use ETHTOOL_GSSET_INFO instead.
222 if (ops->get_sset_count) {
225 rc = ops->get_sset_count(dev, ETH_SS_TEST);
227 info.testinfo_len = rc;
228 rc = ops->get_sset_count(dev, ETH_SS_STATS);
231 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
233 info.n_priv_flags = rc;
235 if (ops->get_regs_len)
236 info.regdump_len = ops->get_regs_len(dev);
237 if (ops->get_eeprom_len)
238 info.eedump_len = ops->get_eeprom_len(dev);
240 if (copy_to_user(useraddr, &info, sizeof(info)))
246 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
248 static noinline int ethtool_get_sset_info(struct net_device *dev,
249 void __user *useraddr)
251 struct ethtool_sset_info info;
252 const struct ethtool_ops *ops = dev->ethtool_ops;
254 int i, idx = 0, n_bits = 0, ret, rc;
255 u32 *info_buf = NULL;
257 if (!ops->get_sset_count)
260 if (copy_from_user(&info, useraddr, sizeof(info)))
263 /* store copy of mask, because we zero struct later on */
264 sset_mask = info.sset_mask;
268 /* calculate size of return buffer */
269 n_bits = hweight64(sset_mask);
271 memset(&info, 0, sizeof(info));
272 info.cmd = ETHTOOL_GSSET_INFO;
274 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
279 * fill return buffer based on input bitmask and successful
280 * get_sset_count return
282 for (i = 0; i < 64; i++) {
283 if (!(sset_mask & (1ULL << i)))
286 rc = ops->get_sset_count(dev, i);
288 info.sset_mask |= (1ULL << i);
289 info_buf[idx++] = rc;
294 if (copy_to_user(useraddr, &info, sizeof(info)))
297 useraddr += offsetof(struct ethtool_sset_info, data);
298 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
309 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
311 static noinline int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
313 struct ethtool_rxnfc cmd;
315 if (!dev->ethtool_ops->set_rxnfc)
318 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
321 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
325 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
327 static noinline int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
329 struct ethtool_rxnfc info;
330 const struct ethtool_ops *ops = dev->ethtool_ops;
332 void *rule_buf = NULL;
337 if (copy_from_user(&info, useraddr, sizeof(info)))
340 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
341 if (info.rule_cnt > 0) {
342 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
349 ret = ops->get_rxnfc(dev, &info, rule_buf);
354 if (copy_to_user(useraddr, &info, sizeof(info)))
358 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
359 if (copy_to_user(useraddr, rule_buf,
360 info.rule_cnt * sizeof(u32)))
371 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
372 struct ethtool_rx_ntuple_flow_spec *spec,
373 struct ethtool_rx_ntuple_flow_spec_container *fsc)
376 /* don't add filters forever */
377 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
378 /* free the container */
383 /* Copy the whole filter over */
384 fsc->fs.flow_type = spec->flow_type;
385 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
386 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
388 fsc->fs.vlan_tag = spec->vlan_tag;
389 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
390 fsc->fs.data = spec->data;
391 fsc->fs.data_mask = spec->data_mask;
392 fsc->fs.action = spec->action;
394 /* add to the list */
395 list_add_tail_rcu(&fsc->list, &list->list);
400 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
402 static noinline int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
404 struct ethtool_rx_ntuple cmd;
405 const struct ethtool_ops *ops = dev->ethtool_ops;
406 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
409 if (!(dev->features & NETIF_F_NTUPLE))
412 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
416 * Cache filter in dev struct for GET operation only if
417 * the underlying driver doesn't have its own GET operation, and
418 * only if the filter was added successfully. First make sure we
419 * can allocate the filter, then continue if successful.
421 if (!ops->get_rx_ntuple) {
422 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
427 ret = ops->set_rx_ntuple(dev, &cmd);
433 if (!ops->get_rx_ntuple)
434 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
439 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
441 struct ethtool_gstrings gstrings;
442 const struct ethtool_ops *ops = dev->ethtool_ops;
443 struct ethtool_rx_ntuple_flow_spec_container *fsc;
446 int ret, i, num_strings = 0;
448 if (!ops->get_sset_count)
451 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
454 ret = ops->get_sset_count(dev, gstrings.string_set);
460 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
464 if (ops->get_rx_ntuple) {
465 /* driver-specific filter grab */
466 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
470 /* default ethtool filter grab */
473 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
474 sprintf(p, "Filter %d:\n", i);
475 p += ETH_GSTRING_LEN;
478 switch (fsc->fs.flow_type) {
480 sprintf(p, "\tFlow Type: TCP\n");
481 p += ETH_GSTRING_LEN;
485 sprintf(p, "\tFlow Type: UDP\n");
486 p += ETH_GSTRING_LEN;
490 sprintf(p, "\tFlow Type: SCTP\n");
491 p += ETH_GSTRING_LEN;
495 sprintf(p, "\tFlow Type: AH ESP\n");
496 p += ETH_GSTRING_LEN;
500 sprintf(p, "\tFlow Type: ESP\n");
501 p += ETH_GSTRING_LEN;
505 sprintf(p, "\tFlow Type: Raw IP\n");
506 p += ETH_GSTRING_LEN;
510 sprintf(p, "\tFlow Type: IPv4\n");
511 p += ETH_GSTRING_LEN;
515 sprintf(p, "\tFlow Type: Unknown\n");
516 p += ETH_GSTRING_LEN;
521 /* now the rest of the filters */
522 switch (fsc->fs.flow_type) {
526 sprintf(p, "\tSrc IP addr: 0x%x\n",
527 fsc->fs.h_u.tcp_ip4_spec.ip4src);
528 p += ETH_GSTRING_LEN;
530 sprintf(p, "\tSrc IP mask: 0x%x\n",
531 fsc->fs.m_u.tcp_ip4_spec.ip4src);
532 p += ETH_GSTRING_LEN;
534 sprintf(p, "\tDest IP addr: 0x%x\n",
535 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
536 p += ETH_GSTRING_LEN;
538 sprintf(p, "\tDest IP mask: 0x%x\n",
539 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
540 p += ETH_GSTRING_LEN;
542 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
543 fsc->fs.h_u.tcp_ip4_spec.psrc,
544 fsc->fs.m_u.tcp_ip4_spec.psrc);
545 p += ETH_GSTRING_LEN;
547 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
548 fsc->fs.h_u.tcp_ip4_spec.pdst,
549 fsc->fs.m_u.tcp_ip4_spec.pdst);
550 p += ETH_GSTRING_LEN;
552 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
553 fsc->fs.h_u.tcp_ip4_spec.tos,
554 fsc->fs.m_u.tcp_ip4_spec.tos);
555 p += ETH_GSTRING_LEN;
560 sprintf(p, "\tSrc IP addr: 0x%x\n",
561 fsc->fs.h_u.ah_ip4_spec.ip4src);
562 p += ETH_GSTRING_LEN;
564 sprintf(p, "\tSrc IP mask: 0x%x\n",
565 fsc->fs.m_u.ah_ip4_spec.ip4src);
566 p += ETH_GSTRING_LEN;
568 sprintf(p, "\tDest IP addr: 0x%x\n",
569 fsc->fs.h_u.ah_ip4_spec.ip4dst);
570 p += ETH_GSTRING_LEN;
572 sprintf(p, "\tDest IP mask: 0x%x\n",
573 fsc->fs.m_u.ah_ip4_spec.ip4dst);
574 p += ETH_GSTRING_LEN;
576 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
577 fsc->fs.h_u.ah_ip4_spec.spi,
578 fsc->fs.m_u.ah_ip4_spec.spi);
579 p += ETH_GSTRING_LEN;
581 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
582 fsc->fs.h_u.ah_ip4_spec.tos,
583 fsc->fs.m_u.ah_ip4_spec.tos);
584 p += ETH_GSTRING_LEN;
588 sprintf(p, "\tSrc IP addr: 0x%x\n",
589 fsc->fs.h_u.raw_ip4_spec.ip4src);
590 p += ETH_GSTRING_LEN;
592 sprintf(p, "\tSrc IP mask: 0x%x\n",
593 fsc->fs.m_u.raw_ip4_spec.ip4src);
594 p += ETH_GSTRING_LEN;
596 sprintf(p, "\tDest IP addr: 0x%x\n",
597 fsc->fs.h_u.raw_ip4_spec.ip4dst);
598 p += ETH_GSTRING_LEN;
600 sprintf(p, "\tDest IP mask: 0x%x\n",
601 fsc->fs.m_u.raw_ip4_spec.ip4dst);
602 p += ETH_GSTRING_LEN;
606 sprintf(p, "\tSrc IP addr: 0x%x\n",
607 fsc->fs.h_u.usr_ip4_spec.ip4src);
608 p += ETH_GSTRING_LEN;
610 sprintf(p, "\tSrc IP mask: 0x%x\n",
611 fsc->fs.m_u.usr_ip4_spec.ip4src);
612 p += ETH_GSTRING_LEN;
614 sprintf(p, "\tDest IP addr: 0x%x\n",
615 fsc->fs.h_u.usr_ip4_spec.ip4dst);
616 p += ETH_GSTRING_LEN;
618 sprintf(p, "\tDest IP mask: 0x%x\n",
619 fsc->fs.m_u.usr_ip4_spec.ip4dst);
620 p += ETH_GSTRING_LEN;
622 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
623 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
624 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
625 p += ETH_GSTRING_LEN;
627 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
628 fsc->fs.h_u.usr_ip4_spec.tos,
629 fsc->fs.m_u.usr_ip4_spec.tos);
630 p += ETH_GSTRING_LEN;
632 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
633 fsc->fs.h_u.usr_ip4_spec.ip_ver,
634 fsc->fs.m_u.usr_ip4_spec.ip_ver);
635 p += ETH_GSTRING_LEN;
637 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
638 fsc->fs.h_u.usr_ip4_spec.proto,
639 fsc->fs.m_u.usr_ip4_spec.proto);
640 p += ETH_GSTRING_LEN;
644 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
645 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
646 p += ETH_GSTRING_LEN;
648 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
649 p += ETH_GSTRING_LEN;
651 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
652 p += ETH_GSTRING_LEN;
654 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
655 sprintf(p, "\tAction: Drop\n");
657 sprintf(p, "\tAction: Direct to queue %d\n",
659 p += ETH_GSTRING_LEN;
665 /* indicate to userspace how many strings we actually have */
666 gstrings.len = num_strings;
668 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
670 useraddr += sizeof(gstrings);
671 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
680 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
682 struct ethtool_regs regs;
683 const struct ethtool_ops *ops = dev->ethtool_ops;
687 if (!ops->get_regs || !ops->get_regs_len)
690 if (copy_from_user(®s, useraddr, sizeof(regs)))
693 reglen = ops->get_regs_len(dev);
694 if (regs.len > reglen)
697 regbuf = kmalloc(reglen, GFP_USER);
701 ops->get_regs(dev, ®s, regbuf);
704 if (copy_to_user(useraddr, ®s, sizeof(regs)))
706 useraddr += offsetof(struct ethtool_regs, data);
707 if (copy_to_user(useraddr, regbuf, regs.len))
716 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
718 struct ethtool_value reset;
721 if (!dev->ethtool_ops->reset)
724 if (copy_from_user(&reset, useraddr, sizeof(reset)))
727 ret = dev->ethtool_ops->reset(dev, &reset.data);
731 if (copy_to_user(useraddr, &reset, sizeof(reset)))
736 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
738 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
740 if (!dev->ethtool_ops->get_wol)
743 dev->ethtool_ops->get_wol(dev, &wol);
745 if (copy_to_user(useraddr, &wol, sizeof(wol)))
750 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
752 struct ethtool_wolinfo wol;
754 if (!dev->ethtool_ops->set_wol)
757 if (copy_from_user(&wol, useraddr, sizeof(wol)))
760 return dev->ethtool_ops->set_wol(dev, &wol);
763 static int ethtool_nway_reset(struct net_device *dev)
765 if (!dev->ethtool_ops->nway_reset)
768 return dev->ethtool_ops->nway_reset(dev);
771 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
773 struct ethtool_eeprom eeprom;
774 const struct ethtool_ops *ops = dev->ethtool_ops;
775 void __user *userbuf = useraddr + sizeof(eeprom);
780 if (!ops->get_eeprom || !ops->get_eeprom_len)
783 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
786 /* Check for wrap and zero */
787 if (eeprom.offset + eeprom.len <= eeprom.offset)
790 /* Check for exceeding total eeprom len */
791 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
794 data = kmalloc(PAGE_SIZE, GFP_USER);
798 bytes_remaining = eeprom.len;
799 while (bytes_remaining > 0) {
800 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
802 ret = ops->get_eeprom(dev, &eeprom, data);
805 if (copy_to_user(userbuf, data, eeprom.len)) {
809 userbuf += eeprom.len;
810 eeprom.offset += eeprom.len;
811 bytes_remaining -= eeprom.len;
814 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
815 eeprom.offset -= eeprom.len;
816 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
823 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
825 struct ethtool_eeprom eeprom;
826 const struct ethtool_ops *ops = dev->ethtool_ops;
827 void __user *userbuf = useraddr + sizeof(eeprom);
832 if (!ops->set_eeprom || !ops->get_eeprom_len)
835 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
838 /* Check for wrap and zero */
839 if (eeprom.offset + eeprom.len <= eeprom.offset)
842 /* Check for exceeding total eeprom len */
843 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
846 data = kmalloc(PAGE_SIZE, GFP_USER);
850 bytes_remaining = eeprom.len;
851 while (bytes_remaining > 0) {
852 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
854 if (copy_from_user(data, userbuf, eeprom.len)) {
858 ret = ops->set_eeprom(dev, &eeprom, data);
861 userbuf += eeprom.len;
862 eeprom.offset += eeprom.len;
863 bytes_remaining -= eeprom.len;
871 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
873 static noinline int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
875 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
877 if (!dev->ethtool_ops->get_coalesce)
880 dev->ethtool_ops->get_coalesce(dev, &coalesce);
882 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
888 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
890 static noinline int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
892 struct ethtool_coalesce coalesce;
894 if (!dev->ethtool_ops->set_coalesce)
897 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
900 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
903 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
905 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
907 if (!dev->ethtool_ops->get_ringparam)
910 dev->ethtool_ops->get_ringparam(dev, &ringparam);
912 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
917 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
919 struct ethtool_ringparam ringparam;
921 if (!dev->ethtool_ops->set_ringparam)
924 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
927 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
930 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
932 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
934 if (!dev->ethtool_ops->get_pauseparam)
937 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
939 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
944 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
946 struct ethtool_pauseparam pauseparam;
948 if (!dev->ethtool_ops->set_pauseparam)
951 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
954 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
957 static int __ethtool_set_sg(struct net_device *dev, u32 data)
961 if (!data && dev->ethtool_ops->set_tso) {
962 err = dev->ethtool_ops->set_tso(dev, 0);
967 if (!data && dev->ethtool_ops->set_ufo) {
968 err = dev->ethtool_ops->set_ufo(dev, 0);
972 return dev->ethtool_ops->set_sg(dev, data);
975 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
977 struct ethtool_value edata;
980 if (!dev->ethtool_ops->set_tx_csum)
983 if (copy_from_user(&edata, useraddr, sizeof(edata)))
986 if (!edata.data && dev->ethtool_ops->set_sg) {
987 err = __ethtool_set_sg(dev, 0);
992 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
995 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
997 struct ethtool_value edata;
999 if (!dev->ethtool_ops->set_rx_csum)
1002 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1005 if (!edata.data && dev->ethtool_ops->set_sg)
1006 dev->features &= ~NETIF_F_GRO;
1008 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1011 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1013 struct ethtool_value edata;
1015 if (!dev->ethtool_ops->set_sg)
1018 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1022 !(dev->features & NETIF_F_ALL_CSUM))
1025 return __ethtool_set_sg(dev, edata.data);
1028 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1030 struct ethtool_value edata;
1032 if (!dev->ethtool_ops->set_tso)
1035 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1038 if (edata.data && !(dev->features & NETIF_F_SG))
1041 return dev->ethtool_ops->set_tso(dev, edata.data);
1044 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1046 struct ethtool_value edata;
1048 if (!dev->ethtool_ops->set_ufo)
1050 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1052 if (edata.data && !(dev->features & NETIF_F_SG))
1054 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1056 return dev->ethtool_ops->set_ufo(dev, edata.data);
1059 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1061 struct ethtool_value edata = { ETHTOOL_GGSO };
1063 edata.data = dev->features & NETIF_F_GSO;
1064 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1069 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1071 struct ethtool_value edata;
1073 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1076 dev->features |= NETIF_F_GSO;
1078 dev->features &= ~NETIF_F_GSO;
1082 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1084 struct ethtool_value edata = { ETHTOOL_GGRO };
1086 edata.data = dev->features & NETIF_F_GRO;
1087 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1092 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1094 struct ethtool_value edata;
1096 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1100 if (!dev->ethtool_ops->get_rx_csum ||
1101 !dev->ethtool_ops->get_rx_csum(dev))
1103 dev->features |= NETIF_F_GRO;
1105 dev->features &= ~NETIF_F_GRO;
1110 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1112 struct ethtool_test test;
1113 const struct ethtool_ops *ops = dev->ethtool_ops;
1117 if (!ops->self_test || !ops->get_sset_count)
1120 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1123 WARN_ON(test_len == 0);
1125 if (copy_from_user(&test, useraddr, sizeof(test)))
1128 test.len = test_len;
1129 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1133 ops->self_test(dev, &test, data);
1136 if (copy_to_user(useraddr, &test, sizeof(test)))
1138 useraddr += sizeof(test);
1139 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1148 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1150 struct ethtool_gstrings gstrings;
1151 const struct ethtool_ops *ops = dev->ethtool_ops;
1155 if (!ops->get_strings || !ops->get_sset_count)
1158 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1161 ret = ops->get_sset_count(dev, gstrings.string_set);
1167 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1171 ops->get_strings(dev, gstrings.string_set, data);
1174 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1176 useraddr += sizeof(gstrings);
1177 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1186 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1188 struct ethtool_value id;
1190 if (!dev->ethtool_ops->phys_id)
1193 if (copy_from_user(&id, useraddr, sizeof(id)))
1196 return dev->ethtool_ops->phys_id(dev, id.data);
1199 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1201 struct ethtool_stats stats;
1202 const struct ethtool_ops *ops = dev->ethtool_ops;
1206 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1209 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1212 WARN_ON(n_stats == 0);
1214 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1217 stats.n_stats = n_stats;
1218 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1222 ops->get_ethtool_stats(dev, &stats, data);
1225 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1227 useraddr += sizeof(stats);
1228 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1237 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1239 struct ethtool_perm_addr epaddr;
1241 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1244 if (epaddr.size < dev->addr_len)
1246 epaddr.size = dev->addr_len;
1248 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1250 useraddr += sizeof(epaddr);
1251 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1256 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1257 u32 cmd, u32 (*actor)(struct net_device *))
1259 struct ethtool_value edata = { .cmd = cmd };
1264 edata.data = actor(dev);
1266 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1271 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1272 void (*actor)(struct net_device *, u32))
1274 struct ethtool_value edata;
1279 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1282 actor(dev, edata.data);
1286 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1287 int (*actor)(struct net_device *, u32))
1289 struct ethtool_value edata;
1294 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1297 return actor(dev, edata.data);
1301 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
1303 static noinline int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1305 struct ethtool_flash efl;
1307 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1310 if (!dev->ethtool_ops->flash_device)
1313 return dev->ethtool_ops->flash_device(dev, &efl);
1316 /* The main entry point in this file. Called from net/core/dev.c */
1318 int dev_ethtool(struct net *net, struct ifreq *ifr)
1320 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1321 void __user *useraddr = ifr->ifr_data;
1324 unsigned long old_features;
1326 if (!dev || !netif_device_present(dev))
1329 if (!dev->ethtool_ops)
1332 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1335 /* Allow some commands to be done by anyone */
1337 case ETHTOOL_GDRVINFO:
1338 case ETHTOOL_GMSGLVL:
1339 case ETHTOOL_GCOALESCE:
1340 case ETHTOOL_GRINGPARAM:
1341 case ETHTOOL_GPAUSEPARAM:
1342 case ETHTOOL_GRXCSUM:
1343 case ETHTOOL_GTXCSUM:
1345 case ETHTOOL_GSTRINGS:
1347 case ETHTOOL_GPERMADDR:
1351 case ETHTOOL_GFLAGS:
1352 case ETHTOOL_GPFLAGS:
1354 case ETHTOOL_GRXRINGS:
1355 case ETHTOOL_GRXCLSRLCNT:
1356 case ETHTOOL_GRXCLSRULE:
1357 case ETHTOOL_GRXCLSRLALL:
1360 if (!capable(CAP_NET_ADMIN))
1364 if (dev->ethtool_ops->begin)
1365 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1368 old_features = dev->features;
1372 rc = ethtool_get_settings(dev, useraddr);
1375 rc = ethtool_set_settings(dev, useraddr);
1377 case ETHTOOL_GDRVINFO:
1378 rc = ethtool_get_drvinfo(dev, useraddr);
1381 rc = ethtool_get_regs(dev, useraddr);
1384 rc = ethtool_get_wol(dev, useraddr);
1387 rc = ethtool_set_wol(dev, useraddr);
1389 case ETHTOOL_GMSGLVL:
1390 rc = ethtool_get_value(dev, useraddr, ethcmd,
1391 dev->ethtool_ops->get_msglevel);
1393 case ETHTOOL_SMSGLVL:
1394 rc = ethtool_set_value_void(dev, useraddr,
1395 dev->ethtool_ops->set_msglevel);
1397 case ETHTOOL_NWAY_RST:
1398 rc = ethtool_nway_reset(dev);
1401 rc = ethtool_get_value(dev, useraddr, ethcmd,
1402 dev->ethtool_ops->get_link);
1404 case ETHTOOL_GEEPROM:
1405 rc = ethtool_get_eeprom(dev, useraddr);
1407 case ETHTOOL_SEEPROM:
1408 rc = ethtool_set_eeprom(dev, useraddr);
1410 case ETHTOOL_GCOALESCE:
1411 rc = ethtool_get_coalesce(dev, useraddr);
1413 case ETHTOOL_SCOALESCE:
1414 rc = ethtool_set_coalesce(dev, useraddr);
1416 case ETHTOOL_GRINGPARAM:
1417 rc = ethtool_get_ringparam(dev, useraddr);
1419 case ETHTOOL_SRINGPARAM:
1420 rc = ethtool_set_ringparam(dev, useraddr);
1422 case ETHTOOL_GPAUSEPARAM:
1423 rc = ethtool_get_pauseparam(dev, useraddr);
1425 case ETHTOOL_SPAUSEPARAM:
1426 rc = ethtool_set_pauseparam(dev, useraddr);
1428 case ETHTOOL_GRXCSUM:
1429 rc = ethtool_get_value(dev, useraddr, ethcmd,
1430 (dev->ethtool_ops->get_rx_csum ?
1431 dev->ethtool_ops->get_rx_csum :
1432 ethtool_op_get_rx_csum));
1434 case ETHTOOL_SRXCSUM:
1435 rc = ethtool_set_rx_csum(dev, useraddr);
1437 case ETHTOOL_GTXCSUM:
1438 rc = ethtool_get_value(dev, useraddr, ethcmd,
1439 (dev->ethtool_ops->get_tx_csum ?
1440 dev->ethtool_ops->get_tx_csum :
1441 ethtool_op_get_tx_csum));
1443 case ETHTOOL_STXCSUM:
1444 rc = ethtool_set_tx_csum(dev, useraddr);
1447 rc = ethtool_get_value(dev, useraddr, ethcmd,
1448 (dev->ethtool_ops->get_sg ?
1449 dev->ethtool_ops->get_sg :
1450 ethtool_op_get_sg));
1453 rc = ethtool_set_sg(dev, useraddr);
1456 rc = ethtool_get_value(dev, useraddr, ethcmd,
1457 (dev->ethtool_ops->get_tso ?
1458 dev->ethtool_ops->get_tso :
1459 ethtool_op_get_tso));
1462 rc = ethtool_set_tso(dev, useraddr);
1465 rc = ethtool_self_test(dev, useraddr);
1467 case ETHTOOL_GSTRINGS:
1468 rc = ethtool_get_strings(dev, useraddr);
1470 case ETHTOOL_PHYS_ID:
1471 rc = ethtool_phys_id(dev, useraddr);
1473 case ETHTOOL_GSTATS:
1474 rc = ethtool_get_stats(dev, useraddr);
1476 case ETHTOOL_GPERMADDR:
1477 rc = ethtool_get_perm_addr(dev, useraddr);
1480 rc = ethtool_get_value(dev, useraddr, ethcmd,
1481 (dev->ethtool_ops->get_ufo ?
1482 dev->ethtool_ops->get_ufo :
1483 ethtool_op_get_ufo));
1486 rc = ethtool_set_ufo(dev, useraddr);
1489 rc = ethtool_get_gso(dev, useraddr);
1492 rc = ethtool_set_gso(dev, useraddr);
1494 case ETHTOOL_GFLAGS:
1495 rc = ethtool_get_value(dev, useraddr, ethcmd,
1496 (dev->ethtool_ops->get_flags ?
1497 dev->ethtool_ops->get_flags :
1498 ethtool_op_get_flags));
1500 case ETHTOOL_SFLAGS:
1501 rc = ethtool_set_value(dev, useraddr,
1502 dev->ethtool_ops->set_flags);
1504 case ETHTOOL_GPFLAGS:
1505 rc = ethtool_get_value(dev, useraddr, ethcmd,
1506 dev->ethtool_ops->get_priv_flags);
1508 case ETHTOOL_SPFLAGS:
1509 rc = ethtool_set_value(dev, useraddr,
1510 dev->ethtool_ops->set_priv_flags);
1513 case ETHTOOL_GRXRINGS:
1514 case ETHTOOL_GRXCLSRLCNT:
1515 case ETHTOOL_GRXCLSRULE:
1516 case ETHTOOL_GRXCLSRLALL:
1517 rc = ethtool_get_rxnfc(dev, useraddr);
1520 case ETHTOOL_SRXCLSRLDEL:
1521 case ETHTOOL_SRXCLSRLINS:
1522 rc = ethtool_set_rxnfc(dev, useraddr);
1525 rc = ethtool_get_gro(dev, useraddr);
1528 rc = ethtool_set_gro(dev, useraddr);
1530 case ETHTOOL_FLASHDEV:
1531 rc = ethtool_flash_device(dev, useraddr);
1534 rc = ethtool_reset(dev, useraddr);
1536 case ETHTOOL_SRXNTUPLE:
1537 rc = ethtool_set_rx_ntuple(dev, useraddr);
1539 case ETHTOOL_GRXNTUPLE:
1540 rc = ethtool_get_rx_ntuple(dev, useraddr);
1542 case ETHTOOL_GSSET_INFO:
1543 rc = ethtool_get_sset_info(dev, useraddr);
1549 if (dev->ethtool_ops->complete)
1550 dev->ethtool_ops->complete(dev);
1552 if (old_features != dev->features)
1553 netdev_features_change(dev);
1558 EXPORT_SYMBOL(ethtool_op_get_link);
1559 EXPORT_SYMBOL(ethtool_op_get_sg);
1560 EXPORT_SYMBOL(ethtool_op_get_tso);
1561 EXPORT_SYMBOL(ethtool_op_set_sg);
1562 EXPORT_SYMBOL(ethtool_op_set_tso);
1563 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1564 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1565 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1566 EXPORT_SYMBOL(ethtool_op_set_ufo);
1567 EXPORT_SYMBOL(ethtool_op_get_ufo);
1568 EXPORT_SYMBOL(ethtool_op_set_flags);
1569 EXPORT_SYMBOL(ethtool_op_get_flags);