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