blob: d5bc288188830970a4e0439f7e26b4b131082f00 [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}
58
Jon Mason69f6a0f2005-05-29 20:27:24 -070059int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
60{
61 if (data)
62 dev->features |= NETIF_F_HW_CSUM;
63 else
64 dev->features &= ~NETIF_F_HW_CSUM;
65
66 return 0;
67}
chavey97f8aef2010-04-07 21:54:42 -070068EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -070069
70int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
71{
72 if (data)
73 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
74 else
75 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
76
77 return 0;
78}
chavey97f8aef2010-04-07 21:54:42 -070079EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Michael Chan6460d942007-07-14 19:07:52 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081u32 ethtool_op_get_sg(struct net_device *dev)
82{
83 return (dev->features & NETIF_F_SG) != 0;
84}
chavey97f8aef2010-04-07 21:54:42 -070085EXPORT_SYMBOL(ethtool_op_get_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87int ethtool_op_set_sg(struct net_device *dev, u32 data)
88{
89 if (data)
90 dev->features |= NETIF_F_SG;
91 else
92 dev->features &= ~NETIF_F_SG;
93
94 return 0;
95}
chavey97f8aef2010-04-07 21:54:42 -070096EXPORT_SYMBOL(ethtool_op_set_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98u32 ethtool_op_get_tso(struct net_device *dev)
99{
100 return (dev->features & NETIF_F_TSO) != 0;
101}
chavey97f8aef2010-04-07 21:54:42 -0700102EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104int ethtool_op_set_tso(struct net_device *dev, u32 data)
105{
106 if (data)
107 dev->features |= NETIF_F_TSO;
108 else
109 dev->features &= ~NETIF_F_TSO;
110
111 return 0;
112}
chavey97f8aef2010-04-07 21:54:42 -0700113EXPORT_SYMBOL(ethtool_op_set_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700115u32 ethtool_op_get_ufo(struct net_device *dev)
116{
117 return (dev->features & NETIF_F_UFO) != 0;
118}
chavey97f8aef2010-04-07 21:54:42 -0700119EXPORT_SYMBOL(ethtool_op_get_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700120
121int ethtool_op_set_ufo(struct net_device *dev, u32 data)
122{
123 if (data)
124 dev->features |= NETIF_F_UFO;
125 else
126 dev->features &= ~NETIF_F_UFO;
127 return 0;
128}
chavey97f8aef2010-04-07 21:54:42 -0700129EXPORT_SYMBOL(ethtool_op_set_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700130
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700131/* the following list of flags are the same as their associated
132 * NETIF_F_xxx values in include/linux/netdevice.h
133 */
134static const u32 flags_dup_features =
Jesse Grossd5dbda22010-10-20 13:56:07 +0000135 (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
136 ETH_FLAG_RXHASH);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700137
138u32 ethtool_op_get_flags(struct net_device *dev)
139{
140 /* in the future, this function will probably contain additional
141 * handling for flags which are not so easily handled
142 * by a simple masking operation
143 */
144
145 return dev->features & flags_dup_features;
146}
chavey97f8aef2010-04-07 21:54:42 -0700147EXPORT_SYMBOL(ethtool_op_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700148
Ben Hutchings1437ce32010-06-30 02:44:32 +0000149int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700150{
Ben Hutchings1437ce32010-06-30 02:44:32 +0000151 if (data & ~supported)
152 return -EINVAL;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000153
Ben Hutchings1437ce32010-06-30 02:44:32 +0000154 dev->features = ((dev->features & ~flags_dup_features) |
155 (data & flags_dup_features));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700156 return 0;
157}
chavey97f8aef2010-04-07 21:54:42 -0700158EXPORT_SYMBOL(ethtool_op_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700159
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800160void ethtool_ntuple_flush(struct net_device *dev)
161{
162 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
163
164 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
165 list_del(&fsc->list);
166 kfree(fsc);
167 }
168 dev->ethtool_ntuple_list.count = 0;
169}
170EXPORT_SYMBOL(ethtool_ntuple_flush);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* Handlers for each ethtool command */
173
174static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
175{
Roland Dreier8e557422010-02-11 12:14:23 -0800176 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int err;
178
179 if (!dev->ethtool_ops->get_settings)
180 return -EOPNOTSUPP;
181
182 err = dev->ethtool_ops->get_settings(dev, &cmd);
183 if (err < 0)
184 return err;
185
186 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
187 return -EFAULT;
188 return 0;
189}
190
191static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
192{
193 struct ethtool_cmd cmd;
194
195 if (!dev->ethtool_ops->set_settings)
196 return -EOPNOTSUPP;
197
198 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
199 return -EFAULT;
200
201 return dev->ethtool_ops->set_settings(dev, &cmd);
202}
203
chavey97f8aef2010-04-07 21:54:42 -0700204static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
205 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700208 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memset(&info, 0, sizeof(info));
211 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700212 if (ops && ops->get_drvinfo) {
213 ops->get_drvinfo(dev, &info);
214 } else if (dev->dev.parent && dev->dev.parent->driver) {
215 strlcpy(info.bus_info, dev_name(dev->dev.parent),
216 sizeof(info.bus_info));
217 strlcpy(info.driver, dev->dev.parent->driver->name,
218 sizeof(info.driver));
219 } else {
220 return -EOPNOTSUPP;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Jeff Garzik723b2f52010-03-03 22:51:50 +0000223 /*
224 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000225 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000226 */
Ben Hutchings01414802010-08-17 02:31:15 -0700227 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700228 int rc;
229
230 rc = ops->get_sset_count(dev, ETH_SS_TEST);
231 if (rc >= 0)
232 info.testinfo_len = rc;
233 rc = ops->get_sset_count(dev, ETH_SS_STATS);
234 if (rc >= 0)
235 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700236 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
237 if (rc >= 0)
238 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700239 }
Ben Hutchings01414802010-08-17 02:31:15 -0700240 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700242 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 info.eedump_len = ops->get_eeprom_len(dev);
244
245 if (copy_to_user(useraddr, &info, sizeof(info)))
246 return -EFAULT;
247 return 0;
248}
249
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800250static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700251 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000252{
253 struct ethtool_sset_info info;
254 const struct ethtool_ops *ops = dev->ethtool_ops;
255 u64 sset_mask;
256 int i, idx = 0, n_bits = 0, ret, rc;
257 u32 *info_buf = NULL;
258
259 if (!ops->get_sset_count)
260 return -EOPNOTSUPP;
261
262 if (copy_from_user(&info, useraddr, sizeof(info)))
263 return -EFAULT;
264
265 /* store copy of mask, because we zero struct later on */
266 sset_mask = info.sset_mask;
267 if (!sset_mask)
268 return 0;
269
270 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000271 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000272
273 memset(&info, 0, sizeof(info));
274 info.cmd = ETHTOOL_GSSET_INFO;
275
276 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
277 if (!info_buf)
278 return -ENOMEM;
279
280 /*
281 * fill return buffer based on input bitmask and successful
282 * get_sset_count return
283 */
284 for (i = 0; i < 64; i++) {
285 if (!(sset_mask & (1ULL << i)))
286 continue;
287
288 rc = ops->get_sset_count(dev, i);
289 if (rc >= 0) {
290 info.sset_mask |= (1ULL << i);
291 info_buf[idx++] = rc;
292 }
293 }
294
295 ret = -EFAULT;
296 if (copy_to_user(useraddr, &info, sizeof(info)))
297 goto out;
298
299 useraddr += offsetof(struct ethtool_sset_info, data);
300 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
301 goto out;
302
303 ret = 0;
304
305out:
306 kfree(info_buf);
307 return ret;
308}
309
chavey97f8aef2010-04-07 21:54:42 -0700310static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000311 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700312{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000313 struct ethtool_rxnfc info;
314 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700315
Santwona Behera59089d82009-02-20 00:58:13 -0800316 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700317 return -EOPNOTSUPP;
318
Ben Hutchingsbf988432010-06-28 08:45:58 +0000319 /* struct ethtool_rxnfc was originally defined for
320 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
321 * members. User-space might still be using that
322 * definition. */
323 if (cmd == ETHTOOL_SRXFH)
324 info_size = (offsetof(struct ethtool_rxnfc, data) +
325 sizeof(info.data));
326
327 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700328 return -EFAULT;
329
Ben Hutchingsbf988432010-06-28 08:45:58 +0000330 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700331}
332
chavey97f8aef2010-04-07 21:54:42 -0700333static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000334 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700335{
336 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000337 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800338 const struct ethtool_ops *ops = dev->ethtool_ops;
339 int ret;
340 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700341
Santwona Behera59089d82009-02-20 00:58:13 -0800342 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700343 return -EOPNOTSUPP;
344
Ben Hutchingsbf988432010-06-28 08:45:58 +0000345 /* struct ethtool_rxnfc was originally defined for
346 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
347 * members. User-space might still be using that
348 * definition. */
349 if (cmd == ETHTOOL_GRXFH)
350 info_size = (offsetof(struct ethtool_rxnfc, data) +
351 sizeof(info.data));
352
353 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700354 return -EFAULT;
355
Santwona Behera59089d82009-02-20 00:58:13 -0800356 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
357 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000358 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000359 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000360 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800361 if (!rule_buf)
362 return -ENOMEM;
363 }
364 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700365
Santwona Behera59089d82009-02-20 00:58:13 -0800366 ret = ops->get_rxnfc(dev, &info, rule_buf);
367 if (ret < 0)
368 goto err_out;
369
370 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000371 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800372 goto err_out;
373
374 if (rule_buf) {
375 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
376 if (copy_to_user(useraddr, rule_buf,
377 info.rule_cnt * sizeof(u32)))
378 goto err_out;
379 }
380 ret = 0;
381
382err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700383 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800384
385 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700386}
387
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000388static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
389 void __user *useraddr)
390{
391 struct ethtool_rxfh_indir *indir;
392 u32 table_size;
393 size_t full_size;
394 int ret;
395
396 if (!dev->ethtool_ops->get_rxfh_indir)
397 return -EOPNOTSUPP;
398
399 if (copy_from_user(&table_size,
400 useraddr + offsetof(struct ethtool_rxfh_indir, size),
401 sizeof(table_size)))
402 return -EFAULT;
403
404 if (table_size >
405 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
406 return -ENOMEM;
407 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
Kees Cookb00916b2010-10-11 12:23:25 -0700408 indir = kzalloc(full_size, GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000409 if (!indir)
410 return -ENOMEM;
411
412 indir->cmd = ETHTOOL_GRXFHINDIR;
413 indir->size = table_size;
414 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
415 if (ret)
416 goto out;
417
418 if (copy_to_user(useraddr, indir, full_size))
419 ret = -EFAULT;
420
421out:
422 kfree(indir);
423 return ret;
424}
425
426static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
427 void __user *useraddr)
428{
429 struct ethtool_rxfh_indir *indir;
430 u32 table_size;
431 size_t full_size;
432 int ret;
433
434 if (!dev->ethtool_ops->set_rxfh_indir)
435 return -EOPNOTSUPP;
436
437 if (copy_from_user(&table_size,
438 useraddr + offsetof(struct ethtool_rxfh_indir, size),
439 sizeof(table_size)))
440 return -EFAULT;
441
442 if (table_size >
443 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
444 return -ENOMEM;
445 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
446 indir = kmalloc(full_size, GFP_USER);
447 if (!indir)
448 return -ENOMEM;
449
450 if (copy_from_user(indir, useraddr, full_size)) {
451 ret = -EFAULT;
452 goto out;
453 }
454
455 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
456
457out:
458 kfree(indir);
459 return ret;
460}
461
Peter Waskiewicze8589112010-02-12 13:48:05 +0000462static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700463 struct ethtool_rx_ntuple_flow_spec *spec,
464 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800465{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800466
467 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000468 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
469 /* free the container */
470 kfree(fsc);
471 return;
472 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800473
474 /* Copy the whole filter over */
475 fsc->fs.flow_type = spec->flow_type;
476 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
477 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
478
479 fsc->fs.vlan_tag = spec->vlan_tag;
480 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
481 fsc->fs.data = spec->data;
482 fsc->fs.data_mask = spec->data_mask;
483 fsc->fs.action = spec->action;
484
485 /* add to the list */
486 list_add_tail_rcu(&fsc->list, &list->list);
487 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800488}
489
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000490/*
491 * ethtool does not (or did not) set masks for flow parameters that are
492 * not specified, so if both value and mask are 0 then this must be
493 * treated as equivalent to a mask with all bits set. Implement that
494 * here rather than in drivers.
495 */
496static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
497{
498 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
499 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
500
501 if (fs->flow_type != TCP_V4_FLOW &&
502 fs->flow_type != UDP_V4_FLOW &&
503 fs->flow_type != SCTP_V4_FLOW)
504 return;
505
506 if (!(entry->ip4src | mask->ip4src))
507 mask->ip4src = htonl(0xffffffff);
508 if (!(entry->ip4dst | mask->ip4dst))
509 mask->ip4dst = htonl(0xffffffff);
510 if (!(entry->psrc | mask->psrc))
511 mask->psrc = htons(0xffff);
512 if (!(entry->pdst | mask->pdst))
513 mask->pdst = htons(0xffff);
514 if (!(entry->tos | mask->tos))
515 mask->tos = 0xff;
516 if (!(fs->vlan_tag | fs->vlan_tag_mask))
517 fs->vlan_tag_mask = 0xffff;
518 if (!(fs->data | fs->data_mask))
519 fs->data_mask = 0xffffffffffffffffULL;
520}
521
chavey97f8aef2010-04-07 21:54:42 -0700522static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
523 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800524{
525 struct ethtool_rx_ntuple cmd;
526 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000527 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800528 int ret;
529
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800530 if (!(dev->features & NETIF_F_NTUPLE))
531 return -EINVAL;
532
533 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
534 return -EFAULT;
535
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000536 rx_ntuple_fix_masks(&cmd.fs);
537
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800538 /*
539 * Cache filter in dev struct for GET operation only if
540 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000541 * only if the filter was added successfully. First make sure we
542 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800543 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000544 if (!ops->get_rx_ntuple) {
545 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
546 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800547 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000548 }
549
550 ret = ops->set_rx_ntuple(dev, &cmd);
551 if (ret) {
552 kfree(fsc);
553 return ret;
554 }
555
556 if (!ops->get_rx_ntuple)
557 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800558
559 return ret;
560}
561
562static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
563{
564 struct ethtool_gstrings gstrings;
565 const struct ethtool_ops *ops = dev->ethtool_ops;
566 struct ethtool_rx_ntuple_flow_spec_container *fsc;
567 u8 *data;
568 char *p;
569 int ret, i, num_strings = 0;
570
571 if (!ops->get_sset_count)
572 return -EOPNOTSUPP;
573
574 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
575 return -EFAULT;
576
577 ret = ops->get_sset_count(dev, gstrings.string_set);
578 if (ret < 0)
579 return ret;
580
581 gstrings.len = ret;
582
Kees Cookb00916b2010-10-11 12:23:25 -0700583 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800584 if (!data)
585 return -ENOMEM;
586
587 if (ops->get_rx_ntuple) {
588 /* driver-specific filter grab */
589 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
590 goto copy;
591 }
592
593 /* default ethtool filter grab */
594 i = 0;
595 p = (char *)data;
596 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
597 sprintf(p, "Filter %d:\n", i);
598 p += ETH_GSTRING_LEN;
599 num_strings++;
600
601 switch (fsc->fs.flow_type) {
602 case TCP_V4_FLOW:
603 sprintf(p, "\tFlow Type: TCP\n");
604 p += ETH_GSTRING_LEN;
605 num_strings++;
606 break;
607 case UDP_V4_FLOW:
608 sprintf(p, "\tFlow Type: UDP\n");
609 p += ETH_GSTRING_LEN;
610 num_strings++;
611 break;
612 case SCTP_V4_FLOW:
613 sprintf(p, "\tFlow Type: SCTP\n");
614 p += ETH_GSTRING_LEN;
615 num_strings++;
616 break;
617 case AH_ESP_V4_FLOW:
618 sprintf(p, "\tFlow Type: AH ESP\n");
619 p += ETH_GSTRING_LEN;
620 num_strings++;
621 break;
622 case ESP_V4_FLOW:
623 sprintf(p, "\tFlow Type: ESP\n");
624 p += ETH_GSTRING_LEN;
625 num_strings++;
626 break;
627 case IP_USER_FLOW:
628 sprintf(p, "\tFlow Type: Raw IP\n");
629 p += ETH_GSTRING_LEN;
630 num_strings++;
631 break;
632 case IPV4_FLOW:
633 sprintf(p, "\tFlow Type: IPv4\n");
634 p += ETH_GSTRING_LEN;
635 num_strings++;
636 break;
637 default:
638 sprintf(p, "\tFlow Type: Unknown\n");
639 p += ETH_GSTRING_LEN;
640 num_strings++;
641 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000642 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800643
644 /* now the rest of the filters */
645 switch (fsc->fs.flow_type) {
646 case TCP_V4_FLOW:
647 case UDP_V4_FLOW:
648 case SCTP_V4_FLOW:
649 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700650 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800651 p += ETH_GSTRING_LEN;
652 num_strings++;
653 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700654 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800655 p += ETH_GSTRING_LEN;
656 num_strings++;
657 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700658 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800659 p += ETH_GSTRING_LEN;
660 num_strings++;
661 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700662 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800663 p += ETH_GSTRING_LEN;
664 num_strings++;
665 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700666 fsc->fs.h_u.tcp_ip4_spec.psrc,
667 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800668 p += ETH_GSTRING_LEN;
669 num_strings++;
670 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700671 fsc->fs.h_u.tcp_ip4_spec.pdst,
672 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800673 p += ETH_GSTRING_LEN;
674 num_strings++;
675 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700676 fsc->fs.h_u.tcp_ip4_spec.tos,
677 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800678 p += ETH_GSTRING_LEN;
679 num_strings++;
680 break;
681 case AH_ESP_V4_FLOW:
682 case ESP_V4_FLOW:
683 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700684 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800685 p += ETH_GSTRING_LEN;
686 num_strings++;
687 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700688 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800689 p += ETH_GSTRING_LEN;
690 num_strings++;
691 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700692 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800693 p += ETH_GSTRING_LEN;
694 num_strings++;
695 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700696 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800697 p += ETH_GSTRING_LEN;
698 num_strings++;
699 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700700 fsc->fs.h_u.ah_ip4_spec.spi,
701 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800702 p += ETH_GSTRING_LEN;
703 num_strings++;
704 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700705 fsc->fs.h_u.ah_ip4_spec.tos,
706 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800707 p += ETH_GSTRING_LEN;
708 num_strings++;
709 break;
710 case IP_USER_FLOW:
711 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000712 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800713 p += ETH_GSTRING_LEN;
714 num_strings++;
715 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000716 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800717 p += ETH_GSTRING_LEN;
718 num_strings++;
719 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000720 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800721 p += ETH_GSTRING_LEN;
722 num_strings++;
723 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000724 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800725 p += ETH_GSTRING_LEN;
726 num_strings++;
727 break;
728 case IPV4_FLOW:
729 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700730 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800731 p += ETH_GSTRING_LEN;
732 num_strings++;
733 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700734 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800735 p += ETH_GSTRING_LEN;
736 num_strings++;
737 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700738 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800739 p += ETH_GSTRING_LEN;
740 num_strings++;
741 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700742 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800743 p += ETH_GSTRING_LEN;
744 num_strings++;
745 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700746 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
747 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800748 p += ETH_GSTRING_LEN;
749 num_strings++;
750 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700751 fsc->fs.h_u.usr_ip4_spec.tos,
752 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800753 p += ETH_GSTRING_LEN;
754 num_strings++;
755 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700756 fsc->fs.h_u.usr_ip4_spec.ip_ver,
757 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800758 p += ETH_GSTRING_LEN;
759 num_strings++;
760 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700761 fsc->fs.h_u.usr_ip4_spec.proto,
762 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800763 p += ETH_GSTRING_LEN;
764 num_strings++;
765 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000766 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800767 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700768 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800769 p += ETH_GSTRING_LEN;
770 num_strings++;
771 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
772 p += ETH_GSTRING_LEN;
773 num_strings++;
774 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
775 p += ETH_GSTRING_LEN;
776 num_strings++;
777 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
778 sprintf(p, "\tAction: Drop\n");
779 else
780 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -0700781 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800782 p += ETH_GSTRING_LEN;
783 num_strings++;
784unknown_filter:
785 i++;
786 }
787copy:
788 /* indicate to userspace how many strings we actually have */
789 gstrings.len = num_strings;
790 ret = -EFAULT;
791 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
792 goto out;
793 useraddr += sizeof(gstrings);
794 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
795 goto out;
796 ret = 0;
797
798out:
799 kfree(data);
800 return ret;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
804{
805 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700806 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 void *regbuf;
808 int reglen, ret;
809
810 if (!ops->get_regs || !ops->get_regs_len)
811 return -EOPNOTSUPP;
812
813 if (copy_from_user(&regs, useraddr, sizeof(regs)))
814 return -EFAULT;
815
816 reglen = ops->get_regs_len(dev);
817 if (regs.len > reglen)
818 regs.len = reglen;
819
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000820 regbuf = vmalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (!regbuf)
822 return -ENOMEM;
823
824 ops->get_regs(dev, &regs, regbuf);
825
826 ret = -EFAULT;
827 if (copy_to_user(useraddr, &regs, sizeof(regs)))
828 goto out;
829 useraddr += offsetof(struct ethtool_regs, data);
830 if (copy_to_user(useraddr, regbuf, regs.len))
831 goto out;
832 ret = 0;
833
834 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000835 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return ret;
837}
838
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000839static int ethtool_reset(struct net_device *dev, char __user *useraddr)
840{
841 struct ethtool_value reset;
842 int ret;
843
844 if (!dev->ethtool_ops->reset)
845 return -EOPNOTSUPP;
846
847 if (copy_from_user(&reset, useraddr, sizeof(reset)))
848 return -EFAULT;
849
850 ret = dev->ethtool_ops->reset(dev, &reset.data);
851 if (ret)
852 return ret;
853
854 if (copy_to_user(useraddr, &reset, sizeof(reset)))
855 return -EFAULT;
856 return 0;
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
860{
Roland Dreier8e557422010-02-11 12:14:23 -0800861 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (!dev->ethtool_ops->get_wol)
864 return -EOPNOTSUPP;
865
866 dev->ethtool_ops->get_wol(dev, &wol);
867
868 if (copy_to_user(useraddr, &wol, sizeof(wol)))
869 return -EFAULT;
870 return 0;
871}
872
873static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
874{
875 struct ethtool_wolinfo wol;
876
877 if (!dev->ethtool_ops->set_wol)
878 return -EOPNOTSUPP;
879
880 if (copy_from_user(&wol, useraddr, sizeof(wol)))
881 return -EFAULT;
882
883 return dev->ethtool_ops->set_wol(dev, &wol);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static int ethtool_nway_reset(struct net_device *dev)
887{
888 if (!dev->ethtool_ops->nway_reset)
889 return -EOPNOTSUPP;
890
891 return dev->ethtool_ops->nway_reset(dev);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
895{
896 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700897 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700898 void __user *userbuf = useraddr + sizeof(eeprom);
899 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700901 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (!ops->get_eeprom || !ops->get_eeprom_len)
904 return -EOPNOTSUPP;
905
906 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
907 return -EFAULT;
908
909 /* Check for wrap and zero */
910 if (eeprom.offset + eeprom.len <= eeprom.offset)
911 return -EINVAL;
912
913 /* Check for exceeding total eeprom len */
914 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
915 return -EINVAL;
916
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700917 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (!data)
919 return -ENOMEM;
920
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700921 bytes_remaining = eeprom.len;
922 while (bytes_remaining > 0) {
923 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700925 ret = ops->get_eeprom(dev, &eeprom, data);
926 if (ret)
927 break;
928 if (copy_to_user(userbuf, data, eeprom.len)) {
929 ret = -EFAULT;
930 break;
931 }
932 userbuf += eeprom.len;
933 eeprom.offset += eeprom.len;
934 bytes_remaining -= eeprom.len;
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700937 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
938 eeprom.offset -= eeprom.len;
939 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
940 ret = -EFAULT;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree(data);
943 return ret;
944}
945
946static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
947{
948 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700949 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700950 void __user *userbuf = useraddr + sizeof(eeprom);
951 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700953 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (!ops->set_eeprom || !ops->get_eeprom_len)
956 return -EOPNOTSUPP;
957
958 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
959 return -EFAULT;
960
961 /* Check for wrap and zero */
962 if (eeprom.offset + eeprom.len <= eeprom.offset)
963 return -EINVAL;
964
965 /* Check for exceeding total eeprom len */
966 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
967 return -EINVAL;
968
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700969 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!data)
971 return -ENOMEM;
972
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700973 bytes_remaining = eeprom.len;
974 while (bytes_remaining > 0) {
975 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700977 if (copy_from_user(data, userbuf, eeprom.len)) {
978 ret = -EFAULT;
979 break;
980 }
981 ret = ops->set_eeprom(dev, &eeprom, data);
982 if (ret)
983 break;
984 userbuf += eeprom.len;
985 eeprom.offset += eeprom.len;
986 bytes_remaining -= eeprom.len;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 kfree(data);
990 return ret;
991}
992
chavey97f8aef2010-04-07 21:54:42 -0700993static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
994 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Roland Dreier8e557422010-02-11 12:14:23 -0800996 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 if (!dev->ethtool_ops->get_coalesce)
999 return -EOPNOTSUPP;
1000
1001 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1002
1003 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1004 return -EFAULT;
1005 return 0;
1006}
1007
chavey97f8aef2010-04-07 21:54:42 -07001008static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1009 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct ethtool_coalesce coalesce;
1012
David S. Millerfa04ae52005-06-06 15:07:19 -07001013 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -EOPNOTSUPP;
1015
1016 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1017 return -EFAULT;
1018
1019 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1020}
1021
1022static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1023{
Roland Dreier8e557422010-02-11 12:14:23 -08001024 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (!dev->ethtool_ops->get_ringparam)
1027 return -EOPNOTSUPP;
1028
1029 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1030
1031 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1032 return -EFAULT;
1033 return 0;
1034}
1035
1036static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1037{
1038 struct ethtool_ringparam ringparam;
1039
1040 if (!dev->ethtool_ops->set_ringparam)
1041 return -EOPNOTSUPP;
1042
1043 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1044 return -EFAULT;
1045
1046 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1047}
1048
1049static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1050{
1051 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1052
1053 if (!dev->ethtool_ops->get_pauseparam)
1054 return -EOPNOTSUPP;
1055
1056 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1057
1058 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1059 return -EFAULT;
1060 return 0;
1061}
1062
1063static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1064{
1065 struct ethtool_pauseparam pauseparam;
1066
Jeff Garzike1b90c42006-07-17 12:54:40 -04001067 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EOPNOTSUPP;
1069
1070 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1071 return -EFAULT;
1072
1073 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076static int __ethtool_set_sg(struct net_device *dev, u32 data)
1077{
1078 int err;
1079
1080 if (!data && dev->ethtool_ops->set_tso) {
1081 err = dev->ethtool_ops->set_tso(dev, 0);
1082 if (err)
1083 return err;
1084 }
1085
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001086 if (!data && dev->ethtool_ops->set_ufo) {
1087 err = dev->ethtool_ops->set_ufo(dev, 0);
1088 if (err)
1089 return err;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return dev->ethtool_ops->set_sg(dev, data);
1092}
1093
1094static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
1095{
1096 struct ethtool_value edata;
1097 int err;
1098
1099 if (!dev->ethtool_ops->set_tx_csum)
1100 return -EOPNOTSUPP;
1101
1102 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1103 return -EFAULT;
1104
1105 if (!edata.data && dev->ethtool_ops->set_sg) {
1106 err = __ethtool_set_sg(dev, 0);
1107 if (err)
1108 return err;
1109 }
1110
1111 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1112}
chavey97f8aef2010-04-07 21:54:42 -07001113EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Herbert Xub240a0e2008-12-15 23:44:31 -08001115static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1116{
1117 struct ethtool_value edata;
1118
1119 if (!dev->ethtool_ops->set_rx_csum)
1120 return -EOPNOTSUPP;
1121
1122 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1123 return -EFAULT;
1124
1125 if (!edata.data && dev->ethtool_ops->set_sg)
1126 dev->features &= ~NETIF_F_GRO;
1127
1128 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1132{
1133 struct ethtool_value edata;
1134
1135 if (!dev->ethtool_ops->set_sg)
1136 return -EOPNOTSUPP;
1137
1138 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1139 return -EFAULT;
1140
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001141 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -07001142 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return -EINVAL;
1144
1145 return __ethtool_set_sg(dev, edata.data);
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1149{
1150 struct ethtool_value edata;
1151
1152 if (!dev->ethtool_ops->set_tso)
1153 return -EOPNOTSUPP;
1154
1155 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1156 return -EFAULT;
1157
1158 if (edata.data && !(dev->features & NETIF_F_SG))
1159 return -EINVAL;
1160
1161 return dev->ethtool_ops->set_tso(dev, edata.data);
1162}
1163
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001164static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1165{
1166 struct ethtool_value edata;
1167
1168 if (!dev->ethtool_ops->set_ufo)
1169 return -EOPNOTSUPP;
1170 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1171 return -EFAULT;
1172 if (edata.data && !(dev->features & NETIF_F_SG))
1173 return -EINVAL;
Michał Mirosław79032642010-11-30 06:38:00 +00001174 if (edata.data && !((dev->features & NETIF_F_GEN_CSUM) ||
1175 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1176 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001177 return -EINVAL;
1178 return dev->ethtool_ops->set_ufo(dev, edata.data);
1179}
1180
Herbert Xu37c31852006-06-22 03:07:29 -07001181static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1182{
1183 struct ethtool_value edata = { ETHTOOL_GGSO };
1184
1185 edata.data = dev->features & NETIF_F_GSO;
1186 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001187 return -EFAULT;
Herbert Xu37c31852006-06-22 03:07:29 -07001188 return 0;
1189}
1190
1191static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1192{
1193 struct ethtool_value edata;
1194
1195 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1196 return -EFAULT;
1197 if (edata.data)
1198 dev->features |= NETIF_F_GSO;
1199 else
1200 dev->features &= ~NETIF_F_GSO;
1201 return 0;
1202}
1203
Herbert Xub240a0e2008-12-15 23:44:31 -08001204static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1205{
1206 struct ethtool_value edata = { ETHTOOL_GGRO };
1207
1208 edata.data = dev->features & NETIF_F_GRO;
1209 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001210 return -EFAULT;
Herbert Xub240a0e2008-12-15 23:44:31 -08001211 return 0;
1212}
1213
1214static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1215{
1216 struct ethtool_value edata;
1217
1218 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1219 return -EFAULT;
1220
1221 if (edata.data) {
Eric Dumazet67c96602010-09-17 11:56:18 -07001222 u32 rxcsum = dev->ethtool_ops->get_rx_csum ?
1223 dev->ethtool_ops->get_rx_csum(dev) :
1224 ethtool_op_get_rx_csum(dev);
1225
1226 if (!rxcsum)
Herbert Xub240a0e2008-12-15 23:44:31 -08001227 return -EINVAL;
1228 dev->features |= NETIF_F_GRO;
1229 } else
1230 dev->features &= ~NETIF_F_GRO;
1231
1232 return 0;
1233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1236{
1237 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001238 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001240 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001242 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001243 return -EOPNOTSUPP;
1244
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001245 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001246 if (test_len < 0)
1247 return test_len;
1248 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (copy_from_user(&test, useraddr, sizeof(test)))
1251 return -EFAULT;
1252
Jeff Garzikff03d492007-08-15 16:01:08 -07001253 test.len = test_len;
1254 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!data)
1256 return -ENOMEM;
1257
1258 ops->self_test(dev, &test, data);
1259
1260 ret = -EFAULT;
1261 if (copy_to_user(useraddr, &test, sizeof(test)))
1262 goto out;
1263 useraddr += sizeof(test);
1264 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1265 goto out;
1266 ret = 0;
1267
1268 out:
1269 kfree(data);
1270 return ret;
1271}
1272
1273static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1274{
1275 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001276 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 u8 *data;
1278 int ret;
1279
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001280 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EOPNOTSUPP;
1282
1283 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1284 return -EFAULT;
1285
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001286 ret = ops->get_sset_count(dev, gstrings.string_set);
1287 if (ret < 0)
1288 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001289
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001290 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1293 if (!data)
1294 return -ENOMEM;
1295
1296 ops->get_strings(dev, gstrings.string_set, data);
1297
1298 ret = -EFAULT;
1299 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1300 goto out;
1301 useraddr += sizeof(gstrings);
1302 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1303 goto out;
1304 ret = 0;
1305
1306 out:
1307 kfree(data);
1308 return ret;
1309}
1310
1311static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1312{
1313 struct ethtool_value id;
1314
1315 if (!dev->ethtool_ops->phys_id)
1316 return -EOPNOTSUPP;
1317
1318 if (copy_from_user(&id, useraddr, sizeof(id)))
1319 return -EFAULT;
1320
1321 return dev->ethtool_ops->phys_id(dev, id.data);
1322}
1323
1324static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1325{
1326 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001327 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001329 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001331 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001332 return -EOPNOTSUPP;
1333
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001334 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001335 if (n_stats < 0)
1336 return n_stats;
1337 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1340 return -EFAULT;
1341
Jeff Garzikff03d492007-08-15 16:01:08 -07001342 stats.n_stats = n_stats;
1343 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!data)
1345 return -ENOMEM;
1346
1347 ops->get_ethtool_stats(dev, &stats, data);
1348
1349 ret = -EFAULT;
1350 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1351 goto out;
1352 useraddr += sizeof(stats);
1353 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1354 goto out;
1355 ret = 0;
1356
1357 out:
1358 kfree(data);
1359 return ret;
1360}
1361
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001362static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001363{
1364 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001365
Matthew Wilcox313674a2007-07-31 14:00:29 -07001366 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001367 return -EFAULT;
1368
Matthew Wilcox313674a2007-07-31 14:00:29 -07001369 if (epaddr.size < dev->addr_len)
1370 return -ETOOSMALL;
1371 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001372
Jon Wetzela6f9a702005-08-20 17:15:54 -07001373 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001374 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001375 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001376 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1377 return -EFAULT;
1378 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001379}
1380
Jeff Garzik13c99b22007-08-15 16:01:56 -07001381static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1382 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001383{
Roland Dreier8e557422010-02-11 12:14:23 -08001384 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001385
Jeff Garzik13c99b22007-08-15 16:01:56 -07001386 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001387 return -EOPNOTSUPP;
1388
Jeff Garzik13c99b22007-08-15 16:01:56 -07001389 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001390
1391 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1392 return -EFAULT;
1393 return 0;
1394}
1395
Jeff Garzik13c99b22007-08-15 16:01:56 -07001396static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1397 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001398{
1399 struct ethtool_value edata;
1400
Jeff Garzik13c99b22007-08-15 16:01:56 -07001401 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001402 return -EOPNOTSUPP;
1403
1404 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1405 return -EFAULT;
1406
Jeff Garzik13c99b22007-08-15 16:01:56 -07001407 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001408 return 0;
1409}
1410
Jeff Garzik13c99b22007-08-15 16:01:56 -07001411static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1412 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001413{
1414 struct ethtool_value edata;
1415
Jeff Garzik13c99b22007-08-15 16:01:56 -07001416 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001417 return -EOPNOTSUPP;
1418
1419 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1420 return -EFAULT;
1421
Jeff Garzik13c99b22007-08-15 16:01:56 -07001422 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001423}
1424
chavey97f8aef2010-04-07 21:54:42 -07001425static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1426 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001427{
1428 struct ethtool_flash efl;
1429
1430 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1431 return -EFAULT;
1432
1433 if (!dev->ethtool_ops->flash_device)
1434 return -EOPNOTSUPP;
1435
1436 return dev->ethtool_ops->flash_device(dev, &efl);
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/* The main entry point in this file. Called from net/core/dev.c */
1440
Eric W. Biederman881d9662007-09-17 11:56:21 -07001441int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001443 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 void __user *useraddr = ifr->ifr_data;
1445 u32 ethcmd;
1446 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -07001447 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!dev || !netif_device_present(dev))
1450 return -ENODEV;
1451
chavey97f8aef2010-04-07 21:54:42 -07001452 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EFAULT;
1454
Ben Hutchings01414802010-08-17 02:31:15 -07001455 if (!dev->ethtool_ops) {
1456 /* ETHTOOL_GDRVINFO does not require any driver support.
1457 * It is also unprivileged and does not change anything,
1458 * so we can take a shortcut to it. */
1459 if (ethcmd == ETHTOOL_GDRVINFO)
1460 return ethtool_get_drvinfo(dev, useraddr);
1461 else
1462 return -EOPNOTSUPP;
1463 }
1464
Stephen Hemminger75f31232006-09-28 15:13:37 -07001465 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001466 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001467 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001468 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001469 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001470 case ETHTOOL_GCOALESCE:
1471 case ETHTOOL_GRINGPARAM:
1472 case ETHTOOL_GPAUSEPARAM:
1473 case ETHTOOL_GRXCSUM:
1474 case ETHTOOL_GTXCSUM:
1475 case ETHTOOL_GSG:
1476 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001477 case ETHTOOL_GTSO:
1478 case ETHTOOL_GPERMADDR:
1479 case ETHTOOL_GUFO:
1480 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001481 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001482 case ETHTOOL_GFLAGS:
1483 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001484 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001485 case ETHTOOL_GRXRINGS:
1486 case ETHTOOL_GRXCLSRLCNT:
1487 case ETHTOOL_GRXCLSRULE:
1488 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001489 break;
1490 default:
1491 if (!capable(CAP_NET_ADMIN))
1492 return -EPERM;
1493 }
1494
chavey97f8aef2010-04-07 21:54:42 -07001495 if (dev->ethtool_ops->begin) {
1496 rc = dev->ethtool_ops->begin(dev);
1497 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001499 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001500 old_features = dev->features;
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 switch (ethcmd) {
1503 case ETHTOOL_GSET:
1504 rc = ethtool_get_settings(dev, useraddr);
1505 break;
1506 case ETHTOOL_SSET:
1507 rc = ethtool_set_settings(dev, useraddr);
1508 break;
1509 case ETHTOOL_GDRVINFO:
1510 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512 case ETHTOOL_GREGS:
1513 rc = ethtool_get_regs(dev, useraddr);
1514 break;
1515 case ETHTOOL_GWOL:
1516 rc = ethtool_get_wol(dev, useraddr);
1517 break;
1518 case ETHTOOL_SWOL:
1519 rc = ethtool_set_wol(dev, useraddr);
1520 break;
1521 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001522 rc = ethtool_get_value(dev, useraddr, ethcmd,
1523 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001526 rc = ethtool_set_value_void(dev, useraddr,
1527 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529 case ETHTOOL_NWAY_RST:
1530 rc = ethtool_nway_reset(dev);
1531 break;
1532 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001533 rc = ethtool_get_value(dev, useraddr, ethcmd,
1534 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536 case ETHTOOL_GEEPROM:
1537 rc = ethtool_get_eeprom(dev, useraddr);
1538 break;
1539 case ETHTOOL_SEEPROM:
1540 rc = ethtool_set_eeprom(dev, useraddr);
1541 break;
1542 case ETHTOOL_GCOALESCE:
1543 rc = ethtool_get_coalesce(dev, useraddr);
1544 break;
1545 case ETHTOOL_SCOALESCE:
1546 rc = ethtool_set_coalesce(dev, useraddr);
1547 break;
1548 case ETHTOOL_GRINGPARAM:
1549 rc = ethtool_get_ringparam(dev, useraddr);
1550 break;
1551 case ETHTOOL_SRINGPARAM:
1552 rc = ethtool_set_ringparam(dev, useraddr);
1553 break;
1554 case ETHTOOL_GPAUSEPARAM:
1555 rc = ethtool_get_pauseparam(dev, useraddr);
1556 break;
1557 case ETHTOOL_SPAUSEPARAM:
1558 rc = ethtool_set_pauseparam(dev, useraddr);
1559 break;
1560 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001561 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001562 (dev->ethtool_ops->get_rx_csum ?
1563 dev->ethtool_ops->get_rx_csum :
1564 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001567 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 break;
1569 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001570 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001571 (dev->ethtool_ops->get_tx_csum ?
1572 dev->ethtool_ops->get_tx_csum :
1573 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 case ETHTOOL_STXCSUM:
1576 rc = ethtool_set_tx_csum(dev, useraddr);
1577 break;
1578 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001579 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001580 (dev->ethtool_ops->get_sg ?
1581 dev->ethtool_ops->get_sg :
1582 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
1584 case ETHTOOL_SSG:
1585 rc = ethtool_set_sg(dev, useraddr);
1586 break;
1587 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001588 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001589 (dev->ethtool_ops->get_tso ?
1590 dev->ethtool_ops->get_tso :
1591 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 break;
1593 case ETHTOOL_STSO:
1594 rc = ethtool_set_tso(dev, useraddr);
1595 break;
1596 case ETHTOOL_TEST:
1597 rc = ethtool_self_test(dev, useraddr);
1598 break;
1599 case ETHTOOL_GSTRINGS:
1600 rc = ethtool_get_strings(dev, useraddr);
1601 break;
1602 case ETHTOOL_PHYS_ID:
1603 rc = ethtool_phys_id(dev, useraddr);
1604 break;
1605 case ETHTOOL_GSTATS:
1606 rc = ethtool_get_stats(dev, useraddr);
1607 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001608 case ETHTOOL_GPERMADDR:
1609 rc = ethtool_get_perm_addr(dev, useraddr);
1610 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001611 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001612 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001613 (dev->ethtool_ops->get_ufo ?
1614 dev->ethtool_ops->get_ufo :
1615 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001616 break;
1617 case ETHTOOL_SUFO:
1618 rc = ethtool_set_ufo(dev, useraddr);
1619 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001620 case ETHTOOL_GGSO:
1621 rc = ethtool_get_gso(dev, useraddr);
1622 break;
1623 case ETHTOOL_SGSO:
1624 rc = ethtool_set_gso(dev, useraddr);
1625 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001626 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001627 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001628 (dev->ethtool_ops->get_flags ?
1629 dev->ethtool_ops->get_flags :
1630 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001631 break;
1632 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001633 rc = ethtool_set_value(dev, useraddr,
1634 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001635 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001636 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001637 rc = ethtool_get_value(dev, useraddr, ethcmd,
1638 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001639 break;
1640 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001641 rc = ethtool_set_value(dev, useraddr,
1642 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001643 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001644 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001645 case ETHTOOL_GRXRINGS:
1646 case ETHTOOL_GRXCLSRLCNT:
1647 case ETHTOOL_GRXCLSRULE:
1648 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001649 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001650 break;
1651 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001652 case ETHTOOL_SRXCLSRLDEL:
1653 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001654 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001655 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001656 case ETHTOOL_GGRO:
1657 rc = ethtool_get_gro(dev, useraddr);
1658 break;
1659 case ETHTOOL_SGRO:
1660 rc = ethtool_set_gro(dev, useraddr);
1661 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001662 case ETHTOOL_FLASHDEV:
1663 rc = ethtool_flash_device(dev, useraddr);
1664 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001665 case ETHTOOL_RESET:
1666 rc = ethtool_reset(dev, useraddr);
1667 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001668 case ETHTOOL_SRXNTUPLE:
1669 rc = ethtool_set_rx_ntuple(dev, useraddr);
1670 break;
1671 case ETHTOOL_GRXNTUPLE:
1672 rc = ethtool_get_rx_ntuple(dev, useraddr);
1673 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001674 case ETHTOOL_GSSET_INFO:
1675 rc = ethtool_get_sset_info(dev, useraddr);
1676 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001677 case ETHTOOL_GRXFHINDIR:
1678 rc = ethtool_get_rxfh_indir(dev, useraddr);
1679 break;
1680 case ETHTOOL_SRXFHINDIR:
1681 rc = ethtool_set_rxfh_indir(dev, useraddr);
1682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001684 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001686
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001687 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001689
1690 if (old_features != dev->features)
1691 netdev_features_change(dev);
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}