events.c 103 KB

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