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