mac80211_hwsim.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
  5. * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /*
  12. * TODO:
  13. * - Add TSF sync and fix IBSS beacon transmission by adding
  14. * competition for "air time" at TBTT
  15. * - RX filtering based on filter configuration (data->rx_filter)
  16. */
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <net/dst.h>
  21. #include <net/xfrm.h>
  22. #include <net/mac80211.h>
  23. #include <net/ieee80211_radiotap.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/rtnetlink.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/module.h>
  30. #include <linux/ktime.h>
  31. #include <net/genetlink.h>
  32. #include <net/net_namespace.h>
  33. #include <net/netns/generic.h>
  34. #include <linux/rhashtable.h>
  35. #include <linux/nospec.h>
  36. #include "mac80211_hwsim.h"
  37. #define WARN_QUEUE 100
  38. #define MAX_QUEUE 200
  39. MODULE_AUTHOR("Jouni Malinen");
  40. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  41. MODULE_LICENSE("GPL");
  42. static int radios = 2;
  43. module_param(radios, int, 0444);
  44. MODULE_PARM_DESC(radios, "Number of simulated radios");
  45. static int channels = 1;
  46. module_param(channels, int, 0444);
  47. MODULE_PARM_DESC(channels, "Number of concurrent channels");
  48. static bool paged_rx = false;
  49. module_param(paged_rx, bool, 0644);
  50. MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
  51. static bool rctbl = false;
  52. module_param(rctbl, bool, 0444);
  53. MODULE_PARM_DESC(rctbl, "Handle rate control table");
  54. static bool support_p2p_device = true;
  55. module_param(support_p2p_device, bool, 0444);
  56. MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
  57. /**
  58. * enum hwsim_regtest - the type of regulatory tests we offer
  59. *
  60. * These are the different values you can use for the regtest
  61. * module parameter. This is useful to help test world roaming
  62. * and the driver regulatory_hint() call and combinations of these.
  63. * If you want to do specific alpha2 regulatory domain tests simply
  64. * use the userspace regulatory request as that will be respected as
  65. * well without the need of this module parameter. This is designed
  66. * only for testing the driver regulatory request, world roaming
  67. * and all possible combinations.
  68. *
  69. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  70. * this is the default value.
  71. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  72. * hint, only one driver regulatory hint will be sent as such the
  73. * secondary radios are expected to follow.
  74. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  75. * request with all radios reporting the same regulatory domain.
  76. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  77. * different regulatory domains requests. Expected behaviour is for
  78. * an intersection to occur but each device will still use their
  79. * respective regulatory requested domains. Subsequent radios will
  80. * use the resulting intersection.
  81. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  82. * this by using a custom beacon-capable regulatory domain for the first
  83. * radio. All other device world roam.
  84. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  85. * domain requests. All radios will adhere to this custom world regulatory
  86. * domain.
  87. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  88. * domain requests. The first radio will adhere to the first custom world
  89. * regulatory domain, the second one to the second custom world regulatory
  90. * domain. All other devices will world roam.
  91. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  92. * settings, only the first radio will send a regulatory domain request
  93. * and use strict settings. The rest of the radios are expected to follow.
  94. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  95. * settings. All radios will adhere to this.
  96. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  97. * domain settings, combined with secondary driver regulatory domain
  98. * settings. The first radio will get a strict regulatory domain setting
  99. * using the first driver regulatory request and the second radio will use
  100. * non-strict settings using the second driver regulatory request. All
  101. * other devices should follow the intersection created between the
  102. * first two.
  103. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  104. * at least 6 radios for a complete test. We will test in this order:
  105. * 1 - driver custom world regulatory domain
  106. * 2 - second custom world regulatory domain
  107. * 3 - first driver regulatory domain request
  108. * 4 - second driver regulatory domain request
  109. * 5 - strict regulatory domain settings using the third driver regulatory
  110. * domain request
  111. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  112. * regulatory requests.
  113. */
  114. enum hwsim_regtest {
  115. HWSIM_REGTEST_DISABLED = 0,
  116. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  117. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  118. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  119. HWSIM_REGTEST_WORLD_ROAM = 4,
  120. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  121. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  122. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  123. HWSIM_REGTEST_STRICT_ALL = 8,
  124. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  125. HWSIM_REGTEST_ALL = 10,
  126. };
  127. /* Set to one of the HWSIM_REGTEST_* values above */
  128. static int regtest = HWSIM_REGTEST_DISABLED;
  129. module_param(regtest, int, 0444);
  130. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  131. static const char *hwsim_alpha2s[] = {
  132. "FI",
  133. "AL",
  134. "US",
  135. "DE",
  136. "JP",
  137. "AL",
  138. };
  139. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  140. .n_reg_rules = 4,
  141. .alpha2 = "99",
  142. .reg_rules = {
  143. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  144. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  145. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  146. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  147. }
  148. };
  149. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  150. .n_reg_rules = 2,
  151. .alpha2 = "99",
  152. .reg_rules = {
  153. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  154. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  155. NL80211_RRF_NO_IR),
  156. }
  157. };
  158. static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
  159. &hwsim_world_regdom_custom_01,
  160. &hwsim_world_regdom_custom_02,
  161. };
  162. struct hwsim_vif_priv {
  163. u32 magic;
  164. u8 bssid[ETH_ALEN];
  165. bool assoc;
  166. bool bcn_en;
  167. u16 aid;
  168. };
  169. #define HWSIM_VIF_MAGIC 0x69537748
  170. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  171. {
  172. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  173. WARN(vp->magic != HWSIM_VIF_MAGIC,
  174. "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
  175. vif, vp->magic, vif->addr, vif->type, vif->p2p);
  176. }
  177. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  178. {
  179. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  180. vp->magic = HWSIM_VIF_MAGIC;
  181. }
  182. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  183. {
  184. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  185. vp->magic = 0;
  186. }
  187. struct hwsim_sta_priv {
  188. u32 magic;
  189. };
  190. #define HWSIM_STA_MAGIC 0x6d537749
  191. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  192. {
  193. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  194. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  195. }
  196. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  197. {
  198. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  199. sp->magic = HWSIM_STA_MAGIC;
  200. }
  201. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  202. {
  203. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  204. sp->magic = 0;
  205. }
  206. struct hwsim_chanctx_priv {
  207. u32 magic;
  208. };
  209. #define HWSIM_CHANCTX_MAGIC 0x6d53774a
  210. static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
  211. {
  212. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  213. WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
  214. }
  215. static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
  216. {
  217. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  218. cp->magic = HWSIM_CHANCTX_MAGIC;
  219. }
  220. static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
  221. {
  222. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  223. cp->magic = 0;
  224. }
  225. static unsigned int hwsim_net_id;
  226. static DEFINE_IDA(hwsim_netgroup_ida);
  227. struct hwsim_net {
  228. int netgroup;
  229. u32 wmediumd;
  230. };
  231. static inline int hwsim_net_get_netgroup(struct net *net)
  232. {
  233. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  234. return hwsim_net->netgroup;
  235. }
  236. static inline int hwsim_net_set_netgroup(struct net *net)
  237. {
  238. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  239. hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
  240. 0, 0, GFP_KERNEL);
  241. return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
  242. }
  243. static inline u32 hwsim_net_get_wmediumd(struct net *net)
  244. {
  245. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  246. return hwsim_net->wmediumd;
  247. }
  248. static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
  249. {
  250. struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
  251. hwsim_net->wmediumd = portid;
  252. }
  253. static struct class *hwsim_class;
  254. static struct net_device *hwsim_mon; /* global monitor netdev */
  255. #define CHAN2G(_freq) { \
  256. .band = NL80211_BAND_2GHZ, \
  257. .center_freq = (_freq), \
  258. .hw_value = (_freq), \
  259. .max_power = 20, \
  260. }
  261. #define CHAN5G(_freq) { \
  262. .band = NL80211_BAND_5GHZ, \
  263. .center_freq = (_freq), \
  264. .hw_value = (_freq), \
  265. .max_power = 20, \
  266. }
  267. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  268. CHAN2G(2412), /* Channel 1 */
  269. CHAN2G(2417), /* Channel 2 */
  270. CHAN2G(2422), /* Channel 3 */
  271. CHAN2G(2427), /* Channel 4 */
  272. CHAN2G(2432), /* Channel 5 */
  273. CHAN2G(2437), /* Channel 6 */
  274. CHAN2G(2442), /* Channel 7 */
  275. CHAN2G(2447), /* Channel 8 */
  276. CHAN2G(2452), /* Channel 9 */
  277. CHAN2G(2457), /* Channel 10 */
  278. CHAN2G(2462), /* Channel 11 */
  279. CHAN2G(2467), /* Channel 12 */
  280. CHAN2G(2472), /* Channel 13 */
  281. CHAN2G(2484), /* Channel 14 */
  282. };
  283. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  284. CHAN5G(5180), /* Channel 36 */
  285. CHAN5G(5200), /* Channel 40 */
  286. CHAN5G(5220), /* Channel 44 */
  287. CHAN5G(5240), /* Channel 48 */
  288. CHAN5G(5260), /* Channel 52 */
  289. CHAN5G(5280), /* Channel 56 */
  290. CHAN5G(5300), /* Channel 60 */
  291. CHAN5G(5320), /* Channel 64 */
  292. CHAN5G(5500), /* Channel 100 */
  293. CHAN5G(5520), /* Channel 104 */
  294. CHAN5G(5540), /* Channel 108 */
  295. CHAN5G(5560), /* Channel 112 */
  296. CHAN5G(5580), /* Channel 116 */
  297. CHAN5G(5600), /* Channel 120 */
  298. CHAN5G(5620), /* Channel 124 */
  299. CHAN5G(5640), /* Channel 128 */
  300. CHAN5G(5660), /* Channel 132 */
  301. CHAN5G(5680), /* Channel 136 */
  302. CHAN5G(5700), /* Channel 140 */
  303. CHAN5G(5745), /* Channel 149 */
  304. CHAN5G(5765), /* Channel 153 */
  305. CHAN5G(5785), /* Channel 157 */
  306. CHAN5G(5805), /* Channel 161 */
  307. CHAN5G(5825), /* Channel 165 */
  308. CHAN5G(5845), /* Channel 169 */
  309. };
  310. static const struct ieee80211_rate hwsim_rates[] = {
  311. { .bitrate = 10 },
  312. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  313. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  314. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  315. { .bitrate = 60 },
  316. { .bitrate = 90 },
  317. { .bitrate = 120 },
  318. { .bitrate = 180 },
  319. { .bitrate = 240 },
  320. { .bitrate = 360 },
  321. { .bitrate = 480 },
  322. { .bitrate = 540 }
  323. };
  324. #define OUI_QCA 0x001374
  325. #define QCA_NL80211_SUBCMD_TEST 1
  326. enum qca_nl80211_vendor_subcmds {
  327. QCA_WLAN_VENDOR_ATTR_TEST = 8,
  328. QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
  329. };
  330. static const struct nla_policy
  331. hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
  332. [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
  333. };
  334. static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
  335. struct wireless_dev *wdev,
  336. const void *data, int data_len)
  337. {
  338. struct sk_buff *skb;
  339. struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
  340. int err;
  341. u32 val;
  342. err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
  343. hwsim_vendor_test_policy, NULL);
  344. if (err)
  345. return err;
  346. if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
  347. return -EINVAL;
  348. val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
  349. wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
  350. /* Send a vendor event as a test. Note that this would not normally be
  351. * done within a command handler, but rather, based on some other
  352. * trigger. For simplicity, this command is used to trigger the event
  353. * here.
  354. *
  355. * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
  356. */
  357. skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
  358. if (skb) {
  359. /* skb_put() or nla_put() will fill up data within
  360. * NL80211_ATTR_VENDOR_DATA.
  361. */
  362. /* Add vendor data */
  363. nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
  364. /* Send the event - this will call nla_nest_end() */
  365. cfg80211_vendor_event(skb, GFP_KERNEL);
  366. }
  367. /* Send a response to the command */
  368. skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
  369. if (!skb)
  370. return -ENOMEM;
  371. /* skb_put() or nla_put() will fill up data within
  372. * NL80211_ATTR_VENDOR_DATA
  373. */
  374. nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
  375. return cfg80211_vendor_cmd_reply(skb);
  376. }
  377. static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
  378. {
  379. .info = { .vendor_id = OUI_QCA,
  380. .subcmd = QCA_NL80211_SUBCMD_TEST },
  381. .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
  382. .doit = mac80211_hwsim_vendor_cmd_test,
  383. }
  384. };
  385. /* Advertise support vendor specific events */
  386. static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
  387. { .vendor_id = OUI_QCA, .subcmd = 1 },
  388. };
  389. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  390. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  391. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  392. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  393. #ifdef CONFIG_MAC80211_MESH
  394. BIT(NL80211_IFTYPE_MESH_POINT) |
  395. #endif
  396. BIT(NL80211_IFTYPE_AP) |
  397. BIT(NL80211_IFTYPE_P2P_GO) },
  398. /* must be last, see hwsim_if_comb */
  399. { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
  400. };
  401. static const struct ieee80211_iface_combination hwsim_if_comb[] = {
  402. {
  403. .limits = hwsim_if_limits,
  404. /* remove the last entry which is P2P_DEVICE */
  405. .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
  406. .max_interfaces = 2048,
  407. .num_different_channels = 1,
  408. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  409. BIT(NL80211_CHAN_WIDTH_20) |
  410. BIT(NL80211_CHAN_WIDTH_40) |
  411. BIT(NL80211_CHAN_WIDTH_80) |
  412. BIT(NL80211_CHAN_WIDTH_160),
  413. },
  414. };
  415. static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
  416. {
  417. .limits = hwsim_if_limits,
  418. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  419. .max_interfaces = 2048,
  420. .num_different_channels = 1,
  421. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  422. BIT(NL80211_CHAN_WIDTH_20) |
  423. BIT(NL80211_CHAN_WIDTH_40) |
  424. BIT(NL80211_CHAN_WIDTH_80) |
  425. BIT(NL80211_CHAN_WIDTH_160),
  426. },
  427. };
  428. static spinlock_t hwsim_radio_lock;
  429. static LIST_HEAD(hwsim_radios);
  430. static struct workqueue_struct *hwsim_wq;
  431. static struct rhashtable hwsim_radios_rht;
  432. static int hwsim_radio_idx;
  433. static int hwsim_radios_generation = 1;
  434. static struct platform_driver mac80211_hwsim_driver = {
  435. .driver = {
  436. .name = "mac80211_hwsim",
  437. },
  438. };
  439. struct mac80211_hwsim_data {
  440. struct list_head list;
  441. struct rhash_head rht;
  442. struct ieee80211_hw *hw;
  443. struct device *dev;
  444. struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
  445. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  446. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  447. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  448. struct ieee80211_iface_combination if_combination;
  449. struct mac_address addresses[2];
  450. int channels, idx;
  451. bool use_chanctx;
  452. bool destroy_on_close;
  453. u32 portid;
  454. char alpha2[2];
  455. const struct ieee80211_regdomain *regd;
  456. struct ieee80211_channel *tmp_chan;
  457. struct ieee80211_channel *roc_chan;
  458. u32 roc_duration;
  459. struct delayed_work roc_start;
  460. struct delayed_work roc_done;
  461. struct delayed_work hw_scan;
  462. struct cfg80211_scan_request *hw_scan_request;
  463. struct ieee80211_vif *hw_scan_vif;
  464. int scan_chan_idx;
  465. u8 scan_addr[ETH_ALEN];
  466. struct {
  467. struct ieee80211_channel *channel;
  468. unsigned long next_start, start, end;
  469. } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
  470. ARRAY_SIZE(hwsim_channels_5ghz)];
  471. struct ieee80211_channel *channel;
  472. u64 beacon_int /* beacon interval in us */;
  473. unsigned int rx_filter;
  474. bool started, idle, scanning;
  475. struct mutex mutex;
  476. struct tasklet_hrtimer beacon_timer;
  477. enum ps_mode {
  478. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  479. } ps;
  480. bool ps_poll_pending;
  481. struct dentry *debugfs;
  482. uintptr_t pending_cookie;
  483. struct sk_buff_head pending; /* packets pending */
  484. /*
  485. * Only radios in the same group can communicate together (the
  486. * channel has to match too). Each bit represents a group. A
  487. * radio can be in more than one group.
  488. */
  489. u64 group;
  490. /* group shared by radios created in the same netns */
  491. int netgroup;
  492. /* wmediumd portid responsible for netgroup of this radio */
  493. u32 wmediumd;
  494. /* difference between this hw's clock and the real clock, in usecs */
  495. s64 tsf_offset;
  496. s64 bcn_delta;
  497. /* absolute beacon transmission time. Used to cover up "tx" delay. */
  498. u64 abs_bcn_ts;
  499. /* Stats */
  500. u64 tx_pkts;
  501. u64 rx_pkts;
  502. u64 tx_bytes;
  503. u64 rx_bytes;
  504. u64 tx_dropped;
  505. u64 tx_failed;
  506. };
  507. static const struct rhashtable_params hwsim_rht_params = {
  508. .nelem_hint = 2,
  509. .automatic_shrinking = true,
  510. .key_len = ETH_ALEN,
  511. .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
  512. .head_offset = offsetof(struct mac80211_hwsim_data, rht),
  513. };
  514. struct hwsim_radiotap_hdr {
  515. struct ieee80211_radiotap_header hdr;
  516. __le64 rt_tsft;
  517. u8 rt_flags;
  518. u8 rt_rate;
  519. __le16 rt_channel;
  520. __le16 rt_chbitmask;
  521. } __packed;
  522. struct hwsim_radiotap_ack_hdr {
  523. struct ieee80211_radiotap_header hdr;
  524. u8 rt_flags;
  525. u8 pad;
  526. __le16 rt_channel;
  527. __le16 rt_chbitmask;
  528. } __packed;
  529. /* MAC80211_HWSIM netlink family */
  530. static struct genl_family hwsim_genl_family;
  531. enum hwsim_multicast_groups {
  532. HWSIM_MCGRP_CONFIG,
  533. };
  534. static const struct genl_multicast_group hwsim_mcgrps[] = {
  535. [HWSIM_MCGRP_CONFIG] = { .name = "config", },
  536. };
  537. /* MAC80211_HWSIM netlink policy */
  538. static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  539. [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
  540. [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
  541. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  542. .len = IEEE80211_MAX_DATA_LEN },
  543. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  544. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  545. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  546. [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
  547. .len = IEEE80211_TX_MAX_RATES *
  548. sizeof(struct hwsim_tx_rate)},
  549. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  550. [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
  551. [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
  552. [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
  553. [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
  554. [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
  555. [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
  556. [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
  557. [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
  558. [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
  559. [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
  560. [HWSIM_ATTR_PERM_ADDR] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
  561. };
  562. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  563. struct sk_buff *skb,
  564. struct ieee80211_channel *chan);
  565. /* sysfs attributes */
  566. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  567. {
  568. struct mac80211_hwsim_data *data = dat;
  569. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  570. struct sk_buff *skb;
  571. struct ieee80211_pspoll *pspoll;
  572. if (!vp->assoc)
  573. return;
  574. wiphy_dbg(data->hw->wiphy,
  575. "%s: send PS-Poll to %pM for aid %d\n",
  576. __func__, vp->bssid, vp->aid);
  577. skb = dev_alloc_skb(sizeof(*pspoll));
  578. if (!skb)
  579. return;
  580. pspoll = skb_put(skb, sizeof(*pspoll));
  581. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  582. IEEE80211_STYPE_PSPOLL |
  583. IEEE80211_FCTL_PM);
  584. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  585. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  586. memcpy(pspoll->ta, mac, ETH_ALEN);
  587. rcu_read_lock();
  588. mac80211_hwsim_tx_frame(data->hw, skb,
  589. rcu_dereference(vif->chanctx_conf)->def.chan);
  590. rcu_read_unlock();
  591. }
  592. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  593. struct ieee80211_vif *vif, int ps)
  594. {
  595. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  596. struct sk_buff *skb;
  597. struct ieee80211_hdr *hdr;
  598. if (!vp->assoc)
  599. return;
  600. wiphy_dbg(data->hw->wiphy,
  601. "%s: send data::nullfunc to %pM ps=%d\n",
  602. __func__, vp->bssid, ps);
  603. skb = dev_alloc_skb(sizeof(*hdr));
  604. if (!skb)
  605. return;
  606. hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  607. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  608. IEEE80211_STYPE_NULLFUNC |
  609. IEEE80211_FCTL_TODS |
  610. (ps ? IEEE80211_FCTL_PM : 0));
  611. hdr->duration_id = cpu_to_le16(0);
  612. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  613. memcpy(hdr->addr2, mac, ETH_ALEN);
  614. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  615. rcu_read_lock();
  616. mac80211_hwsim_tx_frame(data->hw, skb,
  617. rcu_dereference(vif->chanctx_conf)->def.chan);
  618. rcu_read_unlock();
  619. }
  620. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  621. struct ieee80211_vif *vif)
  622. {
  623. struct mac80211_hwsim_data *data = dat;
  624. hwsim_send_nullfunc(data, mac, vif, 1);
  625. }
  626. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  627. struct ieee80211_vif *vif)
  628. {
  629. struct mac80211_hwsim_data *data = dat;
  630. hwsim_send_nullfunc(data, mac, vif, 0);
  631. }
  632. static int hwsim_fops_ps_read(void *dat, u64 *val)
  633. {
  634. struct mac80211_hwsim_data *data = dat;
  635. *val = data->ps;
  636. return 0;
  637. }
  638. static int hwsim_fops_ps_write(void *dat, u64 val)
  639. {
  640. struct mac80211_hwsim_data *data = dat;
  641. enum ps_mode old_ps;
  642. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  643. val != PS_MANUAL_POLL)
  644. return -EINVAL;
  645. if (val == PS_MANUAL_POLL) {
  646. if (data->ps != PS_ENABLED)
  647. return -EINVAL;
  648. local_bh_disable();
  649. ieee80211_iterate_active_interfaces_atomic(
  650. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  651. hwsim_send_ps_poll, data);
  652. local_bh_enable();
  653. return 0;
  654. }
  655. old_ps = data->ps;
  656. data->ps = val;
  657. local_bh_disable();
  658. if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  659. ieee80211_iterate_active_interfaces_atomic(
  660. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  661. hwsim_send_nullfunc_ps, data);
  662. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  663. ieee80211_iterate_active_interfaces_atomic(
  664. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  665. hwsim_send_nullfunc_no_ps, data);
  666. }
  667. local_bh_enable();
  668. return 0;
  669. }
  670. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  671. "%llu\n");
  672. static int hwsim_write_simulate_radar(void *dat, u64 val)
  673. {
  674. struct mac80211_hwsim_data *data = dat;
  675. ieee80211_radar_detected(data->hw);
  676. return 0;
  677. }
  678. DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
  679. hwsim_write_simulate_radar, "%llu\n");
  680. static int hwsim_fops_group_read(void *dat, u64 *val)
  681. {
  682. struct mac80211_hwsim_data *data = dat;
  683. *val = data->group;
  684. return 0;
  685. }
  686. static int hwsim_fops_group_write(void *dat, u64 val)
  687. {
  688. struct mac80211_hwsim_data *data = dat;
  689. data->group = val;
  690. return 0;
  691. }
  692. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  693. hwsim_fops_group_read, hwsim_fops_group_write,
  694. "%llx\n");
  695. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  696. struct net_device *dev)
  697. {
  698. /* TODO: allow packet injection */
  699. dev_kfree_skb(skb);
  700. return NETDEV_TX_OK;
  701. }
  702. static inline u64 mac80211_hwsim_get_tsf_raw(void)
  703. {
  704. return ktime_to_us(ktime_get_real());
  705. }
  706. static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
  707. {
  708. u64 now = mac80211_hwsim_get_tsf_raw();
  709. return cpu_to_le64(now + data->tsf_offset);
  710. }
  711. static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
  712. struct ieee80211_vif *vif)
  713. {
  714. struct mac80211_hwsim_data *data = hw->priv;
  715. return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
  716. }
  717. static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
  718. struct ieee80211_vif *vif, u64 tsf)
  719. {
  720. struct mac80211_hwsim_data *data = hw->priv;
  721. u64 now = mac80211_hwsim_get_tsf(hw, vif);
  722. u32 bcn_int = data->beacon_int;
  723. u64 delta = abs(tsf - now);
  724. /* adjust after beaconing with new timestamp at old TBTT */
  725. if (tsf > now) {
  726. data->tsf_offset += delta;
  727. data->bcn_delta = do_div(delta, bcn_int);
  728. } else {
  729. data->tsf_offset -= delta;
  730. data->bcn_delta = -(s64)do_div(delta, bcn_int);
  731. }
  732. }
  733. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  734. struct sk_buff *tx_skb,
  735. struct ieee80211_channel *chan)
  736. {
  737. struct mac80211_hwsim_data *data = hw->priv;
  738. struct sk_buff *skb;
  739. struct hwsim_radiotap_hdr *hdr;
  740. u16 flags;
  741. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  742. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  743. if (WARN_ON(!txrate))
  744. return;
  745. if (!netif_running(hwsim_mon))
  746. return;
  747. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  748. if (skb == NULL)
  749. return;
  750. hdr = skb_push(skb, sizeof(*hdr));
  751. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  752. hdr->hdr.it_pad = 0;
  753. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  754. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  755. (1 << IEEE80211_RADIOTAP_RATE) |
  756. (1 << IEEE80211_RADIOTAP_TSFT) |
  757. (1 << IEEE80211_RADIOTAP_CHANNEL));
  758. hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
  759. hdr->rt_flags = 0;
  760. hdr->rt_rate = txrate->bitrate / 5;
  761. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  762. flags = IEEE80211_CHAN_2GHZ;
  763. if (txrate->flags & IEEE80211_RATE_ERP_G)
  764. flags |= IEEE80211_CHAN_OFDM;
  765. else
  766. flags |= IEEE80211_CHAN_CCK;
  767. hdr->rt_chbitmask = cpu_to_le16(flags);
  768. skb->dev = hwsim_mon;
  769. skb_reset_mac_header(skb);
  770. skb->ip_summed = CHECKSUM_UNNECESSARY;
  771. skb->pkt_type = PACKET_OTHERHOST;
  772. skb->protocol = htons(ETH_P_802_2);
  773. memset(skb->cb, 0, sizeof(skb->cb));
  774. netif_rx(skb);
  775. }
  776. static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
  777. const u8 *addr)
  778. {
  779. struct sk_buff *skb;
  780. struct hwsim_radiotap_ack_hdr *hdr;
  781. u16 flags;
  782. struct ieee80211_hdr *hdr11;
  783. if (!netif_running(hwsim_mon))
  784. return;
  785. skb = dev_alloc_skb(100);
  786. if (skb == NULL)
  787. return;
  788. hdr = skb_put(skb, sizeof(*hdr));
  789. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  790. hdr->hdr.it_pad = 0;
  791. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  792. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  793. (1 << IEEE80211_RADIOTAP_CHANNEL));
  794. hdr->rt_flags = 0;
  795. hdr->pad = 0;
  796. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  797. flags = IEEE80211_CHAN_2GHZ;
  798. hdr->rt_chbitmask = cpu_to_le16(flags);
  799. hdr11 = skb_put(skb, 10);
  800. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  801. IEEE80211_STYPE_ACK);
  802. hdr11->duration_id = cpu_to_le16(0);
  803. memcpy(hdr11->addr1, addr, ETH_ALEN);
  804. skb->dev = hwsim_mon;
  805. skb_reset_mac_header(skb);
  806. skb->ip_summed = CHECKSUM_UNNECESSARY;
  807. skb->pkt_type = PACKET_OTHERHOST;
  808. skb->protocol = htons(ETH_P_802_2);
  809. memset(skb->cb, 0, sizeof(skb->cb));
  810. netif_rx(skb);
  811. }
  812. struct mac80211_hwsim_addr_match_data {
  813. u8 addr[ETH_ALEN];
  814. bool ret;
  815. };
  816. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  817. struct ieee80211_vif *vif)
  818. {
  819. struct mac80211_hwsim_addr_match_data *md = data;
  820. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  821. md->ret = true;
  822. }
  823. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  824. const u8 *addr)
  825. {
  826. struct mac80211_hwsim_addr_match_data md = {
  827. .ret = false,
  828. };
  829. if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
  830. return true;
  831. memcpy(md.addr, addr, ETH_ALEN);
  832. ieee80211_iterate_active_interfaces_atomic(data->hw,
  833. IEEE80211_IFACE_ITER_NORMAL,
  834. mac80211_hwsim_addr_iter,
  835. &md);
  836. return md.ret;
  837. }
  838. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  839. struct sk_buff *skb)
  840. {
  841. switch (data->ps) {
  842. case PS_DISABLED:
  843. return true;
  844. case PS_ENABLED:
  845. return false;
  846. case PS_AUTO_POLL:
  847. /* TODO: accept (some) Beacons by default and other frames only
  848. * if pending PS-Poll has been sent */
  849. return true;
  850. case PS_MANUAL_POLL:
  851. /* Allow unicast frames to own address if there is a pending
  852. * PS-Poll */
  853. if (data->ps_poll_pending &&
  854. mac80211_hwsim_addr_match(data, skb->data + 4)) {
  855. data->ps_poll_pending = false;
  856. return true;
  857. }
  858. return false;
  859. }
  860. return true;
  861. }
  862. static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
  863. struct sk_buff *skb, int portid)
  864. {
  865. struct net *net;
  866. bool found = false;
  867. int res = -ENOENT;
  868. rcu_read_lock();
  869. for_each_net_rcu(net) {
  870. if (data->netgroup == hwsim_net_get_netgroup(net)) {
  871. res = genlmsg_unicast(net, skb, portid);
  872. found = true;
  873. break;
  874. }
  875. }
  876. rcu_read_unlock();
  877. if (!found)
  878. nlmsg_free(skb);
  879. return res;
  880. }
  881. static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
  882. {
  883. u16 result = 0;
  884. if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  885. result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
  886. if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  887. result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
  888. if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  889. result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
  890. if (rate->flags & IEEE80211_TX_RC_MCS)
  891. result |= MAC80211_HWSIM_TX_RC_MCS;
  892. if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  893. result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
  894. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  895. result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
  896. if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
  897. result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
  898. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  899. result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
  900. if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
  901. result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
  902. if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  903. result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
  904. if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  905. result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
  906. return result;
  907. }
  908. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  909. struct sk_buff *my_skb,
  910. int dst_portid)
  911. {
  912. struct sk_buff *skb;
  913. struct mac80211_hwsim_data *data = hw->priv;
  914. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  915. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  916. void *msg_head;
  917. unsigned int hwsim_flags = 0;
  918. int i;
  919. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  920. struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
  921. uintptr_t cookie;
  922. if (data->ps != PS_DISABLED)
  923. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  924. /* If the queue contains MAX_QUEUE skb's drop some */
  925. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  926. /* Droping until WARN_QUEUE level */
  927. while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
  928. ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
  929. data->tx_dropped++;
  930. }
  931. }
  932. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  933. if (skb == NULL)
  934. goto nla_put_failure;
  935. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  936. HWSIM_CMD_FRAME);
  937. if (msg_head == NULL) {
  938. pr_debug("mac80211_hwsim: problem with msg_head\n");
  939. goto nla_put_failure;
  940. }
  941. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  942. ETH_ALEN, data->addresses[1].addr))
  943. goto nla_put_failure;
  944. /* We get the skb->data */
  945. if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
  946. goto nla_put_failure;
  947. /* We get the flags for this transmission, and we translate them to
  948. wmediumd flags */
  949. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  950. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  951. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  952. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  953. if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
  954. goto nla_put_failure;
  955. if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
  956. goto nla_put_failure;
  957. /* We get the tx control (rate and retries) info*/
  958. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  959. tx_attempts[i].idx = info->status.rates[i].idx;
  960. tx_attempts_flags[i].idx = info->status.rates[i].idx;
  961. tx_attempts[i].count = info->status.rates[i].count;
  962. tx_attempts_flags[i].flags =
  963. trans_tx_rate_flags_ieee2hwsim(
  964. &info->status.rates[i]);
  965. }
  966. if (nla_put(skb, HWSIM_ATTR_TX_INFO,
  967. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  968. tx_attempts))
  969. goto nla_put_failure;
  970. if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
  971. sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
  972. tx_attempts_flags))
  973. goto nla_put_failure;
  974. /* We create a cookie to identify this skb */
  975. data->pending_cookie++;
  976. cookie = data->pending_cookie;
  977. info->rate_driver_data[0] = (void *)cookie;
  978. if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
  979. goto nla_put_failure;
  980. genlmsg_end(skb, msg_head);
  981. if (hwsim_unicast_netgroup(data, skb, dst_portid))
  982. goto err_free_txskb;
  983. /* Enqueue the packet */
  984. skb_queue_tail(&data->pending, my_skb);
  985. data->tx_pkts++;
  986. data->tx_bytes += my_skb->len;
  987. return;
  988. nla_put_failure:
  989. nlmsg_free(skb);
  990. err_free_txskb:
  991. pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
  992. ieee80211_free_txskb(hw, my_skb);
  993. data->tx_failed++;
  994. }
  995. static bool hwsim_chans_compat(struct ieee80211_channel *c1,
  996. struct ieee80211_channel *c2)
  997. {
  998. if (!c1 || !c2)
  999. return false;
  1000. return c1->center_freq == c2->center_freq;
  1001. }
  1002. struct tx_iter_data {
  1003. struct ieee80211_channel *channel;
  1004. bool receive;
  1005. };
  1006. static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
  1007. struct ieee80211_vif *vif)
  1008. {
  1009. struct tx_iter_data *data = _data;
  1010. if (!vif->chanctx_conf)
  1011. return;
  1012. if (!hwsim_chans_compat(data->channel,
  1013. rcu_dereference(vif->chanctx_conf)->def.chan))
  1014. return;
  1015. data->receive = true;
  1016. }
  1017. static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
  1018. {
  1019. /*
  1020. * To enable this code, #define the HWSIM_RADIOTAP_OUI,
  1021. * e.g. like this:
  1022. * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
  1023. * (but you should use a valid OUI, not that)
  1024. *
  1025. * If anyone wants to 'donate' a radiotap OUI/subns code
  1026. * please send a patch removing this #ifdef and changing
  1027. * the values accordingly.
  1028. */
  1029. #ifdef HWSIM_RADIOTAP_OUI
  1030. struct ieee80211_vendor_radiotap *rtap;
  1031. /*
  1032. * Note that this code requires the headroom in the SKB
  1033. * that was allocated earlier.
  1034. */
  1035. rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
  1036. rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
  1037. rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
  1038. rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
  1039. rtap->subns = 127;
  1040. /*
  1041. * Radiotap vendor namespaces can (and should) also be
  1042. * split into fields by using the standard radiotap
  1043. * presence bitmap mechanism. Use just BIT(0) here for
  1044. * the presence bitmap.
  1045. */
  1046. rtap->present = BIT(0);
  1047. /* We have 8 bytes of (dummy) data */
  1048. rtap->len = 8;
  1049. /* For testing, also require it to be aligned */
  1050. rtap->align = 8;
  1051. /* And also test that padding works, 4 bytes */
  1052. rtap->pad = 4;
  1053. /* push the data */
  1054. memcpy(rtap->data, "ABCDEFGH", 8);
  1055. /* make sure to clear padding, mac80211 doesn't */
  1056. memset(rtap->data + 8, 0, 4);
  1057. IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
  1058. #endif
  1059. }
  1060. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  1061. struct sk_buff *skb,
  1062. struct ieee80211_channel *chan)
  1063. {
  1064. struct mac80211_hwsim_data *data = hw->priv, *data2;
  1065. bool ack = false;
  1066. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1067. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1068. struct ieee80211_rx_status rx_status;
  1069. u64 now;
  1070. memset(&rx_status, 0, sizeof(rx_status));
  1071. rx_status.flag |= RX_FLAG_MACTIME_START;
  1072. rx_status.freq = chan->center_freq;
  1073. rx_status.band = chan->band;
  1074. if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
  1075. rx_status.rate_idx =
  1076. ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
  1077. rx_status.nss =
  1078. ieee80211_rate_get_vht_nss(&info->control.rates[0]);
  1079. rx_status.encoding = RX_ENC_VHT;
  1080. } else {
  1081. rx_status.rate_idx = info->control.rates[0].idx;
  1082. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  1083. rx_status.encoding = RX_ENC_HT;
  1084. }
  1085. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1086. rx_status.bw = RATE_INFO_BW_40;
  1087. else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  1088. rx_status.bw = RATE_INFO_BW_80;
  1089. else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  1090. rx_status.bw = RATE_INFO_BW_160;
  1091. else
  1092. rx_status.bw = RATE_INFO_BW_20;
  1093. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  1094. rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
  1095. /* TODO: simulate real signal strength (and optional packet loss) */
  1096. rx_status.signal = -50;
  1097. if (info->control.vif)
  1098. rx_status.signal += info->control.vif->bss_conf.txpower;
  1099. if (data->ps != PS_DISABLED)
  1100. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1101. /* release the skb's source info */
  1102. skb_orphan(skb);
  1103. skb_dst_drop(skb);
  1104. skb->mark = 0;
  1105. secpath_reset(skb);
  1106. nf_reset(skb);
  1107. /*
  1108. * Get absolute mactime here so all HWs RX at the "same time", and
  1109. * absolute TX time for beacon mactime so the timestamp matches.
  1110. * Giving beacons a different mactime than non-beacons looks messy, but
  1111. * it helps the Toffset be exact and a ~10us mactime discrepancy
  1112. * probably doesn't really matter.
  1113. */
  1114. if (ieee80211_is_beacon(hdr->frame_control) ||
  1115. ieee80211_is_probe_resp(hdr->frame_control))
  1116. now = data->abs_bcn_ts;
  1117. else
  1118. now = mac80211_hwsim_get_tsf_raw();
  1119. /* Copy skb to all enabled radios that are on the current frequency */
  1120. spin_lock(&hwsim_radio_lock);
  1121. list_for_each_entry(data2, &hwsim_radios, list) {
  1122. struct sk_buff *nskb;
  1123. struct tx_iter_data tx_iter_data = {
  1124. .receive = false,
  1125. .channel = chan,
  1126. };
  1127. if (data == data2)
  1128. continue;
  1129. if (!data2->started || (data2->idle && !data2->tmp_chan) ||
  1130. !hwsim_ps_rx_ok(data2, skb))
  1131. continue;
  1132. if (!(data->group & data2->group))
  1133. continue;
  1134. if (data->netgroup != data2->netgroup)
  1135. continue;
  1136. if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
  1137. !hwsim_chans_compat(chan, data2->channel)) {
  1138. ieee80211_iterate_active_interfaces_atomic(
  1139. data2->hw, IEEE80211_IFACE_ITER_NORMAL,
  1140. mac80211_hwsim_tx_iter, &tx_iter_data);
  1141. if (!tx_iter_data.receive)
  1142. continue;
  1143. }
  1144. /*
  1145. * reserve some space for our vendor and the normal
  1146. * radiotap header, since we're copying anyway
  1147. */
  1148. if (skb->len < PAGE_SIZE && paged_rx) {
  1149. struct page *page = alloc_page(GFP_ATOMIC);
  1150. if (!page)
  1151. continue;
  1152. nskb = dev_alloc_skb(128);
  1153. if (!nskb) {
  1154. __free_page(page);
  1155. continue;
  1156. }
  1157. memcpy(page_address(page), skb->data, skb->len);
  1158. skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
  1159. } else {
  1160. nskb = skb_copy(skb, GFP_ATOMIC);
  1161. if (!nskb)
  1162. continue;
  1163. }
  1164. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  1165. ack = true;
  1166. rx_status.mactime = now + data2->tsf_offset;
  1167. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  1168. mac80211_hwsim_add_vendor_rtap(nskb);
  1169. data2->rx_pkts++;
  1170. data2->rx_bytes += nskb->len;
  1171. ieee80211_rx_irqsafe(data2->hw, nskb);
  1172. }
  1173. spin_unlock(&hwsim_radio_lock);
  1174. return ack;
  1175. }
  1176. static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
  1177. struct ieee80211_tx_control *control,
  1178. struct sk_buff *skb)
  1179. {
  1180. struct mac80211_hwsim_data *data = hw->priv;
  1181. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  1182. struct ieee80211_hdr *hdr = (void *)skb->data;
  1183. struct ieee80211_chanctx_conf *chanctx_conf;
  1184. struct ieee80211_channel *channel;
  1185. bool ack;
  1186. u32 _portid;
  1187. if (WARN_ON(skb->len < 10)) {
  1188. /* Should not happen; just a sanity check for addr1 use */
  1189. ieee80211_free_txskb(hw, skb);
  1190. return;
  1191. }
  1192. if (!data->use_chanctx) {
  1193. channel = data->channel;
  1194. } else if (txi->hw_queue == 4) {
  1195. channel = data->tmp_chan;
  1196. } else {
  1197. chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
  1198. if (chanctx_conf)
  1199. channel = chanctx_conf->def.chan;
  1200. else
  1201. channel = NULL;
  1202. }
  1203. if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
  1204. ieee80211_free_txskb(hw, skb);
  1205. return;
  1206. }
  1207. if (data->idle && !data->tmp_chan) {
  1208. wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
  1209. ieee80211_free_txskb(hw, skb);
  1210. return;
  1211. }
  1212. if (txi->control.vif)
  1213. hwsim_check_magic(txi->control.vif);
  1214. if (control->sta)
  1215. hwsim_check_sta_magic(control->sta);
  1216. if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
  1217. ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
  1218. txi->control.rates,
  1219. ARRAY_SIZE(txi->control.rates));
  1220. if (skb->len >= 24 + 8 &&
  1221. ieee80211_is_probe_resp(hdr->frame_control)) {
  1222. /* fake header transmission time */
  1223. struct ieee80211_mgmt *mgmt;
  1224. struct ieee80211_rate *txrate;
  1225. u64 ts;
  1226. mgmt = (struct ieee80211_mgmt *)skb->data;
  1227. txrate = ieee80211_get_tx_rate(hw, txi);
  1228. ts = mac80211_hwsim_get_tsf_raw();
  1229. mgmt->u.probe_resp.timestamp =
  1230. cpu_to_le64(ts + data->tsf_offset +
  1231. 24 * 8 * 10 / txrate->bitrate);
  1232. }
  1233. mac80211_hwsim_monitor_rx(hw, skb, channel);
  1234. /* wmediumd mode check */
  1235. _portid = READ_ONCE(data->wmediumd);
  1236. if (_portid)
  1237. return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
  1238. /* NO wmediumd detected, perfect medium simulation */
  1239. data->tx_pkts++;
  1240. data->tx_bytes += skb->len;
  1241. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
  1242. if (ack && skb->len >= 16)
  1243. mac80211_hwsim_monitor_ack(channel, hdr->addr2);
  1244. ieee80211_tx_info_clear_status(txi);
  1245. /* frame was transmitted at most favorable rate at first attempt */
  1246. txi->control.rates[0].count = 1;
  1247. txi->control.rates[1].idx = -1;
  1248. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  1249. txi->flags |= IEEE80211_TX_STAT_ACK;
  1250. ieee80211_tx_status_irqsafe(hw, skb);
  1251. }
  1252. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  1253. {
  1254. struct mac80211_hwsim_data *data = hw->priv;
  1255. wiphy_dbg(hw->wiphy, "%s\n", __func__);
  1256. data->started = true;
  1257. return 0;
  1258. }
  1259. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  1260. {
  1261. struct mac80211_hwsim_data *data = hw->priv;
  1262. data->started = false;
  1263. tasklet_hrtimer_cancel(&data->beacon_timer);
  1264. wiphy_dbg(hw->wiphy, "%s\n", __func__);
  1265. }
  1266. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  1267. struct ieee80211_vif *vif)
  1268. {
  1269. wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  1270. __func__, ieee80211_vif_type_p2p(vif),
  1271. vif->addr);
  1272. hwsim_set_magic(vif);
  1273. vif->cab_queue = 0;
  1274. vif->hw_queue[IEEE80211_AC_VO] = 0;
  1275. vif->hw_queue[IEEE80211_AC_VI] = 1;
  1276. vif->hw_queue[IEEE80211_AC_BE] = 2;
  1277. vif->hw_queue[IEEE80211_AC_BK] = 3;
  1278. return 0;
  1279. }
  1280. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  1281. struct ieee80211_vif *vif,
  1282. enum nl80211_iftype newtype,
  1283. bool newp2p)
  1284. {
  1285. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1286. wiphy_dbg(hw->wiphy,
  1287. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  1288. __func__, ieee80211_vif_type_p2p(vif),
  1289. newtype, vif->addr);
  1290. hwsim_check_magic(vif);
  1291. /*
  1292. * interface may change from non-AP to AP in
  1293. * which case this needs to be set up again
  1294. */
  1295. vif->cab_queue = 0;
  1296. return 0;
  1297. }
  1298. static void mac80211_hwsim_remove_interface(
  1299. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1300. {
  1301. wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  1302. __func__, ieee80211_vif_type_p2p(vif),
  1303. vif->addr);
  1304. hwsim_check_magic(vif);
  1305. hwsim_clear_magic(vif);
  1306. }
  1307. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  1308. struct sk_buff *skb,
  1309. struct ieee80211_channel *chan)
  1310. {
  1311. struct mac80211_hwsim_data *data = hw->priv;
  1312. u32 _pid = READ_ONCE(data->wmediumd);
  1313. if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
  1314. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  1315. ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
  1316. txi->control.rates,
  1317. ARRAY_SIZE(txi->control.rates));
  1318. }
  1319. mac80211_hwsim_monitor_rx(hw, skb, chan);
  1320. if (_pid)
  1321. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  1322. mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
  1323. dev_kfree_skb(skb);
  1324. }
  1325. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  1326. struct ieee80211_vif *vif)
  1327. {
  1328. struct mac80211_hwsim_data *data = arg;
  1329. struct ieee80211_hw *hw = data->hw;
  1330. struct ieee80211_tx_info *info;
  1331. struct ieee80211_rate *txrate;
  1332. struct ieee80211_mgmt *mgmt;
  1333. struct sk_buff *skb;
  1334. hwsim_check_magic(vif);
  1335. if (vif->type != NL80211_IFTYPE_AP &&
  1336. vif->type != NL80211_IFTYPE_MESH_POINT &&
  1337. vif->type != NL80211_IFTYPE_ADHOC)
  1338. return;
  1339. skb = ieee80211_beacon_get(hw, vif);
  1340. if (skb == NULL)
  1341. return;
  1342. info = IEEE80211_SKB_CB(skb);
  1343. if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
  1344. ieee80211_get_tx_rates(vif, NULL, skb,
  1345. info->control.rates,
  1346. ARRAY_SIZE(info->control.rates));
  1347. txrate = ieee80211_get_tx_rate(hw, info);
  1348. mgmt = (struct ieee80211_mgmt *) skb->data;
  1349. /* fake header transmission time */
  1350. data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
  1351. mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
  1352. data->tsf_offset +
  1353. 24 * 8 * 10 / txrate->bitrate);
  1354. mac80211_hwsim_tx_frame(hw, skb,
  1355. rcu_dereference(vif->chanctx_conf)->def.chan);
  1356. if (vif->csa_active && ieee80211_csa_is_complete(vif))
  1357. ieee80211_csa_finish(vif);
  1358. }
  1359. static enum hrtimer_restart
  1360. mac80211_hwsim_beacon(struct hrtimer *timer)
  1361. {
  1362. struct mac80211_hwsim_data *data =
  1363. container_of(timer, struct mac80211_hwsim_data,
  1364. beacon_timer.timer);
  1365. struct ieee80211_hw *hw = data->hw;
  1366. u64 bcn_int = data->beacon_int;
  1367. ktime_t next_bcn;
  1368. if (!data->started)
  1369. goto out;
  1370. ieee80211_iterate_active_interfaces_atomic(
  1371. hw, IEEE80211_IFACE_ITER_NORMAL,
  1372. mac80211_hwsim_beacon_tx, data);
  1373. /* beacon at new TBTT + beacon interval */
  1374. if (data->bcn_delta) {
  1375. bcn_int -= data->bcn_delta;
  1376. data->bcn_delta = 0;
  1377. }
  1378. next_bcn = ktime_add(hrtimer_get_expires(timer),
  1379. ns_to_ktime(bcn_int * 1000));
  1380. tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
  1381. out:
  1382. return HRTIMER_NORESTART;
  1383. }
  1384. static const char * const hwsim_chanwidths[] = {
  1385. [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
  1386. [NL80211_CHAN_WIDTH_20] = "ht20",
  1387. [NL80211_CHAN_WIDTH_40] = "ht40",
  1388. [NL80211_CHAN_WIDTH_80] = "vht80",
  1389. [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
  1390. [NL80211_CHAN_WIDTH_160] = "vht160",
  1391. };
  1392. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  1393. {
  1394. struct mac80211_hwsim_data *data = hw->priv;
  1395. struct ieee80211_conf *conf = &hw->conf;
  1396. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  1397. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  1398. [IEEE80211_SMPS_OFF] = "off",
  1399. [IEEE80211_SMPS_STATIC] = "static",
  1400. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  1401. };
  1402. int idx;
  1403. if (conf->chandef.chan)
  1404. wiphy_dbg(hw->wiphy,
  1405. "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
  1406. __func__,
  1407. conf->chandef.chan->center_freq,
  1408. conf->chandef.center_freq1,
  1409. conf->chandef.center_freq2,
  1410. hwsim_chanwidths[conf->chandef.width],
  1411. !!(conf->flags & IEEE80211_CONF_IDLE),
  1412. !!(conf->flags & IEEE80211_CONF_PS),
  1413. smps_modes[conf->smps_mode]);
  1414. else
  1415. wiphy_dbg(hw->wiphy,
  1416. "%s (freq=0 idle=%d ps=%d smps=%s)\n",
  1417. __func__,
  1418. !!(conf->flags & IEEE80211_CONF_IDLE),
  1419. !!(conf->flags & IEEE80211_CONF_PS),
  1420. smps_modes[conf->smps_mode]);
  1421. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  1422. WARN_ON(conf->chandef.chan && data->use_chanctx);
  1423. mutex_lock(&data->mutex);
  1424. if (data->scanning && conf->chandef.chan) {
  1425. for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
  1426. if (data->survey_data[idx].channel == data->channel) {
  1427. data->survey_data[idx].start =
  1428. data->survey_data[idx].next_start;
  1429. data->survey_data[idx].end = jiffies;
  1430. break;
  1431. }
  1432. }
  1433. data->channel = conf->chandef.chan;
  1434. for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
  1435. if (data->survey_data[idx].channel &&
  1436. data->survey_data[idx].channel != data->channel)
  1437. continue;
  1438. data->survey_data[idx].channel = data->channel;
  1439. data->survey_data[idx].next_start = jiffies;
  1440. break;
  1441. }
  1442. } else {
  1443. data->channel = conf->chandef.chan;
  1444. }
  1445. mutex_unlock(&data->mutex);
  1446. if (!data->started || !data->beacon_int)
  1447. tasklet_hrtimer_cancel(&data->beacon_timer);
  1448. else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
  1449. u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
  1450. u32 bcn_int = data->beacon_int;
  1451. u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
  1452. tasklet_hrtimer_start(&data->beacon_timer,
  1453. ns_to_ktime(until_tbtt * 1000),
  1454. HRTIMER_MODE_REL);
  1455. }
  1456. return 0;
  1457. }
  1458. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  1459. unsigned int changed_flags,
  1460. unsigned int *total_flags,u64 multicast)
  1461. {
  1462. struct mac80211_hwsim_data *data = hw->priv;
  1463. wiphy_dbg(hw->wiphy, "%s\n", __func__);
  1464. data->rx_filter = 0;
  1465. if (*total_flags & FIF_ALLMULTI)
  1466. data->rx_filter |= FIF_ALLMULTI;
  1467. *total_flags = data->rx_filter;
  1468. }
  1469. static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
  1470. struct ieee80211_vif *vif)
  1471. {
  1472. unsigned int *count = data;
  1473. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1474. if (vp->bcn_en)
  1475. (*count)++;
  1476. }
  1477. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  1478. struct ieee80211_vif *vif,
  1479. struct ieee80211_bss_conf *info,
  1480. u32 changed)
  1481. {
  1482. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1483. struct mac80211_hwsim_data *data = hw->priv;
  1484. hwsim_check_magic(vif);
  1485. wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
  1486. __func__, changed, vif->addr);
  1487. if (changed & BSS_CHANGED_BSSID) {
  1488. wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
  1489. __func__, info->bssid);
  1490. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  1491. }
  1492. if (changed & BSS_CHANGED_ASSOC) {
  1493. wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  1494. info->assoc, info->aid);
  1495. vp->assoc = info->assoc;
  1496. vp->aid = info->aid;
  1497. }
  1498. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  1499. wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
  1500. info->enable_beacon, info->beacon_int);
  1501. vp->bcn_en = info->enable_beacon;
  1502. if (data->started &&
  1503. !hrtimer_is_queued(&data->beacon_timer.timer) &&
  1504. info->enable_beacon) {
  1505. u64 tsf, until_tbtt;
  1506. u32 bcn_int;
  1507. data->beacon_int = info->beacon_int * 1024;
  1508. tsf = mac80211_hwsim_get_tsf(hw, vif);
  1509. bcn_int = data->beacon_int;
  1510. until_tbtt = bcn_int - do_div(tsf, bcn_int);
  1511. tasklet_hrtimer_start(&data->beacon_timer,
  1512. ns_to_ktime(until_tbtt * 1000),
  1513. HRTIMER_MODE_REL);
  1514. } else if (!info->enable_beacon) {
  1515. unsigned int count = 0;
  1516. ieee80211_iterate_active_interfaces_atomic(
  1517. data->hw, IEEE80211_IFACE_ITER_NORMAL,
  1518. mac80211_hwsim_bcn_en_iter, &count);
  1519. wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
  1520. count);
  1521. if (count == 0) {
  1522. tasklet_hrtimer_cancel(&data->beacon_timer);
  1523. data->beacon_int = 0;
  1524. }
  1525. }
  1526. }
  1527. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1528. wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
  1529. info->use_cts_prot);
  1530. }
  1531. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1532. wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
  1533. info->use_short_preamble);
  1534. }
  1535. if (changed & BSS_CHANGED_ERP_SLOT) {
  1536. wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  1537. }
  1538. if (changed & BSS_CHANGED_HT) {
  1539. wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
  1540. info->ht_operation_mode);
  1541. }
  1542. if (changed & BSS_CHANGED_BASIC_RATES) {
  1543. wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  1544. (unsigned long long) info->basic_rates);
  1545. }
  1546. if (changed & BSS_CHANGED_TXPOWER)
  1547. wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
  1548. }
  1549. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  1550. struct ieee80211_vif *vif,
  1551. struct ieee80211_sta *sta)
  1552. {
  1553. hwsim_check_magic(vif);
  1554. hwsim_set_sta_magic(sta);
  1555. return 0;
  1556. }
  1557. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  1558. struct ieee80211_vif *vif,
  1559. struct ieee80211_sta *sta)
  1560. {
  1561. hwsim_check_magic(vif);
  1562. hwsim_clear_sta_magic(sta);
  1563. return 0;
  1564. }
  1565. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  1566. struct ieee80211_vif *vif,
  1567. enum sta_notify_cmd cmd,
  1568. struct ieee80211_sta *sta)
  1569. {
  1570. hwsim_check_magic(vif);
  1571. switch (cmd) {
  1572. case STA_NOTIFY_SLEEP:
  1573. case STA_NOTIFY_AWAKE:
  1574. /* TODO: make good use of these flags */
  1575. break;
  1576. default:
  1577. WARN(1, "Invalid sta notify: %d\n", cmd);
  1578. break;
  1579. }
  1580. }
  1581. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  1582. struct ieee80211_sta *sta,
  1583. bool set)
  1584. {
  1585. hwsim_check_sta_magic(sta);
  1586. return 0;
  1587. }
  1588. static int mac80211_hwsim_conf_tx(
  1589. struct ieee80211_hw *hw,
  1590. struct ieee80211_vif *vif, u16 queue,
  1591. const struct ieee80211_tx_queue_params *params)
  1592. {
  1593. wiphy_dbg(hw->wiphy,
  1594. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  1595. __func__, queue,
  1596. params->txop, params->cw_min,
  1597. params->cw_max, params->aifs);
  1598. return 0;
  1599. }
  1600. static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
  1601. struct survey_info *survey)
  1602. {
  1603. struct mac80211_hwsim_data *hwsim = hw->priv;
  1604. if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
  1605. return -ENOENT;
  1606. mutex_lock(&hwsim->mutex);
  1607. survey->channel = hwsim->survey_data[idx].channel;
  1608. if (!survey->channel) {
  1609. mutex_unlock(&hwsim->mutex);
  1610. return -ENOENT;
  1611. }
  1612. /*
  1613. * Magically conjured dummy values --- this is only ok for simulated hardware.
  1614. *
  1615. * A real driver which cannot determine real values noise MUST NOT
  1616. * report any, especially not a magically conjured ones :-)
  1617. */
  1618. survey->filled = SURVEY_INFO_NOISE_DBM |
  1619. SURVEY_INFO_TIME |
  1620. SURVEY_INFO_TIME_BUSY;
  1621. survey->noise = -92;
  1622. survey->time =
  1623. jiffies_to_msecs(hwsim->survey_data[idx].end -
  1624. hwsim->survey_data[idx].start);
  1625. /* report 12.5% of channel time is used */
  1626. survey->time_busy = survey->time/8;
  1627. mutex_unlock(&hwsim->mutex);
  1628. return 0;
  1629. }
  1630. #ifdef CONFIG_NL80211_TESTMODE
  1631. /*
  1632. * This section contains example code for using netlink
  1633. * attributes with the testmode command in nl80211.
  1634. */
  1635. /* These enums need to be kept in sync with userspace */
  1636. enum hwsim_testmode_attr {
  1637. __HWSIM_TM_ATTR_INVALID = 0,
  1638. HWSIM_TM_ATTR_CMD = 1,
  1639. HWSIM_TM_ATTR_PS = 2,
  1640. /* keep last */
  1641. __HWSIM_TM_ATTR_AFTER_LAST,
  1642. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  1643. };
  1644. enum hwsim_testmode_cmd {
  1645. HWSIM_TM_CMD_SET_PS = 0,
  1646. HWSIM_TM_CMD_GET_PS = 1,
  1647. HWSIM_TM_CMD_STOP_QUEUES = 2,
  1648. HWSIM_TM_CMD_WAKE_QUEUES = 3,
  1649. };
  1650. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  1651. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  1652. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  1653. };
  1654. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  1655. struct ieee80211_vif *vif,
  1656. void *data, int len)
  1657. {
  1658. struct mac80211_hwsim_data *hwsim = hw->priv;
  1659. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  1660. struct sk_buff *skb;
  1661. int err, ps;
  1662. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  1663. hwsim_testmode_policy, NULL);
  1664. if (err)
  1665. return err;
  1666. if (!tb[HWSIM_TM_ATTR_CMD])
  1667. return -EINVAL;
  1668. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  1669. case HWSIM_TM_CMD_SET_PS:
  1670. if (!tb[HWSIM_TM_ATTR_PS])
  1671. return -EINVAL;
  1672. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  1673. return hwsim_fops_ps_write(hwsim, ps);
  1674. case HWSIM_TM_CMD_GET_PS:
  1675. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  1676. nla_total_size(sizeof(u32)));
  1677. if (!skb)
  1678. return -ENOMEM;
  1679. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  1680. goto nla_put_failure;
  1681. return cfg80211_testmode_reply(skb);
  1682. case HWSIM_TM_CMD_STOP_QUEUES:
  1683. ieee80211_stop_queues(hw);
  1684. return 0;
  1685. case HWSIM_TM_CMD_WAKE_QUEUES:
  1686. ieee80211_wake_queues(hw);
  1687. return 0;
  1688. default:
  1689. return -EOPNOTSUPP;
  1690. }
  1691. nla_put_failure:
  1692. kfree_skb(skb);
  1693. return -ENOBUFS;
  1694. }
  1695. #endif
  1696. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  1697. struct ieee80211_vif *vif,
  1698. struct ieee80211_ampdu_params *params)
  1699. {
  1700. struct ieee80211_sta *sta = params->sta;
  1701. enum ieee80211_ampdu_mlme_action action = params->action;
  1702. u16 tid = params->tid;
  1703. switch (action) {
  1704. case IEEE80211_AMPDU_TX_START:
  1705. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1706. break;
  1707. case IEEE80211_AMPDU_TX_STOP_CONT:
  1708. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1709. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1710. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1711. break;
  1712. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1713. break;
  1714. case IEEE80211_AMPDU_RX_START:
  1715. case IEEE80211_AMPDU_RX_STOP:
  1716. break;
  1717. default:
  1718. return -EOPNOTSUPP;
  1719. }
  1720. return 0;
  1721. }
  1722. static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
  1723. struct ieee80211_vif *vif,
  1724. u32 queues, bool drop)
  1725. {
  1726. /* Not implemented, queues only on kernel side */
  1727. }
  1728. static void hw_scan_work(struct work_struct *work)
  1729. {
  1730. struct mac80211_hwsim_data *hwsim =
  1731. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  1732. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  1733. int dwell, i;
  1734. mutex_lock(&hwsim->mutex);
  1735. if (hwsim->scan_chan_idx >= req->n_channels) {
  1736. struct cfg80211_scan_info info = {
  1737. .aborted = false,
  1738. };
  1739. wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
  1740. ieee80211_scan_completed(hwsim->hw, &info);
  1741. hwsim->hw_scan_request = NULL;
  1742. hwsim->hw_scan_vif = NULL;
  1743. hwsim->tmp_chan = NULL;
  1744. mutex_unlock(&hwsim->mutex);
  1745. return;
  1746. }
  1747. wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
  1748. req->channels[hwsim->scan_chan_idx]->center_freq);
  1749. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  1750. if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
  1751. IEEE80211_CHAN_RADAR) ||
  1752. !req->n_ssids) {
  1753. dwell = 120;
  1754. } else {
  1755. dwell = 30;
  1756. /* send probes */
  1757. for (i = 0; i < req->n_ssids; i++) {
  1758. struct sk_buff *probe;
  1759. struct ieee80211_mgmt *mgmt;
  1760. probe = ieee80211_probereq_get(hwsim->hw,
  1761. hwsim->scan_addr,
  1762. req->ssids[i].ssid,
  1763. req->ssids[i].ssid_len,
  1764. req->ie_len);
  1765. if (!probe)
  1766. continue;
  1767. mgmt = (struct ieee80211_mgmt *) probe->data;
  1768. memcpy(mgmt->da, req->bssid, ETH_ALEN);
  1769. memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
  1770. if (req->ie_len)
  1771. skb_put_data(probe, req->ie, req->ie_len);
  1772. local_bh_disable();
  1773. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  1774. hwsim->tmp_chan);
  1775. local_bh_enable();
  1776. }
  1777. }
  1778. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  1779. msecs_to_jiffies(dwell));
  1780. hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
  1781. hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
  1782. hwsim->survey_data[hwsim->scan_chan_idx].end =
  1783. jiffies + msecs_to_jiffies(dwell);
  1784. hwsim->scan_chan_idx++;
  1785. mutex_unlock(&hwsim->mutex);
  1786. }
  1787. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  1788. struct ieee80211_vif *vif,
  1789. struct ieee80211_scan_request *hw_req)
  1790. {
  1791. struct mac80211_hwsim_data *hwsim = hw->priv;
  1792. struct cfg80211_scan_request *req = &hw_req->req;
  1793. mutex_lock(&hwsim->mutex);
  1794. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1795. mutex_unlock(&hwsim->mutex);
  1796. return -EBUSY;
  1797. }
  1798. hwsim->hw_scan_request = req;
  1799. hwsim->hw_scan_vif = vif;
  1800. hwsim->scan_chan_idx = 0;
  1801. if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
  1802. get_random_mask_addr(hwsim->scan_addr,
  1803. hw_req->req.mac_addr,
  1804. hw_req->req.mac_addr_mask);
  1805. else
  1806. memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
  1807. memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
  1808. mutex_unlock(&hwsim->mutex);
  1809. wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
  1810. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  1811. return 0;
  1812. }
  1813. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  1814. struct ieee80211_vif *vif)
  1815. {
  1816. struct mac80211_hwsim_data *hwsim = hw->priv;
  1817. struct cfg80211_scan_info info = {
  1818. .aborted = true,
  1819. };
  1820. wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
  1821. cancel_delayed_work_sync(&hwsim->hw_scan);
  1822. mutex_lock(&hwsim->mutex);
  1823. ieee80211_scan_completed(hwsim->hw, &info);
  1824. hwsim->tmp_chan = NULL;
  1825. hwsim->hw_scan_request = NULL;
  1826. hwsim->hw_scan_vif = NULL;
  1827. mutex_unlock(&hwsim->mutex);
  1828. }
  1829. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
  1830. struct ieee80211_vif *vif,
  1831. const u8 *mac_addr)
  1832. {
  1833. struct mac80211_hwsim_data *hwsim = hw->priv;
  1834. mutex_lock(&hwsim->mutex);
  1835. if (hwsim->scanning) {
  1836. pr_debug("two hwsim sw_scans detected!\n");
  1837. goto out;
  1838. }
  1839. pr_debug("hwsim sw_scan request, prepping stuff\n");
  1840. memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
  1841. hwsim->scanning = true;
  1842. memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
  1843. out:
  1844. mutex_unlock(&hwsim->mutex);
  1845. }
  1846. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
  1847. struct ieee80211_vif *vif)
  1848. {
  1849. struct mac80211_hwsim_data *hwsim = hw->priv;
  1850. mutex_lock(&hwsim->mutex);
  1851. pr_debug("hwsim sw_scan_complete\n");
  1852. hwsim->scanning = false;
  1853. eth_zero_addr(hwsim->scan_addr);
  1854. mutex_unlock(&hwsim->mutex);
  1855. }
  1856. static void hw_roc_start(struct work_struct *work)
  1857. {
  1858. struct mac80211_hwsim_data *hwsim =
  1859. container_of(work, struct mac80211_hwsim_data, roc_start.work);
  1860. mutex_lock(&hwsim->mutex);
  1861. wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
  1862. hwsim->tmp_chan = hwsim->roc_chan;
  1863. ieee80211_ready_on_channel(hwsim->hw);
  1864. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
  1865. msecs_to_jiffies(hwsim->roc_duration));
  1866. mutex_unlock(&hwsim->mutex);
  1867. }
  1868. static void hw_roc_done(struct work_struct *work)
  1869. {
  1870. struct mac80211_hwsim_data *hwsim =
  1871. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  1872. mutex_lock(&hwsim->mutex);
  1873. ieee80211_remain_on_channel_expired(hwsim->hw);
  1874. hwsim->tmp_chan = NULL;
  1875. mutex_unlock(&hwsim->mutex);
  1876. wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
  1877. }
  1878. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  1879. struct ieee80211_vif *vif,
  1880. struct ieee80211_channel *chan,
  1881. int duration,
  1882. enum ieee80211_roc_type type)
  1883. {
  1884. struct mac80211_hwsim_data *hwsim = hw->priv;
  1885. mutex_lock(&hwsim->mutex);
  1886. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1887. mutex_unlock(&hwsim->mutex);
  1888. return -EBUSY;
  1889. }
  1890. hwsim->roc_chan = chan;
  1891. hwsim->roc_duration = duration;
  1892. mutex_unlock(&hwsim->mutex);
  1893. wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  1894. chan->center_freq, duration);
  1895. ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
  1896. return 0;
  1897. }
  1898. static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
  1899. {
  1900. struct mac80211_hwsim_data *hwsim = hw->priv;
  1901. cancel_delayed_work_sync(&hwsim->roc_start);
  1902. cancel_delayed_work_sync(&hwsim->roc_done);
  1903. mutex_lock(&hwsim->mutex);
  1904. hwsim->tmp_chan = NULL;
  1905. mutex_unlock(&hwsim->mutex);
  1906. wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
  1907. return 0;
  1908. }
  1909. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  1910. struct ieee80211_chanctx_conf *ctx)
  1911. {
  1912. hwsim_set_chanctx_magic(ctx);
  1913. wiphy_dbg(hw->wiphy,
  1914. "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1915. ctx->def.chan->center_freq, ctx->def.width,
  1916. ctx->def.center_freq1, ctx->def.center_freq2);
  1917. return 0;
  1918. }
  1919. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  1920. struct ieee80211_chanctx_conf *ctx)
  1921. {
  1922. wiphy_dbg(hw->wiphy,
  1923. "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1924. ctx->def.chan->center_freq, ctx->def.width,
  1925. ctx->def.center_freq1, ctx->def.center_freq2);
  1926. hwsim_check_chanctx_magic(ctx);
  1927. hwsim_clear_chanctx_magic(ctx);
  1928. }
  1929. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  1930. struct ieee80211_chanctx_conf *ctx,
  1931. u32 changed)
  1932. {
  1933. hwsim_check_chanctx_magic(ctx);
  1934. wiphy_dbg(hw->wiphy,
  1935. "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1936. ctx->def.chan->center_freq, ctx->def.width,
  1937. ctx->def.center_freq1, ctx->def.center_freq2);
  1938. }
  1939. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  1940. struct ieee80211_vif *vif,
  1941. struct ieee80211_chanctx_conf *ctx)
  1942. {
  1943. hwsim_check_magic(vif);
  1944. hwsim_check_chanctx_magic(ctx);
  1945. return 0;
  1946. }
  1947. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1948. struct ieee80211_vif *vif,
  1949. struct ieee80211_chanctx_conf *ctx)
  1950. {
  1951. hwsim_check_magic(vif);
  1952. hwsim_check_chanctx_magic(ctx);
  1953. }
  1954. static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
  1955. "tx_pkts_nic",
  1956. "tx_bytes_nic",
  1957. "rx_pkts_nic",
  1958. "rx_bytes_nic",
  1959. "d_tx_dropped",
  1960. "d_tx_failed",
  1961. "d_ps_mode",
  1962. "d_group",
  1963. };
  1964. #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
  1965. static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
  1966. struct ieee80211_vif *vif,
  1967. u32 sset, u8 *data)
  1968. {
  1969. if (sset == ETH_SS_STATS)
  1970. memcpy(data, *mac80211_hwsim_gstrings_stats,
  1971. sizeof(mac80211_hwsim_gstrings_stats));
  1972. }
  1973. static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
  1974. struct ieee80211_vif *vif, int sset)
  1975. {
  1976. if (sset == ETH_SS_STATS)
  1977. return MAC80211_HWSIM_SSTATS_LEN;
  1978. return 0;
  1979. }
  1980. static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
  1981. struct ieee80211_vif *vif,
  1982. struct ethtool_stats *stats, u64 *data)
  1983. {
  1984. struct mac80211_hwsim_data *ar = hw->priv;
  1985. int i = 0;
  1986. data[i++] = ar->tx_pkts;
  1987. data[i++] = ar->tx_bytes;
  1988. data[i++] = ar->rx_pkts;
  1989. data[i++] = ar->rx_bytes;
  1990. data[i++] = ar->tx_dropped;
  1991. data[i++] = ar->tx_failed;
  1992. data[i++] = ar->ps;
  1993. data[i++] = ar->group;
  1994. WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
  1995. }
  1996. #define HWSIM_COMMON_OPS \
  1997. .tx = mac80211_hwsim_tx, \
  1998. .start = mac80211_hwsim_start, \
  1999. .stop = mac80211_hwsim_stop, \
  2000. .add_interface = mac80211_hwsim_add_interface, \
  2001. .change_interface = mac80211_hwsim_change_interface, \
  2002. .remove_interface = mac80211_hwsim_remove_interface, \
  2003. .config = mac80211_hwsim_config, \
  2004. .configure_filter = mac80211_hwsim_configure_filter, \
  2005. .bss_info_changed = mac80211_hwsim_bss_info_changed, \
  2006. .sta_add = mac80211_hwsim_sta_add, \
  2007. .sta_remove = mac80211_hwsim_sta_remove, \
  2008. .sta_notify = mac80211_hwsim_sta_notify, \
  2009. .set_tim = mac80211_hwsim_set_tim, \
  2010. .conf_tx = mac80211_hwsim_conf_tx, \
  2011. .get_survey = mac80211_hwsim_get_survey, \
  2012. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
  2013. .ampdu_action = mac80211_hwsim_ampdu_action, \
  2014. .flush = mac80211_hwsim_flush, \
  2015. .get_tsf = mac80211_hwsim_get_tsf, \
  2016. .set_tsf = mac80211_hwsim_set_tsf, \
  2017. .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
  2018. .get_et_stats = mac80211_hwsim_get_et_stats, \
  2019. .get_et_strings = mac80211_hwsim_get_et_strings,
  2020. static const struct ieee80211_ops mac80211_hwsim_ops = {
  2021. HWSIM_COMMON_OPS
  2022. .sw_scan_start = mac80211_hwsim_sw_scan,
  2023. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  2024. };
  2025. static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
  2026. HWSIM_COMMON_OPS
  2027. .hw_scan = mac80211_hwsim_hw_scan,
  2028. .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
  2029. .sw_scan_start = NULL,
  2030. .sw_scan_complete = NULL,
  2031. .remain_on_channel = mac80211_hwsim_roc,
  2032. .cancel_remain_on_channel = mac80211_hwsim_croc,
  2033. .add_chanctx = mac80211_hwsim_add_chanctx,
  2034. .remove_chanctx = mac80211_hwsim_remove_chanctx,
  2035. .change_chanctx = mac80211_hwsim_change_chanctx,
  2036. .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
  2037. .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
  2038. };
  2039. struct hwsim_new_radio_params {
  2040. unsigned int channels;
  2041. const char *reg_alpha2;
  2042. const struct ieee80211_regdomain *regd;
  2043. bool reg_strict;
  2044. bool p2p_device;
  2045. bool use_chanctx;
  2046. bool destroy_on_close;
  2047. const char *hwname;
  2048. bool no_vif;
  2049. const u8 *perm_addr;
  2050. };
  2051. static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
  2052. struct genl_info *info)
  2053. {
  2054. if (info)
  2055. genl_notify(&hwsim_genl_family, mcast_skb, info,
  2056. HWSIM_MCGRP_CONFIG, GFP_KERNEL);
  2057. else
  2058. genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
  2059. HWSIM_MCGRP_CONFIG, GFP_KERNEL);
  2060. }
  2061. static int append_radio_msg(struct sk_buff *skb, int id,
  2062. struct hwsim_new_radio_params *param)
  2063. {
  2064. int ret;
  2065. ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
  2066. if (ret < 0)
  2067. return ret;
  2068. if (param->channels) {
  2069. ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
  2070. if (ret < 0)
  2071. return ret;
  2072. }
  2073. if (param->reg_alpha2) {
  2074. ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
  2075. param->reg_alpha2);
  2076. if (ret < 0)
  2077. return ret;
  2078. }
  2079. if (param->regd) {
  2080. int i;
  2081. for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
  2082. if (hwsim_world_regdom_custom[i] != param->regd)
  2083. continue;
  2084. ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
  2085. if (ret < 0)
  2086. return ret;
  2087. break;
  2088. }
  2089. }
  2090. if (param->reg_strict) {
  2091. ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
  2092. if (ret < 0)
  2093. return ret;
  2094. }
  2095. if (param->p2p_device) {
  2096. ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
  2097. if (ret < 0)
  2098. return ret;
  2099. }
  2100. if (param->use_chanctx) {
  2101. ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
  2102. if (ret < 0)
  2103. return ret;
  2104. }
  2105. if (param->hwname) {
  2106. ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
  2107. strlen(param->hwname), param->hwname);
  2108. if (ret < 0)
  2109. return ret;
  2110. }
  2111. return 0;
  2112. }
  2113. static void hwsim_mcast_new_radio(int id, struct genl_info *info,
  2114. struct hwsim_new_radio_params *param)
  2115. {
  2116. struct sk_buff *mcast_skb;
  2117. void *data;
  2118. mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2119. if (!mcast_skb)
  2120. return;
  2121. data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
  2122. HWSIM_CMD_NEW_RADIO);
  2123. if (!data)
  2124. goto out_err;
  2125. if (append_radio_msg(mcast_skb, id, param) < 0)
  2126. goto out_err;
  2127. genlmsg_end(mcast_skb, data);
  2128. hwsim_mcast_config_msg(mcast_skb, info);
  2129. return;
  2130. out_err:
  2131. nlmsg_free(mcast_skb);
  2132. }
  2133. static const struct ieee80211_sband_iftype_data he_capa_2ghz = {
  2134. /* TODO: should we support other types, e.g., P2P?*/
  2135. .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
  2136. .he_cap = {
  2137. .has_he = true,
  2138. .he_cap_elem = {
  2139. .mac_cap_info[0] =
  2140. IEEE80211_HE_MAC_CAP0_HTC_HE,
  2141. .mac_cap_info[1] =
  2142. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  2143. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_QOS_8,
  2144. .mac_cap_info[2] =
  2145. IEEE80211_HE_MAC_CAP2_BSR |
  2146. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  2147. IEEE80211_HE_MAC_CAP2_ACK_EN,
  2148. .mac_cap_info[3] =
  2149. IEEE80211_HE_MAC_CAP3_GRP_ADDR_MULTI_STA_BA_DL_MU |
  2150. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  2151. IEEE80211_HE_MAC_CAP3_MAX_A_AMPDU_LEN_EXP_VHT_2,
  2152. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
  2153. .phy_cap_info[0] =
  2154. IEEE80211_HE_PHY_CAP0_DUAL_BAND,
  2155. .phy_cap_info[1] =
  2156. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  2157. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  2158. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  2159. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_MAX_NSTS,
  2160. .phy_cap_info[2] =
  2161. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  2162. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  2163. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  2164. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  2165. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  2166. /* Leave all the other PHY capability bytes unset, as
  2167. * DCM, beam forming, RU and PPE threshold information
  2168. * are not supported
  2169. */
  2170. },
  2171. .he_mcs_nss_supp = {
  2172. .rx_mcs_80 = cpu_to_le16(0xfffa),
  2173. .tx_mcs_80 = cpu_to_le16(0xfffa),
  2174. .rx_mcs_160 = cpu_to_le16(0xffff),
  2175. .tx_mcs_160 = cpu_to_le16(0xffff),
  2176. .rx_mcs_80p80 = cpu_to_le16(0xffff),
  2177. .tx_mcs_80p80 = cpu_to_le16(0xffff),
  2178. },
  2179. },
  2180. };
  2181. static const struct ieee80211_sband_iftype_data he_capa_5ghz = {
  2182. /* TODO: should we support other types, e.g., P2P?*/
  2183. .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
  2184. .he_cap = {
  2185. .has_he = true,
  2186. .he_cap_elem = {
  2187. .mac_cap_info[0] =
  2188. IEEE80211_HE_MAC_CAP0_HTC_HE,
  2189. .mac_cap_info[1] =
  2190. IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
  2191. IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_QOS_8,
  2192. .mac_cap_info[2] =
  2193. IEEE80211_HE_MAC_CAP2_BSR |
  2194. IEEE80211_HE_MAC_CAP2_MU_CASCADING |
  2195. IEEE80211_HE_MAC_CAP2_ACK_EN,
  2196. .mac_cap_info[3] =
  2197. IEEE80211_HE_MAC_CAP3_GRP_ADDR_MULTI_STA_BA_DL_MU |
  2198. IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  2199. IEEE80211_HE_MAC_CAP3_MAX_A_AMPDU_LEN_EXP_VHT_2,
  2200. .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
  2201. .phy_cap_info[0] =
  2202. IEEE80211_HE_PHY_CAP0_DUAL_BAND |
  2203. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
  2204. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
  2205. IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
  2206. .phy_cap_info[1] =
  2207. IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
  2208. IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
  2209. IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
  2210. IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_MAX_NSTS,
  2211. .phy_cap_info[2] =
  2212. IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  2213. IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  2214. IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  2215. IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  2216. IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
  2217. /* Leave all the other PHY capability bytes unset, as
  2218. * DCM, beam forming, RU and PPE threshold information
  2219. * are not supported
  2220. */
  2221. },
  2222. .he_mcs_nss_supp = {
  2223. .rx_mcs_80 = cpu_to_le16(0xfffa),
  2224. .tx_mcs_80 = cpu_to_le16(0xfffa),
  2225. .rx_mcs_160 = cpu_to_le16(0xfffa),
  2226. .tx_mcs_160 = cpu_to_le16(0xfffa),
  2227. .rx_mcs_80p80 = cpu_to_le16(0xfffa),
  2228. .tx_mcs_80p80 = cpu_to_le16(0xfffa),
  2229. },
  2230. },
  2231. };
  2232. static void mac80211_hswim_he_capab(struct ieee80211_supported_band *sband)
  2233. {
  2234. if (sband->band == NL80211_BAND_2GHZ)
  2235. sband->iftype_data =
  2236. (struct ieee80211_sband_iftype_data *)&he_capa_2ghz;
  2237. else if (sband->band == NL80211_BAND_5GHZ)
  2238. sband->iftype_data =
  2239. (struct ieee80211_sband_iftype_data *)&he_capa_5ghz;
  2240. else
  2241. return;
  2242. sband->n_iftype_data = 1;
  2243. }
  2244. static int mac80211_hwsim_new_radio(struct genl_info *info,
  2245. struct hwsim_new_radio_params *param)
  2246. {
  2247. int err;
  2248. u8 addr[ETH_ALEN];
  2249. struct mac80211_hwsim_data *data;
  2250. struct ieee80211_hw *hw;
  2251. enum nl80211_band band;
  2252. const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
  2253. struct net *net;
  2254. int idx;
  2255. if (WARN_ON(param->channels > 1 && !param->use_chanctx))
  2256. return -EINVAL;
  2257. spin_lock_bh(&hwsim_radio_lock);
  2258. idx = hwsim_radio_idx++;
  2259. spin_unlock_bh(&hwsim_radio_lock);
  2260. if (param->use_chanctx)
  2261. ops = &mac80211_hwsim_mchan_ops;
  2262. hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
  2263. if (!hw) {
  2264. pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
  2265. err = -ENOMEM;
  2266. goto failed;
  2267. }
  2268. /* ieee80211_alloc_hw_nm may have used a default name */
  2269. param->hwname = wiphy_name(hw->wiphy);
  2270. if (info)
  2271. net = genl_info_net(info);
  2272. else
  2273. net = &init_net;
  2274. wiphy_net_set(hw->wiphy, net);
  2275. data = hw->priv;
  2276. data->hw = hw;
  2277. data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
  2278. if (IS_ERR(data->dev)) {
  2279. printk(KERN_DEBUG
  2280. "mac80211_hwsim: device_create failed (%ld)\n",
  2281. PTR_ERR(data->dev));
  2282. err = -ENOMEM;
  2283. goto failed_drvdata;
  2284. }
  2285. data->dev->driver = &mac80211_hwsim_driver.driver;
  2286. err = device_bind_driver(data->dev);
  2287. if (err != 0) {
  2288. pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
  2289. err);
  2290. goto failed_bind;
  2291. }
  2292. skb_queue_head_init(&data->pending);
  2293. SET_IEEE80211_DEV(hw, data->dev);
  2294. if (!param->perm_addr) {
  2295. eth_zero_addr(addr);
  2296. addr[0] = 0x02;
  2297. addr[3] = idx >> 8;
  2298. addr[4] = idx;
  2299. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  2300. /* Why need here second address ? */
  2301. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  2302. data->addresses[1].addr[0] |= 0x40;
  2303. hw->wiphy->n_addresses = 2;
  2304. hw->wiphy->addresses = data->addresses;
  2305. /* possible address clash is checked at hash table insertion */
  2306. } else {
  2307. memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
  2308. /* compatibility with automatically generated mac addr */
  2309. memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
  2310. hw->wiphy->n_addresses = 2;
  2311. hw->wiphy->addresses = data->addresses;
  2312. }
  2313. data->channels = param->channels;
  2314. data->use_chanctx = param->use_chanctx;
  2315. data->idx = idx;
  2316. data->destroy_on_close = param->destroy_on_close;
  2317. if (info)
  2318. data->portid = info->snd_portid;
  2319. if (data->use_chanctx) {
  2320. hw->wiphy->max_scan_ssids = 255;
  2321. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  2322. hw->wiphy->max_remain_on_channel_duration = 1000;
  2323. hw->wiphy->iface_combinations = &data->if_combination;
  2324. if (param->p2p_device)
  2325. data->if_combination = hwsim_if_comb_p2p_dev[0];
  2326. else
  2327. data->if_combination = hwsim_if_comb[0];
  2328. hw->wiphy->n_iface_combinations = 1;
  2329. /* For channels > 1 DFS is not allowed */
  2330. data->if_combination.radar_detect_widths = 0;
  2331. data->if_combination.num_different_channels = data->channels;
  2332. } else if (param->p2p_device) {
  2333. hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
  2334. hw->wiphy->n_iface_combinations =
  2335. ARRAY_SIZE(hwsim_if_comb_p2p_dev);
  2336. } else {
  2337. hw->wiphy->iface_combinations = hwsim_if_comb;
  2338. hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
  2339. }
  2340. INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
  2341. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  2342. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  2343. hw->queues = 5;
  2344. hw->offchannel_tx_hw_queue = 4;
  2345. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2346. BIT(NL80211_IFTYPE_AP) |
  2347. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  2348. BIT(NL80211_IFTYPE_P2P_GO) |
  2349. BIT(NL80211_IFTYPE_ADHOC) |
  2350. BIT(NL80211_IFTYPE_MESH_POINT);
  2351. if (param->p2p_device)
  2352. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
  2353. ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
  2354. ieee80211_hw_set(hw, CHANCTX_STA_CSA);
  2355. ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
  2356. ieee80211_hw_set(hw, QUEUE_CONTROL);
  2357. ieee80211_hw_set(hw, WANT_MONITOR_VIF);
  2358. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  2359. ieee80211_hw_set(hw, MFP_CAPABLE);
  2360. ieee80211_hw_set(hw, SIGNAL_DBM);
  2361. ieee80211_hw_set(hw, SUPPORTS_PS);
  2362. ieee80211_hw_set(hw, TDLS_WIDER_BW);
  2363. if (rctbl)
  2364. ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
  2365. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  2366. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
  2367. WIPHY_FLAG_AP_UAPSD |
  2368. WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  2369. hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
  2370. NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
  2371. NL80211_FEATURE_STATIC_SMPS |
  2372. NL80211_FEATURE_DYNAMIC_SMPS |
  2373. NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
  2374. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
  2375. /* ask mac80211 to reserve space for magic */
  2376. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  2377. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  2378. hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
  2379. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  2380. sizeof(hwsim_channels_2ghz));
  2381. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  2382. sizeof(hwsim_channels_5ghz));
  2383. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  2384. for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
  2385. struct ieee80211_supported_band *sband = &data->bands[band];
  2386. sband->band = band;
  2387. switch (band) {
  2388. case NL80211_BAND_2GHZ:
  2389. sband->channels = data->channels_2ghz;
  2390. sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
  2391. sband->bitrates = data->rates;
  2392. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  2393. break;
  2394. case NL80211_BAND_5GHZ:
  2395. sband->channels = data->channels_5ghz;
  2396. sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
  2397. sband->bitrates = data->rates + 4;
  2398. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  2399. sband->vht_cap.vht_supported = true;
  2400. sband->vht_cap.cap =
  2401. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  2402. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
  2403. IEEE80211_VHT_CAP_RXLDPC |
  2404. IEEE80211_VHT_CAP_SHORT_GI_80 |
  2405. IEEE80211_VHT_CAP_SHORT_GI_160 |
  2406. IEEE80211_VHT_CAP_TXSTBC |
  2407. IEEE80211_VHT_CAP_RXSTBC_4 |
  2408. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  2409. sband->vht_cap.vht_mcs.rx_mcs_map =
  2410. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
  2411. IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
  2412. IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
  2413. IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
  2414. IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
  2415. IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
  2416. IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
  2417. IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
  2418. sband->vht_cap.vht_mcs.tx_mcs_map =
  2419. sband->vht_cap.vht_mcs.rx_mcs_map;
  2420. break;
  2421. default:
  2422. continue;
  2423. }
  2424. sband->ht_cap.ht_supported = true;
  2425. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  2426. IEEE80211_HT_CAP_GRN_FLD |
  2427. IEEE80211_HT_CAP_SGI_20 |
  2428. IEEE80211_HT_CAP_SGI_40 |
  2429. IEEE80211_HT_CAP_DSSSCCK40;
  2430. sband->ht_cap.ampdu_factor = 0x3;
  2431. sband->ht_cap.ampdu_density = 0x6;
  2432. memset(&sband->ht_cap.mcs, 0,
  2433. sizeof(sband->ht_cap.mcs));
  2434. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  2435. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  2436. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  2437. mac80211_hswim_he_capab(sband);
  2438. hw->wiphy->bands[band] = sband;
  2439. }
  2440. /* By default all radios belong to the first group */
  2441. data->group = 1;
  2442. mutex_init(&data->mutex);
  2443. data->netgroup = hwsim_net_get_netgroup(net);
  2444. data->wmediumd = hwsim_net_get_wmediumd(net);
  2445. /* Enable frame retransmissions for lossy channels */
  2446. hw->max_rates = 4;
  2447. hw->max_rate_tries = 11;
  2448. hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
  2449. hw->wiphy->n_vendor_commands =
  2450. ARRAY_SIZE(mac80211_hwsim_vendor_commands);
  2451. hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
  2452. hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
  2453. if (param->reg_strict)
  2454. hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
  2455. if (param->regd) {
  2456. data->regd = param->regd;
  2457. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
  2458. wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
  2459. /* give the regulatory workqueue a chance to run */
  2460. schedule_timeout_interruptible(1);
  2461. }
  2462. if (param->no_vif)
  2463. ieee80211_hw_set(hw, NO_AUTO_VIF);
  2464. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  2465. tasklet_hrtimer_init(&data->beacon_timer,
  2466. mac80211_hwsim_beacon,
  2467. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  2468. err = ieee80211_register_hw(hw);
  2469. if (err < 0) {
  2470. pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
  2471. err);
  2472. goto failed_hw;
  2473. }
  2474. wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
  2475. if (param->reg_alpha2) {
  2476. data->alpha2[0] = param->reg_alpha2[0];
  2477. data->alpha2[1] = param->reg_alpha2[1];
  2478. regulatory_hint(hw->wiphy, param->reg_alpha2);
  2479. }
  2480. data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
  2481. debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
  2482. debugfs_create_file("group", 0666, data->debugfs, data,
  2483. &hwsim_fops_group);
  2484. if (!data->use_chanctx)
  2485. debugfs_create_file("dfs_simulate_radar", 0222,
  2486. data->debugfs,
  2487. data, &hwsim_simulate_radar);
  2488. spin_lock_bh(&hwsim_radio_lock);
  2489. err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
  2490. hwsim_rht_params);
  2491. if (err < 0) {
  2492. if (info) {
  2493. GENL_SET_ERR_MSG(info, "perm addr already present");
  2494. NL_SET_BAD_ATTR(info->extack,
  2495. info->attrs[HWSIM_ATTR_PERM_ADDR]);
  2496. }
  2497. spin_unlock_bh(&hwsim_radio_lock);
  2498. goto failed_final_insert;
  2499. }
  2500. list_add_tail(&data->list, &hwsim_radios);
  2501. hwsim_radios_generation++;
  2502. spin_unlock_bh(&hwsim_radio_lock);
  2503. hwsim_mcast_new_radio(idx, info, param);
  2504. return idx;
  2505. failed_final_insert:
  2506. debugfs_remove_recursive(data->debugfs);
  2507. ieee80211_unregister_hw(data->hw);
  2508. failed_hw:
  2509. device_release_driver(data->dev);
  2510. failed_bind:
  2511. device_unregister(data->dev);
  2512. failed_drvdata:
  2513. ieee80211_free_hw(hw);
  2514. failed:
  2515. return err;
  2516. }
  2517. static void hwsim_mcast_del_radio(int id, const char *hwname,
  2518. struct genl_info *info)
  2519. {
  2520. struct sk_buff *skb;
  2521. void *data;
  2522. int ret;
  2523. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2524. if (!skb)
  2525. return;
  2526. data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  2527. HWSIM_CMD_DEL_RADIO);
  2528. if (!data)
  2529. goto error;
  2530. ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
  2531. if (ret < 0)
  2532. goto error;
  2533. ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
  2534. hwname);
  2535. if (ret < 0)
  2536. goto error;
  2537. genlmsg_end(skb, data);
  2538. hwsim_mcast_config_msg(skb, info);
  2539. return;
  2540. error:
  2541. nlmsg_free(skb);
  2542. }
  2543. static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
  2544. const char *hwname,
  2545. struct genl_info *info)
  2546. {
  2547. hwsim_mcast_del_radio(data->idx, hwname, info);
  2548. debugfs_remove_recursive(data->debugfs);
  2549. ieee80211_unregister_hw(data->hw);
  2550. device_release_driver(data->dev);
  2551. device_unregister(data->dev);
  2552. ieee80211_free_hw(data->hw);
  2553. }
  2554. static int mac80211_hwsim_get_radio(struct sk_buff *skb,
  2555. struct mac80211_hwsim_data *data,
  2556. u32 portid, u32 seq,
  2557. struct netlink_callback *cb, int flags)
  2558. {
  2559. void *hdr;
  2560. struct hwsim_new_radio_params param = { };
  2561. int res = -EMSGSIZE;
  2562. hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
  2563. HWSIM_CMD_GET_RADIO);
  2564. if (!hdr)
  2565. return -EMSGSIZE;
  2566. if (cb)
  2567. genl_dump_check_consistent(cb, hdr);
  2568. if (data->alpha2[0] && data->alpha2[1])
  2569. param.reg_alpha2 = data->alpha2;
  2570. param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
  2571. REGULATORY_STRICT_REG);
  2572. param.p2p_device = !!(data->hw->wiphy->interface_modes &
  2573. BIT(NL80211_IFTYPE_P2P_DEVICE));
  2574. param.use_chanctx = data->use_chanctx;
  2575. param.regd = data->regd;
  2576. param.channels = data->channels;
  2577. param.hwname = wiphy_name(data->hw->wiphy);
  2578. res = append_radio_msg(skb, data->idx, &param);
  2579. if (res < 0)
  2580. goto out_err;
  2581. genlmsg_end(skb, hdr);
  2582. return 0;
  2583. out_err:
  2584. genlmsg_cancel(skb, hdr);
  2585. return res;
  2586. }
  2587. static void mac80211_hwsim_free(void)
  2588. {
  2589. struct mac80211_hwsim_data *data;
  2590. spin_lock_bh(&hwsim_radio_lock);
  2591. while ((data = list_first_entry_or_null(&hwsim_radios,
  2592. struct mac80211_hwsim_data,
  2593. list))) {
  2594. list_del(&data->list);
  2595. spin_unlock_bh(&hwsim_radio_lock);
  2596. mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
  2597. NULL);
  2598. spin_lock_bh(&hwsim_radio_lock);
  2599. }
  2600. spin_unlock_bh(&hwsim_radio_lock);
  2601. class_destroy(hwsim_class);
  2602. }
  2603. static const struct net_device_ops hwsim_netdev_ops = {
  2604. .ndo_start_xmit = hwsim_mon_xmit,
  2605. .ndo_set_mac_address = eth_mac_addr,
  2606. .ndo_validate_addr = eth_validate_addr,
  2607. };
  2608. static void hwsim_mon_setup(struct net_device *dev)
  2609. {
  2610. dev->netdev_ops = &hwsim_netdev_ops;
  2611. dev->needs_free_netdev = true;
  2612. ether_setup(dev);
  2613. dev->priv_flags |= IFF_NO_QUEUE;
  2614. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  2615. eth_zero_addr(dev->dev_addr);
  2616. dev->dev_addr[0] = 0x12;
  2617. }
  2618. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
  2619. {
  2620. return rhashtable_lookup_fast(&hwsim_radios_rht,
  2621. addr,
  2622. hwsim_rht_params);
  2623. }
  2624. static void hwsim_register_wmediumd(struct net *net, u32 portid)
  2625. {
  2626. struct mac80211_hwsim_data *data;
  2627. hwsim_net_set_wmediumd(net, portid);
  2628. spin_lock_bh(&hwsim_radio_lock);
  2629. list_for_each_entry(data, &hwsim_radios, list) {
  2630. if (data->netgroup == hwsim_net_get_netgroup(net))
  2631. data->wmediumd = portid;
  2632. }
  2633. spin_unlock_bh(&hwsim_radio_lock);
  2634. }
  2635. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  2636. struct genl_info *info)
  2637. {
  2638. struct ieee80211_hdr *hdr;
  2639. struct mac80211_hwsim_data *data2;
  2640. struct ieee80211_tx_info *txi;
  2641. struct hwsim_tx_rate *tx_attempts;
  2642. u64 ret_skb_cookie;
  2643. struct sk_buff *skb, *tmp;
  2644. const u8 *src;
  2645. unsigned int hwsim_flags;
  2646. int i;
  2647. bool found = false;
  2648. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  2649. !info->attrs[HWSIM_ATTR_FLAGS] ||
  2650. !info->attrs[HWSIM_ATTR_COOKIE] ||
  2651. !info->attrs[HWSIM_ATTR_SIGNAL] ||
  2652. !info->attrs[HWSIM_ATTR_TX_INFO])
  2653. goto out;
  2654. src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  2655. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  2656. ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  2657. data2 = get_hwsim_data_ref_from_addr(src);
  2658. if (!data2)
  2659. goto out;
  2660. if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
  2661. goto out;
  2662. if (info->snd_portid != data2->wmediumd)
  2663. goto out;
  2664. /* look for the skb matching the cookie passed back from user */
  2665. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  2666. u64 skb_cookie;
  2667. txi = IEEE80211_SKB_CB(skb);
  2668. skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
  2669. if (skb_cookie == ret_skb_cookie) {
  2670. skb_unlink(skb, &data2->pending);
  2671. found = true;
  2672. break;
  2673. }
  2674. }
  2675. /* not found */
  2676. if (!found)
  2677. goto out;
  2678. /* Tx info received because the frame was broadcasted on user space,
  2679. so we get all the necessary info: tx attempts and skb control buff */
  2680. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  2681. info->attrs[HWSIM_ATTR_TX_INFO]);
  2682. /* now send back TX status */
  2683. txi = IEEE80211_SKB_CB(skb);
  2684. ieee80211_tx_info_clear_status(txi);
  2685. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  2686. txi->status.rates[i].idx = tx_attempts[i].idx;
  2687. txi->status.rates[i].count = tx_attempts[i].count;
  2688. }
  2689. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  2690. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  2691. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  2692. if (skb->len >= 16) {
  2693. hdr = (struct ieee80211_hdr *) skb->data;
  2694. mac80211_hwsim_monitor_ack(data2->channel,
  2695. hdr->addr2);
  2696. }
  2697. txi->flags |= IEEE80211_TX_STAT_ACK;
  2698. }
  2699. ieee80211_tx_status_irqsafe(data2->hw, skb);
  2700. return 0;
  2701. out:
  2702. return -EINVAL;
  2703. }
  2704. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  2705. struct genl_info *info)
  2706. {
  2707. struct mac80211_hwsim_data *data2;
  2708. struct ieee80211_rx_status rx_status;
  2709. const u8 *dst;
  2710. int frame_data_len;
  2711. void *frame_data;
  2712. struct sk_buff *skb = NULL;
  2713. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  2714. !info->attrs[HWSIM_ATTR_FRAME] ||
  2715. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  2716. !info->attrs[HWSIM_ATTR_SIGNAL])
  2717. goto out;
  2718. dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  2719. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  2720. frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  2721. /* Allocate new skb here */
  2722. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  2723. if (skb == NULL)
  2724. goto err;
  2725. if (frame_data_len > IEEE80211_MAX_DATA_LEN)
  2726. goto err;
  2727. /* Copy the data */
  2728. skb_put_data(skb, frame_data, frame_data_len);
  2729. data2 = get_hwsim_data_ref_from_addr(dst);
  2730. if (!data2)
  2731. goto out;
  2732. if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
  2733. goto out;
  2734. if (info->snd_portid != data2->wmediumd)
  2735. goto out;
  2736. /* check if radio is configured properly */
  2737. if (data2->idle || !data2->started)
  2738. goto out;
  2739. /* A frame is received from user space */
  2740. memset(&rx_status, 0, sizeof(rx_status));
  2741. if (info->attrs[HWSIM_ATTR_FREQ]) {
  2742. /* throw away off-channel packets, but allow both the temporary
  2743. * ("hw" scan/remain-on-channel) and regular channel, since the
  2744. * internal datapath also allows this
  2745. */
  2746. mutex_lock(&data2->mutex);
  2747. rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
  2748. if (rx_status.freq != data2->channel->center_freq &&
  2749. (!data2->tmp_chan ||
  2750. rx_status.freq != data2->tmp_chan->center_freq)) {
  2751. mutex_unlock(&data2->mutex);
  2752. goto out;
  2753. }
  2754. mutex_unlock(&data2->mutex);
  2755. } else {
  2756. rx_status.freq = data2->channel->center_freq;
  2757. }
  2758. rx_status.band = data2->channel->band;
  2759. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  2760. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  2761. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  2762. data2->rx_pkts++;
  2763. data2->rx_bytes += skb->len;
  2764. ieee80211_rx_irqsafe(data2->hw, skb);
  2765. return 0;
  2766. err:
  2767. pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
  2768. out:
  2769. dev_kfree_skb(skb);
  2770. return -EINVAL;
  2771. }
  2772. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  2773. struct genl_info *info)
  2774. {
  2775. struct net *net = genl_info_net(info);
  2776. struct mac80211_hwsim_data *data;
  2777. int chans = 1;
  2778. spin_lock_bh(&hwsim_radio_lock);
  2779. list_for_each_entry(data, &hwsim_radios, list)
  2780. chans = max(chans, data->channels);
  2781. spin_unlock_bh(&hwsim_radio_lock);
  2782. /* In the future we should revise the userspace API and allow it
  2783. * to set a flag that it does support multi-channel, then we can
  2784. * let this pass conditionally on the flag.
  2785. * For current userspace, prohibit it since it won't work right.
  2786. */
  2787. if (chans > 1)
  2788. return -EOPNOTSUPP;
  2789. if (hwsim_net_get_wmediumd(net))
  2790. return -EBUSY;
  2791. hwsim_register_wmediumd(net, info->snd_portid);
  2792. pr_debug("mac80211_hwsim: received a REGISTER, "
  2793. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  2794. return 0;
  2795. }
  2796. static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
  2797. {
  2798. struct hwsim_new_radio_params param = { 0 };
  2799. const char *hwname = NULL;
  2800. int ret;
  2801. param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
  2802. param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
  2803. param.channels = channels;
  2804. param.destroy_on_close =
  2805. info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
  2806. if (info->attrs[HWSIM_ATTR_CHANNELS])
  2807. param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
  2808. if (param.channels < 1) {
  2809. GENL_SET_ERR_MSG(info, "must have at least one channel");
  2810. return -EINVAL;
  2811. }
  2812. if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
  2813. GENL_SET_ERR_MSG(info, "too many channels specified");
  2814. return -EINVAL;
  2815. }
  2816. if (info->attrs[HWSIM_ATTR_NO_VIF])
  2817. param.no_vif = true;
  2818. if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
  2819. hwname = kasprintf(GFP_KERNEL, "%.*s",
  2820. nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
  2821. (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
  2822. if (!hwname)
  2823. return -ENOMEM;
  2824. param.hwname = hwname;
  2825. }
  2826. if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
  2827. param.use_chanctx = true;
  2828. else
  2829. param.use_chanctx = (param.channels > 1);
  2830. if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
  2831. param.reg_alpha2 =
  2832. nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
  2833. if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
  2834. u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
  2835. if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
  2836. kfree(hwname);
  2837. return -EINVAL;
  2838. }
  2839. idx = array_index_nospec(idx,
  2840. ARRAY_SIZE(hwsim_world_regdom_custom));
  2841. param.regd = hwsim_world_regdom_custom[idx];
  2842. }
  2843. if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
  2844. if (!is_valid_ether_addr(
  2845. nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
  2846. GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
  2847. NL_SET_BAD_ATTR(info->extack,
  2848. info->attrs[HWSIM_ATTR_PERM_ADDR]);
  2849. kfree(hwname);
  2850. return -EINVAL;
  2851. }
  2852. param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
  2853. }
  2854. ret = mac80211_hwsim_new_radio(info, &param);
  2855. kfree(hwname);
  2856. return ret;
  2857. }
  2858. static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
  2859. {
  2860. struct mac80211_hwsim_data *data;
  2861. s64 idx = -1;
  2862. const char *hwname = NULL;
  2863. if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
  2864. idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
  2865. } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
  2866. hwname = kasprintf(GFP_KERNEL, "%.*s",
  2867. nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
  2868. (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
  2869. if (!hwname)
  2870. return -ENOMEM;
  2871. } else
  2872. return -EINVAL;
  2873. spin_lock_bh(&hwsim_radio_lock);
  2874. list_for_each_entry(data, &hwsim_radios, list) {
  2875. if (idx >= 0) {
  2876. if (data->idx != idx)
  2877. continue;
  2878. } else {
  2879. if (!hwname ||
  2880. strcmp(hwname, wiphy_name(data->hw->wiphy)))
  2881. continue;
  2882. }
  2883. if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
  2884. continue;
  2885. list_del(&data->list);
  2886. rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
  2887. hwsim_rht_params);
  2888. hwsim_radios_generation++;
  2889. spin_unlock_bh(&hwsim_radio_lock);
  2890. mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
  2891. info);
  2892. kfree(hwname);
  2893. return 0;
  2894. }
  2895. spin_unlock_bh(&hwsim_radio_lock);
  2896. kfree(hwname);
  2897. return -ENODEV;
  2898. }
  2899. static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
  2900. {
  2901. struct mac80211_hwsim_data *data;
  2902. struct sk_buff *skb;
  2903. int idx, res = -ENODEV;
  2904. if (!info->attrs[HWSIM_ATTR_RADIO_ID])
  2905. return -EINVAL;
  2906. idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
  2907. spin_lock_bh(&hwsim_radio_lock);
  2908. list_for_each_entry(data, &hwsim_radios, list) {
  2909. if (data->idx != idx)
  2910. continue;
  2911. if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
  2912. continue;
  2913. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  2914. if (!skb) {
  2915. res = -ENOMEM;
  2916. goto out_err;
  2917. }
  2918. res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
  2919. info->snd_seq, NULL, 0);
  2920. if (res < 0) {
  2921. nlmsg_free(skb);
  2922. goto out_err;
  2923. }
  2924. res = genlmsg_reply(skb, info);
  2925. break;
  2926. }
  2927. out_err:
  2928. spin_unlock_bh(&hwsim_radio_lock);
  2929. return res;
  2930. }
  2931. static int hwsim_dump_radio_nl(struct sk_buff *skb,
  2932. struct netlink_callback *cb)
  2933. {
  2934. int last_idx = cb->args[0] - 1;
  2935. struct mac80211_hwsim_data *data = NULL;
  2936. int res = 0;
  2937. void *hdr;
  2938. spin_lock_bh(&hwsim_radio_lock);
  2939. cb->seq = hwsim_radios_generation;
  2940. if (last_idx >= hwsim_radio_idx-1)
  2941. goto done;
  2942. list_for_each_entry(data, &hwsim_radios, list) {
  2943. if (data->idx <= last_idx)
  2944. continue;
  2945. if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
  2946. continue;
  2947. res = mac80211_hwsim_get_radio(skb, data,
  2948. NETLINK_CB(cb->skb).portid,
  2949. cb->nlh->nlmsg_seq, cb,
  2950. NLM_F_MULTI);
  2951. if (res < 0)
  2952. break;
  2953. last_idx = data->idx;
  2954. }
  2955. cb->args[0] = last_idx + 1;
  2956. /* list changed, but no new element sent, set interrupted flag */
  2957. if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
  2958. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  2959. cb->nlh->nlmsg_seq, &hwsim_genl_family,
  2960. NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
  2961. if (hdr) {
  2962. genl_dump_check_consistent(cb, hdr);
  2963. genlmsg_end(skb, hdr);
  2964. } else {
  2965. res = -EMSGSIZE;
  2966. }
  2967. }
  2968. done:
  2969. spin_unlock_bh(&hwsim_radio_lock);
  2970. return res ?: skb->len;
  2971. }
  2972. /* Generic Netlink operations array */
  2973. static const struct genl_ops hwsim_ops[] = {
  2974. {
  2975. .cmd = HWSIM_CMD_REGISTER,
  2976. .policy = hwsim_genl_policy,
  2977. .doit = hwsim_register_received_nl,
  2978. .flags = GENL_UNS_ADMIN_PERM,
  2979. },
  2980. {
  2981. .cmd = HWSIM_CMD_FRAME,
  2982. .policy = hwsim_genl_policy,
  2983. .doit = hwsim_cloned_frame_received_nl,
  2984. },
  2985. {
  2986. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  2987. .policy = hwsim_genl_policy,
  2988. .doit = hwsim_tx_info_frame_received_nl,
  2989. },
  2990. {
  2991. .cmd = HWSIM_CMD_NEW_RADIO,
  2992. .policy = hwsim_genl_policy,
  2993. .doit = hwsim_new_radio_nl,
  2994. .flags = GENL_UNS_ADMIN_PERM,
  2995. },
  2996. {
  2997. .cmd = HWSIM_CMD_DEL_RADIO,
  2998. .policy = hwsim_genl_policy,
  2999. .doit = hwsim_del_radio_nl,
  3000. .flags = GENL_UNS_ADMIN_PERM,
  3001. },
  3002. {
  3003. .cmd = HWSIM_CMD_GET_RADIO,
  3004. .policy = hwsim_genl_policy,
  3005. .doit = hwsim_get_radio_nl,
  3006. .dumpit = hwsim_dump_radio_nl,
  3007. },
  3008. };
  3009. static struct genl_family hwsim_genl_family __ro_after_init = {
  3010. .name = "MAC80211_HWSIM",
  3011. .version = 1,
  3012. .maxattr = HWSIM_ATTR_MAX,
  3013. .netnsok = true,
  3014. .module = THIS_MODULE,
  3015. .ops = hwsim_ops,
  3016. .n_ops = ARRAY_SIZE(hwsim_ops),
  3017. .mcgrps = hwsim_mcgrps,
  3018. .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
  3019. };
  3020. static void remove_user_radios(u32 portid)
  3021. {
  3022. struct mac80211_hwsim_data *entry, *tmp;
  3023. LIST_HEAD(list);
  3024. spin_lock_bh(&hwsim_radio_lock);
  3025. list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
  3026. if (entry->destroy_on_close && entry->portid == portid) {
  3027. list_move(&entry->list, &list);
  3028. rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
  3029. hwsim_rht_params);
  3030. hwsim_radios_generation++;
  3031. }
  3032. }
  3033. spin_unlock_bh(&hwsim_radio_lock);
  3034. list_for_each_entry_safe(entry, tmp, &list, list) {
  3035. list_del(&entry->list);
  3036. mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
  3037. NULL);
  3038. }
  3039. }
  3040. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  3041. unsigned long state,
  3042. void *_notify)
  3043. {
  3044. struct netlink_notify *notify = _notify;
  3045. if (state != NETLINK_URELEASE)
  3046. return NOTIFY_DONE;
  3047. remove_user_radios(notify->portid);
  3048. if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
  3049. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  3050. " socket, switching to perfect channel medium\n");
  3051. hwsim_register_wmediumd(notify->net, 0);
  3052. }
  3053. return NOTIFY_DONE;
  3054. }
  3055. static struct notifier_block hwsim_netlink_notifier = {
  3056. .notifier_call = mac80211_hwsim_netlink_notify,
  3057. };
  3058. static int __init hwsim_init_netlink(void)
  3059. {
  3060. int rc;
  3061. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  3062. rc = genl_register_family(&hwsim_genl_family);
  3063. if (rc)
  3064. goto failure;
  3065. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  3066. if (rc) {
  3067. genl_unregister_family(&hwsim_genl_family);
  3068. goto failure;
  3069. }
  3070. return 0;
  3071. failure:
  3072. pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
  3073. return -EINVAL;
  3074. }
  3075. static __net_init int hwsim_init_net(struct net *net)
  3076. {
  3077. return hwsim_net_set_netgroup(net);
  3078. }
  3079. static void __net_exit hwsim_exit_net(struct net *net)
  3080. {
  3081. struct mac80211_hwsim_data *data, *tmp;
  3082. LIST_HEAD(list);
  3083. spin_lock_bh(&hwsim_radio_lock);
  3084. list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
  3085. if (!net_eq(wiphy_net(data->hw->wiphy), net))
  3086. continue;
  3087. /* Radios created in init_net are returned to init_net. */
  3088. if (data->netgroup == hwsim_net_get_netgroup(&init_net))
  3089. continue;
  3090. list_move(&data->list, &list);
  3091. rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
  3092. hwsim_rht_params);
  3093. hwsim_radios_generation++;
  3094. }
  3095. spin_unlock_bh(&hwsim_radio_lock);
  3096. list_for_each_entry_safe(data, tmp, &list, list) {
  3097. list_del(&data->list);
  3098. mac80211_hwsim_del_radio(data,
  3099. wiphy_name(data->hw->wiphy),
  3100. NULL);
  3101. }
  3102. ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
  3103. }
  3104. static struct pernet_operations hwsim_net_ops = {
  3105. .init = hwsim_init_net,
  3106. .exit = hwsim_exit_net,
  3107. .id = &hwsim_net_id,
  3108. .size = sizeof(struct hwsim_net),
  3109. };
  3110. static void hwsim_exit_netlink(void)
  3111. {
  3112. /* unregister the notifier */
  3113. netlink_unregister_notifier(&hwsim_netlink_notifier);
  3114. /* unregister the family */
  3115. genl_unregister_family(&hwsim_genl_family);
  3116. }
  3117. static int __init init_mac80211_hwsim(void)
  3118. {
  3119. int i, err;
  3120. if (radios < 0 || radios > 100)
  3121. return -EINVAL;
  3122. if (channels < 1)
  3123. return -EINVAL;
  3124. spin_lock_init(&hwsim_radio_lock);
  3125. hwsim_wq = alloc_workqueue("hwsim_wq", 0, 0);
  3126. if (!hwsim_wq)
  3127. return -ENOMEM;
  3128. err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
  3129. if (err)
  3130. goto out_free_wq;
  3131. err = register_pernet_device(&hwsim_net_ops);
  3132. if (err)
  3133. goto out_free_rht;
  3134. err = platform_driver_register(&mac80211_hwsim_driver);
  3135. if (err)
  3136. goto out_unregister_pernet;
  3137. err = hwsim_init_netlink();
  3138. if (err)
  3139. goto out_unregister_driver;
  3140. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  3141. if (IS_ERR(hwsim_class)) {
  3142. err = PTR_ERR(hwsim_class);
  3143. goto out_exit_netlink;
  3144. }
  3145. for (i = 0; i < radios; i++) {
  3146. struct hwsim_new_radio_params param = { 0 };
  3147. param.channels = channels;
  3148. switch (regtest) {
  3149. case HWSIM_REGTEST_DIFF_COUNTRY:
  3150. if (i < ARRAY_SIZE(hwsim_alpha2s))
  3151. param.reg_alpha2 = hwsim_alpha2s[i];
  3152. break;
  3153. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  3154. if (!i)
  3155. param.reg_alpha2 = hwsim_alpha2s[0];
  3156. break;
  3157. case HWSIM_REGTEST_STRICT_ALL:
  3158. param.reg_strict = true;
  3159. case HWSIM_REGTEST_DRIVER_REG_ALL:
  3160. param.reg_alpha2 = hwsim_alpha2s[0];
  3161. break;
  3162. case HWSIM_REGTEST_WORLD_ROAM:
  3163. if (i == 0)
  3164. param.regd = &hwsim_world_regdom_custom_01;
  3165. break;
  3166. case HWSIM_REGTEST_CUSTOM_WORLD:
  3167. param.regd = &hwsim_world_regdom_custom_01;
  3168. break;
  3169. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  3170. if (i == 0)
  3171. param.regd = &hwsim_world_regdom_custom_01;
  3172. else if (i == 1)
  3173. param.regd = &hwsim_world_regdom_custom_02;
  3174. break;
  3175. case HWSIM_REGTEST_STRICT_FOLLOW:
  3176. if (i == 0) {
  3177. param.reg_strict = true;
  3178. param.reg_alpha2 = hwsim_alpha2s[0];
  3179. }
  3180. break;
  3181. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  3182. if (i == 0) {
  3183. param.reg_strict = true;
  3184. param.reg_alpha2 = hwsim_alpha2s[0];
  3185. } else if (i == 1) {
  3186. param.reg_alpha2 = hwsim_alpha2s[1];
  3187. }
  3188. break;
  3189. case HWSIM_REGTEST_ALL:
  3190. switch (i) {
  3191. case 0:
  3192. param.regd = &hwsim_world_regdom_custom_01;
  3193. break;
  3194. case 1:
  3195. param.regd = &hwsim_world_regdom_custom_02;
  3196. break;
  3197. case 2:
  3198. param.reg_alpha2 = hwsim_alpha2s[0];
  3199. break;
  3200. case 3:
  3201. param.reg_alpha2 = hwsim_alpha2s[1];
  3202. break;
  3203. case 4:
  3204. param.reg_strict = true;
  3205. param.reg_alpha2 = hwsim_alpha2s[2];
  3206. break;
  3207. }
  3208. break;
  3209. default:
  3210. break;
  3211. }
  3212. param.p2p_device = support_p2p_device;
  3213. param.use_chanctx = channels > 1;
  3214. err = mac80211_hwsim_new_radio(NULL, &param);
  3215. if (err < 0)
  3216. goto out_free_radios;
  3217. }
  3218. hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
  3219. hwsim_mon_setup);
  3220. if (hwsim_mon == NULL) {
  3221. err = -ENOMEM;
  3222. goto out_free_radios;
  3223. }
  3224. rtnl_lock();
  3225. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  3226. if (err < 0) {
  3227. rtnl_unlock();
  3228. goto out_free_radios;
  3229. }
  3230. err = register_netdevice(hwsim_mon);
  3231. if (err < 0) {
  3232. rtnl_unlock();
  3233. goto out_free_mon;
  3234. }
  3235. rtnl_unlock();
  3236. return 0;
  3237. out_free_mon:
  3238. free_netdev(hwsim_mon);
  3239. out_free_radios:
  3240. mac80211_hwsim_free();
  3241. out_exit_netlink:
  3242. hwsim_exit_netlink();
  3243. out_unregister_driver:
  3244. platform_driver_unregister(&mac80211_hwsim_driver);
  3245. out_unregister_pernet:
  3246. unregister_pernet_device(&hwsim_net_ops);
  3247. out_free_rht:
  3248. rhashtable_destroy(&hwsim_radios_rht);
  3249. out_free_wq:
  3250. destroy_workqueue(hwsim_wq);
  3251. return err;
  3252. }
  3253. module_init(init_mac80211_hwsim);
  3254. static void __exit exit_mac80211_hwsim(void)
  3255. {
  3256. pr_debug("mac80211_hwsim: unregister radios\n");
  3257. hwsim_exit_netlink();
  3258. mac80211_hwsim_free();
  3259. flush_workqueue(hwsim_wq);
  3260. rhashtable_destroy(&hwsim_radios_rht);
  3261. unregister_netdev(hwsim_mon);
  3262. platform_driver_unregister(&mac80211_hwsim_driver);
  3263. unregister_pernet_device(&hwsim_net_ops);
  3264. destroy_workqueue(hwsim_wq);
  3265. }
  3266. module_exit(exit_mac80211_hwsim);