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