ethtool.c 70 KB

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