ioctl.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/core/ethtool.c - Ethtool ioctl handler
  4. * Copyright (c) 2003 Matthew Wilcox <[email protected]>
  5. *
  6. * This file is where we call all the ethtool_ops commands to get
  7. * the information ethtool needs.
  8. */
  9. #include <linux/compat.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/capability.h>
  14. #include <linux/errno.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/net_tstamp.h>
  18. #include <linux/phy.h>
  19. #include <linux/bitops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/sfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/net.h>
  27. #include <linux/pm_runtime.h>
  28. #include <net/devlink.h>
  29. #ifndef __GENKSYMS__
  30. #include <net/ipv6.h>
  31. #endif
  32. #include <net/xdp_sock_drv.h>
  33. #include <net/flow_offload.h>
  34. #include <linux/ethtool_netlink.h>
  35. #include <generated/utsrelease.h>
  36. #include "common.h"
  37. /* State held across locks and calls for commands which have devlink fallback */
  38. struct ethtool_devlink_compat {
  39. struct devlink *devlink;
  40. union {
  41. struct ethtool_flash efl;
  42. struct ethtool_drvinfo info;
  43. };
  44. };
  45. static struct devlink *netdev_to_devlink_get(struct net_device *dev)
  46. {
  47. struct devlink_port *devlink_port;
  48. if (!dev->netdev_ops->ndo_get_devlink_port)
  49. return NULL;
  50. devlink_port = dev->netdev_ops->ndo_get_devlink_port(dev);
  51. if (!devlink_port)
  52. return NULL;
  53. return devlink_try_get(devlink_port->devlink);
  54. }
  55. /*
  56. * Some useful ethtool_ops methods that're device independent.
  57. * If we find that all drivers want to do the same thing here,
  58. * we can turn these into dev_() function calls.
  59. */
  60. u32 ethtool_op_get_link(struct net_device *dev)
  61. {
  62. return netif_carrier_ok(dev) ? 1 : 0;
  63. }
  64. EXPORT_SYMBOL(ethtool_op_get_link);
  65. int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
  66. {
  67. info->so_timestamping =
  68. SOF_TIMESTAMPING_TX_SOFTWARE |
  69. SOF_TIMESTAMPING_RX_SOFTWARE |
  70. SOF_TIMESTAMPING_SOFTWARE;
  71. info->phc_index = -1;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(ethtool_op_get_ts_info);
  75. /* Handlers for each ethtool command */
  76. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  77. {
  78. struct ethtool_gfeatures cmd = {
  79. .cmd = ETHTOOL_GFEATURES,
  80. .size = ETHTOOL_DEV_FEATURE_WORDS,
  81. };
  82. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  83. u32 __user *sizeaddr;
  84. u32 copy_size;
  85. int i;
  86. /* in case feature bits run out again */
  87. BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
  88. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  89. features[i].available = (u32)(dev->hw_features >> (32 * i));
  90. features[i].requested = (u32)(dev->wanted_features >> (32 * i));
  91. features[i].active = (u32)(dev->features >> (32 * i));
  92. features[i].never_changed =
  93. (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
  94. }
  95. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  96. if (get_user(copy_size, sizeaddr))
  97. return -EFAULT;
  98. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  99. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  100. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  101. return -EFAULT;
  102. useraddr += sizeof(cmd);
  103. if (copy_to_user(useraddr, features,
  104. array_size(copy_size, sizeof(*features))))
  105. return -EFAULT;
  106. return 0;
  107. }
  108. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  109. {
  110. struct ethtool_sfeatures cmd;
  111. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  112. netdev_features_t wanted = 0, valid = 0;
  113. int i, ret = 0;
  114. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  115. return -EFAULT;
  116. useraddr += sizeof(cmd);
  117. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  118. return -EINVAL;
  119. if (copy_from_user(features, useraddr, sizeof(features)))
  120. return -EFAULT;
  121. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  122. valid |= (netdev_features_t)features[i].valid << (32 * i);
  123. wanted |= (netdev_features_t)features[i].requested << (32 * i);
  124. }
  125. if (valid & ~NETIF_F_ETHTOOL_BITS)
  126. return -EINVAL;
  127. if (valid & ~dev->hw_features) {
  128. valid &= dev->hw_features;
  129. ret |= ETHTOOL_F_UNSUPPORTED;
  130. }
  131. dev->wanted_features &= ~valid;
  132. dev->wanted_features |= wanted & valid;
  133. __netdev_update_features(dev);
  134. if ((dev->wanted_features ^ dev->features) & valid)
  135. ret |= ETHTOOL_F_WISH;
  136. return ret;
  137. }
  138. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  139. {
  140. const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
  141. const struct ethtool_ops *ops = dev->ethtool_ops;
  142. if (sset == ETH_SS_FEATURES)
  143. return ARRAY_SIZE(netdev_features_strings);
  144. if (sset == ETH_SS_RSS_HASH_FUNCS)
  145. return ARRAY_SIZE(rss_hash_func_strings);
  146. if (sset == ETH_SS_TUNABLES)
  147. return ARRAY_SIZE(tunable_strings);
  148. if (sset == ETH_SS_PHY_TUNABLES)
  149. return ARRAY_SIZE(phy_tunable_strings);
  150. if (sset == ETH_SS_PHY_STATS && dev->phydev &&
  151. !ops->get_ethtool_phy_stats &&
  152. phy_ops && phy_ops->get_sset_count)
  153. return phy_ops->get_sset_count(dev->phydev);
  154. if (sset == ETH_SS_LINK_MODES)
  155. return __ETHTOOL_LINK_MODE_MASK_NBITS;
  156. if (ops->get_sset_count && ops->get_strings)
  157. return ops->get_sset_count(dev, sset);
  158. else
  159. return -EOPNOTSUPP;
  160. }
  161. static void __ethtool_get_strings(struct net_device *dev,
  162. u32 stringset, u8 *data)
  163. {
  164. const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
  165. const struct ethtool_ops *ops = dev->ethtool_ops;
  166. if (stringset == ETH_SS_FEATURES)
  167. memcpy(data, netdev_features_strings,
  168. sizeof(netdev_features_strings));
  169. else if (stringset == ETH_SS_RSS_HASH_FUNCS)
  170. memcpy(data, rss_hash_func_strings,
  171. sizeof(rss_hash_func_strings));
  172. else if (stringset == ETH_SS_TUNABLES)
  173. memcpy(data, tunable_strings, sizeof(tunable_strings));
  174. else if (stringset == ETH_SS_PHY_TUNABLES)
  175. memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
  176. else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
  177. !ops->get_ethtool_phy_stats && phy_ops &&
  178. phy_ops->get_strings)
  179. phy_ops->get_strings(dev->phydev, data);
  180. else if (stringset == ETH_SS_LINK_MODES)
  181. memcpy(data, link_mode_names,
  182. __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
  183. else
  184. /* ops->get_strings is valid because checked earlier */
  185. ops->get_strings(dev, stringset, data);
  186. }
  187. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  188. {
  189. /* feature masks of legacy discrete ethtool ops */
  190. switch (eth_cmd) {
  191. case ETHTOOL_GTXCSUM:
  192. case ETHTOOL_STXCSUM:
  193. return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
  194. NETIF_F_SCTP_CRC;
  195. case ETHTOOL_GRXCSUM:
  196. case ETHTOOL_SRXCSUM:
  197. return NETIF_F_RXCSUM;
  198. case ETHTOOL_GSG:
  199. case ETHTOOL_SSG:
  200. return NETIF_F_SG | NETIF_F_FRAGLIST;
  201. case ETHTOOL_GTSO:
  202. case ETHTOOL_STSO:
  203. return NETIF_F_ALL_TSO;
  204. case ETHTOOL_GGSO:
  205. case ETHTOOL_SGSO:
  206. return NETIF_F_GSO;
  207. case ETHTOOL_GGRO:
  208. case ETHTOOL_SGRO:
  209. return NETIF_F_GRO;
  210. default:
  211. BUG();
  212. }
  213. }
  214. static int ethtool_get_one_feature(struct net_device *dev,
  215. char __user *useraddr, u32 ethcmd)
  216. {
  217. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  218. struct ethtool_value edata = {
  219. .cmd = ethcmd,
  220. .data = !!(dev->features & mask),
  221. };
  222. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  223. return -EFAULT;
  224. return 0;
  225. }
  226. static int ethtool_set_one_feature(struct net_device *dev,
  227. void __user *useraddr, u32 ethcmd)
  228. {
  229. struct ethtool_value edata;
  230. netdev_features_t mask;
  231. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  232. return -EFAULT;
  233. mask = ethtool_get_feature_mask(ethcmd);
  234. mask &= dev->hw_features;
  235. if (!mask)
  236. return -EOPNOTSUPP;
  237. if (edata.data)
  238. dev->wanted_features |= mask;
  239. else
  240. dev->wanted_features &= ~mask;
  241. __netdev_update_features(dev);
  242. return 0;
  243. }
  244. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  245. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  246. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
  247. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
  248. NETIF_F_RXHASH)
  249. static u32 __ethtool_get_flags(struct net_device *dev)
  250. {
  251. u32 flags = 0;
  252. if (dev->features & NETIF_F_LRO)
  253. flags |= ETH_FLAG_LRO;
  254. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  255. flags |= ETH_FLAG_RXVLAN;
  256. if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
  257. flags |= ETH_FLAG_TXVLAN;
  258. if (dev->features & NETIF_F_NTUPLE)
  259. flags |= ETH_FLAG_NTUPLE;
  260. if (dev->features & NETIF_F_RXHASH)
  261. flags |= ETH_FLAG_RXHASH;
  262. return flags;
  263. }
  264. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  265. {
  266. netdev_features_t features = 0, changed;
  267. if (data & ~ETH_ALL_FLAGS)
  268. return -EINVAL;
  269. if (data & ETH_FLAG_LRO)
  270. features |= NETIF_F_LRO;
  271. if (data & ETH_FLAG_RXVLAN)
  272. features |= NETIF_F_HW_VLAN_CTAG_RX;
  273. if (data & ETH_FLAG_TXVLAN)
  274. features |= NETIF_F_HW_VLAN_CTAG_TX;
  275. if (data & ETH_FLAG_NTUPLE)
  276. features |= NETIF_F_NTUPLE;
  277. if (data & ETH_FLAG_RXHASH)
  278. features |= NETIF_F_RXHASH;
  279. /* allow changing only bits set in hw_features */
  280. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  281. if (changed & ~dev->hw_features)
  282. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  283. dev->wanted_features =
  284. (dev->wanted_features & ~changed) | (features & changed);
  285. __netdev_update_features(dev);
  286. return 0;
  287. }
  288. /* Given two link masks, AND them together and save the result in dst. */
  289. void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
  290. struct ethtool_link_ksettings *src)
  291. {
  292. unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
  293. unsigned int idx = 0;
  294. for (; idx < size; idx++) {
  295. dst->link_modes.supported[idx] &=
  296. src->link_modes.supported[idx];
  297. dst->link_modes.advertising[idx] &=
  298. src->link_modes.advertising[idx];
  299. }
  300. }
  301. EXPORT_SYMBOL(ethtool_intersect_link_masks);
  302. void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
  303. u32 legacy_u32)
  304. {
  305. linkmode_zero(dst);
  306. dst[0] = legacy_u32;
  307. }
  308. EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
  309. /* return false if src had higher bits set. lower bits always updated. */
  310. bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
  311. const unsigned long *src)
  312. {
  313. *legacy_u32 = src[0];
  314. return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
  315. __ETHTOOL_LINK_MODE_MASK_NBITS;
  316. }
  317. EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
  318. /* return false if ksettings link modes had higher bits
  319. * set. legacy_settings always updated (best effort)
  320. */
  321. static bool
  322. convert_link_ksettings_to_legacy_settings(
  323. struct ethtool_cmd *legacy_settings,
  324. const struct ethtool_link_ksettings *link_ksettings)
  325. {
  326. bool retval = true;
  327. memset(legacy_settings, 0, sizeof(*legacy_settings));
  328. /* this also clears the deprecated fields in legacy structure:
  329. * __u8 transceiver;
  330. * __u32 maxtxpkt;
  331. * __u32 maxrxpkt;
  332. */
  333. retval &= ethtool_convert_link_mode_to_legacy_u32(
  334. &legacy_settings->supported,
  335. link_ksettings->link_modes.supported);
  336. retval &= ethtool_convert_link_mode_to_legacy_u32(
  337. &legacy_settings->advertising,
  338. link_ksettings->link_modes.advertising);
  339. retval &= ethtool_convert_link_mode_to_legacy_u32(
  340. &legacy_settings->lp_advertising,
  341. link_ksettings->link_modes.lp_advertising);
  342. ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
  343. legacy_settings->duplex
  344. = link_ksettings->base.duplex;
  345. legacy_settings->port
  346. = link_ksettings->base.port;
  347. legacy_settings->phy_address
  348. = link_ksettings->base.phy_address;
  349. legacy_settings->autoneg
  350. = link_ksettings->base.autoneg;
  351. legacy_settings->mdio_support
  352. = link_ksettings->base.mdio_support;
  353. legacy_settings->eth_tp_mdix
  354. = link_ksettings->base.eth_tp_mdix;
  355. legacy_settings->eth_tp_mdix_ctrl
  356. = link_ksettings->base.eth_tp_mdix_ctrl;
  357. legacy_settings->transceiver
  358. = link_ksettings->base.transceiver;
  359. return retval;
  360. }
  361. /* number of 32-bit words to store the user's link mode bitmaps */
  362. #define __ETHTOOL_LINK_MODE_MASK_NU32 \
  363. DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
  364. /* layout of the struct passed from/to userland */
  365. struct ethtool_link_usettings {
  366. struct ethtool_link_settings base;
  367. struct {
  368. __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
  369. __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  370. __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  371. } link_modes;
  372. };
  373. /* Internal kernel helper to query a device ethtool_link_settings. */
  374. int __ethtool_get_link_ksettings(struct net_device *dev,
  375. struct ethtool_link_ksettings *link_ksettings)
  376. {
  377. ASSERT_RTNL();
  378. if (!dev->ethtool_ops->get_link_ksettings)
  379. return -EOPNOTSUPP;
  380. memset(link_ksettings, 0, sizeof(*link_ksettings));
  381. return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
  382. }
  383. EXPORT_SYMBOL(__ethtool_get_link_ksettings);
  384. /* convert ethtool_link_usettings in user space to a kernel internal
  385. * ethtool_link_ksettings. return 0 on success, errno on error.
  386. */
  387. static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
  388. const void __user *from)
  389. {
  390. struct ethtool_link_usettings link_usettings;
  391. if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
  392. return -EFAULT;
  393. memcpy(&to->base, &link_usettings.base, sizeof(to->base));
  394. bitmap_from_arr32(to->link_modes.supported,
  395. link_usettings.link_modes.supported,
  396. __ETHTOOL_LINK_MODE_MASK_NBITS);
  397. bitmap_from_arr32(to->link_modes.advertising,
  398. link_usettings.link_modes.advertising,
  399. __ETHTOOL_LINK_MODE_MASK_NBITS);
  400. bitmap_from_arr32(to->link_modes.lp_advertising,
  401. link_usettings.link_modes.lp_advertising,
  402. __ETHTOOL_LINK_MODE_MASK_NBITS);
  403. return 0;
  404. }
  405. /* Check if the user is trying to change anything besides speed/duplex */
  406. bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
  407. {
  408. struct ethtool_link_settings base2 = {};
  409. base2.speed = cmd->base.speed;
  410. base2.port = PORT_OTHER;
  411. base2.duplex = cmd->base.duplex;
  412. base2.cmd = cmd->base.cmd;
  413. base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
  414. return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
  415. bitmap_empty(cmd->link_modes.supported,
  416. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  417. bitmap_empty(cmd->link_modes.lp_advertising,
  418. __ETHTOOL_LINK_MODE_MASK_NBITS);
  419. }
  420. /* convert a kernel internal ethtool_link_ksettings to
  421. * ethtool_link_usettings in user space. return 0 on success, errno on
  422. * error.
  423. */
  424. static int
  425. store_link_ksettings_for_user(void __user *to,
  426. const struct ethtool_link_ksettings *from)
  427. {
  428. struct ethtool_link_usettings link_usettings;
  429. memcpy(&link_usettings, from, sizeof(link_usettings));
  430. bitmap_to_arr32(link_usettings.link_modes.supported,
  431. from->link_modes.supported,
  432. __ETHTOOL_LINK_MODE_MASK_NBITS);
  433. bitmap_to_arr32(link_usettings.link_modes.advertising,
  434. from->link_modes.advertising,
  435. __ETHTOOL_LINK_MODE_MASK_NBITS);
  436. bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
  437. from->link_modes.lp_advertising,
  438. __ETHTOOL_LINK_MODE_MASK_NBITS);
  439. if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
  440. return -EFAULT;
  441. return 0;
  442. }
  443. /* Query device for its ethtool_link_settings. */
  444. static int ethtool_get_link_ksettings(struct net_device *dev,
  445. void __user *useraddr)
  446. {
  447. int err = 0;
  448. struct ethtool_link_ksettings link_ksettings;
  449. ASSERT_RTNL();
  450. if (!dev->ethtool_ops->get_link_ksettings)
  451. return -EOPNOTSUPP;
  452. /* handle bitmap nbits handshake */
  453. if (copy_from_user(&link_ksettings.base, useraddr,
  454. sizeof(link_ksettings.base)))
  455. return -EFAULT;
  456. if (__ETHTOOL_LINK_MODE_MASK_NU32
  457. != link_ksettings.base.link_mode_masks_nwords) {
  458. /* wrong link mode nbits requested */
  459. memset(&link_ksettings, 0, sizeof(link_ksettings));
  460. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  461. /* send back number of words required as negative val */
  462. compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
  463. "need too many bits for link modes!");
  464. link_ksettings.base.link_mode_masks_nwords
  465. = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
  466. /* copy the base fields back to user, not the link
  467. * mode bitmaps
  468. */
  469. if (copy_to_user(useraddr, &link_ksettings.base,
  470. sizeof(link_ksettings.base)))
  471. return -EFAULT;
  472. return 0;
  473. }
  474. /* handshake successful: user/kernel agree on
  475. * link_mode_masks_nwords
  476. */
  477. memset(&link_ksettings, 0, sizeof(link_ksettings));
  478. err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
  479. if (err < 0)
  480. return err;
  481. /* make sure we tell the right values to user */
  482. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  483. link_ksettings.base.link_mode_masks_nwords
  484. = __ETHTOOL_LINK_MODE_MASK_NU32;
  485. link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
  486. link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
  487. link_ksettings.base.rate_matching = RATE_MATCH_NONE;
  488. return store_link_ksettings_for_user(useraddr, &link_ksettings);
  489. }
  490. /* Update device ethtool_link_settings. */
  491. static int ethtool_set_link_ksettings(struct net_device *dev,
  492. void __user *useraddr)
  493. {
  494. struct ethtool_link_ksettings link_ksettings = {};
  495. int err;
  496. ASSERT_RTNL();
  497. if (!dev->ethtool_ops->set_link_ksettings)
  498. return -EOPNOTSUPP;
  499. /* make sure nbits field has expected value */
  500. if (copy_from_user(&link_ksettings.base, useraddr,
  501. sizeof(link_ksettings.base)))
  502. return -EFAULT;
  503. if (__ETHTOOL_LINK_MODE_MASK_NU32
  504. != link_ksettings.base.link_mode_masks_nwords)
  505. return -EINVAL;
  506. /* copy the whole structure, now that we know it has expected
  507. * format
  508. */
  509. err = load_link_ksettings_from_user(&link_ksettings, useraddr);
  510. if (err)
  511. return err;
  512. /* re-check nwords field, just in case */
  513. if (__ETHTOOL_LINK_MODE_MASK_NU32
  514. != link_ksettings.base.link_mode_masks_nwords)
  515. return -EINVAL;
  516. if (link_ksettings.base.master_slave_cfg ||
  517. link_ksettings.base.master_slave_state)
  518. return -EINVAL;
  519. err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
  520. if (err >= 0) {
  521. ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
  522. ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
  523. }
  524. return err;
  525. }
  526. int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
  527. const struct ethtool_link_ksettings *cmd,
  528. u32 *dev_speed, u8 *dev_duplex)
  529. {
  530. u32 speed;
  531. u8 duplex;
  532. speed = cmd->base.speed;
  533. duplex = cmd->base.duplex;
  534. /* don't allow custom speed and duplex */
  535. if (!ethtool_validate_speed(speed) ||
  536. !ethtool_validate_duplex(duplex) ||
  537. !ethtool_virtdev_validate_cmd(cmd))
  538. return -EINVAL;
  539. *dev_speed = speed;
  540. *dev_duplex = duplex;
  541. return 0;
  542. }
  543. EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
  544. /* Query device for its ethtool_cmd settings.
  545. *
  546. * Backward compatibility note: for compatibility with legacy ethtool, this is
  547. * now implemented via get_link_ksettings. When driver reports higher link mode
  548. * bits, a kernel warning is logged once (with name of 1st driver/device) to
  549. * recommend user to upgrade ethtool, but the command is successful (only the
  550. * lower link mode bits reported back to user). Deprecated fields from
  551. * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
  552. */
  553. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  554. {
  555. struct ethtool_link_ksettings link_ksettings;
  556. struct ethtool_cmd cmd;
  557. int err;
  558. ASSERT_RTNL();
  559. if (!dev->ethtool_ops->get_link_ksettings)
  560. return -EOPNOTSUPP;
  561. memset(&link_ksettings, 0, sizeof(link_ksettings));
  562. err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
  563. if (err < 0)
  564. return err;
  565. convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
  566. /* send a sensible cmd tag back to user */
  567. cmd.cmd = ETHTOOL_GSET;
  568. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  569. return -EFAULT;
  570. return 0;
  571. }
  572. /* Update device link settings with given ethtool_cmd.
  573. *
  574. * Backward compatibility note: for compatibility with legacy ethtool, this is
  575. * now always implemented via set_link_settings. When user's request updates
  576. * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
  577. * warning is logged once (with name of 1st driver/device) to recommend user to
  578. * upgrade ethtool, and the request is rejected.
  579. */
  580. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  581. {
  582. struct ethtool_link_ksettings link_ksettings;
  583. struct ethtool_cmd cmd;
  584. int ret;
  585. ASSERT_RTNL();
  586. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  587. return -EFAULT;
  588. if (!dev->ethtool_ops->set_link_ksettings)
  589. return -EOPNOTSUPP;
  590. if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
  591. return -EINVAL;
  592. link_ksettings.base.link_mode_masks_nwords =
  593. __ETHTOOL_LINK_MODE_MASK_NU32;
  594. ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
  595. if (ret >= 0) {
  596. ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
  597. ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
  598. }
  599. return ret;
  600. }
  601. static int
  602. ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
  603. {
  604. const struct ethtool_ops *ops = dev->ethtool_ops;
  605. rsp->info.cmd = ETHTOOL_GDRVINFO;
  606. strscpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
  607. if (ops->get_drvinfo) {
  608. ops->get_drvinfo(dev, &rsp->info);
  609. } else if (dev->dev.parent && dev->dev.parent->driver) {
  610. strscpy(rsp->info.bus_info, dev_name(dev->dev.parent),
  611. sizeof(rsp->info.bus_info));
  612. strscpy(rsp->info.driver, dev->dev.parent->driver->name,
  613. sizeof(rsp->info.driver));
  614. } else if (dev->rtnl_link_ops) {
  615. strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
  616. sizeof(rsp->info.driver));
  617. } else {
  618. return -EOPNOTSUPP;
  619. }
  620. /*
  621. * this method of obtaining string set info is deprecated;
  622. * Use ETHTOOL_GSSET_INFO instead.
  623. */
  624. if (ops->get_sset_count) {
  625. int rc;
  626. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  627. if (rc >= 0)
  628. rsp->info.testinfo_len = rc;
  629. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  630. if (rc >= 0)
  631. rsp->info.n_stats = rc;
  632. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  633. if (rc >= 0)
  634. rsp->info.n_priv_flags = rc;
  635. }
  636. if (ops->get_regs_len) {
  637. int ret = ops->get_regs_len(dev);
  638. if (ret > 0)
  639. rsp->info.regdump_len = ret;
  640. }
  641. if (ops->get_eeprom_len)
  642. rsp->info.eedump_len = ops->get_eeprom_len(dev);
  643. if (!rsp->info.fw_version[0])
  644. rsp->devlink = netdev_to_devlink_get(dev);
  645. return 0;
  646. }
  647. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  648. void __user *useraddr)
  649. {
  650. struct ethtool_sset_info info;
  651. u64 sset_mask;
  652. int i, idx = 0, n_bits = 0, ret, rc;
  653. u32 *info_buf = NULL;
  654. if (copy_from_user(&info, useraddr, sizeof(info)))
  655. return -EFAULT;
  656. /* store copy of mask, because we zero struct later on */
  657. sset_mask = info.sset_mask;
  658. if (!sset_mask)
  659. return 0;
  660. /* calculate size of return buffer */
  661. n_bits = hweight64(sset_mask);
  662. memset(&info, 0, sizeof(info));
  663. info.cmd = ETHTOOL_GSSET_INFO;
  664. info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
  665. if (!info_buf)
  666. return -ENOMEM;
  667. /*
  668. * fill return buffer based on input bitmask and successful
  669. * get_sset_count return
  670. */
  671. for (i = 0; i < 64; i++) {
  672. if (!(sset_mask & (1ULL << i)))
  673. continue;
  674. rc = __ethtool_get_sset_count(dev, i);
  675. if (rc >= 0) {
  676. info.sset_mask |= (1ULL << i);
  677. info_buf[idx++] = rc;
  678. }
  679. }
  680. ret = -EFAULT;
  681. if (copy_to_user(useraddr, &info, sizeof(info)))
  682. goto out;
  683. useraddr += offsetof(struct ethtool_sset_info, data);
  684. if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
  685. goto out;
  686. ret = 0;
  687. out:
  688. kfree(info_buf);
  689. return ret;
  690. }
  691. static noinline_for_stack int
  692. ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
  693. const struct compat_ethtool_rxnfc __user *useraddr,
  694. size_t size)
  695. {
  696. struct compat_ethtool_rxnfc crxnfc = {};
  697. /* We expect there to be holes between fs.m_ext and
  698. * fs.ring_cookie and at the end of fs, but nowhere else.
  699. * On non-x86, no conversion should be needed.
  700. */
  701. BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
  702. sizeof(struct compat_ethtool_rxnfc) !=
  703. sizeof(struct ethtool_rxnfc));
  704. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  705. sizeof(useraddr->fs.m_ext) !=
  706. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  707. sizeof(rxnfc->fs.m_ext));
  708. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
  709. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  710. offsetof(struct ethtool_rxnfc, fs.location) -
  711. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  712. if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
  713. return -EFAULT;
  714. *rxnfc = (struct ethtool_rxnfc) {
  715. .cmd = crxnfc.cmd,
  716. .flow_type = crxnfc.flow_type,
  717. .data = crxnfc.data,
  718. .fs = {
  719. .flow_type = crxnfc.fs.flow_type,
  720. .h_u = crxnfc.fs.h_u,
  721. .h_ext = crxnfc.fs.h_ext,
  722. .m_u = crxnfc.fs.m_u,
  723. .m_ext = crxnfc.fs.m_ext,
  724. .ring_cookie = crxnfc.fs.ring_cookie,
  725. .location = crxnfc.fs.location,
  726. },
  727. .rule_cnt = crxnfc.rule_cnt,
  728. };
  729. return 0;
  730. }
  731. static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
  732. const void __user *useraddr,
  733. size_t size)
  734. {
  735. if (compat_need_64bit_alignment_fixup())
  736. return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
  737. if (copy_from_user(rxnfc, useraddr, size))
  738. return -EFAULT;
  739. return 0;
  740. }
  741. static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
  742. const struct ethtool_rxnfc *rxnfc,
  743. size_t size, const u32 *rule_buf)
  744. {
  745. struct compat_ethtool_rxnfc crxnfc;
  746. memset(&crxnfc, 0, sizeof(crxnfc));
  747. crxnfc = (struct compat_ethtool_rxnfc) {
  748. .cmd = rxnfc->cmd,
  749. .flow_type = rxnfc->flow_type,
  750. .data = rxnfc->data,
  751. .fs = {
  752. .flow_type = rxnfc->fs.flow_type,
  753. .h_u = rxnfc->fs.h_u,
  754. .h_ext = rxnfc->fs.h_ext,
  755. .m_u = rxnfc->fs.m_u,
  756. .m_ext = rxnfc->fs.m_ext,
  757. .ring_cookie = rxnfc->fs.ring_cookie,
  758. .location = rxnfc->fs.location,
  759. },
  760. .rule_cnt = rxnfc->rule_cnt,
  761. };
  762. if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
  763. return -EFAULT;
  764. return 0;
  765. }
  766. static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
  767. const struct ethtool_rxnfc *rxnfc,
  768. size_t size, const u32 *rule_buf)
  769. {
  770. int ret;
  771. if (compat_need_64bit_alignment_fixup()) {
  772. ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
  773. rule_buf);
  774. useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
  775. } else {
  776. ret = copy_to_user(useraddr, rxnfc, size);
  777. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  778. }
  779. if (ret)
  780. return -EFAULT;
  781. if (rule_buf) {
  782. if (copy_to_user(useraddr, rule_buf,
  783. rxnfc->rule_cnt * sizeof(u32)))
  784. return -EFAULT;
  785. }
  786. return 0;
  787. }
  788. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  789. u32 cmd, void __user *useraddr)
  790. {
  791. struct ethtool_rxnfc info;
  792. size_t info_size = sizeof(info);
  793. int rc;
  794. if (!dev->ethtool_ops->set_rxnfc)
  795. return -EOPNOTSUPP;
  796. /* struct ethtool_rxnfc was originally defined for
  797. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  798. * members. User-space might still be using that
  799. * definition. */
  800. if (cmd == ETHTOOL_SRXFH)
  801. info_size = (offsetof(struct ethtool_rxnfc, data) +
  802. sizeof(info.data));
  803. if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
  804. return -EFAULT;
  805. rc = dev->ethtool_ops->set_rxnfc(dev, &info);
  806. if (rc)
  807. return rc;
  808. if (cmd == ETHTOOL_SRXCLSRLINS &&
  809. ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
  810. return -EFAULT;
  811. return 0;
  812. }
  813. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  814. u32 cmd, void __user *useraddr)
  815. {
  816. struct ethtool_rxnfc info;
  817. size_t info_size = sizeof(info);
  818. const struct ethtool_ops *ops = dev->ethtool_ops;
  819. int ret;
  820. void *rule_buf = NULL;
  821. if (!ops->get_rxnfc)
  822. return -EOPNOTSUPP;
  823. /* struct ethtool_rxnfc was originally defined for
  824. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  825. * members. User-space might still be using that
  826. * definition. */
  827. if (cmd == ETHTOOL_GRXFH)
  828. info_size = (offsetof(struct ethtool_rxnfc, data) +
  829. sizeof(info.data));
  830. if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
  831. return -EFAULT;
  832. /* If FLOW_RSS was requested then user-space must be using the
  833. * new definition, as FLOW_RSS is newer.
  834. */
  835. if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
  836. info_size = sizeof(info);
  837. if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
  838. return -EFAULT;
  839. /* Since malicious users may modify the original data,
  840. * we need to check whether FLOW_RSS is still requested.
  841. */
  842. if (!(info.flow_type & FLOW_RSS))
  843. return -EINVAL;
  844. }
  845. if (info.cmd != cmd)
  846. return -EINVAL;
  847. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  848. if (info.rule_cnt > 0) {
  849. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  850. rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
  851. GFP_USER);
  852. if (!rule_buf)
  853. return -ENOMEM;
  854. }
  855. }
  856. ret = ops->get_rxnfc(dev, &info, rule_buf);
  857. if (ret < 0)
  858. goto err_out;
  859. ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
  860. err_out:
  861. kfree(rule_buf);
  862. return ret;
  863. }
  864. static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
  865. struct ethtool_rxnfc *rx_rings,
  866. u32 size)
  867. {
  868. int i;
  869. if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
  870. return -EFAULT;
  871. /* Validate ring indices */
  872. for (i = 0; i < size; i++)
  873. if (indir[i] >= rx_rings->data)
  874. return -EINVAL;
  875. return 0;
  876. }
  877. u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
  878. void netdev_rss_key_fill(void *buffer, size_t len)
  879. {
  880. BUG_ON(len > sizeof(netdev_rss_key));
  881. net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
  882. memcpy(buffer, netdev_rss_key, len);
  883. }
  884. EXPORT_SYMBOL(netdev_rss_key_fill);
  885. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  886. void __user *useraddr)
  887. {
  888. u32 user_size, dev_size;
  889. u32 *indir;
  890. int ret;
  891. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  892. !dev->ethtool_ops->get_rxfh)
  893. return -EOPNOTSUPP;
  894. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  895. if (dev_size == 0)
  896. return -EOPNOTSUPP;
  897. if (copy_from_user(&user_size,
  898. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  899. sizeof(user_size)))
  900. return -EFAULT;
  901. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  902. &dev_size, sizeof(dev_size)))
  903. return -EFAULT;
  904. /* If the user buffer size is 0, this is just a query for the
  905. * device table size. Otherwise, if it's smaller than the
  906. * device table size it's an error.
  907. */
  908. if (user_size < dev_size)
  909. return user_size == 0 ? 0 : -EINVAL;
  910. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  911. if (!indir)
  912. return -ENOMEM;
  913. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  914. if (ret)
  915. goto out;
  916. if (copy_to_user(useraddr +
  917. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  918. indir, dev_size * sizeof(indir[0])))
  919. ret = -EFAULT;
  920. out:
  921. kfree(indir);
  922. return ret;
  923. }
  924. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  925. void __user *useraddr)
  926. {
  927. struct ethtool_rxnfc rx_rings;
  928. u32 user_size, dev_size, i;
  929. u32 *indir;
  930. const struct ethtool_ops *ops = dev->ethtool_ops;
  931. int ret;
  932. u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
  933. if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
  934. !ops->get_rxnfc)
  935. return -EOPNOTSUPP;
  936. dev_size = ops->get_rxfh_indir_size(dev);
  937. if (dev_size == 0)
  938. return -EOPNOTSUPP;
  939. if (copy_from_user(&user_size,
  940. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  941. sizeof(user_size)))
  942. return -EFAULT;
  943. if (user_size != 0 && user_size != dev_size)
  944. return -EINVAL;
  945. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  946. if (!indir)
  947. return -ENOMEM;
  948. rx_rings.cmd = ETHTOOL_GRXRINGS;
  949. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  950. if (ret)
  951. goto out;
  952. if (user_size == 0) {
  953. for (i = 0; i < dev_size; i++)
  954. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  955. } else {
  956. ret = ethtool_copy_validate_indir(indir,
  957. useraddr + ringidx_offset,
  958. &rx_rings,
  959. dev_size);
  960. if (ret)
  961. goto out;
  962. }
  963. ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
  964. if (ret)
  965. goto out;
  966. /* indicate whether rxfh was set to default */
  967. if (user_size == 0)
  968. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  969. else
  970. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  971. out:
  972. kfree(indir);
  973. return ret;
  974. }
  975. static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
  976. void __user *useraddr)
  977. {
  978. int ret;
  979. const struct ethtool_ops *ops = dev->ethtool_ops;
  980. u32 user_indir_size, user_key_size;
  981. u32 dev_indir_size = 0, dev_key_size = 0;
  982. struct ethtool_rxfh rxfh;
  983. u32 total_size;
  984. u32 indir_bytes;
  985. u32 *indir = NULL;
  986. u8 dev_hfunc = 0;
  987. u8 *hkey = NULL;
  988. u8 *rss_config;
  989. if (!ops->get_rxfh)
  990. return -EOPNOTSUPP;
  991. if (ops->get_rxfh_indir_size)
  992. dev_indir_size = ops->get_rxfh_indir_size(dev);
  993. if (ops->get_rxfh_key_size)
  994. dev_key_size = ops->get_rxfh_key_size(dev);
  995. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  996. return -EFAULT;
  997. user_indir_size = rxfh.indir_size;
  998. user_key_size = rxfh.key_size;
  999. /* Check that reserved fields are 0 for now */
  1000. if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
  1001. return -EINVAL;
  1002. /* Most drivers don't handle rss_context, check it's 0 as well */
  1003. if (rxfh.rss_context && !ops->get_rxfh_context)
  1004. return -EOPNOTSUPP;
  1005. rxfh.indir_size = dev_indir_size;
  1006. rxfh.key_size = dev_key_size;
  1007. if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
  1008. return -EFAULT;
  1009. if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
  1010. (user_key_size && (user_key_size != dev_key_size)))
  1011. return -EINVAL;
  1012. indir_bytes = user_indir_size * sizeof(indir[0]);
  1013. total_size = indir_bytes + user_key_size;
  1014. rss_config = kzalloc(total_size, GFP_USER);
  1015. if (!rss_config)
  1016. return -ENOMEM;
  1017. if (user_indir_size)
  1018. indir = (u32 *)rss_config;
  1019. if (user_key_size)
  1020. hkey = rss_config + indir_bytes;
  1021. if (rxfh.rss_context)
  1022. ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
  1023. &dev_hfunc,
  1024. rxfh.rss_context);
  1025. else
  1026. ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
  1027. if (ret)
  1028. goto out;
  1029. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
  1030. &dev_hfunc, sizeof(rxfh.hfunc))) {
  1031. ret = -EFAULT;
  1032. } else if (copy_to_user(useraddr +
  1033. offsetof(struct ethtool_rxfh, rss_config[0]),
  1034. rss_config, total_size)) {
  1035. ret = -EFAULT;
  1036. }
  1037. out:
  1038. kfree(rss_config);
  1039. return ret;
  1040. }
  1041. static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
  1042. void __user *useraddr)
  1043. {
  1044. int ret;
  1045. const struct ethtool_ops *ops = dev->ethtool_ops;
  1046. struct ethtool_rxnfc rx_rings;
  1047. struct ethtool_rxfh rxfh;
  1048. u32 dev_indir_size = 0, dev_key_size = 0, i;
  1049. u32 *indir = NULL, indir_bytes = 0;
  1050. u8 *hkey = NULL;
  1051. u8 *rss_config;
  1052. u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
  1053. bool delete = false;
  1054. if (!ops->get_rxnfc || !ops->set_rxfh)
  1055. return -EOPNOTSUPP;
  1056. if (ops->get_rxfh_indir_size)
  1057. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1058. if (ops->get_rxfh_key_size)
  1059. dev_key_size = ops->get_rxfh_key_size(dev);
  1060. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1061. return -EFAULT;
  1062. /* Check that reserved fields are 0 for now */
  1063. if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
  1064. return -EINVAL;
  1065. /* Most drivers don't handle rss_context, check it's 0 as well */
  1066. if (rxfh.rss_context && !ops->set_rxfh_context)
  1067. return -EOPNOTSUPP;
  1068. /* If either indir, hash key or function is valid, proceed further.
  1069. * Must request at least one change: indir size, hash key or function.
  1070. */
  1071. if ((rxfh.indir_size &&
  1072. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
  1073. rxfh.indir_size != dev_indir_size) ||
  1074. (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
  1075. (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
  1076. rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
  1077. return -EINVAL;
  1078. if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1079. indir_bytes = dev_indir_size * sizeof(indir[0]);
  1080. rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
  1081. if (!rss_config)
  1082. return -ENOMEM;
  1083. rx_rings.cmd = ETHTOOL_GRXRINGS;
  1084. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  1085. if (ret)
  1086. goto out;
  1087. /* rxfh.indir_size == 0 means reset the indir table to default (master
  1088. * context) or delete the context (other RSS contexts).
  1089. * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
  1090. */
  1091. if (rxfh.indir_size &&
  1092. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
  1093. indir = (u32 *)rss_config;
  1094. ret = ethtool_copy_validate_indir(indir,
  1095. useraddr + rss_cfg_offset,
  1096. &rx_rings,
  1097. rxfh.indir_size);
  1098. if (ret)
  1099. goto out;
  1100. } else if (rxfh.indir_size == 0) {
  1101. if (rxfh.rss_context == 0) {
  1102. indir = (u32 *)rss_config;
  1103. for (i = 0; i < dev_indir_size; i++)
  1104. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  1105. } else {
  1106. delete = true;
  1107. }
  1108. }
  1109. if (rxfh.key_size) {
  1110. hkey = rss_config + indir_bytes;
  1111. if (copy_from_user(hkey,
  1112. useraddr + rss_cfg_offset + indir_bytes,
  1113. rxfh.key_size)) {
  1114. ret = -EFAULT;
  1115. goto out;
  1116. }
  1117. }
  1118. if (rxfh.rss_context)
  1119. ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
  1120. &rxfh.rss_context, delete);
  1121. else
  1122. ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
  1123. if (ret)
  1124. goto out;
  1125. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
  1126. &rxfh.rss_context, sizeof(rxfh.rss_context)))
  1127. ret = -EFAULT;
  1128. if (!rxfh.rss_context) {
  1129. /* indicate whether rxfh was set to default */
  1130. if (rxfh.indir_size == 0)
  1131. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1132. else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1133. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1134. }
  1135. out:
  1136. kfree(rss_config);
  1137. return ret;
  1138. }
  1139. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1140. {
  1141. struct ethtool_regs regs;
  1142. const struct ethtool_ops *ops = dev->ethtool_ops;
  1143. void *regbuf;
  1144. int reglen, ret;
  1145. if (!ops->get_regs || !ops->get_regs_len)
  1146. return -EOPNOTSUPP;
  1147. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1148. return -EFAULT;
  1149. reglen = ops->get_regs_len(dev);
  1150. if (reglen <= 0)
  1151. return reglen;
  1152. if (regs.len > reglen)
  1153. regs.len = reglen;
  1154. regbuf = vzalloc(reglen);
  1155. if (!regbuf)
  1156. return -ENOMEM;
  1157. if (regs.len < reglen)
  1158. reglen = regs.len;
  1159. ops->get_regs(dev, &regs, regbuf);
  1160. ret = -EFAULT;
  1161. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1162. goto out;
  1163. useraddr += offsetof(struct ethtool_regs, data);
  1164. if (copy_to_user(useraddr, regbuf, reglen))
  1165. goto out;
  1166. ret = 0;
  1167. out:
  1168. vfree(regbuf);
  1169. return ret;
  1170. }
  1171. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1172. {
  1173. struct ethtool_value reset;
  1174. int ret;
  1175. if (!dev->ethtool_ops->reset)
  1176. return -EOPNOTSUPP;
  1177. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1178. return -EFAULT;
  1179. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1180. if (ret)
  1181. return ret;
  1182. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1183. return -EFAULT;
  1184. return 0;
  1185. }
  1186. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1187. {
  1188. struct ethtool_wolinfo wol;
  1189. if (!dev->ethtool_ops->get_wol)
  1190. return -EOPNOTSUPP;
  1191. memset(&wol, 0, sizeof(struct ethtool_wolinfo));
  1192. wol.cmd = ETHTOOL_GWOL;
  1193. dev->ethtool_ops->get_wol(dev, &wol);
  1194. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1195. return -EFAULT;
  1196. return 0;
  1197. }
  1198. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1199. {
  1200. struct ethtool_wolinfo wol;
  1201. int ret;
  1202. if (!dev->ethtool_ops->set_wol)
  1203. return -EOPNOTSUPP;
  1204. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1205. return -EFAULT;
  1206. ret = dev->ethtool_ops->set_wol(dev, &wol);
  1207. if (ret)
  1208. return ret;
  1209. dev->wol_enabled = !!wol.wolopts;
  1210. ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
  1211. return 0;
  1212. }
  1213. static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
  1214. {
  1215. struct ethtool_eee edata;
  1216. int rc;
  1217. if (!dev->ethtool_ops->get_eee)
  1218. return -EOPNOTSUPP;
  1219. memset(&edata, 0, sizeof(struct ethtool_eee));
  1220. edata.cmd = ETHTOOL_GEEE;
  1221. rc = dev->ethtool_ops->get_eee(dev, &edata);
  1222. if (rc)
  1223. return rc;
  1224. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1225. return -EFAULT;
  1226. return 0;
  1227. }
  1228. static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
  1229. {
  1230. struct ethtool_eee edata;
  1231. int ret;
  1232. if (!dev->ethtool_ops->set_eee)
  1233. return -EOPNOTSUPP;
  1234. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1235. return -EFAULT;
  1236. ret = dev->ethtool_ops->set_eee(dev, &edata);
  1237. if (!ret)
  1238. ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
  1239. return ret;
  1240. }
  1241. static int ethtool_nway_reset(struct net_device *dev)
  1242. {
  1243. if (!dev->ethtool_ops->nway_reset)
  1244. return -EOPNOTSUPP;
  1245. return dev->ethtool_ops->nway_reset(dev);
  1246. }
  1247. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1248. {
  1249. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1250. int link = __ethtool_get_link(dev);
  1251. if (link < 0)
  1252. return link;
  1253. edata.data = link;
  1254. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1255. return -EFAULT;
  1256. return 0;
  1257. }
  1258. static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
  1259. int (*getter)(struct net_device *,
  1260. struct ethtool_eeprom *, u8 *),
  1261. u32 total_len)
  1262. {
  1263. struct ethtool_eeprom eeprom;
  1264. void __user *userbuf = useraddr + sizeof(eeprom);
  1265. u32 bytes_remaining;
  1266. u8 *data;
  1267. int ret = 0;
  1268. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1269. return -EFAULT;
  1270. /* Check for wrap and zero */
  1271. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1272. return -EINVAL;
  1273. /* Check for exceeding total eeprom len */
  1274. if (eeprom.offset + eeprom.len > total_len)
  1275. return -EINVAL;
  1276. data = kzalloc(PAGE_SIZE, GFP_USER);
  1277. if (!data)
  1278. return -ENOMEM;
  1279. bytes_remaining = eeprom.len;
  1280. while (bytes_remaining > 0) {
  1281. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1282. ret = getter(dev, &eeprom, data);
  1283. if (ret)
  1284. break;
  1285. if (!eeprom.len) {
  1286. ret = -EIO;
  1287. break;
  1288. }
  1289. if (copy_to_user(userbuf, data, eeprom.len)) {
  1290. ret = -EFAULT;
  1291. break;
  1292. }
  1293. userbuf += eeprom.len;
  1294. eeprom.offset += eeprom.len;
  1295. bytes_remaining -= eeprom.len;
  1296. }
  1297. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1298. eeprom.offset -= eeprom.len;
  1299. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1300. ret = -EFAULT;
  1301. kfree(data);
  1302. return ret;
  1303. }
  1304. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1305. {
  1306. const struct ethtool_ops *ops = dev->ethtool_ops;
  1307. if (!ops->get_eeprom || !ops->get_eeprom_len ||
  1308. !ops->get_eeprom_len(dev))
  1309. return -EOPNOTSUPP;
  1310. return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
  1311. ops->get_eeprom_len(dev));
  1312. }
  1313. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1314. {
  1315. struct ethtool_eeprom eeprom;
  1316. const struct ethtool_ops *ops = dev->ethtool_ops;
  1317. void __user *userbuf = useraddr + sizeof(eeprom);
  1318. u32 bytes_remaining;
  1319. u8 *data;
  1320. int ret = 0;
  1321. if (!ops->set_eeprom || !ops->get_eeprom_len ||
  1322. !ops->get_eeprom_len(dev))
  1323. return -EOPNOTSUPP;
  1324. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1325. return -EFAULT;
  1326. /* Check for wrap and zero */
  1327. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1328. return -EINVAL;
  1329. /* Check for exceeding total eeprom len */
  1330. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1331. return -EINVAL;
  1332. data = kzalloc(PAGE_SIZE, GFP_USER);
  1333. if (!data)
  1334. return -ENOMEM;
  1335. bytes_remaining = eeprom.len;
  1336. while (bytes_remaining > 0) {
  1337. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1338. if (copy_from_user(data, userbuf, eeprom.len)) {
  1339. ret = -EFAULT;
  1340. break;
  1341. }
  1342. ret = ops->set_eeprom(dev, &eeprom, data);
  1343. if (ret)
  1344. break;
  1345. userbuf += eeprom.len;
  1346. eeprom.offset += eeprom.len;
  1347. bytes_remaining -= eeprom.len;
  1348. }
  1349. kfree(data);
  1350. return ret;
  1351. }
  1352. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1353. void __user *useraddr)
  1354. {
  1355. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1356. struct kernel_ethtool_coalesce kernel_coalesce = {};
  1357. int ret;
  1358. if (!dev->ethtool_ops->get_coalesce)
  1359. return -EOPNOTSUPP;
  1360. ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
  1361. NULL);
  1362. if (ret)
  1363. return ret;
  1364. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1365. return -EFAULT;
  1366. return 0;
  1367. }
  1368. static bool
  1369. ethtool_set_coalesce_supported(struct net_device *dev,
  1370. struct ethtool_coalesce *coalesce)
  1371. {
  1372. u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
  1373. u32 nonzero_params = 0;
  1374. if (coalesce->rx_coalesce_usecs)
  1375. nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
  1376. if (coalesce->rx_max_coalesced_frames)
  1377. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
  1378. if (coalesce->rx_coalesce_usecs_irq)
  1379. nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
  1380. if (coalesce->rx_max_coalesced_frames_irq)
  1381. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
  1382. if (coalesce->tx_coalesce_usecs)
  1383. nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
  1384. if (coalesce->tx_max_coalesced_frames)
  1385. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
  1386. if (coalesce->tx_coalesce_usecs_irq)
  1387. nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
  1388. if (coalesce->tx_max_coalesced_frames_irq)
  1389. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
  1390. if (coalesce->stats_block_coalesce_usecs)
  1391. nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
  1392. if (coalesce->use_adaptive_rx_coalesce)
  1393. nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
  1394. if (coalesce->use_adaptive_tx_coalesce)
  1395. nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
  1396. if (coalesce->pkt_rate_low)
  1397. nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
  1398. if (coalesce->rx_coalesce_usecs_low)
  1399. nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
  1400. if (coalesce->rx_max_coalesced_frames_low)
  1401. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
  1402. if (coalesce->tx_coalesce_usecs_low)
  1403. nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
  1404. if (coalesce->tx_max_coalesced_frames_low)
  1405. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
  1406. if (coalesce->pkt_rate_high)
  1407. nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
  1408. if (coalesce->rx_coalesce_usecs_high)
  1409. nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
  1410. if (coalesce->rx_max_coalesced_frames_high)
  1411. nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
  1412. if (coalesce->tx_coalesce_usecs_high)
  1413. nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
  1414. if (coalesce->tx_max_coalesced_frames_high)
  1415. nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
  1416. if (coalesce->rate_sample_interval)
  1417. nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
  1418. return (supported_params & nonzero_params) == nonzero_params;
  1419. }
  1420. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1421. void __user *useraddr)
  1422. {
  1423. struct kernel_ethtool_coalesce kernel_coalesce = {};
  1424. struct ethtool_coalesce coalesce;
  1425. int ret;
  1426. if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
  1427. return -EOPNOTSUPP;
  1428. ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
  1429. NULL);
  1430. if (ret)
  1431. return ret;
  1432. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1433. return -EFAULT;
  1434. if (!ethtool_set_coalesce_supported(dev, &coalesce))
  1435. return -EOPNOTSUPP;
  1436. ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
  1437. NULL);
  1438. if (!ret)
  1439. ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
  1440. return ret;
  1441. }
  1442. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1443. {
  1444. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1445. struct kernel_ethtool_ringparam kernel_ringparam = {};
  1446. if (!dev->ethtool_ops->get_ringparam)
  1447. return -EOPNOTSUPP;
  1448. dev->ethtool_ops->get_ringparam(dev, &ringparam,
  1449. &kernel_ringparam, NULL);
  1450. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1451. return -EFAULT;
  1452. return 0;
  1453. }
  1454. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1455. {
  1456. struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
  1457. struct kernel_ethtool_ringparam kernel_ringparam;
  1458. int ret;
  1459. if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
  1460. return -EOPNOTSUPP;
  1461. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1462. return -EFAULT;
  1463. dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
  1464. /* ensure new ring parameters are within the maximums */
  1465. if (ringparam.rx_pending > max.rx_max_pending ||
  1466. ringparam.rx_mini_pending > max.rx_mini_max_pending ||
  1467. ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
  1468. ringparam.tx_pending > max.tx_max_pending)
  1469. return -EINVAL;
  1470. ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
  1471. &kernel_ringparam, NULL);
  1472. if (!ret)
  1473. ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
  1474. return ret;
  1475. }
  1476. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  1477. void __user *useraddr)
  1478. {
  1479. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  1480. if (!dev->ethtool_ops->get_channels)
  1481. return -EOPNOTSUPP;
  1482. dev->ethtool_ops->get_channels(dev, &channels);
  1483. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  1484. return -EFAULT;
  1485. return 0;
  1486. }
  1487. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  1488. void __user *useraddr)
  1489. {
  1490. struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
  1491. u16 from_channel, to_channel;
  1492. u32 max_rx_in_use = 0;
  1493. unsigned int i;
  1494. int ret;
  1495. if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
  1496. return -EOPNOTSUPP;
  1497. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  1498. return -EFAULT;
  1499. dev->ethtool_ops->get_channels(dev, &curr);
  1500. if (channels.rx_count == curr.rx_count &&
  1501. channels.tx_count == curr.tx_count &&
  1502. channels.combined_count == curr.combined_count &&
  1503. channels.other_count == curr.other_count)
  1504. return 0;
  1505. /* ensure new counts are within the maximums */
  1506. if (channels.rx_count > curr.max_rx ||
  1507. channels.tx_count > curr.max_tx ||
  1508. channels.combined_count > curr.max_combined ||
  1509. channels.other_count > curr.max_other)
  1510. return -EINVAL;
  1511. /* ensure there is at least one RX and one TX channel */
  1512. if (!channels.combined_count &&
  1513. (!channels.rx_count || !channels.tx_count))
  1514. return -EINVAL;
  1515. /* ensure the new Rx count fits within the configured Rx flow
  1516. * indirection table settings */
  1517. if (netif_is_rxfh_configured(dev) &&
  1518. !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
  1519. (channels.combined_count + channels.rx_count) <= max_rx_in_use)
  1520. return -EINVAL;
  1521. /* Disabling channels, query zero-copy AF_XDP sockets */
  1522. from_channel = channels.combined_count +
  1523. min(channels.rx_count, channels.tx_count);
  1524. to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
  1525. for (i = from_channel; i < to_channel; i++)
  1526. if (xsk_get_pool_from_qid(dev, i))
  1527. return -EINVAL;
  1528. ret = dev->ethtool_ops->set_channels(dev, &channels);
  1529. if (!ret)
  1530. ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
  1531. return ret;
  1532. }
  1533. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1534. {
  1535. struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
  1536. if (!dev->ethtool_ops->get_pauseparam)
  1537. return -EOPNOTSUPP;
  1538. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1539. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1540. return -EFAULT;
  1541. return 0;
  1542. }
  1543. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1544. {
  1545. struct ethtool_pauseparam pauseparam;
  1546. int ret;
  1547. if (!dev->ethtool_ops->set_pauseparam)
  1548. return -EOPNOTSUPP;
  1549. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1550. return -EFAULT;
  1551. ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1552. if (!ret)
  1553. ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
  1554. return ret;
  1555. }
  1556. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1557. {
  1558. struct ethtool_test test;
  1559. const struct ethtool_ops *ops = dev->ethtool_ops;
  1560. u64 *data;
  1561. int ret, test_len;
  1562. if (!ops->self_test || !ops->get_sset_count)
  1563. return -EOPNOTSUPP;
  1564. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1565. if (test_len < 0)
  1566. return test_len;
  1567. WARN_ON(test_len == 0);
  1568. if (copy_from_user(&test, useraddr, sizeof(test)))
  1569. return -EFAULT;
  1570. test.len = test_len;
  1571. data = kcalloc(test_len, sizeof(u64), GFP_USER);
  1572. if (!data)
  1573. return -ENOMEM;
  1574. netif_testing_on(dev);
  1575. ops->self_test(dev, &test, data);
  1576. netif_testing_off(dev);
  1577. ret = -EFAULT;
  1578. if (copy_to_user(useraddr, &test, sizeof(test)))
  1579. goto out;
  1580. useraddr += sizeof(test);
  1581. if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
  1582. goto out;
  1583. ret = 0;
  1584. out:
  1585. kfree(data);
  1586. return ret;
  1587. }
  1588. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1589. {
  1590. struct ethtool_gstrings gstrings;
  1591. u8 *data;
  1592. int ret;
  1593. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1594. return -EFAULT;
  1595. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1596. if (ret < 0)
  1597. return ret;
  1598. if (ret > S32_MAX / ETH_GSTRING_LEN)
  1599. return -ENOMEM;
  1600. WARN_ON_ONCE(!ret);
  1601. gstrings.len = ret;
  1602. if (gstrings.len) {
  1603. data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
  1604. if (!data)
  1605. return -ENOMEM;
  1606. __ethtool_get_strings(dev, gstrings.string_set, data);
  1607. } else {
  1608. data = NULL;
  1609. }
  1610. ret = -EFAULT;
  1611. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1612. goto out;
  1613. useraddr += sizeof(gstrings);
  1614. if (gstrings.len &&
  1615. copy_to_user(useraddr, data,
  1616. array_size(gstrings.len, ETH_GSTRING_LEN)))
  1617. goto out;
  1618. ret = 0;
  1619. out:
  1620. vfree(data);
  1621. return ret;
  1622. }
  1623. __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
  1624. {
  1625. va_list args;
  1626. va_start(args, fmt);
  1627. vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
  1628. va_end(args);
  1629. *data += ETH_GSTRING_LEN;
  1630. }
  1631. EXPORT_SYMBOL(ethtool_sprintf);
  1632. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1633. {
  1634. struct ethtool_value id;
  1635. static bool busy;
  1636. const struct ethtool_ops *ops = dev->ethtool_ops;
  1637. netdevice_tracker dev_tracker;
  1638. int rc;
  1639. if (!ops->set_phys_id)
  1640. return -EOPNOTSUPP;
  1641. if (busy)
  1642. return -EBUSY;
  1643. if (copy_from_user(&id, useraddr, sizeof(id)))
  1644. return -EFAULT;
  1645. rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  1646. if (rc < 0)
  1647. return rc;
  1648. /* Drop the RTNL lock while waiting, but prevent reentry or
  1649. * removal of the device.
  1650. */
  1651. busy = true;
  1652. netdev_hold(dev, &dev_tracker, GFP_KERNEL);
  1653. rtnl_unlock();
  1654. if (rc == 0) {
  1655. /* Driver will handle this itself */
  1656. schedule_timeout_interruptible(
  1657. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  1658. } else {
  1659. /* Driver expects to be called at twice the frequency in rc */
  1660. int n = rc * 2, interval = HZ / n;
  1661. u64 count = mul_u32_u32(n, id.data);
  1662. u64 i = 0;
  1663. do {
  1664. rtnl_lock();
  1665. rc = ops->set_phys_id(dev,
  1666. (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  1667. rtnl_unlock();
  1668. if (rc)
  1669. break;
  1670. schedule_timeout_interruptible(interval);
  1671. } while (!signal_pending(current) && (!id.data || i < count));
  1672. }
  1673. rtnl_lock();
  1674. netdev_put(dev, &dev_tracker);
  1675. busy = false;
  1676. (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  1677. return rc;
  1678. }
  1679. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1680. {
  1681. struct ethtool_stats stats;
  1682. const struct ethtool_ops *ops = dev->ethtool_ops;
  1683. u64 *data;
  1684. int ret, n_stats;
  1685. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1686. return -EOPNOTSUPP;
  1687. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1688. if (n_stats < 0)
  1689. return n_stats;
  1690. if (n_stats > S32_MAX / sizeof(u64))
  1691. return -ENOMEM;
  1692. WARN_ON_ONCE(!n_stats);
  1693. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1694. return -EFAULT;
  1695. stats.n_stats = n_stats;
  1696. if (n_stats) {
  1697. data = vzalloc(array_size(n_stats, sizeof(u64)));
  1698. if (!data)
  1699. return -ENOMEM;
  1700. ops->get_ethtool_stats(dev, &stats, data);
  1701. } else {
  1702. data = NULL;
  1703. }
  1704. ret = -EFAULT;
  1705. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1706. goto out;
  1707. useraddr += sizeof(stats);
  1708. if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
  1709. goto out;
  1710. ret = 0;
  1711. out:
  1712. vfree(data);
  1713. return ret;
  1714. }
  1715. static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
  1716. {
  1717. const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
  1718. const struct ethtool_ops *ops = dev->ethtool_ops;
  1719. struct phy_device *phydev = dev->phydev;
  1720. struct ethtool_stats stats;
  1721. u64 *data;
  1722. int ret, n_stats;
  1723. if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
  1724. return -EOPNOTSUPP;
  1725. if (phydev && !ops->get_ethtool_phy_stats &&
  1726. phy_ops && phy_ops->get_sset_count)
  1727. n_stats = phy_ops->get_sset_count(phydev);
  1728. else
  1729. n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
  1730. if (n_stats < 0)
  1731. return n_stats;
  1732. if (n_stats > S32_MAX / sizeof(u64))
  1733. return -ENOMEM;
  1734. if (WARN_ON_ONCE(!n_stats))
  1735. return -EOPNOTSUPP;
  1736. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1737. return -EFAULT;
  1738. stats.n_stats = n_stats;
  1739. if (n_stats) {
  1740. data = vzalloc(array_size(n_stats, sizeof(u64)));
  1741. if (!data)
  1742. return -ENOMEM;
  1743. if (phydev && !ops->get_ethtool_phy_stats &&
  1744. phy_ops && phy_ops->get_stats) {
  1745. ret = phy_ops->get_stats(phydev, &stats, data);
  1746. if (ret < 0)
  1747. goto out;
  1748. } else {
  1749. ops->get_ethtool_phy_stats(dev, &stats, data);
  1750. }
  1751. } else {
  1752. data = NULL;
  1753. }
  1754. ret = -EFAULT;
  1755. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1756. goto out;
  1757. useraddr += sizeof(stats);
  1758. if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
  1759. goto out;
  1760. ret = 0;
  1761. out:
  1762. vfree(data);
  1763. return ret;
  1764. }
  1765. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1766. {
  1767. struct ethtool_perm_addr epaddr;
  1768. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1769. return -EFAULT;
  1770. if (epaddr.size < dev->addr_len)
  1771. return -ETOOSMALL;
  1772. epaddr.size = dev->addr_len;
  1773. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1774. return -EFAULT;
  1775. useraddr += sizeof(epaddr);
  1776. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1777. return -EFAULT;
  1778. return 0;
  1779. }
  1780. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1781. u32 cmd, u32 (*actor)(struct net_device *))
  1782. {
  1783. struct ethtool_value edata = { .cmd = cmd };
  1784. if (!actor)
  1785. return -EOPNOTSUPP;
  1786. edata.data = actor(dev);
  1787. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1788. return -EFAULT;
  1789. return 0;
  1790. }
  1791. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1792. void (*actor)(struct net_device *, u32))
  1793. {
  1794. struct ethtool_value edata;
  1795. if (!actor)
  1796. return -EOPNOTSUPP;
  1797. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1798. return -EFAULT;
  1799. actor(dev, edata.data);
  1800. return 0;
  1801. }
  1802. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1803. int (*actor)(struct net_device *, u32))
  1804. {
  1805. struct ethtool_value edata;
  1806. if (!actor)
  1807. return -EOPNOTSUPP;
  1808. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1809. return -EFAULT;
  1810. return actor(dev, edata.data);
  1811. }
  1812. static int
  1813. ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
  1814. {
  1815. if (!dev->ethtool_ops->flash_device) {
  1816. req->devlink = netdev_to_devlink_get(dev);
  1817. return 0;
  1818. }
  1819. return dev->ethtool_ops->flash_device(dev, &req->efl);
  1820. }
  1821. static int ethtool_set_dump(struct net_device *dev,
  1822. void __user *useraddr)
  1823. {
  1824. struct ethtool_dump dump;
  1825. if (!dev->ethtool_ops->set_dump)
  1826. return -EOPNOTSUPP;
  1827. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1828. return -EFAULT;
  1829. return dev->ethtool_ops->set_dump(dev, &dump);
  1830. }
  1831. static int ethtool_get_dump_flag(struct net_device *dev,
  1832. void __user *useraddr)
  1833. {
  1834. int ret;
  1835. struct ethtool_dump dump;
  1836. const struct ethtool_ops *ops = dev->ethtool_ops;
  1837. if (!ops->get_dump_flag)
  1838. return -EOPNOTSUPP;
  1839. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1840. return -EFAULT;
  1841. ret = ops->get_dump_flag(dev, &dump);
  1842. if (ret)
  1843. return ret;
  1844. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  1845. return -EFAULT;
  1846. return 0;
  1847. }
  1848. static int ethtool_get_dump_data(struct net_device *dev,
  1849. void __user *useraddr)
  1850. {
  1851. int ret;
  1852. __u32 len;
  1853. struct ethtool_dump dump, tmp;
  1854. const struct ethtool_ops *ops = dev->ethtool_ops;
  1855. void *data = NULL;
  1856. if (!ops->get_dump_data || !ops->get_dump_flag)
  1857. return -EOPNOTSUPP;
  1858. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1859. return -EFAULT;
  1860. memset(&tmp, 0, sizeof(tmp));
  1861. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  1862. ret = ops->get_dump_flag(dev, &tmp);
  1863. if (ret)
  1864. return ret;
  1865. len = min(tmp.len, dump.len);
  1866. if (!len)
  1867. return -EFAULT;
  1868. /* Don't ever let the driver think there's more space available
  1869. * than it requested with .get_dump_flag().
  1870. */
  1871. dump.len = len;
  1872. /* Always allocate enough space to hold the whole thing so that the
  1873. * driver does not need to check the length and bother with partial
  1874. * dumping.
  1875. */
  1876. data = vzalloc(tmp.len);
  1877. if (!data)
  1878. return -ENOMEM;
  1879. ret = ops->get_dump_data(dev, &dump, data);
  1880. if (ret)
  1881. goto out;
  1882. /* There are two sane possibilities:
  1883. * 1. The driver's .get_dump_data() does not touch dump.len.
  1884. * 2. Or it may set dump.len to how much it really writes, which
  1885. * should be tmp.len (or len if it can do a partial dump).
  1886. * In any case respond to userspace with the actual length of data
  1887. * it's receiving.
  1888. */
  1889. WARN_ON(dump.len != len && dump.len != tmp.len);
  1890. dump.len = len;
  1891. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1892. ret = -EFAULT;
  1893. goto out;
  1894. }
  1895. useraddr += offsetof(struct ethtool_dump, data);
  1896. if (copy_to_user(useraddr, data, len))
  1897. ret = -EFAULT;
  1898. out:
  1899. vfree(data);
  1900. return ret;
  1901. }
  1902. static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
  1903. {
  1904. struct ethtool_ts_info info;
  1905. int err;
  1906. err = __ethtool_get_ts_info(dev, &info);
  1907. if (err)
  1908. return err;
  1909. if (copy_to_user(useraddr, &info, sizeof(info)))
  1910. return -EFAULT;
  1911. return 0;
  1912. }
  1913. int ethtool_get_module_info_call(struct net_device *dev,
  1914. struct ethtool_modinfo *modinfo)
  1915. {
  1916. const struct ethtool_ops *ops = dev->ethtool_ops;
  1917. struct phy_device *phydev = dev->phydev;
  1918. if (dev->sfp_bus)
  1919. return sfp_get_module_info(dev->sfp_bus, modinfo);
  1920. if (phydev && phydev->drv && phydev->drv->module_info)
  1921. return phydev->drv->module_info(phydev, modinfo);
  1922. if (ops->get_module_info)
  1923. return ops->get_module_info(dev, modinfo);
  1924. return -EOPNOTSUPP;
  1925. }
  1926. static int ethtool_get_module_info(struct net_device *dev,
  1927. void __user *useraddr)
  1928. {
  1929. int ret;
  1930. struct ethtool_modinfo modinfo;
  1931. if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
  1932. return -EFAULT;
  1933. ret = ethtool_get_module_info_call(dev, &modinfo);
  1934. if (ret)
  1935. return ret;
  1936. if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
  1937. return -EFAULT;
  1938. return 0;
  1939. }
  1940. int ethtool_get_module_eeprom_call(struct net_device *dev,
  1941. struct ethtool_eeprom *ee, u8 *data)
  1942. {
  1943. const struct ethtool_ops *ops = dev->ethtool_ops;
  1944. struct phy_device *phydev = dev->phydev;
  1945. if (dev->sfp_bus)
  1946. return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
  1947. if (phydev && phydev->drv && phydev->drv->module_eeprom)
  1948. return phydev->drv->module_eeprom(phydev, ee, data);
  1949. if (ops->get_module_eeprom)
  1950. return ops->get_module_eeprom(dev, ee, data);
  1951. return -EOPNOTSUPP;
  1952. }
  1953. static int ethtool_get_module_eeprom(struct net_device *dev,
  1954. void __user *useraddr)
  1955. {
  1956. int ret;
  1957. struct ethtool_modinfo modinfo;
  1958. ret = ethtool_get_module_info_call(dev, &modinfo);
  1959. if (ret)
  1960. return ret;
  1961. return ethtool_get_any_eeprom(dev, useraddr,
  1962. ethtool_get_module_eeprom_call,
  1963. modinfo.eeprom_len);
  1964. }
  1965. static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
  1966. {
  1967. switch (tuna->id) {
  1968. case ETHTOOL_RX_COPYBREAK:
  1969. case ETHTOOL_TX_COPYBREAK:
  1970. case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
  1971. if (tuna->len != sizeof(u32) ||
  1972. tuna->type_id != ETHTOOL_TUNABLE_U32)
  1973. return -EINVAL;
  1974. break;
  1975. case ETHTOOL_PFC_PREVENTION_TOUT:
  1976. if (tuna->len != sizeof(u16) ||
  1977. tuna->type_id != ETHTOOL_TUNABLE_U16)
  1978. return -EINVAL;
  1979. break;
  1980. default:
  1981. return -EINVAL;
  1982. }
  1983. return 0;
  1984. }
  1985. static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
  1986. {
  1987. int ret;
  1988. struct ethtool_tunable tuna;
  1989. const struct ethtool_ops *ops = dev->ethtool_ops;
  1990. void *data;
  1991. if (!ops->get_tunable)
  1992. return -EOPNOTSUPP;
  1993. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1994. return -EFAULT;
  1995. ret = ethtool_tunable_valid(&tuna);
  1996. if (ret)
  1997. return ret;
  1998. data = kzalloc(tuna.len, GFP_USER);
  1999. if (!data)
  2000. return -ENOMEM;
  2001. ret = ops->get_tunable(dev, &tuna, data);
  2002. if (ret)
  2003. goto out;
  2004. useraddr += sizeof(tuna);
  2005. ret = -EFAULT;
  2006. if (copy_to_user(useraddr, data, tuna.len))
  2007. goto out;
  2008. ret = 0;
  2009. out:
  2010. kfree(data);
  2011. return ret;
  2012. }
  2013. static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
  2014. {
  2015. int ret;
  2016. struct ethtool_tunable tuna;
  2017. const struct ethtool_ops *ops = dev->ethtool_ops;
  2018. void *data;
  2019. if (!ops->set_tunable)
  2020. return -EOPNOTSUPP;
  2021. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2022. return -EFAULT;
  2023. ret = ethtool_tunable_valid(&tuna);
  2024. if (ret)
  2025. return ret;
  2026. useraddr += sizeof(tuna);
  2027. data = memdup_user(useraddr, tuna.len);
  2028. if (IS_ERR(data))
  2029. return PTR_ERR(data);
  2030. ret = ops->set_tunable(dev, &tuna, data);
  2031. kfree(data);
  2032. return ret;
  2033. }
  2034. static noinline_for_stack int
  2035. ethtool_get_per_queue_coalesce(struct net_device *dev,
  2036. void __user *useraddr,
  2037. struct ethtool_per_queue_op *per_queue_opt)
  2038. {
  2039. u32 bit;
  2040. int ret;
  2041. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  2042. if (!dev->ethtool_ops->get_per_queue_coalesce)
  2043. return -EOPNOTSUPP;
  2044. useraddr += sizeof(*per_queue_opt);
  2045. bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
  2046. MAX_NUM_QUEUE);
  2047. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  2048. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  2049. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
  2050. if (ret != 0)
  2051. return ret;
  2052. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  2053. return -EFAULT;
  2054. useraddr += sizeof(coalesce);
  2055. }
  2056. return 0;
  2057. }
  2058. static noinline_for_stack int
  2059. ethtool_set_per_queue_coalesce(struct net_device *dev,
  2060. void __user *useraddr,
  2061. struct ethtool_per_queue_op *per_queue_opt)
  2062. {
  2063. u32 bit;
  2064. int i, ret = 0;
  2065. int n_queue;
  2066. struct ethtool_coalesce *backup = NULL, *tmp = NULL;
  2067. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  2068. if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
  2069. (!dev->ethtool_ops->get_per_queue_coalesce))
  2070. return -EOPNOTSUPP;
  2071. useraddr += sizeof(*per_queue_opt);
  2072. bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
  2073. n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
  2074. tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
  2075. if (!backup)
  2076. return -ENOMEM;
  2077. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  2078. struct ethtool_coalesce coalesce;
  2079. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
  2080. if (ret != 0)
  2081. goto roll_back;
  2082. tmp++;
  2083. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
  2084. ret = -EFAULT;
  2085. goto roll_back;
  2086. }
  2087. if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
  2088. ret = -EOPNOTSUPP;
  2089. goto roll_back;
  2090. }
  2091. ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
  2092. if (ret != 0)
  2093. goto roll_back;
  2094. useraddr += sizeof(coalesce);
  2095. }
  2096. roll_back:
  2097. if (ret != 0) {
  2098. tmp = backup;
  2099. for_each_set_bit(i, queue_mask, bit) {
  2100. dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
  2101. tmp++;
  2102. }
  2103. }
  2104. kfree(backup);
  2105. return ret;
  2106. }
  2107. static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
  2108. void __user *useraddr, u32 sub_cmd)
  2109. {
  2110. struct ethtool_per_queue_op per_queue_opt;
  2111. if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
  2112. return -EFAULT;
  2113. if (per_queue_opt.sub_command != sub_cmd)
  2114. return -EINVAL;
  2115. switch (per_queue_opt.sub_command) {
  2116. case ETHTOOL_GCOALESCE:
  2117. return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  2118. case ETHTOOL_SCOALESCE:
  2119. return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  2120. default:
  2121. return -EOPNOTSUPP;
  2122. }
  2123. }
  2124. static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
  2125. {
  2126. switch (tuna->id) {
  2127. case ETHTOOL_PHY_DOWNSHIFT:
  2128. case ETHTOOL_PHY_FAST_LINK_DOWN:
  2129. if (tuna->len != sizeof(u8) ||
  2130. tuna->type_id != ETHTOOL_TUNABLE_U8)
  2131. return -EINVAL;
  2132. break;
  2133. case ETHTOOL_PHY_EDPD:
  2134. if (tuna->len != sizeof(u16) ||
  2135. tuna->type_id != ETHTOOL_TUNABLE_U16)
  2136. return -EINVAL;
  2137. break;
  2138. default:
  2139. return -EINVAL;
  2140. }
  2141. return 0;
  2142. }
  2143. static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
  2144. {
  2145. struct phy_device *phydev = dev->phydev;
  2146. struct ethtool_tunable tuna;
  2147. bool phy_drv_tunable;
  2148. void *data;
  2149. int ret;
  2150. phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
  2151. if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
  2152. return -EOPNOTSUPP;
  2153. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2154. return -EFAULT;
  2155. ret = ethtool_phy_tunable_valid(&tuna);
  2156. if (ret)
  2157. return ret;
  2158. data = kzalloc(tuna.len, GFP_USER);
  2159. if (!data)
  2160. return -ENOMEM;
  2161. if (phy_drv_tunable) {
  2162. mutex_lock(&phydev->lock);
  2163. ret = phydev->drv->get_tunable(phydev, &tuna, data);
  2164. mutex_unlock(&phydev->lock);
  2165. } else {
  2166. ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
  2167. }
  2168. if (ret)
  2169. goto out;
  2170. useraddr += sizeof(tuna);
  2171. ret = -EFAULT;
  2172. if (copy_to_user(useraddr, data, tuna.len))
  2173. goto out;
  2174. ret = 0;
  2175. out:
  2176. kfree(data);
  2177. return ret;
  2178. }
  2179. static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
  2180. {
  2181. struct phy_device *phydev = dev->phydev;
  2182. struct ethtool_tunable tuna;
  2183. bool phy_drv_tunable;
  2184. void *data;
  2185. int ret;
  2186. phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
  2187. if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
  2188. return -EOPNOTSUPP;
  2189. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  2190. return -EFAULT;
  2191. ret = ethtool_phy_tunable_valid(&tuna);
  2192. if (ret)
  2193. return ret;
  2194. useraddr += sizeof(tuna);
  2195. data = memdup_user(useraddr, tuna.len);
  2196. if (IS_ERR(data))
  2197. return PTR_ERR(data);
  2198. if (phy_drv_tunable) {
  2199. mutex_lock(&phydev->lock);
  2200. ret = phydev->drv->set_tunable(phydev, &tuna, data);
  2201. mutex_unlock(&phydev->lock);
  2202. } else {
  2203. ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
  2204. }
  2205. kfree(data);
  2206. return ret;
  2207. }
  2208. static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
  2209. {
  2210. struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
  2211. int rc;
  2212. if (!dev->ethtool_ops->get_fecparam)
  2213. return -EOPNOTSUPP;
  2214. rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
  2215. if (rc)
  2216. return rc;
  2217. if (WARN_ON_ONCE(fecparam.reserved))
  2218. fecparam.reserved = 0;
  2219. if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
  2220. return -EFAULT;
  2221. return 0;
  2222. }
  2223. static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
  2224. {
  2225. struct ethtool_fecparam fecparam;
  2226. if (!dev->ethtool_ops->set_fecparam)
  2227. return -EOPNOTSUPP;
  2228. if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
  2229. return -EFAULT;
  2230. if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
  2231. return -EINVAL;
  2232. fecparam.active_fec = 0;
  2233. fecparam.reserved = 0;
  2234. return dev->ethtool_ops->set_fecparam(dev, &fecparam);
  2235. }
  2236. /* The main entry point in this file. Called from net/core/dev_ioctl.c */
  2237. static int
  2238. __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
  2239. u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
  2240. {
  2241. struct net_device *dev;
  2242. u32 sub_cmd;
  2243. int rc;
  2244. netdev_features_t old_features;
  2245. dev = __dev_get_by_name(net, ifr->ifr_name);
  2246. if (!dev)
  2247. return -ENODEV;
  2248. if (ethcmd == ETHTOOL_PERQUEUE) {
  2249. if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
  2250. return -EFAULT;
  2251. } else {
  2252. sub_cmd = ethcmd;
  2253. }
  2254. /* Allow some commands to be done by anyone */
  2255. switch (sub_cmd) {
  2256. case ETHTOOL_GSET:
  2257. case ETHTOOL_GDRVINFO:
  2258. case ETHTOOL_GMSGLVL:
  2259. case ETHTOOL_GLINK:
  2260. case ETHTOOL_GCOALESCE:
  2261. case ETHTOOL_GRINGPARAM:
  2262. case ETHTOOL_GPAUSEPARAM:
  2263. case ETHTOOL_GRXCSUM:
  2264. case ETHTOOL_GTXCSUM:
  2265. case ETHTOOL_GSG:
  2266. case ETHTOOL_GSSET_INFO:
  2267. case ETHTOOL_GSTRINGS:
  2268. case ETHTOOL_GSTATS:
  2269. case ETHTOOL_GPHYSTATS:
  2270. case ETHTOOL_GTSO:
  2271. case ETHTOOL_GPERMADDR:
  2272. case ETHTOOL_GUFO:
  2273. case ETHTOOL_GGSO:
  2274. case ETHTOOL_GGRO:
  2275. case ETHTOOL_GFLAGS:
  2276. case ETHTOOL_GPFLAGS:
  2277. case ETHTOOL_GRXFH:
  2278. case ETHTOOL_GRXRINGS:
  2279. case ETHTOOL_GRXCLSRLCNT:
  2280. case ETHTOOL_GRXCLSRULE:
  2281. case ETHTOOL_GRXCLSRLALL:
  2282. case ETHTOOL_GRXFHINDIR:
  2283. case ETHTOOL_GRSSH:
  2284. case ETHTOOL_GFEATURES:
  2285. case ETHTOOL_GCHANNELS:
  2286. case ETHTOOL_GET_TS_INFO:
  2287. case ETHTOOL_GEEE:
  2288. case ETHTOOL_GTUNABLE:
  2289. case ETHTOOL_PHY_GTUNABLE:
  2290. case ETHTOOL_GLINKSETTINGS:
  2291. case ETHTOOL_GFECPARAM:
  2292. break;
  2293. default:
  2294. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2295. return -EPERM;
  2296. }
  2297. if (dev->dev.parent)
  2298. pm_runtime_get_sync(dev->dev.parent);
  2299. if (!netif_device_present(dev)) {
  2300. rc = -ENODEV;
  2301. goto out;
  2302. }
  2303. if (dev->ethtool_ops->begin) {
  2304. rc = dev->ethtool_ops->begin(dev);
  2305. if (rc < 0)
  2306. goto out;
  2307. }
  2308. old_features = dev->features;
  2309. switch (ethcmd) {
  2310. case ETHTOOL_GSET:
  2311. rc = ethtool_get_settings(dev, useraddr);
  2312. break;
  2313. case ETHTOOL_SSET:
  2314. rc = ethtool_set_settings(dev, useraddr);
  2315. break;
  2316. case ETHTOOL_GDRVINFO:
  2317. rc = ethtool_get_drvinfo(dev, devlink_state);
  2318. break;
  2319. case ETHTOOL_GREGS:
  2320. rc = ethtool_get_regs(dev, useraddr);
  2321. break;
  2322. case ETHTOOL_GWOL:
  2323. rc = ethtool_get_wol(dev, useraddr);
  2324. break;
  2325. case ETHTOOL_SWOL:
  2326. rc = ethtool_set_wol(dev, useraddr);
  2327. break;
  2328. case ETHTOOL_GMSGLVL:
  2329. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2330. dev->ethtool_ops->get_msglevel);
  2331. break;
  2332. case ETHTOOL_SMSGLVL:
  2333. rc = ethtool_set_value_void(dev, useraddr,
  2334. dev->ethtool_ops->set_msglevel);
  2335. if (!rc)
  2336. ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
  2337. break;
  2338. case ETHTOOL_GEEE:
  2339. rc = ethtool_get_eee(dev, useraddr);
  2340. break;
  2341. case ETHTOOL_SEEE:
  2342. rc = ethtool_set_eee(dev, useraddr);
  2343. break;
  2344. case ETHTOOL_NWAY_RST:
  2345. rc = ethtool_nway_reset(dev);
  2346. break;
  2347. case ETHTOOL_GLINK:
  2348. rc = ethtool_get_link(dev, useraddr);
  2349. break;
  2350. case ETHTOOL_GEEPROM:
  2351. rc = ethtool_get_eeprom(dev, useraddr);
  2352. break;
  2353. case ETHTOOL_SEEPROM:
  2354. rc = ethtool_set_eeprom(dev, useraddr);
  2355. break;
  2356. case ETHTOOL_GCOALESCE:
  2357. rc = ethtool_get_coalesce(dev, useraddr);
  2358. break;
  2359. case ETHTOOL_SCOALESCE:
  2360. rc = ethtool_set_coalesce(dev, useraddr);
  2361. break;
  2362. case ETHTOOL_GRINGPARAM:
  2363. rc = ethtool_get_ringparam(dev, useraddr);
  2364. break;
  2365. case ETHTOOL_SRINGPARAM:
  2366. rc = ethtool_set_ringparam(dev, useraddr);
  2367. break;
  2368. case ETHTOOL_GPAUSEPARAM:
  2369. rc = ethtool_get_pauseparam(dev, useraddr);
  2370. break;
  2371. case ETHTOOL_SPAUSEPARAM:
  2372. rc = ethtool_set_pauseparam(dev, useraddr);
  2373. break;
  2374. case ETHTOOL_TEST:
  2375. rc = ethtool_self_test(dev, useraddr);
  2376. break;
  2377. case ETHTOOL_GSTRINGS:
  2378. rc = ethtool_get_strings(dev, useraddr);
  2379. break;
  2380. case ETHTOOL_PHYS_ID:
  2381. rc = ethtool_phys_id(dev, useraddr);
  2382. break;
  2383. case ETHTOOL_GSTATS:
  2384. rc = ethtool_get_stats(dev, useraddr);
  2385. break;
  2386. case ETHTOOL_GPERMADDR:
  2387. rc = ethtool_get_perm_addr(dev, useraddr);
  2388. break;
  2389. case ETHTOOL_GFLAGS:
  2390. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2391. __ethtool_get_flags);
  2392. break;
  2393. case ETHTOOL_SFLAGS:
  2394. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  2395. break;
  2396. case ETHTOOL_GPFLAGS:
  2397. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2398. dev->ethtool_ops->get_priv_flags);
  2399. if (!rc)
  2400. ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
  2401. break;
  2402. case ETHTOOL_SPFLAGS:
  2403. rc = ethtool_set_value(dev, useraddr,
  2404. dev->ethtool_ops->set_priv_flags);
  2405. break;
  2406. case ETHTOOL_GRXFH:
  2407. case ETHTOOL_GRXRINGS:
  2408. case ETHTOOL_GRXCLSRLCNT:
  2409. case ETHTOOL_GRXCLSRULE:
  2410. case ETHTOOL_GRXCLSRLALL:
  2411. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  2412. break;
  2413. case ETHTOOL_SRXFH:
  2414. case ETHTOOL_SRXCLSRLDEL:
  2415. case ETHTOOL_SRXCLSRLINS:
  2416. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  2417. break;
  2418. case ETHTOOL_FLASHDEV:
  2419. rc = ethtool_flash_device(dev, devlink_state);
  2420. break;
  2421. case ETHTOOL_RESET:
  2422. rc = ethtool_reset(dev, useraddr);
  2423. break;
  2424. case ETHTOOL_GSSET_INFO:
  2425. rc = ethtool_get_sset_info(dev, useraddr);
  2426. break;
  2427. case ETHTOOL_GRXFHINDIR:
  2428. rc = ethtool_get_rxfh_indir(dev, useraddr);
  2429. break;
  2430. case ETHTOOL_SRXFHINDIR:
  2431. rc = ethtool_set_rxfh_indir(dev, useraddr);
  2432. break;
  2433. case ETHTOOL_GRSSH:
  2434. rc = ethtool_get_rxfh(dev, useraddr);
  2435. break;
  2436. case ETHTOOL_SRSSH:
  2437. rc = ethtool_set_rxfh(dev, useraddr);
  2438. break;
  2439. case ETHTOOL_GFEATURES:
  2440. rc = ethtool_get_features(dev, useraddr);
  2441. break;
  2442. case ETHTOOL_SFEATURES:
  2443. rc = ethtool_set_features(dev, useraddr);
  2444. break;
  2445. case ETHTOOL_GTXCSUM:
  2446. case ETHTOOL_GRXCSUM:
  2447. case ETHTOOL_GSG:
  2448. case ETHTOOL_GTSO:
  2449. case ETHTOOL_GGSO:
  2450. case ETHTOOL_GGRO:
  2451. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  2452. break;
  2453. case ETHTOOL_STXCSUM:
  2454. case ETHTOOL_SRXCSUM:
  2455. case ETHTOOL_SSG:
  2456. case ETHTOOL_STSO:
  2457. case ETHTOOL_SGSO:
  2458. case ETHTOOL_SGRO:
  2459. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  2460. break;
  2461. case ETHTOOL_GCHANNELS:
  2462. rc = ethtool_get_channels(dev, useraddr);
  2463. break;
  2464. case ETHTOOL_SCHANNELS:
  2465. rc = ethtool_set_channels(dev, useraddr);
  2466. break;
  2467. case ETHTOOL_SET_DUMP:
  2468. rc = ethtool_set_dump(dev, useraddr);
  2469. break;
  2470. case ETHTOOL_GET_DUMP_FLAG:
  2471. rc = ethtool_get_dump_flag(dev, useraddr);
  2472. break;
  2473. case ETHTOOL_GET_DUMP_DATA:
  2474. rc = ethtool_get_dump_data(dev, useraddr);
  2475. break;
  2476. case ETHTOOL_GET_TS_INFO:
  2477. rc = ethtool_get_ts_info(dev, useraddr);
  2478. break;
  2479. case ETHTOOL_GMODULEINFO:
  2480. rc = ethtool_get_module_info(dev, useraddr);
  2481. break;
  2482. case ETHTOOL_GMODULEEEPROM:
  2483. rc = ethtool_get_module_eeprom(dev, useraddr);
  2484. break;
  2485. case ETHTOOL_GTUNABLE:
  2486. rc = ethtool_get_tunable(dev, useraddr);
  2487. break;
  2488. case ETHTOOL_STUNABLE:
  2489. rc = ethtool_set_tunable(dev, useraddr);
  2490. break;
  2491. case ETHTOOL_GPHYSTATS:
  2492. rc = ethtool_get_phy_stats(dev, useraddr);
  2493. break;
  2494. case ETHTOOL_PERQUEUE:
  2495. rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
  2496. break;
  2497. case ETHTOOL_GLINKSETTINGS:
  2498. rc = ethtool_get_link_ksettings(dev, useraddr);
  2499. break;
  2500. case ETHTOOL_SLINKSETTINGS:
  2501. rc = ethtool_set_link_ksettings(dev, useraddr);
  2502. break;
  2503. case ETHTOOL_PHY_GTUNABLE:
  2504. rc = get_phy_tunable(dev, useraddr);
  2505. break;
  2506. case ETHTOOL_PHY_STUNABLE:
  2507. rc = set_phy_tunable(dev, useraddr);
  2508. break;
  2509. case ETHTOOL_GFECPARAM:
  2510. rc = ethtool_get_fecparam(dev, useraddr);
  2511. break;
  2512. case ETHTOOL_SFECPARAM:
  2513. rc = ethtool_set_fecparam(dev, useraddr);
  2514. break;
  2515. default:
  2516. rc = -EOPNOTSUPP;
  2517. }
  2518. if (dev->ethtool_ops->complete)
  2519. dev->ethtool_ops->complete(dev);
  2520. if (old_features != dev->features)
  2521. netdev_features_change(dev);
  2522. out:
  2523. if (dev->dev.parent)
  2524. pm_runtime_put(dev->dev.parent);
  2525. return rc;
  2526. }
  2527. int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
  2528. {
  2529. struct ethtool_devlink_compat *state;
  2530. u32 ethcmd;
  2531. int rc;
  2532. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  2533. return -EFAULT;
  2534. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2535. if (!state)
  2536. return -ENOMEM;
  2537. switch (ethcmd) {
  2538. case ETHTOOL_FLASHDEV:
  2539. if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
  2540. rc = -EFAULT;
  2541. goto exit_free;
  2542. }
  2543. state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
  2544. break;
  2545. }
  2546. rtnl_lock();
  2547. rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
  2548. rtnl_unlock();
  2549. if (rc)
  2550. goto exit_free;
  2551. switch (ethcmd) {
  2552. case ETHTOOL_FLASHDEV:
  2553. if (state->devlink)
  2554. rc = devlink_compat_flash_update(state->devlink,
  2555. state->efl.data);
  2556. break;
  2557. case ETHTOOL_GDRVINFO:
  2558. if (state->devlink)
  2559. devlink_compat_running_version(state->devlink,
  2560. state->info.fw_version,
  2561. sizeof(state->info.fw_version));
  2562. if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
  2563. rc = -EFAULT;
  2564. goto exit_free;
  2565. }
  2566. break;
  2567. }
  2568. exit_free:
  2569. if (state->devlink)
  2570. devlink_put(state->devlink);
  2571. kfree(state);
  2572. return rc;
  2573. }
  2574. struct ethtool_rx_flow_key {
  2575. struct flow_dissector_key_basic basic;
  2576. union {
  2577. struct flow_dissector_key_ipv4_addrs ipv4;
  2578. struct flow_dissector_key_ipv6_addrs ipv6;
  2579. };
  2580. struct flow_dissector_key_ports tp;
  2581. struct flow_dissector_key_ip ip;
  2582. struct flow_dissector_key_vlan vlan;
  2583. struct flow_dissector_key_eth_addrs eth_addrs;
  2584. } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  2585. struct ethtool_rx_flow_match {
  2586. struct flow_dissector dissector;
  2587. struct ethtool_rx_flow_key key;
  2588. struct ethtool_rx_flow_key mask;
  2589. };
  2590. struct ethtool_rx_flow_rule *
  2591. ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
  2592. {
  2593. const struct ethtool_rx_flow_spec *fs = input->fs;
  2594. struct ethtool_rx_flow_match *match;
  2595. struct ethtool_rx_flow_rule *flow;
  2596. struct flow_action_entry *act;
  2597. flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
  2598. sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
  2599. if (!flow)
  2600. return ERR_PTR(-ENOMEM);
  2601. /* ethtool_rx supports only one single action per rule. */
  2602. flow->rule = flow_rule_alloc(1);
  2603. if (!flow->rule) {
  2604. kfree(flow);
  2605. return ERR_PTR(-ENOMEM);
  2606. }
  2607. match = (struct ethtool_rx_flow_match *)flow->priv;
  2608. flow->rule->match.dissector = &match->dissector;
  2609. flow->rule->match.mask = &match->mask;
  2610. flow->rule->match.key = &match->key;
  2611. match->mask.basic.n_proto = htons(0xffff);
  2612. switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
  2613. case ETHER_FLOW: {
  2614. const struct ethhdr *ether_spec, *ether_m_spec;
  2615. ether_spec = &fs->h_u.ether_spec;
  2616. ether_m_spec = &fs->m_u.ether_spec;
  2617. if (!is_zero_ether_addr(ether_m_spec->h_source)) {
  2618. ether_addr_copy(match->key.eth_addrs.src,
  2619. ether_spec->h_source);
  2620. ether_addr_copy(match->mask.eth_addrs.src,
  2621. ether_m_spec->h_source);
  2622. }
  2623. if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
  2624. ether_addr_copy(match->key.eth_addrs.dst,
  2625. ether_spec->h_dest);
  2626. ether_addr_copy(match->mask.eth_addrs.dst,
  2627. ether_m_spec->h_dest);
  2628. }
  2629. if (ether_m_spec->h_proto) {
  2630. match->key.basic.n_proto = ether_spec->h_proto;
  2631. match->mask.basic.n_proto = ether_m_spec->h_proto;
  2632. }
  2633. }
  2634. break;
  2635. case TCP_V4_FLOW:
  2636. case UDP_V4_FLOW: {
  2637. const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
  2638. match->key.basic.n_proto = htons(ETH_P_IP);
  2639. v4_spec = &fs->h_u.tcp_ip4_spec;
  2640. v4_m_spec = &fs->m_u.tcp_ip4_spec;
  2641. if (v4_m_spec->ip4src) {
  2642. match->key.ipv4.src = v4_spec->ip4src;
  2643. match->mask.ipv4.src = v4_m_spec->ip4src;
  2644. }
  2645. if (v4_m_spec->ip4dst) {
  2646. match->key.ipv4.dst = v4_spec->ip4dst;
  2647. match->mask.ipv4.dst = v4_m_spec->ip4dst;
  2648. }
  2649. if (v4_m_spec->ip4src ||
  2650. v4_m_spec->ip4dst) {
  2651. match->dissector.used_keys |=
  2652. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
  2653. match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
  2654. offsetof(struct ethtool_rx_flow_key, ipv4);
  2655. }
  2656. if (v4_m_spec->psrc) {
  2657. match->key.tp.src = v4_spec->psrc;
  2658. match->mask.tp.src = v4_m_spec->psrc;
  2659. }
  2660. if (v4_m_spec->pdst) {
  2661. match->key.tp.dst = v4_spec->pdst;
  2662. match->mask.tp.dst = v4_m_spec->pdst;
  2663. }
  2664. if (v4_m_spec->psrc ||
  2665. v4_m_spec->pdst) {
  2666. match->dissector.used_keys |=
  2667. BIT(FLOW_DISSECTOR_KEY_PORTS);
  2668. match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
  2669. offsetof(struct ethtool_rx_flow_key, tp);
  2670. }
  2671. if (v4_m_spec->tos) {
  2672. match->key.ip.tos = v4_spec->tos;
  2673. match->mask.ip.tos = v4_m_spec->tos;
  2674. match->dissector.used_keys |=
  2675. BIT(FLOW_DISSECTOR_KEY_IP);
  2676. match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
  2677. offsetof(struct ethtool_rx_flow_key, ip);
  2678. }
  2679. }
  2680. break;
  2681. case TCP_V6_FLOW:
  2682. case UDP_V6_FLOW: {
  2683. const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
  2684. match->key.basic.n_proto = htons(ETH_P_IPV6);
  2685. v6_spec = &fs->h_u.tcp_ip6_spec;
  2686. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  2687. if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
  2688. memcpy(&match->key.ipv6.src, v6_spec->ip6src,
  2689. sizeof(match->key.ipv6.src));
  2690. memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
  2691. sizeof(match->mask.ipv6.src));
  2692. }
  2693. if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
  2694. memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
  2695. sizeof(match->key.ipv6.dst));
  2696. memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
  2697. sizeof(match->mask.ipv6.dst));
  2698. }
  2699. if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
  2700. !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
  2701. match->dissector.used_keys |=
  2702. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
  2703. match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
  2704. offsetof(struct ethtool_rx_flow_key, ipv6);
  2705. }
  2706. if (v6_m_spec->psrc) {
  2707. match->key.tp.src = v6_spec->psrc;
  2708. match->mask.tp.src = v6_m_spec->psrc;
  2709. }
  2710. if (v6_m_spec->pdst) {
  2711. match->key.tp.dst = v6_spec->pdst;
  2712. match->mask.tp.dst = v6_m_spec->pdst;
  2713. }
  2714. if (v6_m_spec->psrc ||
  2715. v6_m_spec->pdst) {
  2716. match->dissector.used_keys |=
  2717. BIT(FLOW_DISSECTOR_KEY_PORTS);
  2718. match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
  2719. offsetof(struct ethtool_rx_flow_key, tp);
  2720. }
  2721. if (v6_m_spec->tclass) {
  2722. match->key.ip.tos = v6_spec->tclass;
  2723. match->mask.ip.tos = v6_m_spec->tclass;
  2724. match->dissector.used_keys |=
  2725. BIT(FLOW_DISSECTOR_KEY_IP);
  2726. match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
  2727. offsetof(struct ethtool_rx_flow_key, ip);
  2728. }
  2729. }
  2730. break;
  2731. default:
  2732. ethtool_rx_flow_rule_destroy(flow);
  2733. return ERR_PTR(-EINVAL);
  2734. }
  2735. switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
  2736. case TCP_V4_FLOW:
  2737. case TCP_V6_FLOW:
  2738. match->key.basic.ip_proto = IPPROTO_TCP;
  2739. match->mask.basic.ip_proto = 0xff;
  2740. break;
  2741. case UDP_V4_FLOW:
  2742. case UDP_V6_FLOW:
  2743. match->key.basic.ip_proto = IPPROTO_UDP;
  2744. match->mask.basic.ip_proto = 0xff;
  2745. break;
  2746. }
  2747. match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
  2748. match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
  2749. offsetof(struct ethtool_rx_flow_key, basic);
  2750. if (fs->flow_type & FLOW_EXT) {
  2751. const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
  2752. const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
  2753. if (ext_m_spec->vlan_etype) {
  2754. match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
  2755. match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
  2756. }
  2757. if (ext_m_spec->vlan_tci) {
  2758. match->key.vlan.vlan_id =
  2759. ntohs(ext_h_spec->vlan_tci) & 0x0fff;
  2760. match->mask.vlan.vlan_id =
  2761. ntohs(ext_m_spec->vlan_tci) & 0x0fff;
  2762. match->key.vlan.vlan_dei =
  2763. !!(ext_h_spec->vlan_tci & htons(0x1000));
  2764. match->mask.vlan.vlan_dei =
  2765. !!(ext_m_spec->vlan_tci & htons(0x1000));
  2766. match->key.vlan.vlan_priority =
  2767. (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
  2768. match->mask.vlan.vlan_priority =
  2769. (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
  2770. }
  2771. if (ext_m_spec->vlan_etype ||
  2772. ext_m_spec->vlan_tci) {
  2773. match->dissector.used_keys |=
  2774. BIT(FLOW_DISSECTOR_KEY_VLAN);
  2775. match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
  2776. offsetof(struct ethtool_rx_flow_key, vlan);
  2777. }
  2778. }
  2779. if (fs->flow_type & FLOW_MAC_EXT) {
  2780. const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
  2781. const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
  2782. memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
  2783. ETH_ALEN);
  2784. memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
  2785. ETH_ALEN);
  2786. match->dissector.used_keys |=
  2787. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
  2788. match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
  2789. offsetof(struct ethtool_rx_flow_key, eth_addrs);
  2790. }
  2791. act = &flow->rule->action.entries[0];
  2792. switch (fs->ring_cookie) {
  2793. case RX_CLS_FLOW_DISC:
  2794. act->id = FLOW_ACTION_DROP;
  2795. break;
  2796. case RX_CLS_FLOW_WAKE:
  2797. act->id = FLOW_ACTION_WAKE;
  2798. break;
  2799. default:
  2800. act->id = FLOW_ACTION_QUEUE;
  2801. if (fs->flow_type & FLOW_RSS)
  2802. act->queue.ctx = input->rss_ctx;
  2803. act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
  2804. act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
  2805. break;
  2806. }
  2807. return flow;
  2808. }
  2809. EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
  2810. void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
  2811. {
  2812. kfree(flow->rule);
  2813. kfree(flow);
  2814. }
  2815. EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);