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
flow_type_hashable(u32 flow_type)981 static bool flow_type_hashable(u32 flow_type)
982 {
983 switch (flow_type) {
984 case TCP_V4_FLOW:
985 case UDP_V4_FLOW:
986 case SCTP_V4_FLOW:
987 case AH_ESP_V4_FLOW:
988 case TCP_V6_FLOW:
989 case UDP_V6_FLOW:
990 case SCTP_V6_FLOW:
991 case AH_ESP_V6_FLOW:
992 case AH_V4_FLOW:
993 case ESP_V4_FLOW:
994 case AH_V6_FLOW:
995 case ESP_V6_FLOW:
996 case IPV4_FLOW:
997 case IPV6_FLOW:
998 case GTPU_V4_FLOW:
999 case GTPU_V6_FLOW:
1000 case GTPC_V4_FLOW:
1001 case GTPC_V6_FLOW:
1002 case GTPC_TEID_V4_FLOW:
1003 case GTPC_TEID_V6_FLOW:
1004 case GTPU_EH_V4_FLOW:
1005 case GTPU_EH_V6_FLOW:
1006 case GTPU_UL_V4_FLOW:
1007 case GTPU_UL_V6_FLOW:
1008 case GTPU_DL_V4_FLOW:
1009 case GTPU_DL_V6_FLOW:
1010 return true;
1011 }
1012
1013 return false;
1014 }
1015
1016 /* When adding a new type, update the assert and, if it's hashable, add it to
1017 * the flow_type_hashable switch case.
1018 */
1019 static_assert(GTPU_DL_V6_FLOW + 1 == __FLOW_TYPE_COUNT);
1020
ethtool_check_xfrm_rxfh(u32 input_xfrm,u64 rxfh)1021 static int ethtool_check_xfrm_rxfh(u32 input_xfrm, u64 rxfh)
1022 {
1023 /* Sanity check: if symmetric-xor/symmetric-or-xor is set, then:
1024 * 1 - no other fields besides IP src/dst and/or L4 src/dst are set
1025 * 2 - If src is set, dst must also be set
1026 */
1027 if ((input_xfrm != RXH_XFRM_NO_CHANGE &&
1028 input_xfrm & (RXH_XFRM_SYM_XOR | RXH_XFRM_SYM_OR_XOR)) &&
1029 ((rxfh & ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
1030 (!!(rxfh & RXH_IP_SRC) ^ !!(rxfh & RXH_IP_DST)) ||
1031 (!!(rxfh & RXH_L4_B_0_1) ^ !!(rxfh & RXH_L4_B_2_3))))
1032 return -EINVAL;
1033
1034 return 0;
1035 }
1036
ethtool_check_flow_types(struct net_device * dev,u32 input_xfrm)1037 static int ethtool_check_flow_types(struct net_device *dev, u32 input_xfrm)
1038 {
1039 const struct ethtool_ops *ops = dev->ethtool_ops;
1040 struct ethtool_rxnfc info = {
1041 .cmd = ETHTOOL_GRXFH,
1042 };
1043 int err;
1044 u32 i;
1045
1046 for (i = 0; i < __FLOW_TYPE_COUNT; i++) {
1047 if (!flow_type_hashable(i))
1048 continue;
1049
1050 info.flow_type = i;
1051 err = ops->get_rxnfc(dev, &info, NULL);
1052 if (err)
1053 continue;
1054
1055 err = ethtool_check_xfrm_rxfh(input_xfrm, info.data);
1056 if (err)
1057 return err;
1058 }
1059
1060 return 0;
1061 }
1062
ethtool_set_rxnfc(struct net_device * dev,u32 cmd,void __user * useraddr)1063 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
1064 u32 cmd, void __user *useraddr)
1065 {
1066 const struct ethtool_ops *ops = dev->ethtool_ops;
1067 struct ethtool_rxnfc info;
1068 size_t info_size = sizeof(info);
1069 int rc;
1070
1071 if (!ops->set_rxnfc)
1072 return -EOPNOTSUPP;
1073
1074 rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1075 if (rc)
1076 return rc;
1077
1078 if (cmd == ETHTOOL_SRXCLSRLINS && info.fs.flow_type & FLOW_RSS) {
1079 /* Nonzero ring with RSS only makes sense
1080 * if NIC adds them together
1081 */
1082 if (!ops->cap_rss_rxnfc_adds &&
1083 ethtool_get_flow_spec_ring(info.fs.ring_cookie))
1084 return -EINVAL;
1085
1086 if (info.rss_context &&
1087 !xa_load(&dev->ethtool->rss_ctx, info.rss_context))
1088 return -EINVAL;
1089 }
1090
1091 if (cmd == ETHTOOL_SRXFH && ops->get_rxfh) {
1092 struct ethtool_rxfh_param rxfh = {};
1093
1094 rc = ops->get_rxfh(dev, &rxfh);
1095 if (rc)
1096 return rc;
1097
1098 rc = ethtool_check_xfrm_rxfh(rxfh.input_xfrm, info.data);
1099 if (rc)
1100 return rc;
1101 }
1102
1103 rc = ops->set_rxnfc(dev, &info);
1104 if (rc)
1105 return rc;
1106
1107 if (cmd == ETHTOOL_SRXCLSRLINS &&
1108 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
1109 return -EFAULT;
1110
1111 return 0;
1112 }
1113
ethtool_get_rxnfc(struct net_device * dev,u32 cmd,void __user * useraddr)1114 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1115 u32 cmd, void __user *useraddr)
1116 {
1117 struct ethtool_rxnfc info;
1118 size_t info_size = sizeof(info);
1119 const struct ethtool_ops *ops = dev->ethtool_ops;
1120 int ret;
1121 void *rule_buf = NULL;
1122
1123 if (!ops->get_rxnfc)
1124 return -EOPNOTSUPP;
1125
1126 ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1127 if (ret)
1128 return ret;
1129
1130 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1131 if (info.rule_cnt > 0) {
1132 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1133 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1134 GFP_USER);
1135 if (!rule_buf)
1136 return -ENOMEM;
1137 }
1138 }
1139
1140 ret = ops->get_rxnfc(dev, &info, rule_buf);
1141 if (ret < 0)
1142 goto err_out;
1143
1144 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1145 err_out:
1146 kfree(rule_buf);
1147
1148 return ret;
1149 }
1150
ethtool_copy_validate_indir(u32 * indir,void __user * useraddr,struct ethtool_rxnfc * rx_rings,u32 size)1151 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1152 struct ethtool_rxnfc *rx_rings,
1153 u32 size)
1154 {
1155 int i;
1156
1157 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1158 return -EFAULT;
1159
1160 /* Validate ring indices */
1161 for (i = 0; i < size; i++)
1162 if (indir[i] >= rx_rings->data)
1163 return -EINVAL;
1164
1165 return 0;
1166 }
1167
1168 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1169
netdev_rss_key_fill(void * buffer,size_t len)1170 void netdev_rss_key_fill(void *buffer, size_t len)
1171 {
1172 BUG_ON(len > sizeof(netdev_rss_key));
1173 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1174 memcpy(buffer, netdev_rss_key, len);
1175 }
1176 EXPORT_SYMBOL(netdev_rss_key_fill);
1177
ethtool_get_rxfh_indir(struct net_device * dev,void __user * useraddr)1178 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1179 void __user *useraddr)
1180 {
1181 struct ethtool_rxfh_param rxfh = {};
1182 u32 user_size;
1183 int ret;
1184
1185 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1186 !dev->ethtool_ops->get_rxfh)
1187 return -EOPNOTSUPP;
1188 rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1189 if (rxfh.indir_size == 0)
1190 return -EOPNOTSUPP;
1191
1192 if (copy_from_user(&user_size,
1193 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1194 sizeof(user_size)))
1195 return -EFAULT;
1196
1197 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1198 &rxfh.indir_size, sizeof(rxfh.indir_size)))
1199 return -EFAULT;
1200
1201 /* If the user buffer size is 0, this is just a query for the
1202 * device table size. Otherwise, if it's smaller than the
1203 * device table size it's an error.
1204 */
1205 if (user_size < rxfh.indir_size)
1206 return user_size == 0 ? 0 : -EINVAL;
1207
1208 rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
1209 if (!rxfh.indir)
1210 return -ENOMEM;
1211
1212 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
1213 if (ret)
1214 goto out;
1215 if (copy_to_user(useraddr +
1216 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1217 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
1218 ret = -EFAULT;
1219
1220 out:
1221 kfree(rxfh.indir);
1222 return ret;
1223 }
1224
ethtool_set_rxfh_indir(struct net_device * dev,void __user * useraddr)1225 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1226 void __user *useraddr)
1227 {
1228 const struct ethtool_ops *ops = dev->ethtool_ops;
1229 struct ethtool_rxfh_param rxfh_dev = {};
1230 struct netlink_ext_ack *extack = NULL;
1231 struct ethtool_rxnfc rx_rings;
1232 u32 user_size, i;
1233 int ret;
1234 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1235
1236 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1237 !ops->get_rxnfc)
1238 return -EOPNOTSUPP;
1239
1240 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1241 if (rxfh_dev.indir_size == 0)
1242 return -EOPNOTSUPP;
1243
1244 if (copy_from_user(&user_size,
1245 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1246 sizeof(user_size)))
1247 return -EFAULT;
1248
1249 if (user_size != 0 && user_size != rxfh_dev.indir_size)
1250 return -EINVAL;
1251
1252 rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
1253 sizeof(rxfh_dev.indir[0]), GFP_USER);
1254 if (!rxfh_dev.indir)
1255 return -ENOMEM;
1256
1257 rx_rings.cmd = ETHTOOL_GRXRINGS;
1258 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1259 if (ret)
1260 goto out;
1261
1262 if (user_size == 0) {
1263 u32 *indir = rxfh_dev.indir;
1264
1265 for (i = 0; i < rxfh_dev.indir_size; i++)
1266 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1267 } else {
1268 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1269 useraddr + ringidx_offset,
1270 &rx_rings,
1271 rxfh_dev.indir_size);
1272 if (ret)
1273 goto out;
1274 }
1275
1276 rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
1277 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1278 if (ret)
1279 goto out;
1280
1281 /* indicate whether rxfh was set to default */
1282 if (user_size == 0)
1283 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1284 else
1285 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1286
1287 out:
1288 kfree(rxfh_dev.indir);
1289 return ret;
1290 }
1291
ethtool_get_rxfh(struct net_device * dev,void __user * useraddr)1292 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1293 void __user *useraddr)
1294 {
1295 const struct ethtool_ops *ops = dev->ethtool_ops;
1296 struct ethtool_rxfh_param rxfh_dev = {};
1297 u32 user_indir_size, user_key_size;
1298 struct ethtool_rxfh_context *ctx;
1299 struct ethtool_rxfh rxfh;
1300 u32 indir_bytes;
1301 u8 *rss_config;
1302 u32 total_size;
1303 int ret;
1304
1305 if (!ops->get_rxfh)
1306 return -EOPNOTSUPP;
1307
1308 if (ops->get_rxfh_indir_size)
1309 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1310 if (ops->get_rxfh_key_size)
1311 rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
1312
1313 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1314 return -EFAULT;
1315 user_indir_size = rxfh.indir_size;
1316 user_key_size = rxfh.key_size;
1317
1318 /* Check that reserved fields are 0 for now */
1319 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1320 return -EINVAL;
1321 /* Most drivers don't handle rss_context, check it's 0 as well */
1322 if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
1323 ops->create_rxfh_context))
1324 return -EOPNOTSUPP;
1325
1326 rxfh.indir_size = rxfh_dev.indir_size;
1327 rxfh.key_size = rxfh_dev.key_size;
1328 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1329 return -EFAULT;
1330
1331 if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
1332 (user_key_size && user_key_size != rxfh_dev.key_size))
1333 return -EINVAL;
1334
1335 indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
1336 total_size = indir_bytes + user_key_size;
1337 rss_config = kzalloc(total_size, GFP_USER);
1338 if (!rss_config)
1339 return -ENOMEM;
1340
1341 if (user_indir_size)
1342 rxfh_dev.indir = (u32 *)rss_config;
1343
1344 if (user_key_size)
1345 rxfh_dev.key = rss_config + indir_bytes;
1346
1347 if (rxfh.rss_context) {
1348 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
1349 if (!ctx) {
1350 ret = -ENOENT;
1351 goto out;
1352 }
1353 if (rxfh_dev.indir)
1354 memcpy(rxfh_dev.indir, ethtool_rxfh_context_indir(ctx),
1355 indir_bytes);
1356 if (!ops->rxfh_per_ctx_key) {
1357 rxfh_dev.key_size = 0;
1358 } else {
1359 if (rxfh_dev.key)
1360 memcpy(rxfh_dev.key,
1361 ethtool_rxfh_context_key(ctx),
1362 user_key_size);
1363 rxfh_dev.hfunc = ctx->hfunc;
1364 }
1365 rxfh_dev.input_xfrm = ctx->input_xfrm;
1366 ret = 0;
1367 } else {
1368 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
1369 if (ret)
1370 goto out;
1371 }
1372
1373 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1374 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
1375 ret = -EFAULT;
1376 } else if (copy_to_user(useraddr +
1377 offsetof(struct ethtool_rxfh, input_xfrm),
1378 &rxfh_dev.input_xfrm,
1379 sizeof(rxfh.input_xfrm))) {
1380 ret = -EFAULT;
1381 } else if (copy_to_user(useraddr +
1382 offsetof(struct ethtool_rxfh, key_size),
1383 &rxfh_dev.key_size,
1384 sizeof(rxfh.key_size))) {
1385 ret = -EFAULT;
1386 } else if (copy_to_user(useraddr +
1387 offsetof(struct ethtool_rxfh, rss_config[0]),
1388 rss_config, total_size)) {
1389 ret = -EFAULT;
1390 }
1391 out:
1392 kfree(rss_config);
1393
1394 return ret;
1395 }
1396
1397 static struct ethtool_rxfh_context *
ethtool_rxfh_ctx_alloc(const struct ethtool_ops * ops,u32 indir_size,u32 key_size)1398 ethtool_rxfh_ctx_alloc(const struct ethtool_ops *ops,
1399 u32 indir_size, u32 key_size)
1400 {
1401 size_t indir_bytes, flex_len, key_off, size;
1402 struct ethtool_rxfh_context *ctx;
1403 u32 priv_bytes, indir_max;
1404 u16 key_max;
1405
1406 key_max = max(key_size, ops->rxfh_key_space);
1407 indir_max = max(indir_size, ops->rxfh_indir_space);
1408
1409 priv_bytes = ALIGN(ops->rxfh_priv_size, sizeof(u32));
1410 indir_bytes = array_size(indir_max, sizeof(u32));
1411
1412 key_off = size_add(priv_bytes, indir_bytes);
1413 flex_len = size_add(key_off, key_max);
1414 size = struct_size_t(struct ethtool_rxfh_context, data, flex_len);
1415
1416 ctx = kzalloc(size, GFP_KERNEL_ACCOUNT);
1417 if (!ctx)
1418 return NULL;
1419
1420 ctx->indir_size = indir_size;
1421 ctx->key_size = key_size;
1422 ctx->key_off = key_off;
1423 ctx->priv_size = ops->rxfh_priv_size;
1424
1425 ctx->hfunc = ETH_RSS_HASH_NO_CHANGE;
1426 ctx->input_xfrm = RXH_XFRM_NO_CHANGE;
1427
1428 return ctx;
1429 }
1430
ethtool_set_rxfh(struct net_device * dev,void __user * useraddr)1431 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1432 void __user *useraddr)
1433 {
1434 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1435 const struct ethtool_ops *ops = dev->ethtool_ops;
1436 u32 dev_indir_size = 0, dev_key_size = 0, i;
1437 u32 user_indir_len = 0, indir_bytes = 0;
1438 struct ethtool_rxfh_param rxfh_dev = {};
1439 struct ethtool_rxfh_context *ctx = NULL;
1440 struct netlink_ext_ack *extack = NULL;
1441 struct ethtool_rxnfc rx_rings;
1442 struct ethtool_rxfh rxfh;
1443 bool locked = false; /* dev->ethtool->rss_lock taken */
1444 bool create = false;
1445 u8 *rss_config;
1446 int ret;
1447
1448 if (!ops->get_rxnfc || !ops->set_rxfh)
1449 return -EOPNOTSUPP;
1450
1451 if (ops->get_rxfh_indir_size)
1452 dev_indir_size = ops->get_rxfh_indir_size(dev);
1453 if (ops->get_rxfh_key_size)
1454 dev_key_size = ops->get_rxfh_key_size(dev);
1455
1456 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1457 return -EFAULT;
1458
1459 /* Check that reserved fields are 0 for now */
1460 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1461 return -EINVAL;
1462 /* Most drivers don't handle rss_context, check it's 0 as well */
1463 if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
1464 ops->create_rxfh_context))
1465 return -EOPNOTSUPP;
1466 /* Check input data transformation capabilities */
1467 if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
1468 rxfh.input_xfrm != RXH_XFRM_SYM_OR_XOR &&
1469 rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
1470 return -EINVAL;
1471 if (rxfh.input_xfrm != RXH_XFRM_NO_CHANGE &&
1472 rxfh.input_xfrm & ~ops->supported_input_xfrm)
1473 return -EOPNOTSUPP;
1474 create = rxfh.rss_context == ETH_RXFH_CONTEXT_ALLOC;
1475
1476 if ((rxfh.indir_size &&
1477 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1478 rxfh.indir_size != dev_indir_size) ||
1479 (rxfh.key_size && rxfh.key_size != dev_key_size))
1480 return -EINVAL;
1481
1482 /* Must request at least one change: indir size, hash key, function
1483 * or input transformation.
1484 * There's no need for any of it in case of context creation.
1485 */
1486 if (!create &&
1487 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1488 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
1489 rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
1490 return -EINVAL;
1491
1492 ret = ethtool_check_flow_types(dev, rxfh.input_xfrm);
1493 if (ret)
1494 return ret;
1495
1496 indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
1497
1498 /* Check settings which may be global rather than per RSS-context */
1499 if (rxfh.rss_context && !ops->rxfh_per_ctx_key)
1500 if (rxfh.key_size ||
1501 (rxfh.hfunc && rxfh.hfunc != ETH_RSS_HASH_NO_CHANGE) ||
1502 (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_NO_CHANGE))
1503 return -EOPNOTSUPP;
1504
1505 rss_config = kzalloc(indir_bytes + dev_key_size, GFP_USER);
1506 if (!rss_config)
1507 return -ENOMEM;
1508
1509 rx_rings.cmd = ETHTOOL_GRXRINGS;
1510 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1511 if (ret)
1512 goto out;
1513
1514 /* rxfh.indir_size == 0 means reset the indir table to default (master
1515 * context) or delete the context (other RSS contexts).
1516 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1517 */
1518 if (rxfh.indir_size &&
1519 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1520 user_indir_len = indir_bytes;
1521 rxfh_dev.indir = (u32 *)rss_config;
1522 rxfh_dev.indir_size = dev_indir_size;
1523 ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1524 useraddr + rss_cfg_offset,
1525 &rx_rings,
1526 rxfh.indir_size);
1527 if (ret)
1528 goto out;
1529 } else if (rxfh.indir_size == 0) {
1530 if (rxfh.rss_context == 0) {
1531 u32 *indir;
1532
1533 rxfh_dev.indir = (u32 *)rss_config;
1534 rxfh_dev.indir_size = dev_indir_size;
1535 indir = rxfh_dev.indir;
1536 for (i = 0; i < dev_indir_size; i++)
1537 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1538 } else {
1539 rxfh_dev.rss_delete = true;
1540 }
1541 }
1542
1543 if (rxfh.key_size) {
1544 rxfh_dev.key_size = dev_key_size;
1545 rxfh_dev.key = rss_config + indir_bytes;
1546 if (copy_from_user(rxfh_dev.key,
1547 useraddr + rss_cfg_offset + user_indir_len,
1548 rxfh.key_size)) {
1549 ret = -EFAULT;
1550 goto out;
1551 }
1552 }
1553
1554 if (rxfh.rss_context) {
1555 mutex_lock(&dev->ethtool->rss_lock);
1556 locked = true;
1557 }
1558
1559 if (rxfh.rss_context && rxfh_dev.rss_delete) {
1560 ret = ethtool_check_rss_ctx_busy(dev, rxfh.rss_context);
1561 if (ret)
1562 goto out;
1563 }
1564
1565 if (create) {
1566 if (rxfh_dev.rss_delete) {
1567 ret = -EINVAL;
1568 goto out;
1569 }
1570 ctx = ethtool_rxfh_ctx_alloc(ops, dev_indir_size, dev_key_size);
1571 if (!ctx) {
1572 ret = -ENOMEM;
1573 goto out;
1574 }
1575
1576 if (ops->create_rxfh_context) {
1577 u32 limit = ops->rxfh_max_num_contexts ?: U32_MAX;
1578 u32 ctx_id;
1579
1580 /* driver uses new API, core allocates ID */
1581 ret = xa_alloc(&dev->ethtool->rss_ctx, &ctx_id, ctx,
1582 XA_LIMIT(1, limit - 1),
1583 GFP_KERNEL_ACCOUNT);
1584 if (ret < 0) {
1585 kfree(ctx);
1586 goto out;
1587 }
1588 WARN_ON(!ctx_id); /* can't happen */
1589 rxfh.rss_context = ctx_id;
1590 }
1591 } else if (rxfh.rss_context) {
1592 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
1593 if (!ctx) {
1594 ret = -ENOENT;
1595 goto out;
1596 }
1597 }
1598 rxfh_dev.hfunc = rxfh.hfunc;
1599 rxfh_dev.rss_context = rxfh.rss_context;
1600 rxfh_dev.input_xfrm = rxfh.input_xfrm;
1601
1602 if (rxfh.rss_context && ops->create_rxfh_context) {
1603 if (create) {
1604 ret = ops->create_rxfh_context(dev, ctx, &rxfh_dev,
1605 extack);
1606 /* Make sure driver populates defaults */
1607 WARN_ON_ONCE(!ret && !rxfh_dev.key &&
1608 ops->rxfh_per_ctx_key &&
1609 !memchr_inv(ethtool_rxfh_context_key(ctx),
1610 0, ctx->key_size));
1611 } else if (rxfh_dev.rss_delete) {
1612 ret = ops->remove_rxfh_context(dev, ctx,
1613 rxfh.rss_context,
1614 extack);
1615 } else {
1616 ret = ops->modify_rxfh_context(dev, ctx, &rxfh_dev,
1617 extack);
1618 }
1619 } else {
1620 ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1621 }
1622 if (ret) {
1623 if (create) {
1624 /* failed to create, free our new tracking entry */
1625 if (ops->create_rxfh_context)
1626 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
1627 kfree(ctx);
1628 }
1629 goto out;
1630 }
1631
1632 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1633 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
1634 ret = -EFAULT;
1635
1636 if (!rxfh_dev.rss_context) {
1637 /* indicate whether rxfh was set to default */
1638 if (rxfh.indir_size == 0)
1639 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1640 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1641 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1642 }
1643 /* Update rss_ctx tracking */
1644 if (create && !ops->create_rxfh_context) {
1645 /* driver uses old API, it chose context ID */
1646 if (WARN_ON(xa_load(&dev->ethtool->rss_ctx, rxfh_dev.rss_context))) {
1647 /* context ID reused, our tracking is screwed */
1648 kfree(ctx);
1649 goto out;
1650 }
1651 /* Allocate the exact ID the driver gave us */
1652 if (xa_is_err(xa_store(&dev->ethtool->rss_ctx, rxfh_dev.rss_context,
1653 ctx, GFP_KERNEL))) {
1654 kfree(ctx);
1655 goto out;
1656 }
1657
1658 /* Fetch the defaults for the old API, in the new API drivers
1659 * should write defaults into ctx themselves.
1660 */
1661 rxfh_dev.indir = (u32 *)rss_config;
1662 rxfh_dev.indir_size = dev_indir_size;
1663
1664 rxfh_dev.key = rss_config + indir_bytes;
1665 rxfh_dev.key_size = dev_key_size;
1666
1667 ret = ops->get_rxfh(dev, &rxfh_dev);
1668 if (WARN_ON(ret)) {
1669 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
1670 kfree(ctx);
1671 goto out;
1672 }
1673 }
1674 if (rxfh_dev.rss_delete) {
1675 WARN_ON(xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context) != ctx);
1676 kfree(ctx);
1677 } else if (ctx) {
1678 if (rxfh_dev.indir) {
1679 for (i = 0; i < dev_indir_size; i++)
1680 ethtool_rxfh_context_indir(ctx)[i] = rxfh_dev.indir[i];
1681 ctx->indir_configured =
1682 rxfh.indir_size &&
1683 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE;
1684 }
1685 if (rxfh_dev.key) {
1686 memcpy(ethtool_rxfh_context_key(ctx), rxfh_dev.key,
1687 dev_key_size);
1688 ctx->key_configured = !!rxfh.key_size;
1689 }
1690 if (rxfh_dev.hfunc != ETH_RSS_HASH_NO_CHANGE)
1691 ctx->hfunc = rxfh_dev.hfunc;
1692 if (rxfh_dev.input_xfrm != RXH_XFRM_NO_CHANGE)
1693 ctx->input_xfrm = rxfh_dev.input_xfrm;
1694 }
1695
1696 out:
1697 if (locked)
1698 mutex_unlock(&dev->ethtool->rss_lock);
1699 kfree(rss_config);
1700 return ret;
1701 }
1702
ethtool_get_regs(struct net_device * dev,char __user * useraddr)1703 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1704 {
1705 struct ethtool_regs regs;
1706 const struct ethtool_ops *ops = dev->ethtool_ops;
1707 void *regbuf;
1708 int reglen, ret;
1709
1710 if (!ops->get_regs || !ops->get_regs_len)
1711 return -EOPNOTSUPP;
1712
1713 if (copy_from_user(®s, useraddr, sizeof(regs)))
1714 return -EFAULT;
1715
1716 reglen = ops->get_regs_len(dev);
1717 if (reglen <= 0)
1718 return reglen;
1719
1720 if (regs.len > reglen)
1721 regs.len = reglen;
1722
1723 regbuf = vzalloc(reglen);
1724 if (!regbuf)
1725 return -ENOMEM;
1726
1727 if (regs.len < reglen)
1728 reglen = regs.len;
1729
1730 ops->get_regs(dev, ®s, regbuf);
1731
1732 ret = -EFAULT;
1733 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1734 goto out;
1735 useraddr += offsetof(struct ethtool_regs, data);
1736 if (copy_to_user(useraddr, regbuf, reglen))
1737 goto out;
1738 ret = 0;
1739
1740 out:
1741 vfree(regbuf);
1742 return ret;
1743 }
1744
ethtool_reset(struct net_device * dev,char __user * useraddr)1745 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1746 {
1747 struct ethtool_value reset;
1748 int ret;
1749
1750 if (!dev->ethtool_ops->reset)
1751 return -EOPNOTSUPP;
1752
1753 if (dev->ethtool->module_fw_flash_in_progress)
1754 return -EBUSY;
1755
1756 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1757 return -EFAULT;
1758
1759 ret = dev->ethtool_ops->reset(dev, &reset.data);
1760 if (ret)
1761 return ret;
1762
1763 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1764 return -EFAULT;
1765 return 0;
1766 }
1767
ethtool_get_wol(struct net_device * dev,char __user * useraddr)1768 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1769 {
1770 struct ethtool_wolinfo wol;
1771
1772 if (!dev->ethtool_ops->get_wol)
1773 return -EOPNOTSUPP;
1774
1775 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1776 wol.cmd = ETHTOOL_GWOL;
1777 dev->ethtool_ops->get_wol(dev, &wol);
1778
1779 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1780 return -EFAULT;
1781 return 0;
1782 }
1783
ethtool_set_wol(struct net_device * dev,char __user * useraddr)1784 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1785 {
1786 struct ethtool_wolinfo wol, cur_wol;
1787 int ret;
1788
1789 if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
1790 return -EOPNOTSUPP;
1791
1792 memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
1793 cur_wol.cmd = ETHTOOL_GWOL;
1794 dev->ethtool_ops->get_wol(dev, &cur_wol);
1795
1796 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1797 return -EFAULT;
1798
1799 if (wol.wolopts & ~cur_wol.supported)
1800 return -EINVAL;
1801
1802 if (wol.wolopts == cur_wol.wolopts &&
1803 !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
1804 return 0;
1805
1806 ret = dev->ethtool_ops->set_wol(dev, &wol);
1807 if (ret)
1808 return ret;
1809
1810 dev->ethtool->wol_enabled = !!wol.wolopts;
1811 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1812
1813 return 0;
1814 }
1815
eee_to_keee(struct ethtool_keee * keee,const struct ethtool_eee * eee)1816 static void eee_to_keee(struct ethtool_keee *keee,
1817 const struct ethtool_eee *eee)
1818 {
1819 memset(keee, 0, sizeof(*keee));
1820
1821 keee->eee_enabled = eee->eee_enabled;
1822 keee->tx_lpi_enabled = eee->tx_lpi_enabled;
1823 keee->tx_lpi_timer = eee->tx_lpi_timer;
1824
1825 ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
1826 eee->advertised);
1827 }
1828
keee_to_eee(struct ethtool_eee * eee,const struct ethtool_keee * keee)1829 static void keee_to_eee(struct ethtool_eee *eee,
1830 const struct ethtool_keee *keee)
1831 {
1832 bool overflow;
1833
1834 memset(eee, 0, sizeof(*eee));
1835
1836 eee->eee_active = keee->eee_active;
1837 eee->eee_enabled = keee->eee_enabled;
1838 eee->tx_lpi_enabled = keee->tx_lpi_enabled;
1839 eee->tx_lpi_timer = keee->tx_lpi_timer;
1840
1841 overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
1842 keee->supported);
1843 ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
1844 keee->advertised);
1845 ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
1846 keee->lp_advertised);
1847 if (overflow)
1848 pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
1849 }
1850
ethtool_get_eee(struct net_device * dev,char __user * useraddr)1851 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1852 {
1853 struct ethtool_keee keee;
1854 struct ethtool_eee eee;
1855 int rc;
1856
1857 if (!dev->ethtool_ops->get_eee)
1858 return -EOPNOTSUPP;
1859
1860 memset(&keee, 0, sizeof(keee));
1861 rc = dev->ethtool_ops->get_eee(dev, &keee);
1862 if (rc)
1863 return rc;
1864
1865 keee_to_eee(&eee, &keee);
1866 if (copy_to_user(useraddr, &eee, sizeof(eee)))
1867 return -EFAULT;
1868
1869 return 0;
1870 }
1871
ethtool_set_eee(struct net_device * dev,char __user * useraddr)1872 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1873 {
1874 struct ethtool_keee keee;
1875 struct ethtool_eee eee;
1876 int ret;
1877
1878 if (!dev->ethtool_ops->set_eee)
1879 return -EOPNOTSUPP;
1880
1881 if (copy_from_user(&eee, useraddr, sizeof(eee)))
1882 return -EFAULT;
1883
1884 eee_to_keee(&keee, &eee);
1885 ret = dev->ethtool_ops->set_eee(dev, &keee);
1886 if (!ret)
1887 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1888 return ret;
1889 }
1890
ethtool_nway_reset(struct net_device * dev)1891 static int ethtool_nway_reset(struct net_device *dev)
1892 {
1893 if (!dev->ethtool_ops->nway_reset)
1894 return -EOPNOTSUPP;
1895
1896 return dev->ethtool_ops->nway_reset(dev);
1897 }
1898
ethtool_get_link(struct net_device * dev,char __user * useraddr)1899 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1900 {
1901 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1902 int link = __ethtool_get_link(dev);
1903
1904 if (link < 0)
1905 return link;
1906
1907 edata.data = link;
1908 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1909 return -EFAULT;
1910 return 0;
1911 }
1912
ethtool_get_any_eeprom(struct net_device * dev,void __user * useraddr,int (* getter)(struct net_device *,struct ethtool_eeprom *,u8 *),u32 total_len)1913 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1914 int (*getter)(struct net_device *,
1915 struct ethtool_eeprom *, u8 *),
1916 u32 total_len)
1917 {
1918 struct ethtool_eeprom eeprom;
1919 void __user *userbuf = useraddr + sizeof(eeprom);
1920 u32 bytes_remaining;
1921 u8 *data;
1922 int ret = 0;
1923
1924 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1925 return -EFAULT;
1926
1927 /* Check for wrap and zero */
1928 if (eeprom.offset + eeprom.len <= eeprom.offset)
1929 return -EINVAL;
1930
1931 /* Check for exceeding total eeprom len */
1932 if (eeprom.offset + eeprom.len > total_len)
1933 return -EINVAL;
1934
1935 data = kzalloc(PAGE_SIZE, GFP_USER);
1936 if (!data)
1937 return -ENOMEM;
1938
1939 bytes_remaining = eeprom.len;
1940 while (bytes_remaining > 0) {
1941 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1942
1943 ret = getter(dev, &eeprom, data);
1944 if (ret)
1945 break;
1946 if (!eeprom.len) {
1947 ret = -EIO;
1948 break;
1949 }
1950 if (copy_to_user(userbuf, data, eeprom.len)) {
1951 ret = -EFAULT;
1952 break;
1953 }
1954 userbuf += eeprom.len;
1955 eeprom.offset += eeprom.len;
1956 bytes_remaining -= eeprom.len;
1957 }
1958
1959 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1960 eeprom.offset -= eeprom.len;
1961 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1962 ret = -EFAULT;
1963
1964 kfree(data);
1965 return ret;
1966 }
1967
ethtool_get_eeprom(struct net_device * dev,void __user * useraddr)1968 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1969 {
1970 const struct ethtool_ops *ops = dev->ethtool_ops;
1971
1972 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1973 !ops->get_eeprom_len(dev))
1974 return -EOPNOTSUPP;
1975
1976 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1977 ops->get_eeprom_len(dev));
1978 }
1979
ethtool_set_eeprom(struct net_device * dev,void __user * useraddr)1980 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1981 {
1982 struct ethtool_eeprom eeprom;
1983 const struct ethtool_ops *ops = dev->ethtool_ops;
1984 void __user *userbuf = useraddr + sizeof(eeprom);
1985 u32 bytes_remaining;
1986 u8 *data;
1987 int ret = 0;
1988
1989 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1990 !ops->get_eeprom_len(dev))
1991 return -EOPNOTSUPP;
1992
1993 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1994 return -EFAULT;
1995
1996 /* Check for wrap and zero */
1997 if (eeprom.offset + eeprom.len <= eeprom.offset)
1998 return -EINVAL;
1999
2000 /* Check for exceeding total eeprom len */
2001 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
2002 return -EINVAL;
2003
2004 data = kzalloc(PAGE_SIZE, GFP_USER);
2005 if (!data)
2006 return -ENOMEM;
2007
2008 bytes_remaining = eeprom.len;
2009 while (bytes_remaining > 0) {
2010 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
2011
2012 if (copy_from_user(data, userbuf, eeprom.len)) {
2013 ret = -EFAULT;
2014 break;
2015 }
2016 ret = ops->set_eeprom(dev, &eeprom, data);
2017 if (ret)
2018 break;
2019 userbuf += eeprom.len;
2020 eeprom.offset += eeprom.len;
2021 bytes_remaining -= eeprom.len;
2022 }
2023
2024 kfree(data);
2025 return ret;
2026 }
2027
ethtool_get_coalesce(struct net_device * dev,void __user * useraddr)2028 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
2029 void __user *useraddr)
2030 {
2031 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2032 struct kernel_ethtool_coalesce kernel_coalesce = {};
2033 int ret;
2034
2035 if (!dev->ethtool_ops->get_coalesce)
2036 return -EOPNOTSUPP;
2037
2038 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
2039 NULL);
2040 if (ret)
2041 return ret;
2042
2043 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2044 return -EFAULT;
2045 return 0;
2046 }
2047
2048 static bool
ethtool_set_coalesce_supported(struct net_device * dev,struct ethtool_coalesce * coalesce)2049 ethtool_set_coalesce_supported(struct net_device *dev,
2050 struct ethtool_coalesce *coalesce)
2051 {
2052 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
2053 u32 nonzero_params = 0;
2054
2055 if (coalesce->rx_coalesce_usecs)
2056 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
2057 if (coalesce->rx_max_coalesced_frames)
2058 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
2059 if (coalesce->rx_coalesce_usecs_irq)
2060 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
2061 if (coalesce->rx_max_coalesced_frames_irq)
2062 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
2063 if (coalesce->tx_coalesce_usecs)
2064 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
2065 if (coalesce->tx_max_coalesced_frames)
2066 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
2067 if (coalesce->tx_coalesce_usecs_irq)
2068 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
2069 if (coalesce->tx_max_coalesced_frames_irq)
2070 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
2071 if (coalesce->stats_block_coalesce_usecs)
2072 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
2073 if (coalesce->use_adaptive_rx_coalesce)
2074 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
2075 if (coalesce->use_adaptive_tx_coalesce)
2076 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
2077 if (coalesce->pkt_rate_low)
2078 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
2079 if (coalesce->rx_coalesce_usecs_low)
2080 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
2081 if (coalesce->rx_max_coalesced_frames_low)
2082 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
2083 if (coalesce->tx_coalesce_usecs_low)
2084 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
2085 if (coalesce->tx_max_coalesced_frames_low)
2086 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
2087 if (coalesce->pkt_rate_high)
2088 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
2089 if (coalesce->rx_coalesce_usecs_high)
2090 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
2091 if (coalesce->rx_max_coalesced_frames_high)
2092 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
2093 if (coalesce->tx_coalesce_usecs_high)
2094 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
2095 if (coalesce->tx_max_coalesced_frames_high)
2096 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
2097 if (coalesce->rate_sample_interval)
2098 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
2099
2100 return (supported_params & nonzero_params) == nonzero_params;
2101 }
2102
ethtool_set_coalesce(struct net_device * dev,void __user * useraddr)2103 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
2104 void __user *useraddr)
2105 {
2106 struct kernel_ethtool_coalesce kernel_coalesce = {};
2107 struct ethtool_coalesce coalesce;
2108 int ret;
2109
2110 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
2111 return -EOPNOTSUPP;
2112
2113 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
2114 NULL);
2115 if (ret)
2116 return ret;
2117
2118 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
2119 return -EFAULT;
2120
2121 if (!ethtool_set_coalesce_supported(dev, &coalesce))
2122 return -EOPNOTSUPP;
2123
2124 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
2125 NULL);
2126 if (!ret)
2127 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
2128 return ret;
2129 }
2130
ethtool_get_ringparam(struct net_device * dev,void __user * useraddr)2131 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
2132 {
2133 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
2134 struct kernel_ethtool_ringparam kernel_ringparam = {};
2135
2136 if (!dev->ethtool_ops->get_ringparam)
2137 return -EOPNOTSUPP;
2138
2139 dev->ethtool_ops->get_ringparam(dev, &ringparam,
2140 &kernel_ringparam, NULL);
2141
2142 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
2143 return -EFAULT;
2144 return 0;
2145 }
2146
ethtool_set_ringparam(struct net_device * dev,void __user * useraddr)2147 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
2148 {
2149 struct kernel_ethtool_ringparam kernel_ringparam;
2150 struct ethtool_ringparam ringparam, max;
2151 int ret;
2152
2153 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
2154 return -EOPNOTSUPP;
2155
2156 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
2157 return -EFAULT;
2158
2159 ethtool_ringparam_get_cfg(dev, &max, &kernel_ringparam, NULL);
2160
2161 /* ensure new ring parameters are within the maximums */
2162 if (ringparam.rx_pending > max.rx_max_pending ||
2163 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
2164 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
2165 ringparam.tx_pending > max.tx_max_pending)
2166 return -EINVAL;
2167
2168 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
2169 &kernel_ringparam, NULL);
2170 if (!ret)
2171 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
2172 return ret;
2173 }
2174
ethtool_get_channels(struct net_device * dev,void __user * useraddr)2175 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
2176 void __user *useraddr)
2177 {
2178 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
2179
2180 if (!dev->ethtool_ops->get_channels)
2181 return -EOPNOTSUPP;
2182
2183 dev->ethtool_ops->get_channels(dev, &channels);
2184
2185 if (copy_to_user(useraddr, &channels, sizeof(channels)))
2186 return -EFAULT;
2187 return 0;
2188 }
2189
ethtool_set_channels(struct net_device * dev,void __user * useraddr)2190 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
2191 void __user *useraddr)
2192 {
2193 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
2194 u16 from_channel, to_channel;
2195 unsigned int i;
2196 int ret;
2197
2198 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
2199 return -EOPNOTSUPP;
2200
2201 if (copy_from_user(&channels, useraddr, sizeof(channels)))
2202 return -EFAULT;
2203
2204 dev->ethtool_ops->get_channels(dev, &curr);
2205
2206 if (channels.rx_count == curr.rx_count &&
2207 channels.tx_count == curr.tx_count &&
2208 channels.combined_count == curr.combined_count &&
2209 channels.other_count == curr.other_count)
2210 return 0;
2211
2212 /* ensure new counts are within the maximums */
2213 if (channels.rx_count > curr.max_rx ||
2214 channels.tx_count > curr.max_tx ||
2215 channels.combined_count > curr.max_combined ||
2216 channels.other_count > curr.max_other)
2217 return -EINVAL;
2218
2219 /* ensure there is at least one RX and one TX channel */
2220 if (!channels.combined_count &&
2221 (!channels.rx_count || !channels.tx_count))
2222 return -EINVAL;
2223
2224 ret = ethtool_check_max_channel(dev, channels, NULL);
2225 if (ret)
2226 return ret;
2227
2228 /* Disabling channels, query zero-copy AF_XDP sockets */
2229 from_channel = channels.combined_count +
2230 min(channels.rx_count, channels.tx_count);
2231 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
2232 for (i = from_channel; i < to_channel; i++)
2233 if (xsk_get_pool_from_qid(dev, i))
2234 return -EINVAL;
2235
2236 ret = dev->ethtool_ops->set_channels(dev, &channels);
2237 if (!ret)
2238 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
2239 return ret;
2240 }
2241
ethtool_get_pauseparam(struct net_device * dev,void __user * useraddr)2242 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
2243 {
2244 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
2245
2246 if (!dev->ethtool_ops->get_pauseparam)
2247 return -EOPNOTSUPP;
2248
2249 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
2250
2251 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
2252 return -EFAULT;
2253 return 0;
2254 }
2255
ethtool_set_pauseparam(struct net_device * dev,void __user * useraddr)2256 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
2257 {
2258 struct ethtool_pauseparam pauseparam;
2259 int ret;
2260
2261 if (!dev->ethtool_ops->set_pauseparam)
2262 return -EOPNOTSUPP;
2263
2264 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
2265 return -EFAULT;
2266
2267 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
2268 if (!ret)
2269 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
2270 return ret;
2271 }
2272
ethtool_self_test(struct net_device * dev,char __user * useraddr)2273 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
2274 {
2275 struct ethtool_test test;
2276 const struct ethtool_ops *ops = dev->ethtool_ops;
2277 u64 *data;
2278 int ret, test_len;
2279
2280 if (!ops->self_test || !ops->get_sset_count)
2281 return -EOPNOTSUPP;
2282
2283 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
2284 if (test_len < 0)
2285 return test_len;
2286 WARN_ON(test_len == 0);
2287
2288 if (copy_from_user(&test, useraddr, sizeof(test)))
2289 return -EFAULT;
2290
2291 test.len = test_len;
2292 data = kcalloc(test_len, sizeof(u64), GFP_USER);
2293 if (!data)
2294 return -ENOMEM;
2295
2296 netif_testing_on(dev);
2297 ops->self_test(dev, &test, data);
2298 netif_testing_off(dev);
2299
2300 ret = -EFAULT;
2301 if (copy_to_user(useraddr, &test, sizeof(test)))
2302 goto out;
2303 useraddr += sizeof(test);
2304 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
2305 goto out;
2306 ret = 0;
2307
2308 out:
2309 kfree(data);
2310 return ret;
2311 }
2312
ethtool_get_strings(struct net_device * dev,void __user * useraddr)2313 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
2314 {
2315 struct ethtool_gstrings gstrings;
2316 u8 *data;
2317 int ret;
2318
2319 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
2320 return -EFAULT;
2321
2322 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
2323 if (ret < 0)
2324 return ret;
2325 if (ret > S32_MAX / ETH_GSTRING_LEN)
2326 return -ENOMEM;
2327 WARN_ON_ONCE(!ret);
2328
2329 gstrings.len = ret;
2330
2331 if (gstrings.len) {
2332 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
2333 if (!data)
2334 return -ENOMEM;
2335
2336 __ethtool_get_strings(dev, gstrings.string_set, data);
2337 } else {
2338 data = NULL;
2339 }
2340
2341 ret = -EFAULT;
2342 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
2343 goto out;
2344 useraddr += sizeof(gstrings);
2345 if (gstrings.len &&
2346 copy_to_user(useraddr, data,
2347 array_size(gstrings.len, ETH_GSTRING_LEN)))
2348 goto out;
2349 ret = 0;
2350
2351 out:
2352 vfree(data);
2353 return ret;
2354 }
2355
ethtool_sprintf(u8 ** data,const char * fmt,...)2356 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
2357 {
2358 va_list args;
2359
2360 va_start(args, fmt);
2361 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
2362 va_end(args);
2363
2364 *data += ETH_GSTRING_LEN;
2365 }
2366 EXPORT_SYMBOL(ethtool_sprintf);
2367
ethtool_puts(u8 ** data,const char * str)2368 void ethtool_puts(u8 **data, const char *str)
2369 {
2370 strscpy(*data, str, ETH_GSTRING_LEN);
2371 *data += ETH_GSTRING_LEN;
2372 }
2373 EXPORT_SYMBOL(ethtool_puts);
2374
ethtool_phys_id(struct net_device * dev,void __user * useraddr)2375 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
2376 {
2377 struct ethtool_value id;
2378 static bool busy;
2379 const struct ethtool_ops *ops = dev->ethtool_ops;
2380 netdevice_tracker dev_tracker;
2381 int rc;
2382
2383 if (!ops->set_phys_id)
2384 return -EOPNOTSUPP;
2385
2386 if (busy)
2387 return -EBUSY;
2388
2389 if (copy_from_user(&id, useraddr, sizeof(id)))
2390 return -EFAULT;
2391
2392 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2393 if (rc < 0)
2394 return rc;
2395
2396 /* Drop the RTNL lock while waiting, but prevent reentry or
2397 * removal of the device.
2398 */
2399 busy = true;
2400 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2401 netdev_unlock_ops(dev);
2402 rtnl_unlock();
2403
2404 if (rc == 0) {
2405 /* Driver will handle this itself */
2406 schedule_timeout_interruptible(
2407 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2408 } else {
2409 /* Driver expects to be called at twice the frequency in rc */
2410 int n = rc * 2, interval = HZ / n;
2411 u64 count = mul_u32_u32(n, id.data);
2412 u64 i = 0;
2413
2414 do {
2415 rtnl_lock();
2416 netdev_lock_ops(dev);
2417 rc = ops->set_phys_id(dev,
2418 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2419 netdev_unlock_ops(dev);
2420 rtnl_unlock();
2421 if (rc)
2422 break;
2423 schedule_timeout_interruptible(interval);
2424 } while (!signal_pending(current) && (!id.data || i < count));
2425 }
2426
2427 rtnl_lock();
2428 netdev_lock_ops(dev);
2429 netdev_put(dev, &dev_tracker);
2430 busy = false;
2431
2432 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2433 return rc;
2434 }
2435
ethtool_get_stats(struct net_device * dev,void __user * useraddr)2436 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2437 {
2438 struct ethtool_stats stats;
2439 const struct ethtool_ops *ops = dev->ethtool_ops;
2440 u64 *data;
2441 int ret, n_stats;
2442
2443 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2444 return -EOPNOTSUPP;
2445
2446 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2447 if (n_stats < 0)
2448 return n_stats;
2449 if (n_stats > S32_MAX / sizeof(u64))
2450 return -ENOMEM;
2451 WARN_ON_ONCE(!n_stats);
2452 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2453 return -EFAULT;
2454
2455 stats.n_stats = n_stats;
2456
2457 if (n_stats) {
2458 data = vzalloc(array_size(n_stats, sizeof(u64)));
2459 if (!data)
2460 return -ENOMEM;
2461 ops->get_ethtool_stats(dev, &stats, data);
2462 } else {
2463 data = NULL;
2464 }
2465
2466 ret = -EFAULT;
2467 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2468 goto out;
2469 useraddr += sizeof(stats);
2470 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2471 goto out;
2472 ret = 0;
2473
2474 out:
2475 vfree(data);
2476 return ret;
2477 }
2478
ethtool_vzalloc_stats_array(int n_stats,u64 ** data)2479 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
2480 {
2481 if (n_stats < 0)
2482 return n_stats;
2483 if (n_stats > S32_MAX / sizeof(u64))
2484 return -ENOMEM;
2485 if (WARN_ON_ONCE(!n_stats))
2486 return -EOPNOTSUPP;
2487
2488 *data = vzalloc(array_size(n_stats, sizeof(u64)));
2489 if (!*data)
2490 return -ENOMEM;
2491
2492 return 0;
2493 }
2494
ethtool_get_phy_stats_phydev(struct phy_device * phydev,struct ethtool_stats * stats,u64 ** data)2495 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2496 struct ethtool_stats *stats,
2497 u64 **data)
2498 {
2499 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2500 int n_stats, ret;
2501
2502 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2503 return -EOPNOTSUPP;
2504
2505 n_stats = phy_ops->get_sset_count(phydev);
2506
2507 ret = ethtool_vzalloc_stats_array(n_stats, data);
2508 if (ret)
2509 return ret;
2510
2511 stats->n_stats = n_stats;
2512 return phy_ops->get_stats(phydev, stats, *data);
2513 }
2514
ethtool_get_phy_stats_ethtool(struct net_device * dev,struct ethtool_stats * stats,u64 ** data)2515 static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2516 struct ethtool_stats *stats,
2517 u64 **data)
2518 {
2519 const struct ethtool_ops *ops = dev->ethtool_ops;
2520 int n_stats, ret;
2521
2522 if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats)
2523 return -EOPNOTSUPP;
2524
2525 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2526
2527 ret = ethtool_vzalloc_stats_array(n_stats, data);
2528 if (ret)
2529 return ret;
2530
2531 stats->n_stats = n_stats;
2532 ops->get_ethtool_phy_stats(dev, stats, *data);
2533
2534 return 0;
2535 }
2536
ethtool_get_phy_stats(struct net_device * dev,void __user * useraddr)2537 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2538 {
2539 struct phy_device *phydev = dev->phydev;
2540 struct ethtool_stats stats;
2541 u64 *data = NULL;
2542 int ret = -EOPNOTSUPP;
2543
2544 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2545 return -EFAULT;
2546
2547 if (phydev)
2548 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
2549
2550 if (ret == -EOPNOTSUPP)
2551 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
2552
2553 if (ret)
2554 goto out;
2555
2556 if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2557 ret = -EFAULT;
2558 goto out;
2559 }
2560
2561 useraddr += sizeof(stats);
2562 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2563 ret = -EFAULT;
2564
2565 out:
2566 vfree(data);
2567 return ret;
2568 }
2569
ethtool_get_perm_addr(struct net_device * dev,void __user * useraddr)2570 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2571 {
2572 struct ethtool_perm_addr epaddr;
2573
2574 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2575 return -EFAULT;
2576
2577 if (epaddr.size < dev->addr_len)
2578 return -ETOOSMALL;
2579 epaddr.size = dev->addr_len;
2580
2581 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2582 return -EFAULT;
2583 useraddr += sizeof(epaddr);
2584 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2585 return -EFAULT;
2586 return 0;
2587 }
2588
ethtool_get_value(struct net_device * dev,char __user * useraddr,u32 cmd,u32 (* actor)(struct net_device *))2589 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2590 u32 cmd, u32 (*actor)(struct net_device *))
2591 {
2592 struct ethtool_value edata = { .cmd = cmd };
2593
2594 if (!actor)
2595 return -EOPNOTSUPP;
2596
2597 edata.data = actor(dev);
2598
2599 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2600 return -EFAULT;
2601 return 0;
2602 }
2603
ethtool_set_value_void(struct net_device * dev,char __user * useraddr,void (* actor)(struct net_device *,u32))2604 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2605 void (*actor)(struct net_device *, u32))
2606 {
2607 struct ethtool_value edata;
2608
2609 if (!actor)
2610 return -EOPNOTSUPP;
2611
2612 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2613 return -EFAULT;
2614
2615 actor(dev, edata.data);
2616 return 0;
2617 }
2618
ethtool_set_value(struct net_device * dev,char __user * useraddr,int (* actor)(struct net_device *,u32))2619 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2620 int (*actor)(struct net_device *, u32))
2621 {
2622 struct ethtool_value edata;
2623
2624 if (!actor)
2625 return -EOPNOTSUPP;
2626
2627 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2628 return -EFAULT;
2629
2630 return actor(dev, edata.data);
2631 }
2632
2633 static int
ethtool_flash_device(struct net_device * dev,struct ethtool_devlink_compat * req)2634 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2635 {
2636 if (!dev->ethtool_ops->flash_device) {
2637 req->devlink = netdev_to_devlink_get(dev);
2638 return 0;
2639 }
2640
2641 return dev->ethtool_ops->flash_device(dev, &req->efl);
2642 }
2643
ethtool_set_dump(struct net_device * dev,void __user * useraddr)2644 static int ethtool_set_dump(struct net_device *dev,
2645 void __user *useraddr)
2646 {
2647 struct ethtool_dump dump;
2648
2649 if (!dev->ethtool_ops->set_dump)
2650 return -EOPNOTSUPP;
2651
2652 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2653 return -EFAULT;
2654
2655 return dev->ethtool_ops->set_dump(dev, &dump);
2656 }
2657
ethtool_get_dump_flag(struct net_device * dev,void __user * useraddr)2658 static int ethtool_get_dump_flag(struct net_device *dev,
2659 void __user *useraddr)
2660 {
2661 int ret;
2662 struct ethtool_dump dump;
2663 const struct ethtool_ops *ops = dev->ethtool_ops;
2664
2665 if (!ops->get_dump_flag)
2666 return -EOPNOTSUPP;
2667
2668 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2669 return -EFAULT;
2670
2671 ret = ops->get_dump_flag(dev, &dump);
2672 if (ret)
2673 return ret;
2674
2675 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2676 return -EFAULT;
2677 return 0;
2678 }
2679
ethtool_get_dump_data(struct net_device * dev,void __user * useraddr)2680 static int ethtool_get_dump_data(struct net_device *dev,
2681 void __user *useraddr)
2682 {
2683 int ret;
2684 __u32 len;
2685 struct ethtool_dump dump, tmp;
2686 const struct ethtool_ops *ops = dev->ethtool_ops;
2687 void *data = NULL;
2688
2689 if (!ops->get_dump_data || !ops->get_dump_flag)
2690 return -EOPNOTSUPP;
2691
2692 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2693 return -EFAULT;
2694
2695 memset(&tmp, 0, sizeof(tmp));
2696 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2697 ret = ops->get_dump_flag(dev, &tmp);
2698 if (ret)
2699 return ret;
2700
2701 len = min(tmp.len, dump.len);
2702 if (!len)
2703 return -EFAULT;
2704
2705 /* Don't ever let the driver think there's more space available
2706 * than it requested with .get_dump_flag().
2707 */
2708 dump.len = len;
2709
2710 /* Always allocate enough space to hold the whole thing so that the
2711 * driver does not need to check the length and bother with partial
2712 * dumping.
2713 */
2714 data = vzalloc(tmp.len);
2715 if (!data)
2716 return -ENOMEM;
2717 ret = ops->get_dump_data(dev, &dump, data);
2718 if (ret)
2719 goto out;
2720
2721 /* There are two sane possibilities:
2722 * 1. The driver's .get_dump_data() does not touch dump.len.
2723 * 2. Or it may set dump.len to how much it really writes, which
2724 * should be tmp.len (or len if it can do a partial dump).
2725 * In any case respond to userspace with the actual length of data
2726 * it's receiving.
2727 */
2728 WARN_ON(dump.len != len && dump.len != tmp.len);
2729 dump.len = len;
2730
2731 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2732 ret = -EFAULT;
2733 goto out;
2734 }
2735 useraddr += offsetof(struct ethtool_dump, data);
2736 if (copy_to_user(useraddr, data, len))
2737 ret = -EFAULT;
2738 out:
2739 vfree(data);
2740 return ret;
2741 }
2742
ethtool_get_ts_info(struct net_device * dev,void __user * useraddr)2743 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2744 {
2745 struct kernel_ethtool_ts_info kernel_info;
2746 struct ethtool_ts_info info = {};
2747 int err;
2748
2749 err = __ethtool_get_ts_info(dev, &kernel_info);
2750 if (err)
2751 return err;
2752
2753 info.cmd = kernel_info.cmd;
2754 info.so_timestamping = kernel_info.so_timestamping;
2755 info.phc_index = kernel_info.phc_index;
2756 info.tx_types = kernel_info.tx_types;
2757 info.rx_filters = kernel_info.rx_filters;
2758
2759 if (copy_to_user(useraddr, &info, sizeof(info)))
2760 return -EFAULT;
2761
2762 return 0;
2763 }
2764
ethtool_get_module_info_call(struct net_device * dev,struct ethtool_modinfo * modinfo)2765 int ethtool_get_module_info_call(struct net_device *dev,
2766 struct ethtool_modinfo *modinfo)
2767 {
2768 const struct ethtool_ops *ops = dev->ethtool_ops;
2769 struct phy_device *phydev = dev->phydev;
2770
2771 if (dev->ethtool->module_fw_flash_in_progress)
2772 return -EBUSY;
2773
2774 if (dev->sfp_bus)
2775 return sfp_get_module_info(dev->sfp_bus, modinfo);
2776
2777 if (phydev && phydev->drv && phydev->drv->module_info)
2778 return phydev->drv->module_info(phydev, modinfo);
2779
2780 if (ops->get_module_info)
2781 return ops->get_module_info(dev, modinfo);
2782
2783 return -EOPNOTSUPP;
2784 }
2785
ethtool_get_module_info(struct net_device * dev,void __user * useraddr)2786 static int ethtool_get_module_info(struct net_device *dev,
2787 void __user *useraddr)
2788 {
2789 int ret;
2790 struct ethtool_modinfo modinfo;
2791
2792 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2793 return -EFAULT;
2794
2795 ret = ethtool_get_module_info_call(dev, &modinfo);
2796 if (ret)
2797 return ret;
2798
2799 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2800 return -EFAULT;
2801
2802 return 0;
2803 }
2804
ethtool_get_module_eeprom_call(struct net_device * dev,struct ethtool_eeprom * ee,u8 * data)2805 int ethtool_get_module_eeprom_call(struct net_device *dev,
2806 struct ethtool_eeprom *ee, u8 *data)
2807 {
2808 const struct ethtool_ops *ops = dev->ethtool_ops;
2809 struct phy_device *phydev = dev->phydev;
2810
2811 if (dev->ethtool->module_fw_flash_in_progress)
2812 return -EBUSY;
2813
2814 if (dev->sfp_bus)
2815 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2816
2817 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2818 return phydev->drv->module_eeprom(phydev, ee, data);
2819
2820 if (ops->get_module_eeprom)
2821 return ops->get_module_eeprom(dev, ee, data);
2822
2823 return -EOPNOTSUPP;
2824 }
2825
ethtool_get_module_eeprom(struct net_device * dev,void __user * useraddr)2826 static int ethtool_get_module_eeprom(struct net_device *dev,
2827 void __user *useraddr)
2828 {
2829 int ret;
2830 struct ethtool_modinfo modinfo;
2831
2832 ret = ethtool_get_module_info_call(dev, &modinfo);
2833 if (ret)
2834 return ret;
2835
2836 return ethtool_get_any_eeprom(dev, useraddr,
2837 ethtool_get_module_eeprom_call,
2838 modinfo.eeprom_len);
2839 }
2840
ethtool_tunable_valid(const struct ethtool_tunable * tuna)2841 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2842 {
2843 switch (tuna->id) {
2844 case ETHTOOL_RX_COPYBREAK:
2845 case ETHTOOL_TX_COPYBREAK:
2846 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2847 if (tuna->len != sizeof(u32) ||
2848 tuna->type_id != ETHTOOL_TUNABLE_U32)
2849 return -EINVAL;
2850 break;
2851 case ETHTOOL_PFC_PREVENTION_TOUT:
2852 if (tuna->len != sizeof(u16) ||
2853 tuna->type_id != ETHTOOL_TUNABLE_U16)
2854 return -EINVAL;
2855 break;
2856 default:
2857 return -EINVAL;
2858 }
2859
2860 return 0;
2861 }
2862
ethtool_get_tunable(struct net_device * dev,void __user * useraddr)2863 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2864 {
2865 int ret;
2866 struct ethtool_tunable tuna;
2867 const struct ethtool_ops *ops = dev->ethtool_ops;
2868 void *data;
2869
2870 if (!ops->get_tunable)
2871 return -EOPNOTSUPP;
2872 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2873 return -EFAULT;
2874 ret = ethtool_tunable_valid(&tuna);
2875 if (ret)
2876 return ret;
2877 data = kzalloc(tuna.len, GFP_USER);
2878 if (!data)
2879 return -ENOMEM;
2880 ret = ops->get_tunable(dev, &tuna, data);
2881 if (ret)
2882 goto out;
2883 useraddr += sizeof(tuna);
2884 ret = -EFAULT;
2885 if (copy_to_user(useraddr, data, tuna.len))
2886 goto out;
2887 ret = 0;
2888
2889 out:
2890 kfree(data);
2891 return ret;
2892 }
2893
ethtool_set_tunable(struct net_device * dev,void __user * useraddr)2894 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2895 {
2896 int ret;
2897 struct ethtool_tunable tuna;
2898 const struct ethtool_ops *ops = dev->ethtool_ops;
2899 void *data;
2900
2901 if (!ops->set_tunable)
2902 return -EOPNOTSUPP;
2903 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2904 return -EFAULT;
2905 ret = ethtool_tunable_valid(&tuna);
2906 if (ret)
2907 return ret;
2908 useraddr += sizeof(tuna);
2909 data = memdup_user(useraddr, tuna.len);
2910 if (IS_ERR(data))
2911 return PTR_ERR(data);
2912 ret = ops->set_tunable(dev, &tuna, data);
2913
2914 kfree(data);
2915 return ret;
2916 }
2917
2918 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)2919 ethtool_get_per_queue_coalesce(struct net_device *dev,
2920 void __user *useraddr,
2921 struct ethtool_per_queue_op *per_queue_opt)
2922 {
2923 u32 bit;
2924 int ret;
2925 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2926
2927 if (!dev->ethtool_ops->get_per_queue_coalesce)
2928 return -EOPNOTSUPP;
2929
2930 useraddr += sizeof(*per_queue_opt);
2931
2932 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2933 MAX_NUM_QUEUE);
2934
2935 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2936 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2937
2938 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2939 if (ret != 0)
2940 return ret;
2941 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2942 return -EFAULT;
2943 useraddr += sizeof(coalesce);
2944 }
2945
2946 return 0;
2947 }
2948
2949 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)2950 ethtool_set_per_queue_coalesce(struct net_device *dev,
2951 void __user *useraddr,
2952 struct ethtool_per_queue_op *per_queue_opt)
2953 {
2954 u32 bit;
2955 int i, ret = 0;
2956 int n_queue;
2957 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2958 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2959
2960 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2961 (!dev->ethtool_ops->get_per_queue_coalesce))
2962 return -EOPNOTSUPP;
2963
2964 useraddr += sizeof(*per_queue_opt);
2965
2966 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2967 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2968 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2969 if (!backup)
2970 return -ENOMEM;
2971
2972 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2973 struct ethtool_coalesce coalesce;
2974
2975 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2976 if (ret != 0)
2977 goto roll_back;
2978
2979 tmp++;
2980
2981 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2982 ret = -EFAULT;
2983 goto roll_back;
2984 }
2985
2986 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2987 ret = -EOPNOTSUPP;
2988 goto roll_back;
2989 }
2990
2991 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2992 if (ret != 0)
2993 goto roll_back;
2994
2995 useraddr += sizeof(coalesce);
2996 }
2997
2998 roll_back:
2999 if (ret != 0) {
3000 tmp = backup;
3001 for_each_set_bit(i, queue_mask, bit) {
3002 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
3003 tmp++;
3004 }
3005 }
3006 kfree(backup);
3007
3008 return ret;
3009 }
3010
ethtool_set_per_queue(struct net_device * dev,void __user * useraddr,u32 sub_cmd)3011 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
3012 void __user *useraddr, u32 sub_cmd)
3013 {
3014 struct ethtool_per_queue_op per_queue_opt;
3015
3016 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
3017 return -EFAULT;
3018
3019 if (per_queue_opt.sub_command != sub_cmd)
3020 return -EINVAL;
3021
3022 switch (per_queue_opt.sub_command) {
3023 case ETHTOOL_GCOALESCE:
3024 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
3025 case ETHTOOL_SCOALESCE:
3026 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
3027 default:
3028 return -EOPNOTSUPP;
3029 }
3030 }
3031
ethtool_phy_tunable_valid(const struct ethtool_tunable * tuna)3032 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
3033 {
3034 switch (tuna->id) {
3035 case ETHTOOL_PHY_DOWNSHIFT:
3036 case ETHTOOL_PHY_FAST_LINK_DOWN:
3037 if (tuna->len != sizeof(u8) ||
3038 tuna->type_id != ETHTOOL_TUNABLE_U8)
3039 return -EINVAL;
3040 break;
3041 case ETHTOOL_PHY_EDPD:
3042 if (tuna->len != sizeof(u16) ||
3043 tuna->type_id != ETHTOOL_TUNABLE_U16)
3044 return -EINVAL;
3045 break;
3046 default:
3047 return -EINVAL;
3048 }
3049
3050 return 0;
3051 }
3052
get_phy_tunable(struct net_device * dev,void __user * useraddr)3053 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
3054 {
3055 struct phy_device *phydev = dev->phydev;
3056 struct ethtool_tunable tuna;
3057 bool phy_drv_tunable;
3058 void *data;
3059 int ret;
3060
3061 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
3062 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
3063 return -EOPNOTSUPP;
3064 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
3065 return -EFAULT;
3066 ret = ethtool_phy_tunable_valid(&tuna);
3067 if (ret)
3068 return ret;
3069 data = kzalloc(tuna.len, GFP_USER);
3070 if (!data)
3071 return -ENOMEM;
3072 if (phy_drv_tunable) {
3073 mutex_lock(&phydev->lock);
3074 ret = phydev->drv->get_tunable(phydev, &tuna, data);
3075 mutex_unlock(&phydev->lock);
3076 } else {
3077 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
3078 }
3079 if (ret)
3080 goto out;
3081 useraddr += sizeof(tuna);
3082 ret = -EFAULT;
3083 if (copy_to_user(useraddr, data, tuna.len))
3084 goto out;
3085 ret = 0;
3086
3087 out:
3088 kfree(data);
3089 return ret;
3090 }
3091
set_phy_tunable(struct net_device * dev,void __user * useraddr)3092 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
3093 {
3094 struct phy_device *phydev = dev->phydev;
3095 struct ethtool_tunable tuna;
3096 bool phy_drv_tunable;
3097 void *data;
3098 int ret;
3099
3100 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
3101 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
3102 return -EOPNOTSUPP;
3103 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
3104 return -EFAULT;
3105 ret = ethtool_phy_tunable_valid(&tuna);
3106 if (ret)
3107 return ret;
3108 useraddr += sizeof(tuna);
3109 data = memdup_user(useraddr, tuna.len);
3110 if (IS_ERR(data))
3111 return PTR_ERR(data);
3112 if (phy_drv_tunable) {
3113 mutex_lock(&phydev->lock);
3114 ret = phydev->drv->set_tunable(phydev, &tuna, data);
3115 mutex_unlock(&phydev->lock);
3116 } else {
3117 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
3118 }
3119
3120 kfree(data);
3121 return ret;
3122 }
3123
ethtool_get_fecparam(struct net_device * dev,void __user * useraddr)3124 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
3125 {
3126 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
3127 int rc;
3128
3129 if (!dev->ethtool_ops->get_fecparam)
3130 return -EOPNOTSUPP;
3131
3132 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
3133 if (rc)
3134 return rc;
3135
3136 if (WARN_ON_ONCE(fecparam.reserved))
3137 fecparam.reserved = 0;
3138
3139 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
3140 return -EFAULT;
3141 return 0;
3142 }
3143
ethtool_set_fecparam(struct net_device * dev,void __user * useraddr)3144 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
3145 {
3146 struct ethtool_fecparam fecparam;
3147
3148 if (!dev->ethtool_ops->set_fecparam)
3149 return -EOPNOTSUPP;
3150
3151 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
3152 return -EFAULT;
3153
3154 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
3155 return -EINVAL;
3156
3157 fecparam.active_fec = 0;
3158 fecparam.reserved = 0;
3159
3160 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
3161 }
3162
3163 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
3164
3165 static int
__dev_ethtool(struct net * net,struct ifreq * ifr,void __user * useraddr,u32 ethcmd,struct ethtool_devlink_compat * devlink_state)3166 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
3167 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
3168 {
3169 struct net_device *dev;
3170 u32 sub_cmd;
3171 int rc;
3172 netdev_features_t old_features;
3173
3174 dev = __dev_get_by_name(net, ifr->ifr_name);
3175 if (!dev)
3176 return -ENODEV;
3177
3178 if (ethcmd == ETHTOOL_PERQUEUE) {
3179 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
3180 return -EFAULT;
3181 } else {
3182 sub_cmd = ethcmd;
3183 }
3184 /* Allow some commands to be done by anyone */
3185 switch (sub_cmd) {
3186 case ETHTOOL_GSET:
3187 case ETHTOOL_GDRVINFO:
3188 case ETHTOOL_GMSGLVL:
3189 case ETHTOOL_GLINK:
3190 case ETHTOOL_GCOALESCE:
3191 case ETHTOOL_GRINGPARAM:
3192 case ETHTOOL_GPAUSEPARAM:
3193 case ETHTOOL_GRXCSUM:
3194 case ETHTOOL_GTXCSUM:
3195 case ETHTOOL_GSG:
3196 case ETHTOOL_GSSET_INFO:
3197 case ETHTOOL_GSTRINGS:
3198 case ETHTOOL_GSTATS:
3199 case ETHTOOL_GPHYSTATS:
3200 case ETHTOOL_GTSO:
3201 case ETHTOOL_GPERMADDR:
3202 case ETHTOOL_GUFO:
3203 case ETHTOOL_GGSO:
3204 case ETHTOOL_GGRO:
3205 case ETHTOOL_GFLAGS:
3206 case ETHTOOL_GPFLAGS:
3207 case ETHTOOL_GRXFH:
3208 case ETHTOOL_GRXRINGS:
3209 case ETHTOOL_GRXCLSRLCNT:
3210 case ETHTOOL_GRXCLSRULE:
3211 case ETHTOOL_GRXCLSRLALL:
3212 case ETHTOOL_GRXFHINDIR:
3213 case ETHTOOL_GRSSH:
3214 case ETHTOOL_GFEATURES:
3215 case ETHTOOL_GCHANNELS:
3216 case ETHTOOL_GET_TS_INFO:
3217 case ETHTOOL_GEEE:
3218 case ETHTOOL_GTUNABLE:
3219 case ETHTOOL_PHY_GTUNABLE:
3220 case ETHTOOL_GLINKSETTINGS:
3221 case ETHTOOL_GFECPARAM:
3222 break;
3223 default:
3224 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3225 return -EPERM;
3226 }
3227
3228 netdev_lock_ops(dev);
3229 if (dev->dev.parent)
3230 pm_runtime_get_sync(dev->dev.parent);
3231
3232 if (!netif_device_present(dev)) {
3233 rc = -ENODEV;
3234 goto out;
3235 }
3236
3237 if (dev->ethtool_ops->begin) {
3238 rc = dev->ethtool_ops->begin(dev);
3239 if (rc < 0)
3240 goto out;
3241 }
3242 old_features = dev->features;
3243
3244 switch (ethcmd) {
3245 case ETHTOOL_GSET:
3246 rc = ethtool_get_settings(dev, useraddr);
3247 break;
3248 case ETHTOOL_SSET:
3249 rc = ethtool_set_settings(dev, useraddr);
3250 break;
3251 case ETHTOOL_GDRVINFO:
3252 rc = ethtool_get_drvinfo(dev, devlink_state);
3253 break;
3254 case ETHTOOL_GREGS:
3255 rc = ethtool_get_regs(dev, useraddr);
3256 break;
3257 case ETHTOOL_GWOL:
3258 rc = ethtool_get_wol(dev, useraddr);
3259 break;
3260 case ETHTOOL_SWOL:
3261 rc = ethtool_set_wol(dev, useraddr);
3262 break;
3263 case ETHTOOL_GMSGLVL:
3264 rc = ethtool_get_value(dev, useraddr, ethcmd,
3265 dev->ethtool_ops->get_msglevel);
3266 break;
3267 case ETHTOOL_SMSGLVL:
3268 rc = ethtool_set_value_void(dev, useraddr,
3269 dev->ethtool_ops->set_msglevel);
3270 if (!rc)
3271 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
3272 break;
3273 case ETHTOOL_GEEE:
3274 rc = ethtool_get_eee(dev, useraddr);
3275 break;
3276 case ETHTOOL_SEEE:
3277 rc = ethtool_set_eee(dev, useraddr);
3278 break;
3279 case ETHTOOL_NWAY_RST:
3280 rc = ethtool_nway_reset(dev);
3281 break;
3282 case ETHTOOL_GLINK:
3283 rc = ethtool_get_link(dev, useraddr);
3284 break;
3285 case ETHTOOL_GEEPROM:
3286 rc = ethtool_get_eeprom(dev, useraddr);
3287 break;
3288 case ETHTOOL_SEEPROM:
3289 rc = ethtool_set_eeprom(dev, useraddr);
3290 break;
3291 case ETHTOOL_GCOALESCE:
3292 rc = ethtool_get_coalesce(dev, useraddr);
3293 break;
3294 case ETHTOOL_SCOALESCE:
3295 rc = ethtool_set_coalesce(dev, useraddr);
3296 break;
3297 case ETHTOOL_GRINGPARAM:
3298 rc = ethtool_get_ringparam(dev, useraddr);
3299 break;
3300 case ETHTOOL_SRINGPARAM:
3301 rc = ethtool_set_ringparam(dev, useraddr);
3302 break;
3303 case ETHTOOL_GPAUSEPARAM:
3304 rc = ethtool_get_pauseparam(dev, useraddr);
3305 break;
3306 case ETHTOOL_SPAUSEPARAM:
3307 rc = ethtool_set_pauseparam(dev, useraddr);
3308 break;
3309 case ETHTOOL_TEST:
3310 rc = ethtool_self_test(dev, useraddr);
3311 break;
3312 case ETHTOOL_GSTRINGS:
3313 rc = ethtool_get_strings(dev, useraddr);
3314 break;
3315 case ETHTOOL_PHYS_ID:
3316 rc = ethtool_phys_id(dev, useraddr);
3317 break;
3318 case ETHTOOL_GSTATS:
3319 rc = ethtool_get_stats(dev, useraddr);
3320 break;
3321 case ETHTOOL_GPERMADDR:
3322 rc = ethtool_get_perm_addr(dev, useraddr);
3323 break;
3324 case ETHTOOL_GFLAGS:
3325 rc = ethtool_get_value(dev, useraddr, ethcmd,
3326 __ethtool_get_flags);
3327 break;
3328 case ETHTOOL_SFLAGS:
3329 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3330 break;
3331 case ETHTOOL_GPFLAGS:
3332 rc = ethtool_get_value(dev, useraddr, ethcmd,
3333 dev->ethtool_ops->get_priv_flags);
3334 if (!rc)
3335 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
3336 break;
3337 case ETHTOOL_SPFLAGS:
3338 rc = ethtool_set_value(dev, useraddr,
3339 dev->ethtool_ops->set_priv_flags);
3340 break;
3341 case ETHTOOL_GRXFH:
3342 case ETHTOOL_GRXRINGS:
3343 case ETHTOOL_GRXCLSRLCNT:
3344 case ETHTOOL_GRXCLSRULE:
3345 case ETHTOOL_GRXCLSRLALL:
3346 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
3347 break;
3348 case ETHTOOL_SRXFH:
3349 case ETHTOOL_SRXCLSRLDEL:
3350 case ETHTOOL_SRXCLSRLINS:
3351 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
3352 break;
3353 case ETHTOOL_FLASHDEV:
3354 rc = ethtool_flash_device(dev, devlink_state);
3355 break;
3356 case ETHTOOL_RESET:
3357 rc = ethtool_reset(dev, useraddr);
3358 break;
3359 case ETHTOOL_GSSET_INFO:
3360 rc = ethtool_get_sset_info(dev, useraddr);
3361 break;
3362 case ETHTOOL_GRXFHINDIR:
3363 rc = ethtool_get_rxfh_indir(dev, useraddr);
3364 break;
3365 case ETHTOOL_SRXFHINDIR:
3366 rc = ethtool_set_rxfh_indir(dev, useraddr);
3367 break;
3368 case ETHTOOL_GRSSH:
3369 rc = ethtool_get_rxfh(dev, useraddr);
3370 break;
3371 case ETHTOOL_SRSSH:
3372 rc = ethtool_set_rxfh(dev, useraddr);
3373 break;
3374 case ETHTOOL_GFEATURES:
3375 rc = ethtool_get_features(dev, useraddr);
3376 break;
3377 case ETHTOOL_SFEATURES:
3378 rc = ethtool_set_features(dev, useraddr);
3379 break;
3380 case ETHTOOL_GTXCSUM:
3381 case ETHTOOL_GRXCSUM:
3382 case ETHTOOL_GSG:
3383 case ETHTOOL_GTSO:
3384 case ETHTOOL_GGSO:
3385 case ETHTOOL_GGRO:
3386 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
3387 break;
3388 case ETHTOOL_STXCSUM:
3389 case ETHTOOL_SRXCSUM:
3390 case ETHTOOL_SSG:
3391 case ETHTOOL_STSO:
3392 case ETHTOOL_SGSO:
3393 case ETHTOOL_SGRO:
3394 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
3395 break;
3396 case ETHTOOL_GCHANNELS:
3397 rc = ethtool_get_channels(dev, useraddr);
3398 break;
3399 case ETHTOOL_SCHANNELS:
3400 rc = ethtool_set_channels(dev, useraddr);
3401 break;
3402 case ETHTOOL_SET_DUMP:
3403 rc = ethtool_set_dump(dev, useraddr);
3404 break;
3405 case ETHTOOL_GET_DUMP_FLAG:
3406 rc = ethtool_get_dump_flag(dev, useraddr);
3407 break;
3408 case ETHTOOL_GET_DUMP_DATA:
3409 rc = ethtool_get_dump_data(dev, useraddr);
3410 break;
3411 case ETHTOOL_GET_TS_INFO:
3412 rc = ethtool_get_ts_info(dev, useraddr);
3413 break;
3414 case ETHTOOL_GMODULEINFO:
3415 rc = ethtool_get_module_info(dev, useraddr);
3416 break;
3417 case ETHTOOL_GMODULEEEPROM:
3418 rc = ethtool_get_module_eeprom(dev, useraddr);
3419 break;
3420 case ETHTOOL_GTUNABLE:
3421 rc = ethtool_get_tunable(dev, useraddr);
3422 break;
3423 case ETHTOOL_STUNABLE:
3424 rc = ethtool_set_tunable(dev, useraddr);
3425 break;
3426 case ETHTOOL_GPHYSTATS:
3427 rc = ethtool_get_phy_stats(dev, useraddr);
3428 break;
3429 case ETHTOOL_PERQUEUE:
3430 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
3431 break;
3432 case ETHTOOL_GLINKSETTINGS:
3433 rc = ethtool_get_link_ksettings(dev, useraddr);
3434 break;
3435 case ETHTOOL_SLINKSETTINGS:
3436 rc = ethtool_set_link_ksettings(dev, useraddr);
3437 break;
3438 case ETHTOOL_PHY_GTUNABLE:
3439 rc = get_phy_tunable(dev, useraddr);
3440 break;
3441 case ETHTOOL_PHY_STUNABLE:
3442 rc = set_phy_tunable(dev, useraddr);
3443 break;
3444 case ETHTOOL_GFECPARAM:
3445 rc = ethtool_get_fecparam(dev, useraddr);
3446 break;
3447 case ETHTOOL_SFECPARAM:
3448 rc = ethtool_set_fecparam(dev, useraddr);
3449 break;
3450 default:
3451 rc = -EOPNOTSUPP;
3452 }
3453
3454 if (dev->ethtool_ops->complete)
3455 dev->ethtool_ops->complete(dev);
3456
3457 if (old_features != dev->features)
3458 netdev_features_change(dev);
3459 out:
3460 if (dev->dev.parent)
3461 pm_runtime_put(dev->dev.parent);
3462 netdev_unlock_ops(dev);
3463
3464 return rc;
3465 }
3466
dev_ethtool(struct net * net,struct ifreq * ifr,void __user * useraddr)3467 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3468 {
3469 struct ethtool_devlink_compat *state;
3470 u32 ethcmd;
3471 int rc;
3472
3473 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3474 return -EFAULT;
3475
3476 state = kzalloc(sizeof(*state), GFP_KERNEL);
3477 if (!state)
3478 return -ENOMEM;
3479
3480 switch (ethcmd) {
3481 case ETHTOOL_FLASHDEV:
3482 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3483 rc = -EFAULT;
3484 goto exit_free;
3485 }
3486 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3487 break;
3488 }
3489
3490 rtnl_lock();
3491 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3492 rtnl_unlock();
3493 if (rc)
3494 goto exit_free;
3495
3496 switch (ethcmd) {
3497 case ETHTOOL_FLASHDEV:
3498 if (state->devlink)
3499 rc = devlink_compat_flash_update(state->devlink,
3500 state->efl.data);
3501 break;
3502 case ETHTOOL_GDRVINFO:
3503 if (state->devlink)
3504 devlink_compat_running_version(state->devlink,
3505 state->info.fw_version,
3506 sizeof(state->info.fw_version));
3507 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3508 rc = -EFAULT;
3509 goto exit_free;
3510 }
3511 break;
3512 }
3513
3514 exit_free:
3515 if (state->devlink)
3516 devlink_put(state->devlink);
3517 kfree(state);
3518 return rc;
3519 }
3520
3521 struct ethtool_rx_flow_key {
3522 struct flow_dissector_key_basic basic;
3523 union {
3524 struct flow_dissector_key_ipv4_addrs ipv4;
3525 struct flow_dissector_key_ipv6_addrs ipv6;
3526 };
3527 struct flow_dissector_key_ports tp;
3528 struct flow_dissector_key_ip ip;
3529 struct flow_dissector_key_vlan vlan;
3530 struct flow_dissector_key_eth_addrs eth_addrs;
3531 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3532
3533 struct ethtool_rx_flow_match {
3534 struct flow_dissector dissector;
3535 struct ethtool_rx_flow_key key;
3536 struct ethtool_rx_flow_key mask;
3537 };
3538
3539 struct ethtool_rx_flow_rule *
ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input * input)3540 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3541 {
3542 const struct ethtool_rx_flow_spec *fs = input->fs;
3543 struct ethtool_rx_flow_match *match;
3544 struct ethtool_rx_flow_rule *flow;
3545 struct flow_action_entry *act;
3546
3547 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3548 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3549 if (!flow)
3550 return ERR_PTR(-ENOMEM);
3551
3552 /* ethtool_rx supports only one single action per rule. */
3553 flow->rule = flow_rule_alloc(1);
3554 if (!flow->rule) {
3555 kfree(flow);
3556 return ERR_PTR(-ENOMEM);
3557 }
3558
3559 match = (struct ethtool_rx_flow_match *)flow->priv;
3560 flow->rule->match.dissector = &match->dissector;
3561 flow->rule->match.mask = &match->mask;
3562 flow->rule->match.key = &match->key;
3563
3564 match->mask.basic.n_proto = htons(0xffff);
3565
3566 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3567 case ETHER_FLOW: {
3568 const struct ethhdr *ether_spec, *ether_m_spec;
3569
3570 ether_spec = &fs->h_u.ether_spec;
3571 ether_m_spec = &fs->m_u.ether_spec;
3572
3573 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3574 ether_addr_copy(match->key.eth_addrs.src,
3575 ether_spec->h_source);
3576 ether_addr_copy(match->mask.eth_addrs.src,
3577 ether_m_spec->h_source);
3578 }
3579 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3580 ether_addr_copy(match->key.eth_addrs.dst,
3581 ether_spec->h_dest);
3582 ether_addr_copy(match->mask.eth_addrs.dst,
3583 ether_m_spec->h_dest);
3584 }
3585 if (ether_m_spec->h_proto) {
3586 match->key.basic.n_proto = ether_spec->h_proto;
3587 match->mask.basic.n_proto = ether_m_spec->h_proto;
3588 }
3589 }
3590 break;
3591 case TCP_V4_FLOW:
3592 case UDP_V4_FLOW: {
3593 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3594
3595 match->key.basic.n_proto = htons(ETH_P_IP);
3596
3597 v4_spec = &fs->h_u.tcp_ip4_spec;
3598 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3599
3600 if (v4_m_spec->ip4src) {
3601 match->key.ipv4.src = v4_spec->ip4src;
3602 match->mask.ipv4.src = v4_m_spec->ip4src;
3603 }
3604 if (v4_m_spec->ip4dst) {
3605 match->key.ipv4.dst = v4_spec->ip4dst;
3606 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3607 }
3608 if (v4_m_spec->ip4src ||
3609 v4_m_spec->ip4dst) {
3610 match->dissector.used_keys |=
3611 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3612 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3613 offsetof(struct ethtool_rx_flow_key, ipv4);
3614 }
3615 if (v4_m_spec->psrc) {
3616 match->key.tp.src = v4_spec->psrc;
3617 match->mask.tp.src = v4_m_spec->psrc;
3618 }
3619 if (v4_m_spec->pdst) {
3620 match->key.tp.dst = v4_spec->pdst;
3621 match->mask.tp.dst = v4_m_spec->pdst;
3622 }
3623 if (v4_m_spec->psrc ||
3624 v4_m_spec->pdst) {
3625 match->dissector.used_keys |=
3626 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3627 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3628 offsetof(struct ethtool_rx_flow_key, tp);
3629 }
3630 if (v4_m_spec->tos) {
3631 match->key.ip.tos = v4_spec->tos;
3632 match->mask.ip.tos = v4_m_spec->tos;
3633 match->dissector.used_keys |=
3634 BIT(FLOW_DISSECTOR_KEY_IP);
3635 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3636 offsetof(struct ethtool_rx_flow_key, ip);
3637 }
3638 }
3639 break;
3640 case TCP_V6_FLOW:
3641 case UDP_V6_FLOW: {
3642 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3643
3644 match->key.basic.n_proto = htons(ETH_P_IPV6);
3645
3646 v6_spec = &fs->h_u.tcp_ip6_spec;
3647 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3648 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
3649 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3650 sizeof(match->key.ipv6.src));
3651 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3652 sizeof(match->mask.ipv6.src));
3653 }
3654 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3655 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3656 sizeof(match->key.ipv6.dst));
3657 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3658 sizeof(match->mask.ipv6.dst));
3659 }
3660 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3661 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3662 match->dissector.used_keys |=
3663 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3664 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3665 offsetof(struct ethtool_rx_flow_key, ipv6);
3666 }
3667 if (v6_m_spec->psrc) {
3668 match->key.tp.src = v6_spec->psrc;
3669 match->mask.tp.src = v6_m_spec->psrc;
3670 }
3671 if (v6_m_spec->pdst) {
3672 match->key.tp.dst = v6_spec->pdst;
3673 match->mask.tp.dst = v6_m_spec->pdst;
3674 }
3675 if (v6_m_spec->psrc ||
3676 v6_m_spec->pdst) {
3677 match->dissector.used_keys |=
3678 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3679 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3680 offsetof(struct ethtool_rx_flow_key, tp);
3681 }
3682 if (v6_m_spec->tclass) {
3683 match->key.ip.tos = v6_spec->tclass;
3684 match->mask.ip.tos = v6_m_spec->tclass;
3685 match->dissector.used_keys |=
3686 BIT_ULL(FLOW_DISSECTOR_KEY_IP);
3687 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3688 offsetof(struct ethtool_rx_flow_key, ip);
3689 }
3690 }
3691 break;
3692 default:
3693 ethtool_rx_flow_rule_destroy(flow);
3694 return ERR_PTR(-EINVAL);
3695 }
3696
3697 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3698 case TCP_V4_FLOW:
3699 case TCP_V6_FLOW:
3700 match->key.basic.ip_proto = IPPROTO_TCP;
3701 match->mask.basic.ip_proto = 0xff;
3702 break;
3703 case UDP_V4_FLOW:
3704 case UDP_V6_FLOW:
3705 match->key.basic.ip_proto = IPPROTO_UDP;
3706 match->mask.basic.ip_proto = 0xff;
3707 break;
3708 }
3709
3710 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
3711 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3712 offsetof(struct ethtool_rx_flow_key, basic);
3713
3714 if (fs->flow_type & FLOW_EXT) {
3715 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3716 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3717
3718 if (ext_m_spec->vlan_etype) {
3719 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3720 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3721 }
3722
3723 if (ext_m_spec->vlan_tci) {
3724 match->key.vlan.vlan_id =
3725 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3726 match->mask.vlan.vlan_id =
3727 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3728
3729 match->key.vlan.vlan_dei =
3730 !!(ext_h_spec->vlan_tci & htons(0x1000));
3731 match->mask.vlan.vlan_dei =
3732 !!(ext_m_spec->vlan_tci & htons(0x1000));
3733
3734 match->key.vlan.vlan_priority =
3735 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3736 match->mask.vlan.vlan_priority =
3737 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3738 }
3739
3740 if (ext_m_spec->vlan_etype ||
3741 ext_m_spec->vlan_tci) {
3742 match->dissector.used_keys |=
3743 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
3744 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3745 offsetof(struct ethtool_rx_flow_key, vlan);
3746 }
3747 }
3748 if (fs->flow_type & FLOW_MAC_EXT) {
3749 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3750 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3751
3752 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3753 ETH_ALEN);
3754 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3755 ETH_ALEN);
3756
3757 match->dissector.used_keys |=
3758 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3759 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3760 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3761 }
3762
3763 act = &flow->rule->action.entries[0];
3764 switch (fs->ring_cookie) {
3765 case RX_CLS_FLOW_DISC:
3766 act->id = FLOW_ACTION_DROP;
3767 break;
3768 case RX_CLS_FLOW_WAKE:
3769 act->id = FLOW_ACTION_WAKE;
3770 break;
3771 default:
3772 act->id = FLOW_ACTION_QUEUE;
3773 if (fs->flow_type & FLOW_RSS)
3774 act->queue.ctx = input->rss_ctx;
3775
3776 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3777 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3778 break;
3779 }
3780
3781 return flow;
3782 }
3783 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3784
ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule * flow)3785 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3786 {
3787 kfree(flow->rule);
3788 kfree(flow);
3789 }
3790 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);
3791