mac80211_hwsim.c 103 KB

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