events.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eapol_supp/eapol_supp_sm.h"
  11. #include "rsn_supp/wpa.h"
  12. #include "eloop.h"
  13. #include "config.h"
  14. #include "l2_packet/l2_packet.h"
  15. #include "wpa_supplicant_i.h"
  16. #include "driver_i.h"
  17. #include "pcsc_funcs.h"
  18. #include "rsn_supp/preauth.h"
  19. #include "rsn_supp/pmksa_cache.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "eap_peer/eap.h"
  22. #include "ap/hostapd.h"
  23. #include "p2p/p2p.h"
  24. #include "fst/fst.h"
  25. #include "wnm_sta.h"
  26. #include "notify.h"
  27. #include "common/ieee802_11_defs.h"
  28. #include "common/ieee802_11_common.h"
  29. #include "common/gas_server.h"
  30. #include "crypto/random.h"
  31. #include "blacklist.h"
  32. #include "wpas_glue.h"
  33. #include "wps_supplicant.h"
  34. #include "ibss_rsn.h"
  35. #include "sme.h"
  36. #include "gas_query.h"
  37. #include "p2p_supplicant.h"
  38. #include "bgscan.h"
  39. #include "autoscan.h"
  40. #include "ap.h"
  41. #include "bss.h"
  42. #include "scan.h"
  43. #include "offchannel.h"
  44. #include "interworking.h"
  45. #include "mesh.h"
  46. #include "mesh_mpm.h"
  47. #include "wmm_ac.h"
  48. #include "dpp_supplicant.h"
  49. #ifndef CONFIG_NO_SCAN_PROCESSING
  50. static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
  51. int new_scan, int own_request);
  52. #endif /* CONFIG_NO_SCAN_PROCESSING */
  53. int wpas_temp_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  54. {
  55. struct os_reltime now;
  56. if (ssid == NULL || ssid->disabled_until.sec == 0)
  57. return 0;
  58. os_get_reltime(&now);
  59. if (ssid->disabled_until.sec > now.sec)
  60. return ssid->disabled_until.sec - now.sec;
  61. wpas_clear_temp_disabled(wpa_s, ssid, 0);
  62. return 0;
  63. }
  64. #ifndef CONFIG_NO_SCAN_PROCESSING
  65. /**
  66. * wpas_reenabled_network_time - Time until first network is re-enabled
  67. * @wpa_s: Pointer to wpa_supplicant data
  68. * Returns: If all enabled networks are temporarily disabled, returns the time
  69. * (in sec) until the first network is re-enabled. Otherwise returns 0.
  70. *
  71. * This function is used in case all enabled networks are temporarily disabled,
  72. * in which case it returns the time (in sec) that the first network will be
  73. * re-enabled. The function assumes that at least one network is enabled.
  74. */
  75. static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
  76. {
  77. struct wpa_ssid *ssid;
  78. int disabled_for, res = 0;
  79. #ifdef CONFIG_INTERWORKING
  80. if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
  81. wpa_s->conf->cred)
  82. return 0;
  83. #endif /* CONFIG_INTERWORKING */
  84. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  85. if (ssid->disabled)
  86. continue;
  87. disabled_for = wpas_temp_disabled(wpa_s, ssid);
  88. if (!disabled_for)
  89. return 0;
  90. if (!res || disabled_for < res)
  91. res = disabled_for;
  92. }
  93. return res;
  94. }
  95. #endif /* CONFIG_NO_SCAN_PROCESSING */
  96. void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
  97. {
  98. struct wpa_supplicant *wpa_s = eloop_ctx;
  99. if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
  100. return;
  101. wpa_dbg(wpa_s, MSG_DEBUG,
  102. "Try to associate due to network getting re-enabled");
  103. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  104. wpa_supplicant_cancel_sched_scan(wpa_s);
  105. wpa_supplicant_req_scan(wpa_s, 0, 0);
  106. }
  107. }
  108. static struct wpa_bss * wpa_supplicant_get_new_bss(
  109. struct wpa_supplicant *wpa_s, const u8 *bssid)
  110. {
  111. struct wpa_bss *bss = NULL;
  112. struct wpa_ssid *ssid = wpa_s->current_ssid;
  113. if (ssid->ssid_len > 0)
  114. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  115. if (!bss)
  116. bss = wpa_bss_get_bssid(wpa_s, bssid);
  117. return bss;
  118. }
  119. static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
  120. {
  121. struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
  122. if (!bss) {
  123. wpa_supplicant_update_scan_results(wpa_s);
  124. /* Get the BSS from the new scan results */
  125. bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
  126. }
  127. if (bss)
  128. wpa_s->current_bss = bss;
  129. }
  130. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  131. {
  132. struct wpa_ssid *ssid, *old_ssid;
  133. u8 drv_ssid[SSID_MAX_LEN];
  134. size_t drv_ssid_len;
  135. int res;
  136. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
  137. wpa_supplicant_update_current_bss(wpa_s);
  138. if (wpa_s->current_ssid->ssid_len == 0)
  139. return 0; /* current profile still in use */
  140. res = wpa_drv_get_ssid(wpa_s, drv_ssid);
  141. if (res < 0) {
  142. wpa_msg(wpa_s, MSG_INFO,
  143. "Failed to read SSID from driver");
  144. return 0; /* try to use current profile */
  145. }
  146. drv_ssid_len = res;
  147. if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
  148. os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
  149. drv_ssid_len) == 0)
  150. return 0; /* current profile still in use */
  151. wpa_msg(wpa_s, MSG_DEBUG,
  152. "Driver-initiated BSS selection changed the SSID to %s",
  153. wpa_ssid_txt(drv_ssid, drv_ssid_len));
  154. /* continue selecting a new network profile */
  155. }
  156. wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
  157. "information");
  158. ssid = wpa_supplicant_get_ssid(wpa_s);
  159. if (ssid == NULL) {
  160. wpa_msg(wpa_s, MSG_INFO,
  161. "No network configuration found for the current AP");
  162. return -1;
  163. }
  164. if (wpas_network_disabled(wpa_s, ssid)) {
  165. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
  166. return -1;
  167. }
  168. if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
  169. disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
  170. wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
  171. return -1;
  172. }
  173. res = wpas_temp_disabled(wpa_s, ssid);
  174. if (res > 0) {
  175. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
  176. "disabled for %d second(s)", res);
  177. return -1;
  178. }
  179. wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
  180. "current AP");
  181. if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  182. u8 wpa_ie[80];
  183. size_t wpa_ie_len = sizeof(wpa_ie);
  184. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  185. wpa_ie, &wpa_ie_len) < 0)
  186. wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
  187. } else {
  188. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  189. }
  190. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  191. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  192. old_ssid = wpa_s->current_ssid;
  193. wpa_s->current_ssid = ssid;
  194. wpa_supplicant_update_current_bss(wpa_s);
  195. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  196. wpa_supplicant_initiate_eapol(wpa_s);
  197. if (old_ssid != wpa_s->current_ssid)
  198. wpas_notify_network_changed(wpa_s);
  199. return 0;
  200. }
  201. void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
  202. {
  203. struct wpa_supplicant *wpa_s = eloop_ctx;
  204. if (wpa_s->countermeasures) {
  205. wpa_s->countermeasures = 0;
  206. wpa_drv_set_countermeasures(wpa_s, 0);
  207. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  208. /*
  209. * It is possible that the device is sched scanning, which means
  210. * that a connection attempt will be done only when we receive
  211. * scan results. However, in this case, it would be preferable
  212. * to scan and connect immediately, so cancel the sched_scan and
  213. * issue a regular scan flow.
  214. */
  215. wpa_supplicant_cancel_sched_scan(wpa_s);
  216. wpa_supplicant_req_scan(wpa_s, 0, 0);
  217. }
  218. }
  219. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  220. {
  221. int bssid_changed;
  222. wnm_bss_keep_alive_deinit(wpa_s);
  223. #ifdef CONFIG_IBSS_RSN
  224. ibss_rsn_deinit(wpa_s->ibss_rsn);
  225. wpa_s->ibss_rsn = NULL;
  226. #endif /* CONFIG_IBSS_RSN */
  227. #ifdef CONFIG_AP
  228. wpa_supplicant_ap_deinit(wpa_s);
  229. #endif /* CONFIG_AP */
  230. #ifdef CONFIG_HS20
  231. /* Clear possibly configured frame filters */
  232. wpa_drv_configure_frame_filters(wpa_s, 0);
  233. #endif /* CONFIG_HS20 */
  234. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  235. return;
  236. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  237. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  238. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  239. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  240. sme_clear_on_disassoc(wpa_s);
  241. wpa_s->current_bss = NULL;
  242. wpa_s->assoc_freq = 0;
  243. if (bssid_changed)
  244. wpas_notify_bssid_changed(wpa_s);
  245. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  246. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  247. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  248. wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
  249. wpa_s->key_mgmt == WPA_KEY_MGMT_DPP)
  250. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  251. wpa_s->ap_ies_from_associnfo = 0;
  252. wpa_s->current_ssid = NULL;
  253. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  254. wpa_s->key_mgmt = 0;
  255. wpas_rrm_reset(wpa_s);
  256. wpa_s->wnmsleep_used = 0;
  257. }
  258. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  259. {
  260. struct wpa_ie_data ie;
  261. int pmksa_set = -1;
  262. size_t i;
  263. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  264. ie.pmkid == NULL)
  265. return;
  266. for (i = 0; i < ie.num_pmkid; i++) {
  267. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  268. ie.pmkid + i * PMKID_LEN,
  269. NULL, NULL, 0, NULL);
  270. if (pmksa_set == 0) {
  271. eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
  272. break;
  273. }
  274. }
  275. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
  276. "PMKSA cache", pmksa_set == 0 ? "" : "not ");
  277. }
  278. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  279. union wpa_event_data *data)
  280. {
  281. if (data == NULL) {
  282. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
  283. "event");
  284. return;
  285. }
  286. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  287. " index=%d preauth=%d",
  288. MAC2STR(data->pmkid_candidate.bssid),
  289. data->pmkid_candidate.index,
  290. data->pmkid_candidate.preauth);
  291. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  292. data->pmkid_candidate.index,
  293. data->pmkid_candidate.preauth);
  294. }
  295. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  296. {
  297. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  298. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  299. return 0;
  300. #ifdef IEEE8021X_EAPOL
  301. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  302. wpa_s->current_ssid &&
  303. !(wpa_s->current_ssid->eapol_flags &
  304. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  305. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  306. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  307. * plaintext or static WEP keys). */
  308. return 0;
  309. }
  310. #endif /* IEEE8021X_EAPOL */
  311. return 1;
  312. }
  313. /**
  314. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  315. * @wpa_s: pointer to wpa_supplicant data
  316. * @ssid: Configuration data for the network
  317. * Returns: 0 on success, -1 on failure
  318. *
  319. * This function is called when starting authentication with a network that is
  320. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  321. */
  322. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  323. struct wpa_ssid *ssid)
  324. {
  325. #ifdef IEEE8021X_EAPOL
  326. #ifdef PCSC_FUNCS
  327. int aka = 0, sim = 0;
  328. if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
  329. wpa_s->scard != NULL || wpa_s->conf->external_sim)
  330. return 0;
  331. if (ssid == NULL || ssid->eap.eap_methods == NULL) {
  332. sim = 1;
  333. aka = 1;
  334. } else {
  335. struct eap_method_type *eap = ssid->eap.eap_methods;
  336. while (eap->vendor != EAP_VENDOR_IETF ||
  337. eap->method != EAP_TYPE_NONE) {
  338. if (eap->vendor == EAP_VENDOR_IETF) {
  339. if (eap->method == EAP_TYPE_SIM)
  340. sim = 1;
  341. else if (eap->method == EAP_TYPE_AKA ||
  342. eap->method == EAP_TYPE_AKA_PRIME)
  343. aka = 1;
  344. }
  345. eap++;
  346. }
  347. }
  348. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  349. sim = 0;
  350. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
  351. eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
  352. NULL)
  353. aka = 0;
  354. if (!sim && !aka) {
  355. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
  356. "use SIM, but neither EAP-SIM nor EAP-AKA are "
  357. "enabled");
  358. return 0;
  359. }
  360. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
  361. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  362. wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
  363. if (wpa_s->scard == NULL) {
  364. wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
  365. "(pcsc-lite)");
  366. return -1;
  367. }
  368. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  369. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  370. #endif /* PCSC_FUNCS */
  371. #endif /* IEEE8021X_EAPOL */
  372. return 0;
  373. }
  374. #ifndef CONFIG_NO_SCAN_PROCESSING
  375. static int has_wep_key(struct wpa_ssid *ssid)
  376. {
  377. int i;
  378. for (i = 0; i < NUM_WEP_KEYS; i++) {
  379. if (ssid->wep_key_len[i])
  380. return 1;
  381. }
  382. return 0;
  383. }
  384. static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
  385. struct wpa_ssid *ssid)
  386. {
  387. int privacy = 0;
  388. if (ssid->mixed_cell)
  389. return 1;
  390. #ifdef CONFIG_WPS
  391. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  392. return 1;
  393. #endif /* CONFIG_WPS */
  394. if (has_wep_key(ssid))
  395. privacy = 1;
  396. #ifdef IEEE8021X_EAPOL
  397. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  398. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  399. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  400. privacy = 1;
  401. #endif /* IEEE8021X_EAPOL */
  402. if (wpa_key_mgmt_wpa(ssid->key_mgmt))
  403. privacy = 1;
  404. if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
  405. privacy = 1;
  406. if (bss->caps & IEEE80211_CAP_PRIVACY)
  407. return privacy;
  408. return !privacy;
  409. }
  410. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  411. struct wpa_ssid *ssid,
  412. struct wpa_bss *bss, int debug_print)
  413. {
  414. struct wpa_ie_data ie;
  415. int proto_match = 0;
  416. const u8 *rsn_ie, *wpa_ie;
  417. int ret;
  418. int wep_ok;
  419. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  420. if (ret >= 0)
  421. return ret;
  422. /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
  423. wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  424. (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  425. ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
  426. (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
  427. rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  428. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  429. proto_match++;
  430. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  431. if (debug_print)
  432. wpa_dbg(wpa_s, MSG_DEBUG,
  433. " skip RSN IE - parse failed");
  434. break;
  435. }
  436. if (wep_ok &&
  437. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  438. {
  439. if (debug_print)
  440. wpa_dbg(wpa_s, MSG_DEBUG,
  441. " selected based on TSN in RSN IE");
  442. return 1;
  443. }
  444. if (!(ie.proto & ssid->proto)) {
  445. if (debug_print)
  446. wpa_dbg(wpa_s, MSG_DEBUG,
  447. " skip RSN IE - proto mismatch");
  448. break;
  449. }
  450. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  451. if (debug_print)
  452. wpa_dbg(wpa_s, MSG_DEBUG,
  453. " skip RSN IE - PTK cipher mismatch");
  454. break;
  455. }
  456. if (!(ie.group_cipher & ssid->group_cipher)) {
  457. if (debug_print)
  458. wpa_dbg(wpa_s, MSG_DEBUG,
  459. " skip RSN IE - GTK cipher mismatch");
  460. break;
  461. }
  462. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  463. if (debug_print)
  464. wpa_dbg(wpa_s, MSG_DEBUG,
  465. " skip RSN IE - key mgmt mismatch");
  466. break;
  467. }
  468. #ifdef CONFIG_IEEE80211W
  469. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  470. wpas_get_ssid_pmf(wpa_s, ssid) ==
  471. MGMT_FRAME_PROTECTION_REQUIRED) {
  472. if (debug_print)
  473. wpa_dbg(wpa_s, MSG_DEBUG,
  474. " skip RSN IE - no mgmt frame protection");
  475. break;
  476. }
  477. #endif /* CONFIG_IEEE80211W */
  478. if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
  479. wpas_get_ssid_pmf(wpa_s, ssid) ==
  480. NO_MGMT_FRAME_PROTECTION) {
  481. if (debug_print)
  482. wpa_dbg(wpa_s, MSG_DEBUG,
  483. " skip RSN IE - no mgmt frame protection enabled but AP requires it");
  484. break;
  485. }
  486. #ifdef CONFIG_MBO
  487. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  488. wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND) &&
  489. wpas_get_ssid_pmf(wpa_s, ssid) !=
  490. NO_MGMT_FRAME_PROTECTION) {
  491. if (debug_print)
  492. wpa_dbg(wpa_s, MSG_DEBUG,
  493. " skip RSN IE - no mgmt frame protection enabled on MBO AP");
  494. break;
  495. }
  496. #endif /* CONFIG_MBO */
  497. if (debug_print)
  498. wpa_dbg(wpa_s, MSG_DEBUG,
  499. " selected based on RSN IE");
  500. return 1;
  501. }
  502. #ifdef CONFIG_IEEE80211W
  503. if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
  504. if (debug_print)
  505. wpa_dbg(wpa_s, MSG_DEBUG,
  506. " skip - MFP Required but network not MFP Capable");
  507. return 0;
  508. }
  509. #endif /* CONFIG_IEEE80211W */
  510. wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  511. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  512. proto_match++;
  513. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  514. if (debug_print)
  515. wpa_dbg(wpa_s, MSG_DEBUG,
  516. " skip WPA IE - parse failed");
  517. break;
  518. }
  519. if (wep_ok &&
  520. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  521. {
  522. if (debug_print)
  523. wpa_dbg(wpa_s, MSG_DEBUG,
  524. " selected based on TSN in WPA IE");
  525. return 1;
  526. }
  527. if (!(ie.proto & ssid->proto)) {
  528. if (debug_print)
  529. wpa_dbg(wpa_s, MSG_DEBUG,
  530. " skip WPA IE - proto mismatch");
  531. break;
  532. }
  533. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  534. if (debug_print)
  535. wpa_dbg(wpa_s, MSG_DEBUG,
  536. " skip WPA IE - PTK cipher mismatch");
  537. break;
  538. }
  539. if (!(ie.group_cipher & ssid->group_cipher)) {
  540. if (debug_print)
  541. wpa_dbg(wpa_s, MSG_DEBUG,
  542. " skip WPA IE - GTK cipher mismatch");
  543. break;
  544. }
  545. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  546. if (debug_print)
  547. wpa_dbg(wpa_s, MSG_DEBUG,
  548. " skip WPA IE - key mgmt mismatch");
  549. break;
  550. }
  551. if (debug_print)
  552. wpa_dbg(wpa_s, MSG_DEBUG,
  553. " selected based on WPA IE");
  554. return 1;
  555. }
  556. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
  557. !rsn_ie) {
  558. if (debug_print)
  559. wpa_dbg(wpa_s, MSG_DEBUG,
  560. " allow for non-WPA IEEE 802.1X");
  561. return 1;
  562. }
  563. if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
  564. wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
  565. if (debug_print)
  566. wpa_dbg(wpa_s, MSG_DEBUG,
  567. " skip - no WPA/RSN proto match");
  568. return 0;
  569. }
  570. if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
  571. wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
  572. if (debug_print)
  573. wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
  574. return 1;
  575. }
  576. if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  577. if (debug_print)
  578. wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
  579. return 1;
  580. }
  581. if (debug_print)
  582. wpa_dbg(wpa_s, MSG_DEBUG,
  583. " reject due to mismatch with WPA/WPA2");
  584. return 0;
  585. }
  586. static int freq_allowed(int *freqs, int freq)
  587. {
  588. int i;
  589. if (freqs == NULL)
  590. return 1;
  591. for (i = 0; freqs[i]; i++)
  592. if (freqs[i] == freq)
  593. return 1;
  594. return 0;
  595. }
  596. static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  597. int debug_print)
  598. {
  599. const struct hostapd_hw_modes *mode = NULL, *modes;
  600. const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
  601. const u8 *rate_ie;
  602. int i, j, k;
  603. if (bss->freq == 0)
  604. return 1; /* Cannot do matching without knowing band */
  605. modes = wpa_s->hw.modes;
  606. if (modes == NULL) {
  607. /*
  608. * The driver does not provide any additional information
  609. * about the utilized hardware, so allow the connection attempt
  610. * to continue.
  611. */
  612. return 1;
  613. }
  614. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  615. for (j = 0; j < modes[i].num_channels; j++) {
  616. int freq = modes[i].channels[j].freq;
  617. if (freq == bss->freq) {
  618. if (mode &&
  619. mode->mode == HOSTAPD_MODE_IEEE80211G)
  620. break; /* do not allow 802.11b replace
  621. * 802.11g */
  622. mode = &modes[i];
  623. break;
  624. }
  625. }
  626. }
  627. if (mode == NULL)
  628. return 0;
  629. for (i = 0; i < (int) sizeof(scan_ie); i++) {
  630. rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
  631. if (rate_ie == NULL)
  632. continue;
  633. for (j = 2; j < rate_ie[1] + 2; j++) {
  634. int flagged = !!(rate_ie[j] & 0x80);
  635. int r = (rate_ie[j] & 0x7f) * 5;
  636. /*
  637. * IEEE Std 802.11n-2009 7.3.2.2:
  638. * The new BSS Membership selector value is encoded
  639. * like a legacy basic rate, but it is not a rate and
  640. * only indicates if the BSS members are required to
  641. * support the mandatory features of Clause 20 [HT PHY]
  642. * in order to join the BSS.
  643. */
  644. if (flagged && ((rate_ie[j] & 0x7f) ==
  645. BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
  646. if (!ht_supported(mode)) {
  647. if (debug_print)
  648. wpa_dbg(wpa_s, MSG_DEBUG,
  649. " hardware does not support HT PHY");
  650. return 0;
  651. }
  652. continue;
  653. }
  654. /* There's also a VHT selector for 802.11ac */
  655. if (flagged && ((rate_ie[j] & 0x7f) ==
  656. BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
  657. if (!vht_supported(mode)) {
  658. if (debug_print)
  659. wpa_dbg(wpa_s, MSG_DEBUG,
  660. " hardware does not support VHT PHY");
  661. return 0;
  662. }
  663. continue;
  664. }
  665. if (!flagged)
  666. continue;
  667. /* check for legacy basic rates */
  668. for (k = 0; k < mode->num_rates; k++) {
  669. if (mode->rates[k] == r)
  670. break;
  671. }
  672. if (k == mode->num_rates) {
  673. /*
  674. * IEEE Std 802.11-2007 7.3.2.2 demands that in
  675. * order to join a BSS all required rates
  676. * have to be supported by the hardware.
  677. */
  678. if (debug_print)
  679. wpa_dbg(wpa_s, MSG_DEBUG,
  680. " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
  681. r / 10, r % 10,
  682. bss->freq, mode->mode, mode->num_rates);
  683. return 0;
  684. }
  685. }
  686. }
  687. return 1;
  688. }
  689. /*
  690. * Test whether BSS is in an ESS.
  691. * This is done differently in DMG (60 GHz) and non-DMG bands
  692. */
  693. static int bss_is_ess(struct wpa_bss *bss)
  694. {
  695. if (bss_is_dmg(bss)) {
  696. return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
  697. IEEE80211_CAP_DMG_AP;
  698. }
  699. return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
  700. IEEE80211_CAP_ESS);
  701. }
  702. static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
  703. {
  704. size_t i;
  705. for (i = 0; i < ETH_ALEN; i++) {
  706. if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
  707. return 0;
  708. }
  709. return 1;
  710. }
  711. static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
  712. {
  713. size_t i;
  714. for (i = 0; i < num; i++) {
  715. const u8 *a = list + i * ETH_ALEN * 2;
  716. const u8 *m = a + ETH_ALEN;
  717. if (match_mac_mask(a, addr, m))
  718. return 1;
  719. }
  720. return 0;
  721. }
  722. struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  723. int i, struct wpa_bss *bss,
  724. struct wpa_ssid *group,
  725. int only_first_ssid, int debug_print)
  726. {
  727. u8 wpa_ie_len, rsn_ie_len;
  728. int wpa;
  729. struct wpa_blacklist *e;
  730. const u8 *ie;
  731. struct wpa_ssid *ssid;
  732. int osen;
  733. #ifdef CONFIG_MBO
  734. const u8 *assoc_disallow;
  735. #endif /* CONFIG_MBO */
  736. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  737. wpa_ie_len = ie ? ie[1] : 0;
  738. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  739. rsn_ie_len = ie ? ie[1] : 0;
  740. ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  741. osen = ie != NULL;
  742. if (debug_print) {
  743. wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
  744. " ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
  745. i, MAC2STR(bss->bssid),
  746. wpa_ssid_txt(bss->ssid, bss->ssid_len),
  747. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  748. bss->freq,
  749. wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
  750. " wps" : "",
  751. (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  752. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
  753. ? " p2p" : "",
  754. osen ? " osen=1" : "");
  755. }
  756. e = wpa_blacklist_get(wpa_s, bss->bssid);
  757. if (e) {
  758. int limit = 1;
  759. if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
  760. /*
  761. * When only a single network is enabled, we can
  762. * trigger blacklisting on the first failure. This
  763. * should not be done with multiple enabled networks to
  764. * avoid getting forced to move into a worse ESS on
  765. * single error if there are no other BSSes of the
  766. * current ESS.
  767. */
  768. limit = 0;
  769. }
  770. if (e->count > limit) {
  771. if (debug_print) {
  772. wpa_dbg(wpa_s, MSG_DEBUG,
  773. " skip - blacklisted (count=%d limit=%d)",
  774. e->count, limit);
  775. }
  776. return NULL;
  777. }
  778. }
  779. if (bss->ssid_len == 0) {
  780. if (debug_print)
  781. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
  782. return NULL;
  783. }
  784. if (disallowed_bssid(wpa_s, bss->bssid)) {
  785. if (debug_print)
  786. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
  787. return NULL;
  788. }
  789. if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  790. if (debug_print)
  791. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
  792. return NULL;
  793. }
  794. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  795. for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
  796. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  797. int res;
  798. if (wpas_network_disabled(wpa_s, ssid)) {
  799. if (debug_print)
  800. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
  801. continue;
  802. }
  803. res = wpas_temp_disabled(wpa_s, ssid);
  804. if (res > 0) {
  805. if (debug_print)
  806. wpa_dbg(wpa_s, MSG_DEBUG,
  807. " skip - disabled temporarily for %d second(s)",
  808. res);
  809. continue;
  810. }
  811. #ifdef CONFIG_WPS
  812. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  813. if (debug_print)
  814. wpa_dbg(wpa_s, MSG_DEBUG,
  815. " skip - blacklisted (WPS)");
  816. continue;
  817. }
  818. if (wpa && ssid->ssid_len == 0 &&
  819. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  820. check_ssid = 0;
  821. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  822. /* Only allow wildcard SSID match if an AP
  823. * advertises active WPS operation that matches
  824. * with our mode. */
  825. check_ssid = 1;
  826. if (ssid->ssid_len == 0 &&
  827. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  828. check_ssid = 0;
  829. }
  830. #endif /* CONFIG_WPS */
  831. if (ssid->bssid_set && ssid->ssid_len == 0 &&
  832. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
  833. check_ssid = 0;
  834. if (check_ssid &&
  835. (bss->ssid_len != ssid->ssid_len ||
  836. os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
  837. if (debug_print)
  838. wpa_dbg(wpa_s, MSG_DEBUG,
  839. " skip - SSID mismatch");
  840. continue;
  841. }
  842. if (ssid->bssid_set &&
  843. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  844. if (debug_print)
  845. wpa_dbg(wpa_s, MSG_DEBUG,
  846. " skip - BSSID mismatch");
  847. continue;
  848. }
  849. /* check blacklist */
  850. if (ssid->num_bssid_blacklist &&
  851. addr_in_list(bss->bssid, ssid->bssid_blacklist,
  852. ssid->num_bssid_blacklist)) {
  853. if (debug_print)
  854. wpa_dbg(wpa_s, MSG_DEBUG,
  855. " skip - BSSID blacklisted");
  856. continue;
  857. }
  858. /* if there is a whitelist, only accept those APs */
  859. if (ssid->num_bssid_whitelist &&
  860. !addr_in_list(bss->bssid, ssid->bssid_whitelist,
  861. ssid->num_bssid_whitelist)) {
  862. if (debug_print)
  863. wpa_dbg(wpa_s, MSG_DEBUG,
  864. " skip - BSSID not in whitelist");
  865. continue;
  866. }
  867. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss,
  868. debug_print))
  869. continue;
  870. if (!osen && !wpa &&
  871. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  872. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  873. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  874. if (debug_print)
  875. wpa_dbg(wpa_s, MSG_DEBUG,
  876. " skip - non-WPA network not allowed");
  877. continue;
  878. }
  879. if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  880. has_wep_key(ssid)) {
  881. if (debug_print)
  882. wpa_dbg(wpa_s, MSG_DEBUG,
  883. " skip - ignore WPA/WPA2 AP for WEP network block");
  884. continue;
  885. }
  886. if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
  887. if (debug_print)
  888. wpa_dbg(wpa_s, MSG_DEBUG,
  889. " skip - non-OSEN network not allowed");
  890. continue;
  891. }
  892. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  893. if (debug_print)
  894. wpa_dbg(wpa_s, MSG_DEBUG,
  895. " skip - privacy mismatch");
  896. continue;
  897. }
  898. if (ssid->mode != IEEE80211_MODE_MESH && !bss_is_ess(bss) &&
  899. !bss_is_pbss(bss)) {
  900. if (debug_print)
  901. wpa_dbg(wpa_s, MSG_DEBUG,
  902. " skip - not ESS, PBSS, or MBSS");
  903. continue;
  904. }
  905. if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
  906. if (debug_print)
  907. wpa_dbg(wpa_s, MSG_DEBUG,
  908. " skip - PBSS mismatch (ssid %d bss %d)",
  909. ssid->pbss, bss_is_pbss(bss));
  910. continue;
  911. }
  912. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  913. if (debug_print)
  914. wpa_dbg(wpa_s, MSG_DEBUG,
  915. " skip - frequency not allowed");
  916. continue;
  917. }
  918. #ifdef CONFIG_MESH
  919. if (ssid->mode == IEEE80211_MODE_MESH && ssid->frequency > 0 &&
  920. ssid->frequency != bss->freq) {
  921. if (debug_print)
  922. wpa_dbg(wpa_s, MSG_DEBUG,
  923. " skip - frequency not allowed (mesh)");
  924. continue;
  925. }
  926. #endif /* CONFIG_MESH */
  927. if (!rate_match(wpa_s, bss, debug_print)) {
  928. if (debug_print)
  929. wpa_dbg(wpa_s, MSG_DEBUG,
  930. " skip - rate sets do not match");
  931. continue;
  932. }
  933. #ifndef CONFIG_IBSS_RSN
  934. if (ssid->mode == WPAS_MODE_IBSS &&
  935. !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
  936. WPA_KEY_MGMT_WPA_NONE))) {
  937. if (debug_print)
  938. wpa_dbg(wpa_s, MSG_DEBUG,
  939. " skip - IBSS RSN not supported in the build");
  940. continue;
  941. }
  942. #endif /* !CONFIG_IBSS_RSN */
  943. #ifdef CONFIG_P2P
  944. if (ssid->p2p_group &&
  945. !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  946. !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  947. if (debug_print)
  948. wpa_dbg(wpa_s, MSG_DEBUG,
  949. " skip - no P2P IE seen");
  950. continue;
  951. }
  952. if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
  953. struct wpabuf *p2p_ie;
  954. u8 dev_addr[ETH_ALEN];
  955. ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  956. if (ie == NULL) {
  957. if (debug_print)
  958. wpa_dbg(wpa_s, MSG_DEBUG,
  959. " skip - no P2P element");
  960. continue;
  961. }
  962. p2p_ie = wpa_bss_get_vendor_ie_multi(
  963. bss, P2P_IE_VENDOR_TYPE);
  964. if (p2p_ie == NULL) {
  965. if (debug_print)
  966. wpa_dbg(wpa_s, MSG_DEBUG,
  967. " skip - could not fetch P2P element");
  968. continue;
  969. }
  970. if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
  971. || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
  972. ETH_ALEN) != 0) {
  973. if (debug_print)
  974. wpa_dbg(wpa_s, MSG_DEBUG,
  975. " skip - no matching GO P2P Device Address in P2P element");
  976. wpabuf_free(p2p_ie);
  977. continue;
  978. }
  979. wpabuf_free(p2p_ie);
  980. }
  981. /*
  982. * TODO: skip the AP if its P2P IE has Group Formation
  983. * bit set in the P2P Group Capability Bitmap and we
  984. * are not in Group Formation with that device.
  985. */
  986. #endif /* CONFIG_P2P */
  987. if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
  988. {
  989. struct os_reltime diff;
  990. os_reltime_sub(&wpa_s->scan_min_time,
  991. &bss->last_update, &diff);
  992. if (debug_print)
  993. wpa_dbg(wpa_s, MSG_DEBUG,
  994. " skip - scan result not recent enough (%u.%06u seconds too old)",
  995. (unsigned int) diff.sec,
  996. (unsigned int) diff.usec);
  997. continue;
  998. }
  999. #ifdef CONFIG_MBO
  1000. #ifdef CONFIG_TESTING_OPTIONS
  1001. if (wpa_s->ignore_assoc_disallow)
  1002. goto skip_assoc_disallow;
  1003. #endif /* CONFIG_TESTING_OPTIONS */
  1004. assoc_disallow = wpas_mbo_get_bss_attr(
  1005. bss, MBO_ATTR_ID_ASSOC_DISALLOW);
  1006. if (assoc_disallow && assoc_disallow[1] >= 1) {
  1007. if (debug_print)
  1008. wpa_dbg(wpa_s, MSG_DEBUG,
  1009. " skip - MBO association disallowed (reason %u)",
  1010. assoc_disallow[2]);
  1011. continue;
  1012. }
  1013. if (wpa_is_bss_tmp_disallowed(wpa_s, bss->bssid)) {
  1014. if (debug_print)
  1015. wpa_dbg(wpa_s, MSG_DEBUG,
  1016. " skip - MBO retry delay has not passed yet");
  1017. continue;
  1018. }
  1019. #ifdef CONFIG_TESTING_OPTIONS
  1020. skip_assoc_disallow:
  1021. #endif /* CONFIG_TESTING_OPTIONS */
  1022. #endif /* CONFIG_MBO */
  1023. #ifdef CONFIG_DPP
  1024. if ((ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
  1025. !wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid) &&
  1026. (!ssid->dpp_connector ||
  1027. !ssid->dpp_netaccesskey ||
  1028. !ssid->dpp_csign)) {
  1029. if (debug_print)
  1030. wpa_dbg(wpa_s, MSG_DEBUG,
  1031. " skip - no PMKSA entry for DPP");
  1032. continue;
  1033. }
  1034. #endif /* CONFIG_DPP */
  1035. /* Matching configuration found */
  1036. return ssid;
  1037. }
  1038. /* No matching configuration found */
  1039. return NULL;
  1040. }
  1041. static struct wpa_bss *
  1042. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  1043. struct wpa_ssid *group,
  1044. struct wpa_ssid **selected_ssid,
  1045. int only_first_ssid)
  1046. {
  1047. unsigned int i;
  1048. if (wpa_s->current_ssid) {
  1049. struct wpa_ssid *ssid;
  1050. wpa_dbg(wpa_s, MSG_DEBUG,
  1051. "Scan results matching the currently selected network");
  1052. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  1053. struct wpa_bss *bss = wpa_s->last_scan_res[i];
  1054. ssid = wpa_scan_res_match(wpa_s, i, bss, group,
  1055. only_first_ssid, 0);
  1056. if (ssid != wpa_s->current_ssid)
  1057. continue;
  1058. wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
  1059. " freq=%d level=%d snr=%d est_throughput=%u",
  1060. i, MAC2STR(bss->bssid), bss->freq, bss->level,
  1061. bss->snr, bss->est_throughput);
  1062. }
  1063. }
  1064. if (only_first_ssid)
  1065. wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
  1066. group->id);
  1067. else
  1068. wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
  1069. group->priority);
  1070. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  1071. struct wpa_bss *bss = wpa_s->last_scan_res[i];
  1072. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
  1073. only_first_ssid, 1);
  1074. if (!*selected_ssid)
  1075. continue;
  1076. wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
  1077. " ssid='%s'",
  1078. MAC2STR(bss->bssid),
  1079. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1080. return bss;
  1081. }
  1082. return NULL;
  1083. }
  1084. struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  1085. struct wpa_ssid **selected_ssid)
  1086. {
  1087. struct wpa_bss *selected = NULL;
  1088. int prio;
  1089. struct wpa_ssid *next_ssid = NULL;
  1090. struct wpa_ssid *ssid;
  1091. if (wpa_s->last_scan_res == NULL ||
  1092. wpa_s->last_scan_res_used == 0)
  1093. return NULL; /* no scan results from last update */
  1094. if (wpa_s->next_ssid) {
  1095. /* check that next_ssid is still valid */
  1096. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1097. if (ssid == wpa_s->next_ssid)
  1098. break;
  1099. }
  1100. next_ssid = ssid;
  1101. wpa_s->next_ssid = NULL;
  1102. }
  1103. while (selected == NULL) {
  1104. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  1105. if (next_ssid && next_ssid->priority ==
  1106. wpa_s->conf->pssid[prio]->priority) {
  1107. selected = wpa_supplicant_select_bss(
  1108. wpa_s, next_ssid, selected_ssid, 1);
  1109. if (selected)
  1110. break;
  1111. }
  1112. selected = wpa_supplicant_select_bss(
  1113. wpa_s, wpa_s->conf->pssid[prio],
  1114. selected_ssid, 0);
  1115. if (selected)
  1116. break;
  1117. }
  1118. if (selected == NULL && wpa_s->blacklist &&
  1119. !wpa_s->countermeasures) {
  1120. wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
  1121. "blacklist and try again");
  1122. wpa_blacklist_clear(wpa_s);
  1123. wpa_s->blacklist_cleared++;
  1124. } else if (selected == NULL)
  1125. break;
  1126. }
  1127. ssid = *selected_ssid;
  1128. if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
  1129. !ssid->passphrase && !ssid->ext_psk) {
  1130. const char *field_name, *txt = NULL;
  1131. wpa_dbg(wpa_s, MSG_DEBUG,
  1132. "PSK/passphrase not yet available for the selected network");
  1133. wpas_notify_network_request(wpa_s, ssid,
  1134. WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
  1135. field_name = wpa_supplicant_ctrl_req_to_string(
  1136. WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
  1137. if (field_name == NULL)
  1138. return NULL;
  1139. wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
  1140. selected = NULL;
  1141. }
  1142. return selected;
  1143. }
  1144. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  1145. int timeout_sec, int timeout_usec)
  1146. {
  1147. if (!wpa_supplicant_enabled_networks(wpa_s)) {
  1148. /*
  1149. * No networks are enabled; short-circuit request so
  1150. * we don't wait timeout seconds before transitioning
  1151. * to INACTIVE state.
  1152. */
  1153. wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
  1154. "since there are no enabled networks");
  1155. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1156. return;
  1157. }
  1158. wpa_s->scan_for_connection = 1;
  1159. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  1160. }
  1161. int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  1162. struct wpa_bss *selected,
  1163. struct wpa_ssid *ssid)
  1164. {
  1165. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  1166. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  1167. "PBC session overlap");
  1168. wpas_notify_wps_event_pbc_overlap(wpa_s);
  1169. #ifdef CONFIG_P2P
  1170. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  1171. wpa_s->p2p_in_provisioning) {
  1172. eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
  1173. wpa_s, NULL);
  1174. return -1;
  1175. }
  1176. #endif /* CONFIG_P2P */
  1177. #ifdef CONFIG_WPS
  1178. wpas_wps_pbc_overlap(wpa_s);
  1179. wpas_wps_cancel(wpa_s);
  1180. #endif /* CONFIG_WPS */
  1181. return -1;
  1182. }
  1183. wpa_msg(wpa_s, MSG_DEBUG,
  1184. "Considering connect request: reassociate: %d selected: "
  1185. MACSTR " bssid: " MACSTR " pending: " MACSTR
  1186. " wpa_state: %s ssid=%p current_ssid=%p",
  1187. wpa_s->reassociate, MAC2STR(selected->bssid),
  1188. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  1189. wpa_supplicant_state_txt(wpa_s->wpa_state),
  1190. ssid, wpa_s->current_ssid);
  1191. /*
  1192. * Do not trigger new association unless the BSSID has changed or if
  1193. * reassociation is requested. If we are in process of associating with
  1194. * the selected BSSID, do not trigger new attempt.
  1195. */
  1196. if (wpa_s->reassociate ||
  1197. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  1198. ((wpa_s->wpa_state != WPA_ASSOCIATING &&
  1199. wpa_s->wpa_state != WPA_AUTHENTICATING) ||
  1200. (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  1201. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  1202. 0) ||
  1203. (is_zero_ether_addr(wpa_s->pending_bssid) &&
  1204. ssid != wpa_s->current_ssid)))) {
  1205. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  1206. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  1207. return 0;
  1208. }
  1209. wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
  1210. MAC2STR(selected->bssid));
  1211. wpa_supplicant_associate(wpa_s, selected, ssid);
  1212. } else {
  1213. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
  1214. "connect with the selected AP");
  1215. }
  1216. return 0;
  1217. }
  1218. static struct wpa_ssid *
  1219. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  1220. {
  1221. int prio;
  1222. struct wpa_ssid *ssid;
  1223. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  1224. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  1225. {
  1226. if (wpas_network_disabled(wpa_s, ssid))
  1227. continue;
  1228. #ifndef CONFIG_IBSS_RSN
  1229. if (ssid->mode == WPAS_MODE_IBSS &&
  1230. !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
  1231. WPA_KEY_MGMT_WPA_NONE))) {
  1232. wpa_msg(wpa_s, MSG_INFO,
  1233. "IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
  1234. wpa_ssid_txt(ssid->ssid,
  1235. ssid->ssid_len));
  1236. continue;
  1237. }
  1238. #endif /* !CONFIG_IBSS_RSN */
  1239. if (ssid->mode == IEEE80211_MODE_IBSS ||
  1240. ssid->mode == IEEE80211_MODE_AP ||
  1241. ssid->mode == IEEE80211_MODE_MESH)
  1242. return ssid;
  1243. }
  1244. }
  1245. return NULL;
  1246. }
  1247. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  1248. * on BSS added and BSS changed events */
  1249. static void wpa_supplicant_rsn_preauth_scan_results(
  1250. struct wpa_supplicant *wpa_s)
  1251. {
  1252. struct wpa_bss *bss;
  1253. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  1254. return;
  1255. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1256. const u8 *ssid, *rsn;
  1257. ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  1258. if (ssid == NULL)
  1259. continue;
  1260. rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1261. if (rsn == NULL)
  1262. continue;
  1263. rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
  1264. }
  1265. }
  1266. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  1267. struct wpa_bss *selected,
  1268. struct wpa_ssid *ssid)
  1269. {
  1270. struct wpa_bss *current_bss = NULL;
  1271. #ifndef CONFIG_NO_ROAMING
  1272. int min_diff, diff;
  1273. int to_5ghz;
  1274. int cur_est, sel_est;
  1275. #endif /* CONFIG_NO_ROAMING */
  1276. if (wpa_s->reassociate)
  1277. return 1; /* explicit request to reassociate */
  1278. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1279. return 1; /* we are not associated; continue */
  1280. if (wpa_s->current_ssid == NULL)
  1281. return 1; /* unknown current SSID */
  1282. if (wpa_s->current_ssid != ssid)
  1283. return 1; /* different network block */
  1284. if (wpas_driver_bss_selection(wpa_s))
  1285. return 0; /* Driver-based roaming */
  1286. if (wpa_s->current_ssid->ssid)
  1287. current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
  1288. wpa_s->current_ssid->ssid,
  1289. wpa_s->current_ssid->ssid_len);
  1290. if (!current_bss)
  1291. current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
  1292. if (!current_bss)
  1293. return 1; /* current BSS not seen in scan results */
  1294. if (current_bss == selected)
  1295. return 0;
  1296. if (selected->last_update_idx > current_bss->last_update_idx)
  1297. return 1; /* current BSS not seen in the last scan */
  1298. #ifndef CONFIG_NO_ROAMING
  1299. wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
  1300. wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
  1301. " freq=%d level=%d snr=%d est_throughput=%u",
  1302. MAC2STR(current_bss->bssid),
  1303. current_bss->freq, current_bss->level,
  1304. current_bss->snr, current_bss->est_throughput);
  1305. wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
  1306. " freq=%d level=%d snr=%d est_throughput=%u",
  1307. MAC2STR(selected->bssid), selected->freq, selected->level,
  1308. selected->snr, selected->est_throughput);
  1309. if (wpa_s->current_ssid->bssid_set &&
  1310. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  1311. 0) {
  1312. wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
  1313. "has preferred BSSID");
  1314. return 1;
  1315. }
  1316. if (selected->est_throughput > current_bss->est_throughput + 5000) {
  1317. wpa_dbg(wpa_s, MSG_DEBUG,
  1318. "Allow reassociation - selected BSS has better estimated throughput");
  1319. return 1;
  1320. }
  1321. to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
  1322. if (current_bss->level < 0 &&
  1323. current_bss->level > selected->level + to_5ghz * 2) {
  1324. wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
  1325. "signal level");
  1326. return 0;
  1327. }
  1328. if (current_bss->est_throughput > selected->est_throughput + 5000) {
  1329. wpa_dbg(wpa_s, MSG_DEBUG,
  1330. "Skip roam - Current BSS has better estimated throughput");
  1331. return 0;
  1332. }
  1333. cur_est = current_bss->est_throughput;
  1334. sel_est = selected->est_throughput;
  1335. min_diff = 2;
  1336. if (current_bss->level < 0) {
  1337. if (current_bss->level < -85)
  1338. min_diff = 1;
  1339. else if (current_bss->level < -80)
  1340. min_diff = 2;
  1341. else if (current_bss->level < -75)
  1342. min_diff = 3;
  1343. else if (current_bss->level < -70)
  1344. min_diff = 4;
  1345. else
  1346. min_diff = 5;
  1347. if (cur_est > sel_est * 1.5)
  1348. min_diff += 10;
  1349. else if (cur_est > sel_est * 1.2)
  1350. min_diff += 5;
  1351. else if (cur_est > sel_est * 1.1)
  1352. min_diff += 2;
  1353. else if (cur_est > sel_est)
  1354. min_diff++;
  1355. }
  1356. if (to_5ghz) {
  1357. int reduce = 2;
  1358. /* Make it easier to move to 5 GHz band */
  1359. if (sel_est > cur_est * 1.5)
  1360. reduce = 5;
  1361. else if (sel_est > cur_est * 1.2)
  1362. reduce = 4;
  1363. else if (sel_est > cur_est * 1.1)
  1364. reduce = 3;
  1365. if (min_diff > reduce)
  1366. min_diff -= reduce;
  1367. else
  1368. min_diff = 0;
  1369. }
  1370. diff = abs(current_bss->level - selected->level);
  1371. if (diff < min_diff) {
  1372. wpa_dbg(wpa_s, MSG_DEBUG,
  1373. "Skip roam - too small difference in signal level (%d < %d)",
  1374. diff, min_diff);
  1375. return 0;
  1376. }
  1377. wpa_dbg(wpa_s, MSG_DEBUG,
  1378. "Allow reassociation due to difference in signal level (%d >= %d)",
  1379. diff, min_diff);
  1380. return 1;
  1381. #else /* CONFIG_NO_ROAMING */
  1382. return 0;
  1383. #endif /* CONFIG_NO_ROAMING */
  1384. }
  1385. /*
  1386. * Return a negative value if no scan results could be fetched or if scan
  1387. * results should not be shared with other virtual interfaces.
  1388. * Return 0 if scan results were fetched and may be shared with other
  1389. * interfaces.
  1390. * Return 1 if scan results may be shared with other virtual interfaces but may
  1391. * not trigger any operations.
  1392. * Return 2 if the interface was removed and cannot be used.
  1393. */
  1394. static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  1395. union wpa_event_data *data,
  1396. int own_request, int update_only)
  1397. {
  1398. struct wpa_scan_results *scan_res = NULL;
  1399. int ret = 0;
  1400. int ap = 0;
  1401. #ifndef CONFIG_NO_RANDOM_POOL
  1402. size_t i, num;
  1403. #endif /* CONFIG_NO_RANDOM_POOL */
  1404. #ifdef CONFIG_AP
  1405. if (wpa_s->ap_iface)
  1406. ap = 1;
  1407. #endif /* CONFIG_AP */
  1408. wpa_supplicant_notify_scanning(wpa_s, 0);
  1409. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  1410. data ? &data->scan_info :
  1411. NULL, 1);
  1412. if (scan_res == NULL) {
  1413. if (wpa_s->conf->ap_scan == 2 || ap ||
  1414. wpa_s->scan_res_handler == scan_only_handler)
  1415. return -1;
  1416. if (!own_request)
  1417. return -1;
  1418. if (data && data->scan_info.external_scan)
  1419. return -1;
  1420. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
  1421. "scanning again");
  1422. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  1423. ret = -1;
  1424. goto scan_work_done;
  1425. }
  1426. #ifndef CONFIG_NO_RANDOM_POOL
  1427. num = scan_res->num;
  1428. if (num > 10)
  1429. num = 10;
  1430. for (i = 0; i < num; i++) {
  1431. u8 buf[5];
  1432. struct wpa_scan_res *res = scan_res->res[i];
  1433. buf[0] = res->bssid[5];
  1434. buf[1] = res->qual & 0xff;
  1435. buf[2] = res->noise & 0xff;
  1436. buf[3] = res->level & 0xff;
  1437. buf[4] = res->tsf & 0xff;
  1438. random_add_randomness(buf, sizeof(buf));
  1439. }
  1440. #endif /* CONFIG_NO_RANDOM_POOL */
  1441. if (update_only) {
  1442. ret = 1;
  1443. goto scan_work_done;
  1444. }
  1445. if (own_request && wpa_s->scan_res_handler &&
  1446. !(data && data->scan_info.external_scan)) {
  1447. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  1448. struct wpa_scan_results *scan_res);
  1449. scan_res_handler = wpa_s->scan_res_handler;
  1450. wpa_s->scan_res_handler = NULL;
  1451. scan_res_handler(wpa_s, scan_res);
  1452. ret = 1;
  1453. goto scan_work_done;
  1454. }
  1455. if (ap) {
  1456. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
  1457. #ifdef CONFIG_AP
  1458. if (wpa_s->ap_iface->scan_cb)
  1459. wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
  1460. #endif /* CONFIG_AP */
  1461. goto scan_work_done;
  1462. }
  1463. wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
  1464. wpa_s->own_scan_running,
  1465. data ? data->scan_info.external_scan : 0);
  1466. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1467. wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
  1468. own_request && !(data && data->scan_info.external_scan)) {
  1469. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1470. wpa_s->manual_scan_id);
  1471. wpa_s->manual_scan_use_id = 0;
  1472. } else {
  1473. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1474. }
  1475. wpas_notify_scan_results(wpa_s);
  1476. wpas_notify_scan_done(wpa_s, 1);
  1477. if (data && data->scan_info.external_scan) {
  1478. wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
  1479. wpa_scan_results_free(scan_res);
  1480. return 0;
  1481. }
  1482. if (wnm_scan_process(wpa_s, 1) > 0)
  1483. goto scan_work_done;
  1484. if (sme_proc_obss_scan(wpa_s) > 0)
  1485. goto scan_work_done;
  1486. if (own_request &&
  1487. wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
  1488. goto scan_work_done;
  1489. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
  1490. goto scan_work_done;
  1491. if (autoscan_notify_scan(wpa_s, scan_res))
  1492. goto scan_work_done;
  1493. if (wpa_s->disconnected) {
  1494. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1495. goto scan_work_done;
  1496. }
  1497. if (!wpas_driver_bss_selection(wpa_s) &&
  1498. bgscan_notify_scan(wpa_s, scan_res) == 1)
  1499. goto scan_work_done;
  1500. wpas_wps_update_ap_info(wpa_s, scan_res);
  1501. if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
  1502. wpa_s->wpa_state < WPA_COMPLETED)
  1503. goto scan_work_done;
  1504. wpa_scan_results_free(scan_res);
  1505. if (own_request && wpa_s->scan_work) {
  1506. struct wpa_radio_work *work = wpa_s->scan_work;
  1507. wpa_s->scan_work = NULL;
  1508. radio_work_done(work);
  1509. }
  1510. return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
  1511. scan_work_done:
  1512. wpa_scan_results_free(scan_res);
  1513. if (own_request && wpa_s->scan_work) {
  1514. struct wpa_radio_work *work = wpa_s->scan_work;
  1515. wpa_s->scan_work = NULL;
  1516. radio_work_done(work);
  1517. }
  1518. return ret;
  1519. }
  1520. static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
  1521. int new_scan, int own_request)
  1522. {
  1523. struct wpa_bss *selected;
  1524. struct wpa_ssid *ssid = NULL;
  1525. int time_to_reenable = wpas_reenabled_network_time(wpa_s);
  1526. if (time_to_reenable > 0) {
  1527. wpa_dbg(wpa_s, MSG_DEBUG,
  1528. "Postpone network selection by %d seconds since all networks are disabled",
  1529. time_to_reenable);
  1530. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  1531. eloop_register_timeout(time_to_reenable, 0,
  1532. wpas_network_reenabled, wpa_s, NULL);
  1533. return 0;
  1534. }
  1535. if (wpa_s->p2p_mgmt)
  1536. return 0; /* no normal connection on p2p_mgmt interface */
  1537. selected = wpa_supplicant_pick_network(wpa_s, &ssid);
  1538. #ifdef CONFIG_MESH
  1539. if (wpa_s->ifmsh) {
  1540. wpa_msg(wpa_s, MSG_INFO,
  1541. "Avoiding join because we already joined a mesh group");
  1542. return 0;
  1543. }
  1544. #endif /* CONFIG_MESH */
  1545. if (selected) {
  1546. int skip;
  1547. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
  1548. if (skip) {
  1549. if (new_scan)
  1550. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  1551. return 0;
  1552. }
  1553. if (ssid != wpa_s->current_ssid &&
  1554. wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1555. wpa_s->own_disconnect_req = 1;
  1556. wpa_supplicant_deauthenticate(
  1557. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1558. }
  1559. if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
  1560. wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
  1561. return -1;
  1562. }
  1563. if (new_scan)
  1564. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  1565. /*
  1566. * Do not allow other virtual radios to trigger operations based
  1567. * on these scan results since we do not want them to start
  1568. * other associations at the same time.
  1569. */
  1570. return 1;
  1571. } else {
  1572. wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
  1573. ssid = wpa_supplicant_pick_new_network(wpa_s);
  1574. if (ssid) {
  1575. wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
  1576. wpa_supplicant_associate(wpa_s, NULL, ssid);
  1577. if (new_scan)
  1578. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  1579. } else if (own_request) {
  1580. /*
  1581. * No SSID found. If SCAN results are as a result of
  1582. * own scan request and not due to a scan request on
  1583. * another shared interface, try another scan.
  1584. */
  1585. int timeout_sec = wpa_s->scan_interval;
  1586. int timeout_usec = 0;
  1587. #ifdef CONFIG_P2P
  1588. int res;
  1589. res = wpas_p2p_scan_no_go_seen(wpa_s);
  1590. if (res == 2)
  1591. return 2;
  1592. if (res == 1)
  1593. return 0;
  1594. if (wpa_s->p2p_in_provisioning ||
  1595. wpa_s->show_group_started ||
  1596. wpa_s->p2p_in_invitation) {
  1597. /*
  1598. * Use shorter wait during P2P Provisioning
  1599. * state and during P2P join-a-group operation
  1600. * to speed up group formation.
  1601. */
  1602. timeout_sec = 0;
  1603. timeout_usec = 250000;
  1604. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1605. timeout_usec);
  1606. return 0;
  1607. }
  1608. #endif /* CONFIG_P2P */
  1609. #ifdef CONFIG_INTERWORKING
  1610. if (wpa_s->conf->auto_interworking &&
  1611. wpa_s->conf->interworking &&
  1612. wpa_s->conf->cred) {
  1613. wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
  1614. "start ANQP fetch since no matching "
  1615. "networks found");
  1616. wpa_s->network_select = 1;
  1617. wpa_s->auto_network_select = 1;
  1618. interworking_start_fetch_anqp(wpa_s);
  1619. return 1;
  1620. }
  1621. #endif /* CONFIG_INTERWORKING */
  1622. #ifdef CONFIG_WPS
  1623. if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
  1624. wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
  1625. timeout_sec = 0;
  1626. timeout_usec = 500000;
  1627. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1628. timeout_usec);
  1629. return 0;
  1630. }
  1631. #endif /* CONFIG_WPS */
  1632. if (wpa_supplicant_req_sched_scan(wpa_s))
  1633. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1634. timeout_usec);
  1635. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1636. WPA_EVENT_NETWORK_NOT_FOUND);
  1637. }
  1638. }
  1639. return 0;
  1640. }
  1641. static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  1642. union wpa_event_data *data)
  1643. {
  1644. struct wpa_supplicant *ifs;
  1645. int res;
  1646. res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
  1647. if (res == 2) {
  1648. /*
  1649. * Interface may have been removed, so must not dereference
  1650. * wpa_s after this.
  1651. */
  1652. return 1;
  1653. }
  1654. if (res < 0) {
  1655. /*
  1656. * If no scan results could be fetched, then no need to
  1657. * notify those interfaces that did not actually request
  1658. * this scan. Similarly, if scan results started a new operation on this
  1659. * interface, do not notify other interfaces to avoid concurrent
  1660. * operations during a connection attempt.
  1661. */
  1662. return 0;
  1663. }
  1664. /*
  1665. * Check other interfaces to see if they share the same radio. If
  1666. * so, they get updated with this same scan info.
  1667. */
  1668. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  1669. radio_list) {
  1670. if (ifs != wpa_s) {
  1671. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  1672. "sibling", ifs->ifname);
  1673. res = _wpa_supplicant_event_scan_results(ifs, data, 0,
  1674. res > 0);
  1675. if (res < 0)
  1676. return 0;
  1677. }
  1678. }
  1679. return 0;
  1680. }
  1681. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1682. int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
  1683. {
  1684. #ifdef CONFIG_NO_SCAN_PROCESSING
  1685. return -1;
  1686. #else /* CONFIG_NO_SCAN_PROCESSING */
  1687. struct os_reltime now;
  1688. wpa_s->ignore_post_flush_scan_res = 0;
  1689. if (wpa_s->last_scan_res_used == 0)
  1690. return -1;
  1691. os_get_reltime(&now);
  1692. if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
  1693. wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
  1694. return -1;
  1695. }
  1696. return wpas_select_network_from_last_scan(wpa_s, 0, 1);
  1697. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1698. }
  1699. #ifdef CONFIG_WNM
  1700. static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
  1701. {
  1702. struct wpa_supplicant *wpa_s = eloop_ctx;
  1703. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1704. return;
  1705. if (!wpa_s->no_keep_alive) {
  1706. wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
  1707. MAC2STR(wpa_s->bssid));
  1708. /* TODO: could skip this if normal data traffic has been sent */
  1709. /* TODO: Consider using some more appropriate data frame for
  1710. * this */
  1711. if (wpa_s->l2)
  1712. l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
  1713. (u8 *) "", 0);
  1714. }
  1715. #ifdef CONFIG_SME
  1716. if (wpa_s->sme.bss_max_idle_period) {
  1717. unsigned int msec;
  1718. msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
  1719. if (msec > 100)
  1720. msec -= 100;
  1721. eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
  1722. wnm_bss_keep_alive, wpa_s, NULL);
  1723. }
  1724. #endif /* CONFIG_SME */
  1725. }
  1726. static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
  1727. const u8 *ies, size_t ies_len)
  1728. {
  1729. struct ieee802_11_elems elems;
  1730. if (ies == NULL)
  1731. return;
  1732. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
  1733. return;
  1734. #ifdef CONFIG_SME
  1735. if (elems.bss_max_idle_period) {
  1736. unsigned int msec;
  1737. wpa_s->sme.bss_max_idle_period =
  1738. WPA_GET_LE16(elems.bss_max_idle_period);
  1739. wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
  1740. "TU)%s", wpa_s->sme.bss_max_idle_period,
  1741. (elems.bss_max_idle_period[2] & 0x01) ?
  1742. " (protected keep-live required)" : "");
  1743. if (wpa_s->sme.bss_max_idle_period == 0)
  1744. wpa_s->sme.bss_max_idle_period = 1;
  1745. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
  1746. eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
  1747. /* msec times 1000 */
  1748. msec = wpa_s->sme.bss_max_idle_period * 1024;
  1749. if (msec > 100)
  1750. msec -= 100;
  1751. eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
  1752. wnm_bss_keep_alive, wpa_s,
  1753. NULL);
  1754. }
  1755. }
  1756. #endif /* CONFIG_SME */
  1757. }
  1758. #endif /* CONFIG_WNM */
  1759. void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
  1760. {
  1761. #ifdef CONFIG_WNM
  1762. eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
  1763. #endif /* CONFIG_WNM */
  1764. }
  1765. #ifdef CONFIG_INTERWORKING
  1766. static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
  1767. size_t len)
  1768. {
  1769. int res;
  1770. wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
  1771. res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
  1772. if (res) {
  1773. wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
  1774. }
  1775. return res;
  1776. }
  1777. static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
  1778. const u8 *ies, size_t ies_len)
  1779. {
  1780. struct ieee802_11_elems elems;
  1781. if (ies == NULL)
  1782. return;
  1783. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
  1784. return;
  1785. if (elems.qos_map_set) {
  1786. wpas_qos_map_set(wpa_s, elems.qos_map_set,
  1787. elems.qos_map_set_len);
  1788. }
  1789. }
  1790. #endif /* CONFIG_INTERWORKING */
  1791. #ifdef CONFIG_FST
  1792. static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
  1793. const u8 *ie, size_t ie_len)
  1794. {
  1795. struct mb_ies_info mb_ies;
  1796. if (!ie || !ie_len || !wpa_s->fst)
  1797. return -ENOENT;
  1798. os_memset(&mb_ies, 0, sizeof(mb_ies));
  1799. while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
  1800. size_t len;
  1801. len = 2 + ie[1];
  1802. if (len > ie_len) {
  1803. wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
  1804. ie, ie_len);
  1805. break;
  1806. }
  1807. if (ie[0] == WLAN_EID_MULTI_BAND) {
  1808. wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
  1809. (unsigned int) len);
  1810. mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
  1811. mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
  1812. mb_ies.nof_ies++;
  1813. }
  1814. ie_len -= len;
  1815. ie += len;
  1816. }
  1817. if (mb_ies.nof_ies > 0) {
  1818. wpabuf_free(wpa_s->received_mb_ies);
  1819. wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
  1820. return 0;
  1821. }
  1822. return -ENOENT;
  1823. }
  1824. #endif /* CONFIG_FST */
  1825. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  1826. union wpa_event_data *data)
  1827. {
  1828. int l, len, found = 0, wpa_found, rsn_found;
  1829. const u8 *p;
  1830. #ifdef CONFIG_IEEE80211R
  1831. u8 bssid[ETH_ALEN];
  1832. #endif /* CONFIG_IEEE80211R */
  1833. wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
  1834. if (data->assoc_info.req_ies)
  1835. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  1836. data->assoc_info.req_ies_len);
  1837. if (data->assoc_info.resp_ies) {
  1838. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  1839. data->assoc_info.resp_ies_len);
  1840. #ifdef CONFIG_TDLS
  1841. wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
  1842. data->assoc_info.resp_ies_len);
  1843. #endif /* CONFIG_TDLS */
  1844. #ifdef CONFIG_WNM
  1845. wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
  1846. data->assoc_info.resp_ies_len);
  1847. #endif /* CONFIG_WNM */
  1848. #ifdef CONFIG_INTERWORKING
  1849. interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
  1850. data->assoc_info.resp_ies_len);
  1851. #endif /* CONFIG_INTERWORKING */
  1852. }
  1853. if (data->assoc_info.beacon_ies)
  1854. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  1855. data->assoc_info.beacon_ies,
  1856. data->assoc_info.beacon_ies_len);
  1857. if (data->assoc_info.freq)
  1858. wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
  1859. data->assoc_info.freq);
  1860. p = data->assoc_info.req_ies;
  1861. l = data->assoc_info.req_ies_len;
  1862. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  1863. while (p && l >= 2) {
  1864. len = p[1] + 2;
  1865. if (len > l) {
  1866. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1867. p, l);
  1868. break;
  1869. }
  1870. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1871. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  1872. (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
  1873. (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
  1874. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  1875. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  1876. break;
  1877. found = 1;
  1878. wpa_find_assoc_pmkid(wpa_s);
  1879. break;
  1880. }
  1881. l -= len;
  1882. p += len;
  1883. }
  1884. if (!found && data->assoc_info.req_ies)
  1885. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1886. #ifdef CONFIG_FILS
  1887. #ifdef CONFIG_SME
  1888. if ((wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
  1889. wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS) &&
  1890. (!data->assoc_info.resp_frame ||
  1891. fils_process_assoc_resp(wpa_s->wpa,
  1892. data->assoc_info.resp_frame,
  1893. data->assoc_info.resp_frame_len) < 0)) {
  1894. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
  1895. return -1;
  1896. }
  1897. #endif /* CONFIG_SME */
  1898. /* Additional processing for FILS when SME is in driver */
  1899. if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS &&
  1900. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1901. wpa_sm_set_reset_fils_completed(wpa_s->wpa, 1);
  1902. #endif /* CONFIG_FILS */
  1903. #ifdef CONFIG_OWE
  1904. if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
  1905. owe_process_assoc_resp(wpa_s->wpa,
  1906. data->assoc_info.resp_ies,
  1907. data->assoc_info.resp_ies_len) < 0) {
  1908. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
  1909. return -1;
  1910. }
  1911. #endif /* CONFIG_OWE */
  1912. #ifdef CONFIG_IEEE80211R
  1913. #ifdef CONFIG_SME
  1914. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  1915. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1916. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1917. data->assoc_info.resp_ies,
  1918. data->assoc_info.resp_ies_len,
  1919. bssid) < 0) {
  1920. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1921. "Reassociation Response failed");
  1922. wpa_supplicant_deauthenticate(
  1923. wpa_s, WLAN_REASON_INVALID_IE);
  1924. return -1;
  1925. }
  1926. }
  1927. p = data->assoc_info.resp_ies;
  1928. l = data->assoc_info.resp_ies_len;
  1929. #ifdef CONFIG_WPS_STRICT
  1930. if (p && wpa_s->current_ssid &&
  1931. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1932. struct wpabuf *wps;
  1933. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  1934. if (wps == NULL) {
  1935. wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
  1936. "include WPS IE in (Re)Association Response");
  1937. return -1;
  1938. }
  1939. if (wps_validate_assoc_resp(wps) < 0) {
  1940. wpabuf_free(wps);
  1941. wpa_supplicant_deauthenticate(
  1942. wpa_s, WLAN_REASON_INVALID_IE);
  1943. return -1;
  1944. }
  1945. wpabuf_free(wps);
  1946. }
  1947. #endif /* CONFIG_WPS_STRICT */
  1948. /* Go through the IEs and make a copy of the MDIE, if present. */
  1949. while (p && l >= 2) {
  1950. len = p[1] + 2;
  1951. if (len > l) {
  1952. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1953. p, l);
  1954. break;
  1955. }
  1956. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  1957. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  1958. wpa_s->sme.ft_used = 1;
  1959. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  1960. MOBILITY_DOMAIN_ID_LEN);
  1961. break;
  1962. }
  1963. l -= len;
  1964. p += len;
  1965. }
  1966. #endif /* CONFIG_SME */
  1967. /* Process FT when SME is in the driver */
  1968. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1969. wpa_ft_is_completed(wpa_s->wpa)) {
  1970. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1971. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1972. data->assoc_info.resp_ies,
  1973. data->assoc_info.resp_ies_len,
  1974. bssid) < 0) {
  1975. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1976. "Reassociation Response failed");
  1977. wpa_supplicant_deauthenticate(
  1978. wpa_s, WLAN_REASON_INVALID_IE);
  1979. return -1;
  1980. }
  1981. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
  1982. }
  1983. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  1984. data->assoc_info.resp_ies_len);
  1985. #endif /* CONFIG_IEEE80211R */
  1986. /* WPA/RSN IE from Beacon/ProbeResp */
  1987. p = data->assoc_info.beacon_ies;
  1988. l = data->assoc_info.beacon_ies_len;
  1989. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  1990. */
  1991. wpa_found = rsn_found = 0;
  1992. while (p && l >= 2) {
  1993. len = p[1] + 2;
  1994. if (len > l) {
  1995. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  1996. p, l);
  1997. break;
  1998. }
  1999. if (!wpa_found &&
  2000. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  2001. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  2002. wpa_found = 1;
  2003. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  2004. }
  2005. if (!rsn_found &&
  2006. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  2007. rsn_found = 1;
  2008. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  2009. }
  2010. l -= len;
  2011. p += len;
  2012. }
  2013. if (!wpa_found && data->assoc_info.beacon_ies)
  2014. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  2015. if (!rsn_found && data->assoc_info.beacon_ies)
  2016. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  2017. if (wpa_found || rsn_found)
  2018. wpa_s->ap_ies_from_associnfo = 1;
  2019. if (wpa_s->assoc_freq && data->assoc_info.freq &&
  2020. wpa_s->assoc_freq != data->assoc_info.freq) {
  2021. wpa_printf(MSG_DEBUG, "Operating frequency changed from "
  2022. "%u to %u MHz",
  2023. wpa_s->assoc_freq, data->assoc_info.freq);
  2024. wpa_supplicant_update_scan_results(wpa_s);
  2025. }
  2026. wpa_s->assoc_freq = data->assoc_info.freq;
  2027. return 0;
  2028. }
  2029. static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
  2030. {
  2031. const u8 *bss_wpa = NULL, *bss_rsn = NULL;
  2032. if (!wpa_s->current_bss || !wpa_s->current_ssid)
  2033. return -1;
  2034. if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
  2035. return 0;
  2036. bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  2037. WPA_IE_VENDOR_TYPE);
  2038. bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
  2039. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  2040. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  2041. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  2042. bss_rsn ? 2 + bss_rsn[1] : 0))
  2043. return -1;
  2044. return 0;
  2045. }
  2046. static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
  2047. union wpa_event_data *data)
  2048. {
  2049. #ifdef CONFIG_FST
  2050. struct assoc_info *ai = data ? &data->assoc_info : NULL;
  2051. struct wpa_bss *bss = wpa_s->current_bss;
  2052. const u8 *ieprb, *iebcn;
  2053. wpabuf_free(wpa_s->received_mb_ies);
  2054. wpa_s->received_mb_ies = NULL;
  2055. if (ai &&
  2056. !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
  2057. wpa_printf(MSG_DEBUG,
  2058. "FST: MB IEs updated from Association Response frame");
  2059. return;
  2060. }
  2061. if (ai &&
  2062. !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
  2063. wpa_printf(MSG_DEBUG,
  2064. "FST: MB IEs updated from association event Beacon IEs");
  2065. return;
  2066. }
  2067. if (!bss)
  2068. return;
  2069. ieprb = (const u8 *) (bss + 1);
  2070. iebcn = ieprb + bss->ie_len;
  2071. if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
  2072. wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
  2073. else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
  2074. wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
  2075. #endif /* CONFIG_FST */
  2076. }
  2077. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  2078. union wpa_event_data *data)
  2079. {
  2080. u8 bssid[ETH_ALEN];
  2081. int ft_completed, already_authorized;
  2082. int new_bss = 0;
  2083. #ifdef CONFIG_AP
  2084. if (wpa_s->ap_iface) {
  2085. if (!data)
  2086. return;
  2087. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  2088. data->assoc_info.addr,
  2089. data->assoc_info.req_ies,
  2090. data->assoc_info.req_ies_len,
  2091. data->assoc_info.reassoc);
  2092. return;
  2093. }
  2094. #endif /* CONFIG_AP */
  2095. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  2096. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  2097. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  2098. return;
  2099. /*
  2100. * FILS authentication can share the same mechanism to mark the
  2101. * connection fully authenticated, so set ft_completed also based on
  2102. * FILS result.
  2103. */
  2104. if (!ft_completed)
  2105. ft_completed = wpa_fils_is_completed(wpa_s->wpa);
  2106. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  2107. wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
  2108. wpa_supplicant_deauthenticate(
  2109. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2110. return;
  2111. }
  2112. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  2113. if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
  2114. wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  2115. MACSTR, MAC2STR(bssid));
  2116. new_bss = 1;
  2117. random_add_randomness(bssid, ETH_ALEN);
  2118. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  2119. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  2120. wpas_notify_bssid_changed(wpa_s);
  2121. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  2122. wpa_clear_keys(wpa_s, bssid);
  2123. }
  2124. if (wpa_supplicant_select_config(wpa_s) < 0) {
  2125. wpa_supplicant_deauthenticate(
  2126. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2127. return;
  2128. }
  2129. }
  2130. if (wpa_s->conf->ap_scan == 1 &&
  2131. wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
  2132. if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
  2133. wpa_msg(wpa_s, MSG_WARNING,
  2134. "WPA/RSN IEs not updated");
  2135. }
  2136. wpas_fst_update_mb_assoc(wpa_s, data);
  2137. #ifdef CONFIG_SME
  2138. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  2139. wpa_s->sme.prev_bssid_set = 1;
  2140. wpa_s->sme.last_unprot_disconnect.sec = 0;
  2141. #endif /* CONFIG_SME */
  2142. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  2143. if (wpa_s->current_ssid) {
  2144. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  2145. * initialized before association, but for other modes,
  2146. * initialize PC/SC here, if the current configuration needs
  2147. * smartcard or SIM/USIM. */
  2148. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  2149. }
  2150. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  2151. if (wpa_s->l2)
  2152. l2_packet_notify_auth_start(wpa_s->l2);
  2153. already_authorized = data && data->assoc_info.authorized;
  2154. /*
  2155. * Set portEnabled first to FALSE in order to get EAP state machine out
  2156. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  2157. * state machine may transit to AUTHENTICATING state based on obsolete
  2158. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  2159. * AUTHENTICATED without ever giving chance to EAP state machine to
  2160. * reset the state.
  2161. */
  2162. if (!ft_completed && !already_authorized) {
  2163. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  2164. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  2165. }
  2166. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  2167. wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
  2168. wpa_s->key_mgmt == WPA_KEY_MGMT_OWE || ft_completed ||
  2169. already_authorized)
  2170. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  2171. /* 802.1X::portControl = Auto */
  2172. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  2173. wpa_s->eapol_received = 0;
  2174. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  2175. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  2176. (wpa_s->current_ssid &&
  2177. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  2178. if (wpa_s->current_ssid &&
  2179. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
  2180. (wpa_s->drv_flags &
  2181. WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
  2182. /*
  2183. * Set the key after having received joined-IBSS event
  2184. * from the driver.
  2185. */
  2186. wpa_supplicant_set_wpa_none_key(wpa_s,
  2187. wpa_s->current_ssid);
  2188. }
  2189. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2190. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  2191. } else if (!ft_completed) {
  2192. /* Timeout for receiving the first EAPOL packet */
  2193. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  2194. }
  2195. wpa_supplicant_cancel_scan(wpa_s);
  2196. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2197. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  2198. /*
  2199. * We are done; the driver will take care of RSN 4-way
  2200. * handshake.
  2201. */
  2202. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2203. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  2204. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  2205. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  2206. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2207. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  2208. /*
  2209. * The driver will take care of RSN 4-way handshake, so we need
  2210. * to allow EAPOL supplicant to complete its work without
  2211. * waiting for WPA supplicant.
  2212. */
  2213. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  2214. } else if (ft_completed) {
  2215. /*
  2216. * FT protocol completed - make sure EAPOL state machine ends
  2217. * up in authenticated.
  2218. */
  2219. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2220. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  2221. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  2222. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  2223. }
  2224. wpa_s->last_eapol_matches_bssid = 0;
  2225. if (wpa_s->pending_eapol_rx) {
  2226. struct os_reltime now, age;
  2227. os_get_reltime(&now);
  2228. os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  2229. if (age.sec == 0 && age.usec < 200000 &&
  2230. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  2231. 0) {
  2232. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
  2233. "frame that was received just before "
  2234. "association notification");
  2235. wpa_supplicant_rx_eapol(
  2236. wpa_s, wpa_s->pending_eapol_rx_src,
  2237. wpabuf_head(wpa_s->pending_eapol_rx),
  2238. wpabuf_len(wpa_s->pending_eapol_rx));
  2239. }
  2240. wpabuf_free(wpa_s->pending_eapol_rx);
  2241. wpa_s->pending_eapol_rx = NULL;
  2242. }
  2243. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  2244. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  2245. wpa_s->current_ssid &&
  2246. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
  2247. /* Set static WEP keys again */
  2248. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  2249. }
  2250. #ifdef CONFIG_IBSS_RSN
  2251. if (wpa_s->current_ssid &&
  2252. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  2253. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  2254. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
  2255. wpa_s->ibss_rsn == NULL) {
  2256. wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
  2257. if (!wpa_s->ibss_rsn) {
  2258. wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
  2259. wpa_supplicant_deauthenticate(
  2260. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2261. return;
  2262. }
  2263. ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
  2264. }
  2265. #endif /* CONFIG_IBSS_RSN */
  2266. wpas_wps_notify_assoc(wpa_s, bssid);
  2267. if (data) {
  2268. wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
  2269. data->assoc_info.resp_ies_len,
  2270. &data->assoc_info.wmm_params);
  2271. if (wpa_s->reassoc_same_bss)
  2272. wmm_ac_restore_tspecs(wpa_s);
  2273. }
  2274. #ifdef CONFIG_FILS
  2275. if (wpa_key_mgmt_fils(wpa_s->key_mgmt)) {
  2276. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, bssid);
  2277. const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
  2278. if (fils_cache_id)
  2279. wpa_sm_set_fils_cache_id(wpa_s->wpa, fils_cache_id);
  2280. }
  2281. #endif /* CONFIG_FILS */
  2282. }
  2283. static int disconnect_reason_recoverable(u16 reason_code)
  2284. {
  2285. return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
  2286. reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
  2287. reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
  2288. }
  2289. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  2290. u16 reason_code,
  2291. int locally_generated)
  2292. {
  2293. const u8 *bssid;
  2294. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  2295. /*
  2296. * At least Host AP driver and a Prism3 card seemed to be
  2297. * generating streams of disconnected events when configuring
  2298. * IBSS for WPA-None. Ignore them for now.
  2299. */
  2300. return;
  2301. }
  2302. bssid = wpa_s->bssid;
  2303. if (is_zero_ether_addr(bssid))
  2304. bssid = wpa_s->pending_bssid;
  2305. if (!is_zero_ether_addr(bssid) ||
  2306. wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2307. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  2308. " reason=%d%s",
  2309. MAC2STR(bssid), reason_code,
  2310. locally_generated ? " locally_generated=1" : "");
  2311. }
  2312. }
  2313. static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
  2314. int locally_generated)
  2315. {
  2316. if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
  2317. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  2318. return 0; /* Not in 4-way handshake with PSK */
  2319. /*
  2320. * It looks like connection was lost while trying to go through PSK
  2321. * 4-way handshake. Filter out known disconnection cases that are caused
  2322. * by something else than PSK mismatch to avoid confusing reports.
  2323. */
  2324. if (locally_generated) {
  2325. if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
  2326. return 0;
  2327. }
  2328. return 1;
  2329. }
  2330. static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
  2331. u16 reason_code,
  2332. int locally_generated)
  2333. {
  2334. const u8 *bssid;
  2335. int authenticating;
  2336. u8 prev_pending_bssid[ETH_ALEN];
  2337. struct wpa_bss *fast_reconnect = NULL;
  2338. struct wpa_ssid *fast_reconnect_ssid = NULL;
  2339. struct wpa_ssid *last_ssid;
  2340. struct wpa_bss *curr = NULL;
  2341. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  2342. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  2343. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  2344. /*
  2345. * At least Host AP driver and a Prism3 card seemed to be
  2346. * generating streams of disconnected events when configuring
  2347. * IBSS for WPA-None. Ignore them for now.
  2348. */
  2349. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
  2350. "IBSS/WPA-None mode");
  2351. return;
  2352. }
  2353. if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
  2354. reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
  2355. locally_generated)
  2356. /*
  2357. * Remove the inactive AP (which is probably out of range) from
  2358. * the BSS list after marking disassociation. In particular
  2359. * mac80211-based drivers use the
  2360. * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
  2361. * locally generated disconnection events for cases where the
  2362. * AP does not reply anymore.
  2363. */
  2364. curr = wpa_s->current_bss;
  2365. if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
  2366. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  2367. "pre-shared key may be incorrect");
  2368. if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
  2369. return; /* P2P group removed */
  2370. wpas_auth_failed(wpa_s, "WRONG_KEY");
  2371. }
  2372. if (!wpa_s->disconnected &&
  2373. (!wpa_s->auto_reconnect_disabled ||
  2374. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
  2375. wpas_wps_searching(wpa_s) ||
  2376. wpas_wps_reenable_networks_pending(wpa_s))) {
  2377. wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
  2378. "reconnect (wps=%d/%d wpa_state=%d)",
  2379. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
  2380. wpas_wps_searching(wpa_s),
  2381. wpa_s->wpa_state);
  2382. if (wpa_s->wpa_state == WPA_COMPLETED &&
  2383. wpa_s->current_ssid &&
  2384. wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  2385. !locally_generated &&
  2386. disconnect_reason_recoverable(reason_code)) {
  2387. /*
  2388. * It looks like the AP has dropped association with
  2389. * us, but could allow us to get back in. Try to
  2390. * reconnect to the same BSS without full scan to save
  2391. * time for some common cases.
  2392. */
  2393. fast_reconnect = wpa_s->current_bss;
  2394. fast_reconnect_ssid = wpa_s->current_ssid;
  2395. } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  2396. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  2397. else
  2398. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
  2399. "immediate scan");
  2400. } else {
  2401. wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
  2402. "try to re-connect");
  2403. wpa_s->reassociate = 0;
  2404. wpa_s->disconnected = 1;
  2405. if (!wpa_s->pno)
  2406. wpa_supplicant_cancel_sched_scan(wpa_s);
  2407. }
  2408. bssid = wpa_s->bssid;
  2409. if (is_zero_ether_addr(bssid))
  2410. bssid = wpa_s->pending_bssid;
  2411. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2412. wpas_connection_failed(wpa_s, bssid);
  2413. wpa_sm_notify_disassoc(wpa_s->wpa);
  2414. if (locally_generated)
  2415. wpa_s->disconnect_reason = -reason_code;
  2416. else
  2417. wpa_s->disconnect_reason = reason_code;
  2418. wpas_notify_disconnect_reason(wpa_s);
  2419. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  2420. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
  2421. wpa_clear_keys(wpa_s, wpa_s->bssid);
  2422. }
  2423. last_ssid = wpa_s->current_ssid;
  2424. wpa_supplicant_mark_disassoc(wpa_s);
  2425. if (curr)
  2426. wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
  2427. if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  2428. sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
  2429. wpa_s->current_ssid = last_ssid;
  2430. }
  2431. if (fast_reconnect &&
  2432. !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
  2433. !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
  2434. !disallowed_ssid(wpa_s, fast_reconnect->ssid,
  2435. fast_reconnect->ssid_len) &&
  2436. !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
  2437. !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect->bssid)) {
  2438. #ifndef CONFIG_NO_SCAN_PROCESSING
  2439. wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
  2440. if (wpa_supplicant_connect(wpa_s, fast_reconnect,
  2441. fast_reconnect_ssid) < 0) {
  2442. /* Recover through full scan */
  2443. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  2444. }
  2445. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2446. } else if (fast_reconnect) {
  2447. /*
  2448. * Could not reconnect to the same BSS due to network being
  2449. * disabled. Use a new scan to match the alternative behavior
  2450. * above, i.e., to continue automatic reconnection attempt in a
  2451. * way that enforces disabled network rules.
  2452. */
  2453. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  2454. }
  2455. }
  2456. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  2457. void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
  2458. {
  2459. struct wpa_supplicant *wpa_s = eloop_ctx;
  2460. if (!wpa_s->pending_mic_error_report)
  2461. return;
  2462. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
  2463. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  2464. wpa_s->pending_mic_error_report = 0;
  2465. }
  2466. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  2467. static void
  2468. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  2469. union wpa_event_data *data)
  2470. {
  2471. int pairwise;
  2472. struct os_reltime t;
  2473. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  2474. pairwise = (data && data->michael_mic_failure.unicast);
  2475. os_get_reltime(&t);
  2476. if ((wpa_s->last_michael_mic_error.sec &&
  2477. !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
  2478. wpa_s->pending_mic_error_report) {
  2479. if (wpa_s->pending_mic_error_report) {
  2480. /*
  2481. * Send the pending MIC error report immediately since
  2482. * we are going to start countermeasures and AP better
  2483. * do the same.
  2484. */
  2485. wpa_sm_key_request(wpa_s->wpa, 1,
  2486. wpa_s->pending_mic_error_pairwise);
  2487. }
  2488. /* Send the new MIC error report immediately since we are going
  2489. * to start countermeasures and AP better do the same.
  2490. */
  2491. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  2492. /* initialize countermeasures */
  2493. wpa_s->countermeasures = 1;
  2494. wpa_blacklist_add(wpa_s, wpa_s->bssid);
  2495. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  2496. /*
  2497. * Need to wait for completion of request frame. We do not get
  2498. * any callback for the message completion, so just wait a
  2499. * short while and hope for the best. */
  2500. os_sleep(0, 10000);
  2501. wpa_drv_set_countermeasures(wpa_s, 1);
  2502. wpa_supplicant_deauthenticate(wpa_s,
  2503. WLAN_REASON_MICHAEL_MIC_FAILURE);
  2504. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  2505. wpa_s, NULL);
  2506. eloop_register_timeout(60, 0,
  2507. wpa_supplicant_stop_countermeasures,
  2508. wpa_s, NULL);
  2509. /* TODO: mark the AP rejected for 60 second. STA is
  2510. * allowed to associate with another AP.. */
  2511. } else {
  2512. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  2513. if (wpa_s->mic_errors_seen) {
  2514. /*
  2515. * Reduce the effectiveness of Michael MIC error
  2516. * reports as a means for attacking against TKIP if
  2517. * more than one MIC failure is noticed with the same
  2518. * PTK. We delay the transmission of the reports by a
  2519. * random time between 0 and 60 seconds in order to
  2520. * force the attacker wait 60 seconds before getting
  2521. * the information on whether a frame resulted in a MIC
  2522. * failure.
  2523. */
  2524. u8 rval[4];
  2525. int sec;
  2526. if (os_get_random(rval, sizeof(rval)) < 0)
  2527. sec = os_random() % 60;
  2528. else
  2529. sec = WPA_GET_BE32(rval) % 60;
  2530. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
  2531. "report %d seconds", sec);
  2532. wpa_s->pending_mic_error_report = 1;
  2533. wpa_s->pending_mic_error_pairwise = pairwise;
  2534. eloop_cancel_timeout(
  2535. wpa_supplicant_delayed_mic_error_report,
  2536. wpa_s, NULL);
  2537. eloop_register_timeout(
  2538. sec, os_random() % 1000000,
  2539. wpa_supplicant_delayed_mic_error_report,
  2540. wpa_s, NULL);
  2541. } else {
  2542. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  2543. }
  2544. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  2545. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  2546. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  2547. }
  2548. wpa_s->last_michael_mic_error = t;
  2549. wpa_s->mic_errors_seen++;
  2550. }
  2551. #ifdef CONFIG_TERMINATE_ONLASTIF
  2552. static int any_interfaces(struct wpa_supplicant *head)
  2553. {
  2554. struct wpa_supplicant *wpa_s;
  2555. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  2556. if (!wpa_s->interface_removed)
  2557. return 1;
  2558. return 0;
  2559. }
  2560. #endif /* CONFIG_TERMINATE_ONLASTIF */
  2561. static void
  2562. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  2563. union wpa_event_data *data)
  2564. {
  2565. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  2566. return;
  2567. switch (data->interface_status.ievent) {
  2568. case EVENT_INTERFACE_ADDED:
  2569. if (!wpa_s->interface_removed)
  2570. break;
  2571. wpa_s->interface_removed = 0;
  2572. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
  2573. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  2574. wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
  2575. "driver after interface was added");
  2576. }
  2577. #ifdef CONFIG_P2P
  2578. if (!wpa_s->global->p2p &&
  2579. !wpa_s->global->p2p_disabled &&
  2580. !wpa_s->conf->p2p_disabled &&
  2581. (wpa_s->drv_flags &
  2582. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2583. wpas_p2p_add_p2pdev_interface(
  2584. wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
  2585. wpa_printf(MSG_INFO,
  2586. "P2P: Failed to enable P2P Device interface");
  2587. /* Try to continue without. P2P will be disabled. */
  2588. }
  2589. #endif /* CONFIG_P2P */
  2590. break;
  2591. case EVENT_INTERFACE_REMOVED:
  2592. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
  2593. wpa_s->interface_removed = 1;
  2594. wpa_supplicant_mark_disassoc(wpa_s);
  2595. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2596. l2_packet_deinit(wpa_s->l2);
  2597. wpa_s->l2 = NULL;
  2598. #ifdef CONFIG_P2P
  2599. if (wpa_s->global->p2p &&
  2600. wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
  2601. (wpa_s->drv_flags &
  2602. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
  2603. wpa_dbg(wpa_s, MSG_DEBUG,
  2604. "Removing P2P Device interface");
  2605. wpa_supplicant_remove_iface(
  2606. wpa_s->global, wpa_s->global->p2p_init_wpa_s,
  2607. 0);
  2608. wpa_s->global->p2p_init_wpa_s = NULL;
  2609. }
  2610. #endif /* CONFIG_P2P */
  2611. #ifdef CONFIG_MATCH_IFACE
  2612. if (wpa_s->matched) {
  2613. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  2614. break;
  2615. }
  2616. #endif /* CONFIG_MATCH_IFACE */
  2617. #ifdef CONFIG_TERMINATE_ONLASTIF
  2618. /* check if last interface */
  2619. if (!any_interfaces(wpa_s->global->ifaces))
  2620. eloop_terminate();
  2621. #endif /* CONFIG_TERMINATE_ONLASTIF */
  2622. break;
  2623. }
  2624. }
  2625. #ifdef CONFIG_PEERKEY
  2626. static void
  2627. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  2628. union wpa_event_data *data)
  2629. {
  2630. if (data == NULL)
  2631. return;
  2632. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  2633. }
  2634. #endif /* CONFIG_PEERKEY */
  2635. #ifdef CONFIG_TDLS
  2636. static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
  2637. union wpa_event_data *data)
  2638. {
  2639. if (data == NULL)
  2640. return;
  2641. switch (data->tdls.oper) {
  2642. case TDLS_REQUEST_SETUP:
  2643. wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
  2644. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  2645. wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
  2646. else
  2647. wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
  2648. break;
  2649. case TDLS_REQUEST_TEARDOWN:
  2650. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  2651. wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
  2652. data->tdls.reason_code);
  2653. else
  2654. wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
  2655. data->tdls.peer);
  2656. break;
  2657. case TDLS_REQUEST_DISCOVER:
  2658. wpa_tdls_send_discovery_request(wpa_s->wpa,
  2659. data->tdls.peer);
  2660. break;
  2661. }
  2662. }
  2663. #endif /* CONFIG_TDLS */
  2664. #ifdef CONFIG_WNM
  2665. static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
  2666. union wpa_event_data *data)
  2667. {
  2668. if (data == NULL)
  2669. return;
  2670. switch (data->wnm.oper) {
  2671. case WNM_OPER_SLEEP:
  2672. wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
  2673. "(action=%d, intval=%d)",
  2674. data->wnm.sleep_action, data->wnm.sleep_intval);
  2675. ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
  2676. data->wnm.sleep_intval, NULL);
  2677. break;
  2678. }
  2679. }
  2680. #endif /* CONFIG_WNM */
  2681. #ifdef CONFIG_IEEE80211R
  2682. static void
  2683. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  2684. union wpa_event_data *data)
  2685. {
  2686. if (data == NULL)
  2687. return;
  2688. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  2689. data->ft_ies.ies_len,
  2690. data->ft_ies.ft_action,
  2691. data->ft_ies.target_ap,
  2692. data->ft_ies.ric_ies,
  2693. data->ft_ies.ric_ies_len) < 0) {
  2694. /* TODO: prevent MLME/driver from trying to associate? */
  2695. }
  2696. }
  2697. #endif /* CONFIG_IEEE80211R */
  2698. #ifdef CONFIG_IBSS_RSN
  2699. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  2700. union wpa_event_data *data)
  2701. {
  2702. struct wpa_ssid *ssid;
  2703. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  2704. return;
  2705. if (data == NULL)
  2706. return;
  2707. ssid = wpa_s->current_ssid;
  2708. if (ssid == NULL)
  2709. return;
  2710. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  2711. return;
  2712. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  2713. }
  2714. static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
  2715. union wpa_event_data *data)
  2716. {
  2717. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2718. if (ssid == NULL)
  2719. return;
  2720. /* check if the ssid is correctly configured as IBSS/RSN */
  2721. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  2722. return;
  2723. ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
  2724. data->rx_mgmt.frame_len);
  2725. }
  2726. #endif /* CONFIG_IBSS_RSN */
  2727. #ifdef CONFIG_IEEE80211R
  2728. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  2729. size_t len)
  2730. {
  2731. const u8 *sta_addr, *target_ap_addr;
  2732. u16 status;
  2733. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  2734. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  2735. return; /* only SME case supported for now */
  2736. if (len < 1 + 2 * ETH_ALEN + 2)
  2737. return;
  2738. if (data[0] != 2)
  2739. return; /* Only FT Action Response is supported for now */
  2740. sta_addr = data + 1;
  2741. target_ap_addr = data + 1 + ETH_ALEN;
  2742. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  2743. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
  2744. MACSTR " TargetAP " MACSTR " status %u",
  2745. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  2746. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  2747. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  2748. " in FT Action Response", MAC2STR(sta_addr));
  2749. return;
  2750. }
  2751. if (status) {
  2752. wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
  2753. "failure (status code %d)", status);
  2754. /* TODO: report error to FT code(?) */
  2755. return;
  2756. }
  2757. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  2758. len - (1 + 2 * ETH_ALEN + 2), 1,
  2759. target_ap_addr, NULL, 0) < 0)
  2760. return;
  2761. #ifdef CONFIG_SME
  2762. {
  2763. struct wpa_bss *bss;
  2764. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  2765. if (bss)
  2766. wpa_s->sme.freq = bss->freq;
  2767. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  2768. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  2769. WLAN_AUTH_FT);
  2770. }
  2771. #endif /* CONFIG_SME */
  2772. }
  2773. #endif /* CONFIG_IEEE80211R */
  2774. static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
  2775. struct unprot_deauth *e)
  2776. {
  2777. #ifdef CONFIG_IEEE80211W
  2778. wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
  2779. "dropped: " MACSTR " -> " MACSTR
  2780. " (reason code %u)",
  2781. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  2782. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  2783. #endif /* CONFIG_IEEE80211W */
  2784. }
  2785. static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
  2786. struct unprot_disassoc *e)
  2787. {
  2788. #ifdef CONFIG_IEEE80211W
  2789. wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
  2790. "dropped: " MACSTR " -> " MACSTR
  2791. " (reason code %u)",
  2792. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  2793. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  2794. #endif /* CONFIG_IEEE80211W */
  2795. }
  2796. static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
  2797. u16 reason_code, int locally_generated,
  2798. const u8 *ie, size_t ie_len, int deauth)
  2799. {
  2800. #ifdef CONFIG_AP
  2801. if (wpa_s->ap_iface && addr) {
  2802. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
  2803. return;
  2804. }
  2805. if (wpa_s->ap_iface) {
  2806. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
  2807. return;
  2808. }
  2809. #endif /* CONFIG_AP */
  2810. if (!locally_generated)
  2811. wpa_s->own_disconnect_req = 0;
  2812. wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
  2813. if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
  2814. ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  2815. (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
  2816. eapol_sm_failed(wpa_s->eapol))) &&
  2817. !wpa_s->eap_expected_failure))
  2818. wpas_auth_failed(wpa_s, "AUTH_FAILED");
  2819. #ifdef CONFIG_P2P
  2820. if (deauth && reason_code > 0) {
  2821. if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
  2822. locally_generated) > 0) {
  2823. /*
  2824. * The interface was removed, so cannot continue
  2825. * processing any additional operations after this.
  2826. */
  2827. return;
  2828. }
  2829. }
  2830. #endif /* CONFIG_P2P */
  2831. wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
  2832. locally_generated);
  2833. }
  2834. static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
  2835. struct disassoc_info *info)
  2836. {
  2837. u16 reason_code = 0;
  2838. int locally_generated = 0;
  2839. const u8 *addr = NULL;
  2840. const u8 *ie = NULL;
  2841. size_t ie_len = 0;
  2842. wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
  2843. if (info) {
  2844. addr = info->addr;
  2845. ie = info->ie;
  2846. ie_len = info->ie_len;
  2847. reason_code = info->reason_code;
  2848. locally_generated = info->locally_generated;
  2849. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
  2850. locally_generated ? " (locally generated)" : "");
  2851. if (addr)
  2852. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  2853. MAC2STR(addr));
  2854. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  2855. ie, ie_len);
  2856. }
  2857. #ifdef CONFIG_AP
  2858. if (wpa_s->ap_iface && info && info->addr) {
  2859. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
  2860. return;
  2861. }
  2862. if (wpa_s->ap_iface) {
  2863. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
  2864. return;
  2865. }
  2866. #endif /* CONFIG_AP */
  2867. #ifdef CONFIG_P2P
  2868. if (info) {
  2869. wpas_p2p_disassoc_notif(
  2870. wpa_s, info->addr, reason_code, info->ie, info->ie_len,
  2871. locally_generated);
  2872. }
  2873. #endif /* CONFIG_P2P */
  2874. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2875. sme_event_disassoc(wpa_s, info);
  2876. wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
  2877. ie, ie_len, 0);
  2878. }
  2879. static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
  2880. struct deauth_info *info)
  2881. {
  2882. u16 reason_code = 0;
  2883. int locally_generated = 0;
  2884. const u8 *addr = NULL;
  2885. const u8 *ie = NULL;
  2886. size_t ie_len = 0;
  2887. wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
  2888. if (info) {
  2889. addr = info->addr;
  2890. ie = info->ie;
  2891. ie_len = info->ie_len;
  2892. reason_code = info->reason_code;
  2893. locally_generated = info->locally_generated;
  2894. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
  2895. reason_code,
  2896. locally_generated ? " (locally generated)" : "");
  2897. if (addr) {
  2898. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  2899. MAC2STR(addr));
  2900. }
  2901. wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
  2902. ie, ie_len);
  2903. }
  2904. wpa_reset_ft_completed(wpa_s->wpa);
  2905. wpas_event_disconnect(wpa_s, addr, reason_code,
  2906. locally_generated, ie, ie_len, 1);
  2907. }
  2908. static const char * reg_init_str(enum reg_change_initiator init)
  2909. {
  2910. switch (init) {
  2911. case REGDOM_SET_BY_CORE:
  2912. return "CORE";
  2913. case REGDOM_SET_BY_USER:
  2914. return "USER";
  2915. case REGDOM_SET_BY_DRIVER:
  2916. return "DRIVER";
  2917. case REGDOM_SET_BY_COUNTRY_IE:
  2918. return "COUNTRY_IE";
  2919. case REGDOM_BEACON_HINT:
  2920. return "BEACON_HINT";
  2921. }
  2922. return "?";
  2923. }
  2924. static const char * reg_type_str(enum reg_type type)
  2925. {
  2926. switch (type) {
  2927. case REGDOM_TYPE_UNKNOWN:
  2928. return "UNKNOWN";
  2929. case REGDOM_TYPE_COUNTRY:
  2930. return "COUNTRY";
  2931. case REGDOM_TYPE_WORLD:
  2932. return "WORLD";
  2933. case REGDOM_TYPE_CUSTOM_WORLD:
  2934. return "CUSTOM_WORLD";
  2935. case REGDOM_TYPE_INTERSECTION:
  2936. return "INTERSECTION";
  2937. }
  2938. return "?";
  2939. }
  2940. static void wpa_supplicant_update_channel_list(
  2941. struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
  2942. {
  2943. struct wpa_supplicant *ifs;
  2944. u8 dfs_domain;
  2945. /*
  2946. * To allow backwards compatibility with higher level layers that
  2947. * assumed the REGDOM_CHANGE event is sent over the initially added
  2948. * interface. Find the highest parent of this interface and use it to
  2949. * send the event.
  2950. */
  2951. for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
  2952. ;
  2953. wpa_msg(ifs, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
  2954. reg_init_str(info->initiator), reg_type_str(info->type),
  2955. info->alpha2[0] ? " alpha2=" : "",
  2956. info->alpha2[0] ? info->alpha2 : "");
  2957. if (wpa_s->drv_priv == NULL)
  2958. return; /* Ignore event during drv initialization */
  2959. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  2960. radio_list) {
  2961. wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
  2962. ifs->ifname);
  2963. free_hw_features(ifs);
  2964. ifs->hw.modes = wpa_drv_get_hw_feature_data(
  2965. ifs, &ifs->hw.num_modes, &ifs->hw.flags, &dfs_domain);
  2966. /* Restart PNO/sched_scan with updated channel list */
  2967. if (ifs->pno) {
  2968. wpas_stop_pno(ifs);
  2969. wpas_start_pno(ifs);
  2970. } else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
  2971. wpa_dbg(ifs, MSG_DEBUG,
  2972. "Channel list changed - restart sched_scan");
  2973. wpas_scan_restart_sched_scan(ifs);
  2974. }
  2975. }
  2976. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
  2977. }
  2978. static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
  2979. const u8 *frame, size_t len, int freq,
  2980. int rssi)
  2981. {
  2982. const struct ieee80211_mgmt *mgmt;
  2983. const u8 *payload;
  2984. size_t plen;
  2985. u8 category;
  2986. if (len < IEEE80211_HDRLEN + 2)
  2987. return;
  2988. mgmt = (const struct ieee80211_mgmt *) frame;
  2989. payload = frame + IEEE80211_HDRLEN;
  2990. category = *payload++;
  2991. plen = len - IEEE80211_HDRLEN - 1;
  2992. wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
  2993. " Category=%u DataLen=%d freq=%d MHz",
  2994. MAC2STR(mgmt->sa), category, (int) plen, freq);
  2995. if (category == WLAN_ACTION_WMM) {
  2996. wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
  2997. return;
  2998. }
  2999. #ifdef CONFIG_IEEE80211R
  3000. if (category == WLAN_ACTION_FT) {
  3001. ft_rx_action(wpa_s, payload, plen);
  3002. return;
  3003. }
  3004. #endif /* CONFIG_IEEE80211R */
  3005. #ifdef CONFIG_IEEE80211W
  3006. #ifdef CONFIG_SME
  3007. if (category == WLAN_ACTION_SA_QUERY) {
  3008. sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
  3009. return;
  3010. }
  3011. #endif /* CONFIG_SME */
  3012. #endif /* CONFIG_IEEE80211W */
  3013. #ifdef CONFIG_WNM
  3014. if (mgmt->u.action.category == WLAN_ACTION_WNM) {
  3015. ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
  3016. return;
  3017. }
  3018. #endif /* CONFIG_WNM */
  3019. #ifdef CONFIG_GAS
  3020. if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
  3021. mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
  3022. gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
  3023. mgmt->u.action.category,
  3024. payload, plen, freq) == 0)
  3025. return;
  3026. #endif /* CONFIG_GAS */
  3027. #ifdef CONFIG_GAS_SERVER
  3028. if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
  3029. mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
  3030. gas_server_rx(wpa_s->gas_server, mgmt->da, mgmt->sa, mgmt->bssid,
  3031. mgmt->u.action.category,
  3032. payload, plen, freq) == 0)
  3033. return;
  3034. #endif /* CONFIG_GAS_SERVER */
  3035. #ifdef CONFIG_TDLS
  3036. if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
  3037. payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
  3038. wpa_dbg(wpa_s, MSG_DEBUG,
  3039. "TDLS: Received Discovery Response from " MACSTR,
  3040. MAC2STR(mgmt->sa));
  3041. return;
  3042. }
  3043. #endif /* CONFIG_TDLS */
  3044. #ifdef CONFIG_INTERWORKING
  3045. if (category == WLAN_ACTION_QOS && plen >= 1 &&
  3046. payload[0] == QOS_QOS_MAP_CONFIG) {
  3047. const u8 *pos = payload + 1;
  3048. size_t qlen = plen - 1;
  3049. wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
  3050. MACSTR, MAC2STR(mgmt->sa));
  3051. if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
  3052. qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
  3053. pos[1] <= qlen - 2 && pos[1] >= 16)
  3054. wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
  3055. return;
  3056. }
  3057. #endif /* CONFIG_INTERWORKING */
  3058. if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
  3059. payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
  3060. wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
  3061. mgmt->da,
  3062. payload + 1,
  3063. plen - 1);
  3064. return;
  3065. }
  3066. if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
  3067. payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
  3068. wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
  3069. return;
  3070. }
  3071. if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
  3072. payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
  3073. wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
  3074. payload + 1, plen - 1,
  3075. rssi);
  3076. return;
  3077. }
  3078. #ifdef CONFIG_FST
  3079. if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
  3080. fst_rx_action(wpa_s->fst, mgmt, len);
  3081. return;
  3082. }
  3083. #endif /* CONFIG_FST */
  3084. #ifdef CONFIG_DPP
  3085. if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
  3086. payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
  3087. WPA_GET_BE24(&payload[1]) == OUI_WFA &&
  3088. payload[4] == DPP_OUI_TYPE) {
  3089. payload += 5;
  3090. plen -= 5;
  3091. wpas_dpp_rx_action(wpa_s, mgmt->sa, payload, plen, freq);
  3092. return;
  3093. }
  3094. #endif /* CONFIG_DPP */
  3095. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  3096. category, payload, plen, freq);
  3097. if (wpa_s->ifmsh)
  3098. mesh_mpm_action_rx(wpa_s, mgmt, len);
  3099. }
  3100. static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
  3101. union wpa_event_data *event)
  3102. {
  3103. struct wpa_freq_range_list *list;
  3104. char *str = NULL;
  3105. list = &event->freq_range;
  3106. if (list->num)
  3107. str = freq_range_list_str(list);
  3108. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
  3109. str ? str : "");
  3110. #ifdef CONFIG_P2P
  3111. if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
  3112. wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
  3113. __func__);
  3114. } else {
  3115. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
  3116. /*
  3117. * The update channel flow will also take care of moving a GO
  3118. * from the unsafe frequency if needed.
  3119. */
  3120. wpas_p2p_update_channel_list(wpa_s,
  3121. WPAS_P2P_CHANNEL_UPDATE_AVOID);
  3122. }
  3123. #endif /* CONFIG_P2P */
  3124. os_free(str);
  3125. }
  3126. static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
  3127. union wpa_event_data *data)
  3128. {
  3129. wpa_dbg(wpa_s, MSG_DEBUG,
  3130. "Connection authorized by device, previous state %d",
  3131. wpa_s->wpa_state);
  3132. if (wpa_s->wpa_state == WPA_ASSOCIATED) {
  3133. wpa_supplicant_cancel_auth_timeout(wpa_s);
  3134. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  3135. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  3136. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  3137. }
  3138. wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
  3139. data->assoc_info.ptk_kck_len,
  3140. data->assoc_info.ptk_kek,
  3141. data->assoc_info.ptk_kek_len);
  3142. #ifdef CONFIG_FILS
  3143. if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
  3144. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
  3145. const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
  3146. /* Update ERP next sequence number */
  3147. eapol_sm_update_erp_next_seq_num(
  3148. wpa_s->eapol, data->assoc_info.fils_erp_next_seq_num);
  3149. if (data->assoc_info.fils_pmk && data->assoc_info.fils_pmkid) {
  3150. /* Add the new PMK and PMKID to the PMKSA cache */
  3151. wpa_sm_pmksa_cache_add(wpa_s->wpa,
  3152. data->assoc_info.fils_pmk,
  3153. data->assoc_info.fils_pmk_len,
  3154. data->assoc_info.fils_pmkid,
  3155. wpa_s->bssid, fils_cache_id);
  3156. } else if (data->assoc_info.fils_pmkid) {
  3157. /* Update the current PMKSA used for this connection */
  3158. pmksa_cache_set_current(wpa_s->wpa,
  3159. data->assoc_info.fils_pmkid,
  3160. NULL, NULL, 0, NULL);
  3161. }
  3162. } else {
  3163. wpa_sm_set_rx_replay_ctr(wpa_s->wpa,
  3164. data->assoc_info.key_replay_ctr);
  3165. }
  3166. #else /* CONFIG_FILS */
  3167. wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
  3168. #endif /* CONFIG_FILS */
  3169. }
  3170. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  3171. union wpa_event_data *data)
  3172. {
  3173. struct wpa_supplicant *wpa_s = ctx;
  3174. int resched;
  3175. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  3176. event != EVENT_INTERFACE_ENABLED &&
  3177. event != EVENT_INTERFACE_STATUS &&
  3178. event != EVENT_SCAN_RESULTS &&
  3179. event != EVENT_SCHED_SCAN_STOPPED) {
  3180. wpa_dbg(wpa_s, MSG_DEBUG,
  3181. "Ignore event %s (%d) while interface is disabled",
  3182. event_to_string(event), event);
  3183. return;
  3184. }
  3185. #ifndef CONFIG_NO_STDOUT_DEBUG
  3186. {
  3187. int level = MSG_DEBUG;
  3188. if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
  3189. const struct ieee80211_hdr *hdr;
  3190. u16 fc;
  3191. hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
  3192. fc = le_to_host16(hdr->frame_control);
  3193. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  3194. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  3195. level = MSG_EXCESSIVE;
  3196. }
  3197. wpa_dbg(wpa_s, level, "Event %s (%d) received",
  3198. event_to_string(event), event);
  3199. }
  3200. #endif /* CONFIG_NO_STDOUT_DEBUG */
  3201. switch (event) {
  3202. case EVENT_AUTH:
  3203. #ifdef CONFIG_FST
  3204. if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
  3205. data->auth.ies_len))
  3206. wpa_printf(MSG_DEBUG,
  3207. "FST: MB IEs updated from auth IE");
  3208. #endif /* CONFIG_FST */
  3209. sme_event_auth(wpa_s, data);
  3210. break;
  3211. case EVENT_ASSOC:
  3212. #ifdef CONFIG_TESTING_OPTIONS
  3213. if (wpa_s->ignore_auth_resp) {
  3214. wpa_printf(MSG_INFO,
  3215. "EVENT_ASSOC - ignore_auth_resp active!");
  3216. break;
  3217. }
  3218. #endif /* CONFIG_TESTING_OPTIONS */
  3219. wpa_supplicant_event_assoc(wpa_s, data);
  3220. if (data &&
  3221. (data->assoc_info.authorized ||
  3222. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  3223. wpa_fils_is_completed(wpa_s->wpa))))
  3224. wpa_supplicant_event_assoc_auth(wpa_s, data);
  3225. if (data) {
  3226. wpa_msg(wpa_s, MSG_INFO,
  3227. WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
  3228. data->assoc_info.subnet_status);
  3229. }
  3230. break;
  3231. case EVENT_DISASSOC:
  3232. wpas_event_disassoc(wpa_s,
  3233. data ? &data->disassoc_info : NULL);
  3234. break;
  3235. case EVENT_DEAUTH:
  3236. #ifdef CONFIG_TESTING_OPTIONS
  3237. if (wpa_s->ignore_auth_resp) {
  3238. wpa_printf(MSG_INFO,
  3239. "EVENT_DEAUTH - ignore_auth_resp active!");
  3240. break;
  3241. }
  3242. #endif /* CONFIG_TESTING_OPTIONS */
  3243. wpas_event_deauth(wpa_s,
  3244. data ? &data->deauth_info : NULL);
  3245. break;
  3246. case EVENT_MICHAEL_MIC_FAILURE:
  3247. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  3248. break;
  3249. #ifndef CONFIG_NO_SCAN_PROCESSING
  3250. case EVENT_SCAN_STARTED:
  3251. if (wpa_s->own_scan_requested ||
  3252. (data && !data->scan_info.external_scan)) {
  3253. struct os_reltime diff;
  3254. os_get_reltime(&wpa_s->scan_start_time);
  3255. os_reltime_sub(&wpa_s->scan_start_time,
  3256. &wpa_s->scan_trigger_time, &diff);
  3257. wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
  3258. diff.sec, diff.usec);
  3259. wpa_s->own_scan_requested = 0;
  3260. wpa_s->own_scan_running = 1;
  3261. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  3262. wpa_s->manual_scan_use_id) {
  3263. wpa_msg_ctrl(wpa_s, MSG_INFO,
  3264. WPA_EVENT_SCAN_STARTED "id=%u",
  3265. wpa_s->manual_scan_id);
  3266. } else {
  3267. wpa_msg_ctrl(wpa_s, MSG_INFO,
  3268. WPA_EVENT_SCAN_STARTED);
  3269. }
  3270. } else {
  3271. wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
  3272. wpa_s->radio->external_scan_running = 1;
  3273. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
  3274. }
  3275. break;
  3276. case EVENT_SCAN_RESULTS:
  3277. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3278. wpa_s->scan_res_handler = NULL;
  3279. wpa_s->own_scan_running = 0;
  3280. wpa_s->radio->external_scan_running = 0;
  3281. wpa_s->last_scan_req = NORMAL_SCAN_REQ;
  3282. break;
  3283. }
  3284. if (!(data && data->scan_info.external_scan) &&
  3285. os_reltime_initialized(&wpa_s->scan_start_time)) {
  3286. struct os_reltime now, diff;
  3287. os_get_reltime(&now);
  3288. os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
  3289. wpa_s->scan_start_time.sec = 0;
  3290. wpa_s->scan_start_time.usec = 0;
  3291. wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
  3292. diff.sec, diff.usec);
  3293. }
  3294. if (wpa_supplicant_event_scan_results(wpa_s, data))
  3295. break; /* interface may have been removed */
  3296. if (!(data && data->scan_info.external_scan))
  3297. wpa_s->own_scan_running = 0;
  3298. if (data && data->scan_info.nl_scan_event)
  3299. wpa_s->radio->external_scan_running = 0;
  3300. radio_work_check_next(wpa_s);
  3301. break;
  3302. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3303. case EVENT_ASSOCINFO:
  3304. wpa_supplicant_event_associnfo(wpa_s, data);
  3305. break;
  3306. case EVENT_INTERFACE_STATUS:
  3307. wpa_supplicant_event_interface_status(wpa_s, data);
  3308. break;
  3309. case EVENT_PMKID_CANDIDATE:
  3310. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  3311. break;
  3312. #ifdef CONFIG_PEERKEY
  3313. case EVENT_STKSTART:
  3314. wpa_supplicant_event_stkstart(wpa_s, data);
  3315. break;
  3316. #endif /* CONFIG_PEERKEY */
  3317. #ifdef CONFIG_TDLS
  3318. case EVENT_TDLS:
  3319. wpa_supplicant_event_tdls(wpa_s, data);
  3320. break;
  3321. #endif /* CONFIG_TDLS */
  3322. #ifdef CONFIG_WNM
  3323. case EVENT_WNM:
  3324. wpa_supplicant_event_wnm(wpa_s, data);
  3325. break;
  3326. #endif /* CONFIG_WNM */
  3327. #ifdef CONFIG_IEEE80211R
  3328. case EVENT_FT_RESPONSE:
  3329. wpa_supplicant_event_ft_response(wpa_s, data);
  3330. break;
  3331. #endif /* CONFIG_IEEE80211R */
  3332. #ifdef CONFIG_IBSS_RSN
  3333. case EVENT_IBSS_RSN_START:
  3334. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  3335. break;
  3336. #endif /* CONFIG_IBSS_RSN */
  3337. case EVENT_ASSOC_REJECT:
  3338. if (data->assoc_reject.bssid)
  3339. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  3340. "bssid=" MACSTR " status_code=%u%s%s%s",
  3341. MAC2STR(data->assoc_reject.bssid),
  3342. data->assoc_reject.status_code,
  3343. data->assoc_reject.timed_out ? " timeout" : "",
  3344. data->assoc_reject.timeout_reason ? "=" : "",
  3345. data->assoc_reject.timeout_reason ?
  3346. data->assoc_reject.timeout_reason : "");
  3347. else
  3348. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  3349. "status_code=%u%s%s%s",
  3350. data->assoc_reject.status_code,
  3351. data->assoc_reject.timed_out ? " timeout" : "",
  3352. data->assoc_reject.timeout_reason ? "=" : "",
  3353. data->assoc_reject.timeout_reason ?
  3354. data->assoc_reject.timeout_reason : "");
  3355. wpa_s->assoc_status_code = data->assoc_reject.status_code;
  3356. wpas_notify_assoc_status_code(wpa_s);
  3357. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  3358. sme_event_assoc_reject(wpa_s, data);
  3359. else {
  3360. const u8 *bssid = data->assoc_reject.bssid;
  3361. #ifdef CONFIG_FILS
  3362. /* Update ERP next sequence number */
  3363. if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS)
  3364. eapol_sm_update_erp_next_seq_num(
  3365. wpa_s->eapol,
  3366. data->assoc_reject.fils_erp_next_seq_num);
  3367. #endif /* CONFIG_FILS */
  3368. if (bssid == NULL || is_zero_ether_addr(bssid))
  3369. bssid = wpa_s->pending_bssid;
  3370. wpas_connection_failed(wpa_s, bssid);
  3371. wpa_supplicant_mark_disassoc(wpa_s);
  3372. }
  3373. break;
  3374. case EVENT_AUTH_TIMED_OUT:
  3375. /* It is possible to get this event from earlier connection */
  3376. if (wpa_s->current_ssid &&
  3377. wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
  3378. wpa_dbg(wpa_s, MSG_DEBUG,
  3379. "Ignore AUTH_TIMED_OUT in mesh configuration");
  3380. break;
  3381. }
  3382. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  3383. sme_event_auth_timed_out(wpa_s, data);
  3384. break;
  3385. case EVENT_ASSOC_TIMED_OUT:
  3386. /* It is possible to get this event from earlier connection */
  3387. if (wpa_s->current_ssid &&
  3388. wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
  3389. wpa_dbg(wpa_s, MSG_DEBUG,
  3390. "Ignore ASSOC_TIMED_OUT in mesh configuration");
  3391. break;
  3392. }
  3393. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  3394. sme_event_assoc_timed_out(wpa_s, data);
  3395. break;
  3396. case EVENT_TX_STATUS:
  3397. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
  3398. " type=%d stype=%d",
  3399. MAC2STR(data->tx_status.dst),
  3400. data->tx_status.type, data->tx_status.stype);
  3401. #ifdef CONFIG_AP
  3402. if (wpa_s->ap_iface == NULL) {
  3403. #ifdef CONFIG_OFFCHANNEL
  3404. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  3405. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  3406. offchannel_send_action_tx_status(
  3407. wpa_s, data->tx_status.dst,
  3408. data->tx_status.data,
  3409. data->tx_status.data_len,
  3410. data->tx_status.ack ?
  3411. OFFCHANNEL_SEND_ACTION_SUCCESS :
  3412. OFFCHANNEL_SEND_ACTION_NO_ACK);
  3413. #endif /* CONFIG_OFFCHANNEL */
  3414. break;
  3415. }
  3416. #endif /* CONFIG_AP */
  3417. #ifdef CONFIG_OFFCHANNEL
  3418. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
  3419. MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
  3420. /*
  3421. * Catch TX status events for Action frames we sent via group
  3422. * interface in GO mode, or via standalone AP interface.
  3423. * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
  3424. * except when the primary interface is used as a GO interface
  3425. * (for drivers which do not have group interface concurrency)
  3426. */
  3427. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  3428. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  3429. os_memcmp(wpa_s->p2pdev->pending_action_dst,
  3430. data->tx_status.dst, ETH_ALEN) == 0) {
  3431. offchannel_send_action_tx_status(
  3432. wpa_s->p2pdev, data->tx_status.dst,
  3433. data->tx_status.data,
  3434. data->tx_status.data_len,
  3435. data->tx_status.ack ?
  3436. OFFCHANNEL_SEND_ACTION_SUCCESS :
  3437. OFFCHANNEL_SEND_ACTION_NO_ACK);
  3438. break;
  3439. }
  3440. #endif /* CONFIG_OFFCHANNEL */
  3441. #ifdef CONFIG_AP
  3442. switch (data->tx_status.type) {
  3443. case WLAN_FC_TYPE_MGMT:
  3444. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  3445. data->tx_status.data_len,
  3446. data->tx_status.stype,
  3447. data->tx_status.ack);
  3448. break;
  3449. case WLAN_FC_TYPE_DATA:
  3450. ap_tx_status(wpa_s, data->tx_status.dst,
  3451. data->tx_status.data,
  3452. data->tx_status.data_len,
  3453. data->tx_status.ack);
  3454. break;
  3455. }
  3456. #endif /* CONFIG_AP */
  3457. break;
  3458. #ifdef CONFIG_AP
  3459. case EVENT_EAPOL_TX_STATUS:
  3460. ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
  3461. data->eapol_tx_status.data,
  3462. data->eapol_tx_status.data_len,
  3463. data->eapol_tx_status.ack);
  3464. break;
  3465. case EVENT_DRIVER_CLIENT_POLL_OK:
  3466. ap_client_poll_ok(wpa_s, data->client_poll.addr);
  3467. break;
  3468. case EVENT_RX_FROM_UNKNOWN:
  3469. if (wpa_s->ap_iface == NULL)
  3470. break;
  3471. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
  3472. data->rx_from_unknown.wds);
  3473. break;
  3474. case EVENT_CH_SWITCH:
  3475. if (!data || !wpa_s->current_ssid)
  3476. break;
  3477. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
  3478. "freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
  3479. data->ch_switch.freq,
  3480. data->ch_switch.ht_enabled,
  3481. data->ch_switch.ch_offset,
  3482. channel_width_to_string(data->ch_switch.ch_width),
  3483. data->ch_switch.cf1,
  3484. data->ch_switch.cf2);
  3485. wpa_s->assoc_freq = data->ch_switch.freq;
  3486. wpa_s->current_ssid->frequency = data->ch_switch.freq;
  3487. if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
  3488. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
  3489. wpa_s->current_ssid->mode ==
  3490. WPAS_MODE_P2P_GROUP_FORMATION) {
  3491. wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
  3492. data->ch_switch.ht_enabled,
  3493. data->ch_switch.ch_offset,
  3494. data->ch_switch.ch_width,
  3495. data->ch_switch.cf1,
  3496. data->ch_switch.cf2);
  3497. }
  3498. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
  3499. break;
  3500. #ifdef NEED_AP_MLME
  3501. case EVENT_DFS_RADAR_DETECTED:
  3502. if (data)
  3503. wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
  3504. break;
  3505. case EVENT_DFS_CAC_STARTED:
  3506. if (data)
  3507. wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
  3508. break;
  3509. case EVENT_DFS_CAC_FINISHED:
  3510. if (data)
  3511. wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
  3512. break;
  3513. case EVENT_DFS_CAC_ABORTED:
  3514. if (data)
  3515. wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
  3516. break;
  3517. case EVENT_DFS_NOP_FINISHED:
  3518. if (data)
  3519. wpas_event_dfs_cac_nop_finished(wpa_s,
  3520. &data->dfs_event);
  3521. break;
  3522. #endif /* NEED_AP_MLME */
  3523. #endif /* CONFIG_AP */
  3524. case EVENT_RX_MGMT: {
  3525. u16 fc, stype;
  3526. const struct ieee80211_mgmt *mgmt;
  3527. #ifdef CONFIG_TESTING_OPTIONS
  3528. if (wpa_s->ext_mgmt_frame_handling) {
  3529. struct rx_mgmt *rx = &data->rx_mgmt;
  3530. size_t hex_len = 2 * rx->frame_len + 1;
  3531. char *hex = os_malloc(hex_len);
  3532. if (hex) {
  3533. wpa_snprintf_hex(hex, hex_len,
  3534. rx->frame, rx->frame_len);
  3535. wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
  3536. rx->freq, rx->datarate, rx->ssi_signal,
  3537. hex);
  3538. os_free(hex);
  3539. }
  3540. break;
  3541. }
  3542. #endif /* CONFIG_TESTING_OPTIONS */
  3543. mgmt = (const struct ieee80211_mgmt *)
  3544. data->rx_mgmt.frame;
  3545. fc = le_to_host16(mgmt->frame_control);
  3546. stype = WLAN_FC_GET_STYPE(fc);
  3547. #ifdef CONFIG_AP
  3548. if (wpa_s->ap_iface == NULL) {
  3549. #endif /* CONFIG_AP */
  3550. #ifdef CONFIG_P2P
  3551. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  3552. data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
  3553. const u8 *src = mgmt->sa;
  3554. const u8 *ie;
  3555. size_t ie_len;
  3556. ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
  3557. ie_len = data->rx_mgmt.frame_len -
  3558. IEEE80211_HDRLEN;
  3559. wpas_p2p_probe_req_rx(
  3560. wpa_s, src, mgmt->da,
  3561. mgmt->bssid, ie, ie_len,
  3562. data->rx_mgmt.freq,
  3563. data->rx_mgmt.ssi_signal);
  3564. break;
  3565. }
  3566. #endif /* CONFIG_P2P */
  3567. #ifdef CONFIG_IBSS_RSN
  3568. if (wpa_s->current_ssid &&
  3569. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  3570. stype == WLAN_FC_STYPE_AUTH &&
  3571. data->rx_mgmt.frame_len >= 30) {
  3572. wpa_supplicant_event_ibss_auth(wpa_s, data);
  3573. break;
  3574. }
  3575. #endif /* CONFIG_IBSS_RSN */
  3576. if (stype == WLAN_FC_STYPE_ACTION) {
  3577. wpas_event_rx_mgmt_action(
  3578. wpa_s, data->rx_mgmt.frame,
  3579. data->rx_mgmt.frame_len,
  3580. data->rx_mgmt.freq,
  3581. data->rx_mgmt.ssi_signal);
  3582. break;
  3583. }
  3584. if (wpa_s->ifmsh) {
  3585. mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
  3586. break;
  3587. }
  3588. wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
  3589. "management frame in non-AP mode");
  3590. break;
  3591. #ifdef CONFIG_AP
  3592. }
  3593. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  3594. data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
  3595. const u8 *ie;
  3596. size_t ie_len;
  3597. ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
  3598. ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
  3599. wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
  3600. mgmt->bssid, ie, ie_len,
  3601. data->rx_mgmt.ssi_signal);
  3602. }
  3603. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  3604. #endif /* CONFIG_AP */
  3605. break;
  3606. }
  3607. case EVENT_RX_PROBE_REQ:
  3608. if (data->rx_probe_req.sa == NULL ||
  3609. data->rx_probe_req.ie == NULL)
  3610. break;
  3611. #ifdef CONFIG_AP
  3612. if (wpa_s->ap_iface) {
  3613. hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
  3614. data->rx_probe_req.sa,
  3615. data->rx_probe_req.da,
  3616. data->rx_probe_req.bssid,
  3617. data->rx_probe_req.ie,
  3618. data->rx_probe_req.ie_len,
  3619. data->rx_probe_req.ssi_signal);
  3620. break;
  3621. }
  3622. #endif /* CONFIG_AP */
  3623. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  3624. data->rx_probe_req.da,
  3625. data->rx_probe_req.bssid,
  3626. data->rx_probe_req.ie,
  3627. data->rx_probe_req.ie_len,
  3628. 0,
  3629. data->rx_probe_req.ssi_signal);
  3630. break;
  3631. case EVENT_REMAIN_ON_CHANNEL:
  3632. #ifdef CONFIG_OFFCHANNEL
  3633. offchannel_remain_on_channel_cb(
  3634. wpa_s, data->remain_on_channel.freq,
  3635. data->remain_on_channel.duration);
  3636. #endif /* CONFIG_OFFCHANNEL */
  3637. wpas_p2p_remain_on_channel_cb(
  3638. wpa_s, data->remain_on_channel.freq,
  3639. data->remain_on_channel.duration);
  3640. break;
  3641. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  3642. #ifdef CONFIG_OFFCHANNEL
  3643. offchannel_cancel_remain_on_channel_cb(
  3644. wpa_s, data->remain_on_channel.freq);
  3645. #endif /* CONFIG_OFFCHANNEL */
  3646. wpas_p2p_cancel_remain_on_channel_cb(
  3647. wpa_s, data->remain_on_channel.freq);
  3648. #ifdef CONFIG_DPP
  3649. wpas_dpp_cancel_remain_on_channel_cb(
  3650. wpa_s, data->remain_on_channel.freq);
  3651. #endif /* CONFIG_DPP */
  3652. break;
  3653. case EVENT_EAPOL_RX:
  3654. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  3655. data->eapol_rx.data,
  3656. data->eapol_rx.data_len);
  3657. break;
  3658. case EVENT_SIGNAL_CHANGE:
  3659. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
  3660. "above=%d signal=%d noise=%d txrate=%d",
  3661. data->signal_change.above_threshold,
  3662. data->signal_change.current_signal,
  3663. data->signal_change.current_noise,
  3664. data->signal_change.current_txrate);
  3665. wpa_bss_update_level(wpa_s->current_bss,
  3666. data->signal_change.current_signal);
  3667. bgscan_notify_signal_change(
  3668. wpa_s, data->signal_change.above_threshold,
  3669. data->signal_change.current_signal,
  3670. data->signal_change.current_noise,
  3671. data->signal_change.current_txrate);
  3672. break;
  3673. case EVENT_INTERFACE_ENABLED:
  3674. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
  3675. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3676. wpa_supplicant_update_mac_addr(wpa_s);
  3677. if (wpa_s->p2p_mgmt) {
  3678. wpa_supplicant_set_state(wpa_s,
  3679. WPA_DISCONNECTED);
  3680. break;
  3681. }
  3682. #ifdef CONFIG_AP
  3683. if (!wpa_s->ap_iface) {
  3684. wpa_supplicant_set_state(wpa_s,
  3685. WPA_DISCONNECTED);
  3686. wpa_s->scan_req = NORMAL_SCAN_REQ;
  3687. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3688. } else
  3689. wpa_supplicant_set_state(wpa_s,
  3690. WPA_COMPLETED);
  3691. #else /* CONFIG_AP */
  3692. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  3693. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3694. #endif /* CONFIG_AP */
  3695. }
  3696. break;
  3697. case EVENT_INTERFACE_DISABLED:
  3698. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
  3699. #ifdef CONFIG_P2P
  3700. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  3701. (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
  3702. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
  3703. /*
  3704. * Mark interface disabled if this happens to end up not
  3705. * being removed as a separate P2P group interface.
  3706. */
  3707. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  3708. /*
  3709. * The interface was externally disabled. Remove
  3710. * it assuming an external entity will start a
  3711. * new session if needed.
  3712. */
  3713. if (wpa_s->current_ssid &&
  3714. wpa_s->current_ssid->p2p_group)
  3715. wpas_p2p_interface_unavailable(wpa_s);
  3716. else
  3717. wpas_p2p_disconnect(wpa_s);
  3718. /*
  3719. * wpa_s instance may have been freed, so must not use
  3720. * it here anymore.
  3721. */
  3722. break;
  3723. }
  3724. if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
  3725. p2p_in_progress(wpa_s->global->p2p) > 1) {
  3726. /* This radio work will be cancelled, so clear P2P
  3727. * state as well.
  3728. */
  3729. p2p_stop_find(wpa_s->global->p2p);
  3730. }
  3731. #endif /* CONFIG_P2P */
  3732. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  3733. /*
  3734. * Indicate disconnection to keep ctrl_iface events
  3735. * consistent.
  3736. */
  3737. wpa_supplicant_event_disassoc(
  3738. wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
  3739. }
  3740. wpa_supplicant_mark_disassoc(wpa_s);
  3741. wpa_bss_flush(wpa_s);
  3742. radio_remove_works(wpa_s, NULL, 0);
  3743. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  3744. break;
  3745. case EVENT_CHANNEL_LIST_CHANGED:
  3746. wpa_supplicant_update_channel_list(
  3747. wpa_s, &data->channel_list_changed);
  3748. break;
  3749. case EVENT_INTERFACE_UNAVAILABLE:
  3750. wpas_p2p_interface_unavailable(wpa_s);
  3751. break;
  3752. case EVENT_BEST_CHANNEL:
  3753. wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
  3754. "(%d %d %d)",
  3755. data->best_chan.freq_24, data->best_chan.freq_5,
  3756. data->best_chan.freq_overall);
  3757. wpa_s->best_24_freq = data->best_chan.freq_24;
  3758. wpa_s->best_5_freq = data->best_chan.freq_5;
  3759. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  3760. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  3761. data->best_chan.freq_5,
  3762. data->best_chan.freq_overall);
  3763. break;
  3764. case EVENT_UNPROT_DEAUTH:
  3765. wpa_supplicant_event_unprot_deauth(wpa_s,
  3766. &data->unprot_deauth);
  3767. break;
  3768. case EVENT_UNPROT_DISASSOC:
  3769. wpa_supplicant_event_unprot_disassoc(wpa_s,
  3770. &data->unprot_disassoc);
  3771. break;
  3772. case EVENT_STATION_LOW_ACK:
  3773. #ifdef CONFIG_AP
  3774. if (wpa_s->ap_iface && data)
  3775. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  3776. data->low_ack.addr);
  3777. #endif /* CONFIG_AP */
  3778. #ifdef CONFIG_TDLS
  3779. if (data)
  3780. wpa_tdls_disable_unreachable_link(wpa_s->wpa,
  3781. data->low_ack.addr);
  3782. #endif /* CONFIG_TDLS */
  3783. break;
  3784. case EVENT_IBSS_PEER_LOST:
  3785. #ifdef CONFIG_IBSS_RSN
  3786. ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
  3787. #endif /* CONFIG_IBSS_RSN */
  3788. break;
  3789. case EVENT_DRIVER_GTK_REKEY:
  3790. if (os_memcmp(data->driver_gtk_rekey.bssid,
  3791. wpa_s->bssid, ETH_ALEN))
  3792. break;
  3793. if (!wpa_s->wpa)
  3794. break;
  3795. wpa_sm_update_replay_ctr(wpa_s->wpa,
  3796. data->driver_gtk_rekey.replay_ctr);
  3797. break;
  3798. case EVENT_SCHED_SCAN_STOPPED:
  3799. wpa_s->sched_scanning = 0;
  3800. resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
  3801. wpa_supplicant_notify_scanning(wpa_s, 0);
  3802. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3803. break;
  3804. /*
  3805. * If the driver stopped scanning without being requested to,
  3806. * request a new scan to continue scanning for networks.
  3807. */
  3808. if (!wpa_s->sched_scan_stop_req &&
  3809. wpa_s->wpa_state == WPA_SCANNING) {
  3810. wpa_dbg(wpa_s, MSG_DEBUG,
  3811. "Restart scanning after unexpected sched_scan stop event");
  3812. wpa_supplicant_req_scan(wpa_s, 1, 0);
  3813. break;
  3814. }
  3815. wpa_s->sched_scan_stop_req = 0;
  3816. /*
  3817. * Start a new sched scan to continue searching for more SSIDs
  3818. * either if timed out or PNO schedule scan is pending.
  3819. */
  3820. if (wpa_s->sched_scan_timed_out) {
  3821. wpa_supplicant_req_sched_scan(wpa_s);
  3822. } else if (wpa_s->pno_sched_pending) {
  3823. wpa_s->pno_sched_pending = 0;
  3824. wpas_start_pno(wpa_s);
  3825. } else if (resched) {
  3826. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3827. }
  3828. break;
  3829. case EVENT_WPS_BUTTON_PUSHED:
  3830. #ifdef CONFIG_WPS
  3831. wpas_wps_start_pbc(wpa_s, NULL, 0);
  3832. #endif /* CONFIG_WPS */
  3833. break;
  3834. case EVENT_AVOID_FREQUENCIES:
  3835. wpa_supplicant_notify_avoid_freq(wpa_s, data);
  3836. break;
  3837. case EVENT_CONNECT_FAILED_REASON:
  3838. #ifdef CONFIG_AP
  3839. if (!wpa_s->ap_iface || !data)
  3840. break;
  3841. hostapd_event_connect_failed_reason(
  3842. wpa_s->ap_iface->bss[0],
  3843. data->connect_failed_reason.addr,
  3844. data->connect_failed_reason.code);
  3845. #endif /* CONFIG_AP */
  3846. break;
  3847. case EVENT_NEW_PEER_CANDIDATE:
  3848. #ifdef CONFIG_MESH
  3849. if (!wpa_s->ifmsh || !data)
  3850. break;
  3851. wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
  3852. data->mesh_peer.ies,
  3853. data->mesh_peer.ie_len);
  3854. #endif /* CONFIG_MESH */
  3855. break;
  3856. case EVENT_SURVEY:
  3857. #ifdef CONFIG_AP
  3858. if (!wpa_s->ap_iface)
  3859. break;
  3860. hostapd_event_get_survey(wpa_s->ap_iface,
  3861. &data->survey_results);
  3862. #endif /* CONFIG_AP */
  3863. break;
  3864. case EVENT_ACS_CHANNEL_SELECTED:
  3865. #ifdef CONFIG_AP
  3866. #ifdef CONFIG_ACS
  3867. if (!wpa_s->ap_iface)
  3868. break;
  3869. hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
  3870. &data->acs_selected_channels);
  3871. #endif /* CONFIG_ACS */
  3872. #endif /* CONFIG_AP */
  3873. break;
  3874. case EVENT_P2P_LO_STOP:
  3875. #ifdef CONFIG_P2P
  3876. wpa_s->p2p_lo_started = 0;
  3877. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
  3878. P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
  3879. data->p2p_lo_stop.reason_code);
  3880. #endif /* CONFIG_P2P */
  3881. break;
  3882. case EVENT_BEACON_LOSS:
  3883. if (!wpa_s->current_bss || !wpa_s->current_ssid)
  3884. break;
  3885. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
  3886. bgscan_notify_beacon_loss(wpa_s);
  3887. break;
  3888. default:
  3889. wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
  3890. break;
  3891. }
  3892. }
  3893. void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
  3894. union wpa_event_data *data)
  3895. {
  3896. struct wpa_supplicant *wpa_s;
  3897. if (event != EVENT_INTERFACE_STATUS)
  3898. return;
  3899. wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
  3900. if (wpa_s && wpa_s->driver->get_ifindex) {
  3901. unsigned int ifindex;
  3902. ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
  3903. if (ifindex != data->interface_status.ifindex) {
  3904. wpa_dbg(wpa_s, MSG_DEBUG,
  3905. "interface status ifindex %d mismatch (%d)",
  3906. ifindex, data->interface_status.ifindex);
  3907. return;
  3908. }
  3909. }
  3910. #ifdef CONFIG_MATCH_IFACE
  3911. else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
  3912. struct wpa_interface *wpa_i;
  3913. wpa_i = wpa_supplicant_match_iface(
  3914. ctx, data->interface_status.ifname);
  3915. if (!wpa_i)
  3916. return;
  3917. wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
  3918. os_free(wpa_i);
  3919. if (wpa_s)
  3920. wpa_s->matched = 1;
  3921. }
  3922. #endif /* CONFIG_MATCH_IFACE */
  3923. if (wpa_s)
  3924. wpa_supplicant_event(wpa_s, event, data);
  3925. }