events.c 80 KB

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