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