events.c 79 KB

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