blob: b8c2bcfee6affc257e6f190a5fd1be4f152c864b [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
39u32 ethtool_op_get_tx_csum(struct net_device *dev)
40{
Herbert Xu8648b302006-06-17 22:06:05 -070041 return (dev->features & NETIF_F_ALL_CSUM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000043EXPORT_SYMBOL(ethtool_op_get_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
46{
47 if (data)
48 dev->features |= NETIF_F_IP_CSUM;
49 else
50 dev->features &= ~NETIF_F_IP_CSUM;
51
52 return 0;
53}
Michał Mirosław9a279ea2011-02-15 16:59:16 +000054EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jon Mason69f6a0f2005-05-29 20:27:24 -070056int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
57{
58 if (data)
59 dev->features |= NETIF_F_HW_CSUM;
60 else
61 dev->features &= ~NETIF_F_HW_CSUM;
62
63 return 0;
64}
chavey97f8aef2010-04-07 21:54:42 -070065EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -070066
67int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
68{
69 if (data)
70 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
71 else
72 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
73
74 return 0;
75}
chavey97f8aef2010-04-07 21:54:42 -070076EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Michael Chan6460d942007-07-14 19:07:52 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078u32 ethtool_op_get_sg(struct net_device *dev)
79{
80 return (dev->features & NETIF_F_SG) != 0;
81}
chavey97f8aef2010-04-07 21:54:42 -070082EXPORT_SYMBOL(ethtool_op_get_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84int ethtool_op_set_sg(struct net_device *dev, u32 data)
85{
86 if (data)
87 dev->features |= NETIF_F_SG;
88 else
89 dev->features &= ~NETIF_F_SG;
90
91 return 0;
92}
chavey97f8aef2010-04-07 21:54:42 -070093EXPORT_SYMBOL(ethtool_op_set_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95u32 ethtool_op_get_tso(struct net_device *dev)
96{
97 return (dev->features & NETIF_F_TSO) != 0;
98}
chavey97f8aef2010-04-07 21:54:42 -070099EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101int ethtool_op_set_tso(struct net_device *dev, u32 data)
102{
103 if (data)
104 dev->features |= NETIF_F_TSO;
105 else
106 dev->features &= ~NETIF_F_TSO;
107
108 return 0;
109}
chavey97f8aef2010-04-07 21:54:42 -0700110EXPORT_SYMBOL(ethtool_op_set_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700112u32 ethtool_op_get_ufo(struct net_device *dev)
113{
114 return (dev->features & NETIF_F_UFO) != 0;
115}
chavey97f8aef2010-04-07 21:54:42 -0700116EXPORT_SYMBOL(ethtool_op_get_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700117
118int ethtool_op_set_ufo(struct net_device *dev, u32 data)
119{
120 if (data)
121 dev->features |= NETIF_F_UFO;
122 else
123 dev->features &= ~NETIF_F_UFO;
124 return 0;
125}
chavey97f8aef2010-04-07 21:54:42 -0700126EXPORT_SYMBOL(ethtool_op_set_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700127
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700128/* the following list of flags are the same as their associated
129 * NETIF_F_xxx values in include/linux/netdevice.h
130 */
131static const u32 flags_dup_features =
Jesse Grossd5dbda22010-10-20 13:56:07 +0000132 (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
133 ETH_FLAG_RXHASH);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700134
135u32 ethtool_op_get_flags(struct net_device *dev)
136{
137 /* in the future, this function will probably contain additional
138 * handling for flags which are not so easily handled
139 * by a simple masking operation
140 */
141
142 return dev->features & flags_dup_features;
143}
chavey97f8aef2010-04-07 21:54:42 -0700144EXPORT_SYMBOL(ethtool_op_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700145
Stanislaw Gruszka673e63c2011-03-22 23:54:49 +0000146/* Check if device can enable (or disable) particular feature coded in "data"
147 * argument. Flags "supported" describe features that can be toggled by device.
148 * If feature can not be toggled, it state (enabled or disabled) must match
149 * hardcoded device features state, otherwise flags are marked as invalid.
150 */
151bool ethtool_invalid_flags(struct net_device *dev, u32 data, u32 supported)
152{
153 u32 features = dev->features & flags_dup_features;
154 /* "data" can contain only flags_dup_features bits,
155 * see __ethtool_set_flags */
156
157 return (features & ~supported) != (data & ~supported);
158}
159EXPORT_SYMBOL(ethtool_invalid_flags);
160
Ben Hutchings1437ce32010-06-30 02:44:32 +0000161int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700162{
Stanislaw Gruszka673e63c2011-03-22 23:54:49 +0000163 if (ethtool_invalid_flags(dev, data, supported))
Ben Hutchings1437ce32010-06-30 02:44:32 +0000164 return -EINVAL;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000165
Ben Hutchings1437ce32010-06-30 02:44:32 +0000166 dev->features = ((dev->features & ~flags_dup_features) |
167 (data & flags_dup_features));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700168 return 0;
169}
chavey97f8aef2010-04-07 21:54:42 -0700170EXPORT_SYMBOL(ethtool_op_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700171
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800172void ethtool_ntuple_flush(struct net_device *dev)
173{
174 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
175
176 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
177 list_del(&fsc->list);
178 kfree(fsc);
179 }
180 dev->ethtool_ntuple_list.count = 0;
181}
182EXPORT_SYMBOL(ethtool_ntuple_flush);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* Handlers for each ethtool command */
185
Michał Mirosław5455c692011-02-15 16:59:17 +0000186#define ETHTOOL_DEV_FEATURE_WORDS 1
187
Michał Mirosław4e4db202011-02-22 16:52:28 +0000188static void ethtool_get_features_compat(struct net_device *dev,
189 struct ethtool_get_features_block *features)
190{
191 if (!dev->ethtool_ops)
192 return;
193
194 /* getting RX checksum */
195 if (dev->ethtool_ops->get_rx_csum)
196 if (dev->ethtool_ops->get_rx_csum(dev))
197 features[0].active |= NETIF_F_RXCSUM;
Michał Mirosław39fc0ce2011-02-22 16:52:29 +0000198
199 /* mark legacy-changeable features */
200 if (dev->ethtool_ops->set_sg)
201 features[0].available |= NETIF_F_SG;
202 if (dev->ethtool_ops->set_tx_csum)
203 features[0].available |= NETIF_F_ALL_CSUM;
204 if (dev->ethtool_ops->set_tso)
205 features[0].available |= NETIF_F_ALL_TSO;
206 if (dev->ethtool_ops->set_rx_csum)
207 features[0].available |= NETIF_F_RXCSUM;
208 if (dev->ethtool_ops->set_flags)
209 features[0].available |= flags_dup_features;
210}
211
212static int ethtool_set_feature_compat(struct net_device *dev,
213 int (*legacy_set)(struct net_device *, u32),
214 struct ethtool_set_features_block *features, u32 mask)
215{
216 u32 do_set;
217
218 if (!legacy_set)
219 return 0;
220
221 if (!(features[0].valid & mask))
222 return 0;
223
224 features[0].valid &= ~mask;
225
226 do_set = !!(features[0].requested & mask);
227
228 if (legacy_set(dev, do_set) < 0)
229 netdev_info(dev,
230 "Legacy feature change (%s) failed for 0x%08x\n",
231 do_set ? "set" : "clear", mask);
232
233 return 1;
234}
235
236static int ethtool_set_features_compat(struct net_device *dev,
237 struct ethtool_set_features_block *features)
238{
239 int compat;
240
241 if (!dev->ethtool_ops)
242 return 0;
243
244 compat = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
245 features, NETIF_F_SG);
246 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
247 features, NETIF_F_ALL_CSUM);
248 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
249 features, NETIF_F_ALL_TSO);
250 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
251 features, NETIF_F_RXCSUM);
252 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_flags,
253 features, flags_dup_features);
254
255 return compat;
Michał Mirosław4e4db202011-02-22 16:52:28 +0000256}
257
Michał Mirosław5455c692011-02-15 16:59:17 +0000258static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
259{
260 struct ethtool_gfeatures cmd = {
261 .cmd = ETHTOOL_GFEATURES,
262 .size = ETHTOOL_DEV_FEATURE_WORDS,
263 };
264 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
265 {
266 .available = dev->hw_features,
267 .requested = dev->wanted_features,
268 .active = dev->features,
269 .never_changed = NETIF_F_NEVER_CHANGE,
270 },
271 };
272 u32 __user *sizeaddr;
273 u32 copy_size;
274
Michał Mirosław4e4db202011-02-22 16:52:28 +0000275 ethtool_get_features_compat(dev, features);
276
Michał Mirosław5455c692011-02-15 16:59:17 +0000277 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
278 if (get_user(copy_size, sizeaddr))
279 return -EFAULT;
280
281 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
282 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
283
284 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
285 return -EFAULT;
286 useraddr += sizeof(cmd);
287 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
288 return -EFAULT;
289
290 return 0;
291}
292
293static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
294{
295 struct ethtool_sfeatures cmd;
296 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
297 int ret = 0;
298
299 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
300 return -EFAULT;
301 useraddr += sizeof(cmd);
302
303 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
304 return -EINVAL;
305
306 if (copy_from_user(features, useraddr, sizeof(features)))
307 return -EFAULT;
308
309 if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
310 return -EINVAL;
311
Michał Mirosław39fc0ce2011-02-22 16:52:29 +0000312 if (ethtool_set_features_compat(dev, features))
313 ret |= ETHTOOL_F_COMPAT;
314
Michał Mirosław5455c692011-02-15 16:59:17 +0000315 if (features[0].valid & ~dev->hw_features) {
316 features[0].valid &= dev->hw_features;
317 ret |= ETHTOOL_F_UNSUPPORTED;
318 }
319
320 dev->wanted_features &= ~features[0].valid;
321 dev->wanted_features |= features[0].valid & features[0].requested;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700322 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000323
324 if ((dev->wanted_features ^ dev->features) & features[0].valid)
325 ret |= ETHTOOL_F_WISH;
326
327 return ret;
328}
329
330static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
331 /* NETIF_F_SG */ "tx-scatter-gather",
332 /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
333 /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
334 /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
335 /* NETIF_F_IPV6_CSUM */ "tx_checksum-ipv6",
336 /* NETIF_F_HIGHDMA */ "highdma",
337 /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
338 /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
339
340 /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
341 /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
342 /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
343 /* NETIF_F_GSO */ "tx-generic-segmentation",
344 /* NETIF_F_LLTX */ "tx-lockless",
345 /* NETIF_F_NETNS_LOCAL */ "netns-local",
346 /* NETIF_F_GRO */ "rx-gro",
347 /* NETIF_F_LRO */ "rx-lro",
348
349 /* NETIF_F_TSO */ "tx-tcp-segmentation",
350 /* NETIF_F_UFO */ "tx-udp-fragmentation",
351 /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
352 /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
353 /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
354 /* NETIF_F_FSO */ "tx-fcoe-segmentation",
355 "",
356 "",
357
358 /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
359 /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
360 /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
361 /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
362 /* NETIF_F_RXHASH */ "rx-hashing",
Michał Mirosławe83d3602011-02-15 16:59:18 +0000363 /* NETIF_F_RXCSUM */ "rx-checksum",
Franco Fichtner864b54182011-05-12 06:42:04 +0000364 /* NETIF_F_NOCACHE_COPY */ "tx-nocache-copy",
Mahesh Bandewareed2a122011-05-04 15:30:11 +0000365 /* NETIF_F_LOOPBACK */ "loopback",
Michał Mirosław5455c692011-02-15 16:59:17 +0000366};
367
Michał Mirosław340ae162011-02-15 16:59:16 +0000368static int __ethtool_get_sset_count(struct net_device *dev, int sset)
369{
370 const struct ethtool_ops *ops = dev->ethtool_ops;
371
Michał Mirosław5455c692011-02-15 16:59:17 +0000372 if (sset == ETH_SS_FEATURES)
373 return ARRAY_SIZE(netdev_features_strings);
374
Michał Mirosław340ae162011-02-15 16:59:16 +0000375 if (ops && ops->get_sset_count && ops->get_strings)
376 return ops->get_sset_count(dev, sset);
377 else
378 return -EOPNOTSUPP;
379}
380
381static void __ethtool_get_strings(struct net_device *dev,
382 u32 stringset, u8 *data)
383{
384 const struct ethtool_ops *ops = dev->ethtool_ops;
385
Michał Mirosław5455c692011-02-15 16:59:17 +0000386 if (stringset == ETH_SS_FEATURES)
387 memcpy(data, netdev_features_strings,
388 sizeof(netdev_features_strings));
389 else
390 /* ops->get_strings is valid because checked earlier */
391 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000392}
393
Michał Mirosław0a417702011-02-15 16:59:17 +0000394static u32 ethtool_get_feature_mask(u32 eth_cmd)
395{
396 /* feature masks of legacy discrete ethtool ops */
397
398 switch (eth_cmd) {
399 case ETHTOOL_GTXCSUM:
400 case ETHTOOL_STXCSUM:
401 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000402 case ETHTOOL_GRXCSUM:
403 case ETHTOOL_SRXCSUM:
404 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000405 case ETHTOOL_GSG:
406 case ETHTOOL_SSG:
407 return NETIF_F_SG;
408 case ETHTOOL_GTSO:
409 case ETHTOOL_STSO:
410 return NETIF_F_ALL_TSO;
411 case ETHTOOL_GUFO:
412 case ETHTOOL_SUFO:
413 return NETIF_F_UFO;
414 case ETHTOOL_GGSO:
415 case ETHTOOL_SGSO:
416 return NETIF_F_GSO;
417 case ETHTOOL_GGRO:
418 case ETHTOOL_SGRO:
419 return NETIF_F_GRO;
420 default:
421 BUG();
422 }
423}
424
425static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
426{
427 const struct ethtool_ops *ops = dev->ethtool_ops;
428
429 if (!ops)
430 return NULL;
431
432 switch (ethcmd) {
433 case ETHTOOL_GTXCSUM:
434 return ops->get_tx_csum;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000435 case ETHTOOL_GRXCSUM:
436 return ops->get_rx_csum;
Michał Mirosław0a417702011-02-15 16:59:17 +0000437 case ETHTOOL_SSG:
438 return ops->get_sg;
439 case ETHTOOL_STSO:
440 return ops->get_tso;
441 case ETHTOOL_SUFO:
442 return ops->get_ufo;
443 default:
444 return NULL;
445 }
446}
447
Michał Mirosławe83d3602011-02-15 16:59:18 +0000448static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
449{
450 return !!(dev->features & NETIF_F_ALL_CSUM);
451}
452
Michał Mirosław0a417702011-02-15 16:59:17 +0000453static int ethtool_get_one_feature(struct net_device *dev,
454 char __user *useraddr, u32 ethcmd)
455{
Michał Mirosław86794882011-02-15 16:59:17 +0000456 u32 mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000457 struct ethtool_value edata = {
458 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000459 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000460 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000461
Michał Mirosław86794882011-02-15 16:59:17 +0000462 /* compatibility with discrete get_ ops */
463 if (!(dev->hw_features & mask)) {
464 u32 (*actor)(struct net_device *);
465
466 actor = __ethtool_get_one_feature_actor(dev, ethcmd);
467
Michał Mirosławe83d3602011-02-15 16:59:18 +0000468 /* bug compatibility with old get_rx_csum */
469 if (ethcmd == ETHTOOL_GRXCSUM && !actor)
470 actor = __ethtool_get_rx_csum_oldbug;
471
Michał Mirosław86794882011-02-15 16:59:17 +0000472 if (actor)
473 edata.data = actor(dev);
474 }
Michał Mirosław0a417702011-02-15 16:59:17 +0000475
476 if (copy_to_user(useraddr, &edata, sizeof(edata)))
477 return -EFAULT;
478 return 0;
479}
480
481static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
Michał Mirosławe83d3602011-02-15 16:59:18 +0000482static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
Michał Mirosław0a417702011-02-15 16:59:17 +0000483static int __ethtool_set_sg(struct net_device *dev, u32 data);
484static int __ethtool_set_tso(struct net_device *dev, u32 data);
485static int __ethtool_set_ufo(struct net_device *dev, u32 data);
486
487static int ethtool_set_one_feature(struct net_device *dev,
488 void __user *useraddr, u32 ethcmd)
489{
490 struct ethtool_value edata;
491 u32 mask;
492
493 if (copy_from_user(&edata, useraddr, sizeof(edata)))
494 return -EFAULT;
495
Michał Mirosław86794882011-02-15 16:59:17 +0000496 mask = ethtool_get_feature_mask(ethcmd);
497 mask &= dev->hw_features;
498 if (mask) {
499 if (edata.data)
500 dev->wanted_features |= mask;
501 else
502 dev->wanted_features &= ~mask;
503
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700504 __netdev_update_features(dev);
Michał Mirosław86794882011-02-15 16:59:17 +0000505 return 0;
506 }
507
508 /* Driver is not converted to ndo_fix_features or does not
509 * support changing this offload. In the latter case it won't
510 * have corresponding ethtool_ops field set.
511 *
512 * Following part is to be removed after all drivers advertise
513 * their changeable features in netdev->hw_features and stop
514 * using discrete offload setting ops.
515 */
516
Michał Mirosław0a417702011-02-15 16:59:17 +0000517 switch (ethcmd) {
518 case ETHTOOL_STXCSUM:
519 return __ethtool_set_tx_csum(dev, edata.data);
Michał Mirosławe83d3602011-02-15 16:59:18 +0000520 case ETHTOOL_SRXCSUM:
521 return __ethtool_set_rx_csum(dev, edata.data);
Michał Mirosław0a417702011-02-15 16:59:17 +0000522 case ETHTOOL_SSG:
523 return __ethtool_set_sg(dev, edata.data);
524 case ETHTOOL_STSO:
525 return __ethtool_set_tso(dev, edata.data);
526 case ETHTOOL_SUFO:
527 return __ethtool_set_ufo(dev, edata.data);
Michał Mirosław0a417702011-02-15 16:59:17 +0000528 default:
529 return -EOPNOTSUPP;
530 }
531}
532
Michał Mirosław27660512011-03-18 16:56:34 +0000533int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000534{
535 u32 changed;
536
537 if (data & ~flags_dup_features)
538 return -EINVAL;
539
540 /* legacy set_flags() op */
541 if (dev->ethtool_ops->set_flags) {
542 if (unlikely(dev->hw_features & flags_dup_features))
543 netdev_warn(dev,
544 "driver BUG: mixed hw_features and set_flags()\n");
545 return dev->ethtool_ops->set_flags(dev, data);
546 }
547
548 /* allow changing only bits set in hw_features */
Michał Mirosław5f8629c2011-04-21 13:59:21 +0000549 changed = (data ^ dev->features) & flags_dup_features;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000550 if (changed & ~dev->hw_features)
551 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
552
553 dev->wanted_features =
Michał Mirosław5f8629c2011-04-21 13:59:21 +0000554 (dev->wanted_features & ~changed) | (data & dev->hw_features);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000555
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700556 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000557
558 return 0;
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
562{
Roland Dreier8e557422010-02-11 12:14:23 -0800563 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int err;
565
566 if (!dev->ethtool_ops->get_settings)
567 return -EOPNOTSUPP;
568
569 err = dev->ethtool_ops->get_settings(dev, &cmd);
570 if (err < 0)
571 return err;
572
573 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
574 return -EFAULT;
575 return 0;
576}
577
578static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
579{
580 struct ethtool_cmd cmd;
581
582 if (!dev->ethtool_ops->set_settings)
583 return -EOPNOTSUPP;
584
585 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
586 return -EFAULT;
587
588 return dev->ethtool_ops->set_settings(dev, &cmd);
589}
590
chavey97f8aef2010-04-07 21:54:42 -0700591static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
592 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700595 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 memset(&info, 0, sizeof(info));
598 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700599 if (ops && ops->get_drvinfo) {
600 ops->get_drvinfo(dev, &info);
601 } else if (dev->dev.parent && dev->dev.parent->driver) {
602 strlcpy(info.bus_info, dev_name(dev->dev.parent),
603 sizeof(info.bus_info));
604 strlcpy(info.driver, dev->dev.parent->driver->name,
605 sizeof(info.driver));
606 } else {
607 return -EOPNOTSUPP;
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jeff Garzik723b2f52010-03-03 22:51:50 +0000610 /*
611 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000612 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000613 */
Ben Hutchings01414802010-08-17 02:31:15 -0700614 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700615 int rc;
616
617 rc = ops->get_sset_count(dev, ETH_SS_TEST);
618 if (rc >= 0)
619 info.testinfo_len = rc;
620 rc = ops->get_sset_count(dev, ETH_SS_STATS);
621 if (rc >= 0)
622 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700623 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
624 if (rc >= 0)
625 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700626 }
Ben Hutchings01414802010-08-17 02:31:15 -0700627 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700629 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 info.eedump_len = ops->get_eeprom_len(dev);
631
632 if (copy_to_user(useraddr, &info, sizeof(info)))
633 return -EFAULT;
634 return 0;
635}
636
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800637static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700638 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000639{
640 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000641 u64 sset_mask;
642 int i, idx = 0, n_bits = 0, ret, rc;
643 u32 *info_buf = NULL;
644
Jeff Garzik723b2f52010-03-03 22:51:50 +0000645 if (copy_from_user(&info, useraddr, sizeof(info)))
646 return -EFAULT;
647
648 /* store copy of mask, because we zero struct later on */
649 sset_mask = info.sset_mask;
650 if (!sset_mask)
651 return 0;
652
653 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000654 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000655
656 memset(&info, 0, sizeof(info));
657 info.cmd = ETHTOOL_GSSET_INFO;
658
659 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
660 if (!info_buf)
661 return -ENOMEM;
662
663 /*
664 * fill return buffer based on input bitmask and successful
665 * get_sset_count return
666 */
667 for (i = 0; i < 64; i++) {
668 if (!(sset_mask & (1ULL << i)))
669 continue;
670
Michał Mirosław340ae162011-02-15 16:59:16 +0000671 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000672 if (rc >= 0) {
673 info.sset_mask |= (1ULL << i);
674 info_buf[idx++] = rc;
675 }
676 }
677
678 ret = -EFAULT;
679 if (copy_to_user(useraddr, &info, sizeof(info)))
680 goto out;
681
682 useraddr += offsetof(struct ethtool_sset_info, data);
683 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
684 goto out;
685
686 ret = 0;
687
688out:
689 kfree(info_buf);
690 return ret;
691}
692
chavey97f8aef2010-04-07 21:54:42 -0700693static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000694 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700695{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000696 struct ethtool_rxnfc info;
697 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700698
Santwona Behera59089d82009-02-20 00:58:13 -0800699 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700700 return -EOPNOTSUPP;
701
Ben Hutchingsbf988432010-06-28 08:45:58 +0000702 /* struct ethtool_rxnfc was originally defined for
703 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
704 * members. User-space might still be using that
705 * definition. */
706 if (cmd == ETHTOOL_SRXFH)
707 info_size = (offsetof(struct ethtool_rxnfc, data) +
708 sizeof(info.data));
709
710 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700711 return -EFAULT;
712
Ben Hutchingsbf988432010-06-28 08:45:58 +0000713 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700714}
715
chavey97f8aef2010-04-07 21:54:42 -0700716static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000717 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700718{
719 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000720 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800721 const struct ethtool_ops *ops = dev->ethtool_ops;
722 int ret;
723 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700724
Santwona Behera59089d82009-02-20 00:58:13 -0800725 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700726 return -EOPNOTSUPP;
727
Ben Hutchingsbf988432010-06-28 08:45:58 +0000728 /* struct ethtool_rxnfc was originally defined for
729 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
730 * members. User-space might still be using that
731 * definition. */
732 if (cmd == ETHTOOL_GRXFH)
733 info_size = (offsetof(struct ethtool_rxnfc, data) +
734 sizeof(info.data));
735
736 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700737 return -EFAULT;
738
Santwona Behera59089d82009-02-20 00:58:13 -0800739 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
740 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000741 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000742 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000743 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800744 if (!rule_buf)
745 return -ENOMEM;
746 }
747 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700748
Santwona Behera59089d82009-02-20 00:58:13 -0800749 ret = ops->get_rxnfc(dev, &info, rule_buf);
750 if (ret < 0)
751 goto err_out;
752
753 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000754 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800755 goto err_out;
756
757 if (rule_buf) {
758 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
759 if (copy_to_user(useraddr, rule_buf,
760 info.rule_cnt * sizeof(u32)))
761 goto err_out;
762 }
763 ret = 0;
764
765err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700766 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800767
768 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700769}
770
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000771static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
772 void __user *useraddr)
773{
774 struct ethtool_rxfh_indir *indir;
775 u32 table_size;
776 size_t full_size;
777 int ret;
778
779 if (!dev->ethtool_ops->get_rxfh_indir)
780 return -EOPNOTSUPP;
781
782 if (copy_from_user(&table_size,
783 useraddr + offsetof(struct ethtool_rxfh_indir, size),
784 sizeof(table_size)))
785 return -EFAULT;
786
787 if (table_size >
788 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
789 return -ENOMEM;
790 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
Kees Cookb00916b2010-10-11 12:23:25 -0700791 indir = kzalloc(full_size, GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000792 if (!indir)
793 return -ENOMEM;
794
795 indir->cmd = ETHTOOL_GRXFHINDIR;
796 indir->size = table_size;
797 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
798 if (ret)
799 goto out;
800
801 if (copy_to_user(useraddr, indir, full_size))
802 ret = -EFAULT;
803
804out:
805 kfree(indir);
806 return ret;
807}
808
809static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
810 void __user *useraddr)
811{
812 struct ethtool_rxfh_indir *indir;
813 u32 table_size;
814 size_t full_size;
815 int ret;
816
817 if (!dev->ethtool_ops->set_rxfh_indir)
818 return -EOPNOTSUPP;
819
820 if (copy_from_user(&table_size,
821 useraddr + offsetof(struct ethtool_rxfh_indir, size),
822 sizeof(table_size)))
823 return -EFAULT;
824
825 if (table_size >
826 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
827 return -ENOMEM;
828 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
829 indir = kmalloc(full_size, GFP_USER);
830 if (!indir)
831 return -ENOMEM;
832
833 if (copy_from_user(indir, useraddr, full_size)) {
834 ret = -EFAULT;
835 goto out;
836 }
837
838 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
839
840out:
841 kfree(indir);
842 return ret;
843}
844
Peter Waskiewicze8589112010-02-12 13:48:05 +0000845static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700846 struct ethtool_rx_ntuple_flow_spec *spec,
847 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800848{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800849
850 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000851 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
852 /* free the container */
853 kfree(fsc);
854 return;
855 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800856
857 /* Copy the whole filter over */
858 fsc->fs.flow_type = spec->flow_type;
859 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
860 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
861
862 fsc->fs.vlan_tag = spec->vlan_tag;
863 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
864 fsc->fs.data = spec->data;
865 fsc->fs.data_mask = spec->data_mask;
866 fsc->fs.action = spec->action;
867
868 /* add to the list */
869 list_add_tail_rcu(&fsc->list, &list->list);
870 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800871}
872
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000873/*
874 * ethtool does not (or did not) set masks for flow parameters that are
875 * not specified, so if both value and mask are 0 then this must be
876 * treated as equivalent to a mask with all bits set. Implement that
877 * here rather than in drivers.
878 */
879static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
880{
881 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
882 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
883
884 if (fs->flow_type != TCP_V4_FLOW &&
885 fs->flow_type != UDP_V4_FLOW &&
886 fs->flow_type != SCTP_V4_FLOW)
887 return;
888
889 if (!(entry->ip4src | mask->ip4src))
890 mask->ip4src = htonl(0xffffffff);
891 if (!(entry->ip4dst | mask->ip4dst))
892 mask->ip4dst = htonl(0xffffffff);
893 if (!(entry->psrc | mask->psrc))
894 mask->psrc = htons(0xffff);
895 if (!(entry->pdst | mask->pdst))
896 mask->pdst = htons(0xffff);
897 if (!(entry->tos | mask->tos))
898 mask->tos = 0xff;
899 if (!(fs->vlan_tag | fs->vlan_tag_mask))
900 fs->vlan_tag_mask = 0xffff;
901 if (!(fs->data | fs->data_mask))
902 fs->data_mask = 0xffffffffffffffffULL;
903}
904
chavey97f8aef2010-04-07 21:54:42 -0700905static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
906 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800907{
908 struct ethtool_rx_ntuple cmd;
909 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000910 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800911 int ret;
912
Alexander Duyck5d9f11c2011-04-08 12:07:22 +0000913 if (!ops->set_rx_ntuple)
914 return -EOPNOTSUPP;
915
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800916 if (!(dev->features & NETIF_F_NTUPLE))
917 return -EINVAL;
918
919 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
920 return -EFAULT;
921
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000922 rx_ntuple_fix_masks(&cmd.fs);
923
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800924 /*
925 * Cache filter in dev struct for GET operation only if
926 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000927 * only if the filter was added successfully. First make sure we
928 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800929 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000930 if (!ops->get_rx_ntuple) {
931 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
932 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800933 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000934 }
935
936 ret = ops->set_rx_ntuple(dev, &cmd);
937 if (ret) {
938 kfree(fsc);
939 return ret;
940 }
941
942 if (!ops->get_rx_ntuple)
943 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800944
945 return ret;
946}
947
948static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
949{
950 struct ethtool_gstrings gstrings;
951 const struct ethtool_ops *ops = dev->ethtool_ops;
952 struct ethtool_rx_ntuple_flow_spec_container *fsc;
953 u8 *data;
954 char *p;
955 int ret, i, num_strings = 0;
956
957 if (!ops->get_sset_count)
958 return -EOPNOTSUPP;
959
960 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
961 return -EFAULT;
962
963 ret = ops->get_sset_count(dev, gstrings.string_set);
964 if (ret < 0)
965 return ret;
966
967 gstrings.len = ret;
968
Kees Cookb00916b2010-10-11 12:23:25 -0700969 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800970 if (!data)
971 return -ENOMEM;
972
973 if (ops->get_rx_ntuple) {
974 /* driver-specific filter grab */
975 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
976 goto copy;
977 }
978
979 /* default ethtool filter grab */
980 i = 0;
981 p = (char *)data;
982 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
983 sprintf(p, "Filter %d:\n", i);
984 p += ETH_GSTRING_LEN;
985 num_strings++;
986
987 switch (fsc->fs.flow_type) {
988 case TCP_V4_FLOW:
989 sprintf(p, "\tFlow Type: TCP\n");
990 p += ETH_GSTRING_LEN;
991 num_strings++;
992 break;
993 case UDP_V4_FLOW:
994 sprintf(p, "\tFlow Type: UDP\n");
995 p += ETH_GSTRING_LEN;
996 num_strings++;
997 break;
998 case SCTP_V4_FLOW:
999 sprintf(p, "\tFlow Type: SCTP\n");
1000 p += ETH_GSTRING_LEN;
1001 num_strings++;
1002 break;
1003 case AH_ESP_V4_FLOW:
1004 sprintf(p, "\tFlow Type: AH ESP\n");
1005 p += ETH_GSTRING_LEN;
1006 num_strings++;
1007 break;
1008 case ESP_V4_FLOW:
1009 sprintf(p, "\tFlow Type: ESP\n");
1010 p += ETH_GSTRING_LEN;
1011 num_strings++;
1012 break;
1013 case IP_USER_FLOW:
1014 sprintf(p, "\tFlow Type: Raw IP\n");
1015 p += ETH_GSTRING_LEN;
1016 num_strings++;
1017 break;
1018 case IPV4_FLOW:
1019 sprintf(p, "\tFlow Type: IPv4\n");
1020 p += ETH_GSTRING_LEN;
1021 num_strings++;
1022 break;
1023 default:
1024 sprintf(p, "\tFlow Type: Unknown\n");
1025 p += ETH_GSTRING_LEN;
1026 num_strings++;
1027 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +00001028 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001029
1030 /* now the rest of the filters */
1031 switch (fsc->fs.flow_type) {
1032 case TCP_V4_FLOW:
1033 case UDP_V4_FLOW:
1034 case SCTP_V4_FLOW:
1035 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001036 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001037 p += ETH_GSTRING_LEN;
1038 num_strings++;
1039 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001040 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001041 p += ETH_GSTRING_LEN;
1042 num_strings++;
1043 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001044 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001045 p += ETH_GSTRING_LEN;
1046 num_strings++;
1047 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001048 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001049 p += ETH_GSTRING_LEN;
1050 num_strings++;
1051 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001052 fsc->fs.h_u.tcp_ip4_spec.psrc,
1053 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001054 p += ETH_GSTRING_LEN;
1055 num_strings++;
1056 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001057 fsc->fs.h_u.tcp_ip4_spec.pdst,
1058 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001059 p += ETH_GSTRING_LEN;
1060 num_strings++;
1061 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001062 fsc->fs.h_u.tcp_ip4_spec.tos,
1063 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001064 p += ETH_GSTRING_LEN;
1065 num_strings++;
1066 break;
1067 case AH_ESP_V4_FLOW:
1068 case ESP_V4_FLOW:
1069 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001070 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001071 p += ETH_GSTRING_LEN;
1072 num_strings++;
1073 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001074 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001075 p += ETH_GSTRING_LEN;
1076 num_strings++;
1077 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001078 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001079 p += ETH_GSTRING_LEN;
1080 num_strings++;
1081 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001082 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001083 p += ETH_GSTRING_LEN;
1084 num_strings++;
1085 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001086 fsc->fs.h_u.ah_ip4_spec.spi,
1087 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001088 p += ETH_GSTRING_LEN;
1089 num_strings++;
1090 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001091 fsc->fs.h_u.ah_ip4_spec.tos,
1092 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001093 p += ETH_GSTRING_LEN;
1094 num_strings++;
1095 break;
1096 case IP_USER_FLOW:
1097 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001098 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001099 p += ETH_GSTRING_LEN;
1100 num_strings++;
1101 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001102 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001103 p += ETH_GSTRING_LEN;
1104 num_strings++;
1105 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001106 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001107 p += ETH_GSTRING_LEN;
1108 num_strings++;
1109 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001110 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001111 p += ETH_GSTRING_LEN;
1112 num_strings++;
1113 break;
1114 case IPV4_FLOW:
1115 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001116 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001117 p += ETH_GSTRING_LEN;
1118 num_strings++;
1119 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001120 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001121 p += ETH_GSTRING_LEN;
1122 num_strings++;
1123 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001124 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001125 p += ETH_GSTRING_LEN;
1126 num_strings++;
1127 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001128 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001129 p += ETH_GSTRING_LEN;
1130 num_strings++;
1131 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001132 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
1133 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001134 p += ETH_GSTRING_LEN;
1135 num_strings++;
1136 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001137 fsc->fs.h_u.usr_ip4_spec.tos,
1138 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001139 p += ETH_GSTRING_LEN;
1140 num_strings++;
1141 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001142 fsc->fs.h_u.usr_ip4_spec.ip_ver,
1143 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001144 p += ETH_GSTRING_LEN;
1145 num_strings++;
1146 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001147 fsc->fs.h_u.usr_ip4_spec.proto,
1148 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001149 p += ETH_GSTRING_LEN;
1150 num_strings++;
1151 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +00001152 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001153 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001154 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001155 p += ETH_GSTRING_LEN;
1156 num_strings++;
1157 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
1158 p += ETH_GSTRING_LEN;
1159 num_strings++;
1160 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
1161 p += ETH_GSTRING_LEN;
1162 num_strings++;
1163 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
1164 sprintf(p, "\tAction: Drop\n");
1165 else
1166 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -07001167 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001168 p += ETH_GSTRING_LEN;
1169 num_strings++;
1170unknown_filter:
1171 i++;
1172 }
1173copy:
1174 /* indicate to userspace how many strings we actually have */
1175 gstrings.len = num_strings;
1176 ret = -EFAULT;
1177 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1178 goto out;
1179 useraddr += sizeof(gstrings);
1180 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1181 goto out;
1182 ret = 0;
1183
1184out:
1185 kfree(data);
1186 return ret;
1187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1190{
1191 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001192 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 void *regbuf;
1194 int reglen, ret;
1195
1196 if (!ops->get_regs || !ops->get_regs_len)
1197 return -EOPNOTSUPP;
1198
1199 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1200 return -EFAULT;
1201
1202 reglen = ops->get_regs_len(dev);
1203 if (regs.len > reglen)
1204 regs.len = reglen;
1205
Eugene Teob7c7d012011-01-24 21:05:17 -08001206 regbuf = vzalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!regbuf)
1208 return -ENOMEM;
1209
1210 ops->get_regs(dev, &regs, regbuf);
1211
1212 ret = -EFAULT;
1213 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1214 goto out;
1215 useraddr += offsetof(struct ethtool_regs, data);
1216 if (copy_to_user(useraddr, regbuf, regs.len))
1217 goto out;
1218 ret = 0;
1219
1220 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001221 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return ret;
1223}
1224
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001225static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1226{
1227 struct ethtool_value reset;
1228 int ret;
1229
1230 if (!dev->ethtool_ops->reset)
1231 return -EOPNOTSUPP;
1232
1233 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1234 return -EFAULT;
1235
1236 ret = dev->ethtool_ops->reset(dev, &reset.data);
1237 if (ret)
1238 return ret;
1239
1240 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1241 return -EFAULT;
1242 return 0;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1246{
Roland Dreier8e557422010-02-11 12:14:23 -08001247 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (!dev->ethtool_ops->get_wol)
1250 return -EOPNOTSUPP;
1251
1252 dev->ethtool_ops->get_wol(dev, &wol);
1253
1254 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1255 return -EFAULT;
1256 return 0;
1257}
1258
1259static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1260{
1261 struct ethtool_wolinfo wol;
1262
1263 if (!dev->ethtool_ops->set_wol)
1264 return -EOPNOTSUPP;
1265
1266 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1267 return -EFAULT;
1268
1269 return dev->ethtool_ops->set_wol(dev, &wol);
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272static int ethtool_nway_reset(struct net_device *dev)
1273{
1274 if (!dev->ethtool_ops->nway_reset)
1275 return -EOPNOTSUPP;
1276
1277 return dev->ethtool_ops->nway_reset(dev);
1278}
1279
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001280static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1281{
1282 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1283
1284 if (!dev->ethtool_ops->get_link)
1285 return -EOPNOTSUPP;
1286
1287 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1288
1289 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1290 return -EFAULT;
1291 return 0;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1295{
1296 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001297 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001298 void __user *userbuf = useraddr + sizeof(eeprom);
1299 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001301 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (!ops->get_eeprom || !ops->get_eeprom_len)
1304 return -EOPNOTSUPP;
1305
1306 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1307 return -EFAULT;
1308
1309 /* Check for wrap and zero */
1310 if (eeprom.offset + eeprom.len <= eeprom.offset)
1311 return -EINVAL;
1312
1313 /* Check for exceeding total eeprom len */
1314 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1315 return -EINVAL;
1316
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001317 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!data)
1319 return -ENOMEM;
1320
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001321 bytes_remaining = eeprom.len;
1322 while (bytes_remaining > 0) {
1323 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001325 ret = ops->get_eeprom(dev, &eeprom, data);
1326 if (ret)
1327 break;
1328 if (copy_to_user(userbuf, data, eeprom.len)) {
1329 ret = -EFAULT;
1330 break;
1331 }
1332 userbuf += eeprom.len;
1333 eeprom.offset += eeprom.len;
1334 bytes_remaining -= eeprom.len;
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001337 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1338 eeprom.offset -= eeprom.len;
1339 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1340 ret = -EFAULT;
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 kfree(data);
1343 return ret;
1344}
1345
1346static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1347{
1348 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001349 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001350 void __user *userbuf = useraddr + sizeof(eeprom);
1351 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001353 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (!ops->set_eeprom || !ops->get_eeprom_len)
1356 return -EOPNOTSUPP;
1357
1358 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1359 return -EFAULT;
1360
1361 /* Check for wrap and zero */
1362 if (eeprom.offset + eeprom.len <= eeprom.offset)
1363 return -EINVAL;
1364
1365 /* Check for exceeding total eeprom len */
1366 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1367 return -EINVAL;
1368
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001369 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (!data)
1371 return -ENOMEM;
1372
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001373 bytes_remaining = eeprom.len;
1374 while (bytes_remaining > 0) {
1375 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001377 if (copy_from_user(data, userbuf, eeprom.len)) {
1378 ret = -EFAULT;
1379 break;
1380 }
1381 ret = ops->set_eeprom(dev, &eeprom, data);
1382 if (ret)
1383 break;
1384 userbuf += eeprom.len;
1385 eeprom.offset += eeprom.len;
1386 bytes_remaining -= eeprom.len;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 kfree(data);
1390 return ret;
1391}
1392
chavey97f8aef2010-04-07 21:54:42 -07001393static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1394 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Roland Dreier8e557422010-02-11 12:14:23 -08001396 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (!dev->ethtool_ops->get_coalesce)
1399 return -EOPNOTSUPP;
1400
1401 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1402
1403 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1404 return -EFAULT;
1405 return 0;
1406}
1407
chavey97f8aef2010-04-07 21:54:42 -07001408static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1409 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 struct ethtool_coalesce coalesce;
1412
David S. Millerfa04ae52005-06-06 15:07:19 -07001413 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EOPNOTSUPP;
1415
1416 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1417 return -EFAULT;
1418
1419 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1420}
1421
1422static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1423{
Roland Dreier8e557422010-02-11 12:14:23 -08001424 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (!dev->ethtool_ops->get_ringparam)
1427 return -EOPNOTSUPP;
1428
1429 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1430
1431 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1432 return -EFAULT;
1433 return 0;
1434}
1435
1436static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1437{
1438 struct ethtool_ringparam ringparam;
1439
1440 if (!dev->ethtool_ops->set_ringparam)
1441 return -EOPNOTSUPP;
1442
1443 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1444 return -EFAULT;
1445
1446 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1447}
1448
amit salecha8b5933c2011-04-07 01:58:42 +00001449static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1450 void __user *useraddr)
1451{
1452 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1453
1454 if (!dev->ethtool_ops->get_channels)
1455 return -EOPNOTSUPP;
1456
1457 dev->ethtool_ops->get_channels(dev, &channels);
1458
1459 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1460 return -EFAULT;
1461 return 0;
1462}
1463
1464static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1465 void __user *useraddr)
1466{
1467 struct ethtool_channels channels;
1468
1469 if (!dev->ethtool_ops->set_channels)
1470 return -EOPNOTSUPP;
1471
1472 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1473 return -EFAULT;
1474
1475 return dev->ethtool_ops->set_channels(dev, &channels);
1476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1479{
1480 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1481
1482 if (!dev->ethtool_ops->get_pauseparam)
1483 return -EOPNOTSUPP;
1484
1485 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1486
1487 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1488 return -EFAULT;
1489 return 0;
1490}
1491
1492static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1493{
1494 struct ethtool_pauseparam pauseparam;
1495
Jeff Garzike1b90c42006-07-17 12:54:40 -04001496 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EOPNOTSUPP;
1498
1499 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1500 return -EFAULT;
1501
1502 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static int __ethtool_set_sg(struct net_device *dev, u32 data)
1506{
1507 int err;
1508
Roger Luethi5e5069b2011-03-17 06:37:21 +00001509 if (!dev->ethtool_ops->set_sg)
1510 return -EOPNOTSUPP;
1511
Michał Mirosław0a417702011-02-15 16:59:17 +00001512 if (data && !(dev->features & NETIF_F_ALL_CSUM))
1513 return -EINVAL;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (!data && dev->ethtool_ops->set_tso) {
1516 err = dev->ethtool_ops->set_tso(dev, 0);
1517 if (err)
1518 return err;
1519 }
1520
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001521 if (!data && dev->ethtool_ops->set_ufo) {
1522 err = dev->ethtool_ops->set_ufo(dev, 0);
1523 if (err)
1524 return err;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return dev->ethtool_ops->set_sg(dev, data);
1527}
1528
Michał Mirosław0a417702011-02-15 16:59:17 +00001529static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int err;
1532
1533 if (!dev->ethtool_ops->set_tx_csum)
1534 return -EOPNOTSUPP;
1535
Michał Mirosław0a417702011-02-15 16:59:17 +00001536 if (!data && dev->ethtool_ops->set_sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 err = __ethtool_set_sg(dev, 0);
1538 if (err)
1539 return err;
1540 }
1541
Michał Mirosław0a417702011-02-15 16:59:17 +00001542 return dev->ethtool_ops->set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Michał Mirosławe83d3602011-02-15 16:59:18 +00001545static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
Herbert Xub240a0e2008-12-15 23:44:31 -08001546{
Herbert Xub240a0e2008-12-15 23:44:31 -08001547 if (!dev->ethtool_ops->set_rx_csum)
1548 return -EOPNOTSUPP;
1549
Michał Mirosławe83d3602011-02-15 16:59:18 +00001550 if (!data)
Herbert Xub240a0e2008-12-15 23:44:31 -08001551 dev->features &= ~NETIF_F_GRO;
1552
Michał Mirosławe83d3602011-02-15 16:59:18 +00001553 return dev->ethtool_ops->set_rx_csum(dev, data);
Herbert Xub240a0e2008-12-15 23:44:31 -08001554}
1555
Michał Mirosław0a417702011-02-15 16:59:17 +00001556static int __ethtool_set_tso(struct net_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (!dev->ethtool_ops->set_tso)
1559 return -EOPNOTSUPP;
1560
Michał Mirosław0a417702011-02-15 16:59:17 +00001561 if (data && !(dev->features & NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -EINVAL;
1563
Michał Mirosław0a417702011-02-15 16:59:17 +00001564 return dev->ethtool_ops->set_tso(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Michał Mirosław0a417702011-02-15 16:59:17 +00001567static int __ethtool_set_ufo(struct net_device *dev, u32 data)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001568{
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001569 if (!dev->ethtool_ops->set_ufo)
1570 return -EOPNOTSUPP;
Michał Mirosław0a417702011-02-15 16:59:17 +00001571 if (data && !(dev->features & NETIF_F_SG))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001572 return -EINVAL;
Michał Mirosław0a417702011-02-15 16:59:17 +00001573 if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
Michał Mirosław79032642010-11-30 06:38:00 +00001574 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1575 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001576 return -EINVAL;
Michał Mirosław0a417702011-02-15 16:59:17 +00001577 return dev->ethtool_ops->set_ufo(dev, data);
Herbert Xub240a0e2008-12-15 23:44:31 -08001578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1581{
1582 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001583 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001585 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001587 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001588 return -EOPNOTSUPP;
1589
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001590 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001591 if (test_len < 0)
1592 return test_len;
1593 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (copy_from_user(&test, useraddr, sizeof(test)))
1596 return -EFAULT;
1597
Jeff Garzikff03d492007-08-15 16:01:08 -07001598 test.len = test_len;
1599 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!data)
1601 return -ENOMEM;
1602
1603 ops->self_test(dev, &test, data);
1604
1605 ret = -EFAULT;
1606 if (copy_to_user(useraddr, &test, sizeof(test)))
1607 goto out;
1608 useraddr += sizeof(test);
1609 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1610 goto out;
1611 ret = 0;
1612
1613 out:
1614 kfree(data);
1615 return ret;
1616}
1617
1618static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1619{
1620 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 u8 *data;
1622 int ret;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1625 return -EFAULT;
1626
Michał Mirosław340ae162011-02-15 16:59:16 +00001627 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001628 if (ret < 0)
1629 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001630
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001631 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1634 if (!data)
1635 return -ENOMEM;
1636
Michał Mirosław340ae162011-02-15 16:59:16 +00001637 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 ret = -EFAULT;
1640 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1641 goto out;
1642 useraddr += sizeof(gstrings);
1643 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1644 goto out;
1645 ret = 0;
1646
Michał Mirosław340ae162011-02-15 16:59:16 +00001647out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 kfree(data);
1649 return ret;
1650}
1651
1652static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1653{
1654 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001655 static bool busy;
1656 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Stephen Hemminger1ab7b6a2011-04-14 23:46:06 -07001658 if (!dev->ethtool_ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -EOPNOTSUPP;
1660
Ben Hutchings68f512f2011-04-02 00:35:15 +01001661 if (busy)
1662 return -EBUSY;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (copy_from_user(&id, useraddr, sizeof(id)))
1665 return -EFAULT;
1666
Ben Hutchings68f512f2011-04-02 00:35:15 +01001667 rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001668 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001669 return rc;
1670
1671 /* Drop the RTNL lock while waiting, but prevent reentry or
1672 * removal of the device.
1673 */
1674 busy = true;
1675 dev_hold(dev);
1676 rtnl_unlock();
1677
1678 if (rc == 0) {
1679 /* Driver will handle this itself */
1680 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001681 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001682 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001683 /* Driver expects to be called at twice the frequency in rc */
1684 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001685
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001686 /* Count down seconds */
1687 do {
1688 /* Count down iterations per second */
1689 i = n;
1690 do {
1691 rtnl_lock();
1692 rc = dev->ethtool_ops->set_phys_id(dev,
1693 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1694 rtnl_unlock();
1695 if (rc)
1696 break;
1697 schedule_timeout_interruptible(interval);
1698 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001699 } while (!signal_pending(current) &&
1700 (id.data == 0 || --id.data != 0));
1701 }
1702
1703 rtnl_lock();
1704 dev_put(dev);
1705 busy = false;
1706
1707 (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1708 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1712{
1713 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001714 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001716 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001718 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001719 return -EOPNOTSUPP;
1720
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001721 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001722 if (n_stats < 0)
1723 return n_stats;
1724 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1727 return -EFAULT;
1728
Jeff Garzikff03d492007-08-15 16:01:08 -07001729 stats.n_stats = n_stats;
1730 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (!data)
1732 return -ENOMEM;
1733
1734 ops->get_ethtool_stats(dev, &stats, data);
1735
1736 ret = -EFAULT;
1737 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1738 goto out;
1739 useraddr += sizeof(stats);
1740 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1741 goto out;
1742 ret = 0;
1743
1744 out:
1745 kfree(data);
1746 return ret;
1747}
1748
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001749static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001750{
1751 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001752
Matthew Wilcox313674a2007-07-31 14:00:29 -07001753 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001754 return -EFAULT;
1755
Matthew Wilcox313674a2007-07-31 14:00:29 -07001756 if (epaddr.size < dev->addr_len)
1757 return -ETOOSMALL;
1758 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001759
Jon Wetzela6f9a702005-08-20 17:15:54 -07001760 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001761 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001762 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001763 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1764 return -EFAULT;
1765 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001766}
1767
Jeff Garzik13c99b22007-08-15 16:01:56 -07001768static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1769 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001770{
Roland Dreier8e557422010-02-11 12:14:23 -08001771 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001772
Jeff Garzik13c99b22007-08-15 16:01:56 -07001773 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001774 return -EOPNOTSUPP;
1775
Jeff Garzik13c99b22007-08-15 16:01:56 -07001776 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001777
1778 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1779 return -EFAULT;
1780 return 0;
1781}
1782
Jeff Garzik13c99b22007-08-15 16:01:56 -07001783static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1784 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001785{
1786 struct ethtool_value edata;
1787
Jeff Garzik13c99b22007-08-15 16:01:56 -07001788 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001789 return -EOPNOTSUPP;
1790
1791 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1792 return -EFAULT;
1793
Jeff Garzik13c99b22007-08-15 16:01:56 -07001794 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001795 return 0;
1796}
1797
Jeff Garzik13c99b22007-08-15 16:01:56 -07001798static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1799 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001800{
1801 struct ethtool_value edata;
1802
Jeff Garzik13c99b22007-08-15 16:01:56 -07001803 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001804 return -EOPNOTSUPP;
1805
1806 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1807 return -EFAULT;
1808
Jeff Garzik13c99b22007-08-15 16:01:56 -07001809 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001810}
1811
chavey97f8aef2010-04-07 21:54:42 -07001812static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1813 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001814{
1815 struct ethtool_flash efl;
1816
1817 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1818 return -EFAULT;
1819
1820 if (!dev->ethtool_ops->flash_device)
1821 return -EOPNOTSUPP;
1822
1823 return dev->ethtool_ops->flash_device(dev, &efl);
1824}
1825
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001826static int ethtool_set_dump(struct net_device *dev,
1827 void __user *useraddr)
1828{
1829 struct ethtool_dump dump;
1830
1831 if (!dev->ethtool_ops->set_dump)
1832 return -EOPNOTSUPP;
1833
1834 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1835 return -EFAULT;
1836
1837 return dev->ethtool_ops->set_dump(dev, &dump);
1838}
1839
1840static int ethtool_get_dump_flag(struct net_device *dev,
1841 void __user *useraddr)
1842{
1843 int ret;
1844 struct ethtool_dump dump;
1845 const struct ethtool_ops *ops = dev->ethtool_ops;
1846
1847 if (!dev->ethtool_ops->get_dump_flag)
1848 return -EOPNOTSUPP;
1849
1850 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1851 return -EFAULT;
1852
1853 ret = ops->get_dump_flag(dev, &dump);
1854 if (ret)
1855 return ret;
1856
1857 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1858 return -EFAULT;
1859 return 0;
1860}
1861
1862static int ethtool_get_dump_data(struct net_device *dev,
1863 void __user *useraddr)
1864{
1865 int ret;
1866 __u32 len;
1867 struct ethtool_dump dump, tmp;
1868 const struct ethtool_ops *ops = dev->ethtool_ops;
1869 void *data = NULL;
1870
1871 if (!dev->ethtool_ops->get_dump_data ||
1872 !dev->ethtool_ops->get_dump_flag)
1873 return -EOPNOTSUPP;
1874
1875 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1876 return -EFAULT;
1877
1878 memset(&tmp, 0, sizeof(tmp));
1879 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1880 ret = ops->get_dump_flag(dev, &tmp);
1881 if (ret)
1882 return ret;
1883
1884 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1885 if (!len)
1886 return -EFAULT;
1887
1888 data = vzalloc(tmp.len);
1889 if (!data)
1890 return -ENOMEM;
1891 ret = ops->get_dump_data(dev, &dump, data);
1892 if (ret)
1893 goto out;
1894
1895 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1896 ret = -EFAULT;
1897 goto out;
1898 }
1899 useraddr += offsetof(struct ethtool_dump, data);
1900 if (copy_to_user(useraddr, data, len))
1901 ret = -EFAULT;
1902out:
1903 vfree(data);
1904 return ret;
1905}
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907/* The main entry point in this file. Called from net/core/dev.c */
1908
Eric W. Biederman881d9662007-09-17 11:56:21 -07001909int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001911 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 void __user *useraddr = ifr->ifr_data;
1913 u32 ethcmd;
1914 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001915 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (!dev || !netif_device_present(dev))
1918 return -ENODEV;
1919
chavey97f8aef2010-04-07 21:54:42 -07001920 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -EFAULT;
1922
Ben Hutchings01414802010-08-17 02:31:15 -07001923 if (!dev->ethtool_ops) {
1924 /* ETHTOOL_GDRVINFO does not require any driver support.
1925 * It is also unprivileged and does not change anything,
1926 * so we can take a shortcut to it. */
1927 if (ethcmd == ETHTOOL_GDRVINFO)
1928 return ethtool_get_drvinfo(dev, useraddr);
1929 else
1930 return -EOPNOTSUPP;
1931 }
1932
Stephen Hemminger75f31232006-09-28 15:13:37 -07001933 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001934 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001935 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001936 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001937 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001938 case ETHTOOL_GCOALESCE:
1939 case ETHTOOL_GRINGPARAM:
1940 case ETHTOOL_GPAUSEPARAM:
1941 case ETHTOOL_GRXCSUM:
1942 case ETHTOOL_GTXCSUM:
1943 case ETHTOOL_GSG:
1944 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001945 case ETHTOOL_GTSO:
1946 case ETHTOOL_GPERMADDR:
1947 case ETHTOOL_GUFO:
1948 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001949 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001950 case ETHTOOL_GFLAGS:
1951 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001952 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001953 case ETHTOOL_GRXRINGS:
1954 case ETHTOOL_GRXCLSRLCNT:
1955 case ETHTOOL_GRXCLSRULE:
1956 case ETHTOOL_GRXCLSRLALL:
Michał Mirosław5455c692011-02-15 16:59:17 +00001957 case ETHTOOL_GFEATURES:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001958 break;
1959 default:
1960 if (!capable(CAP_NET_ADMIN))
1961 return -EPERM;
1962 }
1963
chavey97f8aef2010-04-07 21:54:42 -07001964 if (dev->ethtool_ops->begin) {
1965 rc = dev->ethtool_ops->begin(dev);
1966 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001968 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001969 old_features = dev->features;
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 switch (ethcmd) {
1972 case ETHTOOL_GSET:
1973 rc = ethtool_get_settings(dev, useraddr);
1974 break;
1975 case ETHTOOL_SSET:
1976 rc = ethtool_set_settings(dev, useraddr);
1977 break;
1978 case ETHTOOL_GDRVINFO:
1979 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
1981 case ETHTOOL_GREGS:
1982 rc = ethtool_get_regs(dev, useraddr);
1983 break;
1984 case ETHTOOL_GWOL:
1985 rc = ethtool_get_wol(dev, useraddr);
1986 break;
1987 case ETHTOOL_SWOL:
1988 rc = ethtool_set_wol(dev, useraddr);
1989 break;
1990 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001991 rc = ethtool_get_value(dev, useraddr, ethcmd,
1992 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001995 rc = ethtool_set_value_void(dev, useraddr,
1996 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 case ETHTOOL_NWAY_RST:
1999 rc = ethtool_nway_reset(dev);
2000 break;
2001 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002002 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 case ETHTOOL_GEEPROM:
2005 rc = ethtool_get_eeprom(dev, useraddr);
2006 break;
2007 case ETHTOOL_SEEPROM:
2008 rc = ethtool_set_eeprom(dev, useraddr);
2009 break;
2010 case ETHTOOL_GCOALESCE:
2011 rc = ethtool_get_coalesce(dev, useraddr);
2012 break;
2013 case ETHTOOL_SCOALESCE:
2014 rc = ethtool_set_coalesce(dev, useraddr);
2015 break;
2016 case ETHTOOL_GRINGPARAM:
2017 rc = ethtool_get_ringparam(dev, useraddr);
2018 break;
2019 case ETHTOOL_SRINGPARAM:
2020 rc = ethtool_set_ringparam(dev, useraddr);
2021 break;
2022 case ETHTOOL_GPAUSEPARAM:
2023 rc = ethtool_get_pauseparam(dev, useraddr);
2024 break;
2025 case ETHTOOL_SPAUSEPARAM:
2026 rc = ethtool_set_pauseparam(dev, useraddr);
2027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 case ETHTOOL_TEST:
2029 rc = ethtool_self_test(dev, useraddr);
2030 break;
2031 case ETHTOOL_GSTRINGS:
2032 rc = ethtool_get_strings(dev, useraddr);
2033 break;
2034 case ETHTOOL_PHYS_ID:
2035 rc = ethtool_phys_id(dev, useraddr);
2036 break;
2037 case ETHTOOL_GSTATS:
2038 rc = ethtool_get_stats(dev, useraddr);
2039 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002040 case ETHTOOL_GPERMADDR:
2041 rc = ethtool_get_perm_addr(dev, useraddr);
2042 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002043 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002044 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00002045 (dev->ethtool_ops->get_flags ?
2046 dev->ethtool_ops->get_flags :
2047 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002048 break;
2049 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002050 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002051 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002052 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002053 rc = ethtool_get_value(dev, useraddr, ethcmd,
2054 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002055 break;
2056 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002057 rc = ethtool_set_value(dev, useraddr,
2058 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002059 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002060 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002061 case ETHTOOL_GRXRINGS:
2062 case ETHTOOL_GRXCLSRLCNT:
2063 case ETHTOOL_GRXCLSRULE:
2064 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002065 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002066 break;
2067 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002068 case ETHTOOL_SRXCLSRLDEL:
2069 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002070 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002071 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002072 case ETHTOOL_FLASHDEV:
2073 rc = ethtool_flash_device(dev, useraddr);
2074 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002075 case ETHTOOL_RESET:
2076 rc = ethtool_reset(dev, useraddr);
2077 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08002078 case ETHTOOL_SRXNTUPLE:
2079 rc = ethtool_set_rx_ntuple(dev, useraddr);
2080 break;
2081 case ETHTOOL_GRXNTUPLE:
2082 rc = ethtool_get_rx_ntuple(dev, useraddr);
2083 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002084 case ETHTOOL_GSSET_INFO:
2085 rc = ethtool_get_sset_info(dev, useraddr);
2086 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002087 case ETHTOOL_GRXFHINDIR:
2088 rc = ethtool_get_rxfh_indir(dev, useraddr);
2089 break;
2090 case ETHTOOL_SRXFHINDIR:
2091 rc = ethtool_set_rxfh_indir(dev, useraddr);
2092 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002093 case ETHTOOL_GFEATURES:
2094 rc = ethtool_get_features(dev, useraddr);
2095 break;
2096 case ETHTOOL_SFEATURES:
2097 rc = ethtool_set_features(dev, useraddr);
2098 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002099 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002100 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002101 case ETHTOOL_GSG:
2102 case ETHTOOL_GTSO:
2103 case ETHTOOL_GUFO:
2104 case ETHTOOL_GGSO:
2105 case ETHTOOL_GGRO:
2106 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2107 break;
2108 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002109 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002110 case ETHTOOL_SSG:
2111 case ETHTOOL_STSO:
2112 case ETHTOOL_SUFO:
2113 case ETHTOOL_SGSO:
2114 case ETHTOOL_SGRO:
2115 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2116 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002117 case ETHTOOL_GCHANNELS:
2118 rc = ethtool_get_channels(dev, useraddr);
2119 break;
2120 case ETHTOOL_SCHANNELS:
2121 rc = ethtool_set_channels(dev, useraddr);
2122 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002123 case ETHTOOL_SET_DUMP:
2124 rc = ethtool_set_dump(dev, useraddr);
2125 break;
2126 case ETHTOOL_GET_DUMP_FLAG:
2127 rc = ethtool_get_dump_flag(dev, useraddr);
2128 break;
2129 case ETHTOOL_GET_DUMP_DATA:
2130 rc = ethtool_get_dump_data(dev, useraddr);
2131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002133 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002135
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002136 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002138
2139 if (old_features != dev->features)
2140 netdev_features_change(dev);
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}