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