events.c 81 KB

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