events.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472
  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. static int bss_is_dmg(struct wpa_bss *bss)
  590. {
  591. return bss->freq > 45000;
  592. }
  593. /*
  594. * Test whether BSS is in an ESS.
  595. * This is done differently in DMG (60 GHz) and non-DMG bands
  596. */
  597. static int bss_is_ess(struct wpa_bss *bss)
  598. {
  599. if (bss_is_dmg(bss)) {
  600. return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
  601. IEEE80211_CAP_DMG_AP;
  602. }
  603. return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
  604. IEEE80211_CAP_ESS);
  605. }
  606. static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
  607. int i, struct wpa_bss *bss,
  608. struct wpa_ssid *group,
  609. int only_first_ssid)
  610. {
  611. u8 wpa_ie_len, rsn_ie_len;
  612. int wpa;
  613. struct wpa_blacklist *e;
  614. const u8 *ie;
  615. struct wpa_ssid *ssid;
  616. int osen;
  617. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  618. wpa_ie_len = ie ? ie[1] : 0;
  619. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  620. rsn_ie_len = ie ? ie[1] : 0;
  621. ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  622. osen = ie != NULL;
  623. wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  624. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
  625. i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
  626. wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
  627. wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
  628. (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  629. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
  630. " p2p" : "",
  631. osen ? " osen=1" : "");
  632. e = wpa_blacklist_get(wpa_s, bss->bssid);
  633. if (e) {
  634. int limit = 1;
  635. if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
  636. /*
  637. * When only a single network is enabled, we can
  638. * trigger blacklisting on the first failure. This
  639. * should not be done with multiple enabled networks to
  640. * avoid getting forced to move into a worse ESS on
  641. * single error if there are no other BSSes of the
  642. * current ESS.
  643. */
  644. limit = 0;
  645. }
  646. if (e->count > limit) {
  647. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  648. "(count=%d limit=%d)", e->count, limit);
  649. return NULL;
  650. }
  651. }
  652. if (bss->ssid_len == 0) {
  653. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
  654. return NULL;
  655. }
  656. if (disallowed_bssid(wpa_s, bss->bssid)) {
  657. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
  658. return NULL;
  659. }
  660. if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  661. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
  662. return NULL;
  663. }
  664. wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
  665. for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
  666. int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
  667. int res;
  668. if (wpas_network_disabled(wpa_s, ssid)) {
  669. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
  670. continue;
  671. }
  672. res = wpas_temp_disabled(wpa_s, ssid);
  673. if (res > 0) {
  674. wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
  675. "temporarily for %d second(s)", res);
  676. continue;
  677. }
  678. #ifdef CONFIG_WPS
  679. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
  680. wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
  681. "(WPS)");
  682. continue;
  683. }
  684. if (wpa && ssid->ssid_len == 0 &&
  685. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  686. check_ssid = 0;
  687. if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  688. /* Only allow wildcard SSID match if an AP
  689. * advertises active WPS operation that matches
  690. * with our mode. */
  691. check_ssid = 1;
  692. if (ssid->ssid_len == 0 &&
  693. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  694. check_ssid = 0;
  695. }
  696. #endif /* CONFIG_WPS */
  697. if (ssid->bssid_set && ssid->ssid_len == 0 &&
  698. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
  699. check_ssid = 0;
  700. if (check_ssid &&
  701. (bss->ssid_len != ssid->ssid_len ||
  702. os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
  703. wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
  704. continue;
  705. }
  706. if (ssid->bssid_set &&
  707. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
  708. wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
  709. continue;
  710. }
  711. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  712. continue;
  713. if (!osen && !wpa &&
  714. !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  715. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  716. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
  717. wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
  718. "not allowed");
  719. continue;
  720. }
  721. if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
  722. has_wep_key(ssid)) {
  723. wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
  724. continue;
  725. }
  726. if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
  727. wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
  728. "not allowed");
  729. continue;
  730. }
  731. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  732. wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
  733. "mismatch");
  734. continue;
  735. }
  736. if (!bss_is_ess(bss)) {
  737. wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
  738. continue;
  739. }
  740. if (!freq_allowed(ssid->freq_list, bss->freq)) {
  741. wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
  742. "allowed");
  743. continue;
  744. }
  745. if (!rate_match(wpa_s, bss)) {
  746. wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
  747. "not match");
  748. continue;
  749. }
  750. #ifdef CONFIG_P2P
  751. if (ssid->p2p_group &&
  752. !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  753. !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  754. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
  755. continue;
  756. }
  757. if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
  758. struct wpabuf *p2p_ie;
  759. u8 dev_addr[ETH_ALEN];
  760. ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  761. if (ie == NULL) {
  762. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
  763. continue;
  764. }
  765. p2p_ie = wpa_bss_get_vendor_ie_multi(
  766. bss, P2P_IE_VENDOR_TYPE);
  767. if (p2p_ie == NULL) {
  768. wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
  769. continue;
  770. }
  771. if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
  772. || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
  773. ETH_ALEN) != 0) {
  774. wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
  775. wpabuf_free(p2p_ie);
  776. continue;
  777. }
  778. wpabuf_free(p2p_ie);
  779. }
  780. /*
  781. * TODO: skip the AP if its P2P IE has Group Formation
  782. * bit set in the P2P Group Capability Bitmap and we
  783. * are not in Group Formation with that device.
  784. */
  785. #endif /* CONFIG_P2P */
  786. /* Matching configuration found */
  787. return ssid;
  788. }
  789. /* No matching configuration found */
  790. return NULL;
  791. }
  792. static struct wpa_bss *
  793. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  794. struct wpa_ssid *group,
  795. struct wpa_ssid **selected_ssid,
  796. int only_first_ssid)
  797. {
  798. unsigned int i;
  799. if (only_first_ssid)
  800. wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
  801. group->id);
  802. else
  803. wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
  804. group->priority);
  805. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  806. struct wpa_bss *bss = wpa_s->last_scan_res[i];
  807. *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
  808. only_first_ssid);
  809. if (!*selected_ssid)
  810. continue;
  811. wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
  812. " ssid='%s'",
  813. MAC2STR(bss->bssid),
  814. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  815. return bss;
  816. }
  817. return NULL;
  818. }
  819. struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  820. struct wpa_ssid **selected_ssid)
  821. {
  822. struct wpa_bss *selected = NULL;
  823. int prio;
  824. struct wpa_ssid *next_ssid = NULL;
  825. if (wpa_s->last_scan_res == NULL ||
  826. wpa_s->last_scan_res_used == 0)
  827. return NULL; /* no scan results from last update */
  828. if (wpa_s->next_ssid) {
  829. struct wpa_ssid *ssid;
  830. /* check that next_ssid is still valid */
  831. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  832. if (ssid == wpa_s->next_ssid)
  833. break;
  834. }
  835. next_ssid = ssid;
  836. wpa_s->next_ssid = NULL;
  837. }
  838. while (selected == NULL) {
  839. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  840. if (next_ssid && next_ssid->priority ==
  841. wpa_s->conf->pssid[prio]->priority) {
  842. selected = wpa_supplicant_select_bss(
  843. wpa_s, next_ssid, selected_ssid, 1);
  844. if (selected)
  845. break;
  846. }
  847. selected = wpa_supplicant_select_bss(
  848. wpa_s, wpa_s->conf->pssid[prio],
  849. selected_ssid, 0);
  850. if (selected)
  851. break;
  852. }
  853. if (selected == NULL && wpa_s->blacklist &&
  854. !wpa_s->countermeasures) {
  855. wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
  856. "blacklist and try again");
  857. wpa_blacklist_clear(wpa_s);
  858. wpa_s->blacklist_cleared++;
  859. } else if (selected == NULL)
  860. break;
  861. }
  862. return selected;
  863. }
  864. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  865. int timeout_sec, int timeout_usec)
  866. {
  867. if (!wpa_supplicant_enabled_networks(wpa_s)) {
  868. /*
  869. * No networks are enabled; short-circuit request so
  870. * we don't wait timeout seconds before transitioning
  871. * to INACTIVE state.
  872. */
  873. wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
  874. "since there are no enabled networks");
  875. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  876. return;
  877. }
  878. wpa_s->scan_for_connection = 1;
  879. wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
  880. }
  881. int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  882. struct wpa_bss *selected,
  883. struct wpa_ssid *ssid)
  884. {
  885. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  886. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  887. "PBC session overlap");
  888. #ifdef CONFIG_P2P
  889. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  890. wpa_s->p2p_in_provisioning) {
  891. eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
  892. wpa_s, NULL);
  893. return -1;
  894. }
  895. #endif /* CONFIG_P2P */
  896. #ifdef CONFIG_WPS
  897. wpas_wps_cancel(wpa_s);
  898. #endif /* CONFIG_WPS */
  899. return -1;
  900. }
  901. wpa_msg(wpa_s, MSG_DEBUG,
  902. "Considering connect request: reassociate: %d selected: "
  903. MACSTR " bssid: " MACSTR " pending: " MACSTR
  904. " wpa_state: %s ssid=%p current_ssid=%p",
  905. wpa_s->reassociate, MAC2STR(selected->bssid),
  906. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  907. wpa_supplicant_state_txt(wpa_s->wpa_state),
  908. ssid, wpa_s->current_ssid);
  909. /*
  910. * Do not trigger new association unless the BSSID has changed or if
  911. * reassociation is requested. If we are in process of associating with
  912. * the selected BSSID, do not trigger new attempt.
  913. */
  914. if (wpa_s->reassociate ||
  915. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  916. ((wpa_s->wpa_state != WPA_ASSOCIATING &&
  917. wpa_s->wpa_state != WPA_AUTHENTICATING) ||
  918. (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  919. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  920. 0) ||
  921. (is_zero_ether_addr(wpa_s->pending_bssid) &&
  922. ssid != wpa_s->current_ssid)))) {
  923. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  924. wpa_supplicant_req_new_scan(wpa_s, 10, 0);
  925. return 0;
  926. }
  927. wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
  928. MAC2STR(selected->bssid));
  929. wpa_supplicant_associate(wpa_s, selected, ssid);
  930. } else {
  931. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
  932. "connect with the selected AP");
  933. }
  934. return 0;
  935. }
  936. static struct wpa_ssid *
  937. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  938. {
  939. int prio;
  940. struct wpa_ssid *ssid;
  941. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  942. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  943. {
  944. if (wpas_network_disabled(wpa_s, ssid))
  945. continue;
  946. if (ssid->mode == IEEE80211_MODE_IBSS ||
  947. ssid->mode == IEEE80211_MODE_AP)
  948. return ssid;
  949. }
  950. }
  951. return NULL;
  952. }
  953. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  954. * on BSS added and BSS changed events */
  955. static void wpa_supplicant_rsn_preauth_scan_results(
  956. struct wpa_supplicant *wpa_s)
  957. {
  958. struct wpa_bss *bss;
  959. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  960. return;
  961. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  962. const u8 *ssid, *rsn;
  963. ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  964. if (ssid == NULL)
  965. continue;
  966. rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  967. if (rsn == NULL)
  968. continue;
  969. rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
  970. }
  971. }
  972. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  973. struct wpa_bss *selected,
  974. struct wpa_ssid *ssid)
  975. {
  976. struct wpa_bss *current_bss = NULL;
  977. int min_diff;
  978. if (wpa_s->reassociate)
  979. return 1; /* explicit request to reassociate */
  980. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  981. return 1; /* we are not associated; continue */
  982. if (wpa_s->current_ssid == NULL)
  983. return 1; /* unknown current SSID */
  984. if (wpa_s->current_ssid != ssid)
  985. return 1; /* different network block */
  986. if (wpas_driver_bss_selection(wpa_s))
  987. return 0; /* Driver-based roaming */
  988. if (wpa_s->current_ssid->ssid)
  989. current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
  990. wpa_s->current_ssid->ssid,
  991. wpa_s->current_ssid->ssid_len);
  992. if (!current_bss)
  993. current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
  994. if (!current_bss)
  995. return 1; /* current BSS not seen in scan results */
  996. if (current_bss == selected)
  997. return 0;
  998. if (selected->last_update_idx > current_bss->last_update_idx)
  999. return 1; /* current BSS not seen in the last scan */
  1000. #ifndef CONFIG_NO_ROAMING
  1001. wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
  1002. wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  1003. MAC2STR(current_bss->bssid), current_bss->level);
  1004. wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  1005. MAC2STR(selected->bssid), selected->level);
  1006. if (wpa_s->current_ssid->bssid_set &&
  1007. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  1008. 0) {
  1009. wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
  1010. "has preferred BSSID");
  1011. return 1;
  1012. }
  1013. if (current_bss->level < 0 && current_bss->level > selected->level) {
  1014. wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
  1015. "signal level");
  1016. return 0;
  1017. }
  1018. min_diff = 2;
  1019. if (current_bss->level < 0) {
  1020. if (current_bss->level < -85)
  1021. min_diff = 1;
  1022. else if (current_bss->level < -80)
  1023. min_diff = 2;
  1024. else if (current_bss->level < -75)
  1025. min_diff = 3;
  1026. else if (current_bss->level < -70)
  1027. min_diff = 4;
  1028. else
  1029. min_diff = 5;
  1030. }
  1031. if (abs(current_bss->level - selected->level) < min_diff) {
  1032. wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
  1033. "in signal level");
  1034. return 0;
  1035. }
  1036. return 1;
  1037. #else /* CONFIG_NO_ROAMING */
  1038. return 0;
  1039. #endif /* CONFIG_NO_ROAMING */
  1040. }
  1041. /* Return != 0 if no scan results could be fetched or if scan results should not
  1042. * be shared with other virtual interfaces. */
  1043. static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  1044. union wpa_event_data *data,
  1045. int own_request)
  1046. {
  1047. struct wpa_scan_results *scan_res = NULL;
  1048. int ret = 0;
  1049. int ap = 0;
  1050. #ifndef CONFIG_NO_RANDOM_POOL
  1051. size_t i, num;
  1052. #endif /* CONFIG_NO_RANDOM_POOL */
  1053. #ifdef CONFIG_AP
  1054. if (wpa_s->ap_iface)
  1055. ap = 1;
  1056. #endif /* CONFIG_AP */
  1057. wpa_supplicant_notify_scanning(wpa_s, 0);
  1058. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  1059. data ? &data->scan_info :
  1060. NULL, 1);
  1061. if (scan_res == NULL) {
  1062. if (wpa_s->conf->ap_scan == 2 || ap ||
  1063. wpa_s->scan_res_handler == scan_only_handler)
  1064. return -1;
  1065. if (!own_request)
  1066. return -1;
  1067. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
  1068. "scanning again");
  1069. wpa_supplicant_req_new_scan(wpa_s, 1, 0);
  1070. ret = -1;
  1071. goto scan_work_done;
  1072. }
  1073. #ifndef CONFIG_NO_RANDOM_POOL
  1074. num = scan_res->num;
  1075. if (num > 10)
  1076. num = 10;
  1077. for (i = 0; i < num; i++) {
  1078. u8 buf[5];
  1079. struct wpa_scan_res *res = scan_res->res[i];
  1080. buf[0] = res->bssid[5];
  1081. buf[1] = res->qual & 0xff;
  1082. buf[2] = res->noise & 0xff;
  1083. buf[3] = res->level & 0xff;
  1084. buf[4] = res->tsf & 0xff;
  1085. random_add_randomness(buf, sizeof(buf));
  1086. }
  1087. #endif /* CONFIG_NO_RANDOM_POOL */
  1088. if (own_request && wpa_s->scan_res_handler &&
  1089. (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
  1090. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  1091. struct wpa_scan_results *scan_res);
  1092. scan_res_handler = wpa_s->scan_res_handler;
  1093. wpa_s->scan_res_handler = NULL;
  1094. scan_res_handler(wpa_s, scan_res);
  1095. ret = -2;
  1096. goto scan_work_done;
  1097. }
  1098. if (ap) {
  1099. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
  1100. #ifdef CONFIG_AP
  1101. if (wpa_s->ap_iface->scan_cb)
  1102. wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
  1103. #endif /* CONFIG_AP */
  1104. goto scan_work_done;
  1105. }
  1106. wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
  1107. wpa_s->own_scan_running, wpa_s->external_scan_running);
  1108. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1109. wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
  1110. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1111. wpa_s->manual_scan_id);
  1112. wpa_s->manual_scan_use_id = 0;
  1113. } else {
  1114. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1115. }
  1116. wpas_notify_scan_results(wpa_s);
  1117. wpas_notify_scan_done(wpa_s, 1);
  1118. if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
  1119. wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
  1120. wpa_scan_results_free(scan_res);
  1121. return 0;
  1122. }
  1123. if (sme_proc_obss_scan(wpa_s) > 0)
  1124. goto scan_work_done;
  1125. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
  1126. goto scan_work_done;
  1127. if (autoscan_notify_scan(wpa_s, scan_res))
  1128. goto scan_work_done;
  1129. if (wpa_s->disconnected) {
  1130. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1131. goto scan_work_done;
  1132. }
  1133. if (!wpas_driver_bss_selection(wpa_s) &&
  1134. bgscan_notify_scan(wpa_s, scan_res) == 1)
  1135. goto scan_work_done;
  1136. wpas_wps_update_ap_info(wpa_s, scan_res);
  1137. wpa_scan_results_free(scan_res);
  1138. if (wpa_s->scan_work) {
  1139. struct wpa_radio_work *work = wpa_s->scan_work;
  1140. wpa_s->scan_work = NULL;
  1141. radio_work_done(work);
  1142. }
  1143. return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
  1144. scan_work_done:
  1145. wpa_scan_results_free(scan_res);
  1146. if (wpa_s->scan_work) {
  1147. struct wpa_radio_work *work = wpa_s->scan_work;
  1148. wpa_s->scan_work = NULL;
  1149. radio_work_done(work);
  1150. }
  1151. return ret;
  1152. }
  1153. static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
  1154. int new_scan, int own_request)
  1155. {
  1156. struct wpa_bss *selected;
  1157. struct wpa_ssid *ssid = NULL;
  1158. selected = wpa_supplicant_pick_network(wpa_s, &ssid);
  1159. if (selected) {
  1160. int skip;
  1161. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
  1162. if (skip) {
  1163. if (new_scan)
  1164. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  1165. return 0;
  1166. }
  1167. if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
  1168. wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
  1169. return -1;
  1170. }
  1171. if (new_scan)
  1172. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  1173. /*
  1174. * Do not notify other virtual radios of scan results since we do not
  1175. * want them to start other associations at the same time.
  1176. */
  1177. return 1;
  1178. } else {
  1179. wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
  1180. ssid = wpa_supplicant_pick_new_network(wpa_s);
  1181. if (ssid) {
  1182. wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
  1183. wpa_supplicant_associate(wpa_s, NULL, ssid);
  1184. if (new_scan)
  1185. wpa_supplicant_rsn_preauth_scan_results(wpa_s);
  1186. } else if (own_request) {
  1187. /*
  1188. * No SSID found. If SCAN results are as a result of
  1189. * own scan request and not due to a scan request on
  1190. * another shared interface, try another scan.
  1191. */
  1192. int timeout_sec = wpa_s->scan_interval;
  1193. int timeout_usec = 0;
  1194. #ifdef CONFIG_P2P
  1195. if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
  1196. return 0;
  1197. if (wpa_s->p2p_in_provisioning ||
  1198. wpa_s->show_group_started ||
  1199. wpa_s->p2p_in_invitation) {
  1200. /*
  1201. * Use shorter wait during P2P Provisioning
  1202. * state and during P2P join-a-group operation
  1203. * to speed up group formation.
  1204. */
  1205. timeout_sec = 0;
  1206. timeout_usec = 250000;
  1207. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1208. timeout_usec);
  1209. return 0;
  1210. }
  1211. #endif /* CONFIG_P2P */
  1212. #ifdef CONFIG_INTERWORKING
  1213. if (wpa_s->conf->auto_interworking &&
  1214. wpa_s->conf->interworking &&
  1215. wpa_s->conf->cred) {
  1216. wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
  1217. "start ANQP fetch since no matching "
  1218. "networks found");
  1219. wpa_s->network_select = 1;
  1220. wpa_s->auto_network_select = 1;
  1221. interworking_start_fetch_anqp(wpa_s);
  1222. return 1;
  1223. }
  1224. #endif /* CONFIG_INTERWORKING */
  1225. #ifdef CONFIG_WPS
  1226. if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
  1227. wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
  1228. timeout_sec = 0;
  1229. timeout_usec = 500000;
  1230. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1231. timeout_usec);
  1232. return 0;
  1233. }
  1234. #endif /* CONFIG_WPS */
  1235. if (wpa_supplicant_req_sched_scan(wpa_s))
  1236. wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
  1237. timeout_usec);
  1238. }
  1239. }
  1240. return 0;
  1241. }
  1242. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  1243. union wpa_event_data *data)
  1244. {
  1245. struct wpa_supplicant *ifs;
  1246. if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
  1247. /*
  1248. * If no scan results could be fetched, then no need to
  1249. * notify those interfaces that did not actually request
  1250. * this scan. Similarly, if scan results started a new operation on this
  1251. * interface, do not notify other interfaces to avoid concurrent
  1252. * operations during a connection attempt.
  1253. */
  1254. return;
  1255. }
  1256. /*
  1257. * Check other interfaces to see if they share the same radio. If
  1258. * so, they get updated with this same scan info.
  1259. */
  1260. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  1261. radio_list) {
  1262. if (ifs != wpa_s) {
  1263. wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
  1264. "sibling", ifs->ifname);
  1265. _wpa_supplicant_event_scan_results(ifs, data, 0);
  1266. }
  1267. }
  1268. }
  1269. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1270. int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
  1271. {
  1272. #ifdef CONFIG_NO_SCAN_PROCESSING
  1273. return -1;
  1274. #else /* CONFIG_NO_SCAN_PROCESSING */
  1275. struct os_reltime now;
  1276. if (wpa_s->last_scan_res_used <= 0)
  1277. return -1;
  1278. os_get_reltime(&now);
  1279. if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
  1280. wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
  1281. return -1;
  1282. }
  1283. return wpas_select_network_from_last_scan(wpa_s, 0, 1);
  1284. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1285. }
  1286. #ifdef CONFIG_WNM
  1287. static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
  1288. {
  1289. struct wpa_supplicant *wpa_s = eloop_ctx;
  1290. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  1291. return;
  1292. if (!wpa_s->no_keep_alive) {
  1293. wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
  1294. MAC2STR(wpa_s->bssid));
  1295. /* TODO: could skip this if normal data traffic has been sent */
  1296. /* TODO: Consider using some more appropriate data frame for
  1297. * this */
  1298. if (wpa_s->l2)
  1299. l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
  1300. (u8 *) "", 0);
  1301. }
  1302. #ifdef CONFIG_SME
  1303. if (wpa_s->sme.bss_max_idle_period) {
  1304. unsigned int msec;
  1305. msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
  1306. if (msec > 100)
  1307. msec -= 100;
  1308. eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
  1309. wnm_bss_keep_alive, wpa_s, NULL);
  1310. }
  1311. #endif /* CONFIG_SME */
  1312. }
  1313. static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
  1314. const u8 *ies, size_t ies_len)
  1315. {
  1316. struct ieee802_11_elems elems;
  1317. if (ies == NULL)
  1318. return;
  1319. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
  1320. return;
  1321. #ifdef CONFIG_SME
  1322. if (elems.bss_max_idle_period) {
  1323. unsigned int msec;
  1324. wpa_s->sme.bss_max_idle_period =
  1325. WPA_GET_LE16(elems.bss_max_idle_period);
  1326. wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
  1327. "TU)%s", wpa_s->sme.bss_max_idle_period,
  1328. (elems.bss_max_idle_period[2] & 0x01) ?
  1329. " (protected keep-live required)" : "");
  1330. if (wpa_s->sme.bss_max_idle_period == 0)
  1331. wpa_s->sme.bss_max_idle_period = 1;
  1332. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
  1333. eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
  1334. /* msec times 1000 */
  1335. msec = wpa_s->sme.bss_max_idle_period * 1024;
  1336. if (msec > 100)
  1337. msec -= 100;
  1338. eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
  1339. wnm_bss_keep_alive, wpa_s,
  1340. NULL);
  1341. }
  1342. }
  1343. #endif /* CONFIG_SME */
  1344. }
  1345. #endif /* CONFIG_WNM */
  1346. void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
  1347. {
  1348. #ifdef CONFIG_WNM
  1349. eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
  1350. #endif /* CONFIG_WNM */
  1351. }
  1352. #ifdef CONFIG_INTERWORKING
  1353. static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
  1354. size_t len)
  1355. {
  1356. int res;
  1357. wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
  1358. res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
  1359. if (res) {
  1360. wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
  1361. }
  1362. return res;
  1363. }
  1364. static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
  1365. const u8 *ies, size_t ies_len)
  1366. {
  1367. struct ieee802_11_elems elems;
  1368. if (ies == NULL)
  1369. return;
  1370. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
  1371. return;
  1372. if (elems.qos_map_set) {
  1373. wpas_qos_map_set(wpa_s, elems.qos_map_set,
  1374. elems.qos_map_set_len);
  1375. }
  1376. }
  1377. #endif /* CONFIG_INTERWORKING */
  1378. static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  1379. union wpa_event_data *data)
  1380. {
  1381. int l, len, found = 0, wpa_found, rsn_found;
  1382. const u8 *p;
  1383. #ifdef CONFIG_IEEE80211R
  1384. u8 bssid[ETH_ALEN];
  1385. #endif /* CONFIG_IEEE80211R */
  1386. wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
  1387. if (data->assoc_info.req_ies)
  1388. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  1389. data->assoc_info.req_ies_len);
  1390. if (data->assoc_info.resp_ies) {
  1391. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  1392. data->assoc_info.resp_ies_len);
  1393. #ifdef CONFIG_TDLS
  1394. wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
  1395. data->assoc_info.resp_ies_len);
  1396. #endif /* CONFIG_TDLS */
  1397. #ifdef CONFIG_WNM
  1398. wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
  1399. data->assoc_info.resp_ies_len);
  1400. #endif /* CONFIG_WNM */
  1401. #ifdef CONFIG_INTERWORKING
  1402. interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
  1403. data->assoc_info.resp_ies_len);
  1404. #endif /* CONFIG_INTERWORKING */
  1405. }
  1406. if (data->assoc_info.beacon_ies)
  1407. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  1408. data->assoc_info.beacon_ies,
  1409. data->assoc_info.beacon_ies_len);
  1410. if (data->assoc_info.freq)
  1411. wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
  1412. data->assoc_info.freq);
  1413. p = data->assoc_info.req_ies;
  1414. l = data->assoc_info.req_ies_len;
  1415. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  1416. while (p && l >= 2) {
  1417. len = p[1] + 2;
  1418. if (len > l) {
  1419. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1420. p, l);
  1421. break;
  1422. }
  1423. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1424. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  1425. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  1426. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  1427. break;
  1428. found = 1;
  1429. wpa_find_assoc_pmkid(wpa_s);
  1430. break;
  1431. }
  1432. l -= len;
  1433. p += len;
  1434. }
  1435. if (!found && data->assoc_info.req_ies)
  1436. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1437. #ifdef CONFIG_IEEE80211R
  1438. #ifdef CONFIG_SME
  1439. if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
  1440. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1441. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1442. data->assoc_info.resp_ies,
  1443. data->assoc_info.resp_ies_len,
  1444. bssid) < 0) {
  1445. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1446. "Reassociation Response failed");
  1447. wpa_supplicant_deauthenticate(
  1448. wpa_s, WLAN_REASON_INVALID_IE);
  1449. return -1;
  1450. }
  1451. }
  1452. p = data->assoc_info.resp_ies;
  1453. l = data->assoc_info.resp_ies_len;
  1454. #ifdef CONFIG_WPS_STRICT
  1455. if (p && wpa_s->current_ssid &&
  1456. wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1457. struct wpabuf *wps;
  1458. wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
  1459. if (wps == NULL) {
  1460. wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
  1461. "include WPS IE in (Re)Association Response");
  1462. return -1;
  1463. }
  1464. if (wps_validate_assoc_resp(wps) < 0) {
  1465. wpabuf_free(wps);
  1466. wpa_supplicant_deauthenticate(
  1467. wpa_s, WLAN_REASON_INVALID_IE);
  1468. return -1;
  1469. }
  1470. wpabuf_free(wps);
  1471. }
  1472. #endif /* CONFIG_WPS_STRICT */
  1473. /* Go through the IEs and make a copy of the MDIE, if present. */
  1474. while (p && l >= 2) {
  1475. len = p[1] + 2;
  1476. if (len > l) {
  1477. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  1478. p, l);
  1479. break;
  1480. }
  1481. if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
  1482. p[1] >= MOBILITY_DOMAIN_ID_LEN) {
  1483. wpa_s->sme.ft_used = 1;
  1484. os_memcpy(wpa_s->sme.mobility_domain, p + 2,
  1485. MOBILITY_DOMAIN_ID_LEN);
  1486. break;
  1487. }
  1488. l -= len;
  1489. p += len;
  1490. }
  1491. #endif /* CONFIG_SME */
  1492. /* Process FT when SME is in the driver */
  1493. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1494. wpa_ft_is_completed(wpa_s->wpa)) {
  1495. if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
  1496. wpa_ft_validate_reassoc_resp(wpa_s->wpa,
  1497. data->assoc_info.resp_ies,
  1498. data->assoc_info.resp_ies_len,
  1499. bssid) < 0) {
  1500. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
  1501. "Reassociation Response failed");
  1502. wpa_supplicant_deauthenticate(
  1503. wpa_s, WLAN_REASON_INVALID_IE);
  1504. return -1;
  1505. }
  1506. wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
  1507. }
  1508. wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
  1509. data->assoc_info.resp_ies_len);
  1510. #endif /* CONFIG_IEEE80211R */
  1511. /* WPA/RSN IE from Beacon/ProbeResp */
  1512. p = data->assoc_info.beacon_ies;
  1513. l = data->assoc_info.beacon_ies_len;
  1514. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  1515. */
  1516. wpa_found = rsn_found = 0;
  1517. while (p && l >= 2) {
  1518. len = p[1] + 2;
  1519. if (len > l) {
  1520. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  1521. p, l);
  1522. break;
  1523. }
  1524. if (!wpa_found &&
  1525. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  1526. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  1527. wpa_found = 1;
  1528. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  1529. }
  1530. if (!rsn_found &&
  1531. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  1532. rsn_found = 1;
  1533. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  1534. }
  1535. l -= len;
  1536. p += len;
  1537. }
  1538. if (!wpa_found && data->assoc_info.beacon_ies)
  1539. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  1540. if (!rsn_found && data->assoc_info.beacon_ies)
  1541. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  1542. if (wpa_found || rsn_found)
  1543. wpa_s->ap_ies_from_associnfo = 1;
  1544. if (wpa_s->assoc_freq && data->assoc_info.freq &&
  1545. wpa_s->assoc_freq != data->assoc_info.freq) {
  1546. wpa_printf(MSG_DEBUG, "Operating frequency changed from "
  1547. "%u to %u MHz",
  1548. wpa_s->assoc_freq, data->assoc_info.freq);
  1549. wpa_supplicant_update_scan_results(wpa_s);
  1550. }
  1551. wpa_s->assoc_freq = data->assoc_info.freq;
  1552. return 0;
  1553. }
  1554. static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
  1555. {
  1556. const u8 *bss_wpa = NULL, *bss_rsn = NULL;
  1557. if (!wpa_s->current_bss || !wpa_s->current_ssid)
  1558. return -1;
  1559. if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
  1560. return 0;
  1561. bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1562. WPA_IE_VENDOR_TYPE);
  1563. bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
  1564. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  1565. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  1566. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  1567. bss_rsn ? 2 + bss_rsn[1] : 0))
  1568. return -1;
  1569. return 0;
  1570. }
  1571. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  1572. union wpa_event_data *data)
  1573. {
  1574. u8 bssid[ETH_ALEN];
  1575. int ft_completed;
  1576. #ifdef CONFIG_AP
  1577. if (wpa_s->ap_iface) {
  1578. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  1579. data->assoc_info.addr,
  1580. data->assoc_info.req_ies,
  1581. data->assoc_info.req_ies_len,
  1582. data->assoc_info.reassoc);
  1583. return;
  1584. }
  1585. #endif /* CONFIG_AP */
  1586. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  1587. if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
  1588. return;
  1589. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1590. wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
  1591. wpa_supplicant_deauthenticate(
  1592. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1593. return;
  1594. }
  1595. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  1596. if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
  1597. wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  1598. MACSTR, MAC2STR(bssid));
  1599. random_add_randomness(bssid, ETH_ALEN);
  1600. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  1601. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1602. wpas_notify_bssid_changed(wpa_s);
  1603. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  1604. wpa_clear_keys(wpa_s, bssid);
  1605. }
  1606. if (wpa_supplicant_select_config(wpa_s) < 0) {
  1607. wpa_supplicant_deauthenticate(
  1608. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1609. return;
  1610. }
  1611. if (wpa_s->conf->ap_scan == 1 &&
  1612. wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
  1613. if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
  1614. wpa_msg(wpa_s, MSG_WARNING,
  1615. "WPA/RSN IEs not updated");
  1616. }
  1617. }
  1618. #ifdef CONFIG_SME
  1619. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  1620. wpa_s->sme.prev_bssid_set = 1;
  1621. #endif /* CONFIG_SME */
  1622. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  1623. if (wpa_s->current_ssid) {
  1624. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  1625. * initialized before association, but for other modes,
  1626. * initialize PC/SC here, if the current configuration needs
  1627. * smartcard or SIM/USIM. */
  1628. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  1629. }
  1630. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  1631. if (wpa_s->l2)
  1632. l2_packet_notify_auth_start(wpa_s->l2);
  1633. /*
  1634. * Set portEnabled first to FALSE in order to get EAP state machine out
  1635. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  1636. * state machine may transit to AUTHENTICATING state based on obsolete
  1637. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  1638. * AUTHENTICATED without ever giving chance to EAP state machine to
  1639. * reset the state.
  1640. */
  1641. if (!ft_completed) {
  1642. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1643. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1644. }
  1645. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1646. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1647. /* 802.1X::portControl = Auto */
  1648. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1649. wpa_s->eapol_received = 0;
  1650. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1651. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1652. (wpa_s->current_ssid &&
  1653. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1654. if (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);
  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);
  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. #ifdef CONFIG_P2P
  2356. wpas_p2p_update_channel_list(wpa_s);
  2357. #endif /* CONFIG_P2P */
  2358. /*
  2359. * Check other interfaces to see if they share the same radio. If
  2360. * so, they get updated with this same hw mode info.
  2361. */
  2362. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  2363. radio_list) {
  2364. if (ifs != wpa_s) {
  2365. wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
  2366. ifs->ifname);
  2367. free_hw_features(ifs);
  2368. ifs->hw.modes = wpa_drv_get_hw_feature_data(
  2369. ifs, &ifs->hw.num_modes, &ifs->hw.flags);
  2370. }
  2371. }
  2372. }
  2373. static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
  2374. const struct ieee80211_mgmt *mgmt,
  2375. size_t len, int freq)
  2376. {
  2377. const u8 *payload;
  2378. size_t plen;
  2379. u8 category;
  2380. if (len < IEEE80211_HDRLEN + 2)
  2381. return;
  2382. payload = &mgmt->u.action.category;
  2383. category = *payload++;
  2384. plen = (((const u8 *) mgmt) + len) - payload;
  2385. wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
  2386. " Category=%u DataLen=%d freq=%d MHz",
  2387. MAC2STR(mgmt->sa), category, (int) plen, freq);
  2388. #ifdef CONFIG_IEEE80211R
  2389. if (category == WLAN_ACTION_FT) {
  2390. ft_rx_action(wpa_s, payload, plen);
  2391. return;
  2392. }
  2393. #endif /* CONFIG_IEEE80211R */
  2394. #ifdef CONFIG_IEEE80211W
  2395. #ifdef CONFIG_SME
  2396. if (category == WLAN_ACTION_SA_QUERY) {
  2397. sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
  2398. return;
  2399. }
  2400. #endif /* CONFIG_SME */
  2401. #endif /* CONFIG_IEEE80211W */
  2402. #ifdef CONFIG_WNM
  2403. if (mgmt->u.action.category == WLAN_ACTION_WNM) {
  2404. ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
  2405. return;
  2406. }
  2407. #endif /* CONFIG_WNM */
  2408. #ifdef CONFIG_GAS
  2409. if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
  2410. mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
  2411. gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
  2412. mgmt->u.action.category,
  2413. payload, plen, freq) == 0)
  2414. return;
  2415. #endif /* CONFIG_GAS */
  2416. #ifdef CONFIG_TDLS
  2417. if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
  2418. payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
  2419. wpa_dbg(wpa_s, MSG_DEBUG,
  2420. "TDLS: Received Discovery Response from " MACSTR,
  2421. MAC2STR(mgmt->sa));
  2422. return;
  2423. }
  2424. #endif /* CONFIG_TDLS */
  2425. #ifdef CONFIG_INTERWORKING
  2426. if (category == WLAN_ACTION_QOS && plen >= 1 &&
  2427. payload[0] == QOS_QOS_MAP_CONFIG) {
  2428. const u8 *pos = payload + 1;
  2429. size_t qlen = plen - 1;
  2430. wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
  2431. MACSTR, MAC2STR(mgmt->sa));
  2432. if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
  2433. qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
  2434. pos[1] <= qlen - 2 && pos[1] >= 16)
  2435. wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
  2436. return;
  2437. }
  2438. #endif /* CONFIG_INTERWORKING */
  2439. #ifdef CONFIG_P2P
  2440. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  2441. category, payload, plen, freq);
  2442. #endif /* CONFIG_P2P */
  2443. }
  2444. static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
  2445. union wpa_event_data *event)
  2446. {
  2447. #ifdef CONFIG_P2P
  2448. struct wpa_supplicant *ifs;
  2449. #endif /* CONFIG_P2P */
  2450. struct wpa_freq_range_list *list;
  2451. char *str = NULL;
  2452. list = &event->freq_range;
  2453. if (list->num)
  2454. str = freq_range_list_str(list);
  2455. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
  2456. str ? str : "");
  2457. #ifdef CONFIG_P2P
  2458. if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
  2459. wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
  2460. __func__);
  2461. } else {
  2462. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
  2463. wpas_p2p_update_channel_list(wpa_s);
  2464. }
  2465. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  2466. int freq;
  2467. if (!ifs->current_ssid ||
  2468. !ifs->current_ssid->p2p_group ||
  2469. (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
  2470. ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
  2471. continue;
  2472. freq = ifs->current_ssid->frequency;
  2473. if (!freq_range_list_includes(list, freq)) {
  2474. wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
  2475. freq);
  2476. continue;
  2477. }
  2478. wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
  2479. freq);
  2480. /* TODO: Consider using CSA or removing the group within
  2481. * wpa_supplicant */
  2482. wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  2483. }
  2484. #endif /* CONFIG_P2P */
  2485. os_free(str);
  2486. }
  2487. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  2488. union wpa_event_data *data)
  2489. {
  2490. struct wpa_supplicant *wpa_s = ctx;
  2491. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
  2492. event != EVENT_INTERFACE_ENABLED &&
  2493. event != EVENT_INTERFACE_STATUS &&
  2494. event != EVENT_SCHED_SCAN_STOPPED) {
  2495. wpa_dbg(wpa_s, MSG_DEBUG,
  2496. "Ignore event %s (%d) while interface is disabled",
  2497. event_to_string(event), event);
  2498. return;
  2499. }
  2500. #ifndef CONFIG_NO_STDOUT_DEBUG
  2501. {
  2502. int level = MSG_DEBUG;
  2503. if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
  2504. const struct ieee80211_hdr *hdr;
  2505. u16 fc;
  2506. hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
  2507. fc = le_to_host16(hdr->frame_control);
  2508. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2509. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  2510. level = MSG_EXCESSIVE;
  2511. }
  2512. wpa_dbg(wpa_s, level, "Event %s (%d) received",
  2513. event_to_string(event), event);
  2514. }
  2515. #endif /* CONFIG_NO_STDOUT_DEBUG */
  2516. switch (event) {
  2517. case EVENT_AUTH:
  2518. sme_event_auth(wpa_s, data);
  2519. break;
  2520. case EVENT_ASSOC:
  2521. wpa_supplicant_event_assoc(wpa_s, data);
  2522. break;
  2523. case EVENT_DISASSOC:
  2524. wpas_event_disassoc(wpa_s,
  2525. data ? &data->disassoc_info : NULL);
  2526. break;
  2527. case EVENT_DEAUTH:
  2528. wpas_event_deauth(wpa_s,
  2529. data ? &data->deauth_info : NULL);
  2530. break;
  2531. case EVENT_MICHAEL_MIC_FAILURE:
  2532. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  2533. break;
  2534. #ifndef CONFIG_NO_SCAN_PROCESSING
  2535. case EVENT_SCAN_STARTED:
  2536. os_get_reltime(&wpa_s->scan_start_time);
  2537. if (wpa_s->own_scan_requested) {
  2538. struct os_reltime diff;
  2539. os_reltime_sub(&wpa_s->scan_start_time,
  2540. &wpa_s->scan_trigger_time, &diff);
  2541. wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
  2542. diff.sec, diff.usec);
  2543. wpa_s->own_scan_requested = 0;
  2544. wpa_s->own_scan_running = 1;
  2545. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  2546. wpa_s->manual_scan_use_id) {
  2547. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED "id=%u",
  2548. wpa_s->manual_scan_id);
  2549. } else {
  2550. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
  2551. }
  2552. } else {
  2553. wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
  2554. wpa_s->external_scan_running = 1;
  2555. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
  2556. }
  2557. break;
  2558. case EVENT_SCAN_RESULTS:
  2559. if (os_reltime_initialized(&wpa_s->scan_start_time)) {
  2560. struct os_reltime now, diff;
  2561. os_get_reltime(&now);
  2562. os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
  2563. wpa_s->scan_start_time.sec = 0;
  2564. wpa_s->scan_start_time.usec = 0;
  2565. wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
  2566. diff.sec, diff.usec);
  2567. }
  2568. wpa_supplicant_event_scan_results(wpa_s, data);
  2569. wpa_s->own_scan_running = 0;
  2570. wpa_s->external_scan_running = 0;
  2571. radio_work_check_next(wpa_s);
  2572. break;
  2573. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2574. case EVENT_ASSOCINFO:
  2575. wpa_supplicant_event_associnfo(wpa_s, data);
  2576. break;
  2577. case EVENT_INTERFACE_STATUS:
  2578. wpa_supplicant_event_interface_status(wpa_s, data);
  2579. break;
  2580. case EVENT_PMKID_CANDIDATE:
  2581. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  2582. break;
  2583. #ifdef CONFIG_PEERKEY
  2584. case EVENT_STKSTART:
  2585. wpa_supplicant_event_stkstart(wpa_s, data);
  2586. break;
  2587. #endif /* CONFIG_PEERKEY */
  2588. #ifdef CONFIG_TDLS
  2589. case EVENT_TDLS:
  2590. wpa_supplicant_event_tdls(wpa_s, data);
  2591. break;
  2592. #endif /* CONFIG_TDLS */
  2593. #ifdef CONFIG_WNM
  2594. case EVENT_WNM:
  2595. wpa_supplicant_event_wnm(wpa_s, data);
  2596. break;
  2597. #endif /* CONFIG_WNM */
  2598. #ifdef CONFIG_IEEE80211R
  2599. case EVENT_FT_RESPONSE:
  2600. wpa_supplicant_event_ft_response(wpa_s, data);
  2601. break;
  2602. #endif /* CONFIG_IEEE80211R */
  2603. #ifdef CONFIG_IBSS_RSN
  2604. case EVENT_IBSS_RSN_START:
  2605. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  2606. break;
  2607. #endif /* CONFIG_IBSS_RSN */
  2608. case EVENT_ASSOC_REJECT:
  2609. if (data->assoc_reject.bssid)
  2610. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  2611. "bssid=" MACSTR " status_code=%u",
  2612. MAC2STR(data->assoc_reject.bssid),
  2613. data->assoc_reject.status_code);
  2614. else
  2615. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
  2616. "status_code=%u",
  2617. data->assoc_reject.status_code);
  2618. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2619. sme_event_assoc_reject(wpa_s, data);
  2620. else {
  2621. const u8 *bssid = data->assoc_reject.bssid;
  2622. if (bssid == NULL || is_zero_ether_addr(bssid))
  2623. bssid = wpa_s->pending_bssid;
  2624. wpas_connection_failed(wpa_s, bssid);
  2625. wpa_supplicant_mark_disassoc(wpa_s);
  2626. }
  2627. break;
  2628. case EVENT_AUTH_TIMED_OUT:
  2629. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2630. sme_event_auth_timed_out(wpa_s, data);
  2631. break;
  2632. case EVENT_ASSOC_TIMED_OUT:
  2633. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  2634. sme_event_assoc_timed_out(wpa_s, data);
  2635. break;
  2636. case EVENT_TX_STATUS:
  2637. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
  2638. " type=%d stype=%d",
  2639. MAC2STR(data->tx_status.dst),
  2640. data->tx_status.type, data->tx_status.stype);
  2641. #ifdef CONFIG_AP
  2642. if (wpa_s->ap_iface == NULL) {
  2643. #ifdef CONFIG_OFFCHANNEL
  2644. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  2645. data->tx_status.stype == WLAN_FC_STYPE_ACTION)
  2646. offchannel_send_action_tx_status(
  2647. wpa_s, data->tx_status.dst,
  2648. data->tx_status.data,
  2649. data->tx_status.data_len,
  2650. data->tx_status.ack ?
  2651. OFFCHANNEL_SEND_ACTION_SUCCESS :
  2652. OFFCHANNEL_SEND_ACTION_NO_ACK);
  2653. #endif /* CONFIG_OFFCHANNEL */
  2654. break;
  2655. }
  2656. #endif /* CONFIG_AP */
  2657. #ifdef CONFIG_OFFCHANNEL
  2658. wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
  2659. MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
  2660. /*
  2661. * Catch TX status events for Action frames we sent via group
  2662. * interface in GO mode.
  2663. */
  2664. if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
  2665. data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
  2666. os_memcmp(wpa_s->parent->pending_action_dst,
  2667. data->tx_status.dst, ETH_ALEN) == 0) {
  2668. offchannel_send_action_tx_status(
  2669. wpa_s->parent, data->tx_status.dst,
  2670. data->tx_status.data,
  2671. data->tx_status.data_len,
  2672. data->tx_status.ack ?
  2673. OFFCHANNEL_SEND_ACTION_SUCCESS :
  2674. OFFCHANNEL_SEND_ACTION_NO_ACK);
  2675. break;
  2676. }
  2677. #endif /* CONFIG_OFFCHANNEL */
  2678. #ifdef CONFIG_AP
  2679. switch (data->tx_status.type) {
  2680. case WLAN_FC_TYPE_MGMT:
  2681. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  2682. data->tx_status.data_len,
  2683. data->tx_status.stype,
  2684. data->tx_status.ack);
  2685. break;
  2686. case WLAN_FC_TYPE_DATA:
  2687. ap_tx_status(wpa_s, data->tx_status.dst,
  2688. data->tx_status.data,
  2689. data->tx_status.data_len,
  2690. data->tx_status.ack);
  2691. break;
  2692. }
  2693. #endif /* CONFIG_AP */
  2694. break;
  2695. #ifdef CONFIG_AP
  2696. case EVENT_EAPOL_TX_STATUS:
  2697. ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
  2698. data->eapol_tx_status.data,
  2699. data->eapol_tx_status.data_len,
  2700. data->eapol_tx_status.ack);
  2701. break;
  2702. case EVENT_DRIVER_CLIENT_POLL_OK:
  2703. ap_client_poll_ok(wpa_s, data->client_poll.addr);
  2704. break;
  2705. case EVENT_RX_FROM_UNKNOWN:
  2706. if (wpa_s->ap_iface == NULL)
  2707. break;
  2708. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
  2709. data->rx_from_unknown.wds);
  2710. break;
  2711. case EVENT_CH_SWITCH:
  2712. if (!data)
  2713. break;
  2714. if (!wpa_s->ap_iface) {
  2715. wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
  2716. "event in non-AP mode");
  2717. break;
  2718. }
  2719. wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
  2720. data->ch_switch.ht_enabled,
  2721. data->ch_switch.ch_offset,
  2722. data->ch_switch.ch_width,
  2723. data->ch_switch.cf1,
  2724. data->ch_switch.cf2);
  2725. break;
  2726. #endif /* CONFIG_AP */
  2727. case EVENT_RX_MGMT: {
  2728. u16 fc, stype;
  2729. const struct ieee80211_mgmt *mgmt;
  2730. #ifdef CONFIG_TESTING_OPTIONS
  2731. if (wpa_s->ext_mgmt_frame_handling) {
  2732. struct rx_mgmt *rx = &data->rx_mgmt;
  2733. size_t hex_len = 2 * rx->frame_len + 1;
  2734. char *hex = os_malloc(hex_len);
  2735. if (hex) {
  2736. wpa_snprintf_hex(hex, hex_len,
  2737. rx->frame, rx->frame_len);
  2738. wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
  2739. rx->freq, rx->datarate, rx->ssi_signal,
  2740. hex);
  2741. os_free(hex);
  2742. }
  2743. break;
  2744. }
  2745. #endif /* CONFIG_TESTING_OPTIONS */
  2746. mgmt = (const struct ieee80211_mgmt *)
  2747. data->rx_mgmt.frame;
  2748. fc = le_to_host16(mgmt->frame_control);
  2749. stype = WLAN_FC_GET_STYPE(fc);
  2750. #ifdef CONFIG_AP
  2751. if (wpa_s->ap_iface == NULL) {
  2752. #endif /* CONFIG_AP */
  2753. #ifdef CONFIG_P2P
  2754. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  2755. data->rx_mgmt.frame_len > 24) {
  2756. const u8 *src = mgmt->sa;
  2757. const u8 *ie = mgmt->u.probe_req.variable;
  2758. size_t ie_len = data->rx_mgmt.frame_len -
  2759. (mgmt->u.probe_req.variable -
  2760. data->rx_mgmt.frame);
  2761. wpas_p2p_probe_req_rx(
  2762. wpa_s, src, mgmt->da,
  2763. mgmt->bssid, ie, ie_len,
  2764. data->rx_mgmt.ssi_signal);
  2765. break;
  2766. }
  2767. #endif /* CONFIG_P2P */
  2768. #ifdef CONFIG_IBSS_RSN
  2769. if (stype == WLAN_FC_STYPE_AUTH &&
  2770. data->rx_mgmt.frame_len >= 30) {
  2771. wpa_supplicant_event_ibss_auth(wpa_s, data);
  2772. break;
  2773. }
  2774. #endif /* CONFIG_IBSS_RSN */
  2775. if (stype == WLAN_FC_STYPE_ACTION) {
  2776. wpas_event_rx_mgmt_action(
  2777. wpa_s, mgmt, data->rx_mgmt.frame_len,
  2778. data->rx_mgmt.freq);
  2779. break;
  2780. }
  2781. wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
  2782. "management frame in non-AP mode");
  2783. break;
  2784. #ifdef CONFIG_AP
  2785. }
  2786. if (stype == WLAN_FC_STYPE_PROBE_REQ &&
  2787. data->rx_mgmt.frame_len > 24) {
  2788. const u8 *ie = mgmt->u.probe_req.variable;
  2789. size_t ie_len = data->rx_mgmt.frame_len -
  2790. (mgmt->u.probe_req.variable -
  2791. data->rx_mgmt.frame);
  2792. wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
  2793. mgmt->bssid, ie, ie_len,
  2794. data->rx_mgmt.ssi_signal);
  2795. }
  2796. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  2797. #endif /* CONFIG_AP */
  2798. break;
  2799. }
  2800. case EVENT_RX_PROBE_REQ:
  2801. if (data->rx_probe_req.sa == NULL ||
  2802. data->rx_probe_req.ie == NULL)
  2803. break;
  2804. #ifdef CONFIG_AP
  2805. if (wpa_s->ap_iface) {
  2806. hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
  2807. data->rx_probe_req.sa,
  2808. data->rx_probe_req.da,
  2809. data->rx_probe_req.bssid,
  2810. data->rx_probe_req.ie,
  2811. data->rx_probe_req.ie_len,
  2812. data->rx_probe_req.ssi_signal);
  2813. break;
  2814. }
  2815. #endif /* CONFIG_AP */
  2816. #ifdef CONFIG_P2P
  2817. wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
  2818. data->rx_probe_req.da,
  2819. data->rx_probe_req.bssid,
  2820. data->rx_probe_req.ie,
  2821. data->rx_probe_req.ie_len,
  2822. data->rx_probe_req.ssi_signal);
  2823. #endif /* CONFIG_P2P */
  2824. break;
  2825. case EVENT_REMAIN_ON_CHANNEL:
  2826. #ifdef CONFIG_OFFCHANNEL
  2827. offchannel_remain_on_channel_cb(
  2828. wpa_s, data->remain_on_channel.freq,
  2829. data->remain_on_channel.duration);
  2830. #endif /* CONFIG_OFFCHANNEL */
  2831. #ifdef CONFIG_P2P
  2832. wpas_p2p_remain_on_channel_cb(
  2833. wpa_s, data->remain_on_channel.freq,
  2834. data->remain_on_channel.duration);
  2835. #endif /* CONFIG_P2P */
  2836. break;
  2837. case EVENT_CANCEL_REMAIN_ON_CHANNEL:
  2838. #ifdef CONFIG_OFFCHANNEL
  2839. offchannel_cancel_remain_on_channel_cb(
  2840. wpa_s, data->remain_on_channel.freq);
  2841. #endif /* CONFIG_OFFCHANNEL */
  2842. #ifdef CONFIG_P2P
  2843. wpas_p2p_cancel_remain_on_channel_cb(
  2844. wpa_s, data->remain_on_channel.freq);
  2845. #endif /* CONFIG_P2P */
  2846. break;
  2847. case EVENT_EAPOL_RX:
  2848. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  2849. data->eapol_rx.data,
  2850. data->eapol_rx.data_len);
  2851. break;
  2852. case EVENT_SIGNAL_CHANGE:
  2853. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
  2854. "above=%d signal=%d noise=%d txrate=%d",
  2855. data->signal_change.above_threshold,
  2856. data->signal_change.current_signal,
  2857. data->signal_change.current_noise,
  2858. data->signal_change.current_txrate);
  2859. bgscan_notify_signal_change(
  2860. wpa_s, data->signal_change.above_threshold,
  2861. data->signal_change.current_signal,
  2862. data->signal_change.current_noise,
  2863. data->signal_change.current_txrate);
  2864. break;
  2865. case EVENT_INTERFACE_ENABLED:
  2866. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
  2867. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  2868. wpa_supplicant_update_mac_addr(wpa_s);
  2869. if (wpa_s->p2p_mgmt) {
  2870. wpa_supplicant_set_state(wpa_s,
  2871. WPA_DISCONNECTED);
  2872. break;
  2873. }
  2874. #ifdef CONFIG_AP
  2875. if (!wpa_s->ap_iface) {
  2876. wpa_supplicant_set_state(wpa_s,
  2877. WPA_DISCONNECTED);
  2878. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2879. } else
  2880. wpa_supplicant_set_state(wpa_s,
  2881. WPA_COMPLETED);
  2882. #else /* CONFIG_AP */
  2883. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2884. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2885. #endif /* CONFIG_AP */
  2886. }
  2887. break;
  2888. case EVENT_INTERFACE_DISABLED:
  2889. wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
  2890. #ifdef CONFIG_P2P
  2891. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  2892. (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
  2893. wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
  2894. /*
  2895. * The interface was externally disabled. Remove
  2896. * it assuming an external entity will start a
  2897. * new session if needed.
  2898. */
  2899. wpas_p2p_disconnect(wpa_s);
  2900. break;
  2901. }
  2902. #endif /* CONFIG_P2P */
  2903. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2904. /*
  2905. * Indicate disconnection to keep ctrl_iface events
  2906. * consistent.
  2907. */
  2908. wpa_supplicant_event_disassoc(
  2909. wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
  2910. }
  2911. wpa_supplicant_mark_disassoc(wpa_s);
  2912. radio_remove_works(wpa_s, NULL, 0);
  2913. wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
  2914. break;
  2915. case EVENT_CHANNEL_LIST_CHANGED:
  2916. wpa_supplicant_update_channel_list(
  2917. wpa_s, &data->channel_list_changed);
  2918. break;
  2919. case EVENT_INTERFACE_UNAVAILABLE:
  2920. #ifdef CONFIG_P2P
  2921. wpas_p2p_interface_unavailable(wpa_s);
  2922. #endif /* CONFIG_P2P */
  2923. break;
  2924. case EVENT_BEST_CHANNEL:
  2925. wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
  2926. "(%d %d %d)",
  2927. data->best_chan.freq_24, data->best_chan.freq_5,
  2928. data->best_chan.freq_overall);
  2929. wpa_s->best_24_freq = data->best_chan.freq_24;
  2930. wpa_s->best_5_freq = data->best_chan.freq_5;
  2931. wpa_s->best_overall_freq = data->best_chan.freq_overall;
  2932. #ifdef CONFIG_P2P
  2933. wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
  2934. data->best_chan.freq_5,
  2935. data->best_chan.freq_overall);
  2936. #endif /* CONFIG_P2P */
  2937. break;
  2938. case EVENT_UNPROT_DEAUTH:
  2939. wpa_supplicant_event_unprot_deauth(wpa_s,
  2940. &data->unprot_deauth);
  2941. break;
  2942. case EVENT_UNPROT_DISASSOC:
  2943. wpa_supplicant_event_unprot_disassoc(wpa_s,
  2944. &data->unprot_disassoc);
  2945. break;
  2946. case EVENT_STATION_LOW_ACK:
  2947. #ifdef CONFIG_AP
  2948. if (wpa_s->ap_iface && data)
  2949. hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
  2950. data->low_ack.addr);
  2951. #endif /* CONFIG_AP */
  2952. #ifdef CONFIG_TDLS
  2953. if (data)
  2954. wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
  2955. #endif /* CONFIG_TDLS */
  2956. break;
  2957. case EVENT_IBSS_PEER_LOST:
  2958. #ifdef CONFIG_IBSS_RSN
  2959. ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
  2960. #endif /* CONFIG_IBSS_RSN */
  2961. break;
  2962. case EVENT_DRIVER_GTK_REKEY:
  2963. if (os_memcmp(data->driver_gtk_rekey.bssid,
  2964. wpa_s->bssid, ETH_ALEN))
  2965. break;
  2966. if (!wpa_s->wpa)
  2967. break;
  2968. wpa_sm_update_replay_ctr(wpa_s->wpa,
  2969. data->driver_gtk_rekey.replay_ctr);
  2970. break;
  2971. case EVENT_SCHED_SCAN_STOPPED:
  2972. wpa_s->pno = 0;
  2973. wpa_s->sched_scanning = 0;
  2974. wpa_supplicant_notify_scanning(wpa_s, 0);
  2975. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2976. break;
  2977. /*
  2978. * Start a new sched scan to continue searching for more SSIDs
  2979. * either if timed out or PNO schedule scan is pending.
  2980. */
  2981. if (wpa_s->sched_scan_timed_out) {
  2982. wpa_supplicant_req_sched_scan(wpa_s);
  2983. } else if (wpa_s->pno_sched_pending) {
  2984. wpa_s->pno_sched_pending = 0;
  2985. wpas_start_pno(wpa_s);
  2986. }
  2987. break;
  2988. case EVENT_WPS_BUTTON_PUSHED:
  2989. #ifdef CONFIG_WPS
  2990. wpas_wps_start_pbc(wpa_s, NULL, 0);
  2991. #endif /* CONFIG_WPS */
  2992. break;
  2993. case EVENT_AVOID_FREQUENCIES:
  2994. wpa_supplicant_notify_avoid_freq(wpa_s, data);
  2995. break;
  2996. case EVENT_CONNECT_FAILED_REASON:
  2997. #ifdef CONFIG_AP
  2998. if (!wpa_s->ap_iface || !data)
  2999. break;
  3000. hostapd_event_connect_failed_reason(
  3001. wpa_s->ap_iface->bss[0],
  3002. data->connect_failed_reason.addr,
  3003. data->connect_failed_reason.code);
  3004. #endif /* CONFIG_AP */
  3005. break;
  3006. default:
  3007. wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
  3008. break;
  3009. }
  3010. }