xref: /linux/net/ethtool/ioctl.c (revision 8028e290b6354ddb404e88f17fe5d37945cb122f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/core/ethtool.c - Ethtool ioctl handler
4  * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5  *
6  * This file is where we call all the ethtool_ops commands to get
7  * the information ethtool needs.
8  */
9 
10 #include <linux/compat.h>
11 #include <linux/etherdevice.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/ethtool.h>
17 #include <linux/netdevice.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/phy.h>
20 #include <linux/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/sfp.h>
24 #include <linux/slab.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/sched/signal.h>
27 #include <linux/net.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/utsname.h>
30 #include <net/devlink.h>
31 #include <net/ipv6.h>
32 #include <net/xdp_sock_drv.h>
33 #include <net/flow_offload.h>
34 #include <linux/ethtool_netlink.h>
35 #include "common.h"
36 
37 /* State held across locks and calls for commands which have devlink fallback */
38 struct ethtool_devlink_compat {
39 	struct devlink *devlink;
40 	union {
41 		struct ethtool_flash efl;
42 		struct ethtool_drvinfo info;
43 	};
44 };
45 
46 static struct devlink *netdev_to_devlink_get(struct net_device *dev)
47 {
48 	if (!dev->devlink_port)
49 		return NULL;
50 	return devlink_try_get(dev->devlink_port->devlink);
51 }
52 
53 /*
54  * Some useful ethtool_ops methods that're device independent.
55  * If we find that all drivers want to do the same thing here,
56  * we can turn these into dev_() function calls.
57  */
58 
59 u32 ethtool_op_get_link(struct net_device *dev)
60 {
61 	/* Synchronize carrier state with link watch, see also rtnl_getlink() */
62 	linkwatch_sync_dev(dev);
63 
64 	return netif_carrier_ok(dev) ? 1 : 0;
65 }
66 EXPORT_SYMBOL(ethtool_op_get_link);
67 
68 int ethtool_op_get_ts_info(struct net_device *dev, 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, init_uts_ns.name.release,
717 		sizeof(rsp->info.version));
718 	if (ops->get_drvinfo) {
719 		ops->get_drvinfo(dev, &rsp->info);
720 		if (!rsp->info.bus_info[0] && parent)
721 			strscpy(rsp->info.bus_info, dev_name(parent),
722 				sizeof(rsp->info.bus_info));
723 		if (!rsp->info.driver[0] && parent && parent->driver)
724 			strscpy(rsp->info.driver, parent->driver->name,
725 				sizeof(rsp->info.driver));
726 	} else if (parent && parent->driver) {
727 		strscpy(rsp->info.bus_info, dev_name(parent),
728 			sizeof(rsp->info.bus_info));
729 		strscpy(rsp->info.driver, parent->driver->name,
730 			sizeof(rsp->info.driver));
731 	} else if (dev->rtnl_link_ops) {
732 		strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
733 			sizeof(rsp->info.driver));
734 	} else {
735 		return -EOPNOTSUPP;
736 	}
737 
738 	/*
739 	 * this method of obtaining string set info is deprecated;
740 	 * Use ETHTOOL_GSSET_INFO instead.
741 	 */
742 	if (ops->get_sset_count) {
743 		int rc;
744 
745 		rc = ops->get_sset_count(dev, ETH_SS_TEST);
746 		if (rc >= 0)
747 			rsp->info.testinfo_len = rc;
748 		rc = ops->get_sset_count(dev, ETH_SS_STATS);
749 		if (rc >= 0)
750 			rsp->info.n_stats = rc;
751 		rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
752 		if (rc >= 0)
753 			rsp->info.n_priv_flags = rc;
754 	}
755 	if (ops->get_regs_len) {
756 		int ret = ops->get_regs_len(dev);
757 
758 		if (ret > 0)
759 			rsp->info.regdump_len = ret;
760 	}
761 
762 	if (ops->get_eeprom_len)
763 		rsp->info.eedump_len = ops->get_eeprom_len(dev);
764 
765 	if (!rsp->info.fw_version[0])
766 		rsp->devlink = netdev_to_devlink_get(dev);
767 
768 	return 0;
769 }
770 
771 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
772 						    void __user *useraddr)
773 {
774 	struct ethtool_sset_info info;
775 	u64 sset_mask;
776 	int i, idx = 0, n_bits = 0, ret, rc;
777 	u32 *info_buf = NULL;
778 
779 	if (copy_from_user(&info, useraddr, sizeof(info)))
780 		return -EFAULT;
781 
782 	/* store copy of mask, because we zero struct later on */
783 	sset_mask = info.sset_mask;
784 	if (!sset_mask)
785 		return 0;
786 
787 	/* calculate size of return buffer */
788 	n_bits = hweight64(sset_mask);
789 
790 	memset(&info, 0, sizeof(info));
791 	info.cmd = ETHTOOL_GSSET_INFO;
792 
793 	info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
794 	if (!info_buf)
795 		return -ENOMEM;
796 
797 	/*
798 	 * fill return buffer based on input bitmask and successful
799 	 * get_sset_count return
800 	 */
801 	for (i = 0; i < 64; i++) {
802 		if (!(sset_mask & (1ULL << i)))
803 			continue;
804 
805 		rc = __ethtool_get_sset_count(dev, i);
806 		if (rc >= 0) {
807 			info.sset_mask |= (1ULL << i);
808 			info_buf[idx++] = rc;
809 		}
810 	}
811 
812 	ret = -EFAULT;
813 	if (copy_to_user(useraddr, &info, sizeof(info)))
814 		goto out;
815 
816 	useraddr += offsetof(struct ethtool_sset_info, data);
817 	if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
818 		goto out;
819 
820 	ret = 0;
821 
822 out:
823 	kfree(info_buf);
824 	return ret;
825 }
826 
827 static noinline_for_stack int
828 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
829 			       const struct compat_ethtool_rxnfc __user *useraddr,
830 			       size_t size)
831 {
832 	struct compat_ethtool_rxnfc crxnfc = {};
833 
834 	/* We expect there to be holes between fs.m_ext and
835 	 * fs.ring_cookie and at the end of fs, but nowhere else.
836 	 * On non-x86, no conversion should be needed.
837 	 */
838 	BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
839 		     sizeof(struct compat_ethtool_rxnfc) !=
840 		     sizeof(struct ethtool_rxnfc));
841 	BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
842 		     sizeof(useraddr->fs.m_ext) !=
843 		     offsetof(struct ethtool_rxnfc, fs.m_ext) +
844 		     sizeof(rxnfc->fs.m_ext));
845 	BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
846 		     offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
847 		     offsetof(struct ethtool_rxnfc, fs.location) -
848 		     offsetof(struct ethtool_rxnfc, fs.ring_cookie));
849 
850 	if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
851 		return -EFAULT;
852 
853 	*rxnfc = (struct ethtool_rxnfc) {
854 		.cmd		= crxnfc.cmd,
855 		.flow_type	= crxnfc.flow_type,
856 		.data		= crxnfc.data,
857 		.fs		= {
858 			.flow_type	= crxnfc.fs.flow_type,
859 			.h_u		= crxnfc.fs.h_u,
860 			.h_ext		= crxnfc.fs.h_ext,
861 			.m_u		= crxnfc.fs.m_u,
862 			.m_ext		= crxnfc.fs.m_ext,
863 			.ring_cookie	= crxnfc.fs.ring_cookie,
864 			.location	= crxnfc.fs.location,
865 		},
866 		.rule_cnt	= crxnfc.rule_cnt,
867 	};
868 
869 	return 0;
870 }
871 
872 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
873 					const void __user *useraddr,
874 					size_t size)
875 {
876 	if (compat_need_64bit_alignment_fixup())
877 		return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
878 
879 	if (copy_from_user(rxnfc, useraddr, size))
880 		return -EFAULT;
881 
882 	return 0;
883 }
884 
885 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
886 					const struct ethtool_rxnfc *rxnfc,
887 					size_t size, const u32 *rule_buf)
888 {
889 	struct compat_ethtool_rxnfc crxnfc;
890 
891 	memset(&crxnfc, 0, sizeof(crxnfc));
892 	crxnfc = (struct compat_ethtool_rxnfc) {
893 		.cmd		= rxnfc->cmd,
894 		.flow_type	= rxnfc->flow_type,
895 		.data		= rxnfc->data,
896 		.fs		= {
897 			.flow_type	= rxnfc->fs.flow_type,
898 			.h_u		= rxnfc->fs.h_u,
899 			.h_ext		= rxnfc->fs.h_ext,
900 			.m_u		= rxnfc->fs.m_u,
901 			.m_ext		= rxnfc->fs.m_ext,
902 			.ring_cookie	= rxnfc->fs.ring_cookie,
903 			.location	= rxnfc->fs.location,
904 		},
905 		.rule_cnt	= rxnfc->rule_cnt,
906 	};
907 
908 	if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
909 		return -EFAULT;
910 
911 	return 0;
912 }
913 
914 static int ethtool_rxnfc_copy_struct(u32 cmd, struct ethtool_rxnfc *info,
915 				     size_t *info_size, void __user *useraddr)
916 {
917 	/* struct ethtool_rxnfc was originally defined for
918 	 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
919 	 * members.  User-space might still be using that
920 	 * definition.
921 	 */
922 	if (cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH)
923 		*info_size = (offsetof(struct ethtool_rxnfc, data) +
924 			      sizeof(info->data));
925 
926 	if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
927 		return -EFAULT;
928 
929 	if ((cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH) && info->flow_type & FLOW_RSS) {
930 		*info_size = sizeof(*info);
931 		if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
932 			return -EFAULT;
933 		/* Since malicious users may modify the original data,
934 		 * we need to check whether FLOW_RSS is still requested.
935 		 */
936 		if (!(info->flow_type & FLOW_RSS))
937 			return -EINVAL;
938 	}
939 
940 	if (info->cmd != cmd)
941 		return -EINVAL;
942 
943 	return 0;
944 }
945 
946 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
947 				      const struct ethtool_rxnfc *rxnfc,
948 				      size_t size, const u32 *rule_buf)
949 {
950 	int ret;
951 
952 	if (compat_need_64bit_alignment_fixup()) {
953 		ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
954 						   rule_buf);
955 		useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
956 	} else {
957 		ret = copy_to_user(useraddr, rxnfc, size);
958 		useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
959 	}
960 
961 	if (ret)
962 		return -EFAULT;
963 
964 	if (rule_buf) {
965 		if (copy_to_user(useraddr, rule_buf,
966 				 rxnfc->rule_cnt * sizeof(u32)))
967 			return -EFAULT;
968 	}
969 
970 	return 0;
971 }
972 
973 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
974 						u32 cmd, void __user *useraddr)
975 {
976 	const struct ethtool_ops *ops = dev->ethtool_ops;
977 	struct ethtool_rxnfc info;
978 	size_t info_size = sizeof(info);
979 	int rc;
980 
981 	if (!ops->set_rxnfc)
982 		return -EOPNOTSUPP;
983 
984 	rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
985 	if (rc)
986 		return rc;
987 
988 	if (ops->get_rxfh) {
989 		struct ethtool_rxfh_param rxfh = {};
990 
991 		rc = ops->get_rxfh(dev, &rxfh);
992 		if (rc)
993 			return rc;
994 
995 		/* Sanity check: if symmetric-xor is set, then:
996 		 * 1 - no other fields besides IP src/dst and/or L4 src/dst
997 		 * 2 - If src is set, dst must also be set
998 		 */
999 		if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1000 		    ((info.data & ~(RXH_IP_SRC | RXH_IP_DST |
1001 				    RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
1002 		     (!!(info.data & RXH_IP_SRC) ^ !!(info.data & RXH_IP_DST)) ||
1003 		     (!!(info.data & RXH_L4_B_0_1) ^ !!(info.data & RXH_L4_B_2_3))))
1004 			return -EINVAL;
1005 	}
1006 
1007 	rc = ops->set_rxnfc(dev, &info);
1008 	if (rc)
1009 		return rc;
1010 
1011 	if (cmd == ETHTOOL_SRXCLSRLINS &&
1012 	    ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
1013 		return -EFAULT;
1014 
1015 	return 0;
1016 }
1017 
1018 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1019 						u32 cmd, void __user *useraddr)
1020 {
1021 	struct ethtool_rxnfc info;
1022 	size_t info_size = sizeof(info);
1023 	const struct ethtool_ops *ops = dev->ethtool_ops;
1024 	int ret;
1025 	void *rule_buf = NULL;
1026 
1027 	if (!ops->get_rxnfc)
1028 		return -EOPNOTSUPP;
1029 
1030 	ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1031 	if (ret)
1032 		return ret;
1033 
1034 	if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1035 		if (info.rule_cnt > 0) {
1036 			if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1037 				rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1038 						   GFP_USER);
1039 			if (!rule_buf)
1040 				return -ENOMEM;
1041 		}
1042 	}
1043 
1044 	ret = ops->get_rxnfc(dev, &info, rule_buf);
1045 	if (ret < 0)
1046 		goto err_out;
1047 
1048 	ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1049 err_out:
1050 	kfree(rule_buf);
1051 
1052 	return ret;
1053 }
1054 
1055 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1056 					struct ethtool_rxnfc *rx_rings,
1057 					u32 size)
1058 {
1059 	int i;
1060 
1061 	if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1062 		return -EFAULT;
1063 
1064 	/* Validate ring indices */
1065 	for (i = 0; i < size; i++)
1066 		if (indir[i] >= rx_rings->data)
1067 			return -EINVAL;
1068 
1069 	return 0;
1070 }
1071 
1072 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1073 
1074 void netdev_rss_key_fill(void *buffer, size_t len)
1075 {
1076 	BUG_ON(len > sizeof(netdev_rss_key));
1077 	net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1078 	memcpy(buffer, netdev_rss_key, len);
1079 }
1080 EXPORT_SYMBOL(netdev_rss_key_fill);
1081 
1082 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1083 						     void __user *useraddr)
1084 {
1085 	struct ethtool_rxfh_param rxfh = {};
1086 	u32 user_size;
1087 	int ret;
1088 
1089 	if (!dev->ethtool_ops->get_rxfh_indir_size ||
1090 	    !dev->ethtool_ops->get_rxfh)
1091 		return -EOPNOTSUPP;
1092 	rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1093 	if (rxfh.indir_size == 0)
1094 		return -EOPNOTSUPP;
1095 
1096 	if (copy_from_user(&user_size,
1097 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
1098 			   sizeof(user_size)))
1099 		return -EFAULT;
1100 
1101 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1102 			 &rxfh.indir_size, sizeof(rxfh.indir_size)))
1103 		return -EFAULT;
1104 
1105 	/* If the user buffer size is 0, this is just a query for the
1106 	 * device table size.  Otherwise, if it's smaller than the
1107 	 * device table size it's an error.
1108 	 */
1109 	if (user_size < rxfh.indir_size)
1110 		return user_size == 0 ? 0 : -EINVAL;
1111 
1112 	rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
1113 	if (!rxfh.indir)
1114 		return -ENOMEM;
1115 
1116 	ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
1117 	if (ret)
1118 		goto out;
1119 	if (copy_to_user(useraddr +
1120 			 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1121 			 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
1122 		ret = -EFAULT;
1123 
1124 out:
1125 	kfree(rxfh.indir);
1126 	return ret;
1127 }
1128 
1129 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1130 						     void __user *useraddr)
1131 {
1132 	const struct ethtool_ops *ops = dev->ethtool_ops;
1133 	struct ethtool_rxfh_param rxfh_dev = {};
1134 	struct netlink_ext_ack *extack = NULL;
1135 	struct ethtool_rxnfc rx_rings;
1136 	u32 user_size, i;
1137 	int ret;
1138 	u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1139 
1140 	if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1141 	    !ops->get_rxnfc)
1142 		return -EOPNOTSUPP;
1143 
1144 	rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1145 	if (rxfh_dev.indir_size == 0)
1146 		return -EOPNOTSUPP;
1147 
1148 	if (copy_from_user(&user_size,
1149 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
1150 			   sizeof(user_size)))
1151 		return -EFAULT;
1152 
1153 	if (user_size != 0 && user_size != rxfh_dev.indir_size)
1154 		return -EINVAL;
1155 
1156 	rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
1157 				 sizeof(rxfh_dev.indir[0]), GFP_USER);
1158 	if (!rxfh_dev.indir)
1159 		return -ENOMEM;
1160 
1161 	rx_rings.cmd = ETHTOOL_GRXRINGS;
1162 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1163 	if (ret)
1164 		goto out;
1165 
1166 	if (user_size == 0) {
1167 		u32 *indir = rxfh_dev.indir;
1168 
1169 		for (i = 0; i < rxfh_dev.indir_size; i++)
1170 			indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1171 	} else {
1172 		ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1173 						  useraddr + ringidx_offset,
1174 						  &rx_rings,
1175 						  rxfh_dev.indir_size);
1176 		if (ret)
1177 			goto out;
1178 	}
1179 
1180 	rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
1181 	ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1182 	if (ret)
1183 		goto out;
1184 
1185 	/* indicate whether rxfh was set to default */
1186 	if (user_size == 0)
1187 		dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1188 	else
1189 		dev->priv_flags |= IFF_RXFH_CONFIGURED;
1190 
1191 out:
1192 	kfree(rxfh_dev.indir);
1193 	return ret;
1194 }
1195 
1196 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1197 					       void __user *useraddr)
1198 {
1199 	const struct ethtool_ops *ops = dev->ethtool_ops;
1200 	struct ethtool_rxfh_param rxfh_dev = {};
1201 	u32 user_indir_size, user_key_size;
1202 	struct ethtool_rxfh rxfh;
1203 	u32 indir_bytes;
1204 	u8 *rss_config;
1205 	u32 total_size;
1206 	int ret;
1207 
1208 	if (!ops->get_rxfh)
1209 		return -EOPNOTSUPP;
1210 
1211 	if (ops->get_rxfh_indir_size)
1212 		rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1213 	if (ops->get_rxfh_key_size)
1214 		rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
1215 
1216 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1217 		return -EFAULT;
1218 	user_indir_size = rxfh.indir_size;
1219 	user_key_size = rxfh.key_size;
1220 
1221 	/* Check that reserved fields are 0 for now */
1222 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1223 		return -EINVAL;
1224 	/* Most drivers don't handle rss_context, check it's 0 as well */
1225 	if (rxfh.rss_context && !ops->cap_rss_ctx_supported)
1226 		return -EOPNOTSUPP;
1227 
1228 	rxfh.indir_size = rxfh_dev.indir_size;
1229 	rxfh.key_size = rxfh_dev.key_size;
1230 	if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1231 		return -EFAULT;
1232 
1233 	if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
1234 	    (user_key_size && user_key_size != rxfh_dev.key_size))
1235 		return -EINVAL;
1236 
1237 	indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
1238 	total_size = indir_bytes + user_key_size;
1239 	rss_config = kzalloc(total_size, GFP_USER);
1240 	if (!rss_config)
1241 		return -ENOMEM;
1242 
1243 	if (user_indir_size)
1244 		rxfh_dev.indir = (u32 *)rss_config;
1245 
1246 	if (user_key_size)
1247 		rxfh_dev.key = rss_config + indir_bytes;
1248 
1249 	rxfh_dev.rss_context = rxfh.rss_context;
1250 
1251 	ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
1252 	if (ret)
1253 		goto out;
1254 
1255 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1256 			 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
1257 		ret = -EFAULT;
1258 	} else if (copy_to_user(useraddr +
1259 				offsetof(struct ethtool_rxfh, input_xfrm),
1260 				&rxfh_dev.input_xfrm,
1261 				sizeof(rxfh.input_xfrm))) {
1262 		ret = -EFAULT;
1263 	} else if (copy_to_user(useraddr +
1264 			      offsetof(struct ethtool_rxfh, rss_config[0]),
1265 			      rss_config, total_size)) {
1266 		ret = -EFAULT;
1267 	}
1268 out:
1269 	kfree(rss_config);
1270 
1271 	return ret;
1272 }
1273 
1274 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1275 					       void __user *useraddr)
1276 {
1277 	u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1278 	const struct ethtool_ops *ops = dev->ethtool_ops;
1279 	u32 dev_indir_size = 0, dev_key_size = 0, i;
1280 	struct ethtool_rxfh_param rxfh_dev = {};
1281 	struct netlink_ext_ack *extack = NULL;
1282 	struct ethtool_rxnfc rx_rings;
1283 	struct ethtool_rxfh rxfh;
1284 	u32 indir_bytes = 0;
1285 	u8 *rss_config;
1286 	int ret;
1287 
1288 	if (!ops->get_rxnfc || !ops->set_rxfh)
1289 		return -EOPNOTSUPP;
1290 
1291 	if (ops->get_rxfh_indir_size)
1292 		dev_indir_size = ops->get_rxfh_indir_size(dev);
1293 	if (ops->get_rxfh_key_size)
1294 		dev_key_size = ops->get_rxfh_key_size(dev);
1295 
1296 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1297 		return -EFAULT;
1298 
1299 	/* Check that reserved fields are 0 for now */
1300 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1301 		return -EINVAL;
1302 	/* Most drivers don't handle rss_context, check it's 0 as well */
1303 	if (rxfh.rss_context && !ops->cap_rss_ctx_supported)
1304 		return -EOPNOTSUPP;
1305 	/* Check input data transformation capabilities */
1306 	if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
1307 	    rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
1308 		return -EINVAL;
1309 	if (rxfh.input_xfrm != RXH_XFRM_NO_CHANGE &&
1310 	    (rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
1311 	    !ops->cap_rss_sym_xor_supported)
1312 		return -EOPNOTSUPP;
1313 
1314 	/* If either indir, hash key or function is valid, proceed further.
1315 	 * Must request at least one change: indir size, hash key, function
1316 	 * or input transformation.
1317 	 */
1318 	if ((rxfh.indir_size &&
1319 	     rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1320 	     rxfh.indir_size != dev_indir_size) ||
1321 	    (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1322 	    (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1323 	     rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
1324 	     rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
1325 		return -EINVAL;
1326 
1327 	if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1328 		indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
1329 
1330 	rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1331 	if (!rss_config)
1332 		return -ENOMEM;
1333 
1334 	rx_rings.cmd = ETHTOOL_GRXRINGS;
1335 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1336 	if (ret)
1337 		goto out;
1338 
1339 	/* rxfh.indir_size == 0 means reset the indir table to default (master
1340 	 * context) or delete the context (other RSS contexts).
1341 	 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1342 	 */
1343 	if (rxfh.indir_size &&
1344 	    rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1345 		rxfh_dev.indir = (u32 *)rss_config;
1346 		rxfh_dev.indir_size = dev_indir_size;
1347 		ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1348 						  useraddr + rss_cfg_offset,
1349 						  &rx_rings,
1350 						  rxfh.indir_size);
1351 		if (ret)
1352 			goto out;
1353 	} else if (rxfh.indir_size == 0) {
1354 		if (rxfh.rss_context == 0) {
1355 			u32 *indir;
1356 
1357 			rxfh_dev.indir = (u32 *)rss_config;
1358 			rxfh_dev.indir_size = dev_indir_size;
1359 			indir = rxfh_dev.indir;
1360 			for (i = 0; i < dev_indir_size; i++)
1361 				indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1362 		} else {
1363 			rxfh_dev.rss_delete = true;
1364 		}
1365 	}
1366 
1367 	if (rxfh.key_size) {
1368 		rxfh_dev.key_size = dev_key_size;
1369 		rxfh_dev.key = rss_config + indir_bytes;
1370 		if (copy_from_user(rxfh_dev.key,
1371 				   useraddr + rss_cfg_offset + indir_bytes,
1372 				   rxfh.key_size)) {
1373 			ret = -EFAULT;
1374 			goto out;
1375 		}
1376 	}
1377 
1378 	rxfh_dev.hfunc = rxfh.hfunc;
1379 	rxfh_dev.rss_context = rxfh.rss_context;
1380 	rxfh_dev.input_xfrm = rxfh.input_xfrm;
1381 
1382 	ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1383 	if (ret)
1384 		goto out;
1385 
1386 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1387 			 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
1388 		ret = -EFAULT;
1389 
1390 	if (!rxfh_dev.rss_context) {
1391 		/* indicate whether rxfh was set to default */
1392 		if (rxfh.indir_size == 0)
1393 			dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1394 		else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1395 			dev->priv_flags |= IFF_RXFH_CONFIGURED;
1396 	}
1397 
1398 out:
1399 	kfree(rss_config);
1400 	return ret;
1401 }
1402 
1403 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1404 {
1405 	struct ethtool_regs regs;
1406 	const struct ethtool_ops *ops = dev->ethtool_ops;
1407 	void *regbuf;
1408 	int reglen, ret;
1409 
1410 	if (!ops->get_regs || !ops->get_regs_len)
1411 		return -EOPNOTSUPP;
1412 
1413 	if (copy_from_user(&regs, useraddr, sizeof(regs)))
1414 		return -EFAULT;
1415 
1416 	reglen = ops->get_regs_len(dev);
1417 	if (reglen <= 0)
1418 		return reglen;
1419 
1420 	if (regs.len > reglen)
1421 		regs.len = reglen;
1422 
1423 	regbuf = vzalloc(reglen);
1424 	if (!regbuf)
1425 		return -ENOMEM;
1426 
1427 	if (regs.len < reglen)
1428 		reglen = regs.len;
1429 
1430 	ops->get_regs(dev, &regs, regbuf);
1431 
1432 	ret = -EFAULT;
1433 	if (copy_to_user(useraddr, &regs, sizeof(regs)))
1434 		goto out;
1435 	useraddr += offsetof(struct ethtool_regs, data);
1436 	if (copy_to_user(useraddr, regbuf, reglen))
1437 		goto out;
1438 	ret = 0;
1439 
1440  out:
1441 	vfree(regbuf);
1442 	return ret;
1443 }
1444 
1445 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1446 {
1447 	struct ethtool_value reset;
1448 	int ret;
1449 
1450 	if (!dev->ethtool_ops->reset)
1451 		return -EOPNOTSUPP;
1452 
1453 	if (copy_from_user(&reset, useraddr, sizeof(reset)))
1454 		return -EFAULT;
1455 
1456 	ret = dev->ethtool_ops->reset(dev, &reset.data);
1457 	if (ret)
1458 		return ret;
1459 
1460 	if (copy_to_user(useraddr, &reset, sizeof(reset)))
1461 		return -EFAULT;
1462 	return 0;
1463 }
1464 
1465 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1466 {
1467 	struct ethtool_wolinfo wol;
1468 
1469 	if (!dev->ethtool_ops->get_wol)
1470 		return -EOPNOTSUPP;
1471 
1472 	memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1473 	wol.cmd = ETHTOOL_GWOL;
1474 	dev->ethtool_ops->get_wol(dev, &wol);
1475 
1476 	if (copy_to_user(useraddr, &wol, sizeof(wol)))
1477 		return -EFAULT;
1478 	return 0;
1479 }
1480 
1481 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1482 {
1483 	struct ethtool_wolinfo wol, cur_wol;
1484 	int ret;
1485 
1486 	if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
1487 		return -EOPNOTSUPP;
1488 
1489 	memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
1490 	cur_wol.cmd = ETHTOOL_GWOL;
1491 	dev->ethtool_ops->get_wol(dev, &cur_wol);
1492 
1493 	if (copy_from_user(&wol, useraddr, sizeof(wol)))
1494 		return -EFAULT;
1495 
1496 	if (wol.wolopts & ~cur_wol.supported)
1497 		return -EINVAL;
1498 
1499 	if (wol.wolopts == cur_wol.wolopts &&
1500 	    !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
1501 		return 0;
1502 
1503 	ret = dev->ethtool_ops->set_wol(dev, &wol);
1504 	if (ret)
1505 		return ret;
1506 
1507 	dev->wol_enabled = !!wol.wolopts;
1508 	ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1509 
1510 	return 0;
1511 }
1512 
1513 static void eee_to_keee(struct ethtool_keee *keee,
1514 			const struct ethtool_eee *eee)
1515 {
1516 	memset(keee, 0, sizeof(*keee));
1517 
1518 	keee->eee_enabled = eee->eee_enabled;
1519 	keee->tx_lpi_enabled = eee->tx_lpi_enabled;
1520 	keee->tx_lpi_timer = eee->tx_lpi_timer;
1521 
1522 	ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
1523 						eee->advertised);
1524 }
1525 
1526 static void keee_to_eee(struct ethtool_eee *eee,
1527 			const struct ethtool_keee *keee)
1528 {
1529 	bool overflow;
1530 
1531 	memset(eee, 0, sizeof(*eee));
1532 
1533 	eee->eee_active = keee->eee_active;
1534 	eee->eee_enabled = keee->eee_enabled;
1535 	eee->tx_lpi_enabled = keee->tx_lpi_enabled;
1536 	eee->tx_lpi_timer = keee->tx_lpi_timer;
1537 
1538 	overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
1539 							    keee->supported);
1540 	ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
1541 						keee->advertised);
1542 	ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
1543 						keee->lp_advertised);
1544 	if (overflow)
1545 		pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
1546 }
1547 
1548 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1549 {
1550 	struct ethtool_keee keee;
1551 	struct ethtool_eee eee;
1552 	int rc;
1553 
1554 	if (!dev->ethtool_ops->get_eee)
1555 		return -EOPNOTSUPP;
1556 
1557 	memset(&keee, 0, sizeof(keee));
1558 	rc = dev->ethtool_ops->get_eee(dev, &keee);
1559 	if (rc)
1560 		return rc;
1561 
1562 	keee_to_eee(&eee, &keee);
1563 	if (copy_to_user(useraddr, &eee, sizeof(eee)))
1564 		return -EFAULT;
1565 
1566 	return 0;
1567 }
1568 
1569 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1570 {
1571 	struct ethtool_keee keee;
1572 	struct ethtool_eee eee;
1573 	int ret;
1574 
1575 	if (!dev->ethtool_ops->set_eee)
1576 		return -EOPNOTSUPP;
1577 
1578 	if (copy_from_user(&eee, useraddr, sizeof(eee)))
1579 		return -EFAULT;
1580 
1581 	eee_to_keee(&keee, &eee);
1582 	ret = dev->ethtool_ops->set_eee(dev, &keee);
1583 	if (!ret)
1584 		ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1585 	return ret;
1586 }
1587 
1588 static int ethtool_nway_reset(struct net_device *dev)
1589 {
1590 	if (!dev->ethtool_ops->nway_reset)
1591 		return -EOPNOTSUPP;
1592 
1593 	return dev->ethtool_ops->nway_reset(dev);
1594 }
1595 
1596 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1597 {
1598 	struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1599 	int link = __ethtool_get_link(dev);
1600 
1601 	if (link < 0)
1602 		return link;
1603 
1604 	edata.data = link;
1605 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
1606 		return -EFAULT;
1607 	return 0;
1608 }
1609 
1610 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1611 				  int (*getter)(struct net_device *,
1612 						struct ethtool_eeprom *, u8 *),
1613 				  u32 total_len)
1614 {
1615 	struct ethtool_eeprom eeprom;
1616 	void __user *userbuf = useraddr + sizeof(eeprom);
1617 	u32 bytes_remaining;
1618 	u8 *data;
1619 	int ret = 0;
1620 
1621 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1622 		return -EFAULT;
1623 
1624 	/* Check for wrap and zero */
1625 	if (eeprom.offset + eeprom.len <= eeprom.offset)
1626 		return -EINVAL;
1627 
1628 	/* Check for exceeding total eeprom len */
1629 	if (eeprom.offset + eeprom.len > total_len)
1630 		return -EINVAL;
1631 
1632 	data = kzalloc(PAGE_SIZE, GFP_USER);
1633 	if (!data)
1634 		return -ENOMEM;
1635 
1636 	bytes_remaining = eeprom.len;
1637 	while (bytes_remaining > 0) {
1638 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1639 
1640 		ret = getter(dev, &eeprom, data);
1641 		if (ret)
1642 			break;
1643 		if (!eeprom.len) {
1644 			ret = -EIO;
1645 			break;
1646 		}
1647 		if (copy_to_user(userbuf, data, eeprom.len)) {
1648 			ret = -EFAULT;
1649 			break;
1650 		}
1651 		userbuf += eeprom.len;
1652 		eeprom.offset += eeprom.len;
1653 		bytes_remaining -= eeprom.len;
1654 	}
1655 
1656 	eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1657 	eeprom.offset -= eeprom.len;
1658 	if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1659 		ret = -EFAULT;
1660 
1661 	kfree(data);
1662 	return ret;
1663 }
1664 
1665 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1666 {
1667 	const struct ethtool_ops *ops = dev->ethtool_ops;
1668 
1669 	if (!ops->get_eeprom || !ops->get_eeprom_len ||
1670 	    !ops->get_eeprom_len(dev))
1671 		return -EOPNOTSUPP;
1672 
1673 	return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1674 				      ops->get_eeprom_len(dev));
1675 }
1676 
1677 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1678 {
1679 	struct ethtool_eeprom eeprom;
1680 	const struct ethtool_ops *ops = dev->ethtool_ops;
1681 	void __user *userbuf = useraddr + sizeof(eeprom);
1682 	u32 bytes_remaining;
1683 	u8 *data;
1684 	int ret = 0;
1685 
1686 	if (!ops->set_eeprom || !ops->get_eeprom_len ||
1687 	    !ops->get_eeprom_len(dev))
1688 		return -EOPNOTSUPP;
1689 
1690 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1691 		return -EFAULT;
1692 
1693 	/* Check for wrap and zero */
1694 	if (eeprom.offset + eeprom.len <= eeprom.offset)
1695 		return -EINVAL;
1696 
1697 	/* Check for exceeding total eeprom len */
1698 	if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1699 		return -EINVAL;
1700 
1701 	data = kzalloc(PAGE_SIZE, GFP_USER);
1702 	if (!data)
1703 		return -ENOMEM;
1704 
1705 	bytes_remaining = eeprom.len;
1706 	while (bytes_remaining > 0) {
1707 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1708 
1709 		if (copy_from_user(data, userbuf, eeprom.len)) {
1710 			ret = -EFAULT;
1711 			break;
1712 		}
1713 		ret = ops->set_eeprom(dev, &eeprom, data);
1714 		if (ret)
1715 			break;
1716 		userbuf += eeprom.len;
1717 		eeprom.offset += eeprom.len;
1718 		bytes_remaining -= eeprom.len;
1719 	}
1720 
1721 	kfree(data);
1722 	return ret;
1723 }
1724 
1725 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1726 						   void __user *useraddr)
1727 {
1728 	struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1729 	struct kernel_ethtool_coalesce kernel_coalesce = {};
1730 	int ret;
1731 
1732 	if (!dev->ethtool_ops->get_coalesce)
1733 		return -EOPNOTSUPP;
1734 
1735 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1736 					     NULL);
1737 	if (ret)
1738 		return ret;
1739 
1740 	if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1741 		return -EFAULT;
1742 	return 0;
1743 }
1744 
1745 static bool
1746 ethtool_set_coalesce_supported(struct net_device *dev,
1747 			       struct ethtool_coalesce *coalesce)
1748 {
1749 	u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1750 	u32 nonzero_params = 0;
1751 
1752 	if (coalesce->rx_coalesce_usecs)
1753 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1754 	if (coalesce->rx_max_coalesced_frames)
1755 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1756 	if (coalesce->rx_coalesce_usecs_irq)
1757 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1758 	if (coalesce->rx_max_coalesced_frames_irq)
1759 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1760 	if (coalesce->tx_coalesce_usecs)
1761 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1762 	if (coalesce->tx_max_coalesced_frames)
1763 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1764 	if (coalesce->tx_coalesce_usecs_irq)
1765 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1766 	if (coalesce->tx_max_coalesced_frames_irq)
1767 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1768 	if (coalesce->stats_block_coalesce_usecs)
1769 		nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1770 	if (coalesce->use_adaptive_rx_coalesce)
1771 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1772 	if (coalesce->use_adaptive_tx_coalesce)
1773 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1774 	if (coalesce->pkt_rate_low)
1775 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1776 	if (coalesce->rx_coalesce_usecs_low)
1777 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1778 	if (coalesce->rx_max_coalesced_frames_low)
1779 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1780 	if (coalesce->tx_coalesce_usecs_low)
1781 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1782 	if (coalesce->tx_max_coalesced_frames_low)
1783 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1784 	if (coalesce->pkt_rate_high)
1785 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1786 	if (coalesce->rx_coalesce_usecs_high)
1787 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1788 	if (coalesce->rx_max_coalesced_frames_high)
1789 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1790 	if (coalesce->tx_coalesce_usecs_high)
1791 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1792 	if (coalesce->tx_max_coalesced_frames_high)
1793 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1794 	if (coalesce->rate_sample_interval)
1795 		nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1796 
1797 	return (supported_params & nonzero_params) == nonzero_params;
1798 }
1799 
1800 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1801 						   void __user *useraddr)
1802 {
1803 	struct kernel_ethtool_coalesce kernel_coalesce = {};
1804 	struct ethtool_coalesce coalesce;
1805 	int ret;
1806 
1807 	if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1808 		return -EOPNOTSUPP;
1809 
1810 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1811 					     NULL);
1812 	if (ret)
1813 		return ret;
1814 
1815 	if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1816 		return -EFAULT;
1817 
1818 	if (!ethtool_set_coalesce_supported(dev, &coalesce))
1819 		return -EOPNOTSUPP;
1820 
1821 	ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1822 					     NULL);
1823 	if (!ret)
1824 		ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1825 	return ret;
1826 }
1827 
1828 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1829 {
1830 	struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1831 	struct kernel_ethtool_ringparam kernel_ringparam = {};
1832 
1833 	if (!dev->ethtool_ops->get_ringparam)
1834 		return -EOPNOTSUPP;
1835 
1836 	dev->ethtool_ops->get_ringparam(dev, &ringparam,
1837 					&kernel_ringparam, NULL);
1838 
1839 	if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1840 		return -EFAULT;
1841 	return 0;
1842 }
1843 
1844 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1845 {
1846 	struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1847 	struct kernel_ethtool_ringparam kernel_ringparam;
1848 	int ret;
1849 
1850 	if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1851 		return -EOPNOTSUPP;
1852 
1853 	if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1854 		return -EFAULT;
1855 
1856 	dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
1857 
1858 	/* ensure new ring parameters are within the maximums */
1859 	if (ringparam.rx_pending > max.rx_max_pending ||
1860 	    ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1861 	    ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1862 	    ringparam.tx_pending > max.tx_max_pending)
1863 		return -EINVAL;
1864 
1865 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1866 					      &kernel_ringparam, NULL);
1867 	if (!ret)
1868 		ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1869 	return ret;
1870 }
1871 
1872 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1873 						   void __user *useraddr)
1874 {
1875 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1876 
1877 	if (!dev->ethtool_ops->get_channels)
1878 		return -EOPNOTSUPP;
1879 
1880 	dev->ethtool_ops->get_channels(dev, &channels);
1881 
1882 	if (copy_to_user(useraddr, &channels, sizeof(channels)))
1883 		return -EFAULT;
1884 	return 0;
1885 }
1886 
1887 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1888 						   void __user *useraddr)
1889 {
1890 	struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1891 	u16 from_channel, to_channel;
1892 	u64 max_rxnfc_in_use;
1893 	u32 max_rxfh_in_use;
1894 	unsigned int i;
1895 	int ret;
1896 
1897 	if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1898 		return -EOPNOTSUPP;
1899 
1900 	if (copy_from_user(&channels, useraddr, sizeof(channels)))
1901 		return -EFAULT;
1902 
1903 	dev->ethtool_ops->get_channels(dev, &curr);
1904 
1905 	if (channels.rx_count == curr.rx_count &&
1906 	    channels.tx_count == curr.tx_count &&
1907 	    channels.combined_count == curr.combined_count &&
1908 	    channels.other_count == curr.other_count)
1909 		return 0;
1910 
1911 	/* ensure new counts are within the maximums */
1912 	if (channels.rx_count > curr.max_rx ||
1913 	    channels.tx_count > curr.max_tx ||
1914 	    channels.combined_count > curr.max_combined ||
1915 	    channels.other_count > curr.max_other)
1916 		return -EINVAL;
1917 
1918 	/* ensure there is at least one RX and one TX channel */
1919 	if (!channels.combined_count &&
1920 	    (!channels.rx_count || !channels.tx_count))
1921 		return -EINVAL;
1922 
1923 	/* ensure the new Rx count fits within the configured Rx flow
1924 	 * indirection table/rxnfc settings */
1925 	if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
1926 		max_rxnfc_in_use = 0;
1927 	if (!netif_is_rxfh_configured(dev) ||
1928 	    ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use))
1929 		max_rxfh_in_use = 0;
1930 	if (channels.combined_count + channels.rx_count <=
1931 	    max_t(u64, max_rxnfc_in_use, max_rxfh_in_use))
1932 		return -EINVAL;
1933 
1934 	/* Disabling channels, query zero-copy AF_XDP sockets */
1935 	from_channel = channels.combined_count +
1936 		min(channels.rx_count, channels.tx_count);
1937 	to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1938 	for (i = from_channel; i < to_channel; i++)
1939 		if (xsk_get_pool_from_qid(dev, i))
1940 			return -EINVAL;
1941 
1942 	ret = dev->ethtool_ops->set_channels(dev, &channels);
1943 	if (!ret)
1944 		ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1945 	return ret;
1946 }
1947 
1948 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1949 {
1950 	struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1951 
1952 	if (!dev->ethtool_ops->get_pauseparam)
1953 		return -EOPNOTSUPP;
1954 
1955 	dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1956 
1957 	if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1958 		return -EFAULT;
1959 	return 0;
1960 }
1961 
1962 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1963 {
1964 	struct ethtool_pauseparam pauseparam;
1965 	int ret;
1966 
1967 	if (!dev->ethtool_ops->set_pauseparam)
1968 		return -EOPNOTSUPP;
1969 
1970 	if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1971 		return -EFAULT;
1972 
1973 	ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1974 	if (!ret)
1975 		ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1976 	return ret;
1977 }
1978 
1979 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1980 {
1981 	struct ethtool_test test;
1982 	const struct ethtool_ops *ops = dev->ethtool_ops;
1983 	u64 *data;
1984 	int ret, test_len;
1985 
1986 	if (!ops->self_test || !ops->get_sset_count)
1987 		return -EOPNOTSUPP;
1988 
1989 	test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1990 	if (test_len < 0)
1991 		return test_len;
1992 	WARN_ON(test_len == 0);
1993 
1994 	if (copy_from_user(&test, useraddr, sizeof(test)))
1995 		return -EFAULT;
1996 
1997 	test.len = test_len;
1998 	data = kcalloc(test_len, sizeof(u64), GFP_USER);
1999 	if (!data)
2000 		return -ENOMEM;
2001 
2002 	netif_testing_on(dev);
2003 	ops->self_test(dev, &test, data);
2004 	netif_testing_off(dev);
2005 
2006 	ret = -EFAULT;
2007 	if (copy_to_user(useraddr, &test, sizeof(test)))
2008 		goto out;
2009 	useraddr += sizeof(test);
2010 	if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
2011 		goto out;
2012 	ret = 0;
2013 
2014  out:
2015 	kfree(data);
2016 	return ret;
2017 }
2018 
2019 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
2020 {
2021 	struct ethtool_gstrings gstrings;
2022 	u8 *data;
2023 	int ret;
2024 
2025 	if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
2026 		return -EFAULT;
2027 
2028 	ret = __ethtool_get_sset_count(dev, gstrings.string_set);
2029 	if (ret < 0)
2030 		return ret;
2031 	if (ret > S32_MAX / ETH_GSTRING_LEN)
2032 		return -ENOMEM;
2033 	WARN_ON_ONCE(!ret);
2034 
2035 	gstrings.len = ret;
2036 
2037 	if (gstrings.len) {
2038 		data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
2039 		if (!data)
2040 			return -ENOMEM;
2041 
2042 		__ethtool_get_strings(dev, gstrings.string_set, data);
2043 	} else {
2044 		data = NULL;
2045 	}
2046 
2047 	ret = -EFAULT;
2048 	if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
2049 		goto out;
2050 	useraddr += sizeof(gstrings);
2051 	if (gstrings.len &&
2052 	    copy_to_user(useraddr, data,
2053 			 array_size(gstrings.len, ETH_GSTRING_LEN)))
2054 		goto out;
2055 	ret = 0;
2056 
2057 out:
2058 	vfree(data);
2059 	return ret;
2060 }
2061 
2062 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
2063 {
2064 	va_list args;
2065 
2066 	va_start(args, fmt);
2067 	vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
2068 	va_end(args);
2069 
2070 	*data += ETH_GSTRING_LEN;
2071 }
2072 EXPORT_SYMBOL(ethtool_sprintf);
2073 
2074 void ethtool_puts(u8 **data, const char *str)
2075 {
2076 	strscpy(*data, str, ETH_GSTRING_LEN);
2077 	*data += ETH_GSTRING_LEN;
2078 }
2079 EXPORT_SYMBOL(ethtool_puts);
2080 
2081 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
2082 {
2083 	struct ethtool_value id;
2084 	static bool busy;
2085 	const struct ethtool_ops *ops = dev->ethtool_ops;
2086 	netdevice_tracker dev_tracker;
2087 	int rc;
2088 
2089 	if (!ops->set_phys_id)
2090 		return -EOPNOTSUPP;
2091 
2092 	if (busy)
2093 		return -EBUSY;
2094 
2095 	if (copy_from_user(&id, useraddr, sizeof(id)))
2096 		return -EFAULT;
2097 
2098 	rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2099 	if (rc < 0)
2100 		return rc;
2101 
2102 	/* Drop the RTNL lock while waiting, but prevent reentry or
2103 	 * removal of the device.
2104 	 */
2105 	busy = true;
2106 	netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2107 	rtnl_unlock();
2108 
2109 	if (rc == 0) {
2110 		/* Driver will handle this itself */
2111 		schedule_timeout_interruptible(
2112 			id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2113 	} else {
2114 		/* Driver expects to be called at twice the frequency in rc */
2115 		int n = rc * 2, interval = HZ / n;
2116 		u64 count = mul_u32_u32(n, id.data);
2117 		u64 i = 0;
2118 
2119 		do {
2120 			rtnl_lock();
2121 			rc = ops->set_phys_id(dev,
2122 				    (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2123 			rtnl_unlock();
2124 			if (rc)
2125 				break;
2126 			schedule_timeout_interruptible(interval);
2127 		} while (!signal_pending(current) && (!id.data || i < count));
2128 	}
2129 
2130 	rtnl_lock();
2131 	netdev_put(dev, &dev_tracker);
2132 	busy = false;
2133 
2134 	(void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2135 	return rc;
2136 }
2137 
2138 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2139 {
2140 	struct ethtool_stats stats;
2141 	const struct ethtool_ops *ops = dev->ethtool_ops;
2142 	u64 *data;
2143 	int ret, n_stats;
2144 
2145 	if (!ops->get_ethtool_stats || !ops->get_sset_count)
2146 		return -EOPNOTSUPP;
2147 
2148 	n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2149 	if (n_stats < 0)
2150 		return n_stats;
2151 	if (n_stats > S32_MAX / sizeof(u64))
2152 		return -ENOMEM;
2153 	WARN_ON_ONCE(!n_stats);
2154 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
2155 		return -EFAULT;
2156 
2157 	stats.n_stats = n_stats;
2158 
2159 	if (n_stats) {
2160 		data = vzalloc(array_size(n_stats, sizeof(u64)));
2161 		if (!data)
2162 			return -ENOMEM;
2163 		ops->get_ethtool_stats(dev, &stats, data);
2164 	} else {
2165 		data = NULL;
2166 	}
2167 
2168 	ret = -EFAULT;
2169 	if (copy_to_user(useraddr, &stats, sizeof(stats)))
2170 		goto out;
2171 	useraddr += sizeof(stats);
2172 	if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2173 		goto out;
2174 	ret = 0;
2175 
2176  out:
2177 	vfree(data);
2178 	return ret;
2179 }
2180 
2181 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
2182 {
2183 	if (n_stats < 0)
2184 		return n_stats;
2185 	if (n_stats > S32_MAX / sizeof(u64))
2186 		return -ENOMEM;
2187 	if (WARN_ON_ONCE(!n_stats))
2188 		return -EOPNOTSUPP;
2189 
2190 	*data = vzalloc(array_size(n_stats, sizeof(u64)));
2191 	if (!*data)
2192 		return -ENOMEM;
2193 
2194 	return 0;
2195 }
2196 
2197 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2198 					 struct ethtool_stats *stats,
2199 					 u64 **data)
2200  {
2201 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2202 	int n_stats, ret;
2203 
2204 	if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2205 		return -EOPNOTSUPP;
2206 
2207 	n_stats = phy_ops->get_sset_count(phydev);
2208 
2209 	ret = ethtool_vzalloc_stats_array(n_stats, data);
2210 	if (ret)
2211 		return ret;
2212 
2213 	stats->n_stats = n_stats;
2214 	return phy_ops->get_stats(phydev, stats, *data);
2215 }
2216 
2217 static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2218 					  struct ethtool_stats *stats,
2219 					  u64 **data)
2220 {
2221 	const struct ethtool_ops *ops = dev->ethtool_ops;
2222 	int n_stats, ret;
2223 
2224 	if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats)
2225 		return -EOPNOTSUPP;
2226 
2227 	n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2228 
2229 	ret = ethtool_vzalloc_stats_array(n_stats, data);
2230 	if (ret)
2231 		return ret;
2232 
2233 	stats->n_stats = n_stats;
2234 	ops->get_ethtool_phy_stats(dev, stats, *data);
2235 
2236 	return 0;
2237 }
2238 
2239 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2240 {
2241 	struct phy_device *phydev = dev->phydev;
2242 	struct ethtool_stats stats;
2243 	u64 *data = NULL;
2244 	int ret = -EOPNOTSUPP;
2245 
2246 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
2247 		return -EFAULT;
2248 
2249 	if (phydev)
2250 		ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
2251 
2252 	if (ret == -EOPNOTSUPP)
2253 		ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
2254 
2255 	if (ret)
2256 		goto out;
2257 
2258 	if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2259 		ret = -EFAULT;
2260 		goto out;
2261 	}
2262 
2263 	useraddr += sizeof(stats);
2264 	if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2265 		ret = -EFAULT;
2266 
2267  out:
2268 	vfree(data);
2269 	return ret;
2270 }
2271 
2272 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2273 {
2274 	struct ethtool_perm_addr epaddr;
2275 
2276 	if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2277 		return -EFAULT;
2278 
2279 	if (epaddr.size < dev->addr_len)
2280 		return -ETOOSMALL;
2281 	epaddr.size = dev->addr_len;
2282 
2283 	if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2284 		return -EFAULT;
2285 	useraddr += sizeof(epaddr);
2286 	if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2287 		return -EFAULT;
2288 	return 0;
2289 }
2290 
2291 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2292 			     u32 cmd, u32 (*actor)(struct net_device *))
2293 {
2294 	struct ethtool_value edata = { .cmd = cmd };
2295 
2296 	if (!actor)
2297 		return -EOPNOTSUPP;
2298 
2299 	edata.data = actor(dev);
2300 
2301 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
2302 		return -EFAULT;
2303 	return 0;
2304 }
2305 
2306 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2307 			     void (*actor)(struct net_device *, u32))
2308 {
2309 	struct ethtool_value edata;
2310 
2311 	if (!actor)
2312 		return -EOPNOTSUPP;
2313 
2314 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
2315 		return -EFAULT;
2316 
2317 	actor(dev, edata.data);
2318 	return 0;
2319 }
2320 
2321 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2322 			     int (*actor)(struct net_device *, u32))
2323 {
2324 	struct ethtool_value edata;
2325 
2326 	if (!actor)
2327 		return -EOPNOTSUPP;
2328 
2329 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
2330 		return -EFAULT;
2331 
2332 	return actor(dev, edata.data);
2333 }
2334 
2335 static int
2336 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2337 {
2338 	if (!dev->ethtool_ops->flash_device) {
2339 		req->devlink = netdev_to_devlink_get(dev);
2340 		return 0;
2341 	}
2342 
2343 	return dev->ethtool_ops->flash_device(dev, &req->efl);
2344 }
2345 
2346 static int ethtool_set_dump(struct net_device *dev,
2347 			void __user *useraddr)
2348 {
2349 	struct ethtool_dump dump;
2350 
2351 	if (!dev->ethtool_ops->set_dump)
2352 		return -EOPNOTSUPP;
2353 
2354 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
2355 		return -EFAULT;
2356 
2357 	return dev->ethtool_ops->set_dump(dev, &dump);
2358 }
2359 
2360 static int ethtool_get_dump_flag(struct net_device *dev,
2361 				void __user *useraddr)
2362 {
2363 	int ret;
2364 	struct ethtool_dump dump;
2365 	const struct ethtool_ops *ops = dev->ethtool_ops;
2366 
2367 	if (!ops->get_dump_flag)
2368 		return -EOPNOTSUPP;
2369 
2370 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
2371 		return -EFAULT;
2372 
2373 	ret = ops->get_dump_flag(dev, &dump);
2374 	if (ret)
2375 		return ret;
2376 
2377 	if (copy_to_user(useraddr, &dump, sizeof(dump)))
2378 		return -EFAULT;
2379 	return 0;
2380 }
2381 
2382 static int ethtool_get_dump_data(struct net_device *dev,
2383 				void __user *useraddr)
2384 {
2385 	int ret;
2386 	__u32 len;
2387 	struct ethtool_dump dump, tmp;
2388 	const struct ethtool_ops *ops = dev->ethtool_ops;
2389 	void *data = NULL;
2390 
2391 	if (!ops->get_dump_data || !ops->get_dump_flag)
2392 		return -EOPNOTSUPP;
2393 
2394 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
2395 		return -EFAULT;
2396 
2397 	memset(&tmp, 0, sizeof(tmp));
2398 	tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2399 	ret = ops->get_dump_flag(dev, &tmp);
2400 	if (ret)
2401 		return ret;
2402 
2403 	len = min(tmp.len, dump.len);
2404 	if (!len)
2405 		return -EFAULT;
2406 
2407 	/* Don't ever let the driver think there's more space available
2408 	 * than it requested with .get_dump_flag().
2409 	 */
2410 	dump.len = len;
2411 
2412 	/* Always allocate enough space to hold the whole thing so that the
2413 	 * driver does not need to check the length and bother with partial
2414 	 * dumping.
2415 	 */
2416 	data = vzalloc(tmp.len);
2417 	if (!data)
2418 		return -ENOMEM;
2419 	ret = ops->get_dump_data(dev, &dump, data);
2420 	if (ret)
2421 		goto out;
2422 
2423 	/* There are two sane possibilities:
2424 	 * 1. The driver's .get_dump_data() does not touch dump.len.
2425 	 * 2. Or it may set dump.len to how much it really writes, which
2426 	 *    should be tmp.len (or len if it can do a partial dump).
2427 	 * In any case respond to userspace with the actual length of data
2428 	 * it's receiving.
2429 	 */
2430 	WARN_ON(dump.len != len && dump.len != tmp.len);
2431 	dump.len = len;
2432 
2433 	if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2434 		ret = -EFAULT;
2435 		goto out;
2436 	}
2437 	useraddr += offsetof(struct ethtool_dump, data);
2438 	if (copy_to_user(useraddr, data, len))
2439 		ret = -EFAULT;
2440 out:
2441 	vfree(data);
2442 	return ret;
2443 }
2444 
2445 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2446 {
2447 	struct ethtool_ts_info info;
2448 	int err;
2449 
2450 	err = __ethtool_get_ts_info(dev, &info);
2451 	if (err)
2452 		return err;
2453 
2454 	if (copy_to_user(useraddr, &info, sizeof(info)))
2455 		return -EFAULT;
2456 
2457 	return 0;
2458 }
2459 
2460 int ethtool_get_module_info_call(struct net_device *dev,
2461 				 struct ethtool_modinfo *modinfo)
2462 {
2463 	const struct ethtool_ops *ops = dev->ethtool_ops;
2464 	struct phy_device *phydev = dev->phydev;
2465 
2466 	if (dev->sfp_bus)
2467 		return sfp_get_module_info(dev->sfp_bus, modinfo);
2468 
2469 	if (phydev && phydev->drv && phydev->drv->module_info)
2470 		return phydev->drv->module_info(phydev, modinfo);
2471 
2472 	if (ops->get_module_info)
2473 		return ops->get_module_info(dev, modinfo);
2474 
2475 	return -EOPNOTSUPP;
2476 }
2477 
2478 static int ethtool_get_module_info(struct net_device *dev,
2479 				   void __user *useraddr)
2480 {
2481 	int ret;
2482 	struct ethtool_modinfo modinfo;
2483 
2484 	if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2485 		return -EFAULT;
2486 
2487 	ret = ethtool_get_module_info_call(dev, &modinfo);
2488 	if (ret)
2489 		return ret;
2490 
2491 	if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2492 		return -EFAULT;
2493 
2494 	return 0;
2495 }
2496 
2497 int ethtool_get_module_eeprom_call(struct net_device *dev,
2498 				   struct ethtool_eeprom *ee, u8 *data)
2499 {
2500 	const struct ethtool_ops *ops = dev->ethtool_ops;
2501 	struct phy_device *phydev = dev->phydev;
2502 
2503 	if (dev->sfp_bus)
2504 		return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2505 
2506 	if (phydev && phydev->drv && phydev->drv->module_eeprom)
2507 		return phydev->drv->module_eeprom(phydev, ee, data);
2508 
2509 	if (ops->get_module_eeprom)
2510 		return ops->get_module_eeprom(dev, ee, data);
2511 
2512 	return -EOPNOTSUPP;
2513 }
2514 
2515 static int ethtool_get_module_eeprom(struct net_device *dev,
2516 				     void __user *useraddr)
2517 {
2518 	int ret;
2519 	struct ethtool_modinfo modinfo;
2520 
2521 	ret = ethtool_get_module_info_call(dev, &modinfo);
2522 	if (ret)
2523 		return ret;
2524 
2525 	return ethtool_get_any_eeprom(dev, useraddr,
2526 				      ethtool_get_module_eeprom_call,
2527 				      modinfo.eeprom_len);
2528 }
2529 
2530 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2531 {
2532 	switch (tuna->id) {
2533 	case ETHTOOL_RX_COPYBREAK:
2534 	case ETHTOOL_TX_COPYBREAK:
2535 	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2536 		if (tuna->len != sizeof(u32) ||
2537 		    tuna->type_id != ETHTOOL_TUNABLE_U32)
2538 			return -EINVAL;
2539 		break;
2540 	case ETHTOOL_PFC_PREVENTION_TOUT:
2541 		if (tuna->len != sizeof(u16) ||
2542 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
2543 			return -EINVAL;
2544 		break;
2545 	default:
2546 		return -EINVAL;
2547 	}
2548 
2549 	return 0;
2550 }
2551 
2552 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2553 {
2554 	int ret;
2555 	struct ethtool_tunable tuna;
2556 	const struct ethtool_ops *ops = dev->ethtool_ops;
2557 	void *data;
2558 
2559 	if (!ops->get_tunable)
2560 		return -EOPNOTSUPP;
2561 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2562 		return -EFAULT;
2563 	ret = ethtool_tunable_valid(&tuna);
2564 	if (ret)
2565 		return ret;
2566 	data = kzalloc(tuna.len, GFP_USER);
2567 	if (!data)
2568 		return -ENOMEM;
2569 	ret = ops->get_tunable(dev, &tuna, data);
2570 	if (ret)
2571 		goto out;
2572 	useraddr += sizeof(tuna);
2573 	ret = -EFAULT;
2574 	if (copy_to_user(useraddr, data, tuna.len))
2575 		goto out;
2576 	ret = 0;
2577 
2578 out:
2579 	kfree(data);
2580 	return ret;
2581 }
2582 
2583 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2584 {
2585 	int ret;
2586 	struct ethtool_tunable tuna;
2587 	const struct ethtool_ops *ops = dev->ethtool_ops;
2588 	void *data;
2589 
2590 	if (!ops->set_tunable)
2591 		return -EOPNOTSUPP;
2592 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2593 		return -EFAULT;
2594 	ret = ethtool_tunable_valid(&tuna);
2595 	if (ret)
2596 		return ret;
2597 	useraddr += sizeof(tuna);
2598 	data = memdup_user(useraddr, tuna.len);
2599 	if (IS_ERR(data))
2600 		return PTR_ERR(data);
2601 	ret = ops->set_tunable(dev, &tuna, data);
2602 
2603 	kfree(data);
2604 	return ret;
2605 }
2606 
2607 static noinline_for_stack int
2608 ethtool_get_per_queue_coalesce(struct net_device *dev,
2609 			       void __user *useraddr,
2610 			       struct ethtool_per_queue_op *per_queue_opt)
2611 {
2612 	u32 bit;
2613 	int ret;
2614 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2615 
2616 	if (!dev->ethtool_ops->get_per_queue_coalesce)
2617 		return -EOPNOTSUPP;
2618 
2619 	useraddr += sizeof(*per_queue_opt);
2620 
2621 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2622 			  MAX_NUM_QUEUE);
2623 
2624 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2625 		struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2626 
2627 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2628 		if (ret != 0)
2629 			return ret;
2630 		if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2631 			return -EFAULT;
2632 		useraddr += sizeof(coalesce);
2633 	}
2634 
2635 	return 0;
2636 }
2637 
2638 static noinline_for_stack int
2639 ethtool_set_per_queue_coalesce(struct net_device *dev,
2640 			       void __user *useraddr,
2641 			       struct ethtool_per_queue_op *per_queue_opt)
2642 {
2643 	u32 bit;
2644 	int i, ret = 0;
2645 	int n_queue;
2646 	struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2647 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2648 
2649 	if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2650 	    (!dev->ethtool_ops->get_per_queue_coalesce))
2651 		return -EOPNOTSUPP;
2652 
2653 	useraddr += sizeof(*per_queue_opt);
2654 
2655 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2656 	n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2657 	tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2658 	if (!backup)
2659 		return -ENOMEM;
2660 
2661 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2662 		struct ethtool_coalesce coalesce;
2663 
2664 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2665 		if (ret != 0)
2666 			goto roll_back;
2667 
2668 		tmp++;
2669 
2670 		if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2671 			ret = -EFAULT;
2672 			goto roll_back;
2673 		}
2674 
2675 		if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2676 			ret = -EOPNOTSUPP;
2677 			goto roll_back;
2678 		}
2679 
2680 		ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2681 		if (ret != 0)
2682 			goto roll_back;
2683 
2684 		useraddr += sizeof(coalesce);
2685 	}
2686 
2687 roll_back:
2688 	if (ret != 0) {
2689 		tmp = backup;
2690 		for_each_set_bit(i, queue_mask, bit) {
2691 			dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2692 			tmp++;
2693 		}
2694 	}
2695 	kfree(backup);
2696 
2697 	return ret;
2698 }
2699 
2700 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2701 				 void __user *useraddr, u32 sub_cmd)
2702 {
2703 	struct ethtool_per_queue_op per_queue_opt;
2704 
2705 	if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2706 		return -EFAULT;
2707 
2708 	if (per_queue_opt.sub_command != sub_cmd)
2709 		return -EINVAL;
2710 
2711 	switch (per_queue_opt.sub_command) {
2712 	case ETHTOOL_GCOALESCE:
2713 		return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2714 	case ETHTOOL_SCOALESCE:
2715 		return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2716 	default:
2717 		return -EOPNOTSUPP;
2718 	}
2719 }
2720 
2721 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2722 {
2723 	switch (tuna->id) {
2724 	case ETHTOOL_PHY_DOWNSHIFT:
2725 	case ETHTOOL_PHY_FAST_LINK_DOWN:
2726 		if (tuna->len != sizeof(u8) ||
2727 		    tuna->type_id != ETHTOOL_TUNABLE_U8)
2728 			return -EINVAL;
2729 		break;
2730 	case ETHTOOL_PHY_EDPD:
2731 		if (tuna->len != sizeof(u16) ||
2732 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
2733 			return -EINVAL;
2734 		break;
2735 	default:
2736 		return -EINVAL;
2737 	}
2738 
2739 	return 0;
2740 }
2741 
2742 static int get_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->get_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 	data = kzalloc(tuna.len, GFP_USER);
2759 	if (!data)
2760 		return -ENOMEM;
2761 	if (phy_drv_tunable) {
2762 		mutex_lock(&phydev->lock);
2763 		ret = phydev->drv->get_tunable(phydev, &tuna, data);
2764 		mutex_unlock(&phydev->lock);
2765 	} else {
2766 		ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2767 	}
2768 	if (ret)
2769 		goto out;
2770 	useraddr += sizeof(tuna);
2771 	ret = -EFAULT;
2772 	if (copy_to_user(useraddr, data, tuna.len))
2773 		goto out;
2774 	ret = 0;
2775 
2776 out:
2777 	kfree(data);
2778 	return ret;
2779 }
2780 
2781 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2782 {
2783 	struct phy_device *phydev = dev->phydev;
2784 	struct ethtool_tunable tuna;
2785 	bool phy_drv_tunable;
2786 	void *data;
2787 	int ret;
2788 
2789 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2790 	if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2791 		return -EOPNOTSUPP;
2792 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2793 		return -EFAULT;
2794 	ret = ethtool_phy_tunable_valid(&tuna);
2795 	if (ret)
2796 		return ret;
2797 	useraddr += sizeof(tuna);
2798 	data = memdup_user(useraddr, tuna.len);
2799 	if (IS_ERR(data))
2800 		return PTR_ERR(data);
2801 	if (phy_drv_tunable) {
2802 		mutex_lock(&phydev->lock);
2803 		ret = phydev->drv->set_tunable(phydev, &tuna, data);
2804 		mutex_unlock(&phydev->lock);
2805 	} else {
2806 		ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2807 	}
2808 
2809 	kfree(data);
2810 	return ret;
2811 }
2812 
2813 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2814 {
2815 	struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2816 	int rc;
2817 
2818 	if (!dev->ethtool_ops->get_fecparam)
2819 		return -EOPNOTSUPP;
2820 
2821 	rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2822 	if (rc)
2823 		return rc;
2824 
2825 	if (WARN_ON_ONCE(fecparam.reserved))
2826 		fecparam.reserved = 0;
2827 
2828 	if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2829 		return -EFAULT;
2830 	return 0;
2831 }
2832 
2833 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2834 {
2835 	struct ethtool_fecparam fecparam;
2836 
2837 	if (!dev->ethtool_ops->set_fecparam)
2838 		return -EOPNOTSUPP;
2839 
2840 	if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2841 		return -EFAULT;
2842 
2843 	if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2844 		return -EINVAL;
2845 
2846 	fecparam.active_fec = 0;
2847 	fecparam.reserved = 0;
2848 
2849 	return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2850 }
2851 
2852 /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
2853 
2854 static int
2855 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2856 	      u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2857 {
2858 	struct net_device *dev;
2859 	u32 sub_cmd;
2860 	int rc;
2861 	netdev_features_t old_features;
2862 
2863 	dev = __dev_get_by_name(net, ifr->ifr_name);
2864 	if (!dev)
2865 		return -ENODEV;
2866 
2867 	if (ethcmd == ETHTOOL_PERQUEUE) {
2868 		if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2869 			return -EFAULT;
2870 	} else {
2871 		sub_cmd = ethcmd;
2872 	}
2873 	/* Allow some commands to be done by anyone */
2874 	switch (sub_cmd) {
2875 	case ETHTOOL_GSET:
2876 	case ETHTOOL_GDRVINFO:
2877 	case ETHTOOL_GMSGLVL:
2878 	case ETHTOOL_GLINK:
2879 	case ETHTOOL_GCOALESCE:
2880 	case ETHTOOL_GRINGPARAM:
2881 	case ETHTOOL_GPAUSEPARAM:
2882 	case ETHTOOL_GRXCSUM:
2883 	case ETHTOOL_GTXCSUM:
2884 	case ETHTOOL_GSG:
2885 	case ETHTOOL_GSSET_INFO:
2886 	case ETHTOOL_GSTRINGS:
2887 	case ETHTOOL_GSTATS:
2888 	case ETHTOOL_GPHYSTATS:
2889 	case ETHTOOL_GTSO:
2890 	case ETHTOOL_GPERMADDR:
2891 	case ETHTOOL_GUFO:
2892 	case ETHTOOL_GGSO:
2893 	case ETHTOOL_GGRO:
2894 	case ETHTOOL_GFLAGS:
2895 	case ETHTOOL_GPFLAGS:
2896 	case ETHTOOL_GRXFH:
2897 	case ETHTOOL_GRXRINGS:
2898 	case ETHTOOL_GRXCLSRLCNT:
2899 	case ETHTOOL_GRXCLSRULE:
2900 	case ETHTOOL_GRXCLSRLALL:
2901 	case ETHTOOL_GRXFHINDIR:
2902 	case ETHTOOL_GRSSH:
2903 	case ETHTOOL_GFEATURES:
2904 	case ETHTOOL_GCHANNELS:
2905 	case ETHTOOL_GET_TS_INFO:
2906 	case ETHTOOL_GEEE:
2907 	case ETHTOOL_GTUNABLE:
2908 	case ETHTOOL_PHY_GTUNABLE:
2909 	case ETHTOOL_GLINKSETTINGS:
2910 	case ETHTOOL_GFECPARAM:
2911 		break;
2912 	default:
2913 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2914 			return -EPERM;
2915 	}
2916 
2917 	if (dev->dev.parent)
2918 		pm_runtime_get_sync(dev->dev.parent);
2919 
2920 	if (!netif_device_present(dev)) {
2921 		rc = -ENODEV;
2922 		goto out;
2923 	}
2924 
2925 	if (dev->ethtool_ops->begin) {
2926 		rc = dev->ethtool_ops->begin(dev);
2927 		if (rc < 0)
2928 			goto out;
2929 	}
2930 	old_features = dev->features;
2931 
2932 	switch (ethcmd) {
2933 	case ETHTOOL_GSET:
2934 		rc = ethtool_get_settings(dev, useraddr);
2935 		break;
2936 	case ETHTOOL_SSET:
2937 		rc = ethtool_set_settings(dev, useraddr);
2938 		break;
2939 	case ETHTOOL_GDRVINFO:
2940 		rc = ethtool_get_drvinfo(dev, devlink_state);
2941 		break;
2942 	case ETHTOOL_GREGS:
2943 		rc = ethtool_get_regs(dev, useraddr);
2944 		break;
2945 	case ETHTOOL_GWOL:
2946 		rc = ethtool_get_wol(dev, useraddr);
2947 		break;
2948 	case ETHTOOL_SWOL:
2949 		rc = ethtool_set_wol(dev, useraddr);
2950 		break;
2951 	case ETHTOOL_GMSGLVL:
2952 		rc = ethtool_get_value(dev, useraddr, ethcmd,
2953 				       dev->ethtool_ops->get_msglevel);
2954 		break;
2955 	case ETHTOOL_SMSGLVL:
2956 		rc = ethtool_set_value_void(dev, useraddr,
2957 				       dev->ethtool_ops->set_msglevel);
2958 		if (!rc)
2959 			ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2960 		break;
2961 	case ETHTOOL_GEEE:
2962 		rc = ethtool_get_eee(dev, useraddr);
2963 		break;
2964 	case ETHTOOL_SEEE:
2965 		rc = ethtool_set_eee(dev, useraddr);
2966 		break;
2967 	case ETHTOOL_NWAY_RST:
2968 		rc = ethtool_nway_reset(dev);
2969 		break;
2970 	case ETHTOOL_GLINK:
2971 		rc = ethtool_get_link(dev, useraddr);
2972 		break;
2973 	case ETHTOOL_GEEPROM:
2974 		rc = ethtool_get_eeprom(dev, useraddr);
2975 		break;
2976 	case ETHTOOL_SEEPROM:
2977 		rc = ethtool_set_eeprom(dev, useraddr);
2978 		break;
2979 	case ETHTOOL_GCOALESCE:
2980 		rc = ethtool_get_coalesce(dev, useraddr);
2981 		break;
2982 	case ETHTOOL_SCOALESCE:
2983 		rc = ethtool_set_coalesce(dev, useraddr);
2984 		break;
2985 	case ETHTOOL_GRINGPARAM:
2986 		rc = ethtool_get_ringparam(dev, useraddr);
2987 		break;
2988 	case ETHTOOL_SRINGPARAM:
2989 		rc = ethtool_set_ringparam(dev, useraddr);
2990 		break;
2991 	case ETHTOOL_GPAUSEPARAM:
2992 		rc = ethtool_get_pauseparam(dev, useraddr);
2993 		break;
2994 	case ETHTOOL_SPAUSEPARAM:
2995 		rc = ethtool_set_pauseparam(dev, useraddr);
2996 		break;
2997 	case ETHTOOL_TEST:
2998 		rc = ethtool_self_test(dev, useraddr);
2999 		break;
3000 	case ETHTOOL_GSTRINGS:
3001 		rc = ethtool_get_strings(dev, useraddr);
3002 		break;
3003 	case ETHTOOL_PHYS_ID:
3004 		rc = ethtool_phys_id(dev, useraddr);
3005 		break;
3006 	case ETHTOOL_GSTATS:
3007 		rc = ethtool_get_stats(dev, useraddr);
3008 		break;
3009 	case ETHTOOL_GPERMADDR:
3010 		rc = ethtool_get_perm_addr(dev, useraddr);
3011 		break;
3012 	case ETHTOOL_GFLAGS:
3013 		rc = ethtool_get_value(dev, useraddr, ethcmd,
3014 					__ethtool_get_flags);
3015 		break;
3016 	case ETHTOOL_SFLAGS:
3017 		rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3018 		break;
3019 	case ETHTOOL_GPFLAGS:
3020 		rc = ethtool_get_value(dev, useraddr, ethcmd,
3021 				       dev->ethtool_ops->get_priv_flags);
3022 		if (!rc)
3023 			ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
3024 		break;
3025 	case ETHTOOL_SPFLAGS:
3026 		rc = ethtool_set_value(dev, useraddr,
3027 				       dev->ethtool_ops->set_priv_flags);
3028 		break;
3029 	case ETHTOOL_GRXFH:
3030 	case ETHTOOL_GRXRINGS:
3031 	case ETHTOOL_GRXCLSRLCNT:
3032 	case ETHTOOL_GRXCLSRULE:
3033 	case ETHTOOL_GRXCLSRLALL:
3034 		rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
3035 		break;
3036 	case ETHTOOL_SRXFH:
3037 	case ETHTOOL_SRXCLSRLDEL:
3038 	case ETHTOOL_SRXCLSRLINS:
3039 		rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
3040 		break;
3041 	case ETHTOOL_FLASHDEV:
3042 		rc = ethtool_flash_device(dev, devlink_state);
3043 		break;
3044 	case ETHTOOL_RESET:
3045 		rc = ethtool_reset(dev, useraddr);
3046 		break;
3047 	case ETHTOOL_GSSET_INFO:
3048 		rc = ethtool_get_sset_info(dev, useraddr);
3049 		break;
3050 	case ETHTOOL_GRXFHINDIR:
3051 		rc = ethtool_get_rxfh_indir(dev, useraddr);
3052 		break;
3053 	case ETHTOOL_SRXFHINDIR:
3054 		rc = ethtool_set_rxfh_indir(dev, useraddr);
3055 		break;
3056 	case ETHTOOL_GRSSH:
3057 		rc = ethtool_get_rxfh(dev, useraddr);
3058 		break;
3059 	case ETHTOOL_SRSSH:
3060 		rc = ethtool_set_rxfh(dev, useraddr);
3061 		break;
3062 	case ETHTOOL_GFEATURES:
3063 		rc = ethtool_get_features(dev, useraddr);
3064 		break;
3065 	case ETHTOOL_SFEATURES:
3066 		rc = ethtool_set_features(dev, useraddr);
3067 		break;
3068 	case ETHTOOL_GTXCSUM:
3069 	case ETHTOOL_GRXCSUM:
3070 	case ETHTOOL_GSG:
3071 	case ETHTOOL_GTSO:
3072 	case ETHTOOL_GGSO:
3073 	case ETHTOOL_GGRO:
3074 		rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
3075 		break;
3076 	case ETHTOOL_STXCSUM:
3077 	case ETHTOOL_SRXCSUM:
3078 	case ETHTOOL_SSG:
3079 	case ETHTOOL_STSO:
3080 	case ETHTOOL_SGSO:
3081 	case ETHTOOL_SGRO:
3082 		rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
3083 		break;
3084 	case ETHTOOL_GCHANNELS:
3085 		rc = ethtool_get_channels(dev, useraddr);
3086 		break;
3087 	case ETHTOOL_SCHANNELS:
3088 		rc = ethtool_set_channels(dev, useraddr);
3089 		break;
3090 	case ETHTOOL_SET_DUMP:
3091 		rc = ethtool_set_dump(dev, useraddr);
3092 		break;
3093 	case ETHTOOL_GET_DUMP_FLAG:
3094 		rc = ethtool_get_dump_flag(dev, useraddr);
3095 		break;
3096 	case ETHTOOL_GET_DUMP_DATA:
3097 		rc = ethtool_get_dump_data(dev, useraddr);
3098 		break;
3099 	case ETHTOOL_GET_TS_INFO:
3100 		rc = ethtool_get_ts_info(dev, useraddr);
3101 		break;
3102 	case ETHTOOL_GMODULEINFO:
3103 		rc = ethtool_get_module_info(dev, useraddr);
3104 		break;
3105 	case ETHTOOL_GMODULEEEPROM:
3106 		rc = ethtool_get_module_eeprom(dev, useraddr);
3107 		break;
3108 	case ETHTOOL_GTUNABLE:
3109 		rc = ethtool_get_tunable(dev, useraddr);
3110 		break;
3111 	case ETHTOOL_STUNABLE:
3112 		rc = ethtool_set_tunable(dev, useraddr);
3113 		break;
3114 	case ETHTOOL_GPHYSTATS:
3115 		rc = ethtool_get_phy_stats(dev, useraddr);
3116 		break;
3117 	case ETHTOOL_PERQUEUE:
3118 		rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
3119 		break;
3120 	case ETHTOOL_GLINKSETTINGS:
3121 		rc = ethtool_get_link_ksettings(dev, useraddr);
3122 		break;
3123 	case ETHTOOL_SLINKSETTINGS:
3124 		rc = ethtool_set_link_ksettings(dev, useraddr);
3125 		break;
3126 	case ETHTOOL_PHY_GTUNABLE:
3127 		rc = get_phy_tunable(dev, useraddr);
3128 		break;
3129 	case ETHTOOL_PHY_STUNABLE:
3130 		rc = set_phy_tunable(dev, useraddr);
3131 		break;
3132 	case ETHTOOL_GFECPARAM:
3133 		rc = ethtool_get_fecparam(dev, useraddr);
3134 		break;
3135 	case ETHTOOL_SFECPARAM:
3136 		rc = ethtool_set_fecparam(dev, useraddr);
3137 		break;
3138 	default:
3139 		rc = -EOPNOTSUPP;
3140 	}
3141 
3142 	if (dev->ethtool_ops->complete)
3143 		dev->ethtool_ops->complete(dev);
3144 
3145 	if (old_features != dev->features)
3146 		netdev_features_change(dev);
3147 out:
3148 	if (dev->dev.parent)
3149 		pm_runtime_put(dev->dev.parent);
3150 
3151 	return rc;
3152 }
3153 
3154 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3155 {
3156 	struct ethtool_devlink_compat *state;
3157 	u32 ethcmd;
3158 	int rc;
3159 
3160 	if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3161 		return -EFAULT;
3162 
3163 	state = kzalloc(sizeof(*state), GFP_KERNEL);
3164 	if (!state)
3165 		return -ENOMEM;
3166 
3167 	switch (ethcmd) {
3168 	case ETHTOOL_FLASHDEV:
3169 		if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3170 			rc = -EFAULT;
3171 			goto exit_free;
3172 		}
3173 		state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3174 		break;
3175 	}
3176 
3177 	rtnl_lock();
3178 	rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3179 	rtnl_unlock();
3180 	if (rc)
3181 		goto exit_free;
3182 
3183 	switch (ethcmd) {
3184 	case ETHTOOL_FLASHDEV:
3185 		if (state->devlink)
3186 			rc = devlink_compat_flash_update(state->devlink,
3187 							 state->efl.data);
3188 		break;
3189 	case ETHTOOL_GDRVINFO:
3190 		if (state->devlink)
3191 			devlink_compat_running_version(state->devlink,
3192 						       state->info.fw_version,
3193 						       sizeof(state->info.fw_version));
3194 		if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3195 			rc = -EFAULT;
3196 			goto exit_free;
3197 		}
3198 		break;
3199 	}
3200 
3201 exit_free:
3202 	if (state->devlink)
3203 		devlink_put(state->devlink);
3204 	kfree(state);
3205 	return rc;
3206 }
3207 
3208 struct ethtool_rx_flow_key {
3209 	struct flow_dissector_key_basic			basic;
3210 	union {
3211 		struct flow_dissector_key_ipv4_addrs	ipv4;
3212 		struct flow_dissector_key_ipv6_addrs	ipv6;
3213 	};
3214 	struct flow_dissector_key_ports			tp;
3215 	struct flow_dissector_key_ip			ip;
3216 	struct flow_dissector_key_vlan			vlan;
3217 	struct flow_dissector_key_eth_addrs		eth_addrs;
3218 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3219 
3220 struct ethtool_rx_flow_match {
3221 	struct flow_dissector		dissector;
3222 	struct ethtool_rx_flow_key	key;
3223 	struct ethtool_rx_flow_key	mask;
3224 };
3225 
3226 struct ethtool_rx_flow_rule *
3227 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3228 {
3229 	const struct ethtool_rx_flow_spec *fs = input->fs;
3230 	struct ethtool_rx_flow_match *match;
3231 	struct ethtool_rx_flow_rule *flow;
3232 	struct flow_action_entry *act;
3233 
3234 	flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3235 		       sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3236 	if (!flow)
3237 		return ERR_PTR(-ENOMEM);
3238 
3239 	/* ethtool_rx supports only one single action per rule. */
3240 	flow->rule = flow_rule_alloc(1);
3241 	if (!flow->rule) {
3242 		kfree(flow);
3243 		return ERR_PTR(-ENOMEM);
3244 	}
3245 
3246 	match = (struct ethtool_rx_flow_match *)flow->priv;
3247 	flow->rule->match.dissector	= &match->dissector;
3248 	flow->rule->match.mask		= &match->mask;
3249 	flow->rule->match.key		= &match->key;
3250 
3251 	match->mask.basic.n_proto = htons(0xffff);
3252 
3253 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3254 	case ETHER_FLOW: {
3255 		const struct ethhdr *ether_spec, *ether_m_spec;
3256 
3257 		ether_spec = &fs->h_u.ether_spec;
3258 		ether_m_spec = &fs->m_u.ether_spec;
3259 
3260 		if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3261 			ether_addr_copy(match->key.eth_addrs.src,
3262 					ether_spec->h_source);
3263 			ether_addr_copy(match->mask.eth_addrs.src,
3264 					ether_m_spec->h_source);
3265 		}
3266 		if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3267 			ether_addr_copy(match->key.eth_addrs.dst,
3268 					ether_spec->h_dest);
3269 			ether_addr_copy(match->mask.eth_addrs.dst,
3270 					ether_m_spec->h_dest);
3271 		}
3272 		if (ether_m_spec->h_proto) {
3273 			match->key.basic.n_proto = ether_spec->h_proto;
3274 			match->mask.basic.n_proto = ether_m_spec->h_proto;
3275 		}
3276 		}
3277 		break;
3278 	case TCP_V4_FLOW:
3279 	case UDP_V4_FLOW: {
3280 		const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3281 
3282 		match->key.basic.n_proto = htons(ETH_P_IP);
3283 
3284 		v4_spec = &fs->h_u.tcp_ip4_spec;
3285 		v4_m_spec = &fs->m_u.tcp_ip4_spec;
3286 
3287 		if (v4_m_spec->ip4src) {
3288 			match->key.ipv4.src = v4_spec->ip4src;
3289 			match->mask.ipv4.src = v4_m_spec->ip4src;
3290 		}
3291 		if (v4_m_spec->ip4dst) {
3292 			match->key.ipv4.dst = v4_spec->ip4dst;
3293 			match->mask.ipv4.dst = v4_m_spec->ip4dst;
3294 		}
3295 		if (v4_m_spec->ip4src ||
3296 		    v4_m_spec->ip4dst) {
3297 			match->dissector.used_keys |=
3298 				BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3299 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3300 				offsetof(struct ethtool_rx_flow_key, ipv4);
3301 		}
3302 		if (v4_m_spec->psrc) {
3303 			match->key.tp.src = v4_spec->psrc;
3304 			match->mask.tp.src = v4_m_spec->psrc;
3305 		}
3306 		if (v4_m_spec->pdst) {
3307 			match->key.tp.dst = v4_spec->pdst;
3308 			match->mask.tp.dst = v4_m_spec->pdst;
3309 		}
3310 		if (v4_m_spec->psrc ||
3311 		    v4_m_spec->pdst) {
3312 			match->dissector.used_keys |=
3313 				BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3314 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3315 				offsetof(struct ethtool_rx_flow_key, tp);
3316 		}
3317 		if (v4_m_spec->tos) {
3318 			match->key.ip.tos = v4_spec->tos;
3319 			match->mask.ip.tos = v4_m_spec->tos;
3320 			match->dissector.used_keys |=
3321 				BIT(FLOW_DISSECTOR_KEY_IP);
3322 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3323 				offsetof(struct ethtool_rx_flow_key, ip);
3324 		}
3325 		}
3326 		break;
3327 	case TCP_V6_FLOW:
3328 	case UDP_V6_FLOW: {
3329 		const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3330 
3331 		match->key.basic.n_proto = htons(ETH_P_IPV6);
3332 
3333 		v6_spec = &fs->h_u.tcp_ip6_spec;
3334 		v6_m_spec = &fs->m_u.tcp_ip6_spec;
3335 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
3336 			memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3337 			       sizeof(match->key.ipv6.src));
3338 			memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3339 			       sizeof(match->mask.ipv6.src));
3340 		}
3341 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3342 			memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3343 			       sizeof(match->key.ipv6.dst));
3344 			memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3345 			       sizeof(match->mask.ipv6.dst));
3346 		}
3347 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3348 		    !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3349 			match->dissector.used_keys |=
3350 				BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3351 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3352 				offsetof(struct ethtool_rx_flow_key, ipv6);
3353 		}
3354 		if (v6_m_spec->psrc) {
3355 			match->key.tp.src = v6_spec->psrc;
3356 			match->mask.tp.src = v6_m_spec->psrc;
3357 		}
3358 		if (v6_m_spec->pdst) {
3359 			match->key.tp.dst = v6_spec->pdst;
3360 			match->mask.tp.dst = v6_m_spec->pdst;
3361 		}
3362 		if (v6_m_spec->psrc ||
3363 		    v6_m_spec->pdst) {
3364 			match->dissector.used_keys |=
3365 				BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3366 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3367 				offsetof(struct ethtool_rx_flow_key, tp);
3368 		}
3369 		if (v6_m_spec->tclass) {
3370 			match->key.ip.tos = v6_spec->tclass;
3371 			match->mask.ip.tos = v6_m_spec->tclass;
3372 			match->dissector.used_keys |=
3373 				BIT_ULL(FLOW_DISSECTOR_KEY_IP);
3374 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3375 				offsetof(struct ethtool_rx_flow_key, ip);
3376 		}
3377 		}
3378 		break;
3379 	default:
3380 		ethtool_rx_flow_rule_destroy(flow);
3381 		return ERR_PTR(-EINVAL);
3382 	}
3383 
3384 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3385 	case TCP_V4_FLOW:
3386 	case TCP_V6_FLOW:
3387 		match->key.basic.ip_proto = IPPROTO_TCP;
3388 		match->mask.basic.ip_proto = 0xff;
3389 		break;
3390 	case UDP_V4_FLOW:
3391 	case UDP_V6_FLOW:
3392 		match->key.basic.ip_proto = IPPROTO_UDP;
3393 		match->mask.basic.ip_proto = 0xff;
3394 		break;
3395 	}
3396 
3397 	match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
3398 	match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3399 		offsetof(struct ethtool_rx_flow_key, basic);
3400 
3401 	if (fs->flow_type & FLOW_EXT) {
3402 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3403 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3404 
3405 		if (ext_m_spec->vlan_etype) {
3406 			match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3407 			match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3408 		}
3409 
3410 		if (ext_m_spec->vlan_tci) {
3411 			match->key.vlan.vlan_id =
3412 				ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3413 			match->mask.vlan.vlan_id =
3414 				ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3415 
3416 			match->key.vlan.vlan_dei =
3417 				!!(ext_h_spec->vlan_tci & htons(0x1000));
3418 			match->mask.vlan.vlan_dei =
3419 				!!(ext_m_spec->vlan_tci & htons(0x1000));
3420 
3421 			match->key.vlan.vlan_priority =
3422 				(ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3423 			match->mask.vlan.vlan_priority =
3424 				(ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3425 		}
3426 
3427 		if (ext_m_spec->vlan_etype ||
3428 		    ext_m_spec->vlan_tci) {
3429 			match->dissector.used_keys |=
3430 				BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
3431 			match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3432 				offsetof(struct ethtool_rx_flow_key, vlan);
3433 		}
3434 	}
3435 	if (fs->flow_type & FLOW_MAC_EXT) {
3436 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3437 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3438 
3439 		memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3440 		       ETH_ALEN);
3441 		memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3442 		       ETH_ALEN);
3443 
3444 		match->dissector.used_keys |=
3445 			BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3446 		match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3447 			offsetof(struct ethtool_rx_flow_key, eth_addrs);
3448 	}
3449 
3450 	act = &flow->rule->action.entries[0];
3451 	switch (fs->ring_cookie) {
3452 	case RX_CLS_FLOW_DISC:
3453 		act->id = FLOW_ACTION_DROP;
3454 		break;
3455 	case RX_CLS_FLOW_WAKE:
3456 		act->id = FLOW_ACTION_WAKE;
3457 		break;
3458 	default:
3459 		act->id = FLOW_ACTION_QUEUE;
3460 		if (fs->flow_type & FLOW_RSS)
3461 			act->queue.ctx = input->rss_ctx;
3462 
3463 		act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3464 		act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3465 		break;
3466 	}
3467 
3468 	return flow;
3469 }
3470 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3471 
3472 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3473 {
3474 	kfree(flow->rule);
3475 	kfree(flow);
3476 }
3477 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);
3478