xref: /linux/net/ethtool/ioctl.c (revision 1cc3462159babb69c84c39cb1b4e262aef3ea325)
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 	if (cmd == ETHTOOL_SRXCLSRLINS && info.fs.flow_type & FLOW_RSS) {
996 		/* Nonzero ring with RSS only makes sense
997 		 * if NIC adds them together
998 		 */
999 		if (!ops->cap_rss_rxnfc_adds &&
1000 		    ethtool_get_flow_spec_ring(info.fs.ring_cookie))
1001 			return -EINVAL;
1002 
1003 		if (!xa_load(&dev->ethtool->rss_ctx, info.rss_context))
1004 			return -EINVAL;
1005 	}
1006 
1007 	if (cmd == ETHTOOL_SRXFH && ops->get_rxfh) {
1008 		struct ethtool_rxfh_param rxfh = {};
1009 
1010 		rc = ops->get_rxfh(dev, &rxfh);
1011 		if (rc)
1012 			return rc;
1013 
1014 		/* Sanity check: if symmetric-xor/symmetric-or-xor is set, then:
1015 		 * 1 - no other fields besides IP src/dst and/or L4 src/dst
1016 		 * 2 - If src is set, dst must also be set
1017 		 */
1018 		if ((rxfh.input_xfrm & (RXH_XFRM_SYM_XOR | RXH_XFRM_SYM_OR_XOR)) &&
1019 		    ((info.data & ~(RXH_IP_SRC | RXH_IP_DST |
1020 				    RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
1021 		     (!!(info.data & RXH_IP_SRC) ^ !!(info.data & RXH_IP_DST)) ||
1022 		     (!!(info.data & RXH_L4_B_0_1) ^ !!(info.data & RXH_L4_B_2_3))))
1023 			return -EINVAL;
1024 	}
1025 
1026 	rc = ops->set_rxnfc(dev, &info);
1027 	if (rc)
1028 		return rc;
1029 
1030 	if (cmd == ETHTOOL_SRXCLSRLINS &&
1031 	    ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
1032 		return -EFAULT;
1033 
1034 	return 0;
1035 }
1036 
1037 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1038 						u32 cmd, void __user *useraddr)
1039 {
1040 	struct ethtool_rxnfc info;
1041 	size_t info_size = sizeof(info);
1042 	const struct ethtool_ops *ops = dev->ethtool_ops;
1043 	int ret;
1044 	void *rule_buf = NULL;
1045 
1046 	if (!ops->get_rxnfc)
1047 		return -EOPNOTSUPP;
1048 
1049 	ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1050 	if (ret)
1051 		return ret;
1052 
1053 	if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1054 		if (info.rule_cnt > 0) {
1055 			if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1056 				rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1057 						   GFP_USER);
1058 			if (!rule_buf)
1059 				return -ENOMEM;
1060 		}
1061 	}
1062 
1063 	ret = ops->get_rxnfc(dev, &info, rule_buf);
1064 	if (ret < 0)
1065 		goto err_out;
1066 
1067 	ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1068 err_out:
1069 	kfree(rule_buf);
1070 
1071 	return ret;
1072 }
1073 
1074 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1075 					struct ethtool_rxnfc *rx_rings,
1076 					u32 size)
1077 {
1078 	int i;
1079 
1080 	if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1081 		return -EFAULT;
1082 
1083 	/* Validate ring indices */
1084 	for (i = 0; i < size; i++)
1085 		if (indir[i] >= rx_rings->data)
1086 			return -EINVAL;
1087 
1088 	return 0;
1089 }
1090 
1091 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1092 
1093 void netdev_rss_key_fill(void *buffer, size_t len)
1094 {
1095 	BUG_ON(len > sizeof(netdev_rss_key));
1096 	net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1097 	memcpy(buffer, netdev_rss_key, len);
1098 }
1099 EXPORT_SYMBOL(netdev_rss_key_fill);
1100 
1101 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1102 						     void __user *useraddr)
1103 {
1104 	struct ethtool_rxfh_param rxfh = {};
1105 	u32 user_size;
1106 	int ret;
1107 
1108 	if (!dev->ethtool_ops->get_rxfh_indir_size ||
1109 	    !dev->ethtool_ops->get_rxfh)
1110 		return -EOPNOTSUPP;
1111 	rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1112 	if (rxfh.indir_size == 0)
1113 		return -EOPNOTSUPP;
1114 
1115 	if (copy_from_user(&user_size,
1116 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
1117 			   sizeof(user_size)))
1118 		return -EFAULT;
1119 
1120 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1121 			 &rxfh.indir_size, sizeof(rxfh.indir_size)))
1122 		return -EFAULT;
1123 
1124 	/* If the user buffer size is 0, this is just a query for the
1125 	 * device table size.  Otherwise, if it's smaller than the
1126 	 * device table size it's an error.
1127 	 */
1128 	if (user_size < rxfh.indir_size)
1129 		return user_size == 0 ? 0 : -EINVAL;
1130 
1131 	rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
1132 	if (!rxfh.indir)
1133 		return -ENOMEM;
1134 
1135 	ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
1136 	if (ret)
1137 		goto out;
1138 	if (copy_to_user(useraddr +
1139 			 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1140 			 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
1141 		ret = -EFAULT;
1142 
1143 out:
1144 	kfree(rxfh.indir);
1145 	return ret;
1146 }
1147 
1148 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1149 						     void __user *useraddr)
1150 {
1151 	const struct ethtool_ops *ops = dev->ethtool_ops;
1152 	struct ethtool_rxfh_param rxfh_dev = {};
1153 	struct netlink_ext_ack *extack = NULL;
1154 	struct ethtool_rxnfc rx_rings;
1155 	u32 user_size, i;
1156 	int ret;
1157 	u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1158 
1159 	if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1160 	    !ops->get_rxnfc)
1161 		return -EOPNOTSUPP;
1162 
1163 	rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1164 	if (rxfh_dev.indir_size == 0)
1165 		return -EOPNOTSUPP;
1166 
1167 	if (copy_from_user(&user_size,
1168 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
1169 			   sizeof(user_size)))
1170 		return -EFAULT;
1171 
1172 	if (user_size != 0 && user_size != rxfh_dev.indir_size)
1173 		return -EINVAL;
1174 
1175 	rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
1176 				 sizeof(rxfh_dev.indir[0]), GFP_USER);
1177 	if (!rxfh_dev.indir)
1178 		return -ENOMEM;
1179 
1180 	rx_rings.cmd = ETHTOOL_GRXRINGS;
1181 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1182 	if (ret)
1183 		goto out;
1184 
1185 	if (user_size == 0) {
1186 		u32 *indir = rxfh_dev.indir;
1187 
1188 		for (i = 0; i < rxfh_dev.indir_size; i++)
1189 			indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1190 	} else {
1191 		ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1192 						  useraddr + ringidx_offset,
1193 						  &rx_rings,
1194 						  rxfh_dev.indir_size);
1195 		if (ret)
1196 			goto out;
1197 	}
1198 
1199 	rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
1200 	ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1201 	if (ret)
1202 		goto out;
1203 
1204 	/* indicate whether rxfh was set to default */
1205 	if (user_size == 0)
1206 		dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1207 	else
1208 		dev->priv_flags |= IFF_RXFH_CONFIGURED;
1209 
1210 out:
1211 	kfree(rxfh_dev.indir);
1212 	return ret;
1213 }
1214 
1215 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1216 					       void __user *useraddr)
1217 {
1218 	const struct ethtool_ops *ops = dev->ethtool_ops;
1219 	struct ethtool_rxfh_param rxfh_dev = {};
1220 	u32 user_indir_size, user_key_size;
1221 	struct ethtool_rxfh_context *ctx;
1222 	struct ethtool_rxfh rxfh;
1223 	u32 indir_bytes;
1224 	u8 *rss_config;
1225 	u32 total_size;
1226 	int ret;
1227 
1228 	if (!ops->get_rxfh)
1229 		return -EOPNOTSUPP;
1230 
1231 	if (ops->get_rxfh_indir_size)
1232 		rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
1233 	if (ops->get_rxfh_key_size)
1234 		rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
1235 
1236 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1237 		return -EFAULT;
1238 	user_indir_size = rxfh.indir_size;
1239 	user_key_size = rxfh.key_size;
1240 
1241 	/* Check that reserved fields are 0 for now */
1242 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1243 		return -EINVAL;
1244 	/* Most drivers don't handle rss_context, check it's 0 as well */
1245 	if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
1246 				  ops->create_rxfh_context))
1247 		return -EOPNOTSUPP;
1248 
1249 	rxfh.indir_size = rxfh_dev.indir_size;
1250 	rxfh.key_size = rxfh_dev.key_size;
1251 	if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1252 		return -EFAULT;
1253 
1254 	if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
1255 	    (user_key_size && user_key_size != rxfh_dev.key_size))
1256 		return -EINVAL;
1257 
1258 	indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
1259 	total_size = indir_bytes + user_key_size;
1260 	rss_config = kzalloc(total_size, GFP_USER);
1261 	if (!rss_config)
1262 		return -ENOMEM;
1263 
1264 	if (user_indir_size)
1265 		rxfh_dev.indir = (u32 *)rss_config;
1266 
1267 	if (user_key_size)
1268 		rxfh_dev.key = rss_config + indir_bytes;
1269 
1270 	if (rxfh.rss_context) {
1271 		ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
1272 		if (!ctx) {
1273 			ret = -ENOENT;
1274 			goto out;
1275 		}
1276 		if (rxfh_dev.indir)
1277 			memcpy(rxfh_dev.indir, ethtool_rxfh_context_indir(ctx),
1278 			       indir_bytes);
1279 		if (!ops->rxfh_per_ctx_key) {
1280 			rxfh_dev.key_size = 0;
1281 		} else {
1282 			if (rxfh_dev.key)
1283 				memcpy(rxfh_dev.key,
1284 				       ethtool_rxfh_context_key(ctx),
1285 				       user_key_size);
1286 			rxfh_dev.hfunc = ctx->hfunc;
1287 		}
1288 		rxfh_dev.input_xfrm = ctx->input_xfrm;
1289 		ret = 0;
1290 	} else {
1291 		ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
1292 		if (ret)
1293 			goto out;
1294 	}
1295 
1296 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1297 			 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
1298 		ret = -EFAULT;
1299 	} else if (copy_to_user(useraddr +
1300 				offsetof(struct ethtool_rxfh, input_xfrm),
1301 				&rxfh_dev.input_xfrm,
1302 				sizeof(rxfh.input_xfrm))) {
1303 		ret = -EFAULT;
1304 	} else if (copy_to_user(useraddr +
1305 				offsetof(struct ethtool_rxfh, key_size),
1306 				&rxfh_dev.key_size,
1307 				sizeof(rxfh.key_size))) {
1308 		ret = -EFAULT;
1309 	} else if (copy_to_user(useraddr +
1310 			      offsetof(struct ethtool_rxfh, rss_config[0]),
1311 			      rss_config, total_size)) {
1312 		ret = -EFAULT;
1313 	}
1314 out:
1315 	kfree(rss_config);
1316 
1317 	return ret;
1318 }
1319 
1320 static struct ethtool_rxfh_context *
1321 ethtool_rxfh_ctx_alloc(const struct ethtool_ops *ops,
1322 		       u32 indir_size, u32 key_size)
1323 {
1324 	size_t indir_bytes, flex_len, key_off, size;
1325 	struct ethtool_rxfh_context *ctx;
1326 	u32 priv_bytes, indir_max;
1327 	u16 key_max;
1328 
1329 	key_max = max(key_size, ops->rxfh_key_space);
1330 	indir_max = max(indir_size, ops->rxfh_indir_space);
1331 
1332 	priv_bytes = ALIGN(ops->rxfh_priv_size, sizeof(u32));
1333 	indir_bytes = array_size(indir_max, sizeof(u32));
1334 
1335 	key_off = size_add(priv_bytes, indir_bytes);
1336 	flex_len = size_add(key_off, key_max);
1337 	size = struct_size_t(struct ethtool_rxfh_context, data, flex_len);
1338 
1339 	ctx = kzalloc(size, GFP_KERNEL_ACCOUNT);
1340 	if (!ctx)
1341 		return NULL;
1342 
1343 	ctx->indir_size = indir_size;
1344 	ctx->key_size = key_size;
1345 	ctx->key_off = key_off;
1346 	ctx->priv_size = ops->rxfh_priv_size;
1347 
1348 	ctx->hfunc = ETH_RSS_HASH_NO_CHANGE;
1349 	ctx->input_xfrm = RXH_XFRM_NO_CHANGE;
1350 
1351 	return ctx;
1352 }
1353 
1354 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1355 					       void __user *useraddr)
1356 {
1357 	u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1358 	const struct ethtool_ops *ops = dev->ethtool_ops;
1359 	u32 dev_indir_size = 0, dev_key_size = 0, i;
1360 	u32 user_indir_len = 0, indir_bytes = 0;
1361 	struct ethtool_rxfh_param rxfh_dev = {};
1362 	struct ethtool_rxfh_context *ctx = NULL;
1363 	struct netlink_ext_ack *extack = NULL;
1364 	struct ethtool_rxnfc rx_rings;
1365 	struct ethtool_rxfh rxfh;
1366 	bool locked = false; /* dev->ethtool->rss_lock taken */
1367 	bool create = false;
1368 	u8 *rss_config;
1369 	int ret;
1370 
1371 	if (!ops->get_rxnfc || !ops->set_rxfh)
1372 		return -EOPNOTSUPP;
1373 
1374 	if (ops->get_rxfh_indir_size)
1375 		dev_indir_size = ops->get_rxfh_indir_size(dev);
1376 	if (ops->get_rxfh_key_size)
1377 		dev_key_size = ops->get_rxfh_key_size(dev);
1378 
1379 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1380 		return -EFAULT;
1381 
1382 	/* Check that reserved fields are 0 for now */
1383 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
1384 		return -EINVAL;
1385 	/* Most drivers don't handle rss_context, check it's 0 as well */
1386 	if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
1387 				  ops->create_rxfh_context))
1388 		return -EOPNOTSUPP;
1389 	/* Check input data transformation capabilities */
1390 	if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
1391 	    rxfh.input_xfrm != RXH_XFRM_SYM_OR_XOR &&
1392 	    rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
1393 		return -EINVAL;
1394 	if (rxfh.input_xfrm != RXH_XFRM_NO_CHANGE &&
1395 	    rxfh.input_xfrm & ~ops->supported_input_xfrm)
1396 		return -EOPNOTSUPP;
1397 	create = rxfh.rss_context == ETH_RXFH_CONTEXT_ALLOC;
1398 
1399 	if ((rxfh.indir_size &&
1400 	     rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1401 	     rxfh.indir_size != dev_indir_size) ||
1402 	    (rxfh.key_size && rxfh.key_size != dev_key_size))
1403 		return -EINVAL;
1404 
1405 	/* Must request at least one change: indir size, hash key, function
1406 	 * or input transformation.
1407 	 * There's no need for any of it in case of context creation.
1408 	 */
1409 	if (!create &&
1410 	    (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1411 	     rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
1412 	     rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
1413 		return -EINVAL;
1414 
1415 	indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
1416 
1417 	/* Check settings which may be global rather than per RSS-context */
1418 	if (rxfh.rss_context && !ops->rxfh_per_ctx_key)
1419 		if (rxfh.key_size ||
1420 		    (rxfh.hfunc && rxfh.hfunc != ETH_RSS_HASH_NO_CHANGE) ||
1421 		    (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_NO_CHANGE))
1422 			return -EOPNOTSUPP;
1423 
1424 	rss_config = kzalloc(indir_bytes + dev_key_size, GFP_USER);
1425 	if (!rss_config)
1426 		return -ENOMEM;
1427 
1428 	rx_rings.cmd = ETHTOOL_GRXRINGS;
1429 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1430 	if (ret)
1431 		goto out;
1432 
1433 	/* rxfh.indir_size == 0 means reset the indir table to default (master
1434 	 * context) or delete the context (other RSS contexts).
1435 	 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1436 	 */
1437 	if (rxfh.indir_size &&
1438 	    rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1439 		user_indir_len = indir_bytes;
1440 		rxfh_dev.indir = (u32 *)rss_config;
1441 		rxfh_dev.indir_size = dev_indir_size;
1442 		ret = ethtool_copy_validate_indir(rxfh_dev.indir,
1443 						  useraddr + rss_cfg_offset,
1444 						  &rx_rings,
1445 						  rxfh.indir_size);
1446 		if (ret)
1447 			goto out;
1448 	} else if (rxfh.indir_size == 0) {
1449 		if (rxfh.rss_context == 0) {
1450 			u32 *indir;
1451 
1452 			rxfh_dev.indir = (u32 *)rss_config;
1453 			rxfh_dev.indir_size = dev_indir_size;
1454 			indir = rxfh_dev.indir;
1455 			for (i = 0; i < dev_indir_size; i++)
1456 				indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1457 		} else {
1458 			rxfh_dev.rss_delete = true;
1459 		}
1460 	}
1461 
1462 	if (rxfh.key_size) {
1463 		rxfh_dev.key_size = dev_key_size;
1464 		rxfh_dev.key = rss_config + indir_bytes;
1465 		if (copy_from_user(rxfh_dev.key,
1466 				   useraddr + rss_cfg_offset + user_indir_len,
1467 				   rxfh.key_size)) {
1468 			ret = -EFAULT;
1469 			goto out;
1470 		}
1471 	}
1472 
1473 	if (rxfh.rss_context) {
1474 		mutex_lock(&dev->ethtool->rss_lock);
1475 		locked = true;
1476 	}
1477 
1478 	if (rxfh.rss_context && rxfh_dev.rss_delete) {
1479 		ret = ethtool_check_rss_ctx_busy(dev, rxfh.rss_context);
1480 		if (ret)
1481 			goto out;
1482 	}
1483 
1484 	if (create) {
1485 		if (rxfh_dev.rss_delete) {
1486 			ret = -EINVAL;
1487 			goto out;
1488 		}
1489 		ctx = ethtool_rxfh_ctx_alloc(ops, dev_indir_size, dev_key_size);
1490 		if (!ctx) {
1491 			ret = -ENOMEM;
1492 			goto out;
1493 		}
1494 
1495 		if (ops->create_rxfh_context) {
1496 			u32 limit = ops->rxfh_max_num_contexts ?: U32_MAX;
1497 			u32 ctx_id;
1498 
1499 			/* driver uses new API, core allocates ID */
1500 			ret = xa_alloc(&dev->ethtool->rss_ctx, &ctx_id, ctx,
1501 				       XA_LIMIT(1, limit - 1),
1502 				       GFP_KERNEL_ACCOUNT);
1503 			if (ret < 0) {
1504 				kfree(ctx);
1505 				goto out;
1506 			}
1507 			WARN_ON(!ctx_id); /* can't happen */
1508 			rxfh.rss_context = ctx_id;
1509 		}
1510 	} else if (rxfh.rss_context) {
1511 		ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
1512 		if (!ctx) {
1513 			ret = -ENOENT;
1514 			goto out;
1515 		}
1516 	}
1517 	rxfh_dev.hfunc = rxfh.hfunc;
1518 	rxfh_dev.rss_context = rxfh.rss_context;
1519 	rxfh_dev.input_xfrm = rxfh.input_xfrm;
1520 
1521 	if (rxfh.rss_context && ops->create_rxfh_context) {
1522 		if (create) {
1523 			ret = ops->create_rxfh_context(dev, ctx, &rxfh_dev,
1524 						       extack);
1525 			/* Make sure driver populates defaults */
1526 			WARN_ON_ONCE(!ret && !rxfh_dev.key &&
1527 				     ops->rxfh_per_ctx_key &&
1528 				     !memchr_inv(ethtool_rxfh_context_key(ctx),
1529 						 0, ctx->key_size));
1530 		} else if (rxfh_dev.rss_delete) {
1531 			ret = ops->remove_rxfh_context(dev, ctx,
1532 						       rxfh.rss_context,
1533 						       extack);
1534 		} else {
1535 			ret = ops->modify_rxfh_context(dev, ctx, &rxfh_dev,
1536 						       extack);
1537 		}
1538 	} else {
1539 		ret = ops->set_rxfh(dev, &rxfh_dev, extack);
1540 	}
1541 	if (ret) {
1542 		if (create) {
1543 			/* failed to create, free our new tracking entry */
1544 			if (ops->create_rxfh_context)
1545 				xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
1546 			kfree(ctx);
1547 		}
1548 		goto out;
1549 	}
1550 
1551 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1552 			 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
1553 		ret = -EFAULT;
1554 
1555 	if (!rxfh_dev.rss_context) {
1556 		/* indicate whether rxfh was set to default */
1557 		if (rxfh.indir_size == 0)
1558 			dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1559 		else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1560 			dev->priv_flags |= IFF_RXFH_CONFIGURED;
1561 	}
1562 	/* Update rss_ctx tracking */
1563 	if (create && !ops->create_rxfh_context) {
1564 		/* driver uses old API, it chose context ID */
1565 		if (WARN_ON(xa_load(&dev->ethtool->rss_ctx, rxfh_dev.rss_context))) {
1566 			/* context ID reused, our tracking is screwed */
1567 			kfree(ctx);
1568 			goto out;
1569 		}
1570 		/* Allocate the exact ID the driver gave us */
1571 		if (xa_is_err(xa_store(&dev->ethtool->rss_ctx, rxfh_dev.rss_context,
1572 				       ctx, GFP_KERNEL))) {
1573 			kfree(ctx);
1574 			goto out;
1575 		}
1576 
1577 		/* Fetch the defaults for the old API, in the new API drivers
1578 		 * should write defaults into ctx themselves.
1579 		 */
1580 		rxfh_dev.indir = (u32 *)rss_config;
1581 		rxfh_dev.indir_size = dev_indir_size;
1582 
1583 		rxfh_dev.key = rss_config + indir_bytes;
1584 		rxfh_dev.key_size = dev_key_size;
1585 
1586 		ret = ops->get_rxfh(dev, &rxfh_dev);
1587 		if (WARN_ON(ret)) {
1588 			xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
1589 			kfree(ctx);
1590 			goto out;
1591 		}
1592 	}
1593 	if (rxfh_dev.rss_delete) {
1594 		WARN_ON(xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context) != ctx);
1595 		kfree(ctx);
1596 	} else if (ctx) {
1597 		if (rxfh_dev.indir) {
1598 			for (i = 0; i < dev_indir_size; i++)
1599 				ethtool_rxfh_context_indir(ctx)[i] = rxfh_dev.indir[i];
1600 			ctx->indir_configured =
1601 				rxfh.indir_size &&
1602 				rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE;
1603 		}
1604 		if (rxfh_dev.key) {
1605 			memcpy(ethtool_rxfh_context_key(ctx), rxfh_dev.key,
1606 			       dev_key_size);
1607 			ctx->key_configured = !!rxfh.key_size;
1608 		}
1609 		if (rxfh_dev.hfunc != ETH_RSS_HASH_NO_CHANGE)
1610 			ctx->hfunc = rxfh_dev.hfunc;
1611 		if (rxfh_dev.input_xfrm != RXH_XFRM_NO_CHANGE)
1612 			ctx->input_xfrm = rxfh_dev.input_xfrm;
1613 	}
1614 
1615 out:
1616 	if (locked)
1617 		mutex_unlock(&dev->ethtool->rss_lock);
1618 	kfree(rss_config);
1619 	return ret;
1620 }
1621 
1622 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1623 {
1624 	struct ethtool_regs regs;
1625 	const struct ethtool_ops *ops = dev->ethtool_ops;
1626 	void *regbuf;
1627 	int reglen, ret;
1628 
1629 	if (!ops->get_regs || !ops->get_regs_len)
1630 		return -EOPNOTSUPP;
1631 
1632 	if (copy_from_user(&regs, useraddr, sizeof(regs)))
1633 		return -EFAULT;
1634 
1635 	reglen = ops->get_regs_len(dev);
1636 	if (reglen <= 0)
1637 		return reglen;
1638 
1639 	if (regs.len > reglen)
1640 		regs.len = reglen;
1641 
1642 	regbuf = vzalloc(reglen);
1643 	if (!regbuf)
1644 		return -ENOMEM;
1645 
1646 	if (regs.len < reglen)
1647 		reglen = regs.len;
1648 
1649 	ops->get_regs(dev, &regs, regbuf);
1650 
1651 	ret = -EFAULT;
1652 	if (copy_to_user(useraddr, &regs, sizeof(regs)))
1653 		goto out;
1654 	useraddr += offsetof(struct ethtool_regs, data);
1655 	if (copy_to_user(useraddr, regbuf, reglen))
1656 		goto out;
1657 	ret = 0;
1658 
1659  out:
1660 	vfree(regbuf);
1661 	return ret;
1662 }
1663 
1664 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1665 {
1666 	struct ethtool_value reset;
1667 	int ret;
1668 
1669 	if (!dev->ethtool_ops->reset)
1670 		return -EOPNOTSUPP;
1671 
1672 	if (dev->ethtool->module_fw_flash_in_progress)
1673 		return -EBUSY;
1674 
1675 	if (copy_from_user(&reset, useraddr, sizeof(reset)))
1676 		return -EFAULT;
1677 
1678 	ret = dev->ethtool_ops->reset(dev, &reset.data);
1679 	if (ret)
1680 		return ret;
1681 
1682 	if (copy_to_user(useraddr, &reset, sizeof(reset)))
1683 		return -EFAULT;
1684 	return 0;
1685 }
1686 
1687 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1688 {
1689 	struct ethtool_wolinfo wol;
1690 
1691 	if (!dev->ethtool_ops->get_wol)
1692 		return -EOPNOTSUPP;
1693 
1694 	memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1695 	wol.cmd = ETHTOOL_GWOL;
1696 	dev->ethtool_ops->get_wol(dev, &wol);
1697 
1698 	if (copy_to_user(useraddr, &wol, sizeof(wol)))
1699 		return -EFAULT;
1700 	return 0;
1701 }
1702 
1703 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1704 {
1705 	struct ethtool_wolinfo wol, cur_wol;
1706 	int ret;
1707 
1708 	if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
1709 		return -EOPNOTSUPP;
1710 
1711 	memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
1712 	cur_wol.cmd = ETHTOOL_GWOL;
1713 	dev->ethtool_ops->get_wol(dev, &cur_wol);
1714 
1715 	if (copy_from_user(&wol, useraddr, sizeof(wol)))
1716 		return -EFAULT;
1717 
1718 	if (wol.wolopts & ~cur_wol.supported)
1719 		return -EINVAL;
1720 
1721 	if (wol.wolopts == cur_wol.wolopts &&
1722 	    !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
1723 		return 0;
1724 
1725 	ret = dev->ethtool_ops->set_wol(dev, &wol);
1726 	if (ret)
1727 		return ret;
1728 
1729 	dev->ethtool->wol_enabled = !!wol.wolopts;
1730 	ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1731 
1732 	return 0;
1733 }
1734 
1735 static void eee_to_keee(struct ethtool_keee *keee,
1736 			const struct ethtool_eee *eee)
1737 {
1738 	memset(keee, 0, sizeof(*keee));
1739 
1740 	keee->eee_enabled = eee->eee_enabled;
1741 	keee->tx_lpi_enabled = eee->tx_lpi_enabled;
1742 	keee->tx_lpi_timer = eee->tx_lpi_timer;
1743 
1744 	ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
1745 						eee->advertised);
1746 }
1747 
1748 static void keee_to_eee(struct ethtool_eee *eee,
1749 			const struct ethtool_keee *keee)
1750 {
1751 	bool overflow;
1752 
1753 	memset(eee, 0, sizeof(*eee));
1754 
1755 	eee->eee_active = keee->eee_active;
1756 	eee->eee_enabled = keee->eee_enabled;
1757 	eee->tx_lpi_enabled = keee->tx_lpi_enabled;
1758 	eee->tx_lpi_timer = keee->tx_lpi_timer;
1759 
1760 	overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
1761 							    keee->supported);
1762 	ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
1763 						keee->advertised);
1764 	ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
1765 						keee->lp_advertised);
1766 	if (overflow)
1767 		pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
1768 }
1769 
1770 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1771 {
1772 	struct ethtool_keee keee;
1773 	struct ethtool_eee eee;
1774 	int rc;
1775 
1776 	if (!dev->ethtool_ops->get_eee)
1777 		return -EOPNOTSUPP;
1778 
1779 	memset(&keee, 0, sizeof(keee));
1780 	rc = dev->ethtool_ops->get_eee(dev, &keee);
1781 	if (rc)
1782 		return rc;
1783 
1784 	keee_to_eee(&eee, &keee);
1785 	if (copy_to_user(useraddr, &eee, sizeof(eee)))
1786 		return -EFAULT;
1787 
1788 	return 0;
1789 }
1790 
1791 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1792 {
1793 	struct ethtool_keee keee;
1794 	struct ethtool_eee eee;
1795 	int ret;
1796 
1797 	if (!dev->ethtool_ops->set_eee)
1798 		return -EOPNOTSUPP;
1799 
1800 	if (copy_from_user(&eee, useraddr, sizeof(eee)))
1801 		return -EFAULT;
1802 
1803 	eee_to_keee(&keee, &eee);
1804 	ret = dev->ethtool_ops->set_eee(dev, &keee);
1805 	if (!ret)
1806 		ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1807 	return ret;
1808 }
1809 
1810 static int ethtool_nway_reset(struct net_device *dev)
1811 {
1812 	if (!dev->ethtool_ops->nway_reset)
1813 		return -EOPNOTSUPP;
1814 
1815 	return dev->ethtool_ops->nway_reset(dev);
1816 }
1817 
1818 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1819 {
1820 	struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1821 	int link = __ethtool_get_link(dev);
1822 
1823 	if (link < 0)
1824 		return link;
1825 
1826 	edata.data = link;
1827 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
1828 		return -EFAULT;
1829 	return 0;
1830 }
1831 
1832 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1833 				  int (*getter)(struct net_device *,
1834 						struct ethtool_eeprom *, u8 *),
1835 				  u32 total_len)
1836 {
1837 	struct ethtool_eeprom eeprom;
1838 	void __user *userbuf = useraddr + sizeof(eeprom);
1839 	u32 bytes_remaining;
1840 	u8 *data;
1841 	int ret = 0;
1842 
1843 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1844 		return -EFAULT;
1845 
1846 	/* Check for wrap and zero */
1847 	if (eeprom.offset + eeprom.len <= eeprom.offset)
1848 		return -EINVAL;
1849 
1850 	/* Check for exceeding total eeprom len */
1851 	if (eeprom.offset + eeprom.len > total_len)
1852 		return -EINVAL;
1853 
1854 	data = kzalloc(PAGE_SIZE, GFP_USER);
1855 	if (!data)
1856 		return -ENOMEM;
1857 
1858 	bytes_remaining = eeprom.len;
1859 	while (bytes_remaining > 0) {
1860 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1861 
1862 		ret = getter(dev, &eeprom, data);
1863 		if (ret)
1864 			break;
1865 		if (!eeprom.len) {
1866 			ret = -EIO;
1867 			break;
1868 		}
1869 		if (copy_to_user(userbuf, data, eeprom.len)) {
1870 			ret = -EFAULT;
1871 			break;
1872 		}
1873 		userbuf += eeprom.len;
1874 		eeprom.offset += eeprom.len;
1875 		bytes_remaining -= eeprom.len;
1876 	}
1877 
1878 	eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1879 	eeprom.offset -= eeprom.len;
1880 	if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1881 		ret = -EFAULT;
1882 
1883 	kfree(data);
1884 	return ret;
1885 }
1886 
1887 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1888 {
1889 	const struct ethtool_ops *ops = dev->ethtool_ops;
1890 
1891 	if (!ops->get_eeprom || !ops->get_eeprom_len ||
1892 	    !ops->get_eeprom_len(dev))
1893 		return -EOPNOTSUPP;
1894 
1895 	return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1896 				      ops->get_eeprom_len(dev));
1897 }
1898 
1899 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1900 {
1901 	struct ethtool_eeprom eeprom;
1902 	const struct ethtool_ops *ops = dev->ethtool_ops;
1903 	void __user *userbuf = useraddr + sizeof(eeprom);
1904 	u32 bytes_remaining;
1905 	u8 *data;
1906 	int ret = 0;
1907 
1908 	if (!ops->set_eeprom || !ops->get_eeprom_len ||
1909 	    !ops->get_eeprom_len(dev))
1910 		return -EOPNOTSUPP;
1911 
1912 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1913 		return -EFAULT;
1914 
1915 	/* Check for wrap and zero */
1916 	if (eeprom.offset + eeprom.len <= eeprom.offset)
1917 		return -EINVAL;
1918 
1919 	/* Check for exceeding total eeprom len */
1920 	if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1921 		return -EINVAL;
1922 
1923 	data = kzalloc(PAGE_SIZE, GFP_USER);
1924 	if (!data)
1925 		return -ENOMEM;
1926 
1927 	bytes_remaining = eeprom.len;
1928 	while (bytes_remaining > 0) {
1929 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1930 
1931 		if (copy_from_user(data, userbuf, eeprom.len)) {
1932 			ret = -EFAULT;
1933 			break;
1934 		}
1935 		ret = ops->set_eeprom(dev, &eeprom, data);
1936 		if (ret)
1937 			break;
1938 		userbuf += eeprom.len;
1939 		eeprom.offset += eeprom.len;
1940 		bytes_remaining -= eeprom.len;
1941 	}
1942 
1943 	kfree(data);
1944 	return ret;
1945 }
1946 
1947 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1948 						   void __user *useraddr)
1949 {
1950 	struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1951 	struct kernel_ethtool_coalesce kernel_coalesce = {};
1952 	int ret;
1953 
1954 	if (!dev->ethtool_ops->get_coalesce)
1955 		return -EOPNOTSUPP;
1956 
1957 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1958 					     NULL);
1959 	if (ret)
1960 		return ret;
1961 
1962 	if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1963 		return -EFAULT;
1964 	return 0;
1965 }
1966 
1967 static bool
1968 ethtool_set_coalesce_supported(struct net_device *dev,
1969 			       struct ethtool_coalesce *coalesce)
1970 {
1971 	u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1972 	u32 nonzero_params = 0;
1973 
1974 	if (coalesce->rx_coalesce_usecs)
1975 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1976 	if (coalesce->rx_max_coalesced_frames)
1977 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1978 	if (coalesce->rx_coalesce_usecs_irq)
1979 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1980 	if (coalesce->rx_max_coalesced_frames_irq)
1981 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1982 	if (coalesce->tx_coalesce_usecs)
1983 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1984 	if (coalesce->tx_max_coalesced_frames)
1985 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1986 	if (coalesce->tx_coalesce_usecs_irq)
1987 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1988 	if (coalesce->tx_max_coalesced_frames_irq)
1989 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1990 	if (coalesce->stats_block_coalesce_usecs)
1991 		nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1992 	if (coalesce->use_adaptive_rx_coalesce)
1993 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1994 	if (coalesce->use_adaptive_tx_coalesce)
1995 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1996 	if (coalesce->pkt_rate_low)
1997 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1998 	if (coalesce->rx_coalesce_usecs_low)
1999 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
2000 	if (coalesce->rx_max_coalesced_frames_low)
2001 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
2002 	if (coalesce->tx_coalesce_usecs_low)
2003 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
2004 	if (coalesce->tx_max_coalesced_frames_low)
2005 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
2006 	if (coalesce->pkt_rate_high)
2007 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
2008 	if (coalesce->rx_coalesce_usecs_high)
2009 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
2010 	if (coalesce->rx_max_coalesced_frames_high)
2011 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
2012 	if (coalesce->tx_coalesce_usecs_high)
2013 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
2014 	if (coalesce->tx_max_coalesced_frames_high)
2015 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
2016 	if (coalesce->rate_sample_interval)
2017 		nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
2018 
2019 	return (supported_params & nonzero_params) == nonzero_params;
2020 }
2021 
2022 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
2023 						   void __user *useraddr)
2024 {
2025 	struct kernel_ethtool_coalesce kernel_coalesce = {};
2026 	struct ethtool_coalesce coalesce;
2027 	int ret;
2028 
2029 	if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
2030 		return -EOPNOTSUPP;
2031 
2032 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
2033 					     NULL);
2034 	if (ret)
2035 		return ret;
2036 
2037 	if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
2038 		return -EFAULT;
2039 
2040 	if (!ethtool_set_coalesce_supported(dev, &coalesce))
2041 		return -EOPNOTSUPP;
2042 
2043 	ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
2044 					     NULL);
2045 	if (!ret)
2046 		ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
2047 	return ret;
2048 }
2049 
2050 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
2051 {
2052 	struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
2053 	struct kernel_ethtool_ringparam kernel_ringparam = {};
2054 
2055 	if (!dev->ethtool_ops->get_ringparam)
2056 		return -EOPNOTSUPP;
2057 
2058 	dev->ethtool_ops->get_ringparam(dev, &ringparam,
2059 					&kernel_ringparam, NULL);
2060 
2061 	if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
2062 		return -EFAULT;
2063 	return 0;
2064 }
2065 
2066 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
2067 {
2068 	struct kernel_ethtool_ringparam kernel_ringparam;
2069 	struct ethtool_ringparam ringparam, max;
2070 	int ret;
2071 
2072 	if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
2073 		return -EOPNOTSUPP;
2074 
2075 	if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
2076 		return -EFAULT;
2077 
2078 	ethtool_ringparam_get_cfg(dev, &max, &kernel_ringparam, NULL);
2079 
2080 	/* ensure new ring parameters are within the maximums */
2081 	if (ringparam.rx_pending > max.rx_max_pending ||
2082 	    ringparam.rx_mini_pending > max.rx_mini_max_pending ||
2083 	    ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
2084 	    ringparam.tx_pending > max.tx_max_pending)
2085 		return -EINVAL;
2086 
2087 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
2088 					      &kernel_ringparam, NULL);
2089 	if (!ret)
2090 		ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
2091 	return ret;
2092 }
2093 
2094 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
2095 						   void __user *useraddr)
2096 {
2097 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
2098 
2099 	if (!dev->ethtool_ops->get_channels)
2100 		return -EOPNOTSUPP;
2101 
2102 	dev->ethtool_ops->get_channels(dev, &channels);
2103 
2104 	if (copy_to_user(useraddr, &channels, sizeof(channels)))
2105 		return -EFAULT;
2106 	return 0;
2107 }
2108 
2109 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
2110 						   void __user *useraddr)
2111 {
2112 	struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
2113 	u16 from_channel, to_channel;
2114 	unsigned int i;
2115 	int ret;
2116 
2117 	if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
2118 		return -EOPNOTSUPP;
2119 
2120 	if (copy_from_user(&channels, useraddr, sizeof(channels)))
2121 		return -EFAULT;
2122 
2123 	dev->ethtool_ops->get_channels(dev, &curr);
2124 
2125 	if (channels.rx_count == curr.rx_count &&
2126 	    channels.tx_count == curr.tx_count &&
2127 	    channels.combined_count == curr.combined_count &&
2128 	    channels.other_count == curr.other_count)
2129 		return 0;
2130 
2131 	/* ensure new counts are within the maximums */
2132 	if (channels.rx_count > curr.max_rx ||
2133 	    channels.tx_count > curr.max_tx ||
2134 	    channels.combined_count > curr.max_combined ||
2135 	    channels.other_count > curr.max_other)
2136 		return -EINVAL;
2137 
2138 	/* ensure there is at least one RX and one TX channel */
2139 	if (!channels.combined_count &&
2140 	    (!channels.rx_count || !channels.tx_count))
2141 		return -EINVAL;
2142 
2143 	ret = ethtool_check_max_channel(dev, channels, NULL);
2144 	if (ret)
2145 		return ret;
2146 
2147 	/* Disabling channels, query zero-copy AF_XDP sockets */
2148 	from_channel = channels.combined_count +
2149 		min(channels.rx_count, channels.tx_count);
2150 	to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
2151 	for (i = from_channel; i < to_channel; i++)
2152 		if (xsk_get_pool_from_qid(dev, i))
2153 			return -EINVAL;
2154 
2155 	ret = dev->ethtool_ops->set_channels(dev, &channels);
2156 	if (!ret)
2157 		ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
2158 	return ret;
2159 }
2160 
2161 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
2162 {
2163 	struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
2164 
2165 	if (!dev->ethtool_ops->get_pauseparam)
2166 		return -EOPNOTSUPP;
2167 
2168 	dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
2169 
2170 	if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
2171 		return -EFAULT;
2172 	return 0;
2173 }
2174 
2175 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
2176 {
2177 	struct ethtool_pauseparam pauseparam;
2178 	int ret;
2179 
2180 	if (!dev->ethtool_ops->set_pauseparam)
2181 		return -EOPNOTSUPP;
2182 
2183 	if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
2184 		return -EFAULT;
2185 
2186 	ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
2187 	if (!ret)
2188 		ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
2189 	return ret;
2190 }
2191 
2192 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
2193 {
2194 	struct ethtool_test test;
2195 	const struct ethtool_ops *ops = dev->ethtool_ops;
2196 	u64 *data;
2197 	int ret, test_len;
2198 
2199 	if (!ops->self_test || !ops->get_sset_count)
2200 		return -EOPNOTSUPP;
2201 
2202 	test_len = ops->get_sset_count(dev, ETH_SS_TEST);
2203 	if (test_len < 0)
2204 		return test_len;
2205 	WARN_ON(test_len == 0);
2206 
2207 	if (copy_from_user(&test, useraddr, sizeof(test)))
2208 		return -EFAULT;
2209 
2210 	test.len = test_len;
2211 	data = kcalloc(test_len, sizeof(u64), GFP_USER);
2212 	if (!data)
2213 		return -ENOMEM;
2214 
2215 	netif_testing_on(dev);
2216 	ops->self_test(dev, &test, data);
2217 	netif_testing_off(dev);
2218 
2219 	ret = -EFAULT;
2220 	if (copy_to_user(useraddr, &test, sizeof(test)))
2221 		goto out;
2222 	useraddr += sizeof(test);
2223 	if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
2224 		goto out;
2225 	ret = 0;
2226 
2227  out:
2228 	kfree(data);
2229 	return ret;
2230 }
2231 
2232 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
2233 {
2234 	struct ethtool_gstrings gstrings;
2235 	u8 *data;
2236 	int ret;
2237 
2238 	if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
2239 		return -EFAULT;
2240 
2241 	ret = __ethtool_get_sset_count(dev, gstrings.string_set);
2242 	if (ret < 0)
2243 		return ret;
2244 	if (ret > S32_MAX / ETH_GSTRING_LEN)
2245 		return -ENOMEM;
2246 	WARN_ON_ONCE(!ret);
2247 
2248 	gstrings.len = ret;
2249 
2250 	if (gstrings.len) {
2251 		data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
2252 		if (!data)
2253 			return -ENOMEM;
2254 
2255 		__ethtool_get_strings(dev, gstrings.string_set, data);
2256 	} else {
2257 		data = NULL;
2258 	}
2259 
2260 	ret = -EFAULT;
2261 	if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
2262 		goto out;
2263 	useraddr += sizeof(gstrings);
2264 	if (gstrings.len &&
2265 	    copy_to_user(useraddr, data,
2266 			 array_size(gstrings.len, ETH_GSTRING_LEN)))
2267 		goto out;
2268 	ret = 0;
2269 
2270 out:
2271 	vfree(data);
2272 	return ret;
2273 }
2274 
2275 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
2276 {
2277 	va_list args;
2278 
2279 	va_start(args, fmt);
2280 	vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
2281 	va_end(args);
2282 
2283 	*data += ETH_GSTRING_LEN;
2284 }
2285 EXPORT_SYMBOL(ethtool_sprintf);
2286 
2287 void ethtool_puts(u8 **data, const char *str)
2288 {
2289 	strscpy(*data, str, ETH_GSTRING_LEN);
2290 	*data += ETH_GSTRING_LEN;
2291 }
2292 EXPORT_SYMBOL(ethtool_puts);
2293 
2294 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
2295 {
2296 	struct ethtool_value id;
2297 	static bool busy;
2298 	const struct ethtool_ops *ops = dev->ethtool_ops;
2299 	netdevice_tracker dev_tracker;
2300 	int rc;
2301 
2302 	if (!ops->set_phys_id)
2303 		return -EOPNOTSUPP;
2304 
2305 	if (busy)
2306 		return -EBUSY;
2307 
2308 	if (copy_from_user(&id, useraddr, sizeof(id)))
2309 		return -EFAULT;
2310 
2311 	rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
2312 	if (rc < 0)
2313 		return rc;
2314 
2315 	/* Drop the RTNL lock while waiting, but prevent reentry or
2316 	 * removal of the device.
2317 	 */
2318 	busy = true;
2319 	netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2320 	netdev_unlock_ops(dev);
2321 	rtnl_unlock();
2322 
2323 	if (rc == 0) {
2324 		/* Driver will handle this itself */
2325 		schedule_timeout_interruptible(
2326 			id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2327 	} else {
2328 		/* Driver expects to be called at twice the frequency in rc */
2329 		int n = rc * 2, interval = HZ / n;
2330 		u64 count = mul_u32_u32(n, id.data);
2331 		u64 i = 0;
2332 
2333 		do {
2334 			rtnl_lock();
2335 			netdev_lock_ops(dev);
2336 			rc = ops->set_phys_id(dev,
2337 				    (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2338 			netdev_unlock_ops(dev);
2339 			rtnl_unlock();
2340 			if (rc)
2341 				break;
2342 			schedule_timeout_interruptible(interval);
2343 		} while (!signal_pending(current) && (!id.data || i < count));
2344 	}
2345 
2346 	rtnl_lock();
2347 	netdev_lock_ops(dev);
2348 	netdev_put(dev, &dev_tracker);
2349 	busy = false;
2350 
2351 	(void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2352 	return rc;
2353 }
2354 
2355 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2356 {
2357 	struct ethtool_stats stats;
2358 	const struct ethtool_ops *ops = dev->ethtool_ops;
2359 	u64 *data;
2360 	int ret, n_stats;
2361 
2362 	if (!ops->get_ethtool_stats || !ops->get_sset_count)
2363 		return -EOPNOTSUPP;
2364 
2365 	n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2366 	if (n_stats < 0)
2367 		return n_stats;
2368 	if (n_stats > S32_MAX / sizeof(u64))
2369 		return -ENOMEM;
2370 	WARN_ON_ONCE(!n_stats);
2371 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
2372 		return -EFAULT;
2373 
2374 	stats.n_stats = n_stats;
2375 
2376 	if (n_stats) {
2377 		data = vzalloc(array_size(n_stats, sizeof(u64)));
2378 		if (!data)
2379 			return -ENOMEM;
2380 		ops->get_ethtool_stats(dev, &stats, data);
2381 	} else {
2382 		data = NULL;
2383 	}
2384 
2385 	ret = -EFAULT;
2386 	if (copy_to_user(useraddr, &stats, sizeof(stats)))
2387 		goto out;
2388 	useraddr += sizeof(stats);
2389 	if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2390 		goto out;
2391 	ret = 0;
2392 
2393  out:
2394 	vfree(data);
2395 	return ret;
2396 }
2397 
2398 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
2399 {
2400 	if (n_stats < 0)
2401 		return n_stats;
2402 	if (n_stats > S32_MAX / sizeof(u64))
2403 		return -ENOMEM;
2404 	if (WARN_ON_ONCE(!n_stats))
2405 		return -EOPNOTSUPP;
2406 
2407 	*data = vzalloc(array_size(n_stats, sizeof(u64)));
2408 	if (!*data)
2409 		return -ENOMEM;
2410 
2411 	return 0;
2412 }
2413 
2414 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2415 					 struct ethtool_stats *stats,
2416 					 u64 **data)
2417  {
2418 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2419 	int n_stats, ret;
2420 
2421 	if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2422 		return -EOPNOTSUPP;
2423 
2424 	n_stats = phy_ops->get_sset_count(phydev);
2425 
2426 	ret = ethtool_vzalloc_stats_array(n_stats, data);
2427 	if (ret)
2428 		return ret;
2429 
2430 	stats->n_stats = n_stats;
2431 	return phy_ops->get_stats(phydev, stats, *data);
2432 }
2433 
2434 static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2435 					  struct ethtool_stats *stats,
2436 					  u64 **data)
2437 {
2438 	const struct ethtool_ops *ops = dev->ethtool_ops;
2439 	int n_stats, ret;
2440 
2441 	if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats)
2442 		return -EOPNOTSUPP;
2443 
2444 	n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2445 
2446 	ret = ethtool_vzalloc_stats_array(n_stats, data);
2447 	if (ret)
2448 		return ret;
2449 
2450 	stats->n_stats = n_stats;
2451 	ops->get_ethtool_phy_stats(dev, stats, *data);
2452 
2453 	return 0;
2454 }
2455 
2456 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2457 {
2458 	struct phy_device *phydev = dev->phydev;
2459 	struct ethtool_stats stats;
2460 	u64 *data = NULL;
2461 	int ret = -EOPNOTSUPP;
2462 
2463 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
2464 		return -EFAULT;
2465 
2466 	if (phydev)
2467 		ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
2468 
2469 	if (ret == -EOPNOTSUPP)
2470 		ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
2471 
2472 	if (ret)
2473 		goto out;
2474 
2475 	if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2476 		ret = -EFAULT;
2477 		goto out;
2478 	}
2479 
2480 	useraddr += sizeof(stats);
2481 	if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2482 		ret = -EFAULT;
2483 
2484  out:
2485 	vfree(data);
2486 	return ret;
2487 }
2488 
2489 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2490 {
2491 	struct ethtool_perm_addr epaddr;
2492 
2493 	if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2494 		return -EFAULT;
2495 
2496 	if (epaddr.size < dev->addr_len)
2497 		return -ETOOSMALL;
2498 	epaddr.size = dev->addr_len;
2499 
2500 	if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2501 		return -EFAULT;
2502 	useraddr += sizeof(epaddr);
2503 	if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2504 		return -EFAULT;
2505 	return 0;
2506 }
2507 
2508 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2509 			     u32 cmd, u32 (*actor)(struct net_device *))
2510 {
2511 	struct ethtool_value edata = { .cmd = cmd };
2512 
2513 	if (!actor)
2514 		return -EOPNOTSUPP;
2515 
2516 	edata.data = actor(dev);
2517 
2518 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
2519 		return -EFAULT;
2520 	return 0;
2521 }
2522 
2523 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2524 			     void (*actor)(struct net_device *, u32))
2525 {
2526 	struct ethtool_value edata;
2527 
2528 	if (!actor)
2529 		return -EOPNOTSUPP;
2530 
2531 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
2532 		return -EFAULT;
2533 
2534 	actor(dev, edata.data);
2535 	return 0;
2536 }
2537 
2538 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2539 			     int (*actor)(struct net_device *, u32))
2540 {
2541 	struct ethtool_value edata;
2542 
2543 	if (!actor)
2544 		return -EOPNOTSUPP;
2545 
2546 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
2547 		return -EFAULT;
2548 
2549 	return actor(dev, edata.data);
2550 }
2551 
2552 static int
2553 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2554 {
2555 	if (!dev->ethtool_ops->flash_device) {
2556 		req->devlink = netdev_to_devlink_get(dev);
2557 		return 0;
2558 	}
2559 
2560 	return dev->ethtool_ops->flash_device(dev, &req->efl);
2561 }
2562 
2563 static int ethtool_set_dump(struct net_device *dev,
2564 			void __user *useraddr)
2565 {
2566 	struct ethtool_dump dump;
2567 
2568 	if (!dev->ethtool_ops->set_dump)
2569 		return -EOPNOTSUPP;
2570 
2571 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
2572 		return -EFAULT;
2573 
2574 	return dev->ethtool_ops->set_dump(dev, &dump);
2575 }
2576 
2577 static int ethtool_get_dump_flag(struct net_device *dev,
2578 				void __user *useraddr)
2579 {
2580 	int ret;
2581 	struct ethtool_dump dump;
2582 	const struct ethtool_ops *ops = dev->ethtool_ops;
2583 
2584 	if (!ops->get_dump_flag)
2585 		return -EOPNOTSUPP;
2586 
2587 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
2588 		return -EFAULT;
2589 
2590 	ret = ops->get_dump_flag(dev, &dump);
2591 	if (ret)
2592 		return ret;
2593 
2594 	if (copy_to_user(useraddr, &dump, sizeof(dump)))
2595 		return -EFAULT;
2596 	return 0;
2597 }
2598 
2599 static int ethtool_get_dump_data(struct net_device *dev,
2600 				void __user *useraddr)
2601 {
2602 	int ret;
2603 	__u32 len;
2604 	struct ethtool_dump dump, tmp;
2605 	const struct ethtool_ops *ops = dev->ethtool_ops;
2606 	void *data = NULL;
2607 
2608 	if (!ops->get_dump_data || !ops->get_dump_flag)
2609 		return -EOPNOTSUPP;
2610 
2611 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
2612 		return -EFAULT;
2613 
2614 	memset(&tmp, 0, sizeof(tmp));
2615 	tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2616 	ret = ops->get_dump_flag(dev, &tmp);
2617 	if (ret)
2618 		return ret;
2619 
2620 	len = min(tmp.len, dump.len);
2621 	if (!len)
2622 		return -EFAULT;
2623 
2624 	/* Don't ever let the driver think there's more space available
2625 	 * than it requested with .get_dump_flag().
2626 	 */
2627 	dump.len = len;
2628 
2629 	/* Always allocate enough space to hold the whole thing so that the
2630 	 * driver does not need to check the length and bother with partial
2631 	 * dumping.
2632 	 */
2633 	data = vzalloc(tmp.len);
2634 	if (!data)
2635 		return -ENOMEM;
2636 	ret = ops->get_dump_data(dev, &dump, data);
2637 	if (ret)
2638 		goto out;
2639 
2640 	/* There are two sane possibilities:
2641 	 * 1. The driver's .get_dump_data() does not touch dump.len.
2642 	 * 2. Or it may set dump.len to how much it really writes, which
2643 	 *    should be tmp.len (or len if it can do a partial dump).
2644 	 * In any case respond to userspace with the actual length of data
2645 	 * it's receiving.
2646 	 */
2647 	WARN_ON(dump.len != len && dump.len != tmp.len);
2648 	dump.len = len;
2649 
2650 	if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2651 		ret = -EFAULT;
2652 		goto out;
2653 	}
2654 	useraddr += offsetof(struct ethtool_dump, data);
2655 	if (copy_to_user(useraddr, data, len))
2656 		ret = -EFAULT;
2657 out:
2658 	vfree(data);
2659 	return ret;
2660 }
2661 
2662 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2663 {
2664 	struct kernel_ethtool_ts_info kernel_info;
2665 	struct ethtool_ts_info info = {};
2666 	int err;
2667 
2668 	err = __ethtool_get_ts_info(dev, &kernel_info);
2669 	if (err)
2670 		return err;
2671 
2672 	info.cmd = kernel_info.cmd;
2673 	info.so_timestamping = kernel_info.so_timestamping;
2674 	info.phc_index = kernel_info.phc_index;
2675 	info.tx_types = kernel_info.tx_types;
2676 	info.rx_filters = kernel_info.rx_filters;
2677 
2678 	if (copy_to_user(useraddr, &info, sizeof(info)))
2679 		return -EFAULT;
2680 
2681 	return 0;
2682 }
2683 
2684 int ethtool_get_module_info_call(struct net_device *dev,
2685 				 struct ethtool_modinfo *modinfo)
2686 {
2687 	const struct ethtool_ops *ops = dev->ethtool_ops;
2688 	struct phy_device *phydev = dev->phydev;
2689 
2690 	if (dev->ethtool->module_fw_flash_in_progress)
2691 		return -EBUSY;
2692 
2693 	if (dev->sfp_bus)
2694 		return sfp_get_module_info(dev->sfp_bus, modinfo);
2695 
2696 	if (phydev && phydev->drv && phydev->drv->module_info)
2697 		return phydev->drv->module_info(phydev, modinfo);
2698 
2699 	if (ops->get_module_info)
2700 		return ops->get_module_info(dev, modinfo);
2701 
2702 	return -EOPNOTSUPP;
2703 }
2704 
2705 static int ethtool_get_module_info(struct net_device *dev,
2706 				   void __user *useraddr)
2707 {
2708 	int ret;
2709 	struct ethtool_modinfo modinfo;
2710 
2711 	if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2712 		return -EFAULT;
2713 
2714 	ret = ethtool_get_module_info_call(dev, &modinfo);
2715 	if (ret)
2716 		return ret;
2717 
2718 	if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2719 		return -EFAULT;
2720 
2721 	return 0;
2722 }
2723 
2724 int ethtool_get_module_eeprom_call(struct net_device *dev,
2725 				   struct ethtool_eeprom *ee, u8 *data)
2726 {
2727 	const struct ethtool_ops *ops = dev->ethtool_ops;
2728 	struct phy_device *phydev = dev->phydev;
2729 
2730 	if (dev->ethtool->module_fw_flash_in_progress)
2731 		return -EBUSY;
2732 
2733 	if (dev->sfp_bus)
2734 		return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2735 
2736 	if (phydev && phydev->drv && phydev->drv->module_eeprom)
2737 		return phydev->drv->module_eeprom(phydev, ee, data);
2738 
2739 	if (ops->get_module_eeprom)
2740 		return ops->get_module_eeprom(dev, ee, data);
2741 
2742 	return -EOPNOTSUPP;
2743 }
2744 
2745 static int ethtool_get_module_eeprom(struct net_device *dev,
2746 				     void __user *useraddr)
2747 {
2748 	int ret;
2749 	struct ethtool_modinfo modinfo;
2750 
2751 	ret = ethtool_get_module_info_call(dev, &modinfo);
2752 	if (ret)
2753 		return ret;
2754 
2755 	return ethtool_get_any_eeprom(dev, useraddr,
2756 				      ethtool_get_module_eeprom_call,
2757 				      modinfo.eeprom_len);
2758 }
2759 
2760 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2761 {
2762 	switch (tuna->id) {
2763 	case ETHTOOL_RX_COPYBREAK:
2764 	case ETHTOOL_TX_COPYBREAK:
2765 	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2766 		if (tuna->len != sizeof(u32) ||
2767 		    tuna->type_id != ETHTOOL_TUNABLE_U32)
2768 			return -EINVAL;
2769 		break;
2770 	case ETHTOOL_PFC_PREVENTION_TOUT:
2771 		if (tuna->len != sizeof(u16) ||
2772 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
2773 			return -EINVAL;
2774 		break;
2775 	default:
2776 		return -EINVAL;
2777 	}
2778 
2779 	return 0;
2780 }
2781 
2782 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2783 {
2784 	int ret;
2785 	struct ethtool_tunable tuna;
2786 	const struct ethtool_ops *ops = dev->ethtool_ops;
2787 	void *data;
2788 
2789 	if (!ops->get_tunable)
2790 		return -EOPNOTSUPP;
2791 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2792 		return -EFAULT;
2793 	ret = ethtool_tunable_valid(&tuna);
2794 	if (ret)
2795 		return ret;
2796 	data = kzalloc(tuna.len, GFP_USER);
2797 	if (!data)
2798 		return -ENOMEM;
2799 	ret = ops->get_tunable(dev, &tuna, data);
2800 	if (ret)
2801 		goto out;
2802 	useraddr += sizeof(tuna);
2803 	ret = -EFAULT;
2804 	if (copy_to_user(useraddr, data, tuna.len))
2805 		goto out;
2806 	ret = 0;
2807 
2808 out:
2809 	kfree(data);
2810 	return ret;
2811 }
2812 
2813 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2814 {
2815 	int ret;
2816 	struct ethtool_tunable tuna;
2817 	const struct ethtool_ops *ops = dev->ethtool_ops;
2818 	void *data;
2819 
2820 	if (!ops->set_tunable)
2821 		return -EOPNOTSUPP;
2822 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2823 		return -EFAULT;
2824 	ret = ethtool_tunable_valid(&tuna);
2825 	if (ret)
2826 		return ret;
2827 	useraddr += sizeof(tuna);
2828 	data = memdup_user(useraddr, tuna.len);
2829 	if (IS_ERR(data))
2830 		return PTR_ERR(data);
2831 	ret = ops->set_tunable(dev, &tuna, data);
2832 
2833 	kfree(data);
2834 	return ret;
2835 }
2836 
2837 static noinline_for_stack int
2838 ethtool_get_per_queue_coalesce(struct net_device *dev,
2839 			       void __user *useraddr,
2840 			       struct ethtool_per_queue_op *per_queue_opt)
2841 {
2842 	u32 bit;
2843 	int ret;
2844 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2845 
2846 	if (!dev->ethtool_ops->get_per_queue_coalesce)
2847 		return -EOPNOTSUPP;
2848 
2849 	useraddr += sizeof(*per_queue_opt);
2850 
2851 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2852 			  MAX_NUM_QUEUE);
2853 
2854 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2855 		struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2856 
2857 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2858 		if (ret != 0)
2859 			return ret;
2860 		if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2861 			return -EFAULT;
2862 		useraddr += sizeof(coalesce);
2863 	}
2864 
2865 	return 0;
2866 }
2867 
2868 static noinline_for_stack int
2869 ethtool_set_per_queue_coalesce(struct net_device *dev,
2870 			       void __user *useraddr,
2871 			       struct ethtool_per_queue_op *per_queue_opt)
2872 {
2873 	u32 bit;
2874 	int i, ret = 0;
2875 	int n_queue;
2876 	struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2877 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2878 
2879 	if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2880 	    (!dev->ethtool_ops->get_per_queue_coalesce))
2881 		return -EOPNOTSUPP;
2882 
2883 	useraddr += sizeof(*per_queue_opt);
2884 
2885 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2886 	n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2887 	tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2888 	if (!backup)
2889 		return -ENOMEM;
2890 
2891 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2892 		struct ethtool_coalesce coalesce;
2893 
2894 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2895 		if (ret != 0)
2896 			goto roll_back;
2897 
2898 		tmp++;
2899 
2900 		if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2901 			ret = -EFAULT;
2902 			goto roll_back;
2903 		}
2904 
2905 		if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2906 			ret = -EOPNOTSUPP;
2907 			goto roll_back;
2908 		}
2909 
2910 		ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2911 		if (ret != 0)
2912 			goto roll_back;
2913 
2914 		useraddr += sizeof(coalesce);
2915 	}
2916 
2917 roll_back:
2918 	if (ret != 0) {
2919 		tmp = backup;
2920 		for_each_set_bit(i, queue_mask, bit) {
2921 			dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2922 			tmp++;
2923 		}
2924 	}
2925 	kfree(backup);
2926 
2927 	return ret;
2928 }
2929 
2930 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2931 				 void __user *useraddr, u32 sub_cmd)
2932 {
2933 	struct ethtool_per_queue_op per_queue_opt;
2934 
2935 	if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2936 		return -EFAULT;
2937 
2938 	if (per_queue_opt.sub_command != sub_cmd)
2939 		return -EINVAL;
2940 
2941 	switch (per_queue_opt.sub_command) {
2942 	case ETHTOOL_GCOALESCE:
2943 		return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2944 	case ETHTOOL_SCOALESCE:
2945 		return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2946 	default:
2947 		return -EOPNOTSUPP;
2948 	}
2949 }
2950 
2951 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2952 {
2953 	switch (tuna->id) {
2954 	case ETHTOOL_PHY_DOWNSHIFT:
2955 	case ETHTOOL_PHY_FAST_LINK_DOWN:
2956 		if (tuna->len != sizeof(u8) ||
2957 		    tuna->type_id != ETHTOOL_TUNABLE_U8)
2958 			return -EINVAL;
2959 		break;
2960 	case ETHTOOL_PHY_EDPD:
2961 		if (tuna->len != sizeof(u16) ||
2962 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
2963 			return -EINVAL;
2964 		break;
2965 	default:
2966 		return -EINVAL;
2967 	}
2968 
2969 	return 0;
2970 }
2971 
2972 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2973 {
2974 	struct phy_device *phydev = dev->phydev;
2975 	struct ethtool_tunable tuna;
2976 	bool phy_drv_tunable;
2977 	void *data;
2978 	int ret;
2979 
2980 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2981 	if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2982 		return -EOPNOTSUPP;
2983 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2984 		return -EFAULT;
2985 	ret = ethtool_phy_tunable_valid(&tuna);
2986 	if (ret)
2987 		return ret;
2988 	data = kzalloc(tuna.len, GFP_USER);
2989 	if (!data)
2990 		return -ENOMEM;
2991 	if (phy_drv_tunable) {
2992 		mutex_lock(&phydev->lock);
2993 		ret = phydev->drv->get_tunable(phydev, &tuna, data);
2994 		mutex_unlock(&phydev->lock);
2995 	} else {
2996 		ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2997 	}
2998 	if (ret)
2999 		goto out;
3000 	useraddr += sizeof(tuna);
3001 	ret = -EFAULT;
3002 	if (copy_to_user(useraddr, data, tuna.len))
3003 		goto out;
3004 	ret = 0;
3005 
3006 out:
3007 	kfree(data);
3008 	return ret;
3009 }
3010 
3011 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
3012 {
3013 	struct phy_device *phydev = dev->phydev;
3014 	struct ethtool_tunable tuna;
3015 	bool phy_drv_tunable;
3016 	void *data;
3017 	int ret;
3018 
3019 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
3020 	if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
3021 		return -EOPNOTSUPP;
3022 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
3023 		return -EFAULT;
3024 	ret = ethtool_phy_tunable_valid(&tuna);
3025 	if (ret)
3026 		return ret;
3027 	useraddr += sizeof(tuna);
3028 	data = memdup_user(useraddr, tuna.len);
3029 	if (IS_ERR(data))
3030 		return PTR_ERR(data);
3031 	if (phy_drv_tunable) {
3032 		mutex_lock(&phydev->lock);
3033 		ret = phydev->drv->set_tunable(phydev, &tuna, data);
3034 		mutex_unlock(&phydev->lock);
3035 	} else {
3036 		ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
3037 	}
3038 
3039 	kfree(data);
3040 	return ret;
3041 }
3042 
3043 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
3044 {
3045 	struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
3046 	int rc;
3047 
3048 	if (!dev->ethtool_ops->get_fecparam)
3049 		return -EOPNOTSUPP;
3050 
3051 	rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
3052 	if (rc)
3053 		return rc;
3054 
3055 	if (WARN_ON_ONCE(fecparam.reserved))
3056 		fecparam.reserved = 0;
3057 
3058 	if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
3059 		return -EFAULT;
3060 	return 0;
3061 }
3062 
3063 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
3064 {
3065 	struct ethtool_fecparam fecparam;
3066 
3067 	if (!dev->ethtool_ops->set_fecparam)
3068 		return -EOPNOTSUPP;
3069 
3070 	if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
3071 		return -EFAULT;
3072 
3073 	if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
3074 		return -EINVAL;
3075 
3076 	fecparam.active_fec = 0;
3077 	fecparam.reserved = 0;
3078 
3079 	return dev->ethtool_ops->set_fecparam(dev, &fecparam);
3080 }
3081 
3082 /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
3083 
3084 static int
3085 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
3086 	      u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
3087 {
3088 	struct net_device *dev;
3089 	u32 sub_cmd;
3090 	int rc;
3091 	netdev_features_t old_features;
3092 
3093 	dev = __dev_get_by_name(net, ifr->ifr_name);
3094 	if (!dev)
3095 		return -ENODEV;
3096 
3097 	if (ethcmd == ETHTOOL_PERQUEUE) {
3098 		if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
3099 			return -EFAULT;
3100 	} else {
3101 		sub_cmd = ethcmd;
3102 	}
3103 	/* Allow some commands to be done by anyone */
3104 	switch (sub_cmd) {
3105 	case ETHTOOL_GSET:
3106 	case ETHTOOL_GDRVINFO:
3107 	case ETHTOOL_GMSGLVL:
3108 	case ETHTOOL_GLINK:
3109 	case ETHTOOL_GCOALESCE:
3110 	case ETHTOOL_GRINGPARAM:
3111 	case ETHTOOL_GPAUSEPARAM:
3112 	case ETHTOOL_GRXCSUM:
3113 	case ETHTOOL_GTXCSUM:
3114 	case ETHTOOL_GSG:
3115 	case ETHTOOL_GSSET_INFO:
3116 	case ETHTOOL_GSTRINGS:
3117 	case ETHTOOL_GSTATS:
3118 	case ETHTOOL_GPHYSTATS:
3119 	case ETHTOOL_GTSO:
3120 	case ETHTOOL_GPERMADDR:
3121 	case ETHTOOL_GUFO:
3122 	case ETHTOOL_GGSO:
3123 	case ETHTOOL_GGRO:
3124 	case ETHTOOL_GFLAGS:
3125 	case ETHTOOL_GPFLAGS:
3126 	case ETHTOOL_GRXFH:
3127 	case ETHTOOL_GRXRINGS:
3128 	case ETHTOOL_GRXCLSRLCNT:
3129 	case ETHTOOL_GRXCLSRULE:
3130 	case ETHTOOL_GRXCLSRLALL:
3131 	case ETHTOOL_GRXFHINDIR:
3132 	case ETHTOOL_GRSSH:
3133 	case ETHTOOL_GFEATURES:
3134 	case ETHTOOL_GCHANNELS:
3135 	case ETHTOOL_GET_TS_INFO:
3136 	case ETHTOOL_GEEE:
3137 	case ETHTOOL_GTUNABLE:
3138 	case ETHTOOL_PHY_GTUNABLE:
3139 	case ETHTOOL_GLINKSETTINGS:
3140 	case ETHTOOL_GFECPARAM:
3141 		break;
3142 	default:
3143 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3144 			return -EPERM;
3145 	}
3146 
3147 	netdev_lock_ops(dev);
3148 	if (dev->dev.parent)
3149 		pm_runtime_get_sync(dev->dev.parent);
3150 
3151 	if (!netif_device_present(dev)) {
3152 		rc = -ENODEV;
3153 		goto out;
3154 	}
3155 
3156 	if (dev->ethtool_ops->begin) {
3157 		rc = dev->ethtool_ops->begin(dev);
3158 		if (rc < 0)
3159 			goto out;
3160 	}
3161 	old_features = dev->features;
3162 
3163 	switch (ethcmd) {
3164 	case ETHTOOL_GSET:
3165 		rc = ethtool_get_settings(dev, useraddr);
3166 		break;
3167 	case ETHTOOL_SSET:
3168 		rc = ethtool_set_settings(dev, useraddr);
3169 		break;
3170 	case ETHTOOL_GDRVINFO:
3171 		rc = ethtool_get_drvinfo(dev, devlink_state);
3172 		break;
3173 	case ETHTOOL_GREGS:
3174 		rc = ethtool_get_regs(dev, useraddr);
3175 		break;
3176 	case ETHTOOL_GWOL:
3177 		rc = ethtool_get_wol(dev, useraddr);
3178 		break;
3179 	case ETHTOOL_SWOL:
3180 		rc = ethtool_set_wol(dev, useraddr);
3181 		break;
3182 	case ETHTOOL_GMSGLVL:
3183 		rc = ethtool_get_value(dev, useraddr, ethcmd,
3184 				       dev->ethtool_ops->get_msglevel);
3185 		break;
3186 	case ETHTOOL_SMSGLVL:
3187 		rc = ethtool_set_value_void(dev, useraddr,
3188 				       dev->ethtool_ops->set_msglevel);
3189 		if (!rc)
3190 			ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
3191 		break;
3192 	case ETHTOOL_GEEE:
3193 		rc = ethtool_get_eee(dev, useraddr);
3194 		break;
3195 	case ETHTOOL_SEEE:
3196 		rc = ethtool_set_eee(dev, useraddr);
3197 		break;
3198 	case ETHTOOL_NWAY_RST:
3199 		rc = ethtool_nway_reset(dev);
3200 		break;
3201 	case ETHTOOL_GLINK:
3202 		rc = ethtool_get_link(dev, useraddr);
3203 		break;
3204 	case ETHTOOL_GEEPROM:
3205 		rc = ethtool_get_eeprom(dev, useraddr);
3206 		break;
3207 	case ETHTOOL_SEEPROM:
3208 		rc = ethtool_set_eeprom(dev, useraddr);
3209 		break;
3210 	case ETHTOOL_GCOALESCE:
3211 		rc = ethtool_get_coalesce(dev, useraddr);
3212 		break;
3213 	case ETHTOOL_SCOALESCE:
3214 		rc = ethtool_set_coalesce(dev, useraddr);
3215 		break;
3216 	case ETHTOOL_GRINGPARAM:
3217 		rc = ethtool_get_ringparam(dev, useraddr);
3218 		break;
3219 	case ETHTOOL_SRINGPARAM:
3220 		rc = ethtool_set_ringparam(dev, useraddr);
3221 		break;
3222 	case ETHTOOL_GPAUSEPARAM:
3223 		rc = ethtool_get_pauseparam(dev, useraddr);
3224 		break;
3225 	case ETHTOOL_SPAUSEPARAM:
3226 		rc = ethtool_set_pauseparam(dev, useraddr);
3227 		break;
3228 	case ETHTOOL_TEST:
3229 		rc = ethtool_self_test(dev, useraddr);
3230 		break;
3231 	case ETHTOOL_GSTRINGS:
3232 		rc = ethtool_get_strings(dev, useraddr);
3233 		break;
3234 	case ETHTOOL_PHYS_ID:
3235 		rc = ethtool_phys_id(dev, useraddr);
3236 		break;
3237 	case ETHTOOL_GSTATS:
3238 		rc = ethtool_get_stats(dev, useraddr);
3239 		break;
3240 	case ETHTOOL_GPERMADDR:
3241 		rc = ethtool_get_perm_addr(dev, useraddr);
3242 		break;
3243 	case ETHTOOL_GFLAGS:
3244 		rc = ethtool_get_value(dev, useraddr, ethcmd,
3245 					__ethtool_get_flags);
3246 		break;
3247 	case ETHTOOL_SFLAGS:
3248 		rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3249 		break;
3250 	case ETHTOOL_GPFLAGS:
3251 		rc = ethtool_get_value(dev, useraddr, ethcmd,
3252 				       dev->ethtool_ops->get_priv_flags);
3253 		if (!rc)
3254 			ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
3255 		break;
3256 	case ETHTOOL_SPFLAGS:
3257 		rc = ethtool_set_value(dev, useraddr,
3258 				       dev->ethtool_ops->set_priv_flags);
3259 		break;
3260 	case ETHTOOL_GRXFH:
3261 	case ETHTOOL_GRXRINGS:
3262 	case ETHTOOL_GRXCLSRLCNT:
3263 	case ETHTOOL_GRXCLSRULE:
3264 	case ETHTOOL_GRXCLSRLALL:
3265 		rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
3266 		break;
3267 	case ETHTOOL_SRXFH:
3268 	case ETHTOOL_SRXCLSRLDEL:
3269 	case ETHTOOL_SRXCLSRLINS:
3270 		rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
3271 		break;
3272 	case ETHTOOL_FLASHDEV:
3273 		rc = ethtool_flash_device(dev, devlink_state);
3274 		break;
3275 	case ETHTOOL_RESET:
3276 		rc = ethtool_reset(dev, useraddr);
3277 		break;
3278 	case ETHTOOL_GSSET_INFO:
3279 		rc = ethtool_get_sset_info(dev, useraddr);
3280 		break;
3281 	case ETHTOOL_GRXFHINDIR:
3282 		rc = ethtool_get_rxfh_indir(dev, useraddr);
3283 		break;
3284 	case ETHTOOL_SRXFHINDIR:
3285 		rc = ethtool_set_rxfh_indir(dev, useraddr);
3286 		break;
3287 	case ETHTOOL_GRSSH:
3288 		rc = ethtool_get_rxfh(dev, useraddr);
3289 		break;
3290 	case ETHTOOL_SRSSH:
3291 		rc = ethtool_set_rxfh(dev, useraddr);
3292 		break;
3293 	case ETHTOOL_GFEATURES:
3294 		rc = ethtool_get_features(dev, useraddr);
3295 		break;
3296 	case ETHTOOL_SFEATURES:
3297 		rc = ethtool_set_features(dev, useraddr);
3298 		break;
3299 	case ETHTOOL_GTXCSUM:
3300 	case ETHTOOL_GRXCSUM:
3301 	case ETHTOOL_GSG:
3302 	case ETHTOOL_GTSO:
3303 	case ETHTOOL_GGSO:
3304 	case ETHTOOL_GGRO:
3305 		rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
3306 		break;
3307 	case ETHTOOL_STXCSUM:
3308 	case ETHTOOL_SRXCSUM:
3309 	case ETHTOOL_SSG:
3310 	case ETHTOOL_STSO:
3311 	case ETHTOOL_SGSO:
3312 	case ETHTOOL_SGRO:
3313 		rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
3314 		break;
3315 	case ETHTOOL_GCHANNELS:
3316 		rc = ethtool_get_channels(dev, useraddr);
3317 		break;
3318 	case ETHTOOL_SCHANNELS:
3319 		rc = ethtool_set_channels(dev, useraddr);
3320 		break;
3321 	case ETHTOOL_SET_DUMP:
3322 		rc = ethtool_set_dump(dev, useraddr);
3323 		break;
3324 	case ETHTOOL_GET_DUMP_FLAG:
3325 		rc = ethtool_get_dump_flag(dev, useraddr);
3326 		break;
3327 	case ETHTOOL_GET_DUMP_DATA:
3328 		rc = ethtool_get_dump_data(dev, useraddr);
3329 		break;
3330 	case ETHTOOL_GET_TS_INFO:
3331 		rc = ethtool_get_ts_info(dev, useraddr);
3332 		break;
3333 	case ETHTOOL_GMODULEINFO:
3334 		rc = ethtool_get_module_info(dev, useraddr);
3335 		break;
3336 	case ETHTOOL_GMODULEEEPROM:
3337 		rc = ethtool_get_module_eeprom(dev, useraddr);
3338 		break;
3339 	case ETHTOOL_GTUNABLE:
3340 		rc = ethtool_get_tunable(dev, useraddr);
3341 		break;
3342 	case ETHTOOL_STUNABLE:
3343 		rc = ethtool_set_tunable(dev, useraddr);
3344 		break;
3345 	case ETHTOOL_GPHYSTATS:
3346 		rc = ethtool_get_phy_stats(dev, useraddr);
3347 		break;
3348 	case ETHTOOL_PERQUEUE:
3349 		rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
3350 		break;
3351 	case ETHTOOL_GLINKSETTINGS:
3352 		rc = ethtool_get_link_ksettings(dev, useraddr);
3353 		break;
3354 	case ETHTOOL_SLINKSETTINGS:
3355 		rc = ethtool_set_link_ksettings(dev, useraddr);
3356 		break;
3357 	case ETHTOOL_PHY_GTUNABLE:
3358 		rc = get_phy_tunable(dev, useraddr);
3359 		break;
3360 	case ETHTOOL_PHY_STUNABLE:
3361 		rc = set_phy_tunable(dev, useraddr);
3362 		break;
3363 	case ETHTOOL_GFECPARAM:
3364 		rc = ethtool_get_fecparam(dev, useraddr);
3365 		break;
3366 	case ETHTOOL_SFECPARAM:
3367 		rc = ethtool_set_fecparam(dev, useraddr);
3368 		break;
3369 	default:
3370 		rc = -EOPNOTSUPP;
3371 	}
3372 
3373 	if (dev->ethtool_ops->complete)
3374 		dev->ethtool_ops->complete(dev);
3375 
3376 	if (old_features != dev->features)
3377 		netdev_features_change(dev);
3378 out:
3379 	if (dev->dev.parent)
3380 		pm_runtime_put(dev->dev.parent);
3381 	netdev_unlock_ops(dev);
3382 
3383 	return rc;
3384 }
3385 
3386 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3387 {
3388 	struct ethtool_devlink_compat *state;
3389 	u32 ethcmd;
3390 	int rc;
3391 
3392 	if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3393 		return -EFAULT;
3394 
3395 	state = kzalloc(sizeof(*state), GFP_KERNEL);
3396 	if (!state)
3397 		return -ENOMEM;
3398 
3399 	switch (ethcmd) {
3400 	case ETHTOOL_FLASHDEV:
3401 		if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3402 			rc = -EFAULT;
3403 			goto exit_free;
3404 		}
3405 		state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3406 		break;
3407 	}
3408 
3409 	rtnl_lock();
3410 	rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3411 	rtnl_unlock();
3412 	if (rc)
3413 		goto exit_free;
3414 
3415 	switch (ethcmd) {
3416 	case ETHTOOL_FLASHDEV:
3417 		if (state->devlink)
3418 			rc = devlink_compat_flash_update(state->devlink,
3419 							 state->efl.data);
3420 		break;
3421 	case ETHTOOL_GDRVINFO:
3422 		if (state->devlink)
3423 			devlink_compat_running_version(state->devlink,
3424 						       state->info.fw_version,
3425 						       sizeof(state->info.fw_version));
3426 		if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3427 			rc = -EFAULT;
3428 			goto exit_free;
3429 		}
3430 		break;
3431 	}
3432 
3433 exit_free:
3434 	if (state->devlink)
3435 		devlink_put(state->devlink);
3436 	kfree(state);
3437 	return rc;
3438 }
3439 
3440 struct ethtool_rx_flow_key {
3441 	struct flow_dissector_key_basic			basic;
3442 	union {
3443 		struct flow_dissector_key_ipv4_addrs	ipv4;
3444 		struct flow_dissector_key_ipv6_addrs	ipv6;
3445 	};
3446 	struct flow_dissector_key_ports			tp;
3447 	struct flow_dissector_key_ip			ip;
3448 	struct flow_dissector_key_vlan			vlan;
3449 	struct flow_dissector_key_eth_addrs		eth_addrs;
3450 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
3451 
3452 struct ethtool_rx_flow_match {
3453 	struct flow_dissector		dissector;
3454 	struct ethtool_rx_flow_key	key;
3455 	struct ethtool_rx_flow_key	mask;
3456 };
3457 
3458 struct ethtool_rx_flow_rule *
3459 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3460 {
3461 	const struct ethtool_rx_flow_spec *fs = input->fs;
3462 	struct ethtool_rx_flow_match *match;
3463 	struct ethtool_rx_flow_rule *flow;
3464 	struct flow_action_entry *act;
3465 
3466 	flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3467 		       sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3468 	if (!flow)
3469 		return ERR_PTR(-ENOMEM);
3470 
3471 	/* ethtool_rx supports only one single action per rule. */
3472 	flow->rule = flow_rule_alloc(1);
3473 	if (!flow->rule) {
3474 		kfree(flow);
3475 		return ERR_PTR(-ENOMEM);
3476 	}
3477 
3478 	match = (struct ethtool_rx_flow_match *)flow->priv;
3479 	flow->rule->match.dissector	= &match->dissector;
3480 	flow->rule->match.mask		= &match->mask;
3481 	flow->rule->match.key		= &match->key;
3482 
3483 	match->mask.basic.n_proto = htons(0xffff);
3484 
3485 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3486 	case ETHER_FLOW: {
3487 		const struct ethhdr *ether_spec, *ether_m_spec;
3488 
3489 		ether_spec = &fs->h_u.ether_spec;
3490 		ether_m_spec = &fs->m_u.ether_spec;
3491 
3492 		if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3493 			ether_addr_copy(match->key.eth_addrs.src,
3494 					ether_spec->h_source);
3495 			ether_addr_copy(match->mask.eth_addrs.src,
3496 					ether_m_spec->h_source);
3497 		}
3498 		if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3499 			ether_addr_copy(match->key.eth_addrs.dst,
3500 					ether_spec->h_dest);
3501 			ether_addr_copy(match->mask.eth_addrs.dst,
3502 					ether_m_spec->h_dest);
3503 		}
3504 		if (ether_m_spec->h_proto) {
3505 			match->key.basic.n_proto = ether_spec->h_proto;
3506 			match->mask.basic.n_proto = ether_m_spec->h_proto;
3507 		}
3508 		}
3509 		break;
3510 	case TCP_V4_FLOW:
3511 	case UDP_V4_FLOW: {
3512 		const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3513 
3514 		match->key.basic.n_proto = htons(ETH_P_IP);
3515 
3516 		v4_spec = &fs->h_u.tcp_ip4_spec;
3517 		v4_m_spec = &fs->m_u.tcp_ip4_spec;
3518 
3519 		if (v4_m_spec->ip4src) {
3520 			match->key.ipv4.src = v4_spec->ip4src;
3521 			match->mask.ipv4.src = v4_m_spec->ip4src;
3522 		}
3523 		if (v4_m_spec->ip4dst) {
3524 			match->key.ipv4.dst = v4_spec->ip4dst;
3525 			match->mask.ipv4.dst = v4_m_spec->ip4dst;
3526 		}
3527 		if (v4_m_spec->ip4src ||
3528 		    v4_m_spec->ip4dst) {
3529 			match->dissector.used_keys |=
3530 				BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3531 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3532 				offsetof(struct ethtool_rx_flow_key, ipv4);
3533 		}
3534 		if (v4_m_spec->psrc) {
3535 			match->key.tp.src = v4_spec->psrc;
3536 			match->mask.tp.src = v4_m_spec->psrc;
3537 		}
3538 		if (v4_m_spec->pdst) {
3539 			match->key.tp.dst = v4_spec->pdst;
3540 			match->mask.tp.dst = v4_m_spec->pdst;
3541 		}
3542 		if (v4_m_spec->psrc ||
3543 		    v4_m_spec->pdst) {
3544 			match->dissector.used_keys |=
3545 				BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3546 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3547 				offsetof(struct ethtool_rx_flow_key, tp);
3548 		}
3549 		if (v4_m_spec->tos) {
3550 			match->key.ip.tos = v4_spec->tos;
3551 			match->mask.ip.tos = v4_m_spec->tos;
3552 			match->dissector.used_keys |=
3553 				BIT(FLOW_DISSECTOR_KEY_IP);
3554 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3555 				offsetof(struct ethtool_rx_flow_key, ip);
3556 		}
3557 		}
3558 		break;
3559 	case TCP_V6_FLOW:
3560 	case UDP_V6_FLOW: {
3561 		const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3562 
3563 		match->key.basic.n_proto = htons(ETH_P_IPV6);
3564 
3565 		v6_spec = &fs->h_u.tcp_ip6_spec;
3566 		v6_m_spec = &fs->m_u.tcp_ip6_spec;
3567 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
3568 			memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3569 			       sizeof(match->key.ipv6.src));
3570 			memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3571 			       sizeof(match->mask.ipv6.src));
3572 		}
3573 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3574 			memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3575 			       sizeof(match->key.ipv6.dst));
3576 			memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3577 			       sizeof(match->mask.ipv6.dst));
3578 		}
3579 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
3580 		    !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
3581 			match->dissector.used_keys |=
3582 				BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3583 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3584 				offsetof(struct ethtool_rx_flow_key, ipv6);
3585 		}
3586 		if (v6_m_spec->psrc) {
3587 			match->key.tp.src = v6_spec->psrc;
3588 			match->mask.tp.src = v6_m_spec->psrc;
3589 		}
3590 		if (v6_m_spec->pdst) {
3591 			match->key.tp.dst = v6_spec->pdst;
3592 			match->mask.tp.dst = v6_m_spec->pdst;
3593 		}
3594 		if (v6_m_spec->psrc ||
3595 		    v6_m_spec->pdst) {
3596 			match->dissector.used_keys |=
3597 				BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
3598 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3599 				offsetof(struct ethtool_rx_flow_key, tp);
3600 		}
3601 		if (v6_m_spec->tclass) {
3602 			match->key.ip.tos = v6_spec->tclass;
3603 			match->mask.ip.tos = v6_m_spec->tclass;
3604 			match->dissector.used_keys |=
3605 				BIT_ULL(FLOW_DISSECTOR_KEY_IP);
3606 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3607 				offsetof(struct ethtool_rx_flow_key, ip);
3608 		}
3609 		}
3610 		break;
3611 	default:
3612 		ethtool_rx_flow_rule_destroy(flow);
3613 		return ERR_PTR(-EINVAL);
3614 	}
3615 
3616 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3617 	case TCP_V4_FLOW:
3618 	case TCP_V6_FLOW:
3619 		match->key.basic.ip_proto = IPPROTO_TCP;
3620 		match->mask.basic.ip_proto = 0xff;
3621 		break;
3622 	case UDP_V4_FLOW:
3623 	case UDP_V6_FLOW:
3624 		match->key.basic.ip_proto = IPPROTO_UDP;
3625 		match->mask.basic.ip_proto = 0xff;
3626 		break;
3627 	}
3628 
3629 	match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
3630 	match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3631 		offsetof(struct ethtool_rx_flow_key, basic);
3632 
3633 	if (fs->flow_type & FLOW_EXT) {
3634 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3635 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3636 
3637 		if (ext_m_spec->vlan_etype) {
3638 			match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3639 			match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3640 		}
3641 
3642 		if (ext_m_spec->vlan_tci) {
3643 			match->key.vlan.vlan_id =
3644 				ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3645 			match->mask.vlan.vlan_id =
3646 				ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3647 
3648 			match->key.vlan.vlan_dei =
3649 				!!(ext_h_spec->vlan_tci & htons(0x1000));
3650 			match->mask.vlan.vlan_dei =
3651 				!!(ext_m_spec->vlan_tci & htons(0x1000));
3652 
3653 			match->key.vlan.vlan_priority =
3654 				(ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3655 			match->mask.vlan.vlan_priority =
3656 				(ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3657 		}
3658 
3659 		if (ext_m_spec->vlan_etype ||
3660 		    ext_m_spec->vlan_tci) {
3661 			match->dissector.used_keys |=
3662 				BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
3663 			match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3664 				offsetof(struct ethtool_rx_flow_key, vlan);
3665 		}
3666 	}
3667 	if (fs->flow_type & FLOW_MAC_EXT) {
3668 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3669 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3670 
3671 		memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3672 		       ETH_ALEN);
3673 		memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3674 		       ETH_ALEN);
3675 
3676 		match->dissector.used_keys |=
3677 			BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3678 		match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3679 			offsetof(struct ethtool_rx_flow_key, eth_addrs);
3680 	}
3681 
3682 	act = &flow->rule->action.entries[0];
3683 	switch (fs->ring_cookie) {
3684 	case RX_CLS_FLOW_DISC:
3685 		act->id = FLOW_ACTION_DROP;
3686 		break;
3687 	case RX_CLS_FLOW_WAKE:
3688 		act->id = FLOW_ACTION_WAKE;
3689 		break;
3690 	default:
3691 		act->id = FLOW_ACTION_QUEUE;
3692 		if (fs->flow_type & FLOW_RSS)
3693 			act->queue.ctx = input->rss_ctx;
3694 
3695 		act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3696 		act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3697 		break;
3698 	}
3699 
3700 	return flow;
3701 }
3702 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3703 
3704 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3705 {
3706 	kfree(flow->rule);
3707 	kfree(flow);
3708 }
3709 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);
3710