GNU Linux-libre 4.19.281-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         if (WARN_ON_ONCE(!n_stats))
2027                 return -EOPNOTSUPP;
2028
2029         if (copy_from_user(&stats, useraddr, sizeof(stats)))
2030                 return -EFAULT;
2031
2032         stats.n_stats = n_stats;
2033
2034         if (n_stats) {
2035                 data = vzalloc(array_size(n_stats, sizeof(u64)));
2036                 if (!data)
2037                         return -ENOMEM;
2038
2039                 if (dev->phydev && !ops->get_ethtool_phy_stats) {
2040                         ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2041                         if (ret < 0)
2042                                 goto out;
2043                 } else {
2044                         ops->get_ethtool_phy_stats(dev, &stats, data);
2045                 }
2046         } else {
2047                 data = NULL;
2048         }
2049
2050         ret = -EFAULT;
2051         if (copy_to_user(useraddr, &stats, sizeof(stats)))
2052                 goto out;
2053         useraddr += sizeof(stats);
2054         if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2055                 goto out;
2056         ret = 0;
2057
2058  out:
2059         vfree(data);
2060         return ret;
2061 }
2062
2063 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2064 {
2065         struct ethtool_perm_addr epaddr;
2066
2067         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2068                 return -EFAULT;
2069
2070         if (epaddr.size < dev->addr_len)
2071                 return -ETOOSMALL;
2072         epaddr.size = dev->addr_len;
2073
2074         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2075                 return -EFAULT;
2076         useraddr += sizeof(epaddr);
2077         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2078                 return -EFAULT;
2079         return 0;
2080 }
2081
2082 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2083                              u32 cmd, u32 (*actor)(struct net_device *))
2084 {
2085         struct ethtool_value edata = { .cmd = cmd };
2086
2087         if (!actor)
2088                 return -EOPNOTSUPP;
2089
2090         edata.data = actor(dev);
2091
2092         if (copy_to_user(useraddr, &edata, sizeof(edata)))
2093                 return -EFAULT;
2094         return 0;
2095 }
2096
2097 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2098                              void (*actor)(struct net_device *, u32))
2099 {
2100         struct ethtool_value edata;
2101
2102         if (!actor)
2103                 return -EOPNOTSUPP;
2104
2105         if (copy_from_user(&edata, useraddr, sizeof(edata)))
2106                 return -EFAULT;
2107
2108         actor(dev, edata.data);
2109         return 0;
2110 }
2111
2112 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2113                              int (*actor)(struct net_device *, u32))
2114 {
2115         struct ethtool_value edata;
2116
2117         if (!actor)
2118                 return -EOPNOTSUPP;
2119
2120         if (copy_from_user(&edata, useraddr, sizeof(edata)))
2121                 return -EFAULT;
2122
2123         return actor(dev, edata.data);
2124 }
2125
2126 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2127                                                    char __user *useraddr)
2128 {
2129         struct ethtool_flash efl;
2130
2131         if (copy_from_user(&efl, useraddr, sizeof(efl)))
2132                 return -EFAULT;
2133
2134         if (!dev->ethtool_ops->flash_device)
2135                 return -EOPNOTSUPP;
2136
2137         efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2138
2139         return dev->ethtool_ops->flash_device(dev, &efl);
2140 }
2141
2142 static int ethtool_set_dump(struct net_device *dev,
2143                         void __user *useraddr)
2144 {
2145         struct ethtool_dump dump;
2146
2147         if (!dev->ethtool_ops->set_dump)
2148                 return -EOPNOTSUPP;
2149
2150         if (copy_from_user(&dump, useraddr, sizeof(dump)))
2151                 return -EFAULT;
2152
2153         return dev->ethtool_ops->set_dump(dev, &dump);
2154 }
2155
2156 static int ethtool_get_dump_flag(struct net_device *dev,
2157                                 void __user *useraddr)
2158 {
2159         int ret;
2160         struct ethtool_dump dump;
2161         const struct ethtool_ops *ops = dev->ethtool_ops;
2162
2163         if (!ops->get_dump_flag)
2164                 return -EOPNOTSUPP;
2165
2166         if (copy_from_user(&dump, useraddr, sizeof(dump)))
2167                 return -EFAULT;
2168
2169         ret = ops->get_dump_flag(dev, &dump);
2170         if (ret)
2171                 return ret;
2172
2173         if (copy_to_user(useraddr, &dump, sizeof(dump)))
2174                 return -EFAULT;
2175         return 0;
2176 }
2177
2178 static int ethtool_get_dump_data(struct net_device *dev,
2179                                 void __user *useraddr)
2180 {
2181         int ret;
2182         __u32 len;
2183         struct ethtool_dump dump, tmp;
2184         const struct ethtool_ops *ops = dev->ethtool_ops;
2185         void *data = NULL;
2186
2187         if (!ops->get_dump_data || !ops->get_dump_flag)
2188                 return -EOPNOTSUPP;
2189
2190         if (copy_from_user(&dump, useraddr, sizeof(dump)))
2191                 return -EFAULT;
2192
2193         memset(&tmp, 0, sizeof(tmp));
2194         tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2195         ret = ops->get_dump_flag(dev, &tmp);
2196         if (ret)
2197                 return ret;
2198
2199         len = min(tmp.len, dump.len);
2200         if (!len)
2201                 return -EFAULT;
2202
2203         /* Don't ever let the driver think there's more space available
2204          * than it requested with .get_dump_flag().
2205          */
2206         dump.len = len;
2207
2208         /* Always allocate enough space to hold the whole thing so that the
2209          * driver does not need to check the length and bother with partial
2210          * dumping.
2211          */
2212         data = vzalloc(tmp.len);
2213         if (!data)
2214                 return -ENOMEM;
2215         ret = ops->get_dump_data(dev, &dump, data);
2216         if (ret)
2217                 goto out;
2218
2219         /* There are two sane possibilities:
2220          * 1. The driver's .get_dump_data() does not touch dump.len.
2221          * 2. Or it may set dump.len to how much it really writes, which
2222          *    should be tmp.len (or len if it can do a partial dump).
2223          * In any case respond to userspace with the actual length of data
2224          * it's receiving.
2225          */
2226         WARN_ON(dump.len != len && dump.len != tmp.len);
2227         dump.len = len;
2228
2229         if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2230                 ret = -EFAULT;
2231                 goto out;
2232         }
2233         useraddr += offsetof(struct ethtool_dump, data);
2234         if (copy_to_user(useraddr, data, len))
2235                 ret = -EFAULT;
2236 out:
2237         vfree(data);
2238         return ret;
2239 }
2240
2241 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2242 {
2243         int err = 0;
2244         struct ethtool_ts_info info;
2245         const struct ethtool_ops *ops = dev->ethtool_ops;
2246         struct phy_device *phydev = dev->phydev;
2247
2248         memset(&info, 0, sizeof(info));
2249         info.cmd = ETHTOOL_GET_TS_INFO;
2250
2251         if (phydev && phydev->drv && phydev->drv->ts_info) {
2252                 err = phydev->drv->ts_info(phydev, &info);
2253         } else if (ops->get_ts_info) {
2254                 err = ops->get_ts_info(dev, &info);
2255         } else {
2256                 info.so_timestamping =
2257                         SOF_TIMESTAMPING_RX_SOFTWARE |
2258                         SOF_TIMESTAMPING_SOFTWARE;
2259                 info.phc_index = -1;
2260         }
2261
2262         if (err)
2263                 return err;
2264
2265         if (copy_to_user(useraddr, &info, sizeof(info)))
2266                 err = -EFAULT;
2267
2268         return err;
2269 }
2270
2271 static int __ethtool_get_module_info(struct net_device *dev,
2272                                      struct ethtool_modinfo *modinfo)
2273 {
2274         const struct ethtool_ops *ops = dev->ethtool_ops;
2275         struct phy_device *phydev = dev->phydev;
2276
2277         if (dev->sfp_bus)
2278                 return sfp_get_module_info(dev->sfp_bus, modinfo);
2279
2280         if (phydev && phydev->drv && phydev->drv->module_info)
2281                 return phydev->drv->module_info(phydev, modinfo);
2282
2283         if (ops->get_module_info)
2284                 return ops->get_module_info(dev, modinfo);
2285
2286         return -EOPNOTSUPP;
2287 }
2288
2289 static int ethtool_get_module_info(struct net_device *dev,
2290                                    void __user *useraddr)
2291 {
2292         int ret;
2293         struct ethtool_modinfo modinfo;
2294
2295         if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2296                 return -EFAULT;
2297
2298         ret = __ethtool_get_module_info(dev, &modinfo);
2299         if (ret)
2300                 return ret;
2301
2302         if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2303                 return -EFAULT;
2304
2305         return 0;
2306 }
2307
2308 static int __ethtool_get_module_eeprom(struct net_device *dev,
2309                                        struct ethtool_eeprom *ee, u8 *data)
2310 {
2311         const struct ethtool_ops *ops = dev->ethtool_ops;
2312         struct phy_device *phydev = dev->phydev;
2313
2314         if (dev->sfp_bus)
2315                 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2316
2317         if (phydev && phydev->drv && phydev->drv->module_eeprom)
2318                 return phydev->drv->module_eeprom(phydev, ee, data);
2319
2320         if (ops->get_module_eeprom)
2321                 return ops->get_module_eeprom(dev, ee, data);
2322
2323         return -EOPNOTSUPP;
2324 }
2325
2326 static int ethtool_get_module_eeprom(struct net_device *dev,
2327                                      void __user *useraddr)
2328 {
2329         int ret;
2330         struct ethtool_modinfo modinfo;
2331
2332         ret = __ethtool_get_module_info(dev, &modinfo);
2333         if (ret)
2334                 return ret;
2335
2336         return ethtool_get_any_eeprom(dev, useraddr,
2337                                       __ethtool_get_module_eeprom,
2338                                       modinfo.eeprom_len);
2339 }
2340
2341 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2342 {
2343         switch (tuna->id) {
2344         case ETHTOOL_RX_COPYBREAK:
2345         case ETHTOOL_TX_COPYBREAK:
2346                 if (tuna->len != sizeof(u32) ||
2347                     tuna->type_id != ETHTOOL_TUNABLE_U32)
2348                         return -EINVAL;
2349                 break;
2350         case ETHTOOL_PFC_PREVENTION_TOUT:
2351                 if (tuna->len != sizeof(u16) ||
2352                     tuna->type_id != ETHTOOL_TUNABLE_U16)
2353                         return -EINVAL;
2354                 break;
2355         default:
2356                 return -EINVAL;
2357         }
2358
2359         return 0;
2360 }
2361
2362 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2363 {
2364         int ret;
2365         struct ethtool_tunable tuna;
2366         const struct ethtool_ops *ops = dev->ethtool_ops;
2367         void *data;
2368
2369         if (!ops->get_tunable)
2370                 return -EOPNOTSUPP;
2371         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2372                 return -EFAULT;
2373         ret = ethtool_tunable_valid(&tuna);
2374         if (ret)
2375                 return ret;
2376         data = kzalloc(tuna.len, GFP_USER);
2377         if (!data)
2378                 return -ENOMEM;
2379         ret = ops->get_tunable(dev, &tuna, data);
2380         if (ret)
2381                 goto out;
2382         useraddr += sizeof(tuna);
2383         ret = -EFAULT;
2384         if (copy_to_user(useraddr, data, tuna.len))
2385                 goto out;
2386         ret = 0;
2387
2388 out:
2389         kfree(data);
2390         return ret;
2391 }
2392
2393 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2394 {
2395         int ret;
2396         struct ethtool_tunable tuna;
2397         const struct ethtool_ops *ops = dev->ethtool_ops;
2398         void *data;
2399
2400         if (!ops->set_tunable)
2401                 return -EOPNOTSUPP;
2402         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2403                 return -EFAULT;
2404         ret = ethtool_tunable_valid(&tuna);
2405         if (ret)
2406                 return ret;
2407         useraddr += sizeof(tuna);
2408         data = memdup_user(useraddr, tuna.len);
2409         if (IS_ERR(data))
2410                 return PTR_ERR(data);
2411         ret = ops->set_tunable(dev, &tuna, data);
2412
2413         kfree(data);
2414         return ret;
2415 }
2416
2417 static noinline_for_stack int
2418 ethtool_get_per_queue_coalesce(struct net_device *dev,
2419                                void __user *useraddr,
2420                                struct ethtool_per_queue_op *per_queue_opt)
2421 {
2422         u32 bit;
2423         int ret;
2424         DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2425
2426         if (!dev->ethtool_ops->get_per_queue_coalesce)
2427                 return -EOPNOTSUPP;
2428
2429         useraddr += sizeof(*per_queue_opt);
2430
2431         bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2432                           MAX_NUM_QUEUE);
2433
2434         for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2435                 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2436
2437                 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2438                 if (ret != 0)
2439                         return ret;
2440                 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2441                         return -EFAULT;
2442                 useraddr += sizeof(coalesce);
2443         }
2444
2445         return 0;
2446 }
2447
2448 static noinline_for_stack int
2449 ethtool_set_per_queue_coalesce(struct net_device *dev,
2450                                void __user *useraddr,
2451                                struct ethtool_per_queue_op *per_queue_opt)
2452 {
2453         u32 bit;
2454         int i, ret = 0;
2455         int n_queue;
2456         struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2457         DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2458
2459         if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2460             (!dev->ethtool_ops->get_per_queue_coalesce))
2461                 return -EOPNOTSUPP;
2462
2463         useraddr += sizeof(*per_queue_opt);
2464
2465         bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2466         n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2467         tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2468         if (!backup)
2469                 return -ENOMEM;
2470
2471         for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2472                 struct ethtool_coalesce coalesce;
2473
2474                 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2475                 if (ret != 0)
2476                         goto roll_back;
2477
2478                 tmp++;
2479
2480                 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2481                         ret = -EFAULT;
2482                         goto roll_back;
2483                 }
2484
2485                 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2486                 if (ret != 0)
2487                         goto roll_back;
2488
2489                 useraddr += sizeof(coalesce);
2490         }
2491
2492 roll_back:
2493         if (ret != 0) {
2494                 tmp = backup;
2495                 for_each_set_bit(i, queue_mask, bit) {
2496                         dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2497                         tmp++;
2498                 }
2499         }
2500         kfree(backup);
2501
2502         return ret;
2503 }
2504
2505 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2506                                  void __user *useraddr, u32 sub_cmd)
2507 {
2508         struct ethtool_per_queue_op per_queue_opt;
2509
2510         if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2511                 return -EFAULT;
2512
2513         if (per_queue_opt.sub_command != sub_cmd)
2514                 return -EINVAL;
2515
2516         switch (per_queue_opt.sub_command) {
2517         case ETHTOOL_GCOALESCE:
2518                 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2519         case ETHTOOL_SCOALESCE:
2520                 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2521         default:
2522                 return -EOPNOTSUPP;
2523         };
2524 }
2525
2526 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2527 {
2528         switch (tuna->id) {
2529         case ETHTOOL_PHY_DOWNSHIFT:
2530                 if (tuna->len != sizeof(u8) ||
2531                     tuna->type_id != ETHTOOL_TUNABLE_U8)
2532                         return -EINVAL;
2533                 break;
2534         default:
2535                 return -EINVAL;
2536         }
2537
2538         return 0;
2539 }
2540
2541 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2542 {
2543         int ret;
2544         struct ethtool_tunable tuna;
2545         struct phy_device *phydev = dev->phydev;
2546         void *data;
2547
2548         if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2549                 return -EOPNOTSUPP;
2550
2551         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2552                 return -EFAULT;
2553         ret = ethtool_phy_tunable_valid(&tuna);
2554         if (ret)
2555                 return ret;
2556         data = kzalloc(tuna.len, GFP_USER);
2557         if (!data)
2558                 return -ENOMEM;
2559         mutex_lock(&phydev->lock);
2560         ret = phydev->drv->get_tunable(phydev, &tuna, data);
2561         mutex_unlock(&phydev->lock);
2562         if (ret)
2563                 goto out;
2564         useraddr += sizeof(tuna);
2565         ret = -EFAULT;
2566         if (copy_to_user(useraddr, data, tuna.len))
2567                 goto out;
2568         ret = 0;
2569
2570 out:
2571         kfree(data);
2572         return ret;
2573 }
2574
2575 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2576 {
2577         int ret;
2578         struct ethtool_tunable tuna;
2579         struct phy_device *phydev = dev->phydev;
2580         void *data;
2581
2582         if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2583                 return -EOPNOTSUPP;
2584         if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2585                 return -EFAULT;
2586         ret = ethtool_phy_tunable_valid(&tuna);
2587         if (ret)
2588                 return ret;
2589         useraddr += sizeof(tuna);
2590         data = memdup_user(useraddr, tuna.len);
2591         if (IS_ERR(data))
2592                 return PTR_ERR(data);
2593         mutex_lock(&phydev->lock);
2594         ret = phydev->drv->set_tunable(phydev, &tuna, data);
2595         mutex_unlock(&phydev->lock);
2596
2597         kfree(data);
2598         return ret;
2599 }
2600
2601 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2602 {
2603         struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2604         int rc;
2605
2606         if (!dev->ethtool_ops->get_fecparam)
2607                 return -EOPNOTSUPP;
2608
2609         rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2610         if (rc)
2611                 return rc;
2612
2613         if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2614                 return -EFAULT;
2615         return 0;
2616 }
2617
2618 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2619 {
2620         struct ethtool_fecparam fecparam;
2621
2622         if (!dev->ethtool_ops->set_fecparam)
2623                 return -EOPNOTSUPP;
2624
2625         if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2626                 return -EFAULT;
2627
2628         return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2629 }
2630
2631 /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
2632
2633 int dev_ethtool(struct net *net, struct ifreq *ifr)
2634 {
2635         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2636         void __user *useraddr = ifr->ifr_data;
2637         u32 ethcmd, sub_cmd;
2638         int rc;
2639         netdev_features_t old_features;
2640
2641         if (!dev || !netif_device_present(dev))
2642                 return -ENODEV;
2643
2644         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
2645                 return -EFAULT;
2646
2647         if (ethcmd == ETHTOOL_PERQUEUE) {
2648                 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2649                         return -EFAULT;
2650         } else {
2651                 sub_cmd = ethcmd;
2652         }
2653         /* Allow some commands to be done by anyone */
2654         switch (sub_cmd) {
2655         case ETHTOOL_GSET:
2656         case ETHTOOL_GDRVINFO:
2657         case ETHTOOL_GMSGLVL:
2658         case ETHTOOL_GLINK:
2659         case ETHTOOL_GCOALESCE:
2660         case ETHTOOL_GRINGPARAM:
2661         case ETHTOOL_GPAUSEPARAM:
2662         case ETHTOOL_GRXCSUM:
2663         case ETHTOOL_GTXCSUM:
2664         case ETHTOOL_GSG:
2665         case ETHTOOL_GSSET_INFO:
2666         case ETHTOOL_GSTRINGS:
2667         case ETHTOOL_GSTATS:
2668         case ETHTOOL_GPHYSTATS:
2669         case ETHTOOL_GTSO:
2670         case ETHTOOL_GPERMADDR:
2671         case ETHTOOL_GUFO:
2672         case ETHTOOL_GGSO:
2673         case ETHTOOL_GGRO:
2674         case ETHTOOL_GFLAGS:
2675         case ETHTOOL_GPFLAGS:
2676         case ETHTOOL_GRXFH:
2677         case ETHTOOL_GRXRINGS:
2678         case ETHTOOL_GRXCLSRLCNT:
2679         case ETHTOOL_GRXCLSRULE:
2680         case ETHTOOL_GRXCLSRLALL:
2681         case ETHTOOL_GRXFHINDIR:
2682         case ETHTOOL_GRSSH:
2683         case ETHTOOL_GFEATURES:
2684         case ETHTOOL_GCHANNELS:
2685         case ETHTOOL_GET_TS_INFO:
2686         case ETHTOOL_GEEE:
2687         case ETHTOOL_GTUNABLE:
2688         case ETHTOOL_PHY_GTUNABLE:
2689         case ETHTOOL_GLINKSETTINGS:
2690         case ETHTOOL_GFECPARAM:
2691                 break;
2692         default:
2693                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2694                         return -EPERM;
2695         }
2696
2697         if (dev->ethtool_ops->begin) {
2698                 rc = dev->ethtool_ops->begin(dev);
2699                 if (rc  < 0)
2700                         return rc;
2701         }
2702         old_features = dev->features;
2703
2704         switch (ethcmd) {
2705         case ETHTOOL_GSET:
2706                 rc = ethtool_get_settings(dev, useraddr);
2707                 break;
2708         case ETHTOOL_SSET:
2709                 rc = ethtool_set_settings(dev, useraddr);
2710                 break;
2711         case ETHTOOL_GDRVINFO:
2712                 rc = ethtool_get_drvinfo(dev, useraddr);
2713                 break;
2714         case ETHTOOL_GREGS:
2715                 rc = ethtool_get_regs(dev, useraddr);
2716                 break;
2717         case ETHTOOL_GWOL:
2718                 rc = ethtool_get_wol(dev, useraddr);
2719                 break;
2720         case ETHTOOL_SWOL:
2721                 rc = ethtool_set_wol(dev, useraddr);
2722                 break;
2723         case ETHTOOL_GMSGLVL:
2724                 rc = ethtool_get_value(dev, useraddr, ethcmd,
2725                                        dev->ethtool_ops->get_msglevel);
2726                 break;
2727         case ETHTOOL_SMSGLVL:
2728                 rc = ethtool_set_value_void(dev, useraddr,
2729                                        dev->ethtool_ops->set_msglevel);
2730                 break;
2731         case ETHTOOL_GEEE:
2732                 rc = ethtool_get_eee(dev, useraddr);
2733                 break;
2734         case ETHTOOL_SEEE:
2735                 rc = ethtool_set_eee(dev, useraddr);
2736                 break;
2737         case ETHTOOL_NWAY_RST:
2738                 rc = ethtool_nway_reset(dev);
2739                 break;
2740         case ETHTOOL_GLINK:
2741                 rc = ethtool_get_link(dev, useraddr);
2742                 break;
2743         case ETHTOOL_GEEPROM:
2744                 rc = ethtool_get_eeprom(dev, useraddr);
2745                 break;
2746         case ETHTOOL_SEEPROM:
2747                 rc = ethtool_set_eeprom(dev, useraddr);
2748                 break;
2749         case ETHTOOL_GCOALESCE:
2750                 rc = ethtool_get_coalesce(dev, useraddr);
2751                 break;
2752         case ETHTOOL_SCOALESCE:
2753                 rc = ethtool_set_coalesce(dev, useraddr);
2754                 break;
2755         case ETHTOOL_GRINGPARAM:
2756                 rc = ethtool_get_ringparam(dev, useraddr);
2757                 break;
2758         case ETHTOOL_SRINGPARAM:
2759                 rc = ethtool_set_ringparam(dev, useraddr);
2760                 break;
2761         case ETHTOOL_GPAUSEPARAM:
2762                 rc = ethtool_get_pauseparam(dev, useraddr);
2763                 break;
2764         case ETHTOOL_SPAUSEPARAM:
2765                 rc = ethtool_set_pauseparam(dev, useraddr);
2766                 break;
2767         case ETHTOOL_TEST:
2768                 rc = ethtool_self_test(dev, useraddr);
2769                 break;
2770         case ETHTOOL_GSTRINGS:
2771                 rc = ethtool_get_strings(dev, useraddr);
2772                 break;
2773         case ETHTOOL_PHYS_ID:
2774                 rc = ethtool_phys_id(dev, useraddr);
2775                 break;
2776         case ETHTOOL_GSTATS:
2777                 rc = ethtool_get_stats(dev, useraddr);
2778                 break;
2779         case ETHTOOL_GPERMADDR:
2780                 rc = ethtool_get_perm_addr(dev, useraddr);
2781                 break;
2782         case ETHTOOL_GFLAGS:
2783                 rc = ethtool_get_value(dev, useraddr, ethcmd,
2784                                         __ethtool_get_flags);
2785                 break;
2786         case ETHTOOL_SFLAGS:
2787                 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2788                 break;
2789         case ETHTOOL_GPFLAGS:
2790                 rc = ethtool_get_value(dev, useraddr, ethcmd,
2791                                        dev->ethtool_ops->get_priv_flags);
2792                 break;
2793         case ETHTOOL_SPFLAGS:
2794                 rc = ethtool_set_value(dev, useraddr,
2795                                        dev->ethtool_ops->set_priv_flags);
2796                 break;
2797         case ETHTOOL_GRXFH:
2798         case ETHTOOL_GRXRINGS:
2799         case ETHTOOL_GRXCLSRLCNT:
2800         case ETHTOOL_GRXCLSRULE:
2801         case ETHTOOL_GRXCLSRLALL:
2802                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2803                 break;
2804         case ETHTOOL_SRXFH:
2805         case ETHTOOL_SRXCLSRLDEL:
2806         case ETHTOOL_SRXCLSRLINS:
2807                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2808                 break;
2809         case ETHTOOL_FLASHDEV:
2810                 rc = ethtool_flash_device(dev, useraddr);
2811                 break;
2812         case ETHTOOL_RESET:
2813                 rc = ethtool_reset(dev, useraddr);
2814                 break;
2815         case ETHTOOL_GSSET_INFO:
2816                 rc = ethtool_get_sset_info(dev, useraddr);
2817                 break;
2818         case ETHTOOL_GRXFHINDIR:
2819                 rc = ethtool_get_rxfh_indir(dev, useraddr);
2820                 break;
2821         case ETHTOOL_SRXFHINDIR:
2822                 rc = ethtool_set_rxfh_indir(dev, useraddr);
2823                 break;
2824         case ETHTOOL_GRSSH:
2825                 rc = ethtool_get_rxfh(dev, useraddr);
2826                 break;
2827         case ETHTOOL_SRSSH:
2828                 rc = ethtool_set_rxfh(dev, useraddr);
2829                 break;
2830         case ETHTOOL_GFEATURES:
2831                 rc = ethtool_get_features(dev, useraddr);
2832                 break;
2833         case ETHTOOL_SFEATURES:
2834                 rc = ethtool_set_features(dev, useraddr);
2835                 break;
2836         case ETHTOOL_GTXCSUM:
2837         case ETHTOOL_GRXCSUM:
2838         case ETHTOOL_GSG:
2839         case ETHTOOL_GTSO:
2840         case ETHTOOL_GGSO:
2841         case ETHTOOL_GGRO:
2842                 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2843                 break;
2844         case ETHTOOL_STXCSUM:
2845         case ETHTOOL_SRXCSUM:
2846         case ETHTOOL_SSG:
2847         case ETHTOOL_STSO:
2848         case ETHTOOL_SGSO:
2849         case ETHTOOL_SGRO:
2850                 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2851                 break;
2852         case ETHTOOL_GCHANNELS:
2853                 rc = ethtool_get_channels(dev, useraddr);
2854                 break;
2855         case ETHTOOL_SCHANNELS:
2856                 rc = ethtool_set_channels(dev, useraddr);
2857                 break;
2858         case ETHTOOL_SET_DUMP:
2859                 rc = ethtool_set_dump(dev, useraddr);
2860                 break;
2861         case ETHTOOL_GET_DUMP_FLAG:
2862                 rc = ethtool_get_dump_flag(dev, useraddr);
2863                 break;
2864         case ETHTOOL_GET_DUMP_DATA:
2865                 rc = ethtool_get_dump_data(dev, useraddr);
2866                 break;
2867         case ETHTOOL_GET_TS_INFO:
2868                 rc = ethtool_get_ts_info(dev, useraddr);
2869                 break;
2870         case ETHTOOL_GMODULEINFO:
2871                 rc = ethtool_get_module_info(dev, useraddr);
2872                 break;
2873         case ETHTOOL_GMODULEEEPROM:
2874                 rc = ethtool_get_module_eeprom(dev, useraddr);
2875                 break;
2876         case ETHTOOL_GTUNABLE:
2877                 rc = ethtool_get_tunable(dev, useraddr);
2878                 break;
2879         case ETHTOOL_STUNABLE:
2880                 rc = ethtool_set_tunable(dev, useraddr);
2881                 break;
2882         case ETHTOOL_GPHYSTATS:
2883                 rc = ethtool_get_phy_stats(dev, useraddr);
2884                 break;
2885         case ETHTOOL_PERQUEUE:
2886                 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2887                 break;
2888         case ETHTOOL_GLINKSETTINGS:
2889                 rc = ethtool_get_link_ksettings(dev, useraddr);
2890                 break;
2891         case ETHTOOL_SLINKSETTINGS:
2892                 rc = ethtool_set_link_ksettings(dev, useraddr);
2893                 break;
2894         case ETHTOOL_PHY_GTUNABLE:
2895                 rc = get_phy_tunable(dev, useraddr);
2896                 break;
2897         case ETHTOOL_PHY_STUNABLE:
2898                 rc = set_phy_tunable(dev, useraddr);
2899                 break;
2900         case ETHTOOL_GFECPARAM:
2901                 rc = ethtool_get_fecparam(dev, useraddr);
2902                 break;
2903         case ETHTOOL_SFECPARAM:
2904                 rc = ethtool_set_fecparam(dev, useraddr);
2905                 break;
2906         default:
2907                 rc = -EOPNOTSUPP;
2908         }
2909
2910         if (dev->ethtool_ops->complete)
2911                 dev->ethtool_ops->complete(dev);
2912
2913         if (old_features != dev->features)
2914                 netdev_features_change(dev);
2915
2916         return rc;
2917 }