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