ethtool.c 21 KB

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