events.c 71 KB

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