events.c 109 KB

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