events.c 66 KB

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