events.c 83 KB

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