Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
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
40u32 ethtool_op_get_link(struct net_device *dev)
41{
42 return netif_carrier_ok(dev) ? 1 : 0;
43}
44EXPORT_SYMBOL(ethtool_op_get_link);
45
46int 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}
55EXPORT_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
61static 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
120static const char
121rss_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
127static const char
128tunable_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
135static const char
136phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
137 [ETHTOOL_ID_UNSPEC] = "Unspec",
138 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
139};
140
141static 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
179static 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
219static 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
245static 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
268static 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
296static 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
310static 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
340static 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
358static 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. */
390void 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}
403EXPORT_SYMBOL(ethtool_intersect_link_masks);
404
405void 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}
411EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
412
413/* return false if src had higher bits set. lower bits always updated. */
414bool 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}
435EXPORT_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 */
440static bool
441convert_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 */
488static bool
489convert_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 */
536struct 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. */
546int __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}
557EXPORT_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 */
562static 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 */
588static int
589store_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. */
612static 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. */
666static 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 */
710static 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 */
743static 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
762static 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
817static 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
868out:
869 kfree(info_buf);
870 return ret;
871}
872
873static 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
905static 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
971err_out:
972 kfree(rule_buf);
973
974 return ret;
975}
976
977static 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
994u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
995
996void 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}
1002EXPORT_SYMBOL(netdev_rss_key_fill);
1003
1004static 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
1030out:
1031 kfree(indir);
1032 return ret;
1033}
1034
1035static 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
1078out:
1079 kfree(indir);
1080 return ret;
1081}
1082
1083static 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
1140out:
1141 kfree(indir);
1142 return ret;
1143}
1144
1145static 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 }
1218out:
1219 kfree(rss_config);
1220
1221 return ret;
1222}
1223
1224static 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
1332out:
1333 kfree(rss_config);
1334 return ret;
1335}
1336
1337static 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(®s, 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, ®s, regbuf);
1362
1363 ret = -EFAULT;
1364 if (copy_to_user(useraddr, ®s, 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
1376static 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
1396static 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
1410static 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
1430static 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
1451static 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
1464static 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
1472static 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
1486static 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
1537static 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
1549static 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
1597static 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
1612static 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
1626static 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
1640static 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
1662static 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
1677static 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
1718static 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
1732static 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
1745static 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
1783static 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 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1801 if (gstrings.len && !data)
1802 return -ENOMEM;
1803
1804 __ethtool_get_strings(dev, gstrings.string_set, data);
1805
1806 ret = -EFAULT;
1807 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1808 goto out;
1809 useraddr += sizeof(gstrings);
1810 if (gstrings.len &&
1811 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1812 goto out;
1813 ret = 0;
1814
1815out:
1816 vfree(data);
1817 return ret;
1818}
1819
1820static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1821{
1822 struct ethtool_value id;
1823 static bool busy;
1824 const struct ethtool_ops *ops = dev->ethtool_ops;
1825 int rc;
1826
1827 if (!ops->set_phys_id)
1828 return -EOPNOTSUPP;
1829
1830 if (busy)
1831 return -EBUSY;
1832
1833 if (copy_from_user(&id, useraddr, sizeof(id)))
1834 return -EFAULT;
1835
1836 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1837 if (rc < 0)
1838 return rc;
1839
1840 /* Drop the RTNL lock while waiting, but prevent reentry or
1841 * removal of the device.
1842 */
1843 busy = true;
1844 dev_hold(dev);
1845 rtnl_unlock();
1846
1847 if (rc == 0) {
1848 /* Driver will handle this itself */
1849 schedule_timeout_interruptible(
1850 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1851 } else {
1852 /* Driver expects to be called at twice the frequency in rc */
1853 int n = rc * 2, i, interval = HZ / n;
1854
1855 /* Count down seconds */
1856 do {
1857 /* Count down iterations per second */
1858 i = n;
1859 do {
1860 rtnl_lock();
1861 rc = ops->set_phys_id(dev,
1862 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1863 rtnl_unlock();
1864 if (rc)
1865 break;
1866 schedule_timeout_interruptible(interval);
1867 } while (!signal_pending(current) && --i != 0);
1868 } while (!signal_pending(current) &&
1869 (id.data == 0 || --id.data != 0));
1870 }
1871
1872 rtnl_lock();
1873 dev_put(dev);
1874 busy = false;
1875
1876 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1877 return rc;
1878}
1879
1880static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1881{
1882 struct ethtool_stats stats;
1883 const struct ethtool_ops *ops = dev->ethtool_ops;
1884 u64 *data;
1885 int ret, n_stats;
1886
1887 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1888 return -EOPNOTSUPP;
1889
1890 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1891 if (n_stats < 0)
1892 return n_stats;
1893 if (n_stats > S32_MAX / sizeof(u64))
1894 return -ENOMEM;
1895 WARN_ON_ONCE(!n_stats);
1896 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1897 return -EFAULT;
1898
1899 stats.n_stats = n_stats;
1900 data = vzalloc(array_size(n_stats, sizeof(u64)));
1901 if (n_stats && !data)
1902 return -ENOMEM;
1903
1904 ops->get_ethtool_stats(dev, &stats, data);
1905
1906 ret = -EFAULT;
1907 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1908 goto out;
1909 useraddr += sizeof(stats);
1910 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1911 goto out;
1912 ret = 0;
1913
1914 out:
1915 vfree(data);
1916 return ret;
1917}
1918
1919static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1920{
1921 const struct ethtool_ops *ops = dev->ethtool_ops;
1922 struct phy_device *phydev = dev->phydev;
1923 struct ethtool_stats stats;
1924 u64 *data;
1925 int ret, n_stats;
1926
1927 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1928 return -EOPNOTSUPP;
1929
1930 if (dev->phydev && !ops->get_ethtool_phy_stats)
1931 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1932 else
1933 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1934 if (n_stats < 0)
1935 return n_stats;
1936 if (n_stats > S32_MAX / sizeof(u64))
1937 return -ENOMEM;
1938 WARN_ON_ONCE(!n_stats);
1939
1940 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1941 return -EFAULT;
1942
1943 stats.n_stats = n_stats;
1944 data = vzalloc(array_size(n_stats, sizeof(u64)));
1945 if (n_stats && !data)
1946 return -ENOMEM;
1947
1948 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1949 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
1950 if (ret < 0)
1951 return ret;
1952 } else {
1953 ops->get_ethtool_phy_stats(dev, &stats, data);
1954 }
1955
1956 ret = -EFAULT;
1957 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1958 goto out;
1959 useraddr += sizeof(stats);
1960 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1961 goto out;
1962 ret = 0;
1963
1964 out:
1965 vfree(data);
1966 return ret;
1967}
1968
1969static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1970{
1971 struct ethtool_perm_addr epaddr;
1972
1973 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1974 return -EFAULT;
1975
1976 if (epaddr.size < dev->addr_len)
1977 return -ETOOSMALL;
1978 epaddr.size = dev->addr_len;
1979
1980 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1981 return -EFAULT;
1982 useraddr += sizeof(epaddr);
1983 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1984 return -EFAULT;
1985 return 0;
1986}
1987
1988static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1989 u32 cmd, u32 (*actor)(struct net_device *))
1990{
1991 struct ethtool_value edata = { .cmd = cmd };
1992
1993 if (!actor)
1994 return -EOPNOTSUPP;
1995
1996 edata.data = actor(dev);
1997
1998 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1999 return -EFAULT;
2000 return 0;
2001}
2002
2003static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2004 void (*actor)(struct net_device *, u32))
2005{
2006 struct ethtool_value edata;
2007
2008 if (!actor)
2009 return -EOPNOTSUPP;
2010
2011 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2012 return -EFAULT;
2013
2014 actor(dev, edata.data);
2015 return 0;
2016}
2017
2018static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2019 int (*actor)(struct net_device *, u32))
2020{
2021 struct ethtool_value edata;
2022
2023 if (!actor)
2024 return -EOPNOTSUPP;
2025
2026 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2027 return -EFAULT;
2028
2029 return actor(dev, edata.data);
2030}
2031
2032static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2033 char __user *useraddr)
2034{
2035 struct ethtool_flash efl;
2036
2037 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2038 return -EFAULT;
2039 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2040
2041 if (!dev->ethtool_ops->flash_device)
2042 return devlink_compat_flash_update(dev, efl.data);
2043
2044 return dev->ethtool_ops->flash_device(dev, &efl);
2045}
2046
2047static int ethtool_set_dump(struct net_device *dev,
2048 void __user *useraddr)
2049{
2050 struct ethtool_dump dump;
2051
2052 if (!dev->ethtool_ops->set_dump)
2053 return -EOPNOTSUPP;
2054
2055 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2056 return -EFAULT;
2057
2058 return dev->ethtool_ops->set_dump(dev, &dump);
2059}
2060
2061static int ethtool_get_dump_flag(struct net_device *dev,
2062 void __user *useraddr)
2063{
2064 int ret;
2065 struct ethtool_dump dump;
2066 const struct ethtool_ops *ops = dev->ethtool_ops;
2067
2068 if (!ops->get_dump_flag)
2069 return -EOPNOTSUPP;
2070
2071 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2072 return -EFAULT;
2073
2074 ret = ops->get_dump_flag(dev, &dump);
2075 if (ret)
2076 return ret;
2077
2078 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2079 return -EFAULT;
2080 return 0;
2081}
2082
2083static int ethtool_get_dump_data(struct net_device *dev,
2084 void __user *useraddr)
2085{
2086 int ret;
2087 __u32 len;
2088 struct ethtool_dump dump, tmp;
2089 const struct ethtool_ops *ops = dev->ethtool_ops;
2090 void *data = NULL;
2091
2092 if (!ops->get_dump_data || !ops->get_dump_flag)
2093 return -EOPNOTSUPP;
2094
2095 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2096 return -EFAULT;
2097
2098 memset(&tmp, 0, sizeof(tmp));
2099 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2100 ret = ops->get_dump_flag(dev, &tmp);
2101 if (ret)
2102 return ret;
2103
2104 len = min(tmp.len, dump.len);
2105 if (!len)
2106 return -EFAULT;
2107
2108 /* Don't ever let the driver think there's more space available
2109 * than it requested with .get_dump_flag().
2110 */
2111 dump.len = len;
2112
2113 /* Always allocate enough space to hold the whole thing so that the
2114 * driver does not need to check the length and bother with partial
2115 * dumping.
2116 */
2117 data = vzalloc(tmp.len);
2118 if (!data)
2119 return -ENOMEM;
2120 ret = ops->get_dump_data(dev, &dump, data);
2121 if (ret)
2122 goto out;
2123
2124 /* There are two sane possibilities:
2125 * 1. The driver's .get_dump_data() does not touch dump.len.
2126 * 2. Or it may set dump.len to how much it really writes, which
2127 * should be tmp.len (or len if it can do a partial dump).
2128 * In any case respond to userspace with the actual length of data
2129 * it's receiving.
2130 */
2131 WARN_ON(dump.len != len && dump.len != tmp.len);
2132 dump.len = len;
2133
2134 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2135 ret = -EFAULT;
2136 goto out;
2137 }
2138 useraddr += offsetof(struct ethtool_dump, data);
2139 if (copy_to_user(useraddr, data, len))
2140 ret = -EFAULT;
2141out:
2142 vfree(data);
2143 return ret;
2144}
2145
2146static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2147{
2148 int err = 0;
2149 struct ethtool_ts_info info;
2150 const struct ethtool_ops *ops = dev->ethtool_ops;
2151 struct phy_device *phydev = dev->phydev;
2152
2153 memset(&info, 0, sizeof(info));
2154 info.cmd = ETHTOOL_GET_TS_INFO;
2155
2156 if (phydev && phydev->drv && phydev->drv->ts_info) {
2157 err = phydev->drv->ts_info(phydev, &info);
2158 } else if (ops->get_ts_info) {
2159 err = ops->get_ts_info(dev, &info);
2160 } else {
2161 info.so_timestamping =
2162 SOF_TIMESTAMPING_RX_SOFTWARE |
2163 SOF_TIMESTAMPING_SOFTWARE;
2164 info.phc_index = -1;
2165 }
2166
2167 if (err)
2168 return err;
2169
2170 if (copy_to_user(useraddr, &info, sizeof(info)))
2171 err = -EFAULT;
2172
2173 return err;
2174}
2175
2176static int __ethtool_get_module_info(struct net_device *dev,
2177 struct ethtool_modinfo *modinfo)
2178{
2179 const struct ethtool_ops *ops = dev->ethtool_ops;
2180 struct phy_device *phydev = dev->phydev;
2181
2182 if (dev->sfp_bus)
2183 return sfp_get_module_info(dev->sfp_bus, modinfo);
2184
2185 if (phydev && phydev->drv && phydev->drv->module_info)
2186 return phydev->drv->module_info(phydev, modinfo);
2187
2188 if (ops->get_module_info)
2189 return ops->get_module_info(dev, modinfo);
2190
2191 return -EOPNOTSUPP;
2192}
2193
2194static int ethtool_get_module_info(struct net_device *dev,
2195 void __user *useraddr)
2196{
2197 int ret;
2198 struct ethtool_modinfo modinfo;
2199
2200 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2201 return -EFAULT;
2202
2203 ret = __ethtool_get_module_info(dev, &modinfo);
2204 if (ret)
2205 return ret;
2206
2207 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2208 return -EFAULT;
2209
2210 return 0;
2211}
2212
2213static int __ethtool_get_module_eeprom(struct net_device *dev,
2214 struct ethtool_eeprom *ee, u8 *data)
2215{
2216 const struct ethtool_ops *ops = dev->ethtool_ops;
2217 struct phy_device *phydev = dev->phydev;
2218
2219 if (dev->sfp_bus)
2220 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2221
2222 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2223 return phydev->drv->module_eeprom(phydev, ee, data);
2224
2225 if (ops->get_module_eeprom)
2226 return ops->get_module_eeprom(dev, ee, data);
2227
2228 return -EOPNOTSUPP;
2229}
2230
2231static int ethtool_get_module_eeprom(struct net_device *dev,
2232 void __user *useraddr)
2233{
2234 int ret;
2235 struct ethtool_modinfo modinfo;
2236
2237 ret = __ethtool_get_module_info(dev, &modinfo);
2238 if (ret)
2239 return ret;
2240
2241 return ethtool_get_any_eeprom(dev, useraddr,
2242 __ethtool_get_module_eeprom,
2243 modinfo.eeprom_len);
2244}
2245
2246static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2247{
2248 switch (tuna->id) {
2249 case ETHTOOL_RX_COPYBREAK:
2250 case ETHTOOL_TX_COPYBREAK:
2251 if (tuna->len != sizeof(u32) ||
2252 tuna->type_id != ETHTOOL_TUNABLE_U32)
2253 return -EINVAL;
2254 break;
2255 case ETHTOOL_PFC_PREVENTION_TOUT:
2256 if (tuna->len != sizeof(u16) ||
2257 tuna->type_id != ETHTOOL_TUNABLE_U16)
2258 return -EINVAL;
2259 break;
2260 default:
2261 return -EINVAL;
2262 }
2263
2264 return 0;
2265}
2266
2267static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2268{
2269 int ret;
2270 struct ethtool_tunable tuna;
2271 const struct ethtool_ops *ops = dev->ethtool_ops;
2272 void *data;
2273
2274 if (!ops->get_tunable)
2275 return -EOPNOTSUPP;
2276 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2277 return -EFAULT;
2278 ret = ethtool_tunable_valid(&tuna);
2279 if (ret)
2280 return ret;
2281 data = kmalloc(tuna.len, GFP_USER);
2282 if (!data)
2283 return -ENOMEM;
2284 ret = ops->get_tunable(dev, &tuna, data);
2285 if (ret)
2286 goto out;
2287 useraddr += sizeof(tuna);
2288 ret = -EFAULT;
2289 if (copy_to_user(useraddr, data, tuna.len))
2290 goto out;
2291 ret = 0;
2292
2293out:
2294 kfree(data);
2295 return ret;
2296}
2297
2298static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2299{
2300 int ret;
2301 struct ethtool_tunable tuna;
2302 const struct ethtool_ops *ops = dev->ethtool_ops;
2303 void *data;
2304
2305 if (!ops->set_tunable)
2306 return -EOPNOTSUPP;
2307 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2308 return -EFAULT;
2309 ret = ethtool_tunable_valid(&tuna);
2310 if (ret)
2311 return ret;
2312 useraddr += sizeof(tuna);
2313 data = memdup_user(useraddr, tuna.len);
2314 if (IS_ERR(data))
2315 return PTR_ERR(data);
2316 ret = ops->set_tunable(dev, &tuna, data);
2317
2318 kfree(data);
2319 return ret;
2320}
2321
2322static noinline_for_stack int
2323ethtool_get_per_queue_coalesce(struct net_device *dev,
2324 void __user *useraddr,
2325 struct ethtool_per_queue_op *per_queue_opt)
2326{
2327 u32 bit;
2328 int ret;
2329 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2330
2331 if (!dev->ethtool_ops->get_per_queue_coalesce)
2332 return -EOPNOTSUPP;
2333
2334 useraddr += sizeof(*per_queue_opt);
2335
2336 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2337 MAX_NUM_QUEUE);
2338
2339 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2340 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2341
2342 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2343 if (ret != 0)
2344 return ret;
2345 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2346 return -EFAULT;
2347 useraddr += sizeof(coalesce);
2348 }
2349
2350 return 0;
2351}
2352
2353static noinline_for_stack int
2354ethtool_set_per_queue_coalesce(struct net_device *dev,
2355 void __user *useraddr,
2356 struct ethtool_per_queue_op *per_queue_opt)
2357{
2358 u32 bit;
2359 int i, ret = 0;
2360 int n_queue;
2361 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2362 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2363
2364 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2365 (!dev->ethtool_ops->get_per_queue_coalesce))
2366 return -EOPNOTSUPP;
2367
2368 useraddr += sizeof(*per_queue_opt);
2369
2370 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2371 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2372 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2373 if (!backup)
2374 return -ENOMEM;
2375
2376 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2377 struct ethtool_coalesce coalesce;
2378
2379 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2380 if (ret != 0)
2381 goto roll_back;
2382
2383 tmp++;
2384
2385 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2386 ret = -EFAULT;
2387 goto roll_back;
2388 }
2389
2390 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2391 if (ret != 0)
2392 goto roll_back;
2393
2394 useraddr += sizeof(coalesce);
2395 }
2396
2397roll_back:
2398 if (ret != 0) {
2399 tmp = backup;
2400 for_each_set_bit(i, queue_mask, bit) {
2401 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2402 tmp++;
2403 }
2404 }
2405 kfree(backup);
2406
2407 return ret;
2408}
2409
2410static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2411 void __user *useraddr, u32 sub_cmd)
2412{
2413 struct ethtool_per_queue_op per_queue_opt;
2414
2415 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2416 return -EFAULT;
2417
2418 if (per_queue_opt.sub_command != sub_cmd)
2419 return -EINVAL;
2420
2421 switch (per_queue_opt.sub_command) {
2422 case ETHTOOL_GCOALESCE:
2423 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2424 case ETHTOOL_SCOALESCE:
2425 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2426 default:
2427 return -EOPNOTSUPP;
2428 };
2429}
2430
2431static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2432{
2433 switch (tuna->id) {
2434 case ETHTOOL_PHY_DOWNSHIFT:
2435 if (tuna->len != sizeof(u8) ||
2436 tuna->type_id != ETHTOOL_TUNABLE_U8)
2437 return -EINVAL;
2438 break;
2439 default:
2440 return -EINVAL;
2441 }
2442
2443 return 0;
2444}
2445
2446static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2447{
2448 int ret;
2449 struct ethtool_tunable tuna;
2450 struct phy_device *phydev = dev->phydev;
2451 void *data;
2452
2453 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2454 return -EOPNOTSUPP;
2455
2456 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2457 return -EFAULT;
2458 ret = ethtool_phy_tunable_valid(&tuna);
2459 if (ret)
2460 return ret;
2461 data = kmalloc(tuna.len, GFP_USER);
2462 if (!data)
2463 return -ENOMEM;
2464 mutex_lock(&phydev->lock);
2465 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2466 mutex_unlock(&phydev->lock);
2467 if (ret)
2468 goto out;
2469 useraddr += sizeof(tuna);
2470 ret = -EFAULT;
2471 if (copy_to_user(useraddr, data, tuna.len))
2472 goto out;
2473 ret = 0;
2474
2475out:
2476 kfree(data);
2477 return ret;
2478}
2479
2480static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2481{
2482 int ret;
2483 struct ethtool_tunable tuna;
2484 struct phy_device *phydev = dev->phydev;
2485 void *data;
2486
2487 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2488 return -EOPNOTSUPP;
2489 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2490 return -EFAULT;
2491 ret = ethtool_phy_tunable_valid(&tuna);
2492 if (ret)
2493 return ret;
2494 useraddr += sizeof(tuna);
2495 data = memdup_user(useraddr, tuna.len);
2496 if (IS_ERR(data))
2497 return PTR_ERR(data);
2498 mutex_lock(&phydev->lock);
2499 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2500 mutex_unlock(&phydev->lock);
2501
2502 kfree(data);
2503 return ret;
2504}
2505
2506static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2507{
2508 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2509 int rc;
2510
2511 if (!dev->ethtool_ops->get_fecparam)
2512 return -EOPNOTSUPP;
2513
2514 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2515 if (rc)
2516 return rc;
2517
2518 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2519 return -EFAULT;
2520 return 0;
2521}
2522
2523static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2524{
2525 struct ethtool_fecparam fecparam;
2526
2527 if (!dev->ethtool_ops->set_fecparam)
2528 return -EOPNOTSUPP;
2529
2530 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2531 return -EFAULT;
2532
2533 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2534}
2535
2536/* The main entry point in this file. Called from net/core/dev_ioctl.c */
2537
2538int dev_ethtool(struct net *net, struct ifreq *ifr)
2539{
2540 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2541 void __user *useraddr = ifr->ifr_data;
2542 u32 ethcmd, sub_cmd;
2543 int rc;
2544 netdev_features_t old_features;
2545
2546 if (!dev || !netif_device_present(dev))
2547 return -ENODEV;
2548
2549 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2550 return -EFAULT;
2551
2552 if (ethcmd == ETHTOOL_PERQUEUE) {
2553 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2554 return -EFAULT;
2555 } else {
2556 sub_cmd = ethcmd;
2557 }
2558 /* Allow some commands to be done by anyone */
2559 switch (sub_cmd) {
2560 case ETHTOOL_GSET:
2561 case ETHTOOL_GDRVINFO:
2562 case ETHTOOL_GMSGLVL:
2563 case ETHTOOL_GLINK:
2564 case ETHTOOL_GCOALESCE:
2565 case ETHTOOL_GRINGPARAM:
2566 case ETHTOOL_GPAUSEPARAM:
2567 case ETHTOOL_GRXCSUM:
2568 case ETHTOOL_GTXCSUM:
2569 case ETHTOOL_GSG:
2570 case ETHTOOL_GSSET_INFO:
2571 case ETHTOOL_GSTRINGS:
2572 case ETHTOOL_GSTATS:
2573 case ETHTOOL_GPHYSTATS:
2574 case ETHTOOL_GTSO:
2575 case ETHTOOL_GPERMADDR:
2576 case ETHTOOL_GUFO:
2577 case ETHTOOL_GGSO:
2578 case ETHTOOL_GGRO:
2579 case ETHTOOL_GFLAGS:
2580 case ETHTOOL_GPFLAGS:
2581 case ETHTOOL_GRXFH:
2582 case ETHTOOL_GRXRINGS:
2583 case ETHTOOL_GRXCLSRLCNT:
2584 case ETHTOOL_GRXCLSRULE:
2585 case ETHTOOL_GRXCLSRLALL:
2586 case ETHTOOL_GRXFHINDIR:
2587 case ETHTOOL_GRSSH:
2588 case ETHTOOL_GFEATURES:
2589 case ETHTOOL_GCHANNELS:
2590 case ETHTOOL_GET_TS_INFO:
2591 case ETHTOOL_GEEE:
2592 case ETHTOOL_GTUNABLE:
2593 case ETHTOOL_PHY_GTUNABLE:
2594 case ETHTOOL_GLINKSETTINGS:
2595 case ETHTOOL_GFECPARAM:
2596 break;
2597 default:
2598 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2599 return -EPERM;
2600 }
2601
2602 if (dev->ethtool_ops->begin) {
2603 rc = dev->ethtool_ops->begin(dev);
2604 if (rc < 0)
2605 return rc;
2606 }
2607 old_features = dev->features;
2608
2609 switch (ethcmd) {
2610 case ETHTOOL_GSET:
2611 rc = ethtool_get_settings(dev, useraddr);
2612 break;
2613 case ETHTOOL_SSET:
2614 rc = ethtool_set_settings(dev, useraddr);
2615 break;
2616 case ETHTOOL_GDRVINFO:
2617 rc = ethtool_get_drvinfo(dev, useraddr);
2618 break;
2619 case ETHTOOL_GREGS:
2620 rc = ethtool_get_regs(dev, useraddr);
2621 break;
2622 case ETHTOOL_GWOL:
2623 rc = ethtool_get_wol(dev, useraddr);
2624 break;
2625 case ETHTOOL_SWOL:
2626 rc = ethtool_set_wol(dev, useraddr);
2627 break;
2628 case ETHTOOL_GMSGLVL:
2629 rc = ethtool_get_value(dev, useraddr, ethcmd,
2630 dev->ethtool_ops->get_msglevel);
2631 break;
2632 case ETHTOOL_SMSGLVL:
2633 rc = ethtool_set_value_void(dev, useraddr,
2634 dev->ethtool_ops->set_msglevel);
2635 break;
2636 case ETHTOOL_GEEE:
2637 rc = ethtool_get_eee(dev, useraddr);
2638 break;
2639 case ETHTOOL_SEEE:
2640 rc = ethtool_set_eee(dev, useraddr);
2641 break;
2642 case ETHTOOL_NWAY_RST:
2643 rc = ethtool_nway_reset(dev);
2644 break;
2645 case ETHTOOL_GLINK:
2646 rc = ethtool_get_link(dev, useraddr);
2647 break;
2648 case ETHTOOL_GEEPROM:
2649 rc = ethtool_get_eeprom(dev, useraddr);
2650 break;
2651 case ETHTOOL_SEEPROM:
2652 rc = ethtool_set_eeprom(dev, useraddr);
2653 break;
2654 case ETHTOOL_GCOALESCE:
2655 rc = ethtool_get_coalesce(dev, useraddr);
2656 break;
2657 case ETHTOOL_SCOALESCE:
2658 rc = ethtool_set_coalesce(dev, useraddr);
2659 break;
2660 case ETHTOOL_GRINGPARAM:
2661 rc = ethtool_get_ringparam(dev, useraddr);
2662 break;
2663 case ETHTOOL_SRINGPARAM:
2664 rc = ethtool_set_ringparam(dev, useraddr);
2665 break;
2666 case ETHTOOL_GPAUSEPARAM:
2667 rc = ethtool_get_pauseparam(dev, useraddr);
2668 break;
2669 case ETHTOOL_SPAUSEPARAM:
2670 rc = ethtool_set_pauseparam(dev, useraddr);
2671 break;
2672 case ETHTOOL_TEST:
2673 rc = ethtool_self_test(dev, useraddr);
2674 break;
2675 case ETHTOOL_GSTRINGS:
2676 rc = ethtool_get_strings(dev, useraddr);
2677 break;
2678 case ETHTOOL_PHYS_ID:
2679 rc = ethtool_phys_id(dev, useraddr);
2680 break;
2681 case ETHTOOL_GSTATS:
2682 rc = ethtool_get_stats(dev, useraddr);
2683 break;
2684 case ETHTOOL_GPERMADDR:
2685 rc = ethtool_get_perm_addr(dev, useraddr);
2686 break;
2687 case ETHTOOL_GFLAGS:
2688 rc = ethtool_get_value(dev, useraddr, ethcmd,
2689 __ethtool_get_flags);
2690 break;
2691 case ETHTOOL_SFLAGS:
2692 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2693 break;
2694 case ETHTOOL_GPFLAGS:
2695 rc = ethtool_get_value(dev, useraddr, ethcmd,
2696 dev->ethtool_ops->get_priv_flags);
2697 break;
2698 case ETHTOOL_SPFLAGS:
2699 rc = ethtool_set_value(dev, useraddr,
2700 dev->ethtool_ops->set_priv_flags);
2701 break;
2702 case ETHTOOL_GRXFH:
2703 case ETHTOOL_GRXRINGS:
2704 case ETHTOOL_GRXCLSRLCNT:
2705 case ETHTOOL_GRXCLSRULE:
2706 case ETHTOOL_GRXCLSRLALL:
2707 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2708 break;
2709 case ETHTOOL_SRXFH:
2710 case ETHTOOL_SRXCLSRLDEL:
2711 case ETHTOOL_SRXCLSRLINS:
2712 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2713 break;
2714 case ETHTOOL_FLASHDEV:
2715 rc = ethtool_flash_device(dev, useraddr);
2716 break;
2717 case ETHTOOL_RESET:
2718 rc = ethtool_reset(dev, useraddr);
2719 break;
2720 case ETHTOOL_GSSET_INFO:
2721 rc = ethtool_get_sset_info(dev, useraddr);
2722 break;
2723 case ETHTOOL_GRXFHINDIR:
2724 rc = ethtool_get_rxfh_indir(dev, useraddr);
2725 break;
2726 case ETHTOOL_SRXFHINDIR:
2727 rc = ethtool_set_rxfh_indir(dev, useraddr);
2728 break;
2729 case ETHTOOL_GRSSH:
2730 rc = ethtool_get_rxfh(dev, useraddr);
2731 break;
2732 case ETHTOOL_SRSSH:
2733 rc = ethtool_set_rxfh(dev, useraddr);
2734 break;
2735 case ETHTOOL_GFEATURES:
2736 rc = ethtool_get_features(dev, useraddr);
2737 break;
2738 case ETHTOOL_SFEATURES:
2739 rc = ethtool_set_features(dev, useraddr);
2740 break;
2741 case ETHTOOL_GTXCSUM:
2742 case ETHTOOL_GRXCSUM:
2743 case ETHTOOL_GSG:
2744 case ETHTOOL_GTSO:
2745 case ETHTOOL_GGSO:
2746 case ETHTOOL_GGRO:
2747 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2748 break;
2749 case ETHTOOL_STXCSUM:
2750 case ETHTOOL_SRXCSUM:
2751 case ETHTOOL_SSG:
2752 case ETHTOOL_STSO:
2753 case ETHTOOL_SGSO:
2754 case ETHTOOL_SGRO:
2755 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2756 break;
2757 case ETHTOOL_GCHANNELS:
2758 rc = ethtool_get_channels(dev, useraddr);
2759 break;
2760 case ETHTOOL_SCHANNELS:
2761 rc = ethtool_set_channels(dev, useraddr);
2762 break;
2763 case ETHTOOL_SET_DUMP:
2764 rc = ethtool_set_dump(dev, useraddr);
2765 break;
2766 case ETHTOOL_GET_DUMP_FLAG:
2767 rc = ethtool_get_dump_flag(dev, useraddr);
2768 break;
2769 case ETHTOOL_GET_DUMP_DATA:
2770 rc = ethtool_get_dump_data(dev, useraddr);
2771 break;
2772 case ETHTOOL_GET_TS_INFO:
2773 rc = ethtool_get_ts_info(dev, useraddr);
2774 break;
2775 case ETHTOOL_GMODULEINFO:
2776 rc = ethtool_get_module_info(dev, useraddr);
2777 break;
2778 case ETHTOOL_GMODULEEEPROM:
2779 rc = ethtool_get_module_eeprom(dev, useraddr);
2780 break;
2781 case ETHTOOL_GTUNABLE:
2782 rc = ethtool_get_tunable(dev, useraddr);
2783 break;
2784 case ETHTOOL_STUNABLE:
2785 rc = ethtool_set_tunable(dev, useraddr);
2786 break;
2787 case ETHTOOL_GPHYSTATS:
2788 rc = ethtool_get_phy_stats(dev, useraddr);
2789 break;
2790 case ETHTOOL_PERQUEUE:
2791 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2792 break;
2793 case ETHTOOL_GLINKSETTINGS:
2794 rc = ethtool_get_link_ksettings(dev, useraddr);
2795 break;
2796 case ETHTOOL_SLINKSETTINGS:
2797 rc = ethtool_set_link_ksettings(dev, useraddr);
2798 break;
2799 case ETHTOOL_PHY_GTUNABLE:
2800 rc = get_phy_tunable(dev, useraddr);
2801 break;
2802 case ETHTOOL_PHY_STUNABLE:
2803 rc = set_phy_tunable(dev, useraddr);
2804 break;
2805 case ETHTOOL_GFECPARAM:
2806 rc = ethtool_get_fecparam(dev, useraddr);
2807 break;
2808 case ETHTOOL_SFECPARAM:
2809 rc = ethtool_set_fecparam(dev, useraddr);
2810 break;
2811 default:
2812 rc = -EOPNOTSUPP;
2813 }
2814
2815 if (dev->ethtool_ops->complete)
2816 dev->ethtool_ops->complete(dev);
2817
2818 if (old_features != dev->features)
2819 netdev_features_change(dev);
2820
2821 return rc;
2822}
2823
2824struct ethtool_rx_flow_key {
2825 struct flow_dissector_key_basic basic;
2826 union {
2827 struct flow_dissector_key_ipv4_addrs ipv4;
2828 struct flow_dissector_key_ipv6_addrs ipv6;
2829 };
2830 struct flow_dissector_key_ports tp;
2831 struct flow_dissector_key_ip ip;
2832 struct flow_dissector_key_vlan vlan;
2833 struct flow_dissector_key_eth_addrs eth_addrs;
2834} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2835
2836struct ethtool_rx_flow_match {
2837 struct flow_dissector dissector;
2838 struct ethtool_rx_flow_key key;
2839 struct ethtool_rx_flow_key mask;
2840};
2841
2842struct ethtool_rx_flow_rule *
2843ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
2844{
2845 const struct ethtool_rx_flow_spec *fs = input->fs;
2846 static struct in6_addr zero_addr = {};
2847 struct ethtool_rx_flow_match *match;
2848 struct ethtool_rx_flow_rule *flow;
2849 struct flow_action_entry *act;
2850
2851 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
2852 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
2853 if (!flow)
2854 return ERR_PTR(-ENOMEM);
2855
2856 /* ethtool_rx supports only one single action per rule. */
2857 flow->rule = flow_rule_alloc(1);
2858 if (!flow->rule) {
2859 kfree(flow);
2860 return ERR_PTR(-ENOMEM);
2861 }
2862
2863 match = (struct ethtool_rx_flow_match *)flow->priv;
2864 flow->rule->match.dissector = &match->dissector;
2865 flow->rule->match.mask = &match->mask;
2866 flow->rule->match.key = &match->key;
2867
2868 match->mask.basic.n_proto = htons(0xffff);
2869
2870 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2871 case TCP_V4_FLOW:
2872 case UDP_V4_FLOW: {
2873 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
2874
2875 match->key.basic.n_proto = htons(ETH_P_IP);
2876
2877 v4_spec = &fs->h_u.tcp_ip4_spec;
2878 v4_m_spec = &fs->m_u.tcp_ip4_spec;
2879
2880 if (v4_m_spec->ip4src) {
2881 match->key.ipv4.src = v4_spec->ip4src;
2882 match->mask.ipv4.src = v4_m_spec->ip4src;
2883 }
2884 if (v4_m_spec->ip4dst) {
2885 match->key.ipv4.dst = v4_spec->ip4dst;
2886 match->mask.ipv4.dst = v4_m_spec->ip4dst;
2887 }
2888 if (v4_m_spec->ip4src ||
2889 v4_m_spec->ip4dst) {
2890 match->dissector.used_keys |=
2891 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
2892 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
2893 offsetof(struct ethtool_rx_flow_key, ipv4);
2894 }
2895 if (v4_m_spec->psrc) {
2896 match->key.tp.src = v4_spec->psrc;
2897 match->mask.tp.src = v4_m_spec->psrc;
2898 }
2899 if (v4_m_spec->pdst) {
2900 match->key.tp.dst = v4_spec->pdst;
2901 match->mask.tp.dst = v4_m_spec->pdst;
2902 }
2903 if (v4_m_spec->psrc ||
2904 v4_m_spec->pdst) {
2905 match->dissector.used_keys |=
2906 BIT(FLOW_DISSECTOR_KEY_PORTS);
2907 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
2908 offsetof(struct ethtool_rx_flow_key, tp);
2909 }
2910 if (v4_m_spec->tos) {
2911 match->key.ip.tos = v4_spec->tos;
2912 match->mask.ip.tos = v4_m_spec->tos;
2913 match->dissector.used_keys |=
2914 BIT(FLOW_DISSECTOR_KEY_IP);
2915 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
2916 offsetof(struct ethtool_rx_flow_key, ip);
2917 }
2918 }
2919 break;
2920 case TCP_V6_FLOW:
2921 case UDP_V6_FLOW: {
2922 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
2923
2924 match->key.basic.n_proto = htons(ETH_P_IPV6);
2925
2926 v6_spec = &fs->h_u.tcp_ip6_spec;
2927 v6_m_spec = &fs->m_u.tcp_ip6_spec;
2928 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2929 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
2930 sizeof(match->key.ipv6.src));
2931 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
2932 sizeof(match->mask.ipv6.src));
2933 }
2934 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
2935 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
2936 sizeof(match->key.ipv6.dst));
2937 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
2938 sizeof(match->mask.ipv6.dst));
2939 }
2940 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
2941 memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
2942 match->dissector.used_keys |=
2943 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
2944 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
2945 offsetof(struct ethtool_rx_flow_key, ipv6);
2946 }
2947 if (v6_m_spec->psrc) {
2948 match->key.tp.src = v6_spec->psrc;
2949 match->mask.tp.src = v6_m_spec->psrc;
2950 }
2951 if (v6_m_spec->pdst) {
2952 match->key.tp.dst = v6_spec->pdst;
2953 match->mask.tp.dst = v6_m_spec->pdst;
2954 }
2955 if (v6_m_spec->psrc ||
2956 v6_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 (v6_m_spec->tclass) {
2963 match->key.ip.tos = v6_spec->tclass;
2964 match->mask.ip.tos = v6_m_spec->tclass;
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 default:
2973 ethtool_rx_flow_rule_destroy(flow);
2974 return ERR_PTR(-EINVAL);
2975 }
2976
2977 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
2978 case TCP_V4_FLOW:
2979 case TCP_V6_FLOW:
2980 match->key.basic.ip_proto = IPPROTO_TCP;
2981 break;
2982 case UDP_V4_FLOW:
2983 case UDP_V6_FLOW:
2984 match->key.basic.ip_proto = IPPROTO_UDP;
2985 break;
2986 }
2987 match->mask.basic.ip_proto = 0xff;
2988
2989 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
2990 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
2991 offsetof(struct ethtool_rx_flow_key, basic);
2992
2993 if (fs->flow_type & FLOW_EXT) {
2994 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
2995 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
2996
2997 if (ext_m_spec->vlan_etype &&
2998 ext_m_spec->vlan_tci) {
2999 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3000 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3001
3002 match->key.vlan.vlan_id =
3003 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3004 match->mask.vlan.vlan_id =
3005 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3006
3007 match->key.vlan.vlan_priority =
3008 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3009 match->mask.vlan.vlan_priority =
3010 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3011
3012 match->dissector.used_keys |=
3013 BIT(FLOW_DISSECTOR_KEY_VLAN);
3014 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3015 offsetof(struct ethtool_rx_flow_key, vlan);
3016 }
3017 }
3018 if (fs->flow_type & FLOW_MAC_EXT) {
3019 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3020 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3021
3022 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3023 ETH_ALEN);
3024 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3025 ETH_ALEN);
3026
3027 match->dissector.used_keys |=
3028 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3029 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3030 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3031 }
3032
3033 act = &flow->rule->action.entries[0];
3034 switch (fs->ring_cookie) {
3035 case RX_CLS_FLOW_DISC:
3036 act->id = FLOW_ACTION_DROP;
3037 break;
3038 case RX_CLS_FLOW_WAKE:
3039 act->id = FLOW_ACTION_WAKE;
3040 break;
3041 default:
3042 act->id = FLOW_ACTION_QUEUE;
3043 if (fs->flow_type & FLOW_RSS)
3044 act->queue.ctx = input->rss_ctx;
3045
3046 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3047 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3048 break;
3049 }
3050
3051 return flow;
3052}
3053EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3054
3055void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3056{
3057 kfree(flow->rule);
3058 kfree(flow);
3059}
3060EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);