blob: fd14116ad7f0626aca22412e3a02f1f0b9830d7d [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
Michał Mirosławfd0daf92011-05-26 00:42:57 +0000236static int ethtool_set_flags_compat(struct net_device *dev,
237 int (*legacy_set)(struct net_device *, u32),
238 struct ethtool_set_features_block *features, u32 mask)
239{
240 u32 value;
241
242 if (!legacy_set)
243 return 0;
244
245 if (!(features[0].valid & mask))
246 return 0;
247
248 value = dev->features & ~features[0].valid;
249 value |= features[0].requested;
250
251 features[0].valid &= ~mask;
252
253 if (legacy_set(dev, value & mask) < 0)
254 netdev_info(dev, "Legacy flags change failed\n");
255
256 return 1;
257}
258
Michał Mirosław39fc0ce2011-02-22 16:52:29 +0000259static int ethtool_set_features_compat(struct net_device *dev,
260 struct ethtool_set_features_block *features)
261{
262 int compat;
263
264 if (!dev->ethtool_ops)
265 return 0;
266
267 compat = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
268 features, NETIF_F_SG);
269 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
270 features, NETIF_F_ALL_CSUM);
271 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
272 features, NETIF_F_ALL_TSO);
273 compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
274 features, NETIF_F_RXCSUM);
Michał Mirosławfd0daf92011-05-26 00:42:57 +0000275 compat |= ethtool_set_flags_compat(dev, dev->ethtool_ops->set_flags,
Michał Mirosław39fc0ce2011-02-22 16:52:29 +0000276 features, flags_dup_features);
277
278 return compat;
Michał Mirosław4e4db202011-02-22 16:52:28 +0000279}
280
Michał Mirosław5455c692011-02-15 16:59:17 +0000281static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
282{
283 struct ethtool_gfeatures cmd = {
284 .cmd = ETHTOOL_GFEATURES,
285 .size = ETHTOOL_DEV_FEATURE_WORDS,
286 };
287 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
288 {
289 .available = dev->hw_features,
290 .requested = dev->wanted_features,
291 .active = dev->features,
292 .never_changed = NETIF_F_NEVER_CHANGE,
293 },
294 };
295 u32 __user *sizeaddr;
296 u32 copy_size;
297
Michał Mirosław4e4db202011-02-22 16:52:28 +0000298 ethtool_get_features_compat(dev, features);
299
Michał Mirosław5455c692011-02-15 16:59:17 +0000300 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
301 if (get_user(copy_size, sizeaddr))
302 return -EFAULT;
303
304 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
305 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
306
307 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
308 return -EFAULT;
309 useraddr += sizeof(cmd);
310 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
311 return -EFAULT;
312
313 return 0;
314}
315
316static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
317{
318 struct ethtool_sfeatures cmd;
319 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
320 int ret = 0;
321
322 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
323 return -EFAULT;
324 useraddr += sizeof(cmd);
325
326 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
327 return -EINVAL;
328
329 if (copy_from_user(features, useraddr, sizeof(features)))
330 return -EFAULT;
331
332 if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
333 return -EINVAL;
334
Michał Mirosław39fc0ce2011-02-22 16:52:29 +0000335 if (ethtool_set_features_compat(dev, features))
336 ret |= ETHTOOL_F_COMPAT;
337
Michał Mirosław5455c692011-02-15 16:59:17 +0000338 if (features[0].valid & ~dev->hw_features) {
339 features[0].valid &= dev->hw_features;
340 ret |= ETHTOOL_F_UNSUPPORTED;
341 }
342
343 dev->wanted_features &= ~features[0].valid;
344 dev->wanted_features |= features[0].valid & features[0].requested;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700345 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000346
347 if ((dev->wanted_features ^ dev->features) & features[0].valid)
348 ret |= ETHTOOL_F_WISH;
349
350 return ret;
351}
352
353static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
354 /* NETIF_F_SG */ "tx-scatter-gather",
355 /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
356 /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
357 /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
Michał Mirosław7cc31a92011-05-17 16:50:02 -0400358 /* NETIF_F_IPV6_CSUM */ "tx-checksum-ipv6",
Michał Mirosław5455c692011-02-15 16:59:17 +0000359 /* NETIF_F_HIGHDMA */ "highdma",
360 /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
361 /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
362
363 /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
364 /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
365 /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
366 /* NETIF_F_GSO */ "tx-generic-segmentation",
367 /* NETIF_F_LLTX */ "tx-lockless",
368 /* NETIF_F_NETNS_LOCAL */ "netns-local",
369 /* NETIF_F_GRO */ "rx-gro",
370 /* NETIF_F_LRO */ "rx-lro",
371
372 /* NETIF_F_TSO */ "tx-tcp-segmentation",
373 /* NETIF_F_UFO */ "tx-udp-fragmentation",
374 /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
375 /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
376 /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
377 /* NETIF_F_FSO */ "tx-fcoe-segmentation",
378 "",
379 "",
380
381 /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
382 /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
383 /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
384 /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
385 /* NETIF_F_RXHASH */ "rx-hashing",
Michał Mirosławe83d3602011-02-15 16:59:18 +0000386 /* NETIF_F_RXCSUM */ "rx-checksum",
Franco Fichtner864b54182011-05-12 06:42:04 +0000387 /* NETIF_F_NOCACHE_COPY */ "tx-nocache-copy",
Mahesh Bandewareed2a122011-05-04 15:30:11 +0000388 /* NETIF_F_LOOPBACK */ "loopback",
Michał Mirosław5455c692011-02-15 16:59:17 +0000389};
390
Michał Mirosław340ae162011-02-15 16:59:16 +0000391static int __ethtool_get_sset_count(struct net_device *dev, int sset)
392{
393 const struct ethtool_ops *ops = dev->ethtool_ops;
394
Michał Mirosław5455c692011-02-15 16:59:17 +0000395 if (sset == ETH_SS_FEATURES)
396 return ARRAY_SIZE(netdev_features_strings);
397
Michał Mirosław340ae162011-02-15 16:59:16 +0000398 if (ops && ops->get_sset_count && ops->get_strings)
399 return ops->get_sset_count(dev, sset);
400 else
401 return -EOPNOTSUPP;
402}
403
404static void __ethtool_get_strings(struct net_device *dev,
405 u32 stringset, u8 *data)
406{
407 const struct ethtool_ops *ops = dev->ethtool_ops;
408
Michał Mirosław5455c692011-02-15 16:59:17 +0000409 if (stringset == ETH_SS_FEATURES)
410 memcpy(data, netdev_features_strings,
411 sizeof(netdev_features_strings));
412 else
413 /* ops->get_strings is valid because checked earlier */
414 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000415}
416
Michał Mirosław0a417702011-02-15 16:59:17 +0000417static u32 ethtool_get_feature_mask(u32 eth_cmd)
418{
419 /* feature masks of legacy discrete ethtool ops */
420
421 switch (eth_cmd) {
422 case ETHTOOL_GTXCSUM:
423 case ETHTOOL_STXCSUM:
424 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000425 case ETHTOOL_GRXCSUM:
426 case ETHTOOL_SRXCSUM:
427 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000428 case ETHTOOL_GSG:
429 case ETHTOOL_SSG:
430 return NETIF_F_SG;
431 case ETHTOOL_GTSO:
432 case ETHTOOL_STSO:
433 return NETIF_F_ALL_TSO;
434 case ETHTOOL_GUFO:
435 case ETHTOOL_SUFO:
436 return NETIF_F_UFO;
437 case ETHTOOL_GGSO:
438 case ETHTOOL_SGSO:
439 return NETIF_F_GSO;
440 case ETHTOOL_GGRO:
441 case ETHTOOL_SGRO:
442 return NETIF_F_GRO;
443 default:
444 BUG();
445 }
446}
447
448static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
449{
450 const struct ethtool_ops *ops = dev->ethtool_ops;
451
452 if (!ops)
453 return NULL;
454
455 switch (ethcmd) {
456 case ETHTOOL_GTXCSUM:
457 return ops->get_tx_csum;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000458 case ETHTOOL_GRXCSUM:
459 return ops->get_rx_csum;
Michał Mirosław0a417702011-02-15 16:59:17 +0000460 case ETHTOOL_SSG:
461 return ops->get_sg;
462 case ETHTOOL_STSO:
463 return ops->get_tso;
464 case ETHTOOL_SUFO:
465 return ops->get_ufo;
466 default:
467 return NULL;
468 }
469}
470
Michał Mirosławe83d3602011-02-15 16:59:18 +0000471static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
472{
473 return !!(dev->features & NETIF_F_ALL_CSUM);
474}
475
Michał Mirosław0a417702011-02-15 16:59:17 +0000476static int ethtool_get_one_feature(struct net_device *dev,
477 char __user *useraddr, u32 ethcmd)
478{
Michał Mirosław86794882011-02-15 16:59:17 +0000479 u32 mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000480 struct ethtool_value edata = {
481 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000482 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000483 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000484
Michał Mirosław86794882011-02-15 16:59:17 +0000485 /* compatibility with discrete get_ ops */
486 if (!(dev->hw_features & mask)) {
487 u32 (*actor)(struct net_device *);
488
489 actor = __ethtool_get_one_feature_actor(dev, ethcmd);
490
Michał Mirosławe83d3602011-02-15 16:59:18 +0000491 /* bug compatibility with old get_rx_csum */
492 if (ethcmd == ETHTOOL_GRXCSUM && !actor)
493 actor = __ethtool_get_rx_csum_oldbug;
494
Michał Mirosław86794882011-02-15 16:59:17 +0000495 if (actor)
496 edata.data = actor(dev);
497 }
Michał Mirosław0a417702011-02-15 16:59:17 +0000498
499 if (copy_to_user(useraddr, &edata, sizeof(edata)))
500 return -EFAULT;
501 return 0;
502}
503
504static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
Michał Mirosławe83d3602011-02-15 16:59:18 +0000505static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
Michał Mirosław0a417702011-02-15 16:59:17 +0000506static int __ethtool_set_sg(struct net_device *dev, u32 data);
507static int __ethtool_set_tso(struct net_device *dev, u32 data);
508static int __ethtool_set_ufo(struct net_device *dev, u32 data);
509
510static int ethtool_set_one_feature(struct net_device *dev,
511 void __user *useraddr, u32 ethcmd)
512{
513 struct ethtool_value edata;
514 u32 mask;
515
516 if (copy_from_user(&edata, useraddr, sizeof(edata)))
517 return -EFAULT;
518
Michał Mirosław86794882011-02-15 16:59:17 +0000519 mask = ethtool_get_feature_mask(ethcmd);
520 mask &= dev->hw_features;
521 if (mask) {
522 if (edata.data)
523 dev->wanted_features |= mask;
524 else
525 dev->wanted_features &= ~mask;
526
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700527 __netdev_update_features(dev);
Michał Mirosław86794882011-02-15 16:59:17 +0000528 return 0;
529 }
530
531 /* Driver is not converted to ndo_fix_features or does not
532 * support changing this offload. In the latter case it won't
533 * have corresponding ethtool_ops field set.
534 *
535 * Following part is to be removed after all drivers advertise
536 * their changeable features in netdev->hw_features and stop
537 * using discrete offload setting ops.
538 */
539
Michał Mirosław0a417702011-02-15 16:59:17 +0000540 switch (ethcmd) {
541 case ETHTOOL_STXCSUM:
542 return __ethtool_set_tx_csum(dev, edata.data);
Michał Mirosławe83d3602011-02-15 16:59:18 +0000543 case ETHTOOL_SRXCSUM:
544 return __ethtool_set_rx_csum(dev, edata.data);
Michał Mirosław0a417702011-02-15 16:59:17 +0000545 case ETHTOOL_SSG:
546 return __ethtool_set_sg(dev, edata.data);
547 case ETHTOOL_STSO:
548 return __ethtool_set_tso(dev, edata.data);
549 case ETHTOOL_SUFO:
550 return __ethtool_set_ufo(dev, edata.data);
Michał Mirosław0a417702011-02-15 16:59:17 +0000551 default:
552 return -EOPNOTSUPP;
553 }
554}
555
Michał Mirosław27660512011-03-18 16:56:34 +0000556int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000557{
558 u32 changed;
559
560 if (data & ~flags_dup_features)
561 return -EINVAL;
562
563 /* legacy set_flags() op */
564 if (dev->ethtool_ops->set_flags) {
565 if (unlikely(dev->hw_features & flags_dup_features))
566 netdev_warn(dev,
567 "driver BUG: mixed hw_features and set_flags()\n");
568 return dev->ethtool_ops->set_flags(dev, data);
569 }
570
571 /* allow changing only bits set in hw_features */
Michał Mirosław5f8629c2011-04-21 13:59:21 +0000572 changed = (data ^ dev->features) & flags_dup_features;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000573 if (changed & ~dev->hw_features)
574 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
575
576 dev->wanted_features =
Michał Mirosław5f8629c2011-04-21 13:59:21 +0000577 (dev->wanted_features & ~changed) | (data & dev->hw_features);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000578
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700579 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000580
581 return 0;
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
585{
Roland Dreier8e557422010-02-11 12:14:23 -0800586 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int err;
588
589 if (!dev->ethtool_ops->get_settings)
590 return -EOPNOTSUPP;
591
592 err = dev->ethtool_ops->get_settings(dev, &cmd);
593 if (err < 0)
594 return err;
595
596 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
597 return -EFAULT;
598 return 0;
599}
600
601static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
602{
603 struct ethtool_cmd cmd;
604
605 if (!dev->ethtool_ops->set_settings)
606 return -EOPNOTSUPP;
607
608 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
609 return -EFAULT;
610
611 return dev->ethtool_ops->set_settings(dev, &cmd);
612}
613
chavey97f8aef2010-04-07 21:54:42 -0700614static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
615 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700618 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 memset(&info, 0, sizeof(info));
621 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700622 if (ops && ops->get_drvinfo) {
623 ops->get_drvinfo(dev, &info);
624 } else if (dev->dev.parent && dev->dev.parent->driver) {
625 strlcpy(info.bus_info, dev_name(dev->dev.parent),
626 sizeof(info.bus_info));
627 strlcpy(info.driver, dev->dev.parent->driver->name,
628 sizeof(info.driver));
629 } else {
630 return -EOPNOTSUPP;
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jeff Garzik723b2f52010-03-03 22:51:50 +0000633 /*
634 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000635 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000636 */
Ben Hutchings01414802010-08-17 02:31:15 -0700637 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700638 int rc;
639
640 rc = ops->get_sset_count(dev, ETH_SS_TEST);
641 if (rc >= 0)
642 info.testinfo_len = rc;
643 rc = ops->get_sset_count(dev, ETH_SS_STATS);
644 if (rc >= 0)
645 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700646 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
647 if (rc >= 0)
648 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700649 }
Ben Hutchings01414802010-08-17 02:31:15 -0700650 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700652 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 info.eedump_len = ops->get_eeprom_len(dev);
654
655 if (copy_to_user(useraddr, &info, sizeof(info)))
656 return -EFAULT;
657 return 0;
658}
659
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800660static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700661 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000662{
663 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000664 u64 sset_mask;
665 int i, idx = 0, n_bits = 0, ret, rc;
666 u32 *info_buf = NULL;
667
Jeff Garzik723b2f52010-03-03 22:51:50 +0000668 if (copy_from_user(&info, useraddr, sizeof(info)))
669 return -EFAULT;
670
671 /* store copy of mask, because we zero struct later on */
672 sset_mask = info.sset_mask;
673 if (!sset_mask)
674 return 0;
675
676 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000677 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000678
679 memset(&info, 0, sizeof(info));
680 info.cmd = ETHTOOL_GSSET_INFO;
681
682 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
683 if (!info_buf)
684 return -ENOMEM;
685
686 /*
687 * fill return buffer based on input bitmask and successful
688 * get_sset_count return
689 */
690 for (i = 0; i < 64; i++) {
691 if (!(sset_mask & (1ULL << i)))
692 continue;
693
Michał Mirosław340ae162011-02-15 16:59:16 +0000694 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000695 if (rc >= 0) {
696 info.sset_mask |= (1ULL << i);
697 info_buf[idx++] = rc;
698 }
699 }
700
701 ret = -EFAULT;
702 if (copy_to_user(useraddr, &info, sizeof(info)))
703 goto out;
704
705 useraddr += offsetof(struct ethtool_sset_info, data);
706 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
707 goto out;
708
709 ret = 0;
710
711out:
712 kfree(info_buf);
713 return ret;
714}
715
chavey97f8aef2010-04-07 21:54:42 -0700716static noinline_for_stack int ethtool_set_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{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000719 struct ethtool_rxnfc info;
720 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700721
Santwona Behera59089d82009-02-20 00:58:13 -0800722 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700723 return -EOPNOTSUPP;
724
Ben Hutchingsbf988432010-06-28 08:45:58 +0000725 /* struct ethtool_rxnfc was originally defined for
726 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
727 * members. User-space might still be using that
728 * definition. */
729 if (cmd == ETHTOOL_SRXFH)
730 info_size = (offsetof(struct ethtool_rxnfc, data) +
731 sizeof(info.data));
732
733 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700734 return -EFAULT;
735
Ben Hutchingsbf988432010-06-28 08:45:58 +0000736 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700737}
738
chavey97f8aef2010-04-07 21:54:42 -0700739static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000740 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700741{
742 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000743 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800744 const struct ethtool_ops *ops = dev->ethtool_ops;
745 int ret;
746 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700747
Santwona Behera59089d82009-02-20 00:58:13 -0800748 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700749 return -EOPNOTSUPP;
750
Ben Hutchingsbf988432010-06-28 08:45:58 +0000751 /* struct ethtool_rxnfc was originally defined for
752 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
753 * members. User-space might still be using that
754 * definition. */
755 if (cmd == ETHTOOL_GRXFH)
756 info_size = (offsetof(struct ethtool_rxnfc, data) +
757 sizeof(info.data));
758
759 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700760 return -EFAULT;
761
Santwona Behera59089d82009-02-20 00:58:13 -0800762 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
763 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000764 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000765 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000766 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800767 if (!rule_buf)
768 return -ENOMEM;
769 }
770 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700771
Santwona Behera59089d82009-02-20 00:58:13 -0800772 ret = ops->get_rxnfc(dev, &info, rule_buf);
773 if (ret < 0)
774 goto err_out;
775
776 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000777 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800778 goto err_out;
779
780 if (rule_buf) {
781 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
782 if (copy_to_user(useraddr, rule_buf,
783 info.rule_cnt * sizeof(u32)))
784 goto err_out;
785 }
786 ret = 0;
787
788err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700789 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800790
791 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700792}
793
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000794static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
795 void __user *useraddr)
796{
797 struct ethtool_rxfh_indir *indir;
798 u32 table_size;
799 size_t full_size;
800 int ret;
801
802 if (!dev->ethtool_ops->get_rxfh_indir)
803 return -EOPNOTSUPP;
804
805 if (copy_from_user(&table_size,
806 useraddr + offsetof(struct ethtool_rxfh_indir, size),
807 sizeof(table_size)))
808 return -EFAULT;
809
810 if (table_size >
811 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
812 return -ENOMEM;
813 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
Kees Cookb00916b2010-10-11 12:23:25 -0700814 indir = kzalloc(full_size, GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000815 if (!indir)
816 return -ENOMEM;
817
818 indir->cmd = ETHTOOL_GRXFHINDIR;
819 indir->size = table_size;
820 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
821 if (ret)
822 goto out;
823
824 if (copy_to_user(useraddr, indir, full_size))
825 ret = -EFAULT;
826
827out:
828 kfree(indir);
829 return ret;
830}
831
832static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
833 void __user *useraddr)
834{
835 struct ethtool_rxfh_indir *indir;
836 u32 table_size;
837 size_t full_size;
838 int ret;
839
840 if (!dev->ethtool_ops->set_rxfh_indir)
841 return -EOPNOTSUPP;
842
843 if (copy_from_user(&table_size,
844 useraddr + offsetof(struct ethtool_rxfh_indir, size),
845 sizeof(table_size)))
846 return -EFAULT;
847
848 if (table_size >
849 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
850 return -ENOMEM;
851 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
852 indir = kmalloc(full_size, GFP_USER);
853 if (!indir)
854 return -ENOMEM;
855
856 if (copy_from_user(indir, useraddr, full_size)) {
857 ret = -EFAULT;
858 goto out;
859 }
860
861 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
862
863out:
864 kfree(indir);
865 return ret;
866}
867
Peter Waskiewicze8589112010-02-12 13:48:05 +0000868static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700869 struct ethtool_rx_ntuple_flow_spec *spec,
870 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800871{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800872
873 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000874 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
875 /* free the container */
876 kfree(fsc);
877 return;
878 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800879
880 /* Copy the whole filter over */
881 fsc->fs.flow_type = spec->flow_type;
882 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
883 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
884
885 fsc->fs.vlan_tag = spec->vlan_tag;
886 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
887 fsc->fs.data = spec->data;
888 fsc->fs.data_mask = spec->data_mask;
889 fsc->fs.action = spec->action;
890
891 /* add to the list */
892 list_add_tail_rcu(&fsc->list, &list->list);
893 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800894}
895
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000896/*
897 * ethtool does not (or did not) set masks for flow parameters that are
898 * not specified, so if both value and mask are 0 then this must be
899 * treated as equivalent to a mask with all bits set. Implement that
900 * here rather than in drivers.
901 */
902static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
903{
904 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
905 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
906
907 if (fs->flow_type != TCP_V4_FLOW &&
908 fs->flow_type != UDP_V4_FLOW &&
909 fs->flow_type != SCTP_V4_FLOW)
910 return;
911
912 if (!(entry->ip4src | mask->ip4src))
913 mask->ip4src = htonl(0xffffffff);
914 if (!(entry->ip4dst | mask->ip4dst))
915 mask->ip4dst = htonl(0xffffffff);
916 if (!(entry->psrc | mask->psrc))
917 mask->psrc = htons(0xffff);
918 if (!(entry->pdst | mask->pdst))
919 mask->pdst = htons(0xffff);
920 if (!(entry->tos | mask->tos))
921 mask->tos = 0xff;
922 if (!(fs->vlan_tag | fs->vlan_tag_mask))
923 fs->vlan_tag_mask = 0xffff;
924 if (!(fs->data | fs->data_mask))
925 fs->data_mask = 0xffffffffffffffffULL;
926}
927
chavey97f8aef2010-04-07 21:54:42 -0700928static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
929 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800930{
931 struct ethtool_rx_ntuple cmd;
932 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000933 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800934 int ret;
935
Alexander Duyck5d9f11c2011-04-08 12:07:22 +0000936 if (!ops->set_rx_ntuple)
937 return -EOPNOTSUPP;
938
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800939 if (!(dev->features & NETIF_F_NTUPLE))
940 return -EINVAL;
941
942 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
943 return -EFAULT;
944
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000945 rx_ntuple_fix_masks(&cmd.fs);
946
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800947 /*
948 * Cache filter in dev struct for GET operation only if
949 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000950 * only if the filter was added successfully. First make sure we
951 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800952 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000953 if (!ops->get_rx_ntuple) {
954 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
955 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800956 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000957 }
958
959 ret = ops->set_rx_ntuple(dev, &cmd);
960 if (ret) {
961 kfree(fsc);
962 return ret;
963 }
964
965 if (!ops->get_rx_ntuple)
966 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800967
968 return ret;
969}
970
971static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
972{
973 struct ethtool_gstrings gstrings;
974 const struct ethtool_ops *ops = dev->ethtool_ops;
975 struct ethtool_rx_ntuple_flow_spec_container *fsc;
976 u8 *data;
977 char *p;
978 int ret, i, num_strings = 0;
979
980 if (!ops->get_sset_count)
981 return -EOPNOTSUPP;
982
983 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
984 return -EFAULT;
985
986 ret = ops->get_sset_count(dev, gstrings.string_set);
987 if (ret < 0)
988 return ret;
989
990 gstrings.len = ret;
991
Kees Cookb00916b2010-10-11 12:23:25 -0700992 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800993 if (!data)
994 return -ENOMEM;
995
996 if (ops->get_rx_ntuple) {
997 /* driver-specific filter grab */
998 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
999 goto copy;
1000 }
1001
1002 /* default ethtool filter grab */
1003 i = 0;
1004 p = (char *)data;
1005 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
1006 sprintf(p, "Filter %d:\n", i);
1007 p += ETH_GSTRING_LEN;
1008 num_strings++;
1009
1010 switch (fsc->fs.flow_type) {
1011 case TCP_V4_FLOW:
1012 sprintf(p, "\tFlow Type: TCP\n");
1013 p += ETH_GSTRING_LEN;
1014 num_strings++;
1015 break;
1016 case UDP_V4_FLOW:
1017 sprintf(p, "\tFlow Type: UDP\n");
1018 p += ETH_GSTRING_LEN;
1019 num_strings++;
1020 break;
1021 case SCTP_V4_FLOW:
1022 sprintf(p, "\tFlow Type: SCTP\n");
1023 p += ETH_GSTRING_LEN;
1024 num_strings++;
1025 break;
1026 case AH_ESP_V4_FLOW:
1027 sprintf(p, "\tFlow Type: AH ESP\n");
1028 p += ETH_GSTRING_LEN;
1029 num_strings++;
1030 break;
1031 case ESP_V4_FLOW:
1032 sprintf(p, "\tFlow Type: ESP\n");
1033 p += ETH_GSTRING_LEN;
1034 num_strings++;
1035 break;
1036 case IP_USER_FLOW:
1037 sprintf(p, "\tFlow Type: Raw IP\n");
1038 p += ETH_GSTRING_LEN;
1039 num_strings++;
1040 break;
1041 case IPV4_FLOW:
1042 sprintf(p, "\tFlow Type: IPv4\n");
1043 p += ETH_GSTRING_LEN;
1044 num_strings++;
1045 break;
1046 default:
1047 sprintf(p, "\tFlow Type: Unknown\n");
1048 p += ETH_GSTRING_LEN;
1049 num_strings++;
1050 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +00001051 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001052
1053 /* now the rest of the filters */
1054 switch (fsc->fs.flow_type) {
1055 case TCP_V4_FLOW:
1056 case UDP_V4_FLOW:
1057 case SCTP_V4_FLOW:
1058 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001059 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001060 p += ETH_GSTRING_LEN;
1061 num_strings++;
1062 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001063 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001064 p += ETH_GSTRING_LEN;
1065 num_strings++;
1066 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001067 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001068 p += ETH_GSTRING_LEN;
1069 num_strings++;
1070 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001071 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001072 p += ETH_GSTRING_LEN;
1073 num_strings++;
1074 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001075 fsc->fs.h_u.tcp_ip4_spec.psrc,
1076 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001077 p += ETH_GSTRING_LEN;
1078 num_strings++;
1079 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001080 fsc->fs.h_u.tcp_ip4_spec.pdst,
1081 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001082 p += ETH_GSTRING_LEN;
1083 num_strings++;
1084 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001085 fsc->fs.h_u.tcp_ip4_spec.tos,
1086 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001087 p += ETH_GSTRING_LEN;
1088 num_strings++;
1089 break;
1090 case AH_ESP_V4_FLOW:
1091 case ESP_V4_FLOW:
1092 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001093 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001094 p += ETH_GSTRING_LEN;
1095 num_strings++;
1096 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001097 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001098 p += ETH_GSTRING_LEN;
1099 num_strings++;
1100 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001101 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001102 p += ETH_GSTRING_LEN;
1103 num_strings++;
1104 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001105 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001106 p += ETH_GSTRING_LEN;
1107 num_strings++;
1108 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001109 fsc->fs.h_u.ah_ip4_spec.spi,
1110 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001111 p += ETH_GSTRING_LEN;
1112 num_strings++;
1113 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001114 fsc->fs.h_u.ah_ip4_spec.tos,
1115 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001116 p += ETH_GSTRING_LEN;
1117 num_strings++;
1118 break;
1119 case IP_USER_FLOW:
1120 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001121 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001122 p += ETH_GSTRING_LEN;
1123 num_strings++;
1124 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001125 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001126 p += ETH_GSTRING_LEN;
1127 num_strings++;
1128 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001129 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001130 p += ETH_GSTRING_LEN;
1131 num_strings++;
1132 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +00001133 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001134 p += ETH_GSTRING_LEN;
1135 num_strings++;
1136 break;
1137 case IPV4_FLOW:
1138 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001139 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001140 p += ETH_GSTRING_LEN;
1141 num_strings++;
1142 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001143 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001144 p += ETH_GSTRING_LEN;
1145 num_strings++;
1146 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001147 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001148 p += ETH_GSTRING_LEN;
1149 num_strings++;
1150 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001151 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001152 p += ETH_GSTRING_LEN;
1153 num_strings++;
1154 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001155 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
1156 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001157 p += ETH_GSTRING_LEN;
1158 num_strings++;
1159 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001160 fsc->fs.h_u.usr_ip4_spec.tos,
1161 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001162 p += ETH_GSTRING_LEN;
1163 num_strings++;
1164 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001165 fsc->fs.h_u.usr_ip4_spec.ip_ver,
1166 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001167 p += ETH_GSTRING_LEN;
1168 num_strings++;
1169 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001170 fsc->fs.h_u.usr_ip4_spec.proto,
1171 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001172 p += ETH_GSTRING_LEN;
1173 num_strings++;
1174 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +00001175 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001176 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -07001177 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001178 p += ETH_GSTRING_LEN;
1179 num_strings++;
1180 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
1181 p += ETH_GSTRING_LEN;
1182 num_strings++;
1183 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
1184 p += ETH_GSTRING_LEN;
1185 num_strings++;
1186 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
1187 sprintf(p, "\tAction: Drop\n");
1188 else
1189 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -07001190 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001191 p += ETH_GSTRING_LEN;
1192 num_strings++;
1193unknown_filter:
1194 i++;
1195 }
1196copy:
1197 /* indicate to userspace how many strings we actually have */
1198 gstrings.len = num_strings;
1199 ret = -EFAULT;
1200 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1201 goto out;
1202 useraddr += sizeof(gstrings);
1203 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1204 goto out;
1205 ret = 0;
1206
1207out:
1208 kfree(data);
1209 return ret;
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1213{
1214 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001215 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 void *regbuf;
1217 int reglen, ret;
1218
1219 if (!ops->get_regs || !ops->get_regs_len)
1220 return -EOPNOTSUPP;
1221
1222 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1223 return -EFAULT;
1224
1225 reglen = ops->get_regs_len(dev);
1226 if (regs.len > reglen)
1227 regs.len = reglen;
1228
Eugene Teob7c7d012011-01-24 21:05:17 -08001229 regbuf = vzalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!regbuf)
1231 return -ENOMEM;
1232
1233 ops->get_regs(dev, &regs, regbuf);
1234
1235 ret = -EFAULT;
1236 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1237 goto out;
1238 useraddr += offsetof(struct ethtool_regs, data);
1239 if (copy_to_user(useraddr, regbuf, regs.len))
1240 goto out;
1241 ret = 0;
1242
1243 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001244 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return ret;
1246}
1247
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001248static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1249{
1250 struct ethtool_value reset;
1251 int ret;
1252
1253 if (!dev->ethtool_ops->reset)
1254 return -EOPNOTSUPP;
1255
1256 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1257 return -EFAULT;
1258
1259 ret = dev->ethtool_ops->reset(dev, &reset.data);
1260 if (ret)
1261 return ret;
1262
1263 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1264 return -EFAULT;
1265 return 0;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1269{
Roland Dreier8e557422010-02-11 12:14:23 -08001270 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (!dev->ethtool_ops->get_wol)
1273 return -EOPNOTSUPP;
1274
1275 dev->ethtool_ops->get_wol(dev, &wol);
1276
1277 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1278 return -EFAULT;
1279 return 0;
1280}
1281
1282static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1283{
1284 struct ethtool_wolinfo wol;
1285
1286 if (!dev->ethtool_ops->set_wol)
1287 return -EOPNOTSUPP;
1288
1289 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1290 return -EFAULT;
1291
1292 return dev->ethtool_ops->set_wol(dev, &wol);
1293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295static int ethtool_nway_reset(struct net_device *dev)
1296{
1297 if (!dev->ethtool_ops->nway_reset)
1298 return -EOPNOTSUPP;
1299
1300 return dev->ethtool_ops->nway_reset(dev);
1301}
1302
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001303static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1304{
1305 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1306
1307 if (!dev->ethtool_ops->get_link)
1308 return -EOPNOTSUPP;
1309
1310 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1311
1312 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1313 return -EFAULT;
1314 return 0;
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1318{
1319 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001320 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001321 void __user *userbuf = useraddr + sizeof(eeprom);
1322 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 u8 *data;
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001324 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (!ops->get_eeprom || !ops->get_eeprom_len)
1327 return -EOPNOTSUPP;
1328
1329 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1330 return -EFAULT;
1331
1332 /* Check for wrap and zero */
1333 if (eeprom.offset + eeprom.len <= eeprom.offset)
1334 return -EINVAL;
1335
1336 /* Check for exceeding total eeprom len */
1337 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1338 return -EINVAL;
1339
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001340 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!data)
1342 return -ENOMEM;
1343
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001344 bytes_remaining = eeprom.len;
1345 while (bytes_remaining > 0) {
1346 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001348 ret = ops->get_eeprom(dev, &eeprom, data);
1349 if (ret)
1350 break;
1351 if (copy_to_user(userbuf, data, eeprom.len)) {
1352 ret = -EFAULT;
1353 break;
1354 }
1355 userbuf += eeprom.len;
1356 eeprom.offset += eeprom.len;
1357 bytes_remaining -= eeprom.len;
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001360 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1361 eeprom.offset -= eeprom.len;
1362 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1363 ret = -EFAULT;
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 kfree(data);
1366 return ret;
1367}
1368
1369static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1370{
1371 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001372 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001373 void __user *userbuf = useraddr + sizeof(eeprom);
1374 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 u8 *data;
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001376 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 if (!ops->set_eeprom || !ops->get_eeprom_len)
1379 return -EOPNOTSUPP;
1380
1381 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1382 return -EFAULT;
1383
1384 /* Check for wrap and zero */
1385 if (eeprom.offset + eeprom.len <= eeprom.offset)
1386 return -EINVAL;
1387
1388 /* Check for exceeding total eeprom len */
1389 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1390 return -EINVAL;
1391
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001392 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (!data)
1394 return -ENOMEM;
1395
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001396 bytes_remaining = eeprom.len;
1397 while (bytes_remaining > 0) {
1398 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Mandeep Singh Bainesb131dd5d2008-04-15 19:24:17 -07001400 if (copy_from_user(data, userbuf, eeprom.len)) {
1401 ret = -EFAULT;
1402 break;
1403 }
1404 ret = ops->set_eeprom(dev, &eeprom, data);
1405 if (ret)
1406 break;
1407 userbuf += eeprom.len;
1408 eeprom.offset += eeprom.len;
1409 bytes_remaining -= eeprom.len;
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 kfree(data);
1413 return ret;
1414}
1415
chavey97f8aef2010-04-07 21:54:42 -07001416static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1417 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Roland Dreier8e557422010-02-11 12:14:23 -08001419 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 if (!dev->ethtool_ops->get_coalesce)
1422 return -EOPNOTSUPP;
1423
1424 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1425
1426 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1427 return -EFAULT;
1428 return 0;
1429}
1430
chavey97f8aef2010-04-07 21:54:42 -07001431static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1432 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct ethtool_coalesce coalesce;
1435
David S. Millerfa04ae52005-06-06 15:07:19 -07001436 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -EOPNOTSUPP;
1438
1439 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1440 return -EFAULT;
1441
1442 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1443}
1444
1445static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1446{
Roland Dreier8e557422010-02-11 12:14:23 -08001447 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (!dev->ethtool_ops->get_ringparam)
1450 return -EOPNOTSUPP;
1451
1452 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1453
1454 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1455 return -EFAULT;
1456 return 0;
1457}
1458
1459static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1460{
1461 struct ethtool_ringparam ringparam;
1462
1463 if (!dev->ethtool_ops->set_ringparam)
1464 return -EOPNOTSUPP;
1465
1466 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1467 return -EFAULT;
1468
1469 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1470}
1471
amit salecha8b5933c2011-04-07 01:58:42 +00001472static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1473 void __user *useraddr)
1474{
1475 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1476
1477 if (!dev->ethtool_ops->get_channels)
1478 return -EOPNOTSUPP;
1479
1480 dev->ethtool_ops->get_channels(dev, &channels);
1481
1482 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1483 return -EFAULT;
1484 return 0;
1485}
1486
1487static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1488 void __user *useraddr)
1489{
1490 struct ethtool_channels channels;
1491
1492 if (!dev->ethtool_ops->set_channels)
1493 return -EOPNOTSUPP;
1494
1495 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1496 return -EFAULT;
1497
1498 return dev->ethtool_ops->set_channels(dev, &channels);
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1502{
1503 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1504
1505 if (!dev->ethtool_ops->get_pauseparam)
1506 return -EOPNOTSUPP;
1507
1508 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1509
1510 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1511 return -EFAULT;
1512 return 0;
1513}
1514
1515static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1516{
1517 struct ethtool_pauseparam pauseparam;
1518
Jeff Garzike1b90c42006-07-17 12:54:40 -04001519 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -EOPNOTSUPP;
1521
1522 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1523 return -EFAULT;
1524
1525 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528static int __ethtool_set_sg(struct net_device *dev, u32 data)
1529{
1530 int err;
1531
Roger Luethi5e5069b2011-03-17 06:37:21 +00001532 if (!dev->ethtool_ops->set_sg)
1533 return -EOPNOTSUPP;
1534
Michał Mirosław0a417702011-02-15 16:59:17 +00001535 if (data && !(dev->features & NETIF_F_ALL_CSUM))
1536 return -EINVAL;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (!data && dev->ethtool_ops->set_tso) {
1539 err = dev->ethtool_ops->set_tso(dev, 0);
1540 if (err)
1541 return err;
1542 }
1543
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001544 if (!data && dev->ethtool_ops->set_ufo) {
1545 err = dev->ethtool_ops->set_ufo(dev, 0);
1546 if (err)
1547 return err;
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return dev->ethtool_ops->set_sg(dev, data);
1550}
1551
Michał Mirosław0a417702011-02-15 16:59:17 +00001552static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int err;
1555
1556 if (!dev->ethtool_ops->set_tx_csum)
1557 return -EOPNOTSUPP;
1558
Michał Mirosław0a417702011-02-15 16:59:17 +00001559 if (!data && dev->ethtool_ops->set_sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 err = __ethtool_set_sg(dev, 0);
1561 if (err)
1562 return err;
1563 }
1564
Michał Mirosław0a417702011-02-15 16:59:17 +00001565 return dev->ethtool_ops->set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Michał Mirosławe83d3602011-02-15 16:59:18 +00001568static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
Herbert Xub240a0e2008-12-15 23:44:31 -08001569{
Herbert Xub240a0e2008-12-15 23:44:31 -08001570 if (!dev->ethtool_ops->set_rx_csum)
1571 return -EOPNOTSUPP;
1572
Michał Mirosławe83d3602011-02-15 16:59:18 +00001573 if (!data)
Herbert Xub240a0e2008-12-15 23:44:31 -08001574 dev->features &= ~NETIF_F_GRO;
1575
Michał Mirosławe83d3602011-02-15 16:59:18 +00001576 return dev->ethtool_ops->set_rx_csum(dev, data);
Herbert Xub240a0e2008-12-15 23:44:31 -08001577}
1578
Michał Mirosław0a417702011-02-15 16:59:17 +00001579static int __ethtool_set_tso(struct net_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (!dev->ethtool_ops->set_tso)
1582 return -EOPNOTSUPP;
1583
Michał Mirosław0a417702011-02-15 16:59:17 +00001584 if (data && !(dev->features & NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -EINVAL;
1586
Michał Mirosław0a417702011-02-15 16:59:17 +00001587 return dev->ethtool_ops->set_tso(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Michał Mirosław0a417702011-02-15 16:59:17 +00001590static int __ethtool_set_ufo(struct net_device *dev, u32 data)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001591{
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001592 if (!dev->ethtool_ops->set_ufo)
1593 return -EOPNOTSUPP;
Michał Mirosław0a417702011-02-15 16:59:17 +00001594 if (data && !(dev->features & NETIF_F_SG))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001595 return -EINVAL;
Michał Mirosław0a417702011-02-15 16:59:17 +00001596 if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
Michał Mirosław79032642010-11-30 06:38:00 +00001597 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1598 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001599 return -EINVAL;
Michał Mirosław0a417702011-02-15 16:59:17 +00001600 return dev->ethtool_ops->set_ufo(dev, data);
Herbert Xub240a0e2008-12-15 23:44:31 -08001601}
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1604{
1605 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001606 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001608 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001610 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001611 return -EOPNOTSUPP;
1612
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001613 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001614 if (test_len < 0)
1615 return test_len;
1616 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 if (copy_from_user(&test, useraddr, sizeof(test)))
1619 return -EFAULT;
1620
Jeff Garzikff03d492007-08-15 16:01:08 -07001621 test.len = test_len;
1622 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (!data)
1624 return -ENOMEM;
1625
1626 ops->self_test(dev, &test, data);
1627
1628 ret = -EFAULT;
1629 if (copy_to_user(useraddr, &test, sizeof(test)))
1630 goto out;
1631 useraddr += sizeof(test);
1632 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1633 goto out;
1634 ret = 0;
1635
1636 out:
1637 kfree(data);
1638 return ret;
1639}
1640
1641static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1642{
1643 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 u8 *data;
1645 int ret;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1648 return -EFAULT;
1649
Michał Mirosław340ae162011-02-15 16:59:16 +00001650 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001651 if (ret < 0)
1652 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001653
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001654 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1657 if (!data)
1658 return -ENOMEM;
1659
Michał Mirosław340ae162011-02-15 16:59:16 +00001660 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 ret = -EFAULT;
1663 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1664 goto out;
1665 useraddr += sizeof(gstrings);
1666 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1667 goto out;
1668 ret = 0;
1669
Michał Mirosław340ae162011-02-15 16:59:16 +00001670out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 kfree(data);
1672 return ret;
1673}
1674
1675static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1676{
1677 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001678 static bool busy;
1679 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Stephen Hemminger1ab7b6a2011-04-14 23:46:06 -07001681 if (!dev->ethtool_ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return -EOPNOTSUPP;
1683
Ben Hutchings68f512f2011-04-02 00:35:15 +01001684 if (busy)
1685 return -EBUSY;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (copy_from_user(&id, useraddr, sizeof(id)))
1688 return -EFAULT;
1689
Ben Hutchings68f512f2011-04-02 00:35:15 +01001690 rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001691 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001692 return rc;
1693
1694 /* Drop the RTNL lock while waiting, but prevent reentry or
1695 * removal of the device.
1696 */
1697 busy = true;
1698 dev_hold(dev);
1699 rtnl_unlock();
1700
1701 if (rc == 0) {
1702 /* Driver will handle this itself */
1703 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001704 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001705 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001706 /* Driver expects to be called at twice the frequency in rc */
1707 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001708
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001709 /* Count down seconds */
1710 do {
1711 /* Count down iterations per second */
1712 i = n;
1713 do {
1714 rtnl_lock();
1715 rc = dev->ethtool_ops->set_phys_id(dev,
1716 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1717 rtnl_unlock();
1718 if (rc)
1719 break;
1720 schedule_timeout_interruptible(interval);
1721 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001722 } while (!signal_pending(current) &&
1723 (id.data == 0 || --id.data != 0));
1724 }
1725
1726 rtnl_lock();
1727 dev_put(dev);
1728 busy = false;
1729
1730 (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1731 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1735{
1736 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001737 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001739 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001741 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001742 return -EOPNOTSUPP;
1743
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001744 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001745 if (n_stats < 0)
1746 return n_stats;
1747 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1750 return -EFAULT;
1751
Jeff Garzikff03d492007-08-15 16:01:08 -07001752 stats.n_stats = n_stats;
1753 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (!data)
1755 return -ENOMEM;
1756
1757 ops->get_ethtool_stats(dev, &stats, data);
1758
1759 ret = -EFAULT;
1760 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1761 goto out;
1762 useraddr += sizeof(stats);
1763 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1764 goto out;
1765 ret = 0;
1766
1767 out:
1768 kfree(data);
1769 return ret;
1770}
1771
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001772static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001773{
1774 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001775
Matthew Wilcox313674a2007-07-31 14:00:29 -07001776 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001777 return -EFAULT;
1778
Matthew Wilcox313674a2007-07-31 14:00:29 -07001779 if (epaddr.size < dev->addr_len)
1780 return -ETOOSMALL;
1781 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001782
Jon Wetzela6f9a702005-08-20 17:15:54 -07001783 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001784 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001785 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001786 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1787 return -EFAULT;
1788 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001789}
1790
Jeff Garzik13c99b22007-08-15 16:01:56 -07001791static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1792 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001793{
Roland Dreier8e557422010-02-11 12:14:23 -08001794 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001795
Jeff Garzik13c99b22007-08-15 16:01:56 -07001796 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001797 return -EOPNOTSUPP;
1798
Jeff Garzik13c99b22007-08-15 16:01:56 -07001799 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001800
1801 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1802 return -EFAULT;
1803 return 0;
1804}
1805
Jeff Garzik13c99b22007-08-15 16:01:56 -07001806static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1807 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001808{
1809 struct ethtool_value edata;
1810
Jeff Garzik13c99b22007-08-15 16:01:56 -07001811 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001812 return -EOPNOTSUPP;
1813
1814 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1815 return -EFAULT;
1816
Jeff Garzik13c99b22007-08-15 16:01:56 -07001817 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001818 return 0;
1819}
1820
Jeff Garzik13c99b22007-08-15 16:01:56 -07001821static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1822 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001823{
1824 struct ethtool_value edata;
1825
Jeff Garzik13c99b22007-08-15 16:01:56 -07001826 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001827 return -EOPNOTSUPP;
1828
1829 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1830 return -EFAULT;
1831
Jeff Garzik13c99b22007-08-15 16:01:56 -07001832 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001833}
1834
chavey97f8aef2010-04-07 21:54:42 -07001835static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1836 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001837{
1838 struct ethtool_flash efl;
1839
1840 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1841 return -EFAULT;
1842
1843 if (!dev->ethtool_ops->flash_device)
1844 return -EOPNOTSUPP;
1845
1846 return dev->ethtool_ops->flash_device(dev, &efl);
1847}
1848
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001849static int ethtool_set_dump(struct net_device *dev,
1850 void __user *useraddr)
1851{
1852 struct ethtool_dump dump;
1853
1854 if (!dev->ethtool_ops->set_dump)
1855 return -EOPNOTSUPP;
1856
1857 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1858 return -EFAULT;
1859
1860 return dev->ethtool_ops->set_dump(dev, &dump);
1861}
1862
1863static int ethtool_get_dump_flag(struct net_device *dev,
1864 void __user *useraddr)
1865{
1866 int ret;
1867 struct ethtool_dump dump;
1868 const struct ethtool_ops *ops = dev->ethtool_ops;
1869
1870 if (!dev->ethtool_ops->get_dump_flag)
1871 return -EOPNOTSUPP;
1872
1873 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1874 return -EFAULT;
1875
1876 ret = ops->get_dump_flag(dev, &dump);
1877 if (ret)
1878 return ret;
1879
1880 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1881 return -EFAULT;
1882 return 0;
1883}
1884
1885static int ethtool_get_dump_data(struct net_device *dev,
1886 void __user *useraddr)
1887{
1888 int ret;
1889 __u32 len;
1890 struct ethtool_dump dump, tmp;
1891 const struct ethtool_ops *ops = dev->ethtool_ops;
1892 void *data = NULL;
1893
1894 if (!dev->ethtool_ops->get_dump_data ||
1895 !dev->ethtool_ops->get_dump_flag)
1896 return -EOPNOTSUPP;
1897
1898 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1899 return -EFAULT;
1900
1901 memset(&tmp, 0, sizeof(tmp));
1902 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1903 ret = ops->get_dump_flag(dev, &tmp);
1904 if (ret)
1905 return ret;
1906
1907 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1908 if (!len)
1909 return -EFAULT;
1910
1911 data = vzalloc(tmp.len);
1912 if (!data)
1913 return -ENOMEM;
1914 ret = ops->get_dump_data(dev, &dump, data);
1915 if (ret)
1916 goto out;
1917
1918 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1919 ret = -EFAULT;
1920 goto out;
1921 }
1922 useraddr += offsetof(struct ethtool_dump, data);
1923 if (copy_to_user(useraddr, data, len))
1924 ret = -EFAULT;
1925out:
1926 vfree(data);
1927 return ret;
1928}
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930/* The main entry point in this file. Called from net/core/dev.c */
1931
Eric W. Biederman881d9662007-09-17 11:56:21 -07001932int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001934 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 void __user *useraddr = ifr->ifr_data;
1936 u32 ethcmd;
1937 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001938 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (!dev || !netif_device_present(dev))
1941 return -ENODEV;
1942
chavey97f8aef2010-04-07 21:54:42 -07001943 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return -EFAULT;
1945
Ben Hutchings01414802010-08-17 02:31:15 -07001946 if (!dev->ethtool_ops) {
1947 /* ETHTOOL_GDRVINFO does not require any driver support.
1948 * It is also unprivileged and does not change anything,
1949 * so we can take a shortcut to it. */
1950 if (ethcmd == ETHTOOL_GDRVINFO)
1951 return ethtool_get_drvinfo(dev, useraddr);
1952 else
1953 return -EOPNOTSUPP;
1954 }
1955
Stephen Hemminger75f31232006-09-28 15:13:37 -07001956 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001957 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001958 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001959 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001960 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001961 case ETHTOOL_GCOALESCE:
1962 case ETHTOOL_GRINGPARAM:
1963 case ETHTOOL_GPAUSEPARAM:
1964 case ETHTOOL_GRXCSUM:
1965 case ETHTOOL_GTXCSUM:
1966 case ETHTOOL_GSG:
1967 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001968 case ETHTOOL_GTSO:
1969 case ETHTOOL_GPERMADDR:
1970 case ETHTOOL_GUFO:
1971 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001972 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001973 case ETHTOOL_GFLAGS:
1974 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001975 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001976 case ETHTOOL_GRXRINGS:
1977 case ETHTOOL_GRXCLSRLCNT:
1978 case ETHTOOL_GRXCLSRULE:
1979 case ETHTOOL_GRXCLSRLALL:
Michał Mirosław5455c692011-02-15 16:59:17 +00001980 case ETHTOOL_GFEATURES:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001981 break;
1982 default:
1983 if (!capable(CAP_NET_ADMIN))
1984 return -EPERM;
1985 }
1986
chavey97f8aef2010-04-07 21:54:42 -07001987 if (dev->ethtool_ops->begin) {
1988 rc = dev->ethtool_ops->begin(dev);
1989 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001991 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001992 old_features = dev->features;
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 switch (ethcmd) {
1995 case ETHTOOL_GSET:
1996 rc = ethtool_get_settings(dev, useraddr);
1997 break;
1998 case ETHTOOL_SSET:
1999 rc = ethtool_set_settings(dev, useraddr);
2000 break;
2001 case ETHTOOL_GDRVINFO:
2002 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 case ETHTOOL_GREGS:
2005 rc = ethtool_get_regs(dev, useraddr);
2006 break;
2007 case ETHTOOL_GWOL:
2008 rc = ethtool_get_wol(dev, useraddr);
2009 break;
2010 case ETHTOOL_SWOL:
2011 rc = ethtool_set_wol(dev, useraddr);
2012 break;
2013 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002014 rc = ethtool_get_value(dev, useraddr, ethcmd,
2015 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
2017 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002018 rc = ethtool_set_value_void(dev, useraddr,
2019 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 break;
2021 case ETHTOOL_NWAY_RST:
2022 rc = ethtool_nway_reset(dev);
2023 break;
2024 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002025 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 break;
2027 case ETHTOOL_GEEPROM:
2028 rc = ethtool_get_eeprom(dev, useraddr);
2029 break;
2030 case ETHTOOL_SEEPROM:
2031 rc = ethtool_set_eeprom(dev, useraddr);
2032 break;
2033 case ETHTOOL_GCOALESCE:
2034 rc = ethtool_get_coalesce(dev, useraddr);
2035 break;
2036 case ETHTOOL_SCOALESCE:
2037 rc = ethtool_set_coalesce(dev, useraddr);
2038 break;
2039 case ETHTOOL_GRINGPARAM:
2040 rc = ethtool_get_ringparam(dev, useraddr);
2041 break;
2042 case ETHTOOL_SRINGPARAM:
2043 rc = ethtool_set_ringparam(dev, useraddr);
2044 break;
2045 case ETHTOOL_GPAUSEPARAM:
2046 rc = ethtool_get_pauseparam(dev, useraddr);
2047 break;
2048 case ETHTOOL_SPAUSEPARAM:
2049 rc = ethtool_set_pauseparam(dev, useraddr);
2050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 case ETHTOOL_TEST:
2052 rc = ethtool_self_test(dev, useraddr);
2053 break;
2054 case ETHTOOL_GSTRINGS:
2055 rc = ethtool_get_strings(dev, useraddr);
2056 break;
2057 case ETHTOOL_PHYS_ID:
2058 rc = ethtool_phys_id(dev, useraddr);
2059 break;
2060 case ETHTOOL_GSTATS:
2061 rc = ethtool_get_stats(dev, useraddr);
2062 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002063 case ETHTOOL_GPERMADDR:
2064 rc = ethtool_get_perm_addr(dev, useraddr);
2065 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002066 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002067 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00002068 (dev->ethtool_ops->get_flags ?
2069 dev->ethtool_ops->get_flags :
2070 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002071 break;
2072 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002073 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002074 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002075 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002076 rc = ethtool_get_value(dev, useraddr, ethcmd,
2077 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002078 break;
2079 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002080 rc = ethtool_set_value(dev, useraddr,
2081 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002082 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002083 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002084 case ETHTOOL_GRXRINGS:
2085 case ETHTOOL_GRXCLSRLCNT:
2086 case ETHTOOL_GRXCLSRULE:
2087 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002088 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002089 break;
2090 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002091 case ETHTOOL_SRXCLSRLDEL:
2092 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002093 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002094 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002095 case ETHTOOL_FLASHDEV:
2096 rc = ethtool_flash_device(dev, useraddr);
2097 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002098 case ETHTOOL_RESET:
2099 rc = ethtool_reset(dev, useraddr);
2100 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08002101 case ETHTOOL_SRXNTUPLE:
2102 rc = ethtool_set_rx_ntuple(dev, useraddr);
2103 break;
2104 case ETHTOOL_GRXNTUPLE:
2105 rc = ethtool_get_rx_ntuple(dev, useraddr);
2106 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002107 case ETHTOOL_GSSET_INFO:
2108 rc = ethtool_get_sset_info(dev, useraddr);
2109 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002110 case ETHTOOL_GRXFHINDIR:
2111 rc = ethtool_get_rxfh_indir(dev, useraddr);
2112 break;
2113 case ETHTOOL_SRXFHINDIR:
2114 rc = ethtool_set_rxfh_indir(dev, useraddr);
2115 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002116 case ETHTOOL_GFEATURES:
2117 rc = ethtool_get_features(dev, useraddr);
2118 break;
2119 case ETHTOOL_SFEATURES:
2120 rc = ethtool_set_features(dev, useraddr);
2121 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002122 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002123 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002124 case ETHTOOL_GSG:
2125 case ETHTOOL_GTSO:
2126 case ETHTOOL_GUFO:
2127 case ETHTOOL_GGSO:
2128 case ETHTOOL_GGRO:
2129 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2130 break;
2131 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002132 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002133 case ETHTOOL_SSG:
2134 case ETHTOOL_STSO:
2135 case ETHTOOL_SUFO:
2136 case ETHTOOL_SGSO:
2137 case ETHTOOL_SGRO:
2138 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2139 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002140 case ETHTOOL_GCHANNELS:
2141 rc = ethtool_get_channels(dev, useraddr);
2142 break;
2143 case ETHTOOL_SCHANNELS:
2144 rc = ethtool_set_channels(dev, useraddr);
2145 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002146 case ETHTOOL_SET_DUMP:
2147 rc = ethtool_set_dump(dev, useraddr);
2148 break;
2149 case ETHTOOL_GET_DUMP_FLAG:
2150 rc = ethtool_get_dump_flag(dev, useraddr);
2151 break;
2152 case ETHTOOL_GET_DUMP_DATA:
2153 rc = ethtool_get_dump_data(dev, useraddr);
2154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002156 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002158
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002159 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002161
2162 if (old_features != dev->features)
2163 netdev_features_change(dev);
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}