2 * net/core/ethtool.c - Ethtool ioctl handler
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);
203 static noinline_for_stack 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);
216 * this method of obtaining string set info is deprecated;
217 * Use ETHTOOL_GSSET_INFO instead.
219 if (ops->get_sset_count) {
222 rc = ops->get_sset_count(dev, ETH_SS_TEST);
224 info.testinfo_len = rc;
225 rc = ops->get_sset_count(dev, ETH_SS_STATS);
228 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
230 info.n_priv_flags = rc;
232 if (ops->get_regs_len)
233 info.regdump_len = ops->get_regs_len(dev);
234 if (ops->get_eeprom_len)
235 info.eedump_len = ops->get_eeprom_len(dev);
237 if (copy_to_user(useraddr, &info, sizeof(info)))
242 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
243 void __user *useraddr)
245 struct ethtool_sset_info info;
246 const struct ethtool_ops *ops = dev->ethtool_ops;
248 int i, idx = 0, n_bits = 0, ret, rc;
249 u32 *info_buf = NULL;
251 if (!ops->get_sset_count)
254 if (copy_from_user(&info, useraddr, sizeof(info)))
257 /* store copy of mask, because we zero struct later on */
258 sset_mask = info.sset_mask;
262 /* calculate size of return buffer */
263 n_bits = hweight64(sset_mask);
265 memset(&info, 0, sizeof(info));
266 info.cmd = ETHTOOL_GSSET_INFO;
268 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
273 * fill return buffer based on input bitmask and successful
274 * get_sset_count return
276 for (i = 0; i < 64; i++) {
277 if (!(sset_mask & (1ULL << i)))
280 rc = ops->get_sset_count(dev, i);
282 info.sset_mask |= (1ULL << i);
283 info_buf[idx++] = rc;
288 if (copy_to_user(useraddr, &info, sizeof(info)))
291 useraddr += offsetof(struct ethtool_sset_info, data);
292 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
302 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
304 struct ethtool_rxnfc cmd;
306 if (!dev->ethtool_ops->set_rxnfc)
309 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
312 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
315 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
317 struct ethtool_rxnfc info;
318 const struct ethtool_ops *ops = dev->ethtool_ops;
320 void *rule_buf = NULL;
325 if (copy_from_user(&info, useraddr, sizeof(info)))
328 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
329 if (info.rule_cnt > 0) {
330 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
337 ret = ops->get_rxnfc(dev, &info, rule_buf);
342 if (copy_to_user(useraddr, &info, sizeof(info)))
346 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
347 if (copy_to_user(useraddr, rule_buf,
348 info.rule_cnt * sizeof(u32)))
359 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
360 struct ethtool_rx_ntuple_flow_spec *spec,
361 struct ethtool_rx_ntuple_flow_spec_container *fsc)
364 /* don't add filters forever */
365 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
366 /* free the container */
371 /* Copy the whole filter over */
372 fsc->fs.flow_type = spec->flow_type;
373 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
374 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
376 fsc->fs.vlan_tag = spec->vlan_tag;
377 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
378 fsc->fs.data = spec->data;
379 fsc->fs.data_mask = spec->data_mask;
380 fsc->fs.action = spec->action;
382 /* add to the list */
383 list_add_tail_rcu(&fsc->list, &list->list);
387 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
389 struct ethtool_rx_ntuple cmd;
390 const struct ethtool_ops *ops = dev->ethtool_ops;
391 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
394 if (!(dev->features & NETIF_F_NTUPLE))
397 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
401 * Cache filter in dev struct for GET operation only if
402 * the underlying driver doesn't have its own GET operation, and
403 * only if the filter was added successfully. First make sure we
404 * can allocate the filter, then continue if successful.
406 if (!ops->get_rx_ntuple) {
407 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
412 ret = ops->set_rx_ntuple(dev, &cmd);
418 if (!ops->get_rx_ntuple)
419 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
424 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
426 struct ethtool_gstrings gstrings;
427 const struct ethtool_ops *ops = dev->ethtool_ops;
428 struct ethtool_rx_ntuple_flow_spec_container *fsc;
431 int ret, i, num_strings = 0;
433 if (!ops->get_sset_count)
436 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
439 ret = ops->get_sset_count(dev, gstrings.string_set);
445 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
449 if (ops->get_rx_ntuple) {
450 /* driver-specific filter grab */
451 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
455 /* default ethtool filter grab */
458 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
459 sprintf(p, "Filter %d:\n", i);
460 p += ETH_GSTRING_LEN;
463 switch (fsc->fs.flow_type) {
465 sprintf(p, "\tFlow Type: TCP\n");
466 p += ETH_GSTRING_LEN;
470 sprintf(p, "\tFlow Type: UDP\n");
471 p += ETH_GSTRING_LEN;
475 sprintf(p, "\tFlow Type: SCTP\n");
476 p += ETH_GSTRING_LEN;
480 sprintf(p, "\tFlow Type: AH ESP\n");
481 p += ETH_GSTRING_LEN;
485 sprintf(p, "\tFlow Type: ESP\n");
486 p += ETH_GSTRING_LEN;
490 sprintf(p, "\tFlow Type: Raw IP\n");
491 p += ETH_GSTRING_LEN;
495 sprintf(p, "\tFlow Type: IPv4\n");
496 p += ETH_GSTRING_LEN;
500 sprintf(p, "\tFlow Type: Unknown\n");
501 p += ETH_GSTRING_LEN;
506 /* now the rest of the filters */
507 switch (fsc->fs.flow_type) {
511 sprintf(p, "\tSrc IP addr: 0x%x\n",
512 fsc->fs.h_u.tcp_ip4_spec.ip4src);
513 p += ETH_GSTRING_LEN;
515 sprintf(p, "\tSrc IP mask: 0x%x\n",
516 fsc->fs.m_u.tcp_ip4_spec.ip4src);
517 p += ETH_GSTRING_LEN;
519 sprintf(p, "\tDest IP addr: 0x%x\n",
520 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
521 p += ETH_GSTRING_LEN;
523 sprintf(p, "\tDest IP mask: 0x%x\n",
524 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
525 p += ETH_GSTRING_LEN;
527 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
528 fsc->fs.h_u.tcp_ip4_spec.psrc,
529 fsc->fs.m_u.tcp_ip4_spec.psrc);
530 p += ETH_GSTRING_LEN;
532 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
533 fsc->fs.h_u.tcp_ip4_spec.pdst,
534 fsc->fs.m_u.tcp_ip4_spec.pdst);
535 p += ETH_GSTRING_LEN;
537 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
538 fsc->fs.h_u.tcp_ip4_spec.tos,
539 fsc->fs.m_u.tcp_ip4_spec.tos);
540 p += ETH_GSTRING_LEN;
545 sprintf(p, "\tSrc IP addr: 0x%x\n",
546 fsc->fs.h_u.ah_ip4_spec.ip4src);
547 p += ETH_GSTRING_LEN;
549 sprintf(p, "\tSrc IP mask: 0x%x\n",
550 fsc->fs.m_u.ah_ip4_spec.ip4src);
551 p += ETH_GSTRING_LEN;
553 sprintf(p, "\tDest IP addr: 0x%x\n",
554 fsc->fs.h_u.ah_ip4_spec.ip4dst);
555 p += ETH_GSTRING_LEN;
557 sprintf(p, "\tDest IP mask: 0x%x\n",
558 fsc->fs.m_u.ah_ip4_spec.ip4dst);
559 p += ETH_GSTRING_LEN;
561 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
562 fsc->fs.h_u.ah_ip4_spec.spi,
563 fsc->fs.m_u.ah_ip4_spec.spi);
564 p += ETH_GSTRING_LEN;
566 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
567 fsc->fs.h_u.ah_ip4_spec.tos,
568 fsc->fs.m_u.ah_ip4_spec.tos);
569 p += ETH_GSTRING_LEN;
573 sprintf(p, "\tSrc IP addr: 0x%x\n",
574 fsc->fs.h_u.raw_ip4_spec.ip4src);
575 p += ETH_GSTRING_LEN;
577 sprintf(p, "\tSrc IP mask: 0x%x\n",
578 fsc->fs.m_u.raw_ip4_spec.ip4src);
579 p += ETH_GSTRING_LEN;
581 sprintf(p, "\tDest IP addr: 0x%x\n",
582 fsc->fs.h_u.raw_ip4_spec.ip4dst);
583 p += ETH_GSTRING_LEN;
585 sprintf(p, "\tDest IP mask: 0x%x\n",
586 fsc->fs.m_u.raw_ip4_spec.ip4dst);
587 p += ETH_GSTRING_LEN;
591 sprintf(p, "\tSrc IP addr: 0x%x\n",
592 fsc->fs.h_u.usr_ip4_spec.ip4src);
593 p += ETH_GSTRING_LEN;
595 sprintf(p, "\tSrc IP mask: 0x%x\n",
596 fsc->fs.m_u.usr_ip4_spec.ip4src);
597 p += ETH_GSTRING_LEN;
599 sprintf(p, "\tDest IP addr: 0x%x\n",
600 fsc->fs.h_u.usr_ip4_spec.ip4dst);
601 p += ETH_GSTRING_LEN;
603 sprintf(p, "\tDest IP mask: 0x%x\n",
604 fsc->fs.m_u.usr_ip4_spec.ip4dst);
605 p += ETH_GSTRING_LEN;
607 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
608 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
609 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
610 p += ETH_GSTRING_LEN;
612 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
613 fsc->fs.h_u.usr_ip4_spec.tos,
614 fsc->fs.m_u.usr_ip4_spec.tos);
615 p += ETH_GSTRING_LEN;
617 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
618 fsc->fs.h_u.usr_ip4_spec.ip_ver,
619 fsc->fs.m_u.usr_ip4_spec.ip_ver);
620 p += ETH_GSTRING_LEN;
622 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
623 fsc->fs.h_u.usr_ip4_spec.proto,
624 fsc->fs.m_u.usr_ip4_spec.proto);
625 p += ETH_GSTRING_LEN;
629 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
630 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
631 p += ETH_GSTRING_LEN;
633 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
634 p += ETH_GSTRING_LEN;
636 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
637 p += ETH_GSTRING_LEN;
639 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
640 sprintf(p, "\tAction: Drop\n");
642 sprintf(p, "\tAction: Direct to queue %d\n",
644 p += ETH_GSTRING_LEN;
650 /* indicate to userspace how many strings we actually have */
651 gstrings.len = num_strings;
653 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
655 useraddr += sizeof(gstrings);
656 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
665 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
667 struct ethtool_regs regs;
668 const struct ethtool_ops *ops = dev->ethtool_ops;
672 if (!ops->get_regs || !ops->get_regs_len)
675 if (copy_from_user(®s, useraddr, sizeof(regs)))
678 reglen = ops->get_regs_len(dev);
679 if (regs.len > reglen)
682 regbuf = kmalloc(reglen, GFP_USER);
686 ops->get_regs(dev, ®s, regbuf);
689 if (copy_to_user(useraddr, ®s, sizeof(regs)))
691 useraddr += offsetof(struct ethtool_regs, data);
692 if (copy_to_user(useraddr, regbuf, regs.len))
701 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
703 struct ethtool_value reset;
706 if (!dev->ethtool_ops->reset)
709 if (copy_from_user(&reset, useraddr, sizeof(reset)))
712 ret = dev->ethtool_ops->reset(dev, &reset.data);
716 if (copy_to_user(useraddr, &reset, sizeof(reset)))
721 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
723 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
725 if (!dev->ethtool_ops->get_wol)
728 dev->ethtool_ops->get_wol(dev, &wol);
730 if (copy_to_user(useraddr, &wol, sizeof(wol)))
735 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
737 struct ethtool_wolinfo wol;
739 if (!dev->ethtool_ops->set_wol)
742 if (copy_from_user(&wol, useraddr, sizeof(wol)))
745 return dev->ethtool_ops->set_wol(dev, &wol);
748 static int ethtool_nway_reset(struct net_device *dev)
750 if (!dev->ethtool_ops->nway_reset)
753 return dev->ethtool_ops->nway_reset(dev);
756 static int ethtool_get_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->get_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 ret = ops->get_eeprom(dev, &eeprom, data);
790 if (copy_to_user(userbuf, data, eeprom.len)) {
794 userbuf += eeprom.len;
795 eeprom.offset += eeprom.len;
796 bytes_remaining -= eeprom.len;
799 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
800 eeprom.offset -= eeprom.len;
801 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
808 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
810 struct ethtool_eeprom eeprom;
811 const struct ethtool_ops *ops = dev->ethtool_ops;
812 void __user *userbuf = useraddr + sizeof(eeprom);
817 if (!ops->set_eeprom || !ops->get_eeprom_len)
820 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
823 /* Check for wrap and zero */
824 if (eeprom.offset + eeprom.len <= eeprom.offset)
827 /* Check for exceeding total eeprom len */
828 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
831 data = kmalloc(PAGE_SIZE, GFP_USER);
835 bytes_remaining = eeprom.len;
836 while (bytes_remaining > 0) {
837 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
839 if (copy_from_user(data, userbuf, eeprom.len)) {
843 ret = ops->set_eeprom(dev, &eeprom, data);
846 userbuf += eeprom.len;
847 eeprom.offset += eeprom.len;
848 bytes_remaining -= eeprom.len;
855 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
857 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
859 if (!dev->ethtool_ops->get_coalesce)
862 dev->ethtool_ops->get_coalesce(dev, &coalesce);
864 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
869 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
871 struct ethtool_coalesce coalesce;
873 if (!dev->ethtool_ops->set_coalesce)
876 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
879 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
882 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
884 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
886 if (!dev->ethtool_ops->get_ringparam)
889 dev->ethtool_ops->get_ringparam(dev, &ringparam);
891 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
896 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
898 struct ethtool_ringparam ringparam;
900 if (!dev->ethtool_ops->set_ringparam)
903 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
906 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
909 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
911 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
913 if (!dev->ethtool_ops->get_pauseparam)
916 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
918 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
923 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
925 struct ethtool_pauseparam pauseparam;
927 if (!dev->ethtool_ops->set_pauseparam)
930 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
933 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
936 static int __ethtool_set_sg(struct net_device *dev, u32 data)
940 if (!data && dev->ethtool_ops->set_tso) {
941 err = dev->ethtool_ops->set_tso(dev, 0);
946 if (!data && dev->ethtool_ops->set_ufo) {
947 err = dev->ethtool_ops->set_ufo(dev, 0);
951 return dev->ethtool_ops->set_sg(dev, data);
954 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
956 struct ethtool_value edata;
959 if (!dev->ethtool_ops->set_tx_csum)
962 if (copy_from_user(&edata, useraddr, sizeof(edata)))
965 if (!edata.data && dev->ethtool_ops->set_sg) {
966 err = __ethtool_set_sg(dev, 0);
971 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
974 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
976 struct ethtool_value edata;
978 if (!dev->ethtool_ops->set_rx_csum)
981 if (copy_from_user(&edata, useraddr, sizeof(edata)))
984 if (!edata.data && dev->ethtool_ops->set_sg)
985 dev->features &= ~NETIF_F_GRO;
987 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
990 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
992 struct ethtool_value edata;
994 if (!dev->ethtool_ops->set_sg)
997 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1001 !(dev->features & NETIF_F_ALL_CSUM))
1004 return __ethtool_set_sg(dev, edata.data);
1007 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1009 struct ethtool_value edata;
1011 if (!dev->ethtool_ops->set_tso)
1014 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1017 if (edata.data && !(dev->features & NETIF_F_SG))
1020 return dev->ethtool_ops->set_tso(dev, edata.data);
1023 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1025 struct ethtool_value edata;
1027 if (!dev->ethtool_ops->set_ufo)
1029 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1031 if (edata.data && !(dev->features & NETIF_F_SG))
1033 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1035 return dev->ethtool_ops->set_ufo(dev, edata.data);
1038 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1040 struct ethtool_value edata = { ETHTOOL_GGSO };
1042 edata.data = dev->features & NETIF_F_GSO;
1043 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1048 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1050 struct ethtool_value edata;
1052 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1055 dev->features |= NETIF_F_GSO;
1057 dev->features &= ~NETIF_F_GSO;
1061 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1063 struct ethtool_value edata = { ETHTOOL_GGRO };
1065 edata.data = dev->features & NETIF_F_GRO;
1066 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1071 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1073 struct ethtool_value edata;
1075 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1079 if (!dev->ethtool_ops->get_rx_csum ||
1080 !dev->ethtool_ops->get_rx_csum(dev))
1082 dev->features |= NETIF_F_GRO;
1084 dev->features &= ~NETIF_F_GRO;
1089 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1091 struct ethtool_test test;
1092 const struct ethtool_ops *ops = dev->ethtool_ops;
1096 if (!ops->self_test || !ops->get_sset_count)
1099 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1102 WARN_ON(test_len == 0);
1104 if (copy_from_user(&test, useraddr, sizeof(test)))
1107 test.len = test_len;
1108 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1112 ops->self_test(dev, &test, data);
1115 if (copy_to_user(useraddr, &test, sizeof(test)))
1117 useraddr += sizeof(test);
1118 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1127 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1129 struct ethtool_gstrings gstrings;
1130 const struct ethtool_ops *ops = dev->ethtool_ops;
1134 if (!ops->get_strings || !ops->get_sset_count)
1137 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1140 ret = ops->get_sset_count(dev, gstrings.string_set);
1146 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1150 ops->get_strings(dev, gstrings.string_set, data);
1153 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1155 useraddr += sizeof(gstrings);
1156 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1165 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1167 struct ethtool_value id;
1169 if (!dev->ethtool_ops->phys_id)
1172 if (copy_from_user(&id, useraddr, sizeof(id)))
1175 return dev->ethtool_ops->phys_id(dev, id.data);
1178 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1180 struct ethtool_stats stats;
1181 const struct ethtool_ops *ops = dev->ethtool_ops;
1185 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1188 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1191 WARN_ON(n_stats == 0);
1193 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1196 stats.n_stats = n_stats;
1197 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1201 ops->get_ethtool_stats(dev, &stats, data);
1204 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1206 useraddr += sizeof(stats);
1207 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1216 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1218 struct ethtool_perm_addr epaddr;
1220 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1223 if (epaddr.size < dev->addr_len)
1225 epaddr.size = dev->addr_len;
1227 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1229 useraddr += sizeof(epaddr);
1230 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1235 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1236 u32 cmd, u32 (*actor)(struct net_device *))
1238 struct ethtool_value edata = { .cmd = cmd };
1243 edata.data = actor(dev);
1245 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1250 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1251 void (*actor)(struct net_device *, u32))
1253 struct ethtool_value edata;
1258 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1261 actor(dev, edata.data);
1265 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1266 int (*actor)(struct net_device *, u32))
1268 struct ethtool_value edata;
1273 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1276 return actor(dev, edata.data);
1279 static noinline_for_stack int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1281 struct ethtool_flash efl;
1283 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1286 if (!dev->ethtool_ops->flash_device)
1289 return dev->ethtool_ops->flash_device(dev, &efl);
1292 /* The main entry point in this file. Called from net/core/dev.c */
1294 int dev_ethtool(struct net *net, struct ifreq *ifr)
1296 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1297 void __user *useraddr = ifr->ifr_data;
1300 unsigned long old_features;
1302 if (!dev || !netif_device_present(dev))
1305 if (!dev->ethtool_ops)
1308 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
1311 /* Allow some commands to be done by anyone */
1313 case ETHTOOL_GDRVINFO:
1314 case ETHTOOL_GMSGLVL:
1315 case ETHTOOL_GCOALESCE:
1316 case ETHTOOL_GRINGPARAM:
1317 case ETHTOOL_GPAUSEPARAM:
1318 case ETHTOOL_GRXCSUM:
1319 case ETHTOOL_GTXCSUM:
1321 case ETHTOOL_GSTRINGS:
1323 case ETHTOOL_GPERMADDR:
1327 case ETHTOOL_GFLAGS:
1328 case ETHTOOL_GPFLAGS:
1330 case ETHTOOL_GRXRINGS:
1331 case ETHTOOL_GRXCLSRLCNT:
1332 case ETHTOOL_GRXCLSRULE:
1333 case ETHTOOL_GRXCLSRLALL:
1336 if (!capable(CAP_NET_ADMIN))
1340 if (dev->ethtool_ops->begin)
1341 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1344 old_features = dev->features;
1348 rc = ethtool_get_settings(dev, useraddr);
1351 rc = ethtool_set_settings(dev, useraddr);
1353 case ETHTOOL_GDRVINFO:
1354 rc = ethtool_get_drvinfo(dev, useraddr);
1357 rc = ethtool_get_regs(dev, useraddr);
1360 rc = ethtool_get_wol(dev, useraddr);
1363 rc = ethtool_set_wol(dev, useraddr);
1365 case ETHTOOL_GMSGLVL:
1366 rc = ethtool_get_value(dev, useraddr, ethcmd,
1367 dev->ethtool_ops->get_msglevel);
1369 case ETHTOOL_SMSGLVL:
1370 rc = ethtool_set_value_void(dev, useraddr,
1371 dev->ethtool_ops->set_msglevel);
1373 case ETHTOOL_NWAY_RST:
1374 rc = ethtool_nway_reset(dev);
1377 rc = ethtool_get_value(dev, useraddr, ethcmd,
1378 dev->ethtool_ops->get_link);
1380 case ETHTOOL_GEEPROM:
1381 rc = ethtool_get_eeprom(dev, useraddr);
1383 case ETHTOOL_SEEPROM:
1384 rc = ethtool_set_eeprom(dev, useraddr);
1386 case ETHTOOL_GCOALESCE:
1387 rc = ethtool_get_coalesce(dev, useraddr);
1389 case ETHTOOL_SCOALESCE:
1390 rc = ethtool_set_coalesce(dev, useraddr);
1392 case ETHTOOL_GRINGPARAM:
1393 rc = ethtool_get_ringparam(dev, useraddr);
1395 case ETHTOOL_SRINGPARAM:
1396 rc = ethtool_set_ringparam(dev, useraddr);
1398 case ETHTOOL_GPAUSEPARAM:
1399 rc = ethtool_get_pauseparam(dev, useraddr);
1401 case ETHTOOL_SPAUSEPARAM:
1402 rc = ethtool_set_pauseparam(dev, useraddr);
1404 case ETHTOOL_GRXCSUM:
1405 rc = ethtool_get_value(dev, useraddr, ethcmd,
1406 (dev->ethtool_ops->get_rx_csum ?
1407 dev->ethtool_ops->get_rx_csum :
1408 ethtool_op_get_rx_csum));
1410 case ETHTOOL_SRXCSUM:
1411 rc = ethtool_set_rx_csum(dev, useraddr);
1413 case ETHTOOL_GTXCSUM:
1414 rc = ethtool_get_value(dev, useraddr, ethcmd,
1415 (dev->ethtool_ops->get_tx_csum ?
1416 dev->ethtool_ops->get_tx_csum :
1417 ethtool_op_get_tx_csum));
1419 case ETHTOOL_STXCSUM:
1420 rc = ethtool_set_tx_csum(dev, useraddr);
1423 rc = ethtool_get_value(dev, useraddr, ethcmd,
1424 (dev->ethtool_ops->get_sg ?
1425 dev->ethtool_ops->get_sg :
1426 ethtool_op_get_sg));
1429 rc = ethtool_set_sg(dev, useraddr);
1432 rc = ethtool_get_value(dev, useraddr, ethcmd,
1433 (dev->ethtool_ops->get_tso ?
1434 dev->ethtool_ops->get_tso :
1435 ethtool_op_get_tso));
1438 rc = ethtool_set_tso(dev, useraddr);
1441 rc = ethtool_self_test(dev, useraddr);
1443 case ETHTOOL_GSTRINGS:
1444 rc = ethtool_get_strings(dev, useraddr);
1446 case ETHTOOL_PHYS_ID:
1447 rc = ethtool_phys_id(dev, useraddr);
1449 case ETHTOOL_GSTATS:
1450 rc = ethtool_get_stats(dev, useraddr);
1452 case ETHTOOL_GPERMADDR:
1453 rc = ethtool_get_perm_addr(dev, useraddr);
1456 rc = ethtool_get_value(dev, useraddr, ethcmd,
1457 (dev->ethtool_ops->get_ufo ?
1458 dev->ethtool_ops->get_ufo :
1459 ethtool_op_get_ufo));
1462 rc = ethtool_set_ufo(dev, useraddr);
1465 rc = ethtool_get_gso(dev, useraddr);
1468 rc = ethtool_set_gso(dev, useraddr);
1470 case ETHTOOL_GFLAGS:
1471 rc = ethtool_get_value(dev, useraddr, ethcmd,
1472 (dev->ethtool_ops->get_flags ?
1473 dev->ethtool_ops->get_flags :
1474 ethtool_op_get_flags));
1476 case ETHTOOL_SFLAGS:
1477 rc = ethtool_set_value(dev, useraddr,
1478 dev->ethtool_ops->set_flags);
1480 case ETHTOOL_GPFLAGS:
1481 rc = ethtool_get_value(dev, useraddr, ethcmd,
1482 dev->ethtool_ops->get_priv_flags);
1484 case ETHTOOL_SPFLAGS:
1485 rc = ethtool_set_value(dev, useraddr,
1486 dev->ethtool_ops->set_priv_flags);
1489 case ETHTOOL_GRXRINGS:
1490 case ETHTOOL_GRXCLSRLCNT:
1491 case ETHTOOL_GRXCLSRULE:
1492 case ETHTOOL_GRXCLSRLALL:
1493 rc = ethtool_get_rxnfc(dev, useraddr);
1496 case ETHTOOL_SRXCLSRLDEL:
1497 case ETHTOOL_SRXCLSRLINS:
1498 rc = ethtool_set_rxnfc(dev, useraddr);
1501 rc = ethtool_get_gro(dev, useraddr);
1504 rc = ethtool_set_gro(dev, useraddr);
1506 case ETHTOOL_FLASHDEV:
1507 rc = ethtool_flash_device(dev, useraddr);
1510 rc = ethtool_reset(dev, useraddr);
1512 case ETHTOOL_SRXNTUPLE:
1513 rc = ethtool_set_rx_ntuple(dev, useraddr);
1515 case ETHTOOL_GRXNTUPLE:
1516 rc = ethtool_get_rx_ntuple(dev, useraddr);
1518 case ETHTOOL_GSSET_INFO:
1519 rc = ethtool_get_sset_info(dev, useraddr);
1525 if (dev->ethtool_ops->complete)
1526 dev->ethtool_ops->complete(dev);
1528 if (old_features != dev->features)
1529 netdev_features_change(dev);
1534 EXPORT_SYMBOL(ethtool_op_get_link);
1535 EXPORT_SYMBOL(ethtool_op_get_sg);
1536 EXPORT_SYMBOL(ethtool_op_get_tso);
1537 EXPORT_SYMBOL(ethtool_op_set_sg);
1538 EXPORT_SYMBOL(ethtool_op_set_tso);
1539 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1540 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1541 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1542 EXPORT_SYMBOL(ethtool_op_set_ufo);
1543 EXPORT_SYMBOL(ethtool_op_get_ufo);
1544 EXPORT_SYMBOL(ethtool_op_set_flags);
1545 EXPORT_SYMBOL(ethtool_op_get_flags);