events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. WPA_KEY_MGMT_PSK_SHA256 |
  55. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  56. u8 wpa_ie[80];
  57. size_t wpa_ie_len = sizeof(wpa_ie);
  58. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  59. wpa_ie, &wpa_ie_len);
  60. } else {
  61. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  62. }
  63. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  64. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  65. wpa_s->current_ssid = ssid;
  66. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  67. wpa_supplicant_initiate_eapol(wpa_s);
  68. return 0;
  69. }
  70. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  71. void *sock_ctx)
  72. {
  73. struct wpa_supplicant *wpa_s = eloop_ctx;
  74. if (wpa_s->countermeasures) {
  75. wpa_s->countermeasures = 0;
  76. wpa_drv_set_countermeasures(wpa_s, 0);
  77. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  78. wpa_supplicant_req_scan(wpa_s, 0, 0);
  79. }
  80. }
  81. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  82. {
  83. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  84. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  85. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  86. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  87. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  88. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  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_MFPC) &&
  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_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
  429. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  430. WPA_KEY_MGMT_PSK_SHA256)) &&
  431. (wpa_ie_len != 0 || rsn_ie_len != 0)) {
  432. wpa_printf(MSG_DEBUG, " skip - "
  433. "WPA network");
  434. continue;
  435. }
  436. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  437. wpa_printf(MSG_DEBUG, " skip - "
  438. "privacy mismatch");
  439. continue;
  440. }
  441. if (bss->caps & IEEE80211_CAP_IBSS) {
  442. wpa_printf(MSG_DEBUG, " skip - "
  443. "IBSS (adhoc) network");
  444. continue;
  445. }
  446. selected = bss;
  447. *selected_ssid = ssid;
  448. wpa_printf(MSG_DEBUG, " selected non-WPA AP "
  449. MACSTR " ssid='%s'",
  450. MAC2STR(bss->bssid),
  451. wpa_ssid_txt(ssid_, ssid_len));
  452. break;
  453. }
  454. }
  455. return selected;
  456. }
  457. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
  458. {
  459. int prio, timeout;
  460. struct wpa_scan_res *selected = NULL;
  461. struct wpa_ssid *ssid = NULL;
  462. if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
  463. if (wpa_s->conf->ap_scan == 2)
  464. return;
  465. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  466. "scanning again");
  467. timeout = 1;
  468. goto req_scan;
  469. }
  470. /*
  471. * Don't post the results if this was the initial cached
  472. * and there were no results.
  473. */
  474. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  475. wpa_s->scan_res->num == 0) {
  476. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  477. "empty - not posting");
  478. } else {
  479. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  480. wpa_supplicant_dbus_notify_scan_results(wpa_s);
  481. }
  482. if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
  483. return;
  484. while (selected == NULL) {
  485. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  486. selected = wpa_supplicant_select_bss(
  487. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  488. if (selected)
  489. break;
  490. }
  491. if (selected == NULL && wpa_s->blacklist) {
  492. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  493. "and try again");
  494. wpa_blacklist_clear(wpa_s);
  495. } else if (selected == NULL) {
  496. break;
  497. }
  498. }
  499. if (selected) {
  500. /* Do not trigger new association unless the BSSID has changed
  501. * or if reassociation is requested. If we are in process of
  502. * associating with the selected BSSID, do not trigger new
  503. * attempt. */
  504. if (wpa_s->reassociate ||
  505. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  506. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  507. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  508. ETH_ALEN) != 0))) {
  509. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  510. wpa_supplicant_req_scan(wpa_s, 10, 0);
  511. return;
  512. }
  513. wpa_supplicant_associate(wpa_s, selected, ssid);
  514. } else {
  515. wpa_printf(MSG_DEBUG, "Already associated with the "
  516. "selected AP.");
  517. }
  518. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  519. } else {
  520. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  521. timeout = 5;
  522. goto req_scan;
  523. }
  524. return;
  525. req_scan:
  526. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  527. /*
  528. * Quick recovery if the initial scan results were not
  529. * complete when fetched before the first scan request.
  530. */
  531. wpa_s->scan_res_tried++;
  532. timeout = 0;
  533. }
  534. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  535. }
  536. #endif /* CONFIG_NO_SCAN_PROCESSING */
  537. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  538. union wpa_event_data *data)
  539. {
  540. int l, len, found = 0, wpa_found, rsn_found;
  541. u8 *p;
  542. wpa_printf(MSG_DEBUG, "Association info event");
  543. if (data->assoc_info.req_ies)
  544. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  545. data->assoc_info.req_ies_len);
  546. if (data->assoc_info.resp_ies)
  547. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  548. data->assoc_info.resp_ies_len);
  549. if (data->assoc_info.beacon_ies)
  550. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  551. data->assoc_info.beacon_ies,
  552. data->assoc_info.beacon_ies_len);
  553. p = data->assoc_info.req_ies;
  554. l = data->assoc_info.req_ies_len;
  555. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  556. while (p && l >= 2) {
  557. len = p[1] + 2;
  558. if (len > l) {
  559. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  560. p, l);
  561. break;
  562. }
  563. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  564. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  565. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  566. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  567. break;
  568. found = 1;
  569. wpa_find_assoc_pmkid(wpa_s);
  570. break;
  571. }
  572. l -= len;
  573. p += len;
  574. }
  575. if (!found && data->assoc_info.req_ies)
  576. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  577. /* WPA/RSN IE from Beacon/ProbeResp */
  578. p = data->assoc_info.beacon_ies;
  579. l = data->assoc_info.beacon_ies_len;
  580. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  581. */
  582. wpa_found = rsn_found = 0;
  583. while (p && l >= 2) {
  584. len = p[1] + 2;
  585. if (len > l) {
  586. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  587. p, l);
  588. break;
  589. }
  590. if (!wpa_found &&
  591. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  592. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  593. wpa_found = 1;
  594. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  595. }
  596. if (!rsn_found &&
  597. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  598. rsn_found = 1;
  599. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  600. }
  601. l -= len;
  602. p += len;
  603. }
  604. if (!wpa_found && data->assoc_info.beacon_ies)
  605. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  606. if (!rsn_found && data->assoc_info.beacon_ies)
  607. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  608. if (wpa_found || rsn_found)
  609. wpa_s->ap_ies_from_associnfo = 1;
  610. }
  611. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  612. union wpa_event_data *data)
  613. {
  614. u8 bssid[ETH_ALEN];
  615. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  616. if (data)
  617. wpa_supplicant_event_associnfo(wpa_s, data);
  618. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  619. if (wpa_s->use_client_mlme)
  620. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  621. if (wpa_s->use_client_mlme ||
  622. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  623. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  624. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  625. MACSTR, MAC2STR(bssid));
  626. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  627. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  628. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  629. wpa_clear_keys(wpa_s, bssid);
  630. }
  631. if (wpa_supplicant_select_config(wpa_s) < 0) {
  632. wpa_supplicant_disassociate(
  633. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  634. return;
  635. }
  636. }
  637. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  638. if (wpa_s->current_ssid) {
  639. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  640. * initialized before association, but for other modes,
  641. * initialize PC/SC here, if the current configuration needs
  642. * smartcard or SIM/USIM. */
  643. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  644. }
  645. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  646. l2_packet_notify_auth_start(wpa_s->l2);
  647. /*
  648. * Set portEnabled first to FALSE in order to get EAP state machine out
  649. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  650. * state machine may transit to AUTHENTICATING state based on obsolete
  651. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  652. * AUTHENTICATED without ever giving chance to EAP state machine to
  653. * reset the state.
  654. */
  655. if (!ft_completed) {
  656. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  657. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  658. }
  659. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  660. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  661. /* 802.1X::portControl = Auto */
  662. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  663. wpa_s->eapol_received = 0;
  664. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  665. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  666. wpa_supplicant_cancel_auth_timeout(wpa_s);
  667. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  668. } else if (!ft_completed) {
  669. /* Timeout for receiving the first EAPOL packet */
  670. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  671. }
  672. wpa_supplicant_cancel_scan(wpa_s);
  673. if (wpa_s->driver_4way_handshake &&
  674. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  675. /*
  676. * We are done; the driver will take care of RSN 4-way
  677. * handshake.
  678. */
  679. wpa_supplicant_cancel_auth_timeout(wpa_s);
  680. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  681. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  682. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  683. }
  684. }
  685. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  686. {
  687. const u8 *bssid;
  688. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  689. /*
  690. * At least Host AP driver and a Prism3 card seemed to be
  691. * generating streams of disconnected events when configuring
  692. * IBSS for WPA-None. Ignore them for now.
  693. */
  694. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  695. "IBSS/WPA-None mode");
  696. return;
  697. }
  698. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  699. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  700. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  701. "pre-shared key may be incorrect");
  702. }
  703. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  704. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  705. bssid = wpa_s->bssid;
  706. if (is_zero_ether_addr(bssid))
  707. bssid = wpa_s->pending_bssid;
  708. wpa_blacklist_add(wpa_s, bssid);
  709. wpa_sm_notify_disassoc(wpa_s->wpa);
  710. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  711. "remove keys");
  712. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  713. wpa_s->keys_cleared = 0;
  714. wpa_clear_keys(wpa_s, wpa_s->bssid);
  715. }
  716. wpa_supplicant_mark_disassoc(wpa_s);
  717. }
  718. static void
  719. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  720. union wpa_event_data *data)
  721. {
  722. int pairwise;
  723. struct os_time t;
  724. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  725. pairwise = (data && data->michael_mic_failure.unicast);
  726. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  727. os_get_time(&t);
  728. if (wpa_s->last_michael_mic_error &&
  729. t.sec - wpa_s->last_michael_mic_error <= 60) {
  730. /* initialize countermeasures */
  731. wpa_s->countermeasures = 1;
  732. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  733. /*
  734. * Need to wait for completion of request frame. We do not get
  735. * any callback for the message completion, so just wait a
  736. * short while and hope for the best. */
  737. os_sleep(0, 10000);
  738. wpa_drv_set_countermeasures(wpa_s, 1);
  739. wpa_supplicant_deauthenticate(wpa_s,
  740. WLAN_REASON_MICHAEL_MIC_FAILURE);
  741. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  742. wpa_s, NULL);
  743. eloop_register_timeout(60, 0,
  744. wpa_supplicant_stop_countermeasures,
  745. wpa_s, NULL);
  746. /* TODO: mark the AP rejected for 60 second. STA is
  747. * allowed to associate with another AP.. */
  748. }
  749. wpa_s->last_michael_mic_error = t.sec;
  750. }
  751. static void
  752. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  753. union wpa_event_data *data)
  754. {
  755. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  756. return;
  757. switch (data->interface_status.ievent) {
  758. case EVENT_INTERFACE_ADDED:
  759. if (!wpa_s->interface_removed)
  760. break;
  761. wpa_s->interface_removed = 0;
  762. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  763. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  764. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  765. "after interface was added.");
  766. }
  767. break;
  768. case EVENT_INTERFACE_REMOVED:
  769. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  770. wpa_s->interface_removed = 1;
  771. wpa_supplicant_mark_disassoc(wpa_s);
  772. l2_packet_deinit(wpa_s->l2);
  773. wpa_s->l2 = NULL;
  774. break;
  775. }
  776. }
  777. #ifdef CONFIG_PEERKEY
  778. static void
  779. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  780. union wpa_event_data *data)
  781. {
  782. if (data == NULL)
  783. return;
  784. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  785. }
  786. #endif /* CONFIG_PEERKEY */
  787. #ifdef CONFIG_IEEE80211R
  788. static void
  789. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  790. union wpa_event_data *data)
  791. {
  792. if (data == NULL)
  793. return;
  794. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  795. data->ft_ies.ies_len,
  796. data->ft_ies.ft_action,
  797. data->ft_ies.target_ap) < 0) {
  798. /* TODO: prevent MLME/driver from trying to associate? */
  799. }
  800. }
  801. #endif /* CONFIG_IEEE80211R */
  802. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  803. union wpa_event_data *data)
  804. {
  805. struct wpa_supplicant *wpa_s = ctx;
  806. switch (event) {
  807. case EVENT_ASSOC:
  808. wpa_supplicant_event_assoc(wpa_s, data);
  809. break;
  810. case EVENT_DISASSOC:
  811. wpa_supplicant_event_disassoc(wpa_s);
  812. break;
  813. case EVENT_MICHAEL_MIC_FAILURE:
  814. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  815. break;
  816. #ifndef CONFIG_NO_SCAN_PROCESSING
  817. case EVENT_SCAN_RESULTS:
  818. wpa_supplicant_event_scan_results(wpa_s);
  819. break;
  820. #endif /* CONFIG_NO_SCAN_PROCESSING */
  821. case EVENT_ASSOCINFO:
  822. wpa_supplicant_event_associnfo(wpa_s, data);
  823. break;
  824. case EVENT_INTERFACE_STATUS:
  825. wpa_supplicant_event_interface_status(wpa_s, data);
  826. break;
  827. case EVENT_PMKID_CANDIDATE:
  828. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  829. break;
  830. #ifdef CONFIG_PEERKEY
  831. case EVENT_STKSTART:
  832. wpa_supplicant_event_stkstart(wpa_s, data);
  833. break;
  834. #endif /* CONFIG_PEERKEY */
  835. #ifdef CONFIG_IEEE80211R
  836. case EVENT_FT_RESPONSE:
  837. wpa_supplicant_event_ft_response(wpa_s, data);
  838. break;
  839. #endif /* CONFIG_IEEE80211R */
  840. default:
  841. wpa_printf(MSG_INFO, "Unknown event %d", event);
  842. break;
  843. }
  844. }