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