events.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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. return;
  545. if (wpa_s->disconnected) {
  546. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  547. return;
  548. }
  549. while (selected == NULL) {
  550. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  551. selected = wpa_supplicant_select_bss(
  552. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  553. if (selected)
  554. break;
  555. }
  556. if (selected == NULL && wpa_s->blacklist) {
  557. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  558. "and try again");
  559. wpa_blacklist_clear(wpa_s);
  560. wpa_s->blacklist_cleared++;
  561. } else if (selected == NULL) {
  562. break;
  563. }
  564. }
  565. if (selected) {
  566. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  567. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  568. "PBC session overlap");
  569. timeout = 10;
  570. goto req_scan;
  571. }
  572. /* Do not trigger new association unless the BSSID has changed
  573. * or if reassociation is requested. If we are in process of
  574. * associating with the selected BSSID, do not trigger new
  575. * attempt. */
  576. if (wpa_s->reassociate ||
  577. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  578. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  579. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  580. ETH_ALEN) != 0))) {
  581. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  582. wpa_supplicant_req_scan(wpa_s, 10, 0);
  583. return;
  584. }
  585. wpa_supplicant_associate(wpa_s, selected, ssid);
  586. } else {
  587. wpa_printf(MSG_DEBUG, "Already associated with the "
  588. "selected AP.");
  589. }
  590. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  591. } else {
  592. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  593. timeout = 5;
  594. goto req_scan;
  595. }
  596. return;
  597. req_scan:
  598. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  599. /*
  600. * Quick recovery if the initial scan results were not
  601. * complete when fetched before the first scan request.
  602. */
  603. wpa_s->scan_res_tried++;
  604. timeout = 0;
  605. }
  606. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  607. }
  608. #endif /* CONFIG_NO_SCAN_PROCESSING */
  609. #ifdef CONFIG_IEEE80211R
  610. static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
  611. const u8 *ftie, const u8 *mdie)
  612. {
  613. const u8 *mobility_domain = NULL;
  614. const u8 *r0kh_id = NULL;
  615. size_t r0kh_id_len = 0;
  616. const u8 *r1kh_id = NULL;
  617. struct rsn_ftie *hdr;
  618. const u8 *pos, *end;
  619. if (mdie == NULL || ftie == NULL)
  620. return;
  621. if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
  622. mobility_domain = mdie + 2;
  623. #ifdef CONFIG_SME
  624. wpa_s->sme.ft_used = 1;
  625. os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
  626. #endif /* CONFIG_SME */
  627. }
  628. if (ftie[1] >= sizeof(struct rsn_ftie)) {
  629. end = ftie + 2 + ftie[1];
  630. hdr = (struct rsn_ftie *) (ftie + 2);
  631. pos = (const u8 *) (hdr + 1);
  632. while (pos + 1 < end) {
  633. if (pos + 2 + pos[1] > end)
  634. break;
  635. if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
  636. pos[1] == FT_R1KH_ID_LEN)
  637. r1kh_id = pos + 2;
  638. else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
  639. pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
  640. r0kh_id = pos + 2;
  641. r0kh_id_len = pos[1];
  642. }
  643. pos += 2 + pos[1];
  644. }
  645. }
  646. wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
  647. r0kh_id_len, r1kh_id);
  648. }
  649. #endif /* CONFIG_IEEE80211R */
  650. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  651. union wpa_event_data *data)
  652. {
  653. int l, len, found = 0, wpa_found, rsn_found;
  654. const u8 *p;
  655. #ifdef CONFIG_IEEE80211R
  656. const u8 *mdie = NULL, *ftie = NULL;
  657. #endif /* CONFIG_IEEE80211R */
  658. wpa_printf(MSG_DEBUG, "Association info event");
  659. if (data->assoc_info.req_ies)
  660. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  661. data->assoc_info.req_ies_len);
  662. if (data->assoc_info.resp_ies)
  663. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  664. data->assoc_info.resp_ies_len);
  665. if (data->assoc_info.beacon_ies)
  666. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  667. data->assoc_info.beacon_ies,
  668. data->assoc_info.beacon_ies_len);
  669. p = data->assoc_info.req_ies;
  670. l = data->assoc_info.req_ies_len;
  671. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  672. while (p && l >= 2) {
  673. len = p[1] + 2;
  674. if (len > l) {
  675. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  676. p, l);
  677. break;
  678. }
  679. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  680. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  681. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  682. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  683. break;
  684. found = 1;
  685. wpa_find_assoc_pmkid(wpa_s);
  686. break;
  687. }
  688. l -= len;
  689. p += len;
  690. }
  691. if (!found && data->assoc_info.req_ies)
  692. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  693. #ifdef CONFIG_IEEE80211R
  694. p = data->assoc_info.resp_ies;
  695. l = data->assoc_info.resp_ies_len;
  696. /* Go through the IEs and make a copy of the FT/MD IE, if present. */
  697. while (p && l >= 2) {
  698. len = p[1] + 2;
  699. if (len > l) {
  700. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  701. p, l);
  702. break;
  703. }
  704. if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
  705. ftie = p;
  706. else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
  707. mdie = p;
  708. l -= len;
  709. p += len;
  710. }
  711. wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
  712. #endif /* CONFIG_IEEE80211R */
  713. /* WPA/RSN IE from Beacon/ProbeResp */
  714. p = data->assoc_info.beacon_ies;
  715. l = data->assoc_info.beacon_ies_len;
  716. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  717. */
  718. wpa_found = rsn_found = 0;
  719. while (p && l >= 2) {
  720. len = p[1] + 2;
  721. if (len > l) {
  722. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  723. p, l);
  724. break;
  725. }
  726. if (!wpa_found &&
  727. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  728. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  729. wpa_found = 1;
  730. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  731. }
  732. if (!rsn_found &&
  733. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  734. rsn_found = 1;
  735. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  736. }
  737. l -= len;
  738. p += len;
  739. }
  740. if (!wpa_found && data->assoc_info.beacon_ies)
  741. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  742. if (!rsn_found && data->assoc_info.beacon_ies)
  743. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  744. if (wpa_found || rsn_found)
  745. wpa_s->ap_ies_from_associnfo = 1;
  746. }
  747. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  748. union wpa_event_data *data)
  749. {
  750. u8 bssid[ETH_ALEN];
  751. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  752. int bssid_changed;
  753. if (data)
  754. wpa_supplicant_event_associnfo(wpa_s, data);
  755. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  756. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  757. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  758. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  759. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  760. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  761. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  762. MACSTR, MAC2STR(bssid));
  763. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  764. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  765. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  766. if (bssid_changed)
  767. wpas_notify_bssid_changed(wpa_s);
  768. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  769. wpa_clear_keys(wpa_s, bssid);
  770. }
  771. if (wpa_supplicant_select_config(wpa_s) < 0) {
  772. wpa_supplicant_disassociate(
  773. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  774. return;
  775. }
  776. }
  777. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  778. if (wpa_s->current_ssid) {
  779. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  780. * initialized before association, but for other modes,
  781. * initialize PC/SC here, if the current configuration needs
  782. * smartcard or SIM/USIM. */
  783. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  784. }
  785. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  786. l2_packet_notify_auth_start(wpa_s->l2);
  787. /*
  788. * Set portEnabled first to FALSE in order to get EAP state machine out
  789. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  790. * state machine may transit to AUTHENTICATING state based on obsolete
  791. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  792. * AUTHENTICATED without ever giving chance to EAP state machine to
  793. * reset the state.
  794. */
  795. if (!ft_completed) {
  796. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  797. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  798. }
  799. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  800. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  801. /* 802.1X::portControl = Auto */
  802. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  803. wpa_s->eapol_received = 0;
  804. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  805. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  806. wpa_supplicant_cancel_auth_timeout(wpa_s);
  807. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  808. } else if (!ft_completed) {
  809. /* Timeout for receiving the first EAPOL packet */
  810. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  811. }
  812. wpa_supplicant_cancel_scan(wpa_s);
  813. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  814. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  815. /*
  816. * We are done; the driver will take care of RSN 4-way
  817. * handshake.
  818. */
  819. wpa_supplicant_cancel_auth_timeout(wpa_s);
  820. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  821. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  822. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  823. }
  824. if (wpa_s->pending_eapol_rx) {
  825. struct os_time now, age;
  826. os_get_time(&now);
  827. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  828. if (age.sec == 0 && age.usec < 100000 &&
  829. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  830. 0) {
  831. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  832. "that was received just before association "
  833. "notification");
  834. wpa_supplicant_rx_eapol(
  835. wpa_s, wpa_s->pending_eapol_rx_src,
  836. wpabuf_head(wpa_s->pending_eapol_rx),
  837. wpabuf_len(wpa_s->pending_eapol_rx));
  838. }
  839. wpabuf_free(wpa_s->pending_eapol_rx);
  840. wpa_s->pending_eapol_rx = NULL;
  841. }
  842. }
  843. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  844. {
  845. const u8 *bssid;
  846. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  847. /*
  848. * At least Host AP driver and a Prism3 card seemed to be
  849. * generating streams of disconnected events when configuring
  850. * IBSS for WPA-None. Ignore them for now.
  851. */
  852. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  853. "IBSS/WPA-None mode");
  854. return;
  855. }
  856. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  857. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  858. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  859. "pre-shared key may be incorrect");
  860. }
  861. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  862. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  863. bssid = wpa_s->bssid;
  864. if (is_zero_ether_addr(bssid))
  865. bssid = wpa_s->pending_bssid;
  866. wpa_blacklist_add(wpa_s, bssid);
  867. wpa_sm_notify_disassoc(wpa_s->wpa);
  868. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  869. "remove keys");
  870. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  871. wpa_s->keys_cleared = 0;
  872. wpa_clear_keys(wpa_s, wpa_s->bssid);
  873. }
  874. wpa_supplicant_mark_disassoc(wpa_s);
  875. }
  876. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  877. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  878. void *sock_ctx)
  879. {
  880. struct wpa_supplicant *wpa_s = eloop_ctx;
  881. if (!wpa_s->pending_mic_error_report)
  882. return;
  883. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  884. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  885. wpa_s->pending_mic_error_report = 0;
  886. }
  887. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  888. static void
  889. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  890. union wpa_event_data *data)
  891. {
  892. int pairwise;
  893. struct os_time t;
  894. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  895. pairwise = (data && data->michael_mic_failure.unicast);
  896. os_get_time(&t);
  897. if ((wpa_s->last_michael_mic_error &&
  898. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  899. wpa_s->pending_mic_error_report) {
  900. if (wpa_s->pending_mic_error_report) {
  901. /*
  902. * Send the pending MIC error report immediately since
  903. * we are going to start countermeasures and AP better
  904. * do the same.
  905. */
  906. wpa_sm_key_request(wpa_s->wpa, 1,
  907. wpa_s->pending_mic_error_pairwise);
  908. }
  909. /* Send the new MIC error report immediately since we are going
  910. * to start countermeasures and AP better do the same.
  911. */
  912. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  913. /* initialize countermeasures */
  914. wpa_s->countermeasures = 1;
  915. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  916. /*
  917. * Need to wait for completion of request frame. We do not get
  918. * any callback for the message completion, so just wait a
  919. * short while and hope for the best. */
  920. os_sleep(0, 10000);
  921. wpa_drv_set_countermeasures(wpa_s, 1);
  922. wpa_supplicant_deauthenticate(wpa_s,
  923. WLAN_REASON_MICHAEL_MIC_FAILURE);
  924. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  925. wpa_s, NULL);
  926. eloop_register_timeout(60, 0,
  927. wpa_supplicant_stop_countermeasures,
  928. wpa_s, NULL);
  929. /* TODO: mark the AP rejected for 60 second. STA is
  930. * allowed to associate with another AP.. */
  931. } else {
  932. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  933. if (wpa_s->mic_errors_seen) {
  934. /*
  935. * Reduce the effectiveness of Michael MIC error
  936. * reports as a means for attacking against TKIP if
  937. * more than one MIC failure is noticed with the same
  938. * PTK. We delay the transmission of the reports by a
  939. * random time between 0 and 60 seconds in order to
  940. * force the attacker wait 60 seconds before getting
  941. * the information on whether a frame resulted in a MIC
  942. * failure.
  943. */
  944. u8 rval[4];
  945. int sec;
  946. if (os_get_random(rval, sizeof(rval)) < 0)
  947. sec = os_random() % 60;
  948. else
  949. sec = WPA_GET_BE32(rval) % 60;
  950. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  951. "seconds", sec);
  952. wpa_s->pending_mic_error_report = 1;
  953. wpa_s->pending_mic_error_pairwise = pairwise;
  954. eloop_cancel_timeout(
  955. wpa_supplicant_delayed_mic_error_report,
  956. wpa_s, NULL);
  957. eloop_register_timeout(
  958. sec, os_random() % 1000000,
  959. wpa_supplicant_delayed_mic_error_report,
  960. wpa_s, NULL);
  961. } else {
  962. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  963. }
  964. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  965. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  966. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  967. }
  968. wpa_s->last_michael_mic_error = t.sec;
  969. wpa_s->mic_errors_seen++;
  970. }
  971. #ifdef CONFIG_TERMINATE_ONLASTIF
  972. static int any_interfaces(struct wpa_supplicant *head)
  973. {
  974. struct wpa_supplicant *wpa_s;
  975. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  976. if (!wpa_s->interface_removed)
  977. return 1;
  978. return 0;
  979. }
  980. #endif /* CONFIG_TERMINATE_ONLASTIF */
  981. static void
  982. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  983. union wpa_event_data *data)
  984. {
  985. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  986. return;
  987. switch (data->interface_status.ievent) {
  988. case EVENT_INTERFACE_ADDED:
  989. if (!wpa_s->interface_removed)
  990. break;
  991. wpa_s->interface_removed = 0;
  992. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  993. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  994. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  995. "after interface was added.");
  996. }
  997. break;
  998. case EVENT_INTERFACE_REMOVED:
  999. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1000. wpa_s->interface_removed = 1;
  1001. wpa_supplicant_mark_disassoc(wpa_s);
  1002. l2_packet_deinit(wpa_s->l2);
  1003. wpa_s->l2 = NULL;
  1004. #ifdef CONFIG_TERMINATE_ONLASTIF
  1005. /* check if last interface */
  1006. if (!any_interfaces(wpa_s->global->ifaces))
  1007. eloop_terminate();
  1008. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1009. break;
  1010. }
  1011. }
  1012. #ifdef CONFIG_PEERKEY
  1013. static void
  1014. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1015. union wpa_event_data *data)
  1016. {
  1017. if (data == NULL)
  1018. return;
  1019. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1020. }
  1021. #endif /* CONFIG_PEERKEY */
  1022. #ifdef CONFIG_IEEE80211R
  1023. static void
  1024. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1025. union wpa_event_data *data)
  1026. {
  1027. if (data == NULL)
  1028. return;
  1029. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1030. data->ft_ies.ies_len,
  1031. data->ft_ies.ft_action,
  1032. data->ft_ies.target_ap,
  1033. data->ft_ies.ric_ies,
  1034. data->ft_ies.ric_ies_len) < 0) {
  1035. /* TODO: prevent MLME/driver from trying to associate? */
  1036. }
  1037. }
  1038. #endif /* CONFIG_IEEE80211R */
  1039. #ifdef CONFIG_IBSS_RSN
  1040. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1041. union wpa_event_data *data)
  1042. {
  1043. if (data == NULL)
  1044. return;
  1045. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1046. }
  1047. #endif /* CONFIG_IBSS_RSN */
  1048. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  1049. union wpa_event_data *data)
  1050. {
  1051. struct wpa_supplicant *wpa_s = ctx;
  1052. switch (event) {
  1053. case EVENT_AUTH:
  1054. sme_event_auth(wpa_s, data);
  1055. break;
  1056. case EVENT_ASSOC:
  1057. wpa_supplicant_event_assoc(wpa_s, data);
  1058. break;
  1059. case EVENT_DEAUTH:
  1060. case EVENT_DISASSOC:
  1061. wpa_supplicant_event_disassoc(wpa_s);
  1062. break;
  1063. case EVENT_MICHAEL_MIC_FAILURE:
  1064. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1065. break;
  1066. #ifndef CONFIG_NO_SCAN_PROCESSING
  1067. case EVENT_SCAN_RESULTS:
  1068. wpa_supplicant_event_scan_results(wpa_s);
  1069. break;
  1070. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1071. case EVENT_ASSOCINFO:
  1072. wpa_supplicant_event_associnfo(wpa_s, data);
  1073. break;
  1074. case EVENT_INTERFACE_STATUS:
  1075. wpa_supplicant_event_interface_status(wpa_s, data);
  1076. break;
  1077. case EVENT_PMKID_CANDIDATE:
  1078. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1079. break;
  1080. #ifdef CONFIG_PEERKEY
  1081. case EVENT_STKSTART:
  1082. wpa_supplicant_event_stkstart(wpa_s, data);
  1083. break;
  1084. #endif /* CONFIG_PEERKEY */
  1085. #ifdef CONFIG_IEEE80211R
  1086. case EVENT_FT_RESPONSE:
  1087. wpa_supplicant_event_ft_response(wpa_s, data);
  1088. break;
  1089. #endif /* CONFIG_IEEE80211R */
  1090. #ifdef CONFIG_IBSS_RSN
  1091. case EVENT_IBSS_RSN_START:
  1092. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1093. break;
  1094. #endif /* CONFIG_IBSS_RSN */
  1095. case EVENT_ASSOC_REJECT:
  1096. sme_event_assoc_reject(wpa_s, data);
  1097. break;
  1098. case EVENT_AUTH_TIMED_OUT:
  1099. sme_event_auth_timed_out(wpa_s, data);
  1100. break;
  1101. case EVENT_ASSOC_TIMED_OUT:
  1102. sme_event_assoc_timed_out(wpa_s, data);
  1103. break;
  1104. default:
  1105. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1106. break;
  1107. }
  1108. }