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"
31 #include "ieee802_11_defs.h"
32 #include "blacklist.h"
33 #include "wpas_glue.h"
34 #include "wps_supplicant.h"
40 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
42 struct wpa_ssid *ssid, *old_ssid;
44 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
47 wpa_printf(MSG_DEBUG, "Select network based on association "
49 ssid = wpa_supplicant_get_ssid(wpa_s);
51 wpa_printf(MSG_INFO, "No network configuration found for the "
57 wpa_printf(MSG_DEBUG, "Selected network is disabled");
61 wpa_printf(MSG_DEBUG, "Network configuration found for the current "
63 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
64 WPA_KEY_MGMT_WPA_NONE |
65 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
66 WPA_KEY_MGMT_PSK_SHA256 |
67 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
69 size_t wpa_ie_len = sizeof(wpa_ie);
70 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
73 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
76 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
77 eapol_sm_invalidate_cached_session(wpa_s->eapol);
78 old_ssid = wpa_s->current_ssid;
79 wpa_s->current_ssid = ssid;
80 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
81 wpa_supplicant_initiate_eapol(wpa_s);
82 if (old_ssid != wpa_s->current_ssid)
83 wpas_notify_network_changed(wpa_s);
89 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
92 struct wpa_supplicant *wpa_s = eloop_ctx;
94 if (wpa_s->countermeasures) {
95 wpa_s->countermeasures = 0;
96 wpa_drv_set_countermeasures(wpa_s, 0);
97 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
98 wpa_supplicant_req_scan(wpa_s, 0, 0);
103 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
107 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
108 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
109 os_memset(wpa_s->bssid, 0, ETH_ALEN);
110 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
112 wpas_notify_bssid_changed(wpa_s);
114 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
115 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
116 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
117 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
118 wpa_s->ap_ies_from_associnfo = 0;
122 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
124 struct wpa_ie_data ie;
128 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
132 for (i = 0; i < ie.num_pmkid; i++) {
133 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
134 ie.pmkid + i * PMKID_LEN,
136 if (pmksa_set == 0) {
137 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
142 wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
143 "cache", pmksa_set == 0 ? "" : "not ");
147 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
148 union wpa_event_data *data)
151 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
154 wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
155 " index=%d preauth=%d",
156 MAC2STR(data->pmkid_candidate.bssid),
157 data->pmkid_candidate.index,
158 data->pmkid_candidate.preauth);
160 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
161 data->pmkid_candidate.index,
162 data->pmkid_candidate.preauth);
166 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
168 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
169 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
172 #ifdef IEEE8021X_EAPOL
173 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
174 wpa_s->current_ssid &&
175 !(wpa_s->current_ssid->eapol_flags &
176 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
177 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
178 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
179 * plaintext or static WEP keys). */
182 #endif /* IEEE8021X_EAPOL */
189 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
190 * @wpa_s: pointer to wpa_supplicant data
191 * @ssid: Configuration data for the network
192 * Returns: 0 on success, -1 on failure
194 * This function is called when starting authentication with a network that is
195 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
197 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
198 struct wpa_ssid *ssid)
200 #ifdef IEEE8021X_EAPOL
201 int aka = 0, sim = 0, type;
203 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
206 if (ssid->eap.eap_methods == NULL) {
210 struct eap_method_type *eap = ssid->eap.eap_methods;
211 while (eap->vendor != EAP_VENDOR_IETF ||
212 eap->method != EAP_TYPE_NONE) {
213 if (eap->vendor == EAP_VENDOR_IETF) {
214 if (eap->method == EAP_TYPE_SIM)
216 else if (eap->method == EAP_TYPE_AKA)
223 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
225 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
229 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
230 "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
234 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
235 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
237 type = SCARD_TRY_BOTH;
239 type = SCARD_USIM_ONLY;
241 type = SCARD_GSM_SIM_ONLY;
243 wpa_s->scard = scard_init(type);
244 if (wpa_s->scard == NULL) {
245 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
249 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
250 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
251 #endif /* IEEE8021X_EAPOL */
257 #ifndef CONFIG_NO_SCAN_PROCESSING
258 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
259 struct wpa_ssid *ssid)
263 if (ssid->mixed_cell)
267 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
269 #endif /* CONFIG_WPS */
271 for (i = 0; i < NUM_WEP_KEYS; i++) {
272 if (ssid->wep_key_len[i]) {
277 #ifdef IEEE8021X_EAPOL
278 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
279 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
280 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
282 #endif /* IEEE8021X_EAPOL */
284 if (bss->caps & IEEE80211_CAP_PRIVACY)
290 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
291 struct wpa_ssid *ssid,
292 struct wpa_scan_res *bss)
294 struct wpa_ie_data ie;
296 const u8 *rsn_ie, *wpa_ie;
299 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
303 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
304 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
307 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
308 wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
311 if (!(ie.proto & ssid->proto)) {
312 wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
317 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
318 wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
323 if (!(ie.group_cipher & ssid->group_cipher)) {
324 wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
329 if (!(ie.key_mgmt & ssid->key_mgmt)) {
330 wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
335 #ifdef CONFIG_IEEE80211W
336 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
337 ssid->ieee80211w == IEEE80211W_REQUIRED) {
338 wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
342 #endif /* CONFIG_IEEE80211W */
344 wpa_printf(MSG_DEBUG, " selected based on RSN IE");
348 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
349 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
352 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
353 wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
356 if (!(ie.proto & ssid->proto)) {
357 wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
362 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
363 wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
368 if (!(ie.group_cipher & ssid->group_cipher)) {
369 wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
374 if (!(ie.key_mgmt & ssid->key_mgmt)) {
375 wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
380 wpa_printf(MSG_DEBUG, " selected based on WPA IE");
384 if (proto_match == 0)
385 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
391 static struct wpa_scan_res *
392 wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
393 struct wpa_ssid *group,
394 struct wpa_ssid **selected_ssid)
396 struct wpa_ssid *ssid;
397 struct wpa_scan_res *bss;
399 struct wpa_blacklist *e;
402 wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
403 for (i = 0; i < wpa_s->scan_res->num; i++) {
405 u8 wpa_ie_len, rsn_ie_len, ssid_len;
406 bss = wpa_s->scan_res->res[i];
408 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
409 ssid_ = ie ? ie + 2 : (u8 *) "";
410 ssid_len = ie ? ie[1] : 0;
412 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
413 wpa_ie_len = ie ? ie[1] : 0;
415 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
416 rsn_ie_len = ie ? ie[1] : 0;
418 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
419 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
420 (int) i, MAC2STR(bss->bssid),
421 wpa_ssid_txt(ssid_, ssid_len),
422 wpa_ie_len, rsn_ie_len, bss->caps);
424 e = wpa_blacklist_get(wpa_s, bss->bssid);
425 if (e && e->count > 1) {
426 wpa_printf(MSG_DEBUG, " skip - blacklisted");
430 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
431 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
435 for (ssid = group; ssid; ssid = ssid->pnext) {
438 if (ssid->disabled) {
439 wpa_printf(MSG_DEBUG, " skip - disabled");
444 if (ssid->ssid_len == 0 &&
445 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
447 #endif /* CONFIG_WPS */
450 (ssid_len != ssid->ssid_len ||
451 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
452 wpa_printf(MSG_DEBUG, " skip - "
457 if (ssid->bssid_set &&
458 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
460 wpa_printf(MSG_DEBUG, " skip - "
465 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
468 wpa_printf(MSG_DEBUG, " selected WPA AP "
471 wpa_ssid_txt(ssid_, ssid_len));
472 *selected_ssid = ssid;
481 static struct wpa_scan_res *
482 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
483 struct wpa_ssid *group,
484 struct wpa_ssid **selected_ssid)
486 struct wpa_ssid *ssid;
487 struct wpa_scan_res *bss;
489 struct wpa_blacklist *e;
492 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
493 for (i = 0; i < wpa_s->scan_res->num; i++) {
495 u8 wpa_ie_len, rsn_ie_len, ssid_len;
496 bss = wpa_s->scan_res->res[i];
498 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
499 ssid_ = ie ? ie + 2 : (u8 *) "";
500 ssid_len = ie ? ie[1] : 0;
502 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
503 wpa_ie_len = ie ? ie[1] : 0;
505 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
506 rsn_ie_len = ie ? ie[1] : 0;
508 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
509 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
510 (int) i, MAC2STR(bss->bssid),
511 wpa_ssid_txt(ssid_, ssid_len),
512 wpa_ie_len, rsn_ie_len, bss->caps);
514 e = wpa_blacklist_get(wpa_s, bss->bssid);
515 if (e && e->count > 1) {
516 wpa_printf(MSG_DEBUG, " skip - blacklisted");
520 for (ssid = group; ssid; ssid = ssid->pnext) {
521 int check_ssid = ssid->ssid_len != 0;
523 if (ssid->disabled) {
524 wpa_printf(MSG_DEBUG, " skip - disabled");
529 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
530 /* Only allow wildcard SSID match if an AP
531 * advertises active WPS operation that matches
534 if (ssid->ssid_len == 0 &&
535 wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
539 #endif /* CONFIG_WPS */
542 (ssid_len != ssid->ssid_len ||
543 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
544 wpa_printf(MSG_DEBUG, " skip - "
549 if (ssid->bssid_set &&
550 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
552 wpa_printf(MSG_DEBUG, " skip - "
557 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
558 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
559 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
561 wpa_printf(MSG_DEBUG, " skip - "
562 "non-WPA network not allowed");
566 if ((ssid->key_mgmt &
567 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
568 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
569 WPA_KEY_MGMT_IEEE8021X_SHA256 |
570 WPA_KEY_MGMT_PSK_SHA256)) &&
571 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
572 wpa_printf(MSG_DEBUG, " skip - "
577 if (!wpa_supplicant_match_privacy(bss, ssid)) {
578 wpa_printf(MSG_DEBUG, " skip - "
583 if (bss->caps & IEEE80211_CAP_IBSS) {
584 wpa_printf(MSG_DEBUG, " skip - "
585 "IBSS (adhoc) network");
589 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
592 wpa_ssid_txt(ssid_, ssid_len));
593 *selected_ssid = ssid;
602 static struct wpa_scan_res *
603 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
604 struct wpa_ssid **selected_ssid)
606 struct wpa_scan_res *selected;
608 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
611 /* First, try to find WPA-enabled AP */
612 selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
616 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
618 return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
622 static struct wpa_scan_res *
623 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
624 struct wpa_ssid **selected_ssid)
626 struct wpa_scan_res *selected = NULL;
629 while (selected == NULL) {
630 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
631 selected = wpa_supplicant_select_bss(
632 wpa_s, wpa_s->conf->pssid[prio],
638 if (selected == NULL && wpa_s->blacklist) {
639 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
641 wpa_blacklist_clear(wpa_s);
642 wpa_s->blacklist_cleared++;
643 } else if (selected == NULL)
651 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
654 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
656 * Quick recovery if the initial scan results were not
657 * complete when fetched before the first scan request.
659 wpa_s->scan_res_tried++;
662 wpa_supplicant_req_scan(wpa_s, timeout, 0);
666 static void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
667 struct wpa_scan_res *selected,
668 struct wpa_ssid *ssid)
670 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
671 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
672 "PBC session overlap");
673 wpa_supplicant_req_new_scan(wpa_s, 10);
678 * Do not trigger new association unless the BSSID has changed or if
679 * reassociation is requested. If we are in process of associating with
680 * the selected BSSID, do not trigger new attempt.
682 if (wpa_s->reassociate ||
683 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
684 (wpa_s->wpa_state != WPA_ASSOCIATING ||
685 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
687 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
688 wpa_supplicant_req_new_scan(wpa_s, 10);
691 wpa_supplicant_associate(wpa_s, selected, ssid);
693 wpa_printf(MSG_DEBUG, "Already associated with the selected "
696 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
700 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
702 struct wpa_scan_res *selected;
703 struct wpa_ssid *ssid = NULL;
705 wpa_supplicant_notify_scanning(wpa_s, 0);
707 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
708 if (wpa_s->conf->ap_scan == 2)
710 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
712 wpa_supplicant_req_new_scan(wpa_s, 1);
717 * Don't post the results if this was the initial cached
718 * and there were no results.
720 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
721 wpa_s->scan_res->num == 0) {
722 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
723 "empty - not posting");
725 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
726 wpas_notify_scan_results(wpa_s);
729 wpas_notify_scan_done(wpa_s, 1);
731 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
734 if (wpa_s->disconnected) {
735 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
739 if (bgscan_notify_scan(wpa_s) == 1)
742 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
744 wpa_supplicant_connect(wpa_s, selected, ssid);
746 wpa_printf(MSG_DEBUG, "No suitable network found");
747 wpa_supplicant_req_new_scan(wpa_s, 5);
750 #endif /* CONFIG_NO_SCAN_PROCESSING */
753 #ifdef CONFIG_IEEE80211R
754 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
755 const u8 *ftie, const u8 *mdie)
757 const u8 *mobility_domain = NULL;
758 const u8 *r0kh_id = NULL;
759 size_t r0kh_id_len = 0;
760 const u8 *r1kh_id = NULL;
761 struct rsn_ftie *hdr;
764 if (mdie == NULL || ftie == NULL)
767 if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
768 mobility_domain = mdie + 2;
770 wpa_s->sme.ft_used = 1;
771 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
772 #endif /* CONFIG_SME */
774 if (ftie[1] >= sizeof(struct rsn_ftie)) {
775 end = ftie + 2 + ftie[1];
776 hdr = (struct rsn_ftie *) (ftie + 2);
777 pos = (const u8 *) (hdr + 1);
778 while (pos + 1 < end) {
779 if (pos + 2 + pos[1] > end)
781 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
782 pos[1] == FT_R1KH_ID_LEN)
784 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
785 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
787 r0kh_id_len = pos[1];
792 wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
793 r0kh_id_len, r1kh_id);
795 #endif /* CONFIG_IEEE80211R */
797 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
798 union wpa_event_data *data)
800 int l, len, found = 0, wpa_found, rsn_found;
802 #ifdef CONFIG_IEEE80211R
803 const u8 *mdie = NULL, *ftie = NULL;
804 #endif /* CONFIG_IEEE80211R */
806 wpa_printf(MSG_DEBUG, "Association info event");
807 if (data->assoc_info.req_ies)
808 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
809 data->assoc_info.req_ies_len);
810 if (data->assoc_info.resp_ies)
811 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
812 data->assoc_info.resp_ies_len);
813 if (data->assoc_info.beacon_ies)
814 wpa_hexdump(MSG_DEBUG, "beacon_ies",
815 data->assoc_info.beacon_ies,
816 data->assoc_info.beacon_ies_len);
818 p = data->assoc_info.req_ies;
819 l = data->assoc_info.req_ies_len;
821 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
822 while (p && l >= 2) {
825 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
829 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
830 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
831 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
832 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
835 wpa_find_assoc_pmkid(wpa_s);
841 if (!found && data->assoc_info.req_ies)
842 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
844 #ifdef CONFIG_IEEE80211R
845 p = data->assoc_info.resp_ies;
846 l = data->assoc_info.resp_ies_len;
848 /* Go through the IEs and make a copy of the FT/MD IE, if present. */
849 while (p && l >= 2) {
852 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
856 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
858 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
864 wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
865 #endif /* CONFIG_IEEE80211R */
867 /* WPA/RSN IE from Beacon/ProbeResp */
868 p = data->assoc_info.beacon_ies;
869 l = data->assoc_info.beacon_ies_len;
871 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
873 wpa_found = rsn_found = 0;
874 while (p && l >= 2) {
877 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
882 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
883 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
885 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
889 p[0] == WLAN_EID_RSN && p[1] >= 2) {
891 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
898 if (!wpa_found && data->assoc_info.beacon_ies)
899 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
900 if (!rsn_found && data->assoc_info.beacon_ies)
901 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
902 if (wpa_found || rsn_found)
903 wpa_s->ap_ies_from_associnfo = 1;
907 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
908 union wpa_event_data *data)
911 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
913 struct wpa_driver_capa capa;
916 wpa_supplicant_event_associnfo(wpa_s, data);
918 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
919 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
920 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
921 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
922 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
923 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
924 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
925 MACSTR, MAC2STR(bssid));
926 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
927 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
928 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
930 wpas_notify_bssid_changed(wpa_s);
932 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
933 wpa_clear_keys(wpa_s, bssid);
935 if (wpa_supplicant_select_config(wpa_s) < 0) {
936 wpa_supplicant_disassociate(
937 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
942 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
943 if (wpa_s->current_ssid) {
944 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
945 * initialized before association, but for other modes,
946 * initialize PC/SC here, if the current configuration needs
947 * smartcard or SIM/USIM. */
948 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
950 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
951 l2_packet_notify_auth_start(wpa_s->l2);
954 * Set portEnabled first to FALSE in order to get EAP state machine out
955 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
956 * state machine may transit to AUTHENTICATING state based on obsolete
957 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
958 * AUTHENTICATED without ever giving chance to EAP state machine to
962 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
963 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
965 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
966 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
967 /* 802.1X::portControl = Auto */
968 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
969 wpa_s->eapol_received = 0;
970 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
971 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
972 wpa_supplicant_cancel_auth_timeout(wpa_s);
973 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
974 } else if (!ft_completed) {
975 /* Timeout for receiving the first EAPOL packet */
976 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
978 wpa_supplicant_cancel_scan(wpa_s);
980 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
981 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
983 * We are done; the driver will take care of RSN 4-way
986 wpa_supplicant_cancel_auth_timeout(wpa_s);
987 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
988 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
989 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
992 if (wpa_s->pending_eapol_rx) {
993 struct os_time now, age;
995 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
996 if (age.sec == 0 && age.usec < 100000 &&
997 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
999 wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1000 "that was received just before association "
1002 wpa_supplicant_rx_eapol(
1003 wpa_s, wpa_s->pending_eapol_rx_src,
1004 wpabuf_head(wpa_s->pending_eapol_rx),
1005 wpabuf_len(wpa_s->pending_eapol_rx));
1007 wpabuf_free(wpa_s->pending_eapol_rx);
1008 wpa_s->pending_eapol_rx = NULL;
1011 #ifdef CONFIG_BGSCAN
1012 if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1013 bgscan_deinit(wpa_s);
1014 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1015 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1016 wpa_printf(MSG_DEBUG, "Failed to initialize "
1019 * Live without bgscan; it is only used as a
1020 * roaming optimization, so the initial
1021 * connection is not affected.
1024 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1026 wpa_s->bgscan_ssid = NULL;
1028 #endif /* CONFIG_BGSCAN */
1030 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1031 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1032 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1033 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1034 /* Set static WEP keys again */
1035 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1040 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
1044 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1046 * At least Host AP driver and a Prism3 card seemed to be
1047 * generating streams of disconnected events when configuring
1048 * IBSS for WPA-None. Ignore them for now.
1050 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1051 "IBSS/WPA-None mode");
1055 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1056 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1057 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1058 "pre-shared key may be incorrect");
1060 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1061 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1062 bssid = wpa_s->bssid;
1063 if (is_zero_ether_addr(bssid))
1064 bssid = wpa_s->pending_bssid;
1065 wpa_blacklist_add(wpa_s, bssid);
1066 wpa_sm_notify_disassoc(wpa_s->wpa);
1067 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
1069 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1070 wpa_s->keys_cleared = 0;
1071 wpa_clear_keys(wpa_s, wpa_s->bssid);
1073 wpa_supplicant_mark_disassoc(wpa_s);
1074 bgscan_deinit(wpa_s);
1078 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1079 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1082 struct wpa_supplicant *wpa_s = eloop_ctx;
1084 if (!wpa_s->pending_mic_error_report)
1087 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1088 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1089 wpa_s->pending_mic_error_report = 0;
1091 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1095 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1096 union wpa_event_data *data)
1101 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1102 pairwise = (data && data->michael_mic_failure.unicast);
1104 if ((wpa_s->last_michael_mic_error &&
1105 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1106 wpa_s->pending_mic_error_report) {
1107 if (wpa_s->pending_mic_error_report) {
1109 * Send the pending MIC error report immediately since
1110 * we are going to start countermeasures and AP better
1113 wpa_sm_key_request(wpa_s->wpa, 1,
1114 wpa_s->pending_mic_error_pairwise);
1117 /* Send the new MIC error report immediately since we are going
1118 * to start countermeasures and AP better do the same.
1120 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1122 /* initialize countermeasures */
1123 wpa_s->countermeasures = 1;
1124 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1127 * Need to wait for completion of request frame. We do not get
1128 * any callback for the message completion, so just wait a
1129 * short while and hope for the best. */
1132 wpa_drv_set_countermeasures(wpa_s, 1);
1133 wpa_supplicant_deauthenticate(wpa_s,
1134 WLAN_REASON_MICHAEL_MIC_FAILURE);
1135 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1137 eloop_register_timeout(60, 0,
1138 wpa_supplicant_stop_countermeasures,
1140 /* TODO: mark the AP rejected for 60 second. STA is
1141 * allowed to associate with another AP.. */
1143 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1144 if (wpa_s->mic_errors_seen) {
1146 * Reduce the effectiveness of Michael MIC error
1147 * reports as a means for attacking against TKIP if
1148 * more than one MIC failure is noticed with the same
1149 * PTK. We delay the transmission of the reports by a
1150 * random time between 0 and 60 seconds in order to
1151 * force the attacker wait 60 seconds before getting
1152 * the information on whether a frame resulted in a MIC
1158 if (os_get_random(rval, sizeof(rval)) < 0)
1159 sec = os_random() % 60;
1161 sec = WPA_GET_BE32(rval) % 60;
1162 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1164 wpa_s->pending_mic_error_report = 1;
1165 wpa_s->pending_mic_error_pairwise = pairwise;
1166 eloop_cancel_timeout(
1167 wpa_supplicant_delayed_mic_error_report,
1169 eloop_register_timeout(
1170 sec, os_random() % 1000000,
1171 wpa_supplicant_delayed_mic_error_report,
1174 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1176 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1177 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1178 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1180 wpa_s->last_michael_mic_error = t.sec;
1181 wpa_s->mic_errors_seen++;
1185 #ifdef CONFIG_TERMINATE_ONLASTIF
1186 static int any_interfaces(struct wpa_supplicant *head)
1188 struct wpa_supplicant *wpa_s;
1190 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1191 if (!wpa_s->interface_removed)
1195 #endif /* CONFIG_TERMINATE_ONLASTIF */
1199 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1200 union wpa_event_data *data)
1202 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1205 switch (data->interface_status.ievent) {
1206 case EVENT_INTERFACE_ADDED:
1207 if (!wpa_s->interface_removed)
1209 wpa_s->interface_removed = 0;
1210 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1211 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1212 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1213 "after interface was added.");
1216 case EVENT_INTERFACE_REMOVED:
1217 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1218 wpa_s->interface_removed = 1;
1219 wpa_supplicant_mark_disassoc(wpa_s);
1220 l2_packet_deinit(wpa_s->l2);
1222 #ifdef CONFIG_TERMINATE_ONLASTIF
1223 /* check if last interface */
1224 if (!any_interfaces(wpa_s->global->ifaces))
1226 #endif /* CONFIG_TERMINATE_ONLASTIF */
1232 #ifdef CONFIG_PEERKEY
1234 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1235 union wpa_event_data *data)
1239 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1241 #endif /* CONFIG_PEERKEY */
1244 #ifdef CONFIG_IEEE80211R
1246 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1247 union wpa_event_data *data)
1252 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1253 data->ft_ies.ies_len,
1254 data->ft_ies.ft_action,
1255 data->ft_ies.target_ap,
1256 data->ft_ies.ric_ies,
1257 data->ft_ies.ric_ies_len) < 0) {
1258 /* TODO: prevent MLME/driver from trying to associate? */
1261 #endif /* CONFIG_IEEE80211R */
1264 #ifdef CONFIG_IBSS_RSN
1265 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1266 union wpa_event_data *data)
1270 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1272 #endif /* CONFIG_IBSS_RSN */
1275 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1276 union wpa_event_data *data)
1278 struct wpa_supplicant *wpa_s = ctx;
1282 sme_event_auth(wpa_s, data);
1285 wpa_supplicant_event_assoc(wpa_s, data);
1288 case EVENT_DISASSOC:
1289 wpa_supplicant_event_disassoc(wpa_s);
1291 case EVENT_MICHAEL_MIC_FAILURE:
1292 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1294 #ifndef CONFIG_NO_SCAN_PROCESSING
1295 case EVENT_SCAN_RESULTS:
1296 wpa_supplicant_event_scan_results(wpa_s);
1298 #endif /* CONFIG_NO_SCAN_PROCESSING */
1299 case EVENT_ASSOCINFO:
1300 wpa_supplicant_event_associnfo(wpa_s, data);
1302 case EVENT_INTERFACE_STATUS:
1303 wpa_supplicant_event_interface_status(wpa_s, data);
1305 case EVENT_PMKID_CANDIDATE:
1306 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1308 #ifdef CONFIG_PEERKEY
1309 case EVENT_STKSTART:
1310 wpa_supplicant_event_stkstart(wpa_s, data);
1312 #endif /* CONFIG_PEERKEY */
1313 #ifdef CONFIG_IEEE80211R
1314 case EVENT_FT_RESPONSE:
1315 wpa_supplicant_event_ft_response(wpa_s, data);
1317 #endif /* CONFIG_IEEE80211R */
1318 #ifdef CONFIG_IBSS_RSN
1319 case EVENT_IBSS_RSN_START:
1320 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1322 #endif /* CONFIG_IBSS_RSN */
1323 case EVENT_ASSOC_REJECT:
1324 sme_event_assoc_reject(wpa_s, data);
1326 case EVENT_AUTH_TIMED_OUT:
1327 sme_event_auth_timed_out(wpa_s, data);
1329 case EVENT_ASSOC_TIMED_OUT:
1330 sme_event_assoc_timed_out(wpa_s, data);
1333 wpa_printf(MSG_INFO, "Unknown event %d", event);