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