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