blob: 794cf57078cd0a2b983f90e94cf0d89c26c94d70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
20#include <asm/uaccess.h>
21
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090022/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Some useful ethtool_ops methods that're device independent.
24 * If we find that all drivers want to do the same thing here,
25 * we can turn these into dev_() function calls.
26 */
27
28u32 ethtool_op_get_link(struct net_device *dev)
29{
30 return netif_carrier_ok(dev) ? 1 : 0;
31}
32
Sridhar Samudrala1896e612009-07-22 13:38:22 +000033u32 ethtool_op_get_rx_csum(struct net_device *dev)
34{
35 return (dev->features & NETIF_F_ALL_CSUM) != 0;
36}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000037EXPORT_SYMBOL(ethtool_op_get_rx_csum);
Sridhar Samudrala1896e612009-07-22 13:38:22 +000038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039u32 ethtool_op_get_tx_csum(struct net_device *dev)
40{
Herbert Xu8648b302006-06-17 22:06:05 -070041 return (dev->features & NETIF_F_ALL_CSUM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000043EXPORT_SYMBOL(ethtool_op_get_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
46{
47 if (data)
48 dev->features |= NETIF_F_IP_CSUM;
49 else
50 dev->features &= ~NETIF_F_IP_CSUM;
51
52 return 0;
53}
54
Jon Mason69f6a0f2005-05-29 20:27:24 -070055int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
56{
57 if (data)
58 dev->features |= NETIF_F_HW_CSUM;
59 else
60 dev->features &= ~NETIF_F_HW_CSUM;
61
62 return 0;
63}
Michael Chan6460d942007-07-14 19:07:52 -070064
65int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
66{
67 if (data)
68 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
69 else
70 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
71
72 return 0;
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075u32 ethtool_op_get_sg(struct net_device *dev)
76{
77 return (dev->features & NETIF_F_SG) != 0;
78}
79
80int ethtool_op_set_sg(struct net_device *dev, u32 data)
81{
82 if (data)
83 dev->features |= NETIF_F_SG;
84 else
85 dev->features &= ~NETIF_F_SG;
86
87 return 0;
88}
89
90u32 ethtool_op_get_tso(struct net_device *dev)
91{
92 return (dev->features & NETIF_F_TSO) != 0;
93}
94
95int ethtool_op_set_tso(struct net_device *dev, u32 data)
96{
97 if (data)
98 dev->features |= NETIF_F_TSO;
99 else
100 dev->features &= ~NETIF_F_TSO;
101
102 return 0;
103}
104
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700105u32 ethtool_op_get_ufo(struct net_device *dev)
106{
107 return (dev->features & NETIF_F_UFO) != 0;
108}
109
110int ethtool_op_set_ufo(struct net_device *dev, u32 data)
111{
112 if (data)
113 dev->features |= NETIF_F_UFO;
114 else
115 dev->features &= ~NETIF_F_UFO;
116 return 0;
117}
118
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700119/* the following list of flags are the same as their associated
120 * NETIF_F_xxx values in include/linux/netdevice.h
121 */
122static const u32 flags_dup_features =
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800123 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700124
125u32 ethtool_op_get_flags(struct net_device *dev)
126{
127 /* in the future, this function will probably contain additional
128 * handling for flags which are not so easily handled
129 * by a simple masking operation
130 */
131
132 return dev->features & flags_dup_features;
133}
134
135int ethtool_op_set_flags(struct net_device *dev, u32 data)
136{
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000137 const struct ethtool_ops *ops = dev->ethtool_ops;
138
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700139 if (data & ETH_FLAG_LRO)
140 dev->features |= NETIF_F_LRO;
141 else
142 dev->features &= ~NETIF_F_LRO;
143
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000144 if (data & ETH_FLAG_NTUPLE) {
145 if (!ops->set_rx_ntuple)
146 return -EOPNOTSUPP;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800147 dev->features |= NETIF_F_NTUPLE;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000148 } else {
149 /* safe to clear regardless */
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800150 dev->features &= ~NETIF_F_NTUPLE;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000151 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800152
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700153 return 0;
154}
155
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800156void ethtool_ntuple_flush(struct net_device *dev)
157{
158 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
159
160 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
161 list_del(&fsc->list);
162 kfree(fsc);
163 }
164 dev->ethtool_ntuple_list.count = 0;
165}
166EXPORT_SYMBOL(ethtool_ntuple_flush);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Handlers for each ethtool command */
169
170static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
171{
Roland Dreier8e557422010-02-11 12:14:23 -0800172 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int err;
174
175 if (!dev->ethtool_ops->get_settings)
176 return -EOPNOTSUPP;
177
178 err = dev->ethtool_ops->get_settings(dev, &cmd);
179 if (err < 0)
180 return err;
181
182 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
183 return -EFAULT;
184 return 0;
185}
186
187static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
188{
189 struct ethtool_cmd cmd;
190
191 if (!dev->ethtool_ops->set_settings)
192 return -EOPNOTSUPP;
193
194 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
195 return -EFAULT;
196
197 return dev->ethtool_ops->set_settings(dev, &cmd);
198}
199
200static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
201{
202 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700203 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (!ops->get_drvinfo)
206 return -EOPNOTSUPP;
207
208 memset(&info, 0, sizeof(info));
209 info.cmd = ETHTOOL_GDRVINFO;
210 ops->get_drvinfo(dev, &info);
211
Jeff Garzikff03d492007-08-15 16:01:08 -0700212 if (ops->get_sset_count) {
213 int rc;
214
215 rc = ops->get_sset_count(dev, ETH_SS_TEST);
216 if (rc >= 0)
217 info.testinfo_len = rc;
218 rc = ops->get_sset_count(dev, ETH_SS_STATS);
219 if (rc >= 0)
220 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700221 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
222 if (rc >= 0)
223 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (ops->get_regs_len)
226 info.regdump_len = ops->get_regs_len(dev);
227 if (ops->get_eeprom_len)
228 info.eedump_len = ops->get_eeprom_len(dev);
229
230 if (copy_to_user(useraddr, &info, sizeof(info)))
231 return -EFAULT;
232 return 0;
233}
234
Santwona Behera59089d82009-02-20 00:58:13 -0800235static int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700236{
237 struct ethtool_rxnfc cmd;
238
Santwona Behera59089d82009-02-20 00:58:13 -0800239 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700240 return -EOPNOTSUPP;
241
242 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
243 return -EFAULT;
244
Santwona Behera59089d82009-02-20 00:58:13 -0800245 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
Santwona Behera0853ad62008-07-02 03:47:41 -0700246}
247
Santwona Behera59089d82009-02-20 00:58:13 -0800248static int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700249{
250 struct ethtool_rxnfc info;
Santwona Behera59089d82009-02-20 00:58:13 -0800251 const struct ethtool_ops *ops = dev->ethtool_ops;
252 int ret;
253 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700254
Santwona Behera59089d82009-02-20 00:58:13 -0800255 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700256 return -EOPNOTSUPP;
257
258 if (copy_from_user(&info, useraddr, sizeof(info)))
259 return -EFAULT;
260
Santwona Behera59089d82009-02-20 00:58:13 -0800261 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
262 if (info.rule_cnt > 0) {
263 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
264 GFP_USER);
265 if (!rule_buf)
266 return -ENOMEM;
267 }
268 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700269
Santwona Behera59089d82009-02-20 00:58:13 -0800270 ret = ops->get_rxnfc(dev, &info, rule_buf);
271 if (ret < 0)
272 goto err_out;
273
274 ret = -EFAULT;
Santwona Behera0853ad62008-07-02 03:47:41 -0700275 if (copy_to_user(useraddr, &info, sizeof(info)))
Santwona Behera59089d82009-02-20 00:58:13 -0800276 goto err_out;
277
278 if (rule_buf) {
279 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
280 if (copy_to_user(useraddr, rule_buf,
281 info.rule_cnt * sizeof(u32)))
282 goto err_out;
283 }
284 ret = 0;
285
286err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700287 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800288
289 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700290}
291
Peter Waskiewicze8589112010-02-12 13:48:05 +0000292static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
293 struct ethtool_rx_ntuple_flow_spec *spec,
294 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800295{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800296
297 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000298 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
299 /* free the container */
300 kfree(fsc);
301 return;
302 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800303
304 /* Copy the whole filter over */
305 fsc->fs.flow_type = spec->flow_type;
306 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
307 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
308
309 fsc->fs.vlan_tag = spec->vlan_tag;
310 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
311 fsc->fs.data = spec->data;
312 fsc->fs.data_mask = spec->data_mask;
313 fsc->fs.action = spec->action;
314
315 /* add to the list */
316 list_add_tail_rcu(&fsc->list, &list->list);
317 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800318}
319
320static int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
321{
322 struct ethtool_rx_ntuple cmd;
323 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000324 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800325 int ret;
326
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800327 if (!(dev->features & NETIF_F_NTUPLE))
328 return -EINVAL;
329
330 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
331 return -EFAULT;
332
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800333 /*
334 * Cache filter in dev struct for GET operation only if
335 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000336 * only if the filter was added successfully. First make sure we
337 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800338 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000339 if (!ops->get_rx_ntuple) {
340 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
341 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800342 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000343 }
344
345 ret = ops->set_rx_ntuple(dev, &cmd);
346 if (ret) {
347 kfree(fsc);
348 return ret;
349 }
350
351 if (!ops->get_rx_ntuple)
352 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800353
354 return ret;
355}
356
357static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
358{
359 struct ethtool_gstrings gstrings;
360 const struct ethtool_ops *ops = dev->ethtool_ops;
361 struct ethtool_rx_ntuple_flow_spec_container *fsc;
362 u8 *data;
363 char *p;
364 int ret, i, num_strings = 0;
365
366 if (!ops->get_sset_count)
367 return -EOPNOTSUPP;
368
369 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
370 return -EFAULT;
371
372 ret = ops->get_sset_count(dev, gstrings.string_set);
373 if (ret < 0)
374 return ret;
375
376 gstrings.len = ret;
377
378 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
379 if (!data)
380 return -ENOMEM;
381
382 if (ops->get_rx_ntuple) {
383 /* driver-specific filter grab */
384 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
385 goto copy;
386 }
387
388 /* default ethtool filter grab */
389 i = 0;
390 p = (char *)data;
391 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
392 sprintf(p, "Filter %d:\n", i);
393 p += ETH_GSTRING_LEN;
394 num_strings++;
395
396 switch (fsc->fs.flow_type) {
397 case TCP_V4_FLOW:
398 sprintf(p, "\tFlow Type: TCP\n");
399 p += ETH_GSTRING_LEN;
400 num_strings++;
401 break;
402 case UDP_V4_FLOW:
403 sprintf(p, "\tFlow Type: UDP\n");
404 p += ETH_GSTRING_LEN;
405 num_strings++;
406 break;
407 case SCTP_V4_FLOW:
408 sprintf(p, "\tFlow Type: SCTP\n");
409 p += ETH_GSTRING_LEN;
410 num_strings++;
411 break;
412 case AH_ESP_V4_FLOW:
413 sprintf(p, "\tFlow Type: AH ESP\n");
414 p += ETH_GSTRING_LEN;
415 num_strings++;
416 break;
417 case ESP_V4_FLOW:
418 sprintf(p, "\tFlow Type: ESP\n");
419 p += ETH_GSTRING_LEN;
420 num_strings++;
421 break;
422 case IP_USER_FLOW:
423 sprintf(p, "\tFlow Type: Raw IP\n");
424 p += ETH_GSTRING_LEN;
425 num_strings++;
426 break;
427 case IPV4_FLOW:
428 sprintf(p, "\tFlow Type: IPv4\n");
429 p += ETH_GSTRING_LEN;
430 num_strings++;
431 break;
432 default:
433 sprintf(p, "\tFlow Type: Unknown\n");
434 p += ETH_GSTRING_LEN;
435 num_strings++;
436 goto unknown_filter;
437 };
438
439 /* now the rest of the filters */
440 switch (fsc->fs.flow_type) {
441 case TCP_V4_FLOW:
442 case UDP_V4_FLOW:
443 case SCTP_V4_FLOW:
444 sprintf(p, "\tSrc IP addr: 0x%x\n",
445 fsc->fs.h_u.tcp_ip4_spec.ip4src);
446 p += ETH_GSTRING_LEN;
447 num_strings++;
448 sprintf(p, "\tSrc IP mask: 0x%x\n",
449 fsc->fs.m_u.tcp_ip4_spec.ip4src);
450 p += ETH_GSTRING_LEN;
451 num_strings++;
452 sprintf(p, "\tDest IP addr: 0x%x\n",
453 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
454 p += ETH_GSTRING_LEN;
455 num_strings++;
456 sprintf(p, "\tDest IP mask: 0x%x\n",
457 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
458 p += ETH_GSTRING_LEN;
459 num_strings++;
460 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
461 fsc->fs.h_u.tcp_ip4_spec.psrc,
462 fsc->fs.m_u.tcp_ip4_spec.psrc);
463 p += ETH_GSTRING_LEN;
464 num_strings++;
465 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
466 fsc->fs.h_u.tcp_ip4_spec.pdst,
467 fsc->fs.m_u.tcp_ip4_spec.pdst);
468 p += ETH_GSTRING_LEN;
469 num_strings++;
470 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
471 fsc->fs.h_u.tcp_ip4_spec.tos,
472 fsc->fs.m_u.tcp_ip4_spec.tos);
473 p += ETH_GSTRING_LEN;
474 num_strings++;
475 break;
476 case AH_ESP_V4_FLOW:
477 case ESP_V4_FLOW:
478 sprintf(p, "\tSrc IP addr: 0x%x\n",
479 fsc->fs.h_u.ah_ip4_spec.ip4src);
480 p += ETH_GSTRING_LEN;
481 num_strings++;
482 sprintf(p, "\tSrc IP mask: 0x%x\n",
483 fsc->fs.m_u.ah_ip4_spec.ip4src);
484 p += ETH_GSTRING_LEN;
485 num_strings++;
486 sprintf(p, "\tDest IP addr: 0x%x\n",
487 fsc->fs.h_u.ah_ip4_spec.ip4dst);
488 p += ETH_GSTRING_LEN;
489 num_strings++;
490 sprintf(p, "\tDest IP mask: 0x%x\n",
491 fsc->fs.m_u.ah_ip4_spec.ip4dst);
492 p += ETH_GSTRING_LEN;
493 num_strings++;
494 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
495 fsc->fs.h_u.ah_ip4_spec.spi,
496 fsc->fs.m_u.ah_ip4_spec.spi);
497 p += ETH_GSTRING_LEN;
498 num_strings++;
499 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
500 fsc->fs.h_u.ah_ip4_spec.tos,
501 fsc->fs.m_u.ah_ip4_spec.tos);
502 p += ETH_GSTRING_LEN;
503 num_strings++;
504 break;
505 case IP_USER_FLOW:
506 sprintf(p, "\tSrc IP addr: 0x%x\n",
507 fsc->fs.h_u.raw_ip4_spec.ip4src);
508 p += ETH_GSTRING_LEN;
509 num_strings++;
510 sprintf(p, "\tSrc IP mask: 0x%x\n",
511 fsc->fs.m_u.raw_ip4_spec.ip4src);
512 p += ETH_GSTRING_LEN;
513 num_strings++;
514 sprintf(p, "\tDest IP addr: 0x%x\n",
515 fsc->fs.h_u.raw_ip4_spec.ip4dst);
516 p += ETH_GSTRING_LEN;
517 num_strings++;
518 sprintf(p, "\tDest IP mask: 0x%x\n",
519 fsc->fs.m_u.raw_ip4_spec.ip4dst);
520 p += ETH_GSTRING_LEN;
521 num_strings++;
522 break;
523 case IPV4_FLOW:
524 sprintf(p, "\tSrc IP addr: 0x%x\n",
525 fsc->fs.h_u.usr_ip4_spec.ip4src);
526 p += ETH_GSTRING_LEN;
527 num_strings++;
528 sprintf(p, "\tSrc IP mask: 0x%x\n",
529 fsc->fs.m_u.usr_ip4_spec.ip4src);
530 p += ETH_GSTRING_LEN;
531 num_strings++;
532 sprintf(p, "\tDest IP addr: 0x%x\n",
533 fsc->fs.h_u.usr_ip4_spec.ip4dst);
534 p += ETH_GSTRING_LEN;
535 num_strings++;
536 sprintf(p, "\tDest IP mask: 0x%x\n",
537 fsc->fs.m_u.usr_ip4_spec.ip4dst);
538 p += ETH_GSTRING_LEN;
539 num_strings++;
540 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
541 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
542 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
543 p += ETH_GSTRING_LEN;
544 num_strings++;
545 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
546 fsc->fs.h_u.usr_ip4_spec.tos,
547 fsc->fs.m_u.usr_ip4_spec.tos);
548 p += ETH_GSTRING_LEN;
549 num_strings++;
550 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
551 fsc->fs.h_u.usr_ip4_spec.ip_ver,
552 fsc->fs.m_u.usr_ip4_spec.ip_ver);
553 p += ETH_GSTRING_LEN;
554 num_strings++;
555 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
556 fsc->fs.h_u.usr_ip4_spec.proto,
557 fsc->fs.m_u.usr_ip4_spec.proto);
558 p += ETH_GSTRING_LEN;
559 num_strings++;
560 break;
561 };
562 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
563 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
564 p += ETH_GSTRING_LEN;
565 num_strings++;
566 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
567 p += ETH_GSTRING_LEN;
568 num_strings++;
569 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
570 p += ETH_GSTRING_LEN;
571 num_strings++;
572 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
573 sprintf(p, "\tAction: Drop\n");
574 else
575 sprintf(p, "\tAction: Direct to queue %d\n",
576 fsc->fs.action);
577 p += ETH_GSTRING_LEN;
578 num_strings++;
579unknown_filter:
580 i++;
581 }
582copy:
583 /* indicate to userspace how many strings we actually have */
584 gstrings.len = num_strings;
585 ret = -EFAULT;
586 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
587 goto out;
588 useraddr += sizeof(gstrings);
589 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
590 goto out;
591 ret = 0;
592
593out:
594 kfree(data);
595 return ret;
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
599{
600 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700601 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 void *regbuf;
603 int reglen, ret;
604
605 if (!ops->get_regs || !ops->get_regs_len)
606 return -EOPNOTSUPP;
607
608 if (copy_from_user(&regs, useraddr, sizeof(regs)))
609 return -EFAULT;
610
611 reglen = ops->get_regs_len(dev);
612 if (regs.len > reglen)
613 regs.len = reglen;
614
615 regbuf = kmalloc(reglen, GFP_USER);
616 if (!regbuf)
617 return -ENOMEM;
618
619 ops->get_regs(dev, &regs, regbuf);
620
621 ret = -EFAULT;
622 if (copy_to_user(useraddr, &regs, sizeof(regs)))
623 goto out;
624 useraddr += offsetof(struct ethtool_regs, data);
625 if (copy_to_user(useraddr, regbuf, regs.len))
626 goto out;
627 ret = 0;
628
629 out:
630 kfree(regbuf);
631 return ret;
632}
633
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000634static int ethtool_reset(struct net_device *dev, char __user *useraddr)
635{
636 struct ethtool_value reset;
637 int ret;
638
639 if (!dev->ethtool_ops->reset)
640 return -EOPNOTSUPP;
641
642 if (copy_from_user(&reset, useraddr, sizeof(reset)))
643 return -EFAULT;
644
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800645 /* Clear ethtool n-tuple list */
646 ethtool_ntuple_flush(dev);
647
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000648 ret = dev->ethtool_ops->reset(dev, &reset.data);
649 if (ret)
650 return ret;
651
652 if (copy_to_user(useraddr, &reset, sizeof(reset)))
653 return -EFAULT;
654 return 0;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
658{
Roland Dreier8e557422010-02-11 12:14:23 -0800659 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (!dev->ethtool_ops->get_wol)
662 return -EOPNOTSUPP;
663
664 dev->ethtool_ops->get_wol(dev, &wol);
665
666 if (copy_to_user(useraddr, &wol, sizeof(wol)))
667 return -EFAULT;
668 return 0;
669}
670
671static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
672{
673 struct ethtool_wolinfo wol;
674
675 if (!dev->ethtool_ops->set_wol)
676 return -EOPNOTSUPP;
677
678 if (copy_from_user(&wol, useraddr, sizeof(wol)))
679 return -EFAULT;
680
681 return dev->ethtool_ops->set_wol(dev, &wol);
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int ethtool_nway_reset(struct net_device *dev)
685{
686 if (!dev->ethtool_ops->nway_reset)
687 return -EOPNOTSUPP;
688
689 return dev->ethtool_ops->nway_reset(dev);
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
693{
694 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700695 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700696 void __user *userbuf = useraddr + sizeof(eeprom);
697 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700699 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (!ops->get_eeprom || !ops->get_eeprom_len)
702 return -EOPNOTSUPP;
703
704 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
705 return -EFAULT;
706
707 /* Check for wrap and zero */
708 if (eeprom.offset + eeprom.len <= eeprom.offset)
709 return -EINVAL;
710
711 /* Check for exceeding total eeprom len */
712 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
713 return -EINVAL;
714
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700715 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!data)
717 return -ENOMEM;
718
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700719 bytes_remaining = eeprom.len;
720 while (bytes_remaining > 0) {
721 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700723 ret = ops->get_eeprom(dev, &eeprom, data);
724 if (ret)
725 break;
726 if (copy_to_user(userbuf, data, eeprom.len)) {
727 ret = -EFAULT;
728 break;
729 }
730 userbuf += eeprom.len;
731 eeprom.offset += eeprom.len;
732 bytes_remaining -= eeprom.len;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700735 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
736 eeprom.offset -= eeprom.len;
737 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
738 ret = -EFAULT;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 kfree(data);
741 return ret;
742}
743
744static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
745{
746 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700747 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700748 void __user *userbuf = useraddr + sizeof(eeprom);
749 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700751 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (!ops->set_eeprom || !ops->get_eeprom_len)
754 return -EOPNOTSUPP;
755
756 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
757 return -EFAULT;
758
759 /* Check for wrap and zero */
760 if (eeprom.offset + eeprom.len <= eeprom.offset)
761 return -EINVAL;
762
763 /* Check for exceeding total eeprom len */
764 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
765 return -EINVAL;
766
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700767 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!data)
769 return -ENOMEM;
770
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700771 bytes_remaining = eeprom.len;
772 while (bytes_remaining > 0) {
773 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700775 if (copy_from_user(data, userbuf, eeprom.len)) {
776 ret = -EFAULT;
777 break;
778 }
779 ret = ops->set_eeprom(dev, &eeprom, data);
780 if (ret)
781 break;
782 userbuf += eeprom.len;
783 eeprom.offset += eeprom.len;
784 bytes_remaining -= eeprom.len;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 kfree(data);
788 return ret;
789}
790
791static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
792{
Roland Dreier8e557422010-02-11 12:14:23 -0800793 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (!dev->ethtool_ops->get_coalesce)
796 return -EOPNOTSUPP;
797
798 dev->ethtool_ops->get_coalesce(dev, &coalesce);
799
800 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
801 return -EFAULT;
802 return 0;
803}
804
805static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
806{
807 struct ethtool_coalesce coalesce;
808
David S. Millerfa04ae52005-06-06 15:07:19 -0700809 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EOPNOTSUPP;
811
812 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
813 return -EFAULT;
814
815 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
816}
817
818static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
819{
Roland Dreier8e557422010-02-11 12:14:23 -0800820 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (!dev->ethtool_ops->get_ringparam)
823 return -EOPNOTSUPP;
824
825 dev->ethtool_ops->get_ringparam(dev, &ringparam);
826
827 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
828 return -EFAULT;
829 return 0;
830}
831
832static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
833{
834 struct ethtool_ringparam ringparam;
835
836 if (!dev->ethtool_ops->set_ringparam)
837 return -EOPNOTSUPP;
838
839 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
840 return -EFAULT;
841
842 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
843}
844
845static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
846{
847 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
848
849 if (!dev->ethtool_ops->get_pauseparam)
850 return -EOPNOTSUPP;
851
852 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
853
854 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
855 return -EFAULT;
856 return 0;
857}
858
859static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
860{
861 struct ethtool_pauseparam pauseparam;
862
Jeff Garzike1b90c42006-07-17 12:54:40 -0400863 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -EOPNOTSUPP;
865
866 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
867 return -EFAULT;
868
869 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872static int __ethtool_set_sg(struct net_device *dev, u32 data)
873{
874 int err;
875
876 if (!data && dev->ethtool_ops->set_tso) {
877 err = dev->ethtool_ops->set_tso(dev, 0);
878 if (err)
879 return err;
880 }
881
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700882 if (!data && dev->ethtool_ops->set_ufo) {
883 err = dev->ethtool_ops->set_ufo(dev, 0);
884 if (err)
885 return err;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return dev->ethtool_ops->set_sg(dev, data);
888}
889
890static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
891{
892 struct ethtool_value edata;
893 int err;
894
895 if (!dev->ethtool_ops->set_tx_csum)
896 return -EOPNOTSUPP;
897
898 if (copy_from_user(&edata, useraddr, sizeof(edata)))
899 return -EFAULT;
900
901 if (!edata.data && dev->ethtool_ops->set_sg) {
902 err = __ethtool_set_sg(dev, 0);
903 if (err)
904 return err;
905 }
906
907 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
908}
909
Herbert Xub240a0e2008-12-15 23:44:31 -0800910static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
911{
912 struct ethtool_value edata;
913
914 if (!dev->ethtool_ops->set_rx_csum)
915 return -EOPNOTSUPP;
916
917 if (copy_from_user(&edata, useraddr, sizeof(edata)))
918 return -EFAULT;
919
920 if (!edata.data && dev->ethtool_ops->set_sg)
921 dev->features &= ~NETIF_F_GRO;
922
923 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
927{
928 struct ethtool_value edata;
929
930 if (!dev->ethtool_ops->set_sg)
931 return -EOPNOTSUPP;
932
933 if (copy_from_user(&edata, useraddr, sizeof(edata)))
934 return -EFAULT;
935
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900936 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -0700937 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EINVAL;
939
940 return __ethtool_set_sg(dev, edata.data);
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
944{
945 struct ethtool_value edata;
946
947 if (!dev->ethtool_ops->set_tso)
948 return -EOPNOTSUPP;
949
950 if (copy_from_user(&edata, useraddr, sizeof(edata)))
951 return -EFAULT;
952
953 if (edata.data && !(dev->features & NETIF_F_SG))
954 return -EINVAL;
955
956 return dev->ethtool_ops->set_tso(dev, edata.data);
957}
958
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700959static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
960{
961 struct ethtool_value edata;
962
963 if (!dev->ethtool_ops->set_ufo)
964 return -EOPNOTSUPP;
965 if (copy_from_user(&edata, useraddr, sizeof(edata)))
966 return -EFAULT;
967 if (edata.data && !(dev->features & NETIF_F_SG))
968 return -EINVAL;
969 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
970 return -EINVAL;
971 return dev->ethtool_ops->set_ufo(dev, edata.data);
972}
973
Herbert Xu37c31852006-06-22 03:07:29 -0700974static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
975{
976 struct ethtool_value edata = { ETHTOOL_GGSO };
977
978 edata.data = dev->features & NETIF_F_GSO;
979 if (copy_to_user(useraddr, &edata, sizeof(edata)))
980 return -EFAULT;
981 return 0;
982}
983
984static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
985{
986 struct ethtool_value edata;
987
988 if (copy_from_user(&edata, useraddr, sizeof(edata)))
989 return -EFAULT;
990 if (edata.data)
991 dev->features |= NETIF_F_GSO;
992 else
993 dev->features &= ~NETIF_F_GSO;
994 return 0;
995}
996
Herbert Xub240a0e2008-12-15 23:44:31 -0800997static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
998{
999 struct ethtool_value edata = { ETHTOOL_GGRO };
1000
1001 edata.data = dev->features & NETIF_F_GRO;
1002 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1003 return -EFAULT;
1004 return 0;
1005}
1006
1007static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1008{
1009 struct ethtool_value edata;
1010
1011 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1012 return -EFAULT;
1013
1014 if (edata.data) {
1015 if (!dev->ethtool_ops->get_rx_csum ||
1016 !dev->ethtool_ops->get_rx_csum(dev))
1017 return -EINVAL;
1018 dev->features |= NETIF_F_GRO;
1019 } else
1020 dev->features &= ~NETIF_F_GRO;
1021
1022 return 0;
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1026{
1027 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001028 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001030 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001032 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001033 return -EOPNOTSUPP;
1034
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001035 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001036 if (test_len < 0)
1037 return test_len;
1038 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (copy_from_user(&test, useraddr, sizeof(test)))
1041 return -EFAULT;
1042
Jeff Garzikff03d492007-08-15 16:01:08 -07001043 test.len = test_len;
1044 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!data)
1046 return -ENOMEM;
1047
1048 ops->self_test(dev, &test, data);
1049
1050 ret = -EFAULT;
1051 if (copy_to_user(useraddr, &test, sizeof(test)))
1052 goto out;
1053 useraddr += sizeof(test);
1054 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1055 goto out;
1056 ret = 0;
1057
1058 out:
1059 kfree(data);
1060 return ret;
1061}
1062
1063static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1064{
1065 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001066 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 u8 *data;
1068 int ret;
1069
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001070 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -EOPNOTSUPP;
1072
1073 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1074 return -EFAULT;
1075
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001076 ret = ops->get_sset_count(dev, gstrings.string_set);
1077 if (ret < 0)
1078 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001079
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001080 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1083 if (!data)
1084 return -ENOMEM;
1085
1086 ops->get_strings(dev, gstrings.string_set, data);
1087
1088 ret = -EFAULT;
1089 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1090 goto out;
1091 useraddr += sizeof(gstrings);
1092 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1093 goto out;
1094 ret = 0;
1095
1096 out:
1097 kfree(data);
1098 return ret;
1099}
1100
1101static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1102{
1103 struct ethtool_value id;
1104
1105 if (!dev->ethtool_ops->phys_id)
1106 return -EOPNOTSUPP;
1107
1108 if (copy_from_user(&id, useraddr, sizeof(id)))
1109 return -EFAULT;
1110
1111 return dev->ethtool_ops->phys_id(dev, id.data);
1112}
1113
1114static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1115{
1116 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001117 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001119 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001121 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001122 return -EOPNOTSUPP;
1123
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001124 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001125 if (n_stats < 0)
1126 return n_stats;
1127 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1130 return -EFAULT;
1131
Jeff Garzikff03d492007-08-15 16:01:08 -07001132 stats.n_stats = n_stats;
1133 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!data)
1135 return -ENOMEM;
1136
1137 ops->get_ethtool_stats(dev, &stats, data);
1138
1139 ret = -EFAULT;
1140 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1141 goto out;
1142 useraddr += sizeof(stats);
1143 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1144 goto out;
1145 ret = 0;
1146
1147 out:
1148 kfree(data);
1149 return ret;
1150}
1151
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001152static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001153{
1154 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001155
Matthew Wilcox313674a2007-07-31 14:00:29 -07001156 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001157 return -EFAULT;
1158
Matthew Wilcox313674a2007-07-31 14:00:29 -07001159 if (epaddr.size < dev->addr_len)
1160 return -ETOOSMALL;
1161 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001162
Jon Wetzela6f9a702005-08-20 17:15:54 -07001163 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001164 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001165 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001166 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1167 return -EFAULT;
1168 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001169}
1170
Jeff Garzik13c99b22007-08-15 16:01:56 -07001171static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1172 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001173{
Roland Dreier8e557422010-02-11 12:14:23 -08001174 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001175
Jeff Garzik13c99b22007-08-15 16:01:56 -07001176 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001177 return -EOPNOTSUPP;
1178
Jeff Garzik13c99b22007-08-15 16:01:56 -07001179 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001180
1181 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1182 return -EFAULT;
1183 return 0;
1184}
1185
Jeff Garzik13c99b22007-08-15 16:01:56 -07001186static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1187 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001188{
1189 struct ethtool_value edata;
1190
Jeff Garzik13c99b22007-08-15 16:01:56 -07001191 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001192 return -EOPNOTSUPP;
1193
1194 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1195 return -EFAULT;
1196
Jeff Garzik13c99b22007-08-15 16:01:56 -07001197 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001198 return 0;
1199}
1200
Jeff Garzik13c99b22007-08-15 16:01:56 -07001201static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1202 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001203{
1204 struct ethtool_value edata;
1205
Jeff Garzik13c99b22007-08-15 16:01:56 -07001206 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001207 return -EOPNOTSUPP;
1208
1209 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1210 return -EFAULT;
1211
Jeff Garzik13c99b22007-08-15 16:01:56 -07001212 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001213}
1214
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001215static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1216{
1217 struct ethtool_flash efl;
1218
1219 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1220 return -EFAULT;
1221
1222 if (!dev->ethtool_ops->flash_device)
1223 return -EOPNOTSUPP;
1224
1225 return dev->ethtool_ops->flash_device(dev, &efl);
1226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228/* The main entry point in this file. Called from net/core/dev.c */
1229
Eric W. Biederman881d9662007-09-17 11:56:21 -07001230int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001232 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 void __user *useraddr = ifr->ifr_data;
1234 u32 ethcmd;
1235 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -07001236 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!dev || !netif_device_present(dev))
1239 return -ENODEV;
1240
1241 if (!dev->ethtool_ops)
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001242 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
1245 return -EFAULT;
1246
Stephen Hemminger75f31232006-09-28 15:13:37 -07001247 /* Allow some commands to be done by anyone */
1248 switch(ethcmd) {
Stephen Hemminger75f31232006-09-28 15:13:37 -07001249 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001250 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001251 case ETHTOOL_GCOALESCE:
1252 case ETHTOOL_GRINGPARAM:
1253 case ETHTOOL_GPAUSEPARAM:
1254 case ETHTOOL_GRXCSUM:
1255 case ETHTOOL_GTXCSUM:
1256 case ETHTOOL_GSG:
1257 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001258 case ETHTOOL_GTSO:
1259 case ETHTOOL_GPERMADDR:
1260 case ETHTOOL_GUFO:
1261 case ETHTOOL_GGSO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001262 case ETHTOOL_GFLAGS:
1263 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001264 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001265 case ETHTOOL_GRXRINGS:
1266 case ETHTOOL_GRXCLSRLCNT:
1267 case ETHTOOL_GRXCLSRULE:
1268 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001269 break;
1270 default:
1271 if (!capable(CAP_NET_ADMIN))
1272 return -EPERM;
1273 }
1274
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001275 if (dev->ethtool_ops->begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1277 return rc;
1278
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001279 old_features = dev->features;
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 switch (ethcmd) {
1282 case ETHTOOL_GSET:
1283 rc = ethtool_get_settings(dev, useraddr);
1284 break;
1285 case ETHTOOL_SSET:
1286 rc = ethtool_set_settings(dev, useraddr);
1287 break;
1288 case ETHTOOL_GDRVINFO:
1289 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291 case ETHTOOL_GREGS:
1292 rc = ethtool_get_regs(dev, useraddr);
1293 break;
1294 case ETHTOOL_GWOL:
1295 rc = ethtool_get_wol(dev, useraddr);
1296 break;
1297 case ETHTOOL_SWOL:
1298 rc = ethtool_set_wol(dev, useraddr);
1299 break;
1300 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001301 rc = ethtool_get_value(dev, useraddr, ethcmd,
1302 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001305 rc = ethtool_set_value_void(dev, useraddr,
1306 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
1308 case ETHTOOL_NWAY_RST:
1309 rc = ethtool_nway_reset(dev);
1310 break;
1311 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001312 rc = ethtool_get_value(dev, useraddr, ethcmd,
1313 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 break;
1315 case ETHTOOL_GEEPROM:
1316 rc = ethtool_get_eeprom(dev, useraddr);
1317 break;
1318 case ETHTOOL_SEEPROM:
1319 rc = ethtool_set_eeprom(dev, useraddr);
1320 break;
1321 case ETHTOOL_GCOALESCE:
1322 rc = ethtool_get_coalesce(dev, useraddr);
1323 break;
1324 case ETHTOOL_SCOALESCE:
1325 rc = ethtool_set_coalesce(dev, useraddr);
1326 break;
1327 case ETHTOOL_GRINGPARAM:
1328 rc = ethtool_get_ringparam(dev, useraddr);
1329 break;
1330 case ETHTOOL_SRINGPARAM:
1331 rc = ethtool_set_ringparam(dev, useraddr);
1332 break;
1333 case ETHTOOL_GPAUSEPARAM:
1334 rc = ethtool_get_pauseparam(dev, useraddr);
1335 break;
1336 case ETHTOOL_SPAUSEPARAM:
1337 rc = ethtool_set_pauseparam(dev, useraddr);
1338 break;
1339 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001340 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001341 (dev->ethtool_ops->get_rx_csum ?
1342 dev->ethtool_ops->get_rx_csum :
1343 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001346 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
1348 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001349 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001350 (dev->ethtool_ops->get_tx_csum ?
1351 dev->ethtool_ops->get_tx_csum :
1352 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 break;
1354 case ETHTOOL_STXCSUM:
1355 rc = ethtool_set_tx_csum(dev, useraddr);
1356 break;
1357 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001358 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001359 (dev->ethtool_ops->get_sg ?
1360 dev->ethtool_ops->get_sg :
1361 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
1363 case ETHTOOL_SSG:
1364 rc = ethtool_set_sg(dev, useraddr);
1365 break;
1366 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001367 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001368 (dev->ethtool_ops->get_tso ?
1369 dev->ethtool_ops->get_tso :
1370 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372 case ETHTOOL_STSO:
1373 rc = ethtool_set_tso(dev, useraddr);
1374 break;
1375 case ETHTOOL_TEST:
1376 rc = ethtool_self_test(dev, useraddr);
1377 break;
1378 case ETHTOOL_GSTRINGS:
1379 rc = ethtool_get_strings(dev, useraddr);
1380 break;
1381 case ETHTOOL_PHYS_ID:
1382 rc = ethtool_phys_id(dev, useraddr);
1383 break;
1384 case ETHTOOL_GSTATS:
1385 rc = ethtool_get_stats(dev, useraddr);
1386 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001387 case ETHTOOL_GPERMADDR:
1388 rc = ethtool_get_perm_addr(dev, useraddr);
1389 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001390 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001391 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001392 (dev->ethtool_ops->get_ufo ?
1393 dev->ethtool_ops->get_ufo :
1394 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001395 break;
1396 case ETHTOOL_SUFO:
1397 rc = ethtool_set_ufo(dev, useraddr);
1398 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001399 case ETHTOOL_GGSO:
1400 rc = ethtool_get_gso(dev, useraddr);
1401 break;
1402 case ETHTOOL_SGSO:
1403 rc = ethtool_set_gso(dev, useraddr);
1404 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001405 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001406 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001407 (dev->ethtool_ops->get_flags ?
1408 dev->ethtool_ops->get_flags :
1409 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001410 break;
1411 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001412 rc = ethtool_set_value(dev, useraddr,
1413 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001414 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001415 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001416 rc = ethtool_get_value(dev, useraddr, ethcmd,
1417 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001418 break;
1419 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001420 rc = ethtool_set_value(dev, useraddr,
1421 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001422 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001423 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001424 case ETHTOOL_GRXRINGS:
1425 case ETHTOOL_GRXCLSRLCNT:
1426 case ETHTOOL_GRXCLSRULE:
1427 case ETHTOOL_GRXCLSRLALL:
1428 rc = ethtool_get_rxnfc(dev, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001429 break;
1430 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001431 case ETHTOOL_SRXCLSRLDEL:
1432 case ETHTOOL_SRXCLSRLINS:
1433 rc = ethtool_set_rxnfc(dev, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001434 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001435 case ETHTOOL_GGRO:
1436 rc = ethtool_get_gro(dev, useraddr);
1437 break;
1438 case ETHTOOL_SGRO:
1439 rc = ethtool_set_gro(dev, useraddr);
1440 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001441 case ETHTOOL_FLASHDEV:
1442 rc = ethtool_flash_device(dev, useraddr);
1443 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001444 case ETHTOOL_RESET:
1445 rc = ethtool_reset(dev, useraddr);
1446 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001447 case ETHTOOL_SRXNTUPLE:
1448 rc = ethtool_set_rx_ntuple(dev, useraddr);
1449 break;
1450 case ETHTOOL_GRXNTUPLE:
1451 rc = ethtool_get_rx_ntuple(dev, useraddr);
1452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001454 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001456
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001457 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001459
1460 if (old_features != dev->features)
1461 netdev_features_change(dev);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466EXPORT_SYMBOL(ethtool_op_get_link);
1467EXPORT_SYMBOL(ethtool_op_get_sg);
1468EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469EXPORT_SYMBOL(ethtool_op_set_sg);
1470EXPORT_SYMBOL(ethtool_op_set_tso);
1471EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Jon Mason69f6a0f2005-05-29 20:27:24 -07001472EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -07001473EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001474EXPORT_SYMBOL(ethtool_op_set_ufo);
1475EXPORT_SYMBOL(ethtool_op_get_ufo);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001476EXPORT_SYMBOL(ethtool_op_set_flags);
1477EXPORT_SYMBOL(ethtool_op_get_flags);