events.c 97 KB

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