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