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