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