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