blob: 8cab7744790ed1f0f20e584e13ecb7b90a8df79f [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",
Pravin B Shelar73136262013-03-07 13:21:51 +000085 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Simon Horman0d89d202013-05-23 21:02:52 +000086 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000087
88 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
89 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
90 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
91 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
92 [NETIF_F_RXHASH_BIT] = "rx-hashing",
93 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
94 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
95 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000096 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000097 [NETIF_F_RXALL_BIT] = "rx-all",
Michał Mirosław9d921542011-11-15 15:29:55 +000098};
99
Michał Mirosław5455c692011-02-15 16:59:17 +0000100static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
101{
102 struct ethtool_gfeatures cmd = {
103 .cmd = ETHTOOL_GFEATURES,
104 .size = ETHTOOL_DEV_FEATURE_WORDS,
105 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000106 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000107 u32 __user *sizeaddr;
108 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000109 int i;
110
111 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000112 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000113
114 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000115 features[i].available = (u32)(dev->hw_features >> (32 * i));
116 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
117 features[i].active = (u32)(dev->features >> (32 * i));
118 features[i].never_changed =
119 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000120 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000121
122 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
123 if (get_user(copy_size, sizeaddr))
124 return -EFAULT;
125
126 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
127 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
128
129 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
130 return -EFAULT;
131 useraddr += sizeof(cmd);
132 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
133 return -EFAULT;
134
135 return 0;
136}
137
138static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
139{
140 struct ethtool_sfeatures cmd;
141 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000142 netdev_features_t wanted = 0, valid = 0;
143 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000144
145 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
146 return -EFAULT;
147 useraddr += sizeof(cmd);
148
149 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
150 return -EINVAL;
151
152 if (copy_from_user(features, useraddr, sizeof(features)))
153 return -EFAULT;
154
Michał Mirosław475414f2011-11-15 15:29:55 +0000155 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000156 valid |= (netdev_features_t)features[i].valid << (32 * i);
157 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000158 }
159
160 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000161 return -EINVAL;
162
Michał Mirosław475414f2011-11-15 15:29:55 +0000163 if (valid & ~dev->hw_features) {
164 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000165 ret |= ETHTOOL_F_UNSUPPORTED;
166 }
167
Michał Mirosław475414f2011-11-15 15:29:55 +0000168 dev->wanted_features &= ~valid;
169 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700170 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000171
Michał Mirosław475414f2011-11-15 15:29:55 +0000172 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000173 ret |= ETHTOOL_F_WISH;
174
175 return ret;
176}
177
Michał Mirosław340ae162011-02-15 16:59:16 +0000178static int __ethtool_get_sset_count(struct net_device *dev, int sset)
179{
180 const struct ethtool_ops *ops = dev->ethtool_ops;
181
Michał Mirosław5455c692011-02-15 16:59:17 +0000182 if (sset == ETH_SS_FEATURES)
183 return ARRAY_SIZE(netdev_features_strings);
184
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000185 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000186 return ops->get_sset_count(dev, sset);
187 else
188 return -EOPNOTSUPP;
189}
190
191static void __ethtool_get_strings(struct net_device *dev,
192 u32 stringset, u8 *data)
193{
194 const struct ethtool_ops *ops = dev->ethtool_ops;
195
Michał Mirosław5455c692011-02-15 16:59:17 +0000196 if (stringset == ETH_SS_FEATURES)
197 memcpy(data, netdev_features_strings,
198 sizeof(netdev_features_strings));
199 else
200 /* ops->get_strings is valid because checked earlier */
201 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000202}
203
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000204static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000205{
206 /* feature masks of legacy discrete ethtool ops */
207
208 switch (eth_cmd) {
209 case ETHTOOL_GTXCSUM:
210 case ETHTOOL_STXCSUM:
211 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000212 case ETHTOOL_GRXCSUM:
213 case ETHTOOL_SRXCSUM:
214 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000215 case ETHTOOL_GSG:
216 case ETHTOOL_SSG:
217 return NETIF_F_SG;
218 case ETHTOOL_GTSO:
219 case ETHTOOL_STSO:
220 return NETIF_F_ALL_TSO;
221 case ETHTOOL_GUFO:
222 case ETHTOOL_SUFO:
223 return NETIF_F_UFO;
224 case ETHTOOL_GGSO:
225 case ETHTOOL_SGSO:
226 return NETIF_F_GSO;
227 case ETHTOOL_GGRO:
228 case ETHTOOL_SGRO:
229 return NETIF_F_GRO;
230 default:
231 BUG();
232 }
233}
234
Michał Mirosław0a417702011-02-15 16:59:17 +0000235static int ethtool_get_one_feature(struct net_device *dev,
236 char __user *useraddr, u32 ethcmd)
237{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000238 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000239 struct ethtool_value edata = {
240 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000241 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000242 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000243
Michał Mirosław0a417702011-02-15 16:59:17 +0000244 if (copy_to_user(useraddr, &edata, sizeof(edata)))
245 return -EFAULT;
246 return 0;
247}
248
Michał Mirosław0a417702011-02-15 16:59:17 +0000249static int ethtool_set_one_feature(struct net_device *dev,
250 void __user *useraddr, u32 ethcmd)
251{
252 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000253 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000254
255 if (copy_from_user(&edata, useraddr, sizeof(edata)))
256 return -EFAULT;
257
Michał Mirosław86794882011-02-15 16:59:17 +0000258 mask = ethtool_get_feature_mask(ethcmd);
259 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000260 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000261 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000262
263 if (edata.data)
264 dev->wanted_features |= mask;
265 else
266 dev->wanted_features &= ~mask;
267
268 __netdev_update_features(dev);
269
270 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000271}
272
Michał Mirosław02b3a552011-11-15 15:29:55 +0000273#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
274 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000275#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
276 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
277 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000278
279static u32 __ethtool_get_flags(struct net_device *dev)
280{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000281 u32 flags = 0;
282
Dragos Foianu8a731252013-07-13 14:43:00 +0100283 if (dev->features & NETIF_F_LRO)
284 flags |= ETH_FLAG_LRO;
285 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
286 flags |= ETH_FLAG_RXVLAN;
287 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
288 flags |= ETH_FLAG_TXVLAN;
289 if (dev->features & NETIF_F_NTUPLE)
290 flags |= ETH_FLAG_NTUPLE;
291 if (dev->features & NETIF_F_RXHASH)
292 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000293
294 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000295}
296
297static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000298{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000299 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000300
Michał Mirosław02b3a552011-11-15 15:29:55 +0000301 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000302 return -EINVAL;
303
Dragos Foianu8a731252013-07-13 14:43:00 +0100304 if (data & ETH_FLAG_LRO)
305 features |= NETIF_F_LRO;
306 if (data & ETH_FLAG_RXVLAN)
307 features |= NETIF_F_HW_VLAN_CTAG_RX;
308 if (data & ETH_FLAG_TXVLAN)
309 features |= NETIF_F_HW_VLAN_CTAG_TX;
310 if (data & ETH_FLAG_NTUPLE)
311 features |= NETIF_F_NTUPLE;
312 if (data & ETH_FLAG_RXHASH)
313 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000314
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000315 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000316 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000317 if (changed & ~dev->hw_features)
318 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
319
320 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000321 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000322
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700323 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000324
325 return 0;
326}
327
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000328int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000330 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000332 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EOPNOTSUPP;
334
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000335 memset(cmd, 0, sizeof(struct ethtool_cmd));
336 cmd->cmd = ETHTOOL_GSET;
337 return dev->ethtool_ops->get_settings(dev, cmd);
338}
339EXPORT_SYMBOL(__ethtool_get_settings);
340
341static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
342{
343 int err;
344 struct ethtool_cmd cmd;
345
346 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (err < 0)
348 return err;
349
350 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
351 return -EFAULT;
352 return 0;
353}
354
355static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
356{
357 struct ethtool_cmd cmd;
358
359 if (!dev->ethtool_ops->set_settings)
360 return -EOPNOTSUPP;
361
362 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
363 return -EFAULT;
364
365 return dev->ethtool_ops->set_settings(dev, &cmd);
366}
367
chavey97f8aef2010-04-07 21:54:42 -0700368static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
369 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700372 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 memset(&info, 0, sizeof(info));
375 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000376 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700377 ops->get_drvinfo(dev, &info);
378 } else if (dev->dev.parent && dev->dev.parent->driver) {
379 strlcpy(info.bus_info, dev_name(dev->dev.parent),
380 sizeof(info.bus_info));
381 strlcpy(info.driver, dev->dev.parent->driver->name,
382 sizeof(info.driver));
383 } else {
384 return -EOPNOTSUPP;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jeff Garzik723b2f52010-03-03 22:51:50 +0000387 /*
388 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000389 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000390 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000391 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700392 int rc;
393
394 rc = ops->get_sset_count(dev, ETH_SS_TEST);
395 if (rc >= 0)
396 info.testinfo_len = rc;
397 rc = ops->get_sset_count(dev, ETH_SS_STATS);
398 if (rc >= 0)
399 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700400 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
401 if (rc >= 0)
402 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700403 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000404 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000406 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 info.eedump_len = ops->get_eeprom_len(dev);
408
409 if (copy_to_user(useraddr, &info, sizeof(info)))
410 return -EFAULT;
411 return 0;
412}
413
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800414static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700415 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000416{
417 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000418 u64 sset_mask;
419 int i, idx = 0, n_bits = 0, ret, rc;
420 u32 *info_buf = NULL;
421
Jeff Garzik723b2f52010-03-03 22:51:50 +0000422 if (copy_from_user(&info, useraddr, sizeof(info)))
423 return -EFAULT;
424
425 /* store copy of mask, because we zero struct later on */
426 sset_mask = info.sset_mask;
427 if (!sset_mask)
428 return 0;
429
430 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000431 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000432
433 memset(&info, 0, sizeof(info));
434 info.cmd = ETHTOOL_GSSET_INFO;
435
436 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
437 if (!info_buf)
438 return -ENOMEM;
439
440 /*
441 * fill return buffer based on input bitmask and successful
442 * get_sset_count return
443 */
444 for (i = 0; i < 64; i++) {
445 if (!(sset_mask & (1ULL << i)))
446 continue;
447
Michał Mirosław340ae162011-02-15 16:59:16 +0000448 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000449 if (rc >= 0) {
450 info.sset_mask |= (1ULL << i);
451 info_buf[idx++] = rc;
452 }
453 }
454
455 ret = -EFAULT;
456 if (copy_to_user(useraddr, &info, sizeof(info)))
457 goto out;
458
459 useraddr += offsetof(struct ethtool_sset_info, data);
460 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
461 goto out;
462
463 ret = 0;
464
465out:
466 kfree(info_buf);
467 return ret;
468}
469
chavey97f8aef2010-04-07 21:54:42 -0700470static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000471 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700472{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000473 struct ethtool_rxnfc info;
474 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000475 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700476
Santwona Behera59089d82009-02-20 00:58:13 -0800477 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700478 return -EOPNOTSUPP;
479
Ben Hutchingsbf988432010-06-28 08:45:58 +0000480 /* struct ethtool_rxnfc was originally defined for
481 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
482 * members. User-space might still be using that
483 * definition. */
484 if (cmd == ETHTOOL_SRXFH)
485 info_size = (offsetof(struct ethtool_rxnfc, data) +
486 sizeof(info.data));
487
488 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700489 return -EFAULT;
490
Ben Hutchings55664f32012-01-03 12:04:51 +0000491 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
492 if (rc)
493 return rc;
494
495 if (cmd == ETHTOOL_SRXCLSRLINS &&
496 copy_to_user(useraddr, &info, info_size))
497 return -EFAULT;
498
499 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700500}
501
chavey97f8aef2010-04-07 21:54:42 -0700502static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000503 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700504{
505 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000506 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800507 const struct ethtool_ops *ops = dev->ethtool_ops;
508 int ret;
509 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700510
Santwona Behera59089d82009-02-20 00:58:13 -0800511 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700512 return -EOPNOTSUPP;
513
Ben Hutchingsbf988432010-06-28 08:45:58 +0000514 /* struct ethtool_rxnfc was originally defined for
515 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
516 * members. User-space might still be using that
517 * definition. */
518 if (cmd == ETHTOOL_GRXFH)
519 info_size = (offsetof(struct ethtool_rxnfc, data) +
520 sizeof(info.data));
521
522 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700523 return -EFAULT;
524
Santwona Behera59089d82009-02-20 00:58:13 -0800525 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
526 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000527 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000528 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000529 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800530 if (!rule_buf)
531 return -ENOMEM;
532 }
533 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700534
Santwona Behera59089d82009-02-20 00:58:13 -0800535 ret = ops->get_rxnfc(dev, &info, rule_buf);
536 if (ret < 0)
537 goto err_out;
538
539 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000540 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800541 goto err_out;
542
543 if (rule_buf) {
544 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
545 if (copy_to_user(useraddr, rule_buf,
546 info.rule_cnt * sizeof(u32)))
547 goto err_out;
548 }
549 ret = 0;
550
551err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700552 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800553
554 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700555}
556
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000557static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
558 void __user *useraddr)
559{
Ben Hutchings7850f632011-12-15 13:55:01 +0000560 u32 user_size, dev_size;
561 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000562 int ret;
563
Ben Hutchings7850f632011-12-15 13:55:01 +0000564 if (!dev->ethtool_ops->get_rxfh_indir_size ||
565 !dev->ethtool_ops->get_rxfh_indir)
566 return -EOPNOTSUPP;
567 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
568 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000569 return -EOPNOTSUPP;
570
Ben Hutchings7850f632011-12-15 13:55:01 +0000571 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000572 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000573 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000574 return -EFAULT;
575
Ben Hutchings7850f632011-12-15 13:55:01 +0000576 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
577 &dev_size, sizeof(dev_size)))
578 return -EFAULT;
579
580 /* If the user buffer size is 0, this is just a query for the
581 * device table size. Otherwise, if it's smaller than the
582 * device table size it's an error.
583 */
584 if (user_size < dev_size)
585 return user_size == 0 ? 0 : -EINVAL;
586
587 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000588 if (!indir)
589 return -ENOMEM;
590
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000591 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
592 if (ret)
593 goto out;
594
Ben Hutchings7850f632011-12-15 13:55:01 +0000595 if (copy_to_user(useraddr +
596 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
597 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000598 ret = -EFAULT;
599
600out:
601 kfree(indir);
602 return ret;
603}
604
605static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
606 void __user *useraddr)
607{
Ben Hutchings7850f632011-12-15 13:55:01 +0000608 struct ethtool_rxnfc rx_rings;
609 u32 user_size, dev_size, i;
610 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000611 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000612 int ret;
613
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000614 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
615 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000616 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000617
618 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000619 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000620 return -EOPNOTSUPP;
621
Ben Hutchings7850f632011-12-15 13:55:01 +0000622 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000623 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000624 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000625 return -EFAULT;
626
Ben Hutchings278bc422011-12-15 13:56:49 +0000627 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000628 return -EINVAL;
629
630 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000631 if (!indir)
632 return -ENOMEM;
633
Ben Hutchings7850f632011-12-15 13:55:01 +0000634 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000635 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000636 if (ret)
637 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000638
639 if (user_size == 0) {
640 for (i = 0; i < dev_size; i++)
641 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
642 } else {
643 if (copy_from_user(indir,
644 useraddr +
645 offsetof(struct ethtool_rxfh_indir,
646 ring_index[0]),
647 dev_size * sizeof(indir[0]))) {
648 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000649 goto out;
650 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000651
652 /* Validate ring indices */
653 for (i = 0; i < dev_size; i++) {
654 if (indir[i] >= rx_rings.data) {
655 ret = -EINVAL;
656 goto out;
657 }
658 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000659 }
660
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000661 ret = ops->set_rxfh_indir(dev, indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000662
663out:
664 kfree(indir);
665 return ret;
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
669{
670 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700671 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 void *regbuf;
673 int reglen, ret;
674
675 if (!ops->get_regs || !ops->get_regs_len)
676 return -EOPNOTSUPP;
677
678 if (copy_from_user(&regs, useraddr, sizeof(regs)))
679 return -EFAULT;
680
681 reglen = ops->get_regs_len(dev);
682 if (regs.len > reglen)
683 regs.len = reglen;
684
Eugene Teob7c7d012011-01-24 21:05:17 -0800685 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700686 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -ENOMEM;
688
689 ops->get_regs(dev, &regs, regbuf);
690
691 ret = -EFAULT;
692 if (copy_to_user(useraddr, &regs, sizeof(regs)))
693 goto out;
694 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700695 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto out;
697 ret = 0;
698
699 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000700 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return ret;
702}
703
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000704static int ethtool_reset(struct net_device *dev, char __user *useraddr)
705{
706 struct ethtool_value reset;
707 int ret;
708
709 if (!dev->ethtool_ops->reset)
710 return -EOPNOTSUPP;
711
712 if (copy_from_user(&reset, useraddr, sizeof(reset)))
713 return -EFAULT;
714
715 ret = dev->ethtool_ops->reset(dev, &reset.data);
716 if (ret)
717 return ret;
718
719 if (copy_to_user(useraddr, &reset, sizeof(reset)))
720 return -EFAULT;
721 return 0;
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
725{
Roland Dreier8e557422010-02-11 12:14:23 -0800726 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (!dev->ethtool_ops->get_wol)
729 return -EOPNOTSUPP;
730
731 dev->ethtool_ops->get_wol(dev, &wol);
732
733 if (copy_to_user(useraddr, &wol, sizeof(wol)))
734 return -EFAULT;
735 return 0;
736}
737
738static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
739{
740 struct ethtool_wolinfo wol;
741
742 if (!dev->ethtool_ops->set_wol)
743 return -EOPNOTSUPP;
744
745 if (copy_from_user(&wol, useraddr, sizeof(wol)))
746 return -EFAULT;
747
748 return dev->ethtool_ops->set_wol(dev, &wol);
749}
750
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000751static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
752{
753 struct ethtool_eee edata;
754 int rc;
755
756 if (!dev->ethtool_ops->get_eee)
757 return -EOPNOTSUPP;
758
759 memset(&edata, 0, sizeof(struct ethtool_eee));
760 edata.cmd = ETHTOOL_GEEE;
761 rc = dev->ethtool_ops->get_eee(dev, &edata);
762
763 if (rc)
764 return rc;
765
766 if (copy_to_user(useraddr, &edata, sizeof(edata)))
767 return -EFAULT;
768
769 return 0;
770}
771
772static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
773{
774 struct ethtool_eee edata;
775
776 if (!dev->ethtool_ops->set_eee)
777 return -EOPNOTSUPP;
778
779 if (copy_from_user(&edata, useraddr, sizeof(edata)))
780 return -EFAULT;
781
782 return dev->ethtool_ops->set_eee(dev, &edata);
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static int ethtool_nway_reset(struct net_device *dev)
786{
787 if (!dev->ethtool_ops->nway_reset)
788 return -EOPNOTSUPP;
789
790 return dev->ethtool_ops->nway_reset(dev);
791}
792
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000793static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
794{
795 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
796
797 if (!dev->ethtool_ops->get_link)
798 return -EOPNOTSUPP;
799
800 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
801
802 if (copy_to_user(useraddr, &edata, sizeof(edata)))
803 return -EFAULT;
804 return 0;
805}
806
Ben Hutchings081d0942012-04-12 00:42:12 +0000807static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
808 int (*getter)(struct net_device *,
809 struct ethtool_eeprom *, u8 *),
810 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700813 void __user *userbuf = useraddr + sizeof(eeprom);
814 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700816 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
819 return -EFAULT;
820
821 /* Check for wrap and zero */
822 if (eeprom.offset + eeprom.len <= eeprom.offset)
823 return -EINVAL;
824
825 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +0000826 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -EINVAL;
828
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700829 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!data)
831 return -ENOMEM;
832
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700833 bytes_remaining = eeprom.len;
834 while (bytes_remaining > 0) {
835 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Ben Hutchings081d0942012-04-12 00:42:12 +0000837 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700838 if (ret)
839 break;
840 if (copy_to_user(userbuf, data, eeprom.len)) {
841 ret = -EFAULT;
842 break;
843 }
844 userbuf += eeprom.len;
845 eeprom.offset += eeprom.len;
846 bytes_remaining -= eeprom.len;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700849 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
850 eeprom.offset -= eeprom.len;
851 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
852 ret = -EFAULT;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 kfree(data);
855 return ret;
856}
857
Ben Hutchings081d0942012-04-12 00:42:12 +0000858static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
859{
860 const struct ethtool_ops *ops = dev->ethtool_ops;
861
862 if (!ops->get_eeprom || !ops->get_eeprom_len)
863 return -EOPNOTSUPP;
864
865 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
866 ops->get_eeprom_len(dev));
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
870{
871 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700872 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700873 void __user *userbuf = useraddr + sizeof(eeprom);
874 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700876 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (!ops->set_eeprom || !ops->get_eeprom_len)
879 return -EOPNOTSUPP;
880
881 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
882 return -EFAULT;
883
884 /* Check for wrap and zero */
885 if (eeprom.offset + eeprom.len <= eeprom.offset)
886 return -EINVAL;
887
888 /* Check for exceeding total eeprom len */
889 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
890 return -EINVAL;
891
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700892 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!data)
894 return -ENOMEM;
895
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700896 bytes_remaining = eeprom.len;
897 while (bytes_remaining > 0) {
898 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700900 if (copy_from_user(data, userbuf, eeprom.len)) {
901 ret = -EFAULT;
902 break;
903 }
904 ret = ops->set_eeprom(dev, &eeprom, data);
905 if (ret)
906 break;
907 userbuf += eeprom.len;
908 eeprom.offset += eeprom.len;
909 bytes_remaining -= eeprom.len;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 kfree(data);
913 return ret;
914}
915
chavey97f8aef2010-04-07 21:54:42 -0700916static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
917 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Roland Dreier8e557422010-02-11 12:14:23 -0800919 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!dev->ethtool_ops->get_coalesce)
922 return -EOPNOTSUPP;
923
924 dev->ethtool_ops->get_coalesce(dev, &coalesce);
925
926 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
927 return -EFAULT;
928 return 0;
929}
930
chavey97f8aef2010-04-07 21:54:42 -0700931static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
932 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct ethtool_coalesce coalesce;
935
David S. Millerfa04ae52005-06-06 15:07:19 -0700936 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -EOPNOTSUPP;
938
939 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
940 return -EFAULT;
941
942 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
943}
944
945static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
946{
Roland Dreier8e557422010-02-11 12:14:23 -0800947 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (!dev->ethtool_ops->get_ringparam)
950 return -EOPNOTSUPP;
951
952 dev->ethtool_ops->get_ringparam(dev, &ringparam);
953
954 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
955 return -EFAULT;
956 return 0;
957}
958
959static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
960{
961 struct ethtool_ringparam ringparam;
962
963 if (!dev->ethtool_ops->set_ringparam)
964 return -EOPNOTSUPP;
965
966 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
967 return -EFAULT;
968
969 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
970}
971
amit salecha8b5933c2011-04-07 01:58:42 +0000972static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
973 void __user *useraddr)
974{
975 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
976
977 if (!dev->ethtool_ops->get_channels)
978 return -EOPNOTSUPP;
979
980 dev->ethtool_ops->get_channels(dev, &channels);
981
982 if (copy_to_user(useraddr, &channels, sizeof(channels)))
983 return -EFAULT;
984 return 0;
985}
986
987static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
988 void __user *useraddr)
989{
990 struct ethtool_channels channels;
991
992 if (!dev->ethtool_ops->set_channels)
993 return -EOPNOTSUPP;
994
995 if (copy_from_user(&channels, useraddr, sizeof(channels)))
996 return -EFAULT;
997
998 return dev->ethtool_ops->set_channels(dev, &channels);
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1002{
1003 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1004
1005 if (!dev->ethtool_ops->get_pauseparam)
1006 return -EOPNOTSUPP;
1007
1008 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1009
1010 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1011 return -EFAULT;
1012 return 0;
1013}
1014
1015static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1016{
1017 struct ethtool_pauseparam pauseparam;
1018
Jeff Garzike1b90c42006-07-17 12:54:40 -04001019 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EOPNOTSUPP;
1021
1022 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1023 return -EFAULT;
1024
1025 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1029{
1030 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001031 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001033 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001035 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001036 return -EOPNOTSUPP;
1037
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001038 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001039 if (test_len < 0)
1040 return test_len;
1041 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (copy_from_user(&test, useraddr, sizeof(test)))
1044 return -EFAULT;
1045
Jeff Garzikff03d492007-08-15 16:01:08 -07001046 test.len = test_len;
1047 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (!data)
1049 return -ENOMEM;
1050
1051 ops->self_test(dev, &test, data);
1052
1053 ret = -EFAULT;
1054 if (copy_to_user(useraddr, &test, sizeof(test)))
1055 goto out;
1056 useraddr += sizeof(test);
1057 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1058 goto out;
1059 ret = 0;
1060
1061 out:
1062 kfree(data);
1063 return ret;
1064}
1065
1066static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1067{
1068 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 u8 *data;
1070 int ret;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1073 return -EFAULT;
1074
Michał Mirosław340ae162011-02-15 16:59:16 +00001075 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001076 if (ret < 0)
1077 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001078
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001079 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1082 if (!data)
1083 return -ENOMEM;
1084
Michał Mirosław340ae162011-02-15 16:59:16 +00001085 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 ret = -EFAULT;
1088 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1089 goto out;
1090 useraddr += sizeof(gstrings);
1091 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1092 goto out;
1093 ret = 0;
1094
Michał Mirosław340ae162011-02-15 16:59:16 +00001095out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(data);
1097 return ret;
1098}
1099
1100static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1101{
1102 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001103 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001104 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001105 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001107 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -EOPNOTSUPP;
1109
Ben Hutchings68f512f2011-04-02 00:35:15 +01001110 if (busy)
1111 return -EBUSY;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (copy_from_user(&id, useraddr, sizeof(id)))
1114 return -EFAULT;
1115
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001116 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001117 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001118 return rc;
1119
1120 /* Drop the RTNL lock while waiting, but prevent reentry or
1121 * removal of the device.
1122 */
1123 busy = true;
1124 dev_hold(dev);
1125 rtnl_unlock();
1126
1127 if (rc == 0) {
1128 /* Driver will handle this itself */
1129 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001130 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001131 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001132 /* Driver expects to be called at twice the frequency in rc */
1133 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001134
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001135 /* Count down seconds */
1136 do {
1137 /* Count down iterations per second */
1138 i = n;
1139 do {
1140 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001141 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001142 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1143 rtnl_unlock();
1144 if (rc)
1145 break;
1146 schedule_timeout_interruptible(interval);
1147 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001148 } while (!signal_pending(current) &&
1149 (id.data == 0 || --id.data != 0));
1150 }
1151
1152 rtnl_lock();
1153 dev_put(dev);
1154 busy = false;
1155
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001156 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001157 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1161{
1162 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001163 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001165 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001167 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001168 return -EOPNOTSUPP;
1169
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001170 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001171 if (n_stats < 0)
1172 return n_stats;
1173 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1176 return -EFAULT;
1177
Jeff Garzikff03d492007-08-15 16:01:08 -07001178 stats.n_stats = n_stats;
1179 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (!data)
1181 return -ENOMEM;
1182
1183 ops->get_ethtool_stats(dev, &stats, data);
1184
1185 ret = -EFAULT;
1186 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1187 goto out;
1188 useraddr += sizeof(stats);
1189 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1190 goto out;
1191 ret = 0;
1192
1193 out:
1194 kfree(data);
1195 return ret;
1196}
1197
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001198static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001199{
1200 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001201
Matthew Wilcox313674a2007-07-31 14:00:29 -07001202 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001203 return -EFAULT;
1204
Matthew Wilcox313674a2007-07-31 14:00:29 -07001205 if (epaddr.size < dev->addr_len)
1206 return -ETOOSMALL;
1207 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001208
Jon Wetzela6f9a702005-08-20 17:15:54 -07001209 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001210 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001211 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001212 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1213 return -EFAULT;
1214 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001215}
1216
Jeff Garzik13c99b22007-08-15 16:01:56 -07001217static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1218 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001219{
Roland Dreier8e557422010-02-11 12:14:23 -08001220 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001221
Jeff Garzik13c99b22007-08-15 16:01:56 -07001222 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001223 return -EOPNOTSUPP;
1224
Jeff Garzik13c99b22007-08-15 16:01:56 -07001225 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001226
1227 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1228 return -EFAULT;
1229 return 0;
1230}
1231
Jeff Garzik13c99b22007-08-15 16:01:56 -07001232static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1233 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001234{
1235 struct ethtool_value edata;
1236
Jeff Garzik13c99b22007-08-15 16:01:56 -07001237 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001238 return -EOPNOTSUPP;
1239
1240 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1241 return -EFAULT;
1242
Jeff Garzik13c99b22007-08-15 16:01:56 -07001243 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001244 return 0;
1245}
1246
Jeff Garzik13c99b22007-08-15 16:01:56 -07001247static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1248 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001249{
1250 struct ethtool_value edata;
1251
Jeff Garzik13c99b22007-08-15 16:01:56 -07001252 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001253 return -EOPNOTSUPP;
1254
1255 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1256 return -EFAULT;
1257
Jeff Garzik13c99b22007-08-15 16:01:56 -07001258 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001259}
1260
chavey97f8aef2010-04-07 21:54:42 -07001261static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1262 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001263{
1264 struct ethtool_flash efl;
1265
1266 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1267 return -EFAULT;
1268
1269 if (!dev->ethtool_ops->flash_device)
1270 return -EOPNOTSUPP;
1271
Ben Hutchings786f5282012-02-01 09:32:25 +00001272 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1273
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001274 return dev->ethtool_ops->flash_device(dev, &efl);
1275}
1276
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001277static int ethtool_set_dump(struct net_device *dev,
1278 void __user *useraddr)
1279{
1280 struct ethtool_dump dump;
1281
1282 if (!dev->ethtool_ops->set_dump)
1283 return -EOPNOTSUPP;
1284
1285 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1286 return -EFAULT;
1287
1288 return dev->ethtool_ops->set_dump(dev, &dump);
1289}
1290
1291static int ethtool_get_dump_flag(struct net_device *dev,
1292 void __user *useraddr)
1293{
1294 int ret;
1295 struct ethtool_dump dump;
1296 const struct ethtool_ops *ops = dev->ethtool_ops;
1297
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001298 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001299 return -EOPNOTSUPP;
1300
1301 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1302 return -EFAULT;
1303
1304 ret = ops->get_dump_flag(dev, &dump);
1305 if (ret)
1306 return ret;
1307
1308 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1309 return -EFAULT;
1310 return 0;
1311}
1312
1313static int ethtool_get_dump_data(struct net_device *dev,
1314 void __user *useraddr)
1315{
1316 int ret;
1317 __u32 len;
1318 struct ethtool_dump dump, tmp;
1319 const struct ethtool_ops *ops = dev->ethtool_ops;
1320 void *data = NULL;
1321
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001322 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001323 return -EOPNOTSUPP;
1324
1325 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1326 return -EFAULT;
1327
1328 memset(&tmp, 0, sizeof(tmp));
1329 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1330 ret = ops->get_dump_flag(dev, &tmp);
1331 if (ret)
1332 return ret;
1333
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001334 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001335 if (!len)
1336 return -EFAULT;
1337
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001338 /* Don't ever let the driver think there's more space available
1339 * than it requested with .get_dump_flag().
1340 */
1341 dump.len = len;
1342
1343 /* Always allocate enough space to hold the whole thing so that the
1344 * driver does not need to check the length and bother with partial
1345 * dumping.
1346 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001347 data = vzalloc(tmp.len);
1348 if (!data)
1349 return -ENOMEM;
1350 ret = ops->get_dump_data(dev, &dump, data);
1351 if (ret)
1352 goto out;
1353
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001354 /* There are two sane possibilities:
1355 * 1. The driver's .get_dump_data() does not touch dump.len.
1356 * 2. Or it may set dump.len to how much it really writes, which
1357 * should be tmp.len (or len if it can do a partial dump).
1358 * In any case respond to userspace with the actual length of data
1359 * it's receiving.
1360 */
1361 WARN_ON(dump.len != len && dump.len != tmp.len);
1362 dump.len = len;
1363
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001364 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1365 ret = -EFAULT;
1366 goto out;
1367 }
1368 useraddr += offsetof(struct ethtool_dump, data);
1369 if (copy_to_user(useraddr, data, len))
1370 ret = -EFAULT;
1371out:
1372 vfree(data);
1373 return ret;
1374}
1375
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001376static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1377{
1378 int err = 0;
1379 struct ethtool_ts_info info;
1380 const struct ethtool_ops *ops = dev->ethtool_ops;
1381 struct phy_device *phydev = dev->phydev;
1382
1383 memset(&info, 0, sizeof(info));
1384 info.cmd = ETHTOOL_GET_TS_INFO;
1385
1386 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001387 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001388 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001389 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001390 } else {
1391 info.so_timestamping =
1392 SOF_TIMESTAMPING_RX_SOFTWARE |
1393 SOF_TIMESTAMPING_SOFTWARE;
1394 info.phc_index = -1;
1395 }
1396
1397 if (err)
1398 return err;
1399
1400 if (copy_to_user(useraddr, &info, sizeof(info)))
1401 err = -EFAULT;
1402
1403 return err;
1404}
1405
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001406static int ethtool_get_module_info(struct net_device *dev,
1407 void __user *useraddr)
1408{
1409 int ret;
1410 struct ethtool_modinfo modinfo;
1411 const struct ethtool_ops *ops = dev->ethtool_ops;
1412
1413 if (!ops->get_module_info)
1414 return -EOPNOTSUPP;
1415
1416 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1417 return -EFAULT;
1418
1419 ret = ops->get_module_info(dev, &modinfo);
1420 if (ret)
1421 return ret;
1422
1423 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1424 return -EFAULT;
1425
1426 return 0;
1427}
1428
1429static int ethtool_get_module_eeprom(struct net_device *dev,
1430 void __user *useraddr)
1431{
1432 int ret;
1433 struct ethtool_modinfo modinfo;
1434 const struct ethtool_ops *ops = dev->ethtool_ops;
1435
1436 if (!ops->get_module_info || !ops->get_module_eeprom)
1437 return -EOPNOTSUPP;
1438
1439 ret = ops->get_module_info(dev, &modinfo);
1440 if (ret)
1441 return ret;
1442
1443 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1444 modinfo.eeprom_len);
1445}
1446
Yan Burman600fed52013-06-03 02:03:34 +00001447/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Eric W. Biederman881d9662007-09-17 11:56:21 -07001449int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001451 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 void __user *useraddr = ifr->ifr_data;
1453 u32 ethcmd;
1454 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00001455 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (!dev || !netif_device_present(dev))
1458 return -ENODEV;
1459
chavey97f8aef2010-04-07 21:54:42 -07001460 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EFAULT;
1462
Stephen Hemminger75f31232006-09-28 15:13:37 -07001463 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001464 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001465 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001466 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001467 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001468 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001469 case ETHTOOL_GCOALESCE:
1470 case ETHTOOL_GRINGPARAM:
1471 case ETHTOOL_GPAUSEPARAM:
1472 case ETHTOOL_GRXCSUM:
1473 case ETHTOOL_GTXCSUM:
1474 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001475 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001476 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001477 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001478 case ETHTOOL_GTSO:
1479 case ETHTOOL_GPERMADDR:
1480 case ETHTOOL_GUFO:
1481 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001482 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001483 case ETHTOOL_GFLAGS:
1484 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001485 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001486 case ETHTOOL_GRXRINGS:
1487 case ETHTOOL_GRXCLSRLCNT:
1488 case ETHTOOL_GRXCLSRULE:
1489 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001490 case ETHTOOL_GRXFHINDIR:
Michał Mirosław5455c692011-02-15 16:59:17 +00001491 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001492 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001493 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001494 case ETHTOOL_GEEE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001495 break;
1496 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001497 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001498 return -EPERM;
1499 }
1500
chavey97f8aef2010-04-07 21:54:42 -07001501 if (dev->ethtool_ops->begin) {
1502 rc = dev->ethtool_ops->begin(dev);
1503 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001505 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001506 old_features = dev->features;
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 switch (ethcmd) {
1509 case ETHTOOL_GSET:
1510 rc = ethtool_get_settings(dev, useraddr);
1511 break;
1512 case ETHTOOL_SSET:
1513 rc = ethtool_set_settings(dev, useraddr);
1514 break;
1515 case ETHTOOL_GDRVINFO:
1516 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
1518 case ETHTOOL_GREGS:
1519 rc = ethtool_get_regs(dev, useraddr);
1520 break;
1521 case ETHTOOL_GWOL:
1522 rc = ethtool_get_wol(dev, useraddr);
1523 break;
1524 case ETHTOOL_SWOL:
1525 rc = ethtool_set_wol(dev, useraddr);
1526 break;
1527 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001528 rc = ethtool_get_value(dev, useraddr, ethcmd,
1529 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 break;
1531 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001532 rc = ethtool_set_value_void(dev, useraddr,
1533 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001535 case ETHTOOL_GEEE:
1536 rc = ethtool_get_eee(dev, useraddr);
1537 break;
1538 case ETHTOOL_SEEE:
1539 rc = ethtool_set_eee(dev, useraddr);
1540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 case ETHTOOL_NWAY_RST:
1542 rc = ethtool_nway_reset(dev);
1543 break;
1544 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001545 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 break;
1547 case ETHTOOL_GEEPROM:
1548 rc = ethtool_get_eeprom(dev, useraddr);
1549 break;
1550 case ETHTOOL_SEEPROM:
1551 rc = ethtool_set_eeprom(dev, useraddr);
1552 break;
1553 case ETHTOOL_GCOALESCE:
1554 rc = ethtool_get_coalesce(dev, useraddr);
1555 break;
1556 case ETHTOOL_SCOALESCE:
1557 rc = ethtool_set_coalesce(dev, useraddr);
1558 break;
1559 case ETHTOOL_GRINGPARAM:
1560 rc = ethtool_get_ringparam(dev, useraddr);
1561 break;
1562 case ETHTOOL_SRINGPARAM:
1563 rc = ethtool_set_ringparam(dev, useraddr);
1564 break;
1565 case ETHTOOL_GPAUSEPARAM:
1566 rc = ethtool_get_pauseparam(dev, useraddr);
1567 break;
1568 case ETHTOOL_SPAUSEPARAM:
1569 rc = ethtool_set_pauseparam(dev, useraddr);
1570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 case ETHTOOL_TEST:
1572 rc = ethtool_self_test(dev, useraddr);
1573 break;
1574 case ETHTOOL_GSTRINGS:
1575 rc = ethtool_get_strings(dev, useraddr);
1576 break;
1577 case ETHTOOL_PHYS_ID:
1578 rc = ethtool_phys_id(dev, useraddr);
1579 break;
1580 case ETHTOOL_GSTATS:
1581 rc = ethtool_get_stats(dev, useraddr);
1582 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001583 case ETHTOOL_GPERMADDR:
1584 rc = ethtool_get_perm_addr(dev, useraddr);
1585 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001586 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001587 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001588 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001589 break;
1590 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001591 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001592 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001593 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001594 rc = ethtool_get_value(dev, useraddr, ethcmd,
1595 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001596 break;
1597 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001598 rc = ethtool_set_value(dev, useraddr,
1599 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001600 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001601 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001602 case ETHTOOL_GRXRINGS:
1603 case ETHTOOL_GRXCLSRLCNT:
1604 case ETHTOOL_GRXCLSRULE:
1605 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001606 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001607 break;
1608 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001609 case ETHTOOL_SRXCLSRLDEL:
1610 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001611 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001612 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001613 case ETHTOOL_FLASHDEV:
1614 rc = ethtool_flash_device(dev, useraddr);
1615 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001616 case ETHTOOL_RESET:
1617 rc = ethtool_reset(dev, useraddr);
1618 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001619 case ETHTOOL_GSSET_INFO:
1620 rc = ethtool_get_sset_info(dev, useraddr);
1621 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001622 case ETHTOOL_GRXFHINDIR:
1623 rc = ethtool_get_rxfh_indir(dev, useraddr);
1624 break;
1625 case ETHTOOL_SRXFHINDIR:
1626 rc = ethtool_set_rxfh_indir(dev, useraddr);
1627 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001628 case ETHTOOL_GFEATURES:
1629 rc = ethtool_get_features(dev, useraddr);
1630 break;
1631 case ETHTOOL_SFEATURES:
1632 rc = ethtool_set_features(dev, useraddr);
1633 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001634 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001635 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001636 case ETHTOOL_GSG:
1637 case ETHTOOL_GTSO:
1638 case ETHTOOL_GUFO:
1639 case ETHTOOL_GGSO:
1640 case ETHTOOL_GGRO:
1641 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1642 break;
1643 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001644 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001645 case ETHTOOL_SSG:
1646 case ETHTOOL_STSO:
1647 case ETHTOOL_SUFO:
1648 case ETHTOOL_SGSO:
1649 case ETHTOOL_SGRO:
1650 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1651 break;
amit salecha8b5933c2011-04-07 01:58:42 +00001652 case ETHTOOL_GCHANNELS:
1653 rc = ethtool_get_channels(dev, useraddr);
1654 break;
1655 case ETHTOOL_SCHANNELS:
1656 rc = ethtool_set_channels(dev, useraddr);
1657 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001658 case ETHTOOL_SET_DUMP:
1659 rc = ethtool_set_dump(dev, useraddr);
1660 break;
1661 case ETHTOOL_GET_DUMP_FLAG:
1662 rc = ethtool_get_dump_flag(dev, useraddr);
1663 break;
1664 case ETHTOOL_GET_DUMP_DATA:
1665 rc = ethtool_get_dump_data(dev, useraddr);
1666 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001667 case ETHTOOL_GET_TS_INFO:
1668 rc = ethtool_get_ts_info(dev, useraddr);
1669 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001670 case ETHTOOL_GMODULEINFO:
1671 rc = ethtool_get_module_info(dev, useraddr);
1672 break;
1673 case ETHTOOL_GMODULEEEPROM:
1674 rc = ethtool_get_module_eeprom(dev, useraddr);
1675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001677 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001679
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001680 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001682
1683 if (old_features != dev->features)
1684 netdev_features_change(dev);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}