blob: 9eb82775a5561512f71593ce32830f1651243860 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090025/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Some useful ethtool_ops methods that're device independent.
27 * If we find that all drivers want to do the same thing here,
28 * we can turn these into dev_() function calls.
29 */
30
31u32 ethtool_op_get_link(struct net_device *dev)
32{
33 return netif_carrier_ok(dev) ? 1 : 0;
34}
chavey97f8aef2010-04-07 21:54:42 -070035EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Sridhar Samudrala1896e612009-07-22 13:38:22 +000037u32 ethtool_op_get_rx_csum(struct net_device *dev)
38{
39 return (dev->features & NETIF_F_ALL_CSUM) != 0;
40}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000041EXPORT_SYMBOL(ethtool_op_get_rx_csum);
Sridhar Samudrala1896e612009-07-22 13:38:22 +000042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043u32 ethtool_op_get_tx_csum(struct net_device *dev)
44{
Herbert Xu8648b302006-06-17 22:06:05 -070045 return (dev->features & NETIF_F_ALL_CSUM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000047EXPORT_SYMBOL(ethtool_op_get_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
50{
51 if (data)
52 dev->features |= NETIF_F_IP_CSUM;
53 else
54 dev->features &= ~NETIF_F_IP_CSUM;
55
56 return 0;
57}
Michał Mirosław9a279ea2011-02-15 16:59:16 +000058EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jon Mason69f6a0f2005-05-29 20:27:24 -070060int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
61{
62 if (data)
63 dev->features |= NETIF_F_HW_CSUM;
64 else
65 dev->features &= ~NETIF_F_HW_CSUM;
66
67 return 0;
68}
chavey97f8aef2010-04-07 21:54:42 -070069EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -070070
71int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
72{
73 if (data)
74 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
75 else
76 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
77
78 return 0;
79}
chavey97f8aef2010-04-07 21:54:42 -070080EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Michael Chan6460d942007-07-14 19:07:52 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082u32 ethtool_op_get_sg(struct net_device *dev)
83{
84 return (dev->features & NETIF_F_SG) != 0;
85}
chavey97f8aef2010-04-07 21:54:42 -070086EXPORT_SYMBOL(ethtool_op_get_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88int ethtool_op_set_sg(struct net_device *dev, u32 data)
89{
90 if (data)
91 dev->features |= NETIF_F_SG;
92 else
93 dev->features &= ~NETIF_F_SG;
94
95 return 0;
96}
chavey97f8aef2010-04-07 21:54:42 -070097EXPORT_SYMBOL(ethtool_op_set_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99u32 ethtool_op_get_tso(struct net_device *dev)
100{
101 return (dev->features & NETIF_F_TSO) != 0;
102}
chavey97f8aef2010-04-07 21:54:42 -0700103EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105int ethtool_op_set_tso(struct net_device *dev, u32 data)
106{
107 if (data)
108 dev->features |= NETIF_F_TSO;
109 else
110 dev->features &= ~NETIF_F_TSO;
111
112 return 0;
113}
chavey97f8aef2010-04-07 21:54:42 -0700114EXPORT_SYMBOL(ethtool_op_set_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700116u32 ethtool_op_get_ufo(struct net_device *dev)
117{
118 return (dev->features & NETIF_F_UFO) != 0;
119}
chavey97f8aef2010-04-07 21:54:42 -0700120EXPORT_SYMBOL(ethtool_op_get_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700121
122int ethtool_op_set_ufo(struct net_device *dev, u32 data)
123{
124 if (data)
125 dev->features |= NETIF_F_UFO;
126 else
127 dev->features &= ~NETIF_F_UFO;
128 return 0;
129}
chavey97f8aef2010-04-07 21:54:42 -0700130EXPORT_SYMBOL(ethtool_op_set_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700131
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700132/* the following list of flags are the same as their associated
133 * NETIF_F_xxx values in include/linux/netdevice.h
134 */
135static const u32 flags_dup_features =
Jesse Grossd5dbda22010-10-20 13:56:07 +0000136 (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
137 ETH_FLAG_RXHASH);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700138
139u32 ethtool_op_get_flags(struct net_device *dev)
140{
141 /* in the future, this function will probably contain additional
142 * handling for flags which are not so easily handled
143 * by a simple masking operation
144 */
145
146 return dev->features & flags_dup_features;
147}
chavey97f8aef2010-04-07 21:54:42 -0700148EXPORT_SYMBOL(ethtool_op_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700149
Ben Hutchings1437ce32010-06-30 02:44:32 +0000150int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700151{
Ben Hutchings1437ce32010-06-30 02:44:32 +0000152 if (data & ~supported)
153 return -EINVAL;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000154
Ben Hutchings1437ce32010-06-30 02:44:32 +0000155 dev->features = ((dev->features & ~flags_dup_features) |
156 (data & flags_dup_features));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700157 return 0;
158}
chavey97f8aef2010-04-07 21:54:42 -0700159EXPORT_SYMBOL(ethtool_op_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700160
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800161void ethtool_ntuple_flush(struct net_device *dev)
162{
163 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
164
165 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
166 list_del(&fsc->list);
167 kfree(fsc);
168 }
169 dev->ethtool_ntuple_list.count = 0;
170}
171EXPORT_SYMBOL(ethtool_ntuple_flush);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* Handlers for each ethtool command */
174
175static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
176{
Roland Dreier8e557422010-02-11 12:14:23 -0800177 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int err;
179
180 if (!dev->ethtool_ops->get_settings)
181 return -EOPNOTSUPP;
182
183 err = dev->ethtool_ops->get_settings(dev, &cmd);
184 if (err < 0)
185 return err;
186
187 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
188 return -EFAULT;
189 return 0;
190}
191
192static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
193{
194 struct ethtool_cmd cmd;
195
196 if (!dev->ethtool_ops->set_settings)
197 return -EOPNOTSUPP;
198
199 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
200 return -EFAULT;
201
202 return dev->ethtool_ops->set_settings(dev, &cmd);
203}
204
chavey97f8aef2010-04-07 21:54:42 -0700205static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
206 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700209 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 memset(&info, 0, sizeof(info));
212 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700213 if (ops && ops->get_drvinfo) {
214 ops->get_drvinfo(dev, &info);
215 } else if (dev->dev.parent && dev->dev.parent->driver) {
216 strlcpy(info.bus_info, dev_name(dev->dev.parent),
217 sizeof(info.bus_info));
218 strlcpy(info.driver, dev->dev.parent->driver->name,
219 sizeof(info.driver));
220 } else {
221 return -EOPNOTSUPP;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jeff Garzik723b2f52010-03-03 22:51:50 +0000224 /*
225 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000226 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000227 */
Ben Hutchings01414802010-08-17 02:31:15 -0700228 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700229 int rc;
230
231 rc = ops->get_sset_count(dev, ETH_SS_TEST);
232 if (rc >= 0)
233 info.testinfo_len = rc;
234 rc = ops->get_sset_count(dev, ETH_SS_STATS);
235 if (rc >= 0)
236 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700237 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
238 if (rc >= 0)
239 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700240 }
Ben Hutchings01414802010-08-17 02:31:15 -0700241 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700243 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 info.eedump_len = ops->get_eeprom_len(dev);
245
246 if (copy_to_user(useraddr, &info, sizeof(info)))
247 return -EFAULT;
248 return 0;
249}
250
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800251static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700252 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000253{
254 struct ethtool_sset_info info;
255 const struct ethtool_ops *ops = dev->ethtool_ops;
256 u64 sset_mask;
257 int i, idx = 0, n_bits = 0, ret, rc;
258 u32 *info_buf = NULL;
259
260 if (!ops->get_sset_count)
261 return -EOPNOTSUPP;
262
263 if (copy_from_user(&info, useraddr, sizeof(info)))
264 return -EFAULT;
265
266 /* store copy of mask, because we zero struct later on */
267 sset_mask = info.sset_mask;
268 if (!sset_mask)
269 return 0;
270
271 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000272 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000273
274 memset(&info, 0, sizeof(info));
275 info.cmd = ETHTOOL_GSSET_INFO;
276
277 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
278 if (!info_buf)
279 return -ENOMEM;
280
281 /*
282 * fill return buffer based on input bitmask and successful
283 * get_sset_count return
284 */
285 for (i = 0; i < 64; i++) {
286 if (!(sset_mask & (1ULL << i)))
287 continue;
288
289 rc = ops->get_sset_count(dev, i);
290 if (rc >= 0) {
291 info.sset_mask |= (1ULL << i);
292 info_buf[idx++] = rc;
293 }
294 }
295
296 ret = -EFAULT;
297 if (copy_to_user(useraddr, &info, sizeof(info)))
298 goto out;
299
300 useraddr += offsetof(struct ethtool_sset_info, data);
301 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
302 goto out;
303
304 ret = 0;
305
306out:
307 kfree(info_buf);
308 return ret;
309}
310
chavey97f8aef2010-04-07 21:54:42 -0700311static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000312 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700313{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000314 struct ethtool_rxnfc info;
315 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700316
Santwona Behera59089d82009-02-20 00:58:13 -0800317 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700318 return -EOPNOTSUPP;
319
Ben Hutchingsbf988432010-06-28 08:45:58 +0000320 /* struct ethtool_rxnfc was originally defined for
321 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
322 * members. User-space might still be using that
323 * definition. */
324 if (cmd == ETHTOOL_SRXFH)
325 info_size = (offsetof(struct ethtool_rxnfc, data) +
326 sizeof(info.data));
327
328 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700329 return -EFAULT;
330
Ben Hutchingsbf988432010-06-28 08:45:58 +0000331 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700332}
333
chavey97f8aef2010-04-07 21:54:42 -0700334static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000335 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700336{
337 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000338 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800339 const struct ethtool_ops *ops = dev->ethtool_ops;
340 int ret;
341 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700342
Santwona Behera59089d82009-02-20 00:58:13 -0800343 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700344 return -EOPNOTSUPP;
345
Ben Hutchingsbf988432010-06-28 08:45:58 +0000346 /* struct ethtool_rxnfc was originally defined for
347 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
348 * members. User-space might still be using that
349 * definition. */
350 if (cmd == ETHTOOL_GRXFH)
351 info_size = (offsetof(struct ethtool_rxnfc, data) +
352 sizeof(info.data));
353
354 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700355 return -EFAULT;
356
Santwona Behera59089d82009-02-20 00:58:13 -0800357 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
358 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000359 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000360 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000361 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800362 if (!rule_buf)
363 return -ENOMEM;
364 }
365 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700366
Santwona Behera59089d82009-02-20 00:58:13 -0800367 ret = ops->get_rxnfc(dev, &info, rule_buf);
368 if (ret < 0)
369 goto err_out;
370
371 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000372 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800373 goto err_out;
374
375 if (rule_buf) {
376 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
377 if (copy_to_user(useraddr, rule_buf,
378 info.rule_cnt * sizeof(u32)))
379 goto err_out;
380 }
381 ret = 0;
382
383err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700384 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800385
386 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700387}
388
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000389static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
390 void __user *useraddr)
391{
392 struct ethtool_rxfh_indir *indir;
393 u32 table_size;
394 size_t full_size;
395 int ret;
396
397 if (!dev->ethtool_ops->get_rxfh_indir)
398 return -EOPNOTSUPP;
399
400 if (copy_from_user(&table_size,
401 useraddr + offsetof(struct ethtool_rxfh_indir, size),
402 sizeof(table_size)))
403 return -EFAULT;
404
405 if (table_size >
406 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
407 return -ENOMEM;
408 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
Kees Cookb00916b2010-10-11 12:23:25 -0700409 indir = kzalloc(full_size, GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000410 if (!indir)
411 return -ENOMEM;
412
413 indir->cmd = ETHTOOL_GRXFHINDIR;
414 indir->size = table_size;
415 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
416 if (ret)
417 goto out;
418
419 if (copy_to_user(useraddr, indir, full_size))
420 ret = -EFAULT;
421
422out:
423 kfree(indir);
424 return ret;
425}
426
427static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
428 void __user *useraddr)
429{
430 struct ethtool_rxfh_indir *indir;
431 u32 table_size;
432 size_t full_size;
433 int ret;
434
435 if (!dev->ethtool_ops->set_rxfh_indir)
436 return -EOPNOTSUPP;
437
438 if (copy_from_user(&table_size,
439 useraddr + offsetof(struct ethtool_rxfh_indir, size),
440 sizeof(table_size)))
441 return -EFAULT;
442
443 if (table_size >
444 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
445 return -ENOMEM;
446 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
447 indir = kmalloc(full_size, GFP_USER);
448 if (!indir)
449 return -ENOMEM;
450
451 if (copy_from_user(indir, useraddr, full_size)) {
452 ret = -EFAULT;
453 goto out;
454 }
455
456 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
457
458out:
459 kfree(indir);
460 return ret;
461}
462
Peter Waskiewicze8589112010-02-12 13:48:05 +0000463static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700464 struct ethtool_rx_ntuple_flow_spec *spec,
465 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800466{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800467
468 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000469 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
470 /* free the container */
471 kfree(fsc);
472 return;
473 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800474
475 /* Copy the whole filter over */
476 fsc->fs.flow_type = spec->flow_type;
477 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
478 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
479
480 fsc->fs.vlan_tag = spec->vlan_tag;
481 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
482 fsc->fs.data = spec->data;
483 fsc->fs.data_mask = spec->data_mask;
484 fsc->fs.action = spec->action;
485
486 /* add to the list */
487 list_add_tail_rcu(&fsc->list, &list->list);
488 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800489}
490
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000491/*
492 * ethtool does not (or did not) set masks for flow parameters that are
493 * not specified, so if both value and mask are 0 then this must be
494 * treated as equivalent to a mask with all bits set. Implement that
495 * here rather than in drivers.
496 */
497static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
498{
499 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
500 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
501
502 if (fs->flow_type != TCP_V4_FLOW &&
503 fs->flow_type != UDP_V4_FLOW &&
504 fs->flow_type != SCTP_V4_FLOW)
505 return;
506
507 if (!(entry->ip4src | mask->ip4src))
508 mask->ip4src = htonl(0xffffffff);
509 if (!(entry->ip4dst | mask->ip4dst))
510 mask->ip4dst = htonl(0xffffffff);
511 if (!(entry->psrc | mask->psrc))
512 mask->psrc = htons(0xffff);
513 if (!(entry->pdst | mask->pdst))
514 mask->pdst = htons(0xffff);
515 if (!(entry->tos | mask->tos))
516 mask->tos = 0xff;
517 if (!(fs->vlan_tag | fs->vlan_tag_mask))
518 fs->vlan_tag_mask = 0xffff;
519 if (!(fs->data | fs->data_mask))
520 fs->data_mask = 0xffffffffffffffffULL;
521}
522
chavey97f8aef2010-04-07 21:54:42 -0700523static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
524 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800525{
526 struct ethtool_rx_ntuple cmd;
527 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000528 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800529 int ret;
530
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800531 if (!(dev->features & NETIF_F_NTUPLE))
532 return -EINVAL;
533
534 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
535 return -EFAULT;
536
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000537 rx_ntuple_fix_masks(&cmd.fs);
538
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800539 /*
540 * Cache filter in dev struct for GET operation only if
541 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000542 * only if the filter was added successfully. First make sure we
543 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800544 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000545 if (!ops->get_rx_ntuple) {
546 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
547 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800548 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000549 }
550
551 ret = ops->set_rx_ntuple(dev, &cmd);
552 if (ret) {
553 kfree(fsc);
554 return ret;
555 }
556
557 if (!ops->get_rx_ntuple)
558 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800559
560 return ret;
561}
562
563static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
564{
565 struct ethtool_gstrings gstrings;
566 const struct ethtool_ops *ops = dev->ethtool_ops;
567 struct ethtool_rx_ntuple_flow_spec_container *fsc;
568 u8 *data;
569 char *p;
570 int ret, i, num_strings = 0;
571
572 if (!ops->get_sset_count)
573 return -EOPNOTSUPP;
574
575 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
576 return -EFAULT;
577
578 ret = ops->get_sset_count(dev, gstrings.string_set);
579 if (ret < 0)
580 return ret;
581
582 gstrings.len = ret;
583
Kees Cookb00916b2010-10-11 12:23:25 -0700584 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800585 if (!data)
586 return -ENOMEM;
587
588 if (ops->get_rx_ntuple) {
589 /* driver-specific filter grab */
590 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
591 goto copy;
592 }
593
594 /* default ethtool filter grab */
595 i = 0;
596 p = (char *)data;
597 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
598 sprintf(p, "Filter %d:\n", i);
599 p += ETH_GSTRING_LEN;
600 num_strings++;
601
602 switch (fsc->fs.flow_type) {
603 case TCP_V4_FLOW:
604 sprintf(p, "\tFlow Type: TCP\n");
605 p += ETH_GSTRING_LEN;
606 num_strings++;
607 break;
608 case UDP_V4_FLOW:
609 sprintf(p, "\tFlow Type: UDP\n");
610 p += ETH_GSTRING_LEN;
611 num_strings++;
612 break;
613 case SCTP_V4_FLOW:
614 sprintf(p, "\tFlow Type: SCTP\n");
615 p += ETH_GSTRING_LEN;
616 num_strings++;
617 break;
618 case AH_ESP_V4_FLOW:
619 sprintf(p, "\tFlow Type: AH ESP\n");
620 p += ETH_GSTRING_LEN;
621 num_strings++;
622 break;
623 case ESP_V4_FLOW:
624 sprintf(p, "\tFlow Type: ESP\n");
625 p += ETH_GSTRING_LEN;
626 num_strings++;
627 break;
628 case IP_USER_FLOW:
629 sprintf(p, "\tFlow Type: Raw IP\n");
630 p += ETH_GSTRING_LEN;
631 num_strings++;
632 break;
633 case IPV4_FLOW:
634 sprintf(p, "\tFlow Type: IPv4\n");
635 p += ETH_GSTRING_LEN;
636 num_strings++;
637 break;
638 default:
639 sprintf(p, "\tFlow Type: Unknown\n");
640 p += ETH_GSTRING_LEN;
641 num_strings++;
642 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000643 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800644
645 /* now the rest of the filters */
646 switch (fsc->fs.flow_type) {
647 case TCP_V4_FLOW:
648 case UDP_V4_FLOW:
649 case SCTP_V4_FLOW:
650 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700651 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800652 p += ETH_GSTRING_LEN;
653 num_strings++;
654 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700655 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800656 p += ETH_GSTRING_LEN;
657 num_strings++;
658 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700659 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800660 p += ETH_GSTRING_LEN;
661 num_strings++;
662 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700663 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800664 p += ETH_GSTRING_LEN;
665 num_strings++;
666 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700667 fsc->fs.h_u.tcp_ip4_spec.psrc,
668 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800669 p += ETH_GSTRING_LEN;
670 num_strings++;
671 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700672 fsc->fs.h_u.tcp_ip4_spec.pdst,
673 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800674 p += ETH_GSTRING_LEN;
675 num_strings++;
676 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700677 fsc->fs.h_u.tcp_ip4_spec.tos,
678 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800679 p += ETH_GSTRING_LEN;
680 num_strings++;
681 break;
682 case AH_ESP_V4_FLOW:
683 case ESP_V4_FLOW:
684 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700685 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800686 p += ETH_GSTRING_LEN;
687 num_strings++;
688 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700689 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800690 p += ETH_GSTRING_LEN;
691 num_strings++;
692 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700693 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800694 p += ETH_GSTRING_LEN;
695 num_strings++;
696 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700697 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800698 p += ETH_GSTRING_LEN;
699 num_strings++;
700 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700701 fsc->fs.h_u.ah_ip4_spec.spi,
702 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800703 p += ETH_GSTRING_LEN;
704 num_strings++;
705 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700706 fsc->fs.h_u.ah_ip4_spec.tos,
707 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800708 p += ETH_GSTRING_LEN;
709 num_strings++;
710 break;
711 case IP_USER_FLOW:
712 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000713 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800714 p += ETH_GSTRING_LEN;
715 num_strings++;
716 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000717 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800718 p += ETH_GSTRING_LEN;
719 num_strings++;
720 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000721 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800722 p += ETH_GSTRING_LEN;
723 num_strings++;
724 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000725 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800726 p += ETH_GSTRING_LEN;
727 num_strings++;
728 break;
729 case IPV4_FLOW:
730 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700731 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800732 p += ETH_GSTRING_LEN;
733 num_strings++;
734 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700735 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800736 p += ETH_GSTRING_LEN;
737 num_strings++;
738 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700739 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800740 p += ETH_GSTRING_LEN;
741 num_strings++;
742 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700743 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800744 p += ETH_GSTRING_LEN;
745 num_strings++;
746 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700747 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
748 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800749 p += ETH_GSTRING_LEN;
750 num_strings++;
751 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700752 fsc->fs.h_u.usr_ip4_spec.tos,
753 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800754 p += ETH_GSTRING_LEN;
755 num_strings++;
756 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700757 fsc->fs.h_u.usr_ip4_spec.ip_ver,
758 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800759 p += ETH_GSTRING_LEN;
760 num_strings++;
761 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700762 fsc->fs.h_u.usr_ip4_spec.proto,
763 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800764 p += ETH_GSTRING_LEN;
765 num_strings++;
766 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000767 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800768 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700769 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800770 p += ETH_GSTRING_LEN;
771 num_strings++;
772 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
773 p += ETH_GSTRING_LEN;
774 num_strings++;
775 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
776 p += ETH_GSTRING_LEN;
777 num_strings++;
778 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
779 sprintf(p, "\tAction: Drop\n");
780 else
781 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -0700782 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800783 p += ETH_GSTRING_LEN;
784 num_strings++;
785unknown_filter:
786 i++;
787 }
788copy:
789 /* indicate to userspace how many strings we actually have */
790 gstrings.len = num_strings;
791 ret = -EFAULT;
792 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
793 goto out;
794 useraddr += sizeof(gstrings);
795 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
796 goto out;
797 ret = 0;
798
799out:
800 kfree(data);
801 return ret;
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
805{
806 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700807 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 void *regbuf;
809 int reglen, ret;
810
811 if (!ops->get_regs || !ops->get_regs_len)
812 return -EOPNOTSUPP;
813
814 if (copy_from_user(&regs, useraddr, sizeof(regs)))
815 return -EFAULT;
816
817 reglen = ops->get_regs_len(dev);
818 if (regs.len > reglen)
819 regs.len = reglen;
820
Eugene Teob7c7d012011-01-24 21:05:17 -0800821 regbuf = vzalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!regbuf)
823 return -ENOMEM;
824
825 ops->get_regs(dev, &regs, regbuf);
826
827 ret = -EFAULT;
828 if (copy_to_user(useraddr, &regs, sizeof(regs)))
829 goto out;
830 useraddr += offsetof(struct ethtool_regs, data);
831 if (copy_to_user(useraddr, regbuf, regs.len))
832 goto out;
833 ret = 0;
834
835 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000836 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return ret;
838}
839
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000840static int ethtool_reset(struct net_device *dev, char __user *useraddr)
841{
842 struct ethtool_value reset;
843 int ret;
844
845 if (!dev->ethtool_ops->reset)
846 return -EOPNOTSUPP;
847
848 if (copy_from_user(&reset, useraddr, sizeof(reset)))
849 return -EFAULT;
850
851 ret = dev->ethtool_ops->reset(dev, &reset.data);
852 if (ret)
853 return ret;
854
855 if (copy_to_user(useraddr, &reset, sizeof(reset)))
856 return -EFAULT;
857 return 0;
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
861{
Roland Dreier8e557422010-02-11 12:14:23 -0800862 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (!dev->ethtool_ops->get_wol)
865 return -EOPNOTSUPP;
866
867 dev->ethtool_ops->get_wol(dev, &wol);
868
869 if (copy_to_user(useraddr, &wol, sizeof(wol)))
870 return -EFAULT;
871 return 0;
872}
873
874static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
875{
876 struct ethtool_wolinfo wol;
877
878 if (!dev->ethtool_ops->set_wol)
879 return -EOPNOTSUPP;
880
881 if (copy_from_user(&wol, useraddr, sizeof(wol)))
882 return -EFAULT;
883
884 return dev->ethtool_ops->set_wol(dev, &wol);
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static int ethtool_nway_reset(struct net_device *dev)
888{
889 if (!dev->ethtool_ops->nway_reset)
890 return -EOPNOTSUPP;
891
892 return dev->ethtool_ops->nway_reset(dev);
893}
894
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000895static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
896{
897 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
898
899 if (!dev->ethtool_ops->get_link)
900 return -EOPNOTSUPP;
901
902 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
903
904 if (copy_to_user(useraddr, &edata, sizeof(edata)))
905 return -EFAULT;
906 return 0;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
910{
911 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700912 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700913 void __user *userbuf = useraddr + sizeof(eeprom);
914 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700916 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (!ops->get_eeprom || !ops->get_eeprom_len)
919 return -EOPNOTSUPP;
920
921 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
922 return -EFAULT;
923
924 /* Check for wrap and zero */
925 if (eeprom.offset + eeprom.len <= eeprom.offset)
926 return -EINVAL;
927
928 /* Check for exceeding total eeprom len */
929 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
930 return -EINVAL;
931
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700932 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!data)
934 return -ENOMEM;
935
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700936 bytes_remaining = eeprom.len;
937 while (bytes_remaining > 0) {
938 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700940 ret = ops->get_eeprom(dev, &eeprom, data);
941 if (ret)
942 break;
943 if (copy_to_user(userbuf, data, eeprom.len)) {
944 ret = -EFAULT;
945 break;
946 }
947 userbuf += eeprom.len;
948 eeprom.offset += eeprom.len;
949 bytes_remaining -= eeprom.len;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700952 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
953 eeprom.offset -= eeprom.len;
954 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
955 ret = -EFAULT;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 kfree(data);
958 return ret;
959}
960
961static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
962{
963 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700964 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700965 void __user *userbuf = useraddr + sizeof(eeprom);
966 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700968 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 if (!ops->set_eeprom || !ops->get_eeprom_len)
971 return -EOPNOTSUPP;
972
973 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
974 return -EFAULT;
975
976 /* Check for wrap and zero */
977 if (eeprom.offset + eeprom.len <= eeprom.offset)
978 return -EINVAL;
979
980 /* Check for exceeding total eeprom len */
981 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
982 return -EINVAL;
983
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700984 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (!data)
986 return -ENOMEM;
987
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700988 bytes_remaining = eeprom.len;
989 while (bytes_remaining > 0) {
990 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700992 if (copy_from_user(data, userbuf, eeprom.len)) {
993 ret = -EFAULT;
994 break;
995 }
996 ret = ops->set_eeprom(dev, &eeprom, data);
997 if (ret)
998 break;
999 userbuf += eeprom.len;
1000 eeprom.offset += eeprom.len;
1001 bytes_remaining -= eeprom.len;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 kfree(data);
1005 return ret;
1006}
1007
chavey97f8aef2010-04-07 21:54:42 -07001008static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1009 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Roland Dreier8e557422010-02-11 12:14:23 -08001011 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (!dev->ethtool_ops->get_coalesce)
1014 return -EOPNOTSUPP;
1015
1016 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1017
1018 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1019 return -EFAULT;
1020 return 0;
1021}
1022
chavey97f8aef2010-04-07 21:54:42 -07001023static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1024 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct ethtool_coalesce coalesce;
1027
David S. Millerfa04ae52005-06-06 15:07:19 -07001028 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EOPNOTSUPP;
1030
1031 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1032 return -EFAULT;
1033
1034 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1035}
1036
1037static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1038{
Roland Dreier8e557422010-02-11 12:14:23 -08001039 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (!dev->ethtool_ops->get_ringparam)
1042 return -EOPNOTSUPP;
1043
1044 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1045
1046 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1047 return -EFAULT;
1048 return 0;
1049}
1050
1051static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1052{
1053 struct ethtool_ringparam ringparam;
1054
1055 if (!dev->ethtool_ops->set_ringparam)
1056 return -EOPNOTSUPP;
1057
1058 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1059 return -EFAULT;
1060
1061 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1062}
1063
1064static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1065{
1066 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1067
1068 if (!dev->ethtool_ops->get_pauseparam)
1069 return -EOPNOTSUPP;
1070
1071 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1072
1073 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1074 return -EFAULT;
1075 return 0;
1076}
1077
1078static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1079{
1080 struct ethtool_pauseparam pauseparam;
1081
Jeff Garzike1b90c42006-07-17 12:54:40 -04001082 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -EOPNOTSUPP;
1084
1085 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1086 return -EFAULT;
1087
1088 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static int __ethtool_set_sg(struct net_device *dev, u32 data)
1092{
1093 int err;
1094
1095 if (!data && dev->ethtool_ops->set_tso) {
1096 err = dev->ethtool_ops->set_tso(dev, 0);
1097 if (err)
1098 return err;
1099 }
1100
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001101 if (!data && dev->ethtool_ops->set_ufo) {
1102 err = dev->ethtool_ops->set_ufo(dev, 0);
1103 if (err)
1104 return err;
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return dev->ethtool_ops->set_sg(dev, data);
1107}
1108
1109static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
1110{
1111 struct ethtool_value edata;
1112 int err;
1113
1114 if (!dev->ethtool_ops->set_tx_csum)
1115 return -EOPNOTSUPP;
1116
1117 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1118 return -EFAULT;
1119
1120 if (!edata.data && dev->ethtool_ops->set_sg) {
1121 err = __ethtool_set_sg(dev, 0);
1122 if (err)
1123 return err;
1124 }
1125
1126 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1127}
1128
Herbert Xub240a0e2008-12-15 23:44:31 -08001129static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1130{
1131 struct ethtool_value edata;
1132
1133 if (!dev->ethtool_ops->set_rx_csum)
1134 return -EOPNOTSUPP;
1135
1136 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1137 return -EFAULT;
1138
1139 if (!edata.data && dev->ethtool_ops->set_sg)
1140 dev->features &= ~NETIF_F_GRO;
1141
1142 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1146{
1147 struct ethtool_value edata;
1148
1149 if (!dev->ethtool_ops->set_sg)
1150 return -EOPNOTSUPP;
1151
1152 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1153 return -EFAULT;
1154
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001155 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -07001156 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -EINVAL;
1158
1159 return __ethtool_set_sg(dev, edata.data);
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1163{
1164 struct ethtool_value edata;
1165
1166 if (!dev->ethtool_ops->set_tso)
1167 return -EOPNOTSUPP;
1168
1169 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1170 return -EFAULT;
1171
1172 if (edata.data && !(dev->features & NETIF_F_SG))
1173 return -EINVAL;
1174
1175 return dev->ethtool_ops->set_tso(dev, edata.data);
1176}
1177
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001178static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1179{
1180 struct ethtool_value edata;
1181
1182 if (!dev->ethtool_ops->set_ufo)
1183 return -EOPNOTSUPP;
1184 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1185 return -EFAULT;
1186 if (edata.data && !(dev->features & NETIF_F_SG))
1187 return -EINVAL;
Michał Mirosław79032642010-11-30 06:38:00 +00001188 if (edata.data && !((dev->features & NETIF_F_GEN_CSUM) ||
1189 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1190 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001191 return -EINVAL;
1192 return dev->ethtool_ops->set_ufo(dev, edata.data);
1193}
1194
Herbert Xu37c31852006-06-22 03:07:29 -07001195static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1196{
1197 struct ethtool_value edata = { ETHTOOL_GGSO };
1198
1199 edata.data = dev->features & NETIF_F_GSO;
1200 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001201 return -EFAULT;
Herbert Xu37c31852006-06-22 03:07:29 -07001202 return 0;
1203}
1204
1205static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1206{
1207 struct ethtool_value edata;
1208
1209 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1210 return -EFAULT;
1211 if (edata.data)
1212 dev->features |= NETIF_F_GSO;
1213 else
1214 dev->features &= ~NETIF_F_GSO;
1215 return 0;
1216}
1217
Herbert Xub240a0e2008-12-15 23:44:31 -08001218static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1219{
1220 struct ethtool_value edata = { ETHTOOL_GGRO };
1221
1222 edata.data = dev->features & NETIF_F_GRO;
1223 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001224 return -EFAULT;
Herbert Xub240a0e2008-12-15 23:44:31 -08001225 return 0;
1226}
1227
1228static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1229{
1230 struct ethtool_value edata;
1231
1232 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1233 return -EFAULT;
1234
1235 if (edata.data) {
Eric Dumazet67c96602010-09-17 11:56:18 -07001236 u32 rxcsum = dev->ethtool_ops->get_rx_csum ?
1237 dev->ethtool_ops->get_rx_csum(dev) :
1238 ethtool_op_get_rx_csum(dev);
1239
1240 if (!rxcsum)
Herbert Xub240a0e2008-12-15 23:44:31 -08001241 return -EINVAL;
1242 dev->features |= NETIF_F_GRO;
1243 } else
1244 dev->features &= ~NETIF_F_GRO;
1245
1246 return 0;
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1250{
1251 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001252 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001254 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001256 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001257 return -EOPNOTSUPP;
1258
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001259 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001260 if (test_len < 0)
1261 return test_len;
1262 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (copy_from_user(&test, useraddr, sizeof(test)))
1265 return -EFAULT;
1266
Jeff Garzikff03d492007-08-15 16:01:08 -07001267 test.len = test_len;
1268 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (!data)
1270 return -ENOMEM;
1271
1272 ops->self_test(dev, &test, data);
1273
1274 ret = -EFAULT;
1275 if (copy_to_user(useraddr, &test, sizeof(test)))
1276 goto out;
1277 useraddr += sizeof(test);
1278 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1279 goto out;
1280 ret = 0;
1281
1282 out:
1283 kfree(data);
1284 return ret;
1285}
1286
1287static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1288{
1289 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001290 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 u8 *data;
1292 int ret;
1293
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001294 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -EOPNOTSUPP;
1296
1297 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1298 return -EFAULT;
1299
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001300 ret = ops->get_sset_count(dev, gstrings.string_set);
1301 if (ret < 0)
1302 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001303
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001304 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1307 if (!data)
1308 return -ENOMEM;
1309
1310 ops->get_strings(dev, gstrings.string_set, data);
1311
1312 ret = -EFAULT;
1313 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1314 goto out;
1315 useraddr += sizeof(gstrings);
1316 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1317 goto out;
1318 ret = 0;
1319
1320 out:
1321 kfree(data);
1322 return ret;
1323}
1324
1325static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1326{
1327 struct ethtool_value id;
1328
1329 if (!dev->ethtool_ops->phys_id)
1330 return -EOPNOTSUPP;
1331
1332 if (copy_from_user(&id, useraddr, sizeof(id)))
1333 return -EFAULT;
1334
1335 return dev->ethtool_ops->phys_id(dev, id.data);
1336}
1337
1338static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1339{
1340 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001341 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001343 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001345 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001346 return -EOPNOTSUPP;
1347
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001348 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001349 if (n_stats < 0)
1350 return n_stats;
1351 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1354 return -EFAULT;
1355
Jeff Garzikff03d492007-08-15 16:01:08 -07001356 stats.n_stats = n_stats;
1357 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!data)
1359 return -ENOMEM;
1360
1361 ops->get_ethtool_stats(dev, &stats, data);
1362
1363 ret = -EFAULT;
1364 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1365 goto out;
1366 useraddr += sizeof(stats);
1367 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1368 goto out;
1369 ret = 0;
1370
1371 out:
1372 kfree(data);
1373 return ret;
1374}
1375
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001376static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001377{
1378 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001379
Matthew Wilcox313674a2007-07-31 14:00:29 -07001380 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001381 return -EFAULT;
1382
Matthew Wilcox313674a2007-07-31 14:00:29 -07001383 if (epaddr.size < dev->addr_len)
1384 return -ETOOSMALL;
1385 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001386
Jon Wetzela6f9a702005-08-20 17:15:54 -07001387 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001388 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001389 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001390 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1391 return -EFAULT;
1392 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001393}
1394
Jeff Garzik13c99b22007-08-15 16:01:56 -07001395static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1396 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001397{
Roland Dreier8e557422010-02-11 12:14:23 -08001398 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001399
Jeff Garzik13c99b22007-08-15 16:01:56 -07001400 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001401 return -EOPNOTSUPP;
1402
Jeff Garzik13c99b22007-08-15 16:01:56 -07001403 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001404
1405 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1406 return -EFAULT;
1407 return 0;
1408}
1409
Jeff Garzik13c99b22007-08-15 16:01:56 -07001410static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1411 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001412{
1413 struct ethtool_value edata;
1414
Jeff Garzik13c99b22007-08-15 16:01:56 -07001415 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001416 return -EOPNOTSUPP;
1417
1418 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1419 return -EFAULT;
1420
Jeff Garzik13c99b22007-08-15 16:01:56 -07001421 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001422 return 0;
1423}
1424
Jeff Garzik13c99b22007-08-15 16:01:56 -07001425static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1426 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001427{
1428 struct ethtool_value edata;
1429
Jeff Garzik13c99b22007-08-15 16:01:56 -07001430 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001431 return -EOPNOTSUPP;
1432
1433 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1434 return -EFAULT;
1435
Jeff Garzik13c99b22007-08-15 16:01:56 -07001436 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001437}
1438
chavey97f8aef2010-04-07 21:54:42 -07001439static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1440 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001441{
1442 struct ethtool_flash efl;
1443
1444 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1445 return -EFAULT;
1446
1447 if (!dev->ethtool_ops->flash_device)
1448 return -EOPNOTSUPP;
1449
1450 return dev->ethtool_ops->flash_device(dev, &efl);
1451}
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/* The main entry point in this file. Called from net/core/dev.c */
1454
Eric W. Biederman881d9662007-09-17 11:56:21 -07001455int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001457 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 void __user *useraddr = ifr->ifr_data;
1459 u32 ethcmd;
1460 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001461 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!dev || !netif_device_present(dev))
1464 return -ENODEV;
1465
chavey97f8aef2010-04-07 21:54:42 -07001466 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EFAULT;
1468
Ben Hutchings01414802010-08-17 02:31:15 -07001469 if (!dev->ethtool_ops) {
1470 /* ETHTOOL_GDRVINFO does not require any driver support.
1471 * It is also unprivileged and does not change anything,
1472 * so we can take a shortcut to it. */
1473 if (ethcmd == ETHTOOL_GDRVINFO)
1474 return ethtool_get_drvinfo(dev, useraddr);
1475 else
1476 return -EOPNOTSUPP;
1477 }
1478
Stephen Hemminger75f31232006-09-28 15:13:37 -07001479 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001480 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001481 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001482 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001483 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001484 case ETHTOOL_GCOALESCE:
1485 case ETHTOOL_GRINGPARAM:
1486 case ETHTOOL_GPAUSEPARAM:
1487 case ETHTOOL_GRXCSUM:
1488 case ETHTOOL_GTXCSUM:
1489 case ETHTOOL_GSG:
1490 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001491 case ETHTOOL_GTSO:
1492 case ETHTOOL_GPERMADDR:
1493 case ETHTOOL_GUFO:
1494 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001495 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001496 case ETHTOOL_GFLAGS:
1497 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001498 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001499 case ETHTOOL_GRXRINGS:
1500 case ETHTOOL_GRXCLSRLCNT:
1501 case ETHTOOL_GRXCLSRULE:
1502 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001503 break;
1504 default:
1505 if (!capable(CAP_NET_ADMIN))
1506 return -EPERM;
1507 }
1508
chavey97f8aef2010-04-07 21:54:42 -07001509 if (dev->ethtool_ops->begin) {
1510 rc = dev->ethtool_ops->begin(dev);
1511 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001513 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001514 old_features = dev->features;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 switch (ethcmd) {
1517 case ETHTOOL_GSET:
1518 rc = ethtool_get_settings(dev, useraddr);
1519 break;
1520 case ETHTOOL_SSET:
1521 rc = ethtool_set_settings(dev, useraddr);
1522 break;
1523 case ETHTOOL_GDRVINFO:
1524 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 break;
1526 case ETHTOOL_GREGS:
1527 rc = ethtool_get_regs(dev, useraddr);
1528 break;
1529 case ETHTOOL_GWOL:
1530 rc = ethtool_get_wol(dev, useraddr);
1531 break;
1532 case ETHTOOL_SWOL:
1533 rc = ethtool_set_wol(dev, useraddr);
1534 break;
1535 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001536 rc = ethtool_get_value(dev, useraddr, ethcmd,
1537 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001540 rc = ethtool_set_value_void(dev, useraddr,
1541 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 case ETHTOOL_NWAY_RST:
1544 rc = ethtool_nway_reset(dev);
1545 break;
1546 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001547 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 break;
1549 case ETHTOOL_GEEPROM:
1550 rc = ethtool_get_eeprom(dev, useraddr);
1551 break;
1552 case ETHTOOL_SEEPROM:
1553 rc = ethtool_set_eeprom(dev, useraddr);
1554 break;
1555 case ETHTOOL_GCOALESCE:
1556 rc = ethtool_get_coalesce(dev, useraddr);
1557 break;
1558 case ETHTOOL_SCOALESCE:
1559 rc = ethtool_set_coalesce(dev, useraddr);
1560 break;
1561 case ETHTOOL_GRINGPARAM:
1562 rc = ethtool_get_ringparam(dev, useraddr);
1563 break;
1564 case ETHTOOL_SRINGPARAM:
1565 rc = ethtool_set_ringparam(dev, useraddr);
1566 break;
1567 case ETHTOOL_GPAUSEPARAM:
1568 rc = ethtool_get_pauseparam(dev, useraddr);
1569 break;
1570 case ETHTOOL_SPAUSEPARAM:
1571 rc = ethtool_set_pauseparam(dev, useraddr);
1572 break;
1573 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001574 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001575 (dev->ethtool_ops->get_rx_csum ?
1576 dev->ethtool_ops->get_rx_csum :
1577 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 break;
1579 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001580 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 break;
1582 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001583 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001584 (dev->ethtool_ops->get_tx_csum ?
1585 dev->ethtool_ops->get_tx_csum :
1586 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
1588 case ETHTOOL_STXCSUM:
1589 rc = ethtool_set_tx_csum(dev, useraddr);
1590 break;
1591 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001592 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001593 (dev->ethtool_ops->get_sg ?
1594 dev->ethtool_ops->get_sg :
1595 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 case ETHTOOL_SSG:
1598 rc = ethtool_set_sg(dev, useraddr);
1599 break;
1600 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001601 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001602 (dev->ethtool_ops->get_tso ?
1603 dev->ethtool_ops->get_tso :
1604 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 case ETHTOOL_STSO:
1607 rc = ethtool_set_tso(dev, useraddr);
1608 break;
1609 case ETHTOOL_TEST:
1610 rc = ethtool_self_test(dev, useraddr);
1611 break;
1612 case ETHTOOL_GSTRINGS:
1613 rc = ethtool_get_strings(dev, useraddr);
1614 break;
1615 case ETHTOOL_PHYS_ID:
1616 rc = ethtool_phys_id(dev, useraddr);
1617 break;
1618 case ETHTOOL_GSTATS:
1619 rc = ethtool_get_stats(dev, useraddr);
1620 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001621 case ETHTOOL_GPERMADDR:
1622 rc = ethtool_get_perm_addr(dev, useraddr);
1623 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001624 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001625 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001626 (dev->ethtool_ops->get_ufo ?
1627 dev->ethtool_ops->get_ufo :
1628 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001629 break;
1630 case ETHTOOL_SUFO:
1631 rc = ethtool_set_ufo(dev, useraddr);
1632 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001633 case ETHTOOL_GGSO:
1634 rc = ethtool_get_gso(dev, useraddr);
1635 break;
1636 case ETHTOOL_SGSO:
1637 rc = ethtool_set_gso(dev, useraddr);
1638 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001639 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001640 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001641 (dev->ethtool_ops->get_flags ?
1642 dev->ethtool_ops->get_flags :
1643 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001644 break;
1645 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001646 rc = ethtool_set_value(dev, useraddr,
1647 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001648 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001649 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001650 rc = ethtool_get_value(dev, useraddr, ethcmd,
1651 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001652 break;
1653 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001654 rc = ethtool_set_value(dev, useraddr,
1655 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001656 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001657 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001658 case ETHTOOL_GRXRINGS:
1659 case ETHTOOL_GRXCLSRLCNT:
1660 case ETHTOOL_GRXCLSRULE:
1661 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001662 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001663 break;
1664 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001665 case ETHTOOL_SRXCLSRLDEL:
1666 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001667 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001668 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001669 case ETHTOOL_GGRO:
1670 rc = ethtool_get_gro(dev, useraddr);
1671 break;
1672 case ETHTOOL_SGRO:
1673 rc = ethtool_set_gro(dev, useraddr);
1674 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001675 case ETHTOOL_FLASHDEV:
1676 rc = ethtool_flash_device(dev, useraddr);
1677 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001678 case ETHTOOL_RESET:
1679 rc = ethtool_reset(dev, useraddr);
1680 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001681 case ETHTOOL_SRXNTUPLE:
1682 rc = ethtool_set_rx_ntuple(dev, useraddr);
1683 break;
1684 case ETHTOOL_GRXNTUPLE:
1685 rc = ethtool_get_rx_ntuple(dev, useraddr);
1686 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001687 case ETHTOOL_GSSET_INFO:
1688 rc = ethtool_get_sset_info(dev, useraddr);
1689 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001690 case ETHTOOL_GRXFHINDIR:
1691 rc = ethtool_get_rxfh_indir(dev, useraddr);
1692 break;
1693 case ETHTOOL_SRXFHINDIR:
1694 rc = ethtool_set_rxfh_indir(dev, useraddr);
1695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001697 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001699
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001700 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001702
1703 if (old_features != dev->features)
1704 netdev_features_change(dev);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}