events.c 98 KB

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