events.c 91 KB

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