blob: a29b43d0b450a5e1a265375bd4f77797f0b23d79 [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>
20#include <asm/uaccess.h>
21
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090022/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Some useful ethtool_ops methods that're device independent.
24 * If we find that all drivers want to do the same thing here,
25 * we can turn these into dev_() function calls.
26 */
27
28u32 ethtool_op_get_link(struct net_device *dev)
29{
30 return netif_carrier_ok(dev) ? 1 : 0;
31}
32
33u32 ethtool_op_get_tx_csum(struct net_device *dev)
34{
Herbert Xu8648b302006-06-17 22:06:05 -070035 return (dev->features & NETIF_F_ALL_CSUM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
39{
40 if (data)
41 dev->features |= NETIF_F_IP_CSUM;
42 else
43 dev->features &= ~NETIF_F_IP_CSUM;
44
45 return 0;
46}
47
Jon Mason69f6a0f2005-05-29 20:27:24 -070048int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
49{
50 if (data)
51 dev->features |= NETIF_F_HW_CSUM;
52 else
53 dev->features &= ~NETIF_F_HW_CSUM;
54
55 return 0;
56}
Michael Chan6460d942007-07-14 19:07:52 -070057
58int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
59{
60 if (data)
61 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
62 else
63 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
64
65 return 0;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068u32 ethtool_op_get_sg(struct net_device *dev)
69{
70 return (dev->features & NETIF_F_SG) != 0;
71}
72
73int ethtool_op_set_sg(struct net_device *dev, u32 data)
74{
75 if (data)
76 dev->features |= NETIF_F_SG;
77 else
78 dev->features &= ~NETIF_F_SG;
79
80 return 0;
81}
82
83u32 ethtool_op_get_tso(struct net_device *dev)
84{
85 return (dev->features & NETIF_F_TSO) != 0;
86}
87
88int ethtool_op_set_tso(struct net_device *dev, u32 data)
89{
90 if (data)
91 dev->features |= NETIF_F_TSO;
92 else
93 dev->features &= ~NETIF_F_TSO;
94
95 return 0;
96}
97
Ananda Rajue89e9cf2005-10-18 15:46:41 -070098u32 ethtool_op_get_ufo(struct net_device *dev)
99{
100 return (dev->features & NETIF_F_UFO) != 0;
101}
102
103int ethtool_op_set_ufo(struct net_device *dev, u32 data)
104{
105 if (data)
106 dev->features |= NETIF_F_UFO;
107 else
108 dev->features &= ~NETIF_F_UFO;
109 return 0;
110}
111
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700112/* the following list of flags are the same as their associated
113 * NETIF_F_xxx values in include/linux/netdevice.h
114 */
115static const u32 flags_dup_features =
116 ETH_FLAG_LRO;
117
118u32 ethtool_op_get_flags(struct net_device *dev)
119{
120 /* in the future, this function will probably contain additional
121 * handling for flags which are not so easily handled
122 * by a simple masking operation
123 */
124
125 return dev->features & flags_dup_features;
126}
127
128int ethtool_op_set_flags(struct net_device *dev, u32 data)
129{
130 if (data & ETH_FLAG_LRO)
131 dev->features |= NETIF_F_LRO;
132 else
133 dev->features &= ~NETIF_F_LRO;
134
135 return 0;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Handlers for each ethtool command */
139
140static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
141{
142 struct ethtool_cmd cmd = { ETHTOOL_GSET };
143 int err;
144
145 if (!dev->ethtool_ops->get_settings)
146 return -EOPNOTSUPP;
147
148 err = dev->ethtool_ops->get_settings(dev, &cmd);
149 if (err < 0)
150 return err;
151
152 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
153 return -EFAULT;
154 return 0;
155}
156
157static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
158{
159 struct ethtool_cmd cmd;
160
161 if (!dev->ethtool_ops->set_settings)
162 return -EOPNOTSUPP;
163
164 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
165 return -EFAULT;
166
167 return dev->ethtool_ops->set_settings(dev, &cmd);
168}
169
170static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
171{
172 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700173 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!ops->get_drvinfo)
176 return -EOPNOTSUPP;
177
178 memset(&info, 0, sizeof(info));
179 info.cmd = ETHTOOL_GDRVINFO;
180 ops->get_drvinfo(dev, &info);
181
Jeff Garzikff03d492007-08-15 16:01:08 -0700182 if (ops->get_sset_count) {
183 int rc;
184
185 rc = ops->get_sset_count(dev, ETH_SS_TEST);
186 if (rc >= 0)
187 info.testinfo_len = rc;
188 rc = ops->get_sset_count(dev, ETH_SS_STATS);
189 if (rc >= 0)
190 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700191 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
192 if (rc >= 0)
193 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700194 } else {
195 /* code path for obsolete hooks */
196
197 if (ops->self_test_count)
198 info.testinfo_len = ops->self_test_count(dev);
199 if (ops->get_stats_count)
200 info.n_stats = ops->get_stats_count(dev);
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (ops->get_regs_len)
203 info.regdump_len = ops->get_regs_len(dev);
204 if (ops->get_eeprom_len)
205 info.eedump_len = ops->get_eeprom_len(dev);
206
207 if (copy_to_user(useraddr, &info, sizeof(info)))
208 return -EFAULT;
209 return 0;
210}
211
212static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
213{
214 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700215 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 void *regbuf;
217 int reglen, ret;
218
219 if (!ops->get_regs || !ops->get_regs_len)
220 return -EOPNOTSUPP;
221
222 if (copy_from_user(&regs, useraddr, sizeof(regs)))
223 return -EFAULT;
224
225 reglen = ops->get_regs_len(dev);
226 if (regs.len > reglen)
227 regs.len = reglen;
228
229 regbuf = kmalloc(reglen, GFP_USER);
230 if (!regbuf)
231 return -ENOMEM;
232
233 ops->get_regs(dev, &regs, regbuf);
234
235 ret = -EFAULT;
236 if (copy_to_user(useraddr, &regs, sizeof(regs)))
237 goto out;
238 useraddr += offsetof(struct ethtool_regs, data);
239 if (copy_to_user(useraddr, regbuf, regs.len))
240 goto out;
241 ret = 0;
242
243 out:
244 kfree(regbuf);
245 return ret;
246}
247
248static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
249{
250 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
251
252 if (!dev->ethtool_ops->get_wol)
253 return -EOPNOTSUPP;
254
255 dev->ethtool_ops->get_wol(dev, &wol);
256
257 if (copy_to_user(useraddr, &wol, sizeof(wol)))
258 return -EFAULT;
259 return 0;
260}
261
262static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
263{
264 struct ethtool_wolinfo wol;
265
266 if (!dev->ethtool_ops->set_wol)
267 return -EOPNOTSUPP;
268
269 if (copy_from_user(&wol, useraddr, sizeof(wol)))
270 return -EFAULT;
271
272 return dev->ethtool_ops->set_wol(dev, &wol);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int ethtool_nway_reset(struct net_device *dev)
276{
277 if (!dev->ethtool_ops->nway_reset)
278 return -EOPNOTSUPP;
279
280 return dev->ethtool_ops->nway_reset(dev);
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
284{
285 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700286 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700287 void __user *userbuf = useraddr + sizeof(eeprom);
288 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700290 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (!ops->get_eeprom || !ops->get_eeprom_len)
293 return -EOPNOTSUPP;
294
295 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
296 return -EFAULT;
297
298 /* Check for wrap and zero */
299 if (eeprom.offset + eeprom.len <= eeprom.offset)
300 return -EINVAL;
301
302 /* Check for exceeding total eeprom len */
303 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
304 return -EINVAL;
305
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700306 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!data)
308 return -ENOMEM;
309
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700310 bytes_remaining = eeprom.len;
311 while (bytes_remaining > 0) {
312 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700314 ret = ops->get_eeprom(dev, &eeprom, data);
315 if (ret)
316 break;
317 if (copy_to_user(userbuf, data, eeprom.len)) {
318 ret = -EFAULT;
319 break;
320 }
321 userbuf += eeprom.len;
322 eeprom.offset += eeprom.len;
323 bytes_remaining -= eeprom.len;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 kfree(data);
327 return ret;
328}
329
330static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
331{
332 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700333 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700334 void __user *userbuf = useraddr + sizeof(eeprom);
335 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700337 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!ops->set_eeprom || !ops->get_eeprom_len)
340 return -EOPNOTSUPP;
341
342 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
343 return -EFAULT;
344
345 /* Check for wrap and zero */
346 if (eeprom.offset + eeprom.len <= eeprom.offset)
347 return -EINVAL;
348
349 /* Check for exceeding total eeprom len */
350 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
351 return -EINVAL;
352
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700353 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!data)
355 return -ENOMEM;
356
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700357 bytes_remaining = eeprom.len;
358 while (bytes_remaining > 0) {
359 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700361 if (copy_from_user(data, userbuf, eeprom.len)) {
362 ret = -EFAULT;
363 break;
364 }
365 ret = ops->set_eeprom(dev, &eeprom, data);
366 if (ret)
367 break;
368 userbuf += eeprom.len;
369 eeprom.offset += eeprom.len;
370 bytes_remaining -= eeprom.len;
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(data);
374 return ret;
375}
376
377static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
378{
379 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
380
381 if (!dev->ethtool_ops->get_coalesce)
382 return -EOPNOTSUPP;
383
384 dev->ethtool_ops->get_coalesce(dev, &coalesce);
385
386 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
387 return -EFAULT;
388 return 0;
389}
390
391static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
392{
393 struct ethtool_coalesce coalesce;
394
David S. Millerfa04ae52005-06-06 15:07:19 -0700395 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -EOPNOTSUPP;
397
398 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
399 return -EFAULT;
400
401 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
402}
403
404static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
405{
406 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
407
408 if (!dev->ethtool_ops->get_ringparam)
409 return -EOPNOTSUPP;
410
411 dev->ethtool_ops->get_ringparam(dev, &ringparam);
412
413 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
414 return -EFAULT;
415 return 0;
416}
417
418static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
419{
420 struct ethtool_ringparam ringparam;
421
422 if (!dev->ethtool_ops->set_ringparam)
423 return -EOPNOTSUPP;
424
425 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
426 return -EFAULT;
427
428 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
429}
430
431static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
432{
433 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
434
435 if (!dev->ethtool_ops->get_pauseparam)
436 return -EOPNOTSUPP;
437
438 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
439
440 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
441 return -EFAULT;
442 return 0;
443}
444
445static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
446{
447 struct ethtool_pauseparam pauseparam;
448
Jeff Garzike1b90c42006-07-17 12:54:40 -0400449 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -EOPNOTSUPP;
451
452 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
453 return -EFAULT;
454
455 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static int __ethtool_set_sg(struct net_device *dev, u32 data)
459{
460 int err;
461
462 if (!data && dev->ethtool_ops->set_tso) {
463 err = dev->ethtool_ops->set_tso(dev, 0);
464 if (err)
465 return err;
466 }
467
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700468 if (!data && dev->ethtool_ops->set_ufo) {
469 err = dev->ethtool_ops->set_ufo(dev, 0);
470 if (err)
471 return err;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return dev->ethtool_ops->set_sg(dev, data);
474}
475
476static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
477{
478 struct ethtool_value edata;
479 int err;
480
481 if (!dev->ethtool_ops->set_tx_csum)
482 return -EOPNOTSUPP;
483
484 if (copy_from_user(&edata, useraddr, sizeof(edata)))
485 return -EFAULT;
486
487 if (!edata.data && dev->ethtool_ops->set_sg) {
488 err = __ethtool_set_sg(dev, 0);
489 if (err)
490 return err;
491 }
492
493 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
497{
498 struct ethtool_value edata;
499
500 if (!dev->ethtool_ops->set_sg)
501 return -EOPNOTSUPP;
502
503 if (copy_from_user(&edata, useraddr, sizeof(edata)))
504 return -EFAULT;
505
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900506 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -0700507 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EINVAL;
509
510 return __ethtool_set_sg(dev, edata.data);
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
514{
515 struct ethtool_value edata;
516
517 if (!dev->ethtool_ops->set_tso)
518 return -EOPNOTSUPP;
519
520 if (copy_from_user(&edata, useraddr, sizeof(edata)))
521 return -EFAULT;
522
523 if (edata.data && !(dev->features & NETIF_F_SG))
524 return -EINVAL;
525
526 return dev->ethtool_ops->set_tso(dev, edata.data);
527}
528
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700529static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
530{
531 struct ethtool_value edata;
532
533 if (!dev->ethtool_ops->set_ufo)
534 return -EOPNOTSUPP;
535 if (copy_from_user(&edata, useraddr, sizeof(edata)))
536 return -EFAULT;
537 if (edata.data && !(dev->features & NETIF_F_SG))
538 return -EINVAL;
539 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
540 return -EINVAL;
541 return dev->ethtool_ops->set_ufo(dev, edata.data);
542}
543
Herbert Xu37c31852006-06-22 03:07:29 -0700544static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
545{
546 struct ethtool_value edata = { ETHTOOL_GGSO };
547
548 edata.data = dev->features & NETIF_F_GSO;
549 if (copy_to_user(useraddr, &edata, sizeof(edata)))
550 return -EFAULT;
551 return 0;
552}
553
554static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
555{
556 struct ethtool_value edata;
557
558 if (copy_from_user(&edata, useraddr, sizeof(edata)))
559 return -EFAULT;
560 if (edata.data)
561 dev->features |= NETIF_F_GSO;
562 else
563 dev->features &= ~NETIF_F_GSO;
564 return 0;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
568{
569 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700570 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -0700572 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Jeff Garzikff03d492007-08-15 16:01:08 -0700574 if (!ops->self_test)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EOPNOTSUPP;
Jeff Garzikff03d492007-08-15 16:01:08 -0700576 if (!ops->get_sset_count && !ops->self_test_count)
577 return -EOPNOTSUPP;
578
579 if (ops->get_sset_count)
580 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
581 else
582 /* code path for obsolete hook */
583 test_len = ops->self_test_count(dev);
584 if (test_len < 0)
585 return test_len;
586 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (copy_from_user(&test, useraddr, sizeof(test)))
589 return -EFAULT;
590
Jeff Garzikff03d492007-08-15 16:01:08 -0700591 test.len = test_len;
592 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!data)
594 return -ENOMEM;
595
596 ops->self_test(dev, &test, data);
597
598 ret = -EFAULT;
599 if (copy_to_user(useraddr, &test, sizeof(test)))
600 goto out;
601 useraddr += sizeof(test);
602 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
603 goto out;
604 ret = 0;
605
606 out:
607 kfree(data);
608 return ret;
609}
610
611static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
612{
613 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700614 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 u8 *data;
616 int ret;
617
618 if (!ops->get_strings)
619 return -EOPNOTSUPP;
620
621 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
622 return -EFAULT;
623
Jeff Garzikff03d492007-08-15 16:01:08 -0700624 if (ops->get_sset_count) {
625 ret = ops->get_sset_count(dev, gstrings.string_set);
626 if (ret < 0)
627 return ret;
628
629 gstrings.len = ret;
630 } else {
631 /* code path for obsolete hooks */
632
633 switch (gstrings.string_set) {
634 case ETH_SS_TEST:
635 if (!ops->self_test_count)
636 return -EOPNOTSUPP;
637 gstrings.len = ops->self_test_count(dev);
638 break;
639 case ETH_SS_STATS:
640 if (!ops->get_stats_count)
641 return -EOPNOTSUPP;
642 gstrings.len = ops->get_stats_count(dev);
643 break;
644 default:
645 return -EINVAL;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
650 if (!data)
651 return -ENOMEM;
652
653 ops->get_strings(dev, gstrings.string_set, data);
654
655 ret = -EFAULT;
656 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
657 goto out;
658 useraddr += sizeof(gstrings);
659 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
660 goto out;
661 ret = 0;
662
663 out:
664 kfree(data);
665 return ret;
666}
667
668static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
669{
670 struct ethtool_value id;
671
672 if (!dev->ethtool_ops->phys_id)
673 return -EOPNOTSUPP;
674
675 if (copy_from_user(&id, useraddr, sizeof(id)))
676 return -EFAULT;
677
678 return dev->ethtool_ops->phys_id(dev, id.data);
679}
680
681static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
682{
683 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700684 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -0700686 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jeff Garzikff03d492007-08-15 16:01:08 -0700688 if (!ops->get_ethtool_stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EOPNOTSUPP;
Jeff Garzikff03d492007-08-15 16:01:08 -0700690 if (!ops->get_sset_count && !ops->get_stats_count)
691 return -EOPNOTSUPP;
692
693 if (ops->get_sset_count)
694 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
695 else
696 /* code path for obsolete hook */
697 n_stats = ops->get_stats_count(dev);
698 if (n_stats < 0)
699 return n_stats;
700 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (copy_from_user(&stats, useraddr, sizeof(stats)))
703 return -EFAULT;
704
Jeff Garzikff03d492007-08-15 16:01:08 -0700705 stats.n_stats = n_stats;
706 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!data)
708 return -ENOMEM;
709
710 ops->get_ethtool_stats(dev, &stats, data);
711
712 ret = -EFAULT;
713 if (copy_to_user(useraddr, &stats, sizeof(stats)))
714 goto out;
715 useraddr += sizeof(stats);
716 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
717 goto out;
718 ret = 0;
719
720 out:
721 kfree(data);
722 return ret;
723}
724
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +0100725static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -0700726{
727 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700728
Matthew Wilcox313674a2007-07-31 14:00:29 -0700729 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -0700730 return -EFAULT;
731
Matthew Wilcox313674a2007-07-31 14:00:29 -0700732 if (epaddr.size < dev->addr_len)
733 return -ETOOSMALL;
734 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700735
Jon Wetzela6f9a702005-08-20 17:15:54 -0700736 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -0700737 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700738 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -0700739 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
740 return -EFAULT;
741 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700742}
743
Jeff Garzik13c99b22007-08-15 16:01:56 -0700744static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
745 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700746{
Jeff Garzik13c99b22007-08-15 16:01:56 -0700747 struct ethtool_value edata = { cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700748
Jeff Garzik13c99b22007-08-15 16:01:56 -0700749 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700750 return -EOPNOTSUPP;
751
Jeff Garzik13c99b22007-08-15 16:01:56 -0700752 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700753
754 if (copy_to_user(useraddr, &edata, sizeof(edata)))
755 return -EFAULT;
756 return 0;
757}
758
Jeff Garzik13c99b22007-08-15 16:01:56 -0700759static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
760 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700761{
762 struct ethtool_value edata;
763
Jeff Garzik13c99b22007-08-15 16:01:56 -0700764 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700765 return -EOPNOTSUPP;
766
767 if (copy_from_user(&edata, useraddr, sizeof(edata)))
768 return -EFAULT;
769
Jeff Garzik13c99b22007-08-15 16:01:56 -0700770 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -0700771 return 0;
772}
773
Jeff Garzik13c99b22007-08-15 16:01:56 -0700774static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
775 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -0700776{
777 struct ethtool_value edata;
778
Jeff Garzik13c99b22007-08-15 16:01:56 -0700779 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -0700780 return -EOPNOTSUPP;
781
782 if (copy_from_user(&edata, useraddr, sizeof(edata)))
783 return -EFAULT;
784
Jeff Garzik13c99b22007-08-15 16:01:56 -0700785 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -0700786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/* The main entry point in this file. Called from net/core/dev.c */
789
Eric W. Biederman881d9662007-09-17 11:56:21 -0700790int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700792 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 void __user *useraddr = ifr->ifr_data;
794 u32 ethcmd;
795 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -0700796 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!dev || !netif_device_present(dev))
799 return -ENODEV;
800
801 if (!dev->ethtool_ops)
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700802 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
805 return -EFAULT;
806
Stephen Hemminger75f31232006-09-28 15:13:37 -0700807 /* Allow some commands to be done by anyone */
808 switch(ethcmd) {
Stephen Hemminger75f31232006-09-28 15:13:37 -0700809 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700810 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700811 case ETHTOOL_GCOALESCE:
812 case ETHTOOL_GRINGPARAM:
813 case ETHTOOL_GPAUSEPARAM:
814 case ETHTOOL_GRXCSUM:
815 case ETHTOOL_GTXCSUM:
816 case ETHTOOL_GSG:
817 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700818 case ETHTOOL_GTSO:
819 case ETHTOOL_GPERMADDR:
820 case ETHTOOL_GUFO:
821 case ETHTOOL_GGSO:
Jeff Garzik339bf022007-08-15 16:01:32 -0700822 case ETHTOOL_GFLAGS:
823 case ETHTOOL_GPFLAGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700824 break;
825 default:
826 if (!capable(CAP_NET_ADMIN))
827 return -EPERM;
828 }
829
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700830 if (dev->ethtool_ops->begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
832 return rc;
833
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700834 old_features = dev->features;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 switch (ethcmd) {
837 case ETHTOOL_GSET:
838 rc = ethtool_get_settings(dev, useraddr);
839 break;
840 case ETHTOOL_SSET:
841 rc = ethtool_set_settings(dev, useraddr);
842 break;
843 case ETHTOOL_GDRVINFO:
844 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
846 case ETHTOOL_GREGS:
847 rc = ethtool_get_regs(dev, useraddr);
848 break;
849 case ETHTOOL_GWOL:
850 rc = ethtool_get_wol(dev, useraddr);
851 break;
852 case ETHTOOL_SWOL:
853 rc = ethtool_set_wol(dev, useraddr);
854 break;
855 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700856 rc = ethtool_get_value(dev, useraddr, ethcmd,
857 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700860 rc = ethtool_set_value_void(dev, useraddr,
861 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 case ETHTOOL_NWAY_RST:
864 rc = ethtool_nway_reset(dev);
865 break;
866 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700867 rc = ethtool_get_value(dev, useraddr, ethcmd,
868 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
870 case ETHTOOL_GEEPROM:
871 rc = ethtool_get_eeprom(dev, useraddr);
872 break;
873 case ETHTOOL_SEEPROM:
874 rc = ethtool_set_eeprom(dev, useraddr);
875 break;
876 case ETHTOOL_GCOALESCE:
877 rc = ethtool_get_coalesce(dev, useraddr);
878 break;
879 case ETHTOOL_SCOALESCE:
880 rc = ethtool_set_coalesce(dev, useraddr);
881 break;
882 case ETHTOOL_GRINGPARAM:
883 rc = ethtool_get_ringparam(dev, useraddr);
884 break;
885 case ETHTOOL_SRINGPARAM:
886 rc = ethtool_set_ringparam(dev, useraddr);
887 break;
888 case ETHTOOL_GPAUSEPARAM:
889 rc = ethtool_get_pauseparam(dev, useraddr);
890 break;
891 case ETHTOOL_SPAUSEPARAM:
892 rc = ethtool_set_pauseparam(dev, useraddr);
893 break;
894 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700895 rc = ethtool_get_value(dev, useraddr, ethcmd,
896 dev->ethtool_ops->get_rx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 case ETHTOOL_SRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700899 rc = ethtool_set_value(dev, useraddr,
900 dev->ethtool_ops->set_rx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
902 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700903 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700904 (dev->ethtool_ops->get_tx_csum ?
905 dev->ethtool_ops->get_tx_csum :
906 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 break;
908 case ETHTOOL_STXCSUM:
909 rc = ethtool_set_tx_csum(dev, useraddr);
910 break;
911 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700912 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700913 (dev->ethtool_ops->get_sg ?
914 dev->ethtool_ops->get_sg :
915 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 case ETHTOOL_SSG:
918 rc = ethtool_set_sg(dev, useraddr);
919 break;
920 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700921 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700922 (dev->ethtool_ops->get_tso ?
923 dev->ethtool_ops->get_tso :
924 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 break;
926 case ETHTOOL_STSO:
927 rc = ethtool_set_tso(dev, useraddr);
928 break;
929 case ETHTOOL_TEST:
930 rc = ethtool_self_test(dev, useraddr);
931 break;
932 case ETHTOOL_GSTRINGS:
933 rc = ethtool_get_strings(dev, useraddr);
934 break;
935 case ETHTOOL_PHYS_ID:
936 rc = ethtool_phys_id(dev, useraddr);
937 break;
938 case ETHTOOL_GSTATS:
939 rc = ethtool_get_stats(dev, useraddr);
940 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700941 case ETHTOOL_GPERMADDR:
942 rc = ethtool_get_perm_addr(dev, useraddr);
943 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700944 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700945 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700946 (dev->ethtool_ops->get_ufo ?
947 dev->ethtool_ops->get_ufo :
948 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700949 break;
950 case ETHTOOL_SUFO:
951 rc = ethtool_set_ufo(dev, useraddr);
952 break;
Herbert Xu37c31852006-06-22 03:07:29 -0700953 case ETHTOOL_GGSO:
954 rc = ethtool_get_gso(dev, useraddr);
955 break;
956 case ETHTOOL_SGSO:
957 rc = ethtool_set_gso(dev, useraddr);
958 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700959 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700960 rc = ethtool_get_value(dev, useraddr, ethcmd,
961 dev->ethtool_ops->get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700962 break;
963 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700964 rc = ethtool_set_value(dev, useraddr,
965 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700966 break;
Jeff Garzik339bf022007-08-15 16:01:32 -0700967 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700968 rc = ethtool_get_value(dev, useraddr, ethcmd,
969 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -0700970 break;
971 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700972 rc = ethtool_set_value(dev, useraddr,
973 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -0700974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700976 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900978
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700979 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700981
982 if (old_features != dev->features)
983 netdev_features_change(dev);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988EXPORT_SYMBOL(ethtool_op_get_link);
989EXPORT_SYMBOL(ethtool_op_get_sg);
990EXPORT_SYMBOL(ethtool_op_get_tso);
991EXPORT_SYMBOL(ethtool_op_get_tx_csum);
992EXPORT_SYMBOL(ethtool_op_set_sg);
993EXPORT_SYMBOL(ethtool_op_set_tso);
994EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Jon Mason69f6a0f2005-05-29 20:27:24 -0700995EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -0700996EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700997EXPORT_SYMBOL(ethtool_op_set_ufo);
998EXPORT_SYMBOL(ethtool_op_get_ufo);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700999EXPORT_SYMBOL(ethtool_op_set_flags);
1000EXPORT_SYMBOL(ethtool_op_get_flags);