]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/sme.c
Allow PMF to be enabled by default
[thirdparty/hostap.git] / wpa_supplicant / sme.c
CommitLineData
c2a04078
JM
1/*
2 * wpa_supplicant - SME
71024cb2 3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
c2a04078 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
c2a04078
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
7d878ca7 12#include "utils/eloop.h"
90973fb2 13#include "common/ieee802_11_defs.h"
d9a27b04 14#include "common/ieee802_11_common.h"
c2a04078 15#include "eapol_supp/eapol_supp_sm.h"
90973fb2 16#include "common/wpa_common.h"
3acb5005
JM
17#include "rsn_supp/wpa.h"
18#include "rsn_supp/pmksa_cache.h"
c2a04078
JM
19#include "config.h"
20#include "wpa_supplicant_i.h"
2d5b792d 21#include "driver_i.h"
c2a04078
JM
22#include "wpas_glue.h"
23#include "wps_supplicant.h"
5f3a6aa0 24#include "p2p_supplicant.h"
8bac466b 25#include "notify.h"
6fa81a3b 26#include "bss.h"
9ba9fa07 27#include "scan.h"
c2a04078 28#include "sme.h"
cb418324 29#include "hs20_supplicant.h"
c2a04078 30
e29853bb
BG
31#define SME_AUTH_TIMEOUT 5
32#define SME_ASSOC_TIMEOUT 5
33
34static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
35static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
c3701c66 36static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
e29853bb
BG
37#ifdef CONFIG_IEEE80211W
38static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
39#endif /* CONFIG_IEEE80211W */
40
41
c10347f2
JM
42#ifdef CONFIG_SAE
43
44static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s)
45{
46 struct wpabuf *buf;
47
21af6d15 48 buf = wpabuf_alloc(4 + 2);
c10347f2
JM
49 if (buf == NULL)
50 return NULL;
51
52 wpabuf_put_le16(buf, 1); /* Transaction seq# */
53 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
21af6d15
JM
54 wpabuf_put_le16(buf, 19); /* Finite Cyclic Group */
55 /* TODO: Anti-Clogging Token (if requested) */
56 /* TODO: Scalar */
57 /* TODO: Element */
c10347f2
JM
58
59 return buf;
60}
61
62
63static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s)
64{
65 struct wpabuf *buf;
66
21af6d15 67 buf = wpabuf_alloc(4 + 2);
c10347f2
JM
68 if (buf == NULL)
69 return NULL;
70
71 wpabuf_put_le16(buf, 2); /* Transaction seq# */
72 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
21af6d15
JM
73 wpabuf_put_le16(buf, wpa_s->sme.sae_send_confirm);
74 wpa_s->sme.sae_send_confirm++;
75 /* TODO: Confirm */
c10347f2
JM
76
77 return buf;
78}
79
80#endif /* CONFIG_SAE */
81
82
83void sme_send_authentication(struct wpa_supplicant *wpa_s,
84 struct wpa_bss *bss, struct wpa_ssid *ssid,
85 int start)
c2a04078
JM
86{
87 struct wpa_driver_auth_params params;
8bac466b 88 struct wpa_ssid *old_ssid;
fdbe50ed 89#ifdef CONFIG_IEEE80211R
c2a04078 90 const u8 *ie;
fdbe50ed 91#endif /* CONFIG_IEEE80211R */
c2a04078
JM
92#ifdef CONFIG_IEEE80211R
93 const u8 *md = NULL;
94#endif /* CONFIG_IEEE80211R */
8bac466b 95 int i, bssid_changed;
c10347f2 96 struct wpabuf *resp = NULL;
c2a04078
JM
97
98 if (bss == NULL) {
f049052b
BG
99 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
100 "the network");
c2a04078
JM
101 return;
102 }
103
8f770587
JM
104 wpa_s->current_bss = bss;
105
c2a04078
JM
106 os_memset(&params, 0, sizeof(params));
107 wpa_s->reassociate = 0;
108
109 params.freq = bss->freq;
110 params.bssid = bss->bssid;
6fa81a3b
JM
111 params.ssid = bss->ssid;
112 params.ssid_len = bss->ssid_len;
2f4f73b1 113 params.p2p = ssid->p2p_group;
c2a04078 114
62fa124c
JM
115 if (wpa_s->sme.ssid_len != params.ssid_len ||
116 os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
117 wpa_s->sme.prev_bssid_set = 0;
118
c2a04078
JM
119 wpa_s->sme.freq = params.freq;
120 os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
121 wpa_s->sme.ssid_len = params.ssid_len;
122
abd9fafa 123 params.auth_alg = WPA_AUTH_ALG_OPEN;
c2a04078
JM
124#ifdef IEEE8021X_EAPOL
125 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
126 if (ssid->leap) {
127 if (ssid->non_leap == 0)
abd9fafa 128 params.auth_alg = WPA_AUTH_ALG_LEAP;
c2a04078 129 else
abd9fafa 130 params.auth_alg |= WPA_AUTH_ALG_LEAP;
c2a04078
JM
131 }
132 }
133#endif /* IEEE8021X_EAPOL */
f049052b
BG
134 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
135 params.auth_alg);
c2a04078 136 if (ssid->auth_alg) {
abd9fafa 137 params.auth_alg = ssid->auth_alg;
f049052b
BG
138 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
139 "0x%x", params.auth_alg);
c2a04078 140 }
c10347f2
JM
141#ifdef CONFIG_SAE
142 if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
143 const u8 *rsn;
144 struct wpa_ie_data ied;
145
146 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
147 if (rsn &&
148 wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0) {
149 if (wpa_key_mgmt_sae(ied.key_mgmt)) {
150 wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
151 params.auth_alg = WPA_AUTH_ALG_SAE;
152 }
153 }
154 }
155#endif /* CONFIG_SAE */
c2a04078 156
a0b2f99b
JM
157 for (i = 0; i < NUM_WEP_KEYS; i++) {
158 if (ssid->wep_key_len[i])
159 params.wep_key[i] = ssid->wep_key[i];
160 params.wep_key_len[i] = ssid->wep_key_len[i];
161 }
162 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
163
8bac466b 164 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
c2a04078
JM
165 os_memset(wpa_s->bssid, 0, ETH_ALEN);
166 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
8bac466b
JM
167 if (bssid_changed)
168 wpas_notify_bssid_changed(wpa_s);
c2a04078 169
f337f0e9
JM
170 if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
171 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
0bf927a0 172 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
c2a04078 173 int try_opportunistic;
6e202021
JM
174 try_opportunistic = (ssid->proactive_key_caching < 0 ?
175 wpa_s->conf->okc :
176 ssid->proactive_key_caching) &&
c2a04078
JM
177 (ssid->proto & WPA_PROTO_RSN);
178 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
179 wpa_s->current_ssid,
180 try_opportunistic) == 0)
181 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
182 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
183 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
184 wpa_s->sme.assoc_req_ie,
185 &wpa_s->sme.assoc_req_ie_len)) {
f049052b
BG
186 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
187 "key management and encryption suites");
c2a04078
JM
188 return;
189 }
a3f7e518
JM
190 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
191 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
192 /*
193 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
194 * use non-WPA since the scan results did not indicate that the
195 * AP is using WPA or WPA2.
196 */
197 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
198 wpa_s->sme.assoc_req_ie_len = 0;
0bf927a0 199 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
c2a04078
JM
200 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
201 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
202 wpa_s->sme.assoc_req_ie,
203 &wpa_s->sme.assoc_req_ie_len)) {
f049052b
BG
204 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
205 "key management and encryption suites (no "
206 "scan results)");
c2a04078
JM
207 return;
208 }
209#ifdef CONFIG_WPS
210 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
211 struct wpabuf *wps_ie;
212 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
213 if (wps_ie && wpabuf_len(wps_ie) <=
214 sizeof(wpa_s->sme.assoc_req_ie)) {
215 wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
216 os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
217 wpa_s->sme.assoc_req_ie_len);
218 } else
219 wpa_s->sme.assoc_req_ie_len = 0;
220 wpabuf_free(wps_ie);
221 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
222#endif /* CONFIG_WPS */
223 } else {
224 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
225 wpa_s->sme.assoc_req_ie_len = 0;
226 }
227
228#ifdef CONFIG_IEEE80211R
6fa81a3b 229 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
c2a04078
JM
230 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
231 md = ie + 2;
e7846b68 232 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
c2a04078
JM
233 if (md) {
234 /* Prepare for the next transition */
76b7981d 235 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
c2a04078
JM
236 }
237
0bf927a0 238 if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
c2a04078
JM
239 if (wpa_s->sme.assoc_req_ie_len + 5 <
240 sizeof(wpa_s->sme.assoc_req_ie)) {
241 struct rsn_mdie *mdie;
242 u8 *pos = wpa_s->sme.assoc_req_ie +
243 wpa_s->sme.assoc_req_ie_len;
244 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
245 *pos++ = sizeof(*mdie);
246 mdie = (struct rsn_mdie *) pos;
247 os_memcpy(mdie->mobility_domain, md,
248 MOBILITY_DOMAIN_ID_LEN);
f4ec630d 249 mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
c2a04078
JM
250 wpa_s->sme.assoc_req_ie_len += 5;
251 }
252
253 if (wpa_s->sme.ft_used &&
0d7b4409
JM
254 os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
255 wpa_sm_has_ptk(wpa_s->wpa)) {
f049052b
BG
256 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
257 "over-the-air");
abd9fafa 258 params.auth_alg = WPA_AUTH_ALG_FT;
c2a04078
JM
259 params.ie = wpa_s->sme.ft_ies;
260 params.ie_len = wpa_s->sme.ft_ies_len;
261 }
262 }
263#endif /* CONFIG_IEEE80211R */
264
265#ifdef CONFIG_IEEE80211W
62d49803
JM
266 wpa_s->sme.mfp = ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
267 wpa_s->conf->pmf : ssid->ieee80211w;
268 if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
6fa81a3b 269 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
c2a04078
JM
270 struct wpa_ie_data _ie;
271 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
272 _ie.capabilities &
273 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
f049052b
BG
274 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
275 "MFP: require MFP");
c2a04078
JM
276 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
277 }
278 }
279#endif /* CONFIG_IEEE80211W */
280
5f3a6aa0
JM
281#ifdef CONFIG_P2P
282 if (wpa_s->global->p2p) {
283 u8 *pos;
284 size_t len;
285 int res;
5f3a6aa0
JM
286 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
287 len = sizeof(wpa_s->sme.assoc_req_ie) -
288 wpa_s->sme.assoc_req_ie_len;
ffad8858
JM
289 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
290 ssid->p2p_group);
5f3a6aa0
JM
291 if (res >= 0)
292 wpa_s->sme.assoc_req_ie_len += res;
293 }
294#endif /* CONFIG_P2P */
295
cb418324
JM
296#ifdef CONFIG_HS20
297 if (wpa_s->conf->hs20) {
298 struct wpabuf *hs20;
299 hs20 = wpabuf_alloc(20);
300 if (hs20) {
301 wpas_hs20_add_indication(hs20);
302 os_memcpy(wpa_s->sme.assoc_req_ie +
303 wpa_s->sme.assoc_req_ie_len,
304 wpabuf_head(hs20), wpabuf_len(hs20));
305 wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
306 wpabuf_free(hs20);
307 }
308 }
309#endif /* CONFIG_HS20 */
310
92cbcf91
JM
311#ifdef CONFIG_INTERWORKING
312 if (wpa_s->conf->interworking) {
313 u8 *pos = wpa_s->sme.assoc_req_ie;
314 if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
315 pos += 2 + pos[1];
316 os_memmove(pos + 6, pos,
317 wpa_s->sme.assoc_req_ie_len -
318 (pos - wpa_s->sme.assoc_req_ie));
319 wpa_s->sme.assoc_req_ie_len += 6;
320 *pos++ = WLAN_EID_EXT_CAPAB;
321 *pos++ = 4;
322 *pos++ = 0x00;
323 *pos++ = 0x00;
324 *pos++ = 0x00;
325 *pos++ = 0x80; /* Bit 31 - Interworking */
326 }
327#endif /* CONFIG_INTERWORKING */
328
c10347f2
JM
329#ifdef CONFIG_SAE
330 if (params.auth_alg == WPA_AUTH_ALG_SAE) {
331 if (start)
332 resp = sme_auth_build_sae_commit(wpa_s);
333 else
334 resp = sme_auth_build_sae_confirm(wpa_s);
335 if (resp == NULL)
336 return;
337 params.sae_data = wpabuf_head(resp);
338 params.sae_data_len = wpabuf_len(resp);
21af6d15 339 wpa_s->sme.sae_state = start ? SME_SAE_COMMIT : SME_SAE_CONFIRM;
c10347f2
JM
340 }
341#endif /* CONFIG_SAE */
342
a4cba8f1 343 wpa_supplicant_cancel_sched_scan(wpa_s);
c2a04078
JM
344 wpa_supplicant_cancel_scan(wpa_s);
345
f049052b 346 wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
c2a04078
JM
347 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
348 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
349
350 wpa_clear_keys(wpa_s, bss->bssid);
351 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
8bac466b 352 old_ssid = wpa_s->current_ssid;
c2a04078
JM
353 wpa_s->current_ssid = ssid;
354 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
355 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
356 if (old_ssid != wpa_s->current_ssid)
357 wpas_notify_network_changed(wpa_s);
c2a04078 358
62c72d72 359 wpa_s->sme.auth_alg = params.auth_alg;
c2a04078 360 if (wpa_drv_authenticate(wpa_s, &params) < 0) {
f049052b 361 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
c2a04078 362 "driver failed");
ed57c590 363 wpas_connection_failed(wpa_s, bss->bssid);
1193dc8f 364 wpa_supplicant_mark_disassoc(wpa_s);
c10347f2 365 wpabuf_free(resp);
c2a04078
JM
366 return;
367 }
368
e29853bb
BG
369 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
370 NULL);
c2a04078
JM
371
372 /*
373 * Association will be started based on the authentication event from
374 * the driver.
375 */
c10347f2
JM
376
377 wpabuf_free(resp);
378}
379
380
381void sme_authenticate(struct wpa_supplicant *wpa_s,
382 struct wpa_bss *bss, struct wpa_ssid *ssid)
383{
21af6d15
JM
384 wpa_s->sme.sae_state = SME_SAE_INIT;
385 wpa_s->sme.sae_send_confirm = 0;
c10347f2 386 sme_send_authentication(wpa_s, bss, ssid, 1);
c2a04078
JM
387}
388
389
c10347f2 390#ifdef CONFIG_SAE
21af6d15
JM
391
392static int sme_sae_process_commit(struct wpa_supplicant *wpa_s, const u8 *data,
393 size_t len)
394{
395 /* Check Finite Cyclic Group */
396 if (len < 2)
397 return -1;
398 if (WPA_GET_LE16(data) != 19) {
399 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
400 WPA_GET_LE16(data));
401 return -1;
402 }
403
404 /* TODO */
405
406 return 0;
407}
408
409
410static int sme_sae_process_confirm(struct wpa_supplicant *wpa_s, const u8 *data,
411 size_t len)
412{
413 u16 rc;
414
415 if (len < 2)
416 return -1;
417 rc = WPA_GET_LE16(data);
418 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", rc);
419
420 /* TODO */
421 return 0;
422}
423
424
c10347f2
JM
425static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
426 u16 status_code, const u8 *data, size_t len)
427{
428 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
429 "status code %u", auth_transaction, status_code);
430 wpa_hexdump(MSG_DEBUG, "SME: SAE fields", data, len);
431
432 if (status_code != WLAN_STATUS_SUCCESS)
433 return -1;
434
435 if (auth_transaction == 1) {
436 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
437 if (wpa_s->current_bss == NULL ||
438 wpa_s->current_ssid == NULL)
439 return -1;
21af6d15
JM
440 if (wpa_s->sme.sae_state != SME_SAE_COMMIT)
441 return -1;
442 if (sme_sae_process_commit(wpa_s, data, len) < 0)
443 return -1;
c10347f2
JM
444 sme_send_authentication(wpa_s, wpa_s->current_bss,
445 wpa_s->current_ssid, 0);
446 return 0;
447 } else if (auth_transaction == 2) {
448 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
21af6d15
JM
449 if (wpa_s->sme.sae_state != SME_SAE_CONFIRM)
450 return -1;
451 if (sme_sae_process_confirm(wpa_s, data, len) < 0)
452 return -1;
c10347f2
JM
453 return 1;
454 }
455
456 return -1;
457}
458#endif /* CONFIG_SAE */
459
460
c2a04078
JM
461void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
462{
c2a04078
JM
463 struct wpa_ssid *ssid = wpa_s->current_ssid;
464
465 if (ssid == NULL) {
f049052b
BG
466 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
467 "when network is not selected");
c2a04078
JM
468 return;
469 }
470
471 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
f049052b
BG
472 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
473 "when not in authenticating state");
c2a04078
JM
474 return;
475 }
476
477 if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
f049052b
BG
478 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
479 "unexpected peer " MACSTR,
480 MAC2STR(data->auth.peer));
c2a04078
JM
481 return;
482 }
483
f049052b 484 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
c10347f2 485 " auth_type=%d auth_transaction=%d status_code=%d",
f049052b 486 MAC2STR(data->auth.peer), data->auth.auth_type,
c10347f2 487 data->auth.auth_transaction, data->auth.status_code);
c2a04078
JM
488 wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
489 data->auth.ies, data->auth.ies_len);
490
e29853bb
BG
491 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
492
c10347f2
JM
493#ifdef CONFIG_SAE
494 if (data->auth.auth_type == WLAN_AUTH_SAE) {
21af6d15
JM
495 int res;
496 res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
497 data->auth.status_code, data->auth.ies,
498 data->auth.ies_len);
499 if (res < 0) {
500 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
501 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
502
503 }
504 if (res != 1)
c10347f2
JM
505 return;
506 }
507#endif /* CONFIG_SAE */
508
c2a04078 509 if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
f049052b
BG
510 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
511 "code %d)", data->auth.status_code);
cb1583f6
SO
512
513 if (data->auth.status_code !=
514 WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
515 wpa_s->sme.auth_alg == data->auth.auth_type ||
7e6646c7 516 wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
0fb337c1 517 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
c1c02342 518 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
cb1583f6 519 return;
7e6646c7 520 }
cb1583f6
SO
521
522 switch (data->auth.auth_type) {
523 case WLAN_AUTH_OPEN:
524 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
525
f049052b 526 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
cb1583f6
SO
527 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
528 wpa_s->current_ssid);
529 return;
530
531 case WLAN_AUTH_SHARED_KEY:
532 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
533
f049052b 534 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
cb1583f6
SO
535 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
536 wpa_s->current_ssid);
537 return;
538
539 default:
540 return;
541 }
c2a04078
JM
542 }
543
544#ifdef CONFIG_IEEE80211R
545 if (data->auth.auth_type == WLAN_AUTH_FT) {
546 union wpa_event_data edata;
547 os_memset(&edata, 0, sizeof(edata));
548 edata.ft_ies.ies = data->auth.ies;
549 edata.ft_ies.ies_len = data->auth.ies_len;
550 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
551 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
552 }
553#endif /* CONFIG_IEEE80211R */
554
71024cb2
JM
555 sme_associate(wpa_s, ssid->mode, data->auth.peer,
556 data->auth.auth_type);
557}
558
559
560void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
561 const u8 *bssid, u16 auth_type)
562{
563 struct wpa_driver_associate_params params;
d9a27b04 564 struct ieee802_11_elems elems;
80e8a5ee
BG
565#ifdef CONFIG_HT_OVERRIDES
566 struct ieee80211_ht_capabilities htcaps;
567 struct ieee80211_ht_capabilities htcaps_mask;
568#endif /* CONFIG_HT_OVERRIDES */
71024cb2 569
c2a04078 570 os_memset(&params, 0, sizeof(params));
71024cb2 571 params.bssid = bssid;
c2a04078
JM
572 params.ssid = wpa_s->sme.ssid;
573 params.ssid_len = wpa_s->sme.ssid_len;
574 params.freq = wpa_s->sme.freq;
1f6c0ab8
BS
575 params.bg_scan_period = wpa_s->current_ssid ?
576 wpa_s->current_ssid->bg_scan_period : -1;
c2a04078
JM
577 params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
578 wpa_s->sme.assoc_req_ie : NULL;
579 params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
aca01605
JM
580 params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
581 params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
80e8a5ee
BG
582#ifdef CONFIG_HT_OVERRIDES
583 os_memset(&htcaps, 0, sizeof(htcaps));
584 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
585 params.htcaps = (u8 *) &htcaps;
586 params.htcaps_mask = (u8 *) &htcaps_mask;
587 wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
588#endif /* CONFIG_HT_OVERRIDES */
c2a04078 589#ifdef CONFIG_IEEE80211R
71024cb2 590 if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
c2a04078
JM
591 params.wpa_ie = wpa_s->sme.ft_ies;
592 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
593 }
594#endif /* CONFIG_IEEE80211R */
71024cb2 595 params.mode = mode;
c2a04078 596 params.mgmt_frame_protection = wpa_s->sme.mfp;
62fa124c
JM
597 if (wpa_s->sme.prev_bssid_set)
598 params.prev_bssid = wpa_s->sme.prev_bssid;
c2a04078
JM
599
600 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
601 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
602 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
603 params.freq);
604
605 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
606
9efc3f2a
JM
607 if (params.wpa_ie == NULL ||
608 ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
d9a27b04 609 < 0) {
f049052b 610 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
d9a27b04
JM
611 os_memset(&elems, 0, sizeof(elems));
612 }
64fa840a
JM
613 if (elems.rsn_ie) {
614 params.wpa_proto = WPA_PROTO_RSN;
d9a27b04
JM
615 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
616 elems.rsn_ie_len + 2);
64fa840a
JM
617 } else if (elems.wpa_ie) {
618 params.wpa_proto = WPA_PROTO_WPA;
d9a27b04
JM
619 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
620 elems.wpa_ie_len + 2);
64fa840a 621 } else
d9a27b04 622 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
ffad8858 623 if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
6e3f4b89 624 params.p2p = 1;
d9a27b04 625
eea2fd9e
JM
626 if (wpa_s->parent->set_sta_uapsd)
627 params.uapsd = wpa_s->parent->sta_uapsd;
628 else
629 params.uapsd = -1;
630
c2a04078 631 if (wpa_drv_associate(wpa_s, &params) < 0) {
f049052b
BG
632 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
633 "driver failed");
e5ad96b7 634 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
c1c02342 635 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
e5ad96b7 636 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
c2a04078
JM
637 return;
638 }
639
e29853bb
BG
640 eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
641 NULL);
c2a04078
JM
642}
643
644
645int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
646 const u8 *ies, size_t ies_len)
647{
648 if (md == NULL || ies == NULL) {
f049052b 649 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
c2a04078
JM
650 os_free(wpa_s->sme.ft_ies);
651 wpa_s->sme.ft_ies = NULL;
652 wpa_s->sme.ft_ies_len = 0;
653 wpa_s->sme.ft_used = 0;
654 return 0;
655 }
656
657 os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
658 wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
659 os_free(wpa_s->sme.ft_ies);
660 wpa_s->sme.ft_ies = os_malloc(ies_len);
661 if (wpa_s->sme.ft_ies == NULL)
662 return -1;
663 os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
664 wpa_s->sme.ft_ies_len = ies_len;
665 return 0;
666}
efa46078
JM
667
668
e29853bb 669static void sme_deauth(struct wpa_supplicant *wpa_s)
efa46078 670{
8bac466b
JM
671 int bssid_changed;
672
8bac466b 673 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
76d11d3f 674
76d11d3f
JM
675 if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
676 WLAN_REASON_DEAUTH_LEAVING) < 0) {
f049052b
BG
677 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
678 "failed");
76d11d3f 679 }
62fa124c 680 wpa_s->sme.prev_bssid_set = 0;
76d11d3f 681
0fb337c1 682 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
76d11d3f 683 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
efa46078
JM
684 os_memset(wpa_s->bssid, 0, ETH_ALEN);
685 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
8bac466b
JM
686 if (bssid_changed)
687 wpas_notify_bssid_changed(wpa_s);
efa46078 688}
da1fb17c
JM
689
690
e29853bb
BG
691void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
692 union wpa_event_data *data)
693{
694 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
695 "status code %d", MAC2STR(wpa_s->pending_bssid),
696 data->assoc_reject.status_code);
697
698 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
699
700 /*
701 * For now, unconditionally terminate the previous authentication. In
702 * theory, this should not be needed, but mac80211 gets quite confused
703 * if the authentication is left pending.. Some roaming cases might
704 * benefit from using the previous authentication, so this could be
705 * optimized in the future.
706 */
707 sme_deauth(wpa_s);
708}
709
710
da1fb17c
JM
711void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
712 union wpa_event_data *data)
713{
f049052b 714 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
0fb337c1 715 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1193dc8f 716 wpa_supplicant_mark_disassoc(wpa_s);
da1fb17c
JM
717}
718
719
720void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
721 union wpa_event_data *data)
722{
f049052b 723 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
0fb337c1 724 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
da1fb17c 725 wpa_supplicant_mark_disassoc(wpa_s);
da1fb17c 726}
a84ed99e
JM
727
728
729void sme_event_disassoc(struct wpa_supplicant *wpa_s,
730 union wpa_event_data *data)
731{
f049052b 732 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
17fbb751 733 if (wpa_s->sme.prev_bssid_set) {
a84ed99e
JM
734 /*
735 * cfg80211/mac80211 can get into somewhat confused state if
736 * the AP only disassociates us and leaves us in authenticated
737 * state. For now, force the state to be cleared to avoid
738 * confusing errors if we try to associate with the AP again.
739 */
f049052b
BG
740 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
741 "driver state");
7e26053a 742 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
a84ed99e
JM
743 WLAN_REASON_DEAUTH_LEAVING);
744 }
745}
7d878ca7
JM
746
747
e29853bb
BG
748static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
749{
750 struct wpa_supplicant *wpa_s = eloop_ctx;
751 if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
752 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
753 sme_deauth(wpa_s);
754 }
755}
756
757
758static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
759{
760 struct wpa_supplicant *wpa_s = eloop_ctx;
761 if (wpa_s->wpa_state == WPA_ASSOCIATING) {
762 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
763 sme_deauth(wpa_s);
764 }
765}
766
767
768void sme_state_changed(struct wpa_supplicant *wpa_s)
769{
770 /* Make sure timers are cleaned up appropriately. */
771 if (wpa_s->wpa_state != WPA_ASSOCIATING)
772 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
773 if (wpa_s->wpa_state != WPA_AUTHENTICATING)
774 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
775}
776
777
778void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
779 const u8 *prev_pending_bssid)
780{
781 /*
782 * mac80211-workaround to force deauth on failed auth cmd,
783 * requires us to remain in authenticating state to allow the
784 * second authentication attempt to be continued properly.
785 */
786 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
787 "to proceed after disconnection event");
788 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
789 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
790
791 /*
792 * Re-arm authentication timer in case auth fails for whatever reason.
793 */
794 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
795 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
796 NULL);
797}
798
799
800void sme_deinit(struct wpa_supplicant *wpa_s)
801{
802 os_free(wpa_s->sme.ft_ies);
803 wpa_s->sme.ft_ies = NULL;
804 wpa_s->sme.ft_ies_len = 0;
805#ifdef CONFIG_IEEE80211W
806 sme_stop_sa_query(wpa_s);
807#endif /* CONFIG_IEEE80211W */
808
809 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
810 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
c3701c66
RM
811 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
812}
813
814
815static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
816 const u8 *chan_list, u8 num_channels,
817 u8 num_intol)
818{
819 struct ieee80211_2040_bss_coex_ie *bc_ie;
820 struct ieee80211_2040_intol_chan_report *ic_report;
821 struct wpabuf *buf;
822
823 wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR,
824 MAC2STR(wpa_s->bssid));
825
826 buf = wpabuf_alloc(2 + /* action.category + action_code */
827 sizeof(struct ieee80211_2040_bss_coex_ie) +
828 sizeof(struct ieee80211_2040_intol_chan_report) +
829 num_channels);
830 if (buf == NULL)
831 return;
832
833 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
834 wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
835
836 bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
837 bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
838 bc_ie->length = 1;
839 if (num_intol)
840 bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
841
842 if (num_channels > 0) {
843 ic_report = wpabuf_put(buf, sizeof(*ic_report));
844 ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
845 ic_report->length = num_channels + 1;
846 ic_report->op_class = 0;
847 os_memcpy(wpabuf_put(buf, num_channels), chan_list,
848 num_channels);
849 }
850
851 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
852 wpa_s->own_addr, wpa_s->bssid,
853 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
854 wpa_msg(wpa_s, MSG_INFO,
855 "SME: Failed to send 20/40 BSS Coexistence frame");
856 }
857
858 wpabuf_free(buf);
859}
860
861
862/**
863 * enum wpas_band - Frequency band
864 * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
865 * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
866 */
867enum wpas_band {
868 WPAS_BAND_2GHZ,
869 WPAS_BAND_5GHZ,
870 WPAS_BAND_INVALID
871};
872
873/**
874 * freq_to_channel - Convert frequency into channel info
875 * @channel: Buffer for returning channel number
876 * Returns: Band (2 or 5 GHz)
877 */
878static enum wpas_band freq_to_channel(int freq, u8 *channel)
879{
880 enum wpas_band band = (freq <= 2484) ? WPAS_BAND_2GHZ : WPAS_BAND_5GHZ;
881 u8 chan = 0;
882
883 if (freq >= 2412 && freq <= 2472)
884 chan = (freq - 2407) / 5;
885 else if (freq == 2484)
886 chan = 14;
887 else if (freq >= 5180 && freq <= 5805)
888 chan = (freq - 5000) / 5;
889
890 *channel = chan;
891 return band;
892}
893
894
895int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
896{
897 struct wpa_bss *bss;
898 const u8 *ie;
899 u16 ht_cap;
900 u8 chan_list[P2P_MAX_CHANNELS], channel;
901 u8 num_channels = 0, num_intol = 0, i;
902
903 if (!wpa_s->sme.sched_obss_scan)
904 return 0;
905
906 wpa_s->sme.sched_obss_scan = 0;
907 if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
908 return 1;
909
910 /*
911 * Check whether AP uses regulatory triplet or channel triplet in
912 * country info. Right now the operating class of the BSS channel
913 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
914 * based on the assumption that operating class triplet is not used in
915 * beacon frame. If the First Channel Number/Operating Extension
916 * Identifier octet has a positive integer value of 201 or greater,
917 * then its operating class triplet.
918 *
919 * TODO: If Supported Operating Classes element is present in beacon
920 * frame, have to lookup operating class in Annex E and fill them in
921 * 2040 coex frame.
922 */
923 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
924 if (ie && (ie[1] >= 6) && (ie[5] >= 201))
925 return 1;
926
927 os_memset(chan_list, 0, sizeof(chan_list));
928
929 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
930 /* Skip other band bss */
931 if (freq_to_channel(bss->freq, &channel) != WPAS_BAND_2GHZ)
932 continue;
933
934 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
935 ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
936
937 if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
938 /* Check whether the channel is already considered */
939 for (i = 0; i < num_channels; i++) {
940 if (channel == chan_list[i])
941 break;
942 }
943 if (i != num_channels)
944 continue;
945
946 if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
947 num_intol++;
948
949 chan_list[num_channels++] = channel;
950 }
951 }
952
953 sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
954 return 1;
955}
956
957
6434ad09
JM
958static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
959 u16 num_modes,
960 enum hostapd_hw_mode mode)
961{
962 u16 i;
963
964 for (i = 0; i < num_modes; i++) {
965 if (modes[i].mode == mode)
966 return &modes[i];
967 }
968
969 return NULL;
970}
971
972
973static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
974 enum hostapd_hw_mode band,
975 struct wpa_driver_scan_params *params)
976{
977 /* Include only supported channels for the specified band */
978 struct hostapd_hw_modes *mode;
979 int count, i;
980
981 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
982 if (mode == NULL) {
983 /* No channels supported in this band - use empty list */
984 params->freqs = os_zalloc(sizeof(int));
985 return;
986 }
987
f9884c09 988 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
6434ad09
JM
989 if (params->freqs == NULL)
990 return;
991 for (count = 0, i = 0; i < mode->num_channels; i++) {
992 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
993 continue;
994 params->freqs[count++] = mode->channels[i].freq;
995 }
996}
997
998
c3701c66
RM
999static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1000{
1001 struct wpa_supplicant *wpa_s = eloop_ctx;
1002 struct wpa_driver_scan_params params;
1003
1004 if (!wpa_s->current_bss) {
1005 wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
1006 return;
1007 }
1008
1009 os_memset(&params, 0, sizeof(params));
6434ad09 1010 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params);
c3701c66
RM
1011 wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
1012
1013 if (wpa_supplicant_trigger_scan(wpa_s, &params))
1014 wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
1015 else
1016 wpa_s->sme.sched_obss_scan = 1;
6434ad09 1017 os_free(params.freqs);
c3701c66
RM
1018
1019 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1020 sme_obss_scan_timeout, wpa_s, NULL);
1021}
1022
1023
1024void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
1025{
1026 const u8 *ie;
1027 struct wpa_bss *bss = wpa_s->current_bss;
1028 struct wpa_ssid *ssid = wpa_s->current_ssid;
b6871ebb
AN
1029 struct hostapd_hw_modes *hw_mode = NULL;
1030 int i;
c3701c66
RM
1031
1032 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1033 wpa_s->sme.sched_obss_scan = 0;
1034 if (!enable)
1035 return;
1036
368b1957
AK
1037 /*
1038 * Schedule OBSS scan if driver is using station SME in wpa_supplicant
1039 * or it expects OBSS scan to be performed by wpa_supplicant.
1040 */
1041 if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
1042 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
1043 ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
1044 return;
c3701c66 1045
b6871ebb
AN
1046 if (!wpa_s->hw.modes)
1047 return;
1048
1049 /* only HT caps in 11g mode are relevant */
1050 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1051 hw_mode = &wpa_s->hw.modes[i];
1052 if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
1053 break;
1054 }
1055
1056 /* Driver does not support HT40 for 11g or doesn't have 11g. */
1057 if (i == wpa_s->hw.num_modes || !hw_mode ||
1058 !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1059 return;
c3701c66
RM
1060
1061 if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
1062 return; /* Not associated on 2.4 GHz band */
1063
1064 /* Check whether AP supports HT40 */
1065 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
1066 if (!ie || ie[1] < 2 ||
1067 !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1068 return; /* AP does not support HT40 */
1069
1070 ie = wpa_bss_get_ie(wpa_s->current_bss,
1071 WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
1072 if (!ie || ie[1] < 14)
1073 return; /* AP does not request OBSS scans */
1074
1075 wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
1076 if (wpa_s->sme.obss_scan_int < 10) {
1077 wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
1078 "replaced with the minimum 10 sec",
1079 wpa_s->sme.obss_scan_int);
1080 wpa_s->sme.obss_scan_int = 10;
1081 }
1082 wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
1083 wpa_s->sme.obss_scan_int);
1084 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1085 sme_obss_scan_timeout, wpa_s, NULL);
e29853bb
BG
1086}
1087
1088
7d878ca7
JM
1089#ifdef CONFIG_IEEE80211W
1090
1091static const unsigned int sa_query_max_timeout = 1000;
1092static const unsigned int sa_query_retry_timeout = 201;
1093
1094static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
1095{
1096 u32 tu;
1097 struct os_time now, passed;
1098 os_get_time(&now);
1099 os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
1100 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1101 if (sa_query_max_timeout < tu) {
f049052b 1102 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
7d878ca7
JM
1103 sme_stop_sa_query(wpa_s);
1104 wpa_supplicant_deauthenticate(
1105 wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
1106 return 1;
1107 }
1108
1109 return 0;
1110}
1111
1112
1113static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
1114 const u8 *trans_id)
1115{
1116 u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
f049052b
BG
1117 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
1118 MACSTR, MAC2STR(wpa_s->bssid));
7d878ca7
JM
1119 wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
1120 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1121 req[0] = WLAN_ACTION_SA_QUERY;
1122 req[1] = WLAN_SA_QUERY_REQUEST;
1123 os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
190b9062 1124 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
7d878ca7 1125 wpa_s->own_addr, wpa_s->bssid,
b106173a 1126 req, sizeof(req), 0) < 0)
f049052b
BG
1127 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
1128 "Request");
7d878ca7
JM
1129}
1130
1131
1132static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1133{
1134 struct wpa_supplicant *wpa_s = eloop_ctx;
1135 unsigned int timeout, sec, usec;
1136 u8 *trans_id, *nbuf;
1137
1138 if (wpa_s->sme.sa_query_count > 0 &&
1139 sme_check_sa_query_timeout(wpa_s))
1140 return;
1141
067ffa26
JM
1142 nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
1143 wpa_s->sme.sa_query_count + 1,
1144 WLAN_SA_QUERY_TR_ID_LEN);
7d878ca7
JM
1145 if (nbuf == NULL)
1146 return;
1147 if (wpa_s->sme.sa_query_count == 0) {
1148 /* Starting a new SA Query procedure */
1149 os_get_time(&wpa_s->sme.sa_query_start);
1150 }
1151 trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1152 wpa_s->sme.sa_query_trans_id = nbuf;
1153 wpa_s->sme.sa_query_count++;
1154
1155 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1156
1157 timeout = sa_query_retry_timeout;
1158 sec = ((timeout / 1000) * 1024) / 1000;
1159 usec = (timeout % 1000) * 1024;
1160 eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
1161
f049052b
BG
1162 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
1163 wpa_s->sme.sa_query_count);
7d878ca7
JM
1164
1165 sme_send_sa_query_req(wpa_s, trans_id);
1166}
1167
1168
1169static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
1170{
1171 sme_sa_query_timer(wpa_s, NULL);
1172}
1173
1174
19df9b07 1175static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
7d878ca7
JM
1176{
1177 eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
1178 os_free(wpa_s->sme.sa_query_trans_id);
1179 wpa_s->sme.sa_query_trans_id = NULL;
1180 wpa_s->sme.sa_query_count = 0;
1181}
1182
1183
1184void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
1185 const u8 *da, u16 reason_code)
1186{
1187 struct wpa_ssid *ssid;
1188
1189 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1190 return;
1191 if (wpa_s->wpa_state != WPA_COMPLETED)
1192 return;
1193 ssid = wpa_s->current_ssid;
62d49803
JM
1194 if (ssid == NULL ||
1195 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1196 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION)
7d878ca7
JM
1197 return;
1198 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1199 return;
1200 if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
1201 reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
1202 return;
1203 if (wpa_s->sme.sa_query_count > 0)
1204 return;
1205
f049052b
BG
1206 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
1207 "possible AP/STA state mismatch - trigger SA Query");
7d878ca7
JM
1208 sme_start_sa_query(wpa_s);
1209}
1210
1211
1212void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
1213 const u8 *data, size_t len)
1214{
1215 int i;
1216
1217 if (wpa_s->sme.sa_query_trans_id == NULL ||
1218 len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
1219 data[0] != WLAN_SA_QUERY_RESPONSE)
1220 return;
f049052b
BG
1221 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
1222 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
7d878ca7
JM
1223
1224 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1225 return;
1226
1227 for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
1228 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
1229 i * WLAN_SA_QUERY_TR_ID_LEN,
1230 data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
1231 break;
1232 }
1233
1234 if (i >= wpa_s->sme.sa_query_count) {
f049052b
BG
1235 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
1236 "transaction identifier found");
7d878ca7
JM
1237 return;
1238 }
1239
f049052b
BG
1240 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
1241 "from " MACSTR, MAC2STR(sa));
7d878ca7
JM
1242 sme_stop_sa_query(wpa_s);
1243}
1244
1245#endif /* CONFIG_IEEE80211W */