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