seq_buf: Make seq_buf_puts() null-terminate the buffer
[linux/fpc-iii.git] / net / core / ethtool.c
blobaeabc4831fca691f85f879bffcb574e04067377b
1 /*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
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.
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>
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.
37 u32 ethtool_op_get_link(struct net_device *dev)
39 return netif_carrier_ok(dev) ? 1 : 0;
41 EXPORT_SYMBOL(ethtool_op_get_link);
43 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
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;
52 EXPORT_SYMBOL(ethtool_op_get_ts_info);
54 /* Handlers for each ethtool command */
56 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
58 static 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",
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",
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",
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",
117 static const char
118 rss_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",
124 static const char
125 tunable_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",
132 static const char
133 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
134 [ETHTOOL_ID_UNSPEC] = "Unspec",
135 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
138 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
140 struct ethtool_gfeatures cmd = {
141 .cmd = ETHTOOL_GFEATURES,
142 .size = ETHTOOL_DEV_FEATURE_WORDS,
144 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
145 u32 __user *sizeaddr;
146 u32 copy_size;
147 int i;
149 /* in case feature bits run out again */
150 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
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));
160 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
161 if (get_user(copy_size, sizeaddr))
162 return -EFAULT;
164 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
165 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
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;
173 return 0;
176 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
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;
183 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
184 return -EFAULT;
185 useraddr += sizeof(cmd);
187 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
188 return -EINVAL;
190 if (copy_from_user(features, useraddr, sizeof(features)))
191 return -EFAULT;
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);
198 if (valid & ~NETIF_F_ETHTOOL_BITS)
199 return -EINVAL;
201 if (valid & ~dev->hw_features) {
202 valid &= dev->hw_features;
203 ret |= ETHTOOL_F_UNSUPPORTED;
206 dev->wanted_features &= ~valid;
207 dev->wanted_features |= wanted & valid;
208 __netdev_update_features(dev);
210 if ((dev->wanted_features ^ dev->features) & valid)
211 ret |= ETHTOOL_F_WISH;
213 return ret;
216 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
218 const struct ethtool_ops *ops = dev->ethtool_ops;
220 if (sset == ETH_SS_FEATURES)
221 return ARRAY_SIZE(netdev_features_strings);
223 if (sset == ETH_SS_RSS_HASH_FUNCS)
224 return ARRAY_SIZE(rss_hash_func_strings);
226 if (sset == ETH_SS_TUNABLES)
227 return ARRAY_SIZE(tunable_strings);
229 if (sset == ETH_SS_PHY_TUNABLES)
230 return ARRAY_SIZE(phy_tunable_strings);
232 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
233 !ops->get_ethtool_phy_stats)
234 return phy_ethtool_get_sset_count(dev->phydev);
236 if (ops->get_sset_count && ops->get_strings)
237 return ops->get_sset_count(dev, sset);
238 else
239 return -EOPNOTSUPP;
242 static void __ethtool_get_strings(struct net_device *dev,
243 u32 stringset, u8 *data)
245 const struct ethtool_ops *ops = dev->ethtool_ops;
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);
265 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
267 /* feature masks of legacy discrete ethtool ops */
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();
293 static int ethtool_get_one_feature(struct net_device *dev,
294 char __user *useraddr, u32 ethcmd)
296 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
297 struct ethtool_value edata = {
298 .cmd = ethcmd,
299 .data = !!(dev->features & mask),
302 if (copy_to_user(useraddr, &edata, sizeof(edata)))
303 return -EFAULT;
304 return 0;
307 static int ethtool_set_one_feature(struct net_device *dev,
308 void __user *useraddr, u32 ethcmd)
310 struct ethtool_value edata;
311 netdev_features_t mask;
313 if (copy_from_user(&edata, useraddr, sizeof(edata)))
314 return -EFAULT;
316 mask = ethtool_get_feature_mask(ethcmd);
317 mask &= dev->hw_features;
318 if (!mask)
319 return -EOPNOTSUPP;
321 if (edata.data)
322 dev->wanted_features |= mask;
323 else
324 dev->wanted_features &= ~mask;
326 __netdev_update_features(dev);
328 return 0;
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)
337 static u32 __ethtool_get_flags(struct net_device *dev)
339 u32 flags = 0;
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;
352 return flags;
355 static int __ethtool_set_flags(struct net_device *dev, u32 data)
357 netdev_features_t features = 0, changed;
359 if (data & ~ETH_ALL_FLAGS)
360 return -EINVAL;
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;
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;
378 dev->wanted_features =
379 (dev->wanted_features & ~changed) | (features & changed);
381 __netdev_update_features(dev);
383 return 0;
386 /* Given two link masks, AND them together and save the result in dst. */
387 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
388 struct ethtool_link_ksettings *src)
390 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
391 unsigned int idx = 0;
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];
400 EXPORT_SYMBOL(ethtool_intersect_link_masks);
402 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
403 u32 legacy_u32)
405 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
406 dst[0] = legacy_u32;
408 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
410 /* return false if src had higher bits set. lower bits always updated. */
411 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
412 const unsigned long *src)
414 bool retval = true;
416 /* TODO: following test will soon always be true */
417 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
418 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
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;
429 *legacy_u32 = src[0];
430 return retval;
432 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
434 /* return false if legacy contained non-0 deprecated fields
435 * maxtxpkt/maxrxpkt. rest of ksettings always updated
437 static bool
438 convert_legacy_settings_to_link_ksettings(
439 struct ethtool_link_ksettings *link_ksettings,
440 const struct ethtool_cmd *legacy_settings)
442 bool retval = true;
444 memset(link_ksettings, 0, sizeof(*link_ksettings));
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
450 if (legacy_settings->maxtxpkt ||
451 legacy_settings->maxrxpkt)
452 retval = false;
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;
482 /* return false if ksettings link modes had higher bits
483 * set. legacy_settings always updated (best effort)
485 static bool
486 convert_link_ksettings_to_legacy_settings(
487 struct ethtool_cmd *legacy_settings,
488 const struct ethtool_link_ksettings *link_ksettings)
490 bool retval = true;
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;
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;
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)
532 /* layout of the struct passed from/to userland */
533 struct 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;
542 /* Internal kernel helper to query a device ethtool_link_settings.
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.
552 int __ethtool_get_link_ksettings(struct net_device *dev,
553 struct ethtool_link_ksettings *link_ksettings)
555 int err;
556 struct ethtool_cmd cmd;
558 ASSERT_RTNL();
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);
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
570 if (!dev->ethtool_ops->get_settings)
571 return -EOPNOTSUPP;
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;
579 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
581 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
582 return err;
584 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
586 /* convert ethtool_link_usettings in user space to a kernel internal
587 * ethtool_link_ksettings. return 0 on success, errno on error.
589 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
590 const void __user *from)
592 struct ethtool_link_usettings link_usettings;
594 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
595 return -EFAULT;
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);
608 return 0;
611 /* convert a kernel internal ethtool_link_ksettings to
612 * ethtool_link_usettings in user space. return 0 on success, errno on
613 * error.
615 static int
616 store_link_ksettings_for_user(void __user *to,
617 const struct ethtool_link_ksettings *from)
619 struct ethtool_link_usettings link_usettings;
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);
632 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
633 return -EFAULT;
635 return 0;
638 /* Query device for its ethtool_link_settings.
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.
648 static int ethtool_get_link_ksettings(struct net_device *dev,
649 void __user *useraddr)
651 int err = 0;
652 struct ethtool_link_ksettings link_ksettings;
654 ASSERT_RTNL();
656 if (!dev->ethtool_ops->get_link_ksettings)
657 return -EOPNOTSUPP;
659 /* handle bitmap nbits handshake */
660 if (copy_from_user(&link_ksettings.base, useraddr,
661 sizeof(link_ksettings.base)))
662 return -EFAULT;
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);
675 /* copy the base fields back to user, not the link
676 * mode bitmaps
678 if (copy_to_user(useraddr, &link_ksettings.base,
679 sizeof(link_ksettings.base)))
680 return -EFAULT;
682 return 0;
685 /* handshake successful: user/kernel agree on
686 * link_mode_masks_nwords
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;
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;
699 return store_link_ksettings_for_user(useraddr, &link_ksettings);
702 /* Update device ethtool_link_settings.
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.
712 static int ethtool_set_link_ksettings(struct net_device *dev,
713 void __user *useraddr)
715 int err;
716 struct ethtool_link_ksettings link_ksettings;
718 ASSERT_RTNL();
720 if (!dev->ethtool_ops->set_link_ksettings)
721 return -EOPNOTSUPP;
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;
728 if (__ETHTOOL_LINK_MODE_MASK_NU32
729 != link_ksettings.base.link_mode_masks_nwords)
730 return -EINVAL;
732 /* copy the whole structure, now that we know it has expected
733 * format
735 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
736 if (err)
737 return err;
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;
744 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
747 /* Query device for its ethtool_cmd settings.
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).
757 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
759 struct ethtool_cmd cmd;
761 ASSERT_RTNL();
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;
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);
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.
783 int err;
785 if (!dev->ethtool_ops->get_settings)
786 return -EOPNOTSUPP;
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;
795 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
796 return -EFAULT;
798 return 0;
801 /* Update device link settings with given ethtool_cmd.
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.
811 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
813 struct ethtool_cmd cmd;
815 ASSERT_RTNL();
817 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
818 return -EFAULT;
820 /* first, try new %ethtool_link_ksettings API. */
821 if (dev->ethtool_ops->set_link_ksettings) {
822 struct ethtool_link_ksettings link_ksettings;
824 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
825 &cmd))
826 return -EINVAL;
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);
835 /* legacy %ethtool_cmd API */
837 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
838 * disappears internally
841 if (!dev->ethtool_ops->set_settings)
842 return -EOPNOTSUPP;
844 return dev->ethtool_ops->set_settings(dev, &cmd);
847 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
848 void __user *useraddr)
850 struct ethtool_drvinfo info;
851 const struct ethtool_ops *ops = dev->ethtool_ops;
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;
867 * this method of obtaining string set info is deprecated;
868 * Use ETHTOOL_GSSET_INFO instead.
870 if (ops->get_sset_count) {
871 int rc;
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;
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);
888 if (copy_to_user(useraddr, &info, sizeof(info)))
889 return -EFAULT;
890 return 0;
893 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
894 void __user *useraddr)
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;
901 if (copy_from_user(&info, useraddr, sizeof(info)))
902 return -EFAULT;
904 /* store copy of mask, because we zero struct later on */
905 sset_mask = info.sset_mask;
906 if (!sset_mask)
907 return 0;
909 /* calculate size of return buffer */
910 n_bits = hweight64(sset_mask);
912 memset(&info, 0, sizeof(info));
913 info.cmd = ETHTOOL_GSSET_INFO;
915 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
916 if (!info_buf)
917 return -ENOMEM;
920 * fill return buffer based on input bitmask and successful
921 * get_sset_count return
923 for (i = 0; i < 64; i++) {
924 if (!(sset_mask & (1ULL << i)))
925 continue;
927 rc = __ethtool_get_sset_count(dev, i);
928 if (rc >= 0) {
929 info.sset_mask |= (1ULL << i);
930 info_buf[idx++] = rc;
934 ret = -EFAULT;
935 if (copy_to_user(useraddr, &info, sizeof(info)))
936 goto out;
938 useraddr += offsetof(struct ethtool_sset_info, data);
939 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
940 goto out;
942 ret = 0;
944 out:
945 kfree(info_buf);
946 return ret;
949 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
950 u32 cmd, void __user *useraddr)
952 struct ethtool_rxnfc info;
953 size_t info_size = sizeof(info);
954 int rc;
956 if (!dev->ethtool_ops->set_rxnfc)
957 return -EOPNOTSUPP;
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));
967 if (copy_from_user(&info, useraddr, info_size))
968 return -EFAULT;
970 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
971 if (rc)
972 return rc;
974 if (cmd == ETHTOOL_SRXCLSRLINS &&
975 copy_to_user(useraddr, &info, info_size))
976 return -EFAULT;
978 return 0;
981 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
982 u32 cmd, void __user *useraddr)
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;
990 if (!ops->get_rxnfc)
991 return -EOPNOTSUPP;
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));
1001 if (copy_from_user(&info, useraddr, info_size))
1002 return -EFAULT;
1004 /* If FLOW_RSS was requested then user-space must be using the
1005 * new definition, as FLOW_RSS is newer.
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.
1014 if (!(info.flow_type & FLOW_RSS))
1015 return -EINVAL;
1018 if (info.cmd != cmd)
1019 return -EINVAL;
1021 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1022 if (info.rule_cnt > 0) {
1023 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1024 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1025 GFP_USER);
1026 if (!rule_buf)
1027 return -ENOMEM;
1031 ret = ops->get_rxnfc(dev, &info, rule_buf);
1032 if (ret < 0)
1033 goto err_out;
1035 ret = -EFAULT;
1036 if (copy_to_user(useraddr, &info, info_size))
1037 goto err_out;
1039 if (rule_buf) {
1040 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1041 if (copy_to_user(useraddr, rule_buf,
1042 info.rule_cnt * sizeof(u32)))
1043 goto err_out;
1045 ret = 0;
1047 err_out:
1048 kfree(rule_buf);
1050 return ret;
1053 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1054 struct ethtool_rxnfc *rx_rings,
1055 u32 size)
1057 int i;
1059 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1060 return -EFAULT;
1062 /* Validate ring indices */
1063 for (i = 0; i < size; i++)
1064 if (indir[i] >= rx_rings->data)
1065 return -EINVAL;
1067 return 0;
1070 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1072 void netdev_rss_key_fill(void *buffer, size_t len)
1074 BUG_ON(len > sizeof(netdev_rss_key));
1075 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1076 memcpy(buffer, netdev_rss_key, len);
1078 EXPORT_SYMBOL(netdev_rss_key_fill);
1080 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1082 u32 dev_size, current_max = 0;
1083 u32 *indir;
1084 int ret;
1086 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1087 !dev->ethtool_ops->get_rxfh)
1088 return -EOPNOTSUPP;
1089 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1090 if (dev_size == 0)
1091 return -EOPNOTSUPP;
1093 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1094 if (!indir)
1095 return -ENOMEM;
1097 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1098 if (ret)
1099 goto out;
1101 while (dev_size--)
1102 current_max = max(current_max, indir[dev_size]);
1104 *max = current_max;
1106 out:
1107 kfree(indir);
1108 return ret;
1111 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1112 void __user *useraddr)
1114 u32 user_size, dev_size;
1115 u32 *indir;
1116 int ret;
1118 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1119 !dev->ethtool_ops->get_rxfh)
1120 return -EOPNOTSUPP;
1121 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1122 if (dev_size == 0)
1123 return -EOPNOTSUPP;
1125 if (copy_from_user(&user_size,
1126 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1127 sizeof(user_size)))
1128 return -EFAULT;
1130 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1131 &dev_size, sizeof(dev_size)))
1132 return -EFAULT;
1134 /* If the user buffer size is 0, this is just a query for the
1135 * device table size. Otherwise, if it's smaller than the
1136 * device table size it's an error.
1138 if (user_size < dev_size)
1139 return user_size == 0 ? 0 : -EINVAL;
1141 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1142 if (!indir)
1143 return -ENOMEM;
1145 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1146 if (ret)
1147 goto out;
1149 if (copy_to_user(useraddr +
1150 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1151 indir, dev_size * sizeof(indir[0])))
1152 ret = -EFAULT;
1154 out:
1155 kfree(indir);
1156 return ret;
1159 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1160 void __user *useraddr)
1162 struct ethtool_rxnfc rx_rings;
1163 u32 user_size, dev_size, i;
1164 u32 *indir;
1165 const struct ethtool_ops *ops = dev->ethtool_ops;
1166 int ret;
1167 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1169 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1170 !ops->get_rxnfc)
1171 return -EOPNOTSUPP;
1173 dev_size = ops->get_rxfh_indir_size(dev);
1174 if (dev_size == 0)
1175 return -EOPNOTSUPP;
1177 if (copy_from_user(&user_size,
1178 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1179 sizeof(user_size)))
1180 return -EFAULT;
1182 if (user_size != 0 && user_size != dev_size)
1183 return -EINVAL;
1185 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1186 if (!indir)
1187 return -ENOMEM;
1189 rx_rings.cmd = ETHTOOL_GRXRINGS;
1190 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1191 if (ret)
1192 goto out;
1194 if (user_size == 0) {
1195 for (i = 0; i < dev_size; i++)
1196 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1197 } else {
1198 ret = ethtool_copy_validate_indir(indir,
1199 useraddr + ringidx_offset,
1200 &rx_rings,
1201 dev_size);
1202 if (ret)
1203 goto out;
1206 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1207 if (ret)
1208 goto out;
1210 /* indicate whether rxfh was set to default */
1211 if (user_size == 0)
1212 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1213 else
1214 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1216 out:
1217 kfree(indir);
1218 return ret;
1221 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1222 void __user *useraddr)
1224 int ret;
1225 const struct ethtool_ops *ops = dev->ethtool_ops;
1226 u32 user_indir_size, user_key_size;
1227 u32 dev_indir_size = 0, dev_key_size = 0;
1228 struct ethtool_rxfh rxfh;
1229 u32 total_size;
1230 u32 indir_bytes;
1231 u32 *indir = NULL;
1232 u8 dev_hfunc = 0;
1233 u8 *hkey = NULL;
1234 u8 *rss_config;
1236 if (!ops->get_rxfh)
1237 return -EOPNOTSUPP;
1239 if (ops->get_rxfh_indir_size)
1240 dev_indir_size = ops->get_rxfh_indir_size(dev);
1241 if (ops->get_rxfh_key_size)
1242 dev_key_size = ops->get_rxfh_key_size(dev);
1244 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1245 return -EFAULT;
1246 user_indir_size = rxfh.indir_size;
1247 user_key_size = rxfh.key_size;
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->get_rxfh_context)
1254 return -EOPNOTSUPP;
1256 rxfh.indir_size = dev_indir_size;
1257 rxfh.key_size = dev_key_size;
1258 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1259 return -EFAULT;
1261 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1262 (user_key_size && (user_key_size != dev_key_size)))
1263 return -EINVAL;
1265 indir_bytes = user_indir_size * sizeof(indir[0]);
1266 total_size = indir_bytes + user_key_size;
1267 rss_config = kzalloc(total_size, GFP_USER);
1268 if (!rss_config)
1269 return -ENOMEM;
1271 if (user_indir_size)
1272 indir = (u32 *)rss_config;
1274 if (user_key_size)
1275 hkey = rss_config + indir_bytes;
1277 if (rxfh.rss_context)
1278 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1279 &dev_hfunc,
1280 rxfh.rss_context);
1281 else
1282 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1283 if (ret)
1284 goto out;
1286 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1287 &dev_hfunc, sizeof(rxfh.hfunc))) {
1288 ret = -EFAULT;
1289 } else if (copy_to_user(useraddr +
1290 offsetof(struct ethtool_rxfh, rss_config[0]),
1291 rss_config, total_size)) {
1292 ret = -EFAULT;
1294 out:
1295 kfree(rss_config);
1297 return ret;
1300 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1301 void __user *useraddr)
1303 int ret;
1304 const struct ethtool_ops *ops = dev->ethtool_ops;
1305 struct ethtool_rxnfc rx_rings;
1306 struct ethtool_rxfh rxfh;
1307 u32 dev_indir_size = 0, dev_key_size = 0, i;
1308 u32 *indir = NULL, indir_bytes = 0;
1309 u8 *hkey = NULL;
1310 u8 *rss_config;
1311 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1312 bool delete = false;
1314 if (!ops->get_rxnfc || !ops->set_rxfh)
1315 return -EOPNOTSUPP;
1317 if (ops->get_rxfh_indir_size)
1318 dev_indir_size = ops->get_rxfh_indir_size(dev);
1319 if (ops->get_rxfh_key_size)
1320 dev_key_size = ops->get_rxfh_key_size(dev);
1322 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1323 return -EFAULT;
1325 /* Check that reserved fields are 0 for now */
1326 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1327 return -EINVAL;
1328 /* Most drivers don't handle rss_context, check it's 0 as well */
1329 if (rxfh.rss_context && !ops->set_rxfh_context)
1330 return -EOPNOTSUPP;
1332 /* If either indir, hash key or function is valid, proceed further.
1333 * Must request at least one change: indir size, hash key or function.
1335 if ((rxfh.indir_size &&
1336 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1337 rxfh.indir_size != dev_indir_size) ||
1338 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1339 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1340 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1341 return -EINVAL;
1343 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1344 indir_bytes = dev_indir_size * sizeof(indir[0]);
1346 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1347 if (!rss_config)
1348 return -ENOMEM;
1350 rx_rings.cmd = ETHTOOL_GRXRINGS;
1351 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1352 if (ret)
1353 goto out;
1355 /* rxfh.indir_size == 0 means reset the indir table to default (master
1356 * context) or delete the context (other RSS contexts).
1357 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1359 if (rxfh.indir_size &&
1360 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1361 indir = (u32 *)rss_config;
1362 ret = ethtool_copy_validate_indir(indir,
1363 useraddr + rss_cfg_offset,
1364 &rx_rings,
1365 rxfh.indir_size);
1366 if (ret)
1367 goto out;
1368 } else if (rxfh.indir_size == 0) {
1369 if (rxfh.rss_context == 0) {
1370 indir = (u32 *)rss_config;
1371 for (i = 0; i < dev_indir_size; i++)
1372 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1373 } else {
1374 delete = true;
1378 if (rxfh.key_size) {
1379 hkey = rss_config + indir_bytes;
1380 if (copy_from_user(hkey,
1381 useraddr + rss_cfg_offset + indir_bytes,
1382 rxfh.key_size)) {
1383 ret = -EFAULT;
1384 goto out;
1388 if (rxfh.rss_context)
1389 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1390 &rxfh.rss_context, delete);
1391 else
1392 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1393 if (ret)
1394 goto out;
1396 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1397 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1398 ret = -EFAULT;
1400 if (!rxfh.rss_context) {
1401 /* indicate whether rxfh was set to default */
1402 if (rxfh.indir_size == 0)
1403 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1404 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1405 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1408 out:
1409 kfree(rss_config);
1410 return ret;
1413 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1415 struct ethtool_regs regs;
1416 const struct ethtool_ops *ops = dev->ethtool_ops;
1417 void *regbuf;
1418 int reglen, ret;
1420 if (!ops->get_regs || !ops->get_regs_len)
1421 return -EOPNOTSUPP;
1423 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1424 return -EFAULT;
1426 reglen = ops->get_regs_len(dev);
1427 if (regs.len > reglen)
1428 regs.len = reglen;
1430 regbuf = NULL;
1431 if (reglen) {
1432 regbuf = vzalloc(reglen);
1433 if (!regbuf)
1434 return -ENOMEM;
1437 ops->get_regs(dev, &regs, regbuf);
1439 ret = -EFAULT;
1440 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1441 goto out;
1442 useraddr += offsetof(struct ethtool_regs, data);
1443 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1444 goto out;
1445 ret = 0;
1447 out:
1448 vfree(regbuf);
1449 return ret;
1452 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1454 struct ethtool_value reset;
1455 int ret;
1457 if (!dev->ethtool_ops->reset)
1458 return -EOPNOTSUPP;
1460 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1461 return -EFAULT;
1463 ret = dev->ethtool_ops->reset(dev, &reset.data);
1464 if (ret)
1465 return ret;
1467 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1468 return -EFAULT;
1469 return 0;
1472 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1474 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1476 if (!dev->ethtool_ops->get_wol)
1477 return -EOPNOTSUPP;
1479 dev->ethtool_ops->get_wol(dev, &wol);
1481 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1482 return -EFAULT;
1483 return 0;
1486 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1488 struct ethtool_wolinfo wol;
1489 int ret;
1491 if (!dev->ethtool_ops->set_wol)
1492 return -EOPNOTSUPP;
1494 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1495 return -EFAULT;
1497 ret = dev->ethtool_ops->set_wol(dev, &wol);
1498 if (ret)
1499 return ret;
1501 dev->wol_enabled = !!wol.wolopts;
1503 return 0;
1506 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1508 struct ethtool_eee edata;
1509 int rc;
1511 if (!dev->ethtool_ops->get_eee)
1512 return -EOPNOTSUPP;
1514 memset(&edata, 0, sizeof(struct ethtool_eee));
1515 edata.cmd = ETHTOOL_GEEE;
1516 rc = dev->ethtool_ops->get_eee(dev, &edata);
1518 if (rc)
1519 return rc;
1521 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1522 return -EFAULT;
1524 return 0;
1527 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1529 struct ethtool_eee edata;
1531 if (!dev->ethtool_ops->set_eee)
1532 return -EOPNOTSUPP;
1534 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1535 return -EFAULT;
1537 return dev->ethtool_ops->set_eee(dev, &edata);
1540 static int ethtool_nway_reset(struct net_device *dev)
1542 if (!dev->ethtool_ops->nway_reset)
1543 return -EOPNOTSUPP;
1545 return dev->ethtool_ops->nway_reset(dev);
1548 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1550 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1552 if (!dev->ethtool_ops->get_link)
1553 return -EOPNOTSUPP;
1555 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1557 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1558 return -EFAULT;
1559 return 0;
1562 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1563 int (*getter)(struct net_device *,
1564 struct ethtool_eeprom *, u8 *),
1565 u32 total_len)
1567 struct ethtool_eeprom eeprom;
1568 void __user *userbuf = useraddr + sizeof(eeprom);
1569 u32 bytes_remaining;
1570 u8 *data;
1571 int ret = 0;
1573 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1574 return -EFAULT;
1576 /* Check for wrap and zero */
1577 if (eeprom.offset + eeprom.len <= eeprom.offset)
1578 return -EINVAL;
1580 /* Check for exceeding total eeprom len */
1581 if (eeprom.offset + eeprom.len > total_len)
1582 return -EINVAL;
1584 data = kmalloc(PAGE_SIZE, GFP_USER);
1585 if (!data)
1586 return -ENOMEM;
1588 bytes_remaining = eeprom.len;
1589 while (bytes_remaining > 0) {
1590 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1592 ret = getter(dev, &eeprom, data);
1593 if (ret)
1594 break;
1595 if (copy_to_user(userbuf, data, eeprom.len)) {
1596 ret = -EFAULT;
1597 break;
1599 userbuf += eeprom.len;
1600 eeprom.offset += eeprom.len;
1601 bytes_remaining -= eeprom.len;
1604 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1605 eeprom.offset -= eeprom.len;
1606 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1607 ret = -EFAULT;
1609 kfree(data);
1610 return ret;
1613 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1615 const struct ethtool_ops *ops = dev->ethtool_ops;
1617 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1618 !ops->get_eeprom_len(dev))
1619 return -EOPNOTSUPP;
1621 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1622 ops->get_eeprom_len(dev));
1625 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1627 struct ethtool_eeprom eeprom;
1628 const struct ethtool_ops *ops = dev->ethtool_ops;
1629 void __user *userbuf = useraddr + sizeof(eeprom);
1630 u32 bytes_remaining;
1631 u8 *data;
1632 int ret = 0;
1634 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1635 !ops->get_eeprom_len(dev))
1636 return -EOPNOTSUPP;
1638 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1639 return -EFAULT;
1641 /* Check for wrap and zero */
1642 if (eeprom.offset + eeprom.len <= eeprom.offset)
1643 return -EINVAL;
1645 /* Check for exceeding total eeprom len */
1646 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1647 return -EINVAL;
1649 data = kmalloc(PAGE_SIZE, GFP_USER);
1650 if (!data)
1651 return -ENOMEM;
1653 bytes_remaining = eeprom.len;
1654 while (bytes_remaining > 0) {
1655 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1657 if (copy_from_user(data, userbuf, eeprom.len)) {
1658 ret = -EFAULT;
1659 break;
1661 ret = ops->set_eeprom(dev, &eeprom, data);
1662 if (ret)
1663 break;
1664 userbuf += eeprom.len;
1665 eeprom.offset += eeprom.len;
1666 bytes_remaining -= eeprom.len;
1669 kfree(data);
1670 return ret;
1673 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1674 void __user *useraddr)
1676 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1678 if (!dev->ethtool_ops->get_coalesce)
1679 return -EOPNOTSUPP;
1681 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1683 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1684 return -EFAULT;
1685 return 0;
1688 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1689 void __user *useraddr)
1691 struct ethtool_coalesce coalesce;
1693 if (!dev->ethtool_ops->set_coalesce)
1694 return -EOPNOTSUPP;
1696 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1697 return -EFAULT;
1699 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1702 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1704 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1706 if (!dev->ethtool_ops->get_ringparam)
1707 return -EOPNOTSUPP;
1709 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1711 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1712 return -EFAULT;
1713 return 0;
1716 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1718 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1720 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1721 return -EOPNOTSUPP;
1723 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1724 return -EFAULT;
1726 dev->ethtool_ops->get_ringparam(dev, &max);
1728 /* ensure new ring parameters are within the maximums */
1729 if (ringparam.rx_pending > max.rx_max_pending ||
1730 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1731 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1732 ringparam.tx_pending > max.tx_max_pending)
1733 return -EINVAL;
1735 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1738 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1739 void __user *useraddr)
1741 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1743 if (!dev->ethtool_ops->get_channels)
1744 return -EOPNOTSUPP;
1746 dev->ethtool_ops->get_channels(dev, &channels);
1748 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1749 return -EFAULT;
1750 return 0;
1753 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1754 void __user *useraddr)
1756 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1757 u32 max_rx_in_use = 0;
1759 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1760 return -EOPNOTSUPP;
1762 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1763 return -EFAULT;
1765 dev->ethtool_ops->get_channels(dev, &max);
1767 /* ensure new counts are within the maximums */
1768 if ((channels.rx_count > max.max_rx) ||
1769 (channels.tx_count > max.max_tx) ||
1770 (channels.combined_count > max.max_combined) ||
1771 (channels.other_count > max.max_other))
1772 return -EINVAL;
1774 /* ensure the new Rx count fits within the configured Rx flow
1775 * indirection table settings */
1776 if (netif_is_rxfh_configured(dev) &&
1777 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1778 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1779 return -EINVAL;
1781 return dev->ethtool_ops->set_channels(dev, &channels);
1784 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1786 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1788 if (!dev->ethtool_ops->get_pauseparam)
1789 return -EOPNOTSUPP;
1791 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1793 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1794 return -EFAULT;
1795 return 0;
1798 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1800 struct ethtool_pauseparam pauseparam;
1802 if (!dev->ethtool_ops->set_pauseparam)
1803 return -EOPNOTSUPP;
1805 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1806 return -EFAULT;
1808 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1811 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1813 struct ethtool_test test;
1814 const struct ethtool_ops *ops = dev->ethtool_ops;
1815 u64 *data;
1816 int ret, test_len;
1818 if (!ops->self_test || !ops->get_sset_count)
1819 return -EOPNOTSUPP;
1821 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1822 if (test_len < 0)
1823 return test_len;
1824 WARN_ON(test_len == 0);
1826 if (copy_from_user(&test, useraddr, sizeof(test)))
1827 return -EFAULT;
1829 test.len = test_len;
1830 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1831 if (!data)
1832 return -ENOMEM;
1834 ops->self_test(dev, &test, data);
1836 ret = -EFAULT;
1837 if (copy_to_user(useraddr, &test, sizeof(test)))
1838 goto out;
1839 useraddr += sizeof(test);
1840 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1841 goto out;
1842 ret = 0;
1844 out:
1845 kfree(data);
1846 return ret;
1849 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1851 struct ethtool_gstrings gstrings;
1852 u8 *data;
1853 int ret;
1855 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1856 return -EFAULT;
1858 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1859 if (ret < 0)
1860 return ret;
1861 if (ret > S32_MAX / ETH_GSTRING_LEN)
1862 return -ENOMEM;
1863 WARN_ON_ONCE(!ret);
1865 gstrings.len = ret;
1866 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1867 if (gstrings.len && !data)
1868 return -ENOMEM;
1870 __ethtool_get_strings(dev, gstrings.string_set, data);
1872 ret = -EFAULT;
1873 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1874 goto out;
1875 useraddr += sizeof(gstrings);
1876 if (gstrings.len &&
1877 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1878 goto out;
1879 ret = 0;
1881 out:
1882 vfree(data);
1883 return ret;
1886 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1888 struct ethtool_value id;
1889 static bool busy;
1890 const struct ethtool_ops *ops = dev->ethtool_ops;
1891 int rc;
1893 if (!ops->set_phys_id)
1894 return -EOPNOTSUPP;
1896 if (busy)
1897 return -EBUSY;
1899 if (copy_from_user(&id, useraddr, sizeof(id)))
1900 return -EFAULT;
1902 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1903 if (rc < 0)
1904 return rc;
1906 /* Drop the RTNL lock while waiting, but prevent reentry or
1907 * removal of the device.
1909 busy = true;
1910 dev_hold(dev);
1911 rtnl_unlock();
1913 if (rc == 0) {
1914 /* Driver will handle this itself */
1915 schedule_timeout_interruptible(
1916 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1917 } else {
1918 /* Driver expects to be called at twice the frequency in rc */
1919 int n = rc * 2, i, interval = HZ / n;
1921 /* Count down seconds */
1922 do {
1923 /* Count down iterations per second */
1924 i = n;
1925 do {
1926 rtnl_lock();
1927 rc = ops->set_phys_id(dev,
1928 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1929 rtnl_unlock();
1930 if (rc)
1931 break;
1932 schedule_timeout_interruptible(interval);
1933 } while (!signal_pending(current) && --i != 0);
1934 } while (!signal_pending(current) &&
1935 (id.data == 0 || --id.data != 0));
1938 rtnl_lock();
1939 dev_put(dev);
1940 busy = false;
1942 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1943 return rc;
1946 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1948 struct ethtool_stats stats;
1949 const struct ethtool_ops *ops = dev->ethtool_ops;
1950 u64 *data;
1951 int ret, n_stats;
1953 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1954 return -EOPNOTSUPP;
1956 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1957 if (n_stats < 0)
1958 return n_stats;
1959 if (n_stats > S32_MAX / sizeof(u64))
1960 return -ENOMEM;
1961 WARN_ON_ONCE(!n_stats);
1962 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1963 return -EFAULT;
1965 stats.n_stats = n_stats;
1966 data = vzalloc(array_size(n_stats, sizeof(u64)));
1967 if (n_stats && !data)
1968 return -ENOMEM;
1970 ops->get_ethtool_stats(dev, &stats, data);
1972 ret = -EFAULT;
1973 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1974 goto out;
1975 useraddr += sizeof(stats);
1976 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1977 goto out;
1978 ret = 0;
1980 out:
1981 vfree(data);
1982 return ret;
1985 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1987 const struct ethtool_ops *ops = dev->ethtool_ops;
1988 struct phy_device *phydev = dev->phydev;
1989 struct ethtool_stats stats;
1990 u64 *data;
1991 int ret, n_stats;
1993 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1994 return -EOPNOTSUPP;
1996 if (dev->phydev && !ops->get_ethtool_phy_stats)
1997 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1998 else
1999 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2000 if (n_stats < 0)
2001 return n_stats;
2002 if (n_stats > S32_MAX / sizeof(u64))
2003 return -ENOMEM;
2004 WARN_ON_ONCE(!n_stats);
2006 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2007 return -EFAULT;
2009 stats.n_stats = n_stats;
2010 data = vzalloc(array_size(n_stats, sizeof(u64)));
2011 if (n_stats && !data)
2012 return -ENOMEM;
2014 if (dev->phydev && !ops->get_ethtool_phy_stats) {
2015 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2016 if (ret < 0)
2017 return ret;
2018 } else {
2019 ops->get_ethtool_phy_stats(dev, &stats, data);
2022 ret = -EFAULT;
2023 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2024 goto out;
2025 useraddr += sizeof(stats);
2026 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2027 goto out;
2028 ret = 0;
2030 out:
2031 vfree(data);
2032 return ret;
2035 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2037 struct ethtool_perm_addr epaddr;
2039 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2040 return -EFAULT;
2042 if (epaddr.size < dev->addr_len)
2043 return -ETOOSMALL;
2044 epaddr.size = dev->addr_len;
2046 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2047 return -EFAULT;
2048 useraddr += sizeof(epaddr);
2049 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2050 return -EFAULT;
2051 return 0;
2054 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2055 u32 cmd, u32 (*actor)(struct net_device *))
2057 struct ethtool_value edata = { .cmd = cmd };
2059 if (!actor)
2060 return -EOPNOTSUPP;
2062 edata.data = actor(dev);
2064 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2065 return -EFAULT;
2066 return 0;
2069 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2070 void (*actor)(struct net_device *, u32))
2072 struct ethtool_value edata;
2074 if (!actor)
2075 return -EOPNOTSUPP;
2077 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2078 return -EFAULT;
2080 actor(dev, edata.data);
2081 return 0;
2084 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2085 int (*actor)(struct net_device *, u32))
2087 struct ethtool_value edata;
2089 if (!actor)
2090 return -EOPNOTSUPP;
2092 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2093 return -EFAULT;
2095 return actor(dev, edata.data);
2098 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2099 char __user *useraddr)
2101 struct ethtool_flash efl;
2103 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2104 return -EFAULT;
2106 if (!dev->ethtool_ops->flash_device)
2107 return -EOPNOTSUPP;
2109 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2111 return dev->ethtool_ops->flash_device(dev, &efl);
2114 static int ethtool_set_dump(struct net_device *dev,
2115 void __user *useraddr)
2117 struct ethtool_dump dump;
2119 if (!dev->ethtool_ops->set_dump)
2120 return -EOPNOTSUPP;
2122 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2123 return -EFAULT;
2125 return dev->ethtool_ops->set_dump(dev, &dump);
2128 static int ethtool_get_dump_flag(struct net_device *dev,
2129 void __user *useraddr)
2131 int ret;
2132 struct ethtool_dump dump;
2133 const struct ethtool_ops *ops = dev->ethtool_ops;
2135 if (!ops->get_dump_flag)
2136 return -EOPNOTSUPP;
2138 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2139 return -EFAULT;
2141 ret = ops->get_dump_flag(dev, &dump);
2142 if (ret)
2143 return ret;
2145 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2146 return -EFAULT;
2147 return 0;
2150 static int ethtool_get_dump_data(struct net_device *dev,
2151 void __user *useraddr)
2153 int ret;
2154 __u32 len;
2155 struct ethtool_dump dump, tmp;
2156 const struct ethtool_ops *ops = dev->ethtool_ops;
2157 void *data = NULL;
2159 if (!ops->get_dump_data || !ops->get_dump_flag)
2160 return -EOPNOTSUPP;
2162 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2163 return -EFAULT;
2165 memset(&tmp, 0, sizeof(tmp));
2166 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2167 ret = ops->get_dump_flag(dev, &tmp);
2168 if (ret)
2169 return ret;
2171 len = min(tmp.len, dump.len);
2172 if (!len)
2173 return -EFAULT;
2175 /* Don't ever let the driver think there's more space available
2176 * than it requested with .get_dump_flag().
2178 dump.len = len;
2180 /* Always allocate enough space to hold the whole thing so that the
2181 * driver does not need to check the length and bother with partial
2182 * dumping.
2184 data = vzalloc(tmp.len);
2185 if (!data)
2186 return -ENOMEM;
2187 ret = ops->get_dump_data(dev, &dump, data);
2188 if (ret)
2189 goto out;
2191 /* There are two sane possibilities:
2192 * 1. The driver's .get_dump_data() does not touch dump.len.
2193 * 2. Or it may set dump.len to how much it really writes, which
2194 * should be tmp.len (or len if it can do a partial dump).
2195 * In any case respond to userspace with the actual length of data
2196 * it's receiving.
2198 WARN_ON(dump.len != len && dump.len != tmp.len);
2199 dump.len = len;
2201 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2202 ret = -EFAULT;
2203 goto out;
2205 useraddr += offsetof(struct ethtool_dump, data);
2206 if (copy_to_user(useraddr, data, len))
2207 ret = -EFAULT;
2208 out:
2209 vfree(data);
2210 return ret;
2213 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2215 int err = 0;
2216 struct ethtool_ts_info info;
2217 const struct ethtool_ops *ops = dev->ethtool_ops;
2218 struct phy_device *phydev = dev->phydev;
2220 memset(&info, 0, sizeof(info));
2221 info.cmd = ETHTOOL_GET_TS_INFO;
2223 if (phydev && phydev->drv && phydev->drv->ts_info) {
2224 err = phydev->drv->ts_info(phydev, &info);
2225 } else if (ops->get_ts_info) {
2226 err = ops->get_ts_info(dev, &info);
2227 } else {
2228 info.so_timestamping =
2229 SOF_TIMESTAMPING_RX_SOFTWARE |
2230 SOF_TIMESTAMPING_SOFTWARE;
2231 info.phc_index = -1;
2234 if (err)
2235 return err;
2237 if (copy_to_user(useraddr, &info, sizeof(info)))
2238 err = -EFAULT;
2240 return err;
2243 static int __ethtool_get_module_info(struct net_device *dev,
2244 struct ethtool_modinfo *modinfo)
2246 const struct ethtool_ops *ops = dev->ethtool_ops;
2247 struct phy_device *phydev = dev->phydev;
2249 if (dev->sfp_bus)
2250 return sfp_get_module_info(dev->sfp_bus, modinfo);
2252 if (phydev && phydev->drv && phydev->drv->module_info)
2253 return phydev->drv->module_info(phydev, modinfo);
2255 if (ops->get_module_info)
2256 return ops->get_module_info(dev, modinfo);
2258 return -EOPNOTSUPP;
2261 static int ethtool_get_module_info(struct net_device *dev,
2262 void __user *useraddr)
2264 int ret;
2265 struct ethtool_modinfo modinfo;
2267 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2268 return -EFAULT;
2270 ret = __ethtool_get_module_info(dev, &modinfo);
2271 if (ret)
2272 return ret;
2274 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2275 return -EFAULT;
2277 return 0;
2280 static int __ethtool_get_module_eeprom(struct net_device *dev,
2281 struct ethtool_eeprom *ee, u8 *data)
2283 const struct ethtool_ops *ops = dev->ethtool_ops;
2284 struct phy_device *phydev = dev->phydev;
2286 if (dev->sfp_bus)
2287 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2289 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2290 return phydev->drv->module_eeprom(phydev, ee, data);
2292 if (ops->get_module_eeprom)
2293 return ops->get_module_eeprom(dev, ee, data);
2295 return -EOPNOTSUPP;
2298 static int ethtool_get_module_eeprom(struct net_device *dev,
2299 void __user *useraddr)
2301 int ret;
2302 struct ethtool_modinfo modinfo;
2304 ret = __ethtool_get_module_info(dev, &modinfo);
2305 if (ret)
2306 return ret;
2308 return ethtool_get_any_eeprom(dev, useraddr,
2309 __ethtool_get_module_eeprom,
2310 modinfo.eeprom_len);
2313 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2315 switch (tuna->id) {
2316 case ETHTOOL_RX_COPYBREAK:
2317 case ETHTOOL_TX_COPYBREAK:
2318 if (tuna->len != sizeof(u32) ||
2319 tuna->type_id != ETHTOOL_TUNABLE_U32)
2320 return -EINVAL;
2321 break;
2322 case ETHTOOL_PFC_PREVENTION_TOUT:
2323 if (tuna->len != sizeof(u16) ||
2324 tuna->type_id != ETHTOOL_TUNABLE_U16)
2325 return -EINVAL;
2326 break;
2327 default:
2328 return -EINVAL;
2331 return 0;
2334 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2336 int ret;
2337 struct ethtool_tunable tuna;
2338 const struct ethtool_ops *ops = dev->ethtool_ops;
2339 void *data;
2341 if (!ops->get_tunable)
2342 return -EOPNOTSUPP;
2343 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2344 return -EFAULT;
2345 ret = ethtool_tunable_valid(&tuna);
2346 if (ret)
2347 return ret;
2348 data = kmalloc(tuna.len, GFP_USER);
2349 if (!data)
2350 return -ENOMEM;
2351 ret = ops->get_tunable(dev, &tuna, data);
2352 if (ret)
2353 goto out;
2354 useraddr += sizeof(tuna);
2355 ret = -EFAULT;
2356 if (copy_to_user(useraddr, data, tuna.len))
2357 goto out;
2358 ret = 0;
2360 out:
2361 kfree(data);
2362 return ret;
2365 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2367 int ret;
2368 struct ethtool_tunable tuna;
2369 const struct ethtool_ops *ops = dev->ethtool_ops;
2370 void *data;
2372 if (!ops->set_tunable)
2373 return -EOPNOTSUPP;
2374 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2375 return -EFAULT;
2376 ret = ethtool_tunable_valid(&tuna);
2377 if (ret)
2378 return ret;
2379 useraddr += sizeof(tuna);
2380 data = memdup_user(useraddr, tuna.len);
2381 if (IS_ERR(data))
2382 return PTR_ERR(data);
2383 ret = ops->set_tunable(dev, &tuna, data);
2385 kfree(data);
2386 return ret;
2389 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2390 void __user *useraddr,
2391 struct ethtool_per_queue_op *per_queue_opt)
2393 u32 bit;
2394 int ret;
2395 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2397 if (!dev->ethtool_ops->get_per_queue_coalesce)
2398 return -EOPNOTSUPP;
2400 useraddr += sizeof(*per_queue_opt);
2402 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2403 MAX_NUM_QUEUE);
2405 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2406 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2408 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2409 if (ret != 0)
2410 return ret;
2411 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2412 return -EFAULT;
2413 useraddr += sizeof(coalesce);
2416 return 0;
2419 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2420 void __user *useraddr,
2421 struct ethtool_per_queue_op *per_queue_opt)
2423 u32 bit;
2424 int i, ret = 0;
2425 int n_queue;
2426 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2427 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2429 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2430 (!dev->ethtool_ops->get_per_queue_coalesce))
2431 return -EOPNOTSUPP;
2433 useraddr += sizeof(*per_queue_opt);
2435 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2436 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2437 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2438 if (!backup)
2439 return -ENOMEM;
2441 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2442 struct ethtool_coalesce coalesce;
2444 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2445 if (ret != 0)
2446 goto roll_back;
2448 tmp++;
2450 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2451 ret = -EFAULT;
2452 goto roll_back;
2455 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2456 if (ret != 0)
2457 goto roll_back;
2459 useraddr += sizeof(coalesce);
2462 roll_back:
2463 if (ret != 0) {
2464 tmp = backup;
2465 for_each_set_bit(i, queue_mask, bit) {
2466 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2467 tmp++;
2470 kfree(backup);
2472 return ret;
2475 static int ethtool_set_per_queue(struct net_device *dev,
2476 void __user *useraddr, u32 sub_cmd)
2478 struct ethtool_per_queue_op per_queue_opt;
2480 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2481 return -EFAULT;
2483 if (per_queue_opt.sub_command != sub_cmd)
2484 return -EINVAL;
2486 switch (per_queue_opt.sub_command) {
2487 case ETHTOOL_GCOALESCE:
2488 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2489 case ETHTOOL_SCOALESCE:
2490 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2491 default:
2492 return -EOPNOTSUPP;
2496 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2498 switch (tuna->id) {
2499 case ETHTOOL_PHY_DOWNSHIFT:
2500 if (tuna->len != sizeof(u8) ||
2501 tuna->type_id != ETHTOOL_TUNABLE_U8)
2502 return -EINVAL;
2503 break;
2504 default:
2505 return -EINVAL;
2508 return 0;
2511 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2513 int ret;
2514 struct ethtool_tunable tuna;
2515 struct phy_device *phydev = dev->phydev;
2516 void *data;
2518 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2519 return -EOPNOTSUPP;
2521 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2522 return -EFAULT;
2523 ret = ethtool_phy_tunable_valid(&tuna);
2524 if (ret)
2525 return ret;
2526 data = kmalloc(tuna.len, GFP_USER);
2527 if (!data)
2528 return -ENOMEM;
2529 mutex_lock(&phydev->lock);
2530 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2531 mutex_unlock(&phydev->lock);
2532 if (ret)
2533 goto out;
2534 useraddr += sizeof(tuna);
2535 ret = -EFAULT;
2536 if (copy_to_user(useraddr, data, tuna.len))
2537 goto out;
2538 ret = 0;
2540 out:
2541 kfree(data);
2542 return ret;
2545 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2547 int ret;
2548 struct ethtool_tunable tuna;
2549 struct phy_device *phydev = dev->phydev;
2550 void *data;
2552 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2553 return -EOPNOTSUPP;
2554 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2555 return -EFAULT;
2556 ret = ethtool_phy_tunable_valid(&tuna);
2557 if (ret)
2558 return ret;
2559 useraddr += sizeof(tuna);
2560 data = memdup_user(useraddr, tuna.len);
2561 if (IS_ERR(data))
2562 return PTR_ERR(data);
2563 mutex_lock(&phydev->lock);
2564 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2565 mutex_unlock(&phydev->lock);
2567 kfree(data);
2568 return ret;
2571 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2573 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2574 int rc;
2576 if (!dev->ethtool_ops->get_fecparam)
2577 return -EOPNOTSUPP;
2579 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2580 if (rc)
2581 return rc;
2583 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2584 return -EFAULT;
2585 return 0;
2588 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2590 struct ethtool_fecparam fecparam;
2592 if (!dev->ethtool_ops->set_fecparam)
2593 return -EOPNOTSUPP;
2595 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2596 return -EFAULT;
2598 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2601 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2603 int dev_ethtool(struct net *net, struct ifreq *ifr)
2605 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2606 void __user *useraddr = ifr->ifr_data;
2607 u32 ethcmd, sub_cmd;
2608 int rc;
2609 netdev_features_t old_features;
2611 if (!dev || !netif_device_present(dev))
2612 return -ENODEV;
2614 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
2615 return -EFAULT;
2617 if (ethcmd == ETHTOOL_PERQUEUE) {
2618 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2619 return -EFAULT;
2620 } else {
2621 sub_cmd = ethcmd;
2623 /* Allow some commands to be done by anyone */
2624 switch (sub_cmd) {
2625 case ETHTOOL_GSET:
2626 case ETHTOOL_GDRVINFO:
2627 case ETHTOOL_GMSGLVL:
2628 case ETHTOOL_GLINK:
2629 case ETHTOOL_GCOALESCE:
2630 case ETHTOOL_GRINGPARAM:
2631 case ETHTOOL_GPAUSEPARAM:
2632 case ETHTOOL_GRXCSUM:
2633 case ETHTOOL_GTXCSUM:
2634 case ETHTOOL_GSG:
2635 case ETHTOOL_GSSET_INFO:
2636 case ETHTOOL_GSTRINGS:
2637 case ETHTOOL_GSTATS:
2638 case ETHTOOL_GPHYSTATS:
2639 case ETHTOOL_GTSO:
2640 case ETHTOOL_GPERMADDR:
2641 case ETHTOOL_GUFO:
2642 case ETHTOOL_GGSO:
2643 case ETHTOOL_GGRO:
2644 case ETHTOOL_GFLAGS:
2645 case ETHTOOL_GPFLAGS:
2646 case ETHTOOL_GRXFH:
2647 case ETHTOOL_GRXRINGS:
2648 case ETHTOOL_GRXCLSRLCNT:
2649 case ETHTOOL_GRXCLSRULE:
2650 case ETHTOOL_GRXCLSRLALL:
2651 case ETHTOOL_GRXFHINDIR:
2652 case ETHTOOL_GRSSH:
2653 case ETHTOOL_GFEATURES:
2654 case ETHTOOL_GCHANNELS:
2655 case ETHTOOL_GET_TS_INFO:
2656 case ETHTOOL_GEEE:
2657 case ETHTOOL_GTUNABLE:
2658 case ETHTOOL_PHY_GTUNABLE:
2659 case ETHTOOL_GLINKSETTINGS:
2660 case ETHTOOL_GFECPARAM:
2661 break;
2662 default:
2663 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2664 return -EPERM;
2667 if (dev->ethtool_ops->begin) {
2668 rc = dev->ethtool_ops->begin(dev);
2669 if (rc < 0)
2670 return rc;
2672 old_features = dev->features;
2674 switch (ethcmd) {
2675 case ETHTOOL_GSET:
2676 rc = ethtool_get_settings(dev, useraddr);
2677 break;
2678 case ETHTOOL_SSET:
2679 rc = ethtool_set_settings(dev, useraddr);
2680 break;
2681 case ETHTOOL_GDRVINFO:
2682 rc = ethtool_get_drvinfo(dev, useraddr);
2683 break;
2684 case ETHTOOL_GREGS:
2685 rc = ethtool_get_regs(dev, useraddr);
2686 break;
2687 case ETHTOOL_GWOL:
2688 rc = ethtool_get_wol(dev, useraddr);
2689 break;
2690 case ETHTOOL_SWOL:
2691 rc = ethtool_set_wol(dev, useraddr);
2692 break;
2693 case ETHTOOL_GMSGLVL:
2694 rc = ethtool_get_value(dev, useraddr, ethcmd,
2695 dev->ethtool_ops->get_msglevel);
2696 break;
2697 case ETHTOOL_SMSGLVL:
2698 rc = ethtool_set_value_void(dev, useraddr,
2699 dev->ethtool_ops->set_msglevel);
2700 break;
2701 case ETHTOOL_GEEE:
2702 rc = ethtool_get_eee(dev, useraddr);
2703 break;
2704 case ETHTOOL_SEEE:
2705 rc = ethtool_set_eee(dev, useraddr);
2706 break;
2707 case ETHTOOL_NWAY_RST:
2708 rc = ethtool_nway_reset(dev);
2709 break;
2710 case ETHTOOL_GLINK:
2711 rc = ethtool_get_link(dev, useraddr);
2712 break;
2713 case ETHTOOL_GEEPROM:
2714 rc = ethtool_get_eeprom(dev, useraddr);
2715 break;
2716 case ETHTOOL_SEEPROM:
2717 rc = ethtool_set_eeprom(dev, useraddr);
2718 break;
2719 case ETHTOOL_GCOALESCE:
2720 rc = ethtool_get_coalesce(dev, useraddr);
2721 break;
2722 case ETHTOOL_SCOALESCE:
2723 rc = ethtool_set_coalesce(dev, useraddr);
2724 break;
2725 case ETHTOOL_GRINGPARAM:
2726 rc = ethtool_get_ringparam(dev, useraddr);
2727 break;
2728 case ETHTOOL_SRINGPARAM:
2729 rc = ethtool_set_ringparam(dev, useraddr);
2730 break;
2731 case ETHTOOL_GPAUSEPARAM:
2732 rc = ethtool_get_pauseparam(dev, useraddr);
2733 break;
2734 case ETHTOOL_SPAUSEPARAM:
2735 rc = ethtool_set_pauseparam(dev, useraddr);
2736 break;
2737 case ETHTOOL_TEST:
2738 rc = ethtool_self_test(dev, useraddr);
2739 break;
2740 case ETHTOOL_GSTRINGS:
2741 rc = ethtool_get_strings(dev, useraddr);
2742 break;
2743 case ETHTOOL_PHYS_ID:
2744 rc = ethtool_phys_id(dev, useraddr);
2745 break;
2746 case ETHTOOL_GSTATS:
2747 rc = ethtool_get_stats(dev, useraddr);
2748 break;
2749 case ETHTOOL_GPERMADDR:
2750 rc = ethtool_get_perm_addr(dev, useraddr);
2751 break;
2752 case ETHTOOL_GFLAGS:
2753 rc = ethtool_get_value(dev, useraddr, ethcmd,
2754 __ethtool_get_flags);
2755 break;
2756 case ETHTOOL_SFLAGS:
2757 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2758 break;
2759 case ETHTOOL_GPFLAGS:
2760 rc = ethtool_get_value(dev, useraddr, ethcmd,
2761 dev->ethtool_ops->get_priv_flags);
2762 break;
2763 case ETHTOOL_SPFLAGS:
2764 rc = ethtool_set_value(dev, useraddr,
2765 dev->ethtool_ops->set_priv_flags);
2766 break;
2767 case ETHTOOL_GRXFH:
2768 case ETHTOOL_GRXRINGS:
2769 case ETHTOOL_GRXCLSRLCNT:
2770 case ETHTOOL_GRXCLSRULE:
2771 case ETHTOOL_GRXCLSRLALL:
2772 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2773 break;
2774 case ETHTOOL_SRXFH:
2775 case ETHTOOL_SRXCLSRLDEL:
2776 case ETHTOOL_SRXCLSRLINS:
2777 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2778 break;
2779 case ETHTOOL_FLASHDEV:
2780 rc = ethtool_flash_device(dev, useraddr);
2781 break;
2782 case ETHTOOL_RESET:
2783 rc = ethtool_reset(dev, useraddr);
2784 break;
2785 case ETHTOOL_GSSET_INFO:
2786 rc = ethtool_get_sset_info(dev, useraddr);
2787 break;
2788 case ETHTOOL_GRXFHINDIR:
2789 rc = ethtool_get_rxfh_indir(dev, useraddr);
2790 break;
2791 case ETHTOOL_SRXFHINDIR:
2792 rc = ethtool_set_rxfh_indir(dev, useraddr);
2793 break;
2794 case ETHTOOL_GRSSH:
2795 rc = ethtool_get_rxfh(dev, useraddr);
2796 break;
2797 case ETHTOOL_SRSSH:
2798 rc = ethtool_set_rxfh(dev, useraddr);
2799 break;
2800 case ETHTOOL_GFEATURES:
2801 rc = ethtool_get_features(dev, useraddr);
2802 break;
2803 case ETHTOOL_SFEATURES:
2804 rc = ethtool_set_features(dev, useraddr);
2805 break;
2806 case ETHTOOL_GTXCSUM:
2807 case ETHTOOL_GRXCSUM:
2808 case ETHTOOL_GSG:
2809 case ETHTOOL_GTSO:
2810 case ETHTOOL_GGSO:
2811 case ETHTOOL_GGRO:
2812 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2813 break;
2814 case ETHTOOL_STXCSUM:
2815 case ETHTOOL_SRXCSUM:
2816 case ETHTOOL_SSG:
2817 case ETHTOOL_STSO:
2818 case ETHTOOL_SGSO:
2819 case ETHTOOL_SGRO:
2820 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2821 break;
2822 case ETHTOOL_GCHANNELS:
2823 rc = ethtool_get_channels(dev, useraddr);
2824 break;
2825 case ETHTOOL_SCHANNELS:
2826 rc = ethtool_set_channels(dev, useraddr);
2827 break;
2828 case ETHTOOL_SET_DUMP:
2829 rc = ethtool_set_dump(dev, useraddr);
2830 break;
2831 case ETHTOOL_GET_DUMP_FLAG:
2832 rc = ethtool_get_dump_flag(dev, useraddr);
2833 break;
2834 case ETHTOOL_GET_DUMP_DATA:
2835 rc = ethtool_get_dump_data(dev, useraddr);
2836 break;
2837 case ETHTOOL_GET_TS_INFO:
2838 rc = ethtool_get_ts_info(dev, useraddr);
2839 break;
2840 case ETHTOOL_GMODULEINFO:
2841 rc = ethtool_get_module_info(dev, useraddr);
2842 break;
2843 case ETHTOOL_GMODULEEEPROM:
2844 rc = ethtool_get_module_eeprom(dev, useraddr);
2845 break;
2846 case ETHTOOL_GTUNABLE:
2847 rc = ethtool_get_tunable(dev, useraddr);
2848 break;
2849 case ETHTOOL_STUNABLE:
2850 rc = ethtool_set_tunable(dev, useraddr);
2851 break;
2852 case ETHTOOL_GPHYSTATS:
2853 rc = ethtool_get_phy_stats(dev, useraddr);
2854 break;
2855 case ETHTOOL_PERQUEUE:
2856 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2857 break;
2858 case ETHTOOL_GLINKSETTINGS:
2859 rc = ethtool_get_link_ksettings(dev, useraddr);
2860 break;
2861 case ETHTOOL_SLINKSETTINGS:
2862 rc = ethtool_set_link_ksettings(dev, useraddr);
2863 break;
2864 case ETHTOOL_PHY_GTUNABLE:
2865 rc = get_phy_tunable(dev, useraddr);
2866 break;
2867 case ETHTOOL_PHY_STUNABLE:
2868 rc = set_phy_tunable(dev, useraddr);
2869 break;
2870 case ETHTOOL_GFECPARAM:
2871 rc = ethtool_get_fecparam(dev, useraddr);
2872 break;
2873 case ETHTOOL_SFECPARAM:
2874 rc = ethtool_set_fecparam(dev, useraddr);
2875 break;
2876 default:
2877 rc = -EOPNOTSUPP;
2880 if (dev->ethtool_ops->complete)
2881 dev->ethtool_ops->complete(dev);
2883 if (old_features != dev->features)
2884 netdev_features_change(dev);
2886 return rc;