ethtool.c 71 KB

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