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