ethtool.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. We fall back to calling do_ioctl()
  7. * for drivers which haven't been converted to ethtool_ops yet.
  8. *
  9. * It's GPL, stupid.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/errno.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/netdevice.h>
  16. #include <asm/uaccess.h>
  17. /*
  18. * Some useful ethtool_ops methods that're device independent.
  19. * If we find that all drivers want to do the same thing here,
  20. * we can turn these into dev_() function calls.
  21. */
  22. u32 ethtool_op_get_link(struct net_device *dev)
  23. {
  24. return netif_carrier_ok(dev) ? 1 : 0;
  25. }
  26. u32 ethtool_op_get_tx_csum(struct net_device *dev)
  27. {
  28. return (dev->features & (NETIF_F_IP_CSUM | NETIF_F_HW_CSUM)) != 0;
  29. }
  30. int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
  31. {
  32. if (data)
  33. dev->features |= NETIF_F_IP_CSUM;
  34. else
  35. dev->features &= ~NETIF_F_IP_CSUM;
  36. return 0;
  37. }
  38. int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
  39. {
  40. if (data)
  41. dev->features |= NETIF_F_HW_CSUM;
  42. else
  43. dev->features &= ~NETIF_F_HW_CSUM;
  44. return 0;
  45. }
  46. u32 ethtool_op_get_sg(struct net_device *dev)
  47. {
  48. return (dev->features & NETIF_F_SG) != 0;
  49. }
  50. int ethtool_op_set_sg(struct net_device *dev, u32 data)
  51. {
  52. if (data)
  53. dev->features |= NETIF_F_SG;
  54. else
  55. dev->features &= ~NETIF_F_SG;
  56. return 0;
  57. }
  58. u32 ethtool_op_get_tso(struct net_device *dev)
  59. {
  60. return (dev->features & NETIF_F_TSO) != 0;
  61. }
  62. int ethtool_op_set_tso(struct net_device *dev, u32 data)
  63. {
  64. if (data)
  65. dev->features |= NETIF_F_TSO;
  66. else
  67. dev->features &= ~NETIF_F_TSO;
  68. return 0;
  69. }
  70. int ethtool_op_get_perm_addr(struct net_device *dev, struct ethtool_perm_addr *addr, u8 *data)
  71. {
  72. unsigned char len = dev->addr_len;
  73. if ( addr->size < len )
  74. return -ETOOSMALL;
  75. addr->size = len;
  76. memcpy(data, dev->perm_addr, len);
  77. return 0;
  78. }
  79. /* Handlers for each ethtool command */
  80. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  81. {
  82. struct ethtool_cmd cmd = { ETHTOOL_GSET };
  83. int err;
  84. if (!dev->ethtool_ops->get_settings)
  85. return -EOPNOTSUPP;
  86. err = dev->ethtool_ops->get_settings(dev, &cmd);
  87. if (err < 0)
  88. return err;
  89. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  90. return -EFAULT;
  91. return 0;
  92. }
  93. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  94. {
  95. struct ethtool_cmd cmd;
  96. if (!dev->ethtool_ops->set_settings)
  97. return -EOPNOTSUPP;
  98. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  99. return -EFAULT;
  100. return dev->ethtool_ops->set_settings(dev, &cmd);
  101. }
  102. static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
  103. {
  104. struct ethtool_drvinfo info;
  105. struct ethtool_ops *ops = dev->ethtool_ops;
  106. if (!ops->get_drvinfo)
  107. return -EOPNOTSUPP;
  108. memset(&info, 0, sizeof(info));
  109. info.cmd = ETHTOOL_GDRVINFO;
  110. ops->get_drvinfo(dev, &info);
  111. if (ops->self_test_count)
  112. info.testinfo_len = ops->self_test_count(dev);
  113. if (ops->get_stats_count)
  114. info.n_stats = ops->get_stats_count(dev);
  115. if (ops->get_regs_len)
  116. info.regdump_len = ops->get_regs_len(dev);
  117. if (ops->get_eeprom_len)
  118. info.eedump_len = ops->get_eeprom_len(dev);
  119. if (copy_to_user(useraddr, &info, sizeof(info)))
  120. return -EFAULT;
  121. return 0;
  122. }
  123. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  124. {
  125. struct ethtool_regs regs;
  126. struct ethtool_ops *ops = dev->ethtool_ops;
  127. void *regbuf;
  128. int reglen, ret;
  129. if (!ops->get_regs || !ops->get_regs_len)
  130. return -EOPNOTSUPP;
  131. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  132. return -EFAULT;
  133. reglen = ops->get_regs_len(dev);
  134. if (regs.len > reglen)
  135. regs.len = reglen;
  136. regbuf = kmalloc(reglen, GFP_USER);
  137. if (!regbuf)
  138. return -ENOMEM;
  139. ops->get_regs(dev, &regs, regbuf);
  140. ret = -EFAULT;
  141. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  142. goto out;
  143. useraddr += offsetof(struct ethtool_regs, data);
  144. if (copy_to_user(useraddr, regbuf, regs.len))
  145. goto out;
  146. ret = 0;
  147. out:
  148. kfree(regbuf);
  149. return ret;
  150. }
  151. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  152. {
  153. struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
  154. if (!dev->ethtool_ops->get_wol)
  155. return -EOPNOTSUPP;
  156. dev->ethtool_ops->get_wol(dev, &wol);
  157. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  158. return -EFAULT;
  159. return 0;
  160. }
  161. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  162. {
  163. struct ethtool_wolinfo wol;
  164. if (!dev->ethtool_ops->set_wol)
  165. return -EOPNOTSUPP;
  166. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  167. return -EFAULT;
  168. return dev->ethtool_ops->set_wol(dev, &wol);
  169. }
  170. static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
  171. {
  172. struct ethtool_value edata = { ETHTOOL_GMSGLVL };
  173. if (!dev->ethtool_ops->get_msglevel)
  174. return -EOPNOTSUPP;
  175. edata.data = dev->ethtool_ops->get_msglevel(dev);
  176. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  177. return -EFAULT;
  178. return 0;
  179. }
  180. static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
  181. {
  182. struct ethtool_value edata;
  183. if (!dev->ethtool_ops->set_msglevel)
  184. return -EOPNOTSUPP;
  185. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  186. return -EFAULT;
  187. dev->ethtool_ops->set_msglevel(dev, edata.data);
  188. return 0;
  189. }
  190. static int ethtool_nway_reset(struct net_device *dev)
  191. {
  192. if (!dev->ethtool_ops->nway_reset)
  193. return -EOPNOTSUPP;
  194. return dev->ethtool_ops->nway_reset(dev);
  195. }
  196. static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
  197. {
  198. struct ethtool_value edata = { ETHTOOL_GLINK };
  199. if (!dev->ethtool_ops->get_link)
  200. return -EOPNOTSUPP;
  201. edata.data = dev->ethtool_ops->get_link(dev);
  202. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  203. return -EFAULT;
  204. return 0;
  205. }
  206. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  207. {
  208. struct ethtool_eeprom eeprom;
  209. struct ethtool_ops *ops = dev->ethtool_ops;
  210. u8 *data;
  211. int ret;
  212. if (!ops->get_eeprom || !ops->get_eeprom_len)
  213. return -EOPNOTSUPP;
  214. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  215. return -EFAULT;
  216. /* Check for wrap and zero */
  217. if (eeprom.offset + eeprom.len <= eeprom.offset)
  218. return -EINVAL;
  219. /* Check for exceeding total eeprom len */
  220. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  221. return -EINVAL;
  222. data = kmalloc(eeprom.len, GFP_USER);
  223. if (!data)
  224. return -ENOMEM;
  225. ret = -EFAULT;
  226. if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
  227. goto out;
  228. ret = ops->get_eeprom(dev, &eeprom, data);
  229. if (ret)
  230. goto out;
  231. ret = -EFAULT;
  232. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  233. goto out;
  234. if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
  235. goto out;
  236. ret = 0;
  237. out:
  238. kfree(data);
  239. return ret;
  240. }
  241. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  242. {
  243. struct ethtool_eeprom eeprom;
  244. struct ethtool_ops *ops = dev->ethtool_ops;
  245. u8 *data;
  246. int ret;
  247. if (!ops->set_eeprom || !ops->get_eeprom_len)
  248. return -EOPNOTSUPP;
  249. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  250. return -EFAULT;
  251. /* Check for wrap and zero */
  252. if (eeprom.offset + eeprom.len <= eeprom.offset)
  253. return -EINVAL;
  254. /* Check for exceeding total eeprom len */
  255. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  256. return -EINVAL;
  257. data = kmalloc(eeprom.len, GFP_USER);
  258. if (!data)
  259. return -ENOMEM;
  260. ret = -EFAULT;
  261. if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
  262. goto out;
  263. ret = ops->set_eeprom(dev, &eeprom, data);
  264. if (ret)
  265. goto out;
  266. if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
  267. ret = -EFAULT;
  268. out:
  269. kfree(data);
  270. return ret;
  271. }
  272. static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
  273. {
  274. struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
  275. if (!dev->ethtool_ops->get_coalesce)
  276. return -EOPNOTSUPP;
  277. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  278. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  279. return -EFAULT;
  280. return 0;
  281. }
  282. static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
  283. {
  284. struct ethtool_coalesce coalesce;
  285. if (!dev->ethtool_ops->set_coalesce)
  286. return -EOPNOTSUPP;
  287. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  288. return -EFAULT;
  289. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  290. }
  291. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  292. {
  293. struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
  294. if (!dev->ethtool_ops->get_ringparam)
  295. return -EOPNOTSUPP;
  296. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  297. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  298. return -EFAULT;
  299. return 0;
  300. }
  301. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  302. {
  303. struct ethtool_ringparam ringparam;
  304. if (!dev->ethtool_ops->set_ringparam)
  305. return -EOPNOTSUPP;
  306. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  307. return -EFAULT;
  308. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  309. }
  310. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  311. {
  312. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  313. if (!dev->ethtool_ops->get_pauseparam)
  314. return -EOPNOTSUPP;
  315. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  316. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  317. return -EFAULT;
  318. return 0;
  319. }
  320. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  321. {
  322. struct ethtool_pauseparam pauseparam;
  323. if (!dev->ethtool_ops->get_pauseparam)
  324. return -EOPNOTSUPP;
  325. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  326. return -EFAULT;
  327. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  328. }
  329. static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
  330. {
  331. struct ethtool_value edata = { ETHTOOL_GRXCSUM };
  332. if (!dev->ethtool_ops->get_rx_csum)
  333. return -EOPNOTSUPP;
  334. edata.data = dev->ethtool_ops->get_rx_csum(dev);
  335. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  336. return -EFAULT;
  337. return 0;
  338. }
  339. static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
  340. {
  341. struct ethtool_value edata;
  342. if (!dev->ethtool_ops->set_rx_csum)
  343. return -EOPNOTSUPP;
  344. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  345. return -EFAULT;
  346. dev->ethtool_ops->set_rx_csum(dev, edata.data);
  347. return 0;
  348. }
  349. static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
  350. {
  351. struct ethtool_value edata = { ETHTOOL_GTXCSUM };
  352. if (!dev->ethtool_ops->get_tx_csum)
  353. return -EOPNOTSUPP;
  354. edata.data = dev->ethtool_ops->get_tx_csum(dev);
  355. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  356. return -EFAULT;
  357. return 0;
  358. }
  359. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  360. {
  361. int err;
  362. if (!data && dev->ethtool_ops->set_tso) {
  363. err = dev->ethtool_ops->set_tso(dev, 0);
  364. if (err)
  365. return err;
  366. }
  367. return dev->ethtool_ops->set_sg(dev, data);
  368. }
  369. static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
  370. {
  371. struct ethtool_value edata;
  372. int err;
  373. if (!dev->ethtool_ops->set_tx_csum)
  374. return -EOPNOTSUPP;
  375. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  376. return -EFAULT;
  377. if (!edata.data && dev->ethtool_ops->set_sg) {
  378. err = __ethtool_set_sg(dev, 0);
  379. if (err)
  380. return err;
  381. }
  382. return dev->ethtool_ops->set_tx_csum(dev, edata.data);
  383. }
  384. static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
  385. {
  386. struct ethtool_value edata = { ETHTOOL_GSG };
  387. if (!dev->ethtool_ops->get_sg)
  388. return -EOPNOTSUPP;
  389. edata.data = dev->ethtool_ops->get_sg(dev);
  390. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  391. return -EFAULT;
  392. return 0;
  393. }
  394. static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
  395. {
  396. struct ethtool_value edata;
  397. if (!dev->ethtool_ops->set_sg)
  398. return -EOPNOTSUPP;
  399. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  400. return -EFAULT;
  401. if (edata.data &&
  402. !(dev->features & (NETIF_F_IP_CSUM |
  403. NETIF_F_NO_CSUM |
  404. NETIF_F_HW_CSUM)))
  405. return -EINVAL;
  406. return __ethtool_set_sg(dev, edata.data);
  407. }
  408. static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
  409. {
  410. struct ethtool_value edata = { ETHTOOL_GTSO };
  411. if (!dev->ethtool_ops->get_tso)
  412. return -EOPNOTSUPP;
  413. edata.data = dev->ethtool_ops->get_tso(dev);
  414. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  415. return -EFAULT;
  416. return 0;
  417. }
  418. static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
  419. {
  420. struct ethtool_value edata;
  421. if (!dev->ethtool_ops->set_tso)
  422. return -EOPNOTSUPP;
  423. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  424. return -EFAULT;
  425. if (edata.data && !(dev->features & NETIF_F_SG))
  426. return -EINVAL;
  427. return dev->ethtool_ops->set_tso(dev, edata.data);
  428. }
  429. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  430. {
  431. struct ethtool_test test;
  432. struct ethtool_ops *ops = dev->ethtool_ops;
  433. u64 *data;
  434. int ret;
  435. if (!ops->self_test || !ops->self_test_count)
  436. return -EOPNOTSUPP;
  437. if (copy_from_user(&test, useraddr, sizeof(test)))
  438. return -EFAULT;
  439. test.len = ops->self_test_count(dev);
  440. data = kmalloc(test.len * sizeof(u64), GFP_USER);
  441. if (!data)
  442. return -ENOMEM;
  443. ops->self_test(dev, &test, data);
  444. ret = -EFAULT;
  445. if (copy_to_user(useraddr, &test, sizeof(test)))
  446. goto out;
  447. useraddr += sizeof(test);
  448. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  449. goto out;
  450. ret = 0;
  451. out:
  452. kfree(data);
  453. return ret;
  454. }
  455. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  456. {
  457. struct ethtool_gstrings gstrings;
  458. struct ethtool_ops *ops = dev->ethtool_ops;
  459. u8 *data;
  460. int ret;
  461. if (!ops->get_strings)
  462. return -EOPNOTSUPP;
  463. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  464. return -EFAULT;
  465. switch (gstrings.string_set) {
  466. case ETH_SS_TEST:
  467. if (!ops->self_test_count)
  468. return -EOPNOTSUPP;
  469. gstrings.len = ops->self_test_count(dev);
  470. break;
  471. case ETH_SS_STATS:
  472. if (!ops->get_stats_count)
  473. return -EOPNOTSUPP;
  474. gstrings.len = ops->get_stats_count(dev);
  475. break;
  476. default:
  477. return -EINVAL;
  478. }
  479. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  480. if (!data)
  481. return -ENOMEM;
  482. ops->get_strings(dev, gstrings.string_set, data);
  483. ret = -EFAULT;
  484. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  485. goto out;
  486. useraddr += sizeof(gstrings);
  487. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  488. goto out;
  489. ret = 0;
  490. out:
  491. kfree(data);
  492. return ret;
  493. }
  494. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  495. {
  496. struct ethtool_value id;
  497. if (!dev->ethtool_ops->phys_id)
  498. return -EOPNOTSUPP;
  499. if (copy_from_user(&id, useraddr, sizeof(id)))
  500. return -EFAULT;
  501. return dev->ethtool_ops->phys_id(dev, id.data);
  502. }
  503. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  504. {
  505. struct ethtool_stats stats;
  506. struct ethtool_ops *ops = dev->ethtool_ops;
  507. u64 *data;
  508. int ret;
  509. if (!ops->get_ethtool_stats || !ops->get_stats_count)
  510. return -EOPNOTSUPP;
  511. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  512. return -EFAULT;
  513. stats.n_stats = ops->get_stats_count(dev);
  514. data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
  515. if (!data)
  516. return -ENOMEM;
  517. ops->get_ethtool_stats(dev, &stats, data);
  518. ret = -EFAULT;
  519. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  520. goto out;
  521. useraddr += sizeof(stats);
  522. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  523. goto out;
  524. ret = 0;
  525. out:
  526. kfree(data);
  527. return ret;
  528. }
  529. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  530. {
  531. struct ethtool_perm_addr epaddr;
  532. u8 *data;
  533. int ret;
  534. if (!dev->ethtool_ops->get_perm_addr)
  535. return -EOPNOTSUPP;
  536. if (copy_from_user(&epaddr,useraddr,sizeof(epaddr)))
  537. return -EFAULT;
  538. data = kmalloc(epaddr.size, GFP_USER);
  539. if (!data)
  540. return -ENOMEM;
  541. ret = dev->ethtool_ops->get_perm_addr(dev,&epaddr,data);
  542. if (ret)
  543. return ret;
  544. ret = -EFAULT;
  545. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  546. goto out;
  547. useraddr += sizeof(epaddr);
  548. if (copy_to_user(useraddr, data, epaddr.size))
  549. goto out;
  550. ret = 0;
  551. out:
  552. kfree(data);
  553. return ret;
  554. }
  555. /* The main entry point in this file. Called from net/core/dev.c */
  556. int dev_ethtool(struct ifreq *ifr)
  557. {
  558. struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
  559. void __user *useraddr = ifr->ifr_data;
  560. u32 ethcmd;
  561. int rc;
  562. unsigned long old_features;
  563. /*
  564. * XXX: This can be pushed down into the ethtool_* handlers that
  565. * need it. Keep existing behaviour for the moment.
  566. */
  567. if (!capable(CAP_NET_ADMIN))
  568. return -EPERM;
  569. if (!dev || !netif_device_present(dev))
  570. return -ENODEV;
  571. if (!dev->ethtool_ops)
  572. goto ioctl;
  573. if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
  574. return -EFAULT;
  575. if(dev->ethtool_ops->begin)
  576. if ((rc = dev->ethtool_ops->begin(dev)) < 0)
  577. return rc;
  578. old_features = dev->features;
  579. switch (ethcmd) {
  580. case ETHTOOL_GSET:
  581. rc = ethtool_get_settings(dev, useraddr);
  582. break;
  583. case ETHTOOL_SSET:
  584. rc = ethtool_set_settings(dev, useraddr);
  585. break;
  586. case ETHTOOL_GDRVINFO:
  587. rc = ethtool_get_drvinfo(dev, useraddr);
  588. break;
  589. case ETHTOOL_GREGS:
  590. rc = ethtool_get_regs(dev, useraddr);
  591. break;
  592. case ETHTOOL_GWOL:
  593. rc = ethtool_get_wol(dev, useraddr);
  594. break;
  595. case ETHTOOL_SWOL:
  596. rc = ethtool_set_wol(dev, useraddr);
  597. break;
  598. case ETHTOOL_GMSGLVL:
  599. rc = ethtool_get_msglevel(dev, useraddr);
  600. break;
  601. case ETHTOOL_SMSGLVL:
  602. rc = ethtool_set_msglevel(dev, useraddr);
  603. break;
  604. case ETHTOOL_NWAY_RST:
  605. rc = ethtool_nway_reset(dev);
  606. break;
  607. case ETHTOOL_GLINK:
  608. rc = ethtool_get_link(dev, useraddr);
  609. break;
  610. case ETHTOOL_GEEPROM:
  611. rc = ethtool_get_eeprom(dev, useraddr);
  612. break;
  613. case ETHTOOL_SEEPROM:
  614. rc = ethtool_set_eeprom(dev, useraddr);
  615. break;
  616. case ETHTOOL_GCOALESCE:
  617. rc = ethtool_get_coalesce(dev, useraddr);
  618. break;
  619. case ETHTOOL_SCOALESCE:
  620. rc = ethtool_set_coalesce(dev, useraddr);
  621. break;
  622. case ETHTOOL_GRINGPARAM:
  623. rc = ethtool_get_ringparam(dev, useraddr);
  624. break;
  625. case ETHTOOL_SRINGPARAM:
  626. rc = ethtool_set_ringparam(dev, useraddr);
  627. break;
  628. case ETHTOOL_GPAUSEPARAM:
  629. rc = ethtool_get_pauseparam(dev, useraddr);
  630. break;
  631. case ETHTOOL_SPAUSEPARAM:
  632. rc = ethtool_set_pauseparam(dev, useraddr);
  633. break;
  634. case ETHTOOL_GRXCSUM:
  635. rc = ethtool_get_rx_csum(dev, useraddr);
  636. break;
  637. case ETHTOOL_SRXCSUM:
  638. rc = ethtool_set_rx_csum(dev, useraddr);
  639. break;
  640. case ETHTOOL_GTXCSUM:
  641. rc = ethtool_get_tx_csum(dev, useraddr);
  642. break;
  643. case ETHTOOL_STXCSUM:
  644. rc = ethtool_set_tx_csum(dev, useraddr);
  645. break;
  646. case ETHTOOL_GSG:
  647. rc = ethtool_get_sg(dev, useraddr);
  648. break;
  649. case ETHTOOL_SSG:
  650. rc = ethtool_set_sg(dev, useraddr);
  651. break;
  652. case ETHTOOL_GTSO:
  653. rc = ethtool_get_tso(dev, useraddr);
  654. break;
  655. case ETHTOOL_STSO:
  656. rc = ethtool_set_tso(dev, useraddr);
  657. break;
  658. case ETHTOOL_TEST:
  659. rc = ethtool_self_test(dev, useraddr);
  660. break;
  661. case ETHTOOL_GSTRINGS:
  662. rc = ethtool_get_strings(dev, useraddr);
  663. break;
  664. case ETHTOOL_PHYS_ID:
  665. rc = ethtool_phys_id(dev, useraddr);
  666. break;
  667. case ETHTOOL_GSTATS:
  668. rc = ethtool_get_stats(dev, useraddr);
  669. break;
  670. case ETHTOOL_GPERMADDR:
  671. rc = ethtool_get_perm_addr(dev, useraddr);
  672. break;
  673. default:
  674. rc = -EOPNOTSUPP;
  675. }
  676. if(dev->ethtool_ops->complete)
  677. dev->ethtool_ops->complete(dev);
  678. if (old_features != dev->features)
  679. netdev_features_change(dev);
  680. return rc;
  681. ioctl:
  682. if (dev->do_ioctl)
  683. return dev->do_ioctl(dev, ifr, SIOCETHTOOL);
  684. return -EOPNOTSUPP;
  685. }
  686. EXPORT_SYMBOL(dev_ethtool);
  687. EXPORT_SYMBOL(ethtool_op_get_link);
  688. EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr);
  689. EXPORT_SYMBOL(ethtool_op_get_sg);
  690. EXPORT_SYMBOL(ethtool_op_get_tso);
  691. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  692. EXPORT_SYMBOL(ethtool_op_set_sg);
  693. EXPORT_SYMBOL(ethtool_op_set_tso);
  694. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  695. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);