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