events.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * WPA Supplicant - Driver event processing
  3. * Copyright (c) 2003-2008, 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 "wpa.h"
  18. #include "eloop.h"
  19. #include "drivers/driver.h"
  20. #include "config.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "pcsc_funcs.h"
  24. #include "preauth.h"
  25. #include "pmksa_cache.h"
  26. #include "wpa_ctrl.h"
  27. #include "eap_peer/eap.h"
  28. #include "ctrl_iface_dbus.h"
  29. #include "ieee802_11_defs.h"
  30. #include "blacklist.h"
  31. #include "wpas_glue.h"
  32. #include "wps_supplicant.h"
  33. static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
  34. {
  35. struct wpa_ssid *ssid;
  36. if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
  37. return 0;
  38. wpa_printf(MSG_DEBUG, "Select network based on association "
  39. "information");
  40. ssid = wpa_supplicant_get_ssid(wpa_s);
  41. if (ssid == NULL) {
  42. wpa_printf(MSG_INFO, "No network configuration found for the "
  43. "current AP");
  44. return -1;
  45. }
  46. if (ssid->disabled) {
  47. wpa_printf(MSG_DEBUG, "Selected network is disabled");
  48. return -1;
  49. }
  50. wpa_printf(MSG_DEBUG, "Network configuration found for the current "
  51. "AP");
  52. if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  53. WPA_KEY_MGMT_WPA_NONE |
  54. WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
  55. WPA_KEY_MGMT_PSK_SHA256 |
  56. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  57. u8 wpa_ie[80];
  58. size_t wpa_ie_len = sizeof(wpa_ie);
  59. wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  60. wpa_ie, &wpa_ie_len);
  61. } else {
  62. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  63. }
  64. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
  65. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  66. wpa_s->current_ssid = ssid;
  67. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  68. wpa_supplicant_initiate_eapol(wpa_s);
  69. return 0;
  70. }
  71. static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
  72. void *sock_ctx)
  73. {
  74. struct wpa_supplicant *wpa_s = eloop_ctx;
  75. if (wpa_s->countermeasures) {
  76. wpa_s->countermeasures = 0;
  77. wpa_drv_set_countermeasures(wpa_s, 0);
  78. wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
  79. wpa_supplicant_req_scan(wpa_s, 0, 0);
  80. }
  81. }
  82. void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
  83. {
  84. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  85. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  86. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  87. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  88. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  89. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
  90. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  91. wpa_s->ap_ies_from_associnfo = 0;
  92. }
  93. static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
  94. {
  95. struct wpa_ie_data ie;
  96. int pmksa_set = -1;
  97. size_t i;
  98. if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
  99. ie.pmkid == NULL)
  100. return;
  101. for (i = 0; i < ie.num_pmkid; i++) {
  102. pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
  103. ie.pmkid + i * PMKID_LEN,
  104. NULL, NULL, 0);
  105. if (pmksa_set == 0) {
  106. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  107. break;
  108. }
  109. }
  110. wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
  111. "cache", pmksa_set == 0 ? "" : "not ");
  112. }
  113. static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
  114. union wpa_event_data *data)
  115. {
  116. if (data == NULL) {
  117. wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
  118. return;
  119. }
  120. wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
  121. " index=%d preauth=%d",
  122. MAC2STR(data->pmkid_candidate.bssid),
  123. data->pmkid_candidate.index,
  124. data->pmkid_candidate.preauth);
  125. pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
  126. data->pmkid_candidate.index,
  127. data->pmkid_candidate.preauth);
  128. }
  129. static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
  130. {
  131. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  132. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  133. return 0;
  134. #ifdef IEEE8021X_EAPOL
  135. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  136. wpa_s->current_ssid &&
  137. !(wpa_s->current_ssid->eapol_flags &
  138. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  139. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
  140. /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
  141. * plaintext or static WEP keys). */
  142. return 0;
  143. }
  144. #endif /* IEEE8021X_EAPOL */
  145. return 1;
  146. }
  147. /**
  148. * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
  149. * @wpa_s: pointer to wpa_supplicant data
  150. * @ssid: Configuration data for the network
  151. * Returns: 0 on success, -1 on failure
  152. *
  153. * This function is called when starting authentication with a network that is
  154. * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
  155. */
  156. int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
  157. struct wpa_ssid *ssid)
  158. {
  159. #ifdef IEEE8021X_EAPOL
  160. int aka = 0, sim = 0, type;
  161. if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
  162. return 0;
  163. if (ssid->eap.eap_methods == NULL) {
  164. sim = 1;
  165. aka = 1;
  166. } else {
  167. struct eap_method_type *eap = ssid->eap.eap_methods;
  168. while (eap->vendor != EAP_VENDOR_IETF ||
  169. eap->method != EAP_TYPE_NONE) {
  170. if (eap->vendor == EAP_VENDOR_IETF) {
  171. if (eap->method == EAP_TYPE_SIM)
  172. sim = 1;
  173. else if (eap->method == EAP_TYPE_AKA)
  174. aka = 1;
  175. }
  176. eap++;
  177. }
  178. }
  179. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
  180. sim = 0;
  181. if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
  182. aka = 0;
  183. if (!sim && !aka) {
  184. wpa_printf(MSG_DEBUG, "Selected network is configured to use "
  185. "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
  186. return 0;
  187. }
  188. wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
  189. "(sim=%d aka=%d) - initialize PCSC", sim, aka);
  190. if (sim && aka)
  191. type = SCARD_TRY_BOTH;
  192. else if (aka)
  193. type = SCARD_USIM_ONLY;
  194. else
  195. type = SCARD_GSM_SIM_ONLY;
  196. wpa_s->scard = scard_init(type);
  197. if (wpa_s->scard == NULL) {
  198. wpa_printf(MSG_WARNING, "Failed to initialize SIM "
  199. "(pcsc-lite)");
  200. return -1;
  201. }
  202. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  203. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  204. #endif /* IEEE8021X_EAPOL */
  205. return 0;
  206. }
  207. #ifndef CONFIG_NO_SCAN_PROCESSING
  208. static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
  209. struct wpa_ssid *ssid)
  210. {
  211. int i, privacy = 0;
  212. if (ssid->mixed_cell)
  213. return 1;
  214. for (i = 0; i < NUM_WEP_KEYS; i++) {
  215. if (ssid->wep_key_len[i]) {
  216. privacy = 1;
  217. break;
  218. }
  219. }
  220. #ifdef IEEE8021X_EAPOL
  221. if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
  222. ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  223. EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
  224. privacy = 1;
  225. #endif /* IEEE8021X_EAPOL */
  226. if (bss->caps & IEEE80211_CAP_PRIVACY)
  227. return privacy;
  228. return !privacy;
  229. }
  230. static int wpa_supplicant_ssid_bss_match(struct wpa_ssid *ssid,
  231. struct wpa_scan_res *bss)
  232. {
  233. struct wpa_ie_data ie;
  234. int proto_match = 0;
  235. const u8 *rsn_ie, *wpa_ie;
  236. int ret;
  237. ret = wpas_wps_ssid_bss_match(ssid, bss);
  238. if (ret >= 0)
  239. return ret;
  240. rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  241. while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
  242. proto_match++;
  243. if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
  244. wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
  245. break;
  246. }
  247. if (!(ie.proto & ssid->proto)) {
  248. wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
  249. "mismatch");
  250. break;
  251. }
  252. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  253. wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
  254. "mismatch");
  255. break;
  256. }
  257. if (!(ie.group_cipher & ssid->group_cipher)) {
  258. wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
  259. "mismatch");
  260. break;
  261. }
  262. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  263. wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
  264. "mismatch");
  265. break;
  266. }
  267. #ifdef CONFIG_IEEE80211W
  268. if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
  269. ssid->ieee80211w == IEEE80211W_REQUIRED) {
  270. wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
  271. "protection");
  272. break;
  273. }
  274. #endif /* CONFIG_IEEE80211W */
  275. wpa_printf(MSG_DEBUG, " selected based on RSN IE");
  276. return 1;
  277. }
  278. wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  279. while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
  280. proto_match++;
  281. if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
  282. wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
  283. break;
  284. }
  285. if (!(ie.proto & ssid->proto)) {
  286. wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
  287. "mismatch");
  288. break;
  289. }
  290. if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
  291. wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
  292. "mismatch");
  293. break;
  294. }
  295. if (!(ie.group_cipher & ssid->group_cipher)) {
  296. wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
  297. "mismatch");
  298. break;
  299. }
  300. if (!(ie.key_mgmt & ssid->key_mgmt)) {
  301. wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
  302. "mismatch");
  303. break;
  304. }
  305. wpa_printf(MSG_DEBUG, " selected based on WPA IE");
  306. return 1;
  307. }
  308. if (proto_match == 0)
  309. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
  310. return 0;
  311. }
  312. static struct wpa_scan_res *
  313. wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
  314. struct wpa_ssid *group,
  315. struct wpa_ssid **selected_ssid)
  316. {
  317. struct wpa_ssid *ssid;
  318. struct wpa_scan_res *bss;
  319. size_t i;
  320. struct wpa_blacklist *e;
  321. const u8 *ie;
  322. wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
  323. for (i = 0; i < wpa_s->scan_res->num; i++) {
  324. const u8 *ssid_;
  325. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  326. bss = wpa_s->scan_res->res[i];
  327. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  328. ssid_ = ie ? ie + 2 : (u8 *) "";
  329. ssid_len = ie ? ie[1] : 0;
  330. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  331. wpa_ie_len = ie ? ie[1] : 0;
  332. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  333. rsn_ie_len = ie ? ie[1] : 0;
  334. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  335. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  336. (int) i, MAC2STR(bss->bssid),
  337. wpa_ssid_txt(ssid_, ssid_len),
  338. wpa_ie_len, rsn_ie_len, bss->caps);
  339. e = wpa_blacklist_get(wpa_s, bss->bssid);
  340. if (e && e->count > 1) {
  341. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  342. continue;
  343. }
  344. if (wpa_ie_len == 0 && rsn_ie_len == 0) {
  345. wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
  346. continue;
  347. }
  348. for (ssid = group; ssid; ssid = ssid->pnext) {
  349. int check_ssid = 1;
  350. if (ssid->disabled) {
  351. wpa_printf(MSG_DEBUG, " skip - disabled");
  352. continue;
  353. }
  354. #ifdef CONFIG_WPS
  355. if (ssid->ssid_len == 0 &&
  356. wpas_wps_ssid_wildcard_ok(ssid, bss))
  357. check_ssid = 0;
  358. #endif /* CONFIG_WPS */
  359. if (check_ssid &&
  360. (ssid_len != ssid->ssid_len ||
  361. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  362. wpa_printf(MSG_DEBUG, " skip - "
  363. "SSID mismatch");
  364. continue;
  365. }
  366. if (ssid->bssid_set &&
  367. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  368. {
  369. wpa_printf(MSG_DEBUG, " skip - "
  370. "BSSID mismatch");
  371. continue;
  372. }
  373. if (!wpa_supplicant_ssid_bss_match(ssid, bss))
  374. continue;
  375. wpa_printf(MSG_DEBUG, " selected WPA AP "
  376. MACSTR " ssid='%s'",
  377. MAC2STR(bss->bssid),
  378. wpa_ssid_txt(ssid_, ssid_len));
  379. *selected_ssid = ssid;
  380. return bss;
  381. }
  382. }
  383. return NULL;
  384. }
  385. static struct wpa_scan_res *
  386. wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
  387. struct wpa_ssid *group,
  388. struct wpa_ssid **selected_ssid)
  389. {
  390. struct wpa_ssid *ssid;
  391. struct wpa_scan_res *bss;
  392. size_t i;
  393. struct wpa_blacklist *e;
  394. const u8 *ie;
  395. wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
  396. for (i = 0; i < wpa_s->scan_res->num; i++) {
  397. const u8 *ssid_;
  398. u8 wpa_ie_len, rsn_ie_len, ssid_len;
  399. bss = wpa_s->scan_res->res[i];
  400. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  401. ssid_ = ie ? ie + 2 : (u8 *) "";
  402. ssid_len = ie ? ie[1] : 0;
  403. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  404. wpa_ie_len = ie ? ie[1] : 0;
  405. ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  406. rsn_ie_len = ie ? ie[1] : 0;
  407. wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
  408. "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
  409. (int) i, MAC2STR(bss->bssid),
  410. wpa_ssid_txt(ssid_, ssid_len),
  411. wpa_ie_len, rsn_ie_len, bss->caps);
  412. e = wpa_blacklist_get(wpa_s, bss->bssid);
  413. if (e && e->count > 1) {
  414. wpa_printf(MSG_DEBUG, " skip - blacklisted");
  415. continue;
  416. }
  417. for (ssid = group; ssid; ssid = ssid->pnext) {
  418. int check_ssid = ssid->ssid_len != 0;
  419. if (ssid->disabled) {
  420. wpa_printf(MSG_DEBUG, " skip - disabled");
  421. continue;
  422. }
  423. #ifdef CONFIG_WPS
  424. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  425. /* Only allow wildcard SSID match if an AP
  426. * advertises active WPS operation that matches
  427. * with our mode. */
  428. check_ssid = 1;
  429. if (ssid->ssid_len == 0 &&
  430. wpas_wps_ssid_wildcard_ok(ssid, bss))
  431. check_ssid = 0;
  432. }
  433. #endif /* CONFIG_WPS */
  434. if (check_ssid &&
  435. (ssid_len != ssid->ssid_len ||
  436. os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
  437. wpa_printf(MSG_DEBUG, " skip - "
  438. "SSID mismatch");
  439. continue;
  440. }
  441. if (ssid->bssid_set &&
  442. os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
  443. {
  444. wpa_printf(MSG_DEBUG, " skip - "
  445. "BSSID mismatch");
  446. continue;
  447. }
  448. if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
  449. !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  450. !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
  451. {
  452. wpa_printf(MSG_DEBUG, " skip - "
  453. "non-WPA network not allowed");
  454. continue;
  455. }
  456. if ((ssid->key_mgmt &
  457. (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  458. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
  459. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  460. WPA_KEY_MGMT_PSK_SHA256)) &&
  461. (wpa_ie_len != 0 || rsn_ie_len != 0)) {
  462. wpa_printf(MSG_DEBUG, " skip - "
  463. "WPA network");
  464. continue;
  465. }
  466. if (!wpa_supplicant_match_privacy(bss, ssid)) {
  467. wpa_printf(MSG_DEBUG, " skip - "
  468. "privacy mismatch");
  469. continue;
  470. }
  471. if (bss->caps & IEEE80211_CAP_IBSS) {
  472. wpa_printf(MSG_DEBUG, " skip - "
  473. "IBSS (adhoc) network");
  474. continue;
  475. }
  476. wpa_printf(MSG_DEBUG, " selected non-WPA AP "
  477. MACSTR " ssid='%s'",
  478. MAC2STR(bss->bssid),
  479. wpa_ssid_txt(ssid_, ssid_len));
  480. *selected_ssid = ssid;
  481. return bss;
  482. }
  483. }
  484. return NULL;
  485. }
  486. static struct wpa_scan_res *
  487. wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
  488. struct wpa_ssid **selected_ssid)
  489. {
  490. struct wpa_scan_res *selected;
  491. wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
  492. group->priority);
  493. /* First, try to find WPA-enabled AP */
  494. selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
  495. if (selected)
  496. return selected;
  497. /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
  498. * allows this. */
  499. return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
  500. }
  501. static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
  502. {
  503. int prio, timeout;
  504. struct wpa_scan_res *selected = NULL;
  505. struct wpa_ssid *ssid = NULL;
  506. if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
  507. if (wpa_s->conf->ap_scan == 2)
  508. return;
  509. wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
  510. "scanning again");
  511. timeout = 1;
  512. goto req_scan;
  513. }
  514. /*
  515. * Don't post the results if this was the initial cached
  516. * and there were no results.
  517. */
  518. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
  519. wpa_s->scan_res->num == 0) {
  520. wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
  521. "empty - not posting");
  522. } else {
  523. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  524. wpa_supplicant_dbus_notify_scan_results(wpa_s);
  525. }
  526. if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
  527. return;
  528. while (selected == NULL) {
  529. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  530. selected = wpa_supplicant_select_bss(
  531. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  532. if (selected)
  533. break;
  534. }
  535. if (selected == NULL && wpa_s->blacklist) {
  536. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  537. "and try again");
  538. wpa_blacklist_clear(wpa_s);
  539. } else if (selected == NULL) {
  540. break;
  541. }
  542. }
  543. if (selected) {
  544. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  545. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  546. "PBC session overlap");
  547. timeout = 10;
  548. goto req_scan;
  549. }
  550. /* Do not trigger new association unless the BSSID has changed
  551. * or if reassociation is requested. If we are in process of
  552. * associating with the selected BSSID, do not trigger new
  553. * attempt. */
  554. if (wpa_s->reassociate ||
  555. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  556. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  557. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  558. ETH_ALEN) != 0))) {
  559. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  560. wpa_supplicant_req_scan(wpa_s, 10, 0);
  561. return;
  562. }
  563. wpa_supplicant_associate(wpa_s, selected, ssid);
  564. } else {
  565. wpa_printf(MSG_DEBUG, "Already associated with the "
  566. "selected AP.");
  567. }
  568. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  569. } else {
  570. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  571. timeout = 5;
  572. goto req_scan;
  573. }
  574. return;
  575. req_scan:
  576. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  577. /*
  578. * Quick recovery if the initial scan results were not
  579. * complete when fetched before the first scan request.
  580. */
  581. wpa_s->scan_res_tried++;
  582. timeout = 0;
  583. }
  584. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  585. }
  586. #endif /* CONFIG_NO_SCAN_PROCESSING */
  587. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  588. union wpa_event_data *data)
  589. {
  590. int l, len, found = 0, wpa_found, rsn_found;
  591. u8 *p;
  592. wpa_printf(MSG_DEBUG, "Association info event");
  593. if (data->assoc_info.req_ies)
  594. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  595. data->assoc_info.req_ies_len);
  596. if (data->assoc_info.resp_ies)
  597. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  598. data->assoc_info.resp_ies_len);
  599. if (data->assoc_info.beacon_ies)
  600. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  601. data->assoc_info.beacon_ies,
  602. data->assoc_info.beacon_ies_len);
  603. p = data->assoc_info.req_ies;
  604. l = data->assoc_info.req_ies_len;
  605. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  606. while (p && l >= 2) {
  607. len = p[1] + 2;
  608. if (len > l) {
  609. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  610. p, l);
  611. break;
  612. }
  613. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  614. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  615. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  616. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  617. break;
  618. found = 1;
  619. wpa_find_assoc_pmkid(wpa_s);
  620. break;
  621. }
  622. l -= len;
  623. p += len;
  624. }
  625. if (!found && data->assoc_info.req_ies)
  626. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  627. /* WPA/RSN IE from Beacon/ProbeResp */
  628. p = data->assoc_info.beacon_ies;
  629. l = data->assoc_info.beacon_ies_len;
  630. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  631. */
  632. wpa_found = rsn_found = 0;
  633. while (p && l >= 2) {
  634. len = p[1] + 2;
  635. if (len > l) {
  636. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  637. p, l);
  638. break;
  639. }
  640. if (!wpa_found &&
  641. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  642. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  643. wpa_found = 1;
  644. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  645. }
  646. if (!rsn_found &&
  647. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  648. rsn_found = 1;
  649. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  650. }
  651. l -= len;
  652. p += len;
  653. }
  654. if (!wpa_found && data->assoc_info.beacon_ies)
  655. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  656. if (!rsn_found && data->assoc_info.beacon_ies)
  657. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  658. if (wpa_found || rsn_found)
  659. wpa_s->ap_ies_from_associnfo = 1;
  660. }
  661. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  662. union wpa_event_data *data)
  663. {
  664. u8 bssid[ETH_ALEN];
  665. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  666. if (data)
  667. wpa_supplicant_event_associnfo(wpa_s, data);
  668. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  669. if (wpa_s->use_client_mlme)
  670. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  671. if (wpa_s->use_client_mlme ||
  672. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  673. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  674. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  675. MACSTR, MAC2STR(bssid));
  676. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  677. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  678. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  679. wpa_clear_keys(wpa_s, bssid);
  680. }
  681. if (wpa_supplicant_select_config(wpa_s) < 0) {
  682. wpa_supplicant_disassociate(
  683. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  684. return;
  685. }
  686. }
  687. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  688. if (wpa_s->current_ssid) {
  689. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  690. * initialized before association, but for other modes,
  691. * initialize PC/SC here, if the current configuration needs
  692. * smartcard or SIM/USIM. */
  693. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  694. }
  695. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  696. l2_packet_notify_auth_start(wpa_s->l2);
  697. /*
  698. * Set portEnabled first to FALSE in order to get EAP state machine out
  699. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  700. * state machine may transit to AUTHENTICATING state based on obsolete
  701. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  702. * AUTHENTICATED without ever giving chance to EAP state machine to
  703. * reset the state.
  704. */
  705. if (!ft_completed) {
  706. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  707. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  708. }
  709. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  710. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  711. /* 802.1X::portControl = Auto */
  712. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  713. wpa_s->eapol_received = 0;
  714. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  715. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  716. wpa_supplicant_cancel_auth_timeout(wpa_s);
  717. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  718. } else if (!ft_completed) {
  719. /* Timeout for receiving the first EAPOL packet */
  720. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  721. }
  722. wpa_supplicant_cancel_scan(wpa_s);
  723. if (wpa_s->driver_4way_handshake &&
  724. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  725. /*
  726. * We are done; the driver will take care of RSN 4-way
  727. * handshake.
  728. */
  729. wpa_supplicant_cancel_auth_timeout(wpa_s);
  730. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  731. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  732. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  733. }
  734. }
  735. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  736. {
  737. const u8 *bssid;
  738. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  739. /*
  740. * At least Host AP driver and a Prism3 card seemed to be
  741. * generating streams of disconnected events when configuring
  742. * IBSS for WPA-None. Ignore them for now.
  743. */
  744. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  745. "IBSS/WPA-None mode");
  746. return;
  747. }
  748. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  749. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  750. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  751. "pre-shared key may be incorrect");
  752. }
  753. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  754. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  755. bssid = wpa_s->bssid;
  756. if (is_zero_ether_addr(bssid))
  757. bssid = wpa_s->pending_bssid;
  758. wpa_blacklist_add(wpa_s, bssid);
  759. wpa_sm_notify_disassoc(wpa_s->wpa);
  760. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  761. "remove keys");
  762. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  763. wpa_s->keys_cleared = 0;
  764. wpa_clear_keys(wpa_s, wpa_s->bssid);
  765. }
  766. wpa_supplicant_mark_disassoc(wpa_s);
  767. }
  768. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  769. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  770. void *sock_ctx)
  771. {
  772. struct wpa_supplicant *wpa_s = eloop_ctx;
  773. if (!wpa_s->pending_mic_error_report)
  774. return;
  775. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  776. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  777. wpa_s->pending_mic_error_report = 0;
  778. }
  779. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  780. static void
  781. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  782. union wpa_event_data *data)
  783. {
  784. int pairwise;
  785. struct os_time t;
  786. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  787. pairwise = (data && data->michael_mic_failure.unicast);
  788. os_get_time(&t);
  789. if ((wpa_s->last_michael_mic_error &&
  790. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  791. wpa_s->pending_mic_error_report) {
  792. if (wpa_s->pending_mic_error_report) {
  793. /*
  794. * Send the pending MIC error report immediately since
  795. * we are going to start countermeasures and AP better
  796. * do the same.
  797. */
  798. wpa_sm_key_request(wpa_s->wpa, 1,
  799. wpa_s->pending_mic_error_pairwise);
  800. }
  801. /* Send the new MIC error report immediately since we are going
  802. * to start countermeasures and AP better do the same.
  803. */
  804. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  805. /* initialize countermeasures */
  806. wpa_s->countermeasures = 1;
  807. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  808. /*
  809. * Need to wait for completion of request frame. We do not get
  810. * any callback for the message completion, so just wait a
  811. * short while and hope for the best. */
  812. os_sleep(0, 10000);
  813. wpa_drv_set_countermeasures(wpa_s, 1);
  814. wpa_supplicant_deauthenticate(wpa_s,
  815. WLAN_REASON_MICHAEL_MIC_FAILURE);
  816. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  817. wpa_s, NULL);
  818. eloop_register_timeout(60, 0,
  819. wpa_supplicant_stop_countermeasures,
  820. wpa_s, NULL);
  821. /* TODO: mark the AP rejected for 60 second. STA is
  822. * allowed to associate with another AP.. */
  823. } else {
  824. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  825. if (wpa_s->mic_errors_seen) {
  826. /*
  827. * Reduce the effectiveness of Michael MIC error
  828. * reports as a means for attacking against TKIP if
  829. * more than one MIC failure is noticed with the same
  830. * PTK. We delay the transmission of the reports by a
  831. * random time between 0 and 60 seconds in order to
  832. * force the attacker wait 60 seconds before getting
  833. * the information on whether a frame resulted in a MIC
  834. * failure.
  835. */
  836. u8 rval[4];
  837. int sec;
  838. if (os_get_random(rval, sizeof(rval)) < 0)
  839. sec = os_random() % 60;
  840. else
  841. sec = WPA_GET_BE32(rval) % 60;
  842. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  843. "seconds", sec);
  844. wpa_s->pending_mic_error_report = 1;
  845. wpa_s->pending_mic_error_pairwise = pairwise;
  846. eloop_cancel_timeout(
  847. wpa_supplicant_delayed_mic_error_report,
  848. wpa_s, NULL);
  849. eloop_register_timeout(
  850. sec, os_random() % 1000000,
  851. wpa_supplicant_delayed_mic_error_report,
  852. wpa_s, NULL);
  853. } else {
  854. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  855. }
  856. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  857. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  858. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  859. }
  860. wpa_s->last_michael_mic_error = t.sec;
  861. wpa_s->mic_errors_seen++;
  862. }
  863. static void
  864. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  865. union wpa_event_data *data)
  866. {
  867. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  868. return;
  869. switch (data->interface_status.ievent) {
  870. case EVENT_INTERFACE_ADDED:
  871. if (!wpa_s->interface_removed)
  872. break;
  873. wpa_s->interface_removed = 0;
  874. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  875. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  876. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  877. "after interface was added.");
  878. }
  879. break;
  880. case EVENT_INTERFACE_REMOVED:
  881. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  882. wpa_s->interface_removed = 1;
  883. wpa_supplicant_mark_disassoc(wpa_s);
  884. l2_packet_deinit(wpa_s->l2);
  885. wpa_s->l2 = NULL;
  886. break;
  887. }
  888. }
  889. #ifdef CONFIG_PEERKEY
  890. static void
  891. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  892. union wpa_event_data *data)
  893. {
  894. if (data == NULL)
  895. return;
  896. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  897. }
  898. #endif /* CONFIG_PEERKEY */
  899. #ifdef CONFIG_IEEE80211R
  900. static void
  901. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  902. union wpa_event_data *data)
  903. {
  904. if (data == NULL)
  905. return;
  906. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  907. data->ft_ies.ies_len,
  908. data->ft_ies.ft_action,
  909. data->ft_ies.target_ap) < 0) {
  910. /* TODO: prevent MLME/driver from trying to associate? */
  911. }
  912. }
  913. #endif /* CONFIG_IEEE80211R */
  914. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  915. union wpa_event_data *data)
  916. {
  917. struct wpa_supplicant *wpa_s = ctx;
  918. switch (event) {
  919. case EVENT_ASSOC:
  920. wpa_supplicant_event_assoc(wpa_s, data);
  921. break;
  922. case EVENT_DISASSOC:
  923. wpa_supplicant_event_disassoc(wpa_s);
  924. break;
  925. case EVENT_MICHAEL_MIC_FAILURE:
  926. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  927. break;
  928. #ifndef CONFIG_NO_SCAN_PROCESSING
  929. case EVENT_SCAN_RESULTS:
  930. wpa_supplicant_event_scan_results(wpa_s);
  931. break;
  932. #endif /* CONFIG_NO_SCAN_PROCESSING */
  933. case EVENT_ASSOCINFO:
  934. wpa_supplicant_event_associnfo(wpa_s, data);
  935. break;
  936. case EVENT_INTERFACE_STATUS:
  937. wpa_supplicant_event_interface_status(wpa_s, data);
  938. break;
  939. case EVENT_PMKID_CANDIDATE:
  940. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  941. break;
  942. #ifdef CONFIG_PEERKEY
  943. case EVENT_STKSTART:
  944. wpa_supplicant_event_stkstart(wpa_s, data);
  945. break;
  946. #endif /* CONFIG_PEERKEY */
  947. #ifdef CONFIG_IEEE80211R
  948. case EVENT_FT_RESPONSE:
  949. wpa_supplicant_event_ft_response(wpa_s, data);
  950. break;
  951. #endif /* CONFIG_IEEE80211R */
  952. default:
  953. wpa_printf(MSG_INFO, "Unknown event %d", event);
  954. break;
  955. }
  956. }