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