blob: a1280f643bf4c40cc8f358b4a1398f72c21f7c89 [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{
137 if (data & ETH_FLAG_LRO)
138 dev->features |= NETIF_F_LRO;
139 else
140 dev->features &= ~NETIF_F_LRO;
141
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800142 if (data & ETH_FLAG_NTUPLE)
143 dev->features |= NETIF_F_NTUPLE;
144 else
145 dev->features &= ~NETIF_F_NTUPLE;
146
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700147 return 0;
148}
149
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800150void ethtool_ntuple_flush(struct net_device *dev)
151{
152 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
153
154 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
155 list_del(&fsc->list);
156 kfree(fsc);
157 }
158 dev->ethtool_ntuple_list.count = 0;
159}
160EXPORT_SYMBOL(ethtool_ntuple_flush);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Handlers for each ethtool command */
163
164static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
165{
Roland Dreier8e557422010-02-11 12:14:23 -0800166 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int err;
168
169 if (!dev->ethtool_ops->get_settings)
170 return -EOPNOTSUPP;
171
172 err = dev->ethtool_ops->get_settings(dev, &cmd);
173 if (err < 0)
174 return err;
175
176 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
177 return -EFAULT;
178 return 0;
179}
180
181static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
182{
183 struct ethtool_cmd cmd;
184
185 if (!dev->ethtool_ops->set_settings)
186 return -EOPNOTSUPP;
187
188 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
189 return -EFAULT;
190
191 return dev->ethtool_ops->set_settings(dev, &cmd);
192}
193
194static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
195{
196 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700197 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (!ops->get_drvinfo)
200 return -EOPNOTSUPP;
201
202 memset(&info, 0, sizeof(info));
203 info.cmd = ETHTOOL_GDRVINFO;
204 ops->get_drvinfo(dev, &info);
205
Jeff Garzikff03d492007-08-15 16:01:08 -0700206 if (ops->get_sset_count) {
207 int rc;
208
209 rc = ops->get_sset_count(dev, ETH_SS_TEST);
210 if (rc >= 0)
211 info.testinfo_len = rc;
212 rc = ops->get_sset_count(dev, ETH_SS_STATS);
213 if (rc >= 0)
214 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700215 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
216 if (rc >= 0)
217 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (ops->get_regs_len)
220 info.regdump_len = ops->get_regs_len(dev);
221 if (ops->get_eeprom_len)
222 info.eedump_len = ops->get_eeprom_len(dev);
223
224 if (copy_to_user(useraddr, &info, sizeof(info)))
225 return -EFAULT;
226 return 0;
227}
228
Santwona Behera59089d82009-02-20 00:58:13 -0800229static int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700230{
231 struct ethtool_rxnfc cmd;
232
Santwona Behera59089d82009-02-20 00:58:13 -0800233 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700234 return -EOPNOTSUPP;
235
236 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
237 return -EFAULT;
238
Santwona Behera59089d82009-02-20 00:58:13 -0800239 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
Santwona Behera0853ad62008-07-02 03:47:41 -0700240}
241
Santwona Behera59089d82009-02-20 00:58:13 -0800242static int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700243{
244 struct ethtool_rxnfc info;
Santwona Behera59089d82009-02-20 00:58:13 -0800245 const struct ethtool_ops *ops = dev->ethtool_ops;
246 int ret;
247 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700248
Santwona Behera59089d82009-02-20 00:58:13 -0800249 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700250 return -EOPNOTSUPP;
251
252 if (copy_from_user(&info, useraddr, sizeof(info)))
253 return -EFAULT;
254
Santwona Behera59089d82009-02-20 00:58:13 -0800255 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
256 if (info.rule_cnt > 0) {
257 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
258 GFP_USER);
259 if (!rule_buf)
260 return -ENOMEM;
261 }
262 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700263
Santwona Behera59089d82009-02-20 00:58:13 -0800264 ret = ops->get_rxnfc(dev, &info, rule_buf);
265 if (ret < 0)
266 goto err_out;
267
268 ret = -EFAULT;
Santwona Behera0853ad62008-07-02 03:47:41 -0700269 if (copy_to_user(useraddr, &info, sizeof(info)))
Santwona Behera59089d82009-02-20 00:58:13 -0800270 goto err_out;
271
272 if (rule_buf) {
273 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
274 if (copy_to_user(useraddr, rule_buf,
275 info.rule_cnt * sizeof(u32)))
276 goto err_out;
277 }
278 ret = 0;
279
280err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700281 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800282
283 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700284}
285
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800286static int __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
287 struct ethtool_rx_ntuple_flow_spec *spec)
288{
289 struct ethtool_rx_ntuple_flow_spec_container *fsc;
290
291 /* don't add filters forever */
292 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY)
293 return 0;
294
295 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
296 if (!fsc)
297 return -ENOMEM;
298
299 /* Copy the whole filter over */
300 fsc->fs.flow_type = spec->flow_type;
301 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
302 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
303
304 fsc->fs.vlan_tag = spec->vlan_tag;
305 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
306 fsc->fs.data = spec->data;
307 fsc->fs.data_mask = spec->data_mask;
308 fsc->fs.action = spec->action;
309
310 /* add to the list */
311 list_add_tail_rcu(&fsc->list, &list->list);
312 list->count++;
313
314 return 0;
315}
316
317static int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
318{
319 struct ethtool_rx_ntuple cmd;
320 const struct ethtool_ops *ops = dev->ethtool_ops;
321 int ret;
322
323 if (!ops->set_rx_ntuple)
324 return -EOPNOTSUPP;
325
326 if (!(dev->features & NETIF_F_NTUPLE))
327 return -EINVAL;
328
329 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
330 return -EFAULT;
331
332 ret = ops->set_rx_ntuple(dev, &cmd);
333
334 /*
335 * Cache filter in dev struct for GET operation only if
336 * the underlying driver doesn't have its own GET operation, and
337 * only if the filter was added successfully.
338 */
339 if (!ops->get_rx_ntuple && !ret)
340 if (__rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs))
341 return -ENOMEM;
342
343 return ret;
344}
345
346static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
347{
348 struct ethtool_gstrings gstrings;
349 const struct ethtool_ops *ops = dev->ethtool_ops;
350 struct ethtool_rx_ntuple_flow_spec_container *fsc;
351 u8 *data;
352 char *p;
353 int ret, i, num_strings = 0;
354
355 if (!ops->get_sset_count)
356 return -EOPNOTSUPP;
357
358 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
359 return -EFAULT;
360
361 ret = ops->get_sset_count(dev, gstrings.string_set);
362 if (ret < 0)
363 return ret;
364
365 gstrings.len = ret;
366
367 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
368 if (!data)
369 return -ENOMEM;
370
371 if (ops->get_rx_ntuple) {
372 /* driver-specific filter grab */
373 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
374 goto copy;
375 }
376
377 /* default ethtool filter grab */
378 i = 0;
379 p = (char *)data;
380 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
381 sprintf(p, "Filter %d:\n", i);
382 p += ETH_GSTRING_LEN;
383 num_strings++;
384
385 switch (fsc->fs.flow_type) {
386 case TCP_V4_FLOW:
387 sprintf(p, "\tFlow Type: TCP\n");
388 p += ETH_GSTRING_LEN;
389 num_strings++;
390 break;
391 case UDP_V4_FLOW:
392 sprintf(p, "\tFlow Type: UDP\n");
393 p += ETH_GSTRING_LEN;
394 num_strings++;
395 break;
396 case SCTP_V4_FLOW:
397 sprintf(p, "\tFlow Type: SCTP\n");
398 p += ETH_GSTRING_LEN;
399 num_strings++;
400 break;
401 case AH_ESP_V4_FLOW:
402 sprintf(p, "\tFlow Type: AH ESP\n");
403 p += ETH_GSTRING_LEN;
404 num_strings++;
405 break;
406 case ESP_V4_FLOW:
407 sprintf(p, "\tFlow Type: ESP\n");
408 p += ETH_GSTRING_LEN;
409 num_strings++;
410 break;
411 case IP_USER_FLOW:
412 sprintf(p, "\tFlow Type: Raw IP\n");
413 p += ETH_GSTRING_LEN;
414 num_strings++;
415 break;
416 case IPV4_FLOW:
417 sprintf(p, "\tFlow Type: IPv4\n");
418 p += ETH_GSTRING_LEN;
419 num_strings++;
420 break;
421 default:
422 sprintf(p, "\tFlow Type: Unknown\n");
423 p += ETH_GSTRING_LEN;
424 num_strings++;
425 goto unknown_filter;
426 };
427
428 /* now the rest of the filters */
429 switch (fsc->fs.flow_type) {
430 case TCP_V4_FLOW:
431 case UDP_V4_FLOW:
432 case SCTP_V4_FLOW:
433 sprintf(p, "\tSrc IP addr: 0x%x\n",
434 fsc->fs.h_u.tcp_ip4_spec.ip4src);
435 p += ETH_GSTRING_LEN;
436 num_strings++;
437 sprintf(p, "\tSrc IP mask: 0x%x\n",
438 fsc->fs.m_u.tcp_ip4_spec.ip4src);
439 p += ETH_GSTRING_LEN;
440 num_strings++;
441 sprintf(p, "\tDest IP addr: 0x%x\n",
442 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
443 p += ETH_GSTRING_LEN;
444 num_strings++;
445 sprintf(p, "\tDest IP mask: 0x%x\n",
446 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
447 p += ETH_GSTRING_LEN;
448 num_strings++;
449 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
450 fsc->fs.h_u.tcp_ip4_spec.psrc,
451 fsc->fs.m_u.tcp_ip4_spec.psrc);
452 p += ETH_GSTRING_LEN;
453 num_strings++;
454 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
455 fsc->fs.h_u.tcp_ip4_spec.pdst,
456 fsc->fs.m_u.tcp_ip4_spec.pdst);
457 p += ETH_GSTRING_LEN;
458 num_strings++;
459 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
460 fsc->fs.h_u.tcp_ip4_spec.tos,
461 fsc->fs.m_u.tcp_ip4_spec.tos);
462 p += ETH_GSTRING_LEN;
463 num_strings++;
464 break;
465 case AH_ESP_V4_FLOW:
466 case ESP_V4_FLOW:
467 sprintf(p, "\tSrc IP addr: 0x%x\n",
468 fsc->fs.h_u.ah_ip4_spec.ip4src);
469 p += ETH_GSTRING_LEN;
470 num_strings++;
471 sprintf(p, "\tSrc IP mask: 0x%x\n",
472 fsc->fs.m_u.ah_ip4_spec.ip4src);
473 p += ETH_GSTRING_LEN;
474 num_strings++;
475 sprintf(p, "\tDest IP addr: 0x%x\n",
476 fsc->fs.h_u.ah_ip4_spec.ip4dst);
477 p += ETH_GSTRING_LEN;
478 num_strings++;
479 sprintf(p, "\tDest IP mask: 0x%x\n",
480 fsc->fs.m_u.ah_ip4_spec.ip4dst);
481 p += ETH_GSTRING_LEN;
482 num_strings++;
483 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
484 fsc->fs.h_u.ah_ip4_spec.spi,
485 fsc->fs.m_u.ah_ip4_spec.spi);
486 p += ETH_GSTRING_LEN;
487 num_strings++;
488 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
489 fsc->fs.h_u.ah_ip4_spec.tos,
490 fsc->fs.m_u.ah_ip4_spec.tos);
491 p += ETH_GSTRING_LEN;
492 num_strings++;
493 break;
494 case IP_USER_FLOW:
495 sprintf(p, "\tSrc IP addr: 0x%x\n",
496 fsc->fs.h_u.raw_ip4_spec.ip4src);
497 p += ETH_GSTRING_LEN;
498 num_strings++;
499 sprintf(p, "\tSrc IP mask: 0x%x\n",
500 fsc->fs.m_u.raw_ip4_spec.ip4src);
501 p += ETH_GSTRING_LEN;
502 num_strings++;
503 sprintf(p, "\tDest IP addr: 0x%x\n",
504 fsc->fs.h_u.raw_ip4_spec.ip4dst);
505 p += ETH_GSTRING_LEN;
506 num_strings++;
507 sprintf(p, "\tDest IP mask: 0x%x\n",
508 fsc->fs.m_u.raw_ip4_spec.ip4dst);
509 p += ETH_GSTRING_LEN;
510 num_strings++;
511 break;
512 case IPV4_FLOW:
513 sprintf(p, "\tSrc IP addr: 0x%x\n",
514 fsc->fs.h_u.usr_ip4_spec.ip4src);
515 p += ETH_GSTRING_LEN;
516 num_strings++;
517 sprintf(p, "\tSrc IP mask: 0x%x\n",
518 fsc->fs.m_u.usr_ip4_spec.ip4src);
519 p += ETH_GSTRING_LEN;
520 num_strings++;
521 sprintf(p, "\tDest IP addr: 0x%x\n",
522 fsc->fs.h_u.usr_ip4_spec.ip4dst);
523 p += ETH_GSTRING_LEN;
524 num_strings++;
525 sprintf(p, "\tDest IP mask: 0x%x\n",
526 fsc->fs.m_u.usr_ip4_spec.ip4dst);
527 p += ETH_GSTRING_LEN;
528 num_strings++;
529 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
530 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
531 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
532 p += ETH_GSTRING_LEN;
533 num_strings++;
534 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
535 fsc->fs.h_u.usr_ip4_spec.tos,
536 fsc->fs.m_u.usr_ip4_spec.tos);
537 p += ETH_GSTRING_LEN;
538 num_strings++;
539 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
540 fsc->fs.h_u.usr_ip4_spec.ip_ver,
541 fsc->fs.m_u.usr_ip4_spec.ip_ver);
542 p += ETH_GSTRING_LEN;
543 num_strings++;
544 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
545 fsc->fs.h_u.usr_ip4_spec.proto,
546 fsc->fs.m_u.usr_ip4_spec.proto);
547 p += ETH_GSTRING_LEN;
548 num_strings++;
549 break;
550 };
551 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
552 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
553 p += ETH_GSTRING_LEN;
554 num_strings++;
555 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
556 p += ETH_GSTRING_LEN;
557 num_strings++;
558 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
559 p += ETH_GSTRING_LEN;
560 num_strings++;
561 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
562 sprintf(p, "\tAction: Drop\n");
563 else
564 sprintf(p, "\tAction: Direct to queue %d\n",
565 fsc->fs.action);
566 p += ETH_GSTRING_LEN;
567 num_strings++;
568unknown_filter:
569 i++;
570 }
571copy:
572 /* indicate to userspace how many strings we actually have */
573 gstrings.len = num_strings;
574 ret = -EFAULT;
575 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
576 goto out;
577 useraddr += sizeof(gstrings);
578 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
579 goto out;
580 ret = 0;
581
582out:
583 kfree(data);
584 return ret;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
588{
589 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700590 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 void *regbuf;
592 int reglen, ret;
593
594 if (!ops->get_regs || !ops->get_regs_len)
595 return -EOPNOTSUPP;
596
597 if (copy_from_user(&regs, useraddr, sizeof(regs)))
598 return -EFAULT;
599
600 reglen = ops->get_regs_len(dev);
601 if (regs.len > reglen)
602 regs.len = reglen;
603
604 regbuf = kmalloc(reglen, GFP_USER);
605 if (!regbuf)
606 return -ENOMEM;
607
608 ops->get_regs(dev, &regs, regbuf);
609
610 ret = -EFAULT;
611 if (copy_to_user(useraddr, &regs, sizeof(regs)))
612 goto out;
613 useraddr += offsetof(struct ethtool_regs, data);
614 if (copy_to_user(useraddr, regbuf, regs.len))
615 goto out;
616 ret = 0;
617
618 out:
619 kfree(regbuf);
620 return ret;
621}
622
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000623static int ethtool_reset(struct net_device *dev, char __user *useraddr)
624{
625 struct ethtool_value reset;
626 int ret;
627
628 if (!dev->ethtool_ops->reset)
629 return -EOPNOTSUPP;
630
631 if (copy_from_user(&reset, useraddr, sizeof(reset)))
632 return -EFAULT;
633
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800634 /* Clear ethtool n-tuple list */
635 ethtool_ntuple_flush(dev);
636
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000637 ret = dev->ethtool_ops->reset(dev, &reset.data);
638 if (ret)
639 return ret;
640
641 if (copy_to_user(useraddr, &reset, sizeof(reset)))
642 return -EFAULT;
643 return 0;
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
647{
Roland Dreier8e557422010-02-11 12:14:23 -0800648 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 if (!dev->ethtool_ops->get_wol)
651 return -EOPNOTSUPP;
652
653 dev->ethtool_ops->get_wol(dev, &wol);
654
655 if (copy_to_user(useraddr, &wol, sizeof(wol)))
656 return -EFAULT;
657 return 0;
658}
659
660static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
661{
662 struct ethtool_wolinfo wol;
663
664 if (!dev->ethtool_ops->set_wol)
665 return -EOPNOTSUPP;
666
667 if (copy_from_user(&wol, useraddr, sizeof(wol)))
668 return -EFAULT;
669
670 return dev->ethtool_ops->set_wol(dev, &wol);
671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static int ethtool_nway_reset(struct net_device *dev)
674{
675 if (!dev->ethtool_ops->nway_reset)
676 return -EOPNOTSUPP;
677
678 return dev->ethtool_ops->nway_reset(dev);
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
682{
683 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700684 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700685 void __user *userbuf = useraddr + sizeof(eeprom);
686 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700688 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (!ops->get_eeprom || !ops->get_eeprom_len)
691 return -EOPNOTSUPP;
692
693 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
694 return -EFAULT;
695
696 /* Check for wrap and zero */
697 if (eeprom.offset + eeprom.len <= eeprom.offset)
698 return -EINVAL;
699
700 /* Check for exceeding total eeprom len */
701 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
702 return -EINVAL;
703
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700704 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!data)
706 return -ENOMEM;
707
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700708 bytes_remaining = eeprom.len;
709 while (bytes_remaining > 0) {
710 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700712 ret = ops->get_eeprom(dev, &eeprom, data);
713 if (ret)
714 break;
715 if (copy_to_user(userbuf, data, eeprom.len)) {
716 ret = -EFAULT;
717 break;
718 }
719 userbuf += eeprom.len;
720 eeprom.offset += eeprom.len;
721 bytes_remaining -= eeprom.len;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700724 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
725 eeprom.offset -= eeprom.len;
726 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
727 ret = -EFAULT;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree(data);
730 return ret;
731}
732
733static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
734{
735 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700736 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700737 void __user *userbuf = useraddr + sizeof(eeprom);
738 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700740 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (!ops->set_eeprom || !ops->get_eeprom_len)
743 return -EOPNOTSUPP;
744
745 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
746 return -EFAULT;
747
748 /* Check for wrap and zero */
749 if (eeprom.offset + eeprom.len <= eeprom.offset)
750 return -EINVAL;
751
752 /* Check for exceeding total eeprom len */
753 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
754 return -EINVAL;
755
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700756 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!data)
758 return -ENOMEM;
759
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700760 bytes_remaining = eeprom.len;
761 while (bytes_remaining > 0) {
762 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700764 if (copy_from_user(data, userbuf, eeprom.len)) {
765 ret = -EFAULT;
766 break;
767 }
768 ret = ops->set_eeprom(dev, &eeprom, data);
769 if (ret)
770 break;
771 userbuf += eeprom.len;
772 eeprom.offset += eeprom.len;
773 bytes_remaining -= eeprom.len;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 kfree(data);
777 return ret;
778}
779
780static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
781{
Roland Dreier8e557422010-02-11 12:14:23 -0800782 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (!dev->ethtool_ops->get_coalesce)
785 return -EOPNOTSUPP;
786
787 dev->ethtool_ops->get_coalesce(dev, &coalesce);
788
789 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
790 return -EFAULT;
791 return 0;
792}
793
794static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
795{
796 struct ethtool_coalesce coalesce;
797
David S. Millerfa04ae52005-06-06 15:07:19 -0700798 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -EOPNOTSUPP;
800
801 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
802 return -EFAULT;
803
804 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
805}
806
807static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
808{
Roland Dreier8e557422010-02-11 12:14:23 -0800809 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (!dev->ethtool_ops->get_ringparam)
812 return -EOPNOTSUPP;
813
814 dev->ethtool_ops->get_ringparam(dev, &ringparam);
815
816 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
817 return -EFAULT;
818 return 0;
819}
820
821static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
822{
823 struct ethtool_ringparam ringparam;
824
825 if (!dev->ethtool_ops->set_ringparam)
826 return -EOPNOTSUPP;
827
828 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
829 return -EFAULT;
830
831 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
832}
833
834static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
835{
836 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
837
838 if (!dev->ethtool_ops->get_pauseparam)
839 return -EOPNOTSUPP;
840
841 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
842
843 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
844 return -EFAULT;
845 return 0;
846}
847
848static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
849{
850 struct ethtool_pauseparam pauseparam;
851
Jeff Garzike1b90c42006-07-17 12:54:40 -0400852 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -EOPNOTSUPP;
854
855 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
856 return -EFAULT;
857
858 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861static int __ethtool_set_sg(struct net_device *dev, u32 data)
862{
863 int err;
864
865 if (!data && dev->ethtool_ops->set_tso) {
866 err = dev->ethtool_ops->set_tso(dev, 0);
867 if (err)
868 return err;
869 }
870
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700871 if (!data && dev->ethtool_ops->set_ufo) {
872 err = dev->ethtool_ops->set_ufo(dev, 0);
873 if (err)
874 return err;
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return dev->ethtool_ops->set_sg(dev, data);
877}
878
879static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
880{
881 struct ethtool_value edata;
882 int err;
883
884 if (!dev->ethtool_ops->set_tx_csum)
885 return -EOPNOTSUPP;
886
887 if (copy_from_user(&edata, useraddr, sizeof(edata)))
888 return -EFAULT;
889
890 if (!edata.data && dev->ethtool_ops->set_sg) {
891 err = __ethtool_set_sg(dev, 0);
892 if (err)
893 return err;
894 }
895
896 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
897}
898
Herbert Xub240a0e2008-12-15 23:44:31 -0800899static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
900{
901 struct ethtool_value edata;
902
903 if (!dev->ethtool_ops->set_rx_csum)
904 return -EOPNOTSUPP;
905
906 if (copy_from_user(&edata, useraddr, sizeof(edata)))
907 return -EFAULT;
908
909 if (!edata.data && dev->ethtool_ops->set_sg)
910 dev->features &= ~NETIF_F_GRO;
911
912 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
916{
917 struct ethtool_value edata;
918
919 if (!dev->ethtool_ops->set_sg)
920 return -EOPNOTSUPP;
921
922 if (copy_from_user(&edata, useraddr, sizeof(edata)))
923 return -EFAULT;
924
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900925 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -0700926 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -EINVAL;
928
929 return __ethtool_set_sg(dev, edata.data);
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
933{
934 struct ethtool_value edata;
935
936 if (!dev->ethtool_ops->set_tso)
937 return -EOPNOTSUPP;
938
939 if (copy_from_user(&edata, useraddr, sizeof(edata)))
940 return -EFAULT;
941
942 if (edata.data && !(dev->features & NETIF_F_SG))
943 return -EINVAL;
944
945 return dev->ethtool_ops->set_tso(dev, edata.data);
946}
947
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700948static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
949{
950 struct ethtool_value edata;
951
952 if (!dev->ethtool_ops->set_ufo)
953 return -EOPNOTSUPP;
954 if (copy_from_user(&edata, useraddr, sizeof(edata)))
955 return -EFAULT;
956 if (edata.data && !(dev->features & NETIF_F_SG))
957 return -EINVAL;
958 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
959 return -EINVAL;
960 return dev->ethtool_ops->set_ufo(dev, edata.data);
961}
962
Herbert Xu37c31852006-06-22 03:07:29 -0700963static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
964{
965 struct ethtool_value edata = { ETHTOOL_GGSO };
966
967 edata.data = dev->features & NETIF_F_GSO;
968 if (copy_to_user(useraddr, &edata, sizeof(edata)))
969 return -EFAULT;
970 return 0;
971}
972
973static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
974{
975 struct ethtool_value edata;
976
977 if (copy_from_user(&edata, useraddr, sizeof(edata)))
978 return -EFAULT;
979 if (edata.data)
980 dev->features |= NETIF_F_GSO;
981 else
982 dev->features &= ~NETIF_F_GSO;
983 return 0;
984}
985
Herbert Xub240a0e2008-12-15 23:44:31 -0800986static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
987{
988 struct ethtool_value edata = { ETHTOOL_GGRO };
989
990 edata.data = dev->features & NETIF_F_GRO;
991 if (copy_to_user(useraddr, &edata, sizeof(edata)))
992 return -EFAULT;
993 return 0;
994}
995
996static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
997{
998 struct ethtool_value edata;
999
1000 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1001 return -EFAULT;
1002
1003 if (edata.data) {
1004 if (!dev->ethtool_ops->get_rx_csum ||
1005 !dev->ethtool_ops->get_rx_csum(dev))
1006 return -EINVAL;
1007 dev->features |= NETIF_F_GRO;
1008 } else
1009 dev->features &= ~NETIF_F_GRO;
1010
1011 return 0;
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1015{
1016 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001017 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001019 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001021 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001022 return -EOPNOTSUPP;
1023
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001024 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001025 if (test_len < 0)
1026 return test_len;
1027 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (copy_from_user(&test, useraddr, sizeof(test)))
1030 return -EFAULT;
1031
Jeff Garzikff03d492007-08-15 16:01:08 -07001032 test.len = test_len;
1033 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!data)
1035 return -ENOMEM;
1036
1037 ops->self_test(dev, &test, data);
1038
1039 ret = -EFAULT;
1040 if (copy_to_user(useraddr, &test, sizeof(test)))
1041 goto out;
1042 useraddr += sizeof(test);
1043 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1044 goto out;
1045 ret = 0;
1046
1047 out:
1048 kfree(data);
1049 return ret;
1050}
1051
1052static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1053{
1054 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001055 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 u8 *data;
1057 int ret;
1058
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001059 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -EOPNOTSUPP;
1061
1062 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1063 return -EFAULT;
1064
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001065 ret = ops->get_sset_count(dev, gstrings.string_set);
1066 if (ret < 0)
1067 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001068
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001069 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1072 if (!data)
1073 return -ENOMEM;
1074
1075 ops->get_strings(dev, gstrings.string_set, data);
1076
1077 ret = -EFAULT;
1078 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1079 goto out;
1080 useraddr += sizeof(gstrings);
1081 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1082 goto out;
1083 ret = 0;
1084
1085 out:
1086 kfree(data);
1087 return ret;
1088}
1089
1090static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1091{
1092 struct ethtool_value id;
1093
1094 if (!dev->ethtool_ops->phys_id)
1095 return -EOPNOTSUPP;
1096
1097 if (copy_from_user(&id, useraddr, sizeof(id)))
1098 return -EFAULT;
1099
1100 return dev->ethtool_ops->phys_id(dev, id.data);
1101}
1102
1103static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1104{
1105 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001106 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001108 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001110 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001111 return -EOPNOTSUPP;
1112
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001113 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001114 if (n_stats < 0)
1115 return n_stats;
1116 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1119 return -EFAULT;
1120
Jeff Garzikff03d492007-08-15 16:01:08 -07001121 stats.n_stats = n_stats;
1122 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!data)
1124 return -ENOMEM;
1125
1126 ops->get_ethtool_stats(dev, &stats, data);
1127
1128 ret = -EFAULT;
1129 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1130 goto out;
1131 useraddr += sizeof(stats);
1132 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1133 goto out;
1134 ret = 0;
1135
1136 out:
1137 kfree(data);
1138 return ret;
1139}
1140
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001141static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001142{
1143 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001144
Matthew Wilcox313674a2007-07-31 14:00:29 -07001145 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001146 return -EFAULT;
1147
Matthew Wilcox313674a2007-07-31 14:00:29 -07001148 if (epaddr.size < dev->addr_len)
1149 return -ETOOSMALL;
1150 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001151
Jon Wetzela6f9a702005-08-20 17:15:54 -07001152 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001153 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001154 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001155 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1156 return -EFAULT;
1157 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001158}
1159
Jeff Garzik13c99b22007-08-15 16:01:56 -07001160static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1161 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001162{
Roland Dreier8e557422010-02-11 12:14:23 -08001163 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001164
Jeff Garzik13c99b22007-08-15 16:01:56 -07001165 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001166 return -EOPNOTSUPP;
1167
Jeff Garzik13c99b22007-08-15 16:01:56 -07001168 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001169
1170 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1171 return -EFAULT;
1172 return 0;
1173}
1174
Jeff Garzik13c99b22007-08-15 16:01:56 -07001175static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1176 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001177{
1178 struct ethtool_value edata;
1179
Jeff Garzik13c99b22007-08-15 16:01:56 -07001180 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001181 return -EOPNOTSUPP;
1182
1183 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1184 return -EFAULT;
1185
Jeff Garzik13c99b22007-08-15 16:01:56 -07001186 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001187 return 0;
1188}
1189
Jeff Garzik13c99b22007-08-15 16:01:56 -07001190static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1191 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001192{
1193 struct ethtool_value edata;
1194
Jeff Garzik13c99b22007-08-15 16:01:56 -07001195 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001196 return -EOPNOTSUPP;
1197
1198 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1199 return -EFAULT;
1200
Jeff Garzik13c99b22007-08-15 16:01:56 -07001201 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001202}
1203
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001204static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1205{
1206 struct ethtool_flash efl;
1207
1208 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1209 return -EFAULT;
1210
1211 if (!dev->ethtool_ops->flash_device)
1212 return -EOPNOTSUPP;
1213
1214 return dev->ethtool_ops->flash_device(dev, &efl);
1215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/* The main entry point in this file. Called from net/core/dev.c */
1218
Eric W. Biederman881d9662007-09-17 11:56:21 -07001219int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001221 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 void __user *useraddr = ifr->ifr_data;
1223 u32 ethcmd;
1224 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -07001225 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!dev || !netif_device_present(dev))
1228 return -ENODEV;
1229
1230 if (!dev->ethtool_ops)
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001231 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
1234 return -EFAULT;
1235
Stephen Hemminger75f31232006-09-28 15:13:37 -07001236 /* Allow some commands to be done by anyone */
1237 switch(ethcmd) {
Stephen Hemminger75f31232006-09-28 15:13:37 -07001238 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001239 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001240 case ETHTOOL_GCOALESCE:
1241 case ETHTOOL_GRINGPARAM:
1242 case ETHTOOL_GPAUSEPARAM:
1243 case ETHTOOL_GRXCSUM:
1244 case ETHTOOL_GTXCSUM:
1245 case ETHTOOL_GSG:
1246 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001247 case ETHTOOL_GTSO:
1248 case ETHTOOL_GPERMADDR:
1249 case ETHTOOL_GUFO:
1250 case ETHTOOL_GGSO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001251 case ETHTOOL_GFLAGS:
1252 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001253 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001254 case ETHTOOL_GRXRINGS:
1255 case ETHTOOL_GRXCLSRLCNT:
1256 case ETHTOOL_GRXCLSRULE:
1257 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001258 break;
1259 default:
1260 if (!capable(CAP_NET_ADMIN))
1261 return -EPERM;
1262 }
1263
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001264 if (dev->ethtool_ops->begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1266 return rc;
1267
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001268 old_features = dev->features;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 switch (ethcmd) {
1271 case ETHTOOL_GSET:
1272 rc = ethtool_get_settings(dev, useraddr);
1273 break;
1274 case ETHTOOL_SSET:
1275 rc = ethtool_set_settings(dev, useraddr);
1276 break;
1277 case ETHTOOL_GDRVINFO:
1278 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 case ETHTOOL_GREGS:
1281 rc = ethtool_get_regs(dev, useraddr);
1282 break;
1283 case ETHTOOL_GWOL:
1284 rc = ethtool_get_wol(dev, useraddr);
1285 break;
1286 case ETHTOOL_SWOL:
1287 rc = ethtool_set_wol(dev, useraddr);
1288 break;
1289 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001290 rc = ethtool_get_value(dev, useraddr, ethcmd,
1291 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 break;
1293 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001294 rc = ethtool_set_value_void(dev, useraddr,
1295 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 break;
1297 case ETHTOOL_NWAY_RST:
1298 rc = ethtool_nway_reset(dev);
1299 break;
1300 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001301 rc = ethtool_get_value(dev, useraddr, ethcmd,
1302 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 case ETHTOOL_GEEPROM:
1305 rc = ethtool_get_eeprom(dev, useraddr);
1306 break;
1307 case ETHTOOL_SEEPROM:
1308 rc = ethtool_set_eeprom(dev, useraddr);
1309 break;
1310 case ETHTOOL_GCOALESCE:
1311 rc = ethtool_get_coalesce(dev, useraddr);
1312 break;
1313 case ETHTOOL_SCOALESCE:
1314 rc = ethtool_set_coalesce(dev, useraddr);
1315 break;
1316 case ETHTOOL_GRINGPARAM:
1317 rc = ethtool_get_ringparam(dev, useraddr);
1318 break;
1319 case ETHTOOL_SRINGPARAM:
1320 rc = ethtool_set_ringparam(dev, useraddr);
1321 break;
1322 case ETHTOOL_GPAUSEPARAM:
1323 rc = ethtool_get_pauseparam(dev, useraddr);
1324 break;
1325 case ETHTOOL_SPAUSEPARAM:
1326 rc = ethtool_set_pauseparam(dev, useraddr);
1327 break;
1328 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001329 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001330 (dev->ethtool_ops->get_rx_csum ?
1331 dev->ethtool_ops->get_rx_csum :
1332 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 break;
1334 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001335 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
1337 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001338 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001339 (dev->ethtool_ops->get_tx_csum ?
1340 dev->ethtool_ops->get_tx_csum :
1341 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
1343 case ETHTOOL_STXCSUM:
1344 rc = ethtool_set_tx_csum(dev, useraddr);
1345 break;
1346 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001347 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001348 (dev->ethtool_ops->get_sg ?
1349 dev->ethtool_ops->get_sg :
1350 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
1352 case ETHTOOL_SSG:
1353 rc = ethtool_set_sg(dev, useraddr);
1354 break;
1355 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001356 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001357 (dev->ethtool_ops->get_tso ?
1358 dev->ethtool_ops->get_tso :
1359 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 case ETHTOOL_STSO:
1362 rc = ethtool_set_tso(dev, useraddr);
1363 break;
1364 case ETHTOOL_TEST:
1365 rc = ethtool_self_test(dev, useraddr);
1366 break;
1367 case ETHTOOL_GSTRINGS:
1368 rc = ethtool_get_strings(dev, useraddr);
1369 break;
1370 case ETHTOOL_PHYS_ID:
1371 rc = ethtool_phys_id(dev, useraddr);
1372 break;
1373 case ETHTOOL_GSTATS:
1374 rc = ethtool_get_stats(dev, useraddr);
1375 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001376 case ETHTOOL_GPERMADDR:
1377 rc = ethtool_get_perm_addr(dev, useraddr);
1378 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001379 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001380 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001381 (dev->ethtool_ops->get_ufo ?
1382 dev->ethtool_ops->get_ufo :
1383 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001384 break;
1385 case ETHTOOL_SUFO:
1386 rc = ethtool_set_ufo(dev, useraddr);
1387 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001388 case ETHTOOL_GGSO:
1389 rc = ethtool_get_gso(dev, useraddr);
1390 break;
1391 case ETHTOOL_SGSO:
1392 rc = ethtool_set_gso(dev, useraddr);
1393 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001394 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001395 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001396 (dev->ethtool_ops->get_flags ?
1397 dev->ethtool_ops->get_flags :
1398 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001399 break;
1400 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001401 rc = ethtool_set_value(dev, useraddr,
1402 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001403 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001404 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001405 rc = ethtool_get_value(dev, useraddr, ethcmd,
1406 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001407 break;
1408 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001409 rc = ethtool_set_value(dev, useraddr,
1410 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001411 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001412 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001413 case ETHTOOL_GRXRINGS:
1414 case ETHTOOL_GRXCLSRLCNT:
1415 case ETHTOOL_GRXCLSRULE:
1416 case ETHTOOL_GRXCLSRLALL:
1417 rc = ethtool_get_rxnfc(dev, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001418 break;
1419 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001420 case ETHTOOL_SRXCLSRLDEL:
1421 case ETHTOOL_SRXCLSRLINS:
1422 rc = ethtool_set_rxnfc(dev, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001423 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001424 case ETHTOOL_GGRO:
1425 rc = ethtool_get_gro(dev, useraddr);
1426 break;
1427 case ETHTOOL_SGRO:
1428 rc = ethtool_set_gro(dev, useraddr);
1429 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001430 case ETHTOOL_FLASHDEV:
1431 rc = ethtool_flash_device(dev, useraddr);
1432 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001433 case ETHTOOL_RESET:
1434 rc = ethtool_reset(dev, useraddr);
1435 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001436 case ETHTOOL_SRXNTUPLE:
1437 rc = ethtool_set_rx_ntuple(dev, useraddr);
1438 break;
1439 case ETHTOOL_GRXNTUPLE:
1440 rc = ethtool_get_rx_ntuple(dev, useraddr);
1441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001443 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001445
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001446 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001448
1449 if (old_features != dev->features)
1450 netdev_features_change(dev);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455EXPORT_SYMBOL(ethtool_op_get_link);
1456EXPORT_SYMBOL(ethtool_op_get_sg);
1457EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458EXPORT_SYMBOL(ethtool_op_set_sg);
1459EXPORT_SYMBOL(ethtool_op_set_tso);
1460EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Jon Mason69f6a0f2005-05-29 20:27:24 -07001461EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -07001462EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001463EXPORT_SYMBOL(ethtool_op_set_ufo);
1464EXPORT_SYMBOL(ethtool_op_get_ufo);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001465EXPORT_SYMBOL(ethtool_op_set_flags);
1466EXPORT_SYMBOL(ethtool_op_get_flags);