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