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