]> Pileus Git - ~andy/linux/blob - net/core/ethtool.c
xfrm: allow to avoid copying DSCP during encapsulation
[~andy/linux] / 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/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sched.h>
28
29 /*
30  * Some useful ethtool_ops methods that're device independent.
31  * If we find that all drivers want to do the same thing here,
32  * we can turn these into dev_() function calls.
33  */
34
35 u32 ethtool_op_get_link(struct net_device *dev)
36 {
37         return netif_carrier_ok(dev) ? 1 : 0;
38 }
39 EXPORT_SYMBOL(ethtool_op_get_link);
40
41 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
42 {
43         info->so_timestamping =
44                 SOF_TIMESTAMPING_TX_SOFTWARE |
45                 SOF_TIMESTAMPING_RX_SOFTWARE |
46                 SOF_TIMESTAMPING_SOFTWARE;
47         info->phc_index = -1;
48         return 0;
49 }
50 EXPORT_SYMBOL(ethtool_op_get_ts_info);
51
52 /* Handlers for each ethtool command */
53
54 #define ETHTOOL_DEV_FEATURE_WORDS       ((NETDEV_FEATURE_COUNT + 31) / 32)
55
56 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
57         [NETIF_F_SG_BIT] =               "tx-scatter-gather",
58         [NETIF_F_IP_CSUM_BIT] =          "tx-checksum-ipv4",
59         [NETIF_F_HW_CSUM_BIT] =          "tx-checksum-ip-generic",
60         [NETIF_F_IPV6_CSUM_BIT] =        "tx-checksum-ipv6",
61         [NETIF_F_HIGHDMA_BIT] =          "highdma",
62         [NETIF_F_FRAGLIST_BIT] =         "tx-scatter-gather-fraglist",
63         [NETIF_F_HW_VLAN_TX_BIT] =       "tx-vlan-hw-insert",
64
65         [NETIF_F_HW_VLAN_RX_BIT] =       "rx-vlan-hw-parse",
66         [NETIF_F_HW_VLAN_FILTER_BIT] =   "rx-vlan-filter",
67         [NETIF_F_VLAN_CHALLENGED_BIT] =  "vlan-challenged",
68         [NETIF_F_GSO_BIT] =              "tx-generic-segmentation",
69         [NETIF_F_LLTX_BIT] =             "tx-lockless",
70         [NETIF_F_NETNS_LOCAL_BIT] =      "netns-local",
71         [NETIF_F_GRO_BIT] =              "rx-gro",
72         [NETIF_F_LRO_BIT] =              "rx-lro",
73
74         [NETIF_F_TSO_BIT] =              "tx-tcp-segmentation",
75         [NETIF_F_UFO_BIT] =              "tx-udp-fragmentation",
76         [NETIF_F_GSO_ROBUST_BIT] =       "tx-gso-robust",
77         [NETIF_F_TSO_ECN_BIT] =          "tx-tcp-ecn-segmentation",
78         [NETIF_F_TSO6_BIT] =             "tx-tcp6-segmentation",
79         [NETIF_F_FSO_BIT] =              "tx-fcoe-segmentation",
80         [NETIF_F_GSO_GRE_BIT] =          "tx-gre-segmentation",
81
82         [NETIF_F_FCOE_CRC_BIT] =         "tx-checksum-fcoe-crc",
83         [NETIF_F_SCTP_CSUM_BIT] =        "tx-checksum-sctp",
84         [NETIF_F_FCOE_MTU_BIT] =         "fcoe-mtu",
85         [NETIF_F_NTUPLE_BIT] =           "rx-ntuple-filter",
86         [NETIF_F_RXHASH_BIT] =           "rx-hashing",
87         [NETIF_F_RXCSUM_BIT] =           "rx-checksum",
88         [NETIF_F_NOCACHE_COPY_BIT] =     "tx-nocache-copy",
89         [NETIF_F_LOOPBACK_BIT] =         "loopback",
90         [NETIF_F_RXFCS_BIT] =            "rx-fcs",
91         [NETIF_F_RXALL_BIT] =            "rx-all",
92 };
93
94 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
95 {
96         struct ethtool_gfeatures cmd = {
97                 .cmd = ETHTOOL_GFEATURES,
98                 .size = ETHTOOL_DEV_FEATURE_WORDS,
99         };
100         struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
101         u32 __user *sizeaddr;
102         u32 copy_size;
103         int i;
104
105         /* in case feature bits run out again */
106         BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
107
108         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
109                 features[i].available = (u32)(dev->hw_features >> (32 * i));
110                 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
111                 features[i].active = (u32)(dev->features >> (32 * i));
112                 features[i].never_changed =
113                         (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
114         }
115
116         sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
117         if (get_user(copy_size, sizeaddr))
118                 return -EFAULT;
119
120         if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
121                 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
122
123         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
124                 return -EFAULT;
125         useraddr += sizeof(cmd);
126         if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
127                 return -EFAULT;
128
129         return 0;
130 }
131
132 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
133 {
134         struct ethtool_sfeatures cmd;
135         struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
136         netdev_features_t wanted = 0, valid = 0;
137         int i, ret = 0;
138
139         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
140                 return -EFAULT;
141         useraddr += sizeof(cmd);
142
143         if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
144                 return -EINVAL;
145
146         if (copy_from_user(features, useraddr, sizeof(features)))
147                 return -EFAULT;
148
149         for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
150                 valid |= (netdev_features_t)features[i].valid << (32 * i);
151                 wanted |= (netdev_features_t)features[i].requested << (32 * i);
152         }
153
154         if (valid & ~NETIF_F_ETHTOOL_BITS)
155                 return -EINVAL;
156
157         if (valid & ~dev->hw_features) {
158                 valid &= dev->hw_features;
159                 ret |= ETHTOOL_F_UNSUPPORTED;
160         }
161
162         dev->wanted_features &= ~valid;
163         dev->wanted_features |= wanted & valid;
164         __netdev_update_features(dev);
165
166         if ((dev->wanted_features ^ dev->features) & valid)
167                 ret |= ETHTOOL_F_WISH;
168
169         return ret;
170 }
171
172 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
173 {
174         const struct ethtool_ops *ops = dev->ethtool_ops;
175
176         if (sset == ETH_SS_FEATURES)
177                 return ARRAY_SIZE(netdev_features_strings);
178
179         if (ops->get_sset_count && ops->get_strings)
180                 return ops->get_sset_count(dev, sset);
181         else
182                 return -EOPNOTSUPP;
183 }
184
185 static void __ethtool_get_strings(struct net_device *dev,
186         u32 stringset, u8 *data)
187 {
188         const struct ethtool_ops *ops = dev->ethtool_ops;
189
190         if (stringset == ETH_SS_FEATURES)
191                 memcpy(data, netdev_features_strings,
192                         sizeof(netdev_features_strings));
193         else
194                 /* ops->get_strings is valid because checked earlier */
195                 ops->get_strings(dev, stringset, data);
196 }
197
198 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
199 {
200         /* feature masks of legacy discrete ethtool ops */
201
202         switch (eth_cmd) {
203         case ETHTOOL_GTXCSUM:
204         case ETHTOOL_STXCSUM:
205                 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
206         case ETHTOOL_GRXCSUM:
207         case ETHTOOL_SRXCSUM:
208                 return NETIF_F_RXCSUM;
209         case ETHTOOL_GSG:
210         case ETHTOOL_SSG:
211                 return NETIF_F_SG;
212         case ETHTOOL_GTSO:
213         case ETHTOOL_STSO:
214                 return NETIF_F_ALL_TSO;
215         case ETHTOOL_GUFO:
216         case ETHTOOL_SUFO:
217                 return NETIF_F_UFO;
218         case ETHTOOL_GGSO:
219         case ETHTOOL_SGSO:
220                 return NETIF_F_GSO;
221         case ETHTOOL_GGRO:
222         case ETHTOOL_SGRO:
223                 return NETIF_F_GRO;
224         default:
225                 BUG();
226         }
227 }
228
229 static int ethtool_get_one_feature(struct net_device *dev,
230         char __user *useraddr, u32 ethcmd)
231 {
232         netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
233         struct ethtool_value edata = {
234                 .cmd = ethcmd,
235                 .data = !!(dev->features & mask),
236         };
237
238         if (copy_to_user(useraddr, &edata, sizeof(edata)))
239                 return -EFAULT;
240         return 0;
241 }
242
243 static int ethtool_set_one_feature(struct net_device *dev,
244         void __user *useraddr, u32 ethcmd)
245 {
246         struct ethtool_value edata;
247         netdev_features_t mask;
248
249         if (copy_from_user(&edata, useraddr, sizeof(edata)))
250                 return -EFAULT;
251
252         mask = ethtool_get_feature_mask(ethcmd);
253         mask &= dev->hw_features;
254         if (!mask)
255                 return -EOPNOTSUPP;
256
257         if (edata.data)
258                 dev->wanted_features |= mask;
259         else
260                 dev->wanted_features &= ~mask;
261
262         __netdev_update_features(dev);
263
264         return 0;
265 }
266
267 #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
268                           ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
269 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
270                           NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
271
272 static u32 __ethtool_get_flags(struct net_device *dev)
273 {
274         u32 flags = 0;
275
276         if (dev->features & NETIF_F_LRO)        flags |= ETH_FLAG_LRO;
277         if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
278         if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
279         if (dev->features & NETIF_F_NTUPLE)     flags |= ETH_FLAG_NTUPLE;
280         if (dev->features & NETIF_F_RXHASH)     flags |= ETH_FLAG_RXHASH;
281
282         return flags;
283 }
284
285 static int __ethtool_set_flags(struct net_device *dev, u32 data)
286 {
287         netdev_features_t features = 0, changed;
288
289         if (data & ~ETH_ALL_FLAGS)
290                 return -EINVAL;
291
292         if (data & ETH_FLAG_LRO)        features |= NETIF_F_LRO;
293         if (data & ETH_FLAG_RXVLAN)     features |= NETIF_F_HW_VLAN_RX;
294         if (data & ETH_FLAG_TXVLAN)     features |= NETIF_F_HW_VLAN_TX;
295         if (data & ETH_FLAG_NTUPLE)     features |= NETIF_F_NTUPLE;
296         if (data & ETH_FLAG_RXHASH)     features |= NETIF_F_RXHASH;
297
298         /* allow changing only bits set in hw_features */
299         changed = (features ^ dev->features) & ETH_ALL_FEATURES;
300         if (changed & ~dev->hw_features)
301                 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
302
303         dev->wanted_features =
304                 (dev->wanted_features & ~changed) | (features & changed);
305
306         __netdev_update_features(dev);
307
308         return 0;
309 }
310
311 int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
312 {
313         ASSERT_RTNL();
314
315         if (!dev->ethtool_ops->get_settings)
316                 return -EOPNOTSUPP;
317
318         memset(cmd, 0, sizeof(struct ethtool_cmd));
319         cmd->cmd = ETHTOOL_GSET;
320         return dev->ethtool_ops->get_settings(dev, cmd);
321 }
322 EXPORT_SYMBOL(__ethtool_get_settings);
323
324 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
325 {
326         int err;
327         struct ethtool_cmd cmd;
328
329         err = __ethtool_get_settings(dev, &cmd);
330         if (err < 0)
331                 return err;
332
333         if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
334                 return -EFAULT;
335         return 0;
336 }
337
338 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
339 {
340         struct ethtool_cmd cmd;
341
342         if (!dev->ethtool_ops->set_settings)
343                 return -EOPNOTSUPP;
344
345         if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
346                 return -EFAULT;
347
348         return dev->ethtool_ops->set_settings(dev, &cmd);
349 }
350
351 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
352                                                   void __user *useraddr)
353 {
354         struct ethtool_drvinfo info;
355         const struct ethtool_ops *ops = dev->ethtool_ops;
356
357         memset(&info, 0, sizeof(info));
358         info.cmd = ETHTOOL_GDRVINFO;
359         if (ops->get_drvinfo) {
360                 ops->get_drvinfo(dev, &info);
361         } else if (dev->dev.parent && dev->dev.parent->driver) {
362                 strlcpy(info.bus_info, dev_name(dev->dev.parent),
363                         sizeof(info.bus_info));
364                 strlcpy(info.driver, dev->dev.parent->driver->name,
365                         sizeof(info.driver));
366         } else {
367                 return -EOPNOTSUPP;
368         }
369
370         /*
371          * this method of obtaining string set info is deprecated;
372          * Use ETHTOOL_GSSET_INFO instead.
373          */
374         if (ops->get_sset_count) {
375                 int rc;
376
377                 rc = ops->get_sset_count(dev, ETH_SS_TEST);
378                 if (rc >= 0)
379                         info.testinfo_len = rc;
380                 rc = ops->get_sset_count(dev, ETH_SS_STATS);
381                 if (rc >= 0)
382                         info.n_stats = rc;
383                 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
384                 if (rc >= 0)
385                         info.n_priv_flags = rc;
386         }
387         if (ops->get_regs_len)
388                 info.regdump_len = ops->get_regs_len(dev);
389         if (ops->get_eeprom_len)
390                 info.eedump_len = ops->get_eeprom_len(dev);
391
392         if (copy_to_user(useraddr, &info, sizeof(info)))
393                 return -EFAULT;
394         return 0;
395 }
396
397 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
398                                                     void __user *useraddr)
399 {
400         struct ethtool_sset_info info;
401         u64 sset_mask;
402         int i, idx = 0, n_bits = 0, ret, rc;
403         u32 *info_buf = NULL;
404
405         if (copy_from_user(&info, useraddr, sizeof(info)))
406                 return -EFAULT;
407
408         /* store copy of mask, because we zero struct later on */
409         sset_mask = info.sset_mask;
410         if (!sset_mask)
411                 return 0;
412
413         /* calculate size of return buffer */
414         n_bits = hweight64(sset_mask);
415
416         memset(&info, 0, sizeof(info));
417         info.cmd = ETHTOOL_GSSET_INFO;
418
419         info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
420         if (!info_buf)
421                 return -ENOMEM;
422
423         /*
424          * fill return buffer based on input bitmask and successful
425          * get_sset_count return
426          */
427         for (i = 0; i < 64; i++) {
428                 if (!(sset_mask & (1ULL << i)))
429                         continue;
430
431                 rc = __ethtool_get_sset_count(dev, i);
432                 if (rc >= 0) {
433                         info.sset_mask |= (1ULL << i);
434                         info_buf[idx++] = rc;
435                 }
436         }
437
438         ret = -EFAULT;
439         if (copy_to_user(useraddr, &info, sizeof(info)))
440                 goto out;
441
442         useraddr += offsetof(struct ethtool_sset_info, data);
443         if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
444                 goto out;
445
446         ret = 0;
447
448 out:
449         kfree(info_buf);
450         return ret;
451 }
452
453 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
454                                                 u32 cmd, void __user *useraddr)
455 {
456         struct ethtool_rxnfc info;
457         size_t info_size = sizeof(info);
458         int rc;
459
460         if (!dev->ethtool_ops->set_rxnfc)
461                 return -EOPNOTSUPP;
462
463         /* struct ethtool_rxnfc was originally defined for
464          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
465          * members.  User-space might still be using that
466          * definition. */
467         if (cmd == ETHTOOL_SRXFH)
468                 info_size = (offsetof(struct ethtool_rxnfc, data) +
469                              sizeof(info.data));
470
471         if (copy_from_user(&info, useraddr, info_size))
472                 return -EFAULT;
473
474         rc = dev->ethtool_ops->set_rxnfc(dev, &info);
475         if (rc)
476                 return rc;
477
478         if (cmd == ETHTOOL_SRXCLSRLINS &&
479             copy_to_user(useraddr, &info, info_size))
480                 return -EFAULT;
481
482         return 0;
483 }
484
485 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
486                                                 u32 cmd, void __user *useraddr)
487 {
488         struct ethtool_rxnfc info;
489         size_t info_size = sizeof(info);
490         const struct ethtool_ops *ops = dev->ethtool_ops;
491         int ret;
492         void *rule_buf = NULL;
493
494         if (!ops->get_rxnfc)
495                 return -EOPNOTSUPP;
496
497         /* struct ethtool_rxnfc was originally defined for
498          * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
499          * members.  User-space might still be using that
500          * definition. */
501         if (cmd == ETHTOOL_GRXFH)
502                 info_size = (offsetof(struct ethtool_rxnfc, data) +
503                              sizeof(info.data));
504
505         if (copy_from_user(&info, useraddr, info_size))
506                 return -EFAULT;
507
508         if (info.cmd == ETHTOOL_GRXCLSRLALL) {
509                 if (info.rule_cnt > 0) {
510                         if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
511                                 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
512                                                    GFP_USER);
513                         if (!rule_buf)
514                                 return -ENOMEM;
515                 }
516         }
517
518         ret = ops->get_rxnfc(dev, &info, rule_buf);
519         if (ret < 0)
520                 goto err_out;
521
522         ret = -EFAULT;
523         if (copy_to_user(useraddr, &info, info_size))
524                 goto err_out;
525
526         if (rule_buf) {
527                 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
528                 if (copy_to_user(useraddr, rule_buf,
529                                  info.rule_cnt * sizeof(u32)))
530                         goto err_out;
531         }
532         ret = 0;
533
534 err_out:
535         kfree(rule_buf);
536
537         return ret;
538 }
539
540 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
541                                                      void __user *useraddr)
542 {
543         u32 user_size, dev_size;
544         u32 *indir;
545         int ret;
546
547         if (!dev->ethtool_ops->get_rxfh_indir_size ||
548             !dev->ethtool_ops->get_rxfh_indir)
549                 return -EOPNOTSUPP;
550         dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
551         if (dev_size == 0)
552                 return -EOPNOTSUPP;
553
554         if (copy_from_user(&user_size,
555                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
556                            sizeof(user_size)))
557                 return -EFAULT;
558
559         if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
560                          &dev_size, sizeof(dev_size)))
561                 return -EFAULT;
562
563         /* If the user buffer size is 0, this is just a query for the
564          * device table size.  Otherwise, if it's smaller than the
565          * device table size it's an error.
566          */
567         if (user_size < dev_size)
568                 return user_size == 0 ? 0 : -EINVAL;
569
570         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
571         if (!indir)
572                 return -ENOMEM;
573
574         ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
575         if (ret)
576                 goto out;
577
578         if (copy_to_user(useraddr +
579                          offsetof(struct ethtool_rxfh_indir, ring_index[0]),
580                          indir, dev_size * sizeof(indir[0])))
581                 ret = -EFAULT;
582
583 out:
584         kfree(indir);
585         return ret;
586 }
587
588 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
589                                                      void __user *useraddr)
590 {
591         struct ethtool_rxnfc rx_rings;
592         u32 user_size, dev_size, i;
593         u32 *indir;
594         const struct ethtool_ops *ops = dev->ethtool_ops;
595         int ret;
596
597         if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
598             !ops->get_rxnfc)
599                 return -EOPNOTSUPP;
600
601         dev_size = ops->get_rxfh_indir_size(dev);
602         if (dev_size == 0)
603                 return -EOPNOTSUPP;
604
605         if (copy_from_user(&user_size,
606                            useraddr + offsetof(struct ethtool_rxfh_indir, size),
607                            sizeof(user_size)))
608                 return -EFAULT;
609
610         if (user_size != 0 && user_size != dev_size)
611                 return -EINVAL;
612
613         indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
614         if (!indir)
615                 return -ENOMEM;
616
617         rx_rings.cmd = ETHTOOL_GRXRINGS;
618         ret = ops->get_rxnfc(dev, &rx_rings, NULL);
619         if (ret)
620                 goto out;
621
622         if (user_size == 0) {
623                 for (i = 0; i < dev_size; i++)
624                         indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
625         } else {
626                 if (copy_from_user(indir,
627                                   useraddr +
628                                   offsetof(struct ethtool_rxfh_indir,
629                                            ring_index[0]),
630                                   dev_size * sizeof(indir[0]))) {
631                         ret = -EFAULT;
632                         goto out;
633                 }
634
635                 /* Validate ring indices */
636                 for (i = 0; i < dev_size; i++) {
637                         if (indir[i] >= rx_rings.data) {
638                                 ret = -EINVAL;
639                                 goto out;
640                         }
641                 }
642         }
643
644         ret = ops->set_rxfh_indir(dev, indir);
645
646 out:
647         kfree(indir);
648         return ret;
649 }
650
651 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
652 {
653         struct ethtool_regs regs;
654         const struct ethtool_ops *ops = dev->ethtool_ops;
655         void *regbuf;
656         int reglen, ret;
657
658         if (!ops->get_regs || !ops->get_regs_len)
659                 return -EOPNOTSUPP;
660
661         if (copy_from_user(&regs, useraddr, sizeof(regs)))
662                 return -EFAULT;
663
664         reglen = ops->get_regs_len(dev);
665         if (regs.len > reglen)
666                 regs.len = reglen;
667
668         regbuf = vzalloc(reglen);
669         if (reglen && !regbuf)
670                 return -ENOMEM;
671
672         ops->get_regs(dev, &regs, regbuf);
673
674         ret = -EFAULT;
675         if (copy_to_user(useraddr, &regs, sizeof(regs)))
676                 goto out;
677         useraddr += offsetof(struct ethtool_regs, data);
678         if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
679                 goto out;
680         ret = 0;
681
682  out:
683         vfree(regbuf);
684         return ret;
685 }
686
687 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
688 {
689         struct ethtool_value reset;
690         int ret;
691
692         if (!dev->ethtool_ops->reset)
693                 return -EOPNOTSUPP;
694
695         if (copy_from_user(&reset, useraddr, sizeof(reset)))
696                 return -EFAULT;
697
698         ret = dev->ethtool_ops->reset(dev, &reset.data);
699         if (ret)
700                 return ret;
701
702         if (copy_to_user(useraddr, &reset, sizeof(reset)))
703                 return -EFAULT;
704         return 0;
705 }
706
707 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
708 {
709         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
710
711         if (!dev->ethtool_ops->get_wol)
712                 return -EOPNOTSUPP;
713
714         dev->ethtool_ops->get_wol(dev, &wol);
715
716         if (copy_to_user(useraddr, &wol, sizeof(wol)))
717                 return -EFAULT;
718         return 0;
719 }
720
721 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
722 {
723         struct ethtool_wolinfo wol;
724
725         if (!dev->ethtool_ops->set_wol)
726                 return -EOPNOTSUPP;
727
728         if (copy_from_user(&wol, useraddr, sizeof(wol)))
729                 return -EFAULT;
730
731         return dev->ethtool_ops->set_wol(dev, &wol);
732 }
733
734 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
735 {
736         struct ethtool_eee edata;
737         int rc;
738
739         if (!dev->ethtool_ops->get_eee)
740                 return -EOPNOTSUPP;
741
742         memset(&edata, 0, sizeof(struct ethtool_eee));
743         edata.cmd = ETHTOOL_GEEE;
744         rc = dev->ethtool_ops->get_eee(dev, &edata);
745
746         if (rc)
747                 return rc;
748
749         if (copy_to_user(useraddr, &edata, sizeof(edata)))
750                 return -EFAULT;
751
752         return 0;
753 }
754
755 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
756 {
757         struct ethtool_eee edata;
758
759         if (!dev->ethtool_ops->set_eee)
760                 return -EOPNOTSUPP;
761
762         if (copy_from_user(&edata, useraddr, sizeof(edata)))
763                 return -EFAULT;
764
765         return dev->ethtool_ops->set_eee(dev, &edata);
766 }
767
768 static int ethtool_nway_reset(struct net_device *dev)
769 {
770         if (!dev->ethtool_ops->nway_reset)
771                 return -EOPNOTSUPP;
772
773         return dev->ethtool_ops->nway_reset(dev);
774 }
775
776 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
777 {
778         struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
779
780         if (!dev->ethtool_ops->get_link)
781                 return -EOPNOTSUPP;
782
783         edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
784
785         if (copy_to_user(useraddr, &edata, sizeof(edata)))
786                 return -EFAULT;
787         return 0;
788 }
789
790 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
791                                   int (*getter)(struct net_device *,
792                                                 struct ethtool_eeprom *, u8 *),
793                                   u32 total_len)
794 {
795         struct ethtool_eeprom eeprom;
796         void __user *userbuf = useraddr + sizeof(eeprom);
797         u32 bytes_remaining;
798         u8 *data;
799         int ret = 0;
800
801         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
802                 return -EFAULT;
803
804         /* Check for wrap and zero */
805         if (eeprom.offset + eeprom.len <= eeprom.offset)
806                 return -EINVAL;
807
808         /* Check for exceeding total eeprom len */
809         if (eeprom.offset + eeprom.len > total_len)
810                 return -EINVAL;
811
812         data = kmalloc(PAGE_SIZE, GFP_USER);
813         if (!data)
814                 return -ENOMEM;
815
816         bytes_remaining = eeprom.len;
817         while (bytes_remaining > 0) {
818                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
819
820                 ret = getter(dev, &eeprom, data);
821                 if (ret)
822                         break;
823                 if (copy_to_user(userbuf, data, eeprom.len)) {
824                         ret = -EFAULT;
825                         break;
826                 }
827                 userbuf += eeprom.len;
828                 eeprom.offset += eeprom.len;
829                 bytes_remaining -= eeprom.len;
830         }
831
832         eeprom.len = userbuf - (useraddr + sizeof(eeprom));
833         eeprom.offset -= eeprom.len;
834         if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
835                 ret = -EFAULT;
836
837         kfree(data);
838         return ret;
839 }
840
841 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
842 {
843         const struct ethtool_ops *ops = dev->ethtool_ops;
844
845         if (!ops->get_eeprom || !ops->get_eeprom_len)
846                 return -EOPNOTSUPP;
847
848         return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
849                                       ops->get_eeprom_len(dev));
850 }
851
852 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
853 {
854         struct ethtool_eeprom eeprom;
855         const struct ethtool_ops *ops = dev->ethtool_ops;
856         void __user *userbuf = useraddr + sizeof(eeprom);
857         u32 bytes_remaining;
858         u8 *data;
859         int ret = 0;
860
861         if (!ops->set_eeprom || !ops->get_eeprom_len)
862                 return -EOPNOTSUPP;
863
864         if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
865                 return -EFAULT;
866
867         /* Check for wrap and zero */
868         if (eeprom.offset + eeprom.len <= eeprom.offset)
869                 return -EINVAL;
870
871         /* Check for exceeding total eeprom len */
872         if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
873                 return -EINVAL;
874
875         data = kmalloc(PAGE_SIZE, GFP_USER);
876         if (!data)
877                 return -ENOMEM;
878
879         bytes_remaining = eeprom.len;
880         while (bytes_remaining > 0) {
881                 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
882
883                 if (copy_from_user(data, userbuf, eeprom.len)) {
884                         ret = -EFAULT;
885                         break;
886                 }
887                 ret = ops->set_eeprom(dev, &eeprom, data);
888                 if (ret)
889                         break;
890                 userbuf += eeprom.len;
891                 eeprom.offset += eeprom.len;
892                 bytes_remaining -= eeprom.len;
893         }
894
895         kfree(data);
896         return ret;
897 }
898
899 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
900                                                    void __user *useraddr)
901 {
902         struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
903
904         if (!dev->ethtool_ops->get_coalesce)
905                 return -EOPNOTSUPP;
906
907         dev->ethtool_ops->get_coalesce(dev, &coalesce);
908
909         if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
910                 return -EFAULT;
911         return 0;
912 }
913
914 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
915                                                    void __user *useraddr)
916 {
917         struct ethtool_coalesce coalesce;
918
919         if (!dev->ethtool_ops->set_coalesce)
920                 return -EOPNOTSUPP;
921
922         if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
923                 return -EFAULT;
924
925         return dev->ethtool_ops->set_coalesce(dev, &coalesce);
926 }
927
928 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
929 {
930         struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
931
932         if (!dev->ethtool_ops->get_ringparam)
933                 return -EOPNOTSUPP;
934
935         dev->ethtool_ops->get_ringparam(dev, &ringparam);
936
937         if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
938                 return -EFAULT;
939         return 0;
940 }
941
942 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
943 {
944         struct ethtool_ringparam ringparam;
945
946         if (!dev->ethtool_ops->set_ringparam)
947                 return -EOPNOTSUPP;
948
949         if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
950                 return -EFAULT;
951
952         return dev->ethtool_ops->set_ringparam(dev, &ringparam);
953 }
954
955 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
956                                                    void __user *useraddr)
957 {
958         struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
959
960         if (!dev->ethtool_ops->get_channels)
961                 return -EOPNOTSUPP;
962
963         dev->ethtool_ops->get_channels(dev, &channels);
964
965         if (copy_to_user(useraddr, &channels, sizeof(channels)))
966                 return -EFAULT;
967         return 0;
968 }
969
970 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
971                                                    void __user *useraddr)
972 {
973         struct ethtool_channels channels;
974
975         if (!dev->ethtool_ops->set_channels)
976                 return -EOPNOTSUPP;
977
978         if (copy_from_user(&channels, useraddr, sizeof(channels)))
979                 return -EFAULT;
980
981         return dev->ethtool_ops->set_channels(dev, &channels);
982 }
983
984 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
985 {
986         struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
987
988         if (!dev->ethtool_ops->get_pauseparam)
989                 return -EOPNOTSUPP;
990
991         dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
992
993         if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
994                 return -EFAULT;
995         return 0;
996 }
997
998 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
999 {
1000         struct ethtool_pauseparam pauseparam;
1001
1002         if (!dev->ethtool_ops->set_pauseparam)
1003                 return -EOPNOTSUPP;
1004
1005         if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1006                 return -EFAULT;
1007
1008         return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1009 }
1010
1011 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1012 {
1013         struct ethtool_test test;
1014         const struct ethtool_ops *ops = dev->ethtool_ops;
1015         u64 *data;
1016         int ret, test_len;
1017
1018         if (!ops->self_test || !ops->get_sset_count)
1019                 return -EOPNOTSUPP;
1020
1021         test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1022         if (test_len < 0)
1023                 return test_len;
1024         WARN_ON(test_len == 0);
1025
1026         if (copy_from_user(&test, useraddr, sizeof(test)))
1027                 return -EFAULT;
1028
1029         test.len = test_len;
1030         data = kmalloc(test_len * sizeof(u64), GFP_USER);
1031         if (!data)
1032                 return -ENOMEM;
1033
1034         ops->self_test(dev, &test, data);
1035
1036         ret = -EFAULT;
1037         if (copy_to_user(useraddr, &test, sizeof(test)))
1038                 goto out;
1039         useraddr += sizeof(test);
1040         if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1041                 goto out;
1042         ret = 0;
1043
1044  out:
1045         kfree(data);
1046         return ret;
1047 }
1048
1049 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1050 {
1051         struct ethtool_gstrings gstrings;
1052         u8 *data;
1053         int ret;
1054
1055         if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1056                 return -EFAULT;
1057
1058         ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1059         if (ret < 0)
1060                 return ret;
1061
1062         gstrings.len = ret;
1063
1064         data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1065         if (!data)
1066                 return -ENOMEM;
1067
1068         __ethtool_get_strings(dev, gstrings.string_set, data);
1069
1070         ret = -EFAULT;
1071         if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1072                 goto out;
1073         useraddr += sizeof(gstrings);
1074         if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1075                 goto out;
1076         ret = 0;
1077
1078 out:
1079         kfree(data);
1080         return ret;
1081 }
1082
1083 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1084 {
1085         struct ethtool_value id;
1086         static bool busy;
1087         const struct ethtool_ops *ops = dev->ethtool_ops;
1088         int rc;
1089
1090         if (!ops->set_phys_id)
1091                 return -EOPNOTSUPP;
1092
1093         if (busy)
1094                 return -EBUSY;
1095
1096         if (copy_from_user(&id, useraddr, sizeof(id)))
1097                 return -EFAULT;
1098
1099         rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1100         if (rc < 0)
1101                 return rc;
1102
1103         /* Drop the RTNL lock while waiting, but prevent reentry or
1104          * removal of the device.
1105          */
1106         busy = true;
1107         dev_hold(dev);
1108         rtnl_unlock();
1109
1110         if (rc == 0) {
1111                 /* Driver will handle this itself */
1112                 schedule_timeout_interruptible(
1113                         id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1114         } else {
1115                 /* Driver expects to be called at twice the frequency in rc */
1116                 int n = rc * 2, i, interval = HZ / n;
1117
1118                 /* Count down seconds */
1119                 do {
1120                         /* Count down iterations per second */
1121                         i = n;
1122                         do {
1123                                 rtnl_lock();
1124                                 rc = ops->set_phys_id(dev,
1125                                     (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1126                                 rtnl_unlock();
1127                                 if (rc)
1128                                         break;
1129                                 schedule_timeout_interruptible(interval);
1130                         } while (!signal_pending(current) && --i != 0);
1131                 } while (!signal_pending(current) &&
1132                          (id.data == 0 || --id.data != 0));
1133         }
1134
1135         rtnl_lock();
1136         dev_put(dev);
1137         busy = false;
1138
1139         (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1140         return rc;
1141 }
1142
1143 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1144 {
1145         struct ethtool_stats stats;
1146         const struct ethtool_ops *ops = dev->ethtool_ops;
1147         u64 *data;
1148         int ret, n_stats;
1149
1150         if (!ops->get_ethtool_stats || !ops->get_sset_count)
1151                 return -EOPNOTSUPP;
1152
1153         n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1154         if (n_stats < 0)
1155                 return n_stats;
1156         WARN_ON(n_stats == 0);
1157
1158         if (copy_from_user(&stats, useraddr, sizeof(stats)))
1159                 return -EFAULT;
1160
1161         stats.n_stats = n_stats;
1162         data = kmalloc(n_stats * sizeof(u64), GFP_USER);
1163         if (!data)
1164                 return -ENOMEM;
1165
1166         ops->get_ethtool_stats(dev, &stats, data);
1167
1168         ret = -EFAULT;
1169         if (copy_to_user(useraddr, &stats, sizeof(stats)))
1170                 goto out;
1171         useraddr += sizeof(stats);
1172         if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1173                 goto out;
1174         ret = 0;
1175
1176  out:
1177         kfree(data);
1178         return ret;
1179 }
1180
1181 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1182 {
1183         struct ethtool_perm_addr epaddr;
1184
1185         if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1186                 return -EFAULT;
1187
1188         if (epaddr.size < dev->addr_len)
1189                 return -ETOOSMALL;
1190         epaddr.size = dev->addr_len;
1191
1192         if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
1193                 return -EFAULT;
1194         useraddr += sizeof(epaddr);
1195         if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1196                 return -EFAULT;
1197         return 0;
1198 }
1199
1200 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1201                              u32 cmd, u32 (*actor)(struct net_device *))
1202 {
1203         struct ethtool_value edata = { .cmd = cmd };
1204
1205         if (!actor)
1206                 return -EOPNOTSUPP;
1207
1208         edata.data = actor(dev);
1209
1210         if (copy_to_user(useraddr, &edata, sizeof(edata)))
1211                 return -EFAULT;
1212         return 0;
1213 }
1214
1215 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1216                              void (*actor)(struct net_device *, u32))
1217 {
1218         struct ethtool_value edata;
1219
1220         if (!actor)
1221                 return -EOPNOTSUPP;
1222
1223         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1224                 return -EFAULT;
1225
1226         actor(dev, edata.data);
1227         return 0;
1228 }
1229
1230 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1231                              int (*actor)(struct net_device *, u32))
1232 {
1233         struct ethtool_value edata;
1234
1235         if (!actor)
1236                 return -EOPNOTSUPP;
1237
1238         if (copy_from_user(&edata, useraddr, sizeof(edata)))
1239                 return -EFAULT;
1240
1241         return actor(dev, edata.data);
1242 }
1243
1244 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1245                                                    char __user *useraddr)
1246 {
1247         struct ethtool_flash efl;
1248
1249         if (copy_from_user(&efl, useraddr, sizeof(efl)))
1250                 return -EFAULT;
1251
1252         if (!dev->ethtool_ops->flash_device)
1253                 return -EOPNOTSUPP;
1254
1255         efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1256
1257         return dev->ethtool_ops->flash_device(dev, &efl);
1258 }
1259
1260 static int ethtool_set_dump(struct net_device *dev,
1261                         void __user *useraddr)
1262 {
1263         struct ethtool_dump dump;
1264
1265         if (!dev->ethtool_ops->set_dump)
1266                 return -EOPNOTSUPP;
1267
1268         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1269                 return -EFAULT;
1270
1271         return dev->ethtool_ops->set_dump(dev, &dump);
1272 }
1273
1274 static int ethtool_get_dump_flag(struct net_device *dev,
1275                                 void __user *useraddr)
1276 {
1277         int ret;
1278         struct ethtool_dump dump;
1279         const struct ethtool_ops *ops = dev->ethtool_ops;
1280
1281         if (!ops->get_dump_flag)
1282                 return -EOPNOTSUPP;
1283
1284         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1285                 return -EFAULT;
1286
1287         ret = ops->get_dump_flag(dev, &dump);
1288         if (ret)
1289                 return ret;
1290
1291         if (copy_to_user(useraddr, &dump, sizeof(dump)))
1292                 return -EFAULT;
1293         return 0;
1294 }
1295
1296 static int ethtool_get_dump_data(struct net_device *dev,
1297                                 void __user *useraddr)
1298 {
1299         int ret;
1300         __u32 len;
1301         struct ethtool_dump dump, tmp;
1302         const struct ethtool_ops *ops = dev->ethtool_ops;
1303         void *data = NULL;
1304
1305         if (!ops->get_dump_data || !ops->get_dump_flag)
1306                 return -EOPNOTSUPP;
1307
1308         if (copy_from_user(&dump, useraddr, sizeof(dump)))
1309                 return -EFAULT;
1310
1311         memset(&tmp, 0, sizeof(tmp));
1312         tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1313         ret = ops->get_dump_flag(dev, &tmp);
1314         if (ret)
1315                 return ret;
1316
1317         len = (tmp.len > dump.len) ? dump.len : tmp.len;
1318         if (!len)
1319                 return -EFAULT;
1320
1321         data = vzalloc(tmp.len);
1322         if (!data)
1323                 return -ENOMEM;
1324         ret = ops->get_dump_data(dev, &dump, data);
1325         if (ret)
1326                 goto out;
1327
1328         if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1329                 ret = -EFAULT;
1330                 goto out;
1331         }
1332         useraddr += offsetof(struct ethtool_dump, data);
1333         if (copy_to_user(useraddr, data, len))
1334                 ret = -EFAULT;
1335 out:
1336         vfree(data);
1337         return ret;
1338 }
1339
1340 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1341 {
1342         int err = 0;
1343         struct ethtool_ts_info info;
1344         const struct ethtool_ops *ops = dev->ethtool_ops;
1345         struct phy_device *phydev = dev->phydev;
1346
1347         memset(&info, 0, sizeof(info));
1348         info.cmd = ETHTOOL_GET_TS_INFO;
1349
1350         if (phydev && phydev->drv && phydev->drv->ts_info) {
1351                 err = phydev->drv->ts_info(phydev, &info);
1352         } else if (ops->get_ts_info) {
1353                 err = ops->get_ts_info(dev, &info);
1354         } else {
1355                 info.so_timestamping =
1356                         SOF_TIMESTAMPING_RX_SOFTWARE |
1357                         SOF_TIMESTAMPING_SOFTWARE;
1358                 info.phc_index = -1;
1359         }
1360
1361         if (err)
1362                 return err;
1363
1364         if (copy_to_user(useraddr, &info, sizeof(info)))
1365                 err = -EFAULT;
1366
1367         return err;
1368 }
1369
1370 static int ethtool_get_module_info(struct net_device *dev,
1371                                    void __user *useraddr)
1372 {
1373         int ret;
1374         struct ethtool_modinfo modinfo;
1375         const struct ethtool_ops *ops = dev->ethtool_ops;
1376
1377         if (!ops->get_module_info)
1378                 return -EOPNOTSUPP;
1379
1380         if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1381                 return -EFAULT;
1382
1383         ret = ops->get_module_info(dev, &modinfo);
1384         if (ret)
1385                 return ret;
1386
1387         if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1388                 return -EFAULT;
1389
1390         return 0;
1391 }
1392
1393 static int ethtool_get_module_eeprom(struct net_device *dev,
1394                                      void __user *useraddr)
1395 {
1396         int ret;
1397         struct ethtool_modinfo modinfo;
1398         const struct ethtool_ops *ops = dev->ethtool_ops;
1399
1400         if (!ops->get_module_info || !ops->get_module_eeprom)
1401                 return -EOPNOTSUPP;
1402
1403         ret = ops->get_module_info(dev, &modinfo);
1404         if (ret)
1405                 return ret;
1406
1407         return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1408                                       modinfo.eeprom_len);
1409 }
1410
1411 /* The main entry point in this file.  Called from net/core/dev.c */
1412
1413 int dev_ethtool(struct net *net, struct ifreq *ifr)
1414 {
1415         struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1416         void __user *useraddr = ifr->ifr_data;
1417         u32 ethcmd;
1418         int rc;
1419         u32 old_features;
1420
1421         if (!dev || !netif_device_present(dev))
1422                 return -ENODEV;
1423
1424         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1425                 return -EFAULT;
1426
1427         /* Allow some commands to be done by anyone */
1428         switch (ethcmd) {
1429         case ETHTOOL_GSET:
1430         case ETHTOOL_GDRVINFO:
1431         case ETHTOOL_GMSGLVL:
1432         case ETHTOOL_GLINK:
1433         case ETHTOOL_GCOALESCE:
1434         case ETHTOOL_GRINGPARAM:
1435         case ETHTOOL_GPAUSEPARAM:
1436         case ETHTOOL_GRXCSUM:
1437         case ETHTOOL_GTXCSUM:
1438         case ETHTOOL_GSG:
1439         case ETHTOOL_GSSET_INFO:
1440         case ETHTOOL_GSTRINGS:
1441         case ETHTOOL_GSTATS:
1442         case ETHTOOL_GTSO:
1443         case ETHTOOL_GPERMADDR:
1444         case ETHTOOL_GUFO:
1445         case ETHTOOL_GGSO:
1446         case ETHTOOL_GGRO:
1447         case ETHTOOL_GFLAGS:
1448         case ETHTOOL_GPFLAGS:
1449         case ETHTOOL_GRXFH:
1450         case ETHTOOL_GRXRINGS:
1451         case ETHTOOL_GRXCLSRLCNT:
1452         case ETHTOOL_GRXCLSRULE:
1453         case ETHTOOL_GRXCLSRLALL:
1454         case ETHTOOL_GRXFHINDIR:
1455         case ETHTOOL_GFEATURES:
1456         case ETHTOOL_GCHANNELS:
1457         case ETHTOOL_GET_TS_INFO:
1458         case ETHTOOL_GEEE:
1459                 break;
1460         default:
1461                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1462                         return -EPERM;
1463         }
1464
1465         if (dev->ethtool_ops->begin) {
1466                 rc = dev->ethtool_ops->begin(dev);
1467                 if (rc  < 0)
1468                         return rc;
1469         }
1470         old_features = dev->features;
1471
1472         switch (ethcmd) {
1473         case ETHTOOL_GSET:
1474                 rc = ethtool_get_settings(dev, useraddr);
1475                 break;
1476         case ETHTOOL_SSET:
1477                 rc = ethtool_set_settings(dev, useraddr);
1478                 break;
1479         case ETHTOOL_GDRVINFO:
1480                 rc = ethtool_get_drvinfo(dev, useraddr);
1481                 break;
1482         case ETHTOOL_GREGS:
1483                 rc = ethtool_get_regs(dev, useraddr);
1484                 break;
1485         case ETHTOOL_GWOL:
1486                 rc = ethtool_get_wol(dev, useraddr);
1487                 break;
1488         case ETHTOOL_SWOL:
1489                 rc = ethtool_set_wol(dev, useraddr);
1490                 break;
1491         case ETHTOOL_GMSGLVL:
1492                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1493                                        dev->ethtool_ops->get_msglevel);
1494                 break;
1495         case ETHTOOL_SMSGLVL:
1496                 rc = ethtool_set_value_void(dev, useraddr,
1497                                        dev->ethtool_ops->set_msglevel);
1498                 break;
1499         case ETHTOOL_GEEE:
1500                 rc = ethtool_get_eee(dev, useraddr);
1501                 break;
1502         case ETHTOOL_SEEE:
1503                 rc = ethtool_set_eee(dev, useraddr);
1504                 break;
1505         case ETHTOOL_NWAY_RST:
1506                 rc = ethtool_nway_reset(dev);
1507                 break;
1508         case ETHTOOL_GLINK:
1509                 rc = ethtool_get_link(dev, useraddr);
1510                 break;
1511         case ETHTOOL_GEEPROM:
1512                 rc = ethtool_get_eeprom(dev, useraddr);
1513                 break;
1514         case ETHTOOL_SEEPROM:
1515                 rc = ethtool_set_eeprom(dev, useraddr);
1516                 break;
1517         case ETHTOOL_GCOALESCE:
1518                 rc = ethtool_get_coalesce(dev, useraddr);
1519                 break;
1520         case ETHTOOL_SCOALESCE:
1521                 rc = ethtool_set_coalesce(dev, useraddr);
1522                 break;
1523         case ETHTOOL_GRINGPARAM:
1524                 rc = ethtool_get_ringparam(dev, useraddr);
1525                 break;
1526         case ETHTOOL_SRINGPARAM:
1527                 rc = ethtool_set_ringparam(dev, useraddr);
1528                 break;
1529         case ETHTOOL_GPAUSEPARAM:
1530                 rc = ethtool_get_pauseparam(dev, useraddr);
1531                 break;
1532         case ETHTOOL_SPAUSEPARAM:
1533                 rc = ethtool_set_pauseparam(dev, useraddr);
1534                 break;
1535         case ETHTOOL_TEST:
1536                 rc = ethtool_self_test(dev, useraddr);
1537                 break;
1538         case ETHTOOL_GSTRINGS:
1539                 rc = ethtool_get_strings(dev, useraddr);
1540                 break;
1541         case ETHTOOL_PHYS_ID:
1542                 rc = ethtool_phys_id(dev, useraddr);
1543                 break;
1544         case ETHTOOL_GSTATS:
1545                 rc = ethtool_get_stats(dev, useraddr);
1546                 break;
1547         case ETHTOOL_GPERMADDR:
1548                 rc = ethtool_get_perm_addr(dev, useraddr);
1549                 break;
1550         case ETHTOOL_GFLAGS:
1551                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1552                                         __ethtool_get_flags);
1553                 break;
1554         case ETHTOOL_SFLAGS:
1555                 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
1556                 break;
1557         case ETHTOOL_GPFLAGS:
1558                 rc = ethtool_get_value(dev, useraddr, ethcmd,
1559                                        dev->ethtool_ops->get_priv_flags);
1560                 break;
1561         case ETHTOOL_SPFLAGS:
1562                 rc = ethtool_set_value(dev, useraddr,
1563                                        dev->ethtool_ops->set_priv_flags);
1564                 break;
1565         case ETHTOOL_GRXFH:
1566         case ETHTOOL_GRXRINGS:
1567         case ETHTOOL_GRXCLSRLCNT:
1568         case ETHTOOL_GRXCLSRULE:
1569         case ETHTOOL_GRXCLSRLALL:
1570                 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
1571                 break;
1572         case ETHTOOL_SRXFH:
1573         case ETHTOOL_SRXCLSRLDEL:
1574         case ETHTOOL_SRXCLSRLINS:
1575                 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
1576                 break;
1577         case ETHTOOL_FLASHDEV:
1578                 rc = ethtool_flash_device(dev, useraddr);
1579                 break;
1580         case ETHTOOL_RESET:
1581                 rc = ethtool_reset(dev, useraddr);
1582                 break;
1583         case ETHTOOL_GSSET_INFO:
1584                 rc = ethtool_get_sset_info(dev, useraddr);
1585                 break;
1586         case ETHTOOL_GRXFHINDIR:
1587                 rc = ethtool_get_rxfh_indir(dev, useraddr);
1588                 break;
1589         case ETHTOOL_SRXFHINDIR:
1590                 rc = ethtool_set_rxfh_indir(dev, useraddr);
1591                 break;
1592         case ETHTOOL_GFEATURES:
1593                 rc = ethtool_get_features(dev, useraddr);
1594                 break;
1595         case ETHTOOL_SFEATURES:
1596                 rc = ethtool_set_features(dev, useraddr);
1597                 break;
1598         case ETHTOOL_GTXCSUM:
1599         case ETHTOOL_GRXCSUM:
1600         case ETHTOOL_GSG:
1601         case ETHTOOL_GTSO:
1602         case ETHTOOL_GUFO:
1603         case ETHTOOL_GGSO:
1604         case ETHTOOL_GGRO:
1605                 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1606                 break;
1607         case ETHTOOL_STXCSUM:
1608         case ETHTOOL_SRXCSUM:
1609         case ETHTOOL_SSG:
1610         case ETHTOOL_STSO:
1611         case ETHTOOL_SUFO:
1612         case ETHTOOL_SGSO:
1613         case ETHTOOL_SGRO:
1614                 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1615                 break;
1616         case ETHTOOL_GCHANNELS:
1617                 rc = ethtool_get_channels(dev, useraddr);
1618                 break;
1619         case ETHTOOL_SCHANNELS:
1620                 rc = ethtool_set_channels(dev, useraddr);
1621                 break;
1622         case ETHTOOL_SET_DUMP:
1623                 rc = ethtool_set_dump(dev, useraddr);
1624                 break;
1625         case ETHTOOL_GET_DUMP_FLAG:
1626                 rc = ethtool_get_dump_flag(dev, useraddr);
1627                 break;
1628         case ETHTOOL_GET_DUMP_DATA:
1629                 rc = ethtool_get_dump_data(dev, useraddr);
1630                 break;
1631         case ETHTOOL_GET_TS_INFO:
1632                 rc = ethtool_get_ts_info(dev, useraddr);
1633                 break;
1634         case ETHTOOL_GMODULEINFO:
1635                 rc = ethtool_get_module_info(dev, useraddr);
1636                 break;
1637         case ETHTOOL_GMODULEEEPROM:
1638                 rc = ethtool_get_module_eeprom(dev, useraddr);
1639                 break;
1640         default:
1641                 rc = -EOPNOTSUPP;
1642         }
1643
1644         if (dev->ethtool_ops->complete)
1645                 dev->ethtool_ops->complete(dev);
1646
1647         if (old_features != dev->features)
1648                 netdev_features_change(dev);
1649
1650         return rc;
1651 }