events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "wpa.h"
  18. #include "eloop.h"
  19. #include "drivers/driver.h"
  20. #include "config.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "pcsc_funcs.h"
  24. #include "preauth.h"
  25. #include "pmksa_cache.h"
  26. #include "wpa_ctrl.h"
  27. #include "eap_peer/eap.h"
  28. #include "ctrl_iface_dbus.h"
  29. #include "ieee802_11_defs.h"
  30. #include "blacklist.h"
  31. #include "wpas_glue.h"
  32. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  33. {
  34. struct wpa_ssid *ssid;
  35. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  36. return 0;
  37. wpa_printf(MSG_DEBUG, "Select network based on association "
  38. "information");
  39. ssid = wpa_supplicant_get_ssid(wpa_s);
  40. if (ssid == NULL) {
  41. wpa_printf(MSG_INFO, "No network configuration found for the "
  42. "current AP");
  43. return -1;
  44. }
  45. if (ssid->disabled) {
  46. wpa_printf(MSG_DEBUG, "Selected network is disabled");
  47. return -1;
  48. }
  49. wpa_printf(MSG_DEBUG, "Network configuration found for the current "
  50. "AP");
  51. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  52. WPA_KEY_MGMT_WPA_NONE |
  53. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X))
  54. {
  55. u8 wpa_ie[80];
  56. size_t wpa_ie_len = sizeof(wpa_ie);
  57. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  58. wpa_ie, &wpa_ie_len);
  59. } else {
  60. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  61. }
  62. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  63. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  64. wpa_s->current_ssid = ssid;
  65. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  66. wpa_supplicant_initiate_eapol(wpa_s);
  67. return 0;
  68. }
  69. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  70. void *sock_ctx)
  71. {
  72. struct wpa_supplicant *wpa_s = eloop_ctx;
  73. if (wpa_s->countermeasures) {
  74. wpa_s->countermeasures = 0;
  75. wpa_drv_set_countermeasures(wpa_s, 0);
  76. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  77. wpa_supplicant_req_scan(wpa_s, 0, 0);
  78. }
  79. }
  80. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  81. {
  82. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  83. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  84. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  85. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  86. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  87. if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
  88. wpa_s->key_mgmt == WPA_KEY_MGMT_FT_PSK)
  89. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  90. wpa_s->ap_ies_from_associnfo = 0;
  91. }
  92. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  93. {
  94. struct wpa_ie_data ie;
  95. int pmksa_set = -1;
  96. size_t i;
  97. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  98. ie.pmkid == NULL)
  99. return;
  100. for (i = 0; i < ie.num_pmkid; i++) {
  101. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  102. ie.pmkid + i * PMKID_LEN,
  103. NULL, NULL, 0);
  104. if (pmksa_set == 0) {
  105. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  106. break;
  107. }
  108. }
  109. wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
  110. "cache", pmksa_set == 0 ? "" : "not ");
  111. }
  112. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  113. union wpa_event_data *data)
  114. {
  115. if (data == NULL) {
  116. wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
  117. return;
  118. }
  119. wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  120. " index=%d preauth=%d",
  121. MAC2STR(data->pmkid_candidate.bssid),
  122. data->pmkid_candidate.index,
  123. data->pmkid_candidate.preauth);
  124. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  125. data->pmkid_candidate.index,
  126. data->pmkid_candidate.preauth);
  127. }
  128. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  129. {
  130. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  131. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  132. return 0;
  133. #ifdef IEEE8021X_EAPOL
  134. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  135. wpa_s->current_ssid &&
  136. !(wpa_s->current_ssid->eapol_flags &
  137. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  138. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  139. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  140. * plaintext or static WEP keys). */
  141. return 0;
  142. }
  143. #endif /* IEEE8021X_EAPOL */
  144. return 1;
  145. }
  146. /**
  147. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  148. * @wpa_s: pointer to wpa_supplicant data
  149. * @ssid: Configuration data for the network
  150. * Returns: 0 on success, -1 on failure
  151. *
  152. * This function is called when starting authentication with a network that is
  153. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  154. */
  155. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  156. struct wpa_ssid *ssid)
  157. {
  158. #ifdef IEEE8021X_EAPOL
  159. int aka = 0, sim = 0, type;
  160. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  161. return 0;
  162. if (ssid->eap.eap_methods == NULL) {
  163. sim = 1;
  164. aka = 1;
  165. } else {
  166. struct eap_method_type *eap = ssid->eap.eap_methods;
  167. while (eap->vendor != EAP_VENDOR_IETF ||
  168. eap->method != EAP_TYPE_NONE) {
  169. if (eap->vendor == EAP_VENDOR_IETF) {
  170. if (eap->method == EAP_TYPE_SIM)
  171. sim = 1;
  172. else if (eap->method == EAP_TYPE_AKA)
  173. aka = 1;
  174. }
  175. eap++;
  176. }
  177. }
  178. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  179. sim = 0;
  180. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  181. aka = 0;
  182. if (!sim && !aka) {
  183. wpa_printf(MSG_DEBUG, "Selected network is configured to use "
  184. "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
  185. return 0;
  186. }
  187. wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
  188. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  189. if (sim && aka)
  190. type = SCARD_TRY_BOTH;
  191. else if (aka)
  192. type = SCARD_USIM_ONLY;
  193. else
  194. type = SCARD_GSM_SIM_ONLY;
  195. wpa_s->scard = scard_init(type);
  196. if (wpa_s->scard == NULL) {
  197. wpa_printf(MSG_WARNING, "Failed to initialize SIM "
  198. "(pcsc-lite)");
  199. return -1;
  200. }
  201. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  202. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  203. #endif /* IEEE8021X_EAPOL */
  204. return 0;
  205. }
  206. #ifndef CONFIG_NO_SCAN_PROCESSING
  207. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  208. struct wpa_ssid *ssid)
  209. {
  210. int i, privacy = 0;
  211. if (ssid->mixed_cell)
  212. return 1;
  213. for (i = 0; i < NUM_WEP_KEYS; i++) {
  214. if (ssid->wep_key_len[i]) {
  215. privacy = 1;
  216. break;
  217. }
  218. }
  219. #ifdef IEEE8021X_EAPOL
  220. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  221. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  222. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  223. privacy = 1;
  224. #endif /* IEEE8021X_EAPOL */
  225. if (bss->caps & IEEE80211_CAP_PRIVACY)
  226. return privacy;
  227. return !privacy;
  228. }
  229. static int wpa_supplicant_ssid_bss_match(struct wpa_ssid *ssid,
  230. struct wpa_scan_res *bss)
  231. {
  232. struct wpa_ie_data ie;
  233. int proto_match = 0;
  234. const u8 *rsn_ie, *wpa_ie;
  235. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  236. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  237. proto_match++;
  238. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  239. wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
  240. break;
  241. }
  242. if (!(ie.proto & ssid->proto)) {
  243. wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
  244. "mismatch");
  245. break;
  246. }
  247. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  248. wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
  249. "mismatch");
  250. break;
  251. }
  252. if (!(ie.group_cipher & ssid->group_cipher)) {
  253. wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
  254. "mismatch");
  255. break;
  256. }
  257. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  258. wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
  259. "mismatch");
  260. break;
  261. }
  262. #ifdef CONFIG_IEEE80211W
  263. if (!(ie.capabilities & WPA_CAPABILITY_MGMT_FRAME_PROTECTION)
  264. && ssid->ieee80211w == IEEE80211W_REQUIRED) {
  265. wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
  266. "protection");
  267. break;
  268. }
  269. #endif /* CONFIG_IEEE80211W */
  270. wpa_printf(MSG_DEBUG, " selected based on RSN IE");
  271. return 1;
  272. }
  273. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  274. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  275. proto_match++;
  276. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  277. wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
  278. break;
  279. }
  280. if (!(ie.proto & ssid->proto)) {
  281. wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
  282. "mismatch");
  283. break;
  284. }
  285. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  286. wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
  287. "mismatch");
  288. break;
  289. }
  290. if (!(ie.group_cipher & ssid->group_cipher)) {
  291. wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
  292. "mismatch");
  293. break;
  294. }
  295. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  296. wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
  297. "mismatch");
  298. break;
  299. }
  300. wpa_printf(MSG_DEBUG, " selected based on WPA IE");
  301. return 1;
  302. }
  303. if (proto_match == 0)
  304. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  305. return 0;
  306. }
  307. static struct wpa_scan_res *
  308. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
  309. struct wpa_ssid **selected_ssid)
  310. {
  311. struct wpa_ssid *ssid;
  312. struct wpa_scan_res *bss, *selected = NULL;
  313. size_t i;
  314. struct wpa_blacklist *e;
  315. const u8 *ie;
  316. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  317. group->priority);
  318. bss = NULL;
  319. ssid = NULL;
  320. /* First, try to find WPA-enabled AP */
  321. wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
  322. for (i = 0; i < wpa_s->scan_res->num && !selected; i++) {
  323. const u8 *ssid_;
  324. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  325. bss = wpa_s->scan_res->res[i];
  326. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  327. ssid_ = ie ? ie + 2 : (u8 *) "";
  328. ssid_len = ie ? ie[1] : 0;
  329. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  330. wpa_ie_len = ie ? ie[1] : 0;
  331. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  332. rsn_ie_len = ie ? ie[1] : 0;
  333. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  334. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  335. (int) i, MAC2STR(bss->bssid),
  336. wpa_ssid_txt(ssid_, ssid_len),
  337. wpa_ie_len, rsn_ie_len, bss->caps);
  338. e = wpa_blacklist_get(wpa_s, bss->bssid);
  339. if (e && e->count > 1) {
  340. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  341. continue;
  342. }
  343. if (wpa_ie_len == 0 && rsn_ie_len == 0) {
  344. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
  345. continue;
  346. }
  347. for (ssid = group; ssid; ssid = ssid->pnext) {
  348. if (ssid->disabled) {
  349. wpa_printf(MSG_DEBUG, " skip - disabled");
  350. continue;
  351. }
  352. if (ssid_len != ssid->ssid_len ||
  353. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0) {
  354. wpa_printf(MSG_DEBUG, " skip - "
  355. "SSID mismatch");
  356. continue;
  357. }
  358. if (ssid->bssid_set &&
  359. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  360. {
  361. wpa_printf(MSG_DEBUG, " skip - "
  362. "BSSID mismatch");
  363. continue;
  364. }
  365. if (wpa_supplicant_ssid_bss_match(ssid, bss)) {
  366. selected = bss;
  367. *selected_ssid = ssid;
  368. wpa_printf(MSG_DEBUG, " selected WPA AP "
  369. MACSTR " ssid='%s'",
  370. MAC2STR(bss->bssid),
  371. wpa_ssid_txt(ssid_, ssid_len));
  372. break;
  373. }
  374. }
  375. }
  376. /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
  377. * allows this. */
  378. wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
  379. for (i = 0; i < wpa_s->scan_res->num && !selected; i++) {
  380. const u8 *ssid_;
  381. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  382. bss = wpa_s->scan_res->res[i];
  383. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  384. ssid_ = ie ? ie + 2 : (u8 *) "";
  385. ssid_len = ie ? ie[1] : 0;
  386. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  387. wpa_ie_len = ie ? ie[1] : 0;
  388. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  389. rsn_ie_len = ie ? ie[1] : 0;
  390. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  391. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  392. (int) i, MAC2STR(bss->bssid),
  393. wpa_ssid_txt(ssid_, ssid_len),
  394. wpa_ie_len, rsn_ie_len, bss->caps);
  395. e = wpa_blacklist_get(wpa_s, bss->bssid);
  396. if (e && e->count > 1) {
  397. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  398. continue;
  399. }
  400. for (ssid = group; ssid; ssid = ssid->pnext) {
  401. if (ssid->disabled) {
  402. wpa_printf(MSG_DEBUG, " skip - disabled");
  403. continue;
  404. }
  405. if (ssid->ssid_len != 0 &&
  406. (ssid_len != ssid->ssid_len ||
  407. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  408. wpa_printf(MSG_DEBUG, " skip - "
  409. "SSID mismatch");
  410. continue;
  411. }
  412. if (ssid->bssid_set &&
  413. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  414. {
  415. wpa_printf(MSG_DEBUG, " skip - "
  416. "BSSID mismatch");
  417. continue;
  418. }
  419. if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  420. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
  421. {
  422. wpa_printf(MSG_DEBUG, " skip - "
  423. "non-WPA network not allowed");
  424. continue;
  425. }
  426. if ((ssid->key_mgmt &
  427. (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK)) &&
  428. (wpa_ie_len != 0 || rsn_ie_len != 0)) {
  429. wpa_printf(MSG_DEBUG, " skip - "
  430. "WPA network");
  431. continue;
  432. }
  433. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  434. wpa_printf(MSG_DEBUG, " skip - "
  435. "privacy mismatch");
  436. continue;
  437. }
  438. if (bss->caps & IEEE80211_CAP_IBSS) {
  439. wpa_printf(MSG_DEBUG, " skip - "
  440. "IBSS (adhoc) network");
  441. continue;
  442. }
  443. selected = bss;
  444. *selected_ssid = ssid;
  445. wpa_printf(MSG_DEBUG, " selected non-WPA AP "
  446. MACSTR " ssid='%s'",
  447. MAC2STR(bss->bssid),
  448. wpa_ssid_txt(ssid_, ssid_len));
  449. break;
  450. }
  451. }
  452. return selected;
  453. }
  454. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
  455. {
  456. int prio, timeout;
  457. struct wpa_scan_res *selected = NULL;
  458. struct wpa_ssid *ssid = NULL;
  459. if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
  460. if (wpa_s->conf->ap_scan == 2)
  461. return;
  462. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  463. "scanning again");
  464. timeout = 1;
  465. goto req_scan;
  466. }
  467. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  468. wpa_supplicant_dbus_notify_scan_results(wpa_s);
  469. if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
  470. return;
  471. while (selected == NULL) {
  472. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  473. selected = wpa_supplicant_select_bss(
  474. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  475. if (selected)
  476. break;
  477. }
  478. if (selected == NULL && wpa_s->blacklist) {
  479. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  480. "and try again");
  481. wpa_blacklist_clear(wpa_s);
  482. } else if (selected == NULL) {
  483. break;
  484. }
  485. }
  486. if (selected) {
  487. /* Do not trigger new association unless the BSSID has changed
  488. * or if reassociation is requested. If we are in process of
  489. * associating with the selected BSSID, do not trigger new
  490. * attempt. */
  491. if (wpa_s->reassociate ||
  492. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  493. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  494. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  495. ETH_ALEN) != 0))) {
  496. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  497. wpa_supplicant_req_scan(wpa_s, 10, 0);
  498. return;
  499. }
  500. wpa_supplicant_associate(wpa_s, selected, ssid);
  501. } else {
  502. wpa_printf(MSG_DEBUG, "Already associated with the "
  503. "selected AP.");
  504. }
  505. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  506. } else {
  507. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  508. timeout = 5;
  509. goto req_scan;
  510. }
  511. return;
  512. req_scan:
  513. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  514. /*
  515. * Quick recovery if the initial scan results were not
  516. * complete when fetched before the first scan request.
  517. */
  518. wpa_s->scan_res_tried++;
  519. timeout = 0;
  520. }
  521. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  522. }
  523. #endif /* CONFIG_NO_SCAN_PROCESSING */
  524. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  525. union wpa_event_data *data)
  526. {
  527. int l, len, found = 0, wpa_found, rsn_found;
  528. u8 *p;
  529. wpa_printf(MSG_DEBUG, "Association info event");
  530. if (data->assoc_info.req_ies)
  531. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  532. data->assoc_info.req_ies_len);
  533. if (data->assoc_info.resp_ies)
  534. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  535. data->assoc_info.resp_ies_len);
  536. if (data->assoc_info.beacon_ies)
  537. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  538. data->assoc_info.beacon_ies,
  539. data->assoc_info.beacon_ies_len);
  540. p = data->assoc_info.req_ies;
  541. l = data->assoc_info.req_ies_len;
  542. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  543. while (p && l >= 2) {
  544. len = p[1] + 2;
  545. if (len > l) {
  546. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  547. p, l);
  548. break;
  549. }
  550. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  551. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  552. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  553. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  554. break;
  555. found = 1;
  556. wpa_find_assoc_pmkid(wpa_s);
  557. break;
  558. }
  559. l -= len;
  560. p += len;
  561. }
  562. if (!found && data->assoc_info.req_ies)
  563. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  564. /* WPA/RSN IE from Beacon/ProbeResp */
  565. p = data->assoc_info.beacon_ies;
  566. l = data->assoc_info.beacon_ies_len;
  567. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  568. */
  569. wpa_found = rsn_found = 0;
  570. while (p && l >= 2) {
  571. len = p[1] + 2;
  572. if (len > l) {
  573. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  574. p, l);
  575. break;
  576. }
  577. if (!wpa_found &&
  578. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  579. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  580. wpa_found = 1;
  581. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  582. }
  583. if (!rsn_found &&
  584. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  585. rsn_found = 1;
  586. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  587. }
  588. l -= len;
  589. p += len;
  590. }
  591. if (!wpa_found && data->assoc_info.beacon_ies)
  592. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  593. if (!rsn_found && data->assoc_info.beacon_ies)
  594. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  595. if (wpa_found || rsn_found)
  596. wpa_s->ap_ies_from_associnfo = 1;
  597. }
  598. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  599. union wpa_event_data *data)
  600. {
  601. u8 bssid[ETH_ALEN];
  602. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  603. if (data)
  604. wpa_supplicant_event_associnfo(wpa_s, data);
  605. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  606. if (wpa_s->use_client_mlme)
  607. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  608. if (wpa_s->use_client_mlme ||
  609. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  610. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  611. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  612. MACSTR, MAC2STR(bssid));
  613. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  614. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  615. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  616. wpa_clear_keys(wpa_s, bssid);
  617. }
  618. if (wpa_supplicant_select_config(wpa_s) < 0) {
  619. wpa_supplicant_disassociate(
  620. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  621. return;
  622. }
  623. }
  624. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  625. if (wpa_s->current_ssid) {
  626. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  627. * initialized before association, but for other modes,
  628. * initialize PC/SC here, if the current configuration needs
  629. * smartcard or SIM/USIM. */
  630. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  631. }
  632. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  633. l2_packet_notify_auth_start(wpa_s->l2);
  634. /*
  635. * Set portEnabled first to FALSE in order to get EAP state machine out
  636. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  637. * state machine may transit to AUTHENTICATING state based on obsolete
  638. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  639. * AUTHENTICATED without ever giving chance to EAP state machine to
  640. * reset the state.
  641. */
  642. if (!ft_completed) {
  643. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  644. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  645. }
  646. if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
  647. wpa_s->key_mgmt == WPA_KEY_MGMT_FT_PSK || ft_completed)
  648. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  649. /* 802.1X::portControl = Auto */
  650. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  651. wpa_s->eapol_received = 0;
  652. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  653. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  654. wpa_supplicant_cancel_auth_timeout(wpa_s);
  655. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  656. } else if (!ft_completed) {
  657. /* Timeout for receiving the first EAPOL packet */
  658. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  659. }
  660. wpa_supplicant_cancel_scan(wpa_s);
  661. if (wpa_s->driver_4way_handshake &&
  662. (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
  663. wpa_s->key_mgmt == WPA_KEY_MGMT_FT_PSK)) {
  664. /*
  665. * We are done; the driver will take care of RSN 4-way
  666. * handshake.
  667. */
  668. wpa_supplicant_cancel_auth_timeout(wpa_s);
  669. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  670. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  671. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  672. }
  673. }
  674. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  675. {
  676. const u8 *bssid;
  677. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  678. /*
  679. * At least Host AP driver and a Prism3 card seemed to be
  680. * generating streams of disconnected events when configuring
  681. * IBSS for WPA-None. Ignore them for now.
  682. */
  683. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  684. "IBSS/WPA-None mode");
  685. return;
  686. }
  687. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  688. (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
  689. wpa_s->key_mgmt == WPA_KEY_MGMT_FT_PSK)) {
  690. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  691. "pre-shared key may be incorrect");
  692. }
  693. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  694. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  695. bssid = wpa_s->bssid;
  696. if (os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)
  697. bssid = wpa_s->pending_bssid;
  698. wpa_blacklist_add(wpa_s, bssid);
  699. wpa_sm_notify_disassoc(wpa_s->wpa);
  700. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  701. "remove keys");
  702. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  703. wpa_s->keys_cleared = 0;
  704. wpa_clear_keys(wpa_s, wpa_s->bssid);
  705. }
  706. wpa_supplicant_mark_disassoc(wpa_s);
  707. }
  708. static void
  709. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  710. union wpa_event_data *data)
  711. {
  712. int pairwise;
  713. struct os_time t;
  714. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  715. pairwise = (data && data->michael_mic_failure.unicast);
  716. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  717. os_get_time(&t);
  718. if (wpa_s->last_michael_mic_error &&
  719. t.sec - wpa_s->last_michael_mic_error <= 60) {
  720. /* initialize countermeasures */
  721. wpa_s->countermeasures = 1;
  722. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  723. /*
  724. * Need to wait for completion of request frame. We do not get
  725. * any callback for the message completion, so just wait a
  726. * short while and hope for the best. */
  727. os_sleep(0, 10000);
  728. wpa_drv_set_countermeasures(wpa_s, 1);
  729. wpa_supplicant_deauthenticate(wpa_s,
  730. WLAN_REASON_MICHAEL_MIC_FAILURE);
  731. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  732. wpa_s, NULL);
  733. eloop_register_timeout(60, 0,
  734. wpa_supplicant_stop_countermeasures,
  735. wpa_s, NULL);
  736. /* TODO: mark the AP rejected for 60 second. STA is
  737. * allowed to associate with another AP.. */
  738. }
  739. wpa_s->last_michael_mic_error = t.sec;
  740. }
  741. static void
  742. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  743. union wpa_event_data *data)
  744. {
  745. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  746. return;
  747. switch (data->interface_status.ievent) {
  748. case EVENT_INTERFACE_ADDED:
  749. if (!wpa_s->interface_removed)
  750. break;
  751. wpa_s->interface_removed = 0;
  752. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  753. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  754. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  755. "after interface was added.");
  756. }
  757. break;
  758. case EVENT_INTERFACE_REMOVED:
  759. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  760. wpa_s->interface_removed = 1;
  761. wpa_supplicant_mark_disassoc(wpa_s);
  762. l2_packet_deinit(wpa_s->l2);
  763. wpa_s->l2 = NULL;
  764. break;
  765. }
  766. }
  767. #ifdef CONFIG_PEERKEY
  768. static void
  769. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  770. union wpa_event_data *data)
  771. {
  772. if (data == NULL)
  773. return;
  774. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  775. }
  776. #endif /* CONFIG_PEERKEY */
  777. #ifdef CONFIG_IEEE80211R
  778. static void
  779. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  780. union wpa_event_data *data)
  781. {
  782. if (data == NULL)
  783. return;
  784. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  785. data->ft_ies.ies_len,
  786. data->ft_ies.ft_action,
  787. data->ft_ies.target_ap) < 0) {
  788. /* TODO: prevent MLME/driver from trying to associate? */
  789. }
  790. }
  791. #endif /* CONFIG_IEEE80211R */
  792. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  793. union wpa_event_data *data)
  794. {
  795. struct wpa_supplicant *wpa_s = ctx;
  796. switch (event) {
  797. case EVENT_ASSOC:
  798. wpa_supplicant_event_assoc(wpa_s, data);
  799. break;
  800. case EVENT_DISASSOC:
  801. wpa_supplicant_event_disassoc(wpa_s);
  802. break;
  803. case EVENT_MICHAEL_MIC_FAILURE:
  804. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  805. break;
  806. #ifndef CONFIG_NO_SCAN_PROCESSING
  807. case EVENT_SCAN_RESULTS:
  808. wpa_supplicant_event_scan_results(wpa_s);
  809. break;
  810. #endif /* CONFIG_NO_SCAN_PROCESSING */
  811. case EVENT_ASSOCINFO:
  812. wpa_supplicant_event_associnfo(wpa_s, data);
  813. break;
  814. case EVENT_INTERFACE_STATUS:
  815. wpa_supplicant_event_interface_status(wpa_s, data);
  816. break;
  817. case EVENT_PMKID_CANDIDATE:
  818. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  819. break;
  820. #ifdef CONFIG_PEERKEY
  821. case EVENT_STKSTART:
  822. wpa_supplicant_event_stkstart(wpa_s, data);
  823. break;
  824. #endif /* CONFIG_PEERKEY */
  825. #ifdef CONFIG_IEEE80211R
  826. case EVENT_FT_RESPONSE:
  827. wpa_supplicant_event_ft_response(wpa_s, data);
  828. break;
  829. #endif /* CONFIG_IEEE80211R */
  830. default:
  831. wpa_printf(MSG_INFO, "Unknown event %d", event);
  832. break;
  833. }
  834. }