events.c 81 KB

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