]>
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> | |
20 | #include <asm/uaccess.h> | |
21 | ||
4ec93edb | 22 | /* |
1da177e4 LT |
23 | * Some useful ethtool_ops methods that're device independent. |
24 | * If we find that all drivers want to do the same thing here, | |
25 | * we can turn these into dev_() function calls. | |
26 | */ | |
27 | ||
28 | u32 ethtool_op_get_link(struct net_device *dev) | |
29 | { | |
30 | return netif_carrier_ok(dev) ? 1 : 0; | |
31 | } | |
32 | ||
1896e61f SS |
33 | u32 ethtool_op_get_rx_csum(struct net_device *dev) |
34 | { | |
35 | return (dev->features & NETIF_F_ALL_CSUM) != 0; | |
36 | } | |
8a729fce | 37 | EXPORT_SYMBOL(ethtool_op_get_rx_csum); |
1896e61f | 38 | |
1da177e4 LT |
39 | u32 ethtool_op_get_tx_csum(struct net_device *dev) |
40 | { | |
8648b305 | 41 | return (dev->features & NETIF_F_ALL_CSUM) != 0; |
1da177e4 | 42 | } |
8a729fce | 43 | EXPORT_SYMBOL(ethtool_op_get_tx_csum); |
1da177e4 LT |
44 | |
45 | int ethtool_op_set_tx_csum(struct net_device *dev, u32 data) | |
46 | { | |
47 | if (data) | |
48 | dev->features |= NETIF_F_IP_CSUM; | |
49 | else | |
50 | dev->features &= ~NETIF_F_IP_CSUM; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
69f6a0fa JM |
55 | int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data) |
56 | { | |
57 | if (data) | |
58 | dev->features |= NETIF_F_HW_CSUM; | |
59 | else | |
60 | dev->features &= ~NETIF_F_HW_CSUM; | |
61 | ||
62 | return 0; | |
63 | } | |
6460d948 MC |
64 | |
65 | int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data) | |
66 | { | |
67 | if (data) | |
68 | dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; | |
69 | else | |
70 | dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
1da177e4 LT |
75 | u32 ethtool_op_get_sg(struct net_device *dev) |
76 | { | |
77 | return (dev->features & NETIF_F_SG) != 0; | |
78 | } | |
79 | ||
80 | int ethtool_op_set_sg(struct net_device *dev, u32 data) | |
81 | { | |
82 | if (data) | |
83 | dev->features |= NETIF_F_SG; | |
84 | else | |
85 | dev->features &= ~NETIF_F_SG; | |
86 | ||
87 | return 0; | |
88 | } | |
89 | ||
90 | u32 ethtool_op_get_tso(struct net_device *dev) | |
91 | { | |
92 | return (dev->features & NETIF_F_TSO) != 0; | |
93 | } | |
94 | ||
95 | int ethtool_op_set_tso(struct net_device *dev, u32 data) | |
96 | { | |
97 | if (data) | |
98 | dev->features |= NETIF_F_TSO; | |
99 | else | |
100 | dev->features &= ~NETIF_F_TSO; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
e89e9cf5 AR |
105 | u32 ethtool_op_get_ufo(struct net_device *dev) |
106 | { | |
107 | return (dev->features & NETIF_F_UFO) != 0; | |
108 | } | |
109 | ||
110 | int ethtool_op_set_ufo(struct net_device *dev, u32 data) | |
111 | { | |
112 | if (data) | |
113 | dev->features |= NETIF_F_UFO; | |
114 | else | |
115 | dev->features &= ~NETIF_F_UFO; | |
116 | return 0; | |
117 | } | |
118 | ||
3ae7c0b2 JG |
119 | /* the following list of flags are the same as their associated |
120 | * NETIF_F_xxx values in include/linux/netdevice.h | |
121 | */ | |
122 | static const u32 flags_dup_features = | |
15682bc4 | 123 | (ETH_FLAG_LRO | ETH_FLAG_NTUPLE); |
3ae7c0b2 JG |
124 | |
125 | u32 ethtool_op_get_flags(struct net_device *dev) | |
126 | { | |
127 | /* in the future, this function will probably contain additional | |
128 | * handling for flags which are not so easily handled | |
129 | * by a simple masking operation | |
130 | */ | |
131 | ||
132 | return dev->features & flags_dup_features; | |
133 | } | |
134 | ||
135 | int ethtool_op_set_flags(struct net_device *dev, u32 data) | |
136 | { | |
137 | if (data & ETH_FLAG_LRO) | |
138 | dev->features |= NETIF_F_LRO; | |
139 | else | |
140 | dev->features &= ~NETIF_F_LRO; | |
141 | ||
15682bc4 PWJ |
142 | if (data & ETH_FLAG_NTUPLE) |
143 | dev->features |= NETIF_F_NTUPLE; | |
144 | else | |
145 | dev->features &= ~NETIF_F_NTUPLE; | |
146 | ||
3ae7c0b2 JG |
147 | return 0; |
148 | } | |
149 | ||
15682bc4 PWJ |
150 | void ethtool_ntuple_flush(struct net_device *dev) |
151 | { | |
152 | struct ethtool_rx_ntuple_flow_spec_container *fsc, *f; | |
153 | ||
154 | list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) { | |
155 | list_del(&fsc->list); | |
156 | kfree(fsc); | |
157 | } | |
158 | dev->ethtool_ntuple_list.count = 0; | |
159 | } | |
160 | EXPORT_SYMBOL(ethtool_ntuple_flush); | |
161 | ||
1da177e4 LT |
162 | /* Handlers for each ethtool command */ |
163 | ||
164 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) | |
165 | { | |
8e557421 | 166 | struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET }; |
1da177e4 LT |
167 | int err; |
168 | ||
169 | if (!dev->ethtool_ops->get_settings) | |
170 | return -EOPNOTSUPP; | |
171 | ||
172 | err = dev->ethtool_ops->get_settings(dev, &cmd); | |
173 | if (err < 0) | |
174 | return err; | |
175 | ||
176 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) | |
177 | return -EFAULT; | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) | |
182 | { | |
183 | struct ethtool_cmd cmd; | |
184 | ||
185 | if (!dev->ethtool_ops->set_settings) | |
186 | return -EOPNOTSUPP; | |
187 | ||
188 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) | |
189 | return -EFAULT; | |
190 | ||
191 | return dev->ethtool_ops->set_settings(dev, &cmd); | |
192 | } | |
193 | ||
194 | static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr) | |
195 | { | |
196 | struct ethtool_drvinfo info; | |
76fd8593 | 197 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 LT |
198 | |
199 | if (!ops->get_drvinfo) | |
200 | return -EOPNOTSUPP; | |
201 | ||
202 | memset(&info, 0, sizeof(info)); | |
203 | info.cmd = ETHTOOL_GDRVINFO; | |
204 | ops->get_drvinfo(dev, &info); | |
205 | ||
ff03d49f JG |
206 | if (ops->get_sset_count) { |
207 | int rc; | |
208 | ||
209 | rc = ops->get_sset_count(dev, ETH_SS_TEST); | |
210 | if (rc >= 0) | |
211 | info.testinfo_len = rc; | |
212 | rc = ops->get_sset_count(dev, ETH_SS_STATS); | |
213 | if (rc >= 0) | |
214 | info.n_stats = rc; | |
339bf024 JG |
215 | rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); |
216 | if (rc >= 0) | |
217 | info.n_priv_flags = rc; | |
ff03d49f | 218 | } |
1da177e4 LT |
219 | if (ops->get_regs_len) |
220 | info.regdump_len = ops->get_regs_len(dev); | |
221 | if (ops->get_eeprom_len) | |
222 | info.eedump_len = ops->get_eeprom_len(dev); | |
223 | ||
224 | if (copy_to_user(useraddr, &info, sizeof(info))) | |
225 | return -EFAULT; | |
226 | return 0; | |
227 | } | |
228 | ||
59089d8d | 229 | static int ethtool_set_rxnfc(struct net_device *dev, void __user *useraddr) |
0853ad66 SB |
230 | { |
231 | struct ethtool_rxnfc cmd; | |
232 | ||
59089d8d | 233 | if (!dev->ethtool_ops->set_rxnfc) |
0853ad66 SB |
234 | return -EOPNOTSUPP; |
235 | ||
236 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) | |
237 | return -EFAULT; | |
238 | ||
59089d8d | 239 | return dev->ethtool_ops->set_rxnfc(dev, &cmd); |
0853ad66 SB |
240 | } |
241 | ||
59089d8d | 242 | static int ethtool_get_rxnfc(struct net_device *dev, void __user *useraddr) |
0853ad66 SB |
243 | { |
244 | struct ethtool_rxnfc info; | |
59089d8d SB |
245 | const struct ethtool_ops *ops = dev->ethtool_ops; |
246 | int ret; | |
247 | void *rule_buf = NULL; | |
0853ad66 | 248 | |
59089d8d | 249 | if (!ops->get_rxnfc) |
0853ad66 SB |
250 | return -EOPNOTSUPP; |
251 | ||
252 | if (copy_from_user(&info, useraddr, sizeof(info))) | |
253 | return -EFAULT; | |
254 | ||
59089d8d SB |
255 | if (info.cmd == ETHTOOL_GRXCLSRLALL) { |
256 | if (info.rule_cnt > 0) { | |
257 | rule_buf = kmalloc(info.rule_cnt * sizeof(u32), | |
258 | GFP_USER); | |
259 | if (!rule_buf) | |
260 | return -ENOMEM; | |
261 | } | |
262 | } | |
0853ad66 | 263 | |
59089d8d SB |
264 | ret = ops->get_rxnfc(dev, &info, rule_buf); |
265 | if (ret < 0) | |
266 | goto err_out; | |
267 | ||
268 | ret = -EFAULT; | |
0853ad66 | 269 | if (copy_to_user(useraddr, &info, sizeof(info))) |
59089d8d SB |
270 | goto err_out; |
271 | ||
272 | if (rule_buf) { | |
273 | useraddr += offsetof(struct ethtool_rxnfc, rule_locs); | |
274 | if (copy_to_user(useraddr, rule_buf, | |
275 | info.rule_cnt * sizeof(u32))) | |
276 | goto err_out; | |
277 | } | |
278 | ret = 0; | |
279 | ||
280 | err_out: | |
c9caceca | 281 | kfree(rule_buf); |
59089d8d SB |
282 | |
283 | return ret; | |
0853ad66 SB |
284 | } |
285 | ||
e8589118 PW |
286 | static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list, |
287 | struct ethtool_rx_ntuple_flow_spec *spec, | |
288 | struct ethtool_rx_ntuple_flow_spec_container *fsc) | |
15682bc4 | 289 | { |
15682bc4 PWJ |
290 | |
291 | /* don't add filters forever */ | |
e8589118 PW |
292 | if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) { |
293 | /* free the container */ | |
294 | kfree(fsc); | |
295 | return; | |
296 | } | |
15682bc4 PWJ |
297 | |
298 | /* Copy the whole filter over */ | |
299 | fsc->fs.flow_type = spec->flow_type; | |
300 | memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u)); | |
301 | memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u)); | |
302 | ||
303 | fsc->fs.vlan_tag = spec->vlan_tag; | |
304 | fsc->fs.vlan_tag_mask = spec->vlan_tag_mask; | |
305 | fsc->fs.data = spec->data; | |
306 | fsc->fs.data_mask = spec->data_mask; | |
307 | fsc->fs.action = spec->action; | |
308 | ||
309 | /* add to the list */ | |
310 | list_add_tail_rcu(&fsc->list, &list->list); | |
311 | list->count++; | |
15682bc4 PWJ |
312 | } |
313 | ||
314 | static int ethtool_set_rx_ntuple(struct net_device *dev, void __user *useraddr) | |
315 | { | |
316 | struct ethtool_rx_ntuple cmd; | |
317 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
e8589118 | 318 | struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL; |
15682bc4 PWJ |
319 | int ret; |
320 | ||
321 | if (!ops->set_rx_ntuple) | |
322 | return -EOPNOTSUPP; | |
323 | ||
324 | if (!(dev->features & NETIF_F_NTUPLE)) | |
325 | return -EINVAL; | |
326 | ||
327 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) | |
328 | return -EFAULT; | |
329 | ||
15682bc4 PWJ |
330 | /* |
331 | * Cache filter in dev struct for GET operation only if | |
332 | * the underlying driver doesn't have its own GET operation, and | |
e8589118 PW |
333 | * only if the filter was added successfully. First make sure we |
334 | * can allocate the filter, then continue if successful. | |
15682bc4 | 335 | */ |
e8589118 PW |
336 | if (!ops->get_rx_ntuple) { |
337 | fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC); | |
338 | if (!fsc) | |
15682bc4 | 339 | return -ENOMEM; |
e8589118 PW |
340 | } |
341 | ||
342 | ret = ops->set_rx_ntuple(dev, &cmd); | |
343 | if (ret) { | |
344 | kfree(fsc); | |
345 | return ret; | |
346 | } | |
347 | ||
348 | if (!ops->get_rx_ntuple) | |
349 | __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc); | |
15682bc4 PWJ |
350 | |
351 | return ret; | |
352 | } | |
353 | ||
354 | static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr) | |
355 | { | |
356 | struct ethtool_gstrings gstrings; | |
357 | const struct ethtool_ops *ops = dev->ethtool_ops; | |
358 | struct ethtool_rx_ntuple_flow_spec_container *fsc; | |
359 | u8 *data; | |
360 | char *p; | |
361 | int ret, i, num_strings = 0; | |
362 | ||
363 | if (!ops->get_sset_count) | |
364 | return -EOPNOTSUPP; | |
365 | ||
366 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) | |
367 | return -EFAULT; | |
368 | ||
369 | ret = ops->get_sset_count(dev, gstrings.string_set); | |
370 | if (ret < 0) | |
371 | return ret; | |
372 | ||
373 | gstrings.len = ret; | |
374 | ||
375 | data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER); | |
376 | if (!data) | |
377 | return -ENOMEM; | |
378 | ||
379 | if (ops->get_rx_ntuple) { | |
380 | /* driver-specific filter grab */ | |
381 | ret = ops->get_rx_ntuple(dev, gstrings.string_set, data); | |
382 | goto copy; | |
383 | } | |
384 | ||
385 | /* default ethtool filter grab */ | |
386 | i = 0; | |
387 | p = (char *)data; | |
388 | list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) { | |
389 | sprintf(p, "Filter %d:\n", i); | |
390 | p += ETH_GSTRING_LEN; | |
391 | num_strings++; | |
392 | ||
393 | switch (fsc->fs.flow_type) { | |
394 | case TCP_V4_FLOW: | |
395 | sprintf(p, "\tFlow Type: TCP\n"); | |
396 | p += ETH_GSTRING_LEN; | |
397 | num_strings++; | |
398 | break; | |
399 | case UDP_V4_FLOW: | |
400 | sprintf(p, "\tFlow Type: UDP\n"); | |
401 | p += ETH_GSTRING_LEN; | |
402 | num_strings++; | |
403 | break; | |
404 | case SCTP_V4_FLOW: | |
405 | sprintf(p, "\tFlow Type: SCTP\n"); | |
406 | p += ETH_GSTRING_LEN; | |
407 | num_strings++; | |
408 | break; | |
409 | case AH_ESP_V4_FLOW: | |
410 | sprintf(p, "\tFlow Type: AH ESP\n"); | |
411 | p += ETH_GSTRING_LEN; | |
412 | num_strings++; | |
413 | break; | |
414 | case ESP_V4_FLOW: | |
415 | sprintf(p, "\tFlow Type: ESP\n"); | |
416 | p += ETH_GSTRING_LEN; | |
417 | num_strings++; | |
418 | break; | |
419 | case IP_USER_FLOW: | |
420 | sprintf(p, "\tFlow Type: Raw IP\n"); | |
421 | p += ETH_GSTRING_LEN; | |
422 | num_strings++; | |
423 | break; | |
424 | case IPV4_FLOW: | |
425 | sprintf(p, "\tFlow Type: IPv4\n"); | |
426 | p += ETH_GSTRING_LEN; | |
427 | num_strings++; | |
428 | break; | |
429 | default: | |
430 | sprintf(p, "\tFlow Type: Unknown\n"); | |
431 | p += ETH_GSTRING_LEN; | |
432 | num_strings++; | |
433 | goto unknown_filter; | |
434 | }; | |
435 | ||
436 | /* now the rest of the filters */ | |
437 | switch (fsc->fs.flow_type) { | |
438 | case TCP_V4_FLOW: | |
439 | case UDP_V4_FLOW: | |
440 | case SCTP_V4_FLOW: | |
441 | sprintf(p, "\tSrc IP addr: 0x%x\n", | |
442 | fsc->fs.h_u.tcp_ip4_spec.ip4src); | |
443 | p += ETH_GSTRING_LEN; | |
444 | num_strings++; | |
445 | sprintf(p, "\tSrc IP mask: 0x%x\n", | |
446 | fsc->fs.m_u.tcp_ip4_spec.ip4src); | |
447 | p += ETH_GSTRING_LEN; | |
448 | num_strings++; | |
449 | sprintf(p, "\tDest IP addr: 0x%x\n", | |
450 | fsc->fs.h_u.tcp_ip4_spec.ip4dst); | |
451 | p += ETH_GSTRING_LEN; | |
452 | num_strings++; | |
453 | sprintf(p, "\tDest IP mask: 0x%x\n", | |
454 | fsc->fs.m_u.tcp_ip4_spec.ip4dst); | |
455 | p += ETH_GSTRING_LEN; | |
456 | num_strings++; | |
457 | sprintf(p, "\tSrc Port: %d, mask: 0x%x\n", | |
458 | fsc->fs.h_u.tcp_ip4_spec.psrc, | |
459 | fsc->fs.m_u.tcp_ip4_spec.psrc); | |
460 | p += ETH_GSTRING_LEN; | |
461 | num_strings++; | |
462 | sprintf(p, "\tDest Port: %d, mask: 0x%x\n", | |
463 | fsc->fs.h_u.tcp_ip4_spec.pdst, | |
464 | fsc->fs.m_u.tcp_ip4_spec.pdst); | |
465 | p += ETH_GSTRING_LEN; | |
466 | num_strings++; | |
467 | sprintf(p, "\tTOS: %d, mask: 0x%x\n", | |
468 | fsc->fs.h_u.tcp_ip4_spec.tos, | |
469 | fsc->fs.m_u.tcp_ip4_spec.tos); | |
470 | p += ETH_GSTRING_LEN; | |
471 | num_strings++; | |
472 | break; | |
473 | case AH_ESP_V4_FLOW: | |
474 | case ESP_V4_FLOW: | |
475 | sprintf(p, "\tSrc IP addr: 0x%x\n", | |
476 | fsc->fs.h_u.ah_ip4_spec.ip4src); | |
477 | p += ETH_GSTRING_LEN; | |
478 | num_strings++; | |
479 | sprintf(p, "\tSrc IP mask: 0x%x\n", | |
480 | fsc->fs.m_u.ah_ip4_spec.ip4src); | |
481 | p += ETH_GSTRING_LEN; | |
482 | num_strings++; | |
483 | sprintf(p, "\tDest IP addr: 0x%x\n", | |
484 | fsc->fs.h_u.ah_ip4_spec.ip4dst); | |
485 | p += ETH_GSTRING_LEN; | |
486 | num_strings++; | |
487 | sprintf(p, "\tDest IP mask: 0x%x\n", | |
488 | fsc->fs.m_u.ah_ip4_spec.ip4dst); | |
489 | p += ETH_GSTRING_LEN; | |
490 | num_strings++; | |
491 | sprintf(p, "\tSPI: %d, mask: 0x%x\n", | |
492 | fsc->fs.h_u.ah_ip4_spec.spi, | |
493 | fsc->fs.m_u.ah_ip4_spec.spi); | |
494 | p += ETH_GSTRING_LEN; | |
495 | num_strings++; | |
496 | sprintf(p, "\tTOS: %d, mask: 0x%x\n", | |
497 | fsc->fs.h_u.ah_ip4_spec.tos, | |
498 | fsc->fs.m_u.ah_ip4_spec.tos); | |
499 | p += ETH_GSTRING_LEN; | |
500 | num_strings++; | |
501 | break; | |
502 | case IP_USER_FLOW: | |
503 | sprintf(p, "\tSrc IP addr: 0x%x\n", | |
504 | fsc->fs.h_u.raw_ip4_spec.ip4src); | |
505 | p += ETH_GSTRING_LEN; | |
506 | num_strings++; | |
507 | sprintf(p, "\tSrc IP mask: 0x%x\n", | |
508 | fsc->fs.m_u.raw_ip4_spec.ip4src); | |
509 | p += ETH_GSTRING_LEN; | |
510 | num_strings++; | |
511 | sprintf(p, "\tDest IP addr: 0x%x\n", | |
512 | fsc->fs.h_u.raw_ip4_spec.ip4dst); | |
513 | p += ETH_GSTRING_LEN; | |
514 | num_strings++; | |
515 | sprintf(p, "\tDest IP mask: 0x%x\n", | |
516 | fsc->fs.m_u.raw_ip4_spec.ip4dst); | |
517 | p += ETH_GSTRING_LEN; | |
518 | num_strings++; | |
519 | break; | |
520 | case IPV4_FLOW: | |
521 | sprintf(p, "\tSrc IP addr: 0x%x\n", | |
522 | fsc->fs.h_u.usr_ip4_spec.ip4src); | |
523 | p += ETH_GSTRING_LEN; | |
524 | num_strings++; | |
525 | sprintf(p, "\tSrc IP mask: 0x%x\n", | |
526 | fsc->fs.m_u.usr_ip4_spec.ip4src); | |
527 | p += ETH_GSTRING_LEN; | |
528 | num_strings++; | |
529 | sprintf(p, "\tDest IP addr: 0x%x\n", | |
530 | fsc->fs.h_u.usr_ip4_spec.ip4dst); | |
531 | p += ETH_GSTRING_LEN; | |
532 | num_strings++; | |
533 | sprintf(p, "\tDest IP mask: 0x%x\n", | |
534 | fsc->fs.m_u.usr_ip4_spec.ip4dst); | |
535 | p += ETH_GSTRING_LEN; | |
536 | num_strings++; | |
537 | sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n", | |
538 | fsc->fs.h_u.usr_ip4_spec.l4_4_bytes, | |
539 | fsc->fs.m_u.usr_ip4_spec.l4_4_bytes); | |
540 | p += ETH_GSTRING_LEN; | |
541 | num_strings++; | |
542 | sprintf(p, "\tTOS: %d, mask: 0x%x\n", | |
543 | fsc->fs.h_u.usr_ip4_spec.tos, | |
544 | fsc->fs.m_u.usr_ip4_spec.tos); | |
545 | p += ETH_GSTRING_LEN; | |
546 | num_strings++; | |
547 | sprintf(p, "\tIP Version: %d, mask: 0x%x\n", | |
548 | fsc->fs.h_u.usr_ip4_spec.ip_ver, | |
549 | fsc->fs.m_u.usr_ip4_spec.ip_ver); | |
550 | p += ETH_GSTRING_LEN; | |
551 | num_strings++; | |
552 | sprintf(p, "\tProtocol: %d, mask: 0x%x\n", | |
553 | fsc->fs.h_u.usr_ip4_spec.proto, | |
554 | fsc->fs.m_u.usr_ip4_spec.proto); | |
555 | p += ETH_GSTRING_LEN; | |
556 | num_strings++; | |
557 | break; | |
558 | }; | |
559 | sprintf(p, "\tVLAN: %d, mask: 0x%x\n", | |
560 | fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask); | |
561 | p += ETH_GSTRING_LEN; | |
562 | num_strings++; | |
563 | sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data); | |
564 | p += ETH_GSTRING_LEN; | |
565 | num_strings++; | |
566 | sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask); | |
567 | p += ETH_GSTRING_LEN; | |
568 | num_strings++; | |
569 | if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP) | |
570 | sprintf(p, "\tAction: Drop\n"); | |
571 | else | |
572 | sprintf(p, "\tAction: Direct to queue %d\n", | |
573 | fsc->fs.action); | |
574 | p += ETH_GSTRING_LEN; | |
575 | num_strings++; | |
576 | unknown_filter: | |
577 | i++; | |
578 | } | |
579 | copy: | |
580 | /* indicate to userspace how many strings we actually have */ | |
581 | gstrings.len = num_strings; | |
582 | ret = -EFAULT; | |
583 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) | |
584 | goto out; | |
585 | useraddr += sizeof(gstrings); | |
586 | if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) | |
587 | goto out; | |
588 | ret = 0; | |
589 | ||
590 | out: | |
591 | kfree(data); | |
592 | return ret; | |
593 | } | |
594 | ||
1da177e4 LT |
595 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) |
596 | { | |
597 | struct ethtool_regs regs; | |
76fd8593 | 598 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 LT |
599 | void *regbuf; |
600 | int reglen, ret; | |
601 | ||
602 | if (!ops->get_regs || !ops->get_regs_len) | |
603 | return -EOPNOTSUPP; | |
604 | ||
605 | if (copy_from_user(®s, useraddr, sizeof(regs))) | |
606 | return -EFAULT; | |
607 | ||
608 | reglen = ops->get_regs_len(dev); | |
609 | if (regs.len > reglen) | |
610 | regs.len = reglen; | |
611 | ||
612 | regbuf = kmalloc(reglen, GFP_USER); | |
613 | if (!regbuf) | |
614 | return -ENOMEM; | |
615 | ||
616 | ops->get_regs(dev, ®s, regbuf); | |
617 | ||
618 | ret = -EFAULT; | |
619 | if (copy_to_user(useraddr, ®s, sizeof(regs))) | |
620 | goto out; | |
621 | useraddr += offsetof(struct ethtool_regs, data); | |
622 | if (copy_to_user(useraddr, regbuf, regs.len)) | |
623 | goto out; | |
624 | ret = 0; | |
625 | ||
626 | out: | |
627 | kfree(regbuf); | |
628 | return ret; | |
629 | } | |
630 | ||
d73d3a8c BH |
631 | static int ethtool_reset(struct net_device *dev, char __user *useraddr) |
632 | { | |
633 | struct ethtool_value reset; | |
634 | int ret; | |
635 | ||
636 | if (!dev->ethtool_ops->reset) | |
637 | return -EOPNOTSUPP; | |
638 | ||
639 | if (copy_from_user(&reset, useraddr, sizeof(reset))) | |
640 | return -EFAULT; | |
641 | ||
15682bc4 PWJ |
642 | /* Clear ethtool n-tuple list */ |
643 | ethtool_ntuple_flush(dev); | |
644 | ||
d73d3a8c BH |
645 | ret = dev->ethtool_ops->reset(dev, &reset.data); |
646 | if (ret) | |
647 | return ret; | |
648 | ||
649 | if (copy_to_user(useraddr, &reset, sizeof(reset))) | |
650 | return -EFAULT; | |
651 | return 0; | |
652 | } | |
653 | ||
1da177e4 LT |
654 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) |
655 | { | |
8e557421 | 656 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
1da177e4 LT |
657 | |
658 | if (!dev->ethtool_ops->get_wol) | |
659 | return -EOPNOTSUPP; | |
660 | ||
661 | dev->ethtool_ops->get_wol(dev, &wol); | |
662 | ||
663 | if (copy_to_user(useraddr, &wol, sizeof(wol))) | |
664 | return -EFAULT; | |
665 | return 0; | |
666 | } | |
667 | ||
668 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) | |
669 | { | |
670 | struct ethtool_wolinfo wol; | |
671 | ||
672 | if (!dev->ethtool_ops->set_wol) | |
673 | return -EOPNOTSUPP; | |
674 | ||
675 | if (copy_from_user(&wol, useraddr, sizeof(wol))) | |
676 | return -EFAULT; | |
677 | ||
678 | return dev->ethtool_ops->set_wol(dev, &wol); | |
679 | } | |
680 | ||
1da177e4 LT |
681 | static int ethtool_nway_reset(struct net_device *dev) |
682 | { | |
683 | if (!dev->ethtool_ops->nway_reset) | |
684 | return -EOPNOTSUPP; | |
685 | ||
686 | return dev->ethtool_ops->nway_reset(dev); | |
687 | } | |
688 | ||
1da177e4 LT |
689 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) |
690 | { | |
691 | struct ethtool_eeprom eeprom; | |
76fd8593 | 692 | const struct ethtool_ops *ops = dev->ethtool_ops; |
b131dd5d MSB |
693 | void __user *userbuf = useraddr + sizeof(eeprom); |
694 | u32 bytes_remaining; | |
1da177e4 | 695 | u8 *data; |
b131dd5d | 696 | int ret = 0; |
1da177e4 LT |
697 | |
698 | if (!ops->get_eeprom || !ops->get_eeprom_len) | |
699 | return -EOPNOTSUPP; | |
700 | ||
701 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) | |
702 | return -EFAULT; | |
703 | ||
704 | /* Check for wrap and zero */ | |
705 | if (eeprom.offset + eeprom.len <= eeprom.offset) | |
706 | return -EINVAL; | |
707 | ||
708 | /* Check for exceeding total eeprom len */ | |
709 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) | |
710 | return -EINVAL; | |
711 | ||
b131dd5d | 712 | data = kmalloc(PAGE_SIZE, GFP_USER); |
1da177e4 LT |
713 | if (!data) |
714 | return -ENOMEM; | |
715 | ||
b131dd5d MSB |
716 | bytes_remaining = eeprom.len; |
717 | while (bytes_remaining > 0) { | |
718 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); | |
719 | ||
720 | ret = ops->get_eeprom(dev, &eeprom, data); | |
721 | if (ret) | |
722 | break; | |
723 | if (copy_to_user(userbuf, data, eeprom.len)) { | |
724 | ret = -EFAULT; | |
725 | break; | |
726 | } | |
727 | userbuf += eeprom.len; | |
728 | eeprom.offset += eeprom.len; | |
729 | bytes_remaining -= eeprom.len; | |
730 | } | |
1da177e4 | 731 | |
c5835df9 MSB |
732 | eeprom.len = userbuf - (useraddr + sizeof(eeprom)); |
733 | eeprom.offset -= eeprom.len; | |
734 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) | |
735 | ret = -EFAULT; | |
736 | ||
1da177e4 LT |
737 | kfree(data); |
738 | return ret; | |
739 | } | |
740 | ||
741 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) | |
742 | { | |
743 | struct ethtool_eeprom eeprom; | |
76fd8593 | 744 | const struct ethtool_ops *ops = dev->ethtool_ops; |
b131dd5d MSB |
745 | void __user *userbuf = useraddr + sizeof(eeprom); |
746 | u32 bytes_remaining; | |
1da177e4 | 747 | u8 *data; |
b131dd5d | 748 | int ret = 0; |
1da177e4 LT |
749 | |
750 | if (!ops->set_eeprom || !ops->get_eeprom_len) | |
751 | return -EOPNOTSUPP; | |
752 | ||
753 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) | |
754 | return -EFAULT; | |
755 | ||
756 | /* Check for wrap and zero */ | |
757 | if (eeprom.offset + eeprom.len <= eeprom.offset) | |
758 | return -EINVAL; | |
759 | ||
760 | /* Check for exceeding total eeprom len */ | |
761 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) | |
762 | return -EINVAL; | |
763 | ||
b131dd5d | 764 | data = kmalloc(PAGE_SIZE, GFP_USER); |
1da177e4 LT |
765 | if (!data) |
766 | return -ENOMEM; | |
767 | ||
b131dd5d MSB |
768 | bytes_remaining = eeprom.len; |
769 | while (bytes_remaining > 0) { | |
770 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); | |
771 | ||
772 | if (copy_from_user(data, userbuf, eeprom.len)) { | |
773 | ret = -EFAULT; | |
774 | break; | |
775 | } | |
776 | ret = ops->set_eeprom(dev, &eeprom, data); | |
777 | if (ret) | |
778 | break; | |
779 | userbuf += eeprom.len; | |
780 | eeprom.offset += eeprom.len; | |
781 | bytes_remaining -= eeprom.len; | |
782 | } | |
1da177e4 | 783 | |
1da177e4 LT |
784 | kfree(data); |
785 | return ret; | |
786 | } | |
787 | ||
788 | static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr) | |
789 | { | |
8e557421 | 790 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
1da177e4 LT |
791 | |
792 | if (!dev->ethtool_ops->get_coalesce) | |
793 | return -EOPNOTSUPP; | |
794 | ||
795 | dev->ethtool_ops->get_coalesce(dev, &coalesce); | |
796 | ||
797 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) | |
798 | return -EFAULT; | |
799 | return 0; | |
800 | } | |
801 | ||
802 | static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr) | |
803 | { | |
804 | struct ethtool_coalesce coalesce; | |
805 | ||
fa04ae5c | 806 | if (!dev->ethtool_ops->set_coalesce) |
1da177e4 LT |
807 | return -EOPNOTSUPP; |
808 | ||
809 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) | |
810 | return -EFAULT; | |
811 | ||
812 | return dev->ethtool_ops->set_coalesce(dev, &coalesce); | |
813 | } | |
814 | ||
815 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) | |
816 | { | |
8e557421 | 817 | struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; |
1da177e4 LT |
818 | |
819 | if (!dev->ethtool_ops->get_ringparam) | |
820 | return -EOPNOTSUPP; | |
821 | ||
822 | dev->ethtool_ops->get_ringparam(dev, &ringparam); | |
823 | ||
824 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) | |
825 | return -EFAULT; | |
826 | return 0; | |
827 | } | |
828 | ||
829 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) | |
830 | { | |
831 | struct ethtool_ringparam ringparam; | |
832 | ||
833 | if (!dev->ethtool_ops->set_ringparam) | |
834 | return -EOPNOTSUPP; | |
835 | ||
836 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) | |
837 | return -EFAULT; | |
838 | ||
839 | return dev->ethtool_ops->set_ringparam(dev, &ringparam); | |
840 | } | |
841 | ||
842 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) | |
843 | { | |
844 | struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; | |
845 | ||
846 | if (!dev->ethtool_ops->get_pauseparam) | |
847 | return -EOPNOTSUPP; | |
848 | ||
849 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); | |
850 | ||
851 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) | |
852 | return -EFAULT; | |
853 | return 0; | |
854 | } | |
855 | ||
856 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) | |
857 | { | |
858 | struct ethtool_pauseparam pauseparam; | |
859 | ||
e1b90c41 | 860 | if (!dev->ethtool_ops->set_pauseparam) |
1da177e4 LT |
861 | return -EOPNOTSUPP; |
862 | ||
863 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) | |
864 | return -EFAULT; | |
865 | ||
866 | return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); | |
867 | } | |
868 | ||
1da177e4 LT |
869 | static int __ethtool_set_sg(struct net_device *dev, u32 data) |
870 | { | |
871 | int err; | |
872 | ||
873 | if (!data && dev->ethtool_ops->set_tso) { | |
874 | err = dev->ethtool_ops->set_tso(dev, 0); | |
875 | if (err) | |
876 | return err; | |
877 | } | |
878 | ||
e89e9cf5 AR |
879 | if (!data && dev->ethtool_ops->set_ufo) { |
880 | err = dev->ethtool_ops->set_ufo(dev, 0); | |
881 | if (err) | |
882 | return err; | |
883 | } | |
1da177e4 LT |
884 | return dev->ethtool_ops->set_sg(dev, data); |
885 | } | |
886 | ||
887 | static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr) | |
888 | { | |
889 | struct ethtool_value edata; | |
890 | int err; | |
891 | ||
892 | if (!dev->ethtool_ops->set_tx_csum) | |
893 | return -EOPNOTSUPP; | |
894 | ||
895 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
896 | return -EFAULT; | |
897 | ||
898 | if (!edata.data && dev->ethtool_ops->set_sg) { | |
899 | err = __ethtool_set_sg(dev, 0); | |
900 | if (err) | |
901 | return err; | |
902 | } | |
903 | ||
904 | return dev->ethtool_ops->set_tx_csum(dev, edata.data); | |
905 | } | |
906 | ||
b240a0e5 HX |
907 | static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr) |
908 | { | |
909 | struct ethtool_value edata; | |
910 | ||
911 | if (!dev->ethtool_ops->set_rx_csum) | |
912 | return -EOPNOTSUPP; | |
913 | ||
914 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
915 | return -EFAULT; | |
916 | ||
917 | if (!edata.data && dev->ethtool_ops->set_sg) | |
918 | dev->features &= ~NETIF_F_GRO; | |
919 | ||
920 | return dev->ethtool_ops->set_rx_csum(dev, edata.data); | |
921 | } | |
922 | ||
1da177e4 LT |
923 | static int ethtool_set_sg(struct net_device *dev, char __user *useraddr) |
924 | { | |
925 | struct ethtool_value edata; | |
926 | ||
927 | if (!dev->ethtool_ops->set_sg) | |
928 | return -EOPNOTSUPP; | |
929 | ||
930 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
931 | return -EFAULT; | |
932 | ||
4ec93edb | 933 | if (edata.data && |
8648b305 | 934 | !(dev->features & NETIF_F_ALL_CSUM)) |
1da177e4 LT |
935 | return -EINVAL; |
936 | ||
937 | return __ethtool_set_sg(dev, edata.data); | |
938 | } | |
939 | ||
1da177e4 LT |
940 | static int ethtool_set_tso(struct net_device *dev, char __user *useraddr) |
941 | { | |
942 | struct ethtool_value edata; | |
943 | ||
944 | if (!dev->ethtool_ops->set_tso) | |
945 | return -EOPNOTSUPP; | |
946 | ||
947 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
948 | return -EFAULT; | |
949 | ||
950 | if (edata.data && !(dev->features & NETIF_F_SG)) | |
951 | return -EINVAL; | |
952 | ||
953 | return dev->ethtool_ops->set_tso(dev, edata.data); | |
954 | } | |
955 | ||
e89e9cf5 AR |
956 | static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr) |
957 | { | |
958 | struct ethtool_value edata; | |
959 | ||
960 | if (!dev->ethtool_ops->set_ufo) | |
961 | return -EOPNOTSUPP; | |
962 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
963 | return -EFAULT; | |
964 | if (edata.data && !(dev->features & NETIF_F_SG)) | |
965 | return -EINVAL; | |
966 | if (edata.data && !(dev->features & NETIF_F_HW_CSUM)) | |
967 | return -EINVAL; | |
968 | return dev->ethtool_ops->set_ufo(dev, edata.data); | |
969 | } | |
970 | ||
37c3185a HX |
971 | static int ethtool_get_gso(struct net_device *dev, char __user *useraddr) |
972 | { | |
973 | struct ethtool_value edata = { ETHTOOL_GGSO }; | |
974 | ||
975 | edata.data = dev->features & NETIF_F_GSO; | |
976 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
977 | return -EFAULT; | |
978 | return 0; | |
979 | } | |
980 | ||
981 | static int ethtool_set_gso(struct net_device *dev, char __user *useraddr) | |
982 | { | |
983 | struct ethtool_value edata; | |
984 | ||
985 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
986 | return -EFAULT; | |
987 | if (edata.data) | |
988 | dev->features |= NETIF_F_GSO; | |
989 | else | |
990 | dev->features &= ~NETIF_F_GSO; | |
991 | return 0; | |
992 | } | |
993 | ||
b240a0e5 HX |
994 | static int ethtool_get_gro(struct net_device *dev, char __user *useraddr) |
995 | { | |
996 | struct ethtool_value edata = { ETHTOOL_GGRO }; | |
997 | ||
998 | edata.data = dev->features & NETIF_F_GRO; | |
999 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
1000 | return -EFAULT; | |
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | static int ethtool_set_gro(struct net_device *dev, char __user *useraddr) | |
1005 | { | |
1006 | struct ethtool_value edata; | |
1007 | ||
1008 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
1009 | return -EFAULT; | |
1010 | ||
1011 | if (edata.data) { | |
1012 | if (!dev->ethtool_ops->get_rx_csum || | |
1013 | !dev->ethtool_ops->get_rx_csum(dev)) | |
1014 | return -EINVAL; | |
1015 | dev->features |= NETIF_F_GRO; | |
1016 | } else | |
1017 | dev->features &= ~NETIF_F_GRO; | |
1018 | ||
1019 | return 0; | |
1020 | } | |
1021 | ||
1da177e4 LT |
1022 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
1023 | { | |
1024 | struct ethtool_test test; | |
76fd8593 | 1025 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 | 1026 | u64 *data; |
ff03d49f | 1027 | int ret, test_len; |
1da177e4 | 1028 | |
a9828ec6 | 1029 | if (!ops->self_test || !ops->get_sset_count) |
1da177e4 LT |
1030 | return -EOPNOTSUPP; |
1031 | ||
a9828ec6 | 1032 | test_len = ops->get_sset_count(dev, ETH_SS_TEST); |
ff03d49f JG |
1033 | if (test_len < 0) |
1034 | return test_len; | |
1035 | WARN_ON(test_len == 0); | |
1036 | ||
1da177e4 LT |
1037 | if (copy_from_user(&test, useraddr, sizeof(test))) |
1038 | return -EFAULT; | |
1039 | ||
ff03d49f JG |
1040 | test.len = test_len; |
1041 | data = kmalloc(test_len * sizeof(u64), GFP_USER); | |
1da177e4 LT |
1042 | if (!data) |
1043 | return -ENOMEM; | |
1044 | ||
1045 | ops->self_test(dev, &test, data); | |
1046 | ||
1047 | ret = -EFAULT; | |
1048 | if (copy_to_user(useraddr, &test, sizeof(test))) | |
1049 | goto out; | |
1050 | useraddr += sizeof(test); | |
1051 | if (copy_to_user(useraddr, data, test.len * sizeof(u64))) | |
1052 | goto out; | |
1053 | ret = 0; | |
1054 | ||
1055 | out: | |
1056 | kfree(data); | |
1057 | return ret; | |
1058 | } | |
1059 | ||
1060 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) | |
1061 | { | |
1062 | struct ethtool_gstrings gstrings; | |
76fd8593 | 1063 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 LT |
1064 | u8 *data; |
1065 | int ret; | |
1066 | ||
a9828ec6 | 1067 | if (!ops->get_strings || !ops->get_sset_count) |
1da177e4 LT |
1068 | return -EOPNOTSUPP; |
1069 | ||
1070 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) | |
1071 | return -EFAULT; | |
1072 | ||
a9828ec6 BH |
1073 | ret = ops->get_sset_count(dev, gstrings.string_set); |
1074 | if (ret < 0) | |
1075 | return ret; | |
1076 | ||
1077 | gstrings.len = ret; | |
1da177e4 LT |
1078 | |
1079 | data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER); | |
1080 | if (!data) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | ops->get_strings(dev, gstrings.string_set, data); | |
1084 | ||
1085 | ret = -EFAULT; | |
1086 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) | |
1087 | goto out; | |
1088 | useraddr += sizeof(gstrings); | |
1089 | if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) | |
1090 | goto out; | |
1091 | ret = 0; | |
1092 | ||
1093 | out: | |
1094 | kfree(data); | |
1095 | return ret; | |
1096 | } | |
1097 | ||
1098 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) | |
1099 | { | |
1100 | struct ethtool_value id; | |
1101 | ||
1102 | if (!dev->ethtool_ops->phys_id) | |
1103 | return -EOPNOTSUPP; | |
1104 | ||
1105 | if (copy_from_user(&id, useraddr, sizeof(id))) | |
1106 | return -EFAULT; | |
1107 | ||
1108 | return dev->ethtool_ops->phys_id(dev, id.data); | |
1109 | } | |
1110 | ||
1111 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) | |
1112 | { | |
1113 | struct ethtool_stats stats; | |
76fd8593 | 1114 | const struct ethtool_ops *ops = dev->ethtool_ops; |
1da177e4 | 1115 | u64 *data; |
ff03d49f | 1116 | int ret, n_stats; |
1da177e4 | 1117 | |
a9828ec6 | 1118 | if (!ops->get_ethtool_stats || !ops->get_sset_count) |
1da177e4 LT |
1119 | return -EOPNOTSUPP; |
1120 | ||
a9828ec6 | 1121 | n_stats = ops->get_sset_count(dev, ETH_SS_STATS); |
ff03d49f JG |
1122 | if (n_stats < 0) |
1123 | return n_stats; | |
1124 | WARN_ON(n_stats == 0); | |
1125 | ||
1da177e4 LT |
1126 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
1127 | return -EFAULT; | |
1128 | ||
ff03d49f JG |
1129 | stats.n_stats = n_stats; |
1130 | data = kmalloc(n_stats * sizeof(u64), GFP_USER); | |
1da177e4 LT |
1131 | if (!data) |
1132 | return -ENOMEM; | |
1133 | ||
1134 | ops->get_ethtool_stats(dev, &stats, data); | |
1135 | ||
1136 | ret = -EFAULT; | |
1137 | if (copy_to_user(useraddr, &stats, sizeof(stats))) | |
1138 | goto out; | |
1139 | useraddr += sizeof(stats); | |
1140 | if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64))) | |
1141 | goto out; | |
1142 | ret = 0; | |
1143 | ||
1144 | out: | |
1145 | kfree(data); | |
1146 | return ret; | |
1147 | } | |
1148 | ||
0bf0519d | 1149 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
a6f9a705 JW |
1150 | { |
1151 | struct ethtool_perm_addr epaddr; | |
a6f9a705 | 1152 | |
313674af | 1153 | if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) |
a6f9a705 JW |
1154 | return -EFAULT; |
1155 | ||
313674af MW |
1156 | if (epaddr.size < dev->addr_len) |
1157 | return -ETOOSMALL; | |
1158 | epaddr.size = dev->addr_len; | |
a6f9a705 | 1159 | |
a6f9a705 | 1160 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) |
313674af | 1161 | return -EFAULT; |
a6f9a705 | 1162 | useraddr += sizeof(epaddr); |
313674af MW |
1163 | if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) |
1164 | return -EFAULT; | |
1165 | return 0; | |
a6f9a705 JW |
1166 | } |
1167 | ||
13c99b24 JG |
1168 | static int ethtool_get_value(struct net_device *dev, char __user *useraddr, |
1169 | u32 cmd, u32 (*actor)(struct net_device *)) | |
3ae7c0b2 | 1170 | { |
8e557421 | 1171 | struct ethtool_value edata = { .cmd = cmd }; |
3ae7c0b2 | 1172 | |
13c99b24 | 1173 | if (!actor) |
3ae7c0b2 JG |
1174 | return -EOPNOTSUPP; |
1175 | ||
13c99b24 | 1176 | edata.data = actor(dev); |
3ae7c0b2 JG |
1177 | |
1178 | if (copy_to_user(useraddr, &edata, sizeof(edata))) | |
1179 | return -EFAULT; | |
1180 | return 0; | |
1181 | } | |
1182 | ||
13c99b24 JG |
1183 | static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, |
1184 | void (*actor)(struct net_device *, u32)) | |
3ae7c0b2 JG |
1185 | { |
1186 | struct ethtool_value edata; | |
1187 | ||
13c99b24 | 1188 | if (!actor) |
3ae7c0b2 JG |
1189 | return -EOPNOTSUPP; |
1190 | ||
1191 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
1192 | return -EFAULT; | |
1193 | ||
13c99b24 | 1194 | actor(dev, edata.data); |
339bf024 JG |
1195 | return 0; |
1196 | } | |
1197 | ||
13c99b24 JG |
1198 | static int ethtool_set_value(struct net_device *dev, char __user *useraddr, |
1199 | int (*actor)(struct net_device *, u32)) | |
339bf024 JG |
1200 | { |
1201 | struct ethtool_value edata; | |
1202 | ||
13c99b24 | 1203 | if (!actor) |
339bf024 JG |
1204 | return -EOPNOTSUPP; |
1205 | ||
1206 | if (copy_from_user(&edata, useraddr, sizeof(edata))) | |
1207 | return -EFAULT; | |
1208 | ||
13c99b24 | 1209 | return actor(dev, edata.data); |
339bf024 JG |
1210 | } |
1211 | ||
05c6a8d7 AK |
1212 | static int ethtool_flash_device(struct net_device *dev, char __user *useraddr) |
1213 | { | |
1214 | struct ethtool_flash efl; | |
1215 | ||
1216 | if (copy_from_user(&efl, useraddr, sizeof(efl))) | |
1217 | return -EFAULT; | |
1218 | ||
1219 | if (!dev->ethtool_ops->flash_device) | |
1220 | return -EOPNOTSUPP; | |
1221 | ||
1222 | return dev->ethtool_ops->flash_device(dev, &efl); | |
1223 | } | |
1224 | ||
1da177e4 LT |
1225 | /* The main entry point in this file. Called from net/core/dev.c */ |
1226 | ||
881d966b | 1227 | int dev_ethtool(struct net *net, struct ifreq *ifr) |
1da177e4 | 1228 | { |
881d966b | 1229 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); |
1da177e4 LT |
1230 | void __user *useraddr = ifr->ifr_data; |
1231 | u32 ethcmd; | |
1232 | int rc; | |
81e81575 | 1233 | unsigned long old_features; |
1da177e4 | 1234 | |
1da177e4 LT |
1235 | if (!dev || !netif_device_present(dev)) |
1236 | return -ENODEV; | |
1237 | ||
1238 | if (!dev->ethtool_ops) | |
61a44b9c | 1239 | return -EOPNOTSUPP; |
1da177e4 LT |
1240 | |
1241 | if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd))) | |
1242 | return -EFAULT; | |
1243 | ||
75f3123c SH |
1244 | /* Allow some commands to be done by anyone */ |
1245 | switch(ethcmd) { | |
75f3123c | 1246 | case ETHTOOL_GDRVINFO: |
75f3123c | 1247 | case ETHTOOL_GMSGLVL: |
75f3123c SH |
1248 | case ETHTOOL_GCOALESCE: |
1249 | case ETHTOOL_GRINGPARAM: | |
1250 | case ETHTOOL_GPAUSEPARAM: | |
1251 | case ETHTOOL_GRXCSUM: | |
1252 | case ETHTOOL_GTXCSUM: | |
1253 | case ETHTOOL_GSG: | |
1254 | case ETHTOOL_GSTRINGS: | |
75f3123c SH |
1255 | case ETHTOOL_GTSO: |
1256 | case ETHTOOL_GPERMADDR: | |
1257 | case ETHTOOL_GUFO: | |
1258 | case ETHTOOL_GGSO: | |
339bf024 JG |
1259 | case ETHTOOL_GFLAGS: |
1260 | case ETHTOOL_GPFLAGS: | |
0853ad66 | 1261 | case ETHTOOL_GRXFH: |
59089d8d SB |
1262 | case ETHTOOL_GRXRINGS: |
1263 | case ETHTOOL_GRXCLSRLCNT: | |
1264 | case ETHTOOL_GRXCLSRULE: | |
1265 | case ETHTOOL_GRXCLSRLALL: | |
75f3123c SH |
1266 | break; |
1267 | default: | |
1268 | if (!capable(CAP_NET_ADMIN)) | |
1269 | return -EPERM; | |
1270 | } | |
1271 | ||
e71a4783 | 1272 | if (dev->ethtool_ops->begin) |
1da177e4 LT |
1273 | if ((rc = dev->ethtool_ops->begin(dev)) < 0) |
1274 | return rc; | |
1275 | ||
d8a33ac4 SH |
1276 | old_features = dev->features; |
1277 | ||
1da177e4 LT |
1278 | switch (ethcmd) { |
1279 | case ETHTOOL_GSET: | |
1280 | rc = ethtool_get_settings(dev, useraddr); | |
1281 | break; | |
1282 | case ETHTOOL_SSET: | |
1283 | rc = ethtool_set_settings(dev, useraddr); | |
1284 | break; | |
1285 | case ETHTOOL_GDRVINFO: | |
1286 | rc = ethtool_get_drvinfo(dev, useraddr); | |
1da177e4 LT |
1287 | break; |
1288 | case ETHTOOL_GREGS: | |
1289 | rc = ethtool_get_regs(dev, useraddr); | |
1290 | break; | |
1291 | case ETHTOOL_GWOL: | |
1292 | rc = ethtool_get_wol(dev, useraddr); | |
1293 | break; | |
1294 | case ETHTOOL_SWOL: | |
1295 | rc = ethtool_set_wol(dev, useraddr); | |
1296 | break; | |
1297 | case ETHTOOL_GMSGLVL: | |
13c99b24 JG |
1298 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1299 | dev->ethtool_ops->get_msglevel); | |
1da177e4 LT |
1300 | break; |
1301 | case ETHTOOL_SMSGLVL: | |
13c99b24 JG |
1302 | rc = ethtool_set_value_void(dev, useraddr, |
1303 | dev->ethtool_ops->set_msglevel); | |
1da177e4 LT |
1304 | break; |
1305 | case ETHTOOL_NWAY_RST: | |
1306 | rc = ethtool_nway_reset(dev); | |
1307 | break; | |
1308 | case ETHTOOL_GLINK: | |
13c99b24 JG |
1309 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1310 | dev->ethtool_ops->get_link); | |
1da177e4 LT |
1311 | break; |
1312 | case ETHTOOL_GEEPROM: | |
1313 | rc = ethtool_get_eeprom(dev, useraddr); | |
1314 | break; | |
1315 | case ETHTOOL_SEEPROM: | |
1316 | rc = ethtool_set_eeprom(dev, useraddr); | |
1317 | break; | |
1318 | case ETHTOOL_GCOALESCE: | |
1319 | rc = ethtool_get_coalesce(dev, useraddr); | |
1320 | break; | |
1321 | case ETHTOOL_SCOALESCE: | |
1322 | rc = ethtool_set_coalesce(dev, useraddr); | |
1323 | break; | |
1324 | case ETHTOOL_GRINGPARAM: | |
1325 | rc = ethtool_get_ringparam(dev, useraddr); | |
1326 | break; | |
1327 | case ETHTOOL_SRINGPARAM: | |
1328 | rc = ethtool_set_ringparam(dev, useraddr); | |
1329 | break; | |
1330 | case ETHTOOL_GPAUSEPARAM: | |
1331 | rc = ethtool_get_pauseparam(dev, useraddr); | |
1332 | break; | |
1333 | case ETHTOOL_SPAUSEPARAM: | |
1334 | rc = ethtool_set_pauseparam(dev, useraddr); | |
1335 | break; | |
1336 | case ETHTOOL_GRXCSUM: | |
13c99b24 | 1337 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1896e61f SS |
1338 | (dev->ethtool_ops->get_rx_csum ? |
1339 | dev->ethtool_ops->get_rx_csum : | |
1340 | ethtool_op_get_rx_csum)); | |
1da177e4 LT |
1341 | break; |
1342 | case ETHTOOL_SRXCSUM: | |
b240a0e5 | 1343 | rc = ethtool_set_rx_csum(dev, useraddr); |
1da177e4 LT |
1344 | break; |
1345 | case ETHTOOL_GTXCSUM: | |
13c99b24 | 1346 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
88d3aafd JG |
1347 | (dev->ethtool_ops->get_tx_csum ? |
1348 | dev->ethtool_ops->get_tx_csum : | |
1349 | ethtool_op_get_tx_csum)); | |
1da177e4 LT |
1350 | break; |
1351 | case ETHTOOL_STXCSUM: | |
1352 | rc = ethtool_set_tx_csum(dev, useraddr); | |
1353 | break; | |
1354 | case ETHTOOL_GSG: | |
13c99b24 | 1355 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
88d3aafd JG |
1356 | (dev->ethtool_ops->get_sg ? |
1357 | dev->ethtool_ops->get_sg : | |
1358 | ethtool_op_get_sg)); | |
1da177e4 LT |
1359 | break; |
1360 | case ETHTOOL_SSG: | |
1361 | rc = ethtool_set_sg(dev, useraddr); | |
1362 | break; | |
1363 | case ETHTOOL_GTSO: | |
13c99b24 | 1364 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
88d3aafd JG |
1365 | (dev->ethtool_ops->get_tso ? |
1366 | dev->ethtool_ops->get_tso : | |
1367 | ethtool_op_get_tso)); | |
1da177e4 LT |
1368 | break; |
1369 | case ETHTOOL_STSO: | |
1370 | rc = ethtool_set_tso(dev, useraddr); | |
1371 | break; | |
1372 | case ETHTOOL_TEST: | |
1373 | rc = ethtool_self_test(dev, useraddr); | |
1374 | break; | |
1375 | case ETHTOOL_GSTRINGS: | |
1376 | rc = ethtool_get_strings(dev, useraddr); | |
1377 | break; | |
1378 | case ETHTOOL_PHYS_ID: | |
1379 | rc = ethtool_phys_id(dev, useraddr); | |
1380 | break; | |
1381 | case ETHTOOL_GSTATS: | |
1382 | rc = ethtool_get_stats(dev, useraddr); | |
1383 | break; | |
a6f9a705 JW |
1384 | case ETHTOOL_GPERMADDR: |
1385 | rc = ethtool_get_perm_addr(dev, useraddr); | |
1386 | break; | |
e89e9cf5 | 1387 | case ETHTOOL_GUFO: |
13c99b24 | 1388 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
88d3aafd JG |
1389 | (dev->ethtool_ops->get_ufo ? |
1390 | dev->ethtool_ops->get_ufo : | |
1391 | ethtool_op_get_ufo)); | |
e89e9cf5 AR |
1392 | break; |
1393 | case ETHTOOL_SUFO: | |
1394 | rc = ethtool_set_ufo(dev, useraddr); | |
1395 | break; | |
37c3185a HX |
1396 | case ETHTOOL_GGSO: |
1397 | rc = ethtool_get_gso(dev, useraddr); | |
1398 | break; | |
1399 | case ETHTOOL_SGSO: | |
1400 | rc = ethtool_set_gso(dev, useraddr); | |
1401 | break; | |
3ae7c0b2 | 1402 | case ETHTOOL_GFLAGS: |
13c99b24 | 1403 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1896e61f SS |
1404 | (dev->ethtool_ops->get_flags ? |
1405 | dev->ethtool_ops->get_flags : | |
1406 | ethtool_op_get_flags)); | |
3ae7c0b2 JG |
1407 | break; |
1408 | case ETHTOOL_SFLAGS: | |
13c99b24 JG |
1409 | rc = ethtool_set_value(dev, useraddr, |
1410 | dev->ethtool_ops->set_flags); | |
3ae7c0b2 | 1411 | break; |
339bf024 | 1412 | case ETHTOOL_GPFLAGS: |
13c99b24 JG |
1413 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
1414 | dev->ethtool_ops->get_priv_flags); | |
339bf024 JG |
1415 | break; |
1416 | case ETHTOOL_SPFLAGS: | |
13c99b24 JG |
1417 | rc = ethtool_set_value(dev, useraddr, |
1418 | dev->ethtool_ops->set_priv_flags); | |
339bf024 | 1419 | break; |
0853ad66 | 1420 | case ETHTOOL_GRXFH: |
59089d8d SB |
1421 | case ETHTOOL_GRXRINGS: |
1422 | case ETHTOOL_GRXCLSRLCNT: | |
1423 | case ETHTOOL_GRXCLSRULE: | |
1424 | case ETHTOOL_GRXCLSRLALL: | |
1425 | rc = ethtool_get_rxnfc(dev, useraddr); | |
0853ad66 SB |
1426 | break; |
1427 | case ETHTOOL_SRXFH: | |
59089d8d SB |
1428 | case ETHTOOL_SRXCLSRLDEL: |
1429 | case ETHTOOL_SRXCLSRLINS: | |
1430 | rc = ethtool_set_rxnfc(dev, useraddr); | |
0853ad66 | 1431 | break; |
b240a0e5 HX |
1432 | case ETHTOOL_GGRO: |
1433 | rc = ethtool_get_gro(dev, useraddr); | |
1434 | break; | |
1435 | case ETHTOOL_SGRO: | |
1436 | rc = ethtool_set_gro(dev, useraddr); | |
1437 | break; | |
05c6a8d7 AK |
1438 | case ETHTOOL_FLASHDEV: |
1439 | rc = ethtool_flash_device(dev, useraddr); | |
1440 | break; | |
d73d3a8c BH |
1441 | case ETHTOOL_RESET: |
1442 | rc = ethtool_reset(dev, useraddr); | |
1443 | break; | |
15682bc4 PWJ |
1444 | case ETHTOOL_SRXNTUPLE: |
1445 | rc = ethtool_set_rx_ntuple(dev, useraddr); | |
1446 | break; | |
1447 | case ETHTOOL_GRXNTUPLE: | |
1448 | rc = ethtool_get_rx_ntuple(dev, useraddr); | |
1449 | break; | |
1da177e4 | 1450 | default: |
61a44b9c | 1451 | rc = -EOPNOTSUPP; |
1da177e4 | 1452 | } |
4ec93edb | 1453 | |
e71a4783 | 1454 | if (dev->ethtool_ops->complete) |
1da177e4 | 1455 | dev->ethtool_ops->complete(dev); |
d8a33ac4 SH |
1456 | |
1457 | if (old_features != dev->features) | |
1458 | netdev_features_change(dev); | |
1459 | ||
1da177e4 | 1460 | return rc; |
1da177e4 LT |
1461 | } |
1462 | ||
1da177e4 LT |
1463 | EXPORT_SYMBOL(ethtool_op_get_link); |
1464 | EXPORT_SYMBOL(ethtool_op_get_sg); | |
1465 | EXPORT_SYMBOL(ethtool_op_get_tso); | |
1da177e4 LT |
1466 | EXPORT_SYMBOL(ethtool_op_set_sg); |
1467 | EXPORT_SYMBOL(ethtool_op_set_tso); | |
1468 | EXPORT_SYMBOL(ethtool_op_set_tx_csum); | |
69f6a0fa | 1469 | EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum); |
6460d948 | 1470 | EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum); |
e89e9cf5 AR |
1471 | EXPORT_SYMBOL(ethtool_op_set_ufo); |
1472 | EXPORT_SYMBOL(ethtool_op_get_ufo); | |
3ae7c0b2 JG |
1473 | EXPORT_SYMBOL(ethtool_op_set_flags); |
1474 | EXPORT_SYMBOL(ethtool_op_get_flags); |