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