Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 6 | * the information ethtool needs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/ethtool.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 22 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 33 | u32 ethtool_op_get_tx_csum(struct net_device *dev) |
| 34 | { |
Herbert Xu | 8648b30 | 2006-06-17 22:06:05 -0700 | [diff] [blame] | 35 | return (dev->features & NETIF_F_ALL_CSUM) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | int ethtool_op_set_tx_csum(struct net_device *dev, u32 data) |
| 39 | { |
| 40 | if (data) |
| 41 | dev->features |= NETIF_F_IP_CSUM; |
| 42 | else |
| 43 | dev->features &= ~NETIF_F_IP_CSUM; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jon Mason | 69f6a0f | 2005-05-29 20:27:24 -0700 | [diff] [blame] | 48 | int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data) |
| 49 | { |
| 50 | if (data) |
| 51 | dev->features |= NETIF_F_HW_CSUM; |
| 52 | else |
| 53 | dev->features &= ~NETIF_F_HW_CSUM; |
| 54 | |
| 55 | return 0; |
| 56 | } |
Michael Chan | 6460d94 | 2007-07-14 19:07:52 -0700 | [diff] [blame] | 57 | |
| 58 | int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data) |
| 59 | { |
| 60 | if (data) |
| 61 | dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; |
| 62 | else |
| 63 | dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | u32 ethtool_op_get_sg(struct net_device *dev) |
| 69 | { |
| 70 | return (dev->features & NETIF_F_SG) != 0; |
| 71 | } |
| 72 | |
| 73 | int ethtool_op_set_sg(struct net_device *dev, u32 data) |
| 74 | { |
| 75 | if (data) |
| 76 | dev->features |= NETIF_F_SG; |
| 77 | else |
| 78 | dev->features &= ~NETIF_F_SG; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | u32 ethtool_op_get_tso(struct net_device *dev) |
| 84 | { |
| 85 | return (dev->features & NETIF_F_TSO) != 0; |
| 86 | } |
| 87 | |
| 88 | int ethtool_op_set_tso(struct net_device *dev, u32 data) |
| 89 | { |
| 90 | if (data) |
| 91 | dev->features |= NETIF_F_TSO; |
| 92 | else |
| 93 | dev->features &= ~NETIF_F_TSO; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 98 | u32 ethtool_op_get_ufo(struct net_device *dev) |
| 99 | { |
| 100 | return (dev->features & NETIF_F_UFO) != 0; |
| 101 | } |
| 102 | |
| 103 | int ethtool_op_set_ufo(struct net_device *dev, u32 data) |
| 104 | { |
| 105 | if (data) |
| 106 | dev->features |= NETIF_F_UFO; |
| 107 | else |
| 108 | dev->features &= ~NETIF_F_UFO; |
| 109 | return 0; |
| 110 | } |
| 111 | |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 112 | /* the following list of flags are the same as their associated |
| 113 | * NETIF_F_xxx values in include/linux/netdevice.h |
| 114 | */ |
| 115 | static const u32 flags_dup_features = |
| 116 | ETH_FLAG_LRO; |
| 117 | |
| 118 | u32 ethtool_op_get_flags(struct net_device *dev) |
| 119 | { |
| 120 | /* in the future, this function will probably contain additional |
| 121 | * handling for flags which are not so easily handled |
| 122 | * by a simple masking operation |
| 123 | */ |
| 124 | |
| 125 | return dev->features & flags_dup_features; |
| 126 | } |
| 127 | |
| 128 | int ethtool_op_set_flags(struct net_device *dev, u32 data) |
| 129 | { |
| 130 | if (data & ETH_FLAG_LRO) |
| 131 | dev->features |= NETIF_F_LRO; |
| 132 | else |
| 133 | dev->features &= ~NETIF_F_LRO; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* Handlers for each ethtool command */ |
| 139 | |
| 140 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) |
| 141 | { |
| 142 | struct ethtool_cmd cmd = { ETHTOOL_GSET }; |
| 143 | int err; |
| 144 | |
| 145 | if (!dev->ethtool_ops->get_settings) |
| 146 | return -EOPNOTSUPP; |
| 147 | |
| 148 | err = dev->ethtool_ops->get_settings(dev, &cmd); |
| 149 | if (err < 0) |
| 150 | return err; |
| 151 | |
| 152 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 153 | return -EFAULT; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) |
| 158 | { |
| 159 | struct ethtool_cmd cmd; |
| 160 | |
| 161 | if (!dev->ethtool_ops->set_settings) |
| 162 | return -EOPNOTSUPP; |
| 163 | |
| 164 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 165 | return -EFAULT; |
| 166 | |
| 167 | return dev->ethtool_ops->set_settings(dev, &cmd); |
| 168 | } |
| 169 | |
| 170 | static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr) |
| 171 | { |
| 172 | struct ethtool_drvinfo info; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 173 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | if (!ops->get_drvinfo) |
| 176 | return -EOPNOTSUPP; |
| 177 | |
| 178 | memset(&info, 0, sizeof(info)); |
| 179 | info.cmd = ETHTOOL_GDRVINFO; |
| 180 | ops->get_drvinfo(dev, &info); |
| 181 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 182 | if (ops->get_sset_count) { |
| 183 | int rc; |
| 184 | |
| 185 | rc = ops->get_sset_count(dev, ETH_SS_TEST); |
| 186 | if (rc >= 0) |
| 187 | info.testinfo_len = rc; |
| 188 | rc = ops->get_sset_count(dev, ETH_SS_STATS); |
| 189 | if (rc >= 0) |
| 190 | info.n_stats = rc; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 191 | rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); |
| 192 | if (rc >= 0) |
| 193 | info.n_priv_flags = rc; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 194 | } else { |
| 195 | /* code path for obsolete hooks */ |
| 196 | |
| 197 | if (ops->self_test_count) |
| 198 | info.testinfo_len = ops->self_test_count(dev); |
| 199 | if (ops->get_stats_count) |
| 200 | info.n_stats = ops->get_stats_count(dev); |
| 201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (ops->get_regs_len) |
| 203 | info.regdump_len = ops->get_regs_len(dev); |
| 204 | if (ops->get_eeprom_len) |
| 205 | info.eedump_len = ops->get_eeprom_len(dev); |
| 206 | |
| 207 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 208 | return -EFAULT; |
| 209 | return 0; |
| 210 | } |
| 211 | |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 212 | static int ethtool_set_rxhash(struct net_device *dev, void __user *useraddr) |
| 213 | { |
| 214 | struct ethtool_rxnfc cmd; |
| 215 | |
| 216 | if (!dev->ethtool_ops->set_rxhash) |
| 217 | return -EOPNOTSUPP; |
| 218 | |
| 219 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 220 | return -EFAULT; |
| 221 | |
| 222 | return dev->ethtool_ops->set_rxhash(dev, &cmd); |
| 223 | } |
| 224 | |
| 225 | static int ethtool_get_rxhash(struct net_device *dev, void __user *useraddr) |
| 226 | { |
| 227 | struct ethtool_rxnfc info; |
| 228 | |
| 229 | if (!dev->ethtool_ops->get_rxhash) |
| 230 | return -EOPNOTSUPP; |
| 231 | |
| 232 | if (copy_from_user(&info, useraddr, sizeof(info))) |
| 233 | return -EFAULT; |
| 234 | |
| 235 | dev->ethtool_ops->get_rxhash(dev, &info); |
| 236 | |
| 237 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 238 | return -EFAULT; |
| 239 | return 0; |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) |
| 243 | { |
| 244 | struct ethtool_regs regs; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 245 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | void *regbuf; |
| 247 | int reglen, ret; |
| 248 | |
| 249 | if (!ops->get_regs || !ops->get_regs_len) |
| 250 | return -EOPNOTSUPP; |
| 251 | |
| 252 | if (copy_from_user(®s, useraddr, sizeof(regs))) |
| 253 | return -EFAULT; |
| 254 | |
| 255 | reglen = ops->get_regs_len(dev); |
| 256 | if (regs.len > reglen) |
| 257 | regs.len = reglen; |
| 258 | |
| 259 | regbuf = kmalloc(reglen, GFP_USER); |
| 260 | if (!regbuf) |
| 261 | return -ENOMEM; |
| 262 | |
| 263 | ops->get_regs(dev, ®s, regbuf); |
| 264 | |
| 265 | ret = -EFAULT; |
| 266 | if (copy_to_user(useraddr, ®s, sizeof(regs))) |
| 267 | goto out; |
| 268 | useraddr += offsetof(struct ethtool_regs, data); |
| 269 | if (copy_to_user(useraddr, regbuf, regs.len)) |
| 270 | goto out; |
| 271 | ret = 0; |
| 272 | |
| 273 | out: |
| 274 | kfree(regbuf); |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) |
| 279 | { |
| 280 | struct ethtool_wolinfo wol = { ETHTOOL_GWOL }; |
| 281 | |
| 282 | if (!dev->ethtool_ops->get_wol) |
| 283 | return -EOPNOTSUPP; |
| 284 | |
| 285 | dev->ethtool_ops->get_wol(dev, &wol); |
| 286 | |
| 287 | if (copy_to_user(useraddr, &wol, sizeof(wol))) |
| 288 | return -EFAULT; |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) |
| 293 | { |
| 294 | struct ethtool_wolinfo wol; |
| 295 | |
| 296 | if (!dev->ethtool_ops->set_wol) |
| 297 | return -EOPNOTSUPP; |
| 298 | |
| 299 | if (copy_from_user(&wol, useraddr, sizeof(wol))) |
| 300 | return -EFAULT; |
| 301 | |
| 302 | return dev->ethtool_ops->set_wol(dev, &wol); |
| 303 | } |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | static int ethtool_nway_reset(struct net_device *dev) |
| 306 | { |
| 307 | if (!dev->ethtool_ops->nway_reset) |
| 308 | return -EOPNOTSUPP; |
| 309 | |
| 310 | return dev->ethtool_ops->nway_reset(dev); |
| 311 | } |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) |
| 314 | { |
| 315 | struct ethtool_eeprom eeprom; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 316 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 317 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 318 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 320 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | if (!ops->get_eeprom || !ops->get_eeprom_len) |
| 323 | return -EOPNOTSUPP; |
| 324 | |
| 325 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 326 | return -EFAULT; |
| 327 | |
| 328 | /* Check for wrap and zero */ |
| 329 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 330 | return -EINVAL; |
| 331 | |
| 332 | /* Check for exceeding total eeprom len */ |
| 333 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) |
| 334 | return -EINVAL; |
| 335 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 336 | data = kmalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | if (!data) |
| 338 | return -ENOMEM; |
| 339 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 340 | bytes_remaining = eeprom.len; |
| 341 | while (bytes_remaining > 0) { |
| 342 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 344 | ret = ops->get_eeprom(dev, &eeprom, data); |
| 345 | if (ret) |
| 346 | break; |
| 347 | if (copy_to_user(userbuf, data, eeprom.len)) { |
| 348 | ret = -EFAULT; |
| 349 | break; |
| 350 | } |
| 351 | userbuf += eeprom.len; |
| 352 | eeprom.offset += eeprom.len; |
| 353 | bytes_remaining -= eeprom.len; |
| 354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Mandeep Singh Baines | c5835df | 2008-04-24 20:55:56 -0700 | [diff] [blame] | 356 | eeprom.len = userbuf - (useraddr + sizeof(eeprom)); |
| 357 | eeprom.offset -= eeprom.len; |
| 358 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) |
| 359 | ret = -EFAULT; |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | kfree(data); |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) |
| 366 | { |
| 367 | struct ethtool_eeprom eeprom; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 368 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 369 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 370 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 372 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | if (!ops->set_eeprom || !ops->get_eeprom_len) |
| 375 | return -EOPNOTSUPP; |
| 376 | |
| 377 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 378 | return -EFAULT; |
| 379 | |
| 380 | /* Check for wrap and zero */ |
| 381 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | /* Check for exceeding total eeprom len */ |
| 385 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) |
| 386 | return -EINVAL; |
| 387 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 388 | data = kmalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (!data) |
| 390 | return -ENOMEM; |
| 391 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 392 | bytes_remaining = eeprom.len; |
| 393 | while (bytes_remaining > 0) { |
| 394 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 396 | if (copy_from_user(data, userbuf, eeprom.len)) { |
| 397 | ret = -EFAULT; |
| 398 | break; |
| 399 | } |
| 400 | ret = ops->set_eeprom(dev, &eeprom, data); |
| 401 | if (ret) |
| 402 | break; |
| 403 | userbuf += eeprom.len; |
| 404 | eeprom.offset += eeprom.len; |
| 405 | bytes_remaining -= eeprom.len; |
| 406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | kfree(data); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr) |
| 413 | { |
| 414 | struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE }; |
| 415 | |
| 416 | if (!dev->ethtool_ops->get_coalesce) |
| 417 | return -EOPNOTSUPP; |
| 418 | |
| 419 | dev->ethtool_ops->get_coalesce(dev, &coalesce); |
| 420 | |
| 421 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 422 | return -EFAULT; |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr) |
| 427 | { |
| 428 | struct ethtool_coalesce coalesce; |
| 429 | |
David S. Miller | fa04ae5 | 2005-06-06 15:07:19 -0700 | [diff] [blame] | 430 | if (!dev->ethtool_ops->set_coalesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return -EOPNOTSUPP; |
| 432 | |
| 433 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) |
| 434 | return -EFAULT; |
| 435 | |
| 436 | return dev->ethtool_ops->set_coalesce(dev, &coalesce); |
| 437 | } |
| 438 | |
| 439 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) |
| 440 | { |
| 441 | struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM }; |
| 442 | |
| 443 | if (!dev->ethtool_ops->get_ringparam) |
| 444 | return -EOPNOTSUPP; |
| 445 | |
| 446 | dev->ethtool_ops->get_ringparam(dev, &ringparam); |
| 447 | |
| 448 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) |
| 449 | return -EFAULT; |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) |
| 454 | { |
| 455 | struct ethtool_ringparam ringparam; |
| 456 | |
| 457 | if (!dev->ethtool_ops->set_ringparam) |
| 458 | return -EOPNOTSUPP; |
| 459 | |
| 460 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) |
| 461 | return -EFAULT; |
| 462 | |
| 463 | return dev->ethtool_ops->set_ringparam(dev, &ringparam); |
| 464 | } |
| 465 | |
| 466 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) |
| 467 | { |
| 468 | struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; |
| 469 | |
| 470 | if (!dev->ethtool_ops->get_pauseparam) |
| 471 | return -EOPNOTSUPP; |
| 472 | |
| 473 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); |
| 474 | |
| 475 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) |
| 476 | return -EFAULT; |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) |
| 481 | { |
| 482 | struct ethtool_pauseparam pauseparam; |
| 483 | |
Jeff Garzik | e1b90c4 | 2006-07-17 12:54:40 -0400 | [diff] [blame] | 484 | if (!dev->ethtool_ops->set_pauseparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return -EOPNOTSUPP; |
| 486 | |
| 487 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) |
| 488 | return -EFAULT; |
| 489 | |
| 490 | return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); |
| 491 | } |
| 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | static int __ethtool_set_sg(struct net_device *dev, u32 data) |
| 494 | { |
| 495 | int err; |
| 496 | |
| 497 | if (!data && dev->ethtool_ops->set_tso) { |
| 498 | err = dev->ethtool_ops->set_tso(dev, 0); |
| 499 | if (err) |
| 500 | return err; |
| 501 | } |
| 502 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 503 | if (!data && dev->ethtool_ops->set_ufo) { |
| 504 | err = dev->ethtool_ops->set_ufo(dev, 0); |
| 505 | if (err) |
| 506 | return err; |
| 507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return dev->ethtool_ops->set_sg(dev, data); |
| 509 | } |
| 510 | |
| 511 | static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr) |
| 512 | { |
| 513 | struct ethtool_value edata; |
| 514 | int err; |
| 515 | |
| 516 | if (!dev->ethtool_ops->set_tx_csum) |
| 517 | return -EOPNOTSUPP; |
| 518 | |
| 519 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 520 | return -EFAULT; |
| 521 | |
| 522 | if (!edata.data && dev->ethtool_ops->set_sg) { |
| 523 | err = __ethtool_set_sg(dev, 0); |
| 524 | if (err) |
| 525 | return err; |
| 526 | } |
| 527 | |
| 528 | return dev->ethtool_ops->set_tx_csum(dev, edata.data); |
| 529 | } |
| 530 | |
Herbert Xu | b240a0e | 2008-12-15 23:44:31 -0800 | [diff] [blame^] | 531 | static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr) |
| 532 | { |
| 533 | struct ethtool_value edata; |
| 534 | |
| 535 | if (!dev->ethtool_ops->set_rx_csum) |
| 536 | return -EOPNOTSUPP; |
| 537 | |
| 538 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 539 | return -EFAULT; |
| 540 | |
| 541 | if (!edata.data && dev->ethtool_ops->set_sg) |
| 542 | dev->features &= ~NETIF_F_GRO; |
| 543 | |
| 544 | return dev->ethtool_ops->set_rx_csum(dev, edata.data); |
| 545 | } |
| 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | static int ethtool_set_sg(struct net_device *dev, char __user *useraddr) |
| 548 | { |
| 549 | struct ethtool_value edata; |
| 550 | |
| 551 | if (!dev->ethtool_ops->set_sg) |
| 552 | return -EOPNOTSUPP; |
| 553 | |
| 554 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 555 | return -EFAULT; |
| 556 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 557 | if (edata.data && |
Herbert Xu | 8648b30 | 2006-06-17 22:06:05 -0700 | [diff] [blame] | 558 | !(dev->features & NETIF_F_ALL_CSUM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return -EINVAL; |
| 560 | |
| 561 | return __ethtool_set_sg(dev, edata.data); |
| 562 | } |
| 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | static int ethtool_set_tso(struct net_device *dev, char __user *useraddr) |
| 565 | { |
| 566 | struct ethtool_value edata; |
| 567 | |
| 568 | if (!dev->ethtool_ops->set_tso) |
| 569 | return -EOPNOTSUPP; |
| 570 | |
| 571 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 572 | return -EFAULT; |
| 573 | |
| 574 | if (edata.data && !(dev->features & NETIF_F_SG)) |
| 575 | return -EINVAL; |
| 576 | |
| 577 | return dev->ethtool_ops->set_tso(dev, edata.data); |
| 578 | } |
| 579 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 580 | static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr) |
| 581 | { |
| 582 | struct ethtool_value edata; |
| 583 | |
| 584 | if (!dev->ethtool_ops->set_ufo) |
| 585 | return -EOPNOTSUPP; |
| 586 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 587 | return -EFAULT; |
| 588 | if (edata.data && !(dev->features & NETIF_F_SG)) |
| 589 | return -EINVAL; |
| 590 | if (edata.data && !(dev->features & NETIF_F_HW_CSUM)) |
| 591 | return -EINVAL; |
| 592 | return dev->ethtool_ops->set_ufo(dev, edata.data); |
| 593 | } |
| 594 | |
Herbert Xu | 37c3185 | 2006-06-22 03:07:29 -0700 | [diff] [blame] | 595 | static int ethtool_get_gso(struct net_device *dev, char __user *useraddr) |
| 596 | { |
| 597 | struct ethtool_value edata = { ETHTOOL_GGSO }; |
| 598 | |
| 599 | edata.data = dev->features & NETIF_F_GSO; |
| 600 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 601 | return -EFAULT; |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static int ethtool_set_gso(struct net_device *dev, char __user *useraddr) |
| 606 | { |
| 607 | struct ethtool_value edata; |
| 608 | |
| 609 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 610 | return -EFAULT; |
| 611 | if (edata.data) |
| 612 | dev->features |= NETIF_F_GSO; |
| 613 | else |
| 614 | dev->features &= ~NETIF_F_GSO; |
| 615 | return 0; |
| 616 | } |
| 617 | |
Herbert Xu | b240a0e | 2008-12-15 23:44:31 -0800 | [diff] [blame^] | 618 | static int ethtool_get_gro(struct net_device *dev, char __user *useraddr) |
| 619 | { |
| 620 | struct ethtool_value edata = { ETHTOOL_GGRO }; |
| 621 | |
| 622 | edata.data = dev->features & NETIF_F_GRO; |
| 623 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 624 | return -EFAULT; |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static int ethtool_set_gro(struct net_device *dev, char __user *useraddr) |
| 629 | { |
| 630 | struct ethtool_value edata; |
| 631 | |
| 632 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 633 | return -EFAULT; |
| 634 | |
| 635 | if (edata.data) { |
| 636 | if (!dev->ethtool_ops->get_rx_csum || |
| 637 | !dev->ethtool_ops->get_rx_csum(dev)) |
| 638 | return -EINVAL; |
| 639 | dev->features |= NETIF_F_GRO; |
| 640 | } else |
| 641 | dev->features &= ~NETIF_F_GRO; |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
| 647 | { |
| 648 | struct ethtool_test test; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 649 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 651 | int ret, test_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 653 | if (!ops->self_test) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return -EOPNOTSUPP; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 655 | if (!ops->get_sset_count && !ops->self_test_count) |
| 656 | return -EOPNOTSUPP; |
| 657 | |
| 658 | if (ops->get_sset_count) |
| 659 | test_len = ops->get_sset_count(dev, ETH_SS_TEST); |
| 660 | else |
| 661 | /* code path for obsolete hook */ |
| 662 | test_len = ops->self_test_count(dev); |
| 663 | if (test_len < 0) |
| 664 | return test_len; |
| 665 | WARN_ON(test_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | if (copy_from_user(&test, useraddr, sizeof(test))) |
| 668 | return -EFAULT; |
| 669 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 670 | test.len = test_len; |
| 671 | data = kmalloc(test_len * sizeof(u64), GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (!data) |
| 673 | return -ENOMEM; |
| 674 | |
| 675 | ops->self_test(dev, &test, data); |
| 676 | |
| 677 | ret = -EFAULT; |
| 678 | if (copy_to_user(useraddr, &test, sizeof(test))) |
| 679 | goto out; |
| 680 | useraddr += sizeof(test); |
| 681 | if (copy_to_user(useraddr, data, test.len * sizeof(u64))) |
| 682 | goto out; |
| 683 | ret = 0; |
| 684 | |
| 685 | out: |
| 686 | kfree(data); |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) |
| 691 | { |
| 692 | struct ethtool_gstrings gstrings; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 693 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | u8 *data; |
| 695 | int ret; |
| 696 | |
| 697 | if (!ops->get_strings) |
| 698 | return -EOPNOTSUPP; |
| 699 | |
| 700 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) |
| 701 | return -EFAULT; |
| 702 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 703 | if (ops->get_sset_count) { |
| 704 | ret = ops->get_sset_count(dev, gstrings.string_set); |
| 705 | if (ret < 0) |
| 706 | return ret; |
| 707 | |
| 708 | gstrings.len = ret; |
| 709 | } else { |
| 710 | /* code path for obsolete hooks */ |
| 711 | |
| 712 | switch (gstrings.string_set) { |
| 713 | case ETH_SS_TEST: |
| 714 | if (!ops->self_test_count) |
| 715 | return -EOPNOTSUPP; |
| 716 | gstrings.len = ops->self_test_count(dev); |
| 717 | break; |
| 718 | case ETH_SS_STATS: |
| 719 | if (!ops->get_stats_count) |
| 720 | return -EOPNOTSUPP; |
| 721 | gstrings.len = ops->get_stats_count(dev); |
| 722 | break; |
| 723 | default: |
| 724 | return -EINVAL; |
| 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER); |
| 729 | if (!data) |
| 730 | return -ENOMEM; |
| 731 | |
| 732 | ops->get_strings(dev, gstrings.string_set, data); |
| 733 | |
| 734 | ret = -EFAULT; |
| 735 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) |
| 736 | goto out; |
| 737 | useraddr += sizeof(gstrings); |
| 738 | if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) |
| 739 | goto out; |
| 740 | ret = 0; |
| 741 | |
| 742 | out: |
| 743 | kfree(data); |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) |
| 748 | { |
| 749 | struct ethtool_value id; |
| 750 | |
| 751 | if (!dev->ethtool_ops->phys_id) |
| 752 | return -EOPNOTSUPP; |
| 753 | |
| 754 | if (copy_from_user(&id, useraddr, sizeof(id))) |
| 755 | return -EFAULT; |
| 756 | |
| 757 | return dev->ethtool_ops->phys_id(dev, id.data); |
| 758 | } |
| 759 | |
| 760 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) |
| 761 | { |
| 762 | struct ethtool_stats stats; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 763 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 765 | int ret, n_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 767 | if (!ops->get_ethtool_stats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return -EOPNOTSUPP; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 769 | if (!ops->get_sset_count && !ops->get_stats_count) |
| 770 | return -EOPNOTSUPP; |
| 771 | |
| 772 | if (ops->get_sset_count) |
| 773 | n_stats = ops->get_sset_count(dev, ETH_SS_STATS); |
| 774 | else |
| 775 | /* code path for obsolete hook */ |
| 776 | n_stats = ops->get_stats_count(dev); |
| 777 | if (n_stats < 0) |
| 778 | return n_stats; |
| 779 | WARN_ON(n_stats == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 782 | return -EFAULT; |
| 783 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 784 | stats.n_stats = n_stats; |
| 785 | data = kmalloc(n_stats * sizeof(u64), GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if (!data) |
| 787 | return -ENOMEM; |
| 788 | |
| 789 | ops->get_ethtool_stats(dev, &stats, data); |
| 790 | |
| 791 | ret = -EFAULT; |
| 792 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 793 | goto out; |
| 794 | useraddr += sizeof(stats); |
| 795 | if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64))) |
| 796 | goto out; |
| 797 | ret = 0; |
| 798 | |
| 799 | out: |
| 800 | kfree(data); |
| 801 | return ret; |
| 802 | } |
| 803 | |
viro@ftp.linux.org.uk | 0bf0519d | 2005-09-05 03:26:18 +0100 | [diff] [blame] | 804 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 805 | { |
| 806 | struct ethtool_perm_addr epaddr; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 807 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 808 | if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 809 | return -EFAULT; |
| 810 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 811 | if (epaddr.size < dev->addr_len) |
| 812 | return -ETOOSMALL; |
| 813 | epaddr.size = dev->addr_len; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 814 | |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 815 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 816 | return -EFAULT; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 817 | useraddr += sizeof(epaddr); |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 818 | if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) |
| 819 | return -EFAULT; |
| 820 | return 0; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 823 | static int ethtool_get_value(struct net_device *dev, char __user *useraddr, |
| 824 | u32 cmd, u32 (*actor)(struct net_device *)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 825 | { |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 826 | struct ethtool_value edata = { cmd }; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 827 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 828 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 829 | return -EOPNOTSUPP; |
| 830 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 831 | edata.data = actor(dev); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 832 | |
| 833 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 834 | return -EFAULT; |
| 835 | return 0; |
| 836 | } |
| 837 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 838 | static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, |
| 839 | void (*actor)(struct net_device *, u32)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 840 | { |
| 841 | struct ethtool_value edata; |
| 842 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 843 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 844 | return -EOPNOTSUPP; |
| 845 | |
| 846 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 847 | return -EFAULT; |
| 848 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 849 | actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 853 | static int ethtool_set_value(struct net_device *dev, char __user *useraddr, |
| 854 | int (*actor)(struct net_device *, u32)) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 855 | { |
| 856 | struct ethtool_value edata; |
| 857 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 858 | if (!actor) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 859 | return -EOPNOTSUPP; |
| 860 | |
| 861 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 862 | return -EFAULT; |
| 863 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 864 | return actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /* The main entry point in this file. Called from net/core/dev.c */ |
| 868 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 869 | int dev_ethtool(struct net *net, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 871 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | void __user *useraddr = ifr->ifr_data; |
| 873 | u32 ethcmd; |
| 874 | int rc; |
Stephen Hemminger | 81e8157 | 2005-05-29 14:14:35 -0700 | [diff] [blame] | 875 | unsigned long old_features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | if (!dev || !netif_device_present(dev)) |
| 878 | return -ENODEV; |
| 879 | |
| 880 | if (!dev->ethtool_ops) |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 881 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd))) |
| 884 | return -EFAULT; |
| 885 | |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 886 | /* Allow some commands to be done by anyone */ |
| 887 | switch(ethcmd) { |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 888 | case ETHTOOL_GDRVINFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 889 | case ETHTOOL_GMSGLVL: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 890 | case ETHTOOL_GCOALESCE: |
| 891 | case ETHTOOL_GRINGPARAM: |
| 892 | case ETHTOOL_GPAUSEPARAM: |
| 893 | case ETHTOOL_GRXCSUM: |
| 894 | case ETHTOOL_GTXCSUM: |
| 895 | case ETHTOOL_GSG: |
| 896 | case ETHTOOL_GSTRINGS: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 897 | case ETHTOOL_GTSO: |
| 898 | case ETHTOOL_GPERMADDR: |
| 899 | case ETHTOOL_GUFO: |
| 900 | case ETHTOOL_GGSO: |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 901 | case ETHTOOL_GFLAGS: |
| 902 | case ETHTOOL_GPFLAGS: |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 903 | case ETHTOOL_GRXFH: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 904 | break; |
| 905 | default: |
| 906 | if (!capable(CAP_NET_ADMIN)) |
| 907 | return -EPERM; |
| 908 | } |
| 909 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 910 | if (dev->ethtool_ops->begin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if ((rc = dev->ethtool_ops->begin(dev)) < 0) |
| 912 | return rc; |
| 913 | |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 914 | old_features = dev->features; |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | switch (ethcmd) { |
| 917 | case ETHTOOL_GSET: |
| 918 | rc = ethtool_get_settings(dev, useraddr); |
| 919 | break; |
| 920 | case ETHTOOL_SSET: |
| 921 | rc = ethtool_set_settings(dev, useraddr); |
| 922 | break; |
| 923 | case ETHTOOL_GDRVINFO: |
| 924 | rc = ethtool_get_drvinfo(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | break; |
| 926 | case ETHTOOL_GREGS: |
| 927 | rc = ethtool_get_regs(dev, useraddr); |
| 928 | break; |
| 929 | case ETHTOOL_GWOL: |
| 930 | rc = ethtool_get_wol(dev, useraddr); |
| 931 | break; |
| 932 | case ETHTOOL_SWOL: |
| 933 | rc = ethtool_set_wol(dev, useraddr); |
| 934 | break; |
| 935 | case ETHTOOL_GMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 936 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 937 | dev->ethtool_ops->get_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | break; |
| 939 | case ETHTOOL_SMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 940 | rc = ethtool_set_value_void(dev, useraddr, |
| 941 | dev->ethtool_ops->set_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | break; |
| 943 | case ETHTOOL_NWAY_RST: |
| 944 | rc = ethtool_nway_reset(dev); |
| 945 | break; |
| 946 | case ETHTOOL_GLINK: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 947 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 948 | dev->ethtool_ops->get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | break; |
| 950 | case ETHTOOL_GEEPROM: |
| 951 | rc = ethtool_get_eeprom(dev, useraddr); |
| 952 | break; |
| 953 | case ETHTOOL_SEEPROM: |
| 954 | rc = ethtool_set_eeprom(dev, useraddr); |
| 955 | break; |
| 956 | case ETHTOOL_GCOALESCE: |
| 957 | rc = ethtool_get_coalesce(dev, useraddr); |
| 958 | break; |
| 959 | case ETHTOOL_SCOALESCE: |
| 960 | rc = ethtool_set_coalesce(dev, useraddr); |
| 961 | break; |
| 962 | case ETHTOOL_GRINGPARAM: |
| 963 | rc = ethtool_get_ringparam(dev, useraddr); |
| 964 | break; |
| 965 | case ETHTOOL_SRINGPARAM: |
| 966 | rc = ethtool_set_ringparam(dev, useraddr); |
| 967 | break; |
| 968 | case ETHTOOL_GPAUSEPARAM: |
| 969 | rc = ethtool_get_pauseparam(dev, useraddr); |
| 970 | break; |
| 971 | case ETHTOOL_SPAUSEPARAM: |
| 972 | rc = ethtool_set_pauseparam(dev, useraddr); |
| 973 | break; |
| 974 | case ETHTOOL_GRXCSUM: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 975 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 976 | dev->ethtool_ops->get_rx_csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | break; |
| 978 | case ETHTOOL_SRXCSUM: |
Herbert Xu | b240a0e | 2008-12-15 23:44:31 -0800 | [diff] [blame^] | 979 | rc = ethtool_set_rx_csum(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | break; |
| 981 | case ETHTOOL_GTXCSUM: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 982 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Jeff Garzik | 88d3aaf | 2007-09-15 14:41:06 -0700 | [diff] [blame] | 983 | (dev->ethtool_ops->get_tx_csum ? |
| 984 | dev->ethtool_ops->get_tx_csum : |
| 985 | ethtool_op_get_tx_csum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | break; |
| 987 | case ETHTOOL_STXCSUM: |
| 988 | rc = ethtool_set_tx_csum(dev, useraddr); |
| 989 | break; |
| 990 | case ETHTOOL_GSG: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 991 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Jeff Garzik | 88d3aaf | 2007-09-15 14:41:06 -0700 | [diff] [blame] | 992 | (dev->ethtool_ops->get_sg ? |
| 993 | dev->ethtool_ops->get_sg : |
| 994 | ethtool_op_get_sg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | break; |
| 996 | case ETHTOOL_SSG: |
| 997 | rc = ethtool_set_sg(dev, useraddr); |
| 998 | break; |
| 999 | case ETHTOOL_GTSO: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1000 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Jeff Garzik | 88d3aaf | 2007-09-15 14:41:06 -0700 | [diff] [blame] | 1001 | (dev->ethtool_ops->get_tso ? |
| 1002 | dev->ethtool_ops->get_tso : |
| 1003 | ethtool_op_get_tso)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | case ETHTOOL_STSO: |
| 1006 | rc = ethtool_set_tso(dev, useraddr); |
| 1007 | break; |
| 1008 | case ETHTOOL_TEST: |
| 1009 | rc = ethtool_self_test(dev, useraddr); |
| 1010 | break; |
| 1011 | case ETHTOOL_GSTRINGS: |
| 1012 | rc = ethtool_get_strings(dev, useraddr); |
| 1013 | break; |
| 1014 | case ETHTOOL_PHYS_ID: |
| 1015 | rc = ethtool_phys_id(dev, useraddr); |
| 1016 | break; |
| 1017 | case ETHTOOL_GSTATS: |
| 1018 | rc = ethtool_get_stats(dev, useraddr); |
| 1019 | break; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1020 | case ETHTOOL_GPERMADDR: |
| 1021 | rc = ethtool_get_perm_addr(dev, useraddr); |
| 1022 | break; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1023 | case ETHTOOL_GUFO: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1024 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Jeff Garzik | 88d3aaf | 2007-09-15 14:41:06 -0700 | [diff] [blame] | 1025 | (dev->ethtool_ops->get_ufo ? |
| 1026 | dev->ethtool_ops->get_ufo : |
| 1027 | ethtool_op_get_ufo)); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1028 | break; |
| 1029 | case ETHTOOL_SUFO: |
| 1030 | rc = ethtool_set_ufo(dev, useraddr); |
| 1031 | break; |
Herbert Xu | 37c3185 | 2006-06-22 03:07:29 -0700 | [diff] [blame] | 1032 | case ETHTOOL_GGSO: |
| 1033 | rc = ethtool_get_gso(dev, useraddr); |
| 1034 | break; |
| 1035 | case ETHTOOL_SGSO: |
| 1036 | rc = ethtool_set_gso(dev, useraddr); |
| 1037 | break; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1038 | case ETHTOOL_GFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1039 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 1040 | dev->ethtool_ops->get_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1041 | break; |
| 1042 | case ETHTOOL_SFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1043 | rc = ethtool_set_value(dev, useraddr, |
| 1044 | dev->ethtool_ops->set_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1045 | break; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 1046 | case ETHTOOL_GPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1047 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 1048 | dev->ethtool_ops->get_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 1049 | break; |
| 1050 | case ETHTOOL_SPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 1051 | rc = ethtool_set_value(dev, useraddr, |
| 1052 | dev->ethtool_ops->set_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 1053 | break; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1054 | case ETHTOOL_GRXFH: |
| 1055 | rc = ethtool_get_rxhash(dev, useraddr); |
| 1056 | break; |
| 1057 | case ETHTOOL_SRXFH: |
| 1058 | rc = ethtool_set_rxhash(dev, useraddr); |
| 1059 | break; |
Herbert Xu | b240a0e | 2008-12-15 23:44:31 -0800 | [diff] [blame^] | 1060 | case ETHTOOL_GGRO: |
| 1061 | rc = ethtool_get_gro(dev, useraddr); |
| 1062 | break; |
| 1063 | case ETHTOOL_SGRO: |
| 1064 | rc = ethtool_set_gro(dev, useraddr); |
| 1065 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | default: |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 1067 | rc = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1069 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 1070 | if (dev->ethtool_ops->complete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | dev->ethtool_ops->complete(dev); |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 1072 | |
| 1073 | if (old_features != dev->features) |
| 1074 | netdev_features_change(dev); |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | EXPORT_SYMBOL(ethtool_op_get_link); |
| 1080 | EXPORT_SYMBOL(ethtool_op_get_sg); |
| 1081 | EXPORT_SYMBOL(ethtool_op_get_tso); |
| 1082 | EXPORT_SYMBOL(ethtool_op_get_tx_csum); |
| 1083 | EXPORT_SYMBOL(ethtool_op_set_sg); |
| 1084 | EXPORT_SYMBOL(ethtool_op_set_tso); |
| 1085 | EXPORT_SYMBOL(ethtool_op_set_tx_csum); |
Jon Mason | 69f6a0f | 2005-05-29 20:27:24 -0700 | [diff] [blame] | 1086 | EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum); |
Michael Chan | 6460d94 | 2007-07-14 19:07:52 -0700 | [diff] [blame] | 1087 | EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1088 | EXPORT_SYMBOL(ethtool_op_set_ufo); |
| 1089 | EXPORT_SYMBOL(ethtool_op_get_ufo); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 1090 | EXPORT_SYMBOL(ethtool_op_set_flags); |
| 1091 | EXPORT_SYMBOL(ethtool_op_get_flags); |