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