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