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