ethtool.c 26 KB

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