events.c 93 KB

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