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