blob: fbbe4b49116b959cc94ab903a3d63fd921cbfaf0 [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 Waskiewicze8589112010-02-12 13:48:05 +0000286static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
287 struct ethtool_rx_ntuple_flow_spec *spec,
288 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800289{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800290
291 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000292 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
293 /* free the container */
294 kfree(fsc);
295 return;
296 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800297
298 /* Copy the whole filter over */
299 fsc->fs.flow_type = spec->flow_type;
300 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
301 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
302
303 fsc->fs.vlan_tag = spec->vlan_tag;
304 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
305 fsc->fs.data = spec->data;
306 fsc->fs.data_mask = spec->data_mask;
307 fsc->fs.action = spec->action;
308
309 /* add to the list */
310 list_add_tail_rcu(&fsc->list, &list->list);
311 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800312}
313
314static int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr)
315{
316 struct ethtool_rx_ntuple cmd;
317 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000318 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800319 int ret;
320
321 if (!ops->set_rx_ntuple)
322 return -EOPNOTSUPP;
323
324 if (!(dev->features & NETIF_F_NTUPLE))
325 return -EINVAL;
326
327 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
328 return -EFAULT;
329
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800330 /*
331 * Cache filter in dev struct for GET operation only if
332 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000333 * only if the filter was added successfully. First make sure we
334 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800335 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000336 if (!ops->get_rx_ntuple) {
337 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
338 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800339 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000340 }
341
342 ret = ops->set_rx_ntuple(dev, &cmd);
343 if (ret) {
344 kfree(fsc);
345 return ret;
346 }
347
348 if (!ops->get_rx_ntuple)
349 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800350
351 return ret;
352}
353
354static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
355{
356 struct ethtool_gstrings gstrings;
357 const struct ethtool_ops *ops = dev->ethtool_ops;
358 struct ethtool_rx_ntuple_flow_spec_container *fsc;
359 u8 *data;
360 char *p;
361 int ret, i, num_strings = 0;
362
363 if (!ops->get_sset_count)
364 return -EOPNOTSUPP;
365
366 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
367 return -EFAULT;
368
369 ret = ops->get_sset_count(dev, gstrings.string_set);
370 if (ret < 0)
371 return ret;
372
373 gstrings.len = ret;
374
375 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
376 if (!data)
377 return -ENOMEM;
378
379 if (ops->get_rx_ntuple) {
380 /* driver-specific filter grab */
381 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
382 goto copy;
383 }
384
385 /* default ethtool filter grab */
386 i = 0;
387 p = (char *)data;
388 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
389 sprintf(p, "Filter %d:\n", i);
390 p += ETH_GSTRING_LEN;
391 num_strings++;
392
393 switch (fsc->fs.flow_type) {
394 case TCP_V4_FLOW:
395 sprintf(p, "\tFlow Type: TCP\n");
396 p += ETH_GSTRING_LEN;
397 num_strings++;
398 break;
399 case UDP_V4_FLOW:
400 sprintf(p, "\tFlow Type: UDP\n");
401 p += ETH_GSTRING_LEN;
402 num_strings++;
403 break;
404 case SCTP_V4_FLOW:
405 sprintf(p, "\tFlow Type: SCTP\n");
406 p += ETH_GSTRING_LEN;
407 num_strings++;
408 break;
409 case AH_ESP_V4_FLOW:
410 sprintf(p, "\tFlow Type: AH ESP\n");
411 p += ETH_GSTRING_LEN;
412 num_strings++;
413 break;
414 case ESP_V4_FLOW:
415 sprintf(p, "\tFlow Type: ESP\n");
416 p += ETH_GSTRING_LEN;
417 num_strings++;
418 break;
419 case IP_USER_FLOW:
420 sprintf(p, "\tFlow Type: Raw IP\n");
421 p += ETH_GSTRING_LEN;
422 num_strings++;
423 break;
424 case IPV4_FLOW:
425 sprintf(p, "\tFlow Type: IPv4\n");
426 p += ETH_GSTRING_LEN;
427 num_strings++;
428 break;
429 default:
430 sprintf(p, "\tFlow Type: Unknown\n");
431 p += ETH_GSTRING_LEN;
432 num_strings++;
433 goto unknown_filter;
434 };
435
436 /* now the rest of the filters */
437 switch (fsc->fs.flow_type) {
438 case TCP_V4_FLOW:
439 case UDP_V4_FLOW:
440 case SCTP_V4_FLOW:
441 sprintf(p, "\tSrc IP addr: 0x%x\n",
442 fsc->fs.h_u.tcp_ip4_spec.ip4src);
443 p += ETH_GSTRING_LEN;
444 num_strings++;
445 sprintf(p, "\tSrc IP mask: 0x%x\n",
446 fsc->fs.m_u.tcp_ip4_spec.ip4src);
447 p += ETH_GSTRING_LEN;
448 num_strings++;
449 sprintf(p, "\tDest IP addr: 0x%x\n",
450 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
451 p += ETH_GSTRING_LEN;
452 num_strings++;
453 sprintf(p, "\tDest IP mask: 0x%x\n",
454 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
455 p += ETH_GSTRING_LEN;
456 num_strings++;
457 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
458 fsc->fs.h_u.tcp_ip4_spec.psrc,
459 fsc->fs.m_u.tcp_ip4_spec.psrc);
460 p += ETH_GSTRING_LEN;
461 num_strings++;
462 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
463 fsc->fs.h_u.tcp_ip4_spec.pdst,
464 fsc->fs.m_u.tcp_ip4_spec.pdst);
465 p += ETH_GSTRING_LEN;
466 num_strings++;
467 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
468 fsc->fs.h_u.tcp_ip4_spec.tos,
469 fsc->fs.m_u.tcp_ip4_spec.tos);
470 p += ETH_GSTRING_LEN;
471 num_strings++;
472 break;
473 case AH_ESP_V4_FLOW:
474 case ESP_V4_FLOW:
475 sprintf(p, "\tSrc IP addr: 0x%x\n",
476 fsc->fs.h_u.ah_ip4_spec.ip4src);
477 p += ETH_GSTRING_LEN;
478 num_strings++;
479 sprintf(p, "\tSrc IP mask: 0x%x\n",
480 fsc->fs.m_u.ah_ip4_spec.ip4src);
481 p += ETH_GSTRING_LEN;
482 num_strings++;
483 sprintf(p, "\tDest IP addr: 0x%x\n",
484 fsc->fs.h_u.ah_ip4_spec.ip4dst);
485 p += ETH_GSTRING_LEN;
486 num_strings++;
487 sprintf(p, "\tDest IP mask: 0x%x\n",
488 fsc->fs.m_u.ah_ip4_spec.ip4dst);
489 p += ETH_GSTRING_LEN;
490 num_strings++;
491 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
492 fsc->fs.h_u.ah_ip4_spec.spi,
493 fsc->fs.m_u.ah_ip4_spec.spi);
494 p += ETH_GSTRING_LEN;
495 num_strings++;
496 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
497 fsc->fs.h_u.ah_ip4_spec.tos,
498 fsc->fs.m_u.ah_ip4_spec.tos);
499 p += ETH_GSTRING_LEN;
500 num_strings++;
501 break;
502 case IP_USER_FLOW:
503 sprintf(p, "\tSrc IP addr: 0x%x\n",
504 fsc->fs.h_u.raw_ip4_spec.ip4src);
505 p += ETH_GSTRING_LEN;
506 num_strings++;
507 sprintf(p, "\tSrc IP mask: 0x%x\n",
508 fsc->fs.m_u.raw_ip4_spec.ip4src);
509 p += ETH_GSTRING_LEN;
510 num_strings++;
511 sprintf(p, "\tDest IP addr: 0x%x\n",
512 fsc->fs.h_u.raw_ip4_spec.ip4dst);
513 p += ETH_GSTRING_LEN;
514 num_strings++;
515 sprintf(p, "\tDest IP mask: 0x%x\n",
516 fsc->fs.m_u.raw_ip4_spec.ip4dst);
517 p += ETH_GSTRING_LEN;
518 num_strings++;
519 break;
520 case IPV4_FLOW:
521 sprintf(p, "\tSrc IP addr: 0x%x\n",
522 fsc->fs.h_u.usr_ip4_spec.ip4src);
523 p += ETH_GSTRING_LEN;
524 num_strings++;
525 sprintf(p, "\tSrc IP mask: 0x%x\n",
526 fsc->fs.m_u.usr_ip4_spec.ip4src);
527 p += ETH_GSTRING_LEN;
528 num_strings++;
529 sprintf(p, "\tDest IP addr: 0x%x\n",
530 fsc->fs.h_u.usr_ip4_spec.ip4dst);
531 p += ETH_GSTRING_LEN;
532 num_strings++;
533 sprintf(p, "\tDest IP mask: 0x%x\n",
534 fsc->fs.m_u.usr_ip4_spec.ip4dst);
535 p += ETH_GSTRING_LEN;
536 num_strings++;
537 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
538 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
539 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
540 p += ETH_GSTRING_LEN;
541 num_strings++;
542 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
543 fsc->fs.h_u.usr_ip4_spec.tos,
544 fsc->fs.m_u.usr_ip4_spec.tos);
545 p += ETH_GSTRING_LEN;
546 num_strings++;
547 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
548 fsc->fs.h_u.usr_ip4_spec.ip_ver,
549 fsc->fs.m_u.usr_ip4_spec.ip_ver);
550 p += ETH_GSTRING_LEN;
551 num_strings++;
552 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
553 fsc->fs.h_u.usr_ip4_spec.proto,
554 fsc->fs.m_u.usr_ip4_spec.proto);
555 p += ETH_GSTRING_LEN;
556 num_strings++;
557 break;
558 };
559 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
560 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
561 p += ETH_GSTRING_LEN;
562 num_strings++;
563 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
564 p += ETH_GSTRING_LEN;
565 num_strings++;
566 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
567 p += ETH_GSTRING_LEN;
568 num_strings++;
569 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
570 sprintf(p, "\tAction: Drop\n");
571 else
572 sprintf(p, "\tAction: Direct to queue %d\n",
573 fsc->fs.action);
574 p += ETH_GSTRING_LEN;
575 num_strings++;
576unknown_filter:
577 i++;
578 }
579copy:
580 /* indicate to userspace how many strings we actually have */
581 gstrings.len = num_strings;
582 ret = -EFAULT;
583 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
584 goto out;
585 useraddr += sizeof(gstrings);
586 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
587 goto out;
588 ret = 0;
589
590out:
591 kfree(data);
592 return ret;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
596{
597 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700598 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 void *regbuf;
600 int reglen, ret;
601
602 if (!ops->get_regs || !ops->get_regs_len)
603 return -EOPNOTSUPP;
604
605 if (copy_from_user(&regs, useraddr, sizeof(regs)))
606 return -EFAULT;
607
608 reglen = ops->get_regs_len(dev);
609 if (regs.len > reglen)
610 regs.len = reglen;
611
612 regbuf = kmalloc(reglen, GFP_USER);
613 if (!regbuf)
614 return -ENOMEM;
615
616 ops->get_regs(dev, &regs, regbuf);
617
618 ret = -EFAULT;
619 if (copy_to_user(useraddr, &regs, sizeof(regs)))
620 goto out;
621 useraddr += offsetof(struct ethtool_regs, data);
622 if (copy_to_user(useraddr, regbuf, regs.len))
623 goto out;
624 ret = 0;
625
626 out:
627 kfree(regbuf);
628 return ret;
629}
630
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000631static int ethtool_reset(struct net_device *dev, char __user *useraddr)
632{
633 struct ethtool_value reset;
634 int ret;
635
636 if (!dev->ethtool_ops->reset)
637 return -EOPNOTSUPP;
638
639 if (copy_from_user(&reset, useraddr, sizeof(reset)))
640 return -EFAULT;
641
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800642 /* Clear ethtool n-tuple list */
643 ethtool_ntuple_flush(dev);
644
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000645 ret = dev->ethtool_ops->reset(dev, &reset.data);
646 if (ret)
647 return ret;
648
649 if (copy_to_user(useraddr, &reset, sizeof(reset)))
650 return -EFAULT;
651 return 0;
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
655{
Roland Dreier8e557422010-02-11 12:14:23 -0800656 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!dev->ethtool_ops->get_wol)
659 return -EOPNOTSUPP;
660
661 dev->ethtool_ops->get_wol(dev, &wol);
662
663 if (copy_to_user(useraddr, &wol, sizeof(wol)))
664 return -EFAULT;
665 return 0;
666}
667
668static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
669{
670 struct ethtool_wolinfo wol;
671
672 if (!dev->ethtool_ops->set_wol)
673 return -EOPNOTSUPP;
674
675 if (copy_from_user(&wol, useraddr, sizeof(wol)))
676 return -EFAULT;
677
678 return dev->ethtool_ops->set_wol(dev, &wol);
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static int ethtool_nway_reset(struct net_device *dev)
682{
683 if (!dev->ethtool_ops->nway_reset)
684 return -EOPNOTSUPP;
685
686 return dev->ethtool_ops->nway_reset(dev);
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
690{
691 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700692 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700693 void __user *userbuf = useraddr + sizeof(eeprom);
694 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700696 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (!ops->get_eeprom || !ops->get_eeprom_len)
699 return -EOPNOTSUPP;
700
701 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
702 return -EFAULT;
703
704 /* Check for wrap and zero */
705 if (eeprom.offset + eeprom.len <= eeprom.offset)
706 return -EINVAL;
707
708 /* Check for exceeding total eeprom len */
709 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
710 return -EINVAL;
711
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700712 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!data)
714 return -ENOMEM;
715
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700716 bytes_remaining = eeprom.len;
717 while (bytes_remaining > 0) {
718 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700720 ret = ops->get_eeprom(dev, &eeprom, data);
721 if (ret)
722 break;
723 if (copy_to_user(userbuf, data, eeprom.len)) {
724 ret = -EFAULT;
725 break;
726 }
727 userbuf += eeprom.len;
728 eeprom.offset += eeprom.len;
729 bytes_remaining -= eeprom.len;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700732 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
733 eeprom.offset -= eeprom.len;
734 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
735 ret = -EFAULT;
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 kfree(data);
738 return ret;
739}
740
741static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
742{
743 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700744 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700745 void __user *userbuf = useraddr + sizeof(eeprom);
746 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700748 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (!ops->set_eeprom || !ops->get_eeprom_len)
751 return -EOPNOTSUPP;
752
753 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
754 return -EFAULT;
755
756 /* Check for wrap and zero */
757 if (eeprom.offset + eeprom.len <= eeprom.offset)
758 return -EINVAL;
759
760 /* Check for exceeding total eeprom len */
761 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
762 return -EINVAL;
763
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700764 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!data)
766 return -ENOMEM;
767
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700768 bytes_remaining = eeprom.len;
769 while (bytes_remaining > 0) {
770 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700772 if (copy_from_user(data, userbuf, eeprom.len)) {
773 ret = -EFAULT;
774 break;
775 }
776 ret = ops->set_eeprom(dev, &eeprom, data);
777 if (ret)
778 break;
779 userbuf += eeprom.len;
780 eeprom.offset += eeprom.len;
781 bytes_remaining -= eeprom.len;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 kfree(data);
785 return ret;
786}
787
788static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
789{
Roland Dreier8e557422010-02-11 12:14:23 -0800790 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (!dev->ethtool_ops->get_coalesce)
793 return -EOPNOTSUPP;
794
795 dev->ethtool_ops->get_coalesce(dev, &coalesce);
796
797 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
798 return -EFAULT;
799 return 0;
800}
801
802static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
803{
804 struct ethtool_coalesce coalesce;
805
David S. Millerfa04ae52005-06-06 15:07:19 -0700806 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -EOPNOTSUPP;
808
809 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
810 return -EFAULT;
811
812 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
813}
814
815static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
816{
Roland Dreier8e557422010-02-11 12:14:23 -0800817 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (!dev->ethtool_ops->get_ringparam)
820 return -EOPNOTSUPP;
821
822 dev->ethtool_ops->get_ringparam(dev, &ringparam);
823
824 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
825 return -EFAULT;
826 return 0;
827}
828
829static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
830{
831 struct ethtool_ringparam ringparam;
832
833 if (!dev->ethtool_ops->set_ringparam)
834 return -EOPNOTSUPP;
835
836 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
837 return -EFAULT;
838
839 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
840}
841
842static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
843{
844 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
845
846 if (!dev->ethtool_ops->get_pauseparam)
847 return -EOPNOTSUPP;
848
849 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
850
851 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
852 return -EFAULT;
853 return 0;
854}
855
856static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
857{
858 struct ethtool_pauseparam pauseparam;
859
Jeff Garzike1b90c42006-07-17 12:54:40 -0400860 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EOPNOTSUPP;
862
863 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
864 return -EFAULT;
865
866 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static int __ethtool_set_sg(struct net_device *dev, u32 data)
870{
871 int err;
872
873 if (!data && dev->ethtool_ops->set_tso) {
874 err = dev->ethtool_ops->set_tso(dev, 0);
875 if (err)
876 return err;
877 }
878
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700879 if (!data && dev->ethtool_ops->set_ufo) {
880 err = dev->ethtool_ops->set_ufo(dev, 0);
881 if (err)
882 return err;
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return dev->ethtool_ops->set_sg(dev, data);
885}
886
887static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
888{
889 struct ethtool_value edata;
890 int err;
891
892 if (!dev->ethtool_ops->set_tx_csum)
893 return -EOPNOTSUPP;
894
895 if (copy_from_user(&edata, useraddr, sizeof(edata)))
896 return -EFAULT;
897
898 if (!edata.data && dev->ethtool_ops->set_sg) {
899 err = __ethtool_set_sg(dev, 0);
900 if (err)
901 return err;
902 }
903
904 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
905}
906
Herbert Xub240a0e2008-12-15 23:44:31 -0800907static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
908{
909 struct ethtool_value edata;
910
911 if (!dev->ethtool_ops->set_rx_csum)
912 return -EOPNOTSUPP;
913
914 if (copy_from_user(&edata, useraddr, sizeof(edata)))
915 return -EFAULT;
916
917 if (!edata.data && dev->ethtool_ops->set_sg)
918 dev->features &= ~NETIF_F_GRO;
919
920 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
924{
925 struct ethtool_value edata;
926
927 if (!dev->ethtool_ops->set_sg)
928 return -EOPNOTSUPP;
929
930 if (copy_from_user(&edata, useraddr, sizeof(edata)))
931 return -EFAULT;
932
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900933 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -0700934 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -EINVAL;
936
937 return __ethtool_set_sg(dev, edata.data);
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
941{
942 struct ethtool_value edata;
943
944 if (!dev->ethtool_ops->set_tso)
945 return -EOPNOTSUPP;
946
947 if (copy_from_user(&edata, useraddr, sizeof(edata)))
948 return -EFAULT;
949
950 if (edata.data && !(dev->features & NETIF_F_SG))
951 return -EINVAL;
952
953 return dev->ethtool_ops->set_tso(dev, edata.data);
954}
955
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700956static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
957{
958 struct ethtool_value edata;
959
960 if (!dev->ethtool_ops->set_ufo)
961 return -EOPNOTSUPP;
962 if (copy_from_user(&edata, useraddr, sizeof(edata)))
963 return -EFAULT;
964 if (edata.data && !(dev->features & NETIF_F_SG))
965 return -EINVAL;
966 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
967 return -EINVAL;
968 return dev->ethtool_ops->set_ufo(dev, edata.data);
969}
970
Herbert Xu37c31852006-06-22 03:07:29 -0700971static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
972{
973 struct ethtool_value edata = { ETHTOOL_GGSO };
974
975 edata.data = dev->features & NETIF_F_GSO;
976 if (copy_to_user(useraddr, &edata, sizeof(edata)))
977 return -EFAULT;
978 return 0;
979}
980
981static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
982{
983 struct ethtool_value edata;
984
985 if (copy_from_user(&edata, useraddr, sizeof(edata)))
986 return -EFAULT;
987 if (edata.data)
988 dev->features |= NETIF_F_GSO;
989 else
990 dev->features &= ~NETIF_F_GSO;
991 return 0;
992}
993
Herbert Xub240a0e2008-12-15 23:44:31 -0800994static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
995{
996 struct ethtool_value edata = { ETHTOOL_GGRO };
997
998 edata.data = dev->features & NETIF_F_GRO;
999 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1000 return -EFAULT;
1001 return 0;
1002}
1003
1004static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1005{
1006 struct ethtool_value edata;
1007
1008 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1009 return -EFAULT;
1010
1011 if (edata.data) {
1012 if (!dev->ethtool_ops->get_rx_csum ||
1013 !dev->ethtool_ops->get_rx_csum(dev))
1014 return -EINVAL;
1015 dev->features |= NETIF_F_GRO;
1016 } else
1017 dev->features &= ~NETIF_F_GRO;
1018
1019 return 0;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1023{
1024 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001025 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001027 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001029 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001030 return -EOPNOTSUPP;
1031
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001032 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001033 if (test_len < 0)
1034 return test_len;
1035 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (copy_from_user(&test, useraddr, sizeof(test)))
1038 return -EFAULT;
1039
Jeff Garzikff03d492007-08-15 16:01:08 -07001040 test.len = test_len;
1041 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!data)
1043 return -ENOMEM;
1044
1045 ops->self_test(dev, &test, data);
1046
1047 ret = -EFAULT;
1048 if (copy_to_user(useraddr, &test, sizeof(test)))
1049 goto out;
1050 useraddr += sizeof(test);
1051 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1052 goto out;
1053 ret = 0;
1054
1055 out:
1056 kfree(data);
1057 return ret;
1058}
1059
1060static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1061{
1062 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001063 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 u8 *data;
1065 int ret;
1066
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001067 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EOPNOTSUPP;
1069
1070 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1071 return -EFAULT;
1072
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001073 ret = ops->get_sset_count(dev, gstrings.string_set);
1074 if (ret < 0)
1075 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001076
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001077 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1080 if (!data)
1081 return -ENOMEM;
1082
1083 ops->get_strings(dev, gstrings.string_set, data);
1084
1085 ret = -EFAULT;
1086 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1087 goto out;
1088 useraddr += sizeof(gstrings);
1089 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1090 goto out;
1091 ret = 0;
1092
1093 out:
1094 kfree(data);
1095 return ret;
1096}
1097
1098static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1099{
1100 struct ethtool_value id;
1101
1102 if (!dev->ethtool_ops->phys_id)
1103 return -EOPNOTSUPP;
1104
1105 if (copy_from_user(&id, useraddr, sizeof(id)))
1106 return -EFAULT;
1107
1108 return dev->ethtool_ops->phys_id(dev, id.data);
1109}
1110
1111static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1112{
1113 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001114 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001116 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001118 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001119 return -EOPNOTSUPP;
1120
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001121 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001122 if (n_stats < 0)
1123 return n_stats;
1124 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1127 return -EFAULT;
1128
Jeff Garzikff03d492007-08-15 16:01:08 -07001129 stats.n_stats = n_stats;
1130 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!data)
1132 return -ENOMEM;
1133
1134 ops->get_ethtool_stats(dev, &stats, data);
1135
1136 ret = -EFAULT;
1137 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1138 goto out;
1139 useraddr += sizeof(stats);
1140 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1141 goto out;
1142 ret = 0;
1143
1144 out:
1145 kfree(data);
1146 return ret;
1147}
1148
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001149static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001150{
1151 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001152
Matthew Wilcox313674a2007-07-31 14:00:29 -07001153 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001154 return -EFAULT;
1155
Matthew Wilcox313674a2007-07-31 14:00:29 -07001156 if (epaddr.size < dev->addr_len)
1157 return -ETOOSMALL;
1158 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001159
Jon Wetzela6f9a702005-08-20 17:15:54 -07001160 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001161 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001162 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001163 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1164 return -EFAULT;
1165 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001166}
1167
Jeff Garzik13c99b22007-08-15 16:01:56 -07001168static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1169 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001170{
Roland Dreier8e557422010-02-11 12:14:23 -08001171 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001172
Jeff Garzik13c99b22007-08-15 16:01:56 -07001173 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001174 return -EOPNOTSUPP;
1175
Jeff Garzik13c99b22007-08-15 16:01:56 -07001176 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001177
1178 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1179 return -EFAULT;
1180 return 0;
1181}
1182
Jeff Garzik13c99b22007-08-15 16:01:56 -07001183static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1184 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001185{
1186 struct ethtool_value edata;
1187
Jeff Garzik13c99b22007-08-15 16:01:56 -07001188 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001189 return -EOPNOTSUPP;
1190
1191 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1192 return -EFAULT;
1193
Jeff Garzik13c99b22007-08-15 16:01:56 -07001194 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001195 return 0;
1196}
1197
Jeff Garzik13c99b22007-08-15 16:01:56 -07001198static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1199 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001200{
1201 struct ethtool_value edata;
1202
Jeff Garzik13c99b22007-08-15 16:01:56 -07001203 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001204 return -EOPNOTSUPP;
1205
1206 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1207 return -EFAULT;
1208
Jeff Garzik13c99b22007-08-15 16:01:56 -07001209 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001210}
1211
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001212static int ethtool_flash_device(struct net_device *dev, char __user *useraddr)
1213{
1214 struct ethtool_flash efl;
1215
1216 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1217 return -EFAULT;
1218
1219 if (!dev->ethtool_ops->flash_device)
1220 return -EOPNOTSUPP;
1221
1222 return dev->ethtool_ops->flash_device(dev, &efl);
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/* The main entry point in this file. Called from net/core/dev.c */
1226
Eric W. Biederman881d9662007-09-17 11:56:21 -07001227int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001229 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 void __user *useraddr = ifr->ifr_data;
1231 u32 ethcmd;
1232 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -07001233 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!dev || !netif_device_present(dev))
1236 return -ENODEV;
1237
1238 if (!dev->ethtool_ops)
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001239 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
1242 return -EFAULT;
1243
Stephen Hemminger75f31232006-09-28 15:13:37 -07001244 /* Allow some commands to be done by anyone */
1245 switch(ethcmd) {
Stephen Hemminger75f31232006-09-28 15:13:37 -07001246 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001247 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001248 case ETHTOOL_GCOALESCE:
1249 case ETHTOOL_GRINGPARAM:
1250 case ETHTOOL_GPAUSEPARAM:
1251 case ETHTOOL_GRXCSUM:
1252 case ETHTOOL_GTXCSUM:
1253 case ETHTOOL_GSG:
1254 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001255 case ETHTOOL_GTSO:
1256 case ETHTOOL_GPERMADDR:
1257 case ETHTOOL_GUFO:
1258 case ETHTOOL_GGSO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001259 case ETHTOOL_GFLAGS:
1260 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001261 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001262 case ETHTOOL_GRXRINGS:
1263 case ETHTOOL_GRXCLSRLCNT:
1264 case ETHTOOL_GRXCLSRULE:
1265 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001266 break;
1267 default:
1268 if (!capable(CAP_NET_ADMIN))
1269 return -EPERM;
1270 }
1271
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001272 if (dev->ethtool_ops->begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
1274 return rc;
1275
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001276 old_features = dev->features;
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 switch (ethcmd) {
1279 case ETHTOOL_GSET:
1280 rc = ethtool_get_settings(dev, useraddr);
1281 break;
1282 case ETHTOOL_SSET:
1283 rc = ethtool_set_settings(dev, useraddr);
1284 break;
1285 case ETHTOOL_GDRVINFO:
1286 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case ETHTOOL_GREGS:
1289 rc = ethtool_get_regs(dev, useraddr);
1290 break;
1291 case ETHTOOL_GWOL:
1292 rc = ethtool_get_wol(dev, useraddr);
1293 break;
1294 case ETHTOOL_SWOL:
1295 rc = ethtool_set_wol(dev, useraddr);
1296 break;
1297 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001298 rc = ethtool_get_value(dev, useraddr, ethcmd,
1299 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 break;
1301 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001302 rc = ethtool_set_value_void(dev, useraddr,
1303 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
1305 case ETHTOOL_NWAY_RST:
1306 rc = ethtool_nway_reset(dev);
1307 break;
1308 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001309 rc = ethtool_get_value(dev, useraddr, ethcmd,
1310 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 break;
1312 case ETHTOOL_GEEPROM:
1313 rc = ethtool_get_eeprom(dev, useraddr);
1314 break;
1315 case ETHTOOL_SEEPROM:
1316 rc = ethtool_set_eeprom(dev, useraddr);
1317 break;
1318 case ETHTOOL_GCOALESCE:
1319 rc = ethtool_get_coalesce(dev, useraddr);
1320 break;
1321 case ETHTOOL_SCOALESCE:
1322 rc = ethtool_set_coalesce(dev, useraddr);
1323 break;
1324 case ETHTOOL_GRINGPARAM:
1325 rc = ethtool_get_ringparam(dev, useraddr);
1326 break;
1327 case ETHTOOL_SRINGPARAM:
1328 rc = ethtool_set_ringparam(dev, useraddr);
1329 break;
1330 case ETHTOOL_GPAUSEPARAM:
1331 rc = ethtool_get_pauseparam(dev, useraddr);
1332 break;
1333 case ETHTOOL_SPAUSEPARAM:
1334 rc = ethtool_set_pauseparam(dev, useraddr);
1335 break;
1336 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001337 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001338 (dev->ethtool_ops->get_rx_csum ?
1339 dev->ethtool_ops->get_rx_csum :
1340 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
1342 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001343 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001346 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001347 (dev->ethtool_ops->get_tx_csum ?
1348 dev->ethtool_ops->get_tx_csum :
1349 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 break;
1351 case ETHTOOL_STXCSUM:
1352 rc = ethtool_set_tx_csum(dev, useraddr);
1353 break;
1354 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001355 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001356 (dev->ethtool_ops->get_sg ?
1357 dev->ethtool_ops->get_sg :
1358 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 case ETHTOOL_SSG:
1361 rc = ethtool_set_sg(dev, useraddr);
1362 break;
1363 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001364 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001365 (dev->ethtool_ops->get_tso ?
1366 dev->ethtool_ops->get_tso :
1367 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
1369 case ETHTOOL_STSO:
1370 rc = ethtool_set_tso(dev, useraddr);
1371 break;
1372 case ETHTOOL_TEST:
1373 rc = ethtool_self_test(dev, useraddr);
1374 break;
1375 case ETHTOOL_GSTRINGS:
1376 rc = ethtool_get_strings(dev, useraddr);
1377 break;
1378 case ETHTOOL_PHYS_ID:
1379 rc = ethtool_phys_id(dev, useraddr);
1380 break;
1381 case ETHTOOL_GSTATS:
1382 rc = ethtool_get_stats(dev, useraddr);
1383 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001384 case ETHTOOL_GPERMADDR:
1385 rc = ethtool_get_perm_addr(dev, useraddr);
1386 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001387 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001388 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001389 (dev->ethtool_ops->get_ufo ?
1390 dev->ethtool_ops->get_ufo :
1391 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001392 break;
1393 case ETHTOOL_SUFO:
1394 rc = ethtool_set_ufo(dev, useraddr);
1395 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001396 case ETHTOOL_GGSO:
1397 rc = ethtool_get_gso(dev, useraddr);
1398 break;
1399 case ETHTOOL_SGSO:
1400 rc = ethtool_set_gso(dev, useraddr);
1401 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001402 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001403 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001404 (dev->ethtool_ops->get_flags ?
1405 dev->ethtool_ops->get_flags :
1406 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001407 break;
1408 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001409 rc = ethtool_set_value(dev, useraddr,
1410 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001411 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001412 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001413 rc = ethtool_get_value(dev, useraddr, ethcmd,
1414 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001415 break;
1416 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001417 rc = ethtool_set_value(dev, useraddr,
1418 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001419 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001420 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001421 case ETHTOOL_GRXRINGS:
1422 case ETHTOOL_GRXCLSRLCNT:
1423 case ETHTOOL_GRXCLSRULE:
1424 case ETHTOOL_GRXCLSRLALL:
1425 rc = ethtool_get_rxnfc(dev, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001426 break;
1427 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001428 case ETHTOOL_SRXCLSRLDEL:
1429 case ETHTOOL_SRXCLSRLINS:
1430 rc = ethtool_set_rxnfc(dev, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001431 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001432 case ETHTOOL_GGRO:
1433 rc = ethtool_get_gro(dev, useraddr);
1434 break;
1435 case ETHTOOL_SGRO:
1436 rc = ethtool_set_gro(dev, useraddr);
1437 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001438 case ETHTOOL_FLASHDEV:
1439 rc = ethtool_flash_device(dev, useraddr);
1440 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001441 case ETHTOOL_RESET:
1442 rc = ethtool_reset(dev, useraddr);
1443 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001444 case ETHTOOL_SRXNTUPLE:
1445 rc = ethtool_set_rx_ntuple(dev, useraddr);
1446 break;
1447 case ETHTOOL_GRXNTUPLE:
1448 rc = ethtool_get_rx_ntuple(dev, useraddr);
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001451 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001453
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001454 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001456
1457 if (old_features != dev->features)
1458 netdev_features_change(dev);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463EXPORT_SYMBOL(ethtool_op_get_link);
1464EXPORT_SYMBOL(ethtool_op_get_sg);
1465EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466EXPORT_SYMBOL(ethtool_op_set_sg);
1467EXPORT_SYMBOL(ethtool_op_set_tso);
1468EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Jon Mason69f6a0f2005-05-29 20:27:24 -07001469EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -07001470EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001471EXPORT_SYMBOL(ethtool_op_set_ufo);
1472EXPORT_SYMBOL(ethtool_op_get_ufo);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001473EXPORT_SYMBOL(ethtool_op_set_flags);
1474EXPORT_SYMBOL(ethtool_op_get_flags);