ethtool.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888
  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/net_tstamp.h>
  20. #include <linux/phy.h>
  21. #include <linux/bitops.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/sfp.h>
  25. #include <linux/slab.h>
  26. #include <linux/rtnetlink.h>
  27. #include <linux/sched/signal.h>
  28. #include <linux/net.h>
  29. /*
  30. * Some useful ethtool_ops methods that're device independent.
  31. * If we find that all drivers want to do the same thing here,
  32. * we can turn these into dev_() function calls.
  33. */
  34. u32 ethtool_op_get_link(struct net_device *dev)
  35. {
  36. return netif_carrier_ok(dev) ? 1 : 0;
  37. }
  38. EXPORT_SYMBOL(ethtool_op_get_link);
  39. int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
  40. {
  41. info->so_timestamping =
  42. SOF_TIMESTAMPING_TX_SOFTWARE |
  43. SOF_TIMESTAMPING_RX_SOFTWARE |
  44. SOF_TIMESTAMPING_SOFTWARE;
  45. info->phc_index = -1;
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(ethtool_op_get_ts_info);
  49. /* Handlers for each ethtool command */
  50. #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
  51. static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
  52. [NETIF_F_SG_BIT] = "tx-scatter-gather",
  53. [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
  54. [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
  55. [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
  56. [NETIF_F_HIGHDMA_BIT] = "highdma",
  57. [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
  58. [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
  59. [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
  60. [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
  61. [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
  62. [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
  63. [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
  64. [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
  65. [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
  66. [NETIF_F_LLTX_BIT] = "tx-lockless",
  67. [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
  68. [NETIF_F_GRO_BIT] = "rx-gro",
  69. [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
  70. [NETIF_F_LRO_BIT] = "rx-lro",
  71. [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
  72. [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
  73. [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
  74. [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
  75. [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
  76. [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
  77. [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
  78. [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
  79. [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
  80. [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
  81. [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
  82. [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
  83. [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
  84. [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
  85. [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
  86. [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
  87. [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
  88. [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
  89. [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
  90. [NETIF_F_RXHASH_BIT] = "rx-hashing",
  91. [NETIF_F_RXCSUM_BIT] = "rx-checksum",
  92. [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
  93. [NETIF_F_LOOPBACK_BIT] = "loopback",
  94. [NETIF_F_RXFCS_BIT] = "rx-fcs",
  95. [NETIF_F_RXALL_BIT] = "rx-all",
  96. [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
  97. [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
  98. [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
  99. [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
  100. [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
  101. [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
  102. };
  103. static const char
  104. rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
  105. [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
  106. [ETH_RSS_HASH_XOR_BIT] = "xor",
  107. [ETH_RSS_HASH_CRC32_BIT] = "crc32",
  108. };
  109. static const char
  110. tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
  111. [ETHTOOL_ID_UNSPEC] = "Unspec",
  112. [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
  113. [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
  114. [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
  115. };
  116. static const char
  117. phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
  118. [ETHTOOL_ID_UNSPEC] = "Unspec",
  119. [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
  120. };
  121. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  122. {
  123. struct ethtool_gfeatures cmd = {
  124. .cmd = ETHTOOL_GFEATURES,
  125. .size = ETHTOOL_DEV_FEATURE_WORDS,
  126. };
  127. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  128. u32 __user *sizeaddr;
  129. u32 copy_size;
  130. int i;
  131. /* in case feature bits run out again */
  132. BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
  133. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  134. features[i].available = (u32)(dev->hw_features >> (32 * i));
  135. features[i].requested = (u32)(dev->wanted_features >> (32 * i));
  136. features[i].active = (u32)(dev->features >> (32 * i));
  137. features[i].never_changed =
  138. (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
  139. }
  140. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  141. if (get_user(copy_size, sizeaddr))
  142. return -EFAULT;
  143. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  144. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  145. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  146. return -EFAULT;
  147. useraddr += sizeof(cmd);
  148. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  149. return -EFAULT;
  150. return 0;
  151. }
  152. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  153. {
  154. struct ethtool_sfeatures cmd;
  155. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  156. netdev_features_t wanted = 0, valid = 0;
  157. int i, ret = 0;
  158. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  159. return -EFAULT;
  160. useraddr += sizeof(cmd);
  161. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  162. return -EINVAL;
  163. if (copy_from_user(features, useraddr, sizeof(features)))
  164. return -EFAULT;
  165. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  166. valid |= (netdev_features_t)features[i].valid << (32 * i);
  167. wanted |= (netdev_features_t)features[i].requested << (32 * i);
  168. }
  169. if (valid & ~NETIF_F_ETHTOOL_BITS)
  170. return -EINVAL;
  171. if (valid & ~dev->hw_features) {
  172. valid &= dev->hw_features;
  173. ret |= ETHTOOL_F_UNSUPPORTED;
  174. }
  175. dev->wanted_features &= ~valid;
  176. dev->wanted_features |= wanted & valid;
  177. __netdev_update_features(dev);
  178. if ((dev->wanted_features ^ dev->features) & valid)
  179. ret |= ETHTOOL_F_WISH;
  180. return ret;
  181. }
  182. static int phy_get_sset_count(struct phy_device *phydev)
  183. {
  184. int ret;
  185. if (phydev->drv->get_sset_count &&
  186. phydev->drv->get_strings &&
  187. phydev->drv->get_stats) {
  188. mutex_lock(&phydev->lock);
  189. ret = phydev->drv->get_sset_count(phydev);
  190. mutex_unlock(&phydev->lock);
  191. return ret;
  192. }
  193. return -EOPNOTSUPP;
  194. }
  195. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  196. {
  197. const struct ethtool_ops *ops = dev->ethtool_ops;
  198. if (sset == ETH_SS_FEATURES)
  199. return ARRAY_SIZE(netdev_features_strings);
  200. if (sset == ETH_SS_RSS_HASH_FUNCS)
  201. return ARRAY_SIZE(rss_hash_func_strings);
  202. if (sset == ETH_SS_TUNABLES)
  203. return ARRAY_SIZE(tunable_strings);
  204. if (sset == ETH_SS_PHY_TUNABLES)
  205. return ARRAY_SIZE(phy_tunable_strings);
  206. if (sset == ETH_SS_PHY_STATS) {
  207. if (dev->phydev)
  208. return phy_get_sset_count(dev->phydev);
  209. else
  210. return -EOPNOTSUPP;
  211. }
  212. if (ops->get_sset_count && ops->get_strings)
  213. return ops->get_sset_count(dev, sset);
  214. else
  215. return -EOPNOTSUPP;
  216. }
  217. static void __ethtool_get_strings(struct net_device *dev,
  218. u32 stringset, u8 *data)
  219. {
  220. const struct ethtool_ops *ops = dev->ethtool_ops;
  221. if (stringset == ETH_SS_FEATURES)
  222. memcpy(data, netdev_features_strings,
  223. sizeof(netdev_features_strings));
  224. else if (stringset == ETH_SS_RSS_HASH_FUNCS)
  225. memcpy(data, rss_hash_func_strings,
  226. sizeof(rss_hash_func_strings));
  227. else if (stringset == ETH_SS_TUNABLES)
  228. memcpy(data, tunable_strings, sizeof(tunable_strings));
  229. else if (stringset == ETH_SS_PHY_TUNABLES)
  230. memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
  231. else if (stringset == ETH_SS_PHY_STATS) {
  232. struct phy_device *phydev = dev->phydev;
  233. if (phydev) {
  234. mutex_lock(&phydev->lock);
  235. phydev->drv->get_strings(phydev, data);
  236. mutex_unlock(&phydev->lock);
  237. } else {
  238. return;
  239. }
  240. } else
  241. /* ops->get_strings is valid because checked earlier */
  242. ops->get_strings(dev, stringset, data);
  243. }
  244. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  245. {
  246. /* feature masks of legacy discrete ethtool ops */
  247. switch (eth_cmd) {
  248. case ETHTOOL_GTXCSUM:
  249. case ETHTOOL_STXCSUM:
  250. return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
  251. case ETHTOOL_GRXCSUM:
  252. case ETHTOOL_SRXCSUM:
  253. return NETIF_F_RXCSUM;
  254. case ETHTOOL_GSG:
  255. case ETHTOOL_SSG:
  256. return NETIF_F_SG;
  257. case ETHTOOL_GTSO:
  258. case ETHTOOL_STSO:
  259. return NETIF_F_ALL_TSO;
  260. case ETHTOOL_GGSO:
  261. case ETHTOOL_SGSO:
  262. return NETIF_F_GSO;
  263. case ETHTOOL_GGRO:
  264. case ETHTOOL_SGRO:
  265. return NETIF_F_GRO;
  266. default:
  267. BUG();
  268. }
  269. }
  270. static int ethtool_get_one_feature(struct net_device *dev,
  271. char __user *useraddr, u32 ethcmd)
  272. {
  273. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  274. struct ethtool_value edata = {
  275. .cmd = ethcmd,
  276. .data = !!(dev->features & mask),
  277. };
  278. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  279. return -EFAULT;
  280. return 0;
  281. }
  282. static int ethtool_set_one_feature(struct net_device *dev,
  283. void __user *useraddr, u32 ethcmd)
  284. {
  285. struct ethtool_value edata;
  286. netdev_features_t mask;
  287. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  288. return -EFAULT;
  289. mask = ethtool_get_feature_mask(ethcmd);
  290. mask &= dev->hw_features;
  291. if (!mask)
  292. return -EOPNOTSUPP;
  293. if (edata.data)
  294. dev->wanted_features |= mask;
  295. else
  296. dev->wanted_features &= ~mask;
  297. __netdev_update_features(dev);
  298. return 0;
  299. }
  300. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  301. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  302. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
  303. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
  304. NETIF_F_RXHASH)
  305. static u32 __ethtool_get_flags(struct net_device *dev)
  306. {
  307. u32 flags = 0;
  308. if (dev->features & NETIF_F_LRO)
  309. flags |= ETH_FLAG_LRO;
  310. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  311. flags |= ETH_FLAG_RXVLAN;
  312. if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
  313. flags |= ETH_FLAG_TXVLAN;
  314. if (dev->features & NETIF_F_NTUPLE)
  315. flags |= ETH_FLAG_NTUPLE;
  316. if (dev->features & NETIF_F_RXHASH)
  317. flags |= ETH_FLAG_RXHASH;
  318. return flags;
  319. }
  320. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  321. {
  322. netdev_features_t features = 0, changed;
  323. if (data & ~ETH_ALL_FLAGS)
  324. return -EINVAL;
  325. if (data & ETH_FLAG_LRO)
  326. features |= NETIF_F_LRO;
  327. if (data & ETH_FLAG_RXVLAN)
  328. features |= NETIF_F_HW_VLAN_CTAG_RX;
  329. if (data & ETH_FLAG_TXVLAN)
  330. features |= NETIF_F_HW_VLAN_CTAG_TX;
  331. if (data & ETH_FLAG_NTUPLE)
  332. features |= NETIF_F_NTUPLE;
  333. if (data & ETH_FLAG_RXHASH)
  334. features |= NETIF_F_RXHASH;
  335. /* allow changing only bits set in hw_features */
  336. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  337. if (changed & ~dev->hw_features)
  338. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  339. dev->wanted_features =
  340. (dev->wanted_features & ~changed) | (features & changed);
  341. __netdev_update_features(dev);
  342. return 0;
  343. }
  344. /* Given two link masks, AND them together and save the result in dst. */
  345. void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
  346. struct ethtool_link_ksettings *src)
  347. {
  348. unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
  349. unsigned int idx = 0;
  350. for (; idx < size; idx++) {
  351. dst->link_modes.supported[idx] &=
  352. src->link_modes.supported[idx];
  353. dst->link_modes.advertising[idx] &=
  354. src->link_modes.advertising[idx];
  355. }
  356. }
  357. EXPORT_SYMBOL(ethtool_intersect_link_masks);
  358. void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
  359. u32 legacy_u32)
  360. {
  361. bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
  362. dst[0] = legacy_u32;
  363. }
  364. EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
  365. /* return false if src had higher bits set. lower bits always updated. */
  366. bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
  367. const unsigned long *src)
  368. {
  369. bool retval = true;
  370. /* TODO: following test will soon always be true */
  371. if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
  372. __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
  373. bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
  374. bitmap_fill(ext, 32);
  375. bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
  376. if (bitmap_intersects(ext, src,
  377. __ETHTOOL_LINK_MODE_MASK_NBITS)) {
  378. /* src mask goes beyond bit 31 */
  379. retval = false;
  380. }
  381. }
  382. *legacy_u32 = src[0];
  383. return retval;
  384. }
  385. EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
  386. /* return false if legacy contained non-0 deprecated fields
  387. * maxtxpkt/maxrxpkt. rest of ksettings always updated
  388. */
  389. static bool
  390. convert_legacy_settings_to_link_ksettings(
  391. struct ethtool_link_ksettings *link_ksettings,
  392. const struct ethtool_cmd *legacy_settings)
  393. {
  394. bool retval = true;
  395. memset(link_ksettings, 0, sizeof(*link_ksettings));
  396. /* This is used to tell users that driver is still using these
  397. * deprecated legacy fields, and they should not use
  398. * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
  399. */
  400. if (legacy_settings->maxtxpkt ||
  401. legacy_settings->maxrxpkt)
  402. retval = false;
  403. ethtool_convert_legacy_u32_to_link_mode(
  404. link_ksettings->link_modes.supported,
  405. legacy_settings->supported);
  406. ethtool_convert_legacy_u32_to_link_mode(
  407. link_ksettings->link_modes.advertising,
  408. legacy_settings->advertising);
  409. ethtool_convert_legacy_u32_to_link_mode(
  410. link_ksettings->link_modes.lp_advertising,
  411. legacy_settings->lp_advertising);
  412. link_ksettings->base.speed
  413. = ethtool_cmd_speed(legacy_settings);
  414. link_ksettings->base.duplex
  415. = legacy_settings->duplex;
  416. link_ksettings->base.port
  417. = legacy_settings->port;
  418. link_ksettings->base.phy_address
  419. = legacy_settings->phy_address;
  420. link_ksettings->base.autoneg
  421. = legacy_settings->autoneg;
  422. link_ksettings->base.mdio_support
  423. = legacy_settings->mdio_support;
  424. link_ksettings->base.eth_tp_mdix
  425. = legacy_settings->eth_tp_mdix;
  426. link_ksettings->base.eth_tp_mdix_ctrl
  427. = legacy_settings->eth_tp_mdix_ctrl;
  428. return retval;
  429. }
  430. /* return false if ksettings link modes had higher bits
  431. * set. legacy_settings always updated (best effort)
  432. */
  433. static bool
  434. convert_link_ksettings_to_legacy_settings(
  435. struct ethtool_cmd *legacy_settings,
  436. const struct ethtool_link_ksettings *link_ksettings)
  437. {
  438. bool retval = true;
  439. memset(legacy_settings, 0, sizeof(*legacy_settings));
  440. /* this also clears the deprecated fields in legacy structure:
  441. * __u8 transceiver;
  442. * __u32 maxtxpkt;
  443. * __u32 maxrxpkt;
  444. */
  445. retval &= ethtool_convert_link_mode_to_legacy_u32(
  446. &legacy_settings->supported,
  447. link_ksettings->link_modes.supported);
  448. retval &= ethtool_convert_link_mode_to_legacy_u32(
  449. &legacy_settings->advertising,
  450. link_ksettings->link_modes.advertising);
  451. retval &= ethtool_convert_link_mode_to_legacy_u32(
  452. &legacy_settings->lp_advertising,
  453. link_ksettings->link_modes.lp_advertising);
  454. ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
  455. legacy_settings->duplex
  456. = link_ksettings->base.duplex;
  457. legacy_settings->port
  458. = link_ksettings->base.port;
  459. legacy_settings->phy_address
  460. = link_ksettings->base.phy_address;
  461. legacy_settings->autoneg
  462. = link_ksettings->base.autoneg;
  463. legacy_settings->mdio_support
  464. = link_ksettings->base.mdio_support;
  465. legacy_settings->eth_tp_mdix
  466. = link_ksettings->base.eth_tp_mdix;
  467. legacy_settings->eth_tp_mdix_ctrl
  468. = link_ksettings->base.eth_tp_mdix_ctrl;
  469. legacy_settings->transceiver
  470. = link_ksettings->base.transceiver;
  471. return retval;
  472. }
  473. /* number of 32-bit words to store the user's link mode bitmaps */
  474. #define __ETHTOOL_LINK_MODE_MASK_NU32 \
  475. DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
  476. /* layout of the struct passed from/to userland */
  477. struct ethtool_link_usettings {
  478. struct ethtool_link_settings base;
  479. struct {
  480. __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
  481. __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  482. __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  483. } link_modes;
  484. };
  485. /* Internal kernel helper to query a device ethtool_link_settings.
  486. *
  487. * Backward compatibility note: for compatibility with legacy drivers
  488. * that implement only the ethtool_cmd API, this has to work with both
  489. * drivers implementing get_link_ksettings API and drivers
  490. * implementing get_settings API. When drivers implement get_settings
  491. * and report ethtool_cmd deprecated fields
  492. * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
  493. * because the resulting struct ethtool_link_settings does not report them.
  494. */
  495. int __ethtool_get_link_ksettings(struct net_device *dev,
  496. struct ethtool_link_ksettings *link_ksettings)
  497. {
  498. int err;
  499. struct ethtool_cmd cmd;
  500. ASSERT_RTNL();
  501. if (dev->ethtool_ops->get_link_ksettings) {
  502. memset(link_ksettings, 0, sizeof(*link_ksettings));
  503. return dev->ethtool_ops->get_link_ksettings(dev,
  504. link_ksettings);
  505. }
  506. /* driver doesn't support %ethtool_link_ksettings API. revert to
  507. * legacy %ethtool_cmd API, unless it's not supported either.
  508. * TODO: remove when ethtool_ops::get_settings disappears internally
  509. */
  510. if (!dev->ethtool_ops->get_settings)
  511. return -EOPNOTSUPP;
  512. memset(&cmd, 0, sizeof(cmd));
  513. cmd.cmd = ETHTOOL_GSET;
  514. err = dev->ethtool_ops->get_settings(dev, &cmd);
  515. if (err < 0)
  516. return err;
  517. /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
  518. */
  519. convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
  520. return err;
  521. }
  522. EXPORT_SYMBOL(__ethtool_get_link_ksettings);
  523. /* convert ethtool_link_usettings in user space to a kernel internal
  524. * ethtool_link_ksettings. return 0 on success, errno on error.
  525. */
  526. static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
  527. const void __user *from)
  528. {
  529. struct ethtool_link_usettings link_usettings;
  530. if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
  531. return -EFAULT;
  532. memcpy(&to->base, &link_usettings.base, sizeof(to->base));
  533. bitmap_from_arr32(to->link_modes.supported,
  534. link_usettings.link_modes.supported,
  535. __ETHTOOL_LINK_MODE_MASK_NBITS);
  536. bitmap_from_arr32(to->link_modes.advertising,
  537. link_usettings.link_modes.advertising,
  538. __ETHTOOL_LINK_MODE_MASK_NBITS);
  539. bitmap_from_arr32(to->link_modes.lp_advertising,
  540. link_usettings.link_modes.lp_advertising,
  541. __ETHTOOL_LINK_MODE_MASK_NBITS);
  542. return 0;
  543. }
  544. /* convert a kernel internal ethtool_link_ksettings to
  545. * ethtool_link_usettings in user space. return 0 on success, errno on
  546. * error.
  547. */
  548. static int
  549. store_link_ksettings_for_user(void __user *to,
  550. const struct ethtool_link_ksettings *from)
  551. {
  552. struct ethtool_link_usettings link_usettings;
  553. memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
  554. bitmap_to_arr32(link_usettings.link_modes.supported,
  555. from->link_modes.supported,
  556. __ETHTOOL_LINK_MODE_MASK_NBITS);
  557. bitmap_to_arr32(link_usettings.link_modes.advertising,
  558. from->link_modes.advertising,
  559. __ETHTOOL_LINK_MODE_MASK_NBITS);
  560. bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
  561. from->link_modes.lp_advertising,
  562. __ETHTOOL_LINK_MODE_MASK_NBITS);
  563. if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
  564. return -EFAULT;
  565. return 0;
  566. }
  567. /* Query device for its ethtool_link_settings.
  568. *
  569. * Backward compatibility note: this function must fail when driver
  570. * does not implement ethtool::get_link_ksettings, even if legacy
  571. * ethtool_ops::get_settings is implemented. This tells new versions
  572. * of ethtool that they should use the legacy API %ETHTOOL_GSET for
  573. * this driver, so that they can correctly access the ethtool_cmd
  574. * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
  575. * implements ethtool_ops::get_settings anymore.
  576. */
  577. static int ethtool_get_link_ksettings(struct net_device *dev,
  578. void __user *useraddr)
  579. {
  580. int err = 0;
  581. struct ethtool_link_ksettings link_ksettings;
  582. ASSERT_RTNL();
  583. if (!dev->ethtool_ops->get_link_ksettings)
  584. return -EOPNOTSUPP;
  585. /* handle bitmap nbits handshake */
  586. if (copy_from_user(&link_ksettings.base, useraddr,
  587. sizeof(link_ksettings.base)))
  588. return -EFAULT;
  589. if (__ETHTOOL_LINK_MODE_MASK_NU32
  590. != link_ksettings.base.link_mode_masks_nwords) {
  591. /* wrong link mode nbits requested */
  592. memset(&link_ksettings, 0, sizeof(link_ksettings));
  593. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  594. /* send back number of words required as negative val */
  595. compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
  596. "need too many bits for link modes!");
  597. link_ksettings.base.link_mode_masks_nwords
  598. = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
  599. /* copy the base fields back to user, not the link
  600. * mode bitmaps
  601. */
  602. if (copy_to_user(useraddr, &link_ksettings.base,
  603. sizeof(link_ksettings.base)))
  604. return -EFAULT;
  605. return 0;
  606. }
  607. /* handshake successful: user/kernel agree on
  608. * link_mode_masks_nwords
  609. */
  610. memset(&link_ksettings, 0, sizeof(link_ksettings));
  611. err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
  612. if (err < 0)
  613. return err;
  614. /* make sure we tell the right values to user */
  615. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  616. link_ksettings.base.link_mode_masks_nwords
  617. = __ETHTOOL_LINK_MODE_MASK_NU32;
  618. return store_link_ksettings_for_user(useraddr, &link_ksettings);
  619. }
  620. /* Update device ethtool_link_settings.
  621. *
  622. * Backward compatibility note: this function must fail when driver
  623. * does not implement ethtool::set_link_ksettings, even if legacy
  624. * ethtool_ops::set_settings is implemented. This tells new versions
  625. * of ethtool that they should use the legacy API %ETHTOOL_SSET for
  626. * this driver, so that they can correctly update the ethtool_cmd
  627. * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
  628. * implements ethtool_ops::get_settings anymore.
  629. */
  630. static int ethtool_set_link_ksettings(struct net_device *dev,
  631. void __user *useraddr)
  632. {
  633. int err;
  634. struct ethtool_link_ksettings link_ksettings;
  635. ASSERT_RTNL();
  636. if (!dev->ethtool_ops->set_link_ksettings)
  637. return -EOPNOTSUPP;
  638. /* make sure nbits field has expected value */
  639. if (copy_from_user(&link_ksettings.base, useraddr,
  640. sizeof(link_ksettings.base)))
  641. return -EFAULT;
  642. if (__ETHTOOL_LINK_MODE_MASK_NU32
  643. != link_ksettings.base.link_mode_masks_nwords)
  644. return -EINVAL;
  645. /* copy the whole structure, now that we know it has expected
  646. * format
  647. */
  648. err = load_link_ksettings_from_user(&link_ksettings, useraddr);
  649. if (err)
  650. return err;
  651. /* re-check nwords field, just in case */
  652. if (__ETHTOOL_LINK_MODE_MASK_NU32
  653. != link_ksettings.base.link_mode_masks_nwords)
  654. return -EINVAL;
  655. return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
  656. }
  657. /* Query device for its ethtool_cmd settings.
  658. *
  659. * Backward compatibility note: for compatibility with legacy ethtool,
  660. * this has to work with both drivers implementing get_link_ksettings
  661. * API and drivers implementing get_settings API. When drivers
  662. * implement get_link_ksettings and report higher link mode bits, a
  663. * kernel warning is logged once (with name of 1st driver/device) to
  664. * recommend user to upgrade ethtool, but the command is successful
  665. * (only the lower link mode bits reported back to user).
  666. */
  667. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  668. {
  669. struct ethtool_cmd cmd;
  670. ASSERT_RTNL();
  671. if (dev->ethtool_ops->get_link_ksettings) {
  672. /* First, use link_ksettings API if it is supported */
  673. int err;
  674. struct ethtool_link_ksettings link_ksettings;
  675. memset(&link_ksettings, 0, sizeof(link_ksettings));
  676. err = dev->ethtool_ops->get_link_ksettings(dev,
  677. &link_ksettings);
  678. if (err < 0)
  679. return err;
  680. convert_link_ksettings_to_legacy_settings(&cmd,
  681. &link_ksettings);
  682. /* send a sensible cmd tag back to user */
  683. cmd.cmd = ETHTOOL_GSET;
  684. } else {
  685. /* driver doesn't support %ethtool_link_ksettings
  686. * API. revert to legacy %ethtool_cmd API, unless it's
  687. * not supported either.
  688. */
  689. int err;
  690. if (!dev->ethtool_ops->get_settings)
  691. return -EOPNOTSUPP;
  692. memset(&cmd, 0, sizeof(cmd));
  693. cmd.cmd = ETHTOOL_GSET;
  694. err = dev->ethtool_ops->get_settings(dev, &cmd);
  695. if (err < 0)
  696. return err;
  697. }
  698. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  699. return -EFAULT;
  700. return 0;
  701. }
  702. /* Update device link settings with given ethtool_cmd.
  703. *
  704. * Backward compatibility note: for compatibility with legacy ethtool,
  705. * this has to work with both drivers implementing set_link_ksettings
  706. * API and drivers implementing set_settings API. When drivers
  707. * implement set_link_ksettings and user's request updates deprecated
  708. * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
  709. * warning is logged once (with name of 1st driver/device) to
  710. * recommend user to upgrade ethtool, and the request is rejected.
  711. */
  712. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  713. {
  714. struct ethtool_cmd cmd;
  715. ASSERT_RTNL();
  716. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  717. return -EFAULT;
  718. /* first, try new %ethtool_link_ksettings API. */
  719. if (dev->ethtool_ops->set_link_ksettings) {
  720. struct ethtool_link_ksettings link_ksettings;
  721. if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
  722. &cmd))
  723. return -EINVAL;
  724. link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
  725. link_ksettings.base.link_mode_masks_nwords
  726. = __ETHTOOL_LINK_MODE_MASK_NU32;
  727. return dev->ethtool_ops->set_link_ksettings(dev,
  728. &link_ksettings);
  729. }
  730. /* legacy %ethtool_cmd API */
  731. /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
  732. * disappears internally
  733. */
  734. if (!dev->ethtool_ops->set_settings)
  735. return -EOPNOTSUPP;
  736. return dev->ethtool_ops->set_settings(dev, &cmd);
  737. }
  738. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  739. void __user *useraddr)
  740. {
  741. struct ethtool_drvinfo info;
  742. const struct ethtool_ops *ops = dev->ethtool_ops;
  743. memset(&info, 0, sizeof(info));
  744. info.cmd = ETHTOOL_GDRVINFO;
  745. if (ops->get_drvinfo) {
  746. ops->get_drvinfo(dev, &info);
  747. } else if (dev->dev.parent && dev->dev.parent->driver) {
  748. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  749. sizeof(info.bus_info));
  750. strlcpy(info.driver, dev->dev.parent->driver->name,
  751. sizeof(info.driver));
  752. } else {
  753. return -EOPNOTSUPP;
  754. }
  755. /*
  756. * this method of obtaining string set info is deprecated;
  757. * Use ETHTOOL_GSSET_INFO instead.
  758. */
  759. if (ops->get_sset_count) {
  760. int rc;
  761. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  762. if (rc >= 0)
  763. info.testinfo_len = rc;
  764. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  765. if (rc >= 0)
  766. info.n_stats = rc;
  767. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  768. if (rc >= 0)
  769. info.n_priv_flags = rc;
  770. }
  771. if (ops->get_regs_len)
  772. info.regdump_len = ops->get_regs_len(dev);
  773. if (ops->get_eeprom_len)
  774. info.eedump_len = ops->get_eeprom_len(dev);
  775. if (copy_to_user(useraddr, &info, sizeof(info)))
  776. return -EFAULT;
  777. return 0;
  778. }
  779. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  780. void __user *useraddr)
  781. {
  782. struct ethtool_sset_info info;
  783. u64 sset_mask;
  784. int i, idx = 0, n_bits = 0, ret, rc;
  785. u32 *info_buf = NULL;
  786. if (copy_from_user(&info, useraddr, sizeof(info)))
  787. return -EFAULT;
  788. /* store copy of mask, because we zero struct later on */
  789. sset_mask = info.sset_mask;
  790. if (!sset_mask)
  791. return 0;
  792. /* calculate size of return buffer */
  793. n_bits = hweight64(sset_mask);
  794. memset(&info, 0, sizeof(info));
  795. info.cmd = ETHTOOL_GSSET_INFO;
  796. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  797. if (!info_buf)
  798. return -ENOMEM;
  799. /*
  800. * fill return buffer based on input bitmask and successful
  801. * get_sset_count return
  802. */
  803. for (i = 0; i < 64; i++) {
  804. if (!(sset_mask & (1ULL << i)))
  805. continue;
  806. rc = __ethtool_get_sset_count(dev, i);
  807. if (rc >= 0) {
  808. info.sset_mask |= (1ULL << i);
  809. info_buf[idx++] = rc;
  810. }
  811. }
  812. ret = -EFAULT;
  813. if (copy_to_user(useraddr, &info, sizeof(info)))
  814. goto out;
  815. useraddr += offsetof(struct ethtool_sset_info, data);
  816. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  817. goto out;
  818. ret = 0;
  819. out:
  820. kfree(info_buf);
  821. return ret;
  822. }
  823. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  824. u32 cmd, void __user *useraddr)
  825. {
  826. struct ethtool_rxnfc info;
  827. size_t info_size = sizeof(info);
  828. int rc;
  829. if (!dev->ethtool_ops->set_rxnfc)
  830. return -EOPNOTSUPP;
  831. /* struct ethtool_rxnfc was originally defined for
  832. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  833. * members. User-space might still be using that
  834. * definition. */
  835. if (cmd == ETHTOOL_SRXFH)
  836. info_size = (offsetof(struct ethtool_rxnfc, data) +
  837. sizeof(info.data));
  838. if (copy_from_user(&info, useraddr, info_size))
  839. return -EFAULT;
  840. rc = dev->ethtool_ops->set_rxnfc(dev, &info);
  841. if (rc)
  842. return rc;
  843. if (cmd == ETHTOOL_SRXCLSRLINS &&
  844. copy_to_user(useraddr, &info, info_size))
  845. return -EFAULT;
  846. return 0;
  847. }
  848. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  849. u32 cmd, void __user *useraddr)
  850. {
  851. struct ethtool_rxnfc info;
  852. size_t info_size = sizeof(info);
  853. const struct ethtool_ops *ops = dev->ethtool_ops;
  854. int ret;
  855. void *rule_buf = NULL;
  856. if (!ops->get_rxnfc)
  857. return -EOPNOTSUPP;
  858. /* struct ethtool_rxnfc was originally defined for
  859. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  860. * members. User-space might still be using that
  861. * definition. */
  862. if (cmd == ETHTOOL_GRXFH)
  863. info_size = (offsetof(struct ethtool_rxnfc, data) +
  864. sizeof(info.data));
  865. if (copy_from_user(&info, useraddr, info_size))
  866. return -EFAULT;
  867. /* If FLOW_RSS was requested then user-space must be using the
  868. * new definition, as FLOW_RSS is newer.
  869. */
  870. if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
  871. info_size = sizeof(info);
  872. if (copy_from_user(&info, useraddr, info_size))
  873. return -EFAULT;
  874. /* Since malicious users may modify the original data,
  875. * we need to check whether FLOW_RSS is still requested.
  876. */
  877. if (!(info.flow_type & FLOW_RSS))
  878. return -EINVAL;
  879. }
  880. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  881. if (info.rule_cnt > 0) {
  882. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  883. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  884. GFP_USER);
  885. if (!rule_buf)
  886. return -ENOMEM;
  887. }
  888. }
  889. ret = ops->get_rxnfc(dev, &info, rule_buf);
  890. if (ret < 0)
  891. goto err_out;
  892. ret = -EFAULT;
  893. if (copy_to_user(useraddr, &info, info_size))
  894. goto err_out;
  895. if (rule_buf) {
  896. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  897. if (copy_to_user(useraddr, rule_buf,
  898. info.rule_cnt * sizeof(u32)))
  899. goto err_out;
  900. }
  901. ret = 0;
  902. err_out:
  903. kfree(rule_buf);
  904. return ret;
  905. }
  906. static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
  907. struct ethtool_rxnfc *rx_rings,
  908. u32 size)
  909. {
  910. int i;
  911. if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
  912. return -EFAULT;
  913. /* Validate ring indices */
  914. for (i = 0; i < size; i++)
  915. if (indir[i] >= rx_rings->data)
  916. return -EINVAL;
  917. return 0;
  918. }
  919. u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
  920. void netdev_rss_key_fill(void *buffer, size_t len)
  921. {
  922. BUG_ON(len > sizeof(netdev_rss_key));
  923. net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
  924. memcpy(buffer, netdev_rss_key, len);
  925. }
  926. EXPORT_SYMBOL(netdev_rss_key_fill);
  927. static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
  928. {
  929. u32 dev_size, current_max = 0;
  930. u32 *indir;
  931. int ret;
  932. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  933. !dev->ethtool_ops->get_rxfh)
  934. return -EOPNOTSUPP;
  935. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  936. if (dev_size == 0)
  937. return -EOPNOTSUPP;
  938. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  939. if (!indir)
  940. return -ENOMEM;
  941. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  942. if (ret)
  943. goto out;
  944. while (dev_size--)
  945. current_max = max(current_max, indir[dev_size]);
  946. *max = current_max;
  947. out:
  948. kfree(indir);
  949. return ret;
  950. }
  951. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  952. void __user *useraddr)
  953. {
  954. u32 user_size, dev_size;
  955. u32 *indir;
  956. int ret;
  957. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  958. !dev->ethtool_ops->get_rxfh)
  959. return -EOPNOTSUPP;
  960. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  961. if (dev_size == 0)
  962. return -EOPNOTSUPP;
  963. if (copy_from_user(&user_size,
  964. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  965. sizeof(user_size)))
  966. return -EFAULT;
  967. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  968. &dev_size, sizeof(dev_size)))
  969. return -EFAULT;
  970. /* If the user buffer size is 0, this is just a query for the
  971. * device table size. Otherwise, if it's smaller than the
  972. * device table size it's an error.
  973. */
  974. if (user_size < dev_size)
  975. return user_size == 0 ? 0 : -EINVAL;
  976. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  977. if (!indir)
  978. return -ENOMEM;
  979. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  980. if (ret)
  981. goto out;
  982. if (copy_to_user(useraddr +
  983. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  984. indir, dev_size * sizeof(indir[0])))
  985. ret = -EFAULT;
  986. out:
  987. kfree(indir);
  988. return ret;
  989. }
  990. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  991. void __user *useraddr)
  992. {
  993. struct ethtool_rxnfc rx_rings;
  994. u32 user_size, dev_size, i;
  995. u32 *indir;
  996. const struct ethtool_ops *ops = dev->ethtool_ops;
  997. int ret;
  998. u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
  999. if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
  1000. !ops->get_rxnfc)
  1001. return -EOPNOTSUPP;
  1002. dev_size = ops->get_rxfh_indir_size(dev);
  1003. if (dev_size == 0)
  1004. return -EOPNOTSUPP;
  1005. if (copy_from_user(&user_size,
  1006. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  1007. sizeof(user_size)))
  1008. return -EFAULT;
  1009. if (user_size != 0 && user_size != dev_size)
  1010. return -EINVAL;
  1011. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  1012. if (!indir)
  1013. return -ENOMEM;
  1014. rx_rings.cmd = ETHTOOL_GRXRINGS;
  1015. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  1016. if (ret)
  1017. goto out;
  1018. if (user_size == 0) {
  1019. for (i = 0; i < dev_size; i++)
  1020. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  1021. } else {
  1022. ret = ethtool_copy_validate_indir(indir,
  1023. useraddr + ringidx_offset,
  1024. &rx_rings,
  1025. dev_size);
  1026. if (ret)
  1027. goto out;
  1028. }
  1029. ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
  1030. if (ret)
  1031. goto out;
  1032. /* indicate whether rxfh was set to default */
  1033. if (user_size == 0)
  1034. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1035. else
  1036. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1037. out:
  1038. kfree(indir);
  1039. return ret;
  1040. }
  1041. static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
  1042. void __user *useraddr)
  1043. {
  1044. int ret;
  1045. const struct ethtool_ops *ops = dev->ethtool_ops;
  1046. u32 user_indir_size, user_key_size;
  1047. u32 dev_indir_size = 0, dev_key_size = 0;
  1048. struct ethtool_rxfh rxfh;
  1049. u32 total_size;
  1050. u32 indir_bytes;
  1051. u32 *indir = NULL;
  1052. u8 dev_hfunc = 0;
  1053. u8 *hkey = NULL;
  1054. u8 *rss_config;
  1055. if (!ops->get_rxfh)
  1056. return -EOPNOTSUPP;
  1057. if (ops->get_rxfh_indir_size)
  1058. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1059. if (ops->get_rxfh_key_size)
  1060. dev_key_size = ops->get_rxfh_key_size(dev);
  1061. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1062. return -EFAULT;
  1063. user_indir_size = rxfh.indir_size;
  1064. user_key_size = rxfh.key_size;
  1065. /* Check that reserved fields are 0 for now */
  1066. if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
  1067. return -EINVAL;
  1068. /* Most drivers don't handle rss_context, check it's 0 as well */
  1069. if (rxfh.rss_context && !ops->get_rxfh_context)
  1070. return -EOPNOTSUPP;
  1071. rxfh.indir_size = dev_indir_size;
  1072. rxfh.key_size = dev_key_size;
  1073. if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
  1074. return -EFAULT;
  1075. if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
  1076. (user_key_size && (user_key_size != dev_key_size)))
  1077. return -EINVAL;
  1078. indir_bytes = user_indir_size * sizeof(indir[0]);
  1079. total_size = indir_bytes + user_key_size;
  1080. rss_config = kzalloc(total_size, GFP_USER);
  1081. if (!rss_config)
  1082. return -ENOMEM;
  1083. if (user_indir_size)
  1084. indir = (u32 *)rss_config;
  1085. if (user_key_size)
  1086. hkey = rss_config + indir_bytes;
  1087. if (rxfh.rss_context)
  1088. ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
  1089. &dev_hfunc,
  1090. rxfh.rss_context);
  1091. else
  1092. ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
  1093. if (ret)
  1094. goto out;
  1095. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
  1096. &dev_hfunc, sizeof(rxfh.hfunc))) {
  1097. ret = -EFAULT;
  1098. } else if (copy_to_user(useraddr +
  1099. offsetof(struct ethtool_rxfh, rss_config[0]),
  1100. rss_config, total_size)) {
  1101. ret = -EFAULT;
  1102. }
  1103. out:
  1104. kfree(rss_config);
  1105. return ret;
  1106. }
  1107. static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
  1108. void __user *useraddr)
  1109. {
  1110. int ret;
  1111. const struct ethtool_ops *ops = dev->ethtool_ops;
  1112. struct ethtool_rxnfc rx_rings;
  1113. struct ethtool_rxfh rxfh;
  1114. u32 dev_indir_size = 0, dev_key_size = 0, i;
  1115. u32 *indir = NULL, indir_bytes = 0;
  1116. u8 *hkey = NULL;
  1117. u8 *rss_config;
  1118. u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
  1119. bool delete = false;
  1120. if (!ops->get_rxnfc || !ops->set_rxfh)
  1121. return -EOPNOTSUPP;
  1122. if (ops->get_rxfh_indir_size)
  1123. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1124. if (ops->get_rxfh_key_size)
  1125. dev_key_size = ops->get_rxfh_key_size(dev);
  1126. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1127. return -EFAULT;
  1128. /* Check that reserved fields are 0 for now */
  1129. if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
  1130. return -EINVAL;
  1131. /* Most drivers don't handle rss_context, check it's 0 as well */
  1132. if (rxfh.rss_context && !ops->set_rxfh_context)
  1133. return -EOPNOTSUPP;
  1134. /* If either indir, hash key or function is valid, proceed further.
  1135. * Must request at least one change: indir size, hash key or function.
  1136. */
  1137. if ((rxfh.indir_size &&
  1138. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
  1139. rxfh.indir_size != dev_indir_size) ||
  1140. (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
  1141. (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
  1142. rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
  1143. return -EINVAL;
  1144. if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1145. indir_bytes = dev_indir_size * sizeof(indir[0]);
  1146. rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
  1147. if (!rss_config)
  1148. return -ENOMEM;
  1149. rx_rings.cmd = ETHTOOL_GRXRINGS;
  1150. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  1151. if (ret)
  1152. goto out;
  1153. /* rxfh.indir_size == 0 means reset the indir table to default (master
  1154. * context) or delete the context (other RSS contexts).
  1155. * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
  1156. */
  1157. if (rxfh.indir_size &&
  1158. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
  1159. indir = (u32 *)rss_config;
  1160. ret = ethtool_copy_validate_indir(indir,
  1161. useraddr + rss_cfg_offset,
  1162. &rx_rings,
  1163. rxfh.indir_size);
  1164. if (ret)
  1165. goto out;
  1166. } else if (rxfh.indir_size == 0) {
  1167. if (rxfh.rss_context == 0) {
  1168. indir = (u32 *)rss_config;
  1169. for (i = 0; i < dev_indir_size; i++)
  1170. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  1171. } else {
  1172. delete = true;
  1173. }
  1174. }
  1175. if (rxfh.key_size) {
  1176. hkey = rss_config + indir_bytes;
  1177. if (copy_from_user(hkey,
  1178. useraddr + rss_cfg_offset + indir_bytes,
  1179. rxfh.key_size)) {
  1180. ret = -EFAULT;
  1181. goto out;
  1182. }
  1183. }
  1184. if (rxfh.rss_context)
  1185. ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
  1186. &rxfh.rss_context, delete);
  1187. else
  1188. ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
  1189. if (ret)
  1190. goto out;
  1191. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
  1192. &rxfh.rss_context, sizeof(rxfh.rss_context)))
  1193. ret = -EFAULT;
  1194. if (!rxfh.rss_context) {
  1195. /* indicate whether rxfh was set to default */
  1196. if (rxfh.indir_size == 0)
  1197. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1198. else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1199. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1200. }
  1201. out:
  1202. kfree(rss_config);
  1203. return ret;
  1204. }
  1205. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1206. {
  1207. struct ethtool_regs regs;
  1208. const struct ethtool_ops *ops = dev->ethtool_ops;
  1209. void *regbuf;
  1210. int reglen, ret;
  1211. if (!ops->get_regs || !ops->get_regs_len)
  1212. return -EOPNOTSUPP;
  1213. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1214. return -EFAULT;
  1215. reglen = ops->get_regs_len(dev);
  1216. if (regs.len > reglen)
  1217. regs.len = reglen;
  1218. regbuf = NULL;
  1219. if (reglen) {
  1220. regbuf = vzalloc(reglen);
  1221. if (!regbuf)
  1222. return -ENOMEM;
  1223. }
  1224. ops->get_regs(dev, &regs, regbuf);
  1225. ret = -EFAULT;
  1226. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1227. goto out;
  1228. useraddr += offsetof(struct ethtool_regs, data);
  1229. if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
  1230. goto out;
  1231. ret = 0;
  1232. out:
  1233. vfree(regbuf);
  1234. return ret;
  1235. }
  1236. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1237. {
  1238. struct ethtool_value reset;
  1239. int ret;
  1240. if (!dev->ethtool_ops->reset)
  1241. return -EOPNOTSUPP;
  1242. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1243. return -EFAULT;
  1244. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1245. if (ret)
  1246. return ret;
  1247. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1248. return -EFAULT;
  1249. return 0;
  1250. }
  1251. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1252. {
  1253. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1254. if (!dev->ethtool_ops->get_wol)
  1255. return -EOPNOTSUPP;
  1256. dev->ethtool_ops->get_wol(dev, &wol);
  1257. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1258. return -EFAULT;
  1259. return 0;
  1260. }
  1261. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1262. {
  1263. struct ethtool_wolinfo wol;
  1264. if (!dev->ethtool_ops->set_wol)
  1265. return -EOPNOTSUPP;
  1266. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1267. return -EFAULT;
  1268. return dev->ethtool_ops->set_wol(dev, &wol);
  1269. }
  1270. static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
  1271. {
  1272. struct ethtool_eee edata;
  1273. int rc;
  1274. if (!dev->ethtool_ops->get_eee)
  1275. return -EOPNOTSUPP;
  1276. memset(&edata, 0, sizeof(struct ethtool_eee));
  1277. edata.cmd = ETHTOOL_GEEE;
  1278. rc = dev->ethtool_ops->get_eee(dev, &edata);
  1279. if (rc)
  1280. return rc;
  1281. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1282. return -EFAULT;
  1283. return 0;
  1284. }
  1285. static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
  1286. {
  1287. struct ethtool_eee edata;
  1288. if (!dev->ethtool_ops->set_eee)
  1289. return -EOPNOTSUPP;
  1290. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1291. return -EFAULT;
  1292. return dev->ethtool_ops->set_eee(dev, &edata);
  1293. }
  1294. static int ethtool_nway_reset(struct net_device *dev)
  1295. {
  1296. if (!dev->ethtool_ops->nway_reset)
  1297. return -EOPNOTSUPP;
  1298. return dev->ethtool_ops->nway_reset(dev);
  1299. }
  1300. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1301. {
  1302. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1303. if (!dev->ethtool_ops->get_link)
  1304. return -EOPNOTSUPP;
  1305. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  1306. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1307. return -EFAULT;
  1308. return 0;
  1309. }
  1310. static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
  1311. int (*getter)(struct net_device *,
  1312. struct ethtool_eeprom *, u8 *),
  1313. u32 total_len)
  1314. {
  1315. struct ethtool_eeprom eeprom;
  1316. void __user *userbuf = useraddr + sizeof(eeprom);
  1317. u32 bytes_remaining;
  1318. u8 *data;
  1319. int ret = 0;
  1320. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1321. return -EFAULT;
  1322. /* Check for wrap and zero */
  1323. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1324. return -EINVAL;
  1325. /* Check for exceeding total eeprom len */
  1326. if (eeprom.offset + eeprom.len > total_len)
  1327. return -EINVAL;
  1328. data = kmalloc(PAGE_SIZE, GFP_USER);
  1329. if (!data)
  1330. return -ENOMEM;
  1331. bytes_remaining = eeprom.len;
  1332. while (bytes_remaining > 0) {
  1333. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1334. ret = getter(dev, &eeprom, data);
  1335. if (ret)
  1336. break;
  1337. if (copy_to_user(userbuf, data, eeprom.len)) {
  1338. ret = -EFAULT;
  1339. break;
  1340. }
  1341. userbuf += eeprom.len;
  1342. eeprom.offset += eeprom.len;
  1343. bytes_remaining -= eeprom.len;
  1344. }
  1345. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1346. eeprom.offset -= eeprom.len;
  1347. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1348. ret = -EFAULT;
  1349. kfree(data);
  1350. return ret;
  1351. }
  1352. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1353. {
  1354. const struct ethtool_ops *ops = dev->ethtool_ops;
  1355. if (!ops->get_eeprom || !ops->get_eeprom_len ||
  1356. !ops->get_eeprom_len(dev))
  1357. return -EOPNOTSUPP;
  1358. return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
  1359. ops->get_eeprom_len(dev));
  1360. }
  1361. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1362. {
  1363. struct ethtool_eeprom eeprom;
  1364. const struct ethtool_ops *ops = dev->ethtool_ops;
  1365. void __user *userbuf = useraddr + sizeof(eeprom);
  1366. u32 bytes_remaining;
  1367. u8 *data;
  1368. int ret = 0;
  1369. if (!ops->set_eeprom || !ops->get_eeprom_len ||
  1370. !ops->get_eeprom_len(dev))
  1371. return -EOPNOTSUPP;
  1372. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1373. return -EFAULT;
  1374. /* Check for wrap and zero */
  1375. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1376. return -EINVAL;
  1377. /* Check for exceeding total eeprom len */
  1378. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1379. return -EINVAL;
  1380. data = kmalloc(PAGE_SIZE, GFP_USER);
  1381. if (!data)
  1382. return -ENOMEM;
  1383. bytes_remaining = eeprom.len;
  1384. while (bytes_remaining > 0) {
  1385. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1386. if (copy_from_user(data, userbuf, eeprom.len)) {
  1387. ret = -EFAULT;
  1388. break;
  1389. }
  1390. ret = ops->set_eeprom(dev, &eeprom, data);
  1391. if (ret)
  1392. break;
  1393. userbuf += eeprom.len;
  1394. eeprom.offset += eeprom.len;
  1395. bytes_remaining -= eeprom.len;
  1396. }
  1397. kfree(data);
  1398. return ret;
  1399. }
  1400. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1401. void __user *useraddr)
  1402. {
  1403. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1404. if (!dev->ethtool_ops->get_coalesce)
  1405. return -EOPNOTSUPP;
  1406. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  1407. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1408. return -EFAULT;
  1409. return 0;
  1410. }
  1411. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1412. void __user *useraddr)
  1413. {
  1414. struct ethtool_coalesce coalesce;
  1415. if (!dev->ethtool_ops->set_coalesce)
  1416. return -EOPNOTSUPP;
  1417. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1418. return -EFAULT;
  1419. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  1420. }
  1421. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1422. {
  1423. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1424. if (!dev->ethtool_ops->get_ringparam)
  1425. return -EOPNOTSUPP;
  1426. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  1427. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1428. return -EFAULT;
  1429. return 0;
  1430. }
  1431. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1432. {
  1433. struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
  1434. if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
  1435. return -EOPNOTSUPP;
  1436. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1437. return -EFAULT;
  1438. dev->ethtool_ops->get_ringparam(dev, &max);
  1439. /* ensure new ring parameters are within the maximums */
  1440. if (ringparam.rx_pending > max.rx_max_pending ||
  1441. ringparam.rx_mini_pending > max.rx_mini_max_pending ||
  1442. ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
  1443. ringparam.tx_pending > max.tx_max_pending)
  1444. return -EINVAL;
  1445. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  1446. }
  1447. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  1448. void __user *useraddr)
  1449. {
  1450. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  1451. if (!dev->ethtool_ops->get_channels)
  1452. return -EOPNOTSUPP;
  1453. dev->ethtool_ops->get_channels(dev, &channels);
  1454. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  1455. return -EFAULT;
  1456. return 0;
  1457. }
  1458. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  1459. void __user *useraddr)
  1460. {
  1461. struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
  1462. u32 max_rx_in_use = 0;
  1463. if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
  1464. return -EOPNOTSUPP;
  1465. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  1466. return -EFAULT;
  1467. dev->ethtool_ops->get_channels(dev, &max);
  1468. /* ensure new counts are within the maximums */
  1469. if ((channels.rx_count > max.max_rx) ||
  1470. (channels.tx_count > max.max_tx) ||
  1471. (channels.combined_count > max.max_combined) ||
  1472. (channels.other_count > max.max_other))
  1473. return -EINVAL;
  1474. /* ensure the new Rx count fits within the configured Rx flow
  1475. * indirection table settings */
  1476. if (netif_is_rxfh_configured(dev) &&
  1477. !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
  1478. (channels.combined_count + channels.rx_count) <= max_rx_in_use)
  1479. return -EINVAL;
  1480. return dev->ethtool_ops->set_channels(dev, &channels);
  1481. }
  1482. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1483. {
  1484. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  1485. if (!dev->ethtool_ops->get_pauseparam)
  1486. return -EOPNOTSUPP;
  1487. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1488. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1489. return -EFAULT;
  1490. return 0;
  1491. }
  1492. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1493. {
  1494. struct ethtool_pauseparam pauseparam;
  1495. if (!dev->ethtool_ops->set_pauseparam)
  1496. return -EOPNOTSUPP;
  1497. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1498. return -EFAULT;
  1499. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1500. }
  1501. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1502. {
  1503. struct ethtool_test test;
  1504. const struct ethtool_ops *ops = dev->ethtool_ops;
  1505. u64 *data;
  1506. int ret, test_len;
  1507. if (!ops->self_test || !ops->get_sset_count)
  1508. return -EOPNOTSUPP;
  1509. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1510. if (test_len < 0)
  1511. return test_len;
  1512. WARN_ON(test_len == 0);
  1513. if (copy_from_user(&test, useraddr, sizeof(test)))
  1514. return -EFAULT;
  1515. test.len = test_len;
  1516. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  1517. if (!data)
  1518. return -ENOMEM;
  1519. ops->self_test(dev, &test, data);
  1520. ret = -EFAULT;
  1521. if (copy_to_user(useraddr, &test, sizeof(test)))
  1522. goto out;
  1523. useraddr += sizeof(test);
  1524. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  1525. goto out;
  1526. ret = 0;
  1527. out:
  1528. kfree(data);
  1529. return ret;
  1530. }
  1531. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1532. {
  1533. struct ethtool_gstrings gstrings;
  1534. u8 *data;
  1535. int ret;
  1536. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1537. return -EFAULT;
  1538. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1539. if (ret < 0)
  1540. return ret;
  1541. if (ret > S32_MAX / ETH_GSTRING_LEN)
  1542. return -ENOMEM;
  1543. WARN_ON_ONCE(!ret);
  1544. gstrings.len = ret;
  1545. data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
  1546. if (gstrings.len && !data)
  1547. return -ENOMEM;
  1548. __ethtool_get_strings(dev, gstrings.string_set, data);
  1549. ret = -EFAULT;
  1550. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1551. goto out;
  1552. useraddr += sizeof(gstrings);
  1553. if (gstrings.len &&
  1554. copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1555. goto out;
  1556. ret = 0;
  1557. out:
  1558. vfree(data);
  1559. return ret;
  1560. }
  1561. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1562. {
  1563. struct ethtool_value id;
  1564. static bool busy;
  1565. const struct ethtool_ops *ops = dev->ethtool_ops;
  1566. int rc;
  1567. if (!ops->set_phys_id)
  1568. return -EOPNOTSUPP;
  1569. if (busy)
  1570. return -EBUSY;
  1571. if (copy_from_user(&id, useraddr, sizeof(id)))
  1572. return -EFAULT;
  1573. rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  1574. if (rc < 0)
  1575. return rc;
  1576. /* Drop the RTNL lock while waiting, but prevent reentry or
  1577. * removal of the device.
  1578. */
  1579. busy = true;
  1580. dev_hold(dev);
  1581. rtnl_unlock();
  1582. if (rc == 0) {
  1583. /* Driver will handle this itself */
  1584. schedule_timeout_interruptible(
  1585. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  1586. } else {
  1587. /* Driver expects to be called at twice the frequency in rc */
  1588. int n = rc * 2, i, interval = HZ / n;
  1589. /* Count down seconds */
  1590. do {
  1591. /* Count down iterations per second */
  1592. i = n;
  1593. do {
  1594. rtnl_lock();
  1595. rc = ops->set_phys_id(dev,
  1596. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  1597. rtnl_unlock();
  1598. if (rc)
  1599. break;
  1600. schedule_timeout_interruptible(interval);
  1601. } while (!signal_pending(current) && --i != 0);
  1602. } while (!signal_pending(current) &&
  1603. (id.data == 0 || --id.data != 0));
  1604. }
  1605. rtnl_lock();
  1606. dev_put(dev);
  1607. busy = false;
  1608. (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  1609. return rc;
  1610. }
  1611. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1612. {
  1613. struct ethtool_stats stats;
  1614. const struct ethtool_ops *ops = dev->ethtool_ops;
  1615. u64 *data;
  1616. int ret, n_stats;
  1617. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1618. return -EOPNOTSUPP;
  1619. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1620. if (n_stats < 0)
  1621. return n_stats;
  1622. if (n_stats > S32_MAX / sizeof(u64))
  1623. return -ENOMEM;
  1624. WARN_ON_ONCE(!n_stats);
  1625. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1626. return -EFAULT;
  1627. stats.n_stats = n_stats;
  1628. data = vzalloc(n_stats * sizeof(u64));
  1629. if (n_stats && !data)
  1630. return -ENOMEM;
  1631. ops->get_ethtool_stats(dev, &stats, data);
  1632. ret = -EFAULT;
  1633. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1634. goto out;
  1635. useraddr += sizeof(stats);
  1636. if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
  1637. goto out;
  1638. ret = 0;
  1639. out:
  1640. vfree(data);
  1641. return ret;
  1642. }
  1643. static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
  1644. {
  1645. struct ethtool_stats stats;
  1646. struct phy_device *phydev = dev->phydev;
  1647. u64 *data;
  1648. int ret, n_stats;
  1649. if (!phydev)
  1650. return -EOPNOTSUPP;
  1651. n_stats = phy_get_sset_count(phydev);
  1652. if (n_stats < 0)
  1653. return n_stats;
  1654. if (n_stats > S32_MAX / sizeof(u64))
  1655. return -ENOMEM;
  1656. WARN_ON_ONCE(!n_stats);
  1657. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1658. return -EFAULT;
  1659. stats.n_stats = n_stats;
  1660. data = vzalloc(n_stats * sizeof(u64));
  1661. if (n_stats && !data)
  1662. return -ENOMEM;
  1663. mutex_lock(&phydev->lock);
  1664. phydev->drv->get_stats(phydev, &stats, data);
  1665. mutex_unlock(&phydev->lock);
  1666. ret = -EFAULT;
  1667. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1668. goto out;
  1669. useraddr += sizeof(stats);
  1670. if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
  1671. goto out;
  1672. ret = 0;
  1673. out:
  1674. vfree(data);
  1675. return ret;
  1676. }
  1677. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1678. {
  1679. struct ethtool_perm_addr epaddr;
  1680. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1681. return -EFAULT;
  1682. if (epaddr.size < dev->addr_len)
  1683. return -ETOOSMALL;
  1684. epaddr.size = dev->addr_len;
  1685. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1686. return -EFAULT;
  1687. useraddr += sizeof(epaddr);
  1688. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1689. return -EFAULT;
  1690. return 0;
  1691. }
  1692. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1693. u32 cmd, u32 (*actor)(struct net_device *))
  1694. {
  1695. struct ethtool_value edata = { .cmd = cmd };
  1696. if (!actor)
  1697. return -EOPNOTSUPP;
  1698. edata.data = actor(dev);
  1699. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1700. return -EFAULT;
  1701. return 0;
  1702. }
  1703. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1704. void (*actor)(struct net_device *, u32))
  1705. {
  1706. struct ethtool_value edata;
  1707. if (!actor)
  1708. return -EOPNOTSUPP;
  1709. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1710. return -EFAULT;
  1711. actor(dev, edata.data);
  1712. return 0;
  1713. }
  1714. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1715. int (*actor)(struct net_device *, u32))
  1716. {
  1717. struct ethtool_value edata;
  1718. if (!actor)
  1719. return -EOPNOTSUPP;
  1720. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1721. return -EFAULT;
  1722. return actor(dev, edata.data);
  1723. }
  1724. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  1725. char __user *useraddr)
  1726. {
  1727. struct ethtool_flash efl;
  1728. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  1729. return -EFAULT;
  1730. if (!dev->ethtool_ops->flash_device)
  1731. return -EOPNOTSUPP;
  1732. efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
  1733. return dev->ethtool_ops->flash_device(dev, &efl);
  1734. }
  1735. static int ethtool_set_dump(struct net_device *dev,
  1736. void __user *useraddr)
  1737. {
  1738. struct ethtool_dump dump;
  1739. if (!dev->ethtool_ops->set_dump)
  1740. return -EOPNOTSUPP;
  1741. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1742. return -EFAULT;
  1743. return dev->ethtool_ops->set_dump(dev, &dump);
  1744. }
  1745. static int ethtool_get_dump_flag(struct net_device *dev,
  1746. void __user *useraddr)
  1747. {
  1748. int ret;
  1749. struct ethtool_dump dump;
  1750. const struct ethtool_ops *ops = dev->ethtool_ops;
  1751. if (!ops->get_dump_flag)
  1752. return -EOPNOTSUPP;
  1753. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1754. return -EFAULT;
  1755. ret = ops->get_dump_flag(dev, &dump);
  1756. if (ret)
  1757. return ret;
  1758. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  1759. return -EFAULT;
  1760. return 0;
  1761. }
  1762. static int ethtool_get_dump_data(struct net_device *dev,
  1763. void __user *useraddr)
  1764. {
  1765. int ret;
  1766. __u32 len;
  1767. struct ethtool_dump dump, tmp;
  1768. const struct ethtool_ops *ops = dev->ethtool_ops;
  1769. void *data = NULL;
  1770. if (!ops->get_dump_data || !ops->get_dump_flag)
  1771. return -EOPNOTSUPP;
  1772. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1773. return -EFAULT;
  1774. memset(&tmp, 0, sizeof(tmp));
  1775. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  1776. ret = ops->get_dump_flag(dev, &tmp);
  1777. if (ret)
  1778. return ret;
  1779. len = min(tmp.len, dump.len);
  1780. if (!len)
  1781. return -EFAULT;
  1782. /* Don't ever let the driver think there's more space available
  1783. * than it requested with .get_dump_flag().
  1784. */
  1785. dump.len = len;
  1786. /* Always allocate enough space to hold the whole thing so that the
  1787. * driver does not need to check the length and bother with partial
  1788. * dumping.
  1789. */
  1790. data = vzalloc(tmp.len);
  1791. if (!data)
  1792. return -ENOMEM;
  1793. ret = ops->get_dump_data(dev, &dump, data);
  1794. if (ret)
  1795. goto out;
  1796. /* There are two sane possibilities:
  1797. * 1. The driver's .get_dump_data() does not touch dump.len.
  1798. * 2. Or it may set dump.len to how much it really writes, which
  1799. * should be tmp.len (or len if it can do a partial dump).
  1800. * In any case respond to userspace with the actual length of data
  1801. * it's receiving.
  1802. */
  1803. WARN_ON(dump.len != len && dump.len != tmp.len);
  1804. dump.len = len;
  1805. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1806. ret = -EFAULT;
  1807. goto out;
  1808. }
  1809. useraddr += offsetof(struct ethtool_dump, data);
  1810. if (copy_to_user(useraddr, data, len))
  1811. ret = -EFAULT;
  1812. out:
  1813. vfree(data);
  1814. return ret;
  1815. }
  1816. static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
  1817. {
  1818. int err = 0;
  1819. struct ethtool_ts_info info;
  1820. const struct ethtool_ops *ops = dev->ethtool_ops;
  1821. struct phy_device *phydev = dev->phydev;
  1822. memset(&info, 0, sizeof(info));
  1823. info.cmd = ETHTOOL_GET_TS_INFO;
  1824. if (phydev && phydev->drv && phydev->drv->ts_info) {
  1825. err = phydev->drv->ts_info(phydev, &info);
  1826. } else if (ops->get_ts_info) {
  1827. err = ops->get_ts_info(dev, &info);
  1828. } else {
  1829. info.so_timestamping =
  1830. SOF_TIMESTAMPING_RX_SOFTWARE |
  1831. SOF_TIMESTAMPING_SOFTWARE;
  1832. info.phc_index = -1;
  1833. }
  1834. if (err)
  1835. return err;
  1836. if (copy_to_user(useraddr, &info, sizeof(info)))
  1837. err = -EFAULT;
  1838. return err;
  1839. }
  1840. static int __ethtool_get_module_info(struct net_device *dev,
  1841. struct ethtool_modinfo *modinfo)
  1842. {
  1843. const struct ethtool_ops *ops = dev->ethtool_ops;
  1844. struct phy_device *phydev = dev->phydev;
  1845. if (dev->sfp_bus)
  1846. return sfp_get_module_info(dev->sfp_bus, modinfo);
  1847. if (phydev && phydev->drv && phydev->drv->module_info)
  1848. return phydev->drv->module_info(phydev, modinfo);
  1849. if (ops->get_module_info)
  1850. return ops->get_module_info(dev, modinfo);
  1851. return -EOPNOTSUPP;
  1852. }
  1853. static int ethtool_get_module_info(struct net_device *dev,
  1854. void __user *useraddr)
  1855. {
  1856. int ret;
  1857. struct ethtool_modinfo modinfo;
  1858. if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
  1859. return -EFAULT;
  1860. ret = __ethtool_get_module_info(dev, &modinfo);
  1861. if (ret)
  1862. return ret;
  1863. if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
  1864. return -EFAULT;
  1865. return 0;
  1866. }
  1867. static int __ethtool_get_module_eeprom(struct net_device *dev,
  1868. struct ethtool_eeprom *ee, u8 *data)
  1869. {
  1870. const struct ethtool_ops *ops = dev->ethtool_ops;
  1871. struct phy_device *phydev = dev->phydev;
  1872. if (dev->sfp_bus)
  1873. return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
  1874. if (phydev && phydev->drv && phydev->drv->module_eeprom)
  1875. return phydev->drv->module_eeprom(phydev, ee, data);
  1876. if (ops->get_module_eeprom)
  1877. return ops->get_module_eeprom(dev, ee, data);
  1878. return -EOPNOTSUPP;
  1879. }
  1880. static int ethtool_get_module_eeprom(struct net_device *dev,
  1881. void __user *useraddr)
  1882. {
  1883. int ret;
  1884. struct ethtool_modinfo modinfo;
  1885. ret = __ethtool_get_module_info(dev, &modinfo);
  1886. if (ret)
  1887. return ret;
  1888. return ethtool_get_any_eeprom(dev, useraddr,
  1889. __ethtool_get_module_eeprom,
  1890. modinfo.eeprom_len);
  1891. }
  1892. static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
  1893. {
  1894. switch (tuna->id) {
  1895. case ETHTOOL_RX_COPYBREAK:
  1896. case ETHTOOL_TX_COPYBREAK:
  1897. if (tuna->len != sizeof(u32) ||
  1898. tuna->type_id != ETHTOOL_TUNABLE_U32)
  1899. return -EINVAL;
  1900. break;
  1901. case ETHTOOL_PFC_PREVENTION_TOUT:
  1902. if (tuna->len != sizeof(u16) ||
  1903. tuna->type_id != ETHTOOL_TUNABLE_U16)
  1904. return -EINVAL;
  1905. break;
  1906. default:
  1907. return -EINVAL;
  1908. }
  1909. return 0;
  1910. }
  1911. static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
  1912. {
  1913. int ret;
  1914. struct ethtool_tunable tuna;
  1915. const struct ethtool_ops *ops = dev->ethtool_ops;
  1916. void *data;
  1917. if (!ops->get_tunable)
  1918. return -EOPNOTSUPP;
  1919. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1920. return -EFAULT;
  1921. ret = ethtool_tunable_valid(&tuna);
  1922. if (ret)
  1923. return ret;
  1924. data = kmalloc(tuna.len, GFP_USER);
  1925. if (!data)
  1926. return -ENOMEM;
  1927. ret = ops->get_tunable(dev, &tuna, data);
  1928. if (ret)
  1929. goto out;
  1930. useraddr += sizeof(tuna);
  1931. ret = -EFAULT;
  1932. if (copy_to_user(useraddr, data, tuna.len))
  1933. goto out;
  1934. ret = 0;
  1935. out:
  1936. kfree(data);
  1937. return ret;
  1938. }
  1939. static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
  1940. {
  1941. int ret;
  1942. struct ethtool_tunable tuna;
  1943. const struct ethtool_ops *ops = dev->ethtool_ops;
  1944. void *data;
  1945. if (!ops->set_tunable)
  1946. return -EOPNOTSUPP;
  1947. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1948. return -EFAULT;
  1949. ret = ethtool_tunable_valid(&tuna);
  1950. if (ret)
  1951. return ret;
  1952. useraddr += sizeof(tuna);
  1953. data = memdup_user(useraddr, tuna.len);
  1954. if (IS_ERR(data))
  1955. return PTR_ERR(data);
  1956. ret = ops->set_tunable(dev, &tuna, data);
  1957. kfree(data);
  1958. return ret;
  1959. }
  1960. static int ethtool_get_per_queue_coalesce(struct net_device *dev,
  1961. void __user *useraddr,
  1962. struct ethtool_per_queue_op *per_queue_opt)
  1963. {
  1964. u32 bit;
  1965. int ret;
  1966. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  1967. if (!dev->ethtool_ops->get_per_queue_coalesce)
  1968. return -EOPNOTSUPP;
  1969. useraddr += sizeof(*per_queue_opt);
  1970. bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
  1971. MAX_NUM_QUEUE);
  1972. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  1973. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1974. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
  1975. if (ret != 0)
  1976. return ret;
  1977. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1978. return -EFAULT;
  1979. useraddr += sizeof(coalesce);
  1980. }
  1981. return 0;
  1982. }
  1983. static int ethtool_set_per_queue_coalesce(struct net_device *dev,
  1984. void __user *useraddr,
  1985. struct ethtool_per_queue_op *per_queue_opt)
  1986. {
  1987. u32 bit;
  1988. int i, ret = 0;
  1989. int n_queue;
  1990. struct ethtool_coalesce *backup = NULL, *tmp = NULL;
  1991. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  1992. if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
  1993. (!dev->ethtool_ops->get_per_queue_coalesce))
  1994. return -EOPNOTSUPP;
  1995. useraddr += sizeof(*per_queue_opt);
  1996. bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
  1997. n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
  1998. tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
  1999. if (!backup)
  2000. return -ENOMEM;
  2001. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  2002. struct ethtool_coalesce coalesce;
  2003. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
  2004. if (ret != 0)
  2005. goto roll_back;
  2006. tmp++;
  2007. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
  2008. ret = -EFAULT;
  2009. goto roll_back;
  2010. }
  2011. ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
  2012. if (ret != 0)
  2013. goto roll_back;
  2014. useraddr += sizeof(coalesce);
  2015. }
  2016. roll_back:
  2017. if (ret != 0) {
  2018. tmp = backup;
  2019. for_each_set_bit(i, queue_mask, bit) {
  2020. dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
  2021. tmp++;
  2022. }
  2023. }
  2024. kfree(backup);
  2025. return ret;
  2026. }
  2027. static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
  2028. {
  2029. struct ethtool_per_queue_op per_queue_opt;
  2030. if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
  2031. return -EFAULT;
  2032. switch (per_queue_opt.sub_command) {
  2033. case ETHTOOL_GCOALESCE:
  2034. return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  2035. case ETHTOOL_SCOALESCE:
  2036. return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  2037. default:
  2038. return -EOPNOTSUPP;
  2039. };
  2040. }
  2041. static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
  2042. {
  2043. switch (tuna->id) {
  2044. case ETHTOOL_PHY_DOWNSHIFT:
  2045. if (tuna->len != sizeof(u8) ||
  2046. tuna->type_id != ETHTOOL_TUNABLE_U8)
  2047. return -EINVAL;
  2048. break;
  2049. default:
  2050. return -EINVAL;
  2051. }
  2052. return 0;
  2053. }
  2054. static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
  2055. {
  2056. int ret;
  2057. struct ethtool_tunable tuna;
  2058. struct phy_device *phydev = dev->phydev;
  2059. void *data;
  2060. if (!(phydev && phydev->drv && phydev->drv->get_tunable))
  2061. return -EOPNOTSUPP;
  2062. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2063. return -EFAULT;
  2064. ret = ethtool_phy_tunable_valid(&tuna);
  2065. if (ret)
  2066. return ret;
  2067. data = kmalloc(tuna.len, GFP_USER);
  2068. if (!data)
  2069. return -ENOMEM;
  2070. mutex_lock(&phydev->lock);
  2071. ret = phydev->drv->get_tunable(phydev, &tuna, data);
  2072. mutex_unlock(&phydev->lock);
  2073. if (ret)
  2074. goto out;
  2075. useraddr += sizeof(tuna);
  2076. ret = -EFAULT;
  2077. if (copy_to_user(useraddr, data, tuna.len))
  2078. goto out;
  2079. ret = 0;
  2080. out:
  2081. kfree(data);
  2082. return ret;
  2083. }
  2084. static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
  2085. {
  2086. int ret;
  2087. struct ethtool_tunable tuna;
  2088. struct phy_device *phydev = dev->phydev;
  2089. void *data;
  2090. if (!(phydev && phydev->drv && phydev->drv->set_tunable))
  2091. return -EOPNOTSUPP;
  2092. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2093. return -EFAULT;
  2094. ret = ethtool_phy_tunable_valid(&tuna);
  2095. if (ret)
  2096. return ret;
  2097. useraddr += sizeof(tuna);
  2098. data = memdup_user(useraddr, tuna.len);
  2099. if (IS_ERR(data))
  2100. return PTR_ERR(data);
  2101. mutex_lock(&phydev->lock);
  2102. ret = phydev->drv->set_tunable(phydev, &tuna, data);
  2103. mutex_unlock(&phydev->lock);
  2104. kfree(data);
  2105. return ret;
  2106. }
  2107. static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
  2108. {
  2109. struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
  2110. int rc;
  2111. if (!dev->ethtool_ops->get_fecparam)
  2112. return -EOPNOTSUPP;
  2113. rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
  2114. if (rc)
  2115. return rc;
  2116. if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
  2117. return -EFAULT;
  2118. return 0;
  2119. }
  2120. static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
  2121. {
  2122. struct ethtool_fecparam fecparam;
  2123. if (!dev->ethtool_ops->set_fecparam)
  2124. return -EOPNOTSUPP;
  2125. if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
  2126. return -EFAULT;
  2127. return dev->ethtool_ops->set_fecparam(dev, &fecparam);
  2128. }
  2129. /* The main entry point in this file. Called from net/core/dev_ioctl.c */
  2130. int dev_ethtool(struct net *net, struct ifreq *ifr)
  2131. {
  2132. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  2133. void __user *useraddr = ifr->ifr_data;
  2134. u32 ethcmd, sub_cmd;
  2135. int rc;
  2136. netdev_features_t old_features;
  2137. if (!dev || !netif_device_present(dev))
  2138. return -ENODEV;
  2139. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  2140. return -EFAULT;
  2141. if (ethcmd == ETHTOOL_PERQUEUE) {
  2142. if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
  2143. return -EFAULT;
  2144. } else {
  2145. sub_cmd = ethcmd;
  2146. }
  2147. /* Allow some commands to be done by anyone */
  2148. switch (sub_cmd) {
  2149. case ETHTOOL_GSET:
  2150. case ETHTOOL_GDRVINFO:
  2151. case ETHTOOL_GMSGLVL:
  2152. case ETHTOOL_GLINK:
  2153. case ETHTOOL_GCOALESCE:
  2154. case ETHTOOL_GRINGPARAM:
  2155. case ETHTOOL_GPAUSEPARAM:
  2156. case ETHTOOL_GRXCSUM:
  2157. case ETHTOOL_GTXCSUM:
  2158. case ETHTOOL_GSG:
  2159. case ETHTOOL_GSSET_INFO:
  2160. case ETHTOOL_GSTRINGS:
  2161. case ETHTOOL_GSTATS:
  2162. case ETHTOOL_GPHYSTATS:
  2163. case ETHTOOL_GTSO:
  2164. case ETHTOOL_GPERMADDR:
  2165. case ETHTOOL_GGSO:
  2166. case ETHTOOL_GGRO:
  2167. case ETHTOOL_GFLAGS:
  2168. case ETHTOOL_GPFLAGS:
  2169. case ETHTOOL_GRXFH:
  2170. case ETHTOOL_GRXRINGS:
  2171. case ETHTOOL_GRXCLSRLCNT:
  2172. case ETHTOOL_GRXCLSRULE:
  2173. case ETHTOOL_GRXCLSRLALL:
  2174. case ETHTOOL_GRXFHINDIR:
  2175. case ETHTOOL_GRSSH:
  2176. case ETHTOOL_GFEATURES:
  2177. case ETHTOOL_GCHANNELS:
  2178. case ETHTOOL_GET_TS_INFO:
  2179. case ETHTOOL_GEEE:
  2180. case ETHTOOL_GTUNABLE:
  2181. case ETHTOOL_PHY_GTUNABLE:
  2182. case ETHTOOL_GLINKSETTINGS:
  2183. case ETHTOOL_GFECPARAM:
  2184. break;
  2185. default:
  2186. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2187. return -EPERM;
  2188. }
  2189. if (dev->ethtool_ops->begin) {
  2190. rc = dev->ethtool_ops->begin(dev);
  2191. if (rc < 0)
  2192. return rc;
  2193. }
  2194. old_features = dev->features;
  2195. switch (ethcmd) {
  2196. case ETHTOOL_GSET:
  2197. rc = ethtool_get_settings(dev, useraddr);
  2198. break;
  2199. case ETHTOOL_SSET:
  2200. rc = ethtool_set_settings(dev, useraddr);
  2201. break;
  2202. case ETHTOOL_GDRVINFO:
  2203. rc = ethtool_get_drvinfo(dev, useraddr);
  2204. break;
  2205. case ETHTOOL_GREGS:
  2206. rc = ethtool_get_regs(dev, useraddr);
  2207. break;
  2208. case ETHTOOL_GWOL:
  2209. rc = ethtool_get_wol(dev, useraddr);
  2210. break;
  2211. case ETHTOOL_SWOL:
  2212. rc = ethtool_set_wol(dev, useraddr);
  2213. break;
  2214. case ETHTOOL_GMSGLVL:
  2215. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2216. dev->ethtool_ops->get_msglevel);
  2217. break;
  2218. case ETHTOOL_SMSGLVL:
  2219. rc = ethtool_set_value_void(dev, useraddr,
  2220. dev->ethtool_ops->set_msglevel);
  2221. break;
  2222. case ETHTOOL_GEEE:
  2223. rc = ethtool_get_eee(dev, useraddr);
  2224. break;
  2225. case ETHTOOL_SEEE:
  2226. rc = ethtool_set_eee(dev, useraddr);
  2227. break;
  2228. case ETHTOOL_NWAY_RST:
  2229. rc = ethtool_nway_reset(dev);
  2230. break;
  2231. case ETHTOOL_GLINK:
  2232. rc = ethtool_get_link(dev, useraddr);
  2233. break;
  2234. case ETHTOOL_GEEPROM:
  2235. rc = ethtool_get_eeprom(dev, useraddr);
  2236. break;
  2237. case ETHTOOL_SEEPROM:
  2238. rc = ethtool_set_eeprom(dev, useraddr);
  2239. break;
  2240. case ETHTOOL_GCOALESCE:
  2241. rc = ethtool_get_coalesce(dev, useraddr);
  2242. break;
  2243. case ETHTOOL_SCOALESCE:
  2244. rc = ethtool_set_coalesce(dev, useraddr);
  2245. break;
  2246. case ETHTOOL_GRINGPARAM:
  2247. rc = ethtool_get_ringparam(dev, useraddr);
  2248. break;
  2249. case ETHTOOL_SRINGPARAM:
  2250. rc = ethtool_set_ringparam(dev, useraddr);
  2251. break;
  2252. case ETHTOOL_GPAUSEPARAM:
  2253. rc = ethtool_get_pauseparam(dev, useraddr);
  2254. break;
  2255. case ETHTOOL_SPAUSEPARAM:
  2256. rc = ethtool_set_pauseparam(dev, useraddr);
  2257. break;
  2258. case ETHTOOL_TEST:
  2259. rc = ethtool_self_test(dev, useraddr);
  2260. break;
  2261. case ETHTOOL_GSTRINGS:
  2262. rc = ethtool_get_strings(dev, useraddr);
  2263. break;
  2264. case ETHTOOL_PHYS_ID:
  2265. rc = ethtool_phys_id(dev, useraddr);
  2266. break;
  2267. case ETHTOOL_GSTATS:
  2268. rc = ethtool_get_stats(dev, useraddr);
  2269. break;
  2270. case ETHTOOL_GPERMADDR:
  2271. rc = ethtool_get_perm_addr(dev, useraddr);
  2272. break;
  2273. case ETHTOOL_GFLAGS:
  2274. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2275. __ethtool_get_flags);
  2276. break;
  2277. case ETHTOOL_SFLAGS:
  2278. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  2279. break;
  2280. case ETHTOOL_GPFLAGS:
  2281. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2282. dev->ethtool_ops->get_priv_flags);
  2283. break;
  2284. case ETHTOOL_SPFLAGS:
  2285. rc = ethtool_set_value(dev, useraddr,
  2286. dev->ethtool_ops->set_priv_flags);
  2287. break;
  2288. case ETHTOOL_GRXFH:
  2289. case ETHTOOL_GRXRINGS:
  2290. case ETHTOOL_GRXCLSRLCNT:
  2291. case ETHTOOL_GRXCLSRULE:
  2292. case ETHTOOL_GRXCLSRLALL:
  2293. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  2294. break;
  2295. case ETHTOOL_SRXFH:
  2296. case ETHTOOL_SRXCLSRLDEL:
  2297. case ETHTOOL_SRXCLSRLINS:
  2298. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  2299. break;
  2300. case ETHTOOL_FLASHDEV:
  2301. rc = ethtool_flash_device(dev, useraddr);
  2302. break;
  2303. case ETHTOOL_RESET:
  2304. rc = ethtool_reset(dev, useraddr);
  2305. break;
  2306. case ETHTOOL_GSSET_INFO:
  2307. rc = ethtool_get_sset_info(dev, useraddr);
  2308. break;
  2309. case ETHTOOL_GRXFHINDIR:
  2310. rc = ethtool_get_rxfh_indir(dev, useraddr);
  2311. break;
  2312. case ETHTOOL_SRXFHINDIR:
  2313. rc = ethtool_set_rxfh_indir(dev, useraddr);
  2314. break;
  2315. case ETHTOOL_GRSSH:
  2316. rc = ethtool_get_rxfh(dev, useraddr);
  2317. break;
  2318. case ETHTOOL_SRSSH:
  2319. rc = ethtool_set_rxfh(dev, useraddr);
  2320. break;
  2321. case ETHTOOL_GFEATURES:
  2322. rc = ethtool_get_features(dev, useraddr);
  2323. break;
  2324. case ETHTOOL_SFEATURES:
  2325. rc = ethtool_set_features(dev, useraddr);
  2326. break;
  2327. case ETHTOOL_GTXCSUM:
  2328. case ETHTOOL_GRXCSUM:
  2329. case ETHTOOL_GSG:
  2330. case ETHTOOL_GTSO:
  2331. case ETHTOOL_GGSO:
  2332. case ETHTOOL_GGRO:
  2333. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  2334. break;
  2335. case ETHTOOL_STXCSUM:
  2336. case ETHTOOL_SRXCSUM:
  2337. case ETHTOOL_SSG:
  2338. case ETHTOOL_STSO:
  2339. case ETHTOOL_SGSO:
  2340. case ETHTOOL_SGRO:
  2341. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  2342. break;
  2343. case ETHTOOL_GCHANNELS:
  2344. rc = ethtool_get_channels(dev, useraddr);
  2345. break;
  2346. case ETHTOOL_SCHANNELS:
  2347. rc = ethtool_set_channels(dev, useraddr);
  2348. break;
  2349. case ETHTOOL_SET_DUMP:
  2350. rc = ethtool_set_dump(dev, useraddr);
  2351. break;
  2352. case ETHTOOL_GET_DUMP_FLAG:
  2353. rc = ethtool_get_dump_flag(dev, useraddr);
  2354. break;
  2355. case ETHTOOL_GET_DUMP_DATA:
  2356. rc = ethtool_get_dump_data(dev, useraddr);
  2357. break;
  2358. case ETHTOOL_GET_TS_INFO:
  2359. rc = ethtool_get_ts_info(dev, useraddr);
  2360. break;
  2361. case ETHTOOL_GMODULEINFO:
  2362. rc = ethtool_get_module_info(dev, useraddr);
  2363. break;
  2364. case ETHTOOL_GMODULEEEPROM:
  2365. rc = ethtool_get_module_eeprom(dev, useraddr);
  2366. break;
  2367. case ETHTOOL_GTUNABLE:
  2368. rc = ethtool_get_tunable(dev, useraddr);
  2369. break;
  2370. case ETHTOOL_STUNABLE:
  2371. rc = ethtool_set_tunable(dev, useraddr);
  2372. break;
  2373. case ETHTOOL_GPHYSTATS:
  2374. rc = ethtool_get_phy_stats(dev, useraddr);
  2375. break;
  2376. case ETHTOOL_PERQUEUE:
  2377. rc = ethtool_set_per_queue(dev, useraddr);
  2378. break;
  2379. case ETHTOOL_GLINKSETTINGS:
  2380. rc = ethtool_get_link_ksettings(dev, useraddr);
  2381. break;
  2382. case ETHTOOL_SLINKSETTINGS:
  2383. rc = ethtool_set_link_ksettings(dev, useraddr);
  2384. break;
  2385. case ETHTOOL_PHY_GTUNABLE:
  2386. rc = get_phy_tunable(dev, useraddr);
  2387. break;
  2388. case ETHTOOL_PHY_STUNABLE:
  2389. rc = set_phy_tunable(dev, useraddr);
  2390. break;
  2391. case ETHTOOL_GFECPARAM:
  2392. rc = ethtool_get_fecparam(dev, useraddr);
  2393. break;
  2394. case ETHTOOL_SFECPARAM:
  2395. rc = ethtool_set_fecparam(dev, useraddr);
  2396. break;
  2397. default:
  2398. rc = -EOPNOTSUPP;
  2399. }
  2400. if (dev->ethtool_ops->complete)
  2401. dev->ethtool_ops->complete(dev);
  2402. if (old_features != dev->features)
  2403. netdev_features_change(dev);
  2404. return rc;
  2405. }