events.c 93 KB

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