events.c 37 KB

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