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