ethtool.c 68 KB

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