events.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2010, 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 "notify.h"
  30. #include "common/ieee802_11_defs.h"
  31. #include "blacklist.h"
  32. #include "wpas_glue.h"
  33. #include "wps_supplicant.h"
  34. #include "ibss_rsn.h"
  35. #include "sme.h"
  36. #include "bgscan.h"
  37. #include "ap.h"
  38. #include "bss.h"
  39. #include "mlme.h"
  40. #include "scan.h"
  41. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  42. {
  43. struct wpa_ssid *ssid, *old_ssid;
  44. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  45. return 0;
  46. wpa_printf(MSG_DEBUG, "Select network based on association "
  47. "information");
  48. ssid = wpa_supplicant_get_ssid(wpa_s);
  49. if (ssid == NULL) {
  50. wpa_printf(MSG_INFO, "No network configuration found for the "
  51. "current AP");
  52. return -1;
  53. }
  54. if (ssid->disabled) {
  55. wpa_printf(MSG_DEBUG, "Selected network is disabled");
  56. return -1;
  57. }
  58. wpa_printf(MSG_DEBUG, "Network configuration found for the current "
  59. "AP");
  60. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  61. WPA_KEY_MGMT_WPA_NONE |
  62. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
  63. WPA_KEY_MGMT_PSK_SHA256 |
  64. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  65. u8 wpa_ie[80];
  66. size_t wpa_ie_len = sizeof(wpa_ie);
  67. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  68. wpa_ie, &wpa_ie_len);
  69. } else {
  70. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  71. }
  72. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  73. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  74. old_ssid = wpa_s->current_ssid;
  75. wpa_s->current_ssid = ssid;
  76. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  77. wpa_supplicant_initiate_eapol(wpa_s);
  78. if (old_ssid != wpa_s->current_ssid)
  79. wpas_notify_network_changed(wpa_s);
  80. return 0;
  81. }
  82. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  83. 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. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  97. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  98. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  99. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  100. wpa_s->current_bss = NULL;
  101. if (bssid_changed)
  102. wpas_notify_bssid_changed(wpa_s);
  103. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  104. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  105. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  106. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  107. wpa_s->ap_ies_from_associnfo = 0;
  108. }
  109. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  110. {
  111. struct wpa_ie_data ie;
  112. int pmksa_set = -1;
  113. size_t i;
  114. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  115. ie.pmkid == NULL)
  116. return;
  117. for (i = 0; i < ie.num_pmkid; i++) {
  118. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  119. ie.pmkid + i * PMKID_LEN,
  120. NULL, NULL, 0);
  121. if (pmksa_set == 0) {
  122. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  123. break;
  124. }
  125. }
  126. wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
  127. "cache", pmksa_set == 0 ? "" : "not ");
  128. }
  129. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  130. union wpa_event_data *data)
  131. {
  132. if (data == NULL) {
  133. wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
  134. return;
  135. }
  136. wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  137. " index=%d preauth=%d",
  138. MAC2STR(data->pmkid_candidate.bssid),
  139. data->pmkid_candidate.index,
  140. data->pmkid_candidate.preauth);
  141. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  142. data->pmkid_candidate.index,
  143. data->pmkid_candidate.preauth);
  144. }
  145. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  146. {
  147. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  148. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  149. return 0;
  150. #ifdef IEEE8021X_EAPOL
  151. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  152. wpa_s->current_ssid &&
  153. !(wpa_s->current_ssid->eapol_flags &
  154. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  155. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  156. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  157. * plaintext or static WEP keys). */
  158. return 0;
  159. }
  160. #endif /* IEEE8021X_EAPOL */
  161. return 1;
  162. }
  163. /**
  164. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  165. * @wpa_s: pointer to wpa_supplicant data
  166. * @ssid: Configuration data for the network
  167. * Returns: 0 on success, -1 on failure
  168. *
  169. * This function is called when starting authentication with a network that is
  170. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  171. */
  172. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  173. struct wpa_ssid *ssid)
  174. {
  175. #ifdef IEEE8021X_EAPOL
  176. int aka = 0, sim = 0, type;
  177. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  178. return 0;
  179. if (ssid->eap.eap_methods == NULL) {
  180. sim = 1;
  181. aka = 1;
  182. } else {
  183. struct eap_method_type *eap = ssid->eap.eap_methods;
  184. while (eap->vendor != EAP_VENDOR_IETF ||
  185. eap->method != EAP_TYPE_NONE) {
  186. if (eap->vendor == EAP_VENDOR_IETF) {
  187. if (eap->method == EAP_TYPE_SIM)
  188. sim = 1;
  189. else if (eap->method == EAP_TYPE_AKA)
  190. aka = 1;
  191. }
  192. eap++;
  193. }
  194. }
  195. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  196. sim = 0;
  197. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  198. aka = 0;
  199. if (!sim && !aka) {
  200. wpa_printf(MSG_DEBUG, "Selected network is configured to use "
  201. "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
  202. return 0;
  203. }
  204. wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
  205. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  206. if (sim && aka)
  207. type = SCARD_TRY_BOTH;
  208. else if (aka)
  209. type = SCARD_USIM_ONLY;
  210. else
  211. type = SCARD_GSM_SIM_ONLY;
  212. wpa_s->scard = scard_init(type);
  213. if (wpa_s->scard == NULL) {
  214. wpa_printf(MSG_WARNING, "Failed to initialize SIM "
  215. "(pcsc-lite)");
  216. return -1;
  217. }
  218. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  219. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  220. #endif /* IEEE8021X_EAPOL */
  221. return 0;
  222. }
  223. #ifndef CONFIG_NO_SCAN_PROCESSING
  224. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  225. struct wpa_ssid *ssid)
  226. {
  227. int i, privacy = 0;
  228. if (ssid->mixed_cell)
  229. return 1;
  230. #ifdef CONFIG_WPS
  231. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  232. return 1;
  233. #endif /* CONFIG_WPS */
  234. for (i = 0; i < NUM_WEP_KEYS; i++) {
  235. if (ssid->wep_key_len[i]) {
  236. privacy = 1;
  237. break;
  238. }
  239. }
  240. #ifdef IEEE8021X_EAPOL
  241. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  242. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  243. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  244. privacy = 1;
  245. #endif /* IEEE8021X_EAPOL */
  246. if (bss->caps & IEEE80211_CAP_PRIVACY)
  247. return privacy;
  248. return !privacy;
  249. }
  250. static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
  251. struct wpa_ssid *ssid,
  252. struct wpa_scan_res *bss)
  253. {
  254. struct wpa_ie_data ie;
  255. int proto_match = 0;
  256. const u8 *rsn_ie, *wpa_ie;
  257. int ret;
  258. ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
  259. if (ret >= 0)
  260. return ret;
  261. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  262. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  263. proto_match++;
  264. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  265. wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
  266. break;
  267. }
  268. if (!(ie.proto & ssid->proto)) {
  269. wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
  270. "mismatch");
  271. break;
  272. }
  273. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  274. wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
  275. "mismatch");
  276. break;
  277. }
  278. if (!(ie.group_cipher & ssid->group_cipher)) {
  279. wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
  280. "mismatch");
  281. break;
  282. }
  283. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  284. wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
  285. "mismatch");
  286. break;
  287. }
  288. #ifdef CONFIG_IEEE80211W
  289. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  290. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  291. wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
  292. "protection");
  293. break;
  294. }
  295. #endif /* CONFIG_IEEE80211W */
  296. wpa_printf(MSG_DEBUG, " selected based on RSN IE");
  297. return 1;
  298. }
  299. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  300. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  301. proto_match++;
  302. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  303. wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
  304. break;
  305. }
  306. if (!(ie.proto & ssid->proto)) {
  307. wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
  308. "mismatch");
  309. break;
  310. }
  311. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  312. wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
  313. "mismatch");
  314. break;
  315. }
  316. if (!(ie.group_cipher & ssid->group_cipher)) {
  317. wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
  318. "mismatch");
  319. break;
  320. }
  321. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  322. wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
  323. "mismatch");
  324. break;
  325. }
  326. wpa_printf(MSG_DEBUG, " selected based on WPA IE");
  327. return 1;
  328. }
  329. if (proto_match == 0)
  330. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  331. return 0;
  332. }
  333. static struct wpa_bss *
  334. wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
  335. struct wpa_scan_results *scan_res,
  336. struct wpa_ssid *group,
  337. struct wpa_ssid **selected_ssid)
  338. {
  339. struct wpa_ssid *ssid;
  340. struct wpa_scan_res *bss;
  341. size_t i;
  342. struct wpa_blacklist *e;
  343. const u8 *ie;
  344. wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
  345. for (i = 0; i < scan_res->num; i++) {
  346. const u8 *ssid_;
  347. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  348. bss = scan_res->res[i];
  349. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  350. ssid_ = ie ? ie + 2 : (u8 *) "";
  351. ssid_len = ie ? ie[1] : 0;
  352. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  353. wpa_ie_len = ie ? ie[1] : 0;
  354. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  355. rsn_ie_len = ie ? ie[1] : 0;
  356. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  357. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  358. (int) i, MAC2STR(bss->bssid),
  359. wpa_ssid_txt(ssid_, ssid_len),
  360. wpa_ie_len, rsn_ie_len, bss->caps);
  361. e = wpa_blacklist_get(wpa_s, bss->bssid);
  362. if (e && e->count > 1) {
  363. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  364. continue;
  365. }
  366. if (ssid_len == 0) {
  367. wpa_printf(MSG_DEBUG, " skip - SSID not known");
  368. continue;
  369. }
  370. if (wpa_ie_len == 0 && rsn_ie_len == 0) {
  371. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
  372. continue;
  373. }
  374. for (ssid = group; ssid; ssid = ssid->pnext) {
  375. int check_ssid = 1;
  376. if (ssid->disabled) {
  377. wpa_printf(MSG_DEBUG, " skip - disabled");
  378. continue;
  379. }
  380. #ifdef CONFIG_WPS
  381. if (ssid->ssid_len == 0 &&
  382. wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
  383. check_ssid = 0;
  384. #endif /* CONFIG_WPS */
  385. if (check_ssid &&
  386. (ssid_len != ssid->ssid_len ||
  387. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  388. wpa_printf(MSG_DEBUG, " skip - "
  389. "SSID mismatch");
  390. continue;
  391. }
  392. if (ssid->bssid_set &&
  393. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  394. {
  395. wpa_printf(MSG_DEBUG, " skip - "
  396. "BSSID mismatch");
  397. continue;
  398. }
  399. if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
  400. continue;
  401. wpa_printf(MSG_DEBUG, " selected WPA AP "
  402. MACSTR " ssid='%s'",
  403. MAC2STR(bss->bssid),
  404. wpa_ssid_txt(ssid_, ssid_len));
  405. *selected_ssid = ssid;
  406. return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
  407. }
  408. }
  409. return NULL;
  410. }
  411. static struct wpa_bss *
  412. wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
  413. struct wpa_scan_results *scan_res,
  414. struct wpa_ssid *group,
  415. struct wpa_ssid **selected_ssid)
  416. {
  417. struct wpa_ssid *ssid;
  418. struct wpa_scan_res *bss;
  419. size_t i;
  420. struct wpa_blacklist *e;
  421. const u8 *ie;
  422. wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
  423. for (i = 0; i < scan_res->num; i++) {
  424. const u8 *ssid_;
  425. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  426. bss = scan_res->res[i];
  427. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  428. ssid_ = ie ? ie + 2 : (u8 *) "";
  429. ssid_len = ie ? ie[1] : 0;
  430. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  431. wpa_ie_len = ie ? ie[1] : 0;
  432. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  433. rsn_ie_len = ie ? ie[1] : 0;
  434. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  435. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  436. (int) i, MAC2STR(bss->bssid),
  437. wpa_ssid_txt(ssid_, ssid_len),
  438. wpa_ie_len, rsn_ie_len, bss->caps);
  439. e = wpa_blacklist_get(wpa_s, bss->bssid);
  440. if (e && e->count > 1) {
  441. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  442. continue;
  443. }
  444. if (ssid_len == 0) {
  445. wpa_printf(MSG_DEBUG, " skip - SSID not known");
  446. continue;
  447. }
  448. for (ssid = group; ssid; ssid = ssid->pnext) {
  449. int check_ssid = ssid->ssid_len != 0;
  450. if (ssid->disabled) {
  451. wpa_printf(MSG_DEBUG, " skip - disabled");
  452. continue;
  453. }
  454. #ifdef CONFIG_WPS
  455. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  456. /* Only allow wildcard SSID match if an AP
  457. * advertises active WPS operation that matches
  458. * with our mode. */
  459. check_ssid = 1;
  460. if (ssid->ssid_len == 0 &&
  461. wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
  462. bss))
  463. check_ssid = 0;
  464. }
  465. #endif /* CONFIG_WPS */
  466. if (check_ssid &&
  467. (ssid_len != ssid->ssid_len ||
  468. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  469. wpa_printf(MSG_DEBUG, " skip - "
  470. "SSID mismatch");
  471. continue;
  472. }
  473. if (ssid->bssid_set &&
  474. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  475. {
  476. wpa_printf(MSG_DEBUG, " skip - "
  477. "BSSID mismatch");
  478. continue;
  479. }
  480. if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  481. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  482. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
  483. {
  484. wpa_printf(MSG_DEBUG, " skip - "
  485. "non-WPA network not allowed");
  486. continue;
  487. }
  488. if ((ssid->key_mgmt &
  489. (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  490. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
  491. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  492. WPA_KEY_MGMT_PSK_SHA256)) &&
  493. (wpa_ie_len != 0 || rsn_ie_len != 0)) {
  494. wpa_printf(MSG_DEBUG, " skip - "
  495. "WPA network");
  496. continue;
  497. }
  498. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  499. wpa_printf(MSG_DEBUG, " skip - "
  500. "privacy mismatch");
  501. continue;
  502. }
  503. if (bss->caps & IEEE80211_CAP_IBSS) {
  504. wpa_printf(MSG_DEBUG, " skip - "
  505. "IBSS (adhoc) network");
  506. continue;
  507. }
  508. wpa_printf(MSG_DEBUG, " selected non-WPA AP "
  509. MACSTR " ssid='%s'",
  510. MAC2STR(bss->bssid),
  511. wpa_ssid_txt(ssid_, ssid_len));
  512. *selected_ssid = ssid;
  513. return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
  514. }
  515. }
  516. return NULL;
  517. }
  518. static struct wpa_bss *
  519. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
  520. struct wpa_scan_results *scan_res,
  521. struct wpa_ssid *group,
  522. struct wpa_ssid **selected_ssid)
  523. {
  524. struct wpa_bss *selected;
  525. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  526. group->priority);
  527. /* First, try to find WPA-enabled AP */
  528. selected = wpa_supplicant_select_bss_wpa(wpa_s, scan_res, group,
  529. selected_ssid);
  530. if (selected)
  531. return selected;
  532. /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
  533. * allows this. */
  534. return wpa_supplicant_select_bss_non_wpa(wpa_s, scan_res, group,
  535. selected_ssid);
  536. }
  537. static struct wpa_bss *
  538. wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
  539. struct wpa_scan_results *scan_res,
  540. struct wpa_ssid **selected_ssid)
  541. {
  542. struct wpa_bss *selected = NULL;
  543. int prio;
  544. while (selected == NULL) {
  545. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  546. selected = wpa_supplicant_select_bss(
  547. wpa_s, scan_res, wpa_s->conf->pssid[prio],
  548. selected_ssid);
  549. if (selected)
  550. break;
  551. }
  552. if (selected == NULL && wpa_s->blacklist) {
  553. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  554. "and try again");
  555. wpa_blacklist_clear(wpa_s);
  556. wpa_s->blacklist_cleared++;
  557. } else if (selected == NULL)
  558. break;
  559. }
  560. return selected;
  561. }
  562. static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
  563. int timeout)
  564. {
  565. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  566. /*
  567. * Quick recovery if the initial scan results were not
  568. * complete when fetched before the first scan request.
  569. */
  570. wpa_s->scan_res_tried++;
  571. timeout = 0;
  572. } else if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
  573. /*
  574. * No networks are enabled; short-circuit request so
  575. * we don't wait timeout seconds before transitioning
  576. * to INACTIVE state.
  577. */
  578. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  579. return;
  580. }
  581. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  582. }
  583. static void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
  584. struct wpa_bss *selected,
  585. struct wpa_ssid *ssid)
  586. {
  587. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  588. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  589. "PBC session overlap");
  590. wpa_supplicant_req_new_scan(wpa_s, 10);
  591. return;
  592. }
  593. /*
  594. * Do not trigger new association unless the BSSID has changed or if
  595. * reassociation is requested. If we are in process of associating with
  596. * the selected BSSID, do not trigger new attempt.
  597. */
  598. if (wpa_s->reassociate ||
  599. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  600. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  601. os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
  602. 0))) {
  603. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  604. wpa_supplicant_req_new_scan(wpa_s, 10);
  605. return;
  606. }
  607. wpa_supplicant_associate(wpa_s, selected, ssid);
  608. } else {
  609. wpa_printf(MSG_DEBUG, "Already associated with the selected "
  610. "AP");
  611. }
  612. }
  613. static struct wpa_ssid *
  614. wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
  615. {
  616. int prio;
  617. struct wpa_ssid *ssid;
  618. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  619. for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
  620. {
  621. if (ssid->disabled)
  622. continue;
  623. if (ssid->mode == IEEE80211_MODE_IBSS ||
  624. ssid->mode == IEEE80211_MODE_AP)
  625. return ssid;
  626. }
  627. }
  628. return NULL;
  629. }
  630. /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
  631. * on BSS added and BSS changed events */
  632. static void wpa_supplicant_rsn_preauth_scan_results(
  633. struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
  634. {
  635. int i;
  636. if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
  637. return;
  638. for (i = scan_res->num - 1; i >= 0; i--) {
  639. const u8 *ssid, *rsn;
  640. struct wpa_scan_res *r;
  641. r = scan_res->res[i];
  642. ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
  643. if (ssid == NULL)
  644. continue;
  645. rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
  646. if (rsn == NULL)
  647. continue;
  648. rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
  649. }
  650. }
  651. static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
  652. struct wpa_bss *selected,
  653. struct wpa_ssid *ssid,
  654. struct wpa_scan_results *scan_res)
  655. {
  656. size_t i;
  657. struct wpa_scan_res *current_bss = NULL;
  658. int min_diff;
  659. if (wpa_s->reassociate)
  660. return 1; /* explicit request to reassociate */
  661. if (wpa_s->wpa_state < WPA_ASSOCIATED)
  662. return 1; /* we are not associated; continue */
  663. if (wpa_s->current_ssid == NULL)
  664. return 1; /* unknown current SSID */
  665. if (wpa_s->current_ssid != ssid)
  666. return 1; /* different network block */
  667. for (i = 0; i < scan_res->num; i++) {
  668. struct wpa_scan_res *res = scan_res->res[i];
  669. const u8 *ie;
  670. if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
  671. continue;
  672. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  673. if (ie == NULL)
  674. continue;
  675. if (ie[1] != wpa_s->current_ssid->ssid_len ||
  676. os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
  677. continue;
  678. current_bss = res;
  679. break;
  680. }
  681. if (!current_bss)
  682. return 1; /* current BSS not seen in scan results */
  683. wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
  684. wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
  685. MAC2STR(current_bss->bssid), current_bss->level);
  686. wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
  687. MAC2STR(selected->bssid), selected->level);
  688. if (wpa_s->current_ssid->bssid_set &&
  689. os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
  690. 0) {
  691. wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
  692. "preferred BSSID");
  693. return 1;
  694. }
  695. min_diff = 2;
  696. if (current_bss->level < 0) {
  697. if (current_bss->level < -85)
  698. min_diff = 1;
  699. else if (current_bss->level < -80)
  700. min_diff = 2;
  701. else if (current_bss->level < -75)
  702. min_diff = 3;
  703. else if (current_bss->level < -70)
  704. min_diff = 4;
  705. else
  706. min_diff = 5;
  707. }
  708. if (abs(current_bss->level - selected->level) < min_diff) {
  709. wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
  710. "signal level");
  711. return 0;
  712. }
  713. return 1;
  714. }
  715. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  716. union wpa_event_data *data)
  717. {
  718. struct wpa_bss *selected;
  719. struct wpa_ssid *ssid = NULL;
  720. struct wpa_scan_results *scan_res;
  721. wpa_supplicant_notify_scanning(wpa_s, 0);
  722. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  723. data ? &data->scan_info :
  724. NULL, 1);
  725. if (scan_res == NULL) {
  726. if (wpa_s->conf->ap_scan == 2)
  727. return;
  728. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  729. "scanning again");
  730. wpa_supplicant_req_new_scan(wpa_s, 1);
  731. return;
  732. }
  733. /*
  734. * Don't post the results if this was the initial cached
  735. * and there were no results.
  736. */
  737. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  738. scan_res->num == 0) {
  739. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  740. "empty - not posting");
  741. } else {
  742. wpa_printf(MSG_DEBUG, "New scan results available");
  743. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  744. wpas_notify_scan_results(wpa_s);
  745. }
  746. wpas_notify_scan_done(wpa_s, 1);
  747. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  748. wpa_scan_results_free(scan_res);
  749. return;
  750. }
  751. if (wpa_s->disconnected) {
  752. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  753. wpa_scan_results_free(scan_res);
  754. return;
  755. }
  756. if (bgscan_notify_scan(wpa_s) == 1) {
  757. wpa_scan_results_free(scan_res);
  758. return;
  759. }
  760. wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
  761. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  762. if (selected) {
  763. int skip;
  764. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  765. scan_res);
  766. wpa_scan_results_free(scan_res);
  767. if (skip)
  768. return;
  769. wpa_supplicant_connect(wpa_s, selected, ssid);
  770. } else {
  771. wpa_scan_results_free(scan_res);
  772. wpa_printf(MSG_DEBUG, "No suitable network found");
  773. ssid = wpa_supplicant_pick_new_network(wpa_s);
  774. if (ssid) {
  775. wpa_printf(MSG_DEBUG, "Setup a new network");
  776. wpa_supplicant_associate(wpa_s, NULL, ssid);
  777. } else
  778. wpa_supplicant_req_new_scan(wpa_s, 5);
  779. }
  780. }
  781. #endif /* CONFIG_NO_SCAN_PROCESSING */
  782. #ifdef CONFIG_IEEE80211R
  783. static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
  784. const u8 *ftie, const u8 *mdie)
  785. {
  786. const u8 *mobility_domain = NULL;
  787. const u8 *r0kh_id = NULL;
  788. size_t r0kh_id_len = 0;
  789. const u8 *r1kh_id = NULL;
  790. struct rsn_ftie *hdr;
  791. const u8 *pos, *end;
  792. if (mdie == NULL || ftie == NULL)
  793. return;
  794. if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
  795. mobility_domain = mdie + 2;
  796. #ifdef CONFIG_SME
  797. wpa_s->sme.ft_used = 1;
  798. os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
  799. #endif /* CONFIG_SME */
  800. }
  801. if (ftie[1] >= sizeof(struct rsn_ftie)) {
  802. end = ftie + 2 + ftie[1];
  803. hdr = (struct rsn_ftie *) (ftie + 2);
  804. pos = (const u8 *) (hdr + 1);
  805. while (pos + 1 < end) {
  806. if (pos + 2 + pos[1] > end)
  807. break;
  808. if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
  809. pos[1] == FT_R1KH_ID_LEN)
  810. r1kh_id = pos + 2;
  811. else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
  812. pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
  813. r0kh_id = pos + 2;
  814. r0kh_id_len = pos[1];
  815. }
  816. pos += 2 + pos[1];
  817. }
  818. }
  819. wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
  820. r0kh_id_len, r1kh_id);
  821. }
  822. #endif /* CONFIG_IEEE80211R */
  823. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  824. union wpa_event_data *data)
  825. {
  826. int l, len, found = 0, wpa_found, rsn_found;
  827. const u8 *p;
  828. #ifdef CONFIG_IEEE80211R
  829. const u8 *mdie = NULL, *ftie = NULL;
  830. #endif /* CONFIG_IEEE80211R */
  831. wpa_printf(MSG_DEBUG, "Association info event");
  832. if (data->assoc_info.req_ies)
  833. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  834. data->assoc_info.req_ies_len);
  835. if (data->assoc_info.resp_ies)
  836. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  837. data->assoc_info.resp_ies_len);
  838. if (data->assoc_info.beacon_ies)
  839. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  840. data->assoc_info.beacon_ies,
  841. data->assoc_info.beacon_ies_len);
  842. if (data->assoc_info.freq)
  843. wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
  844. p = data->assoc_info.req_ies;
  845. l = data->assoc_info.req_ies_len;
  846. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  847. while (p && l >= 2) {
  848. len = p[1] + 2;
  849. if (len > l) {
  850. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  851. p, l);
  852. break;
  853. }
  854. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  855. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  856. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  857. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  858. break;
  859. found = 1;
  860. wpa_find_assoc_pmkid(wpa_s);
  861. break;
  862. }
  863. l -= len;
  864. p += len;
  865. }
  866. if (!found && data->assoc_info.req_ies)
  867. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  868. #ifdef CONFIG_IEEE80211R
  869. p = data->assoc_info.resp_ies;
  870. l = data->assoc_info.resp_ies_len;
  871. /* Go through the IEs and make a copy of the FT/MD IE, if present. */
  872. while (p && l >= 2) {
  873. len = p[1] + 2;
  874. if (len > l) {
  875. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  876. p, l);
  877. break;
  878. }
  879. if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
  880. ftie = p;
  881. else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
  882. mdie = p;
  883. l -= len;
  884. p += len;
  885. }
  886. wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
  887. #endif /* CONFIG_IEEE80211R */
  888. /* WPA/RSN IE from Beacon/ProbeResp */
  889. p = data->assoc_info.beacon_ies;
  890. l = data->assoc_info.beacon_ies_len;
  891. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  892. */
  893. wpa_found = rsn_found = 0;
  894. while (p && l >= 2) {
  895. len = p[1] + 2;
  896. if (len > l) {
  897. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  898. p, l);
  899. break;
  900. }
  901. if (!wpa_found &&
  902. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  903. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  904. wpa_found = 1;
  905. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  906. }
  907. if (!rsn_found &&
  908. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  909. rsn_found = 1;
  910. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  911. }
  912. l -= len;
  913. p += len;
  914. }
  915. if (!wpa_found && data->assoc_info.beacon_ies)
  916. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  917. if (!rsn_found && data->assoc_info.beacon_ies)
  918. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  919. if (wpa_found || rsn_found)
  920. wpa_s->ap_ies_from_associnfo = 1;
  921. wpa_s->assoc_freq = data->assoc_info.freq;
  922. }
  923. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  924. union wpa_event_data *data)
  925. {
  926. u8 bssid[ETH_ALEN];
  927. int ft_completed;
  928. int bssid_changed;
  929. struct wpa_driver_capa capa;
  930. #ifdef CONFIG_AP
  931. if (wpa_s->ap_iface) {
  932. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  933. data->assoc_info.addr,
  934. data->assoc_info.req_ies,
  935. data->assoc_info.req_ies_len);
  936. return;
  937. }
  938. #endif /* CONFIG_AP */
  939. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  940. if (data)
  941. wpa_supplicant_event_associnfo(wpa_s, data);
  942. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  943. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  944. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  945. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  946. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  947. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  948. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  949. MACSTR, MAC2STR(bssid));
  950. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  951. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  952. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  953. if (bssid_changed)
  954. wpas_notify_bssid_changed(wpa_s);
  955. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  956. wpa_clear_keys(wpa_s, bssid);
  957. }
  958. if (wpa_supplicant_select_config(wpa_s) < 0) {
  959. wpa_supplicant_disassociate(
  960. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  961. return;
  962. }
  963. if (wpa_s->current_ssid) {
  964. struct wpa_bss *bss = NULL;
  965. struct wpa_ssid *ssid = wpa_s->current_ssid;
  966. if (ssid->ssid_len > 0)
  967. bss = wpa_bss_get(wpa_s, bssid,
  968. ssid->ssid, ssid->ssid_len);
  969. if (!bss)
  970. bss = wpa_bss_get_bssid(wpa_s, bssid);
  971. if (bss)
  972. wpa_s->current_bss = bss;
  973. }
  974. }
  975. #ifdef CONFIG_SME
  976. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  977. wpa_s->sme.prev_bssid_set = 1;
  978. #endif /* CONFIG_SME */
  979. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  980. if (wpa_s->current_ssid) {
  981. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  982. * initialized before association, but for other modes,
  983. * initialize PC/SC here, if the current configuration needs
  984. * smartcard or SIM/USIM. */
  985. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  986. }
  987. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  988. l2_packet_notify_auth_start(wpa_s->l2);
  989. /*
  990. * Set portEnabled first to FALSE in order to get EAP state machine out
  991. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  992. * state machine may transit to AUTHENTICATING state based on obsolete
  993. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  994. * AUTHENTICATED without ever giving chance to EAP state machine to
  995. * reset the state.
  996. */
  997. if (!ft_completed) {
  998. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  999. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1000. }
  1001. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  1002. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  1003. /* 802.1X::portControl = Auto */
  1004. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  1005. wpa_s->eapol_received = 0;
  1006. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1007. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1008. (wpa_s->current_ssid &&
  1009. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1010. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1011. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1012. } else if (!ft_completed) {
  1013. /* Timeout for receiving the first EAPOL packet */
  1014. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1015. }
  1016. wpa_supplicant_cancel_scan(wpa_s);
  1017. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1018. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1019. /*
  1020. * We are done; the driver will take care of RSN 4-way
  1021. * handshake.
  1022. */
  1023. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1024. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1025. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1026. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1027. }
  1028. if (wpa_s->pending_eapol_rx) {
  1029. struct os_time now, age;
  1030. os_get_time(&now);
  1031. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1032. if (age.sec == 0 && age.usec < 100000 &&
  1033. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1034. 0) {
  1035. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  1036. "that was received just before association "
  1037. "notification");
  1038. wpa_supplicant_rx_eapol(
  1039. wpa_s, wpa_s->pending_eapol_rx_src,
  1040. wpabuf_head(wpa_s->pending_eapol_rx),
  1041. wpabuf_len(wpa_s->pending_eapol_rx));
  1042. }
  1043. wpabuf_free(wpa_s->pending_eapol_rx);
  1044. wpa_s->pending_eapol_rx = NULL;
  1045. }
  1046. #ifdef CONFIG_BGSCAN
  1047. if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
  1048. bgscan_deinit(wpa_s);
  1049. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  1050. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  1051. wpa_printf(MSG_DEBUG, "Failed to initialize "
  1052. "bgscan");
  1053. /*
  1054. * Live without bgscan; it is only used as a
  1055. * roaming optimization, so the initial
  1056. * connection is not affected.
  1057. */
  1058. } else
  1059. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  1060. } else
  1061. wpa_s->bgscan_ssid = NULL;
  1062. }
  1063. #endif /* CONFIG_BGSCAN */
  1064. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1065. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1066. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1067. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1068. /* Set static WEP keys again */
  1069. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1070. }
  1071. }
  1072. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  1073. {
  1074. const u8 *bssid;
  1075. #ifdef CONFIG_SME
  1076. int authenticating;
  1077. u8 prev_pending_bssid[ETH_ALEN];
  1078. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1079. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1080. #endif /* CONFIG_SME */
  1081. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1082. /*
  1083. * At least Host AP driver and a Prism3 card seemed to be
  1084. * generating streams of disconnected events when configuring
  1085. * IBSS for WPA-None. Ignore them for now.
  1086. */
  1087. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  1088. "IBSS/WPA-None mode");
  1089. return;
  1090. }
  1091. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1092. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1093. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1094. "pre-shared key may be incorrect");
  1095. }
  1096. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  1097. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1098. bssid = wpa_s->bssid;
  1099. if (is_zero_ether_addr(bssid))
  1100. bssid = wpa_s->pending_bssid;
  1101. wpa_blacklist_add(wpa_s, bssid);
  1102. wpa_sm_notify_disassoc(wpa_s->wpa);
  1103. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  1104. "remove keys");
  1105. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1106. wpa_s->keys_cleared = 0;
  1107. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1108. }
  1109. wpa_supplicant_mark_disassoc(wpa_s);
  1110. bgscan_deinit(wpa_s);
  1111. #ifdef CONFIG_SME
  1112. if (authenticating &&
  1113. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1114. /*
  1115. * mac80211-workaround to force deauth on failed auth cmd,
  1116. * requires us to remain in authenticating state to allow the
  1117. * second authentication attempt to be continued properly.
  1118. */
  1119. wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
  1120. "proceed after disconnection event");
  1121. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1122. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1123. }
  1124. #endif /* CONFIG_SME */
  1125. }
  1126. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1127. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  1128. void *sock_ctx)
  1129. {
  1130. struct wpa_supplicant *wpa_s = eloop_ctx;
  1131. if (!wpa_s->pending_mic_error_report)
  1132. return;
  1133. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  1134. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1135. wpa_s->pending_mic_error_report = 0;
  1136. }
  1137. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1138. static void
  1139. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1140. union wpa_event_data *data)
  1141. {
  1142. int pairwise;
  1143. struct os_time t;
  1144. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1145. pairwise = (data && data->michael_mic_failure.unicast);
  1146. os_get_time(&t);
  1147. if ((wpa_s->last_michael_mic_error &&
  1148. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1149. wpa_s->pending_mic_error_report) {
  1150. if (wpa_s->pending_mic_error_report) {
  1151. /*
  1152. * Send the pending MIC error report immediately since
  1153. * we are going to start countermeasures and AP better
  1154. * do the same.
  1155. */
  1156. wpa_sm_key_request(wpa_s->wpa, 1,
  1157. wpa_s->pending_mic_error_pairwise);
  1158. }
  1159. /* Send the new MIC error report immediately since we are going
  1160. * to start countermeasures and AP better do the same.
  1161. */
  1162. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1163. /* initialize countermeasures */
  1164. wpa_s->countermeasures = 1;
  1165. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1166. /*
  1167. * Need to wait for completion of request frame. We do not get
  1168. * any callback for the message completion, so just wait a
  1169. * short while and hope for the best. */
  1170. os_sleep(0, 10000);
  1171. wpa_drv_set_countermeasures(wpa_s, 1);
  1172. wpa_supplicant_deauthenticate(wpa_s,
  1173. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1174. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1175. wpa_s, NULL);
  1176. eloop_register_timeout(60, 0,
  1177. wpa_supplicant_stop_countermeasures,
  1178. wpa_s, NULL);
  1179. /* TODO: mark the AP rejected for 60 second. STA is
  1180. * allowed to associate with another AP.. */
  1181. } else {
  1182. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1183. if (wpa_s->mic_errors_seen) {
  1184. /*
  1185. * Reduce the effectiveness of Michael MIC error
  1186. * reports as a means for attacking against TKIP if
  1187. * more than one MIC failure is noticed with the same
  1188. * PTK. We delay the transmission of the reports by a
  1189. * random time between 0 and 60 seconds in order to
  1190. * force the attacker wait 60 seconds before getting
  1191. * the information on whether a frame resulted in a MIC
  1192. * failure.
  1193. */
  1194. u8 rval[4];
  1195. int sec;
  1196. if (os_get_random(rval, sizeof(rval)) < 0)
  1197. sec = os_random() % 60;
  1198. else
  1199. sec = WPA_GET_BE32(rval) % 60;
  1200. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  1201. "seconds", sec);
  1202. wpa_s->pending_mic_error_report = 1;
  1203. wpa_s->pending_mic_error_pairwise = pairwise;
  1204. eloop_cancel_timeout(
  1205. wpa_supplicant_delayed_mic_error_report,
  1206. wpa_s, NULL);
  1207. eloop_register_timeout(
  1208. sec, os_random() % 1000000,
  1209. wpa_supplicant_delayed_mic_error_report,
  1210. wpa_s, NULL);
  1211. } else {
  1212. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1213. }
  1214. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1215. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1216. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1217. }
  1218. wpa_s->last_michael_mic_error = t.sec;
  1219. wpa_s->mic_errors_seen++;
  1220. }
  1221. #ifdef CONFIG_TERMINATE_ONLASTIF
  1222. static int any_interfaces(struct wpa_supplicant *head)
  1223. {
  1224. struct wpa_supplicant *wpa_s;
  1225. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1226. if (!wpa_s->interface_removed)
  1227. return 1;
  1228. return 0;
  1229. }
  1230. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1231. static void
  1232. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1233. union wpa_event_data *data)
  1234. {
  1235. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1236. return;
  1237. switch (data->interface_status.ievent) {
  1238. case EVENT_INTERFACE_ADDED:
  1239. if (!wpa_s->interface_removed)
  1240. break;
  1241. wpa_s->interface_removed = 0;
  1242. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  1243. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1244. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  1245. "after interface was added.");
  1246. }
  1247. break;
  1248. case EVENT_INTERFACE_REMOVED:
  1249. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1250. wpa_s->interface_removed = 1;
  1251. wpa_supplicant_mark_disassoc(wpa_s);
  1252. l2_packet_deinit(wpa_s->l2);
  1253. wpa_s->l2 = NULL;
  1254. #ifdef CONFIG_TERMINATE_ONLASTIF
  1255. /* check if last interface */
  1256. if (!any_interfaces(wpa_s->global->ifaces))
  1257. eloop_terminate();
  1258. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1259. break;
  1260. }
  1261. }
  1262. #ifdef CONFIG_PEERKEY
  1263. static void
  1264. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1265. union wpa_event_data *data)
  1266. {
  1267. if (data == NULL)
  1268. return;
  1269. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1270. }
  1271. #endif /* CONFIG_PEERKEY */
  1272. #ifdef CONFIG_IEEE80211R
  1273. static void
  1274. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1275. union wpa_event_data *data)
  1276. {
  1277. if (data == NULL)
  1278. return;
  1279. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1280. data->ft_ies.ies_len,
  1281. data->ft_ies.ft_action,
  1282. data->ft_ies.target_ap,
  1283. data->ft_ies.ric_ies,
  1284. data->ft_ies.ric_ies_len) < 0) {
  1285. /* TODO: prevent MLME/driver from trying to associate? */
  1286. }
  1287. }
  1288. #endif /* CONFIG_IEEE80211R */
  1289. #ifdef CONFIG_IBSS_RSN
  1290. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1291. union wpa_event_data *data)
  1292. {
  1293. if (data == NULL)
  1294. return;
  1295. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1296. }
  1297. #endif /* CONFIG_IBSS_RSN */
  1298. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1299. union wpa_event_data *data)
  1300. {
  1301. struct wpa_supplicant *wpa_s = ctx;
  1302. switch (event) {
  1303. case EVENT_AUTH:
  1304. sme_event_auth(wpa_s, data);
  1305. break;
  1306. case EVENT_ASSOC:
  1307. wpa_supplicant_event_assoc(wpa_s, data);
  1308. break;
  1309. case EVENT_DISASSOC:
  1310. wpa_printf(MSG_DEBUG, "Disassociation notification");
  1311. #ifdef CONFIG_AP
  1312. if (wpa_s->ap_iface && data) {
  1313. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1314. data->disassoc_info.addr);
  1315. break;
  1316. }
  1317. #endif /* CONFIG_AP */
  1318. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1319. sme_event_disassoc(wpa_s, data);
  1320. /* fall through */
  1321. case EVENT_DEAUTH:
  1322. if (event == EVENT_DEAUTH)
  1323. wpa_printf(MSG_DEBUG, "Deauthentication notification");
  1324. #ifdef CONFIG_AP
  1325. if (wpa_s->ap_iface && data) {
  1326. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1327. data->deauth_info.addr);
  1328. break;
  1329. }
  1330. #endif /* CONFIG_AP */
  1331. wpa_supplicant_event_disassoc(wpa_s);
  1332. break;
  1333. case EVENT_MICHAEL_MIC_FAILURE:
  1334. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1335. break;
  1336. #ifndef CONFIG_NO_SCAN_PROCESSING
  1337. case EVENT_SCAN_RESULTS:
  1338. wpa_supplicant_event_scan_results(wpa_s, data);
  1339. break;
  1340. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1341. case EVENT_ASSOCINFO:
  1342. wpa_supplicant_event_associnfo(wpa_s, data);
  1343. break;
  1344. case EVENT_INTERFACE_STATUS:
  1345. wpa_supplicant_event_interface_status(wpa_s, data);
  1346. break;
  1347. case EVENT_PMKID_CANDIDATE:
  1348. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1349. break;
  1350. #ifdef CONFIG_PEERKEY
  1351. case EVENT_STKSTART:
  1352. wpa_supplicant_event_stkstart(wpa_s, data);
  1353. break;
  1354. #endif /* CONFIG_PEERKEY */
  1355. #ifdef CONFIG_IEEE80211R
  1356. case EVENT_FT_RESPONSE:
  1357. wpa_supplicant_event_ft_response(wpa_s, data);
  1358. break;
  1359. #endif /* CONFIG_IEEE80211R */
  1360. #ifdef CONFIG_IBSS_RSN
  1361. case EVENT_IBSS_RSN_START:
  1362. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1363. break;
  1364. #endif /* CONFIG_IBSS_RSN */
  1365. case EVENT_ASSOC_REJECT:
  1366. sme_event_assoc_reject(wpa_s, data);
  1367. break;
  1368. case EVENT_AUTH_TIMED_OUT:
  1369. sme_event_auth_timed_out(wpa_s, data);
  1370. break;
  1371. case EVENT_ASSOC_TIMED_OUT:
  1372. sme_event_assoc_timed_out(wpa_s, data);
  1373. break;
  1374. #ifdef CONFIG_AP
  1375. case EVENT_TX_STATUS:
  1376. if (wpa_s->ap_iface == NULL)
  1377. break;
  1378. switch (data->tx_status.type) {
  1379. case WLAN_FC_TYPE_MGMT:
  1380. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1381. data->tx_status.data_len,
  1382. data->tx_status.stype,
  1383. data->tx_status.ack);
  1384. break;
  1385. case WLAN_FC_TYPE_DATA:
  1386. ap_tx_status(wpa_s, data->tx_status.dst,
  1387. data->tx_status.data,
  1388. data->tx_status.data_len,
  1389. data->tx_status.ack);
  1390. break;
  1391. }
  1392. break;
  1393. case EVENT_RX_FROM_UNKNOWN:
  1394. if (wpa_s->ap_iface == NULL)
  1395. break;
  1396. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
  1397. data->rx_from_unknown.len);
  1398. break;
  1399. case EVENT_RX_MGMT:
  1400. if (wpa_s->ap_iface == NULL)
  1401. break;
  1402. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1403. break;
  1404. #endif /* CONFIG_AP */
  1405. #ifdef CONFIG_CLIENT_MLME
  1406. case EVENT_MLME_RX: {
  1407. struct ieee80211_rx_status rx_status;
  1408. os_memset(&rx_status, 0, sizeof(rx_status));
  1409. rx_status.freq = data->mlme_rx.freq;
  1410. rx_status.channel = data->mlme_rx.channel;
  1411. rx_status.ssi = data->mlme_rx.ssi;
  1412. ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
  1413. &rx_status);
  1414. break;
  1415. }
  1416. #endif /* CONFIG_CLIENT_MLME */
  1417. case EVENT_EAPOL_RX:
  1418. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  1419. data->eapol_rx.data,
  1420. data->eapol_rx.data_len);
  1421. break;
  1422. default:
  1423. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1424. break;
  1425. }
  1426. }