ethtool.c 73 KB

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