Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/core/ethtool.c - Ethtool ioctl handler |
| 3 | * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx> |
| 4 | * |
| 5 | * This file is where we call all the ethtool_ops commands to get |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 6 | * the information ethtool needs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/ethtool.h> |
| 19 | #include <linux/netdevice.h> |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 20 | #include <linux/net_tstamp.h> |
| 21 | #include <linux/phy.h> |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 22 | #include <linux/bitops.h> |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
David S. Miller | 73da16c | 2010-09-21 16:12:11 -0700 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Russell King | e679c9c | 2018-03-28 15:44:16 -0700 | [diff] [blame] | 25 | #include <linux/sfp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 27 | #include <linux/rtnetlink.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 28 | #include <linux/sched/signal.h> |
Eric Dumazet | 960fb62 | 2014-11-16 06:23:05 -0800 | [diff] [blame] | 29 | #include <linux/net.h> |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 30 | #include <net/devlink.h> |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 31 | #include <net/xdp_sock.h> |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 32 | #include <net/flow_offload.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 34 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * Some useful ethtool_ops methods that're device independent. |
| 36 | * If we find that all drivers want to do the same thing here, |
| 37 | * we can turn these into dev_() function calls. |
| 38 | */ |
| 39 | |
| 40 | u32 ethtool_op_get_link(struct net_device *dev) |
| 41 | { |
| 42 | return netif_carrier_ok(dev) ? 1 : 0; |
| 43 | } |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 44 | EXPORT_SYMBOL(ethtool_op_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Richard Cochran | 02eacbd | 2012-04-03 22:59:22 +0000 | [diff] [blame] | 46 | int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) |
| 47 | { |
| 48 | info->so_timestamping = |
| 49 | SOF_TIMESTAMPING_TX_SOFTWARE | |
| 50 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 51 | SOF_TIMESTAMPING_SOFTWARE; |
| 52 | info->phc_index = -1; |
| 53 | return 0; |
| 54 | } |
| 55 | EXPORT_SYMBOL(ethtool_op_get_ts_info); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* Handlers for each ethtool command */ |
| 58 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 59 | #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 60 | |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 61 | static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = { |
| 62 | [NETIF_F_SG_BIT] = "tx-scatter-gather", |
| 63 | [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 64 | [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic", |
| 65 | [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6", |
| 66 | [NETIF_F_HIGHDMA_BIT] = "highdma", |
| 67 | [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist", |
Fernando Luis Vazquez Cao | 788dfca | 2013-06-17 11:28:03 +0900 | [diff] [blame] | 68 | [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 69 | |
Fernando Luis Vazquez Cao | 788dfca | 2013-06-17 11:28:03 +0900 | [diff] [blame] | 70 | [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse", |
| 71 | [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter", |
David S. Miller | 2d6577f | 2013-04-19 16:08:23 -0400 | [diff] [blame] | 72 | [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert", |
| 73 | [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse", |
| 74 | [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 75 | [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged", |
| 76 | [NETIF_F_GSO_BIT] = "tx-generic-segmentation", |
| 77 | [NETIF_F_LLTX_BIT] = "tx-lockless", |
| 78 | [NETIF_F_NETNS_LOCAL_BIT] = "netns-local", |
| 79 | [NETIF_F_GRO_BIT] = "rx-gro", |
Michael Chan | fb1f5f7 | 2017-12-16 03:09:40 -0500 | [diff] [blame] | 80 | [NETIF_F_GRO_HW_BIT] = "rx-gro-hw", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 81 | [NETIF_F_LRO_BIT] = "rx-lro", |
| 82 | |
| 83 | [NETIF_F_TSO_BIT] = "tx-tcp-segmentation", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 84 | [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust", |
| 85 | [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation", |
Alexander Duyck | cbc53e0 | 2016-04-10 21:44:51 -0400 | [diff] [blame] | 86 | [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 87 | [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation", |
| 88 | [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation", |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 89 | [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation", |
Alexander Duyck | 518f213 | 2016-04-10 21:44:44 -0400 | [diff] [blame] | 90 | [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation", |
Tom Herbert | 7e13318 | 2016-05-18 09:06:10 -0700 | [diff] [blame] | 91 | [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation", |
| 92 | [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation", |
Pravin B Shelar | 7313626 | 2013-03-07 13:21:51 +0000 | [diff] [blame] | 93 | [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation", |
Alexander Duyck | 518f213 | 2016-04-10 21:44:44 -0400 | [diff] [blame] | 94 | [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation", |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 95 | [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial", |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 96 | [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation", |
Steffen Klassert | c7ef8f0 | 2017-04-14 10:05:36 +0200 | [diff] [blame] | 97 | [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation", |
Willem de Bruijn | 83aa025 | 2018-04-26 13:42:21 -0400 | [diff] [blame] | 98 | [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 99 | |
| 100 | [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc", |
Tom Herbert | 53692b1 | 2015-12-14 11:19:41 -0800 | [diff] [blame] | 101 | [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 102 | [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu", |
| 103 | [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter", |
| 104 | [NETIF_F_RXHASH_BIT] = "rx-hashing", |
| 105 | [NETIF_F_RXCSUM_BIT] = "rx-checksum", |
| 106 | [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy", |
| 107 | [NETIF_F_LOOPBACK_BIT] = "loopback", |
Ben Greear | 36eabda3 | 2012-02-11 15:39:14 +0000 | [diff] [blame] | 108 | [NETIF_F_RXFCS_BIT] = "rx-fcs", |
Ben Greear | 5e0c03c | 2012-02-11 15:39:45 +0000 | [diff] [blame] | 109 | [NETIF_F_RXALL_BIT] = "rx-all", |
John Fastabend | a6cc0cf | 2013-11-06 09:54:46 -0800 | [diff] [blame] | 110 | [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload", |
John Fastabend | 1c78c64 | 2016-02-16 21:17:37 -0800 | [diff] [blame] | 111 | [NETIF_F_HW_TC_BIT] = "hw-tc-offload", |
Steffen Klassert | c7ef8f0 | 2017-04-14 10:05:36 +0200 | [diff] [blame] | 112 | [NETIF_F_HW_ESP_BIT] = "esp-hw-offload", |
| 113 | [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload", |
Sabrina Dubroca | d764a12 | 2017-07-21 12:49:28 +0200 | [diff] [blame] | 114 | [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload", |
Atul Gupta | e0be6be | 2018-03-31 21:41:53 +0530 | [diff] [blame] | 115 | [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record", |
Ilya Lesokhin | 2342a85 | 2018-04-30 10:16:14 +0300 | [diff] [blame] | 116 | [NETIF_F_HW_TLS_TX_BIT] = "tls-hw-tx-offload", |
Ilya Lesokhin | 1413656 | 2018-07-13 14:33:36 +0300 | [diff] [blame] | 117 | [NETIF_F_HW_TLS_RX_BIT] = "tls-hw-rx-offload", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 120 | static const char |
| 121 | rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = { |
| 122 | [ETH_RSS_HASH_TOP_BIT] = "toeplitz", |
| 123 | [ETH_RSS_HASH_XOR_BIT] = "xor", |
Jakub Kicinski | abb521e | 2017-03-08 08:57:00 -0800 | [diff] [blame] | 124 | [ETH_RSS_HASH_CRC32_BIT] = "crc32", |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 127 | static const char |
| 128 | tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = { |
| 129 | [ETHTOOL_ID_UNSPEC] = "Unspec", |
| 130 | [ETHTOOL_RX_COPYBREAK] = "rx-copybreak", |
| 131 | [ETHTOOL_TX_COPYBREAK] = "tx-copybreak", |
Inbar Karmy | e1577c1 | 2017-11-20 16:14:30 +0200 | [diff] [blame] | 132 | [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout", |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 133 | }; |
| 134 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 135 | static const char |
| 136 | phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = { |
| 137 | [ETHTOOL_ID_UNSPEC] = "Unspec", |
Raju Lakkaraju | 65feddd | 2016-11-17 13:07:23 +0100 | [diff] [blame] | 138 | [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift", |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 141 | static int ethtool_get_features(struct net_device *dev, void __user *useraddr) |
| 142 | { |
| 143 | struct ethtool_gfeatures cmd = { |
| 144 | .cmd = ETHTOOL_GFEATURES, |
| 145 | .size = ETHTOOL_DEV_FEATURE_WORDS, |
| 146 | }; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 147 | struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 148 | u32 __user *sizeaddr; |
| 149 | u32 copy_size; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 150 | int i; |
| 151 | |
| 152 | /* in case feature bits run out again */ |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 153 | BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t)); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 154 | |
| 155 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 156 | features[i].available = (u32)(dev->hw_features >> (32 * i)); |
| 157 | features[i].requested = (u32)(dev->wanted_features >> (32 * i)); |
| 158 | features[i].active = (u32)(dev->features >> (32 * i)); |
| 159 | features[i].never_changed = |
| 160 | (u32)(NETIF_F_NEVER_CHANGE >> (32 * i)); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 161 | } |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 162 | |
| 163 | sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size); |
| 164 | if (get_user(copy_size, sizeaddr)) |
| 165 | return -EFAULT; |
| 166 | |
| 167 | if (copy_size > ETHTOOL_DEV_FEATURE_WORDS) |
| 168 | copy_size = ETHTOOL_DEV_FEATURE_WORDS; |
| 169 | |
| 170 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 171 | return -EFAULT; |
| 172 | useraddr += sizeof(cmd); |
| 173 | if (copy_to_user(useraddr, features, copy_size * sizeof(*features))) |
| 174 | return -EFAULT; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int ethtool_set_features(struct net_device *dev, void __user *useraddr) |
| 180 | { |
| 181 | struct ethtool_sfeatures cmd; |
| 182 | struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 183 | netdev_features_t wanted = 0, valid = 0; |
| 184 | int i, ret = 0; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 185 | |
| 186 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 187 | return -EFAULT; |
| 188 | useraddr += sizeof(cmd); |
| 189 | |
| 190 | if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | if (copy_from_user(features, useraddr, sizeof(features))) |
| 194 | return -EFAULT; |
| 195 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 196 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 197 | valid |= (netdev_features_t)features[i].valid << (32 * i); |
| 198 | wanted |= (netdev_features_t)features[i].requested << (32 * i); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (valid & ~NETIF_F_ETHTOOL_BITS) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 204 | if (valid & ~dev->hw_features) { |
| 205 | valid &= dev->hw_features; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 206 | ret |= ETHTOOL_F_UNSUPPORTED; |
| 207 | } |
| 208 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 209 | dev->wanted_features &= ~valid; |
| 210 | dev->wanted_features |= wanted & valid; |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 211 | __netdev_update_features(dev); |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 212 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 213 | if ((dev->wanted_features ^ dev->features) & valid) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 214 | ret |= ETHTOOL_F_WISH; |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 219 | static int __ethtool_get_sset_count(struct net_device *dev, int sset) |
| 220 | { |
| 221 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 222 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 223 | if (sset == ETH_SS_FEATURES) |
| 224 | return ARRAY_SIZE(netdev_features_strings); |
| 225 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 226 | if (sset == ETH_SS_RSS_HASH_FUNCS) |
| 227 | return ARRAY_SIZE(rss_hash_func_strings); |
| 228 | |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 229 | if (sset == ETH_SS_TUNABLES) |
| 230 | return ARRAY_SIZE(tunable_strings); |
| 231 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 232 | if (sset == ETH_SS_PHY_TUNABLES) |
| 233 | return ARRAY_SIZE(phy_tunable_strings); |
| 234 | |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 235 | if (sset == ETH_SS_PHY_STATS && dev->phydev && |
| 236 | !ops->get_ethtool_phy_stats) |
| 237 | return phy_ethtool_get_sset_count(dev->phydev); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 238 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 239 | if (ops->get_sset_count && ops->get_strings) |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 240 | return ops->get_sset_count(dev, sset); |
| 241 | else |
| 242 | return -EOPNOTSUPP; |
| 243 | } |
| 244 | |
| 245 | static void __ethtool_get_strings(struct net_device *dev, |
| 246 | u32 stringset, u8 *data) |
| 247 | { |
| 248 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 249 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 250 | if (stringset == ETH_SS_FEATURES) |
| 251 | memcpy(data, netdev_features_strings, |
| 252 | sizeof(netdev_features_strings)); |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 253 | else if (stringset == ETH_SS_RSS_HASH_FUNCS) |
| 254 | memcpy(data, rss_hash_func_strings, |
| 255 | sizeof(rss_hash_func_strings)); |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 256 | else if (stringset == ETH_SS_TUNABLES) |
| 257 | memcpy(data, tunable_strings, sizeof(tunable_strings)); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 258 | else if (stringset == ETH_SS_PHY_TUNABLES) |
| 259 | memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings)); |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 260 | else if (stringset == ETH_SS_PHY_STATS && dev->phydev && |
| 261 | !ops->get_ethtool_phy_stats) |
| 262 | phy_ethtool_get_strings(dev->phydev, data); |
| 263 | else |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 264 | /* ops->get_strings is valid because checked earlier */ |
| 265 | ops->get_strings(dev, stringset, data); |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 268 | static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd) |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 269 | { |
| 270 | /* feature masks of legacy discrete ethtool ops */ |
| 271 | |
| 272 | switch (eth_cmd) { |
| 273 | case ETHTOOL_GTXCSUM: |
| 274 | case ETHTOOL_STXCSUM: |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 275 | return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC; |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 276 | case ETHTOOL_GRXCSUM: |
| 277 | case ETHTOOL_SRXCSUM: |
| 278 | return NETIF_F_RXCSUM; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 279 | case ETHTOOL_GSG: |
| 280 | case ETHTOOL_SSG: |
| 281 | return NETIF_F_SG; |
| 282 | case ETHTOOL_GTSO: |
| 283 | case ETHTOOL_STSO: |
| 284 | return NETIF_F_ALL_TSO; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 285 | case ETHTOOL_GGSO: |
| 286 | case ETHTOOL_SGSO: |
| 287 | return NETIF_F_GSO; |
| 288 | case ETHTOOL_GGRO: |
| 289 | case ETHTOOL_SGRO: |
| 290 | return NETIF_F_GRO; |
| 291 | default: |
| 292 | BUG(); |
| 293 | } |
| 294 | } |
| 295 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 296 | static int ethtool_get_one_feature(struct net_device *dev, |
| 297 | char __user *useraddr, u32 ethcmd) |
| 298 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 299 | netdev_features_t mask = ethtool_get_feature_mask(ethcmd); |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 300 | struct ethtool_value edata = { |
| 301 | .cmd = ethcmd, |
Michał Mirosław | 8679488 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 302 | .data = !!(dev->features & mask), |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 303 | }; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 304 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 305 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 306 | return -EFAULT; |
| 307 | return 0; |
| 308 | } |
| 309 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 310 | static int ethtool_set_one_feature(struct net_device *dev, |
| 311 | void __user *useraddr, u32 ethcmd) |
| 312 | { |
| 313 | struct ethtool_value edata; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 314 | netdev_features_t mask; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 315 | |
| 316 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 317 | return -EFAULT; |
| 318 | |
Michał Mirosław | 8679488 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 319 | mask = ethtool_get_feature_mask(ethcmd); |
| 320 | mask &= dev->hw_features; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 321 | if (!mask) |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 322 | return -EOPNOTSUPP; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 323 | |
| 324 | if (edata.data) |
| 325 | dev->wanted_features |= mask; |
| 326 | else |
| 327 | dev->wanted_features &= ~mask; |
| 328 | |
| 329 | __netdev_update_features(dev); |
| 330 | |
| 331 | return 0; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 334 | #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \ |
| 335 | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH) |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 336 | #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \ |
| 337 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \ |
| 338 | NETIF_F_RXHASH) |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 339 | |
| 340 | static u32 __ethtool_get_flags(struct net_device *dev) |
| 341 | { |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 342 | u32 flags = 0; |
| 343 | |
Dragos Foianu | 8a73125 | 2013-07-13 14:43:00 +0100 | [diff] [blame] | 344 | if (dev->features & NETIF_F_LRO) |
| 345 | flags |= ETH_FLAG_LRO; |
| 346 | if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) |
| 347 | flags |= ETH_FLAG_RXVLAN; |
| 348 | if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) |
| 349 | flags |= ETH_FLAG_TXVLAN; |
| 350 | if (dev->features & NETIF_F_NTUPLE) |
| 351 | flags |= ETH_FLAG_NTUPLE; |
| 352 | if (dev->features & NETIF_F_RXHASH) |
| 353 | flags |= ETH_FLAG_RXHASH; |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 354 | |
| 355 | return flags; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static int __ethtool_set_flags(struct net_device *dev, u32 data) |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 359 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 360 | netdev_features_t features = 0, changed; |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 361 | |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 362 | if (data & ~ETH_ALL_FLAGS) |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | |
Dragos Foianu | 8a73125 | 2013-07-13 14:43:00 +0100 | [diff] [blame] | 365 | if (data & ETH_FLAG_LRO) |
| 366 | features |= NETIF_F_LRO; |
| 367 | if (data & ETH_FLAG_RXVLAN) |
| 368 | features |= NETIF_F_HW_VLAN_CTAG_RX; |
| 369 | if (data & ETH_FLAG_TXVLAN) |
| 370 | features |= NETIF_F_HW_VLAN_CTAG_TX; |
| 371 | if (data & ETH_FLAG_NTUPLE) |
| 372 | features |= NETIF_F_NTUPLE; |
| 373 | if (data & ETH_FLAG_RXHASH) |
| 374 | features |= NETIF_F_RXHASH; |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 375 | |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 376 | /* allow changing only bits set in hw_features */ |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 377 | changed = (features ^ dev->features) & ETH_ALL_FEATURES; |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 378 | if (changed & ~dev->hw_features) |
| 379 | return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP; |
| 380 | |
| 381 | dev->wanted_features = |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 382 | (dev->wanted_features & ~changed) | (features & changed); |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 383 | |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 384 | __netdev_update_features(dev); |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Alan Brady | 5a6cd6d | 2017-10-05 14:53:40 -0700 | [diff] [blame] | 389 | /* Given two link masks, AND them together and save the result in dst. */ |
| 390 | void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst, |
| 391 | struct ethtool_link_ksettings *src) |
| 392 | { |
| 393 | unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS); |
| 394 | unsigned int idx = 0; |
| 395 | |
| 396 | for (; idx < size; idx++) { |
| 397 | dst->link_modes.supported[idx] &= |
| 398 | src->link_modes.supported[idx]; |
| 399 | dst->link_modes.advertising[idx] &= |
| 400 | src->link_modes.advertising[idx]; |
| 401 | } |
| 402 | } |
| 403 | EXPORT_SYMBOL(ethtool_intersect_link_masks); |
| 404 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 405 | void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst, |
| 406 | u32 legacy_u32) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 407 | { |
| 408 | bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 409 | dst[0] = legacy_u32; |
| 410 | } |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 411 | EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 412 | |
| 413 | /* return false if src had higher bits set. lower bits always updated. */ |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 414 | bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32, |
| 415 | const unsigned long *src) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 416 | { |
| 417 | bool retval = true; |
| 418 | |
| 419 | /* TODO: following test will soon always be true */ |
| 420 | if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) { |
| 421 | __ETHTOOL_DECLARE_LINK_MODE_MASK(ext); |
| 422 | |
| 423 | bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 424 | bitmap_fill(ext, 32); |
| 425 | bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 426 | if (bitmap_intersects(ext, src, |
| 427 | __ETHTOOL_LINK_MODE_MASK_NBITS)) { |
| 428 | /* src mask goes beyond bit 31 */ |
| 429 | retval = false; |
| 430 | } |
| 431 | } |
| 432 | *legacy_u32 = src[0]; |
| 433 | return retval; |
| 434 | } |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 435 | EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 436 | |
| 437 | /* return false if legacy contained non-0 deprecated fields |
Niklas Söderlund | 95491e3 | 2017-10-20 01:32:08 +0200 | [diff] [blame] | 438 | * maxtxpkt/maxrxpkt. rest of ksettings always updated |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 439 | */ |
| 440 | static bool |
| 441 | convert_legacy_settings_to_link_ksettings( |
| 442 | struct ethtool_link_ksettings *link_ksettings, |
| 443 | const struct ethtool_cmd *legacy_settings) |
| 444 | { |
| 445 | bool retval = true; |
| 446 | |
| 447 | memset(link_ksettings, 0, sizeof(*link_ksettings)); |
| 448 | |
| 449 | /* This is used to tell users that driver is still using these |
| 450 | * deprecated legacy fields, and they should not use |
| 451 | * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS |
| 452 | */ |
Niklas Söderlund | 95491e3 | 2017-10-20 01:32:08 +0200 | [diff] [blame] | 453 | if (legacy_settings->maxtxpkt || |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 454 | legacy_settings->maxrxpkt) |
| 455 | retval = false; |
| 456 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 457 | ethtool_convert_legacy_u32_to_link_mode( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 458 | link_ksettings->link_modes.supported, |
| 459 | legacy_settings->supported); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 460 | ethtool_convert_legacy_u32_to_link_mode( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 461 | link_ksettings->link_modes.advertising, |
| 462 | legacy_settings->advertising); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 463 | ethtool_convert_legacy_u32_to_link_mode( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 464 | link_ksettings->link_modes.lp_advertising, |
| 465 | legacy_settings->lp_advertising); |
| 466 | link_ksettings->base.speed |
| 467 | = ethtool_cmd_speed(legacy_settings); |
| 468 | link_ksettings->base.duplex |
| 469 | = legacy_settings->duplex; |
| 470 | link_ksettings->base.port |
| 471 | = legacy_settings->port; |
| 472 | link_ksettings->base.phy_address |
| 473 | = legacy_settings->phy_address; |
| 474 | link_ksettings->base.autoneg |
| 475 | = legacy_settings->autoneg; |
| 476 | link_ksettings->base.mdio_support |
| 477 | = legacy_settings->mdio_support; |
| 478 | link_ksettings->base.eth_tp_mdix |
| 479 | = legacy_settings->eth_tp_mdix; |
| 480 | link_ksettings->base.eth_tp_mdix_ctrl |
| 481 | = legacy_settings->eth_tp_mdix_ctrl; |
| 482 | return retval; |
| 483 | } |
| 484 | |
| 485 | /* return false if ksettings link modes had higher bits |
| 486 | * set. legacy_settings always updated (best effort) |
| 487 | */ |
| 488 | static bool |
| 489 | convert_link_ksettings_to_legacy_settings( |
| 490 | struct ethtool_cmd *legacy_settings, |
| 491 | const struct ethtool_link_ksettings *link_ksettings) |
| 492 | { |
| 493 | bool retval = true; |
| 494 | |
| 495 | memset(legacy_settings, 0, sizeof(*legacy_settings)); |
| 496 | /* this also clears the deprecated fields in legacy structure: |
| 497 | * __u8 transceiver; |
| 498 | * __u32 maxtxpkt; |
| 499 | * __u32 maxrxpkt; |
| 500 | */ |
| 501 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 502 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 503 | &legacy_settings->supported, |
| 504 | link_ksettings->link_modes.supported); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 505 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 506 | &legacy_settings->advertising, |
| 507 | link_ksettings->link_modes.advertising); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 508 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 509 | &legacy_settings->lp_advertising, |
| 510 | link_ksettings->link_modes.lp_advertising); |
| 511 | ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed); |
| 512 | legacy_settings->duplex |
| 513 | = link_ksettings->base.duplex; |
| 514 | legacy_settings->port |
| 515 | = link_ksettings->base.port; |
| 516 | legacy_settings->phy_address |
| 517 | = link_ksettings->base.phy_address; |
| 518 | legacy_settings->autoneg |
| 519 | = link_ksettings->base.autoneg; |
| 520 | legacy_settings->mdio_support |
| 521 | = link_ksettings->base.mdio_support; |
| 522 | legacy_settings->eth_tp_mdix |
| 523 | = link_ksettings->base.eth_tp_mdix; |
| 524 | legacy_settings->eth_tp_mdix_ctrl |
| 525 | = link_ksettings->base.eth_tp_mdix_ctrl; |
Florian Fainelli | 19cab88 | 2017-09-20 15:52:13 -0700 | [diff] [blame] | 526 | legacy_settings->transceiver |
| 527 | = link_ksettings->base.transceiver; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 528 | return retval; |
| 529 | } |
| 530 | |
| 531 | /* number of 32-bit words to store the user's link mode bitmaps */ |
| 532 | #define __ETHTOOL_LINK_MODE_MASK_NU32 \ |
| 533 | DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32) |
| 534 | |
| 535 | /* layout of the struct passed from/to userland */ |
| 536 | struct ethtool_link_usettings { |
| 537 | struct ethtool_link_settings base; |
| 538 | struct { |
| 539 | __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 540 | __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 541 | __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 542 | } link_modes; |
| 543 | }; |
| 544 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 545 | /* Internal kernel helper to query a device ethtool_link_settings. */ |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 546 | int __ethtool_get_link_ksettings(struct net_device *dev, |
| 547 | struct ethtool_link_ksettings *link_ksettings) |
| 548 | { |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 549 | ASSERT_RTNL(); |
| 550 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 551 | if (!dev->ethtool_ops->get_link_ksettings) |
David Decotigny | 3237fc6 | 2016-02-24 10:58:11 -0800 | [diff] [blame] | 552 | return -EOPNOTSUPP; |
| 553 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 554 | memset(link_ksettings, 0, sizeof(*link_ksettings)); |
| 555 | return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 556 | } |
| 557 | EXPORT_SYMBOL(__ethtool_get_link_ksettings); |
| 558 | |
| 559 | /* convert ethtool_link_usettings in user space to a kernel internal |
| 560 | * ethtool_link_ksettings. return 0 on success, errno on error. |
| 561 | */ |
| 562 | static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to, |
| 563 | const void __user *from) |
| 564 | { |
| 565 | struct ethtool_link_usettings link_usettings; |
| 566 | |
| 567 | if (copy_from_user(&link_usettings, from, sizeof(link_usettings))) |
| 568 | return -EFAULT; |
| 569 | |
| 570 | memcpy(&to->base, &link_usettings.base, sizeof(to->base)); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 571 | bitmap_from_arr32(to->link_modes.supported, |
| 572 | link_usettings.link_modes.supported, |
| 573 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 574 | bitmap_from_arr32(to->link_modes.advertising, |
| 575 | link_usettings.link_modes.advertising, |
| 576 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 577 | bitmap_from_arr32(to->link_modes.lp_advertising, |
| 578 | link_usettings.link_modes.lp_advertising, |
| 579 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* convert a kernel internal ethtool_link_ksettings to |
| 585 | * ethtool_link_usettings in user space. return 0 on success, errno on |
| 586 | * error. |
| 587 | */ |
| 588 | static int |
| 589 | store_link_ksettings_for_user(void __user *to, |
| 590 | const struct ethtool_link_ksettings *from) |
| 591 | { |
| 592 | struct ethtool_link_usettings link_usettings; |
| 593 | |
| 594 | memcpy(&link_usettings.base, &from->base, sizeof(link_usettings)); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 595 | bitmap_to_arr32(link_usettings.link_modes.supported, |
| 596 | from->link_modes.supported, |
| 597 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 598 | bitmap_to_arr32(link_usettings.link_modes.advertising, |
| 599 | from->link_modes.advertising, |
| 600 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 601 | bitmap_to_arr32(link_usettings.link_modes.lp_advertising, |
| 602 | from->link_modes.lp_advertising, |
| 603 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 604 | |
| 605 | if (copy_to_user(to, &link_usettings, sizeof(link_usettings))) |
| 606 | return -EFAULT; |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 611 | /* Query device for its ethtool_link_settings. */ |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 612 | static int ethtool_get_link_ksettings(struct net_device *dev, |
| 613 | void __user *useraddr) |
| 614 | { |
| 615 | int err = 0; |
| 616 | struct ethtool_link_ksettings link_ksettings; |
| 617 | |
| 618 | ASSERT_RTNL(); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 619 | if (!dev->ethtool_ops->get_link_ksettings) |
| 620 | return -EOPNOTSUPP; |
| 621 | |
| 622 | /* handle bitmap nbits handshake */ |
| 623 | if (copy_from_user(&link_ksettings.base, useraddr, |
| 624 | sizeof(link_ksettings.base))) |
| 625 | return -EFAULT; |
| 626 | |
| 627 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 628 | != link_ksettings.base.link_mode_masks_nwords) { |
| 629 | /* wrong link mode nbits requested */ |
| 630 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
Ben Hutchings | 793cf87 | 2016-03-14 01:05:38 +0000 | [diff] [blame] | 631 | link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 632 | /* send back number of words required as negative val */ |
| 633 | compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX, |
| 634 | "need too many bits for link modes!"); |
| 635 | link_ksettings.base.link_mode_masks_nwords |
| 636 | = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32); |
| 637 | |
| 638 | /* copy the base fields back to user, not the link |
| 639 | * mode bitmaps |
| 640 | */ |
| 641 | if (copy_to_user(useraddr, &link_ksettings.base, |
| 642 | sizeof(link_ksettings.base))) |
| 643 | return -EFAULT; |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /* handshake successful: user/kernel agree on |
| 649 | * link_mode_masks_nwords |
| 650 | */ |
| 651 | |
| 652 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
| 653 | err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings); |
| 654 | if (err < 0) |
| 655 | return err; |
| 656 | |
| 657 | /* make sure we tell the right values to user */ |
| 658 | link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; |
| 659 | link_ksettings.base.link_mode_masks_nwords |
| 660 | = __ETHTOOL_LINK_MODE_MASK_NU32; |
| 661 | |
| 662 | return store_link_ksettings_for_user(useraddr, &link_ksettings); |
| 663 | } |
| 664 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 665 | /* Update device ethtool_link_settings. */ |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 666 | static int ethtool_set_link_ksettings(struct net_device *dev, |
| 667 | void __user *useraddr) |
| 668 | { |
| 669 | int err; |
| 670 | struct ethtool_link_ksettings link_ksettings; |
| 671 | |
| 672 | ASSERT_RTNL(); |
| 673 | |
| 674 | if (!dev->ethtool_ops->set_link_ksettings) |
| 675 | return -EOPNOTSUPP; |
| 676 | |
| 677 | /* make sure nbits field has expected value */ |
| 678 | if (copy_from_user(&link_ksettings.base, useraddr, |
| 679 | sizeof(link_ksettings.base))) |
| 680 | return -EFAULT; |
| 681 | |
| 682 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 683 | != link_ksettings.base.link_mode_masks_nwords) |
| 684 | return -EINVAL; |
| 685 | |
| 686 | /* copy the whole structure, now that we know it has expected |
| 687 | * format |
| 688 | */ |
| 689 | err = load_link_ksettings_from_user(&link_ksettings, useraddr); |
| 690 | if (err) |
| 691 | return err; |
| 692 | |
| 693 | /* re-check nwords field, just in case */ |
| 694 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 695 | != link_ksettings.base.link_mode_masks_nwords) |
| 696 | return -EINVAL; |
| 697 | |
| 698 | return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings); |
| 699 | } |
| 700 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 701 | /* Query device for its ethtool_cmd settings. |
| 702 | * |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 703 | * Backward compatibility note: for compatibility with legacy ethtool, this is |
| 704 | * now implemented via get_link_ksettings. When driver reports higher link mode |
| 705 | * bits, a kernel warning is logged once (with name of 1st driver/device) to |
| 706 | * recommend user to upgrade ethtool, but the command is successful (only the |
| 707 | * lower link mode bits reported back to user). Deprecated fields from |
| 708 | * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero. |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 709 | */ |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 710 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) |
| 711 | { |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 712 | struct ethtool_link_ksettings link_ksettings; |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 713 | struct ethtool_cmd cmd; |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 714 | int err; |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 715 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 716 | ASSERT_RTNL(); |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 717 | if (!dev->ethtool_ops->get_link_ksettings) |
| 718 | return -EOPNOTSUPP; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 719 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 720 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
| 721 | err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings); |
| 722 | if (err < 0) |
| 723 | return err; |
| 724 | convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 725 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 726 | /* send a sensible cmd tag back to user */ |
| 727 | cmd.cmd = ETHTOOL_GSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 730 | return -EFAULT; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 735 | /* Update device link settings with given ethtool_cmd. |
| 736 | * |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 737 | * Backward compatibility note: for compatibility with legacy ethtool, this is |
| 738 | * now always implemented via set_link_settings. When user's request updates |
| 739 | * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel |
| 740 | * warning is logged once (with name of 1st driver/device) to recommend user to |
| 741 | * upgrade ethtool, and the request is rejected. |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 742 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) |
| 744 | { |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 745 | struct ethtool_link_ksettings link_ksettings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | struct ethtool_cmd cmd; |
| 747 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 748 | ASSERT_RTNL(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 751 | return -EFAULT; |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 752 | if (!dev->ethtool_ops->set_link_ksettings) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 753 | return -EOPNOTSUPP; |
| 754 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 755 | if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd)) |
| 756 | return -EINVAL; |
| 757 | link_ksettings.base.link_mode_masks_nwords = |
| 758 | __ETHTOOL_LINK_MODE_MASK_NU32; |
| 759 | return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 762 | static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev, |
| 763 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
| 765 | struct ethtool_drvinfo info; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 766 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | memset(&info, 0, sizeof(info)); |
| 769 | info.cmd = ETHTOOL_GDRVINFO; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 770 | if (ops->get_drvinfo) { |
Ben Hutchings | 0141480 | 2010-08-17 02:31:15 -0700 | [diff] [blame] | 771 | ops->get_drvinfo(dev, &info); |
| 772 | } else if (dev->dev.parent && dev->dev.parent->driver) { |
| 773 | strlcpy(info.bus_info, dev_name(dev->dev.parent), |
| 774 | sizeof(info.bus_info)); |
| 775 | strlcpy(info.driver, dev->dev.parent->driver->name, |
| 776 | sizeof(info.driver)); |
| 777 | } else { |
| 778 | return -EOPNOTSUPP; |
| 779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 781 | /* |
| 782 | * this method of obtaining string set info is deprecated; |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 783 | * Use ETHTOOL_GSSET_INFO instead. |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 784 | */ |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 785 | if (ops->get_sset_count) { |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 786 | int rc; |
| 787 | |
| 788 | rc = ops->get_sset_count(dev, ETH_SS_TEST); |
| 789 | if (rc >= 0) |
| 790 | info.testinfo_len = rc; |
| 791 | rc = ops->get_sset_count(dev, ETH_SS_STATS); |
| 792 | if (rc >= 0) |
| 793 | info.n_stats = rc; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 794 | rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); |
| 795 | if (rc >= 0) |
| 796 | info.n_priv_flags = rc; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 797 | } |
Yunsheng Lin | f9fc54d | 2018-12-26 19:51:46 +0800 | [diff] [blame] | 798 | if (ops->get_regs_len) { |
| 799 | int ret = ops->get_regs_len(dev); |
| 800 | |
| 801 | if (ret > 0) |
| 802 | info.regdump_len = ret; |
| 803 | } |
| 804 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 805 | if (ops->get_eeprom_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | info.eedump_len = ops->get_eeprom_len(dev); |
| 807 | |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 808 | rtnl_unlock(); |
| 809 | if (!info.fw_version[0]) |
| 810 | devlink_compat_running_version(dev, info.fw_version, |
| 811 | sizeof(info.fw_version)); |
| 812 | rtnl_lock(); |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 815 | return -EFAULT; |
| 816 | return 0; |
| 817 | } |
| 818 | |
Eric Dumazet | f5c445e | 2010-03-08 12:17:04 -0800 | [diff] [blame] | 819 | static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev, |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 820 | void __user *useraddr) |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 821 | { |
| 822 | struct ethtool_sset_info info; |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 823 | u64 sset_mask; |
| 824 | int i, idx = 0, n_bits = 0, ret, rc; |
| 825 | u32 *info_buf = NULL; |
| 826 | |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 827 | if (copy_from_user(&info, useraddr, sizeof(info))) |
| 828 | return -EFAULT; |
| 829 | |
| 830 | /* store copy of mask, because we zero struct later on */ |
| 831 | sset_mask = info.sset_mask; |
| 832 | if (!sset_mask) |
| 833 | return 0; |
| 834 | |
| 835 | /* calculate size of return buffer */ |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 836 | n_bits = hweight64(sset_mask); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 837 | |
| 838 | memset(&info, 0, sizeof(info)); |
| 839 | info.cmd = ETHTOOL_GSSET_INFO; |
| 840 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 841 | info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 842 | if (!info_buf) |
| 843 | return -ENOMEM; |
| 844 | |
| 845 | /* |
| 846 | * fill return buffer based on input bitmask and successful |
| 847 | * get_sset_count return |
| 848 | */ |
| 849 | for (i = 0; i < 64; i++) { |
| 850 | if (!(sset_mask & (1ULL << i))) |
| 851 | continue; |
| 852 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 853 | rc = __ethtool_get_sset_count(dev, i); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 854 | if (rc >= 0) { |
| 855 | info.sset_mask |= (1ULL << i); |
| 856 | info_buf[idx++] = rc; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | ret = -EFAULT; |
| 861 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 862 | goto out; |
| 863 | |
| 864 | useraddr += offsetof(struct ethtool_sset_info, data); |
| 865 | if (copy_to_user(useraddr, info_buf, idx * sizeof(u32))) |
| 866 | goto out; |
| 867 | |
| 868 | ret = 0; |
| 869 | |
| 870 | out: |
| 871 | kfree(info_buf); |
| 872 | return ret; |
| 873 | } |
| 874 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 875 | static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 876 | u32 cmd, void __user *useraddr) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 877 | { |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 878 | struct ethtool_rxnfc info; |
| 879 | size_t info_size = sizeof(info); |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 880 | int rc; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 881 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 882 | if (!dev->ethtool_ops->set_rxnfc) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 883 | return -EOPNOTSUPP; |
| 884 | |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 885 | /* struct ethtool_rxnfc was originally defined for |
| 886 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data |
| 887 | * members. User-space might still be using that |
| 888 | * definition. */ |
| 889 | if (cmd == ETHTOOL_SRXFH) |
| 890 | info_size = (offsetof(struct ethtool_rxnfc, data) + |
| 891 | sizeof(info.data)); |
| 892 | |
| 893 | if (copy_from_user(&info, useraddr, info_size)) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 894 | return -EFAULT; |
| 895 | |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 896 | rc = dev->ethtool_ops->set_rxnfc(dev, &info); |
| 897 | if (rc) |
| 898 | return rc; |
| 899 | |
| 900 | if (cmd == ETHTOOL_SRXCLSRLINS && |
| 901 | copy_to_user(useraddr, &info, info_size)) |
| 902 | return -EFAULT; |
| 903 | |
| 904 | return 0; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 905 | } |
| 906 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 907 | static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 908 | u32 cmd, void __user *useraddr) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 909 | { |
| 910 | struct ethtool_rxnfc info; |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 911 | size_t info_size = sizeof(info); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 912 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 913 | int ret; |
| 914 | void *rule_buf = NULL; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 915 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 916 | if (!ops->get_rxnfc) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 917 | return -EOPNOTSUPP; |
| 918 | |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 919 | /* struct ethtool_rxnfc was originally defined for |
| 920 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data |
| 921 | * members. User-space might still be using that |
| 922 | * definition. */ |
| 923 | if (cmd == ETHTOOL_GRXFH) |
| 924 | info_size = (offsetof(struct ethtool_rxnfc, data) + |
| 925 | sizeof(info.data)); |
| 926 | |
| 927 | if (copy_from_user(&info, useraddr, info_size)) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 928 | return -EFAULT; |
| 929 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 930 | /* If FLOW_RSS was requested then user-space must be using the |
| 931 | * new definition, as FLOW_RSS is newer. |
| 932 | */ |
| 933 | if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) { |
| 934 | info_size = sizeof(info); |
| 935 | if (copy_from_user(&info, useraddr, info_size)) |
| 936 | return -EFAULT; |
Wenwen Wang | d656fe4 | 2018-04-30 12:31:13 -0500 | [diff] [blame] | 937 | /* Since malicious users may modify the original data, |
| 938 | * we need to check whether FLOW_RSS is still requested. |
| 939 | */ |
| 940 | if (!(info.flow_type & FLOW_RSS)) |
| 941 | return -EINVAL; |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Wenwen Wang | 2bb3207 | 2018-10-09 08:15:38 -0500 | [diff] [blame] | 944 | if (info.cmd != cmd) |
| 945 | return -EINVAL; |
| 946 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 947 | if (info.cmd == ETHTOOL_GRXCLSRLALL) { |
| 948 | if (info.rule_cnt > 0) { |
Ben Hutchings | db048b6 | 2010-06-28 08:44:07 +0000 | [diff] [blame] | 949 | if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32)) |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 950 | rule_buf = kcalloc(info.rule_cnt, sizeof(u32), |
Ben Hutchings | db048b6 | 2010-06-28 08:44:07 +0000 | [diff] [blame] | 951 | GFP_USER); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 952 | if (!rule_buf) |
| 953 | return -ENOMEM; |
| 954 | } |
| 955 | } |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 956 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 957 | ret = ops->get_rxnfc(dev, &info, rule_buf); |
| 958 | if (ret < 0) |
| 959 | goto err_out; |
| 960 | |
| 961 | ret = -EFAULT; |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 962 | if (copy_to_user(useraddr, &info, info_size)) |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 963 | goto err_out; |
| 964 | |
| 965 | if (rule_buf) { |
| 966 | useraddr += offsetof(struct ethtool_rxnfc, rule_locs); |
| 967 | if (copy_to_user(useraddr, rule_buf, |
| 968 | info.rule_cnt * sizeof(u32))) |
| 969 | goto err_out; |
| 970 | } |
| 971 | ret = 0; |
| 972 | |
| 973 | err_out: |
Wei Yongjun | c9cacec | 2009-03-31 15:06:26 -0700 | [diff] [blame] | 974 | kfree(rule_buf); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 975 | |
| 976 | return ret; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 979 | static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr, |
| 980 | struct ethtool_rxnfc *rx_rings, |
| 981 | u32 size) |
| 982 | { |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 983 | int i; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 984 | |
| 985 | if (copy_from_user(indir, useraddr, size * sizeof(indir[0]))) |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 986 | return -EFAULT; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 987 | |
| 988 | /* Validate ring indices */ |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 989 | for (i = 0; i < size; i++) |
| 990 | if (indir[i] >= rx_rings->data) |
| 991 | return -EINVAL; |
| 992 | |
| 993 | return 0; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 994 | } |
| 995 | |
Kim Jones | ba905f5 | 2016-02-02 03:51:16 +0000 | [diff] [blame] | 996 | u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly; |
Eric Dumazet | 960fb62 | 2014-11-16 06:23:05 -0800 | [diff] [blame] | 997 | |
| 998 | void netdev_rss_key_fill(void *buffer, size_t len) |
| 999 | { |
| 1000 | BUG_ON(len > sizeof(netdev_rss_key)); |
| 1001 | net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key)); |
| 1002 | memcpy(buffer, netdev_rss_key, len); |
| 1003 | } |
| 1004 | EXPORT_SYMBOL(netdev_rss_key_fill); |
| 1005 | |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1006 | static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max) |
| 1007 | { |
| 1008 | u32 dev_size, current_max = 0; |
| 1009 | u32 *indir; |
| 1010 | int ret; |
| 1011 | |
| 1012 | if (!dev->ethtool_ops->get_rxfh_indir_size || |
| 1013 | !dev->ethtool_ops->get_rxfh) |
| 1014 | return -EOPNOTSUPP; |
| 1015 | dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); |
| 1016 | if (dev_size == 0) |
| 1017 | return -EOPNOTSUPP; |
| 1018 | |
| 1019 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
| 1020 | if (!indir) |
| 1021 | return -ENOMEM; |
| 1022 | |
| 1023 | ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); |
| 1024 | if (ret) |
| 1025 | goto out; |
| 1026 | |
| 1027 | while (dev_size--) |
| 1028 | current_max = max(current_max, indir[dev_size]); |
| 1029 | |
| 1030 | *max = current_max; |
| 1031 | |
| 1032 | out: |
| 1033 | kfree(indir); |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1037 | static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev, |
| 1038 | void __user *useraddr) |
| 1039 | { |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1040 | u32 user_size, dev_size; |
| 1041 | u32 *indir; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1042 | int ret; |
| 1043 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1044 | if (!dev->ethtool_ops->get_rxfh_indir_size || |
Ben Hutchings | fe62d00 | 2014-05-15 01:25:27 +0100 | [diff] [blame] | 1045 | !dev->ethtool_ops->get_rxfh) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1046 | return -EOPNOTSUPP; |
| 1047 | dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); |
| 1048 | if (dev_size == 0) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1049 | return -EOPNOTSUPP; |
| 1050 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1051 | if (copy_from_user(&user_size, |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1052 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1053 | sizeof(user_size))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1054 | return -EFAULT; |
| 1055 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1056 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size), |
| 1057 | &dev_size, sizeof(dev_size))) |
| 1058 | return -EFAULT; |
| 1059 | |
| 1060 | /* If the user buffer size is 0, this is just a query for the |
| 1061 | * device table size. Otherwise, if it's smaller than the |
| 1062 | * device table size it's an error. |
| 1063 | */ |
| 1064 | if (user_size < dev_size) |
| 1065 | return user_size == 0 ? 0 : -EINVAL; |
| 1066 | |
| 1067 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1068 | if (!indir) |
| 1069 | return -ENOMEM; |
| 1070 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1071 | ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1072 | if (ret) |
| 1073 | goto out; |
| 1074 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1075 | if (copy_to_user(useraddr + |
| 1076 | offsetof(struct ethtool_rxfh_indir, ring_index[0]), |
| 1077 | indir, dev_size * sizeof(indir[0]))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1078 | ret = -EFAULT; |
| 1079 | |
| 1080 | out: |
| 1081 | kfree(indir); |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
| 1085 | static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev, |
| 1086 | void __user *useraddr) |
| 1087 | { |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1088 | struct ethtool_rxnfc rx_rings; |
| 1089 | u32 user_size, dev_size, i; |
| 1090 | u32 *indir; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1091 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1092 | int ret; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1093 | u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1094 | |
Ben Hutchings | fe62d00 | 2014-05-15 01:25:27 +0100 | [diff] [blame] | 1095 | if (!ops->get_rxfh_indir_size || !ops->set_rxfh || |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1096 | !ops->get_rxnfc) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1097 | return -EOPNOTSUPP; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1098 | |
| 1099 | dev_size = ops->get_rxfh_indir_size(dev); |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1100 | if (dev_size == 0) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1101 | return -EOPNOTSUPP; |
| 1102 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1103 | if (copy_from_user(&user_size, |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1104 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1105 | sizeof(user_size))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1106 | return -EFAULT; |
| 1107 | |
Ben Hutchings | 278bc42 | 2011-12-15 13:56:49 +0000 | [diff] [blame] | 1108 | if (user_size != 0 && user_size != dev_size) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1109 | return -EINVAL; |
| 1110 | |
| 1111 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1112 | if (!indir) |
| 1113 | return -ENOMEM; |
| 1114 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1115 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1116 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1117 | if (ret) |
| 1118 | goto out; |
Ben Hutchings | 278bc42 | 2011-12-15 13:56:49 +0000 | [diff] [blame] | 1119 | |
| 1120 | if (user_size == 0) { |
| 1121 | for (i = 0; i < dev_size; i++) |
| 1122 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); |
| 1123 | } else { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1124 | ret = ethtool_copy_validate_indir(indir, |
| 1125 | useraddr + ringidx_offset, |
| 1126 | &rx_rings, |
| 1127 | dev_size); |
| 1128 | if (ret) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1129 | goto out; |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1132 | ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE); |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1133 | if (ret) |
| 1134 | goto out; |
| 1135 | |
| 1136 | /* indicate whether rxfh was set to default */ |
| 1137 | if (user_size == 0) |
| 1138 | dev->priv_flags &= ~IFF_RXFH_CONFIGURED; |
| 1139 | else |
| 1140 | dev->priv_flags |= IFF_RXFH_CONFIGURED; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1141 | |
| 1142 | out: |
| 1143 | kfree(indir); |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1147 | static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev, |
| 1148 | void __user *useraddr) |
| 1149 | { |
| 1150 | int ret; |
| 1151 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1152 | u32 user_indir_size, user_key_size; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1153 | u32 dev_indir_size = 0, dev_key_size = 0; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1154 | struct ethtool_rxfh rxfh; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1155 | u32 total_size; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1156 | u32 indir_bytes; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1157 | u32 *indir = NULL; |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1158 | u8 dev_hfunc = 0; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1159 | u8 *hkey = NULL; |
| 1160 | u8 *rss_config; |
| 1161 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1162 | if (!ops->get_rxfh) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1163 | return -EOPNOTSUPP; |
| 1164 | |
| 1165 | if (ops->get_rxfh_indir_size) |
| 1166 | dev_indir_size = ops->get_rxfh_indir_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1167 | if (ops->get_rxfh_key_size) |
| 1168 | dev_key_size = ops->get_rxfh_key_size(dev); |
| 1169 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1170 | if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1171 | return -EFAULT; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1172 | user_indir_size = rxfh.indir_size; |
| 1173 | user_key_size = rxfh.key_size; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1174 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1175 | /* Check that reserved fields are 0 for now */ |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1176 | if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32) |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1177 | return -EINVAL; |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1178 | /* Most drivers don't handle rss_context, check it's 0 as well */ |
| 1179 | if (rxfh.rss_context && !ops->get_rxfh_context) |
| 1180 | return -EOPNOTSUPP; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1181 | |
| 1182 | rxfh.indir_size = dev_indir_size; |
| 1183 | rxfh.key_size = dev_key_size; |
| 1184 | if (copy_to_user(useraddr, &rxfh, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1185 | return -EFAULT; |
| 1186 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1187 | if ((user_indir_size && (user_indir_size != dev_indir_size)) || |
| 1188 | (user_key_size && (user_key_size != dev_key_size))) |
| 1189 | return -EINVAL; |
| 1190 | |
| 1191 | indir_bytes = user_indir_size * sizeof(indir[0]); |
| 1192 | total_size = indir_bytes + user_key_size; |
| 1193 | rss_config = kzalloc(total_size, GFP_USER); |
| 1194 | if (!rss_config) |
| 1195 | return -ENOMEM; |
| 1196 | |
| 1197 | if (user_indir_size) |
| 1198 | indir = (u32 *)rss_config; |
| 1199 | |
| 1200 | if (user_key_size) |
| 1201 | hkey = rss_config + indir_bytes; |
| 1202 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1203 | if (rxfh.rss_context) |
| 1204 | ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey, |
| 1205 | &dev_hfunc, |
| 1206 | rxfh.rss_context); |
| 1207 | else |
| 1208 | ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc); |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1209 | if (ret) |
| 1210 | goto out; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1211 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1212 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc), |
| 1213 | &dev_hfunc, sizeof(rxfh.hfunc))) { |
| 1214 | ret = -EFAULT; |
| 1215 | } else if (copy_to_user(useraddr + |
| 1216 | offsetof(struct ethtool_rxfh, rss_config[0]), |
| 1217 | rss_config, total_size)) { |
| 1218 | ret = -EFAULT; |
| 1219 | } |
| 1220 | out: |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1221 | kfree(rss_config); |
| 1222 | |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
| 1226 | static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev, |
| 1227 | void __user *useraddr) |
| 1228 | { |
| 1229 | int ret; |
| 1230 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1231 | struct ethtool_rxnfc rx_rings; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1232 | struct ethtool_rxfh rxfh; |
| 1233 | u32 dev_indir_size = 0, dev_key_size = 0, i; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1234 | u32 *indir = NULL, indir_bytes = 0; |
| 1235 | u8 *hkey = NULL; |
| 1236 | u8 *rss_config; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1237 | u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]); |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1238 | bool delete = false; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1239 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1240 | if (!ops->get_rxnfc || !ops->set_rxfh) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1241 | return -EOPNOTSUPP; |
| 1242 | |
| 1243 | if (ops->get_rxfh_indir_size) |
| 1244 | dev_indir_size = ops->get_rxfh_indir_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1245 | if (ops->get_rxfh_key_size) |
Dan Carpenter | d340c86 | 2015-02-20 13:54:05 +0300 | [diff] [blame] | 1246 | dev_key_size = ops->get_rxfh_key_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1247 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1248 | if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1249 | return -EFAULT; |
| 1250 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1251 | /* Check that reserved fields are 0 for now */ |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1252 | if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32) |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1253 | return -EINVAL; |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1254 | /* Most drivers don't handle rss_context, check it's 0 as well */ |
| 1255 | if (rxfh.rss_context && !ops->set_rxfh_context) |
| 1256 | return -EOPNOTSUPP; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1257 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1258 | /* If either indir, hash key or function is valid, proceed further. |
| 1259 | * Must request at least one change: indir size, hash key or function. |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1260 | */ |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1261 | if ((rxfh.indir_size && |
| 1262 | rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE && |
| 1263 | rxfh.indir_size != dev_indir_size) || |
| 1264 | (rxfh.key_size && (rxfh.key_size != dev_key_size)) || |
| 1265 | (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE && |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1266 | rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE)) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1267 | return -EINVAL; |
| 1268 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1269 | if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1270 | indir_bytes = dev_indir_size * sizeof(indir[0]); |
| 1271 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1272 | rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1273 | if (!rss_config) |
| 1274 | return -ENOMEM; |
| 1275 | |
| 1276 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
| 1277 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
| 1278 | if (ret) |
| 1279 | goto out; |
| 1280 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1281 | /* rxfh.indir_size == 0 means reset the indir table to default (master |
| 1282 | * context) or delete the context (other RSS contexts). |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1283 | * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged. |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1284 | */ |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1285 | if (rxfh.indir_size && |
| 1286 | rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1287 | indir = (u32 *)rss_config; |
| 1288 | ret = ethtool_copy_validate_indir(indir, |
| 1289 | useraddr + rss_cfg_offset, |
| 1290 | &rx_rings, |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1291 | rxfh.indir_size); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1292 | if (ret) |
| 1293 | goto out; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1294 | } else if (rxfh.indir_size == 0) { |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1295 | if (rxfh.rss_context == 0) { |
| 1296 | indir = (u32 *)rss_config; |
| 1297 | for (i = 0; i < dev_indir_size; i++) |
| 1298 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); |
| 1299 | } else { |
| 1300 | delete = true; |
| 1301 | } |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1302 | } |
| 1303 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1304 | if (rxfh.key_size) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1305 | hkey = rss_config + indir_bytes; |
| 1306 | if (copy_from_user(hkey, |
| 1307 | useraddr + rss_cfg_offset + indir_bytes, |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1308 | rxfh.key_size)) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1309 | ret = -EFAULT; |
| 1310 | goto out; |
| 1311 | } |
| 1312 | } |
| 1313 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1314 | if (rxfh.rss_context) |
| 1315 | ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc, |
| 1316 | &rxfh.rss_context, delete); |
| 1317 | else |
| 1318 | ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc); |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1319 | if (ret) |
| 1320 | goto out; |
| 1321 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1322 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context), |
| 1323 | &rxfh.rss_context, sizeof(rxfh.rss_context))) |
| 1324 | ret = -EFAULT; |
| 1325 | |
| 1326 | if (!rxfh.rss_context) { |
| 1327 | /* indicate whether rxfh was set to default */ |
| 1328 | if (rxfh.indir_size == 0) |
| 1329 | dev->priv_flags &= ~IFF_RXFH_CONFIGURED; |
| 1330 | else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) |
| 1331 | dev->priv_flags |= IFF_RXFH_CONFIGURED; |
| 1332 | } |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1333 | |
| 1334 | out: |
| 1335 | kfree(rss_config); |
| 1336 | return ret; |
| 1337 | } |
| 1338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) |
| 1340 | { |
| 1341 | struct ethtool_regs regs; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1342 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | void *regbuf; |
| 1344 | int reglen, ret; |
| 1345 | |
| 1346 | if (!ops->get_regs || !ops->get_regs_len) |
| 1347 | return -EOPNOTSUPP; |
| 1348 | |
| 1349 | if (copy_from_user(®s, useraddr, sizeof(regs))) |
| 1350 | return -EFAULT; |
| 1351 | |
| 1352 | reglen = ops->get_regs_len(dev); |
Yunsheng Lin | f9fc54d | 2018-12-26 19:51:46 +0800 | [diff] [blame] | 1353 | if (reglen <= 0) |
| 1354 | return reglen; |
| 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | if (regs.len > reglen) |
| 1357 | regs.len = reglen; |
| 1358 | |
Dan Carpenter | ef76c77 | 2019-02-01 11:24:06 +0300 | [diff] [blame] | 1359 | regbuf = vzalloc(reglen); |
| 1360 | if (!regbuf) |
| 1361 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | ops->get_regs(dev, ®s, regbuf); |
| 1364 | |
| 1365 | ret = -EFAULT; |
| 1366 | if (copy_to_user(useraddr, ®s, sizeof(regs))) |
| 1367 | goto out; |
| 1368 | useraddr += offsetof(struct ethtool_regs, data); |
Ben Hutchings | 67ae7cf | 2011-07-21 15:25:30 -0700 | [diff] [blame] | 1369 | if (regbuf && copy_to_user(useraddr, regbuf, regs.len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | goto out; |
| 1371 | ret = 0; |
| 1372 | |
| 1373 | out: |
Ben Hutchings | a77f5db | 2010-09-20 08:42:17 +0000 | [diff] [blame] | 1374 | vfree(regbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return ret; |
| 1376 | } |
| 1377 | |
Ben Hutchings | d73d3a8 | 2009-10-05 10:59:58 +0000 | [diff] [blame] | 1378 | static int ethtool_reset(struct net_device *dev, char __user *useraddr) |
| 1379 | { |
| 1380 | struct ethtool_value reset; |
| 1381 | int ret; |
| 1382 | |
| 1383 | if (!dev->ethtool_ops->reset) |
| 1384 | return -EOPNOTSUPP; |
| 1385 | |
| 1386 | if (copy_from_user(&reset, useraddr, sizeof(reset))) |
| 1387 | return -EFAULT; |
| 1388 | |
| 1389 | ret = dev->ethtool_ops->reset(dev, &reset.data); |
| 1390 | if (ret) |
| 1391 | return ret; |
| 1392 | |
| 1393 | if (copy_to_user(useraddr, &reset, sizeof(reset))) |
| 1394 | return -EFAULT; |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) |
| 1399 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1400 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | if (!dev->ethtool_ops->get_wol) |
| 1403 | return -EOPNOTSUPP; |
| 1404 | |
| 1405 | dev->ethtool_ops->get_wol(dev, &wol); |
| 1406 | |
| 1407 | if (copy_to_user(useraddr, &wol, sizeof(wol))) |
| 1408 | return -EFAULT; |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) |
| 1413 | { |
| 1414 | struct ethtool_wolinfo wol; |
Heiner Kallweit | 6194114 | 2018-09-24 21:58:59 +0200 | [diff] [blame] | 1415 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | if (!dev->ethtool_ops->set_wol) |
| 1418 | return -EOPNOTSUPP; |
| 1419 | |
| 1420 | if (copy_from_user(&wol, useraddr, sizeof(wol))) |
| 1421 | return -EFAULT; |
| 1422 | |
Heiner Kallweit | 6194114 | 2018-09-24 21:58:59 +0200 | [diff] [blame] | 1423 | ret = dev->ethtool_ops->set_wol(dev, &wol); |
| 1424 | if (ret) |
| 1425 | return ret; |
| 1426 | |
| 1427 | dev->wol_enabled = !!wol.wolopts; |
| 1428 | |
| 1429 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 1432 | static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) |
| 1433 | { |
| 1434 | struct ethtool_eee edata; |
| 1435 | int rc; |
| 1436 | |
| 1437 | if (!dev->ethtool_ops->get_eee) |
| 1438 | return -EOPNOTSUPP; |
| 1439 | |
| 1440 | memset(&edata, 0, sizeof(struct ethtool_eee)); |
| 1441 | edata.cmd = ETHTOOL_GEEE; |
| 1442 | rc = dev->ethtool_ops->get_eee(dev, &edata); |
| 1443 | |
| 1444 | if (rc) |
| 1445 | return rc; |
| 1446 | |
| 1447 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 1448 | return -EFAULT; |
| 1449 | |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) |
| 1454 | { |
| 1455 | struct ethtool_eee edata; |
| 1456 | |
| 1457 | if (!dev->ethtool_ops->set_eee) |
| 1458 | return -EOPNOTSUPP; |
| 1459 | |
| 1460 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 1461 | return -EFAULT; |
| 1462 | |
| 1463 | return dev->ethtool_ops->set_eee(dev, &edata); |
| 1464 | } |
| 1465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | static int ethtool_nway_reset(struct net_device *dev) |
| 1467 | { |
| 1468 | if (!dev->ethtool_ops->nway_reset) |
| 1469 | return -EOPNOTSUPP; |
| 1470 | |
| 1471 | return dev->ethtool_ops->nway_reset(dev); |
| 1472 | } |
| 1473 | |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 1474 | static int ethtool_get_link(struct net_device *dev, char __user *useraddr) |
| 1475 | { |
| 1476 | struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; |
| 1477 | |
| 1478 | if (!dev->ethtool_ops->get_link) |
| 1479 | return -EOPNOTSUPP; |
| 1480 | |
| 1481 | edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev); |
| 1482 | |
| 1483 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 1484 | return -EFAULT; |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1488 | static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, |
| 1489 | int (*getter)(struct net_device *, |
| 1490 | struct ethtool_eeprom *, u8 *), |
| 1491 | u32 total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | { |
| 1493 | struct ethtool_eeprom eeprom; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1494 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 1495 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1497 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 1500 | return -EFAULT; |
| 1501 | |
| 1502 | /* Check for wrap and zero */ |
| 1503 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 1504 | return -EINVAL; |
| 1505 | |
| 1506 | /* Check for exceeding total eeprom len */ |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1507 | if (eeprom.offset + eeprom.len > total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return -EINVAL; |
| 1509 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1510 | data = kmalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | if (!data) |
| 1512 | return -ENOMEM; |
| 1513 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1514 | bytes_remaining = eeprom.len; |
| 1515 | while (bytes_remaining > 0) { |
| 1516 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1518 | ret = getter(dev, &eeprom, data); |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1519 | if (ret) |
| 1520 | break; |
| 1521 | if (copy_to_user(userbuf, data, eeprom.len)) { |
| 1522 | ret = -EFAULT; |
| 1523 | break; |
| 1524 | } |
| 1525 | userbuf += eeprom.len; |
| 1526 | eeprom.offset += eeprom.len; |
| 1527 | bytes_remaining -= eeprom.len; |
| 1528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
Mandeep Singh Baines | c5835df | 2008-04-24 20:55:56 -0700 | [diff] [blame] | 1530 | eeprom.len = userbuf - (useraddr + sizeof(eeprom)); |
| 1531 | eeprom.offset -= eeprom.len; |
| 1532 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) |
| 1533 | ret = -EFAULT; |
| 1534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | kfree(data); |
| 1536 | return ret; |
| 1537 | } |
| 1538 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1539 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) |
| 1540 | { |
| 1541 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1542 | |
Guenter Roeck | e0fb6fb | 2014-10-30 20:50:15 -0700 | [diff] [blame] | 1543 | if (!ops->get_eeprom || !ops->get_eeprom_len || |
| 1544 | !ops->get_eeprom_len(dev)) |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1545 | return -EOPNOTSUPP; |
| 1546 | |
| 1547 | return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, |
| 1548 | ops->get_eeprom_len(dev)); |
| 1549 | } |
| 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) |
| 1552 | { |
| 1553 | struct ethtool_eeprom eeprom; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1554 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1555 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 1556 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1558 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Guenter Roeck | e0fb6fb | 2014-10-30 20:50:15 -0700 | [diff] [blame] | 1560 | if (!ops->set_eeprom || !ops->get_eeprom_len || |
| 1561 | !ops->get_eeprom_len(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | return -EOPNOTSUPP; |
| 1563 | |
| 1564 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 1565 | return -EFAULT; |
| 1566 | |
| 1567 | /* Check for wrap and zero */ |
| 1568 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 1569 | return -EINVAL; |
| 1570 | |
| 1571 | /* Check for exceeding total eeprom len */ |
| 1572 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) |
| 1573 | return -EINVAL; |
| 1574 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1575 | data = kmalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | if (!data) |
| 1577 | return -ENOMEM; |
| 1578 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1579 | bytes_remaining = eeprom.len; |
| 1580 | while (bytes_remaining > 0) { |
| 1581 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1583 | if (copy_from_user(data, userbuf, eeprom.len)) { |
| 1584 | ret = -EFAULT; |
| 1585 | break; |
| 1586 | } |
| 1587 | ret = ops->set_eeprom(dev, &eeprom, data); |
| 1588 | if (ret) |
| 1589 | break; |
| 1590 | userbuf += eeprom.len; |
| 1591 | eeprom.offset += eeprom.len; |
| 1592 | bytes_remaining -= eeprom.len; |
| 1593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | kfree(data); |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1599 | static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, |
| 1600 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1602 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | |
| 1604 | if (!dev->ethtool_ops->get_coalesce) |
| 1605 | return -EOPNOTSUPP; |
| 1606 | |
| 1607 | dev->ethtool_ops->get_coalesce(dev, &coalesce); |
| 1608 | |
| 1609 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 1610 | return -EFAULT; |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1614 | static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, |
| 1615 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | { |
| 1617 | struct ethtool_coalesce coalesce; |
| 1618 | |
David S. Miller | fa04ae5 | 2005-06-06 15:07:19 -0700 | [diff] [blame] | 1619 | if (!dev->ethtool_ops->set_coalesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | return -EOPNOTSUPP; |
| 1621 | |
| 1622 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) |
| 1623 | return -EFAULT; |
| 1624 | |
| 1625 | return dev->ethtool_ops->set_coalesce(dev, &coalesce); |
| 1626 | } |
| 1627 | |
| 1628 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) |
| 1629 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1630 | struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | if (!dev->ethtool_ops->get_ringparam) |
| 1633 | return -EOPNOTSUPP; |
| 1634 | |
| 1635 | dev->ethtool_ops->get_ringparam(dev, &ringparam); |
| 1636 | |
| 1637 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) |
| 1638 | return -EFAULT; |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) |
| 1643 | { |
Eugenia Emantayev | 37e2d99 | 2018-01-08 16:00:24 +0200 | [diff] [blame] | 1644 | struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Eugenia Emantayev | 37e2d99 | 2018-01-08 16:00:24 +0200 | [diff] [blame] | 1646 | if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | return -EOPNOTSUPP; |
| 1648 | |
| 1649 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) |
| 1650 | return -EFAULT; |
| 1651 | |
Eugenia Emantayev | 37e2d99 | 2018-01-08 16:00:24 +0200 | [diff] [blame] | 1652 | dev->ethtool_ops->get_ringparam(dev, &max); |
| 1653 | |
| 1654 | /* ensure new ring parameters are within the maximums */ |
| 1655 | if (ringparam.rx_pending > max.rx_max_pending || |
| 1656 | ringparam.rx_mini_pending > max.rx_mini_max_pending || |
| 1657 | ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending || |
| 1658 | ringparam.tx_pending > max.tx_max_pending) |
| 1659 | return -EINVAL; |
| 1660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | return dev->ethtool_ops->set_ringparam(dev, &ringparam); |
| 1662 | } |
| 1663 | |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1664 | static noinline_for_stack int ethtool_get_channels(struct net_device *dev, |
| 1665 | void __user *useraddr) |
| 1666 | { |
| 1667 | struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; |
| 1668 | |
| 1669 | if (!dev->ethtool_ops->get_channels) |
| 1670 | return -EOPNOTSUPP; |
| 1671 | |
| 1672 | dev->ethtool_ops->get_channels(dev, &channels); |
| 1673 | |
| 1674 | if (copy_to_user(useraddr, &channels, sizeof(channels))) |
| 1675 | return -EFAULT; |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | static noinline_for_stack int ethtool_set_channels(struct net_device *dev, |
| 1680 | void __user *useraddr) |
| 1681 | { |
Jakub Kicinski | b8c8a2e | 2018-10-01 14:51:35 +0200 | [diff] [blame] | 1682 | struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS }; |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1683 | u16 from_channel, to_channel; |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1684 | u32 max_rx_in_use = 0; |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1685 | unsigned int i; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1686 | |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1687 | if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1688 | return -EOPNOTSUPP; |
| 1689 | |
| 1690 | if (copy_from_user(&channels, useraddr, sizeof(channels))) |
| 1691 | return -EFAULT; |
| 1692 | |
Jakub Kicinski | b8c8a2e | 2018-10-01 14:51:35 +0200 | [diff] [blame] | 1693 | dev->ethtool_ops->get_channels(dev, &curr); |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1694 | |
| 1695 | /* ensure new counts are within the maximums */ |
Jakub Kicinski | b8c8a2e | 2018-10-01 14:51:35 +0200 | [diff] [blame] | 1696 | if (channels.rx_count > curr.max_rx || |
| 1697 | channels.tx_count > curr.max_tx || |
| 1698 | channels.combined_count > curr.max_combined || |
| 1699 | channels.other_count > curr.max_other) |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1700 | return -EINVAL; |
| 1701 | |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1702 | /* ensure the new Rx count fits within the configured Rx flow |
| 1703 | * indirection table settings */ |
| 1704 | if (netif_is_rxfh_configured(dev) && |
| 1705 | !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) && |
| 1706 | (channels.combined_count + channels.rx_count) <= max_rx_in_use) |
| 1707 | return -EINVAL; |
| 1708 | |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1709 | /* Disabling channels, query zero-copy AF_XDP sockets */ |
| 1710 | from_channel = channels.combined_count + |
| 1711 | min(channels.rx_count, channels.tx_count); |
| 1712 | to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count); |
| 1713 | for (i = from_channel; i < to_channel; i++) |
| 1714 | if (xdp_get_umem_from_qid(dev, i)) |
| 1715 | return -EINVAL; |
| 1716 | |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1717 | return dev->ethtool_ops->set_channels(dev, &channels); |
| 1718 | } |
| 1719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) |
| 1721 | { |
| 1722 | struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; |
| 1723 | |
| 1724 | if (!dev->ethtool_ops->get_pauseparam) |
| 1725 | return -EOPNOTSUPP; |
| 1726 | |
| 1727 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); |
| 1728 | |
| 1729 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) |
| 1730 | return -EFAULT; |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) |
| 1735 | { |
| 1736 | struct ethtool_pauseparam pauseparam; |
| 1737 | |
Jeff Garzik | e1b90c4 | 2006-07-17 12:54:40 -0400 | [diff] [blame] | 1738 | if (!dev->ethtool_ops->set_pauseparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | return -EOPNOTSUPP; |
| 1740 | |
| 1741 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) |
| 1742 | return -EFAULT; |
| 1743 | |
| 1744 | return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); |
| 1745 | } |
| 1746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
| 1748 | { |
| 1749 | struct ethtool_test test; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1750 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1752 | int ret, test_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1754 | if (!ops->self_test || !ops->get_sset_count) |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1755 | return -EOPNOTSUPP; |
| 1756 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1757 | test_len = ops->get_sset_count(dev, ETH_SS_TEST); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1758 | if (test_len < 0) |
| 1759 | return test_len; |
| 1760 | WARN_ON(test_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
| 1762 | if (copy_from_user(&test, useraddr, sizeof(test))) |
| 1763 | return -EFAULT; |
| 1764 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1765 | test.len = test_len; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1766 | data = kmalloc_array(test_len, sizeof(u64), GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (!data) |
| 1768 | return -ENOMEM; |
| 1769 | |
| 1770 | ops->self_test(dev, &test, data); |
| 1771 | |
| 1772 | ret = -EFAULT; |
| 1773 | if (copy_to_user(useraddr, &test, sizeof(test))) |
| 1774 | goto out; |
| 1775 | useraddr += sizeof(test); |
| 1776 | if (copy_to_user(useraddr, data, test.len * sizeof(u64))) |
| 1777 | goto out; |
| 1778 | ret = 0; |
| 1779 | |
| 1780 | out: |
| 1781 | kfree(data); |
| 1782 | return ret; |
| 1783 | } |
| 1784 | |
| 1785 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) |
| 1786 | { |
| 1787 | struct ethtool_gstrings gstrings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | u8 *data; |
| 1789 | int ret; |
| 1790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) |
| 1792 | return -EFAULT; |
| 1793 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1794 | ret = __ethtool_get_sset_count(dev, gstrings.string_set); |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1795 | if (ret < 0) |
| 1796 | return ret; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1797 | if (ret > S32_MAX / ETH_GSTRING_LEN) |
| 1798 | return -ENOMEM; |
| 1799 | WARN_ON_ONCE(!ret); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1800 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1801 | gstrings.len = ret; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 1802 | data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1803 | if (gstrings.len && !data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | return -ENOMEM; |
| 1805 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1806 | __ethtool_get_strings(dev, gstrings.string_set, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | |
| 1808 | ret = -EFAULT; |
| 1809 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) |
| 1810 | goto out; |
| 1811 | useraddr += sizeof(gstrings); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1812 | if (gstrings.len && |
| 1813 | copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | goto out; |
| 1815 | ret = 0; |
| 1816 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1817 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1818 | vfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | return ret; |
| 1820 | } |
| 1821 | |
| 1822 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) |
| 1823 | { |
| 1824 | struct ethtool_value id; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1825 | static bool busy; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1826 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1827 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1829 | if (!ops->set_phys_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | return -EOPNOTSUPP; |
| 1831 | |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1832 | if (busy) |
| 1833 | return -EBUSY; |
| 1834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | if (copy_from_user(&id, useraddr, sizeof(id))) |
| 1836 | return -EFAULT; |
| 1837 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1838 | rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1839 | if (rc < 0) |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1840 | return rc; |
| 1841 | |
| 1842 | /* Drop the RTNL lock while waiting, but prevent reentry or |
| 1843 | * removal of the device. |
| 1844 | */ |
| 1845 | busy = true; |
| 1846 | dev_hold(dev); |
| 1847 | rtnl_unlock(); |
| 1848 | |
| 1849 | if (rc == 0) { |
| 1850 | /* Driver will handle this itself */ |
| 1851 | schedule_timeout_interruptible( |
Allan, Bruce W | 143780c | 2011-04-11 13:01:59 +0000 | [diff] [blame] | 1852 | id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1853 | } else { |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1854 | /* Driver expects to be called at twice the frequency in rc */ |
| 1855 | int n = rc * 2, i, interval = HZ / n; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1856 | |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1857 | /* Count down seconds */ |
| 1858 | do { |
| 1859 | /* Count down iterations per second */ |
| 1860 | i = n; |
| 1861 | do { |
| 1862 | rtnl_lock(); |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1863 | rc = ops->set_phys_id(dev, |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1864 | (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); |
| 1865 | rtnl_unlock(); |
| 1866 | if (rc) |
| 1867 | break; |
| 1868 | schedule_timeout_interruptible(interval); |
| 1869 | } while (!signal_pending(current) && --i != 0); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1870 | } while (!signal_pending(current) && |
| 1871 | (id.data == 0 || --id.data != 0)); |
| 1872 | } |
| 1873 | |
| 1874 | rtnl_lock(); |
| 1875 | dev_put(dev); |
| 1876 | busy = false; |
| 1877 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1878 | (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1879 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) |
| 1883 | { |
| 1884 | struct ethtool_stats stats; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1885 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1887 | int ret, n_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1889 | if (!ops->get_ethtool_stats || !ops->get_sset_count) |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1890 | return -EOPNOTSUPP; |
| 1891 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1892 | n_stats = ops->get_sset_count(dev, ETH_SS_STATS); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1893 | if (n_stats < 0) |
| 1894 | return n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1895 | if (n_stats > S32_MAX / sizeof(u64)) |
| 1896 | return -ENOMEM; |
| 1897 | WARN_ON_ONCE(!n_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 1899 | return -EFAULT; |
| 1900 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1901 | stats.n_stats = n_stats; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 1902 | data = vzalloc(array_size(n_stats, sizeof(u64))); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1903 | if (n_stats && !data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | return -ENOMEM; |
| 1905 | |
| 1906 | ops->get_ethtool_stats(dev, &stats, data); |
| 1907 | |
| 1908 | ret = -EFAULT; |
| 1909 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 1910 | goto out; |
| 1911 | useraddr += sizeof(stats); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1912 | if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | goto out; |
| 1914 | ret = 0; |
| 1915 | |
| 1916 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1917 | vfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | return ret; |
| 1919 | } |
| 1920 | |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1921 | static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) |
| 1922 | { |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 1923 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1924 | struct phy_device *phydev = dev->phydev; |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 1925 | struct ethtool_stats stats; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1926 | u64 *data; |
| 1927 | int ret, n_stats; |
| 1928 | |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 1929 | if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count)) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1930 | return -EOPNOTSUPP; |
| 1931 | |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 1932 | if (dev->phydev && !ops->get_ethtool_phy_stats) |
| 1933 | n_stats = phy_ethtool_get_sset_count(dev->phydev); |
| 1934 | else |
| 1935 | n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1936 | if (n_stats < 0) |
| 1937 | return n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1938 | if (n_stats > S32_MAX / sizeof(u64)) |
| 1939 | return -ENOMEM; |
| 1940 | WARN_ON_ONCE(!n_stats); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1941 | |
| 1942 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 1943 | return -EFAULT; |
| 1944 | |
| 1945 | stats.n_stats = n_stats; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 1946 | data = vzalloc(array_size(n_stats, sizeof(u64))); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1947 | if (n_stats && !data) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1948 | return -ENOMEM; |
| 1949 | |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 1950 | if (dev->phydev && !ops->get_ethtool_phy_stats) { |
| 1951 | ret = phy_ethtool_get_stats(dev->phydev, &stats, data); |
| 1952 | if (ret < 0) |
| 1953 | return ret; |
| 1954 | } else { |
| 1955 | ops->get_ethtool_phy_stats(dev, &stats, data); |
| 1956 | } |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1957 | |
| 1958 | ret = -EFAULT; |
| 1959 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 1960 | goto out; |
| 1961 | useraddr += sizeof(stats); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1962 | if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1963 | goto out; |
| 1964 | ret = 0; |
| 1965 | |
| 1966 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1967 | vfree(data); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1968 | return ret; |
| 1969 | } |
| 1970 | |
viro@ftp.linux.org.uk | 0bf0519d | 2005-09-05 03:26:18 +0100 | [diff] [blame] | 1971 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1972 | { |
| 1973 | struct ethtool_perm_addr epaddr; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1974 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 1975 | if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1976 | return -EFAULT; |
| 1977 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 1978 | if (epaddr.size < dev->addr_len) |
| 1979 | return -ETOOSMALL; |
| 1980 | epaddr.size = dev->addr_len; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1981 | |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1982 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 1983 | return -EFAULT; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1984 | useraddr += sizeof(epaddr); |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 1985 | if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) |
| 1986 | return -EFAULT; |
| 1987 | return 0; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1990 | static int ethtool_get_value(struct net_device *dev, char __user *useraddr, |
| 1991 | u32 cmd, u32 (*actor)(struct net_device *)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1992 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1993 | struct ethtool_value edata = { .cmd = cmd }; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1994 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1995 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1996 | return -EOPNOTSUPP; |
| 1997 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1998 | edata.data = actor(dev); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1999 | |
| 2000 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 2001 | return -EFAULT; |
| 2002 | return 0; |
| 2003 | } |
| 2004 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2005 | static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, |
| 2006 | void (*actor)(struct net_device *, u32)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2007 | { |
| 2008 | struct ethtool_value edata; |
| 2009 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2010 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2011 | return -EOPNOTSUPP; |
| 2012 | |
| 2013 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 2014 | return -EFAULT; |
| 2015 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2016 | actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2017 | return 0; |
| 2018 | } |
| 2019 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2020 | static int ethtool_set_value(struct net_device *dev, char __user *useraddr, |
| 2021 | int (*actor)(struct net_device *, u32)) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2022 | { |
| 2023 | struct ethtool_value edata; |
| 2024 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2025 | if (!actor) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2026 | return -EOPNOTSUPP; |
| 2027 | |
| 2028 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 2029 | return -EFAULT; |
| 2030 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2031 | return actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2034 | static noinline_for_stack int ethtool_flash_device(struct net_device *dev, |
| 2035 | char __user *useraddr) |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2036 | { |
| 2037 | struct ethtool_flash efl; |
| 2038 | |
| 2039 | if (copy_from_user(&efl, useraddr, sizeof(efl))) |
| 2040 | return -EFAULT; |
Ben Hutchings | 786f528 | 2012-02-01 09:32:25 +0000 | [diff] [blame] | 2041 | efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; |
| 2042 | |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame^] | 2043 | if (!dev->ethtool_ops->flash_device) { |
| 2044 | int ret; |
| 2045 | |
| 2046 | rtnl_unlock(); |
| 2047 | ret = devlink_compat_flash_update(dev, efl.data); |
| 2048 | rtnl_lock(); |
| 2049 | |
| 2050 | return ret; |
| 2051 | } |
| 2052 | |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2053 | return dev->ethtool_ops->flash_device(dev, &efl); |
| 2054 | } |
| 2055 | |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2056 | static int ethtool_set_dump(struct net_device *dev, |
| 2057 | void __user *useraddr) |
| 2058 | { |
| 2059 | struct ethtool_dump dump; |
| 2060 | |
| 2061 | if (!dev->ethtool_ops->set_dump) |
| 2062 | return -EOPNOTSUPP; |
| 2063 | |
| 2064 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2065 | return -EFAULT; |
| 2066 | |
| 2067 | return dev->ethtool_ops->set_dump(dev, &dump); |
| 2068 | } |
| 2069 | |
| 2070 | static int ethtool_get_dump_flag(struct net_device *dev, |
| 2071 | void __user *useraddr) |
| 2072 | { |
| 2073 | int ret; |
| 2074 | struct ethtool_dump dump; |
| 2075 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2076 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2077 | if (!ops->get_dump_flag) |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2078 | return -EOPNOTSUPP; |
| 2079 | |
| 2080 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2081 | return -EFAULT; |
| 2082 | |
| 2083 | ret = ops->get_dump_flag(dev, &dump); |
| 2084 | if (ret) |
| 2085 | return ret; |
| 2086 | |
| 2087 | if (copy_to_user(useraddr, &dump, sizeof(dump))) |
| 2088 | return -EFAULT; |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | static int ethtool_get_dump_data(struct net_device *dev, |
| 2093 | void __user *useraddr) |
| 2094 | { |
| 2095 | int ret; |
| 2096 | __u32 len; |
| 2097 | struct ethtool_dump dump, tmp; |
| 2098 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2099 | void *data = NULL; |
| 2100 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2101 | if (!ops->get_dump_data || !ops->get_dump_flag) |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2102 | return -EOPNOTSUPP; |
| 2103 | |
| 2104 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2105 | return -EFAULT; |
| 2106 | |
| 2107 | memset(&tmp, 0, sizeof(tmp)); |
| 2108 | tmp.cmd = ETHTOOL_GET_DUMP_FLAG; |
| 2109 | ret = ops->get_dump_flag(dev, &tmp); |
| 2110 | if (ret) |
| 2111 | return ret; |
| 2112 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2113 | len = min(tmp.len, dump.len); |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2114 | if (!len) |
| 2115 | return -EFAULT; |
| 2116 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2117 | /* Don't ever let the driver think there's more space available |
| 2118 | * than it requested with .get_dump_flag(). |
| 2119 | */ |
| 2120 | dump.len = len; |
| 2121 | |
| 2122 | /* Always allocate enough space to hold the whole thing so that the |
| 2123 | * driver does not need to check the length and bother with partial |
| 2124 | * dumping. |
| 2125 | */ |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2126 | data = vzalloc(tmp.len); |
| 2127 | if (!data) |
| 2128 | return -ENOMEM; |
| 2129 | ret = ops->get_dump_data(dev, &dump, data); |
| 2130 | if (ret) |
| 2131 | goto out; |
| 2132 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2133 | /* There are two sane possibilities: |
| 2134 | * 1. The driver's .get_dump_data() does not touch dump.len. |
| 2135 | * 2. Or it may set dump.len to how much it really writes, which |
| 2136 | * should be tmp.len (or len if it can do a partial dump). |
| 2137 | * In any case respond to userspace with the actual length of data |
| 2138 | * it's receiving. |
| 2139 | */ |
| 2140 | WARN_ON(dump.len != len && dump.len != tmp.len); |
| 2141 | dump.len = len; |
| 2142 | |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2143 | if (copy_to_user(useraddr, &dump, sizeof(dump))) { |
| 2144 | ret = -EFAULT; |
| 2145 | goto out; |
| 2146 | } |
| 2147 | useraddr += offsetof(struct ethtool_dump, data); |
| 2148 | if (copy_to_user(useraddr, data, len)) |
| 2149 | ret = -EFAULT; |
| 2150 | out: |
| 2151 | vfree(data); |
| 2152 | return ret; |
| 2153 | } |
| 2154 | |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2155 | static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) |
| 2156 | { |
| 2157 | int err = 0; |
| 2158 | struct ethtool_ts_info info; |
| 2159 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2160 | struct phy_device *phydev = dev->phydev; |
| 2161 | |
| 2162 | memset(&info, 0, sizeof(info)); |
| 2163 | info.cmd = ETHTOOL_GET_TS_INFO; |
| 2164 | |
| 2165 | if (phydev && phydev->drv && phydev->drv->ts_info) { |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2166 | err = phydev->drv->ts_info(phydev, &info); |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2167 | } else if (ops->get_ts_info) { |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2168 | err = ops->get_ts_info(dev, &info); |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2169 | } else { |
| 2170 | info.so_timestamping = |
| 2171 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 2172 | SOF_TIMESTAMPING_SOFTWARE; |
| 2173 | info.phc_index = -1; |
| 2174 | } |
| 2175 | |
| 2176 | if (err) |
| 2177 | return err; |
| 2178 | |
| 2179 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 2180 | err = -EFAULT; |
| 2181 | |
| 2182 | return err; |
| 2183 | } |
| 2184 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2185 | static int __ethtool_get_module_info(struct net_device *dev, |
| 2186 | struct ethtool_modinfo *modinfo) |
| 2187 | { |
| 2188 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2189 | struct phy_device *phydev = dev->phydev; |
| 2190 | |
Russell King | e679c9c | 2018-03-28 15:44:16 -0700 | [diff] [blame] | 2191 | if (dev->sfp_bus) |
| 2192 | return sfp_get_module_info(dev->sfp_bus, modinfo); |
| 2193 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2194 | if (phydev && phydev->drv && phydev->drv->module_info) |
| 2195 | return phydev->drv->module_info(phydev, modinfo); |
| 2196 | |
| 2197 | if (ops->get_module_info) |
| 2198 | return ops->get_module_info(dev, modinfo); |
| 2199 | |
| 2200 | return -EOPNOTSUPP; |
| 2201 | } |
| 2202 | |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2203 | static int ethtool_get_module_info(struct net_device *dev, |
| 2204 | void __user *useraddr) |
| 2205 | { |
| 2206 | int ret; |
| 2207 | struct ethtool_modinfo modinfo; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2208 | |
| 2209 | if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) |
| 2210 | return -EFAULT; |
| 2211 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2212 | ret = __ethtool_get_module_info(dev, &modinfo); |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2213 | if (ret) |
| 2214 | return ret; |
| 2215 | |
| 2216 | if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) |
| 2217 | return -EFAULT; |
| 2218 | |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2222 | static int __ethtool_get_module_eeprom(struct net_device *dev, |
| 2223 | struct ethtool_eeprom *ee, u8 *data) |
| 2224 | { |
| 2225 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2226 | struct phy_device *phydev = dev->phydev; |
| 2227 | |
Russell King | e679c9c | 2018-03-28 15:44:16 -0700 | [diff] [blame] | 2228 | if (dev->sfp_bus) |
| 2229 | return sfp_get_module_eeprom(dev->sfp_bus, ee, data); |
| 2230 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2231 | if (phydev && phydev->drv && phydev->drv->module_eeprom) |
| 2232 | return phydev->drv->module_eeprom(phydev, ee, data); |
| 2233 | |
| 2234 | if (ops->get_module_eeprom) |
| 2235 | return ops->get_module_eeprom(dev, ee, data); |
| 2236 | |
| 2237 | return -EOPNOTSUPP; |
| 2238 | } |
| 2239 | |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2240 | static int ethtool_get_module_eeprom(struct net_device *dev, |
| 2241 | void __user *useraddr) |
| 2242 | { |
| 2243 | int ret; |
| 2244 | struct ethtool_modinfo modinfo; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2245 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2246 | ret = __ethtool_get_module_info(dev, &modinfo); |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2247 | if (ret) |
| 2248 | return ret; |
| 2249 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2250 | return ethtool_get_any_eeprom(dev, useraddr, |
| 2251 | __ethtool_get_module_eeprom, |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2252 | modinfo.eeprom_len); |
| 2253 | } |
| 2254 | |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2255 | static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) |
| 2256 | { |
| 2257 | switch (tuna->id) { |
| 2258 | case ETHTOOL_RX_COPYBREAK: |
Eric Dumazet | 1255a50 | 2014-10-05 12:35:21 +0300 | [diff] [blame] | 2259 | case ETHTOOL_TX_COPYBREAK: |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2260 | if (tuna->len != sizeof(u32) || |
| 2261 | tuna->type_id != ETHTOOL_TUNABLE_U32) |
| 2262 | return -EINVAL; |
| 2263 | break; |
Inbar Karmy | e1577c1 | 2017-11-20 16:14:30 +0200 | [diff] [blame] | 2264 | case ETHTOOL_PFC_PREVENTION_TOUT: |
| 2265 | if (tuna->len != sizeof(u16) || |
| 2266 | tuna->type_id != ETHTOOL_TUNABLE_U16) |
| 2267 | return -EINVAL; |
| 2268 | break; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2269 | default: |
| 2270 | return -EINVAL; |
| 2271 | } |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) |
| 2277 | { |
| 2278 | int ret; |
| 2279 | struct ethtool_tunable tuna; |
| 2280 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2281 | void *data; |
| 2282 | |
| 2283 | if (!ops->get_tunable) |
| 2284 | return -EOPNOTSUPP; |
| 2285 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2286 | return -EFAULT; |
| 2287 | ret = ethtool_tunable_valid(&tuna); |
| 2288 | if (ret) |
| 2289 | return ret; |
| 2290 | data = kmalloc(tuna.len, GFP_USER); |
| 2291 | if (!data) |
| 2292 | return -ENOMEM; |
| 2293 | ret = ops->get_tunable(dev, &tuna, data); |
| 2294 | if (ret) |
| 2295 | goto out; |
| 2296 | useraddr += sizeof(tuna); |
| 2297 | ret = -EFAULT; |
| 2298 | if (copy_to_user(useraddr, data, tuna.len)) |
| 2299 | goto out; |
| 2300 | ret = 0; |
| 2301 | |
| 2302 | out: |
| 2303 | kfree(data); |
| 2304 | return ret; |
| 2305 | } |
| 2306 | |
| 2307 | static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) |
| 2308 | { |
| 2309 | int ret; |
| 2310 | struct ethtool_tunable tuna; |
| 2311 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2312 | void *data; |
| 2313 | |
| 2314 | if (!ops->set_tunable) |
| 2315 | return -EOPNOTSUPP; |
| 2316 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2317 | return -EFAULT; |
| 2318 | ret = ethtool_tunable_valid(&tuna); |
| 2319 | if (ret) |
| 2320 | return ret; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2321 | useraddr += sizeof(tuna); |
Al Viro | 30e7e3e | 2017-05-13 18:31:26 -0400 | [diff] [blame] | 2322 | data = memdup_user(useraddr, tuna.len); |
| 2323 | if (IS_ERR(data)) |
| 2324 | return PTR_ERR(data); |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2325 | ret = ops->set_tunable(dev, &tuna, data); |
| 2326 | |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2327 | kfree(data); |
| 2328 | return ret; |
| 2329 | } |
| 2330 | |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2331 | static int ethtool_get_per_queue_coalesce(struct net_device *dev, |
| 2332 | void __user *useraddr, |
| 2333 | struct ethtool_per_queue_op *per_queue_opt) |
| 2334 | { |
| 2335 | u32 bit; |
| 2336 | int ret; |
| 2337 | DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); |
| 2338 | |
| 2339 | if (!dev->ethtool_ops->get_per_queue_coalesce) |
| 2340 | return -EOPNOTSUPP; |
| 2341 | |
| 2342 | useraddr += sizeof(*per_queue_opt); |
| 2343 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 2344 | bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, |
| 2345 | MAX_NUM_QUEUE); |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2346 | |
| 2347 | for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { |
| 2348 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
| 2349 | |
| 2350 | ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); |
| 2351 | if (ret != 0) |
| 2352 | return ret; |
| 2353 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 2354 | return -EFAULT; |
| 2355 | useraddr += sizeof(coalesce); |
| 2356 | } |
| 2357 | |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2361 | static int ethtool_set_per_queue_coalesce(struct net_device *dev, |
| 2362 | void __user *useraddr, |
| 2363 | struct ethtool_per_queue_op *per_queue_opt) |
| 2364 | { |
| 2365 | u32 bit; |
| 2366 | int i, ret = 0; |
| 2367 | int n_queue; |
| 2368 | struct ethtool_coalesce *backup = NULL, *tmp = NULL; |
| 2369 | DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); |
| 2370 | |
| 2371 | if ((!dev->ethtool_ops->set_per_queue_coalesce) || |
| 2372 | (!dev->ethtool_ops->get_per_queue_coalesce)) |
| 2373 | return -EOPNOTSUPP; |
| 2374 | |
| 2375 | useraddr += sizeof(*per_queue_opt); |
| 2376 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 2377 | bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2378 | n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); |
| 2379 | tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); |
| 2380 | if (!backup) |
| 2381 | return -ENOMEM; |
| 2382 | |
| 2383 | for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { |
| 2384 | struct ethtool_coalesce coalesce; |
| 2385 | |
| 2386 | ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); |
| 2387 | if (ret != 0) |
| 2388 | goto roll_back; |
| 2389 | |
| 2390 | tmp++; |
| 2391 | |
| 2392 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { |
| 2393 | ret = -EFAULT; |
| 2394 | goto roll_back; |
| 2395 | } |
| 2396 | |
| 2397 | ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); |
| 2398 | if (ret != 0) |
| 2399 | goto roll_back; |
| 2400 | |
| 2401 | useraddr += sizeof(coalesce); |
| 2402 | } |
| 2403 | |
| 2404 | roll_back: |
| 2405 | if (ret != 0) { |
| 2406 | tmp = backup; |
| 2407 | for_each_set_bit(i, queue_mask, bit) { |
| 2408 | dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); |
| 2409 | tmp++; |
| 2410 | } |
| 2411 | } |
| 2412 | kfree(backup); |
| 2413 | |
| 2414 | return ret; |
| 2415 | } |
| 2416 | |
Wenwen Wang | 58f5bbe | 2018-10-08 10:49:35 -0500 | [diff] [blame] | 2417 | static int ethtool_set_per_queue(struct net_device *dev, |
| 2418 | void __user *useraddr, u32 sub_cmd) |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2419 | { |
| 2420 | struct ethtool_per_queue_op per_queue_opt; |
| 2421 | |
| 2422 | if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) |
| 2423 | return -EFAULT; |
| 2424 | |
Wenwen Wang | 58f5bbe | 2018-10-08 10:49:35 -0500 | [diff] [blame] | 2425 | if (per_queue_opt.sub_command != sub_cmd) |
| 2426 | return -EINVAL; |
| 2427 | |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2428 | switch (per_queue_opt.sub_command) { |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2429 | case ETHTOOL_GCOALESCE: |
| 2430 | return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2431 | case ETHTOOL_SCOALESCE: |
| 2432 | return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2433 | default: |
| 2434 | return -EOPNOTSUPP; |
| 2435 | }; |
| 2436 | } |
| 2437 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2438 | static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) |
| 2439 | { |
| 2440 | switch (tuna->id) { |
Raju Lakkaraju | 65feddd | 2016-11-17 13:07:23 +0100 | [diff] [blame] | 2441 | case ETHTOOL_PHY_DOWNSHIFT: |
| 2442 | if (tuna->len != sizeof(u8) || |
| 2443 | tuna->type_id != ETHTOOL_TUNABLE_U8) |
| 2444 | return -EINVAL; |
| 2445 | break; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2446 | default: |
| 2447 | return -EINVAL; |
| 2448 | } |
| 2449 | |
| 2450 | return 0; |
| 2451 | } |
| 2452 | |
| 2453 | static int get_phy_tunable(struct net_device *dev, void __user *useraddr) |
| 2454 | { |
| 2455 | int ret; |
| 2456 | struct ethtool_tunable tuna; |
| 2457 | struct phy_device *phydev = dev->phydev; |
| 2458 | void *data; |
| 2459 | |
| 2460 | if (!(phydev && phydev->drv && phydev->drv->get_tunable)) |
| 2461 | return -EOPNOTSUPP; |
| 2462 | |
| 2463 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2464 | return -EFAULT; |
| 2465 | ret = ethtool_phy_tunable_valid(&tuna); |
| 2466 | if (ret) |
| 2467 | return ret; |
| 2468 | data = kmalloc(tuna.len, GFP_USER); |
| 2469 | if (!data) |
| 2470 | return -ENOMEM; |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2471 | mutex_lock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2472 | ret = phydev->drv->get_tunable(phydev, &tuna, data); |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2473 | mutex_unlock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2474 | if (ret) |
| 2475 | goto out; |
| 2476 | useraddr += sizeof(tuna); |
| 2477 | ret = -EFAULT; |
| 2478 | if (copy_to_user(useraddr, data, tuna.len)) |
| 2479 | goto out; |
| 2480 | ret = 0; |
| 2481 | |
| 2482 | out: |
| 2483 | kfree(data); |
| 2484 | return ret; |
| 2485 | } |
| 2486 | |
| 2487 | static int set_phy_tunable(struct net_device *dev, void __user *useraddr) |
| 2488 | { |
| 2489 | int ret; |
| 2490 | struct ethtool_tunable tuna; |
| 2491 | struct phy_device *phydev = dev->phydev; |
| 2492 | void *data; |
| 2493 | |
| 2494 | if (!(phydev && phydev->drv && phydev->drv->set_tunable)) |
| 2495 | return -EOPNOTSUPP; |
| 2496 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2497 | return -EFAULT; |
| 2498 | ret = ethtool_phy_tunable_valid(&tuna); |
| 2499 | if (ret) |
| 2500 | return ret; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2501 | useraddr += sizeof(tuna); |
Al Viro | 30e7e3e | 2017-05-13 18:31:26 -0400 | [diff] [blame] | 2502 | data = memdup_user(useraddr, tuna.len); |
| 2503 | if (IS_ERR(data)) |
| 2504 | return PTR_ERR(data); |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2505 | mutex_lock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2506 | ret = phydev->drv->set_tunable(phydev, &tuna, data); |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2507 | mutex_unlock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2508 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2509 | kfree(data); |
| 2510 | return ret; |
| 2511 | } |
| 2512 | |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2513 | static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) |
| 2514 | { |
| 2515 | struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM }; |
Edward Cree | a6d5051 | 2018-02-28 19:15:58 +0000 | [diff] [blame] | 2516 | int rc; |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2517 | |
| 2518 | if (!dev->ethtool_ops->get_fecparam) |
| 2519 | return -EOPNOTSUPP; |
| 2520 | |
Edward Cree | a6d5051 | 2018-02-28 19:15:58 +0000 | [diff] [blame] | 2521 | rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); |
| 2522 | if (rc) |
| 2523 | return rc; |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2524 | |
| 2525 | if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) |
| 2526 | return -EFAULT; |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
| 2530 | static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) |
| 2531 | { |
| 2532 | struct ethtool_fecparam fecparam; |
| 2533 | |
| 2534 | if (!dev->ethtool_ops->set_fecparam) |
| 2535 | return -EOPNOTSUPP; |
| 2536 | |
| 2537 | if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) |
| 2538 | return -EFAULT; |
| 2539 | |
| 2540 | return dev->ethtool_ops->set_fecparam(dev, &fecparam); |
| 2541 | } |
| 2542 | |
Yan Burman | 600fed5 | 2013-06-03 02:03:34 +0000 | [diff] [blame] | 2543 | /* The main entry point in this file. Called from net/core/dev_ioctl.c */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2545 | int dev_ethtool(struct net *net, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2547 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | void __user *useraddr = ifr->ifr_data; |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2549 | u32 ethcmd, sub_cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | int rc; |
Bjørn Mork | b29d314 | 2013-05-01 23:06:42 +0000 | [diff] [blame] | 2551 | netdev_features_t old_features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | if (!dev || !netif_device_present(dev)) |
| 2554 | return -ENODEV; |
| 2555 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2556 | if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | return -EFAULT; |
| 2558 | |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2559 | if (ethcmd == ETHTOOL_PERQUEUE) { |
| 2560 | if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) |
| 2561 | return -EFAULT; |
| 2562 | } else { |
| 2563 | sub_cmd = ethcmd; |
| 2564 | } |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2565 | /* Allow some commands to be done by anyone */ |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2566 | switch (sub_cmd) { |
stephen hemminger | 0fdc100 | 2010-08-23 10:24:18 +0000 | [diff] [blame] | 2567 | case ETHTOOL_GSET: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2568 | case ETHTOOL_GDRVINFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2569 | case ETHTOOL_GMSGLVL: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2570 | case ETHTOOL_GLINK: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2571 | case ETHTOOL_GCOALESCE: |
| 2572 | case ETHTOOL_GRINGPARAM: |
| 2573 | case ETHTOOL_GPAUSEPARAM: |
| 2574 | case ETHTOOL_GRXCSUM: |
| 2575 | case ETHTOOL_GTXCSUM: |
| 2576 | case ETHTOOL_GSG: |
Michał Mirosław | f80400a | 2012-01-22 00:20:40 +0000 | [diff] [blame] | 2577 | case ETHTOOL_GSSET_INFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2578 | case ETHTOOL_GSTRINGS: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2579 | case ETHTOOL_GSTATS: |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2580 | case ETHTOOL_GPHYSTATS: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2581 | case ETHTOOL_GTSO: |
| 2582 | case ETHTOOL_GPERMADDR: |
Maciej Żenczykowski | 474ff26 | 2018-09-22 01:34:01 -0700 | [diff] [blame] | 2583 | case ETHTOOL_GUFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2584 | case ETHTOOL_GGSO: |
stephen hemminger | 1cab819 | 2010-02-11 13:48:29 +0000 | [diff] [blame] | 2585 | case ETHTOOL_GGRO: |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2586 | case ETHTOOL_GFLAGS: |
| 2587 | case ETHTOOL_GPFLAGS: |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2588 | case ETHTOOL_GRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2589 | case ETHTOOL_GRXRINGS: |
| 2590 | case ETHTOOL_GRXCLSRLCNT: |
| 2591 | case ETHTOOL_GRXCLSRULE: |
| 2592 | case ETHTOOL_GRXCLSRLALL: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2593 | case ETHTOOL_GRXFHINDIR: |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 2594 | case ETHTOOL_GRSSH: |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2595 | case ETHTOOL_GFEATURES: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2596 | case ETHTOOL_GCHANNELS: |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2597 | case ETHTOOL_GET_TS_INFO: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2598 | case ETHTOOL_GEEE: |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2599 | case ETHTOOL_GTUNABLE: |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2600 | case ETHTOOL_PHY_GTUNABLE: |
Miroslav Lichvar | 8006f6b | 2016-11-24 10:55:06 +0100 | [diff] [blame] | 2601 | case ETHTOOL_GLINKSETTINGS: |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2602 | case ETHTOOL_GFECPARAM: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2603 | break; |
| 2604 | default: |
Eric W. Biederman | 5e1fccc | 2012-11-16 03:03:04 +0000 | [diff] [blame] | 2605 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2606 | return -EPERM; |
| 2607 | } |
| 2608 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2609 | if (dev->ethtool_ops->begin) { |
| 2610 | rc = dev->ethtool_ops->begin(dev); |
| 2611 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | return rc; |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2613 | } |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 2614 | old_features = dev->features; |
| 2615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | switch (ethcmd) { |
| 2617 | case ETHTOOL_GSET: |
| 2618 | rc = ethtool_get_settings(dev, useraddr); |
| 2619 | break; |
| 2620 | case ETHTOOL_SSET: |
| 2621 | rc = ethtool_set_settings(dev, useraddr); |
| 2622 | break; |
| 2623 | case ETHTOOL_GDRVINFO: |
| 2624 | rc = ethtool_get_drvinfo(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | break; |
| 2626 | case ETHTOOL_GREGS: |
| 2627 | rc = ethtool_get_regs(dev, useraddr); |
| 2628 | break; |
| 2629 | case ETHTOOL_GWOL: |
| 2630 | rc = ethtool_get_wol(dev, useraddr); |
| 2631 | break; |
| 2632 | case ETHTOOL_SWOL: |
| 2633 | rc = ethtool_set_wol(dev, useraddr); |
| 2634 | break; |
| 2635 | case ETHTOOL_GMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2636 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 2637 | dev->ethtool_ops->get_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | break; |
| 2639 | case ETHTOOL_SMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2640 | rc = ethtool_set_value_void(dev, useraddr, |
| 2641 | dev->ethtool_ops->set_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | break; |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 2643 | case ETHTOOL_GEEE: |
| 2644 | rc = ethtool_get_eee(dev, useraddr); |
| 2645 | break; |
| 2646 | case ETHTOOL_SEEE: |
| 2647 | rc = ethtool_set_eee(dev, useraddr); |
| 2648 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | case ETHTOOL_NWAY_RST: |
| 2650 | rc = ethtool_nway_reset(dev); |
| 2651 | break; |
| 2652 | case ETHTOOL_GLINK: |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 2653 | rc = ethtool_get_link(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | break; |
| 2655 | case ETHTOOL_GEEPROM: |
| 2656 | rc = ethtool_get_eeprom(dev, useraddr); |
| 2657 | break; |
| 2658 | case ETHTOOL_SEEPROM: |
| 2659 | rc = ethtool_set_eeprom(dev, useraddr); |
| 2660 | break; |
| 2661 | case ETHTOOL_GCOALESCE: |
| 2662 | rc = ethtool_get_coalesce(dev, useraddr); |
| 2663 | break; |
| 2664 | case ETHTOOL_SCOALESCE: |
| 2665 | rc = ethtool_set_coalesce(dev, useraddr); |
| 2666 | break; |
| 2667 | case ETHTOOL_GRINGPARAM: |
| 2668 | rc = ethtool_get_ringparam(dev, useraddr); |
| 2669 | break; |
| 2670 | case ETHTOOL_SRINGPARAM: |
| 2671 | rc = ethtool_set_ringparam(dev, useraddr); |
| 2672 | break; |
| 2673 | case ETHTOOL_GPAUSEPARAM: |
| 2674 | rc = ethtool_get_pauseparam(dev, useraddr); |
| 2675 | break; |
| 2676 | case ETHTOOL_SPAUSEPARAM: |
| 2677 | rc = ethtool_set_pauseparam(dev, useraddr); |
| 2678 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | case ETHTOOL_TEST: |
| 2680 | rc = ethtool_self_test(dev, useraddr); |
| 2681 | break; |
| 2682 | case ETHTOOL_GSTRINGS: |
| 2683 | rc = ethtool_get_strings(dev, useraddr); |
| 2684 | break; |
| 2685 | case ETHTOOL_PHYS_ID: |
| 2686 | rc = ethtool_phys_id(dev, useraddr); |
| 2687 | break; |
| 2688 | case ETHTOOL_GSTATS: |
| 2689 | rc = ethtool_get_stats(dev, useraddr); |
| 2690 | break; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2691 | case ETHTOOL_GPERMADDR: |
| 2692 | rc = ethtool_get_perm_addr(dev, useraddr); |
| 2693 | break; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2694 | case ETHTOOL_GFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2695 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2696 | __ethtool_get_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2697 | break; |
| 2698 | case ETHTOOL_SFLAGS: |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2699 | rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2700 | break; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2701 | case ETHTOOL_GPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2702 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 2703 | dev->ethtool_ops->get_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2704 | break; |
| 2705 | case ETHTOOL_SPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2706 | rc = ethtool_set_value(dev, useraddr, |
| 2707 | dev->ethtool_ops->set_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2708 | break; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2709 | case ETHTOOL_GRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2710 | case ETHTOOL_GRXRINGS: |
| 2711 | case ETHTOOL_GRXCLSRLCNT: |
| 2712 | case ETHTOOL_GRXCLSRULE: |
| 2713 | case ETHTOOL_GRXCLSRLALL: |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 2714 | rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2715 | break; |
| 2716 | case ETHTOOL_SRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2717 | case ETHTOOL_SRXCLSRLDEL: |
| 2718 | case ETHTOOL_SRXCLSRLINS: |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 2719 | rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2720 | break; |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2721 | case ETHTOOL_FLASHDEV: |
| 2722 | rc = ethtool_flash_device(dev, useraddr); |
| 2723 | break; |
Ben Hutchings | d73d3a8 | 2009-10-05 10:59:58 +0000 | [diff] [blame] | 2724 | case ETHTOOL_RESET: |
| 2725 | rc = ethtool_reset(dev, useraddr); |
| 2726 | break; |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 2727 | case ETHTOOL_GSSET_INFO: |
| 2728 | rc = ethtool_get_sset_info(dev, useraddr); |
| 2729 | break; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 2730 | case ETHTOOL_GRXFHINDIR: |
| 2731 | rc = ethtool_get_rxfh_indir(dev, useraddr); |
| 2732 | break; |
| 2733 | case ETHTOOL_SRXFHINDIR: |
| 2734 | rc = ethtool_set_rxfh_indir(dev, useraddr); |
| 2735 | break; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 2736 | case ETHTOOL_GRSSH: |
| 2737 | rc = ethtool_get_rxfh(dev, useraddr); |
| 2738 | break; |
| 2739 | case ETHTOOL_SRSSH: |
| 2740 | rc = ethtool_set_rxfh(dev, useraddr); |
| 2741 | break; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2742 | case ETHTOOL_GFEATURES: |
| 2743 | rc = ethtool_get_features(dev, useraddr); |
| 2744 | break; |
| 2745 | case ETHTOOL_SFEATURES: |
| 2746 | rc = ethtool_set_features(dev, useraddr); |
| 2747 | break; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2748 | case ETHTOOL_GTXCSUM: |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2749 | case ETHTOOL_GRXCSUM: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2750 | case ETHTOOL_GSG: |
| 2751 | case ETHTOOL_GTSO: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2752 | case ETHTOOL_GGSO: |
| 2753 | case ETHTOOL_GGRO: |
| 2754 | rc = ethtool_get_one_feature(dev, useraddr, ethcmd); |
| 2755 | break; |
| 2756 | case ETHTOOL_STXCSUM: |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2757 | case ETHTOOL_SRXCSUM: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2758 | case ETHTOOL_SSG: |
| 2759 | case ETHTOOL_STSO: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2760 | case ETHTOOL_SGSO: |
| 2761 | case ETHTOOL_SGRO: |
| 2762 | rc = ethtool_set_one_feature(dev, useraddr, ethcmd); |
| 2763 | break; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 2764 | case ETHTOOL_GCHANNELS: |
| 2765 | rc = ethtool_get_channels(dev, useraddr); |
| 2766 | break; |
| 2767 | case ETHTOOL_SCHANNELS: |
| 2768 | rc = ethtool_set_channels(dev, useraddr); |
| 2769 | break; |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2770 | case ETHTOOL_SET_DUMP: |
| 2771 | rc = ethtool_set_dump(dev, useraddr); |
| 2772 | break; |
| 2773 | case ETHTOOL_GET_DUMP_FLAG: |
| 2774 | rc = ethtool_get_dump_flag(dev, useraddr); |
| 2775 | break; |
| 2776 | case ETHTOOL_GET_DUMP_DATA: |
| 2777 | rc = ethtool_get_dump_data(dev, useraddr); |
| 2778 | break; |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2779 | case ETHTOOL_GET_TS_INFO: |
| 2780 | rc = ethtool_get_ts_info(dev, useraddr); |
| 2781 | break; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2782 | case ETHTOOL_GMODULEINFO: |
| 2783 | rc = ethtool_get_module_info(dev, useraddr); |
| 2784 | break; |
| 2785 | case ETHTOOL_GMODULEEEPROM: |
| 2786 | rc = ethtool_get_module_eeprom(dev, useraddr); |
| 2787 | break; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2788 | case ETHTOOL_GTUNABLE: |
| 2789 | rc = ethtool_get_tunable(dev, useraddr); |
| 2790 | break; |
| 2791 | case ETHTOOL_STUNABLE: |
| 2792 | rc = ethtool_set_tunable(dev, useraddr); |
| 2793 | break; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2794 | case ETHTOOL_GPHYSTATS: |
| 2795 | rc = ethtool_get_phy_stats(dev, useraddr); |
| 2796 | break; |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2797 | case ETHTOOL_PERQUEUE: |
Wenwen Wang | 58f5bbe | 2018-10-08 10:49:35 -0500 | [diff] [blame] | 2798 | rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2799 | break; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 2800 | case ETHTOOL_GLINKSETTINGS: |
| 2801 | rc = ethtool_get_link_ksettings(dev, useraddr); |
| 2802 | break; |
| 2803 | case ETHTOOL_SLINKSETTINGS: |
| 2804 | rc = ethtool_set_link_ksettings(dev, useraddr); |
| 2805 | break; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2806 | case ETHTOOL_PHY_GTUNABLE: |
| 2807 | rc = get_phy_tunable(dev, useraddr); |
| 2808 | break; |
| 2809 | case ETHTOOL_PHY_STUNABLE: |
| 2810 | rc = set_phy_tunable(dev, useraddr); |
| 2811 | break; |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2812 | case ETHTOOL_GFECPARAM: |
| 2813 | rc = ethtool_get_fecparam(dev, useraddr); |
| 2814 | break; |
| 2815 | case ETHTOOL_SFECPARAM: |
| 2816 | rc = ethtool_set_fecparam(dev, useraddr); |
| 2817 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | default: |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 2819 | rc = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2821 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 2822 | if (dev->ethtool_ops->complete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | dev->ethtool_ops->complete(dev); |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 2824 | |
| 2825 | if (old_features != dev->features) |
| 2826 | netdev_features_change(dev); |
| 2827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | } |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 2830 | |
| 2831 | struct ethtool_rx_flow_key { |
| 2832 | struct flow_dissector_key_basic basic; |
| 2833 | union { |
| 2834 | struct flow_dissector_key_ipv4_addrs ipv4; |
| 2835 | struct flow_dissector_key_ipv6_addrs ipv6; |
| 2836 | }; |
| 2837 | struct flow_dissector_key_ports tp; |
| 2838 | struct flow_dissector_key_ip ip; |
| 2839 | struct flow_dissector_key_vlan vlan; |
| 2840 | struct flow_dissector_key_eth_addrs eth_addrs; |
| 2841 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
| 2842 | |
| 2843 | struct ethtool_rx_flow_match { |
| 2844 | struct flow_dissector dissector; |
| 2845 | struct ethtool_rx_flow_key key; |
| 2846 | struct ethtool_rx_flow_key mask; |
| 2847 | }; |
| 2848 | |
| 2849 | struct ethtool_rx_flow_rule * |
| 2850 | ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) |
| 2851 | { |
| 2852 | const struct ethtool_rx_flow_spec *fs = input->fs; |
| 2853 | static struct in6_addr zero_addr = {}; |
| 2854 | struct ethtool_rx_flow_match *match; |
| 2855 | struct ethtool_rx_flow_rule *flow; |
| 2856 | struct flow_action_entry *act; |
| 2857 | |
| 2858 | flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + |
| 2859 | sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); |
| 2860 | if (!flow) |
| 2861 | return ERR_PTR(-ENOMEM); |
| 2862 | |
| 2863 | /* ethtool_rx supports only one single action per rule. */ |
| 2864 | flow->rule = flow_rule_alloc(1); |
| 2865 | if (!flow->rule) { |
| 2866 | kfree(flow); |
| 2867 | return ERR_PTR(-ENOMEM); |
| 2868 | } |
| 2869 | |
| 2870 | match = (struct ethtool_rx_flow_match *)flow->priv; |
| 2871 | flow->rule->match.dissector = &match->dissector; |
| 2872 | flow->rule->match.mask = &match->mask; |
| 2873 | flow->rule->match.key = &match->key; |
| 2874 | |
| 2875 | match->mask.basic.n_proto = htons(0xffff); |
| 2876 | |
| 2877 | switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { |
| 2878 | case TCP_V4_FLOW: |
| 2879 | case UDP_V4_FLOW: { |
| 2880 | const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; |
| 2881 | |
| 2882 | match->key.basic.n_proto = htons(ETH_P_IP); |
| 2883 | |
| 2884 | v4_spec = &fs->h_u.tcp_ip4_spec; |
| 2885 | v4_m_spec = &fs->m_u.tcp_ip4_spec; |
| 2886 | |
| 2887 | if (v4_m_spec->ip4src) { |
| 2888 | match->key.ipv4.src = v4_spec->ip4src; |
| 2889 | match->mask.ipv4.src = v4_m_spec->ip4src; |
| 2890 | } |
| 2891 | if (v4_m_spec->ip4dst) { |
| 2892 | match->key.ipv4.dst = v4_spec->ip4dst; |
| 2893 | match->mask.ipv4.dst = v4_m_spec->ip4dst; |
| 2894 | } |
| 2895 | if (v4_m_spec->ip4src || |
| 2896 | v4_m_spec->ip4dst) { |
| 2897 | match->dissector.used_keys |= |
| 2898 | BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS); |
| 2899 | match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = |
| 2900 | offsetof(struct ethtool_rx_flow_key, ipv4); |
| 2901 | } |
| 2902 | if (v4_m_spec->psrc) { |
| 2903 | match->key.tp.src = v4_spec->psrc; |
| 2904 | match->mask.tp.src = v4_m_spec->psrc; |
| 2905 | } |
| 2906 | if (v4_m_spec->pdst) { |
| 2907 | match->key.tp.dst = v4_spec->pdst; |
| 2908 | match->mask.tp.dst = v4_m_spec->pdst; |
| 2909 | } |
| 2910 | if (v4_m_spec->psrc || |
| 2911 | v4_m_spec->pdst) { |
| 2912 | match->dissector.used_keys |= |
| 2913 | BIT(FLOW_DISSECTOR_KEY_PORTS); |
| 2914 | match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = |
| 2915 | offsetof(struct ethtool_rx_flow_key, tp); |
| 2916 | } |
| 2917 | if (v4_m_spec->tos) { |
| 2918 | match->key.ip.tos = v4_spec->tos; |
| 2919 | match->mask.ip.tos = v4_m_spec->tos; |
| 2920 | match->dissector.used_keys |= |
| 2921 | BIT(FLOW_DISSECTOR_KEY_IP); |
| 2922 | match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = |
| 2923 | offsetof(struct ethtool_rx_flow_key, ip); |
| 2924 | } |
| 2925 | } |
| 2926 | break; |
| 2927 | case TCP_V6_FLOW: |
| 2928 | case UDP_V6_FLOW: { |
| 2929 | const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; |
| 2930 | |
| 2931 | match->key.basic.n_proto = htons(ETH_P_IPV6); |
| 2932 | |
| 2933 | v6_spec = &fs->h_u.tcp_ip6_spec; |
| 2934 | v6_m_spec = &fs->m_u.tcp_ip6_spec; |
| 2935 | if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) { |
| 2936 | memcpy(&match->key.ipv6.src, v6_spec->ip6src, |
| 2937 | sizeof(match->key.ipv6.src)); |
| 2938 | memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, |
| 2939 | sizeof(match->mask.ipv6.src)); |
| 2940 | } |
| 2941 | if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { |
| 2942 | memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, |
| 2943 | sizeof(match->key.ipv6.dst)); |
| 2944 | memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, |
| 2945 | sizeof(match->mask.ipv6.dst)); |
| 2946 | } |
| 2947 | if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) || |
| 2948 | memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) { |
| 2949 | match->dissector.used_keys |= |
| 2950 | BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS); |
| 2951 | match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = |
| 2952 | offsetof(struct ethtool_rx_flow_key, ipv6); |
| 2953 | } |
| 2954 | if (v6_m_spec->psrc) { |
| 2955 | match->key.tp.src = v6_spec->psrc; |
| 2956 | match->mask.tp.src = v6_m_spec->psrc; |
| 2957 | } |
| 2958 | if (v6_m_spec->pdst) { |
| 2959 | match->key.tp.dst = v6_spec->pdst; |
| 2960 | match->mask.tp.dst = v6_m_spec->pdst; |
| 2961 | } |
| 2962 | if (v6_m_spec->psrc || |
| 2963 | v6_m_spec->pdst) { |
| 2964 | match->dissector.used_keys |= |
| 2965 | BIT(FLOW_DISSECTOR_KEY_PORTS); |
| 2966 | match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = |
| 2967 | offsetof(struct ethtool_rx_flow_key, tp); |
| 2968 | } |
| 2969 | if (v6_m_spec->tclass) { |
| 2970 | match->key.ip.tos = v6_spec->tclass; |
| 2971 | match->mask.ip.tos = v6_m_spec->tclass; |
| 2972 | match->dissector.used_keys |= |
| 2973 | BIT(FLOW_DISSECTOR_KEY_IP); |
| 2974 | match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = |
| 2975 | offsetof(struct ethtool_rx_flow_key, ip); |
| 2976 | } |
| 2977 | } |
| 2978 | break; |
| 2979 | default: |
| 2980 | ethtool_rx_flow_rule_destroy(flow); |
| 2981 | return ERR_PTR(-EINVAL); |
| 2982 | } |
| 2983 | |
| 2984 | switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { |
| 2985 | case TCP_V4_FLOW: |
| 2986 | case TCP_V6_FLOW: |
| 2987 | match->key.basic.ip_proto = IPPROTO_TCP; |
| 2988 | break; |
| 2989 | case UDP_V4_FLOW: |
| 2990 | case UDP_V6_FLOW: |
| 2991 | match->key.basic.ip_proto = IPPROTO_UDP; |
| 2992 | break; |
| 2993 | } |
| 2994 | match->mask.basic.ip_proto = 0xff; |
| 2995 | |
| 2996 | match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC); |
| 2997 | match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = |
| 2998 | offsetof(struct ethtool_rx_flow_key, basic); |
| 2999 | |
| 3000 | if (fs->flow_type & FLOW_EXT) { |
| 3001 | const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; |
| 3002 | const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; |
| 3003 | |
| 3004 | if (ext_m_spec->vlan_etype && |
| 3005 | ext_m_spec->vlan_tci) { |
| 3006 | match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; |
| 3007 | match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; |
| 3008 | |
| 3009 | match->key.vlan.vlan_id = |
| 3010 | ntohs(ext_h_spec->vlan_tci) & 0x0fff; |
| 3011 | match->mask.vlan.vlan_id = |
| 3012 | ntohs(ext_m_spec->vlan_tci) & 0x0fff; |
| 3013 | |
| 3014 | match->key.vlan.vlan_priority = |
| 3015 | (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; |
| 3016 | match->mask.vlan.vlan_priority = |
| 3017 | (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; |
| 3018 | |
| 3019 | match->dissector.used_keys |= |
| 3020 | BIT(FLOW_DISSECTOR_KEY_VLAN); |
| 3021 | match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = |
| 3022 | offsetof(struct ethtool_rx_flow_key, vlan); |
| 3023 | } |
| 3024 | } |
| 3025 | if (fs->flow_type & FLOW_MAC_EXT) { |
| 3026 | const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; |
| 3027 | const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; |
| 3028 | |
Nathan Chancellor | 8b34ec6 | 2019-02-07 21:46:53 -0700 | [diff] [blame] | 3029 | memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, |
| 3030 | ETH_ALEN); |
| 3031 | memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, |
| 3032 | ETH_ALEN); |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3033 | |
Nathan Chancellor | 8b34ec6 | 2019-02-07 21:46:53 -0700 | [diff] [blame] | 3034 | match->dissector.used_keys |= |
| 3035 | BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS); |
| 3036 | match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = |
| 3037 | offsetof(struct ethtool_rx_flow_key, eth_addrs); |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | act = &flow->rule->action.entries[0]; |
| 3041 | switch (fs->ring_cookie) { |
| 3042 | case RX_CLS_FLOW_DISC: |
| 3043 | act->id = FLOW_ACTION_DROP; |
| 3044 | break; |
| 3045 | case RX_CLS_FLOW_WAKE: |
| 3046 | act->id = FLOW_ACTION_WAKE; |
| 3047 | break; |
| 3048 | default: |
| 3049 | act->id = FLOW_ACTION_QUEUE; |
| 3050 | if (fs->flow_type & FLOW_RSS) |
| 3051 | act->queue.ctx = input->rss_ctx; |
| 3052 | |
| 3053 | act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); |
| 3054 | act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); |
| 3055 | break; |
| 3056 | } |
| 3057 | |
| 3058 | return flow; |
| 3059 | } |
| 3060 | EXPORT_SYMBOL(ethtool_rx_flow_rule_create); |
| 3061 | |
| 3062 | void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) |
| 3063 | { |
| 3064 | kfree(flow->rule); |
| 3065 | kfree(flow); |
| 3066 | } |
| 3067 | EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); |