ethtool.c 68 KB

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