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