events.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2011, 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 "gas_query.h"
  40. #include "p2p_supplicant.h"
  41. #include "bgscan.h"
  42. #include "ap.h"
  43. #include "bss.h"
  44. #include "scan.h"
  45. #include "offchannel.h"
  46. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  47. {
  48. struct wpa_ssid *ssid, *old_ssid;
  49. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  50. return 0;
  51. wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
  52. "information");
  53. ssid = wpa_supplicant_get_ssid(wpa_s);
  54. if (ssid == NULL) {
  55. wpa_msg(wpa_s, MSG_INFO,
  56. "No network configuration found for the current AP");
  57. return -1;
  58. }
  59. if (ssid->disabled) {
  60. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
  61. return -1;
  62. }
  63. wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
  64. "current AP");
  65. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  66. WPA_KEY_MGMT_WPA_NONE |
  67. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
  68. WPA_KEY_MGMT_PSK_SHA256 |
  69. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  70. u8 wpa_ie[80];
  71. size_t wpa_ie_len = sizeof(wpa_ie);
  72. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  73. wpa_ie, &wpa_ie_len);
  74. } else {
  75. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  76. }
  77. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  78. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  79. old_ssid = wpa_s->current_ssid;
  80. wpa_s->current_ssid = ssid;
  81. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  82. wpa_supplicant_initiate_eapol(wpa_s);
  83. if (old_ssid != wpa_s->current_ssid)
  84. wpas_notify_network_changed(wpa_s);
  85. return 0;
  86. }
  87. void wpa_supplicant_stop_countermeasures(void *eloop_ctx, 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. #ifdef CONFIG_IBSS_RSN
  101. ibss_rsn_deinit(wpa_s->ibss_rsn);
  102. wpa_s->ibss_rsn = NULL;
  103. #endif /* CONFIG_IBSS_RSN */
  104. #ifdef CONFIG_AP
  105. wpa_supplicant_ap_deinit(wpa_s);
  106. #endif /* CONFIG_AP */
  107. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  108. return;
  109. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  110. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  111. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  112. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  113. wpa_s->current_bss = NULL;
  114. wpa_s->assoc_freq = 0;
  115. #ifdef CONFIG_IEEE80211R
  116. #ifdef CONFIG_SME
  117. if (wpa_s->sme.ft_ies)
  118. sme_update_ft_ies(wpa_s, NULL, NULL, 0);
  119. #endif /* CONFIG_SME */
  120. #endif /* CONFIG_IEEE80211R */
  121. if (bssid_changed)
  122. wpas_notify_bssid_changed(wpa_s);
  123. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  124. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  125. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  126. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  127. wpa_s->ap_ies_from_associnfo = 0;
  128. }
  129. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  130. {
  131. struct wpa_ie_data ie;
  132. int pmksa_set = -1;
  133. size_t i;
  134. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  135. ie.pmkid == NULL)
  136. return;
  137. for (i = 0; i < ie.num_pmkid; i++) {
  138. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  139. ie.pmkid + i * PMKID_LEN,
  140. NULL, NULL, 0);
  141. if (pmksa_set == 0) {
  142. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  143. break;
  144. }
  145. }
  146. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
  147. "PMKSA cache", pmksa_set == 0 ? "" : "not ");
  148. }
  149. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  150. union wpa_event_data *data)
  151. {
  152. if (data == NULL) {
  153. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
  154. "event");
  155. return;
  156. }
  157. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  158. " index=%d preauth=%d",
  159. MAC2STR(data->pmkid_candidate.bssid),
  160. data->pmkid_candidate.index,
  161. data->pmkid_candidate.preauth);
  162. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  163. data->pmkid_candidate.index,
  164. data->pmkid_candidate.preauth);
  165. }
  166. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  167. {
  168. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  169. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  170. return 0;
  171. #ifdef IEEE8021X_EAPOL
  172. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  173. wpa_s->current_ssid &&
  174. !(wpa_s->current_ssid->eapol_flags &
  175. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  176. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  177. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  178. * plaintext or static WEP keys). */
  179. return 0;
  180. }
  181. #endif /* IEEE8021X_EAPOL */
  182. return 1;
  183. }
  184. /**
  185. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  186. * @wpa_s: pointer to wpa_supplicant data
  187. * @ssid: Configuration data for the network
  188. * Returns: 0 on success, -1 on failure
  189. *
  190. * This function is called when starting authentication with a network that is
  191. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  192. */
  193. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  194. struct wpa_ssid *ssid)
  195. {
  196. #ifdef IEEE8021X_EAPOL
  197. #ifdef PCSC_FUNCS
  198. int aka = 0, sim = 0, type;
  199. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  200. return 0;
  201. if (ssid->eap.eap_methods == NULL) {
  202. sim = 1;
  203. aka = 1;
  204. } else {
  205. struct eap_method_type *eap = ssid->eap.eap_methods;
  206. while (eap->vendor != EAP_VENDOR_IETF ||
  207. eap->method != EAP_TYPE_NONE) {
  208. if (eap->vendor == EAP_VENDOR_IETF) {
  209. if (eap->method == EAP_TYPE_SIM)
  210. sim = 1;
  211. else if (eap->method == EAP_TYPE_AKA)
  212. aka = 1;
  213. }
  214. eap++;
  215. }
  216. }
  217. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  218. sim = 0;
  219. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  220. aka = 0;
  221. if (!sim && !aka) {
  222. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
  223. "use SIM, but neither EAP-SIM nor EAP-AKA are "
  224. "enabled");
  225. return 0;
  226. }
  227. wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
  228. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  229. if (sim && aka)
  230. type = SCARD_TRY_BOTH;
  231. else if (aka)
  232. type = SCARD_USIM_ONLY;
  233. else
  234. type = SCARD_GSM_SIM_ONLY;
  235. wpa_s->scard = scard_init(type);
  236. if (wpa_s->scard == NULL) {
  237. wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
  238. "(pcsc-lite)");
  239. return -1;
  240. }
  241. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  242. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  243. #endif /* PCSC_FUNCS */
  244. #endif /* IEEE8021X_EAPOL */
  245. return 0;
  246. }
  247. #ifndef CONFIG_NO_SCAN_PROCESSING
  248. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  249. struct wpa_ssid *ssid)
  250. {
  251. int i, privacy = 0;
  252. if (ssid->mixed_cell)
  253. return 1;
  254. #ifdef CONFIG_WPS
  255. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  256. return 1;
  257. #endif /* CONFIG_WPS */
  258. for (i = 0; i < NUM_WEP_KEYS; i++) {
  259. if (ssid->wep_key_len[i]) {
  260. privacy = 1;
  261. break;
  262. }
  263. }
  264. #ifdef IEEE8021X_EAPOL
  265. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  266. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  267. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  268. privacy = 1;
  269. #endif /* IEEE8021X_EAPOL */
  270. if (wpa_key_mgmt_wpa(ssid->key_mgmt))
  271. privacy = 1;
  272. if (bss->caps & IEEE80211_CAP_PRIVACY)
  273. return privacy;
  274. return !privacy;
  275. }
  276. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  277. struct wpa_ssid *ssid,
  278. struct wpa_scan_res *bss)
  279. {
  280. struct wpa_ie_data ie;
  281. int proto_match = 0;
  282. const u8 *rsn_ie, *wpa_ie;
  283. int ret;
  284. int wep_ok;
  285. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  286. if (ret >= 0)
  287. return ret;
  288. /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
  289. wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  290. (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  291. ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
  292. (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
  293. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  294. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  295. proto_match++;
  296. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  297. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
  298. "failed");
  299. break;
  300. }
  301. if (wep_ok &&
  302. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  303. {
  304. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
  305. "in RSN IE");
  306. return 1;
  307. }
  308. if (!(ie.proto & ssid->proto)) {
  309. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
  310. "mismatch");
  311. break;
  312. }
  313. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  314. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
  315. "cipher mismatch");
  316. break;
  317. }
  318. if (!(ie.group_cipher & ssid->group_cipher)) {
  319. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
  320. "cipher mismatch");
  321. break;
  322. }
  323. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  324. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
  325. "mismatch");
  326. break;
  327. }
  328. #ifdef CONFIG_IEEE80211W
  329. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  330. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  331. wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
  332. "frame protection");
  333. break;
  334. }
  335. #endif /* CONFIG_IEEE80211W */
  336. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
  337. return 1;
  338. }
  339. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  340. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  341. proto_match++;
  342. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  343. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
  344. "failed");
  345. break;
  346. }
  347. if (wep_ok &&
  348. (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
  349. {
  350. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
  351. "in WPA IE");
  352. return 1;
  353. }
  354. if (!(ie.proto & ssid->proto)) {
  355. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
  356. "mismatch");
  357. break;
  358. }
  359. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  360. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
  361. "cipher mismatch");
  362. break;
  363. }
  364. if (!(ie.group_cipher & ssid->group_cipher)) {
  365. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
  366. "cipher mismatch");
  367. break;
  368. }
  369. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  370. wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
  371. "mismatch");
  372. break;
  373. }
  374. wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
  375. return 1;
  376. }
  377. if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
  378. wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
  379. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
  380. return 0;
  381. }
  382. if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  383. wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
  384. return 1;
  385. }
  386. wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
  387. "WPA/WPA2");
  388. return 0;
  389. }
  390. static int freq_allowed(int *freqs, int freq)
  391. {
  392. int i;
  393. if (freqs == NULL)
  394. return 1;
  395. for (i = 0; freqs[i]; i++)
  396. if (freqs[i] == freq)
  397. return 1;
  398. return 0;
  399. }
  400. static int ht_supported(const struct hostapd_hw_modes *mode)
  401. {
  402. if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
  403. /*
  404. * The driver did not indicate whether it supports HT. Assume
  405. * it does to avoid connection issues.
  406. */
  407. return 1;
  408. }
  409. /*
  410. * IEEE Std 802.11n-2009 20.1.1:
  411. * An HT non-AP STA shall support all EQM rates for one spatial stream.
  412. */
  413. return mode->mcs_set[0] == 0xff;
  414. }
  415. static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
  416. {
  417. const struct hostapd_hw_modes *mode = NULL, *modes;
  418. const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
  419. const u8 *rate_ie;
  420. int i, j, k;
  421. if (bss->freq == 0)
  422. return 1; /* Cannot do matching without knowing band */
  423. modes = wpa_s->hw.modes;
  424. if (modes == NULL) {
  425. /*
  426. * The driver does not provide any additional information
  427. * about the utilized hardware, so allow the connection attempt
  428. * to continue.
  429. */
  430. return 1;
  431. }
  432. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  433. for (j = 0; j < modes[i].num_channels; j++) {
  434. int freq = modes[i].channels[j].freq;
  435. if (freq == bss->freq) {
  436. if (mode &&
  437. mode->mode == HOSTAPD_MODE_IEEE80211G)
  438. break; /* do not allow 802.11b replace
  439. * 802.11g */
  440. mode = &modes[i];
  441. break;
  442. }
  443. }
  444. }
  445. if (mode == NULL)
  446. return 0;
  447. for (i = 0; i < (int) sizeof(scan_ie); i++) {
  448. rate_ie = wpa_scan_get_ie(bss, scan_ie[i]);
  449. if (rate_ie == NULL)
  450. continue;
  451. for (j = 2; j < rate_ie[1] + 2; j++) {
  452. int flagged = !!(rate_ie[j] & 0x80);
  453. int r = (rate_ie[j] & 0x7f) * 5;
  454. /*
  455. * IEEE Std 802.11n-2009 7.3.2.2:
  456. * The new BSS Membership selector value is encoded
  457. * like a legacy basic rate, but it is not a rate and
  458. * only indicates if the BSS members are required to
  459. * support the mandatory features of Clause 20 [HT PHY]
  460. * in order to join the BSS.
  461. */
  462. if (flagged && ((rate_ie[j] & 0x7f) ==
  463. BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
  464. if (!ht_supported(mode)) {
  465. wpa_dbg(wpa_s, MSG_DEBUG,
  466. " hardware does not support "
  467. "HT PHY");
  468. return 0;
  469. }
  470. continue;
  471. }
  472. if (!flagged)
  473. continue;
  474. /* check for legacy basic rates */
  475. for (k = 0; k < mode->num_rates; k++) {
  476. if (mode->rates[k] == r)
  477. break;
  478. }
  479. if (k == mode->num_rates) {
  480. /*
  481. * IEEE Std 802.11-2007 7.3.2.2 demands that in
  482. * order to join a BSS all required rates
  483. * have to be supported by the hardware.
  484. */
  485. wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
  486. "not support required rate %d.%d Mbps",
  487. r / 10, r % 10);
  488. return 0;
  489. }
  490. }
  491. }
  492. return 1;
  493. }
  494. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  495. int i, struct wpa_scan_res *bss,
  496. struct wpa_ssid *group)
  497. {
  498. const u8 *ssid_;
  499. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  500. int wpa;
  501. struct wpa_blacklist *e;
  502. const u8 *ie;
  503. struct wpa_ssid *ssid;
  504. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  505. ssid_ = ie ? ie + 2 : (u8 *) "";
  506. ssid_len = ie ? ie[1] : 0;
  507. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  508. wpa_ie_len = ie ? ie[1] : 0;
  509. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  510. rsn_ie_len = ie ? ie[1] : 0;
  511. wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  512. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
  513. i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
  514. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  515. wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
  516. e = wpa_blacklist_get(wpa_s, bss->bssid);
  517. if (e) {
  518. int limit = 1;
  519. if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
  520. /*
  521. * When only a single network is enabled, we can
  522. * trigger blacklisting on the first failure. This
  523. * should not be done with multiple enabled networks to
  524. * avoid getting forced to move into a worse ESS on
  525. * single error if there are no other BSSes of the
  526. * current ESS.
  527. */
  528. limit = 0;
  529. }
  530. if (e->count > limit) {
  531. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  532. "(count=%d limit=%d)", e->count, limit);
  533. return NULL;
  534. }
  535. }
  536. if (ssid_len == 0) {
  537. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
  538. return NULL;
  539. }
  540. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  541. for (ssid = group; ssid; ssid = ssid->pnext) {
  542. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  543. if (ssid->disabled) {
  544. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
  545. continue;
  546. }
  547. #ifdef CONFIG_WPS
  548. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  549. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  550. "(WPS)");
  551. continue;
  552. }
  553. if (wpa && ssid->ssid_len == 0 &&
  554. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  555. check_ssid = 0;
  556. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  557. /* Only allow wildcard SSID match if an AP
  558. * advertises active WPS operation that matches
  559. * with our mode. */
  560. check_ssid = 1;
  561. if (ssid->ssid_len == 0 &&
  562. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  563. check_ssid = 0;
  564. }
  565. #endif /* CONFIG_WPS */
  566. if (ssid->bssid_set && ssid->ssid_len == 0 &&
  567. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
  568. check_ssid = 0;
  569. if (check_ssid &&
  570. (ssid_len != ssid->ssid_len ||
  571. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  572. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
  573. continue;
  574. }
  575. if (ssid->bssid_set &&
  576. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  577. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
  578. continue;
  579. }
  580. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  581. continue;
  582. if (!wpa &&
  583. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  584. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  585. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  586. wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
  587. "not allowed");
  588. continue;
  589. }
  590. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  591. wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
  592. "mismatch");
  593. continue;
  594. }
  595. if (bss->caps & IEEE80211_CAP_IBSS) {
  596. wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
  597. "network");
  598. continue;
  599. }
  600. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  601. wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
  602. "allowed");
  603. continue;
  604. }
  605. if (!rate_match(wpa_s, bss)) {
  606. wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
  607. "not match");
  608. continue;
  609. }
  610. #ifdef CONFIG_P2P
  611. /*
  612. * TODO: skip the AP if its P2P IE has Group Formation
  613. * bit set in the P2P Group Capability Bitmap and we
  614. * are not in Group Formation with that device.
  615. */
  616. #endif /* CONFIG_P2P */
  617. /* Matching configuration found */
  618. return ssid;
  619. }
  620. /* No matching configuration found */
  621. return NULL;
  622. }
  623. static struct wpa_bss *
  624. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  625. struct wpa_scan_results *scan_res,
  626. struct wpa_ssid *group,
  627. struct wpa_ssid **selected_ssid)
  628. {
  629. size_t i;
  630. wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
  631. group->priority);
  632. for (i = 0; i < scan_res->num; i++) {
  633. struct wpa_scan_res *bss = scan_res->res[i];
  634. const u8 *ie, *ssid;
  635. u8 ssid_len;
  636. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
  637. if (!*selected_ssid)
  638. continue;
  639. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  640. ssid = ie ? ie + 2 : (u8 *) "";
  641. ssid_len = ie ? ie[1] : 0;
  642. wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
  643. " ssid='%s'",
  644. MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  645. return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
  646. }
  647. return NULL;
  648. }
  649. static struct wpa_bss *
  650. wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  651. struct wpa_scan_results *scan_res,
  652. struct wpa_ssid **selected_ssid)
  653. {
  654. struct wpa_bss *selected = NULL;
  655. int prio;
  656. while (selected == NULL) {
  657. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  658. selected = wpa_supplicant_select_bss(
  659. wpa_s, scan_res, wpa_s->conf->pssid[prio],
  660. selected_ssid);
  661. if (selected)
  662. break;
  663. }
  664. if (selected == NULL && wpa_s->blacklist) {
  665. wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
  666. "blacklist and try again");
  667. wpa_blacklist_clear(wpa_s);
  668. wpa_s->blacklist_cleared++;
  669. } else if (selected == NULL)
  670. break;
  671. }
  672. return selected;
  673. }
  674. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  675. int timeout_sec, int timeout_usec)
  676. {
  677. if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
  678. /*
  679. * No networks are enabled; short-circuit request so
  680. * we don't wait timeout seconds before transitioning
  681. * to INACTIVE state.
  682. */
  683. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  684. return;
  685. }
  686. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  687. }
  688. int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  689. struct wpa_bss *selected,
  690. struct wpa_ssid *ssid)
  691. {
  692. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  693. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  694. "PBC session overlap");
  695. #ifdef CONFIG_P2P
  696. if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
  697. return -1;
  698. #endif /* CONFIG_P2P */
  699. #ifdef CONFIG_WPS
  700. wpas_wps_cancel(wpa_s);
  701. #endif /* CONFIG_WPS */
  702. return -1;
  703. }
  704. /*
  705. * Do not trigger new association unless the BSSID has changed or if
  706. * reassociation is requested. If we are in process of associating with
  707. * the selected BSSID, do not trigger new attempt.
  708. */
  709. if (wpa_s->reassociate ||
  710. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  711. ((wpa_s->wpa_state != WPA_ASSOCIATING &&
  712. wpa_s->wpa_state != WPA_AUTHENTICATING) ||
  713. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  714. 0))) {
  715. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  716. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  717. return 0;
  718. }
  719. wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
  720. "reassociate: %d selected: "MACSTR " bssid: " MACSTR
  721. " pending: " MACSTR " wpa_state: %s",
  722. wpa_s->reassociate, MAC2STR(selected->bssid),
  723. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  724. wpa_supplicant_state_txt(wpa_s->wpa_state));
  725. wpa_supplicant_associate(wpa_s, selected, ssid);
  726. } else {
  727. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
  728. "selected AP");
  729. }
  730. return 0;
  731. }
  732. static struct wpa_ssid *
  733. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  734. {
  735. int prio;
  736. struct wpa_ssid *ssid;
  737. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  738. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  739. {
  740. if (ssid->disabled)
  741. continue;
  742. if (ssid->mode == IEEE80211_MODE_IBSS ||
  743. ssid->mode == IEEE80211_MODE_AP)
  744. return ssid;
  745. }
  746. }
  747. return NULL;
  748. }
  749. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  750. * on BSS added and BSS changed events */
  751. static void wpa_supplicant_rsn_preauth_scan_results(
  752. struct wpa_supplicant *wpa_s)
  753. {
  754. struct wpa_bss *bss;
  755. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  756. return;
  757. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  758. const u8 *ssid, *rsn;
  759. ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  760. if (ssid == NULL)
  761. continue;
  762. rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  763. if (rsn == NULL)
  764. continue;
  765. rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
  766. }
  767. }
  768. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  769. struct wpa_bss *selected,
  770. struct wpa_ssid *ssid,
  771. struct wpa_scan_results *scan_res)
  772. {
  773. size_t i;
  774. struct wpa_scan_res *current_bss = NULL;
  775. int min_diff;
  776. if (wpa_s->reassociate)
  777. return 1; /* explicit request to reassociate */
  778. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  779. return 1; /* we are not associated; continue */
  780. if (wpa_s->current_ssid == NULL)
  781. return 1; /* unknown current SSID */
  782. if (wpa_s->current_ssid != ssid)
  783. return 1; /* different network block */
  784. if (wpas_driver_bss_selection(wpa_s))
  785. return 0; /* Driver-based roaming */
  786. for (i = 0; i < scan_res->num; i++) {
  787. struct wpa_scan_res *res = scan_res->res[i];
  788. const u8 *ie;
  789. if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  790. continue;
  791. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  792. if (ie == NULL)
  793. continue;
  794. if (ie[1] != wpa_s->current_ssid->ssid_len ||
  795. os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
  796. continue;
  797. current_bss = res;
  798. break;
  799. }
  800. if (!current_bss)
  801. return 1; /* current BSS not seen in scan results */
  802. wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
  803. wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  804. MAC2STR(current_bss->bssid), current_bss->level);
  805. wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  806. MAC2STR(selected->bssid), selected->level);
  807. if (wpa_s->current_ssid->bssid_set &&
  808. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  809. 0) {
  810. wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
  811. "has preferred BSSID");
  812. return 1;
  813. }
  814. min_diff = 2;
  815. if (current_bss->level < 0) {
  816. if (current_bss->level < -85)
  817. min_diff = 1;
  818. else if (current_bss->level < -80)
  819. min_diff = 2;
  820. else if (current_bss->level < -75)
  821. min_diff = 3;
  822. else if (current_bss->level < -70)
  823. min_diff = 4;
  824. else
  825. min_diff = 5;
  826. }
  827. if (abs(current_bss->level - selected->level) < min_diff) {
  828. wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
  829. "in signal level");
  830. return 0;
  831. }
  832. return 1;
  833. }
  834. /* Return < 0 if no scan results could be fetched. */
  835. static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  836. union wpa_event_data *data)
  837. {
  838. struct wpa_bss *selected;
  839. struct wpa_ssid *ssid = NULL;
  840. struct wpa_scan_results *scan_res;
  841. int ap = 0;
  842. #ifdef CONFIG_AP
  843. if (wpa_s->ap_iface)
  844. ap = 1;
  845. #endif /* CONFIG_AP */
  846. wpa_supplicant_notify_scanning(wpa_s, 0);
  847. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  848. data ? &data->scan_info :
  849. NULL, 1);
  850. if (scan_res == NULL) {
  851. if (wpa_s->conf->ap_scan == 2 || ap)
  852. return -1;
  853. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
  854. "scanning again");
  855. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  856. return -1;
  857. }
  858. #ifndef CONFIG_NO_RANDOM_POOL
  859. size_t i, num;
  860. num = scan_res->num;
  861. if (num > 10)
  862. num = 10;
  863. for (i = 0; i < num; i++) {
  864. u8 buf[5];
  865. struct wpa_scan_res *res = scan_res->res[i];
  866. buf[0] = res->bssid[5];
  867. buf[1] = res->qual & 0xff;
  868. buf[2] = res->noise & 0xff;
  869. buf[3] = res->level & 0xff;
  870. buf[4] = res->tsf & 0xff;
  871. random_add_randomness(buf, sizeof(buf));
  872. }
  873. #endif /* CONFIG_NO_RANDOM_POOL */
  874. if (wpa_s->scan_res_handler) {
  875. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  876. struct wpa_scan_results *scan_res);
  877. scan_res_handler = wpa_s->scan_res_handler;
  878. wpa_s->scan_res_handler = NULL;
  879. scan_res_handler(wpa_s, scan_res);
  880. wpa_scan_results_free(scan_res);
  881. return 0;
  882. }
  883. if (ap) {
  884. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
  885. #ifdef CONFIG_AP
  886. if (wpa_s->ap_iface->scan_cb)
  887. wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
  888. #endif /* CONFIG_AP */
  889. wpa_scan_results_free(scan_res);
  890. return 0;
  891. }
  892. wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
  893. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  894. wpas_notify_scan_results(wpa_s);
  895. wpas_notify_scan_done(wpa_s, 1);
  896. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  897. wpa_scan_results_free(scan_res);
  898. return 0;
  899. }
  900. if (wpa_s->disconnected) {
  901. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  902. wpa_scan_results_free(scan_res);
  903. return 0;
  904. }
  905. if (!wpas_driver_bss_selection(wpa_s) &&
  906. bgscan_notify_scan(wpa_s, scan_res) == 1) {
  907. wpa_scan_results_free(scan_res);
  908. return 0;
  909. }
  910. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  911. if (selected) {
  912. int skip;
  913. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  914. scan_res);
  915. wpa_scan_results_free(scan_res);
  916. if (skip) {
  917. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  918. return 0;
  919. }
  920. if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
  921. wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
  922. return -1;
  923. }
  924. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  925. } else {
  926. wpa_scan_results_free(scan_res);
  927. wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
  928. ssid = wpa_supplicant_pick_new_network(wpa_s);
  929. if (ssid) {
  930. wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
  931. wpa_supplicant_associate(wpa_s, NULL, ssid);
  932. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  933. } else {
  934. int timeout_sec = wpa_s->scan_interval;
  935. int timeout_usec = 0;
  936. #ifdef CONFIG_P2P
  937. if (wpa_s->p2p_in_provisioning) {
  938. /*
  939. * Use shorter wait during P2P Provisioning
  940. * state to speed up group formation.
  941. */
  942. timeout_sec = 0;
  943. timeout_usec = 250000;
  944. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  945. timeout_usec);
  946. return 0;
  947. }
  948. #endif /* CONFIG_P2P */
  949. if (wpa_supplicant_req_sched_scan(wpa_s))
  950. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  951. timeout_usec);
  952. }
  953. }
  954. return 0;
  955. }
  956. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  957. union wpa_event_data *data)
  958. {
  959. const char *rn, *rn2;
  960. struct wpa_supplicant *ifs;
  961. if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
  962. /*
  963. * If no scan results could be fetched, then no need to
  964. * notify those interfaces that did not actually request
  965. * this scan.
  966. */
  967. return;
  968. }
  969. /*
  970. * Check other interfaces to see if they have the same radio-name. If
  971. * so, they get updated with this same scan info.
  972. */
  973. if (!wpa_s->driver->get_radio_name)
  974. return;
  975. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  976. if (rn == NULL || rn[0] == '\0')
  977. return;
  978. wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
  979. "sharing same radio (%s) in event_scan_results", rn);
  980. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  981. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  982. continue;
  983. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  984. if (rn2 && os_strcmp(rn, rn2) == 0) {
  985. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  986. "sibling", ifs->ifname);
  987. _wpa_supplicant_event_scan_results(ifs, data);
  988. }
  989. }
  990. }
  991. #endif /* CONFIG_NO_SCAN_PROCESSING */
  992. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  993. union wpa_event_data *data)
  994. {
  995. int l, len, found = 0, wpa_found, rsn_found;
  996. const u8 *p;
  997. wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
  998. if (data->assoc_info.req_ies)
  999. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  1000. data->assoc_info.req_ies_len);
  1001. if (data->assoc_info.resp_ies) {
  1002. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  1003. data->assoc_info.resp_ies_len);
  1004. #ifdef CONFIG_TDLS
  1005. wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
  1006. data->assoc_info.resp_ies_len);
  1007. #endif /* CONFIG_TDLS */
  1008. }
  1009. if (data->assoc_info.beacon_ies)
  1010. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  1011. data->assoc_info.beacon_ies,
  1012. data->assoc_info.beacon_ies_len);
  1013. if (data->assoc_info.freq)
  1014. wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
  1015. data->assoc_info.freq);
  1016. p = data->assoc_info.req_ies;
  1017. l = data->assoc_info.req_ies_len;
  1018. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  1019. while (p && l >= 2) {
  1020. len = p[1] + 2;
  1021. if (len > l) {
  1022. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1023. p, l);
  1024. break;
  1025. }
  1026. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1027. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  1028. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  1029. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  1030. break;
  1031. found = 1;
  1032. wpa_find_assoc_pmkid(wpa_s);
  1033. break;
  1034. }
  1035. l -= len;
  1036. p += len;
  1037. }
  1038. if (!found && data->assoc_info.req_ies)
  1039. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1040. #ifdef CONFIG_IEEE80211R
  1041. #ifdef CONFIG_SME
  1042. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  1043. u8 bssid[ETH_ALEN];
  1044. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1045. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1046. data->assoc_info.resp_ies,
  1047. data->assoc_info.resp_ies_len,
  1048. bssid) < 0) {
  1049. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1050. "Reassociation Response failed");
  1051. wpa_supplicant_deauthenticate(
  1052. wpa_s, WLAN_REASON_INVALID_IE);
  1053. return -1;
  1054. }
  1055. }
  1056. p = data->assoc_info.resp_ies;
  1057. l = data->assoc_info.resp_ies_len;
  1058. #ifdef CONFIG_WPS_STRICT
  1059. if (p && wpa_s->current_ssid &&
  1060. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1061. struct wpabuf *wps;
  1062. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  1063. if (wps == NULL) {
  1064. wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
  1065. "include WPS IE in (Re)Association Response");
  1066. return -1;
  1067. }
  1068. if (wps_validate_assoc_resp(wps) < 0) {
  1069. wpabuf_free(wps);
  1070. wpa_supplicant_deauthenticate(
  1071. wpa_s, WLAN_REASON_INVALID_IE);
  1072. return -1;
  1073. }
  1074. wpabuf_free(wps);
  1075. }
  1076. #endif /* CONFIG_WPS_STRICT */
  1077. /* Go through the IEs and make a copy of the MDIE, if present. */
  1078. while (p && l >= 2) {
  1079. len = p[1] + 2;
  1080. if (len > l) {
  1081. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1082. p, l);
  1083. break;
  1084. }
  1085. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  1086. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  1087. wpa_s->sme.ft_used = 1;
  1088. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  1089. MOBILITY_DOMAIN_ID_LEN);
  1090. break;
  1091. }
  1092. l -= len;
  1093. p += len;
  1094. }
  1095. #endif /* CONFIG_SME */
  1096. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  1097. data->assoc_info.resp_ies_len);
  1098. #endif /* CONFIG_IEEE80211R */
  1099. /* WPA/RSN IE from Beacon/ProbeResp */
  1100. p = data->assoc_info.beacon_ies;
  1101. l = data->assoc_info.beacon_ies_len;
  1102. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  1103. */
  1104. wpa_found = rsn_found = 0;
  1105. while (p && l >= 2) {
  1106. len = p[1] + 2;
  1107. if (len > l) {
  1108. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  1109. p, l);
  1110. break;
  1111. }
  1112. if (!wpa_found &&
  1113. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1114. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  1115. wpa_found = 1;
  1116. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  1117. }
  1118. if (!rsn_found &&
  1119. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  1120. rsn_found = 1;
  1121. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  1122. }
  1123. l -= len;
  1124. p += len;
  1125. }
  1126. if (!wpa_found && data->assoc_info.beacon_ies)
  1127. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  1128. if (!rsn_found && data->assoc_info.beacon_ies)
  1129. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  1130. if (wpa_found || rsn_found)
  1131. wpa_s->ap_ies_from_associnfo = 1;
  1132. if (wpa_s->assoc_freq && data->assoc_info.freq &&
  1133. wpa_s->assoc_freq != data->assoc_info.freq) {
  1134. wpa_printf(MSG_DEBUG, "Operating frequency changed from "
  1135. "%u to %u MHz",
  1136. wpa_s->assoc_freq, data->assoc_info.freq);
  1137. wpa_supplicant_update_scan_results(wpa_s);
  1138. }
  1139. wpa_s->assoc_freq = data->assoc_info.freq;
  1140. return 0;
  1141. }
  1142. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  1143. union wpa_event_data *data)
  1144. {
  1145. u8 bssid[ETH_ALEN];
  1146. int ft_completed;
  1147. int bssid_changed;
  1148. struct wpa_driver_capa capa;
  1149. #ifdef CONFIG_AP
  1150. if (wpa_s->ap_iface) {
  1151. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  1152. data->assoc_info.addr,
  1153. data->assoc_info.req_ies,
  1154. data->assoc_info.req_ies_len,
  1155. data->assoc_info.reassoc);
  1156. return;
  1157. }
  1158. #endif /* CONFIG_AP */
  1159. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  1160. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  1161. return;
  1162. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  1163. if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  1164. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
  1165. wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  1166. MACSTR, MAC2STR(bssid));
  1167. random_add_randomness(bssid, ETH_ALEN);
  1168. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  1169. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  1170. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1171. if (bssid_changed)
  1172. wpas_notify_bssid_changed(wpa_s);
  1173. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  1174. wpa_clear_keys(wpa_s, bssid);
  1175. }
  1176. if (wpa_supplicant_select_config(wpa_s) < 0) {
  1177. wpa_supplicant_disassociate(
  1178. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1179. return;
  1180. }
  1181. if (wpa_s->current_ssid) {
  1182. struct wpa_bss *bss = NULL;
  1183. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1184. if (ssid->ssid_len > 0)
  1185. bss = wpa_bss_get(wpa_s, bssid,
  1186. ssid->ssid, ssid->ssid_len);
  1187. if (!bss)
  1188. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1189. if (bss)
  1190. wpa_s->current_bss = bss;
  1191. }
  1192. }
  1193. #ifdef CONFIG_SME
  1194. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1195. wpa_s->sme.prev_bssid_set = 1;
  1196. #endif /* CONFIG_SME */
  1197. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1198. if (wpa_s->current_ssid) {
  1199. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1200. * initialized before association, but for other modes,
  1201. * initialize PC/SC here, if the current configuration needs
  1202. * smartcard or SIM/USIM. */
  1203. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1204. }
  1205. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1206. if (wpa_s->l2)
  1207. l2_packet_notify_auth_start(wpa_s->l2);
  1208. /*
  1209. * Set portEnabled first to FALSE in order to get EAP state machine out
  1210. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1211. * state machine may transit to AUTHENTICATING state based on obsolete
  1212. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1213. * AUTHENTICATED without ever giving chance to EAP state machine to
  1214. * reset the state.
  1215. */
  1216. if (!ft_completed) {
  1217. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1218. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1219. }
  1220. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1221. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1222. /* 802.1X::portControl = Auto */
  1223. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1224. wpa_s->eapol_received = 0;
  1225. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1226. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1227. (wpa_s->current_ssid &&
  1228. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1229. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1230. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1231. } else if (!ft_completed) {
  1232. /* Timeout for receiving the first EAPOL packet */
  1233. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1234. }
  1235. wpa_supplicant_cancel_scan(wpa_s);
  1236. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1237. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1238. /*
  1239. * We are done; the driver will take care of RSN 4-way
  1240. * handshake.
  1241. */
  1242. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1243. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1244. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1245. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1246. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1247. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1248. /*
  1249. * The driver will take care of RSN 4-way handshake, so we need
  1250. * to allow EAPOL supplicant to complete its work without
  1251. * waiting for WPA supplicant.
  1252. */
  1253. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1254. } else if (ft_completed) {
  1255. /*
  1256. * FT protocol completed - make sure EAPOL state machine ends
  1257. * up in authenticated.
  1258. */
  1259. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1260. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1261. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1262. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1263. }
  1264. if (wpa_s->pending_eapol_rx) {
  1265. struct os_time now, age;
  1266. os_get_time(&now);
  1267. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1268. if (age.sec == 0 && age.usec < 100000 &&
  1269. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1270. 0) {
  1271. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
  1272. "frame that was received just before "
  1273. "association notification");
  1274. wpa_supplicant_rx_eapol(
  1275. wpa_s, wpa_s->pending_eapol_rx_src,
  1276. wpabuf_head(wpa_s->pending_eapol_rx),
  1277. wpabuf_len(wpa_s->pending_eapol_rx));
  1278. }
  1279. wpabuf_free(wpa_s->pending_eapol_rx);
  1280. wpa_s->pending_eapol_rx = NULL;
  1281. }
  1282. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1283. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1284. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1285. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1286. /* Set static WEP keys again */
  1287. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1288. }
  1289. #ifdef CONFIG_IBSS_RSN
  1290. if (wpa_s->current_ssid &&
  1291. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  1292. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1293. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
  1294. wpa_s->ibss_rsn == NULL) {
  1295. wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
  1296. if (!wpa_s->ibss_rsn) {
  1297. wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
  1298. wpa_supplicant_deauthenticate(
  1299. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1300. return;
  1301. }
  1302. ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
  1303. }
  1304. #endif /* CONFIG_IBSS_RSN */
  1305. }
  1306. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1307. u16 reason_code)
  1308. {
  1309. const u8 *bssid;
  1310. int authenticating;
  1311. u8 prev_pending_bssid[ETH_ALEN];
  1312. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1313. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1314. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1315. /*
  1316. * At least Host AP driver and a Prism3 card seemed to be
  1317. * generating streams of disconnected events when configuring
  1318. * IBSS for WPA-None. Ignore them for now.
  1319. */
  1320. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
  1321. "IBSS/WPA-None mode");
  1322. return;
  1323. }
  1324. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1325. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1326. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1327. "pre-shared key may be incorrect");
  1328. }
  1329. if (!wpa_s->auto_reconnect_disabled ||
  1330. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  1331. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
  1332. "reconnect (wps=%d)",
  1333. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
  1334. if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1335. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1336. } else {
  1337. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
  1338. "try to re-connect");
  1339. wpa_s->reassociate = 0;
  1340. wpa_s->disconnected = 1;
  1341. }
  1342. bssid = wpa_s->bssid;
  1343. if (is_zero_ether_addr(bssid))
  1344. bssid = wpa_s->pending_bssid;
  1345. wpas_connection_failed(wpa_s, bssid);
  1346. wpa_sm_notify_disassoc(wpa_s->wpa);
  1347. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1348. " reason=%d",
  1349. MAC2STR(bssid), reason_code);
  1350. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1351. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
  1352. wpa_s->keys_cleared = 0;
  1353. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1354. }
  1355. wpa_supplicant_mark_disassoc(wpa_s);
  1356. if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1357. sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
  1358. }
  1359. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1360. void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
  1361. {
  1362. struct wpa_supplicant *wpa_s = eloop_ctx;
  1363. if (!wpa_s->pending_mic_error_report)
  1364. return;
  1365. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
  1366. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1367. wpa_s->pending_mic_error_report = 0;
  1368. }
  1369. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1370. static void
  1371. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1372. union wpa_event_data *data)
  1373. {
  1374. int pairwise;
  1375. struct os_time t;
  1376. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1377. pairwise = (data && data->michael_mic_failure.unicast);
  1378. os_get_time(&t);
  1379. if ((wpa_s->last_michael_mic_error &&
  1380. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1381. wpa_s->pending_mic_error_report) {
  1382. if (wpa_s->pending_mic_error_report) {
  1383. /*
  1384. * Send the pending MIC error report immediately since
  1385. * we are going to start countermeasures and AP better
  1386. * do the same.
  1387. */
  1388. wpa_sm_key_request(wpa_s->wpa, 1,
  1389. wpa_s->pending_mic_error_pairwise);
  1390. }
  1391. /* Send the new MIC error report immediately since we are going
  1392. * to start countermeasures and AP better do the same.
  1393. */
  1394. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1395. /* initialize countermeasures */
  1396. wpa_s->countermeasures = 1;
  1397. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1398. /*
  1399. * Need to wait for completion of request frame. We do not get
  1400. * any callback for the message completion, so just wait a
  1401. * short while and hope for the best. */
  1402. os_sleep(0, 10000);
  1403. wpa_drv_set_countermeasures(wpa_s, 1);
  1404. wpa_supplicant_deauthenticate(wpa_s,
  1405. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1406. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1407. wpa_s, NULL);
  1408. eloop_register_timeout(60, 0,
  1409. wpa_supplicant_stop_countermeasures,
  1410. wpa_s, NULL);
  1411. /* TODO: mark the AP rejected for 60 second. STA is
  1412. * allowed to associate with another AP.. */
  1413. } else {
  1414. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1415. if (wpa_s->mic_errors_seen) {
  1416. /*
  1417. * Reduce the effectiveness of Michael MIC error
  1418. * reports as a means for attacking against TKIP if
  1419. * more than one MIC failure is noticed with the same
  1420. * PTK. We delay the transmission of the reports by a
  1421. * random time between 0 and 60 seconds in order to
  1422. * force the attacker wait 60 seconds before getting
  1423. * the information on whether a frame resulted in a MIC
  1424. * failure.
  1425. */
  1426. u8 rval[4];
  1427. int sec;
  1428. if (os_get_random(rval, sizeof(rval)) < 0)
  1429. sec = os_random() % 60;
  1430. else
  1431. sec = WPA_GET_BE32(rval) % 60;
  1432. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
  1433. "report %d seconds", sec);
  1434. wpa_s->pending_mic_error_report = 1;
  1435. wpa_s->pending_mic_error_pairwise = pairwise;
  1436. eloop_cancel_timeout(
  1437. wpa_supplicant_delayed_mic_error_report,
  1438. wpa_s, NULL);
  1439. eloop_register_timeout(
  1440. sec, os_random() % 1000000,
  1441. wpa_supplicant_delayed_mic_error_report,
  1442. wpa_s, NULL);
  1443. } else {
  1444. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1445. }
  1446. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1447. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1448. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1449. }
  1450. wpa_s->last_michael_mic_error = t.sec;
  1451. wpa_s->mic_errors_seen++;
  1452. }
  1453. #ifdef CONFIG_TERMINATE_ONLASTIF
  1454. static int any_interfaces(struct wpa_supplicant *head)
  1455. {
  1456. struct wpa_supplicant *wpa_s;
  1457. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1458. if (!wpa_s->interface_removed)
  1459. return 1;
  1460. return 0;
  1461. }
  1462. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1463. static void
  1464. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1465. union wpa_event_data *data)
  1466. {
  1467. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1468. return;
  1469. switch (data->interface_status.ievent) {
  1470. case EVENT_INTERFACE_ADDED:
  1471. if (!wpa_s->interface_removed)
  1472. break;
  1473. wpa_s->interface_removed = 0;
  1474. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
  1475. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1476. wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
  1477. "driver after interface was added");
  1478. }
  1479. break;
  1480. case EVENT_INTERFACE_REMOVED:
  1481. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
  1482. wpa_s->interface_removed = 1;
  1483. wpa_supplicant_mark_disassoc(wpa_s);
  1484. l2_packet_deinit(wpa_s->l2);
  1485. wpa_s->l2 = NULL;
  1486. #ifdef CONFIG_IBSS_RSN
  1487. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1488. wpa_s->ibss_rsn = NULL;
  1489. #endif /* CONFIG_IBSS_RSN */
  1490. #ifdef CONFIG_TERMINATE_ONLASTIF
  1491. /* check if last interface */
  1492. if (!any_interfaces(wpa_s->global->ifaces))
  1493. eloop_terminate();
  1494. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1495. break;
  1496. }
  1497. }
  1498. #ifdef CONFIG_PEERKEY
  1499. static void
  1500. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1501. union wpa_event_data *data)
  1502. {
  1503. if (data == NULL)
  1504. return;
  1505. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1506. }
  1507. #endif /* CONFIG_PEERKEY */
  1508. #ifdef CONFIG_TDLS
  1509. static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
  1510. union wpa_event_data *data)
  1511. {
  1512. if (data == NULL)
  1513. return;
  1514. switch (data->tdls.oper) {
  1515. case TDLS_REQUEST_SETUP:
  1516. wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
  1517. break;
  1518. case TDLS_REQUEST_TEARDOWN:
  1519. wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
  1520. data->tdls.reason_code);
  1521. break;
  1522. }
  1523. }
  1524. #endif /* CONFIG_TDLS */
  1525. #ifdef CONFIG_IEEE80211R
  1526. static void
  1527. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1528. union wpa_event_data *data)
  1529. {
  1530. if (data == NULL)
  1531. return;
  1532. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1533. data->ft_ies.ies_len,
  1534. data->ft_ies.ft_action,
  1535. data->ft_ies.target_ap,
  1536. data->ft_ies.ric_ies,
  1537. data->ft_ies.ric_ies_len) < 0) {
  1538. /* TODO: prevent MLME/driver from trying to associate? */
  1539. }
  1540. }
  1541. #endif /* CONFIG_IEEE80211R */
  1542. #ifdef CONFIG_IBSS_RSN
  1543. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1544. union wpa_event_data *data)
  1545. {
  1546. struct wpa_ssid *ssid;
  1547. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1548. return;
  1549. if (data == NULL)
  1550. return;
  1551. ssid = wpa_s->current_ssid;
  1552. if (ssid == NULL)
  1553. return;
  1554. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  1555. return;
  1556. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1557. }
  1558. #endif /* CONFIG_IBSS_RSN */
  1559. #ifdef CONFIG_IEEE80211R
  1560. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  1561. size_t len)
  1562. {
  1563. const u8 *sta_addr, *target_ap_addr;
  1564. u16 status;
  1565. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  1566. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1567. return; /* only SME case supported for now */
  1568. if (len < 1 + 2 * ETH_ALEN + 2)
  1569. return;
  1570. if (data[0] != 2)
  1571. return; /* Only FT Action Response is supported for now */
  1572. sta_addr = data + 1;
  1573. target_ap_addr = data + 1 + ETH_ALEN;
  1574. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  1575. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
  1576. MACSTR " TargetAP " MACSTR " status %u",
  1577. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1578. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1579. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  1580. " in FT Action Response", MAC2STR(sta_addr));
  1581. return;
  1582. }
  1583. if (status) {
  1584. wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
  1585. "failure (status code %d)", status);
  1586. /* TODO: report error to FT code(?) */
  1587. return;
  1588. }
  1589. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  1590. len - (1 + 2 * ETH_ALEN + 2), 1,
  1591. target_ap_addr, NULL, 0) < 0)
  1592. return;
  1593. #ifdef CONFIG_SME
  1594. {
  1595. struct wpa_bss *bss;
  1596. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  1597. if (bss)
  1598. wpa_s->sme.freq = bss->freq;
  1599. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  1600. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  1601. WLAN_AUTH_FT);
  1602. }
  1603. #endif /* CONFIG_SME */
  1604. }
  1605. #endif /* CONFIG_IEEE80211R */
  1606. static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
  1607. struct unprot_deauth *e)
  1608. {
  1609. #ifdef CONFIG_IEEE80211W
  1610. wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
  1611. "dropped: " MACSTR " -> " MACSTR
  1612. " (reason code %u)",
  1613. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1614. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1615. #endif /* CONFIG_IEEE80211W */
  1616. }
  1617. static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
  1618. struct unprot_disassoc *e)
  1619. {
  1620. #ifdef CONFIG_IEEE80211W
  1621. wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
  1622. "dropped: " MACSTR " -> " MACSTR
  1623. " (reason code %u)",
  1624. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1625. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1626. #endif /* CONFIG_IEEE80211W */
  1627. }
  1628. static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
  1629. {
  1630. u8 action, mode;
  1631. const u8 *pos, *end;
  1632. if (rx->data == NULL || rx->len == 0)
  1633. return;
  1634. pos = rx->data;
  1635. end = pos + rx->len;
  1636. action = *pos++;
  1637. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  1638. action, MAC2STR(rx->sa));
  1639. switch (action) {
  1640. case WNM_BSS_TRANS_MGMT_REQ:
  1641. if (pos + 5 > end)
  1642. break;
  1643. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
  1644. "Request: dialog_token=%u request_mode=0x%x "
  1645. "disassoc_timer=%u validity_interval=%u",
  1646. pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
  1647. mode = pos[1];
  1648. pos += 5;
  1649. if (mode & 0x08)
  1650. pos += 12; /* BSS Termination Duration */
  1651. if (mode & 0x10) {
  1652. char url[256];
  1653. if (pos + 1 > end || pos + 1 + pos[0] > end) {
  1654. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
  1655. "Transition Management Request "
  1656. "(URL)");
  1657. break;
  1658. }
  1659. os_memcpy(url, pos + 1, pos[0]);
  1660. url[pos[0]] = '\0';
  1661. wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
  1662. "Imminent - session_info_url=%s", url);
  1663. }
  1664. break;
  1665. }
  1666. }
  1667. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1668. union wpa_event_data *data)
  1669. {
  1670. struct wpa_supplicant *wpa_s = ctx;
  1671. u16 reason_code = 0;
  1672. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  1673. event != EVENT_INTERFACE_ENABLED &&
  1674. event != EVENT_INTERFACE_STATUS) {
  1675. wpa_dbg(wpa_s, MSG_DEBUG,
  1676. "Ignore event %s (%d) while interface is disabled",
  1677. event_to_string(event), event);
  1678. return;
  1679. }
  1680. wpa_dbg(wpa_s, MSG_DEBUG, "Event %s (%d) received",
  1681. event_to_string(event), event);
  1682. switch (event) {
  1683. case EVENT_AUTH:
  1684. sme_event_auth(wpa_s, data);
  1685. break;
  1686. case EVENT_ASSOC:
  1687. wpa_supplicant_event_assoc(wpa_s, data);
  1688. break;
  1689. case EVENT_DISASSOC:
  1690. wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
  1691. if (data) {
  1692. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
  1693. data->disassoc_info.reason_code);
  1694. if (data->disassoc_info.addr)
  1695. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  1696. MAC2STR(data->disassoc_info.addr));
  1697. }
  1698. #ifdef CONFIG_AP
  1699. if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
  1700. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1701. data->disassoc_info.addr);
  1702. break;
  1703. }
  1704. #endif /* CONFIG_AP */
  1705. if (data) {
  1706. reason_code = data->disassoc_info.reason_code;
  1707. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  1708. data->disassoc_info.ie,
  1709. data->disassoc_info.ie_len);
  1710. #ifdef CONFIG_P2P
  1711. wpas_p2p_disassoc_notif(
  1712. wpa_s, data->disassoc_info.addr, reason_code,
  1713. data->disassoc_info.ie,
  1714. data->disassoc_info.ie_len);
  1715. #endif /* CONFIG_P2P */
  1716. }
  1717. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1718. sme_event_disassoc(wpa_s, data);
  1719. /* fall through */
  1720. case EVENT_DEAUTH:
  1721. if (event == EVENT_DEAUTH) {
  1722. wpa_dbg(wpa_s, MSG_DEBUG,
  1723. "Deauthentication notification");
  1724. if (data) {
  1725. reason_code = data->deauth_info.reason_code;
  1726. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
  1727. data->deauth_info.reason_code);
  1728. if (data->deauth_info.addr) {
  1729. wpa_dbg(wpa_s, MSG_DEBUG, " * address "
  1730. MACSTR,
  1731. MAC2STR(data->deauth_info.
  1732. addr));
  1733. }
  1734. wpa_hexdump(MSG_DEBUG,
  1735. "Deauthentication frame IE(s)",
  1736. data->deauth_info.ie,
  1737. data->deauth_info.ie_len);
  1738. #ifdef CONFIG_P2P
  1739. wpas_p2p_deauth_notif(
  1740. wpa_s, data->deauth_info.addr,
  1741. reason_code,
  1742. data->deauth_info.ie,
  1743. data->deauth_info.ie_len);
  1744. #endif /* CONFIG_P2P */
  1745. }
  1746. }
  1747. #ifdef CONFIG_AP
  1748. if (wpa_s->ap_iface && data && data->deauth_info.addr) {
  1749. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1750. data->deauth_info.addr);
  1751. break;
  1752. }
  1753. #endif /* CONFIG_AP */
  1754. wpa_supplicant_event_disassoc(wpa_s, reason_code);
  1755. break;
  1756. case EVENT_MICHAEL_MIC_FAILURE:
  1757. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1758. break;
  1759. #ifndef CONFIG_NO_SCAN_PROCESSING
  1760. case EVENT_SCAN_RESULTS:
  1761. wpa_supplicant_event_scan_results(wpa_s, data);
  1762. break;
  1763. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1764. case EVENT_ASSOCINFO:
  1765. wpa_supplicant_event_associnfo(wpa_s, data);
  1766. break;
  1767. case EVENT_INTERFACE_STATUS:
  1768. wpa_supplicant_event_interface_status(wpa_s, data);
  1769. break;
  1770. case EVENT_PMKID_CANDIDATE:
  1771. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1772. break;
  1773. #ifdef CONFIG_PEERKEY
  1774. case EVENT_STKSTART:
  1775. wpa_supplicant_event_stkstart(wpa_s, data);
  1776. break;
  1777. #endif /* CONFIG_PEERKEY */
  1778. #ifdef CONFIG_TDLS
  1779. case EVENT_TDLS:
  1780. wpa_supplicant_event_tdls(wpa_s, data);
  1781. break;
  1782. #endif /* CONFIG_TDLS */
  1783. #ifdef CONFIG_IEEE80211R
  1784. case EVENT_FT_RESPONSE:
  1785. wpa_supplicant_event_ft_response(wpa_s, data);
  1786. break;
  1787. #endif /* CONFIG_IEEE80211R */
  1788. #ifdef CONFIG_IBSS_RSN
  1789. case EVENT_IBSS_RSN_START:
  1790. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1791. break;
  1792. #endif /* CONFIG_IBSS_RSN */
  1793. case EVENT_ASSOC_REJECT:
  1794. if (data->assoc_reject.bssid)
  1795. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1796. "bssid=" MACSTR " status_code=%u",
  1797. MAC2STR(data->assoc_reject.bssid),
  1798. data->assoc_reject.status_code);
  1799. else
  1800. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1801. "status_code=%u",
  1802. data->assoc_reject.status_code);
  1803. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1804. sme_event_assoc_reject(wpa_s, data);
  1805. break;
  1806. case EVENT_AUTH_TIMED_OUT:
  1807. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1808. sme_event_auth_timed_out(wpa_s, data);
  1809. break;
  1810. case EVENT_ASSOC_TIMED_OUT:
  1811. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1812. sme_event_assoc_timed_out(wpa_s, data);
  1813. break;
  1814. case EVENT_TX_STATUS:
  1815. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
  1816. " type=%d stype=%d",
  1817. MAC2STR(data->tx_status.dst),
  1818. data->tx_status.type, data->tx_status.stype);
  1819. #ifdef CONFIG_AP
  1820. if (wpa_s->ap_iface == NULL) {
  1821. #ifdef CONFIG_OFFCHANNEL
  1822. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1823. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  1824. offchannel_send_action_tx_status(
  1825. wpa_s, data->tx_status.dst,
  1826. data->tx_status.data,
  1827. data->tx_status.data_len,
  1828. data->tx_status.ack ?
  1829. OFFCHANNEL_SEND_ACTION_SUCCESS :
  1830. OFFCHANNEL_SEND_ACTION_NO_ACK);
  1831. #endif /* CONFIG_OFFCHANNEL */
  1832. break;
  1833. }
  1834. #endif /* CONFIG_AP */
  1835. #ifdef CONFIG_OFFCHANNEL
  1836. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
  1837. MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
  1838. /*
  1839. * Catch TX status events for Action frames we sent via group
  1840. * interface in GO mode.
  1841. */
  1842. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1843. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  1844. os_memcmp(wpa_s->parent->pending_action_dst,
  1845. data->tx_status.dst, ETH_ALEN) == 0) {
  1846. offchannel_send_action_tx_status(
  1847. wpa_s->parent, data->tx_status.dst,
  1848. data->tx_status.data,
  1849. data->tx_status.data_len,
  1850. data->tx_status.ack ?
  1851. OFFCHANNEL_SEND_ACTION_SUCCESS :
  1852. OFFCHANNEL_SEND_ACTION_NO_ACK);
  1853. break;
  1854. }
  1855. #endif /* CONFIG_OFFCHANNEL */
  1856. #ifdef CONFIG_AP
  1857. switch (data->tx_status.type) {
  1858. case WLAN_FC_TYPE_MGMT:
  1859. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1860. data->tx_status.data_len,
  1861. data->tx_status.stype,
  1862. data->tx_status.ack);
  1863. break;
  1864. case WLAN_FC_TYPE_DATA:
  1865. ap_tx_status(wpa_s, data->tx_status.dst,
  1866. data->tx_status.data,
  1867. data->tx_status.data_len,
  1868. data->tx_status.ack);
  1869. break;
  1870. }
  1871. #endif /* CONFIG_AP */
  1872. break;
  1873. #ifdef CONFIG_AP
  1874. case EVENT_DRIVER_CLIENT_POLL_OK:
  1875. ap_client_poll_ok(wpa_s, data->client_poll.addr);
  1876. break;
  1877. case EVENT_RX_FROM_UNKNOWN:
  1878. if (wpa_s->ap_iface == NULL)
  1879. break;
  1880. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
  1881. data->rx_from_unknown.wds);
  1882. break;
  1883. case EVENT_RX_MGMT:
  1884. if (wpa_s->ap_iface == NULL) {
  1885. #ifdef CONFIG_P2P
  1886. u16 fc, stype;
  1887. const struct ieee80211_mgmt *mgmt;
  1888. mgmt = (const struct ieee80211_mgmt *)
  1889. data->rx_mgmt.frame;
  1890. fc = le_to_host16(mgmt->frame_control);
  1891. stype = WLAN_FC_GET_STYPE(fc);
  1892. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  1893. data->rx_mgmt.frame_len > 24) {
  1894. const u8 *src = mgmt->sa;
  1895. const u8 *ie = mgmt->u.probe_req.variable;
  1896. size_t ie_len = data->rx_mgmt.frame_len -
  1897. (mgmt->u.probe_req.variable -
  1898. data->rx_mgmt.frame);
  1899. wpas_p2p_probe_req_rx(wpa_s, src, mgmt->da,
  1900. mgmt->bssid, ie, ie_len);
  1901. break;
  1902. }
  1903. #endif /* CONFIG_P2P */
  1904. wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
  1905. "management frame in non-AP mode");
  1906. break;
  1907. }
  1908. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1909. break;
  1910. #endif /* CONFIG_AP */
  1911. case EVENT_RX_ACTION:
  1912. wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
  1913. " Category=%u DataLen=%d freq=%d MHz",
  1914. MAC2STR(data->rx_action.sa),
  1915. data->rx_action.category, (int) data->rx_action.len,
  1916. data->rx_action.freq);
  1917. #ifdef CONFIG_IEEE80211R
  1918. if (data->rx_action.category == WLAN_ACTION_FT) {
  1919. ft_rx_action(wpa_s, data->rx_action.data,
  1920. data->rx_action.len);
  1921. break;
  1922. }
  1923. #endif /* CONFIG_IEEE80211R */
  1924. #ifdef CONFIG_IEEE80211W
  1925. #ifdef CONFIG_SME
  1926. if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
  1927. sme_sa_query_rx(wpa_s, data->rx_action.sa,
  1928. data->rx_action.data,
  1929. data->rx_action.len);
  1930. break;
  1931. }
  1932. #endif /* CONFIG_SME */
  1933. #endif /* CONFIG_IEEE80211W */
  1934. #ifdef CONFIG_GAS
  1935. if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
  1936. gas_query_rx(wpa_s->gas, data->rx_action.da,
  1937. data->rx_action.sa, data->rx_action.bssid,
  1938. data->rx_action.data, data->rx_action.len,
  1939. data->rx_action.freq) == 0)
  1940. break;
  1941. #endif /* CONFIG_GAS */
  1942. if (data->rx_action.category == WLAN_ACTION_WNM) {
  1943. wnm_action_rx(wpa_s, &data->rx_action);
  1944. break;
  1945. }
  1946. #ifdef CONFIG_TDLS
  1947. if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
  1948. data->rx_action.len >= 4 &&
  1949. data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
  1950. wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
  1951. "Response from " MACSTR,
  1952. MAC2STR(data->rx_action.sa));
  1953. break;
  1954. }
  1955. #endif /* CONFIG_TDLS */
  1956. #ifdef CONFIG_P2P
  1957. wpas_p2p_rx_action(wpa_s, data->rx_action.da,
  1958. data->rx_action.sa,
  1959. data->rx_action.bssid,
  1960. data->rx_action.category,
  1961. data->rx_action.data,
  1962. data->rx_action.len, data->rx_action.freq);
  1963. #endif /* CONFIG_P2P */
  1964. break;
  1965. case EVENT_RX_PROBE_REQ:
  1966. if (data->rx_probe_req.sa == NULL ||
  1967. data->rx_probe_req.ie == NULL)
  1968. break;
  1969. #ifdef CONFIG_AP
  1970. if (wpa_s->ap_iface) {
  1971. hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
  1972. data->rx_probe_req.sa,
  1973. data->rx_probe_req.da,
  1974. data->rx_probe_req.bssid,
  1975. data->rx_probe_req.ie,
  1976. data->rx_probe_req.ie_len);
  1977. break;
  1978. }
  1979. #endif /* CONFIG_AP */
  1980. #ifdef CONFIG_P2P
  1981. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  1982. data->rx_probe_req.da,
  1983. data->rx_probe_req.bssid,
  1984. data->rx_probe_req.ie,
  1985. data->rx_probe_req.ie_len);
  1986. #endif /* CONFIG_P2P */
  1987. break;
  1988. case EVENT_REMAIN_ON_CHANNEL:
  1989. #ifdef CONFIG_OFFCHANNEL
  1990. offchannel_remain_on_channel_cb(
  1991. wpa_s, data->remain_on_channel.freq,
  1992. data->remain_on_channel.duration);
  1993. #endif /* CONFIG_OFFCHANNEL */
  1994. #ifdef CONFIG_P2P
  1995. wpas_p2p_remain_on_channel_cb(
  1996. wpa_s, data->remain_on_channel.freq,
  1997. data->remain_on_channel.duration);
  1998. #endif /* CONFIG_P2P */
  1999. break;
  2000. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  2001. #ifdef CONFIG_OFFCHANNEL
  2002. offchannel_cancel_remain_on_channel_cb(
  2003. wpa_s, data->remain_on_channel.freq);
  2004. #endif /* CONFIG_OFFCHANNEL */
  2005. #ifdef CONFIG_P2P
  2006. wpas_p2p_cancel_remain_on_channel_cb(
  2007. wpa_s, data->remain_on_channel.freq);
  2008. #endif /* CONFIG_P2P */
  2009. break;
  2010. #ifdef CONFIG_P2P
  2011. case EVENT_P2P_DEV_FOUND: {
  2012. struct p2p_peer_info peer_info;
  2013. os_memset(&peer_info, 0, sizeof(peer_info));
  2014. if (data->p2p_dev_found.dev_addr)
  2015. os_memcpy(peer_info.p2p_device_addr,
  2016. data->p2p_dev_found.dev_addr, ETH_ALEN);
  2017. if (data->p2p_dev_found.pri_dev_type)
  2018. os_memcpy(peer_info.pri_dev_type,
  2019. data->p2p_dev_found.pri_dev_type,
  2020. sizeof(peer_info.pri_dev_type));
  2021. if (data->p2p_dev_found.dev_name)
  2022. os_strlcpy(peer_info.device_name,
  2023. data->p2p_dev_found.dev_name,
  2024. sizeof(peer_info.device_name));
  2025. peer_info.config_methods = data->p2p_dev_found.config_methods;
  2026. peer_info.dev_capab = data->p2p_dev_found.dev_capab;
  2027. peer_info.group_capab = data->p2p_dev_found.group_capab;
  2028. /*
  2029. * FIX: new_device=1 is not necessarily correct. We should
  2030. * maintain a P2P peer database in wpa_supplicant and update
  2031. * this information based on whether the peer is truly new.
  2032. */
  2033. wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
  2034. break;
  2035. }
  2036. case EVENT_P2P_GO_NEG_REQ_RX:
  2037. wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
  2038. data->p2p_go_neg_req_rx.dev_passwd_id);
  2039. break;
  2040. case EVENT_P2P_GO_NEG_COMPLETED:
  2041. wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
  2042. break;
  2043. case EVENT_P2P_PROV_DISC_REQUEST:
  2044. wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
  2045. data->p2p_prov_disc_req.config_methods,
  2046. data->p2p_prov_disc_req.dev_addr,
  2047. data->p2p_prov_disc_req.pri_dev_type,
  2048. data->p2p_prov_disc_req.dev_name,
  2049. data->p2p_prov_disc_req.supp_config_methods,
  2050. data->p2p_prov_disc_req.dev_capab,
  2051. data->p2p_prov_disc_req.group_capab);
  2052. break;
  2053. case EVENT_P2P_PROV_DISC_RESPONSE:
  2054. wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
  2055. data->p2p_prov_disc_resp.config_methods);
  2056. break;
  2057. case EVENT_P2P_SD_REQUEST:
  2058. wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
  2059. data->p2p_sd_req.sa,
  2060. data->p2p_sd_req.dialog_token,
  2061. data->p2p_sd_req.update_indic,
  2062. data->p2p_sd_req.tlvs,
  2063. data->p2p_sd_req.tlvs_len);
  2064. break;
  2065. case EVENT_P2P_SD_RESPONSE:
  2066. wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
  2067. data->p2p_sd_resp.update_indic,
  2068. data->p2p_sd_resp.tlvs,
  2069. data->p2p_sd_resp.tlvs_len);
  2070. break;
  2071. #endif /* CONFIG_P2P */
  2072. case EVENT_EAPOL_RX:
  2073. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  2074. data->eapol_rx.data,
  2075. data->eapol_rx.data_len);
  2076. break;
  2077. case EVENT_SIGNAL_CHANGE:
  2078. bgscan_notify_signal_change(
  2079. wpa_s, data->signal_change.above_threshold,
  2080. data->signal_change.current_signal,
  2081. data->signal_change.current_noise,
  2082. data->signal_change.current_txrate);
  2083. break;
  2084. case EVENT_INTERFACE_ENABLED:
  2085. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
  2086. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2087. #ifdef CONFIG_AP
  2088. if (!wpa_s->ap_iface) {
  2089. wpa_supplicant_set_state(wpa_s,
  2090. WPA_DISCONNECTED);
  2091. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2092. } else
  2093. wpa_supplicant_set_state(wpa_s,
  2094. WPA_COMPLETED);
  2095. #else /* CONFIG_AP */
  2096. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2097. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2098. #endif /* CONFIG_AP */
  2099. }
  2100. break;
  2101. case EVENT_INTERFACE_DISABLED:
  2102. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
  2103. wpa_supplicant_mark_disassoc(wpa_s);
  2104. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2105. break;
  2106. case EVENT_CHANNEL_LIST_CHANGED:
  2107. if (wpa_s->drv_priv == NULL)
  2108. break; /* Ignore event during drv initialization */
  2109. #ifdef CONFIG_P2P
  2110. wpas_p2p_update_channel_list(wpa_s);
  2111. #endif /* CONFIG_P2P */
  2112. break;
  2113. case EVENT_INTERFACE_UNAVAILABLE:
  2114. #ifdef CONFIG_P2P
  2115. wpas_p2p_interface_unavailable(wpa_s);
  2116. #endif /* CONFIG_P2P */
  2117. break;
  2118. case EVENT_BEST_CHANNEL:
  2119. wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
  2120. "(%d %d %d)",
  2121. data->best_chan.freq_24, data->best_chan.freq_5,
  2122. data->best_chan.freq_overall);
  2123. wpa_s->best_24_freq = data->best_chan.freq_24;
  2124. wpa_s->best_5_freq = data->best_chan.freq_5;
  2125. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  2126. #ifdef CONFIG_P2P
  2127. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  2128. data->best_chan.freq_5,
  2129. data->best_chan.freq_overall);
  2130. #endif /* CONFIG_P2P */
  2131. break;
  2132. case EVENT_UNPROT_DEAUTH:
  2133. wpa_supplicant_event_unprot_deauth(wpa_s,
  2134. &data->unprot_deauth);
  2135. break;
  2136. case EVENT_UNPROT_DISASSOC:
  2137. wpa_supplicant_event_unprot_disassoc(wpa_s,
  2138. &data->unprot_disassoc);
  2139. break;
  2140. case EVENT_STATION_LOW_ACK:
  2141. #ifdef CONFIG_AP
  2142. if (wpa_s->ap_iface && data)
  2143. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  2144. data->low_ack.addr);
  2145. #endif /* CONFIG_AP */
  2146. #ifdef CONFIG_TDLS
  2147. if (data)
  2148. wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
  2149. #endif /* CONFIG_TDLS */
  2150. break;
  2151. case EVENT_IBSS_PEER_LOST:
  2152. #ifdef CONFIG_IBSS_RSN
  2153. ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
  2154. #endif /* CONFIG_IBSS_RSN */
  2155. break;
  2156. case EVENT_DRIVER_GTK_REKEY:
  2157. if (os_memcmp(data->driver_gtk_rekey.bssid,
  2158. wpa_s->bssid, ETH_ALEN))
  2159. break;
  2160. if (!wpa_s->wpa)
  2161. break;
  2162. wpa_sm_update_replay_ctr(wpa_s->wpa,
  2163. data->driver_gtk_rekey.replay_ctr);
  2164. break;
  2165. case EVENT_SCHED_SCAN_STOPPED:
  2166. wpa_s->sched_scanning = 0;
  2167. wpa_supplicant_notify_scanning(wpa_s, 0);
  2168. /*
  2169. * If we timed out, start a new sched scan to continue
  2170. * searching for more SSIDs.
  2171. */
  2172. if (wpa_s->sched_scan_timed_out)
  2173. wpa_supplicant_req_sched_scan(wpa_s);
  2174. break;
  2175. case EVENT_WPS_BUTTON_PUSHED:
  2176. #ifdef CONFIG_WPS
  2177. wpas_wps_start_pbc(wpa_s, NULL, 0);
  2178. #endif /* CONFIG_WPS */
  2179. break;
  2180. default:
  2181. wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
  2182. break;
  2183. }
  2184. }