ethtool.c 73 KB

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