events.c 37 KB

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