]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/core/ethtool.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / net / core / ethtool.c
1 /*
2  * net/core/ethtool.c - Ethtool ioctl handler
3  * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4  *
5  * This file is where we call all the ethtool_ops commands to get
6  * the information ethtool needs.
7  *
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.
12  */
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/bitops.h>
21 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
24
25 /*
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
31 u32 ethtool_op_get_link(struct net_device *dev)
32 {
33         return netif_carrier_ok(dev) ? 1 : 0;
34 }
35 EXPORT_SYMBOL(ethtool_op_get_link);
36
37 u32 ethtool_op_get_rx_csum(struct net_device *dev)
38 {
39         return (dev->features & NETIF_F_ALL_CSUM) != 0;
40 }
41 EXPORT_SYMBOL(ethtool_op_get_rx_csum);
42
43 u32 ethtool_op_get_tx_csum(struct net_device *dev)
44 {
45         return (dev->features & NETIF_F_ALL_CSUM) != 0;
46 }
47 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
48
49 int 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
59 int 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 }
68 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
69
70 int 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 }
79 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
80
81 u32 ethtool_op_get_sg(struct net_device *dev)
82 {
83         return (dev->features & NETIF_F_SG) != 0;
84 }
85 EXPORT_SYMBOL(ethtool_op_get_sg);
86
87 int 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 }
96 EXPORT_SYMBOL(ethtool_op_set_sg);
97
98 u32 ethtool_op_get_tso(struct net_device *dev)
99 {
100         return (dev->features & NETIF_F_TSO) != 0;
101 }
102 EXPORT_SYMBOL(ethtool_op_get_tso);
103
104 int 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 }
113 EXPORT_SYMBOL(ethtool_op_set_tso);
114
115 u32 ethtool_op_get_ufo(struct net_device *dev)
116 {
117         return (dev->features & NETIF_F_UFO) != 0;
118 }
119 EXPORT_SYMBOL(ethtool_op_get_ufo);
120
121 int 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 }
129 EXPORT_SYMBOL(ethtool_op_set_ufo);
130
131 /* the following list of flags are the same as their associated
132  * NETIF_F_xxx values in include/linux/netdevice.h
133  */
134 static const u32 flags_dup_features =
135         (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
136
137 u32 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 }
146 EXPORT_SYMBOL(ethtool_op_get_flags);
147
148 int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
149 {
150         if (data & ~supported)
151                 return -EINVAL;
152
153         dev->features = ((dev->features & ~flags_dup_features) |
154                          (data & flags_dup_features));
155         return 0;
156 }
157 EXPORT_SYMBOL(ethtool_op_set_flags);
158
159 void 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 }
169 EXPORT_SYMBOL(ethtool_ntuple_flush);
170
171 /* Handlers for each ethtool command */
172
173 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
174 {
175         struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
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
190 static 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
203 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
204                                                   void __user *useraddr)
205 {
206         struct ethtool_drvinfo info;
207         const struct ethtool_ops *ops = dev->ethtool_ops;
208
209         memset(&info, 0, sizeof(info));
210         info.cmd = ETHTOOL_GDRVINFO;
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         }
221
222         /*
223          * this method of obtaining string set info is deprecated;
224          * Use ETHTOOL_GSSET_INFO instead.
225          */
226         if (ops && ops->get_sset_count) {
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;
235                 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
236                 if (rc >= 0)
237                         info.n_priv_flags = rc;
238         }
239         if (ops && ops->get_regs_len)
240                 info.regdump_len = ops->get_regs_len(dev);
241         if (ops && ops->get_eeprom_len)
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
249 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
250                                                     void __user *useraddr)
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 */
270         n_bits = hweight64(sset_mask);
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
304 out:
305         kfree(info_buf);
306         return ret;
307 }
308
309 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
310                                                 u32 cmd, void __user *useraddr)
311 {
312         struct ethtool_rxnfc info;
313         size_t info_size = sizeof(info);
314
315         if (!dev->ethtool_ops->set_rxnfc)
316                 return -EOPNOTSUPP;
317
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))
327                 return -EFAULT;
328
329         return dev->ethtool_ops->set_rxnfc(dev, &info);
330 }
331
332 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
333                                                 u32 cmd, void __user *useraddr)
334 {
335         struct ethtool_rxnfc info;
336         size_t info_size = sizeof(info);
337         const struct ethtool_ops *ops = dev->ethtool_ops;
338         int ret;
339         void *rule_buf = NULL;
340
341         if (!ops->get_rxnfc)
342                 return -EOPNOTSUPP;
343
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))
353                 return -EFAULT;
354
355         if (info.cmd == ETHTOOL_GRXCLSRLALL) {
356                 if (info.rule_cnt > 0) {
357                         if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
358                                 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
359                                                    GFP_USER);
360                         if (!rule_buf)
361                                 return -ENOMEM;
362                 }
363         }
364
365         ret = ops->get_rxnfc(dev, &info, rule_buf);
366         if (ret < 0)
367                 goto err_out;
368
369         ret = -EFAULT;
370         if (copy_to_user(useraddr, &info, info_size))
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
381 err_out:
382         kfree(rule_buf);
383
384         return ret;
385 }
386
387 static 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 = kzalloc(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
420 out:
421         kfree(indir);
422         return ret;
423 }
424
425 static 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
456 out:
457         kfree(indir);
458         return ret;
459 }
460
461 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
462                         struct ethtool_rx_ntuple_flow_spec *spec,
463                         struct ethtool_rx_ntuple_flow_spec_container *fsc)
464 {
465
466         /* don't add filters forever */
467         if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
468                 /* free the container */
469                 kfree(fsc);
470                 return;
471         }
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++;
487 }
488
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  */
495 static 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
521 static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
522                                                     void __user *useraddr)
523 {
524         struct ethtool_rx_ntuple cmd;
525         const struct ethtool_ops *ops = dev->ethtool_ops;
526         struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
527         int ret;
528
529         if (!(dev->features & NETIF_F_NTUPLE))
530                 return -EINVAL;
531
532         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
533                 return -EFAULT;
534
535         rx_ntuple_fix_masks(&cmd.fs);
536
537         /*
538          * Cache filter in dev struct for GET operation only if
539          * the underlying driver doesn't have its own GET operation, and
540          * only if the filter was added successfully.  First make sure we
541          * can allocate the filter, then continue if successful.
542          */
543         if (!ops->get_rx_ntuple) {
544                 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
545                 if (!fsc)
546                         return -ENOMEM;
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);
557
558         return ret;
559 }
560
561 static 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 = kzalloc(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;
641                 }
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",
649                                 fsc->fs.h_u.tcp_ip4_spec.ip4src);
650                         p += ETH_GSTRING_LEN;
651                         num_strings++;
652                         sprintf(p, "\tSrc IP mask: 0x%x\n",
653                                 fsc->fs.m_u.tcp_ip4_spec.ip4src);
654                         p += ETH_GSTRING_LEN;
655                         num_strings++;
656                         sprintf(p, "\tDest IP addr: 0x%x\n",
657                                 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
658                         p += ETH_GSTRING_LEN;
659                         num_strings++;
660                         sprintf(p, "\tDest IP mask: 0x%x\n",
661                                 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
662                         p += ETH_GSTRING_LEN;
663                         num_strings++;
664                         sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
665                                 fsc->fs.h_u.tcp_ip4_spec.psrc,
666                                 fsc->fs.m_u.tcp_ip4_spec.psrc);
667                         p += ETH_GSTRING_LEN;
668                         num_strings++;
669                         sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
670                                 fsc->fs.h_u.tcp_ip4_spec.pdst,
671                                 fsc->fs.m_u.tcp_ip4_spec.pdst);
672                         p += ETH_GSTRING_LEN;
673                         num_strings++;
674                         sprintf(p, "\tTOS: %d, mask: 0x%x\n",
675                                 fsc->fs.h_u.tcp_ip4_spec.tos,
676                                 fsc->fs.m_u.tcp_ip4_spec.tos);
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",
683                                 fsc->fs.h_u.ah_ip4_spec.ip4src);
684                         p += ETH_GSTRING_LEN;
685                         num_strings++;
686                         sprintf(p, "\tSrc IP mask: 0x%x\n",
687                                 fsc->fs.m_u.ah_ip4_spec.ip4src);
688                         p += ETH_GSTRING_LEN;
689                         num_strings++;
690                         sprintf(p, "\tDest IP addr: 0x%x\n",
691                                 fsc->fs.h_u.ah_ip4_spec.ip4dst);
692                         p += ETH_GSTRING_LEN;
693                         num_strings++;
694                         sprintf(p, "\tDest IP mask: 0x%x\n",
695                                 fsc->fs.m_u.ah_ip4_spec.ip4dst);
696                         p += ETH_GSTRING_LEN;
697                         num_strings++;
698                         sprintf(p, "\tSPI: %d, mask: 0x%x\n",
699                                 fsc->fs.h_u.ah_ip4_spec.spi,
700                                 fsc->fs.m_u.ah_ip4_spec.spi);
701                         p += ETH_GSTRING_LEN;
702                         num_strings++;
703                         sprintf(p, "\tTOS: %d, mask: 0x%x\n",
704                                 fsc->fs.h_u.ah_ip4_spec.tos,
705                                 fsc->fs.m_u.ah_ip4_spec.tos);
706                         p += ETH_GSTRING_LEN;
707                         num_strings++;
708                         break;
709                 case IP_USER_FLOW:
710                         sprintf(p, "\tSrc IP addr: 0x%x\n",
711                                 fsc->fs.h_u.usr_ip4_spec.ip4src);
712                         p += ETH_GSTRING_LEN;
713                         num_strings++;
714                         sprintf(p, "\tSrc IP mask: 0x%x\n",
715                                 fsc->fs.m_u.usr_ip4_spec.ip4src);
716                         p += ETH_GSTRING_LEN;
717                         num_strings++;
718                         sprintf(p, "\tDest IP addr: 0x%x\n",
719                                 fsc->fs.h_u.usr_ip4_spec.ip4dst);
720                         p += ETH_GSTRING_LEN;
721                         num_strings++;
722                         sprintf(p, "\tDest IP mask: 0x%x\n",
723                                 fsc->fs.m_u.usr_ip4_spec.ip4dst);
724                         p += ETH_GSTRING_LEN;
725                         num_strings++;
726                         break;
727                 case IPV4_FLOW:
728                         sprintf(p, "\tSrc IP addr: 0x%x\n",
729                                 fsc->fs.h_u.usr_ip4_spec.ip4src);
730                         p += ETH_GSTRING_LEN;
731                         num_strings++;
732                         sprintf(p, "\tSrc IP mask: 0x%x\n",
733                                 fsc->fs.m_u.usr_ip4_spec.ip4src);
734                         p += ETH_GSTRING_LEN;
735                         num_strings++;
736                         sprintf(p, "\tDest IP addr: 0x%x\n",
737                                 fsc->fs.h_u.usr_ip4_spec.ip4dst);
738                         p += ETH_GSTRING_LEN;
739                         num_strings++;
740                         sprintf(p, "\tDest IP mask: 0x%x\n",
741                                 fsc->fs.m_u.usr_ip4_spec.ip4dst);
742                         p += ETH_GSTRING_LEN;
743                         num_strings++;
744                         sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
745                                 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
746                                 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
747                         p += ETH_GSTRING_LEN;
748                         num_strings++;
749                         sprintf(p, "\tTOS: %d, mask: 0x%x\n",
750                                 fsc->fs.h_u.usr_ip4_spec.tos,
751                                 fsc->fs.m_u.usr_ip4_spec.tos);
752                         p += ETH_GSTRING_LEN;
753                         num_strings++;
754                         sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
755                                 fsc->fs.h_u.usr_ip4_spec.ip_ver,
756                                 fsc->fs.m_u.usr_ip4_spec.ip_ver);
757                         p += ETH_GSTRING_LEN;
758                         num_strings++;
759                         sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
760                                 fsc->fs.h_u.usr_ip4_spec.proto,
761                                 fsc->fs.m_u.usr_ip4_spec.proto);
762                         p += ETH_GSTRING_LEN;
763                         num_strings++;
764                         break;
765                 }
766                 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
767                         fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
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",
780                                 fsc->fs.action);
781                 p += ETH_GSTRING_LEN;
782                 num_strings++;
783 unknown_filter:
784                 i++;
785         }
786 copy:
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
797 out:
798         kfree(data);
799         return ret;
800 }
801
802 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
803 {
804         struct ethtool_regs regs;
805         const struct ethtool_ops *ops = dev->ethtool_ops;
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
819         regbuf = vmalloc(reglen);
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:
834         vfree(regbuf);
835         return ret;
836 }
837
838 static 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
858 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
859 {
860         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
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
872 static 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
885 static 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
893 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
894 {
895         struct ethtool_eeprom eeprom;
896         const struct ethtool_ops *ops = dev->ethtool_ops;
897         void __user *userbuf = useraddr + sizeof(eeprom);
898         u32 bytes_remaining;
899         u8 *data;
900         int ret = 0;
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
916         data = kmalloc(PAGE_SIZE, GFP_USER);
917         if (!data)
918                 return -ENOMEM;
919
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         }
935
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
941         kfree(data);
942         return ret;
943 }
944
945 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
946 {
947         struct ethtool_eeprom eeprom;
948         const struct ethtool_ops *ops = dev->ethtool_ops;
949         void __user *userbuf = useraddr + sizeof(eeprom);
950         u32 bytes_remaining;
951         u8 *data;
952         int ret = 0;
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
968         data = kmalloc(PAGE_SIZE, GFP_USER);
969         if (!data)
970                 return -ENOMEM;
971
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         }
987
988         kfree(data);
989         return ret;
990 }
991
992 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
993                                                    void __user *useraddr)
994 {
995         struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
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
1007 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1008                                                    void __user *useraddr)
1009 {
1010         struct ethtool_coalesce coalesce;
1011
1012         if (!dev->ethtool_ops->set_coalesce)
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
1021 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1022 {
1023         struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
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
1035 static 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
1048 static 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
1062 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1063 {
1064         struct ethtool_pauseparam pauseparam;
1065
1066         if (!dev->ethtool_ops->set_pauseparam)
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
1075 static 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
1085         if (!data && dev->ethtool_ops->set_ufo) {
1086                 err = dev->ethtool_ops->set_ufo(dev, 0);
1087                 if (err)
1088                         return err;
1089         }
1090         return dev->ethtool_ops->set_sg(dev, data);
1091 }
1092
1093 static 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 }
1112 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
1113
1114 static 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
1130 static 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
1140         if (edata.data &&
1141             !(dev->features & NETIF_F_ALL_CSUM))
1142                 return -EINVAL;
1143
1144         return __ethtool_set_sg(dev, edata.data);
1145 }
1146
1147 static 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
1163 static 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
1178 static 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)))
1184                 return -EFAULT;
1185         return 0;
1186 }
1187
1188 static 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
1201 static 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)))
1207                 return -EFAULT;
1208         return 0;
1209 }
1210
1211 static 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) {
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)
1224                         return -EINVAL;
1225                 dev->features |= NETIF_F_GRO;
1226         } else
1227                 dev->features &= ~NETIF_F_GRO;
1228
1229         return 0;
1230 }
1231
1232 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1233 {
1234         struct ethtool_test test;
1235         const struct ethtool_ops *ops = dev->ethtool_ops;
1236         u64 *data;
1237         int ret, test_len;
1238
1239         if (!ops->self_test || !ops->get_sset_count)
1240                 return -EOPNOTSUPP;
1241
1242         test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1243         if (test_len < 0)
1244                 return test_len;
1245         WARN_ON(test_len == 0);
1246
1247         if (copy_from_user(&test, useraddr, sizeof(test)))
1248                 return -EFAULT;
1249
1250         test.len = test_len;
1251         data = kmalloc(test_len * sizeof(u64), GFP_USER);
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
1270 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1271 {
1272         struct ethtool_gstrings gstrings;
1273         const struct ethtool_ops *ops = dev->ethtool_ops;
1274         u8 *data;
1275         int ret;
1276
1277         if (!ops->get_strings || !ops->get_sset_count)
1278                 return -EOPNOTSUPP;
1279
1280         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1281                 return -EFAULT;
1282
1283         ret = ops->get_sset_count(dev, gstrings.string_set);
1284         if (ret < 0)
1285                 return ret;
1286
1287         gstrings.len = ret;
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
1308 static 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
1321 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1322 {
1323         struct ethtool_stats stats;
1324         const struct ethtool_ops *ops = dev->ethtool_ops;
1325         u64 *data;
1326         int ret, n_stats;
1327
1328         if (!ops->get_ethtool_stats || !ops->get_sset_count)
1329                 return -EOPNOTSUPP;
1330
1331         n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1332         if (n_stats < 0)
1333                 return n_stats;
1334         WARN_ON(n_stats == 0);
1335
1336         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1337                 return -EFAULT;
1338
1339         stats.n_stats = n_stats;
1340         data = kmalloc(n_stats * sizeof(u64), GFP_USER);
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
1359 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1360 {
1361         struct ethtool_perm_addr epaddr;
1362
1363         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1364                 return -EFAULT;
1365
1366         if (epaddr.size < dev->addr_len)
1367                 return -ETOOSMALL;
1368         epaddr.size = dev->addr_len;
1369
1370         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1371                 return -EFAULT;
1372         useraddr += sizeof(epaddr);
1373         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1374                 return -EFAULT;
1375         return 0;
1376 }
1377
1378 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1379                              u32 cmd, u32 (*actor)(struct net_device *))
1380 {
1381         struct ethtool_value edata = { .cmd = cmd };
1382
1383         if (!actor)
1384                 return -EOPNOTSUPP;
1385
1386         edata.data = actor(dev);
1387
1388         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1389                 return -EFAULT;
1390         return 0;
1391 }
1392
1393 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1394                              void (*actor)(struct net_device *, u32))
1395 {
1396         struct ethtool_value edata;
1397
1398         if (!actor)
1399                 return -EOPNOTSUPP;
1400
1401         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1402                 return -EFAULT;
1403
1404         actor(dev, edata.data);
1405         return 0;
1406 }
1407
1408 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1409                              int (*actor)(struct net_device *, u32))
1410 {
1411         struct ethtool_value edata;
1412
1413         if (!actor)
1414                 return -EOPNOTSUPP;
1415
1416         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1417                 return -EFAULT;
1418
1419         return actor(dev, edata.data);
1420 }
1421
1422 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1423                                                    char __user *useraddr)
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
1436 /* The main entry point in this file.  Called from net/core/dev.c */
1437
1438 int dev_ethtool(struct net *net, struct ifreq *ifr)
1439 {
1440         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1441         void __user *useraddr = ifr->ifr_data;
1442         u32 ethcmd;
1443         int rc;
1444         unsigned long old_features;
1445
1446         if (!dev || !netif_device_present(dev))
1447                 return -ENODEV;
1448
1449         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1450                 return -EFAULT;
1451
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
1462         /* Allow some commands to be done by anyone */
1463         switch (ethcmd) {
1464         case ETHTOOL_GSET:
1465         case ETHTOOL_GDRVINFO:
1466         case ETHTOOL_GMSGLVL:
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:
1474         case ETHTOOL_GTSO:
1475         case ETHTOOL_GPERMADDR:
1476         case ETHTOOL_GUFO:
1477         case ETHTOOL_GGSO:
1478         case ETHTOOL_GGRO:
1479         case ETHTOOL_GFLAGS:
1480         case ETHTOOL_GPFLAGS:
1481         case ETHTOOL_GRXFH:
1482         case ETHTOOL_GRXRINGS:
1483         case ETHTOOL_GRXCLSRLCNT:
1484         case ETHTOOL_GRXCLSRULE:
1485         case ETHTOOL_GRXCLSRLALL:
1486                 break;
1487         default:
1488                 if (!capable(CAP_NET_ADMIN))
1489                         return -EPERM;
1490         }
1491
1492         if (dev->ethtool_ops->begin) {
1493                 rc = dev->ethtool_ops->begin(dev);
1494                 if (rc  < 0)
1495                         return rc;
1496         }
1497         old_features = dev->features;
1498
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);
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:
1519                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1520                                        dev->ethtool_ops->get_msglevel);
1521                 break;
1522         case ETHTOOL_SMSGLVL:
1523                 rc = ethtool_set_value_void(dev, useraddr,
1524                                        dev->ethtool_ops->set_msglevel);
1525                 break;
1526         case ETHTOOL_NWAY_RST:
1527                 rc = ethtool_nway_reset(dev);
1528                 break;
1529         case ETHTOOL_GLINK:
1530                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1531                                        dev->ethtool_ops->get_link);
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:
1558                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1559                                        (dev->ethtool_ops->get_rx_csum ?
1560                                         dev->ethtool_ops->get_rx_csum :
1561                                         ethtool_op_get_rx_csum));
1562                 break;
1563         case ETHTOOL_SRXCSUM:
1564                 rc = ethtool_set_rx_csum(dev, useraddr);
1565                 break;
1566         case ETHTOOL_GTXCSUM:
1567                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1568                                        (dev->ethtool_ops->get_tx_csum ?
1569                                         dev->ethtool_ops->get_tx_csum :
1570                                         ethtool_op_get_tx_csum));
1571                 break;
1572         case ETHTOOL_STXCSUM:
1573                 rc = ethtool_set_tx_csum(dev, useraddr);
1574                 break;
1575         case ETHTOOL_GSG:
1576                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1577                                        (dev->ethtool_ops->get_sg ?
1578                                         dev->ethtool_ops->get_sg :
1579                                         ethtool_op_get_sg));
1580                 break;
1581         case ETHTOOL_SSG:
1582                 rc = ethtool_set_sg(dev, useraddr);
1583                 break;
1584         case ETHTOOL_GTSO:
1585                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1586                                        (dev->ethtool_ops->get_tso ?
1587                                         dev->ethtool_ops->get_tso :
1588                                         ethtool_op_get_tso));
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;
1605         case ETHTOOL_GPERMADDR:
1606                 rc = ethtool_get_perm_addr(dev, useraddr);
1607                 break;
1608         case ETHTOOL_GUFO:
1609                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1610                                        (dev->ethtool_ops->get_ufo ?
1611                                         dev->ethtool_ops->get_ufo :
1612                                         ethtool_op_get_ufo));
1613                 break;
1614         case ETHTOOL_SUFO:
1615                 rc = ethtool_set_ufo(dev, useraddr);
1616                 break;
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;
1623         case ETHTOOL_GFLAGS:
1624                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1625                                        (dev->ethtool_ops->get_flags ?
1626                                         dev->ethtool_ops->get_flags :
1627                                         ethtool_op_get_flags));
1628                 break;
1629         case ETHTOOL_SFLAGS:
1630                 rc = ethtool_set_value(dev, useraddr,
1631                                        dev->ethtool_ops->set_flags);
1632                 break;
1633         case ETHTOOL_GPFLAGS:
1634                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1635                                        dev->ethtool_ops->get_priv_flags);
1636                 break;
1637         case ETHTOOL_SPFLAGS:
1638                 rc = ethtool_set_value(dev, useraddr,
1639                                        dev->ethtool_ops->set_priv_flags);
1640                 break;
1641         case ETHTOOL_GRXFH:
1642         case ETHTOOL_GRXRINGS:
1643         case ETHTOOL_GRXCLSRLCNT:
1644         case ETHTOOL_GRXCLSRULE:
1645         case ETHTOOL_GRXCLSRLALL:
1646                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1647                 break;
1648         case ETHTOOL_SRXFH:
1649         case ETHTOOL_SRXCLSRLDEL:
1650         case ETHTOOL_SRXCLSRLINS:
1651                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1652                 break;
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;
1659         case ETHTOOL_FLASHDEV:
1660                 rc = ethtool_flash_device(dev, useraddr);
1661                 break;
1662         case ETHTOOL_RESET:
1663                 rc = ethtool_reset(dev, useraddr);
1664                 break;
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;
1671         case ETHTOOL_GSSET_INFO:
1672                 rc = ethtool_get_sset_info(dev, useraddr);
1673                 break;
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;
1680         default:
1681                 rc = -EOPNOTSUPP;
1682         }
1683
1684         if (dev->ethtool_ops->complete)
1685                 dev->ethtool_ops->complete(dev);
1686
1687         if (old_features != dev->features)
1688                 netdev_features_change(dev);
1689
1690         return rc;
1691 }