ethtool.c 25 KB

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