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