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