sch_sfq: use skb_flow_dissect()
[firefly-linux-kernel-4.4.55.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 #include <linux/rtnetlink.h>
25 #include <linux/sched.h>
26
27 /*
28  * Some useful ethtool_ops methods that're device independent.
29  * If we find that all drivers want to do the same thing here,
30  * we can turn these into dev_() function calls.
31  */
32
33 u32 ethtool_op_get_link(struct net_device *dev)
34 {
35         return netif_carrier_ok(dev) ? 1 : 0;
36 }
37 EXPORT_SYMBOL(ethtool_op_get_link);
38
39 /* Handlers for each ethtool command */
40
41 #define ETHTOOL_DEV_FEATURE_WORDS       ((NETDEV_FEATURE_COUNT + 31) / 32)
42
43 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
44         [NETIF_F_SG_BIT] =               "tx-scatter-gather",
45         [NETIF_F_IP_CSUM_BIT] =          "tx-checksum-ipv4",
46         [NETIF_F_HW_CSUM_BIT] =          "tx-checksum-ip-generic",
47         [NETIF_F_IPV6_CSUM_BIT] =        "tx-checksum-ipv6",
48         [NETIF_F_HIGHDMA_BIT] =          "highdma",
49         [NETIF_F_FRAGLIST_BIT] =         "tx-scatter-gather-fraglist",
50         [NETIF_F_HW_VLAN_TX_BIT] =       "tx-vlan-hw-insert",
51
52         [NETIF_F_HW_VLAN_RX_BIT] =       "rx-vlan-hw-parse",
53         [NETIF_F_HW_VLAN_FILTER_BIT] =   "rx-vlan-filter",
54         [NETIF_F_VLAN_CHALLENGED_BIT] =  "vlan-challenged",
55         [NETIF_F_GSO_BIT] =              "tx-generic-segmentation",
56         [NETIF_F_LLTX_BIT] =             "tx-lockless",
57         [NETIF_F_NETNS_LOCAL_BIT] =      "netns-local",
58         [NETIF_F_GRO_BIT] =              "rx-gro",
59         [NETIF_F_LRO_BIT] =              "rx-lro",
60
61         [NETIF_F_TSO_BIT] =              "tx-tcp-segmentation",
62         [NETIF_F_UFO_BIT] =              "tx-udp-fragmentation",
63         [NETIF_F_GSO_ROBUST_BIT] =       "tx-gso-robust",
64         [NETIF_F_TSO_ECN_BIT] =          "tx-tcp-ecn-segmentation",
65         [NETIF_F_TSO6_BIT] =             "tx-tcp6-segmentation",
66         [NETIF_F_FSO_BIT] =              "tx-fcoe-segmentation",
67
68         [NETIF_F_FCOE_CRC_BIT] =         "tx-checksum-fcoe-crc",
69         [NETIF_F_SCTP_CSUM_BIT] =        "tx-checksum-sctp",
70         [NETIF_F_FCOE_MTU_BIT] =         "fcoe-mtu",
71         [NETIF_F_NTUPLE_BIT] =           "rx-ntuple-filter",
72         [NETIF_F_RXHASH_BIT] =           "rx-hashing",
73         [NETIF_F_RXCSUM_BIT] =           "rx-checksum",
74         [NETIF_F_NOCACHE_COPY_BIT] =     "tx-nocache-copy",
75         [NETIF_F_LOOPBACK_BIT] =         "loopback",
76 };
77
78 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
79 {
80         struct ethtool_gfeatures cmd = {
81                 .cmd = ETHTOOL_GFEATURES,
82                 .size = ETHTOOL_DEV_FEATURE_WORDS,
83         };
84         struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
85         u32 __user *sizeaddr;
86         u32 copy_size;
87         int i;
88
89         /* in case feature bits run out again */
90         BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
91
92         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
93                 features[i].available = (u32)(dev->hw_features >> (32 * i));
94                 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
95                 features[i].active = (u32)(dev->features >> (32 * i));
96                 features[i].never_changed =
97                         (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
98         }
99
100         sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
101         if (get_user(copy_size, sizeaddr))
102                 return -EFAULT;
103
104         if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
105                 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
106
107         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
108                 return -EFAULT;
109         useraddr += sizeof(cmd);
110         if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
111                 return -EFAULT;
112
113         return 0;
114 }
115
116 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
117 {
118         struct ethtool_sfeatures cmd;
119         struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
120         netdev_features_t wanted = 0, valid = 0;
121         int i, ret = 0;
122
123         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
124                 return -EFAULT;
125         useraddr += sizeof(cmd);
126
127         if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
128                 return -EINVAL;
129
130         if (copy_from_user(features, useraddr, sizeof(features)))
131                 return -EFAULT;
132
133         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
134                 valid |= (netdev_features_t)features[i].valid << (32 * i);
135                 wanted |= (netdev_features_t)features[i].requested << (32 * i);
136         }
137
138         if (valid & ~NETIF_F_ETHTOOL_BITS)
139                 return -EINVAL;
140
141         if (valid & ~dev->hw_features) {
142                 valid &= dev->hw_features;
143                 ret |= ETHTOOL_F_UNSUPPORTED;
144         }
145
146         dev->wanted_features &= ~valid;
147         dev->wanted_features |= wanted & valid;
148         __netdev_update_features(dev);
149
150         if ((dev->wanted_features ^ dev->features) & valid)
151                 ret |= ETHTOOL_F_WISH;
152
153         return ret;
154 }
155
156 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
157 {
158         const struct ethtool_ops *ops = dev->ethtool_ops;
159
160         if (sset == ETH_SS_FEATURES)
161                 return ARRAY_SIZE(netdev_features_strings);
162
163         if (ops && ops->get_sset_count && ops->get_strings)
164                 return ops->get_sset_count(dev, sset);
165         else
166                 return -EOPNOTSUPP;
167 }
168
169 static void __ethtool_get_strings(struct net_device *dev,
170         u32 stringset, u8 *data)
171 {
172         const struct ethtool_ops *ops = dev->ethtool_ops;
173
174         if (stringset == ETH_SS_FEATURES)
175                 memcpy(data, netdev_features_strings,
176                         sizeof(netdev_features_strings));
177         else
178                 /* ops->get_strings is valid because checked earlier */
179                 ops->get_strings(dev, stringset, data);
180 }
181
182 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
183 {
184         /* feature masks of legacy discrete ethtool ops */
185
186         switch (eth_cmd) {
187         case ETHTOOL_GTXCSUM:
188         case ETHTOOL_STXCSUM:
189                 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
190         case ETHTOOL_GRXCSUM:
191         case ETHTOOL_SRXCSUM:
192                 return NETIF_F_RXCSUM;
193         case ETHTOOL_GSG:
194         case ETHTOOL_SSG:
195                 return NETIF_F_SG;
196         case ETHTOOL_GTSO:
197         case ETHTOOL_STSO:
198                 return NETIF_F_ALL_TSO;
199         case ETHTOOL_GUFO:
200         case ETHTOOL_SUFO:
201                 return NETIF_F_UFO;
202         case ETHTOOL_GGSO:
203         case ETHTOOL_SGSO:
204                 return NETIF_F_GSO;
205         case ETHTOOL_GGRO:
206         case ETHTOOL_SGRO:
207                 return NETIF_F_GRO;
208         default:
209                 BUG();
210         }
211 }
212
213 static int ethtool_get_one_feature(struct net_device *dev,
214         char __user *useraddr, u32 ethcmd)
215 {
216         netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
217         struct ethtool_value edata = {
218                 .cmd = ethcmd,
219                 .data = !!(dev->features & mask),
220         };
221
222         if (copy_to_user(useraddr, &edata, sizeof(edata)))
223                 return -EFAULT;
224         return 0;
225 }
226
227 static int ethtool_set_one_feature(struct net_device *dev,
228         void __user *useraddr, u32 ethcmd)
229 {
230         struct ethtool_value edata;
231         netdev_features_t mask;
232
233         if (copy_from_user(&edata, useraddr, sizeof(edata)))
234                 return -EFAULT;
235
236         mask = ethtool_get_feature_mask(ethcmd);
237         mask &= dev->hw_features;
238         if (!mask)
239                 return -EOPNOTSUPP;
240
241         if (edata.data)
242                 dev->wanted_features |= mask;
243         else
244                 dev->wanted_features &= ~mask;
245
246         __netdev_update_features(dev);
247
248         return 0;
249 }
250
251 #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
252                           ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
253 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
254                           NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
255
256 static u32 __ethtool_get_flags(struct net_device *dev)
257 {
258         u32 flags = 0;
259
260         if (dev->features & NETIF_F_LRO)        flags |= ETH_FLAG_LRO;
261         if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
262         if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
263         if (dev->features & NETIF_F_NTUPLE)     flags |= ETH_FLAG_NTUPLE;
264         if (dev->features & NETIF_F_RXHASH)     flags |= ETH_FLAG_RXHASH;
265
266         return flags;
267 }
268
269 static int __ethtool_set_flags(struct net_device *dev, u32 data)
270 {
271         netdev_features_t features = 0, changed;
272
273         if (data & ~ETH_ALL_FLAGS)
274                 return -EINVAL;
275
276         if (data & ETH_FLAG_LRO)        features |= NETIF_F_LRO;
277         if (data & ETH_FLAG_RXVLAN)     features |= NETIF_F_HW_VLAN_RX;
278         if (data & ETH_FLAG_TXVLAN)     features |= NETIF_F_HW_VLAN_TX;
279         if (data & ETH_FLAG_NTUPLE)     features |= NETIF_F_NTUPLE;
280         if (data & ETH_FLAG_RXHASH)     features |= NETIF_F_RXHASH;
281
282         /* allow changing only bits set in hw_features */
283         changed = (features ^ dev->features) & ETH_ALL_FEATURES;
284         if (changed & ~dev->hw_features)
285                 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
286
287         dev->wanted_features =
288                 (dev->wanted_features & ~changed) | (features & changed);
289
290         __netdev_update_features(dev);
291
292         return 0;
293 }
294
295 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
296 {
297         ASSERT_RTNL();
298
299         if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
300                 return -EOPNOTSUPP;
301
302         memset(cmd, 0, sizeof(struct ethtool_cmd));
303         cmd->cmd = ETHTOOL_GSET;
304         return dev->ethtool_ops->get_settings(dev, cmd);
305 }
306 EXPORT_SYMBOL(__ethtool_get_settings);
307
308 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
309 {
310         int err;
311         struct ethtool_cmd cmd;
312
313         err = __ethtool_get_settings(dev, &cmd);
314         if (err < 0)
315                 return err;
316
317         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
318                 return -EFAULT;
319         return 0;
320 }
321
322 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
323 {
324         struct ethtool_cmd cmd;
325
326         if (!dev->ethtool_ops->set_settings)
327                 return -EOPNOTSUPP;
328
329         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
330                 return -EFAULT;
331
332         return dev->ethtool_ops->set_settings(dev, &cmd);
333 }
334
335 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
336                                                   void __user *useraddr)
337 {
338         struct ethtool_drvinfo info;
339         const struct ethtool_ops *ops = dev->ethtool_ops;
340
341         memset(&info, 0, sizeof(info));
342         info.cmd = ETHTOOL_GDRVINFO;
343         if (ops && ops->get_drvinfo) {
344                 ops->get_drvinfo(dev, &info);
345         } else if (dev->dev.parent && dev->dev.parent->driver) {
346                 strlcpy(info.bus_info, dev_name(dev->dev.parent),
347                         sizeof(info.bus_info));
348                 strlcpy(info.driver, dev->dev.parent->driver->name,
349                         sizeof(info.driver));
350         } else {
351                 return -EOPNOTSUPP;
352         }
353
354         /*
355          * this method of obtaining string set info is deprecated;
356          * Use ETHTOOL_GSSET_INFO instead.
357          */
358         if (ops && ops->get_sset_count) {
359                 int rc;
360
361                 rc = ops->get_sset_count(dev, ETH_SS_TEST);
362                 if (rc >= 0)
363                         info.testinfo_len = rc;
364                 rc = ops->get_sset_count(dev, ETH_SS_STATS);
365                 if (rc >= 0)
366                         info.n_stats = rc;
367                 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
368                 if (rc >= 0)
369                         info.n_priv_flags = rc;
370         }
371         if (ops && ops->get_regs_len)
372                 info.regdump_len = ops->get_regs_len(dev);
373         if (ops && ops->get_eeprom_len)
374                 info.eedump_len = ops->get_eeprom_len(dev);
375
376         if (copy_to_user(useraddr, &info, sizeof(info)))
377                 return -EFAULT;
378         return 0;
379 }
380
381 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
382                                                     void __user *useraddr)
383 {
384         struct ethtool_sset_info info;
385         u64 sset_mask;
386         int i, idx = 0, n_bits = 0, ret, rc;
387         u32 *info_buf = NULL;
388
389         if (copy_from_user(&info, useraddr, sizeof(info)))
390                 return -EFAULT;
391
392         /* store copy of mask, because we zero struct later on */
393         sset_mask = info.sset_mask;
394         if (!sset_mask)
395                 return 0;
396
397         /* calculate size of return buffer */
398         n_bits = hweight64(sset_mask);
399
400         memset(&info, 0, sizeof(info));
401         info.cmd = ETHTOOL_GSSET_INFO;
402
403         info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
404         if (!info_buf)
405                 return -ENOMEM;
406
407         /*
408          * fill return buffer based on input bitmask and successful
409          * get_sset_count return
410          */
411         for (i = 0; i < 64; i++) {
412                 if (!(sset_mask & (1ULL << i)))
413                         continue;
414
415                 rc = __ethtool_get_sset_count(dev, i);
416                 if (rc >= 0) {
417                         info.sset_mask |= (1ULL << i);
418                         info_buf[idx++] = rc;
419                 }
420         }
421
422         ret = -EFAULT;
423         if (copy_to_user(useraddr, &info, sizeof(info)))
424                 goto out;
425
426         useraddr += offsetof(struct ethtool_sset_info, data);
427         if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
428                 goto out;
429
430         ret = 0;
431
432 out:
433         kfree(info_buf);
434         return ret;
435 }
436
437 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
438                                                 u32 cmd, void __user *useraddr)
439 {
440         struct ethtool_rxnfc info;
441         size_t info_size = sizeof(info);
442
443         if (!dev->ethtool_ops->set_rxnfc)
444                 return -EOPNOTSUPP;
445
446         /* struct ethtool_rxnfc was originally defined for
447          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
448          * members.  User-space might still be using that
449          * definition. */
450         if (cmd == ETHTOOL_SRXFH)
451                 info_size = (offsetof(struct ethtool_rxnfc, data) +
452                              sizeof(info.data));
453
454         if (copy_from_user(&info, useraddr, info_size))
455                 return -EFAULT;
456
457         return dev->ethtool_ops->set_rxnfc(dev, &info);
458 }
459
460 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
461                                                 u32 cmd, void __user *useraddr)
462 {
463         struct ethtool_rxnfc info;
464         size_t info_size = sizeof(info);
465         const struct ethtool_ops *ops = dev->ethtool_ops;
466         int ret;
467         void *rule_buf = NULL;
468
469         if (!ops->get_rxnfc)
470                 return -EOPNOTSUPP;
471
472         /* struct ethtool_rxnfc was originally defined for
473          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
474          * members.  User-space might still be using that
475          * definition. */
476         if (cmd == ETHTOOL_GRXFH)
477                 info_size = (offsetof(struct ethtool_rxnfc, data) +
478                              sizeof(info.data));
479
480         if (copy_from_user(&info, useraddr, info_size))
481                 return -EFAULT;
482
483         if (info.cmd == ETHTOOL_GRXCLSRLALL) {
484                 if (info.rule_cnt > 0) {
485                         if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
486                                 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
487                                                    GFP_USER);
488                         if (!rule_buf)
489                                 return -ENOMEM;
490                 }
491         }
492
493         ret = ops->get_rxnfc(dev, &info, rule_buf);
494         if (ret < 0)
495                 goto err_out;
496
497         ret = -EFAULT;
498         if (copy_to_user(useraddr, &info, info_size))
499                 goto err_out;
500
501         if (rule_buf) {
502                 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
503                 if (copy_to_user(useraddr, rule_buf,
504                                  info.rule_cnt * sizeof(u32)))
505                         goto err_out;
506         }
507         ret = 0;
508
509 err_out:
510         kfree(rule_buf);
511
512         return ret;
513 }
514
515 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
516                                                      void __user *useraddr)
517 {
518         struct ethtool_rxfh_indir *indir;
519         u32 table_size;
520         size_t full_size;
521         int ret;
522
523         if (!dev->ethtool_ops->get_rxfh_indir)
524                 return -EOPNOTSUPP;
525
526         if (copy_from_user(&table_size,
527                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
528                            sizeof(table_size)))
529                 return -EFAULT;
530
531         if (table_size >
532             (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
533                 return -ENOMEM;
534         full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
535         indir = kzalloc(full_size, GFP_USER);
536         if (!indir)
537                 return -ENOMEM;
538
539         indir->cmd = ETHTOOL_GRXFHINDIR;
540         indir->size = table_size;
541         ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
542         if (ret)
543                 goto out;
544
545         if (copy_to_user(useraddr, indir, full_size))
546                 ret = -EFAULT;
547
548 out:
549         kfree(indir);
550         return ret;
551 }
552
553 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
554                                                      void __user *useraddr)
555 {
556         struct ethtool_rxfh_indir *indir;
557         u32 table_size;
558         size_t full_size;
559         int ret;
560
561         if (!dev->ethtool_ops->set_rxfh_indir)
562                 return -EOPNOTSUPP;
563
564         if (copy_from_user(&table_size,
565                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
566                            sizeof(table_size)))
567                 return -EFAULT;
568
569         if (table_size >
570             (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
571                 return -ENOMEM;
572         full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
573         indir = kmalloc(full_size, GFP_USER);
574         if (!indir)
575                 return -ENOMEM;
576
577         if (copy_from_user(indir, useraddr, full_size)) {
578                 ret = -EFAULT;
579                 goto out;
580         }
581
582         ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
583
584 out:
585         kfree(indir);
586         return ret;
587 }
588
589 /*
590  * ethtool does not (or did not) set masks for flow parameters that are
591  * not specified, so if both value and mask are 0 then this must be
592  * treated as equivalent to a mask with all bits set.  Implement that
593  * here rather than in drivers.
594  */
595 static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
596 {
597         struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
598         struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
599
600         if (fs->flow_type != TCP_V4_FLOW &&
601             fs->flow_type != UDP_V4_FLOW &&
602             fs->flow_type != SCTP_V4_FLOW)
603                 return;
604
605         if (!(entry->ip4src | mask->ip4src))
606                 mask->ip4src = htonl(0xffffffff);
607         if (!(entry->ip4dst | mask->ip4dst))
608                 mask->ip4dst = htonl(0xffffffff);
609         if (!(entry->psrc | mask->psrc))
610                 mask->psrc = htons(0xffff);
611         if (!(entry->pdst | mask->pdst))
612                 mask->pdst = htons(0xffff);
613         if (!(entry->tos | mask->tos))
614                 mask->tos = 0xff;
615         if (!(fs->vlan_tag | fs->vlan_tag_mask))
616                 fs->vlan_tag_mask = 0xffff;
617         if (!(fs->data | fs->data_mask))
618                 fs->data_mask = 0xffffffffffffffffULL;
619 }
620
621 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
622                                                     void __user *useraddr)
623 {
624         struct ethtool_rx_ntuple cmd;
625         const struct ethtool_ops *ops = dev->ethtool_ops;
626
627         if (!ops->set_rx_ntuple)
628                 return -EOPNOTSUPP;
629
630         if (!(dev->features & NETIF_F_NTUPLE))
631                 return -EINVAL;
632
633         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
634                 return -EFAULT;
635
636         rx_ntuple_fix_masks(&cmd.fs);
637
638         return ops->set_rx_ntuple(dev, &cmd);
639 }
640
641 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
642 {
643         struct ethtool_regs regs;
644         const struct ethtool_ops *ops = dev->ethtool_ops;
645         void *regbuf;
646         int reglen, ret;
647
648         if (!ops->get_regs || !ops->get_regs_len)
649                 return -EOPNOTSUPP;
650
651         if (copy_from_user(&regs, useraddr, sizeof(regs)))
652                 return -EFAULT;
653
654         reglen = ops->get_regs_len(dev);
655         if (regs.len > reglen)
656                 regs.len = reglen;
657
658         regbuf = vzalloc(reglen);
659         if (reglen && !regbuf)
660                 return -ENOMEM;
661
662         ops->get_regs(dev, &regs, regbuf);
663
664         ret = -EFAULT;
665         if (copy_to_user(useraddr, &regs, sizeof(regs)))
666                 goto out;
667         useraddr += offsetof(struct ethtool_regs, data);
668         if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
669                 goto out;
670         ret = 0;
671
672  out:
673         vfree(regbuf);
674         return ret;
675 }
676
677 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
678 {
679         struct ethtool_value reset;
680         int ret;
681
682         if (!dev->ethtool_ops->reset)
683                 return -EOPNOTSUPP;
684
685         if (copy_from_user(&reset, useraddr, sizeof(reset)))
686                 return -EFAULT;
687
688         ret = dev->ethtool_ops->reset(dev, &reset.data);
689         if (ret)
690                 return ret;
691
692         if (copy_to_user(useraddr, &reset, sizeof(reset)))
693                 return -EFAULT;
694         return 0;
695 }
696
697 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
698 {
699         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
700
701         if (!dev->ethtool_ops->get_wol)
702                 return -EOPNOTSUPP;
703
704         dev->ethtool_ops->get_wol(dev, &wol);
705
706         if (copy_to_user(useraddr, &wol, sizeof(wol)))
707                 return -EFAULT;
708         return 0;
709 }
710
711 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
712 {
713         struct ethtool_wolinfo wol;
714
715         if (!dev->ethtool_ops->set_wol)
716                 return -EOPNOTSUPP;
717
718         if (copy_from_user(&wol, useraddr, sizeof(wol)))
719                 return -EFAULT;
720
721         return dev->ethtool_ops->set_wol(dev, &wol);
722 }
723
724 static int ethtool_nway_reset(struct net_device *dev)
725 {
726         if (!dev->ethtool_ops->nway_reset)
727                 return -EOPNOTSUPP;
728
729         return dev->ethtool_ops->nway_reset(dev);
730 }
731
732 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
733 {
734         struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
735
736         if (!dev->ethtool_ops->get_link)
737                 return -EOPNOTSUPP;
738
739         edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
740
741         if (copy_to_user(useraddr, &edata, sizeof(edata)))
742                 return -EFAULT;
743         return 0;
744 }
745
746 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
747 {
748         struct ethtool_eeprom eeprom;
749         const struct ethtool_ops *ops = dev->ethtool_ops;
750         void __user *userbuf = useraddr + sizeof(eeprom);
751         u32 bytes_remaining;
752         u8 *data;
753         int ret = 0;
754
755         if (!ops->get_eeprom || !ops->get_eeprom_len)
756                 return -EOPNOTSUPP;
757
758         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
759                 return -EFAULT;
760
761         /* Check for wrap and zero */
762         if (eeprom.offset + eeprom.len <= eeprom.offset)
763                 return -EINVAL;
764
765         /* Check for exceeding total eeprom len */
766         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
767                 return -EINVAL;
768
769         data = kmalloc(PAGE_SIZE, GFP_USER);
770         if (!data)
771                 return -ENOMEM;
772
773         bytes_remaining = eeprom.len;
774         while (bytes_remaining > 0) {
775                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
776
777                 ret = ops->get_eeprom(dev, &eeprom, data);
778                 if (ret)
779                         break;
780                 if (copy_to_user(userbuf, data, eeprom.len)) {
781                         ret = -EFAULT;
782                         break;
783                 }
784                 userbuf += eeprom.len;
785                 eeprom.offset += eeprom.len;
786                 bytes_remaining -= eeprom.len;
787         }
788
789         eeprom.len = userbuf - (useraddr + sizeof(eeprom));
790         eeprom.offset -= eeprom.len;
791         if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
792                 ret = -EFAULT;
793
794         kfree(data);
795         return ret;
796 }
797
798 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
799 {
800         struct ethtool_eeprom eeprom;
801         const struct ethtool_ops *ops = dev->ethtool_ops;
802         void __user *userbuf = useraddr + sizeof(eeprom);
803         u32 bytes_remaining;
804         u8 *data;
805         int ret = 0;
806
807         if (!ops->set_eeprom || !ops->get_eeprom_len)
808                 return -EOPNOTSUPP;
809
810         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
811                 return -EFAULT;
812
813         /* Check for wrap and zero */
814         if (eeprom.offset + eeprom.len <= eeprom.offset)
815                 return -EINVAL;
816
817         /* Check for exceeding total eeprom len */
818         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
819                 return -EINVAL;
820
821         data = kmalloc(PAGE_SIZE, GFP_USER);
822         if (!data)
823                 return -ENOMEM;
824
825         bytes_remaining = eeprom.len;
826         while (bytes_remaining > 0) {
827                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
828
829                 if (copy_from_user(data, userbuf, eeprom.len)) {
830                         ret = -EFAULT;
831                         break;
832                 }
833                 ret = ops->set_eeprom(dev, &eeprom, data);
834                 if (ret)
835                         break;
836                 userbuf += eeprom.len;
837                 eeprom.offset += eeprom.len;
838                 bytes_remaining -= eeprom.len;
839         }
840
841         kfree(data);
842         return ret;
843 }
844
845 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
846                                                    void __user *useraddr)
847 {
848         struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
849
850         if (!dev->ethtool_ops->get_coalesce)
851                 return -EOPNOTSUPP;
852
853         dev->ethtool_ops->get_coalesce(dev, &coalesce);
854
855         if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
856                 return -EFAULT;
857         return 0;
858 }
859
860 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
861                                                    void __user *useraddr)
862 {
863         struct ethtool_coalesce coalesce;
864
865         if (!dev->ethtool_ops->set_coalesce)
866                 return -EOPNOTSUPP;
867
868         if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
869                 return -EFAULT;
870
871         return dev->ethtool_ops->set_coalesce(dev, &coalesce);
872 }
873
874 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
875 {
876         struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
877
878         if (!dev->ethtool_ops->get_ringparam)
879                 return -EOPNOTSUPP;
880
881         dev->ethtool_ops->get_ringparam(dev, &ringparam);
882
883         if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
884                 return -EFAULT;
885         return 0;
886 }
887
888 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
889 {
890         struct ethtool_ringparam ringparam;
891
892         if (!dev->ethtool_ops->set_ringparam)
893                 return -EOPNOTSUPP;
894
895         if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
896                 return -EFAULT;
897
898         return dev->ethtool_ops->set_ringparam(dev, &ringparam);
899 }
900
901 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
902                                                    void __user *useraddr)
903 {
904         struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
905
906         if (!dev->ethtool_ops->get_channels)
907                 return -EOPNOTSUPP;
908
909         dev->ethtool_ops->get_channels(dev, &channels);
910
911         if (copy_to_user(useraddr, &channels, sizeof(channels)))
912                 return -EFAULT;
913         return 0;
914 }
915
916 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
917                                                    void __user *useraddr)
918 {
919         struct ethtool_channels channels;
920
921         if (!dev->ethtool_ops->set_channels)
922                 return -EOPNOTSUPP;
923
924         if (copy_from_user(&channels, useraddr, sizeof(channels)))
925                 return -EFAULT;
926
927         return dev->ethtool_ops->set_channels(dev, &channels);
928 }
929
930 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
931 {
932         struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
933
934         if (!dev->ethtool_ops->get_pauseparam)
935                 return -EOPNOTSUPP;
936
937         dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
938
939         if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
940                 return -EFAULT;
941         return 0;
942 }
943
944 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
945 {
946         struct ethtool_pauseparam pauseparam;
947
948         if (!dev->ethtool_ops->set_pauseparam)
949                 return -EOPNOTSUPP;
950
951         if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
952                 return -EFAULT;
953
954         return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
955 }
956
957 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
958 {
959         struct ethtool_test test;
960         const struct ethtool_ops *ops = dev->ethtool_ops;
961         u64 *data;
962         int ret, test_len;
963
964         if (!ops->self_test || !ops->get_sset_count)
965                 return -EOPNOTSUPP;
966
967         test_len = ops->get_sset_count(dev, ETH_SS_TEST);
968         if (test_len < 0)
969                 return test_len;
970         WARN_ON(test_len == 0);
971
972         if (copy_from_user(&test, useraddr, sizeof(test)))
973                 return -EFAULT;
974
975         test.len = test_len;
976         data = kmalloc(test_len * sizeof(u64), GFP_USER);
977         if (!data)
978                 return -ENOMEM;
979
980         ops->self_test(dev, &test, data);
981
982         ret = -EFAULT;
983         if (copy_to_user(useraddr, &test, sizeof(test)))
984                 goto out;
985         useraddr += sizeof(test);
986         if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
987                 goto out;
988         ret = 0;
989
990  out:
991         kfree(data);
992         return ret;
993 }
994
995 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
996 {
997         struct ethtool_gstrings gstrings;
998         u8 *data;
999         int ret;
1000
1001         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1002                 return -EFAULT;
1003
1004         ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1005         if (ret < 0)
1006                 return ret;
1007
1008         gstrings.len = ret;
1009
1010         data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1011         if (!data)
1012                 return -ENOMEM;
1013
1014         __ethtool_get_strings(dev, gstrings.string_set, data);
1015
1016         ret = -EFAULT;
1017         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1018                 goto out;
1019         useraddr += sizeof(gstrings);
1020         if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1021                 goto out;
1022         ret = 0;
1023
1024 out:
1025         kfree(data);
1026         return ret;
1027 }
1028
1029 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1030 {
1031         struct ethtool_value id;
1032         static bool busy;
1033         int rc;
1034
1035         if (!dev->ethtool_ops->set_phys_id)
1036                 return -EOPNOTSUPP;
1037
1038         if (busy)
1039                 return -EBUSY;
1040
1041         if (copy_from_user(&id, useraddr, sizeof(id)))
1042                 return -EFAULT;
1043
1044         rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1045         if (rc < 0)
1046                 return rc;
1047
1048         /* Drop the RTNL lock while waiting, but prevent reentry or
1049          * removal of the device.
1050          */
1051         busy = true;
1052         dev_hold(dev);
1053         rtnl_unlock();
1054
1055         if (rc == 0) {
1056                 /* Driver will handle this itself */
1057                 schedule_timeout_interruptible(
1058                         id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1059         } else {
1060                 /* Driver expects to be called at twice the frequency in rc */
1061                 int n = rc * 2, i, interval = HZ / n;
1062
1063                 /* Count down seconds */
1064                 do {
1065                         /* Count down iterations per second */
1066                         i = n;
1067                         do {
1068                                 rtnl_lock();
1069                                 rc = dev->ethtool_ops->set_phys_id(dev,
1070                                     (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1071                                 rtnl_unlock();
1072                                 if (rc)
1073                                         break;
1074                                 schedule_timeout_interruptible(interval);
1075                         } while (!signal_pending(current) && --i != 0);
1076                 } while (!signal_pending(current) &&
1077                          (id.data == 0 || --id.data != 0));
1078         }
1079
1080         rtnl_lock();
1081         dev_put(dev);
1082         busy = false;
1083
1084         (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1085         return rc;
1086 }
1087
1088 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1089 {
1090         struct ethtool_stats stats;
1091         const struct ethtool_ops *ops = dev->ethtool_ops;
1092         u64 *data;
1093         int ret, n_stats;
1094
1095         if (!ops->get_ethtool_stats || !ops->get_sset_count)
1096                 return -EOPNOTSUPP;
1097
1098         n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1099         if (n_stats < 0)
1100                 return n_stats;
1101         WARN_ON(n_stats == 0);
1102
1103         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1104                 return -EFAULT;
1105
1106         stats.n_stats = n_stats;
1107         data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1108         if (!data)
1109                 return -ENOMEM;
1110
1111         ops->get_ethtool_stats(dev, &stats, data);
1112
1113         ret = -EFAULT;
1114         if (copy_to_user(useraddr, &stats, sizeof(stats)))
1115                 goto out;
1116         useraddr += sizeof(stats);
1117         if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1118                 goto out;
1119         ret = 0;
1120
1121  out:
1122         kfree(data);
1123         return ret;
1124 }
1125
1126 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1127 {
1128         struct ethtool_perm_addr epaddr;
1129
1130         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1131                 return -EFAULT;
1132
1133         if (epaddr.size < dev->addr_len)
1134                 return -ETOOSMALL;
1135         epaddr.size = dev->addr_len;
1136
1137         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1138                 return -EFAULT;
1139         useraddr += sizeof(epaddr);
1140         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1141                 return -EFAULT;
1142         return 0;
1143 }
1144
1145 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1146                              u32 cmd, u32 (*actor)(struct net_device *))
1147 {
1148         struct ethtool_value edata = { .cmd = cmd };
1149
1150         if (!actor)
1151                 return -EOPNOTSUPP;
1152
1153         edata.data = actor(dev);
1154
1155         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1156                 return -EFAULT;
1157         return 0;
1158 }
1159
1160 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1161                              void (*actor)(struct net_device *, u32))
1162 {
1163         struct ethtool_value edata;
1164
1165         if (!actor)
1166                 return -EOPNOTSUPP;
1167
1168         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1169                 return -EFAULT;
1170
1171         actor(dev, edata.data);
1172         return 0;
1173 }
1174
1175 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1176                              int (*actor)(struct net_device *, u32))
1177 {
1178         struct ethtool_value edata;
1179
1180         if (!actor)
1181                 return -EOPNOTSUPP;
1182
1183         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1184                 return -EFAULT;
1185
1186         return actor(dev, edata.data);
1187 }
1188
1189 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1190                                                    char __user *useraddr)
1191 {
1192         struct ethtool_flash efl;
1193
1194         if (copy_from_user(&efl, useraddr, sizeof(efl)))
1195                 return -EFAULT;
1196
1197         if (!dev->ethtool_ops->flash_device)
1198                 return -EOPNOTSUPP;
1199
1200         return dev->ethtool_ops->flash_device(dev, &efl);
1201 }
1202
1203 static int ethtool_set_dump(struct net_device *dev,
1204                         void __user *useraddr)
1205 {
1206         struct ethtool_dump dump;
1207
1208         if (!dev->ethtool_ops->set_dump)
1209                 return -EOPNOTSUPP;
1210
1211         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1212                 return -EFAULT;
1213
1214         return dev->ethtool_ops->set_dump(dev, &dump);
1215 }
1216
1217 static int ethtool_get_dump_flag(struct net_device *dev,
1218                                 void __user *useraddr)
1219 {
1220         int ret;
1221         struct ethtool_dump dump;
1222         const struct ethtool_ops *ops = dev->ethtool_ops;
1223
1224         if (!dev->ethtool_ops->get_dump_flag)
1225                 return -EOPNOTSUPP;
1226
1227         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1228                 return -EFAULT;
1229
1230         ret = ops->get_dump_flag(dev, &dump);
1231         if (ret)
1232                 return ret;
1233
1234         if (copy_to_user(useraddr, &dump, sizeof(dump)))
1235                 return -EFAULT;
1236         return 0;
1237 }
1238
1239 static int ethtool_get_dump_data(struct net_device *dev,
1240                                 void __user *useraddr)
1241 {
1242         int ret;
1243         __u32 len;
1244         struct ethtool_dump dump, tmp;
1245         const struct ethtool_ops *ops = dev->ethtool_ops;
1246         void *data = NULL;
1247
1248         if (!dev->ethtool_ops->get_dump_data ||
1249                 !dev->ethtool_ops->get_dump_flag)
1250                 return -EOPNOTSUPP;
1251
1252         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1253                 return -EFAULT;
1254
1255         memset(&tmp, 0, sizeof(tmp));
1256         tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1257         ret = ops->get_dump_flag(dev, &tmp);
1258         if (ret)
1259                 return ret;
1260
1261         len = (tmp.len > dump.len) ? dump.len : tmp.len;
1262         if (!len)
1263                 return -EFAULT;
1264
1265         data = vzalloc(tmp.len);
1266         if (!data)
1267                 return -ENOMEM;
1268         ret = ops->get_dump_data(dev, &dump, data);
1269         if (ret)
1270                 goto out;
1271
1272         if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1273                 ret = -EFAULT;
1274                 goto out;
1275         }
1276         useraddr += offsetof(struct ethtool_dump, data);
1277         if (copy_to_user(useraddr, data, len))
1278                 ret = -EFAULT;
1279 out:
1280         vfree(data);
1281         return ret;
1282 }
1283
1284 /* The main entry point in this file.  Called from net/core/dev.c */
1285
1286 int dev_ethtool(struct net *net, struct ifreq *ifr)
1287 {
1288         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1289         void __user *useraddr = ifr->ifr_data;
1290         u32 ethcmd;
1291         int rc;
1292         u32 old_features;
1293
1294         if (!dev || !netif_device_present(dev))
1295                 return -ENODEV;
1296
1297         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1298                 return -EFAULT;
1299
1300         if (!dev->ethtool_ops) {
1301                 /* ETHTOOL_GDRVINFO does not require any driver support.
1302                  * It is also unprivileged and does not change anything,
1303                  * so we can take a shortcut to it. */
1304                 if (ethcmd == ETHTOOL_GDRVINFO)
1305                         return ethtool_get_drvinfo(dev, useraddr);
1306                 else
1307                         return -EOPNOTSUPP;
1308         }
1309
1310         /* Allow some commands to be done by anyone */
1311         switch (ethcmd) {
1312         case ETHTOOL_GSET:
1313         case ETHTOOL_GDRVINFO:
1314         case ETHTOOL_GMSGLVL:
1315         case ETHTOOL_GCOALESCE:
1316         case ETHTOOL_GRINGPARAM:
1317         case ETHTOOL_GPAUSEPARAM:
1318         case ETHTOOL_GRXCSUM:
1319         case ETHTOOL_GTXCSUM:
1320         case ETHTOOL_GSG:
1321         case ETHTOOL_GSTRINGS:
1322         case ETHTOOL_GTSO:
1323         case ETHTOOL_GPERMADDR:
1324         case ETHTOOL_GUFO:
1325         case ETHTOOL_GGSO:
1326         case ETHTOOL_GGRO:
1327         case ETHTOOL_GFLAGS:
1328         case ETHTOOL_GPFLAGS:
1329         case ETHTOOL_GRXFH:
1330         case ETHTOOL_GRXRINGS:
1331         case ETHTOOL_GRXCLSRLCNT:
1332         case ETHTOOL_GRXCLSRULE:
1333         case ETHTOOL_GRXCLSRLALL:
1334         case ETHTOOL_GFEATURES:
1335                 break;
1336         default:
1337                 if (!capable(CAP_NET_ADMIN))
1338                         return -EPERM;
1339         }
1340
1341         if (dev->ethtool_ops->begin) {
1342                 rc = dev->ethtool_ops->begin(dev);
1343                 if (rc  < 0)
1344                         return rc;
1345         }
1346         old_features = dev->features;
1347
1348         switch (ethcmd) {
1349         case ETHTOOL_GSET:
1350                 rc = ethtool_get_settings(dev, useraddr);
1351                 break;
1352         case ETHTOOL_SSET:
1353                 rc = ethtool_set_settings(dev, useraddr);
1354                 break;
1355         case ETHTOOL_GDRVINFO:
1356                 rc = ethtool_get_drvinfo(dev, useraddr);
1357                 break;
1358         case ETHTOOL_GREGS:
1359                 rc = ethtool_get_regs(dev, useraddr);
1360                 break;
1361         case ETHTOOL_GWOL:
1362                 rc = ethtool_get_wol(dev, useraddr);
1363                 break;
1364         case ETHTOOL_SWOL:
1365                 rc = ethtool_set_wol(dev, useraddr);
1366                 break;
1367         case ETHTOOL_GMSGLVL:
1368                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1369                                        dev->ethtool_ops->get_msglevel);
1370                 break;
1371         case ETHTOOL_SMSGLVL:
1372                 rc = ethtool_set_value_void(dev, useraddr,
1373                                        dev->ethtool_ops->set_msglevel);
1374                 break;
1375         case ETHTOOL_NWAY_RST:
1376                 rc = ethtool_nway_reset(dev);
1377                 break;
1378         case ETHTOOL_GLINK:
1379                 rc = ethtool_get_link(dev, useraddr);
1380                 break;
1381         case ETHTOOL_GEEPROM:
1382                 rc = ethtool_get_eeprom(dev, useraddr);
1383                 break;
1384         case ETHTOOL_SEEPROM:
1385                 rc = ethtool_set_eeprom(dev, useraddr);
1386                 break;
1387         case ETHTOOL_GCOALESCE:
1388                 rc = ethtool_get_coalesce(dev, useraddr);
1389                 break;
1390         case ETHTOOL_SCOALESCE:
1391                 rc = ethtool_set_coalesce(dev, useraddr);
1392                 break;
1393         case ETHTOOL_GRINGPARAM:
1394                 rc = ethtool_get_ringparam(dev, useraddr);
1395                 break;
1396         case ETHTOOL_SRINGPARAM:
1397                 rc = ethtool_set_ringparam(dev, useraddr);
1398                 break;
1399         case ETHTOOL_GPAUSEPARAM:
1400                 rc = ethtool_get_pauseparam(dev, useraddr);
1401                 break;
1402         case ETHTOOL_SPAUSEPARAM:
1403                 rc = ethtool_set_pauseparam(dev, useraddr);
1404                 break;
1405         case ETHTOOL_TEST:
1406                 rc = ethtool_self_test(dev, useraddr);
1407                 break;
1408         case ETHTOOL_GSTRINGS:
1409                 rc = ethtool_get_strings(dev, useraddr);
1410                 break;
1411         case ETHTOOL_PHYS_ID:
1412                 rc = ethtool_phys_id(dev, useraddr);
1413                 break;
1414         case ETHTOOL_GSTATS:
1415                 rc = ethtool_get_stats(dev, useraddr);
1416                 break;
1417         case ETHTOOL_GPERMADDR:
1418                 rc = ethtool_get_perm_addr(dev, useraddr);
1419                 break;
1420         case ETHTOOL_GFLAGS:
1421                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1422                                         __ethtool_get_flags);
1423                 break;
1424         case ETHTOOL_SFLAGS:
1425                 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1426                 break;
1427         case ETHTOOL_GPFLAGS:
1428                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1429                                        dev->ethtool_ops->get_priv_flags);
1430                 break;
1431         case ETHTOOL_SPFLAGS:
1432                 rc = ethtool_set_value(dev, useraddr,
1433                                        dev->ethtool_ops->set_priv_flags);
1434                 break;
1435         case ETHTOOL_GRXFH:
1436         case ETHTOOL_GRXRINGS:
1437         case ETHTOOL_GRXCLSRLCNT:
1438         case ETHTOOL_GRXCLSRULE:
1439         case ETHTOOL_GRXCLSRLALL:
1440                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1441                 break;
1442         case ETHTOOL_SRXFH:
1443         case ETHTOOL_SRXCLSRLDEL:
1444         case ETHTOOL_SRXCLSRLINS:
1445                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1446                 break;
1447         case ETHTOOL_FLASHDEV:
1448                 rc = ethtool_flash_device(dev, useraddr);
1449                 break;
1450         case ETHTOOL_RESET:
1451                 rc = ethtool_reset(dev, useraddr);
1452                 break;
1453         case ETHTOOL_SRXNTUPLE:
1454                 rc = ethtool_set_rx_ntuple(dev, useraddr);
1455                 break;
1456         case ETHTOOL_GSSET_INFO:
1457                 rc = ethtool_get_sset_info(dev, useraddr);
1458                 break;
1459         case ETHTOOL_GRXFHINDIR:
1460                 rc = ethtool_get_rxfh_indir(dev, useraddr);
1461                 break;
1462         case ETHTOOL_SRXFHINDIR:
1463                 rc = ethtool_set_rxfh_indir(dev, useraddr);
1464                 break;
1465         case ETHTOOL_GFEATURES:
1466                 rc = ethtool_get_features(dev, useraddr);
1467                 break;
1468         case ETHTOOL_SFEATURES:
1469                 rc = ethtool_set_features(dev, useraddr);
1470                 break;
1471         case ETHTOOL_GTXCSUM:
1472         case ETHTOOL_GRXCSUM:
1473         case ETHTOOL_GSG:
1474         case ETHTOOL_GTSO:
1475         case ETHTOOL_GUFO:
1476         case ETHTOOL_GGSO:
1477         case ETHTOOL_GGRO:
1478                 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1479                 break;
1480         case ETHTOOL_STXCSUM:
1481         case ETHTOOL_SRXCSUM:
1482         case ETHTOOL_SSG:
1483         case ETHTOOL_STSO:
1484         case ETHTOOL_SUFO:
1485         case ETHTOOL_SGSO:
1486         case ETHTOOL_SGRO:
1487                 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1488                 break;
1489         case ETHTOOL_GCHANNELS:
1490                 rc = ethtool_get_channels(dev, useraddr);
1491                 break;
1492         case ETHTOOL_SCHANNELS:
1493                 rc = ethtool_set_channels(dev, useraddr);
1494                 break;
1495         case ETHTOOL_SET_DUMP:
1496                 rc = ethtool_set_dump(dev, useraddr);
1497                 break;
1498         case ETHTOOL_GET_DUMP_FLAG:
1499                 rc = ethtool_get_dump_flag(dev, useraddr);
1500                 break;
1501         case ETHTOOL_GET_DUMP_DATA:
1502                 rc = ethtool_get_dump_data(dev, useraddr);
1503                 break;
1504         default:
1505                 rc = -EOPNOTSUPP;
1506         }
1507
1508         if (dev->ethtool_ops->complete)
1509                 dev->ethtool_ops->complete(dev);
1510
1511         if (old_features != dev->features)
1512                 netdev_features_change(dev);
1513
1514         return rc;
1515 }