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