events.c 97 KB

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