Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.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/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sched.h>
28 #include <linux/net.h>
29
30 /*
31  * Some useful ethtool_ops methods that're device independent.
32  * If we find that all drivers want to do the same thing here,
33  * we can turn these into dev_() function calls.
34  */
35
36 u32 ethtool_op_get_link(struct net_device *dev)
37 {
38         return netif_carrier_ok(dev) ? 1 : 0;
39 }
40 EXPORT_SYMBOL(ethtool_op_get_link);
41
42 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
43 {
44         info->so_timestamping =
45                 SOF_TIMESTAMPING_TX_SOFTWARE |
46                 SOF_TIMESTAMPING_RX_SOFTWARE |
47                 SOF_TIMESTAMPING_SOFTWARE;
48         info->phc_index = -1;
49         return 0;
50 }
51 EXPORT_SYMBOL(ethtool_op_get_ts_info);
52
53 /* Handlers for each ethtool command */
54
55 #define ETHTOOL_DEV_FEATURE_WORDS       ((NETDEV_FEATURE_COUNT + 31) / 32)
56
57 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
58         [NETIF_F_SG_BIT] =               "tx-scatter-gather",
59         [NETIF_F_IP_CSUM_BIT] =          "tx-checksum-ipv4",
60         [NETIF_F_HW_CSUM_BIT] =          "tx-checksum-ip-generic",
61         [NETIF_F_IPV6_CSUM_BIT] =        "tx-checksum-ipv6",
62         [NETIF_F_HIGHDMA_BIT] =          "highdma",
63         [NETIF_F_FRAGLIST_BIT] =         "tx-scatter-gather-fraglist",
64         [NETIF_F_HW_VLAN_CTAG_TX_BIT] =  "tx-vlan-hw-insert",
65
66         [NETIF_F_HW_VLAN_CTAG_RX_BIT] =  "rx-vlan-hw-parse",
67         [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
68         [NETIF_F_HW_VLAN_STAG_TX_BIT] =  "tx-vlan-stag-hw-insert",
69         [NETIF_F_HW_VLAN_STAG_RX_BIT] =  "rx-vlan-stag-hw-parse",
70         [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
71         [NETIF_F_VLAN_CHALLENGED_BIT] =  "vlan-challenged",
72         [NETIF_F_GSO_BIT] =              "tx-generic-segmentation",
73         [NETIF_F_LLTX_BIT] =             "tx-lockless",
74         [NETIF_F_NETNS_LOCAL_BIT] =      "netns-local",
75         [NETIF_F_GRO_BIT] =              "rx-gro",
76         [NETIF_F_LRO_BIT] =              "rx-lro",
77
78         [NETIF_F_TSO_BIT] =              "tx-tcp-segmentation",
79         [NETIF_F_UFO_BIT] =              "tx-udp-fragmentation",
80         [NETIF_F_GSO_ROBUST_BIT] =       "tx-gso-robust",
81         [NETIF_F_TSO_ECN_BIT] =          "tx-tcp-ecn-segmentation",
82         [NETIF_F_TSO6_BIT] =             "tx-tcp6-segmentation",
83         [NETIF_F_FSO_BIT] =              "tx-fcoe-segmentation",
84         [NETIF_F_GSO_GRE_BIT] =          "tx-gre-segmentation",
85         [NETIF_F_GSO_IPIP_BIT] =         "tx-ipip-segmentation",
86         [NETIF_F_GSO_SIT_BIT] =          "tx-sit-segmentation",
87         [NETIF_F_GSO_UDP_TUNNEL_BIT] =   "tx-udp_tnl-segmentation",
88
89         [NETIF_F_FCOE_CRC_BIT] =         "tx-checksum-fcoe-crc",
90         [NETIF_F_SCTP_CSUM_BIT] =        "tx-checksum-sctp",
91         [NETIF_F_FCOE_MTU_BIT] =         "fcoe-mtu",
92         [NETIF_F_NTUPLE_BIT] =           "rx-ntuple-filter",
93         [NETIF_F_RXHASH_BIT] =           "rx-hashing",
94         [NETIF_F_RXCSUM_BIT] =           "rx-checksum",
95         [NETIF_F_NOCACHE_COPY_BIT] =     "tx-nocache-copy",
96         [NETIF_F_LOOPBACK_BIT] =         "loopback",
97         [NETIF_F_RXFCS_BIT] =            "rx-fcs",
98         [NETIF_F_RXALL_BIT] =            "rx-all",
99         [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
100         [NETIF_F_BUSY_POLL_BIT] =        "busy-poll",
101 };
102
103 static const char
104 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
105         [ETH_RSS_HASH_TOP_BIT] =        "toeplitz",
106         [ETH_RSS_HASH_XOR_BIT] =        "xor",
107 };
108
109 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
110 {
111         struct ethtool_gfeatures cmd = {
112                 .cmd = ETHTOOL_GFEATURES,
113                 .size = ETHTOOL_DEV_FEATURE_WORDS,
114         };
115         struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
116         u32 __user *sizeaddr;
117         u32 copy_size;
118         int i;
119
120         /* in case feature bits run out again */
121         BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
122
123         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
124                 features[i].available = (u32)(dev->hw_features >> (32 * i));
125                 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
126                 features[i].active = (u32)(dev->features >> (32 * i));
127                 features[i].never_changed =
128                         (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
129         }
130
131         sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
132         if (get_user(copy_size, sizeaddr))
133                 return -EFAULT;
134
135         if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
136                 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
137
138         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
139                 return -EFAULT;
140         useraddr += sizeof(cmd);
141         if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
142                 return -EFAULT;
143
144         return 0;
145 }
146
147 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
148 {
149         struct ethtool_sfeatures cmd;
150         struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
151         netdev_features_t wanted = 0, valid = 0;
152         int i, ret = 0;
153
154         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
155                 return -EFAULT;
156         useraddr += sizeof(cmd);
157
158         if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
159                 return -EINVAL;
160
161         if (copy_from_user(features, useraddr, sizeof(features)))
162                 return -EFAULT;
163
164         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
165                 valid |= (netdev_features_t)features[i].valid << (32 * i);
166                 wanted |= (netdev_features_t)features[i].requested << (32 * i);
167         }
168
169         if (valid & ~NETIF_F_ETHTOOL_BITS)
170                 return -EINVAL;
171
172         if (valid & ~dev->hw_features) {
173                 valid &= dev->hw_features;
174                 ret |= ETHTOOL_F_UNSUPPORTED;
175         }
176
177         dev->wanted_features &= ~valid;
178         dev->wanted_features |= wanted & valid;
179         __netdev_update_features(dev);
180
181         if ((dev->wanted_features ^ dev->features) & valid)
182                 ret |= ETHTOOL_F_WISH;
183
184         return ret;
185 }
186
187 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
188 {
189         const struct ethtool_ops *ops = dev->ethtool_ops;
190
191         if (sset == ETH_SS_FEATURES)
192                 return ARRAY_SIZE(netdev_features_strings);
193
194         if (sset == ETH_SS_RSS_HASH_FUNCS)
195                 return ARRAY_SIZE(rss_hash_func_strings);
196
197         if (ops->get_sset_count && ops->get_strings)
198                 return ops->get_sset_count(dev, sset);
199         else
200                 return -EOPNOTSUPP;
201 }
202
203 static void __ethtool_get_strings(struct net_device *dev,
204         u32 stringset, u8 *data)
205 {
206         const struct ethtool_ops *ops = dev->ethtool_ops;
207
208         if (stringset == ETH_SS_FEATURES)
209                 memcpy(data, netdev_features_strings,
210                         sizeof(netdev_features_strings));
211         else if (stringset == ETH_SS_RSS_HASH_FUNCS)
212                 memcpy(data, rss_hash_func_strings,
213                        sizeof(rss_hash_func_strings));
214         else
215                 /* ops->get_strings is valid because checked earlier */
216                 ops->get_strings(dev, stringset, data);
217 }
218
219 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
220 {
221         /* feature masks of legacy discrete ethtool ops */
222
223         switch (eth_cmd) {
224         case ETHTOOL_GTXCSUM:
225         case ETHTOOL_STXCSUM:
226                 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
227         case ETHTOOL_GRXCSUM:
228         case ETHTOOL_SRXCSUM:
229                 return NETIF_F_RXCSUM;
230         case ETHTOOL_GSG:
231         case ETHTOOL_SSG:
232                 return NETIF_F_SG;
233         case ETHTOOL_GTSO:
234         case ETHTOOL_STSO:
235                 return NETIF_F_ALL_TSO;
236         case ETHTOOL_GUFO:
237         case ETHTOOL_SUFO:
238                 return NETIF_F_UFO;
239         case ETHTOOL_GGSO:
240         case ETHTOOL_SGSO:
241                 return NETIF_F_GSO;
242         case ETHTOOL_GGRO:
243         case ETHTOOL_SGRO:
244                 return NETIF_F_GRO;
245         default:
246                 BUG();
247         }
248 }
249
250 static int ethtool_get_one_feature(struct net_device *dev,
251         char __user *useraddr, u32 ethcmd)
252 {
253         netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
254         struct ethtool_value edata = {
255                 .cmd = ethcmd,
256                 .data = !!(dev->features & mask),
257         };
258
259         if (copy_to_user(useraddr, &edata, sizeof(edata)))
260                 return -EFAULT;
261         return 0;
262 }
263
264 static int ethtool_set_one_feature(struct net_device *dev,
265         void __user *useraddr, u32 ethcmd)
266 {
267         struct ethtool_value edata;
268         netdev_features_t mask;
269
270         if (copy_from_user(&edata, useraddr, sizeof(edata)))
271                 return -EFAULT;
272
273         mask = ethtool_get_feature_mask(ethcmd);
274         mask &= dev->hw_features;
275         if (!mask)
276                 return -EOPNOTSUPP;
277
278         if (edata.data)
279                 dev->wanted_features |= mask;
280         else
281                 dev->wanted_features &= ~mask;
282
283         __netdev_update_features(dev);
284
285         return 0;
286 }
287
288 #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
289                           ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
290 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
291                           NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
292                           NETIF_F_RXHASH)
293
294 static u32 __ethtool_get_flags(struct net_device *dev)
295 {
296         u32 flags = 0;
297
298         if (dev->features & NETIF_F_LRO)
299                 flags |= ETH_FLAG_LRO;
300         if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
301                 flags |= ETH_FLAG_RXVLAN;
302         if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
303                 flags |= ETH_FLAG_TXVLAN;
304         if (dev->features & NETIF_F_NTUPLE)
305                 flags |= ETH_FLAG_NTUPLE;
306         if (dev->features & NETIF_F_RXHASH)
307                 flags |= ETH_FLAG_RXHASH;
308
309         return flags;
310 }
311
312 static int __ethtool_set_flags(struct net_device *dev, u32 data)
313 {
314         netdev_features_t features = 0, changed;
315
316         if (data & ~ETH_ALL_FLAGS)
317                 return -EINVAL;
318
319         if (data & ETH_FLAG_LRO)
320                 features |= NETIF_F_LRO;
321         if (data & ETH_FLAG_RXVLAN)
322                 features |= NETIF_F_HW_VLAN_CTAG_RX;
323         if (data & ETH_FLAG_TXVLAN)
324                 features |= NETIF_F_HW_VLAN_CTAG_TX;
325         if (data & ETH_FLAG_NTUPLE)
326                 features |= NETIF_F_NTUPLE;
327         if (data & ETH_FLAG_RXHASH)
328                 features |= NETIF_F_RXHASH;
329
330         /* allow changing only bits set in hw_features */
331         changed = (features ^ dev->features) & ETH_ALL_FEATURES;
332         if (changed & ~dev->hw_features)
333                 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
334
335         dev->wanted_features =
336                 (dev->wanted_features & ~changed) | (features & changed);
337
338         __netdev_update_features(dev);
339
340         return 0;
341 }
342
343 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
344 {
345         ASSERT_RTNL();
346
347         if (!dev->ethtool_ops->get_settings)
348                 return -EOPNOTSUPP;
349
350         memset(cmd, 0, sizeof(struct ethtool_cmd));
351         cmd->cmd = ETHTOOL_GSET;
352         return dev->ethtool_ops->get_settings(dev, cmd);
353 }
354 EXPORT_SYMBOL(__ethtool_get_settings);
355
356 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
357 {
358         int err;
359         struct ethtool_cmd cmd;
360
361         if (!dev->ethtool_ops->get_settings)
362                 return -EOPNOTSUPP;
363
364         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
365                 return -EFAULT;
366
367         cmd.cmd = ETHTOOL_GSET;
368
369         err = dev->ethtool_ops->get_settings(dev, &cmd);
370         if (err < 0)
371                 return err;
372
373         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
374                 return -EFAULT;
375         return 0;
376 }
377
378 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
379 {
380         struct ethtool_cmd cmd;
381
382         if (!dev->ethtool_ops->set_settings)
383                 return -EOPNOTSUPP;
384
385         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
386                 return -EFAULT;
387
388         return dev->ethtool_ops->set_settings(dev, &cmd);
389 }
390
391 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
392                                                   void __user *useraddr)
393 {
394         struct ethtool_drvinfo info;
395         const struct ethtool_ops *ops = dev->ethtool_ops;
396
397         memset(&info, 0, sizeof(info));
398         info.cmd = ETHTOOL_GDRVINFO;
399         if (ops->get_drvinfo) {
400                 ops->get_drvinfo(dev, &info);
401         } else if (dev->dev.parent && dev->dev.parent->driver) {
402                 strlcpy(info.bus_info, dev_name(dev->dev.parent),
403                         sizeof(info.bus_info));
404                 strlcpy(info.driver, dev->dev.parent->driver->name,
405                         sizeof(info.driver));
406         } else {
407                 return -EOPNOTSUPP;
408         }
409
410         /*
411          * this method of obtaining string set info is deprecated;
412          * Use ETHTOOL_GSSET_INFO instead.
413          */
414         if (ops->get_sset_count) {
415                 int rc;
416
417                 rc = ops->get_sset_count(dev, ETH_SS_TEST);
418                 if (rc >= 0)
419                         info.testinfo_len = rc;
420                 rc = ops->get_sset_count(dev, ETH_SS_STATS);
421                 if (rc >= 0)
422                         info.n_stats = rc;
423                 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
424                 if (rc >= 0)
425                         info.n_priv_flags = rc;
426         }
427         if (ops->get_regs_len)
428                 info.regdump_len = ops->get_regs_len(dev);
429         if (ops->get_eeprom_len)
430                 info.eedump_len = ops->get_eeprom_len(dev);
431
432         if (copy_to_user(useraddr, &info, sizeof(info)))
433                 return -EFAULT;
434         return 0;
435 }
436
437 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
438                                                     void __user *useraddr)
439 {
440         struct ethtool_sset_info info;
441         u64 sset_mask;
442         int i, idx = 0, n_bits = 0, ret, rc;
443         u32 *info_buf = NULL;
444
445         if (copy_from_user(&info, useraddr, sizeof(info)))
446                 return -EFAULT;
447
448         /* store copy of mask, because we zero struct later on */
449         sset_mask = info.sset_mask;
450         if (!sset_mask)
451                 return 0;
452
453         /* calculate size of return buffer */
454         n_bits = hweight64(sset_mask);
455
456         memset(&info, 0, sizeof(info));
457         info.cmd = ETHTOOL_GSSET_INFO;
458
459         info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
460         if (!info_buf)
461                 return -ENOMEM;
462
463         /*
464          * fill return buffer based on input bitmask and successful
465          * get_sset_count return
466          */
467         for (i = 0; i < 64; i++) {
468                 if (!(sset_mask & (1ULL << i)))
469                         continue;
470
471                 rc = __ethtool_get_sset_count(dev, i);
472                 if (rc >= 0) {
473                         info.sset_mask |= (1ULL << i);
474                         info_buf[idx++] = rc;
475                 }
476         }
477
478         ret = -EFAULT;
479         if (copy_to_user(useraddr, &info, sizeof(info)))
480                 goto out;
481
482         useraddr += offsetof(struct ethtool_sset_info, data);
483         if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
484                 goto out;
485
486         ret = 0;
487
488 out:
489         kfree(info_buf);
490         return ret;
491 }
492
493 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
494                                                 u32 cmd, void __user *useraddr)
495 {
496         struct ethtool_rxnfc info;
497         size_t info_size = sizeof(info);
498         int rc;
499
500         if (!dev->ethtool_ops->set_rxnfc)
501                 return -EOPNOTSUPP;
502
503         /* struct ethtool_rxnfc was originally defined for
504          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
505          * members.  User-space might still be using that
506          * definition. */
507         if (cmd == ETHTOOL_SRXFH)
508                 info_size = (offsetof(struct ethtool_rxnfc, data) +
509                              sizeof(info.data));
510
511         if (copy_from_user(&info, useraddr, info_size))
512                 return -EFAULT;
513
514         rc = dev->ethtool_ops->set_rxnfc(dev, &info);
515         if (rc)
516                 return rc;
517
518         if (cmd == ETHTOOL_SRXCLSRLINS &&
519             copy_to_user(useraddr, &info, info_size))
520                 return -EFAULT;
521
522         return 0;
523 }
524
525 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
526                                                 u32 cmd, void __user *useraddr)
527 {
528         struct ethtool_rxnfc info;
529         size_t info_size = sizeof(info);
530         const struct ethtool_ops *ops = dev->ethtool_ops;
531         int ret;
532         void *rule_buf = NULL;
533
534         if (!ops->get_rxnfc)
535                 return -EOPNOTSUPP;
536
537         /* struct ethtool_rxnfc was originally defined for
538          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
539          * members.  User-space might still be using that
540          * definition. */
541         if (cmd == ETHTOOL_GRXFH)
542                 info_size = (offsetof(struct ethtool_rxnfc, data) +
543                              sizeof(info.data));
544
545         if (copy_from_user(&info, useraddr, info_size))
546                 return -EFAULT;
547
548         if (info.cmd == ETHTOOL_GRXCLSRLALL) {
549                 if (info.rule_cnt > 0) {
550                         if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
551                                 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
552                                                    GFP_USER);
553                         if (!rule_buf)
554                                 return -ENOMEM;
555                 }
556         }
557
558         ret = ops->get_rxnfc(dev, &info, rule_buf);
559         if (ret < 0)
560                 goto err_out;
561
562         ret = -EFAULT;
563         if (copy_to_user(useraddr, &info, info_size))
564                 goto err_out;
565
566         if (rule_buf) {
567                 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
568                 if (copy_to_user(useraddr, rule_buf,
569                                  info.rule_cnt * sizeof(u32)))
570                         goto err_out;
571         }
572         ret = 0;
573
574 err_out:
575         kfree(rule_buf);
576
577         return ret;
578 }
579
580 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
581                                         struct ethtool_rxnfc *rx_rings,
582                                         u32 size)
583 {
584         int i;
585
586         if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
587                 return -EFAULT;
588
589         /* Validate ring indices */
590         for (i = 0; i < size; i++)
591                 if (indir[i] >= rx_rings->data)
592                         return -EINVAL;
593
594         return 0;
595 }
596
597 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN];
598
599 void netdev_rss_key_fill(void *buffer, size_t len)
600 {
601         BUG_ON(len > sizeof(netdev_rss_key));
602         net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
603         memcpy(buffer, netdev_rss_key, len);
604 }
605 EXPORT_SYMBOL(netdev_rss_key_fill);
606
607 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
608                                                      void __user *useraddr)
609 {
610         u32 user_size, dev_size;
611         u32 *indir;
612         int ret;
613
614         if (!dev->ethtool_ops->get_rxfh_indir_size ||
615             !dev->ethtool_ops->get_rxfh)
616                 return -EOPNOTSUPP;
617         dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
618         if (dev_size == 0)
619                 return -EOPNOTSUPP;
620
621         if (copy_from_user(&user_size,
622                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
623                            sizeof(user_size)))
624                 return -EFAULT;
625
626         if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
627                          &dev_size, sizeof(dev_size)))
628                 return -EFAULT;
629
630         /* If the user buffer size is 0, this is just a query for the
631          * device table size.  Otherwise, if it's smaller than the
632          * device table size it's an error.
633          */
634         if (user_size < dev_size)
635                 return user_size == 0 ? 0 : -EINVAL;
636
637         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
638         if (!indir)
639                 return -ENOMEM;
640
641         ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
642         if (ret)
643                 goto out;
644
645         if (copy_to_user(useraddr +
646                          offsetof(struct ethtool_rxfh_indir, ring_index[0]),
647                          indir, dev_size * sizeof(indir[0])))
648                 ret = -EFAULT;
649
650 out:
651         kfree(indir);
652         return ret;
653 }
654
655 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
656                                                      void __user *useraddr)
657 {
658         struct ethtool_rxnfc rx_rings;
659         u32 user_size, dev_size, i;
660         u32 *indir;
661         const struct ethtool_ops *ops = dev->ethtool_ops;
662         int ret;
663         u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
664
665         if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
666             !ops->get_rxnfc)
667                 return -EOPNOTSUPP;
668
669         dev_size = ops->get_rxfh_indir_size(dev);
670         if (dev_size == 0)
671                 return -EOPNOTSUPP;
672
673         if (copy_from_user(&user_size,
674                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
675                            sizeof(user_size)))
676                 return -EFAULT;
677
678         if (user_size != 0 && user_size != dev_size)
679                 return -EINVAL;
680
681         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
682         if (!indir)
683                 return -ENOMEM;
684
685         rx_rings.cmd = ETHTOOL_GRXRINGS;
686         ret = ops->get_rxnfc(dev, &rx_rings, NULL);
687         if (ret)
688                 goto out;
689
690         if (user_size == 0) {
691                 for (i = 0; i < dev_size; i++)
692                         indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
693         } else {
694                 ret = ethtool_copy_validate_indir(indir,
695                                                   useraddr + ringidx_offset,
696                                                   &rx_rings,
697                                                   dev_size);
698                 if (ret)
699                         goto out;
700         }
701
702         ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
703
704 out:
705         kfree(indir);
706         return ret;
707 }
708
709 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
710                                                void __user *useraddr)
711 {
712         int ret;
713         const struct ethtool_ops *ops = dev->ethtool_ops;
714         u32 user_indir_size, user_key_size;
715         u32 dev_indir_size = 0, dev_key_size = 0;
716         struct ethtool_rxfh rxfh;
717         u32 total_size;
718         u32 indir_bytes;
719         u32 *indir = NULL;
720         u8 dev_hfunc = 0;
721         u8 *hkey = NULL;
722         u8 *rss_config;
723
724         if (!ops->get_rxfh)
725                 return -EOPNOTSUPP;
726
727         if (ops->get_rxfh_indir_size)
728                 dev_indir_size = ops->get_rxfh_indir_size(dev);
729         if (ops->get_rxfh_key_size)
730                 dev_key_size = ops->get_rxfh_key_size(dev);
731
732         if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
733                 return -EFAULT;
734         user_indir_size = rxfh.indir_size;
735         user_key_size = rxfh.key_size;
736
737         /* Check that reserved fields are 0 for now */
738         if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
739             rxfh.rsvd8[2] || rxfh.rsvd32)
740                 return -EINVAL;
741
742         rxfh.indir_size = dev_indir_size;
743         rxfh.key_size = dev_key_size;
744         if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
745                 return -EFAULT;
746
747         if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
748             (user_key_size && (user_key_size != dev_key_size)))
749                 return -EINVAL;
750
751         indir_bytes = user_indir_size * sizeof(indir[0]);
752         total_size = indir_bytes + user_key_size;
753         rss_config = kzalloc(total_size, GFP_USER);
754         if (!rss_config)
755                 return -ENOMEM;
756
757         if (user_indir_size)
758                 indir = (u32 *)rss_config;
759
760         if (user_key_size)
761                 hkey = rss_config + indir_bytes;
762
763         ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
764         if (ret)
765                 goto out;
766
767         if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
768                          &dev_hfunc, sizeof(rxfh.hfunc))) {
769                 ret = -EFAULT;
770         } else if (copy_to_user(useraddr +
771                               offsetof(struct ethtool_rxfh, rss_config[0]),
772                               rss_config, total_size)) {
773                 ret = -EFAULT;
774         }
775 out:
776         kfree(rss_config);
777
778         return ret;
779 }
780
781 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
782                                                void __user *useraddr)
783 {
784         int ret;
785         const struct ethtool_ops *ops = dev->ethtool_ops;
786         struct ethtool_rxnfc rx_rings;
787         struct ethtool_rxfh rxfh;
788         u32 dev_indir_size = 0, dev_key_size = 0, i;
789         u32 *indir = NULL, indir_bytes = 0;
790         u8 *hkey = NULL;
791         u8 *rss_config;
792         u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
793
794         if (!ops->get_rxnfc || !ops->set_rxfh)
795                 return -EOPNOTSUPP;
796
797         if (ops->get_rxfh_indir_size)
798                 dev_indir_size = ops->get_rxfh_indir_size(dev);
799         if (ops->get_rxfh_key_size)
800                 dev_key_size = ops->get_rxfh_key_size(dev);
801
802         if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
803                 return -EFAULT;
804
805         /* Check that reserved fields are 0 for now */
806         if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
807             rxfh.rsvd8[2] || rxfh.rsvd32)
808                 return -EINVAL;
809
810         /* If either indir, hash key or function is valid, proceed further.
811          * Must request at least one change: indir size, hash key or function.
812          */
813         if ((rxfh.indir_size &&
814              rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
815              rxfh.indir_size != dev_indir_size) ||
816             (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
817             (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
818              rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
819                 return -EINVAL;
820
821         if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
822                 indir_bytes = dev_indir_size * sizeof(indir[0]);
823
824         rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
825         if (!rss_config)
826                 return -ENOMEM;
827
828         rx_rings.cmd = ETHTOOL_GRXRINGS;
829         ret = ops->get_rxnfc(dev, &rx_rings, NULL);
830         if (ret)
831                 goto out;
832
833         /* rxfh.indir_size == 0 means reset the indir table to default.
834          * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
835          */
836         if (rxfh.indir_size &&
837             rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
838                 indir = (u32 *)rss_config;
839                 ret = ethtool_copy_validate_indir(indir,
840                                                   useraddr + rss_cfg_offset,
841                                                   &rx_rings,
842                                                   rxfh.indir_size);
843                 if (ret)
844                         goto out;
845         } else if (rxfh.indir_size == 0) {
846                 indir = (u32 *)rss_config;
847                 for (i = 0; i < dev_indir_size; i++)
848                         indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
849         }
850
851         if (rxfh.key_size) {
852                 hkey = rss_config + indir_bytes;
853                 if (copy_from_user(hkey,
854                                    useraddr + rss_cfg_offset + indir_bytes,
855                                    rxfh.key_size)) {
856                         ret = -EFAULT;
857                         goto out;
858                 }
859         }
860
861         ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
862
863 out:
864         kfree(rss_config);
865         return ret;
866 }
867
868 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
869 {
870         struct ethtool_regs regs;
871         const struct ethtool_ops *ops = dev->ethtool_ops;
872         void *regbuf;
873         int reglen, ret;
874
875         if (!ops->get_regs || !ops->get_regs_len)
876                 return -EOPNOTSUPP;
877
878         if (copy_from_user(&regs, useraddr, sizeof(regs)))
879                 return -EFAULT;
880
881         reglen = ops->get_regs_len(dev);
882         if (regs.len > reglen)
883                 regs.len = reglen;
884
885         regbuf = vzalloc(reglen);
886         if (reglen && !regbuf)
887                 return -ENOMEM;
888
889         ops->get_regs(dev, &regs, regbuf);
890
891         ret = -EFAULT;
892         if (copy_to_user(useraddr, &regs, sizeof(regs)))
893                 goto out;
894         useraddr += offsetof(struct ethtool_regs, data);
895         if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
896                 goto out;
897         ret = 0;
898
899  out:
900         vfree(regbuf);
901         return ret;
902 }
903
904 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
905 {
906         struct ethtool_value reset;
907         int ret;
908
909         if (!dev->ethtool_ops->reset)
910                 return -EOPNOTSUPP;
911
912         if (copy_from_user(&reset, useraddr, sizeof(reset)))
913                 return -EFAULT;
914
915         ret = dev->ethtool_ops->reset(dev, &reset.data);
916         if (ret)
917                 return ret;
918
919         if (copy_to_user(useraddr, &reset, sizeof(reset)))
920                 return -EFAULT;
921         return 0;
922 }
923
924 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
925 {
926         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
927
928         if (!dev->ethtool_ops->get_wol)
929                 return -EOPNOTSUPP;
930
931         dev->ethtool_ops->get_wol(dev, &wol);
932
933         if (copy_to_user(useraddr, &wol, sizeof(wol)))
934                 return -EFAULT;
935         return 0;
936 }
937
938 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
939 {
940         struct ethtool_wolinfo wol;
941
942         if (!dev->ethtool_ops->set_wol)
943                 return -EOPNOTSUPP;
944
945         if (copy_from_user(&wol, useraddr, sizeof(wol)))
946                 return -EFAULT;
947
948         return dev->ethtool_ops->set_wol(dev, &wol);
949 }
950
951 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
952 {
953         struct ethtool_eee edata;
954         int rc;
955
956         if (!dev->ethtool_ops->get_eee)
957                 return -EOPNOTSUPP;
958
959         memset(&edata, 0, sizeof(struct ethtool_eee));
960         edata.cmd = ETHTOOL_GEEE;
961         rc = dev->ethtool_ops->get_eee(dev, &edata);
962
963         if (rc)
964                 return rc;
965
966         if (copy_to_user(useraddr, &edata, sizeof(edata)))
967                 return -EFAULT;
968
969         return 0;
970 }
971
972 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
973 {
974         struct ethtool_eee edata;
975
976         if (!dev->ethtool_ops->set_eee)
977                 return -EOPNOTSUPP;
978
979         if (copy_from_user(&edata, useraddr, sizeof(edata)))
980                 return -EFAULT;
981
982         return dev->ethtool_ops->set_eee(dev, &edata);
983 }
984
985 static int ethtool_nway_reset(struct net_device *dev)
986 {
987         if (!dev->ethtool_ops->nway_reset)
988                 return -EOPNOTSUPP;
989
990         return dev->ethtool_ops->nway_reset(dev);
991 }
992
993 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
994 {
995         struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
996
997         if (!dev->ethtool_ops->get_link)
998                 return -EOPNOTSUPP;
999
1000         edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1001
1002         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1003                 return -EFAULT;
1004         return 0;
1005 }
1006
1007 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1008                                   int (*getter)(struct net_device *,
1009                                                 struct ethtool_eeprom *, u8 *),
1010                                   u32 total_len)
1011 {
1012         struct ethtool_eeprom eeprom;
1013         void __user *userbuf = useraddr + sizeof(eeprom);
1014         u32 bytes_remaining;
1015         u8 *data;
1016         int ret = 0;
1017
1018         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1019                 return -EFAULT;
1020
1021         /* Check for wrap and zero */
1022         if (eeprom.offset + eeprom.len <= eeprom.offset)
1023                 return -EINVAL;
1024
1025         /* Check for exceeding total eeprom len */
1026         if (eeprom.offset + eeprom.len > total_len)
1027                 return -EINVAL;
1028
1029         data = kmalloc(PAGE_SIZE, GFP_USER);
1030         if (!data)
1031                 return -ENOMEM;
1032
1033         bytes_remaining = eeprom.len;
1034         while (bytes_remaining > 0) {
1035                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1036
1037                 ret = getter(dev, &eeprom, data);
1038                 if (ret)
1039                         break;
1040                 if (copy_to_user(userbuf, data, eeprom.len)) {
1041                         ret = -EFAULT;
1042                         break;
1043                 }
1044                 userbuf += eeprom.len;
1045                 eeprom.offset += eeprom.len;
1046                 bytes_remaining -= eeprom.len;
1047         }
1048
1049         eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1050         eeprom.offset -= eeprom.len;
1051         if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1052                 ret = -EFAULT;
1053
1054         kfree(data);
1055         return ret;
1056 }
1057
1058 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1059 {
1060         const struct ethtool_ops *ops = dev->ethtool_ops;
1061
1062         if (!ops->get_eeprom || !ops->get_eeprom_len ||
1063             !ops->get_eeprom_len(dev))
1064                 return -EOPNOTSUPP;
1065
1066         return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1067                                       ops->get_eeprom_len(dev));
1068 }
1069
1070 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1071 {
1072         struct ethtool_eeprom eeprom;
1073         const struct ethtool_ops *ops = dev->ethtool_ops;
1074         void __user *userbuf = useraddr + sizeof(eeprom);
1075         u32 bytes_remaining;
1076         u8 *data;
1077         int ret = 0;
1078
1079         if (!ops->set_eeprom || !ops->get_eeprom_len ||
1080             !ops->get_eeprom_len(dev))
1081                 return -EOPNOTSUPP;
1082
1083         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1084                 return -EFAULT;
1085
1086         /* Check for wrap and zero */
1087         if (eeprom.offset + eeprom.len <= eeprom.offset)
1088                 return -EINVAL;
1089
1090         /* Check for exceeding total eeprom len */
1091         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1092                 return -EINVAL;
1093
1094         data = kmalloc(PAGE_SIZE, GFP_USER);
1095         if (!data)
1096                 return -ENOMEM;
1097
1098         bytes_remaining = eeprom.len;
1099         while (bytes_remaining > 0) {
1100                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1101
1102                 if (copy_from_user(data, userbuf, eeprom.len)) {
1103                         ret = -EFAULT;
1104                         break;
1105                 }
1106                 ret = ops->set_eeprom(dev, &eeprom, data);
1107                 if (ret)
1108                         break;
1109                 userbuf += eeprom.len;
1110                 eeprom.offset += eeprom.len;
1111                 bytes_remaining -= eeprom.len;
1112         }
1113
1114         kfree(data);
1115         return ret;
1116 }
1117
1118 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1119                                                    void __user *useraddr)
1120 {
1121         struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1122
1123         if (!dev->ethtool_ops->get_coalesce)
1124                 return -EOPNOTSUPP;
1125
1126         dev->ethtool_ops->get_coalesce(dev, &coalesce);
1127
1128         if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1129                 return -EFAULT;
1130         return 0;
1131 }
1132
1133 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1134                                                    void __user *useraddr)
1135 {
1136         struct ethtool_coalesce coalesce;
1137
1138         if (!dev->ethtool_ops->set_coalesce)
1139                 return -EOPNOTSUPP;
1140
1141         if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1142                 return -EFAULT;
1143
1144         return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1145 }
1146
1147 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1148 {
1149         struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1150
1151         if (!dev->ethtool_ops->get_ringparam)
1152                 return -EOPNOTSUPP;
1153
1154         dev->ethtool_ops->get_ringparam(dev, &ringparam);
1155
1156         if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1157                 return -EFAULT;
1158         return 0;
1159 }
1160
1161 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1162 {
1163         struct ethtool_ringparam ringparam;
1164
1165         if (!dev->ethtool_ops->set_ringparam)
1166                 return -EOPNOTSUPP;
1167
1168         if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1169                 return -EFAULT;
1170
1171         return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1172 }
1173
1174 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1175                                                    void __user *useraddr)
1176 {
1177         struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1178
1179         if (!dev->ethtool_ops->get_channels)
1180                 return -EOPNOTSUPP;
1181
1182         dev->ethtool_ops->get_channels(dev, &channels);
1183
1184         if (copy_to_user(useraddr, &channels, sizeof(channels)))
1185                 return -EFAULT;
1186         return 0;
1187 }
1188
1189 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1190                                                    void __user *useraddr)
1191 {
1192         struct ethtool_channels channels;
1193
1194         if (!dev->ethtool_ops->set_channels)
1195                 return -EOPNOTSUPP;
1196
1197         if (copy_from_user(&channels, useraddr, sizeof(channels)))
1198                 return -EFAULT;
1199
1200         return dev->ethtool_ops->set_channels(dev, &channels);
1201 }
1202
1203 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1204 {
1205         struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1206
1207         if (!dev->ethtool_ops->get_pauseparam)
1208                 return -EOPNOTSUPP;
1209
1210         dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1211
1212         if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1213                 return -EFAULT;
1214         return 0;
1215 }
1216
1217 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1218 {
1219         struct ethtool_pauseparam pauseparam;
1220
1221         if (!dev->ethtool_ops->set_pauseparam)
1222                 return -EOPNOTSUPP;
1223
1224         if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1225                 return -EFAULT;
1226
1227         return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1228 }
1229
1230 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1231 {
1232         struct ethtool_test test;
1233         const struct ethtool_ops *ops = dev->ethtool_ops;
1234         u64 *data;
1235         int ret, test_len;
1236
1237         if (!ops->self_test || !ops->get_sset_count)
1238                 return -EOPNOTSUPP;
1239
1240         test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1241         if (test_len < 0)
1242                 return test_len;
1243         WARN_ON(test_len == 0);
1244
1245         if (copy_from_user(&test, useraddr, sizeof(test)))
1246                 return -EFAULT;
1247
1248         test.len = test_len;
1249         data = kmalloc(test_len * sizeof(u64), GFP_USER);
1250         if (!data)
1251                 return -ENOMEM;
1252
1253         ops->self_test(dev, &test, data);
1254
1255         ret = -EFAULT;
1256         if (copy_to_user(useraddr, &test, sizeof(test)))
1257                 goto out;
1258         useraddr += sizeof(test);
1259         if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1260                 goto out;
1261         ret = 0;
1262
1263  out:
1264         kfree(data);
1265         return ret;
1266 }
1267
1268 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1269 {
1270         struct ethtool_gstrings gstrings;
1271         u8 *data;
1272         int ret;
1273
1274         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1275                 return -EFAULT;
1276
1277         ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1278         if (ret < 0)
1279                 return ret;
1280
1281         gstrings.len = ret;
1282
1283         data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1284         if (!data)
1285                 return -ENOMEM;
1286
1287         __ethtool_get_strings(dev, gstrings.string_set, data);
1288
1289         ret = -EFAULT;
1290         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1291                 goto out;
1292         useraddr += sizeof(gstrings);
1293         if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1294                 goto out;
1295         ret = 0;
1296
1297 out:
1298         kfree(data);
1299         return ret;
1300 }
1301
1302 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1303 {
1304         struct ethtool_value id;
1305         static bool busy;
1306         const struct ethtool_ops *ops = dev->ethtool_ops;
1307         int rc;
1308
1309         if (!ops->set_phys_id)
1310                 return -EOPNOTSUPP;
1311
1312         if (busy)
1313                 return -EBUSY;
1314
1315         if (copy_from_user(&id, useraddr, sizeof(id)))
1316                 return -EFAULT;
1317
1318         rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1319         if (rc < 0)
1320                 return rc;
1321
1322         /* Drop the RTNL lock while waiting, but prevent reentry or
1323          * removal of the device.
1324          */
1325         busy = true;
1326         dev_hold(dev);
1327         rtnl_unlock();
1328
1329         if (rc == 0) {
1330                 /* Driver will handle this itself */
1331                 schedule_timeout_interruptible(
1332                         id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1333         } else {
1334                 /* Driver expects to be called at twice the frequency in rc */
1335                 int n = rc * 2, i, interval = HZ / n;
1336
1337                 /* Count down seconds */
1338                 do {
1339                         /* Count down iterations per second */
1340                         i = n;
1341                         do {
1342                                 rtnl_lock();
1343                                 rc = ops->set_phys_id(dev,
1344                                     (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1345                                 rtnl_unlock();
1346                                 if (rc)
1347                                         break;
1348                                 schedule_timeout_interruptible(interval);
1349                         } while (!signal_pending(current) && --i != 0);
1350                 } while (!signal_pending(current) &&
1351                          (id.data == 0 || --id.data != 0));
1352         }
1353
1354         rtnl_lock();
1355         dev_put(dev);
1356         busy = false;
1357
1358         (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1359         return rc;
1360 }
1361
1362 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1363 {
1364         struct ethtool_stats stats;
1365         const struct ethtool_ops *ops = dev->ethtool_ops;
1366         u64 *data;
1367         int ret, n_stats;
1368
1369         if (!ops->get_ethtool_stats || !ops->get_sset_count)
1370                 return -EOPNOTSUPP;
1371
1372         n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1373         if (n_stats < 0)
1374                 return n_stats;
1375         WARN_ON(n_stats == 0);
1376
1377         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1378                 return -EFAULT;
1379
1380         stats.n_stats = n_stats;
1381         data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1382         if (!data)
1383                 return -ENOMEM;
1384
1385         ops->get_ethtool_stats(dev, &stats, data);
1386
1387         ret = -EFAULT;
1388         if (copy_to_user(useraddr, &stats, sizeof(stats)))
1389                 goto out;
1390         useraddr += sizeof(stats);
1391         if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1392                 goto out;
1393         ret = 0;
1394
1395  out:
1396         kfree(data);
1397         return ret;
1398 }
1399
1400 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1401 {
1402         struct ethtool_perm_addr epaddr;
1403
1404         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1405                 return -EFAULT;
1406
1407         if (epaddr.size < dev->addr_len)
1408                 return -ETOOSMALL;
1409         epaddr.size = dev->addr_len;
1410
1411         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1412                 return -EFAULT;
1413         useraddr += sizeof(epaddr);
1414         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1415                 return -EFAULT;
1416         return 0;
1417 }
1418
1419 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1420                              u32 cmd, u32 (*actor)(struct net_device *))
1421 {
1422         struct ethtool_value edata = { .cmd = cmd };
1423
1424         if (!actor)
1425                 return -EOPNOTSUPP;
1426
1427         edata.data = actor(dev);
1428
1429         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1430                 return -EFAULT;
1431         return 0;
1432 }
1433
1434 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1435                              void (*actor)(struct net_device *, u32))
1436 {
1437         struct ethtool_value edata;
1438
1439         if (!actor)
1440                 return -EOPNOTSUPP;
1441
1442         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1443                 return -EFAULT;
1444
1445         actor(dev, edata.data);
1446         return 0;
1447 }
1448
1449 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1450                              int (*actor)(struct net_device *, u32))
1451 {
1452         struct ethtool_value edata;
1453
1454         if (!actor)
1455                 return -EOPNOTSUPP;
1456
1457         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1458                 return -EFAULT;
1459
1460         return actor(dev, edata.data);
1461 }
1462
1463 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1464                                                    char __user *useraddr)
1465 {
1466         struct ethtool_flash efl;
1467
1468         if (copy_from_user(&efl, useraddr, sizeof(efl)))
1469                 return -EFAULT;
1470
1471         if (!dev->ethtool_ops->flash_device)
1472                 return -EOPNOTSUPP;
1473
1474         efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1475
1476         return dev->ethtool_ops->flash_device(dev, &efl);
1477 }
1478
1479 static int ethtool_set_dump(struct net_device *dev,
1480                         void __user *useraddr)
1481 {
1482         struct ethtool_dump dump;
1483
1484         if (!dev->ethtool_ops->set_dump)
1485                 return -EOPNOTSUPP;
1486
1487         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1488                 return -EFAULT;
1489
1490         return dev->ethtool_ops->set_dump(dev, &dump);
1491 }
1492
1493 static int ethtool_get_dump_flag(struct net_device *dev,
1494                                 void __user *useraddr)
1495 {
1496         int ret;
1497         struct ethtool_dump dump;
1498         const struct ethtool_ops *ops = dev->ethtool_ops;
1499
1500         if (!ops->get_dump_flag)
1501                 return -EOPNOTSUPP;
1502
1503         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1504                 return -EFAULT;
1505
1506         ret = ops->get_dump_flag(dev, &dump);
1507         if (ret)
1508                 return ret;
1509
1510         if (copy_to_user(useraddr, &dump, sizeof(dump)))
1511                 return -EFAULT;
1512         return 0;
1513 }
1514
1515 static int ethtool_get_dump_data(struct net_device *dev,
1516                                 void __user *useraddr)
1517 {
1518         int ret;
1519         __u32 len;
1520         struct ethtool_dump dump, tmp;
1521         const struct ethtool_ops *ops = dev->ethtool_ops;
1522         void *data = NULL;
1523
1524         if (!ops->get_dump_data || !ops->get_dump_flag)
1525                 return -EOPNOTSUPP;
1526
1527         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1528                 return -EFAULT;
1529
1530         memset(&tmp, 0, sizeof(tmp));
1531         tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1532         ret = ops->get_dump_flag(dev, &tmp);
1533         if (ret)
1534                 return ret;
1535
1536         len = min(tmp.len, dump.len);
1537         if (!len)
1538                 return -EFAULT;
1539
1540         /* Don't ever let the driver think there's more space available
1541          * than it requested with .get_dump_flag().
1542          */
1543         dump.len = len;
1544
1545         /* Always allocate enough space to hold the whole thing so that the
1546          * driver does not need to check the length and bother with partial
1547          * dumping.
1548          */
1549         data = vzalloc(tmp.len);
1550         if (!data)
1551                 return -ENOMEM;
1552         ret = ops->get_dump_data(dev, &dump, data);
1553         if (ret)
1554                 goto out;
1555
1556         /* There are two sane possibilities:
1557          * 1. The driver's .get_dump_data() does not touch dump.len.
1558          * 2. Or it may set dump.len to how much it really writes, which
1559          *    should be tmp.len (or len if it can do a partial dump).
1560          * In any case respond to userspace with the actual length of data
1561          * it's receiving.
1562          */
1563         WARN_ON(dump.len != len && dump.len != tmp.len);
1564         dump.len = len;
1565
1566         if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1567                 ret = -EFAULT;
1568                 goto out;
1569         }
1570         useraddr += offsetof(struct ethtool_dump, data);
1571         if (copy_to_user(useraddr, data, len))
1572                 ret = -EFAULT;
1573 out:
1574         vfree(data);
1575         return ret;
1576 }
1577
1578 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1579 {
1580         int err = 0;
1581         struct ethtool_ts_info info;
1582         const struct ethtool_ops *ops = dev->ethtool_ops;
1583         struct phy_device *phydev = dev->phydev;
1584
1585         memset(&info, 0, sizeof(info));
1586         info.cmd = ETHTOOL_GET_TS_INFO;
1587
1588         if (phydev && phydev->drv && phydev->drv->ts_info) {
1589                 err = phydev->drv->ts_info(phydev, &info);
1590         } else if (ops->get_ts_info) {
1591                 err = ops->get_ts_info(dev, &info);
1592         } else {
1593                 info.so_timestamping =
1594                         SOF_TIMESTAMPING_RX_SOFTWARE |
1595                         SOF_TIMESTAMPING_SOFTWARE;
1596                 info.phc_index = -1;
1597         }
1598
1599         if (err)
1600                 return err;
1601
1602         if (copy_to_user(useraddr, &info, sizeof(info)))
1603                 err = -EFAULT;
1604
1605         return err;
1606 }
1607
1608 static int __ethtool_get_module_info(struct net_device *dev,
1609                                      struct ethtool_modinfo *modinfo)
1610 {
1611         const struct ethtool_ops *ops = dev->ethtool_ops;
1612         struct phy_device *phydev = dev->phydev;
1613
1614         if (phydev && phydev->drv && phydev->drv->module_info)
1615                 return phydev->drv->module_info(phydev, modinfo);
1616
1617         if (ops->get_module_info)
1618                 return ops->get_module_info(dev, modinfo);
1619
1620         return -EOPNOTSUPP;
1621 }
1622
1623 static int ethtool_get_module_info(struct net_device *dev,
1624                                    void __user *useraddr)
1625 {
1626         int ret;
1627         struct ethtool_modinfo modinfo;
1628
1629         if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1630                 return -EFAULT;
1631
1632         ret = __ethtool_get_module_info(dev, &modinfo);
1633         if (ret)
1634                 return ret;
1635
1636         if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1637                 return -EFAULT;
1638
1639         return 0;
1640 }
1641
1642 static int __ethtool_get_module_eeprom(struct net_device *dev,
1643                                        struct ethtool_eeprom *ee, u8 *data)
1644 {
1645         const struct ethtool_ops *ops = dev->ethtool_ops;
1646         struct phy_device *phydev = dev->phydev;
1647
1648         if (phydev && phydev->drv && phydev->drv->module_eeprom)
1649                 return phydev->drv->module_eeprom(phydev, ee, data);
1650
1651         if (ops->get_module_eeprom)
1652                 return ops->get_module_eeprom(dev, ee, data);
1653
1654         return -EOPNOTSUPP;
1655 }
1656
1657 static int ethtool_get_module_eeprom(struct net_device *dev,
1658                                      void __user *useraddr)
1659 {
1660         int ret;
1661         struct ethtool_modinfo modinfo;
1662
1663         ret = __ethtool_get_module_info(dev, &modinfo);
1664         if (ret)
1665                 return ret;
1666
1667         return ethtool_get_any_eeprom(dev, useraddr,
1668                                       __ethtool_get_module_eeprom,
1669                                       modinfo.eeprom_len);
1670 }
1671
1672 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
1673 {
1674         switch (tuna->id) {
1675         case ETHTOOL_RX_COPYBREAK:
1676         case ETHTOOL_TX_COPYBREAK:
1677                 if (tuna->len != sizeof(u32) ||
1678                     tuna->type_id != ETHTOOL_TUNABLE_U32)
1679                         return -EINVAL;
1680                 break;
1681         default:
1682                 return -EINVAL;
1683         }
1684
1685         return 0;
1686 }
1687
1688 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
1689 {
1690         int ret;
1691         struct ethtool_tunable tuna;
1692         const struct ethtool_ops *ops = dev->ethtool_ops;
1693         void *data;
1694
1695         if (!ops->get_tunable)
1696                 return -EOPNOTSUPP;
1697         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1698                 return -EFAULT;
1699         ret = ethtool_tunable_valid(&tuna);
1700         if (ret)
1701                 return ret;
1702         data = kmalloc(tuna.len, GFP_USER);
1703         if (!data)
1704                 return -ENOMEM;
1705         ret = ops->get_tunable(dev, &tuna, data);
1706         if (ret)
1707                 goto out;
1708         useraddr += sizeof(tuna);
1709         ret = -EFAULT;
1710         if (copy_to_user(useraddr, data, tuna.len))
1711                 goto out;
1712         ret = 0;
1713
1714 out:
1715         kfree(data);
1716         return ret;
1717 }
1718
1719 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
1720 {
1721         int ret;
1722         struct ethtool_tunable tuna;
1723         const struct ethtool_ops *ops = dev->ethtool_ops;
1724         void *data;
1725
1726         if (!ops->set_tunable)
1727                 return -EOPNOTSUPP;
1728         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
1729                 return -EFAULT;
1730         ret = ethtool_tunable_valid(&tuna);
1731         if (ret)
1732                 return ret;
1733         data = kmalloc(tuna.len, GFP_USER);
1734         if (!data)
1735                 return -ENOMEM;
1736         useraddr += sizeof(tuna);
1737         ret = -EFAULT;
1738         if (copy_from_user(data, useraddr, tuna.len))
1739                 goto out;
1740         ret = ops->set_tunable(dev, &tuna, data);
1741
1742 out:
1743         kfree(data);
1744         return ret;
1745 }
1746
1747 /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
1748
1749 int dev_ethtool(struct net *net, struct ifreq *ifr)
1750 {
1751         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1752         void __user *useraddr = ifr->ifr_data;
1753         u32 ethcmd;
1754         int rc;
1755         netdev_features_t old_features;
1756
1757         if (!dev || !netif_device_present(dev))
1758                 return -ENODEV;
1759
1760         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1761                 return -EFAULT;
1762
1763         /* Allow some commands to be done by anyone */
1764         switch (ethcmd) {
1765         case ETHTOOL_GSET:
1766         case ETHTOOL_GDRVINFO:
1767         case ETHTOOL_GMSGLVL:
1768         case ETHTOOL_GLINK:
1769         case ETHTOOL_GCOALESCE:
1770         case ETHTOOL_GRINGPARAM:
1771         case ETHTOOL_GPAUSEPARAM:
1772         case ETHTOOL_GRXCSUM:
1773         case ETHTOOL_GTXCSUM:
1774         case ETHTOOL_GSG:
1775         case ETHTOOL_GSSET_INFO:
1776         case ETHTOOL_GSTRINGS:
1777         case ETHTOOL_GSTATS:
1778         case ETHTOOL_GTSO:
1779         case ETHTOOL_GPERMADDR:
1780         case ETHTOOL_GUFO:
1781         case ETHTOOL_GGSO:
1782         case ETHTOOL_GGRO:
1783         case ETHTOOL_GFLAGS:
1784         case ETHTOOL_GPFLAGS:
1785         case ETHTOOL_GRXFH:
1786         case ETHTOOL_GRXRINGS:
1787         case ETHTOOL_GRXCLSRLCNT:
1788         case ETHTOOL_GRXCLSRULE:
1789         case ETHTOOL_GRXCLSRLALL:
1790         case ETHTOOL_GRXFHINDIR:
1791         case ETHTOOL_GRSSH:
1792         case ETHTOOL_GFEATURES:
1793         case ETHTOOL_GCHANNELS:
1794         case ETHTOOL_GET_TS_INFO:
1795         case ETHTOOL_GEEE:
1796         case ETHTOOL_GTUNABLE:
1797                 break;
1798         default:
1799                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1800                         return -EPERM;
1801         }
1802
1803         if (dev->ethtool_ops->begin) {
1804                 rc = dev->ethtool_ops->begin(dev);
1805                 if (rc  < 0)
1806                         return rc;
1807         }
1808         old_features = dev->features;
1809
1810         switch (ethcmd) {
1811         case ETHTOOL_GSET:
1812                 rc = ethtool_get_settings(dev, useraddr);
1813                 break;
1814         case ETHTOOL_SSET:
1815                 rc = ethtool_set_settings(dev, useraddr);
1816                 break;
1817         case ETHTOOL_GDRVINFO:
1818                 rc = ethtool_get_drvinfo(dev, useraddr);
1819                 break;
1820         case ETHTOOL_GREGS:
1821                 rc = ethtool_get_regs(dev, useraddr);
1822                 break;
1823         case ETHTOOL_GWOL:
1824                 rc = ethtool_get_wol(dev, useraddr);
1825                 break;
1826         case ETHTOOL_SWOL:
1827                 rc = ethtool_set_wol(dev, useraddr);
1828                 break;
1829         case ETHTOOL_GMSGLVL:
1830                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1831                                        dev->ethtool_ops->get_msglevel);
1832                 break;
1833         case ETHTOOL_SMSGLVL:
1834                 rc = ethtool_set_value_void(dev, useraddr,
1835                                        dev->ethtool_ops->set_msglevel);
1836                 break;
1837         case ETHTOOL_GEEE:
1838                 rc = ethtool_get_eee(dev, useraddr);
1839                 break;
1840         case ETHTOOL_SEEE:
1841                 rc = ethtool_set_eee(dev, useraddr);
1842                 break;
1843         case ETHTOOL_NWAY_RST:
1844                 rc = ethtool_nway_reset(dev);
1845                 break;
1846         case ETHTOOL_GLINK:
1847                 rc = ethtool_get_link(dev, useraddr);
1848                 break;
1849         case ETHTOOL_GEEPROM:
1850                 rc = ethtool_get_eeprom(dev, useraddr);
1851                 break;
1852         case ETHTOOL_SEEPROM:
1853                 rc = ethtool_set_eeprom(dev, useraddr);
1854                 break;
1855         case ETHTOOL_GCOALESCE:
1856                 rc = ethtool_get_coalesce(dev, useraddr);
1857                 break;
1858         case ETHTOOL_SCOALESCE:
1859                 rc = ethtool_set_coalesce(dev, useraddr);
1860                 break;
1861         case ETHTOOL_GRINGPARAM:
1862                 rc = ethtool_get_ringparam(dev, useraddr);
1863                 break;
1864         case ETHTOOL_SRINGPARAM:
1865                 rc = ethtool_set_ringparam(dev, useraddr);
1866                 break;
1867         case ETHTOOL_GPAUSEPARAM:
1868                 rc = ethtool_get_pauseparam(dev, useraddr);
1869                 break;
1870         case ETHTOOL_SPAUSEPARAM:
1871                 rc = ethtool_set_pauseparam(dev, useraddr);
1872                 break;
1873         case ETHTOOL_TEST:
1874                 rc = ethtool_self_test(dev, useraddr);
1875                 break;
1876         case ETHTOOL_GSTRINGS:
1877                 rc = ethtool_get_strings(dev, useraddr);
1878                 break;
1879         case ETHTOOL_PHYS_ID:
1880                 rc = ethtool_phys_id(dev, useraddr);
1881                 break;
1882         case ETHTOOL_GSTATS:
1883                 rc = ethtool_get_stats(dev, useraddr);
1884                 break;
1885         case ETHTOOL_GPERMADDR:
1886                 rc = ethtool_get_perm_addr(dev, useraddr);
1887                 break;
1888         case ETHTOOL_GFLAGS:
1889                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1890                                         __ethtool_get_flags);
1891                 break;
1892         case ETHTOOL_SFLAGS:
1893                 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1894                 break;
1895         case ETHTOOL_GPFLAGS:
1896                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1897                                        dev->ethtool_ops->get_priv_flags);
1898                 break;
1899         case ETHTOOL_SPFLAGS:
1900                 rc = ethtool_set_value(dev, useraddr,
1901                                        dev->ethtool_ops->set_priv_flags);
1902                 break;
1903         case ETHTOOL_GRXFH:
1904         case ETHTOOL_GRXRINGS:
1905         case ETHTOOL_GRXCLSRLCNT:
1906         case ETHTOOL_GRXCLSRULE:
1907         case ETHTOOL_GRXCLSRLALL:
1908                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1909                 break;
1910         case ETHTOOL_SRXFH:
1911         case ETHTOOL_SRXCLSRLDEL:
1912         case ETHTOOL_SRXCLSRLINS:
1913                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1914                 break;
1915         case ETHTOOL_FLASHDEV:
1916                 rc = ethtool_flash_device(dev, useraddr);
1917                 break;
1918         case ETHTOOL_RESET:
1919                 rc = ethtool_reset(dev, useraddr);
1920                 break;
1921         case ETHTOOL_GSSET_INFO:
1922                 rc = ethtool_get_sset_info(dev, useraddr);
1923                 break;
1924         case ETHTOOL_GRXFHINDIR:
1925                 rc = ethtool_get_rxfh_indir(dev, useraddr);
1926                 break;
1927         case ETHTOOL_SRXFHINDIR:
1928                 rc = ethtool_set_rxfh_indir(dev, useraddr);
1929                 break;
1930         case ETHTOOL_GRSSH:
1931                 rc = ethtool_get_rxfh(dev, useraddr);
1932                 break;
1933         case ETHTOOL_SRSSH:
1934                 rc = ethtool_set_rxfh(dev, useraddr);
1935                 break;
1936         case ETHTOOL_GFEATURES:
1937                 rc = ethtool_get_features(dev, useraddr);
1938                 break;
1939         case ETHTOOL_SFEATURES:
1940                 rc = ethtool_set_features(dev, useraddr);
1941                 break;
1942         case ETHTOOL_GTXCSUM:
1943         case ETHTOOL_GRXCSUM:
1944         case ETHTOOL_GSG:
1945         case ETHTOOL_GTSO:
1946         case ETHTOOL_GUFO:
1947         case ETHTOOL_GGSO:
1948         case ETHTOOL_GGRO:
1949                 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1950                 break;
1951         case ETHTOOL_STXCSUM:
1952         case ETHTOOL_SRXCSUM:
1953         case ETHTOOL_SSG:
1954         case ETHTOOL_STSO:
1955         case ETHTOOL_SUFO:
1956         case ETHTOOL_SGSO:
1957         case ETHTOOL_SGRO:
1958                 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1959                 break;
1960         case ETHTOOL_GCHANNELS:
1961                 rc = ethtool_get_channels(dev, useraddr);
1962                 break;
1963         case ETHTOOL_SCHANNELS:
1964                 rc = ethtool_set_channels(dev, useraddr);
1965                 break;
1966         case ETHTOOL_SET_DUMP:
1967                 rc = ethtool_set_dump(dev, useraddr);
1968                 break;
1969         case ETHTOOL_GET_DUMP_FLAG:
1970                 rc = ethtool_get_dump_flag(dev, useraddr);
1971                 break;
1972         case ETHTOOL_GET_DUMP_DATA:
1973                 rc = ethtool_get_dump_data(dev, useraddr);
1974                 break;
1975         case ETHTOOL_GET_TS_INFO:
1976                 rc = ethtool_get_ts_info(dev, useraddr);
1977                 break;
1978         case ETHTOOL_GMODULEINFO:
1979                 rc = ethtool_get_module_info(dev, useraddr);
1980                 break;
1981         case ETHTOOL_GMODULEEEPROM:
1982                 rc = ethtool_get_module_eeprom(dev, useraddr);
1983                 break;
1984         case ETHTOOL_GTUNABLE:
1985                 rc = ethtool_get_tunable(dev, useraddr);
1986                 break;
1987         case ETHTOOL_STUNABLE:
1988                 rc = ethtool_set_tunable(dev, useraddr);
1989                 break;
1990         default:
1991                 rc = -EOPNOTSUPP;
1992         }
1993
1994         if (dev->ethtool_ops->complete)
1995                 dev->ethtool_ops->complete(dev);
1996
1997         if (old_features != dev->features)
1998                 netdev_features_change(dev);
1999
2000         return rc;
2001 }