events.c 93 KB

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