events.c 37 KB

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