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 <linux/uaccess.h>
22 #include <linux/slab.h>
25 * Some useful ethtool_ops methods that're device independent.
26 * If we find that all drivers want to do the same thing here,
27 * we can turn these into dev_() function calls.
30 u32 ethtool_op_get_link(struct net_device *dev)
32 return netif_carrier_ok(dev) ? 1 : 0;
34 EXPORT_SYMBOL(ethtool_op_get_link);
36 u32 ethtool_op_get_rx_csum(struct net_device *dev)
38 return (dev->features & NETIF_F_ALL_CSUM) != 0;
40 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
42 u32 ethtool_op_get_tx_csum(struct net_device *dev)
44 return (dev->features & NETIF_F_ALL_CSUM) != 0;
46 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
48 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
51 dev->features |= NETIF_F_IP_CSUM;
53 dev->features &= ~NETIF_F_IP_CSUM;
58 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
61 dev->features |= NETIF_F_HW_CSUM;
63 dev->features &= ~NETIF_F_HW_CSUM;
67 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
69 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
72 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
74 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
78 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
80 u32 ethtool_op_get_sg(struct net_device *dev)
82 return (dev->features & NETIF_F_SG) != 0;
84 EXPORT_SYMBOL(ethtool_op_get_sg);
86 int ethtool_op_set_sg(struct net_device *dev, u32 data)
89 dev->features |= NETIF_F_SG;
91 dev->features &= ~NETIF_F_SG;
95 EXPORT_SYMBOL(ethtool_op_set_sg);
97 u32 ethtool_op_get_tso(struct net_device *dev)
99 return (dev->features & NETIF_F_TSO) != 0;
101 EXPORT_SYMBOL(ethtool_op_get_tso);
103 int ethtool_op_set_tso(struct net_device *dev, u32 data)
106 dev->features |= NETIF_F_TSO;
108 dev->features &= ~NETIF_F_TSO;
112 EXPORT_SYMBOL(ethtool_op_set_tso);
114 u32 ethtool_op_get_ufo(struct net_device *dev)
116 return (dev->features & NETIF_F_UFO) != 0;
118 EXPORT_SYMBOL(ethtool_op_get_ufo);
120 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
123 dev->features |= NETIF_F_UFO;
125 dev->features &= ~NETIF_F_UFO;
128 EXPORT_SYMBOL(ethtool_op_set_ufo);
130 /* the following list of flags are the same as their associated
131 * NETIF_F_xxx values in include/linux/netdevice.h
133 static const u32 flags_dup_features =
134 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
136 u32 ethtool_op_get_flags(struct net_device *dev)
138 /* in the future, this function will probably contain additional
139 * handling for flags which are not so easily handled
140 * by a simple masking operation
143 return dev->features & flags_dup_features;
145 EXPORT_SYMBOL(ethtool_op_get_flags);
147 int ethtool_op_set_flags(struct net_device *dev, u32 data)
149 const struct ethtool_ops *ops = dev->ethtool_ops;
150 unsigned long features = dev->features;
152 if (data & ETH_FLAG_LRO)
153 features |= NETIF_F_LRO;
155 features &= ~NETIF_F_LRO;
157 if (data & ETH_FLAG_NTUPLE) {
158 if (!ops->set_rx_ntuple)
160 features |= NETIF_F_NTUPLE;
162 /* safe to clear regardless */
163 features &= ~NETIF_F_NTUPLE;
166 if (data & ETH_FLAG_RXHASH)
167 features |= NETIF_F_RXHASH;
169 features &= ~NETIF_F_RXHASH;
171 dev->features = features;
174 EXPORT_SYMBOL(ethtool_op_set_flags);
176 void ethtool_ntuple_flush(struct net_device *dev)
178 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
180 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
181 list_del(&fsc->list);
184 dev->ethtool_ntuple_list.count = 0;
186 EXPORT_SYMBOL(ethtool_ntuple_flush);
188 /* Handlers for each ethtool command */
190 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
192 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
195 if (!dev->ethtool_ops->get_settings)
198 err = dev->ethtool_ops->get_settings(dev, &cmd);
202 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
207 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
209 struct ethtool_cmd cmd;
211 if (!dev->ethtool_ops->set_settings)
214 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
217 return dev->ethtool_ops->set_settings(dev, &cmd);
220 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
221 void __user *useraddr)
223 struct ethtool_drvinfo info;
224 const struct ethtool_ops *ops = dev->ethtool_ops;
226 if (!ops->get_drvinfo)
229 memset(&info, 0, sizeof(info));
230 info.cmd = ETHTOOL_GDRVINFO;
231 ops->get_drvinfo(dev, &info);
234 * this method of obtaining string set info is deprecated;
235 * Use ETHTOOL_GSSET_INFO instead.
237 if (ops->get_sset_count) {
240 rc = ops->get_sset_count(dev, ETH_SS_TEST);
242 info.testinfo_len = rc;
243 rc = ops->get_sset_count(dev, ETH_SS_STATS);
246 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
248 info.n_priv_flags = rc;
250 if (ops->get_regs_len)
251 info.regdump_len = ops->get_regs_len(dev);
252 if (ops->get_eeprom_len)
253 info.eedump_len = ops->get_eeprom_len(dev);
255 if (copy_to_user(useraddr, &info, sizeof(info)))
260 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
261 void __user *useraddr)
263 struct ethtool_sset_info info;
264 const struct ethtool_ops *ops = dev->ethtool_ops;
266 int i, idx = 0, n_bits = 0, ret, rc;
267 u32 *info_buf = NULL;
269 if (!ops->get_sset_count)
272 if (copy_from_user(&info, useraddr, sizeof(info)))
275 /* store copy of mask, because we zero struct later on */
276 sset_mask = info.sset_mask;
280 /* calculate size of return buffer */
281 n_bits = hweight64(sset_mask);
283 memset(&info, 0, sizeof(info));
284 info.cmd = ETHTOOL_GSSET_INFO;
286 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
291 * fill return buffer based on input bitmask and successful
292 * get_sset_count return
294 for (i = 0; i < 64; i++) {
295 if (!(sset_mask & (1ULL << i)))
298 rc = ops->get_sset_count(dev, i);
300 info.sset_mask |= (1ULL << i);
301 info_buf[idx++] = rc;
306 if (copy_to_user(useraddr, &info, sizeof(info)))
309 useraddr += offsetof(struct ethtool_sset_info, data);
310 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
320 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
321 u32 cmd, void __user *useraddr)
323 struct ethtool_rxnfc info;
324 size_t info_size = sizeof(info);
326 if (!dev->ethtool_ops->set_rxnfc)
329 /* struct ethtool_rxnfc was originally defined for
330 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
331 * members. User-space might still be using that
333 if (cmd == ETHTOOL_SRXFH)
334 info_size = (offsetof(struct ethtool_rxnfc, data) +
337 if (copy_from_user(&info, useraddr, info_size))
340 return dev->ethtool_ops->set_rxnfc(dev, &info);
343 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
344 u32 cmd, void __user *useraddr)
346 struct ethtool_rxnfc info;
347 size_t info_size = sizeof(info);
348 const struct ethtool_ops *ops = dev->ethtool_ops;
350 void *rule_buf = NULL;
355 /* struct ethtool_rxnfc was originally defined for
356 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
357 * members. User-space might still be using that
359 if (cmd == ETHTOOL_GRXFH)
360 info_size = (offsetof(struct ethtool_rxnfc, data) +
363 if (copy_from_user(&info, useraddr, info_size))
366 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
367 if (info.rule_cnt > 0) {
368 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
369 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
376 ret = ops->get_rxnfc(dev, &info, rule_buf);
381 if (copy_to_user(useraddr, &info, info_size))
385 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
386 if (copy_to_user(useraddr, rule_buf,
387 info.rule_cnt * sizeof(u32)))
398 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
399 struct ethtool_rx_ntuple_flow_spec *spec,
400 struct ethtool_rx_ntuple_flow_spec_container *fsc)
403 /* don't add filters forever */
404 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
405 /* free the container */
410 /* Copy the whole filter over */
411 fsc->fs.flow_type = spec->flow_type;
412 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
413 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
415 fsc->fs.vlan_tag = spec->vlan_tag;
416 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
417 fsc->fs.data = spec->data;
418 fsc->fs.data_mask = spec->data_mask;
419 fsc->fs.action = spec->action;
421 /* add to the list */
422 list_add_tail_rcu(&fsc->list, &list->list);
426 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
427 void __user *useraddr)
429 struct ethtool_rx_ntuple cmd;
430 const struct ethtool_ops *ops = dev->ethtool_ops;
431 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
434 if (!(dev->features & NETIF_F_NTUPLE))
437 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
441 * Cache filter in dev struct for GET operation only if
442 * the underlying driver doesn't have its own GET operation, and
443 * only if the filter was added successfully. First make sure we
444 * can allocate the filter, then continue if successful.
446 if (!ops->get_rx_ntuple) {
447 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
452 ret = ops->set_rx_ntuple(dev, &cmd);
458 if (!ops->get_rx_ntuple)
459 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
464 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
466 struct ethtool_gstrings gstrings;
467 const struct ethtool_ops *ops = dev->ethtool_ops;
468 struct ethtool_rx_ntuple_flow_spec_container *fsc;
471 int ret, i, num_strings = 0;
473 if (!ops->get_sset_count)
476 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
479 ret = ops->get_sset_count(dev, gstrings.string_set);
485 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
489 if (ops->get_rx_ntuple) {
490 /* driver-specific filter grab */
491 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
495 /* default ethtool filter grab */
498 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
499 sprintf(p, "Filter %d:\n", i);
500 p += ETH_GSTRING_LEN;
503 switch (fsc->fs.flow_type) {
505 sprintf(p, "\tFlow Type: TCP\n");
506 p += ETH_GSTRING_LEN;
510 sprintf(p, "\tFlow Type: UDP\n");
511 p += ETH_GSTRING_LEN;
515 sprintf(p, "\tFlow Type: SCTP\n");
516 p += ETH_GSTRING_LEN;
520 sprintf(p, "\tFlow Type: AH ESP\n");
521 p += ETH_GSTRING_LEN;
525 sprintf(p, "\tFlow Type: ESP\n");
526 p += ETH_GSTRING_LEN;
530 sprintf(p, "\tFlow Type: Raw IP\n");
531 p += ETH_GSTRING_LEN;
535 sprintf(p, "\tFlow Type: IPv4\n");
536 p += ETH_GSTRING_LEN;
540 sprintf(p, "\tFlow Type: Unknown\n");
541 p += ETH_GSTRING_LEN;
546 /* now the rest of the filters */
547 switch (fsc->fs.flow_type) {
551 sprintf(p, "\tSrc IP addr: 0x%x\n",
552 fsc->fs.h_u.tcp_ip4_spec.ip4src);
553 p += ETH_GSTRING_LEN;
555 sprintf(p, "\tSrc IP mask: 0x%x\n",
556 fsc->fs.m_u.tcp_ip4_spec.ip4src);
557 p += ETH_GSTRING_LEN;
559 sprintf(p, "\tDest IP addr: 0x%x\n",
560 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
561 p += ETH_GSTRING_LEN;
563 sprintf(p, "\tDest IP mask: 0x%x\n",
564 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
565 p += ETH_GSTRING_LEN;
567 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
568 fsc->fs.h_u.tcp_ip4_spec.psrc,
569 fsc->fs.m_u.tcp_ip4_spec.psrc);
570 p += ETH_GSTRING_LEN;
572 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
573 fsc->fs.h_u.tcp_ip4_spec.pdst,
574 fsc->fs.m_u.tcp_ip4_spec.pdst);
575 p += ETH_GSTRING_LEN;
577 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
578 fsc->fs.h_u.tcp_ip4_spec.tos,
579 fsc->fs.m_u.tcp_ip4_spec.tos);
580 p += ETH_GSTRING_LEN;
585 sprintf(p, "\tSrc IP addr: 0x%x\n",
586 fsc->fs.h_u.ah_ip4_spec.ip4src);
587 p += ETH_GSTRING_LEN;
589 sprintf(p, "\tSrc IP mask: 0x%x\n",
590 fsc->fs.m_u.ah_ip4_spec.ip4src);
591 p += ETH_GSTRING_LEN;
593 sprintf(p, "\tDest IP addr: 0x%x\n",
594 fsc->fs.h_u.ah_ip4_spec.ip4dst);
595 p += ETH_GSTRING_LEN;
597 sprintf(p, "\tDest IP mask: 0x%x\n",
598 fsc->fs.m_u.ah_ip4_spec.ip4dst);
599 p += ETH_GSTRING_LEN;
601 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
602 fsc->fs.h_u.ah_ip4_spec.spi,
603 fsc->fs.m_u.ah_ip4_spec.spi);
604 p += ETH_GSTRING_LEN;
606 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
607 fsc->fs.h_u.ah_ip4_spec.tos,
608 fsc->fs.m_u.ah_ip4_spec.tos);
609 p += ETH_GSTRING_LEN;
613 sprintf(p, "\tSrc IP addr: 0x%x\n",
614 fsc->fs.h_u.raw_ip4_spec.ip4src);
615 p += ETH_GSTRING_LEN;
617 sprintf(p, "\tSrc IP mask: 0x%x\n",
618 fsc->fs.m_u.raw_ip4_spec.ip4src);
619 p += ETH_GSTRING_LEN;
621 sprintf(p, "\tDest IP addr: 0x%x\n",
622 fsc->fs.h_u.raw_ip4_spec.ip4dst);
623 p += ETH_GSTRING_LEN;
625 sprintf(p, "\tDest IP mask: 0x%x\n",
626 fsc->fs.m_u.raw_ip4_spec.ip4dst);
627 p += ETH_GSTRING_LEN;
631 sprintf(p, "\tSrc IP addr: 0x%x\n",
632 fsc->fs.h_u.usr_ip4_spec.ip4src);
633 p += ETH_GSTRING_LEN;
635 sprintf(p, "\tSrc IP mask: 0x%x\n",
636 fsc->fs.m_u.usr_ip4_spec.ip4src);
637 p += ETH_GSTRING_LEN;
639 sprintf(p, "\tDest IP addr: 0x%x\n",
640 fsc->fs.h_u.usr_ip4_spec.ip4dst);
641 p += ETH_GSTRING_LEN;
643 sprintf(p, "\tDest IP mask: 0x%x\n",
644 fsc->fs.m_u.usr_ip4_spec.ip4dst);
645 p += ETH_GSTRING_LEN;
647 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
648 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
649 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
650 p += ETH_GSTRING_LEN;
652 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
653 fsc->fs.h_u.usr_ip4_spec.tos,
654 fsc->fs.m_u.usr_ip4_spec.tos);
655 p += ETH_GSTRING_LEN;
657 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
658 fsc->fs.h_u.usr_ip4_spec.ip_ver,
659 fsc->fs.m_u.usr_ip4_spec.ip_ver);
660 p += ETH_GSTRING_LEN;
662 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
663 fsc->fs.h_u.usr_ip4_spec.proto,
664 fsc->fs.m_u.usr_ip4_spec.proto);
665 p += ETH_GSTRING_LEN;
669 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
670 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
671 p += ETH_GSTRING_LEN;
673 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
674 p += ETH_GSTRING_LEN;
676 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
677 p += ETH_GSTRING_LEN;
679 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
680 sprintf(p, "\tAction: Drop\n");
682 sprintf(p, "\tAction: Direct to queue %d\n",
684 p += ETH_GSTRING_LEN;
690 /* indicate to userspace how many strings we actually have */
691 gstrings.len = num_strings;
693 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
695 useraddr += sizeof(gstrings);
696 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
705 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
707 struct ethtool_regs regs;
708 const struct ethtool_ops *ops = dev->ethtool_ops;
712 if (!ops->get_regs || !ops->get_regs_len)
715 if (copy_from_user(®s, useraddr, sizeof(regs)))
718 reglen = ops->get_regs_len(dev);
719 if (regs.len > reglen)
722 regbuf = kmalloc(reglen, GFP_USER);
726 ops->get_regs(dev, ®s, regbuf);
729 if (copy_to_user(useraddr, ®s, sizeof(regs)))
731 useraddr += offsetof(struct ethtool_regs, data);
732 if (copy_to_user(useraddr, regbuf, regs.len))
741 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
743 struct ethtool_value reset;
746 if (!dev->ethtool_ops->reset)
749 if (copy_from_user(&reset, useraddr, sizeof(reset)))
752 ret = dev->ethtool_ops->reset(dev, &reset.data);
756 if (copy_to_user(useraddr, &reset, sizeof(reset)))
761 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
763 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
765 if (!dev->ethtool_ops->get_wol)
768 dev->ethtool_ops->get_wol(dev, &wol);
770 if (copy_to_user(useraddr, &wol, sizeof(wol)))
775 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
777 struct ethtool_wolinfo wol;
779 if (!dev->ethtool_ops->set_wol)
782 if (copy_from_user(&wol, useraddr, sizeof(wol)))
785 return dev->ethtool_ops->set_wol(dev, &wol);
788 static int ethtool_nway_reset(struct net_device *dev)
790 if (!dev->ethtool_ops->nway_reset)
793 return dev->ethtool_ops->nway_reset(dev);
796 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
798 struct ethtool_eeprom eeprom;
799 const struct ethtool_ops *ops = dev->ethtool_ops;
800 void __user *userbuf = useraddr + sizeof(eeprom);
805 if (!ops->get_eeprom || !ops->get_eeprom_len)
808 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
811 /* Check for wrap and zero */
812 if (eeprom.offset + eeprom.len <= eeprom.offset)
815 /* Check for exceeding total eeprom len */
816 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
819 data = kmalloc(PAGE_SIZE, GFP_USER);
823 bytes_remaining = eeprom.len;
824 while (bytes_remaining > 0) {
825 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
827 ret = ops->get_eeprom(dev, &eeprom, data);
830 if (copy_to_user(userbuf, data, eeprom.len)) {
834 userbuf += eeprom.len;
835 eeprom.offset += eeprom.len;
836 bytes_remaining -= eeprom.len;
839 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
840 eeprom.offset -= eeprom.len;
841 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
848 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
850 struct ethtool_eeprom eeprom;
851 const struct ethtool_ops *ops = dev->ethtool_ops;
852 void __user *userbuf = useraddr + sizeof(eeprom);
857 if (!ops->set_eeprom || !ops->get_eeprom_len)
860 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
863 /* Check for wrap and zero */
864 if (eeprom.offset + eeprom.len <= eeprom.offset)
867 /* Check for exceeding total eeprom len */
868 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
871 data = kmalloc(PAGE_SIZE, GFP_USER);
875 bytes_remaining = eeprom.len;
876 while (bytes_remaining > 0) {
877 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
879 if (copy_from_user(data, userbuf, eeprom.len)) {
883 ret = ops->set_eeprom(dev, &eeprom, data);
886 userbuf += eeprom.len;
887 eeprom.offset += eeprom.len;
888 bytes_remaining -= eeprom.len;
895 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
896 void __user *useraddr)
898 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
900 if (!dev->ethtool_ops->get_coalesce)
903 dev->ethtool_ops->get_coalesce(dev, &coalesce);
905 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
910 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
911 void __user *useraddr)
913 struct ethtool_coalesce coalesce;
915 if (!dev->ethtool_ops->set_coalesce)
918 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
921 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
924 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
926 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
928 if (!dev->ethtool_ops->get_ringparam)
931 dev->ethtool_ops->get_ringparam(dev, &ringparam);
933 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
938 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
940 struct ethtool_ringparam ringparam;
942 if (!dev->ethtool_ops->set_ringparam)
945 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
948 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
951 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
953 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
955 if (!dev->ethtool_ops->get_pauseparam)
958 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
960 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
965 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
967 struct ethtool_pauseparam pauseparam;
969 if (!dev->ethtool_ops->set_pauseparam)
972 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
975 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
978 static int __ethtool_set_sg(struct net_device *dev, u32 data)
982 if (!data && dev->ethtool_ops->set_tso) {
983 err = dev->ethtool_ops->set_tso(dev, 0);
988 if (!data && dev->ethtool_ops->set_ufo) {
989 err = dev->ethtool_ops->set_ufo(dev, 0);
993 return dev->ethtool_ops->set_sg(dev, data);
996 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
998 struct ethtool_value edata;
1001 if (!dev->ethtool_ops->set_tx_csum)
1004 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1007 if (!edata.data && dev->ethtool_ops->set_sg) {
1008 err = __ethtool_set_sg(dev, 0);
1013 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1015 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1017 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1019 struct ethtool_value edata;
1021 if (!dev->ethtool_ops->set_rx_csum)
1024 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1027 if (!edata.data && dev->ethtool_ops->set_sg)
1028 dev->features &= ~NETIF_F_GRO;
1030 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1033 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1035 struct ethtool_value edata;
1037 if (!dev->ethtool_ops->set_sg)
1040 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1044 !(dev->features & NETIF_F_ALL_CSUM))
1047 return __ethtool_set_sg(dev, edata.data);
1050 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1052 struct ethtool_value edata;
1054 if (!dev->ethtool_ops->set_tso)
1057 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1060 if (edata.data && !(dev->features & NETIF_F_SG))
1063 return dev->ethtool_ops->set_tso(dev, edata.data);
1066 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1068 struct ethtool_value edata;
1070 if (!dev->ethtool_ops->set_ufo)
1072 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1074 if (edata.data && !(dev->features & NETIF_F_SG))
1076 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1078 return dev->ethtool_ops->set_ufo(dev, edata.data);
1081 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1083 struct ethtool_value edata = { ETHTOOL_GGSO };
1085 edata.data = dev->features & NETIF_F_GSO;
1086 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1091 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1093 struct ethtool_value edata;
1095 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1098 dev->features |= NETIF_F_GSO;
1100 dev->features &= ~NETIF_F_GSO;
1104 static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1106 struct ethtool_value edata = { ETHTOOL_GGRO };
1108 edata.data = dev->features & NETIF_F_GRO;
1109 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1114 static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1116 struct ethtool_value edata;
1118 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1122 if (!dev->ethtool_ops->get_rx_csum ||
1123 !dev->ethtool_ops->get_rx_csum(dev))
1125 dev->features |= NETIF_F_GRO;
1127 dev->features &= ~NETIF_F_GRO;
1132 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1134 struct ethtool_test test;
1135 const struct ethtool_ops *ops = dev->ethtool_ops;
1139 if (!ops->self_test || !ops->get_sset_count)
1142 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1145 WARN_ON(test_len == 0);
1147 if (copy_from_user(&test, useraddr, sizeof(test)))
1150 test.len = test_len;
1151 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1155 ops->self_test(dev, &test, data);
1158 if (copy_to_user(useraddr, &test, sizeof(test)))
1160 useraddr += sizeof(test);
1161 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1170 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1172 struct ethtool_gstrings gstrings;
1173 const struct ethtool_ops *ops = dev->ethtool_ops;
1177 if (!ops->get_strings || !ops->get_sset_count)
1180 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1183 ret = ops->get_sset_count(dev, gstrings.string_set);
1189 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1193 ops->get_strings(dev, gstrings.string_set, data);
1196 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1198 useraddr += sizeof(gstrings);
1199 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1208 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1210 struct ethtool_value id;
1212 if (!dev->ethtool_ops->phys_id)
1215 if (copy_from_user(&id, useraddr, sizeof(id)))
1218 return dev->ethtool_ops->phys_id(dev, id.data);
1221 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1223 struct ethtool_stats stats;
1224 const struct ethtool_ops *ops = dev->ethtool_ops;
1228 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1231 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1234 WARN_ON(n_stats == 0);
1236 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1239 stats.n_stats = n_stats;
1240 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1244 ops->get_ethtool_stats(dev, &stats, data);
1247 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1249 useraddr += sizeof(stats);
1250 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1259 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1261 struct ethtool_perm_addr epaddr;
1263 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1266 if (epaddr.size < dev->addr_len)
1268 epaddr.size = dev->addr_len;
1270 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1272 useraddr += sizeof(epaddr);
1273 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1278 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1279 u32 cmd, u32 (*actor)(struct net_device *))
1281 struct ethtool_value edata = { .cmd = cmd };
1286 edata.data = actor(dev);
1288 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1293 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1294 void (*actor)(struct net_device *, u32))
1296 struct ethtool_value edata;
1301 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1304 actor(dev, edata.data);
1308 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1309 int (*actor)(struct net_device *, u32))
1311 struct ethtool_value edata;
1316 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1319 return actor(dev, edata.data);
1322 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1323 char __user *useraddr)
1325 struct ethtool_flash efl;
1327 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1330 if (!dev->ethtool_ops->flash_device)
1333 return dev->ethtool_ops->flash_device(dev, &efl);
1336 /* The main entry point in this file. Called from net/core/dev.c */
1338 int dev_ethtool(struct net *net, struct ifreq *ifr)
1340 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1341 void __user *useraddr = ifr->ifr_data;
1344 unsigned long old_features;
1346 if (!dev || !netif_device_present(dev))
1349 if (!dev->ethtool_ops)
1352 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
1355 /* Allow some commands to be done by anyone */
1357 case ETHTOOL_GDRVINFO:
1358 case ETHTOOL_GMSGLVL:
1359 case ETHTOOL_GCOALESCE:
1360 case ETHTOOL_GRINGPARAM:
1361 case ETHTOOL_GPAUSEPARAM:
1362 case ETHTOOL_GRXCSUM:
1363 case ETHTOOL_GTXCSUM:
1365 case ETHTOOL_GSTRINGS:
1367 case ETHTOOL_GPERMADDR:
1371 case ETHTOOL_GFLAGS:
1372 case ETHTOOL_GPFLAGS:
1374 case ETHTOOL_GRXRINGS:
1375 case ETHTOOL_GRXCLSRLCNT:
1376 case ETHTOOL_GRXCLSRULE:
1377 case ETHTOOL_GRXCLSRLALL:
1380 if (!capable(CAP_NET_ADMIN))
1384 if (dev->ethtool_ops->begin) {
1385 rc = dev->ethtool_ops->begin(dev);
1389 old_features = dev->features;
1393 rc = ethtool_get_settings(dev, useraddr);
1396 rc = ethtool_set_settings(dev, useraddr);
1398 case ETHTOOL_GDRVINFO:
1399 rc = ethtool_get_drvinfo(dev, useraddr);
1402 rc = ethtool_get_regs(dev, useraddr);
1405 rc = ethtool_get_wol(dev, useraddr);
1408 rc = ethtool_set_wol(dev, useraddr);
1410 case ETHTOOL_GMSGLVL:
1411 rc = ethtool_get_value(dev, useraddr, ethcmd,
1412 dev->ethtool_ops->get_msglevel);
1414 case ETHTOOL_SMSGLVL:
1415 rc = ethtool_set_value_void(dev, useraddr,
1416 dev->ethtool_ops->set_msglevel);
1418 case ETHTOOL_NWAY_RST:
1419 rc = ethtool_nway_reset(dev);
1422 rc = ethtool_get_value(dev, useraddr, ethcmd,
1423 dev->ethtool_ops->get_link);
1425 case ETHTOOL_GEEPROM:
1426 rc = ethtool_get_eeprom(dev, useraddr);
1428 case ETHTOOL_SEEPROM:
1429 rc = ethtool_set_eeprom(dev, useraddr);
1431 case ETHTOOL_GCOALESCE:
1432 rc = ethtool_get_coalesce(dev, useraddr);
1434 case ETHTOOL_SCOALESCE:
1435 rc = ethtool_set_coalesce(dev, useraddr);
1437 case ETHTOOL_GRINGPARAM:
1438 rc = ethtool_get_ringparam(dev, useraddr);
1440 case ETHTOOL_SRINGPARAM:
1441 rc = ethtool_set_ringparam(dev, useraddr);
1443 case ETHTOOL_GPAUSEPARAM:
1444 rc = ethtool_get_pauseparam(dev, useraddr);
1446 case ETHTOOL_SPAUSEPARAM:
1447 rc = ethtool_set_pauseparam(dev, useraddr);
1449 case ETHTOOL_GRXCSUM:
1450 rc = ethtool_get_value(dev, useraddr, ethcmd,
1451 (dev->ethtool_ops->get_rx_csum ?
1452 dev->ethtool_ops->get_rx_csum :
1453 ethtool_op_get_rx_csum));
1455 case ETHTOOL_SRXCSUM:
1456 rc = ethtool_set_rx_csum(dev, useraddr);
1458 case ETHTOOL_GTXCSUM:
1459 rc = ethtool_get_value(dev, useraddr, ethcmd,
1460 (dev->ethtool_ops->get_tx_csum ?
1461 dev->ethtool_ops->get_tx_csum :
1462 ethtool_op_get_tx_csum));
1464 case ETHTOOL_STXCSUM:
1465 rc = ethtool_set_tx_csum(dev, useraddr);
1468 rc = ethtool_get_value(dev, useraddr, ethcmd,
1469 (dev->ethtool_ops->get_sg ?
1470 dev->ethtool_ops->get_sg :
1471 ethtool_op_get_sg));
1474 rc = ethtool_set_sg(dev, useraddr);
1477 rc = ethtool_get_value(dev, useraddr, ethcmd,
1478 (dev->ethtool_ops->get_tso ?
1479 dev->ethtool_ops->get_tso :
1480 ethtool_op_get_tso));
1483 rc = ethtool_set_tso(dev, useraddr);
1486 rc = ethtool_self_test(dev, useraddr);
1488 case ETHTOOL_GSTRINGS:
1489 rc = ethtool_get_strings(dev, useraddr);
1491 case ETHTOOL_PHYS_ID:
1492 rc = ethtool_phys_id(dev, useraddr);
1494 case ETHTOOL_GSTATS:
1495 rc = ethtool_get_stats(dev, useraddr);
1497 case ETHTOOL_GPERMADDR:
1498 rc = ethtool_get_perm_addr(dev, useraddr);
1501 rc = ethtool_get_value(dev, useraddr, ethcmd,
1502 (dev->ethtool_ops->get_ufo ?
1503 dev->ethtool_ops->get_ufo :
1504 ethtool_op_get_ufo));
1507 rc = ethtool_set_ufo(dev, useraddr);
1510 rc = ethtool_get_gso(dev, useraddr);
1513 rc = ethtool_set_gso(dev, useraddr);
1515 case ETHTOOL_GFLAGS:
1516 rc = ethtool_get_value(dev, useraddr, ethcmd,
1517 (dev->ethtool_ops->get_flags ?
1518 dev->ethtool_ops->get_flags :
1519 ethtool_op_get_flags));
1521 case ETHTOOL_SFLAGS:
1522 rc = ethtool_set_value(dev, useraddr,
1523 dev->ethtool_ops->set_flags);
1525 case ETHTOOL_GPFLAGS:
1526 rc = ethtool_get_value(dev, useraddr, ethcmd,
1527 dev->ethtool_ops->get_priv_flags);
1529 case ETHTOOL_SPFLAGS:
1530 rc = ethtool_set_value(dev, useraddr,
1531 dev->ethtool_ops->set_priv_flags);
1534 case ETHTOOL_GRXRINGS:
1535 case ETHTOOL_GRXCLSRLCNT:
1536 case ETHTOOL_GRXCLSRULE:
1537 case ETHTOOL_GRXCLSRLALL:
1538 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1541 case ETHTOOL_SRXCLSRLDEL:
1542 case ETHTOOL_SRXCLSRLINS:
1543 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1546 rc = ethtool_get_gro(dev, useraddr);
1549 rc = ethtool_set_gro(dev, useraddr);
1551 case ETHTOOL_FLASHDEV:
1552 rc = ethtool_flash_device(dev, useraddr);
1555 rc = ethtool_reset(dev, useraddr);
1557 case ETHTOOL_SRXNTUPLE:
1558 rc = ethtool_set_rx_ntuple(dev, useraddr);
1560 case ETHTOOL_GRXNTUPLE:
1561 rc = ethtool_get_rx_ntuple(dev, useraddr);
1563 case ETHTOOL_GSSET_INFO:
1564 rc = ethtool_get_sset_info(dev, useraddr);
1570 if (dev->ethtool_ops->complete)
1571 dev->ethtool_ops->complete(dev);
1573 if (old_features != dev->features)
1574 netdev_features_change(dev);