blob: dae2fd053c2b3c51a8fed31c380e93034bcf04b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
Matthew Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000020#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070021#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090024/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * Some useful ethtool_ops methods that're device independent.
26 * If we find that all drivers want to do the same thing here,
27 * we can turn these into dev_() function calls.
28 */
29
30u32 ethtool_op_get_link(struct net_device *dev)
31{
32 return netif_carrier_ok(dev) ? 1 : 0;
33}
chavey97f8aef2010-04-07 21:54:42 -070034EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Sridhar Samudrala1896e612009-07-22 13:38:22 +000036u32 ethtool_op_get_rx_csum(struct net_device *dev)
37{
38 return (dev->features & NETIF_F_ALL_CSUM) != 0;
39}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000040EXPORT_SYMBOL(ethtool_op_get_rx_csum);
Sridhar Samudrala1896e612009-07-22 13:38:22 +000041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042u32 ethtool_op_get_tx_csum(struct net_device *dev)
43{
Herbert Xu8648b302006-06-17 22:06:05 -070044 return (dev->features & NETIF_F_ALL_CSUM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
Eric Dumazet8a729fc2009-07-26 23:18:11 +000046EXPORT_SYMBOL(ethtool_op_get_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
49{
50 if (data)
51 dev->features |= NETIF_F_IP_CSUM;
52 else
53 dev->features &= ~NETIF_F_IP_CSUM;
54
55 return 0;
56}
57
Jon Mason69f6a0f2005-05-29 20:27:24 -070058int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
59{
60 if (data)
61 dev->features |= NETIF_F_HW_CSUM;
62 else
63 dev->features &= ~NETIF_F_HW_CSUM;
64
65 return 0;
66}
chavey97f8aef2010-04-07 21:54:42 -070067EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
Michael Chan6460d942007-07-14 19:07:52 -070068
69int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
70{
71 if (data)
72 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
73 else
74 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
75
76 return 0;
77}
chavey97f8aef2010-04-07 21:54:42 -070078EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
Michael Chan6460d942007-07-14 19:07:52 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080u32 ethtool_op_get_sg(struct net_device *dev)
81{
82 return (dev->features & NETIF_F_SG) != 0;
83}
chavey97f8aef2010-04-07 21:54:42 -070084EXPORT_SYMBOL(ethtool_op_get_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86int ethtool_op_set_sg(struct net_device *dev, u32 data)
87{
88 if (data)
89 dev->features |= NETIF_F_SG;
90 else
91 dev->features &= ~NETIF_F_SG;
92
93 return 0;
94}
chavey97f8aef2010-04-07 21:54:42 -070095EXPORT_SYMBOL(ethtool_op_set_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97u32 ethtool_op_get_tso(struct net_device *dev)
98{
99 return (dev->features & NETIF_F_TSO) != 0;
100}
chavey97f8aef2010-04-07 21:54:42 -0700101EXPORT_SYMBOL(ethtool_op_get_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103int ethtool_op_set_tso(struct net_device *dev, u32 data)
104{
105 if (data)
106 dev->features |= NETIF_F_TSO;
107 else
108 dev->features &= ~NETIF_F_TSO;
109
110 return 0;
111}
chavey97f8aef2010-04-07 21:54:42 -0700112EXPORT_SYMBOL(ethtool_op_set_tso);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700114u32 ethtool_op_get_ufo(struct net_device *dev)
115{
116 return (dev->features & NETIF_F_UFO) != 0;
117}
chavey97f8aef2010-04-07 21:54:42 -0700118EXPORT_SYMBOL(ethtool_op_get_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700119
120int ethtool_op_set_ufo(struct net_device *dev, u32 data)
121{
122 if (data)
123 dev->features |= NETIF_F_UFO;
124 else
125 dev->features &= ~NETIF_F_UFO;
126 return 0;
127}
chavey97f8aef2010-04-07 21:54:42 -0700128EXPORT_SYMBOL(ethtool_op_set_ufo);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700129
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700130/* the following list of flags are the same as their associated
131 * NETIF_F_xxx values in include/linux/netdevice.h
132 */
133static const u32 flags_dup_features =
stephen hemmingerb00fabb2010-03-29 14:47:27 +0000134 (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700135
136u32 ethtool_op_get_flags(struct net_device *dev)
137{
138 /* in the future, this function will probably contain additional
139 * handling for flags which are not so easily handled
140 * by a simple masking operation
141 */
142
143 return dev->features & flags_dup_features;
144}
chavey97f8aef2010-04-07 21:54:42 -0700145EXPORT_SYMBOL(ethtool_op_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700146
Ben Hutchings1437ce32010-06-30 02:44:32 +0000147int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700148{
Ben Hutchings1437ce32010-06-30 02:44:32 +0000149 if (data & ~supported)
150 return -EINVAL;
Peter Waskiewicz0d643e12010-02-12 13:48:25 +0000151
Ben Hutchings1437ce32010-06-30 02:44:32 +0000152 dev->features = ((dev->features & ~flags_dup_features) |
153 (data & flags_dup_features));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700154 return 0;
155}
chavey97f8aef2010-04-07 21:54:42 -0700156EXPORT_SYMBOL(ethtool_op_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -0700157
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800158void ethtool_ntuple_flush(struct net_device *dev)
159{
160 struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
161
162 list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
163 list_del(&fsc->list);
164 kfree(fsc);
165 }
166 dev->ethtool_ntuple_list.count = 0;
167}
168EXPORT_SYMBOL(ethtool_ntuple_flush);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Handlers for each ethtool command */
171
172static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
173{
Roland Dreier8e557422010-02-11 12:14:23 -0800174 struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int err;
176
177 if (!dev->ethtool_ops->get_settings)
178 return -EOPNOTSUPP;
179
180 err = dev->ethtool_ops->get_settings(dev, &cmd);
181 if (err < 0)
182 return err;
183
184 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
185 return -EFAULT;
186 return 0;
187}
188
189static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
190{
191 struct ethtool_cmd cmd;
192
193 if (!dev->ethtool_ops->set_settings)
194 return -EOPNOTSUPP;
195
196 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
197 return -EFAULT;
198
199 return dev->ethtool_ops->set_settings(dev, &cmd);
200}
201
chavey97f8aef2010-04-07 21:54:42 -0700202static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
203 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700206 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 memset(&info, 0, sizeof(info));
209 info.cmd = ETHTOOL_GDRVINFO;
Ben Hutchings01414802010-08-17 02:31:15 -0700210 if (ops && ops->get_drvinfo) {
211 ops->get_drvinfo(dev, &info);
212 } else if (dev->dev.parent && dev->dev.parent->driver) {
213 strlcpy(info.bus_info, dev_name(dev->dev.parent),
214 sizeof(info.bus_info));
215 strlcpy(info.driver, dev->dev.parent->driver->name,
216 sizeof(info.driver));
217 } else {
218 return -EOPNOTSUPP;
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jeff Garzik723b2f52010-03-03 22:51:50 +0000221 /*
222 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000223 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000224 */
Ben Hutchings01414802010-08-17 02:31:15 -0700225 if (ops && ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700226 int rc;
227
228 rc = ops->get_sset_count(dev, ETH_SS_TEST);
229 if (rc >= 0)
230 info.testinfo_len = rc;
231 rc = ops->get_sset_count(dev, ETH_SS_STATS);
232 if (rc >= 0)
233 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700234 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
235 if (rc >= 0)
236 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700237 }
Ben Hutchings01414802010-08-17 02:31:15 -0700238 if (ops && ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 info.regdump_len = ops->get_regs_len(dev);
Ben Hutchings01414802010-08-17 02:31:15 -0700240 if (ops && ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 info.eedump_len = ops->get_eeprom_len(dev);
242
243 if (copy_to_user(useraddr, &info, sizeof(info)))
244 return -EFAULT;
245 return 0;
246}
247
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800248static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700249 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000250{
251 struct ethtool_sset_info info;
252 const struct ethtool_ops *ops = dev->ethtool_ops;
253 u64 sset_mask;
254 int i, idx = 0, n_bits = 0, ret, rc;
255 u32 *info_buf = NULL;
256
257 if (!ops->get_sset_count)
258 return -EOPNOTSUPP;
259
260 if (copy_from_user(&info, useraddr, sizeof(info)))
261 return -EFAULT;
262
263 /* store copy of mask, because we zero struct later on */
264 sset_mask = info.sset_mask;
265 if (!sset_mask)
266 return 0;
267
268 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000269 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000270
271 memset(&info, 0, sizeof(info));
272 info.cmd = ETHTOOL_GSSET_INFO;
273
274 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
275 if (!info_buf)
276 return -ENOMEM;
277
278 /*
279 * fill return buffer based on input bitmask and successful
280 * get_sset_count return
281 */
282 for (i = 0; i < 64; i++) {
283 if (!(sset_mask & (1ULL << i)))
284 continue;
285
286 rc = ops->get_sset_count(dev, i);
287 if (rc >= 0) {
288 info.sset_mask |= (1ULL << i);
289 info_buf[idx++] = rc;
290 }
291 }
292
293 ret = -EFAULT;
294 if (copy_to_user(useraddr, &info, sizeof(info)))
295 goto out;
296
297 useraddr += offsetof(struct ethtool_sset_info, data);
298 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
299 goto out;
300
301 ret = 0;
302
303out:
304 kfree(info_buf);
305 return ret;
306}
307
chavey97f8aef2010-04-07 21:54:42 -0700308static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000309 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700310{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000311 struct ethtool_rxnfc info;
312 size_t info_size = sizeof(info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700313
Santwona Behera59089d82009-02-20 00:58:13 -0800314 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700315 return -EOPNOTSUPP;
316
Ben Hutchingsbf988432010-06-28 08:45:58 +0000317 /* struct ethtool_rxnfc was originally defined for
318 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
319 * members. User-space might still be using that
320 * definition. */
321 if (cmd == ETHTOOL_SRXFH)
322 info_size = (offsetof(struct ethtool_rxnfc, data) +
323 sizeof(info.data));
324
325 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700326 return -EFAULT;
327
Ben Hutchingsbf988432010-06-28 08:45:58 +0000328 return dev->ethtool_ops->set_rxnfc(dev, &info);
Santwona Behera0853ad62008-07-02 03:47:41 -0700329}
330
chavey97f8aef2010-04-07 21:54:42 -0700331static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000332 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700333{
334 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000335 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800336 const struct ethtool_ops *ops = dev->ethtool_ops;
337 int ret;
338 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700339
Santwona Behera59089d82009-02-20 00:58:13 -0800340 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700341 return -EOPNOTSUPP;
342
Ben Hutchingsbf988432010-06-28 08:45:58 +0000343 /* struct ethtool_rxnfc was originally defined for
344 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
345 * members. User-space might still be using that
346 * definition. */
347 if (cmd == ETHTOOL_GRXFH)
348 info_size = (offsetof(struct ethtool_rxnfc, data) +
349 sizeof(info.data));
350
351 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700352 return -EFAULT;
353
Santwona Behera59089d82009-02-20 00:58:13 -0800354 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
355 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +0000356 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
357 rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
358 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -0800359 if (!rule_buf)
360 return -ENOMEM;
361 }
362 }
Santwona Behera0853ad62008-07-02 03:47:41 -0700363
Santwona Behera59089d82009-02-20 00:58:13 -0800364 ret = ops->get_rxnfc(dev, &info, rule_buf);
365 if (ret < 0)
366 goto err_out;
367
368 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000369 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -0800370 goto err_out;
371
372 if (rule_buf) {
373 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
374 if (copy_to_user(useraddr, rule_buf,
375 info.rule_cnt * sizeof(u32)))
376 goto err_out;
377 }
378 ret = 0;
379
380err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -0700381 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -0800382
383 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -0700384}
385
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +0000386static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
387 void __user *useraddr)
388{
389 struct ethtool_rxfh_indir *indir;
390 u32 table_size;
391 size_t full_size;
392 int ret;
393
394 if (!dev->ethtool_ops->get_rxfh_indir)
395 return -EOPNOTSUPP;
396
397 if (copy_from_user(&table_size,
398 useraddr + offsetof(struct ethtool_rxfh_indir, size),
399 sizeof(table_size)))
400 return -EFAULT;
401
402 if (table_size >
403 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
404 return -ENOMEM;
405 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
406 indir = kmalloc(full_size, GFP_USER);
407 if (!indir)
408 return -ENOMEM;
409
410 indir->cmd = ETHTOOL_GRXFHINDIR;
411 indir->size = table_size;
412 ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
413 if (ret)
414 goto out;
415
416 if (copy_to_user(useraddr, indir, full_size))
417 ret = -EFAULT;
418
419out:
420 kfree(indir);
421 return ret;
422}
423
424static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
425 void __user *useraddr)
426{
427 struct ethtool_rxfh_indir *indir;
428 u32 table_size;
429 size_t full_size;
430 int ret;
431
432 if (!dev->ethtool_ops->set_rxfh_indir)
433 return -EOPNOTSUPP;
434
435 if (copy_from_user(&table_size,
436 useraddr + offsetof(struct ethtool_rxfh_indir, size),
437 sizeof(table_size)))
438 return -EFAULT;
439
440 if (table_size >
441 (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
442 return -ENOMEM;
443 full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
444 indir = kmalloc(full_size, GFP_USER);
445 if (!indir)
446 return -ENOMEM;
447
448 if (copy_from_user(indir, useraddr, full_size)) {
449 ret = -EFAULT;
450 goto out;
451 }
452
453 ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
454
455out:
456 kfree(indir);
457 return ret;
458}
459
Peter Waskiewicze8589112010-02-12 13:48:05 +0000460static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
chavey97f8aef2010-04-07 21:54:42 -0700461 struct ethtool_rx_ntuple_flow_spec *spec,
462 struct ethtool_rx_ntuple_flow_spec_container *fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800463{
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800464
465 /* don't add filters forever */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000466 if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
467 /* free the container */
468 kfree(fsc);
469 return;
470 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800471
472 /* Copy the whole filter over */
473 fsc->fs.flow_type = spec->flow_type;
474 memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
475 memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
476
477 fsc->fs.vlan_tag = spec->vlan_tag;
478 fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
479 fsc->fs.data = spec->data;
480 fsc->fs.data_mask = spec->data_mask;
481 fsc->fs.action = spec->action;
482
483 /* add to the list */
484 list_add_tail_rcu(&fsc->list, &list->list);
485 list->count++;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800486}
487
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000488/*
489 * ethtool does not (or did not) set masks for flow parameters that are
490 * not specified, so if both value and mask are 0 then this must be
491 * treated as equivalent to a mask with all bits set. Implement that
492 * here rather than in drivers.
493 */
494static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
495{
496 struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
497 struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
498
499 if (fs->flow_type != TCP_V4_FLOW &&
500 fs->flow_type != UDP_V4_FLOW &&
501 fs->flow_type != SCTP_V4_FLOW)
502 return;
503
504 if (!(entry->ip4src | mask->ip4src))
505 mask->ip4src = htonl(0xffffffff);
506 if (!(entry->ip4dst | mask->ip4dst))
507 mask->ip4dst = htonl(0xffffffff);
508 if (!(entry->psrc | mask->psrc))
509 mask->psrc = htons(0xffff);
510 if (!(entry->pdst | mask->pdst))
511 mask->pdst = htons(0xffff);
512 if (!(entry->tos | mask->tos))
513 mask->tos = 0xff;
514 if (!(fs->vlan_tag | fs->vlan_tag_mask))
515 fs->vlan_tag_mask = 0xffff;
516 if (!(fs->data | fs->data_mask))
517 fs->data_mask = 0xffffffffffffffffULL;
518}
519
chavey97f8aef2010-04-07 21:54:42 -0700520static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
521 void __user *useraddr)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800522{
523 struct ethtool_rx_ntuple cmd;
524 const struct ethtool_ops *ops = dev->ethtool_ops;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000525 struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800526 int ret;
527
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800528 if (!(dev->features & NETIF_F_NTUPLE))
529 return -EINVAL;
530
531 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
532 return -EFAULT;
533
Ben Hutchingsbe2902d2010-09-16 11:28:07 +0000534 rx_ntuple_fix_masks(&cmd.fs);
535
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800536 /*
537 * Cache filter in dev struct for GET operation only if
538 * the underlying driver doesn't have its own GET operation, and
Peter Waskiewicze8589112010-02-12 13:48:05 +0000539 * only if the filter was added successfully. First make sure we
540 * can allocate the filter, then continue if successful.
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800541 */
Peter Waskiewicze8589112010-02-12 13:48:05 +0000542 if (!ops->get_rx_ntuple) {
543 fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
544 if (!fsc)
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800545 return -ENOMEM;
Peter Waskiewicze8589112010-02-12 13:48:05 +0000546 }
547
548 ret = ops->set_rx_ntuple(dev, &cmd);
549 if (ret) {
550 kfree(fsc);
551 return ret;
552 }
553
554 if (!ops->get_rx_ntuple)
555 __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800556
557 return ret;
558}
559
560static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
561{
562 struct ethtool_gstrings gstrings;
563 const struct ethtool_ops *ops = dev->ethtool_ops;
564 struct ethtool_rx_ntuple_flow_spec_container *fsc;
565 u8 *data;
566 char *p;
567 int ret, i, num_strings = 0;
568
569 if (!ops->get_sset_count)
570 return -EOPNOTSUPP;
571
572 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
573 return -EFAULT;
574
575 ret = ops->get_sset_count(dev, gstrings.string_set);
576 if (ret < 0)
577 return ret;
578
579 gstrings.len = ret;
580
581 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
582 if (!data)
583 return -ENOMEM;
584
585 if (ops->get_rx_ntuple) {
586 /* driver-specific filter grab */
587 ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
588 goto copy;
589 }
590
591 /* default ethtool filter grab */
592 i = 0;
593 p = (char *)data;
594 list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
595 sprintf(p, "Filter %d:\n", i);
596 p += ETH_GSTRING_LEN;
597 num_strings++;
598
599 switch (fsc->fs.flow_type) {
600 case TCP_V4_FLOW:
601 sprintf(p, "\tFlow Type: TCP\n");
602 p += ETH_GSTRING_LEN;
603 num_strings++;
604 break;
605 case UDP_V4_FLOW:
606 sprintf(p, "\tFlow Type: UDP\n");
607 p += ETH_GSTRING_LEN;
608 num_strings++;
609 break;
610 case SCTP_V4_FLOW:
611 sprintf(p, "\tFlow Type: SCTP\n");
612 p += ETH_GSTRING_LEN;
613 num_strings++;
614 break;
615 case AH_ESP_V4_FLOW:
616 sprintf(p, "\tFlow Type: AH ESP\n");
617 p += ETH_GSTRING_LEN;
618 num_strings++;
619 break;
620 case ESP_V4_FLOW:
621 sprintf(p, "\tFlow Type: ESP\n");
622 p += ETH_GSTRING_LEN;
623 num_strings++;
624 break;
625 case IP_USER_FLOW:
626 sprintf(p, "\tFlow Type: Raw IP\n");
627 p += ETH_GSTRING_LEN;
628 num_strings++;
629 break;
630 case IPV4_FLOW:
631 sprintf(p, "\tFlow Type: IPv4\n");
632 p += ETH_GSTRING_LEN;
633 num_strings++;
634 break;
635 default:
636 sprintf(p, "\tFlow Type: Unknown\n");
637 p += ETH_GSTRING_LEN;
638 num_strings++;
639 goto unknown_filter;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000640 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800641
642 /* now the rest of the filters */
643 switch (fsc->fs.flow_type) {
644 case TCP_V4_FLOW:
645 case UDP_V4_FLOW:
646 case SCTP_V4_FLOW:
647 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700648 fsc->fs.h_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800649 p += ETH_GSTRING_LEN;
650 num_strings++;
651 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700652 fsc->fs.m_u.tcp_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800653 p += ETH_GSTRING_LEN;
654 num_strings++;
655 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700656 fsc->fs.h_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800657 p += ETH_GSTRING_LEN;
658 num_strings++;
659 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700660 fsc->fs.m_u.tcp_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800661 p += ETH_GSTRING_LEN;
662 num_strings++;
663 sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700664 fsc->fs.h_u.tcp_ip4_spec.psrc,
665 fsc->fs.m_u.tcp_ip4_spec.psrc);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800666 p += ETH_GSTRING_LEN;
667 num_strings++;
668 sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700669 fsc->fs.h_u.tcp_ip4_spec.pdst,
670 fsc->fs.m_u.tcp_ip4_spec.pdst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800671 p += ETH_GSTRING_LEN;
672 num_strings++;
673 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700674 fsc->fs.h_u.tcp_ip4_spec.tos,
675 fsc->fs.m_u.tcp_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800676 p += ETH_GSTRING_LEN;
677 num_strings++;
678 break;
679 case AH_ESP_V4_FLOW:
680 case ESP_V4_FLOW:
681 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700682 fsc->fs.h_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800683 p += ETH_GSTRING_LEN;
684 num_strings++;
685 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700686 fsc->fs.m_u.ah_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800687 p += ETH_GSTRING_LEN;
688 num_strings++;
689 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700690 fsc->fs.h_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800691 p += ETH_GSTRING_LEN;
692 num_strings++;
693 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700694 fsc->fs.m_u.ah_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800695 p += ETH_GSTRING_LEN;
696 num_strings++;
697 sprintf(p, "\tSPI: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700698 fsc->fs.h_u.ah_ip4_spec.spi,
699 fsc->fs.m_u.ah_ip4_spec.spi);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800700 p += ETH_GSTRING_LEN;
701 num_strings++;
702 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700703 fsc->fs.h_u.ah_ip4_spec.tos,
704 fsc->fs.m_u.ah_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800705 p += ETH_GSTRING_LEN;
706 num_strings++;
707 break;
708 case IP_USER_FLOW:
709 sprintf(p, "\tSrc IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000710 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800711 p += ETH_GSTRING_LEN;
712 num_strings++;
713 sprintf(p, "\tSrc IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000714 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800715 p += ETH_GSTRING_LEN;
716 num_strings++;
717 sprintf(p, "\tDest IP addr: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000718 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800719 p += ETH_GSTRING_LEN;
720 num_strings++;
721 sprintf(p, "\tDest IP mask: 0x%x\n",
Ben Hutchingse0de7c92010-09-14 09:13:08 +0000722 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800723 p += ETH_GSTRING_LEN;
724 num_strings++;
725 break;
726 case IPV4_FLOW:
727 sprintf(p, "\tSrc IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700728 fsc->fs.h_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800729 p += ETH_GSTRING_LEN;
730 num_strings++;
731 sprintf(p, "\tSrc IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700732 fsc->fs.m_u.usr_ip4_spec.ip4src);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800733 p += ETH_GSTRING_LEN;
734 num_strings++;
735 sprintf(p, "\tDest IP addr: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700736 fsc->fs.h_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800737 p += ETH_GSTRING_LEN;
738 num_strings++;
739 sprintf(p, "\tDest IP mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700740 fsc->fs.m_u.usr_ip4_spec.ip4dst);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800741 p += ETH_GSTRING_LEN;
742 num_strings++;
743 sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700744 fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
745 fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800746 p += ETH_GSTRING_LEN;
747 num_strings++;
748 sprintf(p, "\tTOS: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700749 fsc->fs.h_u.usr_ip4_spec.tos,
750 fsc->fs.m_u.usr_ip4_spec.tos);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800751 p += ETH_GSTRING_LEN;
752 num_strings++;
753 sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700754 fsc->fs.h_u.usr_ip4_spec.ip_ver,
755 fsc->fs.m_u.usr_ip4_spec.ip_ver);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800756 p += ETH_GSTRING_LEN;
757 num_strings++;
758 sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700759 fsc->fs.h_u.usr_ip4_spec.proto,
760 fsc->fs.m_u.usr_ip4_spec.proto);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800761 p += ETH_GSTRING_LEN;
762 num_strings++;
763 break;
Joe Perchesccbd6a52010-05-14 10:58:26 +0000764 }
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800765 sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
chavey97f8aef2010-04-07 21:54:42 -0700766 fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800767 p += ETH_GSTRING_LEN;
768 num_strings++;
769 sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
770 p += ETH_GSTRING_LEN;
771 num_strings++;
772 sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
773 p += ETH_GSTRING_LEN;
774 num_strings++;
775 if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
776 sprintf(p, "\tAction: Drop\n");
777 else
778 sprintf(p, "\tAction: Direct to queue %d\n",
chavey97f8aef2010-04-07 21:54:42 -0700779 fsc->fs.action);
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -0800780 p += ETH_GSTRING_LEN;
781 num_strings++;
782unknown_filter:
783 i++;
784 }
785copy:
786 /* indicate to userspace how many strings we actually have */
787 gstrings.len = num_strings;
788 ret = -EFAULT;
789 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
790 goto out;
791 useraddr += sizeof(gstrings);
792 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
793 goto out;
794 ret = 0;
795
796out:
797 kfree(data);
798 return ret;
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
802{
803 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700804 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 void *regbuf;
806 int reglen, ret;
807
808 if (!ops->get_regs || !ops->get_regs_len)
809 return -EOPNOTSUPP;
810
811 if (copy_from_user(&regs, useraddr, sizeof(regs)))
812 return -EFAULT;
813
814 reglen = ops->get_regs_len(dev);
815 if (regs.len > reglen)
816 regs.len = reglen;
817
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000818 regbuf = vmalloc(reglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!regbuf)
820 return -ENOMEM;
821
822 ops->get_regs(dev, &regs, regbuf);
823
824 ret = -EFAULT;
825 if (copy_to_user(useraddr, &regs, sizeof(regs)))
826 goto out;
827 useraddr += offsetof(struct ethtool_regs, data);
828 if (copy_to_user(useraddr, regbuf, regs.len))
829 goto out;
830 ret = 0;
831
832 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +0000833 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return ret;
835}
836
Ben Hutchingsd73d3a82009-10-05 10:59:58 +0000837static int ethtool_reset(struct net_device *dev, char __user *useraddr)
838{
839 struct ethtool_value reset;
840 int ret;
841
842 if (!dev->ethtool_ops->reset)
843 return -EOPNOTSUPP;
844
845 if (copy_from_user(&reset, useraddr, sizeof(reset)))
846 return -EFAULT;
847
848 ret = dev->ethtool_ops->reset(dev, &reset.data);
849 if (ret)
850 return ret;
851
852 if (copy_to_user(useraddr, &reset, sizeof(reset)))
853 return -EFAULT;
854 return 0;
855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
858{
Roland Dreier8e557422010-02-11 12:14:23 -0800859 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (!dev->ethtool_ops->get_wol)
862 return -EOPNOTSUPP;
863
864 dev->ethtool_ops->get_wol(dev, &wol);
865
866 if (copy_to_user(useraddr, &wol, sizeof(wol)))
867 return -EFAULT;
868 return 0;
869}
870
871static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
872{
873 struct ethtool_wolinfo wol;
874
875 if (!dev->ethtool_ops->set_wol)
876 return -EOPNOTSUPP;
877
878 if (copy_from_user(&wol, useraddr, sizeof(wol)))
879 return -EFAULT;
880
881 return dev->ethtool_ops->set_wol(dev, &wol);
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static int ethtool_nway_reset(struct net_device *dev)
885{
886 if (!dev->ethtool_ops->nway_reset)
887 return -EOPNOTSUPP;
888
889 return dev->ethtool_ops->nway_reset(dev);
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
893{
894 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700895 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700896 void __user *userbuf = useraddr + sizeof(eeprom);
897 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700899 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (!ops->get_eeprom || !ops->get_eeprom_len)
902 return -EOPNOTSUPP;
903
904 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
905 return -EFAULT;
906
907 /* Check for wrap and zero */
908 if (eeprom.offset + eeprom.len <= eeprom.offset)
909 return -EINVAL;
910
911 /* Check for exceeding total eeprom len */
912 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
913 return -EINVAL;
914
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700915 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!data)
917 return -ENOMEM;
918
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700919 bytes_remaining = eeprom.len;
920 while (bytes_remaining > 0) {
921 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700923 ret = ops->get_eeprom(dev, &eeprom, data);
924 if (ret)
925 break;
926 if (copy_to_user(userbuf, data, eeprom.len)) {
927 ret = -EFAULT;
928 break;
929 }
930 userbuf += eeprom.len;
931 eeprom.offset += eeprom.len;
932 bytes_remaining -= eeprom.len;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -0700935 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
936 eeprom.offset -= eeprom.len;
937 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
938 ret = -EFAULT;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 kfree(data);
941 return ret;
942}
943
944static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
945{
946 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700947 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700948 void __user *userbuf = useraddr + sizeof(eeprom);
949 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700951 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (!ops->set_eeprom || !ops->get_eeprom_len)
954 return -EOPNOTSUPP;
955
956 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
957 return -EFAULT;
958
959 /* Check for wrap and zero */
960 if (eeprom.offset + eeprom.len <= eeprom.offset)
961 return -EINVAL;
962
963 /* Check for exceeding total eeprom len */
964 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
965 return -EINVAL;
966
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700967 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!data)
969 return -ENOMEM;
970
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700971 bytes_remaining = eeprom.len;
972 while (bytes_remaining > 0) {
973 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -0700975 if (copy_from_user(data, userbuf, eeprom.len)) {
976 ret = -EFAULT;
977 break;
978 }
979 ret = ops->set_eeprom(dev, &eeprom, data);
980 if (ret)
981 break;
982 userbuf += eeprom.len;
983 eeprom.offset += eeprom.len;
984 bytes_remaining -= eeprom.len;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 kfree(data);
988 return ret;
989}
990
chavey97f8aef2010-04-07 21:54:42 -0700991static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
992 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Roland Dreier8e557422010-02-11 12:14:23 -0800994 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (!dev->ethtool_ops->get_coalesce)
997 return -EOPNOTSUPP;
998
999 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1000
1001 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1002 return -EFAULT;
1003 return 0;
1004}
1005
chavey97f8aef2010-04-07 21:54:42 -07001006static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1007 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct ethtool_coalesce coalesce;
1010
David S. Millerfa04ae52005-06-06 15:07:19 -07001011 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -EOPNOTSUPP;
1013
1014 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1015 return -EFAULT;
1016
1017 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1018}
1019
1020static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1021{
Roland Dreier8e557422010-02-11 12:14:23 -08001022 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (!dev->ethtool_ops->get_ringparam)
1025 return -EOPNOTSUPP;
1026
1027 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1028
1029 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1030 return -EFAULT;
1031 return 0;
1032}
1033
1034static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1035{
1036 struct ethtool_ringparam ringparam;
1037
1038 if (!dev->ethtool_ops->set_ringparam)
1039 return -EOPNOTSUPP;
1040
1041 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1042 return -EFAULT;
1043
1044 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1045}
1046
1047static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1048{
1049 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1050
1051 if (!dev->ethtool_ops->get_pauseparam)
1052 return -EOPNOTSUPP;
1053
1054 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1055
1056 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1057 return -EFAULT;
1058 return 0;
1059}
1060
1061static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1062{
1063 struct ethtool_pauseparam pauseparam;
1064
Jeff Garzike1b90c42006-07-17 12:54:40 -04001065 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return -EOPNOTSUPP;
1067
1068 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1069 return -EFAULT;
1070
1071 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static int __ethtool_set_sg(struct net_device *dev, u32 data)
1075{
1076 int err;
1077
1078 if (!data && dev->ethtool_ops->set_tso) {
1079 err = dev->ethtool_ops->set_tso(dev, 0);
1080 if (err)
1081 return err;
1082 }
1083
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001084 if (!data && dev->ethtool_ops->set_ufo) {
1085 err = dev->ethtool_ops->set_ufo(dev, 0);
1086 if (err)
1087 return err;
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return dev->ethtool_ops->set_sg(dev, data);
1090}
1091
1092static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
1093{
1094 struct ethtool_value edata;
1095 int err;
1096
1097 if (!dev->ethtool_ops->set_tx_csum)
1098 return -EOPNOTSUPP;
1099
1100 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1101 return -EFAULT;
1102
1103 if (!edata.data && dev->ethtool_ops->set_sg) {
1104 err = __ethtool_set_sg(dev, 0);
1105 if (err)
1106 return err;
1107 }
1108
1109 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
1110}
chavey97f8aef2010-04-07 21:54:42 -07001111EXPORT_SYMBOL(ethtool_op_set_tx_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Herbert Xub240a0e2008-12-15 23:44:31 -08001113static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
1114{
1115 struct ethtool_value edata;
1116
1117 if (!dev->ethtool_ops->set_rx_csum)
1118 return -EOPNOTSUPP;
1119
1120 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1121 return -EFAULT;
1122
1123 if (!edata.data && dev->ethtool_ops->set_sg)
1124 dev->features &= ~NETIF_F_GRO;
1125
1126 return dev->ethtool_ops->set_rx_csum(dev, edata.data);
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
1130{
1131 struct ethtool_value edata;
1132
1133 if (!dev->ethtool_ops->set_sg)
1134 return -EOPNOTSUPP;
1135
1136 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1137 return -EFAULT;
1138
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001139 if (edata.data &&
Herbert Xu8648b302006-06-17 22:06:05 -07001140 !(dev->features & NETIF_F_ALL_CSUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return -EINVAL;
1142
1143 return __ethtool_set_sg(dev, edata.data);
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
1147{
1148 struct ethtool_value edata;
1149
1150 if (!dev->ethtool_ops->set_tso)
1151 return -EOPNOTSUPP;
1152
1153 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1154 return -EFAULT;
1155
1156 if (edata.data && !(dev->features & NETIF_F_SG))
1157 return -EINVAL;
1158
1159 return dev->ethtool_ops->set_tso(dev, edata.data);
1160}
1161
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001162static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
1163{
1164 struct ethtool_value edata;
1165
1166 if (!dev->ethtool_ops->set_ufo)
1167 return -EOPNOTSUPP;
1168 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1169 return -EFAULT;
1170 if (edata.data && !(dev->features & NETIF_F_SG))
1171 return -EINVAL;
1172 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
1173 return -EINVAL;
1174 return dev->ethtool_ops->set_ufo(dev, edata.data);
1175}
1176
Herbert Xu37c31852006-06-22 03:07:29 -07001177static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
1178{
1179 struct ethtool_value edata = { ETHTOOL_GGSO };
1180
1181 edata.data = dev->features & NETIF_F_GSO;
1182 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001183 return -EFAULT;
Herbert Xu37c31852006-06-22 03:07:29 -07001184 return 0;
1185}
1186
1187static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
1188{
1189 struct ethtool_value edata;
1190
1191 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1192 return -EFAULT;
1193 if (edata.data)
1194 dev->features |= NETIF_F_GSO;
1195 else
1196 dev->features &= ~NETIF_F_GSO;
1197 return 0;
1198}
1199
Herbert Xub240a0e2008-12-15 23:44:31 -08001200static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
1201{
1202 struct ethtool_value edata = { ETHTOOL_GGRO };
1203
1204 edata.data = dev->features & NETIF_F_GRO;
1205 if (copy_to_user(useraddr, &edata, sizeof(edata)))
chavey97f8aef2010-04-07 21:54:42 -07001206 return -EFAULT;
Herbert Xub240a0e2008-12-15 23:44:31 -08001207 return 0;
1208}
1209
1210static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
1211{
1212 struct ethtool_value edata;
1213
1214 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1215 return -EFAULT;
1216
1217 if (edata.data) {
Eric Dumazet67c96602010-09-17 11:56:18 -07001218 u32 rxcsum = dev->ethtool_ops->get_rx_csum ?
1219 dev->ethtool_ops->get_rx_csum(dev) :
1220 ethtool_op_get_rx_csum(dev);
1221
1222 if (!rxcsum)
Herbert Xub240a0e2008-12-15 23:44:31 -08001223 return -EINVAL;
1224 dev->features |= NETIF_F_GRO;
1225 } else
1226 dev->features &= ~NETIF_F_GRO;
1227
1228 return 0;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1232{
1233 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001234 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001236 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001238 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001239 return -EOPNOTSUPP;
1240
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001241 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001242 if (test_len < 0)
1243 return test_len;
1244 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (copy_from_user(&test, useraddr, sizeof(test)))
1247 return -EFAULT;
1248
Jeff Garzikff03d492007-08-15 16:01:08 -07001249 test.len = test_len;
1250 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (!data)
1252 return -ENOMEM;
1253
1254 ops->self_test(dev, &test, data);
1255
1256 ret = -EFAULT;
1257 if (copy_to_user(useraddr, &test, sizeof(test)))
1258 goto out;
1259 useraddr += sizeof(test);
1260 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1261 goto out;
1262 ret = 0;
1263
1264 out:
1265 kfree(data);
1266 return ret;
1267}
1268
1269static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1270{
1271 struct ethtool_gstrings gstrings;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001272 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 u8 *data;
1274 int ret;
1275
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001276 if (!ops->get_strings || !ops->get_sset_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EOPNOTSUPP;
1278
1279 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1280 return -EFAULT;
1281
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001282 ret = ops->get_sset_count(dev, gstrings.string_set);
1283 if (ret < 0)
1284 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001285
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001286 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
1289 if (!data)
1290 return -ENOMEM;
1291
1292 ops->get_strings(dev, gstrings.string_set, data);
1293
1294 ret = -EFAULT;
1295 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1296 goto out;
1297 useraddr += sizeof(gstrings);
1298 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1299 goto out;
1300 ret = 0;
1301
1302 out:
1303 kfree(data);
1304 return ret;
1305}
1306
1307static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1308{
1309 struct ethtool_value id;
1310
1311 if (!dev->ethtool_ops->phys_id)
1312 return -EOPNOTSUPP;
1313
1314 if (copy_from_user(&id, useraddr, sizeof(id)))
1315 return -EFAULT;
1316
1317 return dev->ethtool_ops->phys_id(dev, id.data);
1318}
1319
1320static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1321{
1322 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001323 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001325 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001327 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001328 return -EOPNOTSUPP;
1329
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001330 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001331 if (n_stats < 0)
1332 return n_stats;
1333 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1336 return -EFAULT;
1337
Jeff Garzikff03d492007-08-15 16:01:08 -07001338 stats.n_stats = n_stats;
1339 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!data)
1341 return -ENOMEM;
1342
1343 ops->get_ethtool_stats(dev, &stats, data);
1344
1345 ret = -EFAULT;
1346 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1347 goto out;
1348 useraddr += sizeof(stats);
1349 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1350 goto out;
1351 ret = 0;
1352
1353 out:
1354 kfree(data);
1355 return ret;
1356}
1357
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001358static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001359{
1360 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001361
Matthew Wilcox313674a2007-07-31 14:00:29 -07001362 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001363 return -EFAULT;
1364
Matthew Wilcox313674a2007-07-31 14:00:29 -07001365 if (epaddr.size < dev->addr_len)
1366 return -ETOOSMALL;
1367 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001368
Jon Wetzela6f9a702005-08-20 17:15:54 -07001369 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001370 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001371 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001372 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1373 return -EFAULT;
1374 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001375}
1376
Jeff Garzik13c99b22007-08-15 16:01:56 -07001377static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1378 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001379{
Roland Dreier8e557422010-02-11 12:14:23 -08001380 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001381
Jeff Garzik13c99b22007-08-15 16:01:56 -07001382 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001383 return -EOPNOTSUPP;
1384
Jeff Garzik13c99b22007-08-15 16:01:56 -07001385 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001386
1387 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1388 return -EFAULT;
1389 return 0;
1390}
1391
Jeff Garzik13c99b22007-08-15 16:01:56 -07001392static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1393 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001394{
1395 struct ethtool_value edata;
1396
Jeff Garzik13c99b22007-08-15 16:01:56 -07001397 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001398 return -EOPNOTSUPP;
1399
1400 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1401 return -EFAULT;
1402
Jeff Garzik13c99b22007-08-15 16:01:56 -07001403 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001404 return 0;
1405}
1406
Jeff Garzik13c99b22007-08-15 16:01:56 -07001407static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
1408 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07001409{
1410 struct ethtool_value edata;
1411
Jeff Garzik13c99b22007-08-15 16:01:56 -07001412 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07001413 return -EOPNOTSUPP;
1414
1415 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1416 return -EFAULT;
1417
Jeff Garzik13c99b22007-08-15 16:01:56 -07001418 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07001419}
1420
chavey97f8aef2010-04-07 21:54:42 -07001421static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
1422 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001423{
1424 struct ethtool_flash efl;
1425
1426 if (copy_from_user(&efl, useraddr, sizeof(efl)))
1427 return -EFAULT;
1428
1429 if (!dev->ethtool_ops->flash_device)
1430 return -EOPNOTSUPP;
1431
1432 return dev->ethtool_ops->flash_device(dev, &efl);
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/* The main entry point in this file. Called from net/core/dev.c */
1436
Eric W. Biederman881d9662007-09-17 11:56:21 -07001437int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001439 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 void __user *useraddr = ifr->ifr_data;
1441 u32 ethcmd;
1442 int rc;
Stephen Hemminger81e81572005-05-29 14:14:35 -07001443 unsigned long old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (!dev || !netif_device_present(dev))
1446 return -ENODEV;
1447
chavey97f8aef2010-04-07 21:54:42 -07001448 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EFAULT;
1450
Ben Hutchings01414802010-08-17 02:31:15 -07001451 if (!dev->ethtool_ops) {
1452 /* ETHTOOL_GDRVINFO does not require any driver support.
1453 * It is also unprivileged and does not change anything,
1454 * so we can take a shortcut to it. */
1455 if (ethcmd == ETHTOOL_GDRVINFO)
1456 return ethtool_get_drvinfo(dev, useraddr);
1457 else
1458 return -EOPNOTSUPP;
1459 }
1460
Stephen Hemminger75f31232006-09-28 15:13:37 -07001461 /* Allow some commands to be done by anyone */
chavey97f8aef2010-04-07 21:54:42 -07001462 switch (ethcmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00001463 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001464 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001465 case ETHTOOL_GMSGLVL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001466 case ETHTOOL_GCOALESCE:
1467 case ETHTOOL_GRINGPARAM:
1468 case ETHTOOL_GPAUSEPARAM:
1469 case ETHTOOL_GRXCSUM:
1470 case ETHTOOL_GTXCSUM:
1471 case ETHTOOL_GSG:
1472 case ETHTOOL_GSTRINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001473 case ETHTOOL_GTSO:
1474 case ETHTOOL_GPERMADDR:
1475 case ETHTOOL_GUFO:
1476 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00001477 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07001478 case ETHTOOL_GFLAGS:
1479 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07001480 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001481 case ETHTOOL_GRXRINGS:
1482 case ETHTOOL_GRXCLSRLCNT:
1483 case ETHTOOL_GRXCLSRULE:
1484 case ETHTOOL_GRXCLSRLALL:
Stephen Hemminger75f31232006-09-28 15:13:37 -07001485 break;
1486 default:
1487 if (!capable(CAP_NET_ADMIN))
1488 return -EPERM;
1489 }
1490
chavey97f8aef2010-04-07 21:54:42 -07001491 if (dev->ethtool_ops->begin) {
1492 rc = dev->ethtool_ops->begin(dev);
1493 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return rc;
chavey97f8aef2010-04-07 21:54:42 -07001495 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001496 old_features = dev->features;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 switch (ethcmd) {
1499 case ETHTOOL_GSET:
1500 rc = ethtool_get_settings(dev, useraddr);
1501 break;
1502 case ETHTOOL_SSET:
1503 rc = ethtool_set_settings(dev, useraddr);
1504 break;
1505 case ETHTOOL_GDRVINFO:
1506 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 break;
1508 case ETHTOOL_GREGS:
1509 rc = ethtool_get_regs(dev, useraddr);
1510 break;
1511 case ETHTOOL_GWOL:
1512 rc = ethtool_get_wol(dev, useraddr);
1513 break;
1514 case ETHTOOL_SWOL:
1515 rc = ethtool_set_wol(dev, useraddr);
1516 break;
1517 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001518 rc = ethtool_get_value(dev, useraddr, ethcmd,
1519 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001522 rc = ethtool_set_value_void(dev, useraddr,
1523 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 case ETHTOOL_NWAY_RST:
1526 rc = ethtool_nway_reset(dev);
1527 break;
1528 case ETHTOOL_GLINK:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001529 rc = ethtool_get_value(dev, useraddr, ethcmd,
1530 dev->ethtool_ops->get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 break;
1532 case ETHTOOL_GEEPROM:
1533 rc = ethtool_get_eeprom(dev, useraddr);
1534 break;
1535 case ETHTOOL_SEEPROM:
1536 rc = ethtool_set_eeprom(dev, useraddr);
1537 break;
1538 case ETHTOOL_GCOALESCE:
1539 rc = ethtool_get_coalesce(dev, useraddr);
1540 break;
1541 case ETHTOOL_SCOALESCE:
1542 rc = ethtool_set_coalesce(dev, useraddr);
1543 break;
1544 case ETHTOOL_GRINGPARAM:
1545 rc = ethtool_get_ringparam(dev, useraddr);
1546 break;
1547 case ETHTOOL_SRINGPARAM:
1548 rc = ethtool_set_ringparam(dev, useraddr);
1549 break;
1550 case ETHTOOL_GPAUSEPARAM:
1551 rc = ethtool_get_pauseparam(dev, useraddr);
1552 break;
1553 case ETHTOOL_SPAUSEPARAM:
1554 rc = ethtool_set_pauseparam(dev, useraddr);
1555 break;
1556 case ETHTOOL_GRXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001557 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001558 (dev->ethtool_ops->get_rx_csum ?
1559 dev->ethtool_ops->get_rx_csum :
1560 ethtool_op_get_rx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 break;
1562 case ETHTOOL_SRXCSUM:
Herbert Xub240a0e2008-12-15 23:44:31 -08001563 rc = ethtool_set_rx_csum(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565 case ETHTOOL_GTXCSUM:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001566 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001567 (dev->ethtool_ops->get_tx_csum ?
1568 dev->ethtool_ops->get_tx_csum :
1569 ethtool_op_get_tx_csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 break;
1571 case ETHTOOL_STXCSUM:
1572 rc = ethtool_set_tx_csum(dev, useraddr);
1573 break;
1574 case ETHTOOL_GSG:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001575 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001576 (dev->ethtool_ops->get_sg ?
1577 dev->ethtool_ops->get_sg :
1578 ethtool_op_get_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
1580 case ETHTOOL_SSG:
1581 rc = ethtool_set_sg(dev, useraddr);
1582 break;
1583 case ETHTOOL_GTSO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001584 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001585 (dev->ethtool_ops->get_tso ?
1586 dev->ethtool_ops->get_tso :
1587 ethtool_op_get_tso));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
1589 case ETHTOOL_STSO:
1590 rc = ethtool_set_tso(dev, useraddr);
1591 break;
1592 case ETHTOOL_TEST:
1593 rc = ethtool_self_test(dev, useraddr);
1594 break;
1595 case ETHTOOL_GSTRINGS:
1596 rc = ethtool_get_strings(dev, useraddr);
1597 break;
1598 case ETHTOOL_PHYS_ID:
1599 rc = ethtool_phys_id(dev, useraddr);
1600 break;
1601 case ETHTOOL_GSTATS:
1602 rc = ethtool_get_stats(dev, useraddr);
1603 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001604 case ETHTOOL_GPERMADDR:
1605 rc = ethtool_get_perm_addr(dev, useraddr);
1606 break;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001607 case ETHTOOL_GUFO:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001608 rc = ethtool_get_value(dev, useraddr, ethcmd,
Jeff Garzik88d3aaf2007-09-15 14:41:06 -07001609 (dev->ethtool_ops->get_ufo ?
1610 dev->ethtool_ops->get_ufo :
1611 ethtool_op_get_ufo));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001612 break;
1613 case ETHTOOL_SUFO:
1614 rc = ethtool_set_ufo(dev, useraddr);
1615 break;
Herbert Xu37c31852006-06-22 03:07:29 -07001616 case ETHTOOL_GGSO:
1617 rc = ethtool_get_gso(dev, useraddr);
1618 break;
1619 case ETHTOOL_SGSO:
1620 rc = ethtool_set_gso(dev, useraddr);
1621 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001622 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001623 rc = ethtool_get_value(dev, useraddr, ethcmd,
Sridhar Samudrala1896e612009-07-22 13:38:22 +00001624 (dev->ethtool_ops->get_flags ?
1625 dev->ethtool_ops->get_flags :
1626 ethtool_op_get_flags));
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001627 break;
1628 case ETHTOOL_SFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001629 rc = ethtool_set_value(dev, useraddr,
1630 dev->ethtool_ops->set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001631 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07001632 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001633 rc = ethtool_get_value(dev, useraddr, ethcmd,
1634 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001635 break;
1636 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07001637 rc = ethtool_set_value(dev, useraddr,
1638 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07001639 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07001640 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001641 case ETHTOOL_GRXRINGS:
1642 case ETHTOOL_GRXCLSRLCNT:
1643 case ETHTOOL_GRXCLSRULE:
1644 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001645 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001646 break;
1647 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08001648 case ETHTOOL_SRXCLSRLDEL:
1649 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00001650 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07001651 break;
Herbert Xub240a0e2008-12-15 23:44:31 -08001652 case ETHTOOL_GGRO:
1653 rc = ethtool_get_gro(dev, useraddr);
1654 break;
1655 case ETHTOOL_SGRO:
1656 rc = ethtool_set_gro(dev, useraddr);
1657 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00001658 case ETHTOOL_FLASHDEV:
1659 rc = ethtool_flash_device(dev, useraddr);
1660 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001661 case ETHTOOL_RESET:
1662 rc = ethtool_reset(dev, useraddr);
1663 break;
Peter P Waskiewicz Jr15682bc2010-02-10 20:03:05 -08001664 case ETHTOOL_SRXNTUPLE:
1665 rc = ethtool_set_rx_ntuple(dev, useraddr);
1666 break;
1667 case ETHTOOL_GRXNTUPLE:
1668 rc = ethtool_get_rx_ntuple(dev, useraddr);
1669 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00001670 case ETHTOOL_GSSET_INFO:
1671 rc = ethtool_get_sset_info(dev, useraddr);
1672 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001673 case ETHTOOL_GRXFHINDIR:
1674 rc = ethtool_get_rxfh_indir(dev, useraddr);
1675 break;
1676 case ETHTOOL_SRXFHINDIR:
1677 rc = ethtool_set_rxfh_indir(dev, useraddr);
1678 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07001680 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001682
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001683 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07001685
1686 if (old_features != dev->features)
1687 netdev_features_change(dev);
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}