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