blob: 862989898f611e8f9f2dea28b273b551eeb2b0bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010026#include <linux/rtnetlink.h>
27#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 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
35u32 ethtool_op_get_link(struct net_device *dev)
36{
37 return netif_carrier_ok(dev) ? 1 : 0;
38}
chavey97f8aef2010-04-07 21:54:42 -070039EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Richard Cochran02eacbd2012-04-03 22:59:22 +000041int 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}
50EXPORT_SYMBOL(ethtool_op_get_ts_info);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* Handlers for each ethtool command */
53
Michał Mirosław475414f2011-11-15 15:29:55 +000054#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000055
Michał Mirosław9d921542011-11-15 15:29:55 +000056static 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",
Michał Mirosław9d921542011-11-15 15:29:55 +000059 [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",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090063 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000064
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090065 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
66 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040067 [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",
Michał Mirosław9d921542011-11-15 15:29:55 +000070 [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",
Pravin B Shelar68c33162013-02-14 14:02:41 +000083 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Eric Dumazetcb32f512013-10-19 11:42:57 -070084 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
Eric Dumazet61c1db72013-10-20 20:47:30 -070085 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000086 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Simon Horman0d89d202013-05-23 21:02:52 +000087 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000088
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",
Ben Greear36eabda32012-02-11 15:39:14 +000097 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000098 [NETIF_F_RXALL_BIT] = "rx-all",
Michał Mirosław9d921542011-11-15 15:29:55 +000099};
100
Michał Mirosław5455c692011-02-15 16:59:17 +0000101static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
102{
103 struct ethtool_gfeatures cmd = {
104 .cmd = ETHTOOL_GFEATURES,
105 .size = ETHTOOL_DEV_FEATURE_WORDS,
106 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000107 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000108 u32 __user *sizeaddr;
109 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000110 int i;
111
112 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000113 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000114
115 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000116 features[i].available = (u32)(dev->hw_features >> (32 * i));
117 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
118 features[i].active = (u32)(dev->features >> (32 * i));
119 features[i].never_changed =
120 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000121 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000122
123 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
124 if (get_user(copy_size, sizeaddr))
125 return -EFAULT;
126
127 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
128 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
129
130 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
131 return -EFAULT;
132 useraddr += sizeof(cmd);
133 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
134 return -EFAULT;
135
136 return 0;
137}
138
139static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
140{
141 struct ethtool_sfeatures cmd;
142 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000143 netdev_features_t wanted = 0, valid = 0;
144 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000145
146 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
147 return -EFAULT;
148 useraddr += sizeof(cmd);
149
150 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
151 return -EINVAL;
152
153 if (copy_from_user(features, useraddr, sizeof(features)))
154 return -EFAULT;
155
Michał Mirosław475414f2011-11-15 15:29:55 +0000156 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000157 valid |= (netdev_features_t)features[i].valid << (32 * i);
158 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000159 }
160
161 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000162 return -EINVAL;
163
Michał Mirosław475414f2011-11-15 15:29:55 +0000164 if (valid & ~dev->hw_features) {
165 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000166 ret |= ETHTOOL_F_UNSUPPORTED;
167 }
168
Michał Mirosław475414f2011-11-15 15:29:55 +0000169 dev->wanted_features &= ~valid;
170 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700171 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000172
Michał Mirosław475414f2011-11-15 15:29:55 +0000173 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000174 ret |= ETHTOOL_F_WISH;
175
176 return ret;
177}
178
Michał Mirosław340ae162011-02-15 16:59:16 +0000179static int __ethtool_get_sset_count(struct net_device *dev, int sset)
180{
181 const struct ethtool_ops *ops = dev->ethtool_ops;
182
Michał Mirosław5455c692011-02-15 16:59:17 +0000183 if (sset == ETH_SS_FEATURES)
184 return ARRAY_SIZE(netdev_features_strings);
185
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000186 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000187 return ops->get_sset_count(dev, sset);
188 else
189 return -EOPNOTSUPP;
190}
191
192static void __ethtool_get_strings(struct net_device *dev,
193 u32 stringset, u8 *data)
194{
195 const struct ethtool_ops *ops = dev->ethtool_ops;
196
Michał Mirosław5455c692011-02-15 16:59:17 +0000197 if (stringset == ETH_SS_FEATURES)
198 memcpy(data, netdev_features_strings,
199 sizeof(netdev_features_strings));
200 else
201 /* ops->get_strings is valid because checked earlier */
202 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000203}
204
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000205static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000206{
207 /* feature masks of legacy discrete ethtool ops */
208
209 switch (eth_cmd) {
210 case ETHTOOL_GTXCSUM:
211 case ETHTOOL_STXCSUM:
212 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000213 case ETHTOOL_GRXCSUM:
214 case ETHTOOL_SRXCSUM:
215 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000216 case ETHTOOL_GSG:
217 case ETHTOOL_SSG:
218 return NETIF_F_SG;
219 case ETHTOOL_GTSO:
220 case ETHTOOL_STSO:
221 return NETIF_F_ALL_TSO;
222 case ETHTOOL_GUFO:
223 case ETHTOOL_SUFO:
224 return NETIF_F_UFO;
225 case ETHTOOL_GGSO:
226 case ETHTOOL_SGSO:
227 return NETIF_F_GSO;
228 case ETHTOOL_GGRO:
229 case ETHTOOL_SGRO:
230 return NETIF_F_GRO;
231 default:
232 BUG();
233 }
234}
235
Michał Mirosław0a417702011-02-15 16:59:17 +0000236static int ethtool_get_one_feature(struct net_device *dev,
237 char __user *useraddr, u32 ethcmd)
238{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000239 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000240 struct ethtool_value edata = {
241 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000242 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000243 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000244
Michał Mirosław0a417702011-02-15 16:59:17 +0000245 if (copy_to_user(useraddr, &edata, sizeof(edata)))
246 return -EFAULT;
247 return 0;
248}
249
Michał Mirosław0a417702011-02-15 16:59:17 +0000250static int ethtool_set_one_feature(struct net_device *dev,
251 void __user *useraddr, u32 ethcmd)
252{
253 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000254 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000255
256 if (copy_from_user(&edata, useraddr, sizeof(edata)))
257 return -EFAULT;
258
Michał Mirosław86794882011-02-15 16:59:17 +0000259 mask = ethtool_get_feature_mask(ethcmd);
260 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000261 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000262 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000263
264 if (edata.data)
265 dev->wanted_features |= mask;
266 else
267 dev->wanted_features &= ~mask;
268
269 __netdev_update_features(dev);
270
271 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000272}
273
Michał Mirosław02b3a552011-11-15 15:29:55 +0000274#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
275 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000276#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
277 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
278 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000279
280static u32 __ethtool_get_flags(struct net_device *dev)
281{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000282 u32 flags = 0;
283
Dragos Foianu8a731252013-07-13 14:43:00 +0100284 if (dev->features & NETIF_F_LRO)
285 flags |= ETH_FLAG_LRO;
286 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
287 flags |= ETH_FLAG_RXVLAN;
288 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
289 flags |= ETH_FLAG_TXVLAN;
290 if (dev->features & NETIF_F_NTUPLE)
291 flags |= ETH_FLAG_NTUPLE;
292 if (dev->features & NETIF_F_RXHASH)
293 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000294
295 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000296}
297
298static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000299{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000300 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000301
Michał Mirosław02b3a552011-11-15 15:29:55 +0000302 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000303 return -EINVAL;
304
Dragos Foianu8a731252013-07-13 14:43:00 +0100305 if (data & ETH_FLAG_LRO)
306 features |= NETIF_F_LRO;
307 if (data & ETH_FLAG_RXVLAN)
308 features |= NETIF_F_HW_VLAN_CTAG_RX;
309 if (data & ETH_FLAG_TXVLAN)
310 features |= NETIF_F_HW_VLAN_CTAG_TX;
311 if (data & ETH_FLAG_NTUPLE)
312 features |= NETIF_F_NTUPLE;
313 if (data & ETH_FLAG_RXHASH)
314 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000315
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000316 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000317 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000318 if (changed & ~dev->hw_features)
319 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
320
321 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000322 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000323
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700324 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000325
326 return 0;
327}
328
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000329int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000331 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000333 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EOPNOTSUPP;
335
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000336 memset(cmd, 0, sizeof(struct ethtool_cmd));
337 cmd->cmd = ETHTOOL_GSET;
338 return dev->ethtool_ops->get_settings(dev, cmd);
339}
340EXPORT_SYMBOL(__ethtool_get_settings);
341
342static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
343{
344 int err;
345 struct ethtool_cmd cmd;
346
347 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (err < 0)
349 return err;
350
351 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
352 return -EFAULT;
353 return 0;
354}
355
356static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
357{
358 struct ethtool_cmd cmd;
359
360 if (!dev->ethtool_ops->set_settings)
361 return -EOPNOTSUPP;
362
363 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
364 return -EFAULT;
365
366 return dev->ethtool_ops->set_settings(dev, &cmd);
367}
368
chavey97f8aef2010-04-07 21:54:42 -0700369static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
370 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700373 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 memset(&info, 0, sizeof(info));
376 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000377 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700378 ops->get_drvinfo(dev, &info);
379 } else if (dev->dev.parent && dev->dev.parent->driver) {
380 strlcpy(info.bus_info, dev_name(dev->dev.parent),
381 sizeof(info.bus_info));
382 strlcpy(info.driver, dev->dev.parent->driver->name,
383 sizeof(info.driver));
384 } else {
385 return -EOPNOTSUPP;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Jeff Garzik723b2f52010-03-03 22:51:50 +0000388 /*
389 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000390 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000391 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000392 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700393 int rc;
394
395 rc = ops->get_sset_count(dev, ETH_SS_TEST);
396 if (rc >= 0)
397 info.testinfo_len = rc;
398 rc = ops->get_sset_count(dev, ETH_SS_STATS);
399 if (rc >= 0)
400 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700401 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
402 if (rc >= 0)
403 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700404 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000405 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000407 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 info.eedump_len = ops->get_eeprom_len(dev);
409
410 if (copy_to_user(useraddr, &info, sizeof(info)))
411 return -EFAULT;
412 return 0;
413}
414
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800415static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700416 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000417{
418 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000419 u64 sset_mask;
420 int i, idx = 0, n_bits = 0, ret, rc;
421 u32 *info_buf = NULL;
422
Jeff Garzik723b2f52010-03-03 22:51:50 +0000423 if (copy_from_user(&info, useraddr, sizeof(info)))
424 return -EFAULT;
425
426 /* store copy of mask, because we zero struct later on */
427 sset_mask = info.sset_mask;
428 if (!sset_mask)
429 return 0;
430
431 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000432 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000433
434 memset(&info, 0, sizeof(info));
435 info.cmd = ETHTOOL_GSSET_INFO;
436
437 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
438 if (!info_buf)
439 return -ENOMEM;
440
441 /*
442 * fill return buffer based on input bitmask and successful
443 * get_sset_count return
444 */
445 for (i = 0; i < 64; i++) {
446 if (!(sset_mask & (1ULL << i)))
447 continue;
448
Michał Mirosław340ae162011-02-15 16:59:16 +0000449 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000450 if (rc >= 0) {
451 info.sset_mask |= (1ULL << i);
452 info_buf[idx++] = rc;
453 }
454 }
455
456 ret = -EFAULT;
457 if (copy_to_user(useraddr, &info, sizeof(info)))
458 goto out;
459
460 useraddr += offsetof(struct ethtool_sset_info, data);
461 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
462 goto out;
463
464 ret = 0;
465
466out:
467 kfree(info_buf);
468 return ret;
469}
470
chavey97f8aef2010-04-07 21:54:42 -0700471static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000472 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700473{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000474 struct ethtool_rxnfc info;
475 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000476 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700477
Santwona Behera59089d82009-02-20 00:58:13 -0800478 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700479 return -EOPNOTSUPP;
480
Ben Hutchingsbf988432010-06-28 08:45:58 +0000481 /* struct ethtool_rxnfc was originally defined for
482 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
483 * members. User-space might still be using that
484 * definition. */
485 if (cmd == ETHTOOL_SRXFH)
486 info_size = (offsetof(struct ethtool_rxnfc, data) +
487 sizeof(info.data));
488
489 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700490 return -EFAULT;
491
Ben Hutchings55664f32012-01-03 12:04:51 +0000492 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
493 if (rc)
494 return rc;
495
496 if (cmd == ETHTOOL_SRXCLSRLINS &&
497 copy_to_user(useraddr, &info, info_size))
498 return -EFAULT;
499
500 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700501}
502
chavey97f8aef2010-04-07 21:54:42 -0700503static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000504 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700505{
506 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000507 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800508 const struct ethtool_ops *ops = dev->ethtool_ops;
509 int ret;
510 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700511
Santwona Behera59089d82009-02-20 00:58:13 -0800512 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700513 return -EOPNOTSUPP;
514
Ben Hutchingsbf988432010-06-28 08:45:58 +0000515 /* struct ethtool_rxnfc was originally defined for
516 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
517 * members. User-space might still be using that
518 * definition. */
519 if (cmd == ETHTOOL_GRXFH)
520 info_size = (offsetof(struct ethtool_rxnfc, data) +
521 sizeof(info.data));
522
523 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700524 return -EFAULT;
525
Santwona Behera59089d82009-02-20 00:58:13 -0800526 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
527 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000528 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000529 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000530 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800531 if (!rule_buf)
532 return -ENOMEM;
533 }
534 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700535
Santwona Behera59089d82009-02-20 00:58:13 -0800536 ret = ops->get_rxnfc(dev, &info, rule_buf);
537 if (ret < 0)
538 goto err_out;
539
540 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000541 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800542 goto err_out;
543
544 if (rule_buf) {
545 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
546 if (copy_to_user(useraddr, rule_buf,
547 info.rule_cnt * sizeof(u32)))
548 goto err_out;
549 }
550 ret = 0;
551
552err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700553 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800554
555 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700556}
557
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000558static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
559 void __user *useraddr)
560{
Ben Hutchings7850f632011-12-15 13:55:01 +0000561 u32 user_size, dev_size;
562 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000563 int ret;
564
Ben Hutchings7850f632011-12-15 13:55:01 +0000565 if (!dev->ethtool_ops->get_rxfh_indir_size ||
566 !dev->ethtool_ops->get_rxfh_indir)
567 return -EOPNOTSUPP;
568 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
569 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000570 return -EOPNOTSUPP;
571
Ben Hutchings7850f632011-12-15 13:55:01 +0000572 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000573 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000574 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000575 return -EFAULT;
576
Ben Hutchings7850f632011-12-15 13:55:01 +0000577 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
578 &dev_size, sizeof(dev_size)))
579 return -EFAULT;
580
581 /* If the user buffer size is 0, this is just a query for the
582 * device table size. Otherwise, if it's smaller than the
583 * device table size it's an error.
584 */
585 if (user_size < dev_size)
586 return user_size == 0 ? 0 : -EINVAL;
587
588 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000589 if (!indir)
590 return -ENOMEM;
591
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000592 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
593 if (ret)
594 goto out;
595
Ben Hutchings7850f632011-12-15 13:55:01 +0000596 if (copy_to_user(useraddr +
597 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
598 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000599 ret = -EFAULT;
600
601out:
602 kfree(indir);
603 return ret;
604}
605
606static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
607 void __user *useraddr)
608{
Ben Hutchings7850f632011-12-15 13:55:01 +0000609 struct ethtool_rxnfc rx_rings;
610 u32 user_size, dev_size, i;
611 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000612 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000613 int ret;
614
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000615 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
616 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000617 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000618
619 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000620 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000621 return -EOPNOTSUPP;
622
Ben Hutchings7850f632011-12-15 13:55:01 +0000623 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000624 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000625 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000626 return -EFAULT;
627
Ben Hutchings278bc422011-12-15 13:56:49 +0000628 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000629 return -EINVAL;
630
631 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000632 if (!indir)
633 return -ENOMEM;
634
Ben Hutchings7850f632011-12-15 13:55:01 +0000635 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000636 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000637 if (ret)
638 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000639
640 if (user_size == 0) {
641 for (i = 0; i < dev_size; i++)
642 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
643 } else {
644 if (copy_from_user(indir,
645 useraddr +
646 offsetof(struct ethtool_rxfh_indir,
647 ring_index[0]),
648 dev_size * sizeof(indir[0]))) {
649 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000650 goto out;
651 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000652
653 /* Validate ring indices */
654 for (i = 0; i < dev_size; i++) {
655 if (indir[i] >= rx_rings.data) {
656 ret = -EINVAL;
657 goto out;
658 }
659 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000660 }
661
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000662 ret = ops->set_rxfh_indir(dev, indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000663
664out:
665 kfree(indir);
666 return ret;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
670{
671 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700672 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 void *regbuf;
674 int reglen, ret;
675
676 if (!ops->get_regs || !ops->get_regs_len)
677 return -EOPNOTSUPP;
678
679 if (copy_from_user(&regs, useraddr, sizeof(regs)))
680 return -EFAULT;
681
682 reglen = ops->get_regs_len(dev);
683 if (regs.len > reglen)
684 regs.len = reglen;
685
Eugene Teob7c7d012011-01-24 21:05:17 -0800686 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700687 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -ENOMEM;
689
690 ops->get_regs(dev, &regs, regbuf);
691
692 ret = -EFAULT;
693 if (copy_to_user(useraddr, &regs, sizeof(regs)))
694 goto out;
695 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700696 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto out;
698 ret = 0;
699
700 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000701 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return ret;
703}
704
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000705static int ethtool_reset(struct net_device *dev, char __user *useraddr)
706{
707 struct ethtool_value reset;
708 int ret;
709
710 if (!dev->ethtool_ops->reset)
711 return -EOPNOTSUPP;
712
713 if (copy_from_user(&reset, useraddr, sizeof(reset)))
714 return -EFAULT;
715
716 ret = dev->ethtool_ops->reset(dev, &reset.data);
717 if (ret)
718 return ret;
719
720 if (copy_to_user(useraddr, &reset, sizeof(reset)))
721 return -EFAULT;
722 return 0;
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
726{
Roland Dreier8e557422010-02-11 12:14:23 -0800727 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (!dev->ethtool_ops->get_wol)
730 return -EOPNOTSUPP;
731
732 dev->ethtool_ops->get_wol(dev, &wol);
733
734 if (copy_to_user(useraddr, &wol, sizeof(wol)))
735 return -EFAULT;
736 return 0;
737}
738
739static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
740{
741 struct ethtool_wolinfo wol;
742
743 if (!dev->ethtool_ops->set_wol)
744 return -EOPNOTSUPP;
745
746 if (copy_from_user(&wol, useraddr, sizeof(wol)))
747 return -EFAULT;
748
749 return dev->ethtool_ops->set_wol(dev, &wol);
750}
751
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000752static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
753{
754 struct ethtool_eee edata;
755 int rc;
756
757 if (!dev->ethtool_ops->get_eee)
758 return -EOPNOTSUPP;
759
760 memset(&edata, 0, sizeof(struct ethtool_eee));
761 edata.cmd = ETHTOOL_GEEE;
762 rc = dev->ethtool_ops->get_eee(dev, &edata);
763
764 if (rc)
765 return rc;
766
767 if (copy_to_user(useraddr, &edata, sizeof(edata)))
768 return -EFAULT;
769
770 return 0;
771}
772
773static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
774{
775 struct ethtool_eee edata;
776
777 if (!dev->ethtool_ops->set_eee)
778 return -EOPNOTSUPP;
779
780 if (copy_from_user(&edata, useraddr, sizeof(edata)))
781 return -EFAULT;
782
783 return dev->ethtool_ops->set_eee(dev, &edata);
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static int ethtool_nway_reset(struct net_device *dev)
787{
788 if (!dev->ethtool_ops->nway_reset)
789 return -EOPNOTSUPP;
790
791 return dev->ethtool_ops->nway_reset(dev);
792}
793
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000794static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
795{
796 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
797
798 if (!dev->ethtool_ops->get_link)
799 return -EOPNOTSUPP;
800
801 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
802
803 if (copy_to_user(useraddr, &edata, sizeof(edata)))
804 return -EFAULT;
805 return 0;
806}
807
Ben Hutchings081d0942012-04-12 00:42:12 +0000808static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
809 int (*getter)(struct net_device *,
810 struct ethtool_eeprom *, u8 *),
811 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700814 void __user *userbuf = useraddr + sizeof(eeprom);
815 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700817 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
820 return -EFAULT;
821
822 /* Check for wrap and zero */
823 if (eeprom.offset + eeprom.len <= eeprom.offset)
824 return -EINVAL;
825
826 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +0000827 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EINVAL;
829
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700830 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!data)
832 return -ENOMEM;
833
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700834 bytes_remaining = eeprom.len;
835 while (bytes_remaining > 0) {
836 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Ben Hutchings081d0942012-04-12 00:42:12 +0000838 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700839 if (ret)
840 break;
841 if (copy_to_user(userbuf, data, eeprom.len)) {
842 ret = -EFAULT;
843 break;
844 }
845 userbuf += eeprom.len;
846 eeprom.offset += eeprom.len;
847 bytes_remaining -= eeprom.len;
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700850 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
851 eeprom.offset -= eeprom.len;
852 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
853 ret = -EFAULT;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 kfree(data);
856 return ret;
857}
858
Ben Hutchings081d0942012-04-12 00:42:12 +0000859static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
860{
861 const struct ethtool_ops *ops = dev->ethtool_ops;
862
863 if (!ops->get_eeprom || !ops->get_eeprom_len)
864 return -EOPNOTSUPP;
865
866 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
867 ops->get_eeprom_len(dev));
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
871{
872 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700873 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700874 void __user *userbuf = useraddr + sizeof(eeprom);
875 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700877 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (!ops->set_eeprom || !ops->get_eeprom_len)
880 return -EOPNOTSUPP;
881
882 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
883 return -EFAULT;
884
885 /* Check for wrap and zero */
886 if (eeprom.offset + eeprom.len <= eeprom.offset)
887 return -EINVAL;
888
889 /* Check for exceeding total eeprom len */
890 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
891 return -EINVAL;
892
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700893 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!data)
895 return -ENOMEM;
896
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700897 bytes_remaining = eeprom.len;
898 while (bytes_remaining > 0) {
899 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700901 if (copy_from_user(data, userbuf, eeprom.len)) {
902 ret = -EFAULT;
903 break;
904 }
905 ret = ops->set_eeprom(dev, &eeprom, data);
906 if (ret)
907 break;
908 userbuf += eeprom.len;
909 eeprom.offset += eeprom.len;
910 bytes_remaining -= eeprom.len;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 kfree(data);
914 return ret;
915}
916
chavey97f8aef2010-04-07 21:54:42 -0700917static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
918 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Roland Dreier8e557422010-02-11 12:14:23 -0800920 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (!dev->ethtool_ops->get_coalesce)
923 return -EOPNOTSUPP;
924
925 dev->ethtool_ops->get_coalesce(dev, &coalesce);
926
927 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
928 return -EFAULT;
929 return 0;
930}
931
chavey97f8aef2010-04-07 21:54:42 -0700932static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
933 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct ethtool_coalesce coalesce;
936
David S. Millerfa04ae52005-06-06 15:07:19 -0700937 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EOPNOTSUPP;
939
940 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
941 return -EFAULT;
942
943 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
944}
945
946static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
947{
Roland Dreier8e557422010-02-11 12:14:23 -0800948 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (!dev->ethtool_ops->get_ringparam)
951 return -EOPNOTSUPP;
952
953 dev->ethtool_ops->get_ringparam(dev, &ringparam);
954
955 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
956 return -EFAULT;
957 return 0;
958}
959
960static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
961{
962 struct ethtool_ringparam ringparam;
963
964 if (!dev->ethtool_ops->set_ringparam)
965 return -EOPNOTSUPP;
966
967 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
968 return -EFAULT;
969
970 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
971}
972
amit salecha8b5933c2011-04-07 01:58:42 +0000973static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
974 void __user *useraddr)
975{
976 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
977
978 if (!dev->ethtool_ops->get_channels)
979 return -EOPNOTSUPP;
980
981 dev->ethtool_ops->get_channels(dev, &channels);
982
983 if (copy_to_user(useraddr, &channels, sizeof(channels)))
984 return -EFAULT;
985 return 0;
986}
987
988static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
989 void __user *useraddr)
990{
991 struct ethtool_channels channels;
992
993 if (!dev->ethtool_ops->set_channels)
994 return -EOPNOTSUPP;
995
996 if (copy_from_user(&channels, useraddr, sizeof(channels)))
997 return -EFAULT;
998
999 return dev->ethtool_ops->set_channels(dev, &channels);
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1003{
1004 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1005
1006 if (!dev->ethtool_ops->get_pauseparam)
1007 return -EOPNOTSUPP;
1008
1009 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1010
1011 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1012 return -EFAULT;
1013 return 0;
1014}
1015
1016static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1017{
1018 struct ethtool_pauseparam pauseparam;
1019
Jeff Garzike1b90c42006-07-17 12:54:40 -04001020 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EOPNOTSUPP;
1022
1023 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1024 return -EFAULT;
1025
1026 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1030{
1031 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001032 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001034 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001036 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001037 return -EOPNOTSUPP;
1038
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001039 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001040 if (test_len < 0)
1041 return test_len;
1042 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (copy_from_user(&test, useraddr, sizeof(test)))
1045 return -EFAULT;
1046
Jeff Garzikff03d492007-08-15 16:01:08 -07001047 test.len = test_len;
1048 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!data)
1050 return -ENOMEM;
1051
1052 ops->self_test(dev, &test, data);
1053
1054 ret = -EFAULT;
1055 if (copy_to_user(useraddr, &test, sizeof(test)))
1056 goto out;
1057 useraddr += sizeof(test);
1058 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1059 goto out;
1060 ret = 0;
1061
1062 out:
1063 kfree(data);
1064 return ret;
1065}
1066
1067static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1068{
1069 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 u8 *data;
1071 int ret;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1074 return -EFAULT;
1075
Michał Mirosław340ae162011-02-15 16:59:16 +00001076 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001077 if (ret < 0)
1078 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001079
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001080 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1083 if (!data)
1084 return -ENOMEM;
1085
Michał Mirosław340ae162011-02-15 16:59:16 +00001086 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 ret = -EFAULT;
1089 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1090 goto out;
1091 useraddr += sizeof(gstrings);
1092 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1093 goto out;
1094 ret = 0;
1095
Michał Mirosław340ae162011-02-15 16:59:16 +00001096out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 kfree(data);
1098 return ret;
1099}
1100
1101static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1102{
1103 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001104 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001105 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001106 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001108 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EOPNOTSUPP;
1110
Ben Hutchings68f512f2011-04-02 00:35:15 +01001111 if (busy)
1112 return -EBUSY;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (copy_from_user(&id, useraddr, sizeof(id)))
1115 return -EFAULT;
1116
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001117 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001118 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001119 return rc;
1120
1121 /* Drop the RTNL lock while waiting, but prevent reentry or
1122 * removal of the device.
1123 */
1124 busy = true;
1125 dev_hold(dev);
1126 rtnl_unlock();
1127
1128 if (rc == 0) {
1129 /* Driver will handle this itself */
1130 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001131 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001132 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001133 /* Driver expects to be called at twice the frequency in rc */
1134 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001135
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001136 /* Count down seconds */
1137 do {
1138 /* Count down iterations per second */
1139 i = n;
1140 do {
1141 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001142 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001143 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1144 rtnl_unlock();
1145 if (rc)
1146 break;
1147 schedule_timeout_interruptible(interval);
1148 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001149 } while (!signal_pending(current) &&
1150 (id.data == 0 || --id.data != 0));
1151 }
1152
1153 rtnl_lock();
1154 dev_put(dev);
1155 busy = false;
1156
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001157 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001158 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1162{
1163 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001164 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001166 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001168 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001169 return -EOPNOTSUPP;
1170
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001171 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001172 if (n_stats < 0)
1173 return n_stats;
1174 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1177 return -EFAULT;
1178
Jeff Garzikff03d492007-08-15 16:01:08 -07001179 stats.n_stats = n_stats;
1180 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!data)
1182 return -ENOMEM;
1183
1184 ops->get_ethtool_stats(dev, &stats, data);
1185
1186 ret = -EFAULT;
1187 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1188 goto out;
1189 useraddr += sizeof(stats);
1190 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1191 goto out;
1192 ret = 0;
1193
1194 out:
1195 kfree(data);
1196 return ret;
1197}
1198
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001199static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001200{
1201 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001202
Matthew Wilcox313674a2007-07-31 14:00:29 -07001203 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001204 return -EFAULT;
1205
Matthew Wilcox313674a2007-07-31 14:00:29 -07001206 if (epaddr.size < dev->addr_len)
1207 return -ETOOSMALL;
1208 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001209
Jon Wetzela6f9a702005-08-20 17:15:54 -07001210 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001211 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001212 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001213 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1214 return -EFAULT;
1215 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001216}
1217
Jeff Garzik13c99b22007-08-15 16:01:56 -07001218static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1219 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001220{
Roland Dreier8e557422010-02-11 12:14:23 -08001221 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001222
Jeff Garzik13c99b22007-08-15 16:01:56 -07001223 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001224 return -EOPNOTSUPP;
1225
Jeff Garzik13c99b22007-08-15 16:01:56 -07001226 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001227
1228 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1229 return -EFAULT;
1230 return 0;
1231}
1232
Jeff Garzik13c99b22007-08-15 16:01:56 -07001233static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1234 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001235{
1236 struct ethtool_value edata;
1237
Jeff Garzik13c99b22007-08-15 16:01:56 -07001238 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001239 return -EOPNOTSUPP;
1240
1241 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1242 return -EFAULT;
1243
Jeff Garzik13c99b22007-08-15 16:01:56 -07001244 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001245 return 0;
1246}
1247
Jeff Garzik13c99b22007-08-15 16:01:56 -07001248static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1249 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001250{
1251 struct ethtool_value edata;
1252
Jeff Garzik13c99b22007-08-15 16:01:56 -07001253 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001254 return -EOPNOTSUPP;
1255
1256 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1257 return -EFAULT;
1258
Jeff Garzik13c99b22007-08-15 16:01:56 -07001259 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001260}
1261
chavey97f8aef2010-04-07 21:54:42 -07001262static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1263 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001264{
1265 struct ethtool_flash efl;
1266
1267 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1268 return -EFAULT;
1269
1270 if (!dev->ethtool_ops->flash_device)
1271 return -EOPNOTSUPP;
1272
Ben Hutchings786f5282012-02-01 09:32:25 +00001273 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1274
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001275 return dev->ethtool_ops->flash_device(dev, &efl);
1276}
1277
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001278static int ethtool_set_dump(struct net_device *dev,
1279 void __user *useraddr)
1280{
1281 struct ethtool_dump dump;
1282
1283 if (!dev->ethtool_ops->set_dump)
1284 return -EOPNOTSUPP;
1285
1286 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1287 return -EFAULT;
1288
1289 return dev->ethtool_ops->set_dump(dev, &dump);
1290}
1291
1292static int ethtool_get_dump_flag(struct net_device *dev,
1293 void __user *useraddr)
1294{
1295 int ret;
1296 struct ethtool_dump dump;
1297 const struct ethtool_ops *ops = dev->ethtool_ops;
1298
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001299 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001300 return -EOPNOTSUPP;
1301
1302 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1303 return -EFAULT;
1304
1305 ret = ops->get_dump_flag(dev, &dump);
1306 if (ret)
1307 return ret;
1308
1309 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1310 return -EFAULT;
1311 return 0;
1312}
1313
1314static int ethtool_get_dump_data(struct net_device *dev,
1315 void __user *useraddr)
1316{
1317 int ret;
1318 __u32 len;
1319 struct ethtool_dump dump, tmp;
1320 const struct ethtool_ops *ops = dev->ethtool_ops;
1321 void *data = NULL;
1322
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001323 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001324 return -EOPNOTSUPP;
1325
1326 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1327 return -EFAULT;
1328
1329 memset(&tmp, 0, sizeof(tmp));
1330 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1331 ret = ops->get_dump_flag(dev, &tmp);
1332 if (ret)
1333 return ret;
1334
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001335 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001336 if (!len)
1337 return -EFAULT;
1338
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001339 /* Don't ever let the driver think there's more space available
1340 * than it requested with .get_dump_flag().
1341 */
1342 dump.len = len;
1343
1344 /* Always allocate enough space to hold the whole thing so that the
1345 * driver does not need to check the length and bother with partial
1346 * dumping.
1347 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001348 data = vzalloc(tmp.len);
1349 if (!data)
1350 return -ENOMEM;
1351 ret = ops->get_dump_data(dev, &dump, data);
1352 if (ret)
1353 goto out;
1354
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001355 /* There are two sane possibilities:
1356 * 1. The driver's .get_dump_data() does not touch dump.len.
1357 * 2. Or it may set dump.len to how much it really writes, which
1358 * should be tmp.len (or len if it can do a partial dump).
1359 * In any case respond to userspace with the actual length of data
1360 * it's receiving.
1361 */
1362 WARN_ON(dump.len != len && dump.len != tmp.len);
1363 dump.len = len;
1364
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001365 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1366 ret = -EFAULT;
1367 goto out;
1368 }
1369 useraddr += offsetof(struct ethtool_dump, data);
1370 if (copy_to_user(useraddr, data, len))
1371 ret = -EFAULT;
1372out:
1373 vfree(data);
1374 return ret;
1375}
1376
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001377static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1378{
1379 int err = 0;
1380 struct ethtool_ts_info info;
1381 const struct ethtool_ops *ops = dev->ethtool_ops;
1382 struct phy_device *phydev = dev->phydev;
1383
1384 memset(&info, 0, sizeof(info));
1385 info.cmd = ETHTOOL_GET_TS_INFO;
1386
1387 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001388 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001389 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001390 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001391 } else {
1392 info.so_timestamping =
1393 SOF_TIMESTAMPING_RX_SOFTWARE |
1394 SOF_TIMESTAMPING_SOFTWARE;
1395 info.phc_index = -1;
1396 }
1397
1398 if (err)
1399 return err;
1400
1401 if (copy_to_user(useraddr, &info, sizeof(info)))
1402 err = -EFAULT;
1403
1404 return err;
1405}
1406
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001407static int ethtool_get_module_info(struct net_device *dev,
1408 void __user *useraddr)
1409{
1410 int ret;
1411 struct ethtool_modinfo modinfo;
1412 const struct ethtool_ops *ops = dev->ethtool_ops;
1413
1414 if (!ops->get_module_info)
1415 return -EOPNOTSUPP;
1416
1417 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1418 return -EFAULT;
1419
1420 ret = ops->get_module_info(dev, &modinfo);
1421 if (ret)
1422 return ret;
1423
1424 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1425 return -EFAULT;
1426
1427 return 0;
1428}
1429
1430static int ethtool_get_module_eeprom(struct net_device *dev,
1431 void __user *useraddr)
1432{
1433 int ret;
1434 struct ethtool_modinfo modinfo;
1435 const struct ethtool_ops *ops = dev->ethtool_ops;
1436
1437 if (!ops->get_module_info || !ops->get_module_eeprom)
1438 return -EOPNOTSUPP;
1439
1440 ret = ops->get_module_info(dev, &modinfo);
1441 if (ret)
1442 return ret;
1443
1444 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1445 modinfo.eeprom_len);
1446}
1447
Yan Burman600fed52013-06-03 02:03:34 +00001448/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Eric W. Biederman881d9662007-09-17 11:56:21 -07001450int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001452 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 void __user *useraddr = ifr->ifr_data;
1454 u32 ethcmd;
1455 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00001456 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (!dev || !netif_device_present(dev))
1459 return -ENODEV;
1460
chavey97f8aef2010-04-07 21:54:42 -07001461 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EFAULT;
1463
Stephen Hemminger75f31232006-09-28 15:13:37 -07001464 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001465 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001466 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001467 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001468 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001469 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001470 case ETHTOOL_GCOALESCE:
1471 case ETHTOOL_GRINGPARAM:
1472 case ETHTOOL_GPAUSEPARAM:
1473 case ETHTOOL_GRXCSUM:
1474 case ETHTOOL_GTXCSUM:
1475 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001476 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001477 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001478 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001479 case ETHTOOL_GTSO:
1480 case ETHTOOL_GPERMADDR:
1481 case ETHTOOL_GUFO:
1482 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001483 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001484 case ETHTOOL_GFLAGS:
1485 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001486 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001487 case ETHTOOL_GRXRINGS:
1488 case ETHTOOL_GRXCLSRLCNT:
1489 case ETHTOOL_GRXCLSRULE:
1490 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001491 case ETHTOOL_GRXFHINDIR:
Michał Mirosław5455c692011-02-15 16:59:17 +00001492 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001493 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001494 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001495 case ETHTOOL_GEEE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001496 break;
1497 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001498 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001499 return -EPERM;
1500 }
1501
chavey97f8aef2010-04-07 21:54:42 -07001502 if (dev->ethtool_ops->begin) {
1503 rc = dev->ethtool_ops->begin(dev);
1504 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001506 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001507 old_features = dev->features;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 switch (ethcmd) {
1510 case ETHTOOL_GSET:
1511 rc = ethtool_get_settings(dev, useraddr);
1512 break;
1513 case ETHTOOL_SSET:
1514 rc = ethtool_set_settings(dev, useraddr);
1515 break;
1516 case ETHTOOL_GDRVINFO:
1517 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 break;
1519 case ETHTOOL_GREGS:
1520 rc = ethtool_get_regs(dev, useraddr);
1521 break;
1522 case ETHTOOL_GWOL:
1523 rc = ethtool_get_wol(dev, useraddr);
1524 break;
1525 case ETHTOOL_SWOL:
1526 rc = ethtool_set_wol(dev, useraddr);
1527 break;
1528 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001529 rc = ethtool_get_value(dev, useraddr, ethcmd,
1530 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 break;
1532 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001533 rc = ethtool_set_value_void(dev, useraddr,
1534 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001536 case ETHTOOL_GEEE:
1537 rc = ethtool_get_eee(dev, useraddr);
1538 break;
1539 case ETHTOOL_SEEE:
1540 rc = ethtool_set_eee(dev, useraddr);
1541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 case ETHTOOL_NWAY_RST:
1543 rc = ethtool_nway_reset(dev);
1544 break;
1545 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001546 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 break;
1548 case ETHTOOL_GEEPROM:
1549 rc = ethtool_get_eeprom(dev, useraddr);
1550 break;
1551 case ETHTOOL_SEEPROM:
1552 rc = ethtool_set_eeprom(dev, useraddr);
1553 break;
1554 case ETHTOOL_GCOALESCE:
1555 rc = ethtool_get_coalesce(dev, useraddr);
1556 break;
1557 case ETHTOOL_SCOALESCE:
1558 rc = ethtool_set_coalesce(dev, useraddr);
1559 break;
1560 case ETHTOOL_GRINGPARAM:
1561 rc = ethtool_get_ringparam(dev, useraddr);
1562 break;
1563 case ETHTOOL_SRINGPARAM:
1564 rc = ethtool_set_ringparam(dev, useraddr);
1565 break;
1566 case ETHTOOL_GPAUSEPARAM:
1567 rc = ethtool_get_pauseparam(dev, useraddr);
1568 break;
1569 case ETHTOOL_SPAUSEPARAM:
1570 rc = ethtool_set_pauseparam(dev, useraddr);
1571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 case ETHTOOL_TEST:
1573 rc = ethtool_self_test(dev, useraddr);
1574 break;
1575 case ETHTOOL_GSTRINGS:
1576 rc = ethtool_get_strings(dev, useraddr);
1577 break;
1578 case ETHTOOL_PHYS_ID:
1579 rc = ethtool_phys_id(dev, useraddr);
1580 break;
1581 case ETHTOOL_GSTATS:
1582 rc = ethtool_get_stats(dev, useraddr);
1583 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001584 case ETHTOOL_GPERMADDR:
1585 rc = ethtool_get_perm_addr(dev, useraddr);
1586 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001587 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001588 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001589 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001590 break;
1591 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001592 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001593 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001594 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001595 rc = ethtool_get_value(dev, useraddr, ethcmd,
1596 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001597 break;
1598 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001599 rc = ethtool_set_value(dev, useraddr,
1600 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001601 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001602 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001603 case ETHTOOL_GRXRINGS:
1604 case ETHTOOL_GRXCLSRLCNT:
1605 case ETHTOOL_GRXCLSRULE:
1606 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001607 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001608 break;
1609 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001610 case ETHTOOL_SRXCLSRLDEL:
1611 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001612 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001613 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001614 case ETHTOOL_FLASHDEV:
1615 rc = ethtool_flash_device(dev, useraddr);
1616 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001617 case ETHTOOL_RESET:
1618 rc = ethtool_reset(dev, useraddr);
1619 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001620 case ETHTOOL_GSSET_INFO:
1621 rc = ethtool_get_sset_info(dev, useraddr);
1622 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001623 case ETHTOOL_GRXFHINDIR:
1624 rc = ethtool_get_rxfh_indir(dev, useraddr);
1625 break;
1626 case ETHTOOL_SRXFHINDIR:
1627 rc = ethtool_set_rxfh_indir(dev, useraddr);
1628 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001629 case ETHTOOL_GFEATURES:
1630 rc = ethtool_get_features(dev, useraddr);
1631 break;
1632 case ETHTOOL_SFEATURES:
1633 rc = ethtool_set_features(dev, useraddr);
1634 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001635 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001636 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001637 case ETHTOOL_GSG:
1638 case ETHTOOL_GTSO:
1639 case ETHTOOL_GUFO:
1640 case ETHTOOL_GGSO:
1641 case ETHTOOL_GGRO:
1642 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1643 break;
1644 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001645 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001646 case ETHTOOL_SSG:
1647 case ETHTOOL_STSO:
1648 case ETHTOOL_SUFO:
1649 case ETHTOOL_SGSO:
1650 case ETHTOOL_SGRO:
1651 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1652 break;
amit salecha8b5933c2011-04-07 01:58:42 +00001653 case ETHTOOL_GCHANNELS:
1654 rc = ethtool_get_channels(dev, useraddr);
1655 break;
1656 case ETHTOOL_SCHANNELS:
1657 rc = ethtool_set_channels(dev, useraddr);
1658 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001659 case ETHTOOL_SET_DUMP:
1660 rc = ethtool_set_dump(dev, useraddr);
1661 break;
1662 case ETHTOOL_GET_DUMP_FLAG:
1663 rc = ethtool_get_dump_flag(dev, useraddr);
1664 break;
1665 case ETHTOOL_GET_DUMP_DATA:
1666 rc = ethtool_get_dump_data(dev, useraddr);
1667 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001668 case ETHTOOL_GET_TS_INFO:
1669 rc = ethtool_get_ts_info(dev, useraddr);
1670 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001671 case ETHTOOL_GMODULEINFO:
1672 rc = ethtool_get_module_info(dev, useraddr);
1673 break;
1674 case ETHTOOL_GMODULEEEPROM:
1675 rc = ethtool_get_module_eeprom(dev, useraddr);
1676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001678 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001680
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001681 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001683
1684 if (old_features != dev->features)
1685 netdev_features_change(dev);
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}