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