events.c 71 KB

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