mac80211_hwsim.c 99 KB

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