mac80211_hwsim.c 109 KB

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