events.c 41 KB

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