Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/core/ethtool.c - Ethtool ioctl handler | |
3 | * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx> | |
4 | * | |
5 | * This file is where we call all the ethtool_ops commands to get | |
6 | * the information ethtool needs. We fall back to calling do_ioctl() | |
7 | * for drivers which haven't been converted to ethtool_ops yet. | |
8 | * | |
9 | * It's GPL, stupid. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/types.h> | |
4fc268d2 | 14 | #include <linux/capability.h> |
1da177e4 LT |
15 | #include <linux/errno.h> |
16 | #include <linux/ethtool.h> | |
17 | #include <linux/netdevice.h> | |
18 | #include <asm/uaccess.h> | |
19 | ||
20 | /* | |
21 | * Some useful ethtool_ops methods that're device independent. | |
22 | * If we find that all drivers want to do the same thing here, | |
23 | * we can turn these into dev_() function calls. | |
24 | */ | |
25 | ||
26 | u32 ethtool_op_get_link(struct net_device *dev) | |
27 | { | |
28 | return netif_carrier_ok(dev) ? 1 : 0; | |
29 | } | |
30 | ||
31 | u32 ethtool_op_get_tx_csum(struct net_device *dev) | |
32 | { | |
8648b305 | 33 | return (dev->features & NETIF_F_ALL_CSUM) != 0; |
1da177e4 LT |
34 | } |
35 | ||
36 | int ethtool_op_set_tx_csum(struct net_device *dev, u32 data) | |
37 | { | |
38 | if (data) | |
39 | dev->features |= NETIF_F_IP_CSUM; | |
40 | else | |
41 | dev->features &= ~NETIF_F_IP_CSUM; | |
42 | ||
43 | return 0; | |
44 | } | |
45 | ||
69f6a0fa JM |
46 | int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data) |
47 | { | |
48 | if (data) | |
49 | dev->features |= NETIF_F_HW_CSUM; | |
50 | else | |
51 | dev->features &= ~NETIF_F_HW_CSUM; | |
52 | ||
53 | return 0; | |
54 | } | |
1da177e4 LT |
55 | u32 ethtool_op_get_sg(struct net_device *dev) |
56 | { | |
57 | return (dev->features & NETIF_F_SG) != 0; | |
58 | } | |
59 | ||
60 | int ethtool_op_set_sg(struct net_device *dev, u32 data) | |
61 | { | |
62 | if (data) | |
63 | dev->features |= NETIF_F_SG; | |
64 | else | |
65 | dev->features &= ~NETIF_F_SG; | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
70 | u32 ethtool_op_get_tso(struct net_device *dev) | |
71 | { | |
72 | return (dev->features & NETIF_F_TSO) != 0; | |
73 | } | |
74 | ||
75 | int ethtool_op_set_tso(struct net_device *dev, u32 data) | |
76 | { | |
77 | if (data) | |
78 | dev->features |= NETIF_F_TSO; | |
79 | else | |
80 | dev->features &= ~NETIF_F_TSO; | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
a6f9a705 JW |
85 | int ethtool_op_get_perm_addr(struct net_device *dev, struct ethtool_perm_addr *addr, u8 *data) |
86 | { | |
87 | unsigned char len = dev->addr_len; | |
88 | if ( addr->size < len ) | |
89 | return -ETOOSMALL; | |
90 | ||
91 | addr->size = len; | |
92 | memcpy(data, dev->perm_addr, len); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | ||
e89e9cf5 AR |
97 | u32 ethtool_op_get_ufo(struct net_device *dev) |
98 | { | |
99 | return (dev->features & NETIF_F_UFO) != 0; | |
100 | } | |
101 | ||
102 | int ethtool_op_set_ufo(struct net_device *dev, u32 data) | |
103 | { | |
104 | if (data) | |
105 | dev->features |= NETIF_F_UFO; | |
106 | else | |
107 | dev->features &= ~NETIF_F_UFO; | |
108 | return 0; | |
109 | } | |
110 | ||
1da177e4 LT |
111 | /* Handlers for each ethtool command */ |
112 | ||
113 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) | |
114 | { | |
115 | struct ethtool_cmd cmd = { ETHTOOL_GSET }; | |
116 | int err; | |
117 | ||
118 | if (!dev->ethtool_ops->get_settings) | |
119 | return -EOPNOTSUPP; | |
120 | ||
121 | err = dev->ethtool_ops->get_settings(dev, &cmd); | |
122 | if (err < 0) | |
123 | return err; | |
124 | ||
125 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) | |
126 | return -EFAULT; | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) | |
131 | { | |
132 | struct ethtool_cmd cmd; | |
133 | ||
134 | if (!dev->ethtool_ops->set_settings) | |
135 | return -EOPNOTSUPP; | |
136 | ||
137 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) | |
138 | return -EFAULT; | |
139 | ||
140 | return dev->ethtool_ops->set_settings(dev, &cmd); | |
141 | } | |
142 | ||
143 | static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr) | |
144 | { | |
145 | struct ethtool_drvinfo info; | |
146 | struct ethtool_ops *ops = dev->ethtool_ops; | |
147 | ||
148 | if (!ops->get_drvinfo) | |
149 | return -EOPNOTSUPP; | |
150 | ||
151 | memset(&info, 0, sizeof(info)); | |
152 | info.cmd = ETHTOOL_GDRVINFO; | |
153 | ops->get_drvinfo(dev, &info); | |
154 | ||
155 | if (ops->self_test_count) | |
156 | info.testinfo_len = ops->self_test_count(dev); | |
157 | if (ops->get_stats_count) | |
158 | info.n_stats = ops->get_stats_count(dev); | |
159 | if (ops->get_regs_len) | |
160 | info.regdump_len = ops->get_regs_len(dev); | |
161 | if (ops->get_eeprom_len) | |
162 | info.eedump_len = ops->get_eeprom_len(dev); | |
163 | ||
164 | if (copy_to_user(useraddr, &info, sizeof(info))) | |
165 | return -EFAULT; | |
166 | return 0; | |
167 | } | |
168 | ||
169 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) | |
170 | { | |
171 | struct ethtool_regs regs; | |
172 | struct ethtool_ops *ops = dev->ethtool_ops; | |
173 | void *regbuf; | |
174 | int reglen, ret; | |
175 | ||
176 | if (!ops->get_regs || !ops->get_regs_len) | |
177 | return -EOPNOTSUPP; | |
178 | ||
179 | if (copy_from_user(®s, useraddr, sizeof(regs))) | |
180 | return -EFAULT; | |
181 | ||
182 | reglen = ops->get_regs_len(dev); | |
183 | if (regs.len > reglen) | |
184 | regs.len = reglen; | |
185 | ||
186 | regbuf = kmalloc(reglen, GFP_USER); | |
187 | if (!regbuf) | |
188 | return -ENOMEM; | |
189 | ||
190 | ops->get_regs(dev, ®s, regbuf); | |
191 | ||
192 | ret = -EFAULT; | |
193 | if (copy_to_user(useraddr, ®s, sizeof(regs))) | |
194 | goto out; | |
195 | useraddr += offsetof(struct ethtool_regs, data); | |
196 | if (copy_to_user(useraddr, regbuf, regs.len)) | |
197 | goto out; | |
198 | ret = 0; | |
199 | ||
200 | out: | |
201 | kfree(regbuf); | |
202 | return ret; | |
203 | } | |
204 | ||
205 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) | |
206 | { | |
207 | struct ethtool_wolinfo wol = { ETHTOOL_GWOL }; | |
208 | ||
209 | if (!dev->ethtool_ops->get_wol) | |
210 | return -EOPNOTSUPP; | |
211 | ||
212 | dev->ethtool_ops->get_wol(dev, &wol); | |
213 | ||
214 | if (copy_to_user(useraddr, &wol, sizeof(wol))) | |
215 | return -EFAULT; | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) | |
220 | { | |
221 | struct ethtool_wolinfo wol; | |
222 | ||
223 | if (!dev->ethtool_ops->set_wol) | |
224 | return -EOPNOTSUPP; | |
225 | ||
226 | if (copy_from_user(&wol, useraddr, sizeof(wol))) | |
227 | return -EFAULT; | |
228 | ||
229 | return dev->ethtool_ops->set_wol(dev, &wol); | |
230 | } | |
231 | ||
232 | static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr) | |
233 | { | |
234 | struct ethtool_value edata = { ETHTOOL_GMSGLVL }; | |
235 | ||
236 | if (!dev->ethtool_ops->get_msglevel) | |
237 | return -EOPNOTSUPP; | |
238 | ||
239 | edata.data = dev->ethtool_ops->get_msglevel(dev); | |
240 | ||
241 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
242 | return -EFAULT; | |
243 | return 0; | |
244 | } | |
245 | ||
246 | static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr) | |
247 | { | |
248 | struct ethtool_value edata; | |
249 | ||
250 | if (!dev->ethtool_ops->set_msglevel) | |
251 | return -EOPNOTSUPP; | |
252 | ||
253 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
254 | return -EFAULT; | |
255 | ||
256 | dev->ethtool_ops->set_msglevel(dev, edata.data); | |
257 | return 0; | |
258 | } | |
259 | ||
260 | static int ethtool_nway_reset(struct net_device *dev) | |
261 | { | |
262 | if (!dev->ethtool_ops->nway_reset) | |
263 | return -EOPNOTSUPP; | |
264 | ||
265 | return dev->ethtool_ops->nway_reset(dev); | |
266 | } | |
267 | ||
268 | static int ethtool_get_link(struct net_device *dev, void __user *useraddr) | |
269 | { | |
270 | struct ethtool_value edata = { ETHTOOL_GLINK }; | |
271 | ||
272 | if (!dev->ethtool_ops->get_link) | |
273 | return -EOPNOTSUPP; | |
274 | ||
275 | edata.data = dev->ethtool_ops->get_link(dev); | |
276 | ||
277 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
278 | return -EFAULT; | |
279 | return 0; | |
280 | } | |
281 | ||
282 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) | |
283 | { | |
284 | struct ethtool_eeprom eeprom; | |
285 | struct ethtool_ops *ops = dev->ethtool_ops; | |
286 | u8 *data; | |
287 | int ret; | |
288 | ||
289 | if (!ops->get_eeprom || !ops->get_eeprom_len) | |
290 | return -EOPNOTSUPP; | |
291 | ||
292 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) | |
293 | return -EFAULT; | |
294 | ||
295 | /* Check for wrap and zero */ | |
296 | if (eeprom.offset + eeprom.len <= eeprom.offset) | |
297 | return -EINVAL; | |
298 | ||
299 | /* Check for exceeding total eeprom len */ | |
300 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) | |
301 | return -EINVAL; | |
302 | ||
303 | data = kmalloc(eeprom.len, GFP_USER); | |
304 | if (!data) | |
305 | return -ENOMEM; | |
306 | ||
307 | ret = -EFAULT; | |
308 | if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len)) | |
309 | goto out; | |
310 | ||
311 | ret = ops->get_eeprom(dev, &eeprom, data); | |
312 | if (ret) | |
313 | goto out; | |
314 | ||
315 | ret = -EFAULT; | |
316 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) | |
317 | goto out; | |
318 | if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len)) | |
319 | goto out; | |
320 | ret = 0; | |
321 | ||
322 | out: | |
323 | kfree(data); | |
324 | return ret; | |
325 | } | |
326 | ||
327 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) | |
328 | { | |
329 | struct ethtool_eeprom eeprom; | |
330 | struct ethtool_ops *ops = dev->ethtool_ops; | |
331 | u8 *data; | |
332 | int ret; | |
333 | ||
334 | if (!ops->set_eeprom || !ops->get_eeprom_len) | |
335 | return -EOPNOTSUPP; | |
336 | ||
337 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) | |
338 | return -EFAULT; | |
339 | ||
340 | /* Check for wrap and zero */ | |
341 | if (eeprom.offset + eeprom.len <= eeprom.offset) | |
342 | return -EINVAL; | |
343 | ||
344 | /* Check for exceeding total eeprom len */ | |
345 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) | |
346 | return -EINVAL; | |
347 | ||
348 | data = kmalloc(eeprom.len, GFP_USER); | |
349 | if (!data) | |
350 | return -ENOMEM; | |
351 | ||
352 | ret = -EFAULT; | |
353 | if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len)) | |
354 | goto out; | |
355 | ||
356 | ret = ops->set_eeprom(dev, &eeprom, data); | |
357 | if (ret) | |
358 | goto out; | |
359 | ||
360 | if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len)) | |
361 | ret = -EFAULT; | |
362 | ||
363 | out: | |
364 | kfree(data); | |
365 | return ret; | |
366 | } | |
367 | ||
368 | static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr) | |
369 | { | |
370 | struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE }; | |
371 | ||
372 | if (!dev->ethtool_ops->get_coalesce) | |
373 | return -EOPNOTSUPP; | |
374 | ||
375 | dev->ethtool_ops->get_coalesce(dev, &coalesce); | |
376 | ||
377 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) | |
378 | return -EFAULT; | |
379 | return 0; | |
380 | } | |
381 | ||
382 | static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr) | |
383 | { | |
384 | struct ethtool_coalesce coalesce; | |
385 | ||
fa04ae5c | 386 | if (!dev->ethtool_ops->set_coalesce) |
1da177e4 LT |
387 | return -EOPNOTSUPP; |
388 | ||
389 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) | |
390 | return -EFAULT; | |
391 | ||
392 | return dev->ethtool_ops->set_coalesce(dev, &coalesce); | |
393 | } | |
394 | ||
395 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) | |
396 | { | |
397 | struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM }; | |
398 | ||
399 | if (!dev->ethtool_ops->get_ringparam) | |
400 | return -EOPNOTSUPP; | |
401 | ||
402 | dev->ethtool_ops->get_ringparam(dev, &ringparam); | |
403 | ||
404 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) | |
405 | return -EFAULT; | |
406 | return 0; | |
407 | } | |
408 | ||
409 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) | |
410 | { | |
411 | struct ethtool_ringparam ringparam; | |
412 | ||
413 | if (!dev->ethtool_ops->set_ringparam) | |
414 | return -EOPNOTSUPP; | |
415 | ||
416 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) | |
417 | return -EFAULT; | |
418 | ||
419 | return dev->ethtool_ops->set_ringparam(dev, &ringparam); | |
420 | } | |
421 | ||
422 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) | |
423 | { | |
424 | struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; | |
425 | ||
426 | if (!dev->ethtool_ops->get_pauseparam) | |
427 | return -EOPNOTSUPP; | |
428 | ||
429 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); | |
430 | ||
431 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) | |
432 | return -EFAULT; | |
433 | return 0; | |
434 | } | |
435 | ||
436 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) | |
437 | { | |
438 | struct ethtool_pauseparam pauseparam; | |
439 | ||
440 | if (!dev->ethtool_ops->get_pauseparam) | |
441 | return -EOPNOTSUPP; | |
442 | ||
443 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) | |
444 | return -EFAULT; | |
445 | ||
446 | return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); | |
447 | } | |
448 | ||
449 | static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr) | |
450 | { | |
451 | struct ethtool_value edata = { ETHTOOL_GRXCSUM }; | |
452 | ||
453 | if (!dev->ethtool_ops->get_rx_csum) | |
454 | return -EOPNOTSUPP; | |
455 | ||
456 | edata.data = dev->ethtool_ops->get_rx_csum(dev); | |
457 | ||
458 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
459 | return -EFAULT; | |
460 | return 0; | |
461 | } | |
462 | ||
463 | static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr) | |
464 | { | |
465 | struct ethtool_value edata; | |
466 | ||
467 | if (!dev->ethtool_ops->set_rx_csum) | |
468 | return -EOPNOTSUPP; | |
469 | ||
470 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
471 | return -EFAULT; | |
472 | ||
473 | dev->ethtool_ops->set_rx_csum(dev, edata.data); | |
474 | return 0; | |
475 | } | |
476 | ||
477 | static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr) | |
478 | { | |
479 | struct ethtool_value edata = { ETHTOOL_GTXCSUM }; | |
480 | ||
481 | if (!dev->ethtool_ops->get_tx_csum) | |
482 | return -EOPNOTSUPP; | |
483 | ||
484 | edata.data = dev->ethtool_ops->get_tx_csum(dev); | |
485 | ||
486 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
487 | return -EFAULT; | |
488 | return 0; | |
489 | } | |
490 | ||
491 | static int __ethtool_set_sg(struct net_device *dev, u32 data) | |
492 | { | |
493 | int err; | |
494 | ||
495 | if (!data && dev->ethtool_ops->set_tso) { | |
496 | err = dev->ethtool_ops->set_tso(dev, 0); | |
497 | if (err) | |
498 | return err; | |
499 | } | |
500 | ||
e89e9cf5 AR |
501 | if (!data && dev->ethtool_ops->set_ufo) { |
502 | err = dev->ethtool_ops->set_ufo(dev, 0); | |
503 | if (err) | |
504 | return err; | |
505 | } | |
1da177e4 LT |
506 | return dev->ethtool_ops->set_sg(dev, data); |
507 | } | |
508 | ||
509 | static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr) | |
510 | { | |
511 | struct ethtool_value edata; | |
512 | int err; | |
513 | ||
514 | if (!dev->ethtool_ops->set_tx_csum) | |
515 | return -EOPNOTSUPP; | |
516 | ||
517 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
518 | return -EFAULT; | |
519 | ||
520 | if (!edata.data && dev->ethtool_ops->set_sg) { | |
521 | err = __ethtool_set_sg(dev, 0); | |
522 | if (err) | |
523 | return err; | |
524 | } | |
525 | ||
526 | return dev->ethtool_ops->set_tx_csum(dev, edata.data); | |
527 | } | |
528 | ||
529 | static int ethtool_get_sg(struct net_device *dev, char __user *useraddr) | |
530 | { | |
531 | struct ethtool_value edata = { ETHTOOL_GSG }; | |
532 | ||
533 | if (!dev->ethtool_ops->get_sg) | |
534 | return -EOPNOTSUPP; | |
535 | ||
536 | edata.data = dev->ethtool_ops->get_sg(dev); | |
537 | ||
538 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
539 | return -EFAULT; | |
540 | return 0; | |
541 | } | |
542 | ||
543 | static int ethtool_set_sg(struct net_device *dev, char __user *useraddr) | |
544 | { | |
545 | struct ethtool_value edata; | |
546 | ||
547 | if (!dev->ethtool_ops->set_sg) | |
548 | return -EOPNOTSUPP; | |
549 | ||
550 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
551 | return -EFAULT; | |
552 | ||
553 | if (edata.data && | |
8648b305 | 554 | !(dev->features & NETIF_F_ALL_CSUM)) |
1da177e4 LT |
555 | return -EINVAL; |
556 | ||
557 | return __ethtool_set_sg(dev, edata.data); | |
558 | } | |
559 | ||
560 | static int ethtool_get_tso(struct net_device *dev, char __user *useraddr) | |
561 | { | |
562 | struct ethtool_value edata = { ETHTOOL_GTSO }; | |
563 | ||
564 | if (!dev->ethtool_ops->get_tso) | |
565 | return -EOPNOTSUPP; | |
566 | ||
567 | edata.data = dev->ethtool_ops->get_tso(dev); | |
568 | ||
569 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
570 | return -EFAULT; | |
571 | return 0; | |
572 | } | |
573 | ||
574 | static int ethtool_set_tso(struct net_device *dev, char __user *useraddr) | |
575 | { | |
576 | struct ethtool_value edata; | |
577 | ||
578 | if (!dev->ethtool_ops->set_tso) | |
579 | return -EOPNOTSUPP; | |
580 | ||
581 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
582 | return -EFAULT; | |
583 | ||
584 | if (edata.data && !(dev->features & NETIF_F_SG)) | |
585 | return -EINVAL; | |
586 | ||
587 | return dev->ethtool_ops->set_tso(dev, edata.data); | |
588 | } | |
589 | ||
e89e9cf5 AR |
590 | static int ethtool_get_ufo(struct net_device *dev, char __user *useraddr) |
591 | { | |
47552c4e | 592 | struct ethtool_value edata = { ETHTOOL_GUFO }; |
e89e9cf5 AR |
593 | |
594 | if (!dev->ethtool_ops->get_ufo) | |
595 | return -EOPNOTSUPP; | |
596 | edata.data = dev->ethtool_ops->get_ufo(dev); | |
597 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
598 | return -EFAULT; | |
599 | return 0; | |
600 | } | |
47552c4e | 601 | |
e89e9cf5 AR |
602 | static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr) |
603 | { | |
604 | struct ethtool_value edata; | |
605 | ||
606 | if (!dev->ethtool_ops->set_ufo) | |
607 | return -EOPNOTSUPP; | |
608 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
609 | return -EFAULT; | |
610 | if (edata.data && !(dev->features & NETIF_F_SG)) | |
611 | return -EINVAL; | |
612 | if (edata.data && !(dev->features & NETIF_F_HW_CSUM)) | |
613 | return -EINVAL; | |
614 | return dev->ethtool_ops->set_ufo(dev, edata.data); | |
615 | } | |
616 | ||
1da177e4 LT |
617 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
618 | { | |
619 | struct ethtool_test test; | |
620 | struct ethtool_ops *ops = dev->ethtool_ops; | |
621 | u64 *data; | |
622 | int ret; | |
623 | ||
624 | if (!ops->self_test || !ops->self_test_count) | |
625 | return -EOPNOTSUPP; | |
626 | ||
627 | if (copy_from_user(&test, useraddr, sizeof(test))) | |
628 | return -EFAULT; | |
629 | ||
630 | test.len = ops->self_test_count(dev); | |
631 | data = kmalloc(test.len * sizeof(u64), GFP_USER); | |
632 | if (!data) | |
633 | return -ENOMEM; | |
634 | ||
635 | ops->self_test(dev, &test, data); | |
636 | ||
637 | ret = -EFAULT; | |
638 | if (copy_to_user(useraddr, &test, sizeof(test))) | |
639 | goto out; | |
640 | useraddr += sizeof(test); | |
641 | if (copy_to_user(useraddr, data, test.len * sizeof(u64))) | |
642 | goto out; | |
643 | ret = 0; | |
644 | ||
645 | out: | |
646 | kfree(data); | |
647 | return ret; | |
648 | } | |
649 | ||
650 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) | |
651 | { | |
652 | struct ethtool_gstrings gstrings; | |
653 | struct ethtool_ops *ops = dev->ethtool_ops; | |
654 | u8 *data; | |
655 | int ret; | |
656 | ||
657 | if (!ops->get_strings) | |
658 | return -EOPNOTSUPP; | |
659 | ||
660 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) | |
661 | return -EFAULT; | |
662 | ||
663 | switch (gstrings.string_set) { | |
664 | case ETH_SS_TEST: | |
665 | if (!ops->self_test_count) | |
666 | return -EOPNOTSUPP; | |
667 | gstrings.len = ops->self_test_count(dev); | |
668 | break; | |
669 | case ETH_SS_STATS: | |
670 | if (!ops->get_stats_count) | |
671 | return -EOPNOTSUPP; | |
672 | gstrings.len = ops->get_stats_count(dev); | |
673 | break; | |
674 | default: | |
675 | return -EINVAL; | |
676 | } | |
677 | ||
678 | data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER); | |
679 | if (!data) | |
680 | return -ENOMEM; | |
681 | ||
682 | ops->get_strings(dev, gstrings.string_set, data); | |
683 | ||
684 | ret = -EFAULT; | |
685 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) | |
686 | goto out; | |
687 | useraddr += sizeof(gstrings); | |
688 | if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) | |
689 | goto out; | |
690 | ret = 0; | |
691 | ||
692 | out: | |
693 | kfree(data); | |
694 | return ret; | |
695 | } | |
696 | ||
697 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) | |
698 | { | |
699 | struct ethtool_value id; | |
700 | ||
701 | if (!dev->ethtool_ops->phys_id) | |
702 | return -EOPNOTSUPP; | |
703 | ||
704 | if (copy_from_user(&id, useraddr, sizeof(id))) | |
705 | return -EFAULT; | |
706 | ||
707 | return dev->ethtool_ops->phys_id(dev, id.data); | |
708 | } | |
709 | ||
710 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) | |
711 | { | |
712 | struct ethtool_stats stats; | |
713 | struct ethtool_ops *ops = dev->ethtool_ops; | |
714 | u64 *data; | |
715 | int ret; | |
716 | ||
717 | if (!ops->get_ethtool_stats || !ops->get_stats_count) | |
718 | return -EOPNOTSUPP; | |
719 | ||
720 | if (copy_from_user(&stats, useraddr, sizeof(stats))) | |
721 | return -EFAULT; | |
722 | ||
723 | stats.n_stats = ops->get_stats_count(dev); | |
724 | data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER); | |
725 | if (!data) | |
726 | return -ENOMEM; | |
727 | ||
728 | ops->get_ethtool_stats(dev, &stats, data); | |
729 | ||
730 | ret = -EFAULT; | |
731 | if (copy_to_user(useraddr, &stats, sizeof(stats))) | |
732 | goto out; | |
733 | useraddr += sizeof(stats); | |
734 | if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64))) | |
735 | goto out; | |
736 | ret = 0; | |
737 | ||
738 | out: | |
739 | kfree(data); | |
740 | return ret; | |
741 | } | |
742 | ||
0bf0519d | 743 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
a6f9a705 JW |
744 | { |
745 | struct ethtool_perm_addr epaddr; | |
746 | u8 *data; | |
747 | int ret; | |
748 | ||
749 | if (!dev->ethtool_ops->get_perm_addr) | |
750 | return -EOPNOTSUPP; | |
751 | ||
752 | if (copy_from_user(&epaddr,useraddr,sizeof(epaddr))) | |
753 | return -EFAULT; | |
754 | ||
755 | data = kmalloc(epaddr.size, GFP_USER); | |
756 | if (!data) | |
757 | return -ENOMEM; | |
758 | ||
759 | ret = dev->ethtool_ops->get_perm_addr(dev,&epaddr,data); | |
760 | if (ret) | |
761 | return ret; | |
762 | ||
763 | ret = -EFAULT; | |
764 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) | |
765 | goto out; | |
766 | useraddr += sizeof(epaddr); | |
767 | if (copy_to_user(useraddr, data, epaddr.size)) | |
768 | goto out; | |
769 | ret = 0; | |
770 | ||
771 | out: | |
772 | kfree(data); | |
773 | return ret; | |
774 | } | |
775 | ||
1da177e4 LT |
776 | /* The main entry point in this file. Called from net/core/dev.c */ |
777 | ||
778 | int dev_ethtool(struct ifreq *ifr) | |
779 | { | |
780 | struct net_device *dev = __dev_get_by_name(ifr->ifr_name); | |
781 | void __user *useraddr = ifr->ifr_data; | |
782 | u32 ethcmd; | |
783 | int rc; | |
81e81575 | 784 | unsigned long old_features; |
1da177e4 LT |
785 | |
786 | /* | |
787 | * XXX: This can be pushed down into the ethtool_* handlers that | |
788 | * need it. Keep existing behaviour for the moment. | |
789 | */ | |
790 | if (!capable(CAP_NET_ADMIN)) | |
791 | return -EPERM; | |
792 | ||
793 | if (!dev || !netif_device_present(dev)) | |
794 | return -ENODEV; | |
795 | ||
796 | if (!dev->ethtool_ops) | |
797 | goto ioctl; | |
798 | ||
799 | if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd))) | |
800 | return -EFAULT; | |
801 | ||
802 | if(dev->ethtool_ops->begin) | |
803 | if ((rc = dev->ethtool_ops->begin(dev)) < 0) | |
804 | return rc; | |
805 | ||
d8a33ac4 SH |
806 | old_features = dev->features; |
807 | ||
1da177e4 LT |
808 | switch (ethcmd) { |
809 | case ETHTOOL_GSET: | |
810 | rc = ethtool_get_settings(dev, useraddr); | |
811 | break; | |
812 | case ETHTOOL_SSET: | |
813 | rc = ethtool_set_settings(dev, useraddr); | |
814 | break; | |
815 | case ETHTOOL_GDRVINFO: | |
816 | rc = ethtool_get_drvinfo(dev, useraddr); | |
1da177e4 LT |
817 | break; |
818 | case ETHTOOL_GREGS: | |
819 | rc = ethtool_get_regs(dev, useraddr); | |
820 | break; | |
821 | case ETHTOOL_GWOL: | |
822 | rc = ethtool_get_wol(dev, useraddr); | |
823 | break; | |
824 | case ETHTOOL_SWOL: | |
825 | rc = ethtool_set_wol(dev, useraddr); | |
826 | break; | |
827 | case ETHTOOL_GMSGLVL: | |
828 | rc = ethtool_get_msglevel(dev, useraddr); | |
829 | break; | |
830 | case ETHTOOL_SMSGLVL: | |
831 | rc = ethtool_set_msglevel(dev, useraddr); | |
832 | break; | |
833 | case ETHTOOL_NWAY_RST: | |
834 | rc = ethtool_nway_reset(dev); | |
835 | break; | |
836 | case ETHTOOL_GLINK: | |
837 | rc = ethtool_get_link(dev, useraddr); | |
838 | break; | |
839 | case ETHTOOL_GEEPROM: | |
840 | rc = ethtool_get_eeprom(dev, useraddr); | |
841 | break; | |
842 | case ETHTOOL_SEEPROM: | |
843 | rc = ethtool_set_eeprom(dev, useraddr); | |
844 | break; | |
845 | case ETHTOOL_GCOALESCE: | |
846 | rc = ethtool_get_coalesce(dev, useraddr); | |
847 | break; | |
848 | case ETHTOOL_SCOALESCE: | |
849 | rc = ethtool_set_coalesce(dev, useraddr); | |
850 | break; | |
851 | case ETHTOOL_GRINGPARAM: | |
852 | rc = ethtool_get_ringparam(dev, useraddr); | |
853 | break; | |
854 | case ETHTOOL_SRINGPARAM: | |
855 | rc = ethtool_set_ringparam(dev, useraddr); | |
856 | break; | |
857 | case ETHTOOL_GPAUSEPARAM: | |
858 | rc = ethtool_get_pauseparam(dev, useraddr); | |
859 | break; | |
860 | case ETHTOOL_SPAUSEPARAM: | |
861 | rc = ethtool_set_pauseparam(dev, useraddr); | |
862 | break; | |
863 | case ETHTOOL_GRXCSUM: | |
864 | rc = ethtool_get_rx_csum(dev, useraddr); | |
865 | break; | |
866 | case ETHTOOL_SRXCSUM: | |
867 | rc = ethtool_set_rx_csum(dev, useraddr); | |
868 | break; | |
869 | case ETHTOOL_GTXCSUM: | |
870 | rc = ethtool_get_tx_csum(dev, useraddr); | |
871 | break; | |
872 | case ETHTOOL_STXCSUM: | |
873 | rc = ethtool_set_tx_csum(dev, useraddr); | |
874 | break; | |
875 | case ETHTOOL_GSG: | |
876 | rc = ethtool_get_sg(dev, useraddr); | |
877 | break; | |
878 | case ETHTOOL_SSG: | |
879 | rc = ethtool_set_sg(dev, useraddr); | |
880 | break; | |
881 | case ETHTOOL_GTSO: | |
882 | rc = ethtool_get_tso(dev, useraddr); | |
883 | break; | |
884 | case ETHTOOL_STSO: | |
885 | rc = ethtool_set_tso(dev, useraddr); | |
886 | break; | |
887 | case ETHTOOL_TEST: | |
888 | rc = ethtool_self_test(dev, useraddr); | |
889 | break; | |
890 | case ETHTOOL_GSTRINGS: | |
891 | rc = ethtool_get_strings(dev, useraddr); | |
892 | break; | |
893 | case ETHTOOL_PHYS_ID: | |
894 | rc = ethtool_phys_id(dev, useraddr); | |
895 | break; | |
896 | case ETHTOOL_GSTATS: | |
897 | rc = ethtool_get_stats(dev, useraddr); | |
898 | break; | |
a6f9a705 JW |
899 | case ETHTOOL_GPERMADDR: |
900 | rc = ethtool_get_perm_addr(dev, useraddr); | |
901 | break; | |
e89e9cf5 AR |
902 | case ETHTOOL_GUFO: |
903 | rc = ethtool_get_ufo(dev, useraddr); | |
904 | break; | |
905 | case ETHTOOL_SUFO: | |
906 | rc = ethtool_set_ufo(dev, useraddr); | |
907 | break; | |
1da177e4 LT |
908 | default: |
909 | rc = -EOPNOTSUPP; | |
910 | } | |
911 | ||
912 | if(dev->ethtool_ops->complete) | |
913 | dev->ethtool_ops->complete(dev); | |
d8a33ac4 SH |
914 | |
915 | if (old_features != dev->features) | |
916 | netdev_features_change(dev); | |
917 | ||
1da177e4 LT |
918 | return rc; |
919 | ||
920 | ioctl: | |
921 | if (dev->do_ioctl) | |
922 | return dev->do_ioctl(dev, ifr, SIOCETHTOOL); | |
923 | return -EOPNOTSUPP; | |
924 | } | |
925 | ||
926 | EXPORT_SYMBOL(dev_ethtool); | |
927 | EXPORT_SYMBOL(ethtool_op_get_link); | |
a6f9a705 | 928 | EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr); |
1da177e4 LT |
929 | EXPORT_SYMBOL(ethtool_op_get_sg); |
930 | EXPORT_SYMBOL(ethtool_op_get_tso); | |
931 | EXPORT_SYMBOL(ethtool_op_get_tx_csum); | |
932 | EXPORT_SYMBOL(ethtool_op_set_sg); | |
933 | EXPORT_SYMBOL(ethtool_op_set_tso); | |
934 | EXPORT_SYMBOL(ethtool_op_set_tx_csum); | |
69f6a0fa | 935 | EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum); |
e89e9cf5 AR |
936 | EXPORT_SYMBOL(ethtool_op_set_ufo); |
937 | EXPORT_SYMBOL(ethtool_op_get_ufo); |