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