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