ethtool.c 26 KB

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