events.c 52 KB

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