ethtool.c 71 KB

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