events.c 48 KB

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