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 wpa_supplicant_notify_scanning(wpa_s, 0);
620 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
621 if (wpa_s->conf->ap_scan == 2)
623 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
630 * Don't post the results if this was the initial cached
631 * and there were no results.
633 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
634 wpa_s->scan_res->num == 0) {
635 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
636 "empty - not posting");
638 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
639 wpa_supplicant_dbus_notify_scan_results(wpa_s);
640 wpas_wps_notify_scan_results(wpa_s);
643 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)) ||
647 while (selected == NULL) {
648 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
649 selected = wpa_supplicant_select_bss(
650 wpa_s, wpa_s->conf->pssid[prio], &ssid);
655 if (selected == NULL && wpa_s->blacklist) {
656 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
658 wpa_blacklist_clear(wpa_s);
659 wpa_s->blacklist_cleared++;
660 } else if (selected == NULL) {
666 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
667 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
668 "PBC session overlap");
673 /* Do not trigger new association unless the BSSID has changed
674 * or if reassociation is requested. If we are in process of
675 * associating with the selected BSSID, do not trigger new
677 if (wpa_s->reassociate ||
678 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
679 (wpa_s->wpa_state != WPA_ASSOCIATING ||
680 os_memcmp(selected->bssid, wpa_s->pending_bssid,
682 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
683 wpa_supplicant_req_scan(wpa_s, 10, 0);
686 wpa_supplicant_associate(wpa_s, selected, ssid);
688 wpa_printf(MSG_DEBUG, "Already associated with the "
691 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
693 wpa_printf(MSG_DEBUG, "No suitable AP found.");
701 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
703 * Quick recovery if the initial scan results were not
704 * complete when fetched before the first scan request.
706 wpa_s->scan_res_tried++;
709 wpa_supplicant_req_scan(wpa_s, timeout, 0);
711 #endif /* CONFIG_NO_SCAN_PROCESSING */
714 #ifdef CONFIG_IEEE80211R
715 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
716 const u8 *ftie, const u8 *mdie)
718 const u8 *mobility_domain = NULL;
719 const u8 *r0kh_id = NULL;
720 size_t r0kh_id_len = 0;
721 const u8 *r1kh_id = NULL;
722 struct rsn_ftie *hdr;
725 if (mdie == NULL || ftie == NULL)
728 if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
729 mobility_domain = mdie + 2;
731 wpa_s->sme.ft_used = 1;
732 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
733 #endif /* CONFIG_SME */
735 if (ftie[1] >= sizeof(struct rsn_ftie)) {
736 end = ftie + 2 + ftie[1];
737 hdr = (struct rsn_ftie *) (ftie + 2);
738 pos = (const u8 *) (hdr + 1);
739 while (pos + 1 < end) {
740 if (pos + 2 + pos[1] > end)
742 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
743 pos[1] == FT_R1KH_ID_LEN)
745 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
746 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
748 r0kh_id_len = pos[1];
753 wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
754 r0kh_id_len, r1kh_id);
756 #endif /* CONFIG_IEEE80211R */
758 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
759 union wpa_event_data *data)
761 int l, len, found = 0, wpa_found, rsn_found;
763 #ifdef CONFIG_IEEE80211R
764 const u8 *mdie = NULL, *ftie = NULL;
765 #endif /* CONFIG_IEEE80211R */
767 wpa_printf(MSG_DEBUG, "Association info event");
768 if (data->assoc_info.req_ies)
769 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
770 data->assoc_info.req_ies_len);
771 if (data->assoc_info.resp_ies)
772 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
773 data->assoc_info.resp_ies_len);
774 if (data->assoc_info.beacon_ies)
775 wpa_hexdump(MSG_DEBUG, "beacon_ies",
776 data->assoc_info.beacon_ies,
777 data->assoc_info.beacon_ies_len);
779 p = data->assoc_info.req_ies;
780 l = data->assoc_info.req_ies_len;
782 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
783 while (p && l >= 2) {
786 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
790 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
791 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
792 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
793 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
796 wpa_find_assoc_pmkid(wpa_s);
802 if (!found && data->assoc_info.req_ies)
803 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
805 #ifdef CONFIG_IEEE80211R
806 p = data->assoc_info.resp_ies;
807 l = data->assoc_info.resp_ies_len;
809 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
810 while (p && l >= 2) {
813 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
817 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
819 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
825 wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
826 #endif /* CONFIG_IEEE80211R */
828 /* WPA/RSN IE from Beacon/ProbeResp */
829 p = data->assoc_info.beacon_ies;
830 l = data->assoc_info.beacon_ies_len;
832 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
834 wpa_found = rsn_found = 0;
835 while (p && l >= 2) {
838 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
843 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
844 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
846 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
850 p[0] == WLAN_EID_RSN && p[1] >= 2) {
852 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
859 if (!wpa_found && data->assoc_info.beacon_ies)
860 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
861 if (!rsn_found && data->assoc_info.beacon_ies)
862 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
863 if (wpa_found || rsn_found)
864 wpa_s->ap_ies_from_associnfo = 1;
868 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
869 union wpa_event_data *data)
872 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
875 wpa_supplicant_event_associnfo(wpa_s, data);
877 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
878 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
879 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
880 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
881 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
882 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
883 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
884 MACSTR, MAC2STR(bssid));
885 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
886 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
887 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
888 wpa_clear_keys(wpa_s, bssid);
890 if (wpa_supplicant_select_config(wpa_s) < 0) {
891 wpa_supplicant_disassociate(
892 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
897 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
898 if (wpa_s->current_ssid) {
899 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
900 * initialized before association, but for other modes,
901 * initialize PC/SC here, if the current configuration needs
902 * smartcard or SIM/USIM. */
903 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
905 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
906 l2_packet_notify_auth_start(wpa_s->l2);
909 * Set portEnabled first to FALSE in order to get EAP state machine out
910 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
911 * state machine may transit to AUTHENTICATING state based on obsolete
912 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
913 * AUTHENTICATED without ever giving chance to EAP state machine to
917 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
918 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
920 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
921 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
922 /* 802.1X::portControl = Auto */
923 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
924 wpa_s->eapol_received = 0;
925 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
926 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
927 wpa_supplicant_cancel_auth_timeout(wpa_s);
928 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
929 } else if (!ft_completed) {
930 /* Timeout for receiving the first EAPOL packet */
931 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
933 wpa_supplicant_cancel_scan(wpa_s);
935 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
936 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
938 * We are done; the driver will take care of RSN 4-way
941 wpa_supplicant_cancel_auth_timeout(wpa_s);
942 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
943 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
944 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
949 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
953 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
955 * At least Host AP driver and a Prism3 card seemed to be
956 * generating streams of disconnected events when configuring
957 * IBSS for WPA-None. Ignore them for now.
959 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
960 "IBSS/WPA-None mode");
964 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
965 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
966 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
967 "pre-shared key may be incorrect");
969 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
970 wpa_supplicant_req_scan(wpa_s, 0, 100000);
971 bssid = wpa_s->bssid;
972 if (is_zero_ether_addr(bssid))
973 bssid = wpa_s->pending_bssid;
974 wpa_blacklist_add(wpa_s, bssid);
975 wpa_sm_notify_disassoc(wpa_s->wpa);
976 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
978 if (wpa_supplicant_dynamic_keys(wpa_s)) {
979 wpa_s->keys_cleared = 0;
980 wpa_clear_keys(wpa_s, wpa_s->bssid);
982 wpa_supplicant_mark_disassoc(wpa_s);
986 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
987 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
990 struct wpa_supplicant *wpa_s = eloop_ctx;
992 if (!wpa_s->pending_mic_error_report)
995 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
996 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
997 wpa_s->pending_mic_error_report = 0;
999 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1003 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1004 union wpa_event_data *data)
1009 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1010 pairwise = (data && data->michael_mic_failure.unicast);
1012 if ((wpa_s->last_michael_mic_error &&
1013 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1014 wpa_s->pending_mic_error_report) {
1015 if (wpa_s->pending_mic_error_report) {
1017 * Send the pending MIC error report immediately since
1018 * we are going to start countermeasures and AP better
1021 wpa_sm_key_request(wpa_s->wpa, 1,
1022 wpa_s->pending_mic_error_pairwise);
1025 /* Send the new MIC error report immediately since we are going
1026 * to start countermeasures and AP better do the same.
1028 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1030 /* initialize countermeasures */
1031 wpa_s->countermeasures = 1;
1032 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1035 * Need to wait for completion of request frame. We do not get
1036 * any callback for the message completion, so just wait a
1037 * short while and hope for the best. */
1040 wpa_drv_set_countermeasures(wpa_s, 1);
1041 wpa_supplicant_deauthenticate(wpa_s,
1042 WLAN_REASON_MICHAEL_MIC_FAILURE);
1043 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1045 eloop_register_timeout(60, 0,
1046 wpa_supplicant_stop_countermeasures,
1048 /* TODO: mark the AP rejected for 60 second. STA is
1049 * allowed to associate with another AP.. */
1051 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1052 if (wpa_s->mic_errors_seen) {
1054 * Reduce the effectiveness of Michael MIC error
1055 * reports as a means for attacking against TKIP if
1056 * more than one MIC failure is noticed with the same
1057 * PTK. We delay the transmission of the reports by a
1058 * random time between 0 and 60 seconds in order to
1059 * force the attacker wait 60 seconds before getting
1060 * the information on whether a frame resulted in a MIC
1066 if (os_get_random(rval, sizeof(rval)) < 0)
1067 sec = os_random() % 60;
1069 sec = WPA_GET_BE32(rval) % 60;
1070 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1072 wpa_s->pending_mic_error_report = 1;
1073 wpa_s->pending_mic_error_pairwise = pairwise;
1074 eloop_cancel_timeout(
1075 wpa_supplicant_delayed_mic_error_report,
1077 eloop_register_timeout(
1078 sec, os_random() % 1000000,
1079 wpa_supplicant_delayed_mic_error_report,
1082 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1084 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1085 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1086 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1088 wpa_s->last_michael_mic_error = t.sec;
1089 wpa_s->mic_errors_seen++;
1093 #ifdef CONFIG_TERMINATE_ONLASTIF
1094 static int any_interfaces(struct wpa_supplicant *head)
1096 struct wpa_supplicant *wpa_s;
1098 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1099 if (!wpa_s->interface_removed)
1103 #endif /* CONFIG_TERMINATE_ONLASTIF */
1107 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1108 union wpa_event_data *data)
1110 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1113 switch (data->interface_status.ievent) {
1114 case EVENT_INTERFACE_ADDED:
1115 if (!wpa_s->interface_removed)
1117 wpa_s->interface_removed = 0;
1118 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1119 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1120 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1121 "after interface was added.");
1124 case EVENT_INTERFACE_REMOVED:
1125 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1126 wpa_s->interface_removed = 1;
1127 wpa_supplicant_mark_disassoc(wpa_s);
1128 l2_packet_deinit(wpa_s->l2);
1130 #ifdef CONFIG_TERMINATE_ONLASTIF
1131 /* check if last interface */
1132 if (!any_interfaces(wpa_s->global->ifaces))
1134 #endif /* CONFIG_TERMINATE_ONLASTIF */
1140 #ifdef CONFIG_PEERKEY
1142 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1143 union wpa_event_data *data)
1147 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1149 #endif /* CONFIG_PEERKEY */
1152 #ifdef CONFIG_IEEE80211R
1154 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1155 union wpa_event_data *data)
1160 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1161 data->ft_ies.ies_len,
1162 data->ft_ies.ft_action,
1163 data->ft_ies.target_ap,
1164 data->ft_ies.ric_ies,
1165 data->ft_ies.ric_ies_len) < 0) {
1166 /* TODO: prevent MLME/driver from trying to associate? */
1169 #endif /* CONFIG_IEEE80211R */
1172 #ifdef CONFIG_IBSS_RSN
1173 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1174 union wpa_event_data *data)
1178 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1180 #endif /* CONFIG_IBSS_RSN */
1183 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1184 union wpa_event_data *data)
1186 struct wpa_supplicant *wpa_s = ctx;
1190 sme_event_auth(wpa_s, data);
1193 wpa_supplicant_event_assoc(wpa_s, data);
1196 case EVENT_DISASSOC:
1197 wpa_supplicant_event_disassoc(wpa_s);
1199 case EVENT_MICHAEL_MIC_FAILURE:
1200 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1202 #ifndef CONFIG_NO_SCAN_PROCESSING
1203 case EVENT_SCAN_RESULTS:
1204 wpa_supplicant_event_scan_results(wpa_s);
1206 #endif /* CONFIG_NO_SCAN_PROCESSING */
1207 case EVENT_ASSOCINFO:
1208 wpa_supplicant_event_associnfo(wpa_s, data);
1210 case EVENT_INTERFACE_STATUS:
1211 wpa_supplicant_event_interface_status(wpa_s, data);
1213 case EVENT_PMKID_CANDIDATE:
1214 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1216 #ifdef CONFIG_PEERKEY
1217 case EVENT_STKSTART:
1218 wpa_supplicant_event_stkstart(wpa_s, data);
1220 #endif /* CONFIG_PEERKEY */
1221 #ifdef CONFIG_IEEE80211R
1222 case EVENT_FT_RESPONSE:
1223 wpa_supplicant_event_ft_response(wpa_s, data);
1225 #endif /* CONFIG_IEEE80211R */
1226 #ifdef CONFIG_IBSS_RSN
1227 case EVENT_IBSS_RSN_START:
1228 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1230 #endif /* CONFIG_IBSS_RSN */
1231 case EVENT_ASSOC_REJECT:
1232 sme_event_assoc_reject(wpa_s, data);
1234 case EVENT_AUTH_TIMED_OUT:
1235 sme_event_auth_timed_out(wpa_s, data);
1237 case EVENT_ASSOC_TIMED_OUT:
1238 sme_event_assoc_timed_out(wpa_s, data);
1241 wpa_printf(MSG_INFO, "Unknown event %d", event);