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