mac80211_hwsim.c 92 KB

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