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