events.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466
  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 != NULL && ssid->eap.pcsc == NULL) ||
  252. wpa_s->scard != NULL || wpa_s->conf->external_sim)
  253. return 0;
  254. if (ssid == NULL || 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. /*
  590. * Test whether BSS is in an ESS.
  591. * This is done differently in DMG (60 GHz) and non-DMG bands
  592. */
  593. static int bss_is_ess(struct wpa_bss *bss)
  594. {
  595. if (bss_is_dmg(bss)) {
  596. return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
  597. IEEE80211_CAP_DMG_AP;
  598. }
  599. return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
  600. IEEE80211_CAP_ESS);
  601. }
  602. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  603. int i, struct wpa_bss *bss,
  604. struct wpa_ssid *group,
  605. int only_first_ssid)
  606. {
  607. u8 wpa_ie_len, rsn_ie_len;
  608. int wpa;
  609. struct wpa_blacklist *e;
  610. const u8 *ie;
  611. struct wpa_ssid *ssid;
  612. int osen;
  613. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  614. wpa_ie_len = ie ? ie[1] : 0;
  615. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  616. rsn_ie_len = ie ? ie[1] : 0;
  617. ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  618. osen = ie != NULL;
  619. wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  620. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
  621. i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
  622. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  623. wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
  624. (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  625. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
  626. " p2p" : "",
  627. osen ? " osen=1" : "");
  628. e = wpa_blacklist_get(wpa_s, bss->bssid);
  629. if (e) {
  630. int limit = 1;
  631. if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
  632. /*
  633. * When only a single network is enabled, we can
  634. * trigger blacklisting on the first failure. This
  635. * should not be done with multiple enabled networks to
  636. * avoid getting forced to move into a worse ESS on
  637. * single error if there are no other BSSes of the
  638. * current ESS.
  639. */
  640. limit = 0;
  641. }
  642. if (e->count > limit) {
  643. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  644. "(count=%d limit=%d)", e->count, limit);
  645. return NULL;
  646. }
  647. }
  648. if (bss->ssid_len == 0) {
  649. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
  650. return NULL;
  651. }
  652. if (disallowed_bssid(wpa_s, bss->bssid)) {
  653. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
  654. return NULL;
  655. }
  656. if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  657. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
  658. return NULL;
  659. }
  660. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  661. for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
  662. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  663. int res;
  664. if (wpas_network_disabled(wpa_s, ssid)) {
  665. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
  666. continue;
  667. }
  668. res = wpas_temp_disabled(wpa_s, ssid);
  669. if (res > 0) {
  670. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
  671. "temporarily for %d second(s)", res);
  672. continue;
  673. }
  674. #ifdef CONFIG_WPS
  675. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  676. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  677. "(WPS)");
  678. continue;
  679. }
  680. if (wpa && ssid->ssid_len == 0 &&
  681. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  682. check_ssid = 0;
  683. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  684. /* Only allow wildcard SSID match if an AP
  685. * advertises active WPS operation that matches
  686. * with our mode. */
  687. check_ssid = 1;
  688. if (ssid->ssid_len == 0 &&
  689. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  690. check_ssid = 0;
  691. }
  692. #endif /* CONFIG_WPS */
  693. if (ssid->bssid_set && ssid->ssid_len == 0 &&
  694. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
  695. check_ssid = 0;
  696. if (check_ssid &&
  697. (bss->ssid_len != ssid->ssid_len ||
  698. os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
  699. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
  700. continue;
  701. }
  702. if (ssid->bssid_set &&
  703. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  704. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
  705. continue;
  706. }
  707. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  708. continue;
  709. if (!osen && !wpa &&
  710. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  711. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  712. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  713. wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
  714. "not allowed");
  715. continue;
  716. }
  717. if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  718. has_wep_key(ssid)) {
  719. wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
  720. continue;
  721. }
  722. if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
  723. wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
  724. "not allowed");
  725. continue;
  726. }
  727. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  728. wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
  729. "mismatch");
  730. continue;
  731. }
  732. if (!bss_is_ess(bss)) {
  733. wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
  734. continue;
  735. }
  736. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  737. wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
  738. "allowed");
  739. continue;
  740. }
  741. if (!rate_match(wpa_s, bss)) {
  742. wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
  743. "not match");
  744. continue;
  745. }
  746. #ifdef CONFIG_P2P
  747. if (ssid->p2p_group &&
  748. !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  749. !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  750. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
  751. continue;
  752. }
  753. if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
  754. struct wpabuf *p2p_ie;
  755. u8 dev_addr[ETH_ALEN];
  756. ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  757. if (ie == NULL) {
  758. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
  759. continue;
  760. }
  761. p2p_ie = wpa_bss_get_vendor_ie_multi(
  762. bss, P2P_IE_VENDOR_TYPE);
  763. if (p2p_ie == NULL) {
  764. wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
  765. continue;
  766. }
  767. if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
  768. || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
  769. ETH_ALEN) != 0) {
  770. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
  771. wpabuf_free(p2p_ie);
  772. continue;
  773. }
  774. wpabuf_free(p2p_ie);
  775. }
  776. /*
  777. * TODO: skip the AP if its P2P IE has Group Formation
  778. * bit set in the P2P Group Capability Bitmap and we
  779. * are not in Group Formation with that device.
  780. */
  781. #endif /* CONFIG_P2P */
  782. /* Matching configuration found */
  783. return ssid;
  784. }
  785. /* No matching configuration found */
  786. return NULL;
  787. }
  788. static struct wpa_bss *
  789. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  790. struct wpa_ssid *group,
  791. struct wpa_ssid **selected_ssid,
  792. int only_first_ssid)
  793. {
  794. unsigned int i;
  795. if (only_first_ssid)
  796. wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
  797. group->id);
  798. else
  799. wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
  800. group->priority);
  801. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  802. struct wpa_bss *bss = wpa_s->last_scan_res[i];
  803. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
  804. only_first_ssid);
  805. if (!*selected_ssid)
  806. continue;
  807. wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
  808. " ssid='%s'",
  809. MAC2STR(bss->bssid),
  810. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  811. return bss;
  812. }
  813. return NULL;
  814. }
  815. struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  816. struct wpa_ssid **selected_ssid)
  817. {
  818. struct wpa_bss *selected = NULL;
  819. int prio;
  820. struct wpa_ssid *next_ssid = NULL;
  821. if (wpa_s->last_scan_res == NULL ||
  822. wpa_s->last_scan_res_used == 0)
  823. return NULL; /* no scan results from last update */
  824. if (wpa_s->next_ssid) {
  825. struct wpa_ssid *ssid;
  826. /* check that next_ssid is still valid */
  827. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  828. if (ssid == wpa_s->next_ssid)
  829. break;
  830. }
  831. next_ssid = ssid;
  832. wpa_s->next_ssid = NULL;
  833. }
  834. while (selected == NULL) {
  835. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  836. if (next_ssid && next_ssid->priority ==
  837. wpa_s->conf->pssid[prio]->priority) {
  838. selected = wpa_supplicant_select_bss(
  839. wpa_s, next_ssid, selected_ssid, 1);
  840. if (selected)
  841. break;
  842. }
  843. selected = wpa_supplicant_select_bss(
  844. wpa_s, wpa_s->conf->pssid[prio],
  845. selected_ssid, 0);
  846. if (selected)
  847. break;
  848. }
  849. if (selected == NULL && wpa_s->blacklist &&
  850. !wpa_s->countermeasures) {
  851. wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
  852. "blacklist and try again");
  853. wpa_blacklist_clear(wpa_s);
  854. wpa_s->blacklist_cleared++;
  855. } else if (selected == NULL)
  856. break;
  857. }
  858. return selected;
  859. }
  860. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  861. int timeout_sec, int timeout_usec)
  862. {
  863. if (!wpa_supplicant_enabled_networks(wpa_s)) {
  864. /*
  865. * No networks are enabled; short-circuit request so
  866. * we don't wait timeout seconds before transitioning
  867. * to INACTIVE state.
  868. */
  869. wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
  870. "since there are no enabled networks");
  871. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  872. return;
  873. }
  874. wpa_s->scan_for_connection = 1;
  875. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  876. }
  877. int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  878. struct wpa_bss *selected,
  879. struct wpa_ssid *ssid)
  880. {
  881. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  882. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  883. "PBC session overlap");
  884. #ifdef CONFIG_P2P
  885. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  886. wpa_s->p2p_in_provisioning) {
  887. eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
  888. wpa_s, NULL);
  889. return -1;
  890. }
  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. wpa_s->p2p_in_invitation) {
  1196. /*
  1197. * Use shorter wait during P2P Provisioning
  1198. * state and during P2P join-a-group operation
  1199. * to speed up group formation.
  1200. */
  1201. timeout_sec = 0;
  1202. timeout_usec = 250000;
  1203. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1204. timeout_usec);
  1205. return 0;
  1206. }
  1207. #endif /* CONFIG_P2P */
  1208. #ifdef CONFIG_INTERWORKING
  1209. if (wpa_s->conf->auto_interworking &&
  1210. wpa_s->conf->interworking &&
  1211. wpa_s->conf->cred) {
  1212. wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
  1213. "start ANQP fetch since no matching "
  1214. "networks found");
  1215. wpa_s->network_select = 1;
  1216. wpa_s->auto_network_select = 1;
  1217. interworking_start_fetch_anqp(wpa_s);
  1218. return 1;
  1219. }
  1220. #endif /* CONFIG_INTERWORKING */
  1221. #ifdef CONFIG_WPS
  1222. if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
  1223. wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
  1224. timeout_sec = 0;
  1225. timeout_usec = 500000;
  1226. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1227. timeout_usec);
  1228. return 0;
  1229. }
  1230. #endif /* CONFIG_WPS */
  1231. if (wpa_supplicant_req_sched_scan(wpa_s))
  1232. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1233. timeout_usec);
  1234. }
  1235. }
  1236. return 0;
  1237. }
  1238. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  1239. union wpa_event_data *data)
  1240. {
  1241. struct wpa_supplicant *ifs;
  1242. if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
  1243. /*
  1244. * If no scan results could be fetched, then no need to
  1245. * notify those interfaces that did not actually request
  1246. * this scan. Similarly, if scan results started a new operation on this
  1247. * interface, do not notify other interfaces to avoid concurrent
  1248. * operations during a connection attempt.
  1249. */
  1250. return;
  1251. }
  1252. /*
  1253. * Check other interfaces to see if they share the same radio. If
  1254. * so, they get updated with this same scan info.
  1255. */
  1256. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  1257. radio_list) {
  1258. if (ifs != wpa_s) {
  1259. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  1260. "sibling", ifs->ifname);
  1261. _wpa_supplicant_event_scan_results(ifs, data, 0);
  1262. }
  1263. }
  1264. }
  1265. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1266. int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
  1267. {
  1268. #ifdef CONFIG_NO_SCAN_PROCESSING
  1269. return -1;
  1270. #else /* CONFIG_NO_SCAN_PROCESSING */
  1271. struct os_reltime now;
  1272. if (wpa_s->last_scan_res_used <= 0)
  1273. return -1;
  1274. os_get_reltime(&now);
  1275. if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
  1276. wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
  1277. return -1;
  1278. }
  1279. return wpas_select_network_from_last_scan(wpa_s, 0, 1);
  1280. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1281. }
  1282. #ifdef CONFIG_WNM
  1283. static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
  1284. {
  1285. struct wpa_supplicant *wpa_s = eloop_ctx;
  1286. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1287. return;
  1288. if (!wpa_s->no_keep_alive) {
  1289. wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
  1290. MAC2STR(wpa_s->bssid));
  1291. /* TODO: could skip this if normal data traffic has been sent */
  1292. /* TODO: Consider using some more appropriate data frame for
  1293. * this */
  1294. if (wpa_s->l2)
  1295. l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
  1296. (u8 *) "", 0);
  1297. }
  1298. #ifdef CONFIG_SME
  1299. if (wpa_s->sme.bss_max_idle_period) {
  1300. unsigned int msec;
  1301. msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
  1302. if (msec > 100)
  1303. msec -= 100;
  1304. eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
  1305. wnm_bss_keep_alive, wpa_s, NULL);
  1306. }
  1307. #endif /* CONFIG_SME */
  1308. }
  1309. static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
  1310. const u8 *ies, size_t ies_len)
  1311. {
  1312. struct ieee802_11_elems elems;
  1313. if (ies == NULL)
  1314. return;
  1315. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
  1316. return;
  1317. #ifdef CONFIG_SME
  1318. if (elems.bss_max_idle_period) {
  1319. unsigned int msec;
  1320. wpa_s->sme.bss_max_idle_period =
  1321. WPA_GET_LE16(elems.bss_max_idle_period);
  1322. wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
  1323. "TU)%s", wpa_s->sme.bss_max_idle_period,
  1324. (elems.bss_max_idle_period[2] & 0x01) ?
  1325. " (protected keep-live required)" : "");
  1326. if (wpa_s->sme.bss_max_idle_period == 0)
  1327. wpa_s->sme.bss_max_idle_period = 1;
  1328. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
  1329. eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
  1330. /* msec times 1000 */
  1331. msec = wpa_s->sme.bss_max_idle_period * 1024;
  1332. if (msec > 100)
  1333. msec -= 100;
  1334. eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
  1335. wnm_bss_keep_alive, wpa_s,
  1336. NULL);
  1337. }
  1338. }
  1339. #endif /* CONFIG_SME */
  1340. }
  1341. #endif /* CONFIG_WNM */
  1342. void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
  1343. {
  1344. #ifdef CONFIG_WNM
  1345. eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
  1346. #endif /* CONFIG_WNM */
  1347. }
  1348. #ifdef CONFIG_INTERWORKING
  1349. static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
  1350. size_t len)
  1351. {
  1352. int res;
  1353. wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
  1354. res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
  1355. if (res) {
  1356. wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
  1357. }
  1358. return res;
  1359. }
  1360. static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
  1361. const u8 *ies, size_t ies_len)
  1362. {
  1363. struct ieee802_11_elems elems;
  1364. if (ies == NULL)
  1365. return;
  1366. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
  1367. return;
  1368. if (elems.qos_map_set) {
  1369. wpas_qos_map_set(wpa_s, elems.qos_map_set,
  1370. elems.qos_map_set_len);
  1371. }
  1372. }
  1373. #endif /* CONFIG_INTERWORKING */
  1374. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  1375. union wpa_event_data *data)
  1376. {
  1377. int l, len, found = 0, wpa_found, rsn_found;
  1378. const u8 *p;
  1379. #ifdef CONFIG_IEEE80211R
  1380. u8 bssid[ETH_ALEN];
  1381. #endif /* CONFIG_IEEE80211R */
  1382. wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
  1383. if (data->assoc_info.req_ies)
  1384. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  1385. data->assoc_info.req_ies_len);
  1386. if (data->assoc_info.resp_ies) {
  1387. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  1388. data->assoc_info.resp_ies_len);
  1389. #ifdef CONFIG_TDLS
  1390. wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
  1391. data->assoc_info.resp_ies_len);
  1392. #endif /* CONFIG_TDLS */
  1393. #ifdef CONFIG_WNM
  1394. wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
  1395. data->assoc_info.resp_ies_len);
  1396. #endif /* CONFIG_WNM */
  1397. #ifdef CONFIG_INTERWORKING
  1398. interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
  1399. data->assoc_info.resp_ies_len);
  1400. #endif /* CONFIG_INTERWORKING */
  1401. }
  1402. if (data->assoc_info.beacon_ies)
  1403. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  1404. data->assoc_info.beacon_ies,
  1405. data->assoc_info.beacon_ies_len);
  1406. if (data->assoc_info.freq)
  1407. wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
  1408. data->assoc_info.freq);
  1409. p = data->assoc_info.req_ies;
  1410. l = data->assoc_info.req_ies_len;
  1411. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  1412. while (p && l >= 2) {
  1413. len = p[1] + 2;
  1414. if (len > l) {
  1415. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1416. p, l);
  1417. break;
  1418. }
  1419. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1420. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  1421. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  1422. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  1423. break;
  1424. found = 1;
  1425. wpa_find_assoc_pmkid(wpa_s);
  1426. break;
  1427. }
  1428. l -= len;
  1429. p += len;
  1430. }
  1431. if (!found && data->assoc_info.req_ies)
  1432. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1433. #ifdef CONFIG_IEEE80211R
  1434. #ifdef CONFIG_SME
  1435. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  1436. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1437. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1438. data->assoc_info.resp_ies,
  1439. data->assoc_info.resp_ies_len,
  1440. bssid) < 0) {
  1441. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1442. "Reassociation Response failed");
  1443. wpa_supplicant_deauthenticate(
  1444. wpa_s, WLAN_REASON_INVALID_IE);
  1445. return -1;
  1446. }
  1447. }
  1448. p = data->assoc_info.resp_ies;
  1449. l = data->assoc_info.resp_ies_len;
  1450. #ifdef CONFIG_WPS_STRICT
  1451. if (p && wpa_s->current_ssid &&
  1452. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1453. struct wpabuf *wps;
  1454. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  1455. if (wps == NULL) {
  1456. wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
  1457. "include WPS IE in (Re)Association Response");
  1458. return -1;
  1459. }
  1460. if (wps_validate_assoc_resp(wps) < 0) {
  1461. wpabuf_free(wps);
  1462. wpa_supplicant_deauthenticate(
  1463. wpa_s, WLAN_REASON_INVALID_IE);
  1464. return -1;
  1465. }
  1466. wpabuf_free(wps);
  1467. }
  1468. #endif /* CONFIG_WPS_STRICT */
  1469. /* Go through the IEs and make a copy of the MDIE, if present. */
  1470. while (p && l >= 2) {
  1471. len = p[1] + 2;
  1472. if (len > l) {
  1473. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1474. p, l);
  1475. break;
  1476. }
  1477. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  1478. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  1479. wpa_s->sme.ft_used = 1;
  1480. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  1481. MOBILITY_DOMAIN_ID_LEN);
  1482. break;
  1483. }
  1484. l -= len;
  1485. p += len;
  1486. }
  1487. #endif /* CONFIG_SME */
  1488. /* Process FT when SME is in the driver */
  1489. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1490. wpa_ft_is_completed(wpa_s->wpa)) {
  1491. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1492. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1493. data->assoc_info.resp_ies,
  1494. data->assoc_info.resp_ies_len,
  1495. bssid) < 0) {
  1496. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1497. "Reassociation Response failed");
  1498. wpa_supplicant_deauthenticate(
  1499. wpa_s, WLAN_REASON_INVALID_IE);
  1500. return -1;
  1501. }
  1502. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
  1503. }
  1504. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  1505. data->assoc_info.resp_ies_len);
  1506. #endif /* CONFIG_IEEE80211R */
  1507. /* WPA/RSN IE from Beacon/ProbeResp */
  1508. p = data->assoc_info.beacon_ies;
  1509. l = data->assoc_info.beacon_ies_len;
  1510. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  1511. */
  1512. wpa_found = rsn_found = 0;
  1513. while (p && l >= 2) {
  1514. len = p[1] + 2;
  1515. if (len > l) {
  1516. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  1517. p, l);
  1518. break;
  1519. }
  1520. if (!wpa_found &&
  1521. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1522. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  1523. wpa_found = 1;
  1524. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  1525. }
  1526. if (!rsn_found &&
  1527. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  1528. rsn_found = 1;
  1529. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  1530. }
  1531. l -= len;
  1532. p += len;
  1533. }
  1534. if (!wpa_found && data->assoc_info.beacon_ies)
  1535. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  1536. if (!rsn_found && data->assoc_info.beacon_ies)
  1537. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  1538. if (wpa_found || rsn_found)
  1539. wpa_s->ap_ies_from_associnfo = 1;
  1540. if (wpa_s->assoc_freq && data->assoc_info.freq &&
  1541. wpa_s->assoc_freq != data->assoc_info.freq) {
  1542. wpa_printf(MSG_DEBUG, "Operating frequency changed from "
  1543. "%u to %u MHz",
  1544. wpa_s->assoc_freq, data->assoc_info.freq);
  1545. wpa_supplicant_update_scan_results(wpa_s);
  1546. }
  1547. wpa_s->assoc_freq = data->assoc_info.freq;
  1548. return 0;
  1549. }
  1550. static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
  1551. {
  1552. const u8 *bss_wpa = NULL, *bss_rsn = NULL;
  1553. if (!wpa_s->current_bss || !wpa_s->current_ssid)
  1554. return -1;
  1555. if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
  1556. return 0;
  1557. bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1558. WPA_IE_VENDOR_TYPE);
  1559. bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
  1560. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  1561. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  1562. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  1563. bss_rsn ? 2 + bss_rsn[1] : 0))
  1564. return -1;
  1565. return 0;
  1566. }
  1567. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  1568. union wpa_event_data *data)
  1569. {
  1570. u8 bssid[ETH_ALEN];
  1571. int ft_completed;
  1572. #ifdef CONFIG_AP
  1573. if (wpa_s->ap_iface) {
  1574. if (!data)
  1575. return;
  1576. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  1577. data->assoc_info.addr,
  1578. data->assoc_info.req_ies,
  1579. data->assoc_info.req_ies_len,
  1580. data->assoc_info.reassoc);
  1581. return;
  1582. }
  1583. #endif /* CONFIG_AP */
  1584. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  1585. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  1586. return;
  1587. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1588. wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
  1589. wpa_supplicant_deauthenticate(
  1590. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1591. return;
  1592. }
  1593. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  1594. if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
  1595. wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  1596. MACSTR, MAC2STR(bssid));
  1597. random_add_randomness(bssid, ETH_ALEN);
  1598. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  1599. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1600. wpas_notify_bssid_changed(wpa_s);
  1601. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  1602. wpa_clear_keys(wpa_s, bssid);
  1603. }
  1604. if (wpa_supplicant_select_config(wpa_s) < 0) {
  1605. wpa_supplicant_deauthenticate(
  1606. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1607. return;
  1608. }
  1609. if (wpa_s->conf->ap_scan == 1 &&
  1610. wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
  1611. if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
  1612. wpa_msg(wpa_s, MSG_WARNING,
  1613. "WPA/RSN IEs not updated");
  1614. }
  1615. }
  1616. #ifdef CONFIG_SME
  1617. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1618. wpa_s->sme.prev_bssid_set = 1;
  1619. wpa_s->sme.last_unprot_disconnect.sec = 0;
  1620. #endif /* CONFIG_SME */
  1621. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1622. if (wpa_s->current_ssid) {
  1623. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1624. * initialized before association, but for other modes,
  1625. * initialize PC/SC here, if the current configuration needs
  1626. * smartcard or SIM/USIM. */
  1627. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1628. }
  1629. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1630. if (wpa_s->l2)
  1631. l2_packet_notify_auth_start(wpa_s->l2);
  1632. /*
  1633. * Set portEnabled first to FALSE in order to get EAP state machine out
  1634. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1635. * state machine may transit to AUTHENTICATING state based on obsolete
  1636. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1637. * AUTHENTICATED without ever giving chance to EAP state machine to
  1638. * reset the state.
  1639. */
  1640. if (!ft_completed) {
  1641. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1642. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1643. }
  1644. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1645. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1646. /* 802.1X::portControl = Auto */
  1647. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1648. wpa_s->eapol_received = 0;
  1649. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1650. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1651. (wpa_s->current_ssid &&
  1652. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1653. if (wpa_s->current_ssid &&
  1654. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
  1655. (wpa_s->drv_flags &
  1656. WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
  1657. /*
  1658. * Set the key after having received joined-IBSS event
  1659. * from the driver.
  1660. */
  1661. wpa_supplicant_set_wpa_none_key(wpa_s,
  1662. wpa_s->current_ssid);
  1663. }
  1664. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1665. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1666. } else if (!ft_completed) {
  1667. /* Timeout for receiving the first EAPOL packet */
  1668. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1669. }
  1670. wpa_supplicant_cancel_scan(wpa_s);
  1671. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1672. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1673. /*
  1674. * We are done; the driver will take care of RSN 4-way
  1675. * handshake.
  1676. */
  1677. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1678. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1679. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1680. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1681. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1682. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1683. /*
  1684. * The driver will take care of RSN 4-way handshake, so we need
  1685. * to allow EAPOL supplicant to complete its work without
  1686. * waiting for WPA supplicant.
  1687. */
  1688. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1689. } else if (ft_completed) {
  1690. /*
  1691. * FT protocol completed - make sure EAPOL state machine ends
  1692. * up in authenticated.
  1693. */
  1694. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1695. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1696. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1697. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1698. }
  1699. wpa_s->last_eapol_matches_bssid = 0;
  1700. if (wpa_s->pending_eapol_rx) {
  1701. struct os_reltime now, age;
  1702. os_get_reltime(&now);
  1703. os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1704. if (age.sec == 0 && age.usec < 100000 &&
  1705. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1706. 0) {
  1707. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
  1708. "frame that was received just before "
  1709. "association notification");
  1710. wpa_supplicant_rx_eapol(
  1711. wpa_s, wpa_s->pending_eapol_rx_src,
  1712. wpabuf_head(wpa_s->pending_eapol_rx),
  1713. wpabuf_len(wpa_s->pending_eapol_rx));
  1714. }
  1715. wpabuf_free(wpa_s->pending_eapol_rx);
  1716. wpa_s->pending_eapol_rx = NULL;
  1717. }
  1718. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1719. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1720. wpa_s->current_ssid &&
  1721. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
  1722. /* Set static WEP keys again */
  1723. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1724. }
  1725. #ifdef CONFIG_IBSS_RSN
  1726. if (wpa_s->current_ssid &&
  1727. wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
  1728. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1729. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
  1730. wpa_s->ibss_rsn == NULL) {
  1731. wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
  1732. if (!wpa_s->ibss_rsn) {
  1733. wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
  1734. wpa_supplicant_deauthenticate(
  1735. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1736. return;
  1737. }
  1738. ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
  1739. }
  1740. #endif /* CONFIG_IBSS_RSN */
  1741. wpas_wps_notify_assoc(wpa_s, bssid);
  1742. }
  1743. static int disconnect_reason_recoverable(u16 reason_code)
  1744. {
  1745. return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
  1746. reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
  1747. reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
  1748. }
  1749. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
  1750. u16 reason_code,
  1751. int locally_generated)
  1752. {
  1753. const u8 *bssid;
  1754. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1755. /*
  1756. * At least Host AP driver and a Prism3 card seemed to be
  1757. * generating streams of disconnected events when configuring
  1758. * IBSS for WPA-None. Ignore them for now.
  1759. */
  1760. return;
  1761. }
  1762. bssid = wpa_s->bssid;
  1763. if (is_zero_ether_addr(bssid))
  1764. bssid = wpa_s->pending_bssid;
  1765. if (!is_zero_ether_addr(bssid) ||
  1766. wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  1767. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
  1768. " reason=%d%s",
  1769. MAC2STR(bssid), reason_code,
  1770. locally_generated ? " locally_generated=1" : "");
  1771. }
  1772. }
  1773. static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
  1774. int locally_generated)
  1775. {
  1776. if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
  1777. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  1778. return 0; /* Not in 4-way handshake with PSK */
  1779. /*
  1780. * It looks like connection was lost while trying to go through PSK
  1781. * 4-way handshake. Filter out known disconnection cases that are caused
  1782. * by something else than PSK mismatch to avoid confusing reports.
  1783. */
  1784. if (locally_generated) {
  1785. if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
  1786. return 0;
  1787. }
  1788. return 1;
  1789. }
  1790. static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
  1791. u16 reason_code,
  1792. int locally_generated)
  1793. {
  1794. const u8 *bssid;
  1795. int authenticating;
  1796. u8 prev_pending_bssid[ETH_ALEN];
  1797. struct wpa_bss *fast_reconnect = NULL;
  1798. struct wpa_ssid *fast_reconnect_ssid = NULL;
  1799. struct wpa_ssid *last_ssid;
  1800. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1801. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1802. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1803. /*
  1804. * At least Host AP driver and a Prism3 card seemed to be
  1805. * generating streams of disconnected events when configuring
  1806. * IBSS for WPA-None. Ignore them for now.
  1807. */
  1808. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
  1809. "IBSS/WPA-None mode");
  1810. return;
  1811. }
  1812. if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
  1813. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1814. "pre-shared key may be incorrect");
  1815. if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
  1816. return; /* P2P group removed */
  1817. wpas_auth_failed(wpa_s, "WRONG_KEY");
  1818. }
  1819. if (!wpa_s->disconnected &&
  1820. (!wpa_s->auto_reconnect_disabled ||
  1821. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
  1822. wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
  1823. "reconnect (wps=%d wpa_state=%d)",
  1824. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
  1825. wpa_s->wpa_state);
  1826. if (wpa_s->wpa_state == WPA_COMPLETED &&
  1827. wpa_s->current_ssid &&
  1828. wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  1829. !locally_generated &&
  1830. disconnect_reason_recoverable(reason_code)) {
  1831. /*
  1832. * It looks like the AP has dropped association with
  1833. * us, but could allow us to get back in. Try to
  1834. * reconnect to the same BSS without full scan to save
  1835. * time for some common cases.
  1836. */
  1837. fast_reconnect = wpa_s->current_bss;
  1838. fast_reconnect_ssid = wpa_s->current_ssid;
  1839. } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
  1840. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1841. else
  1842. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
  1843. "immediate scan");
  1844. } else {
  1845. wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
  1846. "try to re-connect");
  1847. wpa_s->reassociate = 0;
  1848. wpa_s->disconnected = 1;
  1849. wpa_supplicant_cancel_sched_scan(wpa_s);
  1850. }
  1851. bssid = wpa_s->bssid;
  1852. if (is_zero_ether_addr(bssid))
  1853. bssid = wpa_s->pending_bssid;
  1854. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  1855. wpas_connection_failed(wpa_s, bssid);
  1856. wpa_sm_notify_disassoc(wpa_s->wpa);
  1857. if (locally_generated)
  1858. wpa_s->disconnect_reason = -reason_code;
  1859. else
  1860. wpa_s->disconnect_reason = reason_code;
  1861. wpas_notify_disconnect_reason(wpa_s);
  1862. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1863. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
  1864. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1865. }
  1866. last_ssid = wpa_s->current_ssid;
  1867. wpa_supplicant_mark_disassoc(wpa_s);
  1868. if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1869. sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
  1870. wpa_s->current_ssid = last_ssid;
  1871. }
  1872. if (fast_reconnect &&
  1873. !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
  1874. !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
  1875. !disallowed_ssid(wpa_s, fast_reconnect->ssid,
  1876. fast_reconnect->ssid_len) &&
  1877. !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
  1878. #ifndef CONFIG_NO_SCAN_PROCESSING
  1879. wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
  1880. if (wpa_supplicant_connect(wpa_s, fast_reconnect,
  1881. fast_reconnect_ssid) < 0) {
  1882. /* Recover through full scan */
  1883. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1884. }
  1885. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1886. } else if (fast_reconnect) {
  1887. /*
  1888. * Could not reconnect to the same BSS due to network being
  1889. * disabled. Use a new scan to match the alternative behavior
  1890. * above, i.e., to continue automatic reconnection attempt in a
  1891. * way that enforces disabled network rules.
  1892. */
  1893. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1894. }
  1895. }
  1896. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1897. void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
  1898. {
  1899. struct wpa_supplicant *wpa_s = eloop_ctx;
  1900. if (!wpa_s->pending_mic_error_report)
  1901. return;
  1902. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
  1903. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1904. wpa_s->pending_mic_error_report = 0;
  1905. }
  1906. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1907. static void
  1908. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1909. union wpa_event_data *data)
  1910. {
  1911. int pairwise;
  1912. struct os_reltime t;
  1913. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1914. pairwise = (data && data->michael_mic_failure.unicast);
  1915. os_get_reltime(&t);
  1916. if ((wpa_s->last_michael_mic_error.sec &&
  1917. !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
  1918. wpa_s->pending_mic_error_report) {
  1919. if (wpa_s->pending_mic_error_report) {
  1920. /*
  1921. * Send the pending MIC error report immediately since
  1922. * we are going to start countermeasures and AP better
  1923. * do the same.
  1924. */
  1925. wpa_sm_key_request(wpa_s->wpa, 1,
  1926. wpa_s->pending_mic_error_pairwise);
  1927. }
  1928. /* Send the new MIC error report immediately since we are going
  1929. * to start countermeasures and AP better do the same.
  1930. */
  1931. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1932. /* initialize countermeasures */
  1933. wpa_s->countermeasures = 1;
  1934. wpa_blacklist_add(wpa_s, wpa_s->bssid);
  1935. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1936. /*
  1937. * Need to wait for completion of request frame. We do not get
  1938. * any callback for the message completion, so just wait a
  1939. * short while and hope for the best. */
  1940. os_sleep(0, 10000);
  1941. wpa_drv_set_countermeasures(wpa_s, 1);
  1942. wpa_supplicant_deauthenticate(wpa_s,
  1943. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1944. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1945. wpa_s, NULL);
  1946. eloop_register_timeout(60, 0,
  1947. wpa_supplicant_stop_countermeasures,
  1948. wpa_s, NULL);
  1949. /* TODO: mark the AP rejected for 60 second. STA is
  1950. * allowed to associate with another AP.. */
  1951. } else {
  1952. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1953. if (wpa_s->mic_errors_seen) {
  1954. /*
  1955. * Reduce the effectiveness of Michael MIC error
  1956. * reports as a means for attacking against TKIP if
  1957. * more than one MIC failure is noticed with the same
  1958. * PTK. We delay the transmission of the reports by a
  1959. * random time between 0 and 60 seconds in order to
  1960. * force the attacker wait 60 seconds before getting
  1961. * the information on whether a frame resulted in a MIC
  1962. * failure.
  1963. */
  1964. u8 rval[4];
  1965. int sec;
  1966. if (os_get_random(rval, sizeof(rval)) < 0)
  1967. sec = os_random() % 60;
  1968. else
  1969. sec = WPA_GET_BE32(rval) % 60;
  1970. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
  1971. "report %d seconds", sec);
  1972. wpa_s->pending_mic_error_report = 1;
  1973. wpa_s->pending_mic_error_pairwise = pairwise;
  1974. eloop_cancel_timeout(
  1975. wpa_supplicant_delayed_mic_error_report,
  1976. wpa_s, NULL);
  1977. eloop_register_timeout(
  1978. sec, os_random() % 1000000,
  1979. wpa_supplicant_delayed_mic_error_report,
  1980. wpa_s, NULL);
  1981. } else {
  1982. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1983. }
  1984. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1985. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1986. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1987. }
  1988. wpa_s->last_michael_mic_error = t;
  1989. wpa_s->mic_errors_seen++;
  1990. }
  1991. #ifdef CONFIG_TERMINATE_ONLASTIF
  1992. static int any_interfaces(struct wpa_supplicant *head)
  1993. {
  1994. struct wpa_supplicant *wpa_s;
  1995. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1996. if (!wpa_s->interface_removed)
  1997. return 1;
  1998. return 0;
  1999. }
  2000. #endif /* CONFIG_TERMINATE_ONLASTIF */
  2001. static void
  2002. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  2003. union wpa_event_data *data)
  2004. {
  2005. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  2006. return;
  2007. switch (data->interface_status.ievent) {
  2008. case EVENT_INTERFACE_ADDED:
  2009. if (!wpa_s->interface_removed)
  2010. break;
  2011. wpa_s->interface_removed = 0;
  2012. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
  2013. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  2014. wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
  2015. "driver after interface was added");
  2016. }
  2017. break;
  2018. case EVENT_INTERFACE_REMOVED:
  2019. wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
  2020. wpa_s->interface_removed = 1;
  2021. wpa_supplicant_mark_disassoc(wpa_s);
  2022. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2023. l2_packet_deinit(wpa_s->l2);
  2024. wpa_s->l2 = NULL;
  2025. #ifdef CONFIG_TERMINATE_ONLASTIF
  2026. /* check if last interface */
  2027. if (!any_interfaces(wpa_s->global->ifaces))
  2028. eloop_terminate();
  2029. #endif /* CONFIG_TERMINATE_ONLASTIF */
  2030. break;
  2031. }
  2032. }
  2033. #ifdef CONFIG_PEERKEY
  2034. static void
  2035. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  2036. union wpa_event_data *data)
  2037. {
  2038. if (data == NULL)
  2039. return;
  2040. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  2041. }
  2042. #endif /* CONFIG_PEERKEY */
  2043. #ifdef CONFIG_TDLS
  2044. static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
  2045. union wpa_event_data *data)
  2046. {
  2047. if (data == NULL)
  2048. return;
  2049. switch (data->tdls.oper) {
  2050. case TDLS_REQUEST_SETUP:
  2051. wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
  2052. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  2053. wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
  2054. else
  2055. wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
  2056. break;
  2057. case TDLS_REQUEST_TEARDOWN:
  2058. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  2059. wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
  2060. data->tdls.reason_code);
  2061. else
  2062. wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
  2063. data->tdls.peer);
  2064. break;
  2065. }
  2066. }
  2067. #endif /* CONFIG_TDLS */
  2068. #ifdef CONFIG_WNM
  2069. static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
  2070. union wpa_event_data *data)
  2071. {
  2072. if (data == NULL)
  2073. return;
  2074. switch (data->wnm.oper) {
  2075. case WNM_OPER_SLEEP:
  2076. wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
  2077. "(action=%d, intval=%d)",
  2078. data->wnm.sleep_action, data->wnm.sleep_intval);
  2079. ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
  2080. data->wnm.sleep_intval, NULL);
  2081. break;
  2082. }
  2083. }
  2084. #endif /* CONFIG_WNM */
  2085. #ifdef CONFIG_IEEE80211R
  2086. static void
  2087. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  2088. union wpa_event_data *data)
  2089. {
  2090. if (data == NULL)
  2091. return;
  2092. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  2093. data->ft_ies.ies_len,
  2094. data->ft_ies.ft_action,
  2095. data->ft_ies.target_ap,
  2096. data->ft_ies.ric_ies,
  2097. data->ft_ies.ric_ies_len) < 0) {
  2098. /* TODO: prevent MLME/driver from trying to associate? */
  2099. }
  2100. }
  2101. #endif /* CONFIG_IEEE80211R */
  2102. #ifdef CONFIG_IBSS_RSN
  2103. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  2104. union wpa_event_data *data)
  2105. {
  2106. struct wpa_ssid *ssid;
  2107. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  2108. return;
  2109. if (data == NULL)
  2110. return;
  2111. ssid = wpa_s->current_ssid;
  2112. if (ssid == NULL)
  2113. return;
  2114. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  2115. return;
  2116. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  2117. }
  2118. static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
  2119. union wpa_event_data *data)
  2120. {
  2121. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2122. if (ssid == NULL)
  2123. return;
  2124. /* check if the ssid is correctly configured as IBSS/RSN */
  2125. if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
  2126. return;
  2127. ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
  2128. data->rx_mgmt.frame_len);
  2129. }
  2130. #endif /* CONFIG_IBSS_RSN */
  2131. #ifdef CONFIG_IEEE80211R
  2132. static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
  2133. size_t len)
  2134. {
  2135. const u8 *sta_addr, *target_ap_addr;
  2136. u16 status;
  2137. wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
  2138. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
  2139. return; /* only SME case supported for now */
  2140. if (len < 1 + 2 * ETH_ALEN + 2)
  2141. return;
  2142. if (data[0] != 2)
  2143. return; /* Only FT Action Response is supported for now */
  2144. sta_addr = data + 1;
  2145. target_ap_addr = data + 1 + ETH_ALEN;
  2146. status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
  2147. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
  2148. MACSTR " TargetAP " MACSTR " status %u",
  2149. MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
  2150. if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
  2151. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
  2152. " in FT Action Response", MAC2STR(sta_addr));
  2153. return;
  2154. }
  2155. if (status) {
  2156. wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
  2157. "failure (status code %d)", status);
  2158. /* TODO: report error to FT code(?) */
  2159. return;
  2160. }
  2161. if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
  2162. len - (1 + 2 * ETH_ALEN + 2), 1,
  2163. target_ap_addr, NULL, 0) < 0)
  2164. return;
  2165. #ifdef CONFIG_SME
  2166. {
  2167. struct wpa_bss *bss;
  2168. bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
  2169. if (bss)
  2170. wpa_s->sme.freq = bss->freq;
  2171. wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
  2172. sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
  2173. WLAN_AUTH_FT);
  2174. }
  2175. #endif /* CONFIG_SME */
  2176. }
  2177. #endif /* CONFIG_IEEE80211R */
  2178. static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
  2179. struct unprot_deauth *e)
  2180. {
  2181. #ifdef CONFIG_IEEE80211W
  2182. wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
  2183. "dropped: " MACSTR " -> " MACSTR
  2184. " (reason code %u)",
  2185. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  2186. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  2187. #endif /* CONFIG_IEEE80211W */
  2188. }
  2189. static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
  2190. struct unprot_disassoc *e)
  2191. {
  2192. #ifdef CONFIG_IEEE80211W
  2193. wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
  2194. "dropped: " MACSTR " -> " MACSTR
  2195. " (reason code %u)",
  2196. MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
  2197. sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
  2198. #endif /* CONFIG_IEEE80211W */
  2199. }
  2200. static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
  2201. u16 reason_code, int locally_generated,
  2202. const u8 *ie, size_t ie_len, int deauth)
  2203. {
  2204. #ifdef CONFIG_AP
  2205. if (wpa_s->ap_iface && addr) {
  2206. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
  2207. return;
  2208. }
  2209. if (wpa_s->ap_iface) {
  2210. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
  2211. return;
  2212. }
  2213. #endif /* CONFIG_AP */
  2214. wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
  2215. if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
  2216. ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  2217. (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
  2218. eapol_sm_failed(wpa_s->eapol))) &&
  2219. !wpa_s->eap_expected_failure))
  2220. wpas_auth_failed(wpa_s, "AUTH_FAILED");
  2221. #ifdef CONFIG_P2P
  2222. if (deauth && reason_code > 0) {
  2223. if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
  2224. locally_generated) > 0) {
  2225. /*
  2226. * The interface was removed, so cannot continue
  2227. * processing any additional operations after this.
  2228. */
  2229. return;
  2230. }
  2231. }
  2232. #endif /* CONFIG_P2P */
  2233. wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
  2234. locally_generated);
  2235. }
  2236. static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
  2237. struct disassoc_info *info)
  2238. {
  2239. u16 reason_code = 0;
  2240. int locally_generated = 0;
  2241. const u8 *addr = NULL;
  2242. const u8 *ie = NULL;
  2243. size_t ie_len = 0;
  2244. wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
  2245. if (info) {
  2246. addr = info->addr;
  2247. ie = info->ie;
  2248. ie_len = info->ie_len;
  2249. reason_code = info->reason_code;
  2250. locally_generated = info->locally_generated;
  2251. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
  2252. locally_generated ? " (locally generated)" : "");
  2253. if (addr)
  2254. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  2255. MAC2STR(addr));
  2256. wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
  2257. ie, ie_len);
  2258. }
  2259. #ifdef CONFIG_AP
  2260. if (wpa_s->ap_iface && info && info->addr) {
  2261. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
  2262. return;
  2263. }
  2264. if (wpa_s->ap_iface) {
  2265. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
  2266. return;
  2267. }
  2268. #endif /* CONFIG_AP */
  2269. #ifdef CONFIG_P2P
  2270. if (info) {
  2271. wpas_p2p_disassoc_notif(
  2272. wpa_s, info->addr, reason_code, info->ie, info->ie_len,
  2273. locally_generated);
  2274. }
  2275. #endif /* CONFIG_P2P */
  2276. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2277. sme_event_disassoc(wpa_s, info);
  2278. wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
  2279. ie, ie_len, 0);
  2280. }
  2281. static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
  2282. struct deauth_info *info)
  2283. {
  2284. u16 reason_code = 0;
  2285. int locally_generated = 0;
  2286. const u8 *addr = NULL;
  2287. const u8 *ie = NULL;
  2288. size_t ie_len = 0;
  2289. wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
  2290. if (info) {
  2291. addr = info->addr;
  2292. ie = info->ie;
  2293. ie_len = info->ie_len;
  2294. reason_code = info->reason_code;
  2295. locally_generated = info->locally_generated;
  2296. wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
  2297. reason_code,
  2298. locally_generated ? " (locally generated)" : "");
  2299. if (addr) {
  2300. wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
  2301. MAC2STR(addr));
  2302. }
  2303. wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
  2304. ie, ie_len);
  2305. }
  2306. wpa_reset_ft_completed(wpa_s->wpa);
  2307. wpas_event_disconnect(wpa_s, addr, reason_code,
  2308. locally_generated, ie, ie_len, 1);
  2309. }
  2310. static const char * reg_init_str(enum reg_change_initiator init)
  2311. {
  2312. switch (init) {
  2313. case REGDOM_SET_BY_CORE:
  2314. return "CORE";
  2315. case REGDOM_SET_BY_USER:
  2316. return "USER";
  2317. case REGDOM_SET_BY_DRIVER:
  2318. return "DRIVER";
  2319. case REGDOM_SET_BY_COUNTRY_IE:
  2320. return "COUNTRY_IE";
  2321. case REGDOM_BEACON_HINT:
  2322. return "BEACON_HINT";
  2323. }
  2324. return "?";
  2325. }
  2326. static const char * reg_type_str(enum reg_type type)
  2327. {
  2328. switch (type) {
  2329. case REGDOM_TYPE_UNKNOWN:
  2330. return "UNKNOWN";
  2331. case REGDOM_TYPE_COUNTRY:
  2332. return "COUNTRY";
  2333. case REGDOM_TYPE_WORLD:
  2334. return "WORLD";
  2335. case REGDOM_TYPE_CUSTOM_WORLD:
  2336. return "CUSTOM_WORLD";
  2337. case REGDOM_TYPE_INTERSECTION:
  2338. return "INTERSECTION";
  2339. }
  2340. return "?";
  2341. }
  2342. static void wpa_supplicant_update_channel_list(
  2343. struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
  2344. {
  2345. struct wpa_supplicant *ifs;
  2346. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
  2347. reg_init_str(info->type), reg_type_str(info->type),
  2348. info->alpha2[0] ? " alpha2=" : "",
  2349. info->alpha2[0] ? info->alpha2 : "");
  2350. if (wpa_s->drv_priv == NULL)
  2351. return; /* Ignore event during drv initialization */
  2352. free_hw_features(wpa_s);
  2353. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
  2354. wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
  2355. wpas_p2p_update_channel_list(wpa_s);
  2356. /*
  2357. * Check other interfaces to see if they share the same radio. If
  2358. * so, they get updated with this same hw mode info.
  2359. */
  2360. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  2361. radio_list) {
  2362. if (ifs != wpa_s) {
  2363. wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
  2364. ifs->ifname);
  2365. free_hw_features(ifs);
  2366. ifs->hw.modes = wpa_drv_get_hw_feature_data(
  2367. ifs, &ifs->hw.num_modes, &ifs->hw.flags);
  2368. }
  2369. }
  2370. }
  2371. static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
  2372. const u8 *frame, size_t len, int freq)
  2373. {
  2374. const struct ieee80211_mgmt *mgmt;
  2375. const u8 *payload;
  2376. size_t plen;
  2377. u8 category;
  2378. if (len < IEEE80211_HDRLEN + 2)
  2379. return;
  2380. mgmt = (const struct ieee80211_mgmt *) frame;
  2381. payload = frame + IEEE80211_HDRLEN;
  2382. category = *payload++;
  2383. plen = len - IEEE80211_HDRLEN - 1;
  2384. wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
  2385. " Category=%u DataLen=%d freq=%d MHz",
  2386. MAC2STR(mgmt->sa), category, (int) plen, freq);
  2387. #ifdef CONFIG_IEEE80211R
  2388. if (category == WLAN_ACTION_FT) {
  2389. ft_rx_action(wpa_s, payload, plen);
  2390. return;
  2391. }
  2392. #endif /* CONFIG_IEEE80211R */
  2393. #ifdef CONFIG_IEEE80211W
  2394. #ifdef CONFIG_SME
  2395. if (category == WLAN_ACTION_SA_QUERY) {
  2396. sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
  2397. return;
  2398. }
  2399. #endif /* CONFIG_SME */
  2400. #endif /* CONFIG_IEEE80211W */
  2401. #ifdef CONFIG_WNM
  2402. if (mgmt->u.action.category == WLAN_ACTION_WNM) {
  2403. ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
  2404. return;
  2405. }
  2406. #endif /* CONFIG_WNM */
  2407. #ifdef CONFIG_GAS
  2408. if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
  2409. mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
  2410. gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
  2411. mgmt->u.action.category,
  2412. payload, plen, freq) == 0)
  2413. return;
  2414. #endif /* CONFIG_GAS */
  2415. #ifdef CONFIG_TDLS
  2416. if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
  2417. payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
  2418. wpa_dbg(wpa_s, MSG_DEBUG,
  2419. "TDLS: Received Discovery Response from " MACSTR,
  2420. MAC2STR(mgmt->sa));
  2421. return;
  2422. }
  2423. #endif /* CONFIG_TDLS */
  2424. #ifdef CONFIG_INTERWORKING
  2425. if (category == WLAN_ACTION_QOS && plen >= 1 &&
  2426. payload[0] == QOS_QOS_MAP_CONFIG) {
  2427. const u8 *pos = payload + 1;
  2428. size_t qlen = plen - 1;
  2429. wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
  2430. MACSTR, MAC2STR(mgmt->sa));
  2431. if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
  2432. qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
  2433. pos[1] <= qlen - 2 && pos[1] >= 16)
  2434. wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
  2435. return;
  2436. }
  2437. #endif /* CONFIG_INTERWORKING */
  2438. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  2439. category, payload, plen, freq);
  2440. }
  2441. static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
  2442. union wpa_event_data *event)
  2443. {
  2444. #ifdef CONFIG_P2P
  2445. struct wpa_supplicant *ifs;
  2446. #endif /* CONFIG_P2P */
  2447. struct wpa_freq_range_list *list;
  2448. char *str = NULL;
  2449. list = &event->freq_range;
  2450. if (list->num)
  2451. str = freq_range_list_str(list);
  2452. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
  2453. str ? str : "");
  2454. #ifdef CONFIG_P2P
  2455. if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
  2456. wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
  2457. __func__);
  2458. } else {
  2459. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
  2460. wpas_p2p_update_channel_list(wpa_s);
  2461. }
  2462. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  2463. int freq;
  2464. if (!ifs->current_ssid ||
  2465. !ifs->current_ssid->p2p_group ||
  2466. (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
  2467. ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
  2468. continue;
  2469. freq = ifs->current_ssid->frequency;
  2470. if (!freq_range_list_includes(list, freq)) {
  2471. wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
  2472. freq);
  2473. continue;
  2474. }
  2475. wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
  2476. freq);
  2477. /* TODO: Consider using CSA or removing the group within
  2478. * wpa_supplicant */
  2479. wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  2480. }
  2481. #endif /* CONFIG_P2P */
  2482. os_free(str);
  2483. }
  2484. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  2485. union wpa_event_data *data)
  2486. {
  2487. struct wpa_supplicant *wpa_s = ctx;
  2488. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  2489. event != EVENT_INTERFACE_ENABLED &&
  2490. event != EVENT_INTERFACE_STATUS &&
  2491. event != EVENT_SCHED_SCAN_STOPPED) {
  2492. wpa_dbg(wpa_s, MSG_DEBUG,
  2493. "Ignore event %s (%d) while interface is disabled",
  2494. event_to_string(event), event);
  2495. return;
  2496. }
  2497. #ifndef CONFIG_NO_STDOUT_DEBUG
  2498. {
  2499. int level = MSG_DEBUG;
  2500. if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
  2501. const struct ieee80211_hdr *hdr;
  2502. u16 fc;
  2503. hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
  2504. fc = le_to_host16(hdr->frame_control);
  2505. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2506. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  2507. level = MSG_EXCESSIVE;
  2508. }
  2509. wpa_dbg(wpa_s, level, "Event %s (%d) received",
  2510. event_to_string(event), event);
  2511. }
  2512. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2513. switch (event) {
  2514. case EVENT_AUTH:
  2515. sme_event_auth(wpa_s, data);
  2516. break;
  2517. case EVENT_ASSOC:
  2518. wpa_supplicant_event_assoc(wpa_s, data);
  2519. break;
  2520. case EVENT_DISASSOC:
  2521. wpas_event_disassoc(wpa_s,
  2522. data ? &data->disassoc_info : NULL);
  2523. break;
  2524. case EVENT_DEAUTH:
  2525. wpas_event_deauth(wpa_s,
  2526. data ? &data->deauth_info : NULL);
  2527. break;
  2528. case EVENT_MICHAEL_MIC_FAILURE:
  2529. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  2530. break;
  2531. #ifndef CONFIG_NO_SCAN_PROCESSING
  2532. case EVENT_SCAN_STARTED:
  2533. os_get_reltime(&wpa_s->scan_start_time);
  2534. if (wpa_s->own_scan_requested) {
  2535. struct os_reltime diff;
  2536. os_reltime_sub(&wpa_s->scan_start_time,
  2537. &wpa_s->scan_trigger_time, &diff);
  2538. wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
  2539. diff.sec, diff.usec);
  2540. wpa_s->own_scan_requested = 0;
  2541. wpa_s->own_scan_running = 1;
  2542. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  2543. wpa_s->manual_scan_use_id) {
  2544. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED "id=%u",
  2545. wpa_s->manual_scan_id);
  2546. } else {
  2547. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
  2548. }
  2549. } else {
  2550. wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
  2551. wpa_s->external_scan_running = 1;
  2552. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
  2553. }
  2554. break;
  2555. case EVENT_SCAN_RESULTS:
  2556. if (os_reltime_initialized(&wpa_s->scan_start_time)) {
  2557. struct os_reltime now, diff;
  2558. os_get_reltime(&now);
  2559. os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
  2560. wpa_s->scan_start_time.sec = 0;
  2561. wpa_s->scan_start_time.usec = 0;
  2562. wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
  2563. diff.sec, diff.usec);
  2564. }
  2565. wpa_supplicant_event_scan_results(wpa_s, data);
  2566. wpa_s->own_scan_running = 0;
  2567. wpa_s->external_scan_running = 0;
  2568. radio_work_check_next(wpa_s);
  2569. break;
  2570. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2571. case EVENT_ASSOCINFO:
  2572. wpa_supplicant_event_associnfo(wpa_s, data);
  2573. break;
  2574. case EVENT_INTERFACE_STATUS:
  2575. wpa_supplicant_event_interface_status(wpa_s, data);
  2576. break;
  2577. case EVENT_PMKID_CANDIDATE:
  2578. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  2579. break;
  2580. #ifdef CONFIG_PEERKEY
  2581. case EVENT_STKSTART:
  2582. wpa_supplicant_event_stkstart(wpa_s, data);
  2583. break;
  2584. #endif /* CONFIG_PEERKEY */
  2585. #ifdef CONFIG_TDLS
  2586. case EVENT_TDLS:
  2587. wpa_supplicant_event_tdls(wpa_s, data);
  2588. break;
  2589. #endif /* CONFIG_TDLS */
  2590. #ifdef CONFIG_WNM
  2591. case EVENT_WNM:
  2592. wpa_supplicant_event_wnm(wpa_s, data);
  2593. break;
  2594. #endif /* CONFIG_WNM */
  2595. #ifdef CONFIG_IEEE80211R
  2596. case EVENT_FT_RESPONSE:
  2597. wpa_supplicant_event_ft_response(wpa_s, data);
  2598. break;
  2599. #endif /* CONFIG_IEEE80211R */
  2600. #ifdef CONFIG_IBSS_RSN
  2601. case EVENT_IBSS_RSN_START:
  2602. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  2603. break;
  2604. #endif /* CONFIG_IBSS_RSN */
  2605. case EVENT_ASSOC_REJECT:
  2606. if (data->assoc_reject.bssid)
  2607. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  2608. "bssid=" MACSTR " status_code=%u",
  2609. MAC2STR(data->assoc_reject.bssid),
  2610. data->assoc_reject.status_code);
  2611. else
  2612. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  2613. "status_code=%u",
  2614. data->assoc_reject.status_code);
  2615. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2616. sme_event_assoc_reject(wpa_s, data);
  2617. else {
  2618. const u8 *bssid = data->assoc_reject.bssid;
  2619. if (bssid == NULL || is_zero_ether_addr(bssid))
  2620. bssid = wpa_s->pending_bssid;
  2621. wpas_connection_failed(wpa_s, bssid);
  2622. wpa_supplicant_mark_disassoc(wpa_s);
  2623. }
  2624. break;
  2625. case EVENT_AUTH_TIMED_OUT:
  2626. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2627. sme_event_auth_timed_out(wpa_s, data);
  2628. break;
  2629. case EVENT_ASSOC_TIMED_OUT:
  2630. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2631. sme_event_assoc_timed_out(wpa_s, data);
  2632. break;
  2633. case EVENT_TX_STATUS:
  2634. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
  2635. " type=%d stype=%d",
  2636. MAC2STR(data->tx_status.dst),
  2637. data->tx_status.type, data->tx_status.stype);
  2638. #ifdef CONFIG_AP
  2639. if (wpa_s->ap_iface == NULL) {
  2640. #ifdef CONFIG_OFFCHANNEL
  2641. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  2642. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  2643. offchannel_send_action_tx_status(
  2644. wpa_s, data->tx_status.dst,
  2645. data->tx_status.data,
  2646. data->tx_status.data_len,
  2647. data->tx_status.ack ?
  2648. OFFCHANNEL_SEND_ACTION_SUCCESS :
  2649. OFFCHANNEL_SEND_ACTION_NO_ACK);
  2650. #endif /* CONFIG_OFFCHANNEL */
  2651. break;
  2652. }
  2653. #endif /* CONFIG_AP */
  2654. #ifdef CONFIG_OFFCHANNEL
  2655. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
  2656. MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
  2657. /*
  2658. * Catch TX status events for Action frames we sent via group
  2659. * interface in GO mode.
  2660. */
  2661. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  2662. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  2663. os_memcmp(wpa_s->parent->pending_action_dst,
  2664. data->tx_status.dst, ETH_ALEN) == 0) {
  2665. offchannel_send_action_tx_status(
  2666. wpa_s->parent, data->tx_status.dst,
  2667. data->tx_status.data,
  2668. data->tx_status.data_len,
  2669. data->tx_status.ack ?
  2670. OFFCHANNEL_SEND_ACTION_SUCCESS :
  2671. OFFCHANNEL_SEND_ACTION_NO_ACK);
  2672. break;
  2673. }
  2674. #endif /* CONFIG_OFFCHANNEL */
  2675. #ifdef CONFIG_AP
  2676. switch (data->tx_status.type) {
  2677. case WLAN_FC_TYPE_MGMT:
  2678. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  2679. data->tx_status.data_len,
  2680. data->tx_status.stype,
  2681. data->tx_status.ack);
  2682. break;
  2683. case WLAN_FC_TYPE_DATA:
  2684. ap_tx_status(wpa_s, data->tx_status.dst,
  2685. data->tx_status.data,
  2686. data->tx_status.data_len,
  2687. data->tx_status.ack);
  2688. break;
  2689. }
  2690. #endif /* CONFIG_AP */
  2691. break;
  2692. #ifdef CONFIG_AP
  2693. case EVENT_EAPOL_TX_STATUS:
  2694. ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
  2695. data->eapol_tx_status.data,
  2696. data->eapol_tx_status.data_len,
  2697. data->eapol_tx_status.ack);
  2698. break;
  2699. case EVENT_DRIVER_CLIENT_POLL_OK:
  2700. ap_client_poll_ok(wpa_s, data->client_poll.addr);
  2701. break;
  2702. case EVENT_RX_FROM_UNKNOWN:
  2703. if (wpa_s->ap_iface == NULL)
  2704. break;
  2705. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
  2706. data->rx_from_unknown.wds);
  2707. break;
  2708. case EVENT_CH_SWITCH:
  2709. if (!data)
  2710. break;
  2711. if (!wpa_s->ap_iface) {
  2712. wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
  2713. "event in non-AP mode");
  2714. break;
  2715. }
  2716. wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
  2717. data->ch_switch.ht_enabled,
  2718. data->ch_switch.ch_offset,
  2719. data->ch_switch.ch_width,
  2720. data->ch_switch.cf1,
  2721. data->ch_switch.cf2);
  2722. break;
  2723. #endif /* CONFIG_AP */
  2724. case EVENT_RX_MGMT: {
  2725. u16 fc, stype;
  2726. const struct ieee80211_mgmt *mgmt;
  2727. #ifdef CONFIG_TESTING_OPTIONS
  2728. if (wpa_s->ext_mgmt_frame_handling) {
  2729. struct rx_mgmt *rx = &data->rx_mgmt;
  2730. size_t hex_len = 2 * rx->frame_len + 1;
  2731. char *hex = os_malloc(hex_len);
  2732. if (hex) {
  2733. wpa_snprintf_hex(hex, hex_len,
  2734. rx->frame, rx->frame_len);
  2735. wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
  2736. rx->freq, rx->datarate, rx->ssi_signal,
  2737. hex);
  2738. os_free(hex);
  2739. }
  2740. break;
  2741. }
  2742. #endif /* CONFIG_TESTING_OPTIONS */
  2743. mgmt = (const struct ieee80211_mgmt *)
  2744. data->rx_mgmt.frame;
  2745. fc = le_to_host16(mgmt->frame_control);
  2746. stype = WLAN_FC_GET_STYPE(fc);
  2747. #ifdef CONFIG_AP
  2748. if (wpa_s->ap_iface == NULL) {
  2749. #endif /* CONFIG_AP */
  2750. #ifdef CONFIG_P2P
  2751. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  2752. data->rx_mgmt.frame_len > 24) {
  2753. const u8 *src = mgmt->sa;
  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_p2p_probe_req_rx(
  2759. wpa_s, src, mgmt->da,
  2760. mgmt->bssid, ie, ie_len,
  2761. data->rx_mgmt.ssi_signal);
  2762. break;
  2763. }
  2764. #endif /* CONFIG_P2P */
  2765. #ifdef CONFIG_IBSS_RSN
  2766. if (stype == WLAN_FC_STYPE_AUTH &&
  2767. data->rx_mgmt.frame_len >= 30) {
  2768. wpa_supplicant_event_ibss_auth(wpa_s, data);
  2769. break;
  2770. }
  2771. #endif /* CONFIG_IBSS_RSN */
  2772. if (stype == WLAN_FC_STYPE_ACTION) {
  2773. wpas_event_rx_mgmt_action(
  2774. wpa_s, data->rx_mgmt.frame,
  2775. data->rx_mgmt.frame_len,
  2776. data->rx_mgmt.freq);
  2777. break;
  2778. }
  2779. wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
  2780. "management frame in non-AP mode");
  2781. break;
  2782. #ifdef CONFIG_AP
  2783. }
  2784. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  2785. data->rx_mgmt.frame_len > 24) {
  2786. const u8 *ie = mgmt->u.probe_req.variable;
  2787. size_t ie_len = data->rx_mgmt.frame_len -
  2788. (mgmt->u.probe_req.variable -
  2789. data->rx_mgmt.frame);
  2790. wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
  2791. mgmt->bssid, ie, ie_len,
  2792. data->rx_mgmt.ssi_signal);
  2793. }
  2794. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  2795. #endif /* CONFIG_AP */
  2796. break;
  2797. }
  2798. case EVENT_RX_PROBE_REQ:
  2799. if (data->rx_probe_req.sa == NULL ||
  2800. data->rx_probe_req.ie == NULL)
  2801. break;
  2802. #ifdef CONFIG_AP
  2803. if (wpa_s->ap_iface) {
  2804. hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
  2805. data->rx_probe_req.sa,
  2806. data->rx_probe_req.da,
  2807. data->rx_probe_req.bssid,
  2808. data->rx_probe_req.ie,
  2809. data->rx_probe_req.ie_len,
  2810. data->rx_probe_req.ssi_signal);
  2811. break;
  2812. }
  2813. #endif /* CONFIG_AP */
  2814. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  2815. data->rx_probe_req.da,
  2816. data->rx_probe_req.bssid,
  2817. data->rx_probe_req.ie,
  2818. data->rx_probe_req.ie_len,
  2819. data->rx_probe_req.ssi_signal);
  2820. break;
  2821. case EVENT_REMAIN_ON_CHANNEL:
  2822. #ifdef CONFIG_OFFCHANNEL
  2823. offchannel_remain_on_channel_cb(
  2824. wpa_s, data->remain_on_channel.freq,
  2825. data->remain_on_channel.duration);
  2826. #endif /* CONFIG_OFFCHANNEL */
  2827. wpas_p2p_remain_on_channel_cb(
  2828. wpa_s, data->remain_on_channel.freq,
  2829. data->remain_on_channel.duration);
  2830. break;
  2831. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  2832. #ifdef CONFIG_OFFCHANNEL
  2833. offchannel_cancel_remain_on_channel_cb(
  2834. wpa_s, data->remain_on_channel.freq);
  2835. #endif /* CONFIG_OFFCHANNEL */
  2836. wpas_p2p_cancel_remain_on_channel_cb(
  2837. wpa_s, data->remain_on_channel.freq);
  2838. break;
  2839. case EVENT_EAPOL_RX:
  2840. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  2841. data->eapol_rx.data,
  2842. data->eapol_rx.data_len);
  2843. break;
  2844. case EVENT_SIGNAL_CHANGE:
  2845. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
  2846. "above=%d signal=%d noise=%d txrate=%d",
  2847. data->signal_change.above_threshold,
  2848. data->signal_change.current_signal,
  2849. data->signal_change.current_noise,
  2850. data->signal_change.current_txrate);
  2851. bgscan_notify_signal_change(
  2852. wpa_s, data->signal_change.above_threshold,
  2853. data->signal_change.current_signal,
  2854. data->signal_change.current_noise,
  2855. data->signal_change.current_txrate);
  2856. break;
  2857. case EVENT_INTERFACE_ENABLED:
  2858. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
  2859. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2860. wpa_supplicant_update_mac_addr(wpa_s);
  2861. if (wpa_s->p2p_mgmt) {
  2862. wpa_supplicant_set_state(wpa_s,
  2863. WPA_DISCONNECTED);
  2864. break;
  2865. }
  2866. #ifdef CONFIG_AP
  2867. if (!wpa_s->ap_iface) {
  2868. wpa_supplicant_set_state(wpa_s,
  2869. WPA_DISCONNECTED);
  2870. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2871. } else
  2872. wpa_supplicant_set_state(wpa_s,
  2873. WPA_COMPLETED);
  2874. #else /* CONFIG_AP */
  2875. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2876. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2877. #endif /* CONFIG_AP */
  2878. }
  2879. break;
  2880. case EVENT_INTERFACE_DISABLED:
  2881. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
  2882. #ifdef CONFIG_P2P
  2883. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  2884. (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
  2885. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
  2886. /*
  2887. * The interface was externally disabled. Remove
  2888. * it assuming an external entity will start a
  2889. * new session if needed.
  2890. */
  2891. wpas_p2p_disconnect(wpa_s);
  2892. break;
  2893. }
  2894. if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
  2895. p2p_in_progress(wpa_s->global->p2p) > 1) {
  2896. /* This radio work will be cancelled, so clear P2P
  2897. * state as well.
  2898. */
  2899. p2p_stop_find(wpa_s->global->p2p);
  2900. }
  2901. #endif /* CONFIG_P2P */
  2902. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2903. /*
  2904. * Indicate disconnection to keep ctrl_iface events
  2905. * consistent.
  2906. */
  2907. wpa_supplicant_event_disassoc(
  2908. wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
  2909. }
  2910. wpa_supplicant_mark_disassoc(wpa_s);
  2911. radio_remove_works(wpa_s, NULL, 0);
  2912. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2913. break;
  2914. case EVENT_CHANNEL_LIST_CHANGED:
  2915. wpa_supplicant_update_channel_list(
  2916. wpa_s, &data->channel_list_changed);
  2917. break;
  2918. case EVENT_INTERFACE_UNAVAILABLE:
  2919. wpas_p2p_interface_unavailable(wpa_s);
  2920. break;
  2921. case EVENT_BEST_CHANNEL:
  2922. wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
  2923. "(%d %d %d)",
  2924. data->best_chan.freq_24, data->best_chan.freq_5,
  2925. data->best_chan.freq_overall);
  2926. wpa_s->best_24_freq = data->best_chan.freq_24;
  2927. wpa_s->best_5_freq = data->best_chan.freq_5;
  2928. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  2929. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  2930. data->best_chan.freq_5,
  2931. data->best_chan.freq_overall);
  2932. break;
  2933. case EVENT_UNPROT_DEAUTH:
  2934. wpa_supplicant_event_unprot_deauth(wpa_s,
  2935. &data->unprot_deauth);
  2936. break;
  2937. case EVENT_UNPROT_DISASSOC:
  2938. wpa_supplicant_event_unprot_disassoc(wpa_s,
  2939. &data->unprot_disassoc);
  2940. break;
  2941. case EVENT_STATION_LOW_ACK:
  2942. #ifdef CONFIG_AP
  2943. if (wpa_s->ap_iface && data)
  2944. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  2945. data->low_ack.addr);
  2946. #endif /* CONFIG_AP */
  2947. #ifdef CONFIG_TDLS
  2948. if (data)
  2949. wpa_tdls_disable_unreachable_link(wpa_s->wpa,
  2950. data->low_ack.addr);
  2951. #endif /* CONFIG_TDLS */
  2952. break;
  2953. case EVENT_IBSS_PEER_LOST:
  2954. #ifdef CONFIG_IBSS_RSN
  2955. ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
  2956. #endif /* CONFIG_IBSS_RSN */
  2957. break;
  2958. case EVENT_DRIVER_GTK_REKEY:
  2959. if (os_memcmp(data->driver_gtk_rekey.bssid,
  2960. wpa_s->bssid, ETH_ALEN))
  2961. break;
  2962. if (!wpa_s->wpa)
  2963. break;
  2964. wpa_sm_update_replay_ctr(wpa_s->wpa,
  2965. data->driver_gtk_rekey.replay_ctr);
  2966. break;
  2967. case EVENT_SCHED_SCAN_STOPPED:
  2968. wpa_s->pno = 0;
  2969. wpa_s->sched_scanning = 0;
  2970. wpa_supplicant_notify_scanning(wpa_s, 0);
  2971. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2972. break;
  2973. /*
  2974. * Start a new sched scan to continue searching for more SSIDs
  2975. * either if timed out or PNO schedule scan is pending.
  2976. */
  2977. if (wpa_s->sched_scan_timed_out) {
  2978. wpa_supplicant_req_sched_scan(wpa_s);
  2979. } else if (wpa_s->pno_sched_pending) {
  2980. wpa_s->pno_sched_pending = 0;
  2981. wpas_start_pno(wpa_s);
  2982. }
  2983. break;
  2984. case EVENT_WPS_BUTTON_PUSHED:
  2985. #ifdef CONFIG_WPS
  2986. wpas_wps_start_pbc(wpa_s, NULL, 0);
  2987. #endif /* CONFIG_WPS */
  2988. break;
  2989. case EVENT_AVOID_FREQUENCIES:
  2990. wpa_supplicant_notify_avoid_freq(wpa_s, data);
  2991. break;
  2992. case EVENT_CONNECT_FAILED_REASON:
  2993. #ifdef CONFIG_AP
  2994. if (!wpa_s->ap_iface || !data)
  2995. break;
  2996. hostapd_event_connect_failed_reason(
  2997. wpa_s->ap_iface->bss[0],
  2998. data->connect_failed_reason.addr,
  2999. data->connect_failed_reason.code);
  3000. #endif /* CONFIG_AP */
  3001. break;
  3002. default:
  3003. wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
  3004. break;
  3005. }
  3006. }