events.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. min_diff = 2;
  689. if (current_bss->level < 0) {
  690. if (current_bss->level < -85)
  691. min_diff = 1;
  692. else if (current_bss->level < -80)
  693. min_diff = 2;
  694. else if (current_bss->level < -75)
  695. min_diff = 3;
  696. else if (current_bss->level < -70)
  697. min_diff = 4;
  698. else
  699. min_diff = 5;
  700. }
  701. if (abs(current_bss->level - selected->level) < min_diff) {
  702. wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
  703. "signal level");
  704. return 0;
  705. }
  706. return 1;
  707. }
  708. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
  709. union wpa_event_data *data)
  710. {
  711. struct wpa_bss *selected;
  712. struct wpa_ssid *ssid = NULL;
  713. struct wpa_scan_results *scan_res;
  714. wpa_supplicant_notify_scanning(wpa_s, 0);
  715. scan_res = wpa_supplicant_get_scan_results(wpa_s,
  716. data ? &data->scan_info :
  717. NULL, 1);
  718. if (scan_res == NULL) {
  719. if (wpa_s->conf->ap_scan == 2)
  720. return;
  721. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  722. "scanning again");
  723. wpa_supplicant_req_new_scan(wpa_s, 1);
  724. return;
  725. }
  726. /*
  727. * Don't post the results if this was the initial cached
  728. * and there were no results.
  729. */
  730. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  731. scan_res->num == 0) {
  732. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  733. "empty - not posting");
  734. } else {
  735. wpa_printf(MSG_DEBUG, "New scan results available");
  736. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  737. wpas_notify_scan_results(wpa_s);
  738. }
  739. wpas_notify_scan_done(wpa_s, 1);
  740. if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
  741. wpa_scan_results_free(scan_res);
  742. return;
  743. }
  744. if (wpa_s->disconnected) {
  745. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  746. wpa_scan_results_free(scan_res);
  747. return;
  748. }
  749. if (bgscan_notify_scan(wpa_s) == 1) {
  750. wpa_scan_results_free(scan_res);
  751. return;
  752. }
  753. wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
  754. selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
  755. if (selected) {
  756. int skip;
  757. skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
  758. scan_res);
  759. wpa_scan_results_free(scan_res);
  760. if (skip)
  761. return;
  762. wpa_supplicant_connect(wpa_s, selected, ssid);
  763. } else {
  764. wpa_scan_results_free(scan_res);
  765. wpa_printf(MSG_DEBUG, "No suitable network found");
  766. ssid = wpa_supplicant_pick_new_network(wpa_s);
  767. if (ssid) {
  768. wpa_printf(MSG_DEBUG, "Setup a new network");
  769. wpa_supplicant_associate(wpa_s, NULL, ssid);
  770. } else
  771. wpa_supplicant_req_new_scan(wpa_s, 5);
  772. }
  773. }
  774. #endif /* CONFIG_NO_SCAN_PROCESSING */
  775. #ifdef CONFIG_IEEE80211R
  776. static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
  777. const u8 *ftie, const u8 *mdie)
  778. {
  779. const u8 *mobility_domain = NULL;
  780. const u8 *r0kh_id = NULL;
  781. size_t r0kh_id_len = 0;
  782. const u8 *r1kh_id = NULL;
  783. struct rsn_ftie *hdr;
  784. const u8 *pos, *end;
  785. if (mdie == NULL || ftie == NULL)
  786. return;
  787. if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
  788. mobility_domain = mdie + 2;
  789. #ifdef CONFIG_SME
  790. wpa_s->sme.ft_used = 1;
  791. os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
  792. #endif /* CONFIG_SME */
  793. }
  794. if (ftie[1] >= sizeof(struct rsn_ftie)) {
  795. end = ftie + 2 + ftie[1];
  796. hdr = (struct rsn_ftie *) (ftie + 2);
  797. pos = (const u8 *) (hdr + 1);
  798. while (pos + 1 < end) {
  799. if (pos + 2 + pos[1] > end)
  800. break;
  801. if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
  802. pos[1] == FT_R1KH_ID_LEN)
  803. r1kh_id = pos + 2;
  804. else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
  805. pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
  806. r0kh_id = pos + 2;
  807. r0kh_id_len = pos[1];
  808. }
  809. pos += 2 + pos[1];
  810. }
  811. }
  812. wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
  813. r0kh_id_len, r1kh_id);
  814. }
  815. #endif /* CONFIG_IEEE80211R */
  816. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  817. union wpa_event_data *data)
  818. {
  819. int l, len, found = 0, wpa_found, rsn_found;
  820. const u8 *p;
  821. #ifdef CONFIG_IEEE80211R
  822. const u8 *mdie = NULL, *ftie = NULL;
  823. #endif /* CONFIG_IEEE80211R */
  824. wpa_printf(MSG_DEBUG, "Association info event");
  825. if (data->assoc_info.req_ies)
  826. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  827. data->assoc_info.req_ies_len);
  828. if (data->assoc_info.resp_ies)
  829. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  830. data->assoc_info.resp_ies_len);
  831. if (data->assoc_info.beacon_ies)
  832. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  833. data->assoc_info.beacon_ies,
  834. data->assoc_info.beacon_ies_len);
  835. if (data->assoc_info.freq)
  836. wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
  837. p = data->assoc_info.req_ies;
  838. l = data->assoc_info.req_ies_len;
  839. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  840. while (p && l >= 2) {
  841. len = p[1] + 2;
  842. if (len > l) {
  843. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  844. p, l);
  845. break;
  846. }
  847. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  848. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  849. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  850. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  851. break;
  852. found = 1;
  853. wpa_find_assoc_pmkid(wpa_s);
  854. break;
  855. }
  856. l -= len;
  857. p += len;
  858. }
  859. if (!found && data->assoc_info.req_ies)
  860. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  861. #ifdef CONFIG_IEEE80211R
  862. p = data->assoc_info.resp_ies;
  863. l = data->assoc_info.resp_ies_len;
  864. /* Go through the IEs and make a copy of the FT/MD IE, if present. */
  865. while (p && l >= 2) {
  866. len = p[1] + 2;
  867. if (len > l) {
  868. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  869. p, l);
  870. break;
  871. }
  872. if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
  873. ftie = p;
  874. else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
  875. mdie = p;
  876. l -= len;
  877. p += len;
  878. }
  879. wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
  880. #endif /* CONFIG_IEEE80211R */
  881. /* WPA/RSN IE from Beacon/ProbeResp */
  882. p = data->assoc_info.beacon_ies;
  883. l = data->assoc_info.beacon_ies_len;
  884. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  885. */
  886. wpa_found = rsn_found = 0;
  887. while (p && l >= 2) {
  888. len = p[1] + 2;
  889. if (len > l) {
  890. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  891. p, l);
  892. break;
  893. }
  894. if (!wpa_found &&
  895. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  896. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  897. wpa_found = 1;
  898. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  899. }
  900. if (!rsn_found &&
  901. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  902. rsn_found = 1;
  903. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  904. }
  905. l -= len;
  906. p += len;
  907. }
  908. if (!wpa_found && data->assoc_info.beacon_ies)
  909. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  910. if (!rsn_found && data->assoc_info.beacon_ies)
  911. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  912. if (wpa_found || rsn_found)
  913. wpa_s->ap_ies_from_associnfo = 1;
  914. wpa_s->assoc_freq = data->assoc_info.freq;
  915. }
  916. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  917. union wpa_event_data *data)
  918. {
  919. u8 bssid[ETH_ALEN];
  920. int ft_completed;
  921. int bssid_changed;
  922. struct wpa_driver_capa capa;
  923. #ifdef CONFIG_AP
  924. if (wpa_s->ap_iface) {
  925. hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
  926. data->assoc_info.addr,
  927. data->assoc_info.req_ies,
  928. data->assoc_info.req_ies_len);
  929. return;
  930. }
  931. #endif /* CONFIG_AP */
  932. ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  933. if (data)
  934. wpa_supplicant_event_associnfo(wpa_s, data);
  935. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  936. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  937. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  938. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  939. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  940. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  941. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  942. MACSTR, MAC2STR(bssid));
  943. bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
  944. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  945. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  946. if (bssid_changed)
  947. wpas_notify_bssid_changed(wpa_s);
  948. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  949. wpa_clear_keys(wpa_s, bssid);
  950. }
  951. if (wpa_supplicant_select_config(wpa_s) < 0) {
  952. wpa_supplicant_disassociate(
  953. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  954. return;
  955. }
  956. if (wpa_s->current_ssid) {
  957. struct wpa_bss *bss = NULL;
  958. struct wpa_ssid *ssid = wpa_s->current_ssid;
  959. if (ssid->ssid_len > 0)
  960. bss = wpa_bss_get(wpa_s, bssid,
  961. ssid->ssid, ssid->ssid_len);
  962. if (!bss)
  963. bss = wpa_bss_get_bssid(wpa_s, bssid);
  964. if (bss)
  965. wpa_s->current_bss = bss;
  966. }
  967. }
  968. #ifdef CONFIG_SME
  969. os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
  970. wpa_s->sme.prev_bssid_set = 1;
  971. #endif /* CONFIG_SME */
  972. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  973. if (wpa_s->current_ssid) {
  974. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  975. * initialized before association, but for other modes,
  976. * initialize PC/SC here, if the current configuration needs
  977. * smartcard or SIM/USIM. */
  978. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  979. }
  980. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  981. l2_packet_notify_auth_start(wpa_s->l2);
  982. /*
  983. * Set portEnabled first to FALSE in order to get EAP state machine out
  984. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  985. * state machine may transit to AUTHENTICATING state based on obsolete
  986. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  987. * AUTHENTICATED without ever giving chance to EAP state machine to
  988. * reset the state.
  989. */
  990. if (!ft_completed) {
  991. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  992. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  993. }
  994. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  995. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  996. /* 802.1X::portControl = Auto */
  997. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  998. wpa_s->eapol_received = 0;
  999. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1000. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
  1001. (wpa_s->current_ssid &&
  1002. wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
  1003. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1004. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1005. } else if (!ft_completed) {
  1006. /* Timeout for receiving the first EAPOL packet */
  1007. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  1008. }
  1009. wpa_supplicant_cancel_scan(wpa_s);
  1010. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1011. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1012. /*
  1013. * We are done; the driver will take care of RSN 4-way
  1014. * handshake.
  1015. */
  1016. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1017. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1018. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1019. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  1020. }
  1021. if (wpa_s->pending_eapol_rx) {
  1022. struct os_time now, age;
  1023. os_get_time(&now);
  1024. os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
  1025. if (age.sec == 0 && age.usec < 100000 &&
  1026. os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
  1027. 0) {
  1028. wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
  1029. "that was received just before association "
  1030. "notification");
  1031. wpa_supplicant_rx_eapol(
  1032. wpa_s, wpa_s->pending_eapol_rx_src,
  1033. wpabuf_head(wpa_s->pending_eapol_rx),
  1034. wpabuf_len(wpa_s->pending_eapol_rx));
  1035. }
  1036. wpabuf_free(wpa_s->pending_eapol_rx);
  1037. wpa_s->pending_eapol_rx = NULL;
  1038. }
  1039. #ifdef CONFIG_BGSCAN
  1040. if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
  1041. bgscan_deinit(wpa_s);
  1042. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  1043. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  1044. wpa_printf(MSG_DEBUG, "Failed to initialize "
  1045. "bgscan");
  1046. /*
  1047. * Live without bgscan; it is only used as a
  1048. * roaming optimization, so the initial
  1049. * connection is not affected.
  1050. */
  1051. } else
  1052. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  1053. } else
  1054. wpa_s->bgscan_ssid = NULL;
  1055. }
  1056. #endif /* CONFIG_BGSCAN */
  1057. if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1058. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  1059. wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1060. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
  1061. /* Set static WEP keys again */
  1062. wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
  1063. }
  1064. }
  1065. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  1066. {
  1067. const u8 *bssid;
  1068. #ifdef CONFIG_SME
  1069. int authenticating;
  1070. u8 prev_pending_bssid[ETH_ALEN];
  1071. authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
  1072. os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
  1073. #endif /* CONFIG_SME */
  1074. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1075. /*
  1076. * At least Host AP driver and a Prism3 card seemed to be
  1077. * generating streams of disconnected events when configuring
  1078. * IBSS for WPA-None. Ignore them for now.
  1079. */
  1080. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  1081. "IBSS/WPA-None mode");
  1082. return;
  1083. }
  1084. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  1085. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  1086. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  1087. "pre-shared key may be incorrect");
  1088. }
  1089. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  1090. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  1091. bssid = wpa_s->bssid;
  1092. if (is_zero_ether_addr(bssid))
  1093. bssid = wpa_s->pending_bssid;
  1094. wpa_blacklist_add(wpa_s, bssid);
  1095. wpa_sm_notify_disassoc(wpa_s->wpa);
  1096. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  1097. "remove keys");
  1098. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  1099. wpa_s->keys_cleared = 0;
  1100. wpa_clear_keys(wpa_s, wpa_s->bssid);
  1101. }
  1102. wpa_supplicant_mark_disassoc(wpa_s);
  1103. bgscan_deinit(wpa_s);
  1104. #ifdef CONFIG_SME
  1105. if (authenticating &&
  1106. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
  1107. /*
  1108. * mac80211-workaround to force deauth on failed auth cmd,
  1109. * requires us to remain in authenticating state to allow the
  1110. * second authentication attempt to be continued properly.
  1111. */
  1112. wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
  1113. "proceed after disconnection event");
  1114. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  1115. os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
  1116. }
  1117. #endif /* CONFIG_SME */
  1118. }
  1119. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1120. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  1121. void *sock_ctx)
  1122. {
  1123. struct wpa_supplicant *wpa_s = eloop_ctx;
  1124. if (!wpa_s->pending_mic_error_report)
  1125. return;
  1126. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  1127. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  1128. wpa_s->pending_mic_error_report = 0;
  1129. }
  1130. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1131. static void
  1132. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  1133. union wpa_event_data *data)
  1134. {
  1135. int pairwise;
  1136. struct os_time t;
  1137. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  1138. pairwise = (data && data->michael_mic_failure.unicast);
  1139. os_get_time(&t);
  1140. if ((wpa_s->last_michael_mic_error &&
  1141. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  1142. wpa_s->pending_mic_error_report) {
  1143. if (wpa_s->pending_mic_error_report) {
  1144. /*
  1145. * Send the pending MIC error report immediately since
  1146. * we are going to start countermeasures and AP better
  1147. * do the same.
  1148. */
  1149. wpa_sm_key_request(wpa_s->wpa, 1,
  1150. wpa_s->pending_mic_error_pairwise);
  1151. }
  1152. /* Send the new MIC error report immediately since we are going
  1153. * to start countermeasures and AP better do the same.
  1154. */
  1155. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1156. /* initialize countermeasures */
  1157. wpa_s->countermeasures = 1;
  1158. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  1159. /*
  1160. * Need to wait for completion of request frame. We do not get
  1161. * any callback for the message completion, so just wait a
  1162. * short while and hope for the best. */
  1163. os_sleep(0, 10000);
  1164. wpa_drv_set_countermeasures(wpa_s, 1);
  1165. wpa_supplicant_deauthenticate(wpa_s,
  1166. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1167. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  1168. wpa_s, NULL);
  1169. eloop_register_timeout(60, 0,
  1170. wpa_supplicant_stop_countermeasures,
  1171. wpa_s, NULL);
  1172. /* TODO: mark the AP rejected for 60 second. STA is
  1173. * allowed to associate with another AP.. */
  1174. } else {
  1175. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  1176. if (wpa_s->mic_errors_seen) {
  1177. /*
  1178. * Reduce the effectiveness of Michael MIC error
  1179. * reports as a means for attacking against TKIP if
  1180. * more than one MIC failure is noticed with the same
  1181. * PTK. We delay the transmission of the reports by a
  1182. * random time between 0 and 60 seconds in order to
  1183. * force the attacker wait 60 seconds before getting
  1184. * the information on whether a frame resulted in a MIC
  1185. * failure.
  1186. */
  1187. u8 rval[4];
  1188. int sec;
  1189. if (os_get_random(rval, sizeof(rval)) < 0)
  1190. sec = os_random() % 60;
  1191. else
  1192. sec = WPA_GET_BE32(rval) % 60;
  1193. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  1194. "seconds", sec);
  1195. wpa_s->pending_mic_error_report = 1;
  1196. wpa_s->pending_mic_error_pairwise = pairwise;
  1197. eloop_cancel_timeout(
  1198. wpa_supplicant_delayed_mic_error_report,
  1199. wpa_s, NULL);
  1200. eloop_register_timeout(
  1201. sec, os_random() % 1000000,
  1202. wpa_supplicant_delayed_mic_error_report,
  1203. wpa_s, NULL);
  1204. } else {
  1205. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1206. }
  1207. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1208. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  1209. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  1210. }
  1211. wpa_s->last_michael_mic_error = t.sec;
  1212. wpa_s->mic_errors_seen++;
  1213. }
  1214. #ifdef CONFIG_TERMINATE_ONLASTIF
  1215. static int any_interfaces(struct wpa_supplicant *head)
  1216. {
  1217. struct wpa_supplicant *wpa_s;
  1218. for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
  1219. if (!wpa_s->interface_removed)
  1220. return 1;
  1221. return 0;
  1222. }
  1223. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1224. static void
  1225. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  1226. union wpa_event_data *data)
  1227. {
  1228. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  1229. return;
  1230. switch (data->interface_status.ievent) {
  1231. case EVENT_INTERFACE_ADDED:
  1232. if (!wpa_s->interface_removed)
  1233. break;
  1234. wpa_s->interface_removed = 0;
  1235. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  1236. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  1237. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  1238. "after interface was added.");
  1239. }
  1240. break;
  1241. case EVENT_INTERFACE_REMOVED:
  1242. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  1243. wpa_s->interface_removed = 1;
  1244. wpa_supplicant_mark_disassoc(wpa_s);
  1245. l2_packet_deinit(wpa_s->l2);
  1246. wpa_s->l2 = NULL;
  1247. #ifdef CONFIG_TERMINATE_ONLASTIF
  1248. /* check if last interface */
  1249. if (!any_interfaces(wpa_s->global->ifaces))
  1250. eloop_terminate();
  1251. #endif /* CONFIG_TERMINATE_ONLASTIF */
  1252. break;
  1253. }
  1254. }
  1255. #ifdef CONFIG_PEERKEY
  1256. static void
  1257. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  1258. union wpa_event_data *data)
  1259. {
  1260. if (data == NULL)
  1261. return;
  1262. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  1263. }
  1264. #endif /* CONFIG_PEERKEY */
  1265. #ifdef CONFIG_IEEE80211R
  1266. static void
  1267. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  1268. union wpa_event_data *data)
  1269. {
  1270. if (data == NULL)
  1271. return;
  1272. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  1273. data->ft_ies.ies_len,
  1274. data->ft_ies.ft_action,
  1275. data->ft_ies.target_ap,
  1276. data->ft_ies.ric_ies,
  1277. data->ft_ies.ric_ies_len) < 0) {
  1278. /* TODO: prevent MLME/driver from trying to associate? */
  1279. }
  1280. }
  1281. #endif /* CONFIG_IEEE80211R */
  1282. #ifdef CONFIG_IBSS_RSN
  1283. static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
  1284. union wpa_event_data *data)
  1285. {
  1286. if (data == NULL)
  1287. return;
  1288. ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
  1289. }
  1290. #endif /* CONFIG_IBSS_RSN */
  1291. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  1292. union wpa_event_data *data)
  1293. {
  1294. struct wpa_supplicant *wpa_s = ctx;
  1295. switch (event) {
  1296. case EVENT_AUTH:
  1297. sme_event_auth(wpa_s, data);
  1298. break;
  1299. case EVENT_ASSOC:
  1300. wpa_supplicant_event_assoc(wpa_s, data);
  1301. break;
  1302. case EVENT_DISASSOC:
  1303. wpa_printf(MSG_DEBUG, "Disassociation notification");
  1304. #ifdef CONFIG_AP
  1305. if (wpa_s->ap_iface && data) {
  1306. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1307. data->disassoc_info.addr);
  1308. break;
  1309. }
  1310. #endif /* CONFIG_AP */
  1311. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
  1312. sme_event_disassoc(wpa_s, data);
  1313. /* fall through */
  1314. case EVENT_DEAUTH:
  1315. if (event == EVENT_DEAUTH)
  1316. wpa_printf(MSG_DEBUG, "Deauthentication notification");
  1317. #ifdef CONFIG_AP
  1318. if (wpa_s->ap_iface && data) {
  1319. hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
  1320. data->deauth_info.addr);
  1321. break;
  1322. }
  1323. #endif /* CONFIG_AP */
  1324. wpa_supplicant_event_disassoc(wpa_s);
  1325. break;
  1326. case EVENT_MICHAEL_MIC_FAILURE:
  1327. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  1328. break;
  1329. #ifndef CONFIG_NO_SCAN_PROCESSING
  1330. case EVENT_SCAN_RESULTS:
  1331. wpa_supplicant_event_scan_results(wpa_s, data);
  1332. break;
  1333. #endif /* CONFIG_NO_SCAN_PROCESSING */
  1334. case EVENT_ASSOCINFO:
  1335. wpa_supplicant_event_associnfo(wpa_s, data);
  1336. break;
  1337. case EVENT_INTERFACE_STATUS:
  1338. wpa_supplicant_event_interface_status(wpa_s, data);
  1339. break;
  1340. case EVENT_PMKID_CANDIDATE:
  1341. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  1342. break;
  1343. #ifdef CONFIG_PEERKEY
  1344. case EVENT_STKSTART:
  1345. wpa_supplicant_event_stkstart(wpa_s, data);
  1346. break;
  1347. #endif /* CONFIG_PEERKEY */
  1348. #ifdef CONFIG_IEEE80211R
  1349. case EVENT_FT_RESPONSE:
  1350. wpa_supplicant_event_ft_response(wpa_s, data);
  1351. break;
  1352. #endif /* CONFIG_IEEE80211R */
  1353. #ifdef CONFIG_IBSS_RSN
  1354. case EVENT_IBSS_RSN_START:
  1355. wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
  1356. break;
  1357. #endif /* CONFIG_IBSS_RSN */
  1358. case EVENT_ASSOC_REJECT:
  1359. sme_event_assoc_reject(wpa_s, data);
  1360. break;
  1361. case EVENT_AUTH_TIMED_OUT:
  1362. sme_event_auth_timed_out(wpa_s, data);
  1363. break;
  1364. case EVENT_ASSOC_TIMED_OUT:
  1365. sme_event_assoc_timed_out(wpa_s, data);
  1366. break;
  1367. #ifdef CONFIG_AP
  1368. case EVENT_TX_STATUS:
  1369. if (wpa_s->ap_iface == NULL)
  1370. break;
  1371. switch (data->tx_status.type) {
  1372. case WLAN_FC_TYPE_MGMT:
  1373. ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
  1374. data->tx_status.data_len,
  1375. data->tx_status.stype,
  1376. data->tx_status.ack);
  1377. break;
  1378. case WLAN_FC_TYPE_DATA:
  1379. ap_tx_status(wpa_s, data->tx_status.dst,
  1380. data->tx_status.data,
  1381. data->tx_status.data_len,
  1382. data->tx_status.ack);
  1383. break;
  1384. }
  1385. break;
  1386. case EVENT_RX_FROM_UNKNOWN:
  1387. if (wpa_s->ap_iface == NULL)
  1388. break;
  1389. ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
  1390. data->rx_from_unknown.len);
  1391. break;
  1392. case EVENT_RX_MGMT:
  1393. if (wpa_s->ap_iface == NULL)
  1394. break;
  1395. ap_mgmt_rx(wpa_s, &data->rx_mgmt);
  1396. break;
  1397. #endif /* CONFIG_AP */
  1398. #ifdef CONFIG_CLIENT_MLME
  1399. case EVENT_MLME_RX: {
  1400. struct ieee80211_rx_status rx_status;
  1401. os_memset(&rx_status, 0, sizeof(rx_status));
  1402. rx_status.freq = data->mlme_rx.freq;
  1403. rx_status.channel = data->mlme_rx.channel;
  1404. rx_status.ssi = data->mlme_rx.ssi;
  1405. ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
  1406. &rx_status);
  1407. break;
  1408. }
  1409. #endif /* CONFIG_CLIENT_MLME */
  1410. case EVENT_EAPOL_RX:
  1411. wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
  1412. data->eapol_rx.data,
  1413. data->eapol_rx.data_len);
  1414. break;
  1415. default:
  1416. wpa_printf(MSG_INFO, "Unknown event %d", event);
  1417. break;
  1418. }
  1419. }