events.c 80 KB

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