events.c 81 KB

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