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