events.c 96 KB

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