blob: 3e9b2c3e30f063cb3e59545f2af31176ea7b0151 [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",
63 [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
64
65 [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
66 [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
67 [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",
Michał Mirosław9d921542011-11-15 15:29:55 +000081
82 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
83 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
84 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
85 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
86 [NETIF_F_RXHASH_BIT] = "rx-hashing",
87 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
88 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
89 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000090 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000091 [NETIF_F_RXALL_BIT] = "rx-all",
Michał Mirosław9d921542011-11-15 15:29:55 +000092};
93
Michał Mirosław5455c692011-02-15 16:59:17 +000094static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
95{
96 struct ethtool_gfeatures cmd = {
97 .cmd = ETHTOOL_GFEATURES,
98 .size = ETHTOOL_DEV_FEATURE_WORDS,
99 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000100 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000101 u32 __user *sizeaddr;
102 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000103 int i;
104
105 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000106 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000107
108 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000109 features[i].available = (u32)(dev->hw_features >> (32 * i));
110 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
111 features[i].active = (u32)(dev->features >> (32 * i));
112 features[i].never_changed =
113 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000114 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000115
116 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
117 if (get_user(copy_size, sizeaddr))
118 return -EFAULT;
119
120 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
121 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
122
123 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
124 return -EFAULT;
125 useraddr += sizeof(cmd);
126 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
127 return -EFAULT;
128
129 return 0;
130}
131
132static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
133{
134 struct ethtool_sfeatures cmd;
135 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000136 netdev_features_t wanted = 0, valid = 0;
137 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000138
139 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
140 return -EFAULT;
141 useraddr += sizeof(cmd);
142
143 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
144 return -EINVAL;
145
146 if (copy_from_user(features, useraddr, sizeof(features)))
147 return -EFAULT;
148
Michał Mirosław475414f2011-11-15 15:29:55 +0000149 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000150 valid |= (netdev_features_t)features[i].valid << (32 * i);
151 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000152 }
153
154 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000155 return -EINVAL;
156
Michał Mirosław475414f2011-11-15 15:29:55 +0000157 if (valid & ~dev->hw_features) {
158 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000159 ret |= ETHTOOL_F_UNSUPPORTED;
160 }
161
Michał Mirosław475414f2011-11-15 15:29:55 +0000162 dev->wanted_features &= ~valid;
163 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700164 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000165
Michał Mirosław475414f2011-11-15 15:29:55 +0000166 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000167 ret |= ETHTOOL_F_WISH;
168
169 return ret;
170}
171
Michał Mirosław340ae162011-02-15 16:59:16 +0000172static int __ethtool_get_sset_count(struct net_device *dev, int sset)
173{
174 const struct ethtool_ops *ops = dev->ethtool_ops;
175
Michał Mirosław5455c692011-02-15 16:59:17 +0000176 if (sset == ETH_SS_FEATURES)
177 return ARRAY_SIZE(netdev_features_strings);
178
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000179 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000180 return ops->get_sset_count(dev, sset);
181 else
182 return -EOPNOTSUPP;
183}
184
185static void __ethtool_get_strings(struct net_device *dev,
186 u32 stringset, u8 *data)
187{
188 const struct ethtool_ops *ops = dev->ethtool_ops;
189
Michał Mirosław5455c692011-02-15 16:59:17 +0000190 if (stringset == ETH_SS_FEATURES)
191 memcpy(data, netdev_features_strings,
192 sizeof(netdev_features_strings));
193 else
194 /* ops->get_strings is valid because checked earlier */
195 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000196}
197
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000198static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000199{
200 /* feature masks of legacy discrete ethtool ops */
201
202 switch (eth_cmd) {
203 case ETHTOOL_GTXCSUM:
204 case ETHTOOL_STXCSUM:
205 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000206 case ETHTOOL_GRXCSUM:
207 case ETHTOOL_SRXCSUM:
208 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000209 case ETHTOOL_GSG:
210 case ETHTOOL_SSG:
211 return NETIF_F_SG;
212 case ETHTOOL_GTSO:
213 case ETHTOOL_STSO:
214 return NETIF_F_ALL_TSO;
215 case ETHTOOL_GUFO:
216 case ETHTOOL_SUFO:
217 return NETIF_F_UFO;
218 case ETHTOOL_GGSO:
219 case ETHTOOL_SGSO:
220 return NETIF_F_GSO;
221 case ETHTOOL_GGRO:
222 case ETHTOOL_SGRO:
223 return NETIF_F_GRO;
224 default:
225 BUG();
226 }
227}
228
Michał Mirosław0a417702011-02-15 16:59:17 +0000229static int ethtool_get_one_feature(struct net_device *dev,
230 char __user *useraddr, u32 ethcmd)
231{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000232 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000233 struct ethtool_value edata = {
234 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000235 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000236 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000237
Michał Mirosław0a417702011-02-15 16:59:17 +0000238 if (copy_to_user(useraddr, &edata, sizeof(edata)))
239 return -EFAULT;
240 return 0;
241}
242
Michał Mirosław0a417702011-02-15 16:59:17 +0000243static int ethtool_set_one_feature(struct net_device *dev,
244 void __user *useraddr, u32 ethcmd)
245{
246 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000247 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000248
249 if (copy_from_user(&edata, useraddr, sizeof(edata)))
250 return -EFAULT;
251
Michał Mirosław86794882011-02-15 16:59:17 +0000252 mask = ethtool_get_feature_mask(ethcmd);
253 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000254 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000255 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000256
257 if (edata.data)
258 dev->wanted_features |= mask;
259 else
260 dev->wanted_features &= ~mask;
261
262 __netdev_update_features(dev);
263
264 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000265}
266
Michał Mirosław02b3a552011-11-15 15:29:55 +0000267#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
268 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
269#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
270 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000271
272static u32 __ethtool_get_flags(struct net_device *dev)
273{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000274 u32 flags = 0;
275
276 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
277 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
278 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
279 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
280 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
281
282 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000283}
284
285static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000286{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000287 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000288
Michał Mirosław02b3a552011-11-15 15:29:55 +0000289 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000290 return -EINVAL;
291
Michał Mirosław02b3a552011-11-15 15:29:55 +0000292 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
293 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
294 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
295 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
296 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
297
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000298 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000299 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000300 if (changed & ~dev->hw_features)
301 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
302
303 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000304 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000305
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700306 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000307
308 return 0;
309}
310
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000311int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000313 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000315 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EOPNOTSUPP;
317
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000318 memset(cmd, 0, sizeof(struct ethtool_cmd));
319 cmd->cmd = ETHTOOL_GSET;
320 return dev->ethtool_ops->get_settings(dev, cmd);
321}
322EXPORT_SYMBOL(__ethtool_get_settings);
323
324static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
325{
326 int err;
327 struct ethtool_cmd cmd;
328
329 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (err < 0)
331 return err;
332
333 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
334 return -EFAULT;
335 return 0;
336}
337
338static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
339{
340 struct ethtool_cmd cmd;
341
342 if (!dev->ethtool_ops->set_settings)
343 return -EOPNOTSUPP;
344
345 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
346 return -EFAULT;
347
348 return dev->ethtool_ops->set_settings(dev, &cmd);
349}
350
chavey97f8aef2010-04-07 21:54:42 -0700351static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
352 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700355 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 memset(&info, 0, sizeof(info));
358 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000359 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700360 ops->get_drvinfo(dev, &info);
361 } else if (dev->dev.parent && dev->dev.parent->driver) {
362 strlcpy(info.bus_info, dev_name(dev->dev.parent),
363 sizeof(info.bus_info));
364 strlcpy(info.driver, dev->dev.parent->driver->name,
365 sizeof(info.driver));
366 } else {
367 return -EOPNOTSUPP;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jeff Garzik723b2f52010-03-03 22:51:50 +0000370 /*
371 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000372 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000373 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000374 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700375 int rc;
376
377 rc = ops->get_sset_count(dev, ETH_SS_TEST);
378 if (rc >= 0)
379 info.testinfo_len = rc;
380 rc = ops->get_sset_count(dev, ETH_SS_STATS);
381 if (rc >= 0)
382 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700383 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
384 if (rc >= 0)
385 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700386 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000387 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000389 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 info.eedump_len = ops->get_eeprom_len(dev);
391
392 if (copy_to_user(useraddr, &info, sizeof(info)))
393 return -EFAULT;
394 return 0;
395}
396
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800397static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700398 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000399{
400 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000401 u64 sset_mask;
402 int i, idx = 0, n_bits = 0, ret, rc;
403 u32 *info_buf = NULL;
404
Jeff Garzik723b2f52010-03-03 22:51:50 +0000405 if (copy_from_user(&info, useraddr, sizeof(info)))
406 return -EFAULT;
407
408 /* store copy of mask, because we zero struct later on */
409 sset_mask = info.sset_mask;
410 if (!sset_mask)
411 return 0;
412
413 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000414 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000415
416 memset(&info, 0, sizeof(info));
417 info.cmd = ETHTOOL_GSSET_INFO;
418
419 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
420 if (!info_buf)
421 return -ENOMEM;
422
423 /*
424 * fill return buffer based on input bitmask and successful
425 * get_sset_count return
426 */
427 for (i = 0; i < 64; i++) {
428 if (!(sset_mask & (1ULL << i)))
429 continue;
430
Michał Mirosław340ae162011-02-15 16:59:16 +0000431 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000432 if (rc >= 0) {
433 info.sset_mask |= (1ULL << i);
434 info_buf[idx++] = rc;
435 }
436 }
437
438 ret = -EFAULT;
439 if (copy_to_user(useraddr, &info, sizeof(info)))
440 goto out;
441
442 useraddr += offsetof(struct ethtool_sset_info, data);
443 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
444 goto out;
445
446 ret = 0;
447
448out:
449 kfree(info_buf);
450 return ret;
451}
452
chavey97f8aef2010-04-07 21:54:42 -0700453static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000454 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700455{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000456 struct ethtool_rxnfc info;
457 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000458 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700459
Santwona Behera59089d82009-02-20 00:58:13 -0800460 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700461 return -EOPNOTSUPP;
462
Ben Hutchingsbf988432010-06-28 08:45:58 +0000463 /* struct ethtool_rxnfc was originally defined for
464 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
465 * members. User-space might still be using that
466 * definition. */
467 if (cmd == ETHTOOL_SRXFH)
468 info_size = (offsetof(struct ethtool_rxnfc, data) +
469 sizeof(info.data));
470
471 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700472 return -EFAULT;
473
Ben Hutchings55664f32012-01-03 12:04:51 +0000474 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
475 if (rc)
476 return rc;
477
478 if (cmd == ETHTOOL_SRXCLSRLINS &&
479 copy_to_user(useraddr, &info, info_size))
480 return -EFAULT;
481
482 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700483}
484
chavey97f8aef2010-04-07 21:54:42 -0700485static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000486 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700487{
488 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000489 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800490 const struct ethtool_ops *ops = dev->ethtool_ops;
491 int ret;
492 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700493
Santwona Behera59089d82009-02-20 00:58:13 -0800494 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700495 return -EOPNOTSUPP;
496
Ben Hutchingsbf988432010-06-28 08:45:58 +0000497 /* struct ethtool_rxnfc was originally defined for
498 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
499 * members. User-space might still be using that
500 * definition. */
501 if (cmd == ETHTOOL_GRXFH)
502 info_size = (offsetof(struct ethtool_rxnfc, data) +
503 sizeof(info.data));
504
505 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700506 return -EFAULT;
507
Santwona Behera59089d82009-02-20 00:58:13 -0800508 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
509 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000510 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000511 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000512 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800513 if (!rule_buf)
514 return -ENOMEM;
515 }
516 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700517
Santwona Behera59089d82009-02-20 00:58:13 -0800518 ret = ops->get_rxnfc(dev, &info, rule_buf);
519 if (ret < 0)
520 goto err_out;
521
522 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000523 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800524 goto err_out;
525
526 if (rule_buf) {
527 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
528 if (copy_to_user(useraddr, rule_buf,
529 info.rule_cnt * sizeof(u32)))
530 goto err_out;
531 }
532 ret = 0;
533
534err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700535 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800536
537 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700538}
539
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000540static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
541 void __user *useraddr)
542{
Ben Hutchings7850f632011-12-15 13:55:01 +0000543 u32 user_size, dev_size;
544 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000545 int ret;
546
Ben Hutchings7850f632011-12-15 13:55:01 +0000547 if (!dev->ethtool_ops->get_rxfh_indir_size ||
548 !dev->ethtool_ops->get_rxfh_indir)
549 return -EOPNOTSUPP;
550 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
551 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000552 return -EOPNOTSUPP;
553
Ben Hutchings7850f632011-12-15 13:55:01 +0000554 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000555 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000556 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000557 return -EFAULT;
558
Ben Hutchings7850f632011-12-15 13:55:01 +0000559 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
560 &dev_size, sizeof(dev_size)))
561 return -EFAULT;
562
563 /* If the user buffer size is 0, this is just a query for the
564 * device table size. Otherwise, if it's smaller than the
565 * device table size it's an error.
566 */
567 if (user_size < dev_size)
568 return user_size == 0 ? 0 : -EINVAL;
569
570 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000571 if (!indir)
572 return -ENOMEM;
573
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000574 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
575 if (ret)
576 goto out;
577
Ben Hutchings7850f632011-12-15 13:55:01 +0000578 if (copy_to_user(useraddr +
579 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
580 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000581 ret = -EFAULT;
582
583out:
584 kfree(indir);
585 return ret;
586}
587
588static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
589 void __user *useraddr)
590{
Ben Hutchings7850f632011-12-15 13:55:01 +0000591 struct ethtool_rxnfc rx_rings;
592 u32 user_size, dev_size, i;
593 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000594 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000595 int ret;
596
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000597 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
598 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000599 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000600
601 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000602 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000603 return -EOPNOTSUPP;
604
Ben Hutchings7850f632011-12-15 13:55:01 +0000605 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000606 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000607 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000608 return -EFAULT;
609
Ben Hutchings278bc422011-12-15 13:56:49 +0000610 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000611 return -EINVAL;
612
613 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000614 if (!indir)
615 return -ENOMEM;
616
Ben Hutchings7850f632011-12-15 13:55:01 +0000617 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000618 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000619 if (ret)
620 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000621
622 if (user_size == 0) {
623 for (i = 0; i < dev_size; i++)
624 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
625 } else {
626 if (copy_from_user(indir,
627 useraddr +
628 offsetof(struct ethtool_rxfh_indir,
629 ring_index[0]),
630 dev_size * sizeof(indir[0]))) {
631 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000632 goto out;
633 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000634
635 /* Validate ring indices */
636 for (i = 0; i < dev_size; i++) {
637 if (indir[i] >= rx_rings.data) {
638 ret = -EINVAL;
639 goto out;
640 }
641 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000642 }
643
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000644 ret = ops->set_rxfh_indir(dev, indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000645
646out:
647 kfree(indir);
648 return ret;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
652{
653 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700654 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 void *regbuf;
656 int reglen, ret;
657
658 if (!ops->get_regs || !ops->get_regs_len)
659 return -EOPNOTSUPP;
660
661 if (copy_from_user(&regs, useraddr, sizeof(regs)))
662 return -EFAULT;
663
664 reglen = ops->get_regs_len(dev);
665 if (regs.len > reglen)
666 regs.len = reglen;
667
Eugene Teob7c7d012011-01-24 21:05:17 -0800668 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700669 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -ENOMEM;
671
672 ops->get_regs(dev, &regs, regbuf);
673
674 ret = -EFAULT;
675 if (copy_to_user(useraddr, &regs, sizeof(regs)))
676 goto out;
677 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700678 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto out;
680 ret = 0;
681
682 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000683 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return ret;
685}
686
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000687static int ethtool_reset(struct net_device *dev, char __user *useraddr)
688{
689 struct ethtool_value reset;
690 int ret;
691
692 if (!dev->ethtool_ops->reset)
693 return -EOPNOTSUPP;
694
695 if (copy_from_user(&reset, useraddr, sizeof(reset)))
696 return -EFAULT;
697
698 ret = dev->ethtool_ops->reset(dev, &reset.data);
699 if (ret)
700 return ret;
701
702 if (copy_to_user(useraddr, &reset, sizeof(reset)))
703 return -EFAULT;
704 return 0;
705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
708{
Roland Dreier8e557422010-02-11 12:14:23 -0800709 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 if (!dev->ethtool_ops->get_wol)
712 return -EOPNOTSUPP;
713
714 dev->ethtool_ops->get_wol(dev, &wol);
715
716 if (copy_to_user(useraddr, &wol, sizeof(wol)))
717 return -EFAULT;
718 return 0;
719}
720
721static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
722{
723 struct ethtool_wolinfo wol;
724
725 if (!dev->ethtool_ops->set_wol)
726 return -EOPNOTSUPP;
727
728 if (copy_from_user(&wol, useraddr, sizeof(wol)))
729 return -EFAULT;
730
731 return dev->ethtool_ops->set_wol(dev, &wol);
732}
733
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000734static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
735{
736 struct ethtool_eee edata;
737 int rc;
738
739 if (!dev->ethtool_ops->get_eee)
740 return -EOPNOTSUPP;
741
742 memset(&edata, 0, sizeof(struct ethtool_eee));
743 edata.cmd = ETHTOOL_GEEE;
744 rc = dev->ethtool_ops->get_eee(dev, &edata);
745
746 if (rc)
747 return rc;
748
749 if (copy_to_user(useraddr, &edata, sizeof(edata)))
750 return -EFAULT;
751
752 return 0;
753}
754
755static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
756{
757 struct ethtool_eee edata;
758
759 if (!dev->ethtool_ops->set_eee)
760 return -EOPNOTSUPP;
761
762 if (copy_from_user(&edata, useraddr, sizeof(edata)))
763 return -EFAULT;
764
765 return dev->ethtool_ops->set_eee(dev, &edata);
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768static int ethtool_nway_reset(struct net_device *dev)
769{
770 if (!dev->ethtool_ops->nway_reset)
771 return -EOPNOTSUPP;
772
773 return dev->ethtool_ops->nway_reset(dev);
774}
775
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000776static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
777{
778 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
779
780 if (!dev->ethtool_ops->get_link)
781 return -EOPNOTSUPP;
782
783 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
784
785 if (copy_to_user(useraddr, &edata, sizeof(edata)))
786 return -EFAULT;
787 return 0;
788}
789
Ben Hutchings081d0942012-04-12 00:42:12 +0000790static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
791 int (*getter)(struct net_device *,
792 struct ethtool_eeprom *, u8 *),
793 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700796 void __user *userbuf = useraddr + sizeof(eeprom);
797 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700799 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
802 return -EFAULT;
803
804 /* Check for wrap and zero */
805 if (eeprom.offset + eeprom.len <= eeprom.offset)
806 return -EINVAL;
807
808 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +0000809 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EINVAL;
811
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700812 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!data)
814 return -ENOMEM;
815
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700816 bytes_remaining = eeprom.len;
817 while (bytes_remaining > 0) {
818 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Ben Hutchings081d0942012-04-12 00:42:12 +0000820 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700821 if (ret)
822 break;
823 if (copy_to_user(userbuf, data, eeprom.len)) {
824 ret = -EFAULT;
825 break;
826 }
827 userbuf += eeprom.len;
828 eeprom.offset += eeprom.len;
829 bytes_remaining -= eeprom.len;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700832 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
833 eeprom.offset -= eeprom.len;
834 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
835 ret = -EFAULT;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 kfree(data);
838 return ret;
839}
840
Ben Hutchings081d0942012-04-12 00:42:12 +0000841static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
842{
843 const struct ethtool_ops *ops = dev->ethtool_ops;
844
845 if (!ops->get_eeprom || !ops->get_eeprom_len)
846 return -EOPNOTSUPP;
847
848 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
849 ops->get_eeprom_len(dev));
850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
853{
854 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700855 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700856 void __user *userbuf = useraddr + sizeof(eeprom);
857 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700859 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (!ops->set_eeprom || !ops->get_eeprom_len)
862 return -EOPNOTSUPP;
863
864 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
865 return -EFAULT;
866
867 /* Check for wrap and zero */
868 if (eeprom.offset + eeprom.len <= eeprom.offset)
869 return -EINVAL;
870
871 /* Check for exceeding total eeprom len */
872 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
873 return -EINVAL;
874
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700875 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!data)
877 return -ENOMEM;
878
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700879 bytes_remaining = eeprom.len;
880 while (bytes_remaining > 0) {
881 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700883 if (copy_from_user(data, userbuf, eeprom.len)) {
884 ret = -EFAULT;
885 break;
886 }
887 ret = ops->set_eeprom(dev, &eeprom, data);
888 if (ret)
889 break;
890 userbuf += eeprom.len;
891 eeprom.offset += eeprom.len;
892 bytes_remaining -= eeprom.len;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 kfree(data);
896 return ret;
897}
898
chavey97f8aef2010-04-07 21:54:42 -0700899static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
900 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Roland Dreier8e557422010-02-11 12:14:23 -0800902 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (!dev->ethtool_ops->get_coalesce)
905 return -EOPNOTSUPP;
906
907 dev->ethtool_ops->get_coalesce(dev, &coalesce);
908
909 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
910 return -EFAULT;
911 return 0;
912}
913
chavey97f8aef2010-04-07 21:54:42 -0700914static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
915 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct ethtool_coalesce coalesce;
918
David S. Millerfa04ae52005-06-06 15:07:19 -0700919 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EOPNOTSUPP;
921
922 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
923 return -EFAULT;
924
925 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
926}
927
928static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
929{
Roland Dreier8e557422010-02-11 12:14:23 -0800930 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!dev->ethtool_ops->get_ringparam)
933 return -EOPNOTSUPP;
934
935 dev->ethtool_ops->get_ringparam(dev, &ringparam);
936
937 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
938 return -EFAULT;
939 return 0;
940}
941
942static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
943{
944 struct ethtool_ringparam ringparam;
945
946 if (!dev->ethtool_ops->set_ringparam)
947 return -EOPNOTSUPP;
948
949 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
950 return -EFAULT;
951
952 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
953}
954
amit salecha8b5933c2011-04-07 01:58:42 +0000955static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
956 void __user *useraddr)
957{
958 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
959
960 if (!dev->ethtool_ops->get_channels)
961 return -EOPNOTSUPP;
962
963 dev->ethtool_ops->get_channels(dev, &channels);
964
965 if (copy_to_user(useraddr, &channels, sizeof(channels)))
966 return -EFAULT;
967 return 0;
968}
969
970static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
971 void __user *useraddr)
972{
973 struct ethtool_channels channels;
974
975 if (!dev->ethtool_ops->set_channels)
976 return -EOPNOTSUPP;
977
978 if (copy_from_user(&channels, useraddr, sizeof(channels)))
979 return -EFAULT;
980
981 return dev->ethtool_ops->set_channels(dev, &channels);
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
985{
986 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
987
988 if (!dev->ethtool_ops->get_pauseparam)
989 return -EOPNOTSUPP;
990
991 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
992
993 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
994 return -EFAULT;
995 return 0;
996}
997
998static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
999{
1000 struct ethtool_pauseparam pauseparam;
1001
Jeff Garzike1b90c42006-07-17 12:54:40 -04001002 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -EOPNOTSUPP;
1004
1005 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1006 return -EFAULT;
1007
1008 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1012{
1013 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001014 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001016 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001018 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001019 return -EOPNOTSUPP;
1020
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001021 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001022 if (test_len < 0)
1023 return test_len;
1024 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (copy_from_user(&test, useraddr, sizeof(test)))
1027 return -EFAULT;
1028
Jeff Garzikff03d492007-08-15 16:01:08 -07001029 test.len = test_len;
1030 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!data)
1032 return -ENOMEM;
1033
1034 ops->self_test(dev, &test, data);
1035
1036 ret = -EFAULT;
1037 if (copy_to_user(useraddr, &test, sizeof(test)))
1038 goto out;
1039 useraddr += sizeof(test);
1040 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1041 goto out;
1042 ret = 0;
1043
1044 out:
1045 kfree(data);
1046 return ret;
1047}
1048
1049static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1050{
1051 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 u8 *data;
1053 int ret;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1056 return -EFAULT;
1057
Michał Mirosław340ae162011-02-15 16:59:16 +00001058 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001059 if (ret < 0)
1060 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001061
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001062 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1065 if (!data)
1066 return -ENOMEM;
1067
Michał Mirosław340ae162011-02-15 16:59:16 +00001068 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 ret = -EFAULT;
1071 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1072 goto out;
1073 useraddr += sizeof(gstrings);
1074 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1075 goto out;
1076 ret = 0;
1077
Michał Mirosław340ae162011-02-15 16:59:16 +00001078out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 kfree(data);
1080 return ret;
1081}
1082
1083static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1084{
1085 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001086 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001087 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001088 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001090 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -EOPNOTSUPP;
1092
Ben Hutchings68f512f2011-04-02 00:35:15 +01001093 if (busy)
1094 return -EBUSY;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (copy_from_user(&id, useraddr, sizeof(id)))
1097 return -EFAULT;
1098
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001099 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001100 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001101 return rc;
1102
1103 /* Drop the RTNL lock while waiting, but prevent reentry or
1104 * removal of the device.
1105 */
1106 busy = true;
1107 dev_hold(dev);
1108 rtnl_unlock();
1109
1110 if (rc == 0) {
1111 /* Driver will handle this itself */
1112 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001113 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001114 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001115 /* Driver expects to be called at twice the frequency in rc */
1116 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001117
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001118 /* Count down seconds */
1119 do {
1120 /* Count down iterations per second */
1121 i = n;
1122 do {
1123 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001124 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001125 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1126 rtnl_unlock();
1127 if (rc)
1128 break;
1129 schedule_timeout_interruptible(interval);
1130 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001131 } while (!signal_pending(current) &&
1132 (id.data == 0 || --id.data != 0));
1133 }
1134
1135 rtnl_lock();
1136 dev_put(dev);
1137 busy = false;
1138
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001139 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001140 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1144{
1145 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001146 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001148 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001150 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001151 return -EOPNOTSUPP;
1152
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001153 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001154 if (n_stats < 0)
1155 return n_stats;
1156 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1159 return -EFAULT;
1160
Jeff Garzikff03d492007-08-15 16:01:08 -07001161 stats.n_stats = n_stats;
1162 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!data)
1164 return -ENOMEM;
1165
1166 ops->get_ethtool_stats(dev, &stats, data);
1167
1168 ret = -EFAULT;
1169 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1170 goto out;
1171 useraddr += sizeof(stats);
1172 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1173 goto out;
1174 ret = 0;
1175
1176 out:
1177 kfree(data);
1178 return ret;
1179}
1180
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001181static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001182{
1183 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001184
Matthew Wilcox313674a2007-07-31 14:00:29 -07001185 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001186 return -EFAULT;
1187
Matthew Wilcox313674a2007-07-31 14:00:29 -07001188 if (epaddr.size < dev->addr_len)
1189 return -ETOOSMALL;
1190 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001191
Jon Wetzela6f9a702005-08-20 17:15:54 -07001192 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001193 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001194 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001195 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1196 return -EFAULT;
1197 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001198}
1199
Jeff Garzik13c99b22007-08-15 16:01:56 -07001200static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1201 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001202{
Roland Dreier8e557422010-02-11 12:14:23 -08001203 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001204
Jeff Garzik13c99b22007-08-15 16:01:56 -07001205 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001206 return -EOPNOTSUPP;
1207
Jeff Garzik13c99b22007-08-15 16:01:56 -07001208 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001209
1210 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1211 return -EFAULT;
1212 return 0;
1213}
1214
Jeff Garzik13c99b22007-08-15 16:01:56 -07001215static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1216 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001217{
1218 struct ethtool_value edata;
1219
Jeff Garzik13c99b22007-08-15 16:01:56 -07001220 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001221 return -EOPNOTSUPP;
1222
1223 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1224 return -EFAULT;
1225
Jeff Garzik13c99b22007-08-15 16:01:56 -07001226 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001227 return 0;
1228}
1229
Jeff Garzik13c99b22007-08-15 16:01:56 -07001230static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1231 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001232{
1233 struct ethtool_value edata;
1234
Jeff Garzik13c99b22007-08-15 16:01:56 -07001235 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001236 return -EOPNOTSUPP;
1237
1238 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1239 return -EFAULT;
1240
Jeff Garzik13c99b22007-08-15 16:01:56 -07001241 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001242}
1243
chavey97f8aef2010-04-07 21:54:42 -07001244static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1245 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001246{
1247 struct ethtool_flash efl;
1248
1249 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1250 return -EFAULT;
1251
1252 if (!dev->ethtool_ops->flash_device)
1253 return -EOPNOTSUPP;
1254
Ben Hutchings786f5282012-02-01 09:32:25 +00001255 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1256
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001257 return dev->ethtool_ops->flash_device(dev, &efl);
1258}
1259
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001260static int ethtool_set_dump(struct net_device *dev,
1261 void __user *useraddr)
1262{
1263 struct ethtool_dump dump;
1264
1265 if (!dev->ethtool_ops->set_dump)
1266 return -EOPNOTSUPP;
1267
1268 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1269 return -EFAULT;
1270
1271 return dev->ethtool_ops->set_dump(dev, &dump);
1272}
1273
1274static int ethtool_get_dump_flag(struct net_device *dev,
1275 void __user *useraddr)
1276{
1277 int ret;
1278 struct ethtool_dump dump;
1279 const struct ethtool_ops *ops = dev->ethtool_ops;
1280
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001281 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001282 return -EOPNOTSUPP;
1283
1284 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1285 return -EFAULT;
1286
1287 ret = ops->get_dump_flag(dev, &dump);
1288 if (ret)
1289 return ret;
1290
1291 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1292 return -EFAULT;
1293 return 0;
1294}
1295
1296static int ethtool_get_dump_data(struct net_device *dev,
1297 void __user *useraddr)
1298{
1299 int ret;
1300 __u32 len;
1301 struct ethtool_dump dump, tmp;
1302 const struct ethtool_ops *ops = dev->ethtool_ops;
1303 void *data = NULL;
1304
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001305 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001306 return -EOPNOTSUPP;
1307
1308 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1309 return -EFAULT;
1310
1311 memset(&tmp, 0, sizeof(tmp));
1312 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1313 ret = ops->get_dump_flag(dev, &tmp);
1314 if (ret)
1315 return ret;
1316
1317 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1318 if (!len)
1319 return -EFAULT;
1320
1321 data = vzalloc(tmp.len);
1322 if (!data)
1323 return -ENOMEM;
1324 ret = ops->get_dump_data(dev, &dump, data);
1325 if (ret)
1326 goto out;
1327
1328 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1329 ret = -EFAULT;
1330 goto out;
1331 }
1332 useraddr += offsetof(struct ethtool_dump, data);
1333 if (copy_to_user(useraddr, data, len))
1334 ret = -EFAULT;
1335out:
1336 vfree(data);
1337 return ret;
1338}
1339
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001340static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1341{
1342 int err = 0;
1343 struct ethtool_ts_info info;
1344 const struct ethtool_ops *ops = dev->ethtool_ops;
1345 struct phy_device *phydev = dev->phydev;
1346
1347 memset(&info, 0, sizeof(info));
1348 info.cmd = ETHTOOL_GET_TS_INFO;
1349
1350 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001351 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001352 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001353 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001354 } else {
1355 info.so_timestamping =
1356 SOF_TIMESTAMPING_RX_SOFTWARE |
1357 SOF_TIMESTAMPING_SOFTWARE;
1358 info.phc_index = -1;
1359 }
1360
1361 if (err)
1362 return err;
1363
1364 if (copy_to_user(useraddr, &info, sizeof(info)))
1365 err = -EFAULT;
1366
1367 return err;
1368}
1369
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001370static int ethtool_get_module_info(struct net_device *dev,
1371 void __user *useraddr)
1372{
1373 int ret;
1374 struct ethtool_modinfo modinfo;
1375 const struct ethtool_ops *ops = dev->ethtool_ops;
1376
1377 if (!ops->get_module_info)
1378 return -EOPNOTSUPP;
1379
1380 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1381 return -EFAULT;
1382
1383 ret = ops->get_module_info(dev, &modinfo);
1384 if (ret)
1385 return ret;
1386
1387 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1388 return -EFAULT;
1389
1390 return 0;
1391}
1392
1393static int ethtool_get_module_eeprom(struct net_device *dev,
1394 void __user *useraddr)
1395{
1396 int ret;
1397 struct ethtool_modinfo modinfo;
1398 const struct ethtool_ops *ops = dev->ethtool_ops;
1399
1400 if (!ops->get_module_info || !ops->get_module_eeprom)
1401 return -EOPNOTSUPP;
1402
1403 ret = ops->get_module_info(dev, &modinfo);
1404 if (ret)
1405 return ret;
1406
1407 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1408 modinfo.eeprom_len);
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/* The main entry point in this file. Called from net/core/dev.c */
1412
Eric W. Biederman881d9662007-09-17 11:56:21 -07001413int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001415 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 void __user *useraddr = ifr->ifr_data;
1417 u32 ethcmd;
1418 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001419 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!dev || !netif_device_present(dev))
1422 return -ENODEV;
1423
chavey97f8aef2010-04-07 21:54:42 -07001424 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -EFAULT;
1426
Stephen Hemminger75f31232006-09-28 15:13:37 -07001427 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001428 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001429 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001430 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001431 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001432 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001433 case ETHTOOL_GCOALESCE:
1434 case ETHTOOL_GRINGPARAM:
1435 case ETHTOOL_GPAUSEPARAM:
1436 case ETHTOOL_GRXCSUM:
1437 case ETHTOOL_GTXCSUM:
1438 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001439 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001440 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001441 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001442 case ETHTOOL_GTSO:
1443 case ETHTOOL_GPERMADDR:
1444 case ETHTOOL_GUFO:
1445 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001446 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001447 case ETHTOOL_GFLAGS:
1448 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001449 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001450 case ETHTOOL_GRXRINGS:
1451 case ETHTOOL_GRXCLSRLCNT:
1452 case ETHTOOL_GRXCLSRULE:
1453 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001454 case ETHTOOL_GRXFHINDIR:
Michał Mirosław5455c692011-02-15 16:59:17 +00001455 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001456 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001457 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001458 case ETHTOOL_GEEE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001459 break;
1460 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001461 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001462 return -EPERM;
1463 }
1464
chavey97f8aef2010-04-07 21:54:42 -07001465 if (dev->ethtool_ops->begin) {
1466 rc = dev->ethtool_ops->begin(dev);
1467 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001469 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001470 old_features = dev->features;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 switch (ethcmd) {
1473 case ETHTOOL_GSET:
1474 rc = ethtool_get_settings(dev, useraddr);
1475 break;
1476 case ETHTOOL_SSET:
1477 rc = ethtool_set_settings(dev, useraddr);
1478 break;
1479 case ETHTOOL_GDRVINFO:
1480 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 break;
1482 case ETHTOOL_GREGS:
1483 rc = ethtool_get_regs(dev, useraddr);
1484 break;
1485 case ETHTOOL_GWOL:
1486 rc = ethtool_get_wol(dev, useraddr);
1487 break;
1488 case ETHTOOL_SWOL:
1489 rc = ethtool_set_wol(dev, useraddr);
1490 break;
1491 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001492 rc = ethtool_get_value(dev, useraddr, ethcmd,
1493 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001496 rc = ethtool_set_value_void(dev, useraddr,
1497 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001499 case ETHTOOL_GEEE:
1500 rc = ethtool_get_eee(dev, useraddr);
1501 break;
1502 case ETHTOOL_SEEE:
1503 rc = ethtool_set_eee(dev, useraddr);
1504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 case ETHTOOL_NWAY_RST:
1506 rc = ethtool_nway_reset(dev);
1507 break;
1508 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001509 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 break;
1511 case ETHTOOL_GEEPROM:
1512 rc = ethtool_get_eeprom(dev, useraddr);
1513 break;
1514 case ETHTOOL_SEEPROM:
1515 rc = ethtool_set_eeprom(dev, useraddr);
1516 break;
1517 case ETHTOOL_GCOALESCE:
1518 rc = ethtool_get_coalesce(dev, useraddr);
1519 break;
1520 case ETHTOOL_SCOALESCE:
1521 rc = ethtool_set_coalesce(dev, useraddr);
1522 break;
1523 case ETHTOOL_GRINGPARAM:
1524 rc = ethtool_get_ringparam(dev, useraddr);
1525 break;
1526 case ETHTOOL_SRINGPARAM:
1527 rc = ethtool_set_ringparam(dev, useraddr);
1528 break;
1529 case ETHTOOL_GPAUSEPARAM:
1530 rc = ethtool_get_pauseparam(dev, useraddr);
1531 break;
1532 case ETHTOOL_SPAUSEPARAM:
1533 rc = ethtool_set_pauseparam(dev, useraddr);
1534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 case ETHTOOL_TEST:
1536 rc = ethtool_self_test(dev, useraddr);
1537 break;
1538 case ETHTOOL_GSTRINGS:
1539 rc = ethtool_get_strings(dev, useraddr);
1540 break;
1541 case ETHTOOL_PHYS_ID:
1542 rc = ethtool_phys_id(dev, useraddr);
1543 break;
1544 case ETHTOOL_GSTATS:
1545 rc = ethtool_get_stats(dev, useraddr);
1546 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001547 case ETHTOOL_GPERMADDR:
1548 rc = ethtool_get_perm_addr(dev, useraddr);
1549 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001550 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001551 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001552 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001553 break;
1554 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001555 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001556 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001557 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001558 rc = ethtool_get_value(dev, useraddr, ethcmd,
1559 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001560 break;
1561 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001562 rc = ethtool_set_value(dev, useraddr,
1563 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001564 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001565 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001566 case ETHTOOL_GRXRINGS:
1567 case ETHTOOL_GRXCLSRLCNT:
1568 case ETHTOOL_GRXCLSRULE:
1569 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001570 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001571 break;
1572 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001573 case ETHTOOL_SRXCLSRLDEL:
1574 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001575 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001576 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001577 case ETHTOOL_FLASHDEV:
1578 rc = ethtool_flash_device(dev, useraddr);
1579 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001580 case ETHTOOL_RESET:
1581 rc = ethtool_reset(dev, useraddr);
1582 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001583 case ETHTOOL_GSSET_INFO:
1584 rc = ethtool_get_sset_info(dev, useraddr);
1585 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001586 case ETHTOOL_GRXFHINDIR:
1587 rc = ethtool_get_rxfh_indir(dev, useraddr);
1588 break;
1589 case ETHTOOL_SRXFHINDIR:
1590 rc = ethtool_set_rxfh_indir(dev, useraddr);
1591 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001592 case ETHTOOL_GFEATURES:
1593 rc = ethtool_get_features(dev, useraddr);
1594 break;
1595 case ETHTOOL_SFEATURES:
1596 rc = ethtool_set_features(dev, useraddr);
1597 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001598 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001599 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001600 case ETHTOOL_GSG:
1601 case ETHTOOL_GTSO:
1602 case ETHTOOL_GUFO:
1603 case ETHTOOL_GGSO:
1604 case ETHTOOL_GGRO:
1605 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1606 break;
1607 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001608 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001609 case ETHTOOL_SSG:
1610 case ETHTOOL_STSO:
1611 case ETHTOOL_SUFO:
1612 case ETHTOOL_SGSO:
1613 case ETHTOOL_SGRO:
1614 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1615 break;
amit salecha8b5933c2011-04-07 01:58:42 +00001616 case ETHTOOL_GCHANNELS:
1617 rc = ethtool_get_channels(dev, useraddr);
1618 break;
1619 case ETHTOOL_SCHANNELS:
1620 rc = ethtool_set_channels(dev, useraddr);
1621 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001622 case ETHTOOL_SET_DUMP:
1623 rc = ethtool_set_dump(dev, useraddr);
1624 break;
1625 case ETHTOOL_GET_DUMP_FLAG:
1626 rc = ethtool_get_dump_flag(dev, useraddr);
1627 break;
1628 case ETHTOOL_GET_DUMP_DATA:
1629 rc = ethtool_get_dump_data(dev, useraddr);
1630 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001631 case ETHTOOL_GET_TS_INFO:
1632 rc = ethtool_get_ts_info(dev, useraddr);
1633 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001634 case ETHTOOL_GMODULEINFO:
1635 rc = ethtool_get_module_info(dev, useraddr);
1636 break;
1637 case ETHTOOL_GMODULEEEPROM:
1638 rc = ethtool_get_module_eeprom(dev, useraddr);
1639 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001641 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001643
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001644 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001646
1647 if (old_features != dev->features)
1648 netdev_features_change(dev);
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}