ethtool.c 71 KB

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