events.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. wpas_wps_notify_scan_results(wpa_s);
  526. }
  527. if (wpa_s->conf->ap_scan == 2 || wpa_s->disconnected)
  528. return;
  529. while (selected == NULL) {
  530. for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
  531. selected = wpa_supplicant_select_bss(
  532. wpa_s, wpa_s->conf->pssid[prio], &ssid);
  533. if (selected)
  534. break;
  535. }
  536. if (selected == NULL && wpa_s->blacklist) {
  537. wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
  538. "and try again");
  539. wpa_blacklist_clear(wpa_s);
  540. } else if (selected == NULL) {
  541. break;
  542. }
  543. }
  544. if (selected) {
  545. if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
  546. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
  547. "PBC session overlap");
  548. timeout = 10;
  549. goto req_scan;
  550. }
  551. /* Do not trigger new association unless the BSSID has changed
  552. * or if reassociation is requested. If we are in process of
  553. * associating with the selected BSSID, do not trigger new
  554. * attempt. */
  555. if (wpa_s->reassociate ||
  556. (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
  557. (wpa_s->wpa_state != WPA_ASSOCIATING ||
  558. os_memcmp(selected->bssid, wpa_s->pending_bssid,
  559. ETH_ALEN) != 0))) {
  560. if (wpa_supplicant_scard_init(wpa_s, ssid)) {
  561. wpa_supplicant_req_scan(wpa_s, 10, 0);
  562. return;
  563. }
  564. wpa_supplicant_associate(wpa_s, selected, ssid);
  565. } else {
  566. wpa_printf(MSG_DEBUG, "Already associated with the "
  567. "selected AP.");
  568. }
  569. rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
  570. } else {
  571. wpa_printf(MSG_DEBUG, "No suitable AP found.");
  572. timeout = 5;
  573. goto req_scan;
  574. }
  575. return;
  576. req_scan:
  577. if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
  578. /*
  579. * Quick recovery if the initial scan results were not
  580. * complete when fetched before the first scan request.
  581. */
  582. wpa_s->scan_res_tried++;
  583. timeout = 0;
  584. }
  585. wpa_supplicant_req_scan(wpa_s, timeout, 0);
  586. }
  587. #endif /* CONFIG_NO_SCAN_PROCESSING */
  588. static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
  589. union wpa_event_data *data)
  590. {
  591. int l, len, found = 0, wpa_found, rsn_found;
  592. u8 *p;
  593. wpa_printf(MSG_DEBUG, "Association info event");
  594. if (data->assoc_info.req_ies)
  595. wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
  596. data->assoc_info.req_ies_len);
  597. if (data->assoc_info.resp_ies)
  598. wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
  599. data->assoc_info.resp_ies_len);
  600. if (data->assoc_info.beacon_ies)
  601. wpa_hexdump(MSG_DEBUG, "beacon_ies",
  602. data->assoc_info.beacon_ies,
  603. data->assoc_info.beacon_ies_len);
  604. p = data->assoc_info.req_ies;
  605. l = data->assoc_info.req_ies_len;
  606. /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
  607. while (p && l >= 2) {
  608. len = p[1] + 2;
  609. if (len > l) {
  610. wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
  611. p, l);
  612. break;
  613. }
  614. if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  615. (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
  616. (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
  617. if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
  618. break;
  619. found = 1;
  620. wpa_find_assoc_pmkid(wpa_s);
  621. break;
  622. }
  623. l -= len;
  624. p += len;
  625. }
  626. if (!found && data->assoc_info.req_ies)
  627. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  628. /* WPA/RSN IE from Beacon/ProbeResp */
  629. p = data->assoc_info.beacon_ies;
  630. l = data->assoc_info.beacon_ies_len;
  631. /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
  632. */
  633. wpa_found = rsn_found = 0;
  634. while (p && l >= 2) {
  635. len = p[1] + 2;
  636. if (len > l) {
  637. wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
  638. p, l);
  639. break;
  640. }
  641. if (!wpa_found &&
  642. p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
  643. os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
  644. wpa_found = 1;
  645. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
  646. }
  647. if (!rsn_found &&
  648. p[0] == WLAN_EID_RSN && p[1] >= 2) {
  649. rsn_found = 1;
  650. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
  651. }
  652. l -= len;
  653. p += len;
  654. }
  655. if (!wpa_found && data->assoc_info.beacon_ies)
  656. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  657. if (!rsn_found && data->assoc_info.beacon_ies)
  658. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  659. if (wpa_found || rsn_found)
  660. wpa_s->ap_ies_from_associnfo = 1;
  661. }
  662. static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
  663. union wpa_event_data *data)
  664. {
  665. u8 bssid[ETH_ALEN];
  666. int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
  667. if (data)
  668. wpa_supplicant_event_associnfo(wpa_s, data);
  669. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
  670. if (wpa_s->use_client_mlme)
  671. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  672. if (wpa_s->use_client_mlme ||
  673. (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
  674. os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
  675. wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
  676. MACSTR, MAC2STR(bssid));
  677. os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
  678. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  679. if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
  680. wpa_clear_keys(wpa_s, bssid);
  681. }
  682. if (wpa_supplicant_select_config(wpa_s) < 0) {
  683. wpa_supplicant_disassociate(
  684. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  685. return;
  686. }
  687. }
  688. wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
  689. if (wpa_s->current_ssid) {
  690. /* When using scanning (ap_scan=1), SIM PC/SC interface can be
  691. * initialized before association, but for other modes,
  692. * initialize PC/SC here, if the current configuration needs
  693. * smartcard or SIM/USIM. */
  694. wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
  695. }
  696. wpa_sm_notify_assoc(wpa_s->wpa, bssid);
  697. l2_packet_notify_auth_start(wpa_s->l2);
  698. /*
  699. * Set portEnabled first to FALSE in order to get EAP state machine out
  700. * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
  701. * state machine may transit to AUTHENTICATING state based on obsolete
  702. * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
  703. * AUTHENTICATED without ever giving chance to EAP state machine to
  704. * reset the state.
  705. */
  706. if (!ft_completed) {
  707. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  708. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  709. }
  710. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
  711. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  712. /* 802.1X::portControl = Auto */
  713. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  714. wpa_s->eapol_received = 0;
  715. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  716. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  717. wpa_supplicant_cancel_auth_timeout(wpa_s);
  718. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  719. } else if (!ft_completed) {
  720. /* Timeout for receiving the first EAPOL packet */
  721. wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
  722. }
  723. wpa_supplicant_cancel_scan(wpa_s);
  724. if (wpa_s->driver_4way_handshake &&
  725. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  726. /*
  727. * We are done; the driver will take care of RSN 4-way
  728. * handshake.
  729. */
  730. wpa_supplicant_cancel_auth_timeout(wpa_s);
  731. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  732. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  733. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  734. }
  735. }
  736. static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
  737. {
  738. const u8 *bssid;
  739. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  740. /*
  741. * At least Host AP driver and a Prism3 card seemed to be
  742. * generating streams of disconnected events when configuring
  743. * IBSS for WPA-None. Ignore them for now.
  744. */
  745. wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
  746. "IBSS/WPA-None mode");
  747. return;
  748. }
  749. if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
  750. wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  751. wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
  752. "pre-shared key may be incorrect");
  753. }
  754. if (wpa_s->wpa_state >= WPA_ASSOCIATED)
  755. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  756. bssid = wpa_s->bssid;
  757. if (is_zero_ether_addr(bssid))
  758. bssid = wpa_s->pending_bssid;
  759. wpa_blacklist_add(wpa_s, bssid);
  760. wpa_sm_notify_disassoc(wpa_s->wpa);
  761. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
  762. "remove keys");
  763. if (wpa_supplicant_dynamic_keys(wpa_s)) {
  764. wpa_s->keys_cleared = 0;
  765. wpa_clear_keys(wpa_s, wpa_s->bssid);
  766. }
  767. wpa_supplicant_mark_disassoc(wpa_s);
  768. }
  769. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  770. static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
  771. void *sock_ctx)
  772. {
  773. struct wpa_supplicant *wpa_s = eloop_ctx;
  774. if (!wpa_s->pending_mic_error_report)
  775. return;
  776. wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
  777. wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
  778. wpa_s->pending_mic_error_report = 0;
  779. }
  780. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  781. static void
  782. wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
  783. union wpa_event_data *data)
  784. {
  785. int pairwise;
  786. struct os_time t;
  787. wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
  788. pairwise = (data && data->michael_mic_failure.unicast);
  789. os_get_time(&t);
  790. if ((wpa_s->last_michael_mic_error &&
  791. t.sec - wpa_s->last_michael_mic_error <= 60) ||
  792. wpa_s->pending_mic_error_report) {
  793. if (wpa_s->pending_mic_error_report) {
  794. /*
  795. * Send the pending MIC error report immediately since
  796. * we are going to start countermeasures and AP better
  797. * do the same.
  798. */
  799. wpa_sm_key_request(wpa_s->wpa, 1,
  800. wpa_s->pending_mic_error_pairwise);
  801. }
  802. /* Send the new MIC error report immediately since we are going
  803. * to start countermeasures and AP better do the same.
  804. */
  805. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  806. /* initialize countermeasures */
  807. wpa_s->countermeasures = 1;
  808. wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
  809. /*
  810. * Need to wait for completion of request frame. We do not get
  811. * any callback for the message completion, so just wait a
  812. * short while and hope for the best. */
  813. os_sleep(0, 10000);
  814. wpa_drv_set_countermeasures(wpa_s, 1);
  815. wpa_supplicant_deauthenticate(wpa_s,
  816. WLAN_REASON_MICHAEL_MIC_FAILURE);
  817. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
  818. wpa_s, NULL);
  819. eloop_register_timeout(60, 0,
  820. wpa_supplicant_stop_countermeasures,
  821. wpa_s, NULL);
  822. /* TODO: mark the AP rejected for 60 second. STA is
  823. * allowed to associate with another AP.. */
  824. } else {
  825. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  826. if (wpa_s->mic_errors_seen) {
  827. /*
  828. * Reduce the effectiveness of Michael MIC error
  829. * reports as a means for attacking against TKIP if
  830. * more than one MIC failure is noticed with the same
  831. * PTK. We delay the transmission of the reports by a
  832. * random time between 0 and 60 seconds in order to
  833. * force the attacker wait 60 seconds before getting
  834. * the information on whether a frame resulted in a MIC
  835. * failure.
  836. */
  837. u8 rval[4];
  838. int sec;
  839. if (os_get_random(rval, sizeof(rval)) < 0)
  840. sec = os_random() % 60;
  841. else
  842. sec = WPA_GET_BE32(rval) % 60;
  843. wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
  844. "seconds", sec);
  845. wpa_s->pending_mic_error_report = 1;
  846. wpa_s->pending_mic_error_pairwise = pairwise;
  847. eloop_cancel_timeout(
  848. wpa_supplicant_delayed_mic_error_report,
  849. wpa_s, NULL);
  850. eloop_register_timeout(
  851. sec, os_random() % 1000000,
  852. wpa_supplicant_delayed_mic_error_report,
  853. wpa_s, NULL);
  854. } else {
  855. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  856. }
  857. #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  858. wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
  859. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  860. }
  861. wpa_s->last_michael_mic_error = t.sec;
  862. wpa_s->mic_errors_seen++;
  863. }
  864. static void
  865. wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
  866. union wpa_event_data *data)
  867. {
  868. if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
  869. return;
  870. switch (data->interface_status.ievent) {
  871. case EVENT_INTERFACE_ADDED:
  872. if (!wpa_s->interface_removed)
  873. break;
  874. wpa_s->interface_removed = 0;
  875. wpa_printf(MSG_DEBUG, "Configured interface was added.");
  876. if (wpa_supplicant_driver_init(wpa_s) < 0) {
  877. wpa_printf(MSG_INFO, "Failed to initialize the driver "
  878. "after interface was added.");
  879. }
  880. break;
  881. case EVENT_INTERFACE_REMOVED:
  882. wpa_printf(MSG_DEBUG, "Configured interface was removed.");
  883. wpa_s->interface_removed = 1;
  884. wpa_supplicant_mark_disassoc(wpa_s);
  885. l2_packet_deinit(wpa_s->l2);
  886. wpa_s->l2 = NULL;
  887. break;
  888. }
  889. }
  890. #ifdef CONFIG_PEERKEY
  891. static void
  892. wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
  893. union wpa_event_data *data)
  894. {
  895. if (data == NULL)
  896. return;
  897. wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
  898. }
  899. #endif /* CONFIG_PEERKEY */
  900. #ifdef CONFIG_IEEE80211R
  901. static void
  902. wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
  903. union wpa_event_data *data)
  904. {
  905. if (data == NULL)
  906. return;
  907. if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
  908. data->ft_ies.ies_len,
  909. data->ft_ies.ft_action,
  910. data->ft_ies.target_ap) < 0) {
  911. /* TODO: prevent MLME/driver from trying to associate? */
  912. }
  913. }
  914. #endif /* CONFIG_IEEE80211R */
  915. void wpa_supplicant_event(void *ctx, wpa_event_type event,
  916. union wpa_event_data *data)
  917. {
  918. struct wpa_supplicant *wpa_s = ctx;
  919. switch (event) {
  920. case EVENT_ASSOC:
  921. wpa_supplicant_event_assoc(wpa_s, data);
  922. break;
  923. case EVENT_DISASSOC:
  924. wpa_supplicant_event_disassoc(wpa_s);
  925. break;
  926. case EVENT_MICHAEL_MIC_FAILURE:
  927. wpa_supplicant_event_michael_mic_failure(wpa_s, data);
  928. break;
  929. #ifndef CONFIG_NO_SCAN_PROCESSING
  930. case EVENT_SCAN_RESULTS:
  931. wpa_supplicant_event_scan_results(wpa_s);
  932. break;
  933. #endif /* CONFIG_NO_SCAN_PROCESSING */
  934. case EVENT_ASSOCINFO:
  935. wpa_supplicant_event_associnfo(wpa_s, data);
  936. break;
  937. case EVENT_INTERFACE_STATUS:
  938. wpa_supplicant_event_interface_status(wpa_s, data);
  939. break;
  940. case EVENT_PMKID_CANDIDATE:
  941. wpa_supplicant_event_pmkid_candidate(wpa_s, data);
  942. break;
  943. #ifdef CONFIG_PEERKEY
  944. case EVENT_STKSTART:
  945. wpa_supplicant_event_stkstart(wpa_s, data);
  946. break;
  947. #endif /* CONFIG_PEERKEY */
  948. #ifdef CONFIG_IEEE80211R
  949. case EVENT_FT_RESPONSE:
  950. wpa_supplicant_event_ft_response(wpa_s, data);
  951. break;
  952. #endif /* CONFIG_IEEE80211R */
  953. default:
  954. wpa_printf(MSG_INFO, "Unknown event %d", event);
  955. break;
  956. }
  957. }