events.c 81 KB

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