events.c 40 KB

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