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