ethtool.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  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 <linux/bitops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/slab.h>
  23. /*
  24. * Some useful ethtool_ops methods that're device independent.
  25. * If we find that all drivers want to do the same thing here,
  26. * we can turn these into dev_() function calls.
  27. */
  28. u32 ethtool_op_get_link(struct net_device *dev)
  29. {
  30. return netif_carrier_ok(dev) ? 1 : 0;
  31. }
  32. EXPORT_SYMBOL(ethtool_op_get_link);
  33. u32 ethtool_op_get_tx_csum(struct net_device *dev)
  34. {
  35. return (dev->features & NETIF_F_ALL_CSUM) != 0;
  36. }
  37. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  38. int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
  39. {
  40. if (data)
  41. dev->features |= NETIF_F_IP_CSUM;
  42. else
  43. dev->features &= ~NETIF_F_IP_CSUM;
  44. return 0;
  45. }
  46. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  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. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
  56. int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
  57. {
  58. if (data)
  59. dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  60. else
  61. dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
  65. u32 ethtool_op_get_sg(struct net_device *dev)
  66. {
  67. return (dev->features & NETIF_F_SG) != 0;
  68. }
  69. EXPORT_SYMBOL(ethtool_op_get_sg);
  70. int ethtool_op_set_sg(struct net_device *dev, u32 data)
  71. {
  72. if (data)
  73. dev->features |= NETIF_F_SG;
  74. else
  75. dev->features &= ~NETIF_F_SG;
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(ethtool_op_set_sg);
  79. u32 ethtool_op_get_tso(struct net_device *dev)
  80. {
  81. return (dev->features & NETIF_F_TSO) != 0;
  82. }
  83. EXPORT_SYMBOL(ethtool_op_get_tso);
  84. int ethtool_op_set_tso(struct net_device *dev, u32 data)
  85. {
  86. if (data)
  87. dev->features |= NETIF_F_TSO;
  88. else
  89. dev->features &= ~NETIF_F_TSO;
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(ethtool_op_set_tso);
  93. u32 ethtool_op_get_ufo(struct net_device *dev)
  94. {
  95. return (dev->features & NETIF_F_UFO) != 0;
  96. }
  97. EXPORT_SYMBOL(ethtool_op_get_ufo);
  98. int ethtool_op_set_ufo(struct net_device *dev, u32 data)
  99. {
  100. if (data)
  101. dev->features |= NETIF_F_UFO;
  102. else
  103. dev->features &= ~NETIF_F_UFO;
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(ethtool_op_set_ufo);
  107. /* the following list of flags are the same as their associated
  108. * NETIF_F_xxx values in include/linux/netdevice.h
  109. */
  110. static const u32 flags_dup_features =
  111. (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
  112. ETH_FLAG_RXHASH);
  113. u32 ethtool_op_get_flags(struct net_device *dev)
  114. {
  115. /* in the future, this function will probably contain additional
  116. * handling for flags which are not so easily handled
  117. * by a simple masking operation
  118. */
  119. return dev->features & flags_dup_features;
  120. }
  121. EXPORT_SYMBOL(ethtool_op_get_flags);
  122. /* Check if device can enable (or disable) particular feature coded in "data"
  123. * argument. Flags "supported" describe features that can be toggled by device.
  124. * If feature can not be toggled, it state (enabled or disabled) must match
  125. * hardcoded device features state, otherwise flags are marked as invalid.
  126. */
  127. bool ethtool_invalid_flags(struct net_device *dev, u32 data, u32 supported)
  128. {
  129. u32 features = dev->features & flags_dup_features;
  130. /* "data" can contain only flags_dup_features bits,
  131. * see __ethtool_set_flags */
  132. return (features & ~supported) != (data & ~supported);
  133. }
  134. EXPORT_SYMBOL(ethtool_invalid_flags);
  135. int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
  136. {
  137. if (ethtool_invalid_flags(dev, data, supported))
  138. return -EINVAL;
  139. dev->features = ((dev->features & ~flags_dup_features) |
  140. (data & flags_dup_features));
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(ethtool_op_set_flags);
  144. void ethtool_ntuple_flush(struct net_device *dev)
  145. {
  146. struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
  147. list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
  148. list_del(&fsc->list);
  149. kfree(fsc);
  150. }
  151. dev->ethtool_ntuple_list.count = 0;
  152. }
  153. EXPORT_SYMBOL(ethtool_ntuple_flush);
  154. /* Handlers for each ethtool command */
  155. #define ETHTOOL_DEV_FEATURE_WORDS 1
  156. static void ethtool_get_features_compat(struct net_device *dev,
  157. struct ethtool_get_features_block *features)
  158. {
  159. if (!dev->ethtool_ops)
  160. return;
  161. /* getting RX checksum */
  162. if (dev->ethtool_ops->get_rx_csum)
  163. if (dev->ethtool_ops->get_rx_csum(dev))
  164. features[0].active |= NETIF_F_RXCSUM;
  165. /* mark legacy-changeable features */
  166. if (dev->ethtool_ops->set_sg)
  167. features[0].available |= NETIF_F_SG;
  168. if (dev->ethtool_ops->set_tx_csum)
  169. features[0].available |= NETIF_F_ALL_CSUM;
  170. if (dev->ethtool_ops->set_tso)
  171. features[0].available |= NETIF_F_ALL_TSO;
  172. if (dev->ethtool_ops->set_rx_csum)
  173. features[0].available |= NETIF_F_RXCSUM;
  174. if (dev->ethtool_ops->set_flags)
  175. features[0].available |= flags_dup_features;
  176. }
  177. static int ethtool_set_feature_compat(struct net_device *dev,
  178. int (*legacy_set)(struct net_device *, u32),
  179. struct ethtool_set_features_block *features, u32 mask)
  180. {
  181. u32 do_set;
  182. if (!legacy_set)
  183. return 0;
  184. if (!(features[0].valid & mask))
  185. return 0;
  186. features[0].valid &= ~mask;
  187. do_set = !!(features[0].requested & mask);
  188. if (legacy_set(dev, do_set) < 0)
  189. netdev_info(dev,
  190. "Legacy feature change (%s) failed for 0x%08x\n",
  191. do_set ? "set" : "clear", mask);
  192. return 1;
  193. }
  194. static int ethtool_set_features_compat(struct net_device *dev,
  195. struct ethtool_set_features_block *features)
  196. {
  197. int compat;
  198. if (!dev->ethtool_ops)
  199. return 0;
  200. compat = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
  201. features, NETIF_F_SG);
  202. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
  203. features, NETIF_F_ALL_CSUM);
  204. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
  205. features, NETIF_F_ALL_TSO);
  206. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
  207. features, NETIF_F_RXCSUM);
  208. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_flags,
  209. features, flags_dup_features);
  210. return compat;
  211. }
  212. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  213. {
  214. struct ethtool_gfeatures cmd = {
  215. .cmd = ETHTOOL_GFEATURES,
  216. .size = ETHTOOL_DEV_FEATURE_WORDS,
  217. };
  218. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
  219. {
  220. .available = dev->hw_features,
  221. .requested = dev->wanted_features,
  222. .active = dev->features,
  223. .never_changed = NETIF_F_NEVER_CHANGE,
  224. },
  225. };
  226. u32 __user *sizeaddr;
  227. u32 copy_size;
  228. ethtool_get_features_compat(dev, features);
  229. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  230. if (get_user(copy_size, sizeaddr))
  231. return -EFAULT;
  232. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  233. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  234. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  235. return -EFAULT;
  236. useraddr += sizeof(cmd);
  237. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  238. return -EFAULT;
  239. return 0;
  240. }
  241. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  242. {
  243. struct ethtool_sfeatures cmd;
  244. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  245. int ret = 0;
  246. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  247. return -EFAULT;
  248. useraddr += sizeof(cmd);
  249. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  250. return -EINVAL;
  251. if (copy_from_user(features, useraddr, sizeof(features)))
  252. return -EFAULT;
  253. if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
  254. return -EINVAL;
  255. if (ethtool_set_features_compat(dev, features))
  256. ret |= ETHTOOL_F_COMPAT;
  257. if (features[0].valid & ~dev->hw_features) {
  258. features[0].valid &= dev->hw_features;
  259. ret |= ETHTOOL_F_UNSUPPORTED;
  260. }
  261. dev->wanted_features &= ~features[0].valid;
  262. dev->wanted_features |= features[0].valid & features[0].requested;
  263. netdev_update_features(dev);
  264. if ((dev->wanted_features ^ dev->features) & features[0].valid)
  265. ret |= ETHTOOL_F_WISH;
  266. return ret;
  267. }
  268. static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
  269. /* NETIF_F_SG */ "tx-scatter-gather",
  270. /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
  271. /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
  272. /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
  273. /* NETIF_F_IPV6_CSUM */ "tx_checksum-ipv6",
  274. /* NETIF_F_HIGHDMA */ "highdma",
  275. /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
  276. /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
  277. /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
  278. /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
  279. /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
  280. /* NETIF_F_GSO */ "tx-generic-segmentation",
  281. /* NETIF_F_LLTX */ "tx-lockless",
  282. /* NETIF_F_NETNS_LOCAL */ "netns-local",
  283. /* NETIF_F_GRO */ "rx-gro",
  284. /* NETIF_F_LRO */ "rx-lro",
  285. /* NETIF_F_TSO */ "tx-tcp-segmentation",
  286. /* NETIF_F_UFO */ "tx-udp-fragmentation",
  287. /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
  288. /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
  289. /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
  290. /* NETIF_F_FSO */ "tx-fcoe-segmentation",
  291. "",
  292. "",
  293. /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
  294. /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
  295. /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
  296. /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
  297. /* NETIF_F_RXHASH */ "rx-hashing",
  298. /* NETIF_F_RXCSUM */ "rx-checksum",
  299. "",
  300. "",
  301. };
  302. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  303. {
  304. const struct ethtool_ops *ops = dev->ethtool_ops;
  305. if (sset == ETH_SS_FEATURES)
  306. return ARRAY_SIZE(netdev_features_strings);
  307. if (ops && ops->get_sset_count && ops->get_strings)
  308. return ops->get_sset_count(dev, sset);
  309. else
  310. return -EOPNOTSUPP;
  311. }
  312. static void __ethtool_get_strings(struct net_device *dev,
  313. u32 stringset, u8 *data)
  314. {
  315. const struct ethtool_ops *ops = dev->ethtool_ops;
  316. if (stringset == ETH_SS_FEATURES)
  317. memcpy(data, netdev_features_strings,
  318. sizeof(netdev_features_strings));
  319. else
  320. /* ops->get_strings is valid because checked earlier */
  321. ops->get_strings(dev, stringset, data);
  322. }
  323. static u32 ethtool_get_feature_mask(u32 eth_cmd)
  324. {
  325. /* feature masks of legacy discrete ethtool ops */
  326. switch (eth_cmd) {
  327. case ETHTOOL_GTXCSUM:
  328. case ETHTOOL_STXCSUM:
  329. return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
  330. case ETHTOOL_GRXCSUM:
  331. case ETHTOOL_SRXCSUM:
  332. return NETIF_F_RXCSUM;
  333. case ETHTOOL_GSG:
  334. case ETHTOOL_SSG:
  335. return NETIF_F_SG;
  336. case ETHTOOL_GTSO:
  337. case ETHTOOL_STSO:
  338. return NETIF_F_ALL_TSO;
  339. case ETHTOOL_GUFO:
  340. case ETHTOOL_SUFO:
  341. return NETIF_F_UFO;
  342. case ETHTOOL_GGSO:
  343. case ETHTOOL_SGSO:
  344. return NETIF_F_GSO;
  345. case ETHTOOL_GGRO:
  346. case ETHTOOL_SGRO:
  347. return NETIF_F_GRO;
  348. default:
  349. BUG();
  350. }
  351. }
  352. static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
  353. {
  354. const struct ethtool_ops *ops = dev->ethtool_ops;
  355. if (!ops)
  356. return NULL;
  357. switch (ethcmd) {
  358. case ETHTOOL_GTXCSUM:
  359. return ops->get_tx_csum;
  360. case ETHTOOL_GRXCSUM:
  361. return ops->get_rx_csum;
  362. case ETHTOOL_SSG:
  363. return ops->get_sg;
  364. case ETHTOOL_STSO:
  365. return ops->get_tso;
  366. case ETHTOOL_SUFO:
  367. return ops->get_ufo;
  368. default:
  369. return NULL;
  370. }
  371. }
  372. static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
  373. {
  374. return !!(dev->features & NETIF_F_ALL_CSUM);
  375. }
  376. static int ethtool_get_one_feature(struct net_device *dev,
  377. char __user *useraddr, u32 ethcmd)
  378. {
  379. u32 mask = ethtool_get_feature_mask(ethcmd);
  380. struct ethtool_value edata = {
  381. .cmd = ethcmd,
  382. .data = !!(dev->features & mask),
  383. };
  384. /* compatibility with discrete get_ ops */
  385. if (!(dev->hw_features & mask)) {
  386. u32 (*actor)(struct net_device *);
  387. actor = __ethtool_get_one_feature_actor(dev, ethcmd);
  388. /* bug compatibility with old get_rx_csum */
  389. if (ethcmd == ETHTOOL_GRXCSUM && !actor)
  390. actor = __ethtool_get_rx_csum_oldbug;
  391. if (actor)
  392. edata.data = actor(dev);
  393. }
  394. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  395. return -EFAULT;
  396. return 0;
  397. }
  398. static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
  399. static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
  400. static int __ethtool_set_sg(struct net_device *dev, u32 data);
  401. static int __ethtool_set_tso(struct net_device *dev, u32 data);
  402. static int __ethtool_set_ufo(struct net_device *dev, u32 data);
  403. static int ethtool_set_one_feature(struct net_device *dev,
  404. void __user *useraddr, u32 ethcmd)
  405. {
  406. struct ethtool_value edata;
  407. u32 mask;
  408. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  409. return -EFAULT;
  410. mask = ethtool_get_feature_mask(ethcmd);
  411. mask &= dev->hw_features;
  412. if (mask) {
  413. if (edata.data)
  414. dev->wanted_features |= mask;
  415. else
  416. dev->wanted_features &= ~mask;
  417. netdev_update_features(dev);
  418. return 0;
  419. }
  420. /* Driver is not converted to ndo_fix_features or does not
  421. * support changing this offload. In the latter case it won't
  422. * have corresponding ethtool_ops field set.
  423. *
  424. * Following part is to be removed after all drivers advertise
  425. * their changeable features in netdev->hw_features and stop
  426. * using discrete offload setting ops.
  427. */
  428. switch (ethcmd) {
  429. case ETHTOOL_STXCSUM:
  430. return __ethtool_set_tx_csum(dev, edata.data);
  431. case ETHTOOL_SRXCSUM:
  432. return __ethtool_set_rx_csum(dev, edata.data);
  433. case ETHTOOL_SSG:
  434. return __ethtool_set_sg(dev, edata.data);
  435. case ETHTOOL_STSO:
  436. return __ethtool_set_tso(dev, edata.data);
  437. case ETHTOOL_SUFO:
  438. return __ethtool_set_ufo(dev, edata.data);
  439. default:
  440. return -EOPNOTSUPP;
  441. }
  442. }
  443. int __ethtool_set_flags(struct net_device *dev, u32 data)
  444. {
  445. u32 changed;
  446. if (data & ~flags_dup_features)
  447. return -EINVAL;
  448. /* legacy set_flags() op */
  449. if (dev->ethtool_ops->set_flags) {
  450. if (unlikely(dev->hw_features & flags_dup_features))
  451. netdev_warn(dev,
  452. "driver BUG: mixed hw_features and set_flags()\n");
  453. return dev->ethtool_ops->set_flags(dev, data);
  454. }
  455. /* allow changing only bits set in hw_features */
  456. changed = (data ^ dev->wanted_features) & flags_dup_features;
  457. if (changed & ~dev->hw_features)
  458. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  459. dev->wanted_features =
  460. (dev->wanted_features & ~changed) | data;
  461. netdev_update_features(dev);
  462. return 0;
  463. }
  464. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  465. {
  466. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  467. int err;
  468. if (!dev->ethtool_ops->get_settings)
  469. return -EOPNOTSUPP;
  470. err = dev->ethtool_ops->get_settings(dev, &cmd);
  471. if (err < 0)
  472. return err;
  473. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  474. return -EFAULT;
  475. return 0;
  476. }
  477. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  478. {
  479. struct ethtool_cmd cmd;
  480. if (!dev->ethtool_ops->set_settings)
  481. return -EOPNOTSUPP;
  482. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  483. return -EFAULT;
  484. return dev->ethtool_ops->set_settings(dev, &cmd);
  485. }
  486. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  487. void __user *useraddr)
  488. {
  489. struct ethtool_drvinfo info;
  490. const struct ethtool_ops *ops = dev->ethtool_ops;
  491. memset(&info, 0, sizeof(info));
  492. info.cmd = ETHTOOL_GDRVINFO;
  493. if (ops && ops->get_drvinfo) {
  494. ops->get_drvinfo(dev, &info);
  495. } else if (dev->dev.parent && dev->dev.parent->driver) {
  496. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  497. sizeof(info.bus_info));
  498. strlcpy(info.driver, dev->dev.parent->driver->name,
  499. sizeof(info.driver));
  500. } else {
  501. return -EOPNOTSUPP;
  502. }
  503. /*
  504. * this method of obtaining string set info is deprecated;
  505. * Use ETHTOOL_GSSET_INFO instead.
  506. */
  507. if (ops && ops->get_sset_count) {
  508. int rc;
  509. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  510. if (rc >= 0)
  511. info.testinfo_len = rc;
  512. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  513. if (rc >= 0)
  514. info.n_stats = rc;
  515. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  516. if (rc >= 0)
  517. info.n_priv_flags = rc;
  518. }
  519. if (ops && ops->get_regs_len)
  520. info.regdump_len = ops->get_regs_len(dev);
  521. if (ops && ops->get_eeprom_len)
  522. info.eedump_len = ops->get_eeprom_len(dev);
  523. if (copy_to_user(useraddr, &info, sizeof(info)))
  524. return -EFAULT;
  525. return 0;
  526. }
  527. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  528. void __user *useraddr)
  529. {
  530. struct ethtool_sset_info info;
  531. u64 sset_mask;
  532. int i, idx = 0, n_bits = 0, ret, rc;
  533. u32 *info_buf = NULL;
  534. if (copy_from_user(&info, useraddr, sizeof(info)))
  535. return -EFAULT;
  536. /* store copy of mask, because we zero struct later on */
  537. sset_mask = info.sset_mask;
  538. if (!sset_mask)
  539. return 0;
  540. /* calculate size of return buffer */
  541. n_bits = hweight64(sset_mask);
  542. memset(&info, 0, sizeof(info));
  543. info.cmd = ETHTOOL_GSSET_INFO;
  544. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  545. if (!info_buf)
  546. return -ENOMEM;
  547. /*
  548. * fill return buffer based on input bitmask and successful
  549. * get_sset_count return
  550. */
  551. for (i = 0; i < 64; i++) {
  552. if (!(sset_mask & (1ULL << i)))
  553. continue;
  554. rc = __ethtool_get_sset_count(dev, i);
  555. if (rc >= 0) {
  556. info.sset_mask |= (1ULL << i);
  557. info_buf[idx++] = rc;
  558. }
  559. }
  560. ret = -EFAULT;
  561. if (copy_to_user(useraddr, &info, sizeof(info)))
  562. goto out;
  563. useraddr += offsetof(struct ethtool_sset_info, data);
  564. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  565. goto out;
  566. ret = 0;
  567. out:
  568. kfree(info_buf);
  569. return ret;
  570. }
  571. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  572. u32 cmd, void __user *useraddr)
  573. {
  574. struct ethtool_rxnfc info;
  575. size_t info_size = sizeof(info);
  576. if (!dev->ethtool_ops->set_rxnfc)
  577. return -EOPNOTSUPP;
  578. /* struct ethtool_rxnfc was originally defined for
  579. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  580. * members. User-space might still be using that
  581. * definition. */
  582. if (cmd == ETHTOOL_SRXFH)
  583. info_size = (offsetof(struct ethtool_rxnfc, data) +
  584. sizeof(info.data));
  585. if (copy_from_user(&info, useraddr, info_size))
  586. return -EFAULT;
  587. return dev->ethtool_ops->set_rxnfc(dev, &info);
  588. }
  589. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  590. u32 cmd, void __user *useraddr)
  591. {
  592. struct ethtool_rxnfc info;
  593. size_t info_size = sizeof(info);
  594. const struct ethtool_ops *ops = dev->ethtool_ops;
  595. int ret;
  596. void *rule_buf = NULL;
  597. if (!ops->get_rxnfc)
  598. return -EOPNOTSUPP;
  599. /* struct ethtool_rxnfc was originally defined for
  600. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  601. * members. User-space might still be using that
  602. * definition. */
  603. if (cmd == ETHTOOL_GRXFH)
  604. info_size = (offsetof(struct ethtool_rxnfc, data) +
  605. sizeof(info.data));
  606. if (copy_from_user(&info, useraddr, info_size))
  607. return -EFAULT;
  608. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  609. if (info.rule_cnt > 0) {
  610. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  611. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  612. GFP_USER);
  613. if (!rule_buf)
  614. return -ENOMEM;
  615. }
  616. }
  617. ret = ops->get_rxnfc(dev, &info, rule_buf);
  618. if (ret < 0)
  619. goto err_out;
  620. ret = -EFAULT;
  621. if (copy_to_user(useraddr, &info, info_size))
  622. goto err_out;
  623. if (rule_buf) {
  624. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  625. if (copy_to_user(useraddr, rule_buf,
  626. info.rule_cnt * sizeof(u32)))
  627. goto err_out;
  628. }
  629. ret = 0;
  630. err_out:
  631. kfree(rule_buf);
  632. return ret;
  633. }
  634. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  635. void __user *useraddr)
  636. {
  637. struct ethtool_rxfh_indir *indir;
  638. u32 table_size;
  639. size_t full_size;
  640. int ret;
  641. if (!dev->ethtool_ops->get_rxfh_indir)
  642. return -EOPNOTSUPP;
  643. if (copy_from_user(&table_size,
  644. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  645. sizeof(table_size)))
  646. return -EFAULT;
  647. if (table_size >
  648. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  649. return -ENOMEM;
  650. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  651. indir = kzalloc(full_size, GFP_USER);
  652. if (!indir)
  653. return -ENOMEM;
  654. indir->cmd = ETHTOOL_GRXFHINDIR;
  655. indir->size = table_size;
  656. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  657. if (ret)
  658. goto out;
  659. if (copy_to_user(useraddr, indir, full_size))
  660. ret = -EFAULT;
  661. out:
  662. kfree(indir);
  663. return ret;
  664. }
  665. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  666. void __user *useraddr)
  667. {
  668. struct ethtool_rxfh_indir *indir;
  669. u32 table_size;
  670. size_t full_size;
  671. int ret;
  672. if (!dev->ethtool_ops->set_rxfh_indir)
  673. return -EOPNOTSUPP;
  674. if (copy_from_user(&table_size,
  675. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  676. sizeof(table_size)))
  677. return -EFAULT;
  678. if (table_size >
  679. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  680. return -ENOMEM;
  681. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  682. indir = kmalloc(full_size, GFP_USER);
  683. if (!indir)
  684. return -ENOMEM;
  685. if (copy_from_user(indir, useraddr, full_size)) {
  686. ret = -EFAULT;
  687. goto out;
  688. }
  689. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  690. out:
  691. kfree(indir);
  692. return ret;
  693. }
  694. static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
  695. struct ethtool_rx_ntuple_flow_spec *spec,
  696. struct ethtool_rx_ntuple_flow_spec_container *fsc)
  697. {
  698. /* don't add filters forever */
  699. if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
  700. /* free the container */
  701. kfree(fsc);
  702. return;
  703. }
  704. /* Copy the whole filter over */
  705. fsc->fs.flow_type = spec->flow_type;
  706. memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
  707. memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
  708. fsc->fs.vlan_tag = spec->vlan_tag;
  709. fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
  710. fsc->fs.data = spec->data;
  711. fsc->fs.data_mask = spec->data_mask;
  712. fsc->fs.action = spec->action;
  713. /* add to the list */
  714. list_add_tail_rcu(&fsc->list, &list->list);
  715. list->count++;
  716. }
  717. /*
  718. * ethtool does not (or did not) set masks for flow parameters that are
  719. * not specified, so if both value and mask are 0 then this must be
  720. * treated as equivalent to a mask with all bits set. Implement that
  721. * here rather than in drivers.
  722. */
  723. static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
  724. {
  725. struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
  726. struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
  727. if (fs->flow_type != TCP_V4_FLOW &&
  728. fs->flow_type != UDP_V4_FLOW &&
  729. fs->flow_type != SCTP_V4_FLOW)
  730. return;
  731. if (!(entry->ip4src | mask->ip4src))
  732. mask->ip4src = htonl(0xffffffff);
  733. if (!(entry->ip4dst | mask->ip4dst))
  734. mask->ip4dst = htonl(0xffffffff);
  735. if (!(entry->psrc | mask->psrc))
  736. mask->psrc = htons(0xffff);
  737. if (!(entry->pdst | mask->pdst))
  738. mask->pdst = htons(0xffff);
  739. if (!(entry->tos | mask->tos))
  740. mask->tos = 0xff;
  741. if (!(fs->vlan_tag | fs->vlan_tag_mask))
  742. fs->vlan_tag_mask = 0xffff;
  743. if (!(fs->data | fs->data_mask))
  744. fs->data_mask = 0xffffffffffffffffULL;
  745. }
  746. static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
  747. void __user *useraddr)
  748. {
  749. struct ethtool_rx_ntuple cmd;
  750. const struct ethtool_ops *ops = dev->ethtool_ops;
  751. struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
  752. int ret;
  753. if (!(dev->features & NETIF_F_NTUPLE))
  754. return -EINVAL;
  755. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  756. return -EFAULT;
  757. rx_ntuple_fix_masks(&cmd.fs);
  758. /*
  759. * Cache filter in dev struct for GET operation only if
  760. * the underlying driver doesn't have its own GET operation, and
  761. * only if the filter was added successfully. First make sure we
  762. * can allocate the filter, then continue if successful.
  763. */
  764. if (!ops->get_rx_ntuple) {
  765. fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
  766. if (!fsc)
  767. return -ENOMEM;
  768. }
  769. ret = ops->set_rx_ntuple(dev, &cmd);
  770. if (ret) {
  771. kfree(fsc);
  772. return ret;
  773. }
  774. if (!ops->get_rx_ntuple)
  775. __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
  776. return ret;
  777. }
  778. static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
  779. {
  780. struct ethtool_gstrings gstrings;
  781. const struct ethtool_ops *ops = dev->ethtool_ops;
  782. struct ethtool_rx_ntuple_flow_spec_container *fsc;
  783. u8 *data;
  784. char *p;
  785. int ret, i, num_strings = 0;
  786. if (!ops->get_sset_count)
  787. return -EOPNOTSUPP;
  788. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  789. return -EFAULT;
  790. ret = ops->get_sset_count(dev, gstrings.string_set);
  791. if (ret < 0)
  792. return ret;
  793. gstrings.len = ret;
  794. data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  795. if (!data)
  796. return -ENOMEM;
  797. if (ops->get_rx_ntuple) {
  798. /* driver-specific filter grab */
  799. ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
  800. goto copy;
  801. }
  802. /* default ethtool filter grab */
  803. i = 0;
  804. p = (char *)data;
  805. list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
  806. sprintf(p, "Filter %d:\n", i);
  807. p += ETH_GSTRING_LEN;
  808. num_strings++;
  809. switch (fsc->fs.flow_type) {
  810. case TCP_V4_FLOW:
  811. sprintf(p, "\tFlow Type: TCP\n");
  812. p += ETH_GSTRING_LEN;
  813. num_strings++;
  814. break;
  815. case UDP_V4_FLOW:
  816. sprintf(p, "\tFlow Type: UDP\n");
  817. p += ETH_GSTRING_LEN;
  818. num_strings++;
  819. break;
  820. case SCTP_V4_FLOW:
  821. sprintf(p, "\tFlow Type: SCTP\n");
  822. p += ETH_GSTRING_LEN;
  823. num_strings++;
  824. break;
  825. case AH_ESP_V4_FLOW:
  826. sprintf(p, "\tFlow Type: AH ESP\n");
  827. p += ETH_GSTRING_LEN;
  828. num_strings++;
  829. break;
  830. case ESP_V4_FLOW:
  831. sprintf(p, "\tFlow Type: ESP\n");
  832. p += ETH_GSTRING_LEN;
  833. num_strings++;
  834. break;
  835. case IP_USER_FLOW:
  836. sprintf(p, "\tFlow Type: Raw IP\n");
  837. p += ETH_GSTRING_LEN;
  838. num_strings++;
  839. break;
  840. case IPV4_FLOW:
  841. sprintf(p, "\tFlow Type: IPv4\n");
  842. p += ETH_GSTRING_LEN;
  843. num_strings++;
  844. break;
  845. default:
  846. sprintf(p, "\tFlow Type: Unknown\n");
  847. p += ETH_GSTRING_LEN;
  848. num_strings++;
  849. goto unknown_filter;
  850. }
  851. /* now the rest of the filters */
  852. switch (fsc->fs.flow_type) {
  853. case TCP_V4_FLOW:
  854. case UDP_V4_FLOW:
  855. case SCTP_V4_FLOW:
  856. sprintf(p, "\tSrc IP addr: 0x%x\n",
  857. fsc->fs.h_u.tcp_ip4_spec.ip4src);
  858. p += ETH_GSTRING_LEN;
  859. num_strings++;
  860. sprintf(p, "\tSrc IP mask: 0x%x\n",
  861. fsc->fs.m_u.tcp_ip4_spec.ip4src);
  862. p += ETH_GSTRING_LEN;
  863. num_strings++;
  864. sprintf(p, "\tDest IP addr: 0x%x\n",
  865. fsc->fs.h_u.tcp_ip4_spec.ip4dst);
  866. p += ETH_GSTRING_LEN;
  867. num_strings++;
  868. sprintf(p, "\tDest IP mask: 0x%x\n",
  869. fsc->fs.m_u.tcp_ip4_spec.ip4dst);
  870. p += ETH_GSTRING_LEN;
  871. num_strings++;
  872. sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
  873. fsc->fs.h_u.tcp_ip4_spec.psrc,
  874. fsc->fs.m_u.tcp_ip4_spec.psrc);
  875. p += ETH_GSTRING_LEN;
  876. num_strings++;
  877. sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
  878. fsc->fs.h_u.tcp_ip4_spec.pdst,
  879. fsc->fs.m_u.tcp_ip4_spec.pdst);
  880. p += ETH_GSTRING_LEN;
  881. num_strings++;
  882. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  883. fsc->fs.h_u.tcp_ip4_spec.tos,
  884. fsc->fs.m_u.tcp_ip4_spec.tos);
  885. p += ETH_GSTRING_LEN;
  886. num_strings++;
  887. break;
  888. case AH_ESP_V4_FLOW:
  889. case ESP_V4_FLOW:
  890. sprintf(p, "\tSrc IP addr: 0x%x\n",
  891. fsc->fs.h_u.ah_ip4_spec.ip4src);
  892. p += ETH_GSTRING_LEN;
  893. num_strings++;
  894. sprintf(p, "\tSrc IP mask: 0x%x\n",
  895. fsc->fs.m_u.ah_ip4_spec.ip4src);
  896. p += ETH_GSTRING_LEN;
  897. num_strings++;
  898. sprintf(p, "\tDest IP addr: 0x%x\n",
  899. fsc->fs.h_u.ah_ip4_spec.ip4dst);
  900. p += ETH_GSTRING_LEN;
  901. num_strings++;
  902. sprintf(p, "\tDest IP mask: 0x%x\n",
  903. fsc->fs.m_u.ah_ip4_spec.ip4dst);
  904. p += ETH_GSTRING_LEN;
  905. num_strings++;
  906. sprintf(p, "\tSPI: %d, mask: 0x%x\n",
  907. fsc->fs.h_u.ah_ip4_spec.spi,
  908. fsc->fs.m_u.ah_ip4_spec.spi);
  909. p += ETH_GSTRING_LEN;
  910. num_strings++;
  911. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  912. fsc->fs.h_u.ah_ip4_spec.tos,
  913. fsc->fs.m_u.ah_ip4_spec.tos);
  914. p += ETH_GSTRING_LEN;
  915. num_strings++;
  916. break;
  917. case IP_USER_FLOW:
  918. sprintf(p, "\tSrc IP addr: 0x%x\n",
  919. fsc->fs.h_u.usr_ip4_spec.ip4src);
  920. p += ETH_GSTRING_LEN;
  921. num_strings++;
  922. sprintf(p, "\tSrc IP mask: 0x%x\n",
  923. fsc->fs.m_u.usr_ip4_spec.ip4src);
  924. p += ETH_GSTRING_LEN;
  925. num_strings++;
  926. sprintf(p, "\tDest IP addr: 0x%x\n",
  927. fsc->fs.h_u.usr_ip4_spec.ip4dst);
  928. p += ETH_GSTRING_LEN;
  929. num_strings++;
  930. sprintf(p, "\tDest IP mask: 0x%x\n",
  931. fsc->fs.m_u.usr_ip4_spec.ip4dst);
  932. p += ETH_GSTRING_LEN;
  933. num_strings++;
  934. break;
  935. case IPV4_FLOW:
  936. sprintf(p, "\tSrc IP addr: 0x%x\n",
  937. fsc->fs.h_u.usr_ip4_spec.ip4src);
  938. p += ETH_GSTRING_LEN;
  939. num_strings++;
  940. sprintf(p, "\tSrc IP mask: 0x%x\n",
  941. fsc->fs.m_u.usr_ip4_spec.ip4src);
  942. p += ETH_GSTRING_LEN;
  943. num_strings++;
  944. sprintf(p, "\tDest IP addr: 0x%x\n",
  945. fsc->fs.h_u.usr_ip4_spec.ip4dst);
  946. p += ETH_GSTRING_LEN;
  947. num_strings++;
  948. sprintf(p, "\tDest IP mask: 0x%x\n",
  949. fsc->fs.m_u.usr_ip4_spec.ip4dst);
  950. p += ETH_GSTRING_LEN;
  951. num_strings++;
  952. sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
  953. fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
  954. fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
  955. p += ETH_GSTRING_LEN;
  956. num_strings++;
  957. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  958. fsc->fs.h_u.usr_ip4_spec.tos,
  959. fsc->fs.m_u.usr_ip4_spec.tos);
  960. p += ETH_GSTRING_LEN;
  961. num_strings++;
  962. sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
  963. fsc->fs.h_u.usr_ip4_spec.ip_ver,
  964. fsc->fs.m_u.usr_ip4_spec.ip_ver);
  965. p += ETH_GSTRING_LEN;
  966. num_strings++;
  967. sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
  968. fsc->fs.h_u.usr_ip4_spec.proto,
  969. fsc->fs.m_u.usr_ip4_spec.proto);
  970. p += ETH_GSTRING_LEN;
  971. num_strings++;
  972. break;
  973. }
  974. sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
  975. fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
  976. p += ETH_GSTRING_LEN;
  977. num_strings++;
  978. sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
  979. p += ETH_GSTRING_LEN;
  980. num_strings++;
  981. sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
  982. p += ETH_GSTRING_LEN;
  983. num_strings++;
  984. if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
  985. sprintf(p, "\tAction: Drop\n");
  986. else
  987. sprintf(p, "\tAction: Direct to queue %d\n",
  988. fsc->fs.action);
  989. p += ETH_GSTRING_LEN;
  990. num_strings++;
  991. unknown_filter:
  992. i++;
  993. }
  994. copy:
  995. /* indicate to userspace how many strings we actually have */
  996. gstrings.len = num_strings;
  997. ret = -EFAULT;
  998. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  999. goto out;
  1000. useraddr += sizeof(gstrings);
  1001. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1002. goto out;
  1003. ret = 0;
  1004. out:
  1005. kfree(data);
  1006. return ret;
  1007. }
  1008. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1009. {
  1010. struct ethtool_regs regs;
  1011. const struct ethtool_ops *ops = dev->ethtool_ops;
  1012. void *regbuf;
  1013. int reglen, ret;
  1014. if (!ops->get_regs || !ops->get_regs_len)
  1015. return -EOPNOTSUPP;
  1016. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1017. return -EFAULT;
  1018. reglen = ops->get_regs_len(dev);
  1019. if (regs.len > reglen)
  1020. regs.len = reglen;
  1021. regbuf = vzalloc(reglen);
  1022. if (!regbuf)
  1023. return -ENOMEM;
  1024. ops->get_regs(dev, &regs, regbuf);
  1025. ret = -EFAULT;
  1026. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1027. goto out;
  1028. useraddr += offsetof(struct ethtool_regs, data);
  1029. if (copy_to_user(useraddr, regbuf, regs.len))
  1030. goto out;
  1031. ret = 0;
  1032. out:
  1033. vfree(regbuf);
  1034. return ret;
  1035. }
  1036. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1037. {
  1038. struct ethtool_value reset;
  1039. int ret;
  1040. if (!dev->ethtool_ops->reset)
  1041. return -EOPNOTSUPP;
  1042. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1043. return -EFAULT;
  1044. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1045. if (ret)
  1046. return ret;
  1047. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1048. return -EFAULT;
  1049. return 0;
  1050. }
  1051. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1052. {
  1053. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1054. if (!dev->ethtool_ops->get_wol)
  1055. return -EOPNOTSUPP;
  1056. dev->ethtool_ops->get_wol(dev, &wol);
  1057. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1058. return -EFAULT;
  1059. return 0;
  1060. }
  1061. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1062. {
  1063. struct ethtool_wolinfo wol;
  1064. if (!dev->ethtool_ops->set_wol)
  1065. return -EOPNOTSUPP;
  1066. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1067. return -EFAULT;
  1068. return dev->ethtool_ops->set_wol(dev, &wol);
  1069. }
  1070. static int ethtool_nway_reset(struct net_device *dev)
  1071. {
  1072. if (!dev->ethtool_ops->nway_reset)
  1073. return -EOPNOTSUPP;
  1074. return dev->ethtool_ops->nway_reset(dev);
  1075. }
  1076. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1077. {
  1078. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1079. if (!dev->ethtool_ops->get_link)
  1080. return -EOPNOTSUPP;
  1081. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  1082. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1083. return -EFAULT;
  1084. return 0;
  1085. }
  1086. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1087. {
  1088. struct ethtool_eeprom eeprom;
  1089. const struct ethtool_ops *ops = dev->ethtool_ops;
  1090. void __user *userbuf = useraddr + sizeof(eeprom);
  1091. u32 bytes_remaining;
  1092. u8 *data;
  1093. int ret = 0;
  1094. if (!ops->get_eeprom || !ops->get_eeprom_len)
  1095. return -EOPNOTSUPP;
  1096. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1097. return -EFAULT;
  1098. /* Check for wrap and zero */
  1099. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1100. return -EINVAL;
  1101. /* Check for exceeding total eeprom len */
  1102. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1103. return -EINVAL;
  1104. data = kmalloc(PAGE_SIZE, GFP_USER);
  1105. if (!data)
  1106. return -ENOMEM;
  1107. bytes_remaining = eeprom.len;
  1108. while (bytes_remaining > 0) {
  1109. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1110. ret = ops->get_eeprom(dev, &eeprom, data);
  1111. if (ret)
  1112. break;
  1113. if (copy_to_user(userbuf, data, eeprom.len)) {
  1114. ret = -EFAULT;
  1115. break;
  1116. }
  1117. userbuf += eeprom.len;
  1118. eeprom.offset += eeprom.len;
  1119. bytes_remaining -= eeprom.len;
  1120. }
  1121. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1122. eeprom.offset -= eeprom.len;
  1123. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1124. ret = -EFAULT;
  1125. kfree(data);
  1126. return ret;
  1127. }
  1128. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1129. {
  1130. struct ethtool_eeprom eeprom;
  1131. const struct ethtool_ops *ops = dev->ethtool_ops;
  1132. void __user *userbuf = useraddr + sizeof(eeprom);
  1133. u32 bytes_remaining;
  1134. u8 *data;
  1135. int ret = 0;
  1136. if (!ops->set_eeprom || !ops->get_eeprom_len)
  1137. return -EOPNOTSUPP;
  1138. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1139. return -EFAULT;
  1140. /* Check for wrap and zero */
  1141. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1142. return -EINVAL;
  1143. /* Check for exceeding total eeprom len */
  1144. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1145. return -EINVAL;
  1146. data = kmalloc(PAGE_SIZE, GFP_USER);
  1147. if (!data)
  1148. return -ENOMEM;
  1149. bytes_remaining = eeprom.len;
  1150. while (bytes_remaining > 0) {
  1151. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1152. if (copy_from_user(data, userbuf, eeprom.len)) {
  1153. ret = -EFAULT;
  1154. break;
  1155. }
  1156. ret = ops->set_eeprom(dev, &eeprom, data);
  1157. if (ret)
  1158. break;
  1159. userbuf += eeprom.len;
  1160. eeprom.offset += eeprom.len;
  1161. bytes_remaining -= eeprom.len;
  1162. }
  1163. kfree(data);
  1164. return ret;
  1165. }
  1166. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1167. void __user *useraddr)
  1168. {
  1169. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1170. if (!dev->ethtool_ops->get_coalesce)
  1171. return -EOPNOTSUPP;
  1172. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  1173. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1174. return -EFAULT;
  1175. return 0;
  1176. }
  1177. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1178. void __user *useraddr)
  1179. {
  1180. struct ethtool_coalesce coalesce;
  1181. if (!dev->ethtool_ops->set_coalesce)
  1182. return -EOPNOTSUPP;
  1183. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1184. return -EFAULT;
  1185. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  1186. }
  1187. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1188. {
  1189. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1190. if (!dev->ethtool_ops->get_ringparam)
  1191. return -EOPNOTSUPP;
  1192. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  1193. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1194. return -EFAULT;
  1195. return 0;
  1196. }
  1197. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1198. {
  1199. struct ethtool_ringparam ringparam;
  1200. if (!dev->ethtool_ops->set_ringparam)
  1201. return -EOPNOTSUPP;
  1202. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1203. return -EFAULT;
  1204. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  1205. }
  1206. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1207. {
  1208. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  1209. if (!dev->ethtool_ops->get_pauseparam)
  1210. return -EOPNOTSUPP;
  1211. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1212. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1213. return -EFAULT;
  1214. return 0;
  1215. }
  1216. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1217. {
  1218. struct ethtool_pauseparam pauseparam;
  1219. if (!dev->ethtool_ops->set_pauseparam)
  1220. return -EOPNOTSUPP;
  1221. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1222. return -EFAULT;
  1223. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1224. }
  1225. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  1226. {
  1227. int err;
  1228. if (!dev->ethtool_ops->set_sg)
  1229. return -EOPNOTSUPP;
  1230. if (data && !(dev->features & NETIF_F_ALL_CSUM))
  1231. return -EINVAL;
  1232. if (!data && dev->ethtool_ops->set_tso) {
  1233. err = dev->ethtool_ops->set_tso(dev, 0);
  1234. if (err)
  1235. return err;
  1236. }
  1237. if (!data && dev->ethtool_ops->set_ufo) {
  1238. err = dev->ethtool_ops->set_ufo(dev, 0);
  1239. if (err)
  1240. return err;
  1241. }
  1242. return dev->ethtool_ops->set_sg(dev, data);
  1243. }
  1244. static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
  1245. {
  1246. int err;
  1247. if (!dev->ethtool_ops->set_tx_csum)
  1248. return -EOPNOTSUPP;
  1249. if (!data && dev->ethtool_ops->set_sg) {
  1250. err = __ethtool_set_sg(dev, 0);
  1251. if (err)
  1252. return err;
  1253. }
  1254. return dev->ethtool_ops->set_tx_csum(dev, data);
  1255. }
  1256. static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
  1257. {
  1258. if (!dev->ethtool_ops->set_rx_csum)
  1259. return -EOPNOTSUPP;
  1260. if (!data)
  1261. dev->features &= ~NETIF_F_GRO;
  1262. return dev->ethtool_ops->set_rx_csum(dev, data);
  1263. }
  1264. static int __ethtool_set_tso(struct net_device *dev, u32 data)
  1265. {
  1266. if (!dev->ethtool_ops->set_tso)
  1267. return -EOPNOTSUPP;
  1268. if (data && !(dev->features & NETIF_F_SG))
  1269. return -EINVAL;
  1270. return dev->ethtool_ops->set_tso(dev, data);
  1271. }
  1272. static int __ethtool_set_ufo(struct net_device *dev, u32 data)
  1273. {
  1274. if (!dev->ethtool_ops->set_ufo)
  1275. return -EOPNOTSUPP;
  1276. if (data && !(dev->features & NETIF_F_SG))
  1277. return -EINVAL;
  1278. if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
  1279. (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
  1280. == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
  1281. return -EINVAL;
  1282. return dev->ethtool_ops->set_ufo(dev, data);
  1283. }
  1284. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1285. {
  1286. struct ethtool_test test;
  1287. const struct ethtool_ops *ops = dev->ethtool_ops;
  1288. u64 *data;
  1289. int ret, test_len;
  1290. if (!ops->self_test || !ops->get_sset_count)
  1291. return -EOPNOTSUPP;
  1292. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1293. if (test_len < 0)
  1294. return test_len;
  1295. WARN_ON(test_len == 0);
  1296. if (copy_from_user(&test, useraddr, sizeof(test)))
  1297. return -EFAULT;
  1298. test.len = test_len;
  1299. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  1300. if (!data)
  1301. return -ENOMEM;
  1302. ops->self_test(dev, &test, data);
  1303. ret = -EFAULT;
  1304. if (copy_to_user(useraddr, &test, sizeof(test)))
  1305. goto out;
  1306. useraddr += sizeof(test);
  1307. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  1308. goto out;
  1309. ret = 0;
  1310. out:
  1311. kfree(data);
  1312. return ret;
  1313. }
  1314. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1315. {
  1316. struct ethtool_gstrings gstrings;
  1317. u8 *data;
  1318. int ret;
  1319. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1320. return -EFAULT;
  1321. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1322. if (ret < 0)
  1323. return ret;
  1324. gstrings.len = ret;
  1325. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  1326. if (!data)
  1327. return -ENOMEM;
  1328. __ethtool_get_strings(dev, gstrings.string_set, data);
  1329. ret = -EFAULT;
  1330. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1331. goto out;
  1332. useraddr += sizeof(gstrings);
  1333. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1334. goto out;
  1335. ret = 0;
  1336. out:
  1337. kfree(data);
  1338. return ret;
  1339. }
  1340. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1341. {
  1342. struct ethtool_value id;
  1343. if (!dev->ethtool_ops->phys_id)
  1344. return -EOPNOTSUPP;
  1345. if (copy_from_user(&id, useraddr, sizeof(id)))
  1346. return -EFAULT;
  1347. return dev->ethtool_ops->phys_id(dev, id.data);
  1348. }
  1349. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1350. {
  1351. struct ethtool_stats stats;
  1352. const struct ethtool_ops *ops = dev->ethtool_ops;
  1353. u64 *data;
  1354. int ret, n_stats;
  1355. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1356. return -EOPNOTSUPP;
  1357. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1358. if (n_stats < 0)
  1359. return n_stats;
  1360. WARN_ON(n_stats == 0);
  1361. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1362. return -EFAULT;
  1363. stats.n_stats = n_stats;
  1364. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  1365. if (!data)
  1366. return -ENOMEM;
  1367. ops->get_ethtool_stats(dev, &stats, data);
  1368. ret = -EFAULT;
  1369. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1370. goto out;
  1371. useraddr += sizeof(stats);
  1372. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1373. goto out;
  1374. ret = 0;
  1375. out:
  1376. kfree(data);
  1377. return ret;
  1378. }
  1379. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1380. {
  1381. struct ethtool_perm_addr epaddr;
  1382. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1383. return -EFAULT;
  1384. if (epaddr.size < dev->addr_len)
  1385. return -ETOOSMALL;
  1386. epaddr.size = dev->addr_len;
  1387. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1388. return -EFAULT;
  1389. useraddr += sizeof(epaddr);
  1390. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1391. return -EFAULT;
  1392. return 0;
  1393. }
  1394. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1395. u32 cmd, u32 (*actor)(struct net_device *))
  1396. {
  1397. struct ethtool_value edata = { .cmd = cmd };
  1398. if (!actor)
  1399. return -EOPNOTSUPP;
  1400. edata.data = actor(dev);
  1401. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1402. return -EFAULT;
  1403. return 0;
  1404. }
  1405. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1406. void (*actor)(struct net_device *, u32))
  1407. {
  1408. struct ethtool_value edata;
  1409. if (!actor)
  1410. return -EOPNOTSUPP;
  1411. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1412. return -EFAULT;
  1413. actor(dev, edata.data);
  1414. return 0;
  1415. }
  1416. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1417. int (*actor)(struct net_device *, u32))
  1418. {
  1419. struct ethtool_value edata;
  1420. if (!actor)
  1421. return -EOPNOTSUPP;
  1422. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1423. return -EFAULT;
  1424. return actor(dev, edata.data);
  1425. }
  1426. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  1427. char __user *useraddr)
  1428. {
  1429. struct ethtool_flash efl;
  1430. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  1431. return -EFAULT;
  1432. if (!dev->ethtool_ops->flash_device)
  1433. return -EOPNOTSUPP;
  1434. return dev->ethtool_ops->flash_device(dev, &efl);
  1435. }
  1436. /* The main entry point in this file. Called from net/core/dev.c */
  1437. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1438. {
  1439. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1440. void __user *useraddr = ifr->ifr_data;
  1441. u32 ethcmd;
  1442. int rc;
  1443. u32 old_features;
  1444. if (!dev || !netif_device_present(dev))
  1445. return -ENODEV;
  1446. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1447. return -EFAULT;
  1448. if (!dev->ethtool_ops) {
  1449. /* ETHTOOL_GDRVINFO does not require any driver support.
  1450. * It is also unprivileged and does not change anything,
  1451. * so we can take a shortcut to it. */
  1452. if (ethcmd == ETHTOOL_GDRVINFO)
  1453. return ethtool_get_drvinfo(dev, useraddr);
  1454. else
  1455. return -EOPNOTSUPP;
  1456. }
  1457. /* Allow some commands to be done by anyone */
  1458. switch (ethcmd) {
  1459. case ETHTOOL_GSET:
  1460. case ETHTOOL_GDRVINFO:
  1461. case ETHTOOL_GMSGLVL:
  1462. case ETHTOOL_GCOALESCE:
  1463. case ETHTOOL_GRINGPARAM:
  1464. case ETHTOOL_GPAUSEPARAM:
  1465. case ETHTOOL_GRXCSUM:
  1466. case ETHTOOL_GTXCSUM:
  1467. case ETHTOOL_GSG:
  1468. case ETHTOOL_GSTRINGS:
  1469. case ETHTOOL_GTSO:
  1470. case ETHTOOL_GPERMADDR:
  1471. case ETHTOOL_GUFO:
  1472. case ETHTOOL_GGSO:
  1473. case ETHTOOL_GGRO:
  1474. case ETHTOOL_GFLAGS:
  1475. case ETHTOOL_GPFLAGS:
  1476. case ETHTOOL_GRXFH:
  1477. case ETHTOOL_GRXRINGS:
  1478. case ETHTOOL_GRXCLSRLCNT:
  1479. case ETHTOOL_GRXCLSRULE:
  1480. case ETHTOOL_GRXCLSRLALL:
  1481. case ETHTOOL_GFEATURES:
  1482. break;
  1483. default:
  1484. if (!capable(CAP_NET_ADMIN))
  1485. return -EPERM;
  1486. }
  1487. if (dev->ethtool_ops->begin) {
  1488. rc = dev->ethtool_ops->begin(dev);
  1489. if (rc < 0)
  1490. return rc;
  1491. }
  1492. old_features = dev->features;
  1493. switch (ethcmd) {
  1494. case ETHTOOL_GSET:
  1495. rc = ethtool_get_settings(dev, useraddr);
  1496. break;
  1497. case ETHTOOL_SSET:
  1498. rc = ethtool_set_settings(dev, useraddr);
  1499. break;
  1500. case ETHTOOL_GDRVINFO:
  1501. rc = ethtool_get_drvinfo(dev, useraddr);
  1502. break;
  1503. case ETHTOOL_GREGS:
  1504. rc = ethtool_get_regs(dev, useraddr);
  1505. break;
  1506. case ETHTOOL_GWOL:
  1507. rc = ethtool_get_wol(dev, useraddr);
  1508. break;
  1509. case ETHTOOL_SWOL:
  1510. rc = ethtool_set_wol(dev, useraddr);
  1511. break;
  1512. case ETHTOOL_GMSGLVL:
  1513. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1514. dev->ethtool_ops->get_msglevel);
  1515. break;
  1516. case ETHTOOL_SMSGLVL:
  1517. rc = ethtool_set_value_void(dev, useraddr,
  1518. dev->ethtool_ops->set_msglevel);
  1519. break;
  1520. case ETHTOOL_NWAY_RST:
  1521. rc = ethtool_nway_reset(dev);
  1522. break;
  1523. case ETHTOOL_GLINK:
  1524. rc = ethtool_get_link(dev, useraddr);
  1525. break;
  1526. case ETHTOOL_GEEPROM:
  1527. rc = ethtool_get_eeprom(dev, useraddr);
  1528. break;
  1529. case ETHTOOL_SEEPROM:
  1530. rc = ethtool_set_eeprom(dev, useraddr);
  1531. break;
  1532. case ETHTOOL_GCOALESCE:
  1533. rc = ethtool_get_coalesce(dev, useraddr);
  1534. break;
  1535. case ETHTOOL_SCOALESCE:
  1536. rc = ethtool_set_coalesce(dev, useraddr);
  1537. break;
  1538. case ETHTOOL_GRINGPARAM:
  1539. rc = ethtool_get_ringparam(dev, useraddr);
  1540. break;
  1541. case ETHTOOL_SRINGPARAM:
  1542. rc = ethtool_set_ringparam(dev, useraddr);
  1543. break;
  1544. case ETHTOOL_GPAUSEPARAM:
  1545. rc = ethtool_get_pauseparam(dev, useraddr);
  1546. break;
  1547. case ETHTOOL_SPAUSEPARAM:
  1548. rc = ethtool_set_pauseparam(dev, useraddr);
  1549. break;
  1550. case ETHTOOL_TEST:
  1551. rc = ethtool_self_test(dev, useraddr);
  1552. break;
  1553. case ETHTOOL_GSTRINGS:
  1554. rc = ethtool_get_strings(dev, useraddr);
  1555. break;
  1556. case ETHTOOL_PHYS_ID:
  1557. rc = ethtool_phys_id(dev, useraddr);
  1558. break;
  1559. case ETHTOOL_GSTATS:
  1560. rc = ethtool_get_stats(dev, useraddr);
  1561. break;
  1562. case ETHTOOL_GPERMADDR:
  1563. rc = ethtool_get_perm_addr(dev, useraddr);
  1564. break;
  1565. case ETHTOOL_GFLAGS:
  1566. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1567. (dev->ethtool_ops->get_flags ?
  1568. dev->ethtool_ops->get_flags :
  1569. ethtool_op_get_flags));
  1570. break;
  1571. case ETHTOOL_SFLAGS:
  1572. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  1573. break;
  1574. case ETHTOOL_GPFLAGS:
  1575. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1576. dev->ethtool_ops->get_priv_flags);
  1577. break;
  1578. case ETHTOOL_SPFLAGS:
  1579. rc = ethtool_set_value(dev, useraddr,
  1580. dev->ethtool_ops->set_priv_flags);
  1581. break;
  1582. case ETHTOOL_GRXFH:
  1583. case ETHTOOL_GRXRINGS:
  1584. case ETHTOOL_GRXCLSRLCNT:
  1585. case ETHTOOL_GRXCLSRULE:
  1586. case ETHTOOL_GRXCLSRLALL:
  1587. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1588. break;
  1589. case ETHTOOL_SRXFH:
  1590. case ETHTOOL_SRXCLSRLDEL:
  1591. case ETHTOOL_SRXCLSRLINS:
  1592. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1593. break;
  1594. case ETHTOOL_FLASHDEV:
  1595. rc = ethtool_flash_device(dev, useraddr);
  1596. break;
  1597. case ETHTOOL_RESET:
  1598. rc = ethtool_reset(dev, useraddr);
  1599. break;
  1600. case ETHTOOL_SRXNTUPLE:
  1601. rc = ethtool_set_rx_ntuple(dev, useraddr);
  1602. break;
  1603. case ETHTOOL_GRXNTUPLE:
  1604. rc = ethtool_get_rx_ntuple(dev, useraddr);
  1605. break;
  1606. case ETHTOOL_GSSET_INFO:
  1607. rc = ethtool_get_sset_info(dev, useraddr);
  1608. break;
  1609. case ETHTOOL_GRXFHINDIR:
  1610. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1611. break;
  1612. case ETHTOOL_SRXFHINDIR:
  1613. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1614. break;
  1615. case ETHTOOL_GFEATURES:
  1616. rc = ethtool_get_features(dev, useraddr);
  1617. break;
  1618. case ETHTOOL_SFEATURES:
  1619. rc = ethtool_set_features(dev, useraddr);
  1620. break;
  1621. case ETHTOOL_GTXCSUM:
  1622. case ETHTOOL_GRXCSUM:
  1623. case ETHTOOL_GSG:
  1624. case ETHTOOL_GTSO:
  1625. case ETHTOOL_GUFO:
  1626. case ETHTOOL_GGSO:
  1627. case ETHTOOL_GGRO:
  1628. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  1629. break;
  1630. case ETHTOOL_STXCSUM:
  1631. case ETHTOOL_SRXCSUM:
  1632. case ETHTOOL_SSG:
  1633. case ETHTOOL_STSO:
  1634. case ETHTOOL_SUFO:
  1635. case ETHTOOL_SGSO:
  1636. case ETHTOOL_SGRO:
  1637. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  1638. break;
  1639. default:
  1640. rc = -EOPNOTSUPP;
  1641. }
  1642. if (dev->ethtool_ops->complete)
  1643. dev->ethtool_ops->complete(dev);
  1644. if (old_features != dev->features)
  1645. netdev_features_change(dev);
  1646. return rc;
  1647. }