ioctl.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/core/ethtool.c - Ethtool ioctl handler
  4. * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
  5. *
  6. * This file is where we call all the ethtool_ops commands to get
  7. * the information ethtool needs.
  8. */
  9. #include <linux/compat.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/capability.h>
  14. #include <linux/errno.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/net_tstamp.h>
  18. #include <linux/phy.h>
  19. #include <linux/bitops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/sfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/net.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/utsname.h>
  29. #include <net/devlink.h>
  30. #include <net/ipv6.h>
  31. #include <net/xdp_sock_drv.h>
  32. #include <net/flow_offload.h>
  33. #include <linux/ethtool_netlink.h>
  34. #include "common.h"
  35. /* State held across locks and calls for commands which have devlink fallback */
  36. struct ethtool_devlink_compat {
  37. struct devlink *devlink;
  38. union {
  39. struct ethtool_flash efl;
  40. struct ethtool_drvinfo info;
  41. };
  42. };
  43. static struct devlink *netdev_to_devlink_get(struct net_device *dev)
  44. {
  45. if (!dev->devlink_port)
  46. return NULL;
  47. return devlink_try_get(dev->devlink_port->devlink);
  48. }
  49. /*
  50. * Some useful ethtool_ops methods that're device independent.
  51. * If we find that all drivers want to do the same thing here,
  52. * we can turn these into dev_() function calls.
  53. */
  54. u32 ethtool_op_get_link(struct net_device *dev)
  55. {
  56. /* Synchronize carrier state with link watch, see also rtnl_getlink() */
  57. linkwatch_sync_dev(dev);
  58. return netif_carrier_ok(dev) ? 1 : 0;
  59. }
  60. EXPORT_SYMBOL(ethtool_op_get_link);
  61. int ethtool_op_get_ts_info(struct net_device *dev,
  62. struct kernel_ethtool_ts_info *info)
  63. {
  64. info->so_timestamping =
  65. SOF_TIMESTAMPING_TX_SOFTWARE |
  66. SOF_TIMESTAMPING_RX_SOFTWARE |
  67. SOF_TIMESTAMPING_SOFTWARE;
  68. info->phc_index = -1;
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(ethtool_op_get_ts_info);
  72. /* Handlers for each ethtool command */
  73. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  74. {
  75. struct ethtool_gfeatures cmd = {
  76. .cmd = ETHTOOL_GFEATURES,
  77. .size = ETHTOOL_DEV_FEATURE_WORDS,
  78. };
  79. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  80. u32 __user *sizeaddr;
  81. u32 copy_size;
  82. int i;
  83. /* in case feature bits run out again */
  84. BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
  85. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  86. features[i].available = (u32)(dev->hw_features >> (32 * i));
  87. features[i].requested = (u32)(dev->wanted_features >> (32 * i));
  88. features[i].active = (u32)(dev->features >> (32 * i));
  89. features[i].never_changed =
  90. (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
  91. }
  92. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  93. if (get_user(copy_size, sizeaddr))
  94. return -EFAULT;
  95. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  96. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  97. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  98. return -EFAULT;
  99. useraddr += sizeof(cmd);
  100. if (copy_to_user(useraddr, features,
  101. array_size(copy_size, sizeof(*features))))
  102. return -EFAULT;
  103. return 0;
  104. }
  105. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  106. {
  107. struct ethtool_sfeatures cmd;
  108. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  109. netdev_features_t wanted = 0, valid = 0;
  110. int i, ret = 0;
  111. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  112. return -EFAULT;
  113. useraddr += sizeof(cmd);
  114. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  115. return -EINVAL;
  116. if (copy_from_user(features, useraddr, sizeof(features)))
  117. return -EFAULT;
  118. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  119. valid |= (netdev_features_t)features[i].valid << (32 * i);
  120. wanted |= (netdev_features_t)features[i].requested << (32 * i);
  121. }
  122. if (valid & ~NETIF_F_ETHTOOL_BITS)
  123. return -EINVAL;
  124. if (valid & ~dev->hw_features) {
  125. valid &= dev->hw_features;
  126. ret |= ETHTOOL_F_UNSUPPORTED;
  127. }
  128. dev->wanted_features &= ~valid;
  129. dev->wanted_features |= wanted & valid;
  130. __netdev_update_features(dev);
  131. if ((dev->wanted_features ^ dev->features) & valid)
  132. ret |= ETHTOOL_F_WISH;
  133. return ret;
  134. }
  135. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  136. {
  137. const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
  138. const struct ethtool_ops *ops = dev->ethtool_ops;
  139. if (sset == ETH_SS_FEATURES)
  140. return ARRAY_SIZE(netdev_features_strings);
  141. if (sset == ETH_SS_RSS_HASH_FUNCS)
  142. return ARRAY_SIZE(rss_hash_func_strings);
  143. if (sset == ETH_SS_TUNABLES)
  144. return ARRAY_SIZE(tunable_strings);
  145. if (sset == ETH_SS_PHY_TUNABLES)
  146. return ARRAY_SIZE(phy_tunable_strings);
  147. if (sset == ETH_SS_PHY_STATS && dev->phydev &&
  148. !ops->get_ethtool_phy_stats &&
  149. phy_ops && phy_ops->get_sset_count)
  150. return phy_ops->get_sset_count(dev->phydev);
  151. if (sset == ETH_SS_LINK_MODES)
  152. return __ETHTOOL_LINK_MODE_MASK_NBITS;
  153. if (ops->get_sset_count && ops->get_strings)
  154. return ops->get_sset_count(dev, sset);
  155. else
  156. return -EOPNOTSUPP;
  157. }
  158. static void __ethtool_get_strings(struct net_device *dev,
  159. u32 stringset, u8 *data)
  160. {
  161. const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
  162. const struct ethtool_ops *ops = dev->ethtool_ops;
  163. if (stringset == ETH_SS_FEATURES)
  164. memcpy(data, netdev_features_strings,
  165. sizeof(netdev_features_strings));
  166. else if (stringset == ETH_SS_RSS_HASH_FUNCS)
  167. memcpy(data, rss_hash_func_strings,
  168. sizeof(rss_hash_func_strings));
  169. else if (stringset == ETH_SS_TUNABLES)
  170. memcpy(data, tunable_strings, sizeof(tunable_strings));
  171. else if (stringset == ETH_SS_PHY_TUNABLES)
  172. memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
  173. else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
  174. !ops->get_ethtool_phy_stats && phy_ops &&
  175. phy_ops->get_strings)
  176. phy_ops->get_strings(dev->phydev, data);
  177. else if (stringset == ETH_SS_LINK_MODES)
  178. memcpy(data, link_mode_names,
  179. __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
  180. else
  181. /* ops->get_strings is valid because checked earlier */
  182. ops->get_strings(dev, stringset, data);
  183. }
  184. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  185. {
  186. /* feature masks of legacy discrete ethtool ops */
  187. switch (eth_cmd) {
  188. case ETHTOOL_GTXCSUM:
  189. case ETHTOOL_STXCSUM:
  190. return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
  191. NETIF_F_SCTP_CRC;
  192. case ETHTOOL_GRXCSUM:
  193. case ETHTOOL_SRXCSUM:
  194. return NETIF_F_RXCSUM;
  195. case ETHTOOL_GSG:
  196. case ETHTOOL_SSG:
  197. return NETIF_F_SG | NETIF_F_FRAGLIST;
  198. case ETHTOOL_GTSO:
  199. case ETHTOOL_STSO:
  200. return NETIF_F_ALL_TSO;
  201. case ETHTOOL_GGSO:
  202. case ETHTOOL_SGSO:
  203. return NETIF_F_GSO;
  204. case ETHTOOL_GGRO:
  205. case ETHTOOL_SGRO:
  206. return NETIF_F_GRO;
  207. default:
  208. BUG();
  209. }
  210. }
  211. static int ethtool_get_one_feature(struct net_device *dev,
  212. char __user *useraddr, u32 ethcmd)
  213. {
  214. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  215. struct ethtool_value edata = {
  216. .cmd = ethcmd,
  217. .data = !!(dev->features & mask),
  218. };
  219. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  220. return -EFAULT;
  221. return 0;
  222. }
  223. static int ethtool_set_one_feature(struct net_device *dev,
  224. void __user *useraddr, u32 ethcmd)
  225. {
  226. struct ethtool_value edata;
  227. netdev_features_t mask;
  228. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  229. return -EFAULT;
  230. mask = ethtool_get_feature_mask(ethcmd);
  231. mask &= dev->hw_features;
  232. if (!mask)
  233. return -EOPNOTSUPP;
  234. if (edata.data)
  235. dev->wanted_features |= mask;
  236. else
  237. dev->wanted_features &= ~mask;
  238. __netdev_update_features(dev);
  239. return 0;
  240. }
  241. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  242. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  243. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
  244. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
  245. NETIF_F_RXHASH)
  246. static u32 __ethtool_get_flags(struct net_device *dev)
  247. {
  248. u32 flags = 0;
  249. if (dev->features & NETIF_F_LRO)
  250. flags |= ETH_FLAG_LRO;
  251. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  252. flags |= ETH_FLAG_RXVLAN;
  253. if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
  254. flags |= ETH_FLAG_TXVLAN;
  255. if (dev->features & NETIF_F_NTUPLE)
  256. flags |= ETH_FLAG_NTUPLE;
  257. if (dev->features & NETIF_F_RXHASH)
  258. flags |= ETH_FLAG_RXHASH;
  259. return flags;
  260. }
  261. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  262. {
  263. netdev_features_t features = 0, changed;
  264. if (data & ~ETH_ALL_FLAGS)
  265. return -EINVAL;
  266. if (data & ETH_FLAG_LRO)
  267. features |= NETIF_F_LRO;
  268. if (data & ETH_FLAG_RXVLAN)
  269. features |= NETIF_F_HW_VLAN_CTAG_RX;
  270. if (data & ETH_FLAG_TXVLAN)
  271. features |= NETIF_F_HW_VLAN_CTAG_TX;
  272. if (data & ETH_FLAG_NTUPLE)
  273. features |= NETIF_F_NTUPLE;
  274. if (data & ETH_FLAG_RXHASH)
  275. features |= NETIF_F_RXHASH;
  276. /* allow changing only bits set in hw_features */
  277. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  278. if (changed & ~dev->hw_features)
  279. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  280. dev->wanted_features =
  281. (dev->wanted_features & ~changed) | (features & changed);
  282. __netdev_update_features(dev);
  283. return 0;
  284. }
  285. /* Given two link masks, AND them together and save the result in dst. */
  286. void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
  287. struct ethtool_link_ksettings *src)
  288. {
  289. unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
  290. unsigned int idx = 0;
  291. for (; idx < size; idx++) {
  292. dst->link_modes.supported[idx] &=
  293. src->link_modes.supported[idx];
  294. dst->link_modes.advertising[idx] &=
  295. src->link_modes.advertising[idx];
  296. }
  297. }
  298. EXPORT_SYMBOL(ethtool_intersect_link_masks);
  299. void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
  300. u32 legacy_u32)
  301. {
  302. linkmode_zero(dst);
  303. dst[0] = legacy_u32;
  304. }
  305. EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
  306. /* return false if src had higher bits set. lower bits always updated. */
  307. bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
  308. const unsigned long *src)
  309. {
  310. *legacy_u32 = src[0];
  311. return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
  312. __ETHTOOL_LINK_MODE_MASK_NBITS;
  313. }
  314. EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
  315. /* return false if ksettings link modes had higher bits
  316. * set. legacy_settings always updated (best effort)
  317. */
  318. static bool
  319. convert_link_ksettings_to_legacy_settings(
  320. struct ethtool_cmd *legacy_settings,
  321. const struct ethtool_link_ksettings *link_ksettings)
  322. {
  323. bool retval = true;
  324. memset(legacy_settings, 0, sizeof(*legacy_settings));
  325. /* this also clears the deprecated fields in legacy structure:
  326. * __u8 transceiver;
  327. * __u32 maxtxpkt;
  328. * __u32 maxrxpkt;
  329. */
  330. retval &= ethtool_convert_link_mode_to_legacy_u32(
  331. &legacy_settings->supported,
  332. link_ksettings->link_modes.supported);
  333. retval &= ethtool_convert_link_mode_to_legacy_u32(
  334. &legacy_settings->advertising,
  335. link_ksettings->link_modes.advertising);
  336. retval &= ethtool_convert_link_mode_to_legacy_u32(
  337. &legacy_settings->lp_advertising,
  338. link_ksettings->link_modes.lp_advertising);
  339. ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
  340. legacy_settings->duplex
  341. = link_ksettings->base.duplex;
  342. legacy_settings->port
  343. = link_ksettings->base.port;
  344. legacy_settings->phy_address
  345. = link_ksettings->base.phy_address;
  346. legacy_settings->autoneg
  347. = link_ksettings->base.autoneg;
  348. legacy_settings->mdio_support
  349. = link_ksettings->base.mdio_support;
  350. legacy_settings->eth_tp_mdix
  351. = link_ksettings->base.eth_tp_mdix;
  352. legacy_settings->eth_tp_mdix_ctrl
  353. = link_ksettings->base.eth_tp_mdix_ctrl;
  354. legacy_settings->transceiver
  355. = link_ksettings->base.transceiver;
  356. return retval;
  357. }
  358. /* number of 32-bit words to store the user's link mode bitmaps */
  359. #define __ETHTOOL_LINK_MODE_MASK_NU32 \
  360. DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
  361. /* layout of the struct passed from/to userland */
  362. struct ethtool_link_usettings {
  363. struct ethtool_link_settings base;
  364. struct {
  365. __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
  366. __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  367. __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  368. } link_modes;
  369. };
  370. /* Internal kernel helper to query a device ethtool_link_settings. */
  371. int __ethtool_get_link_ksettings(struct net_device *dev,
  372. struct ethtool_link_ksettings *link_ksettings)
  373. {
  374. ASSERT_RTNL();
  375. if (!dev->ethtool_ops->get_link_ksettings)
  376. return -EOPNOTSUPP;
  377. if (!netif_device_present(dev))
  378. return -ENODEV;
  379. memset(link_ksettings, 0, sizeof(*link_ksettings));
  380. return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
  381. }
  382. EXPORT_SYMBOL(__ethtool_get_link_ksettings);
  383. /* convert ethtool_link_usettings in user space to a kernel internal
  384. * ethtool_link_ksettings. return 0 on success, errno on error.
  385. */
  386. static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
  387. const void __user *from)
  388. {
  389. struct ethtool_link_usettings link_usettings;
  390. if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
  391. return -EFAULT;
  392. memcpy(&to->base, &link_usettings.base, sizeof(to->base));
  393. bitmap_from_arr32(to->link_modes.supported,
  394. link_usettings.link_modes.supported,
  395. __ETHTOOL_LINK_MODE_MASK_NBITS);
  396. bitmap_from_arr32(to->link_modes.advertising,
  397. link_usettings.link_modes.advertising,
  398. __ETHTOOL_LINK_MODE_MASK_NBITS);
  399. bitmap_from_arr32(to->link_modes.lp_advertising,
  400. link_usettings.link_modes.lp_advertising,
  401. __ETHTOOL_LINK_MODE_MASK_NBITS);
  402. return 0;
  403. }
  404. /* Check if the user is trying to change anything besides speed/duplex */
  405. bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
  406. {
  407. struct ethtool_link_settings base2 = {};
  408. base2.speed = cmd->base.speed;
  409. base2.port = PORT_OTHER;
  410. base2.duplex = cmd->base.duplex;
  411. base2.cmd = cmd->base.cmd;
  412. base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
  413. return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
  414. bitmap_empty(cmd->link_modes.supported,
  415. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  416. bitmap_empty(cmd->link_modes.lp_advertising,
  417. __ETHTOOL_LINK_MODE_MASK_NBITS);
  418. }
  419. /* convert a kernel internal ethtool_link_ksettings to
  420. * ethtool_link_usettings in user space. return 0 on success, errno on
  421. * error.
  422. */
  423. static int
  424. store_link_ksettings_for_user(void __user *to,
  425. const struct ethtool_link_ksettings *from)
  426. {
  427. struct ethtool_link_usettings link_usettings;
  428. memcpy(&link_usettings, from, sizeof(link_usettings));
  429. bitmap_to_arr32(link_usettings.link_modes.supported,
  430. from->link_modes.supported,
  431. __ETHTOOL_LINK_MODE_MASK_NBITS);
  432. bitmap_to_arr32(link_usettings.link_modes.advertising,
  433. from->link_modes.advertising,
  434. __ETHTOOL_LINK_MODE_MASK_NBITS);
  435. bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
  436. from->link_modes.lp_advertising,
  437. __ETHTOOL_LINK_MODE_MASK_NBITS);
  438. if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
  439. return -EFAULT;
  440. return 0;
  441. }
  442. /* Query device for its ethtool_link_settings. */
  443. static int ethtool_get_link_ksettings(struct net_device *dev,
  444. void __user *useraddr)
  445. {
  446. int err = 0;
  447. struct ethtool_link_ksettings link_ksettings;
  448. ASSERT_RTNL();
  449. if (!dev->ethtool_ops->get_link_ksettings)
  450. return -EOPNOTSUPP;
  451. /* handle bitmap nbits handshake */
  452. if (copy_from_user(&link_ksettings.base, useraddr,
  453. sizeof(link_ksettings.base)))
  454. return -EFAULT;
  455. if (__ETHTOOL_LINK_MODE_MASK_NU32
  456. != link_ksettings.base.link_mode_masks_nwords) {
  457. /* wrong link mode nbits requested */
  458. memset(&link_ksettings, 0, sizeof(link_ksettings));
  459. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  460. /* send back number of words required as negative val */
  461. compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
  462. "need too many bits for link modes!");
  463. link_ksettings.base.link_mode_masks_nwords
  464. = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
  465. /* copy the base fields back to user, not the link
  466. * mode bitmaps
  467. */
  468. if (copy_to_user(useraddr, &link_ksettings.base,
  469. sizeof(link_ksettings.base)))
  470. return -EFAULT;
  471. return 0;
  472. }
  473. /* handshake successful: user/kernel agree on
  474. * link_mode_masks_nwords
  475. */
  476. memset(&link_ksettings, 0, sizeof(link_ksettings));
  477. err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
  478. if (err < 0)
  479. return err;
  480. /* make sure we tell the right values to user */
  481. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  482. link_ksettings.base.link_mode_masks_nwords
  483. = __ETHTOOL_LINK_MODE_MASK_NU32;
  484. link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
  485. link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
  486. link_ksettings.base.rate_matching = RATE_MATCH_NONE;
  487. return store_link_ksettings_for_user(useraddr, &link_ksettings);
  488. }
  489. /* Update device ethtool_link_settings. */
  490. static int ethtool_set_link_ksettings(struct net_device *dev,
  491. void __user *useraddr)
  492. {
  493. struct ethtool_link_ksettings link_ksettings = {};
  494. int err;
  495. ASSERT_RTNL();
  496. if (!dev->ethtool_ops->set_link_ksettings)
  497. return -EOPNOTSUPP;
  498. /* make sure nbits field has expected value */
  499. if (copy_from_user(&link_ksettings.base, useraddr,
  500. sizeof(link_ksettings.base)))
  501. return -EFAULT;
  502. if (__ETHTOOL_LINK_MODE_MASK_NU32
  503. != link_ksettings.base.link_mode_masks_nwords)
  504. return -EINVAL;
  505. /* copy the whole structure, now that we know it has expected
  506. * format
  507. */
  508. err = load_link_ksettings_from_user(&link_ksettings, useraddr);
  509. if (err)
  510. return err;
  511. /* re-check nwords field, just in case */
  512. if (__ETHTOOL_LINK_MODE_MASK_NU32
  513. != link_ksettings.base.link_mode_masks_nwords)
  514. return -EINVAL;
  515. if (link_ksettings.base.master_slave_cfg ||
  516. link_ksettings.base.master_slave_state)
  517. return -EINVAL;
  518. err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
  519. if (err >= 0) {
  520. ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
  521. ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
  522. }
  523. return err;
  524. }
  525. int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
  526. const struct ethtool_link_ksettings *cmd,
  527. u32 *dev_speed, u8 *dev_duplex)
  528. {
  529. u32 speed;
  530. u8 duplex;
  531. speed = cmd->base.speed;
  532. duplex = cmd->base.duplex;
  533. /* don't allow custom speed and duplex */
  534. if (!ethtool_validate_speed(speed) ||
  535. !ethtool_validate_duplex(duplex) ||
  536. !ethtool_virtdev_validate_cmd(cmd))
  537. return -EINVAL;
  538. *dev_speed = speed;
  539. *dev_duplex = duplex;
  540. return 0;
  541. }
  542. EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
  543. /* Query device for its ethtool_cmd settings.
  544. *
  545. * Backward compatibility note: for compatibility with legacy ethtool, this is
  546. * now implemented via get_link_ksettings. When driver reports higher link mode
  547. * bits, a kernel warning is logged once (with name of 1st driver/device) to
  548. * recommend user to upgrade ethtool, but the command is successful (only the
  549. * lower link mode bits reported back to user). Deprecated fields from
  550. * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
  551. */
  552. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  553. {
  554. struct ethtool_link_ksettings link_ksettings;
  555. struct ethtool_cmd cmd;
  556. int err;
  557. ASSERT_RTNL();
  558. if (!dev->ethtool_ops->get_link_ksettings)
  559. return -EOPNOTSUPP;
  560. if (dev->ethtool->module_fw_flash_in_progress)
  561. return -EBUSY;
  562. memset(&link_ksettings, 0, sizeof(link_ksettings));
  563. err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
  564. if (err < 0)
  565. return err;
  566. convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
  567. /* send a sensible cmd tag back to user */
  568. cmd.cmd = ETHTOOL_GSET;
  569. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  570. return -EFAULT;
  571. return 0;
  572. }
  573. /* Update device link settings with given ethtool_cmd.
  574. *
  575. * Backward compatibility note: for compatibility with legacy ethtool, this is
  576. * now always implemented via set_link_settings. When user's request updates
  577. * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
  578. * warning is logged once (with name of 1st driver/device) to recommend user to
  579. * upgrade ethtool, and the request is rejected.
  580. */
  581. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  582. {
  583. struct ethtool_link_ksettings link_ksettings;
  584. struct ethtool_cmd cmd;
  585. int ret;
  586. ASSERT_RTNL();
  587. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  588. return -EFAULT;
  589. if (!dev->ethtool_ops->set_link_ksettings)
  590. return -EOPNOTSUPP;
  591. if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
  592. return -EINVAL;
  593. link_ksettings.base.link_mode_masks_nwords =
  594. __ETHTOOL_LINK_MODE_MASK_NU32;
  595. ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
  596. if (ret >= 0) {
  597. ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
  598. ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
  599. }
  600. return ret;
  601. }
  602. static int
  603. ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
  604. {
  605. const struct ethtool_ops *ops = dev->ethtool_ops;
  606. struct device *parent = dev->dev.parent;
  607. rsp->info.cmd = ETHTOOL_GDRVINFO;
  608. strscpy(rsp->info.version, init_uts_ns.name.release,
  609. sizeof(rsp->info.version));
  610. if (ops->get_drvinfo) {
  611. ops->get_drvinfo(dev, &rsp->info);
  612. if (!rsp->info.bus_info[0] && parent)
  613. strscpy(rsp->info.bus_info, dev_name(parent),
  614. sizeof(rsp->info.bus_info));
  615. if (!rsp->info.driver[0] && parent && parent->driver)
  616. strscpy(rsp->info.driver, parent->driver->name,
  617. sizeof(rsp->info.driver));
  618. } else if (parent && parent->driver) {
  619. strscpy(rsp->info.bus_info, dev_name(parent),
  620. sizeof(rsp->info.bus_info));
  621. strscpy(rsp->info.driver, parent->driver->name,
  622. sizeof(rsp->info.driver));
  623. } else if (dev->rtnl_link_ops) {
  624. strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
  625. sizeof(rsp->info.driver));
  626. } else {
  627. return -EOPNOTSUPP;
  628. }
  629. /*
  630. * this method of obtaining string set info is deprecated;
  631. * Use ETHTOOL_GSSET_INFO instead.
  632. */
  633. if (ops->get_sset_count) {
  634. int rc;
  635. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  636. if (rc >= 0)
  637. rsp->info.testinfo_len = rc;
  638. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  639. if (rc >= 0)
  640. rsp->info.n_stats = rc;
  641. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  642. if (rc >= 0)
  643. rsp->info.n_priv_flags = rc;
  644. }
  645. if (ops->get_regs_len) {
  646. int ret = ops->get_regs_len(dev);
  647. if (ret > 0)
  648. rsp->info.regdump_len = ret;
  649. }
  650. if (ops->get_eeprom_len)
  651. rsp->info.eedump_len = ops->get_eeprom_len(dev);
  652. if (!rsp->info.fw_version[0])
  653. rsp->devlink = netdev_to_devlink_get(dev);
  654. return 0;
  655. }
  656. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  657. void __user *useraddr)
  658. {
  659. struct ethtool_sset_info info;
  660. u64 sset_mask;
  661. int i, idx = 0, n_bits = 0, ret, rc;
  662. u32 *info_buf = NULL;
  663. if (copy_from_user(&info, useraddr, sizeof(info)))
  664. return -EFAULT;
  665. /* store copy of mask, because we zero struct later on */
  666. sset_mask = info.sset_mask;
  667. if (!sset_mask)
  668. return 0;
  669. /* calculate size of return buffer */
  670. n_bits = hweight64(sset_mask);
  671. memset(&info, 0, sizeof(info));
  672. info.cmd = ETHTOOL_GSSET_INFO;
  673. info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
  674. if (!info_buf)
  675. return -ENOMEM;
  676. /*
  677. * fill return buffer based on input bitmask and successful
  678. * get_sset_count return
  679. */
  680. for (i = 0; i < 64; i++) {
  681. if (!(sset_mask & (1ULL << i)))
  682. continue;
  683. rc = __ethtool_get_sset_count(dev, i);
  684. if (rc >= 0) {
  685. info.sset_mask |= (1ULL << i);
  686. info_buf[idx++] = rc;
  687. }
  688. }
  689. ret = -EFAULT;
  690. if (copy_to_user(useraddr, &info, sizeof(info)))
  691. goto out;
  692. useraddr += offsetof(struct ethtool_sset_info, data);
  693. if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
  694. goto out;
  695. ret = 0;
  696. out:
  697. kfree(info_buf);
  698. return ret;
  699. }
  700. static noinline_for_stack int
  701. ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
  702. const struct compat_ethtool_rxnfc __user *useraddr,
  703. size_t size)
  704. {
  705. struct compat_ethtool_rxnfc crxnfc = {};
  706. /* We expect there to be holes between fs.m_ext and
  707. * fs.ring_cookie and at the end of fs, but nowhere else.
  708. * On non-x86, no conversion should be needed.
  709. */
  710. BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
  711. sizeof(struct compat_ethtool_rxnfc) !=
  712. sizeof(struct ethtool_rxnfc));
  713. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  714. sizeof(useraddr->fs.m_ext) !=
  715. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  716. sizeof(rxnfc->fs.m_ext));
  717. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
  718. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  719. offsetof(struct ethtool_rxnfc, fs.location) -
  720. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  721. if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
  722. return -EFAULT;
  723. *rxnfc = (struct ethtool_rxnfc) {
  724. .cmd = crxnfc.cmd,
  725. .flow_type = crxnfc.flow_type,
  726. .data = crxnfc.data,
  727. .fs = {
  728. .flow_type = crxnfc.fs.flow_type,
  729. .h_u = crxnfc.fs.h_u,
  730. .h_ext = crxnfc.fs.h_ext,
  731. .m_u = crxnfc.fs.m_u,
  732. .m_ext = crxnfc.fs.m_ext,
  733. .ring_cookie = crxnfc.fs.ring_cookie,
  734. .location = crxnfc.fs.location,
  735. },
  736. .rule_cnt = crxnfc.rule_cnt,
  737. };
  738. return 0;
  739. }
  740. static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
  741. const void __user *useraddr,
  742. size_t size)
  743. {
  744. if (compat_need_64bit_alignment_fixup())
  745. return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
  746. if (copy_from_user(rxnfc, useraddr, size))
  747. return -EFAULT;
  748. return 0;
  749. }
  750. static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
  751. const struct ethtool_rxnfc *rxnfc,
  752. size_t size, const u32 *rule_buf)
  753. {
  754. struct compat_ethtool_rxnfc crxnfc;
  755. memset(&crxnfc, 0, sizeof(crxnfc));
  756. crxnfc = (struct compat_ethtool_rxnfc) {
  757. .cmd = rxnfc->cmd,
  758. .flow_type = rxnfc->flow_type,
  759. .data = rxnfc->data,
  760. .fs = {
  761. .flow_type = rxnfc->fs.flow_type,
  762. .h_u = rxnfc->fs.h_u,
  763. .h_ext = rxnfc->fs.h_ext,
  764. .m_u = rxnfc->fs.m_u,
  765. .m_ext = rxnfc->fs.m_ext,
  766. .ring_cookie = rxnfc->fs.ring_cookie,
  767. .location = rxnfc->fs.location,
  768. },
  769. .rule_cnt = rxnfc->rule_cnt,
  770. };
  771. if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
  772. return -EFAULT;
  773. return 0;
  774. }
  775. static int ethtool_rxnfc_copy_struct(u32 cmd, struct ethtool_rxnfc *info,
  776. size_t *info_size, void __user *useraddr)
  777. {
  778. /* struct ethtool_rxnfc was originally defined for
  779. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  780. * members. User-space might still be using that
  781. * definition.
  782. */
  783. if (cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH)
  784. *info_size = (offsetof(struct ethtool_rxnfc, data) +
  785. sizeof(info->data));
  786. if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
  787. return -EFAULT;
  788. if ((cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH) && info->flow_type & FLOW_RSS) {
  789. *info_size = sizeof(*info);
  790. if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
  791. return -EFAULT;
  792. /* Since malicious users may modify the original data,
  793. * we need to check whether FLOW_RSS is still requested.
  794. */
  795. if (!(info->flow_type & FLOW_RSS))
  796. return -EINVAL;
  797. }
  798. if (info->cmd != cmd)
  799. return -EINVAL;
  800. return 0;
  801. }
  802. static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
  803. const struct ethtool_rxnfc *rxnfc,
  804. size_t size, const u32 *rule_buf)
  805. {
  806. int ret;
  807. if (compat_need_64bit_alignment_fixup()) {
  808. ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
  809. rule_buf);
  810. useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
  811. } else {
  812. ret = copy_to_user(useraddr, rxnfc, size);
  813. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  814. }
  815. if (ret)
  816. return -EFAULT;
  817. if (rule_buf) {
  818. if (copy_to_user(useraddr, rule_buf,
  819. rxnfc->rule_cnt * sizeof(u32)))
  820. return -EFAULT;
  821. }
  822. return 0;
  823. }
  824. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  825. u32 cmd, void __user *useraddr)
  826. {
  827. const struct ethtool_ops *ops = dev->ethtool_ops;
  828. struct ethtool_rxnfc info;
  829. size_t info_size = sizeof(info);
  830. int rc;
  831. if (!ops->set_rxnfc)
  832. return -EOPNOTSUPP;
  833. rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
  834. if (rc)
  835. return rc;
  836. /* Nonzero ring with RSS only makes sense if NIC adds them together */
  837. if (cmd == ETHTOOL_SRXCLSRLINS && info.fs.flow_type & FLOW_RSS &&
  838. !ops->cap_rss_rxnfc_adds &&
  839. ethtool_get_flow_spec_ring(info.fs.ring_cookie))
  840. return -EINVAL;
  841. if (cmd == ETHTOOL_SRXFH && ops->get_rxfh) {
  842. struct ethtool_rxfh_param rxfh = {};
  843. rc = ops->get_rxfh(dev, &rxfh);
  844. if (rc)
  845. return rc;
  846. /* Sanity check: if symmetric-xor is set, then:
  847. * 1 - no other fields besides IP src/dst and/or L4 src/dst
  848. * 2 - If src is set, dst must also be set
  849. */
  850. if ((rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
  851. ((info.data & ~(RXH_IP_SRC | RXH_IP_DST |
  852. RXH_L4_B_0_1 | RXH_L4_B_2_3)) ||
  853. (!!(info.data & RXH_IP_SRC) ^ !!(info.data & RXH_IP_DST)) ||
  854. (!!(info.data & RXH_L4_B_0_1) ^ !!(info.data & RXH_L4_B_2_3))))
  855. return -EINVAL;
  856. }
  857. rc = ops->set_rxnfc(dev, &info);
  858. if (rc)
  859. return rc;
  860. if (cmd == ETHTOOL_SRXCLSRLINS &&
  861. ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
  862. return -EFAULT;
  863. return 0;
  864. }
  865. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  866. u32 cmd, void __user *useraddr)
  867. {
  868. struct ethtool_rxnfc info;
  869. size_t info_size = sizeof(info);
  870. const struct ethtool_ops *ops = dev->ethtool_ops;
  871. int ret;
  872. void *rule_buf = NULL;
  873. if (!ops->get_rxnfc)
  874. return -EOPNOTSUPP;
  875. ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
  876. if (ret)
  877. return ret;
  878. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  879. if (info.rule_cnt > 0) {
  880. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  881. rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
  882. GFP_USER);
  883. if (!rule_buf)
  884. return -ENOMEM;
  885. }
  886. }
  887. ret = ops->get_rxnfc(dev, &info, rule_buf);
  888. if (ret < 0)
  889. goto err_out;
  890. ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
  891. err_out:
  892. kfree(rule_buf);
  893. return ret;
  894. }
  895. static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
  896. struct ethtool_rxnfc *rx_rings,
  897. u32 size)
  898. {
  899. int i;
  900. if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
  901. return -EFAULT;
  902. /* Validate ring indices */
  903. for (i = 0; i < size; i++)
  904. if (indir[i] >= rx_rings->data)
  905. return -EINVAL;
  906. return 0;
  907. }
  908. u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
  909. void netdev_rss_key_fill(void *buffer, size_t len)
  910. {
  911. BUG_ON(len > sizeof(netdev_rss_key));
  912. net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
  913. memcpy(buffer, netdev_rss_key, len);
  914. }
  915. EXPORT_SYMBOL(netdev_rss_key_fill);
  916. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  917. void __user *useraddr)
  918. {
  919. struct ethtool_rxfh_param rxfh = {};
  920. u32 user_size;
  921. int ret;
  922. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  923. !dev->ethtool_ops->get_rxfh)
  924. return -EOPNOTSUPP;
  925. rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  926. if (rxfh.indir_size == 0)
  927. return -EOPNOTSUPP;
  928. if (copy_from_user(&user_size,
  929. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  930. sizeof(user_size)))
  931. return -EFAULT;
  932. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  933. &rxfh.indir_size, sizeof(rxfh.indir_size)))
  934. return -EFAULT;
  935. /* If the user buffer size is 0, this is just a query for the
  936. * device table size. Otherwise, if it's smaller than the
  937. * device table size it's an error.
  938. */
  939. if (user_size < rxfh.indir_size)
  940. return user_size == 0 ? 0 : -EINVAL;
  941. rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER);
  942. if (!rxfh.indir)
  943. return -ENOMEM;
  944. ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
  945. if (ret)
  946. goto out;
  947. if (copy_to_user(useraddr +
  948. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  949. rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir)))
  950. ret = -EFAULT;
  951. out:
  952. kfree(rxfh.indir);
  953. return ret;
  954. }
  955. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  956. void __user *useraddr)
  957. {
  958. const struct ethtool_ops *ops = dev->ethtool_ops;
  959. struct ethtool_rxfh_param rxfh_dev = {};
  960. struct netlink_ext_ack *extack = NULL;
  961. struct ethtool_rxnfc rx_rings;
  962. u32 user_size, i;
  963. int ret;
  964. u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
  965. if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
  966. !ops->get_rxnfc)
  967. return -EOPNOTSUPP;
  968. rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
  969. if (rxfh_dev.indir_size == 0)
  970. return -EOPNOTSUPP;
  971. if (copy_from_user(&user_size,
  972. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  973. sizeof(user_size)))
  974. return -EFAULT;
  975. if (user_size != 0 && user_size != rxfh_dev.indir_size)
  976. return -EINVAL;
  977. rxfh_dev.indir = kcalloc(rxfh_dev.indir_size,
  978. sizeof(rxfh_dev.indir[0]), GFP_USER);
  979. if (!rxfh_dev.indir)
  980. return -ENOMEM;
  981. rx_rings.cmd = ETHTOOL_GRXRINGS;
  982. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  983. if (ret)
  984. goto out;
  985. if (user_size == 0) {
  986. u32 *indir = rxfh_dev.indir;
  987. for (i = 0; i < rxfh_dev.indir_size; i++)
  988. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  989. } else {
  990. ret = ethtool_copy_validate_indir(rxfh_dev.indir,
  991. useraddr + ringidx_offset,
  992. &rx_rings,
  993. rxfh_dev.indir_size);
  994. if (ret)
  995. goto out;
  996. }
  997. rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE;
  998. ret = ops->set_rxfh(dev, &rxfh_dev, extack);
  999. if (ret)
  1000. goto out;
  1001. /* indicate whether rxfh was set to default */
  1002. if (user_size == 0)
  1003. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1004. else
  1005. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1006. out:
  1007. kfree(rxfh_dev.indir);
  1008. return ret;
  1009. }
  1010. static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
  1011. void __user *useraddr)
  1012. {
  1013. const struct ethtool_ops *ops = dev->ethtool_ops;
  1014. struct ethtool_rxfh_param rxfh_dev = {};
  1015. u32 user_indir_size, user_key_size;
  1016. struct ethtool_rxfh_context *ctx;
  1017. struct ethtool_rxfh rxfh;
  1018. u32 indir_bytes;
  1019. u8 *rss_config;
  1020. u32 total_size;
  1021. int ret;
  1022. if (!ops->get_rxfh)
  1023. return -EOPNOTSUPP;
  1024. if (ops->get_rxfh_indir_size)
  1025. rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev);
  1026. if (ops->get_rxfh_key_size)
  1027. rxfh_dev.key_size = ops->get_rxfh_key_size(dev);
  1028. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1029. return -EFAULT;
  1030. user_indir_size = rxfh.indir_size;
  1031. user_key_size = rxfh.key_size;
  1032. /* Check that reserved fields are 0 for now */
  1033. if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
  1034. return -EINVAL;
  1035. /* Most drivers don't handle rss_context, check it's 0 as well */
  1036. if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
  1037. ops->create_rxfh_context))
  1038. return -EOPNOTSUPP;
  1039. rxfh.indir_size = rxfh_dev.indir_size;
  1040. rxfh.key_size = rxfh_dev.key_size;
  1041. if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
  1042. return -EFAULT;
  1043. if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) ||
  1044. (user_key_size && user_key_size != rxfh_dev.key_size))
  1045. return -EINVAL;
  1046. indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]);
  1047. total_size = indir_bytes + user_key_size;
  1048. rss_config = kzalloc(total_size, GFP_USER);
  1049. if (!rss_config)
  1050. return -ENOMEM;
  1051. if (user_indir_size)
  1052. rxfh_dev.indir = (u32 *)rss_config;
  1053. if (user_key_size)
  1054. rxfh_dev.key = rss_config + indir_bytes;
  1055. if (rxfh.rss_context) {
  1056. ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
  1057. if (!ctx) {
  1058. ret = -ENOENT;
  1059. goto out;
  1060. }
  1061. if (rxfh_dev.indir)
  1062. memcpy(rxfh_dev.indir, ethtool_rxfh_context_indir(ctx),
  1063. indir_bytes);
  1064. if (!ops->rxfh_per_ctx_key) {
  1065. rxfh_dev.key_size = 0;
  1066. } else {
  1067. if (rxfh_dev.key)
  1068. memcpy(rxfh_dev.key,
  1069. ethtool_rxfh_context_key(ctx),
  1070. user_key_size);
  1071. rxfh_dev.hfunc = ctx->hfunc;
  1072. }
  1073. rxfh_dev.input_xfrm = ctx->input_xfrm;
  1074. ret = 0;
  1075. } else {
  1076. ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev);
  1077. if (ret)
  1078. goto out;
  1079. }
  1080. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
  1081. &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) {
  1082. ret = -EFAULT;
  1083. } else if (copy_to_user(useraddr +
  1084. offsetof(struct ethtool_rxfh, input_xfrm),
  1085. &rxfh_dev.input_xfrm,
  1086. sizeof(rxfh.input_xfrm))) {
  1087. ret = -EFAULT;
  1088. } else if (copy_to_user(useraddr +
  1089. offsetof(struct ethtool_rxfh, key_size),
  1090. &rxfh_dev.key_size,
  1091. sizeof(rxfh.key_size))) {
  1092. ret = -EFAULT;
  1093. } else if (copy_to_user(useraddr +
  1094. offsetof(struct ethtool_rxfh, rss_config[0]),
  1095. rss_config, total_size)) {
  1096. ret = -EFAULT;
  1097. }
  1098. out:
  1099. kfree(rss_config);
  1100. return ret;
  1101. }
  1102. static struct ethtool_rxfh_context *
  1103. ethtool_rxfh_ctx_alloc(const struct ethtool_ops *ops,
  1104. u32 indir_size, u32 key_size)
  1105. {
  1106. size_t indir_bytes, flex_len, key_off, size;
  1107. struct ethtool_rxfh_context *ctx;
  1108. u32 priv_bytes, indir_max;
  1109. u16 key_max;
  1110. key_max = max(key_size, ops->rxfh_key_space);
  1111. indir_max = max(indir_size, ops->rxfh_indir_space);
  1112. priv_bytes = ALIGN(ops->rxfh_priv_size, sizeof(u32));
  1113. indir_bytes = array_size(indir_max, sizeof(u32));
  1114. key_off = size_add(priv_bytes, indir_bytes);
  1115. flex_len = size_add(key_off, key_max);
  1116. size = struct_size_t(struct ethtool_rxfh_context, data, flex_len);
  1117. ctx = kzalloc(size, GFP_KERNEL_ACCOUNT);
  1118. if (!ctx)
  1119. return NULL;
  1120. ctx->indir_size = indir_size;
  1121. ctx->key_size = key_size;
  1122. ctx->key_off = key_off;
  1123. ctx->priv_size = ops->rxfh_priv_size;
  1124. ctx->hfunc = ETH_RSS_HASH_NO_CHANGE;
  1125. ctx->input_xfrm = RXH_XFRM_NO_CHANGE;
  1126. return ctx;
  1127. }
  1128. static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
  1129. void __user *useraddr)
  1130. {
  1131. u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
  1132. const struct ethtool_ops *ops = dev->ethtool_ops;
  1133. u32 dev_indir_size = 0, dev_key_size = 0, i;
  1134. u32 user_indir_len = 0, indir_bytes = 0;
  1135. struct ethtool_rxfh_param rxfh_dev = {};
  1136. struct ethtool_rxfh_context *ctx = NULL;
  1137. struct netlink_ext_ack *extack = NULL;
  1138. struct ethtool_rxnfc rx_rings;
  1139. struct ethtool_rxfh rxfh;
  1140. bool locked = false; /* dev->ethtool->rss_lock taken */
  1141. bool create = false;
  1142. u8 *rss_config;
  1143. int ret;
  1144. if (!ops->get_rxnfc || !ops->set_rxfh)
  1145. return -EOPNOTSUPP;
  1146. if (ops->get_rxfh_indir_size)
  1147. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1148. if (ops->get_rxfh_key_size)
  1149. dev_key_size = ops->get_rxfh_key_size(dev);
  1150. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1151. return -EFAULT;
  1152. /* Check that reserved fields are 0 for now */
  1153. if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32)
  1154. return -EINVAL;
  1155. /* Most drivers don't handle rss_context, check it's 0 as well */
  1156. if (rxfh.rss_context && !(ops->cap_rss_ctx_supported ||
  1157. ops->create_rxfh_context))
  1158. return -EOPNOTSUPP;
  1159. /* Check input data transformation capabilities */
  1160. if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR &&
  1161. rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)
  1162. return -EINVAL;
  1163. if (rxfh.input_xfrm != RXH_XFRM_NO_CHANGE &&
  1164. (rxfh.input_xfrm & RXH_XFRM_SYM_XOR) &&
  1165. !ops->cap_rss_sym_xor_supported)
  1166. return -EOPNOTSUPP;
  1167. create = rxfh.rss_context == ETH_RXFH_CONTEXT_ALLOC;
  1168. if ((rxfh.indir_size &&
  1169. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
  1170. rxfh.indir_size != dev_indir_size) ||
  1171. (rxfh.key_size && rxfh.key_size != dev_key_size))
  1172. return -EINVAL;
  1173. /* Must request at least one change: indir size, hash key, function
  1174. * or input transformation.
  1175. * There's no need for any of it in case of context creation.
  1176. */
  1177. if (!create &&
  1178. (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
  1179. rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE &&
  1180. rxfh.input_xfrm == RXH_XFRM_NO_CHANGE))
  1181. return -EINVAL;
  1182. indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]);
  1183. /* Check settings which may be global rather than per RSS-context */
  1184. if (rxfh.rss_context && !ops->rxfh_per_ctx_key)
  1185. if (rxfh.key_size ||
  1186. (rxfh.hfunc && rxfh.hfunc != ETH_RSS_HASH_NO_CHANGE) ||
  1187. (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_NO_CHANGE))
  1188. return -EOPNOTSUPP;
  1189. rss_config = kzalloc(indir_bytes + dev_key_size, GFP_USER);
  1190. if (!rss_config)
  1191. return -ENOMEM;
  1192. rx_rings.cmd = ETHTOOL_GRXRINGS;
  1193. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  1194. if (ret)
  1195. goto out;
  1196. /* rxfh.indir_size == 0 means reset the indir table to default (master
  1197. * context) or delete the context (other RSS contexts).
  1198. * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
  1199. */
  1200. if (rxfh.indir_size &&
  1201. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
  1202. user_indir_len = indir_bytes;
  1203. rxfh_dev.indir = (u32 *)rss_config;
  1204. rxfh_dev.indir_size = dev_indir_size;
  1205. ret = ethtool_copy_validate_indir(rxfh_dev.indir,
  1206. useraddr + rss_cfg_offset,
  1207. &rx_rings,
  1208. rxfh.indir_size);
  1209. if (ret)
  1210. goto out;
  1211. } else if (rxfh.indir_size == 0) {
  1212. if (rxfh.rss_context == 0) {
  1213. u32 *indir;
  1214. rxfh_dev.indir = (u32 *)rss_config;
  1215. rxfh_dev.indir_size = dev_indir_size;
  1216. indir = rxfh_dev.indir;
  1217. for (i = 0; i < dev_indir_size; i++)
  1218. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  1219. } else {
  1220. rxfh_dev.rss_delete = true;
  1221. }
  1222. }
  1223. if (rxfh.key_size) {
  1224. rxfh_dev.key_size = dev_key_size;
  1225. rxfh_dev.key = rss_config + indir_bytes;
  1226. if (copy_from_user(rxfh_dev.key,
  1227. useraddr + rss_cfg_offset + user_indir_len,
  1228. rxfh.key_size)) {
  1229. ret = -EFAULT;
  1230. goto out;
  1231. }
  1232. }
  1233. if (rxfh.rss_context) {
  1234. mutex_lock(&dev->ethtool->rss_lock);
  1235. locked = true;
  1236. }
  1237. if (create) {
  1238. if (rxfh_dev.rss_delete) {
  1239. ret = -EINVAL;
  1240. goto out;
  1241. }
  1242. ctx = ethtool_rxfh_ctx_alloc(ops, dev_indir_size, dev_key_size);
  1243. if (!ctx) {
  1244. ret = -ENOMEM;
  1245. goto out;
  1246. }
  1247. if (ops->create_rxfh_context) {
  1248. u32 limit = ops->rxfh_max_num_contexts ?: U32_MAX;
  1249. u32 ctx_id;
  1250. /* driver uses new API, core allocates ID */
  1251. ret = xa_alloc(&dev->ethtool->rss_ctx, &ctx_id, ctx,
  1252. XA_LIMIT(1, limit - 1),
  1253. GFP_KERNEL_ACCOUNT);
  1254. if (ret < 0) {
  1255. kfree(ctx);
  1256. goto out;
  1257. }
  1258. WARN_ON(!ctx_id); /* can't happen */
  1259. rxfh.rss_context = ctx_id;
  1260. }
  1261. } else if (rxfh.rss_context) {
  1262. ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context);
  1263. if (!ctx) {
  1264. ret = -ENOENT;
  1265. goto out;
  1266. }
  1267. }
  1268. rxfh_dev.hfunc = rxfh.hfunc;
  1269. rxfh_dev.rss_context = rxfh.rss_context;
  1270. rxfh_dev.input_xfrm = rxfh.input_xfrm;
  1271. if (rxfh.rss_context && ops->create_rxfh_context) {
  1272. if (create) {
  1273. ret = ops->create_rxfh_context(dev, ctx, &rxfh_dev,
  1274. extack);
  1275. /* Make sure driver populates defaults */
  1276. WARN_ON_ONCE(!ret && !rxfh_dev.key &&
  1277. !memchr_inv(ethtool_rxfh_context_key(ctx),
  1278. 0, ctx->key_size));
  1279. } else if (rxfh_dev.rss_delete) {
  1280. ret = ops->remove_rxfh_context(dev, ctx,
  1281. rxfh.rss_context,
  1282. extack);
  1283. } else {
  1284. ret = ops->modify_rxfh_context(dev, ctx, &rxfh_dev,
  1285. extack);
  1286. }
  1287. } else {
  1288. ret = ops->set_rxfh(dev, &rxfh_dev, extack);
  1289. }
  1290. if (ret) {
  1291. if (create) {
  1292. /* failed to create, free our new tracking entry */
  1293. if (ops->create_rxfh_context)
  1294. xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
  1295. kfree(ctx);
  1296. }
  1297. goto out;
  1298. }
  1299. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
  1300. &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context)))
  1301. ret = -EFAULT;
  1302. if (!rxfh_dev.rss_context) {
  1303. /* indicate whether rxfh was set to default */
  1304. if (rxfh.indir_size == 0)
  1305. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1306. else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1307. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1308. }
  1309. /* Update rss_ctx tracking */
  1310. if (create && !ops->create_rxfh_context) {
  1311. /* driver uses old API, it chose context ID */
  1312. if (WARN_ON(xa_load(&dev->ethtool->rss_ctx, rxfh_dev.rss_context))) {
  1313. /* context ID reused, our tracking is screwed */
  1314. kfree(ctx);
  1315. goto out;
  1316. }
  1317. /* Allocate the exact ID the driver gave us */
  1318. if (xa_is_err(xa_store(&dev->ethtool->rss_ctx, rxfh_dev.rss_context,
  1319. ctx, GFP_KERNEL))) {
  1320. kfree(ctx);
  1321. goto out;
  1322. }
  1323. /* Fetch the defaults for the old API, in the new API drivers
  1324. * should write defaults into ctx themselves.
  1325. */
  1326. rxfh_dev.indir = (u32 *)rss_config;
  1327. rxfh_dev.indir_size = dev_indir_size;
  1328. rxfh_dev.key = rss_config + indir_bytes;
  1329. rxfh_dev.key_size = dev_key_size;
  1330. ret = ops->get_rxfh(dev, &rxfh_dev);
  1331. if (WARN_ON(ret)) {
  1332. xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context);
  1333. kfree(ctx);
  1334. goto out;
  1335. }
  1336. }
  1337. if (rxfh_dev.rss_delete) {
  1338. WARN_ON(xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context) != ctx);
  1339. kfree(ctx);
  1340. } else if (ctx) {
  1341. if (rxfh_dev.indir) {
  1342. for (i = 0; i < dev_indir_size; i++)
  1343. ethtool_rxfh_context_indir(ctx)[i] = rxfh_dev.indir[i];
  1344. ctx->indir_configured =
  1345. rxfh.indir_size &&
  1346. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE;
  1347. }
  1348. if (rxfh_dev.key) {
  1349. memcpy(ethtool_rxfh_context_key(ctx), rxfh_dev.key,
  1350. dev_key_size);
  1351. ctx->key_configured = !!rxfh.key_size;
  1352. }
  1353. if (rxfh_dev.hfunc != ETH_RSS_HASH_NO_CHANGE)
  1354. ctx->hfunc = rxfh_dev.hfunc;
  1355. if (rxfh_dev.input_xfrm != RXH_XFRM_NO_CHANGE)
  1356. ctx->input_xfrm = rxfh_dev.input_xfrm;
  1357. }
  1358. out:
  1359. if (locked)
  1360. mutex_unlock(&dev->ethtool->rss_lock);
  1361. kfree(rss_config);
  1362. return ret;
  1363. }
  1364. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1365. {
  1366. struct ethtool_regs regs;
  1367. const struct ethtool_ops *ops = dev->ethtool_ops;
  1368. void *regbuf;
  1369. int reglen, ret;
  1370. if (!ops->get_regs || !ops->get_regs_len)
  1371. return -EOPNOTSUPP;
  1372. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1373. return -EFAULT;
  1374. reglen = ops->get_regs_len(dev);
  1375. if (reglen <= 0)
  1376. return reglen;
  1377. if (regs.len > reglen)
  1378. regs.len = reglen;
  1379. regbuf = vzalloc(reglen);
  1380. if (!regbuf)
  1381. return -ENOMEM;
  1382. if (regs.len < reglen)
  1383. reglen = regs.len;
  1384. ops->get_regs(dev, &regs, regbuf);
  1385. ret = -EFAULT;
  1386. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1387. goto out;
  1388. useraddr += offsetof(struct ethtool_regs, data);
  1389. if (copy_to_user(useraddr, regbuf, reglen))
  1390. goto out;
  1391. ret = 0;
  1392. out:
  1393. vfree(regbuf);
  1394. return ret;
  1395. }
  1396. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1397. {
  1398. struct ethtool_value reset;
  1399. int ret;
  1400. if (!dev->ethtool_ops->reset)
  1401. return -EOPNOTSUPP;
  1402. if (dev->ethtool->module_fw_flash_in_progress)
  1403. return -EBUSY;
  1404. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1405. return -EFAULT;
  1406. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1407. if (ret)
  1408. return ret;
  1409. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1410. return -EFAULT;
  1411. return 0;
  1412. }
  1413. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1414. {
  1415. struct ethtool_wolinfo wol;
  1416. if (!dev->ethtool_ops->get_wol)
  1417. return -EOPNOTSUPP;
  1418. memset(&wol, 0, sizeof(struct ethtool_wolinfo));
  1419. wol.cmd = ETHTOOL_GWOL;
  1420. dev->ethtool_ops->get_wol(dev, &wol);
  1421. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1422. return -EFAULT;
  1423. return 0;
  1424. }
  1425. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1426. {
  1427. struct ethtool_wolinfo wol, cur_wol;
  1428. int ret;
  1429. if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
  1430. return -EOPNOTSUPP;
  1431. memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
  1432. cur_wol.cmd = ETHTOOL_GWOL;
  1433. dev->ethtool_ops->get_wol(dev, &cur_wol);
  1434. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1435. return -EFAULT;
  1436. if (wol.wolopts & ~cur_wol.supported)
  1437. return -EINVAL;
  1438. if (wol.wolopts == cur_wol.wolopts &&
  1439. !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
  1440. return 0;
  1441. ret = dev->ethtool_ops->set_wol(dev, &wol);
  1442. if (ret)
  1443. return ret;
  1444. dev->ethtool->wol_enabled = !!wol.wolopts;
  1445. ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
  1446. return 0;
  1447. }
  1448. static void eee_to_keee(struct ethtool_keee *keee,
  1449. const struct ethtool_eee *eee)
  1450. {
  1451. memset(keee, 0, sizeof(*keee));
  1452. keee->eee_enabled = eee->eee_enabled;
  1453. keee->tx_lpi_enabled = eee->tx_lpi_enabled;
  1454. keee->tx_lpi_timer = eee->tx_lpi_timer;
  1455. ethtool_convert_legacy_u32_to_link_mode(keee->advertised,
  1456. eee->advertised);
  1457. }
  1458. static void keee_to_eee(struct ethtool_eee *eee,
  1459. const struct ethtool_keee *keee)
  1460. {
  1461. bool overflow;
  1462. memset(eee, 0, sizeof(*eee));
  1463. eee->eee_active = keee->eee_active;
  1464. eee->eee_enabled = keee->eee_enabled;
  1465. eee->tx_lpi_enabled = keee->tx_lpi_enabled;
  1466. eee->tx_lpi_timer = keee->tx_lpi_timer;
  1467. overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported,
  1468. keee->supported);
  1469. ethtool_convert_link_mode_to_legacy_u32(&eee->advertised,
  1470. keee->advertised);
  1471. ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised,
  1472. keee->lp_advertised);
  1473. if (overflow)
  1474. pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n");
  1475. }
  1476. static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
  1477. {
  1478. struct ethtool_keee keee;
  1479. struct ethtool_eee eee;
  1480. int rc;
  1481. if (!dev->ethtool_ops->get_eee)
  1482. return -EOPNOTSUPP;
  1483. memset(&keee, 0, sizeof(keee));
  1484. rc = dev->ethtool_ops->get_eee(dev, &keee);
  1485. if (rc)
  1486. return rc;
  1487. keee_to_eee(&eee, &keee);
  1488. if (copy_to_user(useraddr, &eee, sizeof(eee)))
  1489. return -EFAULT;
  1490. return 0;
  1491. }
  1492. static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
  1493. {
  1494. struct ethtool_keee keee;
  1495. struct ethtool_eee eee;
  1496. int ret;
  1497. if (!dev->ethtool_ops->set_eee)
  1498. return -EOPNOTSUPP;
  1499. if (copy_from_user(&eee, useraddr, sizeof(eee)))
  1500. return -EFAULT;
  1501. eee_to_keee(&keee, &eee);
  1502. ret = dev->ethtool_ops->set_eee(dev, &keee);
  1503. if (!ret)
  1504. ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
  1505. return ret;
  1506. }
  1507. static int ethtool_nway_reset(struct net_device *dev)
  1508. {
  1509. if (!dev->ethtool_ops->nway_reset)
  1510. return -EOPNOTSUPP;
  1511. return dev->ethtool_ops->nway_reset(dev);
  1512. }
  1513. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1514. {
  1515. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1516. int link = __ethtool_get_link(dev);
  1517. if (link < 0)
  1518. return link;
  1519. edata.data = link;
  1520. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1521. return -EFAULT;
  1522. return 0;
  1523. }
  1524. static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
  1525. int (*getter)(struct net_device *,
  1526. struct ethtool_eeprom *, u8 *),
  1527. u32 total_len)
  1528. {
  1529. struct ethtool_eeprom eeprom;
  1530. void __user *userbuf = useraddr + sizeof(eeprom);
  1531. u32 bytes_remaining;
  1532. u8 *data;
  1533. int ret = 0;
  1534. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1535. return -EFAULT;
  1536. /* Check for wrap and zero */
  1537. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1538. return -EINVAL;
  1539. /* Check for exceeding total eeprom len */
  1540. if (eeprom.offset + eeprom.len > total_len)
  1541. return -EINVAL;
  1542. data = kzalloc(PAGE_SIZE, GFP_USER);
  1543. if (!data)
  1544. return -ENOMEM;
  1545. bytes_remaining = eeprom.len;
  1546. while (bytes_remaining > 0) {
  1547. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1548. ret = getter(dev, &eeprom, data);
  1549. if (ret)
  1550. break;
  1551. if (!eeprom.len) {
  1552. ret = -EIO;
  1553. break;
  1554. }
  1555. if (copy_to_user(userbuf, data, eeprom.len)) {
  1556. ret = -EFAULT;
  1557. break;
  1558. }
  1559. userbuf += eeprom.len;
  1560. eeprom.offset += eeprom.len;
  1561. bytes_remaining -= eeprom.len;
  1562. }
  1563. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1564. eeprom.offset -= eeprom.len;
  1565. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1566. ret = -EFAULT;
  1567. kfree(data);
  1568. return ret;
  1569. }
  1570. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1571. {
  1572. const struct ethtool_ops *ops = dev->ethtool_ops;
  1573. if (!ops->get_eeprom || !ops->get_eeprom_len ||
  1574. !ops->get_eeprom_len(dev))
  1575. return -EOPNOTSUPP;
  1576. return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
  1577. ops->get_eeprom_len(dev));
  1578. }
  1579. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1580. {
  1581. struct ethtool_eeprom eeprom;
  1582. const struct ethtool_ops *ops = dev->ethtool_ops;
  1583. void __user *userbuf = useraddr + sizeof(eeprom);
  1584. u32 bytes_remaining;
  1585. u8 *data;
  1586. int ret = 0;
  1587. if (!ops->set_eeprom || !ops->get_eeprom_len ||
  1588. !ops->get_eeprom_len(dev))
  1589. return -EOPNOTSUPP;
  1590. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1591. return -EFAULT;
  1592. /* Check for wrap and zero */
  1593. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1594. return -EINVAL;
  1595. /* Check for exceeding total eeprom len */
  1596. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1597. return -EINVAL;
  1598. data = kzalloc(PAGE_SIZE, GFP_USER);
  1599. if (!data)
  1600. return -ENOMEM;
  1601. bytes_remaining = eeprom.len;
  1602. while (bytes_remaining > 0) {
  1603. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1604. if (copy_from_user(data, userbuf, eeprom.len)) {
  1605. ret = -EFAULT;
  1606. break;
  1607. }
  1608. ret = ops->set_eeprom(dev, &eeprom, data);
  1609. if (ret)
  1610. break;
  1611. userbuf += eeprom.len;
  1612. eeprom.offset += eeprom.len;
  1613. bytes_remaining -= eeprom.len;
  1614. }
  1615. kfree(data);
  1616. return ret;
  1617. }
  1618. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1619. void __user *useraddr)
  1620. {
  1621. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1622. struct kernel_ethtool_coalesce kernel_coalesce = {};
  1623. int ret;
  1624. if (!dev->ethtool_ops->get_coalesce)
  1625. return -EOPNOTSUPP;
  1626. ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
  1627. NULL);
  1628. if (ret)
  1629. return ret;
  1630. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1631. return -EFAULT;
  1632. return 0;
  1633. }
  1634. static bool
  1635. ethtool_set_coalesce_supported(struct net_device *dev,
  1636. struct ethtool_coalesce *coalesce)
  1637. {
  1638. u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
  1639. u32 nonzero_params = 0;
  1640. if (coalesce->rx_coalesce_usecs)
  1641. nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
  1642. if (coalesce->rx_max_coalesced_frames)
  1643. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
  1644. if (coalesce->rx_coalesce_usecs_irq)
  1645. nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
  1646. if (coalesce->rx_max_coalesced_frames_irq)
  1647. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
  1648. if (coalesce->tx_coalesce_usecs)
  1649. nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
  1650. if (coalesce->tx_max_coalesced_frames)
  1651. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
  1652. if (coalesce->tx_coalesce_usecs_irq)
  1653. nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
  1654. if (coalesce->tx_max_coalesced_frames_irq)
  1655. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
  1656. if (coalesce->stats_block_coalesce_usecs)
  1657. nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
  1658. if (coalesce->use_adaptive_rx_coalesce)
  1659. nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
  1660. if (coalesce->use_adaptive_tx_coalesce)
  1661. nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
  1662. if (coalesce->pkt_rate_low)
  1663. nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
  1664. if (coalesce->rx_coalesce_usecs_low)
  1665. nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
  1666. if (coalesce->rx_max_coalesced_frames_low)
  1667. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
  1668. if (coalesce->tx_coalesce_usecs_low)
  1669. nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
  1670. if (coalesce->tx_max_coalesced_frames_low)
  1671. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
  1672. if (coalesce->pkt_rate_high)
  1673. nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
  1674. if (coalesce->rx_coalesce_usecs_high)
  1675. nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
  1676. if (coalesce->rx_max_coalesced_frames_high)
  1677. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
  1678. if (coalesce->tx_coalesce_usecs_high)
  1679. nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
  1680. if (coalesce->tx_max_coalesced_frames_high)
  1681. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
  1682. if (coalesce->rate_sample_interval)
  1683. nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
  1684. return (supported_params & nonzero_params) == nonzero_params;
  1685. }
  1686. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1687. void __user *useraddr)
  1688. {
  1689. struct kernel_ethtool_coalesce kernel_coalesce = {};
  1690. struct ethtool_coalesce coalesce;
  1691. int ret;
  1692. if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
  1693. return -EOPNOTSUPP;
  1694. ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
  1695. NULL);
  1696. if (ret)
  1697. return ret;
  1698. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1699. return -EFAULT;
  1700. if (!ethtool_set_coalesce_supported(dev, &coalesce))
  1701. return -EOPNOTSUPP;
  1702. ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
  1703. NULL);
  1704. if (!ret)
  1705. ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
  1706. return ret;
  1707. }
  1708. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1709. {
  1710. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1711. struct kernel_ethtool_ringparam kernel_ringparam = {};
  1712. if (!dev->ethtool_ops->get_ringparam)
  1713. return -EOPNOTSUPP;
  1714. dev->ethtool_ops->get_ringparam(dev, &ringparam,
  1715. &kernel_ringparam, NULL);
  1716. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1717. return -EFAULT;
  1718. return 0;
  1719. }
  1720. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1721. {
  1722. struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
  1723. struct kernel_ethtool_ringparam kernel_ringparam;
  1724. int ret;
  1725. if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
  1726. return -EOPNOTSUPP;
  1727. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1728. return -EFAULT;
  1729. dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
  1730. /* ensure new ring parameters are within the maximums */
  1731. if (ringparam.rx_pending > max.rx_max_pending ||
  1732. ringparam.rx_mini_pending > max.rx_mini_max_pending ||
  1733. ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
  1734. ringparam.tx_pending > max.tx_max_pending)
  1735. return -EINVAL;
  1736. ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
  1737. &kernel_ringparam, NULL);
  1738. if (!ret)
  1739. ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
  1740. return ret;
  1741. }
  1742. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  1743. void __user *useraddr)
  1744. {
  1745. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  1746. if (!dev->ethtool_ops->get_channels)
  1747. return -EOPNOTSUPP;
  1748. dev->ethtool_ops->get_channels(dev, &channels);
  1749. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  1750. return -EFAULT;
  1751. return 0;
  1752. }
  1753. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  1754. void __user *useraddr)
  1755. {
  1756. struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
  1757. u16 from_channel, to_channel;
  1758. unsigned int i;
  1759. int ret;
  1760. if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
  1761. return -EOPNOTSUPP;
  1762. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  1763. return -EFAULT;
  1764. dev->ethtool_ops->get_channels(dev, &curr);
  1765. if (channels.rx_count == curr.rx_count &&
  1766. channels.tx_count == curr.tx_count &&
  1767. channels.combined_count == curr.combined_count &&
  1768. channels.other_count == curr.other_count)
  1769. return 0;
  1770. /* ensure new counts are within the maximums */
  1771. if (channels.rx_count > curr.max_rx ||
  1772. channels.tx_count > curr.max_tx ||
  1773. channels.combined_count > curr.max_combined ||
  1774. channels.other_count > curr.max_other)
  1775. return -EINVAL;
  1776. /* ensure there is at least one RX and one TX channel */
  1777. if (!channels.combined_count &&
  1778. (!channels.rx_count || !channels.tx_count))
  1779. return -EINVAL;
  1780. ret = ethtool_check_max_channel(dev, channels, NULL);
  1781. if (ret)
  1782. return ret;
  1783. /* Disabling channels, query zero-copy AF_XDP sockets */
  1784. from_channel = channels.combined_count +
  1785. min(channels.rx_count, channels.tx_count);
  1786. to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
  1787. for (i = from_channel; i < to_channel; i++)
  1788. if (xsk_get_pool_from_qid(dev, i))
  1789. return -EINVAL;
  1790. ret = dev->ethtool_ops->set_channels(dev, &channels);
  1791. if (!ret)
  1792. ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
  1793. return ret;
  1794. }
  1795. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1796. {
  1797. struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
  1798. if (!dev->ethtool_ops->get_pauseparam)
  1799. return -EOPNOTSUPP;
  1800. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1801. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1802. return -EFAULT;
  1803. return 0;
  1804. }
  1805. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1806. {
  1807. struct ethtool_pauseparam pauseparam;
  1808. int ret;
  1809. if (!dev->ethtool_ops->set_pauseparam)
  1810. return -EOPNOTSUPP;
  1811. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1812. return -EFAULT;
  1813. ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1814. if (!ret)
  1815. ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
  1816. return ret;
  1817. }
  1818. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1819. {
  1820. struct ethtool_test test;
  1821. const struct ethtool_ops *ops = dev->ethtool_ops;
  1822. u64 *data;
  1823. int ret, test_len;
  1824. if (!ops->self_test || !ops->get_sset_count)
  1825. return -EOPNOTSUPP;
  1826. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1827. if (test_len < 0)
  1828. return test_len;
  1829. WARN_ON(test_len == 0);
  1830. if (copy_from_user(&test, useraddr, sizeof(test)))
  1831. return -EFAULT;
  1832. test.len = test_len;
  1833. data = kcalloc(test_len, sizeof(u64), GFP_USER);
  1834. if (!data)
  1835. return -ENOMEM;
  1836. netif_testing_on(dev);
  1837. ops->self_test(dev, &test, data);
  1838. netif_testing_off(dev);
  1839. ret = -EFAULT;
  1840. if (copy_to_user(useraddr, &test, sizeof(test)))
  1841. goto out;
  1842. useraddr += sizeof(test);
  1843. if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
  1844. goto out;
  1845. ret = 0;
  1846. out:
  1847. kfree(data);
  1848. return ret;
  1849. }
  1850. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1851. {
  1852. struct ethtool_gstrings gstrings;
  1853. u8 *data;
  1854. int ret;
  1855. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1856. return -EFAULT;
  1857. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1858. if (ret < 0)
  1859. return ret;
  1860. if (ret > S32_MAX / ETH_GSTRING_LEN)
  1861. return -ENOMEM;
  1862. WARN_ON_ONCE(!ret);
  1863. gstrings.len = ret;
  1864. if (gstrings.len) {
  1865. data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
  1866. if (!data)
  1867. return -ENOMEM;
  1868. __ethtool_get_strings(dev, gstrings.string_set, data);
  1869. } else {
  1870. data = NULL;
  1871. }
  1872. ret = -EFAULT;
  1873. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1874. goto out;
  1875. useraddr += sizeof(gstrings);
  1876. if (gstrings.len &&
  1877. copy_to_user(useraddr, data,
  1878. array_size(gstrings.len, ETH_GSTRING_LEN)))
  1879. goto out;
  1880. ret = 0;
  1881. out:
  1882. vfree(data);
  1883. return ret;
  1884. }
  1885. __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
  1886. {
  1887. va_list args;
  1888. va_start(args, fmt);
  1889. vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
  1890. va_end(args);
  1891. *data += ETH_GSTRING_LEN;
  1892. }
  1893. EXPORT_SYMBOL(ethtool_sprintf);
  1894. void ethtool_puts(u8 **data, const char *str)
  1895. {
  1896. strscpy(*data, str, ETH_GSTRING_LEN);
  1897. *data += ETH_GSTRING_LEN;
  1898. }
  1899. EXPORT_SYMBOL(ethtool_puts);
  1900. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1901. {
  1902. struct ethtool_value id;
  1903. static bool busy;
  1904. const struct ethtool_ops *ops = dev->ethtool_ops;
  1905. netdevice_tracker dev_tracker;
  1906. int rc;
  1907. if (!ops->set_phys_id)
  1908. return -EOPNOTSUPP;
  1909. if (busy)
  1910. return -EBUSY;
  1911. if (copy_from_user(&id, useraddr, sizeof(id)))
  1912. return -EFAULT;
  1913. rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  1914. if (rc < 0)
  1915. return rc;
  1916. /* Drop the RTNL lock while waiting, but prevent reentry or
  1917. * removal of the device.
  1918. */
  1919. busy = true;
  1920. netdev_hold(dev, &dev_tracker, GFP_KERNEL);
  1921. rtnl_unlock();
  1922. if (rc == 0) {
  1923. /* Driver will handle this itself */
  1924. schedule_timeout_interruptible(
  1925. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  1926. } else {
  1927. /* Driver expects to be called at twice the frequency in rc */
  1928. int n = rc * 2, interval = HZ / n;
  1929. u64 count = mul_u32_u32(n, id.data);
  1930. u64 i = 0;
  1931. do {
  1932. rtnl_lock();
  1933. rc = ops->set_phys_id(dev,
  1934. (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  1935. rtnl_unlock();
  1936. if (rc)
  1937. break;
  1938. schedule_timeout_interruptible(interval);
  1939. } while (!signal_pending(current) && (!id.data || i < count));
  1940. }
  1941. rtnl_lock();
  1942. netdev_put(dev, &dev_tracker);
  1943. busy = false;
  1944. (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  1945. return rc;
  1946. }
  1947. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1948. {
  1949. struct ethtool_stats stats;
  1950. const struct ethtool_ops *ops = dev->ethtool_ops;
  1951. u64 *data;
  1952. int ret, n_stats;
  1953. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1954. return -EOPNOTSUPP;
  1955. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1956. if (n_stats < 0)
  1957. return n_stats;
  1958. if (n_stats > S32_MAX / sizeof(u64))
  1959. return -ENOMEM;
  1960. WARN_ON_ONCE(!n_stats);
  1961. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1962. return -EFAULT;
  1963. stats.n_stats = n_stats;
  1964. if (n_stats) {
  1965. data = vzalloc(array_size(n_stats, sizeof(u64)));
  1966. if (!data)
  1967. return -ENOMEM;
  1968. ops->get_ethtool_stats(dev, &stats, data);
  1969. } else {
  1970. data = NULL;
  1971. }
  1972. ret = -EFAULT;
  1973. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1974. goto out;
  1975. useraddr += sizeof(stats);
  1976. if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
  1977. goto out;
  1978. ret = 0;
  1979. out:
  1980. vfree(data);
  1981. return ret;
  1982. }
  1983. static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
  1984. {
  1985. if (n_stats < 0)
  1986. return n_stats;
  1987. if (n_stats > S32_MAX / sizeof(u64))
  1988. return -ENOMEM;
  1989. if (WARN_ON_ONCE(!n_stats))
  1990. return -EOPNOTSUPP;
  1991. *data = vzalloc(array_size(n_stats, sizeof(u64)));
  1992. if (!*data)
  1993. return -ENOMEM;
  1994. return 0;
  1995. }
  1996. static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
  1997. struct ethtool_stats *stats,
  1998. u64 **data)
  1999. {
  2000. const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
  2001. int n_stats, ret;
  2002. if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
  2003. return -EOPNOTSUPP;
  2004. n_stats = phy_ops->get_sset_count(phydev);
  2005. ret = ethtool_vzalloc_stats_array(n_stats, data);
  2006. if (ret)
  2007. return ret;
  2008. stats->n_stats = n_stats;
  2009. return phy_ops->get_stats(phydev, stats, *data);
  2010. }
  2011. static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
  2012. struct ethtool_stats *stats,
  2013. u64 **data)
  2014. {
  2015. const struct ethtool_ops *ops = dev->ethtool_ops;
  2016. int n_stats, ret;
  2017. if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats)
  2018. return -EOPNOTSUPP;
  2019. n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
  2020. ret = ethtool_vzalloc_stats_array(n_stats, data);
  2021. if (ret)
  2022. return ret;
  2023. stats->n_stats = n_stats;
  2024. ops->get_ethtool_phy_stats(dev, stats, *data);
  2025. return 0;
  2026. }
  2027. static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
  2028. {
  2029. struct phy_device *phydev = dev->phydev;
  2030. struct ethtool_stats stats;
  2031. u64 *data = NULL;
  2032. int ret = -EOPNOTSUPP;
  2033. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  2034. return -EFAULT;
  2035. if (phydev)
  2036. ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
  2037. if (ret == -EOPNOTSUPP)
  2038. ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
  2039. if (ret)
  2040. goto out;
  2041. if (copy_to_user(useraddr, &stats, sizeof(stats))) {
  2042. ret = -EFAULT;
  2043. goto out;
  2044. }
  2045. useraddr += sizeof(stats);
  2046. if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
  2047. ret = -EFAULT;
  2048. out:
  2049. vfree(data);
  2050. return ret;
  2051. }
  2052. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  2053. {
  2054. struct ethtool_perm_addr epaddr;
  2055. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  2056. return -EFAULT;
  2057. if (epaddr.size < dev->addr_len)
  2058. return -ETOOSMALL;
  2059. epaddr.size = dev->addr_len;
  2060. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  2061. return -EFAULT;
  2062. useraddr += sizeof(epaddr);
  2063. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  2064. return -EFAULT;
  2065. return 0;
  2066. }
  2067. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  2068. u32 cmd, u32 (*actor)(struct net_device *))
  2069. {
  2070. struct ethtool_value edata = { .cmd = cmd };
  2071. if (!actor)
  2072. return -EOPNOTSUPP;
  2073. edata.data = actor(dev);
  2074. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  2075. return -EFAULT;
  2076. return 0;
  2077. }
  2078. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  2079. void (*actor)(struct net_device *, u32))
  2080. {
  2081. struct ethtool_value edata;
  2082. if (!actor)
  2083. return -EOPNOTSUPP;
  2084. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  2085. return -EFAULT;
  2086. actor(dev, edata.data);
  2087. return 0;
  2088. }
  2089. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  2090. int (*actor)(struct net_device *, u32))
  2091. {
  2092. struct ethtool_value edata;
  2093. if (!actor)
  2094. return -EOPNOTSUPP;
  2095. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  2096. return -EFAULT;
  2097. return actor(dev, edata.data);
  2098. }
  2099. static int
  2100. ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
  2101. {
  2102. if (!dev->ethtool_ops->flash_device) {
  2103. req->devlink = netdev_to_devlink_get(dev);
  2104. return 0;
  2105. }
  2106. return dev->ethtool_ops->flash_device(dev, &req->efl);
  2107. }
  2108. static int ethtool_set_dump(struct net_device *dev,
  2109. void __user *useraddr)
  2110. {
  2111. struct ethtool_dump dump;
  2112. if (!dev->ethtool_ops->set_dump)
  2113. return -EOPNOTSUPP;
  2114. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  2115. return -EFAULT;
  2116. return dev->ethtool_ops->set_dump(dev, &dump);
  2117. }
  2118. static int ethtool_get_dump_flag(struct net_device *dev,
  2119. void __user *useraddr)
  2120. {
  2121. int ret;
  2122. struct ethtool_dump dump;
  2123. const struct ethtool_ops *ops = dev->ethtool_ops;
  2124. if (!ops->get_dump_flag)
  2125. return -EOPNOTSUPP;
  2126. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  2127. return -EFAULT;
  2128. ret = ops->get_dump_flag(dev, &dump);
  2129. if (ret)
  2130. return ret;
  2131. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  2132. return -EFAULT;
  2133. return 0;
  2134. }
  2135. static int ethtool_get_dump_data(struct net_device *dev,
  2136. void __user *useraddr)
  2137. {
  2138. int ret;
  2139. __u32 len;
  2140. struct ethtool_dump dump, tmp;
  2141. const struct ethtool_ops *ops = dev->ethtool_ops;
  2142. void *data = NULL;
  2143. if (!ops->get_dump_data || !ops->get_dump_flag)
  2144. return -EOPNOTSUPP;
  2145. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  2146. return -EFAULT;
  2147. memset(&tmp, 0, sizeof(tmp));
  2148. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  2149. ret = ops->get_dump_flag(dev, &tmp);
  2150. if (ret)
  2151. return ret;
  2152. len = min(tmp.len, dump.len);
  2153. if (!len)
  2154. return -EFAULT;
  2155. /* Don't ever let the driver think there's more space available
  2156. * than it requested with .get_dump_flag().
  2157. */
  2158. dump.len = len;
  2159. /* Always allocate enough space to hold the whole thing so that the
  2160. * driver does not need to check the length and bother with partial
  2161. * dumping.
  2162. */
  2163. data = vzalloc(tmp.len);
  2164. if (!data)
  2165. return -ENOMEM;
  2166. ret = ops->get_dump_data(dev, &dump, data);
  2167. if (ret)
  2168. goto out;
  2169. /* There are two sane possibilities:
  2170. * 1. The driver's .get_dump_data() does not touch dump.len.
  2171. * 2. Or it may set dump.len to how much it really writes, which
  2172. * should be tmp.len (or len if it can do a partial dump).
  2173. * In any case respond to userspace with the actual length of data
  2174. * it's receiving.
  2175. */
  2176. WARN_ON(dump.len != len && dump.len != tmp.len);
  2177. dump.len = len;
  2178. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  2179. ret = -EFAULT;
  2180. goto out;
  2181. }
  2182. useraddr += offsetof(struct ethtool_dump, data);
  2183. if (copy_to_user(useraddr, data, len))
  2184. ret = -EFAULT;
  2185. out:
  2186. vfree(data);
  2187. return ret;
  2188. }
  2189. static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
  2190. {
  2191. struct kernel_ethtool_ts_info kernel_info;
  2192. struct ethtool_ts_info info = {};
  2193. int err;
  2194. err = __ethtool_get_ts_info(dev, &kernel_info);
  2195. if (err)
  2196. return err;
  2197. info.cmd = kernel_info.cmd;
  2198. info.so_timestamping = kernel_info.so_timestamping;
  2199. info.phc_index = kernel_info.phc_index;
  2200. info.tx_types = kernel_info.tx_types;
  2201. info.rx_filters = kernel_info.rx_filters;
  2202. if (copy_to_user(useraddr, &info, sizeof(info)))
  2203. return -EFAULT;
  2204. return 0;
  2205. }
  2206. int ethtool_get_module_info_call(struct net_device *dev,
  2207. struct ethtool_modinfo *modinfo)
  2208. {
  2209. const struct ethtool_ops *ops = dev->ethtool_ops;
  2210. struct phy_device *phydev = dev->phydev;
  2211. if (dev->ethtool->module_fw_flash_in_progress)
  2212. return -EBUSY;
  2213. if (dev->sfp_bus)
  2214. return sfp_get_module_info(dev->sfp_bus, modinfo);
  2215. if (phydev && phydev->drv && phydev->drv->module_info)
  2216. return phydev->drv->module_info(phydev, modinfo);
  2217. if (ops->get_module_info)
  2218. return ops->get_module_info(dev, modinfo);
  2219. return -EOPNOTSUPP;
  2220. }
  2221. static int ethtool_get_module_info(struct net_device *dev,
  2222. void __user *useraddr)
  2223. {
  2224. int ret;
  2225. struct ethtool_modinfo modinfo;
  2226. if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
  2227. return -EFAULT;
  2228. ret = ethtool_get_module_info_call(dev, &modinfo);
  2229. if (ret)
  2230. return ret;
  2231. if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
  2232. return -EFAULT;
  2233. return 0;
  2234. }
  2235. int ethtool_get_module_eeprom_call(struct net_device *dev,
  2236. struct ethtool_eeprom *ee, u8 *data)
  2237. {
  2238. const struct ethtool_ops *ops = dev->ethtool_ops;
  2239. struct phy_device *phydev = dev->phydev;
  2240. if (dev->ethtool->module_fw_flash_in_progress)
  2241. return -EBUSY;
  2242. if (dev->sfp_bus)
  2243. return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
  2244. if (phydev && phydev->drv && phydev->drv->module_eeprom)
  2245. return phydev->drv->module_eeprom(phydev, ee, data);
  2246. if (ops->get_module_eeprom)
  2247. return ops->get_module_eeprom(dev, ee, data);
  2248. return -EOPNOTSUPP;
  2249. }
  2250. static int ethtool_get_module_eeprom(struct net_device *dev,
  2251. void __user *useraddr)
  2252. {
  2253. int ret;
  2254. struct ethtool_modinfo modinfo;
  2255. ret = ethtool_get_module_info_call(dev, &modinfo);
  2256. if (ret)
  2257. return ret;
  2258. return ethtool_get_any_eeprom(dev, useraddr,
  2259. ethtool_get_module_eeprom_call,
  2260. modinfo.eeprom_len);
  2261. }
  2262. static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
  2263. {
  2264. switch (tuna->id) {
  2265. case ETHTOOL_RX_COPYBREAK:
  2266. case ETHTOOL_TX_COPYBREAK:
  2267. case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
  2268. if (tuna->len != sizeof(u32) ||
  2269. tuna->type_id != ETHTOOL_TUNABLE_U32)
  2270. return -EINVAL;
  2271. break;
  2272. case ETHTOOL_PFC_PREVENTION_TOUT:
  2273. if (tuna->len != sizeof(u16) ||
  2274. tuna->type_id != ETHTOOL_TUNABLE_U16)
  2275. return -EINVAL;
  2276. break;
  2277. default:
  2278. return -EINVAL;
  2279. }
  2280. return 0;
  2281. }
  2282. static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
  2283. {
  2284. int ret;
  2285. struct ethtool_tunable tuna;
  2286. const struct ethtool_ops *ops = dev->ethtool_ops;
  2287. void *data;
  2288. if (!ops->get_tunable)
  2289. return -EOPNOTSUPP;
  2290. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2291. return -EFAULT;
  2292. ret = ethtool_tunable_valid(&tuna);
  2293. if (ret)
  2294. return ret;
  2295. data = kzalloc(tuna.len, GFP_USER);
  2296. if (!data)
  2297. return -ENOMEM;
  2298. ret = ops->get_tunable(dev, &tuna, data);
  2299. if (ret)
  2300. goto out;
  2301. useraddr += sizeof(tuna);
  2302. ret = -EFAULT;
  2303. if (copy_to_user(useraddr, data, tuna.len))
  2304. goto out;
  2305. ret = 0;
  2306. out:
  2307. kfree(data);
  2308. return ret;
  2309. }
  2310. static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
  2311. {
  2312. int ret;
  2313. struct ethtool_tunable tuna;
  2314. const struct ethtool_ops *ops = dev->ethtool_ops;
  2315. void *data;
  2316. if (!ops->set_tunable)
  2317. return -EOPNOTSUPP;
  2318. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2319. return -EFAULT;
  2320. ret = ethtool_tunable_valid(&tuna);
  2321. if (ret)
  2322. return ret;
  2323. useraddr += sizeof(tuna);
  2324. data = memdup_user(useraddr, tuna.len);
  2325. if (IS_ERR(data))
  2326. return PTR_ERR(data);
  2327. ret = ops->set_tunable(dev, &tuna, data);
  2328. kfree(data);
  2329. return ret;
  2330. }
  2331. static noinline_for_stack int
  2332. ethtool_get_per_queue_coalesce(struct net_device *dev,
  2333. void __user *useraddr,
  2334. struct ethtool_per_queue_op *per_queue_opt)
  2335. {
  2336. u32 bit;
  2337. int ret;
  2338. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  2339. if (!dev->ethtool_ops->get_per_queue_coalesce)
  2340. return -EOPNOTSUPP;
  2341. useraddr += sizeof(*per_queue_opt);
  2342. bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
  2343. MAX_NUM_QUEUE);
  2344. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  2345. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  2346. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
  2347. if (ret != 0)
  2348. return ret;
  2349. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  2350. return -EFAULT;
  2351. useraddr += sizeof(coalesce);
  2352. }
  2353. return 0;
  2354. }
  2355. static noinline_for_stack int
  2356. ethtool_set_per_queue_coalesce(struct net_device *dev,
  2357. void __user *useraddr,
  2358. struct ethtool_per_queue_op *per_queue_opt)
  2359. {
  2360. u32 bit;
  2361. int i, ret = 0;
  2362. int n_queue;
  2363. struct ethtool_coalesce *backup = NULL, *tmp = NULL;
  2364. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  2365. if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
  2366. (!dev->ethtool_ops->get_per_queue_coalesce))
  2367. return -EOPNOTSUPP;
  2368. useraddr += sizeof(*per_queue_opt);
  2369. bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
  2370. n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
  2371. tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
  2372. if (!backup)
  2373. return -ENOMEM;
  2374. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  2375. struct ethtool_coalesce coalesce;
  2376. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
  2377. if (ret != 0)
  2378. goto roll_back;
  2379. tmp++;
  2380. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
  2381. ret = -EFAULT;
  2382. goto roll_back;
  2383. }
  2384. if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
  2385. ret = -EOPNOTSUPP;
  2386. goto roll_back;
  2387. }
  2388. ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
  2389. if (ret != 0)
  2390. goto roll_back;
  2391. useraddr += sizeof(coalesce);
  2392. }
  2393. roll_back:
  2394. if (ret != 0) {
  2395. tmp = backup;
  2396. for_each_set_bit(i, queue_mask, bit) {
  2397. dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
  2398. tmp++;
  2399. }
  2400. }
  2401. kfree(backup);
  2402. return ret;
  2403. }
  2404. static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
  2405. void __user *useraddr, u32 sub_cmd)
  2406. {
  2407. struct ethtool_per_queue_op per_queue_opt;
  2408. if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
  2409. return -EFAULT;
  2410. if (per_queue_opt.sub_command != sub_cmd)
  2411. return -EINVAL;
  2412. switch (per_queue_opt.sub_command) {
  2413. case ETHTOOL_GCOALESCE:
  2414. return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  2415. case ETHTOOL_SCOALESCE:
  2416. return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  2417. default:
  2418. return -EOPNOTSUPP;
  2419. }
  2420. }
  2421. static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
  2422. {
  2423. switch (tuna->id) {
  2424. case ETHTOOL_PHY_DOWNSHIFT:
  2425. case ETHTOOL_PHY_FAST_LINK_DOWN:
  2426. if (tuna->len != sizeof(u8) ||
  2427. tuna->type_id != ETHTOOL_TUNABLE_U8)
  2428. return -EINVAL;
  2429. break;
  2430. case ETHTOOL_PHY_EDPD:
  2431. if (tuna->len != sizeof(u16) ||
  2432. tuna->type_id != ETHTOOL_TUNABLE_U16)
  2433. return -EINVAL;
  2434. break;
  2435. default:
  2436. return -EINVAL;
  2437. }
  2438. return 0;
  2439. }
  2440. static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
  2441. {
  2442. struct phy_device *phydev = dev->phydev;
  2443. struct ethtool_tunable tuna;
  2444. bool phy_drv_tunable;
  2445. void *data;
  2446. int ret;
  2447. phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
  2448. if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
  2449. return -EOPNOTSUPP;
  2450. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2451. return -EFAULT;
  2452. ret = ethtool_phy_tunable_valid(&tuna);
  2453. if (ret)
  2454. return ret;
  2455. data = kzalloc(tuna.len, GFP_USER);
  2456. if (!data)
  2457. return -ENOMEM;
  2458. if (phy_drv_tunable) {
  2459. mutex_lock(&phydev->lock);
  2460. ret = phydev->drv->get_tunable(phydev, &tuna, data);
  2461. mutex_unlock(&phydev->lock);
  2462. } else {
  2463. ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
  2464. }
  2465. if (ret)
  2466. goto out;
  2467. useraddr += sizeof(tuna);
  2468. ret = -EFAULT;
  2469. if (copy_to_user(useraddr, data, tuna.len))
  2470. goto out;
  2471. ret = 0;
  2472. out:
  2473. kfree(data);
  2474. return ret;
  2475. }
  2476. static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
  2477. {
  2478. struct phy_device *phydev = dev->phydev;
  2479. struct ethtool_tunable tuna;
  2480. bool phy_drv_tunable;
  2481. void *data;
  2482. int ret;
  2483. phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
  2484. if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
  2485. return -EOPNOTSUPP;
  2486. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2487. return -EFAULT;
  2488. ret = ethtool_phy_tunable_valid(&tuna);
  2489. if (ret)
  2490. return ret;
  2491. useraddr += sizeof(tuna);
  2492. data = memdup_user(useraddr, tuna.len);
  2493. if (IS_ERR(data))
  2494. return PTR_ERR(data);
  2495. if (phy_drv_tunable) {
  2496. mutex_lock(&phydev->lock);
  2497. ret = phydev->drv->set_tunable(phydev, &tuna, data);
  2498. mutex_unlock(&phydev->lock);
  2499. } else {
  2500. ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
  2501. }
  2502. kfree(data);
  2503. return ret;
  2504. }
  2505. static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
  2506. {
  2507. struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
  2508. int rc;
  2509. if (!dev->ethtool_ops->get_fecparam)
  2510. return -EOPNOTSUPP;
  2511. rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
  2512. if (rc)
  2513. return rc;
  2514. if (WARN_ON_ONCE(fecparam.reserved))
  2515. fecparam.reserved = 0;
  2516. if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
  2517. return -EFAULT;
  2518. return 0;
  2519. }
  2520. static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
  2521. {
  2522. struct ethtool_fecparam fecparam;
  2523. if (!dev->ethtool_ops->set_fecparam)
  2524. return -EOPNOTSUPP;
  2525. if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
  2526. return -EFAULT;
  2527. if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
  2528. return -EINVAL;
  2529. fecparam.active_fec = 0;
  2530. fecparam.reserved = 0;
  2531. return dev->ethtool_ops->set_fecparam(dev, &fecparam);
  2532. }
  2533. /* The main entry point in this file. Called from net/core/dev_ioctl.c */
  2534. static int
  2535. __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
  2536. u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
  2537. {
  2538. struct net_device *dev;
  2539. u32 sub_cmd;
  2540. int rc;
  2541. netdev_features_t old_features;
  2542. dev = __dev_get_by_name(net, ifr->ifr_name);
  2543. if (!dev)
  2544. return -ENODEV;
  2545. if (ethcmd == ETHTOOL_PERQUEUE) {
  2546. if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
  2547. return -EFAULT;
  2548. } else {
  2549. sub_cmd = ethcmd;
  2550. }
  2551. /* Allow some commands to be done by anyone */
  2552. switch (sub_cmd) {
  2553. case ETHTOOL_GSET:
  2554. case ETHTOOL_GDRVINFO:
  2555. case ETHTOOL_GMSGLVL:
  2556. case ETHTOOL_GLINK:
  2557. case ETHTOOL_GCOALESCE:
  2558. case ETHTOOL_GRINGPARAM:
  2559. case ETHTOOL_GPAUSEPARAM:
  2560. case ETHTOOL_GRXCSUM:
  2561. case ETHTOOL_GTXCSUM:
  2562. case ETHTOOL_GSG:
  2563. case ETHTOOL_GSSET_INFO:
  2564. case ETHTOOL_GSTRINGS:
  2565. case ETHTOOL_GSTATS:
  2566. case ETHTOOL_GPHYSTATS:
  2567. case ETHTOOL_GTSO:
  2568. case ETHTOOL_GPERMADDR:
  2569. case ETHTOOL_GUFO:
  2570. case ETHTOOL_GGSO:
  2571. case ETHTOOL_GGRO:
  2572. case ETHTOOL_GFLAGS:
  2573. case ETHTOOL_GPFLAGS:
  2574. case ETHTOOL_GRXFH:
  2575. case ETHTOOL_GRXRINGS:
  2576. case ETHTOOL_GRXCLSRLCNT:
  2577. case ETHTOOL_GRXCLSRULE:
  2578. case ETHTOOL_GRXCLSRLALL:
  2579. case ETHTOOL_GRXFHINDIR:
  2580. case ETHTOOL_GRSSH:
  2581. case ETHTOOL_GFEATURES:
  2582. case ETHTOOL_GCHANNELS:
  2583. case ETHTOOL_GET_TS_INFO:
  2584. case ETHTOOL_GEEE:
  2585. case ETHTOOL_GTUNABLE:
  2586. case ETHTOOL_PHY_GTUNABLE:
  2587. case ETHTOOL_GLINKSETTINGS:
  2588. case ETHTOOL_GFECPARAM:
  2589. break;
  2590. default:
  2591. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2592. return -EPERM;
  2593. }
  2594. if (dev->dev.parent)
  2595. pm_runtime_get_sync(dev->dev.parent);
  2596. if (!netif_device_present(dev)) {
  2597. rc = -ENODEV;
  2598. goto out;
  2599. }
  2600. if (dev->ethtool_ops->begin) {
  2601. rc = dev->ethtool_ops->begin(dev);
  2602. if (rc < 0)
  2603. goto out;
  2604. }
  2605. old_features = dev->features;
  2606. switch (ethcmd) {
  2607. case ETHTOOL_GSET:
  2608. rc = ethtool_get_settings(dev, useraddr);
  2609. break;
  2610. case ETHTOOL_SSET:
  2611. rc = ethtool_set_settings(dev, useraddr);
  2612. break;
  2613. case ETHTOOL_GDRVINFO:
  2614. rc = ethtool_get_drvinfo(dev, devlink_state);
  2615. break;
  2616. case ETHTOOL_GREGS:
  2617. rc = ethtool_get_regs(dev, useraddr);
  2618. break;
  2619. case ETHTOOL_GWOL:
  2620. rc = ethtool_get_wol(dev, useraddr);
  2621. break;
  2622. case ETHTOOL_SWOL:
  2623. rc = ethtool_set_wol(dev, useraddr);
  2624. break;
  2625. case ETHTOOL_GMSGLVL:
  2626. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2627. dev->ethtool_ops->get_msglevel);
  2628. break;
  2629. case ETHTOOL_SMSGLVL:
  2630. rc = ethtool_set_value_void(dev, useraddr,
  2631. dev->ethtool_ops->set_msglevel);
  2632. if (!rc)
  2633. ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
  2634. break;
  2635. case ETHTOOL_GEEE:
  2636. rc = ethtool_get_eee(dev, useraddr);
  2637. break;
  2638. case ETHTOOL_SEEE:
  2639. rc = ethtool_set_eee(dev, useraddr);
  2640. break;
  2641. case ETHTOOL_NWAY_RST:
  2642. rc = ethtool_nway_reset(dev);
  2643. break;
  2644. case ETHTOOL_GLINK:
  2645. rc = ethtool_get_link(dev, useraddr);
  2646. break;
  2647. case ETHTOOL_GEEPROM:
  2648. rc = ethtool_get_eeprom(dev, useraddr);
  2649. break;
  2650. case ETHTOOL_SEEPROM:
  2651. rc = ethtool_set_eeprom(dev, useraddr);
  2652. break;
  2653. case ETHTOOL_GCOALESCE:
  2654. rc = ethtool_get_coalesce(dev, useraddr);
  2655. break;
  2656. case ETHTOOL_SCOALESCE:
  2657. rc = ethtool_set_coalesce(dev, useraddr);
  2658. break;
  2659. case ETHTOOL_GRINGPARAM:
  2660. rc = ethtool_get_ringparam(dev, useraddr);
  2661. break;
  2662. case ETHTOOL_SRINGPARAM:
  2663. rc = ethtool_set_ringparam(dev, useraddr);
  2664. break;
  2665. case ETHTOOL_GPAUSEPARAM:
  2666. rc = ethtool_get_pauseparam(dev, useraddr);
  2667. break;
  2668. case ETHTOOL_SPAUSEPARAM:
  2669. rc = ethtool_set_pauseparam(dev, useraddr);
  2670. break;
  2671. case ETHTOOL_TEST:
  2672. rc = ethtool_self_test(dev, useraddr);
  2673. break;
  2674. case ETHTOOL_GSTRINGS:
  2675. rc = ethtool_get_strings(dev, useraddr);
  2676. break;
  2677. case ETHTOOL_PHYS_ID:
  2678. rc = ethtool_phys_id(dev, useraddr);
  2679. break;
  2680. case ETHTOOL_GSTATS:
  2681. rc = ethtool_get_stats(dev, useraddr);
  2682. break;
  2683. case ETHTOOL_GPERMADDR:
  2684. rc = ethtool_get_perm_addr(dev, useraddr);
  2685. break;
  2686. case ETHTOOL_GFLAGS:
  2687. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2688. __ethtool_get_flags);
  2689. break;
  2690. case ETHTOOL_SFLAGS:
  2691. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  2692. break;
  2693. case ETHTOOL_GPFLAGS:
  2694. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2695. dev->ethtool_ops->get_priv_flags);
  2696. if (!rc)
  2697. ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
  2698. break;
  2699. case ETHTOOL_SPFLAGS:
  2700. rc = ethtool_set_value(dev, useraddr,
  2701. dev->ethtool_ops->set_priv_flags);
  2702. break;
  2703. case ETHTOOL_GRXFH:
  2704. case ETHTOOL_GRXRINGS:
  2705. case ETHTOOL_GRXCLSRLCNT:
  2706. case ETHTOOL_GRXCLSRULE:
  2707. case ETHTOOL_GRXCLSRLALL:
  2708. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  2709. break;
  2710. case ETHTOOL_SRXFH:
  2711. case ETHTOOL_SRXCLSRLDEL:
  2712. case ETHTOOL_SRXCLSRLINS:
  2713. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  2714. break;
  2715. case ETHTOOL_FLASHDEV:
  2716. rc = ethtool_flash_device(dev, devlink_state);
  2717. break;
  2718. case ETHTOOL_RESET:
  2719. rc = ethtool_reset(dev, useraddr);
  2720. break;
  2721. case ETHTOOL_GSSET_INFO:
  2722. rc = ethtool_get_sset_info(dev, useraddr);
  2723. break;
  2724. case ETHTOOL_GRXFHINDIR:
  2725. rc = ethtool_get_rxfh_indir(dev, useraddr);
  2726. break;
  2727. case ETHTOOL_SRXFHINDIR:
  2728. rc = ethtool_set_rxfh_indir(dev, useraddr);
  2729. break;
  2730. case ETHTOOL_GRSSH:
  2731. rc = ethtool_get_rxfh(dev, useraddr);
  2732. break;
  2733. case ETHTOOL_SRSSH:
  2734. rc = ethtool_set_rxfh(dev, useraddr);
  2735. break;
  2736. case ETHTOOL_GFEATURES:
  2737. rc = ethtool_get_features(dev, useraddr);
  2738. break;
  2739. case ETHTOOL_SFEATURES:
  2740. rc = ethtool_set_features(dev, useraddr);
  2741. break;
  2742. case ETHTOOL_GTXCSUM:
  2743. case ETHTOOL_GRXCSUM:
  2744. case ETHTOOL_GSG:
  2745. case ETHTOOL_GTSO:
  2746. case ETHTOOL_GGSO:
  2747. case ETHTOOL_GGRO:
  2748. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  2749. break;
  2750. case ETHTOOL_STXCSUM:
  2751. case ETHTOOL_SRXCSUM:
  2752. case ETHTOOL_SSG:
  2753. case ETHTOOL_STSO:
  2754. case ETHTOOL_SGSO:
  2755. case ETHTOOL_SGRO:
  2756. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  2757. break;
  2758. case ETHTOOL_GCHANNELS:
  2759. rc = ethtool_get_channels(dev, useraddr);
  2760. break;
  2761. case ETHTOOL_SCHANNELS:
  2762. rc = ethtool_set_channels(dev, useraddr);
  2763. break;
  2764. case ETHTOOL_SET_DUMP:
  2765. rc = ethtool_set_dump(dev, useraddr);
  2766. break;
  2767. case ETHTOOL_GET_DUMP_FLAG:
  2768. rc = ethtool_get_dump_flag(dev, useraddr);
  2769. break;
  2770. case ETHTOOL_GET_DUMP_DATA:
  2771. rc = ethtool_get_dump_data(dev, useraddr);
  2772. break;
  2773. case ETHTOOL_GET_TS_INFO:
  2774. rc = ethtool_get_ts_info(dev, useraddr);
  2775. break;
  2776. case ETHTOOL_GMODULEINFO:
  2777. rc = ethtool_get_module_info(dev, useraddr);
  2778. break;
  2779. case ETHTOOL_GMODULEEEPROM:
  2780. rc = ethtool_get_module_eeprom(dev, useraddr);
  2781. break;
  2782. case ETHTOOL_GTUNABLE:
  2783. rc = ethtool_get_tunable(dev, useraddr);
  2784. break;
  2785. case ETHTOOL_STUNABLE:
  2786. rc = ethtool_set_tunable(dev, useraddr);
  2787. break;
  2788. case ETHTOOL_GPHYSTATS:
  2789. rc = ethtool_get_phy_stats(dev, useraddr);
  2790. break;
  2791. case ETHTOOL_PERQUEUE:
  2792. rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
  2793. break;
  2794. case ETHTOOL_GLINKSETTINGS:
  2795. rc = ethtool_get_link_ksettings(dev, useraddr);
  2796. break;
  2797. case ETHTOOL_SLINKSETTINGS:
  2798. rc = ethtool_set_link_ksettings(dev, useraddr);
  2799. break;
  2800. case ETHTOOL_PHY_GTUNABLE:
  2801. rc = get_phy_tunable(dev, useraddr);
  2802. break;
  2803. case ETHTOOL_PHY_STUNABLE:
  2804. rc = set_phy_tunable(dev, useraddr);
  2805. break;
  2806. case ETHTOOL_GFECPARAM:
  2807. rc = ethtool_get_fecparam(dev, useraddr);
  2808. break;
  2809. case ETHTOOL_SFECPARAM:
  2810. rc = ethtool_set_fecparam(dev, useraddr);
  2811. break;
  2812. default:
  2813. rc = -EOPNOTSUPP;
  2814. }
  2815. if (dev->ethtool_ops->complete)
  2816. dev->ethtool_ops->complete(dev);
  2817. if (old_features != dev->features)
  2818. netdev_features_change(dev);
  2819. out:
  2820. if (dev->dev.parent)
  2821. pm_runtime_put(dev->dev.parent);
  2822. return rc;
  2823. }
  2824. int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
  2825. {
  2826. struct ethtool_devlink_compat *state;
  2827. u32 ethcmd;
  2828. int rc;
  2829. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  2830. return -EFAULT;
  2831. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2832. if (!state)
  2833. return -ENOMEM;
  2834. switch (ethcmd) {
  2835. case ETHTOOL_FLASHDEV:
  2836. if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
  2837. rc = -EFAULT;
  2838. goto exit_free;
  2839. }
  2840. state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
  2841. break;
  2842. }
  2843. rtnl_lock();
  2844. rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
  2845. rtnl_unlock();
  2846. if (rc)
  2847. goto exit_free;
  2848. switch (ethcmd) {
  2849. case ETHTOOL_FLASHDEV:
  2850. if (state->devlink)
  2851. rc = devlink_compat_flash_update(state->devlink,
  2852. state->efl.data);
  2853. break;
  2854. case ETHTOOL_GDRVINFO:
  2855. if (state->devlink)
  2856. devlink_compat_running_version(state->devlink,
  2857. state->info.fw_version,
  2858. sizeof(state->info.fw_version));
  2859. if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
  2860. rc = -EFAULT;
  2861. goto exit_free;
  2862. }
  2863. break;
  2864. }
  2865. exit_free:
  2866. if (state->devlink)
  2867. devlink_put(state->devlink);
  2868. kfree(state);
  2869. return rc;
  2870. }
  2871. struct ethtool_rx_flow_key {
  2872. struct flow_dissector_key_basic basic;
  2873. union {
  2874. struct flow_dissector_key_ipv4_addrs ipv4;
  2875. struct flow_dissector_key_ipv6_addrs ipv6;
  2876. };
  2877. struct flow_dissector_key_ports tp;
  2878. struct flow_dissector_key_ip ip;
  2879. struct flow_dissector_key_vlan vlan;
  2880. struct flow_dissector_key_eth_addrs eth_addrs;
  2881. } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  2882. struct ethtool_rx_flow_match {
  2883. struct flow_dissector dissector;
  2884. struct ethtool_rx_flow_key key;
  2885. struct ethtool_rx_flow_key mask;
  2886. };
  2887. struct ethtool_rx_flow_rule *
  2888. ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
  2889. {
  2890. const struct ethtool_rx_flow_spec *fs = input->fs;
  2891. struct ethtool_rx_flow_match *match;
  2892. struct ethtool_rx_flow_rule *flow;
  2893. struct flow_action_entry *act;
  2894. flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
  2895. sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
  2896. if (!flow)
  2897. return ERR_PTR(-ENOMEM);
  2898. /* ethtool_rx supports only one single action per rule. */
  2899. flow->rule = flow_rule_alloc(1);
  2900. if (!flow->rule) {
  2901. kfree(flow);
  2902. return ERR_PTR(-ENOMEM);
  2903. }
  2904. match = (struct ethtool_rx_flow_match *)flow->priv;
  2905. flow->rule->match.dissector = &match->dissector;
  2906. flow->rule->match.mask = &match->mask;
  2907. flow->rule->match.key = &match->key;
  2908. match->mask.basic.n_proto = htons(0xffff);
  2909. switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
  2910. case ETHER_FLOW: {
  2911. const struct ethhdr *ether_spec, *ether_m_spec;
  2912. ether_spec = &fs->h_u.ether_spec;
  2913. ether_m_spec = &fs->m_u.ether_spec;
  2914. if (!is_zero_ether_addr(ether_m_spec->h_source)) {
  2915. ether_addr_copy(match->key.eth_addrs.src,
  2916. ether_spec->h_source);
  2917. ether_addr_copy(match->mask.eth_addrs.src,
  2918. ether_m_spec->h_source);
  2919. }
  2920. if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
  2921. ether_addr_copy(match->key.eth_addrs.dst,
  2922. ether_spec->h_dest);
  2923. ether_addr_copy(match->mask.eth_addrs.dst,
  2924. ether_m_spec->h_dest);
  2925. }
  2926. if (ether_m_spec->h_proto) {
  2927. match->key.basic.n_proto = ether_spec->h_proto;
  2928. match->mask.basic.n_proto = ether_m_spec->h_proto;
  2929. }
  2930. }
  2931. break;
  2932. case TCP_V4_FLOW:
  2933. case UDP_V4_FLOW: {
  2934. const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
  2935. match->key.basic.n_proto = htons(ETH_P_IP);
  2936. v4_spec = &fs->h_u.tcp_ip4_spec;
  2937. v4_m_spec = &fs->m_u.tcp_ip4_spec;
  2938. if (v4_m_spec->ip4src) {
  2939. match->key.ipv4.src = v4_spec->ip4src;
  2940. match->mask.ipv4.src = v4_m_spec->ip4src;
  2941. }
  2942. if (v4_m_spec->ip4dst) {
  2943. match->key.ipv4.dst = v4_spec->ip4dst;
  2944. match->mask.ipv4.dst = v4_m_spec->ip4dst;
  2945. }
  2946. if (v4_m_spec->ip4src ||
  2947. v4_m_spec->ip4dst) {
  2948. match->dissector.used_keys |=
  2949. BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
  2950. match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
  2951. offsetof(struct ethtool_rx_flow_key, ipv4);
  2952. }
  2953. if (v4_m_spec->psrc) {
  2954. match->key.tp.src = v4_spec->psrc;
  2955. match->mask.tp.src = v4_m_spec->psrc;
  2956. }
  2957. if (v4_m_spec->pdst) {
  2958. match->key.tp.dst = v4_spec->pdst;
  2959. match->mask.tp.dst = v4_m_spec->pdst;
  2960. }
  2961. if (v4_m_spec->psrc ||
  2962. v4_m_spec->pdst) {
  2963. match->dissector.used_keys |=
  2964. BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
  2965. match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
  2966. offsetof(struct ethtool_rx_flow_key, tp);
  2967. }
  2968. if (v4_m_spec->tos) {
  2969. match->key.ip.tos = v4_spec->tos;
  2970. match->mask.ip.tos = v4_m_spec->tos;
  2971. match->dissector.used_keys |=
  2972. BIT(FLOW_DISSECTOR_KEY_IP);
  2973. match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
  2974. offsetof(struct ethtool_rx_flow_key, ip);
  2975. }
  2976. }
  2977. break;
  2978. case TCP_V6_FLOW:
  2979. case UDP_V6_FLOW: {
  2980. const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
  2981. match->key.basic.n_proto = htons(ETH_P_IPV6);
  2982. v6_spec = &fs->h_u.tcp_ip6_spec;
  2983. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  2984. if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
  2985. memcpy(&match->key.ipv6.src, v6_spec->ip6src,
  2986. sizeof(match->key.ipv6.src));
  2987. memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
  2988. sizeof(match->mask.ipv6.src));
  2989. }
  2990. if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
  2991. memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
  2992. sizeof(match->key.ipv6.dst));
  2993. memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
  2994. sizeof(match->mask.ipv6.dst));
  2995. }
  2996. if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
  2997. !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
  2998. match->dissector.used_keys |=
  2999. BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
  3000. match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
  3001. offsetof(struct ethtool_rx_flow_key, ipv6);
  3002. }
  3003. if (v6_m_spec->psrc) {
  3004. match->key.tp.src = v6_spec->psrc;
  3005. match->mask.tp.src = v6_m_spec->psrc;
  3006. }
  3007. if (v6_m_spec->pdst) {
  3008. match->key.tp.dst = v6_spec->pdst;
  3009. match->mask.tp.dst = v6_m_spec->pdst;
  3010. }
  3011. if (v6_m_spec->psrc ||
  3012. v6_m_spec->pdst) {
  3013. match->dissector.used_keys |=
  3014. BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
  3015. match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
  3016. offsetof(struct ethtool_rx_flow_key, tp);
  3017. }
  3018. if (v6_m_spec->tclass) {
  3019. match->key.ip.tos = v6_spec->tclass;
  3020. match->mask.ip.tos = v6_m_spec->tclass;
  3021. match->dissector.used_keys |=
  3022. BIT_ULL(FLOW_DISSECTOR_KEY_IP);
  3023. match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
  3024. offsetof(struct ethtool_rx_flow_key, ip);
  3025. }
  3026. }
  3027. break;
  3028. default:
  3029. ethtool_rx_flow_rule_destroy(flow);
  3030. return ERR_PTR(-EINVAL);
  3031. }
  3032. switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
  3033. case TCP_V4_FLOW:
  3034. case TCP_V6_FLOW:
  3035. match->key.basic.ip_proto = IPPROTO_TCP;
  3036. match->mask.basic.ip_proto = 0xff;
  3037. break;
  3038. case UDP_V4_FLOW:
  3039. case UDP_V6_FLOW:
  3040. match->key.basic.ip_proto = IPPROTO_UDP;
  3041. match->mask.basic.ip_proto = 0xff;
  3042. break;
  3043. }
  3044. match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
  3045. match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
  3046. offsetof(struct ethtool_rx_flow_key, basic);
  3047. if (fs->flow_type & FLOW_EXT) {
  3048. const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
  3049. const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
  3050. if (ext_m_spec->vlan_etype) {
  3051. match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
  3052. match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
  3053. }
  3054. if (ext_m_spec->vlan_tci) {
  3055. match->key.vlan.vlan_id =
  3056. ntohs(ext_h_spec->vlan_tci) & 0x0fff;
  3057. match->mask.vlan.vlan_id =
  3058. ntohs(ext_m_spec->vlan_tci) & 0x0fff;
  3059. match->key.vlan.vlan_dei =
  3060. !!(ext_h_spec->vlan_tci & htons(0x1000));
  3061. match->mask.vlan.vlan_dei =
  3062. !!(ext_m_spec->vlan_tci & htons(0x1000));
  3063. match->key.vlan.vlan_priority =
  3064. (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
  3065. match->mask.vlan.vlan_priority =
  3066. (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
  3067. }
  3068. if (ext_m_spec->vlan_etype ||
  3069. ext_m_spec->vlan_tci) {
  3070. match->dissector.used_keys |=
  3071. BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
  3072. match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
  3073. offsetof(struct ethtool_rx_flow_key, vlan);
  3074. }
  3075. }
  3076. if (fs->flow_type & FLOW_MAC_EXT) {
  3077. const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
  3078. const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
  3079. memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
  3080. ETH_ALEN);
  3081. memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
  3082. ETH_ALEN);
  3083. match->dissector.used_keys |=
  3084. BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
  3085. match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
  3086. offsetof(struct ethtool_rx_flow_key, eth_addrs);
  3087. }
  3088. act = &flow->rule->action.entries[0];
  3089. switch (fs->ring_cookie) {
  3090. case RX_CLS_FLOW_DISC:
  3091. act->id = FLOW_ACTION_DROP;
  3092. break;
  3093. case RX_CLS_FLOW_WAKE:
  3094. act->id = FLOW_ACTION_WAKE;
  3095. break;
  3096. default:
  3097. act->id = FLOW_ACTION_QUEUE;
  3098. if (fs->flow_type & FLOW_RSS)
  3099. act->queue.ctx = input->rss_ctx;
  3100. act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
  3101. act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
  3102. break;
  3103. }
  3104. return flow;
  3105. }
  3106. EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
  3107. void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
  3108. {
  3109. kfree(flow->rule);
  3110. kfree(flow);
  3111. }
  3112. EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);