events.c 84 KB

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