ethtool: factorize get/set_one_feature
[pandora-kernel.git] / 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 <linux/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
24
25 /*
26  * Some useful ethtool_ops methods that're device independent.
27  * If we find that all drivers want to do the same thing here,
28  * we can turn these into dev_() function calls.
29  */
30
31 u32 ethtool_op_get_link(struct net_device *dev)
32 {
33         return netif_carrier_ok(dev) ? 1 : 0;
34 }
35 EXPORT_SYMBOL(ethtool_op_get_link);
36
37 u32 ethtool_op_get_rx_csum(struct net_device *dev)
38 {
39         return (dev->features & NETIF_F_ALL_CSUM) != 0;
40 }
41 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
42
43 u32 ethtool_op_get_tx_csum(struct net_device *dev)
44 {
45         return (dev->features & NETIF_F_ALL_CSUM) != 0;
46 }
47 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
48
49 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
50 {
51         if (data)
52                 dev->features |= NETIF_F_IP_CSUM;
53         else
54                 dev->features &= ~NETIF_F_IP_CSUM;
55
56         return 0;
57 }
58 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
59
60 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
61 {
62         if (data)
63                 dev->features |= NETIF_F_HW_CSUM;
64         else
65                 dev->features &= ~NETIF_F_HW_CSUM;
66
67         return 0;
68 }
69 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
70
71 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
72 {
73         if (data)
74                 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
75         else
76                 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
77
78         return 0;
79 }
80 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
81
82 u32 ethtool_op_get_sg(struct net_device *dev)
83 {
84         return (dev->features & NETIF_F_SG) != 0;
85 }
86 EXPORT_SYMBOL(ethtool_op_get_sg);
87
88 int ethtool_op_set_sg(struct net_device *dev, u32 data)
89 {
90         if (data)
91                 dev->features |= NETIF_F_SG;
92         else
93                 dev->features &= ~NETIF_F_SG;
94
95         return 0;
96 }
97 EXPORT_SYMBOL(ethtool_op_set_sg);
98
99 u32 ethtool_op_get_tso(struct net_device *dev)
100 {
101         return (dev->features & NETIF_F_TSO) != 0;
102 }
103 EXPORT_SYMBOL(ethtool_op_get_tso);
104
105 int ethtool_op_set_tso(struct net_device *dev, u32 data)
106 {
107         if (data)
108                 dev->features |= NETIF_F_TSO;
109         else
110                 dev->features &= ~NETIF_F_TSO;
111
112         return 0;
113 }
114 EXPORT_SYMBOL(ethtool_op_set_tso);
115
116 u32 ethtool_op_get_ufo(struct net_device *dev)
117 {
118         return (dev->features & NETIF_F_UFO) != 0;
119 }
120 EXPORT_SYMBOL(ethtool_op_get_ufo);
121
122 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
123 {
124         if (data)
125                 dev->features |= NETIF_F_UFO;
126         else
127                 dev->features &= ~NETIF_F_UFO;
128         return 0;
129 }
130 EXPORT_SYMBOL(ethtool_op_set_ufo);
131
132 /* the following list of flags are the same as their associated
133  * NETIF_F_xxx values in include/linux/netdevice.h
134  */
135 static const u32 flags_dup_features =
136         (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
137          ETH_FLAG_RXHASH);
138
139 u32 ethtool_op_get_flags(struct net_device *dev)
140 {
141         /* in the future, this function will probably contain additional
142          * handling for flags which are not so easily handled
143          * by a simple masking operation
144          */
145
146         return dev->features & flags_dup_features;
147 }
148 EXPORT_SYMBOL(ethtool_op_get_flags);
149
150 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
151 {
152         if (data & ~supported)
153                 return -EINVAL;
154
155         dev->features = ((dev->features & ~flags_dup_features) |
156                          (data & flags_dup_features));
157         return 0;
158 }
159 EXPORT_SYMBOL(ethtool_op_set_flags);
160
161 void ethtool_ntuple_flush(struct net_device *dev)
162 {
163         struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
164
165         list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
166                 list_del(&fsc->list);
167                 kfree(fsc);
168         }
169         dev->ethtool_ntuple_list.count = 0;
170 }
171 EXPORT_SYMBOL(ethtool_ntuple_flush);
172
173 /* Handlers for each ethtool command */
174
175 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
176 {
177         const struct ethtool_ops *ops = dev->ethtool_ops;
178
179         if (ops && ops->get_sset_count && ops->get_strings)
180                 return ops->get_sset_count(dev, sset);
181         else
182                 return -EOPNOTSUPP;
183 }
184
185 static void __ethtool_get_strings(struct net_device *dev,
186         u32 stringset, u8 *data)
187 {
188         const struct ethtool_ops *ops = dev->ethtool_ops;
189
190         /* ops->get_strings is valid because checked earlier */
191         ops->get_strings(dev, stringset, data);
192 }
193
194 static u32 ethtool_get_feature_mask(u32 eth_cmd)
195 {
196         /* feature masks of legacy discrete ethtool ops */
197
198         switch (eth_cmd) {
199         case ETHTOOL_GTXCSUM:
200         case ETHTOOL_STXCSUM:
201                 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
202         case ETHTOOL_GSG:
203         case ETHTOOL_SSG:
204                 return NETIF_F_SG;
205         case ETHTOOL_GTSO:
206         case ETHTOOL_STSO:
207                 return NETIF_F_ALL_TSO;
208         case ETHTOOL_GUFO:
209         case ETHTOOL_SUFO:
210                 return NETIF_F_UFO;
211         case ETHTOOL_GGSO:
212         case ETHTOOL_SGSO:
213                 return NETIF_F_GSO;
214         case ETHTOOL_GGRO:
215         case ETHTOOL_SGRO:
216                 return NETIF_F_GRO;
217         default:
218                 BUG();
219         }
220 }
221
222 static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
223 {
224         const struct ethtool_ops *ops = dev->ethtool_ops;
225
226         if (!ops)
227                 return NULL;
228
229         switch (ethcmd) {
230         case ETHTOOL_GTXCSUM:
231                 return ops->get_tx_csum;
232         case ETHTOOL_SSG:
233                 return ops->get_sg;
234         case ETHTOOL_STSO:
235                 return ops->get_tso;
236         case ETHTOOL_SUFO:
237                 return ops->get_ufo;
238         default:
239                 return NULL;
240         }
241 }
242
243 static int ethtool_get_one_feature(struct net_device *dev,
244         char __user *useraddr, u32 ethcmd)
245 {
246         struct ethtool_value edata = {
247                 .cmd = ethcmd,
248                 .data = !!(dev->features & ethtool_get_feature_mask(ethcmd)),
249         };
250         u32 (*actor)(struct net_device *);
251
252         actor = __ethtool_get_one_feature_actor(dev, ethcmd);
253         if (actor)
254                 edata.data = actor(dev);
255
256         if (copy_to_user(useraddr, &edata, sizeof(edata)))
257                 return -EFAULT;
258         return 0;
259 }
260
261 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
262 static int __ethtool_set_sg(struct net_device *dev, u32 data);
263 static int __ethtool_set_tso(struct net_device *dev, u32 data);
264 static int __ethtool_set_ufo(struct net_device *dev, u32 data);
265
266 static int ethtool_set_one_feature(struct net_device *dev,
267         void __user *useraddr, u32 ethcmd)
268 {
269         struct ethtool_value edata;
270         u32 mask;
271
272         if (copy_from_user(&edata, useraddr, sizeof(edata)))
273                 return -EFAULT;
274
275         switch (ethcmd) {
276         case ETHTOOL_STXCSUM:
277                 return __ethtool_set_tx_csum(dev, edata.data);
278         case ETHTOOL_SSG:
279                 return __ethtool_set_sg(dev, edata.data);
280         case ETHTOOL_STSO:
281                 return __ethtool_set_tso(dev, edata.data);
282         case ETHTOOL_SUFO:
283                 return __ethtool_set_ufo(dev, edata.data);
284         case ETHTOOL_SGSO:
285         case ETHTOOL_SGRO:
286                 mask = ethtool_get_feature_mask(ethcmd);
287                 if (edata.data)
288                         dev->features |= mask;
289                 else
290                         dev->features &= ~mask;
291                 return 0;
292         default:
293                 return -EOPNOTSUPP;
294         }
295 }
296
297 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
298 {
299         struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
300         int err;
301
302         if (!dev->ethtool_ops->get_settings)
303                 return -EOPNOTSUPP;
304
305         err = dev->ethtool_ops->get_settings(dev, &cmd);
306         if (err < 0)
307                 return err;
308
309         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
310                 return -EFAULT;
311         return 0;
312 }
313
314 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
315 {
316         struct ethtool_cmd cmd;
317
318         if (!dev->ethtool_ops->set_settings)
319                 return -EOPNOTSUPP;
320
321         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
322                 return -EFAULT;
323
324         return dev->ethtool_ops->set_settings(dev, &cmd);
325 }
326
327 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
328                                                   void __user *useraddr)
329 {
330         struct ethtool_drvinfo info;
331         const struct ethtool_ops *ops = dev->ethtool_ops;
332
333         memset(&info, 0, sizeof(info));
334         info.cmd = ETHTOOL_GDRVINFO;
335         if (ops && ops->get_drvinfo) {
336                 ops->get_drvinfo(dev, &info);
337         } else if (dev->dev.parent && dev->dev.parent->driver) {
338                 strlcpy(info.bus_info, dev_name(dev->dev.parent),
339                         sizeof(info.bus_info));
340                 strlcpy(info.driver, dev->dev.parent->driver->name,
341                         sizeof(info.driver));
342         } else {
343                 return -EOPNOTSUPP;
344         }
345
346         /*
347          * this method of obtaining string set info is deprecated;
348          * Use ETHTOOL_GSSET_INFO instead.
349          */
350         if (ops && ops->get_sset_count) {
351                 int rc;
352
353                 rc = ops->get_sset_count(dev, ETH_SS_TEST);
354                 if (rc >= 0)
355                         info.testinfo_len = rc;
356                 rc = ops->get_sset_count(dev, ETH_SS_STATS);
357                 if (rc >= 0)
358                         info.n_stats = rc;
359                 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
360                 if (rc >= 0)
361                         info.n_priv_flags = rc;
362         }
363         if (ops && ops->get_regs_len)
364                 info.regdump_len = ops->get_regs_len(dev);
365         if (ops && ops->get_eeprom_len)
366                 info.eedump_len = ops->get_eeprom_len(dev);
367
368         if (copy_to_user(useraddr, &info, sizeof(info)))
369                 return -EFAULT;
370         return 0;
371 }
372
373 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
374                                                     void __user *useraddr)
375 {
376         struct ethtool_sset_info info;
377         u64 sset_mask;
378         int i, idx = 0, n_bits = 0, ret, rc;
379         u32 *info_buf = NULL;
380
381         if (copy_from_user(&info, useraddr, sizeof(info)))
382                 return -EFAULT;
383
384         /* store copy of mask, because we zero struct later on */
385         sset_mask = info.sset_mask;
386         if (!sset_mask)
387                 return 0;
388
389         /* calculate size of return buffer */
390         n_bits = hweight64(sset_mask);
391
392         memset(&info, 0, sizeof(info));
393         info.cmd = ETHTOOL_GSSET_INFO;
394
395         info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
396         if (!info_buf)
397                 return -ENOMEM;
398
399         /*
400          * fill return buffer based on input bitmask and successful
401          * get_sset_count return
402          */
403         for (i = 0; i < 64; i++) {
404                 if (!(sset_mask & (1ULL << i)))
405                         continue;
406
407                 rc = __ethtool_get_sset_count(dev, i);
408                 if (rc >= 0) {
409                         info.sset_mask |= (1ULL << i);
410                         info_buf[idx++] = rc;
411                 }
412         }
413
414         ret = -EFAULT;
415         if (copy_to_user(useraddr, &info, sizeof(info)))
416                 goto out;
417
418         useraddr += offsetof(struct ethtool_sset_info, data);
419         if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
420                 goto out;
421
422         ret = 0;
423
424 out:
425         kfree(info_buf);
426         return ret;
427 }
428
429 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
430                                                 u32 cmd, void __user *useraddr)
431 {
432         struct ethtool_rxnfc info;
433         size_t info_size = sizeof(info);
434
435         if (!dev->ethtool_ops->set_rxnfc)
436                 return -EOPNOTSUPP;
437
438         /* struct ethtool_rxnfc was originally defined for
439          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
440          * members.  User-space might still be using that
441          * definition. */
442         if (cmd == ETHTOOL_SRXFH)
443                 info_size = (offsetof(struct ethtool_rxnfc, data) +
444                              sizeof(info.data));
445
446         if (copy_from_user(&info, useraddr, info_size))
447                 return -EFAULT;
448
449         return dev->ethtool_ops->set_rxnfc(dev, &info);
450 }
451
452 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
453                                                 u32 cmd, void __user *useraddr)
454 {
455         struct ethtool_rxnfc info;
456         size_t info_size = sizeof(info);
457         const struct ethtool_ops *ops = dev->ethtool_ops;
458         int ret;
459         void *rule_buf = NULL;
460
461         if (!ops->get_rxnfc)
462                 return -EOPNOTSUPP;
463
464         /* struct ethtool_rxnfc was originally defined for
465          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
466          * members.  User-space might still be using that
467          * definition. */
468         if (cmd == ETHTOOL_GRXFH)
469                 info_size = (offsetof(struct ethtool_rxnfc, data) +
470                              sizeof(info.data));
471
472         if (copy_from_user(&info, useraddr, info_size))
473                 return -EFAULT;
474
475         if (info.cmd == ETHTOOL_GRXCLSRLALL) {
476                 if (info.rule_cnt > 0) {
477                         if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
478                                 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
479                                                    GFP_USER);
480                         if (!rule_buf)
481                                 return -ENOMEM;
482                 }
483         }
484
485         ret = ops->get_rxnfc(dev, &info, rule_buf);
486         if (ret < 0)
487                 goto err_out;
488
489         ret = -EFAULT;
490         if (copy_to_user(useraddr, &info, info_size))
491                 goto err_out;
492
493         if (rule_buf) {
494                 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
495                 if (copy_to_user(useraddr, rule_buf,
496                                  info.rule_cnt * sizeof(u32)))
497                         goto err_out;
498         }
499         ret = 0;
500
501 err_out:
502         kfree(rule_buf);
503
504         return ret;
505 }
506
507 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
508                                                      void __user *useraddr)
509 {
510         struct ethtool_rxfh_indir *indir;
511         u32 table_size;
512         size_t full_size;
513         int ret;
514
515         if (!dev->ethtool_ops->get_rxfh_indir)
516                 return -EOPNOTSUPP;
517
518         if (copy_from_user(&table_size,
519                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
520                            sizeof(table_size)))
521                 return -EFAULT;
522
523         if (table_size >
524             (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
525                 return -ENOMEM;
526         full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
527         indir = kzalloc(full_size, GFP_USER);
528         if (!indir)
529                 return -ENOMEM;
530
531         indir->cmd = ETHTOOL_GRXFHINDIR;
532         indir->size = table_size;
533         ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
534         if (ret)
535                 goto out;
536
537         if (copy_to_user(useraddr, indir, full_size))
538                 ret = -EFAULT;
539
540 out:
541         kfree(indir);
542         return ret;
543 }
544
545 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
546                                                      void __user *useraddr)
547 {
548         struct ethtool_rxfh_indir *indir;
549         u32 table_size;
550         size_t full_size;
551         int ret;
552
553         if (!dev->ethtool_ops->set_rxfh_indir)
554                 return -EOPNOTSUPP;
555
556         if (copy_from_user(&table_size,
557                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
558                            sizeof(table_size)))
559                 return -EFAULT;
560
561         if (table_size >
562             (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
563                 return -ENOMEM;
564         full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
565         indir = kmalloc(full_size, GFP_USER);
566         if (!indir)
567                 return -ENOMEM;
568
569         if (copy_from_user(indir, useraddr, full_size)) {
570                 ret = -EFAULT;
571                 goto out;
572         }
573
574         ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
575
576 out:
577         kfree(indir);
578         return ret;
579 }
580
581 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
582                         struct ethtool_rx_ntuple_flow_spec *spec,
583                         struct ethtool_rx_ntuple_flow_spec_container *fsc)
584 {
585
586         /* don't add filters forever */
587         if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
588                 /* free the container */
589                 kfree(fsc);
590                 return;
591         }
592
593         /* Copy the whole filter over */
594         fsc->fs.flow_type = spec->flow_type;
595         memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
596         memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
597
598         fsc->fs.vlan_tag = spec->vlan_tag;
599         fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
600         fsc->fs.data = spec->data;
601         fsc->fs.data_mask = spec->data_mask;
602         fsc->fs.action = spec->action;
603
604         /* add to the list */
605         list_add_tail_rcu(&fsc->list, &list->list);
606         list->count++;
607 }
608
609 /*
610  * ethtool does not (or did not) set masks for flow parameters that are
611  * not specified, so if both value and mask are 0 then this must be
612  * treated as equivalent to a mask with all bits set.  Implement that
613  * here rather than in drivers.
614  */
615 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
616 {
617         struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
618         struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
619
620         if (fs->flow_type != TCP_V4_FLOW &&
621             fs->flow_type != UDP_V4_FLOW &&
622             fs->flow_type != SCTP_V4_FLOW)
623                 return;
624
625         if (!(entry->ip4src | mask->ip4src))
626                 mask->ip4src = htonl(0xffffffff);
627         if (!(entry->ip4dst | mask->ip4dst))
628                 mask->ip4dst = htonl(0xffffffff);
629         if (!(entry->psrc | mask->psrc))
630                 mask->psrc = htons(0xffff);
631         if (!(entry->pdst | mask->pdst))
632                 mask->pdst = htons(0xffff);
633         if (!(entry->tos | mask->tos))
634                 mask->tos = 0xff;
635         if (!(fs->vlan_tag | fs->vlan_tag_mask))
636                 fs->vlan_tag_mask = 0xffff;
637         if (!(fs->data | fs->data_mask))
638                 fs->data_mask = 0xffffffffffffffffULL;
639 }
640
641 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
642                                                     void __user *useraddr)
643 {
644         struct ethtool_rx_ntuple cmd;
645         const struct ethtool_ops *ops = dev->ethtool_ops;
646         struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
647         int ret;
648
649         if (!(dev->features & NETIF_F_NTUPLE))
650                 return -EINVAL;
651
652         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
653                 return -EFAULT;
654
655         rx_ntuple_fix_masks(&cmd.fs);
656
657         /*
658          * Cache filter in dev struct for GET operation only if
659          * the underlying driver doesn't have its own GET operation, and
660          * only if the filter was added successfully.  First make sure we
661          * can allocate the filter, then continue if successful.
662          */
663         if (!ops->get_rx_ntuple) {
664                 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
665                 if (!fsc)
666                         return -ENOMEM;
667         }
668
669         ret = ops->set_rx_ntuple(dev, &cmd);
670         if (ret) {
671                 kfree(fsc);
672                 return ret;
673         }
674
675         if (!ops->get_rx_ntuple)
676                 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
677
678         return ret;
679 }
680
681 static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
682 {
683         struct ethtool_gstrings gstrings;
684         const struct ethtool_ops *ops = dev->ethtool_ops;
685         struct ethtool_rx_ntuple_flow_spec_container *fsc;
686         u8 *data;
687         char *p;
688         int ret, i, num_strings = 0;
689
690         if (!ops->get_sset_count)
691                 return -EOPNOTSUPP;
692
693         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
694                 return -EFAULT;
695
696         ret = ops->get_sset_count(dev, gstrings.string_set);
697         if (ret < 0)
698                 return ret;
699
700         gstrings.len = ret;
701
702         data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
703         if (!data)
704                 return -ENOMEM;
705
706         if (ops->get_rx_ntuple) {
707                 /* driver-specific filter grab */
708                 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
709                 goto copy;
710         }
711
712         /* default ethtool filter grab */
713         i = 0;
714         p = (char *)data;
715         list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
716                 sprintf(p, "Filter %d:\n", i);
717                 p += ETH_GSTRING_LEN;
718                 num_strings++;
719
720                 switch (fsc->fs.flow_type) {
721                 case TCP_V4_FLOW:
722                         sprintf(p, "\tFlow Type: TCP\n");
723                         p += ETH_GSTRING_LEN;
724                         num_strings++;
725                         break;
726                 case UDP_V4_FLOW:
727                         sprintf(p, "\tFlow Type: UDP\n");
728                         p += ETH_GSTRING_LEN;
729                         num_strings++;
730                         break;
731                 case SCTP_V4_FLOW:
732                         sprintf(p, "\tFlow Type: SCTP\n");
733                         p += ETH_GSTRING_LEN;
734                         num_strings++;
735                         break;
736                 case AH_ESP_V4_FLOW:
737                         sprintf(p, "\tFlow Type: AH ESP\n");
738                         p += ETH_GSTRING_LEN;
739                         num_strings++;
740                         break;
741                 case ESP_V4_FLOW:
742                         sprintf(p, "\tFlow Type: ESP\n");
743                         p += ETH_GSTRING_LEN;
744                         num_strings++;
745                         break;
746                 case IP_USER_FLOW:
747                         sprintf(p, "\tFlow Type: Raw IP\n");
748                         p += ETH_GSTRING_LEN;
749                         num_strings++;
750                         break;
751                 case IPV4_FLOW:
752                         sprintf(p, "\tFlow Type: IPv4\n");
753                         p += ETH_GSTRING_LEN;
754                         num_strings++;
755                         break;
756                 default:
757                         sprintf(p, "\tFlow Type: Unknown\n");
758                         p += ETH_GSTRING_LEN;
759                         num_strings++;
760                         goto unknown_filter;
761                 }
762
763                 /* now the rest of the filters */
764                 switch (fsc->fs.flow_type) {
765                 case TCP_V4_FLOW:
766                 case UDP_V4_FLOW:
767                 case SCTP_V4_FLOW:
768                         sprintf(p, "\tSrc IP addr: 0x%x\n",
769                                 fsc->fs.h_u.tcp_ip4_spec.ip4src);
770                         p += ETH_GSTRING_LEN;
771                         num_strings++;
772                         sprintf(p, "\tSrc IP mask: 0x%x\n",
773                                 fsc->fs.m_u.tcp_ip4_spec.ip4src);
774                         p += ETH_GSTRING_LEN;
775                         num_strings++;
776                         sprintf(p, "\tDest IP addr: 0x%x\n",
777                                 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
778                         p += ETH_GSTRING_LEN;
779                         num_strings++;
780                         sprintf(p, "\tDest IP mask: 0x%x\n",
781                                 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
782                         p += ETH_GSTRING_LEN;
783                         num_strings++;
784                         sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
785                                 fsc->fs.h_u.tcp_ip4_spec.psrc,
786                                 fsc->fs.m_u.tcp_ip4_spec.psrc);
787                         p += ETH_GSTRING_LEN;
788                         num_strings++;
789                         sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
790                                 fsc->fs.h_u.tcp_ip4_spec.pdst,
791                                 fsc->fs.m_u.tcp_ip4_spec.pdst);
792                         p += ETH_GSTRING_LEN;
793                         num_strings++;
794                         sprintf(p, "\tTOS: %d, mask: 0x%x\n",
795                                 fsc->fs.h_u.tcp_ip4_spec.tos,
796                                 fsc->fs.m_u.tcp_ip4_spec.tos);
797                         p += ETH_GSTRING_LEN;
798                         num_strings++;
799                         break;
800                 case AH_ESP_V4_FLOW:
801                 case ESP_V4_FLOW:
802                         sprintf(p, "\tSrc IP addr: 0x%x\n",
803                                 fsc->fs.h_u.ah_ip4_spec.ip4src);
804                         p += ETH_GSTRING_LEN;
805                         num_strings++;
806                         sprintf(p, "\tSrc IP mask: 0x%x\n",
807                                 fsc->fs.m_u.ah_ip4_spec.ip4src);
808                         p += ETH_GSTRING_LEN;
809                         num_strings++;
810                         sprintf(p, "\tDest IP addr: 0x%x\n",
811                                 fsc->fs.h_u.ah_ip4_spec.ip4dst);
812                         p += ETH_GSTRING_LEN;
813                         num_strings++;
814                         sprintf(p, "\tDest IP mask: 0x%x\n",
815                                 fsc->fs.m_u.ah_ip4_spec.ip4dst);
816                         p += ETH_GSTRING_LEN;
817                         num_strings++;
818                         sprintf(p, "\tSPI: %d, mask: 0x%x\n",
819                                 fsc->fs.h_u.ah_ip4_spec.spi,
820                                 fsc->fs.m_u.ah_ip4_spec.spi);
821                         p += ETH_GSTRING_LEN;
822                         num_strings++;
823                         sprintf(p, "\tTOS: %d, mask: 0x%x\n",
824                                 fsc->fs.h_u.ah_ip4_spec.tos,
825                                 fsc->fs.m_u.ah_ip4_spec.tos);
826                         p += ETH_GSTRING_LEN;
827                         num_strings++;
828                         break;
829                 case IP_USER_FLOW:
830                         sprintf(p, "\tSrc IP addr: 0x%x\n",
831                                 fsc->fs.h_u.usr_ip4_spec.ip4src);
832                         p += ETH_GSTRING_LEN;
833                         num_strings++;
834                         sprintf(p, "\tSrc IP mask: 0x%x\n",
835                                 fsc->fs.m_u.usr_ip4_spec.ip4src);
836                         p += ETH_GSTRING_LEN;
837                         num_strings++;
838                         sprintf(p, "\tDest IP addr: 0x%x\n",
839                                 fsc->fs.h_u.usr_ip4_spec.ip4dst);
840                         p += ETH_GSTRING_LEN;
841                         num_strings++;
842                         sprintf(p, "\tDest IP mask: 0x%x\n",
843                                 fsc->fs.m_u.usr_ip4_spec.ip4dst);
844                         p += ETH_GSTRING_LEN;
845                         num_strings++;
846                         break;
847                 case IPV4_FLOW:
848                         sprintf(p, "\tSrc IP addr: 0x%x\n",
849                                 fsc->fs.h_u.usr_ip4_spec.ip4src);
850                         p += ETH_GSTRING_LEN;
851                         num_strings++;
852                         sprintf(p, "\tSrc IP mask: 0x%x\n",
853                                 fsc->fs.m_u.usr_ip4_spec.ip4src);
854                         p += ETH_GSTRING_LEN;
855                         num_strings++;
856                         sprintf(p, "\tDest IP addr: 0x%x\n",
857                                 fsc->fs.h_u.usr_ip4_spec.ip4dst);
858                         p += ETH_GSTRING_LEN;
859                         num_strings++;
860                         sprintf(p, "\tDest IP mask: 0x%x\n",
861                                 fsc->fs.m_u.usr_ip4_spec.ip4dst);
862                         p += ETH_GSTRING_LEN;
863                         num_strings++;
864                         sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
865                                 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
866                                 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
867                         p += ETH_GSTRING_LEN;
868                         num_strings++;
869                         sprintf(p, "\tTOS: %d, mask: 0x%x\n",
870                                 fsc->fs.h_u.usr_ip4_spec.tos,
871                                 fsc->fs.m_u.usr_ip4_spec.tos);
872                         p += ETH_GSTRING_LEN;
873                         num_strings++;
874                         sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
875                                 fsc->fs.h_u.usr_ip4_spec.ip_ver,
876                                 fsc->fs.m_u.usr_ip4_spec.ip_ver);
877                         p += ETH_GSTRING_LEN;
878                         num_strings++;
879                         sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
880                                 fsc->fs.h_u.usr_ip4_spec.proto,
881                                 fsc->fs.m_u.usr_ip4_spec.proto);
882                         p += ETH_GSTRING_LEN;
883                         num_strings++;
884                         break;
885                 }
886                 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
887                         fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
888                 p += ETH_GSTRING_LEN;
889                 num_strings++;
890                 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
891                 p += ETH_GSTRING_LEN;
892                 num_strings++;
893                 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
894                 p += ETH_GSTRING_LEN;
895                 num_strings++;
896                 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
897                         sprintf(p, "\tAction: Drop\n");
898                 else
899                         sprintf(p, "\tAction: Direct to queue %d\n",
900                                 fsc->fs.action);
901                 p += ETH_GSTRING_LEN;
902                 num_strings++;
903 unknown_filter:
904                 i++;
905         }
906 copy:
907         /* indicate to userspace how many strings we actually have */
908         gstrings.len = num_strings;
909         ret = -EFAULT;
910         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
911                 goto out;
912         useraddr += sizeof(gstrings);
913         if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
914                 goto out;
915         ret = 0;
916
917 out:
918         kfree(data);
919         return ret;
920 }
921
922 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
923 {
924         struct ethtool_regs regs;
925         const struct ethtool_ops *ops = dev->ethtool_ops;
926         void *regbuf;
927         int reglen, ret;
928
929         if (!ops->get_regs || !ops->get_regs_len)
930                 return -EOPNOTSUPP;
931
932         if (copy_from_user(&regs, useraddr, sizeof(regs)))
933                 return -EFAULT;
934
935         reglen = ops->get_regs_len(dev);
936         if (regs.len > reglen)
937                 regs.len = reglen;
938
939         regbuf = vzalloc(reglen);
940         if (!regbuf)
941                 return -ENOMEM;
942
943         ops->get_regs(dev, &regs, regbuf);
944
945         ret = -EFAULT;
946         if (copy_to_user(useraddr, &regs, sizeof(regs)))
947                 goto out;
948         useraddr += offsetof(struct ethtool_regs, data);
949         if (copy_to_user(useraddr, regbuf, regs.len))
950                 goto out;
951         ret = 0;
952
953  out:
954         vfree(regbuf);
955         return ret;
956 }
957
958 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
959 {
960         struct ethtool_value reset;
961         int ret;
962
963         if (!dev->ethtool_ops->reset)
964                 return -EOPNOTSUPP;
965
966         if (copy_from_user(&reset, useraddr, sizeof(reset)))
967                 return -EFAULT;
968
969         ret = dev->ethtool_ops->reset(dev, &reset.data);
970         if (ret)
971                 return ret;
972
973         if (copy_to_user(useraddr, &reset, sizeof(reset)))
974                 return -EFAULT;
975         return 0;
976 }
977
978 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
979 {
980         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
981
982         if (!dev->ethtool_ops->get_wol)
983                 return -EOPNOTSUPP;
984
985         dev->ethtool_ops->get_wol(dev, &wol);
986
987         if (copy_to_user(useraddr, &wol, sizeof(wol)))
988                 return -EFAULT;
989         return 0;
990 }
991
992 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
993 {
994         struct ethtool_wolinfo wol;
995
996         if (!dev->ethtool_ops->set_wol)
997                 return -EOPNOTSUPP;
998
999         if (copy_from_user(&wol, useraddr, sizeof(wol)))
1000                 return -EFAULT;
1001
1002         return dev->ethtool_ops->set_wol(dev, &wol);
1003 }
1004
1005 static int ethtool_nway_reset(struct net_device *dev)
1006 {
1007         if (!dev->ethtool_ops->nway_reset)
1008                 return -EOPNOTSUPP;
1009
1010         return dev->ethtool_ops->nway_reset(dev);
1011 }
1012
1013 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1014 {
1015         struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1016
1017         if (!dev->ethtool_ops->get_link)
1018                 return -EOPNOTSUPP;
1019
1020         edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1021
1022         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1023                 return -EFAULT;
1024         return 0;
1025 }
1026
1027 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1028 {
1029         struct ethtool_eeprom eeprom;
1030         const struct ethtool_ops *ops = dev->ethtool_ops;
1031         void __user *userbuf = useraddr + sizeof(eeprom);
1032         u32 bytes_remaining;
1033         u8 *data;
1034         int ret = 0;
1035
1036         if (!ops->get_eeprom || !ops->get_eeprom_len)
1037                 return -EOPNOTSUPP;
1038
1039         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1040                 return -EFAULT;
1041
1042         /* Check for wrap and zero */
1043         if (eeprom.offset + eeprom.len <= eeprom.offset)
1044                 return -EINVAL;
1045
1046         /* Check for exceeding total eeprom len */
1047         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1048                 return -EINVAL;
1049
1050         data = kmalloc(PAGE_SIZE, GFP_USER);
1051         if (!data)
1052                 return -ENOMEM;
1053
1054         bytes_remaining = eeprom.len;
1055         while (bytes_remaining > 0) {
1056                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1057
1058                 ret = ops->get_eeprom(dev, &eeprom, data);
1059                 if (ret)
1060                         break;
1061                 if (copy_to_user(userbuf, data, eeprom.len)) {
1062                         ret = -EFAULT;
1063                         break;
1064                 }
1065                 userbuf += eeprom.len;
1066                 eeprom.offset += eeprom.len;
1067                 bytes_remaining -= eeprom.len;
1068         }
1069
1070         eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1071         eeprom.offset -= eeprom.len;
1072         if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1073                 ret = -EFAULT;
1074
1075         kfree(data);
1076         return ret;
1077 }
1078
1079 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1080 {
1081         struct ethtool_eeprom eeprom;
1082         const struct ethtool_ops *ops = dev->ethtool_ops;
1083         void __user *userbuf = useraddr + sizeof(eeprom);
1084         u32 bytes_remaining;
1085         u8 *data;
1086         int ret = 0;
1087
1088         if (!ops->set_eeprom || !ops->get_eeprom_len)
1089                 return -EOPNOTSUPP;
1090
1091         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1092                 return -EFAULT;
1093
1094         /* Check for wrap and zero */
1095         if (eeprom.offset + eeprom.len <= eeprom.offset)
1096                 return -EINVAL;
1097
1098         /* Check for exceeding total eeprom len */
1099         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1100                 return -EINVAL;
1101
1102         data = kmalloc(PAGE_SIZE, GFP_USER);
1103         if (!data)
1104                 return -ENOMEM;
1105
1106         bytes_remaining = eeprom.len;
1107         while (bytes_remaining > 0) {
1108                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1109
1110                 if (copy_from_user(data, userbuf, eeprom.len)) {
1111                         ret = -EFAULT;
1112                         break;
1113                 }
1114                 ret = ops->set_eeprom(dev, &eeprom, data);
1115                 if (ret)
1116                         break;
1117                 userbuf += eeprom.len;
1118                 eeprom.offset += eeprom.len;
1119                 bytes_remaining -= eeprom.len;
1120         }
1121
1122         kfree(data);
1123         return ret;
1124 }
1125
1126 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1127                                                    void __user *useraddr)
1128 {
1129         struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1130
1131         if (!dev->ethtool_ops->get_coalesce)
1132                 return -EOPNOTSUPP;
1133
1134         dev->ethtool_ops->get_coalesce(dev, &coalesce);
1135
1136         if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1137                 return -EFAULT;
1138         return 0;
1139 }
1140
1141 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1142                                                    void __user *useraddr)
1143 {
1144         struct ethtool_coalesce coalesce;
1145
1146         if (!dev->ethtool_ops->set_coalesce)
1147                 return -EOPNOTSUPP;
1148
1149         if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1150                 return -EFAULT;
1151
1152         return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1153 }
1154
1155 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1156 {
1157         struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1158
1159         if (!dev->ethtool_ops->get_ringparam)
1160                 return -EOPNOTSUPP;
1161
1162         dev->ethtool_ops->get_ringparam(dev, &ringparam);
1163
1164         if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1165                 return -EFAULT;
1166         return 0;
1167 }
1168
1169 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1170 {
1171         struct ethtool_ringparam ringparam;
1172
1173         if (!dev->ethtool_ops->set_ringparam)
1174                 return -EOPNOTSUPP;
1175
1176         if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1177                 return -EFAULT;
1178
1179         return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1180 }
1181
1182 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1183 {
1184         struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1185
1186         if (!dev->ethtool_ops->get_pauseparam)
1187                 return -EOPNOTSUPP;
1188
1189         dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1190
1191         if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1192                 return -EFAULT;
1193         return 0;
1194 }
1195
1196 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1197 {
1198         struct ethtool_pauseparam pauseparam;
1199
1200         if (!dev->ethtool_ops->set_pauseparam)
1201                 return -EOPNOTSUPP;
1202
1203         if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1204                 return -EFAULT;
1205
1206         return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1207 }
1208
1209 static int __ethtool_set_sg(struct net_device *dev, u32 data)
1210 {
1211         int err;
1212
1213         if (data && !(dev->features & NETIF_F_ALL_CSUM))
1214                 return -EINVAL;
1215
1216         if (!data && dev->ethtool_ops->set_tso) {
1217                 err = dev->ethtool_ops->set_tso(dev, 0);
1218                 if (err)
1219                         return err;
1220         }
1221
1222         if (!data && dev->ethtool_ops->set_ufo) {
1223                 err = dev->ethtool_ops->set_ufo(dev, 0);
1224                 if (err)
1225                         return err;
1226         }
1227         return dev->ethtool_ops->set_sg(dev, data);
1228 }
1229
1230 static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
1231 {
1232         int err;
1233
1234         if (!dev->ethtool_ops->set_tx_csum)
1235                 return -EOPNOTSUPP;
1236
1237         if (!data && dev->ethtool_ops->set_sg) {
1238                 err = __ethtool_set_sg(dev, 0);
1239                 if (err)
1240                         return err;
1241         }
1242
1243         return dev->ethtool_ops->set_tx_csum(dev, data);
1244 }
1245
1246 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1247 {
1248         struct ethtool_value edata;
1249
1250         if (!dev->ethtool_ops->set_rx_csum)
1251                 return -EOPNOTSUPP;
1252
1253         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1254                 return -EFAULT;
1255
1256         if (!edata.data && dev->ethtool_ops->set_sg)
1257                 dev->features &= ~NETIF_F_GRO;
1258
1259         return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1260 }
1261
1262 static int __ethtool_set_tso(struct net_device *dev, u32 data)
1263 {
1264         if (!dev->ethtool_ops->set_tso)
1265                 return -EOPNOTSUPP;
1266
1267         if (data && !(dev->features & NETIF_F_SG))
1268                 return -EINVAL;
1269
1270         return dev->ethtool_ops->set_tso(dev, data);
1271 }
1272
1273 static int __ethtool_set_ufo(struct net_device *dev, u32 data)
1274 {
1275         if (!dev->ethtool_ops->set_ufo)
1276                 return -EOPNOTSUPP;
1277         if (data && !(dev->features & NETIF_F_SG))
1278                 return -EINVAL;
1279         if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
1280                 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
1281                         == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
1282                 return -EINVAL;
1283         return dev->ethtool_ops->set_ufo(dev, data);
1284 }
1285
1286 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1287 {
1288         struct ethtool_test test;
1289         const struct ethtool_ops *ops = dev->ethtool_ops;
1290         u64 *data;
1291         int ret, test_len;
1292
1293         if (!ops->self_test || !ops->get_sset_count)
1294                 return -EOPNOTSUPP;
1295
1296         test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1297         if (test_len < 0)
1298                 return test_len;
1299         WARN_ON(test_len == 0);
1300
1301         if (copy_from_user(&test, useraddr, sizeof(test)))
1302                 return -EFAULT;
1303
1304         test.len = test_len;
1305         data = kmalloc(test_len * sizeof(u64), GFP_USER);
1306         if (!data)
1307                 return -ENOMEM;
1308
1309         ops->self_test(dev, &test, data);
1310
1311         ret = -EFAULT;
1312         if (copy_to_user(useraddr, &test, sizeof(test)))
1313                 goto out;
1314         useraddr += sizeof(test);
1315         if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1316                 goto out;
1317         ret = 0;
1318
1319  out:
1320         kfree(data);
1321         return ret;
1322 }
1323
1324 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1325 {
1326         struct ethtool_gstrings gstrings;
1327         u8 *data;
1328         int ret;
1329
1330         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1331                 return -EFAULT;
1332
1333         ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1334         if (ret < 0)
1335                 return ret;
1336
1337         gstrings.len = ret;
1338
1339         data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1340         if (!data)
1341                 return -ENOMEM;
1342
1343         __ethtool_get_strings(dev, gstrings.string_set, data);
1344
1345         ret = -EFAULT;
1346         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1347                 goto out;
1348         useraddr += sizeof(gstrings);
1349         if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1350                 goto out;
1351         ret = 0;
1352
1353 out:
1354         kfree(data);
1355         return ret;
1356 }
1357
1358 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1359 {
1360         struct ethtool_value id;
1361
1362         if (!dev->ethtool_ops->phys_id)
1363                 return -EOPNOTSUPP;
1364
1365         if (copy_from_user(&id, useraddr, sizeof(id)))
1366                 return -EFAULT;
1367
1368         return dev->ethtool_ops->phys_id(dev, id.data);
1369 }
1370
1371 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1372 {
1373         struct ethtool_stats stats;
1374         const struct ethtool_ops *ops = dev->ethtool_ops;
1375         u64 *data;
1376         int ret, n_stats;
1377
1378         if (!ops->get_ethtool_stats || !ops->get_sset_count)
1379                 return -EOPNOTSUPP;
1380
1381         n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1382         if (n_stats < 0)
1383                 return n_stats;
1384         WARN_ON(n_stats == 0);
1385
1386         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1387                 return -EFAULT;
1388
1389         stats.n_stats = n_stats;
1390         data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1391         if (!data)
1392                 return -ENOMEM;
1393
1394         ops->get_ethtool_stats(dev, &stats, data);
1395
1396         ret = -EFAULT;
1397         if (copy_to_user(useraddr, &stats, sizeof(stats)))
1398                 goto out;
1399         useraddr += sizeof(stats);
1400         if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1401                 goto out;
1402         ret = 0;
1403
1404  out:
1405         kfree(data);
1406         return ret;
1407 }
1408
1409 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1410 {
1411         struct ethtool_perm_addr epaddr;
1412
1413         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1414                 return -EFAULT;
1415
1416         if (epaddr.size < dev->addr_len)
1417                 return -ETOOSMALL;
1418         epaddr.size = dev->addr_len;
1419
1420         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1421                 return -EFAULT;
1422         useraddr += sizeof(epaddr);
1423         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1424                 return -EFAULT;
1425         return 0;
1426 }
1427
1428 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1429                              u32 cmd, u32 (*actor)(struct net_device *))
1430 {
1431         struct ethtool_value edata = { .cmd = cmd };
1432
1433         if (!actor)
1434                 return -EOPNOTSUPP;
1435
1436         edata.data = actor(dev);
1437
1438         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1439                 return -EFAULT;
1440         return 0;
1441 }
1442
1443 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1444                              void (*actor)(struct net_device *, u32))
1445 {
1446         struct ethtool_value edata;
1447
1448         if (!actor)
1449                 return -EOPNOTSUPP;
1450
1451         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1452                 return -EFAULT;
1453
1454         actor(dev, edata.data);
1455         return 0;
1456 }
1457
1458 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1459                              int (*actor)(struct net_device *, u32))
1460 {
1461         struct ethtool_value edata;
1462
1463         if (!actor)
1464                 return -EOPNOTSUPP;
1465
1466         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1467                 return -EFAULT;
1468
1469         return actor(dev, edata.data);
1470 }
1471
1472 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1473                                                    char __user *useraddr)
1474 {
1475         struct ethtool_flash efl;
1476
1477         if (copy_from_user(&efl, useraddr, sizeof(efl)))
1478                 return -EFAULT;
1479
1480         if (!dev->ethtool_ops->flash_device)
1481                 return -EOPNOTSUPP;
1482
1483         return dev->ethtool_ops->flash_device(dev, &efl);
1484 }
1485
1486 /* The main entry point in this file.  Called from net/core/dev.c */
1487
1488 int dev_ethtool(struct net *net, struct ifreq *ifr)
1489 {
1490         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1491         void __user *useraddr = ifr->ifr_data;
1492         u32 ethcmd;
1493         int rc;
1494         u32 old_features;
1495
1496         if (!dev || !netif_device_present(dev))
1497                 return -ENODEV;
1498
1499         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1500                 return -EFAULT;
1501
1502         if (!dev->ethtool_ops) {
1503                 /* ETHTOOL_GDRVINFO does not require any driver support.
1504                  * It is also unprivileged and does not change anything,
1505                  * so we can take a shortcut to it. */
1506                 if (ethcmd == ETHTOOL_GDRVINFO)
1507                         return ethtool_get_drvinfo(dev, useraddr);
1508                 else
1509                         return -EOPNOTSUPP;
1510         }
1511
1512         /* Allow some commands to be done by anyone */
1513         switch (ethcmd) {
1514         case ETHTOOL_GSET:
1515         case ETHTOOL_GDRVINFO:
1516         case ETHTOOL_GMSGLVL:
1517         case ETHTOOL_GCOALESCE:
1518         case ETHTOOL_GRINGPARAM:
1519         case ETHTOOL_GPAUSEPARAM:
1520         case ETHTOOL_GRXCSUM:
1521         case ETHTOOL_GTXCSUM:
1522         case ETHTOOL_GSG:
1523         case ETHTOOL_GSTRINGS:
1524         case ETHTOOL_GTSO:
1525         case ETHTOOL_GPERMADDR:
1526         case ETHTOOL_GUFO:
1527         case ETHTOOL_GGSO:
1528         case ETHTOOL_GGRO:
1529         case ETHTOOL_GFLAGS:
1530         case ETHTOOL_GPFLAGS:
1531         case ETHTOOL_GRXFH:
1532         case ETHTOOL_GRXRINGS:
1533         case ETHTOOL_GRXCLSRLCNT:
1534         case ETHTOOL_GRXCLSRULE:
1535         case ETHTOOL_GRXCLSRLALL:
1536                 break;
1537         default:
1538                 if (!capable(CAP_NET_ADMIN))
1539                         return -EPERM;
1540         }
1541
1542         if (dev->ethtool_ops->begin) {
1543                 rc = dev->ethtool_ops->begin(dev);
1544                 if (rc  < 0)
1545                         return rc;
1546         }
1547         old_features = dev->features;
1548
1549         switch (ethcmd) {
1550         case ETHTOOL_GSET:
1551                 rc = ethtool_get_settings(dev, useraddr);
1552                 break;
1553         case ETHTOOL_SSET:
1554                 rc = ethtool_set_settings(dev, useraddr);
1555                 break;
1556         case ETHTOOL_GDRVINFO:
1557                 rc = ethtool_get_drvinfo(dev, useraddr);
1558                 break;
1559         case ETHTOOL_GREGS:
1560                 rc = ethtool_get_regs(dev, useraddr);
1561                 break;
1562         case ETHTOOL_GWOL:
1563                 rc = ethtool_get_wol(dev, useraddr);
1564                 break;
1565         case ETHTOOL_SWOL:
1566                 rc = ethtool_set_wol(dev, useraddr);
1567                 break;
1568         case ETHTOOL_GMSGLVL:
1569                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1570                                        dev->ethtool_ops->get_msglevel);
1571                 break;
1572         case ETHTOOL_SMSGLVL:
1573                 rc = ethtool_set_value_void(dev, useraddr,
1574                                        dev->ethtool_ops->set_msglevel);
1575                 break;
1576         case ETHTOOL_NWAY_RST:
1577                 rc = ethtool_nway_reset(dev);
1578                 break;
1579         case ETHTOOL_GLINK:
1580                 rc = ethtool_get_link(dev, useraddr);
1581                 break;
1582         case ETHTOOL_GEEPROM:
1583                 rc = ethtool_get_eeprom(dev, useraddr);
1584                 break;
1585         case ETHTOOL_SEEPROM:
1586                 rc = ethtool_set_eeprom(dev, useraddr);
1587                 break;
1588         case ETHTOOL_GCOALESCE:
1589                 rc = ethtool_get_coalesce(dev, useraddr);
1590                 break;
1591         case ETHTOOL_SCOALESCE:
1592                 rc = ethtool_set_coalesce(dev, useraddr);
1593                 break;
1594         case ETHTOOL_GRINGPARAM:
1595                 rc = ethtool_get_ringparam(dev, useraddr);
1596                 break;
1597         case ETHTOOL_SRINGPARAM:
1598                 rc = ethtool_set_ringparam(dev, useraddr);
1599                 break;
1600         case ETHTOOL_GPAUSEPARAM:
1601                 rc = ethtool_get_pauseparam(dev, useraddr);
1602                 break;
1603         case ETHTOOL_SPAUSEPARAM:
1604                 rc = ethtool_set_pauseparam(dev, useraddr);
1605                 break;
1606         case ETHTOOL_GRXCSUM:
1607                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1608                                        (dev->ethtool_ops->get_rx_csum ?
1609                                         dev->ethtool_ops->get_rx_csum :
1610                                         ethtool_op_get_rx_csum));
1611                 break;
1612         case ETHTOOL_SRXCSUM:
1613                 rc = ethtool_set_rx_csum(dev, useraddr);
1614                 break;
1615         case ETHTOOL_TEST:
1616                 rc = ethtool_self_test(dev, useraddr);
1617                 break;
1618         case ETHTOOL_GSTRINGS:
1619                 rc = ethtool_get_strings(dev, useraddr);
1620                 break;
1621         case ETHTOOL_PHYS_ID:
1622                 rc = ethtool_phys_id(dev, useraddr);
1623                 break;
1624         case ETHTOOL_GSTATS:
1625                 rc = ethtool_get_stats(dev, useraddr);
1626                 break;
1627         case ETHTOOL_GPERMADDR:
1628                 rc = ethtool_get_perm_addr(dev, useraddr);
1629                 break;
1630         case ETHTOOL_GFLAGS:
1631                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1632                                        (dev->ethtool_ops->get_flags ?
1633                                         dev->ethtool_ops->get_flags :
1634                                         ethtool_op_get_flags));
1635                 break;
1636         case ETHTOOL_SFLAGS:
1637                 rc = ethtool_set_value(dev, useraddr,
1638                                        dev->ethtool_ops->set_flags);
1639                 break;
1640         case ETHTOOL_GPFLAGS:
1641                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1642                                        dev->ethtool_ops->get_priv_flags);
1643                 break;
1644         case ETHTOOL_SPFLAGS:
1645                 rc = ethtool_set_value(dev, useraddr,
1646                                        dev->ethtool_ops->set_priv_flags);
1647                 break;
1648         case ETHTOOL_GRXFH:
1649         case ETHTOOL_GRXRINGS:
1650         case ETHTOOL_GRXCLSRLCNT:
1651         case ETHTOOL_GRXCLSRULE:
1652         case ETHTOOL_GRXCLSRLALL:
1653                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1654                 break;
1655         case ETHTOOL_SRXFH:
1656         case ETHTOOL_SRXCLSRLDEL:
1657         case ETHTOOL_SRXCLSRLINS:
1658                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1659                 break;
1660         case ETHTOOL_FLASHDEV:
1661                 rc = ethtool_flash_device(dev, useraddr);
1662                 break;
1663         case ETHTOOL_RESET:
1664                 rc = ethtool_reset(dev, useraddr);
1665                 break;
1666         case ETHTOOL_SRXNTUPLE:
1667                 rc = ethtool_set_rx_ntuple(dev, useraddr);
1668                 break;
1669         case ETHTOOL_GRXNTUPLE:
1670                 rc = ethtool_get_rx_ntuple(dev, useraddr);
1671                 break;
1672         case ETHTOOL_GSSET_INFO:
1673                 rc = ethtool_get_sset_info(dev, useraddr);
1674                 break;
1675         case ETHTOOL_GRXFHINDIR:
1676                 rc = ethtool_get_rxfh_indir(dev, useraddr);
1677                 break;
1678         case ETHTOOL_SRXFHINDIR:
1679                 rc = ethtool_set_rxfh_indir(dev, useraddr);
1680                 break;
1681         case ETHTOOL_GTXCSUM:
1682         case ETHTOOL_GSG:
1683         case ETHTOOL_GTSO:
1684         case ETHTOOL_GUFO:
1685         case ETHTOOL_GGSO:
1686         case ETHTOOL_GGRO:
1687                 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1688                 break;
1689         case ETHTOOL_STXCSUM:
1690         case ETHTOOL_SSG:
1691         case ETHTOOL_STSO:
1692         case ETHTOOL_SUFO:
1693         case ETHTOOL_SGSO:
1694         case ETHTOOL_SGRO:
1695                 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1696                 break;
1697         default:
1698                 rc = -EOPNOTSUPP;
1699         }
1700
1701         if (dev->ethtool_ops->complete)
1702                 dev->ethtool_ops->complete(dev);
1703
1704         if (old_features != dev->features)
1705                 netdev_features_change(dev);
1706
1707         return rc;
1708 }