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