ethtool.c 72 KB

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