blob: ab5fa6336c840785b93c6f469b441d6fcdec1379 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010026#include <linux/rtnetlink.h>
27#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Some useful ethtool_ops methods that're device independent.
31 * If we find that all drivers want to do the same thing here,
32 * we can turn these into dev_() function calls.
33 */
34
35u32 ethtool_op_get_link(struct net_device *dev)
36{
37 return netif_carrier_ok(dev) ? 1 : 0;
38}
chavey97f8aef2010-04-07 21:54:42 -070039EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Richard Cochran02eacbd2012-04-03 22:59:22 +000041int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
42{
43 info->so_timestamping =
44 SOF_TIMESTAMPING_TX_SOFTWARE |
45 SOF_TIMESTAMPING_RX_SOFTWARE |
46 SOF_TIMESTAMPING_SOFTWARE;
47 info->phc_index = -1;
48 return 0;
49}
50EXPORT_SYMBOL(ethtool_op_get_ts_info);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* Handlers for each ethtool command */
53
Michał Mirosław475414f2011-11-15 15:29:55 +000054#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000055
Michał Mirosław9d921542011-11-15 15:29:55 +000056static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
57 [NETIF_F_SG_BIT] = "tx-scatter-gather",
58 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
Michał Mirosław9d921542011-11-15 15:29:55 +000059 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
60 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
61 [NETIF_F_HIGHDMA_BIT] = "highdma",
62 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090063 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000064
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090065 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
66 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040067 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
68 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
69 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
Michał Mirosław9d921542011-11-15 15:29:55 +000070 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
71 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
72 [NETIF_F_LLTX_BIT] = "tx-lockless",
73 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
74 [NETIF_F_GRO_BIT] = "rx-gro",
75 [NETIF_F_LRO_BIT] = "rx-lro",
76
77 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
78 [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
79 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
80 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
81 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
82 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000083 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000084 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Simon Horman0d89d202013-05-23 21:02:52 +000085 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000086
87 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
88 [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
89 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
90 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
91 [NETIF_F_RXHASH_BIT] = "rx-hashing",
92 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
93 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
94 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000095 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +000096 [NETIF_F_RXALL_BIT] = "rx-all",
Michał Mirosław9d921542011-11-15 15:29:55 +000097};
98
Michał Mirosław5455c692011-02-15 16:59:17 +000099static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
100{
101 struct ethtool_gfeatures cmd = {
102 .cmd = ETHTOOL_GFEATURES,
103 .size = ETHTOOL_DEV_FEATURE_WORDS,
104 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000105 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000106 u32 __user *sizeaddr;
107 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000108 int i;
109
110 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000111 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000112
113 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000114 features[i].available = (u32)(dev->hw_features >> (32 * i));
115 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
116 features[i].active = (u32)(dev->features >> (32 * i));
117 features[i].never_changed =
118 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000119 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000120
121 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
122 if (get_user(copy_size, sizeaddr))
123 return -EFAULT;
124
125 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
126 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
127
128 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
129 return -EFAULT;
130 useraddr += sizeof(cmd);
131 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
132 return -EFAULT;
133
134 return 0;
135}
136
137static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
138{
139 struct ethtool_sfeatures cmd;
140 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000141 netdev_features_t wanted = 0, valid = 0;
142 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000143
144 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
145 return -EFAULT;
146 useraddr += sizeof(cmd);
147
148 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
149 return -EINVAL;
150
151 if (copy_from_user(features, useraddr, sizeof(features)))
152 return -EFAULT;
153
Michał Mirosław475414f2011-11-15 15:29:55 +0000154 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000155 valid |= (netdev_features_t)features[i].valid << (32 * i);
156 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000157 }
158
159 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000160 return -EINVAL;
161
Michał Mirosław475414f2011-11-15 15:29:55 +0000162 if (valid & ~dev->hw_features) {
163 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000164 ret |= ETHTOOL_F_UNSUPPORTED;
165 }
166
Michał Mirosław475414f2011-11-15 15:29:55 +0000167 dev->wanted_features &= ~valid;
168 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700169 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000170
Michał Mirosław475414f2011-11-15 15:29:55 +0000171 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000172 ret |= ETHTOOL_F_WISH;
173
174 return ret;
175}
176
Michał Mirosław340ae162011-02-15 16:59:16 +0000177static int __ethtool_get_sset_count(struct net_device *dev, int sset)
178{
179 const struct ethtool_ops *ops = dev->ethtool_ops;
180
Michał Mirosław5455c692011-02-15 16:59:17 +0000181 if (sset == ETH_SS_FEATURES)
182 return ARRAY_SIZE(netdev_features_strings);
183
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000184 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000185 return ops->get_sset_count(dev, sset);
186 else
187 return -EOPNOTSUPP;
188}
189
190static void __ethtool_get_strings(struct net_device *dev,
191 u32 stringset, u8 *data)
192{
193 const struct ethtool_ops *ops = dev->ethtool_ops;
194
Michał Mirosław5455c692011-02-15 16:59:17 +0000195 if (stringset == ETH_SS_FEATURES)
196 memcpy(data, netdev_features_strings,
197 sizeof(netdev_features_strings));
198 else
199 /* ops->get_strings is valid because checked earlier */
200 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000201}
202
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000203static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000204{
205 /* feature masks of legacy discrete ethtool ops */
206
207 switch (eth_cmd) {
208 case ETHTOOL_GTXCSUM:
209 case ETHTOOL_STXCSUM:
210 return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000211 case ETHTOOL_GRXCSUM:
212 case ETHTOOL_SRXCSUM:
213 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000214 case ETHTOOL_GSG:
215 case ETHTOOL_SSG:
216 return NETIF_F_SG;
217 case ETHTOOL_GTSO:
218 case ETHTOOL_STSO:
219 return NETIF_F_ALL_TSO;
220 case ETHTOOL_GUFO:
221 case ETHTOOL_SUFO:
222 return NETIF_F_UFO;
223 case ETHTOOL_GGSO:
224 case ETHTOOL_SGSO:
225 return NETIF_F_GSO;
226 case ETHTOOL_GGRO:
227 case ETHTOOL_SGRO:
228 return NETIF_F_GRO;
229 default:
230 BUG();
231 }
232}
233
Michał Mirosław0a417702011-02-15 16:59:17 +0000234static int ethtool_get_one_feature(struct net_device *dev,
235 char __user *useraddr, u32 ethcmd)
236{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000237 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000238 struct ethtool_value edata = {
239 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000240 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000241 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000242
Michał Mirosław0a417702011-02-15 16:59:17 +0000243 if (copy_to_user(useraddr, &edata, sizeof(edata)))
244 return -EFAULT;
245 return 0;
246}
247
Michał Mirosław0a417702011-02-15 16:59:17 +0000248static int ethtool_set_one_feature(struct net_device *dev,
249 void __user *useraddr, u32 ethcmd)
250{
251 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000252 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000253
254 if (copy_from_user(&edata, useraddr, sizeof(edata)))
255 return -EFAULT;
256
Michał Mirosław86794882011-02-15 16:59:17 +0000257 mask = ethtool_get_feature_mask(ethcmd);
258 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000259 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000260 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000261
262 if (edata.data)
263 dev->wanted_features |= mask;
264 else
265 dev->wanted_features &= ~mask;
266
267 __netdev_update_features(dev);
268
269 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000270}
271
Michał Mirosław02b3a552011-11-15 15:29:55 +0000272#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
273 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000274#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
275 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
276 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000277
278static u32 __ethtool_get_flags(struct net_device *dev)
279{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000280 u32 flags = 0;
281
Patrick McHardyf6469682013-04-19 02:04:27 +0000282 if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
283 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) flags |= ETH_FLAG_RXVLAN;
284 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) flags |= ETH_FLAG_TXVLAN;
285 if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
286 if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000287
288 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000289}
290
291static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000292{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000293 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000294
Michał Mirosław02b3a552011-11-15 15:29:55 +0000295 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000296 return -EINVAL;
297
Michał Mirosław02b3a552011-11-15 15:29:55 +0000298 if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
Patrick McHardyf6469682013-04-19 02:04:27 +0000299 if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_CTAG_RX;
300 if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_CTAG_TX;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000301 if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
302 if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
303
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000304 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000305 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000306 if (changed & ~dev->hw_features)
307 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
308
309 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000310 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000311
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700312 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000313
314 return 0;
315}
316
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000317int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000319 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000321 if (!dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -EOPNOTSUPP;
323
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000324 memset(cmd, 0, sizeof(struct ethtool_cmd));
325 cmd->cmd = ETHTOOL_GSET;
326 return dev->ethtool_ops->get_settings(dev, cmd);
327}
328EXPORT_SYMBOL(__ethtool_get_settings);
329
330static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
331{
332 int err;
333 struct ethtool_cmd cmd;
334
335 err = __ethtool_get_settings(dev, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (err < 0)
337 return err;
338
339 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
340 return -EFAULT;
341 return 0;
342}
343
344static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
345{
346 struct ethtool_cmd cmd;
347
348 if (!dev->ethtool_ops->set_settings)
349 return -EOPNOTSUPP;
350
351 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
352 return -EFAULT;
353
354 return dev->ethtool_ops->set_settings(dev, &cmd);
355}
356
chavey97f8aef2010-04-07 21:54:42 -0700357static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
358 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700361 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 memset(&info, 0, sizeof(info));
364 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000365 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700366 ops->get_drvinfo(dev, &info);
367 } else if (dev->dev.parent && dev->dev.parent->driver) {
368 strlcpy(info.bus_info, dev_name(dev->dev.parent),
369 sizeof(info.bus_info));
370 strlcpy(info.driver, dev->dev.parent->driver->name,
371 sizeof(info.driver));
372 } else {
373 return -EOPNOTSUPP;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jeff Garzik723b2f52010-03-03 22:51:50 +0000376 /*
377 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000378 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000379 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000380 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700381 int rc;
382
383 rc = ops->get_sset_count(dev, ETH_SS_TEST);
384 if (rc >= 0)
385 info.testinfo_len = rc;
386 rc = ops->get_sset_count(dev, ETH_SS_STATS);
387 if (rc >= 0)
388 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700389 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
390 if (rc >= 0)
391 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700392 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000393 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000395 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 info.eedump_len = ops->get_eeprom_len(dev);
397
398 if (copy_to_user(useraddr, &info, sizeof(info)))
399 return -EFAULT;
400 return 0;
401}
402
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800403static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700404 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000405{
406 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000407 u64 sset_mask;
408 int i, idx = 0, n_bits = 0, ret, rc;
409 u32 *info_buf = NULL;
410
Jeff Garzik723b2f52010-03-03 22:51:50 +0000411 if (copy_from_user(&info, useraddr, sizeof(info)))
412 return -EFAULT;
413
414 /* store copy of mask, because we zero struct later on */
415 sset_mask = info.sset_mask;
416 if (!sset_mask)
417 return 0;
418
419 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000420 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000421
422 memset(&info, 0, sizeof(info));
423 info.cmd = ETHTOOL_GSSET_INFO;
424
425 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
426 if (!info_buf)
427 return -ENOMEM;
428
429 /*
430 * fill return buffer based on input bitmask and successful
431 * get_sset_count return
432 */
433 for (i = 0; i < 64; i++) {
434 if (!(sset_mask & (1ULL << i)))
435 continue;
436
Michał Mirosław340ae162011-02-15 16:59:16 +0000437 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000438 if (rc >= 0) {
439 info.sset_mask |= (1ULL << i);
440 info_buf[idx++] = rc;
441 }
442 }
443
444 ret = -EFAULT;
445 if (copy_to_user(useraddr, &info, sizeof(info)))
446 goto out;
447
448 useraddr += offsetof(struct ethtool_sset_info, data);
449 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
450 goto out;
451
452 ret = 0;
453
454out:
455 kfree(info_buf);
456 return ret;
457}
458
chavey97f8aef2010-04-07 21:54:42 -0700459static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000460 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700461{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000462 struct ethtool_rxnfc info;
463 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000464 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700465
Santwona Behera59089d82009-02-20 00:58:13 -0800466 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700467 return -EOPNOTSUPP;
468
Ben Hutchingsbf988432010-06-28 08:45:58 +0000469 /* struct ethtool_rxnfc was originally defined for
470 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
471 * members. User-space might still be using that
472 * definition. */
473 if (cmd == ETHTOOL_SRXFH)
474 info_size = (offsetof(struct ethtool_rxnfc, data) +
475 sizeof(info.data));
476
477 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700478 return -EFAULT;
479
Ben Hutchings55664f32012-01-03 12:04:51 +0000480 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
481 if (rc)
482 return rc;
483
484 if (cmd == ETHTOOL_SRXCLSRLINS &&
485 copy_to_user(useraddr, &info, info_size))
486 return -EFAULT;
487
488 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700489}
490
chavey97f8aef2010-04-07 21:54:42 -0700491static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000492 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700493{
494 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000495 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800496 const struct ethtool_ops *ops = dev->ethtool_ops;
497 int ret;
498 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700499
Santwona Behera59089d82009-02-20 00:58:13 -0800500 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700501 return -EOPNOTSUPP;
502
Ben Hutchingsbf988432010-06-28 08:45:58 +0000503 /* struct ethtool_rxnfc was originally defined for
504 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
505 * members. User-space might still be using that
506 * definition. */
507 if (cmd == ETHTOOL_GRXFH)
508 info_size = (offsetof(struct ethtool_rxnfc, data) +
509 sizeof(info.data));
510
511 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700512 return -EFAULT;
513
Santwona Behera59089d82009-02-20 00:58:13 -0800514 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
515 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000516 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +0000517 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000518 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800519 if (!rule_buf)
520 return -ENOMEM;
521 }
522 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700523
Santwona Behera59089d82009-02-20 00:58:13 -0800524 ret = ops->get_rxnfc(dev, &info, rule_buf);
525 if (ret < 0)
526 goto err_out;
527
528 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000529 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800530 goto err_out;
531
532 if (rule_buf) {
533 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
534 if (copy_to_user(useraddr, rule_buf,
535 info.rule_cnt * sizeof(u32)))
536 goto err_out;
537 }
538 ret = 0;
539
540err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700541 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800542
543 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700544}
545
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000546static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
547 void __user *useraddr)
548{
Ben Hutchings7850f632011-12-15 13:55:01 +0000549 u32 user_size, dev_size;
550 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000551 int ret;
552
Ben Hutchings7850f632011-12-15 13:55:01 +0000553 if (!dev->ethtool_ops->get_rxfh_indir_size ||
554 !dev->ethtool_ops->get_rxfh_indir)
555 return -EOPNOTSUPP;
556 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
557 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000558 return -EOPNOTSUPP;
559
Ben Hutchings7850f632011-12-15 13:55:01 +0000560 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000561 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000562 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000563 return -EFAULT;
564
Ben Hutchings7850f632011-12-15 13:55:01 +0000565 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
566 &dev_size, sizeof(dev_size)))
567 return -EFAULT;
568
569 /* If the user buffer size is 0, this is just a query for the
570 * device table size. Otherwise, if it's smaller than the
571 * device table size it's an error.
572 */
573 if (user_size < dev_size)
574 return user_size == 0 ? 0 : -EINVAL;
575
576 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000577 if (!indir)
578 return -ENOMEM;
579
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000580 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
581 if (ret)
582 goto out;
583
Ben Hutchings7850f632011-12-15 13:55:01 +0000584 if (copy_to_user(useraddr +
585 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
586 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000587 ret = -EFAULT;
588
589out:
590 kfree(indir);
591 return ret;
592}
593
594static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
595 void __user *useraddr)
596{
Ben Hutchings7850f632011-12-15 13:55:01 +0000597 struct ethtool_rxnfc rx_rings;
598 u32 user_size, dev_size, i;
599 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000600 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000601 int ret;
602
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000603 if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir ||
604 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +0000605 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000606
607 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +0000608 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000609 return -EOPNOTSUPP;
610
Ben Hutchings7850f632011-12-15 13:55:01 +0000611 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000612 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +0000613 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000614 return -EFAULT;
615
Ben Hutchings278bc422011-12-15 13:56:49 +0000616 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +0000617 return -EINVAL;
618
619 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000620 if (!indir)
621 return -ENOMEM;
622
Ben Hutchings7850f632011-12-15 13:55:01 +0000623 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000624 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +0000625 if (ret)
626 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +0000627
628 if (user_size == 0) {
629 for (i = 0; i < dev_size; i++)
630 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
631 } else {
632 if (copy_from_user(indir,
633 useraddr +
634 offsetof(struct ethtool_rxfh_indir,
635 ring_index[0]),
636 dev_size * sizeof(indir[0]))) {
637 ret = -EFAULT;
Ben Hutchings7850f632011-12-15 13:55:01 +0000638 goto out;
639 }
Ben Hutchings278bc422011-12-15 13:56:49 +0000640
641 /* Validate ring indices */
642 for (i = 0; i < dev_size; i++) {
643 if (indir[i] >= rx_rings.data) {
644 ret = -EINVAL;
645 goto out;
646 }
647 }
Ben Hutchings7850f632011-12-15 13:55:01 +0000648 }
649
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000650 ret = ops->set_rxfh_indir(dev, indir);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000651
652out:
653 kfree(indir);
654 return ret;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
658{
659 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700660 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 void *regbuf;
662 int reglen, ret;
663
664 if (!ops->get_regs || !ops->get_regs_len)
665 return -EOPNOTSUPP;
666
667 if (copy_from_user(&regs, useraddr, sizeof(regs)))
668 return -EFAULT;
669
670 reglen = ops->get_regs_len(dev);
671 if (regs.len > reglen)
672 regs.len = reglen;
673
Eugene Teob7c7d012011-01-24 21:05:17 -0800674 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700675 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENOMEM;
677
678 ops->get_regs(dev, &regs, regbuf);
679
680 ret = -EFAULT;
681 if (copy_to_user(useraddr, &regs, sizeof(regs)))
682 goto out;
683 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -0700684 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto out;
686 ret = 0;
687
688 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000689 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return ret;
691}
692
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000693static int ethtool_reset(struct net_device *dev, char __user *useraddr)
694{
695 struct ethtool_value reset;
696 int ret;
697
698 if (!dev->ethtool_ops->reset)
699 return -EOPNOTSUPP;
700
701 if (copy_from_user(&reset, useraddr, sizeof(reset)))
702 return -EFAULT;
703
704 ret = dev->ethtool_ops->reset(dev, &reset.data);
705 if (ret)
706 return ret;
707
708 if (copy_to_user(useraddr, &reset, sizeof(reset)))
709 return -EFAULT;
710 return 0;
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
714{
Roland Dreier8e557422010-02-11 12:14:23 -0800715 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (!dev->ethtool_ops->get_wol)
718 return -EOPNOTSUPP;
719
720 dev->ethtool_ops->get_wol(dev, &wol);
721
722 if (copy_to_user(useraddr, &wol, sizeof(wol)))
723 return -EFAULT;
724 return 0;
725}
726
727static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
728{
729 struct ethtool_wolinfo wol;
730
731 if (!dev->ethtool_ops->set_wol)
732 return -EOPNOTSUPP;
733
734 if (copy_from_user(&wol, useraddr, sizeof(wol)))
735 return -EFAULT;
736
737 return dev->ethtool_ops->set_wol(dev, &wol);
738}
739
Yuval Mintz80f12ec2012-06-06 17:13:06 +0000740static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
741{
742 struct ethtool_eee edata;
743 int rc;
744
745 if (!dev->ethtool_ops->get_eee)
746 return -EOPNOTSUPP;
747
748 memset(&edata, 0, sizeof(struct ethtool_eee));
749 edata.cmd = ETHTOOL_GEEE;
750 rc = dev->ethtool_ops->get_eee(dev, &edata);
751
752 if (rc)
753 return rc;
754
755 if (copy_to_user(useraddr, &edata, sizeof(edata)))
756 return -EFAULT;
757
758 return 0;
759}
760
761static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
762{
763 struct ethtool_eee edata;
764
765 if (!dev->ethtool_ops->set_eee)
766 return -EOPNOTSUPP;
767
768 if (copy_from_user(&edata, useraddr, sizeof(edata)))
769 return -EFAULT;
770
771 return dev->ethtool_ops->set_eee(dev, &edata);
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static int ethtool_nway_reset(struct net_device *dev)
775{
776 if (!dev->ethtool_ops->nway_reset)
777 return -EOPNOTSUPP;
778
779 return dev->ethtool_ops->nway_reset(dev);
780}
781
Ben Hutchingse596e6e2010-12-09 12:08:35 +0000782static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
783{
784 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
785
786 if (!dev->ethtool_ops->get_link)
787 return -EOPNOTSUPP;
788
789 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
790
791 if (copy_to_user(useraddr, &edata, sizeof(edata)))
792 return -EFAULT;
793 return 0;
794}
795
Ben Hutchings081d0942012-04-12 00:42:12 +0000796static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
797 int (*getter)(struct net_device *,
798 struct ethtool_eeprom *, u8 *),
799 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700802 void __user *userbuf = useraddr + sizeof(eeprom);
803 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700805 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
808 return -EFAULT;
809
810 /* Check for wrap and zero */
811 if (eeprom.offset + eeprom.len <= eeprom.offset)
812 return -EINVAL;
813
814 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +0000815 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EINVAL;
817
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700818 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!data)
820 return -ENOMEM;
821
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700822 bytes_remaining = eeprom.len;
823 while (bytes_remaining > 0) {
824 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Ben Hutchings081d0942012-04-12 00:42:12 +0000826 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700827 if (ret)
828 break;
829 if (copy_to_user(userbuf, data, eeprom.len)) {
830 ret = -EFAULT;
831 break;
832 }
833 userbuf += eeprom.len;
834 eeprom.offset += eeprom.len;
835 bytes_remaining -= eeprom.len;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700838 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
839 eeprom.offset -= eeprom.len;
840 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
841 ret = -EFAULT;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kfree(data);
844 return ret;
845}
846
Ben Hutchings081d0942012-04-12 00:42:12 +0000847static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
848{
849 const struct ethtool_ops *ops = dev->ethtool_ops;
850
851 if (!ops->get_eeprom || !ops->get_eeprom_len)
852 return -EOPNOTSUPP;
853
854 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
855 ops->get_eeprom_len(dev));
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
859{
860 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700861 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700862 void __user *userbuf = useraddr + sizeof(eeprom);
863 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700865 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (!ops->set_eeprom || !ops->get_eeprom_len)
868 return -EOPNOTSUPP;
869
870 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
871 return -EFAULT;
872
873 /* Check for wrap and zero */
874 if (eeprom.offset + eeprom.len <= eeprom.offset)
875 return -EINVAL;
876
877 /* Check for exceeding total eeprom len */
878 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
879 return -EINVAL;
880
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700881 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (!data)
883 return -ENOMEM;
884
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700885 bytes_remaining = eeprom.len;
886 while (bytes_remaining > 0) {
887 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700889 if (copy_from_user(data, userbuf, eeprom.len)) {
890 ret = -EFAULT;
891 break;
892 }
893 ret = ops->set_eeprom(dev, &eeprom, data);
894 if (ret)
895 break;
896 userbuf += eeprom.len;
897 eeprom.offset += eeprom.len;
898 bytes_remaining -= eeprom.len;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 kfree(data);
902 return ret;
903}
904
chavey97f8aef2010-04-07 21:54:42 -0700905static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
906 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Roland Dreier8e557422010-02-11 12:14:23 -0800908 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (!dev->ethtool_ops->get_coalesce)
911 return -EOPNOTSUPP;
912
913 dev->ethtool_ops->get_coalesce(dev, &coalesce);
914
915 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
916 return -EFAULT;
917 return 0;
918}
919
chavey97f8aef2010-04-07 21:54:42 -0700920static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
921 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct ethtool_coalesce coalesce;
924
David S. Millerfa04ae52005-06-06 15:07:19 -0700925 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EOPNOTSUPP;
927
928 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
929 return -EFAULT;
930
931 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
932}
933
934static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
935{
Roland Dreier8e557422010-02-11 12:14:23 -0800936 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!dev->ethtool_ops->get_ringparam)
939 return -EOPNOTSUPP;
940
941 dev->ethtool_ops->get_ringparam(dev, &ringparam);
942
943 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
944 return -EFAULT;
945 return 0;
946}
947
948static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
949{
950 struct ethtool_ringparam ringparam;
951
952 if (!dev->ethtool_ops->set_ringparam)
953 return -EOPNOTSUPP;
954
955 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
956 return -EFAULT;
957
958 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
959}
960
amit salecha8b5933c2011-04-07 01:58:42 +0000961static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
962 void __user *useraddr)
963{
964 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
965
966 if (!dev->ethtool_ops->get_channels)
967 return -EOPNOTSUPP;
968
969 dev->ethtool_ops->get_channels(dev, &channels);
970
971 if (copy_to_user(useraddr, &channels, sizeof(channels)))
972 return -EFAULT;
973 return 0;
974}
975
976static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
977 void __user *useraddr)
978{
979 struct ethtool_channels channels;
980
981 if (!dev->ethtool_ops->set_channels)
982 return -EOPNOTSUPP;
983
984 if (copy_from_user(&channels, useraddr, sizeof(channels)))
985 return -EFAULT;
986
987 return dev->ethtool_ops->set_channels(dev, &channels);
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
991{
992 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
993
994 if (!dev->ethtool_ops->get_pauseparam)
995 return -EOPNOTSUPP;
996
997 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
998
999 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1000 return -EFAULT;
1001 return 0;
1002}
1003
1004static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1005{
1006 struct ethtool_pauseparam pauseparam;
1007
Jeff Garzike1b90c42006-07-17 12:54:40 -04001008 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return -EOPNOTSUPP;
1010
1011 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1012 return -EFAULT;
1013
1014 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1018{
1019 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001020 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001022 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001024 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001025 return -EOPNOTSUPP;
1026
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001027 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001028 if (test_len < 0)
1029 return test_len;
1030 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (copy_from_user(&test, useraddr, sizeof(test)))
1033 return -EFAULT;
1034
Jeff Garzikff03d492007-08-15 16:01:08 -07001035 test.len = test_len;
1036 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!data)
1038 return -ENOMEM;
1039
1040 ops->self_test(dev, &test, data);
1041
1042 ret = -EFAULT;
1043 if (copy_to_user(useraddr, &test, sizeof(test)))
1044 goto out;
1045 useraddr += sizeof(test);
1046 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1047 goto out;
1048 ret = 0;
1049
1050 out:
1051 kfree(data);
1052 return ret;
1053}
1054
1055static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1056{
1057 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 u8 *data;
1059 int ret;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1062 return -EFAULT;
1063
Michał Mirosław340ae162011-02-15 16:59:16 +00001064 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001065 if (ret < 0)
1066 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001067
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001068 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1071 if (!data)
1072 return -ENOMEM;
1073
Michał Mirosław340ae162011-02-15 16:59:16 +00001074 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 ret = -EFAULT;
1077 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1078 goto out;
1079 useraddr += sizeof(gstrings);
1080 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1081 goto out;
1082 ret = 0;
1083
Michał Mirosław340ae162011-02-15 16:59:16 +00001084out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 kfree(data);
1086 return ret;
1087}
1088
1089static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1090{
1091 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001092 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001093 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001094 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001096 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EOPNOTSUPP;
1098
Ben Hutchings68f512f2011-04-02 00:35:15 +01001099 if (busy)
1100 return -EBUSY;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (copy_from_user(&id, useraddr, sizeof(id)))
1103 return -EFAULT;
1104
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001105 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001106 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001107 return rc;
1108
1109 /* Drop the RTNL lock while waiting, but prevent reentry or
1110 * removal of the device.
1111 */
1112 busy = true;
1113 dev_hold(dev);
1114 rtnl_unlock();
1115
1116 if (rc == 0) {
1117 /* Driver will handle this itself */
1118 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001119 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001120 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001121 /* Driver expects to be called at twice the frequency in rc */
1122 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001123
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001124 /* Count down seconds */
1125 do {
1126 /* Count down iterations per second */
1127 i = n;
1128 do {
1129 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001130 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001131 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1132 rtnl_unlock();
1133 if (rc)
1134 break;
1135 schedule_timeout_interruptible(interval);
1136 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001137 } while (!signal_pending(current) &&
1138 (id.data == 0 || --id.data != 0));
1139 }
1140
1141 rtnl_lock();
1142 dev_put(dev);
1143 busy = false;
1144
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001145 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001146 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1150{
1151 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001152 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001154 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001156 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001157 return -EOPNOTSUPP;
1158
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001159 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001160 if (n_stats < 0)
1161 return n_stats;
1162 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1165 return -EFAULT;
1166
Jeff Garzikff03d492007-08-15 16:01:08 -07001167 stats.n_stats = n_stats;
1168 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (!data)
1170 return -ENOMEM;
1171
1172 ops->get_ethtool_stats(dev, &stats, data);
1173
1174 ret = -EFAULT;
1175 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1176 goto out;
1177 useraddr += sizeof(stats);
1178 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1179 goto out;
1180 ret = 0;
1181
1182 out:
1183 kfree(data);
1184 return ret;
1185}
1186
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001187static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001188{
1189 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001190
Matthew Wilcox313674a2007-07-31 14:00:29 -07001191 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001192 return -EFAULT;
1193
Matthew Wilcox313674a2007-07-31 14:00:29 -07001194 if (epaddr.size < dev->addr_len)
1195 return -ETOOSMALL;
1196 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001197
Jon Wetzela6f9a702005-08-20 17:15:54 -07001198 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001199 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001200 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001201 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1202 return -EFAULT;
1203 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001204}
1205
Jeff Garzik13c99b22007-08-15 16:01:56 -07001206static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1207 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001208{
Roland Dreier8e557422010-02-11 12:14:23 -08001209 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001210
Jeff Garzik13c99b22007-08-15 16:01:56 -07001211 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001212 return -EOPNOTSUPP;
1213
Jeff Garzik13c99b22007-08-15 16:01:56 -07001214 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001215
1216 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1217 return -EFAULT;
1218 return 0;
1219}
1220
Jeff Garzik13c99b22007-08-15 16:01:56 -07001221static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1222 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001223{
1224 struct ethtool_value edata;
1225
Jeff Garzik13c99b22007-08-15 16:01:56 -07001226 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001227 return -EOPNOTSUPP;
1228
1229 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1230 return -EFAULT;
1231
Jeff Garzik13c99b22007-08-15 16:01:56 -07001232 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001233 return 0;
1234}
1235
Jeff Garzik13c99b22007-08-15 16:01:56 -07001236static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1237 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001238{
1239 struct ethtool_value edata;
1240
Jeff Garzik13c99b22007-08-15 16:01:56 -07001241 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001242 return -EOPNOTSUPP;
1243
1244 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1245 return -EFAULT;
1246
Jeff Garzik13c99b22007-08-15 16:01:56 -07001247 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001248}
1249
chavey97f8aef2010-04-07 21:54:42 -07001250static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1251 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001252{
1253 struct ethtool_flash efl;
1254
1255 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1256 return -EFAULT;
1257
1258 if (!dev->ethtool_ops->flash_device)
1259 return -EOPNOTSUPP;
1260
Ben Hutchings786f5282012-02-01 09:32:25 +00001261 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
1262
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001263 return dev->ethtool_ops->flash_device(dev, &efl);
1264}
1265
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001266static int ethtool_set_dump(struct net_device *dev,
1267 void __user *useraddr)
1268{
1269 struct ethtool_dump dump;
1270
1271 if (!dev->ethtool_ops->set_dump)
1272 return -EOPNOTSUPP;
1273
1274 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1275 return -EFAULT;
1276
1277 return dev->ethtool_ops->set_dump(dev, &dump);
1278}
1279
1280static int ethtool_get_dump_flag(struct net_device *dev,
1281 void __user *useraddr)
1282{
1283 int ret;
1284 struct ethtool_dump dump;
1285 const struct ethtool_ops *ops = dev->ethtool_ops;
1286
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001287 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001288 return -EOPNOTSUPP;
1289
1290 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1291 return -EFAULT;
1292
1293 ret = ops->get_dump_flag(dev, &dump);
1294 if (ret)
1295 return ret;
1296
1297 if (copy_to_user(useraddr, &dump, sizeof(dump)))
1298 return -EFAULT;
1299 return 0;
1300}
1301
1302static int ethtool_get_dump_data(struct net_device *dev,
1303 void __user *useraddr)
1304{
1305 int ret;
1306 __u32 len;
1307 struct ethtool_dump dump, tmp;
1308 const struct ethtool_ops *ops = dev->ethtool_ops;
1309 void *data = NULL;
1310
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001311 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001312 return -EOPNOTSUPP;
1313
1314 if (copy_from_user(&dump, useraddr, sizeof(dump)))
1315 return -EFAULT;
1316
1317 memset(&tmp, 0, sizeof(tmp));
1318 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
1319 ret = ops->get_dump_flag(dev, &tmp);
1320 if (ret)
1321 return ret;
1322
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001323 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001324 if (!len)
1325 return -EFAULT;
1326
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001327 /* Don't ever let the driver think there's more space available
1328 * than it requested with .get_dump_flag().
1329 */
1330 dump.len = len;
1331
1332 /* Always allocate enough space to hold the whole thing so that the
1333 * driver does not need to check the length and bother with partial
1334 * dumping.
1335 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001336 data = vzalloc(tmp.len);
1337 if (!data)
1338 return -ENOMEM;
1339 ret = ops->get_dump_data(dev, &dump, data);
1340 if (ret)
1341 goto out;
1342
Michal Schmidtc590b5e2013-07-01 17:23:30 +02001343 /* There are two sane possibilities:
1344 * 1. The driver's .get_dump_data() does not touch dump.len.
1345 * 2. Or it may set dump.len to how much it really writes, which
1346 * should be tmp.len (or len if it can do a partial dump).
1347 * In any case respond to userspace with the actual length of data
1348 * it's receiving.
1349 */
1350 WARN_ON(dump.len != len && dump.len != tmp.len);
1351 dump.len = len;
1352
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001353 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
1354 ret = -EFAULT;
1355 goto out;
1356 }
1357 useraddr += offsetof(struct ethtool_dump, data);
1358 if (copy_to_user(useraddr, data, len))
1359 ret = -EFAULT;
1360out:
1361 vfree(data);
1362 return ret;
1363}
1364
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001365static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
1366{
1367 int err = 0;
1368 struct ethtool_ts_info info;
1369 const struct ethtool_ops *ops = dev->ethtool_ops;
1370 struct phy_device *phydev = dev->phydev;
1371
1372 memset(&info, 0, sizeof(info));
1373 info.cmd = ETHTOOL_GET_TS_INFO;
1374
1375 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001376 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001377 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001378 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001379 } else {
1380 info.so_timestamping =
1381 SOF_TIMESTAMPING_RX_SOFTWARE |
1382 SOF_TIMESTAMPING_SOFTWARE;
1383 info.phc_index = -1;
1384 }
1385
1386 if (err)
1387 return err;
1388
1389 if (copy_to_user(useraddr, &info, sizeof(info)))
1390 err = -EFAULT;
1391
1392 return err;
1393}
1394
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001395static int ethtool_get_module_info(struct net_device *dev,
1396 void __user *useraddr)
1397{
1398 int ret;
1399 struct ethtool_modinfo modinfo;
1400 const struct ethtool_ops *ops = dev->ethtool_ops;
1401
1402 if (!ops->get_module_info)
1403 return -EOPNOTSUPP;
1404
1405 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
1406 return -EFAULT;
1407
1408 ret = ops->get_module_info(dev, &modinfo);
1409 if (ret)
1410 return ret;
1411
1412 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
1413 return -EFAULT;
1414
1415 return 0;
1416}
1417
1418static int ethtool_get_module_eeprom(struct net_device *dev,
1419 void __user *useraddr)
1420{
1421 int ret;
1422 struct ethtool_modinfo modinfo;
1423 const struct ethtool_ops *ops = dev->ethtool_ops;
1424
1425 if (!ops->get_module_info || !ops->get_module_eeprom)
1426 return -EOPNOTSUPP;
1427
1428 ret = ops->get_module_info(dev, &modinfo);
1429 if (ret)
1430 return ret;
1431
1432 return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom,
1433 modinfo.eeprom_len);
1434}
1435
Yan Burman600fed52013-06-03 02:03:34 +00001436/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Eric W. Biederman881d9662007-09-17 11:56:21 -07001438int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001440 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 void __user *useraddr = ifr->ifr_data;
1442 u32 ethcmd;
1443 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00001444 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (!dev || !netif_device_present(dev))
1447 return -ENODEV;
1448
chavey97f8aef2010-04-07 21:54:42 -07001449 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EFAULT;
1451
Stephen Hemminger75f31232006-09-28 15:13:37 -07001452 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001453 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001454 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001455 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001456 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001457 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001458 case ETHTOOL_GCOALESCE:
1459 case ETHTOOL_GRINGPARAM:
1460 case ETHTOOL_GPAUSEPARAM:
1461 case ETHTOOL_GRXCSUM:
1462 case ETHTOOL_GTXCSUM:
1463 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00001464 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001465 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001466 case ETHTOOL_GSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001467 case ETHTOOL_GTSO:
1468 case ETHTOOL_GPERMADDR:
1469 case ETHTOOL_GUFO:
1470 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001471 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001472 case ETHTOOL_GFLAGS:
1473 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001474 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001475 case ETHTOOL_GRXRINGS:
1476 case ETHTOOL_GRXCLSRLCNT:
1477 case ETHTOOL_GRXCLSRULE:
1478 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001479 case ETHTOOL_GRXFHINDIR:
Michał Mirosław5455c692011-02-15 16:59:17 +00001480 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001481 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001482 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00001483 case ETHTOOL_GEEE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001484 break;
1485 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00001486 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07001487 return -EPERM;
1488 }
1489
chavey97f8aef2010-04-07 21:54:42 -07001490 if (dev->ethtool_ops->begin) {
1491 rc = dev->ethtool_ops->begin(dev);
1492 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001494 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001495 old_features = dev->features;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 switch (ethcmd) {
1498 case ETHTOOL_GSET:
1499 rc = ethtool_get_settings(dev, useraddr);
1500 break;
1501 case ETHTOOL_SSET:
1502 rc = ethtool_set_settings(dev, useraddr);
1503 break;
1504 case ETHTOOL_GDRVINFO:
1505 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
1507 case ETHTOOL_GREGS:
1508 rc = ethtool_get_regs(dev, useraddr);
1509 break;
1510 case ETHTOOL_GWOL:
1511 rc = ethtool_get_wol(dev, useraddr);
1512 break;
1513 case ETHTOOL_SWOL:
1514 rc = ethtool_set_wol(dev, useraddr);
1515 break;
1516 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001517 rc = ethtool_get_value(dev, useraddr, ethcmd,
1518 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
1520 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001521 rc = ethtool_set_value_void(dev, useraddr,
1522 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001524 case ETHTOOL_GEEE:
1525 rc = ethtool_get_eee(dev, useraddr);
1526 break;
1527 case ETHTOOL_SEEE:
1528 rc = ethtool_set_eee(dev, useraddr);
1529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 case ETHTOOL_NWAY_RST:
1531 rc = ethtool_nway_reset(dev);
1532 break;
1533 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001534 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536 case ETHTOOL_GEEPROM:
1537 rc = ethtool_get_eeprom(dev, useraddr);
1538 break;
1539 case ETHTOOL_SEEPROM:
1540 rc = ethtool_set_eeprom(dev, useraddr);
1541 break;
1542 case ETHTOOL_GCOALESCE:
1543 rc = ethtool_get_coalesce(dev, useraddr);
1544 break;
1545 case ETHTOOL_SCOALESCE:
1546 rc = ethtool_set_coalesce(dev, useraddr);
1547 break;
1548 case ETHTOOL_GRINGPARAM:
1549 rc = ethtool_get_ringparam(dev, useraddr);
1550 break;
1551 case ETHTOOL_SRINGPARAM:
1552 rc = ethtool_set_ringparam(dev, useraddr);
1553 break;
1554 case ETHTOOL_GPAUSEPARAM:
1555 rc = ethtool_get_pauseparam(dev, useraddr);
1556 break;
1557 case ETHTOOL_SPAUSEPARAM:
1558 rc = ethtool_set_pauseparam(dev, useraddr);
1559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 case ETHTOOL_TEST:
1561 rc = ethtool_self_test(dev, useraddr);
1562 break;
1563 case ETHTOOL_GSTRINGS:
1564 rc = ethtool_get_strings(dev, useraddr);
1565 break;
1566 case ETHTOOL_PHYS_ID:
1567 rc = ethtool_phys_id(dev, useraddr);
1568 break;
1569 case ETHTOOL_GSTATS:
1570 rc = ethtool_get_stats(dev, useraddr);
1571 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001572 case ETHTOOL_GPERMADDR:
1573 rc = ethtool_get_perm_addr(dev, useraddr);
1574 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001575 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001576 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00001577 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001578 break;
1579 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00001580 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001581 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001582 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001583 rc = ethtool_get_value(dev, useraddr, ethcmd,
1584 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001585 break;
1586 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001587 rc = ethtool_set_value(dev, useraddr,
1588 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001589 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001590 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001591 case ETHTOOL_GRXRINGS:
1592 case ETHTOOL_GRXCLSRLCNT:
1593 case ETHTOOL_GRXCLSRULE:
1594 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001595 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001596 break;
1597 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001598 case ETHTOOL_SRXCLSRLDEL:
1599 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001600 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001601 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001602 case ETHTOOL_FLASHDEV:
1603 rc = ethtool_flash_device(dev, useraddr);
1604 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001605 case ETHTOOL_RESET:
1606 rc = ethtool_reset(dev, useraddr);
1607 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001608 case ETHTOOL_GSSET_INFO:
1609 rc = ethtool_get_sset_info(dev, useraddr);
1610 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001611 case ETHTOOL_GRXFHINDIR:
1612 rc = ethtool_get_rxfh_indir(dev, useraddr);
1613 break;
1614 case ETHTOOL_SRXFHINDIR:
1615 rc = ethtool_set_rxfh_indir(dev, useraddr);
1616 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00001617 case ETHTOOL_GFEATURES:
1618 rc = ethtool_get_features(dev, useraddr);
1619 break;
1620 case ETHTOOL_SFEATURES:
1621 rc = ethtool_set_features(dev, useraddr);
1622 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00001623 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001624 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001625 case ETHTOOL_GSG:
1626 case ETHTOOL_GTSO:
1627 case ETHTOOL_GUFO:
1628 case ETHTOOL_GGSO:
1629 case ETHTOOL_GGRO:
1630 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
1631 break;
1632 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00001633 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00001634 case ETHTOOL_SSG:
1635 case ETHTOOL_STSO:
1636 case ETHTOOL_SUFO:
1637 case ETHTOOL_SGSO:
1638 case ETHTOOL_SGRO:
1639 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
1640 break;
amit salecha8b5933c2011-04-07 01:58:42 +00001641 case ETHTOOL_GCHANNELS:
1642 rc = ethtool_get_channels(dev, useraddr);
1643 break;
1644 case ETHTOOL_SCHANNELS:
1645 rc = ethtool_set_channels(dev, useraddr);
1646 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00001647 case ETHTOOL_SET_DUMP:
1648 rc = ethtool_set_dump(dev, useraddr);
1649 break;
1650 case ETHTOOL_GET_DUMP_FLAG:
1651 rc = ethtool_get_dump_flag(dev, useraddr);
1652 break;
1653 case ETHTOOL_GET_DUMP_DATA:
1654 rc = ethtool_get_dump_data(dev, useraddr);
1655 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00001656 case ETHTOOL_GET_TS_INFO:
1657 rc = ethtool_get_ts_info(dev, useraddr);
1658 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01001659 case ETHTOOL_GMODULEINFO:
1660 rc = ethtool_get_module_info(dev, useraddr);
1661 break;
1662 case ETHTOOL_GMODULEEEPROM:
1663 rc = ethtool_get_module_eeprom(dev, useraddr);
1664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001666 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001668
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001669 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001671
1672 if (old_features != dev->features)
1673 netdev_features_change(dev);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}