]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/ethtool.c
pkt_sched: gen_estimator: Dont report fake rate estimators
[net-next-2.6.git] / net / core / ethtool.c
CommitLineData
1da177e4
LT
1/*
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
61a44b9c 6 * the information ethtool needs.
1da177e4 7 *
61a44b9c
MW
8 * 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.
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/types.h>
4fc268d2 16#include <linux/capability.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
20#include <asm/uaccess.h>
21
4ec93edb 22/*
1da177e4
LT
23 * 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
1896e61f
SS
33u32 ethtool_op_get_rx_csum(struct net_device *dev)
34{
35 return (dev->features & NETIF_F_ALL_CSUM) != 0;
36}
8a729fce 37EXPORT_SYMBOL(ethtool_op_get_rx_csum);
1896e61f 38
1da177e4
LT
39u32 ethtool_op_get_tx_csum(struct net_device *dev)
40{
8648b305 41 return (dev->features & NETIF_F_ALL_CSUM) != 0;
1da177e4 42}
8a729fce 43EXPORT_SYMBOL(ethtool_op_get_tx_csum);
1da177e4
LT
44
45int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
46{
47 if (data)
48 dev->features |= NETIF_F_IP_CSUM;
49 else
50 dev->features &= ~NETIF_F_IP_CSUM;
51
52 return 0;
53}
54
69f6a0fa
JM
55int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
56{
57 if (data)
58 dev->features |= NETIF_F_HW_CSUM;
59 else
60 dev->features &= ~NETIF_F_HW_CSUM;
61
62 return 0;
63}
6460d948
MC
64
65int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
66{
67 if (data)
68 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
69 else
70 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
71
72 return 0;
73}
74
1da177e4
LT
75u32 ethtool_op_get_sg(struct net_device *dev)
76{
77 return (dev->features & NETIF_F_SG) != 0;
78}
79
80int ethtool_op_set_sg(struct net_device *dev, u32 data)
81{
82 if (data)
83 dev->features |= NETIF_F_SG;
84 else
85 dev->features &= ~NETIF_F_SG;
86
87 return 0;
88}
89
90u32 ethtool_op_get_tso(struct net_device *dev)
91{
92 return (dev->features & NETIF_F_TSO) != 0;
93}
94
95int ethtool_op_set_tso(struct net_device *dev, u32 data)
96{
97 if (data)
98 dev->features |= NETIF_F_TSO;
99 else
100 dev->features &= ~NETIF_F_TSO;
101
102 return 0;
103}
104
e89e9cf5
AR
105u32 ethtool_op_get_ufo(struct net_device *dev)
106{
107 return (dev->features & NETIF_F_UFO) != 0;
108}
109
110int ethtool_op_set_ufo(struct net_device *dev, u32 data)
111{
112 if (data)
113 dev->features |= NETIF_F_UFO;
114 else
115 dev->features &= ~NETIF_F_UFO;
116 return 0;
117}
118
3ae7c0b2
JG
119/* the following list of flags are the same as their associated
120 * NETIF_F_xxx values in include/linux/netdevice.h
121 */
122static const u32 flags_dup_features =
123 ETH_FLAG_LRO;
124
125u32 ethtool_op_get_flags(struct net_device *dev)
126{
127 /* in the future, this function will probably contain additional
128 * handling for flags which are not so easily handled
129 * by a simple masking operation
130 */
131
132 return dev->features & flags_dup_features;
133}
134
135int ethtool_op_set_flags(struct net_device *dev, u32 data)
136{
137 if (data & ETH_FLAG_LRO)
138 dev->features |= NETIF_F_LRO;
139 else
140 dev->features &= ~NETIF_F_LRO;
141
142 return 0;
143}
144
1da177e4
LT
145/* Handlers for each ethtool command */
146
147static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
148{
149 struct ethtool_cmd cmd = { ETHTOOL_GSET };
150 int err;
151
152 if (!dev->ethtool_ops->get_settings)
153 return -EOPNOTSUPP;
154
155 err = dev->ethtool_ops->get_settings(dev, &cmd);
156 if (err < 0)
157 return err;
158
159 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
160 return -EFAULT;
161 return 0;
162}
163
164static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
165{
166 struct ethtool_cmd cmd;
167
168 if (!dev->ethtool_ops->set_settings)
169 return -EOPNOTSUPP;
170
171 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
172 return -EFAULT;
173
174 return dev->ethtool_ops->set_settings(dev, &cmd);
175}
176
177static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
178{
179 struct ethtool_drvinfo info;
76fd8593 180 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
181
182 if (!ops->get_drvinfo)
183 return -EOPNOTSUPP;
184
185 memset(&info, 0, sizeof(info));
186 info.cmd = ETHTOOL_GDRVINFO;
187 ops->get_drvinfo(dev, &info);
188
ff03d49f
JG
189 if (ops->get_sset_count) {
190 int rc;
191
192 rc = ops->get_sset_count(dev, ETH_SS_TEST);
193 if (rc >= 0)
194 info.testinfo_len = rc;
195 rc = ops->get_sset_count(dev, ETH_SS_STATS);
196 if (rc >= 0)
197 info.n_stats = rc;
339bf024
JG
198 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
199 if (rc >= 0)
200 info.n_priv_flags = rc;
ff03d49f 201 }
1da177e4
LT
202 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
59089d8d 212static int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
0853ad66
SB
213{
214 struct ethtool_rxnfc cmd;
215
59089d8d 216 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
217 return -EOPNOTSUPP;
218
219 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
220 return -EFAULT;
221
59089d8d 222 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
0853ad66
SB
223}
224
59089d8d 225static int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
0853ad66
SB
226{
227 struct ethtool_rxnfc info;
59089d8d
SB
228 const struct ethtool_ops *ops = dev->ethtool_ops;
229 int ret;
230 void *rule_buf = NULL;
0853ad66 231
59089d8d 232 if (!ops->get_rxnfc)
0853ad66
SB
233 return -EOPNOTSUPP;
234
235 if (copy_from_user(&info, useraddr, sizeof(info)))
236 return -EFAULT;
237
59089d8d
SB
238 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
239 if (info.rule_cnt > 0) {
240 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
241 GFP_USER);
242 if (!rule_buf)
243 return -ENOMEM;
244 }
245 }
0853ad66 246
59089d8d
SB
247 ret = ops->get_rxnfc(dev, &info, rule_buf);
248 if (ret < 0)
249 goto err_out;
250
251 ret = -EFAULT;
0853ad66 252 if (copy_to_user(useraddr, &info, sizeof(info)))
59089d8d
SB
253 goto err_out;
254
255 if (rule_buf) {
256 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
257 if (copy_to_user(useraddr, rule_buf,
258 info.rule_cnt * sizeof(u32)))
259 goto err_out;
260 }
261 ret = 0;
262
263err_out:
c9caceca 264 kfree(rule_buf);
59089d8d
SB
265
266 return ret;
0853ad66
SB
267}
268
1da177e4
LT
269static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
270{
271 struct ethtool_regs regs;
76fd8593 272 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
273 void *regbuf;
274 int reglen, ret;
275
276 if (!ops->get_regs || !ops->get_regs_len)
277 return -EOPNOTSUPP;
278
279 if (copy_from_user(&regs, useraddr, sizeof(regs)))
280 return -EFAULT;
281
282 reglen = ops->get_regs_len(dev);
283 if (regs.len > reglen)
284 regs.len = reglen;
285
286 regbuf = kmalloc(reglen, GFP_USER);
287 if (!regbuf)
288 return -ENOMEM;
289
290 ops->get_regs(dev, &regs, regbuf);
291
292 ret = -EFAULT;
293 if (copy_to_user(useraddr, &regs, sizeof(regs)))
294 goto out;
295 useraddr += offsetof(struct ethtool_regs, data);
296 if (copy_to_user(useraddr, regbuf, regs.len))
297 goto out;
298 ret = 0;
299
300 out:
301 kfree(regbuf);
302 return ret;
303}
304
305static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
306{
307 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
308
309 if (!dev->ethtool_ops->get_wol)
310 return -EOPNOTSUPP;
311
312 dev->ethtool_ops->get_wol(dev, &wol);
313
314 if (copy_to_user(useraddr, &wol, sizeof(wol)))
315 return -EFAULT;
316 return 0;
317}
318
319static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
320{
321 struct ethtool_wolinfo wol;
322
323 if (!dev->ethtool_ops->set_wol)
324 return -EOPNOTSUPP;
325
326 if (copy_from_user(&wol, useraddr, sizeof(wol)))
327 return -EFAULT;
328
329 return dev->ethtool_ops->set_wol(dev, &wol);
330}
331
1da177e4
LT
332static int ethtool_nway_reset(struct net_device *dev)
333{
334 if (!dev->ethtool_ops->nway_reset)
335 return -EOPNOTSUPP;
336
337 return dev->ethtool_ops->nway_reset(dev);
338}
339
1da177e4
LT
340static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
341{
342 struct ethtool_eeprom eeprom;
76fd8593 343 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
344 void __user *userbuf = useraddr + sizeof(eeprom);
345 u32 bytes_remaining;
1da177e4 346 u8 *data;
b131dd5d 347 int ret = 0;
1da177e4
LT
348
349 if (!ops->get_eeprom || !ops->get_eeprom_len)
350 return -EOPNOTSUPP;
351
352 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
353 return -EFAULT;
354
355 /* Check for wrap and zero */
356 if (eeprom.offset + eeprom.len <= eeprom.offset)
357 return -EINVAL;
358
359 /* Check for exceeding total eeprom len */
360 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
361 return -EINVAL;
362
b131dd5d 363 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
364 if (!data)
365 return -ENOMEM;
366
b131dd5d
MSB
367 bytes_remaining = eeprom.len;
368 while (bytes_remaining > 0) {
369 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
370
371 ret = ops->get_eeprom(dev, &eeprom, data);
372 if (ret)
373 break;
374 if (copy_to_user(userbuf, data, eeprom.len)) {
375 ret = -EFAULT;
376 break;
377 }
378 userbuf += eeprom.len;
379 eeprom.offset += eeprom.len;
380 bytes_remaining -= eeprom.len;
381 }
1da177e4 382
c5835df9
MSB
383 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
384 eeprom.offset -= eeprom.len;
385 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
386 ret = -EFAULT;
387
1da177e4
LT
388 kfree(data);
389 return ret;
390}
391
392static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
393{
394 struct ethtool_eeprom eeprom;
76fd8593 395 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
396 void __user *userbuf = useraddr + sizeof(eeprom);
397 u32 bytes_remaining;
1da177e4 398 u8 *data;
b131dd5d 399 int ret = 0;
1da177e4
LT
400
401 if (!ops->set_eeprom || !ops->get_eeprom_len)
402 return -EOPNOTSUPP;
403
404 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
405 return -EFAULT;
406
407 /* Check for wrap and zero */
408 if (eeprom.offset + eeprom.len <= eeprom.offset)
409 return -EINVAL;
410
411 /* Check for exceeding total eeprom len */
412 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
413 return -EINVAL;
414
b131dd5d 415 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
416 if (!data)
417 return -ENOMEM;
418
b131dd5d
MSB
419 bytes_remaining = eeprom.len;
420 while (bytes_remaining > 0) {
421 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
422
423 if (copy_from_user(data, userbuf, eeprom.len)) {
424 ret = -EFAULT;
425 break;
426 }
427 ret = ops->set_eeprom(dev, &eeprom, data);
428 if (ret)
429 break;
430 userbuf += eeprom.len;
431 eeprom.offset += eeprom.len;
432 bytes_remaining -= eeprom.len;
433 }
1da177e4 434
1da177e4
LT
435 kfree(data);
436 return ret;
437}
438
439static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
440{
441 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
442
443 if (!dev->ethtool_ops->get_coalesce)
444 return -EOPNOTSUPP;
445
446 dev->ethtool_ops->get_coalesce(dev, &coalesce);
447
448 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
449 return -EFAULT;
450 return 0;
451}
452
453static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
454{
455 struct ethtool_coalesce coalesce;
456
fa04ae5c 457 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
458 return -EOPNOTSUPP;
459
460 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
461 return -EFAULT;
462
463 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
464}
465
466static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
467{
468 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
469
470 if (!dev->ethtool_ops->get_ringparam)
471 return -EOPNOTSUPP;
472
473 dev->ethtool_ops->get_ringparam(dev, &ringparam);
474
475 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
476 return -EFAULT;
477 return 0;
478}
479
480static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
481{
482 struct ethtool_ringparam ringparam;
483
484 if (!dev->ethtool_ops->set_ringparam)
485 return -EOPNOTSUPP;
486
487 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
488 return -EFAULT;
489
490 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
491}
492
493static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
494{
495 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
496
497 if (!dev->ethtool_ops->get_pauseparam)
498 return -EOPNOTSUPP;
499
500 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
501
502 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
503 return -EFAULT;
504 return 0;
505}
506
507static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
508{
509 struct ethtool_pauseparam pauseparam;
510
e1b90c41 511 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
512 return -EOPNOTSUPP;
513
514 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
515 return -EFAULT;
516
517 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
518}
519
1da177e4
LT
520static int __ethtool_set_sg(struct net_device *dev, u32 data)
521{
522 int err;
523
524 if (!data && dev->ethtool_ops->set_tso) {
525 err = dev->ethtool_ops->set_tso(dev, 0);
526 if (err)
527 return err;
528 }
529
e89e9cf5
AR
530 if (!data && dev->ethtool_ops->set_ufo) {
531 err = dev->ethtool_ops->set_ufo(dev, 0);
532 if (err)
533 return err;
534 }
1da177e4
LT
535 return dev->ethtool_ops->set_sg(dev, data);
536}
537
538static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
539{
540 struct ethtool_value edata;
541 int err;
542
543 if (!dev->ethtool_ops->set_tx_csum)
544 return -EOPNOTSUPP;
545
546 if (copy_from_user(&edata, useraddr, sizeof(edata)))
547 return -EFAULT;
548
549 if (!edata.data && dev->ethtool_ops->set_sg) {
550 err = __ethtool_set_sg(dev, 0);
551 if (err)
552 return err;
553 }
554
555 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
556}
557
b240a0e5
HX
558static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
559{
560 struct ethtool_value edata;
561
562 if (!dev->ethtool_ops->set_rx_csum)
563 return -EOPNOTSUPP;
564
565 if (copy_from_user(&edata, useraddr, sizeof(edata)))
566 return -EFAULT;
567
568 if (!edata.data && dev->ethtool_ops->set_sg)
569 dev->features &= ~NETIF_F_GRO;
570
571 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
572}
573
1da177e4
LT
574static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
575{
576 struct ethtool_value edata;
577
578 if (!dev->ethtool_ops->set_sg)
579 return -EOPNOTSUPP;
580
581 if (copy_from_user(&edata, useraddr, sizeof(edata)))
582 return -EFAULT;
583
4ec93edb 584 if (edata.data &&
8648b305 585 !(dev->features & NETIF_F_ALL_CSUM))
1da177e4
LT
586 return -EINVAL;
587
588 return __ethtool_set_sg(dev, edata.data);
589}
590
1da177e4
LT
591static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
592{
593 struct ethtool_value edata;
594
595 if (!dev->ethtool_ops->set_tso)
596 return -EOPNOTSUPP;
597
598 if (copy_from_user(&edata, useraddr, sizeof(edata)))
599 return -EFAULT;
600
601 if (edata.data && !(dev->features & NETIF_F_SG))
602 return -EINVAL;
603
604 return dev->ethtool_ops->set_tso(dev, edata.data);
605}
606
e89e9cf5
AR
607static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
608{
609 struct ethtool_value edata;
610
611 if (!dev->ethtool_ops->set_ufo)
612 return -EOPNOTSUPP;
613 if (copy_from_user(&edata, useraddr, sizeof(edata)))
614 return -EFAULT;
615 if (edata.data && !(dev->features & NETIF_F_SG))
616 return -EINVAL;
617 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
618 return -EINVAL;
619 return dev->ethtool_ops->set_ufo(dev, edata.data);
620}
621
37c3185a
HX
622static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
623{
624 struct ethtool_value edata = { ETHTOOL_GGSO };
625
626 edata.data = dev->features & NETIF_F_GSO;
627 if (copy_to_user(useraddr, &edata, sizeof(edata)))
628 return -EFAULT;
629 return 0;
630}
631
632static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
633{
634 struct ethtool_value edata;
635
636 if (copy_from_user(&edata, useraddr, sizeof(edata)))
637 return -EFAULT;
638 if (edata.data)
639 dev->features |= NETIF_F_GSO;
640 else
641 dev->features &= ~NETIF_F_GSO;
642 return 0;
643}
644
b240a0e5
HX
645static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
646{
647 struct ethtool_value edata = { ETHTOOL_GGRO };
648
649 edata.data = dev->features & NETIF_F_GRO;
650 if (copy_to_user(useraddr, &edata, sizeof(edata)))
651 return -EFAULT;
652 return 0;
653}
654
655static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
656{
657 struct ethtool_value edata;
658
659 if (copy_from_user(&edata, useraddr, sizeof(edata)))
660 return -EFAULT;
661
662 if (edata.data) {
663 if (!dev->ethtool_ops->get_rx_csum ||
664 !dev->ethtool_ops->get_rx_csum(dev))
665 return -EINVAL;
666 dev->features |= NETIF_F_GRO;
667 } else
668 dev->features &= ~NETIF_F_GRO;
669
670 return 0;
671}
672
1da177e4
LT
673static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
674{
675 struct ethtool_test test;
76fd8593 676 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 677 u64 *data;
ff03d49f 678 int ret, test_len;
1da177e4 679
a9828ec6 680 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
681 return -EOPNOTSUPP;
682
a9828ec6 683 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
684 if (test_len < 0)
685 return test_len;
686 WARN_ON(test_len == 0);
687
1da177e4
LT
688 if (copy_from_user(&test, useraddr, sizeof(test)))
689 return -EFAULT;
690
ff03d49f
JG
691 test.len = test_len;
692 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
693 if (!data)
694 return -ENOMEM;
695
696 ops->self_test(dev, &test, data);
697
698 ret = -EFAULT;
699 if (copy_to_user(useraddr, &test, sizeof(test)))
700 goto out;
701 useraddr += sizeof(test);
702 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
703 goto out;
704 ret = 0;
705
706 out:
707 kfree(data);
708 return ret;
709}
710
711static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
712{
713 struct ethtool_gstrings gstrings;
76fd8593 714 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
715 u8 *data;
716 int ret;
717
a9828ec6 718 if (!ops->get_strings || !ops->get_sset_count)
1da177e4
LT
719 return -EOPNOTSUPP;
720
721 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
722 return -EFAULT;
723
a9828ec6
BH
724 ret = ops->get_sset_count(dev, gstrings.string_set);
725 if (ret < 0)
726 return ret;
727
728 gstrings.len = ret;
1da177e4
LT
729
730 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
731 if (!data)
732 return -ENOMEM;
733
734 ops->get_strings(dev, gstrings.string_set, data);
735
736 ret = -EFAULT;
737 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
738 goto out;
739 useraddr += sizeof(gstrings);
740 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
741 goto out;
742 ret = 0;
743
744 out:
745 kfree(data);
746 return ret;
747}
748
749static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
750{
751 struct ethtool_value id;
752
753 if (!dev->ethtool_ops->phys_id)
754 return -EOPNOTSUPP;
755
756 if (copy_from_user(&id, useraddr, sizeof(id)))
757 return -EFAULT;
758
759 return dev->ethtool_ops->phys_id(dev, id.data);
760}
761
762static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
763{
764 struct ethtool_stats stats;
76fd8593 765 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 766 u64 *data;
ff03d49f 767 int ret, n_stats;
1da177e4 768
a9828ec6 769 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
770 return -EOPNOTSUPP;
771
a9828ec6 772 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
773 if (n_stats < 0)
774 return n_stats;
775 WARN_ON(n_stats == 0);
776
1da177e4
LT
777 if (copy_from_user(&stats, useraddr, sizeof(stats)))
778 return -EFAULT;
779
ff03d49f
JG
780 stats.n_stats = n_stats;
781 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1da177e4
LT
782 if (!data)
783 return -ENOMEM;
784
785 ops->get_ethtool_stats(dev, &stats, data);
786
787 ret = -EFAULT;
788 if (copy_to_user(useraddr, &stats, sizeof(stats)))
789 goto out;
790 useraddr += sizeof(stats);
791 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
792 goto out;
793 ret = 0;
794
795 out:
796 kfree(data);
797 return ret;
798}
799
0bf0519d 800static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
801{
802 struct ethtool_perm_addr epaddr;
a6f9a705 803
313674af 804 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
805 return -EFAULT;
806
313674af
MW
807 if (epaddr.size < dev->addr_len)
808 return -ETOOSMALL;
809 epaddr.size = dev->addr_len;
a6f9a705 810
a6f9a705 811 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 812 return -EFAULT;
a6f9a705 813 useraddr += sizeof(epaddr);
313674af
MW
814 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
815 return -EFAULT;
816 return 0;
a6f9a705
JW
817}
818
13c99b24
JG
819static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
820 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 821{
13c99b24 822 struct ethtool_value edata = { cmd };
3ae7c0b2 823
13c99b24 824 if (!actor)
3ae7c0b2
JG
825 return -EOPNOTSUPP;
826
13c99b24 827 edata.data = actor(dev);
3ae7c0b2
JG
828
829 if (copy_to_user(useraddr, &edata, sizeof(edata)))
830 return -EFAULT;
831 return 0;
832}
833
13c99b24
JG
834static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
835 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
836{
837 struct ethtool_value edata;
838
13c99b24 839 if (!actor)
3ae7c0b2
JG
840 return -EOPNOTSUPP;
841
842 if (copy_from_user(&edata, useraddr, sizeof(edata)))
843 return -EFAULT;
844
13c99b24 845 actor(dev, edata.data);
339bf024
JG
846 return 0;
847}
848
13c99b24
JG
849static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
850 int (*actor)(struct net_device *, u32))
339bf024
JG
851{
852 struct ethtool_value edata;
853
13c99b24 854 if (!actor)
339bf024
JG
855 return -EOPNOTSUPP;
856
857 if (copy_from_user(&edata, useraddr, sizeof(edata)))
858 return -EFAULT;
859
13c99b24 860 return actor(dev, edata.data);
339bf024
JG
861}
862
05c6a8d7
AK
863static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
864{
865 struct ethtool_flash efl;
866
867 if (copy_from_user(&efl, useraddr, sizeof(efl)))
868 return -EFAULT;
869
870 if (!dev->ethtool_ops->flash_device)
871 return -EOPNOTSUPP;
872
873 return dev->ethtool_ops->flash_device(dev, &efl);
874}
875
1da177e4
LT
876/* The main entry point in this file. Called from net/core/dev.c */
877
881d966b 878int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 879{
881d966b 880 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4
LT
881 void __user *useraddr = ifr->ifr_data;
882 u32 ethcmd;
883 int rc;
81e81575 884 unsigned long old_features;
1da177e4 885
1da177e4
LT
886 if (!dev || !netif_device_present(dev))
887 return -ENODEV;
888
889 if (!dev->ethtool_ops)
61a44b9c 890 return -EOPNOTSUPP;
1da177e4
LT
891
892 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
893 return -EFAULT;
894
75f3123c
SH
895 /* Allow some commands to be done by anyone */
896 switch(ethcmd) {
75f3123c 897 case ETHTOOL_GDRVINFO:
75f3123c 898 case ETHTOOL_GMSGLVL:
75f3123c
SH
899 case ETHTOOL_GCOALESCE:
900 case ETHTOOL_GRINGPARAM:
901 case ETHTOOL_GPAUSEPARAM:
902 case ETHTOOL_GRXCSUM:
903 case ETHTOOL_GTXCSUM:
904 case ETHTOOL_GSG:
905 case ETHTOOL_GSTRINGS:
75f3123c
SH
906 case ETHTOOL_GTSO:
907 case ETHTOOL_GPERMADDR:
908 case ETHTOOL_GUFO:
909 case ETHTOOL_GGSO:
339bf024
JG
910 case ETHTOOL_GFLAGS:
911 case ETHTOOL_GPFLAGS:
0853ad66 912 case ETHTOOL_GRXFH:
59089d8d
SB
913 case ETHTOOL_GRXRINGS:
914 case ETHTOOL_GRXCLSRLCNT:
915 case ETHTOOL_GRXCLSRULE:
916 case ETHTOOL_GRXCLSRLALL:
75f3123c
SH
917 break;
918 default:
919 if (!capable(CAP_NET_ADMIN))
920 return -EPERM;
921 }
922
e71a4783 923 if (dev->ethtool_ops->begin)
1da177e4
LT
924 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
925 return rc;
926
d8a33ac4
SH
927 old_features = dev->features;
928
1da177e4
LT
929 switch (ethcmd) {
930 case ETHTOOL_GSET:
931 rc = ethtool_get_settings(dev, useraddr);
932 break;
933 case ETHTOOL_SSET:
934 rc = ethtool_set_settings(dev, useraddr);
935 break;
936 case ETHTOOL_GDRVINFO:
937 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
938 break;
939 case ETHTOOL_GREGS:
940 rc = ethtool_get_regs(dev, useraddr);
941 break;
942 case ETHTOOL_GWOL:
943 rc = ethtool_get_wol(dev, useraddr);
944 break;
945 case ETHTOOL_SWOL:
946 rc = ethtool_set_wol(dev, useraddr);
947 break;
948 case ETHTOOL_GMSGLVL:
13c99b24
JG
949 rc = ethtool_get_value(dev, useraddr, ethcmd,
950 dev->ethtool_ops->get_msglevel);
1da177e4
LT
951 break;
952 case ETHTOOL_SMSGLVL:
13c99b24
JG
953 rc = ethtool_set_value_void(dev, useraddr,
954 dev->ethtool_ops->set_msglevel);
1da177e4
LT
955 break;
956 case ETHTOOL_NWAY_RST:
957 rc = ethtool_nway_reset(dev);
958 break;
959 case ETHTOOL_GLINK:
13c99b24
JG
960 rc = ethtool_get_value(dev, useraddr, ethcmd,
961 dev->ethtool_ops->get_link);
1da177e4
LT
962 break;
963 case ETHTOOL_GEEPROM:
964 rc = ethtool_get_eeprom(dev, useraddr);
965 break;
966 case ETHTOOL_SEEPROM:
967 rc = ethtool_set_eeprom(dev, useraddr);
968 break;
969 case ETHTOOL_GCOALESCE:
970 rc = ethtool_get_coalesce(dev, useraddr);
971 break;
972 case ETHTOOL_SCOALESCE:
973 rc = ethtool_set_coalesce(dev, useraddr);
974 break;
975 case ETHTOOL_GRINGPARAM:
976 rc = ethtool_get_ringparam(dev, useraddr);
977 break;
978 case ETHTOOL_SRINGPARAM:
979 rc = ethtool_set_ringparam(dev, useraddr);
980 break;
981 case ETHTOOL_GPAUSEPARAM:
982 rc = ethtool_get_pauseparam(dev, useraddr);
983 break;
984 case ETHTOOL_SPAUSEPARAM:
985 rc = ethtool_set_pauseparam(dev, useraddr);
986 break;
987 case ETHTOOL_GRXCSUM:
13c99b24 988 rc = ethtool_get_value(dev, useraddr, ethcmd,
1896e61f
SS
989 (dev->ethtool_ops->get_rx_csum ?
990 dev->ethtool_ops->get_rx_csum :
991 ethtool_op_get_rx_csum));
1da177e4
LT
992 break;
993 case ETHTOOL_SRXCSUM:
b240a0e5 994 rc = ethtool_set_rx_csum(dev, useraddr);
1da177e4
LT
995 break;
996 case ETHTOOL_GTXCSUM:
13c99b24 997 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
998 (dev->ethtool_ops->get_tx_csum ?
999 dev->ethtool_ops->get_tx_csum :
1000 ethtool_op_get_tx_csum));
1da177e4
LT
1001 break;
1002 case ETHTOOL_STXCSUM:
1003 rc = ethtool_set_tx_csum(dev, useraddr);
1004 break;
1005 case ETHTOOL_GSG:
13c99b24 1006 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1007 (dev->ethtool_ops->get_sg ?
1008 dev->ethtool_ops->get_sg :
1009 ethtool_op_get_sg));
1da177e4
LT
1010 break;
1011 case ETHTOOL_SSG:
1012 rc = ethtool_set_sg(dev, useraddr);
1013 break;
1014 case ETHTOOL_GTSO:
13c99b24 1015 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1016 (dev->ethtool_ops->get_tso ?
1017 dev->ethtool_ops->get_tso :
1018 ethtool_op_get_tso));
1da177e4
LT
1019 break;
1020 case ETHTOOL_STSO:
1021 rc = ethtool_set_tso(dev, useraddr);
1022 break;
1023 case ETHTOOL_TEST:
1024 rc = ethtool_self_test(dev, useraddr);
1025 break;
1026 case ETHTOOL_GSTRINGS:
1027 rc = ethtool_get_strings(dev, useraddr);
1028 break;
1029 case ETHTOOL_PHYS_ID:
1030 rc = ethtool_phys_id(dev, useraddr);
1031 break;
1032 case ETHTOOL_GSTATS:
1033 rc = ethtool_get_stats(dev, useraddr);
1034 break;
a6f9a705
JW
1035 case ETHTOOL_GPERMADDR:
1036 rc = ethtool_get_perm_addr(dev, useraddr);
1037 break;
e89e9cf5 1038 case ETHTOOL_GUFO:
13c99b24 1039 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1040 (dev->ethtool_ops->get_ufo ?
1041 dev->ethtool_ops->get_ufo :
1042 ethtool_op_get_ufo));
e89e9cf5
AR
1043 break;
1044 case ETHTOOL_SUFO:
1045 rc = ethtool_set_ufo(dev, useraddr);
1046 break;
37c3185a
HX
1047 case ETHTOOL_GGSO:
1048 rc = ethtool_get_gso(dev, useraddr);
1049 break;
1050 case ETHTOOL_SGSO:
1051 rc = ethtool_set_gso(dev, useraddr);
1052 break;
3ae7c0b2 1053 case ETHTOOL_GFLAGS:
13c99b24 1054 rc = ethtool_get_value(dev, useraddr, ethcmd,
1896e61f
SS
1055 (dev->ethtool_ops->get_flags ?
1056 dev->ethtool_ops->get_flags :
1057 ethtool_op_get_flags));
3ae7c0b2
JG
1058 break;
1059 case ETHTOOL_SFLAGS:
13c99b24
JG
1060 rc = ethtool_set_value(dev, useraddr,
1061 dev->ethtool_ops->set_flags);
3ae7c0b2 1062 break;
339bf024 1063 case ETHTOOL_GPFLAGS:
13c99b24
JG
1064 rc = ethtool_get_value(dev, useraddr, ethcmd,
1065 dev->ethtool_ops->get_priv_flags);
339bf024
JG
1066 break;
1067 case ETHTOOL_SPFLAGS:
13c99b24
JG
1068 rc = ethtool_set_value(dev, useraddr,
1069 dev->ethtool_ops->set_priv_flags);
339bf024 1070 break;
0853ad66 1071 case ETHTOOL_GRXFH:
59089d8d
SB
1072 case ETHTOOL_GRXRINGS:
1073 case ETHTOOL_GRXCLSRLCNT:
1074 case ETHTOOL_GRXCLSRULE:
1075 case ETHTOOL_GRXCLSRLALL:
1076 rc = ethtool_get_rxnfc(dev, useraddr);
0853ad66
SB
1077 break;
1078 case ETHTOOL_SRXFH:
59089d8d
SB
1079 case ETHTOOL_SRXCLSRLDEL:
1080 case ETHTOOL_SRXCLSRLINS:
1081 rc = ethtool_set_rxnfc(dev, useraddr);
0853ad66 1082 break;
b240a0e5
HX
1083 case ETHTOOL_GGRO:
1084 rc = ethtool_get_gro(dev, useraddr);
1085 break;
1086 case ETHTOOL_SGRO:
1087 rc = ethtool_set_gro(dev, useraddr);
1088 break;
05c6a8d7
AK
1089 case ETHTOOL_FLASHDEV:
1090 rc = ethtool_flash_device(dev, useraddr);
1091 break;
1da177e4 1092 default:
61a44b9c 1093 rc = -EOPNOTSUPP;
1da177e4 1094 }
4ec93edb 1095
e71a4783 1096 if (dev->ethtool_ops->complete)
1da177e4 1097 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
1098
1099 if (old_features != dev->features)
1100 netdev_features_change(dev);
1101
1da177e4 1102 return rc;
1da177e4
LT
1103}
1104
1da177e4
LT
1105EXPORT_SYMBOL(ethtool_op_get_link);
1106EXPORT_SYMBOL(ethtool_op_get_sg);
1107EXPORT_SYMBOL(ethtool_op_get_tso);
1da177e4
LT
1108EXPORT_SYMBOL(ethtool_op_set_sg);
1109EXPORT_SYMBOL(ethtool_op_set_tso);
1110EXPORT_SYMBOL(ethtool_op_set_tx_csum);
69f6a0fa 1111EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
6460d948 1112EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
e89e9cf5
AR
1113EXPORT_SYMBOL(ethtool_op_set_ufo);
1114EXPORT_SYMBOL(ethtool_op_get_ufo);
3ae7c0b2
JG
1115EXPORT_SYMBOL(ethtool_op_set_flags);
1116EXPORT_SYMBOL(ethtool_op_get_flags);