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