ethtool.c 25 KB

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