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