events.c 103 KB

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