events.c 84 KB

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