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