events.c 41 KB

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