events.c 92 KB

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