mac80211_hwsim.c 93 KB

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