events.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2010, 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 "p2p_supplicant.h"
  40. #include "bgscan.h"
  41. #include "ap.h"
  42. #include "bss.h"
  43. #include "mlme.h"
  44. #include "scan.h"
  45. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  46. {
  47. struct wpa_ssid *ssid, *old_ssid;
  48. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  49. return 0;
  50. wpa_printf(MSG_DEBUG, "Select network based on association "
  51. "information");
  52. ssid = wpa_supplicant_get_ssid(wpa_s);
  53. if (ssid == NULL) {
  54. wpa_printf(MSG_INFO, "No network configuration found for the "
  55. "current AP");
  56. return -1;
  57. }
  58. if (ssid->disabled) {
  59. wpa_printf(MSG_DEBUG, "Selected network is disabled");
  60. return -1;
  61. }
  62. wpa_printf(MSG_DEBUG, "Network configuration found for the current "
  63. "AP");
  64. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  65. WPA_KEY_MGMT_WPA_NONE |
  66. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
  67. WPA_KEY_MGMT_PSK_SHA256 |
  68. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  69. u8 wpa_ie[80];
  70. size_t wpa_ie_len = sizeof(wpa_ie);
  71. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  72. wpa_ie, &wpa_ie_len);
  73. } else {
  74. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  75. }
  76. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  77. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  78. old_ssid = wpa_s->current_ssid;
  79. wpa_s->current_ssid = ssid;
  80. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  81. wpa_supplicant_initiate_eapol(wpa_s);
  82. if (old_ssid != wpa_s->current_ssid)
  83. wpas_notify_network_changed(wpa_s);
  84. return 0;
  85. }
  86. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  87. void *sock_ctx)
  88. {
  89. struct wpa_supplicant *wpa_s = eloop_ctx;
  90. if (wpa_s->countermeasures) {
  91. wpa_s->countermeasures = 0;
  92. wpa_drv_set_countermeasures(wpa_s, 0);
  93. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  94. wpa_supplicant_req_scan(wpa_s, 0, 0);
  95. }
  96. }
  97. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  98. {
  99. int bssid_changed;
  100. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  101. return;
  102. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  103. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  104. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  105. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  106. wpa_s->current_bss = NULL;
  107. wpa_s->assoc_freq = 0;
  108. if (bssid_changed)
  109. wpas_notify_bssid_changed(wpa_s);
  110. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  111. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  112. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  113. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  114. wpa_s->ap_ies_from_associnfo = 0;
  115. }
  116. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  117. {
  118. struct wpa_ie_data ie;
  119. int pmksa_set = -1;
  120. size_t i;
  121. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  122. ie.pmkid == NULL)
  123. return;
  124. for (i = 0; i < ie.num_pmkid; i++) {
  125. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  126. ie.pmkid + i * PMKID_LEN,
  127. NULL, NULL, 0);
  128. if (pmksa_set == 0) {
  129. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  130. break;
  131. }
  132. }
  133. wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
  134. "cache", pmksa_set == 0 ? "" : "not ");
  135. }
  136. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  137. union wpa_event_data *data)
  138. {
  139. if (data == NULL) {
  140. wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
  141. return;
  142. }
  143. wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  144. " index=%d preauth=%d",
  145. MAC2STR(data->pmkid_candidate.bssid),
  146. data->pmkid_candidate.index,
  147. data->pmkid_candidate.preauth);
  148. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  149. data->pmkid_candidate.index,
  150. data->pmkid_candidate.preauth);
  151. }
  152. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  153. {
  154. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  155. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  156. return 0;
  157. #ifdef IEEE8021X_EAPOL
  158. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  159. wpa_s->current_ssid &&
  160. !(wpa_s->current_ssid->eapol_flags &
  161. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  162. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  163. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  164. * plaintext or static WEP keys). */
  165. return 0;
  166. }
  167. #endif /* IEEE8021X_EAPOL */
  168. return 1;
  169. }
  170. /**
  171. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  172. * @wpa_s: pointer to wpa_supplicant data
  173. * @ssid: Configuration data for the network
  174. * Returns: 0 on success, -1 on failure
  175. *
  176. * This function is called when starting authentication with a network that is
  177. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  178. */
  179. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  180. struct wpa_ssid *ssid)
  181. {
  182. #ifdef IEEE8021X_EAPOL
  183. int aka = 0, sim = 0, type;
  184. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  185. return 0;
  186. if (ssid->eap.eap_methods == NULL) {
  187. sim = 1;
  188. aka = 1;
  189. } else {
  190. struct eap_method_type *eap = ssid->eap.eap_methods;
  191. while (eap->vendor != EAP_VENDOR_IETF ||
  192. eap->method != EAP_TYPE_NONE) {
  193. if (eap->vendor == EAP_VENDOR_IETF) {
  194. if (eap->method == EAP_TYPE_SIM)
  195. sim = 1;
  196. else if (eap->method == EAP_TYPE_AKA)
  197. aka = 1;
  198. }
  199. eap++;
  200. }
  201. }
  202. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  203. sim = 0;
  204. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  205. aka = 0;
  206. if (!sim && !aka) {
  207. wpa_printf(MSG_DEBUG, "Selected network is configured to use "
  208. "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
  209. return 0;
  210. }
  211. wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
  212. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  213. if (sim && aka)
  214. type = SCARD_TRY_BOTH;
  215. else if (aka)
  216. type = SCARD_USIM_ONLY;
  217. else
  218. type = SCARD_GSM_SIM_ONLY;
  219. wpa_s->scard = scard_init(type);
  220. if (wpa_s->scard == NULL) {
  221. wpa_printf(MSG_WARNING, "Failed to initialize SIM "
  222. "(pcsc-lite)");
  223. return -1;
  224. }
  225. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  226. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  227. #endif /* IEEE8021X_EAPOL */
  228. return 0;
  229. }
  230. #ifndef CONFIG_NO_SCAN_PROCESSING
  231. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  232. struct wpa_ssid *ssid)
  233. {
  234. int i, privacy = 0;
  235. if (ssid->mixed_cell)
  236. return 1;
  237. #ifdef CONFIG_WPS
  238. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  239. return 1;
  240. #endif /* CONFIG_WPS */
  241. for (i = 0; i < NUM_WEP_KEYS; i++) {
  242. if (ssid->wep_key_len[i]) {
  243. privacy = 1;
  244. break;
  245. }
  246. }
  247. #ifdef IEEE8021X_EAPOL
  248. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  249. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  250. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  251. privacy = 1;
  252. #endif /* IEEE8021X_EAPOL */
  253. if (bss->caps & IEEE80211_CAP_PRIVACY)
  254. return privacy;
  255. return !privacy;
  256. }
  257. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  258. struct wpa_ssid *ssid,
  259. struct wpa_scan_res *bss)
  260. {
  261. struct wpa_ie_data ie;
  262. int proto_match = 0;
  263. const u8 *rsn_ie, *wpa_ie;
  264. int ret;
  265. int wep_ok;
  266. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  267. if (ret >= 0)
  268. return ret;
  269. /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
  270. wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  271. (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  272. ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
  273. (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
  274. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  275. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  276. proto_match++;
  277. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  278. wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
  279. break;
  280. }
  281. if (wep_ok &&
  282. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  283. {
  284. wpa_printf(MSG_DEBUG, " selected based on TSN in "
  285. "RSN IE");
  286. return 1;
  287. }
  288. if (!(ie.proto & ssid->proto)) {
  289. wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
  290. "mismatch");
  291. break;
  292. }
  293. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  294. wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
  295. "mismatch");
  296. break;
  297. }
  298. if (!(ie.group_cipher & ssid->group_cipher)) {
  299. wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
  300. "mismatch");
  301. break;
  302. }
  303. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  304. wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
  305. "mismatch");
  306. break;
  307. }
  308. #ifdef CONFIG_IEEE80211W
  309. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  310. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  311. wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
  312. "protection");
  313. break;
  314. }
  315. #endif /* CONFIG_IEEE80211W */
  316. wpa_printf(MSG_DEBUG, " selected based on RSN IE");
  317. return 1;
  318. }
  319. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  320. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  321. proto_match++;
  322. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  323. wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
  324. break;
  325. }
  326. if (wep_ok &&
  327. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  328. {
  329. wpa_printf(MSG_DEBUG, " selected based on TSN in "
  330. "WPA IE");
  331. return 1;
  332. }
  333. if (!(ie.proto & ssid->proto)) {
  334. wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
  335. "mismatch");
  336. break;
  337. }
  338. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  339. wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
  340. "mismatch");
  341. break;
  342. }
  343. if (!(ie.group_cipher & ssid->group_cipher)) {
  344. wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
  345. "mismatch");
  346. break;
  347. }
  348. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  349. wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
  350. "mismatch");
  351. break;
  352. }
  353. wpa_printf(MSG_DEBUG, " selected based on WPA IE");
  354. return 1;
  355. }
  356. if (proto_match == 0)
  357. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  358. return 0;
  359. }
  360. static int freq_allowed(int *freqs, int freq)
  361. {
  362. int i;
  363. if (freqs == NULL)
  364. return 1;
  365. for (i = 0; freqs[i]; i++)
  366. if (freqs[i] == freq)
  367. return 1;
  368. return 0;
  369. }
  370. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  371. int i, struct wpa_scan_res *bss,
  372. struct wpa_ssid *group)
  373. {
  374. const u8 *ssid_;
  375. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  376. int wpa;
  377. struct wpa_blacklist *e;
  378. const u8 *ie;
  379. struct wpa_ssid *ssid;
  380. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  381. ssid_ = ie ? ie + 2 : (u8 *) "";
  382. ssid_len = ie ? ie[1] : 0;
  383. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  384. wpa_ie_len = ie ? ie[1] : 0;
  385. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  386. rsn_ie_len = ie ? ie[1] : 0;
  387. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  388. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
  389. i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
  390. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  391. wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
  392. " wps" : "");
  393. e = wpa_blacklist_get(wpa_s, bss->bssid);
  394. if (e) {
  395. int limit = 1;
  396. if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
  397. /*
  398. * When only a single network is enabled, we can
  399. * trigger blacklisting on the first failure. This
  400. * should not be done with multiple enabled networks to
  401. * avoid getting forced to move into a worse ESS on
  402. * single error if there are no other BSSes of the
  403. * current ESS.
  404. */
  405. limit = 0;
  406. }
  407. if (e->count > limit) {
  408. wpa_printf(MSG_DEBUG, " skip - blacklisted "
  409. "(count=%d limit=%d)", e->count, limit);
  410. return 0;
  411. }
  412. }
  413. if (ssid_len == 0) {
  414. wpa_printf(MSG_DEBUG, " skip - SSID not known");
  415. return 0;
  416. }
  417. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  418. for (ssid = group; ssid; ssid = ssid->pnext) {
  419. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  420. if (ssid->disabled) {
  421. wpa_printf(MSG_DEBUG, " skip - disabled");
  422. continue;
  423. }
  424. #ifdef CONFIG_WPS
  425. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  426. wpa_printf(MSG_DEBUG, " skip - blacklisted (WPS)");
  427. continue;
  428. }
  429. if (wpa && ssid->ssid_len == 0 &&
  430. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  431. check_ssid = 0;
  432. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  433. /* Only allow wildcard SSID match if an AP
  434. * advertises active WPS operation that matches
  435. * with our mode. */
  436. check_ssid = 1;
  437. if (ssid->ssid_len == 0 &&
  438. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  439. check_ssid = 0;
  440. }
  441. #endif /* CONFIG_WPS */
  442. if (check_ssid &&
  443. (ssid_len != ssid->ssid_len ||
  444. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  445. wpa_printf(MSG_DEBUG, " skip - SSID mismatch");
  446. continue;
  447. }
  448. if (ssid->bssid_set &&
  449. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  450. wpa_printf(MSG_DEBUG, " skip - BSSID mismatch");
  451. continue;
  452. }
  453. if (wpa && !wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  454. continue;
  455. if (!wpa &&
  456. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  457. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  458. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  459. wpa_printf(MSG_DEBUG, " skip - non-WPA network not "
  460. "allowed");
  461. continue;
  462. }
  463. if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
  464. wpa_printf(MSG_DEBUG, " skip - privacy mismatch");
  465. continue;
  466. }
  467. if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
  468. wpa_printf(MSG_DEBUG, " skip - IBSS (adhoc) "
  469. "network");
  470. continue;
  471. }
  472. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  473. wpa_printf(MSG_DEBUG, " skip - frequency not "
  474. "allowed");
  475. continue;
  476. }
  477. #ifdef CONFIG_P2P
  478. /*
  479. * TODO: skip the AP if its P2P IE has Group Formation
  480. * bit set in the P2P Group Capability Bitmap and we
  481. * are not in Group Formation with that device.
  482. */
  483. #endif /* CONFIG_P2P */
  484. /* Matching configuration found */
  485. return ssid;
  486. }
  487. /* No matching configuration found */
  488. return 0;
  489. }
  490. static struct wpa_bss *
  491. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  492. struct wpa_scan_results *scan_res,
  493. struct wpa_ssid *group,
  494. struct wpa_ssid **selected_ssid)
  495. {
  496. size_t i;
  497. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  498. group->priority);
  499. for (i = 0; i < scan_res->num; i++) {
  500. struct wpa_scan_res *bss = scan_res->res[i];
  501. const u8 *ie, *ssid;
  502. u8 ssid_len;
  503. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
  504. if (!*selected_ssid)
  505. continue;
  506. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  507. ssid = ie ? ie + 2 : (u8 *) "";
  508. ssid_len = ie ? ie[1] : 0;
  509. wpa_printf(MSG_DEBUG, " selected BSS " MACSTR " ssid='%s'",
  510. MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  511. return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
  512. }
  513. return NULL;
  514. }
  515. static struct wpa_bss *
  516. wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  517. struct wpa_scan_results *scan_res,
  518. struct wpa_ssid **selected_ssid)
  519. {
  520. struct wpa_bss *selected = NULL;
  521. int prio;
  522. while (selected == NULL) {
  523. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  524. selected = wpa_supplicant_select_bss(
  525. wpa_s, scan_res, wpa_s->conf->pssid[prio],
  526. selected_ssid);
  527. if (selected)
  528. break;
  529. }
  530. if (selected == NULL && wpa_s->blacklist) {
  531. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  532. "and try again");
  533. wpa_blacklist_clear(wpa_s);
  534. wpa_s->blacklist_cleared++;
  535. } else if (selected == NULL)
  536. break;
  537. }
  538. return selected;
  539. }
  540. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  541. int timeout_sec, int timeout_usec)
  542. {
  543. if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
  544. /*
  545. * No networks are enabled; short-circuit request so
  546. * we don't wait timeout seconds before transitioning
  547. * to INACTIVE state.
  548. */
  549. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  550. return;
  551. }
  552. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  553. }
  554. void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  555. struct wpa_bss *selected,
  556. struct wpa_ssid *ssid)
  557. {
  558. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  559. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  560. "PBC session overlap");
  561. #ifdef CONFIG_P2P
  562. if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
  563. return;
  564. #endif /* CONFIG_P2P */
  565. #ifdef CONFIG_WPS
  566. wpas_wps_cancel(wpa_s);
  567. #endif /* CONFIG_WPS */
  568. return;
  569. }
  570. /*
  571. * Do not trigger new association unless the BSSID has changed or if
  572. * reassociation is requested. If we are in process of associating with
  573. * the selected BSSID, do not trigger new attempt.
  574. */
  575. if (wpa_s->reassociate ||
  576. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  577. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  578. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  579. 0))) {
  580. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  581. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  582. return;
  583. }
  584. wpa_supplicant_associate(wpa_s, selected, ssid);
  585. } else {
  586. wpa_printf(MSG_DEBUG, "Already associated with the selected "
  587. "AP");
  588. }
  589. }
  590. static struct wpa_ssid *
  591. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  592. {
  593. int prio;
  594. struct wpa_ssid *ssid;
  595. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  596. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  597. {
  598. if (ssid->disabled)
  599. continue;
  600. if (ssid->mode == IEEE80211_MODE_IBSS ||
  601. ssid->mode == IEEE80211_MODE_AP)
  602. return ssid;
  603. }
  604. }
  605. return NULL;
  606. }
  607. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  608. * on BSS added and BSS changed events */
  609. static void wpa_supplicant_rsn_preauth_scan_results(
  610. struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
  611. {
  612. int i;
  613. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  614. return;
  615. for (i = scan_res->num - 1; i >= 0; i--) {
  616. const u8 *ssid, *rsn;
  617. struct wpa_scan_res *r;
  618. r = scan_res->res[i];
  619. ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
  620. if (ssid == NULL)
  621. continue;
  622. rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
  623. if (rsn == NULL)
  624. continue;
  625. rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
  626. }
  627. }
  628. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  629. struct wpa_bss *selected,
  630. struct wpa_ssid *ssid,
  631. struct wpa_scan_results *scan_res)
  632. {
  633. size_t i;
  634. struct wpa_scan_res *current_bss = NULL;
  635. int min_diff;
  636. if (wpa_s->reassociate)
  637. return 1; /* explicit request to reassociate */
  638. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  639. return 1; /* we are not associated; continue */
  640. if (wpa_s->current_ssid == NULL)
  641. return 1; /* unknown current SSID */
  642. if (wpa_s->current_ssid != ssid)
  643. return 1; /* different network block */
  644. for (i = 0; i < scan_res->num; i++) {
  645. struct wpa_scan_res *res = scan_res->res[i];
  646. const u8 *ie;
  647. if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  648. continue;
  649. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  650. if (ie == NULL)
  651. continue;
  652. if (ie[1] != wpa_s->current_ssid->ssid_len ||
  653. os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
  654. continue;
  655. current_bss = res;
  656. break;
  657. }
  658. if (!current_bss)
  659. return 1; /* current BSS not seen in scan results */
  660. wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
  661. wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  662. MAC2STR(current_bss->bssid), current_bss->level);
  663. wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  664. MAC2STR(selected->bssid), selected->level);
  665. if (wpa_s->current_ssid->bssid_set &&
  666. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  667. 0) {
  668. wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
  669. "preferred BSSID");
  670. return 1;
  671. }
  672. min_diff = 2;
  673. if (current_bss->level < 0) {
  674. if (current_bss->level < -85)
  675. min_diff = 1;
  676. else if (current_bss->level < -80)
  677. min_diff = 2;
  678. else if (current_bss->level < -75)
  679. min_diff = 3;
  680. else if (current_bss->level < -70)
  681. min_diff = 4;
  682. else
  683. min_diff = 5;
  684. }
  685. if (abs(current_bss->level - selected->level) < min_diff) {
  686. wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
  687. "signal level");
  688. return 0;
  689. }
  690. return 1;
  691. }
  692. static void _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  693. union wpa_event_data *data)
  694. {
  695. struct wpa_bss *selected;
  696. struct wpa_ssid *ssid = NULL;
  697. struct wpa_scan_results *scan_res;
  698. int ap = 0;
  699. #ifdef CONFIG_AP
  700. if (wpa_s->ap_iface)
  701. ap = 1;
  702. #endif /* CONFIG_AP */
  703. wpa_supplicant_notify_scanning(wpa_s, 0);
  704. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  705. data ? &data->scan_info :
  706. NULL, 1);
  707. if (scan_res == NULL) {
  708. if (wpa_s->conf->ap_scan == 2 || ap)
  709. return;
  710. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  711. "scanning again");
  712. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  713. return;
  714. }
  715. #ifndef CONFIG_NO_RANDOM_POOL
  716. size_t i, num;
  717. num = scan_res->num;
  718. if (num > 10)
  719. num = 10;
  720. for (i = 0; i < num; i++) {
  721. u8 buf[5];
  722. struct wpa_scan_res *res = scan_res->res[i];
  723. buf[0] = res->bssid[5];
  724. buf[1] = res->qual & 0xff;
  725. buf[2] = res->noise & 0xff;
  726. buf[3] = res->level & 0xff;
  727. buf[4] = res->tsf & 0xff;
  728. random_add_randomness(buf, sizeof(buf));
  729. }
  730. #endif /* CONFIG_NO_RANDOM_POOL */
  731. if (wpa_s->scan_res_handler) {
  732. wpa_s->scan_res_handler(wpa_s, scan_res);
  733. wpa_s->scan_res_handler = NULL;
  734. wpa_scan_results_free(scan_res);
  735. return;
  736. }
  737. if (ap) {
  738. wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
  739. wpa_scan_results_free(scan_res);
  740. return;
  741. }
  742. wpa_printf(MSG_DEBUG, "%s: New scan results available", wpa_s->ifname);
  743. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  744. wpas_notify_scan_results(wpa_s);
  745. wpas_notify_scan_done(wpa_s, 1);
  746. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  747. wpa_scan_results_free(scan_res);
  748. return;
  749. }
  750. if (wpa_s->disconnected) {
  751. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  752. wpa_scan_results_free(scan_res);
  753. return;
  754. }
  755. if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
  756. wpa_scan_results_free(scan_res);
  757. return;
  758. }
  759. wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
  760. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  761. if (selected) {
  762. int skip;
  763. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  764. scan_res);
  765. wpa_scan_results_free(scan_res);
  766. if (skip)
  767. return;
  768. wpa_supplicant_connect(wpa_s, selected, ssid);
  769. } else {
  770. wpa_scan_results_free(scan_res);
  771. wpa_printf(MSG_DEBUG, "No suitable network found");
  772. ssid = wpa_supplicant_pick_new_network(wpa_s);
  773. if (ssid) {
  774. wpa_printf(MSG_DEBUG, "Setup a new network");
  775. wpa_supplicant_associate(wpa_s, NULL, ssid);
  776. } else {
  777. int timeout_sec = 5;
  778. int timeout_usec = 0;
  779. #ifdef CONFIG_P2P
  780. if (wpa_s->p2p_in_provisioning) {
  781. /*
  782. * Use shorter wait during P2P Provisioning
  783. * state to speed up group formation.
  784. */
  785. timeout_sec = 0;
  786. timeout_usec = 250000;
  787. }
  788. #endif /* CONFIG_P2P */
  789. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  790. timeout_usec);
  791. }
  792. }
  793. }
  794. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  795. union wpa_event_data *data)
  796. {
  797. const char *rn, *rn2;
  798. struct wpa_supplicant *ifs;
  799. _wpa_supplicant_event_scan_results(wpa_s, data);
  800. /*
  801. * Check other interfaces to see if they have the same radio-name. If
  802. * so, they get updated with this same scan info.
  803. */
  804. if (!wpa_s->driver->get_radio_name)
  805. return;
  806. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  807. if (rn == NULL || rn[0] == '\0')
  808. return;
  809. wpa_printf(MSG_DEBUG, "%s: Checking for other virtual interfaces "
  810. "sharing same radio (%s) in event_scan_results",
  811. wpa_s->ifname, rn);
  812. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  813. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  814. continue;
  815. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  816. if (rn2 && os_strcmp(rn, rn2) == 0) {
  817. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  818. "sibling", ifs->ifname);
  819. _wpa_supplicant_event_scan_results(ifs, data);
  820. }
  821. }
  822. }
  823. #endif /* CONFIG_NO_SCAN_PROCESSING */
  824. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  825. union wpa_event_data *data)
  826. {
  827. int l, len, found = 0, wpa_found, rsn_found;
  828. const u8 *p;
  829. wpa_printf(MSG_DEBUG, "Association info event");
  830. if (data->assoc_info.req_ies)
  831. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  832. data->assoc_info.req_ies_len);
  833. if (data->assoc_info.resp_ies)
  834. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  835. data->assoc_info.resp_ies_len);
  836. if (data->assoc_info.beacon_ies)
  837. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  838. data->assoc_info.beacon_ies,
  839. data->assoc_info.beacon_ies_len);
  840. if (data->assoc_info.freq)
  841. wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
  842. p = data->assoc_info.req_ies;
  843. l = data->assoc_info.req_ies_len;
  844. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  845. while (p && l >= 2) {
  846. len = p[1] + 2;
  847. if (len > l) {
  848. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  849. p, l);
  850. break;
  851. }
  852. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  853. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  854. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  855. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  856. break;
  857. found = 1;
  858. wpa_find_assoc_pmkid(wpa_s);
  859. break;
  860. }
  861. l -= len;
  862. p += len;
  863. }
  864. if (!found && data->assoc_info.req_ies)
  865. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  866. #ifdef CONFIG_IEEE80211R
  867. #ifdef CONFIG_SME
  868. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  869. u8 bssid[ETH_ALEN];
  870. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  871. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  872. data->assoc_info.resp_ies,
  873. data->assoc_info.resp_ies_len,
  874. bssid) < 0) {
  875. wpa_printf(MSG_DEBUG, "FT: Validation of "
  876. "Reassociation Response failed");
  877. wpa_supplicant_deauthenticate(
  878. wpa_s, WLAN_REASON_INVALID_IE);
  879. return -1;
  880. }
  881. }
  882. p = data->assoc_info.resp_ies;
  883. l = data->assoc_info.resp_ies_len;
  884. #ifdef CONFIG_WPS_STRICT
  885. if (wpa_s->current_ssid &&
  886. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  887. struct wpabuf *wps;
  888. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  889. if (wps == NULL) {
  890. wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
  891. "WPS IE in (Re)Association Response");
  892. return -1;
  893. }
  894. if (wps_validate_assoc_resp(wps) < 0) {
  895. wpabuf_free(wps);
  896. wpa_supplicant_deauthenticate(
  897. wpa_s, WLAN_REASON_INVALID_IE);
  898. return -1;
  899. }
  900. wpabuf_free(wps);
  901. }
  902. #endif /* CONFIG_WPS_STRICT */
  903. /* Go through the IEs and make a copy of the MDIE, if present. */
  904. while (p && l >= 2) {
  905. len = p[1] + 2;
  906. if (len > l) {
  907. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  908. p, l);
  909. break;
  910. }
  911. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  912. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  913. wpa_s->sme.ft_used = 1;
  914. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  915. MOBILITY_DOMAIN_ID_LEN);
  916. break;
  917. }
  918. l -= len;
  919. p += len;
  920. }
  921. #endif /* CONFIG_SME */
  922. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  923. data->assoc_info.resp_ies_len);
  924. #endif /* CONFIG_IEEE80211R */
  925. /* WPA/RSN IE from Beacon/ProbeResp */
  926. p = data->assoc_info.beacon_ies;
  927. l = data->assoc_info.beacon_ies_len;
  928. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  929. */
  930. wpa_found = rsn_found = 0;
  931. while (p && l >= 2) {
  932. len = p[1] + 2;
  933. if (len > l) {
  934. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  935. p, l);
  936. break;
  937. }
  938. if (!wpa_found &&
  939. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  940. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  941. wpa_found = 1;
  942. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  943. }
  944. if (!rsn_found &&
  945. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  946. rsn_found = 1;
  947. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  948. }
  949. l -= len;
  950. p += len;
  951. }
  952. if (!wpa_found && data->assoc_info.beacon_ies)
  953. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  954. if (!rsn_found && data->assoc_info.beacon_ies)
  955. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  956. if (wpa_found || rsn_found)
  957. wpa_s->ap_ies_from_associnfo = 1;
  958. wpa_s->assoc_freq = data->assoc_info.freq;
  959. return 0;
  960. }
  961. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  962. union wpa_event_data *data)
  963. {
  964. u8 bssid[ETH_ALEN];
  965. int ft_completed;
  966. int bssid_changed;
  967. struct wpa_driver_capa capa;
  968. #ifdef CONFIG_AP
  969. if (wpa_s->ap_iface) {
  970. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  971. data->assoc_info.addr,
  972. data->assoc_info.req_ies,
  973. data->assoc_info.req_ies_len);
  974. return;
  975. }
  976. #endif /* CONFIG_AP */
  977. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  978. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  979. return;
  980. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  981. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  982. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  983. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  984. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  985. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  986. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  987. MACSTR, MAC2STR(bssid));
  988. random_add_randomness(bssid, ETH_ALEN);
  989. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  990. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  991. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  992. if (bssid_changed)
  993. wpas_notify_bssid_changed(wpa_s);
  994. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  995. wpa_clear_keys(wpa_s, bssid);
  996. }
  997. if (wpa_supplicant_select_config(wpa_s) < 0) {
  998. wpa_supplicant_disassociate(
  999. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1000. return;
  1001. }
  1002. if (wpa_s->current_ssid) {
  1003. struct wpa_bss *bss = NULL;
  1004. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1005. if (ssid->ssid_len > 0)
  1006. bss = wpa_bss_get(wpa_s, bssid,
  1007. ssid->ssid, ssid->ssid_len);
  1008. if (!bss)
  1009. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1010. if (bss)
  1011. wpa_s->current_bss = bss;
  1012. }
  1013. }
  1014. #ifdef CONFIG_SME
  1015. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1016. wpa_s->sme.prev_bssid_set = 1;
  1017. #endif /* CONFIG_SME */
  1018. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1019. if (wpa_s->current_ssid) {
  1020. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1021. * initialized before association, but for other modes,
  1022. * initialize PC/SC here, if the current configuration needs
  1023. * smartcard or SIM/USIM. */
  1024. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1025. }
  1026. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1027. if (wpa_s->l2)
  1028. l2_packet_notify_auth_start(wpa_s->l2);
  1029. /*
  1030. * Set portEnabled first to FALSE in order to get EAP state machine out
  1031. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1032. * state machine may transit to AUTHENTICATING state based on obsolete
  1033. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1034. * AUTHENTICATED without ever giving chance to EAP state machine to
  1035. * reset the state.
  1036. */
  1037. if (!ft_completed) {
  1038. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1039. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1040. }
  1041. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1042. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1043. /* 802.1X::portControl = Auto */
  1044. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1045. wpa_s->eapol_received = 0;
  1046. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1047. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1048. (wpa_s->current_ssid &&
  1049. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1050. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1051. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1052. } else if (!ft_completed) {
  1053. /* Timeout for receiving the first EAPOL packet */
  1054. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1055. }
  1056. wpa_supplicant_cancel_scan(wpa_s);
  1057. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1058. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1059. /*
  1060. * We are done; the driver will take care of RSN 4-way
  1061. * handshake.
  1062. */
  1063. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1064. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1065. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1066. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1067. }
  1068. if (wpa_s->pending_eapol_rx) {
  1069. struct os_time now, age;
  1070. os_get_time(&now);
  1071. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1072. if (age.sec == 0 && age.usec < 100000 &&
  1073. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1074. 0) {
  1075. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  1076. "that was received just before association "
  1077. "notification");
  1078. wpa_supplicant_rx_eapol(
  1079. wpa_s, wpa_s->pending_eapol_rx_src,
  1080. wpabuf_head(wpa_s->pending_eapol_rx),
  1081. wpabuf_len(wpa_s->pending_eapol_rx));
  1082. }
  1083. wpabuf_free(wpa_s->pending_eapol_rx);
  1084. wpa_s->pending_eapol_rx = NULL;
  1085. }
  1086. #ifdef CONFIG_BGSCAN
  1087. if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
  1088. bgscan_deinit(wpa_s);
  1089. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  1090. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  1091. wpa_printf(MSG_DEBUG, "Failed to initialize "
  1092. "bgscan");
  1093. /*
  1094. * Live without bgscan; it is only used as a
  1095. * roaming optimization, so the initial
  1096. * connection is not affected.
  1097. */
  1098. } else
  1099. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  1100. } else
  1101. wpa_s->bgscan_ssid = NULL;
  1102. }
  1103. #endif /* CONFIG_BGSCAN */
  1104. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1105. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1106. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1107. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1108. /* Set static WEP keys again */
  1109. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1110. }
  1111. }
  1112. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1113. u16 reason_code)
  1114. {
  1115. const u8 *bssid;
  1116. #ifdef CONFIG_SME
  1117. int authenticating;
  1118. u8 prev_pending_bssid[ETH_ALEN];
  1119. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1120. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1121. #endif /* CONFIG_SME */
  1122. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1123. /*
  1124. * At least Host AP driver and a Prism3 card seemed to be
  1125. * generating streams of disconnected events when configuring
  1126. * IBSS for WPA-None. Ignore them for now.
  1127. */
  1128. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  1129. "IBSS/WPA-None mode");
  1130. return;
  1131. }
  1132. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1133. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1134. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1135. "pre-shared key may be incorrect");
  1136. }
  1137. if (!wpa_s->auto_reconnect_disabled ||
  1138. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  1139. wpa_printf(MSG_DEBUG, "WPA: Auto connect enabled: try to "
  1140. "reconnect (wps=%d)",
  1141. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
  1142. if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1143. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1144. } else {
  1145. wpa_printf(MSG_DEBUG, "WPA: Auto connect disabled: do not try "
  1146. "to re-connect");
  1147. wpa_s->reassociate = 0;
  1148. wpa_s->disconnected = 1;
  1149. }
  1150. bssid = wpa_s->bssid;
  1151. if (is_zero_ether_addr(bssid))
  1152. bssid = wpa_s->pending_bssid;
  1153. wpas_connection_failed(wpa_s, bssid);
  1154. wpa_sm_notify_disassoc(wpa_s->wpa);
  1155. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1156. " reason=%d",
  1157. MAC2STR(bssid), reason_code);
  1158. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1159. wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
  1160. wpa_s->keys_cleared = 0;
  1161. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1162. }
  1163. wpa_supplicant_mark_disassoc(wpa_s);
  1164. bgscan_deinit(wpa_s);
  1165. wpa_s->bgscan_ssid = NULL;
  1166. #ifdef CONFIG_SME
  1167. if (authenticating &&
  1168. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1169. /*
  1170. * mac80211-workaround to force deauth on failed auth cmd,
  1171. * requires us to remain in authenticating state to allow the
  1172. * second authentication attempt to be continued properly.
  1173. */
  1174. wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
  1175. "proceed after disconnection event");
  1176. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1177. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1178. }
  1179. #endif /* CONFIG_SME */
  1180. }
  1181. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1182. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  1183. void *sock_ctx)
  1184. {
  1185. struct wpa_supplicant *wpa_s = eloop_ctx;
  1186. if (!wpa_s->pending_mic_error_report)
  1187. return;
  1188. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  1189. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1190. wpa_s->pending_mic_error_report = 0;
  1191. }
  1192. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1193. static void
  1194. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1195. union wpa_event_data *data)
  1196. {
  1197. int pairwise;
  1198. struct os_time t;
  1199. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1200. pairwise = (data && data->michael_mic_failure.unicast);
  1201. os_get_time(&t);
  1202. if ((wpa_s->last_michael_mic_error &&
  1203. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1204. wpa_s->pending_mic_error_report) {
  1205. if (wpa_s->pending_mic_error_report) {
  1206. /*
  1207. * Send the pending MIC error report immediately since
  1208. * we are going to start countermeasures and AP better
  1209. * do the same.
  1210. */
  1211. wpa_sm_key_request(wpa_s->wpa, 1,
  1212. wpa_s->pending_mic_error_pairwise);
  1213. }
  1214. /* Send the new MIC error report immediately since we are going
  1215. * to start countermeasures and AP better do the same.
  1216. */
  1217. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1218. /* initialize countermeasures */
  1219. wpa_s->countermeasures = 1;
  1220. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1221. /*
  1222. * Need to wait for completion of request frame. We do not get
  1223. * any callback for the message completion, so just wait a
  1224. * short while and hope for the best. */
  1225. os_sleep(0, 10000);
  1226. wpa_drv_set_countermeasures(wpa_s, 1);
  1227. wpa_supplicant_deauthenticate(wpa_s,
  1228. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1229. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1230. wpa_s, NULL);
  1231. eloop_register_timeout(60, 0,
  1232. wpa_supplicant_stop_countermeasures,
  1233. wpa_s, NULL);
  1234. /* TODO: mark the AP rejected for 60 second. STA is
  1235. * allowed to associate with another AP.. */
  1236. } else {
  1237. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1238. if (wpa_s->mic_errors_seen) {
  1239. /*
  1240. * Reduce the effectiveness of Michael MIC error
  1241. * reports as a means for attacking against TKIP if
  1242. * more than one MIC failure is noticed with the same
  1243. * PTK. We delay the transmission of the reports by a
  1244. * random time between 0 and 60 seconds in order to
  1245. * force the attacker wait 60 seconds before getting
  1246. * the information on whether a frame resulted in a MIC
  1247. * failure.
  1248. */
  1249. u8 rval[4];
  1250. int sec;
  1251. if (os_get_random(rval, sizeof(rval)) < 0)
  1252. sec = os_random() % 60;
  1253. else
  1254. sec = WPA_GET_BE32(rval) % 60;
  1255. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  1256. "seconds", sec);
  1257. wpa_s->pending_mic_error_report = 1;
  1258. wpa_s->pending_mic_error_pairwise = pairwise;
  1259. eloop_cancel_timeout(
  1260. wpa_supplicant_delayed_mic_error_report,
  1261. wpa_s, NULL);
  1262. eloop_register_timeout(
  1263. sec, os_random() % 1000000,
  1264. wpa_supplicant_delayed_mic_error_report,
  1265. wpa_s, NULL);
  1266. } else {
  1267. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1268. }
  1269. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1270. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1271. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1272. }
  1273. wpa_s->last_michael_mic_error = t.sec;
  1274. wpa_s->mic_errors_seen++;
  1275. }
  1276. #ifdef CONFIG_TERMINATE_ONLASTIF
  1277. static int any_interfaces(struct wpa_supplicant *head)
  1278. {
  1279. struct wpa_supplicant *wpa_s;
  1280. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1281. if (!wpa_s->interface_removed)
  1282. return 1;
  1283. return 0;
  1284. }
  1285. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1286. static void
  1287. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1288. union wpa_event_data *data)
  1289. {
  1290. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1291. return;
  1292. switch (data->interface_status.ievent) {
  1293. case EVENT_INTERFACE_ADDED:
  1294. if (!wpa_s->interface_removed)
  1295. break;
  1296. wpa_s->interface_removed = 0;
  1297. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  1298. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1299. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  1300. "after interface was added.");
  1301. }
  1302. break;
  1303. case EVENT_INTERFACE_REMOVED:
  1304. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1305. wpa_s->interface_removed = 1;
  1306. wpa_supplicant_mark_disassoc(wpa_s);
  1307. l2_packet_deinit(wpa_s->l2);
  1308. wpa_s->l2 = NULL;
  1309. #ifdef CONFIG_TERMINATE_ONLASTIF
  1310. /* check if last interface */
  1311. if (!any_interfaces(wpa_s->global->ifaces))
  1312. eloop_terminate();
  1313. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1314. break;
  1315. }
  1316. }
  1317. #ifdef CONFIG_PEERKEY
  1318. static void
  1319. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1320. union wpa_event_data *data)
  1321. {
  1322. if (data == NULL)
  1323. return;
  1324. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1325. }
  1326. #endif /* CONFIG_PEERKEY */
  1327. #ifdef CONFIG_IEEE80211R
  1328. static void
  1329. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1330. union wpa_event_data *data)
  1331. {
  1332. if (data == NULL)
  1333. return;
  1334. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1335. data->ft_ies.ies_len,
  1336. data->ft_ies.ft_action,
  1337. data->ft_ies.target_ap,
  1338. data->ft_ies.ric_ies,
  1339. data->ft_ies.ric_ies_len) < 0) {
  1340. /* TODO: prevent MLME/driver from trying to associate? */
  1341. }
  1342. }
  1343. #endif /* CONFIG_IEEE80211R */
  1344. #ifdef CONFIG_IBSS_RSN
  1345. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1346. union wpa_event_data *data)
  1347. {
  1348. if (data == NULL)
  1349. return;
  1350. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1351. }
  1352. #endif /* CONFIG_IBSS_RSN */
  1353. #ifdef CONFIG_IEEE80211R
  1354. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  1355. size_t len)
  1356. {
  1357. const u8 *sta_addr, *target_ap_addr;
  1358. u16 status;
  1359. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  1360. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1361. return; /* only SME case supported for now */
  1362. if (len < 1 + 2 * ETH_ALEN + 2)
  1363. return;
  1364. if (data[0] != 2)
  1365. return; /* Only FT Action Response is supported for now */
  1366. sta_addr = data + 1;
  1367. target_ap_addr = data + 1 + ETH_ALEN;
  1368. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  1369. wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
  1370. " TargetAP " MACSTR " status %u",
  1371. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1372. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1373. wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  1374. " in FT Action Response", MAC2STR(sta_addr));
  1375. return;
  1376. }
  1377. if (status) {
  1378. wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
  1379. "failure (status code %d)", status);
  1380. /* TODO: report error to FT code(?) */
  1381. return;
  1382. }
  1383. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  1384. len - (1 + 2 * ETH_ALEN + 2), 1,
  1385. target_ap_addr, NULL, 0) < 0)
  1386. return;
  1387. #ifdef CONFIG_SME
  1388. {
  1389. struct wpa_bss *bss;
  1390. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  1391. if (bss)
  1392. wpa_s->sme.freq = bss->freq;
  1393. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  1394. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  1395. WLAN_AUTH_FT);
  1396. }
  1397. #endif /* CONFIG_SME */
  1398. }
  1399. #endif /* CONFIG_IEEE80211R */
  1400. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1401. union wpa_event_data *data)
  1402. {
  1403. struct wpa_supplicant *wpa_s = ctx;
  1404. u16 reason_code = 0;
  1405. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  1406. event != EVENT_INTERFACE_ENABLED &&
  1407. event != EVENT_INTERFACE_STATUS) {
  1408. wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
  1409. "disabled", event);
  1410. return;
  1411. }
  1412. wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
  1413. event, wpa_s->ifname);
  1414. switch (event) {
  1415. case EVENT_AUTH:
  1416. sme_event_auth(wpa_s, data);
  1417. break;
  1418. case EVENT_ASSOC:
  1419. wpa_supplicant_event_assoc(wpa_s, data);
  1420. break;
  1421. case EVENT_DISASSOC:
  1422. wpa_printf(MSG_DEBUG, "Disassociation notification");
  1423. if (data) {
  1424. wpa_printf(MSG_DEBUG, " * reason %u",
  1425. data->disassoc_info.reason_code);
  1426. if (data->disassoc_info.addr)
  1427. wpa_printf(MSG_DEBUG, " * address " MACSTR,
  1428. MAC2STR(data->disassoc_info.addr));
  1429. }
  1430. #ifdef CONFIG_AP
  1431. if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
  1432. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1433. data->disassoc_info.addr);
  1434. break;
  1435. }
  1436. #endif /* CONFIG_AP */
  1437. if (data) {
  1438. reason_code = data->disassoc_info.reason_code;
  1439. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  1440. data->disassoc_info.ie,
  1441. data->disassoc_info.ie_len);
  1442. #ifdef CONFIG_P2P
  1443. wpas_p2p_disassoc_notif(
  1444. wpa_s, data->disassoc_info.addr, reason_code,
  1445. data->disassoc_info.ie,
  1446. data->disassoc_info.ie_len);
  1447. #endif /* CONFIG_P2P */
  1448. }
  1449. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1450. sme_event_disassoc(wpa_s, data);
  1451. /* fall through */
  1452. case EVENT_DEAUTH:
  1453. if (event == EVENT_DEAUTH) {
  1454. wpa_printf(MSG_DEBUG, "Deauthentication notification");
  1455. if (data) {
  1456. reason_code = data->deauth_info.reason_code;
  1457. wpa_printf(MSG_DEBUG, " * reason %u",
  1458. data->deauth_info.reason_code);
  1459. if (data->deauth_info.addr) {
  1460. wpa_printf(MSG_DEBUG, " * address "
  1461. MACSTR,
  1462. MAC2STR(data->deauth_info.
  1463. addr));
  1464. }
  1465. wpa_hexdump(MSG_DEBUG,
  1466. "Deauthentication frame IE(s)",
  1467. data->deauth_info.ie,
  1468. data->deauth_info.ie_len);
  1469. #ifdef CONFIG_P2P
  1470. wpas_p2p_deauth_notif(
  1471. wpa_s, data->deauth_info.addr,
  1472. reason_code,
  1473. data->deauth_info.ie,
  1474. data->deauth_info.ie_len);
  1475. #endif /* CONFIG_P2P */
  1476. }
  1477. }
  1478. #ifdef CONFIG_AP
  1479. if (wpa_s->ap_iface && data && data->deauth_info.addr) {
  1480. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1481. data->deauth_info.addr);
  1482. break;
  1483. }
  1484. #endif /* CONFIG_AP */
  1485. wpa_supplicant_event_disassoc(wpa_s, reason_code);
  1486. break;
  1487. case EVENT_MICHAEL_MIC_FAILURE:
  1488. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1489. break;
  1490. #ifndef CONFIG_NO_SCAN_PROCESSING
  1491. case EVENT_SCAN_RESULTS:
  1492. wpa_supplicant_event_scan_results(wpa_s, data);
  1493. break;
  1494. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1495. case EVENT_ASSOCINFO:
  1496. wpa_supplicant_event_associnfo(wpa_s, data);
  1497. break;
  1498. case EVENT_INTERFACE_STATUS:
  1499. wpa_supplicant_event_interface_status(wpa_s, data);
  1500. break;
  1501. case EVENT_PMKID_CANDIDATE:
  1502. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1503. break;
  1504. #ifdef CONFIG_PEERKEY
  1505. case EVENT_STKSTART:
  1506. wpa_supplicant_event_stkstart(wpa_s, data);
  1507. break;
  1508. #endif /* CONFIG_PEERKEY */
  1509. #ifdef CONFIG_IEEE80211R
  1510. case EVENT_FT_RESPONSE:
  1511. wpa_supplicant_event_ft_response(wpa_s, data);
  1512. break;
  1513. #endif /* CONFIG_IEEE80211R */
  1514. #ifdef CONFIG_IBSS_RSN
  1515. case EVENT_IBSS_RSN_START:
  1516. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1517. break;
  1518. #endif /* CONFIG_IBSS_RSN */
  1519. case EVENT_ASSOC_REJECT:
  1520. if (data->assoc_reject.bssid)
  1521. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1522. "bssid=" MACSTR " status_code=%u",
  1523. MAC2STR(data->assoc_reject.bssid),
  1524. data->assoc_reject.status_code);
  1525. else
  1526. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1527. "status_code=%u",
  1528. data->assoc_reject.status_code);
  1529. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1530. sme_event_assoc_reject(wpa_s, data);
  1531. break;
  1532. case EVENT_AUTH_TIMED_OUT:
  1533. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1534. sme_event_auth_timed_out(wpa_s, data);
  1535. break;
  1536. case EVENT_ASSOC_TIMED_OUT:
  1537. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1538. sme_event_assoc_timed_out(wpa_s, data);
  1539. break;
  1540. #ifdef CONFIG_AP
  1541. case EVENT_TX_STATUS:
  1542. wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS on %s dst=" MACSTR
  1543. " type=%d stype=%d",
  1544. wpa_s->ifname, MAC2STR(data->tx_status.dst),
  1545. data->tx_status.type, data->tx_status.stype);
  1546. if (wpa_s->ap_iface == NULL) {
  1547. #ifdef CONFIG_P2P
  1548. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1549. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  1550. wpas_send_action_tx_status(
  1551. wpa_s, data->tx_status.dst,
  1552. data->tx_status.data,
  1553. data->tx_status.data_len,
  1554. data->tx_status.ack ?
  1555. P2P_SEND_ACTION_SUCCESS :
  1556. P2P_SEND_ACTION_NO_ACK);
  1557. #endif /* CONFIG_P2P */
  1558. break;
  1559. }
  1560. #ifdef CONFIG_P2P
  1561. wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS pending_dst=" MACSTR,
  1562. MAC2STR(wpa_s->parent->pending_action_dst));
  1563. /*
  1564. * Catch TX status events for Action frames we sent via group
  1565. * interface in GO mode.
  1566. */
  1567. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1568. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  1569. os_memcmp(wpa_s->parent->pending_action_dst,
  1570. data->tx_status.dst, ETH_ALEN) == 0) {
  1571. wpas_send_action_tx_status(
  1572. wpa_s->parent, data->tx_status.dst,
  1573. data->tx_status.data,
  1574. data->tx_status.data_len,
  1575. data->tx_status.ack ?
  1576. P2P_SEND_ACTION_SUCCESS :
  1577. P2P_SEND_ACTION_NO_ACK);
  1578. break;
  1579. }
  1580. #endif /* CONFIG_P2P */
  1581. switch (data->tx_status.type) {
  1582. case WLAN_FC_TYPE_MGMT:
  1583. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1584. data->tx_status.data_len,
  1585. data->tx_status.stype,
  1586. data->tx_status.ack);
  1587. break;
  1588. case WLAN_FC_TYPE_DATA:
  1589. ap_tx_status(wpa_s, data->tx_status.dst,
  1590. data->tx_status.data,
  1591. data->tx_status.data_len,
  1592. data->tx_status.ack);
  1593. break;
  1594. }
  1595. break;
  1596. case EVENT_RX_FROM_UNKNOWN:
  1597. if (wpa_s->ap_iface == NULL)
  1598. break;
  1599. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
  1600. data->rx_from_unknown.len);
  1601. break;
  1602. case EVENT_RX_MGMT:
  1603. if (wpa_s->ap_iface == NULL) {
  1604. #ifdef CONFIG_P2P
  1605. u16 fc, stype;
  1606. const struct ieee80211_mgmt *mgmt;
  1607. mgmt = (const struct ieee80211_mgmt *)
  1608. data->rx_mgmt.frame;
  1609. fc = le_to_host16(mgmt->frame_control);
  1610. stype = WLAN_FC_GET_STYPE(fc);
  1611. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  1612. data->rx_mgmt.frame_len > 24) {
  1613. const u8 *src = mgmt->sa;
  1614. const u8 *ie = mgmt->u.probe_req.variable;
  1615. size_t ie_len = data->rx_mgmt.frame_len -
  1616. (mgmt->u.probe_req.variable -
  1617. data->rx_mgmt.frame);
  1618. wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
  1619. break;
  1620. }
  1621. #endif /* CONFIG_P2P */
  1622. wpa_printf(MSG_DEBUG, "AP: ignore received management "
  1623. "frame in non-AP mode");
  1624. break;
  1625. }
  1626. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1627. break;
  1628. #endif /* CONFIG_AP */
  1629. case EVENT_RX_ACTION:
  1630. wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
  1631. " Category=%u DataLen=%d freq=%d MHz",
  1632. MAC2STR(data->rx_action.sa),
  1633. data->rx_action.category, (int) data->rx_action.len,
  1634. data->rx_action.freq);
  1635. #ifdef CONFIG_IEEE80211R
  1636. if (data->rx_action.category == WLAN_ACTION_FT) {
  1637. ft_rx_action(wpa_s, data->rx_action.data,
  1638. data->rx_action.len);
  1639. break;
  1640. }
  1641. #endif /* CONFIG_IEEE80211R */
  1642. #ifdef CONFIG_P2P
  1643. wpas_p2p_rx_action(wpa_s, data->rx_action.da,
  1644. data->rx_action.sa,
  1645. data->rx_action.bssid,
  1646. data->rx_action.category,
  1647. data->rx_action.data,
  1648. data->rx_action.len, data->rx_action.freq);
  1649. #endif /* CONFIG_P2P */
  1650. break;
  1651. #ifdef CONFIG_P2P
  1652. case EVENT_REMAIN_ON_CHANNEL:
  1653. wpas_p2p_remain_on_channel_cb(
  1654. wpa_s, data->remain_on_channel.freq,
  1655. data->remain_on_channel.duration);
  1656. break;
  1657. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  1658. wpas_p2p_cancel_remain_on_channel_cb(
  1659. wpa_s, data->remain_on_channel.freq);
  1660. break;
  1661. case EVENT_RX_PROBE_REQ:
  1662. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  1663. data->rx_probe_req.ie,
  1664. data->rx_probe_req.ie_len);
  1665. break;
  1666. #endif /* CONFIG_P2P */
  1667. #ifdef CONFIG_CLIENT_MLME
  1668. case EVENT_MLME_RX: {
  1669. struct ieee80211_rx_status rx_status;
  1670. os_memset(&rx_status, 0, sizeof(rx_status));
  1671. rx_status.freq = data->mlme_rx.freq;
  1672. rx_status.channel = data->mlme_rx.channel;
  1673. rx_status.ssi = data->mlme_rx.ssi;
  1674. ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
  1675. &rx_status);
  1676. break;
  1677. }
  1678. #endif /* CONFIG_CLIENT_MLME */
  1679. case EVENT_EAPOL_RX:
  1680. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  1681. data->eapol_rx.data,
  1682. data->eapol_rx.data_len);
  1683. break;
  1684. case EVENT_SIGNAL_CHANGE:
  1685. bgscan_notify_signal_change(
  1686. wpa_s, data->signal_change.above_threshold,
  1687. data->signal_change.current_signal,
  1688. data->signal_change.current_noise,
  1689. data->signal_change.current_txrate);
  1690. break;
  1691. case EVENT_INTERFACE_ENABLED:
  1692. wpa_printf(MSG_DEBUG, "Interface was enabled");
  1693. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  1694. #ifdef CONFIG_AP
  1695. if (!wpa_s->ap_iface) {
  1696. wpa_supplicant_set_state(wpa_s,
  1697. WPA_DISCONNECTED);
  1698. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1699. } else
  1700. wpa_supplicant_set_state(wpa_s,
  1701. WPA_COMPLETED);
  1702. #else /* CONFIG_AP */
  1703. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1704. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1705. #endif /* CONFIG_AP */
  1706. }
  1707. break;
  1708. case EVENT_INTERFACE_DISABLED:
  1709. wpa_printf(MSG_DEBUG, "Interface was disabled");
  1710. wpa_supplicant_mark_disassoc(wpa_s);
  1711. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  1712. break;
  1713. case EVENT_CHANNEL_LIST_CHANGED:
  1714. if (wpa_s->drv_priv == NULL)
  1715. break; /* Ignore event during drv initialization */
  1716. #ifdef CONFIG_P2P
  1717. wpas_p2p_update_channel_list(wpa_s);
  1718. #endif /* CONFIG_P2P */
  1719. break;
  1720. case EVENT_INTERFACE_UNAVAILABLE:
  1721. #ifdef CONFIG_P2P
  1722. wpas_p2p_interface_unavailable(wpa_s);
  1723. #endif /* CONFIG_P2P */
  1724. break;
  1725. case EVENT_BEST_CHANNEL:
  1726. wpa_printf(MSG_DEBUG, "Best channel event received (%d %d %d)",
  1727. data->best_chan.freq_24, data->best_chan.freq_5,
  1728. data->best_chan.freq_overall);
  1729. wpa_s->best_24_freq = data->best_chan.freq_24;
  1730. wpa_s->best_5_freq = data->best_chan.freq_5;
  1731. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  1732. #ifdef CONFIG_P2P
  1733. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  1734. data->best_chan.freq_5,
  1735. data->best_chan.freq_overall);
  1736. #endif /* CONFIG_P2P */
  1737. break;
  1738. default:
  1739. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1740. break;
  1741. }
  1742. }