ethtool.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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
  6. * the information ethtool needs.
  7. *
  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.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/capability.h>
  16. #include <linux/errno.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/netdevice.h>
  19. #include <asm/uaccess.h>
  20. /*
  21. * Some useful ethtool_ops methods that're device independent.
  22. * If we find that all drivers want to do the same thing here,
  23. * we can turn these into dev_() function calls.
  24. */
  25. u32 ethtool_op_get_link(struct net_device *dev)
  26. {
  27. return netif_carrier_ok(dev) ? 1 : 0;
  28. }
  29. u32 ethtool_op_get_tx_csum(struct net_device *dev)
  30. {
  31. return (dev->features & NETIF_F_ALL_CSUM) != 0;
  32. }
  33. int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
  34. {
  35. if (data)
  36. dev->features |= NETIF_F_IP_CSUM;
  37. else
  38. dev->features &= ~NETIF_F_IP_CSUM;
  39. return 0;
  40. }
  41. int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
  42. {
  43. if (data)
  44. dev->features |= NETIF_F_HW_CSUM;
  45. else
  46. dev->features &= ~NETIF_F_HW_CSUM;
  47. return 0;
  48. }
  49. int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
  50. {
  51. if (data)
  52. dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  53. else
  54. dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
  55. return 0;
  56. }
  57. u32 ethtool_op_get_sg(struct net_device *dev)
  58. {
  59. return (dev->features & NETIF_F_SG) != 0;
  60. }
  61. int ethtool_op_set_sg(struct net_device *dev, u32 data)
  62. {
  63. if (data)
  64. dev->features |= NETIF_F_SG;
  65. else
  66. dev->features &= ~NETIF_F_SG;
  67. return 0;
  68. }
  69. u32 ethtool_op_get_tso(struct net_device *dev)
  70. {
  71. return (dev->features & NETIF_F_TSO) != 0;
  72. }
  73. int ethtool_op_set_tso(struct net_device *dev, u32 data)
  74. {
  75. if (data)
  76. dev->features |= NETIF_F_TSO;
  77. else
  78. dev->features &= ~NETIF_F_TSO;
  79. return 0;
  80. }
  81. u32 ethtool_op_get_ufo(struct net_device *dev)
  82. {
  83. return (dev->features & NETIF_F_UFO) != 0;
  84. }
  85. int ethtool_op_set_ufo(struct net_device *dev, u32 data)
  86. {
  87. if (data)
  88. dev->features |= NETIF_F_UFO;
  89. else
  90. dev->features &= ~NETIF_F_UFO;
  91. return 0;
  92. }
  93. /* the following list of flags are the same as their associated
  94. * NETIF_F_xxx values in include/linux/netdevice.h
  95. */
  96. static const u32 flags_dup_features =
  97. ETH_FLAG_LRO;
  98. u32 ethtool_op_get_flags(struct net_device *dev)
  99. {
  100. /* in the future, this function will probably contain additional
  101. * handling for flags which are not so easily handled
  102. * by a simple masking operation
  103. */
  104. return dev->features & flags_dup_features;
  105. }
  106. int ethtool_op_set_flags(struct net_device *dev, u32 data)
  107. {
  108. if (data & ETH_FLAG_LRO)
  109. dev->features |= NETIF_F_LRO;
  110. else
  111. dev->features &= ~NETIF_F_LRO;
  112. return 0;
  113. }
  114. /* Handlers for each ethtool command */
  115. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  116. {
  117. struct ethtool_cmd cmd = { ETHTOOL_GSET };
  118. int err;
  119. if (!dev->ethtool_ops->get_settings)
  120. return -EOPNOTSUPP;
  121. err = dev->ethtool_ops->get_settings(dev, &cmd);
  122. if (err < 0)
  123. return err;
  124. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  125. return -EFAULT;
  126. return 0;
  127. }
  128. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  129. {
  130. struct ethtool_cmd cmd;
  131. if (!dev->ethtool_ops->set_settings)
  132. return -EOPNOTSUPP;
  133. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  134. return -EFAULT;
  135. return dev->ethtool_ops->set_settings(dev, &cmd);
  136. }
  137. static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
  138. {
  139. struct ethtool_drvinfo info;
  140. const struct ethtool_ops *ops = dev->ethtool_ops;
  141. if (!ops->get_drvinfo)
  142. return -EOPNOTSUPP;
  143. memset(&info, 0, sizeof(info));
  144. info.cmd = ETHTOOL_GDRVINFO;
  145. ops->get_drvinfo(dev, &info);
  146. if (ops->get_sset_count) {
  147. int rc;
  148. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  149. if (rc >= 0)
  150. info.testinfo_len = rc;
  151. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  152. if (rc >= 0)
  153. info.n_stats = rc;
  154. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  155. if (rc >= 0)
  156. info.n_priv_flags = rc;
  157. } else {
  158. /* code path for obsolete hooks */
  159. if (ops->self_test_count)
  160. info.testinfo_len = ops->self_test_count(dev);
  161. if (ops->get_stats_count)
  162. info.n_stats = ops->get_stats_count(dev);
  163. }
  164. if (ops->get_regs_len)
  165. info.regdump_len = ops->get_regs_len(dev);
  166. if (ops->get_eeprom_len)
  167. info.eedump_len = ops->get_eeprom_len(dev);
  168. if (copy_to_user(useraddr, &info, sizeof(info)))
  169. return -EFAULT;
  170. return 0;
  171. }
  172. static int ethtool_set_rxhash(struct net_device *dev, void __user *useraddr)
  173. {
  174. struct ethtool_rxnfc cmd;
  175. if (!dev->ethtool_ops->set_rxhash)
  176. return -EOPNOTSUPP;
  177. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  178. return -EFAULT;
  179. return dev->ethtool_ops->set_rxhash(dev, &cmd);
  180. }
  181. static int ethtool_get_rxhash(struct net_device *dev, void __user *useraddr)
  182. {
  183. struct ethtool_rxnfc info;
  184. if (!dev->ethtool_ops->get_rxhash)
  185. return -EOPNOTSUPP;
  186. if (copy_from_user(&info, useraddr, sizeof(info)))
  187. return -EFAULT;
  188. dev->ethtool_ops->get_rxhash(dev, &info);
  189. if (copy_to_user(useraddr, &info, sizeof(info)))
  190. return -EFAULT;
  191. return 0;
  192. }
  193. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  194. {
  195. struct ethtool_regs regs;
  196. const struct ethtool_ops *ops = dev->ethtool_ops;
  197. void *regbuf;
  198. int reglen, ret;
  199. if (!ops->get_regs || !ops->get_regs_len)
  200. return -EOPNOTSUPP;
  201. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  202. return -EFAULT;
  203. reglen = ops->get_regs_len(dev);
  204. if (regs.len > reglen)
  205. regs.len = reglen;
  206. regbuf = kmalloc(reglen, GFP_USER);
  207. if (!regbuf)
  208. return -ENOMEM;
  209. ops->get_regs(dev, &regs, regbuf);
  210. ret = -EFAULT;
  211. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  212. goto out;
  213. useraddr += offsetof(struct ethtool_regs, data);
  214. if (copy_to_user(useraddr, regbuf, regs.len))
  215. goto out;
  216. ret = 0;
  217. out:
  218. kfree(regbuf);
  219. return ret;
  220. }
  221. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  222. {
  223. struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
  224. if (!dev->ethtool_ops->get_wol)
  225. return -EOPNOTSUPP;
  226. dev->ethtool_ops->get_wol(dev, &wol);
  227. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  228. return -EFAULT;
  229. return 0;
  230. }
  231. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  232. {
  233. struct ethtool_wolinfo wol;
  234. if (!dev->ethtool_ops->set_wol)
  235. return -EOPNOTSUPP;
  236. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  237. return -EFAULT;
  238. return dev->ethtool_ops->set_wol(dev, &wol);
  239. }
  240. static int ethtool_nway_reset(struct net_device *dev)
  241. {
  242. if (!dev->ethtool_ops->nway_reset)
  243. return -EOPNOTSUPP;
  244. return dev->ethtool_ops->nway_reset(dev);
  245. }
  246. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  247. {
  248. struct ethtool_eeprom eeprom;
  249. const struct ethtool_ops *ops = dev->ethtool_ops;
  250. void __user *userbuf = useraddr + sizeof(eeprom);
  251. u32 bytes_remaining;
  252. u8 *data;
  253. int ret = 0;
  254. if (!ops->get_eeprom || !ops->get_eeprom_len)
  255. return -EOPNOTSUPP;
  256. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  257. return -EFAULT;
  258. /* Check for wrap and zero */
  259. if (eeprom.offset + eeprom.len <= eeprom.offset)
  260. return -EINVAL;
  261. /* Check for exceeding total eeprom len */
  262. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  263. return -EINVAL;
  264. data = kmalloc(PAGE_SIZE, GFP_USER);
  265. if (!data)
  266. return -ENOMEM;
  267. bytes_remaining = eeprom.len;
  268. while (bytes_remaining > 0) {
  269. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  270. ret = ops->get_eeprom(dev, &eeprom, data);
  271. if (ret)
  272. break;
  273. if (copy_to_user(userbuf, data, eeprom.len)) {
  274. ret = -EFAULT;
  275. break;
  276. }
  277. userbuf += eeprom.len;
  278. eeprom.offset += eeprom.len;
  279. bytes_remaining -= eeprom.len;
  280. }
  281. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  282. eeprom.offset -= eeprom.len;
  283. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  284. ret = -EFAULT;
  285. kfree(data);
  286. return ret;
  287. }
  288. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  289. {
  290. struct ethtool_eeprom eeprom;
  291. const struct ethtool_ops *ops = dev->ethtool_ops;
  292. void __user *userbuf = useraddr + sizeof(eeprom);
  293. u32 bytes_remaining;
  294. u8 *data;
  295. int ret = 0;
  296. if (!ops->set_eeprom || !ops->get_eeprom_len)
  297. return -EOPNOTSUPP;
  298. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  299. return -EFAULT;
  300. /* Check for wrap and zero */
  301. if (eeprom.offset + eeprom.len <= eeprom.offset)
  302. return -EINVAL;
  303. /* Check for exceeding total eeprom len */
  304. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  305. return -EINVAL;
  306. data = kmalloc(PAGE_SIZE, GFP_USER);
  307. if (!data)
  308. return -ENOMEM;
  309. bytes_remaining = eeprom.len;
  310. while (bytes_remaining > 0) {
  311. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  312. if (copy_from_user(data, userbuf, eeprom.len)) {
  313. ret = -EFAULT;
  314. break;
  315. }
  316. ret = ops->set_eeprom(dev, &eeprom, data);
  317. if (ret)
  318. break;
  319. userbuf += eeprom.len;
  320. eeprom.offset += eeprom.len;
  321. bytes_remaining -= eeprom.len;
  322. }
  323. kfree(data);
  324. return ret;
  325. }
  326. static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
  327. {
  328. struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
  329. if (!dev->ethtool_ops->get_coalesce)
  330. return -EOPNOTSUPP;
  331. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  332. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  333. return -EFAULT;
  334. return 0;
  335. }
  336. static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
  337. {
  338. struct ethtool_coalesce coalesce;
  339. if (!dev->ethtool_ops->set_coalesce)
  340. return -EOPNOTSUPP;
  341. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  342. return -EFAULT;
  343. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  344. }
  345. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  346. {
  347. struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
  348. if (!dev->ethtool_ops->get_ringparam)
  349. return -EOPNOTSUPP;
  350. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  351. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  352. return -EFAULT;
  353. return 0;
  354. }
  355. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  356. {
  357. struct ethtool_ringparam ringparam;
  358. if (!dev->ethtool_ops->set_ringparam)
  359. return -EOPNOTSUPP;
  360. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  361. return -EFAULT;
  362. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  363. }
  364. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  365. {
  366. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  367. if (!dev->ethtool_ops->get_pauseparam)
  368. return -EOPNOTSUPP;
  369. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  370. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  371. return -EFAULT;
  372. return 0;
  373. }
  374. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  375. {
  376. struct ethtool_pauseparam pauseparam;
  377. if (!dev->ethtool_ops->set_pauseparam)
  378. return -EOPNOTSUPP;
  379. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  380. return -EFAULT;
  381. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  382. }
  383. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  384. {
  385. int err;
  386. if (!data && dev->ethtool_ops->set_tso) {
  387. err = dev->ethtool_ops->set_tso(dev, 0);
  388. if (err)
  389. return err;
  390. }
  391. if (!data && dev->ethtool_ops->set_ufo) {
  392. err = dev->ethtool_ops->set_ufo(dev, 0);
  393. if (err)
  394. return err;
  395. }
  396. return dev->ethtool_ops->set_sg(dev, data);
  397. }
  398. static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
  399. {
  400. struct ethtool_value edata;
  401. int err;
  402. if (!dev->ethtool_ops->set_tx_csum)
  403. return -EOPNOTSUPP;
  404. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  405. return -EFAULT;
  406. if (!edata.data && dev->ethtool_ops->set_sg) {
  407. err = __ethtool_set_sg(dev, 0);
  408. if (err)
  409. return err;
  410. }
  411. return dev->ethtool_ops->set_tx_csum(dev, edata.data);
  412. }
  413. static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
  414. {
  415. struct ethtool_value edata;
  416. if (!dev->ethtool_ops->set_sg)
  417. return -EOPNOTSUPP;
  418. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  419. return -EFAULT;
  420. if (edata.data &&
  421. !(dev->features & NETIF_F_ALL_CSUM))
  422. return -EINVAL;
  423. return __ethtool_set_sg(dev, edata.data);
  424. }
  425. static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
  426. {
  427. struct ethtool_value edata;
  428. if (!dev->ethtool_ops->set_tso)
  429. return -EOPNOTSUPP;
  430. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  431. return -EFAULT;
  432. if (edata.data && !(dev->features & NETIF_F_SG))
  433. return -EINVAL;
  434. return dev->ethtool_ops->set_tso(dev, edata.data);
  435. }
  436. static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
  437. {
  438. struct ethtool_value edata;
  439. if (!dev->ethtool_ops->set_ufo)
  440. return -EOPNOTSUPP;
  441. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  442. return -EFAULT;
  443. if (edata.data && !(dev->features & NETIF_F_SG))
  444. return -EINVAL;
  445. if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
  446. return -EINVAL;
  447. return dev->ethtool_ops->set_ufo(dev, edata.data);
  448. }
  449. static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
  450. {
  451. struct ethtool_value edata = { ETHTOOL_GGSO };
  452. edata.data = dev->features & NETIF_F_GSO;
  453. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  454. return -EFAULT;
  455. return 0;
  456. }
  457. static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
  458. {
  459. struct ethtool_value edata;
  460. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  461. return -EFAULT;
  462. if (edata.data)
  463. dev->features |= NETIF_F_GSO;
  464. else
  465. dev->features &= ~NETIF_F_GSO;
  466. return 0;
  467. }
  468. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  469. {
  470. struct ethtool_test test;
  471. const struct ethtool_ops *ops = dev->ethtool_ops;
  472. u64 *data;
  473. int ret, test_len;
  474. if (!ops->self_test)
  475. return -EOPNOTSUPP;
  476. if (!ops->get_sset_count && !ops->self_test_count)
  477. return -EOPNOTSUPP;
  478. if (ops->get_sset_count)
  479. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  480. else
  481. /* code path for obsolete hook */
  482. test_len = ops->self_test_count(dev);
  483. if (test_len < 0)
  484. return test_len;
  485. WARN_ON(test_len == 0);
  486. if (copy_from_user(&test, useraddr, sizeof(test)))
  487. return -EFAULT;
  488. test.len = test_len;
  489. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  490. if (!data)
  491. return -ENOMEM;
  492. ops->self_test(dev, &test, data);
  493. ret = -EFAULT;
  494. if (copy_to_user(useraddr, &test, sizeof(test)))
  495. goto out;
  496. useraddr += sizeof(test);
  497. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  498. goto out;
  499. ret = 0;
  500. out:
  501. kfree(data);
  502. return ret;
  503. }
  504. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  505. {
  506. struct ethtool_gstrings gstrings;
  507. const struct ethtool_ops *ops = dev->ethtool_ops;
  508. u8 *data;
  509. int ret;
  510. if (!ops->get_strings)
  511. return -EOPNOTSUPP;
  512. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  513. return -EFAULT;
  514. if (ops->get_sset_count) {
  515. ret = ops->get_sset_count(dev, gstrings.string_set);
  516. if (ret < 0)
  517. return ret;
  518. gstrings.len = ret;
  519. } else {
  520. /* code path for obsolete hooks */
  521. switch (gstrings.string_set) {
  522. case ETH_SS_TEST:
  523. if (!ops->self_test_count)
  524. return -EOPNOTSUPP;
  525. gstrings.len = ops->self_test_count(dev);
  526. break;
  527. case ETH_SS_STATS:
  528. if (!ops->get_stats_count)
  529. return -EOPNOTSUPP;
  530. gstrings.len = ops->get_stats_count(dev);
  531. break;
  532. default:
  533. return -EINVAL;
  534. }
  535. }
  536. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  537. if (!data)
  538. return -ENOMEM;
  539. ops->get_strings(dev, gstrings.string_set, data);
  540. ret = -EFAULT;
  541. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  542. goto out;
  543. useraddr += sizeof(gstrings);
  544. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  545. goto out;
  546. ret = 0;
  547. out:
  548. kfree(data);
  549. return ret;
  550. }
  551. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  552. {
  553. struct ethtool_value id;
  554. if (!dev->ethtool_ops->phys_id)
  555. return -EOPNOTSUPP;
  556. if (copy_from_user(&id, useraddr, sizeof(id)))
  557. return -EFAULT;
  558. return dev->ethtool_ops->phys_id(dev, id.data);
  559. }
  560. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  561. {
  562. struct ethtool_stats stats;
  563. const struct ethtool_ops *ops = dev->ethtool_ops;
  564. u64 *data;
  565. int ret, n_stats;
  566. if (!ops->get_ethtool_stats)
  567. return -EOPNOTSUPP;
  568. if (!ops->get_sset_count && !ops->get_stats_count)
  569. return -EOPNOTSUPP;
  570. if (ops->get_sset_count)
  571. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  572. else
  573. /* code path for obsolete hook */
  574. n_stats = ops->get_stats_count(dev);
  575. if (n_stats < 0)
  576. return n_stats;
  577. WARN_ON(n_stats == 0);
  578. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  579. return -EFAULT;
  580. stats.n_stats = n_stats;
  581. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  582. if (!data)
  583. return -ENOMEM;
  584. ops->get_ethtool_stats(dev, &stats, data);
  585. ret = -EFAULT;
  586. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  587. goto out;
  588. useraddr += sizeof(stats);
  589. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  590. goto out;
  591. ret = 0;
  592. out:
  593. kfree(data);
  594. return ret;
  595. }
  596. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  597. {
  598. struct ethtool_perm_addr epaddr;
  599. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  600. return -EFAULT;
  601. if (epaddr.size < dev->addr_len)
  602. return -ETOOSMALL;
  603. epaddr.size = dev->addr_len;
  604. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  605. return -EFAULT;
  606. useraddr += sizeof(epaddr);
  607. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  608. return -EFAULT;
  609. return 0;
  610. }
  611. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  612. u32 cmd, u32 (*actor)(struct net_device *))
  613. {
  614. struct ethtool_value edata = { cmd };
  615. if (!actor)
  616. return -EOPNOTSUPP;
  617. edata.data = actor(dev);
  618. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  619. return -EFAULT;
  620. return 0;
  621. }
  622. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  623. void (*actor)(struct net_device *, u32))
  624. {
  625. struct ethtool_value edata;
  626. if (!actor)
  627. return -EOPNOTSUPP;
  628. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  629. return -EFAULT;
  630. actor(dev, edata.data);
  631. return 0;
  632. }
  633. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  634. int (*actor)(struct net_device *, u32))
  635. {
  636. struct ethtool_value edata;
  637. if (!actor)
  638. return -EOPNOTSUPP;
  639. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  640. return -EFAULT;
  641. return actor(dev, edata.data);
  642. }
  643. /* The main entry point in this file. Called from net/core/dev.c */
  644. int dev_ethtool(struct net *net, struct ifreq *ifr)
  645. {
  646. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  647. void __user *useraddr = ifr->ifr_data;
  648. u32 ethcmd;
  649. int rc;
  650. unsigned long old_features;
  651. if (!dev || !netif_device_present(dev))
  652. return -ENODEV;
  653. if (!dev->ethtool_ops)
  654. return -EOPNOTSUPP;
  655. if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
  656. return -EFAULT;
  657. /* Allow some commands to be done by anyone */
  658. switch(ethcmd) {
  659. case ETHTOOL_GDRVINFO:
  660. case ETHTOOL_GMSGLVL:
  661. case ETHTOOL_GCOALESCE:
  662. case ETHTOOL_GRINGPARAM:
  663. case ETHTOOL_GPAUSEPARAM:
  664. case ETHTOOL_GRXCSUM:
  665. case ETHTOOL_GTXCSUM:
  666. case ETHTOOL_GSG:
  667. case ETHTOOL_GSTRINGS:
  668. case ETHTOOL_GTSO:
  669. case ETHTOOL_GPERMADDR:
  670. case ETHTOOL_GUFO:
  671. case ETHTOOL_GGSO:
  672. case ETHTOOL_GFLAGS:
  673. case ETHTOOL_GPFLAGS:
  674. case ETHTOOL_GRXFH:
  675. break;
  676. default:
  677. if (!capable(CAP_NET_ADMIN))
  678. return -EPERM;
  679. }
  680. if (dev->ethtool_ops->begin)
  681. if ((rc = dev->ethtool_ops->begin(dev)) < 0)
  682. return rc;
  683. old_features = dev->features;
  684. switch (ethcmd) {
  685. case ETHTOOL_GSET:
  686. rc = ethtool_get_settings(dev, useraddr);
  687. break;
  688. case ETHTOOL_SSET:
  689. rc = ethtool_set_settings(dev, useraddr);
  690. break;
  691. case ETHTOOL_GDRVINFO:
  692. rc = ethtool_get_drvinfo(dev, useraddr);
  693. break;
  694. case ETHTOOL_GREGS:
  695. rc = ethtool_get_regs(dev, useraddr);
  696. break;
  697. case ETHTOOL_GWOL:
  698. rc = ethtool_get_wol(dev, useraddr);
  699. break;
  700. case ETHTOOL_SWOL:
  701. rc = ethtool_set_wol(dev, useraddr);
  702. break;
  703. case ETHTOOL_GMSGLVL:
  704. rc = ethtool_get_value(dev, useraddr, ethcmd,
  705. dev->ethtool_ops->get_msglevel);
  706. break;
  707. case ETHTOOL_SMSGLVL:
  708. rc = ethtool_set_value_void(dev, useraddr,
  709. dev->ethtool_ops->set_msglevel);
  710. break;
  711. case ETHTOOL_NWAY_RST:
  712. rc = ethtool_nway_reset(dev);
  713. break;
  714. case ETHTOOL_GLINK:
  715. rc = ethtool_get_value(dev, useraddr, ethcmd,
  716. dev->ethtool_ops->get_link);
  717. break;
  718. case ETHTOOL_GEEPROM:
  719. rc = ethtool_get_eeprom(dev, useraddr);
  720. break;
  721. case ETHTOOL_SEEPROM:
  722. rc = ethtool_set_eeprom(dev, useraddr);
  723. break;
  724. case ETHTOOL_GCOALESCE:
  725. rc = ethtool_get_coalesce(dev, useraddr);
  726. break;
  727. case ETHTOOL_SCOALESCE:
  728. rc = ethtool_set_coalesce(dev, useraddr);
  729. break;
  730. case ETHTOOL_GRINGPARAM:
  731. rc = ethtool_get_ringparam(dev, useraddr);
  732. break;
  733. case ETHTOOL_SRINGPARAM:
  734. rc = ethtool_set_ringparam(dev, useraddr);
  735. break;
  736. case ETHTOOL_GPAUSEPARAM:
  737. rc = ethtool_get_pauseparam(dev, useraddr);
  738. break;
  739. case ETHTOOL_SPAUSEPARAM:
  740. rc = ethtool_set_pauseparam(dev, useraddr);
  741. break;
  742. case ETHTOOL_GRXCSUM:
  743. rc = ethtool_get_value(dev, useraddr, ethcmd,
  744. dev->ethtool_ops->get_rx_csum);
  745. break;
  746. case ETHTOOL_SRXCSUM:
  747. rc = ethtool_set_value(dev, useraddr,
  748. dev->ethtool_ops->set_rx_csum);
  749. break;
  750. case ETHTOOL_GTXCSUM:
  751. rc = ethtool_get_value(dev, useraddr, ethcmd,
  752. (dev->ethtool_ops->get_tx_csum ?
  753. dev->ethtool_ops->get_tx_csum :
  754. ethtool_op_get_tx_csum));
  755. break;
  756. case ETHTOOL_STXCSUM:
  757. rc = ethtool_set_tx_csum(dev, useraddr);
  758. break;
  759. case ETHTOOL_GSG:
  760. rc = ethtool_get_value(dev, useraddr, ethcmd,
  761. (dev->ethtool_ops->get_sg ?
  762. dev->ethtool_ops->get_sg :
  763. ethtool_op_get_sg));
  764. break;
  765. case ETHTOOL_SSG:
  766. rc = ethtool_set_sg(dev, useraddr);
  767. break;
  768. case ETHTOOL_GTSO:
  769. rc = ethtool_get_value(dev, useraddr, ethcmd,
  770. (dev->ethtool_ops->get_tso ?
  771. dev->ethtool_ops->get_tso :
  772. ethtool_op_get_tso));
  773. break;
  774. case ETHTOOL_STSO:
  775. rc = ethtool_set_tso(dev, useraddr);
  776. break;
  777. case ETHTOOL_TEST:
  778. rc = ethtool_self_test(dev, useraddr);
  779. break;
  780. case ETHTOOL_GSTRINGS:
  781. rc = ethtool_get_strings(dev, useraddr);
  782. break;
  783. case ETHTOOL_PHYS_ID:
  784. rc = ethtool_phys_id(dev, useraddr);
  785. break;
  786. case ETHTOOL_GSTATS:
  787. rc = ethtool_get_stats(dev, useraddr);
  788. break;
  789. case ETHTOOL_GPERMADDR:
  790. rc = ethtool_get_perm_addr(dev, useraddr);
  791. break;
  792. case ETHTOOL_GUFO:
  793. rc = ethtool_get_value(dev, useraddr, ethcmd,
  794. (dev->ethtool_ops->get_ufo ?
  795. dev->ethtool_ops->get_ufo :
  796. ethtool_op_get_ufo));
  797. break;
  798. case ETHTOOL_SUFO:
  799. rc = ethtool_set_ufo(dev, useraddr);
  800. break;
  801. case ETHTOOL_GGSO:
  802. rc = ethtool_get_gso(dev, useraddr);
  803. break;
  804. case ETHTOOL_SGSO:
  805. rc = ethtool_set_gso(dev, useraddr);
  806. break;
  807. case ETHTOOL_GFLAGS:
  808. rc = ethtool_get_value(dev, useraddr, ethcmd,
  809. dev->ethtool_ops->get_flags);
  810. break;
  811. case ETHTOOL_SFLAGS:
  812. rc = ethtool_set_value(dev, useraddr,
  813. dev->ethtool_ops->set_flags);
  814. break;
  815. case ETHTOOL_GPFLAGS:
  816. rc = ethtool_get_value(dev, useraddr, ethcmd,
  817. dev->ethtool_ops->get_priv_flags);
  818. break;
  819. case ETHTOOL_SPFLAGS:
  820. rc = ethtool_set_value(dev, useraddr,
  821. dev->ethtool_ops->set_priv_flags);
  822. break;
  823. case ETHTOOL_GRXFH:
  824. rc = ethtool_get_rxhash(dev, useraddr);
  825. break;
  826. case ETHTOOL_SRXFH:
  827. rc = ethtool_set_rxhash(dev, useraddr);
  828. break;
  829. default:
  830. rc = -EOPNOTSUPP;
  831. }
  832. if (dev->ethtool_ops->complete)
  833. dev->ethtool_ops->complete(dev);
  834. if (old_features != dev->features)
  835. netdev_features_change(dev);
  836. return rc;
  837. }
  838. EXPORT_SYMBOL(ethtool_op_get_link);
  839. EXPORT_SYMBOL(ethtool_op_get_sg);
  840. EXPORT_SYMBOL(ethtool_op_get_tso);
  841. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  842. EXPORT_SYMBOL(ethtool_op_set_sg);
  843. EXPORT_SYMBOL(ethtool_op_set_tso);
  844. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  845. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
  846. EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
  847. EXPORT_SYMBOL(ethtool_op_set_ufo);
  848. EXPORT_SYMBOL(ethtool_op_get_ufo);
  849. EXPORT_SYMBOL(ethtool_op_set_flags);
  850. EXPORT_SYMBOL(ethtool_op_get_flags);