events.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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 ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
  357. wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
  358. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  359. return 0;
  360. }
  361. /* Allow in non-WPA configuration */
  362. return 1;
  363. }
  364. static int freq_allowed(int *freqs, int freq)
  365. {
  366. int i;
  367. if (freqs == NULL)
  368. return 1;
  369. for (i = 0; freqs[i]; i++)
  370. if (freqs[i] == freq)
  371. return 1;
  372. return 0;
  373. }
  374. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  375. int i, struct wpa_scan_res *bss,
  376. struct wpa_ssid *group)
  377. {
  378. const u8 *ssid_;
  379. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  380. int wpa;
  381. struct wpa_blacklist *e;
  382. const u8 *ie;
  383. struct wpa_ssid *ssid;
  384. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  385. ssid_ = ie ? ie + 2 : (u8 *) "";
  386. ssid_len = ie ? ie[1] : 0;
  387. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  388. wpa_ie_len = ie ? ie[1] : 0;
  389. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  390. rsn_ie_len = ie ? ie[1] : 0;
  391. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  392. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
  393. i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
  394. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  395. wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
  396. " wps" : "");
  397. e = wpa_blacklist_get(wpa_s, bss->bssid);
  398. if (e) {
  399. int limit = 1;
  400. if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
  401. /*
  402. * When only a single network is enabled, we can
  403. * trigger blacklisting on the first failure. This
  404. * should not be done with multiple enabled networks to
  405. * avoid getting forced to move into a worse ESS on
  406. * single error if there are no other BSSes of the
  407. * current ESS.
  408. */
  409. limit = 0;
  410. }
  411. if (e->count > limit) {
  412. wpa_printf(MSG_DEBUG, " skip - blacklisted "
  413. "(count=%d limit=%d)", e->count, limit);
  414. return 0;
  415. }
  416. }
  417. if (ssid_len == 0) {
  418. wpa_printf(MSG_DEBUG, " skip - SSID not known");
  419. return 0;
  420. }
  421. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  422. for (ssid = group; ssid; ssid = ssid->pnext) {
  423. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  424. if (ssid->disabled) {
  425. wpa_printf(MSG_DEBUG, " skip - disabled");
  426. continue;
  427. }
  428. #ifdef CONFIG_WPS
  429. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  430. wpa_printf(MSG_DEBUG, " skip - blacklisted (WPS)");
  431. continue;
  432. }
  433. if (wpa && ssid->ssid_len == 0 &&
  434. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  435. check_ssid = 0;
  436. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  437. /* Only allow wildcard SSID match if an AP
  438. * advertises active WPS operation that matches
  439. * with our mode. */
  440. check_ssid = 1;
  441. if (ssid->ssid_len == 0 &&
  442. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  443. check_ssid = 0;
  444. }
  445. #endif /* CONFIG_WPS */
  446. if (check_ssid &&
  447. (ssid_len != ssid->ssid_len ||
  448. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  449. wpa_printf(MSG_DEBUG, " skip - SSID mismatch");
  450. continue;
  451. }
  452. if (ssid->bssid_set &&
  453. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  454. wpa_printf(MSG_DEBUG, " skip - BSSID mismatch");
  455. continue;
  456. }
  457. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  458. continue;
  459. if (!wpa &&
  460. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  461. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  462. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  463. wpa_printf(MSG_DEBUG, " skip - non-WPA network not "
  464. "allowed");
  465. continue;
  466. }
  467. if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
  468. wpa_printf(MSG_DEBUG, " skip - privacy mismatch");
  469. continue;
  470. }
  471. if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
  472. wpa_printf(MSG_DEBUG, " skip - IBSS (adhoc) "
  473. "network");
  474. continue;
  475. }
  476. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  477. wpa_printf(MSG_DEBUG, " skip - frequency not "
  478. "allowed");
  479. continue;
  480. }
  481. #ifdef CONFIG_P2P
  482. /*
  483. * TODO: skip the AP if its P2P IE has Group Formation
  484. * bit set in the P2P Group Capability Bitmap and we
  485. * are not in Group Formation with that device.
  486. */
  487. #endif /* CONFIG_P2P */
  488. /* Matching configuration found */
  489. return ssid;
  490. }
  491. /* No matching configuration found */
  492. return 0;
  493. }
  494. static struct wpa_bss *
  495. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  496. struct wpa_scan_results *scan_res,
  497. struct wpa_ssid *group,
  498. struct wpa_ssid **selected_ssid)
  499. {
  500. size_t i;
  501. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  502. group->priority);
  503. for (i = 0; i < scan_res->num; i++) {
  504. struct wpa_scan_res *bss = scan_res->res[i];
  505. const u8 *ie, *ssid;
  506. u8 ssid_len;
  507. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
  508. if (!*selected_ssid)
  509. continue;
  510. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  511. ssid = ie ? ie + 2 : (u8 *) "";
  512. ssid_len = ie ? ie[1] : 0;
  513. wpa_printf(MSG_DEBUG, " selected BSS " MACSTR " ssid='%s'",
  514. MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  515. return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
  516. }
  517. return NULL;
  518. }
  519. static struct wpa_bss *
  520. wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  521. struct wpa_scan_results *scan_res,
  522. struct wpa_ssid **selected_ssid)
  523. {
  524. struct wpa_bss *selected = NULL;
  525. int prio;
  526. while (selected == NULL) {
  527. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  528. selected = wpa_supplicant_select_bss(
  529. wpa_s, scan_res, wpa_s->conf->pssid[prio],
  530. selected_ssid);
  531. if (selected)
  532. break;
  533. }
  534. if (selected == NULL && wpa_s->blacklist) {
  535. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  536. "and try again");
  537. wpa_blacklist_clear(wpa_s);
  538. wpa_s->blacklist_cleared++;
  539. } else if (selected == NULL)
  540. break;
  541. }
  542. return selected;
  543. }
  544. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  545. int timeout_sec, int timeout_usec)
  546. {
  547. if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
  548. /*
  549. * No networks are enabled; short-circuit request so
  550. * we don't wait timeout seconds before transitioning
  551. * to INACTIVE state.
  552. */
  553. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  554. return;
  555. }
  556. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  557. }
  558. void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  559. struct wpa_bss *selected,
  560. struct wpa_ssid *ssid)
  561. {
  562. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  563. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  564. "PBC session overlap");
  565. #ifdef CONFIG_P2P
  566. if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
  567. return;
  568. #endif /* CONFIG_P2P */
  569. #ifdef CONFIG_WPS
  570. wpas_wps_cancel(wpa_s);
  571. #endif /* CONFIG_WPS */
  572. return;
  573. }
  574. /*
  575. * Do not trigger new association unless the BSSID has changed or if
  576. * reassociation is requested. If we are in process of associating with
  577. * the selected BSSID, do not trigger new attempt.
  578. */
  579. if (wpa_s->reassociate ||
  580. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  581. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  582. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  583. 0))) {
  584. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  585. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  586. return;
  587. }
  588. wpa_supplicant_associate(wpa_s, selected, ssid);
  589. } else {
  590. wpa_printf(MSG_DEBUG, "Already associated with the selected "
  591. "AP");
  592. }
  593. }
  594. static struct wpa_ssid *
  595. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  596. {
  597. int prio;
  598. struct wpa_ssid *ssid;
  599. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  600. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  601. {
  602. if (ssid->disabled)
  603. continue;
  604. if (ssid->mode == IEEE80211_MODE_IBSS ||
  605. ssid->mode == IEEE80211_MODE_AP)
  606. return ssid;
  607. }
  608. }
  609. return NULL;
  610. }
  611. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  612. * on BSS added and BSS changed events */
  613. static void wpa_supplicant_rsn_preauth_scan_results(
  614. struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
  615. {
  616. int i;
  617. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  618. return;
  619. for (i = scan_res->num - 1; i >= 0; i--) {
  620. const u8 *ssid, *rsn;
  621. struct wpa_scan_res *r;
  622. r = scan_res->res[i];
  623. ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
  624. if (ssid == NULL)
  625. continue;
  626. rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
  627. if (rsn == NULL)
  628. continue;
  629. rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
  630. }
  631. }
  632. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  633. struct wpa_bss *selected,
  634. struct wpa_ssid *ssid,
  635. struct wpa_scan_results *scan_res)
  636. {
  637. size_t i;
  638. struct wpa_scan_res *current_bss = NULL;
  639. int min_diff;
  640. if (wpa_s->reassociate)
  641. return 1; /* explicit request to reassociate */
  642. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  643. return 1; /* we are not associated; continue */
  644. if (wpa_s->current_ssid == NULL)
  645. return 1; /* unknown current SSID */
  646. if (wpa_s->current_ssid != ssid)
  647. return 1; /* different network block */
  648. for (i = 0; i < scan_res->num; i++) {
  649. struct wpa_scan_res *res = scan_res->res[i];
  650. const u8 *ie;
  651. if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  652. continue;
  653. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  654. if (ie == NULL)
  655. continue;
  656. if (ie[1] != wpa_s->current_ssid->ssid_len ||
  657. os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
  658. continue;
  659. current_bss = res;
  660. break;
  661. }
  662. if (!current_bss)
  663. return 1; /* current BSS not seen in scan results */
  664. wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
  665. wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  666. MAC2STR(current_bss->bssid), current_bss->level);
  667. wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  668. MAC2STR(selected->bssid), selected->level);
  669. if (wpa_s->current_ssid->bssid_set &&
  670. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  671. 0) {
  672. wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
  673. "preferred BSSID");
  674. return 1;
  675. }
  676. min_diff = 2;
  677. if (current_bss->level < 0) {
  678. if (current_bss->level < -85)
  679. min_diff = 1;
  680. else if (current_bss->level < -80)
  681. min_diff = 2;
  682. else if (current_bss->level < -75)
  683. min_diff = 3;
  684. else if (current_bss->level < -70)
  685. min_diff = 4;
  686. else
  687. min_diff = 5;
  688. }
  689. if (abs(current_bss->level - selected->level) < min_diff) {
  690. wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
  691. "signal level");
  692. return 0;
  693. }
  694. return 1;
  695. }
  696. static void _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  697. union wpa_event_data *data)
  698. {
  699. struct wpa_bss *selected;
  700. struct wpa_ssid *ssid = NULL;
  701. struct wpa_scan_results *scan_res;
  702. int ap = 0;
  703. #ifdef CONFIG_AP
  704. if (wpa_s->ap_iface)
  705. ap = 1;
  706. #endif /* CONFIG_AP */
  707. wpa_supplicant_notify_scanning(wpa_s, 0);
  708. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  709. data ? &data->scan_info :
  710. NULL, 1);
  711. if (scan_res == NULL) {
  712. if (wpa_s->conf->ap_scan == 2 || ap)
  713. return;
  714. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  715. "scanning again");
  716. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  717. return;
  718. }
  719. #ifndef CONFIG_NO_RANDOM_POOL
  720. size_t i, num;
  721. num = scan_res->num;
  722. if (num > 10)
  723. num = 10;
  724. for (i = 0; i < num; i++) {
  725. u8 buf[5];
  726. struct wpa_scan_res *res = scan_res->res[i];
  727. buf[0] = res->bssid[5];
  728. buf[1] = res->qual & 0xff;
  729. buf[2] = res->noise & 0xff;
  730. buf[3] = res->level & 0xff;
  731. buf[4] = res->tsf & 0xff;
  732. random_add_randomness(buf, sizeof(buf));
  733. }
  734. #endif /* CONFIG_NO_RANDOM_POOL */
  735. if (wpa_s->scan_res_handler) {
  736. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  737. struct wpa_scan_results *scan_res);
  738. scan_res_handler = wpa_s->scan_res_handler;
  739. wpa_s->scan_res_handler = NULL;
  740. scan_res_handler(wpa_s, scan_res);
  741. wpa_scan_results_free(scan_res);
  742. return;
  743. }
  744. if (ap) {
  745. wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
  746. wpa_scan_results_free(scan_res);
  747. return;
  748. }
  749. wpa_printf(MSG_DEBUG, "%s: New scan results available", wpa_s->ifname);
  750. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  751. wpas_notify_scan_results(wpa_s);
  752. wpas_notify_scan_done(wpa_s, 1);
  753. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  754. wpa_scan_results_free(scan_res);
  755. return;
  756. }
  757. if (wpa_s->disconnected) {
  758. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  759. wpa_scan_results_free(scan_res);
  760. return;
  761. }
  762. if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
  763. wpa_scan_results_free(scan_res);
  764. return;
  765. }
  766. wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
  767. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  768. if (selected) {
  769. int skip;
  770. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  771. scan_res);
  772. wpa_scan_results_free(scan_res);
  773. if (skip)
  774. return;
  775. wpa_supplicant_connect(wpa_s, selected, ssid);
  776. } else {
  777. wpa_scan_results_free(scan_res);
  778. wpa_printf(MSG_DEBUG, "No suitable network found");
  779. ssid = wpa_supplicant_pick_new_network(wpa_s);
  780. if (ssid) {
  781. wpa_printf(MSG_DEBUG, "Setup a new network");
  782. wpa_supplicant_associate(wpa_s, NULL, ssid);
  783. } else {
  784. int timeout_sec = 5;
  785. int timeout_usec = 0;
  786. #ifdef CONFIG_P2P
  787. if (wpa_s->p2p_in_provisioning) {
  788. /*
  789. * Use shorter wait during P2P Provisioning
  790. * state to speed up group formation.
  791. */
  792. timeout_sec = 0;
  793. timeout_usec = 250000;
  794. }
  795. #endif /* CONFIG_P2P */
  796. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  797. timeout_usec);
  798. }
  799. }
  800. }
  801. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  802. union wpa_event_data *data)
  803. {
  804. const char *rn, *rn2;
  805. struct wpa_supplicant *ifs;
  806. _wpa_supplicant_event_scan_results(wpa_s, data);
  807. /*
  808. * Check other interfaces to see if they have the same radio-name. If
  809. * so, they get updated with this same scan info.
  810. */
  811. if (!wpa_s->driver->get_radio_name)
  812. return;
  813. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  814. if (rn == NULL || rn[0] == '\0')
  815. return;
  816. wpa_printf(MSG_DEBUG, "%s: Checking for other virtual interfaces "
  817. "sharing same radio (%s) in event_scan_results",
  818. wpa_s->ifname, rn);
  819. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  820. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  821. continue;
  822. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  823. if (rn2 && os_strcmp(rn, rn2) == 0) {
  824. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  825. "sibling", ifs->ifname);
  826. _wpa_supplicant_event_scan_results(ifs, data);
  827. }
  828. }
  829. }
  830. #endif /* CONFIG_NO_SCAN_PROCESSING */
  831. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  832. union wpa_event_data *data)
  833. {
  834. int l, len, found = 0, wpa_found, rsn_found;
  835. const u8 *p;
  836. wpa_printf(MSG_DEBUG, "Association info event");
  837. if (data->assoc_info.req_ies)
  838. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  839. data->assoc_info.req_ies_len);
  840. if (data->assoc_info.resp_ies)
  841. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  842. data->assoc_info.resp_ies_len);
  843. if (data->assoc_info.beacon_ies)
  844. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  845. data->assoc_info.beacon_ies,
  846. data->assoc_info.beacon_ies_len);
  847. if (data->assoc_info.freq)
  848. wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
  849. p = data->assoc_info.req_ies;
  850. l = data->assoc_info.req_ies_len;
  851. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  852. while (p && l >= 2) {
  853. len = p[1] + 2;
  854. if (len > l) {
  855. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  856. p, l);
  857. break;
  858. }
  859. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  860. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  861. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  862. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  863. break;
  864. found = 1;
  865. wpa_find_assoc_pmkid(wpa_s);
  866. break;
  867. }
  868. l -= len;
  869. p += len;
  870. }
  871. if (!found && data->assoc_info.req_ies)
  872. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  873. #ifdef CONFIG_IEEE80211R
  874. #ifdef CONFIG_SME
  875. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  876. u8 bssid[ETH_ALEN];
  877. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  878. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  879. data->assoc_info.resp_ies,
  880. data->assoc_info.resp_ies_len,
  881. bssid) < 0) {
  882. wpa_printf(MSG_DEBUG, "FT: Validation of "
  883. "Reassociation Response failed");
  884. wpa_supplicant_deauthenticate(
  885. wpa_s, WLAN_REASON_INVALID_IE);
  886. return -1;
  887. }
  888. }
  889. p = data->assoc_info.resp_ies;
  890. l = data->assoc_info.resp_ies_len;
  891. #ifdef CONFIG_WPS_STRICT
  892. if (wpa_s->current_ssid &&
  893. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  894. struct wpabuf *wps;
  895. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  896. if (wps == NULL) {
  897. wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
  898. "WPS IE in (Re)Association Response");
  899. return -1;
  900. }
  901. if (wps_validate_assoc_resp(wps) < 0) {
  902. wpabuf_free(wps);
  903. wpa_supplicant_deauthenticate(
  904. wpa_s, WLAN_REASON_INVALID_IE);
  905. return -1;
  906. }
  907. wpabuf_free(wps);
  908. }
  909. #endif /* CONFIG_WPS_STRICT */
  910. /* Go through the IEs and make a copy of the MDIE, if present. */
  911. while (p && l >= 2) {
  912. len = p[1] + 2;
  913. if (len > l) {
  914. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  915. p, l);
  916. break;
  917. }
  918. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  919. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  920. wpa_s->sme.ft_used = 1;
  921. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  922. MOBILITY_DOMAIN_ID_LEN);
  923. break;
  924. }
  925. l -= len;
  926. p += len;
  927. }
  928. #endif /* CONFIG_SME */
  929. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  930. data->assoc_info.resp_ies_len);
  931. #endif /* CONFIG_IEEE80211R */
  932. /* WPA/RSN IE from Beacon/ProbeResp */
  933. p = data->assoc_info.beacon_ies;
  934. l = data->assoc_info.beacon_ies_len;
  935. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  936. */
  937. wpa_found = rsn_found = 0;
  938. while (p && l >= 2) {
  939. len = p[1] + 2;
  940. if (len > l) {
  941. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  942. p, l);
  943. break;
  944. }
  945. if (!wpa_found &&
  946. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  947. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  948. wpa_found = 1;
  949. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  950. }
  951. if (!rsn_found &&
  952. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  953. rsn_found = 1;
  954. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  955. }
  956. l -= len;
  957. p += len;
  958. }
  959. if (!wpa_found && data->assoc_info.beacon_ies)
  960. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  961. if (!rsn_found && data->assoc_info.beacon_ies)
  962. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  963. if (wpa_found || rsn_found)
  964. wpa_s->ap_ies_from_associnfo = 1;
  965. wpa_s->assoc_freq = data->assoc_info.freq;
  966. return 0;
  967. }
  968. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  969. union wpa_event_data *data)
  970. {
  971. u8 bssid[ETH_ALEN];
  972. int ft_completed;
  973. int bssid_changed;
  974. struct wpa_driver_capa capa;
  975. #ifdef CONFIG_AP
  976. if (wpa_s->ap_iface) {
  977. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  978. data->assoc_info.addr,
  979. data->assoc_info.req_ies,
  980. data->assoc_info.req_ies_len);
  981. return;
  982. }
  983. #endif /* CONFIG_AP */
  984. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  985. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  986. return;
  987. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  988. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  989. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  990. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  991. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  992. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  993. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  994. MACSTR, MAC2STR(bssid));
  995. random_add_randomness(bssid, ETH_ALEN);
  996. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  997. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  998. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  999. if (bssid_changed)
  1000. wpas_notify_bssid_changed(wpa_s);
  1001. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  1002. wpa_clear_keys(wpa_s, bssid);
  1003. }
  1004. if (wpa_supplicant_select_config(wpa_s) < 0) {
  1005. wpa_supplicant_disassociate(
  1006. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1007. return;
  1008. }
  1009. if (wpa_s->current_ssid) {
  1010. struct wpa_bss *bss = NULL;
  1011. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1012. if (ssid->ssid_len > 0)
  1013. bss = wpa_bss_get(wpa_s, bssid,
  1014. ssid->ssid, ssid->ssid_len);
  1015. if (!bss)
  1016. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1017. if (bss)
  1018. wpa_s->current_bss = bss;
  1019. }
  1020. }
  1021. #ifdef CONFIG_SME
  1022. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1023. wpa_s->sme.prev_bssid_set = 1;
  1024. #endif /* CONFIG_SME */
  1025. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1026. if (wpa_s->current_ssid) {
  1027. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1028. * initialized before association, but for other modes,
  1029. * initialize PC/SC here, if the current configuration needs
  1030. * smartcard or SIM/USIM. */
  1031. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1032. }
  1033. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1034. if (wpa_s->l2)
  1035. l2_packet_notify_auth_start(wpa_s->l2);
  1036. /*
  1037. * Set portEnabled first to FALSE in order to get EAP state machine out
  1038. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1039. * state machine may transit to AUTHENTICATING state based on obsolete
  1040. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1041. * AUTHENTICATED without ever giving chance to EAP state machine to
  1042. * reset the state.
  1043. */
  1044. if (!ft_completed) {
  1045. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1046. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1047. }
  1048. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1049. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1050. /* 802.1X::portControl = Auto */
  1051. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1052. wpa_s->eapol_received = 0;
  1053. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1054. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1055. (wpa_s->current_ssid &&
  1056. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1057. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1058. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1059. } else if (!ft_completed) {
  1060. /* Timeout for receiving the first EAPOL packet */
  1061. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1062. }
  1063. wpa_supplicant_cancel_scan(wpa_s);
  1064. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1065. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1066. /*
  1067. * We are done; the driver will take care of RSN 4-way
  1068. * handshake.
  1069. */
  1070. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1071. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1072. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1073. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1074. }
  1075. if (wpa_s->pending_eapol_rx) {
  1076. struct os_time now, age;
  1077. os_get_time(&now);
  1078. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1079. if (age.sec == 0 && age.usec < 100000 &&
  1080. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1081. 0) {
  1082. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  1083. "that was received just before association "
  1084. "notification");
  1085. wpa_supplicant_rx_eapol(
  1086. wpa_s, wpa_s->pending_eapol_rx_src,
  1087. wpabuf_head(wpa_s->pending_eapol_rx),
  1088. wpabuf_len(wpa_s->pending_eapol_rx));
  1089. }
  1090. wpabuf_free(wpa_s->pending_eapol_rx);
  1091. wpa_s->pending_eapol_rx = NULL;
  1092. }
  1093. #ifdef CONFIG_BGSCAN
  1094. if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
  1095. bgscan_deinit(wpa_s);
  1096. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  1097. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  1098. wpa_printf(MSG_DEBUG, "Failed to initialize "
  1099. "bgscan");
  1100. /*
  1101. * Live without bgscan; it is only used as a
  1102. * roaming optimization, so the initial
  1103. * connection is not affected.
  1104. */
  1105. } else
  1106. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  1107. } else
  1108. wpa_s->bgscan_ssid = NULL;
  1109. }
  1110. #endif /* CONFIG_BGSCAN */
  1111. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1112. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1113. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1114. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1115. /* Set static WEP keys again */
  1116. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1117. }
  1118. #ifdef CONFIG_IBSS_RSN
  1119. if (wpa_s->current_ssid &&
  1120. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  1121. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1122. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE)
  1123. ibss_rsn_connected(wpa_s->ibss_rsn);
  1124. #endif /* CONFIG_IBSS_RSN */
  1125. }
  1126. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1127. u16 reason_code)
  1128. {
  1129. const u8 *bssid;
  1130. #ifdef CONFIG_SME
  1131. int authenticating;
  1132. u8 prev_pending_bssid[ETH_ALEN];
  1133. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1134. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1135. #endif /* CONFIG_SME */
  1136. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1137. /*
  1138. * At least Host AP driver and a Prism3 card seemed to be
  1139. * generating streams of disconnected events when configuring
  1140. * IBSS for WPA-None. Ignore them for now.
  1141. */
  1142. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  1143. "IBSS/WPA-None mode");
  1144. return;
  1145. }
  1146. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1147. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1148. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1149. "pre-shared key may be incorrect");
  1150. }
  1151. if (!wpa_s->auto_reconnect_disabled ||
  1152. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  1153. wpa_printf(MSG_DEBUG, "WPA: Auto connect enabled: try to "
  1154. "reconnect (wps=%d)",
  1155. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
  1156. if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1157. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1158. } else {
  1159. wpa_printf(MSG_DEBUG, "WPA: Auto connect disabled: do not try "
  1160. "to re-connect");
  1161. wpa_s->reassociate = 0;
  1162. wpa_s->disconnected = 1;
  1163. }
  1164. bssid = wpa_s->bssid;
  1165. if (is_zero_ether_addr(bssid))
  1166. bssid = wpa_s->pending_bssid;
  1167. wpas_connection_failed(wpa_s, bssid);
  1168. wpa_sm_notify_disassoc(wpa_s->wpa);
  1169. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1170. " reason=%d",
  1171. MAC2STR(bssid), reason_code);
  1172. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1173. wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
  1174. wpa_s->keys_cleared = 0;
  1175. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1176. }
  1177. wpa_supplicant_mark_disassoc(wpa_s);
  1178. bgscan_deinit(wpa_s);
  1179. wpa_s->bgscan_ssid = NULL;
  1180. #ifdef CONFIG_SME
  1181. if (authenticating &&
  1182. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1183. /*
  1184. * mac80211-workaround to force deauth on failed auth cmd,
  1185. * requires us to remain in authenticating state to allow the
  1186. * second authentication attempt to be continued properly.
  1187. */
  1188. wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
  1189. "proceed after disconnection event");
  1190. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1191. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1192. }
  1193. #endif /* CONFIG_SME */
  1194. }
  1195. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1196. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  1197. void *sock_ctx)
  1198. {
  1199. struct wpa_supplicant *wpa_s = eloop_ctx;
  1200. if (!wpa_s->pending_mic_error_report)
  1201. return;
  1202. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  1203. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1204. wpa_s->pending_mic_error_report = 0;
  1205. }
  1206. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1207. static void
  1208. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1209. union wpa_event_data *data)
  1210. {
  1211. int pairwise;
  1212. struct os_time t;
  1213. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1214. pairwise = (data && data->michael_mic_failure.unicast);
  1215. os_get_time(&t);
  1216. if ((wpa_s->last_michael_mic_error &&
  1217. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1218. wpa_s->pending_mic_error_report) {
  1219. if (wpa_s->pending_mic_error_report) {
  1220. /*
  1221. * Send the pending MIC error report immediately since
  1222. * we are going to start countermeasures and AP better
  1223. * do the same.
  1224. */
  1225. wpa_sm_key_request(wpa_s->wpa, 1,
  1226. wpa_s->pending_mic_error_pairwise);
  1227. }
  1228. /* Send the new MIC error report immediately since we are going
  1229. * to start countermeasures and AP better do the same.
  1230. */
  1231. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1232. /* initialize countermeasures */
  1233. wpa_s->countermeasures = 1;
  1234. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1235. /*
  1236. * Need to wait for completion of request frame. We do not get
  1237. * any callback for the message completion, so just wait a
  1238. * short while and hope for the best. */
  1239. os_sleep(0, 10000);
  1240. wpa_drv_set_countermeasures(wpa_s, 1);
  1241. wpa_supplicant_deauthenticate(wpa_s,
  1242. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1243. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1244. wpa_s, NULL);
  1245. eloop_register_timeout(60, 0,
  1246. wpa_supplicant_stop_countermeasures,
  1247. wpa_s, NULL);
  1248. /* TODO: mark the AP rejected for 60 second. STA is
  1249. * allowed to associate with another AP.. */
  1250. } else {
  1251. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1252. if (wpa_s->mic_errors_seen) {
  1253. /*
  1254. * Reduce the effectiveness of Michael MIC error
  1255. * reports as a means for attacking against TKIP if
  1256. * more than one MIC failure is noticed with the same
  1257. * PTK. We delay the transmission of the reports by a
  1258. * random time between 0 and 60 seconds in order to
  1259. * force the attacker wait 60 seconds before getting
  1260. * the information on whether a frame resulted in a MIC
  1261. * failure.
  1262. */
  1263. u8 rval[4];
  1264. int sec;
  1265. if (os_get_random(rval, sizeof(rval)) < 0)
  1266. sec = os_random() % 60;
  1267. else
  1268. sec = WPA_GET_BE32(rval) % 60;
  1269. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  1270. "seconds", sec);
  1271. wpa_s->pending_mic_error_report = 1;
  1272. wpa_s->pending_mic_error_pairwise = pairwise;
  1273. eloop_cancel_timeout(
  1274. wpa_supplicant_delayed_mic_error_report,
  1275. wpa_s, NULL);
  1276. eloop_register_timeout(
  1277. sec, os_random() % 1000000,
  1278. wpa_supplicant_delayed_mic_error_report,
  1279. wpa_s, NULL);
  1280. } else {
  1281. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1282. }
  1283. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1284. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1285. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1286. }
  1287. wpa_s->last_michael_mic_error = t.sec;
  1288. wpa_s->mic_errors_seen++;
  1289. }
  1290. #ifdef CONFIG_TERMINATE_ONLASTIF
  1291. static int any_interfaces(struct wpa_supplicant *head)
  1292. {
  1293. struct wpa_supplicant *wpa_s;
  1294. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1295. if (!wpa_s->interface_removed)
  1296. return 1;
  1297. return 0;
  1298. }
  1299. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1300. static void
  1301. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1302. union wpa_event_data *data)
  1303. {
  1304. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1305. return;
  1306. switch (data->interface_status.ievent) {
  1307. case EVENT_INTERFACE_ADDED:
  1308. if (!wpa_s->interface_removed)
  1309. break;
  1310. wpa_s->interface_removed = 0;
  1311. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  1312. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1313. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  1314. "after interface was added.");
  1315. }
  1316. break;
  1317. case EVENT_INTERFACE_REMOVED:
  1318. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1319. wpa_s->interface_removed = 1;
  1320. wpa_supplicant_mark_disassoc(wpa_s);
  1321. l2_packet_deinit(wpa_s->l2);
  1322. wpa_s->l2 = NULL;
  1323. #ifdef CONFIG_TERMINATE_ONLASTIF
  1324. /* check if last interface */
  1325. if (!any_interfaces(wpa_s->global->ifaces))
  1326. eloop_terminate();
  1327. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1328. break;
  1329. }
  1330. }
  1331. #ifdef CONFIG_PEERKEY
  1332. static void
  1333. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1334. union wpa_event_data *data)
  1335. {
  1336. if (data == NULL)
  1337. return;
  1338. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1339. }
  1340. #endif /* CONFIG_PEERKEY */
  1341. #ifdef CONFIG_IEEE80211R
  1342. static void
  1343. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1344. union wpa_event_data *data)
  1345. {
  1346. if (data == NULL)
  1347. return;
  1348. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1349. data->ft_ies.ies_len,
  1350. data->ft_ies.ft_action,
  1351. data->ft_ies.target_ap,
  1352. data->ft_ies.ric_ies,
  1353. data->ft_ies.ric_ies_len) < 0) {
  1354. /* TODO: prevent MLME/driver from trying to associate? */
  1355. }
  1356. }
  1357. #endif /* CONFIG_IEEE80211R */
  1358. #ifdef CONFIG_IBSS_RSN
  1359. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1360. union wpa_event_data *data)
  1361. {
  1362. struct wpa_ssid *ssid;
  1363. if (data == NULL)
  1364. return;
  1365. ssid = wpa_s->current_ssid;
  1366. if (ssid == NULL)
  1367. return;
  1368. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  1369. return;
  1370. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1371. }
  1372. #endif /* CONFIG_IBSS_RSN */
  1373. #ifdef CONFIG_IEEE80211R
  1374. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  1375. size_t len)
  1376. {
  1377. const u8 *sta_addr, *target_ap_addr;
  1378. u16 status;
  1379. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  1380. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1381. return; /* only SME case supported for now */
  1382. if (len < 1 + 2 * ETH_ALEN + 2)
  1383. return;
  1384. if (data[0] != 2)
  1385. return; /* Only FT Action Response is supported for now */
  1386. sta_addr = data + 1;
  1387. target_ap_addr = data + 1 + ETH_ALEN;
  1388. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  1389. wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
  1390. " TargetAP " MACSTR " status %u",
  1391. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1392. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1393. wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  1394. " in FT Action Response", MAC2STR(sta_addr));
  1395. return;
  1396. }
  1397. if (status) {
  1398. wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
  1399. "failure (status code %d)", status);
  1400. /* TODO: report error to FT code(?) */
  1401. return;
  1402. }
  1403. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  1404. len - (1 + 2 * ETH_ALEN + 2), 1,
  1405. target_ap_addr, NULL, 0) < 0)
  1406. return;
  1407. #ifdef CONFIG_SME
  1408. {
  1409. struct wpa_bss *bss;
  1410. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  1411. if (bss)
  1412. wpa_s->sme.freq = bss->freq;
  1413. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  1414. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  1415. WLAN_AUTH_FT);
  1416. }
  1417. #endif /* CONFIG_SME */
  1418. }
  1419. #endif /* CONFIG_IEEE80211R */
  1420. static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
  1421. struct unprot_deauth *e)
  1422. {
  1423. #ifdef CONFIG_IEEE80211W
  1424. wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
  1425. "dropped: " MACSTR " -> " MACSTR
  1426. " (reason code %u)",
  1427. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1428. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1429. #endif /* CONFIG_IEEE80211W */
  1430. }
  1431. static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
  1432. struct unprot_disassoc *e)
  1433. {
  1434. #ifdef CONFIG_IEEE80211W
  1435. wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
  1436. "dropped: " MACSTR " -> " MACSTR
  1437. " (reason code %u)",
  1438. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1439. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1440. #endif /* CONFIG_IEEE80211W */
  1441. }
  1442. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1443. union wpa_event_data *data)
  1444. {
  1445. struct wpa_supplicant *wpa_s = ctx;
  1446. u16 reason_code = 0;
  1447. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  1448. event != EVENT_INTERFACE_ENABLED &&
  1449. event != EVENT_INTERFACE_STATUS) {
  1450. wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
  1451. "disabled", event);
  1452. return;
  1453. }
  1454. wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
  1455. event, wpa_s->ifname);
  1456. switch (event) {
  1457. case EVENT_AUTH:
  1458. sme_event_auth(wpa_s, data);
  1459. break;
  1460. case EVENT_ASSOC:
  1461. wpa_supplicant_event_assoc(wpa_s, data);
  1462. break;
  1463. case EVENT_DISASSOC:
  1464. wpa_printf(MSG_DEBUG, "Disassociation notification");
  1465. if (data) {
  1466. wpa_printf(MSG_DEBUG, " * reason %u",
  1467. data->disassoc_info.reason_code);
  1468. if (data->disassoc_info.addr)
  1469. wpa_printf(MSG_DEBUG, " * address " MACSTR,
  1470. MAC2STR(data->disassoc_info.addr));
  1471. }
  1472. #ifdef CONFIG_AP
  1473. if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
  1474. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1475. data->disassoc_info.addr);
  1476. break;
  1477. }
  1478. #endif /* CONFIG_AP */
  1479. if (data) {
  1480. reason_code = data->disassoc_info.reason_code;
  1481. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  1482. data->disassoc_info.ie,
  1483. data->disassoc_info.ie_len);
  1484. #ifdef CONFIG_P2P
  1485. wpas_p2p_disassoc_notif(
  1486. wpa_s, data->disassoc_info.addr, reason_code,
  1487. data->disassoc_info.ie,
  1488. data->disassoc_info.ie_len);
  1489. #endif /* CONFIG_P2P */
  1490. }
  1491. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1492. sme_event_disassoc(wpa_s, data);
  1493. /* fall through */
  1494. case EVENT_DEAUTH:
  1495. if (event == EVENT_DEAUTH) {
  1496. wpa_printf(MSG_DEBUG, "Deauthentication notification");
  1497. if (data) {
  1498. reason_code = data->deauth_info.reason_code;
  1499. wpa_printf(MSG_DEBUG, " * reason %u",
  1500. data->deauth_info.reason_code);
  1501. if (data->deauth_info.addr) {
  1502. wpa_printf(MSG_DEBUG, " * address "
  1503. MACSTR,
  1504. MAC2STR(data->deauth_info.
  1505. addr));
  1506. }
  1507. wpa_hexdump(MSG_DEBUG,
  1508. "Deauthentication frame IE(s)",
  1509. data->deauth_info.ie,
  1510. data->deauth_info.ie_len);
  1511. #ifdef CONFIG_P2P
  1512. wpas_p2p_deauth_notif(
  1513. wpa_s, data->deauth_info.addr,
  1514. reason_code,
  1515. data->deauth_info.ie,
  1516. data->deauth_info.ie_len);
  1517. #endif /* CONFIG_P2P */
  1518. }
  1519. }
  1520. #ifdef CONFIG_AP
  1521. if (wpa_s->ap_iface && data && data->deauth_info.addr) {
  1522. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1523. data->deauth_info.addr);
  1524. break;
  1525. }
  1526. #endif /* CONFIG_AP */
  1527. wpa_supplicant_event_disassoc(wpa_s, reason_code);
  1528. break;
  1529. case EVENT_MICHAEL_MIC_FAILURE:
  1530. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1531. break;
  1532. #ifndef CONFIG_NO_SCAN_PROCESSING
  1533. case EVENT_SCAN_RESULTS:
  1534. wpa_supplicant_event_scan_results(wpa_s, data);
  1535. break;
  1536. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1537. case EVENT_ASSOCINFO:
  1538. wpa_supplicant_event_associnfo(wpa_s, data);
  1539. break;
  1540. case EVENT_INTERFACE_STATUS:
  1541. wpa_supplicant_event_interface_status(wpa_s, data);
  1542. break;
  1543. case EVENT_PMKID_CANDIDATE:
  1544. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1545. break;
  1546. #ifdef CONFIG_PEERKEY
  1547. case EVENT_STKSTART:
  1548. wpa_supplicant_event_stkstart(wpa_s, data);
  1549. break;
  1550. #endif /* CONFIG_PEERKEY */
  1551. #ifdef CONFIG_IEEE80211R
  1552. case EVENT_FT_RESPONSE:
  1553. wpa_supplicant_event_ft_response(wpa_s, data);
  1554. break;
  1555. #endif /* CONFIG_IEEE80211R */
  1556. #ifdef CONFIG_IBSS_RSN
  1557. case EVENT_IBSS_RSN_START:
  1558. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1559. break;
  1560. #endif /* CONFIG_IBSS_RSN */
  1561. case EVENT_ASSOC_REJECT:
  1562. if (data->assoc_reject.bssid)
  1563. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1564. "bssid=" MACSTR " status_code=%u",
  1565. MAC2STR(data->assoc_reject.bssid),
  1566. data->assoc_reject.status_code);
  1567. else
  1568. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1569. "status_code=%u",
  1570. data->assoc_reject.status_code);
  1571. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1572. sme_event_assoc_reject(wpa_s, data);
  1573. break;
  1574. case EVENT_AUTH_TIMED_OUT:
  1575. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1576. sme_event_auth_timed_out(wpa_s, data);
  1577. break;
  1578. case EVENT_ASSOC_TIMED_OUT:
  1579. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1580. sme_event_assoc_timed_out(wpa_s, data);
  1581. break;
  1582. #ifdef CONFIG_AP
  1583. case EVENT_TX_STATUS:
  1584. wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS on %s dst=" MACSTR
  1585. " type=%d stype=%d",
  1586. wpa_s->ifname, MAC2STR(data->tx_status.dst),
  1587. data->tx_status.type, data->tx_status.stype);
  1588. if (wpa_s->ap_iface == NULL) {
  1589. #ifdef CONFIG_P2P
  1590. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1591. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  1592. wpas_send_action_tx_status(
  1593. wpa_s, data->tx_status.dst,
  1594. data->tx_status.data,
  1595. data->tx_status.data_len,
  1596. data->tx_status.ack ?
  1597. P2P_SEND_ACTION_SUCCESS :
  1598. P2P_SEND_ACTION_NO_ACK);
  1599. #endif /* CONFIG_P2P */
  1600. break;
  1601. }
  1602. #ifdef CONFIG_P2P
  1603. wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS pending_dst=" MACSTR,
  1604. MAC2STR(wpa_s->parent->pending_action_dst));
  1605. /*
  1606. * Catch TX status events for Action frames we sent via group
  1607. * interface in GO mode.
  1608. */
  1609. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1610. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  1611. os_memcmp(wpa_s->parent->pending_action_dst,
  1612. data->tx_status.dst, ETH_ALEN) == 0) {
  1613. wpas_send_action_tx_status(
  1614. wpa_s->parent, data->tx_status.dst,
  1615. data->tx_status.data,
  1616. data->tx_status.data_len,
  1617. data->tx_status.ack ?
  1618. P2P_SEND_ACTION_SUCCESS :
  1619. P2P_SEND_ACTION_NO_ACK);
  1620. break;
  1621. }
  1622. #endif /* CONFIG_P2P */
  1623. switch (data->tx_status.type) {
  1624. case WLAN_FC_TYPE_MGMT:
  1625. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1626. data->tx_status.data_len,
  1627. data->tx_status.stype,
  1628. data->tx_status.ack);
  1629. break;
  1630. case WLAN_FC_TYPE_DATA:
  1631. ap_tx_status(wpa_s, data->tx_status.dst,
  1632. data->tx_status.data,
  1633. data->tx_status.data_len,
  1634. data->tx_status.ack);
  1635. break;
  1636. }
  1637. break;
  1638. case EVENT_RX_FROM_UNKNOWN:
  1639. if (wpa_s->ap_iface == NULL)
  1640. break;
  1641. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
  1642. data->rx_from_unknown.len);
  1643. break;
  1644. case EVENT_RX_MGMT:
  1645. if (wpa_s->ap_iface == NULL) {
  1646. #ifdef CONFIG_P2P
  1647. u16 fc, stype;
  1648. const struct ieee80211_mgmt *mgmt;
  1649. mgmt = (const struct ieee80211_mgmt *)
  1650. data->rx_mgmt.frame;
  1651. fc = le_to_host16(mgmt->frame_control);
  1652. stype = WLAN_FC_GET_STYPE(fc);
  1653. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  1654. data->rx_mgmt.frame_len > 24) {
  1655. const u8 *src = mgmt->sa;
  1656. const u8 *ie = mgmt->u.probe_req.variable;
  1657. size_t ie_len = data->rx_mgmt.frame_len -
  1658. (mgmt->u.probe_req.variable -
  1659. data->rx_mgmt.frame);
  1660. wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
  1661. break;
  1662. }
  1663. #endif /* CONFIG_P2P */
  1664. wpa_printf(MSG_DEBUG, "AP: ignore received management "
  1665. "frame in non-AP mode");
  1666. break;
  1667. }
  1668. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1669. break;
  1670. #endif /* CONFIG_AP */
  1671. case EVENT_RX_ACTION:
  1672. wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
  1673. " Category=%u DataLen=%d freq=%d MHz",
  1674. MAC2STR(data->rx_action.sa),
  1675. data->rx_action.category, (int) data->rx_action.len,
  1676. data->rx_action.freq);
  1677. #ifdef CONFIG_IEEE80211R
  1678. if (data->rx_action.category == WLAN_ACTION_FT) {
  1679. ft_rx_action(wpa_s, data->rx_action.data,
  1680. data->rx_action.len);
  1681. break;
  1682. }
  1683. #endif /* CONFIG_IEEE80211R */
  1684. #ifdef CONFIG_IEEE80211W
  1685. #ifdef CONFIG_SME
  1686. if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
  1687. sme_sa_query_rx(wpa_s, data->rx_action.sa,
  1688. data->rx_action.data,
  1689. data->rx_action.len);
  1690. break;
  1691. }
  1692. #endif /* CONFIG_SME */
  1693. #endif /* CONFIG_IEEE80211W */
  1694. #ifdef CONFIG_P2P
  1695. wpas_p2p_rx_action(wpa_s, data->rx_action.da,
  1696. data->rx_action.sa,
  1697. data->rx_action.bssid,
  1698. data->rx_action.category,
  1699. data->rx_action.data,
  1700. data->rx_action.len, data->rx_action.freq);
  1701. #endif /* CONFIG_P2P */
  1702. break;
  1703. #ifdef CONFIG_P2P
  1704. case EVENT_REMAIN_ON_CHANNEL:
  1705. wpas_p2p_remain_on_channel_cb(
  1706. wpa_s, data->remain_on_channel.freq,
  1707. data->remain_on_channel.duration);
  1708. break;
  1709. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  1710. wpas_p2p_cancel_remain_on_channel_cb(
  1711. wpa_s, data->remain_on_channel.freq);
  1712. break;
  1713. case EVENT_RX_PROBE_REQ:
  1714. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  1715. data->rx_probe_req.ie,
  1716. data->rx_probe_req.ie_len);
  1717. break;
  1718. case EVENT_P2P_DEV_FOUND:
  1719. wpas_dev_found(wpa_s, data->p2p_dev_found.addr,
  1720. data->p2p_dev_found.dev_addr,
  1721. data->p2p_dev_found.pri_dev_type,
  1722. data->p2p_dev_found.dev_name,
  1723. data->p2p_dev_found.config_methods,
  1724. data->p2p_dev_found.dev_capab,
  1725. data->p2p_dev_found.group_capab);
  1726. break;
  1727. case EVENT_P2P_GO_NEG_REQ_RX:
  1728. wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
  1729. data->p2p_go_neg_req_rx.dev_passwd_id);
  1730. break;
  1731. case EVENT_P2P_GO_NEG_COMPLETED:
  1732. wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
  1733. break;
  1734. case EVENT_P2P_PROV_DISC_REQUEST:
  1735. wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
  1736. data->p2p_prov_disc_req.config_methods,
  1737. data->p2p_prov_disc_req.dev_addr,
  1738. data->p2p_prov_disc_req.pri_dev_type,
  1739. data->p2p_prov_disc_req.dev_name,
  1740. data->p2p_prov_disc_req.supp_config_methods,
  1741. data->p2p_prov_disc_req.dev_capab,
  1742. data->p2p_prov_disc_req.group_capab);
  1743. break;
  1744. case EVENT_P2P_PROV_DISC_RESPONSE:
  1745. wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
  1746. data->p2p_prov_disc_resp.config_methods);
  1747. break;
  1748. case EVENT_P2P_SD_REQUEST:
  1749. wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
  1750. data->p2p_sd_req.sa,
  1751. data->p2p_sd_req.dialog_token,
  1752. data->p2p_sd_req.update_indic,
  1753. data->p2p_sd_req.tlvs,
  1754. data->p2p_sd_req.tlvs_len);
  1755. break;
  1756. case EVENT_P2P_SD_RESPONSE:
  1757. wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
  1758. data->p2p_sd_resp.update_indic,
  1759. data->p2p_sd_resp.tlvs,
  1760. data->p2p_sd_resp.tlvs_len);
  1761. break;
  1762. #endif /* CONFIG_P2P */
  1763. #ifdef CONFIG_CLIENT_MLME
  1764. case EVENT_MLME_RX: {
  1765. struct ieee80211_rx_status rx_status;
  1766. os_memset(&rx_status, 0, sizeof(rx_status));
  1767. rx_status.freq = data->mlme_rx.freq;
  1768. rx_status.channel = data->mlme_rx.channel;
  1769. rx_status.ssi = data->mlme_rx.ssi;
  1770. ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
  1771. &rx_status);
  1772. break;
  1773. }
  1774. #endif /* CONFIG_CLIENT_MLME */
  1775. case EVENT_EAPOL_RX:
  1776. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  1777. data->eapol_rx.data,
  1778. data->eapol_rx.data_len);
  1779. break;
  1780. case EVENT_SIGNAL_CHANGE:
  1781. bgscan_notify_signal_change(
  1782. wpa_s, data->signal_change.above_threshold,
  1783. data->signal_change.current_signal,
  1784. data->signal_change.current_noise,
  1785. data->signal_change.current_txrate);
  1786. break;
  1787. case EVENT_INTERFACE_ENABLED:
  1788. wpa_printf(MSG_DEBUG, "Interface was enabled");
  1789. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  1790. #ifdef CONFIG_AP
  1791. if (!wpa_s->ap_iface) {
  1792. wpa_supplicant_set_state(wpa_s,
  1793. WPA_DISCONNECTED);
  1794. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1795. } else
  1796. wpa_supplicant_set_state(wpa_s,
  1797. WPA_COMPLETED);
  1798. #else /* CONFIG_AP */
  1799. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1800. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1801. #endif /* CONFIG_AP */
  1802. }
  1803. break;
  1804. case EVENT_INTERFACE_DISABLED:
  1805. wpa_printf(MSG_DEBUG, "Interface was disabled");
  1806. wpa_supplicant_mark_disassoc(wpa_s);
  1807. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  1808. break;
  1809. case EVENT_CHANNEL_LIST_CHANGED:
  1810. if (wpa_s->drv_priv == NULL)
  1811. break; /* Ignore event during drv initialization */
  1812. #ifdef CONFIG_P2P
  1813. wpas_p2p_update_channel_list(wpa_s);
  1814. #endif /* CONFIG_P2P */
  1815. break;
  1816. case EVENT_INTERFACE_UNAVAILABLE:
  1817. #ifdef CONFIG_P2P
  1818. wpas_p2p_interface_unavailable(wpa_s);
  1819. #endif /* CONFIG_P2P */
  1820. break;
  1821. case EVENT_BEST_CHANNEL:
  1822. wpa_printf(MSG_DEBUG, "Best channel event received (%d %d %d)",
  1823. data->best_chan.freq_24, data->best_chan.freq_5,
  1824. data->best_chan.freq_overall);
  1825. wpa_s->best_24_freq = data->best_chan.freq_24;
  1826. wpa_s->best_5_freq = data->best_chan.freq_5;
  1827. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  1828. #ifdef CONFIG_P2P
  1829. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  1830. data->best_chan.freq_5,
  1831. data->best_chan.freq_overall);
  1832. #endif /* CONFIG_P2P */
  1833. break;
  1834. case EVENT_UNPROT_DEAUTH:
  1835. wpa_supplicant_event_unprot_deauth(wpa_s,
  1836. &data->unprot_deauth);
  1837. break;
  1838. case EVENT_UNPROT_DISASSOC:
  1839. wpa_supplicant_event_unprot_disassoc(wpa_s,
  1840. &data->unprot_disassoc);
  1841. break;
  1842. case EVENT_STATION_LOW_ACK:
  1843. #ifdef CONFIG_AP
  1844. if (wpa_s->ap_iface && data)
  1845. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  1846. data->low_ack.addr);
  1847. #endif /* CONFIG_AP */
  1848. break;
  1849. default:
  1850. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1851. break;
  1852. }
  1853. }