events.c 92 KB

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