events.c 94 KB

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