ethtool.c 23 KB

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