ethtool.c 68 KB

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