events.c 39 KB

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