events.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  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. return 0;
  918. if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
  919. wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
  920. return -1;
  921. }
  922. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  923. } else {
  924. wpa_scan_results_free(scan_res);
  925. wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
  926. ssid = wpa_supplicant_pick_new_network(wpa_s);
  927. if (ssid) {
  928. wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
  929. wpa_supplicant_associate(wpa_s, NULL, ssid);
  930. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  931. } else {
  932. int timeout_sec = wpa_s->scan_interval;
  933. int timeout_usec = 0;
  934. #ifdef CONFIG_P2P
  935. if (wpa_s->p2p_in_provisioning) {
  936. /*
  937. * Use shorter wait during P2P Provisioning
  938. * state to speed up group formation.
  939. */
  940. timeout_sec = 0;
  941. timeout_usec = 250000;
  942. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  943. timeout_usec);
  944. return 0;
  945. }
  946. #endif /* CONFIG_P2P */
  947. if (wpa_supplicant_req_sched_scan(wpa_s))
  948. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  949. timeout_usec);
  950. }
  951. }
  952. return 0;
  953. }
  954. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  955. union wpa_event_data *data)
  956. {
  957. const char *rn, *rn2;
  958. struct wpa_supplicant *ifs;
  959. if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
  960. /*
  961. * If no scan results could be fetched, then no need to
  962. * notify those interfaces that did not actually request
  963. * this scan.
  964. */
  965. return;
  966. }
  967. /*
  968. * Check other interfaces to see if they have the same radio-name. If
  969. * so, they get updated with this same scan info.
  970. */
  971. if (!wpa_s->driver->get_radio_name)
  972. return;
  973. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  974. if (rn == NULL || rn[0] == '\0')
  975. return;
  976. wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
  977. "sharing same radio (%s) in event_scan_results", rn);
  978. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  979. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  980. continue;
  981. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  982. if (rn2 && os_strcmp(rn, rn2) == 0) {
  983. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  984. "sibling", ifs->ifname);
  985. _wpa_supplicant_event_scan_results(ifs, data);
  986. }
  987. }
  988. }
  989. #endif /* CONFIG_NO_SCAN_PROCESSING */
  990. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  991. union wpa_event_data *data)
  992. {
  993. int l, len, found = 0, wpa_found, rsn_found;
  994. const u8 *p;
  995. wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
  996. if (data->assoc_info.req_ies)
  997. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  998. data->assoc_info.req_ies_len);
  999. if (data->assoc_info.resp_ies) {
  1000. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  1001. data->assoc_info.resp_ies_len);
  1002. #ifdef CONFIG_TDLS
  1003. wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
  1004. data->assoc_info.resp_ies_len);
  1005. #endif /* CONFIG_TDLS */
  1006. }
  1007. if (data->assoc_info.beacon_ies)
  1008. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  1009. data->assoc_info.beacon_ies,
  1010. data->assoc_info.beacon_ies_len);
  1011. if (data->assoc_info.freq)
  1012. wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
  1013. data->assoc_info.freq);
  1014. p = data->assoc_info.req_ies;
  1015. l = data->assoc_info.req_ies_len;
  1016. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  1017. while (p && l >= 2) {
  1018. len = p[1] + 2;
  1019. if (len > l) {
  1020. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1021. p, l);
  1022. break;
  1023. }
  1024. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1025. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  1026. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  1027. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  1028. break;
  1029. found = 1;
  1030. wpa_find_assoc_pmkid(wpa_s);
  1031. break;
  1032. }
  1033. l -= len;
  1034. p += len;
  1035. }
  1036. if (!found && data->assoc_info.req_ies)
  1037. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1038. #ifdef CONFIG_IEEE80211R
  1039. #ifdef CONFIG_SME
  1040. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  1041. u8 bssid[ETH_ALEN];
  1042. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1043. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1044. data->assoc_info.resp_ies,
  1045. data->assoc_info.resp_ies_len,
  1046. bssid) < 0) {
  1047. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1048. "Reassociation Response failed");
  1049. wpa_supplicant_deauthenticate(
  1050. wpa_s, WLAN_REASON_INVALID_IE);
  1051. return -1;
  1052. }
  1053. }
  1054. p = data->assoc_info.resp_ies;
  1055. l = data->assoc_info.resp_ies_len;
  1056. #ifdef CONFIG_WPS_STRICT
  1057. if (p && wpa_s->current_ssid &&
  1058. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1059. struct wpabuf *wps;
  1060. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  1061. if (wps == NULL) {
  1062. wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
  1063. "include WPS IE in (Re)Association Response");
  1064. return -1;
  1065. }
  1066. if (wps_validate_assoc_resp(wps) < 0) {
  1067. wpabuf_free(wps);
  1068. wpa_supplicant_deauthenticate(
  1069. wpa_s, WLAN_REASON_INVALID_IE);
  1070. return -1;
  1071. }
  1072. wpabuf_free(wps);
  1073. }
  1074. #endif /* CONFIG_WPS_STRICT */
  1075. /* Go through the IEs and make a copy of the MDIE, if present. */
  1076. while (p && l >= 2) {
  1077. len = p[1] + 2;
  1078. if (len > l) {
  1079. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1080. p, l);
  1081. break;
  1082. }
  1083. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  1084. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  1085. wpa_s->sme.ft_used = 1;
  1086. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  1087. MOBILITY_DOMAIN_ID_LEN);
  1088. break;
  1089. }
  1090. l -= len;
  1091. p += len;
  1092. }
  1093. #endif /* CONFIG_SME */
  1094. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  1095. data->assoc_info.resp_ies_len);
  1096. #endif /* CONFIG_IEEE80211R */
  1097. /* WPA/RSN IE from Beacon/ProbeResp */
  1098. p = data->assoc_info.beacon_ies;
  1099. l = data->assoc_info.beacon_ies_len;
  1100. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  1101. */
  1102. wpa_found = rsn_found = 0;
  1103. while (p && l >= 2) {
  1104. len = p[1] + 2;
  1105. if (len > l) {
  1106. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  1107. p, l);
  1108. break;
  1109. }
  1110. if (!wpa_found &&
  1111. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1112. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  1113. wpa_found = 1;
  1114. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  1115. }
  1116. if (!rsn_found &&
  1117. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  1118. rsn_found = 1;
  1119. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  1120. }
  1121. l -= len;
  1122. p += len;
  1123. }
  1124. if (!wpa_found && data->assoc_info.beacon_ies)
  1125. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  1126. if (!rsn_found && data->assoc_info.beacon_ies)
  1127. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  1128. if (wpa_found || rsn_found)
  1129. wpa_s->ap_ies_from_associnfo = 1;
  1130. if (wpa_s->assoc_freq && data->assoc_info.freq &&
  1131. wpa_s->assoc_freq != data->assoc_info.freq) {
  1132. wpa_printf(MSG_DEBUG, "Operating frequency changed from "
  1133. "%u to %u MHz",
  1134. wpa_s->assoc_freq, data->assoc_info.freq);
  1135. wpa_supplicant_update_scan_results(wpa_s);
  1136. }
  1137. wpa_s->assoc_freq = data->assoc_info.freq;
  1138. return 0;
  1139. }
  1140. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  1141. union wpa_event_data *data)
  1142. {
  1143. u8 bssid[ETH_ALEN];
  1144. int ft_completed;
  1145. int bssid_changed;
  1146. struct wpa_driver_capa capa;
  1147. #ifdef CONFIG_AP
  1148. if (wpa_s->ap_iface) {
  1149. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  1150. data->assoc_info.addr,
  1151. data->assoc_info.req_ies,
  1152. data->assoc_info.req_ies_len,
  1153. data->assoc_info.reassoc);
  1154. return;
  1155. }
  1156. #endif /* CONFIG_AP */
  1157. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  1158. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  1159. return;
  1160. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  1161. if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  1162. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
  1163. wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  1164. MACSTR, MAC2STR(bssid));
  1165. random_add_randomness(bssid, ETH_ALEN);
  1166. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  1167. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  1168. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1169. if (bssid_changed)
  1170. wpas_notify_bssid_changed(wpa_s);
  1171. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  1172. wpa_clear_keys(wpa_s, bssid);
  1173. }
  1174. if (wpa_supplicant_select_config(wpa_s) < 0) {
  1175. wpa_supplicant_disassociate(
  1176. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1177. return;
  1178. }
  1179. if (wpa_s->current_ssid) {
  1180. struct wpa_bss *bss = NULL;
  1181. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1182. if (ssid->ssid_len > 0)
  1183. bss = wpa_bss_get(wpa_s, bssid,
  1184. ssid->ssid, ssid->ssid_len);
  1185. if (!bss)
  1186. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1187. if (bss)
  1188. wpa_s->current_bss = bss;
  1189. }
  1190. }
  1191. #ifdef CONFIG_SME
  1192. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1193. wpa_s->sme.prev_bssid_set = 1;
  1194. #endif /* CONFIG_SME */
  1195. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1196. if (wpa_s->current_ssid) {
  1197. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1198. * initialized before association, but for other modes,
  1199. * initialize PC/SC here, if the current configuration needs
  1200. * smartcard or SIM/USIM. */
  1201. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1202. }
  1203. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1204. if (wpa_s->l2)
  1205. l2_packet_notify_auth_start(wpa_s->l2);
  1206. /*
  1207. * Set portEnabled first to FALSE in order to get EAP state machine out
  1208. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1209. * state machine may transit to AUTHENTICATING state based on obsolete
  1210. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1211. * AUTHENTICATED without ever giving chance to EAP state machine to
  1212. * reset the state.
  1213. */
  1214. if (!ft_completed) {
  1215. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1216. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1217. }
  1218. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1219. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1220. /* 802.1X::portControl = Auto */
  1221. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1222. wpa_s->eapol_received = 0;
  1223. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1224. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1225. (wpa_s->current_ssid &&
  1226. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1227. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1228. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1229. } else if (!ft_completed) {
  1230. /* Timeout for receiving the first EAPOL packet */
  1231. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1232. }
  1233. wpa_supplicant_cancel_scan(wpa_s);
  1234. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1235. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1236. /*
  1237. * We are done; the driver will take care of RSN 4-way
  1238. * handshake.
  1239. */
  1240. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1241. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1242. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1243. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1244. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1245. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1246. /*
  1247. * The driver will take care of RSN 4-way handshake, so we need
  1248. * to allow EAPOL supplicant to complete its work without
  1249. * waiting for WPA supplicant.
  1250. */
  1251. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1252. } else if (ft_completed) {
  1253. /*
  1254. * FT protocol completed - make sure EAPOL state machine ends
  1255. * up in authenticated.
  1256. */
  1257. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1258. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1259. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1260. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1261. }
  1262. if (wpa_s->pending_eapol_rx) {
  1263. struct os_time now, age;
  1264. os_get_time(&now);
  1265. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1266. if (age.sec == 0 && age.usec < 100000 &&
  1267. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1268. 0) {
  1269. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
  1270. "frame that was received just before "
  1271. "association notification");
  1272. wpa_supplicant_rx_eapol(
  1273. wpa_s, wpa_s->pending_eapol_rx_src,
  1274. wpabuf_head(wpa_s->pending_eapol_rx),
  1275. wpabuf_len(wpa_s->pending_eapol_rx));
  1276. }
  1277. wpabuf_free(wpa_s->pending_eapol_rx);
  1278. wpa_s->pending_eapol_rx = NULL;
  1279. }
  1280. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1281. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1282. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1283. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1284. /* Set static WEP keys again */
  1285. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1286. }
  1287. #ifdef CONFIG_IBSS_RSN
  1288. if (wpa_s->current_ssid &&
  1289. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  1290. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1291. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
  1292. wpa_s->ibss_rsn == NULL) {
  1293. wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
  1294. if (!wpa_s->ibss_rsn) {
  1295. wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
  1296. wpa_supplicant_deauthenticate(
  1297. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1298. return;
  1299. }
  1300. ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
  1301. }
  1302. #endif /* CONFIG_IBSS_RSN */
  1303. }
  1304. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1305. u16 reason_code)
  1306. {
  1307. const u8 *bssid;
  1308. int authenticating;
  1309. u8 prev_pending_bssid[ETH_ALEN];
  1310. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1311. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1312. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1313. /*
  1314. * At least Host AP driver and a Prism3 card seemed to be
  1315. * generating streams of disconnected events when configuring
  1316. * IBSS for WPA-None. Ignore them for now.
  1317. */
  1318. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
  1319. "IBSS/WPA-None mode");
  1320. return;
  1321. }
  1322. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1323. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1324. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1325. "pre-shared key may be incorrect");
  1326. }
  1327. if (!wpa_s->auto_reconnect_disabled ||
  1328. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  1329. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
  1330. "reconnect (wps=%d)",
  1331. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
  1332. if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1333. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1334. } else {
  1335. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
  1336. "try to re-connect");
  1337. wpa_s->reassociate = 0;
  1338. wpa_s->disconnected = 1;
  1339. }
  1340. bssid = wpa_s->bssid;
  1341. if (is_zero_ether_addr(bssid))
  1342. bssid = wpa_s->pending_bssid;
  1343. wpas_connection_failed(wpa_s, bssid);
  1344. wpa_sm_notify_disassoc(wpa_s->wpa);
  1345. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1346. " reason=%d",
  1347. MAC2STR(bssid), reason_code);
  1348. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1349. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
  1350. wpa_s->keys_cleared = 0;
  1351. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1352. }
  1353. wpa_supplicant_mark_disassoc(wpa_s);
  1354. if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1355. sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
  1356. }
  1357. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1358. void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
  1359. {
  1360. struct wpa_supplicant *wpa_s = eloop_ctx;
  1361. if (!wpa_s->pending_mic_error_report)
  1362. return;
  1363. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
  1364. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1365. wpa_s->pending_mic_error_report = 0;
  1366. }
  1367. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1368. static void
  1369. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1370. union wpa_event_data *data)
  1371. {
  1372. int pairwise;
  1373. struct os_time t;
  1374. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1375. pairwise = (data && data->michael_mic_failure.unicast);
  1376. os_get_time(&t);
  1377. if ((wpa_s->last_michael_mic_error &&
  1378. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1379. wpa_s->pending_mic_error_report) {
  1380. if (wpa_s->pending_mic_error_report) {
  1381. /*
  1382. * Send the pending MIC error report immediately since
  1383. * we are going to start countermeasures and AP better
  1384. * do the same.
  1385. */
  1386. wpa_sm_key_request(wpa_s->wpa, 1,
  1387. wpa_s->pending_mic_error_pairwise);
  1388. }
  1389. /* Send the new MIC error report immediately since we are going
  1390. * to start countermeasures and AP better do the same.
  1391. */
  1392. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1393. /* initialize countermeasures */
  1394. wpa_s->countermeasures = 1;
  1395. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1396. /*
  1397. * Need to wait for completion of request frame. We do not get
  1398. * any callback for the message completion, so just wait a
  1399. * short while and hope for the best. */
  1400. os_sleep(0, 10000);
  1401. wpa_drv_set_countermeasures(wpa_s, 1);
  1402. wpa_supplicant_deauthenticate(wpa_s,
  1403. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1404. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1405. wpa_s, NULL);
  1406. eloop_register_timeout(60, 0,
  1407. wpa_supplicant_stop_countermeasures,
  1408. wpa_s, NULL);
  1409. /* TODO: mark the AP rejected for 60 second. STA is
  1410. * allowed to associate with another AP.. */
  1411. } else {
  1412. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1413. if (wpa_s->mic_errors_seen) {
  1414. /*
  1415. * Reduce the effectiveness of Michael MIC error
  1416. * reports as a means for attacking against TKIP if
  1417. * more than one MIC failure is noticed with the same
  1418. * PTK. We delay the transmission of the reports by a
  1419. * random time between 0 and 60 seconds in order to
  1420. * force the attacker wait 60 seconds before getting
  1421. * the information on whether a frame resulted in a MIC
  1422. * failure.
  1423. */
  1424. u8 rval[4];
  1425. int sec;
  1426. if (os_get_random(rval, sizeof(rval)) < 0)
  1427. sec = os_random() % 60;
  1428. else
  1429. sec = WPA_GET_BE32(rval) % 60;
  1430. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
  1431. "report %d seconds", sec);
  1432. wpa_s->pending_mic_error_report = 1;
  1433. wpa_s->pending_mic_error_pairwise = pairwise;
  1434. eloop_cancel_timeout(
  1435. wpa_supplicant_delayed_mic_error_report,
  1436. wpa_s, NULL);
  1437. eloop_register_timeout(
  1438. sec, os_random() % 1000000,
  1439. wpa_supplicant_delayed_mic_error_report,
  1440. wpa_s, NULL);
  1441. } else {
  1442. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1443. }
  1444. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1445. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1446. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1447. }
  1448. wpa_s->last_michael_mic_error = t.sec;
  1449. wpa_s->mic_errors_seen++;
  1450. }
  1451. #ifdef CONFIG_TERMINATE_ONLASTIF
  1452. static int any_interfaces(struct wpa_supplicant *head)
  1453. {
  1454. struct wpa_supplicant *wpa_s;
  1455. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1456. if (!wpa_s->interface_removed)
  1457. return 1;
  1458. return 0;
  1459. }
  1460. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1461. static void
  1462. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1463. union wpa_event_data *data)
  1464. {
  1465. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1466. return;
  1467. switch (data->interface_status.ievent) {
  1468. case EVENT_INTERFACE_ADDED:
  1469. if (!wpa_s->interface_removed)
  1470. break;
  1471. wpa_s->interface_removed = 0;
  1472. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
  1473. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1474. wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
  1475. "driver after interface was added");
  1476. }
  1477. break;
  1478. case EVENT_INTERFACE_REMOVED:
  1479. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
  1480. wpa_s->interface_removed = 1;
  1481. wpa_supplicant_mark_disassoc(wpa_s);
  1482. l2_packet_deinit(wpa_s->l2);
  1483. wpa_s->l2 = NULL;
  1484. #ifdef CONFIG_IBSS_RSN
  1485. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1486. wpa_s->ibss_rsn = NULL;
  1487. #endif /* CONFIG_IBSS_RSN */
  1488. #ifdef CONFIG_TERMINATE_ONLASTIF
  1489. /* check if last interface */
  1490. if (!any_interfaces(wpa_s->global->ifaces))
  1491. eloop_terminate();
  1492. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1493. break;
  1494. }
  1495. }
  1496. #ifdef CONFIG_PEERKEY
  1497. static void
  1498. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1499. union wpa_event_data *data)
  1500. {
  1501. if (data == NULL)
  1502. return;
  1503. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1504. }
  1505. #endif /* CONFIG_PEERKEY */
  1506. #ifdef CONFIG_TDLS
  1507. static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
  1508. union wpa_event_data *data)
  1509. {
  1510. if (data == NULL)
  1511. return;
  1512. switch (data->tdls.oper) {
  1513. case TDLS_REQUEST_SETUP:
  1514. wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
  1515. break;
  1516. case TDLS_REQUEST_TEARDOWN:
  1517. wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
  1518. data->tdls.reason_code);
  1519. break;
  1520. }
  1521. }
  1522. #endif /* CONFIG_TDLS */
  1523. #ifdef CONFIG_IEEE80211R
  1524. static void
  1525. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1526. union wpa_event_data *data)
  1527. {
  1528. if (data == NULL)
  1529. return;
  1530. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1531. data->ft_ies.ies_len,
  1532. data->ft_ies.ft_action,
  1533. data->ft_ies.target_ap,
  1534. data->ft_ies.ric_ies,
  1535. data->ft_ies.ric_ies_len) < 0) {
  1536. /* TODO: prevent MLME/driver from trying to associate? */
  1537. }
  1538. }
  1539. #endif /* CONFIG_IEEE80211R */
  1540. #ifdef CONFIG_IBSS_RSN
  1541. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1542. union wpa_event_data *data)
  1543. {
  1544. struct wpa_ssid *ssid;
  1545. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1546. return;
  1547. if (data == NULL)
  1548. return;
  1549. ssid = wpa_s->current_ssid;
  1550. if (ssid == NULL)
  1551. return;
  1552. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  1553. return;
  1554. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1555. }
  1556. #endif /* CONFIG_IBSS_RSN */
  1557. #ifdef CONFIG_IEEE80211R
  1558. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  1559. size_t len)
  1560. {
  1561. const u8 *sta_addr, *target_ap_addr;
  1562. u16 status;
  1563. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  1564. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  1565. return; /* only SME case supported for now */
  1566. if (len < 1 + 2 * ETH_ALEN + 2)
  1567. return;
  1568. if (data[0] != 2)
  1569. return; /* Only FT Action Response is supported for now */
  1570. sta_addr = data + 1;
  1571. target_ap_addr = data + 1 + ETH_ALEN;
  1572. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  1573. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
  1574. MACSTR " TargetAP " MACSTR " status %u",
  1575. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  1576. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  1577. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  1578. " in FT Action Response", MAC2STR(sta_addr));
  1579. return;
  1580. }
  1581. if (status) {
  1582. wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
  1583. "failure (status code %d)", status);
  1584. /* TODO: report error to FT code(?) */
  1585. return;
  1586. }
  1587. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  1588. len - (1 + 2 * ETH_ALEN + 2), 1,
  1589. target_ap_addr, NULL, 0) < 0)
  1590. return;
  1591. #ifdef CONFIG_SME
  1592. {
  1593. struct wpa_bss *bss;
  1594. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  1595. if (bss)
  1596. wpa_s->sme.freq = bss->freq;
  1597. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  1598. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  1599. WLAN_AUTH_FT);
  1600. }
  1601. #endif /* CONFIG_SME */
  1602. }
  1603. #endif /* CONFIG_IEEE80211R */
  1604. static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
  1605. struct unprot_deauth *e)
  1606. {
  1607. #ifdef CONFIG_IEEE80211W
  1608. wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
  1609. "dropped: " MACSTR " -> " MACSTR
  1610. " (reason code %u)",
  1611. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1612. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1613. #endif /* CONFIG_IEEE80211W */
  1614. }
  1615. static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
  1616. struct unprot_disassoc *e)
  1617. {
  1618. #ifdef CONFIG_IEEE80211W
  1619. wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
  1620. "dropped: " MACSTR " -> " MACSTR
  1621. " (reason code %u)",
  1622. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  1623. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  1624. #endif /* CONFIG_IEEE80211W */
  1625. }
  1626. static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
  1627. {
  1628. u8 action, mode;
  1629. const u8 *pos, *end;
  1630. if (rx->data == NULL || rx->len == 0)
  1631. return;
  1632. pos = rx->data;
  1633. end = pos + rx->len;
  1634. action = *pos++;
  1635. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  1636. action, MAC2STR(rx->sa));
  1637. switch (action) {
  1638. case WNM_BSS_TRANS_MGMT_REQ:
  1639. if (pos + 5 > end)
  1640. break;
  1641. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
  1642. "Request: dialog_token=%u request_mode=0x%x "
  1643. "disassoc_timer=%u validity_interval=%u",
  1644. pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
  1645. mode = pos[1];
  1646. pos += 5;
  1647. if (mode & 0x08)
  1648. pos += 12; /* BSS Termination Duration */
  1649. if (mode & 0x10) {
  1650. char url[256];
  1651. if (pos + 1 > end || pos + 1 + pos[0] > end) {
  1652. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
  1653. "Transition Management Request "
  1654. "(URL)");
  1655. break;
  1656. }
  1657. os_memcpy(url, pos + 1, pos[0]);
  1658. url[pos[0]] = '\0';
  1659. wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
  1660. "Imminent - session_info_url=%s", url);
  1661. }
  1662. break;
  1663. }
  1664. }
  1665. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1666. union wpa_event_data *data)
  1667. {
  1668. struct wpa_supplicant *wpa_s = ctx;
  1669. u16 reason_code = 0;
  1670. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  1671. event != EVENT_INTERFACE_ENABLED &&
  1672. event != EVENT_INTERFACE_STATUS) {
  1673. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore event %d while interface is "
  1674. "disabled", event);
  1675. return;
  1676. }
  1677. wpa_dbg(wpa_s, MSG_DEBUG, "Event %d received on interface %s",
  1678. event, wpa_s->ifname);
  1679. switch (event) {
  1680. case EVENT_AUTH:
  1681. sme_event_auth(wpa_s, data);
  1682. break;
  1683. case EVENT_ASSOC:
  1684. wpa_supplicant_event_assoc(wpa_s, data);
  1685. break;
  1686. case EVENT_DISASSOC:
  1687. wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
  1688. if (data) {
  1689. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
  1690. data->disassoc_info.reason_code);
  1691. if (data->disassoc_info.addr)
  1692. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  1693. MAC2STR(data->disassoc_info.addr));
  1694. }
  1695. #ifdef CONFIG_AP
  1696. if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
  1697. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1698. data->disassoc_info.addr);
  1699. break;
  1700. }
  1701. #endif /* CONFIG_AP */
  1702. if (data) {
  1703. reason_code = data->disassoc_info.reason_code;
  1704. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  1705. data->disassoc_info.ie,
  1706. data->disassoc_info.ie_len);
  1707. #ifdef CONFIG_P2P
  1708. wpas_p2p_disassoc_notif(
  1709. wpa_s, data->disassoc_info.addr, reason_code,
  1710. data->disassoc_info.ie,
  1711. data->disassoc_info.ie_len);
  1712. #endif /* CONFIG_P2P */
  1713. }
  1714. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1715. sme_event_disassoc(wpa_s, data);
  1716. /* fall through */
  1717. case EVENT_DEAUTH:
  1718. if (event == EVENT_DEAUTH) {
  1719. wpa_dbg(wpa_s, MSG_DEBUG,
  1720. "Deauthentication notification");
  1721. if (data) {
  1722. reason_code = data->deauth_info.reason_code;
  1723. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
  1724. data->deauth_info.reason_code);
  1725. if (data->deauth_info.addr) {
  1726. wpa_dbg(wpa_s, MSG_DEBUG, " * address "
  1727. MACSTR,
  1728. MAC2STR(data->deauth_info.
  1729. addr));
  1730. }
  1731. wpa_hexdump(MSG_DEBUG,
  1732. "Deauthentication frame IE(s)",
  1733. data->deauth_info.ie,
  1734. data->deauth_info.ie_len);
  1735. #ifdef CONFIG_P2P
  1736. wpas_p2p_deauth_notif(
  1737. wpa_s, data->deauth_info.addr,
  1738. reason_code,
  1739. data->deauth_info.ie,
  1740. data->deauth_info.ie_len);
  1741. #endif /* CONFIG_P2P */
  1742. }
  1743. }
  1744. #ifdef CONFIG_AP
  1745. if (wpa_s->ap_iface && data && data->deauth_info.addr) {
  1746. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1747. data->deauth_info.addr);
  1748. break;
  1749. }
  1750. #endif /* CONFIG_AP */
  1751. wpa_supplicant_event_disassoc(wpa_s, reason_code);
  1752. break;
  1753. case EVENT_MICHAEL_MIC_FAILURE:
  1754. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1755. break;
  1756. #ifndef CONFIG_NO_SCAN_PROCESSING
  1757. case EVENT_SCAN_RESULTS:
  1758. wpa_supplicant_event_scan_results(wpa_s, data);
  1759. break;
  1760. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1761. case EVENT_ASSOCINFO:
  1762. wpa_supplicant_event_associnfo(wpa_s, data);
  1763. break;
  1764. case EVENT_INTERFACE_STATUS:
  1765. wpa_supplicant_event_interface_status(wpa_s, data);
  1766. break;
  1767. case EVENT_PMKID_CANDIDATE:
  1768. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1769. break;
  1770. #ifdef CONFIG_PEERKEY
  1771. case EVENT_STKSTART:
  1772. wpa_supplicant_event_stkstart(wpa_s, data);
  1773. break;
  1774. #endif /* CONFIG_PEERKEY */
  1775. #ifdef CONFIG_TDLS
  1776. case EVENT_TDLS:
  1777. wpa_supplicant_event_tdls(wpa_s, data);
  1778. break;
  1779. #endif /* CONFIG_TDLS */
  1780. #ifdef CONFIG_IEEE80211R
  1781. case EVENT_FT_RESPONSE:
  1782. wpa_supplicant_event_ft_response(wpa_s, data);
  1783. break;
  1784. #endif /* CONFIG_IEEE80211R */
  1785. #ifdef CONFIG_IBSS_RSN
  1786. case EVENT_IBSS_RSN_START:
  1787. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1788. break;
  1789. #endif /* CONFIG_IBSS_RSN */
  1790. case EVENT_ASSOC_REJECT:
  1791. if (data->assoc_reject.bssid)
  1792. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1793. "bssid=" MACSTR " status_code=%u",
  1794. MAC2STR(data->assoc_reject.bssid),
  1795. data->assoc_reject.status_code);
  1796. else
  1797. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  1798. "status_code=%u",
  1799. data->assoc_reject.status_code);
  1800. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1801. sme_event_assoc_reject(wpa_s, data);
  1802. break;
  1803. case EVENT_AUTH_TIMED_OUT:
  1804. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1805. sme_event_auth_timed_out(wpa_s, data);
  1806. break;
  1807. case EVENT_ASSOC_TIMED_OUT:
  1808. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1809. sme_event_assoc_timed_out(wpa_s, data);
  1810. break;
  1811. case EVENT_TX_STATUS:
  1812. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
  1813. " type=%d stype=%d",
  1814. MAC2STR(data->tx_status.dst),
  1815. data->tx_status.type, data->tx_status.stype);
  1816. #ifdef CONFIG_AP
  1817. if (wpa_s->ap_iface == NULL) {
  1818. #ifdef CONFIG_OFFCHANNEL
  1819. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1820. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  1821. offchannel_send_action_tx_status(
  1822. wpa_s, data->tx_status.dst,
  1823. data->tx_status.data,
  1824. data->tx_status.data_len,
  1825. data->tx_status.ack ?
  1826. OFFCHANNEL_SEND_ACTION_SUCCESS :
  1827. OFFCHANNEL_SEND_ACTION_NO_ACK);
  1828. #endif /* CONFIG_OFFCHANNEL */
  1829. break;
  1830. }
  1831. #endif /* CONFIG_AP */
  1832. #ifdef CONFIG_OFFCHANNEL
  1833. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
  1834. MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
  1835. /*
  1836. * Catch TX status events for Action frames we sent via group
  1837. * interface in GO mode.
  1838. */
  1839. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  1840. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  1841. os_memcmp(wpa_s->parent->pending_action_dst,
  1842. data->tx_status.dst, ETH_ALEN) == 0) {
  1843. offchannel_send_action_tx_status(
  1844. wpa_s->parent, data->tx_status.dst,
  1845. data->tx_status.data,
  1846. data->tx_status.data_len,
  1847. data->tx_status.ack ?
  1848. OFFCHANNEL_SEND_ACTION_SUCCESS :
  1849. OFFCHANNEL_SEND_ACTION_NO_ACK);
  1850. break;
  1851. }
  1852. #endif /* CONFIG_OFFCHANNEL */
  1853. #ifdef CONFIG_AP
  1854. switch (data->tx_status.type) {
  1855. case WLAN_FC_TYPE_MGMT:
  1856. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1857. data->tx_status.data_len,
  1858. data->tx_status.stype,
  1859. data->tx_status.ack);
  1860. break;
  1861. case WLAN_FC_TYPE_DATA:
  1862. ap_tx_status(wpa_s, data->tx_status.dst,
  1863. data->tx_status.data,
  1864. data->tx_status.data_len,
  1865. data->tx_status.ack);
  1866. break;
  1867. }
  1868. #endif /* CONFIG_AP */
  1869. break;
  1870. #ifdef CONFIG_AP
  1871. case EVENT_DRIVER_CLIENT_POLL_OK:
  1872. ap_client_poll_ok(wpa_s, data->client_poll.addr);
  1873. break;
  1874. case EVENT_RX_FROM_UNKNOWN:
  1875. if (wpa_s->ap_iface == NULL)
  1876. break;
  1877. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
  1878. data->rx_from_unknown.wds);
  1879. break;
  1880. case EVENT_RX_MGMT:
  1881. if (wpa_s->ap_iface == NULL) {
  1882. #ifdef CONFIG_P2P
  1883. u16 fc, stype;
  1884. const struct ieee80211_mgmt *mgmt;
  1885. mgmt = (const struct ieee80211_mgmt *)
  1886. data->rx_mgmt.frame;
  1887. fc = le_to_host16(mgmt->frame_control);
  1888. stype = WLAN_FC_GET_STYPE(fc);
  1889. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  1890. data->rx_mgmt.frame_len > 24) {
  1891. const u8 *src = mgmt->sa;
  1892. const u8 *ie = mgmt->u.probe_req.variable;
  1893. size_t ie_len = data->rx_mgmt.frame_len -
  1894. (mgmt->u.probe_req.variable -
  1895. data->rx_mgmt.frame);
  1896. wpas_p2p_probe_req_rx(wpa_s, src, mgmt->da,
  1897. mgmt->bssid, ie, ie_len);
  1898. break;
  1899. }
  1900. #endif /* CONFIG_P2P */
  1901. wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
  1902. "management frame in non-AP mode");
  1903. break;
  1904. }
  1905. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1906. break;
  1907. #endif /* CONFIG_AP */
  1908. case EVENT_RX_ACTION:
  1909. wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
  1910. " Category=%u DataLen=%d freq=%d MHz",
  1911. MAC2STR(data->rx_action.sa),
  1912. data->rx_action.category, (int) data->rx_action.len,
  1913. data->rx_action.freq);
  1914. #ifdef CONFIG_IEEE80211R
  1915. if (data->rx_action.category == WLAN_ACTION_FT) {
  1916. ft_rx_action(wpa_s, data->rx_action.data,
  1917. data->rx_action.len);
  1918. break;
  1919. }
  1920. #endif /* CONFIG_IEEE80211R */
  1921. #ifdef CONFIG_IEEE80211W
  1922. #ifdef CONFIG_SME
  1923. if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
  1924. sme_sa_query_rx(wpa_s, data->rx_action.sa,
  1925. data->rx_action.data,
  1926. data->rx_action.len);
  1927. break;
  1928. }
  1929. #endif /* CONFIG_SME */
  1930. #endif /* CONFIG_IEEE80211W */
  1931. #ifdef CONFIG_GAS
  1932. if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
  1933. gas_query_rx(wpa_s->gas, data->rx_action.da,
  1934. data->rx_action.sa, data->rx_action.bssid,
  1935. data->rx_action.data, data->rx_action.len,
  1936. data->rx_action.freq) == 0)
  1937. break;
  1938. #endif /* CONFIG_GAS */
  1939. if (data->rx_action.category == WLAN_ACTION_WNM) {
  1940. wnm_action_rx(wpa_s, &data->rx_action);
  1941. break;
  1942. }
  1943. #ifdef CONFIG_TDLS
  1944. if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
  1945. data->rx_action.len >= 4 &&
  1946. data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
  1947. wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
  1948. "Response from " MACSTR,
  1949. MAC2STR(data->rx_action.sa));
  1950. break;
  1951. }
  1952. #endif /* CONFIG_TDLS */
  1953. #ifdef CONFIG_P2P
  1954. wpas_p2p_rx_action(wpa_s, data->rx_action.da,
  1955. data->rx_action.sa,
  1956. data->rx_action.bssid,
  1957. data->rx_action.category,
  1958. data->rx_action.data,
  1959. data->rx_action.len, data->rx_action.freq);
  1960. #endif /* CONFIG_P2P */
  1961. break;
  1962. case EVENT_RX_PROBE_REQ:
  1963. if (data->rx_probe_req.sa == NULL ||
  1964. data->rx_probe_req.ie == NULL)
  1965. break;
  1966. #ifdef CONFIG_AP
  1967. if (wpa_s->ap_iface) {
  1968. hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
  1969. data->rx_probe_req.sa,
  1970. data->rx_probe_req.da,
  1971. data->rx_probe_req.bssid,
  1972. data->rx_probe_req.ie,
  1973. data->rx_probe_req.ie_len);
  1974. break;
  1975. }
  1976. #endif /* CONFIG_AP */
  1977. #ifdef CONFIG_P2P
  1978. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  1979. data->rx_probe_req.da,
  1980. data->rx_probe_req.bssid,
  1981. data->rx_probe_req.ie,
  1982. data->rx_probe_req.ie_len);
  1983. #endif /* CONFIG_P2P */
  1984. break;
  1985. case EVENT_REMAIN_ON_CHANNEL:
  1986. #ifdef CONFIG_OFFCHANNEL
  1987. offchannel_remain_on_channel_cb(
  1988. wpa_s, data->remain_on_channel.freq,
  1989. data->remain_on_channel.duration);
  1990. #endif /* CONFIG_OFFCHANNEL */
  1991. #ifdef CONFIG_P2P
  1992. wpas_p2p_remain_on_channel_cb(
  1993. wpa_s, data->remain_on_channel.freq,
  1994. data->remain_on_channel.duration);
  1995. #endif /* CONFIG_P2P */
  1996. break;
  1997. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  1998. #ifdef CONFIG_OFFCHANNEL
  1999. offchannel_cancel_remain_on_channel_cb(
  2000. wpa_s, data->remain_on_channel.freq);
  2001. #endif /* CONFIG_OFFCHANNEL */
  2002. #ifdef CONFIG_P2P
  2003. wpas_p2p_cancel_remain_on_channel_cb(
  2004. wpa_s, data->remain_on_channel.freq);
  2005. #endif /* CONFIG_P2P */
  2006. break;
  2007. #ifdef CONFIG_P2P
  2008. case EVENT_P2P_DEV_FOUND: {
  2009. struct p2p_peer_info peer_info;
  2010. os_memset(&peer_info, 0, sizeof(peer_info));
  2011. if (data->p2p_dev_found.dev_addr)
  2012. os_memcpy(peer_info.p2p_device_addr,
  2013. data->p2p_dev_found.dev_addr, ETH_ALEN);
  2014. if (data->p2p_dev_found.pri_dev_type)
  2015. os_memcpy(peer_info.pri_dev_type,
  2016. data->p2p_dev_found.pri_dev_type,
  2017. sizeof(peer_info.pri_dev_type));
  2018. if (data->p2p_dev_found.dev_name)
  2019. os_strlcpy(peer_info.device_name,
  2020. data->p2p_dev_found.dev_name,
  2021. sizeof(peer_info.device_name));
  2022. peer_info.config_methods = data->p2p_dev_found.config_methods;
  2023. peer_info.dev_capab = data->p2p_dev_found.dev_capab;
  2024. peer_info.group_capab = data->p2p_dev_found.group_capab;
  2025. /*
  2026. * FIX: new_device=1 is not necessarily correct. We should
  2027. * maintain a P2P peer database in wpa_supplicant and update
  2028. * this information based on whether the peer is truly new.
  2029. */
  2030. wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
  2031. break;
  2032. }
  2033. case EVENT_P2P_GO_NEG_REQ_RX:
  2034. wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
  2035. data->p2p_go_neg_req_rx.dev_passwd_id);
  2036. break;
  2037. case EVENT_P2P_GO_NEG_COMPLETED:
  2038. wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
  2039. break;
  2040. case EVENT_P2P_PROV_DISC_REQUEST:
  2041. wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
  2042. data->p2p_prov_disc_req.config_methods,
  2043. data->p2p_prov_disc_req.dev_addr,
  2044. data->p2p_prov_disc_req.pri_dev_type,
  2045. data->p2p_prov_disc_req.dev_name,
  2046. data->p2p_prov_disc_req.supp_config_methods,
  2047. data->p2p_prov_disc_req.dev_capab,
  2048. data->p2p_prov_disc_req.group_capab);
  2049. break;
  2050. case EVENT_P2P_PROV_DISC_RESPONSE:
  2051. wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
  2052. data->p2p_prov_disc_resp.config_methods);
  2053. break;
  2054. case EVENT_P2P_SD_REQUEST:
  2055. wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
  2056. data->p2p_sd_req.sa,
  2057. data->p2p_sd_req.dialog_token,
  2058. data->p2p_sd_req.update_indic,
  2059. data->p2p_sd_req.tlvs,
  2060. data->p2p_sd_req.tlvs_len);
  2061. break;
  2062. case EVENT_P2P_SD_RESPONSE:
  2063. wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
  2064. data->p2p_sd_resp.update_indic,
  2065. data->p2p_sd_resp.tlvs,
  2066. data->p2p_sd_resp.tlvs_len);
  2067. break;
  2068. #endif /* CONFIG_P2P */
  2069. case EVENT_EAPOL_RX:
  2070. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  2071. data->eapol_rx.data,
  2072. data->eapol_rx.data_len);
  2073. break;
  2074. case EVENT_SIGNAL_CHANGE:
  2075. bgscan_notify_signal_change(
  2076. wpa_s, data->signal_change.above_threshold,
  2077. data->signal_change.current_signal,
  2078. data->signal_change.current_noise,
  2079. data->signal_change.current_txrate);
  2080. break;
  2081. case EVENT_INTERFACE_ENABLED:
  2082. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
  2083. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2084. #ifdef CONFIG_AP
  2085. if (!wpa_s->ap_iface) {
  2086. wpa_supplicant_set_state(wpa_s,
  2087. WPA_DISCONNECTED);
  2088. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2089. } else
  2090. wpa_supplicant_set_state(wpa_s,
  2091. WPA_COMPLETED);
  2092. #else /* CONFIG_AP */
  2093. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2094. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2095. #endif /* CONFIG_AP */
  2096. }
  2097. break;
  2098. case EVENT_INTERFACE_DISABLED:
  2099. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
  2100. wpa_supplicant_mark_disassoc(wpa_s);
  2101. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2102. break;
  2103. case EVENT_CHANNEL_LIST_CHANGED:
  2104. if (wpa_s->drv_priv == NULL)
  2105. break; /* Ignore event during drv initialization */
  2106. #ifdef CONFIG_P2P
  2107. wpas_p2p_update_channel_list(wpa_s);
  2108. #endif /* CONFIG_P2P */
  2109. break;
  2110. case EVENT_INTERFACE_UNAVAILABLE:
  2111. #ifdef CONFIG_P2P
  2112. wpas_p2p_interface_unavailable(wpa_s);
  2113. #endif /* CONFIG_P2P */
  2114. break;
  2115. case EVENT_BEST_CHANNEL:
  2116. wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
  2117. "(%d %d %d)",
  2118. data->best_chan.freq_24, data->best_chan.freq_5,
  2119. data->best_chan.freq_overall);
  2120. wpa_s->best_24_freq = data->best_chan.freq_24;
  2121. wpa_s->best_5_freq = data->best_chan.freq_5;
  2122. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  2123. #ifdef CONFIG_P2P
  2124. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  2125. data->best_chan.freq_5,
  2126. data->best_chan.freq_overall);
  2127. #endif /* CONFIG_P2P */
  2128. break;
  2129. case EVENT_UNPROT_DEAUTH:
  2130. wpa_supplicant_event_unprot_deauth(wpa_s,
  2131. &data->unprot_deauth);
  2132. break;
  2133. case EVENT_UNPROT_DISASSOC:
  2134. wpa_supplicant_event_unprot_disassoc(wpa_s,
  2135. &data->unprot_disassoc);
  2136. break;
  2137. case EVENT_STATION_LOW_ACK:
  2138. #ifdef CONFIG_AP
  2139. if (wpa_s->ap_iface && data)
  2140. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  2141. data->low_ack.addr);
  2142. #endif /* CONFIG_AP */
  2143. #ifdef CONFIG_TDLS
  2144. if (data)
  2145. wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
  2146. #endif /* CONFIG_TDLS */
  2147. break;
  2148. case EVENT_IBSS_PEER_LOST:
  2149. #ifdef CONFIG_IBSS_RSN
  2150. ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
  2151. #endif /* CONFIG_IBSS_RSN */
  2152. break;
  2153. case EVENT_DRIVER_GTK_REKEY:
  2154. if (os_memcmp(data->driver_gtk_rekey.bssid,
  2155. wpa_s->bssid, ETH_ALEN))
  2156. break;
  2157. if (!wpa_s->wpa)
  2158. break;
  2159. wpa_sm_update_replay_ctr(wpa_s->wpa,
  2160. data->driver_gtk_rekey.replay_ctr);
  2161. break;
  2162. case EVENT_SCHED_SCAN_STOPPED:
  2163. wpa_s->sched_scanning = 0;
  2164. wpa_supplicant_notify_scanning(wpa_s, 0);
  2165. /*
  2166. * If we timed out, start a new sched scan to continue
  2167. * searching for more SSIDs.
  2168. */
  2169. if (wpa_s->sched_scan_timed_out)
  2170. wpa_supplicant_req_sched_scan(wpa_s);
  2171. break;
  2172. case EVENT_WPS_BUTTON_PUSHED:
  2173. #ifdef CONFIG_WPS
  2174. wpas_wps_start_pbc(wpa_s, NULL, 0);
  2175. #endif /* CONFIG_WPS */
  2176. break;
  2177. default:
  2178. wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
  2179. break;
  2180. }
  2181. }