events.c 66 KB

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