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