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