events.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2008, 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 "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 "preauth.h"
  25. #include "pmksa_cache.h"
  26. #include "wpa_ctrl.h"
  27. #include "eap_peer/eap.h"
  28. #include "notify.h"
  29. #include "ieee802_11_defs.h"
  30. #include "blacklist.h"
  31. #include "wpas_glue.h"
  32. #include "wps_supplicant.h"
  33. #include "ibss_rsn.h"
  34. #include "sme.h"
  35. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  36. {
  37. struct wpa_ssid *ssid, *old_ssid;
  38. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  39. return 0;
  40. wpa_printf(MSG_DEBUG, "Select network based on association "
  41. "information");
  42. ssid = wpa_supplicant_get_ssid(wpa_s);
  43. if (ssid == NULL) {
  44. wpa_printf(MSG_INFO, "No network configuration found for the "
  45. "current AP");
  46. return -1;
  47. }
  48. if (ssid->disabled) {
  49. wpa_printf(MSG_DEBUG, "Selected network is disabled");
  50. return -1;
  51. }
  52. wpa_printf(MSG_DEBUG, "Network configuration found for the current "
  53. "AP");
  54. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  55. WPA_KEY_MGMT_WPA_NONE |
  56. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
  57. WPA_KEY_MGMT_PSK_SHA256 |
  58. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  59. u8 wpa_ie[80];
  60. size_t wpa_ie_len = sizeof(wpa_ie);
  61. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  62. wpa_ie, &wpa_ie_len);
  63. } else {
  64. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  65. }
  66. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  67. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  68. old_ssid = wpa_s->current_ssid;
  69. wpa_s->current_ssid = ssid;
  70. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  71. wpa_supplicant_initiate_eapol(wpa_s);
  72. if (old_ssid != wpa_s->current_ssid)
  73. wpas_notify_network_changed(wpa_s);
  74. return 0;
  75. }
  76. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  77. void *sock_ctx)
  78. {
  79. struct wpa_supplicant *wpa_s = eloop_ctx;
  80. if (wpa_s->countermeasures) {
  81. wpa_s->countermeasures = 0;
  82. wpa_drv_set_countermeasures(wpa_s, 0);
  83. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  84. wpa_supplicant_req_scan(wpa_s, 0, 0);
  85. }
  86. }
  87. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  88. {
  89. int bssid_changed;
  90. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  91. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  92. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  93. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  94. if (bssid_changed)
  95. wpas_notify_bssid_changed(wpa_s);
  96. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  97. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  98. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  99. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  100. wpa_s->ap_ies_from_associnfo = 0;
  101. }
  102. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  103. {
  104. struct wpa_ie_data ie;
  105. int pmksa_set = -1;
  106. size_t i;
  107. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  108. ie.pmkid == NULL)
  109. return;
  110. for (i = 0; i < ie.num_pmkid; i++) {
  111. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  112. ie.pmkid + i * PMKID_LEN,
  113. NULL, NULL, 0);
  114. if (pmksa_set == 0) {
  115. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  116. break;
  117. }
  118. }
  119. wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
  120. "cache", pmksa_set == 0 ? "" : "not ");
  121. }
  122. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  123. union wpa_event_data *data)
  124. {
  125. if (data == NULL) {
  126. wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
  127. return;
  128. }
  129. wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  130. " index=%d preauth=%d",
  131. MAC2STR(data->pmkid_candidate.bssid),
  132. data->pmkid_candidate.index,
  133. data->pmkid_candidate.preauth);
  134. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  135. data->pmkid_candidate.index,
  136. data->pmkid_candidate.preauth);
  137. }
  138. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  139. {
  140. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  141. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  142. return 0;
  143. #ifdef IEEE8021X_EAPOL
  144. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  145. wpa_s->current_ssid &&
  146. !(wpa_s->current_ssid->eapol_flags &
  147. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  148. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  149. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  150. * plaintext or static WEP keys). */
  151. return 0;
  152. }
  153. #endif /* IEEE8021X_EAPOL */
  154. return 1;
  155. }
  156. /**
  157. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  158. * @wpa_s: pointer to wpa_supplicant data
  159. * @ssid: Configuration data for the network
  160. * Returns: 0 on success, -1 on failure
  161. *
  162. * This function is called when starting authentication with a network that is
  163. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  164. */
  165. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  166. struct wpa_ssid *ssid)
  167. {
  168. #ifdef IEEE8021X_EAPOL
  169. int aka = 0, sim = 0, type;
  170. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  171. return 0;
  172. if (ssid->eap.eap_methods == NULL) {
  173. sim = 1;
  174. aka = 1;
  175. } else {
  176. struct eap_method_type *eap = ssid->eap.eap_methods;
  177. while (eap->vendor != EAP_VENDOR_IETF ||
  178. eap->method != EAP_TYPE_NONE) {
  179. if (eap->vendor == EAP_VENDOR_IETF) {
  180. if (eap->method == EAP_TYPE_SIM)
  181. sim = 1;
  182. else if (eap->method == EAP_TYPE_AKA)
  183. aka = 1;
  184. }
  185. eap++;
  186. }
  187. }
  188. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  189. sim = 0;
  190. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  191. aka = 0;
  192. if (!sim && !aka) {
  193. wpa_printf(MSG_DEBUG, "Selected network is configured to use "
  194. "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
  195. return 0;
  196. }
  197. wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
  198. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  199. if (sim && aka)
  200. type = SCARD_TRY_BOTH;
  201. else if (aka)
  202. type = SCARD_USIM_ONLY;
  203. else
  204. type = SCARD_GSM_SIM_ONLY;
  205. wpa_s->scard = scard_init(type);
  206. if (wpa_s->scard == NULL) {
  207. wpa_printf(MSG_WARNING, "Failed to initialize SIM "
  208. "(pcsc-lite)");
  209. return -1;
  210. }
  211. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  212. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  213. #endif /* IEEE8021X_EAPOL */
  214. return 0;
  215. }
  216. #ifndef CONFIG_NO_SCAN_PROCESSING
  217. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  218. struct wpa_ssid *ssid)
  219. {
  220. int i, privacy = 0;
  221. if (ssid->mixed_cell)
  222. return 1;
  223. #ifdef CONFIG_WPS
  224. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  225. return 1;
  226. #endif /* CONFIG_WPS */
  227. for (i = 0; i < NUM_WEP_KEYS; i++) {
  228. if (ssid->wep_key_len[i]) {
  229. privacy = 1;
  230. break;
  231. }
  232. }
  233. #ifdef IEEE8021X_EAPOL
  234. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  235. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  236. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  237. privacy = 1;
  238. #endif /* IEEE8021X_EAPOL */
  239. if (bss->caps & IEEE80211_CAP_PRIVACY)
  240. return privacy;
  241. return !privacy;
  242. }
  243. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  244. struct wpa_ssid *ssid,
  245. struct wpa_scan_res *bss)
  246. {
  247. struct wpa_ie_data ie;
  248. int proto_match = 0;
  249. const u8 *rsn_ie, *wpa_ie;
  250. int ret;
  251. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  252. if (ret >= 0)
  253. return ret;
  254. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  255. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  256. proto_match++;
  257. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  258. wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
  259. break;
  260. }
  261. if (!(ie.proto & ssid->proto)) {
  262. wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
  263. "mismatch");
  264. break;
  265. }
  266. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  267. wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
  268. "mismatch");
  269. break;
  270. }
  271. if (!(ie.group_cipher & ssid->group_cipher)) {
  272. wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
  273. "mismatch");
  274. break;
  275. }
  276. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  277. wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
  278. "mismatch");
  279. break;
  280. }
  281. #ifdef CONFIG_IEEE80211W
  282. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  283. ssid->ieee80211w == IEEE80211W_REQUIRED) {
  284. wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
  285. "protection");
  286. break;
  287. }
  288. #endif /* CONFIG_IEEE80211W */
  289. wpa_printf(MSG_DEBUG, " selected based on RSN IE");
  290. return 1;
  291. }
  292. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  293. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  294. proto_match++;
  295. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  296. wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
  297. break;
  298. }
  299. if (!(ie.proto & ssid->proto)) {
  300. wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
  301. "mismatch");
  302. break;
  303. }
  304. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  305. wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
  306. "mismatch");
  307. break;
  308. }
  309. if (!(ie.group_cipher & ssid->group_cipher)) {
  310. wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
  311. "mismatch");
  312. break;
  313. }
  314. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  315. wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
  316. "mismatch");
  317. break;
  318. }
  319. wpa_printf(MSG_DEBUG, " selected based on WPA IE");
  320. return 1;
  321. }
  322. if (proto_match == 0)
  323. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  324. return 0;
  325. }
  326. static struct wpa_scan_res *
  327. wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
  328. struct wpa_ssid *group,
  329. struct wpa_ssid **selected_ssid)
  330. {
  331. struct wpa_ssid *ssid;
  332. struct wpa_scan_res *bss;
  333. size_t i;
  334. struct wpa_blacklist *e;
  335. const u8 *ie;
  336. wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
  337. for (i = 0; i < wpa_s->scan_res->num; i++) {
  338. const u8 *ssid_;
  339. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  340. bss = wpa_s->scan_res->res[i];
  341. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  342. ssid_ = ie ? ie + 2 : (u8 *) "";
  343. ssid_len = ie ? ie[1] : 0;
  344. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  345. wpa_ie_len = ie ? ie[1] : 0;
  346. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  347. rsn_ie_len = ie ? ie[1] : 0;
  348. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  349. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  350. (int) i, MAC2STR(bss->bssid),
  351. wpa_ssid_txt(ssid_, ssid_len),
  352. wpa_ie_len, rsn_ie_len, bss->caps);
  353. e = wpa_blacklist_get(wpa_s, bss->bssid);
  354. if (e && e->count > 1) {
  355. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  356. continue;
  357. }
  358. if (wpa_ie_len == 0 && rsn_ie_len == 0) {
  359. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
  360. continue;
  361. }
  362. for (ssid = group; ssid; ssid = ssid->pnext) {
  363. int check_ssid = 1;
  364. if (ssid->disabled) {
  365. wpa_printf(MSG_DEBUG, " skip - disabled");
  366. continue;
  367. }
  368. #ifdef CONFIG_WPS
  369. if (ssid->ssid_len == 0 &&
  370. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  371. check_ssid = 0;
  372. #endif /* CONFIG_WPS */
  373. if (check_ssid &&
  374. (ssid_len != ssid->ssid_len ||
  375. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  376. wpa_printf(MSG_DEBUG, " skip - "
  377. "SSID mismatch");
  378. continue;
  379. }
  380. if (ssid->bssid_set &&
  381. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  382. {
  383. wpa_printf(MSG_DEBUG, " skip - "
  384. "BSSID mismatch");
  385. continue;
  386. }
  387. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  388. continue;
  389. wpa_printf(MSG_DEBUG, " selected WPA AP "
  390. MACSTR " ssid='%s'",
  391. MAC2STR(bss->bssid),
  392. wpa_ssid_txt(ssid_, ssid_len));
  393. *selected_ssid = ssid;
  394. return bss;
  395. }
  396. }
  397. return NULL;
  398. }
  399. static struct wpa_scan_res *
  400. wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
  401. struct wpa_ssid *group,
  402. struct wpa_ssid **selected_ssid)
  403. {
  404. struct wpa_ssid *ssid;
  405. struct wpa_scan_res *bss;
  406. size_t i;
  407. struct wpa_blacklist *e;
  408. const u8 *ie;
  409. wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
  410. for (i = 0; i < wpa_s->scan_res->num; i++) {
  411. const u8 *ssid_;
  412. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  413. bss = wpa_s->scan_res->res[i];
  414. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  415. ssid_ = ie ? ie + 2 : (u8 *) "";
  416. ssid_len = ie ? ie[1] : 0;
  417. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  418. wpa_ie_len = ie ? ie[1] : 0;
  419. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  420. rsn_ie_len = ie ? ie[1] : 0;
  421. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  422. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  423. (int) i, MAC2STR(bss->bssid),
  424. wpa_ssid_txt(ssid_, ssid_len),
  425. wpa_ie_len, rsn_ie_len, bss->caps);
  426. e = wpa_blacklist_get(wpa_s, bss->bssid);
  427. if (e && e->count > 1) {
  428. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  429. continue;
  430. }
  431. for (ssid = group; ssid; ssid = ssid->pnext) {
  432. int check_ssid = ssid->ssid_len != 0;
  433. if (ssid->disabled) {
  434. wpa_printf(MSG_DEBUG, " skip - disabled");
  435. continue;
  436. }
  437. #ifdef CONFIG_WPS
  438. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  439. /* Only allow wildcard SSID match if an AP
  440. * advertises active WPS operation that matches
  441. * with our mode. */
  442. check_ssid = 1;
  443. if (ssid->ssid_len == 0 &&
  444. wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
  445. bss))
  446. check_ssid = 0;
  447. }
  448. #endif /* CONFIG_WPS */
  449. if (check_ssid &&
  450. (ssid_len != ssid->ssid_len ||
  451. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  452. wpa_printf(MSG_DEBUG, " skip - "
  453. "SSID mismatch");
  454. continue;
  455. }
  456. if (ssid->bssid_set &&
  457. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  458. {
  459. wpa_printf(MSG_DEBUG, " skip - "
  460. "BSSID mismatch");
  461. continue;
  462. }
  463. if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  464. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  465. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
  466. {
  467. wpa_printf(MSG_DEBUG, " skip - "
  468. "non-WPA network not allowed");
  469. continue;
  470. }
  471. if ((ssid->key_mgmt &
  472. (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  473. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
  474. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  475. WPA_KEY_MGMT_PSK_SHA256)) &&
  476. (wpa_ie_len != 0 || rsn_ie_len != 0)) {
  477. wpa_printf(MSG_DEBUG, " skip - "
  478. "WPA network");
  479. continue;
  480. }
  481. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  482. wpa_printf(MSG_DEBUG, " skip - "
  483. "privacy mismatch");
  484. continue;
  485. }
  486. if (bss->caps & IEEE80211_CAP_IBSS) {
  487. wpa_printf(MSG_DEBUG, " skip - "
  488. "IBSS (adhoc) network");
  489. continue;
  490. }
  491. wpa_printf(MSG_DEBUG, " selected non-WPA AP "
  492. MACSTR " ssid='%s'",
  493. MAC2STR(bss->bssid),
  494. wpa_ssid_txt(ssid_, ssid_len));
  495. *selected_ssid = ssid;
  496. return bss;
  497. }
  498. }
  499. return NULL;
  500. }
  501. static struct wpa_scan_res *
  502. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
  503. struct wpa_ssid **selected_ssid)
  504. {
  505. struct wpa_scan_res *selected;
  506. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  507. group->priority);
  508. /* First, try to find WPA-enabled AP */
  509. selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
  510. if (selected)
  511. return selected;
  512. /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
  513. * allows this. */
  514. return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
  515. }
  516. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
  517. {
  518. int prio, timeout;
  519. struct wpa_scan_res *selected = NULL;
  520. struct wpa_ssid *ssid = NULL;
  521. wpa_supplicant_notify_scanning(wpa_s, 0);
  522. if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
  523. if (wpa_s->conf->ap_scan == 2)
  524. return;
  525. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  526. "scanning again");
  527. timeout = 1;
  528. goto req_scan;
  529. }
  530. /*
  531. * Don't post the results if this was the initial cached
  532. * and there were no results.
  533. */
  534. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  535. wpa_s->scan_res->num == 0) {
  536. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  537. "empty - not posting");
  538. } else {
  539. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  540. wpas_notify_scan_results(wpa_s);
  541. }
  542. wpas_notify_scan_done(wpa_s, 1);
  543. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)) ||
  544. wpa_s->disconnected)
  545. return;
  546. while (selected == NULL) {
  547. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  548. selected = wpa_supplicant_select_bss(
  549. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  550. if (selected)
  551. break;
  552. }
  553. if (selected == NULL && wpa_s->blacklist) {
  554. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  555. "and try again");
  556. wpa_blacklist_clear(wpa_s);
  557. wpa_s->blacklist_cleared++;
  558. } else if (selected == NULL) {
  559. break;
  560. }
  561. }
  562. if (selected) {
  563. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  564. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  565. "PBC session overlap");
  566. timeout = 10;
  567. goto req_scan;
  568. }
  569. /* Do not trigger new association unless the BSSID has changed
  570. * or if reassociation is requested. If we are in process of
  571. * associating with the selected BSSID, do not trigger new
  572. * attempt. */
  573. if (wpa_s->reassociate ||
  574. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  575. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  576. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  577. ETH_ALEN) != 0))) {
  578. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  579. wpa_supplicant_req_scan(wpa_s, 10, 0);
  580. return;
  581. }
  582. wpa_supplicant_associate(wpa_s, selected, ssid);
  583. } else {
  584. wpa_printf(MSG_DEBUG, "Already associated with the "
  585. "selected AP.");
  586. }
  587. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  588. } else {
  589. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  590. timeout = 5;
  591. goto req_scan;
  592. }
  593. return;
  594. req_scan:
  595. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  596. /*
  597. * Quick recovery if the initial scan results were not
  598. * complete when fetched before the first scan request.
  599. */
  600. wpa_s->scan_res_tried++;
  601. timeout = 0;
  602. }
  603. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  604. }
  605. #endif /* CONFIG_NO_SCAN_PROCESSING */
  606. #ifdef CONFIG_IEEE80211R
  607. static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
  608. const u8 *ftie, const u8 *mdie)
  609. {
  610. const u8 *mobility_domain = NULL;
  611. const u8 *r0kh_id = NULL;
  612. size_t r0kh_id_len = 0;
  613. const u8 *r1kh_id = NULL;
  614. struct rsn_ftie *hdr;
  615. const u8 *pos, *end;
  616. if (mdie == NULL || ftie == NULL)
  617. return;
  618. if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
  619. mobility_domain = mdie + 2;
  620. #ifdef CONFIG_SME
  621. wpa_s->sme.ft_used = 1;
  622. os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
  623. #endif /* CONFIG_SME */
  624. }
  625. if (ftie[1] >= sizeof(struct rsn_ftie)) {
  626. end = ftie + 2 + ftie[1];
  627. hdr = (struct rsn_ftie *) (ftie + 2);
  628. pos = (const u8 *) (hdr + 1);
  629. while (pos + 1 < end) {
  630. if (pos + 2 + pos[1] > end)
  631. break;
  632. if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
  633. pos[1] == FT_R1KH_ID_LEN)
  634. r1kh_id = pos + 2;
  635. else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
  636. pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
  637. r0kh_id = pos + 2;
  638. r0kh_id_len = pos[1];
  639. }
  640. pos += 2 + pos[1];
  641. }
  642. }
  643. wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
  644. r0kh_id_len, r1kh_id);
  645. }
  646. #endif /* CONFIG_IEEE80211R */
  647. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  648. union wpa_event_data *data)
  649. {
  650. int l, len, found = 0, wpa_found, rsn_found;
  651. const u8 *p;
  652. #ifdef CONFIG_IEEE80211R
  653. const u8 *mdie = NULL, *ftie = NULL;
  654. #endif /* CONFIG_IEEE80211R */
  655. wpa_printf(MSG_DEBUG, "Association info event");
  656. if (data->assoc_info.req_ies)
  657. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  658. data->assoc_info.req_ies_len);
  659. if (data->assoc_info.resp_ies)
  660. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  661. data->assoc_info.resp_ies_len);
  662. if (data->assoc_info.beacon_ies)
  663. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  664. data->assoc_info.beacon_ies,
  665. data->assoc_info.beacon_ies_len);
  666. p = data->assoc_info.req_ies;
  667. l = data->assoc_info.req_ies_len;
  668. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  669. while (p && l >= 2) {
  670. len = p[1] + 2;
  671. if (len > l) {
  672. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  673. p, l);
  674. break;
  675. }
  676. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  677. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  678. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  679. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  680. break;
  681. found = 1;
  682. wpa_find_assoc_pmkid(wpa_s);
  683. break;
  684. }
  685. l -= len;
  686. p += len;
  687. }
  688. if (!found && data->assoc_info.req_ies)
  689. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  690. #ifdef CONFIG_IEEE80211R
  691. p = data->assoc_info.resp_ies;
  692. l = data->assoc_info.resp_ies_len;
  693. /* Go through the IEs and make a copy of the FT/MD IE, if present. */
  694. while (p && l >= 2) {
  695. len = p[1] + 2;
  696. if (len > l) {
  697. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  698. p, l);
  699. break;
  700. }
  701. if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
  702. ftie = p;
  703. else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
  704. mdie = p;
  705. l -= len;
  706. p += len;
  707. }
  708. wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
  709. #endif /* CONFIG_IEEE80211R */
  710. /* WPA/RSN IE from Beacon/ProbeResp */
  711. p = data->assoc_info.beacon_ies;
  712. l = data->assoc_info.beacon_ies_len;
  713. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  714. */
  715. wpa_found = rsn_found = 0;
  716. while (p && l >= 2) {
  717. len = p[1] + 2;
  718. if (len > l) {
  719. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  720. p, l);
  721. break;
  722. }
  723. if (!wpa_found &&
  724. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  725. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  726. wpa_found = 1;
  727. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  728. }
  729. if (!rsn_found &&
  730. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  731. rsn_found = 1;
  732. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  733. }
  734. l -= len;
  735. p += len;
  736. }
  737. if (!wpa_found && data->assoc_info.beacon_ies)
  738. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  739. if (!rsn_found && data->assoc_info.beacon_ies)
  740. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  741. if (wpa_found || rsn_found)
  742. wpa_s->ap_ies_from_associnfo = 1;
  743. }
  744. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  745. union wpa_event_data *data)
  746. {
  747. u8 bssid[ETH_ALEN];
  748. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  749. int bssid_changed;
  750. if (data)
  751. wpa_supplicant_event_associnfo(wpa_s, data);
  752. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  753. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  754. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  755. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  756. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  757. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  758. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  759. MACSTR, MAC2STR(bssid));
  760. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  761. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  762. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  763. if (bssid_changed)
  764. wpas_notify_bssid_changed(wpa_s);
  765. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  766. wpa_clear_keys(wpa_s, bssid);
  767. }
  768. if (wpa_supplicant_select_config(wpa_s) < 0) {
  769. wpa_supplicant_disassociate(
  770. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  771. return;
  772. }
  773. }
  774. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  775. if (wpa_s->current_ssid) {
  776. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  777. * initialized before association, but for other modes,
  778. * initialize PC/SC here, if the current configuration needs
  779. * smartcard or SIM/USIM. */
  780. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  781. }
  782. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  783. l2_packet_notify_auth_start(wpa_s->l2);
  784. /*
  785. * Set portEnabled first to FALSE in order to get EAP state machine out
  786. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  787. * state machine may transit to AUTHENTICATING state based on obsolete
  788. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  789. * AUTHENTICATED without ever giving chance to EAP state machine to
  790. * reset the state.
  791. */
  792. if (!ft_completed) {
  793. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  794. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  795. }
  796. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  797. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  798. /* 802.1X::portControl = Auto */
  799. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  800. wpa_s->eapol_received = 0;
  801. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  802. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  803. wpa_supplicant_cancel_auth_timeout(wpa_s);
  804. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  805. } else if (!ft_completed) {
  806. /* Timeout for receiving the first EAPOL packet */
  807. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  808. }
  809. wpa_supplicant_cancel_scan(wpa_s);
  810. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  811. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  812. /*
  813. * We are done; the driver will take care of RSN 4-way
  814. * handshake.
  815. */
  816. wpa_supplicant_cancel_auth_timeout(wpa_s);
  817. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  818. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  819. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  820. }
  821. if (wpa_s->pending_eapol_rx) {
  822. struct os_time now, age;
  823. os_get_time(&now);
  824. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  825. if (age.sec == 0 && age.usec < 100000 &&
  826. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  827. 0) {
  828. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  829. "that was received just before association "
  830. "notification");
  831. wpa_supplicant_rx_eapol(
  832. wpa_s, wpa_s->pending_eapol_rx_src,
  833. wpabuf_head(wpa_s->pending_eapol_rx),
  834. wpabuf_len(wpa_s->pending_eapol_rx));
  835. }
  836. wpabuf_free(wpa_s->pending_eapol_rx);
  837. wpa_s->pending_eapol_rx = NULL;
  838. }
  839. }
  840. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  841. {
  842. const u8 *bssid;
  843. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  844. /*
  845. * At least Host AP driver and a Prism3 card seemed to be
  846. * generating streams of disconnected events when configuring
  847. * IBSS for WPA-None. Ignore them for now.
  848. */
  849. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  850. "IBSS/WPA-None mode");
  851. return;
  852. }
  853. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  854. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  855. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  856. "pre-shared key may be incorrect");
  857. }
  858. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  859. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  860. bssid = wpa_s->bssid;
  861. if (is_zero_ether_addr(bssid))
  862. bssid = wpa_s->pending_bssid;
  863. wpa_blacklist_add(wpa_s, bssid);
  864. wpa_sm_notify_disassoc(wpa_s->wpa);
  865. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  866. "remove keys");
  867. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  868. wpa_s->keys_cleared = 0;
  869. wpa_clear_keys(wpa_s, wpa_s->bssid);
  870. }
  871. wpa_supplicant_mark_disassoc(wpa_s);
  872. }
  873. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  874. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  875. void *sock_ctx)
  876. {
  877. struct wpa_supplicant *wpa_s = eloop_ctx;
  878. if (!wpa_s->pending_mic_error_report)
  879. return;
  880. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  881. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  882. wpa_s->pending_mic_error_report = 0;
  883. }
  884. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  885. static void
  886. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  887. union wpa_event_data *data)
  888. {
  889. int pairwise;
  890. struct os_time t;
  891. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  892. pairwise = (data && data->michael_mic_failure.unicast);
  893. os_get_time(&t);
  894. if ((wpa_s->last_michael_mic_error &&
  895. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  896. wpa_s->pending_mic_error_report) {
  897. if (wpa_s->pending_mic_error_report) {
  898. /*
  899. * Send the pending MIC error report immediately since
  900. * we are going to start countermeasures and AP better
  901. * do the same.
  902. */
  903. wpa_sm_key_request(wpa_s->wpa, 1,
  904. wpa_s->pending_mic_error_pairwise);
  905. }
  906. /* Send the new MIC error report immediately since we are going
  907. * to start countermeasures and AP better do the same.
  908. */
  909. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  910. /* initialize countermeasures */
  911. wpa_s->countermeasures = 1;
  912. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  913. /*
  914. * Need to wait for completion of request frame. We do not get
  915. * any callback for the message completion, so just wait a
  916. * short while and hope for the best. */
  917. os_sleep(0, 10000);
  918. wpa_drv_set_countermeasures(wpa_s, 1);
  919. wpa_supplicant_deauthenticate(wpa_s,
  920. WLAN_REASON_MICHAEL_MIC_FAILURE);
  921. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  922. wpa_s, NULL);
  923. eloop_register_timeout(60, 0,
  924. wpa_supplicant_stop_countermeasures,
  925. wpa_s, NULL);
  926. /* TODO: mark the AP rejected for 60 second. STA is
  927. * allowed to associate with another AP.. */
  928. } else {
  929. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  930. if (wpa_s->mic_errors_seen) {
  931. /*
  932. * Reduce the effectiveness of Michael MIC error
  933. * reports as a means for attacking against TKIP if
  934. * more than one MIC failure is noticed with the same
  935. * PTK. We delay the transmission of the reports by a
  936. * random time between 0 and 60 seconds in order to
  937. * force the attacker wait 60 seconds before getting
  938. * the information on whether a frame resulted in a MIC
  939. * failure.
  940. */
  941. u8 rval[4];
  942. int sec;
  943. if (os_get_random(rval, sizeof(rval)) < 0)
  944. sec = os_random() % 60;
  945. else
  946. sec = WPA_GET_BE32(rval) % 60;
  947. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  948. "seconds", sec);
  949. wpa_s->pending_mic_error_report = 1;
  950. wpa_s->pending_mic_error_pairwise = pairwise;
  951. eloop_cancel_timeout(
  952. wpa_supplicant_delayed_mic_error_report,
  953. wpa_s, NULL);
  954. eloop_register_timeout(
  955. sec, os_random() % 1000000,
  956. wpa_supplicant_delayed_mic_error_report,
  957. wpa_s, NULL);
  958. } else {
  959. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  960. }
  961. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  962. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  963. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  964. }
  965. wpa_s->last_michael_mic_error = t.sec;
  966. wpa_s->mic_errors_seen++;
  967. }
  968. #ifdef CONFIG_TERMINATE_ONLASTIF
  969. static int any_interfaces(struct wpa_supplicant *head)
  970. {
  971. struct wpa_supplicant *wpa_s;
  972. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  973. if (!wpa_s->interface_removed)
  974. return 1;
  975. return 0;
  976. }
  977. #endif /* CONFIG_TERMINATE_ONLASTIF */
  978. static void
  979. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  980. union wpa_event_data *data)
  981. {
  982. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  983. return;
  984. switch (data->interface_status.ievent) {
  985. case EVENT_INTERFACE_ADDED:
  986. if (!wpa_s->interface_removed)
  987. break;
  988. wpa_s->interface_removed = 0;
  989. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  990. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  991. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  992. "after interface was added.");
  993. }
  994. break;
  995. case EVENT_INTERFACE_REMOVED:
  996. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  997. wpa_s->interface_removed = 1;
  998. wpa_supplicant_mark_disassoc(wpa_s);
  999. l2_packet_deinit(wpa_s->l2);
  1000. wpa_s->l2 = NULL;
  1001. #ifdef CONFIG_TERMINATE_ONLASTIF
  1002. /* check if last interface */
  1003. if (!any_interfaces(wpa_s->global->ifaces))
  1004. eloop_terminate();
  1005. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1006. break;
  1007. }
  1008. }
  1009. #ifdef CONFIG_PEERKEY
  1010. static void
  1011. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1012. union wpa_event_data *data)
  1013. {
  1014. if (data == NULL)
  1015. return;
  1016. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1017. }
  1018. #endif /* CONFIG_PEERKEY */
  1019. #ifdef CONFIG_IEEE80211R
  1020. static void
  1021. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1022. union wpa_event_data *data)
  1023. {
  1024. if (data == NULL)
  1025. return;
  1026. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1027. data->ft_ies.ies_len,
  1028. data->ft_ies.ft_action,
  1029. data->ft_ies.target_ap,
  1030. data->ft_ies.ric_ies,
  1031. data->ft_ies.ric_ies_len) < 0) {
  1032. /* TODO: prevent MLME/driver from trying to associate? */
  1033. }
  1034. }
  1035. #endif /* CONFIG_IEEE80211R */
  1036. #ifdef CONFIG_IBSS_RSN
  1037. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1038. union wpa_event_data *data)
  1039. {
  1040. if (data == NULL)
  1041. return;
  1042. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1043. }
  1044. #endif /* CONFIG_IBSS_RSN */
  1045. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  1046. union wpa_event_data *data)
  1047. {
  1048. struct wpa_supplicant *wpa_s = ctx;
  1049. switch (event) {
  1050. case EVENT_AUTH:
  1051. sme_event_auth(wpa_s, data);
  1052. break;
  1053. case EVENT_ASSOC:
  1054. wpa_supplicant_event_assoc(wpa_s, data);
  1055. break;
  1056. case EVENT_DEAUTH:
  1057. case EVENT_DISASSOC:
  1058. wpa_supplicant_event_disassoc(wpa_s);
  1059. break;
  1060. case EVENT_MICHAEL_MIC_FAILURE:
  1061. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1062. break;
  1063. #ifndef CONFIG_NO_SCAN_PROCESSING
  1064. case EVENT_SCAN_RESULTS:
  1065. wpa_supplicant_event_scan_results(wpa_s);
  1066. break;
  1067. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1068. case EVENT_ASSOCINFO:
  1069. wpa_supplicant_event_associnfo(wpa_s, data);
  1070. break;
  1071. case EVENT_INTERFACE_STATUS:
  1072. wpa_supplicant_event_interface_status(wpa_s, data);
  1073. break;
  1074. case EVENT_PMKID_CANDIDATE:
  1075. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1076. break;
  1077. #ifdef CONFIG_PEERKEY
  1078. case EVENT_STKSTART:
  1079. wpa_supplicant_event_stkstart(wpa_s, data);
  1080. break;
  1081. #endif /* CONFIG_PEERKEY */
  1082. #ifdef CONFIG_IEEE80211R
  1083. case EVENT_FT_RESPONSE:
  1084. wpa_supplicant_event_ft_response(wpa_s, data);
  1085. break;
  1086. #endif /* CONFIG_IEEE80211R */
  1087. #ifdef CONFIG_IBSS_RSN
  1088. case EVENT_IBSS_RSN_START:
  1089. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1090. break;
  1091. #endif /* CONFIG_IBSS_RSN */
  1092. case EVENT_ASSOC_REJECT:
  1093. sme_event_assoc_reject(wpa_s, data);
  1094. break;
  1095. case EVENT_AUTH_TIMED_OUT:
  1096. sme_event_auth_timed_out(wpa_s, data);
  1097. break;
  1098. case EVENT_ASSOC_TIMED_OUT:
  1099. sme_event_assoc_timed_out(wpa_s, data);
  1100. break;
  1101. default:
  1102. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1103. break;
  1104. }
  1105. }