events.c 68 KB

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