ethtool.c 26 KB

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