blob: 1347e11f5cc9b0aa6ef8485cf44feaa1965a04e9 [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",
90 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
91 [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",
Rami Rosen65e92562015-02-20 21:34:58 +0200101 [NETIF_F_HW_SWITCH_OFFLOAD_BIT] = "hw-switch-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000102};
103
Eyal Perry892311f2014-12-02 18:12:10 +0200104static const char
105rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
106 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
107 [ETH_RSS_HASH_XOR_BIT] = "xor",
108};
109
Michał Mirosław5455c692011-02-15 16:59:17 +0000110static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
111{
112 struct ethtool_gfeatures cmd = {
113 .cmd = ETHTOOL_GFEATURES,
114 .size = ETHTOOL_DEV_FEATURE_WORDS,
115 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000116 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000117 u32 __user *sizeaddr;
118 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000119 int i;
120
121 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000122 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000123
124 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000125 features[i].available = (u32)(dev->hw_features >> (32 * i));
126 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
127 features[i].active = (u32)(dev->features >> (32 * i));
128 features[i].never_changed =
129 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000130 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000131
132 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
133 if (get_user(copy_size, sizeaddr))
134 return -EFAULT;
135
136 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
137 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
138
139 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
140 return -EFAULT;
141 useraddr += sizeof(cmd);
142 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
143 return -EFAULT;
144
145 return 0;
146}
147
148static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
149{
150 struct ethtool_sfeatures cmd;
151 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000152 netdev_features_t wanted = 0, valid = 0;
153 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000154
155 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
156 return -EFAULT;
157 useraddr += sizeof(cmd);
158
159 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
160 return -EINVAL;
161
162 if (copy_from_user(features, useraddr, sizeof(features)))
163 return -EFAULT;
164
Michał Mirosław475414f2011-11-15 15:29:55 +0000165 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000166 valid |= (netdev_features_t)features[i].valid << (32 * i);
167 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000168 }
169
170 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000171 return -EINVAL;
172
Michał Mirosław475414f2011-11-15 15:29:55 +0000173 if (valid & ~dev->hw_features) {
174 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000175 ret |= ETHTOOL_F_UNSUPPORTED;
176 }
177
Michał Mirosław475414f2011-11-15 15:29:55 +0000178 dev->wanted_features &= ~valid;
179 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700180 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000181
Michał Mirosław475414f2011-11-15 15:29:55 +0000182 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000183 ret |= ETHTOOL_F_WISH;
184
185 return ret;
186}
187
Michał Mirosław340ae162011-02-15 16:59:16 +0000188static int __ethtool_get_sset_count(struct net_device *dev, int sset)
189{
190 const struct ethtool_ops *ops = dev->ethtool_ops;
191
Michał Mirosław5455c692011-02-15 16:59:17 +0000192 if (sset == ETH_SS_FEATURES)
193 return ARRAY_SIZE(netdev_features_strings);
194
Eyal Perry892311f2014-12-02 18:12:10 +0200195 if (sset == ETH_SS_RSS_HASH_FUNCS)
196 return ARRAY_SIZE(rss_hash_func_strings);
197
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000198 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000199 return ops->get_sset_count(dev, sset);
200 else
201 return -EOPNOTSUPP;
202}
203
204static void __ethtool_get_strings(struct net_device *dev,
205 u32 stringset, u8 *data)
206{
207 const struct ethtool_ops *ops = dev->ethtool_ops;
208
Michał Mirosław5455c692011-02-15 16:59:17 +0000209 if (stringset == ETH_SS_FEATURES)
210 memcpy(data, netdev_features_strings,
211 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200212 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
213 memcpy(data, rss_hash_func_strings,
214 sizeof(rss_hash_func_strings));
Michał Mirosław5455c692011-02-15 16:59:17 +0000215 else
216 /* ops->get_strings is valid because checked earlier */
217 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000218}
219
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000220static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000221{
222 /* feature masks of legacy discrete ethtool ops */
223
224 switch (eth_cmd) {
225 case ETHTOOL_GTXCSUM:
226 case ETHTOOL_STXCSUM:
227 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000228 case ETHTOOL_GRXCSUM:
229 case ETHTOOL_SRXCSUM:
230 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000231 case ETHTOOL_GSG:
232 case ETHTOOL_SSG:
233 return NETIF_F_SG;
234 case ETHTOOL_GTSO:
235 case ETHTOOL_STSO:
236 return NETIF_F_ALL_TSO;
237 case ETHTOOL_GUFO:
238 case ETHTOOL_SUFO:
239 return NETIF_F_UFO;
240 case ETHTOOL_GGSO:
241 case ETHTOOL_SGSO:
242 return NETIF_F_GSO;
243 case ETHTOOL_GGRO:
244 case ETHTOOL_SGRO:
245 return NETIF_F_GRO;
246 default:
247 BUG();
248 }
249}
250
Michał Mirosław0a417702011-02-15 16:59:17 +0000251static int ethtool_get_one_feature(struct net_device *dev,
252 char __user *useraddr, u32 ethcmd)
253{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000254 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000255 struct ethtool_value edata = {
256 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000257 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000258 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000259
Michał Mirosław0a417702011-02-15 16:59:17 +0000260 if (copy_to_user(useraddr, &edata, sizeof(edata)))
261 return -EFAULT;
262 return 0;
263}
264
Michał Mirosław0a417702011-02-15 16:59:17 +0000265static int ethtool_set_one_feature(struct net_device *dev,
266 void __user *useraddr, u32 ethcmd)
267{
268 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000269 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000270
271 if (copy_from_user(&edata, useraddr, sizeof(edata)))
272 return -EFAULT;
273
Michał Mirosław86794882011-02-15 16:59:17 +0000274 mask = ethtool_get_feature_mask(ethcmd);
275 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000276 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000277 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000278
279 if (edata.data)
280 dev->wanted_features |= mask;
281 else
282 dev->wanted_features &= ~mask;
283
284 __netdev_update_features(dev);
285
286 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000287}
288
Michał Mirosław02b3a552011-11-15 15:29:55 +0000289#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
290 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000291#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
292 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
293 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000294
295static u32 __ethtool_get_flags(struct net_device *dev)
296{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000297 u32 flags = 0;
298
Dragos Foianu8a731252013-07-13 14:43:00 +0100299 if (dev->features & NETIF_F_LRO)
300 flags |= ETH_FLAG_LRO;
301 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
302 flags |= ETH_FLAG_RXVLAN;
303 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
304 flags |= ETH_FLAG_TXVLAN;
305 if (dev->features & NETIF_F_NTUPLE)
306 flags |= ETH_FLAG_NTUPLE;
307 if (dev->features & NETIF_F_RXHASH)
308 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000309
310 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000311}
312
313static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000314{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000315 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000316
Michał Mirosław02b3a552011-11-15 15:29:55 +0000317 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000318 return -EINVAL;
319
Dragos Foianu8a731252013-07-13 14:43:00 +0100320 if (data & ETH_FLAG_LRO)
321 features |= NETIF_F_LRO;
322 if (data & ETH_FLAG_RXVLAN)
323 features |= NETIF_F_HW_VLAN_CTAG_RX;
324 if (data & ETH_FLAG_TXVLAN)
325 features |= NETIF_F_HW_VLAN_CTAG_TX;
326 if (data & ETH_FLAG_NTUPLE)
327 features |= NETIF_F_NTUPLE;
328 if (data & ETH_FLAG_RXHASH)
329 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000330
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000331 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000332 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000333 if (changed & ~dev->hw_features)
334 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
335
336 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000337 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000338
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700339 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000340
341 return 0;
342}
343
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000344int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000346 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000348 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EOPNOTSUPP;
350
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000351 memset(cmd, 0, sizeof(struct ethtool_cmd));
352 cmd->cmd = ETHTOOL_GSET;
353 return dev->ethtool_ops->get_settings(dev, cmd);
354}
355EXPORT_SYMBOL(__ethtool_get_settings);
356
357static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
358{
359 int err;
360 struct ethtool_cmd cmd;
361
Arun Parameswaranf96dee12015-05-20 14:35:30 -0700362 if (!dev->ethtool_ops->get_settings)
363 return -EOPNOTSUPP;
364
365 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
366 return -EFAULT;
367
368 cmd.cmd = ETHTOOL_GSET;
369
370 err = dev->ethtool_ops->get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (err < 0)
372 return err;
373
374 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
375 return -EFAULT;
376 return 0;
377}
378
379static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
380{
381 struct ethtool_cmd cmd;
382
383 if (!dev->ethtool_ops->set_settings)
384 return -EOPNOTSUPP;
385
386 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
387 return -EFAULT;
388
389 return dev->ethtool_ops->set_settings(dev, &cmd);
390}
391
chavey97f8aef2010-04-07 21:54:42 -0700392static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
393 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700396 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 memset(&info, 0, sizeof(info));
399 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000400 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700401 ops->get_drvinfo(dev, &info);
402 } else if (dev->dev.parent && dev->dev.parent->driver) {
403 strlcpy(info.bus_info, dev_name(dev->dev.parent),
404 sizeof(info.bus_info));
405 strlcpy(info.driver, dev->dev.parent->driver->name,
406 sizeof(info.driver));
407 } else {
408 return -EOPNOTSUPP;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jeff Garzik723b2f52010-03-03 22:51:50 +0000411 /*
412 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000413 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000414 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000415 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700416 int rc;
417
418 rc = ops->get_sset_count(dev, ETH_SS_TEST);
419 if (rc >= 0)
420 info.testinfo_len = rc;
421 rc = ops->get_sset_count(dev, ETH_SS_STATS);
422 if (rc >= 0)
423 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700424 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
425 if (rc >= 0)
426 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700427 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000428 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000430 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 info.eedump_len = ops->get_eeprom_len(dev);
432
433 if (copy_to_user(useraddr, &info, sizeof(info)))
434 return -EFAULT;
435 return 0;
436}
437
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800438static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700439 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000440{
441 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000442 u64 sset_mask;
443 int i, idx = 0, n_bits = 0, ret, rc;
444 u32 *info_buf = NULL;
445
Jeff Garzik723b2f52010-03-03 22:51:50 +0000446 if (copy_from_user(&info, useraddr, sizeof(info)))
447 return -EFAULT;
448
449 /* store copy of mask, because we zero struct later on */
450 sset_mask = info.sset_mask;
451 if (!sset_mask)
452 return 0;
453
454 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000455 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000456
457 memset(&info, 0, sizeof(info));
458 info.cmd = ETHTOOL_GSSET_INFO;
459
460 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
461 if (!info_buf)
462 return -ENOMEM;
463
464 /*
465 * fill return buffer based on input bitmask and successful
466 * get_sset_count return
467 */
468 for (i = 0; i < 64; i++) {
469 if (!(sset_mask & (1ULL << i)))
470 continue;
471
Michał Mirosław340ae162011-02-15 16:59:16 +0000472 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000473 if (rc >= 0) {
474 info.sset_mask |= (1ULL << i);
475 info_buf[idx++] = rc;
476 }
477 }
478
479 ret = -EFAULT;
480 if (copy_to_user(useraddr, &info, sizeof(info)))
481 goto out;
482
483 useraddr += offsetof(struct ethtool_sset_info, data);
484 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
485 goto out;
486
487 ret = 0;
488
489out:
490 kfree(info_buf);
491 return ret;
492}
493
chavey97f8aef2010-04-07 21:54:42 -0700494static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000495 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700496{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000497 struct ethtool_rxnfc info;
498 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000499 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700500
Santwona Behera59089d82009-02-20 00:58:13 -0800501 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700502 return -EOPNOTSUPP;
503
Ben Hutchingsbf988432010-06-28 08:45:58 +0000504 /* struct ethtool_rxnfc was originally defined for
505 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
506 * members. User-space might still be using that
507 * definition. */
508 if (cmd == ETHTOOL_SRXFH)
509 info_size = (offsetof(struct ethtool_rxnfc, data) +
510 sizeof(info.data));
511
512 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700513 return -EFAULT;
514
Ben Hutchings55664f32012-01-03 12:04:51 +0000515 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
516 if (rc)
517 return rc;
518
519 if (cmd == ETHTOOL_SRXCLSRLINS &&
520 copy_to_user(useraddr, &info, info_size))
521 return -EFAULT;
522
523 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700524}
525
chavey97f8aef2010-04-07 21:54:42 -0700526static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000527 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700528{
529 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000530 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800531 const struct ethtool_ops *ops = dev->ethtool_ops;
532 int ret;
533 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700534
Santwona Behera59089d82009-02-20 00:58:13 -0800535 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700536 return -EOPNOTSUPP;
537
Ben Hutchingsbf988432010-06-28 08:45:58 +0000538 /* struct ethtool_rxnfc was originally defined for
539 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
540 * members. User-space might still be using that
541 * definition. */
542 if (cmd == ETHTOOL_GRXFH)
543 info_size = (offsetof(struct ethtool_rxnfc, data) +
544 sizeof(info.data));
545
546 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700547 return -EFAULT;
548
Santwona Behera59089d82009-02-20 00:58:13 -0800549 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
550 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000551 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000552 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000553 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800554 if (!rule_buf)
555 return -ENOMEM;
556 }
557 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700558
Santwona Behera59089d82009-02-20 00:58:13 -0800559 ret = ops->get_rxnfc(dev, &info, rule_buf);
560 if (ret < 0)
561 goto err_out;
562
563 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000564 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800565 goto err_out;
566
567 if (rule_buf) {
568 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
569 if (copy_to_user(useraddr, rule_buf,
570 info.rule_cnt * sizeof(u32)))
571 goto err_out;
572 }
573 ret = 0;
574
575err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700576 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800577
578 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700579}
580
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530581static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
582 struct ethtool_rxnfc *rx_rings,
583 u32 size)
584{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100585 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530586
587 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100588 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530589
590 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +0100591 for (i = 0; i < size; i++)
592 if (indir[i] >= rx_rings->data)
593 return -EINVAL;
594
595 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530596}
597
Eric Dumazet960fb622014-11-16 06:23:05 -0800598u8 netdev_rss_key[NETDEV_RSS_KEY_LEN];
599
600void netdev_rss_key_fill(void *buffer, size_t len)
601{
602 BUG_ON(len > sizeof(netdev_rss_key));
603 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
604 memcpy(buffer, netdev_rss_key, len);
605}
606EXPORT_SYMBOL(netdev_rss_key_fill);
607
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000608static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
609 void __user *useraddr)
610{
Ben Hutchings7850f632011-12-15 13:55:01 +0000611 u32 user_size, dev_size;
612 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000613 int ret;
614
Ben Hutchings7850f632011-12-15 13:55:01 +0000615 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100616 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +0000617 return -EOPNOTSUPP;
618 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
619 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000620 return -EOPNOTSUPP;
621
Ben Hutchings7850f632011-12-15 13:55:01 +0000622 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000623 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000624 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000625 return -EFAULT;
626
Ben Hutchings7850f632011-12-15 13:55:01 +0000627 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
628 &dev_size, sizeof(dev_size)))
629 return -EFAULT;
630
631 /* If the user buffer size is 0, this is just a query for the
632 * device table size. Otherwise, if it's smaller than the
633 * device table size it's an error.
634 */
635 if (user_size < dev_size)
636 return user_size == 0 ? 0 : -EINVAL;
637
638 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000639 if (!indir)
640 return -ENOMEM;
641
Eyal Perry892311f2014-12-02 18:12:10 +0200642 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000643 if (ret)
644 goto out;
645
Ben Hutchings7850f632011-12-15 13:55:01 +0000646 if (copy_to_user(useraddr +
647 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
648 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000649 ret = -EFAULT;
650
651out:
652 kfree(indir);
653 return ret;
654}
655
656static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
657 void __user *useraddr)
658{
Ben Hutchings7850f632011-12-15 13:55:01 +0000659 struct ethtool_rxnfc rx_rings;
660 u32 user_size, dev_size, i;
661 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000662 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000663 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530664 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000665
Ben Hutchingsfe62d002014-05-15 01:25:27 +0100666 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000667 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000668 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000669
670 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000671 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000672 return -EOPNOTSUPP;
673
Ben Hutchings7850f632011-12-15 13:55:01 +0000674 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000675 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000676 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000677 return -EFAULT;
678
Ben Hutchings278bc422011-12-15 13:56:49 +0000679 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000680 return -EINVAL;
681
682 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000683 if (!indir)
684 return -ENOMEM;
685
Ben Hutchings7850f632011-12-15 13:55:01 +0000686 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000687 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000688 if (ret)
689 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000690
691 if (user_size == 0) {
692 for (i = 0; i < dev_size; i++)
693 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
694 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530695 ret = ethtool_copy_validate_indir(indir,
696 useraddr + ringidx_offset,
697 &rx_rings,
698 dev_size);
699 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +0000700 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +0000701 }
702
Eyal Perry892311f2014-12-02 18:12:10 +0200703 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000704
705out:
706 kfree(indir);
707 return ret;
708}
709
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530710static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
711 void __user *useraddr)
712{
713 int ret;
714 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100715 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530716 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100717 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530718 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100719 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530720 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +0200721 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530722 u8 *hkey = NULL;
723 u8 *rss_config;
724
Eyal Perry892311f2014-12-02 18:12:10 +0200725 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530726 return -EOPNOTSUPP;
727
728 if (ops->get_rxfh_indir_size)
729 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530730 if (ops->get_rxfh_key_size)
731 dev_key_size = ops->get_rxfh_key_size(dev);
732
Ben Hutchingsf062a382014-05-15 16:28:07 +0100733 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530734 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100735 user_indir_size = rxfh.indir_size;
736 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530737
Ben Hutchingsf062a382014-05-15 16:28:07 +0100738 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +0200739 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
740 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +0100741 return -EINVAL;
742
743 rxfh.indir_size = dev_indir_size;
744 rxfh.key_size = dev_key_size;
745 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530746 return -EFAULT;
747
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530748 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
749 (user_key_size && (user_key_size != dev_key_size)))
750 return -EINVAL;
751
752 indir_bytes = user_indir_size * sizeof(indir[0]);
753 total_size = indir_bytes + user_key_size;
754 rss_config = kzalloc(total_size, GFP_USER);
755 if (!rss_config)
756 return -ENOMEM;
757
758 if (user_indir_size)
759 indir = (u32 *)rss_config;
760
761 if (user_key_size)
762 hkey = rss_config + indir_bytes;
763
Eyal Perry892311f2014-12-02 18:12:10 +0200764 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
765 if (ret)
766 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530767
Eyal Perry892311f2014-12-02 18:12:10 +0200768 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
769 &dev_hfunc, sizeof(rxfh.hfunc))) {
770 ret = -EFAULT;
771 } else if (copy_to_user(useraddr +
772 offsetof(struct ethtool_rxfh, rss_config[0]),
773 rss_config, total_size)) {
774 ret = -EFAULT;
775 }
776out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530777 kfree(rss_config);
778
779 return ret;
780}
781
782static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
783 void __user *useraddr)
784{
785 int ret;
786 const struct ethtool_ops *ops = dev->ethtool_ops;
787 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100788 struct ethtool_rxfh rxfh;
789 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530790 u32 *indir = NULL, indir_bytes = 0;
791 u8 *hkey = NULL;
792 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530793 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
794
Eyal Perry892311f2014-12-02 18:12:10 +0200795 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530796 return -EOPNOTSUPP;
797
798 if (ops->get_rxfh_indir_size)
799 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530800 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +0300801 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530802
Ben Hutchingsf062a382014-05-15 16:28:07 +0100803 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530804 return -EFAULT;
805
Ben Hutchingsf062a382014-05-15 16:28:07 +0100806 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +0200807 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
808 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +0100809 return -EINVAL;
810
Eyal Perry892311f2014-12-02 18:12:10 +0200811 /* If either indir, hash key or function is valid, proceed further.
812 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530813 */
Ben Hutchingsf062a382014-05-15 16:28:07 +0100814 if ((rxfh.indir_size &&
815 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
816 rxfh.indir_size != dev_indir_size) ||
817 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
818 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +0200819 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530820 return -EINVAL;
821
Ben Hutchingsf062a382014-05-15 16:28:07 +0100822 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530823 indir_bytes = dev_indir_size * sizeof(indir[0]);
824
Ben Hutchingsf062a382014-05-15 16:28:07 +0100825 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530826 if (!rss_config)
827 return -ENOMEM;
828
829 rx_rings.cmd = ETHTOOL_GRXRINGS;
830 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
831 if (ret)
832 goto out;
833
Ben Hutchingsf062a382014-05-15 16:28:07 +0100834 /* rxfh.indir_size == 0 means reset the indir table to default.
835 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530836 */
Ben Hutchingsf062a382014-05-15 16:28:07 +0100837 if (rxfh.indir_size &&
838 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530839 indir = (u32 *)rss_config;
840 ret = ethtool_copy_validate_indir(indir,
841 useraddr + rss_cfg_offset,
842 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +0100843 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530844 if (ret)
845 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +0100846 } else if (rxfh.indir_size == 0) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530847 indir = (u32 *)rss_config;
848 for (i = 0; i < dev_indir_size; i++)
849 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
850 }
851
Ben Hutchingsf062a382014-05-15 16:28:07 +0100852 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530853 hkey = rss_config + indir_bytes;
854 if (copy_from_user(hkey,
855 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +0100856 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530857 ret = -EFAULT;
858 goto out;
859 }
860 }
861
Eyal Perry892311f2014-12-02 18:12:10 +0200862 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +0530863
864out:
865 kfree(rss_config);
866 return ret;
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
870{
871 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700872 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 void *regbuf;
874 int reglen, ret;
875
876 if (!ops->get_regs || !ops->get_regs_len)
877 return -EOPNOTSUPP;
878
879 if (copy_from_user(&regs, useraddr, sizeof(regs)))
880 return -EFAULT;
881
882 reglen = ops->get_regs_len(dev);
883 if (regs.len > reglen)
884 regs.len = reglen;
885
Eugene Teob7c7d012011-01-24 21:05:17 -0800886 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700887 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -ENOMEM;
889
890 ops->get_regs(dev, &regs, regbuf);
891
892 ret = -EFAULT;
893 if (copy_to_user(useraddr, &regs, sizeof(regs)))
894 goto out;
895 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700896 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto out;
898 ret = 0;
899
900 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000901 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return ret;
903}
904
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000905static int ethtool_reset(struct net_device *dev, char __user *useraddr)
906{
907 struct ethtool_value reset;
908 int ret;
909
910 if (!dev->ethtool_ops->reset)
911 return -EOPNOTSUPP;
912
913 if (copy_from_user(&reset, useraddr, sizeof(reset)))
914 return -EFAULT;
915
916 ret = dev->ethtool_ops->reset(dev, &reset.data);
917 if (ret)
918 return ret;
919
920 if (copy_to_user(useraddr, &reset, sizeof(reset)))
921 return -EFAULT;
922 return 0;
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
926{
Roland Dreier8e557422010-02-11 12:14:23 -0800927 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (!dev->ethtool_ops->get_wol)
930 return -EOPNOTSUPP;
931
932 dev->ethtool_ops->get_wol(dev, &wol);
933
934 if (copy_to_user(useraddr, &wol, sizeof(wol)))
935 return -EFAULT;
936 return 0;
937}
938
939static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
940{
941 struct ethtool_wolinfo wol;
942
943 if (!dev->ethtool_ops->set_wol)
944 return -EOPNOTSUPP;
945
946 if (copy_from_user(&wol, useraddr, sizeof(wol)))
947 return -EFAULT;
948
949 return dev->ethtool_ops->set_wol(dev, &wol);
950}
951
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000952static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
953{
954 struct ethtool_eee edata;
955 int rc;
956
957 if (!dev->ethtool_ops->get_eee)
958 return -EOPNOTSUPP;
959
960 memset(&edata, 0, sizeof(struct ethtool_eee));
961 edata.cmd = ETHTOOL_GEEE;
962 rc = dev->ethtool_ops->get_eee(dev, &edata);
963
964 if (rc)
965 return rc;
966
967 if (copy_to_user(useraddr, &edata, sizeof(edata)))
968 return -EFAULT;
969
970 return 0;
971}
972
973static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
974{
975 struct ethtool_eee edata;
976
977 if (!dev->ethtool_ops->set_eee)
978 return -EOPNOTSUPP;
979
980 if (copy_from_user(&edata, useraddr, sizeof(edata)))
981 return -EFAULT;
982
983 return dev->ethtool_ops->set_eee(dev, &edata);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static int ethtool_nway_reset(struct net_device *dev)
987{
988 if (!dev->ethtool_ops->nway_reset)
989 return -EOPNOTSUPP;
990
991 return dev->ethtool_ops->nway_reset(dev);
992}
993
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000994static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
995{
996 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
997
998 if (!dev->ethtool_ops->get_link)
999 return -EOPNOTSUPP;
1000
1001 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1002
1003 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1004 return -EFAULT;
1005 return 0;
1006}
1007
Ben Hutchings081d0942012-04-12 00:42:12 +00001008static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1009 int (*getter)(struct net_device *,
1010 struct ethtool_eeprom *, u8 *),
1011 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001014 void __user *userbuf = useraddr + sizeof(eeprom);
1015 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001017 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1020 return -EFAULT;
1021
1022 /* Check for wrap and zero */
1023 if (eeprom.offset + eeprom.len <= eeprom.offset)
1024 return -EINVAL;
1025
1026 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001027 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -EINVAL;
1029
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001030 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!data)
1032 return -ENOMEM;
1033
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001034 bytes_remaining = eeprom.len;
1035 while (bytes_remaining > 0) {
1036 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Ben Hutchings081d0942012-04-12 00:42:12 +00001038 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001039 if (ret)
1040 break;
1041 if (copy_to_user(userbuf, data, eeprom.len)) {
1042 ret = -EFAULT;
1043 break;
1044 }
1045 userbuf += eeprom.len;
1046 eeprom.offset += eeprom.len;
1047 bytes_remaining -= eeprom.len;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001050 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1051 eeprom.offset -= eeprom.len;
1052 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1053 ret = -EFAULT;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 kfree(data);
1056 return ret;
1057}
1058
Ben Hutchings081d0942012-04-12 00:42:12 +00001059static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1060{
1061 const struct ethtool_ops *ops = dev->ethtool_ops;
1062
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001063 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1064 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001065 return -EOPNOTSUPP;
1066
1067 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1068 ops->get_eeprom_len(dev));
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1072{
1073 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001074 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001075 void __user *userbuf = useraddr + sizeof(eeprom);
1076 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001078 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001080 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1081 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EOPNOTSUPP;
1083
1084 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1085 return -EFAULT;
1086
1087 /* Check for wrap and zero */
1088 if (eeprom.offset + eeprom.len <= eeprom.offset)
1089 return -EINVAL;
1090
1091 /* Check for exceeding total eeprom len */
1092 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1093 return -EINVAL;
1094
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001095 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!data)
1097 return -ENOMEM;
1098
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001099 bytes_remaining = eeprom.len;
1100 while (bytes_remaining > 0) {
1101 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001103 if (copy_from_user(data, userbuf, eeprom.len)) {
1104 ret = -EFAULT;
1105 break;
1106 }
1107 ret = ops->set_eeprom(dev, &eeprom, data);
1108 if (ret)
1109 break;
1110 userbuf += eeprom.len;
1111 eeprom.offset += eeprom.len;
1112 bytes_remaining -= eeprom.len;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 kfree(data);
1116 return ret;
1117}
1118
chavey97f8aef2010-04-07 21:54:42 -07001119static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1120 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Roland Dreier8e557422010-02-11 12:14:23 -08001122 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (!dev->ethtool_ops->get_coalesce)
1125 return -EOPNOTSUPP;
1126
1127 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1128
1129 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1130 return -EFAULT;
1131 return 0;
1132}
1133
chavey97f8aef2010-04-07 21:54:42 -07001134static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1135 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct ethtool_coalesce coalesce;
1138
David S. Millerfa04ae52005-06-06 15:07:19 -07001139 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return -EOPNOTSUPP;
1141
1142 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1143 return -EFAULT;
1144
1145 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1146}
1147
1148static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1149{
Roland Dreier8e557422010-02-11 12:14:23 -08001150 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (!dev->ethtool_ops->get_ringparam)
1153 return -EOPNOTSUPP;
1154
1155 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1156
1157 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1158 return -EFAULT;
1159 return 0;
1160}
1161
1162static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1163{
1164 struct ethtool_ringparam ringparam;
1165
1166 if (!dev->ethtool_ops->set_ringparam)
1167 return -EOPNOTSUPP;
1168
1169 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1170 return -EFAULT;
1171
1172 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1173}
1174
amit salecha8b5933c2011-04-07 01:58:42 +00001175static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1176 void __user *useraddr)
1177{
1178 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1179
1180 if (!dev->ethtool_ops->get_channels)
1181 return -EOPNOTSUPP;
1182
1183 dev->ethtool_ops->get_channels(dev, &channels);
1184
1185 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1186 return -EFAULT;
1187 return 0;
1188}
1189
1190static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1191 void __user *useraddr)
1192{
1193 struct ethtool_channels channels;
1194
1195 if (!dev->ethtool_ops->set_channels)
1196 return -EOPNOTSUPP;
1197
1198 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1199 return -EFAULT;
1200
1201 return dev->ethtool_ops->set_channels(dev, &channels);
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1205{
1206 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1207
1208 if (!dev->ethtool_ops->get_pauseparam)
1209 return -EOPNOTSUPP;
1210
1211 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1212
1213 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1214 return -EFAULT;
1215 return 0;
1216}
1217
1218static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1219{
1220 struct ethtool_pauseparam pauseparam;
1221
Jeff Garzike1b90c42006-07-17 12:54:40 -04001222 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EOPNOTSUPP;
1224
1225 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1226 return -EFAULT;
1227
1228 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1232{
1233 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001234 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001236 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001238 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001239 return -EOPNOTSUPP;
1240
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001241 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001242 if (test_len < 0)
1243 return test_len;
1244 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (copy_from_user(&test, useraddr, sizeof(test)))
1247 return -EFAULT;
1248
Jeff Garzikff03d492007-08-15 16:01:08 -07001249 test.len = test_len;
1250 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (!data)
1252 return -ENOMEM;
1253
1254 ops->self_test(dev, &test, data);
1255
1256 ret = -EFAULT;
1257 if (copy_to_user(useraddr, &test, sizeof(test)))
1258 goto out;
1259 useraddr += sizeof(test);
1260 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1261 goto out;
1262 ret = 0;
1263
1264 out:
1265 kfree(data);
1266 return ret;
1267}
1268
1269static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1270{
1271 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 u8 *data;
1273 int ret;
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1276 return -EFAULT;
1277
Michał Mirosław340ae162011-02-15 16:59:16 +00001278 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001279 if (ret < 0)
1280 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001281
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001282 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1285 if (!data)
1286 return -ENOMEM;
1287
Michał Mirosław340ae162011-02-15 16:59:16 +00001288 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 ret = -EFAULT;
1291 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1292 goto out;
1293 useraddr += sizeof(gstrings);
1294 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1295 goto out;
1296 ret = 0;
1297
Michał Mirosław340ae162011-02-15 16:59:16 +00001298out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 kfree(data);
1300 return ret;
1301}
1302
1303static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1304{
1305 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001306 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001307 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001308 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001310 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EOPNOTSUPP;
1312
Ben Hutchings68f512f2011-04-02 00:35:15 +01001313 if (busy)
1314 return -EBUSY;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (copy_from_user(&id, useraddr, sizeof(id)))
1317 return -EFAULT;
1318
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001319 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001320 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001321 return rc;
1322
1323 /* Drop the RTNL lock while waiting, but prevent reentry or
1324 * removal of the device.
1325 */
1326 busy = true;
1327 dev_hold(dev);
1328 rtnl_unlock();
1329
1330 if (rc == 0) {
1331 /* Driver will handle this itself */
1332 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001333 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001334 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001335 /* Driver expects to be called at twice the frequency in rc */
1336 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001337
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001338 /* Count down seconds */
1339 do {
1340 /* Count down iterations per second */
1341 i = n;
1342 do {
1343 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001344 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001345 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1346 rtnl_unlock();
1347 if (rc)
1348 break;
1349 schedule_timeout_interruptible(interval);
1350 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001351 } while (!signal_pending(current) &&
1352 (id.data == 0 || --id.data != 0));
1353 }
1354
1355 rtnl_lock();
1356 dev_put(dev);
1357 busy = false;
1358
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001359 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001360 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1364{
1365 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001366 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001368 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001370 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001371 return -EOPNOTSUPP;
1372
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001373 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001374 if (n_stats < 0)
1375 return n_stats;
1376 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1379 return -EFAULT;
1380
Jeff Garzikff03d492007-08-15 16:01:08 -07001381 stats.n_stats = n_stats;
1382 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!data)
1384 return -ENOMEM;
1385
1386 ops->get_ethtool_stats(dev, &stats, data);
1387
1388 ret = -EFAULT;
1389 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1390 goto out;
1391 useraddr += sizeof(stats);
1392 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1393 goto out;
1394 ret = 0;
1395
1396 out:
1397 kfree(data);
1398 return ret;
1399}
1400
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001401static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001402{
1403 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001404
Matthew Wilcox313674a2007-07-31 14:00:29 -07001405 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001406 return -EFAULT;
1407
Matthew Wilcox313674a2007-07-31 14:00:29 -07001408 if (epaddr.size < dev->addr_len)
1409 return -ETOOSMALL;
1410 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001411
Jon Wetzela6f9a702005-08-20 17:15:54 -07001412 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001413 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001414 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001415 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1416 return -EFAULT;
1417 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001418}
1419
Jeff Garzik13c99b22007-08-15 16:01:56 -07001420static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1421 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001422{
Roland Dreier8e557422010-02-11 12:14:23 -08001423 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001424
Jeff Garzik13c99b22007-08-15 16:01:56 -07001425 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001426 return -EOPNOTSUPP;
1427
Jeff Garzik13c99b22007-08-15 16:01:56 -07001428 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001429
1430 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1431 return -EFAULT;
1432 return 0;
1433}
1434
Jeff Garzik13c99b22007-08-15 16:01:56 -07001435static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1436 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001437{
1438 struct ethtool_value edata;
1439
Jeff Garzik13c99b22007-08-15 16:01:56 -07001440 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001441 return -EOPNOTSUPP;
1442
1443 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1444 return -EFAULT;
1445
Jeff Garzik13c99b22007-08-15 16:01:56 -07001446 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001447 return 0;
1448}
1449
Jeff Garzik13c99b22007-08-15 16:01:56 -07001450static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1451 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001452{
1453 struct ethtool_value edata;
1454
Jeff Garzik13c99b22007-08-15 16:01:56 -07001455 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001456 return -EOPNOTSUPP;
1457
1458 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1459 return -EFAULT;
1460
Jeff Garzik13c99b22007-08-15 16:01:56 -07001461 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001462}
1463
chavey97f8aef2010-04-07 21:54:42 -07001464static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1465 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001466{
1467 struct ethtool_flash efl;
1468
1469 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1470 return -EFAULT;
1471
1472 if (!dev->ethtool_ops->flash_device)
1473 return -EOPNOTSUPP;
1474
Ben Hutchings786f5282012-02-01 09:32:25 +00001475 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1476
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001477 return dev->ethtool_ops->flash_device(dev, &efl);
1478}
1479
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001480static int ethtool_set_dump(struct net_device *dev,
1481 void __user *useraddr)
1482{
1483 struct ethtool_dump dump;
1484
1485 if (!dev->ethtool_ops->set_dump)
1486 return -EOPNOTSUPP;
1487
1488 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1489 return -EFAULT;
1490
1491 return dev->ethtool_ops->set_dump(dev, &dump);
1492}
1493
1494static int ethtool_get_dump_flag(struct net_device *dev,
1495 void __user *useraddr)
1496{
1497 int ret;
1498 struct ethtool_dump dump;
1499 const struct ethtool_ops *ops = dev->ethtool_ops;
1500
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001501 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001502 return -EOPNOTSUPP;
1503
1504 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1505 return -EFAULT;
1506
1507 ret = ops->get_dump_flag(dev, &dump);
1508 if (ret)
1509 return ret;
1510
1511 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1512 return -EFAULT;
1513 return 0;
1514}
1515
1516static int ethtool_get_dump_data(struct net_device *dev,
1517 void __user *useraddr)
1518{
1519 int ret;
1520 __u32 len;
1521 struct ethtool_dump dump, tmp;
1522 const struct ethtool_ops *ops = dev->ethtool_ops;
1523 void *data = NULL;
1524
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001525 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001526 return -EOPNOTSUPP;
1527
1528 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1529 return -EFAULT;
1530
1531 memset(&tmp, 0, sizeof(tmp));
1532 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1533 ret = ops->get_dump_flag(dev, &tmp);
1534 if (ret)
1535 return ret;
1536
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001537 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001538 if (!len)
1539 return -EFAULT;
1540
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001541 /* Don't ever let the driver think there's more space available
1542 * than it requested with .get_dump_flag().
1543 */
1544 dump.len = len;
1545
1546 /* Always allocate enough space to hold the whole thing so that the
1547 * driver does not need to check the length and bother with partial
1548 * dumping.
1549 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001550 data = vzalloc(tmp.len);
1551 if (!data)
1552 return -ENOMEM;
1553 ret = ops->get_dump_data(dev, &dump, data);
1554 if (ret)
1555 goto out;
1556
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001557 /* There are two sane possibilities:
1558 * 1. The driver's .get_dump_data() does not touch dump.len.
1559 * 2. Or it may set dump.len to how much it really writes, which
1560 * should be tmp.len (or len if it can do a partial dump).
1561 * In any case respond to userspace with the actual length of data
1562 * it's receiving.
1563 */
1564 WARN_ON(dump.len != len && dump.len != tmp.len);
1565 dump.len = len;
1566
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001567 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1568 ret = -EFAULT;
1569 goto out;
1570 }
1571 useraddr += offsetof(struct ethtool_dump, data);
1572 if (copy_to_user(useraddr, data, len))
1573 ret = -EFAULT;
1574out:
1575 vfree(data);
1576 return ret;
1577}
1578
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001579static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1580{
1581 int err = 0;
1582 struct ethtool_ts_info info;
1583 const struct ethtool_ops *ops = dev->ethtool_ops;
1584 struct phy_device *phydev = dev->phydev;
1585
1586 memset(&info, 0, sizeof(info));
1587 info.cmd = ETHTOOL_GET_TS_INFO;
1588
1589 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001590 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001591 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001592 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001593 } else {
1594 info.so_timestamping =
1595 SOF_TIMESTAMPING_RX_SOFTWARE |
1596 SOF_TIMESTAMPING_SOFTWARE;
1597 info.phc_index = -1;
1598 }
1599
1600 if (err)
1601 return err;
1602
1603 if (copy_to_user(useraddr, &info, sizeof(info)))
1604 err = -EFAULT;
1605
1606 return err;
1607}
1608
Ed Swierk2f438362015-01-02 17:27:56 -08001609static int __ethtool_get_module_info(struct net_device *dev,
1610 struct ethtool_modinfo *modinfo)
1611{
1612 const struct ethtool_ops *ops = dev->ethtool_ops;
1613 struct phy_device *phydev = dev->phydev;
1614
1615 if (phydev && phydev->drv && phydev->drv->module_info)
1616 return phydev->drv->module_info(phydev, modinfo);
1617
1618 if (ops->get_module_info)
1619 return ops->get_module_info(dev, modinfo);
1620
1621 return -EOPNOTSUPP;
1622}
1623
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001624static int ethtool_get_module_info(struct net_device *dev,
1625 void __user *useraddr)
1626{
1627 int ret;
1628 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001629
1630 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1631 return -EFAULT;
1632
Ed Swierk2f438362015-01-02 17:27:56 -08001633 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001634 if (ret)
1635 return ret;
1636
1637 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1638 return -EFAULT;
1639
1640 return 0;
1641}
1642
Ed Swierk2f438362015-01-02 17:27:56 -08001643static int __ethtool_get_module_eeprom(struct net_device *dev,
1644 struct ethtool_eeprom *ee, u8 *data)
1645{
1646 const struct ethtool_ops *ops = dev->ethtool_ops;
1647 struct phy_device *phydev = dev->phydev;
1648
1649 if (phydev && phydev->drv && phydev->drv->module_eeprom)
1650 return phydev->drv->module_eeprom(phydev, ee, data);
1651
1652 if (ops->get_module_eeprom)
1653 return ops->get_module_eeprom(dev, ee, data);
1654
1655 return -EOPNOTSUPP;
1656}
1657
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001658static int ethtool_get_module_eeprom(struct net_device *dev,
1659 void __user *useraddr)
1660{
1661 int ret;
1662 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001663
Ed Swierk2f438362015-01-02 17:27:56 -08001664 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001665 if (ret)
1666 return ret;
1667
Ed Swierk2f438362015-01-02 17:27:56 -08001668 return ethtool_get_any_eeprom(dev, useraddr,
1669 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001670 modinfo.eeprom_len);
1671}
1672
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301673static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
1674{
1675 switch (tuna->id) {
1676 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03001677 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301678 if (tuna->len != sizeof(u32) ||
1679 tuna->type_id != ETHTOOL_TUNABLE_U32)
1680 return -EINVAL;
1681 break;
1682 default:
1683 return -EINVAL;
1684 }
1685
1686 return 0;
1687}
1688
1689static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
1690{
1691 int ret;
1692 struct ethtool_tunable tuna;
1693 const struct ethtool_ops *ops = dev->ethtool_ops;
1694 void *data;
1695
1696 if (!ops->get_tunable)
1697 return -EOPNOTSUPP;
1698 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1699 return -EFAULT;
1700 ret = ethtool_tunable_valid(&tuna);
1701 if (ret)
1702 return ret;
1703 data = kmalloc(tuna.len, GFP_USER);
1704 if (!data)
1705 return -ENOMEM;
1706 ret = ops->get_tunable(dev, &tuna, data);
1707 if (ret)
1708 goto out;
1709 useraddr += sizeof(tuna);
1710 ret = -EFAULT;
1711 if (copy_to_user(useraddr, data, tuna.len))
1712 goto out;
1713 ret = 0;
1714
1715out:
1716 kfree(data);
1717 return ret;
1718}
1719
1720static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
1721{
1722 int ret;
1723 struct ethtool_tunable tuna;
1724 const struct ethtool_ops *ops = dev->ethtool_ops;
1725 void *data;
1726
1727 if (!ops->set_tunable)
1728 return -EOPNOTSUPP;
1729 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1730 return -EFAULT;
1731 ret = ethtool_tunable_valid(&tuna);
1732 if (ret)
1733 return ret;
1734 data = kmalloc(tuna.len, GFP_USER);
1735 if (!data)
1736 return -ENOMEM;
1737 useraddr += sizeof(tuna);
1738 ret = -EFAULT;
1739 if (copy_from_user(data, useraddr, tuna.len))
1740 goto out;
1741 ret = ops->set_tunable(dev, &tuna, data);
1742
1743out:
1744 kfree(data);
1745 return ret;
1746}
1747
Yan Burman600fed52013-06-03 02:03:34 +00001748/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Eric W. Biederman881d9662007-09-17 11:56:21 -07001750int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001752 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 void __user *useraddr = ifr->ifr_data;
1754 u32 ethcmd;
1755 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00001756 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (!dev || !netif_device_present(dev))
1759 return -ENODEV;
1760
chavey97f8aef2010-04-07 21:54:42 -07001761 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return -EFAULT;
1763
Stephen Hemminger75f31232006-09-28 15:13:37 -07001764 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001765 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001766 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001767 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001768 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001769 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001770 case ETHTOOL_GCOALESCE:
1771 case ETHTOOL_GRINGPARAM:
1772 case ETHTOOL_GPAUSEPARAM:
1773 case ETHTOOL_GRXCSUM:
1774 case ETHTOOL_GTXCSUM:
1775 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001776 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001777 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001778 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001779 case ETHTOOL_GTSO:
1780 case ETHTOOL_GPERMADDR:
1781 case ETHTOOL_GUFO:
1782 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001783 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001784 case ETHTOOL_GFLAGS:
1785 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001786 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001787 case ETHTOOL_GRXRINGS:
1788 case ETHTOOL_GRXCLSRLCNT:
1789 case ETHTOOL_GRXCLSRULE:
1790 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001791 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301792 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00001793 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001794 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001795 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001796 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301797 case ETHTOOL_GTUNABLE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001798 break;
1799 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001800 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001801 return -EPERM;
1802 }
1803
chavey97f8aef2010-04-07 21:54:42 -07001804 if (dev->ethtool_ops->begin) {
1805 rc = dev->ethtool_ops->begin(dev);
1806 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001808 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001809 old_features = dev->features;
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 switch (ethcmd) {
1812 case ETHTOOL_GSET:
1813 rc = ethtool_get_settings(dev, useraddr);
1814 break;
1815 case ETHTOOL_SSET:
1816 rc = ethtool_set_settings(dev, useraddr);
1817 break;
1818 case ETHTOOL_GDRVINFO:
1819 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 case ETHTOOL_GREGS:
1822 rc = ethtool_get_regs(dev, useraddr);
1823 break;
1824 case ETHTOOL_GWOL:
1825 rc = ethtool_get_wol(dev, useraddr);
1826 break;
1827 case ETHTOOL_SWOL:
1828 rc = ethtool_set_wol(dev, useraddr);
1829 break;
1830 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001831 rc = ethtool_get_value(dev, useraddr, ethcmd,
1832 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 break;
1834 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001835 rc = ethtool_set_value_void(dev, useraddr,
1836 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001838 case ETHTOOL_GEEE:
1839 rc = ethtool_get_eee(dev, useraddr);
1840 break;
1841 case ETHTOOL_SEEE:
1842 rc = ethtool_set_eee(dev, useraddr);
1843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 case ETHTOOL_NWAY_RST:
1845 rc = ethtool_nway_reset(dev);
1846 break;
1847 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001848 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850 case ETHTOOL_GEEPROM:
1851 rc = ethtool_get_eeprom(dev, useraddr);
1852 break;
1853 case ETHTOOL_SEEPROM:
1854 rc = ethtool_set_eeprom(dev, useraddr);
1855 break;
1856 case ETHTOOL_GCOALESCE:
1857 rc = ethtool_get_coalesce(dev, useraddr);
1858 break;
1859 case ETHTOOL_SCOALESCE:
1860 rc = ethtool_set_coalesce(dev, useraddr);
1861 break;
1862 case ETHTOOL_GRINGPARAM:
1863 rc = ethtool_get_ringparam(dev, useraddr);
1864 break;
1865 case ETHTOOL_SRINGPARAM:
1866 rc = ethtool_set_ringparam(dev, useraddr);
1867 break;
1868 case ETHTOOL_GPAUSEPARAM:
1869 rc = ethtool_get_pauseparam(dev, useraddr);
1870 break;
1871 case ETHTOOL_SPAUSEPARAM:
1872 rc = ethtool_set_pauseparam(dev, useraddr);
1873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 case ETHTOOL_TEST:
1875 rc = ethtool_self_test(dev, useraddr);
1876 break;
1877 case ETHTOOL_GSTRINGS:
1878 rc = ethtool_get_strings(dev, useraddr);
1879 break;
1880 case ETHTOOL_PHYS_ID:
1881 rc = ethtool_phys_id(dev, useraddr);
1882 break;
1883 case ETHTOOL_GSTATS:
1884 rc = ethtool_get_stats(dev, useraddr);
1885 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001886 case ETHTOOL_GPERMADDR:
1887 rc = ethtool_get_perm_addr(dev, useraddr);
1888 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001889 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001890 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001891 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001892 break;
1893 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001894 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001895 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001896 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001897 rc = ethtool_get_value(dev, useraddr, ethcmd,
1898 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001899 break;
1900 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001901 rc = ethtool_set_value(dev, useraddr,
1902 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001903 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001904 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001905 case ETHTOOL_GRXRINGS:
1906 case ETHTOOL_GRXCLSRLCNT:
1907 case ETHTOOL_GRXCLSRULE:
1908 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001909 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001910 break;
1911 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001912 case ETHTOOL_SRXCLSRLDEL:
1913 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001914 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001915 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001916 case ETHTOOL_FLASHDEV:
1917 rc = ethtool_flash_device(dev, useraddr);
1918 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001919 case ETHTOOL_RESET:
1920 rc = ethtool_reset(dev, useraddr);
1921 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001922 case ETHTOOL_GSSET_INFO:
1923 rc = ethtool_get_sset_info(dev, useraddr);
1924 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001925 case ETHTOOL_GRXFHINDIR:
1926 rc = ethtool_get_rxfh_indir(dev, useraddr);
1927 break;
1928 case ETHTOOL_SRXFHINDIR:
1929 rc = ethtool_set_rxfh_indir(dev, useraddr);
1930 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301931 case ETHTOOL_GRSSH:
1932 rc = ethtool_get_rxfh(dev, useraddr);
1933 break;
1934 case ETHTOOL_SRSSH:
1935 rc = ethtool_set_rxfh(dev, useraddr);
1936 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001937 case ETHTOOL_GFEATURES:
1938 rc = ethtool_get_features(dev, useraddr);
1939 break;
1940 case ETHTOOL_SFEATURES:
1941 rc = ethtool_set_features(dev, useraddr);
1942 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001943 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001944 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001945 case ETHTOOL_GSG:
1946 case ETHTOOL_GTSO:
1947 case ETHTOOL_GUFO:
1948 case ETHTOOL_GGSO:
1949 case ETHTOOL_GGRO:
1950 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1951 break;
1952 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001953 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001954 case ETHTOOL_SSG:
1955 case ETHTOOL_STSO:
1956 case ETHTOOL_SUFO:
1957 case ETHTOOL_SGSO:
1958 case ETHTOOL_SGRO:
1959 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1960 break;
amit salecha8b5933c2011-04-07 01:58:42 +00001961 case ETHTOOL_GCHANNELS:
1962 rc = ethtool_get_channels(dev, useraddr);
1963 break;
1964 case ETHTOOL_SCHANNELS:
1965 rc = ethtool_set_channels(dev, useraddr);
1966 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001967 case ETHTOOL_SET_DUMP:
1968 rc = ethtool_set_dump(dev, useraddr);
1969 break;
1970 case ETHTOOL_GET_DUMP_FLAG:
1971 rc = ethtool_get_dump_flag(dev, useraddr);
1972 break;
1973 case ETHTOOL_GET_DUMP_DATA:
1974 rc = ethtool_get_dump_data(dev, useraddr);
1975 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001976 case ETHTOOL_GET_TS_INFO:
1977 rc = ethtool_get_ts_info(dev, useraddr);
1978 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001979 case ETHTOOL_GMODULEINFO:
1980 rc = ethtool_get_module_info(dev, useraddr);
1981 break;
1982 case ETHTOOL_GMODULEEEPROM:
1983 rc = ethtool_get_module_eeprom(dev, useraddr);
1984 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05301985 case ETHTOOL_GTUNABLE:
1986 rc = ethtool_get_tunable(dev, useraddr);
1987 break;
1988 case ETHTOOL_STUNABLE:
1989 rc = ethtool_set_tunable(dev, useraddr);
1990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001992 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001994
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001995 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001997
1998 if (old_features != dev->features)
1999 netdev_features_change(dev);
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}