events.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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 "p2p/p2p.h"
  30. #include "notify.h"
  31. #include "common/ieee802_11_defs.h"
  32. #include "common/ieee802_11_common.h"
  33. #include "crypto/random.h"
  34. #include "blacklist.h"
  35. #include "wpas_glue.h"
  36. #include "wps_supplicant.h"
  37. #include "ibss_rsn.h"
  38. #include "sme.h"
  39. #include "p2p_supplicant.h"
  40. #include "bgscan.h"
  41. #include "ap.h"
  42. #include "bss.h"
  43. #include "mlme.h"
  44. #include "scan.h"
  45. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  46. {
  47. struct wpa_ssid *ssid, *old_ssid;
  48. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  49. return 0;
  50. wpa_printf(MSG_DEBUG, "Select network based on association "
  51. "information");
  52. ssid = wpa_supplicant_get_ssid(wpa_s);
  53. if (ssid == NULL) {
  54. wpa_printf(MSG_INFO, "No network configuration found for the "
  55. "current AP");
  56. return -1;
  57. }
  58. if (ssid->disabled) {
  59. wpa_printf(MSG_DEBUG, "Selected network is disabled");
  60. return -1;
  61. }
  62. wpa_printf(MSG_DEBUG, "Network configuration found for the current "
  63. "AP");
  64. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  65. WPA_KEY_MGMT_WPA_NONE |
  66. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
  67. WPA_KEY_MGMT_PSK_SHA256 |
  68. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  69. u8 wpa_ie[80];
  70. size_t wpa_ie_len = sizeof(wpa_ie);
  71. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  72. wpa_ie, &wpa_ie_len);
  73. } else {
  74. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  75. }
  76. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  77. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  78. old_ssid = wpa_s->current_ssid;
  79. wpa_s->current_ssid = ssid;
  80. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  81. wpa_supplicant_initiate_eapol(wpa_s);
  82. if (old_ssid != wpa_s->current_ssid)
  83. wpas_notify_network_changed(wpa_s);
  84. return 0;
  85. }
  86. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  87. void *sock_ctx)
  88. {
  89. struct wpa_supplicant *wpa_s = eloop_ctx;
  90. if (wpa_s->countermeasures) {
  91. wpa_s->countermeasures = 0;
  92. wpa_drv_set_countermeasures(wpa_s, 0);
  93. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  94. wpa_supplicant_req_scan(wpa_s, 0, 0);
  95. }
  96. }
  97. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  98. {
  99. int bssid_changed;
  100. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  101. return;
  102. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  103. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  104. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  105. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  106. wpa_s->current_bss = NULL;
  107. wpa_s->assoc_freq = 0;
  108. if (bssid_changed)
  109. wpas_notify_bssid_changed(wpa_s);
  110. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  111. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  112. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  113. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  114. wpa_s->ap_ies_from_associnfo = 0;
  115. }
  116. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  117. {
  118. struct wpa_ie_data ie;
  119. int pmksa_set = -1;
  120. size_t i;
  121. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  122. ie.pmkid == NULL)
  123. return;
  124. for (i = 0; i < ie.num_pmkid; i++) {
  125. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  126. ie.pmkid + i * PMKID_LEN,
  127. NULL, NULL, 0);
  128. if (pmksa_set == 0) {
  129. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  130. break;
  131. }
  132. }
  133. wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
  134. "cache", pmksa_set == 0 ? "" : "not ");
  135. }
  136. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  137. union wpa_event_data *data)
  138. {
  139. if (data == NULL) {
  140. wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
  141. return;
  142. }
  143. wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  144. " index=%d preauth=%d",
  145. MAC2STR(data->pmkid_candidate.bssid),
  146. data->pmkid_candidate.index,
  147. data->pmkid_candidate.preauth);
  148. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  149. data->pmkid_candidate.index,
  150. data->pmkid_candidate.preauth);
  151. }
  152. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  153. {
  154. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  155. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  156. return 0;
  157. #ifdef IEEE8021X_EAPOL
  158. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  159. wpa_s->current_ssid &&
  160. !(wpa_s->current_ssid->eapol_flags &
  161. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  162. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  163. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  164. * plaintext or static WEP keys). */
  165. return 0;
  166. }
  167. #endif /* IEEE8021X_EAPOL */
  168. return 1;
  169. }
  170. /**
  171. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  172. * @wpa_s: pointer to wpa_supplicant data
  173. * @ssid: Configuration data for the network
  174. * Returns: 0 on success, -1 on failure
  175. *
  176. * This function is called when starting authentication with a network that is
  177. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  178. */
  179. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  180. struct wpa_ssid *ssid)
  181. {
  182. #ifdef IEEE8021X_EAPOL
  183. int aka = 0, sim = 0, type;
  184. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  185. return 0;
  186. if (ssid->eap.eap_methods == NULL) {
  187. sim = 1;
  188. aka = 1;
  189. } else {
  190. struct eap_method_type *eap = ssid->eap.eap_methods;
  191. while (eap->vendor != EAP_VENDOR_IETF ||
  192. eap->method != EAP_TYPE_NONE) {
  193. if (eap->vendor == EAP_VENDOR_IETF) {
  194. if (eap->method == EAP_TYPE_SIM)
  195. sim = 1;
  196. else if (eap->method == EAP_TYPE_AKA)
  197. aka = 1;
  198. }
  199. eap++;
  200. }
  201. }
  202. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  203. sim = 0;
  204. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  205. aka = 0;
  206. if (!sim && !aka) {
  207. wpa_printf(MSG_DEBUG, "Selected network is configured to use "
  208. "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
  209. return 0;
  210. }
  211. wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
  212. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  213. if (sim && aka)
  214. type = SCARD_TRY_BOTH;
  215. else if (aka)
  216. type = SCARD_USIM_ONLY;
  217. else
  218. type = SCARD_GSM_SIM_ONLY;
  219. wpa_s->scard = scard_init(type);
  220. if (wpa_s->scard == NULL) {
  221. wpa_printf(MSG_WARNING, "Failed to initialize SIM "
  222. "(pcsc-lite)");
  223. return -1;
  224. }
  225. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  226. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  227. #endif /* IEEE8021X_EAPOL */
  228. return 0;
  229. }
  230. #ifndef CONFIG_NO_SCAN_PROCESSING
  231. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  232. struct wpa_ssid *ssid)
  233. {
  234. int i, privacy = 0;
  235. if (ssid->mixed_cell)
  236. return 1;
  237. #ifdef CONFIG_WPS
  238. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  239. return 1;
  240. #endif /* CONFIG_WPS */
  241. for (i = 0; i < NUM_WEP_KEYS; i++) {
  242. if (ssid->wep_key_len[i]) {
  243. privacy = 1;
  244. break;
  245. }
  246. }
  247. #ifdef IEEE8021X_EAPOL
  248. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  249. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  250. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  251. privacy = 1;
  252. #endif /* IEEE8021X_EAPOL */
  253. if (bss->caps & IEEE80211_CAP_PRIVACY)
  254. return privacy;
  255. return !privacy;
  256. }
  257. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  258. struct wpa_ssid *ssid,
  259. struct wpa_scan_res *bss)
  260. {
  261. struct wpa_ie_data ie;
  262. int proto_match = 0;
  263. const u8 *rsn_ie, *wpa_ie;
  264. int ret;
  265. int wep_ok;
  266. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  267. if (ret >= 0)
  268. return ret;
  269. /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
  270. wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  271. (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  272. ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
  273. (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
  274. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  275. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  276. proto_match++;
  277. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  278. wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
  279. break;
  280. }
  281. if (wep_ok &&
  282. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  283. {
  284. wpa_printf(MSG_DEBUG, " selected based on TSN in "
  285. "RSN IE");
  286. return 1;
  287. }
  288. if (!(ie.proto & ssid->proto)) {
  289. wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
  290. "mismatch");
  291. break;
  292. }
  293. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  294. wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
  295. "mismatch");
  296. break;
  297. }
  298. if (!(ie.group_cipher & ssid->group_cipher)) {
  299. wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
  300. "mismatch");
  301. break;
  302. }
  303. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  304. wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
  305. "mismatch");
  306. break;
  307. }
  308. #ifdef CONFIG_IEEE80211W
  309. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  310. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  311. wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
  312. "protection");
  313. break;
  314. }
  315. #endif /* CONFIG_IEEE80211W */
  316. wpa_printf(MSG_DEBUG, " selected based on RSN IE");
  317. return 1;
  318. }
  319. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  320. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  321. proto_match++;
  322. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  323. wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
  324. break;
  325. }
  326. if (wep_ok &&
  327. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  328. {
  329. wpa_printf(MSG_DEBUG, " selected based on TSN in "
  330. "WPA IE");
  331. return 1;
  332. }
  333. if (!(ie.proto & ssid->proto)) {
  334. wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
  335. "mismatch");
  336. break;
  337. }
  338. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  339. wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
  340. "mismatch");
  341. break;
  342. }
  343. if (!(ie.group_cipher & ssid->group_cipher)) {
  344. wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
  345. "mismatch");
  346. break;
  347. }
  348. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  349. wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
  350. "mismatch");
  351. break;
  352. }
  353. wpa_printf(MSG_DEBUG, " selected based on WPA IE");
  354. return 1;
  355. }
  356. if (proto_match == 0)
  357. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  358. return 0;
  359. }
  360. static int freq_allowed(int *freqs, int freq)
  361. {
  362. int i;
  363. if (freqs == NULL)
  364. return 1;
  365. for (i = 0; freqs[i]; i++)
  366. if (freqs[i] == freq)
  367. return 1;
  368. return 0;
  369. }
  370. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  371. int i, struct wpa_scan_res *bss,
  372. struct wpa_ssid *group)
  373. {
  374. const u8 *ssid_;
  375. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  376. int wpa;
  377. struct wpa_blacklist *e;
  378. const u8 *ie;
  379. struct wpa_ssid *ssid;
  380. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  381. ssid_ = ie ? ie + 2 : (u8 *) "";
  382. ssid_len = ie ? ie[1] : 0;
  383. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  384. wpa_ie_len = ie ? ie[1] : 0;
  385. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  386. rsn_ie_len = ie ? ie[1] : 0;
  387. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  388. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
  389. i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
  390. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  391. wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
  392. " wps" : "");
  393. e = wpa_blacklist_get(wpa_s, bss->bssid);
  394. if (e && e->count > 1) {
  395. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  396. return 0;
  397. }
  398. if (ssid_len == 0) {
  399. wpa_printf(MSG_DEBUG, " skip - SSID not known");
  400. return 0;
  401. }
  402. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  403. for (ssid = group; ssid; ssid = ssid->pnext) {
  404. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  405. if (ssid->disabled) {
  406. wpa_printf(MSG_DEBUG, " skip - disabled");
  407. continue;
  408. }
  409. #ifdef CONFIG_WPS
  410. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  411. wpa_printf(MSG_DEBUG, " skip - blacklisted (WPS)");
  412. continue;
  413. }
  414. if (wpa && ssid->ssid_len == 0 &&
  415. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  416. check_ssid = 0;
  417. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  418. /* Only allow wildcard SSID match if an AP
  419. * advertises active WPS operation that matches
  420. * with our mode. */
  421. check_ssid = 1;
  422. if (ssid->ssid_len == 0 &&
  423. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  424. check_ssid = 0;
  425. }
  426. #endif /* CONFIG_WPS */
  427. if (check_ssid &&
  428. (ssid_len != ssid->ssid_len ||
  429. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  430. wpa_printf(MSG_DEBUG, " skip - SSID mismatch");
  431. continue;
  432. }
  433. if (ssid->bssid_set &&
  434. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  435. wpa_printf(MSG_DEBUG, " skip - BSSID mismatch");
  436. continue;
  437. }
  438. if (wpa && !wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  439. continue;
  440. if (!wpa &&
  441. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  442. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  443. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  444. wpa_printf(MSG_DEBUG, " skip - non-WPA network not "
  445. "allowed");
  446. continue;
  447. }
  448. if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
  449. wpa_printf(MSG_DEBUG, " skip - privacy mismatch");
  450. continue;
  451. }
  452. if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
  453. wpa_printf(MSG_DEBUG, " skip - IBSS (adhoc) "
  454. "network");
  455. continue;
  456. }
  457. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  458. wpa_printf(MSG_DEBUG, " skip - frequency not "
  459. "allowed");
  460. continue;
  461. }
  462. #ifdef CONFIG_P2P
  463. /*
  464. * TODO: skip the AP if its P2P IE has Group Formation
  465. * bit set in the P2P Group Capability Bitmap and we
  466. * are not in Group Formation with that device.
  467. */
  468. #endif /* CONFIG_P2P */
  469. /* Matching configuration found */
  470. return ssid;
  471. }
  472. /* No matching configuration found */
  473. return 0;
  474. }
  475. static struct wpa_bss *
  476. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  477. struct wpa_scan_results *scan_res,
  478. struct wpa_ssid *group,
  479. struct wpa_ssid **selected_ssid)
  480. {
  481. size_t i;
  482. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  483. group->priority);
  484. for (i = 0; i < scan_res->num; i++) {
  485. struct wpa_scan_res *bss = scan_res->res[i];
  486. const u8 *ie, *ssid;
  487. u8 ssid_len;
  488. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
  489. if (!*selected_ssid)
  490. continue;
  491. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  492. ssid = ie ? ie + 2 : (u8 *) "";
  493. ssid_len = ie ? ie[1] : 0;
  494. wpa_printf(MSG_DEBUG, " selected BSS " MACSTR " ssid='%s'",
  495. MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  496. return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
  497. }
  498. return NULL;
  499. }
  500. static struct wpa_bss *
  501. wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  502. struct wpa_scan_results *scan_res,
  503. struct wpa_ssid **selected_ssid)
  504. {
  505. struct wpa_bss *selected = NULL;
  506. int prio;
  507. while (selected == NULL) {
  508. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  509. selected = wpa_supplicant_select_bss(
  510. wpa_s, scan_res, wpa_s->conf->pssid[prio],
  511. selected_ssid);
  512. if (selected)
  513. break;
  514. }
  515. if (selected == NULL && wpa_s->blacklist) {
  516. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  517. "and try again");
  518. wpa_blacklist_clear(wpa_s);
  519. wpa_s->blacklist_cleared++;
  520. } else if (selected == NULL)
  521. break;
  522. }
  523. return selected;
  524. }
  525. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  526. int timeout_sec, int timeout_usec)
  527. {
  528. if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
  529. /*
  530. * No networks are enabled; short-circuit request so
  531. * we don't wait timeout seconds before transitioning
  532. * to INACTIVE state.
  533. */
  534. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  535. return;
  536. }
  537. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  538. }
  539. void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  540. struct wpa_bss *selected,
  541. struct wpa_ssid *ssid)
  542. {
  543. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  544. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  545. "PBC session overlap");
  546. #ifdef CONFIG_P2P
  547. if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
  548. return;
  549. #endif /* CONFIG_P2P */
  550. #ifdef CONFIG_WPS
  551. wpas_wps_cancel(wpa_s);
  552. #endif /* CONFIG_WPS */
  553. return;
  554. }
  555. /*
  556. * Do not trigger new association unless the BSSID has changed or if
  557. * reassociation is requested. If we are in process of associating with
  558. * the selected BSSID, do not trigger new attempt.
  559. */
  560. if (wpa_s->reassociate ||
  561. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  562. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  563. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  564. 0))) {
  565. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  566. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  567. return;
  568. }
  569. wpa_supplicant_associate(wpa_s, selected, ssid);
  570. } else {
  571. wpa_printf(MSG_DEBUG, "Already associated with the selected "
  572. "AP");
  573. }
  574. }
  575. static struct wpa_ssid *
  576. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  577. {
  578. int prio;
  579. struct wpa_ssid *ssid;
  580. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  581. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  582. {
  583. if (ssid->disabled)
  584. continue;
  585. if (ssid->mode == IEEE80211_MODE_IBSS ||
  586. ssid->mode == IEEE80211_MODE_AP)
  587. return ssid;
  588. }
  589. }
  590. return NULL;
  591. }
  592. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  593. * on BSS added and BSS changed events */
  594. static void wpa_supplicant_rsn_preauth_scan_results(
  595. struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
  596. {
  597. int i;
  598. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  599. return;
  600. for (i = scan_res->num - 1; i >= 0; i--) {
  601. const u8 *ssid, *rsn;
  602. struct wpa_scan_res *r;
  603. r = scan_res->res[i];
  604. ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
  605. if (ssid == NULL)
  606. continue;
  607. rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
  608. if (rsn == NULL)
  609. continue;
  610. rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
  611. }
  612. }
  613. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  614. struct wpa_bss *selected,
  615. struct wpa_ssid *ssid,
  616. struct wpa_scan_results *scan_res)
  617. {
  618. size_t i;
  619. struct wpa_scan_res *current_bss = NULL;
  620. int min_diff;
  621. if (wpa_s->reassociate)
  622. return 1; /* explicit request to reassociate */
  623. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  624. return 1; /* we are not associated; continue */
  625. if (wpa_s->current_ssid == NULL)
  626. return 1; /* unknown current SSID */
  627. if (wpa_s->current_ssid != ssid)
  628. return 1; /* different network block */
  629. for (i = 0; i < scan_res->num; i++) {
  630. struct wpa_scan_res *res = scan_res->res[i];
  631. const u8 *ie;
  632. if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  633. continue;
  634. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  635. if (ie == NULL)
  636. continue;
  637. if (ie[1] != wpa_s->current_ssid->ssid_len ||
  638. os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
  639. continue;
  640. current_bss = res;
  641. break;
  642. }
  643. if (!current_bss)
  644. return 1; /* current BSS not seen in scan results */
  645. wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
  646. wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  647. MAC2STR(current_bss->bssid), current_bss->level);
  648. wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  649. MAC2STR(selected->bssid), selected->level);
  650. if (wpa_s->current_ssid->bssid_set &&
  651. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  652. 0) {
  653. wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
  654. "preferred BSSID");
  655. return 1;
  656. }
  657. min_diff = 2;
  658. if (current_bss->level < 0) {
  659. if (current_bss->level < -85)
  660. min_diff = 1;
  661. else if (current_bss->level < -80)
  662. min_diff = 2;
  663. else if (current_bss->level < -75)
  664. min_diff = 3;
  665. else if (current_bss->level < -70)
  666. min_diff = 4;
  667. else
  668. min_diff = 5;
  669. }
  670. if (abs(current_bss->level - selected->level) < min_diff) {
  671. wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
  672. "signal level");
  673. return 0;
  674. }
  675. return 1;
  676. }
  677. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  678. union wpa_event_data *data)
  679. {
  680. struct wpa_bss *selected;
  681. struct wpa_ssid *ssid = NULL;
  682. struct wpa_scan_results *scan_res;
  683. int ap = 0;
  684. #ifdef CONFIG_AP
  685. if (wpa_s->ap_iface)
  686. ap = 1;
  687. #endif /* CONFIG_AP */
  688. wpa_supplicant_notify_scanning(wpa_s, 0);
  689. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  690. data ? &data->scan_info :
  691. NULL, 1);
  692. if (scan_res == NULL) {
  693. if (wpa_s->conf->ap_scan == 2 || ap)
  694. return;
  695. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  696. "scanning again");
  697. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  698. return;
  699. }
  700. #ifndef CONFIG_NO_RANDOM_POOL
  701. size_t i, num;
  702. num = scan_res->num;
  703. if (num > 10)
  704. num = 10;
  705. for (i = 0; i < num; i++) {
  706. u8 buf[5];
  707. struct wpa_scan_res *res = scan_res->res[i];
  708. buf[0] = res->bssid[5];
  709. buf[1] = res->qual & 0xff;
  710. buf[2] = res->noise & 0xff;
  711. buf[3] = res->level & 0xff;
  712. buf[4] = res->tsf & 0xff;
  713. random_add_randomness(buf, sizeof(buf));
  714. }
  715. #endif /* CONFIG_NO_RANDOM_POOL */
  716. if (wpa_s->scan_res_handler) {
  717. wpa_s->scan_res_handler(wpa_s, scan_res);
  718. wpa_s->scan_res_handler = NULL;
  719. wpa_scan_results_free(scan_res);
  720. return;
  721. }
  722. if (ap) {
  723. wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
  724. wpa_scan_results_free(scan_res);
  725. return;
  726. }
  727. wpa_printf(MSG_DEBUG, "New scan results available");
  728. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  729. wpas_notify_scan_results(wpa_s);
  730. wpas_notify_scan_done(wpa_s, 1);
  731. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  732. wpa_scan_results_free(scan_res);
  733. return;
  734. }
  735. if (wpa_s->disconnected) {
  736. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  737. wpa_scan_results_free(scan_res);
  738. return;
  739. }
  740. if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
  741. wpa_scan_results_free(scan_res);
  742. return;
  743. }
  744. wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
  745. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  746. if (selected) {
  747. int skip;
  748. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  749. scan_res);
  750. wpa_scan_results_free(scan_res);
  751. if (skip)
  752. return;
  753. wpa_supplicant_connect(wpa_s, selected, ssid);
  754. } else {
  755. wpa_scan_results_free(scan_res);
  756. wpa_printf(MSG_DEBUG, "No suitable network found");
  757. ssid = wpa_supplicant_pick_new_network(wpa_s);
  758. if (ssid) {
  759. wpa_printf(MSG_DEBUG, "Setup a new network");
  760. wpa_supplicant_associate(wpa_s, NULL, ssid);
  761. } else {
  762. int timeout_sec = 5;
  763. int timeout_usec = 0;
  764. #ifdef CONFIG_P2P
  765. if (wpa_s->p2p_in_provisioning) {
  766. /*
  767. * Use shorter wait during P2P Provisioning
  768. * state to speed up group formation.
  769. */
  770. timeout_sec = 0;
  771. timeout_usec = 250000;
  772. }
  773. #endif /* CONFIG_P2P */
  774. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  775. timeout_usec);
  776. }
  777. }
  778. }
  779. #endif /* CONFIG_NO_SCAN_PROCESSING */
  780. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  781. union wpa_event_data *data)
  782. {
  783. int l, len, found = 0, wpa_found, rsn_found;
  784. const u8 *p;
  785. wpa_printf(MSG_DEBUG, "Association info event");
  786. if (data->assoc_info.req_ies)
  787. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  788. data->assoc_info.req_ies_len);
  789. if (data->assoc_info.resp_ies)
  790. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  791. data->assoc_info.resp_ies_len);
  792. if (data->assoc_info.beacon_ies)
  793. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  794. data->assoc_info.beacon_ies,
  795. data->assoc_info.beacon_ies_len);
  796. if (data->assoc_info.freq)
  797. wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
  798. p = data->assoc_info.req_ies;
  799. l = data->assoc_info.req_ies_len;
  800. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  801. while (p && l >= 2) {
  802. len = p[1] + 2;
  803. if (len > l) {
  804. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  805. p, l);
  806. break;
  807. }
  808. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  809. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  810. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  811. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  812. break;
  813. found = 1;
  814. wpa_find_assoc_pmkid(wpa_s);
  815. break;
  816. }
  817. l -= len;
  818. p += len;
  819. }
  820. if (!found && data->assoc_info.req_ies)
  821. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  822. #ifdef CONFIG_IEEE80211R
  823. #ifdef CONFIG_SME
  824. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  825. u8 bssid[ETH_ALEN];
  826. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  827. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  828. data->assoc_info.resp_ies,
  829. data->assoc_info.resp_ies_len,
  830. bssid) < 0) {
  831. wpa_printf(MSG_DEBUG, "FT: Validation of "
  832. "Reassociation Response failed");
  833. wpa_supplicant_deauthenticate(
  834. wpa_s, WLAN_REASON_INVALID_IE);
  835. return -1;
  836. }
  837. }
  838. p = data->assoc_info.resp_ies;
  839. l = data->assoc_info.resp_ies_len;
  840. #ifdef CONFIG_WPS_STRICT
  841. if (wpa_s->current_ssid &&
  842. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  843. struct wpabuf *wps;
  844. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  845. if (wps == NULL) {
  846. wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
  847. "WPS IE in (Re)Association Response");
  848. return -1;
  849. }
  850. if (wps_validate_assoc_resp(wps) < 0) {
  851. wpabuf_free(wps);
  852. wpa_supplicant_deauthenticate(
  853. wpa_s, WLAN_REASON_INVALID_IE);
  854. return -1;
  855. }
  856. wpabuf_free(wps);
  857. }
  858. #endif /* CONFIG_WPS_STRICT */
  859. /* Go through the IEs and make a copy of the MDIE, if present. */
  860. while (p && l >= 2) {
  861. len = p[1] + 2;
  862. if (len > l) {
  863. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  864. p, l);
  865. break;
  866. }
  867. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  868. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  869. wpa_s->sme.ft_used = 1;
  870. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  871. MOBILITY_DOMAIN_ID_LEN);
  872. break;
  873. }
  874. l -= len;
  875. p += len;
  876. }
  877. #endif /* CONFIG_SME */
  878. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  879. data->assoc_info.resp_ies_len);
  880. #endif /* CONFIG_IEEE80211R */
  881. /* WPA/RSN IE from Beacon/ProbeResp */
  882. p = data->assoc_info.beacon_ies;
  883. l = data->assoc_info.beacon_ies_len;
  884. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  885. */
  886. wpa_found = rsn_found = 0;
  887. while (p && l >= 2) {
  888. len = p[1] + 2;
  889. if (len > l) {
  890. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  891. p, l);
  892. break;
  893. }
  894. if (!wpa_found &&
  895. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  896. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  897. wpa_found = 1;
  898. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  899. }
  900. if (!rsn_found &&
  901. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  902. rsn_found = 1;
  903. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  904. }
  905. l -= len;
  906. p += len;
  907. }
  908. if (!wpa_found && data->assoc_info.beacon_ies)
  909. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  910. if (!rsn_found && data->assoc_info.beacon_ies)
  911. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  912. if (wpa_found || rsn_found)
  913. wpa_s->ap_ies_from_associnfo = 1;
  914. wpa_s->assoc_freq = data->assoc_info.freq;
  915. return 0;
  916. }
  917. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  918. union wpa_event_data *data)
  919. {
  920. u8 bssid[ETH_ALEN];
  921. int ft_completed;
  922. int bssid_changed;
  923. struct wpa_driver_capa capa;
  924. #ifdef CONFIG_AP
  925. if (wpa_s->ap_iface) {
  926. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  927. data->assoc_info.addr,
  928. data->assoc_info.req_ies,
  929. data->assoc_info.req_ies_len);
  930. return;
  931. }
  932. #endif /* CONFIG_AP */
  933. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  934. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  935. return;
  936. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  937. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  938. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  939. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  940. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  941. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  942. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  943. MACSTR, MAC2STR(bssid));
  944. random_add_randomness(bssid, ETH_ALEN);
  945. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  946. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  947. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  948. if (bssid_changed)
  949. wpas_notify_bssid_changed(wpa_s);
  950. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  951. wpa_clear_keys(wpa_s, bssid);
  952. }
  953. if (wpa_supplicant_select_config(wpa_s) < 0) {
  954. wpa_supplicant_disassociate(
  955. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  956. return;
  957. }
  958. if (wpa_s->current_ssid) {
  959. struct wpa_bss *bss = NULL;
  960. struct wpa_ssid *ssid = wpa_s->current_ssid;
  961. if (ssid->ssid_len > 0)
  962. bss = wpa_bss_get(wpa_s, bssid,
  963. ssid->ssid, ssid->ssid_len);
  964. if (!bss)
  965. bss = wpa_bss_get_bssid(wpa_s, bssid);
  966. if (bss)
  967. wpa_s->current_bss = bss;
  968. }
  969. }
  970. #ifdef CONFIG_SME
  971. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  972. wpa_s->sme.prev_bssid_set = 1;
  973. #endif /* CONFIG_SME */
  974. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  975. if (wpa_s->current_ssid) {
  976. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  977. * initialized before association, but for other modes,
  978. * initialize PC/SC here, if the current configuration needs
  979. * smartcard or SIM/USIM. */
  980. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  981. }
  982. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  983. if (wpa_s->l2)
  984. l2_packet_notify_auth_start(wpa_s->l2);
  985. /*
  986. * Set portEnabled first to FALSE in order to get EAP state machine out
  987. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  988. * state machine may transit to AUTHENTICATING state based on obsolete
  989. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  990. * AUTHENTICATED without ever giving chance to EAP state machine to
  991. * reset the state.
  992. */
  993. if (!ft_completed) {
  994. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  995. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  996. }
  997. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  998. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  999. /* 802.1X::portControl = Auto */
  1000. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1001. wpa_s->eapol_received = 0;
  1002. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1003. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1004. (wpa_s->current_ssid &&
  1005. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1006. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1007. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1008. } else if (!ft_completed) {
  1009. /* Timeout for receiving the first EAPOL packet */
  1010. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1011. }
  1012. wpa_supplicant_cancel_scan(wpa_s);
  1013. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1014. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1015. /*
  1016. * We are done; the driver will take care of RSN 4-way
  1017. * handshake.
  1018. */
  1019. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1020. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1021. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1022. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1023. }
  1024. if (wpa_s->pending_eapol_rx) {
  1025. struct os_time now, age;
  1026. os_get_time(&now);
  1027. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1028. if (age.sec == 0 && age.usec < 100000 &&
  1029. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1030. 0) {
  1031. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  1032. "that was received just before association "
  1033. "notification");
  1034. wpa_supplicant_rx_eapol(
  1035. wpa_s, wpa_s->pending_eapol_rx_src,
  1036. wpabuf_head(wpa_s->pending_eapol_rx),
  1037. wpabuf_len(wpa_s->pending_eapol_rx));
  1038. }
  1039. wpabuf_free(wpa_s->pending_eapol_rx);
  1040. wpa_s->pending_eapol_rx = NULL;
  1041. }
  1042. #ifdef CONFIG_BGSCAN
  1043. if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
  1044. bgscan_deinit(wpa_s);
  1045. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  1046. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  1047. wpa_printf(MSG_DEBUG, "Failed to initialize "
  1048. "bgscan");
  1049. /*
  1050. * Live without bgscan; it is only used as a
  1051. * roaming optimization, so the initial
  1052. * connection is not affected.
  1053. */
  1054. } else
  1055. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  1056. } else
  1057. wpa_s->bgscan_ssid = NULL;
  1058. }
  1059. #endif /* CONFIG_BGSCAN */
  1060. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1061. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1062. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1063. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1064. /* Set static WEP keys again */
  1065. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1066. }
  1067. }
  1068. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1069. u16 reason_code)
  1070. {
  1071. const u8 *bssid;
  1072. #ifdef CONFIG_SME
  1073. int authenticating;
  1074. u8 prev_pending_bssid[ETH_ALEN];
  1075. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1076. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1077. #endif /* CONFIG_SME */
  1078. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1079. /*
  1080. * At least Host AP driver and a Prism3 card seemed to be
  1081. * generating streams of disconnected events when configuring
  1082. * IBSS for WPA-None. Ignore them for now.
  1083. */
  1084. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  1085. "IBSS/WPA-None mode");
  1086. return;
  1087. }
  1088. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1089. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1090. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1091. "pre-shared key may be incorrect");
  1092. }
  1093. if (!wpa_s->auto_reconnect_disabled ||
  1094. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  1095. wpa_printf(MSG_DEBUG, "WPA: Auto connect enabled: try to "
  1096. "reconnect (wps=%d)",
  1097. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
  1098. if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1099. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1100. } else {
  1101. wpa_printf(MSG_DEBUG, "WPA: Auto connect disabled: do not try "
  1102. "to re-connect");
  1103. wpa_s->reassociate = 0;
  1104. wpa_s->disconnected = 1;
  1105. }
  1106. bssid = wpa_s->bssid;
  1107. if (is_zero_ether_addr(bssid))
  1108. bssid = wpa_s->pending_bssid;
  1109. wpa_blacklist_add(wpa_s, bssid);
  1110. wpa_sm_notify_disassoc(wpa_s->wpa);
  1111. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1112. " reason=%d",
  1113. MAC2STR(bssid), reason_code);
  1114. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1115. wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
  1116. wpa_s->keys_cleared = 0;
  1117. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1118. }
  1119. wpa_supplicant_mark_disassoc(wpa_s);
  1120. bgscan_deinit(wpa_s);
  1121. wpa_s->bgscan_ssid = NULL;
  1122. #ifdef CONFIG_SME
  1123. if (authenticating &&
  1124. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1125. /*
  1126. * mac80211-workaround to force deauth on failed auth cmd,
  1127. * requires us to remain in authenticating state to allow the
  1128. * second authentication attempt to be continued properly.
  1129. */
  1130. wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
  1131. "proceed after disconnection event");
  1132. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1133. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1134. }
  1135. #endif /* CONFIG_SME */
  1136. }
  1137. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1138. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  1139. void *sock_ctx)
  1140. {
  1141. struct wpa_supplicant *wpa_s = eloop_ctx;
  1142. if (!wpa_s->pending_mic_error_report)
  1143. return;
  1144. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  1145. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1146. wpa_s->pending_mic_error_report = 0;
  1147. }
  1148. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1149. static void
  1150. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1151. union wpa_event_data *data)
  1152. {
  1153. int pairwise;
  1154. struct os_time t;
  1155. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1156. pairwise = (data && data->michael_mic_failure.unicast);
  1157. os_get_time(&t);
  1158. if ((wpa_s->last_michael_mic_error &&
  1159. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1160. wpa_s->pending_mic_error_report) {
  1161. if (wpa_s->pending_mic_error_report) {
  1162. /*
  1163. * Send the pending MIC error report immediately since
  1164. * we are going to start countermeasures and AP better
  1165. * do the same.
  1166. */
  1167. wpa_sm_key_request(wpa_s->wpa, 1,
  1168. wpa_s->pending_mic_error_pairwise);
  1169. }
  1170. /* Send the new MIC error report immediately since we are going
  1171. * to start countermeasures and AP better do the same.
  1172. */
  1173. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1174. /* initialize countermeasures */
  1175. wpa_s->countermeasures = 1;
  1176. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1177. /*
  1178. * Need to wait for completion of request frame. We do not get
  1179. * any callback for the message completion, so just wait a
  1180. * short while and hope for the best. */
  1181. os_sleep(0, 10000);
  1182. wpa_drv_set_countermeasures(wpa_s, 1);
  1183. wpa_supplicant_deauthenticate(wpa_s,
  1184. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1185. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1186. wpa_s, NULL);
  1187. eloop_register_timeout(60, 0,
  1188. wpa_supplicant_stop_countermeasures,
  1189. wpa_s, NULL);
  1190. /* TODO: mark the AP rejected for 60 second. STA is
  1191. * allowed to associate with another AP.. */
  1192. } else {
  1193. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1194. if (wpa_s->mic_errors_seen) {
  1195. /*
  1196. * Reduce the effectiveness of Michael MIC error
  1197. * reports as a means for attacking against TKIP if
  1198. * more than one MIC failure is noticed with the same
  1199. * PTK. We delay the transmission of the reports by a
  1200. * random time between 0 and 60 seconds in order to
  1201. * force the attacker wait 60 seconds before getting
  1202. * the information on whether a frame resulted in a MIC
  1203. * failure.
  1204. */
  1205. u8 rval[4];
  1206. int sec;
  1207. if (os_get_random(rval, sizeof(rval)) < 0)
  1208. sec = os_random() % 60;
  1209. else
  1210. sec = WPA_GET_BE32(rval) % 60;
  1211. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  1212. "seconds", sec);
  1213. wpa_s->pending_mic_error_report = 1;
  1214. wpa_s->pending_mic_error_pairwise = pairwise;
  1215. eloop_cancel_timeout(
  1216. wpa_supplicant_delayed_mic_error_report,
  1217. wpa_s, NULL);
  1218. eloop_register_timeout(
  1219. sec, os_random() % 1000000,
  1220. wpa_supplicant_delayed_mic_error_report,
  1221. wpa_s, NULL);
  1222. } else {
  1223. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1224. }
  1225. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1226. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1227. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1228. }
  1229. wpa_s->last_michael_mic_error = t.sec;
  1230. wpa_s->mic_errors_seen++;
  1231. }
  1232. #ifdef CONFIG_TERMINATE_ONLASTIF
  1233. static int any_interfaces(struct wpa_supplicant *head)
  1234. {
  1235. struct wpa_supplicant *wpa_s;
  1236. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1237. if (!wpa_s->interface_removed)
  1238. return 1;
  1239. return 0;
  1240. }
  1241. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1242. static void
  1243. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1244. union wpa_event_data *data)
  1245. {
  1246. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1247. return;
  1248. switch (data->interface_status.ievent) {
  1249. case EVENT_INTERFACE_ADDED:
  1250. if (!wpa_s->interface_removed)
  1251. break;
  1252. wpa_s->interface_removed = 0;
  1253. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  1254. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1255. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  1256. "after interface was added.");
  1257. }
  1258. break;
  1259. case EVENT_INTERFACE_REMOVED:
  1260. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1261. wpa_s->interface_removed = 1;
  1262. wpa_supplicant_mark_disassoc(wpa_s);
  1263. l2_packet_deinit(wpa_s->l2);
  1264. wpa_s->l2 = NULL;
  1265. #ifdef CONFIG_TERMINATE_ONLASTIF
  1266. /* check if last interface */
  1267. if (!any_interfaces(wpa_s->global->ifaces))
  1268. eloop_terminate();
  1269. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1270. break;
  1271. }
  1272. }
  1273. #ifdef CONFIG_PEERKEY
  1274. static void
  1275. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1276. union wpa_event_data *data)
  1277. {
  1278. if (data == NULL)
  1279. return;
  1280. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1281. }
  1282. #endif /* CONFIG_PEERKEY */
  1283. #ifdef CONFIG_IEEE80211R
  1284. static void
  1285. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1286. union wpa_event_data *data)
  1287. {
  1288. if (data == NULL)
  1289. return;
  1290. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1291. data->ft_ies.ies_len,
  1292. data->ft_ies.ft_action,
  1293. data->ft_ies.target_ap,
  1294. data->ft_ies.ric_ies,
  1295. data->ft_ies.ric_ies_len) < 0) {
  1296. /* TODO: prevent MLME/driver from trying to associate? */
  1297. }
  1298. }
  1299. #endif /* CONFIG_IEEE80211R */
  1300. #ifdef CONFIG_IBSS_RSN
  1301. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1302. union wpa_event_data *data)
  1303. {
  1304. if (data == NULL)
  1305. return;
  1306. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1307. }
  1308. #endif /* CONFIG_IBSS_RSN */
  1309. #ifdef CONFIG_IEEE80211R
  1310. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  1311. size_t len)
  1312. {
  1313. const u8 *sta_addr, *target_ap_addr;
  1314. u16 status;
  1315. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  1316. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1317. return; /* only SME case supported for now */
  1318. if (len < 1 + 2 * ETH_ALEN + 2)
  1319. return;
  1320. if (data[0] != 2)
  1321. return; /* Only FT Action Response is supported for now */
  1322. sta_addr = data + 1;
  1323. target_ap_addr = data + 1 + ETH_ALEN;
  1324. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  1325. wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
  1326. " TargetAP " MACSTR " status %u",
  1327. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1328. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1329. wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  1330. " in FT Action Response", MAC2STR(sta_addr));
  1331. return;
  1332. }
  1333. if (status) {
  1334. wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
  1335. "failure (status code %d)", status);
  1336. /* TODO: report error to FT code(?) */
  1337. return;
  1338. }
  1339. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  1340. len - (1 + 2 * ETH_ALEN + 2), 1,
  1341. target_ap_addr, NULL, 0) < 0)
  1342. return;
  1343. #ifdef CONFIG_SME
  1344. {
  1345. struct wpa_bss *bss;
  1346. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  1347. if (bss)
  1348. wpa_s->sme.freq = bss->freq;
  1349. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  1350. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  1351. WLAN_AUTH_FT);
  1352. }
  1353. #endif /* CONFIG_SME */
  1354. }
  1355. #endif /* CONFIG_IEEE80211R */
  1356. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1357. union wpa_event_data *data)
  1358. {
  1359. struct wpa_supplicant *wpa_s = ctx;
  1360. u16 reason_code = 0;
  1361. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  1362. event != EVENT_INTERFACE_ENABLED &&
  1363. event != EVENT_INTERFACE_STATUS) {
  1364. wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
  1365. "disabled", event);
  1366. return;
  1367. }
  1368. wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
  1369. event, wpa_s->ifname);
  1370. switch (event) {
  1371. case EVENT_AUTH:
  1372. sme_event_auth(wpa_s, data);
  1373. break;
  1374. case EVENT_ASSOC:
  1375. wpa_supplicant_event_assoc(wpa_s, data);
  1376. break;
  1377. case EVENT_DISASSOC:
  1378. wpa_printf(MSG_DEBUG, "Disassociation notification");
  1379. if (data) {
  1380. wpa_printf(MSG_DEBUG, " * reason %u",
  1381. data->disassoc_info.reason_code);
  1382. if (data->disassoc_info.addr)
  1383. wpa_printf(MSG_DEBUG, " * address " MACSTR,
  1384. MAC2STR(data->disassoc_info.addr));
  1385. }
  1386. #ifdef CONFIG_AP
  1387. if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
  1388. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1389. data->disassoc_info.addr);
  1390. break;
  1391. }
  1392. #endif /* CONFIG_AP */
  1393. if (data) {
  1394. reason_code = data->disassoc_info.reason_code;
  1395. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  1396. data->disassoc_info.ie,
  1397. data->disassoc_info.ie_len);
  1398. #ifdef CONFIG_P2P
  1399. wpas_p2p_disassoc_notif(
  1400. wpa_s, data->disassoc_info.addr, reason_code,
  1401. data->disassoc_info.ie,
  1402. data->disassoc_info.ie_len);
  1403. #endif /* CONFIG_P2P */
  1404. }
  1405. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1406. sme_event_disassoc(wpa_s, data);
  1407. /* fall through */
  1408. case EVENT_DEAUTH:
  1409. if (event == EVENT_DEAUTH) {
  1410. wpa_printf(MSG_DEBUG, "Deauthentication notification");
  1411. if (data) {
  1412. reason_code = data->deauth_info.reason_code;
  1413. wpa_printf(MSG_DEBUG, " * reason %u",
  1414. data->deauth_info.reason_code);
  1415. if (data->deauth_info.addr) {
  1416. wpa_printf(MSG_DEBUG, " * address "
  1417. MACSTR,
  1418. MAC2STR(data->deauth_info.
  1419. addr));
  1420. }
  1421. wpa_hexdump(MSG_DEBUG,
  1422. "Deauthentication frame IE(s)",
  1423. data->deauth_info.ie,
  1424. data->deauth_info.ie_len);
  1425. #ifdef CONFIG_P2P
  1426. wpas_p2p_deauth_notif(
  1427. wpa_s, data->deauth_info.addr,
  1428. reason_code,
  1429. data->deauth_info.ie,
  1430. data->deauth_info.ie_len);
  1431. #endif /* CONFIG_P2P */
  1432. }
  1433. }
  1434. #ifdef CONFIG_AP
  1435. if (wpa_s->ap_iface && data && data->deauth_info.addr) {
  1436. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1437. data->deauth_info.addr);
  1438. break;
  1439. }
  1440. #endif /* CONFIG_AP */
  1441. wpa_supplicant_event_disassoc(wpa_s, reason_code);
  1442. break;
  1443. case EVENT_MICHAEL_MIC_FAILURE:
  1444. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1445. break;
  1446. #ifndef CONFIG_NO_SCAN_PROCESSING
  1447. case EVENT_SCAN_RESULTS:
  1448. wpa_supplicant_event_scan_results(wpa_s, data);
  1449. break;
  1450. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1451. case EVENT_ASSOCINFO:
  1452. wpa_supplicant_event_associnfo(wpa_s, data);
  1453. break;
  1454. case EVENT_INTERFACE_STATUS:
  1455. wpa_supplicant_event_interface_status(wpa_s, data);
  1456. break;
  1457. case EVENT_PMKID_CANDIDATE:
  1458. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1459. break;
  1460. #ifdef CONFIG_PEERKEY
  1461. case EVENT_STKSTART:
  1462. wpa_supplicant_event_stkstart(wpa_s, data);
  1463. break;
  1464. #endif /* CONFIG_PEERKEY */
  1465. #ifdef CONFIG_IEEE80211R
  1466. case EVENT_FT_RESPONSE:
  1467. wpa_supplicant_event_ft_response(wpa_s, data);
  1468. break;
  1469. #endif /* CONFIG_IEEE80211R */
  1470. #ifdef CONFIG_IBSS_RSN
  1471. case EVENT_IBSS_RSN_START:
  1472. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1473. break;
  1474. #endif /* CONFIG_IBSS_RSN */
  1475. case EVENT_ASSOC_REJECT:
  1476. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT "bssid=" MACSTR
  1477. " status_code=%u",
  1478. MAC2STR(data->assoc_reject.bssid),
  1479. data->assoc_reject.status_code);
  1480. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1481. sme_event_assoc_reject(wpa_s, data);
  1482. break;
  1483. case EVENT_AUTH_TIMED_OUT:
  1484. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1485. sme_event_auth_timed_out(wpa_s, data);
  1486. break;
  1487. case EVENT_ASSOC_TIMED_OUT:
  1488. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1489. sme_event_assoc_timed_out(wpa_s, data);
  1490. break;
  1491. #ifdef CONFIG_AP
  1492. case EVENT_TX_STATUS:
  1493. wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS on %s dst=" MACSTR
  1494. " type=%d stype=%d",
  1495. wpa_s->ifname, MAC2STR(data->tx_status.dst),
  1496. data->tx_status.type, data->tx_status.stype);
  1497. if (wpa_s->ap_iface == NULL) {
  1498. #ifdef CONFIG_P2P
  1499. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1500. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  1501. wpas_send_action_tx_status(
  1502. wpa_s, data->tx_status.dst,
  1503. data->tx_status.data,
  1504. data->tx_status.data_len,
  1505. data->tx_status.ack ?
  1506. P2P_SEND_ACTION_SUCCESS :
  1507. P2P_SEND_ACTION_NO_ACK);
  1508. #endif /* CONFIG_P2P */
  1509. break;
  1510. }
  1511. #ifdef CONFIG_P2P
  1512. wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS pending_dst=" MACSTR,
  1513. MAC2STR(wpa_s->parent->pending_action_dst));
  1514. /*
  1515. * Catch TX status events for Action frames we sent via group
  1516. * interface in GO mode.
  1517. */
  1518. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1519. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  1520. os_memcmp(wpa_s->parent->pending_action_dst,
  1521. data->tx_status.dst, ETH_ALEN) == 0) {
  1522. wpas_send_action_tx_status(
  1523. wpa_s->parent, data->tx_status.dst,
  1524. data->tx_status.data,
  1525. data->tx_status.data_len,
  1526. data->tx_status.ack ?
  1527. P2P_SEND_ACTION_SUCCESS :
  1528. P2P_SEND_ACTION_NO_ACK);
  1529. break;
  1530. }
  1531. #endif /* CONFIG_P2P */
  1532. switch (data->tx_status.type) {
  1533. case WLAN_FC_TYPE_MGMT:
  1534. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1535. data->tx_status.data_len,
  1536. data->tx_status.stype,
  1537. data->tx_status.ack);
  1538. break;
  1539. case WLAN_FC_TYPE_DATA:
  1540. ap_tx_status(wpa_s, data->tx_status.dst,
  1541. data->tx_status.data,
  1542. data->tx_status.data_len,
  1543. data->tx_status.ack);
  1544. break;
  1545. }
  1546. break;
  1547. case EVENT_RX_FROM_UNKNOWN:
  1548. if (wpa_s->ap_iface == NULL)
  1549. break;
  1550. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
  1551. data->rx_from_unknown.len);
  1552. break;
  1553. case EVENT_RX_MGMT:
  1554. if (wpa_s->ap_iface == NULL) {
  1555. #ifdef CONFIG_P2P
  1556. u16 fc, stype;
  1557. const struct ieee80211_mgmt *mgmt;
  1558. mgmt = (const struct ieee80211_mgmt *)
  1559. data->rx_mgmt.frame;
  1560. fc = le_to_host16(mgmt->frame_control);
  1561. stype = WLAN_FC_GET_STYPE(fc);
  1562. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  1563. data->rx_mgmt.frame_len > 24) {
  1564. const u8 *src = mgmt->sa;
  1565. const u8 *ie = mgmt->u.probe_req.variable;
  1566. size_t ie_len = data->rx_mgmt.frame_len -
  1567. (mgmt->u.probe_req.variable -
  1568. data->rx_mgmt.frame);
  1569. wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
  1570. break;
  1571. }
  1572. #endif /* CONFIG_P2P */
  1573. wpa_printf(MSG_DEBUG, "AP: ignore received management "
  1574. "frame in non-AP mode");
  1575. break;
  1576. }
  1577. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1578. break;
  1579. #endif /* CONFIG_AP */
  1580. case EVENT_RX_ACTION:
  1581. wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
  1582. " Category=%u DataLen=%d freq=%d MHz",
  1583. MAC2STR(data->rx_action.sa),
  1584. data->rx_action.category, (int) data->rx_action.len,
  1585. data->rx_action.freq);
  1586. #ifdef CONFIG_IEEE80211R
  1587. if (data->rx_action.category == WLAN_ACTION_FT) {
  1588. ft_rx_action(wpa_s, data->rx_action.data,
  1589. data->rx_action.len);
  1590. break;
  1591. }
  1592. #endif /* CONFIG_IEEE80211R */
  1593. #ifdef CONFIG_P2P
  1594. wpas_p2p_rx_action(wpa_s, data->rx_action.da,
  1595. data->rx_action.sa,
  1596. data->rx_action.bssid,
  1597. data->rx_action.category,
  1598. data->rx_action.data,
  1599. data->rx_action.len, data->rx_action.freq);
  1600. #endif /* CONFIG_P2P */
  1601. break;
  1602. #ifdef CONFIG_P2P
  1603. case EVENT_REMAIN_ON_CHANNEL:
  1604. wpas_p2p_remain_on_channel_cb(
  1605. wpa_s, data->remain_on_channel.freq,
  1606. data->remain_on_channel.duration);
  1607. break;
  1608. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  1609. wpas_p2p_cancel_remain_on_channel_cb(
  1610. wpa_s, data->remain_on_channel.freq);
  1611. break;
  1612. case EVENT_RX_PROBE_REQ:
  1613. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  1614. data->rx_probe_req.ie,
  1615. data->rx_probe_req.ie_len);
  1616. break;
  1617. #endif /* CONFIG_P2P */
  1618. #ifdef CONFIG_CLIENT_MLME
  1619. case EVENT_MLME_RX: {
  1620. struct ieee80211_rx_status rx_status;
  1621. os_memset(&rx_status, 0, sizeof(rx_status));
  1622. rx_status.freq = data->mlme_rx.freq;
  1623. rx_status.channel = data->mlme_rx.channel;
  1624. rx_status.ssi = data->mlme_rx.ssi;
  1625. ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
  1626. &rx_status);
  1627. break;
  1628. }
  1629. #endif /* CONFIG_CLIENT_MLME */
  1630. case EVENT_EAPOL_RX:
  1631. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  1632. data->eapol_rx.data,
  1633. data->eapol_rx.data_len);
  1634. break;
  1635. case EVENT_SIGNAL_CHANGE:
  1636. bgscan_notify_signal_change(
  1637. wpa_s, data->signal_change.above_threshold,
  1638. data->signal_change.current_signal,
  1639. data->signal_change.current_noise,
  1640. data->signal_change.current_txrate);
  1641. break;
  1642. case EVENT_INTERFACE_ENABLED:
  1643. wpa_printf(MSG_DEBUG, "Interface was enabled");
  1644. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  1645. #ifdef CONFIG_AP
  1646. if (!wpa_s->ap_iface) {
  1647. wpa_supplicant_set_state(wpa_s,
  1648. WPA_DISCONNECTED);
  1649. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1650. } else
  1651. wpa_supplicant_set_state(wpa_s,
  1652. WPA_COMPLETED);
  1653. #else /* CONFIG_AP */
  1654. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1655. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1656. #endif /* CONFIG_AP */
  1657. }
  1658. break;
  1659. case EVENT_INTERFACE_DISABLED:
  1660. wpa_printf(MSG_DEBUG, "Interface was disabled");
  1661. wpa_supplicant_mark_disassoc(wpa_s);
  1662. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  1663. break;
  1664. case EVENT_CHANNEL_LIST_CHANGED:
  1665. if (wpa_s->drv_priv == NULL)
  1666. break; /* Ignore event during drv initialization */
  1667. #ifdef CONFIG_P2P
  1668. wpas_p2p_update_channel_list(wpa_s);
  1669. #endif /* CONFIG_P2P */
  1670. break;
  1671. case EVENT_INTERFACE_UNAVAILABLE:
  1672. #ifdef CONFIG_P2P
  1673. wpas_p2p_interface_unavailable(wpa_s);
  1674. #endif /* CONFIG_P2P */
  1675. break;
  1676. case EVENT_BEST_CHANNEL:
  1677. wpa_printf(MSG_DEBUG, "Best channel event received (%d %d %d)",
  1678. data->best_chan.freq_24, data->best_chan.freq_5,
  1679. data->best_chan.freq_overall);
  1680. wpa_s->best_24_freq = data->best_chan.freq_24;
  1681. wpa_s->best_5_freq = data->best_chan.freq_5;
  1682. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  1683. #ifdef CONFIG_P2P
  1684. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  1685. data->best_chan.freq_5,
  1686. data->best_chan.freq_overall);
  1687. #endif /* CONFIG_P2P */
  1688. break;
  1689. default:
  1690. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1691. break;
  1692. }
  1693. }