ethtool.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. u8 *data;
  230. int ret;
  231. if (!ops->get_eeprom || !ops->get_eeprom_len)
  232. return -EOPNOTSUPP;
  233. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  234. return -EFAULT;
  235. /* Check for wrap and zero */
  236. if (eeprom.offset + eeprom.len <= eeprom.offset)
  237. return -EINVAL;
  238. /* Check for exceeding total eeprom len */
  239. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  240. return -EINVAL;
  241. data = kmalloc(eeprom.len, GFP_USER);
  242. if (!data)
  243. return -ENOMEM;
  244. ret = -EFAULT;
  245. if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
  246. goto out;
  247. ret = ops->get_eeprom(dev, &eeprom, data);
  248. if (ret)
  249. goto out;
  250. ret = -EFAULT;
  251. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  252. goto out;
  253. if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
  254. goto out;
  255. ret = 0;
  256. out:
  257. kfree(data);
  258. return ret;
  259. }
  260. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  261. {
  262. struct ethtool_eeprom eeprom;
  263. const struct ethtool_ops *ops = dev->ethtool_ops;
  264. u8 *data;
  265. int ret;
  266. if (!ops->set_eeprom || !ops->get_eeprom_len)
  267. return -EOPNOTSUPP;
  268. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  269. return -EFAULT;
  270. /* Check for wrap and zero */
  271. if (eeprom.offset + eeprom.len <= eeprom.offset)
  272. return -EINVAL;
  273. /* Check for exceeding total eeprom len */
  274. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  275. return -EINVAL;
  276. data = kmalloc(eeprom.len, GFP_USER);
  277. if (!data)
  278. return -ENOMEM;
  279. ret = -EFAULT;
  280. if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
  281. goto out;
  282. ret = ops->set_eeprom(dev, &eeprom, data);
  283. if (ret)
  284. goto out;
  285. if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
  286. ret = -EFAULT;
  287. out:
  288. kfree(data);
  289. return ret;
  290. }
  291. static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
  292. {
  293. struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
  294. if (!dev->ethtool_ops->get_coalesce)
  295. return -EOPNOTSUPP;
  296. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  297. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  298. return -EFAULT;
  299. return 0;
  300. }
  301. static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
  302. {
  303. struct ethtool_coalesce coalesce;
  304. if (!dev->ethtool_ops->set_coalesce)
  305. return -EOPNOTSUPP;
  306. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  307. return -EFAULT;
  308. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  309. }
  310. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  311. {
  312. struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
  313. if (!dev->ethtool_ops->get_ringparam)
  314. return -EOPNOTSUPP;
  315. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  316. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  317. return -EFAULT;
  318. return 0;
  319. }
  320. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  321. {
  322. struct ethtool_ringparam ringparam;
  323. if (!dev->ethtool_ops->set_ringparam)
  324. return -EOPNOTSUPP;
  325. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  326. return -EFAULT;
  327. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  328. }
  329. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  330. {
  331. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  332. if (!dev->ethtool_ops->get_pauseparam)
  333. return -EOPNOTSUPP;
  334. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  335. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  336. return -EFAULT;
  337. return 0;
  338. }
  339. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  340. {
  341. struct ethtool_pauseparam pauseparam;
  342. if (!dev->ethtool_ops->set_pauseparam)
  343. return -EOPNOTSUPP;
  344. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  345. return -EFAULT;
  346. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  347. }
  348. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  349. {
  350. int err;
  351. if (!data && dev->ethtool_ops->set_tso) {
  352. err = dev->ethtool_ops->set_tso(dev, 0);
  353. if (err)
  354. return err;
  355. }
  356. if (!data && dev->ethtool_ops->set_ufo) {
  357. err = dev->ethtool_ops->set_ufo(dev, 0);
  358. if (err)
  359. return err;
  360. }
  361. return dev->ethtool_ops->set_sg(dev, data);
  362. }
  363. static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
  364. {
  365. struct ethtool_value edata;
  366. int err;
  367. if (!dev->ethtool_ops->set_tx_csum)
  368. return -EOPNOTSUPP;
  369. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  370. return -EFAULT;
  371. if (!edata.data && dev->ethtool_ops->set_sg) {
  372. err = __ethtool_set_sg(dev, 0);
  373. if (err)
  374. return err;
  375. }
  376. return dev->ethtool_ops->set_tx_csum(dev, edata.data);
  377. }
  378. static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
  379. {
  380. struct ethtool_value edata;
  381. if (!dev->ethtool_ops->set_sg)
  382. return -EOPNOTSUPP;
  383. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  384. return -EFAULT;
  385. if (edata.data &&
  386. !(dev->features & NETIF_F_ALL_CSUM))
  387. return -EINVAL;
  388. return __ethtool_set_sg(dev, edata.data);
  389. }
  390. static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
  391. {
  392. struct ethtool_value edata;
  393. if (!dev->ethtool_ops->set_tso)
  394. return -EOPNOTSUPP;
  395. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  396. return -EFAULT;
  397. if (edata.data && !(dev->features & NETIF_F_SG))
  398. return -EINVAL;
  399. return dev->ethtool_ops->set_tso(dev, edata.data);
  400. }
  401. static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
  402. {
  403. struct ethtool_value edata;
  404. if (!dev->ethtool_ops->set_ufo)
  405. return -EOPNOTSUPP;
  406. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  407. return -EFAULT;
  408. if (edata.data && !(dev->features & NETIF_F_SG))
  409. return -EINVAL;
  410. if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
  411. return -EINVAL;
  412. return dev->ethtool_ops->set_ufo(dev, edata.data);
  413. }
  414. static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
  415. {
  416. struct ethtool_value edata = { ETHTOOL_GGSO };
  417. edata.data = dev->features & NETIF_F_GSO;
  418. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  419. return -EFAULT;
  420. return 0;
  421. }
  422. static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
  423. {
  424. struct ethtool_value edata;
  425. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  426. return -EFAULT;
  427. if (edata.data)
  428. dev->features |= NETIF_F_GSO;
  429. else
  430. dev->features &= ~NETIF_F_GSO;
  431. return 0;
  432. }
  433. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  434. {
  435. struct ethtool_test test;
  436. const struct ethtool_ops *ops = dev->ethtool_ops;
  437. u64 *data;
  438. int ret, test_len;
  439. if (!ops->self_test)
  440. return -EOPNOTSUPP;
  441. if (!ops->get_sset_count && !ops->self_test_count)
  442. return -EOPNOTSUPP;
  443. if (ops->get_sset_count)
  444. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  445. else
  446. /* code path for obsolete hook */
  447. test_len = ops->self_test_count(dev);
  448. if (test_len < 0)
  449. return test_len;
  450. WARN_ON(test_len == 0);
  451. if (copy_from_user(&test, useraddr, sizeof(test)))
  452. return -EFAULT;
  453. test.len = test_len;
  454. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  455. if (!data)
  456. return -ENOMEM;
  457. ops->self_test(dev, &test, data);
  458. ret = -EFAULT;
  459. if (copy_to_user(useraddr, &test, sizeof(test)))
  460. goto out;
  461. useraddr += sizeof(test);
  462. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  463. goto out;
  464. ret = 0;
  465. out:
  466. kfree(data);
  467. return ret;
  468. }
  469. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  470. {
  471. struct ethtool_gstrings gstrings;
  472. const struct ethtool_ops *ops = dev->ethtool_ops;
  473. u8 *data;
  474. int ret;
  475. if (!ops->get_strings)
  476. return -EOPNOTSUPP;
  477. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  478. return -EFAULT;
  479. if (ops->get_sset_count) {
  480. ret = ops->get_sset_count(dev, gstrings.string_set);
  481. if (ret < 0)
  482. return ret;
  483. gstrings.len = ret;
  484. } else {
  485. /* code path for obsolete hooks */
  486. switch (gstrings.string_set) {
  487. case ETH_SS_TEST:
  488. if (!ops->self_test_count)
  489. return -EOPNOTSUPP;
  490. gstrings.len = ops->self_test_count(dev);
  491. break;
  492. case ETH_SS_STATS:
  493. if (!ops->get_stats_count)
  494. return -EOPNOTSUPP;
  495. gstrings.len = ops->get_stats_count(dev);
  496. break;
  497. default:
  498. return -EINVAL;
  499. }
  500. }
  501. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  502. if (!data)
  503. return -ENOMEM;
  504. ops->get_strings(dev, gstrings.string_set, data);
  505. ret = -EFAULT;
  506. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  507. goto out;
  508. useraddr += sizeof(gstrings);
  509. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  510. goto out;
  511. ret = 0;
  512. out:
  513. kfree(data);
  514. return ret;
  515. }
  516. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  517. {
  518. struct ethtool_value id;
  519. if (!dev->ethtool_ops->phys_id)
  520. return -EOPNOTSUPP;
  521. if (copy_from_user(&id, useraddr, sizeof(id)))
  522. return -EFAULT;
  523. return dev->ethtool_ops->phys_id(dev, id.data);
  524. }
  525. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  526. {
  527. struct ethtool_stats stats;
  528. const struct ethtool_ops *ops = dev->ethtool_ops;
  529. u64 *data;
  530. int ret, n_stats;
  531. if (!ops->get_ethtool_stats)
  532. return -EOPNOTSUPP;
  533. if (!ops->get_sset_count && !ops->get_stats_count)
  534. return -EOPNOTSUPP;
  535. if (ops->get_sset_count)
  536. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  537. else
  538. /* code path for obsolete hook */
  539. n_stats = ops->get_stats_count(dev);
  540. if (n_stats < 0)
  541. return n_stats;
  542. WARN_ON(n_stats == 0);
  543. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  544. return -EFAULT;
  545. stats.n_stats = n_stats;
  546. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  547. if (!data)
  548. return -ENOMEM;
  549. ops->get_ethtool_stats(dev, &stats, data);
  550. ret = -EFAULT;
  551. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  552. goto out;
  553. useraddr += sizeof(stats);
  554. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  555. goto out;
  556. ret = 0;
  557. out:
  558. kfree(data);
  559. return ret;
  560. }
  561. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  562. {
  563. struct ethtool_perm_addr epaddr;
  564. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  565. return -EFAULT;
  566. if (epaddr.size < dev->addr_len)
  567. return -ETOOSMALL;
  568. epaddr.size = dev->addr_len;
  569. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  570. return -EFAULT;
  571. useraddr += sizeof(epaddr);
  572. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  573. return -EFAULT;
  574. return 0;
  575. }
  576. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  577. u32 cmd, u32 (*actor)(struct net_device *))
  578. {
  579. struct ethtool_value edata = { cmd };
  580. if (!actor)
  581. return -EOPNOTSUPP;
  582. edata.data = actor(dev);
  583. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  584. return -EFAULT;
  585. return 0;
  586. }
  587. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  588. void (*actor)(struct net_device *, u32))
  589. {
  590. struct ethtool_value edata;
  591. if (!actor)
  592. return -EOPNOTSUPP;
  593. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  594. return -EFAULT;
  595. actor(dev, edata.data);
  596. return 0;
  597. }
  598. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  599. int (*actor)(struct net_device *, u32))
  600. {
  601. struct ethtool_value edata;
  602. if (!actor)
  603. return -EOPNOTSUPP;
  604. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  605. return -EFAULT;
  606. return actor(dev, edata.data);
  607. }
  608. /* The main entry point in this file. Called from net/core/dev.c */
  609. int dev_ethtool(struct net *net, struct ifreq *ifr)
  610. {
  611. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  612. void __user *useraddr = ifr->ifr_data;
  613. u32 ethcmd;
  614. int rc;
  615. unsigned long old_features;
  616. if (!dev || !netif_device_present(dev))
  617. return -ENODEV;
  618. if (!dev->ethtool_ops)
  619. return -EOPNOTSUPP;
  620. if (copy_from_user(&ethcmd, useraddr, sizeof (ethcmd)))
  621. return -EFAULT;
  622. /* Allow some commands to be done by anyone */
  623. switch(ethcmd) {
  624. case ETHTOOL_GDRVINFO:
  625. case ETHTOOL_GMSGLVL:
  626. case ETHTOOL_GCOALESCE:
  627. case ETHTOOL_GRINGPARAM:
  628. case ETHTOOL_GPAUSEPARAM:
  629. case ETHTOOL_GRXCSUM:
  630. case ETHTOOL_GTXCSUM:
  631. case ETHTOOL_GSG:
  632. case ETHTOOL_GSTRINGS:
  633. case ETHTOOL_GTSO:
  634. case ETHTOOL_GPERMADDR:
  635. case ETHTOOL_GUFO:
  636. case ETHTOOL_GGSO:
  637. case ETHTOOL_GFLAGS:
  638. case ETHTOOL_GPFLAGS:
  639. break;
  640. default:
  641. if (!capable(CAP_NET_ADMIN))
  642. return -EPERM;
  643. }
  644. if (dev->ethtool_ops->begin)
  645. if ((rc = dev->ethtool_ops->begin(dev)) < 0)
  646. return rc;
  647. old_features = dev->features;
  648. switch (ethcmd) {
  649. case ETHTOOL_GSET:
  650. rc = ethtool_get_settings(dev, useraddr);
  651. break;
  652. case ETHTOOL_SSET:
  653. rc = ethtool_set_settings(dev, useraddr);
  654. break;
  655. case ETHTOOL_GDRVINFO:
  656. rc = ethtool_get_drvinfo(dev, useraddr);
  657. break;
  658. case ETHTOOL_GREGS:
  659. rc = ethtool_get_regs(dev, useraddr);
  660. break;
  661. case ETHTOOL_GWOL:
  662. rc = ethtool_get_wol(dev, useraddr);
  663. break;
  664. case ETHTOOL_SWOL:
  665. rc = ethtool_set_wol(dev, useraddr);
  666. break;
  667. case ETHTOOL_GMSGLVL:
  668. rc = ethtool_get_value(dev, useraddr, ethcmd,
  669. dev->ethtool_ops->get_msglevel);
  670. break;
  671. case ETHTOOL_SMSGLVL:
  672. rc = ethtool_set_value_void(dev, useraddr,
  673. dev->ethtool_ops->set_msglevel);
  674. break;
  675. case ETHTOOL_NWAY_RST:
  676. rc = ethtool_nway_reset(dev);
  677. break;
  678. case ETHTOOL_GLINK:
  679. rc = ethtool_get_value(dev, useraddr, ethcmd,
  680. dev->ethtool_ops->get_link);
  681. break;
  682. case ETHTOOL_GEEPROM:
  683. rc = ethtool_get_eeprom(dev, useraddr);
  684. break;
  685. case ETHTOOL_SEEPROM:
  686. rc = ethtool_set_eeprom(dev, useraddr);
  687. break;
  688. case ETHTOOL_GCOALESCE:
  689. rc = ethtool_get_coalesce(dev, useraddr);
  690. break;
  691. case ETHTOOL_SCOALESCE:
  692. rc = ethtool_set_coalesce(dev, useraddr);
  693. break;
  694. case ETHTOOL_GRINGPARAM:
  695. rc = ethtool_get_ringparam(dev, useraddr);
  696. break;
  697. case ETHTOOL_SRINGPARAM:
  698. rc = ethtool_set_ringparam(dev, useraddr);
  699. break;
  700. case ETHTOOL_GPAUSEPARAM:
  701. rc = ethtool_get_pauseparam(dev, useraddr);
  702. break;
  703. case ETHTOOL_SPAUSEPARAM:
  704. rc = ethtool_set_pauseparam(dev, useraddr);
  705. break;
  706. case ETHTOOL_GRXCSUM:
  707. rc = ethtool_get_value(dev, useraddr, ethcmd,
  708. dev->ethtool_ops->get_rx_csum);
  709. break;
  710. case ETHTOOL_SRXCSUM:
  711. rc = ethtool_set_value(dev, useraddr,
  712. dev->ethtool_ops->set_rx_csum);
  713. break;
  714. case ETHTOOL_GTXCSUM:
  715. rc = ethtool_get_value(dev, useraddr, ethcmd,
  716. (dev->ethtool_ops->get_tx_csum ?
  717. dev->ethtool_ops->get_tx_csum :
  718. ethtool_op_get_tx_csum));
  719. break;
  720. case ETHTOOL_STXCSUM:
  721. rc = ethtool_set_tx_csum(dev, useraddr);
  722. break;
  723. case ETHTOOL_GSG:
  724. rc = ethtool_get_value(dev, useraddr, ethcmd,
  725. (dev->ethtool_ops->get_sg ?
  726. dev->ethtool_ops->get_sg :
  727. ethtool_op_get_sg));
  728. break;
  729. case ETHTOOL_SSG:
  730. rc = ethtool_set_sg(dev, useraddr);
  731. break;
  732. case ETHTOOL_GTSO:
  733. rc = ethtool_get_value(dev, useraddr, ethcmd,
  734. (dev->ethtool_ops->get_tso ?
  735. dev->ethtool_ops->get_tso :
  736. ethtool_op_get_tso));
  737. break;
  738. case ETHTOOL_STSO:
  739. rc = ethtool_set_tso(dev, useraddr);
  740. break;
  741. case ETHTOOL_TEST:
  742. rc = ethtool_self_test(dev, useraddr);
  743. break;
  744. case ETHTOOL_GSTRINGS:
  745. rc = ethtool_get_strings(dev, useraddr);
  746. break;
  747. case ETHTOOL_PHYS_ID:
  748. rc = ethtool_phys_id(dev, useraddr);
  749. break;
  750. case ETHTOOL_GSTATS:
  751. rc = ethtool_get_stats(dev, useraddr);
  752. break;
  753. case ETHTOOL_GPERMADDR:
  754. rc = ethtool_get_perm_addr(dev, useraddr);
  755. break;
  756. case ETHTOOL_GUFO:
  757. rc = ethtool_get_value(dev, useraddr, ethcmd,
  758. (dev->ethtool_ops->get_ufo ?
  759. dev->ethtool_ops->get_ufo :
  760. ethtool_op_get_ufo));
  761. break;
  762. case ETHTOOL_SUFO:
  763. rc = ethtool_set_ufo(dev, useraddr);
  764. break;
  765. case ETHTOOL_GGSO:
  766. rc = ethtool_get_gso(dev, useraddr);
  767. break;
  768. case ETHTOOL_SGSO:
  769. rc = ethtool_set_gso(dev, useraddr);
  770. break;
  771. case ETHTOOL_GFLAGS:
  772. rc = ethtool_get_value(dev, useraddr, ethcmd,
  773. dev->ethtool_ops->get_flags);
  774. break;
  775. case ETHTOOL_SFLAGS:
  776. rc = ethtool_set_value(dev, useraddr,
  777. dev->ethtool_ops->set_flags);
  778. break;
  779. case ETHTOOL_GPFLAGS:
  780. rc = ethtool_get_value(dev, useraddr, ethcmd,
  781. dev->ethtool_ops->get_priv_flags);
  782. break;
  783. case ETHTOOL_SPFLAGS:
  784. rc = ethtool_set_value(dev, useraddr,
  785. dev->ethtool_ops->set_priv_flags);
  786. break;
  787. default:
  788. rc = -EOPNOTSUPP;
  789. }
  790. if (dev->ethtool_ops->complete)
  791. dev->ethtool_ops->complete(dev);
  792. if (old_features != dev->features)
  793. netdev_features_change(dev);
  794. return rc;
  795. }
  796. EXPORT_SYMBOL(ethtool_op_get_link);
  797. EXPORT_SYMBOL(ethtool_op_get_sg);
  798. EXPORT_SYMBOL(ethtool_op_get_tso);
  799. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  800. EXPORT_SYMBOL(ethtool_op_set_sg);
  801. EXPORT_SYMBOL(ethtool_op_set_tso);
  802. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  803. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
  804. EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
  805. EXPORT_SYMBOL(ethtool_op_set_ufo);
  806. EXPORT_SYMBOL(ethtool_op_get_ufo);
  807. EXPORT_SYMBOL(ethtool_op_set_flags);
  808. EXPORT_SYMBOL(ethtool_op_get_flags);