blob: 14ada537f895c10f86006b63b4ba4f312eb610d3 [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
33u32 ethtool_op_get_tx_csum(struct net_device *dev)
34{
Herbert Xu8648b302006-06-17 22:06:05 -070035 return (dev->features & NETIF_F_ALL_CSUM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
39{
40 if (data)
41 dev->features |= NETIF_F_IP_CSUM;
42 else
43 dev->features &= ~NETIF_F_IP_CSUM;
44
45 return 0;
46}
47
Jon Mason69f6a0f2005-05-29 20:27:24 -070048int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
49{
50 if (data)
51 dev->features |= NETIF_F_HW_CSUM;
52 else
53 dev->features &= ~NETIF_F_HW_CSUM;
54
55 return 0;
56}
Michael Chan6460d942007-07-14 19:07:52 -070057
58int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
59{
60 if (data)
61 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
62 else
63 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
64
65 return 0;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068u32 ethtool_op_get_sg(struct net_device *dev)
69{
70 return (dev->features & NETIF_F_SG) != 0;
71}
72
73int ethtool_op_set_sg(struct net_device *dev, u32 data)
74{
75 if (data)
76 dev->features |= NETIF_F_SG;
77 else
78 dev->features &= ~NETIF_F_SG;
79
80 return 0;
81}
82
83u32 ethtool_op_get_tso(struct net_device *dev)
84{
85 return (dev->features & NETIF_F_TSO) != 0;
86}
87
88int ethtool_op_set_tso(struct net_device *dev, u32 data)
89{
90 if (data)
91 dev->features |= NETIF_F_TSO;
92 else
93 dev->features &= ~NETIF_F_TSO;
94
95 return 0;
96}
97
Ananda Rajue89e9cf2005-10-18 15:46:41 -070098u32 ethtool_op_get_ufo(struct net_device *dev)
99{
100 return (dev->features & NETIF_F_UFO) != 0;
101}
102
103int ethtool_op_set_ufo(struct net_device *dev, u32 data)
104{
105 if (data)
106 dev->features |= NETIF_F_UFO;
107 else
108 dev->features &= ~NETIF_F_UFO;
109 return 0;
110}
111
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700112/* the following list of flags are the same as their associated
113 * NETIF_F_xxx values in include/linux/netdevice.h
114 */
115static const u32 flags_dup_features =
116 ETH_FLAG_LRO;
117
118u32 ethtool_op_get_flags(struct net_device *dev)
119{
120 /* in the future, this function will probably contain additional
121 * handling for flags which are not so easily handled
122 * by a simple masking operation
123 */
124
125 return dev->features & flags_dup_features;
126}
127
128int ethtool_op_set_flags(struct net_device *dev, u32 data)
129{
130 if (data & ETH_FLAG_LRO)
131 dev->features |= NETIF_F_LRO;
132 else
133 dev->features &= ~NETIF_F_LRO;
134
135 return 0;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Handlers for each ethtool command */
139
140static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
141{
142 struct ethtool_cmd cmd = { ETHTOOL_GSET };
143 int err;
144
145 if (!dev->ethtool_ops->get_settings)
146 return -EOPNOTSUPP;
147
148 err = dev->ethtool_ops->get_settings(dev, &cmd);
149 if (err < 0)
150 return err;
151
152 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
153 return -EFAULT;
154 return 0;
155}
156
157static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
158{
159 struct ethtool_cmd cmd;
160
161 if (!dev->ethtool_ops->set_settings)
162 return -EOPNOTSUPP;
163
164 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
165 return -EFAULT;
166
167 return dev->ethtool_ops->set_settings(dev, &cmd);
168}
169
170static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
171{
172 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700173 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!ops->get_drvinfo)
176 return -EOPNOTSUPP;
177
178 memset(&info, 0, sizeof(info));
179 info.cmd = ETHTOOL_GDRVINFO;
180 ops->get_drvinfo(dev, &info);
181
Jeff Garzikff03d492007-08-15 16:01:08 -0700182 if (ops->get_sset_count) {
183 int rc;
184
185 rc = ops->get_sset_count(dev, ETH_SS_TEST);
186 if (rc >= 0)
187 info.testinfo_len = rc;
188 rc = ops->get_sset_count(dev, ETH_SS_STATS);
189 if (rc >= 0)
190 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700191 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
192 if (rc >= 0)
193 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700194 } else {
195 /* code path for obsolete hooks */
196
197 if (ops->self_test_count)
198 info.testinfo_len = ops->self_test_count(dev);
199 if (ops->get_stats_count)
200 info.n_stats = ops->get_stats_count(dev);
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (ops->get_regs_len)
203 info.regdump_len = ops->get_regs_len(dev);
204 if (ops->get_eeprom_len)
205 info.eedump_len = ops->get_eeprom_len(dev);
206
207 if (copy_to_user(useraddr, &info, sizeof(info)))
208 return -EFAULT;
209 return 0;
210}
211
Santwona Behera0853ad62008-07-02 03:47:41 -0700212static int ethtool_set_rxhash(struct net_device *dev, void __user *useraddr)
213{
214 struct ethtool_rxnfc cmd;
215
216 if (!dev->ethtool_ops->set_rxhash)
217 return -EOPNOTSUPP;
218
219 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
220 return -EFAULT;
221
222 return dev->ethtool_ops->set_rxhash(dev, &cmd);
223}
224
225static int ethtool_get_rxhash(struct net_device *dev, void __user *useraddr)
226{
227 struct ethtool_rxnfc info;
228
229 if (!dev->ethtool_ops->get_rxhash)
230 return -EOPNOTSUPP;
231
232 if (copy_from_user(&info, useraddr, sizeof(info)))
233 return -EFAULT;
234
235 dev->ethtool_ops->get_rxhash(dev, &info);
236
237 if (copy_to_user(useraddr, &info, sizeof(info)))
238 return -EFAULT;
239 return 0;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
243{
244 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700245 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 void *regbuf;
247 int reglen, ret;
248
249 if (!ops->get_regs || !ops->get_regs_len)
250 return -EOPNOTSUPP;
251
252 if (copy_from_user(&regs, useraddr, sizeof(regs)))
253 return -EFAULT;
254
255 reglen = ops->get_regs_len(dev);
256 if (regs.len > reglen)
257 regs.len = reglen;
258
259 regbuf = kmalloc(reglen, GFP_USER);
260 if (!regbuf)
261 return -ENOMEM;
262
263 ops->get_regs(dev, &regs, regbuf);
264
265 ret = -EFAULT;
266 if (copy_to_user(useraddr, &regs, sizeof(regs)))
267 goto out;
268 useraddr += offsetof(struct ethtool_regs, data);
269 if (copy_to_user(useraddr, regbuf, regs.len))
270 goto out;
271 ret = 0;
272
273 out:
274 kfree(regbuf);
275 return ret;
276}
277
278static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
279{
280 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
281
282 if (!dev->ethtool_ops->get_wol)
283 return -EOPNOTSUPP;
284
285 dev->ethtool_ops->get_wol(dev, &wol);
286
287 if (copy_to_user(useraddr, &wol, sizeof(wol)))
288 return -EFAULT;
289 return 0;
290}
291
292static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
293{
294 struct ethtool_wolinfo wol;
295
296 if (!dev->ethtool_ops->set_wol)
297 return -EOPNOTSUPP;
298
299 if (copy_from_user(&wol, useraddr, sizeof(wol)))
300 return -EFAULT;
301
302 return dev->ethtool_ops->set_wol(dev, &wol);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static int ethtool_nway_reset(struct net_device *dev)
306{
307 if (!dev->ethtool_ops->nway_reset)
308 return -EOPNOTSUPP;
309
310 return dev->ethtool_ops->nway_reset(dev);
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
314{
315 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700316 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700317 void __user *userbuf = useraddr + sizeof(eeprom);
318 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700320 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (!ops->get_eeprom || !ops->get_eeprom_len)
323 return -EOPNOTSUPP;
324
325 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
326 return -EFAULT;
327
328 /* Check for wrap and zero */
329 if (eeprom.offset + eeprom.len <= eeprom.offset)
330 return -EINVAL;
331
332 /* Check for exceeding total eeprom len */
333 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
334 return -EINVAL;
335
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700336 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!data)
338 return -ENOMEM;
339
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700340 bytes_remaining = eeprom.len;
341 while (bytes_remaining > 0) {
342 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700344 ret = ops->get_eeprom(dev, &eeprom, data);
345 if (ret)
346 break;
347 if (copy_to_user(userbuf, data, eeprom.len)) {
348 ret = -EFAULT;
349 break;
350 }
351 userbuf += eeprom.len;
352 eeprom.offset += eeprom.len;
353 bytes_remaining -= eeprom.len;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700356 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
357 eeprom.offset -= eeprom.len;
358 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
359 ret = -EFAULT;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 kfree(data);
362 return ret;
363}
364
365static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
366{
367 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700368 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700369 void __user *userbuf = useraddr + sizeof(eeprom);
370 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700372 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (!ops->set_eeprom || !ops->get_eeprom_len)
375 return -EOPNOTSUPP;
376
377 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
378 return -EFAULT;
379
380 /* Check for wrap and zero */
381 if (eeprom.offset + eeprom.len <= eeprom.offset)
382 return -EINVAL;
383
384 /* Check for exceeding total eeprom len */
385 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
386 return -EINVAL;
387
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700388 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!data)
390 return -ENOMEM;
391
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700392 bytes_remaining = eeprom.len;
393 while (bytes_remaining > 0) {
394 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700396 if (copy_from_user(data, userbuf, eeprom.len)) {
397 ret = -EFAULT;
398 break;
399 }
400 ret = ops->set_eeprom(dev, &eeprom, data);
401 if (ret)
402 break;
403 userbuf += eeprom.len;
404 eeprom.offset += eeprom.len;
405 bytes_remaining -= eeprom.len;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 kfree(data);
409 return ret;
410}
411
412static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
413{
414 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
415
416 if (!dev->ethtool_ops->get_coalesce)
417 return -EOPNOTSUPP;
418
419 dev->ethtool_ops->get_coalesce(dev, &coalesce);
420
421 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
422 return -EFAULT;
423 return 0;
424}
425
426static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
427{
428 struct ethtool_coalesce coalesce;
429
David S. Millerfa04ae52005-06-06 15:07:19 -0700430 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -EOPNOTSUPP;
432
433 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
434 return -EFAULT;
435
436 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
437}
438
439static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
440{
441 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
442
443 if (!dev->ethtool_ops->get_ringparam)
444 return -EOPNOTSUPP;
445
446 dev->ethtool_ops->get_ringparam(dev, &ringparam);
447
448 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
449 return -EFAULT;
450 return 0;
451}
452
453static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
454{
455 struct ethtool_ringparam ringparam;
456
457 if (!dev->ethtool_ops->set_ringparam)
458 return -EOPNOTSUPP;
459
460 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
461 return -EFAULT;
462
463 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
464}
465
466static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
467{
468 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
469
470 if (!dev->ethtool_ops->get_pauseparam)
471 return -EOPNOTSUPP;
472
473 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
474
475 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
476 return -EFAULT;
477 return 0;
478}
479
480static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
481{
482 struct ethtool_pauseparam pauseparam;
483
Jeff Garzike1b90c42006-07-17 12:54:40 -0400484 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -EOPNOTSUPP;
486
487 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
488 return -EFAULT;
489
490 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int __ethtool_set_sg(struct net_device *dev, u32 data)
494{
495 int err;
496
497 if (!data && dev->ethtool_ops->set_tso) {
498 err = dev->ethtool_ops->set_tso(dev, 0);
499 if (err)
500 return err;
501 }
502
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700503 if (!data && dev->ethtool_ops->set_ufo) {
504 err = dev->ethtool_ops->set_ufo(dev, 0);
505 if (err)
506 return err;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return dev->ethtool_ops->set_sg(dev, data);
509}
510
511static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
512{
513 struct ethtool_value edata;
514 int err;
515
516 if (!dev->ethtool_ops->set_tx_csum)
517 return -EOPNOTSUPP;
518
519 if (copy_from_user(&edata, useraddr, sizeof(edata)))
520 return -EFAULT;
521
522 if (!edata.data && dev->ethtool_ops->set_sg) {
523 err = __ethtool_set_sg(dev, 0);
524 if (err)
525 return err;
526 }
527
528 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
532{
533 struct ethtool_value edata;
534
535 if (!dev->ethtool_ops->set_sg)
536 return -EOPNOTSUPP;
537
538 if (copy_from_user(&edata, useraddr, sizeof(edata)))
539 return -EFAULT;
540
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900541 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -0700542 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EINVAL;
544
545 return __ethtool_set_sg(dev, edata.data);
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
549{
550 struct ethtool_value edata;
551
552 if (!dev->ethtool_ops->set_tso)
553 return -EOPNOTSUPP;
554
555 if (copy_from_user(&edata, useraddr, sizeof(edata)))
556 return -EFAULT;
557
558 if (edata.data && !(dev->features & NETIF_F_SG))
559 return -EINVAL;
560
561 return dev->ethtool_ops->set_tso(dev, edata.data);
562}
563
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700564static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
565{
566 struct ethtool_value edata;
567
568 if (!dev->ethtool_ops->set_ufo)
569 return -EOPNOTSUPP;
570 if (copy_from_user(&edata, useraddr, sizeof(edata)))
571 return -EFAULT;
572 if (edata.data && !(dev->features & NETIF_F_SG))
573 return -EINVAL;
574 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
575 return -EINVAL;
576 return dev->ethtool_ops->set_ufo(dev, edata.data);
577}
578
Herbert Xu37c31852006-06-22 03:07:29 -0700579static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
580{
581 struct ethtool_value edata = { ETHTOOL_GGSO };
582
583 edata.data = dev->features & NETIF_F_GSO;
584 if (copy_to_user(useraddr, &edata, sizeof(edata)))
585 return -EFAULT;
586 return 0;
587}
588
589static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
590{
591 struct ethtool_value edata;
592
593 if (copy_from_user(&edata, useraddr, sizeof(edata)))
594 return -EFAULT;
595 if (edata.data)
596 dev->features |= NETIF_F_GSO;
597 else
598 dev->features &= ~NETIF_F_GSO;
599 return 0;
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
603{
604 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700605 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -0700607 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jeff Garzikff03d492007-08-15 16:01:08 -0700609 if (!ops->self_test)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -EOPNOTSUPP;
Jeff Garzikff03d492007-08-15 16:01:08 -0700611 if (!ops->get_sset_count && !ops->self_test_count)
612 return -EOPNOTSUPP;
613
614 if (ops->get_sset_count)
615 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
616 else
617 /* code path for obsolete hook */
618 test_len = ops->self_test_count(dev);
619 if (test_len < 0)
620 return test_len;
621 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (copy_from_user(&test, useraddr, sizeof(test)))
624 return -EFAULT;
625
Jeff Garzikff03d492007-08-15 16:01:08 -0700626 test.len = test_len;
627 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!data)
629 return -ENOMEM;
630
631 ops->self_test(dev, &test, data);
632
633 ret = -EFAULT;
634 if (copy_to_user(useraddr, &test, sizeof(test)))
635 goto out;
636 useraddr += sizeof(test);
637 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
638 goto out;
639 ret = 0;
640
641 out:
642 kfree(data);
643 return ret;
644}
645
646static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
647{
648 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700649 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 u8 *data;
651 int ret;
652
653 if (!ops->get_strings)
654 return -EOPNOTSUPP;
655
656 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
657 return -EFAULT;
658
Jeff Garzikff03d492007-08-15 16:01:08 -0700659 if (ops->get_sset_count) {
660 ret = ops->get_sset_count(dev, gstrings.string_set);
661 if (ret < 0)
662 return ret;
663
664 gstrings.len = ret;
665 } else {
666 /* code path for obsolete hooks */
667
668 switch (gstrings.string_set) {
669 case ETH_SS_TEST:
670 if (!ops->self_test_count)
671 return -EOPNOTSUPP;
672 gstrings.len = ops->self_test_count(dev);
673 break;
674 case ETH_SS_STATS:
675 if (!ops->get_stats_count)
676 return -EOPNOTSUPP;
677 gstrings.len = ops->get_stats_count(dev);
678 break;
679 default:
680 return -EINVAL;
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
685 if (!data)
686 return -ENOMEM;
687
688 ops->get_strings(dev, gstrings.string_set, data);
689
690 ret = -EFAULT;
691 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
692 goto out;
693 useraddr += sizeof(gstrings);
694 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
695 goto out;
696 ret = 0;
697
698 out:
699 kfree(data);
700 return ret;
701}
702
703static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
704{
705 struct ethtool_value id;
706
707 if (!dev->ethtool_ops->phys_id)
708 return -EOPNOTSUPP;
709
710 if (copy_from_user(&id, useraddr, sizeof(id)))
711 return -EFAULT;
712
713 return dev->ethtool_ops->phys_id(dev, id.data);
714}
715
716static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
717{
718 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700719 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -0700721 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jeff Garzikff03d492007-08-15 16:01:08 -0700723 if (!ops->get_ethtool_stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return -EOPNOTSUPP;
Jeff Garzikff03d492007-08-15 16:01:08 -0700725 if (!ops->get_sset_count && !ops->get_stats_count)
726 return -EOPNOTSUPP;
727
728 if (ops->get_sset_count)
729 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
730 else
731 /* code path for obsolete hook */
732 n_stats = ops->get_stats_count(dev);
733 if (n_stats < 0)
734 return n_stats;
735 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (copy_from_user(&stats, useraddr, sizeof(stats)))
738 return -EFAULT;
739
Jeff Garzikff03d492007-08-15 16:01:08 -0700740 stats.n_stats = n_stats;
741 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!data)
743 return -ENOMEM;
744
745 ops->get_ethtool_stats(dev, &stats, data);
746
747 ret = -EFAULT;
748 if (copy_to_user(useraddr, &stats, sizeof(stats)))
749 goto out;
750 useraddr += sizeof(stats);
751 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
752 goto out;
753 ret = 0;
754
755 out:
756 kfree(data);
757 return ret;
758}
759
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +0100760static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -0700761{
762 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700763
Matthew Wilcox313674a2007-07-31 14:00:29 -0700764 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -0700765 return -EFAULT;
766
Matthew Wilcox313674a2007-07-31 14:00:29 -0700767 if (epaddr.size < dev->addr_len)
768 return -ETOOSMALL;
769 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700770
Jon Wetzela6f9a702005-08-20 17:15:54 -0700771 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -0700772 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700773 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -0700774 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
775 return -EFAULT;
776 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700777}
778
Jeff Garzik13c99b22007-08-15 16:01:56 -0700779static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
780 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700781{
Jeff Garzik13c99b22007-08-15 16:01:56 -0700782 struct ethtool_value edata = { cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700783
Jeff Garzik13c99b22007-08-15 16:01:56 -0700784 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700785 return -EOPNOTSUPP;
786
Jeff Garzik13c99b22007-08-15 16:01:56 -0700787 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700788
789 if (copy_to_user(useraddr, &edata, sizeof(edata)))
790 return -EFAULT;
791 return 0;
792}
793
Jeff Garzik13c99b22007-08-15 16:01:56 -0700794static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
795 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700796{
797 struct ethtool_value edata;
798
Jeff Garzik13c99b22007-08-15 16:01:56 -0700799 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700800 return -EOPNOTSUPP;
801
802 if (copy_from_user(&edata, useraddr, sizeof(edata)))
803 return -EFAULT;
804
Jeff Garzik13c99b22007-08-15 16:01:56 -0700805 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -0700806 return 0;
807}
808
Jeff Garzik13c99b22007-08-15 16:01:56 -0700809static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
810 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -0700811{
812 struct ethtool_value edata;
813
Jeff Garzik13c99b22007-08-15 16:01:56 -0700814 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -0700815 return -EOPNOTSUPP;
816
817 if (copy_from_user(&edata, useraddr, sizeof(edata)))
818 return -EFAULT;
819
Jeff Garzik13c99b22007-08-15 16:01:56 -0700820 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -0700821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/* The main entry point in this file. Called from net/core/dev.c */
824
Eric W. Biederman881d9662007-09-17 11:56:21 -0700825int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700827 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 void __user *useraddr = ifr->ifr_data;
829 u32 ethcmd;
830 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -0700831 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (!dev || !netif_device_present(dev))
834 return -ENODEV;
835
836 if (!dev->ethtool_ops)
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700837 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
840 return -EFAULT;
841
Stephen Hemminger75f31232006-09-28 15:13:37 -0700842 /* Allow some commands to be done by anyone */
843 switch(ethcmd) {
Stephen Hemminger75f31232006-09-28 15:13:37 -0700844 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700845 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700846 case ETHTOOL_GCOALESCE:
847 case ETHTOOL_GRINGPARAM:
848 case ETHTOOL_GPAUSEPARAM:
849 case ETHTOOL_GRXCSUM:
850 case ETHTOOL_GTXCSUM:
851 case ETHTOOL_GSG:
852 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700853 case ETHTOOL_GTSO:
854 case ETHTOOL_GPERMADDR:
855 case ETHTOOL_GUFO:
856 case ETHTOOL_GGSO:
Jeff Garzik339bf022007-08-15 16:01:32 -0700857 case ETHTOOL_GFLAGS:
858 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -0700859 case ETHTOOL_GRXFH:
Stephen Hemminger75f31232006-09-28 15:13:37 -0700860 break;
861 default:
862 if (!capable(CAP_NET_ADMIN))
863 return -EPERM;
864 }
865
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700866 if (dev->ethtool_ops->begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
868 return rc;
869
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700870 old_features = dev->features;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 switch (ethcmd) {
873 case ETHTOOL_GSET:
874 rc = ethtool_get_settings(dev, useraddr);
875 break;
876 case ETHTOOL_SSET:
877 rc = ethtool_set_settings(dev, useraddr);
878 break;
879 case ETHTOOL_GDRVINFO:
880 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 case ETHTOOL_GREGS:
883 rc = ethtool_get_regs(dev, useraddr);
884 break;
885 case ETHTOOL_GWOL:
886 rc = ethtool_get_wol(dev, useraddr);
887 break;
888 case ETHTOOL_SWOL:
889 rc = ethtool_set_wol(dev, useraddr);
890 break;
891 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700892 rc = ethtool_get_value(dev, useraddr, ethcmd,
893 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
895 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700896 rc = ethtool_set_value_void(dev, useraddr,
897 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 case ETHTOOL_NWAY_RST:
900 rc = ethtool_nway_reset(dev);
901 break;
902 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700903 rc = ethtool_get_value(dev, useraddr, ethcmd,
904 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 break;
906 case ETHTOOL_GEEPROM:
907 rc = ethtool_get_eeprom(dev, useraddr);
908 break;
909 case ETHTOOL_SEEPROM:
910 rc = ethtool_set_eeprom(dev, useraddr);
911 break;
912 case ETHTOOL_GCOALESCE:
913 rc = ethtool_get_coalesce(dev, useraddr);
914 break;
915 case ETHTOOL_SCOALESCE:
916 rc = ethtool_set_coalesce(dev, useraddr);
917 break;
918 case ETHTOOL_GRINGPARAM:
919 rc = ethtool_get_ringparam(dev, useraddr);
920 break;
921 case ETHTOOL_SRINGPARAM:
922 rc = ethtool_set_ringparam(dev, useraddr);
923 break;
924 case ETHTOOL_GPAUSEPARAM:
925 rc = ethtool_get_pauseparam(dev, useraddr);
926 break;
927 case ETHTOOL_SPAUSEPARAM:
928 rc = ethtool_set_pauseparam(dev, useraddr);
929 break;
930 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700931 rc = ethtool_get_value(dev, useraddr, ethcmd,
932 dev->ethtool_ops->get_rx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
934 case ETHTOOL_SRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700935 rc = ethtool_set_value(dev, useraddr,
936 dev->ethtool_ops->set_rx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
938 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700939 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700940 (dev->ethtool_ops->get_tx_csum ?
941 dev->ethtool_ops->get_tx_csum :
942 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 case ETHTOOL_STXCSUM:
945 rc = ethtool_set_tx_csum(dev, useraddr);
946 break;
947 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700948 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700949 (dev->ethtool_ops->get_sg ?
950 dev->ethtool_ops->get_sg :
951 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
953 case ETHTOOL_SSG:
954 rc = ethtool_set_sg(dev, useraddr);
955 break;
956 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700957 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700958 (dev->ethtool_ops->get_tso ?
959 dev->ethtool_ops->get_tso :
960 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case ETHTOOL_STSO:
963 rc = ethtool_set_tso(dev, useraddr);
964 break;
965 case ETHTOOL_TEST:
966 rc = ethtool_self_test(dev, useraddr);
967 break;
968 case ETHTOOL_GSTRINGS:
969 rc = ethtool_get_strings(dev, useraddr);
970 break;
971 case ETHTOOL_PHYS_ID:
972 rc = ethtool_phys_id(dev, useraddr);
973 break;
974 case ETHTOOL_GSTATS:
975 rc = ethtool_get_stats(dev, useraddr);
976 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -0700977 case ETHTOOL_GPERMADDR:
978 rc = ethtool_get_perm_addr(dev, useraddr);
979 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700980 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700981 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -0700982 (dev->ethtool_ops->get_ufo ?
983 dev->ethtool_ops->get_ufo :
984 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700985 break;
986 case ETHTOOL_SUFO:
987 rc = ethtool_set_ufo(dev, useraddr);
988 break;
Herbert Xu37c31852006-06-22 03:07:29 -0700989 case ETHTOOL_GGSO:
990 rc = ethtool_get_gso(dev, useraddr);
991 break;
992 case ETHTOOL_SGSO:
993 rc = ethtool_set_gso(dev, useraddr);
994 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700995 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -0700996 rc = ethtool_get_value(dev, useraddr, ethcmd,
997 dev->ethtool_ops->get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700998 break;
999 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001000 rc = ethtool_set_value(dev, useraddr,
1001 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001002 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001003 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001004 rc = ethtool_get_value(dev, useraddr, ethcmd,
1005 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001006 break;
1007 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001008 rc = ethtool_set_value(dev, useraddr,
1009 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001010 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001011 case ETHTOOL_GRXFH:
1012 rc = ethtool_get_rxhash(dev, useraddr);
1013 break;
1014 case ETHTOOL_SRXFH:
1015 rc = ethtool_set_rxhash(dev, useraddr);
1016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001018 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001020
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001021 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001023
1024 if (old_features != dev->features)
1025 netdev_features_change(dev);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030EXPORT_SYMBOL(ethtool_op_get_link);
1031EXPORT_SYMBOL(ethtool_op_get_sg);
1032EXPORT_SYMBOL(ethtool_op_get_tso);
1033EXPORT_SYMBOL(ethtool_op_get_tx_csum);
1034EXPORT_SYMBOL(ethtool_op_set_sg);
1035EXPORT_SYMBOL(ethtool_op_set_tso);
1036EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Jon Mason69f6a0f2005-05-29 20:27:24 -07001037EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -07001038EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001039EXPORT_SYMBOL(ethtool_op_set_ufo);
1040EXPORT_SYMBOL(ethtool_op_get_ufo);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001041EXPORT_SYMBOL(ethtool_op_set_flags);
1042EXPORT_SYMBOL(ethtool_op_get_flags);