• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sched.h>
28 
29 /*
30  * Some useful ethtool_ops methods that're device independent.
31  * If we find that all drivers want to do the same thing here,
32  * we can turn these into dev_() function calls.
33  */
34 
ethtool_op_get_link(struct net_device * dev)35 u32 ethtool_op_get_link(struct net_device *dev)
36 {
37 	return netif_carrier_ok(dev) ? 1 : 0;
38 }
39 EXPORT_SYMBOL(ethtool_op_get_link);
40 
ethtool_op_get_ts_info(struct net_device * dev,struct ethtool_ts_info * info)41 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
42 {
43 	info->so_timestamping =
44 		SOF_TIMESTAMPING_TX_SOFTWARE |
45 		SOF_TIMESTAMPING_RX_SOFTWARE |
46 		SOF_TIMESTAMPING_SOFTWARE;
47 	info->phc_index = -1;
48 	return 0;
49 }
50 EXPORT_SYMBOL(ethtool_op_get_ts_info);
51 
52 /* Handlers for each ethtool command */
53 
54 #define ETHTOOL_DEV_FEATURE_WORDS	((NETDEV_FEATURE_COUNT + 31) / 32)
55 
56 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
57 	[NETIF_F_SG_BIT] =               "tx-scatter-gather",
58 	[NETIF_F_IP_CSUM_BIT] =          "tx-checksum-ipv4",
59 	[NETIF_F_HW_CSUM_BIT] =          "tx-checksum-ip-generic",
60 	[NETIF_F_IPV6_CSUM_BIT] =        "tx-checksum-ipv6",
61 	[NETIF_F_HIGHDMA_BIT] =          "highdma",
62 	[NETIF_F_FRAGLIST_BIT] =         "tx-scatter-gather-fraglist",
63 	[NETIF_F_HW_VLAN_CTAG_TX_BIT] =  "tx-vlan-hw-insert",
64 
65 	[NETIF_F_HW_VLAN_CTAG_RX_BIT] =  "rx-vlan-hw-parse",
66 	[NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
67 	[NETIF_F_HW_VLAN_STAG_TX_BIT] =  "tx-vlan-stag-hw-insert",
68 	[NETIF_F_HW_VLAN_STAG_RX_BIT] =  "rx-vlan-stag-hw-parse",
69 	[NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
70 	[NETIF_F_VLAN_CHALLENGED_BIT] =  "vlan-challenged",
71 	[NETIF_F_GSO_BIT] =              "tx-generic-segmentation",
72 	[NETIF_F_LLTX_BIT] =             "tx-lockless",
73 	[NETIF_F_NETNS_LOCAL_BIT] =      "netns-local",
74 	[NETIF_F_GRO_BIT] =              "rx-gro",
75 	[NETIF_F_LRO_BIT] =              "rx-lro",
76 
77 	[NETIF_F_TSO_BIT] =              "tx-tcp-segmentation",
78 	[NETIF_F_UFO_BIT] =              "tx-udp-fragmentation",
79 	[NETIF_F_GSO_ROBUST_BIT] =       "tx-gso-robust",
80 	[NETIF_F_TSO_ECN_BIT] =          "tx-tcp-ecn-segmentation",
81 	[NETIF_F_TSO6_BIT] =             "tx-tcp6-segmentation",
82 	[NETIF_F_FSO_BIT] =              "tx-fcoe-segmentation",
83 	[NETIF_F_GSO_GRE_BIT] =		 "tx-gre-segmentation",
84 	[NETIF_F_GSO_UDP_TUNNEL_BIT] =	 "tx-udp_tnl-segmentation",
85 
86 	[NETIF_F_FCOE_CRC_BIT] =         "tx-checksum-fcoe-crc",
87 	[NETIF_F_SCTP_CSUM_BIT] =        "tx-checksum-sctp",
88 	[NETIF_F_FCOE_MTU_BIT] =         "fcoe-mtu",
89 	[NETIF_F_NTUPLE_BIT] =           "rx-ntuple-filter",
90 	[NETIF_F_RXHASH_BIT] =           "rx-hashing",
91 	[NETIF_F_RXCSUM_BIT] =           "rx-checksum",
92 	[NETIF_F_NOCACHE_COPY_BIT] =     "tx-nocache-copy",
93 	[NETIF_F_LOOPBACK_BIT] =         "loopback",
94 	[NETIF_F_RXFCS_BIT] =            "rx-fcs",
95 	[NETIF_F_RXALL_BIT] =            "rx-all",
96 };
97 
ethtool_get_features(struct net_device * dev,void __user * useraddr)98 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
99 {
100 	struct ethtool_gfeatures cmd = {
101 		.cmd = ETHTOOL_GFEATURES,
102 		.size = ETHTOOL_DEV_FEATURE_WORDS,
103 	};
104 	struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
105 	u32 __user *sizeaddr;
106 	u32 copy_size;
107 	int i;
108 
109 	/* in case feature bits run out again */
110 	BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
111 
112 	for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
113 		features[i].available = (u32)(dev->hw_features >> (32 * i));
114 		features[i].requested = (u32)(dev->wanted_features >> (32 * i));
115 		features[i].active = (u32)(dev->features >> (32 * i));
116 		features[i].never_changed =
117 			(u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
118 	}
119 
120 	sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
121 	if (get_user(copy_size, sizeaddr))
122 		return -EFAULT;
123 
124 	if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
125 		copy_size = ETHTOOL_DEV_FEATURE_WORDS;
126 
127 	if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
128 		return -EFAULT;
129 	useraddr += sizeof(cmd);
130 	if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
131 		return -EFAULT;
132 
133 	return 0;
134 }
135 
ethtool_set_features(struct net_device * dev,void __user * useraddr)136 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
137 {
138 	struct ethtool_sfeatures cmd;
139 	struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
140 	netdev_features_t wanted = 0, valid = 0;
141 	int i, ret = 0;
142 
143 	if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
144 		return -EFAULT;
145 	useraddr += sizeof(cmd);
146 
147 	if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
148 		return -EINVAL;
149 
150 	if (copy_from_user(features, useraddr, sizeof(features)))
151 		return -EFAULT;
152 
153 	for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
154 		valid |= (netdev_features_t)features[i].valid << (32 * i);
155 		wanted |= (netdev_features_t)features[i].requested << (32 * i);
156 	}
157 
158 	if (valid & ~NETIF_F_ETHTOOL_BITS)
159 		return -EINVAL;
160 
161 	if (valid & ~dev->hw_features) {
162 		valid &= dev->hw_features;
163 		ret |= ETHTOOL_F_UNSUPPORTED;
164 	}
165 
166 	dev->wanted_features &= ~valid;
167 	dev->wanted_features |= wanted & valid;
168 	__netdev_update_features(dev);
169 
170 	if ((dev->wanted_features ^ dev->features) & valid)
171 		ret |= ETHTOOL_F_WISH;
172 
173 	return ret;
174 }
175 
__ethtool_get_sset_count(struct net_device * dev,int sset)176 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
177 {
178 	const struct ethtool_ops *ops = dev->ethtool_ops;
179 
180 	if (sset == ETH_SS_FEATURES)
181 		return ARRAY_SIZE(netdev_features_strings);
182 
183 	if (ops->get_sset_count && ops->get_strings)
184 		return ops->get_sset_count(dev, sset);
185 	else
186 		return -EOPNOTSUPP;
187 }
188 
__ethtool_get_strings(struct net_device * dev,u32 stringset,u8 * data)189 static void __ethtool_get_strings(struct net_device *dev,
190 	u32 stringset, u8 *data)
191 {
192 	const struct ethtool_ops *ops = dev->ethtool_ops;
193 
194 	if (stringset == ETH_SS_FEATURES)
195 		memcpy(data, netdev_features_strings,
196 			sizeof(netdev_features_strings));
197 	else
198 		/* ops->get_strings is valid because checked earlier */
199 		ops->get_strings(dev, stringset, data);
200 }
201 
ethtool_get_feature_mask(u32 eth_cmd)202 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
203 {
204 	/* feature masks of legacy discrete ethtool ops */
205 
206 	switch (eth_cmd) {
207 	case ETHTOOL_GTXCSUM:
208 	case ETHTOOL_STXCSUM:
209 		return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
210 	case ETHTOOL_GRXCSUM:
211 	case ETHTOOL_SRXCSUM:
212 		return NETIF_F_RXCSUM;
213 	case ETHTOOL_GSG:
214 	case ETHTOOL_SSG:
215 		return NETIF_F_SG;
216 	case ETHTOOL_GTSO:
217 	case ETHTOOL_STSO:
218 		return NETIF_F_ALL_TSO;
219 	case ETHTOOL_GUFO:
220 	case ETHTOOL_SUFO:
221 		return NETIF_F_UFO;
222 	case ETHTOOL_GGSO:
223 	case ETHTOOL_SGSO:
224 		return NETIF_F_GSO;
225 	case ETHTOOL_GGRO:
226 	case ETHTOOL_SGRO:
227 		return NETIF_F_GRO;
228 	default:
229 		BUG();
230 	}
231 }
232 
ethtool_get_one_feature(struct net_device * dev,char __user * useraddr,u32 ethcmd)233 static int ethtool_get_one_feature(struct net_device *dev,
234 	char __user *useraddr, u32 ethcmd)
235 {
236 	netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
237 	struct ethtool_value edata = {
238 		.cmd = ethcmd,
239 		.data = !!(dev->features & mask),
240 	};
241 
242 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
243 		return -EFAULT;
244 	return 0;
245 }
246 
ethtool_set_one_feature(struct net_device * dev,void __user * useraddr,u32 ethcmd)247 static int ethtool_set_one_feature(struct net_device *dev,
248 	void __user *useraddr, u32 ethcmd)
249 {
250 	struct ethtool_value edata;
251 	netdev_features_t mask;
252 
253 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
254 		return -EFAULT;
255 
256 	mask = ethtool_get_feature_mask(ethcmd);
257 	mask &= dev->hw_features;
258 	if (!mask)
259 		return -EOPNOTSUPP;
260 
261 	if (edata.data)
262 		dev->wanted_features |= mask;
263 	else
264 		dev->wanted_features &= ~mask;
265 
266 	__netdev_update_features(dev);
267 
268 	return 0;
269 }
270 
271 #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
272 			  ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
273 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
274 			  NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
275 			  NETIF_F_RXHASH)
276 
__ethtool_get_flags(struct net_device * dev)277 static u32 __ethtool_get_flags(struct net_device *dev)
278 {
279 	u32 flags = 0;
280 
281 	if (dev->features & NETIF_F_LRO)	     flags |= ETH_FLAG_LRO;
282 	if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) flags |= ETH_FLAG_RXVLAN;
283 	if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) flags |= ETH_FLAG_TXVLAN;
284 	if (dev->features & NETIF_F_NTUPLE)	     flags |= ETH_FLAG_NTUPLE;
285 	if (dev->features & NETIF_F_RXHASH)	     flags |= ETH_FLAG_RXHASH;
286 
287 	return flags;
288 }
289 
__ethtool_set_flags(struct net_device * dev,u32 data)290 static int __ethtool_set_flags(struct net_device *dev, u32 data)
291 {
292 	netdev_features_t features = 0, changed;
293 
294 	if (data & ~ETH_ALL_FLAGS)
295 		return -EINVAL;
296 
297 	if (data & ETH_FLAG_LRO)	features |= NETIF_F_LRO;
298 	if (data & ETH_FLAG_RXVLAN)	features |= NETIF_F_HW_VLAN_CTAG_RX;
299 	if (data & ETH_FLAG_TXVLAN)	features |= NETIF_F_HW_VLAN_CTAG_TX;
300 	if (data & ETH_FLAG_NTUPLE)	features |= NETIF_F_NTUPLE;
301 	if (data & ETH_FLAG_RXHASH)	features |= NETIF_F_RXHASH;
302 
303 	/* allow changing only bits set in hw_features */
304 	changed = (features ^ dev->features) & ETH_ALL_FEATURES;
305 	if (changed & ~dev->hw_features)
306 		return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
307 
308 	dev->wanted_features =
309 		(dev->wanted_features & ~changed) | (features & changed);
310 
311 	__netdev_update_features(dev);
312 
313 	return 0;
314 }
315 
__ethtool_get_settings(struct net_device * dev,struct ethtool_cmd * cmd)316 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
317 {
318 	ASSERT_RTNL();
319 
320 	if (!dev->ethtool_ops->get_settings)
321 		return -EOPNOTSUPP;
322 
323 	memset(cmd, 0, sizeof(struct ethtool_cmd));
324 	cmd->cmd = ETHTOOL_GSET;
325 	return dev->ethtool_ops->get_settings(dev, cmd);
326 }
327 EXPORT_SYMBOL(__ethtool_get_settings);
328 
ethtool_get_settings(struct net_device * dev,void __user * useraddr)329 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
330 {
331 	int err;
332 	struct ethtool_cmd cmd;
333 
334 	err = __ethtool_get_settings(dev, &cmd);
335 	if (err < 0)
336 		return err;
337 
338 	if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
339 		return -EFAULT;
340 	return 0;
341 }
342 
ethtool_set_settings(struct net_device * dev,void __user * useraddr)343 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
344 {
345 	struct ethtool_cmd cmd;
346 
347 	if (!dev->ethtool_ops->set_settings)
348 		return -EOPNOTSUPP;
349 
350 	if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
351 		return -EFAULT;
352 
353 	return dev->ethtool_ops->set_settings(dev, &cmd);
354 }
355 
ethtool_get_drvinfo(struct net_device * dev,void __user * useraddr)356 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
357 						  void __user *useraddr)
358 {
359 	struct ethtool_drvinfo info;
360 	const struct ethtool_ops *ops = dev->ethtool_ops;
361 
362 	memset(&info, 0, sizeof(info));
363 	info.cmd = ETHTOOL_GDRVINFO;
364 	if (ops->get_drvinfo) {
365 		ops->get_drvinfo(dev, &info);
366 	} else if (dev->dev.parent && dev->dev.parent->driver) {
367 		strlcpy(info.bus_info, dev_name(dev->dev.parent),
368 			sizeof(info.bus_info));
369 		strlcpy(info.driver, dev->dev.parent->driver->name,
370 			sizeof(info.driver));
371 	} else {
372 		return -EOPNOTSUPP;
373 	}
374 
375 	/*
376 	 * this method of obtaining string set info is deprecated;
377 	 * Use ETHTOOL_GSSET_INFO instead.
378 	 */
379 	if (ops->get_sset_count) {
380 		int rc;
381 
382 		rc = ops->get_sset_count(dev, ETH_SS_TEST);
383 		if (rc >= 0)
384 			info.testinfo_len = rc;
385 		rc = ops->get_sset_count(dev, ETH_SS_STATS);
386 		if (rc >= 0)
387 			info.n_stats = rc;
388 		rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
389 		if (rc >= 0)
390 			info.n_priv_flags = rc;
391 	}
392 	if (ops->get_regs_len)
393 		info.regdump_len = ops->get_regs_len(dev);
394 	if (ops->get_eeprom_len)
395 		info.eedump_len = ops->get_eeprom_len(dev);
396 
397 	if (copy_to_user(useraddr, &info, sizeof(info)))
398 		return -EFAULT;
399 	return 0;
400 }
401 
ethtool_get_sset_info(struct net_device * dev,void __user * useraddr)402 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
403 						    void __user *useraddr)
404 {
405 	struct ethtool_sset_info info;
406 	u64 sset_mask;
407 	int i, idx = 0, n_bits = 0, ret, rc;
408 	u32 *info_buf = NULL;
409 
410 	if (copy_from_user(&info, useraddr, sizeof(info)))
411 		return -EFAULT;
412 
413 	/* store copy of mask, because we zero struct later on */
414 	sset_mask = info.sset_mask;
415 	if (!sset_mask)
416 		return 0;
417 
418 	/* calculate size of return buffer */
419 	n_bits = hweight64(sset_mask);
420 
421 	memset(&info, 0, sizeof(info));
422 	info.cmd = ETHTOOL_GSSET_INFO;
423 
424 	info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
425 	if (!info_buf)
426 		return -ENOMEM;
427 
428 	/*
429 	 * fill return buffer based on input bitmask and successful
430 	 * get_sset_count return
431 	 */
432 	for (i = 0; i < 64; i++) {
433 		if (!(sset_mask & (1ULL << i)))
434 			continue;
435 
436 		rc = __ethtool_get_sset_count(dev, i);
437 		if (rc >= 0) {
438 			info.sset_mask |= (1ULL << i);
439 			info_buf[idx++] = rc;
440 		}
441 	}
442 
443 	ret = -EFAULT;
444 	if (copy_to_user(useraddr, &info, sizeof(info)))
445 		goto out;
446 
447 	useraddr += offsetof(struct ethtool_sset_info, data);
448 	if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
449 		goto out;
450 
451 	ret = 0;
452 
453 out:
454 	kfree(info_buf);
455 	return ret;
456 }
457 
ethtool_set_rxnfc(struct net_device * dev,u32 cmd,void __user * useraddr)458 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
459 						u32 cmd, void __user *useraddr)
460 {
461 	struct ethtool_rxnfc info;
462 	size_t info_size = sizeof(info);
463 	int rc;
464 
465 	if (!dev->ethtool_ops->set_rxnfc)
466 		return -EOPNOTSUPP;
467 
468 	/* struct ethtool_rxnfc was originally defined for
469 	 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
470 	 * members.  User-space might still be using that
471 	 * definition. */
472 	if (cmd == ETHTOOL_SRXFH)
473 		info_size = (offsetof(struct ethtool_rxnfc, data) +
474 			     sizeof(info.data));
475 
476 	if (copy_from_user(&info, useraddr, info_size))
477 		return -EFAULT;
478 
479 	rc = dev->ethtool_ops->set_rxnfc(dev, &info);
480 	if (rc)
481 		return rc;
482 
483 	if (cmd == ETHTOOL_SRXCLSRLINS &&
484 	    copy_to_user(useraddr, &info, info_size))
485 		return -EFAULT;
486 
487 	return 0;
488 }
489 
ethtool_get_rxnfc(struct net_device * dev,u32 cmd,void __user * useraddr)490 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
491 						u32 cmd, void __user *useraddr)
492 {
493 	struct ethtool_rxnfc info;
494 	size_t info_size = sizeof(info);
495 	const struct ethtool_ops *ops = dev->ethtool_ops;
496 	int ret;
497 	void *rule_buf = NULL;
498 
499 	if (!ops->get_rxnfc)
500 		return -EOPNOTSUPP;
501 
502 	/* struct ethtool_rxnfc was originally defined for
503 	 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
504 	 * members.  User-space might still be using that
505 	 * definition. */
506 	if (cmd == ETHTOOL_GRXFH)
507 		info_size = (offsetof(struct ethtool_rxnfc, data) +
508 			     sizeof(info.data));
509 
510 	if (copy_from_user(&info, useraddr, info_size))
511 		return -EFAULT;
512 
513 	if (info.cmd == ETHTOOL_GRXCLSRLALL) {
514 		if (info.rule_cnt > 0) {
515 			if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
516 				rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
517 						   GFP_USER);
518 			if (!rule_buf)
519 				return -ENOMEM;
520 		}
521 	}
522 
523 	ret = ops->get_rxnfc(dev, &info, rule_buf);
524 	if (ret < 0)
525 		goto err_out;
526 
527 	ret = -EFAULT;
528 	if (copy_to_user(useraddr, &info, info_size))
529 		goto err_out;
530 
531 	if (rule_buf) {
532 		useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
533 		if (copy_to_user(useraddr, rule_buf,
534 				 info.rule_cnt * sizeof(u32)))
535 			goto err_out;
536 	}
537 	ret = 0;
538 
539 err_out:
540 	kfree(rule_buf);
541 
542 	return ret;
543 }
544 
ethtool_get_rxfh_indir(struct net_device * dev,void __user * useraddr)545 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
546 						     void __user *useraddr)
547 {
548 	u32 user_size, dev_size;
549 	u32 *indir;
550 	int ret;
551 
552 	if (!dev->ethtool_ops->get_rxfh_indir_size ||
553 	    !dev->ethtool_ops->get_rxfh_indir)
554 		return -EOPNOTSUPP;
555 	dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
556 	if (dev_size == 0)
557 		return -EOPNOTSUPP;
558 
559 	if (copy_from_user(&user_size,
560 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
561 			   sizeof(user_size)))
562 		return -EFAULT;
563 
564 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
565 			 &dev_size, sizeof(dev_size)))
566 		return -EFAULT;
567 
568 	/* If the user buffer size is 0, this is just a query for the
569 	 * device table size.  Otherwise, if it's smaller than the
570 	 * device table size it's an error.
571 	 */
572 	if (user_size < dev_size)
573 		return user_size == 0 ? 0 : -EINVAL;
574 
575 	indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
576 	if (!indir)
577 		return -ENOMEM;
578 
579 	ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
580 	if (ret)
581 		goto out;
582 
583 	if (copy_to_user(useraddr +
584 			 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
585 			 indir, dev_size * sizeof(indir[0])))
586 		ret = -EFAULT;
587 
588 out:
589 	kfree(indir);
590 	return ret;
591 }
592 
ethtool_set_rxfh_indir(struct net_device * dev,void __user * useraddr)593 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
594 						     void __user *useraddr)
595 {
596 	struct ethtool_rxnfc rx_rings;
597 	u32 user_size, dev_size, i;
598 	u32 *indir;
599 	const struct ethtool_ops *ops = dev->ethtool_ops;
600 	int ret;
601 
602 	if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
603 	    !ops->get_rxnfc)
604 		return -EOPNOTSUPP;
605 
606 	dev_size = ops->get_rxfh_indir_size(dev);
607 	if (dev_size == 0)
608 		return -EOPNOTSUPP;
609 
610 	if (copy_from_user(&user_size,
611 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
612 			   sizeof(user_size)))
613 		return -EFAULT;
614 
615 	if (user_size != 0 && user_size != dev_size)
616 		return -EINVAL;
617 
618 	indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
619 	if (!indir)
620 		return -ENOMEM;
621 
622 	rx_rings.cmd = ETHTOOL_GRXRINGS;
623 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
624 	if (ret)
625 		goto out;
626 
627 	if (user_size == 0) {
628 		for (i = 0; i < dev_size; i++)
629 			indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
630 	} else {
631 		if (copy_from_user(indir,
632 				  useraddr +
633 				  offsetof(struct ethtool_rxfh_indir,
634 					   ring_index[0]),
635 				  dev_size * sizeof(indir[0]))) {
636 			ret = -EFAULT;
637 			goto out;
638 		}
639 
640 		/* Validate ring indices */
641 		for (i = 0; i < dev_size; i++) {
642 			if (indir[i] >= rx_rings.data) {
643 				ret = -EINVAL;
644 				goto out;
645 			}
646 		}
647 	}
648 
649 	ret = ops->set_rxfh_indir(dev, indir);
650 
651 out:
652 	kfree(indir);
653 	return ret;
654 }
655 
ethtool_get_regs(struct net_device * dev,char __user * useraddr)656 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
657 {
658 	struct ethtool_regs regs;
659 	const struct ethtool_ops *ops = dev->ethtool_ops;
660 	void *regbuf;
661 	int reglen, ret;
662 
663 	if (!ops->get_regs || !ops->get_regs_len)
664 		return -EOPNOTSUPP;
665 
666 	if (copy_from_user(&regs, useraddr, sizeof(regs)))
667 		return -EFAULT;
668 
669 	reglen = ops->get_regs_len(dev);
670 	if (regs.len > reglen)
671 		regs.len = reglen;
672 
673 	regbuf = vzalloc(reglen);
674 	if (reglen && !regbuf)
675 		return -ENOMEM;
676 
677 	ops->get_regs(dev, &regs, regbuf);
678 
679 	ret = -EFAULT;
680 	if (copy_to_user(useraddr, &regs, sizeof(regs)))
681 		goto out;
682 	useraddr += offsetof(struct ethtool_regs, data);
683 	if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
684 		goto out;
685 	ret = 0;
686 
687  out:
688 	vfree(regbuf);
689 	return ret;
690 }
691 
ethtool_reset(struct net_device * dev,char __user * useraddr)692 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
693 {
694 	struct ethtool_value reset;
695 	int ret;
696 
697 	if (!dev->ethtool_ops->reset)
698 		return -EOPNOTSUPP;
699 
700 	if (copy_from_user(&reset, useraddr, sizeof(reset)))
701 		return -EFAULT;
702 
703 	ret = dev->ethtool_ops->reset(dev, &reset.data);
704 	if (ret)
705 		return ret;
706 
707 	if (copy_to_user(useraddr, &reset, sizeof(reset)))
708 		return -EFAULT;
709 	return 0;
710 }
711 
ethtool_get_wol(struct net_device * dev,char __user * useraddr)712 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
713 {
714 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
715 
716 	if (!dev->ethtool_ops->get_wol)
717 		return -EOPNOTSUPP;
718 
719 	dev->ethtool_ops->get_wol(dev, &wol);
720 
721 	if (copy_to_user(useraddr, &wol, sizeof(wol)))
722 		return -EFAULT;
723 	return 0;
724 }
725 
ethtool_set_wol(struct net_device * dev,char __user * useraddr)726 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
727 {
728 	struct ethtool_wolinfo wol;
729 
730 	if (!dev->ethtool_ops->set_wol)
731 		return -EOPNOTSUPP;
732 
733 	if (copy_from_user(&wol, useraddr, sizeof(wol)))
734 		return -EFAULT;
735 
736 	return dev->ethtool_ops->set_wol(dev, &wol);
737 }
738 
ethtool_get_eee(struct net_device * dev,char __user * useraddr)739 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
740 {
741 	struct ethtool_eee edata;
742 	int rc;
743 
744 	if (!dev->ethtool_ops->get_eee)
745 		return -EOPNOTSUPP;
746 
747 	memset(&edata, 0, sizeof(struct ethtool_eee));
748 	edata.cmd = ETHTOOL_GEEE;
749 	rc = dev->ethtool_ops->get_eee(dev, &edata);
750 
751 	if (rc)
752 		return rc;
753 
754 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
755 		return -EFAULT;
756 
757 	return 0;
758 }
759 
ethtool_set_eee(struct net_device * dev,char __user * useraddr)760 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
761 {
762 	struct ethtool_eee edata;
763 
764 	if (!dev->ethtool_ops->set_eee)
765 		return -EOPNOTSUPP;
766 
767 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
768 		return -EFAULT;
769 
770 	return dev->ethtool_ops->set_eee(dev, &edata);
771 }
772 
ethtool_nway_reset(struct net_device * dev)773 static int ethtool_nway_reset(struct net_device *dev)
774 {
775 	if (!dev->ethtool_ops->nway_reset)
776 		return -EOPNOTSUPP;
777 
778 	return dev->ethtool_ops->nway_reset(dev);
779 }
780 
ethtool_get_link(struct net_device * dev,char __user * useraddr)781 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
782 {
783 	struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
784 
785 	if (!dev->ethtool_ops->get_link)
786 		return -EOPNOTSUPP;
787 
788 	edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
789 
790 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
791 		return -EFAULT;
792 	return 0;
793 }
794 
ethtool_get_any_eeprom(struct net_device * dev,void __user * useraddr,int (* getter)(struct net_device *,struct ethtool_eeprom *,u8 *),u32 total_len)795 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
796 				  int (*getter)(struct net_device *,
797 						struct ethtool_eeprom *, u8 *),
798 				  u32 total_len)
799 {
800 	struct ethtool_eeprom eeprom;
801 	void __user *userbuf = useraddr + sizeof(eeprom);
802 	u32 bytes_remaining;
803 	u8 *data;
804 	int ret = 0;
805 
806 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
807 		return -EFAULT;
808 
809 	/* Check for wrap and zero */
810 	if (eeprom.offset + eeprom.len <= eeprom.offset)
811 		return -EINVAL;
812 
813 	/* Check for exceeding total eeprom len */
814 	if (eeprom.offset + eeprom.len > total_len)
815 		return -EINVAL;
816 
817 	data = kmalloc(PAGE_SIZE, GFP_USER);
818 	if (!data)
819 		return -ENOMEM;
820 
821 	bytes_remaining = eeprom.len;
822 	while (bytes_remaining > 0) {
823 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
824 
825 		ret = getter(dev, &eeprom, data);
826 		if (ret)
827 			break;
828 		if (copy_to_user(userbuf, data, eeprom.len)) {
829 			ret = -EFAULT;
830 			break;
831 		}
832 		userbuf += eeprom.len;
833 		eeprom.offset += eeprom.len;
834 		bytes_remaining -= eeprom.len;
835 	}
836 
837 	eeprom.len = userbuf - (useraddr + sizeof(eeprom));
838 	eeprom.offset -= eeprom.len;
839 	if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
840 		ret = -EFAULT;
841 
842 	kfree(data);
843 	return ret;
844 }
845 
ethtool_get_eeprom(struct net_device * dev,void __user * useraddr)846 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
847 {
848 	const struct ethtool_ops *ops = dev->ethtool_ops;
849 
850 	if (!ops->get_eeprom || !ops->get_eeprom_len)
851 		return -EOPNOTSUPP;
852 
853 	return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
854 				      ops->get_eeprom_len(dev));
855 }
856 
ethtool_set_eeprom(struct net_device * dev,void __user * useraddr)857 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
858 {
859 	struct ethtool_eeprom eeprom;
860 	const struct ethtool_ops *ops = dev->ethtool_ops;
861 	void __user *userbuf = useraddr + sizeof(eeprom);
862 	u32 bytes_remaining;
863 	u8 *data;
864 	int ret = 0;
865 
866 	if (!ops->set_eeprom || !ops->get_eeprom_len)
867 		return -EOPNOTSUPP;
868 
869 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
870 		return -EFAULT;
871 
872 	/* Check for wrap and zero */
873 	if (eeprom.offset + eeprom.len <= eeprom.offset)
874 		return -EINVAL;
875 
876 	/* Check for exceeding total eeprom len */
877 	if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
878 		return -EINVAL;
879 
880 	data = kmalloc(PAGE_SIZE, GFP_USER);
881 	if (!data)
882 		return -ENOMEM;
883 
884 	bytes_remaining = eeprom.len;
885 	while (bytes_remaining > 0) {
886 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
887 
888 		if (copy_from_user(data, userbuf, eeprom.len)) {
889 			ret = -EFAULT;
890 			break;
891 		}
892 		ret = ops->set_eeprom(dev, &eeprom, data);
893 		if (ret)
894 			break;
895 		userbuf += eeprom.len;
896 		eeprom.offset += eeprom.len;
897 		bytes_remaining -= eeprom.len;
898 	}
899 
900 	kfree(data);
901 	return ret;
902 }
903 
ethtool_get_coalesce(struct net_device * dev,void __user * useraddr)904 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
905 						   void __user *useraddr)
906 {
907 	struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
908 
909 	if (!dev->ethtool_ops->get_coalesce)
910 		return -EOPNOTSUPP;
911 
912 	dev->ethtool_ops->get_coalesce(dev, &coalesce);
913 
914 	if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
915 		return -EFAULT;
916 	return 0;
917 }
918 
ethtool_set_coalesce(struct net_device * dev,void __user * useraddr)919 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
920 						   void __user *useraddr)
921 {
922 	struct ethtool_coalesce coalesce;
923 
924 	if (!dev->ethtool_ops->set_coalesce)
925 		return -EOPNOTSUPP;
926 
927 	if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
928 		return -EFAULT;
929 
930 	return dev->ethtool_ops->set_coalesce(dev, &coalesce);
931 }
932 
ethtool_get_ringparam(struct net_device * dev,void __user * useraddr)933 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
934 {
935 	struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
936 
937 	if (!dev->ethtool_ops->get_ringparam)
938 		return -EOPNOTSUPP;
939 
940 	dev->ethtool_ops->get_ringparam(dev, &ringparam);
941 
942 	if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
943 		return -EFAULT;
944 	return 0;
945 }
946 
ethtool_set_ringparam(struct net_device * dev,void __user * useraddr)947 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
948 {
949 	struct ethtool_ringparam ringparam;
950 
951 	if (!dev->ethtool_ops->set_ringparam)
952 		return -EOPNOTSUPP;
953 
954 	if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
955 		return -EFAULT;
956 
957 	return dev->ethtool_ops->set_ringparam(dev, &ringparam);
958 }
959 
ethtool_get_channels(struct net_device * dev,void __user * useraddr)960 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
961 						   void __user *useraddr)
962 {
963 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
964 
965 	if (!dev->ethtool_ops->get_channels)
966 		return -EOPNOTSUPP;
967 
968 	dev->ethtool_ops->get_channels(dev, &channels);
969 
970 	if (copy_to_user(useraddr, &channels, sizeof(channels)))
971 		return -EFAULT;
972 	return 0;
973 }
974 
ethtool_set_channels(struct net_device * dev,void __user * useraddr)975 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
976 						   void __user *useraddr)
977 {
978 	struct ethtool_channels channels;
979 
980 	if (!dev->ethtool_ops->set_channels)
981 		return -EOPNOTSUPP;
982 
983 	if (copy_from_user(&channels, useraddr, sizeof(channels)))
984 		return -EFAULT;
985 
986 	return dev->ethtool_ops->set_channels(dev, &channels);
987 }
988 
ethtool_get_pauseparam(struct net_device * dev,void __user * useraddr)989 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
990 {
991 	struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
992 
993 	if (!dev->ethtool_ops->get_pauseparam)
994 		return -EOPNOTSUPP;
995 
996 	dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
997 
998 	if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
999 		return -EFAULT;
1000 	return 0;
1001 }
1002 
ethtool_set_pauseparam(struct net_device * dev,void __user * useraddr)1003 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1004 {
1005 	struct ethtool_pauseparam pauseparam;
1006 
1007 	if (!dev->ethtool_ops->set_pauseparam)
1008 		return -EOPNOTSUPP;
1009 
1010 	if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1011 		return -EFAULT;
1012 
1013 	return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1014 }
1015 
ethtool_self_test(struct net_device * dev,char __user * useraddr)1016 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1017 {
1018 	struct ethtool_test test;
1019 	const struct ethtool_ops *ops = dev->ethtool_ops;
1020 	u64 *data;
1021 	int ret, test_len;
1022 
1023 	if (!ops->self_test || !ops->get_sset_count)
1024 		return -EOPNOTSUPP;
1025 
1026 	test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1027 	if (test_len < 0)
1028 		return test_len;
1029 	WARN_ON(test_len == 0);
1030 
1031 	if (copy_from_user(&test, useraddr, sizeof(test)))
1032 		return -EFAULT;
1033 
1034 	test.len = test_len;
1035 	data = kmalloc(test_len * sizeof(u64), GFP_USER);
1036 	if (!data)
1037 		return -ENOMEM;
1038 
1039 	ops->self_test(dev, &test, data);
1040 
1041 	ret = -EFAULT;
1042 	if (copy_to_user(useraddr, &test, sizeof(test)))
1043 		goto out;
1044 	useraddr += sizeof(test);
1045 	if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1046 		goto out;
1047 	ret = 0;
1048 
1049  out:
1050 	kfree(data);
1051 	return ret;
1052 }
1053 
ethtool_get_strings(struct net_device * dev,void __user * useraddr)1054 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1055 {
1056 	struct ethtool_gstrings gstrings;
1057 	u8 *data;
1058 	int ret;
1059 
1060 	if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1061 		return -EFAULT;
1062 
1063 	ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1064 	if (ret < 0)
1065 		return ret;
1066 
1067 	gstrings.len = ret;
1068 
1069 	data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1070 	if (!data)
1071 		return -ENOMEM;
1072 
1073 	__ethtool_get_strings(dev, gstrings.string_set, data);
1074 
1075 	ret = -EFAULT;
1076 	if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1077 		goto out;
1078 	useraddr += sizeof(gstrings);
1079 	if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1080 		goto out;
1081 	ret = 0;
1082 
1083 out:
1084 	kfree(data);
1085 	return ret;
1086 }
1087 
ethtool_phys_id(struct net_device * dev,void __user * useraddr)1088 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1089 {
1090 	struct ethtool_value id;
1091 	static bool busy;
1092 	const struct ethtool_ops *ops = dev->ethtool_ops;
1093 	int rc;
1094 
1095 	if (!ops->set_phys_id)
1096 		return -EOPNOTSUPP;
1097 
1098 	if (busy)
1099 		return -EBUSY;
1100 
1101 	if (copy_from_user(&id, useraddr, sizeof(id)))
1102 		return -EFAULT;
1103 
1104 	rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1105 	if (rc < 0)
1106 		return rc;
1107 
1108 	/* Drop the RTNL lock while waiting, but prevent reentry or
1109 	 * removal of the device.
1110 	 */
1111 	busy = true;
1112 	dev_hold(dev);
1113 	rtnl_unlock();
1114 
1115 	if (rc == 0) {
1116 		/* Driver will handle this itself */
1117 		schedule_timeout_interruptible(
1118 			id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1119 	} else {
1120 		/* Driver expects to be called at twice the frequency in rc */
1121 		int n = rc * 2, i, interval = HZ / n;
1122 
1123 		/* Count down seconds */
1124 		do {
1125 			/* Count down iterations per second */
1126 			i = n;
1127 			do {
1128 				rtnl_lock();
1129 				rc = ops->set_phys_id(dev,
1130 				    (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1131 				rtnl_unlock();
1132 				if (rc)
1133 					break;
1134 				schedule_timeout_interruptible(interval);
1135 			} while (!signal_pending(current) && --i != 0);
1136 		} while (!signal_pending(current) &&
1137 			 (id.data == 0 || --id.data != 0));
1138 	}
1139 
1140 	rtnl_lock();
1141 	dev_put(dev);
1142 	busy = false;
1143 
1144 	(void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1145 	return rc;
1146 }
1147 
ethtool_get_stats(struct net_device * dev,void __user * useraddr)1148 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1149 {
1150 	struct ethtool_stats stats;
1151 	const struct ethtool_ops *ops = dev->ethtool_ops;
1152 	u64 *data;
1153 	int ret, n_stats;
1154 
1155 	if (!ops->get_ethtool_stats || !ops->get_sset_count)
1156 		return -EOPNOTSUPP;
1157 
1158 	n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1159 	if (n_stats < 0)
1160 		return n_stats;
1161 	WARN_ON(n_stats == 0);
1162 
1163 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
1164 		return -EFAULT;
1165 
1166 	stats.n_stats = n_stats;
1167 	data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1168 	if (!data)
1169 		return -ENOMEM;
1170 
1171 	ops->get_ethtool_stats(dev, &stats, data);
1172 
1173 	ret = -EFAULT;
1174 	if (copy_to_user(useraddr, &stats, sizeof(stats)))
1175 		goto out;
1176 	useraddr += sizeof(stats);
1177 	if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1178 		goto out;
1179 	ret = 0;
1180 
1181  out:
1182 	kfree(data);
1183 	return ret;
1184 }
1185 
ethtool_get_perm_addr(struct net_device * dev,void __user * useraddr)1186 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1187 {
1188 	struct ethtool_perm_addr epaddr;
1189 
1190 	if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1191 		return -EFAULT;
1192 
1193 	if (epaddr.size < dev->addr_len)
1194 		return -ETOOSMALL;
1195 	epaddr.size = dev->addr_len;
1196 
1197 	if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1198 		return -EFAULT;
1199 	useraddr += sizeof(epaddr);
1200 	if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1201 		return -EFAULT;
1202 	return 0;
1203 }
1204 
ethtool_get_value(struct net_device * dev,char __user * useraddr,u32 cmd,u32 (* actor)(struct net_device *))1205 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1206 			     u32 cmd, u32 (*actor)(struct net_device *))
1207 {
1208 	struct ethtool_value edata = { .cmd = cmd };
1209 
1210 	if (!actor)
1211 		return -EOPNOTSUPP;
1212 
1213 	edata.data = actor(dev);
1214 
1215 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
1216 		return -EFAULT;
1217 	return 0;
1218 }
1219 
ethtool_set_value_void(struct net_device * dev,char __user * useraddr,void (* actor)(struct net_device *,u32))1220 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1221 			     void (*actor)(struct net_device *, u32))
1222 {
1223 	struct ethtool_value edata;
1224 
1225 	if (!actor)
1226 		return -EOPNOTSUPP;
1227 
1228 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
1229 		return -EFAULT;
1230 
1231 	actor(dev, edata.data);
1232 	return 0;
1233 }
1234 
ethtool_set_value(struct net_device * dev,char __user * useraddr,int (* actor)(struct net_device *,u32))1235 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1236 			     int (*actor)(struct net_device *, u32))
1237 {
1238 	struct ethtool_value edata;
1239 
1240 	if (!actor)
1241 		return -EOPNOTSUPP;
1242 
1243 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
1244 		return -EFAULT;
1245 
1246 	return actor(dev, edata.data);
1247 }
1248 
ethtool_flash_device(struct net_device * dev,char __user * useraddr)1249 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1250 						   char __user *useraddr)
1251 {
1252 	struct ethtool_flash efl;
1253 
1254 	if (copy_from_user(&efl, useraddr, sizeof(efl)))
1255 		return -EFAULT;
1256 
1257 	if (!dev->ethtool_ops->flash_device)
1258 		return -EOPNOTSUPP;
1259 
1260 	efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1261 
1262 	return dev->ethtool_ops->flash_device(dev, &efl);
1263 }
1264 
ethtool_set_dump(struct net_device * dev,void __user * useraddr)1265 static int ethtool_set_dump(struct net_device *dev,
1266 			void __user *useraddr)
1267 {
1268 	struct ethtool_dump dump;
1269 
1270 	if (!dev->ethtool_ops->set_dump)
1271 		return -EOPNOTSUPP;
1272 
1273 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
1274 		return -EFAULT;
1275 
1276 	return dev->ethtool_ops->set_dump(dev, &dump);
1277 }
1278 
ethtool_get_dump_flag(struct net_device * dev,void __user * useraddr)1279 static int ethtool_get_dump_flag(struct net_device *dev,
1280 				void __user *useraddr)
1281 {
1282 	int ret;
1283 	struct ethtool_dump dump;
1284 	const struct ethtool_ops *ops = dev->ethtool_ops;
1285 
1286 	if (!ops->get_dump_flag)
1287 		return -EOPNOTSUPP;
1288 
1289 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
1290 		return -EFAULT;
1291 
1292 	ret = ops->get_dump_flag(dev, &dump);
1293 	if (ret)
1294 		return ret;
1295 
1296 	if (copy_to_user(useraddr, &dump, sizeof(dump)))
1297 		return -EFAULT;
1298 	return 0;
1299 }
1300 
ethtool_get_dump_data(struct net_device * dev,void __user * useraddr)1301 static int ethtool_get_dump_data(struct net_device *dev,
1302 				void __user *useraddr)
1303 {
1304 	int ret;
1305 	__u32 len;
1306 	struct ethtool_dump dump, tmp;
1307 	const struct ethtool_ops *ops = dev->ethtool_ops;
1308 	void *data = NULL;
1309 
1310 	if (!ops->get_dump_data || !ops->get_dump_flag)
1311 		return -EOPNOTSUPP;
1312 
1313 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
1314 		return -EFAULT;
1315 
1316 	memset(&tmp, 0, sizeof(tmp));
1317 	tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1318 	ret = ops->get_dump_flag(dev, &tmp);
1319 	if (ret)
1320 		return ret;
1321 
1322 	len = (tmp.len > dump.len) ? dump.len : tmp.len;
1323 	if (!len)
1324 		return -EFAULT;
1325 
1326 	data = vzalloc(tmp.len);
1327 	if (!data)
1328 		return -ENOMEM;
1329 	ret = ops->get_dump_data(dev, &dump, data);
1330 	if (ret)
1331 		goto out;
1332 
1333 	if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1334 		ret = -EFAULT;
1335 		goto out;
1336 	}
1337 	useraddr += offsetof(struct ethtool_dump, data);
1338 	if (copy_to_user(useraddr, data, len))
1339 		ret = -EFAULT;
1340 out:
1341 	vfree(data);
1342 	return ret;
1343 }
1344 
ethtool_get_ts_info(struct net_device * dev,void __user * useraddr)1345 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1346 {
1347 	int err = 0;
1348 	struct ethtool_ts_info info;
1349 	const struct ethtool_ops *ops = dev->ethtool_ops;
1350 	struct phy_device *phydev = dev->phydev;
1351 
1352 	memset(&info, 0, sizeof(info));
1353 	info.cmd = ETHTOOL_GET_TS_INFO;
1354 
1355 	if (phydev && phydev->drv && phydev->drv->ts_info) {
1356 		err = phydev->drv->ts_info(phydev, &info);
1357 	} else if (ops->get_ts_info) {
1358 		err = ops->get_ts_info(dev, &info);
1359 	} else {
1360 		info.so_timestamping =
1361 			SOF_TIMESTAMPING_RX_SOFTWARE |
1362 			SOF_TIMESTAMPING_SOFTWARE;
1363 		info.phc_index = -1;
1364 	}
1365 
1366 	if (err)
1367 		return err;
1368 
1369 	if (copy_to_user(useraddr, &info, sizeof(info)))
1370 		err = -EFAULT;
1371 
1372 	return err;
1373 }
1374 
ethtool_get_module_info(struct net_device * dev,void __user * useraddr)1375 static int ethtool_get_module_info(struct net_device *dev,
1376 				   void __user *useraddr)
1377 {
1378 	int ret;
1379 	struct ethtool_modinfo modinfo;
1380 	const struct ethtool_ops *ops = dev->ethtool_ops;
1381 
1382 	if (!ops->get_module_info)
1383 		return -EOPNOTSUPP;
1384 
1385 	if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1386 		return -EFAULT;
1387 
1388 	ret = ops->get_module_info(dev, &modinfo);
1389 	if (ret)
1390 		return ret;
1391 
1392 	if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1393 		return -EFAULT;
1394 
1395 	return 0;
1396 }
1397 
ethtool_get_module_eeprom(struct net_device * dev,void __user * useraddr)1398 static int ethtool_get_module_eeprom(struct net_device *dev,
1399 				     void __user *useraddr)
1400 {
1401 	int ret;
1402 	struct ethtool_modinfo modinfo;
1403 	const struct ethtool_ops *ops = dev->ethtool_ops;
1404 
1405 	if (!ops->get_module_info || !ops->get_module_eeprom)
1406 		return -EOPNOTSUPP;
1407 
1408 	ret = ops->get_module_info(dev, &modinfo);
1409 	if (ret)
1410 		return ret;
1411 
1412 	return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1413 				      modinfo.eeprom_len);
1414 }
1415 
1416 /* The main entry point in this file.  Called from net/core/dev.c */
1417 
dev_ethtool(struct net * net,struct ifreq * ifr)1418 int dev_ethtool(struct net *net, struct ifreq *ifr)
1419 {
1420 	struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1421 	void __user *useraddr = ifr->ifr_data;
1422 	u32 ethcmd;
1423 	int rc;
1424 	netdev_features_t old_features;
1425 
1426 	if (!dev || !netif_device_present(dev))
1427 		return -ENODEV;
1428 
1429 	if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1430 		return -EFAULT;
1431 
1432 	/* Allow some commands to be done by anyone */
1433 	switch (ethcmd) {
1434 	case ETHTOOL_GSET:
1435 	case ETHTOOL_GDRVINFO:
1436 	case ETHTOOL_GMSGLVL:
1437 	case ETHTOOL_GLINK:
1438 	case ETHTOOL_GCOALESCE:
1439 	case ETHTOOL_GRINGPARAM:
1440 	case ETHTOOL_GPAUSEPARAM:
1441 	case ETHTOOL_GRXCSUM:
1442 	case ETHTOOL_GTXCSUM:
1443 	case ETHTOOL_GSG:
1444 	case ETHTOOL_GSSET_INFO:
1445 	case ETHTOOL_GSTRINGS:
1446 	case ETHTOOL_GSTATS:
1447 	case ETHTOOL_GTSO:
1448 	case ETHTOOL_GPERMADDR:
1449 	case ETHTOOL_GUFO:
1450 	case ETHTOOL_GGSO:
1451 	case ETHTOOL_GGRO:
1452 	case ETHTOOL_GFLAGS:
1453 	case ETHTOOL_GPFLAGS:
1454 	case ETHTOOL_GRXFH:
1455 	case ETHTOOL_GRXRINGS:
1456 	case ETHTOOL_GRXCLSRLCNT:
1457 	case ETHTOOL_GRXCLSRULE:
1458 	case ETHTOOL_GRXCLSRLALL:
1459 	case ETHTOOL_GRXFHINDIR:
1460 	case ETHTOOL_GFEATURES:
1461 	case ETHTOOL_GCHANNELS:
1462 	case ETHTOOL_GET_TS_INFO:
1463 	case ETHTOOL_GEEE:
1464 		break;
1465 	default:
1466 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1467 			return -EPERM;
1468 	}
1469 
1470 	if (dev->ethtool_ops->begin) {
1471 		rc = dev->ethtool_ops->begin(dev);
1472 		if (rc  < 0)
1473 			return rc;
1474 	}
1475 	old_features = dev->features;
1476 
1477 	switch (ethcmd) {
1478 	case ETHTOOL_GSET:
1479 		rc = ethtool_get_settings(dev, useraddr);
1480 		break;
1481 	case ETHTOOL_SSET:
1482 		rc = ethtool_set_settings(dev, useraddr);
1483 		break;
1484 	case ETHTOOL_GDRVINFO:
1485 		rc = ethtool_get_drvinfo(dev, useraddr);
1486 		break;
1487 	case ETHTOOL_GREGS:
1488 		rc = ethtool_get_regs(dev, useraddr);
1489 		break;
1490 	case ETHTOOL_GWOL:
1491 		rc = ethtool_get_wol(dev, useraddr);
1492 		break;
1493 	case ETHTOOL_SWOL:
1494 		rc = ethtool_set_wol(dev, useraddr);
1495 		break;
1496 	case ETHTOOL_GMSGLVL:
1497 		rc = ethtool_get_value(dev, useraddr, ethcmd,
1498 				       dev->ethtool_ops->get_msglevel);
1499 		break;
1500 	case ETHTOOL_SMSGLVL:
1501 		rc = ethtool_set_value_void(dev, useraddr,
1502 				       dev->ethtool_ops->set_msglevel);
1503 		break;
1504 	case ETHTOOL_GEEE:
1505 		rc = ethtool_get_eee(dev, useraddr);
1506 		break;
1507 	case ETHTOOL_SEEE:
1508 		rc = ethtool_set_eee(dev, useraddr);
1509 		break;
1510 	case ETHTOOL_NWAY_RST:
1511 		rc = ethtool_nway_reset(dev);
1512 		break;
1513 	case ETHTOOL_GLINK:
1514 		rc = ethtool_get_link(dev, useraddr);
1515 		break;
1516 	case ETHTOOL_GEEPROM:
1517 		rc = ethtool_get_eeprom(dev, useraddr);
1518 		break;
1519 	case ETHTOOL_SEEPROM:
1520 		rc = ethtool_set_eeprom(dev, useraddr);
1521 		break;
1522 	case ETHTOOL_GCOALESCE:
1523 		rc = ethtool_get_coalesce(dev, useraddr);
1524 		break;
1525 	case ETHTOOL_SCOALESCE:
1526 		rc = ethtool_set_coalesce(dev, useraddr);
1527 		break;
1528 	case ETHTOOL_GRINGPARAM:
1529 		rc = ethtool_get_ringparam(dev, useraddr);
1530 		break;
1531 	case ETHTOOL_SRINGPARAM:
1532 		rc = ethtool_set_ringparam(dev, useraddr);
1533 		break;
1534 	case ETHTOOL_GPAUSEPARAM:
1535 		rc = ethtool_get_pauseparam(dev, useraddr);
1536 		break;
1537 	case ETHTOOL_SPAUSEPARAM:
1538 		rc = ethtool_set_pauseparam(dev, useraddr);
1539 		break;
1540 	case ETHTOOL_TEST:
1541 		rc = ethtool_self_test(dev, useraddr);
1542 		break;
1543 	case ETHTOOL_GSTRINGS:
1544 		rc = ethtool_get_strings(dev, useraddr);
1545 		break;
1546 	case ETHTOOL_PHYS_ID:
1547 		rc = ethtool_phys_id(dev, useraddr);
1548 		break;
1549 	case ETHTOOL_GSTATS:
1550 		rc = ethtool_get_stats(dev, useraddr);
1551 		break;
1552 	case ETHTOOL_GPERMADDR:
1553 		rc = ethtool_get_perm_addr(dev, useraddr);
1554 		break;
1555 	case ETHTOOL_GFLAGS:
1556 		rc = ethtool_get_value(dev, useraddr, ethcmd,
1557 					__ethtool_get_flags);
1558 		break;
1559 	case ETHTOOL_SFLAGS:
1560 		rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1561 		break;
1562 	case ETHTOOL_GPFLAGS:
1563 		rc = ethtool_get_value(dev, useraddr, ethcmd,
1564 				       dev->ethtool_ops->get_priv_flags);
1565 		break;
1566 	case ETHTOOL_SPFLAGS:
1567 		rc = ethtool_set_value(dev, useraddr,
1568 				       dev->ethtool_ops->set_priv_flags);
1569 		break;
1570 	case ETHTOOL_GRXFH:
1571 	case ETHTOOL_GRXRINGS:
1572 	case ETHTOOL_GRXCLSRLCNT:
1573 	case ETHTOOL_GRXCLSRULE:
1574 	case ETHTOOL_GRXCLSRLALL:
1575 		rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1576 		break;
1577 	case ETHTOOL_SRXFH:
1578 	case ETHTOOL_SRXCLSRLDEL:
1579 	case ETHTOOL_SRXCLSRLINS:
1580 		rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1581 		break;
1582 	case ETHTOOL_FLASHDEV:
1583 		rc = ethtool_flash_device(dev, useraddr);
1584 		break;
1585 	case ETHTOOL_RESET:
1586 		rc = ethtool_reset(dev, useraddr);
1587 		break;
1588 	case ETHTOOL_GSSET_INFO:
1589 		rc = ethtool_get_sset_info(dev, useraddr);
1590 		break;
1591 	case ETHTOOL_GRXFHINDIR:
1592 		rc = ethtool_get_rxfh_indir(dev, useraddr);
1593 		break;
1594 	case ETHTOOL_SRXFHINDIR:
1595 		rc = ethtool_set_rxfh_indir(dev, useraddr);
1596 		break;
1597 	case ETHTOOL_GFEATURES:
1598 		rc = ethtool_get_features(dev, useraddr);
1599 		break;
1600 	case ETHTOOL_SFEATURES:
1601 		rc = ethtool_set_features(dev, useraddr);
1602 		break;
1603 	case ETHTOOL_GTXCSUM:
1604 	case ETHTOOL_GRXCSUM:
1605 	case ETHTOOL_GSG:
1606 	case ETHTOOL_GTSO:
1607 	case ETHTOOL_GUFO:
1608 	case ETHTOOL_GGSO:
1609 	case ETHTOOL_GGRO:
1610 		rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1611 		break;
1612 	case ETHTOOL_STXCSUM:
1613 	case ETHTOOL_SRXCSUM:
1614 	case ETHTOOL_SSG:
1615 	case ETHTOOL_STSO:
1616 	case ETHTOOL_SUFO:
1617 	case ETHTOOL_SGSO:
1618 	case ETHTOOL_SGRO:
1619 		rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1620 		break;
1621 	case ETHTOOL_GCHANNELS:
1622 		rc = ethtool_get_channels(dev, useraddr);
1623 		break;
1624 	case ETHTOOL_SCHANNELS:
1625 		rc = ethtool_set_channels(dev, useraddr);
1626 		break;
1627 	case ETHTOOL_SET_DUMP:
1628 		rc = ethtool_set_dump(dev, useraddr);
1629 		break;
1630 	case ETHTOOL_GET_DUMP_FLAG:
1631 		rc = ethtool_get_dump_flag(dev, useraddr);
1632 		break;
1633 	case ETHTOOL_GET_DUMP_DATA:
1634 		rc = ethtool_get_dump_data(dev, useraddr);
1635 		break;
1636 	case ETHTOOL_GET_TS_INFO:
1637 		rc = ethtool_get_ts_info(dev, useraddr);
1638 		break;
1639 	case ETHTOOL_GMODULEINFO:
1640 		rc = ethtool_get_module_info(dev, useraddr);
1641 		break;
1642 	case ETHTOOL_GMODULEEEPROM:
1643 		rc = ethtool_get_module_eeprom(dev, useraddr);
1644 		break;
1645 	default:
1646 		rc = -EOPNOTSUPP;
1647 	}
1648 
1649 	if (dev->ethtool_ops->complete)
1650 		dev->ethtool_ops->complete(dev);
1651 
1652 	if (old_features != dev->features)
1653 		netdev_features_change(dev);
1654 
1655 	return rc;
1656 }
1657