events.c 40 KB

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