events.c 96 KB

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