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