ethtool.c 74 KB

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