events.c 93 KB

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