events.c 29 KB

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