events.c 68 KB

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