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