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