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