ethtool.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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_get_regs(struct net_device *dev, char __user *useraddr)
  173. {
  174. struct ethtool_regs regs;
  175. const struct ethtool_ops *ops = dev->ethtool_ops;
  176. void *regbuf;
  177. int reglen, ret;
  178. if (!ops->get_regs || !ops->get_regs_len)
  179. return -EOPNOTSUPP;
  180. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  181. return -EFAULT;
  182. reglen = ops->get_regs_len(dev);
  183. if (regs.len > reglen)
  184. regs.len = reglen;
  185. regbuf = kmalloc(reglen, GFP_USER);
  186. if (!regbuf)
  187. return -ENOMEM;
  188. ops->get_regs(dev, &regs, regbuf);
  189. ret = -EFAULT;
  190. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  191. goto out;
  192. useraddr += offsetof(struct ethtool_regs, data);
  193. if (copy_to_user(useraddr, regbuf, regs.len))
  194. goto out;
  195. ret = 0;
  196. out:
  197. kfree(regbuf);
  198. return ret;
  199. }
  200. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  201. {
  202. struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
  203. if (!dev->ethtool_ops->get_wol)
  204. return -EOPNOTSUPP;
  205. dev->ethtool_ops->get_wol(dev, &wol);
  206. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  207. return -EFAULT;
  208. return 0;
  209. }
  210. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  211. {
  212. struct ethtool_wolinfo wol;
  213. if (!dev->ethtool_ops->set_wol)
  214. return -EOPNOTSUPP;
  215. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  216. return -EFAULT;
  217. return dev->ethtool_ops->set_wol(dev, &wol);
  218. }
  219. static int ethtool_nway_reset(struct net_device *dev)
  220. {
  221. if (!dev->ethtool_ops->nway_reset)
  222. return -EOPNOTSUPP;
  223. return dev->ethtool_ops->nway_reset(dev);
  224. }
  225. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  226. {
  227. struct ethtool_eeprom eeprom;
  228. const struct ethtool_ops *ops = dev->ethtool_ops;
  229. void __user *userbuf = useraddr + sizeof(eeprom);
  230. u32 bytes_remaining;
  231. u8 *data;
  232. int ret = 0;
  233. if (!ops->get_eeprom || !ops->get_eeprom_len)
  234. return -EOPNOTSUPP;
  235. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  236. return -EFAULT;
  237. /* Check for wrap and zero */
  238. if (eeprom.offset + eeprom.len <= eeprom.offset)
  239. return -EINVAL;
  240. /* Check for exceeding total eeprom len */
  241. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  242. return -EINVAL;
  243. data = kmalloc(PAGE_SIZE, GFP_USER);
  244. if (!data)
  245. return -ENOMEM;
  246. bytes_remaining = eeprom.len;
  247. while (bytes_remaining > 0) {
  248. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  249. ret = ops->get_eeprom(dev, &eeprom, data);
  250. if (ret)
  251. break;
  252. if (copy_to_user(userbuf, data, eeprom.len)) {
  253. ret = -EFAULT;
  254. break;
  255. }
  256. userbuf += eeprom.len;
  257. eeprom.offset += eeprom.len;
  258. bytes_remaining -= eeprom.len;
  259. }
  260. kfree(data);
  261. return ret;
  262. }
  263. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  264. {
  265. struct ethtool_eeprom eeprom;
  266. const struct ethtool_ops *ops = dev->ethtool_ops;
  267. void __user *userbuf = useraddr + sizeof(eeprom);
  268. u32 bytes_remaining;
  269. u8 *data;
  270. int ret = 0;
  271. if (!ops->set_eeprom || !ops->get_eeprom_len)
  272. return -EOPNOTSUPP;
  273. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  274. return -EFAULT;
  275. /* Check for wrap and zero */
  276. if (eeprom.offset + eeprom.len <= eeprom.offset)
  277. return -EINVAL;
  278. /* Check for exceeding total eeprom len */
  279. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  280. return -EINVAL;
  281. data = kmalloc(PAGE_SIZE, GFP_USER);
  282. if (!data)
  283. return -ENOMEM;
  284. bytes_remaining = eeprom.len;
  285. while (bytes_remaining > 0) {
  286. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  287. if (copy_from_user(data, userbuf, eeprom.len)) {
  288. ret = -EFAULT;
  289. break;
  290. }
  291. ret = ops->set_eeprom(dev, &eeprom, data);
  292. if (ret)
  293. break;
  294. userbuf += eeprom.len;
  295. eeprom.offset += eeprom.len;
  296. bytes_remaining -= eeprom.len;
  297. }
  298. kfree(data);
  299. return ret;
  300. }
  301. static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
  302. {
  303. struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
  304. if (!dev->ethtool_ops->get_coalesce)
  305. return -EOPNOTSUPP;
  306. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  307. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  308. return -EFAULT;
  309. return 0;
  310. }
  311. static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
  312. {
  313. struct ethtool_coalesce coalesce;
  314. if (!dev->ethtool_ops->set_coalesce)
  315. return -EOPNOTSUPP;
  316. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  317. return -EFAULT;
  318. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  319. }
  320. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  321. {
  322. struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
  323. if (!dev->ethtool_ops->get_ringparam)
  324. return -EOPNOTSUPP;
  325. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  326. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  327. return -EFAULT;
  328. return 0;
  329. }
  330. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  331. {
  332. struct ethtool_ringparam ringparam;
  333. if (!dev->ethtool_ops->set_ringparam)
  334. return -EOPNOTSUPP;
  335. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  336. return -EFAULT;
  337. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  338. }
  339. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  340. {
  341. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  342. if (!dev->ethtool_ops->get_pauseparam)
  343. return -EOPNOTSUPP;
  344. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  345. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  346. return -EFAULT;
  347. return 0;
  348. }
  349. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  350. {
  351. struct ethtool_pauseparam pauseparam;
  352. if (!dev->ethtool_ops->set_pauseparam)
  353. return -EOPNOTSUPP;
  354. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  355. return -EFAULT;
  356. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  357. }
  358. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  359. {
  360. int err;
  361. if (!data && dev->ethtool_ops->set_tso) {
  362. err = dev->ethtool_ops->set_tso(dev, 0);
  363. if (err)
  364. return err;
  365. }
  366. if (!data && dev->ethtool_ops->set_ufo) {
  367. err = dev->ethtool_ops->set_ufo(dev, 0);
  368. if (err)
  369. return err;
  370. }
  371. return dev->ethtool_ops->set_sg(dev, data);
  372. }
  373. static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
  374. {
  375. struct ethtool_value edata;
  376. int err;
  377. if (!dev->ethtool_ops->set_tx_csum)
  378. return -EOPNOTSUPP;
  379. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  380. return -EFAULT;
  381. if (!edata.data && dev->ethtool_ops->set_sg) {
  382. err = __ethtool_set_sg(dev, 0);
  383. if (err)
  384. return err;
  385. }
  386. return dev->ethtool_ops->set_tx_csum(dev, edata.data);
  387. }
  388. static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
  389. {
  390. struct ethtool_value edata;
  391. if (!dev->ethtool_ops->set_sg)
  392. return -EOPNOTSUPP;
  393. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  394. return -EFAULT;
  395. if (edata.data &&
  396. !(dev->features & NETIF_F_ALL_CSUM))
  397. return -EINVAL;
  398. return __ethtool_set_sg(dev, edata.data);
  399. }
  400. static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
  401. {
  402. struct ethtool_value edata;
  403. if (!dev->ethtool_ops->set_tso)
  404. return -EOPNOTSUPP;
  405. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  406. return -EFAULT;
  407. if (edata.data && !(dev->features & NETIF_F_SG))
  408. return -EINVAL;
  409. return dev->ethtool_ops->set_tso(dev, edata.data);
  410. }
  411. static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
  412. {
  413. struct ethtool_value edata;
  414. if (!dev->ethtool_ops->set_ufo)
  415. return -EOPNOTSUPP;
  416. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  417. return -EFAULT;
  418. if (edata.data && !(dev->features & NETIF_F_SG))
  419. return -EINVAL;
  420. if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
  421. return -EINVAL;
  422. return dev->ethtool_ops->set_ufo(dev, edata.data);
  423. }
  424. static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
  425. {
  426. struct ethtool_value edata = { ETHTOOL_GGSO };
  427. edata.data = dev->features & NETIF_F_GSO;
  428. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  429. return -EFAULT;
  430. return 0;
  431. }
  432. static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
  433. {
  434. struct ethtool_value edata;
  435. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  436. return -EFAULT;
  437. if (edata.data)
  438. dev->features |= NETIF_F_GSO;
  439. else
  440. dev->features &= ~NETIF_F_GSO;
  441. return 0;
  442. }
  443. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  444. {
  445. struct ethtool_test test;
  446. const struct ethtool_ops *ops = dev->ethtool_ops;
  447. u64 *data;
  448. int ret, test_len;
  449. if (!ops->self_test)
  450. return -EOPNOTSUPP;
  451. if (!ops->get_sset_count && !ops->self_test_count)
  452. return -EOPNOTSUPP;
  453. if (ops->get_sset_count)
  454. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  455. else
  456. /* code path for obsolete hook */
  457. test_len = ops->self_test_count(dev);
  458. if (test_len < 0)
  459. return test_len;
  460. WARN_ON(test_len == 0);
  461. if (copy_from_user(&test, useraddr, sizeof(test)))
  462. return -EFAULT;
  463. test.len = test_len;
  464. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  465. if (!data)
  466. return -ENOMEM;
  467. ops->self_test(dev, &test, data);
  468. ret = -EFAULT;
  469. if (copy_to_user(useraddr, &test, sizeof(test)))
  470. goto out;
  471. useraddr += sizeof(test);
  472. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  473. goto out;
  474. ret = 0;
  475. out:
  476. kfree(data);
  477. return ret;
  478. }
  479. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  480. {
  481. struct ethtool_gstrings gstrings;
  482. const struct ethtool_ops *ops = dev->ethtool_ops;
  483. u8 *data;
  484. int ret;
  485. if (!ops->get_strings)
  486. return -EOPNOTSUPP;
  487. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  488. return -EFAULT;
  489. if (ops->get_sset_count) {
  490. ret = ops->get_sset_count(dev, gstrings.string_set);
  491. if (ret < 0)
  492. return ret;
  493. gstrings.len = ret;
  494. } else {
  495. /* code path for obsolete hooks */
  496. switch (gstrings.string_set) {
  497. case ETH_SS_TEST:
  498. if (!ops->self_test_count)
  499. return -EOPNOTSUPP;
  500. gstrings.len = ops->self_test_count(dev);
  501. break;
  502. case ETH_SS_STATS:
  503. if (!ops->get_stats_count)
  504. return -EOPNOTSUPP;
  505. gstrings.len = ops->get_stats_count(dev);
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. }
  511. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  512. if (!data)
  513. return -ENOMEM;
  514. ops->get_strings(dev, gstrings.string_set, data);
  515. ret = -EFAULT;
  516. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  517. goto out;
  518. useraddr += sizeof(gstrings);
  519. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  520. goto out;
  521. ret = 0;
  522. out:
  523. kfree(data);
  524. return ret;
  525. }
  526. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  527. {
  528. struct ethtool_value id;
  529. if (!dev->ethtool_ops->phys_id)
  530. return -EOPNOTSUPP;
  531. if (copy_from_user(&id, useraddr, sizeof(id)))
  532. return -EFAULT;
  533. return dev->ethtool_ops->phys_id(dev, id.data);
  534. }
  535. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  536. {
  537. struct ethtool_stats stats;
  538. const struct ethtool_ops *ops = dev->ethtool_ops;
  539. u64 *data;
  540. int ret, n_stats;
  541. if (!ops->get_ethtool_stats)
  542. return -EOPNOTSUPP;
  543. if (!ops->get_sset_count && !ops->get_stats_count)
  544. return -EOPNOTSUPP;
  545. if (ops->get_sset_count)
  546. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  547. else
  548. /* code path for obsolete hook */
  549. n_stats = ops->get_stats_count(dev);
  550. if (n_stats < 0)
  551. return n_stats;
  552. WARN_ON(n_stats == 0);
  553. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  554. return -EFAULT;
  555. stats.n_stats = n_stats;
  556. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  557. if (!data)
  558. return -ENOMEM;
  559. ops->get_ethtool_stats(dev, &stats, data);
  560. ret = -EFAULT;
  561. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  562. goto out;
  563. useraddr += sizeof(stats);
  564. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  565. goto out;
  566. ret = 0;
  567. out:
  568. kfree(data);
  569. return ret;
  570. }
  571. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  572. {
  573. struct ethtool_perm_addr epaddr;
  574. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  575. return -EFAULT;
  576. if (epaddr.size < dev->addr_len)
  577. return -ETOOSMALL;
  578. epaddr.size = dev->addr_len;
  579. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  580. return -EFAULT;
  581. useraddr += sizeof(epaddr);
  582. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  583. return -EFAULT;
  584. return 0;
  585. }
  586. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  587. u32 cmd, u32 (*actor)(struct net_device *))
  588. {
  589. struct ethtool_value edata = { cmd };
  590. if (!actor)
  591. return -EOPNOTSUPP;
  592. edata.data = actor(dev);
  593. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  594. return -EFAULT;
  595. return 0;
  596. }
  597. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  598. void (*actor)(struct net_device *, u32))
  599. {
  600. struct ethtool_value edata;
  601. if (!actor)
  602. return -EOPNOTSUPP;
  603. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  604. return -EFAULT;
  605. actor(dev, edata.data);
  606. return 0;
  607. }
  608. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  609. int (*actor)(struct net_device *, u32))
  610. {
  611. struct ethtool_value edata;
  612. if (!actor)
  613. return -EOPNOTSUPP;
  614. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  615. return -EFAULT;
  616. return actor(dev, edata.data);
  617. }
  618. /* The main entry point in this file. Called from net/core/dev.c */
  619. int dev_ethtool(struct net *net, struct ifreq *ifr)
  620. {
  621. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  622. void __user *useraddr = ifr->ifr_data;
  623. u32 ethcmd;
  624. int rc;
  625. unsigned long old_features;
  626. if (!dev || !netif_device_present(dev))
  627. return -ENODEV;
  628. if (!dev->ethtool_ops)
  629. return -EOPNOTSUPP;
  630. if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
  631. return -EFAULT;
  632. /* Allow some commands to be done by anyone */
  633. switch(ethcmd) {
  634. case ETHTOOL_GDRVINFO:
  635. case ETHTOOL_GMSGLVL:
  636. case ETHTOOL_GCOALESCE:
  637. case ETHTOOL_GRINGPARAM:
  638. case ETHTOOL_GPAUSEPARAM:
  639. case ETHTOOL_GRXCSUM:
  640. case ETHTOOL_GTXCSUM:
  641. case ETHTOOL_GSG:
  642. case ETHTOOL_GSTRINGS:
  643. case ETHTOOL_GTSO:
  644. case ETHTOOL_GPERMADDR:
  645. case ETHTOOL_GUFO:
  646. case ETHTOOL_GGSO:
  647. case ETHTOOL_GFLAGS:
  648. case ETHTOOL_GPFLAGS:
  649. break;
  650. default:
  651. if (!capable(CAP_NET_ADMIN))
  652. return -EPERM;
  653. }
  654. if (dev->ethtool_ops->begin)
  655. if ((rc = dev->ethtool_ops->begin(dev)) < 0)
  656. return rc;
  657. old_features = dev->features;
  658. switch (ethcmd) {
  659. case ETHTOOL_GSET:
  660. rc = ethtool_get_settings(dev, useraddr);
  661. break;
  662. case ETHTOOL_SSET:
  663. rc = ethtool_set_settings(dev, useraddr);
  664. break;
  665. case ETHTOOL_GDRVINFO:
  666. rc = ethtool_get_drvinfo(dev, useraddr);
  667. break;
  668. case ETHTOOL_GREGS:
  669. rc = ethtool_get_regs(dev, useraddr);
  670. break;
  671. case ETHTOOL_GWOL:
  672. rc = ethtool_get_wol(dev, useraddr);
  673. break;
  674. case ETHTOOL_SWOL:
  675. rc = ethtool_set_wol(dev, useraddr);
  676. break;
  677. case ETHTOOL_GMSGLVL:
  678. rc = ethtool_get_value(dev, useraddr, ethcmd,
  679. dev->ethtool_ops->get_msglevel);
  680. break;
  681. case ETHTOOL_SMSGLVL:
  682. rc = ethtool_set_value_void(dev, useraddr,
  683. dev->ethtool_ops->set_msglevel);
  684. break;
  685. case ETHTOOL_NWAY_RST:
  686. rc = ethtool_nway_reset(dev);
  687. break;
  688. case ETHTOOL_GLINK:
  689. rc = ethtool_get_value(dev, useraddr, ethcmd,
  690. dev->ethtool_ops->get_link);
  691. break;
  692. case ETHTOOL_GEEPROM:
  693. rc = ethtool_get_eeprom(dev, useraddr);
  694. break;
  695. case ETHTOOL_SEEPROM:
  696. rc = ethtool_set_eeprom(dev, useraddr);
  697. break;
  698. case ETHTOOL_GCOALESCE:
  699. rc = ethtool_get_coalesce(dev, useraddr);
  700. break;
  701. case ETHTOOL_SCOALESCE:
  702. rc = ethtool_set_coalesce(dev, useraddr);
  703. break;
  704. case ETHTOOL_GRINGPARAM:
  705. rc = ethtool_get_ringparam(dev, useraddr);
  706. break;
  707. case ETHTOOL_SRINGPARAM:
  708. rc = ethtool_set_ringparam(dev, useraddr);
  709. break;
  710. case ETHTOOL_GPAUSEPARAM:
  711. rc = ethtool_get_pauseparam(dev, useraddr);
  712. break;
  713. case ETHTOOL_SPAUSEPARAM:
  714. rc = ethtool_set_pauseparam(dev, useraddr);
  715. break;
  716. case ETHTOOL_GRXCSUM:
  717. rc = ethtool_get_value(dev, useraddr, ethcmd,
  718. dev->ethtool_ops->get_rx_csum);
  719. break;
  720. case ETHTOOL_SRXCSUM:
  721. rc = ethtool_set_value(dev, useraddr,
  722. dev->ethtool_ops->set_rx_csum);
  723. break;
  724. case ETHTOOL_GTXCSUM:
  725. rc = ethtool_get_value(dev, useraddr, ethcmd,
  726. (dev->ethtool_ops->get_tx_csum ?
  727. dev->ethtool_ops->get_tx_csum :
  728. ethtool_op_get_tx_csum));
  729. break;
  730. case ETHTOOL_STXCSUM:
  731. rc = ethtool_set_tx_csum(dev, useraddr);
  732. break;
  733. case ETHTOOL_GSG:
  734. rc = ethtool_get_value(dev, useraddr, ethcmd,
  735. (dev->ethtool_ops->get_sg ?
  736. dev->ethtool_ops->get_sg :
  737. ethtool_op_get_sg));
  738. break;
  739. case ETHTOOL_SSG:
  740. rc = ethtool_set_sg(dev, useraddr);
  741. break;
  742. case ETHTOOL_GTSO:
  743. rc = ethtool_get_value(dev, useraddr, ethcmd,
  744. (dev->ethtool_ops->get_tso ?
  745. dev->ethtool_ops->get_tso :
  746. ethtool_op_get_tso));
  747. break;
  748. case ETHTOOL_STSO:
  749. rc = ethtool_set_tso(dev, useraddr);
  750. break;
  751. case ETHTOOL_TEST:
  752. rc = ethtool_self_test(dev, useraddr);
  753. break;
  754. case ETHTOOL_GSTRINGS:
  755. rc = ethtool_get_strings(dev, useraddr);
  756. break;
  757. case ETHTOOL_PHYS_ID:
  758. rc = ethtool_phys_id(dev, useraddr);
  759. break;
  760. case ETHTOOL_GSTATS:
  761. rc = ethtool_get_stats(dev, useraddr);
  762. break;
  763. case ETHTOOL_GPERMADDR:
  764. rc = ethtool_get_perm_addr(dev, useraddr);
  765. break;
  766. case ETHTOOL_GUFO:
  767. rc = ethtool_get_value(dev, useraddr, ethcmd,
  768. (dev->ethtool_ops->get_ufo ?
  769. dev->ethtool_ops->get_ufo :
  770. ethtool_op_get_ufo));
  771. break;
  772. case ETHTOOL_SUFO:
  773. rc = ethtool_set_ufo(dev, useraddr);
  774. break;
  775. case ETHTOOL_GGSO:
  776. rc = ethtool_get_gso(dev, useraddr);
  777. break;
  778. case ETHTOOL_SGSO:
  779. rc = ethtool_set_gso(dev, useraddr);
  780. break;
  781. case ETHTOOL_GFLAGS:
  782. rc = ethtool_get_value(dev, useraddr, ethcmd,
  783. dev->ethtool_ops->get_flags);
  784. break;
  785. case ETHTOOL_SFLAGS:
  786. rc = ethtool_set_value(dev, useraddr,
  787. dev->ethtool_ops->set_flags);
  788. break;
  789. case ETHTOOL_GPFLAGS:
  790. rc = ethtool_get_value(dev, useraddr, ethcmd,
  791. dev->ethtool_ops->get_priv_flags);
  792. break;
  793. case ETHTOOL_SPFLAGS:
  794. rc = ethtool_set_value(dev, useraddr,
  795. dev->ethtool_ops->set_priv_flags);
  796. break;
  797. default:
  798. rc = -EOPNOTSUPP;
  799. }
  800. if (dev->ethtool_ops->complete)
  801. dev->ethtool_ops->complete(dev);
  802. if (old_features != dev->features)
  803. netdev_features_change(dev);
  804. return rc;
  805. }
  806. EXPORT_SYMBOL(ethtool_op_get_link);
  807. EXPORT_SYMBOL(ethtool_op_get_sg);
  808. EXPORT_SYMBOL(ethtool_op_get_tso);
  809. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  810. EXPORT_SYMBOL(ethtool_op_set_sg);
  811. EXPORT_SYMBOL(ethtool_op_set_tso);
  812. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  813. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
  814. EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
  815. EXPORT_SYMBOL(ethtool_op_set_ufo);
  816. EXPORT_SYMBOL(ethtool_op_get_ufo);
  817. EXPORT_SYMBOL(ethtool_op_set_flags);
  818. EXPORT_SYMBOL(ethtool_op_get_flags);