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