events.c 82 KB

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