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