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