events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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_wpa(struct wpa_supplicant *wpa_s,
  309. struct wpa_ssid *group,
  310. struct wpa_ssid **selected_ssid)
  311. {
  312. struct wpa_ssid *ssid;
  313. struct wpa_scan_res *bss;
  314. size_t i;
  315. struct wpa_blacklist *e;
  316. const u8 *ie;
  317. wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
  318. for (i = 0; i < wpa_s->scan_res->num; i++) {
  319. const u8 *ssid_;
  320. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  321. bss = wpa_s->scan_res->res[i];
  322. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  323. ssid_ = ie ? ie + 2 : (u8 *) "";
  324. ssid_len = ie ? ie[1] : 0;
  325. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  326. wpa_ie_len = ie ? ie[1] : 0;
  327. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  328. rsn_ie_len = ie ? ie[1] : 0;
  329. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  330. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  331. (int) i, MAC2STR(bss->bssid),
  332. wpa_ssid_txt(ssid_, ssid_len),
  333. wpa_ie_len, rsn_ie_len, bss->caps);
  334. e = wpa_blacklist_get(wpa_s, bss->bssid);
  335. if (e && e->count > 1) {
  336. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  337. continue;
  338. }
  339. if (wpa_ie_len == 0 && rsn_ie_len == 0) {
  340. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
  341. continue;
  342. }
  343. for (ssid = group; ssid; ssid = ssid->pnext) {
  344. if (ssid->disabled) {
  345. wpa_printf(MSG_DEBUG, " skip - disabled");
  346. continue;
  347. }
  348. if (ssid_len != ssid->ssid_len ||
  349. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0) {
  350. wpa_printf(MSG_DEBUG, " skip - "
  351. "SSID mismatch");
  352. continue;
  353. }
  354. if (ssid->bssid_set &&
  355. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  356. {
  357. wpa_printf(MSG_DEBUG, " skip - "
  358. "BSSID mismatch");
  359. continue;
  360. }
  361. if (!wpa_supplicant_ssid_bss_match(ssid, bss))
  362. continue;
  363. wpa_printf(MSG_DEBUG, " selected WPA AP "
  364. MACSTR " ssid='%s'",
  365. MAC2STR(bss->bssid),
  366. wpa_ssid_txt(ssid_, ssid_len));
  367. *selected_ssid = ssid;
  368. return bss;
  369. }
  370. }
  371. return NULL;
  372. }
  373. static struct wpa_scan_res *
  374. wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
  375. struct wpa_ssid *group,
  376. struct wpa_ssid **selected_ssid)
  377. {
  378. struct wpa_ssid *ssid;
  379. struct wpa_scan_res *bss;
  380. size_t i;
  381. struct wpa_blacklist *e;
  382. const u8 *ie;
  383. wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
  384. for (i = 0; i < wpa_s->scan_res->num; i++) {
  385. const u8 *ssid_;
  386. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  387. bss = wpa_s->scan_res->res[i];
  388. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  389. ssid_ = ie ? ie + 2 : (u8 *) "";
  390. ssid_len = ie ? ie[1] : 0;
  391. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  392. wpa_ie_len = ie ? ie[1] : 0;
  393. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  394. rsn_ie_len = ie ? ie[1] : 0;
  395. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  396. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  397. (int) i, MAC2STR(bss->bssid),
  398. wpa_ssid_txt(ssid_, ssid_len),
  399. wpa_ie_len, rsn_ie_len, bss->caps);
  400. e = wpa_blacklist_get(wpa_s, bss->bssid);
  401. if (e && e->count > 1) {
  402. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  403. continue;
  404. }
  405. for (ssid = group; ssid; ssid = ssid->pnext) {
  406. if (ssid->disabled) {
  407. wpa_printf(MSG_DEBUG, " skip - disabled");
  408. continue;
  409. }
  410. if (ssid->ssid_len != 0 &&
  411. (ssid_len != ssid->ssid_len ||
  412. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  413. wpa_printf(MSG_DEBUG, " skip - "
  414. "SSID mismatch");
  415. continue;
  416. }
  417. if (ssid->bssid_set &&
  418. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  419. {
  420. wpa_printf(MSG_DEBUG, " skip - "
  421. "BSSID mismatch");
  422. continue;
  423. }
  424. if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  425. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
  426. {
  427. wpa_printf(MSG_DEBUG, " skip - "
  428. "non-WPA network not allowed");
  429. continue;
  430. }
  431. if ((ssid->key_mgmt &
  432. (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  433. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
  434. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  435. WPA_KEY_MGMT_PSK_SHA256)) &&
  436. (wpa_ie_len != 0 || rsn_ie_len != 0)) {
  437. wpa_printf(MSG_DEBUG, " skip - "
  438. "WPA network");
  439. continue;
  440. }
  441. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  442. wpa_printf(MSG_DEBUG, " skip - "
  443. "privacy mismatch");
  444. continue;
  445. }
  446. if (bss->caps & IEEE80211_CAP_IBSS) {
  447. wpa_printf(MSG_DEBUG, " skip - "
  448. "IBSS (adhoc) network");
  449. continue;
  450. }
  451. wpa_printf(MSG_DEBUG, " selected non-WPA AP "
  452. MACSTR " ssid='%s'",
  453. MAC2STR(bss->bssid),
  454. wpa_ssid_txt(ssid_, ssid_len));
  455. *selected_ssid = ssid;
  456. return bss;
  457. }
  458. }
  459. return NULL;
  460. }
  461. static struct wpa_scan_res *
  462. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
  463. struct wpa_ssid **selected_ssid)
  464. {
  465. struct wpa_scan_res *selected;
  466. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  467. group->priority);
  468. /* First, try to find WPA-enabled AP */
  469. selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
  470. if (selected)
  471. return selected;
  472. /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
  473. * allows this. */
  474. return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
  475. }
  476. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
  477. {
  478. int prio, timeout;
  479. struct wpa_scan_res *selected = NULL;
  480. struct wpa_ssid *ssid = NULL;
  481. if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
  482. if (wpa_s->conf->ap_scan == 2)
  483. return;
  484. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  485. "scanning again");
  486. timeout = 1;
  487. goto req_scan;
  488. }
  489. /*
  490. * Don't post the results if this was the initial cached
  491. * and there were no results.
  492. */
  493. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  494. wpa_s->scan_res->num == 0) {
  495. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  496. "empty - not posting");
  497. } else {
  498. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  499. wpa_supplicant_dbus_notify_scan_results(wpa_s);
  500. }
  501. if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
  502. return;
  503. while (selected == NULL) {
  504. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  505. selected = wpa_supplicant_select_bss(
  506. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  507. if (selected)
  508. break;
  509. }
  510. if (selected == NULL && wpa_s->blacklist) {
  511. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  512. "and try again");
  513. wpa_blacklist_clear(wpa_s);
  514. } else if (selected == NULL) {
  515. break;
  516. }
  517. }
  518. if (selected) {
  519. /* Do not trigger new association unless the BSSID has changed
  520. * or if reassociation is requested. If we are in process of
  521. * associating with the selected BSSID, do not trigger new
  522. * attempt. */
  523. if (wpa_s->reassociate ||
  524. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  525. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  526. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  527. ETH_ALEN) != 0))) {
  528. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  529. wpa_supplicant_req_scan(wpa_s, 10, 0);
  530. return;
  531. }
  532. wpa_supplicant_associate(wpa_s, selected, ssid);
  533. } else {
  534. wpa_printf(MSG_DEBUG, "Already associated with the "
  535. "selected AP.");
  536. }
  537. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  538. } else {
  539. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  540. timeout = 5;
  541. goto req_scan;
  542. }
  543. return;
  544. req_scan:
  545. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  546. /*
  547. * Quick recovery if the initial scan results were not
  548. * complete when fetched before the first scan request.
  549. */
  550. wpa_s->scan_res_tried++;
  551. timeout = 0;
  552. }
  553. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  554. }
  555. #endif /* CONFIG_NO_SCAN_PROCESSING */
  556. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  557. union wpa_event_data *data)
  558. {
  559. int l, len, found = 0, wpa_found, rsn_found;
  560. u8 *p;
  561. wpa_printf(MSG_DEBUG, "Association info event");
  562. if (data->assoc_info.req_ies)
  563. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  564. data->assoc_info.req_ies_len);
  565. if (data->assoc_info.resp_ies)
  566. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  567. data->assoc_info.resp_ies_len);
  568. if (data->assoc_info.beacon_ies)
  569. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  570. data->assoc_info.beacon_ies,
  571. data->assoc_info.beacon_ies_len);
  572. p = data->assoc_info.req_ies;
  573. l = data->assoc_info.req_ies_len;
  574. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  575. while (p && l >= 2) {
  576. len = p[1] + 2;
  577. if (len > l) {
  578. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  579. p, l);
  580. break;
  581. }
  582. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  583. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  584. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  585. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  586. break;
  587. found = 1;
  588. wpa_find_assoc_pmkid(wpa_s);
  589. break;
  590. }
  591. l -= len;
  592. p += len;
  593. }
  594. if (!found && data->assoc_info.req_ies)
  595. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  596. /* WPA/RSN IE from Beacon/ProbeResp */
  597. p = data->assoc_info.beacon_ies;
  598. l = data->assoc_info.beacon_ies_len;
  599. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  600. */
  601. wpa_found = rsn_found = 0;
  602. while (p && l >= 2) {
  603. len = p[1] + 2;
  604. if (len > l) {
  605. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  606. p, l);
  607. break;
  608. }
  609. if (!wpa_found &&
  610. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  611. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  612. wpa_found = 1;
  613. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  614. }
  615. if (!rsn_found &&
  616. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  617. rsn_found = 1;
  618. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  619. }
  620. l -= len;
  621. p += len;
  622. }
  623. if (!wpa_found && data->assoc_info.beacon_ies)
  624. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  625. if (!rsn_found && data->assoc_info.beacon_ies)
  626. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  627. if (wpa_found || rsn_found)
  628. wpa_s->ap_ies_from_associnfo = 1;
  629. }
  630. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  631. union wpa_event_data *data)
  632. {
  633. u8 bssid[ETH_ALEN];
  634. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  635. if (data)
  636. wpa_supplicant_event_associnfo(wpa_s, data);
  637. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  638. if (wpa_s->use_client_mlme)
  639. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  640. if (wpa_s->use_client_mlme ||
  641. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  642. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  643. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  644. MACSTR, MAC2STR(bssid));
  645. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  646. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  647. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  648. wpa_clear_keys(wpa_s, bssid);
  649. }
  650. if (wpa_supplicant_select_config(wpa_s) < 0) {
  651. wpa_supplicant_disassociate(
  652. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  653. return;
  654. }
  655. }
  656. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  657. if (wpa_s->current_ssid) {
  658. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  659. * initialized before association, but for other modes,
  660. * initialize PC/SC here, if the current configuration needs
  661. * smartcard or SIM/USIM. */
  662. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  663. }
  664. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  665. l2_packet_notify_auth_start(wpa_s->l2);
  666. /*
  667. * Set portEnabled first to FALSE in order to get EAP state machine out
  668. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  669. * state machine may transit to AUTHENTICATING state based on obsolete
  670. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  671. * AUTHENTICATED without ever giving chance to EAP state machine to
  672. * reset the state.
  673. */
  674. if (!ft_completed) {
  675. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  676. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  677. }
  678. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  679. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  680. /* 802.1X::portControl = Auto */
  681. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  682. wpa_s->eapol_received = 0;
  683. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  684. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  685. wpa_supplicant_cancel_auth_timeout(wpa_s);
  686. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  687. } else if (!ft_completed) {
  688. /* Timeout for receiving the first EAPOL packet */
  689. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  690. }
  691. wpa_supplicant_cancel_scan(wpa_s);
  692. if (wpa_s->driver_4way_handshake &&
  693. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  694. /*
  695. * We are done; the driver will take care of RSN 4-way
  696. * handshake.
  697. */
  698. wpa_supplicant_cancel_auth_timeout(wpa_s);
  699. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  700. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  701. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  702. }
  703. }
  704. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  705. {
  706. const u8 *bssid;
  707. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  708. /*
  709. * At least Host AP driver and a Prism3 card seemed to be
  710. * generating streams of disconnected events when configuring
  711. * IBSS for WPA-None. Ignore them for now.
  712. */
  713. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  714. "IBSS/WPA-None mode");
  715. return;
  716. }
  717. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  718. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  719. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  720. "pre-shared key may be incorrect");
  721. }
  722. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  723. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  724. bssid = wpa_s->bssid;
  725. if (is_zero_ether_addr(bssid))
  726. bssid = wpa_s->pending_bssid;
  727. wpa_blacklist_add(wpa_s, bssid);
  728. wpa_sm_notify_disassoc(wpa_s->wpa);
  729. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  730. "remove keys");
  731. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  732. wpa_s->keys_cleared = 0;
  733. wpa_clear_keys(wpa_s, wpa_s->bssid);
  734. }
  735. wpa_supplicant_mark_disassoc(wpa_s);
  736. }
  737. static void
  738. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  739. union wpa_event_data *data)
  740. {
  741. int pairwise;
  742. struct os_time t;
  743. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  744. pairwise = (data && data->michael_mic_failure.unicast);
  745. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  746. os_get_time(&t);
  747. if (wpa_s->last_michael_mic_error &&
  748. t.sec - wpa_s->last_michael_mic_error <= 60) {
  749. /* initialize countermeasures */
  750. wpa_s->countermeasures = 1;
  751. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  752. /*
  753. * Need to wait for completion of request frame. We do not get
  754. * any callback for the message completion, so just wait a
  755. * short while and hope for the best. */
  756. os_sleep(0, 10000);
  757. wpa_drv_set_countermeasures(wpa_s, 1);
  758. wpa_supplicant_deauthenticate(wpa_s,
  759. WLAN_REASON_MICHAEL_MIC_FAILURE);
  760. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  761. wpa_s, NULL);
  762. eloop_register_timeout(60, 0,
  763. wpa_supplicant_stop_countermeasures,
  764. wpa_s, NULL);
  765. /* TODO: mark the AP rejected for 60 second. STA is
  766. * allowed to associate with another AP.. */
  767. }
  768. wpa_s->last_michael_mic_error = t.sec;
  769. }
  770. static void
  771. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  772. union wpa_event_data *data)
  773. {
  774. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  775. return;
  776. switch (data->interface_status.ievent) {
  777. case EVENT_INTERFACE_ADDED:
  778. if (!wpa_s->interface_removed)
  779. break;
  780. wpa_s->interface_removed = 0;
  781. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  782. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  783. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  784. "after interface was added.");
  785. }
  786. break;
  787. case EVENT_INTERFACE_REMOVED:
  788. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  789. wpa_s->interface_removed = 1;
  790. wpa_supplicant_mark_disassoc(wpa_s);
  791. l2_packet_deinit(wpa_s->l2);
  792. wpa_s->l2 = NULL;
  793. break;
  794. }
  795. }
  796. #ifdef CONFIG_PEERKEY
  797. static void
  798. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  799. union wpa_event_data *data)
  800. {
  801. if (data == NULL)
  802. return;
  803. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  804. }
  805. #endif /* CONFIG_PEERKEY */
  806. #ifdef CONFIG_IEEE80211R
  807. static void
  808. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  809. union wpa_event_data *data)
  810. {
  811. if (data == NULL)
  812. return;
  813. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  814. data->ft_ies.ies_len,
  815. data->ft_ies.ft_action,
  816. data->ft_ies.target_ap) < 0) {
  817. /* TODO: prevent MLME/driver from trying to associate? */
  818. }
  819. }
  820. #endif /* CONFIG_IEEE80211R */
  821. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  822. union wpa_event_data *data)
  823. {
  824. struct wpa_supplicant *wpa_s = ctx;
  825. switch (event) {
  826. case EVENT_ASSOC:
  827. wpa_supplicant_event_assoc(wpa_s, data);
  828. break;
  829. case EVENT_DISASSOC:
  830. wpa_supplicant_event_disassoc(wpa_s);
  831. break;
  832. case EVENT_MICHAEL_MIC_FAILURE:
  833. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  834. break;
  835. #ifndef CONFIG_NO_SCAN_PROCESSING
  836. case EVENT_SCAN_RESULTS:
  837. wpa_supplicant_event_scan_results(wpa_s);
  838. break;
  839. #endif /* CONFIG_NO_SCAN_PROCESSING */
  840. case EVENT_ASSOCINFO:
  841. wpa_supplicant_event_associnfo(wpa_s, data);
  842. break;
  843. case EVENT_INTERFACE_STATUS:
  844. wpa_supplicant_event_interface_status(wpa_s, data);
  845. break;
  846. case EVENT_PMKID_CANDIDATE:
  847. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  848. break;
  849. #ifdef CONFIG_PEERKEY
  850. case EVENT_STKSTART:
  851. wpa_supplicant_event_stkstart(wpa_s, data);
  852. break;
  853. #endif /* CONFIG_PEERKEY */
  854. #ifdef CONFIG_IEEE80211R
  855. case EVENT_FT_RESPONSE:
  856. wpa_supplicant_event_ft_response(wpa_s, data);
  857. break;
  858. #endif /* CONFIG_IEEE80211R */
  859. default:
  860. wpa_printf(MSG_INFO, "Unknown event %d", event);
  861. break;
  862. }
  863. }