2 * WPA Supplicant - Driver event processing
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "eapol_supp/eapol_supp_sm.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
25 #include "pcsc_funcs.h"
27 #include "pmksa_cache.h"
29 #include "eap_peer/eap.h"
30 #include "ctrl_iface_dbus.h"
31 #include "ieee802_11_defs.h"
32 #include "blacklist.h"
33 #include "wpas_glue.h"
34 #include "wps_supplicant.h"
39 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
41 struct wpa_ssid *ssid;
43 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
46 wpa_printf(MSG_DEBUG, "Select network based on association "
48 ssid = wpa_supplicant_get_ssid(wpa_s);
50 wpa_printf(MSG_INFO, "No network configuration found for the "
56 wpa_printf(MSG_DEBUG, "Selected network is disabled");
60 wpa_printf(MSG_DEBUG, "Network configuration found for the current "
62 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
63 WPA_KEY_MGMT_WPA_NONE |
64 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
65 WPA_KEY_MGMT_PSK_SHA256 |
66 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
68 size_t wpa_ie_len = sizeof(wpa_ie);
69 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
72 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
75 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
76 eapol_sm_invalidate_cached_session(wpa_s->eapol);
77 wpa_s->current_ssid = ssid;
78 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
79 wpa_supplicant_initiate_eapol(wpa_s);
85 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
88 struct wpa_supplicant *wpa_s = eloop_ctx;
90 if (wpa_s->countermeasures) {
91 wpa_s->countermeasures = 0;
92 wpa_drv_set_countermeasures(wpa_s, 0);
93 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
94 wpa_supplicant_req_scan(wpa_s, 0, 0);
99 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
101 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
102 os_memset(wpa_s->bssid, 0, ETH_ALEN);
103 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
104 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
105 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
106 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
107 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
108 wpa_s->ap_ies_from_associnfo = 0;
112 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
114 struct wpa_ie_data ie;
118 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
122 for (i = 0; i < ie.num_pmkid; i++) {
123 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
124 ie.pmkid + i * PMKID_LEN,
126 if (pmksa_set == 0) {
127 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
132 wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
133 "cache", pmksa_set == 0 ? "" : "not ");
137 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
138 union wpa_event_data *data)
141 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
144 wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
145 " index=%d preauth=%d",
146 MAC2STR(data->pmkid_candidate.bssid),
147 data->pmkid_candidate.index,
148 data->pmkid_candidate.preauth);
150 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
151 data->pmkid_candidate.index,
152 data->pmkid_candidate.preauth);
156 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
158 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
159 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
162 #ifdef IEEE8021X_EAPOL
163 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
164 wpa_s->current_ssid &&
165 !(wpa_s->current_ssid->eapol_flags &
166 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
167 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
168 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
169 * plaintext or static WEP keys). */
172 #endif /* IEEE8021X_EAPOL */
179 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
180 * @wpa_s: pointer to wpa_supplicant data
181 * @ssid: Configuration data for the network
182 * Returns: 0 on success, -1 on failure
184 * This function is called when starting authentication with a network that is
185 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
187 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
188 struct wpa_ssid *ssid)
190 #ifdef IEEE8021X_EAPOL
191 int aka = 0, sim = 0, type;
193 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
196 if (ssid->eap.eap_methods == NULL) {
200 struct eap_method_type *eap = ssid->eap.eap_methods;
201 while (eap->vendor != EAP_VENDOR_IETF ||
202 eap->method != EAP_TYPE_NONE) {
203 if (eap->vendor == EAP_VENDOR_IETF) {
204 if (eap->method == EAP_TYPE_SIM)
206 else if (eap->method == EAP_TYPE_AKA)
213 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
215 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
219 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
220 "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
224 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
225 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
227 type = SCARD_TRY_BOTH;
229 type = SCARD_USIM_ONLY;
231 type = SCARD_GSM_SIM_ONLY;
233 wpa_s->scard = scard_init(type);
234 if (wpa_s->scard == NULL) {
235 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
239 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
240 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
241 #endif /* IEEE8021X_EAPOL */
247 #ifndef CONFIG_NO_SCAN_PROCESSING
248 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
249 struct wpa_ssid *ssid)
253 if (ssid->mixed_cell)
257 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
259 #endif /* CONFIG_WPS */
261 for (i = 0; i < NUM_WEP_KEYS; i++) {
262 if (ssid->wep_key_len[i]) {
267 #ifdef IEEE8021X_EAPOL
268 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
269 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
270 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
272 #endif /* IEEE8021X_EAPOL */
274 if (bss->caps & IEEE80211_CAP_PRIVACY)
280 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
281 struct wpa_ssid *ssid,
282 struct wpa_scan_res *bss)
284 struct wpa_ie_data ie;
286 const u8 *rsn_ie, *wpa_ie;
289 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
293 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
294 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
297 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
298 wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
301 if (!(ie.proto & ssid->proto)) {
302 wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
307 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
308 wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
313 if (!(ie.group_cipher & ssid->group_cipher)) {
314 wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
319 if (!(ie.key_mgmt & ssid->key_mgmt)) {
320 wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
325 #ifdef CONFIG_IEEE80211W
326 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
327 ssid->ieee80211w == IEEE80211W_REQUIRED) {
328 wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
332 #endif /* CONFIG_IEEE80211W */
334 wpa_printf(MSG_DEBUG, " selected based on RSN IE");
338 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
339 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
342 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
343 wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
346 if (!(ie.proto & ssid->proto)) {
347 wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
352 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
353 wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
358 if (!(ie.group_cipher & ssid->group_cipher)) {
359 wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
364 if (!(ie.key_mgmt & ssid->key_mgmt)) {
365 wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
370 wpa_printf(MSG_DEBUG, " selected based on WPA IE");
374 if (proto_match == 0)
375 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
381 static struct wpa_scan_res *
382 wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
383 struct wpa_ssid *group,
384 struct wpa_ssid **selected_ssid)
386 struct wpa_ssid *ssid;
387 struct wpa_scan_res *bss;
389 struct wpa_blacklist *e;
392 wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
393 for (i = 0; i < wpa_s->scan_res->num; i++) {
395 u8 wpa_ie_len, rsn_ie_len, ssid_len;
396 bss = wpa_s->scan_res->res[i];
398 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
399 ssid_ = ie ? ie + 2 : (u8 *) "";
400 ssid_len = ie ? ie[1] : 0;
402 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
403 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;
408 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
409 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
410 (int) i, MAC2STR(bss->bssid),
411 wpa_ssid_txt(ssid_, ssid_len),
412 wpa_ie_len, rsn_ie_len, bss->caps);
414 e = wpa_blacklist_get(wpa_s, bss->bssid);
415 if (e && e->count > 1) {
416 wpa_printf(MSG_DEBUG, " skip - blacklisted");
420 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
421 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
425 for (ssid = group; ssid; ssid = ssid->pnext) {
428 if (ssid->disabled) {
429 wpa_printf(MSG_DEBUG, " skip - disabled");
434 if (ssid->ssid_len == 0 &&
435 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
437 #endif /* CONFIG_WPS */
440 (ssid_len != ssid->ssid_len ||
441 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
442 wpa_printf(MSG_DEBUG, " skip - "
447 if (ssid->bssid_set &&
448 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
450 wpa_printf(MSG_DEBUG, " skip - "
455 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
458 wpa_printf(MSG_DEBUG, " selected WPA AP "
461 wpa_ssid_txt(ssid_, ssid_len));
462 *selected_ssid = ssid;
471 static struct wpa_scan_res *
472 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
473 struct wpa_ssid *group,
474 struct wpa_ssid **selected_ssid)
476 struct wpa_ssid *ssid;
477 struct wpa_scan_res *bss;
479 struct wpa_blacklist *e;
482 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
483 for (i = 0; i < wpa_s->scan_res->num; i++) {
485 u8 wpa_ie_len, rsn_ie_len, ssid_len;
486 bss = wpa_s->scan_res->res[i];
488 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
489 ssid_ = ie ? ie + 2 : (u8 *) "";
490 ssid_len = ie ? ie[1] : 0;
492 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
493 wpa_ie_len = ie ? ie[1] : 0;
495 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
496 rsn_ie_len = ie ? ie[1] : 0;
498 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
499 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
500 (int) i, MAC2STR(bss->bssid),
501 wpa_ssid_txt(ssid_, ssid_len),
502 wpa_ie_len, rsn_ie_len, bss->caps);
504 e = wpa_blacklist_get(wpa_s, bss->bssid);
505 if (e && e->count > 1) {
506 wpa_printf(MSG_DEBUG, " skip - blacklisted");
510 for (ssid = group; ssid; ssid = ssid->pnext) {
511 int check_ssid = ssid->ssid_len != 0;
513 if (ssid->disabled) {
514 wpa_printf(MSG_DEBUG, " skip - disabled");
519 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
520 /* Only allow wildcard SSID match if an AP
521 * advertises active WPS operation that matches
524 if (ssid->ssid_len == 0 &&
525 wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
529 #endif /* CONFIG_WPS */
532 (ssid_len != ssid->ssid_len ||
533 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
534 wpa_printf(MSG_DEBUG, " skip - "
539 if (ssid->bssid_set &&
540 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
542 wpa_printf(MSG_DEBUG, " skip - "
547 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
548 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
549 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
551 wpa_printf(MSG_DEBUG, " skip - "
552 "non-WPA network not allowed");
556 if ((ssid->key_mgmt &
557 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
558 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
559 WPA_KEY_MGMT_IEEE8021X_SHA256 |
560 WPA_KEY_MGMT_PSK_SHA256)) &&
561 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
562 wpa_printf(MSG_DEBUG, " skip - "
567 if (!wpa_supplicant_match_privacy(bss, ssid)) {
568 wpa_printf(MSG_DEBUG, " skip - "
573 if (bss->caps & IEEE80211_CAP_IBSS) {
574 wpa_printf(MSG_DEBUG, " skip - "
575 "IBSS (adhoc) network");
579 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
582 wpa_ssid_txt(ssid_, ssid_len));
583 *selected_ssid = ssid;
592 static struct wpa_scan_res *
593 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
594 struct wpa_ssid **selected_ssid)
596 struct wpa_scan_res *selected;
598 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
601 /* First, try to find WPA-enabled AP */
602 selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
606 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
608 return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
612 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
615 struct wpa_scan_res *selected = NULL;
616 struct wpa_ssid *ssid = NULL;
618 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
619 if (wpa_s->conf->ap_scan == 2)
621 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
628 * Don't post the results if this was the initial cached
629 * and there were no results.
631 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
632 wpa_s->scan_res->num == 0) {
633 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
634 "empty - not posting");
636 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
637 wpa_supplicant_dbus_notify_scan_results(wpa_s);
638 wpas_wps_notify_scan_results(wpa_s);
641 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)) ||
645 while (selected == NULL) {
646 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
647 selected = wpa_supplicant_select_bss(
648 wpa_s, wpa_s->conf->pssid[prio], &ssid);
653 if (selected == NULL && wpa_s->blacklist) {
654 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
656 wpa_blacklist_clear(wpa_s);
657 wpa_s->blacklist_cleared++;
658 } else if (selected == NULL) {
664 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
665 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
666 "PBC session overlap");
671 /* Do not trigger new association unless the BSSID has changed
672 * or if reassociation is requested. If we are in process of
673 * associating with the selected BSSID, do not trigger new
675 if (wpa_s->reassociate ||
676 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
677 (wpa_s->wpa_state != WPA_ASSOCIATING ||
678 os_memcmp(selected->bssid, wpa_s->pending_bssid,
680 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
681 wpa_supplicant_req_scan(wpa_s, 10, 0);
684 wpa_supplicant_associate(wpa_s, selected, ssid);
686 wpa_printf(MSG_DEBUG, "Already associated with the "
689 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
691 wpa_printf(MSG_DEBUG, "No suitable AP found.");
699 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
701 * Quick recovery if the initial scan results were not
702 * complete when fetched before the first scan request.
704 wpa_s->scan_res_tried++;
707 wpa_supplicant_req_scan(wpa_s, timeout, 0);
709 #endif /* CONFIG_NO_SCAN_PROCESSING */
712 #ifdef CONFIG_IEEE80211R
713 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
714 const u8 *ftie, const u8 *mdie)
716 const u8 *mobility_domain = NULL;
717 const u8 *r0kh_id = NULL;
718 size_t r0kh_id_len = 0;
719 const u8 *r1kh_id = NULL;
720 struct rsn_ftie *hdr;
723 if (mdie == NULL || ftie == NULL)
726 if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
727 mobility_domain = mdie + 2;
729 wpa_s->sme.ft_used = 1;
730 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
731 #endif /* CONFIG_SME */
733 if (ftie[1] >= sizeof(struct rsn_ftie)) {
734 end = ftie + 2 + ftie[1];
735 hdr = (struct rsn_ftie *) (ftie + 2);
736 pos = (const u8 *) (hdr + 1);
737 while (pos + 1 < end) {
738 if (pos + 2 + pos[1] > end)
740 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
741 pos[1] == FT_R1KH_ID_LEN)
743 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
744 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
746 r0kh_id_len = pos[1];
751 wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
752 r0kh_id_len, r1kh_id);
754 #endif /* CONFIG_IEEE80211R */
756 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
757 union wpa_event_data *data)
759 int l, len, found = 0, wpa_found, rsn_found;
761 #ifdef CONFIG_IEEE80211R
762 const u8 *mdie = NULL, *ftie = NULL;
763 #endif /* CONFIG_IEEE80211R */
765 wpa_printf(MSG_DEBUG, "Association info event");
766 if (data->assoc_info.req_ies)
767 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
768 data->assoc_info.req_ies_len);
769 if (data->assoc_info.resp_ies)
770 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
771 data->assoc_info.resp_ies_len);
772 if (data->assoc_info.beacon_ies)
773 wpa_hexdump(MSG_DEBUG, "beacon_ies",
774 data->assoc_info.beacon_ies,
775 data->assoc_info.beacon_ies_len);
777 p = data->assoc_info.req_ies;
778 l = data->assoc_info.req_ies_len;
780 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
781 while (p && l >= 2) {
784 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
788 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
789 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
790 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
791 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
794 wpa_find_assoc_pmkid(wpa_s);
800 if (!found && data->assoc_info.req_ies)
801 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
803 #ifdef CONFIG_IEEE80211R
804 p = data->assoc_info.resp_ies;
805 l = data->assoc_info.resp_ies_len;
807 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
808 while (p && l >= 2) {
811 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
815 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
817 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
823 wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
824 #endif /* CONFIG_IEEE80211R */
826 /* WPA/RSN IE from Beacon/ProbeResp */
827 p = data->assoc_info.beacon_ies;
828 l = data->assoc_info.beacon_ies_len;
830 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
832 wpa_found = rsn_found = 0;
833 while (p && l >= 2) {
836 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
841 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
842 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
844 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
848 p[0] == WLAN_EID_RSN && p[1] >= 2) {
850 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
857 if (!wpa_found && data->assoc_info.beacon_ies)
858 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
859 if (!rsn_found && data->assoc_info.beacon_ies)
860 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
861 if (wpa_found || rsn_found)
862 wpa_s->ap_ies_from_associnfo = 1;
866 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
867 union wpa_event_data *data)
870 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
873 wpa_supplicant_event_associnfo(wpa_s, data);
875 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
876 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
877 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
878 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
879 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
880 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
881 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
882 MACSTR, MAC2STR(bssid));
883 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
884 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
885 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
886 wpa_clear_keys(wpa_s, bssid);
888 if (wpa_supplicant_select_config(wpa_s) < 0) {
889 wpa_supplicant_disassociate(
890 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
895 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
896 if (wpa_s->current_ssid) {
897 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
898 * initialized before association, but for other modes,
899 * initialize PC/SC here, if the current configuration needs
900 * smartcard or SIM/USIM. */
901 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
903 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
904 l2_packet_notify_auth_start(wpa_s->l2);
907 * Set portEnabled first to FALSE in order to get EAP state machine out
908 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
909 * state machine may transit to AUTHENTICATING state based on obsolete
910 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
911 * AUTHENTICATED without ever giving chance to EAP state machine to
915 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
916 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
918 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
919 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
920 /* 802.1X::portControl = Auto */
921 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
922 wpa_s->eapol_received = 0;
923 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
924 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
925 wpa_supplicant_cancel_auth_timeout(wpa_s);
926 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
927 } else if (!ft_completed) {
928 /* Timeout for receiving the first EAPOL packet */
929 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
931 wpa_supplicant_cancel_scan(wpa_s);
933 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
934 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
936 * We are done; the driver will take care of RSN 4-way
939 wpa_supplicant_cancel_auth_timeout(wpa_s);
940 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
941 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
942 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
947 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
951 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
953 * At least Host AP driver and a Prism3 card seemed to be
954 * generating streams of disconnected events when configuring
955 * IBSS for WPA-None. Ignore them for now.
957 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
958 "IBSS/WPA-None mode");
962 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
963 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
964 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
965 "pre-shared key may be incorrect");
967 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
968 wpa_supplicant_req_scan(wpa_s, 0, 100000);
969 bssid = wpa_s->bssid;
970 if (is_zero_ether_addr(bssid))
971 bssid = wpa_s->pending_bssid;
972 wpa_blacklist_add(wpa_s, bssid);
973 wpa_sm_notify_disassoc(wpa_s->wpa);
974 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
976 if (wpa_supplicant_dynamic_keys(wpa_s)) {
977 wpa_s->keys_cleared = 0;
978 wpa_clear_keys(wpa_s, wpa_s->bssid);
980 wpa_supplicant_mark_disassoc(wpa_s);
984 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
985 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
988 struct wpa_supplicant *wpa_s = eloop_ctx;
990 if (!wpa_s->pending_mic_error_report)
993 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
994 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
995 wpa_s->pending_mic_error_report = 0;
997 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1001 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1002 union wpa_event_data *data)
1007 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1008 pairwise = (data && data->michael_mic_failure.unicast);
1010 if ((wpa_s->last_michael_mic_error &&
1011 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1012 wpa_s->pending_mic_error_report) {
1013 if (wpa_s->pending_mic_error_report) {
1015 * Send the pending MIC error report immediately since
1016 * we are going to start countermeasures and AP better
1019 wpa_sm_key_request(wpa_s->wpa, 1,
1020 wpa_s->pending_mic_error_pairwise);
1023 /* Send the new MIC error report immediately since we are going
1024 * to start countermeasures and AP better do the same.
1026 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1028 /* initialize countermeasures */
1029 wpa_s->countermeasures = 1;
1030 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1033 * Need to wait for completion of request frame. We do not get
1034 * any callback for the message completion, so just wait a
1035 * short while and hope for the best. */
1038 wpa_drv_set_countermeasures(wpa_s, 1);
1039 wpa_supplicant_deauthenticate(wpa_s,
1040 WLAN_REASON_MICHAEL_MIC_FAILURE);
1041 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1043 eloop_register_timeout(60, 0,
1044 wpa_supplicant_stop_countermeasures,
1046 /* TODO: mark the AP rejected for 60 second. STA is
1047 * allowed to associate with another AP.. */
1049 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1050 if (wpa_s->mic_errors_seen) {
1052 * Reduce the effectiveness of Michael MIC error
1053 * reports as a means for attacking against TKIP if
1054 * more than one MIC failure is noticed with the same
1055 * PTK. We delay the transmission of the reports by a
1056 * random time between 0 and 60 seconds in order to
1057 * force the attacker wait 60 seconds before getting
1058 * the information on whether a frame resulted in a MIC
1064 if (os_get_random(rval, sizeof(rval)) < 0)
1065 sec = os_random() % 60;
1067 sec = WPA_GET_BE32(rval) % 60;
1068 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1070 wpa_s->pending_mic_error_report = 1;
1071 wpa_s->pending_mic_error_pairwise = pairwise;
1072 eloop_cancel_timeout(
1073 wpa_supplicant_delayed_mic_error_report,
1075 eloop_register_timeout(
1076 sec, os_random() % 1000000,
1077 wpa_supplicant_delayed_mic_error_report,
1080 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1082 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1083 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1084 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1086 wpa_s->last_michael_mic_error = t.sec;
1087 wpa_s->mic_errors_seen++;
1091 #ifdef CONFIG_TERMINATE_ONLASTIF
1092 static int any_interfaces(struct wpa_supplicant *head)
1094 struct wpa_supplicant *wpa_s;
1096 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1097 if (!wpa_s->interface_removed)
1101 #endif /* CONFIG_TERMINATE_ONLASTIF */
1105 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1106 union wpa_event_data *data)
1108 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1111 switch (data->interface_status.ievent) {
1112 case EVENT_INTERFACE_ADDED:
1113 if (!wpa_s->interface_removed)
1115 wpa_s->interface_removed = 0;
1116 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1117 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1118 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1119 "after interface was added.");
1122 case EVENT_INTERFACE_REMOVED:
1123 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1124 wpa_s->interface_removed = 1;
1125 wpa_supplicant_mark_disassoc(wpa_s);
1126 l2_packet_deinit(wpa_s->l2);
1128 #ifdef CONFIG_TERMINATE_ONLASTIF
1129 /* check if last interface */
1130 if (!any_interfaces(wpa_s->global->ifaces))
1132 #endif /* CONFIG_TERMINATE_ONLASTIF */
1138 #ifdef CONFIG_PEERKEY
1140 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1141 union wpa_event_data *data)
1145 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1147 #endif /* CONFIG_PEERKEY */
1150 #ifdef CONFIG_IEEE80211R
1152 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1153 union wpa_event_data *data)
1158 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1159 data->ft_ies.ies_len,
1160 data->ft_ies.ft_action,
1161 data->ft_ies.target_ap,
1162 data->ft_ies.ric_ies,
1163 data->ft_ies.ric_ies_len) < 0) {
1164 /* TODO: prevent MLME/driver from trying to associate? */
1167 #endif /* CONFIG_IEEE80211R */
1170 #ifdef CONFIG_IBSS_RSN
1171 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1172 union wpa_event_data *data)
1176 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1178 #endif /* CONFIG_IBSS_RSN */
1181 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1182 union wpa_event_data *data)
1184 struct wpa_supplicant *wpa_s = ctx;
1188 sme_event_auth(wpa_s, data);
1191 wpa_supplicant_event_assoc(wpa_s, data);
1194 case EVENT_DISASSOC:
1195 wpa_supplicant_event_disassoc(wpa_s);
1197 case EVENT_MICHAEL_MIC_FAILURE:
1198 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1200 #ifndef CONFIG_NO_SCAN_PROCESSING
1201 case EVENT_SCAN_RESULTS:
1202 wpa_supplicant_event_scan_results(wpa_s);
1204 #endif /* CONFIG_NO_SCAN_PROCESSING */
1205 case EVENT_ASSOCINFO:
1206 wpa_supplicant_event_associnfo(wpa_s, data);
1208 case EVENT_INTERFACE_STATUS:
1209 wpa_supplicant_event_interface_status(wpa_s, data);
1211 case EVENT_PMKID_CANDIDATE:
1212 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1214 #ifdef CONFIG_PEERKEY
1215 case EVENT_STKSTART:
1216 wpa_supplicant_event_stkstart(wpa_s, data);
1218 #endif /* CONFIG_PEERKEY */
1219 #ifdef CONFIG_IEEE80211R
1220 case EVENT_FT_RESPONSE:
1221 wpa_supplicant_event_ft_response(wpa_s, data);
1223 #endif /* CONFIG_IEEE80211R */
1224 #ifdef CONFIG_IBSS_RSN
1225 case EVENT_IBSS_RSN_START:
1226 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1228 #endif /* CONFIG_IBSS_RSN */
1229 case EVENT_ASSOC_REJECT:
1230 sme_event_assoc_reject(wpa_s, data);
1232 case EVENT_AUTH_TIMED_OUT:
1233 sme_event_auth_timed_out(wpa_s, data);
1235 case EVENT_ASSOC_TIMED_OUT:
1236 sme_event_assoc_timed_out(wpa_s, data);
1239 wpa_printf(MSG_INFO, "Unknown event %d", event);