ethtool.c 26 KB

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