events.c 66 KB

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