ethtool.c 73 KB

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