events.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2010, 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. union wpa_event_data *data)
  639. {
  640. struct wpa_scan_res *selected;
  641. struct wpa_ssid *ssid = NULL;
  642. wpa_supplicant_notify_scanning(wpa_s, 0);
  643. if (wpa_supplicant_get_scan_results(wpa_s, data ? &data->scan_info :
  644. NULL, 1) < 0) {
  645. if (wpa_s->conf->ap_scan == 2)
  646. return;
  647. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  648. "scanning again");
  649. wpa_supplicant_req_new_scan(wpa_s, 1);
  650. return;
  651. }
  652. /*
  653. * Don't post the results if this was the initial cached
  654. * and there were no results.
  655. */
  656. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  657. wpa_s->scan_res->num == 0) {
  658. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  659. "empty - not posting");
  660. } else {
  661. wpa_printf(MSG_DEBUG, "New scan results available");
  662. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  663. wpas_notify_scan_results(wpa_s);
  664. }
  665. wpas_notify_scan_done(wpa_s, 1);
  666. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
  667. return;
  668. if (wpa_s->disconnected) {
  669. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  670. return;
  671. }
  672. if (bgscan_notify_scan(wpa_s) == 1)
  673. return;
  674. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  675. selected = wpa_supplicant_pick_network(wpa_s, &ssid);
  676. if (selected) {
  677. wpa_supplicant_connect(wpa_s, selected, ssid);
  678. } else {
  679. wpa_printf(MSG_DEBUG, "No suitable network found");
  680. ssid = wpa_supplicant_pick_new_network(wpa_s);
  681. if (ssid) {
  682. wpa_printf(MSG_DEBUG, "Setup a new network");
  683. wpa_supplicant_associate(wpa_s, NULL, ssid);
  684. } else
  685. wpa_supplicant_req_new_scan(wpa_s, 5);
  686. }
  687. }
  688. #endif /* CONFIG_NO_SCAN_PROCESSING */
  689. #ifdef CONFIG_IEEE80211R
  690. static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
  691. const u8 *ftie, const u8 *mdie)
  692. {
  693. const u8 *mobility_domain = NULL;
  694. const u8 *r0kh_id = NULL;
  695. size_t r0kh_id_len = 0;
  696. const u8 *r1kh_id = NULL;
  697. struct rsn_ftie *hdr;
  698. const u8 *pos, *end;
  699. if (mdie == NULL || ftie == NULL)
  700. return;
  701. if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
  702. mobility_domain = mdie + 2;
  703. #ifdef CONFIG_SME
  704. wpa_s->sme.ft_used = 1;
  705. os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
  706. #endif /* CONFIG_SME */
  707. }
  708. if (ftie[1] >= sizeof(struct rsn_ftie)) {
  709. end = ftie + 2 + ftie[1];
  710. hdr = (struct rsn_ftie *) (ftie + 2);
  711. pos = (const u8 *) (hdr + 1);
  712. while (pos + 1 < end) {
  713. if (pos + 2 + pos[1] > end)
  714. break;
  715. if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
  716. pos[1] == FT_R1KH_ID_LEN)
  717. r1kh_id = pos + 2;
  718. else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
  719. pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
  720. r0kh_id = pos + 2;
  721. r0kh_id_len = pos[1];
  722. }
  723. pos += 2 + pos[1];
  724. }
  725. }
  726. wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
  727. r0kh_id_len, r1kh_id);
  728. }
  729. #endif /* CONFIG_IEEE80211R */
  730. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  731. union wpa_event_data *data)
  732. {
  733. int l, len, found = 0, wpa_found, rsn_found;
  734. const u8 *p;
  735. #ifdef CONFIG_IEEE80211R
  736. const u8 *mdie = NULL, *ftie = NULL;
  737. #endif /* CONFIG_IEEE80211R */
  738. wpa_printf(MSG_DEBUG, "Association info event");
  739. if (data->assoc_info.req_ies)
  740. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  741. data->assoc_info.req_ies_len);
  742. if (data->assoc_info.resp_ies)
  743. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  744. data->assoc_info.resp_ies_len);
  745. if (data->assoc_info.beacon_ies)
  746. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  747. data->assoc_info.beacon_ies,
  748. data->assoc_info.beacon_ies_len);
  749. if (data->assoc_info.freq)
  750. wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
  751. p = data->assoc_info.req_ies;
  752. l = data->assoc_info.req_ies_len;
  753. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  754. while (p && l >= 2) {
  755. len = p[1] + 2;
  756. if (len > l) {
  757. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  758. p, l);
  759. break;
  760. }
  761. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  762. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  763. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  764. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  765. break;
  766. found = 1;
  767. wpa_find_assoc_pmkid(wpa_s);
  768. break;
  769. }
  770. l -= len;
  771. p += len;
  772. }
  773. if (!found && data->assoc_info.req_ies)
  774. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  775. #ifdef CONFIG_IEEE80211R
  776. p = data->assoc_info.resp_ies;
  777. l = data->assoc_info.resp_ies_len;
  778. /* Go through the IEs and make a copy of the FT/MD IE, if present. */
  779. while (p && l >= 2) {
  780. len = p[1] + 2;
  781. if (len > l) {
  782. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  783. p, l);
  784. break;
  785. }
  786. if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
  787. ftie = p;
  788. else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
  789. mdie = p;
  790. l -= len;
  791. p += len;
  792. }
  793. wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
  794. #endif /* CONFIG_IEEE80211R */
  795. /* WPA/RSN IE from Beacon/ProbeResp */
  796. p = data->assoc_info.beacon_ies;
  797. l = data->assoc_info.beacon_ies_len;
  798. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  799. */
  800. wpa_found = rsn_found = 0;
  801. while (p && l >= 2) {
  802. len = p[1] + 2;
  803. if (len > l) {
  804. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  805. p, l);
  806. break;
  807. }
  808. if (!wpa_found &&
  809. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  810. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  811. wpa_found = 1;
  812. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  813. }
  814. if (!rsn_found &&
  815. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  816. rsn_found = 1;
  817. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  818. }
  819. l -= len;
  820. p += len;
  821. }
  822. if (!wpa_found && data->assoc_info.beacon_ies)
  823. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  824. if (!rsn_found && data->assoc_info.beacon_ies)
  825. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  826. if (wpa_found || rsn_found)
  827. wpa_s->ap_ies_from_associnfo = 1;
  828. wpa_s->assoc_freq = data->assoc_info.freq;
  829. }
  830. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  831. union wpa_event_data *data)
  832. {
  833. u8 bssid[ETH_ALEN];
  834. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  835. int bssid_changed;
  836. struct wpa_driver_capa capa;
  837. if (data)
  838. wpa_supplicant_event_associnfo(wpa_s, data);
  839. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  840. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  841. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  842. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  843. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  844. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  845. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  846. MACSTR, MAC2STR(bssid));
  847. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  848. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  849. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  850. if (bssid_changed)
  851. wpas_notify_bssid_changed(wpa_s);
  852. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  853. wpa_clear_keys(wpa_s, bssid);
  854. }
  855. if (wpa_supplicant_select_config(wpa_s) < 0) {
  856. wpa_supplicant_disassociate(
  857. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  858. return;
  859. }
  860. }
  861. #ifdef CONFIG_SME
  862. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  863. wpa_s->sme.prev_bssid_set = 1;
  864. #endif /* CONFIG_SME */
  865. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  866. if (wpa_s->current_ssid) {
  867. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  868. * initialized before association, but for other modes,
  869. * initialize PC/SC here, if the current configuration needs
  870. * smartcard or SIM/USIM. */
  871. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  872. }
  873. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  874. l2_packet_notify_auth_start(wpa_s->l2);
  875. /*
  876. * Set portEnabled first to FALSE in order to get EAP state machine out
  877. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  878. * state machine may transit to AUTHENTICATING state based on obsolete
  879. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  880. * AUTHENTICATED without ever giving chance to EAP state machine to
  881. * reset the state.
  882. */
  883. if (!ft_completed) {
  884. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  885. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  886. }
  887. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  888. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  889. /* 802.1X::portControl = Auto */
  890. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  891. wpa_s->eapol_received = 0;
  892. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  893. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  894. (wpa_s->current_ssid &&
  895. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  896. wpa_supplicant_cancel_auth_timeout(wpa_s);
  897. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  898. } else if (!ft_completed) {
  899. /* Timeout for receiving the first EAPOL packet */
  900. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  901. }
  902. wpa_supplicant_cancel_scan(wpa_s);
  903. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  904. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  905. /*
  906. * We are done; the driver will take care of RSN 4-way
  907. * handshake.
  908. */
  909. wpa_supplicant_cancel_auth_timeout(wpa_s);
  910. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  911. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  912. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  913. }
  914. if (wpa_s->pending_eapol_rx) {
  915. struct os_time now, age;
  916. os_get_time(&now);
  917. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  918. if (age.sec == 0 && age.usec < 100000 &&
  919. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  920. 0) {
  921. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  922. "that was received just before association "
  923. "notification");
  924. wpa_supplicant_rx_eapol(
  925. wpa_s, wpa_s->pending_eapol_rx_src,
  926. wpabuf_head(wpa_s->pending_eapol_rx),
  927. wpabuf_len(wpa_s->pending_eapol_rx));
  928. }
  929. wpabuf_free(wpa_s->pending_eapol_rx);
  930. wpa_s->pending_eapol_rx = NULL;
  931. }
  932. #ifdef CONFIG_BGSCAN
  933. if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
  934. bgscan_deinit(wpa_s);
  935. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  936. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  937. wpa_printf(MSG_DEBUG, "Failed to initialize "
  938. "bgscan");
  939. /*
  940. * Live without bgscan; it is only used as a
  941. * roaming optimization, so the initial
  942. * connection is not affected.
  943. */
  944. } else
  945. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  946. } else
  947. wpa_s->bgscan_ssid = NULL;
  948. }
  949. #endif /* CONFIG_BGSCAN */
  950. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  951. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  952. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  953. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  954. /* Set static WEP keys again */
  955. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  956. }
  957. }
  958. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  959. {
  960. const u8 *bssid;
  961. #ifdef CONFIG_SME
  962. int authenticating;
  963. u8 prev_pending_bssid[ETH_ALEN];
  964. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  965. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  966. #endif /* CONFIG_SME */
  967. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  968. /*
  969. * At least Host AP driver and a Prism3 card seemed to be
  970. * generating streams of disconnected events when configuring
  971. * IBSS for WPA-None. Ignore them for now.
  972. */
  973. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  974. "IBSS/WPA-None mode");
  975. return;
  976. }
  977. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  978. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  979. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  980. "pre-shared key may be incorrect");
  981. }
  982. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  983. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  984. bssid = wpa_s->bssid;
  985. if (is_zero_ether_addr(bssid))
  986. bssid = wpa_s->pending_bssid;
  987. wpa_blacklist_add(wpa_s, bssid);
  988. wpa_sm_notify_disassoc(wpa_s->wpa);
  989. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  990. "remove keys");
  991. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  992. wpa_s->keys_cleared = 0;
  993. wpa_clear_keys(wpa_s, wpa_s->bssid);
  994. }
  995. wpa_supplicant_mark_disassoc(wpa_s);
  996. bgscan_deinit(wpa_s);
  997. #ifdef CONFIG_SME
  998. if (authenticating &&
  999. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1000. /*
  1001. * mac80211-workaround to force deauth on failed auth cmd,
  1002. * requires us to remain in authenticating state to allow the
  1003. * second authentication attempt to be continued properly.
  1004. */
  1005. wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
  1006. "proceed after disconnection event");
  1007. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1008. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1009. }
  1010. #endif /* CONFIG_SME */
  1011. }
  1012. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1013. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  1014. void *sock_ctx)
  1015. {
  1016. struct wpa_supplicant *wpa_s = eloop_ctx;
  1017. if (!wpa_s->pending_mic_error_report)
  1018. return;
  1019. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  1020. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1021. wpa_s->pending_mic_error_report = 0;
  1022. }
  1023. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1024. static void
  1025. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1026. union wpa_event_data *data)
  1027. {
  1028. int pairwise;
  1029. struct os_time t;
  1030. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1031. pairwise = (data && data->michael_mic_failure.unicast);
  1032. os_get_time(&t);
  1033. if ((wpa_s->last_michael_mic_error &&
  1034. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1035. wpa_s->pending_mic_error_report) {
  1036. if (wpa_s->pending_mic_error_report) {
  1037. /*
  1038. * Send the pending MIC error report immediately since
  1039. * we are going to start countermeasures and AP better
  1040. * do the same.
  1041. */
  1042. wpa_sm_key_request(wpa_s->wpa, 1,
  1043. wpa_s->pending_mic_error_pairwise);
  1044. }
  1045. /* Send the new MIC error report immediately since we are going
  1046. * to start countermeasures and AP better do the same.
  1047. */
  1048. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1049. /* initialize countermeasures */
  1050. wpa_s->countermeasures = 1;
  1051. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1052. /*
  1053. * Need to wait for completion of request frame. We do not get
  1054. * any callback for the message completion, so just wait a
  1055. * short while and hope for the best. */
  1056. os_sleep(0, 10000);
  1057. wpa_drv_set_countermeasures(wpa_s, 1);
  1058. wpa_supplicant_deauthenticate(wpa_s,
  1059. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1060. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1061. wpa_s, NULL);
  1062. eloop_register_timeout(60, 0,
  1063. wpa_supplicant_stop_countermeasures,
  1064. wpa_s, NULL);
  1065. /* TODO: mark the AP rejected for 60 second. STA is
  1066. * allowed to associate with another AP.. */
  1067. } else {
  1068. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1069. if (wpa_s->mic_errors_seen) {
  1070. /*
  1071. * Reduce the effectiveness of Michael MIC error
  1072. * reports as a means for attacking against TKIP if
  1073. * more than one MIC failure is noticed with the same
  1074. * PTK. We delay the transmission of the reports by a
  1075. * random time between 0 and 60 seconds in order to
  1076. * force the attacker wait 60 seconds before getting
  1077. * the information on whether a frame resulted in a MIC
  1078. * failure.
  1079. */
  1080. u8 rval[4];
  1081. int sec;
  1082. if (os_get_random(rval, sizeof(rval)) < 0)
  1083. sec = os_random() % 60;
  1084. else
  1085. sec = WPA_GET_BE32(rval) % 60;
  1086. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  1087. "seconds", sec);
  1088. wpa_s->pending_mic_error_report = 1;
  1089. wpa_s->pending_mic_error_pairwise = pairwise;
  1090. eloop_cancel_timeout(
  1091. wpa_supplicant_delayed_mic_error_report,
  1092. wpa_s, NULL);
  1093. eloop_register_timeout(
  1094. sec, os_random() % 1000000,
  1095. wpa_supplicant_delayed_mic_error_report,
  1096. wpa_s, NULL);
  1097. } else {
  1098. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1099. }
  1100. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1101. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1102. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1103. }
  1104. wpa_s->last_michael_mic_error = t.sec;
  1105. wpa_s->mic_errors_seen++;
  1106. }
  1107. #ifdef CONFIG_TERMINATE_ONLASTIF
  1108. static int any_interfaces(struct wpa_supplicant *head)
  1109. {
  1110. struct wpa_supplicant *wpa_s;
  1111. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1112. if (!wpa_s->interface_removed)
  1113. return 1;
  1114. return 0;
  1115. }
  1116. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1117. static void
  1118. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1119. union wpa_event_data *data)
  1120. {
  1121. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1122. return;
  1123. switch (data->interface_status.ievent) {
  1124. case EVENT_INTERFACE_ADDED:
  1125. if (!wpa_s->interface_removed)
  1126. break;
  1127. wpa_s->interface_removed = 0;
  1128. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  1129. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1130. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  1131. "after interface was added.");
  1132. }
  1133. break;
  1134. case EVENT_INTERFACE_REMOVED:
  1135. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1136. wpa_s->interface_removed = 1;
  1137. wpa_supplicant_mark_disassoc(wpa_s);
  1138. l2_packet_deinit(wpa_s->l2);
  1139. wpa_s->l2 = NULL;
  1140. #ifdef CONFIG_TERMINATE_ONLASTIF
  1141. /* check if last interface */
  1142. if (!any_interfaces(wpa_s->global->ifaces))
  1143. eloop_terminate();
  1144. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1145. break;
  1146. }
  1147. }
  1148. #ifdef CONFIG_PEERKEY
  1149. static void
  1150. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1151. union wpa_event_data *data)
  1152. {
  1153. if (data == NULL)
  1154. return;
  1155. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1156. }
  1157. #endif /* CONFIG_PEERKEY */
  1158. #ifdef CONFIG_IEEE80211R
  1159. static void
  1160. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1161. union wpa_event_data *data)
  1162. {
  1163. if (data == NULL)
  1164. return;
  1165. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1166. data->ft_ies.ies_len,
  1167. data->ft_ies.ft_action,
  1168. data->ft_ies.target_ap,
  1169. data->ft_ies.ric_ies,
  1170. data->ft_ies.ric_ies_len) < 0) {
  1171. /* TODO: prevent MLME/driver from trying to associate? */
  1172. }
  1173. }
  1174. #endif /* CONFIG_IEEE80211R */
  1175. #ifdef CONFIG_IBSS_RSN
  1176. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1177. union wpa_event_data *data)
  1178. {
  1179. if (data == NULL)
  1180. return;
  1181. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1182. }
  1183. #endif /* CONFIG_IBSS_RSN */
  1184. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  1185. union wpa_event_data *data)
  1186. {
  1187. struct wpa_supplicant *wpa_s = ctx;
  1188. switch (event) {
  1189. case EVENT_AUTH:
  1190. sme_event_auth(wpa_s, data);
  1191. break;
  1192. case EVENT_ASSOC:
  1193. wpa_supplicant_event_assoc(wpa_s, data);
  1194. break;
  1195. case EVENT_DISASSOC:
  1196. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1197. sme_event_disassoc(wpa_s, data);
  1198. /* fall through */
  1199. case EVENT_DEAUTH:
  1200. wpa_supplicant_event_disassoc(wpa_s);
  1201. break;
  1202. case EVENT_MICHAEL_MIC_FAILURE:
  1203. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1204. break;
  1205. #ifndef CONFIG_NO_SCAN_PROCESSING
  1206. case EVENT_SCAN_RESULTS:
  1207. wpa_supplicant_event_scan_results(wpa_s, data);
  1208. break;
  1209. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1210. case EVENT_ASSOCINFO:
  1211. wpa_supplicant_event_associnfo(wpa_s, data);
  1212. break;
  1213. case EVENT_INTERFACE_STATUS:
  1214. wpa_supplicant_event_interface_status(wpa_s, data);
  1215. break;
  1216. case EVENT_PMKID_CANDIDATE:
  1217. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1218. break;
  1219. #ifdef CONFIG_PEERKEY
  1220. case EVENT_STKSTART:
  1221. wpa_supplicant_event_stkstart(wpa_s, data);
  1222. break;
  1223. #endif /* CONFIG_PEERKEY */
  1224. #ifdef CONFIG_IEEE80211R
  1225. case EVENT_FT_RESPONSE:
  1226. wpa_supplicant_event_ft_response(wpa_s, data);
  1227. break;
  1228. #endif /* CONFIG_IEEE80211R */
  1229. #ifdef CONFIG_IBSS_RSN
  1230. case EVENT_IBSS_RSN_START:
  1231. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1232. break;
  1233. #endif /* CONFIG_IBSS_RSN */
  1234. case EVENT_ASSOC_REJECT:
  1235. sme_event_assoc_reject(wpa_s, data);
  1236. break;
  1237. case EVENT_AUTH_TIMED_OUT:
  1238. sme_event_auth_timed_out(wpa_s, data);
  1239. break;
  1240. case EVENT_ASSOC_TIMED_OUT:
  1241. sme_event_assoc_timed_out(wpa_s, data);
  1242. break;
  1243. #ifdef CONFIG_AP
  1244. case EVENT_TX_STATUS:
  1245. if (wpa_s->ap_iface == NULL)
  1246. break;
  1247. switch (data->tx_status.type) {
  1248. case WLAN_FC_TYPE_MGMT:
  1249. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1250. data->tx_status.data_len,
  1251. data->tx_status.stype,
  1252. data->tx_status.ack);
  1253. break;
  1254. case WLAN_FC_TYPE_DATA:
  1255. ap_tx_status(wpa_s, data->tx_status.dst,
  1256. data->tx_status.data,
  1257. data->tx_status.data_len,
  1258. data->tx_status.ack);
  1259. break;
  1260. }
  1261. break;
  1262. case EVENT_RX_FROM_UNKNOWN:
  1263. if (wpa_s->ap_iface == NULL)
  1264. break;
  1265. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.hdr,
  1266. data->rx_from_unknown.len);
  1267. break;
  1268. case EVENT_RX_MGMT:
  1269. if (wpa_s->ap_iface == NULL)
  1270. break;
  1271. ap_mgmt_rx(wpa_s, data->rx_mgmt.frame,
  1272. data->rx_mgmt.frame_len, data->rx_mgmt.fi);
  1273. break;
  1274. #endif /* CONFIG_AP */
  1275. default:
  1276. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1277. break;
  1278. }
  1279. }