mac80211_hwsim.c 95 KB

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