blob: 65f907aea7770e2e9d7ea1c82ffd6f616e424e63 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010026#include <linux/rtnetlink.h>
27#include <linux/sched.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080028#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * Some useful ethtool_ops methods that're device independent.
32 * If we find that all drivers want to do the same thing here,
33 * we can turn these into dev_() function calls.
34 */
35
36u32 ethtool_op_get_link(struct net_device *dev)
37{
38 return netif_carrier_ok(dev) ? 1 : 0;
39}
chavey97f8aef2010-04-07 21:54:42 -070040EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Richard Cochran02eacbd2012-04-03 22:59:22 +000042int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
43{
44 info->so_timestamping =
45 SOF_TIMESTAMPING_TX_SOFTWARE |
46 SOF_TIMESTAMPING_RX_SOFTWARE |
47 SOF_TIMESTAMPING_SOFTWARE;
48 info->phc_index = -1;
49 return 0;
50}
51EXPORT_SYMBOL(ethtool_op_get_ts_info);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Handlers for each ethtool command */
54
Michał Mirosław475414f2011-11-15 15:29:55 +000055#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000056
Michał Mirosław9d921542011-11-15 15:29:55 +000057static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
58 [NETIF_F_SG_BIT] = "tx-scatter-gather",
59 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000060 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
61 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
62 [NETIF_F_HIGHDMA_BIT] = "highdma",
63 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090064 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000065
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090066 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040068 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
69 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
70 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000071 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
72 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
73 [NETIF_F_LLTX_BIT] = "tx-lockless",
74 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
75 [NETIF_F_GRO_BIT] = "rx-gro",
76 [NETIF_F_LRO_BIT] = "rx-lro",
77
78 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
79 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
80 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
81 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
82 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
83 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000084 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Eric Dumazetcb32f512013-10-19 11:42:57 -070085 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
Eric Dumazet61c1db72013-10-20 20:47:30 -070086 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000087 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000088
89 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080090 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000091 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
92 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
93 [NETIF_F_RXHASH_BIT] = "rx-hashing",
94 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
95 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
96 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000097 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000098 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -080099 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
Jiri Pirkod0290212014-04-02 23:09:31 +0200100 [NETIF_F_BUSY_POLL_BIT] = "busy-poll",
Michał Mirosław9d921542011-11-15 15:29:55 +0000101};
102
Eyal Perry892311f2014-12-02 18:12:10 +0200103static const char
104rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
105 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
106 [ETH_RSS_HASH_XOR_BIT] = "xor",
107};
108
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300109static const char
110tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
111 [ETHTOOL_ID_UNSPEC] = "Unspec",
112 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
113 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
114};
115
Michał Mirosław5455c692011-02-15 16:59:17 +0000116static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
117{
118 struct ethtool_gfeatures cmd = {
119 .cmd = ETHTOOL_GFEATURES,
120 .size = ETHTOOL_DEV_FEATURE_WORDS,
121 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000122 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000123 u32 __user *sizeaddr;
124 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000125 int i;
126
127 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000128 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000129
130 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000131 features[i].available = (u32)(dev->hw_features >> (32 * i));
132 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
133 features[i].active = (u32)(dev->features >> (32 * i));
134 features[i].never_changed =
135 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000136 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000137
138 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
139 if (get_user(copy_size, sizeaddr))
140 return -EFAULT;
141
142 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
143 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
144
145 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
146 return -EFAULT;
147 useraddr += sizeof(cmd);
148 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
149 return -EFAULT;
150
151 return 0;
152}
153
154static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
155{
156 struct ethtool_sfeatures cmd;
157 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000158 netdev_features_t wanted = 0, valid = 0;
159 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000160
161 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
162 return -EFAULT;
163 useraddr += sizeof(cmd);
164
165 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
166 return -EINVAL;
167
168 if (copy_from_user(features, useraddr, sizeof(features)))
169 return -EFAULT;
170
Michał Mirosław475414f2011-11-15 15:29:55 +0000171 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000172 valid |= (netdev_features_t)features[i].valid << (32 * i);
173 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000174 }
175
176 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000177 return -EINVAL;
178
Michał Mirosław475414f2011-11-15 15:29:55 +0000179 if (valid & ~dev->hw_features) {
180 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000181 ret |= ETHTOOL_F_UNSUPPORTED;
182 }
183
Michał Mirosław475414f2011-11-15 15:29:55 +0000184 dev->wanted_features &= ~valid;
185 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700186 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000187
Michał Mirosław475414f2011-11-15 15:29:55 +0000188 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000189 ret |= ETHTOOL_F_WISH;
190
191 return ret;
192}
193
Andrew Lunnf3a40942015-12-30 16:28:25 +0100194static int phy_get_sset_count(struct phy_device *phydev)
195{
196 int ret;
197
198 if (phydev->drv->get_sset_count &&
199 phydev->drv->get_strings &&
200 phydev->drv->get_stats) {
201 mutex_lock(&phydev->lock);
202 ret = phydev->drv->get_sset_count(phydev);
203 mutex_unlock(&phydev->lock);
204
205 return ret;
206 }
207
208 return -EOPNOTSUPP;
209}
210
Michał Mirosław340ae162011-02-15 16:59:16 +0000211static int __ethtool_get_sset_count(struct net_device *dev, int sset)
212{
213 const struct ethtool_ops *ops = dev->ethtool_ops;
214
Michał Mirosław5455c692011-02-15 16:59:17 +0000215 if (sset == ETH_SS_FEATURES)
216 return ARRAY_SIZE(netdev_features_strings);
217
Eyal Perry892311f2014-12-02 18:12:10 +0200218 if (sset == ETH_SS_RSS_HASH_FUNCS)
219 return ARRAY_SIZE(rss_hash_func_strings);
220
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300221 if (sset == ETH_SS_TUNABLES)
222 return ARRAY_SIZE(tunable_strings);
223
Andrew Lunnf3a40942015-12-30 16:28:25 +0100224 if (sset == ETH_SS_PHY_STATS) {
225 if (dev->phydev)
226 return phy_get_sset_count(dev->phydev);
227 else
228 return -EOPNOTSUPP;
229 }
230
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000231 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000232 return ops->get_sset_count(dev, sset);
233 else
234 return -EOPNOTSUPP;
235}
236
237static void __ethtool_get_strings(struct net_device *dev,
238 u32 stringset, u8 *data)
239{
240 const struct ethtool_ops *ops = dev->ethtool_ops;
241
Michał Mirosław5455c692011-02-15 16:59:17 +0000242 if (stringset == ETH_SS_FEATURES)
243 memcpy(data, netdev_features_strings,
244 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200245 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
246 memcpy(data, rss_hash_func_strings,
247 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300248 else if (stringset == ETH_SS_TUNABLES)
249 memcpy(data, tunable_strings, sizeof(tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100250 else if (stringset == ETH_SS_PHY_STATS) {
251 struct phy_device *phydev = dev->phydev;
252
253 if (phydev) {
254 mutex_lock(&phydev->lock);
255 phydev->drv->get_strings(phydev, data);
256 mutex_unlock(&phydev->lock);
257 } else {
258 return;
259 }
260 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000261 /* ops->get_strings is valid because checked earlier */
262 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000263}
264
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000265static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000266{
267 /* feature masks of legacy discrete ethtool ops */
268
269 switch (eth_cmd) {
270 case ETHTOOL_GTXCSUM:
271 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800272 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000273 case ETHTOOL_GRXCSUM:
274 case ETHTOOL_SRXCSUM:
275 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000276 case ETHTOOL_GSG:
277 case ETHTOOL_SSG:
278 return NETIF_F_SG;
279 case ETHTOOL_GTSO:
280 case ETHTOOL_STSO:
281 return NETIF_F_ALL_TSO;
282 case ETHTOOL_GUFO:
283 case ETHTOOL_SUFO:
284 return NETIF_F_UFO;
285 case ETHTOOL_GGSO:
286 case ETHTOOL_SGSO:
287 return NETIF_F_GSO;
288 case ETHTOOL_GGRO:
289 case ETHTOOL_SGRO:
290 return NETIF_F_GRO;
291 default:
292 BUG();
293 }
294}
295
Michał Mirosław0a417702011-02-15 16:59:17 +0000296static int ethtool_get_one_feature(struct net_device *dev,
297 char __user *useraddr, u32 ethcmd)
298{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000299 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000300 struct ethtool_value edata = {
301 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000302 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000303 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000304
Michał Mirosław0a417702011-02-15 16:59:17 +0000305 if (copy_to_user(useraddr, &edata, sizeof(edata)))
306 return -EFAULT;
307 return 0;
308}
309
Michał Mirosław0a417702011-02-15 16:59:17 +0000310static int ethtool_set_one_feature(struct net_device *dev,
311 void __user *useraddr, u32 ethcmd)
312{
313 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000314 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000315
316 if (copy_from_user(&edata, useraddr, sizeof(edata)))
317 return -EFAULT;
318
Michał Mirosław86794882011-02-15 16:59:17 +0000319 mask = ethtool_get_feature_mask(ethcmd);
320 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000321 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000322 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000323
324 if (edata.data)
325 dev->wanted_features |= mask;
326 else
327 dev->wanted_features &= ~mask;
328
329 __netdev_update_features(dev);
330
331 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000332}
333
Michał Mirosław02b3a552011-11-15 15:29:55 +0000334#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
335 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000336#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
337 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
338 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000339
340static u32 __ethtool_get_flags(struct net_device *dev)
341{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000342 u32 flags = 0;
343
Dragos Foianu8a731252013-07-13 14:43:00 +0100344 if (dev->features & NETIF_F_LRO)
345 flags |= ETH_FLAG_LRO;
346 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
347 flags |= ETH_FLAG_RXVLAN;
348 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
349 flags |= ETH_FLAG_TXVLAN;
350 if (dev->features & NETIF_F_NTUPLE)
351 flags |= ETH_FLAG_NTUPLE;
352 if (dev->features & NETIF_F_RXHASH)
353 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000354
355 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000356}
357
358static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000359{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000360 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000361
Michał Mirosław02b3a552011-11-15 15:29:55 +0000362 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000363 return -EINVAL;
364
Dragos Foianu8a731252013-07-13 14:43:00 +0100365 if (data & ETH_FLAG_LRO)
366 features |= NETIF_F_LRO;
367 if (data & ETH_FLAG_RXVLAN)
368 features |= NETIF_F_HW_VLAN_CTAG_RX;
369 if (data & ETH_FLAG_TXVLAN)
370 features |= NETIF_F_HW_VLAN_CTAG_TX;
371 if (data & ETH_FLAG_NTUPLE)
372 features |= NETIF_F_NTUPLE;
373 if (data & ETH_FLAG_RXHASH)
374 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000375
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000376 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000377 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000378 if (changed & ~dev->hw_features)
379 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
380
381 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000382 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000383
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700384 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000385
386 return 0;
387}
388
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000389int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000391 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000393 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EOPNOTSUPP;
395
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000396 memset(cmd, 0, sizeof(struct ethtool_cmd));
397 cmd->cmd = ETHTOOL_GSET;
398 return dev->ethtool_ops->get_settings(dev, cmd);
399}
400EXPORT_SYMBOL(__ethtool_get_settings);
401
402static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
403{
404 int err;
405 struct ethtool_cmd cmd;
406
David S. Miller18ec8982015-06-01 14:43:50 -0700407 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (err < 0)
409 return err;
410
411 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
412 return -EFAULT;
413 return 0;
414}
415
416static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
417{
418 struct ethtool_cmd cmd;
419
420 if (!dev->ethtool_ops->set_settings)
421 return -EOPNOTSUPP;
422
423 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
424 return -EFAULT;
425
426 return dev->ethtool_ops->set_settings(dev, &cmd);
427}
428
chavey97f8aef2010-04-07 21:54:42 -0700429static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
430 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700433 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 memset(&info, 0, sizeof(info));
436 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000437 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700438 ops->get_drvinfo(dev, &info);
439 } else if (dev->dev.parent && dev->dev.parent->driver) {
440 strlcpy(info.bus_info, dev_name(dev->dev.parent),
441 sizeof(info.bus_info));
442 strlcpy(info.driver, dev->dev.parent->driver->name,
443 sizeof(info.driver));
444 } else {
445 return -EOPNOTSUPP;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jeff Garzik723b2f52010-03-03 22:51:50 +0000448 /*
449 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000450 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000451 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000452 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700453 int rc;
454
455 rc = ops->get_sset_count(dev, ETH_SS_TEST);
456 if (rc >= 0)
457 info.testinfo_len = rc;
458 rc = ops->get_sset_count(dev, ETH_SS_STATS);
459 if (rc >= 0)
460 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700461 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
462 if (rc >= 0)
463 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700464 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000465 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000467 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 info.eedump_len = ops->get_eeprom_len(dev);
469
470 if (copy_to_user(useraddr, &info, sizeof(info)))
471 return -EFAULT;
472 return 0;
473}
474
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800475static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700476 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000477{
478 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000479 u64 sset_mask;
480 int i, idx = 0, n_bits = 0, ret, rc;
481 u32 *info_buf = NULL;
482
Jeff Garzik723b2f52010-03-03 22:51:50 +0000483 if (copy_from_user(&info, useraddr, sizeof(info)))
484 return -EFAULT;
485
486 /* store copy of mask, because we zero struct later on */
487 sset_mask = info.sset_mask;
488 if (!sset_mask)
489 return 0;
490
491 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000492 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000493
494 memset(&info, 0, sizeof(info));
495 info.cmd = ETHTOOL_GSSET_INFO;
496
497 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
498 if (!info_buf)
499 return -ENOMEM;
500
501 /*
502 * fill return buffer based on input bitmask and successful
503 * get_sset_count return
504 */
505 for (i = 0; i < 64; i++) {
506 if (!(sset_mask & (1ULL << i)))
507 continue;
508
Michał Mirosław340ae162011-02-15 16:59:16 +0000509 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000510 if (rc >= 0) {
511 info.sset_mask |= (1ULL << i);
512 info_buf[idx++] = rc;
513 }
514 }
515
516 ret = -EFAULT;
517 if (copy_to_user(useraddr, &info, sizeof(info)))
518 goto out;
519
520 useraddr += offsetof(struct ethtool_sset_info, data);
521 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
522 goto out;
523
524 ret = 0;
525
526out:
527 kfree(info_buf);
528 return ret;
529}
530
chavey97f8aef2010-04-07 21:54:42 -0700531static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000532 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700533{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000534 struct ethtool_rxnfc info;
535 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000536 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700537
Santwona Behera59089d82009-02-20 00:58:13 -0800538 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700539 return -EOPNOTSUPP;
540
Ben Hutchingsbf988432010-06-28 08:45:58 +0000541 /* struct ethtool_rxnfc was originally defined for
542 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
543 * members. User-space might still be using that
544 * definition. */
545 if (cmd == ETHTOOL_SRXFH)
546 info_size = (offsetof(struct ethtool_rxnfc, data) +
547 sizeof(info.data));
548
549 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700550 return -EFAULT;
551
Ben Hutchings55664f32012-01-03 12:04:51 +0000552 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
553 if (rc)
554 return rc;
555
556 if (cmd == ETHTOOL_SRXCLSRLINS &&
557 copy_to_user(useraddr, &info, info_size))
558 return -EFAULT;
559
560 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700561}
562
chavey97f8aef2010-04-07 21:54:42 -0700563static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000564 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700565{
566 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000567 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800568 const struct ethtool_ops *ops = dev->ethtool_ops;
569 int ret;
570 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700571
Santwona Behera59089d82009-02-20 00:58:13 -0800572 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700573 return -EOPNOTSUPP;
574
Ben Hutchingsbf988432010-06-28 08:45:58 +0000575 /* struct ethtool_rxnfc was originally defined for
576 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
577 * members. User-space might still be using that
578 * definition. */
579 if (cmd == ETHTOOL_GRXFH)
580 info_size = (offsetof(struct ethtool_rxnfc, data) +
581 sizeof(info.data));
582
583 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700584 return -EFAULT;
585
Santwona Behera59089d82009-02-20 00:58:13 -0800586 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
587 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000588 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000589 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000590 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800591 if (!rule_buf)
592 return -ENOMEM;
593 }
594 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700595
Santwona Behera59089d82009-02-20 00:58:13 -0800596 ret = ops->get_rxnfc(dev, &info, rule_buf);
597 if (ret < 0)
598 goto err_out;
599
600 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000601 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800602 goto err_out;
603
604 if (rule_buf) {
605 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
606 if (copy_to_user(useraddr, rule_buf,
607 info.rule_cnt * sizeof(u32)))
608 goto err_out;
609 }
610 ret = 0;
611
612err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700613 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800614
615 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700616}
617
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530618static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
619 struct ethtool_rxnfc *rx_rings,
620 u32 size)
621{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100622 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530623
624 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100625 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530626
627 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100628 for (i = 0; i < size; i++)
629 if (indir[i] >= rx_rings->data)
630 return -EINVAL;
631
632 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530633}
634
Kim Jonesba905f52016-02-02 03:51:16 +0000635u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -0800636
637void netdev_rss_key_fill(void *buffer, size_t len)
638{
639 BUG_ON(len > sizeof(netdev_rss_key));
640 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
641 memcpy(buffer, netdev_rss_key, len);
642}
643EXPORT_SYMBOL(netdev_rss_key_fill);
644
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -0800645static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
646{
647 u32 dev_size, current_max = 0;
648 u32 *indir;
649 int ret;
650
651 if (!dev->ethtool_ops->get_rxfh_indir_size ||
652 !dev->ethtool_ops->get_rxfh)
653 return -EOPNOTSUPP;
654 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
655 if (dev_size == 0)
656 return -EOPNOTSUPP;
657
658 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
659 if (!indir)
660 return -ENOMEM;
661
662 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
663 if (ret)
664 goto out;
665
666 while (dev_size--)
667 current_max = max(current_max, indir[dev_size]);
668
669 *max = current_max;
670
671out:
672 kfree(indir);
673 return ret;
674}
675
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000676static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
677 void __user *useraddr)
678{
Ben Hutchings7850f632011-12-15 13:55:01 +0000679 u32 user_size, dev_size;
680 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000681 int ret;
682
Ben Hutchings7850f632011-12-15 13:55:01 +0000683 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100684 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +0000685 return -EOPNOTSUPP;
686 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
687 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000688 return -EOPNOTSUPP;
689
Ben Hutchings7850f632011-12-15 13:55:01 +0000690 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000691 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000692 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000693 return -EFAULT;
694
Ben Hutchings7850f632011-12-15 13:55:01 +0000695 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
696 &dev_size, sizeof(dev_size)))
697 return -EFAULT;
698
699 /* If the user buffer size is 0, this is just a query for the
700 * device table size. Otherwise, if it's smaller than the
701 * device table size it's an error.
702 */
703 if (user_size < dev_size)
704 return user_size == 0 ? 0 : -EINVAL;
705
706 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000707 if (!indir)
708 return -ENOMEM;
709
Eyal Perry892311f2014-12-02 18:12:10 +0200710 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000711 if (ret)
712 goto out;
713
Ben Hutchings7850f632011-12-15 13:55:01 +0000714 if (copy_to_user(useraddr +
715 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
716 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000717 ret = -EFAULT;
718
719out:
720 kfree(indir);
721 return ret;
722}
723
724static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
725 void __user *useraddr)
726{
Ben Hutchings7850f632011-12-15 13:55:01 +0000727 struct ethtool_rxnfc rx_rings;
728 u32 user_size, dev_size, i;
729 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000730 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000731 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530732 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000733
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100734 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000735 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000736 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000737
738 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000739 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000740 return -EOPNOTSUPP;
741
Ben Hutchings7850f632011-12-15 13:55:01 +0000742 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000743 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000744 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000745 return -EFAULT;
746
Ben Hutchings278bc422011-12-15 13:56:49 +0000747 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000748 return -EINVAL;
749
750 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000751 if (!indir)
752 return -ENOMEM;
753
Ben Hutchings7850f632011-12-15 13:55:01 +0000754 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000755 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000756 if (ret)
757 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000758
759 if (user_size == 0) {
760 for (i = 0; i < dev_size; i++)
761 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
762 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530763 ret = ethtool_copy_validate_indir(indir,
764 useraddr + ringidx_offset,
765 &rx_rings,
766 dev_size);
767 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +0000768 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +0000769 }
770
Eyal Perry892311f2014-12-02 18:12:10 +0200771 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -0800772 if (ret)
773 goto out;
774
775 /* indicate whether rxfh was set to default */
776 if (user_size == 0)
777 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
778 else
779 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000780
781out:
782 kfree(indir);
783 return ret;
784}
785
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530786static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
787 void __user *useraddr)
788{
789 int ret;
790 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100791 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530792 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100793 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530794 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100795 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530796 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +0200797 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530798 u8 *hkey = NULL;
799 u8 *rss_config;
800
Eyal Perry892311f2014-12-02 18:12:10 +0200801 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530802 return -EOPNOTSUPP;
803
804 if (ops->get_rxfh_indir_size)
805 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530806 if (ops->get_rxfh_key_size)
807 dev_key_size = ops->get_rxfh_key_size(dev);
808
Ben Hutchingsf062a382014-05-15 16:28:07 +0100809 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530810 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100811 user_indir_size = rxfh.indir_size;
812 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530813
Ben Hutchingsf062a382014-05-15 16:28:07 +0100814 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +0200815 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
816 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +0100817 return -EINVAL;
818
819 rxfh.indir_size = dev_indir_size;
820 rxfh.key_size = dev_key_size;
821 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530822 return -EFAULT;
823
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530824 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
825 (user_key_size && (user_key_size != dev_key_size)))
826 return -EINVAL;
827
828 indir_bytes = user_indir_size * sizeof(indir[0]);
829 total_size = indir_bytes + user_key_size;
830 rss_config = kzalloc(total_size, GFP_USER);
831 if (!rss_config)
832 return -ENOMEM;
833
834 if (user_indir_size)
835 indir = (u32 *)rss_config;
836
837 if (user_key_size)
838 hkey = rss_config + indir_bytes;
839
Eyal Perry892311f2014-12-02 18:12:10 +0200840 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
841 if (ret)
842 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530843
Eyal Perry892311f2014-12-02 18:12:10 +0200844 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
845 &dev_hfunc, sizeof(rxfh.hfunc))) {
846 ret = -EFAULT;
847 } else if (copy_to_user(useraddr +
848 offsetof(struct ethtool_rxfh, rss_config[0]),
849 rss_config, total_size)) {
850 ret = -EFAULT;
851 }
852out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530853 kfree(rss_config);
854
855 return ret;
856}
857
858static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
859 void __user *useraddr)
860{
861 int ret;
862 const struct ethtool_ops *ops = dev->ethtool_ops;
863 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100864 struct ethtool_rxfh rxfh;
865 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530866 u32 *indir = NULL, indir_bytes = 0;
867 u8 *hkey = NULL;
868 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530869 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
870
Eyal Perry892311f2014-12-02 18:12:10 +0200871 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530872 return -EOPNOTSUPP;
873
874 if (ops->get_rxfh_indir_size)
875 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530876 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +0300877 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530878
Ben Hutchingsf062a382014-05-15 16:28:07 +0100879 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530880 return -EFAULT;
881
Ben Hutchingsf062a382014-05-15 16:28:07 +0100882 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +0200883 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
884 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +0100885 return -EINVAL;
886
Eyal Perry892311f2014-12-02 18:12:10 +0200887 /* If either indir, hash key or function is valid, proceed further.
888 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530889 */
Ben Hutchingsf062a382014-05-15 16:28:07 +0100890 if ((rxfh.indir_size &&
891 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
892 rxfh.indir_size != dev_indir_size) ||
893 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
894 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +0200895 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530896 return -EINVAL;
897
Ben Hutchingsf062a382014-05-15 16:28:07 +0100898 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530899 indir_bytes = dev_indir_size * sizeof(indir[0]);
900
Ben Hutchingsf062a382014-05-15 16:28:07 +0100901 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530902 if (!rss_config)
903 return -ENOMEM;
904
905 rx_rings.cmd = ETHTOOL_GRXRINGS;
906 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
907 if (ret)
908 goto out;
909
Ben Hutchingsf062a382014-05-15 16:28:07 +0100910 /* rxfh.indir_size == 0 means reset the indir table to default.
911 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530912 */
Ben Hutchingsf062a382014-05-15 16:28:07 +0100913 if (rxfh.indir_size &&
914 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530915 indir = (u32 *)rss_config;
916 ret = ethtool_copy_validate_indir(indir,
917 useraddr + rss_cfg_offset,
918 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +0100919 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530920 if (ret)
921 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100922 } else if (rxfh.indir_size == 0) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530923 indir = (u32 *)rss_config;
924 for (i = 0; i < dev_indir_size; i++)
925 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
926 }
927
Ben Hutchingsf062a382014-05-15 16:28:07 +0100928 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530929 hkey = rss_config + indir_bytes;
930 if (copy_from_user(hkey,
931 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +0100932 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530933 ret = -EFAULT;
934 goto out;
935 }
936 }
937
Eyal Perry892311f2014-12-02 18:12:10 +0200938 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -0800939 if (ret)
940 goto out;
941
942 /* indicate whether rxfh was set to default */
943 if (rxfh.indir_size == 0)
944 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
945 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
946 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530947
948out:
949 kfree(rss_config);
950 return ret;
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
954{
955 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700956 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 void *regbuf;
958 int reglen, ret;
959
960 if (!ops->get_regs || !ops->get_regs_len)
961 return -EOPNOTSUPP;
962
963 if (copy_from_user(&regs, useraddr, sizeof(regs)))
964 return -EFAULT;
965
966 reglen = ops->get_regs_len(dev);
967 if (regs.len > reglen)
968 regs.len = reglen;
969
Eugene Teob7c7d012011-01-24 21:05:17 -0800970 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700971 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -ENOMEM;
973
974 ops->get_regs(dev, &regs, regbuf);
975
976 ret = -EFAULT;
977 if (copy_to_user(useraddr, &regs, sizeof(regs)))
978 goto out;
979 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700980 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto out;
982 ret = 0;
983
984 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000985 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return ret;
987}
988
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000989static int ethtool_reset(struct net_device *dev, char __user *useraddr)
990{
991 struct ethtool_value reset;
992 int ret;
993
994 if (!dev->ethtool_ops->reset)
995 return -EOPNOTSUPP;
996
997 if (copy_from_user(&reset, useraddr, sizeof(reset)))
998 return -EFAULT;
999
1000 ret = dev->ethtool_ops->reset(dev, &reset.data);
1001 if (ret)
1002 return ret;
1003
1004 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1005 return -EFAULT;
1006 return 0;
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1010{
Roland Dreier8e557422010-02-11 12:14:23 -08001011 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (!dev->ethtool_ops->get_wol)
1014 return -EOPNOTSUPP;
1015
1016 dev->ethtool_ops->get_wol(dev, &wol);
1017
1018 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1019 return -EFAULT;
1020 return 0;
1021}
1022
1023static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1024{
1025 struct ethtool_wolinfo wol;
1026
1027 if (!dev->ethtool_ops->set_wol)
1028 return -EOPNOTSUPP;
1029
1030 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1031 return -EFAULT;
1032
1033 return dev->ethtool_ops->set_wol(dev, &wol);
1034}
1035
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001036static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1037{
1038 struct ethtool_eee edata;
1039 int rc;
1040
1041 if (!dev->ethtool_ops->get_eee)
1042 return -EOPNOTSUPP;
1043
1044 memset(&edata, 0, sizeof(struct ethtool_eee));
1045 edata.cmd = ETHTOOL_GEEE;
1046 rc = dev->ethtool_ops->get_eee(dev, &edata);
1047
1048 if (rc)
1049 return rc;
1050
1051 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1052 return -EFAULT;
1053
1054 return 0;
1055}
1056
1057static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1058{
1059 struct ethtool_eee edata;
1060
1061 if (!dev->ethtool_ops->set_eee)
1062 return -EOPNOTSUPP;
1063
1064 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1065 return -EFAULT;
1066
1067 return dev->ethtool_ops->set_eee(dev, &edata);
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070static int ethtool_nway_reset(struct net_device *dev)
1071{
1072 if (!dev->ethtool_ops->nway_reset)
1073 return -EOPNOTSUPP;
1074
1075 return dev->ethtool_ops->nway_reset(dev);
1076}
1077
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001078static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1079{
1080 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1081
1082 if (!dev->ethtool_ops->get_link)
1083 return -EOPNOTSUPP;
1084
1085 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1086
1087 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1088 return -EFAULT;
1089 return 0;
1090}
1091
Ben Hutchings081d0942012-04-12 00:42:12 +00001092static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1093 int (*getter)(struct net_device *,
1094 struct ethtool_eeprom *, u8 *),
1095 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001098 void __user *userbuf = useraddr + sizeof(eeprom);
1099 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001101 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1104 return -EFAULT;
1105
1106 /* Check for wrap and zero */
1107 if (eeprom.offset + eeprom.len <= eeprom.offset)
1108 return -EINVAL;
1109
1110 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001111 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EINVAL;
1113
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001114 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!data)
1116 return -ENOMEM;
1117
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001118 bytes_remaining = eeprom.len;
1119 while (bytes_remaining > 0) {
1120 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Ben Hutchings081d0942012-04-12 00:42:12 +00001122 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001123 if (ret)
1124 break;
1125 if (copy_to_user(userbuf, data, eeprom.len)) {
1126 ret = -EFAULT;
1127 break;
1128 }
1129 userbuf += eeprom.len;
1130 eeprom.offset += eeprom.len;
1131 bytes_remaining -= eeprom.len;
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001134 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1135 eeprom.offset -= eeprom.len;
1136 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1137 ret = -EFAULT;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 kfree(data);
1140 return ret;
1141}
1142
Ben Hutchings081d0942012-04-12 00:42:12 +00001143static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1144{
1145 const struct ethtool_ops *ops = dev->ethtool_ops;
1146
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001147 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1148 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001149 return -EOPNOTSUPP;
1150
1151 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1152 ops->get_eeprom_len(dev));
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1156{
1157 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001158 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001159 void __user *userbuf = useraddr + sizeof(eeprom);
1160 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001162 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001164 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1165 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return -EOPNOTSUPP;
1167
1168 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1169 return -EFAULT;
1170
1171 /* Check for wrap and zero */
1172 if (eeprom.offset + eeprom.len <= eeprom.offset)
1173 return -EINVAL;
1174
1175 /* Check for exceeding total eeprom len */
1176 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1177 return -EINVAL;
1178
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001179 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (!data)
1181 return -ENOMEM;
1182
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001183 bytes_remaining = eeprom.len;
1184 while (bytes_remaining > 0) {
1185 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001187 if (copy_from_user(data, userbuf, eeprom.len)) {
1188 ret = -EFAULT;
1189 break;
1190 }
1191 ret = ops->set_eeprom(dev, &eeprom, data);
1192 if (ret)
1193 break;
1194 userbuf += eeprom.len;
1195 eeprom.offset += eeprom.len;
1196 bytes_remaining -= eeprom.len;
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 kfree(data);
1200 return ret;
1201}
1202
chavey97f8aef2010-04-07 21:54:42 -07001203static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1204 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Roland Dreier8e557422010-02-11 12:14:23 -08001206 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (!dev->ethtool_ops->get_coalesce)
1209 return -EOPNOTSUPP;
1210
1211 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1212
1213 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1214 return -EFAULT;
1215 return 0;
1216}
1217
chavey97f8aef2010-04-07 21:54:42 -07001218static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1219 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct ethtool_coalesce coalesce;
1222
David S. Millerfa04ae52005-06-06 15:07:19 -07001223 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return -EOPNOTSUPP;
1225
1226 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1227 return -EFAULT;
1228
1229 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1230}
1231
1232static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1233{
Roland Dreier8e557422010-02-11 12:14:23 -08001234 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (!dev->ethtool_ops->get_ringparam)
1237 return -EOPNOTSUPP;
1238
1239 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1240
1241 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1242 return -EFAULT;
1243 return 0;
1244}
1245
1246static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1247{
1248 struct ethtool_ringparam ringparam;
1249
1250 if (!dev->ethtool_ops->set_ringparam)
1251 return -EOPNOTSUPP;
1252
1253 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1254 return -EFAULT;
1255
1256 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1257}
1258
amit salecha8b5933c2011-04-07 01:58:42 +00001259static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1260 void __user *useraddr)
1261{
1262 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1263
1264 if (!dev->ethtool_ops->get_channels)
1265 return -EOPNOTSUPP;
1266
1267 dev->ethtool_ops->get_channels(dev, &channels);
1268
1269 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1270 return -EFAULT;
1271 return 0;
1272}
1273
1274static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1275 void __user *useraddr)
1276{
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001277 struct ethtool_channels channels, max;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001278 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001279
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001280 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001281 return -EOPNOTSUPP;
1282
1283 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1284 return -EFAULT;
1285
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001286 dev->ethtool_ops->get_channels(dev, &max);
1287
1288 /* ensure new counts are within the maximums */
1289 if ((channels.rx_count > max.max_rx) ||
1290 (channels.tx_count > max.max_tx) ||
1291 (channels.combined_count > max.max_combined) ||
1292 (channels.other_count > max.max_other))
1293 return -EINVAL;
1294
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001295 /* ensure the new Rx count fits within the configured Rx flow
1296 * indirection table settings */
1297 if (netif_is_rxfh_configured(dev) &&
1298 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1299 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1300 return -EINVAL;
1301
amit salecha8b5933c2011-04-07 01:58:42 +00001302 return dev->ethtool_ops->set_channels(dev, &channels);
1303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1306{
1307 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1308
1309 if (!dev->ethtool_ops->get_pauseparam)
1310 return -EOPNOTSUPP;
1311
1312 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1313
1314 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1315 return -EFAULT;
1316 return 0;
1317}
1318
1319static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1320{
1321 struct ethtool_pauseparam pauseparam;
1322
Jeff Garzike1b90c42006-07-17 12:54:40 -04001323 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EOPNOTSUPP;
1325
1326 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1327 return -EFAULT;
1328
1329 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1330}
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1333{
1334 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001335 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001337 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001339 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001340 return -EOPNOTSUPP;
1341
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001342 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001343 if (test_len < 0)
1344 return test_len;
1345 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 if (copy_from_user(&test, useraddr, sizeof(test)))
1348 return -EFAULT;
1349
Jeff Garzikff03d492007-08-15 16:01:08 -07001350 test.len = test_len;
1351 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!data)
1353 return -ENOMEM;
1354
1355 ops->self_test(dev, &test, data);
1356
1357 ret = -EFAULT;
1358 if (copy_to_user(useraddr, &test, sizeof(test)))
1359 goto out;
1360 useraddr += sizeof(test);
1361 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1362 goto out;
1363 ret = 0;
1364
1365 out:
1366 kfree(data);
1367 return ret;
1368}
1369
1370static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1371{
1372 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 u8 *data;
1374 int ret;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1377 return -EFAULT;
1378
Michał Mirosław340ae162011-02-15 16:59:16 +00001379 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001380 if (ret < 0)
1381 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001382
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001383 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Joe Perches077cb372015-10-14 01:09:40 -07001385 data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!data)
1387 return -ENOMEM;
1388
Michał Mirosław340ae162011-02-15 16:59:16 +00001389 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 ret = -EFAULT;
1392 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1393 goto out;
1394 useraddr += sizeof(gstrings);
1395 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1396 goto out;
1397 ret = 0;
1398
Michał Mirosław340ae162011-02-15 16:59:16 +00001399out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 kfree(data);
1401 return ret;
1402}
1403
1404static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1405{
1406 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001407 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001408 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001409 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001411 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -EOPNOTSUPP;
1413
Ben Hutchings68f512f2011-04-02 00:35:15 +01001414 if (busy)
1415 return -EBUSY;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (copy_from_user(&id, useraddr, sizeof(id)))
1418 return -EFAULT;
1419
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001420 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001421 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001422 return rc;
1423
1424 /* Drop the RTNL lock while waiting, but prevent reentry or
1425 * removal of the device.
1426 */
1427 busy = true;
1428 dev_hold(dev);
1429 rtnl_unlock();
1430
1431 if (rc == 0) {
1432 /* Driver will handle this itself */
1433 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001434 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001435 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001436 /* Driver expects to be called at twice the frequency in rc */
1437 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001438
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001439 /* Count down seconds */
1440 do {
1441 /* Count down iterations per second */
1442 i = n;
1443 do {
1444 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001445 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001446 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1447 rtnl_unlock();
1448 if (rc)
1449 break;
1450 schedule_timeout_interruptible(interval);
1451 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001452 } while (!signal_pending(current) &&
1453 (id.data == 0 || --id.data != 0));
1454 }
1455
1456 rtnl_lock();
1457 dev_put(dev);
1458 busy = false;
1459
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001460 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001461 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1465{
1466 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001467 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001469 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001471 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001472 return -EOPNOTSUPP;
1473
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001474 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001475 if (n_stats < 0)
1476 return n_stats;
1477 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1480 return -EFAULT;
1481
Jeff Garzikff03d492007-08-15 16:01:08 -07001482 stats.n_stats = n_stats;
1483 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (!data)
1485 return -ENOMEM;
1486
1487 ops->get_ethtool_stats(dev, &stats, data);
1488
1489 ret = -EFAULT;
1490 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1491 goto out;
1492 useraddr += sizeof(stats);
1493 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1494 goto out;
1495 ret = 0;
1496
1497 out:
1498 kfree(data);
1499 return ret;
1500}
1501
Andrew Lunnf3a40942015-12-30 16:28:25 +01001502static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1503{
1504 struct ethtool_stats stats;
1505 struct phy_device *phydev = dev->phydev;
1506 u64 *data;
1507 int ret, n_stats;
1508
1509 if (!phydev)
1510 return -EOPNOTSUPP;
1511
1512 n_stats = phy_get_sset_count(phydev);
1513
1514 if (n_stats < 0)
1515 return n_stats;
1516 WARN_ON(n_stats == 0);
1517
1518 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1519 return -EFAULT;
1520
1521 stats.n_stats = n_stats;
1522 data = kmalloc_array(n_stats, sizeof(u64), GFP_USER);
1523 if (!data)
1524 return -ENOMEM;
1525
1526 mutex_lock(&phydev->lock);
1527 phydev->drv->get_stats(phydev, &stats, data);
1528 mutex_unlock(&phydev->lock);
1529
1530 ret = -EFAULT;
1531 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1532 goto out;
1533 useraddr += sizeof(stats);
1534 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1535 goto out;
1536 ret = 0;
1537
1538 out:
1539 kfree(data);
1540 return ret;
1541}
1542
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001543static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001544{
1545 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001546
Matthew Wilcox313674a2007-07-31 14:00:29 -07001547 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001548 return -EFAULT;
1549
Matthew Wilcox313674a2007-07-31 14:00:29 -07001550 if (epaddr.size < dev->addr_len)
1551 return -ETOOSMALL;
1552 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001553
Jon Wetzela6f9a702005-08-20 17:15:54 -07001554 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001555 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001556 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001557 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1558 return -EFAULT;
1559 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001560}
1561
Jeff Garzik13c99b22007-08-15 16:01:56 -07001562static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1563 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001564{
Roland Dreier8e557422010-02-11 12:14:23 -08001565 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001566
Jeff Garzik13c99b22007-08-15 16:01:56 -07001567 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001568 return -EOPNOTSUPP;
1569
Jeff Garzik13c99b22007-08-15 16:01:56 -07001570 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001571
1572 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1573 return -EFAULT;
1574 return 0;
1575}
1576
Jeff Garzik13c99b22007-08-15 16:01:56 -07001577static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1578 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001579{
1580 struct ethtool_value edata;
1581
Jeff Garzik13c99b22007-08-15 16:01:56 -07001582 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001583 return -EOPNOTSUPP;
1584
1585 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1586 return -EFAULT;
1587
Jeff Garzik13c99b22007-08-15 16:01:56 -07001588 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001589 return 0;
1590}
1591
Jeff Garzik13c99b22007-08-15 16:01:56 -07001592static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1593 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001594{
1595 struct ethtool_value edata;
1596
Jeff Garzik13c99b22007-08-15 16:01:56 -07001597 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001598 return -EOPNOTSUPP;
1599
1600 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1601 return -EFAULT;
1602
Jeff Garzik13c99b22007-08-15 16:01:56 -07001603 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001604}
1605
chavey97f8aef2010-04-07 21:54:42 -07001606static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1607 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001608{
1609 struct ethtool_flash efl;
1610
1611 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1612 return -EFAULT;
1613
1614 if (!dev->ethtool_ops->flash_device)
1615 return -EOPNOTSUPP;
1616
Ben Hutchings786f5282012-02-01 09:32:25 +00001617 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1618
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001619 return dev->ethtool_ops->flash_device(dev, &efl);
1620}
1621
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001622static int ethtool_set_dump(struct net_device *dev,
1623 void __user *useraddr)
1624{
1625 struct ethtool_dump dump;
1626
1627 if (!dev->ethtool_ops->set_dump)
1628 return -EOPNOTSUPP;
1629
1630 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1631 return -EFAULT;
1632
1633 return dev->ethtool_ops->set_dump(dev, &dump);
1634}
1635
1636static int ethtool_get_dump_flag(struct net_device *dev,
1637 void __user *useraddr)
1638{
1639 int ret;
1640 struct ethtool_dump dump;
1641 const struct ethtool_ops *ops = dev->ethtool_ops;
1642
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001643 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001644 return -EOPNOTSUPP;
1645
1646 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1647 return -EFAULT;
1648
1649 ret = ops->get_dump_flag(dev, &dump);
1650 if (ret)
1651 return ret;
1652
1653 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1654 return -EFAULT;
1655 return 0;
1656}
1657
1658static int ethtool_get_dump_data(struct net_device *dev,
1659 void __user *useraddr)
1660{
1661 int ret;
1662 __u32 len;
1663 struct ethtool_dump dump, tmp;
1664 const struct ethtool_ops *ops = dev->ethtool_ops;
1665 void *data = NULL;
1666
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001667 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001668 return -EOPNOTSUPP;
1669
1670 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1671 return -EFAULT;
1672
1673 memset(&tmp, 0, sizeof(tmp));
1674 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1675 ret = ops->get_dump_flag(dev, &tmp);
1676 if (ret)
1677 return ret;
1678
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001679 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001680 if (!len)
1681 return -EFAULT;
1682
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001683 /* Don't ever let the driver think there's more space available
1684 * than it requested with .get_dump_flag().
1685 */
1686 dump.len = len;
1687
1688 /* Always allocate enough space to hold the whole thing so that the
1689 * driver does not need to check the length and bother with partial
1690 * dumping.
1691 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001692 data = vzalloc(tmp.len);
1693 if (!data)
1694 return -ENOMEM;
1695 ret = ops->get_dump_data(dev, &dump, data);
1696 if (ret)
1697 goto out;
1698
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001699 /* There are two sane possibilities:
1700 * 1. The driver's .get_dump_data() does not touch dump.len.
1701 * 2. Or it may set dump.len to how much it really writes, which
1702 * should be tmp.len (or len if it can do a partial dump).
1703 * In any case respond to userspace with the actual length of data
1704 * it's receiving.
1705 */
1706 WARN_ON(dump.len != len && dump.len != tmp.len);
1707 dump.len = len;
1708
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001709 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1710 ret = -EFAULT;
1711 goto out;
1712 }
1713 useraddr += offsetof(struct ethtool_dump, data);
1714 if (copy_to_user(useraddr, data, len))
1715 ret = -EFAULT;
1716out:
1717 vfree(data);
1718 return ret;
1719}
1720
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001721static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1722{
1723 int err = 0;
1724 struct ethtool_ts_info info;
1725 const struct ethtool_ops *ops = dev->ethtool_ops;
1726 struct phy_device *phydev = dev->phydev;
1727
1728 memset(&info, 0, sizeof(info));
1729 info.cmd = ETHTOOL_GET_TS_INFO;
1730
1731 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001732 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001733 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001734 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001735 } else {
1736 info.so_timestamping =
1737 SOF_TIMESTAMPING_RX_SOFTWARE |
1738 SOF_TIMESTAMPING_SOFTWARE;
1739 info.phc_index = -1;
1740 }
1741
1742 if (err)
1743 return err;
1744
1745 if (copy_to_user(useraddr, &info, sizeof(info)))
1746 err = -EFAULT;
1747
1748 return err;
1749}
1750
Ed Swierk2f438362015-01-02 17:27:56 -08001751static int __ethtool_get_module_info(struct net_device *dev,
1752 struct ethtool_modinfo *modinfo)
1753{
1754 const struct ethtool_ops *ops = dev->ethtool_ops;
1755 struct phy_device *phydev = dev->phydev;
1756
1757 if (phydev && phydev->drv && phydev->drv->module_info)
1758 return phydev->drv->module_info(phydev, modinfo);
1759
1760 if (ops->get_module_info)
1761 return ops->get_module_info(dev, modinfo);
1762
1763 return -EOPNOTSUPP;
1764}
1765
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001766static int ethtool_get_module_info(struct net_device *dev,
1767 void __user *useraddr)
1768{
1769 int ret;
1770 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001771
1772 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1773 return -EFAULT;
1774
Ed Swierk2f438362015-01-02 17:27:56 -08001775 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001776 if (ret)
1777 return ret;
1778
1779 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1780 return -EFAULT;
1781
1782 return 0;
1783}
1784
Ed Swierk2f438362015-01-02 17:27:56 -08001785static int __ethtool_get_module_eeprom(struct net_device *dev,
1786 struct ethtool_eeprom *ee, u8 *data)
1787{
1788 const struct ethtool_ops *ops = dev->ethtool_ops;
1789 struct phy_device *phydev = dev->phydev;
1790
1791 if (phydev && phydev->drv && phydev->drv->module_eeprom)
1792 return phydev->drv->module_eeprom(phydev, ee, data);
1793
1794 if (ops->get_module_eeprom)
1795 return ops->get_module_eeprom(dev, ee, data);
1796
1797 return -EOPNOTSUPP;
1798}
1799
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001800static int ethtool_get_module_eeprom(struct net_device *dev,
1801 void __user *useraddr)
1802{
1803 int ret;
1804 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001805
Ed Swierk2f438362015-01-02 17:27:56 -08001806 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001807 if (ret)
1808 return ret;
1809
Ed Swierk2f438362015-01-02 17:27:56 -08001810 return ethtool_get_any_eeprom(dev, useraddr,
1811 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001812 modinfo.eeprom_len);
1813}
1814
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301815static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
1816{
1817 switch (tuna->id) {
1818 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03001819 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301820 if (tuna->len != sizeof(u32) ||
1821 tuna->type_id != ETHTOOL_TUNABLE_U32)
1822 return -EINVAL;
1823 break;
1824 default:
1825 return -EINVAL;
1826 }
1827
1828 return 0;
1829}
1830
1831static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
1832{
1833 int ret;
1834 struct ethtool_tunable tuna;
1835 const struct ethtool_ops *ops = dev->ethtool_ops;
1836 void *data;
1837
1838 if (!ops->get_tunable)
1839 return -EOPNOTSUPP;
1840 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1841 return -EFAULT;
1842 ret = ethtool_tunable_valid(&tuna);
1843 if (ret)
1844 return ret;
1845 data = kmalloc(tuna.len, GFP_USER);
1846 if (!data)
1847 return -ENOMEM;
1848 ret = ops->get_tunable(dev, &tuna, data);
1849 if (ret)
1850 goto out;
1851 useraddr += sizeof(tuna);
1852 ret = -EFAULT;
1853 if (copy_to_user(useraddr, data, tuna.len))
1854 goto out;
1855 ret = 0;
1856
1857out:
1858 kfree(data);
1859 return ret;
1860}
1861
1862static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
1863{
1864 int ret;
1865 struct ethtool_tunable tuna;
1866 const struct ethtool_ops *ops = dev->ethtool_ops;
1867 void *data;
1868
1869 if (!ops->set_tunable)
1870 return -EOPNOTSUPP;
1871 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1872 return -EFAULT;
1873 ret = ethtool_tunable_valid(&tuna);
1874 if (ret)
1875 return ret;
1876 data = kmalloc(tuna.len, GFP_USER);
1877 if (!data)
1878 return -ENOMEM;
1879 useraddr += sizeof(tuna);
1880 ret = -EFAULT;
1881 if (copy_from_user(data, useraddr, tuna.len))
1882 goto out;
1883 ret = ops->set_tunable(dev, &tuna, data);
1884
1885out:
1886 kfree(data);
1887 return ret;
1888}
1889
Yan Burman600fed52013-06-03 02:03:34 +00001890/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Eric W. Biederman881d9662007-09-17 11:56:21 -07001892int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001894 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 void __user *useraddr = ifr->ifr_data;
1896 u32 ethcmd;
1897 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00001898 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!dev || !netif_device_present(dev))
1901 return -ENODEV;
1902
chavey97f8aef2010-04-07 21:54:42 -07001903 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return -EFAULT;
1905
Stephen Hemminger75f31232006-09-28 15:13:37 -07001906 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001907 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001908 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001909 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001910 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001911 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001912 case ETHTOOL_GCOALESCE:
1913 case ETHTOOL_GRINGPARAM:
1914 case ETHTOOL_GPAUSEPARAM:
1915 case ETHTOOL_GRXCSUM:
1916 case ETHTOOL_GTXCSUM:
1917 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001918 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001919 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001920 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01001921 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001922 case ETHTOOL_GTSO:
1923 case ETHTOOL_GPERMADDR:
1924 case ETHTOOL_GUFO:
1925 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001926 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001927 case ETHTOOL_GFLAGS:
1928 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001929 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001930 case ETHTOOL_GRXRINGS:
1931 case ETHTOOL_GRXCLSRLCNT:
1932 case ETHTOOL_GRXCLSRULE:
1933 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001934 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301935 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00001936 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001937 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001938 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001939 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301940 case ETHTOOL_GTUNABLE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001941 break;
1942 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001943 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001944 return -EPERM;
1945 }
1946
chavey97f8aef2010-04-07 21:54:42 -07001947 if (dev->ethtool_ops->begin) {
1948 rc = dev->ethtool_ops->begin(dev);
1949 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001951 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001952 old_features = dev->features;
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 switch (ethcmd) {
1955 case ETHTOOL_GSET:
1956 rc = ethtool_get_settings(dev, useraddr);
1957 break;
1958 case ETHTOOL_SSET:
1959 rc = ethtool_set_settings(dev, useraddr);
1960 break;
1961 case ETHTOOL_GDRVINFO:
1962 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
1964 case ETHTOOL_GREGS:
1965 rc = ethtool_get_regs(dev, useraddr);
1966 break;
1967 case ETHTOOL_GWOL:
1968 rc = ethtool_get_wol(dev, useraddr);
1969 break;
1970 case ETHTOOL_SWOL:
1971 rc = ethtool_set_wol(dev, useraddr);
1972 break;
1973 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001974 rc = ethtool_get_value(dev, useraddr, ethcmd,
1975 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
1977 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001978 rc = ethtool_set_value_void(dev, useraddr,
1979 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001981 case ETHTOOL_GEEE:
1982 rc = ethtool_get_eee(dev, useraddr);
1983 break;
1984 case ETHTOOL_SEEE:
1985 rc = ethtool_set_eee(dev, useraddr);
1986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 case ETHTOOL_NWAY_RST:
1988 rc = ethtool_nway_reset(dev);
1989 break;
1990 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001991 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 break;
1993 case ETHTOOL_GEEPROM:
1994 rc = ethtool_get_eeprom(dev, useraddr);
1995 break;
1996 case ETHTOOL_SEEPROM:
1997 rc = ethtool_set_eeprom(dev, useraddr);
1998 break;
1999 case ETHTOOL_GCOALESCE:
2000 rc = ethtool_get_coalesce(dev, useraddr);
2001 break;
2002 case ETHTOOL_SCOALESCE:
2003 rc = ethtool_set_coalesce(dev, useraddr);
2004 break;
2005 case ETHTOOL_GRINGPARAM:
2006 rc = ethtool_get_ringparam(dev, useraddr);
2007 break;
2008 case ETHTOOL_SRINGPARAM:
2009 rc = ethtool_set_ringparam(dev, useraddr);
2010 break;
2011 case ETHTOOL_GPAUSEPARAM:
2012 rc = ethtool_get_pauseparam(dev, useraddr);
2013 break;
2014 case ETHTOOL_SPAUSEPARAM:
2015 rc = ethtool_set_pauseparam(dev, useraddr);
2016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 case ETHTOOL_TEST:
2018 rc = ethtool_self_test(dev, useraddr);
2019 break;
2020 case ETHTOOL_GSTRINGS:
2021 rc = ethtool_get_strings(dev, useraddr);
2022 break;
2023 case ETHTOOL_PHYS_ID:
2024 rc = ethtool_phys_id(dev, useraddr);
2025 break;
2026 case ETHTOOL_GSTATS:
2027 rc = ethtool_get_stats(dev, useraddr);
2028 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002029 case ETHTOOL_GPERMADDR:
2030 rc = ethtool_get_perm_addr(dev, useraddr);
2031 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002032 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002033 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002034 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002035 break;
2036 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002037 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002038 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002039 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002040 rc = ethtool_get_value(dev, useraddr, ethcmd,
2041 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002042 break;
2043 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002044 rc = ethtool_set_value(dev, useraddr,
2045 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002046 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002047 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002048 case ETHTOOL_GRXRINGS:
2049 case ETHTOOL_GRXCLSRLCNT:
2050 case ETHTOOL_GRXCLSRULE:
2051 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002052 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002053 break;
2054 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002055 case ETHTOOL_SRXCLSRLDEL:
2056 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002057 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002058 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002059 case ETHTOOL_FLASHDEV:
2060 rc = ethtool_flash_device(dev, useraddr);
2061 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002062 case ETHTOOL_RESET:
2063 rc = ethtool_reset(dev, useraddr);
2064 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002065 case ETHTOOL_GSSET_INFO:
2066 rc = ethtool_get_sset_info(dev, useraddr);
2067 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002068 case ETHTOOL_GRXFHINDIR:
2069 rc = ethtool_get_rxfh_indir(dev, useraddr);
2070 break;
2071 case ETHTOOL_SRXFHINDIR:
2072 rc = ethtool_set_rxfh_indir(dev, useraddr);
2073 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302074 case ETHTOOL_GRSSH:
2075 rc = ethtool_get_rxfh(dev, useraddr);
2076 break;
2077 case ETHTOOL_SRSSH:
2078 rc = ethtool_set_rxfh(dev, useraddr);
2079 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002080 case ETHTOOL_GFEATURES:
2081 rc = ethtool_get_features(dev, useraddr);
2082 break;
2083 case ETHTOOL_SFEATURES:
2084 rc = ethtool_set_features(dev, useraddr);
2085 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002086 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002087 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002088 case ETHTOOL_GSG:
2089 case ETHTOOL_GTSO:
2090 case ETHTOOL_GUFO:
2091 case ETHTOOL_GGSO:
2092 case ETHTOOL_GGRO:
2093 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2094 break;
2095 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002096 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002097 case ETHTOOL_SSG:
2098 case ETHTOOL_STSO:
2099 case ETHTOOL_SUFO:
2100 case ETHTOOL_SGSO:
2101 case ETHTOOL_SGRO:
2102 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2103 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002104 case ETHTOOL_GCHANNELS:
2105 rc = ethtool_get_channels(dev, useraddr);
2106 break;
2107 case ETHTOOL_SCHANNELS:
2108 rc = ethtool_set_channels(dev, useraddr);
2109 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002110 case ETHTOOL_SET_DUMP:
2111 rc = ethtool_set_dump(dev, useraddr);
2112 break;
2113 case ETHTOOL_GET_DUMP_FLAG:
2114 rc = ethtool_get_dump_flag(dev, useraddr);
2115 break;
2116 case ETHTOOL_GET_DUMP_DATA:
2117 rc = ethtool_get_dump_data(dev, useraddr);
2118 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002119 case ETHTOOL_GET_TS_INFO:
2120 rc = ethtool_get_ts_info(dev, useraddr);
2121 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002122 case ETHTOOL_GMODULEINFO:
2123 rc = ethtool_get_module_info(dev, useraddr);
2124 break;
2125 case ETHTOOL_GMODULEEEPROM:
2126 rc = ethtool_get_module_eeprom(dev, useraddr);
2127 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302128 case ETHTOOL_GTUNABLE:
2129 rc = ethtool_get_tunable(dev, useraddr);
2130 break;
2131 case ETHTOOL_STUNABLE:
2132 rc = ethtool_set_tunable(dev, useraddr);
2133 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002134 case ETHTOOL_GPHYSTATS:
2135 rc = ethtool_get_phy_stats(dev, useraddr);
2136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002138 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002140
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002141 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002143
2144 if (old_features != dev->features)
2145 netdev_features_change(dev);
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}