events.c 40 KB

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