events.c 82 KB

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