events.c 67 KB

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