blob: 685c7005e87fe928df83c780f1c7aa8552f5117e [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 =
stephen hemmingerb00fabb2010-03-29 14:47:27 +0000135 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700136
137u32 ethtool_op_get_flags(struct net_device *dev)
138{
139 /* in the future, this function will probably contain additional
140 * handling for flags which are not so easily handled
141 * by a simple masking operation
142 */
143
144 return dev->features & flags_dup_features;
145}
chavey97f8aef2010-04-07 21:54:42 -0700146EXPORT_SYMBOL(ethtool_op_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700147
Ben Hutchings1437ce32010-06-30 02:44:32 +0000148int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700149{
Ben Hutchings1437ce32010-06-30 02:44:32 +0000150 if (data & ~supported)
151 return -EINVAL;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000152
Ben Hutchings1437ce32010-06-30 02:44:32 +0000153 dev->features = ((dev->features & ~flags_dup_features) |
154 (data & flags_dup_features));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700155 return 0;
156}
chavey97f8aef2010-04-07 21:54:42 -0700157EXPORT_SYMBOL(ethtool_op_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700158
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800159void ethtool_ntuple_flush(struct net_device *dev)
160{
161 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
162
163 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
164 list_del(&fsc->list);
165 kfree(fsc);
166 }
167 dev->ethtool_ntuple_list.count = 0;
168}
169EXPORT_SYMBOL(ethtool_ntuple_flush);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/* Handlers for each ethtool command */
172
173static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
174{
Roland Dreier8e557422010-02-11 12:14:23 -0800175 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int err;
177
178 if (!dev->ethtool_ops->get_settings)
179 return -EOPNOTSUPP;
180
181 err = dev->ethtool_ops->get_settings(dev, &cmd);
182 if (err < 0)
183 return err;
184
185 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
186 return -EFAULT;
187 return 0;
188}
189
190static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
191{
192 struct ethtool_cmd cmd;
193
194 if (!dev->ethtool_ops->set_settings)
195 return -EOPNOTSUPP;
196
197 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
198 return -EFAULT;
199
200 return dev->ethtool_ops->set_settings(dev, &cmd);
201}
202
chavey97f8aef2010-04-07 21:54:42 -0700203static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
204 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700207 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 memset(&info, 0, sizeof(info));
210 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700211 if (ops && ops->get_drvinfo) {
212 ops->get_drvinfo(dev, &info);
213 } else if (dev->dev.parent && dev->dev.parent->driver) {
214 strlcpy(info.bus_info, dev_name(dev->dev.parent),
215 sizeof(info.bus_info));
216 strlcpy(info.driver, dev->dev.parent->driver->name,
217 sizeof(info.driver));
218 } else {
219 return -EOPNOTSUPP;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jeff Garzik723b2f52010-03-03 22:51:50 +0000222 /*
223 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000224 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000225 */
Ben Hutchings01414802010-08-17 02:31:15 -0700226 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700227 int rc;
228
229 rc = ops->get_sset_count(dev, ETH_SS_TEST);
230 if (rc >= 0)
231 info.testinfo_len = rc;
232 rc = ops->get_sset_count(dev, ETH_SS_STATS);
233 if (rc >= 0)
234 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700235 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
236 if (rc >= 0)
237 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700238 }
Ben Hutchings01414802010-08-17 02:31:15 -0700239 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700241 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 info.eedump_len = ops->get_eeprom_len(dev);
243
244 if (copy_to_user(useraddr, &info, sizeof(info)))
245 return -EFAULT;
246 return 0;
247}
248
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800249static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700250 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000251{
252 struct ethtool_sset_info info;
253 const struct ethtool_ops *ops = dev->ethtool_ops;
254 u64 sset_mask;
255 int i, idx = 0, n_bits = 0, ret, rc;
256 u32 *info_buf = NULL;
257
258 if (!ops->get_sset_count)
259 return -EOPNOTSUPP;
260
261 if (copy_from_user(&info, useraddr, sizeof(info)))
262 return -EFAULT;
263
264 /* store copy of mask, because we zero struct later on */
265 sset_mask = info.sset_mask;
266 if (!sset_mask)
267 return 0;
268
269 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000270 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000271
272 memset(&info, 0, sizeof(info));
273 info.cmd = ETHTOOL_GSSET_INFO;
274
275 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
276 if (!info_buf)
277 return -ENOMEM;
278
279 /*
280 * fill return buffer based on input bitmask and successful
281 * get_sset_count return
282 */
283 for (i = 0; i < 64; i++) {
284 if (!(sset_mask & (1ULL << i)))
285 continue;
286
287 rc = ops->get_sset_count(dev, i);
288 if (rc >= 0) {
289 info.sset_mask |= (1ULL << i);
290 info_buf[idx++] = rc;
291 }
292 }
293
294 ret = -EFAULT;
295 if (copy_to_user(useraddr, &info, sizeof(info)))
296 goto out;
297
298 useraddr += offsetof(struct ethtool_sset_info, data);
299 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
300 goto out;
301
302 ret = 0;
303
304out:
305 kfree(info_buf);
306 return ret;
307}
308
chavey97f8aef2010-04-07 21:54:42 -0700309static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000310 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700311{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000312 struct ethtool_rxnfc info;
313 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700314
Santwona Behera59089d82009-02-20 00:58:13 -0800315 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700316 return -EOPNOTSUPP;
317
Ben Hutchingsbf988432010-06-28 08:45:58 +0000318 /* struct ethtool_rxnfc was originally defined for
319 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
320 * members. User-space might still be using that
321 * definition. */
322 if (cmd == ETHTOOL_SRXFH)
323 info_size = (offsetof(struct ethtool_rxnfc, data) +
324 sizeof(info.data));
325
326 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700327 return -EFAULT;
328
Ben Hutchingsbf988432010-06-28 08:45:58 +0000329 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700330}
331
chavey97f8aef2010-04-07 21:54:42 -0700332static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000333 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700334{
335 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000336 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800337 const struct ethtool_ops *ops = dev->ethtool_ops;
338 int ret;
339 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700340
Santwona Behera59089d82009-02-20 00:58:13 -0800341 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700342 return -EOPNOTSUPP;
343
Ben Hutchingsbf988432010-06-28 08:45:58 +0000344 /* struct ethtool_rxnfc was originally defined for
345 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
346 * members. User-space might still be using that
347 * definition. */
348 if (cmd == ETHTOOL_GRXFH)
349 info_size = (offsetof(struct ethtool_rxnfc, data) +
350 sizeof(info.data));
351
352 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700353 return -EFAULT;
354
Santwona Behera59089d82009-02-20 00:58:13 -0800355 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
356 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000357 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000358 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000359 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800360 if (!rule_buf)
361 return -ENOMEM;
362 }
363 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700364
Santwona Behera59089d82009-02-20 00:58:13 -0800365 ret = ops->get_rxnfc(dev, &info, rule_buf);
366 if (ret < 0)
367 goto err_out;
368
369 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000370 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800371 goto err_out;
372
373 if (rule_buf) {
374 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
375 if (copy_to_user(useraddr, rule_buf,
376 info.rule_cnt * sizeof(u32)))
377 goto err_out;
378 }
379 ret = 0;
380
381err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700382 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800383
384 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700385}
386
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000387static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
388 void __user *useraddr)
389{
390 struct ethtool_rxfh_indir *indir;
391 u32 table_size;
392 size_t full_size;
393 int ret;
394
395 if (!dev->ethtool_ops->get_rxfh_indir)
396 return -EOPNOTSUPP;
397
398 if (copy_from_user(&table_size,
399 useraddr + offsetof(struct ethtool_rxfh_indir, size),
400 sizeof(table_size)))
401 return -EFAULT;
402
403 if (table_size >
404 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
405 return -ENOMEM;
406 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
Kees Cookb00916b2010-10-11 12:23:25 -0700407 indir = kzalloc(full_size, GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000408 if (!indir)
409 return -ENOMEM;
410
411 indir->cmd = ETHTOOL_GRXFHINDIR;
412 indir->size = table_size;
413 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
414 if (ret)
415 goto out;
416
417 if (copy_to_user(useraddr, indir, full_size))
418 ret = -EFAULT;
419
420out:
421 kfree(indir);
422 return ret;
423}
424
425static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
426 void __user *useraddr)
427{
428 struct ethtool_rxfh_indir *indir;
429 u32 table_size;
430 size_t full_size;
431 int ret;
432
433 if (!dev->ethtool_ops->set_rxfh_indir)
434 return -EOPNOTSUPP;
435
436 if (copy_from_user(&table_size,
437 useraddr + offsetof(struct ethtool_rxfh_indir, size),
438 sizeof(table_size)))
439 return -EFAULT;
440
441 if (table_size >
442 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
443 return -ENOMEM;
444 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
445 indir = kmalloc(full_size, GFP_USER);
446 if (!indir)
447 return -ENOMEM;
448
449 if (copy_from_user(indir, useraddr, full_size)) {
450 ret = -EFAULT;
451 goto out;
452 }
453
454 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
455
456out:
457 kfree(indir);
458 return ret;
459}
460
Peter Waskiewicze8589112010-02-12 13:48:05 +0000461static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700462 struct ethtool_rx_ntuple_flow_spec *spec,
463 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800464{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800465
466 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000467 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
468 /* free the container */
469 kfree(fsc);
470 return;
471 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800472
473 /* Copy the whole filter over */
474 fsc->fs.flow_type = spec->flow_type;
475 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
476 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
477
478 fsc->fs.vlan_tag = spec->vlan_tag;
479 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
480 fsc->fs.data = spec->data;
481 fsc->fs.data_mask = spec->data_mask;
482 fsc->fs.action = spec->action;
483
484 /* add to the list */
485 list_add_tail_rcu(&fsc->list, &list->list);
486 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800487}
488
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000489/*
490 * ethtool does not (or did not) set masks for flow parameters that are
491 * not specified, so if both value and mask are 0 then this must be
492 * treated as equivalent to a mask with all bits set. Implement that
493 * here rather than in drivers.
494 */
495static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
496{
497 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
498 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
499
500 if (fs->flow_type != TCP_V4_FLOW &&
501 fs->flow_type != UDP_V4_FLOW &&
502 fs->flow_type != SCTP_V4_FLOW)
503 return;
504
505 if (!(entry->ip4src | mask->ip4src))
506 mask->ip4src = htonl(0xffffffff);
507 if (!(entry->ip4dst | mask->ip4dst))
508 mask->ip4dst = htonl(0xffffffff);
509 if (!(entry->psrc | mask->psrc))
510 mask->psrc = htons(0xffff);
511 if (!(entry->pdst | mask->pdst))
512 mask->pdst = htons(0xffff);
513 if (!(entry->tos | mask->tos))
514 mask->tos = 0xff;
515 if (!(fs->vlan_tag | fs->vlan_tag_mask))
516 fs->vlan_tag_mask = 0xffff;
517 if (!(fs->data | fs->data_mask))
518 fs->data_mask = 0xffffffffffffffffULL;
519}
520
chavey97f8aef2010-04-07 21:54:42 -0700521static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
522 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800523{
524 struct ethtool_rx_ntuple cmd;
525 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000526 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800527 int ret;
528
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800529 if (!(dev->features & NETIF_F_NTUPLE))
530 return -EINVAL;
531
532 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
533 return -EFAULT;
534
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000535 rx_ntuple_fix_masks(&cmd.fs);
536
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800537 /*
538 * Cache filter in dev struct for GET operation only if
539 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000540 * only if the filter was added successfully. First make sure we
541 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800542 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000543 if (!ops->get_rx_ntuple) {
544 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
545 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800546 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000547 }
548
549 ret = ops->set_rx_ntuple(dev, &cmd);
550 if (ret) {
551 kfree(fsc);
552 return ret;
553 }
554
555 if (!ops->get_rx_ntuple)
556 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800557
558 return ret;
559}
560
561static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
562{
563 struct ethtool_gstrings gstrings;
564 const struct ethtool_ops *ops = dev->ethtool_ops;
565 struct ethtool_rx_ntuple_flow_spec_container *fsc;
566 u8 *data;
567 char *p;
568 int ret, i, num_strings = 0;
569
570 if (!ops->get_sset_count)
571 return -EOPNOTSUPP;
572
573 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
574 return -EFAULT;
575
576 ret = ops->get_sset_count(dev, gstrings.string_set);
577 if (ret < 0)
578 return ret;
579
580 gstrings.len = ret;
581
Kees Cookb00916b2010-10-11 12:23:25 -0700582 data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800583 if (!data)
584 return -ENOMEM;
585
586 if (ops->get_rx_ntuple) {
587 /* driver-specific filter grab */
588 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
589 goto copy;
590 }
591
592 /* default ethtool filter grab */
593 i = 0;
594 p = (char *)data;
595 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
596 sprintf(p, "Filter %d:\n", i);
597 p += ETH_GSTRING_LEN;
598 num_strings++;
599
600 switch (fsc->fs.flow_type) {
601 case TCP_V4_FLOW:
602 sprintf(p, "\tFlow Type: TCP\n");
603 p += ETH_GSTRING_LEN;
604 num_strings++;
605 break;
606 case UDP_V4_FLOW:
607 sprintf(p, "\tFlow Type: UDP\n");
608 p += ETH_GSTRING_LEN;
609 num_strings++;
610 break;
611 case SCTP_V4_FLOW:
612 sprintf(p, "\tFlow Type: SCTP\n");
613 p += ETH_GSTRING_LEN;
614 num_strings++;
615 break;
616 case AH_ESP_V4_FLOW:
617 sprintf(p, "\tFlow Type: AH ESP\n");
618 p += ETH_GSTRING_LEN;
619 num_strings++;
620 break;
621 case ESP_V4_FLOW:
622 sprintf(p, "\tFlow Type: ESP\n");
623 p += ETH_GSTRING_LEN;
624 num_strings++;
625 break;
626 case IP_USER_FLOW:
627 sprintf(p, "\tFlow Type: Raw IP\n");
628 p += ETH_GSTRING_LEN;
629 num_strings++;
630 break;
631 case IPV4_FLOW:
632 sprintf(p, "\tFlow Type: IPv4\n");
633 p += ETH_GSTRING_LEN;
634 num_strings++;
635 break;
636 default:
637 sprintf(p, "\tFlow Type: Unknown\n");
638 p += ETH_GSTRING_LEN;
639 num_strings++;
640 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000641 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800642
643 /* now the rest of the filters */
644 switch (fsc->fs.flow_type) {
645 case TCP_V4_FLOW:
646 case UDP_V4_FLOW:
647 case SCTP_V4_FLOW:
648 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700649 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800650 p += ETH_GSTRING_LEN;
651 num_strings++;
652 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700653 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800654 p += ETH_GSTRING_LEN;
655 num_strings++;
656 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700657 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800658 p += ETH_GSTRING_LEN;
659 num_strings++;
660 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700661 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800662 p += ETH_GSTRING_LEN;
663 num_strings++;
664 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700665 fsc->fs.h_u.tcp_ip4_spec.psrc,
666 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800667 p += ETH_GSTRING_LEN;
668 num_strings++;
669 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700670 fsc->fs.h_u.tcp_ip4_spec.pdst,
671 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800672 p += ETH_GSTRING_LEN;
673 num_strings++;
674 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700675 fsc->fs.h_u.tcp_ip4_spec.tos,
676 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800677 p += ETH_GSTRING_LEN;
678 num_strings++;
679 break;
680 case AH_ESP_V4_FLOW:
681 case ESP_V4_FLOW:
682 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700683 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800684 p += ETH_GSTRING_LEN;
685 num_strings++;
686 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700687 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800688 p += ETH_GSTRING_LEN;
689 num_strings++;
690 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700691 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800692 p += ETH_GSTRING_LEN;
693 num_strings++;
694 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700695 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800696 p += ETH_GSTRING_LEN;
697 num_strings++;
698 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700699 fsc->fs.h_u.ah_ip4_spec.spi,
700 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800701 p += ETH_GSTRING_LEN;
702 num_strings++;
703 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700704 fsc->fs.h_u.ah_ip4_spec.tos,
705 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800706 p += ETH_GSTRING_LEN;
707 num_strings++;
708 break;
709 case IP_USER_FLOW:
710 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000711 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800712 p += ETH_GSTRING_LEN;
713 num_strings++;
714 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000715 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800716 p += ETH_GSTRING_LEN;
717 num_strings++;
718 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000719 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800720 p += ETH_GSTRING_LEN;
721 num_strings++;
722 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000723 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800724 p += ETH_GSTRING_LEN;
725 num_strings++;
726 break;
727 case IPV4_FLOW:
728 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700729 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800730 p += ETH_GSTRING_LEN;
731 num_strings++;
732 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700733 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800734 p += ETH_GSTRING_LEN;
735 num_strings++;
736 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700737 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800738 p += ETH_GSTRING_LEN;
739 num_strings++;
740 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700741 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800742 p += ETH_GSTRING_LEN;
743 num_strings++;
744 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700745 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
746 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800747 p += ETH_GSTRING_LEN;
748 num_strings++;
749 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700750 fsc->fs.h_u.usr_ip4_spec.tos,
751 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800752 p += ETH_GSTRING_LEN;
753 num_strings++;
754 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700755 fsc->fs.h_u.usr_ip4_spec.ip_ver,
756 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800757 p += ETH_GSTRING_LEN;
758 num_strings++;
759 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700760 fsc->fs.h_u.usr_ip4_spec.proto,
761 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800762 p += ETH_GSTRING_LEN;
763 num_strings++;
764 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000765 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800766 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700767 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800768 p += ETH_GSTRING_LEN;
769 num_strings++;
770 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
771 p += ETH_GSTRING_LEN;
772 num_strings++;
773 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
774 p += ETH_GSTRING_LEN;
775 num_strings++;
776 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
777 sprintf(p, "\tAction: Drop\n");
778 else
779 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -0700780 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800781 p += ETH_GSTRING_LEN;
782 num_strings++;
783unknown_filter:
784 i++;
785 }
786copy:
787 /* indicate to userspace how many strings we actually have */
788 gstrings.len = num_strings;
789 ret = -EFAULT;
790 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
791 goto out;
792 useraddr += sizeof(gstrings);
793 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
794 goto out;
795 ret = 0;
796
797out:
798 kfree(data);
799 return ret;
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
803{
804 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700805 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 void *regbuf;
807 int reglen, ret;
808
809 if (!ops->get_regs || !ops->get_regs_len)
810 return -EOPNOTSUPP;
811
812 if (copy_from_user(&regs, useraddr, sizeof(regs)))
813 return -EFAULT;
814
815 reglen = ops->get_regs_len(dev);
816 if (regs.len > reglen)
817 regs.len = reglen;
818
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000819 regbuf = vmalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!regbuf)
821 return -ENOMEM;
822
823 ops->get_regs(dev, &regs, regbuf);
824
825 ret = -EFAULT;
826 if (copy_to_user(useraddr, &regs, sizeof(regs)))
827 goto out;
828 useraddr += offsetof(struct ethtool_regs, data);
829 if (copy_to_user(useraddr, regbuf, regs.len))
830 goto out;
831 ret = 0;
832
833 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000834 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
836}
837
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000838static int ethtool_reset(struct net_device *dev, char __user *useraddr)
839{
840 struct ethtool_value reset;
841 int ret;
842
843 if (!dev->ethtool_ops->reset)
844 return -EOPNOTSUPP;
845
846 if (copy_from_user(&reset, useraddr, sizeof(reset)))
847 return -EFAULT;
848
849 ret = dev->ethtool_ops->reset(dev, &reset.data);
850 if (ret)
851 return ret;
852
853 if (copy_to_user(useraddr, &reset, sizeof(reset)))
854 return -EFAULT;
855 return 0;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
859{
Roland Dreier8e557422010-02-11 12:14:23 -0800860 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (!dev->ethtool_ops->get_wol)
863 return -EOPNOTSUPP;
864
865 dev->ethtool_ops->get_wol(dev, &wol);
866
867 if (copy_to_user(useraddr, &wol, sizeof(wol)))
868 return -EFAULT;
869 return 0;
870}
871
872static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
873{
874 struct ethtool_wolinfo wol;
875
876 if (!dev->ethtool_ops->set_wol)
877 return -EOPNOTSUPP;
878
879 if (copy_from_user(&wol, useraddr, sizeof(wol)))
880 return -EFAULT;
881
882 return dev->ethtool_ops->set_wol(dev, &wol);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static int ethtool_nway_reset(struct net_device *dev)
886{
887 if (!dev->ethtool_ops->nway_reset)
888 return -EOPNOTSUPP;
889
890 return dev->ethtool_ops->nway_reset(dev);
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
894{
895 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700896 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700897 void __user *userbuf = useraddr + sizeof(eeprom);
898 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700900 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (!ops->get_eeprom || !ops->get_eeprom_len)
903 return -EOPNOTSUPP;
904
905 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
906 return -EFAULT;
907
908 /* Check for wrap and zero */
909 if (eeprom.offset + eeprom.len <= eeprom.offset)
910 return -EINVAL;
911
912 /* Check for exceeding total eeprom len */
913 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
914 return -EINVAL;
915
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700916 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!data)
918 return -ENOMEM;
919
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700920 bytes_remaining = eeprom.len;
921 while (bytes_remaining > 0) {
922 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700924 ret = ops->get_eeprom(dev, &eeprom, data);
925 if (ret)
926 break;
927 if (copy_to_user(userbuf, data, eeprom.len)) {
928 ret = -EFAULT;
929 break;
930 }
931 userbuf += eeprom.len;
932 eeprom.offset += eeprom.len;
933 bytes_remaining -= eeprom.len;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700936 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
937 eeprom.offset -= eeprom.len;
938 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
939 ret = -EFAULT;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 kfree(data);
942 return ret;
943}
944
945static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
946{
947 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700948 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700949 void __user *userbuf = useraddr + sizeof(eeprom);
950 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700952 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 if (!ops->set_eeprom || !ops->get_eeprom_len)
955 return -EOPNOTSUPP;
956
957 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
958 return -EFAULT;
959
960 /* Check for wrap and zero */
961 if (eeprom.offset + eeprom.len <= eeprom.offset)
962 return -EINVAL;
963
964 /* Check for exceeding total eeprom len */
965 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
966 return -EINVAL;
967
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700968 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (!data)
970 return -ENOMEM;
971
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700972 bytes_remaining = eeprom.len;
973 while (bytes_remaining > 0) {
974 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700976 if (copy_from_user(data, userbuf, eeprom.len)) {
977 ret = -EFAULT;
978 break;
979 }
980 ret = ops->set_eeprom(dev, &eeprom, data);
981 if (ret)
982 break;
983 userbuf += eeprom.len;
984 eeprom.offset += eeprom.len;
985 bytes_remaining -= eeprom.len;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 kfree(data);
989 return ret;
990}
991
chavey97f8aef2010-04-07 21:54:42 -0700992static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
993 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Roland Dreier8e557422010-02-11 12:14:23 -0800995 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (!dev->ethtool_ops->get_coalesce)
998 return -EOPNOTSUPP;
999
1000 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1001
1002 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1003 return -EFAULT;
1004 return 0;
1005}
1006
chavey97f8aef2010-04-07 21:54:42 -07001007static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1008 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct ethtool_coalesce coalesce;
1011
David S. Millerfa04ae52005-06-06 15:07:19 -07001012 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -EOPNOTSUPP;
1014
1015 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1016 return -EFAULT;
1017
1018 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1019}
1020
1021static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1022{
Roland Dreier8e557422010-02-11 12:14:23 -08001023 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (!dev->ethtool_ops->get_ringparam)
1026 return -EOPNOTSUPP;
1027
1028 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1029
1030 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1031 return -EFAULT;
1032 return 0;
1033}
1034
1035static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1036{
1037 struct ethtool_ringparam ringparam;
1038
1039 if (!dev->ethtool_ops->set_ringparam)
1040 return -EOPNOTSUPP;
1041
1042 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1043 return -EFAULT;
1044
1045 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1046}
1047
1048static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1049{
1050 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1051
1052 if (!dev->ethtool_ops->get_pauseparam)
1053 return -EOPNOTSUPP;
1054
1055 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1056
1057 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1058 return -EFAULT;
1059 return 0;
1060}
1061
1062static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1063{
1064 struct ethtool_pauseparam pauseparam;
1065
Jeff Garzike1b90c42006-07-17 12:54:40 -04001066 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -EOPNOTSUPP;
1068
1069 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1070 return -EFAULT;
1071
1072 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075static int __ethtool_set_sg(struct net_device *dev, u32 data)
1076{
1077 int err;
1078
1079 if (!data && dev->ethtool_ops->set_tso) {
1080 err = dev->ethtool_ops->set_tso(dev, 0);
1081 if (err)
1082 return err;
1083 }
1084
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001085 if (!data && dev->ethtool_ops->set_ufo) {
1086 err = dev->ethtool_ops->set_ufo(dev, 0);
1087 if (err)
1088 return err;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return dev->ethtool_ops->set_sg(dev, data);
1091}
1092
1093static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
1094{
1095 struct ethtool_value edata;
1096 int err;
1097
1098 if (!dev->ethtool_ops->set_tx_csum)
1099 return -EOPNOTSUPP;
1100
1101 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1102 return -EFAULT;
1103
1104 if (!edata.data && dev->ethtool_ops->set_sg) {
1105 err = __ethtool_set_sg(dev, 0);
1106 if (err)
1107 return err;
1108 }
1109
1110 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1111}
chavey97f8aef2010-04-07 21:54:42 -07001112EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Herbert Xub240a0e2008-12-15 23:44:31 -08001114static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1115{
1116 struct ethtool_value edata;
1117
1118 if (!dev->ethtool_ops->set_rx_csum)
1119 return -EOPNOTSUPP;
1120
1121 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1122 return -EFAULT;
1123
1124 if (!edata.data && dev->ethtool_ops->set_sg)
1125 dev->features &= ~NETIF_F_GRO;
1126
1127 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1131{
1132 struct ethtool_value edata;
1133
1134 if (!dev->ethtool_ops->set_sg)
1135 return -EOPNOTSUPP;
1136
1137 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1138 return -EFAULT;
1139
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001140 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -07001141 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -EINVAL;
1143
1144 return __ethtool_set_sg(dev, edata.data);
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1148{
1149 struct ethtool_value edata;
1150
1151 if (!dev->ethtool_ops->set_tso)
1152 return -EOPNOTSUPP;
1153
1154 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1155 return -EFAULT;
1156
1157 if (edata.data && !(dev->features & NETIF_F_SG))
1158 return -EINVAL;
1159
1160 return dev->ethtool_ops->set_tso(dev, edata.data);
1161}
1162
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001163static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1164{
1165 struct ethtool_value edata;
1166
1167 if (!dev->ethtool_ops->set_ufo)
1168 return -EOPNOTSUPP;
1169 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1170 return -EFAULT;
1171 if (edata.data && !(dev->features & NETIF_F_SG))
1172 return -EINVAL;
1173 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1174 return -EINVAL;
1175 return dev->ethtool_ops->set_ufo(dev, edata.data);
1176}
1177
Herbert Xu37c31852006-06-22 03:07:29 -07001178static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1179{
1180 struct ethtool_value edata = { ETHTOOL_GGSO };
1181
1182 edata.data = dev->features & NETIF_F_GSO;
1183 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001184 return -EFAULT;
Herbert Xu37c31852006-06-22 03:07:29 -07001185 return 0;
1186}
1187
1188static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1189{
1190 struct ethtool_value edata;
1191
1192 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1193 return -EFAULT;
1194 if (edata.data)
1195 dev->features |= NETIF_F_GSO;
1196 else
1197 dev->features &= ~NETIF_F_GSO;
1198 return 0;
1199}
1200
Herbert Xub240a0e2008-12-15 23:44:31 -08001201static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1202{
1203 struct ethtool_value edata = { ETHTOOL_GGRO };
1204
1205 edata.data = dev->features & NETIF_F_GRO;
1206 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001207 return -EFAULT;
Herbert Xub240a0e2008-12-15 23:44:31 -08001208 return 0;
1209}
1210
1211static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1212{
1213 struct ethtool_value edata;
1214
1215 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1216 return -EFAULT;
1217
1218 if (edata.data) {
Eric Dumazet67c96602010-09-17 11:56:18 -07001219 u32 rxcsum = dev->ethtool_ops->get_rx_csum ?
1220 dev->ethtool_ops->get_rx_csum(dev) :
1221 ethtool_op_get_rx_csum(dev);
1222
1223 if (!rxcsum)
Herbert Xub240a0e2008-12-15 23:44:31 -08001224 return -EINVAL;
1225 dev->features |= NETIF_F_GRO;
1226 } else
1227 dev->features &= ~NETIF_F_GRO;
1228
1229 return 0;
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1233{
1234 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001235 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001237 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001239 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001240 return -EOPNOTSUPP;
1241
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001242 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001243 if (test_len < 0)
1244 return test_len;
1245 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (copy_from_user(&test, useraddr, sizeof(test)))
1248 return -EFAULT;
1249
Jeff Garzikff03d492007-08-15 16:01:08 -07001250 test.len = test_len;
1251 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (!data)
1253 return -ENOMEM;
1254
1255 ops->self_test(dev, &test, data);
1256
1257 ret = -EFAULT;
1258 if (copy_to_user(useraddr, &test, sizeof(test)))
1259 goto out;
1260 useraddr += sizeof(test);
1261 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1262 goto out;
1263 ret = 0;
1264
1265 out:
1266 kfree(data);
1267 return ret;
1268}
1269
1270static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1271{
1272 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001273 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 u8 *data;
1275 int ret;
1276
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001277 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EOPNOTSUPP;
1279
1280 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1281 return -EFAULT;
1282
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001283 ret = ops->get_sset_count(dev, gstrings.string_set);
1284 if (ret < 0)
1285 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001286
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001287 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1290 if (!data)
1291 return -ENOMEM;
1292
1293 ops->get_strings(dev, gstrings.string_set, data);
1294
1295 ret = -EFAULT;
1296 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1297 goto out;
1298 useraddr += sizeof(gstrings);
1299 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1300 goto out;
1301 ret = 0;
1302
1303 out:
1304 kfree(data);
1305 return ret;
1306}
1307
1308static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1309{
1310 struct ethtool_value id;
1311
1312 if (!dev->ethtool_ops->phys_id)
1313 return -EOPNOTSUPP;
1314
1315 if (copy_from_user(&id, useraddr, sizeof(id)))
1316 return -EFAULT;
1317
1318 return dev->ethtool_ops->phys_id(dev, id.data);
1319}
1320
1321static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1322{
1323 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001324 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001326 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001328 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001329 return -EOPNOTSUPP;
1330
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001331 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001332 if (n_stats < 0)
1333 return n_stats;
1334 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1337 return -EFAULT;
1338
Jeff Garzikff03d492007-08-15 16:01:08 -07001339 stats.n_stats = n_stats;
1340 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!data)
1342 return -ENOMEM;
1343
1344 ops->get_ethtool_stats(dev, &stats, data);
1345
1346 ret = -EFAULT;
1347 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1348 goto out;
1349 useraddr += sizeof(stats);
1350 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1351 goto out;
1352 ret = 0;
1353
1354 out:
1355 kfree(data);
1356 return ret;
1357}
1358
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001359static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001360{
1361 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001362
Matthew Wilcox313674a2007-07-31 14:00:29 -07001363 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001364 return -EFAULT;
1365
Matthew Wilcox313674a2007-07-31 14:00:29 -07001366 if (epaddr.size < dev->addr_len)
1367 return -ETOOSMALL;
1368 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001369
Jon Wetzela6f9a702005-08-20 17:15:54 -07001370 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001371 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001372 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001373 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1374 return -EFAULT;
1375 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001376}
1377
Jeff Garzik13c99b22007-08-15 16:01:56 -07001378static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1379 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001380{
Roland Dreier8e557422010-02-11 12:14:23 -08001381 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001382
Jeff Garzik13c99b22007-08-15 16:01:56 -07001383 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001384 return -EOPNOTSUPP;
1385
Jeff Garzik13c99b22007-08-15 16:01:56 -07001386 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001387
1388 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1389 return -EFAULT;
1390 return 0;
1391}
1392
Jeff Garzik13c99b22007-08-15 16:01:56 -07001393static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1394 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001395{
1396 struct ethtool_value edata;
1397
Jeff Garzik13c99b22007-08-15 16:01:56 -07001398 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001399 return -EOPNOTSUPP;
1400
1401 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1402 return -EFAULT;
1403
Jeff Garzik13c99b22007-08-15 16:01:56 -07001404 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001405 return 0;
1406}
1407
Jeff Garzik13c99b22007-08-15 16:01:56 -07001408static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1409 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001410{
1411 struct ethtool_value edata;
1412
Jeff Garzik13c99b22007-08-15 16:01:56 -07001413 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001414 return -EOPNOTSUPP;
1415
1416 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1417 return -EFAULT;
1418
Jeff Garzik13c99b22007-08-15 16:01:56 -07001419 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001420}
1421
chavey97f8aef2010-04-07 21:54:42 -07001422static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1423 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001424{
1425 struct ethtool_flash efl;
1426
1427 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1428 return -EFAULT;
1429
1430 if (!dev->ethtool_ops->flash_device)
1431 return -EOPNOTSUPP;
1432
1433 return dev->ethtool_ops->flash_device(dev, &efl);
1434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/* The main entry point in this file. Called from net/core/dev.c */
1437
Eric W. Biederman881d9662007-09-17 11:56:21 -07001438int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001440 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 void __user *useraddr = ifr->ifr_data;
1442 u32 ethcmd;
1443 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -07001444 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (!dev || !netif_device_present(dev))
1447 return -ENODEV;
1448
chavey97f8aef2010-04-07 21:54:42 -07001449 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EFAULT;
1451
Ben Hutchings01414802010-08-17 02:31:15 -07001452 if (!dev->ethtool_ops) {
1453 /* ETHTOOL_GDRVINFO does not require any driver support.
1454 * It is also unprivileged and does not change anything,
1455 * so we can take a shortcut to it. */
1456 if (ethcmd == ETHTOOL_GDRVINFO)
1457 return ethtool_get_drvinfo(dev, useraddr);
1458 else
1459 return -EOPNOTSUPP;
1460 }
1461
Stephen Hemminger75f31232006-09-28 15:13:37 -07001462 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001463 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001464 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001465 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001466 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001467 case ETHTOOL_GCOALESCE:
1468 case ETHTOOL_GRINGPARAM:
1469 case ETHTOOL_GPAUSEPARAM:
1470 case ETHTOOL_GRXCSUM:
1471 case ETHTOOL_GTXCSUM:
1472 case ETHTOOL_GSG:
1473 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001474 case ETHTOOL_GTSO:
1475 case ETHTOOL_GPERMADDR:
1476 case ETHTOOL_GUFO:
1477 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001478 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001479 case ETHTOOL_GFLAGS:
1480 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001481 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001482 case ETHTOOL_GRXRINGS:
1483 case ETHTOOL_GRXCLSRLCNT:
1484 case ETHTOOL_GRXCLSRULE:
1485 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001486 break;
1487 default:
1488 if (!capable(CAP_NET_ADMIN))
1489 return -EPERM;
1490 }
1491
chavey97f8aef2010-04-07 21:54:42 -07001492 if (dev->ethtool_ops->begin) {
1493 rc = dev->ethtool_ops->begin(dev);
1494 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001496 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001497 old_features = dev->features;
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 switch (ethcmd) {
1500 case ETHTOOL_GSET:
1501 rc = ethtool_get_settings(dev, useraddr);
1502 break;
1503 case ETHTOOL_SSET:
1504 rc = ethtool_set_settings(dev, useraddr);
1505 break;
1506 case ETHTOOL_GDRVINFO:
1507 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 break;
1509 case ETHTOOL_GREGS:
1510 rc = ethtool_get_regs(dev, useraddr);
1511 break;
1512 case ETHTOOL_GWOL:
1513 rc = ethtool_get_wol(dev, useraddr);
1514 break;
1515 case ETHTOOL_SWOL:
1516 rc = ethtool_set_wol(dev, useraddr);
1517 break;
1518 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001519 rc = ethtool_get_value(dev, useraddr, ethcmd,
1520 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001523 rc = ethtool_set_value_void(dev, useraddr,
1524 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 break;
1526 case ETHTOOL_NWAY_RST:
1527 rc = ethtool_nway_reset(dev);
1528 break;
1529 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001530 rc = ethtool_get_value(dev, useraddr, ethcmd,
1531 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 break;
1533 case ETHTOOL_GEEPROM:
1534 rc = ethtool_get_eeprom(dev, useraddr);
1535 break;
1536 case ETHTOOL_SEEPROM:
1537 rc = ethtool_set_eeprom(dev, useraddr);
1538 break;
1539 case ETHTOOL_GCOALESCE:
1540 rc = ethtool_get_coalesce(dev, useraddr);
1541 break;
1542 case ETHTOOL_SCOALESCE:
1543 rc = ethtool_set_coalesce(dev, useraddr);
1544 break;
1545 case ETHTOOL_GRINGPARAM:
1546 rc = ethtool_get_ringparam(dev, useraddr);
1547 break;
1548 case ETHTOOL_SRINGPARAM:
1549 rc = ethtool_set_ringparam(dev, useraddr);
1550 break;
1551 case ETHTOOL_GPAUSEPARAM:
1552 rc = ethtool_get_pauseparam(dev, useraddr);
1553 break;
1554 case ETHTOOL_SPAUSEPARAM:
1555 rc = ethtool_set_pauseparam(dev, useraddr);
1556 break;
1557 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001558 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001559 (dev->ethtool_ops->get_rx_csum ?
1560 dev->ethtool_ops->get_rx_csum :
1561 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 break;
1563 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001564 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001567 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001568 (dev->ethtool_ops->get_tx_csum ?
1569 dev->ethtool_ops->get_tx_csum :
1570 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572 case ETHTOOL_STXCSUM:
1573 rc = ethtool_set_tx_csum(dev, useraddr);
1574 break;
1575 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001576 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001577 (dev->ethtool_ops->get_sg ?
1578 dev->ethtool_ops->get_sg :
1579 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 break;
1581 case ETHTOOL_SSG:
1582 rc = ethtool_set_sg(dev, useraddr);
1583 break;
1584 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001585 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001586 (dev->ethtool_ops->get_tso ?
1587 dev->ethtool_ops->get_tso :
1588 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 break;
1590 case ETHTOOL_STSO:
1591 rc = ethtool_set_tso(dev, useraddr);
1592 break;
1593 case ETHTOOL_TEST:
1594 rc = ethtool_self_test(dev, useraddr);
1595 break;
1596 case ETHTOOL_GSTRINGS:
1597 rc = ethtool_get_strings(dev, useraddr);
1598 break;
1599 case ETHTOOL_PHYS_ID:
1600 rc = ethtool_phys_id(dev, useraddr);
1601 break;
1602 case ETHTOOL_GSTATS:
1603 rc = ethtool_get_stats(dev, useraddr);
1604 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001605 case ETHTOOL_GPERMADDR:
1606 rc = ethtool_get_perm_addr(dev, useraddr);
1607 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001608 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001609 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001610 (dev->ethtool_ops->get_ufo ?
1611 dev->ethtool_ops->get_ufo :
1612 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001613 break;
1614 case ETHTOOL_SUFO:
1615 rc = ethtool_set_ufo(dev, useraddr);
1616 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001617 case ETHTOOL_GGSO:
1618 rc = ethtool_get_gso(dev, useraddr);
1619 break;
1620 case ETHTOOL_SGSO:
1621 rc = ethtool_set_gso(dev, useraddr);
1622 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001623 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001624 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001625 (dev->ethtool_ops->get_flags ?
1626 dev->ethtool_ops->get_flags :
1627 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001628 break;
1629 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001630 rc = ethtool_set_value(dev, useraddr,
1631 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001632 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001633 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001634 rc = ethtool_get_value(dev, useraddr, ethcmd,
1635 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001636 break;
1637 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001638 rc = ethtool_set_value(dev, useraddr,
1639 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001640 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001641 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001642 case ETHTOOL_GRXRINGS:
1643 case ETHTOOL_GRXCLSRLCNT:
1644 case ETHTOOL_GRXCLSRULE:
1645 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001646 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001647 break;
1648 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001649 case ETHTOOL_SRXCLSRLDEL:
1650 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001651 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001652 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001653 case ETHTOOL_GGRO:
1654 rc = ethtool_get_gro(dev, useraddr);
1655 break;
1656 case ETHTOOL_SGRO:
1657 rc = ethtool_set_gro(dev, useraddr);
1658 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001659 case ETHTOOL_FLASHDEV:
1660 rc = ethtool_flash_device(dev, useraddr);
1661 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001662 case ETHTOOL_RESET:
1663 rc = ethtool_reset(dev, useraddr);
1664 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001665 case ETHTOOL_SRXNTUPLE:
1666 rc = ethtool_set_rx_ntuple(dev, useraddr);
1667 break;
1668 case ETHTOOL_GRXNTUPLE:
1669 rc = ethtool_get_rx_ntuple(dev, useraddr);
1670 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001671 case ETHTOOL_GSSET_INFO:
1672 rc = ethtool_get_sset_info(dev, useraddr);
1673 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001674 case ETHTOOL_GRXFHINDIR:
1675 rc = ethtool_get_rxfh_indir(dev, useraddr);
1676 break;
1677 case ETHTOOL_SRXFHINDIR:
1678 rc = ethtool_set_rxfh_indir(dev, useraddr);
1679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001681 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001683
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001684 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001686
1687 if (old_features != dev->features)
1688 netdev_features_change(dev);
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}