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