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");
431 wpa_printf(MSG_DEBUG, " skip - SSID not known");
435 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
436 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
440 for (ssid = group; ssid; ssid = ssid->pnext) {
443 if (ssid->disabled) {
444 wpa_printf(MSG_DEBUG, " skip - disabled");
449 if (ssid->ssid_len == 0 &&
450 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
452 #endif /* CONFIG_WPS */
455 (ssid_len != ssid->ssid_len ||
456 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
457 wpa_printf(MSG_DEBUG, " skip - "
462 if (ssid->bssid_set &&
463 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
465 wpa_printf(MSG_DEBUG, " skip - "
470 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
473 wpa_printf(MSG_DEBUG, " selected WPA AP "
476 wpa_ssid_txt(ssid_, ssid_len));
477 *selected_ssid = ssid;
486 static struct wpa_scan_res *
487 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
488 struct wpa_ssid *group,
489 struct wpa_ssid **selected_ssid)
491 struct wpa_ssid *ssid;
492 struct wpa_scan_res *bss;
494 struct wpa_blacklist *e;
497 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
498 for (i = 0; i < wpa_s->scan_res->num; i++) {
500 u8 wpa_ie_len, rsn_ie_len, ssid_len;
501 bss = wpa_s->scan_res->res[i];
503 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
504 ssid_ = ie ? ie + 2 : (u8 *) "";
505 ssid_len = ie ? ie[1] : 0;
507 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
508 wpa_ie_len = ie ? ie[1] : 0;
510 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
511 rsn_ie_len = ie ? ie[1] : 0;
513 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
514 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
515 (int) i, MAC2STR(bss->bssid),
516 wpa_ssid_txt(ssid_, ssid_len),
517 wpa_ie_len, rsn_ie_len, bss->caps);
519 e = wpa_blacklist_get(wpa_s, bss->bssid);
520 if (e && e->count > 1) {
521 wpa_printf(MSG_DEBUG, " skip - blacklisted");
526 wpa_printf(MSG_DEBUG, " skip - SSID not known");
530 for (ssid = group; ssid; ssid = ssid->pnext) {
531 int check_ssid = ssid->ssid_len != 0;
533 if (ssid->disabled) {
534 wpa_printf(MSG_DEBUG, " skip - disabled");
539 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
540 /* Only allow wildcard SSID match if an AP
541 * advertises active WPS operation that matches
544 if (ssid->ssid_len == 0 &&
545 wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
549 #endif /* CONFIG_WPS */
552 (ssid_len != ssid->ssid_len ||
553 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
554 wpa_printf(MSG_DEBUG, " skip - "
559 if (ssid->bssid_set &&
560 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
562 wpa_printf(MSG_DEBUG, " skip - "
567 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
568 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
569 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
571 wpa_printf(MSG_DEBUG, " skip - "
572 "non-WPA network not allowed");
576 if ((ssid->key_mgmt &
577 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
578 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
579 WPA_KEY_MGMT_IEEE8021X_SHA256 |
580 WPA_KEY_MGMT_PSK_SHA256)) &&
581 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
582 wpa_printf(MSG_DEBUG, " skip - "
587 if (!wpa_supplicant_match_privacy(bss, ssid)) {
588 wpa_printf(MSG_DEBUG, " skip - "
593 if (bss->caps & IEEE80211_CAP_IBSS) {
594 wpa_printf(MSG_DEBUG, " skip - "
595 "IBSS (adhoc) network");
599 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
602 wpa_ssid_txt(ssid_, ssid_len));
603 *selected_ssid = ssid;
612 static struct wpa_scan_res *
613 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
614 struct wpa_ssid **selected_ssid)
616 struct wpa_scan_res *selected;
618 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
621 /* First, try to find WPA-enabled AP */
622 selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
626 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
628 return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
632 static struct wpa_scan_res *
633 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
634 struct wpa_ssid **selected_ssid)
636 struct wpa_scan_res *selected = NULL;
639 while (selected == NULL) {
640 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
641 selected = wpa_supplicant_select_bss(
642 wpa_s, wpa_s->conf->pssid[prio],
648 if (selected == NULL && wpa_s->blacklist) {
649 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
651 wpa_blacklist_clear(wpa_s);
652 wpa_s->blacklist_cleared++;
653 } else if (selected == NULL)
661 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
664 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
666 * Quick recovery if the initial scan results were not
667 * complete when fetched before the first scan request.
669 wpa_s->scan_res_tried++;
671 } else if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
673 * No networks are enabled; short-circuit request so
674 * we don't wait timeout seconds before transitioning
677 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
680 wpa_supplicant_req_scan(wpa_s, timeout, 0);
684 static void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
685 struct wpa_scan_res *selected,
686 struct wpa_ssid *ssid)
688 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
689 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
690 "PBC session overlap");
691 wpa_supplicant_req_new_scan(wpa_s, 10);
696 * Do not trigger new association unless the BSSID has changed or if
697 * reassociation is requested. If we are in process of associating with
698 * the selected BSSID, do not trigger new attempt.
700 if (wpa_s->reassociate ||
701 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
702 (wpa_s->wpa_state != WPA_ASSOCIATING ||
703 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
705 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
706 wpa_supplicant_req_new_scan(wpa_s, 10);
709 wpa_supplicant_associate(wpa_s, selected, ssid);
711 wpa_printf(MSG_DEBUG, "Already associated with the selected "
714 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
718 static struct wpa_ssid *
719 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
722 struct wpa_ssid *ssid;
724 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
725 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
729 if (ssid->mode == IEEE80211_MODE_IBSS ||
730 ssid->mode == IEEE80211_MODE_AP)
738 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
740 struct wpa_scan_res *selected;
741 struct wpa_ssid *ssid = NULL;
743 wpa_supplicant_notify_scanning(wpa_s, 0);
745 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
746 if (wpa_s->conf->ap_scan == 2)
748 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
750 wpa_supplicant_req_new_scan(wpa_s, 1);
755 * Don't post the results if this was the initial cached
756 * and there were no results.
758 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
759 wpa_s->scan_res->num == 0) {
760 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
761 "empty - not posting");
763 wpa_printf(MSG_DEBUG, "New scan results available");
764 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
765 wpas_notify_scan_results(wpa_s);
768 wpas_notify_scan_done(wpa_s, 1);
770 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
773 if (wpa_s->disconnected) {
774 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
778 if (bgscan_notify_scan(wpa_s) == 1)
781 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
783 wpa_supplicant_connect(wpa_s, selected, ssid);
785 wpa_printf(MSG_DEBUG, "No suitable network found");
786 ssid = wpa_supplicant_pick_new_network(wpa_s);
788 wpa_printf(MSG_DEBUG, "Setup a new network");
789 wpa_supplicant_associate(wpa_s, NULL, ssid);
791 wpa_supplicant_req_new_scan(wpa_s, 5);
794 #endif /* CONFIG_NO_SCAN_PROCESSING */
797 #ifdef CONFIG_IEEE80211R
798 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
799 const u8 *ftie, const u8 *mdie)
801 const u8 *mobility_domain = NULL;
802 const u8 *r0kh_id = NULL;
803 size_t r0kh_id_len = 0;
804 const u8 *r1kh_id = NULL;
805 struct rsn_ftie *hdr;
808 if (mdie == NULL || ftie == NULL)
811 if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
812 mobility_domain = mdie + 2;
814 wpa_s->sme.ft_used = 1;
815 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
816 #endif /* CONFIG_SME */
818 if (ftie[1] >= sizeof(struct rsn_ftie)) {
819 end = ftie + 2 + ftie[1];
820 hdr = (struct rsn_ftie *) (ftie + 2);
821 pos = (const u8 *) (hdr + 1);
822 while (pos + 1 < end) {
823 if (pos + 2 + pos[1] > end)
825 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
826 pos[1] == FT_R1KH_ID_LEN)
828 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
829 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
831 r0kh_id_len = pos[1];
836 wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
837 r0kh_id_len, r1kh_id);
839 #endif /* CONFIG_IEEE80211R */
841 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
842 union wpa_event_data *data)
844 int l, len, found = 0, wpa_found, rsn_found;
846 #ifdef CONFIG_IEEE80211R
847 const u8 *mdie = NULL, *ftie = NULL;
848 #endif /* CONFIG_IEEE80211R */
850 wpa_printf(MSG_DEBUG, "Association info event");
851 if (data->assoc_info.req_ies)
852 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
853 data->assoc_info.req_ies_len);
854 if (data->assoc_info.resp_ies)
855 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
856 data->assoc_info.resp_ies_len);
857 if (data->assoc_info.beacon_ies)
858 wpa_hexdump(MSG_DEBUG, "beacon_ies",
859 data->assoc_info.beacon_ies,
860 data->assoc_info.beacon_ies_len);
862 p = data->assoc_info.req_ies;
863 l = data->assoc_info.req_ies_len;
865 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
866 while (p && l >= 2) {
869 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
873 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
874 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
875 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
876 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
879 wpa_find_assoc_pmkid(wpa_s);
885 if (!found && data->assoc_info.req_ies)
886 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
888 #ifdef CONFIG_IEEE80211R
889 p = data->assoc_info.resp_ies;
890 l = data->assoc_info.resp_ies_len;
892 /* Go through the IEs and make a copy of the FT/MD IE, if present. */
893 while (p && l >= 2) {
896 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
900 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
902 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
908 wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
909 #endif /* CONFIG_IEEE80211R */
911 /* WPA/RSN IE from Beacon/ProbeResp */
912 p = data->assoc_info.beacon_ies;
913 l = data->assoc_info.beacon_ies_len;
915 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
917 wpa_found = rsn_found = 0;
918 while (p && l >= 2) {
921 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
926 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
927 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
929 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
933 p[0] == WLAN_EID_RSN && p[1] >= 2) {
935 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
942 if (!wpa_found && data->assoc_info.beacon_ies)
943 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
944 if (!rsn_found && data->assoc_info.beacon_ies)
945 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
946 if (wpa_found || rsn_found)
947 wpa_s->ap_ies_from_associnfo = 1;
951 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
952 union wpa_event_data *data)
955 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
957 struct wpa_driver_capa capa;
960 wpa_supplicant_event_associnfo(wpa_s, data);
962 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
963 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
964 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
965 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
966 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
967 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
968 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
969 MACSTR, MAC2STR(bssid));
970 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
971 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
972 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
974 wpas_notify_bssid_changed(wpa_s);
976 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
977 wpa_clear_keys(wpa_s, bssid);
979 if (wpa_supplicant_select_config(wpa_s) < 0) {
980 wpa_supplicant_disassociate(
981 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
987 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
988 wpa_s->sme.prev_bssid_set = 1;
989 #endif /* CONFIG_SME */
991 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
992 if (wpa_s->current_ssid) {
993 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
994 * initialized before association, but for other modes,
995 * initialize PC/SC here, if the current configuration needs
996 * smartcard or SIM/USIM. */
997 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
999 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1000 l2_packet_notify_auth_start(wpa_s->l2);
1003 * Set portEnabled first to FALSE in order to get EAP state machine out
1004 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1005 * state machine may transit to AUTHENTICATING state based on obsolete
1006 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1007 * AUTHENTICATED without ever giving chance to EAP state machine to
1010 if (!ft_completed) {
1011 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1012 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1014 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1015 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1016 /* 802.1X::portControl = Auto */
1017 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1018 wpa_s->eapol_received = 0;
1019 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1020 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1021 wpa_supplicant_cancel_auth_timeout(wpa_s);
1022 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1023 } else if (!ft_completed) {
1024 /* Timeout for receiving the first EAPOL packet */
1025 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1027 wpa_supplicant_cancel_scan(wpa_s);
1029 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1030 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1032 * We are done; the driver will take care of RSN 4-way
1035 wpa_supplicant_cancel_auth_timeout(wpa_s);
1036 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1037 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1038 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1041 if (wpa_s->pending_eapol_rx) {
1042 struct os_time now, age;
1044 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1045 if (age.sec == 0 && age.usec < 100000 &&
1046 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1048 wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1049 "that was received just before association "
1051 wpa_supplicant_rx_eapol(
1052 wpa_s, wpa_s->pending_eapol_rx_src,
1053 wpabuf_head(wpa_s->pending_eapol_rx),
1054 wpabuf_len(wpa_s->pending_eapol_rx));
1056 wpabuf_free(wpa_s->pending_eapol_rx);
1057 wpa_s->pending_eapol_rx = NULL;
1060 #ifdef CONFIG_BGSCAN
1061 if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1062 bgscan_deinit(wpa_s);
1063 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1064 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1065 wpa_printf(MSG_DEBUG, "Failed to initialize "
1068 * Live without bgscan; it is only used as a
1069 * roaming optimization, so the initial
1070 * connection is not affected.
1073 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1075 wpa_s->bgscan_ssid = NULL;
1077 #endif /* CONFIG_BGSCAN */
1079 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1080 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1081 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1082 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1083 /* Set static WEP keys again */
1084 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1089 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
1094 u8 prev_pending_bssid[ETH_ALEN];
1096 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1097 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1098 #endif /* CONFIG_SME */
1100 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1102 * At least Host AP driver and a Prism3 card seemed to be
1103 * generating streams of disconnected events when configuring
1104 * IBSS for WPA-None. Ignore them for now.
1106 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1107 "IBSS/WPA-None mode");
1111 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1112 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1113 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1114 "pre-shared key may be incorrect");
1116 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1117 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1118 bssid = wpa_s->bssid;
1119 if (is_zero_ether_addr(bssid))
1120 bssid = wpa_s->pending_bssid;
1121 wpa_blacklist_add(wpa_s, bssid);
1122 wpa_sm_notify_disassoc(wpa_s->wpa);
1123 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
1125 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1126 wpa_s->keys_cleared = 0;
1127 wpa_clear_keys(wpa_s, wpa_s->bssid);
1129 wpa_supplicant_mark_disassoc(wpa_s);
1130 bgscan_deinit(wpa_s);
1132 if (authenticating &&
1133 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1135 * mac80211-workaround to force deauth on failed auth cmd,
1136 * requires us to remain in authenticating state to allow the
1137 * second authentication attempt to be continued properly.
1139 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1140 "proceed after disconnection event");
1141 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1142 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1144 #endif /* CONFIG_SME */
1148 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1149 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1152 struct wpa_supplicant *wpa_s = eloop_ctx;
1154 if (!wpa_s->pending_mic_error_report)
1157 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1158 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1159 wpa_s->pending_mic_error_report = 0;
1161 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1165 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1166 union wpa_event_data *data)
1171 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1172 pairwise = (data && data->michael_mic_failure.unicast);
1174 if ((wpa_s->last_michael_mic_error &&
1175 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1176 wpa_s->pending_mic_error_report) {
1177 if (wpa_s->pending_mic_error_report) {
1179 * Send the pending MIC error report immediately since
1180 * we are going to start countermeasures and AP better
1183 wpa_sm_key_request(wpa_s->wpa, 1,
1184 wpa_s->pending_mic_error_pairwise);
1187 /* Send the new MIC error report immediately since we are going
1188 * to start countermeasures and AP better do the same.
1190 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1192 /* initialize countermeasures */
1193 wpa_s->countermeasures = 1;
1194 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1197 * Need to wait for completion of request frame. We do not get
1198 * any callback for the message completion, so just wait a
1199 * short while and hope for the best. */
1202 wpa_drv_set_countermeasures(wpa_s, 1);
1203 wpa_supplicant_deauthenticate(wpa_s,
1204 WLAN_REASON_MICHAEL_MIC_FAILURE);
1205 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1207 eloop_register_timeout(60, 0,
1208 wpa_supplicant_stop_countermeasures,
1210 /* TODO: mark the AP rejected for 60 second. STA is
1211 * allowed to associate with another AP.. */
1213 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1214 if (wpa_s->mic_errors_seen) {
1216 * Reduce the effectiveness of Michael MIC error
1217 * reports as a means for attacking against TKIP if
1218 * more than one MIC failure is noticed with the same
1219 * PTK. We delay the transmission of the reports by a
1220 * random time between 0 and 60 seconds in order to
1221 * force the attacker wait 60 seconds before getting
1222 * the information on whether a frame resulted in a MIC
1228 if (os_get_random(rval, sizeof(rval)) < 0)
1229 sec = os_random() % 60;
1231 sec = WPA_GET_BE32(rval) % 60;
1232 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1234 wpa_s->pending_mic_error_report = 1;
1235 wpa_s->pending_mic_error_pairwise = pairwise;
1236 eloop_cancel_timeout(
1237 wpa_supplicant_delayed_mic_error_report,
1239 eloop_register_timeout(
1240 sec, os_random() % 1000000,
1241 wpa_supplicant_delayed_mic_error_report,
1244 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1246 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1247 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1248 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1250 wpa_s->last_michael_mic_error = t.sec;
1251 wpa_s->mic_errors_seen++;
1255 #ifdef CONFIG_TERMINATE_ONLASTIF
1256 static int any_interfaces(struct wpa_supplicant *head)
1258 struct wpa_supplicant *wpa_s;
1260 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1261 if (!wpa_s->interface_removed)
1265 #endif /* CONFIG_TERMINATE_ONLASTIF */
1269 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1270 union wpa_event_data *data)
1272 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1275 switch (data->interface_status.ievent) {
1276 case EVENT_INTERFACE_ADDED:
1277 if (!wpa_s->interface_removed)
1279 wpa_s->interface_removed = 0;
1280 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1281 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1282 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1283 "after interface was added.");
1286 case EVENT_INTERFACE_REMOVED:
1287 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1288 wpa_s->interface_removed = 1;
1289 wpa_supplicant_mark_disassoc(wpa_s);
1290 l2_packet_deinit(wpa_s->l2);
1292 #ifdef CONFIG_TERMINATE_ONLASTIF
1293 /* check if last interface */
1294 if (!any_interfaces(wpa_s->global->ifaces))
1296 #endif /* CONFIG_TERMINATE_ONLASTIF */
1302 #ifdef CONFIG_PEERKEY
1304 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1305 union wpa_event_data *data)
1309 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1311 #endif /* CONFIG_PEERKEY */
1314 #ifdef CONFIG_IEEE80211R
1316 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1317 union wpa_event_data *data)
1322 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1323 data->ft_ies.ies_len,
1324 data->ft_ies.ft_action,
1325 data->ft_ies.target_ap,
1326 data->ft_ies.ric_ies,
1327 data->ft_ies.ric_ies_len) < 0) {
1328 /* TODO: prevent MLME/driver from trying to associate? */
1331 #endif /* CONFIG_IEEE80211R */
1334 #ifdef CONFIG_IBSS_RSN
1335 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1336 union wpa_event_data *data)
1340 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1342 #endif /* CONFIG_IBSS_RSN */
1345 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1346 union wpa_event_data *data)
1348 struct wpa_supplicant *wpa_s = ctx;
1352 sme_event_auth(wpa_s, data);
1355 wpa_supplicant_event_assoc(wpa_s, data);
1358 case EVENT_DISASSOC:
1359 wpa_supplicant_event_disassoc(wpa_s);
1361 case EVENT_MICHAEL_MIC_FAILURE:
1362 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1364 #ifndef CONFIG_NO_SCAN_PROCESSING
1365 case EVENT_SCAN_RESULTS:
1366 wpa_supplicant_event_scan_results(wpa_s);
1368 #endif /* CONFIG_NO_SCAN_PROCESSING */
1369 case EVENT_ASSOCINFO:
1370 wpa_supplicant_event_associnfo(wpa_s, data);
1372 case EVENT_INTERFACE_STATUS:
1373 wpa_supplicant_event_interface_status(wpa_s, data);
1375 case EVENT_PMKID_CANDIDATE:
1376 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1378 #ifdef CONFIG_PEERKEY
1379 case EVENT_STKSTART:
1380 wpa_supplicant_event_stkstart(wpa_s, data);
1382 #endif /* CONFIG_PEERKEY */
1383 #ifdef CONFIG_IEEE80211R
1384 case EVENT_FT_RESPONSE:
1385 wpa_supplicant_event_ft_response(wpa_s, data);
1387 #endif /* CONFIG_IEEE80211R */
1388 #ifdef CONFIG_IBSS_RSN
1389 case EVENT_IBSS_RSN_START:
1390 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1392 #endif /* CONFIG_IBSS_RSN */
1393 case EVENT_ASSOC_REJECT:
1394 sme_event_assoc_reject(wpa_s, data);
1396 case EVENT_AUTH_TIMED_OUT:
1397 sme_event_auth_timed_out(wpa_s, data);
1399 case EVENT_ASSOC_TIMED_OUT:
1400 sme_event_assoc_timed_out(wpa_s, data);
1403 wpa_printf(MSG_INFO, "Unknown event %d", event);