ethtool.c 71 KB

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