3 * Copyright (c) 2009-2010, 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 "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "common/wpa_common.h"
23 #include "rsn_supp/wpa.h"
24 #include "rsn_supp/pmksa_cache.h"
26 #include "wpa_supplicant_i.h"
28 #include "wpas_glue.h"
29 #include "wps_supplicant.h"
30 #include "p2p_supplicant.h"
32 #include "blacklist.h"
37 #define SME_AUTH_TIMEOUT 5
38 #define SME_ASSOC_TIMEOUT 5
40 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
41 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
42 #ifdef CONFIG_IEEE80211W
43 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
44 #endif /* CONFIG_IEEE80211W */
47 void sme_authenticate(struct wpa_supplicant *wpa_s,
48 struct wpa_bss *bss, struct wpa_ssid *ssid)
50 struct wpa_driver_auth_params params;
51 struct wpa_ssid *old_ssid;
52 #ifdef CONFIG_IEEE80211R
54 #endif /* CONFIG_IEEE80211R */
55 #ifdef CONFIG_IEEE80211R
57 #endif /* CONFIG_IEEE80211R */
61 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
66 wpa_s->current_bss = bss;
68 os_memset(¶ms, 0, sizeof(params));
69 wpa_s->reassociate = 0;
71 params.freq = bss->freq;
72 params.bssid = bss->bssid;
73 params.ssid = bss->ssid;
74 params.ssid_len = bss->ssid_len;
76 if (wpa_s->sme.ssid_len != params.ssid_len ||
77 os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
78 wpa_s->sme.prev_bssid_set = 0;
80 wpa_s->sme.freq = params.freq;
81 os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
82 wpa_s->sme.ssid_len = params.ssid_len;
84 params.auth_alg = WPA_AUTH_ALG_OPEN;
85 #ifdef IEEE8021X_EAPOL
86 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
88 if (ssid->non_leap == 0)
89 params.auth_alg = WPA_AUTH_ALG_LEAP;
91 params.auth_alg |= WPA_AUTH_ALG_LEAP;
94 #endif /* IEEE8021X_EAPOL */
95 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
98 params.auth_alg = ssid->auth_alg;
99 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
100 "0x%x", params.auth_alg);
103 for (i = 0; i < NUM_WEP_KEYS; i++) {
104 if (ssid->wep_key_len[i])
105 params.wep_key[i] = ssid->wep_key[i];
106 params.wep_key_len[i] = ssid->wep_key_len[i];
108 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
110 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
111 os_memset(wpa_s->bssid, 0, ETH_ALEN);
112 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
114 wpas_notify_bssid_changed(wpa_s);
116 if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
117 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
118 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
119 WPA_KEY_MGMT_FT_IEEE8021X |
120 WPA_KEY_MGMT_FT_PSK |
121 WPA_KEY_MGMT_IEEE8021X_SHA256 |
122 WPA_KEY_MGMT_PSK_SHA256))) {
123 int try_opportunistic;
124 try_opportunistic = ssid->proactive_key_caching &&
125 (ssid->proto & WPA_PROTO_RSN);
126 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
128 try_opportunistic) == 0)
129 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
130 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
131 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
132 wpa_s->sme.assoc_req_ie,
133 &wpa_s->sme.assoc_req_ie_len)) {
134 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
135 "key management and encryption suites");
138 } else if (ssid->key_mgmt &
139 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
140 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
141 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
142 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
143 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
144 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
145 wpa_s->sme.assoc_req_ie,
146 &wpa_s->sme.assoc_req_ie_len)) {
147 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
148 "key management and encryption suites (no "
153 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
154 struct wpabuf *wps_ie;
155 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
156 if (wps_ie && wpabuf_len(wps_ie) <=
157 sizeof(wpa_s->sme.assoc_req_ie)) {
158 wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
159 os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
160 wpa_s->sme.assoc_req_ie_len);
162 wpa_s->sme.assoc_req_ie_len = 0;
164 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
165 #endif /* CONFIG_WPS */
167 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
168 wpa_s->sme.assoc_req_ie_len = 0;
171 #ifdef CONFIG_IEEE80211R
172 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
173 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
175 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
177 /* Prepare for the next transition */
178 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
181 if (md && ssid->key_mgmt & (WPA_KEY_MGMT_FT_PSK |
182 WPA_KEY_MGMT_FT_IEEE8021X)) {
183 if (wpa_s->sme.assoc_req_ie_len + 5 <
184 sizeof(wpa_s->sme.assoc_req_ie)) {
185 struct rsn_mdie *mdie;
186 u8 *pos = wpa_s->sme.assoc_req_ie +
187 wpa_s->sme.assoc_req_ie_len;
188 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
189 *pos++ = sizeof(*mdie);
190 mdie = (struct rsn_mdie *) pos;
191 os_memcpy(mdie->mobility_domain, md,
192 MOBILITY_DOMAIN_ID_LEN);
193 mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
194 wpa_s->sme.assoc_req_ie_len += 5;
197 if (wpa_s->sme.ft_used &&
198 os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
199 wpa_sm_has_ptk(wpa_s->wpa)) {
200 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
202 params.auth_alg = WPA_AUTH_ALG_FT;
203 params.ie = wpa_s->sme.ft_ies;
204 params.ie_len = wpa_s->sme.ft_ies_len;
207 #endif /* CONFIG_IEEE80211R */
209 #ifdef CONFIG_IEEE80211W
210 wpa_s->sme.mfp = ssid->ieee80211w;
211 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
212 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
213 struct wpa_ie_data _ie;
214 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
216 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
217 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
219 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
222 #endif /* CONFIG_IEEE80211W */
225 if (wpa_s->global->p2p) {
230 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
231 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
232 len = sizeof(wpa_s->sme.assoc_req_ie) -
233 wpa_s->sme.assoc_req_ie_len;
234 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
236 wpa_s->sme.assoc_req_ie_len += res;
238 #endif /* CONFIG_P2P */
240 wpa_supplicant_cancel_scan(wpa_s);
242 wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
243 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
244 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
246 wpa_clear_keys(wpa_s, bss->bssid);
247 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
248 old_ssid = wpa_s->current_ssid;
249 wpa_s->current_ssid = ssid;
250 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
251 wpa_supplicant_initiate_eapol(wpa_s);
252 if (old_ssid != wpa_s->current_ssid)
253 wpas_notify_network_changed(wpa_s);
255 wpa_s->sme.auth_alg = params.auth_alg;
256 if (wpa_drv_authenticate(wpa_s, ¶ms) < 0) {
257 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
259 wpa_supplicant_req_scan(wpa_s, 1, 0);
263 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
267 * Association will be started based on the authentication event from
273 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
275 struct wpa_ssid *ssid = wpa_s->current_ssid;
278 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
279 "when network is not selected");
283 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
284 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
285 "when not in authenticating state");
289 if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
290 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
291 "unexpected peer " MACSTR,
292 MAC2STR(data->auth.peer));
296 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
297 " auth_type=%d status_code=%d",
298 MAC2STR(data->auth.peer), data->auth.auth_type,
299 data->auth.status_code);
300 wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
301 data->auth.ies, data->auth.ies_len);
303 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
305 if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
306 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
307 "code %d)", data->auth.status_code);
309 if (data->auth.status_code !=
310 WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
311 wpa_s->sme.auth_alg == data->auth.auth_type ||
312 wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
313 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
317 switch (data->auth.auth_type) {
319 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
321 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
322 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
323 wpa_s->current_ssid);
326 case WLAN_AUTH_SHARED_KEY:
327 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
329 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
330 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
331 wpa_s->current_ssid);
339 #ifdef CONFIG_IEEE80211R
340 if (data->auth.auth_type == WLAN_AUTH_FT) {
341 union wpa_event_data edata;
342 os_memset(&edata, 0, sizeof(edata));
343 edata.ft_ies.ies = data->auth.ies;
344 edata.ft_ies.ies_len = data->auth.ies_len;
345 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
346 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
348 #endif /* CONFIG_IEEE80211R */
350 sme_associate(wpa_s, ssid->mode, data->auth.peer,
351 data->auth.auth_type);
355 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
356 const u8 *bssid, u16 auth_type)
358 struct wpa_driver_associate_params params;
359 struct ieee802_11_elems elems;
361 os_memset(¶ms, 0, sizeof(params));
362 params.bssid = bssid;
363 params.ssid = wpa_s->sme.ssid;
364 params.ssid_len = wpa_s->sme.ssid_len;
365 params.freq = wpa_s->sme.freq;
366 params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
367 wpa_s->sme.assoc_req_ie : NULL;
368 params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
369 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
370 params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
371 #ifdef CONFIG_IEEE80211R
372 if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
373 params.wpa_ie = wpa_s->sme.ft_ies;
374 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
376 #endif /* CONFIG_IEEE80211R */
378 params.mgmt_frame_protection = wpa_s->sme.mfp;
379 if (wpa_s->sme.prev_bssid_set)
380 params.prev_bssid = wpa_s->sme.prev_bssid;
382 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
383 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
384 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
387 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
389 if (params.wpa_ie == NULL ||
390 ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
392 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
393 os_memset(&elems, 0, sizeof(elems));
396 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
397 elems.rsn_ie_len + 2);
398 else if (elems.wpa_ie)
399 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
400 elems.wpa_ie_len + 2);
402 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
404 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
407 if (wpa_s->parent->set_sta_uapsd)
408 params.uapsd = wpa_s->parent->sta_uapsd;
412 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
413 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
415 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
416 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
420 eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
425 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
426 const u8 *ies, size_t ies_len)
428 if (md == NULL || ies == NULL) {
429 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
430 os_free(wpa_s->sme.ft_ies);
431 wpa_s->sme.ft_ies = NULL;
432 wpa_s->sme.ft_ies_len = 0;
433 wpa_s->sme.ft_used = 0;
437 os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
438 wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
439 os_free(wpa_s->sme.ft_ies);
440 wpa_s->sme.ft_ies = os_malloc(ies_len);
441 if (wpa_s->sme.ft_ies == NULL)
443 os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
444 wpa_s->sme.ft_ies_len = ies_len;
449 static void sme_deauth(struct wpa_supplicant *wpa_s)
453 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
455 if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
456 WLAN_REASON_DEAUTH_LEAVING) < 0) {
457 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
460 wpa_s->sme.prev_bssid_set = 0;
462 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
463 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
464 os_memset(wpa_s->bssid, 0, ETH_ALEN);
465 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
467 wpas_notify_bssid_changed(wpa_s);
471 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
472 union wpa_event_data *data)
474 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
475 "status code %d", MAC2STR(wpa_s->pending_bssid),
476 data->assoc_reject.status_code);
478 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
481 * For now, unconditionally terminate the previous authentication. In
482 * theory, this should not be needed, but mac80211 gets quite confused
483 * if the authentication is left pending.. Some roaming cases might
484 * benefit from using the previous authentication, so this could be
485 * optimized in the future.
491 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
492 union wpa_event_data *data)
494 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
495 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
499 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
500 union wpa_event_data *data)
502 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
503 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
504 wpa_supplicant_mark_disassoc(wpa_s);
508 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
509 union wpa_event_data *data)
511 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
512 if (wpa_s->sme.prev_bssid_set &&
513 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)) {
515 * cfg80211/mac80211 can get into somewhat confused state if
516 * the AP only disassociates us and leaves us in authenticated
517 * state. For now, force the state to be cleared to avoid
518 * confusing errors if we try to associate with the AP again.
520 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
522 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
523 WLAN_REASON_DEAUTH_LEAVING);
528 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
530 struct wpa_supplicant *wpa_s = eloop_ctx;
531 if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
532 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
538 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
540 struct wpa_supplicant *wpa_s = eloop_ctx;
541 if (wpa_s->wpa_state == WPA_ASSOCIATING) {
542 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
548 void sme_state_changed(struct wpa_supplicant *wpa_s)
550 /* Make sure timers are cleaned up appropriately. */
551 if (wpa_s->wpa_state != WPA_ASSOCIATING)
552 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
553 if (wpa_s->wpa_state != WPA_AUTHENTICATING)
554 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
558 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
559 const u8 *prev_pending_bssid)
562 * mac80211-workaround to force deauth on failed auth cmd,
563 * requires us to remain in authenticating state to allow the
564 * second authentication attempt to be continued properly.
566 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
567 "to proceed after disconnection event");
568 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
569 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
572 * Re-arm authentication timer in case auth fails for whatever reason.
574 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
575 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
580 void sme_deinit(struct wpa_supplicant *wpa_s)
582 os_free(wpa_s->sme.ft_ies);
583 wpa_s->sme.ft_ies = NULL;
584 wpa_s->sme.ft_ies_len = 0;
585 #ifdef CONFIG_IEEE80211W
586 sme_stop_sa_query(wpa_s);
587 #endif /* CONFIG_IEEE80211W */
589 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
590 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
594 #ifdef CONFIG_IEEE80211W
596 static const unsigned int sa_query_max_timeout = 1000;
597 static const unsigned int sa_query_retry_timeout = 201;
599 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
602 struct os_time now, passed;
604 os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
605 tu = (passed.sec * 1000000 + passed.usec) / 1024;
606 if (sa_query_max_timeout < tu) {
607 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
608 sme_stop_sa_query(wpa_s);
609 wpa_supplicant_deauthenticate(
610 wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
618 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
621 u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
622 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
623 MACSTR, MAC2STR(wpa_s->bssid));
624 wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
625 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
626 req[0] = WLAN_ACTION_SA_QUERY;
627 req[1] = WLAN_SA_QUERY_REQUEST;
628 os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
629 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
630 wpa_s->own_addr, wpa_s->bssid,
631 req, sizeof(req)) < 0)
632 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
637 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
639 struct wpa_supplicant *wpa_s = eloop_ctx;
640 unsigned int timeout, sec, usec;
643 if (wpa_s->sme.sa_query_count > 0 &&
644 sme_check_sa_query_timeout(wpa_s))
647 nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
648 (wpa_s->sme.sa_query_count + 1) *
649 WLAN_SA_QUERY_TR_ID_LEN);
652 if (wpa_s->sme.sa_query_count == 0) {
653 /* Starting a new SA Query procedure */
654 os_get_time(&wpa_s->sme.sa_query_start);
656 trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
657 wpa_s->sme.sa_query_trans_id = nbuf;
658 wpa_s->sme.sa_query_count++;
660 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
662 timeout = sa_query_retry_timeout;
663 sec = ((timeout / 1000) * 1024) / 1000;
664 usec = (timeout % 1000) * 1024;
665 eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
667 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
668 wpa_s->sme.sa_query_count);
670 sme_send_sa_query_req(wpa_s, trans_id);
674 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
676 sme_sa_query_timer(wpa_s, NULL);
680 void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
682 eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
683 os_free(wpa_s->sme.sa_query_trans_id);
684 wpa_s->sme.sa_query_trans_id = NULL;
685 wpa_s->sme.sa_query_count = 0;
689 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
690 const u8 *da, u16 reason_code)
692 struct wpa_ssid *ssid;
694 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
696 if (wpa_s->wpa_state != WPA_COMPLETED)
698 ssid = wpa_s->current_ssid;
699 if (ssid == NULL || ssid->ieee80211w == 0)
701 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
703 if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
704 reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
706 if (wpa_s->sme.sa_query_count > 0)
709 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
710 "possible AP/STA state mismatch - trigger SA Query");
711 sme_start_sa_query(wpa_s);
715 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
716 const u8 *data, size_t len)
720 if (wpa_s->sme.sa_query_trans_id == NULL ||
721 len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
722 data[0] != WLAN_SA_QUERY_RESPONSE)
724 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
725 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
727 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
730 for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
731 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
732 i * WLAN_SA_QUERY_TR_ID_LEN,
733 data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
737 if (i >= wpa_s->sme.sa_query_count) {
738 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
739 "transaction identifier found");
743 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
744 "from " MACSTR, MAC2STR(sa));
745 sme_stop_sa_query(wpa_s);
748 #endif /* CONFIG_IEEE80211W */