events.c 95 KB

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