]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/ethtool.c
net: fix ethtool coding style errors and warnings
[net-next-2.6.git] / net / core / ethtool.c
CommitLineData
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
61a44b9c 6 * the information ethtool needs.
1da177e4 7 *
61a44b9c
MW
8 * 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.
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/types.h>
4fc268d2 16#include <linux/capability.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
d17792eb 20#include <linux/bitops.h>
97f8aefb 21#include <linux/uaccess.h>
1da177e4 22
4ec93edb 23/*
1da177e4
LT
24 * Some useful ethtool_ops methods that're device independent.
25 * If we find that all drivers want to do the same thing here,
26 * we can turn these into dev_() function calls.
27 */
28
29u32 ethtool_op_get_link(struct net_device *dev)
30{
31 return netif_carrier_ok(dev) ? 1 : 0;
32}
97f8aefb 33EXPORT_SYMBOL(ethtool_op_get_link);
1da177e4 34
1896e61f
SS
35u32 ethtool_op_get_rx_csum(struct net_device *dev)
36{
37 return (dev->features & NETIF_F_ALL_CSUM) != 0;
38}
8a729fce 39EXPORT_SYMBOL(ethtool_op_get_rx_csum);
1896e61f 40
1da177e4
LT
41u32 ethtool_op_get_tx_csum(struct net_device *dev)
42{
8648b305 43 return (dev->features & NETIF_F_ALL_CSUM) != 0;
1da177e4 44}
8a729fce 45EXPORT_SYMBOL(ethtool_op_get_tx_csum);
1da177e4
LT
46
47int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
48{
49 if (data)
50 dev->features |= NETIF_F_IP_CSUM;
51 else
52 dev->features &= ~NETIF_F_IP_CSUM;
53
54 return 0;
55}
56
69f6a0fa
JM
57int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
58{
59 if (data)
60 dev->features |= NETIF_F_HW_CSUM;
61 else
62 dev->features &= ~NETIF_F_HW_CSUM;
63
64 return 0;
65}
97f8aefb 66EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
6460d948
MC
67
68int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
69{
70 if (data)
71 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
72 else
73 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
74
75 return 0;
76}
97f8aefb 77EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
6460d948 78
1da177e4
LT
79u32 ethtool_op_get_sg(struct net_device *dev)
80{
81 return (dev->features & NETIF_F_SG) != 0;
82}
97f8aefb 83EXPORT_SYMBOL(ethtool_op_get_sg);
1da177e4
LT
84
85int ethtool_op_set_sg(struct net_device *dev, u32 data)
86{
87 if (data)
88 dev->features |= NETIF_F_SG;
89 else
90 dev->features &= ~NETIF_F_SG;
91
92 return 0;
93}
97f8aefb 94EXPORT_SYMBOL(ethtool_op_set_sg);
1da177e4
LT
95
96u32 ethtool_op_get_tso(struct net_device *dev)
97{
98 return (dev->features & NETIF_F_TSO) != 0;
99}
97f8aefb 100EXPORT_SYMBOL(ethtool_op_get_tso);
1da177e4
LT
101
102int ethtool_op_set_tso(struct net_device *dev, u32 data)
103{
104 if (data)
105 dev->features |= NETIF_F_TSO;
106 else
107 dev->features &= ~NETIF_F_TSO;
108
109 return 0;
110}
97f8aefb 111EXPORT_SYMBOL(ethtool_op_set_tso);
1da177e4 112
e89e9cf5
AR
113u32 ethtool_op_get_ufo(struct net_device *dev)
114{
115 return (dev->features & NETIF_F_UFO) != 0;
116}
97f8aefb 117EXPORT_SYMBOL(ethtool_op_get_ufo);
e89e9cf5
AR
118
119int ethtool_op_set_ufo(struct net_device *dev, u32 data)
120{
121 if (data)
122 dev->features |= NETIF_F_UFO;
123 else
124 dev->features &= ~NETIF_F_UFO;
125 return 0;
126}
97f8aefb 127EXPORT_SYMBOL(ethtool_op_set_ufo);
e89e9cf5 128
3ae7c0b2
JG
129/* the following list of flags are the same as their associated
130 * NETIF_F_xxx values in include/linux/netdevice.h
131 */
132static const u32 flags_dup_features =
b00fabb4 133 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
3ae7c0b2
JG
134
135u32 ethtool_op_get_flags(struct net_device *dev)
136{
137 /* in the future, this function will probably contain additional
138 * handling for flags which are not so easily handled
139 * by a simple masking operation
140 */
141
142 return dev->features & flags_dup_features;
143}
97f8aefb 144EXPORT_SYMBOL(ethtool_op_get_flags);
3ae7c0b2
JG
145
146int ethtool_op_set_flags(struct net_device *dev, u32 data)
147{
0d643e1f 148 const struct ethtool_ops *ops = dev->ethtool_ops;
9675478b 149 unsigned long features = dev->features;
0d643e1f 150
3ae7c0b2 151 if (data & ETH_FLAG_LRO)
9675478b 152 features |= NETIF_F_LRO;
3ae7c0b2 153 else
9675478b 154 features &= ~NETIF_F_LRO;
3ae7c0b2 155
0d643e1f
PW
156 if (data & ETH_FLAG_NTUPLE) {
157 if (!ops->set_rx_ntuple)
158 return -EOPNOTSUPP;
9675478b 159 features |= NETIF_F_NTUPLE;
0d643e1f
PW
160 } else {
161 /* safe to clear regardless */
9675478b 162 features &= ~NETIF_F_NTUPLE;
0d643e1f 163 }
15682bc4 164
b00fabb4 165 if (data & ETH_FLAG_RXHASH)
166 features |= NETIF_F_RXHASH;
167 else
168 features &= ~NETIF_F_RXHASH;
169
9675478b 170 dev->features = features;
3ae7c0b2
JG
171 return 0;
172}
97f8aefb 173EXPORT_SYMBOL(ethtool_op_set_flags);
3ae7c0b2 174
15682bc4
PWJ
175void ethtool_ntuple_flush(struct net_device *dev)
176{
177 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
178
179 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
180 list_del(&fsc->list);
181 kfree(fsc);
182 }
183 dev->ethtool_ntuple_list.count = 0;
184}
185EXPORT_SYMBOL(ethtool_ntuple_flush);
186
1da177e4
LT
187/* Handlers for each ethtool command */
188
189static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
190{
8e557421 191 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
1da177e4
LT
192 int err;
193
194 if (!dev->ethtool_ops->get_settings)
195 return -EOPNOTSUPP;
196
197 err = dev->ethtool_ops->get_settings(dev, &cmd);
198 if (err < 0)
199 return err;
200
201 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
202 return -EFAULT;
203 return 0;
204}
205
206static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
207{
208 struct ethtool_cmd cmd;
209
210 if (!dev->ethtool_ops->set_settings)
211 return -EOPNOTSUPP;
212
213 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
214 return -EFAULT;
215
216 return dev->ethtool_ops->set_settings(dev, &cmd);
217}
218
97f8aefb 219static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
220 void __user *useraddr)
1da177e4
LT
221{
222 struct ethtool_drvinfo info;
76fd8593 223 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
224
225 if (!ops->get_drvinfo)
226 return -EOPNOTSUPP;
227
228 memset(&info, 0, sizeof(info));
229 info.cmd = ETHTOOL_GDRVINFO;
230 ops->get_drvinfo(dev, &info);
231
723b2f57
JG
232 /*
233 * this method of obtaining string set info is deprecated;
d17792eb 234 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 235 */
ff03d49f
JG
236 if (ops->get_sset_count) {
237 int rc;
238
239 rc = ops->get_sset_count(dev, ETH_SS_TEST);
240 if (rc >= 0)
241 info.testinfo_len = rc;
242 rc = ops->get_sset_count(dev, ETH_SS_STATS);
243 if (rc >= 0)
244 info.n_stats = rc;
339bf024
JG
245 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
246 if (rc >= 0)
247 info.n_priv_flags = rc;
ff03d49f 248 }
1da177e4
LT
249 if (ops->get_regs_len)
250 info.regdump_len = ops->get_regs_len(dev);
251 if (ops->get_eeprom_len)
252 info.eedump_len = ops->get_eeprom_len(dev);
253
254 if (copy_to_user(useraddr, &info, sizeof(info)))
255 return -EFAULT;
256 return 0;
257}
258
f5c445ed 259static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 260 void __user *useraddr)
723b2f57
JG
261{
262 struct ethtool_sset_info info;
263 const struct ethtool_ops *ops = dev->ethtool_ops;
264 u64 sset_mask;
265 int i, idx = 0, n_bits = 0, ret, rc;
266 u32 *info_buf = NULL;
267
268 if (!ops->get_sset_count)
269 return -EOPNOTSUPP;
270
271 if (copy_from_user(&info, useraddr, sizeof(info)))
272 return -EFAULT;
273
274 /* store copy of mask, because we zero struct later on */
275 sset_mask = info.sset_mask;
276 if (!sset_mask)
277 return 0;
278
279 /* calculate size of return buffer */
d17792eb 280 n_bits = hweight64(sset_mask);
723b2f57
JG
281
282 memset(&info, 0, sizeof(info));
283 info.cmd = ETHTOOL_GSSET_INFO;
284
285 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
286 if (!info_buf)
287 return -ENOMEM;
288
289 /*
290 * fill return buffer based on input bitmask and successful
291 * get_sset_count return
292 */
293 for (i = 0; i < 64; i++) {
294 if (!(sset_mask & (1ULL << i)))
295 continue;
296
297 rc = ops->get_sset_count(dev, i);
298 if (rc >= 0) {
299 info.sset_mask |= (1ULL << i);
300 info_buf[idx++] = rc;
301 }
302 }
303
304 ret = -EFAULT;
305 if (copy_to_user(useraddr, &info, sizeof(info)))
306 goto out;
307
308 useraddr += offsetof(struct ethtool_sset_info, data);
309 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
310 goto out;
311
312 ret = 0;
313
314out:
315 kfree(info_buf);
316 return ret;
317}
318
97f8aefb 319static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
320 void __user *useraddr)
0853ad66
SB
321{
322 struct ethtool_rxnfc cmd;
323
59089d8d 324 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
325 return -EOPNOTSUPP;
326
327 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
328 return -EFAULT;
329
59089d8d 330 return dev->ethtool_ops->set_rxnfc(dev, &cmd);
0853ad66
SB
331}
332
97f8aefb 333static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
334 void __user *useraddr)
0853ad66
SB
335{
336 struct ethtool_rxnfc info;
59089d8d
SB
337 const struct ethtool_ops *ops = dev->ethtool_ops;
338 int ret;
339 void *rule_buf = NULL;
0853ad66 340
59089d8d 341 if (!ops->get_rxnfc)
0853ad66
SB
342 return -EOPNOTSUPP;
343
344 if (copy_from_user(&info, useraddr, sizeof(info)))
345 return -EFAULT;
346
59089d8d
SB
347 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
348 if (info.rule_cnt > 0) {
349 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
350 GFP_USER);
351 if (!rule_buf)
352 return -ENOMEM;
353 }
354 }
0853ad66 355
59089d8d
SB
356 ret = ops->get_rxnfc(dev, &info, rule_buf);
357 if (ret < 0)
358 goto err_out;
359
360 ret = -EFAULT;
0853ad66 361 if (copy_to_user(useraddr, &info, sizeof(info)))
59089d8d
SB
362 goto err_out;
363
364 if (rule_buf) {
365 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
366 if (copy_to_user(useraddr, rule_buf,
367 info.rule_cnt * sizeof(u32)))
368 goto err_out;
369 }
370 ret = 0;
371
372err_out:
c9caceca 373 kfree(rule_buf);
59089d8d
SB
374
375 return ret;
0853ad66
SB
376}
377
e8589118 378static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
97f8aefb 379 struct ethtool_rx_ntuple_flow_spec *spec,
380 struct ethtool_rx_ntuple_flow_spec_container *fsc)
15682bc4 381{
15682bc4
PWJ
382
383 /* don't add filters forever */
e8589118
PW
384 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
385 /* free the container */
386 kfree(fsc);
387 return;
388 }
15682bc4
PWJ
389
390 /* Copy the whole filter over */
391 fsc->fs.flow_type = spec->flow_type;
392 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
393 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
394
395 fsc->fs.vlan_tag = spec->vlan_tag;
396 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
397 fsc->fs.data = spec->data;
398 fsc->fs.data_mask = spec->data_mask;
399 fsc->fs.action = spec->action;
400
401 /* add to the list */
402 list_add_tail_rcu(&fsc->list, &list->list);
403 list->count++;
15682bc4
PWJ
404}
405
97f8aefb 406static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
407 void __user *useraddr)
15682bc4
PWJ
408{
409 struct ethtool_rx_ntuple cmd;
410 const struct ethtool_ops *ops = dev->ethtool_ops;
e8589118 411 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
15682bc4
PWJ
412 int ret;
413
15682bc4
PWJ
414 if (!(dev->features & NETIF_F_NTUPLE))
415 return -EINVAL;
416
417 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
418 return -EFAULT;
419
15682bc4
PWJ
420 /*
421 * Cache filter in dev struct for GET operation only if
422 * the underlying driver doesn't have its own GET operation, and
e8589118
PW
423 * only if the filter was added successfully. First make sure we
424 * can allocate the filter, then continue if successful.
15682bc4 425 */
e8589118
PW
426 if (!ops->get_rx_ntuple) {
427 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
428 if (!fsc)
15682bc4 429 return -ENOMEM;
e8589118
PW
430 }
431
432 ret = ops->set_rx_ntuple(dev, &cmd);
433 if (ret) {
434 kfree(fsc);
435 return ret;
436 }
437
438 if (!ops->get_rx_ntuple)
439 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
15682bc4
PWJ
440
441 return ret;
442}
443
444static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
445{
446 struct ethtool_gstrings gstrings;
447 const struct ethtool_ops *ops = dev->ethtool_ops;
448 struct ethtool_rx_ntuple_flow_spec_container *fsc;
449 u8 *data;
450 char *p;
451 int ret, i, num_strings = 0;
452
453 if (!ops->get_sset_count)
454 return -EOPNOTSUPP;
455
456 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
457 return -EFAULT;
458
459 ret = ops->get_sset_count(dev, gstrings.string_set);
460 if (ret < 0)
461 return ret;
462
463 gstrings.len = ret;
464
465 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
466 if (!data)
467 return -ENOMEM;
468
469 if (ops->get_rx_ntuple) {
470 /* driver-specific filter grab */
471 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
472 goto copy;
473 }
474
475 /* default ethtool filter grab */
476 i = 0;
477 p = (char *)data;
478 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
479 sprintf(p, "Filter %d:\n", i);
480 p += ETH_GSTRING_LEN;
481 num_strings++;
482
483 switch (fsc->fs.flow_type) {
484 case TCP_V4_FLOW:
485 sprintf(p, "\tFlow Type: TCP\n");
486 p += ETH_GSTRING_LEN;
487 num_strings++;
488 break;
489 case UDP_V4_FLOW:
490 sprintf(p, "\tFlow Type: UDP\n");
491 p += ETH_GSTRING_LEN;
492 num_strings++;
493 break;
494 case SCTP_V4_FLOW:
495 sprintf(p, "\tFlow Type: SCTP\n");
496 p += ETH_GSTRING_LEN;
497 num_strings++;
498 break;
499 case AH_ESP_V4_FLOW:
500 sprintf(p, "\tFlow Type: AH ESP\n");
501 p += ETH_GSTRING_LEN;
502 num_strings++;
503 break;
504 case ESP_V4_FLOW:
505 sprintf(p, "\tFlow Type: ESP\n");
506 p += ETH_GSTRING_LEN;
507 num_strings++;
508 break;
509 case IP_USER_FLOW:
510 sprintf(p, "\tFlow Type: Raw IP\n");
511 p += ETH_GSTRING_LEN;
512 num_strings++;
513 break;
514 case IPV4_FLOW:
515 sprintf(p, "\tFlow Type: IPv4\n");
516 p += ETH_GSTRING_LEN;
517 num_strings++;
518 break;
519 default:
520 sprintf(p, "\tFlow Type: Unknown\n");
521 p += ETH_GSTRING_LEN;
522 num_strings++;
523 goto unknown_filter;
524 };
525
526 /* now the rest of the filters */
527 switch (fsc->fs.flow_type) {
528 case TCP_V4_FLOW:
529 case UDP_V4_FLOW:
530 case SCTP_V4_FLOW:
531 sprintf(p, "\tSrc IP addr: 0x%x\n",
97f8aefb 532 fsc->fs.h_u.tcp_ip4_spec.ip4src);
15682bc4
PWJ
533 p += ETH_GSTRING_LEN;
534 num_strings++;
535 sprintf(p, "\tSrc IP mask: 0x%x\n",
97f8aefb 536 fsc->fs.m_u.tcp_ip4_spec.ip4src);
15682bc4
PWJ
537 p += ETH_GSTRING_LEN;
538 num_strings++;
539 sprintf(p, "\tDest IP addr: 0x%x\n",
97f8aefb 540 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
15682bc4
PWJ
541 p += ETH_GSTRING_LEN;
542 num_strings++;
543 sprintf(p, "\tDest IP mask: 0x%x\n",
97f8aefb 544 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
15682bc4
PWJ
545 p += ETH_GSTRING_LEN;
546 num_strings++;
547 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
97f8aefb 548 fsc->fs.h_u.tcp_ip4_spec.psrc,
549 fsc->fs.m_u.tcp_ip4_spec.psrc);
15682bc4
PWJ
550 p += ETH_GSTRING_LEN;
551 num_strings++;
552 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
97f8aefb 553 fsc->fs.h_u.tcp_ip4_spec.pdst,
554 fsc->fs.m_u.tcp_ip4_spec.pdst);
15682bc4
PWJ
555 p += ETH_GSTRING_LEN;
556 num_strings++;
557 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
97f8aefb 558 fsc->fs.h_u.tcp_ip4_spec.tos,
559 fsc->fs.m_u.tcp_ip4_spec.tos);
15682bc4
PWJ
560 p += ETH_GSTRING_LEN;
561 num_strings++;
562 break;
563 case AH_ESP_V4_FLOW:
564 case ESP_V4_FLOW:
565 sprintf(p, "\tSrc IP addr: 0x%x\n",
97f8aefb 566 fsc->fs.h_u.ah_ip4_spec.ip4src);
15682bc4
PWJ
567 p += ETH_GSTRING_LEN;
568 num_strings++;
569 sprintf(p, "\tSrc IP mask: 0x%x\n",
97f8aefb 570 fsc->fs.m_u.ah_ip4_spec.ip4src);
15682bc4
PWJ
571 p += ETH_GSTRING_LEN;
572 num_strings++;
573 sprintf(p, "\tDest IP addr: 0x%x\n",
97f8aefb 574 fsc->fs.h_u.ah_ip4_spec.ip4dst);
15682bc4
PWJ
575 p += ETH_GSTRING_LEN;
576 num_strings++;
577 sprintf(p, "\tDest IP mask: 0x%x\n",
97f8aefb 578 fsc->fs.m_u.ah_ip4_spec.ip4dst);
15682bc4
PWJ
579 p += ETH_GSTRING_LEN;
580 num_strings++;
581 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
97f8aefb 582 fsc->fs.h_u.ah_ip4_spec.spi,
583 fsc->fs.m_u.ah_ip4_spec.spi);
15682bc4
PWJ
584 p += ETH_GSTRING_LEN;
585 num_strings++;
586 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
97f8aefb 587 fsc->fs.h_u.ah_ip4_spec.tos,
588 fsc->fs.m_u.ah_ip4_spec.tos);
15682bc4
PWJ
589 p += ETH_GSTRING_LEN;
590 num_strings++;
591 break;
592 case IP_USER_FLOW:
593 sprintf(p, "\tSrc IP addr: 0x%x\n",
97f8aefb 594 fsc->fs.h_u.raw_ip4_spec.ip4src);
15682bc4
PWJ
595 p += ETH_GSTRING_LEN;
596 num_strings++;
597 sprintf(p, "\tSrc IP mask: 0x%x\n",
97f8aefb 598 fsc->fs.m_u.raw_ip4_spec.ip4src);
15682bc4
PWJ
599 p += ETH_GSTRING_LEN;
600 num_strings++;
601 sprintf(p, "\tDest IP addr: 0x%x\n",
97f8aefb 602 fsc->fs.h_u.raw_ip4_spec.ip4dst);
15682bc4
PWJ
603 p += ETH_GSTRING_LEN;
604 num_strings++;
605 sprintf(p, "\tDest IP mask: 0x%x\n",
97f8aefb 606 fsc->fs.m_u.raw_ip4_spec.ip4dst);
15682bc4
PWJ
607 p += ETH_GSTRING_LEN;
608 num_strings++;
609 break;
610 case IPV4_FLOW:
611 sprintf(p, "\tSrc IP addr: 0x%x\n",
97f8aefb 612 fsc->fs.h_u.usr_ip4_spec.ip4src);
15682bc4
PWJ
613 p += ETH_GSTRING_LEN;
614 num_strings++;
615 sprintf(p, "\tSrc IP mask: 0x%x\n",
97f8aefb 616 fsc->fs.m_u.usr_ip4_spec.ip4src);
15682bc4
PWJ
617 p += ETH_GSTRING_LEN;
618 num_strings++;
619 sprintf(p, "\tDest IP addr: 0x%x\n",
97f8aefb 620 fsc->fs.h_u.usr_ip4_spec.ip4dst);
15682bc4
PWJ
621 p += ETH_GSTRING_LEN;
622 num_strings++;
623 sprintf(p, "\tDest IP mask: 0x%x\n",
97f8aefb 624 fsc->fs.m_u.usr_ip4_spec.ip4dst);
15682bc4
PWJ
625 p += ETH_GSTRING_LEN;
626 num_strings++;
627 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
97f8aefb 628 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
629 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
15682bc4
PWJ
630 p += ETH_GSTRING_LEN;
631 num_strings++;
632 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
97f8aefb 633 fsc->fs.h_u.usr_ip4_spec.tos,
634 fsc->fs.m_u.usr_ip4_spec.tos);
15682bc4
PWJ
635 p += ETH_GSTRING_LEN;
636 num_strings++;
637 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
97f8aefb 638 fsc->fs.h_u.usr_ip4_spec.ip_ver,
639 fsc->fs.m_u.usr_ip4_spec.ip_ver);
15682bc4
PWJ
640 p += ETH_GSTRING_LEN;
641 num_strings++;
642 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
97f8aefb 643 fsc->fs.h_u.usr_ip4_spec.proto,
644 fsc->fs.m_u.usr_ip4_spec.proto);
15682bc4
PWJ
645 p += ETH_GSTRING_LEN;
646 num_strings++;
647 break;
648 };
649 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
97f8aefb 650 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
15682bc4
PWJ
651 p += ETH_GSTRING_LEN;
652 num_strings++;
653 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
654 p += ETH_GSTRING_LEN;
655 num_strings++;
656 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
657 p += ETH_GSTRING_LEN;
658 num_strings++;
659 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
660 sprintf(p, "\tAction: Drop\n");
661 else
662 sprintf(p, "\tAction: Direct to queue %d\n",
97f8aefb 663 fsc->fs.action);
15682bc4
PWJ
664 p += ETH_GSTRING_LEN;
665 num_strings++;
666unknown_filter:
667 i++;
668 }
669copy:
670 /* indicate to userspace how many strings we actually have */
671 gstrings.len = num_strings;
672 ret = -EFAULT;
673 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
674 goto out;
675 useraddr += sizeof(gstrings);
676 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
677 goto out;
678 ret = 0;
679
680out:
681 kfree(data);
682 return ret;
683}
684
1da177e4
LT
685static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
686{
687 struct ethtool_regs regs;
76fd8593 688 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
689 void *regbuf;
690 int reglen, ret;
691
692 if (!ops->get_regs || !ops->get_regs_len)
693 return -EOPNOTSUPP;
694
695 if (copy_from_user(&regs, useraddr, sizeof(regs)))
696 return -EFAULT;
697
698 reglen = ops->get_regs_len(dev);
699 if (regs.len > reglen)
700 regs.len = reglen;
701
702 regbuf = kmalloc(reglen, GFP_USER);
703 if (!regbuf)
704 return -ENOMEM;
705
706 ops->get_regs(dev, &regs, regbuf);
707
708 ret = -EFAULT;
709 if (copy_to_user(useraddr, &regs, sizeof(regs)))
710 goto out;
711 useraddr += offsetof(struct ethtool_regs, data);
712 if (copy_to_user(useraddr, regbuf, regs.len))
713 goto out;
714 ret = 0;
715
716 out:
717 kfree(regbuf);
718 return ret;
719}
720
d73d3a8c
BH
721static int ethtool_reset(struct net_device *dev, char __user *useraddr)
722{
723 struct ethtool_value reset;
724 int ret;
725
726 if (!dev->ethtool_ops->reset)
727 return -EOPNOTSUPP;
728
729 if (copy_from_user(&reset, useraddr, sizeof(reset)))
730 return -EFAULT;
731
732 ret = dev->ethtool_ops->reset(dev, &reset.data);
733 if (ret)
734 return ret;
735
736 if (copy_to_user(useraddr, &reset, sizeof(reset)))
737 return -EFAULT;
738 return 0;
739}
740
1da177e4
LT
741static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
742{
8e557421 743 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1da177e4
LT
744
745 if (!dev->ethtool_ops->get_wol)
746 return -EOPNOTSUPP;
747
748 dev->ethtool_ops->get_wol(dev, &wol);
749
750 if (copy_to_user(useraddr, &wol, sizeof(wol)))
751 return -EFAULT;
752 return 0;
753}
754
755static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
756{
757 struct ethtool_wolinfo wol;
758
759 if (!dev->ethtool_ops->set_wol)
760 return -EOPNOTSUPP;
761
762 if (copy_from_user(&wol, useraddr, sizeof(wol)))
763 return -EFAULT;
764
765 return dev->ethtool_ops->set_wol(dev, &wol);
766}
767
1da177e4
LT
768static int ethtool_nway_reset(struct net_device *dev)
769{
770 if (!dev->ethtool_ops->nway_reset)
771 return -EOPNOTSUPP;
772
773 return dev->ethtool_ops->nway_reset(dev);
774}
775
1da177e4
LT
776static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
777{
778 struct ethtool_eeprom eeprom;
76fd8593 779 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
780 void __user *userbuf = useraddr + sizeof(eeprom);
781 u32 bytes_remaining;
1da177e4 782 u8 *data;
b131dd5d 783 int ret = 0;
1da177e4
LT
784
785 if (!ops->get_eeprom || !ops->get_eeprom_len)
786 return -EOPNOTSUPP;
787
788 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
789 return -EFAULT;
790
791 /* Check for wrap and zero */
792 if (eeprom.offset + eeprom.len <= eeprom.offset)
793 return -EINVAL;
794
795 /* Check for exceeding total eeprom len */
796 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
797 return -EINVAL;
798
b131dd5d 799 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
800 if (!data)
801 return -ENOMEM;
802
b131dd5d
MSB
803 bytes_remaining = eeprom.len;
804 while (bytes_remaining > 0) {
805 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
806
807 ret = ops->get_eeprom(dev, &eeprom, data);
808 if (ret)
809 break;
810 if (copy_to_user(userbuf, data, eeprom.len)) {
811 ret = -EFAULT;
812 break;
813 }
814 userbuf += eeprom.len;
815 eeprom.offset += eeprom.len;
816 bytes_remaining -= eeprom.len;
817 }
1da177e4 818
c5835df9
MSB
819 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
820 eeprom.offset -= eeprom.len;
821 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
822 ret = -EFAULT;
823
1da177e4
LT
824 kfree(data);
825 return ret;
826}
827
828static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
829{
830 struct ethtool_eeprom eeprom;
76fd8593 831 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
832 void __user *userbuf = useraddr + sizeof(eeprom);
833 u32 bytes_remaining;
1da177e4 834 u8 *data;
b131dd5d 835 int ret = 0;
1da177e4
LT
836
837 if (!ops->set_eeprom || !ops->get_eeprom_len)
838 return -EOPNOTSUPP;
839
840 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
841 return -EFAULT;
842
843 /* Check for wrap and zero */
844 if (eeprom.offset + eeprom.len <= eeprom.offset)
845 return -EINVAL;
846
847 /* Check for exceeding total eeprom len */
848 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
849 return -EINVAL;
850
b131dd5d 851 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
852 if (!data)
853 return -ENOMEM;
854
b131dd5d
MSB
855 bytes_remaining = eeprom.len;
856 while (bytes_remaining > 0) {
857 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
858
859 if (copy_from_user(data, userbuf, eeprom.len)) {
860 ret = -EFAULT;
861 break;
862 }
863 ret = ops->set_eeprom(dev, &eeprom, data);
864 if (ret)
865 break;
866 userbuf += eeprom.len;
867 eeprom.offset += eeprom.len;
868 bytes_remaining -= eeprom.len;
869 }
1da177e4 870
1da177e4
LT
871 kfree(data);
872 return ret;
873}
874
97f8aefb 875static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
876 void __user *useraddr)
1da177e4 877{
8e557421 878 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
879
880 if (!dev->ethtool_ops->get_coalesce)
881 return -EOPNOTSUPP;
882
883 dev->ethtool_ops->get_coalesce(dev, &coalesce);
884
885 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
886 return -EFAULT;
887 return 0;
888}
889
97f8aefb 890static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
891 void __user *useraddr)
1da177e4
LT
892{
893 struct ethtool_coalesce coalesce;
894
fa04ae5c 895 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
896 return -EOPNOTSUPP;
897
898 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
899 return -EFAULT;
900
901 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
902}
903
904static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
905{
8e557421 906 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
907
908 if (!dev->ethtool_ops->get_ringparam)
909 return -EOPNOTSUPP;
910
911 dev->ethtool_ops->get_ringparam(dev, &ringparam);
912
913 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
914 return -EFAULT;
915 return 0;
916}
917
918static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
919{
920 struct ethtool_ringparam ringparam;
921
922 if (!dev->ethtool_ops->set_ringparam)
923 return -EOPNOTSUPP;
924
925 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
926 return -EFAULT;
927
928 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
929}
930
931static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
932{
933 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
934
935 if (!dev->ethtool_ops->get_pauseparam)
936 return -EOPNOTSUPP;
937
938 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
939
940 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
941 return -EFAULT;
942 return 0;
943}
944
945static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
946{
947 struct ethtool_pauseparam pauseparam;
948
e1b90c41 949 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
950 return -EOPNOTSUPP;
951
952 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
953 return -EFAULT;
954
955 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
956}
957
1da177e4
LT
958static int __ethtool_set_sg(struct net_device *dev, u32 data)
959{
960 int err;
961
962 if (!data && dev->ethtool_ops->set_tso) {
963 err = dev->ethtool_ops->set_tso(dev, 0);
964 if (err)
965 return err;
966 }
967
e89e9cf5
AR
968 if (!data && dev->ethtool_ops->set_ufo) {
969 err = dev->ethtool_ops->set_ufo(dev, 0);
970 if (err)
971 return err;
972 }
1da177e4
LT
973 return dev->ethtool_ops->set_sg(dev, data);
974}
975
976static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
977{
978 struct ethtool_value edata;
979 int err;
980
981 if (!dev->ethtool_ops->set_tx_csum)
982 return -EOPNOTSUPP;
983
984 if (copy_from_user(&edata, useraddr, sizeof(edata)))
985 return -EFAULT;
986
987 if (!edata.data && dev->ethtool_ops->set_sg) {
988 err = __ethtool_set_sg(dev, 0);
989 if (err)
990 return err;
991 }
992
993 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
994}
97f8aefb 995EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1da177e4 996
b240a0e5
HX
997static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
998{
999 struct ethtool_value edata;
1000
1001 if (!dev->ethtool_ops->set_rx_csum)
1002 return -EOPNOTSUPP;
1003
1004 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1005 return -EFAULT;
1006
1007 if (!edata.data && dev->ethtool_ops->set_sg)
1008 dev->features &= ~NETIF_F_GRO;
1009
1010 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1011}
1012
1da177e4
LT
1013static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1014{
1015 struct ethtool_value edata;
1016
1017 if (!dev->ethtool_ops->set_sg)
1018 return -EOPNOTSUPP;
1019
1020 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1021 return -EFAULT;
1022
4ec93edb 1023 if (edata.data &&
8648b305 1024 !(dev->features & NETIF_F_ALL_CSUM))
1da177e4
LT
1025 return -EINVAL;
1026
1027 return __ethtool_set_sg(dev, edata.data);
1028}
1029
1da177e4
LT
1030static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1031{
1032 struct ethtool_value edata;
1033
1034 if (!dev->ethtool_ops->set_tso)
1035 return -EOPNOTSUPP;
1036
1037 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1038 return -EFAULT;
1039
1040 if (edata.data && !(dev->features & NETIF_F_SG))
1041 return -EINVAL;
1042
1043 return dev->ethtool_ops->set_tso(dev, edata.data);
1044}
1045
e89e9cf5
AR
1046static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1047{
1048 struct ethtool_value edata;
1049
1050 if (!dev->ethtool_ops->set_ufo)
1051 return -EOPNOTSUPP;
1052 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1053 return -EFAULT;
1054 if (edata.data && !(dev->features & NETIF_F_SG))
1055 return -EINVAL;
1056 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1057 return -EINVAL;
1058 return dev->ethtool_ops->set_ufo(dev, edata.data);
1059}
1060
37c3185a
HX
1061static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1062{
1063 struct ethtool_value edata = { ETHTOOL_GGSO };
1064
1065 edata.data = dev->features & NETIF_F_GSO;
1066 if (copy_to_user(useraddr, &edata, sizeof(edata)))
97f8aefb 1067 return -EFAULT;
37c3185a
HX
1068 return 0;
1069}
1070
1071static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1072{
1073 struct ethtool_value edata;
1074
1075 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1076 return -EFAULT;
1077 if (edata.data)
1078 dev->features |= NETIF_F_GSO;
1079 else
1080 dev->features &= ~NETIF_F_GSO;
1081 return 0;
1082}
1083
b240a0e5
HX
1084static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1085{
1086 struct ethtool_value edata = { ETHTOOL_GGRO };
1087
1088 edata.data = dev->features & NETIF_F_GRO;
1089 if (copy_to_user(useraddr, &edata, sizeof(edata)))
97f8aefb 1090 return -EFAULT;
b240a0e5
HX
1091 return 0;
1092}
1093
1094static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1095{
1096 struct ethtool_value edata;
1097
1098 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1099 return -EFAULT;
1100
1101 if (edata.data) {
1102 if (!dev->ethtool_ops->get_rx_csum ||
1103 !dev->ethtool_ops->get_rx_csum(dev))
1104 return -EINVAL;
1105 dev->features |= NETIF_F_GRO;
1106 } else
1107 dev->features &= ~NETIF_F_GRO;
1108
1109 return 0;
1110}
1111
1da177e4
LT
1112static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1113{
1114 struct ethtool_test test;
76fd8593 1115 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1116 u64 *data;
ff03d49f 1117 int ret, test_len;
1da177e4 1118
a9828ec6 1119 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
1120 return -EOPNOTSUPP;
1121
a9828ec6 1122 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
1123 if (test_len < 0)
1124 return test_len;
1125 WARN_ON(test_len == 0);
1126
1da177e4
LT
1127 if (copy_from_user(&test, useraddr, sizeof(test)))
1128 return -EFAULT;
1129
ff03d49f
JG
1130 test.len = test_len;
1131 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
1132 if (!data)
1133 return -ENOMEM;
1134
1135 ops->self_test(dev, &test, data);
1136
1137 ret = -EFAULT;
1138 if (copy_to_user(useraddr, &test, sizeof(test)))
1139 goto out;
1140 useraddr += sizeof(test);
1141 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1142 goto out;
1143 ret = 0;
1144
1145 out:
1146 kfree(data);
1147 return ret;
1148}
1149
1150static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1151{
1152 struct ethtool_gstrings gstrings;
76fd8593 1153 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
1154 u8 *data;
1155 int ret;
1156
a9828ec6 1157 if (!ops->get_strings || !ops->get_sset_count)
1da177e4
LT
1158 return -EOPNOTSUPP;
1159
1160 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1161 return -EFAULT;
1162
a9828ec6
BH
1163 ret = ops->get_sset_count(dev, gstrings.string_set);
1164 if (ret < 0)
1165 return ret;
1166
1167 gstrings.len = ret;
1da177e4
LT
1168
1169 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1170 if (!data)
1171 return -ENOMEM;
1172
1173 ops->get_strings(dev, gstrings.string_set, data);
1174
1175 ret = -EFAULT;
1176 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1177 goto out;
1178 useraddr += sizeof(gstrings);
1179 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1180 goto out;
1181 ret = 0;
1182
1183 out:
1184 kfree(data);
1185 return ret;
1186}
1187
1188static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1189{
1190 struct ethtool_value id;
1191
1192 if (!dev->ethtool_ops->phys_id)
1193 return -EOPNOTSUPP;
1194
1195 if (copy_from_user(&id, useraddr, sizeof(id)))
1196 return -EFAULT;
1197
1198 return dev->ethtool_ops->phys_id(dev, id.data);
1199}
1200
1201static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1202{
1203 struct ethtool_stats stats;
76fd8593 1204 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1205 u64 *data;
ff03d49f 1206 int ret, n_stats;
1da177e4 1207
a9828ec6 1208 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1209 return -EOPNOTSUPP;
1210
a9828ec6 1211 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1212 if (n_stats < 0)
1213 return n_stats;
1214 WARN_ON(n_stats == 0);
1215
1da177e4
LT
1216 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1217 return -EFAULT;
1218
ff03d49f
JG
1219 stats.n_stats = n_stats;
1220 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1da177e4
LT
1221 if (!data)
1222 return -ENOMEM;
1223
1224 ops->get_ethtool_stats(dev, &stats, data);
1225
1226 ret = -EFAULT;
1227 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1228 goto out;
1229 useraddr += sizeof(stats);
1230 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1231 goto out;
1232 ret = 0;
1233
1234 out:
1235 kfree(data);
1236 return ret;
1237}
1238
0bf0519d 1239static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
1240{
1241 struct ethtool_perm_addr epaddr;
a6f9a705 1242
313674af 1243 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
1244 return -EFAULT;
1245
313674af
MW
1246 if (epaddr.size < dev->addr_len)
1247 return -ETOOSMALL;
1248 epaddr.size = dev->addr_len;
a6f9a705 1249
a6f9a705 1250 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 1251 return -EFAULT;
a6f9a705 1252 useraddr += sizeof(epaddr);
313674af
MW
1253 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1254 return -EFAULT;
1255 return 0;
a6f9a705
JW
1256}
1257
13c99b24
JG
1258static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1259 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 1260{
8e557421 1261 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 1262
13c99b24 1263 if (!actor)
3ae7c0b2
JG
1264 return -EOPNOTSUPP;
1265
13c99b24 1266 edata.data = actor(dev);
3ae7c0b2
JG
1267
1268 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1269 return -EFAULT;
1270 return 0;
1271}
1272
13c99b24
JG
1273static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1274 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
1275{
1276 struct ethtool_value edata;
1277
13c99b24 1278 if (!actor)
3ae7c0b2
JG
1279 return -EOPNOTSUPP;
1280
1281 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1282 return -EFAULT;
1283
13c99b24 1284 actor(dev, edata.data);
339bf024
JG
1285 return 0;
1286}
1287
13c99b24
JG
1288static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1289 int (*actor)(struct net_device *, u32))
339bf024
JG
1290{
1291 struct ethtool_value edata;
1292
13c99b24 1293 if (!actor)
339bf024
JG
1294 return -EOPNOTSUPP;
1295
1296 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1297 return -EFAULT;
1298
13c99b24 1299 return actor(dev, edata.data);
339bf024
JG
1300}
1301
97f8aefb 1302static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1303 char __user *useraddr)
05c6a8d7
AK
1304{
1305 struct ethtool_flash efl;
1306
1307 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1308 return -EFAULT;
1309
1310 if (!dev->ethtool_ops->flash_device)
1311 return -EOPNOTSUPP;
1312
1313 return dev->ethtool_ops->flash_device(dev, &efl);
1314}
1315
1da177e4
LT
1316/* The main entry point in this file. Called from net/core/dev.c */
1317
881d966b 1318int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 1319{
881d966b 1320 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4
LT
1321 void __user *useraddr = ifr->ifr_data;
1322 u32 ethcmd;
1323 int rc;
81e81575 1324 unsigned long old_features;
1da177e4 1325
1da177e4
LT
1326 if (!dev || !netif_device_present(dev))
1327 return -ENODEV;
1328
1329 if (!dev->ethtool_ops)
61a44b9c 1330 return -EOPNOTSUPP;
1da177e4 1331
97f8aefb 1332 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
1333 return -EFAULT;
1334
75f3123c 1335 /* Allow some commands to be done by anyone */
97f8aefb 1336 switch (ethcmd) {
75f3123c 1337 case ETHTOOL_GDRVINFO:
75f3123c 1338 case ETHTOOL_GMSGLVL:
75f3123c
SH
1339 case ETHTOOL_GCOALESCE:
1340 case ETHTOOL_GRINGPARAM:
1341 case ETHTOOL_GPAUSEPARAM:
1342 case ETHTOOL_GRXCSUM:
1343 case ETHTOOL_GTXCSUM:
1344 case ETHTOOL_GSG:
1345 case ETHTOOL_GSTRINGS:
75f3123c
SH
1346 case ETHTOOL_GTSO:
1347 case ETHTOOL_GPERMADDR:
1348 case ETHTOOL_GUFO:
1349 case ETHTOOL_GGSO:
1cab819b 1350 case ETHTOOL_GGRO:
339bf024
JG
1351 case ETHTOOL_GFLAGS:
1352 case ETHTOOL_GPFLAGS:
0853ad66 1353 case ETHTOOL_GRXFH:
59089d8d
SB
1354 case ETHTOOL_GRXRINGS:
1355 case ETHTOOL_GRXCLSRLCNT:
1356 case ETHTOOL_GRXCLSRULE:
1357 case ETHTOOL_GRXCLSRLALL:
75f3123c
SH
1358 break;
1359 default:
1360 if (!capable(CAP_NET_ADMIN))
1361 return -EPERM;
1362 }
1363
97f8aefb 1364 if (dev->ethtool_ops->begin) {
1365 rc = dev->ethtool_ops->begin(dev);
1366 if (rc < 0)
1da177e4 1367 return rc;
97f8aefb 1368 }
d8a33ac4
SH
1369 old_features = dev->features;
1370
1da177e4
LT
1371 switch (ethcmd) {
1372 case ETHTOOL_GSET:
1373 rc = ethtool_get_settings(dev, useraddr);
1374 break;
1375 case ETHTOOL_SSET:
1376 rc = ethtool_set_settings(dev, useraddr);
1377 break;
1378 case ETHTOOL_GDRVINFO:
1379 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
1380 break;
1381 case ETHTOOL_GREGS:
1382 rc = ethtool_get_regs(dev, useraddr);
1383 break;
1384 case ETHTOOL_GWOL:
1385 rc = ethtool_get_wol(dev, useraddr);
1386 break;
1387 case ETHTOOL_SWOL:
1388 rc = ethtool_set_wol(dev, useraddr);
1389 break;
1390 case ETHTOOL_GMSGLVL:
13c99b24
JG
1391 rc = ethtool_get_value(dev, useraddr, ethcmd,
1392 dev->ethtool_ops->get_msglevel);
1da177e4
LT
1393 break;
1394 case ETHTOOL_SMSGLVL:
13c99b24
JG
1395 rc = ethtool_set_value_void(dev, useraddr,
1396 dev->ethtool_ops->set_msglevel);
1da177e4
LT
1397 break;
1398 case ETHTOOL_NWAY_RST:
1399 rc = ethtool_nway_reset(dev);
1400 break;
1401 case ETHTOOL_GLINK:
13c99b24
JG
1402 rc = ethtool_get_value(dev, useraddr, ethcmd,
1403 dev->ethtool_ops->get_link);
1da177e4
LT
1404 break;
1405 case ETHTOOL_GEEPROM:
1406 rc = ethtool_get_eeprom(dev, useraddr);
1407 break;
1408 case ETHTOOL_SEEPROM:
1409 rc = ethtool_set_eeprom(dev, useraddr);
1410 break;
1411 case ETHTOOL_GCOALESCE:
1412 rc = ethtool_get_coalesce(dev, useraddr);
1413 break;
1414 case ETHTOOL_SCOALESCE:
1415 rc = ethtool_set_coalesce(dev, useraddr);
1416 break;
1417 case ETHTOOL_GRINGPARAM:
1418 rc = ethtool_get_ringparam(dev, useraddr);
1419 break;
1420 case ETHTOOL_SRINGPARAM:
1421 rc = ethtool_set_ringparam(dev, useraddr);
1422 break;
1423 case ETHTOOL_GPAUSEPARAM:
1424 rc = ethtool_get_pauseparam(dev, useraddr);
1425 break;
1426 case ETHTOOL_SPAUSEPARAM:
1427 rc = ethtool_set_pauseparam(dev, useraddr);
1428 break;
1429 case ETHTOOL_GRXCSUM:
13c99b24 1430 rc = ethtool_get_value(dev, useraddr, ethcmd,
1896e61f
SS
1431 (dev->ethtool_ops->get_rx_csum ?
1432 dev->ethtool_ops->get_rx_csum :
1433 ethtool_op_get_rx_csum));
1da177e4
LT
1434 break;
1435 case ETHTOOL_SRXCSUM:
b240a0e5 1436 rc = ethtool_set_rx_csum(dev, useraddr);
1da177e4
LT
1437 break;
1438 case ETHTOOL_GTXCSUM:
13c99b24 1439 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1440 (dev->ethtool_ops->get_tx_csum ?
1441 dev->ethtool_ops->get_tx_csum :
1442 ethtool_op_get_tx_csum));
1da177e4
LT
1443 break;
1444 case ETHTOOL_STXCSUM:
1445 rc = ethtool_set_tx_csum(dev, useraddr);
1446 break;
1447 case ETHTOOL_GSG:
13c99b24 1448 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1449 (dev->ethtool_ops->get_sg ?
1450 dev->ethtool_ops->get_sg :
1451 ethtool_op_get_sg));
1da177e4
LT
1452 break;
1453 case ETHTOOL_SSG:
1454 rc = ethtool_set_sg(dev, useraddr);
1455 break;
1456 case ETHTOOL_GTSO:
13c99b24 1457 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1458 (dev->ethtool_ops->get_tso ?
1459 dev->ethtool_ops->get_tso :
1460 ethtool_op_get_tso));
1da177e4
LT
1461 break;
1462 case ETHTOOL_STSO:
1463 rc = ethtool_set_tso(dev, useraddr);
1464 break;
1465 case ETHTOOL_TEST:
1466 rc = ethtool_self_test(dev, useraddr);
1467 break;
1468 case ETHTOOL_GSTRINGS:
1469 rc = ethtool_get_strings(dev, useraddr);
1470 break;
1471 case ETHTOOL_PHYS_ID:
1472 rc = ethtool_phys_id(dev, useraddr);
1473 break;
1474 case ETHTOOL_GSTATS:
1475 rc = ethtool_get_stats(dev, useraddr);
1476 break;
a6f9a705
JW
1477 case ETHTOOL_GPERMADDR:
1478 rc = ethtool_get_perm_addr(dev, useraddr);
1479 break;
e89e9cf5 1480 case ETHTOOL_GUFO:
13c99b24 1481 rc = ethtool_get_value(dev, useraddr, ethcmd,
88d3aafd
JG
1482 (dev->ethtool_ops->get_ufo ?
1483 dev->ethtool_ops->get_ufo :
1484 ethtool_op_get_ufo));
e89e9cf5
AR
1485 break;
1486 case ETHTOOL_SUFO:
1487 rc = ethtool_set_ufo(dev, useraddr);
1488 break;
37c3185a
HX
1489 case ETHTOOL_GGSO:
1490 rc = ethtool_get_gso(dev, useraddr);
1491 break;
1492 case ETHTOOL_SGSO:
1493 rc = ethtool_set_gso(dev, useraddr);
1494 break;
3ae7c0b2 1495 case ETHTOOL_GFLAGS:
13c99b24 1496 rc = ethtool_get_value(dev, useraddr, ethcmd,
1896e61f
SS
1497 (dev->ethtool_ops->get_flags ?
1498 dev->ethtool_ops->get_flags :
1499 ethtool_op_get_flags));
3ae7c0b2
JG
1500 break;
1501 case ETHTOOL_SFLAGS:
13c99b24
JG
1502 rc = ethtool_set_value(dev, useraddr,
1503 dev->ethtool_ops->set_flags);
3ae7c0b2 1504 break;
339bf024 1505 case ETHTOOL_GPFLAGS:
13c99b24
JG
1506 rc = ethtool_get_value(dev, useraddr, ethcmd,
1507 dev->ethtool_ops->get_priv_flags);
339bf024
JG
1508 break;
1509 case ETHTOOL_SPFLAGS:
13c99b24
JG
1510 rc = ethtool_set_value(dev, useraddr,
1511 dev->ethtool_ops->set_priv_flags);
339bf024 1512 break;
0853ad66 1513 case ETHTOOL_GRXFH:
59089d8d
SB
1514 case ETHTOOL_GRXRINGS:
1515 case ETHTOOL_GRXCLSRLCNT:
1516 case ETHTOOL_GRXCLSRULE:
1517 case ETHTOOL_GRXCLSRLALL:
1518 rc = ethtool_get_rxnfc(dev, useraddr);
0853ad66
SB
1519 break;
1520 case ETHTOOL_SRXFH:
59089d8d
SB
1521 case ETHTOOL_SRXCLSRLDEL:
1522 case ETHTOOL_SRXCLSRLINS:
1523 rc = ethtool_set_rxnfc(dev, useraddr);
0853ad66 1524 break;
b240a0e5
HX
1525 case ETHTOOL_GGRO:
1526 rc = ethtool_get_gro(dev, useraddr);
1527 break;
1528 case ETHTOOL_SGRO:
1529 rc = ethtool_set_gro(dev, useraddr);
1530 break;
05c6a8d7
AK
1531 case ETHTOOL_FLASHDEV:
1532 rc = ethtool_flash_device(dev, useraddr);
1533 break;
d73d3a8c
BH
1534 case ETHTOOL_RESET:
1535 rc = ethtool_reset(dev, useraddr);
1536 break;
15682bc4
PWJ
1537 case ETHTOOL_SRXNTUPLE:
1538 rc = ethtool_set_rx_ntuple(dev, useraddr);
1539 break;
1540 case ETHTOOL_GRXNTUPLE:
1541 rc = ethtool_get_rx_ntuple(dev, useraddr);
1542 break;
723b2f57
JG
1543 case ETHTOOL_GSSET_INFO:
1544 rc = ethtool_get_sset_info(dev, useraddr);
1545 break;
1da177e4 1546 default:
61a44b9c 1547 rc = -EOPNOTSUPP;
1da177e4 1548 }
4ec93edb 1549
e71a4783 1550 if (dev->ethtool_ops->complete)
1da177e4 1551 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
1552
1553 if (old_features != dev->features)
1554 netdev_features_change(dev);
1555
1da177e4 1556 return rc;
1da177e4 1557}