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