ethtool.c 71 KB

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