events.c 108 KB

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