ethtool.c 26 KB

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