blob: c3fb8f90de6d492caadb37af517f3d1869318197 [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
Michał Mirosław340ae162011-02-15 16:59:16 +0000175static int __ethtool_get_sset_count(struct net_device *dev, int sset)
176{
177 const struct ethtool_ops *ops = dev->ethtool_ops;
178
179 if (ops && ops->get_sset_count && ops->get_strings)
180 return ops->get_sset_count(dev, sset);
181 else
182 return -EOPNOTSUPP;
183}
184
185static void __ethtool_get_strings(struct net_device *dev,
186 u32 stringset, u8 *data)
187{
188 const struct ethtool_ops *ops = dev->ethtool_ops;
189
190 /* ops->get_strings is valid because checked earlier */
191 ops->get_strings(dev, stringset, data);
192}
193
Michał Mirosław0a417702011-02-15 16:59:17 +0000194static u32 ethtool_get_feature_mask(u32 eth_cmd)
195{
196 /* feature masks of legacy discrete ethtool ops */
197
198 switch (eth_cmd) {
199 case ETHTOOL_GTXCSUM:
200 case ETHTOOL_STXCSUM:
201 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
202 case ETHTOOL_GSG:
203 case ETHTOOL_SSG:
204 return NETIF_F_SG;
205 case ETHTOOL_GTSO:
206 case ETHTOOL_STSO:
207 return NETIF_F_ALL_TSO;
208 case ETHTOOL_GUFO:
209 case ETHTOOL_SUFO:
210 return NETIF_F_UFO;
211 case ETHTOOL_GGSO:
212 case ETHTOOL_SGSO:
213 return NETIF_F_GSO;
214 case ETHTOOL_GGRO:
215 case ETHTOOL_SGRO:
216 return NETIF_F_GRO;
217 default:
218 BUG();
219 }
220}
221
222static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
223{
224 const struct ethtool_ops *ops = dev->ethtool_ops;
225
226 if (!ops)
227 return NULL;
228
229 switch (ethcmd) {
230 case ETHTOOL_GTXCSUM:
231 return ops->get_tx_csum;
232 case ETHTOOL_SSG:
233 return ops->get_sg;
234 case ETHTOOL_STSO:
235 return ops->get_tso;
236 case ETHTOOL_SUFO:
237 return ops->get_ufo;
238 default:
239 return NULL;
240 }
241}
242
243static int ethtool_get_one_feature(struct net_device *dev,
244 char __user *useraddr, u32 ethcmd)
245{
246 struct ethtool_value edata = {
247 .cmd = ethcmd,
248 .data = !!(dev->features & ethtool_get_feature_mask(ethcmd)),
249 };
250 u32 (*actor)(struct net_device *);
251
252 actor = __ethtool_get_one_feature_actor(dev, ethcmd);
253 if (actor)
254 edata.data = actor(dev);
255
256 if (copy_to_user(useraddr, &edata, sizeof(edata)))
257 return -EFAULT;
258 return 0;
259}
260
261static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
262static int __ethtool_set_sg(struct net_device *dev, u32 data);
263static int __ethtool_set_tso(struct net_device *dev, u32 data);
264static int __ethtool_set_ufo(struct net_device *dev, u32 data);
265
266static int ethtool_set_one_feature(struct net_device *dev,
267 void __user *useraddr, u32 ethcmd)
268{
269 struct ethtool_value edata;
270 u32 mask;
271
272 if (copy_from_user(&edata, useraddr, sizeof(edata)))
273 return -EFAULT;
274
275 switch (ethcmd) {
276 case ETHTOOL_STXCSUM:
277 return __ethtool_set_tx_csum(dev, edata.data);
278 case ETHTOOL_SSG:
279 return __ethtool_set_sg(dev, edata.data);
280 case ETHTOOL_STSO:
281 return __ethtool_set_tso(dev, edata.data);
282 case ETHTOOL_SUFO:
283 return __ethtool_set_ufo(dev, edata.data);
284 case ETHTOOL_SGSO:
285 case ETHTOOL_SGRO:
286 mask = ethtool_get_feature_mask(ethcmd);
287 if (edata.data)
288 dev->features |= mask;
289 else
290 dev->features &= ~mask;
291 return 0;
292 default:
293 return -EOPNOTSUPP;
294 }
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
298{
Roland Dreier8e557422010-02-11 12:14:23 -0800299 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int err;
301
302 if (!dev->ethtool_ops->get_settings)
303 return -EOPNOTSUPP;
304
305 err = dev->ethtool_ops->get_settings(dev, &cmd);
306 if (err < 0)
307 return err;
308
309 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
310 return -EFAULT;
311 return 0;
312}
313
314static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
315{
316 struct ethtool_cmd cmd;
317
318 if (!dev->ethtool_ops->set_settings)
319 return -EOPNOTSUPP;
320
321 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
322 return -EFAULT;
323
324 return dev->ethtool_ops->set_settings(dev, &cmd);
325}
326
chavey97f8aef2010-04-07 21:54:42 -0700327static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
328 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700331 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 memset(&info, 0, sizeof(info));
334 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700335 if (ops && ops->get_drvinfo) {
336 ops->get_drvinfo(dev, &info);
337 } else if (dev->dev.parent && dev->dev.parent->driver) {
338 strlcpy(info.bus_info, dev_name(dev->dev.parent),
339 sizeof(info.bus_info));
340 strlcpy(info.driver, dev->dev.parent->driver->name,
341 sizeof(info.driver));
342 } else {
343 return -EOPNOTSUPP;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jeff Garzik723b2f52010-03-03 22:51:50 +0000346 /*
347 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000348 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000349 */
Ben Hutchings01414802010-08-17 02:31:15 -0700350 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700351 int rc;
352
353 rc = ops->get_sset_count(dev, ETH_SS_TEST);
354 if (rc >= 0)
355 info.testinfo_len = rc;
356 rc = ops->get_sset_count(dev, ETH_SS_STATS);
357 if (rc >= 0)
358 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700359 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
360 if (rc >= 0)
361 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700362 }
Ben Hutchings01414802010-08-17 02:31:15 -0700363 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700365 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 info.eedump_len = ops->get_eeprom_len(dev);
367
368 if (copy_to_user(useraddr, &info, sizeof(info)))
369 return -EFAULT;
370 return 0;
371}
372
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800373static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700374 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000375{
376 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000377 u64 sset_mask;
378 int i, idx = 0, n_bits = 0, ret, rc;
379 u32 *info_buf = NULL;
380
Jeff Garzik723b2f52010-03-03 22:51:50 +0000381 if (copy_from_user(&info, useraddr, sizeof(info)))
382 return -EFAULT;
383
384 /* store copy of mask, because we zero struct later on */
385 sset_mask = info.sset_mask;
386 if (!sset_mask)
387 return 0;
388
389 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000390 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000391
392 memset(&info, 0, sizeof(info));
393 info.cmd = ETHTOOL_GSSET_INFO;
394
395 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
396 if (!info_buf)
397 return -ENOMEM;
398
399 /*
400 * fill return buffer based on input bitmask and successful
401 * get_sset_count return
402 */
403 for (i = 0; i < 64; i++) {
404 if (!(sset_mask & (1ULL << i)))
405 continue;
406
Michał Mirosław340ae162011-02-15 16:59:16 +0000407 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000408 if (rc >= 0) {
409 info.sset_mask |= (1ULL << i);
410 info_buf[idx++] = rc;
411 }
412 }
413
414 ret = -EFAULT;
415 if (copy_to_user(useraddr, &info, sizeof(info)))
416 goto out;
417
418 useraddr += offsetof(struct ethtool_sset_info, data);
419 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
420 goto out;
421
422 ret = 0;
423
424out:
425 kfree(info_buf);
426 return ret;
427}
428
chavey97f8aef2010-04-07 21:54:42 -0700429static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000430 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700431{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000432 struct ethtool_rxnfc info;
433 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700434
Santwona Behera59089d82009-02-20 00:58:13 -0800435 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700436 return -EOPNOTSUPP;
437
Ben Hutchingsbf988432010-06-28 08:45:58 +0000438 /* struct ethtool_rxnfc was originally defined for
439 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
440 * members. User-space might still be using that
441 * definition. */
442 if (cmd == ETHTOOL_SRXFH)
443 info_size = (offsetof(struct ethtool_rxnfc, data) +
444 sizeof(info.data));
445
446 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700447 return -EFAULT;
448
Ben Hutchingsbf988432010-06-28 08:45:58 +0000449 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700450}
451
chavey97f8aef2010-04-07 21:54:42 -0700452static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000453 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700454{
455 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000456 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800457 const struct ethtool_ops *ops = dev->ethtool_ops;
458 int ret;
459 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700460
Santwona Behera59089d82009-02-20 00:58:13 -0800461 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700462 return -EOPNOTSUPP;
463
Ben Hutchingsbf988432010-06-28 08:45:58 +0000464 /* struct ethtool_rxnfc was originally defined for
465 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
466 * members. User-space might still be using that
467 * definition. */
468 if (cmd == ETHTOOL_GRXFH)
469 info_size = (offsetof(struct ethtool_rxnfc, data) +
470 sizeof(info.data));
471
472 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700473 return -EFAULT;
474
Santwona Behera59089d82009-02-20 00:58:13 -0800475 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
476 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000477 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000478 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000479 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800480 if (!rule_buf)
481 return -ENOMEM;
482 }
483 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700484
Santwona Behera59089d82009-02-20 00:58:13 -0800485 ret = ops->get_rxnfc(dev, &info, rule_buf);
486 if (ret < 0)
487 goto err_out;
488
489 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000490 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800491 goto err_out;
492
493 if (rule_buf) {
494 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
495 if (copy_to_user(useraddr, rule_buf,
496 info.rule_cnt * sizeof(u32)))
497 goto err_out;
498 }
499 ret = 0;
500
501err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700502 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800503
504 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700505}
506
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000507static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
508 void __user *useraddr)
509{
510 struct ethtool_rxfh_indir *indir;
511 u32 table_size;
512 size_t full_size;
513 int ret;
514
515 if (!dev->ethtool_ops->get_rxfh_indir)
516 return -EOPNOTSUPP;
517
518 if (copy_from_user(&table_size,
519 useraddr + offsetof(struct ethtool_rxfh_indir, size),
520 sizeof(table_size)))
521 return -EFAULT;
522
523 if (table_size >
524 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
525 return -ENOMEM;
526 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
Kees Cookb00916b2010-10-11 12:23:25 -0700527 indir = kzalloc(full_size, GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000528 if (!indir)
529 return -ENOMEM;
530
531 indir->cmd = ETHTOOL_GRXFHINDIR;
532 indir->size = table_size;
533 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
534 if (ret)
535 goto out;
536
537 if (copy_to_user(useraddr, indir, full_size))
538 ret = -EFAULT;
539
540out:
541 kfree(indir);
542 return ret;
543}
544
545static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
546 void __user *useraddr)
547{
548 struct ethtool_rxfh_indir *indir;
549 u32 table_size;
550 size_t full_size;
551 int ret;
552
553 if (!dev->ethtool_ops->set_rxfh_indir)
554 return -EOPNOTSUPP;
555
556 if (copy_from_user(&table_size,
557 useraddr + offsetof(struct ethtool_rxfh_indir, size),
558 sizeof(table_size)))
559 return -EFAULT;
560
561 if (table_size >
562 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
563 return -ENOMEM;
564 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
565 indir = kmalloc(full_size, GFP_USER);
566 if (!indir)
567 return -ENOMEM;
568
569 if (copy_from_user(indir, useraddr, full_size)) {
570 ret = -EFAULT;
571 goto out;
572 }
573
574 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
575
576out:
577 kfree(indir);
578 return ret;
579}
580
Peter Waskiewicze8589112010-02-12 13:48:05 +0000581static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700582 struct ethtool_rx_ntuple_flow_spec *spec,
583 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800584{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800585
586 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000587 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
588 /* free the container */
589 kfree(fsc);
590 return;
591 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800592
593 /* Copy the whole filter over */
594 fsc->fs.flow_type = spec->flow_type;
595 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
596 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
597
598 fsc->fs.vlan_tag = spec->vlan_tag;
599 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
600 fsc->fs.data = spec->data;
601 fsc->fs.data_mask = spec->data_mask;
602 fsc->fs.action = spec->action;
603
604 /* add to the list */
605 list_add_tail_rcu(&fsc->list, &list->list);
606 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800607}
608
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000609/*
610 * ethtool does not (or did not) set masks for flow parameters that are
611 * not specified, so if both value and mask are 0 then this must be
612 * treated as equivalent to a mask with all bits set. Implement that
613 * here rather than in drivers.
614 */
615static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
616{
617 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
618 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
619
620 if (fs->flow_type != TCP_V4_FLOW &&
621 fs->flow_type != UDP_V4_FLOW &&
622 fs->flow_type != SCTP_V4_FLOW)
623 return;
624
625 if (!(entry->ip4src | mask->ip4src))
626 mask->ip4src = htonl(0xffffffff);
627 if (!(entry->ip4dst | mask->ip4dst))
628 mask->ip4dst = htonl(0xffffffff);
629 if (!(entry->psrc | mask->psrc))
630 mask->psrc = htons(0xffff);
631 if (!(entry->pdst | mask->pdst))
632 mask->pdst = htons(0xffff);
633 if (!(entry->tos | mask->tos))
634 mask->tos = 0xff;
635 if (!(fs->vlan_tag | fs->vlan_tag_mask))
636 fs->vlan_tag_mask = 0xffff;
637 if (!(fs->data | fs->data_mask))
638 fs->data_mask = 0xffffffffffffffffULL;
639}
640
chavey97f8aef2010-04-07 21:54:42 -0700641static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
642 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800643{
644 struct ethtool_rx_ntuple cmd;
645 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000646 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800647 int ret;
648
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800649 if (!(dev->features & NETIF_F_NTUPLE))
650 return -EINVAL;
651
652 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
653 return -EFAULT;
654
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000655 rx_ntuple_fix_masks(&cmd.fs);
656
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800657 /*
658 * Cache filter in dev struct for GET operation only if
659 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000660 * only if the filter was added successfully. First make sure we
661 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800662 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000663 if (!ops->get_rx_ntuple) {
664 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
665 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800666 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000667 }
668
669 ret = ops->set_rx_ntuple(dev, &cmd);
670 if (ret) {
671 kfree(fsc);
672 return ret;
673 }
674
675 if (!ops->get_rx_ntuple)
676 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800677
678 return ret;
679}
680
681static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
682{
683 struct ethtool_gstrings gstrings;
684 const struct ethtool_ops *ops = dev->ethtool_ops;
685 struct ethtool_rx_ntuple_flow_spec_container *fsc;
686 u8 *data;
687 char *p;
688 int ret, i, num_strings = 0;
689
690 if (!ops->get_sset_count)
691 return -EOPNOTSUPP;
692
693 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
694 return -EFAULT;
695
696 ret = ops->get_sset_count(dev, gstrings.string_set);
697 if (ret < 0)
698 return ret;
699
700 gstrings.len = ret;
701
Kees Cookb00916b2010-10-11 12:23:25 -0700702 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800703 if (!data)
704 return -ENOMEM;
705
706 if (ops->get_rx_ntuple) {
707 /* driver-specific filter grab */
708 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
709 goto copy;
710 }
711
712 /* default ethtool filter grab */
713 i = 0;
714 p = (char *)data;
715 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
716 sprintf(p, "Filter %d:\n", i);
717 p += ETH_GSTRING_LEN;
718 num_strings++;
719
720 switch (fsc->fs.flow_type) {
721 case TCP_V4_FLOW:
722 sprintf(p, "\tFlow Type: TCP\n");
723 p += ETH_GSTRING_LEN;
724 num_strings++;
725 break;
726 case UDP_V4_FLOW:
727 sprintf(p, "\tFlow Type: UDP\n");
728 p += ETH_GSTRING_LEN;
729 num_strings++;
730 break;
731 case SCTP_V4_FLOW:
732 sprintf(p, "\tFlow Type: SCTP\n");
733 p += ETH_GSTRING_LEN;
734 num_strings++;
735 break;
736 case AH_ESP_V4_FLOW:
737 sprintf(p, "\tFlow Type: AH ESP\n");
738 p += ETH_GSTRING_LEN;
739 num_strings++;
740 break;
741 case ESP_V4_FLOW:
742 sprintf(p, "\tFlow Type: ESP\n");
743 p += ETH_GSTRING_LEN;
744 num_strings++;
745 break;
746 case IP_USER_FLOW:
747 sprintf(p, "\tFlow Type: Raw IP\n");
748 p += ETH_GSTRING_LEN;
749 num_strings++;
750 break;
751 case IPV4_FLOW:
752 sprintf(p, "\tFlow Type: IPv4\n");
753 p += ETH_GSTRING_LEN;
754 num_strings++;
755 break;
756 default:
757 sprintf(p, "\tFlow Type: Unknown\n");
758 p += ETH_GSTRING_LEN;
759 num_strings++;
760 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000761 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800762
763 /* now the rest of the filters */
764 switch (fsc->fs.flow_type) {
765 case TCP_V4_FLOW:
766 case UDP_V4_FLOW:
767 case SCTP_V4_FLOW:
768 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700769 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800770 p += ETH_GSTRING_LEN;
771 num_strings++;
772 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700773 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800774 p += ETH_GSTRING_LEN;
775 num_strings++;
776 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700777 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800778 p += ETH_GSTRING_LEN;
779 num_strings++;
780 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700781 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800782 p += ETH_GSTRING_LEN;
783 num_strings++;
784 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700785 fsc->fs.h_u.tcp_ip4_spec.psrc,
786 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800787 p += ETH_GSTRING_LEN;
788 num_strings++;
789 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700790 fsc->fs.h_u.tcp_ip4_spec.pdst,
791 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800792 p += ETH_GSTRING_LEN;
793 num_strings++;
794 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700795 fsc->fs.h_u.tcp_ip4_spec.tos,
796 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800797 p += ETH_GSTRING_LEN;
798 num_strings++;
799 break;
800 case AH_ESP_V4_FLOW:
801 case ESP_V4_FLOW:
802 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700803 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800804 p += ETH_GSTRING_LEN;
805 num_strings++;
806 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700807 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800808 p += ETH_GSTRING_LEN;
809 num_strings++;
810 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700811 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800812 p += ETH_GSTRING_LEN;
813 num_strings++;
814 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700815 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800816 p += ETH_GSTRING_LEN;
817 num_strings++;
818 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700819 fsc->fs.h_u.ah_ip4_spec.spi,
820 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800821 p += ETH_GSTRING_LEN;
822 num_strings++;
823 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700824 fsc->fs.h_u.ah_ip4_spec.tos,
825 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800826 p += ETH_GSTRING_LEN;
827 num_strings++;
828 break;
829 case IP_USER_FLOW:
830 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000831 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800832 p += ETH_GSTRING_LEN;
833 num_strings++;
834 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000835 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800836 p += ETH_GSTRING_LEN;
837 num_strings++;
838 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000839 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800840 p += ETH_GSTRING_LEN;
841 num_strings++;
842 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000843 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800844 p += ETH_GSTRING_LEN;
845 num_strings++;
846 break;
847 case IPV4_FLOW:
848 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700849 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800850 p += ETH_GSTRING_LEN;
851 num_strings++;
852 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700853 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800854 p += ETH_GSTRING_LEN;
855 num_strings++;
856 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700857 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800858 p += ETH_GSTRING_LEN;
859 num_strings++;
860 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700861 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800862 p += ETH_GSTRING_LEN;
863 num_strings++;
864 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700865 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
866 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800867 p += ETH_GSTRING_LEN;
868 num_strings++;
869 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700870 fsc->fs.h_u.usr_ip4_spec.tos,
871 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800872 p += ETH_GSTRING_LEN;
873 num_strings++;
874 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700875 fsc->fs.h_u.usr_ip4_spec.ip_ver,
876 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800877 p += ETH_GSTRING_LEN;
878 num_strings++;
879 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700880 fsc->fs.h_u.usr_ip4_spec.proto,
881 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800882 p += ETH_GSTRING_LEN;
883 num_strings++;
884 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000885 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800886 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700887 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800888 p += ETH_GSTRING_LEN;
889 num_strings++;
890 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
891 p += ETH_GSTRING_LEN;
892 num_strings++;
893 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
894 p += ETH_GSTRING_LEN;
895 num_strings++;
896 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
897 sprintf(p, "\tAction: Drop\n");
898 else
899 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -0700900 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800901 p += ETH_GSTRING_LEN;
902 num_strings++;
903unknown_filter:
904 i++;
905 }
906copy:
907 /* indicate to userspace how many strings we actually have */
908 gstrings.len = num_strings;
909 ret = -EFAULT;
910 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
911 goto out;
912 useraddr += sizeof(gstrings);
913 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
914 goto out;
915 ret = 0;
916
917out:
918 kfree(data);
919 return ret;
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
923{
924 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700925 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 void *regbuf;
927 int reglen, ret;
928
929 if (!ops->get_regs || !ops->get_regs_len)
930 return -EOPNOTSUPP;
931
932 if (copy_from_user(&regs, useraddr, sizeof(regs)))
933 return -EFAULT;
934
935 reglen = ops->get_regs_len(dev);
936 if (regs.len > reglen)
937 regs.len = reglen;
938
Eugene Teob7c7d012011-01-24 21:05:17 -0800939 regbuf = vzalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!regbuf)
941 return -ENOMEM;
942
943 ops->get_regs(dev, &regs, regbuf);
944
945 ret = -EFAULT;
946 if (copy_to_user(useraddr, &regs, sizeof(regs)))
947 goto out;
948 useraddr += offsetof(struct ethtool_regs, data);
949 if (copy_to_user(useraddr, regbuf, regs.len))
950 goto out;
951 ret = 0;
952
953 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000954 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return ret;
956}
957
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000958static int ethtool_reset(struct net_device *dev, char __user *useraddr)
959{
960 struct ethtool_value reset;
961 int ret;
962
963 if (!dev->ethtool_ops->reset)
964 return -EOPNOTSUPP;
965
966 if (copy_from_user(&reset, useraddr, sizeof(reset)))
967 return -EFAULT;
968
969 ret = dev->ethtool_ops->reset(dev, &reset.data);
970 if (ret)
971 return ret;
972
973 if (copy_to_user(useraddr, &reset, sizeof(reset)))
974 return -EFAULT;
975 return 0;
976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
979{
Roland Dreier8e557422010-02-11 12:14:23 -0800980 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!dev->ethtool_ops->get_wol)
983 return -EOPNOTSUPP;
984
985 dev->ethtool_ops->get_wol(dev, &wol);
986
987 if (copy_to_user(useraddr, &wol, sizeof(wol)))
988 return -EFAULT;
989 return 0;
990}
991
992static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
993{
994 struct ethtool_wolinfo wol;
995
996 if (!dev->ethtool_ops->set_wol)
997 return -EOPNOTSUPP;
998
999 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1000 return -EFAULT;
1001
1002 return dev->ethtool_ops->set_wol(dev, &wol);
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005static int ethtool_nway_reset(struct net_device *dev)
1006{
1007 if (!dev->ethtool_ops->nway_reset)
1008 return -EOPNOTSUPP;
1009
1010 return dev->ethtool_ops->nway_reset(dev);
1011}
1012
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001013static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1014{
1015 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1016
1017 if (!dev->ethtool_ops->get_link)
1018 return -EOPNOTSUPP;
1019
1020 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1021
1022 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1023 return -EFAULT;
1024 return 0;
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1028{
1029 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001030 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001031 void __user *userbuf = useraddr + sizeof(eeprom);
1032 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001034 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (!ops->get_eeprom || !ops->get_eeprom_len)
1037 return -EOPNOTSUPP;
1038
1039 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1040 return -EFAULT;
1041
1042 /* Check for wrap and zero */
1043 if (eeprom.offset + eeprom.len <= eeprom.offset)
1044 return -EINVAL;
1045
1046 /* Check for exceeding total eeprom len */
1047 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1048 return -EINVAL;
1049
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001050 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!data)
1052 return -ENOMEM;
1053
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001054 bytes_remaining = eeprom.len;
1055 while (bytes_remaining > 0) {
1056 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001058 ret = ops->get_eeprom(dev, &eeprom, data);
1059 if (ret)
1060 break;
1061 if (copy_to_user(userbuf, data, eeprom.len)) {
1062 ret = -EFAULT;
1063 break;
1064 }
1065 userbuf += eeprom.len;
1066 eeprom.offset += eeprom.len;
1067 bytes_remaining -= eeprom.len;
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001070 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1071 eeprom.offset -= eeprom.len;
1072 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1073 ret = -EFAULT;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 kfree(data);
1076 return ret;
1077}
1078
1079static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1080{
1081 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001082 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001083 void __user *userbuf = useraddr + sizeof(eeprom);
1084 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001086 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (!ops->set_eeprom || !ops->get_eeprom_len)
1089 return -EOPNOTSUPP;
1090
1091 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1092 return -EFAULT;
1093
1094 /* Check for wrap and zero */
1095 if (eeprom.offset + eeprom.len <= eeprom.offset)
1096 return -EINVAL;
1097
1098 /* Check for exceeding total eeprom len */
1099 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1100 return -EINVAL;
1101
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001102 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!data)
1104 return -ENOMEM;
1105
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001106 bytes_remaining = eeprom.len;
1107 while (bytes_remaining > 0) {
1108 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001110 if (copy_from_user(data, userbuf, eeprom.len)) {
1111 ret = -EFAULT;
1112 break;
1113 }
1114 ret = ops->set_eeprom(dev, &eeprom, data);
1115 if (ret)
1116 break;
1117 userbuf += eeprom.len;
1118 eeprom.offset += eeprom.len;
1119 bytes_remaining -= eeprom.len;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 kfree(data);
1123 return ret;
1124}
1125
chavey97f8aef2010-04-07 21:54:42 -07001126static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1127 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Roland Dreier8e557422010-02-11 12:14:23 -08001129 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (!dev->ethtool_ops->get_coalesce)
1132 return -EOPNOTSUPP;
1133
1134 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1135
1136 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1137 return -EFAULT;
1138 return 0;
1139}
1140
chavey97f8aef2010-04-07 21:54:42 -07001141static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1142 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct ethtool_coalesce coalesce;
1145
David S. Millerfa04ae52005-06-06 15:07:19 -07001146 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -EOPNOTSUPP;
1148
1149 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1150 return -EFAULT;
1151
1152 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1153}
1154
1155static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1156{
Roland Dreier8e557422010-02-11 12:14:23 -08001157 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (!dev->ethtool_ops->get_ringparam)
1160 return -EOPNOTSUPP;
1161
1162 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1163
1164 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1165 return -EFAULT;
1166 return 0;
1167}
1168
1169static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1170{
1171 struct ethtool_ringparam ringparam;
1172
1173 if (!dev->ethtool_ops->set_ringparam)
1174 return -EOPNOTSUPP;
1175
1176 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1177 return -EFAULT;
1178
1179 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1180}
1181
1182static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1183{
1184 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1185
1186 if (!dev->ethtool_ops->get_pauseparam)
1187 return -EOPNOTSUPP;
1188
1189 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1190
1191 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1192 return -EFAULT;
1193 return 0;
1194}
1195
1196static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1197{
1198 struct ethtool_pauseparam pauseparam;
1199
Jeff Garzike1b90c42006-07-17 12:54:40 -04001200 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return -EOPNOTSUPP;
1202
1203 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1204 return -EFAULT;
1205
1206 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1207}
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209static int __ethtool_set_sg(struct net_device *dev, u32 data)
1210{
1211 int err;
1212
Michał Mirosław0a417702011-02-15 16:59:17 +00001213 if (data && !(dev->features & NETIF_F_ALL_CSUM))
1214 return -EINVAL;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!data && dev->ethtool_ops->set_tso) {
1217 err = dev->ethtool_ops->set_tso(dev, 0);
1218 if (err)
1219 return err;
1220 }
1221
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001222 if (!data && dev->ethtool_ops->set_ufo) {
1223 err = dev->ethtool_ops->set_ufo(dev, 0);
1224 if (err)
1225 return err;
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return dev->ethtool_ops->set_sg(dev, data);
1228}
1229
Michał Mirosław0a417702011-02-15 16:59:17 +00001230static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233
1234 if (!dev->ethtool_ops->set_tx_csum)
1235 return -EOPNOTSUPP;
1236
Michał Mirosław0a417702011-02-15 16:59:17 +00001237 if (!data && dev->ethtool_ops->set_sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 err = __ethtool_set_sg(dev, 0);
1239 if (err)
1240 return err;
1241 }
1242
Michał Mirosław0a417702011-02-15 16:59:17 +00001243 return dev->ethtool_ops->set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Herbert Xub240a0e2008-12-15 23:44:31 -08001246static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1247{
1248 struct ethtool_value edata;
1249
1250 if (!dev->ethtool_ops->set_rx_csum)
1251 return -EOPNOTSUPP;
1252
1253 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1254 return -EFAULT;
1255
1256 if (!edata.data && dev->ethtool_ops->set_sg)
1257 dev->features &= ~NETIF_F_GRO;
1258
1259 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1260}
1261
Michał Mirosław0a417702011-02-15 16:59:17 +00001262static int __ethtool_set_tso(struct net_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (!dev->ethtool_ops->set_tso)
1265 return -EOPNOTSUPP;
1266
Michał Mirosław0a417702011-02-15 16:59:17 +00001267 if (data && !(dev->features & NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -EINVAL;
1269
Michał Mirosław0a417702011-02-15 16:59:17 +00001270 return dev->ethtool_ops->set_tso(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Michał Mirosław0a417702011-02-15 16:59:17 +00001273static int __ethtool_set_ufo(struct net_device *dev, u32 data)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001274{
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001275 if (!dev->ethtool_ops->set_ufo)
1276 return -EOPNOTSUPP;
Michał Mirosław0a417702011-02-15 16:59:17 +00001277 if (data && !(dev->features & NETIF_F_SG))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001278 return -EINVAL;
Michał Mirosław0a417702011-02-15 16:59:17 +00001279 if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
Michał Mirosław79032642010-11-30 06:38:00 +00001280 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1281 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001282 return -EINVAL;
Michał Mirosław0a417702011-02-15 16:59:17 +00001283 return dev->ethtool_ops->set_ufo(dev, data);
Herbert Xub240a0e2008-12-15 23:44:31 -08001284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1287{
1288 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001289 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001291 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001293 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001294 return -EOPNOTSUPP;
1295
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001296 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001297 if (test_len < 0)
1298 return test_len;
1299 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (copy_from_user(&test, useraddr, sizeof(test)))
1302 return -EFAULT;
1303
Jeff Garzikff03d492007-08-15 16:01:08 -07001304 test.len = test_len;
1305 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!data)
1307 return -ENOMEM;
1308
1309 ops->self_test(dev, &test, data);
1310
1311 ret = -EFAULT;
1312 if (copy_to_user(useraddr, &test, sizeof(test)))
1313 goto out;
1314 useraddr += sizeof(test);
1315 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1316 goto out;
1317 ret = 0;
1318
1319 out:
1320 kfree(data);
1321 return ret;
1322}
1323
1324static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1325{
1326 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 u8 *data;
1328 int ret;
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1331 return -EFAULT;
1332
Michał Mirosław340ae162011-02-15 16:59:16 +00001333 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001334 if (ret < 0)
1335 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001336
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001337 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1340 if (!data)
1341 return -ENOMEM;
1342
Michał Mirosław340ae162011-02-15 16:59:16 +00001343 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 ret = -EFAULT;
1346 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1347 goto out;
1348 useraddr += sizeof(gstrings);
1349 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1350 goto out;
1351 ret = 0;
1352
Michał Mirosław340ae162011-02-15 16:59:16 +00001353out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 kfree(data);
1355 return ret;
1356}
1357
1358static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1359{
1360 struct ethtool_value id;
1361
1362 if (!dev->ethtool_ops->phys_id)
1363 return -EOPNOTSUPP;
1364
1365 if (copy_from_user(&id, useraddr, sizeof(id)))
1366 return -EFAULT;
1367
1368 return dev->ethtool_ops->phys_id(dev, id.data);
1369}
1370
1371static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1372{
1373 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001374 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001376 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001378 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001379 return -EOPNOTSUPP;
1380
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001381 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001382 if (n_stats < 0)
1383 return n_stats;
1384 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1387 return -EFAULT;
1388
Jeff Garzikff03d492007-08-15 16:01:08 -07001389 stats.n_stats = n_stats;
1390 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!data)
1392 return -ENOMEM;
1393
1394 ops->get_ethtool_stats(dev, &stats, data);
1395
1396 ret = -EFAULT;
1397 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1398 goto out;
1399 useraddr += sizeof(stats);
1400 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1401 goto out;
1402 ret = 0;
1403
1404 out:
1405 kfree(data);
1406 return ret;
1407}
1408
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001409static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001410{
1411 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001412
Matthew Wilcox313674a2007-07-31 14:00:29 -07001413 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001414 return -EFAULT;
1415
Matthew Wilcox313674a2007-07-31 14:00:29 -07001416 if (epaddr.size < dev->addr_len)
1417 return -ETOOSMALL;
1418 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001419
Jon Wetzela6f9a702005-08-20 17:15:54 -07001420 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001421 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001422 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001423 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1424 return -EFAULT;
1425 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001426}
1427
Jeff Garzik13c99b22007-08-15 16:01:56 -07001428static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1429 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001430{
Roland Dreier8e557422010-02-11 12:14:23 -08001431 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001432
Jeff Garzik13c99b22007-08-15 16:01:56 -07001433 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001434 return -EOPNOTSUPP;
1435
Jeff Garzik13c99b22007-08-15 16:01:56 -07001436 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001437
1438 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1439 return -EFAULT;
1440 return 0;
1441}
1442
Jeff Garzik13c99b22007-08-15 16:01:56 -07001443static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1444 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001445{
1446 struct ethtool_value edata;
1447
Jeff Garzik13c99b22007-08-15 16:01:56 -07001448 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001449 return -EOPNOTSUPP;
1450
1451 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1452 return -EFAULT;
1453
Jeff Garzik13c99b22007-08-15 16:01:56 -07001454 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001455 return 0;
1456}
1457
Jeff Garzik13c99b22007-08-15 16:01:56 -07001458static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1459 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001460{
1461 struct ethtool_value edata;
1462
Jeff Garzik13c99b22007-08-15 16:01:56 -07001463 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001464 return -EOPNOTSUPP;
1465
1466 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1467 return -EFAULT;
1468
Jeff Garzik13c99b22007-08-15 16:01:56 -07001469 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001470}
1471
chavey97f8aef2010-04-07 21:54:42 -07001472static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1473 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001474{
1475 struct ethtool_flash efl;
1476
1477 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1478 return -EFAULT;
1479
1480 if (!dev->ethtool_ops->flash_device)
1481 return -EOPNOTSUPP;
1482
1483 return dev->ethtool_ops->flash_device(dev, &efl);
1484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/* The main entry point in this file. Called from net/core/dev.c */
1487
Eric W. Biederman881d9662007-09-17 11:56:21 -07001488int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001490 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 void __user *useraddr = ifr->ifr_data;
1492 u32 ethcmd;
1493 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001494 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (!dev || !netif_device_present(dev))
1497 return -ENODEV;
1498
chavey97f8aef2010-04-07 21:54:42 -07001499 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EFAULT;
1501
Ben Hutchings01414802010-08-17 02:31:15 -07001502 if (!dev->ethtool_ops) {
1503 /* ETHTOOL_GDRVINFO does not require any driver support.
1504 * It is also unprivileged and does not change anything,
1505 * so we can take a shortcut to it. */
1506 if (ethcmd == ETHTOOL_GDRVINFO)
1507 return ethtool_get_drvinfo(dev, useraddr);
1508 else
1509 return -EOPNOTSUPP;
1510 }
1511
Stephen Hemminger75f31232006-09-28 15:13:37 -07001512 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001513 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001514 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001515 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001516 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001517 case ETHTOOL_GCOALESCE:
1518 case ETHTOOL_GRINGPARAM:
1519 case ETHTOOL_GPAUSEPARAM:
1520 case ETHTOOL_GRXCSUM:
1521 case ETHTOOL_GTXCSUM:
1522 case ETHTOOL_GSG:
1523 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001524 case ETHTOOL_GTSO:
1525 case ETHTOOL_GPERMADDR:
1526 case ETHTOOL_GUFO:
1527 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001528 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001529 case ETHTOOL_GFLAGS:
1530 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001531 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001532 case ETHTOOL_GRXRINGS:
1533 case ETHTOOL_GRXCLSRLCNT:
1534 case ETHTOOL_GRXCLSRULE:
1535 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001536 break;
1537 default:
1538 if (!capable(CAP_NET_ADMIN))
1539 return -EPERM;
1540 }
1541
chavey97f8aef2010-04-07 21:54:42 -07001542 if (dev->ethtool_ops->begin) {
1543 rc = dev->ethtool_ops->begin(dev);
1544 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001546 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001547 old_features = dev->features;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 switch (ethcmd) {
1550 case ETHTOOL_GSET:
1551 rc = ethtool_get_settings(dev, useraddr);
1552 break;
1553 case ETHTOOL_SSET:
1554 rc = ethtool_set_settings(dev, useraddr);
1555 break;
1556 case ETHTOOL_GDRVINFO:
1557 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
1559 case ETHTOOL_GREGS:
1560 rc = ethtool_get_regs(dev, useraddr);
1561 break;
1562 case ETHTOOL_GWOL:
1563 rc = ethtool_get_wol(dev, useraddr);
1564 break;
1565 case ETHTOOL_SWOL:
1566 rc = ethtool_set_wol(dev, useraddr);
1567 break;
1568 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001569 rc = ethtool_get_value(dev, useraddr, ethcmd,
1570 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001573 rc = ethtool_set_value_void(dev, useraddr,
1574 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 break;
1576 case ETHTOOL_NWAY_RST:
1577 rc = ethtool_nway_reset(dev);
1578 break;
1579 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001580 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 break;
1582 case ETHTOOL_GEEPROM:
1583 rc = ethtool_get_eeprom(dev, useraddr);
1584 break;
1585 case ETHTOOL_SEEPROM:
1586 rc = ethtool_set_eeprom(dev, useraddr);
1587 break;
1588 case ETHTOOL_GCOALESCE:
1589 rc = ethtool_get_coalesce(dev, useraddr);
1590 break;
1591 case ETHTOOL_SCOALESCE:
1592 rc = ethtool_set_coalesce(dev, useraddr);
1593 break;
1594 case ETHTOOL_GRINGPARAM:
1595 rc = ethtool_get_ringparam(dev, useraddr);
1596 break;
1597 case ETHTOOL_SRINGPARAM:
1598 rc = ethtool_set_ringparam(dev, useraddr);
1599 break;
1600 case ETHTOOL_GPAUSEPARAM:
1601 rc = ethtool_get_pauseparam(dev, useraddr);
1602 break;
1603 case ETHTOOL_SPAUSEPARAM:
1604 rc = ethtool_set_pauseparam(dev, useraddr);
1605 break;
1606 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001607 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001608 (dev->ethtool_ops->get_rx_csum ?
1609 dev->ethtool_ops->get_rx_csum :
1610 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 break;
1612 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001613 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 case ETHTOOL_TEST:
1616 rc = ethtool_self_test(dev, useraddr);
1617 break;
1618 case ETHTOOL_GSTRINGS:
1619 rc = ethtool_get_strings(dev, useraddr);
1620 break;
1621 case ETHTOOL_PHYS_ID:
1622 rc = ethtool_phys_id(dev, useraddr);
1623 break;
1624 case ETHTOOL_GSTATS:
1625 rc = ethtool_get_stats(dev, useraddr);
1626 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001627 case ETHTOOL_GPERMADDR:
1628 rc = ethtool_get_perm_addr(dev, useraddr);
1629 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001630 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001631 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001632 (dev->ethtool_ops->get_flags ?
1633 dev->ethtool_ops->get_flags :
1634 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001635 break;
1636 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001637 rc = ethtool_set_value(dev, useraddr,
1638 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001639 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001640 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001641 rc = ethtool_get_value(dev, useraddr, ethcmd,
1642 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001643 break;
1644 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001645 rc = ethtool_set_value(dev, useraddr,
1646 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001647 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001648 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001649 case ETHTOOL_GRXRINGS:
1650 case ETHTOOL_GRXCLSRLCNT:
1651 case ETHTOOL_GRXCLSRULE:
1652 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001653 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001654 break;
1655 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001656 case ETHTOOL_SRXCLSRLDEL:
1657 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001658 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001659 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001660 case ETHTOOL_FLASHDEV:
1661 rc = ethtool_flash_device(dev, useraddr);
1662 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001663 case ETHTOOL_RESET:
1664 rc = ethtool_reset(dev, useraddr);
1665 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001666 case ETHTOOL_SRXNTUPLE:
1667 rc = ethtool_set_rx_ntuple(dev, useraddr);
1668 break;
1669 case ETHTOOL_GRXNTUPLE:
1670 rc = ethtool_get_rx_ntuple(dev, useraddr);
1671 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001672 case ETHTOOL_GSSET_INFO:
1673 rc = ethtool_get_sset_info(dev, useraddr);
1674 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001675 case ETHTOOL_GRXFHINDIR:
1676 rc = ethtool_get_rxfh_indir(dev, useraddr);
1677 break;
1678 case ETHTOOL_SRXFHINDIR:
1679 rc = ethtool_set_rxfh_indir(dev, useraddr);
1680 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001681 case ETHTOOL_GTXCSUM:
1682 case ETHTOOL_GSG:
1683 case ETHTOOL_GTSO:
1684 case ETHTOOL_GUFO:
1685 case ETHTOOL_GGSO:
1686 case ETHTOOL_GGRO:
1687 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1688 break;
1689 case ETHTOOL_STXCSUM:
1690 case ETHTOOL_SSG:
1691 case ETHTOOL_STSO:
1692 case ETHTOOL_SUFO:
1693 case ETHTOOL_SGSO:
1694 case ETHTOOL_SGRO:
1695 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001698 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001700
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001701 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001703
1704 if (old_features != dev->features)
1705 netdev_features_change(dev);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}