[ETHTOOL]: Add ETHTOOL_[GS]FLAGS sub-ioctls
[safe/jmp/linux-2.6] / net / core / ethtool.c
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
6  * the information ethtool needs.
7  *
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.
12  */
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <asm/uaccess.h>
21
22 /*
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
28 u32 ethtool_op_get_link(struct net_device *dev)
29 {
30         return netif_carrier_ok(dev) ? 1 : 0;
31 }
32
33 u32 ethtool_op_get_tx_csum(struct net_device *dev)
34 {
35         return (dev->features & NETIF_F_ALL_CSUM) != 0;
36 }
37
38 int 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
48 int 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 }
57
58 int 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
68 u32 ethtool_op_get_sg(struct net_device *dev)
69 {
70         return (dev->features & NETIF_F_SG) != 0;
71 }
72
73 int 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
83 u32 ethtool_op_get_tso(struct net_device *dev)
84 {
85         return (dev->features & NETIF_F_TSO) != 0;
86 }
87
88 int 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
98 u32 ethtool_op_get_ufo(struct net_device *dev)
99 {
100         return (dev->features & NETIF_F_UFO) != 0;
101 }
102
103 int 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
112 /* the following list of flags are the same as their associated
113  * NETIF_F_xxx values in include/linux/netdevice.h
114  */
115 static const u32 flags_dup_features =
116         ETH_FLAG_LRO;
117
118 u32 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
128 int 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
138 /* Handlers for each ethtool command */
139
140 static 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
157 static 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
170 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
171 {
172         struct ethtool_drvinfo info;
173         const struct ethtool_ops *ops = dev->ethtool_ops;
174
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
182         if (ops->self_test_count)
183                 info.testinfo_len = ops->self_test_count(dev);
184         if (ops->get_stats_count)
185                 info.n_stats = ops->get_stats_count(dev);
186         if (ops->get_regs_len)
187                 info.regdump_len = ops->get_regs_len(dev);
188         if (ops->get_eeprom_len)
189                 info.eedump_len = ops->get_eeprom_len(dev);
190
191         if (copy_to_user(useraddr, &info, sizeof(info)))
192                 return -EFAULT;
193         return 0;
194 }
195
196 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
197 {
198         struct ethtool_regs regs;
199         const struct ethtool_ops *ops = dev->ethtool_ops;
200         void *regbuf;
201         int reglen, ret;
202
203         if (!ops->get_regs || !ops->get_regs_len)
204                 return -EOPNOTSUPP;
205
206         if (copy_from_user(&regs, useraddr, sizeof(regs)))
207                 return -EFAULT;
208
209         reglen = ops->get_regs_len(dev);
210         if (regs.len > reglen)
211                 regs.len = reglen;
212
213         regbuf = kmalloc(reglen, GFP_USER);
214         if (!regbuf)
215                 return -ENOMEM;
216
217         ops->get_regs(dev, &regs, regbuf);
218
219         ret = -EFAULT;
220         if (copy_to_user(useraddr, &regs, sizeof(regs)))
221                 goto out;
222         useraddr += offsetof(struct ethtool_regs, data);
223         if (copy_to_user(useraddr, regbuf, regs.len))
224                 goto out;
225         ret = 0;
226
227  out:
228         kfree(regbuf);
229         return ret;
230 }
231
232 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
233 {
234         struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
235
236         if (!dev->ethtool_ops->get_wol)
237                 return -EOPNOTSUPP;
238
239         dev->ethtool_ops->get_wol(dev, &wol);
240
241         if (copy_to_user(useraddr, &wol, sizeof(wol)))
242                 return -EFAULT;
243         return 0;
244 }
245
246 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
247 {
248         struct ethtool_wolinfo wol;
249
250         if (!dev->ethtool_ops->set_wol)
251                 return -EOPNOTSUPP;
252
253         if (copy_from_user(&wol, useraddr, sizeof(wol)))
254                 return -EFAULT;
255
256         return dev->ethtool_ops->set_wol(dev, &wol);
257 }
258
259 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
260 {
261         struct ethtool_value edata = { ETHTOOL_GMSGLVL };
262
263         if (!dev->ethtool_ops->get_msglevel)
264                 return -EOPNOTSUPP;
265
266         edata.data = dev->ethtool_ops->get_msglevel(dev);
267
268         if (copy_to_user(useraddr, &edata, sizeof(edata)))
269                 return -EFAULT;
270         return 0;
271 }
272
273 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
274 {
275         struct ethtool_value edata;
276
277         if (!dev->ethtool_ops->set_msglevel)
278                 return -EOPNOTSUPP;
279
280         if (copy_from_user(&edata, useraddr, sizeof(edata)))
281                 return -EFAULT;
282
283         dev->ethtool_ops->set_msglevel(dev, edata.data);
284         return 0;
285 }
286
287 static int ethtool_nway_reset(struct net_device *dev)
288 {
289         if (!dev->ethtool_ops->nway_reset)
290                 return -EOPNOTSUPP;
291
292         return dev->ethtool_ops->nway_reset(dev);
293 }
294
295 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
296 {
297         struct ethtool_value edata = { ETHTOOL_GLINK };
298
299         if (!dev->ethtool_ops->get_link)
300                 return -EOPNOTSUPP;
301
302         edata.data = dev->ethtool_ops->get_link(dev);
303
304         if (copy_to_user(useraddr, &edata, sizeof(edata)))
305                 return -EFAULT;
306         return 0;
307 }
308
309 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
310 {
311         struct ethtool_eeprom eeprom;
312         const struct ethtool_ops *ops = dev->ethtool_ops;
313         u8 *data;
314         int ret;
315
316         if (!ops->get_eeprom || !ops->get_eeprom_len)
317                 return -EOPNOTSUPP;
318
319         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
320                 return -EFAULT;
321
322         /* Check for wrap and zero */
323         if (eeprom.offset + eeprom.len <= eeprom.offset)
324                 return -EINVAL;
325
326         /* Check for exceeding total eeprom len */
327         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
328                 return -EINVAL;
329
330         data = kmalloc(eeprom.len, GFP_USER);
331         if (!data)
332                 return -ENOMEM;
333
334         ret = -EFAULT;
335         if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
336                 goto out;
337
338         ret = ops->get_eeprom(dev, &eeprom, data);
339         if (ret)
340                 goto out;
341
342         ret = -EFAULT;
343         if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
344                 goto out;
345         if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
346                 goto out;
347         ret = 0;
348
349  out:
350         kfree(data);
351         return ret;
352 }
353
354 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
355 {
356         struct ethtool_eeprom eeprom;
357         const struct ethtool_ops *ops = dev->ethtool_ops;
358         u8 *data;
359         int ret;
360
361         if (!ops->set_eeprom || !ops->get_eeprom_len)
362                 return -EOPNOTSUPP;
363
364         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
365                 return -EFAULT;
366
367         /* Check for wrap and zero */
368         if (eeprom.offset + eeprom.len <= eeprom.offset)
369                 return -EINVAL;
370
371         /* Check for exceeding total eeprom len */
372         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
373                 return -EINVAL;
374
375         data = kmalloc(eeprom.len, GFP_USER);
376         if (!data)
377                 return -ENOMEM;
378
379         ret = -EFAULT;
380         if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
381                 goto out;
382
383         ret = ops->set_eeprom(dev, &eeprom, data);
384         if (ret)
385                 goto out;
386
387         if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
388                 ret = -EFAULT;
389
390  out:
391         kfree(data);
392         return ret;
393 }
394
395 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
396 {
397         struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
398
399         if (!dev->ethtool_ops->get_coalesce)
400                 return -EOPNOTSUPP;
401
402         dev->ethtool_ops->get_coalesce(dev, &coalesce);
403
404         if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
405                 return -EFAULT;
406         return 0;
407 }
408
409 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
410 {
411         struct ethtool_coalesce coalesce;
412
413         if (!dev->ethtool_ops->set_coalesce)
414                 return -EOPNOTSUPP;
415
416         if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
417                 return -EFAULT;
418
419         return dev->ethtool_ops->set_coalesce(dev, &coalesce);
420 }
421
422 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
423 {
424         struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
425
426         if (!dev->ethtool_ops->get_ringparam)
427                 return -EOPNOTSUPP;
428
429         dev->ethtool_ops->get_ringparam(dev, &ringparam);
430
431         if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
432                 return -EFAULT;
433         return 0;
434 }
435
436 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
437 {
438         struct ethtool_ringparam ringparam;
439
440         if (!dev->ethtool_ops->set_ringparam)
441                 return -EOPNOTSUPP;
442
443         if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
444                 return -EFAULT;
445
446         return dev->ethtool_ops->set_ringparam(dev, &ringparam);
447 }
448
449 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
450 {
451         struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
452
453         if (!dev->ethtool_ops->get_pauseparam)
454                 return -EOPNOTSUPP;
455
456         dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
457
458         if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
459                 return -EFAULT;
460         return 0;
461 }
462
463 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
464 {
465         struct ethtool_pauseparam pauseparam;
466
467         if (!dev->ethtool_ops->set_pauseparam)
468                 return -EOPNOTSUPP;
469
470         if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
471                 return -EFAULT;
472
473         return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
474 }
475
476 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
477 {
478         struct ethtool_value edata = { ETHTOOL_GRXCSUM };
479
480         if (!dev->ethtool_ops->get_rx_csum)
481                 return -EOPNOTSUPP;
482
483         edata.data = dev->ethtool_ops->get_rx_csum(dev);
484
485         if (copy_to_user(useraddr, &edata, sizeof(edata)))
486                 return -EFAULT;
487         return 0;
488 }
489
490 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
491 {
492         struct ethtool_value edata;
493
494         if (!dev->ethtool_ops->set_rx_csum)
495                 return -EOPNOTSUPP;
496
497         if (copy_from_user(&edata, useraddr, sizeof(edata)))
498                 return -EFAULT;
499
500         dev->ethtool_ops->set_rx_csum(dev, edata.data);
501         return 0;
502 }
503
504 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
505 {
506         struct ethtool_value edata = { ETHTOOL_GTXCSUM };
507
508         if (!dev->ethtool_ops->get_tx_csum)
509                 return -EOPNOTSUPP;
510
511         edata.data = dev->ethtool_ops->get_tx_csum(dev);
512
513         if (copy_to_user(useraddr, &edata, sizeof(edata)))
514                 return -EFAULT;
515         return 0;
516 }
517
518 static int __ethtool_set_sg(struct net_device *dev, u32 data)
519 {
520         int err;
521
522         if (!data && dev->ethtool_ops->set_tso) {
523                 err = dev->ethtool_ops->set_tso(dev, 0);
524                 if (err)
525                         return err;
526         }
527
528         if (!data && dev->ethtool_ops->set_ufo) {
529                 err = dev->ethtool_ops->set_ufo(dev, 0);
530                 if (err)
531                         return err;
532         }
533         return dev->ethtool_ops->set_sg(dev, data);
534 }
535
536 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
537 {
538         struct ethtool_value edata;
539         int err;
540
541         if (!dev->ethtool_ops->set_tx_csum)
542                 return -EOPNOTSUPP;
543
544         if (copy_from_user(&edata, useraddr, sizeof(edata)))
545                 return -EFAULT;
546
547         if (!edata.data && dev->ethtool_ops->set_sg) {
548                 err = __ethtool_set_sg(dev, 0);
549                 if (err)
550                         return err;
551         }
552
553         return dev->ethtool_ops->set_tx_csum(dev, edata.data);
554 }
555
556 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
557 {
558         struct ethtool_value edata = { ETHTOOL_GSG };
559
560         if (!dev->ethtool_ops->get_sg)
561                 return -EOPNOTSUPP;
562
563         edata.data = dev->ethtool_ops->get_sg(dev);
564
565         if (copy_to_user(useraddr, &edata, sizeof(edata)))
566                 return -EFAULT;
567         return 0;
568 }
569
570 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
571 {
572         struct ethtool_value edata;
573
574         if (!dev->ethtool_ops->set_sg)
575                 return -EOPNOTSUPP;
576
577         if (copy_from_user(&edata, useraddr, sizeof(edata)))
578                 return -EFAULT;
579
580         if (edata.data &&
581             !(dev->features & NETIF_F_ALL_CSUM))
582                 return -EINVAL;
583
584         return __ethtool_set_sg(dev, edata.data);
585 }
586
587 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
588 {
589         struct ethtool_value edata = { ETHTOOL_GTSO };
590
591         if (!dev->ethtool_ops->get_tso)
592                 return -EOPNOTSUPP;
593
594         edata.data = dev->ethtool_ops->get_tso(dev);
595
596         if (copy_to_user(useraddr, &edata, sizeof(edata)))
597                 return -EFAULT;
598         return 0;
599 }
600
601 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
602 {
603         struct ethtool_value edata;
604
605         if (!dev->ethtool_ops->set_tso)
606                 return -EOPNOTSUPP;
607
608         if (copy_from_user(&edata, useraddr, sizeof(edata)))
609                 return -EFAULT;
610
611         if (edata.data && !(dev->features & NETIF_F_SG))
612                 return -EINVAL;
613
614         return dev->ethtool_ops->set_tso(dev, edata.data);
615 }
616
617 static int ethtool_get_ufo(struct net_device *dev, char __user *useraddr)
618 {
619         struct ethtool_value edata = { ETHTOOL_GUFO };
620
621         if (!dev->ethtool_ops->get_ufo)
622                 return -EOPNOTSUPP;
623         edata.data = dev->ethtool_ops->get_ufo(dev);
624         if (copy_to_user(useraddr, &edata, sizeof(edata)))
625                  return -EFAULT;
626         return 0;
627 }
628
629 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
630 {
631         struct ethtool_value edata;
632
633         if (!dev->ethtool_ops->set_ufo)
634                 return -EOPNOTSUPP;
635         if (copy_from_user(&edata, useraddr, sizeof(edata)))
636                 return -EFAULT;
637         if (edata.data && !(dev->features & NETIF_F_SG))
638                 return -EINVAL;
639         if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
640                 return -EINVAL;
641         return dev->ethtool_ops->set_ufo(dev, edata.data);
642 }
643
644 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
645 {
646         struct ethtool_value edata = { ETHTOOL_GGSO };
647
648         edata.data = dev->features & NETIF_F_GSO;
649         if (copy_to_user(useraddr, &edata, sizeof(edata)))
650                  return -EFAULT;
651         return 0;
652 }
653
654 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
655 {
656         struct ethtool_value edata;
657
658         if (copy_from_user(&edata, useraddr, sizeof(edata)))
659                 return -EFAULT;
660         if (edata.data)
661                 dev->features |= NETIF_F_GSO;
662         else
663                 dev->features &= ~NETIF_F_GSO;
664         return 0;
665 }
666
667 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
668 {
669         struct ethtool_test test;
670         const struct ethtool_ops *ops = dev->ethtool_ops;
671         u64 *data;
672         int ret;
673
674         if (!ops->self_test || !ops->self_test_count)
675                 return -EOPNOTSUPP;
676
677         if (copy_from_user(&test, useraddr, sizeof(test)))
678                 return -EFAULT;
679
680         test.len = ops->self_test_count(dev);
681         data = kmalloc(test.len * sizeof(u64), GFP_USER);
682         if (!data)
683                 return -ENOMEM;
684
685         ops->self_test(dev, &test, data);
686
687         ret = -EFAULT;
688         if (copy_to_user(useraddr, &test, sizeof(test)))
689                 goto out;
690         useraddr += sizeof(test);
691         if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
692                 goto out;
693         ret = 0;
694
695  out:
696         kfree(data);
697         return ret;
698 }
699
700 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
701 {
702         struct ethtool_gstrings gstrings;
703         const struct ethtool_ops *ops = dev->ethtool_ops;
704         u8 *data;
705         int ret;
706
707         if (!ops->get_strings)
708                 return -EOPNOTSUPP;
709
710         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
711                 return -EFAULT;
712
713         switch (gstrings.string_set) {
714         case ETH_SS_TEST:
715                 if (!ops->self_test_count)
716                         return -EOPNOTSUPP;
717                 gstrings.len = ops->self_test_count(dev);
718                 break;
719         case ETH_SS_STATS:
720                 if (!ops->get_stats_count)
721                         return -EOPNOTSUPP;
722                 gstrings.len = ops->get_stats_count(dev);
723                 break;
724         default:
725                 return -EINVAL;
726         }
727
728         data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
729         if (!data)
730                 return -ENOMEM;
731
732         ops->get_strings(dev, gstrings.string_set, data);
733
734         ret = -EFAULT;
735         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
736                 goto out;
737         useraddr += sizeof(gstrings);
738         if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
739                 goto out;
740         ret = 0;
741
742  out:
743         kfree(data);
744         return ret;
745 }
746
747 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
748 {
749         struct ethtool_value id;
750
751         if (!dev->ethtool_ops->phys_id)
752                 return -EOPNOTSUPP;
753
754         if (copy_from_user(&id, useraddr, sizeof(id)))
755                 return -EFAULT;
756
757         return dev->ethtool_ops->phys_id(dev, id.data);
758 }
759
760 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
761 {
762         struct ethtool_stats stats;
763         const struct ethtool_ops *ops = dev->ethtool_ops;
764         u64 *data;
765         int ret;
766
767         if (!ops->get_ethtool_stats || !ops->get_stats_count)
768                 return -EOPNOTSUPP;
769
770         if (copy_from_user(&stats, useraddr, sizeof(stats)))
771                 return -EFAULT;
772
773         stats.n_stats = ops->get_stats_count(dev);
774         data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
775         if (!data)
776                 return -ENOMEM;
777
778         ops->get_ethtool_stats(dev, &stats, data);
779
780         ret = -EFAULT;
781         if (copy_to_user(useraddr, &stats, sizeof(stats)))
782                 goto out;
783         useraddr += sizeof(stats);
784         if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
785                 goto out;
786         ret = 0;
787
788  out:
789         kfree(data);
790         return ret;
791 }
792
793 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
794 {
795         struct ethtool_perm_addr epaddr;
796
797         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
798                 return -EFAULT;
799
800         if (epaddr.size < dev->addr_len)
801                 return -ETOOSMALL;
802         epaddr.size = dev->addr_len;
803
804         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
805                 return -EFAULT;
806         useraddr += sizeof(epaddr);
807         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
808                 return -EFAULT;
809         return 0;
810 }
811
812 static int ethtool_get_flags(struct net_device *dev, char __user *useraddr)
813 {
814         struct ethtool_value edata = { ETHTOOL_GFLAGS };
815
816         if (!dev->ethtool_ops->get_flags)
817                 return -EOPNOTSUPP;
818
819         edata.data = dev->ethtool_ops->get_flags(dev);
820
821         if (copy_to_user(useraddr, &edata, sizeof(edata)))
822                 return -EFAULT;
823         return 0;
824 }
825
826 static int ethtool_set_flags(struct net_device *dev, char __user *useraddr)
827 {
828         struct ethtool_value edata;
829
830         if (!dev->ethtool_ops->set_flags)
831                 return -EOPNOTSUPP;
832
833         if (copy_from_user(&edata, useraddr, sizeof(edata)))
834                 return -EFAULT;
835
836         return dev->ethtool_ops->set_flags(dev, edata.data);
837 }
838
839 /* The main entry point in this file.  Called from net/core/dev.c */
840
841 int dev_ethtool(struct ifreq *ifr)
842 {
843         struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
844         void __user *useraddr = ifr->ifr_data;
845         u32 ethcmd;
846         int rc;
847         unsigned long old_features;
848
849         if (!dev || !netif_device_present(dev))
850                 return -ENODEV;
851
852         if (!dev->ethtool_ops)
853                 return -EOPNOTSUPP;
854
855         if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
856                 return -EFAULT;
857
858         /* Allow some commands to be done by anyone */
859         switch(ethcmd) {
860         case ETHTOOL_GDRVINFO:
861         case ETHTOOL_GMSGLVL:
862         case ETHTOOL_GCOALESCE:
863         case ETHTOOL_GRINGPARAM:
864         case ETHTOOL_GPAUSEPARAM:
865         case ETHTOOL_GRXCSUM:
866         case ETHTOOL_GTXCSUM:
867         case ETHTOOL_GSG:
868         case ETHTOOL_GSTRINGS:
869         case ETHTOOL_GTSO:
870         case ETHTOOL_GPERMADDR:
871         case ETHTOOL_GUFO:
872         case ETHTOOL_GGSO:
873                 break;
874         default:
875                 if (!capable(CAP_NET_ADMIN))
876                         return -EPERM;
877         }
878
879         if (dev->ethtool_ops->begin)
880                 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
881                         return rc;
882
883         old_features = dev->features;
884
885         switch (ethcmd) {
886         case ETHTOOL_GSET:
887                 rc = ethtool_get_settings(dev, useraddr);
888                 break;
889         case ETHTOOL_SSET:
890                 rc = ethtool_set_settings(dev, useraddr);
891                 break;
892         case ETHTOOL_GDRVINFO:
893                 rc = ethtool_get_drvinfo(dev, useraddr);
894                 break;
895         case ETHTOOL_GREGS:
896                 rc = ethtool_get_regs(dev, useraddr);
897                 break;
898         case ETHTOOL_GWOL:
899                 rc = ethtool_get_wol(dev, useraddr);
900                 break;
901         case ETHTOOL_SWOL:
902                 rc = ethtool_set_wol(dev, useraddr);
903                 break;
904         case ETHTOOL_GMSGLVL:
905                 rc = ethtool_get_msglevel(dev, useraddr);
906                 break;
907         case ETHTOOL_SMSGLVL:
908                 rc = ethtool_set_msglevel(dev, useraddr);
909                 break;
910         case ETHTOOL_NWAY_RST:
911                 rc = ethtool_nway_reset(dev);
912                 break;
913         case ETHTOOL_GLINK:
914                 rc = ethtool_get_link(dev, useraddr);
915                 break;
916         case ETHTOOL_GEEPROM:
917                 rc = ethtool_get_eeprom(dev, useraddr);
918                 break;
919         case ETHTOOL_SEEPROM:
920                 rc = ethtool_set_eeprom(dev, useraddr);
921                 break;
922         case ETHTOOL_GCOALESCE:
923                 rc = ethtool_get_coalesce(dev, useraddr);
924                 break;
925         case ETHTOOL_SCOALESCE:
926                 rc = ethtool_set_coalesce(dev, useraddr);
927                 break;
928         case ETHTOOL_GRINGPARAM:
929                 rc = ethtool_get_ringparam(dev, useraddr);
930                 break;
931         case ETHTOOL_SRINGPARAM:
932                 rc = ethtool_set_ringparam(dev, useraddr);
933                 break;
934         case ETHTOOL_GPAUSEPARAM:
935                 rc = ethtool_get_pauseparam(dev, useraddr);
936                 break;
937         case ETHTOOL_SPAUSEPARAM:
938                 rc = ethtool_set_pauseparam(dev, useraddr);
939                 break;
940         case ETHTOOL_GRXCSUM:
941                 rc = ethtool_get_rx_csum(dev, useraddr);
942                 break;
943         case ETHTOOL_SRXCSUM:
944                 rc = ethtool_set_rx_csum(dev, useraddr);
945                 break;
946         case ETHTOOL_GTXCSUM:
947                 rc = ethtool_get_tx_csum(dev, useraddr);
948                 break;
949         case ETHTOOL_STXCSUM:
950                 rc = ethtool_set_tx_csum(dev, useraddr);
951                 break;
952         case ETHTOOL_GSG:
953                 rc = ethtool_get_sg(dev, useraddr);
954                 break;
955         case ETHTOOL_SSG:
956                 rc = ethtool_set_sg(dev, useraddr);
957                 break;
958         case ETHTOOL_GTSO:
959                 rc = ethtool_get_tso(dev, useraddr);
960                 break;
961         case ETHTOOL_STSO:
962                 rc = ethtool_set_tso(dev, useraddr);
963                 break;
964         case ETHTOOL_TEST:
965                 rc = ethtool_self_test(dev, useraddr);
966                 break;
967         case ETHTOOL_GSTRINGS:
968                 rc = ethtool_get_strings(dev, useraddr);
969                 break;
970         case ETHTOOL_PHYS_ID:
971                 rc = ethtool_phys_id(dev, useraddr);
972                 break;
973         case ETHTOOL_GSTATS:
974                 rc = ethtool_get_stats(dev, useraddr);
975                 break;
976         case ETHTOOL_GPERMADDR:
977                 rc = ethtool_get_perm_addr(dev, useraddr);
978                 break;
979         case ETHTOOL_GUFO:
980                 rc = ethtool_get_ufo(dev, useraddr);
981                 break;
982         case ETHTOOL_SUFO:
983                 rc = ethtool_set_ufo(dev, useraddr);
984                 break;
985         case ETHTOOL_GGSO:
986                 rc = ethtool_get_gso(dev, useraddr);
987                 break;
988         case ETHTOOL_SGSO:
989                 rc = ethtool_set_gso(dev, useraddr);
990                 break;
991         case ETHTOOL_GFLAGS:
992                 rc = ethtool_get_flags(dev, useraddr);
993                 break;
994         case ETHTOOL_SFLAGS:
995                 rc = ethtool_set_flags(dev, useraddr);
996                 break;
997         default:
998                 rc = -EOPNOTSUPP;
999         }
1000
1001         if (dev->ethtool_ops->complete)
1002                 dev->ethtool_ops->complete(dev);
1003
1004         if (old_features != dev->features)
1005                 netdev_features_change(dev);
1006
1007         return rc;
1008 }
1009
1010 EXPORT_SYMBOL(ethtool_op_get_link);
1011 EXPORT_SYMBOL(ethtool_op_get_sg);
1012 EXPORT_SYMBOL(ethtool_op_get_tso);
1013 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
1014 EXPORT_SYMBOL(ethtool_op_set_sg);
1015 EXPORT_SYMBOL(ethtool_op_set_tso);
1016 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1017 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
1018 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
1019 EXPORT_SYMBOL(ethtool_op_set_ufo);
1020 EXPORT_SYMBOL(ethtool_op_get_ufo);
1021 EXPORT_SYMBOL(ethtool_op_set_flags);
1022 EXPORT_SYMBOL(ethtool_op_get_flags);