blob: adc1351e6873aa826e71aa4ff7e96a9df23d74b4 [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",
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)
270#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
271 NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000272
273static u32 __ethtool_get_flags(struct net_device *dev)
274{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000275 u32 flags = 0;
276
277 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
278 if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
279 if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
280 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
281 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
282
283 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000284}
285
286static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000287{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000288 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000289
Michał Mirosław02b3a552011-11-15 15:29:55 +0000290 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000291 return -EINVAL;
292
Michał Mirosław02b3a552011-11-15 15:29:55 +0000293 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
294 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
295 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
296 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
297 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
298
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000299 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000300 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000301 if (changed & ~dev->hw_features)
302 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
303
304 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000305 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000306
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700307 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000308
309 return 0;
310}
311
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000312int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000314 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000316 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -EOPNOTSUPP;
318
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000319 memset(cmd, 0, sizeof(struct ethtool_cmd));
320 cmd->cmd = ETHTOOL_GSET;
321 return dev->ethtool_ops->get_settings(dev, cmd);
322}
323EXPORT_SYMBOL(__ethtool_get_settings);
324
325static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
326{
327 int err;
328 struct ethtool_cmd cmd;
329
330 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (err < 0)
332 return err;
333
334 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
335 return -EFAULT;
336 return 0;
337}
338
339static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
340{
341 struct ethtool_cmd cmd;
342
343 if (!dev->ethtool_ops->set_settings)
344 return -EOPNOTSUPP;
345
346 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
347 return -EFAULT;
348
349 return dev->ethtool_ops->set_settings(dev, &cmd);
350}
351
chavey97f8aef2010-04-07 21:54:42 -0700352static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
353 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700356 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 memset(&info, 0, sizeof(info));
359 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000360 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700361 ops->get_drvinfo(dev, &info);
362 } else if (dev->dev.parent && dev->dev.parent->driver) {
363 strlcpy(info.bus_info, dev_name(dev->dev.parent),
364 sizeof(info.bus_info));
365 strlcpy(info.driver, dev->dev.parent->driver->name,
366 sizeof(info.driver));
367 } else {
368 return -EOPNOTSUPP;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jeff Garzik723b2f52010-03-03 22:51:50 +0000371 /*
372 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000373 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000374 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000375 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700376 int rc;
377
378 rc = ops->get_sset_count(dev, ETH_SS_TEST);
379 if (rc >= 0)
380 info.testinfo_len = rc;
381 rc = ops->get_sset_count(dev, ETH_SS_STATS);
382 if (rc >= 0)
383 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700384 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
385 if (rc >= 0)
386 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700387 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000388 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000390 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 info.eedump_len = ops->get_eeprom_len(dev);
392
393 if (copy_to_user(useraddr, &info, sizeof(info)))
394 return -EFAULT;
395 return 0;
396}
397
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800398static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700399 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000400{
401 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000402 u64 sset_mask;
403 int i, idx = 0, n_bits = 0, ret, rc;
404 u32 *info_buf = NULL;
405
Jeff Garzik723b2f52010-03-03 22:51:50 +0000406 if (copy_from_user(&info, useraddr, sizeof(info)))
407 return -EFAULT;
408
409 /* store copy of mask, because we zero struct later on */
410 sset_mask = info.sset_mask;
411 if (!sset_mask)
412 return 0;
413
414 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000415 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000416
417 memset(&info, 0, sizeof(info));
418 info.cmd = ETHTOOL_GSSET_INFO;
419
420 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
421 if (!info_buf)
422 return -ENOMEM;
423
424 /*
425 * fill return buffer based on input bitmask and successful
426 * get_sset_count return
427 */
428 for (i = 0; i < 64; i++) {
429 if (!(sset_mask & (1ULL << i)))
430 continue;
431
Michał Mirosław340ae162011-02-15 16:59:16 +0000432 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000433 if (rc >= 0) {
434 info.sset_mask |= (1ULL << i);
435 info_buf[idx++] = rc;
436 }
437 }
438
439 ret = -EFAULT;
440 if (copy_to_user(useraddr, &info, sizeof(info)))
441 goto out;
442
443 useraddr += offsetof(struct ethtool_sset_info, data);
444 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
445 goto out;
446
447 ret = 0;
448
449out:
450 kfree(info_buf);
451 return ret;
452}
453
chavey97f8aef2010-04-07 21:54:42 -0700454static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000455 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700456{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000457 struct ethtool_rxnfc info;
458 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000459 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700460
Santwona Behera59089d82009-02-20 00:58:13 -0800461 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700462 return -EOPNOTSUPP;
463
Ben Hutchingsbf988432010-06-28 08:45:58 +0000464 /* struct ethtool_rxnfc was originally defined for
465 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
466 * members. User-space might still be using that
467 * definition. */
468 if (cmd == ETHTOOL_SRXFH)
469 info_size = (offsetof(struct ethtool_rxnfc, data) +
470 sizeof(info.data));
471
472 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700473 return -EFAULT;
474
Ben Hutchings55664f32012-01-03 12:04:51 +0000475 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
476 if (rc)
477 return rc;
478
479 if (cmd == ETHTOOL_SRXCLSRLINS &&
480 copy_to_user(useraddr, &info, info_size))
481 return -EFAULT;
482
483 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700484}
485
chavey97f8aef2010-04-07 21:54:42 -0700486static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000487 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700488{
489 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000490 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800491 const struct ethtool_ops *ops = dev->ethtool_ops;
492 int ret;
493 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700494
Santwona Behera59089d82009-02-20 00:58:13 -0800495 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700496 return -EOPNOTSUPP;
497
Ben Hutchingsbf988432010-06-28 08:45:58 +0000498 /* struct ethtool_rxnfc was originally defined for
499 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
500 * members. User-space might still be using that
501 * definition. */
502 if (cmd == ETHTOOL_GRXFH)
503 info_size = (offsetof(struct ethtool_rxnfc, data) +
504 sizeof(info.data));
505
506 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700507 return -EFAULT;
508
Santwona Behera59089d82009-02-20 00:58:13 -0800509 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
510 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000511 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000512 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000513 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800514 if (!rule_buf)
515 return -ENOMEM;
516 }
517 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700518
Santwona Behera59089d82009-02-20 00:58:13 -0800519 ret = ops->get_rxnfc(dev, &info, rule_buf);
520 if (ret < 0)
521 goto err_out;
522
523 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000524 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800525 goto err_out;
526
527 if (rule_buf) {
528 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
529 if (copy_to_user(useraddr, rule_buf,
530 info.rule_cnt * sizeof(u32)))
531 goto err_out;
532 }
533 ret = 0;
534
535err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700536 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800537
538 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700539}
540
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000541static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
542 void __user *useraddr)
543{
Ben Hutchings7850f632011-12-15 13:55:01 +0000544 u32 user_size, dev_size;
545 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000546 int ret;
547
Ben Hutchings7850f632011-12-15 13:55:01 +0000548 if (!dev->ethtool_ops->get_rxfh_indir_size ||
549 !dev->ethtool_ops->get_rxfh_indir)
550 return -EOPNOTSUPP;
551 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
552 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000553 return -EOPNOTSUPP;
554
Ben Hutchings7850f632011-12-15 13:55:01 +0000555 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000556 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000557 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000558 return -EFAULT;
559
Ben Hutchings7850f632011-12-15 13:55:01 +0000560 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
561 &dev_size, sizeof(dev_size)))
562 return -EFAULT;
563
564 /* If the user buffer size is 0, this is just a query for the
565 * device table size. Otherwise, if it's smaller than the
566 * device table size it's an error.
567 */
568 if (user_size < dev_size)
569 return user_size == 0 ? 0 : -EINVAL;
570
571 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000572 if (!indir)
573 return -ENOMEM;
574
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000575 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
576 if (ret)
577 goto out;
578
Ben Hutchings7850f632011-12-15 13:55:01 +0000579 if (copy_to_user(useraddr +
580 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
581 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000582 ret = -EFAULT;
583
584out:
585 kfree(indir);
586 return ret;
587}
588
589static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
590 void __user *useraddr)
591{
Ben Hutchings7850f632011-12-15 13:55:01 +0000592 struct ethtool_rxnfc rx_rings;
593 u32 user_size, dev_size, i;
594 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000595 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000596 int ret;
597
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000598 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
599 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000600 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000601
602 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000603 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000604 return -EOPNOTSUPP;
605
Ben Hutchings7850f632011-12-15 13:55:01 +0000606 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000607 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000608 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000609 return -EFAULT;
610
Ben Hutchings278bc422011-12-15 13:56:49 +0000611 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000612 return -EINVAL;
613
614 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000615 if (!indir)
616 return -ENOMEM;
617
Ben Hutchings7850f632011-12-15 13:55:01 +0000618 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000619 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000620 if (ret)
621 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000622
623 if (user_size == 0) {
624 for (i = 0; i < dev_size; i++)
625 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
626 } else {
627 if (copy_from_user(indir,
628 useraddr +
629 offsetof(struct ethtool_rxfh_indir,
630 ring_index[0]),
631 dev_size * sizeof(indir[0]))) {
632 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000633 goto out;
634 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000635
636 /* Validate ring indices */
637 for (i = 0; i < dev_size; i++) {
638 if (indir[i] >= rx_rings.data) {
639 ret = -EINVAL;
640 goto out;
641 }
642 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000643 }
644
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000645 ret = ops->set_rxfh_indir(dev, indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000646
647out:
648 kfree(indir);
649 return ret;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
653{
654 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700655 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 void *regbuf;
657 int reglen, ret;
658
659 if (!ops->get_regs || !ops->get_regs_len)
660 return -EOPNOTSUPP;
661
662 if (copy_from_user(&regs, useraddr, sizeof(regs)))
663 return -EFAULT;
664
665 reglen = ops->get_regs_len(dev);
666 if (regs.len > reglen)
667 regs.len = reglen;
668
Eugene Teob7c7d012011-01-24 21:05:17 -0800669 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700670 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -ENOMEM;
672
673 ops->get_regs(dev, &regs, regbuf);
674
675 ret = -EFAULT;
676 if (copy_to_user(useraddr, &regs, sizeof(regs)))
677 goto out;
678 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700679 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto out;
681 ret = 0;
682
683 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000684 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return ret;
686}
687
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000688static int ethtool_reset(struct net_device *dev, char __user *useraddr)
689{
690 struct ethtool_value reset;
691 int ret;
692
693 if (!dev->ethtool_ops->reset)
694 return -EOPNOTSUPP;
695
696 if (copy_from_user(&reset, useraddr, sizeof(reset)))
697 return -EFAULT;
698
699 ret = dev->ethtool_ops->reset(dev, &reset.data);
700 if (ret)
701 return ret;
702
703 if (copy_to_user(useraddr, &reset, sizeof(reset)))
704 return -EFAULT;
705 return 0;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
709{
Roland Dreier8e557422010-02-11 12:14:23 -0800710 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (!dev->ethtool_ops->get_wol)
713 return -EOPNOTSUPP;
714
715 dev->ethtool_ops->get_wol(dev, &wol);
716
717 if (copy_to_user(useraddr, &wol, sizeof(wol)))
718 return -EFAULT;
719 return 0;
720}
721
722static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
723{
724 struct ethtool_wolinfo wol;
725
726 if (!dev->ethtool_ops->set_wol)
727 return -EOPNOTSUPP;
728
729 if (copy_from_user(&wol, useraddr, sizeof(wol)))
730 return -EFAULT;
731
732 return dev->ethtool_ops->set_wol(dev, &wol);
733}
734
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000735static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
736{
737 struct ethtool_eee edata;
738 int rc;
739
740 if (!dev->ethtool_ops->get_eee)
741 return -EOPNOTSUPP;
742
743 memset(&edata, 0, sizeof(struct ethtool_eee));
744 edata.cmd = ETHTOOL_GEEE;
745 rc = dev->ethtool_ops->get_eee(dev, &edata);
746
747 if (rc)
748 return rc;
749
750 if (copy_to_user(useraddr, &edata, sizeof(edata)))
751 return -EFAULT;
752
753 return 0;
754}
755
756static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
757{
758 struct ethtool_eee edata;
759
760 if (!dev->ethtool_ops->set_eee)
761 return -EOPNOTSUPP;
762
763 if (copy_from_user(&edata, useraddr, sizeof(edata)))
764 return -EFAULT;
765
766 return dev->ethtool_ops->set_eee(dev, &edata);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static int ethtool_nway_reset(struct net_device *dev)
770{
771 if (!dev->ethtool_ops->nway_reset)
772 return -EOPNOTSUPP;
773
774 return dev->ethtool_ops->nway_reset(dev);
775}
776
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000777static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
778{
779 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
780
781 if (!dev->ethtool_ops->get_link)
782 return -EOPNOTSUPP;
783
784 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
785
786 if (copy_to_user(useraddr, &edata, sizeof(edata)))
787 return -EFAULT;
788 return 0;
789}
790
Ben Hutchings081d0942012-04-12 00:42:12 +0000791static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
792 int (*getter)(struct net_device *,
793 struct ethtool_eeprom *, u8 *),
794 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700797 void __user *userbuf = useraddr + sizeof(eeprom);
798 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700800 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
803 return -EFAULT;
804
805 /* Check for wrap and zero */
806 if (eeprom.offset + eeprom.len <= eeprom.offset)
807 return -EINVAL;
808
809 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +0000810 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -EINVAL;
812
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700813 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!data)
815 return -ENOMEM;
816
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700817 bytes_remaining = eeprom.len;
818 while (bytes_remaining > 0) {
819 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Ben Hutchings081d0942012-04-12 00:42:12 +0000821 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700822 if (ret)
823 break;
824 if (copy_to_user(userbuf, data, eeprom.len)) {
825 ret = -EFAULT;
826 break;
827 }
828 userbuf += eeprom.len;
829 eeprom.offset += eeprom.len;
830 bytes_remaining -= eeprom.len;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700833 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
834 eeprom.offset -= eeprom.len;
835 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
836 ret = -EFAULT;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kfree(data);
839 return ret;
840}
841
Ben Hutchings081d0942012-04-12 00:42:12 +0000842static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
843{
844 const struct ethtool_ops *ops = dev->ethtool_ops;
845
846 if (!ops->get_eeprom || !ops->get_eeprom_len)
847 return -EOPNOTSUPP;
848
849 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
850 ops->get_eeprom_len(dev));
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
854{
855 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700856 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700857 void __user *userbuf = useraddr + sizeof(eeprom);
858 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700860 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (!ops->set_eeprom || !ops->get_eeprom_len)
863 return -EOPNOTSUPP;
864
865 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
866 return -EFAULT;
867
868 /* Check for wrap and zero */
869 if (eeprom.offset + eeprom.len <= eeprom.offset)
870 return -EINVAL;
871
872 /* Check for exceeding total eeprom len */
873 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
874 return -EINVAL;
875
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700876 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!data)
878 return -ENOMEM;
879
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700880 bytes_remaining = eeprom.len;
881 while (bytes_remaining > 0) {
882 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700884 if (copy_from_user(data, userbuf, eeprom.len)) {
885 ret = -EFAULT;
886 break;
887 }
888 ret = ops->set_eeprom(dev, &eeprom, data);
889 if (ret)
890 break;
891 userbuf += eeprom.len;
892 eeprom.offset += eeprom.len;
893 bytes_remaining -= eeprom.len;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 kfree(data);
897 return ret;
898}
899
chavey97f8aef2010-04-07 21:54:42 -0700900static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
901 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Roland Dreier8e557422010-02-11 12:14:23 -0800903 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (!dev->ethtool_ops->get_coalesce)
906 return -EOPNOTSUPP;
907
908 dev->ethtool_ops->get_coalesce(dev, &coalesce);
909
910 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
911 return -EFAULT;
912 return 0;
913}
914
chavey97f8aef2010-04-07 21:54:42 -0700915static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
916 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct ethtool_coalesce coalesce;
919
David S. Millerfa04ae52005-06-06 15:07:19 -0700920 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -EOPNOTSUPP;
922
923 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
924 return -EFAULT;
925
926 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
927}
928
929static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
930{
Roland Dreier8e557422010-02-11 12:14:23 -0800931 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!dev->ethtool_ops->get_ringparam)
934 return -EOPNOTSUPP;
935
936 dev->ethtool_ops->get_ringparam(dev, &ringparam);
937
938 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
939 return -EFAULT;
940 return 0;
941}
942
943static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
944{
945 struct ethtool_ringparam ringparam;
946
947 if (!dev->ethtool_ops->set_ringparam)
948 return -EOPNOTSUPP;
949
950 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
951 return -EFAULT;
952
953 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
954}
955
amit salecha8b5933c32011-04-07 01:58:42 +0000956static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
957 void __user *useraddr)
958{
959 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
960
961 if (!dev->ethtool_ops->get_channels)
962 return -EOPNOTSUPP;
963
964 dev->ethtool_ops->get_channels(dev, &channels);
965
966 if (copy_to_user(useraddr, &channels, sizeof(channels)))
967 return -EFAULT;
968 return 0;
969}
970
971static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
972 void __user *useraddr)
973{
974 struct ethtool_channels channels;
975
976 if (!dev->ethtool_ops->set_channels)
977 return -EOPNOTSUPP;
978
979 if (copy_from_user(&channels, useraddr, sizeof(channels)))
980 return -EFAULT;
981
982 return dev->ethtool_ops->set_channels(dev, &channels);
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
986{
987 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
988
989 if (!dev->ethtool_ops->get_pauseparam)
990 return -EOPNOTSUPP;
991
992 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
993
994 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
995 return -EFAULT;
996 return 0;
997}
998
999static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1000{
1001 struct ethtool_pauseparam pauseparam;
1002
Jeff Garzike1b90c42006-07-17 12:54:40 -04001003 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -EOPNOTSUPP;
1005
1006 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1007 return -EFAULT;
1008
1009 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1013{
1014 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001015 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001017 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001019 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001020 return -EOPNOTSUPP;
1021
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001022 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001023 if (test_len < 0)
1024 return test_len;
1025 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (copy_from_user(&test, useraddr, sizeof(test)))
1028 return -EFAULT;
1029
Jeff Garzikff03d492007-08-15 16:01:08 -07001030 test.len = test_len;
1031 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!data)
1033 return -ENOMEM;
1034
1035 ops->self_test(dev, &test, data);
1036
1037 ret = -EFAULT;
1038 if (copy_to_user(useraddr, &test, sizeof(test)))
1039 goto out;
1040 useraddr += sizeof(test);
1041 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1042 goto out;
1043 ret = 0;
1044
1045 out:
1046 kfree(data);
1047 return ret;
1048}
1049
1050static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1051{
1052 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 u8 *data;
1054 int ret;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1057 return -EFAULT;
1058
Michał Mirosław340ae162011-02-15 16:59:16 +00001059 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001060 if (ret < 0)
1061 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001062
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001063 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1066 if (!data)
1067 return -ENOMEM;
1068
Michał Mirosław340ae162011-02-15 16:59:16 +00001069 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 ret = -EFAULT;
1072 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1073 goto out;
1074 useraddr += sizeof(gstrings);
1075 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1076 goto out;
1077 ret = 0;
1078
Michał Mirosław340ae162011-02-15 16:59:16 +00001079out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 kfree(data);
1081 return ret;
1082}
1083
1084static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1085{
1086 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001087 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001088 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001089 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001091 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EOPNOTSUPP;
1093
Ben Hutchings68f512f2011-04-02 00:35:15 +01001094 if (busy)
1095 return -EBUSY;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (copy_from_user(&id, useraddr, sizeof(id)))
1098 return -EFAULT;
1099
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001100 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001101 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001102 return rc;
1103
1104 /* Drop the RTNL lock while waiting, but prevent reentry or
1105 * removal of the device.
1106 */
1107 busy = true;
1108 dev_hold(dev);
1109 rtnl_unlock();
1110
1111 if (rc == 0) {
1112 /* Driver will handle this itself */
1113 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001114 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001115 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001116 /* Driver expects to be called at twice the frequency in rc */
1117 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001118
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001119 /* Count down seconds */
1120 do {
1121 /* Count down iterations per second */
1122 i = n;
1123 do {
1124 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001125 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001126 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1127 rtnl_unlock();
1128 if (rc)
1129 break;
1130 schedule_timeout_interruptible(interval);
1131 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001132 } while (!signal_pending(current) &&
1133 (id.data == 0 || --id.data != 0));
1134 }
1135
1136 rtnl_lock();
1137 dev_put(dev);
1138 busy = false;
1139
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001140 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001141 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1145{
1146 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001147 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001149 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001151 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001152 return -EOPNOTSUPP;
1153
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001154 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001155 if (n_stats < 0)
1156 return n_stats;
1157 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1160 return -EFAULT;
1161
Jeff Garzikff03d492007-08-15 16:01:08 -07001162 stats.n_stats = n_stats;
1163 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (!data)
1165 return -ENOMEM;
1166
1167 ops->get_ethtool_stats(dev, &stats, data);
1168
1169 ret = -EFAULT;
1170 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1171 goto out;
1172 useraddr += sizeof(stats);
1173 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1174 goto out;
1175 ret = 0;
1176
1177 out:
1178 kfree(data);
1179 return ret;
1180}
1181
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001182static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001183{
1184 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001185
Matthew Wilcox313674a2007-07-31 14:00:29 -07001186 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001187 return -EFAULT;
1188
Matthew Wilcox313674a2007-07-31 14:00:29 -07001189 if (epaddr.size < dev->addr_len)
1190 return -ETOOSMALL;
1191 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001192
Jon Wetzela6f9a702005-08-20 17:15:54 -07001193 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001194 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001195 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001196 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1197 return -EFAULT;
1198 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001199}
1200
Jeff Garzik13c99b22007-08-15 16:01:56 -07001201static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1202 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001203{
Roland Dreier8e557422010-02-11 12:14:23 -08001204 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001205
Jeff Garzik13c99b22007-08-15 16:01:56 -07001206 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001207 return -EOPNOTSUPP;
1208
Jeff Garzik13c99b22007-08-15 16:01:56 -07001209 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001210
1211 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1212 return -EFAULT;
1213 return 0;
1214}
1215
Jeff Garzik13c99b22007-08-15 16:01:56 -07001216static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1217 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001218{
1219 struct ethtool_value edata;
1220
Jeff Garzik13c99b22007-08-15 16:01:56 -07001221 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001222 return -EOPNOTSUPP;
1223
1224 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1225 return -EFAULT;
1226
Jeff Garzik13c99b22007-08-15 16:01:56 -07001227 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001228 return 0;
1229}
1230
Jeff Garzik13c99b22007-08-15 16:01:56 -07001231static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1232 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001233{
1234 struct ethtool_value edata;
1235
Jeff Garzik13c99b22007-08-15 16:01:56 -07001236 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001237 return -EOPNOTSUPP;
1238
1239 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1240 return -EFAULT;
1241
Jeff Garzik13c99b22007-08-15 16:01:56 -07001242 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001243}
1244
chavey97f8aef2010-04-07 21:54:42 -07001245static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1246 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001247{
1248 struct ethtool_flash efl;
1249
1250 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1251 return -EFAULT;
1252
1253 if (!dev->ethtool_ops->flash_device)
1254 return -EOPNOTSUPP;
1255
Ben Hutchings786f5282012-02-01 09:32:25 +00001256 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1257
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001258 return dev->ethtool_ops->flash_device(dev, &efl);
1259}
1260
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001261static int ethtool_set_dump(struct net_device *dev,
1262 void __user *useraddr)
1263{
1264 struct ethtool_dump dump;
1265
1266 if (!dev->ethtool_ops->set_dump)
1267 return -EOPNOTSUPP;
1268
1269 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1270 return -EFAULT;
1271
1272 return dev->ethtool_ops->set_dump(dev, &dump);
1273}
1274
1275static int ethtool_get_dump_flag(struct net_device *dev,
1276 void __user *useraddr)
1277{
1278 int ret;
1279 struct ethtool_dump dump;
1280 const struct ethtool_ops *ops = dev->ethtool_ops;
1281
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001282 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001283 return -EOPNOTSUPP;
1284
1285 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1286 return -EFAULT;
1287
1288 ret = ops->get_dump_flag(dev, &dump);
1289 if (ret)
1290 return ret;
1291
1292 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1293 return -EFAULT;
1294 return 0;
1295}
1296
1297static int ethtool_get_dump_data(struct net_device *dev,
1298 void __user *useraddr)
1299{
1300 int ret;
1301 __u32 len;
1302 struct ethtool_dump dump, tmp;
1303 const struct ethtool_ops *ops = dev->ethtool_ops;
1304 void *data = NULL;
1305
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001306 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001307 return -EOPNOTSUPP;
1308
1309 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1310 return -EFAULT;
1311
1312 memset(&tmp, 0, sizeof(tmp));
1313 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1314 ret = ops->get_dump_flag(dev, &tmp);
1315 if (ret)
1316 return ret;
1317
1318 len = (tmp.len > dump.len) ? dump.len : tmp.len;
1319 if (!len)
1320 return -EFAULT;
1321
1322 data = vzalloc(tmp.len);
1323 if (!data)
1324 return -ENOMEM;
1325 ret = ops->get_dump_data(dev, &dump, data);
1326 if (ret)
1327 goto out;
1328
1329 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1330 ret = -EFAULT;
1331 goto out;
1332 }
1333 useraddr += offsetof(struct ethtool_dump, data);
1334 if (copy_to_user(useraddr, data, len))
1335 ret = -EFAULT;
1336out:
1337 vfree(data);
1338 return ret;
1339}
1340
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001341static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1342{
1343 int err = 0;
1344 struct ethtool_ts_info info;
1345 const struct ethtool_ops *ops = dev->ethtool_ops;
1346 struct phy_device *phydev = dev->phydev;
1347
1348 memset(&info, 0, sizeof(info));
1349 info.cmd = ETHTOOL_GET_TS_INFO;
1350
1351 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001352 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001353 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001354 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001355 } else {
1356 info.so_timestamping =
1357 SOF_TIMESTAMPING_RX_SOFTWARE |
1358 SOF_TIMESTAMPING_SOFTWARE;
1359 info.phc_index = -1;
1360 }
1361
1362 if (err)
1363 return err;
1364
1365 if (copy_to_user(useraddr, &info, sizeof(info)))
1366 err = -EFAULT;
1367
1368 return err;
1369}
1370
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001371static int ethtool_get_module_info(struct net_device *dev,
1372 void __user *useraddr)
1373{
1374 int ret;
1375 struct ethtool_modinfo modinfo;
1376 const struct ethtool_ops *ops = dev->ethtool_ops;
1377
1378 if (!ops->get_module_info)
1379 return -EOPNOTSUPP;
1380
1381 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1382 return -EFAULT;
1383
1384 ret = ops->get_module_info(dev, &modinfo);
1385 if (ret)
1386 return ret;
1387
1388 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1389 return -EFAULT;
1390
1391 return 0;
1392}
1393
1394static int ethtool_get_module_eeprom(struct net_device *dev,
1395 void __user *useraddr)
1396{
1397 int ret;
1398 struct ethtool_modinfo modinfo;
1399 const struct ethtool_ops *ops = dev->ethtool_ops;
1400
1401 if (!ops->get_module_info || !ops->get_module_eeprom)
1402 return -EOPNOTSUPP;
1403
1404 ret = ops->get_module_info(dev, &modinfo);
1405 if (ret)
1406 return ret;
1407
1408 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1409 modinfo.eeprom_len);
1410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/* The main entry point in this file. Called from net/core/dev.c */
1413
Eric W. Biederman881d9662007-09-17 11:56:21 -07001414int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001416 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 void __user *useraddr = ifr->ifr_data;
1418 u32 ethcmd;
1419 int rc;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001420 u32 old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!dev || !netif_device_present(dev))
1423 return -ENODEV;
1424
chavey97f8aef2010-04-07 21:54:42 -07001425 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -EFAULT;
1427
Stephen Hemminger75f31232006-09-28 15:13:37 -07001428 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001429 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001430 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001431 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001432 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001433 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001434 case ETHTOOL_GCOALESCE:
1435 case ETHTOOL_GRINGPARAM:
1436 case ETHTOOL_GPAUSEPARAM:
1437 case ETHTOOL_GRXCSUM:
1438 case ETHTOOL_GTXCSUM:
1439 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001440 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001441 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001442 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001443 case ETHTOOL_GTSO:
1444 case ETHTOOL_GPERMADDR:
1445 case ETHTOOL_GUFO:
1446 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001447 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001448 case ETHTOOL_GFLAGS:
1449 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001450 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001451 case ETHTOOL_GRXRINGS:
1452 case ETHTOOL_GRXCLSRLCNT:
1453 case ETHTOOL_GRXCLSRULE:
1454 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001455 case ETHTOOL_GRXFHINDIR:
Michał Mirosław5455c692011-02-15 16:59:17 +00001456 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001457 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001458 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001459 case ETHTOOL_GEEE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001460 break;
1461 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001462 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001463 return -EPERM;
1464 }
1465
chavey97f8aef2010-04-07 21:54:42 -07001466 if (dev->ethtool_ops->begin) {
1467 rc = dev->ethtool_ops->begin(dev);
1468 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001470 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001471 old_features = dev->features;
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 switch (ethcmd) {
1474 case ETHTOOL_GSET:
1475 rc = ethtool_get_settings(dev, useraddr);
1476 break;
1477 case ETHTOOL_SSET:
1478 rc = ethtool_set_settings(dev, useraddr);
1479 break;
1480 case ETHTOOL_GDRVINFO:
1481 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 case ETHTOOL_GREGS:
1484 rc = ethtool_get_regs(dev, useraddr);
1485 break;
1486 case ETHTOOL_GWOL:
1487 rc = ethtool_get_wol(dev, useraddr);
1488 break;
1489 case ETHTOOL_SWOL:
1490 rc = ethtool_set_wol(dev, useraddr);
1491 break;
1492 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001493 rc = ethtool_get_value(dev, useraddr, ethcmd,
1494 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001497 rc = ethtool_set_value_void(dev, useraddr,
1498 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001500 case ETHTOOL_GEEE:
1501 rc = ethtool_get_eee(dev, useraddr);
1502 break;
1503 case ETHTOOL_SEEE:
1504 rc = ethtool_set_eee(dev, useraddr);
1505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 case ETHTOOL_NWAY_RST:
1507 rc = ethtool_nway_reset(dev);
1508 break;
1509 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001510 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512 case ETHTOOL_GEEPROM:
1513 rc = ethtool_get_eeprom(dev, useraddr);
1514 break;
1515 case ETHTOOL_SEEPROM:
1516 rc = ethtool_set_eeprom(dev, useraddr);
1517 break;
1518 case ETHTOOL_GCOALESCE:
1519 rc = ethtool_get_coalesce(dev, useraddr);
1520 break;
1521 case ETHTOOL_SCOALESCE:
1522 rc = ethtool_set_coalesce(dev, useraddr);
1523 break;
1524 case ETHTOOL_GRINGPARAM:
1525 rc = ethtool_get_ringparam(dev, useraddr);
1526 break;
1527 case ETHTOOL_SRINGPARAM:
1528 rc = ethtool_set_ringparam(dev, useraddr);
1529 break;
1530 case ETHTOOL_GPAUSEPARAM:
1531 rc = ethtool_get_pauseparam(dev, useraddr);
1532 break;
1533 case ETHTOOL_SPAUSEPARAM:
1534 rc = ethtool_set_pauseparam(dev, useraddr);
1535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 case ETHTOOL_TEST:
1537 rc = ethtool_self_test(dev, useraddr);
1538 break;
1539 case ETHTOOL_GSTRINGS:
1540 rc = ethtool_get_strings(dev, useraddr);
1541 break;
1542 case ETHTOOL_PHYS_ID:
1543 rc = ethtool_phys_id(dev, useraddr);
1544 break;
1545 case ETHTOOL_GSTATS:
1546 rc = ethtool_get_stats(dev, useraddr);
1547 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001548 case ETHTOOL_GPERMADDR:
1549 rc = ethtool_get_perm_addr(dev, useraddr);
1550 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001551 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001552 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001553 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001554 break;
1555 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001556 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001557 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001558 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001559 rc = ethtool_get_value(dev, useraddr, ethcmd,
1560 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001561 break;
1562 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001563 rc = ethtool_set_value(dev, useraddr,
1564 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001565 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001566 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001567 case ETHTOOL_GRXRINGS:
1568 case ETHTOOL_GRXCLSRLCNT:
1569 case ETHTOOL_GRXCLSRULE:
1570 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001571 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001572 break;
1573 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001574 case ETHTOOL_SRXCLSRLDEL:
1575 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001576 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001577 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001578 case ETHTOOL_FLASHDEV:
1579 rc = ethtool_flash_device(dev, useraddr);
1580 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001581 case ETHTOOL_RESET:
1582 rc = ethtool_reset(dev, useraddr);
1583 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001584 case ETHTOOL_GSSET_INFO:
1585 rc = ethtool_get_sset_info(dev, useraddr);
1586 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001587 case ETHTOOL_GRXFHINDIR:
1588 rc = ethtool_get_rxfh_indir(dev, useraddr);
1589 break;
1590 case ETHTOOL_SRXFHINDIR:
1591 rc = ethtool_set_rxfh_indir(dev, useraddr);
1592 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001593 case ETHTOOL_GFEATURES:
1594 rc = ethtool_get_features(dev, useraddr);
1595 break;
1596 case ETHTOOL_SFEATURES:
1597 rc = ethtool_set_features(dev, useraddr);
1598 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001599 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001600 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001601 case ETHTOOL_GSG:
1602 case ETHTOOL_GTSO:
1603 case ETHTOOL_GUFO:
1604 case ETHTOOL_GGSO:
1605 case ETHTOOL_GGRO:
1606 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1607 break;
1608 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001609 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001610 case ETHTOOL_SSG:
1611 case ETHTOOL_STSO:
1612 case ETHTOOL_SUFO:
1613 case ETHTOOL_SGSO:
1614 case ETHTOOL_SGRO:
1615 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1616 break;
amit salecha8b5933c32011-04-07 01:58:42 +00001617 case ETHTOOL_GCHANNELS:
1618 rc = ethtool_get_channels(dev, useraddr);
1619 break;
1620 case ETHTOOL_SCHANNELS:
1621 rc = ethtool_set_channels(dev, useraddr);
1622 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001623 case ETHTOOL_SET_DUMP:
1624 rc = ethtool_set_dump(dev, useraddr);
1625 break;
1626 case ETHTOOL_GET_DUMP_FLAG:
1627 rc = ethtool_get_dump_flag(dev, useraddr);
1628 break;
1629 case ETHTOOL_GET_DUMP_DATA:
1630 rc = ethtool_get_dump_data(dev, useraddr);
1631 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001632 case ETHTOOL_GET_TS_INFO:
1633 rc = ethtool_get_ts_info(dev, useraddr);
1634 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001635 case ETHTOOL_GMODULEINFO:
1636 rc = ethtool_get_module_info(dev, useraddr);
1637 break;
1638 case ETHTOOL_GMODULEEEPROM:
1639 rc = ethtool_get_module_eeprom(dev, useraddr);
1640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001642 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001644
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001645 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001647
1648 if (old_features != dev->features)
1649 netdev_features_change(dev);
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}