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