events.c 39 KB

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