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