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