]> asedeno.scripts.mit.edu Git - linux.git/blob - net/core/ethtool.c
b849fdae7e87ec517ef2a8d72c161564590a58a2
[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/sfp.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sched/signal.h>
29 #include <linux/net.h>
30
31 /*
32  * Some useful ethtool_ops methods that're device independent.
33  * If we find that all drivers want to do the same thing here,
34  * we can turn these into dev_() function calls.
35  */
36
37 u32 ethtool_op_get_link(struct net_device *dev)
38 {
39         return netif_carrier_ok(dev) ? 1 : 0;
40 }
41 EXPORT_SYMBOL(ethtool_op_get_link);
42
43 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44 {
45         info->so_timestamping =
46                 SOF_TIMESTAMPING_TX_SOFTWARE |
47                 SOF_TIMESTAMPING_RX_SOFTWARE |
48                 SOF_TIMESTAMPING_SOFTWARE;
49         info->phc_index = -1;
50         return 0;
51 }
52 EXPORT_SYMBOL(ethtool_op_get_ts_info);
53
54 /* Handlers for each ethtool command */
55
56 #define ETHTOOL_DEV_FEATURE_WORDS       ((NETDEV_FEATURE_COUNT + 31) / 32)
57
58 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59         [NETIF_F_SG_BIT] =               "tx-scatter-gather",
60         [NETIF_F_IP_CSUM_BIT] =          "tx-checksum-ipv4",
61         [NETIF_F_HW_CSUM_BIT] =          "tx-checksum-ip-generic",
62         [NETIF_F_IPV6_CSUM_BIT] =        "tx-checksum-ipv6",
63         [NETIF_F_HIGHDMA_BIT] =          "highdma",
64         [NETIF_F_FRAGLIST_BIT] =         "tx-scatter-gather-fraglist",
65         [NETIF_F_HW_VLAN_CTAG_TX_BIT] =  "tx-vlan-hw-insert",
66
67         [NETIF_F_HW_VLAN_CTAG_RX_BIT] =  "rx-vlan-hw-parse",
68         [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
69         [NETIF_F_HW_VLAN_STAG_TX_BIT] =  "tx-vlan-stag-hw-insert",
70         [NETIF_F_HW_VLAN_STAG_RX_BIT] =  "rx-vlan-stag-hw-parse",
71         [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
72         [NETIF_F_VLAN_CHALLENGED_BIT] =  "vlan-challenged",
73         [NETIF_F_GSO_BIT] =              "tx-generic-segmentation",
74         [NETIF_F_LLTX_BIT] =             "tx-lockless",
75         [NETIF_F_NETNS_LOCAL_BIT] =      "netns-local",
76         [NETIF_F_GRO_BIT] =              "rx-gro",
77         [NETIF_F_GRO_HW_BIT] =           "rx-gro-hw",
78         [NETIF_F_LRO_BIT] =              "rx-lro",
79
80         [NETIF_F_TSO_BIT] =              "tx-tcp-segmentation",
81         [NETIF_F_GSO_ROBUST_BIT] =       "tx-gso-robust",
82         [NETIF_F_TSO_ECN_BIT] =          "tx-tcp-ecn-segmentation",
83         [NETIF_F_TSO_MANGLEID_BIT] =     "tx-tcp-mangleid-segmentation",
84         [NETIF_F_TSO6_BIT] =             "tx-tcp6-segmentation",
85         [NETIF_F_FSO_BIT] =              "tx-fcoe-segmentation",
86         [NETIF_F_GSO_GRE_BIT] =          "tx-gre-segmentation",
87         [NETIF_F_GSO_GRE_CSUM_BIT] =     "tx-gre-csum-segmentation",
88         [NETIF_F_GSO_IPXIP4_BIT] =       "tx-ipxip4-segmentation",
89         [NETIF_F_GSO_IPXIP6_BIT] =       "tx-ipxip6-segmentation",
90         [NETIF_F_GSO_UDP_TUNNEL_BIT] =   "tx-udp_tnl-segmentation",
91         [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
92         [NETIF_F_GSO_PARTIAL_BIT] =      "tx-gso-partial",
93         [NETIF_F_GSO_SCTP_BIT] =         "tx-sctp-segmentation",
94         [NETIF_F_GSO_ESP_BIT] =          "tx-esp-segmentation",
95         [NETIF_F_GSO_UDP_L4_BIT] =       "tx-udp-segmentation",
96
97         [NETIF_F_FCOE_CRC_BIT] =         "tx-checksum-fcoe-crc",
98         [NETIF_F_SCTP_CRC_BIT] =        "tx-checksum-sctp",
99         [NETIF_F_FCOE_MTU_BIT] =         "fcoe-mtu",
100         [NETIF_F_NTUPLE_BIT] =           "rx-ntuple-filter",
101         [NETIF_F_RXHASH_BIT] =           "rx-hashing",
102         [NETIF_F_RXCSUM_BIT] =           "rx-checksum",
103         [NETIF_F_NOCACHE_COPY_BIT] =     "tx-nocache-copy",
104         [NETIF_F_LOOPBACK_BIT] =         "loopback",
105         [NETIF_F_RXFCS_BIT] =            "rx-fcs",
106         [NETIF_F_RXALL_BIT] =            "rx-all",
107         [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
108         [NETIF_F_HW_TC_BIT] =            "hw-tc-offload",
109         [NETIF_F_HW_ESP_BIT] =           "esp-hw-offload",
110         [NETIF_F_HW_ESP_TX_CSUM_BIT] =   "esp-tx-csum-hw-offload",
111         [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] =       "rx-udp_tunnel-port-offload",
112         [NETIF_F_HW_TLS_RECORD_BIT] =   "tls-hw-record",
113 };
114
115 static const char
116 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
117         [ETH_RSS_HASH_TOP_BIT] =        "toeplitz",
118         [ETH_RSS_HASH_XOR_BIT] =        "xor",
119         [ETH_RSS_HASH_CRC32_BIT] =      "crc32",
120 };
121
122 static const char
123 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
124         [ETHTOOL_ID_UNSPEC]     = "Unspec",
125         [ETHTOOL_RX_COPYBREAK]  = "rx-copybreak",
126         [ETHTOOL_TX_COPYBREAK]  = "tx-copybreak",
127         [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
128 };
129
130 static const char
131 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
132         [ETHTOOL_ID_UNSPEC]     = "Unspec",
133         [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
134 };
135
136 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
137 {
138         struct ethtool_gfeatures cmd = {
139                 .cmd = ETHTOOL_GFEATURES,
140                 .size = ETHTOOL_DEV_FEATURE_WORDS,
141         };
142         struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
143         u32 __user *sizeaddr;
144         u32 copy_size;
145         int i;
146
147         /* in case feature bits run out again */
148         BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
149
150         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
151                 features[i].available = (u32)(dev->hw_features >> (32 * i));
152                 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
153                 features[i].active = (u32)(dev->features >> (32 * i));
154                 features[i].never_changed =
155                         (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
156         }
157
158         sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
159         if (get_user(copy_size, sizeaddr))
160                 return -EFAULT;
161
162         if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
163                 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
164
165         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
166                 return -EFAULT;
167         useraddr += sizeof(cmd);
168         if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
169                 return -EFAULT;
170
171         return 0;
172 }
173
174 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
175 {
176         struct ethtool_sfeatures cmd;
177         struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
178         netdev_features_t wanted = 0, valid = 0;
179         int i, ret = 0;
180
181         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
182                 return -EFAULT;
183         useraddr += sizeof(cmd);
184
185         if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
186                 return -EINVAL;
187
188         if (copy_from_user(features, useraddr, sizeof(features)))
189                 return -EFAULT;
190
191         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
192                 valid |= (netdev_features_t)features[i].valid << (32 * i);
193                 wanted |= (netdev_features_t)features[i].requested << (32 * i);
194         }
195
196         if (valid & ~NETIF_F_ETHTOOL_BITS)
197                 return -EINVAL;
198
199         if (valid & ~dev->hw_features) {
200                 valid &= dev->hw_features;
201                 ret |= ETHTOOL_F_UNSUPPORTED;
202         }
203
204         dev->wanted_features &= ~valid;
205         dev->wanted_features |= wanted & valid;
206         __netdev_update_features(dev);
207
208         if ((dev->wanted_features ^ dev->features) & valid)
209                 ret |= ETHTOOL_F_WISH;
210
211         return ret;
212 }
213
214 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
215 {
216         const struct ethtool_ops *ops = dev->ethtool_ops;
217
218         if (sset == ETH_SS_FEATURES)
219                 return ARRAY_SIZE(netdev_features_strings);
220
221         if (sset == ETH_SS_RSS_HASH_FUNCS)
222                 return ARRAY_SIZE(rss_hash_func_strings);
223
224         if (sset == ETH_SS_TUNABLES)
225                 return ARRAY_SIZE(tunable_strings);
226
227         if (sset == ETH_SS_PHY_TUNABLES)
228                 return ARRAY_SIZE(phy_tunable_strings);
229
230         if (sset == ETH_SS_PHY_STATS && dev->phydev &&
231             !ops->get_ethtool_phy_stats)
232                 return phy_ethtool_get_sset_count(dev->phydev);
233
234         if (ops->get_sset_count && ops->get_strings)
235                 return ops->get_sset_count(dev, sset);
236         else
237                 return -EOPNOTSUPP;
238 }
239
240 static void __ethtool_get_strings(struct net_device *dev,
241         u32 stringset, u8 *data)
242 {
243         const struct ethtool_ops *ops = dev->ethtool_ops;
244
245         if (stringset == ETH_SS_FEATURES)
246                 memcpy(data, netdev_features_strings,
247                         sizeof(netdev_features_strings));
248         else if (stringset == ETH_SS_RSS_HASH_FUNCS)
249                 memcpy(data, rss_hash_func_strings,
250                        sizeof(rss_hash_func_strings));
251         else if (stringset == ETH_SS_TUNABLES)
252                 memcpy(data, tunable_strings, sizeof(tunable_strings));
253         else if (stringset == ETH_SS_PHY_TUNABLES)
254                 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
255         else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
256                  !ops->get_ethtool_phy_stats)
257                 phy_ethtool_get_strings(dev->phydev, data);
258         else
259                 /* ops->get_strings is valid because checked earlier */
260                 ops->get_strings(dev, stringset, data);
261 }
262
263 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
264 {
265         /* feature masks of legacy discrete ethtool ops */
266
267         switch (eth_cmd) {
268         case ETHTOOL_GTXCSUM:
269         case ETHTOOL_STXCSUM:
270                 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
271         case ETHTOOL_GRXCSUM:
272         case ETHTOOL_SRXCSUM:
273                 return NETIF_F_RXCSUM;
274         case ETHTOOL_GSG:
275         case ETHTOOL_SSG:
276                 return NETIF_F_SG;
277         case ETHTOOL_GTSO:
278         case ETHTOOL_STSO:
279                 return NETIF_F_ALL_TSO;
280         case ETHTOOL_GGSO:
281         case ETHTOOL_SGSO:
282                 return NETIF_F_GSO;
283         case ETHTOOL_GGRO:
284         case ETHTOOL_SGRO:
285                 return NETIF_F_GRO;
286         default:
287                 BUG();
288         }
289 }
290
291 static int ethtool_get_one_feature(struct net_device *dev,
292         char __user *useraddr, u32 ethcmd)
293 {
294         netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
295         struct ethtool_value edata = {
296                 .cmd = ethcmd,
297                 .data = !!(dev->features & mask),
298         };
299
300         if (copy_to_user(useraddr, &edata, sizeof(edata)))
301                 return -EFAULT;
302         return 0;
303 }
304
305 static int ethtool_set_one_feature(struct net_device *dev,
306         void __user *useraddr, u32 ethcmd)
307 {
308         struct ethtool_value edata;
309         netdev_features_t mask;
310
311         if (copy_from_user(&edata, useraddr, sizeof(edata)))
312                 return -EFAULT;
313
314         mask = ethtool_get_feature_mask(ethcmd);
315         mask &= dev->hw_features;
316         if (!mask)
317                 return -EOPNOTSUPP;
318
319         if (edata.data)
320                 dev->wanted_features |= mask;
321         else
322                 dev->wanted_features &= ~mask;
323
324         __netdev_update_features(dev);
325
326         return 0;
327 }
328
329 #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
330                           ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
331 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
332                           NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
333                           NETIF_F_RXHASH)
334
335 static u32 __ethtool_get_flags(struct net_device *dev)
336 {
337         u32 flags = 0;
338
339         if (dev->features & NETIF_F_LRO)
340                 flags |= ETH_FLAG_LRO;
341         if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
342                 flags |= ETH_FLAG_RXVLAN;
343         if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
344                 flags |= ETH_FLAG_TXVLAN;
345         if (dev->features & NETIF_F_NTUPLE)
346                 flags |= ETH_FLAG_NTUPLE;
347         if (dev->features & NETIF_F_RXHASH)
348                 flags |= ETH_FLAG_RXHASH;
349
350         return flags;
351 }
352
353 static int __ethtool_set_flags(struct net_device *dev, u32 data)
354 {
355         netdev_features_t features = 0, changed;
356
357         if (data & ~ETH_ALL_FLAGS)
358                 return -EINVAL;
359
360         if (data & ETH_FLAG_LRO)
361                 features |= NETIF_F_LRO;
362         if (data & ETH_FLAG_RXVLAN)
363                 features |= NETIF_F_HW_VLAN_CTAG_RX;
364         if (data & ETH_FLAG_TXVLAN)
365                 features |= NETIF_F_HW_VLAN_CTAG_TX;
366         if (data & ETH_FLAG_NTUPLE)
367                 features |= NETIF_F_NTUPLE;
368         if (data & ETH_FLAG_RXHASH)
369                 features |= NETIF_F_RXHASH;
370
371         /* allow changing only bits set in hw_features */
372         changed = (features ^ dev->features) & ETH_ALL_FEATURES;
373         if (changed & ~dev->hw_features)
374                 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
375
376         dev->wanted_features =
377                 (dev->wanted_features & ~changed) | (features & changed);
378
379         __netdev_update_features(dev);
380
381         return 0;
382 }
383
384 /* Given two link masks, AND them together and save the result in dst. */
385 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
386                                   struct ethtool_link_ksettings *src)
387 {
388         unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
389         unsigned int idx = 0;
390
391         for (; idx < size; idx++) {
392                 dst->link_modes.supported[idx] &=
393                         src->link_modes.supported[idx];
394                 dst->link_modes.advertising[idx] &=
395                         src->link_modes.advertising[idx];
396         }
397 }
398 EXPORT_SYMBOL(ethtool_intersect_link_masks);
399
400 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
401                                              u32 legacy_u32)
402 {
403         bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
404         dst[0] = legacy_u32;
405 }
406 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
407
408 /* return false if src had higher bits set. lower bits always updated. */
409 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
410                                              const unsigned long *src)
411 {
412         bool retval = true;
413
414         /* TODO: following test will soon always be true */
415         if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
416                 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
417
418                 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
419                 bitmap_fill(ext, 32);
420                 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
421                 if (bitmap_intersects(ext, src,
422                                       __ETHTOOL_LINK_MODE_MASK_NBITS)) {
423                         /* src mask goes beyond bit 31 */
424                         retval = false;
425                 }
426         }
427         *legacy_u32 = src[0];
428         return retval;
429 }
430 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
431
432 /* return false if legacy contained non-0 deprecated fields
433  * maxtxpkt/maxrxpkt. rest of ksettings always updated
434  */
435 static bool
436 convert_legacy_settings_to_link_ksettings(
437         struct ethtool_link_ksettings *link_ksettings,
438         const struct ethtool_cmd *legacy_settings)
439 {
440         bool retval = true;
441
442         memset(link_ksettings, 0, sizeof(*link_ksettings));
443
444         /* This is used to tell users that driver is still using these
445          * deprecated legacy fields, and they should not use
446          * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
447          */
448         if (legacy_settings->maxtxpkt ||
449             legacy_settings->maxrxpkt)
450                 retval = false;
451
452         ethtool_convert_legacy_u32_to_link_mode(
453                 link_ksettings->link_modes.supported,
454                 legacy_settings->supported);
455         ethtool_convert_legacy_u32_to_link_mode(
456                 link_ksettings->link_modes.advertising,
457                 legacy_settings->advertising);
458         ethtool_convert_legacy_u32_to_link_mode(
459                 link_ksettings->link_modes.lp_advertising,
460                 legacy_settings->lp_advertising);
461         link_ksettings->base.speed
462                 = ethtool_cmd_speed(legacy_settings);
463         link_ksettings->base.duplex
464                 = legacy_settings->duplex;
465         link_ksettings->base.port
466                 = legacy_settings->port;
467         link_ksettings->base.phy_address
468                 = legacy_settings->phy_address;
469         link_ksettings->base.autoneg
470                 = legacy_settings->autoneg;
471         link_ksettings->base.mdio_support
472                 = legacy_settings->mdio_support;
473         link_ksettings->base.eth_tp_mdix
474                 = legacy_settings->eth_tp_mdix;
475         link_ksettings->base.eth_tp_mdix_ctrl
476                 = legacy_settings->eth_tp_mdix_ctrl;
477         return retval;
478 }
479
480 /* return false if ksettings link modes had higher bits
481  * set. legacy_settings always updated (best effort)
482  */
483 static bool
484 convert_link_ksettings_to_legacy_settings(
485         struct ethtool_cmd *legacy_settings,
486         const struct ethtool_link_ksettings *link_ksettings)
487 {
488         bool retval = true;
489
490         memset(legacy_settings, 0, sizeof(*legacy_settings));
491         /* this also clears the deprecated fields in legacy structure:
492          * __u8         transceiver;
493          * __u32        maxtxpkt;
494          * __u32        maxrxpkt;
495          */
496
497         retval &= ethtool_convert_link_mode_to_legacy_u32(
498                 &legacy_settings->supported,
499                 link_ksettings->link_modes.supported);
500         retval &= ethtool_convert_link_mode_to_legacy_u32(
501                 &legacy_settings->advertising,
502                 link_ksettings->link_modes.advertising);
503         retval &= ethtool_convert_link_mode_to_legacy_u32(
504                 &legacy_settings->lp_advertising,
505                 link_ksettings->link_modes.lp_advertising);
506         ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
507         legacy_settings->duplex
508                 = link_ksettings->base.duplex;
509         legacy_settings->port
510                 = link_ksettings->base.port;
511         legacy_settings->phy_address
512                 = link_ksettings->base.phy_address;
513         legacy_settings->autoneg
514                 = link_ksettings->base.autoneg;
515         legacy_settings->mdio_support
516                 = link_ksettings->base.mdio_support;
517         legacy_settings->eth_tp_mdix
518                 = link_ksettings->base.eth_tp_mdix;
519         legacy_settings->eth_tp_mdix_ctrl
520                 = link_ksettings->base.eth_tp_mdix_ctrl;
521         legacy_settings->transceiver
522                 = link_ksettings->base.transceiver;
523         return retval;
524 }
525
526 /* number of 32-bit words to store the user's link mode bitmaps */
527 #define __ETHTOOL_LINK_MODE_MASK_NU32                   \
528         DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
529
530 /* layout of the struct passed from/to userland */
531 struct ethtool_link_usettings {
532         struct ethtool_link_settings base;
533         struct {
534                 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
535                 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
536                 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
537         } link_modes;
538 };
539
540 /* Internal kernel helper to query a device ethtool_link_settings.
541  *
542  * Backward compatibility note: for compatibility with legacy drivers
543  * that implement only the ethtool_cmd API, this has to work with both
544  * drivers implementing get_link_ksettings API and drivers
545  * implementing get_settings API. When drivers implement get_settings
546  * and report ethtool_cmd deprecated fields
547  * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
548  * because the resulting struct ethtool_link_settings does not report them.
549  */
550 int __ethtool_get_link_ksettings(struct net_device *dev,
551                                  struct ethtool_link_ksettings *link_ksettings)
552 {
553         int err;
554         struct ethtool_cmd cmd;
555
556         ASSERT_RTNL();
557
558         if (dev->ethtool_ops->get_link_ksettings) {
559                 memset(link_ksettings, 0, sizeof(*link_ksettings));
560                 return dev->ethtool_ops->get_link_ksettings(dev,
561                                                             link_ksettings);
562         }
563
564         /* driver doesn't support %ethtool_link_ksettings API. revert to
565          * legacy %ethtool_cmd API, unless it's not supported either.
566          * TODO: remove when ethtool_ops::get_settings disappears internally
567          */
568         if (!dev->ethtool_ops->get_settings)
569                 return -EOPNOTSUPP;
570
571         memset(&cmd, 0, sizeof(cmd));
572         cmd.cmd = ETHTOOL_GSET;
573         err = dev->ethtool_ops->get_settings(dev, &cmd);
574         if (err < 0)
575                 return err;
576
577         /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
578          */
579         convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
580         return err;
581 }
582 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
583
584 /* convert ethtool_link_usettings in user space to a kernel internal
585  * ethtool_link_ksettings. return 0 on success, errno on error.
586  */
587 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
588                                          const void __user *from)
589 {
590         struct ethtool_link_usettings link_usettings;
591
592         if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
593                 return -EFAULT;
594
595         memcpy(&to->base, &link_usettings.base, sizeof(to->base));
596         bitmap_from_arr32(to->link_modes.supported,
597                           link_usettings.link_modes.supported,
598                           __ETHTOOL_LINK_MODE_MASK_NBITS);
599         bitmap_from_arr32(to->link_modes.advertising,
600                           link_usettings.link_modes.advertising,
601                           __ETHTOOL_LINK_MODE_MASK_NBITS);
602         bitmap_from_arr32(to->link_modes.lp_advertising,
603                           link_usettings.link_modes.lp_advertising,
604                           __ETHTOOL_LINK_MODE_MASK_NBITS);
605
606         return 0;
607 }
608
609 /* convert a kernel internal ethtool_link_ksettings to
610  * ethtool_link_usettings in user space. return 0 on success, errno on
611  * error.
612  */
613 static int
614 store_link_ksettings_for_user(void __user *to,
615                               const struct ethtool_link_ksettings *from)
616 {
617         struct ethtool_link_usettings link_usettings;
618
619         memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
620         bitmap_to_arr32(link_usettings.link_modes.supported,
621                         from->link_modes.supported,
622                         __ETHTOOL_LINK_MODE_MASK_NBITS);
623         bitmap_to_arr32(link_usettings.link_modes.advertising,
624                         from->link_modes.advertising,
625                         __ETHTOOL_LINK_MODE_MASK_NBITS);
626         bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
627                         from->link_modes.lp_advertising,
628                         __ETHTOOL_LINK_MODE_MASK_NBITS);
629
630         if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
631                 return -EFAULT;
632
633         return 0;
634 }
635
636 /* Query device for its ethtool_link_settings.
637  *
638  * Backward compatibility note: this function must fail when driver
639  * does not implement ethtool::get_link_ksettings, even if legacy
640  * ethtool_ops::get_settings is implemented. This tells new versions
641  * of ethtool that they should use the legacy API %ETHTOOL_GSET for
642  * this driver, so that they can correctly access the ethtool_cmd
643  * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
644  * implements ethtool_ops::get_settings anymore.
645  */
646 static int ethtool_get_link_ksettings(struct net_device *dev,
647                                       void __user *useraddr)
648 {
649         int err = 0;
650         struct ethtool_link_ksettings link_ksettings;
651
652         ASSERT_RTNL();
653
654         if (!dev->ethtool_ops->get_link_ksettings)
655                 return -EOPNOTSUPP;
656
657         /* handle bitmap nbits handshake */
658         if (copy_from_user(&link_ksettings.base, useraddr,
659                            sizeof(link_ksettings.base)))
660                 return -EFAULT;
661
662         if (__ETHTOOL_LINK_MODE_MASK_NU32
663             != link_ksettings.base.link_mode_masks_nwords) {
664                 /* wrong link mode nbits requested */
665                 memset(&link_ksettings, 0, sizeof(link_ksettings));
666                 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
667                 /* send back number of words required as negative val */
668                 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
669                                    "need too many bits for link modes!");
670                 link_ksettings.base.link_mode_masks_nwords
671                         = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
672
673                 /* copy the base fields back to user, not the link
674                  * mode bitmaps
675                  */
676                 if (copy_to_user(useraddr, &link_ksettings.base,
677                                  sizeof(link_ksettings.base)))
678                         return -EFAULT;
679
680                 return 0;
681         }
682
683         /* handshake successful: user/kernel agree on
684          * link_mode_masks_nwords
685          */
686
687         memset(&link_ksettings, 0, sizeof(link_ksettings));
688         err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
689         if (err < 0)
690                 return err;
691
692         /* make sure we tell the right values to user */
693         link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
694         link_ksettings.base.link_mode_masks_nwords
695                 = __ETHTOOL_LINK_MODE_MASK_NU32;
696
697         return store_link_ksettings_for_user(useraddr, &link_ksettings);
698 }
699
700 /* Update device ethtool_link_settings.
701  *
702  * Backward compatibility note: this function must fail when driver
703  * does not implement ethtool::set_link_ksettings, even if legacy
704  * ethtool_ops::set_settings is implemented. This tells new versions
705  * of ethtool that they should use the legacy API %ETHTOOL_SSET for
706  * this driver, so that they can correctly update the ethtool_cmd
707  * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
708  * implements ethtool_ops::get_settings anymore.
709  */
710 static int ethtool_set_link_ksettings(struct net_device *dev,
711                                       void __user *useraddr)
712 {
713         int err;
714         struct ethtool_link_ksettings link_ksettings;
715
716         ASSERT_RTNL();
717
718         if (!dev->ethtool_ops->set_link_ksettings)
719                 return -EOPNOTSUPP;
720
721         /* make sure nbits field has expected value */
722         if (copy_from_user(&link_ksettings.base, useraddr,
723                            sizeof(link_ksettings.base)))
724                 return -EFAULT;
725
726         if (__ETHTOOL_LINK_MODE_MASK_NU32
727             != link_ksettings.base.link_mode_masks_nwords)
728                 return -EINVAL;
729
730         /* copy the whole structure, now that we know it has expected
731          * format
732          */
733         err = load_link_ksettings_from_user(&link_ksettings, useraddr);
734         if (err)
735                 return err;
736
737         /* re-check nwords field, just in case */
738         if (__ETHTOOL_LINK_MODE_MASK_NU32
739             != link_ksettings.base.link_mode_masks_nwords)
740                 return -EINVAL;
741
742         return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
743 }
744
745 /* Query device for its ethtool_cmd settings.
746  *
747  * Backward compatibility note: for compatibility with legacy ethtool,
748  * this has to work with both drivers implementing get_link_ksettings
749  * API and drivers implementing get_settings API. When drivers
750  * implement get_link_ksettings and report higher link mode bits, a
751  * kernel warning is logged once (with name of 1st driver/device) to
752  * recommend user to upgrade ethtool, but the command is successful
753  * (only the lower link mode bits reported back to user).
754  */
755 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
756 {
757         struct ethtool_cmd cmd;
758
759         ASSERT_RTNL();
760
761         if (dev->ethtool_ops->get_link_ksettings) {
762                 /* First, use link_ksettings API if it is supported */
763                 int err;
764                 struct ethtool_link_ksettings link_ksettings;
765
766                 memset(&link_ksettings, 0, sizeof(link_ksettings));
767                 err = dev->ethtool_ops->get_link_ksettings(dev,
768                                                            &link_ksettings);
769                 if (err < 0)
770                         return err;
771                 convert_link_ksettings_to_legacy_settings(&cmd,
772                                                           &link_ksettings);
773
774                 /* send a sensible cmd tag back to user */
775                 cmd.cmd = ETHTOOL_GSET;
776         } else {
777                 /* driver doesn't support %ethtool_link_ksettings
778                  * API. revert to legacy %ethtool_cmd API, unless it's
779                  * not supported either.
780                  */
781                 int err;
782
783                 if (!dev->ethtool_ops->get_settings)
784                         return -EOPNOTSUPP;
785
786                 memset(&cmd, 0, sizeof(cmd));
787                 cmd.cmd = ETHTOOL_GSET;
788                 err = dev->ethtool_ops->get_settings(dev, &cmd);
789                 if (err < 0)
790                         return err;
791         }
792
793         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
794                 return -EFAULT;
795
796         return 0;
797 }
798
799 /* Update device link settings with given ethtool_cmd.
800  *
801  * Backward compatibility note: for compatibility with legacy ethtool,
802  * this has to work with both drivers implementing set_link_ksettings
803  * API and drivers implementing set_settings API. When drivers
804  * implement set_link_ksettings and user's request updates deprecated
805  * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
806  * warning is logged once (with name of 1st driver/device) to
807  * recommend user to upgrade ethtool, and the request is rejected.
808  */
809 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
810 {
811         struct ethtool_cmd cmd;
812
813         ASSERT_RTNL();
814
815         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
816                 return -EFAULT;
817
818         /* first, try new %ethtool_link_ksettings API. */
819         if (dev->ethtool_ops->set_link_ksettings) {
820                 struct ethtool_link_ksettings link_ksettings;
821
822                 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
823                                                                &cmd))
824                         return -EINVAL;
825
826                 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
827                 link_ksettings.base.link_mode_masks_nwords
828                         = __ETHTOOL_LINK_MODE_MASK_NU32;
829                 return dev->ethtool_ops->set_link_ksettings(dev,
830                                                             &link_ksettings);
831         }
832
833         /* legacy %ethtool_cmd API */
834
835         /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
836          * disappears internally
837          */
838
839         if (!dev->ethtool_ops->set_settings)
840                 return -EOPNOTSUPP;
841
842         return dev->ethtool_ops->set_settings(dev, &cmd);
843 }
844
845 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
846                                                   void __user *useraddr)
847 {
848         struct ethtool_drvinfo info;
849         const struct ethtool_ops *ops = dev->ethtool_ops;
850
851         memset(&info, 0, sizeof(info));
852         info.cmd = ETHTOOL_GDRVINFO;
853         if (ops->get_drvinfo) {
854                 ops->get_drvinfo(dev, &info);
855         } else if (dev->dev.parent && dev->dev.parent->driver) {
856                 strlcpy(info.bus_info, dev_name(dev->dev.parent),
857                         sizeof(info.bus_info));
858                 strlcpy(info.driver, dev->dev.parent->driver->name,
859                         sizeof(info.driver));
860         } else {
861                 return -EOPNOTSUPP;
862         }
863
864         /*
865          * this method of obtaining string set info is deprecated;
866          * Use ETHTOOL_GSSET_INFO instead.
867          */
868         if (ops->get_sset_count) {
869                 int rc;
870
871                 rc = ops->get_sset_count(dev, ETH_SS_TEST);
872                 if (rc >= 0)
873                         info.testinfo_len = rc;
874                 rc = ops->get_sset_count(dev, ETH_SS_STATS);
875                 if (rc >= 0)
876                         info.n_stats = rc;
877                 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
878                 if (rc >= 0)
879                         info.n_priv_flags = rc;
880         }
881         if (ops->get_regs_len)
882                 info.regdump_len = ops->get_regs_len(dev);
883         if (ops->get_eeprom_len)
884                 info.eedump_len = ops->get_eeprom_len(dev);
885
886         if (copy_to_user(useraddr, &info, sizeof(info)))
887                 return -EFAULT;
888         return 0;
889 }
890
891 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
892                                                     void __user *useraddr)
893 {
894         struct ethtool_sset_info info;
895         u64 sset_mask;
896         int i, idx = 0, n_bits = 0, ret, rc;
897         u32 *info_buf = NULL;
898
899         if (copy_from_user(&info, useraddr, sizeof(info)))
900                 return -EFAULT;
901
902         /* store copy of mask, because we zero struct later on */
903         sset_mask = info.sset_mask;
904         if (!sset_mask)
905                 return 0;
906
907         /* calculate size of return buffer */
908         n_bits = hweight64(sset_mask);
909
910         memset(&info, 0, sizeof(info));
911         info.cmd = ETHTOOL_GSSET_INFO;
912
913         info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
914         if (!info_buf)
915                 return -ENOMEM;
916
917         /*
918          * fill return buffer based on input bitmask and successful
919          * get_sset_count return
920          */
921         for (i = 0; i < 64; i++) {
922                 if (!(sset_mask & (1ULL << i)))
923                         continue;
924
925                 rc = __ethtool_get_sset_count(dev, i);
926                 if (rc >= 0) {
927                         info.sset_mask |= (1ULL << i);
928                         info_buf[idx++] = rc;
929                 }
930         }
931
932         ret = -EFAULT;
933         if (copy_to_user(useraddr, &info, sizeof(info)))
934                 goto out;
935
936         useraddr += offsetof(struct ethtool_sset_info, data);
937         if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
938                 goto out;
939
940         ret = 0;
941
942 out:
943         kfree(info_buf);
944         return ret;
945 }
946
947 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
948                                                 u32 cmd, void __user *useraddr)
949 {
950         struct ethtool_rxnfc info;
951         size_t info_size = sizeof(info);
952         int rc;
953
954         if (!dev->ethtool_ops->set_rxnfc)
955                 return -EOPNOTSUPP;
956
957         /* struct ethtool_rxnfc was originally defined for
958          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
959          * members.  User-space might still be using that
960          * definition. */
961         if (cmd == ETHTOOL_SRXFH)
962                 info_size = (offsetof(struct ethtool_rxnfc, data) +
963                              sizeof(info.data));
964
965         if (copy_from_user(&info, useraddr, info_size))
966                 return -EFAULT;
967
968         rc = dev->ethtool_ops->set_rxnfc(dev, &info);
969         if (rc)
970                 return rc;
971
972         if (cmd == ETHTOOL_SRXCLSRLINS &&
973             copy_to_user(useraddr, &info, info_size))
974                 return -EFAULT;
975
976         return 0;
977 }
978
979 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
980                                                 u32 cmd, void __user *useraddr)
981 {
982         struct ethtool_rxnfc info;
983         size_t info_size = sizeof(info);
984         const struct ethtool_ops *ops = dev->ethtool_ops;
985         int ret;
986         void *rule_buf = NULL;
987
988         if (!ops->get_rxnfc)
989                 return -EOPNOTSUPP;
990
991         /* struct ethtool_rxnfc was originally defined for
992          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
993          * members.  User-space might still be using that
994          * definition. */
995         if (cmd == ETHTOOL_GRXFH)
996                 info_size = (offsetof(struct ethtool_rxnfc, data) +
997                              sizeof(info.data));
998
999         if (copy_from_user(&info, useraddr, info_size))
1000                 return -EFAULT;
1001
1002         /* If FLOW_RSS was requested then user-space must be using the
1003          * new definition, as FLOW_RSS is newer.
1004          */
1005         if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1006                 info_size = sizeof(info);
1007                 if (copy_from_user(&info, useraddr, info_size))
1008                         return -EFAULT;
1009         }
1010
1011         if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1012                 if (info.rule_cnt > 0) {
1013                         if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1014                                 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
1015                                                    GFP_USER);
1016                         if (!rule_buf)
1017                                 return -ENOMEM;
1018                 }
1019         }
1020
1021         ret = ops->get_rxnfc(dev, &info, rule_buf);
1022         if (ret < 0)
1023                 goto err_out;
1024
1025         ret = -EFAULT;
1026         if (copy_to_user(useraddr, &info, info_size))
1027                 goto err_out;
1028
1029         if (rule_buf) {
1030                 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1031                 if (copy_to_user(useraddr, rule_buf,
1032                                  info.rule_cnt * sizeof(u32)))
1033                         goto err_out;
1034         }
1035         ret = 0;
1036
1037 err_out:
1038         kfree(rule_buf);
1039
1040         return ret;
1041 }
1042
1043 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1044                                         struct ethtool_rxnfc *rx_rings,
1045                                         u32 size)
1046 {
1047         int i;
1048
1049         if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1050                 return -EFAULT;
1051
1052         /* Validate ring indices */
1053         for (i = 0; i < size; i++)
1054                 if (indir[i] >= rx_rings->data)
1055                         return -EINVAL;
1056
1057         return 0;
1058 }
1059
1060 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1061
1062 void netdev_rss_key_fill(void *buffer, size_t len)
1063 {
1064         BUG_ON(len > sizeof(netdev_rss_key));
1065         net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1066         memcpy(buffer, netdev_rss_key, len);
1067 }
1068 EXPORT_SYMBOL(netdev_rss_key_fill);
1069
1070 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1071 {
1072         u32 dev_size, current_max = 0;
1073         u32 *indir;
1074         int ret;
1075
1076         if (!dev->ethtool_ops->get_rxfh_indir_size ||
1077             !dev->ethtool_ops->get_rxfh)
1078                 return -EOPNOTSUPP;
1079         dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1080         if (dev_size == 0)
1081                 return -EOPNOTSUPP;
1082
1083         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1084         if (!indir)
1085                 return -ENOMEM;
1086
1087         ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1088         if (ret)
1089                 goto out;
1090
1091         while (dev_size--)
1092                 current_max = max(current_max, indir[dev_size]);
1093
1094         *max = current_max;
1095
1096 out:
1097         kfree(indir);
1098         return ret;
1099 }
1100
1101 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1102                                                      void __user *useraddr)
1103 {
1104         u32 user_size, dev_size;
1105         u32 *indir;
1106         int ret;
1107
1108         if (!dev->ethtool_ops->get_rxfh_indir_size ||
1109             !dev->ethtool_ops->get_rxfh)
1110                 return -EOPNOTSUPP;
1111         dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1112         if (dev_size == 0)
1113                 return -EOPNOTSUPP;
1114
1115         if (copy_from_user(&user_size,
1116                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
1117                            sizeof(user_size)))
1118                 return -EFAULT;
1119
1120         if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1121                          &dev_size, sizeof(dev_size)))
1122                 return -EFAULT;
1123
1124         /* If the user buffer size is 0, this is just a query for the
1125          * device table size.  Otherwise, if it's smaller than the
1126          * device table size it's an error.
1127          */
1128         if (user_size < dev_size)
1129                 return user_size == 0 ? 0 : -EINVAL;
1130
1131         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1132         if (!indir)
1133                 return -ENOMEM;
1134
1135         ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1136         if (ret)
1137                 goto out;
1138
1139         if (copy_to_user(useraddr +
1140                          offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1141                          indir, dev_size * sizeof(indir[0])))
1142                 ret = -EFAULT;
1143
1144 out:
1145         kfree(indir);
1146         return ret;
1147 }
1148
1149 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1150                                                      void __user *useraddr)
1151 {
1152         struct ethtool_rxnfc rx_rings;
1153         u32 user_size, dev_size, i;
1154         u32 *indir;
1155         const struct ethtool_ops *ops = dev->ethtool_ops;
1156         int ret;
1157         u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1158
1159         if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1160             !ops->get_rxnfc)
1161                 return -EOPNOTSUPP;
1162
1163         dev_size = ops->get_rxfh_indir_size(dev);
1164         if (dev_size == 0)
1165                 return -EOPNOTSUPP;
1166
1167         if (copy_from_user(&user_size,
1168                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
1169                            sizeof(user_size)))
1170                 return -EFAULT;
1171
1172         if (user_size != 0 && user_size != dev_size)
1173                 return -EINVAL;
1174
1175         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1176         if (!indir)
1177                 return -ENOMEM;
1178
1179         rx_rings.cmd = ETHTOOL_GRXRINGS;
1180         ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1181         if (ret)
1182                 goto out;
1183
1184         if (user_size == 0) {
1185                 for (i = 0; i < dev_size; i++)
1186                         indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1187         } else {
1188                 ret = ethtool_copy_validate_indir(indir,
1189                                                   useraddr + ringidx_offset,
1190                                                   &rx_rings,
1191                                                   dev_size);
1192                 if (ret)
1193                         goto out;
1194         }
1195
1196         ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1197         if (ret)
1198                 goto out;
1199
1200         /* indicate whether rxfh was set to default */
1201         if (user_size == 0)
1202                 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1203         else
1204                 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1205
1206 out:
1207         kfree(indir);
1208         return ret;
1209 }
1210
1211 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1212                                                void __user *useraddr)
1213 {
1214         int ret;
1215         const struct ethtool_ops *ops = dev->ethtool_ops;
1216         u32 user_indir_size, user_key_size;
1217         u32 dev_indir_size = 0, dev_key_size = 0;
1218         struct ethtool_rxfh rxfh;
1219         u32 total_size;
1220         u32 indir_bytes;
1221         u32 *indir = NULL;
1222         u8 dev_hfunc = 0;
1223         u8 *hkey = NULL;
1224         u8 *rss_config;
1225
1226         if (!ops->get_rxfh)
1227                 return -EOPNOTSUPP;
1228
1229         if (ops->get_rxfh_indir_size)
1230                 dev_indir_size = ops->get_rxfh_indir_size(dev);
1231         if (ops->get_rxfh_key_size)
1232                 dev_key_size = ops->get_rxfh_key_size(dev);
1233
1234         if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1235                 return -EFAULT;
1236         user_indir_size = rxfh.indir_size;
1237         user_key_size = rxfh.key_size;
1238
1239         /* Check that reserved fields are 0 for now */
1240         if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1241                 return -EINVAL;
1242         /* Most drivers don't handle rss_context, check it's 0 as well */
1243         if (rxfh.rss_context && !ops->get_rxfh_context)
1244                 return -EOPNOTSUPP;
1245
1246         rxfh.indir_size = dev_indir_size;
1247         rxfh.key_size = dev_key_size;
1248         if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1249                 return -EFAULT;
1250
1251         if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1252             (user_key_size && (user_key_size != dev_key_size)))
1253                 return -EINVAL;
1254
1255         indir_bytes = user_indir_size * sizeof(indir[0]);
1256         total_size = indir_bytes + user_key_size;
1257         rss_config = kzalloc(total_size, GFP_USER);
1258         if (!rss_config)
1259                 return -ENOMEM;
1260
1261         if (user_indir_size)
1262                 indir = (u32 *)rss_config;
1263
1264         if (user_key_size)
1265                 hkey = rss_config + indir_bytes;
1266
1267         if (rxfh.rss_context)
1268                 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1269                                                          &dev_hfunc,
1270                                                          rxfh.rss_context);
1271         else
1272                 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1273         if (ret)
1274                 goto out;
1275
1276         if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1277                          &dev_hfunc, sizeof(rxfh.hfunc))) {
1278                 ret = -EFAULT;
1279         } else if (copy_to_user(useraddr +
1280                               offsetof(struct ethtool_rxfh, rss_config[0]),
1281                               rss_config, total_size)) {
1282                 ret = -EFAULT;
1283         }
1284 out:
1285         kfree(rss_config);
1286
1287         return ret;
1288 }
1289
1290 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1291                                                void __user *useraddr)
1292 {
1293         int ret;
1294         const struct ethtool_ops *ops = dev->ethtool_ops;
1295         struct ethtool_rxnfc rx_rings;
1296         struct ethtool_rxfh rxfh;
1297         u32 dev_indir_size = 0, dev_key_size = 0, i;
1298         u32 *indir = NULL, indir_bytes = 0;
1299         u8 *hkey = NULL;
1300         u8 *rss_config;
1301         u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1302         bool delete = false;
1303
1304         if (!ops->get_rxnfc || !ops->set_rxfh)
1305                 return -EOPNOTSUPP;
1306
1307         if (ops->get_rxfh_indir_size)
1308                 dev_indir_size = ops->get_rxfh_indir_size(dev);
1309         if (ops->get_rxfh_key_size)
1310                 dev_key_size = ops->get_rxfh_key_size(dev);
1311
1312         if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1313                 return -EFAULT;
1314
1315         /* Check that reserved fields are 0 for now */
1316         if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1317                 return -EINVAL;
1318         /* Most drivers don't handle rss_context, check it's 0 as well */
1319         if (rxfh.rss_context && !ops->set_rxfh_context)
1320                 return -EOPNOTSUPP;
1321
1322         /* If either indir, hash key or function is valid, proceed further.
1323          * Must request at least one change: indir size, hash key or function.
1324          */
1325         if ((rxfh.indir_size &&
1326              rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1327              rxfh.indir_size != dev_indir_size) ||
1328             (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1329             (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1330              rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1331                 return -EINVAL;
1332
1333         if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1334                 indir_bytes = dev_indir_size * sizeof(indir[0]);
1335
1336         rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1337         if (!rss_config)
1338                 return -ENOMEM;
1339
1340         rx_rings.cmd = ETHTOOL_GRXRINGS;
1341         ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1342         if (ret)
1343                 goto out;
1344
1345         /* rxfh.indir_size == 0 means reset the indir table to default (master
1346          * context) or delete the context (other RSS contexts).
1347          * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1348          */
1349         if (rxfh.indir_size &&
1350             rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1351                 indir = (u32 *)rss_config;
1352                 ret = ethtool_copy_validate_indir(indir,
1353                                                   useraddr + rss_cfg_offset,
1354                                                   &rx_rings,
1355                                                   rxfh.indir_size);
1356                 if (ret)
1357                         goto out;
1358         } else if (rxfh.indir_size == 0) {
1359                 if (rxfh.rss_context == 0) {
1360                         indir = (u32 *)rss_config;
1361                         for (i = 0; i < dev_indir_size; i++)
1362                                 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1363                 } else {
1364                         delete = true;
1365                 }
1366         }
1367
1368         if (rxfh.key_size) {
1369                 hkey = rss_config + indir_bytes;
1370                 if (copy_from_user(hkey,
1371                                    useraddr + rss_cfg_offset + indir_bytes,
1372                                    rxfh.key_size)) {
1373                         ret = -EFAULT;
1374                         goto out;
1375                 }
1376         }
1377
1378         if (rxfh.rss_context)
1379                 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1380                                             &rxfh.rss_context, delete);
1381         else
1382                 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1383         if (ret)
1384                 goto out;
1385
1386         if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1387                          &rxfh.rss_context, sizeof(rxfh.rss_context)))
1388                 ret = -EFAULT;
1389
1390         if (!rxfh.rss_context) {
1391                 /* indicate whether rxfh was set to default */
1392                 if (rxfh.indir_size == 0)
1393                         dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1394                 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1395                         dev->priv_flags |= IFF_RXFH_CONFIGURED;
1396         }
1397
1398 out:
1399         kfree(rss_config);
1400         return ret;
1401 }
1402
1403 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1404 {
1405         struct ethtool_regs regs;
1406         const struct ethtool_ops *ops = dev->ethtool_ops;
1407         void *regbuf;
1408         int reglen, ret;
1409
1410         if (!ops->get_regs || !ops->get_regs_len)
1411                 return -EOPNOTSUPP;
1412
1413         if (copy_from_user(&regs, useraddr, sizeof(regs)))
1414                 return -EFAULT;
1415
1416         reglen = ops->get_regs_len(dev);
1417         if (regs.len > reglen)
1418                 regs.len = reglen;
1419
1420         regbuf = NULL;
1421         if (reglen) {
1422                 regbuf = vzalloc(reglen);
1423                 if (!regbuf)
1424                         return -ENOMEM;
1425         }
1426
1427         ops->get_regs(dev, &regs, regbuf);
1428
1429         ret = -EFAULT;
1430         if (copy_to_user(useraddr, &regs, sizeof(regs)))
1431                 goto out;
1432         useraddr += offsetof(struct ethtool_regs, data);
1433         if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1434                 goto out;
1435         ret = 0;
1436
1437  out:
1438         vfree(regbuf);
1439         return ret;
1440 }
1441
1442 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1443 {
1444         struct ethtool_value reset;
1445         int ret;
1446
1447         if (!dev->ethtool_ops->reset)
1448                 return -EOPNOTSUPP;
1449
1450         if (copy_from_user(&reset, useraddr, sizeof(reset)))
1451                 return -EFAULT;
1452
1453         ret = dev->ethtool_ops->reset(dev, &reset.data);
1454         if (ret)
1455                 return ret;
1456
1457         if (copy_to_user(useraddr, &reset, sizeof(reset)))
1458                 return -EFAULT;
1459         return 0;
1460 }
1461
1462 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1463 {
1464         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1465
1466         if (!dev->ethtool_ops->get_wol)
1467                 return -EOPNOTSUPP;
1468
1469         dev->ethtool_ops->get_wol(dev, &wol);
1470
1471         if (copy_to_user(useraddr, &wol, sizeof(wol)))
1472                 return -EFAULT;
1473         return 0;
1474 }
1475
1476 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1477 {
1478         struct ethtool_wolinfo wol;
1479
1480         if (!dev->ethtool_ops->set_wol)
1481                 return -EOPNOTSUPP;
1482
1483         if (copy_from_user(&wol, useraddr, sizeof(wol)))
1484                 return -EFAULT;
1485
1486         return dev->ethtool_ops->set_wol(dev, &wol);
1487 }
1488
1489 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1490 {
1491         struct ethtool_eee edata;
1492         int rc;
1493
1494         if (!dev->ethtool_ops->get_eee)
1495                 return -EOPNOTSUPP;
1496
1497         memset(&edata, 0, sizeof(struct ethtool_eee));
1498         edata.cmd = ETHTOOL_GEEE;
1499         rc = dev->ethtool_ops->get_eee(dev, &edata);
1500
1501         if (rc)
1502                 return rc;
1503
1504         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1505                 return -EFAULT;
1506
1507         return 0;
1508 }
1509
1510 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1511 {
1512         struct ethtool_eee edata;
1513
1514         if (!dev->ethtool_ops->set_eee)
1515                 return -EOPNOTSUPP;
1516
1517         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1518                 return -EFAULT;
1519
1520         return dev->ethtool_ops->set_eee(dev, &edata);
1521 }
1522
1523 static int ethtool_nway_reset(struct net_device *dev)
1524 {
1525         if (!dev->ethtool_ops->nway_reset)
1526                 return -EOPNOTSUPP;
1527
1528         return dev->ethtool_ops->nway_reset(dev);
1529 }
1530
1531 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1532 {
1533         struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1534
1535         if (!dev->ethtool_ops->get_link)
1536                 return -EOPNOTSUPP;
1537
1538         edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1539
1540         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1541                 return -EFAULT;
1542         return 0;
1543 }
1544
1545 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1546                                   int (*getter)(struct net_device *,
1547                                                 struct ethtool_eeprom *, u8 *),
1548                                   u32 total_len)
1549 {
1550         struct ethtool_eeprom eeprom;
1551         void __user *userbuf = useraddr + sizeof(eeprom);
1552         u32 bytes_remaining;
1553         u8 *data;
1554         int ret = 0;
1555
1556         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1557                 return -EFAULT;
1558
1559         /* Check for wrap and zero */
1560         if (eeprom.offset + eeprom.len <= eeprom.offset)
1561                 return -EINVAL;
1562
1563         /* Check for exceeding total eeprom len */
1564         if (eeprom.offset + eeprom.len > total_len)
1565                 return -EINVAL;
1566
1567         data = kmalloc(PAGE_SIZE, GFP_USER);
1568         if (!data)
1569                 return -ENOMEM;
1570
1571         bytes_remaining = eeprom.len;
1572         while (bytes_remaining > 0) {
1573                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1574
1575                 ret = getter(dev, &eeprom, data);
1576                 if (ret)
1577                         break;
1578                 if (copy_to_user(userbuf, data, eeprom.len)) {
1579                         ret = -EFAULT;
1580                         break;
1581                 }
1582                 userbuf += eeprom.len;
1583                 eeprom.offset += eeprom.len;
1584                 bytes_remaining -= eeprom.len;
1585         }
1586
1587         eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1588         eeprom.offset -= eeprom.len;
1589         if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1590                 ret = -EFAULT;
1591
1592         kfree(data);
1593         return ret;
1594 }
1595
1596 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1597 {
1598         const struct ethtool_ops *ops = dev->ethtool_ops;
1599
1600         if (!ops->get_eeprom || !ops->get_eeprom_len ||
1601             !ops->get_eeprom_len(dev))
1602                 return -EOPNOTSUPP;
1603
1604         return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1605                                       ops->get_eeprom_len(dev));
1606 }
1607
1608 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1609 {
1610         struct ethtool_eeprom eeprom;
1611         const struct ethtool_ops *ops = dev->ethtool_ops;
1612         void __user *userbuf = useraddr + sizeof(eeprom);
1613         u32 bytes_remaining;
1614         u8 *data;
1615         int ret = 0;
1616
1617         if (!ops->set_eeprom || !ops->get_eeprom_len ||
1618             !ops->get_eeprom_len(dev))
1619                 return -EOPNOTSUPP;
1620
1621         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1622                 return -EFAULT;
1623
1624         /* Check for wrap and zero */
1625         if (eeprom.offset + eeprom.len <= eeprom.offset)
1626                 return -EINVAL;
1627
1628         /* Check for exceeding total eeprom len */
1629         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1630                 return -EINVAL;
1631
1632         data = kmalloc(PAGE_SIZE, GFP_USER);
1633         if (!data)
1634                 return -ENOMEM;
1635
1636         bytes_remaining = eeprom.len;
1637         while (bytes_remaining > 0) {
1638                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1639
1640                 if (copy_from_user(data, userbuf, eeprom.len)) {
1641                         ret = -EFAULT;
1642                         break;
1643                 }
1644                 ret = ops->set_eeprom(dev, &eeprom, data);
1645                 if (ret)
1646                         break;
1647                 userbuf += eeprom.len;
1648                 eeprom.offset += eeprom.len;
1649                 bytes_remaining -= eeprom.len;
1650         }
1651
1652         kfree(data);
1653         return ret;
1654 }
1655
1656 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1657                                                    void __user *useraddr)
1658 {
1659         struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1660
1661         if (!dev->ethtool_ops->get_coalesce)
1662                 return -EOPNOTSUPP;
1663
1664         dev->ethtool_ops->get_coalesce(dev, &coalesce);
1665
1666         if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1667                 return -EFAULT;
1668         return 0;
1669 }
1670
1671 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1672                                                    void __user *useraddr)
1673 {
1674         struct ethtool_coalesce coalesce;
1675
1676         if (!dev->ethtool_ops->set_coalesce)
1677                 return -EOPNOTSUPP;
1678
1679         if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1680                 return -EFAULT;
1681
1682         return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1683 }
1684
1685 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1686 {
1687         struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1688
1689         if (!dev->ethtool_ops->get_ringparam)
1690                 return -EOPNOTSUPP;
1691
1692         dev->ethtool_ops->get_ringparam(dev, &ringparam);
1693
1694         if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1695                 return -EFAULT;
1696         return 0;
1697 }
1698
1699 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1700 {
1701         struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1702
1703         if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1704                 return -EOPNOTSUPP;
1705
1706         if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1707                 return -EFAULT;
1708
1709         dev->ethtool_ops->get_ringparam(dev, &max);
1710
1711         /* ensure new ring parameters are within the maximums */
1712         if (ringparam.rx_pending > max.rx_max_pending ||
1713             ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1714             ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1715             ringparam.tx_pending > max.tx_max_pending)
1716                 return -EINVAL;
1717
1718         return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1719 }
1720
1721 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1722                                                    void __user *useraddr)
1723 {
1724         struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1725
1726         if (!dev->ethtool_ops->get_channels)
1727                 return -EOPNOTSUPP;
1728
1729         dev->ethtool_ops->get_channels(dev, &channels);
1730
1731         if (copy_to_user(useraddr, &channels, sizeof(channels)))
1732                 return -EFAULT;
1733         return 0;
1734 }
1735
1736 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1737                                                    void __user *useraddr)
1738 {
1739         struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1740         u32 max_rx_in_use = 0;
1741
1742         if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1743                 return -EOPNOTSUPP;
1744
1745         if (copy_from_user(&channels, useraddr, sizeof(channels)))
1746                 return -EFAULT;
1747
1748         dev->ethtool_ops->get_channels(dev, &max);
1749
1750         /* ensure new counts are within the maximums */
1751         if ((channels.rx_count > max.max_rx) ||
1752             (channels.tx_count > max.max_tx) ||
1753             (channels.combined_count > max.max_combined) ||
1754             (channels.other_count > max.max_other))
1755                 return -EINVAL;
1756
1757         /* ensure the new Rx count fits within the configured Rx flow
1758          * indirection table settings */
1759         if (netif_is_rxfh_configured(dev) &&
1760             !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1761             (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1762             return -EINVAL;
1763
1764         return dev->ethtool_ops->set_channels(dev, &channels);
1765 }
1766
1767 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1768 {
1769         struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1770
1771         if (!dev->ethtool_ops->get_pauseparam)
1772                 return -EOPNOTSUPP;
1773
1774         dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1775
1776         if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1777                 return -EFAULT;
1778         return 0;
1779 }
1780
1781 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1782 {
1783         struct ethtool_pauseparam pauseparam;
1784
1785         if (!dev->ethtool_ops->set_pauseparam)
1786                 return -EOPNOTSUPP;
1787
1788         if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1789                 return -EFAULT;
1790
1791         return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1792 }
1793
1794 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1795 {
1796         struct ethtool_test test;
1797         const struct ethtool_ops *ops = dev->ethtool_ops;
1798         u64 *data;
1799         int ret, test_len;
1800
1801         if (!ops->self_test || !ops->get_sset_count)
1802                 return -EOPNOTSUPP;
1803
1804         test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1805         if (test_len < 0)
1806                 return test_len;
1807         WARN_ON(test_len == 0);
1808
1809         if (copy_from_user(&test, useraddr, sizeof(test)))
1810                 return -EFAULT;
1811
1812         test.len = test_len;
1813         data = kmalloc(test_len * sizeof(u64), GFP_USER);
1814         if (!data)
1815                 return -ENOMEM;
1816
1817         ops->self_test(dev, &test, data);
1818
1819         ret = -EFAULT;
1820         if (copy_to_user(useraddr, &test, sizeof(test)))
1821                 goto out;
1822         useraddr += sizeof(test);
1823         if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1824                 goto out;
1825         ret = 0;
1826
1827  out:
1828         kfree(data);
1829         return ret;
1830 }
1831
1832 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1833 {
1834         struct ethtool_gstrings gstrings;
1835         u8 *data;
1836         int ret;
1837
1838         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1839                 return -EFAULT;
1840
1841         ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1842         if (ret < 0)
1843                 return ret;
1844         if (ret > S32_MAX / ETH_GSTRING_LEN)
1845                 return -ENOMEM;
1846         WARN_ON_ONCE(!ret);
1847
1848         gstrings.len = ret;
1849         data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1850         if (gstrings.len && !data)
1851                 return -ENOMEM;
1852
1853         __ethtool_get_strings(dev, gstrings.string_set, data);
1854
1855         ret = -EFAULT;
1856         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1857                 goto out;
1858         useraddr += sizeof(gstrings);
1859         if (gstrings.len &&
1860             copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1861                 goto out;
1862         ret = 0;
1863
1864 out:
1865         vfree(data);
1866         return ret;
1867 }
1868
1869 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1870 {
1871         struct ethtool_value id;
1872         static bool busy;
1873         const struct ethtool_ops *ops = dev->ethtool_ops;
1874         int rc;
1875
1876         if (!ops->set_phys_id)
1877                 return -EOPNOTSUPP;
1878
1879         if (busy)
1880                 return -EBUSY;
1881
1882         if (copy_from_user(&id, useraddr, sizeof(id)))
1883                 return -EFAULT;
1884
1885         rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1886         if (rc < 0)
1887                 return rc;
1888
1889         /* Drop the RTNL lock while waiting, but prevent reentry or
1890          * removal of the device.
1891          */
1892         busy = true;
1893         dev_hold(dev);
1894         rtnl_unlock();
1895
1896         if (rc == 0) {
1897                 /* Driver will handle this itself */
1898                 schedule_timeout_interruptible(
1899                         id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1900         } else {
1901                 /* Driver expects to be called at twice the frequency in rc */
1902                 int n = rc * 2, i, interval = HZ / n;
1903
1904                 /* Count down seconds */
1905                 do {
1906                         /* Count down iterations per second */
1907                         i = n;
1908                         do {
1909                                 rtnl_lock();
1910                                 rc = ops->set_phys_id(dev,
1911                                     (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1912                                 rtnl_unlock();
1913                                 if (rc)
1914                                         break;
1915                                 schedule_timeout_interruptible(interval);
1916                         } while (!signal_pending(current) && --i != 0);
1917                 } while (!signal_pending(current) &&
1918                          (id.data == 0 || --id.data != 0));
1919         }
1920
1921         rtnl_lock();
1922         dev_put(dev);
1923         busy = false;
1924
1925         (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1926         return rc;
1927 }
1928
1929 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1930 {
1931         struct ethtool_stats stats;
1932         const struct ethtool_ops *ops = dev->ethtool_ops;
1933         u64 *data;
1934         int ret, n_stats;
1935
1936         if (!ops->get_ethtool_stats || !ops->get_sset_count)
1937                 return -EOPNOTSUPP;
1938
1939         n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1940         if (n_stats < 0)
1941                 return n_stats;
1942         if (n_stats > S32_MAX / sizeof(u64))
1943                 return -ENOMEM;
1944         WARN_ON_ONCE(!n_stats);
1945         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1946                 return -EFAULT;
1947
1948         stats.n_stats = n_stats;
1949         data = vzalloc(n_stats * sizeof(u64));
1950         if (n_stats && !data)
1951                 return -ENOMEM;
1952
1953         ops->get_ethtool_stats(dev, &stats, data);
1954
1955         ret = -EFAULT;
1956         if (copy_to_user(useraddr, &stats, sizeof(stats)))
1957                 goto out;
1958         useraddr += sizeof(stats);
1959         if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1960                 goto out;
1961         ret = 0;
1962
1963  out:
1964         vfree(data);
1965         return ret;
1966 }
1967
1968 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1969 {
1970         const struct ethtool_ops *ops = dev->ethtool_ops;
1971         struct phy_device *phydev = dev->phydev;
1972         struct ethtool_stats stats;
1973         u64 *data;
1974         int ret, n_stats;
1975
1976         if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1977                 return -EOPNOTSUPP;
1978
1979         if (dev->phydev && !ops->get_ethtool_phy_stats)
1980                 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1981         else
1982                 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1983         if (n_stats < 0)
1984                 return n_stats;
1985         if (n_stats > S32_MAX / sizeof(u64))
1986                 return -ENOMEM;
1987         WARN_ON_ONCE(!n_stats);
1988
1989         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1990                 return -EFAULT;
1991
1992         stats.n_stats = n_stats;
1993         data = vzalloc(n_stats * sizeof(u64));
1994         if (n_stats && !data)
1995                 return -ENOMEM;
1996
1997         if (dev->phydev && !ops->get_ethtool_phy_stats) {
1998                 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1999                 if (ret < 0)
2000                         return ret;
2001         } else {
2002                 ops->get_ethtool_phy_stats(dev, &stats, data);
2003         }
2004
2005         ret = -EFAULT;
2006         if (copy_to_user(useraddr, &stats, sizeof(stats)))
2007                 goto out;
2008         useraddr += sizeof(stats);
2009         if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2010                 goto out;
2011         ret = 0;
2012
2013  out:
2014         vfree(data);
2015         return ret;
2016 }
2017
2018 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2019 {
2020         struct ethtool_perm_addr epaddr;
2021
2022         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2023                 return -EFAULT;
2024
2025         if (epaddr.size < dev->addr_len)
2026                 return -ETOOSMALL;
2027         epaddr.size = dev->addr_len;
2028
2029         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2030                 return -EFAULT;
2031         useraddr += sizeof(epaddr);
2032         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2033                 return -EFAULT;
2034         return 0;
2035 }
2036
2037 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2038                              u32 cmd, u32 (*actor)(struct net_device *))
2039 {
2040         struct ethtool_value edata = { .cmd = cmd };
2041
2042         if (!actor)
2043                 return -EOPNOTSUPP;
2044
2045         edata.data = actor(dev);
2046
2047         if (copy_to_user(useraddr, &edata, sizeof(edata)))
2048                 return -EFAULT;
2049         return 0;
2050 }
2051
2052 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2053                              void (*actor)(struct net_device *, u32))
2054 {
2055         struct ethtool_value edata;
2056
2057         if (!actor)
2058                 return -EOPNOTSUPP;
2059
2060         if (copy_from_user(&edata, useraddr, sizeof(edata)))
2061                 return -EFAULT;
2062
2063         actor(dev, edata.data);
2064         return 0;
2065 }
2066
2067 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2068                              int (*actor)(struct net_device *, u32))
2069 {
2070         struct ethtool_value edata;
2071
2072         if (!actor)
2073                 return -EOPNOTSUPP;
2074
2075         if (copy_from_user(&edata, useraddr, sizeof(edata)))
2076                 return -EFAULT;
2077
2078         return actor(dev, edata.data);
2079 }
2080
2081 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2082                                                    char __user *useraddr)
2083 {
2084         struct ethtool_flash efl;
2085
2086         if (copy_from_user(&efl, useraddr, sizeof(efl)))
2087                 return -EFAULT;
2088
2089         if (!dev->ethtool_ops->flash_device)
2090                 return -EOPNOTSUPP;
2091
2092         efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2093
2094         return dev->ethtool_ops->flash_device(dev, &efl);
2095 }
2096
2097 static int ethtool_set_dump(struct net_device *dev,
2098                         void __user *useraddr)
2099 {
2100         struct ethtool_dump dump;
2101
2102         if (!dev->ethtool_ops->set_dump)
2103                 return -EOPNOTSUPP;
2104
2105         if (copy_from_user(&dump, useraddr, sizeof(dump)))
2106                 return -EFAULT;
2107
2108         return dev->ethtool_ops->set_dump(dev, &dump);
2109 }
2110
2111 static int ethtool_get_dump_flag(struct net_device *dev,
2112                                 void __user *useraddr)
2113 {
2114         int ret;
2115         struct ethtool_dump dump;
2116         const struct ethtool_ops *ops = dev->ethtool_ops;
2117
2118         if (!ops->get_dump_flag)
2119                 return -EOPNOTSUPP;
2120
2121         if (copy_from_user(&dump, useraddr, sizeof(dump)))
2122                 return -EFAULT;
2123
2124         ret = ops->get_dump_flag(dev, &dump);
2125         if (ret)
2126                 return ret;
2127
2128         if (copy_to_user(useraddr, &dump, sizeof(dump)))
2129                 return -EFAULT;
2130         return 0;
2131 }
2132
2133 static int ethtool_get_dump_data(struct net_device *dev,
2134                                 void __user *useraddr)
2135 {
2136         int ret;
2137         __u32 len;
2138         struct ethtool_dump dump, tmp;
2139         const struct ethtool_ops *ops = dev->ethtool_ops;
2140         void *data = NULL;
2141
2142         if (!ops->get_dump_data || !ops->get_dump_flag)
2143                 return -EOPNOTSUPP;
2144
2145         if (copy_from_user(&dump, useraddr, sizeof(dump)))
2146                 return -EFAULT;
2147
2148         memset(&tmp, 0, sizeof(tmp));
2149         tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2150         ret = ops->get_dump_flag(dev, &tmp);
2151         if (ret)
2152                 return ret;
2153
2154         len = min(tmp.len, dump.len);
2155         if (!len)
2156                 return -EFAULT;
2157
2158         /* Don't ever let the driver think there's more space available
2159          * than it requested with .get_dump_flag().
2160          */
2161         dump.len = len;
2162
2163         /* Always allocate enough space to hold the whole thing so that the
2164          * driver does not need to check the length and bother with partial
2165          * dumping.
2166          */
2167         data = vzalloc(tmp.len);
2168         if (!data)
2169                 return -ENOMEM;
2170         ret = ops->get_dump_data(dev, &dump, data);
2171         if (ret)
2172                 goto out;
2173
2174         /* There are two sane possibilities:
2175          * 1. The driver's .get_dump_data() does not touch dump.len.
2176          * 2. Or it may set dump.len to how much it really writes, which
2177          *    should be tmp.len (or len if it can do a partial dump).
2178          * In any case respond to userspace with the actual length of data
2179          * it's receiving.
2180          */
2181         WARN_ON(dump.len != len && dump.len != tmp.len);
2182         dump.len = len;
2183
2184         if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2185                 ret = -EFAULT;
2186                 goto out;
2187         }
2188         useraddr += offsetof(struct ethtool_dump, data);
2189         if (copy_to_user(useraddr, data, len))
2190                 ret = -EFAULT;
2191 out:
2192         vfree(data);
2193         return ret;
2194 }
2195
2196 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2197 {
2198         int err = 0;
2199         struct ethtool_ts_info info;
2200         const struct ethtool_ops *ops = dev->ethtool_ops;
2201         struct phy_device *phydev = dev->phydev;
2202
2203         memset(&info, 0, sizeof(info));
2204         info.cmd = ETHTOOL_GET_TS_INFO;
2205
2206         if (phydev && phydev->drv && phydev->drv->ts_info) {
2207                 err = phydev->drv->ts_info(phydev, &info);
2208         } else if (ops->get_ts_info) {
2209                 err = ops->get_ts_info(dev, &info);
2210         } else {
2211                 info.so_timestamping =
2212                         SOF_TIMESTAMPING_RX_SOFTWARE |
2213                         SOF_TIMESTAMPING_SOFTWARE;
2214                 info.phc_index = -1;
2215         }
2216
2217         if (err)
2218                 return err;
2219
2220         if (copy_to_user(useraddr, &info, sizeof(info)))
2221                 err = -EFAULT;
2222
2223         return err;
2224 }
2225
2226 static int __ethtool_get_module_info(struct net_device *dev,
2227                                      struct ethtool_modinfo *modinfo)
2228 {
2229         const struct ethtool_ops *ops = dev->ethtool_ops;
2230         struct phy_device *phydev = dev->phydev;
2231
2232         if (dev->sfp_bus)
2233                 return sfp_get_module_info(dev->sfp_bus, modinfo);
2234
2235         if (phydev && phydev->drv && phydev->drv->module_info)
2236                 return phydev->drv->module_info(phydev, modinfo);
2237
2238         if (ops->get_module_info)
2239                 return ops->get_module_info(dev, modinfo);
2240
2241         return -EOPNOTSUPP;
2242 }
2243
2244 static int ethtool_get_module_info(struct net_device *dev,
2245                                    void __user *useraddr)
2246 {
2247         int ret;
2248         struct ethtool_modinfo modinfo;
2249
2250         if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2251                 return -EFAULT;
2252
2253         ret = __ethtool_get_module_info(dev, &modinfo);
2254         if (ret)
2255                 return ret;
2256
2257         if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2258                 return -EFAULT;
2259
2260         return 0;
2261 }
2262
2263 static int __ethtool_get_module_eeprom(struct net_device *dev,
2264                                        struct ethtool_eeprom *ee, u8 *data)
2265 {
2266         const struct ethtool_ops *ops = dev->ethtool_ops;
2267         struct phy_device *phydev = dev->phydev;
2268
2269         if (dev->sfp_bus)
2270                 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2271
2272         if (phydev && phydev->drv && phydev->drv->module_eeprom)
2273                 return phydev->drv->module_eeprom(phydev, ee, data);
2274
2275         if (ops->get_module_eeprom)
2276                 return ops->get_module_eeprom(dev, ee, data);
2277
2278         return -EOPNOTSUPP;
2279 }
2280
2281 static int ethtool_get_module_eeprom(struct net_device *dev,
2282                                      void __user *useraddr)
2283 {
2284         int ret;
2285         struct ethtool_modinfo modinfo;
2286
2287         ret = __ethtool_get_module_info(dev, &modinfo);
2288         if (ret)
2289                 return ret;
2290
2291         return ethtool_get_any_eeprom(dev, useraddr,
2292                                       __ethtool_get_module_eeprom,
2293                                       modinfo.eeprom_len);
2294 }
2295
2296 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2297 {
2298         switch (tuna->id) {
2299         case ETHTOOL_RX_COPYBREAK:
2300         case ETHTOOL_TX_COPYBREAK:
2301                 if (tuna->len != sizeof(u32) ||
2302                     tuna->type_id != ETHTOOL_TUNABLE_U32)
2303                         return -EINVAL;
2304                 break;
2305         case ETHTOOL_PFC_PREVENTION_TOUT:
2306                 if (tuna->len != sizeof(u16) ||
2307                     tuna->type_id != ETHTOOL_TUNABLE_U16)
2308                         return -EINVAL;
2309                 break;
2310         default:
2311                 return -EINVAL;
2312         }
2313
2314         return 0;
2315 }
2316
2317 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2318 {
2319         int ret;
2320         struct ethtool_tunable tuna;
2321         const struct ethtool_ops *ops = dev->ethtool_ops;
2322         void *data;
2323
2324         if (!ops->get_tunable)
2325                 return -EOPNOTSUPP;
2326         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2327                 return -EFAULT;
2328         ret = ethtool_tunable_valid(&tuna);
2329         if (ret)
2330                 return ret;
2331         data = kmalloc(tuna.len, GFP_USER);
2332         if (!data)
2333                 return -ENOMEM;
2334         ret = ops->get_tunable(dev, &tuna, data);
2335         if (ret)
2336                 goto out;
2337         useraddr += sizeof(tuna);
2338         ret = -EFAULT;
2339         if (copy_to_user(useraddr, data, tuna.len))
2340                 goto out;
2341         ret = 0;
2342
2343 out:
2344         kfree(data);
2345         return ret;
2346 }
2347
2348 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2349 {
2350         int ret;
2351         struct ethtool_tunable tuna;
2352         const struct ethtool_ops *ops = dev->ethtool_ops;
2353         void *data;
2354
2355         if (!ops->set_tunable)
2356                 return -EOPNOTSUPP;
2357         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2358                 return -EFAULT;
2359         ret = ethtool_tunable_valid(&tuna);
2360         if (ret)
2361                 return ret;
2362         useraddr += sizeof(tuna);
2363         data = memdup_user(useraddr, tuna.len);
2364         if (IS_ERR(data))
2365                 return PTR_ERR(data);
2366         ret = ops->set_tunable(dev, &tuna, data);
2367
2368         kfree(data);
2369         return ret;
2370 }
2371
2372 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2373                                           void __user *useraddr,
2374                                           struct ethtool_per_queue_op *per_queue_opt)
2375 {
2376         u32 bit;
2377         int ret;
2378         DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2379
2380         if (!dev->ethtool_ops->get_per_queue_coalesce)
2381                 return -EOPNOTSUPP;
2382
2383         useraddr += sizeof(*per_queue_opt);
2384
2385         bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2386                           MAX_NUM_QUEUE);
2387
2388         for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2389                 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2390
2391                 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2392                 if (ret != 0)
2393                         return ret;
2394                 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2395                         return -EFAULT;
2396                 useraddr += sizeof(coalesce);
2397         }
2398
2399         return 0;
2400 }
2401
2402 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2403                                           void __user *useraddr,
2404                                           struct ethtool_per_queue_op *per_queue_opt)
2405 {
2406         u32 bit;
2407         int i, ret = 0;
2408         int n_queue;
2409         struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2410         DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2411
2412         if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2413             (!dev->ethtool_ops->get_per_queue_coalesce))
2414                 return -EOPNOTSUPP;
2415
2416         useraddr += sizeof(*per_queue_opt);
2417
2418         bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2419         n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2420         tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2421         if (!backup)
2422                 return -ENOMEM;
2423
2424         for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2425                 struct ethtool_coalesce coalesce;
2426
2427                 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2428                 if (ret != 0)
2429                         goto roll_back;
2430
2431                 tmp++;
2432
2433                 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2434                         ret = -EFAULT;
2435                         goto roll_back;
2436                 }
2437
2438                 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2439                 if (ret != 0)
2440                         goto roll_back;
2441
2442                 useraddr += sizeof(coalesce);
2443         }
2444
2445 roll_back:
2446         if (ret != 0) {
2447                 tmp = backup;
2448                 for_each_set_bit(i, queue_mask, bit) {
2449                         dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2450                         tmp++;
2451                 }
2452         }
2453         kfree(backup);
2454
2455         return ret;
2456 }
2457
2458 static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2459 {
2460         struct ethtool_per_queue_op per_queue_opt;
2461
2462         if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2463                 return -EFAULT;
2464
2465         switch (per_queue_opt.sub_command) {
2466         case ETHTOOL_GCOALESCE:
2467                 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2468         case ETHTOOL_SCOALESCE:
2469                 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2470         default:
2471                 return -EOPNOTSUPP;
2472         };
2473 }
2474
2475 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2476 {
2477         switch (tuna->id) {
2478         case ETHTOOL_PHY_DOWNSHIFT:
2479                 if (tuna->len != sizeof(u8) ||
2480                     tuna->type_id != ETHTOOL_TUNABLE_U8)
2481                         return -EINVAL;
2482                 break;
2483         default:
2484                 return -EINVAL;
2485         }
2486
2487         return 0;
2488 }
2489
2490 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2491 {
2492         int ret;
2493         struct ethtool_tunable tuna;
2494         struct phy_device *phydev = dev->phydev;
2495         void *data;
2496
2497         if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2498                 return -EOPNOTSUPP;
2499
2500         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2501                 return -EFAULT;
2502         ret = ethtool_phy_tunable_valid(&tuna);
2503         if (ret)
2504                 return ret;
2505         data = kmalloc(tuna.len, GFP_USER);
2506         if (!data)
2507                 return -ENOMEM;
2508         mutex_lock(&phydev->lock);
2509         ret = phydev->drv->get_tunable(phydev, &tuna, data);
2510         mutex_unlock(&phydev->lock);
2511         if (ret)
2512                 goto out;
2513         useraddr += sizeof(tuna);
2514         ret = -EFAULT;
2515         if (copy_to_user(useraddr, data, tuna.len))
2516                 goto out;
2517         ret = 0;
2518
2519 out:
2520         kfree(data);
2521         return ret;
2522 }
2523
2524 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2525 {
2526         int ret;
2527         struct ethtool_tunable tuna;
2528         struct phy_device *phydev = dev->phydev;
2529         void *data;
2530
2531         if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2532                 return -EOPNOTSUPP;
2533         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2534                 return -EFAULT;
2535         ret = ethtool_phy_tunable_valid(&tuna);
2536         if (ret)
2537                 return ret;
2538         useraddr += sizeof(tuna);
2539         data = memdup_user(useraddr, tuna.len);
2540         if (IS_ERR(data))
2541                 return PTR_ERR(data);
2542         mutex_lock(&phydev->lock);
2543         ret = phydev->drv->set_tunable(phydev, &tuna, data);
2544         mutex_unlock(&phydev->lock);
2545
2546         kfree(data);
2547         return ret;
2548 }
2549
2550 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2551 {
2552         struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2553         int rc;
2554
2555         if (!dev->ethtool_ops->get_fecparam)
2556                 return -EOPNOTSUPP;
2557
2558         rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2559         if (rc)
2560                 return rc;
2561
2562         if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2563                 return -EFAULT;
2564         return 0;
2565 }
2566
2567 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2568 {
2569         struct ethtool_fecparam fecparam;
2570
2571         if (!dev->ethtool_ops->set_fecparam)
2572                 return -EOPNOTSUPP;
2573
2574         if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2575                 return -EFAULT;
2576
2577         return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2578 }
2579
2580 /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
2581
2582 int dev_ethtool(struct net *net, struct ifreq *ifr)
2583 {
2584         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2585         void __user *useraddr = ifr->ifr_data;
2586         u32 ethcmd, sub_cmd;
2587         int rc;
2588         netdev_features_t old_features;
2589
2590         if (!dev || !netif_device_present(dev))
2591                 return -ENODEV;
2592
2593         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
2594                 return -EFAULT;
2595
2596         if (ethcmd == ETHTOOL_PERQUEUE) {
2597                 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2598                         return -EFAULT;
2599         } else {
2600                 sub_cmd = ethcmd;
2601         }
2602         /* Allow some commands to be done by anyone */
2603         switch (sub_cmd) {
2604         case ETHTOOL_GSET:
2605         case ETHTOOL_GDRVINFO:
2606         case ETHTOOL_GMSGLVL:
2607         case ETHTOOL_GLINK:
2608         case ETHTOOL_GCOALESCE:
2609         case ETHTOOL_GRINGPARAM:
2610         case ETHTOOL_GPAUSEPARAM:
2611         case ETHTOOL_GRXCSUM:
2612         case ETHTOOL_GTXCSUM:
2613         case ETHTOOL_GSG:
2614         case ETHTOOL_GSSET_INFO:
2615         case ETHTOOL_GSTRINGS:
2616         case ETHTOOL_GSTATS:
2617         case ETHTOOL_GPHYSTATS:
2618         case ETHTOOL_GTSO:
2619         case ETHTOOL_GPERMADDR:
2620         case ETHTOOL_GGSO:
2621         case ETHTOOL_GGRO:
2622         case ETHTOOL_GFLAGS:
2623         case ETHTOOL_GPFLAGS:
2624         case ETHTOOL_GRXFH:
2625         case ETHTOOL_GRXRINGS:
2626         case ETHTOOL_GRXCLSRLCNT:
2627         case ETHTOOL_GRXCLSRULE:
2628         case ETHTOOL_GRXCLSRLALL:
2629         case ETHTOOL_GRXFHINDIR:
2630         case ETHTOOL_GRSSH:
2631         case ETHTOOL_GFEATURES:
2632         case ETHTOOL_GCHANNELS:
2633         case ETHTOOL_GET_TS_INFO:
2634         case ETHTOOL_GEEE:
2635         case ETHTOOL_GTUNABLE:
2636         case ETHTOOL_PHY_GTUNABLE:
2637         case ETHTOOL_GLINKSETTINGS:
2638         case ETHTOOL_GFECPARAM:
2639                 break;
2640         default:
2641                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2642                         return -EPERM;
2643         }
2644
2645         if (dev->ethtool_ops->begin) {
2646                 rc = dev->ethtool_ops->begin(dev);
2647                 if (rc  < 0)
2648                         return rc;
2649         }
2650         old_features = dev->features;
2651
2652         switch (ethcmd) {
2653         case ETHTOOL_GSET:
2654                 rc = ethtool_get_settings(dev, useraddr);
2655                 break;
2656         case ETHTOOL_SSET:
2657                 rc = ethtool_set_settings(dev, useraddr);
2658                 break;
2659         case ETHTOOL_GDRVINFO:
2660                 rc = ethtool_get_drvinfo(dev, useraddr);
2661                 break;
2662         case ETHTOOL_GREGS:
2663                 rc = ethtool_get_regs(dev, useraddr);
2664                 break;
2665         case ETHTOOL_GWOL:
2666                 rc = ethtool_get_wol(dev, useraddr);
2667                 break;
2668         case ETHTOOL_SWOL:
2669                 rc = ethtool_set_wol(dev, useraddr);
2670                 break;
2671         case ETHTOOL_GMSGLVL:
2672                 rc = ethtool_get_value(dev, useraddr, ethcmd,
2673                                        dev->ethtool_ops->get_msglevel);
2674                 break;
2675         case ETHTOOL_SMSGLVL:
2676                 rc = ethtool_set_value_void(dev, useraddr,
2677                                        dev->ethtool_ops->set_msglevel);
2678                 break;
2679         case ETHTOOL_GEEE:
2680                 rc = ethtool_get_eee(dev, useraddr);
2681                 break;
2682         case ETHTOOL_SEEE:
2683                 rc = ethtool_set_eee(dev, useraddr);
2684                 break;
2685         case ETHTOOL_NWAY_RST:
2686                 rc = ethtool_nway_reset(dev);
2687                 break;
2688         case ETHTOOL_GLINK:
2689                 rc = ethtool_get_link(dev, useraddr);
2690                 break;
2691         case ETHTOOL_GEEPROM:
2692                 rc = ethtool_get_eeprom(dev, useraddr);
2693                 break;
2694         case ETHTOOL_SEEPROM:
2695                 rc = ethtool_set_eeprom(dev, useraddr);
2696                 break;
2697         case ETHTOOL_GCOALESCE:
2698                 rc = ethtool_get_coalesce(dev, useraddr);
2699                 break;
2700         case ETHTOOL_SCOALESCE:
2701                 rc = ethtool_set_coalesce(dev, useraddr);
2702                 break;
2703         case ETHTOOL_GRINGPARAM:
2704                 rc = ethtool_get_ringparam(dev, useraddr);
2705                 break;
2706         case ETHTOOL_SRINGPARAM:
2707                 rc = ethtool_set_ringparam(dev, useraddr);
2708                 break;
2709         case ETHTOOL_GPAUSEPARAM:
2710                 rc = ethtool_get_pauseparam(dev, useraddr);
2711                 break;
2712         case ETHTOOL_SPAUSEPARAM:
2713                 rc = ethtool_set_pauseparam(dev, useraddr);
2714                 break;
2715         case ETHTOOL_TEST:
2716                 rc = ethtool_self_test(dev, useraddr);
2717                 break;
2718         case ETHTOOL_GSTRINGS:
2719                 rc = ethtool_get_strings(dev, useraddr);
2720                 break;
2721         case ETHTOOL_PHYS_ID:
2722                 rc = ethtool_phys_id(dev, useraddr);
2723                 break;
2724         case ETHTOOL_GSTATS:
2725                 rc = ethtool_get_stats(dev, useraddr);
2726                 break;
2727         case ETHTOOL_GPERMADDR:
2728                 rc = ethtool_get_perm_addr(dev, useraddr);
2729                 break;
2730         case ETHTOOL_GFLAGS:
2731                 rc = ethtool_get_value(dev, useraddr, ethcmd,
2732                                         __ethtool_get_flags);
2733                 break;
2734         case ETHTOOL_SFLAGS:
2735                 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2736                 break;
2737         case ETHTOOL_GPFLAGS:
2738                 rc = ethtool_get_value(dev, useraddr, ethcmd,
2739                                        dev->ethtool_ops->get_priv_flags);
2740                 break;
2741         case ETHTOOL_SPFLAGS:
2742                 rc = ethtool_set_value(dev, useraddr,
2743                                        dev->ethtool_ops->set_priv_flags);
2744                 break;
2745         case ETHTOOL_GRXFH:
2746         case ETHTOOL_GRXRINGS:
2747         case ETHTOOL_GRXCLSRLCNT:
2748         case ETHTOOL_GRXCLSRULE:
2749         case ETHTOOL_GRXCLSRLALL:
2750                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2751                 break;
2752         case ETHTOOL_SRXFH:
2753         case ETHTOOL_SRXCLSRLDEL:
2754         case ETHTOOL_SRXCLSRLINS:
2755                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2756                 break;
2757         case ETHTOOL_FLASHDEV:
2758                 rc = ethtool_flash_device(dev, useraddr);
2759                 break;
2760         case ETHTOOL_RESET:
2761                 rc = ethtool_reset(dev, useraddr);
2762                 break;
2763         case ETHTOOL_GSSET_INFO:
2764                 rc = ethtool_get_sset_info(dev, useraddr);
2765                 break;
2766         case ETHTOOL_GRXFHINDIR:
2767                 rc = ethtool_get_rxfh_indir(dev, useraddr);
2768                 break;
2769         case ETHTOOL_SRXFHINDIR:
2770                 rc = ethtool_set_rxfh_indir(dev, useraddr);
2771                 break;
2772         case ETHTOOL_GRSSH:
2773                 rc = ethtool_get_rxfh(dev, useraddr);
2774                 break;
2775         case ETHTOOL_SRSSH:
2776                 rc = ethtool_set_rxfh(dev, useraddr);
2777                 break;
2778         case ETHTOOL_GFEATURES:
2779                 rc = ethtool_get_features(dev, useraddr);
2780                 break;
2781         case ETHTOOL_SFEATURES:
2782                 rc = ethtool_set_features(dev, useraddr);
2783                 break;
2784         case ETHTOOL_GTXCSUM:
2785         case ETHTOOL_GRXCSUM:
2786         case ETHTOOL_GSG:
2787         case ETHTOOL_GTSO:
2788         case ETHTOOL_GGSO:
2789         case ETHTOOL_GGRO:
2790                 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2791                 break;
2792         case ETHTOOL_STXCSUM:
2793         case ETHTOOL_SRXCSUM:
2794         case ETHTOOL_SSG:
2795         case ETHTOOL_STSO:
2796         case ETHTOOL_SGSO:
2797         case ETHTOOL_SGRO:
2798                 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2799                 break;
2800         case ETHTOOL_GCHANNELS:
2801                 rc = ethtool_get_channels(dev, useraddr);
2802                 break;
2803         case ETHTOOL_SCHANNELS:
2804                 rc = ethtool_set_channels(dev, useraddr);
2805                 break;
2806         case ETHTOOL_SET_DUMP:
2807                 rc = ethtool_set_dump(dev, useraddr);
2808                 break;
2809         case ETHTOOL_GET_DUMP_FLAG:
2810                 rc = ethtool_get_dump_flag(dev, useraddr);
2811                 break;
2812         case ETHTOOL_GET_DUMP_DATA:
2813                 rc = ethtool_get_dump_data(dev, useraddr);
2814                 break;
2815         case ETHTOOL_GET_TS_INFO:
2816                 rc = ethtool_get_ts_info(dev, useraddr);
2817                 break;
2818         case ETHTOOL_GMODULEINFO:
2819                 rc = ethtool_get_module_info(dev, useraddr);
2820                 break;
2821         case ETHTOOL_GMODULEEEPROM:
2822                 rc = ethtool_get_module_eeprom(dev, useraddr);
2823                 break;
2824         case ETHTOOL_GTUNABLE:
2825                 rc = ethtool_get_tunable(dev, useraddr);
2826                 break;
2827         case ETHTOOL_STUNABLE:
2828                 rc = ethtool_set_tunable(dev, useraddr);
2829                 break;
2830         case ETHTOOL_GPHYSTATS:
2831                 rc = ethtool_get_phy_stats(dev, useraddr);
2832                 break;
2833         case ETHTOOL_PERQUEUE:
2834                 rc = ethtool_set_per_queue(dev, useraddr);
2835                 break;
2836         case ETHTOOL_GLINKSETTINGS:
2837                 rc = ethtool_get_link_ksettings(dev, useraddr);
2838                 break;
2839         case ETHTOOL_SLINKSETTINGS:
2840                 rc = ethtool_set_link_ksettings(dev, useraddr);
2841                 break;
2842         case ETHTOOL_PHY_GTUNABLE:
2843                 rc = get_phy_tunable(dev, useraddr);
2844                 break;
2845         case ETHTOOL_PHY_STUNABLE:
2846                 rc = set_phy_tunable(dev, useraddr);
2847                 break;
2848         case ETHTOOL_GFECPARAM:
2849                 rc = ethtool_get_fecparam(dev, useraddr);
2850                 break;
2851         case ETHTOOL_SFECPARAM:
2852                 rc = ethtool_set_fecparam(dev, useraddr);
2853                 break;
2854         default:
2855                 rc = -EOPNOTSUPP;
2856         }
2857
2858         if (dev->ethtool_ops->complete)
2859                 dev->ethtool_ops->complete(dev);
2860
2861         if (old_features != dev->features)
2862                 netdev_features_change(dev);
2863
2864         return rc;
2865 }