Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | |
61a44b9c | 6 | * the information ethtool needs. |
1da177e4 | 7 | * |
61a44b9c MW |
8 | * 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. | |
1da177e4 LT |
12 | */ |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/types.h> | |
4fc268d2 | 16 | #include <linux/capability.h> |
1da177e4 LT |
17 | #include <linux/errno.h> |
18 | #include <linux/ethtool.h> | |
19 | #include <linux/netdevice.h> | |
c8f3a8c3 RC |
20 | #include <linux/net_tstamp.h> |
21 | #include <linux/phy.h> | |
d17792eb | 22 | #include <linux/bitops.h> |
97f8aefb | 23 | #include <linux/uaccess.h> |
73da16c2 | 24 | #include <linux/vmalloc.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
68f512f2 BH |
26 | #include <linux/rtnetlink.h> |
27 | #include <linux/sched.h> | |
1da177e4 | 28 | |
4ec93edb | 29 | /* |
1da177e4 LT |
30 | * 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 | ||
35 | u32 ethtool_op_get_link(struct net_device *dev) | |
36 | { | |
37 | return netif_carrier_ok(dev) ? 1 : 0; | |
38 | } | |
97f8aefb | 39 | EXPORT_SYMBOL(ethtool_op_get_link); |
1da177e4 | 40 | |
02eacbd0 RC |
41 | int 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 | } | |
50 | EXPORT_SYMBOL(ethtool_op_get_ts_info); | |
51 | ||
1da177e4 LT |
52 | /* Handlers for each ethtool command */ |
53 | ||
475414f6 | 54 | #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32) |
5455c699 | 55 | |
9d921549 MM |
56 | static 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", | |
9d921549 MM |
59 | [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", | |
788dfcac | 63 | [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert", |
9d921549 | 64 | |
788dfcac FLVC |
65 | [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse", |
66 | [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter", | |
2d6577f1 DM |
67 | [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", | |
9d921549 MM |
70 | [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", | |
68c33163 | 83 | [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation", |
cb32f511 | 84 | [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation", |
61c1db7f | 85 | [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation", |
73136267 | 86 | [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation", |
0d89d203 | 87 | [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation", |
9d921549 MM |
88 | |
89 | [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc", | |
90 | [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp", | |
91 | [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu", | |
92 | [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter", | |
93 | [NETIF_F_RXHASH_BIT] = "rx-hashing", | |
94 | [NETIF_F_RXCSUM_BIT] = "rx-checksum", | |
95 | [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy", | |
96 | [NETIF_F_LOOPBACK_BIT] = "loopback", | |
36eabda3 | 97 | [NETIF_F_RXFCS_BIT] = "rx-fcs", |
5e0c03c8 | 98 | [NETIF_F_RXALL_BIT] = "rx-all", |
a6cc0cfa | 99 | [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload", |
d0290214 | 100 | [NETIF_F_BUSY_POLL_BIT] = "busy-poll", |
9d921549 MM |
101 | }; |
102 | ||
5455c699 MM |
103 | static int ethtool_get_features(struct net_device *dev, void __user *useraddr) |
104 | { | |
105 | struct ethtool_gfeatures cmd = { | |
106 | .cmd = ETHTOOL_GFEATURES, | |
107 | .size = ETHTOOL_DEV_FEATURE_WORDS, | |
108 | }; | |
475414f6 | 109 | struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
5455c699 MM |
110 | u32 __user *sizeaddr; |
111 | u32 copy_size; | |
475414f6 MM |
112 | int i; |
113 | ||
114 | /* in case feature bits run out again */ | |
09da71b1 | 115 | BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t)); |
475414f6 MM |
116 | |
117 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { | |
09da71b1 MM |
118 | features[i].available = (u32)(dev->hw_features >> (32 * i)); |
119 | features[i].requested = (u32)(dev->wanted_features >> (32 * i)); | |
120 | features[i].active = (u32)(dev->features >> (32 * i)); | |
121 | features[i].never_changed = | |
122 | (u32)(NETIF_F_NEVER_CHANGE >> (32 * i)); | |
475414f6 | 123 | } |
5455c699 MM |
124 | |
125 | sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size); | |
126 | if (get_user(copy_size, sizeaddr)) | |
127 | return -EFAULT; | |
128 | ||
129 | if (copy_size > ETHTOOL_DEV_FEATURE_WORDS) | |
130 | copy_size = ETHTOOL_DEV_FEATURE_WORDS; | |
131 | ||
132 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) | |
133 | return -EFAULT; | |
134 | useraddr += sizeof(cmd); | |
135 | if (copy_to_user(useraddr, features, copy_size * sizeof(*features))) | |
136 | return -EFAULT; | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | static int ethtool_set_features(struct net_device *dev, void __user *useraddr) | |
142 | { | |
143 | struct ethtool_sfeatures cmd; | |
144 | struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; | |
475414f6 MM |
145 | netdev_features_t wanted = 0, valid = 0; |
146 | int i, ret = 0; | |
5455c699 MM |
147 | |
148 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) | |
149 | return -EFAULT; | |
150 | useraddr += sizeof(cmd); | |
151 | ||
152 | if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS) | |
153 | return -EINVAL; | |
154 | ||
155 | if (copy_from_user(features, useraddr, sizeof(features))) | |
156 | return -EFAULT; | |
157 | ||
475414f6 | 158 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
09da71b1 MM |
159 | valid |= (netdev_features_t)features[i].valid << (32 * i); |
160 | wanted |= (netdev_features_t)features[i].requested << (32 * i); | |
475414f6 MM |
161 | } |
162 | ||
163 | if (valid & ~NETIF_F_ETHTOOL_BITS) | |
5455c699 MM |
164 | return -EINVAL; |
165 | ||
475414f6 MM |
166 | if (valid & ~dev->hw_features) { |
167 | valid &= dev->hw_features; | |
5455c699 MM |
168 | ret |= ETHTOOL_F_UNSUPPORTED; |
169 | } | |
170 | ||
475414f6 MM |
171 | dev->wanted_features &= ~valid; |
172 | dev->wanted_features |= wanted & valid; | |
6cb6a27c | 173 | __netdev_update_features(dev); |
5455c699 | 174 | |
475414f6 | 175 | if ((dev->wanted_features ^ dev->features) & valid) |
5455c699 MM |
176 | ret |= ETHTOOL_F_WISH; |
177 | ||
178 | return ret; | |
179 | } | |
180 | ||
340ae165 MM |
181 | static int __ethtool_get_sset_count(struct net_device *dev, int sset) |
182 | { | |
183 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
184 | ||
5455c699 MM |
185 | if (sset == ETH_SS_FEATURES) |
186 | return ARRAY_SIZE(netdev_features_strings); | |
187 | ||
c03a14e8 | 188 | if (ops->get_sset_count && ops->get_strings) |
340ae165 MM |
189 | return ops->get_sset_count(dev, sset); |
190 | else | |
191 | return -EOPNOTSUPP; | |
192 | } | |
193 | ||
194 | static void __ethtool_get_strings(struct net_device *dev, | |
195 | u32 stringset, u8 *data) | |
196 | { | |
197 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
198 | ||
5455c699 MM |
199 | if (stringset == ETH_SS_FEATURES) |
200 | memcpy(data, netdev_features_strings, | |
201 | sizeof(netdev_features_strings)); | |
202 | else | |
203 | /* ops->get_strings is valid because checked earlier */ | |
204 | ops->get_strings(dev, stringset, data); | |
340ae165 MM |
205 | } |
206 | ||
c8f44aff | 207 | static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd) |
0a417704 MM |
208 | { |
209 | /* feature masks of legacy discrete ethtool ops */ | |
210 | ||
211 | switch (eth_cmd) { | |
212 | case ETHTOOL_GTXCSUM: | |
213 | case ETHTOOL_STXCSUM: | |
214 | return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM; | |
e83d360d MM |
215 | case ETHTOOL_GRXCSUM: |
216 | case ETHTOOL_SRXCSUM: | |
217 | return NETIF_F_RXCSUM; | |
0a417704 MM |
218 | case ETHTOOL_GSG: |
219 | case ETHTOOL_SSG: | |
220 | return NETIF_F_SG; | |
221 | case ETHTOOL_GTSO: | |
222 | case ETHTOOL_STSO: | |
223 | return NETIF_F_ALL_TSO; | |
224 | case ETHTOOL_GUFO: | |
225 | case ETHTOOL_SUFO: | |
226 | return NETIF_F_UFO; | |
227 | case ETHTOOL_GGSO: | |
228 | case ETHTOOL_SGSO: | |
229 | return NETIF_F_GSO; | |
230 | case ETHTOOL_GGRO: | |
231 | case ETHTOOL_SGRO: | |
232 | return NETIF_F_GRO; | |
233 | default: | |
234 | BUG(); | |
235 | } | |
236 | } | |
237 | ||
0a417704 MM |
238 | static int ethtool_get_one_feature(struct net_device *dev, |
239 | char __user *useraddr, u32 ethcmd) | |
240 | { | |
c8f44aff | 241 | netdev_features_t mask = ethtool_get_feature_mask(ethcmd); |
0a417704 MM |
242 | struct ethtool_value edata = { |
243 | .cmd = ethcmd, | |
86794881 | 244 | .data = !!(dev->features & mask), |
0a417704 | 245 | }; |
0a417704 | 246 | |
0a417704 MM |
247 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
248 | return -EFAULT; | |
249 | return 0; | |
250 | } | |
251 | ||
0a417704 MM |
252 | static int ethtool_set_one_feature(struct net_device *dev, |
253 | void __user *useraddr, u32 ethcmd) | |
254 | { | |
255 | struct ethtool_value edata; | |
c8f44aff | 256 | netdev_features_t mask; |
0a417704 MM |
257 | |
258 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
259 | return -EFAULT; | |
260 | ||
86794881 MM |
261 | mask = ethtool_get_feature_mask(ethcmd); |
262 | mask &= dev->hw_features; | |
bc5787c6 MM |
263 | if (!mask) |
264 | return -EOPNOTSUPP; | |
86794881 | 265 | |
bc5787c6 MM |
266 | if (edata.data) |
267 | dev->wanted_features |= mask; | |
268 | else | |
269 | dev->wanted_features &= ~mask; | |
86794881 | 270 | |
bc5787c6 | 271 | __netdev_update_features(dev); |
86794881 | 272 | |
bc5787c6 MM |
273 | return 0; |
274 | } | |
275 | ||
02b3a552 MM |
276 | #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \ |
277 | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH) | |
f646968f PM |
278 | #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \ |
279 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \ | |
280 | NETIF_F_RXHASH) | |
bc5787c6 MM |
281 | |
282 | static u32 __ethtool_get_flags(struct net_device *dev) | |
283 | { | |
02b3a552 MM |
284 | u32 flags = 0; |
285 | ||
8a73125c DF |
286 | if (dev->features & NETIF_F_LRO) |
287 | flags |= ETH_FLAG_LRO; | |
288 | if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) | |
289 | flags |= ETH_FLAG_RXVLAN; | |
290 | if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) | |
291 | flags |= ETH_FLAG_TXVLAN; | |
292 | if (dev->features & NETIF_F_NTUPLE) | |
293 | flags |= ETH_FLAG_NTUPLE; | |
294 | if (dev->features & NETIF_F_RXHASH) | |
295 | flags |= ETH_FLAG_RXHASH; | |
02b3a552 MM |
296 | |
297 | return flags; | |
0a417704 MM |
298 | } |
299 | ||
bc5787c6 | 300 | static int __ethtool_set_flags(struct net_device *dev, u32 data) |
da8ac86c | 301 | { |
c8f44aff | 302 | netdev_features_t features = 0, changed; |
da8ac86c | 303 | |
02b3a552 | 304 | if (data & ~ETH_ALL_FLAGS) |
da8ac86c MM |
305 | return -EINVAL; |
306 | ||
8a73125c DF |
307 | if (data & ETH_FLAG_LRO) |
308 | features |= NETIF_F_LRO; | |
309 | if (data & ETH_FLAG_RXVLAN) | |
310 | features |= NETIF_F_HW_VLAN_CTAG_RX; | |
311 | if (data & ETH_FLAG_TXVLAN) | |
312 | features |= NETIF_F_HW_VLAN_CTAG_TX; | |
313 | if (data & ETH_FLAG_NTUPLE) | |
314 | features |= NETIF_F_NTUPLE; | |
315 | if (data & ETH_FLAG_RXHASH) | |
316 | features |= NETIF_F_RXHASH; | |
02b3a552 | 317 | |
da8ac86c | 318 | /* allow changing only bits set in hw_features */ |
02b3a552 | 319 | changed = (features ^ dev->features) & ETH_ALL_FEATURES; |
da8ac86c MM |
320 | if (changed & ~dev->hw_features) |
321 | return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP; | |
322 | ||
323 | dev->wanted_features = | |
02b3a552 | 324 | (dev->wanted_features & ~changed) | (features & changed); |
da8ac86c | 325 | |
6cb6a27c | 326 | __netdev_update_features(dev); |
da8ac86c MM |
327 | |
328 | return 0; | |
329 | } | |
330 | ||
4bc71cb9 | 331 | int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
1da177e4 | 332 | { |
4bc71cb9 | 333 | ASSERT_RTNL(); |
1da177e4 | 334 | |
c03a14e8 | 335 | if (!dev->ethtool_ops->get_settings) |
1da177e4 LT |
336 | return -EOPNOTSUPP; |
337 | ||
4bc71cb9 JP |
338 | memset(cmd, 0, sizeof(struct ethtool_cmd)); |
339 | cmd->cmd = ETHTOOL_GSET; | |
340 | return dev->ethtool_ops->get_settings(dev, cmd); | |
341 | } | |
342 | EXPORT_SYMBOL(__ethtool_get_settings); | |
343 | ||
344 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) | |
345 | { | |
346 | int err; | |
347 | struct ethtool_cmd cmd; | |
348 | ||
349 | err = __ethtool_get_settings(dev, &cmd); | |
1da177e4 LT |
350 | if (err < 0) |
351 | return err; | |
352 | ||
353 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) | |
354 | return -EFAULT; | |
355 | return 0; | |
356 | } | |
357 | ||
358 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) | |
359 | { | |
360 | struct ethtool_cmd cmd; | |
361 | ||
362 | if (!dev->ethtool_ops->set_settings) | |
363 | return -EOPNOTSUPP; | |
364 | ||
365 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) | |
366 | return -EFAULT; | |
367 | ||
368 | return dev->ethtool_ops->set_settings(dev, &cmd); | |
369 | } | |
370 | ||
97f8aefb | 371 | static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev, |
372 | void __user *useraddr) | |
1da177e4 LT |
373 | { |
374 | struct ethtool_drvinfo info; | |
76fd8593 | 375 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 | 376 | |
1da177e4 LT |
377 | memset(&info, 0, sizeof(info)); |
378 | info.cmd = ETHTOOL_GDRVINFO; | |
c03a14e8 | 379 | if (ops->get_drvinfo) { |
01414802 BH |
380 | ops->get_drvinfo(dev, &info); |
381 | } else if (dev->dev.parent && dev->dev.parent->driver) { | |
382 | strlcpy(info.bus_info, dev_name(dev->dev.parent), | |
383 | sizeof(info.bus_info)); | |
384 | strlcpy(info.driver, dev->dev.parent->driver->name, | |
385 | sizeof(info.driver)); | |
386 | } else { | |
387 | return -EOPNOTSUPP; | |
388 | } | |
1da177e4 | 389 | |
723b2f57 JG |
390 | /* |
391 | * this method of obtaining string set info is deprecated; | |
d17792eb | 392 | * Use ETHTOOL_GSSET_INFO instead. |
723b2f57 | 393 | */ |
c03a14e8 | 394 | if (ops->get_sset_count) { |
ff03d49f JG |
395 | int rc; |
396 | ||
397 | rc = ops->get_sset_count(dev, ETH_SS_TEST); | |
398 | if (rc >= 0) | |
399 | info.testinfo_len = rc; | |
400 | rc = ops->get_sset_count(dev, ETH_SS_STATS); | |
401 | if (rc >= 0) | |
402 | info.n_stats = rc; | |
339bf024 JG |
403 | rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); |
404 | if (rc >= 0) | |
405 | info.n_priv_flags = rc; | |
ff03d49f | 406 | } |
c03a14e8 | 407 | if (ops->get_regs_len) |
1da177e4 | 408 | info.regdump_len = ops->get_regs_len(dev); |
c03a14e8 | 409 | if (ops->get_eeprom_len) |
1da177e4 LT |
410 | info.eedump_len = ops->get_eeprom_len(dev); |
411 | ||
412 | if (copy_to_user(useraddr, &info, sizeof(info))) | |
413 | return -EFAULT; | |
414 | return 0; | |
415 | } | |
416 | ||
f5c445ed | 417 | static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev, |
97f8aefb | 418 | void __user *useraddr) |
723b2f57 JG |
419 | { |
420 | struct ethtool_sset_info info; | |
723b2f57 JG |
421 | u64 sset_mask; |
422 | int i, idx = 0, n_bits = 0, ret, rc; | |
423 | u32 *info_buf = NULL; | |
424 | ||
723b2f57 JG |
425 | if (copy_from_user(&info, useraddr, sizeof(info))) |
426 | return -EFAULT; | |
427 | ||
428 | /* store copy of mask, because we zero struct later on */ | |
429 | sset_mask = info.sset_mask; | |
430 | if (!sset_mask) | |
431 | return 0; | |
432 | ||
433 | /* calculate size of return buffer */ | |
d17792eb | 434 | n_bits = hweight64(sset_mask); |
723b2f57 JG |
435 | |
436 | memset(&info, 0, sizeof(info)); | |
437 | info.cmd = ETHTOOL_GSSET_INFO; | |
438 | ||
439 | info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER); | |
440 | if (!info_buf) | |
441 | return -ENOMEM; | |
442 | ||
443 | /* | |
444 | * fill return buffer based on input bitmask and successful | |
445 | * get_sset_count return | |
446 | */ | |
447 | for (i = 0; i < 64; i++) { | |
448 | if (!(sset_mask & (1ULL << i))) | |
449 | continue; | |
450 | ||
340ae165 | 451 | rc = __ethtool_get_sset_count(dev, i); |
723b2f57 JG |
452 | if (rc >= 0) { |
453 | info.sset_mask |= (1ULL << i); | |
454 | info_buf[idx++] = rc; | |
455 | } | |
456 | } | |
457 | ||
458 | ret = -EFAULT; | |
459 | if (copy_to_user(useraddr, &info, sizeof(info))) | |
460 | goto out; | |
461 | ||
462 | useraddr += offsetof(struct ethtool_sset_info, data); | |
463 | if (copy_to_user(useraddr, info_buf, idx * sizeof(u32))) | |
464 | goto out; | |
465 | ||
466 | ret = 0; | |
467 | ||
468 | out: | |
469 | kfree(info_buf); | |
470 | return ret; | |
471 | } | |
472 | ||
97f8aefb | 473 | static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, |
bf988435 | 474 | u32 cmd, void __user *useraddr) |
0853ad66 | 475 | { |
bf988435 BH |
476 | struct ethtool_rxnfc info; |
477 | size_t info_size = sizeof(info); | |
55664f32 | 478 | int rc; |
0853ad66 | 479 | |
59089d8d | 480 | if (!dev->ethtool_ops->set_rxnfc) |
0853ad66 SB |
481 | return -EOPNOTSUPP; |
482 | ||
bf988435 BH |
483 | /* struct ethtool_rxnfc was originally defined for |
484 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data | |
485 | * members. User-space might still be using that | |
486 | * definition. */ | |
487 | if (cmd == ETHTOOL_SRXFH) | |
488 | info_size = (offsetof(struct ethtool_rxnfc, data) + | |
489 | sizeof(info.data)); | |
490 | ||
491 | if (copy_from_user(&info, useraddr, info_size)) | |
0853ad66 SB |
492 | return -EFAULT; |
493 | ||
55664f32 BH |
494 | rc = dev->ethtool_ops->set_rxnfc(dev, &info); |
495 | if (rc) | |
496 | return rc; | |
497 | ||
498 | if (cmd == ETHTOOL_SRXCLSRLINS && | |
499 | copy_to_user(useraddr, &info, info_size)) | |
500 | return -EFAULT; | |
501 | ||
502 | return 0; | |
0853ad66 SB |
503 | } |
504 | ||
97f8aefb | 505 | static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, |
bf988435 | 506 | u32 cmd, void __user *useraddr) |
0853ad66 SB |
507 | { |
508 | struct ethtool_rxnfc info; | |
bf988435 | 509 | size_t info_size = sizeof(info); |
59089d8d SB |
510 | const struct ethtool_ops *ops = dev->ethtool_ops; |
511 | int ret; | |
512 | void *rule_buf = NULL; | |
0853ad66 | 513 | |
59089d8d | 514 | if (!ops->get_rxnfc) |
0853ad66 SB |
515 | return -EOPNOTSUPP; |
516 | ||
bf988435 BH |
517 | /* struct ethtool_rxnfc was originally defined for |
518 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data | |
519 | * members. User-space might still be using that | |
520 | * definition. */ | |
521 | if (cmd == ETHTOOL_GRXFH) | |
522 | info_size = (offsetof(struct ethtool_rxnfc, data) + | |
523 | sizeof(info.data)); | |
524 | ||
525 | if (copy_from_user(&info, useraddr, info_size)) | |
0853ad66 SB |
526 | return -EFAULT; |
527 | ||
59089d8d SB |
528 | if (info.cmd == ETHTOOL_GRXCLSRLALL) { |
529 | if (info.rule_cnt > 0) { | |
db048b69 | 530 | if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32)) |
ae6df5f9 | 531 | rule_buf = kzalloc(info.rule_cnt * sizeof(u32), |
db048b69 | 532 | GFP_USER); |
59089d8d SB |
533 | if (!rule_buf) |
534 | return -ENOMEM; | |
535 | } | |
536 | } | |
0853ad66 | 537 | |
59089d8d SB |
538 | ret = ops->get_rxnfc(dev, &info, rule_buf); |
539 | if (ret < 0) | |
540 | goto err_out; | |
541 | ||
542 | ret = -EFAULT; | |
bf988435 | 543 | if (copy_to_user(useraddr, &info, info_size)) |
59089d8d SB |
544 | goto err_out; |
545 | ||
546 | if (rule_buf) { | |
547 | useraddr += offsetof(struct ethtool_rxnfc, rule_locs); | |
548 | if (copy_to_user(useraddr, rule_buf, | |
549 | info.rule_cnt * sizeof(u32))) | |
550 | goto err_out; | |
551 | } | |
552 | ret = 0; | |
553 | ||
554 | err_out: | |
c9caceca | 555 | kfree(rule_buf); |
59089d8d SB |
556 | |
557 | return ret; | |
0853ad66 SB |
558 | } |
559 | ||
3de0b592 VD |
560 | static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr, |
561 | struct ethtool_rxnfc *rx_rings, | |
562 | u32 size) | |
563 | { | |
564 | int ret = 0, i; | |
565 | ||
566 | if (copy_from_user(indir, useraddr, size * sizeof(indir[0]))) | |
567 | ret = -EFAULT; | |
568 | ||
569 | /* Validate ring indices */ | |
570 | for (i = 0; i < size; i++) { | |
266a1646 | 571 | if (indir[i] >= rx_rings->data) { |
3de0b592 | 572 | ret = -EINVAL; |
266a1646 JS |
573 | break; |
574 | } | |
3de0b592 VD |
575 | } |
576 | return ret; | |
577 | } | |
578 | ||
a5b6ee29 BH |
579 | static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev, |
580 | void __user *useraddr) | |
581 | { | |
7850f63f BH |
582 | u32 user_size, dev_size; |
583 | u32 *indir; | |
a5b6ee29 BH |
584 | int ret; |
585 | ||
7850f63f BH |
586 | if (!dev->ethtool_ops->get_rxfh_indir_size || |
587 | !dev->ethtool_ops->get_rxfh_indir) | |
588 | return -EOPNOTSUPP; | |
589 | dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); | |
590 | if (dev_size == 0) | |
a5b6ee29 BH |
591 | return -EOPNOTSUPP; |
592 | ||
7850f63f | 593 | if (copy_from_user(&user_size, |
a5b6ee29 | 594 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
7850f63f | 595 | sizeof(user_size))) |
a5b6ee29 BH |
596 | return -EFAULT; |
597 | ||
7850f63f BH |
598 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size), |
599 | &dev_size, sizeof(dev_size))) | |
600 | return -EFAULT; | |
601 | ||
602 | /* If the user buffer size is 0, this is just a query for the | |
603 | * device table size. Otherwise, if it's smaller than the | |
604 | * device table size it's an error. | |
605 | */ | |
606 | if (user_size < dev_size) | |
607 | return user_size == 0 ? 0 : -EINVAL; | |
608 | ||
609 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); | |
a5b6ee29 BH |
610 | if (!indir) |
611 | return -ENOMEM; | |
612 | ||
a5b6ee29 BH |
613 | ret = dev->ethtool_ops->get_rxfh_indir(dev, indir); |
614 | if (ret) | |
615 | goto out; | |
616 | ||
7850f63f BH |
617 | if (copy_to_user(useraddr + |
618 | offsetof(struct ethtool_rxfh_indir, ring_index[0]), | |
619 | indir, dev_size * sizeof(indir[0]))) | |
a5b6ee29 BH |
620 | ret = -EFAULT; |
621 | ||
622 | out: | |
623 | kfree(indir); | |
624 | return ret; | |
625 | } | |
626 | ||
627 | static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev, | |
628 | void __user *useraddr) | |
629 | { | |
7850f63f BH |
630 | struct ethtool_rxnfc rx_rings; |
631 | u32 user_size, dev_size, i; | |
632 | u32 *indir; | |
c03a14e8 | 633 | const struct ethtool_ops *ops = dev->ethtool_ops; |
a5b6ee29 | 634 | int ret; |
3de0b592 | 635 | u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]); |
a5b6ee29 | 636 | |
c03a14e8 JP |
637 | if (!ops->get_rxfh_indir_size || !ops->set_rxfh_indir || |
638 | !ops->get_rxnfc) | |
7850f63f | 639 | return -EOPNOTSUPP; |
c03a14e8 JP |
640 | |
641 | dev_size = ops->get_rxfh_indir_size(dev); | |
7850f63f | 642 | if (dev_size == 0) |
a5b6ee29 BH |
643 | return -EOPNOTSUPP; |
644 | ||
7850f63f | 645 | if (copy_from_user(&user_size, |
a5b6ee29 | 646 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
7850f63f | 647 | sizeof(user_size))) |
a5b6ee29 BH |
648 | return -EFAULT; |
649 | ||
278bc429 | 650 | if (user_size != 0 && user_size != dev_size) |
7850f63f BH |
651 | return -EINVAL; |
652 | ||
653 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); | |
a5b6ee29 BH |
654 | if (!indir) |
655 | return -ENOMEM; | |
656 | ||
7850f63f | 657 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
c03a14e8 | 658 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
7850f63f BH |
659 | if (ret) |
660 | goto out; | |
278bc429 BH |
661 | |
662 | if (user_size == 0) { | |
663 | for (i = 0; i < dev_size; i++) | |
664 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); | |
665 | } else { | |
3de0b592 VD |
666 | ret = ethtool_copy_validate_indir(indir, |
667 | useraddr + ringidx_offset, | |
668 | &rx_rings, | |
669 | dev_size); | |
670 | if (ret) | |
671 | goto out; | |
672 | } | |
673 | ||
674 | ret = ops->set_rxfh_indir(dev, indir); | |
675 | ||
676 | out: | |
677 | kfree(indir); | |
678 | return ret; | |
679 | } | |
680 | ||
681 | static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev, | |
682 | void __user *useraddr) | |
683 | { | |
684 | int ret; | |
685 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
686 | u32 user_indir_size = 0, user_key_size = 0; | |
687 | u32 dev_indir_size = 0, dev_key_size = 0; | |
688 | u32 total_size; | |
689 | u32 indir_offset, indir_bytes; | |
690 | u32 key_offset; | |
691 | u32 *indir = NULL; | |
692 | u8 *hkey = NULL; | |
693 | u8 *rss_config; | |
694 | ||
695 | if (!(dev->ethtool_ops->get_rxfh_indir_size || | |
696 | dev->ethtool_ops->get_rxfh_key_size) || | |
697 | !dev->ethtool_ops->get_rxfh) | |
698 | return -EOPNOTSUPP; | |
699 | ||
700 | if (ops->get_rxfh_indir_size) | |
701 | dev_indir_size = ops->get_rxfh_indir_size(dev); | |
702 | ||
703 | indir_offset = offsetof(struct ethtool_rxfh, indir_size); | |
704 | ||
705 | if (copy_from_user(&user_indir_size, | |
706 | useraddr + indir_offset, | |
707 | sizeof(user_indir_size))) | |
708 | return -EFAULT; | |
709 | ||
710 | if (copy_to_user(useraddr + indir_offset, | |
711 | &dev_indir_size, sizeof(dev_indir_size))) | |
712 | return -EFAULT; | |
713 | ||
714 | if (ops->get_rxfh_key_size) | |
715 | dev_key_size = ops->get_rxfh_key_size(dev); | |
716 | ||
717 | if ((dev_key_size + dev_indir_size) == 0) | |
718 | return -EOPNOTSUPP; | |
719 | ||
720 | key_offset = offsetof(struct ethtool_rxfh, key_size); | |
721 | ||
722 | if (copy_from_user(&user_key_size, | |
723 | useraddr + key_offset, | |
724 | sizeof(user_key_size))) | |
725 | return -EFAULT; | |
726 | ||
727 | if (copy_to_user(useraddr + key_offset, | |
728 | &dev_key_size, sizeof(dev_key_size))) | |
729 | return -EFAULT; | |
730 | ||
731 | /* If the user buffer size is 0, this is just a query for the | |
732 | * device table size and key size. Otherwise, if the User size is | |
733 | * not equal to device table size or key size it's an error. | |
734 | */ | |
735 | if (!user_indir_size && !user_key_size) | |
736 | return 0; | |
737 | ||
738 | if ((user_indir_size && (user_indir_size != dev_indir_size)) || | |
739 | (user_key_size && (user_key_size != dev_key_size))) | |
740 | return -EINVAL; | |
741 | ||
742 | indir_bytes = user_indir_size * sizeof(indir[0]); | |
743 | total_size = indir_bytes + user_key_size; | |
744 | rss_config = kzalloc(total_size, GFP_USER); | |
745 | if (!rss_config) | |
746 | return -ENOMEM; | |
747 | ||
748 | if (user_indir_size) | |
749 | indir = (u32 *)rss_config; | |
750 | ||
751 | if (user_key_size) | |
752 | hkey = rss_config + indir_bytes; | |
753 | ||
754 | ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey); | |
755 | if (!ret) { | |
756 | if (copy_to_user(useraddr + | |
757 | offsetof(struct ethtool_rxfh, rss_config[0]), | |
758 | rss_config, total_size)) | |
278bc429 | 759 | ret = -EFAULT; |
3de0b592 VD |
760 | } |
761 | ||
762 | kfree(rss_config); | |
763 | ||
764 | return ret; | |
765 | } | |
766 | ||
767 | static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev, | |
768 | void __user *useraddr) | |
769 | { | |
770 | int ret; | |
771 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
772 | struct ethtool_rxnfc rx_rings; | |
773 | u32 user_indir_size = 0, dev_indir_size = 0, i; | |
774 | u32 user_key_size = 0, dev_key_size = 0; | |
775 | u32 *indir = NULL, indir_bytes = 0; | |
776 | u8 *hkey = NULL; | |
777 | u8 *rss_config; | |
778 | u32 indir_offset, key_offset; | |
779 | u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]); | |
780 | ||
781 | if (!(ops->get_rxfh_indir_size || ops->get_rxfh_key_size) || | |
782 | !ops->get_rxnfc || !ops->set_rxfh) | |
783 | return -EOPNOTSUPP; | |
784 | ||
785 | if (ops->get_rxfh_indir_size) | |
786 | dev_indir_size = ops->get_rxfh_indir_size(dev); | |
787 | ||
788 | indir_offset = offsetof(struct ethtool_rxfh, indir_size); | |
789 | if (copy_from_user(&user_indir_size, | |
790 | useraddr + indir_offset, | |
791 | sizeof(user_indir_size))) | |
792 | return -EFAULT; | |
793 | ||
794 | if (ops->get_rxfh_key_size) | |
795 | dev_key_size = dev->ethtool_ops->get_rxfh_key_size(dev); | |
796 | ||
797 | if ((dev_key_size + dev_indir_size) == 0) | |
798 | return -EOPNOTSUPP; | |
799 | ||
800 | key_offset = offsetof(struct ethtool_rxfh, key_size); | |
801 | if (copy_from_user(&user_key_size, | |
802 | useraddr + key_offset, | |
803 | sizeof(user_key_size))) | |
804 | return -EFAULT; | |
805 | ||
806 | /* If either indir or hash key is valid, proceed further. | |
807 | */ | |
808 | if ((user_indir_size && ((user_indir_size != 0xDEADBEEF) && | |
809 | user_indir_size != dev_indir_size)) || | |
810 | (user_key_size && (user_key_size != dev_key_size))) | |
811 | return -EINVAL; | |
812 | ||
813 | if (user_indir_size != 0xDEADBEEF) | |
814 | indir_bytes = dev_indir_size * sizeof(indir[0]); | |
815 | ||
816 | rss_config = kzalloc(indir_bytes + user_key_size, GFP_USER); | |
817 | if (!rss_config) | |
818 | return -ENOMEM; | |
819 | ||
820 | rx_rings.cmd = ETHTOOL_GRXRINGS; | |
821 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); | |
822 | if (ret) | |
823 | goto out; | |
824 | ||
825 | /* user_indir_size == 0 means reset the indir table to default. | |
826 | * user_indir_size == 0xDEADBEEF means indir setting is not requested. | |
827 | */ | |
828 | if (user_indir_size && user_indir_size != 0xDEADBEEF) { | |
829 | indir = (u32 *)rss_config; | |
830 | ret = ethtool_copy_validate_indir(indir, | |
831 | useraddr + rss_cfg_offset, | |
832 | &rx_rings, | |
833 | user_indir_size); | |
834 | if (ret) | |
7850f63f | 835 | goto out; |
3de0b592 VD |
836 | } else if (user_indir_size == 0) { |
837 | indir = (u32 *)rss_config; | |
838 | for (i = 0; i < dev_indir_size; i++) | |
839 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); | |
840 | } | |
278bc429 | 841 | |
3de0b592 VD |
842 | if (user_key_size) { |
843 | hkey = rss_config + indir_bytes; | |
844 | if (copy_from_user(hkey, | |
845 | useraddr + rss_cfg_offset + indir_bytes, | |
846 | user_key_size)) { | |
847 | ret = -EFAULT; | |
848 | goto out; | |
278bc429 | 849 | } |
7850f63f BH |
850 | } |
851 | ||
3de0b592 | 852 | ret = ops->set_rxfh(dev, indir, hkey); |
a5b6ee29 BH |
853 | |
854 | out: | |
3de0b592 | 855 | kfree(rss_config); |
a5b6ee29 BH |
856 | return ret; |
857 | } | |
858 | ||
1da177e4 LT |
859 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) |
860 | { | |
861 | struct ethtool_regs regs; | |
76fd8593 | 862 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 LT |
863 | void *regbuf; |
864 | int reglen, ret; | |
865 | ||
866 | if (!ops->get_regs || !ops->get_regs_len) | |
867 | return -EOPNOTSUPP; | |
868 | ||
869 | if (copy_from_user(®s, useraddr, sizeof(regs))) | |
870 | return -EFAULT; | |
871 | ||
872 | reglen = ops->get_regs_len(dev); | |
873 | if (regs.len > reglen) | |
874 | regs.len = reglen; | |
875 | ||
b7c7d01a | 876 | regbuf = vzalloc(reglen); |
67ae7cf1 | 877 | if (reglen && !regbuf) |
1da177e4 LT |
878 | return -ENOMEM; |
879 | ||
880 | ops->get_regs(dev, ®s, regbuf); | |
881 | ||
882 | ret = -EFAULT; | |
883 | if (copy_to_user(useraddr, ®s, sizeof(regs))) | |
884 | goto out; | |
885 | useraddr += offsetof(struct ethtool_regs, data); | |
67ae7cf1 | 886 | if (regbuf && copy_to_user(useraddr, regbuf, regs.len)) |
1da177e4 LT |
887 | goto out; |
888 | ret = 0; | |
889 | ||
890 | out: | |
a77f5db3 | 891 | vfree(regbuf); |
1da177e4 LT |
892 | return ret; |
893 | } | |
894 | ||
d73d3a8c BH |
895 | static int ethtool_reset(struct net_device *dev, char __user *useraddr) |
896 | { | |
897 | struct ethtool_value reset; | |
898 | int ret; | |
899 | ||
900 | if (!dev->ethtool_ops->reset) | |
901 | return -EOPNOTSUPP; | |
902 | ||
903 | if (copy_from_user(&reset, useraddr, sizeof(reset))) | |
904 | return -EFAULT; | |
905 | ||
906 | ret = dev->ethtool_ops->reset(dev, &reset.data); | |
907 | if (ret) | |
908 | return ret; | |
909 | ||
910 | if (copy_to_user(useraddr, &reset, sizeof(reset))) | |
911 | return -EFAULT; | |
912 | return 0; | |
913 | } | |
914 | ||
1da177e4 LT |
915 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) |
916 | { | |
8e557421 | 917 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
1da177e4 LT |
918 | |
919 | if (!dev->ethtool_ops->get_wol) | |
920 | return -EOPNOTSUPP; | |
921 | ||
922 | dev->ethtool_ops->get_wol(dev, &wol); | |
923 | ||
924 | if (copy_to_user(useraddr, &wol, sizeof(wol))) | |
925 | return -EFAULT; | |
926 | return 0; | |
927 | } | |
928 | ||
929 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) | |
930 | { | |
931 | struct ethtool_wolinfo wol; | |
932 | ||
933 | if (!dev->ethtool_ops->set_wol) | |
934 | return -EOPNOTSUPP; | |
935 | ||
936 | if (copy_from_user(&wol, useraddr, sizeof(wol))) | |
937 | return -EFAULT; | |
938 | ||
939 | return dev->ethtool_ops->set_wol(dev, &wol); | |
940 | } | |
941 | ||
80f12ecc YM |
942 | static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) |
943 | { | |
944 | struct ethtool_eee edata; | |
945 | int rc; | |
946 | ||
947 | if (!dev->ethtool_ops->get_eee) | |
948 | return -EOPNOTSUPP; | |
949 | ||
950 | memset(&edata, 0, sizeof(struct ethtool_eee)); | |
951 | edata.cmd = ETHTOOL_GEEE; | |
952 | rc = dev->ethtool_ops->get_eee(dev, &edata); | |
953 | ||
954 | if (rc) | |
955 | return rc; | |
956 | ||
957 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
958 | return -EFAULT; | |
959 | ||
960 | return 0; | |
961 | } | |
962 | ||
963 | static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) | |
964 | { | |
965 | struct ethtool_eee edata; | |
966 | ||
967 | if (!dev->ethtool_ops->set_eee) | |
968 | return -EOPNOTSUPP; | |
969 | ||
970 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
971 | return -EFAULT; | |
972 | ||
973 | return dev->ethtool_ops->set_eee(dev, &edata); | |
974 | } | |
975 | ||
1da177e4 LT |
976 | static int ethtool_nway_reset(struct net_device *dev) |
977 | { | |
978 | if (!dev->ethtool_ops->nway_reset) | |
979 | return -EOPNOTSUPP; | |
980 | ||
981 | return dev->ethtool_ops->nway_reset(dev); | |
982 | } | |
983 | ||
e596e6e4 BH |
984 | static int ethtool_get_link(struct net_device *dev, char __user *useraddr) |
985 | { | |
986 | struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; | |
987 | ||
988 | if (!dev->ethtool_ops->get_link) | |
989 | return -EOPNOTSUPP; | |
990 | ||
991 | edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev); | |
992 | ||
993 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
994 | return -EFAULT; | |
995 | return 0; | |
996 | } | |
997 | ||
081d094e BH |
998 | static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, |
999 | int (*getter)(struct net_device *, | |
1000 | struct ethtool_eeprom *, u8 *), | |
1001 | u32 total_len) | |
1da177e4 LT |
1002 | { |
1003 | struct ethtool_eeprom eeprom; | |
b131dd5d MSB |
1004 | void __user *userbuf = useraddr + sizeof(eeprom); |
1005 | u32 bytes_remaining; | |
1da177e4 | 1006 | u8 *data; |
b131dd5d | 1007 | int ret = 0; |
1da177e4 | 1008 | |
1da177e4 LT |
1009 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
1010 | return -EFAULT; | |
1011 | ||
1012 | /* Check for wrap and zero */ | |
1013 | if (eeprom.offset + eeprom.len <= eeprom.offset) | |
1014 | return -EINVAL; | |
1015 | ||
1016 | /* Check for exceeding total eeprom len */ | |
081d094e | 1017 | if (eeprom.offset + eeprom.len > total_len) |
1da177e4 LT |
1018 | return -EINVAL; |
1019 | ||
b131dd5d | 1020 | data = kmalloc(PAGE_SIZE, GFP_USER); |
1da177e4 LT |
1021 | if (!data) |
1022 | return -ENOMEM; | |
1023 | ||
b131dd5d MSB |
1024 | bytes_remaining = eeprom.len; |
1025 | while (bytes_remaining > 0) { | |
1026 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); | |
1027 | ||
081d094e | 1028 | ret = getter(dev, &eeprom, data); |
b131dd5d MSB |
1029 | if (ret) |
1030 | break; | |
1031 | if (copy_to_user(userbuf, data, eeprom.len)) { | |
1032 | ret = -EFAULT; | |
1033 | break; | |
1034 | } | |
1035 | userbuf += eeprom.len; | |
1036 | eeprom.offset += eeprom.len; | |
1037 | bytes_remaining -= eeprom.len; | |
1038 | } | |
1da177e4 | 1039 | |
c5835df9 MSB |
1040 | eeprom.len = userbuf - (useraddr + sizeof(eeprom)); |
1041 | eeprom.offset -= eeprom.len; | |
1042 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) | |
1043 | ret = -EFAULT; | |
1044 | ||
1da177e4 LT |
1045 | kfree(data); |
1046 | return ret; | |
1047 | } | |
1048 | ||
081d094e BH |
1049 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) |
1050 | { | |
1051 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
1052 | ||
1053 | if (!ops->get_eeprom || !ops->get_eeprom_len) | |
1054 | return -EOPNOTSUPP; | |
1055 | ||
1056 | return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, | |
1057 | ops->get_eeprom_len(dev)); | |
1058 | } | |
1059 | ||
1da177e4 LT |
1060 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) |
1061 | { | |
1062 | struct ethtool_eeprom eeprom; | |
76fd8593 | 1063 | const struct ethtool_ops *ops = dev->ethtool_ops; |
b131dd5d MSB |
1064 | void __user *userbuf = useraddr + sizeof(eeprom); |
1065 | u32 bytes_remaining; | |
1da177e4 | 1066 | u8 *data; |
b131dd5d | 1067 | int ret = 0; |
1da177e4 LT |
1068 | |
1069 | if (!ops->set_eeprom || !ops->get_eeprom_len) | |
1070 | return -EOPNOTSUPP; | |
1071 | ||
1072 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) | |
1073 | return -EFAULT; | |
1074 | ||
1075 | /* Check for wrap and zero */ | |
1076 | if (eeprom.offset + eeprom.len <= eeprom.offset) | |
1077 | return -EINVAL; | |
1078 | ||
1079 | /* Check for exceeding total eeprom len */ | |
1080 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) | |
1081 | return -EINVAL; | |
1082 | ||
b131dd5d | 1083 | data = kmalloc(PAGE_SIZE, GFP_USER); |
1da177e4 LT |
1084 | if (!data) |
1085 | return -ENOMEM; | |
1086 | ||
b131dd5d MSB |
1087 | bytes_remaining = eeprom.len; |
1088 | while (bytes_remaining > 0) { | |
1089 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); | |
1090 | ||
1091 | if (copy_from_user(data, userbuf, eeprom.len)) { | |
1092 | ret = -EFAULT; | |
1093 | break; | |
1094 | } | |
1095 | ret = ops->set_eeprom(dev, &eeprom, data); | |
1096 | if (ret) | |
1097 | break; | |
1098 | userbuf += eeprom.len; | |
1099 | eeprom.offset += eeprom.len; | |
1100 | bytes_remaining -= eeprom.len; | |
1101 | } | |
1da177e4 | 1102 | |
1da177e4 LT |
1103 | kfree(data); |
1104 | return ret; | |
1105 | } | |
1106 | ||
97f8aefb | 1107 | static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, |
1108 | void __user *useraddr) | |
1da177e4 | 1109 | { |
8e557421 | 1110 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
1da177e4 LT |
1111 | |
1112 | if (!dev->ethtool_ops->get_coalesce) | |
1113 | return -EOPNOTSUPP; | |
1114 | ||
1115 | dev->ethtool_ops->get_coalesce(dev, &coalesce); | |
1116 | ||
1117 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) | |
1118 | return -EFAULT; | |
1119 | return 0; | |
1120 | } | |
1121 | ||
97f8aefb | 1122 | static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, |
1123 | void __user *useraddr) | |
1da177e4 LT |
1124 | { |
1125 | struct ethtool_coalesce coalesce; | |
1126 | ||
fa04ae5c | 1127 | if (!dev->ethtool_ops->set_coalesce) |
1da177e4 LT |
1128 | return -EOPNOTSUPP; |
1129 | ||
1130 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) | |
1131 | return -EFAULT; | |
1132 | ||
1133 | return dev->ethtool_ops->set_coalesce(dev, &coalesce); | |
1134 | } | |
1135 | ||
1136 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) | |
1137 | { | |
8e557421 | 1138 | struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; |
1da177e4 LT |
1139 | |
1140 | if (!dev->ethtool_ops->get_ringparam) | |
1141 | return -EOPNOTSUPP; | |
1142 | ||
1143 | dev->ethtool_ops->get_ringparam(dev, &ringparam); | |
1144 | ||
1145 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) | |
1146 | return -EFAULT; | |
1147 | return 0; | |
1148 | } | |
1149 | ||
1150 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) | |
1151 | { | |
1152 | struct ethtool_ringparam ringparam; | |
1153 | ||
1154 | if (!dev->ethtool_ops->set_ringparam) | |
1155 | return -EOPNOTSUPP; | |
1156 | ||
1157 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) | |
1158 | return -EFAULT; | |
1159 | ||
1160 | return dev->ethtool_ops->set_ringparam(dev, &ringparam); | |
1161 | } | |
1162 | ||
8b5933c3 | 1163 | static noinline_for_stack int ethtool_get_channels(struct net_device *dev, |
1164 | void __user *useraddr) | |
1165 | { | |
1166 | struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; | |
1167 | ||
1168 | if (!dev->ethtool_ops->get_channels) | |
1169 | return -EOPNOTSUPP; | |
1170 | ||
1171 | dev->ethtool_ops->get_channels(dev, &channels); | |
1172 | ||
1173 | if (copy_to_user(useraddr, &channels, sizeof(channels))) | |
1174 | return -EFAULT; | |
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | static noinline_for_stack int ethtool_set_channels(struct net_device *dev, | |
1179 | void __user *useraddr) | |
1180 | { | |
1181 | struct ethtool_channels channels; | |
1182 | ||
1183 | if (!dev->ethtool_ops->set_channels) | |
1184 | return -EOPNOTSUPP; | |
1185 | ||
1186 | if (copy_from_user(&channels, useraddr, sizeof(channels))) | |
1187 | return -EFAULT; | |
1188 | ||
1189 | return dev->ethtool_ops->set_channels(dev, &channels); | |
1190 | } | |
1191 | ||
1da177e4 LT |
1192 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) |
1193 | { | |
1194 | struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; | |
1195 | ||
1196 | if (!dev->ethtool_ops->get_pauseparam) | |
1197 | return -EOPNOTSUPP; | |
1198 | ||
1199 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); | |
1200 | ||
1201 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) | |
1202 | return -EFAULT; | |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) | |
1207 | { | |
1208 | struct ethtool_pauseparam pauseparam; | |
1209 | ||
e1b90c41 | 1210 | if (!dev->ethtool_ops->set_pauseparam) |
1da177e4 LT |
1211 | return -EOPNOTSUPP; |
1212 | ||
1213 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) | |
1214 | return -EFAULT; | |
1215 | ||
1216 | return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); | |
1217 | } | |
1218 | ||
1da177e4 LT |
1219 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
1220 | { | |
1221 | struct ethtool_test test; | |
76fd8593 | 1222 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 | 1223 | u64 *data; |
ff03d49f | 1224 | int ret, test_len; |
1da177e4 | 1225 | |
a9828ec6 | 1226 | if (!ops->self_test || !ops->get_sset_count) |
1da177e4 LT |
1227 | return -EOPNOTSUPP; |
1228 | ||
a9828ec6 | 1229 | test_len = ops->get_sset_count(dev, ETH_SS_TEST); |
ff03d49f JG |
1230 | if (test_len < 0) |
1231 | return test_len; | |
1232 | WARN_ON(test_len == 0); | |
1233 | ||
1da177e4 LT |
1234 | if (copy_from_user(&test, useraddr, sizeof(test))) |
1235 | return -EFAULT; | |
1236 | ||
ff03d49f JG |
1237 | test.len = test_len; |
1238 | data = kmalloc(test_len * sizeof(u64), GFP_USER); | |
1da177e4 LT |
1239 | if (!data) |
1240 | return -ENOMEM; | |
1241 | ||
1242 | ops->self_test(dev, &test, data); | |
1243 | ||
1244 | ret = -EFAULT; | |
1245 | if (copy_to_user(useraddr, &test, sizeof(test))) | |
1246 | goto out; | |
1247 | useraddr += sizeof(test); | |
1248 | if (copy_to_user(useraddr, data, test.len * sizeof(u64))) | |
1249 | goto out; | |
1250 | ret = 0; | |
1251 | ||
1252 | out: | |
1253 | kfree(data); | |
1254 | return ret; | |
1255 | } | |
1256 | ||
1257 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) | |
1258 | { | |
1259 | struct ethtool_gstrings gstrings; | |
1da177e4 LT |
1260 | u8 *data; |
1261 | int ret; | |
1262 | ||
1da177e4 LT |
1263 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) |
1264 | return -EFAULT; | |
1265 | ||
340ae165 | 1266 | ret = __ethtool_get_sset_count(dev, gstrings.string_set); |
a9828ec6 BH |
1267 | if (ret < 0) |
1268 | return ret; | |
1269 | ||
1270 | gstrings.len = ret; | |
1da177e4 LT |
1271 | |
1272 | data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER); | |
1273 | if (!data) | |
1274 | return -ENOMEM; | |
1275 | ||
340ae165 | 1276 | __ethtool_get_strings(dev, gstrings.string_set, data); |
1da177e4 LT |
1277 | |
1278 | ret = -EFAULT; | |
1279 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) | |
1280 | goto out; | |
1281 | useraddr += sizeof(gstrings); | |
1282 | if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) | |
1283 | goto out; | |
1284 | ret = 0; | |
1285 | ||
340ae165 | 1286 | out: |
1da177e4 LT |
1287 | kfree(data); |
1288 | return ret; | |
1289 | } | |
1290 | ||
1291 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) | |
1292 | { | |
1293 | struct ethtool_value id; | |
68f512f2 | 1294 | static bool busy; |
c03a14e8 | 1295 | const struct ethtool_ops *ops = dev->ethtool_ops; |
68f512f2 | 1296 | int rc; |
1da177e4 | 1297 | |
c03a14e8 | 1298 | if (!ops->set_phys_id) |
1da177e4 LT |
1299 | return -EOPNOTSUPP; |
1300 | ||
68f512f2 BH |
1301 | if (busy) |
1302 | return -EBUSY; | |
1303 | ||
1da177e4 LT |
1304 | if (copy_from_user(&id, useraddr, sizeof(id))) |
1305 | return -EFAULT; | |
1306 | ||
c03a14e8 | 1307 | rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); |
fce55922 | 1308 | if (rc < 0) |
68f512f2 BH |
1309 | return rc; |
1310 | ||
1311 | /* Drop the RTNL lock while waiting, but prevent reentry or | |
1312 | * removal of the device. | |
1313 | */ | |
1314 | busy = true; | |
1315 | dev_hold(dev); | |
1316 | rtnl_unlock(); | |
1317 | ||
1318 | if (rc == 0) { | |
1319 | /* Driver will handle this itself */ | |
1320 | schedule_timeout_interruptible( | |
143780c6 | 1321 | id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); |
68f512f2 | 1322 | } else { |
fce55922 AB |
1323 | /* Driver expects to be called at twice the frequency in rc */ |
1324 | int n = rc * 2, i, interval = HZ / n; | |
1325 | ||
1326 | /* Count down seconds */ | |
68f512f2 | 1327 | do { |
fce55922 AB |
1328 | /* Count down iterations per second */ |
1329 | i = n; | |
1330 | do { | |
1331 | rtnl_lock(); | |
c03a14e8 | 1332 | rc = ops->set_phys_id(dev, |
fce55922 AB |
1333 | (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); |
1334 | rtnl_unlock(); | |
1335 | if (rc) | |
1336 | break; | |
1337 | schedule_timeout_interruptible(interval); | |
1338 | } while (!signal_pending(current) && --i != 0); | |
68f512f2 BH |
1339 | } while (!signal_pending(current) && |
1340 | (id.data == 0 || --id.data != 0)); | |
1341 | } | |
1342 | ||
1343 | rtnl_lock(); | |
1344 | dev_put(dev); | |
1345 | busy = false; | |
1346 | ||
c03a14e8 | 1347 | (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); |
68f512f2 | 1348 | return rc; |
1da177e4 LT |
1349 | } |
1350 | ||
1351 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) | |
1352 | { | |
1353 | struct ethtool_stats stats; | |
76fd8593 | 1354 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 | 1355 | u64 *data; |
ff03d49f | 1356 | int ret, n_stats; |
1da177e4 | 1357 | |
a9828ec6 | 1358 | if (!ops->get_ethtool_stats || !ops->get_sset_count) |
1da177e4 LT |
1359 | return -EOPNOTSUPP; |
1360 | ||
a9828ec6 | 1361 | n_stats = ops->get_sset_count(dev, ETH_SS_STATS); |
ff03d49f JG |
1362 | if (n_stats < 0) |
1363 | return n_stats; | |
1364 | WARN_ON(n_stats == 0); | |
1365 | ||
1da177e4 LT |
1366 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
1367 | return -EFAULT; | |
1368 | ||
ff03d49f JG |
1369 | stats.n_stats = n_stats; |
1370 | data = kmalloc(n_stats * sizeof(u64), GFP_USER); | |
1da177e4 LT |
1371 | if (!data) |
1372 | return -ENOMEM; | |
1373 | ||
1374 | ops->get_ethtool_stats(dev, &stats, data); | |
1375 | ||
1376 | ret = -EFAULT; | |
1377 | if (copy_to_user(useraddr, &stats, sizeof(stats))) | |
1378 | goto out; | |
1379 | useraddr += sizeof(stats); | |
1380 | if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64))) | |
1381 | goto out; | |
1382 | ret = 0; | |
1383 | ||
1384 | out: | |
1385 | kfree(data); | |
1386 | return ret; | |
1387 | } | |
1388 | ||
0bf0519d | 1389 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
a6f9a705 JW |
1390 | { |
1391 | struct ethtool_perm_addr epaddr; | |
a6f9a705 | 1392 | |
313674af | 1393 | if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) |
a6f9a705 JW |
1394 | return -EFAULT; |
1395 | ||
313674af MW |
1396 | if (epaddr.size < dev->addr_len) |
1397 | return -ETOOSMALL; | |
1398 | epaddr.size = dev->addr_len; | |
a6f9a705 | 1399 | |
a6f9a705 | 1400 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) |
313674af | 1401 | return -EFAULT; |
a6f9a705 | 1402 | useraddr += sizeof(epaddr); |
313674af MW |
1403 | if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) |
1404 | return -EFAULT; | |
1405 | return 0; | |
a6f9a705 JW |
1406 | } |
1407 | ||
13c99b24 JG |
1408 | static int ethtool_get_value(struct net_device *dev, char __user *useraddr, |
1409 | u32 cmd, u32 (*actor)(struct net_device *)) | |
3ae7c0b2 | 1410 | { |
8e557421 | 1411 | struct ethtool_value edata = { .cmd = cmd }; |
3ae7c0b2 | 1412 | |
13c99b24 | 1413 | if (!actor) |
3ae7c0b2 JG |
1414 | return -EOPNOTSUPP; |
1415 | ||
13c99b24 | 1416 | edata.data = actor(dev); |
3ae7c0b2 JG |
1417 | |
1418 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
1419 | return -EFAULT; | |
1420 | return 0; | |
1421 | } | |
1422 | ||
13c99b24 JG |
1423 | static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, |
1424 | void (*actor)(struct net_device *, u32)) | |
3ae7c0b2 JG |
1425 | { |
1426 | struct ethtool_value edata; | |
1427 | ||
13c99b24 | 1428 | if (!actor) |
3ae7c0b2 JG |
1429 | return -EOPNOTSUPP; |
1430 | ||
1431 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
1432 | return -EFAULT; | |
1433 | ||
13c99b24 | 1434 | actor(dev, edata.data); |
339bf024 JG |
1435 | return 0; |
1436 | } | |
1437 | ||
13c99b24 JG |
1438 | static int ethtool_set_value(struct net_device *dev, char __user *useraddr, |
1439 | int (*actor)(struct net_device *, u32)) | |
339bf024 JG |
1440 | { |
1441 | struct ethtool_value edata; | |
1442 | ||
13c99b24 | 1443 | if (!actor) |
339bf024 JG |
1444 | return -EOPNOTSUPP; |
1445 | ||
1446 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
1447 | return -EFAULT; | |
1448 | ||
13c99b24 | 1449 | return actor(dev, edata.data); |
339bf024 JG |
1450 | } |
1451 | ||
97f8aefb | 1452 | static noinline_for_stack int ethtool_flash_device(struct net_device *dev, |
1453 | char __user *useraddr) | |
05c6a8d7 AK |
1454 | { |
1455 | struct ethtool_flash efl; | |
1456 | ||
1457 | if (copy_from_user(&efl, useraddr, sizeof(efl))) | |
1458 | return -EFAULT; | |
1459 | ||
1460 | if (!dev->ethtool_ops->flash_device) | |
1461 | return -EOPNOTSUPP; | |
1462 | ||
786f5281 BH |
1463 | efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; |
1464 | ||
05c6a8d7 AK |
1465 | return dev->ethtool_ops->flash_device(dev, &efl); |
1466 | } | |
1467 | ||
29dd54b7 AC |
1468 | static int ethtool_set_dump(struct net_device *dev, |
1469 | void __user *useraddr) | |
1470 | { | |
1471 | struct ethtool_dump dump; | |
1472 | ||
1473 | if (!dev->ethtool_ops->set_dump) | |
1474 | return -EOPNOTSUPP; | |
1475 | ||
1476 | if (copy_from_user(&dump, useraddr, sizeof(dump))) | |
1477 | return -EFAULT; | |
1478 | ||
1479 | return dev->ethtool_ops->set_dump(dev, &dump); | |
1480 | } | |
1481 | ||
1482 | static int ethtool_get_dump_flag(struct net_device *dev, | |
1483 | void __user *useraddr) | |
1484 | { | |
1485 | int ret; | |
1486 | struct ethtool_dump dump; | |
1487 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
1488 | ||
c03a14e8 | 1489 | if (!ops->get_dump_flag) |
29dd54b7 AC |
1490 | return -EOPNOTSUPP; |
1491 | ||
1492 | if (copy_from_user(&dump, useraddr, sizeof(dump))) | |
1493 | return -EFAULT; | |
1494 | ||
1495 | ret = ops->get_dump_flag(dev, &dump); | |
1496 | if (ret) | |
1497 | return ret; | |
1498 | ||
1499 | if (copy_to_user(useraddr, &dump, sizeof(dump))) | |
1500 | return -EFAULT; | |
1501 | return 0; | |
1502 | } | |
1503 | ||
1504 | static int ethtool_get_dump_data(struct net_device *dev, | |
1505 | void __user *useraddr) | |
1506 | { | |
1507 | int ret; | |
1508 | __u32 len; | |
1509 | struct ethtool_dump dump, tmp; | |
1510 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
1511 | void *data = NULL; | |
1512 | ||
c03a14e8 | 1513 | if (!ops->get_dump_data || !ops->get_dump_flag) |
29dd54b7 AC |
1514 | return -EOPNOTSUPP; |
1515 | ||
1516 | if (copy_from_user(&dump, useraddr, sizeof(dump))) | |
1517 | return -EFAULT; | |
1518 | ||
1519 | memset(&tmp, 0, sizeof(tmp)); | |
1520 | tmp.cmd = ETHTOOL_GET_DUMP_FLAG; | |
1521 | ret = ops->get_dump_flag(dev, &tmp); | |
1522 | if (ret) | |
1523 | return ret; | |
1524 | ||
c590b5e2 | 1525 | len = min(tmp.len, dump.len); |
29dd54b7 AC |
1526 | if (!len) |
1527 | return -EFAULT; | |
1528 | ||
c590b5e2 MS |
1529 | /* Don't ever let the driver think there's more space available |
1530 | * than it requested with .get_dump_flag(). | |
1531 | */ | |
1532 | dump.len = len; | |
1533 | ||
1534 | /* Always allocate enough space to hold the whole thing so that the | |
1535 | * driver does not need to check the length and bother with partial | |
1536 | * dumping. | |
1537 | */ | |
29dd54b7 AC |
1538 | data = vzalloc(tmp.len); |
1539 | if (!data) | |
1540 | return -ENOMEM; | |
1541 | ret = ops->get_dump_data(dev, &dump, data); | |
1542 | if (ret) | |
1543 | goto out; | |
1544 | ||
c590b5e2 MS |
1545 | /* There are two sane possibilities: |
1546 | * 1. The driver's .get_dump_data() does not touch dump.len. | |
1547 | * 2. Or it may set dump.len to how much it really writes, which | |
1548 | * should be tmp.len (or len if it can do a partial dump). | |
1549 | * In any case respond to userspace with the actual length of data | |
1550 | * it's receiving. | |
1551 | */ | |
1552 | WARN_ON(dump.len != len && dump.len != tmp.len); | |
1553 | dump.len = len; | |
1554 | ||
29dd54b7 AC |
1555 | if (copy_to_user(useraddr, &dump, sizeof(dump))) { |
1556 | ret = -EFAULT; | |
1557 | goto out; | |
1558 | } | |
1559 | useraddr += offsetof(struct ethtool_dump, data); | |
1560 | if (copy_to_user(useraddr, data, len)) | |
1561 | ret = -EFAULT; | |
1562 | out: | |
1563 | vfree(data); | |
1564 | return ret; | |
1565 | } | |
1566 | ||
c8f3a8c3 RC |
1567 | static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) |
1568 | { | |
1569 | int err = 0; | |
1570 | struct ethtool_ts_info info; | |
1571 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
1572 | struct phy_device *phydev = dev->phydev; | |
1573 | ||
1574 | memset(&info, 0, sizeof(info)); | |
1575 | info.cmd = ETHTOOL_GET_TS_INFO; | |
1576 | ||
1577 | if (phydev && phydev->drv && phydev->drv->ts_info) { | |
c8f3a8c3 | 1578 | err = phydev->drv->ts_info(phydev, &info); |
c03a14e8 | 1579 | } else if (ops->get_ts_info) { |
c8f3a8c3 | 1580 | err = ops->get_ts_info(dev, &info); |
c8f3a8c3 RC |
1581 | } else { |
1582 | info.so_timestamping = | |
1583 | SOF_TIMESTAMPING_RX_SOFTWARE | | |
1584 | SOF_TIMESTAMPING_SOFTWARE; | |
1585 | info.phc_index = -1; | |
1586 | } | |
1587 | ||
1588 | if (err) | |
1589 | return err; | |
1590 | ||
1591 | if (copy_to_user(useraddr, &info, sizeof(info))) | |
1592 | err = -EFAULT; | |
1593 | ||
1594 | return err; | |
1595 | } | |
1596 | ||
41c3cb6d SH |
1597 | static int ethtool_get_module_info(struct net_device *dev, |
1598 | void __user *useraddr) | |
1599 | { | |
1600 | int ret; | |
1601 | struct ethtool_modinfo modinfo; | |
1602 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
1603 | ||
1604 | if (!ops->get_module_info) | |
1605 | return -EOPNOTSUPP; | |
1606 | ||
1607 | if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) | |
1608 | return -EFAULT; | |
1609 | ||
1610 | ret = ops->get_module_info(dev, &modinfo); | |
1611 | if (ret) | |
1612 | return ret; | |
1613 | ||
1614 | if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) | |
1615 | return -EFAULT; | |
1616 | ||
1617 | return 0; | |
1618 | } | |
1619 | ||
1620 | static int ethtool_get_module_eeprom(struct net_device *dev, | |
1621 | void __user *useraddr) | |
1622 | { | |
1623 | int ret; | |
1624 | struct ethtool_modinfo modinfo; | |
1625 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
1626 | ||
1627 | if (!ops->get_module_info || !ops->get_module_eeprom) | |
1628 | return -EOPNOTSUPP; | |
1629 | ||
1630 | ret = ops->get_module_info(dev, &modinfo); | |
1631 | if (ret) | |
1632 | return ret; | |
1633 | ||
1634 | return ethtool_get_any_eeprom(dev, useraddr, ops->get_module_eeprom, | |
1635 | modinfo.eeprom_len); | |
1636 | } | |
1637 | ||
600fed5e | 1638 | /* The main entry point in this file. Called from net/core/dev_ioctl.c */ |
1da177e4 | 1639 | |
881d966b | 1640 | int dev_ethtool(struct net *net, struct ifreq *ifr) |
1da177e4 | 1641 | { |
881d966b | 1642 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); |
1da177e4 LT |
1643 | void __user *useraddr = ifr->ifr_data; |
1644 | u32 ethcmd; | |
1645 | int rc; | |
b29d3145 | 1646 | netdev_features_t old_features; |
1da177e4 | 1647 | |
1da177e4 LT |
1648 | if (!dev || !netif_device_present(dev)) |
1649 | return -ENODEV; | |
1650 | ||
97f8aefb | 1651 | if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) |
1da177e4 LT |
1652 | return -EFAULT; |
1653 | ||
75f3123c | 1654 | /* Allow some commands to be done by anyone */ |
97f8aefb | 1655 | switch (ethcmd) { |
0fdc100b | 1656 | case ETHTOOL_GSET: |
75f3123c | 1657 | case ETHTOOL_GDRVINFO: |
75f3123c | 1658 | case ETHTOOL_GMSGLVL: |
2da45db2 | 1659 | case ETHTOOL_GLINK: |
75f3123c SH |
1660 | case ETHTOOL_GCOALESCE: |
1661 | case ETHTOOL_GRINGPARAM: | |
1662 | case ETHTOOL_GPAUSEPARAM: | |
1663 | case ETHTOOL_GRXCSUM: | |
1664 | case ETHTOOL_GTXCSUM: | |
1665 | case ETHTOOL_GSG: | |
f80400a2 | 1666 | case ETHTOOL_GSSET_INFO: |
75f3123c | 1667 | case ETHTOOL_GSTRINGS: |
2da45db2 | 1668 | case ETHTOOL_GSTATS: |
75f3123c SH |
1669 | case ETHTOOL_GTSO: |
1670 | case ETHTOOL_GPERMADDR: | |
1671 | case ETHTOOL_GUFO: | |
1672 | case ETHTOOL_GGSO: | |
1cab819b | 1673 | case ETHTOOL_GGRO: |
339bf024 JG |
1674 | case ETHTOOL_GFLAGS: |
1675 | case ETHTOOL_GPFLAGS: | |
0853ad66 | 1676 | case ETHTOOL_GRXFH: |
59089d8d SB |
1677 | case ETHTOOL_GRXRINGS: |
1678 | case ETHTOOL_GRXCLSRLCNT: | |
1679 | case ETHTOOL_GRXCLSRULE: | |
1680 | case ETHTOOL_GRXCLSRLALL: | |
2da45db2 | 1681 | case ETHTOOL_GRXFHINDIR: |
3de0b592 | 1682 | case ETHTOOL_GRSSH: |
5455c699 | 1683 | case ETHTOOL_GFEATURES: |
2da45db2 | 1684 | case ETHTOOL_GCHANNELS: |
c8f3a8c3 | 1685 | case ETHTOOL_GET_TS_INFO: |
2da45db2 | 1686 | case ETHTOOL_GEEE: |
75f3123c SH |
1687 | break; |
1688 | default: | |
5e1fccc0 | 1689 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
75f3123c SH |
1690 | return -EPERM; |
1691 | } | |
1692 | ||
97f8aefb | 1693 | if (dev->ethtool_ops->begin) { |
1694 | rc = dev->ethtool_ops->begin(dev); | |
1695 | if (rc < 0) | |
1da177e4 | 1696 | return rc; |
97f8aefb | 1697 | } |
d8a33ac4 SH |
1698 | old_features = dev->features; |
1699 | ||
1da177e4 LT |
1700 | switch (ethcmd) { |
1701 | case ETHTOOL_GSET: | |
1702 | rc = ethtool_get_settings(dev, useraddr); | |
1703 | break; | |
1704 | case ETHTOOL_SSET: | |
1705 | rc = ethtool_set_settings(dev, useraddr); | |
1706 | break; | |
1707 | case ETHTOOL_GDRVINFO: | |
1708 | rc = ethtool_get_drvinfo(dev, useraddr); | |
1da177e4 LT |
1709 | break; |
1710 | case ETHTOOL_GREGS: | |
1711 | rc = ethtool_get_regs(dev, useraddr); | |
1712 | break; | |
1713 | case ETHTOOL_GWOL: | |
1714 | rc = ethtool_get_wol(dev, useraddr); | |
1715 | break; | |
1716 | case ETHTOOL_SWOL: | |
1717 | rc = ethtool_set_wol(dev, useraddr); | |
1718 | break; | |
1719 | case ETHTOOL_GMSGLVL: | |
13c99b24 JG |
1720 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1721 | dev->ethtool_ops->get_msglevel); | |
1da177e4 LT |
1722 | break; |
1723 | case ETHTOOL_SMSGLVL: | |
13c99b24 JG |
1724 | rc = ethtool_set_value_void(dev, useraddr, |
1725 | dev->ethtool_ops->set_msglevel); | |
1da177e4 | 1726 | break; |
80f12ecc YM |
1727 | case ETHTOOL_GEEE: |
1728 | rc = ethtool_get_eee(dev, useraddr); | |
1729 | break; | |
1730 | case ETHTOOL_SEEE: | |
1731 | rc = ethtool_set_eee(dev, useraddr); | |
1732 | break; | |
1da177e4 LT |
1733 | case ETHTOOL_NWAY_RST: |
1734 | rc = ethtool_nway_reset(dev); | |
1735 | break; | |
1736 | case ETHTOOL_GLINK: | |
e596e6e4 | 1737 | rc = ethtool_get_link(dev, useraddr); |
1da177e4 LT |
1738 | break; |
1739 | case ETHTOOL_GEEPROM: | |
1740 | rc = ethtool_get_eeprom(dev, useraddr); | |
1741 | break; | |
1742 | case ETHTOOL_SEEPROM: | |
1743 | rc = ethtool_set_eeprom(dev, useraddr); | |
1744 | break; | |
1745 | case ETHTOOL_GCOALESCE: | |
1746 | rc = ethtool_get_coalesce(dev, useraddr); | |
1747 | break; | |
1748 | case ETHTOOL_SCOALESCE: | |
1749 | rc = ethtool_set_coalesce(dev, useraddr); | |
1750 | break; | |
1751 | case ETHTOOL_GRINGPARAM: | |
1752 | rc = ethtool_get_ringparam(dev, useraddr); | |
1753 | break; | |
1754 | case ETHTOOL_SRINGPARAM: | |
1755 | rc = ethtool_set_ringparam(dev, useraddr); | |
1756 | break; | |
1757 | case ETHTOOL_GPAUSEPARAM: | |
1758 | rc = ethtool_get_pauseparam(dev, useraddr); | |
1759 | break; | |
1760 | case ETHTOOL_SPAUSEPARAM: | |
1761 | rc = ethtool_set_pauseparam(dev, useraddr); | |
1762 | break; | |
1da177e4 LT |
1763 | case ETHTOOL_TEST: |
1764 | rc = ethtool_self_test(dev, useraddr); | |
1765 | break; | |
1766 | case ETHTOOL_GSTRINGS: | |
1767 | rc = ethtool_get_strings(dev, useraddr); | |
1768 | break; | |
1769 | case ETHTOOL_PHYS_ID: | |
1770 | rc = ethtool_phys_id(dev, useraddr); | |
1771 | break; | |
1772 | case ETHTOOL_GSTATS: | |
1773 | rc = ethtool_get_stats(dev, useraddr); | |
1774 | break; | |
a6f9a705 JW |
1775 | case ETHTOOL_GPERMADDR: |
1776 | rc = ethtool_get_perm_addr(dev, useraddr); | |
1777 | break; | |
3ae7c0b2 | 1778 | case ETHTOOL_GFLAGS: |
13c99b24 | 1779 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
bc5787c6 | 1780 | __ethtool_get_flags); |
3ae7c0b2 JG |
1781 | break; |
1782 | case ETHTOOL_SFLAGS: | |
da8ac86c | 1783 | rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); |
3ae7c0b2 | 1784 | break; |
339bf024 | 1785 | case ETHTOOL_GPFLAGS: |
13c99b24 JG |
1786 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1787 | dev->ethtool_ops->get_priv_flags); | |
339bf024 JG |
1788 | break; |
1789 | case ETHTOOL_SPFLAGS: | |
13c99b24 JG |
1790 | rc = ethtool_set_value(dev, useraddr, |
1791 | dev->ethtool_ops->set_priv_flags); | |
339bf024 | 1792 | break; |
0853ad66 | 1793 | case ETHTOOL_GRXFH: |
59089d8d SB |
1794 | case ETHTOOL_GRXRINGS: |
1795 | case ETHTOOL_GRXCLSRLCNT: | |
1796 | case ETHTOOL_GRXCLSRULE: | |
1797 | case ETHTOOL_GRXCLSRLALL: | |
bf988435 | 1798 | rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); |
0853ad66 SB |
1799 | break; |
1800 | case ETHTOOL_SRXFH: | |
59089d8d SB |
1801 | case ETHTOOL_SRXCLSRLDEL: |
1802 | case ETHTOOL_SRXCLSRLINS: | |
bf988435 | 1803 | rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); |
0853ad66 | 1804 | break; |
05c6a8d7 AK |
1805 | case ETHTOOL_FLASHDEV: |
1806 | rc = ethtool_flash_device(dev, useraddr); | |
1807 | break; | |
d73d3a8c BH |
1808 | case ETHTOOL_RESET: |
1809 | rc = ethtool_reset(dev, useraddr); | |
1810 | break; | |
723b2f57 JG |
1811 | case ETHTOOL_GSSET_INFO: |
1812 | rc = ethtool_get_sset_info(dev, useraddr); | |
1813 | break; | |
a5b6ee29 BH |
1814 | case ETHTOOL_GRXFHINDIR: |
1815 | rc = ethtool_get_rxfh_indir(dev, useraddr); | |
1816 | break; | |
1817 | case ETHTOOL_SRXFHINDIR: | |
1818 | rc = ethtool_set_rxfh_indir(dev, useraddr); | |
1819 | break; | |
3de0b592 VD |
1820 | case ETHTOOL_GRSSH: |
1821 | rc = ethtool_get_rxfh(dev, useraddr); | |
1822 | break; | |
1823 | case ETHTOOL_SRSSH: | |
1824 | rc = ethtool_set_rxfh(dev, useraddr); | |
1825 | break; | |
5455c699 MM |
1826 | case ETHTOOL_GFEATURES: |
1827 | rc = ethtool_get_features(dev, useraddr); | |
1828 | break; | |
1829 | case ETHTOOL_SFEATURES: | |
1830 | rc = ethtool_set_features(dev, useraddr); | |
1831 | break; | |
0a417704 | 1832 | case ETHTOOL_GTXCSUM: |
e83d360d | 1833 | case ETHTOOL_GRXCSUM: |
0a417704 MM |
1834 | case ETHTOOL_GSG: |
1835 | case ETHTOOL_GTSO: | |
1836 | case ETHTOOL_GUFO: | |
1837 | case ETHTOOL_GGSO: | |
1838 | case ETHTOOL_GGRO: | |
1839 | rc = ethtool_get_one_feature(dev, useraddr, ethcmd); | |
1840 | break; | |
1841 | case ETHTOOL_STXCSUM: | |
e83d360d | 1842 | case ETHTOOL_SRXCSUM: |
0a417704 MM |
1843 | case ETHTOOL_SSG: |
1844 | case ETHTOOL_STSO: | |
1845 | case ETHTOOL_SUFO: | |
1846 | case ETHTOOL_SGSO: | |
1847 | case ETHTOOL_SGRO: | |
1848 | rc = ethtool_set_one_feature(dev, useraddr, ethcmd); | |
1849 | break; | |
8b5933c3 | 1850 | case ETHTOOL_GCHANNELS: |
1851 | rc = ethtool_get_channels(dev, useraddr); | |
1852 | break; | |
1853 | case ETHTOOL_SCHANNELS: | |
1854 | rc = ethtool_set_channels(dev, useraddr); | |
1855 | break; | |
29dd54b7 AC |
1856 | case ETHTOOL_SET_DUMP: |
1857 | rc = ethtool_set_dump(dev, useraddr); | |
1858 | break; | |
1859 | case ETHTOOL_GET_DUMP_FLAG: | |
1860 | rc = ethtool_get_dump_flag(dev, useraddr); | |
1861 | break; | |
1862 | case ETHTOOL_GET_DUMP_DATA: | |
1863 | rc = ethtool_get_dump_data(dev, useraddr); | |
1864 | break; | |
c8f3a8c3 RC |
1865 | case ETHTOOL_GET_TS_INFO: |
1866 | rc = ethtool_get_ts_info(dev, useraddr); | |
1867 | break; | |
41c3cb6d SH |
1868 | case ETHTOOL_GMODULEINFO: |
1869 | rc = ethtool_get_module_info(dev, useraddr); | |
1870 | break; | |
1871 | case ETHTOOL_GMODULEEEPROM: | |
1872 | rc = ethtool_get_module_eeprom(dev, useraddr); | |
1873 | break; | |
1da177e4 | 1874 | default: |
61a44b9c | 1875 | rc = -EOPNOTSUPP; |
1da177e4 | 1876 | } |
4ec93edb | 1877 | |
e71a4783 | 1878 | if (dev->ethtool_ops->complete) |
1da177e4 | 1879 | dev->ethtool_ops->complete(dev); |
d8a33ac4 SH |
1880 | |
1881 | if (old_features != dev->features) | |
1882 | netdev_features_change(dev); | |
1883 | ||
1da177e4 | 1884 | return rc; |
1da177e4 | 1885 | } |