events.c 91 KB

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