mac80211_hwsim.c 94 KB

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