blob: 03416e6dd5d7b0c2bb4dbfc805832e4fb67fba92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * 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 Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Russell Kinge679c9c2018-03-28 15:44:16 -070025#include <linux/sfp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010027#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080029#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
35 */
36
37u32 ethtool_op_get_link(struct net_device *dev)
38{
39 return netif_carrier_ok(dev) ? 1 : 0;
40}
chavey97f8aef2010-04-07 21:54:42 -070041EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Richard Cochran02eacbd2012-04-03 22:59:22 +000043int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44{
45 info->so_timestamping =
46 SOF_TIMESTAMPING_TX_SOFTWARE |
47 SOF_TIMESTAMPING_RX_SOFTWARE |
48 SOF_TIMESTAMPING_SOFTWARE;
49 info->phc_index = -1;
50 return 0;
51}
52EXPORT_SYMBOL(ethtool_op_get_ts_info);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Handlers for each ethtool command */
55
Michał Mirosław475414f2011-11-15 15:29:55 +000056#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000057
Michał Mirosław9d921542011-11-15 15:29:55 +000058static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59 [NETIF_F_SG_BIT] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000061 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT] = "highdma",
64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090065 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000066
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090067 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040069 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000072 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
76 [NETIF_F_GRO_BIT] = "rx-gro",
Michael Chanfb1f5f72017-12-16 03:09:40 -050077 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
Michał Mirosław9d921542011-11-15 15:29:55 +000078 [NETIF_F_LRO_BIT] = "rx-lro",
79
80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000081 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
Alexander Duyckcbc53e02016-04-10 21:44:51 -040083 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000084 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000086 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040087 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Tom Herbert7e133182016-05-18 09:06:10 -070088 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000090 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040091 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Alexander Duyck802ab552016-04-10 21:45:03 -040092 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030093 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +020094 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000095
96 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080097 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000098 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
99 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
100 [NETIF_F_RXHASH_BIT] = "rx-hashing",
101 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
102 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
103 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000104 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000105 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800106 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800107 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200108 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
109 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200110 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Atul Guptae0be6be2018-03-31 21:41:53 +0530111 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
Michał Mirosław9d921542011-11-15 15:29:55 +0000112};
113
Eyal Perry892311f2014-12-02 18:12:10 +0200114static const char
115rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
116 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
117 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800118 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200119};
120
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300121static const char
122tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
123 [ETHTOOL_ID_UNSPEC] = "Unspec",
124 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
125 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
Inbar Karmye1577c12017-11-20 16:14:30 +0200126 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300127};
128
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100129static const char
130phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
131 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100132 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100133};
134
Michał Mirosław5455c692011-02-15 16:59:17 +0000135static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
136{
137 struct ethtool_gfeatures cmd = {
138 .cmd = ETHTOOL_GFEATURES,
139 .size = ETHTOOL_DEV_FEATURE_WORDS,
140 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000141 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000142 u32 __user *sizeaddr;
143 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000144 int i;
145
146 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000147 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000148
149 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000150 features[i].available = (u32)(dev->hw_features >> (32 * i));
151 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
152 features[i].active = (u32)(dev->features >> (32 * i));
153 features[i].never_changed =
154 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000155 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000156
157 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
158 if (get_user(copy_size, sizeaddr))
159 return -EFAULT;
160
161 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
162 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
163
164 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
165 return -EFAULT;
166 useraddr += sizeof(cmd);
167 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
168 return -EFAULT;
169
170 return 0;
171}
172
173static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
174{
175 struct ethtool_sfeatures cmd;
176 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000177 netdev_features_t wanted = 0, valid = 0;
178 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000179
180 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
181 return -EFAULT;
182 useraddr += sizeof(cmd);
183
184 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
185 return -EINVAL;
186
187 if (copy_from_user(features, useraddr, sizeof(features)))
188 return -EFAULT;
189
Michał Mirosław475414f2011-11-15 15:29:55 +0000190 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000191 valid |= (netdev_features_t)features[i].valid << (32 * i);
192 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000193 }
194
195 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000196 return -EINVAL;
197
Michał Mirosław475414f2011-11-15 15:29:55 +0000198 if (valid & ~dev->hw_features) {
199 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000200 ret |= ETHTOOL_F_UNSUPPORTED;
201 }
202
Michał Mirosław475414f2011-11-15 15:29:55 +0000203 dev->wanted_features &= ~valid;
204 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700205 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000206
Michał Mirosław475414f2011-11-15 15:29:55 +0000207 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000208 ret |= ETHTOOL_F_WISH;
209
210 return ret;
211}
212
Andrew Lunnf3a40942015-12-30 16:28:25 +0100213static int phy_get_sset_count(struct phy_device *phydev)
214{
215 int ret;
216
217 if (phydev->drv->get_sset_count &&
218 phydev->drv->get_strings &&
219 phydev->drv->get_stats) {
220 mutex_lock(&phydev->lock);
221 ret = phydev->drv->get_sset_count(phydev);
222 mutex_unlock(&phydev->lock);
223
224 return ret;
225 }
226
227 return -EOPNOTSUPP;
228}
229
Michał Mirosław340ae162011-02-15 16:59:16 +0000230static int __ethtool_get_sset_count(struct net_device *dev, int sset)
231{
232 const struct ethtool_ops *ops = dev->ethtool_ops;
233
Michał Mirosław5455c692011-02-15 16:59:17 +0000234 if (sset == ETH_SS_FEATURES)
235 return ARRAY_SIZE(netdev_features_strings);
236
Eyal Perry892311f2014-12-02 18:12:10 +0200237 if (sset == ETH_SS_RSS_HASH_FUNCS)
238 return ARRAY_SIZE(rss_hash_func_strings);
239
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300240 if (sset == ETH_SS_TUNABLES)
241 return ARRAY_SIZE(tunable_strings);
242
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100243 if (sset == ETH_SS_PHY_TUNABLES)
244 return ARRAY_SIZE(phy_tunable_strings);
245
Andrew Lunnf3a40942015-12-30 16:28:25 +0100246 if (sset == ETH_SS_PHY_STATS) {
247 if (dev->phydev)
248 return phy_get_sset_count(dev->phydev);
249 else
250 return -EOPNOTSUPP;
251 }
252
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000253 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000254 return ops->get_sset_count(dev, sset);
255 else
256 return -EOPNOTSUPP;
257}
258
259static void __ethtool_get_strings(struct net_device *dev,
260 u32 stringset, u8 *data)
261{
262 const struct ethtool_ops *ops = dev->ethtool_ops;
263
Michał Mirosław5455c692011-02-15 16:59:17 +0000264 if (stringset == ETH_SS_FEATURES)
265 memcpy(data, netdev_features_strings,
266 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200267 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
268 memcpy(data, rss_hash_func_strings,
269 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300270 else if (stringset == ETH_SS_TUNABLES)
271 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100272 else if (stringset == ETH_SS_PHY_TUNABLES)
273 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100274 else if (stringset == ETH_SS_PHY_STATS) {
275 struct phy_device *phydev = dev->phydev;
276
277 if (phydev) {
278 mutex_lock(&phydev->lock);
279 phydev->drv->get_strings(phydev, data);
280 mutex_unlock(&phydev->lock);
281 } else {
282 return;
283 }
284 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000285 /* ops->get_strings is valid because checked earlier */
286 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000287}
288
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000289static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000290{
291 /* feature masks of legacy discrete ethtool ops */
292
293 switch (eth_cmd) {
294 case ETHTOOL_GTXCSUM:
295 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800296 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000297 case ETHTOOL_GRXCSUM:
298 case ETHTOOL_SRXCSUM:
299 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000300 case ETHTOOL_GSG:
301 case ETHTOOL_SSG:
302 return NETIF_F_SG;
303 case ETHTOOL_GTSO:
304 case ETHTOOL_STSO:
305 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000306 case ETHTOOL_GGSO:
307 case ETHTOOL_SGSO:
308 return NETIF_F_GSO;
309 case ETHTOOL_GGRO:
310 case ETHTOOL_SGRO:
311 return NETIF_F_GRO;
312 default:
313 BUG();
314 }
315}
316
Michał Mirosław0a417702011-02-15 16:59:17 +0000317static int ethtool_get_one_feature(struct net_device *dev,
318 char __user *useraddr, u32 ethcmd)
319{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000320 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000321 struct ethtool_value edata = {
322 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000323 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000324 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000325
Michał Mirosław0a417702011-02-15 16:59:17 +0000326 if (copy_to_user(useraddr, &edata, sizeof(edata)))
327 return -EFAULT;
328 return 0;
329}
330
Michał Mirosław0a417702011-02-15 16:59:17 +0000331static int ethtool_set_one_feature(struct net_device *dev,
332 void __user *useraddr, u32 ethcmd)
333{
334 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000335 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000336
337 if (copy_from_user(&edata, useraddr, sizeof(edata)))
338 return -EFAULT;
339
Michał Mirosław86794882011-02-15 16:59:17 +0000340 mask = ethtool_get_feature_mask(ethcmd);
341 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000342 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000343 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000344
345 if (edata.data)
346 dev->wanted_features |= mask;
347 else
348 dev->wanted_features &= ~mask;
349
350 __netdev_update_features(dev);
351
352 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000353}
354
Michał Mirosław02b3a552011-11-15 15:29:55 +0000355#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
356 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000357#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
358 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
359 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000360
361static u32 __ethtool_get_flags(struct net_device *dev)
362{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000363 u32 flags = 0;
364
Dragos Foianu8a731252013-07-13 14:43:00 +0100365 if (dev->features & NETIF_F_LRO)
366 flags |= ETH_FLAG_LRO;
367 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
368 flags |= ETH_FLAG_RXVLAN;
369 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
370 flags |= ETH_FLAG_TXVLAN;
371 if (dev->features & NETIF_F_NTUPLE)
372 flags |= ETH_FLAG_NTUPLE;
373 if (dev->features & NETIF_F_RXHASH)
374 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000375
376 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000377}
378
379static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000380{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000381 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000382
Michał Mirosław02b3a552011-11-15 15:29:55 +0000383 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000384 return -EINVAL;
385
Dragos Foianu8a731252013-07-13 14:43:00 +0100386 if (data & ETH_FLAG_LRO)
387 features |= NETIF_F_LRO;
388 if (data & ETH_FLAG_RXVLAN)
389 features |= NETIF_F_HW_VLAN_CTAG_RX;
390 if (data & ETH_FLAG_TXVLAN)
391 features |= NETIF_F_HW_VLAN_CTAG_TX;
392 if (data & ETH_FLAG_NTUPLE)
393 features |= NETIF_F_NTUPLE;
394 if (data & ETH_FLAG_RXHASH)
395 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000396
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000397 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000398 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000399 if (changed & ~dev->hw_features)
400 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
401
402 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000403 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000404
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700405 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000406
407 return 0;
408}
409
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700410/* Given two link masks, AND them together and save the result in dst. */
411void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
412 struct ethtool_link_ksettings *src)
413{
414 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
415 unsigned int idx = 0;
416
417 for (; idx < size; idx++) {
418 dst->link_modes.supported[idx] &=
419 src->link_modes.supported[idx];
420 dst->link_modes.advertising[idx] &=
421 src->link_modes.advertising[idx];
422 }
423}
424EXPORT_SYMBOL(ethtool_intersect_link_masks);
425
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200426void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
427 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800428{
429 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
430 dst[0] = legacy_u32;
431}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200432EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800433
434/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200435bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
436 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800437{
438 bool retval = true;
439
440 /* TODO: following test will soon always be true */
441 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
442 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
443
444 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
445 bitmap_fill(ext, 32);
446 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
447 if (bitmap_intersects(ext, src,
448 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
449 /* src mask goes beyond bit 31 */
450 retval = false;
451 }
452 }
453 *legacy_u32 = src[0];
454 return retval;
455}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200456EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800457
458/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200459 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800460 */
461static bool
462convert_legacy_settings_to_link_ksettings(
463 struct ethtool_link_ksettings *link_ksettings,
464 const struct ethtool_cmd *legacy_settings)
465{
466 bool retval = true;
467
468 memset(link_ksettings, 0, sizeof(*link_ksettings));
469
470 /* This is used to tell users that driver is still using these
471 * deprecated legacy fields, and they should not use
472 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
473 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200474 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800475 legacy_settings->maxrxpkt)
476 retval = false;
477
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200478 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800479 link_ksettings->link_modes.supported,
480 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200481 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800482 link_ksettings->link_modes.advertising,
483 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200484 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800485 link_ksettings->link_modes.lp_advertising,
486 legacy_settings->lp_advertising);
487 link_ksettings->base.speed
488 = ethtool_cmd_speed(legacy_settings);
489 link_ksettings->base.duplex
490 = legacy_settings->duplex;
491 link_ksettings->base.port
492 = legacy_settings->port;
493 link_ksettings->base.phy_address
494 = legacy_settings->phy_address;
495 link_ksettings->base.autoneg
496 = legacy_settings->autoneg;
497 link_ksettings->base.mdio_support
498 = legacy_settings->mdio_support;
499 link_ksettings->base.eth_tp_mdix
500 = legacy_settings->eth_tp_mdix;
501 link_ksettings->base.eth_tp_mdix_ctrl
502 = legacy_settings->eth_tp_mdix_ctrl;
503 return retval;
504}
505
506/* return false if ksettings link modes had higher bits
507 * set. legacy_settings always updated (best effort)
508 */
509static bool
510convert_link_ksettings_to_legacy_settings(
511 struct ethtool_cmd *legacy_settings,
512 const struct ethtool_link_ksettings *link_ksettings)
513{
514 bool retval = true;
515
516 memset(legacy_settings, 0, sizeof(*legacy_settings));
517 /* this also clears the deprecated fields in legacy structure:
518 * __u8 transceiver;
519 * __u32 maxtxpkt;
520 * __u32 maxrxpkt;
521 */
522
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200523 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800524 &legacy_settings->supported,
525 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200526 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800527 &legacy_settings->advertising,
528 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200529 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800530 &legacy_settings->lp_advertising,
531 link_ksettings->link_modes.lp_advertising);
532 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
533 legacy_settings->duplex
534 = link_ksettings->base.duplex;
535 legacy_settings->port
536 = link_ksettings->base.port;
537 legacy_settings->phy_address
538 = link_ksettings->base.phy_address;
539 legacy_settings->autoneg
540 = link_ksettings->base.autoneg;
541 legacy_settings->mdio_support
542 = link_ksettings->base.mdio_support;
543 legacy_settings->eth_tp_mdix
544 = link_ksettings->base.eth_tp_mdix;
545 legacy_settings->eth_tp_mdix_ctrl
546 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700547 legacy_settings->transceiver
548 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800549 return retval;
550}
551
552/* number of 32-bit words to store the user's link mode bitmaps */
553#define __ETHTOOL_LINK_MODE_MASK_NU32 \
554 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
555
556/* layout of the struct passed from/to userland */
557struct ethtool_link_usettings {
558 struct ethtool_link_settings base;
559 struct {
560 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
561 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
562 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
563 } link_modes;
564};
565
566/* Internal kernel helper to query a device ethtool_link_settings.
567 *
568 * Backward compatibility note: for compatibility with legacy drivers
569 * that implement only the ethtool_cmd API, this has to work with both
570 * drivers implementing get_link_ksettings API and drivers
571 * implementing get_settings API. When drivers implement get_settings
572 * and report ethtool_cmd deprecated fields
573 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
574 * because the resulting struct ethtool_link_settings does not report them.
575 */
576int __ethtool_get_link_ksettings(struct net_device *dev,
577 struct ethtool_link_ksettings *link_ksettings)
578{
579 int err;
580 struct ethtool_cmd cmd;
581
582 ASSERT_RTNL();
583
584 if (dev->ethtool_ops->get_link_ksettings) {
585 memset(link_ksettings, 0, sizeof(*link_ksettings));
586 return dev->ethtool_ops->get_link_ksettings(dev,
587 link_ksettings);
588 }
589
590 /* driver doesn't support %ethtool_link_ksettings API. revert to
591 * legacy %ethtool_cmd API, unless it's not supported either.
592 * TODO: remove when ethtool_ops::get_settings disappears internally
593 */
David Decotigny3237fc62016-02-24 10:58:11 -0800594 if (!dev->ethtool_ops->get_settings)
595 return -EOPNOTSUPP;
596
597 memset(&cmd, 0, sizeof(cmd));
598 cmd.cmd = ETHTOOL_GSET;
599 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800600 if (err < 0)
601 return err;
602
603 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
604 */
605 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
606 return err;
607}
608EXPORT_SYMBOL(__ethtool_get_link_ksettings);
609
610/* convert ethtool_link_usettings in user space to a kernel internal
611 * ethtool_link_ksettings. return 0 on success, errno on error.
612 */
613static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
614 const void __user *from)
615{
616 struct ethtool_link_usettings link_usettings;
617
618 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
619 return -EFAULT;
620
621 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800622 bitmap_from_arr32(to->link_modes.supported,
623 link_usettings.link_modes.supported,
624 __ETHTOOL_LINK_MODE_MASK_NBITS);
625 bitmap_from_arr32(to->link_modes.advertising,
626 link_usettings.link_modes.advertising,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
628 bitmap_from_arr32(to->link_modes.lp_advertising,
629 link_usettings.link_modes.lp_advertising,
630 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800631
632 return 0;
633}
634
635/* convert a kernel internal ethtool_link_ksettings to
636 * ethtool_link_usettings in user space. return 0 on success, errno on
637 * error.
638 */
639static int
640store_link_ksettings_for_user(void __user *to,
641 const struct ethtool_link_ksettings *from)
642{
643 struct ethtool_link_usettings link_usettings;
644
645 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800646 bitmap_to_arr32(link_usettings.link_modes.supported,
647 from->link_modes.supported,
648 __ETHTOOL_LINK_MODE_MASK_NBITS);
649 bitmap_to_arr32(link_usettings.link_modes.advertising,
650 from->link_modes.advertising,
651 __ETHTOOL_LINK_MODE_MASK_NBITS);
652 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
653 from->link_modes.lp_advertising,
654 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800655
656 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
657 return -EFAULT;
658
659 return 0;
660}
661
662/* Query device for its ethtool_link_settings.
663 *
664 * Backward compatibility note: this function must fail when driver
665 * does not implement ethtool::get_link_ksettings, even if legacy
666 * ethtool_ops::get_settings is implemented. This tells new versions
667 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
668 * this driver, so that they can correctly access the ethtool_cmd
669 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
670 * implements ethtool_ops::get_settings anymore.
671 */
672static int ethtool_get_link_ksettings(struct net_device *dev,
673 void __user *useraddr)
674{
675 int err = 0;
676 struct ethtool_link_ksettings link_ksettings;
677
678 ASSERT_RTNL();
679
680 if (!dev->ethtool_ops->get_link_ksettings)
681 return -EOPNOTSUPP;
682
683 /* handle bitmap nbits handshake */
684 if (copy_from_user(&link_ksettings.base, useraddr,
685 sizeof(link_ksettings.base)))
686 return -EFAULT;
687
688 if (__ETHTOOL_LINK_MODE_MASK_NU32
689 != link_ksettings.base.link_mode_masks_nwords) {
690 /* wrong link mode nbits requested */
691 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000692 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800693 /* send back number of words required as negative val */
694 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
695 "need too many bits for link modes!");
696 link_ksettings.base.link_mode_masks_nwords
697 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
698
699 /* copy the base fields back to user, not the link
700 * mode bitmaps
701 */
702 if (copy_to_user(useraddr, &link_ksettings.base,
703 sizeof(link_ksettings.base)))
704 return -EFAULT;
705
706 return 0;
707 }
708
709 /* handshake successful: user/kernel agree on
710 * link_mode_masks_nwords
711 */
712
713 memset(&link_ksettings, 0, sizeof(link_ksettings));
714 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
715 if (err < 0)
716 return err;
717
718 /* make sure we tell the right values to user */
719 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
720 link_ksettings.base.link_mode_masks_nwords
721 = __ETHTOOL_LINK_MODE_MASK_NU32;
722
723 return store_link_ksettings_for_user(useraddr, &link_ksettings);
724}
725
726/* Update device ethtool_link_settings.
727 *
728 * Backward compatibility note: this function must fail when driver
729 * does not implement ethtool::set_link_ksettings, even if legacy
730 * ethtool_ops::set_settings is implemented. This tells new versions
731 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
732 * this driver, so that they can correctly update the ethtool_cmd
733 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
734 * implements ethtool_ops::get_settings anymore.
735 */
736static int ethtool_set_link_ksettings(struct net_device *dev,
737 void __user *useraddr)
738{
739 int err;
740 struct ethtool_link_ksettings link_ksettings;
741
742 ASSERT_RTNL();
743
744 if (!dev->ethtool_ops->set_link_ksettings)
745 return -EOPNOTSUPP;
746
747 /* make sure nbits field has expected value */
748 if (copy_from_user(&link_ksettings.base, useraddr,
749 sizeof(link_ksettings.base)))
750 return -EFAULT;
751
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings.base.link_mode_masks_nwords)
754 return -EINVAL;
755
756 /* copy the whole structure, now that we know it has expected
757 * format
758 */
759 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
760 if (err)
761 return err;
762
763 /* re-check nwords field, just in case */
764 if (__ETHTOOL_LINK_MODE_MASK_NU32
765 != link_ksettings.base.link_mode_masks_nwords)
766 return -EINVAL;
767
768 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
769}
770
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800771/* Query device for its ethtool_cmd settings.
772 *
773 * Backward compatibility note: for compatibility with legacy ethtool,
774 * this has to work with both drivers implementing get_link_ksettings
775 * API and drivers implementing get_settings API. When drivers
776 * implement get_link_ksettings and report higher link mode bits, a
777 * kernel warning is logged once (with name of 1st driver/device) to
778 * recommend user to upgrade ethtool, but the command is successful
779 * (only the lower link mode bits reported back to user).
780 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000781static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
782{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000783 struct ethtool_cmd cmd;
784
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800785 ASSERT_RTNL();
786
787 if (dev->ethtool_ops->get_link_ksettings) {
788 /* First, use link_ksettings API if it is supported */
789 int err;
790 struct ethtool_link_ksettings link_ksettings;
791
792 memset(&link_ksettings, 0, sizeof(link_ksettings));
793 err = dev->ethtool_ops->get_link_ksettings(dev,
794 &link_ksettings);
795 if (err < 0)
796 return err;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800797 convert_link_ksettings_to_legacy_settings(&cmd,
798 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800799
800 /* send a sensible cmd tag back to user */
801 cmd.cmd = ETHTOOL_GSET;
802 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800803 /* driver doesn't support %ethtool_link_ksettings
804 * API. revert to legacy %ethtool_cmd API, unless it's
805 * not supported either.
806 */
David Decotigny3237fc62016-02-24 10:58:11 -0800807 int err;
808
809 if (!dev->ethtool_ops->get_settings)
810 return -EOPNOTSUPP;
811
812 memset(&cmd, 0, sizeof(cmd));
813 cmd.cmd = ETHTOOL_GSET;
814 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800815 if (err < 0)
816 return err;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
820 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823}
824
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800825/* Update device link settings with given ethtool_cmd.
826 *
827 * Backward compatibility note: for compatibility with legacy ethtool,
828 * this has to work with both drivers implementing set_link_ksettings
829 * API and drivers implementing set_settings API. When drivers
830 * implement set_link_ksettings and user's request updates deprecated
831 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
832 * warning is logged once (with name of 1st driver/device) to
833 * recommend user to upgrade ethtool, and the request is rejected.
834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
836{
837 struct ethtool_cmd cmd;
838
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800839 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
842 return -EFAULT;
843
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800844 /* first, try new %ethtool_link_ksettings API. */
845 if (dev->ethtool_ops->set_link_ksettings) {
846 struct ethtool_link_ksettings link_ksettings;
847
848 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
849 &cmd))
850 return -EINVAL;
851
852 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
853 link_ksettings.base.link_mode_masks_nwords
854 = __ETHTOOL_LINK_MODE_MASK_NU32;
855 return dev->ethtool_ops->set_link_ksettings(dev,
856 &link_ksettings);
857 }
858
859 /* legacy %ethtool_cmd API */
860
861 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
862 * disappears internally
863 */
864
865 if (!dev->ethtool_ops->set_settings)
866 return -EOPNOTSUPP;
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return dev->ethtool_ops->set_settings(dev, &cmd);
869}
870
chavey97f8aef2010-04-07 21:54:42 -0700871static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
872 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700875 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 memset(&info, 0, sizeof(info));
878 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000879 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700880 ops->get_drvinfo(dev, &info);
881 } else if (dev->dev.parent && dev->dev.parent->driver) {
882 strlcpy(info.bus_info, dev_name(dev->dev.parent),
883 sizeof(info.bus_info));
884 strlcpy(info.driver, dev->dev.parent->driver->name,
885 sizeof(info.driver));
886 } else {
887 return -EOPNOTSUPP;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Jeff Garzik723b2f52010-03-03 22:51:50 +0000890 /*
891 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000892 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000893 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000894 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700895 int rc;
896
897 rc = ops->get_sset_count(dev, ETH_SS_TEST);
898 if (rc >= 0)
899 info.testinfo_len = rc;
900 rc = ops->get_sset_count(dev, ETH_SS_STATS);
901 if (rc >= 0)
902 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700903 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
904 if (rc >= 0)
905 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700906 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000907 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000909 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 info.eedump_len = ops->get_eeprom_len(dev);
911
912 if (copy_to_user(useraddr, &info, sizeof(info)))
913 return -EFAULT;
914 return 0;
915}
916
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800917static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700918 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000919{
920 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000921 u64 sset_mask;
922 int i, idx = 0, n_bits = 0, ret, rc;
923 u32 *info_buf = NULL;
924
Jeff Garzik723b2f52010-03-03 22:51:50 +0000925 if (copy_from_user(&info, useraddr, sizeof(info)))
926 return -EFAULT;
927
928 /* store copy of mask, because we zero struct later on */
929 sset_mask = info.sset_mask;
930 if (!sset_mask)
931 return 0;
932
933 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000934 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000935
936 memset(&info, 0, sizeof(info));
937 info.cmd = ETHTOOL_GSSET_INFO;
938
939 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
940 if (!info_buf)
941 return -ENOMEM;
942
943 /*
944 * fill return buffer based on input bitmask and successful
945 * get_sset_count return
946 */
947 for (i = 0; i < 64; i++) {
948 if (!(sset_mask & (1ULL << i)))
949 continue;
950
Michał Mirosław340ae162011-02-15 16:59:16 +0000951 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000952 if (rc >= 0) {
953 info.sset_mask |= (1ULL << i);
954 info_buf[idx++] = rc;
955 }
956 }
957
958 ret = -EFAULT;
959 if (copy_to_user(useraddr, &info, sizeof(info)))
960 goto out;
961
962 useraddr += offsetof(struct ethtool_sset_info, data);
963 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
964 goto out;
965
966 ret = 0;
967
968out:
969 kfree(info_buf);
970 return ret;
971}
972
chavey97f8aef2010-04-07 21:54:42 -0700973static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000974 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700975{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000976 struct ethtool_rxnfc info;
977 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000978 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700979
Santwona Behera59089d82009-02-20 00:58:13 -0800980 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700981 return -EOPNOTSUPP;
982
Ben Hutchingsbf988432010-06-28 08:45:58 +0000983 /* struct ethtool_rxnfc was originally defined for
984 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
985 * members. User-space might still be using that
986 * definition. */
987 if (cmd == ETHTOOL_SRXFH)
988 info_size = (offsetof(struct ethtool_rxnfc, data) +
989 sizeof(info.data));
990
991 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700992 return -EFAULT;
993
Ben Hutchings55664f32012-01-03 12:04:51 +0000994 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
995 if (rc)
996 return rc;
997
998 if (cmd == ETHTOOL_SRXCLSRLINS &&
999 copy_to_user(useraddr, &info, info_size))
1000 return -EFAULT;
1001
1002 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -07001003}
1004
chavey97f8aef2010-04-07 21:54:42 -07001005static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +00001006 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -07001007{
1008 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001009 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -08001010 const struct ethtool_ops *ops = dev->ethtool_ops;
1011 int ret;
1012 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -07001013
Santwona Behera59089d82009-02-20 00:58:13 -08001014 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -07001015 return -EOPNOTSUPP;
1016
Ben Hutchingsbf988432010-06-28 08:45:58 +00001017 /* struct ethtool_rxnfc was originally defined for
1018 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1019 * members. User-space might still be using that
1020 * definition. */
1021 if (cmd == ETHTOOL_GRXFH)
1022 info_size = (offsetof(struct ethtool_rxnfc, data) +
1023 sizeof(info.data));
1024
1025 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001026 return -EFAULT;
1027
Edward Cree84a1d9c42018-03-08 15:45:03 +00001028 /* If FLOW_RSS was requested then user-space must be using the
1029 * new definition, as FLOW_RSS is newer.
1030 */
1031 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1032 info_size = sizeof(info);
1033 if (copy_from_user(&info, useraddr, info_size))
1034 return -EFAULT;
1035 }
1036
Santwona Behera59089d82009-02-20 00:58:13 -08001037 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1038 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001039 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001040 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001041 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001042 if (!rule_buf)
1043 return -ENOMEM;
1044 }
1045 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001046
Santwona Behera59089d82009-02-20 00:58:13 -08001047 ret = ops->get_rxnfc(dev, &info, rule_buf);
1048 if (ret < 0)
1049 goto err_out;
1050
1051 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001052 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001053 goto err_out;
1054
1055 if (rule_buf) {
1056 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1057 if (copy_to_user(useraddr, rule_buf,
1058 info.rule_cnt * sizeof(u32)))
1059 goto err_out;
1060 }
1061 ret = 0;
1062
1063err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001064 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001065
1066 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001067}
1068
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301069static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1070 struct ethtool_rxnfc *rx_rings,
1071 u32 size)
1072{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001073 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301074
1075 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001076 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301077
1078 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001079 for (i = 0; i < size; i++)
1080 if (indir[i] >= rx_rings->data)
1081 return -EINVAL;
1082
1083 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301084}
1085
Kim Jonesba905f52016-02-02 03:51:16 +00001086u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001087
1088void netdev_rss_key_fill(void *buffer, size_t len)
1089{
1090 BUG_ON(len > sizeof(netdev_rss_key));
1091 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1092 memcpy(buffer, netdev_rss_key, len);
1093}
1094EXPORT_SYMBOL(netdev_rss_key_fill);
1095
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001096static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1097{
1098 u32 dev_size, current_max = 0;
1099 u32 *indir;
1100 int ret;
1101
1102 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1103 !dev->ethtool_ops->get_rxfh)
1104 return -EOPNOTSUPP;
1105 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1106 if (dev_size == 0)
1107 return -EOPNOTSUPP;
1108
1109 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1110 if (!indir)
1111 return -ENOMEM;
1112
1113 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1114 if (ret)
1115 goto out;
1116
1117 while (dev_size--)
1118 current_max = max(current_max, indir[dev_size]);
1119
1120 *max = current_max;
1121
1122out:
1123 kfree(indir);
1124 return ret;
1125}
1126
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001127static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1128 void __user *useraddr)
1129{
Ben Hutchings7850f632011-12-15 13:55:01 +00001130 u32 user_size, dev_size;
1131 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001132 int ret;
1133
Ben Hutchings7850f632011-12-15 13:55:01 +00001134 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001135 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001136 return -EOPNOTSUPP;
1137 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1138 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001139 return -EOPNOTSUPP;
1140
Ben Hutchings7850f632011-12-15 13:55:01 +00001141 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001142 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001143 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001144 return -EFAULT;
1145
Ben Hutchings7850f632011-12-15 13:55:01 +00001146 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1147 &dev_size, sizeof(dev_size)))
1148 return -EFAULT;
1149
1150 /* If the user buffer size is 0, this is just a query for the
1151 * device table size. Otherwise, if it's smaller than the
1152 * device table size it's an error.
1153 */
1154 if (user_size < dev_size)
1155 return user_size == 0 ? 0 : -EINVAL;
1156
1157 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001158 if (!indir)
1159 return -ENOMEM;
1160
Eyal Perry892311f2014-12-02 18:12:10 +02001161 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001162 if (ret)
1163 goto out;
1164
Ben Hutchings7850f632011-12-15 13:55:01 +00001165 if (copy_to_user(useraddr +
1166 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1167 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001168 ret = -EFAULT;
1169
1170out:
1171 kfree(indir);
1172 return ret;
1173}
1174
1175static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1176 void __user *useraddr)
1177{
Ben Hutchings7850f632011-12-15 13:55:01 +00001178 struct ethtool_rxnfc rx_rings;
1179 u32 user_size, dev_size, i;
1180 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001181 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001182 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301183 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001184
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001185 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001186 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001187 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001188
1189 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001190 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001191 return -EOPNOTSUPP;
1192
Ben Hutchings7850f632011-12-15 13:55:01 +00001193 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001194 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001195 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001196 return -EFAULT;
1197
Ben Hutchings278bc422011-12-15 13:56:49 +00001198 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001199 return -EINVAL;
1200
1201 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001202 if (!indir)
1203 return -ENOMEM;
1204
Ben Hutchings7850f632011-12-15 13:55:01 +00001205 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001206 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001207 if (ret)
1208 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001209
1210 if (user_size == 0) {
1211 for (i = 0; i < dev_size; i++)
1212 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1213 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301214 ret = ethtool_copy_validate_indir(indir,
1215 useraddr + ringidx_offset,
1216 &rx_rings,
1217 dev_size);
1218 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001219 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001220 }
1221
Eyal Perry892311f2014-12-02 18:12:10 +02001222 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001223 if (ret)
1224 goto out;
1225
1226 /* indicate whether rxfh was set to default */
1227 if (user_size == 0)
1228 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1229 else
1230 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001231
1232out:
1233 kfree(indir);
1234 return ret;
1235}
1236
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301237static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1238 void __user *useraddr)
1239{
1240 int ret;
1241 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001242 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301243 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001244 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301245 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001246 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301247 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001248 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301249 u8 *hkey = NULL;
1250 u8 *rss_config;
1251
Eyal Perry892311f2014-12-02 18:12:10 +02001252 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301253 return -EOPNOTSUPP;
1254
1255 if (ops->get_rxfh_indir_size)
1256 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301257 if (ops->get_rxfh_key_size)
1258 dev_key_size = ops->get_rxfh_key_size(dev);
1259
Ben Hutchingsf062a382014-05-15 16:28:07 +01001260 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301261 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001262 user_indir_size = rxfh.indir_size;
1263 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301264
Ben Hutchingsf062a382014-05-15 16:28:07 +01001265 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001266 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001267 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001268 /* Most drivers don't handle rss_context, check it's 0 as well */
1269 if (rxfh.rss_context && !ops->get_rxfh_context)
1270 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001271
1272 rxfh.indir_size = dev_indir_size;
1273 rxfh.key_size = dev_key_size;
1274 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301275 return -EFAULT;
1276
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301277 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1278 (user_key_size && (user_key_size != dev_key_size)))
1279 return -EINVAL;
1280
1281 indir_bytes = user_indir_size * sizeof(indir[0]);
1282 total_size = indir_bytes + user_key_size;
1283 rss_config = kzalloc(total_size, GFP_USER);
1284 if (!rss_config)
1285 return -ENOMEM;
1286
1287 if (user_indir_size)
1288 indir = (u32 *)rss_config;
1289
1290 if (user_key_size)
1291 hkey = rss_config + indir_bytes;
1292
Edward Cree84a1d9c42018-03-08 15:45:03 +00001293 if (rxfh.rss_context)
1294 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1295 &dev_hfunc,
1296 rxfh.rss_context);
1297 else
1298 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001299 if (ret)
1300 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301301
Eyal Perry892311f2014-12-02 18:12:10 +02001302 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1303 &dev_hfunc, sizeof(rxfh.hfunc))) {
1304 ret = -EFAULT;
1305 } else if (copy_to_user(useraddr +
1306 offsetof(struct ethtool_rxfh, rss_config[0]),
1307 rss_config, total_size)) {
1308 ret = -EFAULT;
1309 }
1310out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301311 kfree(rss_config);
1312
1313 return ret;
1314}
1315
1316static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1317 void __user *useraddr)
1318{
1319 int ret;
1320 const struct ethtool_ops *ops = dev->ethtool_ops;
1321 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001322 struct ethtool_rxfh rxfh;
1323 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301324 u32 *indir = NULL, indir_bytes = 0;
1325 u8 *hkey = NULL;
1326 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301327 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001328 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301329
Eyal Perry892311f2014-12-02 18:12:10 +02001330 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301331 return -EOPNOTSUPP;
1332
1333 if (ops->get_rxfh_indir_size)
1334 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301335 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001336 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301337
Ben Hutchingsf062a382014-05-15 16:28:07 +01001338 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301339 return -EFAULT;
1340
Ben Hutchingsf062a382014-05-15 16:28:07 +01001341 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001342 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001343 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001344 /* Most drivers don't handle rss_context, check it's 0 as well */
1345 if (rxfh.rss_context && !ops->set_rxfh_context)
1346 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001347
Eyal Perry892311f2014-12-02 18:12:10 +02001348 /* If either indir, hash key or function is valid, proceed further.
1349 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301350 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001351 if ((rxfh.indir_size &&
1352 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1353 rxfh.indir_size != dev_indir_size) ||
1354 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1355 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001356 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301357 return -EINVAL;
1358
Ben Hutchingsf062a382014-05-15 16:28:07 +01001359 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301360 indir_bytes = dev_indir_size * sizeof(indir[0]);
1361
Ben Hutchingsf062a382014-05-15 16:28:07 +01001362 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301363 if (!rss_config)
1364 return -ENOMEM;
1365
1366 rx_rings.cmd = ETHTOOL_GRXRINGS;
1367 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1368 if (ret)
1369 goto out;
1370
Edward Cree84a1d9c42018-03-08 15:45:03 +00001371 /* rxfh.indir_size == 0 means reset the indir table to default (master
1372 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001373 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301374 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001375 if (rxfh.indir_size &&
1376 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301377 indir = (u32 *)rss_config;
1378 ret = ethtool_copy_validate_indir(indir,
1379 useraddr + rss_cfg_offset,
1380 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001381 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301382 if (ret)
1383 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001384 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001385 if (rxfh.rss_context == 0) {
1386 indir = (u32 *)rss_config;
1387 for (i = 0; i < dev_indir_size; i++)
1388 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1389 } else {
1390 delete = true;
1391 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301392 }
1393
Ben Hutchingsf062a382014-05-15 16:28:07 +01001394 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301395 hkey = rss_config + indir_bytes;
1396 if (copy_from_user(hkey,
1397 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001398 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301399 ret = -EFAULT;
1400 goto out;
1401 }
1402 }
1403
Edward Cree84a1d9c42018-03-08 15:45:03 +00001404 if (rxfh.rss_context)
1405 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1406 &rxfh.rss_context, delete);
1407 else
1408 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001409 if (ret)
1410 goto out;
1411
Edward Cree84a1d9c42018-03-08 15:45:03 +00001412 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1413 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1414 ret = -EFAULT;
1415
1416 if (!rxfh.rss_context) {
1417 /* indicate whether rxfh was set to default */
1418 if (rxfh.indir_size == 0)
1419 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1420 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1421 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1422 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301423
1424out:
1425 kfree(rss_config);
1426 return ret;
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1430{
1431 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001432 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 void *regbuf;
1434 int reglen, ret;
1435
1436 if (!ops->get_regs || !ops->get_regs_len)
1437 return -EOPNOTSUPP;
1438
1439 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1440 return -EFAULT;
1441
1442 reglen = ops->get_regs_len(dev);
1443 if (regs.len > reglen)
1444 regs.len = reglen;
1445
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001446 regbuf = NULL;
1447 if (reglen) {
1448 regbuf = vzalloc(reglen);
1449 if (!regbuf)
1450 return -ENOMEM;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 ops->get_regs(dev, &regs, regbuf);
1454
1455 ret = -EFAULT;
1456 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1457 goto out;
1458 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001459 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto out;
1461 ret = 0;
1462
1463 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001464 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return ret;
1466}
1467
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001468static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1469{
1470 struct ethtool_value reset;
1471 int ret;
1472
1473 if (!dev->ethtool_ops->reset)
1474 return -EOPNOTSUPP;
1475
1476 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1477 return -EFAULT;
1478
1479 ret = dev->ethtool_ops->reset(dev, &reset.data);
1480 if (ret)
1481 return ret;
1482
1483 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1484 return -EFAULT;
1485 return 0;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1489{
Roland Dreier8e557422010-02-11 12:14:23 -08001490 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (!dev->ethtool_ops->get_wol)
1493 return -EOPNOTSUPP;
1494
1495 dev->ethtool_ops->get_wol(dev, &wol);
1496
1497 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1498 return -EFAULT;
1499 return 0;
1500}
1501
1502static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1503{
1504 struct ethtool_wolinfo wol;
1505
1506 if (!dev->ethtool_ops->set_wol)
1507 return -EOPNOTSUPP;
1508
1509 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1510 return -EFAULT;
1511
1512 return dev->ethtool_ops->set_wol(dev, &wol);
1513}
1514
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001515static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1516{
1517 struct ethtool_eee edata;
1518 int rc;
1519
1520 if (!dev->ethtool_ops->get_eee)
1521 return -EOPNOTSUPP;
1522
1523 memset(&edata, 0, sizeof(struct ethtool_eee));
1524 edata.cmd = ETHTOOL_GEEE;
1525 rc = dev->ethtool_ops->get_eee(dev, &edata);
1526
1527 if (rc)
1528 return rc;
1529
1530 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1531 return -EFAULT;
1532
1533 return 0;
1534}
1535
1536static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1537{
1538 struct ethtool_eee edata;
1539
1540 if (!dev->ethtool_ops->set_eee)
1541 return -EOPNOTSUPP;
1542
1543 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1544 return -EFAULT;
1545
1546 return dev->ethtool_ops->set_eee(dev, &edata);
1547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549static int ethtool_nway_reset(struct net_device *dev)
1550{
1551 if (!dev->ethtool_ops->nway_reset)
1552 return -EOPNOTSUPP;
1553
1554 return dev->ethtool_ops->nway_reset(dev);
1555}
1556
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001557static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1558{
1559 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1560
1561 if (!dev->ethtool_ops->get_link)
1562 return -EOPNOTSUPP;
1563
1564 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1565
1566 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1567 return -EFAULT;
1568 return 0;
1569}
1570
Ben Hutchings081d0942012-04-12 00:42:12 +00001571static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1572 int (*getter)(struct net_device *,
1573 struct ethtool_eeprom *, u8 *),
1574 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001577 void __user *userbuf = useraddr + sizeof(eeprom);
1578 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001580 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1583 return -EFAULT;
1584
1585 /* Check for wrap and zero */
1586 if (eeprom.offset + eeprom.len <= eeprom.offset)
1587 return -EINVAL;
1588
1589 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001590 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -EINVAL;
1592
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001593 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!data)
1595 return -ENOMEM;
1596
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001597 bytes_remaining = eeprom.len;
1598 while (bytes_remaining > 0) {
1599 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Ben Hutchings081d0942012-04-12 00:42:12 +00001601 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001602 if (ret)
1603 break;
1604 if (copy_to_user(userbuf, data, eeprom.len)) {
1605 ret = -EFAULT;
1606 break;
1607 }
1608 userbuf += eeprom.len;
1609 eeprom.offset += eeprom.len;
1610 bytes_remaining -= eeprom.len;
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001613 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1614 eeprom.offset -= eeprom.len;
1615 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1616 ret = -EFAULT;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 kfree(data);
1619 return ret;
1620}
1621
Ben Hutchings081d0942012-04-12 00:42:12 +00001622static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1623{
1624 const struct ethtool_ops *ops = dev->ethtool_ops;
1625
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001626 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1627 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001628 return -EOPNOTSUPP;
1629
1630 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1631 ops->get_eeprom_len(dev));
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1635{
1636 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001637 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001638 void __user *userbuf = useraddr + sizeof(eeprom);
1639 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001641 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001643 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1644 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return -EOPNOTSUPP;
1646
1647 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1648 return -EFAULT;
1649
1650 /* Check for wrap and zero */
1651 if (eeprom.offset + eeprom.len <= eeprom.offset)
1652 return -EINVAL;
1653
1654 /* Check for exceeding total eeprom len */
1655 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1656 return -EINVAL;
1657
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001658 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (!data)
1660 return -ENOMEM;
1661
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001662 bytes_remaining = eeprom.len;
1663 while (bytes_remaining > 0) {
1664 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001666 if (copy_from_user(data, userbuf, eeprom.len)) {
1667 ret = -EFAULT;
1668 break;
1669 }
1670 ret = ops->set_eeprom(dev, &eeprom, data);
1671 if (ret)
1672 break;
1673 userbuf += eeprom.len;
1674 eeprom.offset += eeprom.len;
1675 bytes_remaining -= eeprom.len;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 kfree(data);
1679 return ret;
1680}
1681
chavey97f8aef2010-04-07 21:54:42 -07001682static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1683 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Roland Dreier8e557422010-02-11 12:14:23 -08001685 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 if (!dev->ethtool_ops->get_coalesce)
1688 return -EOPNOTSUPP;
1689
1690 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1691
1692 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1693 return -EFAULT;
1694 return 0;
1695}
1696
chavey97f8aef2010-04-07 21:54:42 -07001697static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1698 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 struct ethtool_coalesce coalesce;
1701
David S. Millerfa04ae52005-06-06 15:07:19 -07001702 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return -EOPNOTSUPP;
1704
1705 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1706 return -EFAULT;
1707
1708 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1709}
1710
1711static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1712{
Roland Dreier8e557422010-02-11 12:14:23 -08001713 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (!dev->ethtool_ops->get_ringparam)
1716 return -EOPNOTSUPP;
1717
1718 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1719
1720 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1721 return -EFAULT;
1722 return 0;
1723}
1724
1725static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1726{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001727 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001729 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return -EOPNOTSUPP;
1731
1732 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1733 return -EFAULT;
1734
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001735 dev->ethtool_ops->get_ringparam(dev, &max);
1736
1737 /* ensure new ring parameters are within the maximums */
1738 if (ringparam.rx_pending > max.rx_max_pending ||
1739 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1740 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1741 ringparam.tx_pending > max.tx_max_pending)
1742 return -EINVAL;
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1745}
1746
amit salecha8b5933c2011-04-07 01:58:42 +00001747static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1748 void __user *useraddr)
1749{
1750 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1751
1752 if (!dev->ethtool_ops->get_channels)
1753 return -EOPNOTSUPP;
1754
1755 dev->ethtool_ops->get_channels(dev, &channels);
1756
1757 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1758 return -EFAULT;
1759 return 0;
1760}
1761
1762static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1763 void __user *useraddr)
1764{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001765 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001766 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001767
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001768 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001769 return -EOPNOTSUPP;
1770
1771 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1772 return -EFAULT;
1773
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001774 dev->ethtool_ops->get_channels(dev, &max);
1775
1776 /* ensure new counts are within the maximums */
1777 if ((channels.rx_count > max.max_rx) ||
1778 (channels.tx_count > max.max_tx) ||
1779 (channels.combined_count > max.max_combined) ||
1780 (channels.other_count > max.max_other))
1781 return -EINVAL;
1782
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001783 /* ensure the new Rx count fits within the configured Rx flow
1784 * indirection table settings */
1785 if (netif_is_rxfh_configured(dev) &&
1786 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1787 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1788 return -EINVAL;
1789
amit salecha8b5933c2011-04-07 01:58:42 +00001790 return dev->ethtool_ops->set_channels(dev, &channels);
1791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1794{
1795 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1796
1797 if (!dev->ethtool_ops->get_pauseparam)
1798 return -EOPNOTSUPP;
1799
1800 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1801
1802 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1803 return -EFAULT;
1804 return 0;
1805}
1806
1807static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1808{
1809 struct ethtool_pauseparam pauseparam;
1810
Jeff Garzike1b90c42006-07-17 12:54:40 -04001811 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return -EOPNOTSUPP;
1813
1814 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1815 return -EFAULT;
1816
1817 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1821{
1822 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001823 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001825 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001827 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001828 return -EOPNOTSUPP;
1829
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001830 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001831 if (test_len < 0)
1832 return test_len;
1833 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 if (copy_from_user(&test, useraddr, sizeof(test)))
1836 return -EFAULT;
1837
Jeff Garzikff03d492007-08-15 16:01:08 -07001838 test.len = test_len;
1839 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (!data)
1841 return -ENOMEM;
1842
1843 ops->self_test(dev, &test, data);
1844
1845 ret = -EFAULT;
1846 if (copy_to_user(useraddr, &test, sizeof(test)))
1847 goto out;
1848 useraddr += sizeof(test);
1849 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1850 goto out;
1851 ret = 0;
1852
1853 out:
1854 kfree(data);
1855 return ret;
1856}
1857
1858static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1859{
1860 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 u8 *data;
1862 int ret;
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1865 return -EFAULT;
1866
Michał Mirosław340ae162011-02-15 16:59:16 +00001867 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001868 if (ret < 0)
1869 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001870 if (ret > S32_MAX / ETH_GSTRING_LEN)
1871 return -ENOMEM;
1872 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001873
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001874 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001875 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1876 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return -ENOMEM;
1878
Michał Mirosław340ae162011-02-15 16:59:16 +00001879 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 ret = -EFAULT;
1882 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1883 goto out;
1884 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001885 if (gstrings.len &&
1886 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto out;
1888 ret = 0;
1889
Michał Mirosław340ae162011-02-15 16:59:16 +00001890out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001891 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return ret;
1893}
1894
1895static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1896{
1897 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001898 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001899 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001900 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001902 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return -EOPNOTSUPP;
1904
Ben Hutchings68f512f2011-04-02 00:35:15 +01001905 if (busy)
1906 return -EBUSY;
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (copy_from_user(&id, useraddr, sizeof(id)))
1909 return -EFAULT;
1910
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001911 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001912 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001913 return rc;
1914
1915 /* Drop the RTNL lock while waiting, but prevent reentry or
1916 * removal of the device.
1917 */
1918 busy = true;
1919 dev_hold(dev);
1920 rtnl_unlock();
1921
1922 if (rc == 0) {
1923 /* Driver will handle this itself */
1924 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001925 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001926 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001927 /* Driver expects to be called at twice the frequency in rc */
1928 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001929
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001930 /* Count down seconds */
1931 do {
1932 /* Count down iterations per second */
1933 i = n;
1934 do {
1935 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001936 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001937 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1938 rtnl_unlock();
1939 if (rc)
1940 break;
1941 schedule_timeout_interruptible(interval);
1942 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001943 } while (!signal_pending(current) &&
1944 (id.data == 0 || --id.data != 0));
1945 }
1946
1947 rtnl_lock();
1948 dev_put(dev);
1949 busy = false;
1950
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001951 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001952 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1956{
1957 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001958 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001960 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001962 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001963 return -EOPNOTSUPP;
1964
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001965 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001966 if (n_stats < 0)
1967 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001968 if (n_stats > S32_MAX / sizeof(u64))
1969 return -ENOMEM;
1970 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1972 return -EFAULT;
1973
Jeff Garzikff03d492007-08-15 16:01:08 -07001974 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001975 data = vzalloc(n_stats * sizeof(u64));
1976 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -ENOMEM;
1978
1979 ops->get_ethtool_stats(dev, &stats, data);
1980
1981 ret = -EFAULT;
1982 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1983 goto out;
1984 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001985 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 goto out;
1987 ret = 0;
1988
1989 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001990 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return ret;
1992}
1993
Andrew Lunnf3a40942015-12-30 16:28:25 +01001994static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1995{
1996 struct ethtool_stats stats;
1997 struct phy_device *phydev = dev->phydev;
1998 u64 *data;
1999 int ret, n_stats;
2000
2001 if (!phydev)
2002 return -EOPNOTSUPP;
2003
2004 n_stats = phy_get_sset_count(phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002005 if (n_stats < 0)
2006 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002007 if (n_stats > S32_MAX / sizeof(u64))
2008 return -ENOMEM;
2009 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002010
2011 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2012 return -EFAULT;
2013
2014 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002015 data = vzalloc(n_stats * sizeof(u64));
2016 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01002017 return -ENOMEM;
2018
2019 mutex_lock(&phydev->lock);
2020 phydev->drv->get_stats(phydev, &stats, data);
2021 mutex_unlock(&phydev->lock);
2022
2023 ret = -EFAULT;
2024 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2025 goto out;
2026 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002027 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002028 goto out;
2029 ret = 0;
2030
2031 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002032 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002033 return ret;
2034}
2035
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002036static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002037{
2038 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002039
Matthew Wilcox313674a2007-07-31 14:00:29 -07002040 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002041 return -EFAULT;
2042
Matthew Wilcox313674a2007-07-31 14:00:29 -07002043 if (epaddr.size < dev->addr_len)
2044 return -ETOOSMALL;
2045 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002046
Jon Wetzela6f9a702005-08-20 17:15:54 -07002047 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002048 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002049 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002050 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2051 return -EFAULT;
2052 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002053}
2054
Jeff Garzik13c99b22007-08-15 16:01:56 -07002055static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2056 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002057{
Roland Dreier8e557422010-02-11 12:14:23 -08002058 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002059
Jeff Garzik13c99b22007-08-15 16:01:56 -07002060 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002061 return -EOPNOTSUPP;
2062
Jeff Garzik13c99b22007-08-15 16:01:56 -07002063 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002064
2065 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2066 return -EFAULT;
2067 return 0;
2068}
2069
Jeff Garzik13c99b22007-08-15 16:01:56 -07002070static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2071 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002072{
2073 struct ethtool_value edata;
2074
Jeff Garzik13c99b22007-08-15 16:01:56 -07002075 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002076 return -EOPNOTSUPP;
2077
2078 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2079 return -EFAULT;
2080
Jeff Garzik13c99b22007-08-15 16:01:56 -07002081 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002082 return 0;
2083}
2084
Jeff Garzik13c99b22007-08-15 16:01:56 -07002085static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2086 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002087{
2088 struct ethtool_value edata;
2089
Jeff Garzik13c99b22007-08-15 16:01:56 -07002090 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002091 return -EOPNOTSUPP;
2092
2093 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2094 return -EFAULT;
2095
Jeff Garzik13c99b22007-08-15 16:01:56 -07002096 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002097}
2098
chavey97f8aef2010-04-07 21:54:42 -07002099static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2100 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002101{
2102 struct ethtool_flash efl;
2103
2104 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2105 return -EFAULT;
2106
2107 if (!dev->ethtool_ops->flash_device)
2108 return -EOPNOTSUPP;
2109
Ben Hutchings786f5282012-02-01 09:32:25 +00002110 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2111
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002112 return dev->ethtool_ops->flash_device(dev, &efl);
2113}
2114
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002115static int ethtool_set_dump(struct net_device *dev,
2116 void __user *useraddr)
2117{
2118 struct ethtool_dump dump;
2119
2120 if (!dev->ethtool_ops->set_dump)
2121 return -EOPNOTSUPP;
2122
2123 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2124 return -EFAULT;
2125
2126 return dev->ethtool_ops->set_dump(dev, &dump);
2127}
2128
2129static int ethtool_get_dump_flag(struct net_device *dev,
2130 void __user *useraddr)
2131{
2132 int ret;
2133 struct ethtool_dump dump;
2134 const struct ethtool_ops *ops = dev->ethtool_ops;
2135
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002136 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002137 return -EOPNOTSUPP;
2138
2139 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2140 return -EFAULT;
2141
2142 ret = ops->get_dump_flag(dev, &dump);
2143 if (ret)
2144 return ret;
2145
2146 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2147 return -EFAULT;
2148 return 0;
2149}
2150
2151static int ethtool_get_dump_data(struct net_device *dev,
2152 void __user *useraddr)
2153{
2154 int ret;
2155 __u32 len;
2156 struct ethtool_dump dump, tmp;
2157 const struct ethtool_ops *ops = dev->ethtool_ops;
2158 void *data = NULL;
2159
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002160 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002161 return -EOPNOTSUPP;
2162
2163 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2164 return -EFAULT;
2165
2166 memset(&tmp, 0, sizeof(tmp));
2167 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2168 ret = ops->get_dump_flag(dev, &tmp);
2169 if (ret)
2170 return ret;
2171
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002172 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002173 if (!len)
2174 return -EFAULT;
2175
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002176 /* Don't ever let the driver think there's more space available
2177 * than it requested with .get_dump_flag().
2178 */
2179 dump.len = len;
2180
2181 /* Always allocate enough space to hold the whole thing so that the
2182 * driver does not need to check the length and bother with partial
2183 * dumping.
2184 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002185 data = vzalloc(tmp.len);
2186 if (!data)
2187 return -ENOMEM;
2188 ret = ops->get_dump_data(dev, &dump, data);
2189 if (ret)
2190 goto out;
2191
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002192 /* There are two sane possibilities:
2193 * 1. The driver's .get_dump_data() does not touch dump.len.
2194 * 2. Or it may set dump.len to how much it really writes, which
2195 * should be tmp.len (or len if it can do a partial dump).
2196 * In any case respond to userspace with the actual length of data
2197 * it's receiving.
2198 */
2199 WARN_ON(dump.len != len && dump.len != tmp.len);
2200 dump.len = len;
2201
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002202 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2203 ret = -EFAULT;
2204 goto out;
2205 }
2206 useraddr += offsetof(struct ethtool_dump, data);
2207 if (copy_to_user(useraddr, data, len))
2208 ret = -EFAULT;
2209out:
2210 vfree(data);
2211 return ret;
2212}
2213
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002214static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2215{
2216 int err = 0;
2217 struct ethtool_ts_info info;
2218 const struct ethtool_ops *ops = dev->ethtool_ops;
2219 struct phy_device *phydev = dev->phydev;
2220
2221 memset(&info, 0, sizeof(info));
2222 info.cmd = ETHTOOL_GET_TS_INFO;
2223
2224 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002225 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002226 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002227 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002228 } else {
2229 info.so_timestamping =
2230 SOF_TIMESTAMPING_RX_SOFTWARE |
2231 SOF_TIMESTAMPING_SOFTWARE;
2232 info.phc_index = -1;
2233 }
2234
2235 if (err)
2236 return err;
2237
2238 if (copy_to_user(useraddr, &info, sizeof(info)))
2239 err = -EFAULT;
2240
2241 return err;
2242}
2243
Ed Swierk2f438362015-01-02 17:27:56 -08002244static int __ethtool_get_module_info(struct net_device *dev,
2245 struct ethtool_modinfo *modinfo)
2246{
2247 const struct ethtool_ops *ops = dev->ethtool_ops;
2248 struct phy_device *phydev = dev->phydev;
2249
Russell Kinge679c9c2018-03-28 15:44:16 -07002250 if (dev->sfp_bus)
2251 return sfp_get_module_info(dev->sfp_bus, modinfo);
2252
Ed Swierk2f438362015-01-02 17:27:56 -08002253 if (phydev && phydev->drv && phydev->drv->module_info)
2254 return phydev->drv->module_info(phydev, modinfo);
2255
2256 if (ops->get_module_info)
2257 return ops->get_module_info(dev, modinfo);
2258
2259 return -EOPNOTSUPP;
2260}
2261
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002262static int ethtool_get_module_info(struct net_device *dev,
2263 void __user *useraddr)
2264{
2265 int ret;
2266 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002267
2268 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2269 return -EFAULT;
2270
Ed Swierk2f438362015-01-02 17:27:56 -08002271 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002272 if (ret)
2273 return ret;
2274
2275 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2276 return -EFAULT;
2277
2278 return 0;
2279}
2280
Ed Swierk2f438362015-01-02 17:27:56 -08002281static int __ethtool_get_module_eeprom(struct net_device *dev,
2282 struct ethtool_eeprom *ee, u8 *data)
2283{
2284 const struct ethtool_ops *ops = dev->ethtool_ops;
2285 struct phy_device *phydev = dev->phydev;
2286
Russell Kinge679c9c2018-03-28 15:44:16 -07002287 if (dev->sfp_bus)
2288 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2289
Ed Swierk2f438362015-01-02 17:27:56 -08002290 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2291 return phydev->drv->module_eeprom(phydev, ee, data);
2292
2293 if (ops->get_module_eeprom)
2294 return ops->get_module_eeprom(dev, ee, data);
2295
2296 return -EOPNOTSUPP;
2297}
2298
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002299static int ethtool_get_module_eeprom(struct net_device *dev,
2300 void __user *useraddr)
2301{
2302 int ret;
2303 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002304
Ed Swierk2f438362015-01-02 17:27:56 -08002305 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002306 if (ret)
2307 return ret;
2308
Ed Swierk2f438362015-01-02 17:27:56 -08002309 return ethtool_get_any_eeprom(dev, useraddr,
2310 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002311 modinfo.eeprom_len);
2312}
2313
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302314static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2315{
2316 switch (tuna->id) {
2317 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002318 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302319 if (tuna->len != sizeof(u32) ||
2320 tuna->type_id != ETHTOOL_TUNABLE_U32)
2321 return -EINVAL;
2322 break;
Inbar Karmye1577c12017-11-20 16:14:30 +02002323 case ETHTOOL_PFC_PREVENTION_TOUT:
2324 if (tuna->len != sizeof(u16) ||
2325 tuna->type_id != ETHTOOL_TUNABLE_U16)
2326 return -EINVAL;
2327 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302328 default:
2329 return -EINVAL;
2330 }
2331
2332 return 0;
2333}
2334
2335static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2336{
2337 int ret;
2338 struct ethtool_tunable tuna;
2339 const struct ethtool_ops *ops = dev->ethtool_ops;
2340 void *data;
2341
2342 if (!ops->get_tunable)
2343 return -EOPNOTSUPP;
2344 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2345 return -EFAULT;
2346 ret = ethtool_tunable_valid(&tuna);
2347 if (ret)
2348 return ret;
2349 data = kmalloc(tuna.len, GFP_USER);
2350 if (!data)
2351 return -ENOMEM;
2352 ret = ops->get_tunable(dev, &tuna, data);
2353 if (ret)
2354 goto out;
2355 useraddr += sizeof(tuna);
2356 ret = -EFAULT;
2357 if (copy_to_user(useraddr, data, tuna.len))
2358 goto out;
2359 ret = 0;
2360
2361out:
2362 kfree(data);
2363 return ret;
2364}
2365
2366static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2367{
2368 int ret;
2369 struct ethtool_tunable tuna;
2370 const struct ethtool_ops *ops = dev->ethtool_ops;
2371 void *data;
2372
2373 if (!ops->set_tunable)
2374 return -EOPNOTSUPP;
2375 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2376 return -EFAULT;
2377 ret = ethtool_tunable_valid(&tuna);
2378 if (ret)
2379 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302380 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002381 data = memdup_user(useraddr, tuna.len);
2382 if (IS_ERR(data))
2383 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302384 ret = ops->set_tunable(dev, &tuna, data);
2385
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302386 kfree(data);
2387 return ret;
2388}
2389
Kan Liang421797b2016-02-19 09:24:02 -05002390static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2391 void __user *useraddr,
2392 struct ethtool_per_queue_op *per_queue_opt)
2393{
2394 u32 bit;
2395 int ret;
2396 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2397
2398 if (!dev->ethtool_ops->get_per_queue_coalesce)
2399 return -EOPNOTSUPP;
2400
2401 useraddr += sizeof(*per_queue_opt);
2402
Yury Norov3aa56882018-02-06 15:38:06 -08002403 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2404 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002405
2406 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2407 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2408
2409 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2410 if (ret != 0)
2411 return ret;
2412 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2413 return -EFAULT;
2414 useraddr += sizeof(coalesce);
2415 }
2416
2417 return 0;
2418}
2419
Kan Liangf38d1382016-02-19 09:24:03 -05002420static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2421 void __user *useraddr,
2422 struct ethtool_per_queue_op *per_queue_opt)
2423{
2424 u32 bit;
2425 int i, ret = 0;
2426 int n_queue;
2427 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2428 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2429
2430 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2431 (!dev->ethtool_ops->get_per_queue_coalesce))
2432 return -EOPNOTSUPP;
2433
2434 useraddr += sizeof(*per_queue_opt);
2435
Yury Norov3aa56882018-02-06 15:38:06 -08002436 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002437 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2438 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2439 if (!backup)
2440 return -ENOMEM;
2441
2442 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2443 struct ethtool_coalesce coalesce;
2444
2445 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2446 if (ret != 0)
2447 goto roll_back;
2448
2449 tmp++;
2450
2451 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2452 ret = -EFAULT;
2453 goto roll_back;
2454 }
2455
2456 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2457 if (ret != 0)
2458 goto roll_back;
2459
2460 useraddr += sizeof(coalesce);
2461 }
2462
2463roll_back:
2464 if (ret != 0) {
2465 tmp = backup;
2466 for_each_set_bit(i, queue_mask, bit) {
2467 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2468 tmp++;
2469 }
2470 }
2471 kfree(backup);
2472
2473 return ret;
2474}
2475
Kan Liangac2c7ad2016-02-19 09:24:01 -05002476static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2477{
2478 struct ethtool_per_queue_op per_queue_opt;
2479
2480 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2481 return -EFAULT;
2482
2483 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002484 case ETHTOOL_GCOALESCE:
2485 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002486 case ETHTOOL_SCOALESCE:
2487 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002488 default:
2489 return -EOPNOTSUPP;
2490 };
2491}
2492
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002493static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2494{
2495 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002496 case ETHTOOL_PHY_DOWNSHIFT:
2497 if (tuna->len != sizeof(u8) ||
2498 tuna->type_id != ETHTOOL_TUNABLE_U8)
2499 return -EINVAL;
2500 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002501 default:
2502 return -EINVAL;
2503 }
2504
2505 return 0;
2506}
2507
2508static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2509{
2510 int ret;
2511 struct ethtool_tunable tuna;
2512 struct phy_device *phydev = dev->phydev;
2513 void *data;
2514
2515 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2516 return -EOPNOTSUPP;
2517
2518 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2519 return -EFAULT;
2520 ret = ethtool_phy_tunable_valid(&tuna);
2521 if (ret)
2522 return ret;
2523 data = kmalloc(tuna.len, GFP_USER);
2524 if (!data)
2525 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002526 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002527 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002528 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002529 if (ret)
2530 goto out;
2531 useraddr += sizeof(tuna);
2532 ret = -EFAULT;
2533 if (copy_to_user(useraddr, data, tuna.len))
2534 goto out;
2535 ret = 0;
2536
2537out:
2538 kfree(data);
2539 return ret;
2540}
2541
2542static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2543{
2544 int ret;
2545 struct ethtool_tunable tuna;
2546 struct phy_device *phydev = dev->phydev;
2547 void *data;
2548
2549 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2550 return -EOPNOTSUPP;
2551 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2552 return -EFAULT;
2553 ret = ethtool_phy_tunable_valid(&tuna);
2554 if (ret)
2555 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002556 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002557 data = memdup_user(useraddr, tuna.len);
2558 if (IS_ERR(data))
2559 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002560 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002561 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002562 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002563
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002564 kfree(data);
2565 return ret;
2566}
2567
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002568static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2569{
2570 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002571 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002572
2573 if (!dev->ethtool_ops->get_fecparam)
2574 return -EOPNOTSUPP;
2575
Edward Creea6d50512018-02-28 19:15:58 +00002576 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2577 if (rc)
2578 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002579
2580 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2581 return -EFAULT;
2582 return 0;
2583}
2584
2585static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2586{
2587 struct ethtool_fecparam fecparam;
2588
2589 if (!dev->ethtool_ops->set_fecparam)
2590 return -EOPNOTSUPP;
2591
2592 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2593 return -EFAULT;
2594
2595 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2596}
2597
Yan Burman600fed52013-06-03 02:03:34 +00002598/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Eric W. Biederman881d9662007-09-17 11:56:21 -07002600int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002602 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002604 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002606 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (!dev || !netif_device_present(dev))
2609 return -ENODEV;
2610
chavey97f8aef2010-04-07 21:54:42 -07002611 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return -EFAULT;
2613
Kan Liangac2c7ad2016-02-19 09:24:01 -05002614 if (ethcmd == ETHTOOL_PERQUEUE) {
2615 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2616 return -EFAULT;
2617 } else {
2618 sub_cmd = ethcmd;
2619 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002620 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002621 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002622 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002623 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002624 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002625 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002626 case ETHTOOL_GCOALESCE:
2627 case ETHTOOL_GRINGPARAM:
2628 case ETHTOOL_GPAUSEPARAM:
2629 case ETHTOOL_GRXCSUM:
2630 case ETHTOOL_GTXCSUM:
2631 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002632 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002633 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002634 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002635 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002636 case ETHTOOL_GTSO:
2637 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002638 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002639 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002640 case ETHTOOL_GFLAGS:
2641 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002642 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002643 case ETHTOOL_GRXRINGS:
2644 case ETHTOOL_GRXCLSRLCNT:
2645 case ETHTOOL_GRXCLSRULE:
2646 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002647 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302648 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002649 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002650 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002651 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002652 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302653 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002654 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002655 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002656 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002657 break;
2658 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002659 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002660 return -EPERM;
2661 }
2662
chavey97f8aef2010-04-07 21:54:42 -07002663 if (dev->ethtool_ops->begin) {
2664 rc = dev->ethtool_ops->begin(dev);
2665 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002667 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002668 old_features = dev->features;
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 switch (ethcmd) {
2671 case ETHTOOL_GSET:
2672 rc = ethtool_get_settings(dev, useraddr);
2673 break;
2674 case ETHTOOL_SSET:
2675 rc = ethtool_set_settings(dev, useraddr);
2676 break;
2677 case ETHTOOL_GDRVINFO:
2678 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 break;
2680 case ETHTOOL_GREGS:
2681 rc = ethtool_get_regs(dev, useraddr);
2682 break;
2683 case ETHTOOL_GWOL:
2684 rc = ethtool_get_wol(dev, useraddr);
2685 break;
2686 case ETHTOOL_SWOL:
2687 rc = ethtool_set_wol(dev, useraddr);
2688 break;
2689 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002690 rc = ethtool_get_value(dev, useraddr, ethcmd,
2691 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 break;
2693 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002694 rc = ethtool_set_value_void(dev, useraddr,
2695 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002697 case ETHTOOL_GEEE:
2698 rc = ethtool_get_eee(dev, useraddr);
2699 break;
2700 case ETHTOOL_SEEE:
2701 rc = ethtool_set_eee(dev, useraddr);
2702 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 case ETHTOOL_NWAY_RST:
2704 rc = ethtool_nway_reset(dev);
2705 break;
2706 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002707 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 break;
2709 case ETHTOOL_GEEPROM:
2710 rc = ethtool_get_eeprom(dev, useraddr);
2711 break;
2712 case ETHTOOL_SEEPROM:
2713 rc = ethtool_set_eeprom(dev, useraddr);
2714 break;
2715 case ETHTOOL_GCOALESCE:
2716 rc = ethtool_get_coalesce(dev, useraddr);
2717 break;
2718 case ETHTOOL_SCOALESCE:
2719 rc = ethtool_set_coalesce(dev, useraddr);
2720 break;
2721 case ETHTOOL_GRINGPARAM:
2722 rc = ethtool_get_ringparam(dev, useraddr);
2723 break;
2724 case ETHTOOL_SRINGPARAM:
2725 rc = ethtool_set_ringparam(dev, useraddr);
2726 break;
2727 case ETHTOOL_GPAUSEPARAM:
2728 rc = ethtool_get_pauseparam(dev, useraddr);
2729 break;
2730 case ETHTOOL_SPAUSEPARAM:
2731 rc = ethtool_set_pauseparam(dev, useraddr);
2732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 case ETHTOOL_TEST:
2734 rc = ethtool_self_test(dev, useraddr);
2735 break;
2736 case ETHTOOL_GSTRINGS:
2737 rc = ethtool_get_strings(dev, useraddr);
2738 break;
2739 case ETHTOOL_PHYS_ID:
2740 rc = ethtool_phys_id(dev, useraddr);
2741 break;
2742 case ETHTOOL_GSTATS:
2743 rc = ethtool_get_stats(dev, useraddr);
2744 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002745 case ETHTOOL_GPERMADDR:
2746 rc = ethtool_get_perm_addr(dev, useraddr);
2747 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002748 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002749 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002750 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002751 break;
2752 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002753 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002754 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002755 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002756 rc = ethtool_get_value(dev, useraddr, ethcmd,
2757 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002758 break;
2759 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002760 rc = ethtool_set_value(dev, useraddr,
2761 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002762 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002763 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002764 case ETHTOOL_GRXRINGS:
2765 case ETHTOOL_GRXCLSRLCNT:
2766 case ETHTOOL_GRXCLSRULE:
2767 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002768 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002769 break;
2770 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002771 case ETHTOOL_SRXCLSRLDEL:
2772 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002773 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002774 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002775 case ETHTOOL_FLASHDEV:
2776 rc = ethtool_flash_device(dev, useraddr);
2777 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002778 case ETHTOOL_RESET:
2779 rc = ethtool_reset(dev, useraddr);
2780 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002781 case ETHTOOL_GSSET_INFO:
2782 rc = ethtool_get_sset_info(dev, useraddr);
2783 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002784 case ETHTOOL_GRXFHINDIR:
2785 rc = ethtool_get_rxfh_indir(dev, useraddr);
2786 break;
2787 case ETHTOOL_SRXFHINDIR:
2788 rc = ethtool_set_rxfh_indir(dev, useraddr);
2789 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302790 case ETHTOOL_GRSSH:
2791 rc = ethtool_get_rxfh(dev, useraddr);
2792 break;
2793 case ETHTOOL_SRSSH:
2794 rc = ethtool_set_rxfh(dev, useraddr);
2795 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002796 case ETHTOOL_GFEATURES:
2797 rc = ethtool_get_features(dev, useraddr);
2798 break;
2799 case ETHTOOL_SFEATURES:
2800 rc = ethtool_set_features(dev, useraddr);
2801 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002802 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002803 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002804 case ETHTOOL_GSG:
2805 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002806 case ETHTOOL_GGSO:
2807 case ETHTOOL_GGRO:
2808 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2809 break;
2810 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002811 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002812 case ETHTOOL_SSG:
2813 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002814 case ETHTOOL_SGSO:
2815 case ETHTOOL_SGRO:
2816 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2817 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002818 case ETHTOOL_GCHANNELS:
2819 rc = ethtool_get_channels(dev, useraddr);
2820 break;
2821 case ETHTOOL_SCHANNELS:
2822 rc = ethtool_set_channels(dev, useraddr);
2823 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002824 case ETHTOOL_SET_DUMP:
2825 rc = ethtool_set_dump(dev, useraddr);
2826 break;
2827 case ETHTOOL_GET_DUMP_FLAG:
2828 rc = ethtool_get_dump_flag(dev, useraddr);
2829 break;
2830 case ETHTOOL_GET_DUMP_DATA:
2831 rc = ethtool_get_dump_data(dev, useraddr);
2832 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002833 case ETHTOOL_GET_TS_INFO:
2834 rc = ethtool_get_ts_info(dev, useraddr);
2835 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002836 case ETHTOOL_GMODULEINFO:
2837 rc = ethtool_get_module_info(dev, useraddr);
2838 break;
2839 case ETHTOOL_GMODULEEEPROM:
2840 rc = ethtool_get_module_eeprom(dev, useraddr);
2841 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302842 case ETHTOOL_GTUNABLE:
2843 rc = ethtool_get_tunable(dev, useraddr);
2844 break;
2845 case ETHTOOL_STUNABLE:
2846 rc = ethtool_set_tunable(dev, useraddr);
2847 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002848 case ETHTOOL_GPHYSTATS:
2849 rc = ethtool_get_phy_stats(dev, useraddr);
2850 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002851 case ETHTOOL_PERQUEUE:
2852 rc = ethtool_set_per_queue(dev, useraddr);
2853 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002854 case ETHTOOL_GLINKSETTINGS:
2855 rc = ethtool_get_link_ksettings(dev, useraddr);
2856 break;
2857 case ETHTOOL_SLINKSETTINGS:
2858 rc = ethtool_set_link_ksettings(dev, useraddr);
2859 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002860 case ETHTOOL_PHY_GTUNABLE:
2861 rc = get_phy_tunable(dev, useraddr);
2862 break;
2863 case ETHTOOL_PHY_STUNABLE:
2864 rc = set_phy_tunable(dev, useraddr);
2865 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002866 case ETHTOOL_GFECPARAM:
2867 rc = ethtool_get_fecparam(dev, useraddr);
2868 break;
2869 case ETHTOOL_SFECPARAM:
2870 rc = ethtool_set_fecparam(dev, useraddr);
2871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002873 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002875
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002876 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002878
2879 if (old_features != dev->features)
2880 netdev_features_change(dev);
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}