ethtool.c 25 KB

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