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