events.c 69 KB

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