events.c 33 KB

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