ethtool.c 23 KB

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