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