3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "eapol_supp/eapol_supp_sm.h"
16 #include "common/wpa_common.h"
17 #include "rsn_supp/wpa.h"
18 #include "rsn_supp/pmksa_cache.h"
20 #include "wpa_supplicant_i.h"
22 #include "wpas_glue.h"
23 #include "wps_supplicant.h"
24 #include "p2p_supplicant.h"
29 #include "hs20_supplicant.h"
31 #define SME_AUTH_TIMEOUT 5
32 #define SME_ASSOC_TIMEOUT 5
34 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
35 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
36 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
37 #ifdef CONFIG_IEEE80211W
38 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
39 #endif /* CONFIG_IEEE80211W */
42 void sme_authenticate(struct wpa_supplicant *wpa_s,
43 struct wpa_bss *bss, struct wpa_ssid *ssid)
45 struct wpa_driver_auth_params params;
46 struct wpa_ssid *old_ssid;
47 #ifdef CONFIG_IEEE80211R
49 #endif /* CONFIG_IEEE80211R */
50 #ifdef CONFIG_IEEE80211R
52 #endif /* CONFIG_IEEE80211R */
56 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
61 wpa_s->current_bss = bss;
63 os_memset(¶ms, 0, sizeof(params));
64 wpa_s->reassociate = 0;
66 params.freq = bss->freq;
67 params.bssid = bss->bssid;
68 params.ssid = bss->ssid;
69 params.ssid_len = bss->ssid_len;
70 params.p2p = ssid->p2p_group;
72 if (wpa_s->sme.ssid_len != params.ssid_len ||
73 os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
74 wpa_s->sme.prev_bssid_set = 0;
76 wpa_s->sme.freq = params.freq;
77 os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
78 wpa_s->sme.ssid_len = params.ssid_len;
80 params.auth_alg = WPA_AUTH_ALG_OPEN;
81 #ifdef IEEE8021X_EAPOL
82 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
84 if (ssid->non_leap == 0)
85 params.auth_alg = WPA_AUTH_ALG_LEAP;
87 params.auth_alg |= WPA_AUTH_ALG_LEAP;
90 #endif /* IEEE8021X_EAPOL */
91 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
94 params.auth_alg = ssid->auth_alg;
95 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
96 "0x%x", params.auth_alg);
99 for (i = 0; i < NUM_WEP_KEYS; i++) {
100 if (ssid->wep_key_len[i])
101 params.wep_key[i] = ssid->wep_key[i];
102 params.wep_key_len[i] = ssid->wep_key_len[i];
104 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
106 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
107 os_memset(wpa_s->bssid, 0, ETH_ALEN);
108 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
110 wpas_notify_bssid_changed(wpa_s);
112 if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
113 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
114 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
115 int try_opportunistic;
116 try_opportunistic = ssid->proactive_key_caching &&
117 (ssid->proto & WPA_PROTO_RSN);
118 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
120 try_opportunistic) == 0)
121 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
122 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
123 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
124 wpa_s->sme.assoc_req_ie,
125 &wpa_s->sme.assoc_req_ie_len)) {
126 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
127 "key management and encryption suites");
130 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
131 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
132 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
133 wpa_s->sme.assoc_req_ie,
134 &wpa_s->sme.assoc_req_ie_len)) {
135 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
136 "key management and encryption suites (no "
141 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
142 struct wpabuf *wps_ie;
143 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
144 if (wps_ie && wpabuf_len(wps_ie) <=
145 sizeof(wpa_s->sme.assoc_req_ie)) {
146 wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
147 os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
148 wpa_s->sme.assoc_req_ie_len);
150 wpa_s->sme.assoc_req_ie_len = 0;
152 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
153 #endif /* CONFIG_WPS */
155 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
156 wpa_s->sme.assoc_req_ie_len = 0;
159 #ifdef CONFIG_IEEE80211R
160 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
161 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
163 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
165 /* Prepare for the next transition */
166 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
169 if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
170 if (wpa_s->sme.assoc_req_ie_len + 5 <
171 sizeof(wpa_s->sme.assoc_req_ie)) {
172 struct rsn_mdie *mdie;
173 u8 *pos = wpa_s->sme.assoc_req_ie +
174 wpa_s->sme.assoc_req_ie_len;
175 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
176 *pos++ = sizeof(*mdie);
177 mdie = (struct rsn_mdie *) pos;
178 os_memcpy(mdie->mobility_domain, md,
179 MOBILITY_DOMAIN_ID_LEN);
180 mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
181 wpa_s->sme.assoc_req_ie_len += 5;
184 if (wpa_s->sme.ft_used &&
185 os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
186 wpa_sm_has_ptk(wpa_s->wpa)) {
187 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
189 params.auth_alg = WPA_AUTH_ALG_FT;
190 params.ie = wpa_s->sme.ft_ies;
191 params.ie_len = wpa_s->sme.ft_ies_len;
194 #endif /* CONFIG_IEEE80211R */
196 #ifdef CONFIG_IEEE80211W
197 wpa_s->sme.mfp = ssid->ieee80211w;
198 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
199 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
200 struct wpa_ie_data _ie;
201 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
203 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
204 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
206 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
209 #endif /* CONFIG_IEEE80211W */
212 if (wpa_s->global->p2p) {
216 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
217 len = sizeof(wpa_s->sme.assoc_req_ie) -
218 wpa_s->sme.assoc_req_ie_len;
219 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
222 wpa_s->sme.assoc_req_ie_len += res;
224 #endif /* CONFIG_P2P */
227 if (wpa_s->conf->hs20) {
229 hs20 = wpabuf_alloc(20);
231 wpas_hs20_add_indication(hs20);
232 os_memcpy(wpa_s->sme.assoc_req_ie +
233 wpa_s->sme.assoc_req_ie_len,
234 wpabuf_head(hs20), wpabuf_len(hs20));
235 wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
239 #endif /* CONFIG_HS20 */
241 #ifdef CONFIG_INTERWORKING
242 if (wpa_s->conf->interworking) {
243 u8 *pos = wpa_s->sme.assoc_req_ie;
244 if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
246 os_memmove(pos + 6, pos,
247 wpa_s->sme.assoc_req_ie_len -
248 (pos - wpa_s->sme.assoc_req_ie));
249 wpa_s->sme.assoc_req_ie_len += 6;
250 *pos++ = WLAN_EID_EXT_CAPAB;
255 *pos++ = 0x80; /* Bit 31 - Interworking */
257 #endif /* CONFIG_INTERWORKING */
259 wpa_supplicant_cancel_sched_scan(wpa_s);
260 wpa_supplicant_cancel_scan(wpa_s);
262 wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
263 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
264 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
266 wpa_clear_keys(wpa_s, bss->bssid);
267 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
268 old_ssid = wpa_s->current_ssid;
269 wpa_s->current_ssid = ssid;
270 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
271 wpa_supplicant_initiate_eapol(wpa_s);
272 if (old_ssid != wpa_s->current_ssid)
273 wpas_notify_network_changed(wpa_s);
275 wpa_s->sme.auth_alg = params.auth_alg;
276 if (wpa_drv_authenticate(wpa_s, ¶ms) < 0) {
277 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
279 wpas_connection_failed(wpa_s, bss->bssid);
280 wpa_supplicant_mark_disassoc(wpa_s);
284 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
288 * Association will be started based on the authentication event from
294 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
296 struct wpa_ssid *ssid = wpa_s->current_ssid;
299 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
300 "when network is not selected");
304 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
305 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
306 "when not in authenticating state");
310 if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
311 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
312 "unexpected peer " MACSTR,
313 MAC2STR(data->auth.peer));
317 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
318 " auth_type=%d status_code=%d",
319 MAC2STR(data->auth.peer), data->auth.auth_type,
320 data->auth.status_code);
321 wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
322 data->auth.ies, data->auth.ies_len);
324 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
326 if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
327 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
328 "code %d)", data->auth.status_code);
330 if (data->auth.status_code !=
331 WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
332 wpa_s->sme.auth_alg == data->auth.auth_type ||
333 wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
334 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
335 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
339 switch (data->auth.auth_type) {
341 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
343 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
344 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
345 wpa_s->current_ssid);
348 case WLAN_AUTH_SHARED_KEY:
349 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
351 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
352 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
353 wpa_s->current_ssid);
361 #ifdef CONFIG_IEEE80211R
362 if (data->auth.auth_type == WLAN_AUTH_FT) {
363 union wpa_event_data edata;
364 os_memset(&edata, 0, sizeof(edata));
365 edata.ft_ies.ies = data->auth.ies;
366 edata.ft_ies.ies_len = data->auth.ies_len;
367 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
368 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
370 #endif /* CONFIG_IEEE80211R */
372 sme_associate(wpa_s, ssid->mode, data->auth.peer,
373 data->auth.auth_type);
377 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
378 const u8 *bssid, u16 auth_type)
380 struct wpa_driver_associate_params params;
381 struct ieee802_11_elems elems;
382 #ifdef CONFIG_HT_OVERRIDES
383 struct ieee80211_ht_capabilities htcaps;
384 struct ieee80211_ht_capabilities htcaps_mask;
385 #endif /* CONFIG_HT_OVERRIDES */
387 os_memset(¶ms, 0, sizeof(params));
388 params.bssid = bssid;
389 params.ssid = wpa_s->sme.ssid;
390 params.ssid_len = wpa_s->sme.ssid_len;
391 params.freq = wpa_s->sme.freq;
392 params.bg_scan_period = wpa_s->current_ssid ?
393 wpa_s->current_ssid->bg_scan_period : -1;
394 params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
395 wpa_s->sme.assoc_req_ie : NULL;
396 params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
397 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
398 params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
399 #ifdef CONFIG_HT_OVERRIDES
400 os_memset(&htcaps, 0, sizeof(htcaps));
401 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
402 params.htcaps = (u8 *) &htcaps;
403 params.htcaps_mask = (u8 *) &htcaps_mask;
404 wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, ¶ms);
405 #endif /* CONFIG_HT_OVERRIDES */
406 #ifdef CONFIG_IEEE80211R
407 if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
408 params.wpa_ie = wpa_s->sme.ft_ies;
409 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
411 #endif /* CONFIG_IEEE80211R */
413 params.mgmt_frame_protection = wpa_s->sme.mfp;
414 if (wpa_s->sme.prev_bssid_set)
415 params.prev_bssid = wpa_s->sme.prev_bssid;
417 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
418 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
419 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
422 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
424 if (params.wpa_ie == NULL ||
425 ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
427 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
428 os_memset(&elems, 0, sizeof(elems));
431 params.wpa_proto = WPA_PROTO_RSN;
432 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
433 elems.rsn_ie_len + 2);
434 } else if (elems.wpa_ie) {
435 params.wpa_proto = WPA_PROTO_WPA;
436 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
437 elems.wpa_ie_len + 2);
439 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
440 if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
443 if (wpa_s->parent->set_sta_uapsd)
444 params.uapsd = wpa_s->parent->sta_uapsd;
448 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
449 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
451 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
452 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
453 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
457 eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
462 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
463 const u8 *ies, size_t ies_len)
465 if (md == NULL || ies == NULL) {
466 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
467 os_free(wpa_s->sme.ft_ies);
468 wpa_s->sme.ft_ies = NULL;
469 wpa_s->sme.ft_ies_len = 0;
470 wpa_s->sme.ft_used = 0;
474 os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
475 wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
476 os_free(wpa_s->sme.ft_ies);
477 wpa_s->sme.ft_ies = os_malloc(ies_len);
478 if (wpa_s->sme.ft_ies == NULL)
480 os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
481 wpa_s->sme.ft_ies_len = ies_len;
486 static void sme_deauth(struct wpa_supplicant *wpa_s)
490 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
492 if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
493 WLAN_REASON_DEAUTH_LEAVING) < 0) {
494 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
497 wpa_s->sme.prev_bssid_set = 0;
499 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
500 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
501 os_memset(wpa_s->bssid, 0, ETH_ALEN);
502 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
504 wpas_notify_bssid_changed(wpa_s);
508 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
509 union wpa_event_data *data)
511 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
512 "status code %d", MAC2STR(wpa_s->pending_bssid),
513 data->assoc_reject.status_code);
515 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
518 * For now, unconditionally terminate the previous authentication. In
519 * theory, this should not be needed, but mac80211 gets quite confused
520 * if the authentication is left pending.. Some roaming cases might
521 * benefit from using the previous authentication, so this could be
522 * optimized in the future.
528 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
529 union wpa_event_data *data)
531 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
532 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
533 wpa_supplicant_mark_disassoc(wpa_s);
537 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
538 union wpa_event_data *data)
540 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
541 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
542 wpa_supplicant_mark_disassoc(wpa_s);
546 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
547 union wpa_event_data *data)
549 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
550 if (wpa_s->sme.prev_bssid_set) {
552 * cfg80211/mac80211 can get into somewhat confused state if
553 * the AP only disassociates us and leaves us in authenticated
554 * state. For now, force the state to be cleared to avoid
555 * confusing errors if we try to associate with the AP again.
557 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
559 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
560 WLAN_REASON_DEAUTH_LEAVING);
565 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
567 struct wpa_supplicant *wpa_s = eloop_ctx;
568 if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
569 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
575 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
577 struct wpa_supplicant *wpa_s = eloop_ctx;
578 if (wpa_s->wpa_state == WPA_ASSOCIATING) {
579 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
585 void sme_state_changed(struct wpa_supplicant *wpa_s)
587 /* Make sure timers are cleaned up appropriately. */
588 if (wpa_s->wpa_state != WPA_ASSOCIATING)
589 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
590 if (wpa_s->wpa_state != WPA_AUTHENTICATING)
591 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
595 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
596 const u8 *prev_pending_bssid)
599 * mac80211-workaround to force deauth on failed auth cmd,
600 * requires us to remain in authenticating state to allow the
601 * second authentication attempt to be continued properly.
603 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
604 "to proceed after disconnection event");
605 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
606 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
609 * Re-arm authentication timer in case auth fails for whatever reason.
611 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
612 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
617 void sme_deinit(struct wpa_supplicant *wpa_s)
619 os_free(wpa_s->sme.ft_ies);
620 wpa_s->sme.ft_ies = NULL;
621 wpa_s->sme.ft_ies_len = 0;
622 #ifdef CONFIG_IEEE80211W
623 sme_stop_sa_query(wpa_s);
624 #endif /* CONFIG_IEEE80211W */
626 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
627 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
628 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
632 static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
633 const u8 *chan_list, u8 num_channels,
636 struct ieee80211_2040_bss_coex_ie *bc_ie;
637 struct ieee80211_2040_intol_chan_report *ic_report;
640 wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR,
641 MAC2STR(wpa_s->bssid));
643 buf = wpabuf_alloc(2 + /* action.category + action_code */
644 sizeof(struct ieee80211_2040_bss_coex_ie) +
645 sizeof(struct ieee80211_2040_intol_chan_report) +
650 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
651 wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
653 bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
654 bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
657 bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
659 if (num_channels > 0) {
660 ic_report = wpabuf_put(buf, sizeof(*ic_report));
661 ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
662 ic_report->length = num_channels + 1;
663 ic_report->op_class = 0;
664 os_memcpy(wpabuf_put(buf, num_channels), chan_list,
668 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
669 wpa_s->own_addr, wpa_s->bssid,
670 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
671 wpa_msg(wpa_s, MSG_INFO,
672 "SME: Failed to send 20/40 BSS Coexistence frame");
680 * enum wpas_band - Frequency band
681 * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
682 * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
691 * freq_to_channel - Convert frequency into channel info
692 * @channel: Buffer for returning channel number
693 * Returns: Band (2 or 5 GHz)
695 static enum wpas_band freq_to_channel(int freq, u8 *channel)
697 enum wpas_band band = (freq <= 2484) ? WPAS_BAND_2GHZ : WPAS_BAND_5GHZ;
700 if (freq >= 2412 && freq <= 2472)
701 chan = (freq - 2407) / 5;
702 else if (freq == 2484)
704 else if (freq >= 5180 && freq <= 5805)
705 chan = (freq - 5000) / 5;
712 int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
717 u8 chan_list[P2P_MAX_CHANNELS], channel;
718 u8 num_channels = 0, num_intol = 0, i;
720 if (!wpa_s->sme.sched_obss_scan)
723 wpa_s->sme.sched_obss_scan = 0;
724 if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
728 * Check whether AP uses regulatory triplet or channel triplet in
729 * country info. Right now the operating class of the BSS channel
730 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
731 * based on the assumption that operating class triplet is not used in
732 * beacon frame. If the First Channel Number/Operating Extension
733 * Identifier octet has a positive integer value of 201 or greater,
734 * then its operating class triplet.
736 * TODO: If Supported Operating Classes element is present in beacon
737 * frame, have to lookup operating class in Annex E and fill them in
740 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
741 if (ie && (ie[1] >= 6) && (ie[5] >= 201))
744 os_memset(chan_list, 0, sizeof(chan_list));
746 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
747 /* Skip other band bss */
748 if (freq_to_channel(bss->freq, &channel) != WPAS_BAND_2GHZ)
751 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
752 ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
754 if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
755 /* Check whether the channel is already considered */
756 for (i = 0; i < num_channels; i++) {
757 if (channel == chan_list[i])
760 if (i != num_channels)
763 if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
766 chan_list[num_channels++] = channel;
770 sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
775 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
777 enum hostapd_hw_mode mode)
781 for (i = 0; i < num_modes; i++) {
782 if (modes[i].mode == mode)
790 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
791 enum hostapd_hw_mode band,
792 struct wpa_driver_scan_params *params)
794 /* Include only supported channels for the specified band */
795 struct hostapd_hw_modes *mode;
798 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
800 /* No channels supported in this band - use empty list */
801 params->freqs = os_zalloc(sizeof(int));
805 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
806 if (params->freqs == NULL)
808 for (count = 0, i = 0; i < mode->num_channels; i++) {
809 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
811 params->freqs[count++] = mode->channels[i].freq;
816 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
818 struct wpa_supplicant *wpa_s = eloop_ctx;
819 struct wpa_driver_scan_params params;
821 if (!wpa_s->current_bss) {
822 wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
826 os_memset(¶ms, 0, sizeof(params));
827 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, ¶ms);
828 wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
830 if (wpa_supplicant_trigger_scan(wpa_s, ¶ms))
831 wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
833 wpa_s->sme.sched_obss_scan = 1;
834 os_free(params.freqs);
836 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
837 sme_obss_scan_timeout, wpa_s, NULL);
841 void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
844 struct wpa_bss *bss = wpa_s->current_bss;
845 struct wpa_ssid *ssid = wpa_s->current_ssid;
846 struct hostapd_hw_modes *hw_mode = NULL;
849 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
850 wpa_s->sme.sched_obss_scan = 0;
854 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) || ssid == NULL ||
855 ssid->mode != IEEE80211_MODE_INFRA)
856 return; /* Not using station SME in wpa_supplicant */
858 if (!wpa_s->hw.modes)
861 /* only HT caps in 11g mode are relevant */
862 for (i = 0; i < wpa_s->hw.num_modes; i++) {
863 hw_mode = &wpa_s->hw.modes[i];
864 if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
868 /* Driver does not support HT40 for 11g or doesn't have 11g. */
869 if (i == wpa_s->hw.num_modes || !hw_mode ||
870 !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
873 if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
874 return; /* Not associated on 2.4 GHz band */
876 /* Check whether AP supports HT40 */
877 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
878 if (!ie || ie[1] < 2 ||
879 !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
880 return; /* AP does not support HT40 */
882 ie = wpa_bss_get_ie(wpa_s->current_bss,
883 WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
884 if (!ie || ie[1] < 14)
885 return; /* AP does not request OBSS scans */
887 wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
888 if (wpa_s->sme.obss_scan_int < 10) {
889 wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
890 "replaced with the minimum 10 sec",
891 wpa_s->sme.obss_scan_int);
892 wpa_s->sme.obss_scan_int = 10;
894 wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
895 wpa_s->sme.obss_scan_int);
896 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
897 sme_obss_scan_timeout, wpa_s, NULL);
901 #ifdef CONFIG_IEEE80211W
903 static const unsigned int sa_query_max_timeout = 1000;
904 static const unsigned int sa_query_retry_timeout = 201;
906 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
909 struct os_time now, passed;
911 os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
912 tu = (passed.sec * 1000000 + passed.usec) / 1024;
913 if (sa_query_max_timeout < tu) {
914 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
915 sme_stop_sa_query(wpa_s);
916 wpa_supplicant_deauthenticate(
917 wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
925 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
928 u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
929 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
930 MACSTR, MAC2STR(wpa_s->bssid));
931 wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
932 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
933 req[0] = WLAN_ACTION_SA_QUERY;
934 req[1] = WLAN_SA_QUERY_REQUEST;
935 os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
936 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
937 wpa_s->own_addr, wpa_s->bssid,
938 req, sizeof(req), 0) < 0)
939 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
944 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
946 struct wpa_supplicant *wpa_s = eloop_ctx;
947 unsigned int timeout, sec, usec;
950 if (wpa_s->sme.sa_query_count > 0 &&
951 sme_check_sa_query_timeout(wpa_s))
954 nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
955 (wpa_s->sme.sa_query_count + 1) *
956 WLAN_SA_QUERY_TR_ID_LEN);
959 if (wpa_s->sme.sa_query_count == 0) {
960 /* Starting a new SA Query procedure */
961 os_get_time(&wpa_s->sme.sa_query_start);
963 trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
964 wpa_s->sme.sa_query_trans_id = nbuf;
965 wpa_s->sme.sa_query_count++;
967 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
969 timeout = sa_query_retry_timeout;
970 sec = ((timeout / 1000) * 1024) / 1000;
971 usec = (timeout % 1000) * 1024;
972 eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
974 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
975 wpa_s->sme.sa_query_count);
977 sme_send_sa_query_req(wpa_s, trans_id);
981 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
983 sme_sa_query_timer(wpa_s, NULL);
987 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
989 eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
990 os_free(wpa_s->sme.sa_query_trans_id);
991 wpa_s->sme.sa_query_trans_id = NULL;
992 wpa_s->sme.sa_query_count = 0;
996 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
997 const u8 *da, u16 reason_code)
999 struct wpa_ssid *ssid;
1001 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1003 if (wpa_s->wpa_state != WPA_COMPLETED)
1005 ssid = wpa_s->current_ssid;
1006 if (ssid == NULL || ssid->ieee80211w == 0)
1008 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1010 if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
1011 reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
1013 if (wpa_s->sme.sa_query_count > 0)
1016 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
1017 "possible AP/STA state mismatch - trigger SA Query");
1018 sme_start_sa_query(wpa_s);
1022 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
1023 const u8 *data, size_t len)
1027 if (wpa_s->sme.sa_query_trans_id == NULL ||
1028 len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
1029 data[0] != WLAN_SA_QUERY_RESPONSE)
1031 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
1032 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
1034 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1037 for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
1038 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
1039 i * WLAN_SA_QUERY_TR_ID_LEN,
1040 data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
1044 if (i >= wpa_s->sme.sa_query_count) {
1045 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
1046 "transaction identifier found");
1050 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
1051 "from " MACSTR, MAC2STR(sa));
1052 sme_stop_sa_query(wpa_s);
1055 #endif /* CONFIG_IEEE80211W */