blob: b87712cfd26cdaf5cdc24b87e579acc918a39004 [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",
Patrick McHardyf6469682013-04-19 02:04:27 +000063 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-ctag-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000064
Patrick McHardyf6469682013-04-19 02:04:27 +000065 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-ctag-hw-parse",
66 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-ctag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000067 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
68 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
69 [NETIF_F_LLTX_BIT] = "tx-lockless",
70 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
71 [NETIF_F_GRO_BIT] = "rx-gro",
72 [NETIF_F_LRO_BIT] = "rx-lro",
73
74 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
75 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
76 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
77 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
78 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
79 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000080 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000081 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000082
83 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
84 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
85 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
86 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
87 [NETIF_F_RXHASH_BIT] = "rx-hashing",
88 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
89 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
90 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000091 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000092 [NETIF_F_RXALL_BIT] = "rx-all",
Michał Mirosław9d921542011-11-15 15:29:55 +000093};
94
Michał Mirosław5455c692011-02-15 16:59:17 +000095static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
96{
97 struct ethtool_gfeatures cmd = {
98 .cmd = ETHTOOL_GFEATURES,
99 .size = ETHTOOL_DEV_FEATURE_WORDS,
100 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000101 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000102 u32 __user *sizeaddr;
103 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000104 int i;
105
106 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000107 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000108
109 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000110 features[i].available = (u32)(dev->hw_features >> (32 * i));
111 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
112 features[i].active = (u32)(dev->features >> (32 * i));
113 features[i].never_changed =
114 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000115 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000116
117 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
118 if (get_user(copy_size, sizeaddr))
119 return -EFAULT;
120
121 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
122 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
123
124 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
125 return -EFAULT;
126 useraddr += sizeof(cmd);
127 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
128 return -EFAULT;
129
130 return 0;
131}
132
133static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
134{
135 struct ethtool_sfeatures cmd;
136 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000137 netdev_features_t wanted = 0, valid = 0;
138 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000139
140 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
141 return -EFAULT;
142 useraddr += sizeof(cmd);
143
144 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
145 return -EINVAL;
146
147 if (copy_from_user(features, useraddr, sizeof(features)))
148 return -EFAULT;
149
Michał Mirosław475414f2011-11-15 15:29:55 +0000150 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000151 valid |= (netdev_features_t)features[i].valid << (32 * i);
152 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000153 }
154
155 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000156 return -EINVAL;
157
Michał Mirosław475414f2011-11-15 15:29:55 +0000158 if (valid & ~dev->hw_features) {
159 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000160 ret |= ETHTOOL_F_UNSUPPORTED;
161 }
162
Michał Mirosław475414f2011-11-15 15:29:55 +0000163 dev->wanted_features &= ~valid;
164 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700165 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000166
Michał Mirosław475414f2011-11-15 15:29:55 +0000167 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000168 ret |= ETHTOOL_F_WISH;
169
170 return ret;
171}
172
Michał Mirosław340ae162011-02-15 16:59:16 +0000173static int __ethtool_get_sset_count(struct net_device *dev, int sset)
174{
175 const struct ethtool_ops *ops = dev->ethtool_ops;
176
Michał Mirosław5455c692011-02-15 16:59:17 +0000177 if (sset == ETH_SS_FEATURES)
178 return ARRAY_SIZE(netdev_features_strings);
179
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000180 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000181 return ops->get_sset_count(dev, sset);
182 else
183 return -EOPNOTSUPP;
184}
185
186static void __ethtool_get_strings(struct net_device *dev,
187 u32 stringset, u8 *data)
188{
189 const struct ethtool_ops *ops = dev->ethtool_ops;
190
Michał Mirosław5455c692011-02-15 16:59:17 +0000191 if (stringset == ETH_SS_FEATURES)
192 memcpy(data, netdev_features_strings,
193 sizeof(netdev_features_strings));
194 else
195 /* ops->get_strings is valid because checked earlier */
196 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000197}
198
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000199static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000200{
201 /* feature masks of legacy discrete ethtool ops */
202
203 switch (eth_cmd) {
204 case ETHTOOL_GTXCSUM:
205 case ETHTOOL_STXCSUM:
206 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000207 case ETHTOOL_GRXCSUM:
208 case ETHTOOL_SRXCSUM:
209 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000210 case ETHTOOL_GSG:
211 case ETHTOOL_SSG:
212 return NETIF_F_SG;
213 case ETHTOOL_GTSO:
214 case ETHTOOL_STSO:
215 return NETIF_F_ALL_TSO;
216 case ETHTOOL_GUFO:
217 case ETHTOOL_SUFO:
218 return NETIF_F_UFO;
219 case ETHTOOL_GGSO:
220 case ETHTOOL_SGSO:
221 return NETIF_F_GSO;
222 case ETHTOOL_GGRO:
223 case ETHTOOL_SGRO:
224 return NETIF_F_GRO;
225 default:
226 BUG();
227 }
228}
229
Michał Mirosław0a417702011-02-15 16:59:17 +0000230static int ethtool_get_one_feature(struct net_device *dev,
231 char __user *useraddr, u32 ethcmd)
232{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000233 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000234 struct ethtool_value edata = {
235 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000236 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000237 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000238
Michał Mirosław0a417702011-02-15 16:59:17 +0000239 if (copy_to_user(useraddr, &edata, sizeof(edata)))
240 return -EFAULT;
241 return 0;
242}
243
Michał Mirosław0a417702011-02-15 16:59:17 +0000244static int ethtool_set_one_feature(struct net_device *dev,
245 void __user *useraddr, u32 ethcmd)
246{
247 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000248 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000249
250 if (copy_from_user(&edata, useraddr, sizeof(edata)))
251 return -EFAULT;
252
Michał Mirosław86794882011-02-15 16:59:17 +0000253 mask = ethtool_get_feature_mask(ethcmd);
254 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000255 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000256 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000257
258 if (edata.data)
259 dev->wanted_features |= mask;
260 else
261 dev->wanted_features &= ~mask;
262
263 __netdev_update_features(dev);
264
265 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000266}
267
Michał Mirosław02b3a552011-11-15 15:29:55 +0000268#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
269 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000270#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
271 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
272 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000273
274static u32 __ethtool_get_flags(struct net_device *dev)
275{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000276 u32 flags = 0;
277
Patrick McHardyf6469682013-04-19 02:04:27 +0000278 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
279 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) flags |= ETH_FLAG_RXVLAN;
280 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) flags |= ETH_FLAG_TXVLAN;
281 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
282 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000283
284 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000285}
286
287static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000288{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000289 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000290
Michał Mirosław02b3a552011-11-15 15:29:55 +0000291 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000292 return -EINVAL;
293
Michał Mirosław02b3a552011-11-15 15:29:55 +0000294 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
Patrick McHardyf6469682013-04-19 02:04:27 +0000295 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_CTAG_RX;
296 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_CTAG_TX;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000297 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
298 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
299
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000300 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000301 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000302 if (changed & ~dev->hw_features)
303 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
304
305 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000306 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000307
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700308 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000309
310 return 0;
311}
312
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000313int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000315 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000317 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EOPNOTSUPP;
319
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000320 memset(cmd, 0, sizeof(struct ethtool_cmd));
321 cmd->cmd = ETHTOOL_GSET;
322 return dev->ethtool_ops->get_settings(dev, cmd);
323}
324EXPORT_SYMBOL(__ethtool_get_settings);
325
326static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
327{
328 int err;
329 struct ethtool_cmd cmd;
330
331 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (err < 0)
333 return err;
334
335 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
336 return -EFAULT;
337 return 0;
338}
339
340static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
341{
342 struct ethtool_cmd cmd;
343
344 if (!dev->ethtool_ops->set_settings)
345 return -EOPNOTSUPP;
346
347 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
348 return -EFAULT;
349
350 return dev->ethtool_ops->set_settings(dev, &cmd);
351}
352
chavey97f8aef2010-04-07 21:54:42 -0700353static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
354 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700357 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 memset(&info, 0, sizeof(info));
360 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000361 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700362 ops->get_drvinfo(dev, &info);
363 } else if (dev->dev.parent && dev->dev.parent->driver) {
364 strlcpy(info.bus_info, dev_name(dev->dev.parent),
365 sizeof(info.bus_info));
366 strlcpy(info.driver, dev->dev.parent->driver->name,
367 sizeof(info.driver));
368 } else {
369 return -EOPNOTSUPP;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jeff Garzik723b2f52010-03-03 22:51:50 +0000372 /*
373 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000374 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000375 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000376 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700377 int rc;
378
379 rc = ops->get_sset_count(dev, ETH_SS_TEST);
380 if (rc >= 0)
381 info.testinfo_len = rc;
382 rc = ops->get_sset_count(dev, ETH_SS_STATS);
383 if (rc >= 0)
384 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700385 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
386 if (rc >= 0)
387 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700388 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000389 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000391 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 info.eedump_len = ops->get_eeprom_len(dev);
393
394 if (copy_to_user(useraddr, &info, sizeof(info)))
395 return -EFAULT;
396 return 0;
397}
398
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800399static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700400 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000401{
402 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000403 u64 sset_mask;
404 int i, idx = 0, n_bits = 0, ret, rc;
405 u32 *info_buf = NULL;
406
Jeff Garzik723b2f52010-03-03 22:51:50 +0000407 if (copy_from_user(&info, useraddr, sizeof(info)))
408 return -EFAULT;
409
410 /* store copy of mask, because we zero struct later on */
411 sset_mask = info.sset_mask;
412 if (!sset_mask)
413 return 0;
414
415 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000416 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000417
418 memset(&info, 0, sizeof(info));
419 info.cmd = ETHTOOL_GSSET_INFO;
420
421 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
422 if (!info_buf)
423 return -ENOMEM;
424
425 /*
426 * fill return buffer based on input bitmask and successful
427 * get_sset_count return
428 */
429 for (i = 0; i < 64; i++) {
430 if (!(sset_mask & (1ULL << i)))
431 continue;
432
Michał Mirosław340ae162011-02-15 16:59:16 +0000433 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000434 if (rc >= 0) {
435 info.sset_mask |= (1ULL << i);
436 info_buf[idx++] = rc;
437 }
438 }
439
440 ret = -EFAULT;
441 if (copy_to_user(useraddr, &info, sizeof(info)))
442 goto out;
443
444 useraddr += offsetof(struct ethtool_sset_info, data);
445 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
446 goto out;
447
448 ret = 0;
449
450out:
451 kfree(info_buf);
452 return ret;
453}
454
chavey97f8aef2010-04-07 21:54:42 -0700455static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000456 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700457{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000458 struct ethtool_rxnfc info;
459 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000460 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700461
Santwona Behera59089d82009-02-20 00:58:13 -0800462 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700463 return -EOPNOTSUPP;
464
Ben Hutchingsbf988432010-06-28 08:45:58 +0000465 /* struct ethtool_rxnfc was originally defined for
466 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
467 * members. User-space might still be using that
468 * definition. */
469 if (cmd == ETHTOOL_SRXFH)
470 info_size = (offsetof(struct ethtool_rxnfc, data) +
471 sizeof(info.data));
472
473 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700474 return -EFAULT;
475
Ben Hutchings55664f32012-01-03 12:04:51 +0000476 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
477 if (rc)
478 return rc;
479
480 if (cmd == ETHTOOL_SRXCLSRLINS &&
481 copy_to_user(useraddr, &info, info_size))
482 return -EFAULT;
483
484 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700485}
486
chavey97f8aef2010-04-07 21:54:42 -0700487static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000488 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700489{
490 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000491 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800492 const struct ethtool_ops *ops = dev->ethtool_ops;
493 int ret;
494 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700495
Santwona Behera59089d82009-02-20 00:58:13 -0800496 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700497 return -EOPNOTSUPP;
498
Ben Hutchingsbf988432010-06-28 08:45:58 +0000499 /* struct ethtool_rxnfc was originally defined for
500 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
501 * members. User-space might still be using that
502 * definition. */
503 if (cmd == ETHTOOL_GRXFH)
504 info_size = (offsetof(struct ethtool_rxnfc, data) +
505 sizeof(info.data));
506
507 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700508 return -EFAULT;
509
Santwona Behera59089d82009-02-20 00:58:13 -0800510 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
511 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000512 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000513 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000514 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800515 if (!rule_buf)
516 return -ENOMEM;
517 }
518 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700519
Santwona Behera59089d82009-02-20 00:58:13 -0800520 ret = ops->get_rxnfc(dev, &info, rule_buf);
521 if (ret < 0)
522 goto err_out;
523
524 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000525 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800526 goto err_out;
527
528 if (rule_buf) {
529 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
530 if (copy_to_user(useraddr, rule_buf,
531 info.rule_cnt * sizeof(u32)))
532 goto err_out;
533 }
534 ret = 0;
535
536err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700537 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800538
539 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700540}
541
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000542static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
543 void __user *useraddr)
544{
Ben Hutchings7850f632011-12-15 13:55:01 +0000545 u32 user_size, dev_size;
546 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000547 int ret;
548
Ben Hutchings7850f632011-12-15 13:55:01 +0000549 if (!dev->ethtool_ops->get_rxfh_indir_size ||
550 !dev->ethtool_ops->get_rxfh_indir)
551 return -EOPNOTSUPP;
552 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
553 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000554 return -EOPNOTSUPP;
555
Ben Hutchings7850f632011-12-15 13:55:01 +0000556 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000557 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000558 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000559 return -EFAULT;
560
Ben Hutchings7850f632011-12-15 13:55:01 +0000561 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
562 &dev_size, sizeof(dev_size)))
563 return -EFAULT;
564
565 /* If the user buffer size is 0, this is just a query for the
566 * device table size. Otherwise, if it's smaller than the
567 * device table size it's an error.
568 */
569 if (user_size < dev_size)
570 return user_size == 0 ? 0 : -EINVAL;
571
572 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000573 if (!indir)
574 return -ENOMEM;
575
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000576 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
577 if (ret)
578 goto out;
579
Ben Hutchings7850f632011-12-15 13:55:01 +0000580 if (copy_to_user(useraddr +
581 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
582 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000583 ret = -EFAULT;
584
585out:
586 kfree(indir);
587 return ret;
588}
589
590static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
591 void __user *useraddr)
592{
Ben Hutchings7850f632011-12-15 13:55:01 +0000593 struct ethtool_rxnfc rx_rings;
594 u32 user_size, dev_size, i;
595 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000596 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000597 int ret;
598
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000599 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
600 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000601 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000602
603 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000604 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000605 return -EOPNOTSUPP;
606
Ben Hutchings7850f632011-12-15 13:55:01 +0000607 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000608 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000609 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000610 return -EFAULT;
611
Ben Hutchings278bc422011-12-15 13:56:49 +0000612 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000613 return -EINVAL;
614
615 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000616 if (!indir)
617 return -ENOMEM;
618
Ben Hutchings7850f632011-12-15 13:55:01 +0000619 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000620 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000621 if (ret)
622 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000623
624 if (user_size == 0) {
625 for (i = 0; i < dev_size; i++)
626 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
627 } else {
628 if (copy_from_user(indir,
629 useraddr +
630 offsetof(struct ethtool_rxfh_indir,
631 ring_index[0]),
632 dev_size * sizeof(indir[0]))) {
633 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000634 goto out;
635 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000636
637 /* Validate ring indices */
638 for (i = 0; i < dev_size; i++) {
639 if (indir[i] >= rx_rings.data) {
640 ret = -EINVAL;
641 goto out;
642 }
643 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000644 }
645
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000646 ret = ops->set_rxfh_indir(dev, indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000647
648out:
649 kfree(indir);
650 return ret;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
654{
655 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700656 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 void *regbuf;
658 int reglen, ret;
659
660 if (!ops->get_regs || !ops->get_regs_len)
661 return -EOPNOTSUPP;
662
663 if (copy_from_user(&regs, useraddr, sizeof(regs)))
664 return -EFAULT;
665
666 reglen = ops->get_regs_len(dev);
667 if (regs.len > reglen)
668 regs.len = reglen;
669
Eugene Teob7c7d012011-01-24 21:05:17 -0800670 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700671 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -ENOMEM;
673
674 ops->get_regs(dev, &regs, regbuf);
675
676 ret = -EFAULT;
677 if (copy_to_user(useraddr, &regs, sizeof(regs)))
678 goto out;
679 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700680 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto out;
682 ret = 0;
683
684 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000685 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return ret;
687}
688
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000689static int ethtool_reset(struct net_device *dev, char __user *useraddr)
690{
691 struct ethtool_value reset;
692 int ret;
693
694 if (!dev->ethtool_ops->reset)
695 return -EOPNOTSUPP;
696
697 if (copy_from_user(&reset, useraddr, sizeof(reset)))
698 return -EFAULT;
699
700 ret = dev->ethtool_ops->reset(dev, &reset.data);
701 if (ret)
702 return ret;
703
704 if (copy_to_user(useraddr, &reset, sizeof(reset)))
705 return -EFAULT;
706 return 0;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
710{
Roland Dreier8e557422010-02-11 12:14:23 -0800711 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (!dev->ethtool_ops->get_wol)
714 return -EOPNOTSUPP;
715
716 dev->ethtool_ops->get_wol(dev, &wol);
717
718 if (copy_to_user(useraddr, &wol, sizeof(wol)))
719 return -EFAULT;
720 return 0;
721}
722
723static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
724{
725 struct ethtool_wolinfo wol;
726
727 if (!dev->ethtool_ops->set_wol)
728 return -EOPNOTSUPP;
729
730 if (copy_from_user(&wol, useraddr, sizeof(wol)))
731 return -EFAULT;
732
733 return dev->ethtool_ops->set_wol(dev, &wol);
734}
735
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000736static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
737{
738 struct ethtool_eee edata;
739 int rc;
740
741 if (!dev->ethtool_ops->get_eee)
742 return -EOPNOTSUPP;
743
744 memset(&edata, 0, sizeof(struct ethtool_eee));
745 edata.cmd = ETHTOOL_GEEE;
746 rc = dev->ethtool_ops->get_eee(dev, &edata);
747
748 if (rc)
749 return rc;
750
751 if (copy_to_user(useraddr, &edata, sizeof(edata)))
752 return -EFAULT;
753
754 return 0;
755}
756
757static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
758{
759 struct ethtool_eee edata;
760
761 if (!dev->ethtool_ops->set_eee)
762 return -EOPNOTSUPP;
763
764 if (copy_from_user(&edata, useraddr, sizeof(edata)))
765 return -EFAULT;
766
767 return dev->ethtool_ops->set_eee(dev, &edata);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static int ethtool_nway_reset(struct net_device *dev)
771{
772 if (!dev->ethtool_ops->nway_reset)
773 return -EOPNOTSUPP;
774
775 return dev->ethtool_ops->nway_reset(dev);
776}
777
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000778static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
779{
780 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
781
782 if (!dev->ethtool_ops->get_link)
783 return -EOPNOTSUPP;
784
785 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
786
787 if (copy_to_user(useraddr, &edata, sizeof(edata)))
788 return -EFAULT;
789 return 0;
790}
791
Ben Hutchings081d0942012-04-12 00:42:12 +0000792static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
793 int (*getter)(struct net_device *,
794 struct ethtool_eeprom *, u8 *),
795 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700798 void __user *userbuf = useraddr + sizeof(eeprom);
799 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700801 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
804 return -EFAULT;
805
806 /* Check for wrap and zero */
807 if (eeprom.offset + eeprom.len <= eeprom.offset)
808 return -EINVAL;
809
810 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +0000811 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EINVAL;
813
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700814 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!data)
816 return -ENOMEM;
817
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700818 bytes_remaining = eeprom.len;
819 while (bytes_remaining > 0) {
820 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Ben Hutchings081d0942012-04-12 00:42:12 +0000822 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700823 if (ret)
824 break;
825 if (copy_to_user(userbuf, data, eeprom.len)) {
826 ret = -EFAULT;
827 break;
828 }
829 userbuf += eeprom.len;
830 eeprom.offset += eeprom.len;
831 bytes_remaining -= eeprom.len;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700834 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
835 eeprom.offset -= eeprom.len;
836 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
837 ret = -EFAULT;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 kfree(data);
840 return ret;
841}
842
Ben Hutchings081d0942012-04-12 00:42:12 +0000843static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
844{
845 const struct ethtool_ops *ops = dev->ethtool_ops;
846
847 if (!ops->get_eeprom || !ops->get_eeprom_len)
848 return -EOPNOTSUPP;
849
850 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
851 ops->get_eeprom_len(dev));
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
855{
856 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700857 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700858 void __user *userbuf = useraddr + sizeof(eeprom);
859 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700861 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (!ops->set_eeprom || !ops->get_eeprom_len)
864 return -EOPNOTSUPP;
865
866 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
867 return -EFAULT;
868
869 /* Check for wrap and zero */
870 if (eeprom.offset + eeprom.len <= eeprom.offset)
871 return -EINVAL;
872
873 /* Check for exceeding total eeprom len */
874 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
875 return -EINVAL;
876
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700877 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!data)
879 return -ENOMEM;
880
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700881 bytes_remaining = eeprom.len;
882 while (bytes_remaining > 0) {
883 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700885 if (copy_from_user(data, userbuf, eeprom.len)) {
886 ret = -EFAULT;
887 break;
888 }
889 ret = ops->set_eeprom(dev, &eeprom, data);
890 if (ret)
891 break;
892 userbuf += eeprom.len;
893 eeprom.offset += eeprom.len;
894 bytes_remaining -= eeprom.len;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 kfree(data);
898 return ret;
899}
900
chavey97f8aef2010-04-07 21:54:42 -0700901static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
902 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Roland Dreier8e557422010-02-11 12:14:23 -0800904 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (!dev->ethtool_ops->get_coalesce)
907 return -EOPNOTSUPP;
908
909 dev->ethtool_ops->get_coalesce(dev, &coalesce);
910
911 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
912 return -EFAULT;
913 return 0;
914}
915
chavey97f8aef2010-04-07 21:54:42 -0700916static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
917 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct ethtool_coalesce coalesce;
920
David S. Millerfa04ae52005-06-06 15:07:19 -0700921 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -EOPNOTSUPP;
923
924 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
925 return -EFAULT;
926
927 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
928}
929
930static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
931{
Roland Dreier8e557422010-02-11 12:14:23 -0800932 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (!dev->ethtool_ops->get_ringparam)
935 return -EOPNOTSUPP;
936
937 dev->ethtool_ops->get_ringparam(dev, &ringparam);
938
939 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
940 return -EFAULT;
941 return 0;
942}
943
944static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
945{
946 struct ethtool_ringparam ringparam;
947
948 if (!dev->ethtool_ops->set_ringparam)
949 return -EOPNOTSUPP;
950
951 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
952 return -EFAULT;
953
954 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
955}
956
amit salecha8b5933c32011-04-07 01:58:42 +0000957static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
958 void __user *useraddr)
959{
960 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
961
962 if (!dev->ethtool_ops->get_channels)
963 return -EOPNOTSUPP;
964
965 dev->ethtool_ops->get_channels(dev, &channels);
966
967 if (copy_to_user(useraddr, &channels, sizeof(channels)))
968 return -EFAULT;
969 return 0;
970}
971
972static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
973 void __user *useraddr)
974{
975 struct ethtool_channels channels;
976
977 if (!dev->ethtool_ops->set_channels)
978 return -EOPNOTSUPP;
979
980 if (copy_from_user(&channels, useraddr, sizeof(channels)))
981 return -EFAULT;
982
983 return dev->ethtool_ops->set_channels(dev, &channels);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
987{
988 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
989
990 if (!dev->ethtool_ops->get_pauseparam)
991 return -EOPNOTSUPP;
992
993 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
994
995 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
996 return -EFAULT;
997 return 0;
998}
999
1000static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1001{
1002 struct ethtool_pauseparam pauseparam;
1003
Jeff Garzike1b90c42006-07-17 12:54:40 -04001004 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EOPNOTSUPP;
1006
1007 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1008 return -EFAULT;
1009
1010 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1014{
1015 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001016 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001018 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001020 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001021 return -EOPNOTSUPP;
1022
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001023 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001024 if (test_len < 0)
1025 return test_len;
1026 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (copy_from_user(&test, useraddr, sizeof(test)))
1029 return -EFAULT;
1030
Jeff Garzikff03d492007-08-15 16:01:08 -07001031 test.len = test_len;
1032 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!data)
1034 return -ENOMEM;
1035
1036 ops->self_test(dev, &test, data);
1037
1038 ret = -EFAULT;
1039 if (copy_to_user(useraddr, &test, sizeof(test)))
1040 goto out;
1041 useraddr += sizeof(test);
1042 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1043 goto out;
1044 ret = 0;
1045
1046 out:
1047 kfree(data);
1048 return ret;
1049}
1050
1051static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1052{
1053 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 u8 *data;
1055 int ret;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1058 return -EFAULT;
1059
Michał Mirosław340ae162011-02-15 16:59:16 +00001060 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001061 if (ret < 0)
1062 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001063
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001064 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1067 if (!data)
1068 return -ENOMEM;
1069
Michał Mirosław340ae162011-02-15 16:59:16 +00001070 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 ret = -EFAULT;
1073 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1074 goto out;
1075 useraddr += sizeof(gstrings);
1076 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1077 goto out;
1078 ret = 0;
1079
Michał Mirosław340ae162011-02-15 16:59:16 +00001080out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 kfree(data);
1082 return ret;
1083}
1084
1085static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1086{
1087 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001088 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001089 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001090 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001092 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -EOPNOTSUPP;
1094
Ben Hutchings68f512f2011-04-02 00:35:15 +01001095 if (busy)
1096 return -EBUSY;
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (copy_from_user(&id, useraddr, sizeof(id)))
1099 return -EFAULT;
1100
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001101 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001102 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001103 return rc;
1104
1105 /* Drop the RTNL lock while waiting, but prevent reentry or
1106 * removal of the device.
1107 */
1108 busy = true;
1109 dev_hold(dev);
1110 rtnl_unlock();
1111
1112 if (rc == 0) {
1113 /* Driver will handle this itself */
1114 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001115 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001116 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001117 /* Driver expects to be called at twice the frequency in rc */
1118 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001119
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001120 /* Count down seconds */
1121 do {
1122 /* Count down iterations per second */
1123 i = n;
1124 do {
1125 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001126 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001127 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1128 rtnl_unlock();
1129 if (rc)
1130 break;
1131 schedule_timeout_interruptible(interval);
1132 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001133 } while (!signal_pending(current) &&
1134 (id.data == 0 || --id.data != 0));
1135 }
1136
1137 rtnl_lock();
1138 dev_put(dev);
1139 busy = false;
1140
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001141 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001142 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1146{
1147 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001148 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001150 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001152 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001153 return -EOPNOTSUPP;
1154
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001155 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001156 if (n_stats < 0)
1157 return n_stats;
1158 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1161 return -EFAULT;
1162
Jeff Garzikff03d492007-08-15 16:01:08 -07001163 stats.n_stats = n_stats;
1164 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!data)
1166 return -ENOMEM;
1167
1168 ops->get_ethtool_stats(dev, &stats, data);
1169
1170 ret = -EFAULT;
1171 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1172 goto out;
1173 useraddr += sizeof(stats);
1174 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1175 goto out;
1176 ret = 0;
1177
1178 out:
1179 kfree(data);
1180 return ret;
1181}
1182
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001183static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001184{
1185 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001186
Matthew Wilcox313674a2007-07-31 14:00:29 -07001187 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001188 return -EFAULT;
1189
Matthew Wilcox313674a2007-07-31 14:00:29 -07001190 if (epaddr.size < dev->addr_len)
1191 return -ETOOSMALL;
1192 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001193
Jon Wetzela6f9a702005-08-20 17:15:54 -07001194 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001195 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001196 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001197 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1198 return -EFAULT;
1199 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001200}
1201
Jeff Garzik13c99b22007-08-15 16:01:56 -07001202static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1203 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001204{
Roland Dreier8e557422010-02-11 12:14:23 -08001205 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001206
Jeff Garzik13c99b22007-08-15 16:01:56 -07001207 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001208 return -EOPNOTSUPP;
1209
Jeff Garzik13c99b22007-08-15 16:01:56 -07001210 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001211
1212 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1213 return -EFAULT;
1214 return 0;
1215}
1216
Jeff Garzik13c99b22007-08-15 16:01:56 -07001217static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1218 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001219{
1220 struct ethtool_value edata;
1221
Jeff Garzik13c99b22007-08-15 16:01:56 -07001222 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001223 return -EOPNOTSUPP;
1224
1225 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1226 return -EFAULT;
1227
Jeff Garzik13c99b22007-08-15 16:01:56 -07001228 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001229 return 0;
1230}
1231
Jeff Garzik13c99b22007-08-15 16:01:56 -07001232static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1233 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001234{
1235 struct ethtool_value edata;
1236
Jeff Garzik13c99b22007-08-15 16:01:56 -07001237 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -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 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001244}
1245
chavey97f8aef2010-04-07 21:54:42 -07001246static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1247 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001248{
1249 struct ethtool_flash efl;
1250
1251 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1252 return -EFAULT;
1253
1254 if (!dev->ethtool_ops->flash_device)
1255 return -EOPNOTSUPP;
1256
Ben Hutchings786f5282012-02-01 09:32:25 +00001257 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1258
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001259 return dev->ethtool_ops->flash_device(dev, &efl);
1260}
1261
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001262static int ethtool_set_dump(struct net_device *dev,
1263 void __user *useraddr)
1264{
1265 struct ethtool_dump dump;
1266
1267 if (!dev->ethtool_ops->set_dump)
1268 return -EOPNOTSUPP;
1269
1270 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1271 return -EFAULT;
1272
1273 return dev->ethtool_ops->set_dump(dev, &dump);
1274}
1275
1276static int ethtool_get_dump_flag(struct net_device *dev,
1277 void __user *useraddr)
1278{
1279 int ret;
1280 struct ethtool_dump dump;
1281 const struct ethtool_ops *ops = dev->ethtool_ops;
1282
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001283 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001284 return -EOPNOTSUPP;
1285
1286 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1287 return -EFAULT;
1288
1289 ret = ops->get_dump_flag(dev, &dump);
1290 if (ret)
1291 return ret;
1292
1293 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1294 return -EFAULT;
1295 return 0;
1296}
1297
1298static int ethtool_get_dump_data(struct net_device *dev,
1299 void __user *useraddr)
1300{
1301 int ret;
1302 __u32 len;
1303 struct ethtool_dump dump, tmp;
1304 const struct ethtool_ops *ops = dev->ethtool_ops;
1305 void *data = NULL;
1306
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001307 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001308 return -EOPNOTSUPP;
1309
1310 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1311 return -EFAULT;
1312
1313 memset(&tmp, 0, sizeof(tmp));
1314 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1315 ret = ops->get_dump_flag(dev, &tmp);
1316 if (ret)
1317 return ret;
1318
1319 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1320 if (!len)
1321 return -EFAULT;
1322
1323 data = vzalloc(tmp.len);
1324 if (!data)
1325 return -ENOMEM;
1326 ret = ops->get_dump_data(dev, &dump, data);
1327 if (ret)
1328 goto out;
1329
1330 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1331 ret = -EFAULT;
1332 goto out;
1333 }
1334 useraddr += offsetof(struct ethtool_dump, data);
1335 if (copy_to_user(useraddr, data, len))
1336 ret = -EFAULT;
1337out:
1338 vfree(data);
1339 return ret;
1340}
1341
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001342static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1343{
1344 int err = 0;
1345 struct ethtool_ts_info info;
1346 const struct ethtool_ops *ops = dev->ethtool_ops;
1347 struct phy_device *phydev = dev->phydev;
1348
1349 memset(&info, 0, sizeof(info));
1350 info.cmd = ETHTOOL_GET_TS_INFO;
1351
1352 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001353 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001354 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001355 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001356 } else {
1357 info.so_timestamping =
1358 SOF_TIMESTAMPING_RX_SOFTWARE |
1359 SOF_TIMESTAMPING_SOFTWARE;
1360 info.phc_index = -1;
1361 }
1362
1363 if (err)
1364 return err;
1365
1366 if (copy_to_user(useraddr, &info, sizeof(info)))
1367 err = -EFAULT;
1368
1369 return err;
1370}
1371
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001372static int ethtool_get_module_info(struct net_device *dev,
1373 void __user *useraddr)
1374{
1375 int ret;
1376 struct ethtool_modinfo modinfo;
1377 const struct ethtool_ops *ops = dev->ethtool_ops;
1378
1379 if (!ops->get_module_info)
1380 return -EOPNOTSUPP;
1381
1382 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1383 return -EFAULT;
1384
1385 ret = ops->get_module_info(dev, &modinfo);
1386 if (ret)
1387 return ret;
1388
1389 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1390 return -EFAULT;
1391
1392 return 0;
1393}
1394
1395static int ethtool_get_module_eeprom(struct net_device *dev,
1396 void __user *useraddr)
1397{
1398 int ret;
1399 struct ethtool_modinfo modinfo;
1400 const struct ethtool_ops *ops = dev->ethtool_ops;
1401
1402 if (!ops->get_module_info || !ops->get_module_eeprom)
1403 return -EOPNOTSUPP;
1404
1405 ret = ops->get_module_info(dev, &modinfo);
1406 if (ret)
1407 return ret;
1408
1409 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1410 modinfo.eeprom_len);
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/* The main entry point in this file. Called from net/core/dev.c */
1414
Eric W. Biederman881d9662007-09-17 11:56:21 -07001415int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001417 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 void __user *useraddr = ifr->ifr_data;
1419 u32 ethcmd;
1420 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001421 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!dev || !netif_device_present(dev))
1424 return -ENODEV;
1425
chavey97f8aef2010-04-07 21:54:42 -07001426 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EFAULT;
1428
Stephen Hemminger75f31232006-09-28 15:13:37 -07001429 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001430 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001431 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001432 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001433 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001434 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001435 case ETHTOOL_GCOALESCE:
1436 case ETHTOOL_GRINGPARAM:
1437 case ETHTOOL_GPAUSEPARAM:
1438 case ETHTOOL_GRXCSUM:
1439 case ETHTOOL_GTXCSUM:
1440 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001441 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001442 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001443 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001444 case ETHTOOL_GTSO:
1445 case ETHTOOL_GPERMADDR:
1446 case ETHTOOL_GUFO:
1447 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001448 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001449 case ETHTOOL_GFLAGS:
1450 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001451 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001452 case ETHTOOL_GRXRINGS:
1453 case ETHTOOL_GRXCLSRLCNT:
1454 case ETHTOOL_GRXCLSRULE:
1455 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001456 case ETHTOOL_GRXFHINDIR:
Michał Mirosław5455c692011-02-15 16:59:17 +00001457 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001458 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001459 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001460 case ETHTOOL_GEEE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001461 break;
1462 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001463 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001464 return -EPERM;
1465 }
1466
chavey97f8aef2010-04-07 21:54:42 -07001467 if (dev->ethtool_ops->begin) {
1468 rc = dev->ethtool_ops->begin(dev);
1469 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001471 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001472 old_features = dev->features;
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 switch (ethcmd) {
1475 case ETHTOOL_GSET:
1476 rc = ethtool_get_settings(dev, useraddr);
1477 break;
1478 case ETHTOOL_SSET:
1479 rc = ethtool_set_settings(dev, useraddr);
1480 break;
1481 case ETHTOOL_GDRVINFO:
1482 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484 case ETHTOOL_GREGS:
1485 rc = ethtool_get_regs(dev, useraddr);
1486 break;
1487 case ETHTOOL_GWOL:
1488 rc = ethtool_get_wol(dev, useraddr);
1489 break;
1490 case ETHTOOL_SWOL:
1491 rc = ethtool_set_wol(dev, useraddr);
1492 break;
1493 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001494 rc = ethtool_get_value(dev, useraddr, ethcmd,
1495 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001498 rc = ethtool_set_value_void(dev, useraddr,
1499 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001501 case ETHTOOL_GEEE:
1502 rc = ethtool_get_eee(dev, useraddr);
1503 break;
1504 case ETHTOOL_SEEE:
1505 rc = ethtool_set_eee(dev, useraddr);
1506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 case ETHTOOL_NWAY_RST:
1508 rc = ethtool_nway_reset(dev);
1509 break;
1510 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001511 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 break;
1513 case ETHTOOL_GEEPROM:
1514 rc = ethtool_get_eeprom(dev, useraddr);
1515 break;
1516 case ETHTOOL_SEEPROM:
1517 rc = ethtool_set_eeprom(dev, useraddr);
1518 break;
1519 case ETHTOOL_GCOALESCE:
1520 rc = ethtool_get_coalesce(dev, useraddr);
1521 break;
1522 case ETHTOOL_SCOALESCE:
1523 rc = ethtool_set_coalesce(dev, useraddr);
1524 break;
1525 case ETHTOOL_GRINGPARAM:
1526 rc = ethtool_get_ringparam(dev, useraddr);
1527 break;
1528 case ETHTOOL_SRINGPARAM:
1529 rc = ethtool_set_ringparam(dev, useraddr);
1530 break;
1531 case ETHTOOL_GPAUSEPARAM:
1532 rc = ethtool_get_pauseparam(dev, useraddr);
1533 break;
1534 case ETHTOOL_SPAUSEPARAM:
1535 rc = ethtool_set_pauseparam(dev, useraddr);
1536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 case ETHTOOL_TEST:
1538 rc = ethtool_self_test(dev, useraddr);
1539 break;
1540 case ETHTOOL_GSTRINGS:
1541 rc = ethtool_get_strings(dev, useraddr);
1542 break;
1543 case ETHTOOL_PHYS_ID:
1544 rc = ethtool_phys_id(dev, useraddr);
1545 break;
1546 case ETHTOOL_GSTATS:
1547 rc = ethtool_get_stats(dev, useraddr);
1548 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001549 case ETHTOOL_GPERMADDR:
1550 rc = ethtool_get_perm_addr(dev, useraddr);
1551 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001552 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001553 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001554 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001555 break;
1556 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001557 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001558 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001559 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001560 rc = ethtool_get_value(dev, useraddr, ethcmd,
1561 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001562 break;
1563 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001564 rc = ethtool_set_value(dev, useraddr,
1565 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001566 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001567 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001568 case ETHTOOL_GRXRINGS:
1569 case ETHTOOL_GRXCLSRLCNT:
1570 case ETHTOOL_GRXCLSRULE:
1571 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001572 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001573 break;
1574 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001575 case ETHTOOL_SRXCLSRLDEL:
1576 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001577 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001578 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001579 case ETHTOOL_FLASHDEV:
1580 rc = ethtool_flash_device(dev, useraddr);
1581 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001582 case ETHTOOL_RESET:
1583 rc = ethtool_reset(dev, useraddr);
1584 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001585 case ETHTOOL_GSSET_INFO:
1586 rc = ethtool_get_sset_info(dev, useraddr);
1587 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001588 case ETHTOOL_GRXFHINDIR:
1589 rc = ethtool_get_rxfh_indir(dev, useraddr);
1590 break;
1591 case ETHTOOL_SRXFHINDIR:
1592 rc = ethtool_set_rxfh_indir(dev, useraddr);
1593 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001594 case ETHTOOL_GFEATURES:
1595 rc = ethtool_get_features(dev, useraddr);
1596 break;
1597 case ETHTOOL_SFEATURES:
1598 rc = ethtool_set_features(dev, useraddr);
1599 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001600 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001601 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001602 case ETHTOOL_GSG:
1603 case ETHTOOL_GTSO:
1604 case ETHTOOL_GUFO:
1605 case ETHTOOL_GGSO:
1606 case ETHTOOL_GGRO:
1607 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1608 break;
1609 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001610 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001611 case ETHTOOL_SSG:
1612 case ETHTOOL_STSO:
1613 case ETHTOOL_SUFO:
1614 case ETHTOOL_SGSO:
1615 case ETHTOOL_SGRO:
1616 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1617 break;
amit salecha8b5933c32011-04-07 01:58:42 +00001618 case ETHTOOL_GCHANNELS:
1619 rc = ethtool_get_channels(dev, useraddr);
1620 break;
1621 case ETHTOOL_SCHANNELS:
1622 rc = ethtool_set_channels(dev, useraddr);
1623 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001624 case ETHTOOL_SET_DUMP:
1625 rc = ethtool_set_dump(dev, useraddr);
1626 break;
1627 case ETHTOOL_GET_DUMP_FLAG:
1628 rc = ethtool_get_dump_flag(dev, useraddr);
1629 break;
1630 case ETHTOOL_GET_DUMP_DATA:
1631 rc = ethtool_get_dump_data(dev, useraddr);
1632 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001633 case ETHTOOL_GET_TS_INFO:
1634 rc = ethtool_get_ts_info(dev, useraddr);
1635 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001636 case ETHTOOL_GMODULEINFO:
1637 rc = ethtool_get_module_info(dev, useraddr);
1638 break;
1639 case ETHTOOL_GMODULEEEPROM:
1640 rc = ethtool_get_module_eeprom(dev, useraddr);
1641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001643 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001645
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001646 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001648
1649 if (old_features != dev->features)
1650 netdev_features_change(dev);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}