mac80211_hwsim.c 99 KB

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