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