]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/sme.c
SAE: Add support for using the optional Password Identifier
[thirdparty/hostap.git] / wpa_supplicant / sme.c
1 /*
2 * wpa_supplicant - SME
3 * Copyright (c) 2009-2014, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
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 "common/sae.h"
18 #include "rsn_supp/wpa.h"
19 #include "rsn_supp/pmksa_cache.h"
20 #include "config.h"
21 #include "wpa_supplicant_i.h"
22 #include "driver_i.h"
23 #include "wpas_glue.h"
24 #include "wps_supplicant.h"
25 #include "p2p_supplicant.h"
26 #include "notify.h"
27 #include "bss.h"
28 #include "scan.h"
29 #include "sme.h"
30 #include "hs20_supplicant.h"
31
32 #define SME_AUTH_TIMEOUT 5
33 #define SME_ASSOC_TIMEOUT 5
34
35 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
36 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
37 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
38 #ifdef CONFIG_IEEE80211W
39 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
40 #endif /* CONFIG_IEEE80211W */
41
42
43 #ifdef CONFIG_SAE
44
45 static int index_within_array(const int *array, int idx)
46 {
47 int i;
48 for (i = 0; i < idx; i++) {
49 if (array[i] <= 0)
50 return 0;
51 }
52 return 1;
53 }
54
55
56 static int sme_set_sae_group(struct wpa_supplicant *wpa_s)
57 {
58 int *groups = wpa_s->conf->sae_groups;
59 int default_groups[] = { 19, 20, 21, 25, 26, 0 };
60
61 if (!groups || groups[0] <= 0)
62 groups = default_groups;
63
64 /* Configuration may have changed, so validate current index */
65 if (!index_within_array(groups, wpa_s->sme.sae_group_index))
66 return -1;
67
68 for (;;) {
69 int group = groups[wpa_s->sme.sae_group_index];
70 if (group <= 0)
71 break;
72 if (sae_set_group(&wpa_s->sme.sae, group) == 0) {
73 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
74 wpa_s->sme.sae.group);
75 return 0;
76 }
77 wpa_s->sme.sae_group_index++;
78 }
79
80 return -1;
81 }
82
83
84 static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s,
85 struct wpa_ssid *ssid,
86 const u8 *bssid, int external)
87 {
88 struct wpabuf *buf;
89 size_t len;
90 const char *password;
91
92 #ifdef CONFIG_TESTING_OPTIONS
93 if (wpa_s->sae_commit_override) {
94 wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
95 buf = wpabuf_alloc(4 + wpabuf_len(wpa_s->sae_commit_override));
96 if (!buf)
97 return NULL;
98 wpabuf_put_le16(buf, 1); /* Transaction seq# */
99 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
100 wpabuf_put_buf(buf, wpa_s->sae_commit_override);
101 return buf;
102 }
103 #endif /* CONFIG_TESTING_OPTIONS */
104
105 password = ssid->sae_password;
106 if (!password)
107 password = ssid->passphrase;
108 if (!password) {
109 wpa_printf(MSG_DEBUG, "SAE: No password available");
110 return NULL;
111 }
112
113 if (sme_set_sae_group(wpa_s) < 0) {
114 wpa_printf(MSG_DEBUG, "SAE: Failed to select group");
115 return NULL;
116 }
117
118 if (sae_prepare_commit(wpa_s->own_addr, bssid,
119 (u8 *) password, os_strlen(password),
120 ssid->sae_password_id,
121 &wpa_s->sme.sae) < 0) {
122 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
123 return NULL;
124 }
125
126 len = wpa_s->sme.sae_token ? wpabuf_len(wpa_s->sme.sae_token) : 0;
127 if (ssid->sae_password_id)
128 len += 4 + os_strlen(ssid->sae_password_id);
129 buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
130 if (buf == NULL)
131 return NULL;
132 if (!external) {
133 wpabuf_put_le16(buf, 1); /* Transaction seq# */
134 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
135 }
136 sae_write_commit(&wpa_s->sme.sae, buf, wpa_s->sme.sae_token,
137 ssid->sae_password_id);
138
139 return buf;
140 }
141
142
143 static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s,
144 int external)
145 {
146 struct wpabuf *buf;
147
148 buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN);
149 if (buf == NULL)
150 return NULL;
151
152 if (!external) {
153 wpabuf_put_le16(buf, 2); /* Transaction seq# */
154 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
155 }
156 sae_write_confirm(&wpa_s->sme.sae, buf);
157
158 return buf;
159 }
160
161 #endif /* CONFIG_SAE */
162
163
164 /**
165 * sme_auth_handle_rrm - Handle RRM aspects of current authentication attempt
166 * @wpa_s: Pointer to wpa_supplicant data
167 * @bss: Pointer to the bss which is the target of authentication attempt
168 */
169 static void sme_auth_handle_rrm(struct wpa_supplicant *wpa_s,
170 struct wpa_bss *bss)
171 {
172 const u8 rrm_ie_len = 5;
173 u8 *pos;
174 const u8 *rrm_ie;
175
176 wpa_s->rrm.rrm_used = 0;
177
178 wpa_printf(MSG_DEBUG,
179 "RRM: Determining whether RRM can be used - device support: 0x%x",
180 wpa_s->drv_rrm_flags);
181
182 rrm_ie = wpa_bss_get_ie(bss, WLAN_EID_RRM_ENABLED_CAPABILITIES);
183 if (!rrm_ie || !(bss->caps & IEEE80211_CAP_RRM)) {
184 wpa_printf(MSG_DEBUG, "RRM: No RRM in network");
185 return;
186 }
187
188 if (!((wpa_s->drv_rrm_flags &
189 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
190 (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
191 !(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) {
192 wpa_printf(MSG_DEBUG,
193 "RRM: Insufficient RRM support in driver - do not use RRM");
194 return;
195 }
196
197 if (sizeof(wpa_s->sme.assoc_req_ie) <
198 wpa_s->sme.assoc_req_ie_len + rrm_ie_len + 2) {
199 wpa_printf(MSG_INFO,
200 "RRM: Unable to use RRM, no room for RRM IE");
201 return;
202 }
203
204 wpa_printf(MSG_DEBUG, "RRM: Adding RRM IE to Association Request");
205 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
206 os_memset(pos, 0, 2 + rrm_ie_len);
207 *pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
208 *pos++ = rrm_ie_len;
209
210 /* Set supported capabilites flags */
211 if (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)
212 *pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT;
213
214 *pos |= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
215 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
216 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
217
218 if (wpa_s->lci)
219 pos[1] |= WLAN_RRM_CAPS_LCI_MEASUREMENT;
220
221 wpa_s->sme.assoc_req_ie_len += rrm_ie_len + 2;
222 wpa_s->rrm.rrm_used = 1;
223 }
224
225
226 static void sme_send_authentication(struct wpa_supplicant *wpa_s,
227 struct wpa_bss *bss, struct wpa_ssid *ssid,
228 int start)
229 {
230 struct wpa_driver_auth_params params;
231 struct wpa_ssid *old_ssid;
232 #ifdef CONFIG_IEEE80211R
233 const u8 *ie;
234 #endif /* CONFIG_IEEE80211R */
235 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_FILS)
236 const u8 *md = NULL;
237 #endif /* CONFIG_IEEE80211R || CONFIG_FILS */
238 int i, bssid_changed;
239 struct wpabuf *resp = NULL;
240 u8 ext_capab[18];
241 int ext_capab_len;
242 int skip_auth;
243
244 if (bss == NULL) {
245 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
246 "the network");
247 wpas_connect_work_done(wpa_s);
248 return;
249 }
250
251 skip_auth = wpa_s->conf->reassoc_same_bss_optim &&
252 wpa_s->reassoc_same_bss;
253 wpa_s->current_bss = bss;
254
255 os_memset(&params, 0, sizeof(params));
256 wpa_s->reassociate = 0;
257
258 params.freq = bss->freq;
259 params.bssid = bss->bssid;
260 params.ssid = bss->ssid;
261 params.ssid_len = bss->ssid_len;
262 params.p2p = ssid->p2p_group;
263
264 if (wpa_s->sme.ssid_len != params.ssid_len ||
265 os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
266 wpa_s->sme.prev_bssid_set = 0;
267
268 wpa_s->sme.freq = params.freq;
269 os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
270 wpa_s->sme.ssid_len = params.ssid_len;
271
272 params.auth_alg = WPA_AUTH_ALG_OPEN;
273 #ifdef IEEE8021X_EAPOL
274 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
275 if (ssid->leap) {
276 if (ssid->non_leap == 0)
277 params.auth_alg = WPA_AUTH_ALG_LEAP;
278 else
279 params.auth_alg |= WPA_AUTH_ALG_LEAP;
280 }
281 }
282 #endif /* IEEE8021X_EAPOL */
283 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
284 params.auth_alg);
285 if (ssid->auth_alg) {
286 params.auth_alg = ssid->auth_alg;
287 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
288 "0x%x", params.auth_alg);
289 }
290 #ifdef CONFIG_SAE
291 wpa_s->sme.sae_pmksa_caching = 0;
292 if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
293 const u8 *rsn;
294 struct wpa_ie_data ied;
295
296 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
297 if (!rsn) {
298 wpa_dbg(wpa_s, MSG_DEBUG,
299 "SAE enabled, but target BSS does not advertise RSN");
300 } else if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
301 wpa_key_mgmt_sae(ied.key_mgmt)) {
302 wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
303 params.auth_alg = WPA_AUTH_ALG_SAE;
304 } else {
305 wpa_dbg(wpa_s, MSG_DEBUG,
306 "SAE enabled, but target BSS does not advertise SAE AKM for RSN");
307 }
308 }
309 #endif /* CONFIG_SAE */
310
311 for (i = 0; i < NUM_WEP_KEYS; i++) {
312 if (ssid->wep_key_len[i])
313 params.wep_key[i] = ssid->wep_key[i];
314 params.wep_key_len[i] = ssid->wep_key_len[i];
315 }
316 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
317
318 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
319 os_memset(wpa_s->bssid, 0, ETH_ALEN);
320 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
321 if (bssid_changed)
322 wpas_notify_bssid_changed(wpa_s);
323
324 if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
325 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
326 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
327 int try_opportunistic;
328 const u8 *cache_id = NULL;
329
330 try_opportunistic = (ssid->proactive_key_caching < 0 ?
331 wpa_s->conf->okc :
332 ssid->proactive_key_caching) &&
333 (ssid->proto & WPA_PROTO_RSN);
334 #ifdef CONFIG_FILS
335 if (wpa_key_mgmt_fils(ssid->key_mgmt))
336 cache_id = wpa_bss_get_fils_cache_id(bss);
337 #endif /* CONFIG_FILS */
338 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
339 wpa_s->current_ssid,
340 try_opportunistic, cache_id,
341 0) == 0)
342 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
343 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
344 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
345 wpa_s->sme.assoc_req_ie,
346 &wpa_s->sme.assoc_req_ie_len)) {
347 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
348 "key management and encryption suites");
349 wpas_connect_work_done(wpa_s);
350 return;
351 }
352 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
353 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
354 /*
355 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
356 * use non-WPA since the scan results did not indicate that the
357 * AP is using WPA or WPA2.
358 */
359 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
360 wpa_s->sme.assoc_req_ie_len = 0;
361 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
362 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
363 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
364 wpa_s->sme.assoc_req_ie,
365 &wpa_s->sme.assoc_req_ie_len)) {
366 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
367 "key management and encryption suites (no "
368 "scan results)");
369 wpas_connect_work_done(wpa_s);
370 return;
371 }
372 #ifdef CONFIG_WPS
373 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
374 struct wpabuf *wps_ie;
375 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
376 if (wps_ie && wpabuf_len(wps_ie) <=
377 sizeof(wpa_s->sme.assoc_req_ie)) {
378 wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
379 os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
380 wpa_s->sme.assoc_req_ie_len);
381 } else
382 wpa_s->sme.assoc_req_ie_len = 0;
383 wpabuf_free(wps_ie);
384 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
385 #endif /* CONFIG_WPS */
386 } else {
387 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
388 wpa_s->sme.assoc_req_ie_len = 0;
389 }
390
391 #ifdef CONFIG_IEEE80211R
392 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
393 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
394 md = ie + 2;
395 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
396 if (md) {
397 /* Prepare for the next transition */
398 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
399 }
400
401 if (md && !wpa_key_mgmt_ft(ssid->key_mgmt))
402 md = NULL;
403 if (md) {
404 wpa_dbg(wpa_s, MSG_DEBUG, "SME: FT mobility domain %02x%02x",
405 md[0], md[1]);
406
407 if (wpa_s->sme.assoc_req_ie_len + 5 <
408 sizeof(wpa_s->sme.assoc_req_ie)) {
409 struct rsn_mdie *mdie;
410 u8 *pos = wpa_s->sme.assoc_req_ie +
411 wpa_s->sme.assoc_req_ie_len;
412 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
413 *pos++ = sizeof(*mdie);
414 mdie = (struct rsn_mdie *) pos;
415 os_memcpy(mdie->mobility_domain, md,
416 MOBILITY_DOMAIN_ID_LEN);
417 mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
418 wpa_s->sme.assoc_req_ie_len += 5;
419 }
420
421 if (wpa_s->sme.ft_used &&
422 os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
423 wpa_sm_has_ptk(wpa_s->wpa)) {
424 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
425 "over-the-air");
426 params.auth_alg = WPA_AUTH_ALG_FT;
427 params.ie = wpa_s->sme.ft_ies;
428 params.ie_len = wpa_s->sme.ft_ies_len;
429 }
430 }
431 #endif /* CONFIG_IEEE80211R */
432
433 #ifdef CONFIG_IEEE80211W
434 wpa_s->sme.mfp = wpas_get_ssid_pmf(wpa_s, ssid);
435 if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
436 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
437 struct wpa_ie_data _ie;
438 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
439 _ie.capabilities &
440 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
441 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
442 "MFP: require MFP");
443 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
444 }
445 }
446 #endif /* CONFIG_IEEE80211W */
447
448 #ifdef CONFIG_P2P
449 if (wpa_s->global->p2p) {
450 u8 *pos;
451 size_t len;
452 int res;
453 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
454 len = sizeof(wpa_s->sme.assoc_req_ie) -
455 wpa_s->sme.assoc_req_ie_len;
456 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
457 ssid->p2p_group);
458 if (res >= 0)
459 wpa_s->sme.assoc_req_ie_len += res;
460 }
461 #endif /* CONFIG_P2P */
462
463 #ifdef CONFIG_FST
464 if (wpa_s->fst_ies) {
465 int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
466
467 if (wpa_s->sme.assoc_req_ie_len + fst_ies_len <=
468 sizeof(wpa_s->sme.assoc_req_ie)) {
469 os_memcpy(wpa_s->sme.assoc_req_ie +
470 wpa_s->sme.assoc_req_ie_len,
471 wpabuf_head(wpa_s->fst_ies),
472 fst_ies_len);
473 wpa_s->sme.assoc_req_ie_len += fst_ies_len;
474 }
475 }
476 #endif /* CONFIG_FST */
477
478 sme_auth_handle_rrm(wpa_s, bss);
479
480 wpa_s->sme.assoc_req_ie_len += wpas_supp_op_class_ie(
481 wpa_s, bss->freq,
482 wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
483 sizeof(wpa_s->sme.assoc_req_ie) - wpa_s->sme.assoc_req_ie_len);
484
485 if (params.p2p)
486 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
487 else
488 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
489
490 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
491 sizeof(ext_capab));
492 if (ext_capab_len > 0) {
493 u8 *pos = wpa_s->sme.assoc_req_ie;
494 if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
495 pos += 2 + pos[1];
496 os_memmove(pos + ext_capab_len, pos,
497 wpa_s->sme.assoc_req_ie_len -
498 (pos - wpa_s->sme.assoc_req_ie));
499 wpa_s->sme.assoc_req_ie_len += ext_capab_len;
500 os_memcpy(pos, ext_capab, ext_capab_len);
501 }
502
503 #ifdef CONFIG_HS20
504 if (is_hs20_network(wpa_s, ssid, bss)) {
505 struct wpabuf *hs20;
506
507 hs20 = wpabuf_alloc(20 + MAX_ROAMING_CONS_OI_LEN);
508 if (hs20) {
509 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
510 size_t len;
511
512 wpas_hs20_add_indication(hs20, pps_mo_id);
513 wpas_hs20_add_roam_cons_sel(hs20, ssid);
514 len = sizeof(wpa_s->sme.assoc_req_ie) -
515 wpa_s->sme.assoc_req_ie_len;
516 if (wpabuf_len(hs20) <= len) {
517 os_memcpy(wpa_s->sme.assoc_req_ie +
518 wpa_s->sme.assoc_req_ie_len,
519 wpabuf_head(hs20), wpabuf_len(hs20));
520 wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
521 }
522 wpabuf_free(hs20);
523 }
524 }
525 #endif /* CONFIG_HS20 */
526
527 if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
528 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
529 size_t len;
530
531 len = sizeof(wpa_s->sme.assoc_req_ie) -
532 wpa_s->sme.assoc_req_ie_len;
533 if (wpabuf_len(buf) <= len) {
534 os_memcpy(wpa_s->sme.assoc_req_ie +
535 wpa_s->sme.assoc_req_ie_len,
536 wpabuf_head(buf), wpabuf_len(buf));
537 wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
538 }
539 }
540
541 #ifdef CONFIG_MBO
542 if (wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
543 int len;
544
545 len = wpas_mbo_ie(wpa_s, wpa_s->sme.assoc_req_ie +
546 wpa_s->sme.assoc_req_ie_len,
547 sizeof(wpa_s->sme.assoc_req_ie) -
548 wpa_s->sme.assoc_req_ie_len);
549 if (len >= 0)
550 wpa_s->sme.assoc_req_ie_len += len;
551 }
552 #endif /* CONFIG_MBO */
553
554 #ifdef CONFIG_SAE
555 if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE &&
556 pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid, ssid, 0,
557 NULL, WPA_KEY_MGMT_SAE) == 0) {
558 wpa_dbg(wpa_s, MSG_DEBUG,
559 "PMKSA cache entry found - try to use PMKSA caching instead of new SAE authentication");
560 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
561 params.auth_alg = WPA_AUTH_ALG_OPEN;
562 wpa_s->sme.sae_pmksa_caching = 1;
563 }
564
565 if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE) {
566 if (start)
567 resp = sme_auth_build_sae_commit(wpa_s, ssid,
568 bss->bssid, 0);
569 else
570 resp = sme_auth_build_sae_confirm(wpa_s, 0);
571 if (resp == NULL) {
572 wpas_connection_failed(wpa_s, bss->bssid);
573 return;
574 }
575 params.auth_data = wpabuf_head(resp);
576 params.auth_data_len = wpabuf_len(resp);
577 wpa_s->sme.sae.state = start ? SAE_COMMITTED : SAE_CONFIRMED;
578 }
579 #endif /* CONFIG_SAE */
580
581 old_ssid = wpa_s->current_ssid;
582 wpa_s->current_ssid = ssid;
583 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
584 wpa_supplicant_initiate_eapol(wpa_s);
585
586 #ifdef CONFIG_FILS
587 /* TODO: FILS operations can in some cases be done between different
588 * network_ctx (i.e., same credentials can be used with multiple
589 * networks). */
590 if (params.auth_alg == WPA_AUTH_ALG_OPEN &&
591 wpa_key_mgmt_fils(ssid->key_mgmt)) {
592 const u8 *indic;
593 u16 fils_info;
594
595 /*
596 * Check FILS Indication element (FILS Information field) bits
597 * indicating supported authentication algorithms against local
598 * configuration (ssid->fils_dh_group). Try to use FILS
599 * authentication only if the AP supports the combination in the
600 * network profile. */
601 indic = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
602 if (!indic || indic[1] < 2) {
603 wpa_printf(MSG_DEBUG, "SME: " MACSTR
604 " does not include FILS Indication element - cannot use FILS authentication with it",
605 MAC2STR(bss->bssid));
606 goto no_fils;
607 }
608
609 fils_info = WPA_GET_LE16(indic + 2);
610 if (ssid->fils_dh_group == 0 && !(fils_info & BIT(9))) {
611 wpa_printf(MSG_DEBUG, "SME: " MACSTR
612 " does not support FILS SK without PFS - cannot use FILS authentication with it",
613 MAC2STR(bss->bssid));
614 goto no_fils;
615 }
616 if (ssid->fils_dh_group != 0 && !(fils_info & BIT(10))) {
617 wpa_printf(MSG_DEBUG, "SME: " MACSTR
618 " does not support FILS SK with PFS - cannot use FILS authentication with it",
619 MAC2STR(bss->bssid));
620 goto no_fils;
621 }
622
623 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
624 ssid, 0,
625 wpa_bss_get_fils_cache_id(bss),
626 0) == 0)
627 wpa_printf(MSG_DEBUG,
628 "SME: Try to use FILS with PMKSA caching");
629 resp = fils_build_auth(wpa_s->wpa, ssid->fils_dh_group, md);
630 if (resp) {
631 int auth_alg;
632
633 if (ssid->fils_dh_group)
634 wpa_printf(MSG_DEBUG,
635 "SME: Try to use FILS SK authentication with PFS (DH Group %u)",
636 ssid->fils_dh_group);
637 else
638 wpa_printf(MSG_DEBUG,
639 "SME: Try to use FILS SK authentication without PFS");
640 auth_alg = ssid->fils_dh_group ?
641 WPA_AUTH_ALG_FILS_SK_PFS : WPA_AUTH_ALG_FILS;
642 params.auth_alg = auth_alg;
643 params.auth_data = wpabuf_head(resp);
644 params.auth_data_len = wpabuf_len(resp);
645 wpa_s->sme.auth_alg = auth_alg;
646 }
647 }
648 no_fils:
649 #endif /* CONFIG_FILS */
650
651 wpa_supplicant_cancel_sched_scan(wpa_s);
652 wpa_supplicant_cancel_scan(wpa_s);
653
654 wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
655 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
656 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
657
658 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
659 wpa_clear_keys(wpa_s, bss->bssid);
660 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
661 if (old_ssid != wpa_s->current_ssid)
662 wpas_notify_network_changed(wpa_s);
663
664 #ifdef CONFIG_HS20
665 hs20_configure_frame_filters(wpa_s);
666 #endif /* CONFIG_HS20 */
667
668 #ifdef CONFIG_P2P
669 /*
670 * If multi-channel concurrency is not supported, check for any
671 * frequency conflict. In case of any frequency conflict, remove the
672 * least prioritized connection.
673 */
674 if (wpa_s->num_multichan_concurrent < 2) {
675 int freq, num;
676 num = get_shared_radio_freqs(wpa_s, &freq, 1);
677 if (num > 0 && freq > 0 && freq != params.freq) {
678 wpa_printf(MSG_DEBUG,
679 "Conflicting frequency found (%d != %d)",
680 freq, params.freq);
681 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
682 params.freq,
683 ssid) < 0) {
684 wpas_connection_failed(wpa_s, bss->bssid);
685 wpa_supplicant_mark_disassoc(wpa_s);
686 wpabuf_free(resp);
687 wpas_connect_work_done(wpa_s);
688 return;
689 }
690 }
691 }
692 #endif /* CONFIG_P2P */
693
694 if (skip_auth) {
695 wpa_msg(wpa_s, MSG_DEBUG,
696 "SME: Skip authentication step on reassoc-to-same-BSS");
697 wpabuf_free(resp);
698 sme_associate(wpa_s, ssid->mode, bss->bssid, WLAN_AUTH_OPEN);
699 return;
700 }
701
702
703 wpa_s->sme.auth_alg = params.auth_alg;
704 if (wpa_drv_authenticate(wpa_s, &params) < 0) {
705 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
706 "driver failed");
707 wpas_connection_failed(wpa_s, bss->bssid);
708 wpa_supplicant_mark_disassoc(wpa_s);
709 wpabuf_free(resp);
710 wpas_connect_work_done(wpa_s);
711 return;
712 }
713
714 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
715 NULL);
716
717 /*
718 * Association will be started based on the authentication event from
719 * the driver.
720 */
721
722 wpabuf_free(resp);
723 }
724
725
726 static void sme_auth_start_cb(struct wpa_radio_work *work, int deinit)
727 {
728 struct wpa_connect_work *cwork = work->ctx;
729 struct wpa_supplicant *wpa_s = work->wpa_s;
730
731 if (deinit) {
732 if (work->started)
733 wpa_s->connect_work = NULL;
734
735 wpas_connect_work_free(cwork);
736 return;
737 }
738
739 wpa_s->connect_work = work;
740
741 if (cwork->bss_removed ||
742 !wpas_valid_bss_ssid(wpa_s, cwork->bss, cwork->ssid) ||
743 wpas_network_disabled(wpa_s, cwork->ssid)) {
744 wpa_dbg(wpa_s, MSG_DEBUG, "SME: BSS/SSID entry for authentication not valid anymore - drop connection attempt");
745 wpas_connect_work_done(wpa_s);
746 return;
747 }
748
749 /* Starting new connection, so clear the possibly used WPA IE from the
750 * previous association. */
751 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
752
753 sme_send_authentication(wpa_s, cwork->bss, cwork->ssid, 1);
754 }
755
756
757 void sme_authenticate(struct wpa_supplicant *wpa_s,
758 struct wpa_bss *bss, struct wpa_ssid *ssid)
759 {
760 struct wpa_connect_work *cwork;
761
762 if (bss == NULL || ssid == NULL)
763 return;
764 if (wpa_s->connect_work) {
765 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reject sme_authenticate() call since connect_work exist");
766 return;
767 }
768
769 if (radio_work_pending(wpa_s, "sme-connect")) {
770 /*
771 * The previous sme-connect work might no longer be valid due to
772 * the fact that the BSS list was updated. In addition, it makes
773 * sense to adhere to the 'newer' decision.
774 */
775 wpa_dbg(wpa_s, MSG_DEBUG,
776 "SME: Remove previous pending sme-connect");
777 radio_remove_works(wpa_s, "sme-connect", 0);
778 }
779
780 wpas_abort_ongoing_scan(wpa_s);
781
782 cwork = os_zalloc(sizeof(*cwork));
783 if (cwork == NULL)
784 return;
785 cwork->bss = bss;
786 cwork->ssid = ssid;
787 cwork->sme = 1;
788
789 #ifdef CONFIG_SAE
790 wpa_s->sme.sae.state = SAE_NOTHING;
791 wpa_s->sme.sae.send_confirm = 0;
792 wpa_s->sme.sae_group_index = 0;
793 #endif /* CONFIG_SAE */
794
795 if (radio_add_work(wpa_s, bss->freq, "sme-connect", 1,
796 sme_auth_start_cb, cwork) < 0)
797 wpas_connect_work_free(cwork);
798 }
799
800
801 #ifdef CONFIG_SAE
802
803 static int sme_external_auth_build_buf(struct wpabuf *buf,
804 struct wpabuf *params,
805 const u8 *sa, const u8 *da,
806 u16 auth_transaction, u16 seq_num)
807 {
808 struct ieee80211_mgmt *resp;
809
810 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
811 u.auth.variable));
812
813 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
814 (WLAN_FC_STYPE_AUTH << 4));
815 os_memcpy(resp->da, da, ETH_ALEN);
816 os_memcpy(resp->sa, sa, ETH_ALEN);
817 os_memcpy(resp->bssid, da, ETH_ALEN);
818 resp->u.auth.auth_alg = WLAN_AUTH_SAE;
819 resp->seq_ctrl = seq_num << 4;
820 resp->u.auth.auth_transaction = auth_transaction;
821 resp->u.auth.status_code = WLAN_STATUS_SUCCESS;
822 if (params)
823 wpabuf_put_buf(buf, params);
824
825 return 0;
826 }
827
828
829 static void sme_external_auth_send_sae_commit(struct wpa_supplicant *wpa_s,
830 const u8 *bssid,
831 struct wpa_ssid *ssid)
832 {
833 struct wpabuf *resp, *buf;
834
835 resp = sme_auth_build_sae_commit(wpa_s, ssid, bssid, 1);
836 if (!resp)
837 return;
838
839 wpa_s->sme.sae.state = SAE_COMMITTED;
840 buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + wpabuf_len(resp));
841 if (!buf) {
842 wpabuf_free(resp);
843 return;
844 }
845
846 wpa_s->sme.seq_num++;
847 sme_external_auth_build_buf(buf, resp, wpa_s->own_addr,
848 bssid, 1, wpa_s->sme.seq_num);
849 wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1, 0);
850 wpabuf_free(resp);
851 wpabuf_free(buf);
852 }
853
854
855 static void sme_send_external_auth_status(struct wpa_supplicant *wpa_s,
856 u16 status)
857 {
858 struct external_auth params;
859
860 os_memset(&params, 0, sizeof(params));
861 params.status = status;
862 os_memcpy(params.ssid, wpa_s->sme.ext_auth.ssid,
863 wpa_s->sme.ext_auth.ssid_len);
864 params.ssid_len = wpa_s->sme.ext_auth.ssid_len;
865 os_memcpy(params.bssid, wpa_s->sme.ext_auth.bssid, ETH_ALEN);
866 wpa_drv_send_external_auth_status(wpa_s, &params);
867 }
868
869
870 static void sme_handle_external_auth_start(struct wpa_supplicant *wpa_s,
871 union wpa_event_data *data)
872 {
873 struct wpa_ssid *ssid;
874 size_t ssid_str_len = data->external_auth.ssid_len;
875 u8 *ssid_str = data->external_auth.ssid;
876
877 /* Get the SSID conf from the ssid string obtained */
878 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
879 if (!wpas_network_disabled(wpa_s, ssid) &&
880 ssid_str_len == ssid->ssid_len &&
881 os_memcmp(ssid_str, ssid->ssid, ssid_str_len) == 0)
882 break;
883 }
884 if (ssid)
885 sme_external_auth_send_sae_commit(wpa_s,
886 data->external_auth.bssid,
887 ssid);
888 else
889 sme_send_external_auth_status(wpa_s,
890 WLAN_STATUS_UNSPECIFIED_FAILURE);
891 }
892
893
894 static void sme_external_auth_send_sae_confirm(struct wpa_supplicant *wpa_s,
895 const u8 *da)
896 {
897 struct wpabuf *resp, *buf;
898
899 resp = sme_auth_build_sae_confirm(wpa_s, 1);
900 if (!resp) {
901 wpa_printf(MSG_DEBUG, "SAE: Confirm message buf alloc failure");
902 return;
903 }
904
905 wpa_s->sme.sae.state = SAE_CONFIRMED;
906 buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN + wpabuf_len(resp));
907 if (!buf) {
908 wpa_printf(MSG_DEBUG, "SAE: Auth Confirm buf alloc failure");
909 wpabuf_free(resp);
910 return;
911 }
912 wpa_s->sme.seq_num++;
913 sme_external_auth_build_buf(buf, resp, wpa_s->own_addr,
914 da, 2, wpa_s->sme.seq_num);
915 wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1, 0);
916 wpabuf_free(resp);
917 wpabuf_free(buf);
918 }
919
920
921 void sme_external_auth_trigger(struct wpa_supplicant *wpa_s,
922 union wpa_event_data *data)
923 {
924 if (RSN_SELECTOR_GET(&data->external_auth.key_mgmt_suite) !=
925 RSN_AUTH_KEY_MGMT_SAE)
926 return;
927
928 if (data->external_auth.action == EXT_AUTH_START) {
929 os_memcpy(&wpa_s->sme.ext_auth, data,
930 sizeof(struct external_auth));
931 wpa_s->sme.seq_num = 0;
932 wpa_s->sme.sae.state = SAE_NOTHING;
933 wpa_s->sme.sae.send_confirm = 0;
934 wpa_s->sme.sae_group_index = 0;
935 sme_handle_external_auth_start(wpa_s, data);
936 } else if (data->external_auth.action == EXT_AUTH_ABORT) {
937 /* Report failure to driver for the wrong trigger */
938 sme_send_external_auth_status(wpa_s,
939 WLAN_STATUS_UNSPECIFIED_FAILURE);
940 }
941 }
942
943
944 static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
945 u16 status_code, const u8 *data, size_t len,
946 int external, const u8 *sa)
947 {
948 int *groups;
949
950 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
951 "status code %u", auth_transaction, status_code);
952
953 if (auth_transaction == 1 &&
954 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
955 wpa_s->sme.sae.state == SAE_COMMITTED &&
956 (external || wpa_s->current_bss) && wpa_s->current_ssid) {
957 int default_groups[] = { 19, 20, 21, 25, 26, 0 };
958 u16 group;
959
960 groups = wpa_s->conf->sae_groups;
961 if (!groups || groups[0] <= 0)
962 groups = default_groups;
963
964 if (len < sizeof(le16)) {
965 wpa_dbg(wpa_s, MSG_DEBUG,
966 "SME: Too short SAE anti-clogging token request");
967 return -1;
968 }
969 group = WPA_GET_LE16(data);
970 wpa_dbg(wpa_s, MSG_DEBUG,
971 "SME: SAE anti-clogging token requested (group %u)",
972 group);
973 if (sae_group_allowed(&wpa_s->sme.sae, groups, group) !=
974 WLAN_STATUS_SUCCESS) {
975 wpa_dbg(wpa_s, MSG_ERROR,
976 "SME: SAE group %u of anti-clogging request is invalid",
977 group);
978 return -1;
979 }
980 wpabuf_free(wpa_s->sme.sae_token);
981 wpa_s->sme.sae_token = wpabuf_alloc_copy(data + sizeof(le16),
982 len - sizeof(le16));
983 if (!external)
984 sme_send_authentication(wpa_s, wpa_s->current_bss,
985 wpa_s->current_ssid, 1);
986 else
987 sme_external_auth_send_sae_commit(
988 wpa_s, wpa_s->sme.ext_auth.bssid,
989 wpa_s->current_ssid);
990 return 0;
991 }
992
993 if (auth_transaction == 1 &&
994 status_code == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
995 wpa_s->sme.sae.state == SAE_COMMITTED &&
996 (external || wpa_s->current_bss) && wpa_s->current_ssid) {
997 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE group not supported");
998 wpa_s->sme.sae_group_index++;
999 if (sme_set_sae_group(wpa_s) < 0)
1000 return -1; /* no other groups enabled */
1001 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Try next enabled SAE group");
1002 if (!external)
1003 sme_send_authentication(wpa_s, wpa_s->current_bss,
1004 wpa_s->current_ssid, 1);
1005 else
1006 sme_external_auth_send_sae_commit(
1007 wpa_s, wpa_s->sme.ext_auth.bssid,
1008 wpa_s->current_ssid);
1009 return 0;
1010 }
1011
1012 if (auth_transaction == 1 &&
1013 status_code == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
1014 const u8 *bssid = sa ? sa : wpa_s->pending_bssid;
1015
1016 wpa_msg(wpa_s, MSG_INFO,
1017 WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER MACSTR,
1018 MAC2STR(bssid));
1019 return -1;
1020 }
1021
1022 if (status_code != WLAN_STATUS_SUCCESS)
1023 return -1;
1024
1025 if (auth_transaction == 1) {
1026 u16 res;
1027
1028 groups = wpa_s->conf->sae_groups;
1029
1030 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
1031 if ((!external && wpa_s->current_bss == NULL) ||
1032 wpa_s->current_ssid == NULL)
1033 return -1;
1034 if (wpa_s->sme.sae.state != SAE_COMMITTED)
1035 return -1;
1036 if (groups && groups[0] <= 0)
1037 groups = NULL;
1038 res = sae_parse_commit(&wpa_s->sme.sae, data, len, NULL, NULL,
1039 groups);
1040 if (res == SAE_SILENTLY_DISCARD) {
1041 wpa_printf(MSG_DEBUG,
1042 "SAE: Drop commit message due to reflection attack");
1043 return 0;
1044 }
1045 if (res != WLAN_STATUS_SUCCESS)
1046 return -1;
1047
1048 if (sae_process_commit(&wpa_s->sme.sae) < 0) {
1049 wpa_printf(MSG_DEBUG, "SAE: Failed to process peer "
1050 "commit");
1051 return -1;
1052 }
1053
1054 wpabuf_free(wpa_s->sme.sae_token);
1055 wpa_s->sme.sae_token = NULL;
1056 if (!external)
1057 sme_send_authentication(wpa_s, wpa_s->current_bss,
1058 wpa_s->current_ssid, 0);
1059 else
1060 sme_external_auth_send_sae_confirm(wpa_s, sa);
1061 return 0;
1062 } else if (auth_transaction == 2) {
1063 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
1064 if (wpa_s->sme.sae.state != SAE_CONFIRMED)
1065 return -1;
1066 if (sae_check_confirm(&wpa_s->sme.sae, data, len) < 0)
1067 return -1;
1068 wpa_s->sme.sae.state = SAE_ACCEPTED;
1069 sae_clear_temp_data(&wpa_s->sme.sae);
1070
1071 if (external) {
1072 /* Report success to driver */
1073 sme_send_external_auth_status(wpa_s,
1074 WLAN_STATUS_SUCCESS);
1075 }
1076
1077 return 1;
1078 }
1079
1080 return -1;
1081 }
1082
1083
1084 void sme_external_auth_mgmt_rx(struct wpa_supplicant *wpa_s,
1085 const u8 *auth_frame, size_t len)
1086 {
1087 const struct ieee80211_mgmt *header;
1088 size_t auth_length;
1089
1090 header = (const struct ieee80211_mgmt *) auth_frame;
1091 auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
1092
1093 if (len < auth_length) {
1094 /* Notify failure to the driver */
1095 sme_send_external_auth_status(wpa_s,
1096 WLAN_STATUS_UNSPECIFIED_FAILURE);
1097 return;
1098 }
1099
1100 if (header->u.auth.auth_alg == WLAN_AUTH_SAE) {
1101 int res;
1102
1103 res = sme_sae_auth(wpa_s, header->u.auth.auth_transaction,
1104 header->u.auth.status_code,
1105 header->u.auth.variable,
1106 len - auth_length, 1, header->sa);
1107 if (res < 0) {
1108 /* Notify failure to the driver */
1109 sme_send_external_auth_status(
1110 wpa_s, WLAN_STATUS_UNSPECIFIED_FAILURE);
1111 return;
1112 }
1113 if (res != 1)
1114 return;
1115
1116 wpa_printf(MSG_DEBUG,
1117 "SME: SAE completed - setting PMK for 4-way handshake");
1118 wpa_sm_set_pmk(wpa_s->wpa, wpa_s->sme.sae.pmk, PMK_LEN,
1119 wpa_s->sme.sae.pmkid, wpa_s->pending_bssid);
1120 }
1121 }
1122
1123 #endif /* CONFIG_SAE */
1124
1125
1126 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
1127 {
1128 struct wpa_ssid *ssid = wpa_s->current_ssid;
1129
1130 if (ssid == NULL) {
1131 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
1132 "when network is not selected");
1133 return;
1134 }
1135
1136 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
1137 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
1138 "when not in authenticating state");
1139 return;
1140 }
1141
1142 if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
1143 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
1144 "unexpected peer " MACSTR,
1145 MAC2STR(data->auth.peer));
1146 return;
1147 }
1148
1149 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
1150 " auth_type=%d auth_transaction=%d status_code=%d",
1151 MAC2STR(data->auth.peer), data->auth.auth_type,
1152 data->auth.auth_transaction, data->auth.status_code);
1153 wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
1154 data->auth.ies, data->auth.ies_len);
1155
1156 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1157
1158 #ifdef CONFIG_SAE
1159 if (data->auth.auth_type == WLAN_AUTH_SAE) {
1160 int res;
1161 res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
1162 data->auth.status_code, data->auth.ies,
1163 data->auth.ies_len, 0, NULL);
1164 if (res < 0) {
1165 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1166 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1167
1168 }
1169 if (res != 1)
1170 return;
1171
1172 wpa_printf(MSG_DEBUG, "SME: SAE completed - setting PMK for "
1173 "4-way handshake");
1174 wpa_sm_set_pmk(wpa_s->wpa, wpa_s->sme.sae.pmk, PMK_LEN,
1175 wpa_s->sme.sae.pmkid, wpa_s->pending_bssid);
1176 }
1177 #endif /* CONFIG_SAE */
1178
1179 if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
1180 char *ie_txt = NULL;
1181
1182 if (data->auth.ies && data->auth.ies_len) {
1183 size_t buflen = 2 * data->auth.ies_len + 1;
1184 ie_txt = os_malloc(buflen);
1185 if (ie_txt) {
1186 wpa_snprintf_hex(ie_txt, buflen, data->auth.ies,
1187 data->auth.ies_len);
1188 }
1189 }
1190 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AUTH_REJECT MACSTR
1191 " auth_type=%u auth_transaction=%u status_code=%u%s%s",
1192 MAC2STR(data->auth.peer), data->auth.auth_type,
1193 data->auth.auth_transaction, data->auth.status_code,
1194 ie_txt ? " ie=" : "",
1195 ie_txt ? ie_txt : "");
1196 os_free(ie_txt);
1197
1198 if (data->auth.status_code !=
1199 WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
1200 wpa_s->sme.auth_alg == data->auth.auth_type ||
1201 wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
1202 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1203 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1204 return;
1205 }
1206
1207 wpas_connect_work_done(wpa_s);
1208
1209 switch (data->auth.auth_type) {
1210 case WLAN_AUTH_OPEN:
1211 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
1212
1213 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
1214 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
1215 wpa_s->current_ssid);
1216 return;
1217
1218 case WLAN_AUTH_SHARED_KEY:
1219 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
1220
1221 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
1222 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
1223 wpa_s->current_ssid);
1224 return;
1225
1226 default:
1227 return;
1228 }
1229 }
1230
1231 #ifdef CONFIG_IEEE80211R
1232 if (data->auth.auth_type == WLAN_AUTH_FT) {
1233 const u8 *ric_ies = NULL;
1234 size_t ric_ies_len = 0;
1235
1236 if (wpa_s->ric_ies) {
1237 ric_ies = wpabuf_head(wpa_s->ric_ies);
1238 ric_ies_len = wpabuf_len(wpa_s->ric_ies);
1239 }
1240 if (wpa_ft_process_response(wpa_s->wpa, data->auth.ies,
1241 data->auth.ies_len, 0,
1242 data->auth.peer,
1243 ric_ies, ric_ies_len) < 0) {
1244 wpa_dbg(wpa_s, MSG_DEBUG,
1245 "SME: FT Authentication response processing failed");
1246 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
1247 MACSTR
1248 " reason=%d locally_generated=1",
1249 MAC2STR(wpa_s->pending_bssid),
1250 WLAN_REASON_DEAUTH_LEAVING);
1251 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1252 wpa_supplicant_mark_disassoc(wpa_s);
1253 return;
1254 }
1255 }
1256 #endif /* CONFIG_IEEE80211R */
1257
1258 #ifdef CONFIG_FILS
1259 if (data->auth.auth_type == WLAN_AUTH_FILS_SK ||
1260 data->auth.auth_type == WLAN_AUTH_FILS_SK_PFS) {
1261 u16 expect_auth_type;
1262
1263 expect_auth_type = wpa_s->sme.auth_alg ==
1264 WPA_AUTH_ALG_FILS_SK_PFS ? WLAN_AUTH_FILS_SK_PFS :
1265 WLAN_AUTH_FILS_SK;
1266 if (data->auth.auth_type != expect_auth_type) {
1267 wpa_dbg(wpa_s, MSG_DEBUG,
1268 "SME: FILS Authentication response used different auth alg (%u; expected %u)",
1269 data->auth.auth_type, expect_auth_type);
1270 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
1271 MACSTR
1272 " reason=%d locally_generated=1",
1273 MAC2STR(wpa_s->pending_bssid),
1274 WLAN_REASON_DEAUTH_LEAVING);
1275 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1276 wpa_supplicant_mark_disassoc(wpa_s);
1277 return;
1278 }
1279
1280 if (fils_process_auth(wpa_s->wpa, wpa_s->pending_bssid,
1281 data->auth.ies, data->auth.ies_len) < 0) {
1282 wpa_dbg(wpa_s, MSG_DEBUG,
1283 "SME: FILS Authentication response processing failed");
1284 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
1285 MACSTR
1286 " reason=%d locally_generated=1",
1287 MAC2STR(wpa_s->pending_bssid),
1288 WLAN_REASON_DEAUTH_LEAVING);
1289 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1290 wpa_supplicant_mark_disassoc(wpa_s);
1291 return;
1292 }
1293 }
1294 #endif /* CONFIG_FILS */
1295
1296 sme_associate(wpa_s, ssid->mode, data->auth.peer,
1297 data->auth.auth_type);
1298 }
1299
1300
1301 #ifdef CONFIG_FILS
1302 #ifdef CONFIG_IEEE80211R
1303 static void remove_ie(u8 *buf, size_t *len, u8 eid)
1304 {
1305 u8 *pos, *next, *end;
1306
1307 pos = (u8 *) get_ie(buf, *len, eid);
1308 if (pos) {
1309 next = pos + 2 + pos[1];
1310 end = buf + *len;
1311 *len -= 2 + pos[1];
1312 os_memmove(pos, next, end - next);
1313 }
1314 }
1315 #endif /* CONFIG_IEEE80211R */
1316 #endif /* CONFIG_FILS */
1317
1318
1319 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
1320 const u8 *bssid, u16 auth_type)
1321 {
1322 struct wpa_driver_associate_params params;
1323 struct ieee802_11_elems elems;
1324 #ifdef CONFIG_FILS
1325 u8 nonces[2 * FILS_NONCE_LEN];
1326 #endif /* CONFIG_FILS */
1327 #ifdef CONFIG_HT_OVERRIDES
1328 struct ieee80211_ht_capabilities htcaps;
1329 struct ieee80211_ht_capabilities htcaps_mask;
1330 #endif /* CONFIG_HT_OVERRIDES */
1331 #ifdef CONFIG_VHT_OVERRIDES
1332 struct ieee80211_vht_capabilities vhtcaps;
1333 struct ieee80211_vht_capabilities vhtcaps_mask;
1334 #endif /* CONFIG_VHT_OVERRIDES */
1335
1336 os_memset(&params, 0, sizeof(params));
1337
1338 #ifdef CONFIG_FILS
1339 if (auth_type == WLAN_AUTH_FILS_SK ||
1340 auth_type == WLAN_AUTH_FILS_SK_PFS) {
1341 struct wpabuf *buf;
1342 const u8 *snonce, *anonce;
1343 const unsigned int max_hlp = 20;
1344 struct wpabuf *hlp[max_hlp];
1345 unsigned int i, num_hlp = 0;
1346 struct fils_hlp_req *req;
1347
1348 dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
1349 list) {
1350 hlp[num_hlp] = wpabuf_alloc(2 * ETH_ALEN + 6 +
1351 wpabuf_len(req->pkt));
1352 if (!hlp[num_hlp])
1353 break;
1354 wpabuf_put_data(hlp[num_hlp], req->dst, ETH_ALEN);
1355 wpabuf_put_data(hlp[num_hlp], wpa_s->own_addr,
1356 ETH_ALEN);
1357 wpabuf_put_data(hlp[num_hlp],
1358 "\xaa\xaa\x03\x00\x00\x00", 6);
1359 wpabuf_put_buf(hlp[num_hlp], req->pkt);
1360 num_hlp++;
1361 if (num_hlp >= max_hlp)
1362 break;
1363 }
1364
1365 buf = fils_build_assoc_req(wpa_s->wpa, &params.fils_kek,
1366 &params.fils_kek_len, &snonce,
1367 &anonce,
1368 (const struct wpabuf **) hlp,
1369 num_hlp);
1370 for (i = 0; i < num_hlp; i++)
1371 wpabuf_free(hlp[i]);
1372 if (!buf)
1373 return;
1374 wpa_hexdump(MSG_DEBUG, "FILS: assoc_req before FILS elements",
1375 wpa_s->sme.assoc_req_ie,
1376 wpa_s->sme.assoc_req_ie_len);
1377 #ifdef CONFIG_IEEE80211R
1378 if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
1379 /* Remove RSNE and MDE to allow them to be overridden
1380 * with FILS+FT specific values from
1381 * fils_build_assoc_req(). */
1382 remove_ie(wpa_s->sme.assoc_req_ie,
1383 &wpa_s->sme.assoc_req_ie_len,
1384 WLAN_EID_RSN);
1385 wpa_hexdump(MSG_DEBUG,
1386 "FILS: assoc_req after RSNE removal",
1387 wpa_s->sme.assoc_req_ie,
1388 wpa_s->sme.assoc_req_ie_len);
1389 remove_ie(wpa_s->sme.assoc_req_ie,
1390 &wpa_s->sme.assoc_req_ie_len,
1391 WLAN_EID_MOBILITY_DOMAIN);
1392 wpa_hexdump(MSG_DEBUG,
1393 "FILS: assoc_req after MDE removal",
1394 wpa_s->sme.assoc_req_ie,
1395 wpa_s->sme.assoc_req_ie_len);
1396 }
1397 #endif /* CONFIG_IEEE80211R */
1398 /* TODO: Make wpa_s->sme.assoc_req_ie use dynamic allocation */
1399 if (wpa_s->sme.assoc_req_ie_len + wpabuf_len(buf) >
1400 sizeof(wpa_s->sme.assoc_req_ie)) {
1401 wpa_printf(MSG_ERROR,
1402 "FILS: Not enough buffer room for own AssocReq elements");
1403 wpabuf_free(buf);
1404 return;
1405 }
1406 os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
1407 wpabuf_head(buf), wpabuf_len(buf));
1408 wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
1409 wpabuf_free(buf);
1410 wpa_hexdump(MSG_DEBUG, "FILS: assoc_req after FILS elements",
1411 wpa_s->sme.assoc_req_ie,
1412 wpa_s->sme.assoc_req_ie_len);
1413
1414 os_memcpy(nonces, snonce, FILS_NONCE_LEN);
1415 os_memcpy(nonces + FILS_NONCE_LEN, anonce, FILS_NONCE_LEN);
1416 params.fils_nonces = nonces;
1417 params.fils_nonces_len = sizeof(nonces);
1418 }
1419 #endif /* CONFIG_FILS */
1420
1421 #ifdef CONFIG_OWE
1422 #ifdef CONFIG_TESTING_OPTIONS
1423 if (get_ie_ext(wpa_s->sme.assoc_req_ie, wpa_s->sme.assoc_req_ie_len,
1424 WLAN_EID_EXT_OWE_DH_PARAM)) {
1425 wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
1426 } else
1427 #endif /* CONFIG_TESTING_OPTIONS */
1428 if (auth_type == WLAN_AUTH_OPEN &&
1429 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
1430 struct wpabuf *owe_ie;
1431 u16 group;
1432
1433 if (wpa_s->current_ssid && wpa_s->current_ssid->owe_group) {
1434 group = wpa_s->current_ssid->owe_group;
1435 } else {
1436 if (wpa_s->last_owe_group == 19)
1437 group = 20;
1438 else if (wpa_s->last_owe_group == 20)
1439 group = 21;
1440 else
1441 group = OWE_DH_GROUP;
1442 }
1443 wpa_s->last_owe_group = group;
1444 wpa_printf(MSG_DEBUG, "OWE: Try to use group %u", group);
1445 owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
1446 if (!owe_ie) {
1447 wpa_printf(MSG_ERROR,
1448 "OWE: Failed to build IE for Association Request frame");
1449 return;
1450 }
1451 if (wpa_s->sme.assoc_req_ie_len + wpabuf_len(owe_ie) >
1452 sizeof(wpa_s->sme.assoc_req_ie)) {
1453 wpa_printf(MSG_ERROR,
1454 "OWE: Not enough buffer room for own Association Request frame elements");
1455 wpabuf_free(owe_ie);
1456 return;
1457 }
1458 os_memcpy(wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
1459 wpabuf_head(owe_ie), wpabuf_len(owe_ie));
1460 wpa_s->sme.assoc_req_ie_len += wpabuf_len(owe_ie);
1461 wpabuf_free(owe_ie);
1462 }
1463 #endif /* CONFIG_OWE */
1464
1465 params.bssid = bssid;
1466 params.ssid = wpa_s->sme.ssid;
1467 params.ssid_len = wpa_s->sme.ssid_len;
1468 params.freq.freq = wpa_s->sme.freq;
1469 params.bg_scan_period = wpa_s->current_ssid ?
1470 wpa_s->current_ssid->bg_scan_period : -1;
1471 params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
1472 wpa_s->sme.assoc_req_ie : NULL;
1473 params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
1474 params.pairwise_suite = wpa_s->pairwise_cipher;
1475 params.group_suite = wpa_s->group_cipher;
1476 params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
1477 params.key_mgmt_suite = wpa_s->key_mgmt;
1478 params.wpa_proto = wpa_s->wpa_proto;
1479 #ifdef CONFIG_HT_OVERRIDES
1480 os_memset(&htcaps, 0, sizeof(htcaps));
1481 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1482 params.htcaps = (u8 *) &htcaps;
1483 params.htcaps_mask = (u8 *) &htcaps_mask;
1484 wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
1485 #endif /* CONFIG_HT_OVERRIDES */
1486 #ifdef CONFIG_VHT_OVERRIDES
1487 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
1488 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
1489 params.vhtcaps = &vhtcaps;
1490 params.vhtcaps_mask = &vhtcaps_mask;
1491 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
1492 #endif /* CONFIG_VHT_OVERRIDES */
1493 #ifdef CONFIG_IEEE80211R
1494 if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
1495 params.wpa_ie = wpa_s->sme.ft_ies;
1496 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
1497 }
1498 #endif /* CONFIG_IEEE80211R */
1499 params.mode = mode;
1500 params.mgmt_frame_protection = wpa_s->sme.mfp;
1501 params.rrm_used = wpa_s->rrm.rrm_used;
1502 if (wpa_s->sme.prev_bssid_set)
1503 params.prev_bssid = wpa_s->sme.prev_bssid;
1504
1505 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1506 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
1507 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
1508 params.freq.freq);
1509
1510 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1511
1512 if (params.wpa_ie == NULL ||
1513 ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
1514 < 0) {
1515 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
1516 os_memset(&elems, 0, sizeof(elems));
1517 }
1518 if (elems.rsn_ie) {
1519 params.wpa_proto = WPA_PROTO_RSN;
1520 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
1521 elems.rsn_ie_len + 2);
1522 } else if (elems.wpa_ie) {
1523 params.wpa_proto = WPA_PROTO_WPA;
1524 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
1525 elems.wpa_ie_len + 2);
1526 } else if (elems.osen) {
1527 params.wpa_proto = WPA_PROTO_OSEN;
1528 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.osen - 2,
1529 elems.osen_len + 2);
1530 } else
1531 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1532 if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
1533 params.p2p = 1;
1534
1535 if (wpa_s->p2pdev->set_sta_uapsd)
1536 params.uapsd = wpa_s->p2pdev->sta_uapsd;
1537 else
1538 params.uapsd = -1;
1539
1540 if (wpa_drv_associate(wpa_s, &params) < 0) {
1541 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
1542 "driver failed");
1543 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1544 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1545 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1546 return;
1547 }
1548
1549 eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
1550 NULL);
1551
1552 #ifdef CONFIG_TESTING_OPTIONS
1553 wpabuf_free(wpa_s->last_assoc_req_wpa_ie);
1554 wpa_s->last_assoc_req_wpa_ie = NULL;
1555 if (params.wpa_ie)
1556 wpa_s->last_assoc_req_wpa_ie =
1557 wpabuf_alloc_copy(params.wpa_ie, params.wpa_ie_len);
1558 #endif /* CONFIG_TESTING_OPTIONS */
1559 }
1560
1561
1562 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
1563 const u8 *ies, size_t ies_len)
1564 {
1565 if (md == NULL || ies == NULL) {
1566 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
1567 os_free(wpa_s->sme.ft_ies);
1568 wpa_s->sme.ft_ies = NULL;
1569 wpa_s->sme.ft_ies_len = 0;
1570 wpa_s->sme.ft_used = 0;
1571 return 0;
1572 }
1573
1574 os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
1575 wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
1576 os_free(wpa_s->sme.ft_ies);
1577 wpa_s->sme.ft_ies = os_memdup(ies, ies_len);
1578 if (wpa_s->sme.ft_ies == NULL)
1579 return -1;
1580 wpa_s->sme.ft_ies_len = ies_len;
1581 return 0;
1582 }
1583
1584
1585 static void sme_deauth(struct wpa_supplicant *wpa_s)
1586 {
1587 int bssid_changed;
1588
1589 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1590
1591 if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
1592 WLAN_REASON_DEAUTH_LEAVING) < 0) {
1593 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
1594 "failed");
1595 }
1596 wpa_s->sme.prev_bssid_set = 0;
1597
1598 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1599 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1600 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1601 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1602 if (bssid_changed)
1603 wpas_notify_bssid_changed(wpa_s);
1604 }
1605
1606
1607 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
1608 union wpa_event_data *data)
1609 {
1610 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
1611 "status code %d", MAC2STR(wpa_s->pending_bssid),
1612 data->assoc_reject.status_code);
1613
1614 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1615
1616 #ifdef CONFIG_SAE
1617 if (wpa_s->sme.sae_pmksa_caching && wpa_s->current_ssid &&
1618 wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt)) {
1619 wpa_dbg(wpa_s, MSG_DEBUG,
1620 "PMKSA caching attempt rejected - drop PMKSA cache entry and fall back to SAE authentication");
1621 wpa_sm_aborted_cached(wpa_s->wpa);
1622 wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
1623 if (wpa_s->current_bss) {
1624 struct wpa_bss *bss = wpa_s->current_bss;
1625 struct wpa_ssid *ssid = wpa_s->current_ssid;
1626
1627 wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
1628 WLAN_REASON_DEAUTH_LEAVING);
1629 wpas_connect_work_done(wpa_s);
1630 wpa_supplicant_mark_disassoc(wpa_s);
1631 wpa_supplicant_connect(wpa_s, bss, ssid);
1632 return;
1633 }
1634 }
1635 #endif /* CONFIG_SAE */
1636
1637 /*
1638 * For now, unconditionally terminate the previous authentication. In
1639 * theory, this should not be needed, but mac80211 gets quite confused
1640 * if the authentication is left pending.. Some roaming cases might
1641 * benefit from using the previous authentication, so this could be
1642 * optimized in the future.
1643 */
1644 sme_deauth(wpa_s);
1645 }
1646
1647
1648 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
1649 union wpa_event_data *data)
1650 {
1651 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
1652 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1653 wpa_supplicant_mark_disassoc(wpa_s);
1654 }
1655
1656
1657 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
1658 union wpa_event_data *data)
1659 {
1660 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
1661 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1662 wpa_supplicant_mark_disassoc(wpa_s);
1663 }
1664
1665
1666 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
1667 struct disassoc_info *info)
1668 {
1669 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
1670 if (wpa_s->sme.prev_bssid_set) {
1671 /*
1672 * cfg80211/mac80211 can get into somewhat confused state if
1673 * the AP only disassociates us and leaves us in authenticated
1674 * state. For now, force the state to be cleared to avoid
1675 * confusing errors if we try to associate with the AP again.
1676 */
1677 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
1678 "driver state");
1679 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
1680 WLAN_REASON_DEAUTH_LEAVING);
1681 }
1682 }
1683
1684
1685 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
1686 {
1687 struct wpa_supplicant *wpa_s = eloop_ctx;
1688 if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
1689 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
1690 sme_deauth(wpa_s);
1691 }
1692 }
1693
1694
1695 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
1696 {
1697 struct wpa_supplicant *wpa_s = eloop_ctx;
1698 if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1699 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
1700 sme_deauth(wpa_s);
1701 }
1702 }
1703
1704
1705 void sme_state_changed(struct wpa_supplicant *wpa_s)
1706 {
1707 /* Make sure timers are cleaned up appropriately. */
1708 if (wpa_s->wpa_state != WPA_ASSOCIATING)
1709 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1710 if (wpa_s->wpa_state != WPA_AUTHENTICATING)
1711 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1712 }
1713
1714
1715 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
1716 const u8 *prev_pending_bssid)
1717 {
1718 /*
1719 * mac80211-workaround to force deauth on failed auth cmd,
1720 * requires us to remain in authenticating state to allow the
1721 * second authentication attempt to be continued properly.
1722 */
1723 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
1724 "to proceed after disconnection event");
1725 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1726 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1727
1728 /*
1729 * Re-arm authentication timer in case auth fails for whatever reason.
1730 */
1731 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1732 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
1733 NULL);
1734 }
1735
1736
1737 void sme_clear_on_disassoc(struct wpa_supplicant *wpa_s)
1738 {
1739 wpa_s->sme.prev_bssid_set = 0;
1740 #ifdef CONFIG_SAE
1741 wpabuf_free(wpa_s->sme.sae_token);
1742 wpa_s->sme.sae_token = NULL;
1743 sae_clear_data(&wpa_s->sme.sae);
1744 #endif /* CONFIG_SAE */
1745 #ifdef CONFIG_IEEE80211R
1746 if (wpa_s->sme.ft_ies || wpa_s->sme.ft_used)
1747 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
1748 #endif /* CONFIG_IEEE80211R */
1749 }
1750
1751
1752 void sme_deinit(struct wpa_supplicant *wpa_s)
1753 {
1754 os_free(wpa_s->sme.ft_ies);
1755 wpa_s->sme.ft_ies = NULL;
1756 wpa_s->sme.ft_ies_len = 0;
1757 #ifdef CONFIG_IEEE80211W
1758 sme_stop_sa_query(wpa_s);
1759 #endif /* CONFIG_IEEE80211W */
1760 sme_clear_on_disassoc(wpa_s);
1761
1762 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1763 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1764 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1765 }
1766
1767
1768 static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
1769 const u8 *chan_list, u8 num_channels,
1770 u8 num_intol)
1771 {
1772 struct ieee80211_2040_bss_coex_ie *bc_ie;
1773 struct ieee80211_2040_intol_chan_report *ic_report;
1774 struct wpabuf *buf;
1775
1776 wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR
1777 " (num_channels=%u num_intol=%u)",
1778 MAC2STR(wpa_s->bssid), num_channels, num_intol);
1779 wpa_hexdump(MSG_DEBUG, "SME: 20/40 BSS Intolerant Channels",
1780 chan_list, num_channels);
1781
1782 buf = wpabuf_alloc(2 + /* action.category + action_code */
1783 sizeof(struct ieee80211_2040_bss_coex_ie) +
1784 sizeof(struct ieee80211_2040_intol_chan_report) +
1785 num_channels);
1786 if (buf == NULL)
1787 return;
1788
1789 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
1790 wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
1791
1792 bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
1793 bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
1794 bc_ie->length = 1;
1795 if (num_intol)
1796 bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
1797
1798 if (num_channels > 0) {
1799 ic_report = wpabuf_put(buf, sizeof(*ic_report));
1800 ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
1801 ic_report->length = num_channels + 1;
1802 ic_report->op_class = 0;
1803 os_memcpy(wpabuf_put(buf, num_channels), chan_list,
1804 num_channels);
1805 }
1806
1807 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1808 wpa_s->own_addr, wpa_s->bssid,
1809 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
1810 wpa_msg(wpa_s, MSG_INFO,
1811 "SME: Failed to send 20/40 BSS Coexistence frame");
1812 }
1813
1814 wpabuf_free(buf);
1815 }
1816
1817
1818 int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
1819 {
1820 struct wpa_bss *bss;
1821 const u8 *ie;
1822 u16 ht_cap;
1823 u8 chan_list[P2P_MAX_CHANNELS], channel;
1824 u8 num_channels = 0, num_intol = 0, i;
1825
1826 if (!wpa_s->sme.sched_obss_scan)
1827 return 0;
1828
1829 wpa_s->sme.sched_obss_scan = 0;
1830 if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
1831 return 1;
1832
1833 /*
1834 * Check whether AP uses regulatory triplet or channel triplet in
1835 * country info. Right now the operating class of the BSS channel
1836 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
1837 * based on the assumption that operating class triplet is not used in
1838 * beacon frame. If the First Channel Number/Operating Extension
1839 * Identifier octet has a positive integer value of 201 or greater,
1840 * then its operating class triplet.
1841 *
1842 * TODO: If Supported Operating Classes element is present in beacon
1843 * frame, have to lookup operating class in Annex E and fill them in
1844 * 2040 coex frame.
1845 */
1846 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
1847 if (ie && (ie[1] >= 6) && (ie[5] >= 201))
1848 return 1;
1849
1850 os_memset(chan_list, 0, sizeof(chan_list));
1851
1852 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1853 /* Skip other band bss */
1854 enum hostapd_hw_mode mode;
1855 mode = ieee80211_freq_to_chan(bss->freq, &channel);
1856 if (mode != HOSTAPD_MODE_IEEE80211G &&
1857 mode != HOSTAPD_MODE_IEEE80211B)
1858 continue;
1859
1860 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
1861 ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
1862 wpa_printf(MSG_DEBUG, "SME OBSS scan BSS " MACSTR
1863 " freq=%u chan=%u ht_cap=0x%x",
1864 MAC2STR(bss->bssid), bss->freq, channel, ht_cap);
1865
1866 if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
1867 if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
1868 num_intol++;
1869
1870 /* Check whether the channel is already considered */
1871 for (i = 0; i < num_channels; i++) {
1872 if (channel == chan_list[i])
1873 break;
1874 }
1875 if (i != num_channels)
1876 continue;
1877
1878 chan_list[num_channels++] = channel;
1879 }
1880 }
1881
1882 sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
1883 return 1;
1884 }
1885
1886
1887 static void wpa_obss_scan_freqs_list(struct wpa_supplicant *wpa_s,
1888 struct wpa_driver_scan_params *params)
1889 {
1890 /* Include only affected channels */
1891 struct hostapd_hw_modes *mode;
1892 int count, i;
1893 int start, end;
1894
1895 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
1896 HOSTAPD_MODE_IEEE80211G);
1897 if (mode == NULL) {
1898 /* No channels supported in this band - use empty list */
1899 params->freqs = os_zalloc(sizeof(int));
1900 return;
1901 }
1902
1903 if (wpa_s->sme.ht_sec_chan == HT_SEC_CHAN_UNKNOWN &&
1904 wpa_s->current_bss) {
1905 const u8 *ie;
1906
1907 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_OPERATION);
1908 if (ie && ie[1] >= 2) {
1909 u8 o;
1910
1911 o = ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
1912 if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
1913 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
1914 else if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
1915 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
1916 }
1917 }
1918
1919 start = wpa_s->assoc_freq - 10;
1920 end = wpa_s->assoc_freq + 10;
1921 switch (wpa_s->sme.ht_sec_chan) {
1922 case HT_SEC_CHAN_UNKNOWN:
1923 /* HT40+ possible on channels 1..9 */
1924 if (wpa_s->assoc_freq <= 2452)
1925 start -= 20;
1926 /* HT40- possible on channels 5-13 */
1927 if (wpa_s->assoc_freq >= 2432)
1928 end += 20;
1929 break;
1930 case HT_SEC_CHAN_ABOVE:
1931 end += 20;
1932 break;
1933 case HT_SEC_CHAN_BELOW:
1934 start -= 20;
1935 break;
1936 }
1937 wpa_printf(MSG_DEBUG,
1938 "OBSS: assoc_freq %d possible affected range %d-%d",
1939 wpa_s->assoc_freq, start, end);
1940
1941 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
1942 if (params->freqs == NULL)
1943 return;
1944 for (count = 0, i = 0; i < mode->num_channels; i++) {
1945 int freq;
1946
1947 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
1948 continue;
1949 freq = mode->channels[i].freq;
1950 if (freq - 10 >= end || freq + 10 <= start)
1951 continue; /* not affected */
1952 params->freqs[count++] = freq;
1953 }
1954 }
1955
1956
1957 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1958 {
1959 struct wpa_supplicant *wpa_s = eloop_ctx;
1960 struct wpa_driver_scan_params params;
1961
1962 if (!wpa_s->current_bss) {
1963 wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
1964 return;
1965 }
1966
1967 os_memset(&params, 0, sizeof(params));
1968 wpa_obss_scan_freqs_list(wpa_s, &params);
1969 params.low_priority = 1;
1970 wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
1971
1972 if (wpa_supplicant_trigger_scan(wpa_s, &params))
1973 wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
1974 else
1975 wpa_s->sme.sched_obss_scan = 1;
1976 os_free(params.freqs);
1977
1978 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1979 sme_obss_scan_timeout, wpa_s, NULL);
1980 }
1981
1982
1983 void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
1984 {
1985 const u8 *ie;
1986 struct wpa_bss *bss = wpa_s->current_bss;
1987 struct wpa_ssid *ssid = wpa_s->current_ssid;
1988 struct hostapd_hw_modes *hw_mode = NULL;
1989 int i;
1990
1991 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1992 wpa_s->sme.sched_obss_scan = 0;
1993 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
1994 if (!enable)
1995 return;
1996
1997 /*
1998 * Schedule OBSS scan if driver is using station SME in wpa_supplicant
1999 * or it expects OBSS scan to be performed by wpa_supplicant.
2000 */
2001 if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
2002 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
2003 ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
2004 return;
2005
2006 if (!wpa_s->hw.modes)
2007 return;
2008
2009 /* only HT caps in 11g mode are relevant */
2010 for (i = 0; i < wpa_s->hw.num_modes; i++) {
2011 hw_mode = &wpa_s->hw.modes[i];
2012 if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
2013 break;
2014 }
2015
2016 /* Driver does not support HT40 for 11g or doesn't have 11g. */
2017 if (i == wpa_s->hw.num_modes || !hw_mode ||
2018 !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2019 return;
2020
2021 if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
2022 return; /* Not associated on 2.4 GHz band */
2023
2024 /* Check whether AP supports HT40 */
2025 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
2026 if (!ie || ie[1] < 2 ||
2027 !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2028 return; /* AP does not support HT40 */
2029
2030 ie = wpa_bss_get_ie(wpa_s->current_bss,
2031 WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
2032 if (!ie || ie[1] < 14)
2033 return; /* AP does not request OBSS scans */
2034
2035 wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
2036 if (wpa_s->sme.obss_scan_int < 10) {
2037 wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
2038 "replaced with the minimum 10 sec",
2039 wpa_s->sme.obss_scan_int);
2040 wpa_s->sme.obss_scan_int = 10;
2041 }
2042 wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
2043 wpa_s->sme.obss_scan_int);
2044 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
2045 sme_obss_scan_timeout, wpa_s, NULL);
2046 }
2047
2048
2049 #ifdef CONFIG_IEEE80211W
2050
2051 static const unsigned int sa_query_max_timeout = 1000;
2052 static const unsigned int sa_query_retry_timeout = 201;
2053
2054 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
2055 {
2056 u32 tu;
2057 struct os_reltime now, passed;
2058 os_get_reltime(&now);
2059 os_reltime_sub(&now, &wpa_s->sme.sa_query_start, &passed);
2060 tu = (passed.sec * 1000000 + passed.usec) / 1024;
2061 if (sa_query_max_timeout < tu) {
2062 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
2063 sme_stop_sa_query(wpa_s);
2064 wpa_supplicant_deauthenticate(
2065 wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
2066 return 1;
2067 }
2068
2069 return 0;
2070 }
2071
2072
2073 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
2074 const u8 *trans_id)
2075 {
2076 u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
2077 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
2078 MACSTR, MAC2STR(wpa_s->bssid));
2079 wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
2080 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
2081 req[0] = WLAN_ACTION_SA_QUERY;
2082 req[1] = WLAN_SA_QUERY_REQUEST;
2083 os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
2084 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2085 wpa_s->own_addr, wpa_s->bssid,
2086 req, sizeof(req), 0) < 0)
2087 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
2088 "Request");
2089 }
2090
2091
2092 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
2093 {
2094 struct wpa_supplicant *wpa_s = eloop_ctx;
2095 unsigned int timeout, sec, usec;
2096 u8 *trans_id, *nbuf;
2097
2098 if (wpa_s->sme.sa_query_count > 0 &&
2099 sme_check_sa_query_timeout(wpa_s))
2100 return;
2101
2102 nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
2103 wpa_s->sme.sa_query_count + 1,
2104 WLAN_SA_QUERY_TR_ID_LEN);
2105 if (nbuf == NULL) {
2106 sme_stop_sa_query(wpa_s);
2107 return;
2108 }
2109 if (wpa_s->sme.sa_query_count == 0) {
2110 /* Starting a new SA Query procedure */
2111 os_get_reltime(&wpa_s->sme.sa_query_start);
2112 }
2113 trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
2114 wpa_s->sme.sa_query_trans_id = nbuf;
2115 wpa_s->sme.sa_query_count++;
2116
2117 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
2118 wpa_printf(MSG_DEBUG, "Could not generate SA Query ID");
2119 sme_stop_sa_query(wpa_s);
2120 return;
2121 }
2122
2123 timeout = sa_query_retry_timeout;
2124 sec = ((timeout / 1000) * 1024) / 1000;
2125 usec = (timeout % 1000) * 1024;
2126 eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
2127
2128 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
2129 wpa_s->sme.sa_query_count);
2130
2131 sme_send_sa_query_req(wpa_s, trans_id);
2132 }
2133
2134
2135 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
2136 {
2137 sme_sa_query_timer(wpa_s, NULL);
2138 }
2139
2140
2141 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
2142 {
2143 eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
2144 os_free(wpa_s->sme.sa_query_trans_id);
2145 wpa_s->sme.sa_query_trans_id = NULL;
2146 wpa_s->sme.sa_query_count = 0;
2147 }
2148
2149
2150 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
2151 const u8 *da, u16 reason_code)
2152 {
2153 struct wpa_ssid *ssid;
2154 struct os_reltime now;
2155
2156 if (wpa_s->wpa_state != WPA_COMPLETED)
2157 return;
2158 ssid = wpa_s->current_ssid;
2159 if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION)
2160 return;
2161 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
2162 return;
2163 if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
2164 reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
2165 return;
2166 if (wpa_s->sme.sa_query_count > 0)
2167 return;
2168
2169 os_get_reltime(&now);
2170 if (wpa_s->sme.last_unprot_disconnect.sec &&
2171 !os_reltime_expired(&now, &wpa_s->sme.last_unprot_disconnect, 10))
2172 return; /* limit SA Query procedure frequency */
2173 wpa_s->sme.last_unprot_disconnect = now;
2174
2175 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
2176 "possible AP/STA state mismatch - trigger SA Query");
2177 sme_start_sa_query(wpa_s);
2178 }
2179
2180
2181 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
2182 const u8 *data, size_t len)
2183 {
2184 int i;
2185
2186 if (wpa_s->sme.sa_query_trans_id == NULL ||
2187 len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
2188 data[0] != WLAN_SA_QUERY_RESPONSE)
2189 return;
2190 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
2191 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
2192
2193 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
2194 return;
2195
2196 for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
2197 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
2198 i * WLAN_SA_QUERY_TR_ID_LEN,
2199 data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
2200 break;
2201 }
2202
2203 if (i >= wpa_s->sme.sa_query_count) {
2204 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
2205 "transaction identifier found");
2206 return;
2207 }
2208
2209 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
2210 "from " MACSTR, MAC2STR(sa));
2211 sme_stop_sa_query(wpa_s);
2212 }
2213
2214 #endif /* CONFIG_IEEE80211W */