ioctl.c 98 KB

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