blob: e88b80d41f736be4868688fcaaa7d01f83e666ab [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>
Jeff Garzikd17792e2010-03-04 08:21:53 +000020#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070021#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070022#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010024#include <linux/rtnetlink.h>
25#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090027/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Some useful ethtool_ops methods that're device independent.
29 * If we find that all drivers want to do the same thing here,
30 * we can turn these into dev_() function calls.
31 */
32
33u32 ethtool_op_get_link(struct net_device *dev)
34{
35 return netif_carrier_ok(dev) ? 1 : 0;
36}
chavey97f8aef2010-04-07 21:54:42 -070037EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* Handlers for each ethtool command */
40
Michał Mirosław475414f2011-11-15 15:29:55 +000041#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000042
Michał Mirosław9d921542011-11-15 15:29:55 +000043static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
44 [NETIF_F_SG_BIT] = "tx-scatter-gather",
45 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000046 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
47 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
48 [NETIF_F_HIGHDMA_BIT] = "highdma",
49 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
50 [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
51
52 [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
53 [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
54 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
55 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
56 [NETIF_F_LLTX_BIT] = "tx-lockless",
57 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
58 [NETIF_F_GRO_BIT] = "rx-gro",
59 [NETIF_F_LRO_BIT] = "rx-lro",
60
61 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
62 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
63 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
64 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
65 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
66 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
67
68 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
69 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
70 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
71 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
72 [NETIF_F_RXHASH_BIT] = "rx-hashing",
73 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
74 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
75 [NETIF_F_LOOPBACK_BIT] = "loopback",
76};
77
Michał Mirosław5455c692011-02-15 16:59:17 +000078static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
79{
80 struct ethtool_gfeatures cmd = {
81 .cmd = ETHTOOL_GFEATURES,
82 .size = ETHTOOL_DEV_FEATURE_WORDS,
83 };
Michał Mirosław475414f2011-11-15 15:29:55 +000084 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +000085 u32 __user *sizeaddr;
86 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +000087 int i;
88
89 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +000090 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +000091
92 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +000093 features[i].available = (u32)(dev->hw_features >> (32 * i));
94 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
95 features[i].active = (u32)(dev->features >> (32 * i));
96 features[i].never_changed =
97 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +000098 }
Michał Mirosław5455c692011-02-15 16:59:17 +000099
100 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
101 if (get_user(copy_size, sizeaddr))
102 return -EFAULT;
103
104 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
105 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
106
107 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
108 return -EFAULT;
109 useraddr += sizeof(cmd);
110 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
111 return -EFAULT;
112
113 return 0;
114}
115
116static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
117{
118 struct ethtool_sfeatures cmd;
119 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000120 netdev_features_t wanted = 0, valid = 0;
121 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000122
123 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
124 return -EFAULT;
125 useraddr += sizeof(cmd);
126
127 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
128 return -EINVAL;
129
130 if (copy_from_user(features, useraddr, sizeof(features)))
131 return -EFAULT;
132
Michał Mirosław475414f2011-11-15 15:29:55 +0000133 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000134 valid |= (netdev_features_t)features[i].valid << (32 * i);
135 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000136 }
137
138 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000139 return -EINVAL;
140
Michał Mirosław475414f2011-11-15 15:29:55 +0000141 if (valid & ~dev->hw_features) {
142 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000143 ret |= ETHTOOL_F_UNSUPPORTED;
144 }
145
Michał Mirosław475414f2011-11-15 15:29:55 +0000146 dev->wanted_features &= ~valid;
147 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700148 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000149
Michał Mirosław475414f2011-11-15 15:29:55 +0000150 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000151 ret |= ETHTOOL_F_WISH;
152
153 return ret;
154}
155
Michał Mirosław340ae162011-02-15 16:59:16 +0000156static int __ethtool_get_sset_count(struct net_device *dev, int sset)
157{
158 const struct ethtool_ops *ops = dev->ethtool_ops;
159
Michał Mirosław5455c692011-02-15 16:59:17 +0000160 if (sset == ETH_SS_FEATURES)
161 return ARRAY_SIZE(netdev_features_strings);
162
Michał Mirosław340ae162011-02-15 16:59:16 +0000163 if (ops && ops->get_sset_count && ops->get_strings)
164 return ops->get_sset_count(dev, sset);
165 else
166 return -EOPNOTSUPP;
167}
168
169static void __ethtool_get_strings(struct net_device *dev,
170 u32 stringset, u8 *data)
171{
172 const struct ethtool_ops *ops = dev->ethtool_ops;
173
Michał Mirosław5455c692011-02-15 16:59:17 +0000174 if (stringset == ETH_SS_FEATURES)
175 memcpy(data, netdev_features_strings,
176 sizeof(netdev_features_strings));
177 else
178 /* ops->get_strings is valid because checked earlier */
179 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000180}
181
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000182static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000183{
184 /* feature masks of legacy discrete ethtool ops */
185
186 switch (eth_cmd) {
187 case ETHTOOL_GTXCSUM:
188 case ETHTOOL_STXCSUM:
189 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000190 case ETHTOOL_GRXCSUM:
191 case ETHTOOL_SRXCSUM:
192 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000193 case ETHTOOL_GSG:
194 case ETHTOOL_SSG:
195 return NETIF_F_SG;
196 case ETHTOOL_GTSO:
197 case ETHTOOL_STSO:
198 return NETIF_F_ALL_TSO;
199 case ETHTOOL_GUFO:
200 case ETHTOOL_SUFO:
201 return NETIF_F_UFO;
202 case ETHTOOL_GGSO:
203 case ETHTOOL_SGSO:
204 return NETIF_F_GSO;
205 case ETHTOOL_GGRO:
206 case ETHTOOL_SGRO:
207 return NETIF_F_GRO;
208 default:
209 BUG();
210 }
211}
212
Michał Mirosław0a417702011-02-15 16:59:17 +0000213static int ethtool_get_one_feature(struct net_device *dev,
214 char __user *useraddr, u32 ethcmd)
215{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000216 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000217 struct ethtool_value edata = {
218 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000219 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000220 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000221
Michał Mirosław0a417702011-02-15 16:59:17 +0000222 if (copy_to_user(useraddr, &edata, sizeof(edata)))
223 return -EFAULT;
224 return 0;
225}
226
Michał Mirosław0a417702011-02-15 16:59:17 +0000227static int ethtool_set_one_feature(struct net_device *dev,
228 void __user *useraddr, u32 ethcmd)
229{
230 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000231 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000232
233 if (copy_from_user(&edata, useraddr, sizeof(edata)))
234 return -EFAULT;
235
Michał Mirosław86794882011-02-15 16:59:17 +0000236 mask = ethtool_get_feature_mask(ethcmd);
237 mask &= dev->hw_features;
Michał Mirosławbc5787c2011-11-15 15:29:55 +0000238 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000239 return -EOPNOTSUPP;
Michał Mirosławbc5787c2011-11-15 15:29:55 +0000240
241 if (edata.data)
242 dev->wanted_features |= mask;
243 else
244 dev->wanted_features &= ~mask;
245
246 __netdev_update_features(dev);
247
248 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000249}
250
Michał Mirosław02b3a552011-11-15 15:29:55 +0000251#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
252 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
253#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
254 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
Michał Mirosławbc5787c2011-11-15 15:29:55 +0000255
256static u32 __ethtool_get_flags(struct net_device *dev)
257{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000258 u32 flags = 0;
259
260 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
261 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
262 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
263 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
264 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
265
266 return flags;
Michał Mirosławbc5787c2011-11-15 15:29:55 +0000267}
268
269static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000270{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000271 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000272
Michał Mirosław02b3a552011-11-15 15:29:55 +0000273 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000274 return -EINVAL;
275
Michał Mirosław02b3a552011-11-15 15:29:55 +0000276 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
277 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
278 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
279 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
280 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
281
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000282 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000283 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000284 if (changed & ~dev->hw_features)
285 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
286
287 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000288 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000289
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700290 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000291
292 return 0;
293}
294
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000295int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000297 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000299 if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EOPNOTSUPP;
301
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000302 memset(cmd, 0, sizeof(struct ethtool_cmd));
303 cmd->cmd = ETHTOOL_GSET;
304 return dev->ethtool_ops->get_settings(dev, cmd);
305}
306EXPORT_SYMBOL(__ethtool_get_settings);
307
308static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
309{
310 int err;
311 struct ethtool_cmd cmd;
312
313 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (err < 0)
315 return err;
316
317 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
318 return -EFAULT;
319 return 0;
320}
321
322static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
323{
324 struct ethtool_cmd cmd;
325
326 if (!dev->ethtool_ops->set_settings)
327 return -EOPNOTSUPP;
328
329 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
330 return -EFAULT;
331
332 return dev->ethtool_ops->set_settings(dev, &cmd);
333}
334
chavey97f8aef2010-04-07 21:54:42 -0700335static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
336 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700339 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 memset(&info, 0, sizeof(info));
342 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700343 if (ops && ops->get_drvinfo) {
344 ops->get_drvinfo(dev, &info);
345 } else if (dev->dev.parent && dev->dev.parent->driver) {
346 strlcpy(info.bus_info, dev_name(dev->dev.parent),
347 sizeof(info.bus_info));
348 strlcpy(info.driver, dev->dev.parent->driver->name,
349 sizeof(info.driver));
350 } else {
351 return -EOPNOTSUPP;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jeff Garzik723b2f52010-03-03 22:51:50 +0000354 /*
355 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000356 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000357 */
Ben Hutchings01414802010-08-17 02:31:15 -0700358 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700359 int rc;
360
361 rc = ops->get_sset_count(dev, ETH_SS_TEST);
362 if (rc >= 0)
363 info.testinfo_len = rc;
364 rc = ops->get_sset_count(dev, ETH_SS_STATS);
365 if (rc >= 0)
366 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700367 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
368 if (rc >= 0)
369 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700370 }
Ben Hutchings01414802010-08-17 02:31:15 -0700371 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700373 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 info.eedump_len = ops->get_eeprom_len(dev);
375
376 if (copy_to_user(useraddr, &info, sizeof(info)))
377 return -EFAULT;
378 return 0;
379}
380
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800381static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700382 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000383{
384 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000385 u64 sset_mask;
386 int i, idx = 0, n_bits = 0, ret, rc;
387 u32 *info_buf = NULL;
388
Jeff Garzik723b2f52010-03-03 22:51:50 +0000389 if (copy_from_user(&info, useraddr, sizeof(info)))
390 return -EFAULT;
391
392 /* store copy of mask, because we zero struct later on */
393 sset_mask = info.sset_mask;
394 if (!sset_mask)
395 return 0;
396
397 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000398 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000399
400 memset(&info, 0, sizeof(info));
401 info.cmd = ETHTOOL_GSSET_INFO;
402
403 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
404 if (!info_buf)
405 return -ENOMEM;
406
407 /*
408 * fill return buffer based on input bitmask and successful
409 * get_sset_count return
410 */
411 for (i = 0; i < 64; i++) {
412 if (!(sset_mask & (1ULL << i)))
413 continue;
414
Michał Mirosław340ae162011-02-15 16:59:16 +0000415 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000416 if (rc >= 0) {
417 info.sset_mask |= (1ULL << i);
418 info_buf[idx++] = rc;
419 }
420 }
421
422 ret = -EFAULT;
423 if (copy_to_user(useraddr, &info, sizeof(info)))
424 goto out;
425
426 useraddr += offsetof(struct ethtool_sset_info, data);
427 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
428 goto out;
429
430 ret = 0;
431
432out:
433 kfree(info_buf);
434 return ret;
435}
436
chavey97f8aef2010-04-07 21:54:42 -0700437static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000438 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700439{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000440 struct ethtool_rxnfc info;
441 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000442 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700443
Santwona Behera59089d82009-02-20 00:58:13 -0800444 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700445 return -EOPNOTSUPP;
446
Ben Hutchingsbf988432010-06-28 08:45:58 +0000447 /* struct ethtool_rxnfc was originally defined for
448 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
449 * members. User-space might still be using that
450 * definition. */
451 if (cmd == ETHTOOL_SRXFH)
452 info_size = (offsetof(struct ethtool_rxnfc, data) +
453 sizeof(info.data));
454
455 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700456 return -EFAULT;
457
Ben Hutchings55664f32012-01-03 12:04:51 +0000458 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
459 if (rc)
460 return rc;
461
462 if (cmd == ETHTOOL_SRXCLSRLINS &&
463 copy_to_user(useraddr, &info, info_size))
464 return -EFAULT;
465
466 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700467}
468
chavey97f8aef2010-04-07 21:54:42 -0700469static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000470 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700471{
472 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000473 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800474 const struct ethtool_ops *ops = dev->ethtool_ops;
475 int ret;
476 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700477
Santwona Behera59089d82009-02-20 00:58:13 -0800478 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700479 return -EOPNOTSUPP;
480
Ben Hutchingsbf988432010-06-28 08:45:58 +0000481 /* struct ethtool_rxnfc was originally defined for
482 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
483 * members. User-space might still be using that
484 * definition. */
485 if (cmd == ETHTOOL_GRXFH)
486 info_size = (offsetof(struct ethtool_rxnfc, data) +
487 sizeof(info.data));
488
489 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700490 return -EFAULT;
491
Santwona Behera59089d82009-02-20 00:58:13 -0800492 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
493 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000494 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000495 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000496 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800497 if (!rule_buf)
498 return -ENOMEM;
499 }
500 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700501
Santwona Behera59089d82009-02-20 00:58:13 -0800502 ret = ops->get_rxnfc(dev, &info, rule_buf);
503 if (ret < 0)
504 goto err_out;
505
506 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000507 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800508 goto err_out;
509
510 if (rule_buf) {
511 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
512 if (copy_to_user(useraddr, rule_buf,
513 info.rule_cnt * sizeof(u32)))
514 goto err_out;
515 }
516 ret = 0;
517
518err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700519 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800520
521 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700522}
523
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000524static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
525 void __user *useraddr)
526{
Ben Hutchings7850f632011-12-15 13:55:01 +0000527 u32 user_size, dev_size;
528 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000529 int ret;
530
Ben Hutchings7850f632011-12-15 13:55:01 +0000531 if (!dev->ethtool_ops->get_rxfh_indir_size ||
532 !dev->ethtool_ops->get_rxfh_indir)
533 return -EOPNOTSUPP;
534 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
535 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000536 return -EOPNOTSUPP;
537
Ben Hutchings7850f632011-12-15 13:55:01 +0000538 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000539 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000540 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000541 return -EFAULT;
542
Ben Hutchings7850f632011-12-15 13:55:01 +0000543 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
544 &dev_size, sizeof(dev_size)))
545 return -EFAULT;
546
547 /* If the user buffer size is 0, this is just a query for the
548 * device table size. Otherwise, if it's smaller than the
549 * device table size it's an error.
550 */
551 if (user_size < dev_size)
552 return user_size == 0 ? 0 : -EINVAL;
553
554 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000555 if (!indir)
556 return -ENOMEM;
557
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000558 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
559 if (ret)
560 goto out;
561
Ben Hutchings7850f632011-12-15 13:55:01 +0000562 if (copy_to_user(useraddr +
563 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
564 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000565 ret = -EFAULT;
566
567out:
568 kfree(indir);
569 return ret;
570}
571
572static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
573 void __user *useraddr)
574{
Ben Hutchings7850f632011-12-15 13:55:01 +0000575 struct ethtool_rxnfc rx_rings;
576 u32 user_size, dev_size, i;
577 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000578 int ret;
579
Ben Hutchings7850f632011-12-15 13:55:01 +0000580 if (!dev->ethtool_ops->get_rxfh_indir_size ||
581 !dev->ethtool_ops->set_rxfh_indir ||
582 !dev->ethtool_ops->get_rxnfc)
583 return -EOPNOTSUPP;
584 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
585 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000586 return -EOPNOTSUPP;
587
Ben Hutchings7850f632011-12-15 13:55:01 +0000588 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000589 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000590 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000591 return -EFAULT;
592
Ben Hutchings278bc422011-12-15 13:56:49 +0000593 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000594 return -EINVAL;
595
596 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000597 if (!indir)
598 return -ENOMEM;
599
Ben Hutchings7850f632011-12-15 13:55:01 +0000600 rx_rings.cmd = ETHTOOL_GRXRINGS;
601 ret = dev->ethtool_ops->get_rxnfc(dev, &rx_rings, NULL);
602 if (ret)
603 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000604
605 if (user_size == 0) {
606 for (i = 0; i < dev_size; i++)
607 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
608 } else {
609 if (copy_from_user(indir,
610 useraddr +
611 offsetof(struct ethtool_rxfh_indir,
612 ring_index[0]),
613 dev_size * sizeof(indir[0]))) {
614 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000615 goto out;
616 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000617
618 /* Validate ring indices */
619 for (i = 0; i < dev_size; i++) {
620 if (indir[i] >= rx_rings.data) {
621 ret = -EINVAL;
622 goto out;
623 }
624 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000625 }
626
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000627 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
628
629out:
630 kfree(indir);
631 return ret;
632}
633
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000634/*
635 * ethtool does not (or did not) set masks for flow parameters that are
636 * not specified, so if both value and mask are 0 then this must be
637 * treated as equivalent to a mask with all bits set. Implement that
638 * here rather than in drivers.
639 */
640static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
641{
642 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
643 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
644
645 if (fs->flow_type != TCP_V4_FLOW &&
646 fs->flow_type != UDP_V4_FLOW &&
647 fs->flow_type != SCTP_V4_FLOW)
648 return;
649
650 if (!(entry->ip4src | mask->ip4src))
651 mask->ip4src = htonl(0xffffffff);
652 if (!(entry->ip4dst | mask->ip4dst))
653 mask->ip4dst = htonl(0xffffffff);
654 if (!(entry->psrc | mask->psrc))
655 mask->psrc = htons(0xffff);
656 if (!(entry->pdst | mask->pdst))
657 mask->pdst = htons(0xffff);
658 if (!(entry->tos | mask->tos))
659 mask->tos = 0xff;
660 if (!(fs->vlan_tag | fs->vlan_tag_mask))
661 fs->vlan_tag_mask = 0xffff;
662 if (!(fs->data | fs->data_mask))
663 fs->data_mask = 0xffffffffffffffffULL;
664}
665
chavey97f8aef2010-04-07 21:54:42 -0700666static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
667 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800668{
669 struct ethtool_rx_ntuple cmd;
670 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800671
Alexander Duyck5d9f11c2011-04-08 12:07:22 +0000672 if (!ops->set_rx_ntuple)
673 return -EOPNOTSUPP;
674
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800675 if (!(dev->features & NETIF_F_NTUPLE))
676 return -EINVAL;
677
678 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
679 return -EFAULT;
680
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000681 rx_ntuple_fix_masks(&cmd.fs);
682
Alexander Duyckbff55272011-06-08 12:35:08 +0000683 return ops->set_rx_ntuple(dev, &cmd);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
687{
688 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700689 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 void *regbuf;
691 int reglen, ret;
692
693 if (!ops->get_regs || !ops->get_regs_len)
694 return -EOPNOTSUPP;
695
696 if (copy_from_user(&regs, useraddr, sizeof(regs)))
697 return -EFAULT;
698
699 reglen = ops->get_regs_len(dev);
700 if (regs.len > reglen)
701 regs.len = reglen;
702
Eugene Teob7c7d012011-01-24 21:05:17 -0800703 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700704 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
706
707 ops->get_regs(dev, &regs, regbuf);
708
709 ret = -EFAULT;
710 if (copy_to_user(useraddr, &regs, sizeof(regs)))
711 goto out;
712 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700713 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto out;
715 ret = 0;
716
717 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000718 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ret;
720}
721
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000722static int ethtool_reset(struct net_device *dev, char __user *useraddr)
723{
724 struct ethtool_value reset;
725 int ret;
726
727 if (!dev->ethtool_ops->reset)
728 return -EOPNOTSUPP;
729
730 if (copy_from_user(&reset, useraddr, sizeof(reset)))
731 return -EFAULT;
732
733 ret = dev->ethtool_ops->reset(dev, &reset.data);
734 if (ret)
735 return ret;
736
737 if (copy_to_user(useraddr, &reset, sizeof(reset)))
738 return -EFAULT;
739 return 0;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
743{
Roland Dreier8e557422010-02-11 12:14:23 -0800744 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (!dev->ethtool_ops->get_wol)
747 return -EOPNOTSUPP;
748
749 dev->ethtool_ops->get_wol(dev, &wol);
750
751 if (copy_to_user(useraddr, &wol, sizeof(wol)))
752 return -EFAULT;
753 return 0;
754}
755
756static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
757{
758 struct ethtool_wolinfo wol;
759
760 if (!dev->ethtool_ops->set_wol)
761 return -EOPNOTSUPP;
762
763 if (copy_from_user(&wol, useraddr, sizeof(wol)))
764 return -EFAULT;
765
766 return dev->ethtool_ops->set_wol(dev, &wol);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static int ethtool_nway_reset(struct net_device *dev)
770{
771 if (!dev->ethtool_ops->nway_reset)
772 return -EOPNOTSUPP;
773
774 return dev->ethtool_ops->nway_reset(dev);
775}
776
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000777static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
778{
779 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
780
781 if (!dev->ethtool_ops->get_link)
782 return -EOPNOTSUPP;
783
784 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
785
786 if (copy_to_user(useraddr, &edata, sizeof(edata)))
787 return -EFAULT;
788 return 0;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
792{
793 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700794 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700795 void __user *userbuf = useraddr + sizeof(eeprom);
796 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700798 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!ops->get_eeprom || !ops->get_eeprom_len)
801 return -EOPNOTSUPP;
802
803 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
804 return -EFAULT;
805
806 /* Check for wrap and zero */
807 if (eeprom.offset + eeprom.len <= eeprom.offset)
808 return -EINVAL;
809
810 /* Check for exceeding total eeprom len */
811 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
812 return -EINVAL;
813
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700814 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!data)
816 return -ENOMEM;
817
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700818 bytes_remaining = eeprom.len;
819 while (bytes_remaining > 0) {
820 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700822 ret = ops->get_eeprom(dev, &eeprom, data);
823 if (ret)
824 break;
825 if (copy_to_user(userbuf, data, eeprom.len)) {
826 ret = -EFAULT;
827 break;
828 }
829 userbuf += eeprom.len;
830 eeprom.offset += eeprom.len;
831 bytes_remaining -= eeprom.len;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700834 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
835 eeprom.offset -= eeprom.len;
836 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
837 ret = -EFAULT;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 kfree(data);
840 return ret;
841}
842
843static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
844{
845 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700846 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700847 void __user *userbuf = useraddr + sizeof(eeprom);
848 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700850 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (!ops->set_eeprom || !ops->get_eeprom_len)
853 return -EOPNOTSUPP;
854
855 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
856 return -EFAULT;
857
858 /* Check for wrap and zero */
859 if (eeprom.offset + eeprom.len <= eeprom.offset)
860 return -EINVAL;
861
862 /* Check for exceeding total eeprom len */
863 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
864 return -EINVAL;
865
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700866 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!data)
868 return -ENOMEM;
869
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700870 bytes_remaining = eeprom.len;
871 while (bytes_remaining > 0) {
872 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700874 if (copy_from_user(data, userbuf, eeprom.len)) {
875 ret = -EFAULT;
876 break;
877 }
878 ret = ops->set_eeprom(dev, &eeprom, data);
879 if (ret)
880 break;
881 userbuf += eeprom.len;
882 eeprom.offset += eeprom.len;
883 bytes_remaining -= eeprom.len;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 kfree(data);
887 return ret;
888}
889
chavey97f8aef2010-04-07 21:54:42 -0700890static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
891 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Roland Dreier8e557422010-02-11 12:14:23 -0800893 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (!dev->ethtool_ops->get_coalesce)
896 return -EOPNOTSUPP;
897
898 dev->ethtool_ops->get_coalesce(dev, &coalesce);
899
900 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
901 return -EFAULT;
902 return 0;
903}
904
chavey97f8aef2010-04-07 21:54:42 -0700905static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
906 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct ethtool_coalesce coalesce;
909
David S. Millerfa04ae52005-06-06 15:07:19 -0700910 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -EOPNOTSUPP;
912
913 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
914 return -EFAULT;
915
916 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
917}
918
919static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
920{
Roland Dreier8e557422010-02-11 12:14:23 -0800921 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (!dev->ethtool_ops->get_ringparam)
924 return -EOPNOTSUPP;
925
926 dev->ethtool_ops->get_ringparam(dev, &ringparam);
927
928 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
929 return -EFAULT;
930 return 0;
931}
932
933static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
934{
935 struct ethtool_ringparam ringparam;
936
937 if (!dev->ethtool_ops->set_ringparam)
938 return -EOPNOTSUPP;
939
940 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
941 return -EFAULT;
942
943 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
944}
945
amit salecha8b5933c2011-04-07 01:58:42 +0000946static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
947 void __user *useraddr)
948{
949 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
950
951 if (!dev->ethtool_ops->get_channels)
952 return -EOPNOTSUPP;
953
954 dev->ethtool_ops->get_channels(dev, &channels);
955
956 if (copy_to_user(useraddr, &channels, sizeof(channels)))
957 return -EFAULT;
958 return 0;
959}
960
961static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
962 void __user *useraddr)
963{
964 struct ethtool_channels channels;
965
966 if (!dev->ethtool_ops->set_channels)
967 return -EOPNOTSUPP;
968
969 if (copy_from_user(&channels, useraddr, sizeof(channels)))
970 return -EFAULT;
971
972 return dev->ethtool_ops->set_channels(dev, &channels);
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
976{
977 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
978
979 if (!dev->ethtool_ops->get_pauseparam)
980 return -EOPNOTSUPP;
981
982 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
983
984 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
985 return -EFAULT;
986 return 0;
987}
988
989static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
990{
991 struct ethtool_pauseparam pauseparam;
992
Jeff Garzike1b90c42006-07-17 12:54:40 -0400993 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -EOPNOTSUPP;
995
996 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
997 return -EFAULT;
998
999 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1003{
1004 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001005 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001007 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001009 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001010 return -EOPNOTSUPP;
1011
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001012 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001013 if (test_len < 0)
1014 return test_len;
1015 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (copy_from_user(&test, useraddr, sizeof(test)))
1018 return -EFAULT;
1019
Jeff Garzikff03d492007-08-15 16:01:08 -07001020 test.len = test_len;
1021 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (!data)
1023 return -ENOMEM;
1024
1025 ops->self_test(dev, &test, data);
1026
1027 ret = -EFAULT;
1028 if (copy_to_user(useraddr, &test, sizeof(test)))
1029 goto out;
1030 useraddr += sizeof(test);
1031 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1032 goto out;
1033 ret = 0;
1034
1035 out:
1036 kfree(data);
1037 return ret;
1038}
1039
1040static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1041{
1042 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 u8 *data;
1044 int ret;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1047 return -EFAULT;
1048
Michał Mirosław340ae162011-02-15 16:59:16 +00001049 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001050 if (ret < 0)
1051 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001052
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001053 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1056 if (!data)
1057 return -ENOMEM;
1058
Michał Mirosław340ae162011-02-15 16:59:16 +00001059 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 ret = -EFAULT;
1062 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1063 goto out;
1064 useraddr += sizeof(gstrings);
1065 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1066 goto out;
1067 ret = 0;
1068
Michał Mirosław340ae162011-02-15 16:59:16 +00001069out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 kfree(data);
1071 return ret;
1072}
1073
1074static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1075{
1076 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001077 static bool busy;
1078 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Stephen Hemminger1ab7b6a2011-04-14 23:46:06 -07001080 if (!dev->ethtool_ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -EOPNOTSUPP;
1082
Ben Hutchings68f512f2011-04-02 00:35:15 +01001083 if (busy)
1084 return -EBUSY;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (copy_from_user(&id, useraddr, sizeof(id)))
1087 return -EFAULT;
1088
Ben Hutchings68f512f2011-04-02 00:35:15 +01001089 rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001090 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001091 return rc;
1092
1093 /* Drop the RTNL lock while waiting, but prevent reentry or
1094 * removal of the device.
1095 */
1096 busy = true;
1097 dev_hold(dev);
1098 rtnl_unlock();
1099
1100 if (rc == 0) {
1101 /* Driver will handle this itself */
1102 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001103 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001104 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001105 /* Driver expects to be called at twice the frequency in rc */
1106 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001107
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001108 /* Count down seconds */
1109 do {
1110 /* Count down iterations per second */
1111 i = n;
1112 do {
1113 rtnl_lock();
1114 rc = dev->ethtool_ops->set_phys_id(dev,
1115 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1116 rtnl_unlock();
1117 if (rc)
1118 break;
1119 schedule_timeout_interruptible(interval);
1120 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001121 } while (!signal_pending(current) &&
1122 (id.data == 0 || --id.data != 0));
1123 }
1124
1125 rtnl_lock();
1126 dev_put(dev);
1127 busy = false;
1128
1129 (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1130 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1134{
1135 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001136 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001138 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001140 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001141 return -EOPNOTSUPP;
1142
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001143 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001144 if (n_stats < 0)
1145 return n_stats;
1146 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1149 return -EFAULT;
1150
Jeff Garzikff03d492007-08-15 16:01:08 -07001151 stats.n_stats = n_stats;
1152 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!data)
1154 return -ENOMEM;
1155
1156 ops->get_ethtool_stats(dev, &stats, data);
1157
1158 ret = -EFAULT;
1159 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1160 goto out;
1161 useraddr += sizeof(stats);
1162 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1163 goto out;
1164 ret = 0;
1165
1166 out:
1167 kfree(data);
1168 return ret;
1169}
1170
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001171static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001172{
1173 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001174
Matthew Wilcox313674a2007-07-31 14:00:29 -07001175 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001176 return -EFAULT;
1177
Matthew Wilcox313674a2007-07-31 14:00:29 -07001178 if (epaddr.size < dev->addr_len)
1179 return -ETOOSMALL;
1180 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001181
Jon Wetzela6f9a702005-08-20 17:15:54 -07001182 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001183 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001184 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001185 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1186 return -EFAULT;
1187 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001188}
1189
Jeff Garzik13c99b22007-08-15 16:01:56 -07001190static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1191 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001192{
Roland Dreier8e557422010-02-11 12:14:23 -08001193 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001194
Jeff Garzik13c99b22007-08-15 16:01:56 -07001195 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001196 return -EOPNOTSUPP;
1197
Jeff Garzik13c99b22007-08-15 16:01:56 -07001198 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001199
1200 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1201 return -EFAULT;
1202 return 0;
1203}
1204
Jeff Garzik13c99b22007-08-15 16:01:56 -07001205static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1206 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001207{
1208 struct ethtool_value edata;
1209
Jeff Garzik13c99b22007-08-15 16:01:56 -07001210 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001211 return -EOPNOTSUPP;
1212
1213 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1214 return -EFAULT;
1215
Jeff Garzik13c99b22007-08-15 16:01:56 -07001216 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001217 return 0;
1218}
1219
Jeff Garzik13c99b22007-08-15 16:01:56 -07001220static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1221 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001222{
1223 struct ethtool_value edata;
1224
Jeff Garzik13c99b22007-08-15 16:01:56 -07001225 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001226 return -EOPNOTSUPP;
1227
1228 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1229 return -EFAULT;
1230
Jeff Garzik13c99b22007-08-15 16:01:56 -07001231 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001232}
1233
chavey97f8aef2010-04-07 21:54:42 -07001234static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1235 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001236{
1237 struct ethtool_flash efl;
1238
1239 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1240 return -EFAULT;
1241
1242 if (!dev->ethtool_ops->flash_device)
1243 return -EOPNOTSUPP;
1244
1245 return dev->ethtool_ops->flash_device(dev, &efl);
1246}
1247
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001248static int ethtool_set_dump(struct net_device *dev,
1249 void __user *useraddr)
1250{
1251 struct ethtool_dump dump;
1252
1253 if (!dev->ethtool_ops->set_dump)
1254 return -EOPNOTSUPP;
1255
1256 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1257 return -EFAULT;
1258
1259 return dev->ethtool_ops->set_dump(dev, &dump);
1260}
1261
1262static int ethtool_get_dump_flag(struct net_device *dev,
1263 void __user *useraddr)
1264{
1265 int ret;
1266 struct ethtool_dump dump;
1267 const struct ethtool_ops *ops = dev->ethtool_ops;
1268
1269 if (!dev->ethtool_ops->get_dump_flag)
1270 return -EOPNOTSUPP;
1271
1272 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1273 return -EFAULT;
1274
1275 ret = ops->get_dump_flag(dev, &dump);
1276 if (ret)
1277 return ret;
1278
1279 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1280 return -EFAULT;
1281 return 0;
1282}
1283
1284static int ethtool_get_dump_data(struct net_device *dev,
1285 void __user *useraddr)
1286{
1287 int ret;
1288 __u32 len;
1289 struct ethtool_dump dump, tmp;
1290 const struct ethtool_ops *ops = dev->ethtool_ops;
1291 void *data = NULL;
1292
1293 if (!dev->ethtool_ops->get_dump_data ||
1294 !dev->ethtool_ops->get_dump_flag)
1295 return -EOPNOTSUPP;
1296
1297 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1298 return -EFAULT;
1299
1300 memset(&tmp, 0, sizeof(tmp));
1301 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1302 ret = ops->get_dump_flag(dev, &tmp);
1303 if (ret)
1304 return ret;
1305
1306 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1307 if (!len)
1308 return -EFAULT;
1309
1310 data = vzalloc(tmp.len);
1311 if (!data)
1312 return -ENOMEM;
1313 ret = ops->get_dump_data(dev, &dump, data);
1314 if (ret)
1315 goto out;
1316
1317 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1318 ret = -EFAULT;
1319 goto out;
1320 }
1321 useraddr += offsetof(struct ethtool_dump, data);
1322 if (copy_to_user(useraddr, data, len))
1323 ret = -EFAULT;
1324out:
1325 vfree(data);
1326 return ret;
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/* The main entry point in this file. Called from net/core/dev.c */
1330
Eric W. Biederman881d9662007-09-17 11:56:21 -07001331int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001333 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 void __user *useraddr = ifr->ifr_data;
1335 u32 ethcmd;
1336 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001337 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (!dev || !netif_device_present(dev))
1340 return -ENODEV;
1341
chavey97f8aef2010-04-07 21:54:42 -07001342 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EFAULT;
1344
Ben Hutchings01414802010-08-17 02:31:15 -07001345 if (!dev->ethtool_ops) {
1346 /* ETHTOOL_GDRVINFO does not require any driver support.
1347 * It is also unprivileged and does not change anything,
1348 * so we can take a shortcut to it. */
1349 if (ethcmd == ETHTOOL_GDRVINFO)
1350 return ethtool_get_drvinfo(dev, useraddr);
1351 else
1352 return -EOPNOTSUPP;
1353 }
1354
Stephen Hemminger75f31232006-09-28 15:13:37 -07001355 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001356 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001357 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001358 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001359 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001360 case ETHTOOL_GCOALESCE:
1361 case ETHTOOL_GRINGPARAM:
1362 case ETHTOOL_GPAUSEPARAM:
1363 case ETHTOOL_GRXCSUM:
1364 case ETHTOOL_GTXCSUM:
1365 case ETHTOOL_GSG:
1366 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001367 case ETHTOOL_GTSO:
1368 case ETHTOOL_GPERMADDR:
1369 case ETHTOOL_GUFO:
1370 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001371 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001372 case ETHTOOL_GFLAGS:
1373 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001374 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001375 case ETHTOOL_GRXRINGS:
1376 case ETHTOOL_GRXCLSRLCNT:
1377 case ETHTOOL_GRXCLSRULE:
1378 case ETHTOOL_GRXCLSRLALL:
Michał Mirosław5455c692011-02-15 16:59:17 +00001379 case ETHTOOL_GFEATURES:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001380 break;
1381 default:
1382 if (!capable(CAP_NET_ADMIN))
1383 return -EPERM;
1384 }
1385
chavey97f8aef2010-04-07 21:54:42 -07001386 if (dev->ethtool_ops->begin) {
1387 rc = dev->ethtool_ops->begin(dev);
1388 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001390 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001391 old_features = dev->features;
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 switch (ethcmd) {
1394 case ETHTOOL_GSET:
1395 rc = ethtool_get_settings(dev, useraddr);
1396 break;
1397 case ETHTOOL_SSET:
1398 rc = ethtool_set_settings(dev, useraddr);
1399 break;
1400 case ETHTOOL_GDRVINFO:
1401 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 case ETHTOOL_GREGS:
1404 rc = ethtool_get_regs(dev, useraddr);
1405 break;
1406 case ETHTOOL_GWOL:
1407 rc = ethtool_get_wol(dev, useraddr);
1408 break;
1409 case ETHTOOL_SWOL:
1410 rc = ethtool_set_wol(dev, useraddr);
1411 break;
1412 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001413 rc = ethtool_get_value(dev, useraddr, ethcmd,
1414 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001417 rc = ethtool_set_value_void(dev, useraddr,
1418 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 case ETHTOOL_NWAY_RST:
1421 rc = ethtool_nway_reset(dev);
1422 break;
1423 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001424 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 case ETHTOOL_GEEPROM:
1427 rc = ethtool_get_eeprom(dev, useraddr);
1428 break;
1429 case ETHTOOL_SEEPROM:
1430 rc = ethtool_set_eeprom(dev, useraddr);
1431 break;
1432 case ETHTOOL_GCOALESCE:
1433 rc = ethtool_get_coalesce(dev, useraddr);
1434 break;
1435 case ETHTOOL_SCOALESCE:
1436 rc = ethtool_set_coalesce(dev, useraddr);
1437 break;
1438 case ETHTOOL_GRINGPARAM:
1439 rc = ethtool_get_ringparam(dev, useraddr);
1440 break;
1441 case ETHTOOL_SRINGPARAM:
1442 rc = ethtool_set_ringparam(dev, useraddr);
1443 break;
1444 case ETHTOOL_GPAUSEPARAM:
1445 rc = ethtool_get_pauseparam(dev, useraddr);
1446 break;
1447 case ETHTOOL_SPAUSEPARAM:
1448 rc = ethtool_set_pauseparam(dev, useraddr);
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 case ETHTOOL_TEST:
1451 rc = ethtool_self_test(dev, useraddr);
1452 break;
1453 case ETHTOOL_GSTRINGS:
1454 rc = ethtool_get_strings(dev, useraddr);
1455 break;
1456 case ETHTOOL_PHYS_ID:
1457 rc = ethtool_phys_id(dev, useraddr);
1458 break;
1459 case ETHTOOL_GSTATS:
1460 rc = ethtool_get_stats(dev, useraddr);
1461 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001462 case ETHTOOL_GPERMADDR:
1463 rc = ethtool_get_perm_addr(dev, useraddr);
1464 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001465 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001466 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c2011-11-15 15:29:55 +00001467 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001468 break;
1469 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001470 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001471 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001472 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001473 rc = ethtool_get_value(dev, useraddr, ethcmd,
1474 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001475 break;
1476 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001477 rc = ethtool_set_value(dev, useraddr,
1478 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001479 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001480 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001481 case ETHTOOL_GRXRINGS:
1482 case ETHTOOL_GRXCLSRLCNT:
1483 case ETHTOOL_GRXCLSRULE:
1484 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001485 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001486 break;
1487 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001488 case ETHTOOL_SRXCLSRLDEL:
1489 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001490 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001491 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001492 case ETHTOOL_FLASHDEV:
1493 rc = ethtool_flash_device(dev, useraddr);
1494 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001495 case ETHTOOL_RESET:
1496 rc = ethtool_reset(dev, useraddr);
1497 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001498 case ETHTOOL_SRXNTUPLE:
1499 rc = ethtool_set_rx_ntuple(dev, useraddr);
1500 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001501 case ETHTOOL_GSSET_INFO:
1502 rc = ethtool_get_sset_info(dev, useraddr);
1503 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001504 case ETHTOOL_GRXFHINDIR:
1505 rc = ethtool_get_rxfh_indir(dev, useraddr);
1506 break;
1507 case ETHTOOL_SRXFHINDIR:
1508 rc = ethtool_set_rxfh_indir(dev, useraddr);
1509 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001510 case ETHTOOL_GFEATURES:
1511 rc = ethtool_get_features(dev, useraddr);
1512 break;
1513 case ETHTOOL_SFEATURES:
1514 rc = ethtool_set_features(dev, useraddr);
1515 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001516 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001517 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001518 case ETHTOOL_GSG:
1519 case ETHTOOL_GTSO:
1520 case ETHTOOL_GUFO:
1521 case ETHTOOL_GGSO:
1522 case ETHTOOL_GGRO:
1523 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1524 break;
1525 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001526 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001527 case ETHTOOL_SSG:
1528 case ETHTOOL_STSO:
1529 case ETHTOOL_SUFO:
1530 case ETHTOOL_SGSO:
1531 case ETHTOOL_SGRO:
1532 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1533 break;
amit salecha8b5933c2011-04-07 01:58:42 +00001534 case ETHTOOL_GCHANNELS:
1535 rc = ethtool_get_channels(dev, useraddr);
1536 break;
1537 case ETHTOOL_SCHANNELS:
1538 rc = ethtool_set_channels(dev, useraddr);
1539 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001540 case ETHTOOL_SET_DUMP:
1541 rc = ethtool_set_dump(dev, useraddr);
1542 break;
1543 case ETHTOOL_GET_DUMP_FLAG:
1544 rc = ethtool_get_dump_flag(dev, useraddr);
1545 break;
1546 case ETHTOOL_GET_DUMP_DATA:
1547 rc = ethtool_get_dump_data(dev, useraddr);
1548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001550 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001552
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001553 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001555
1556 if (old_features != dev->features)
1557 netdev_features_change(dev);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}