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