blob: d34668a5accfd16d5879ffff9212d231d892e914 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - SME
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2009-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "common/ieee802_11_common.h"
15#include "eapol_supp/eapol_supp_sm.h"
16#include "common/wpa_common.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080017#include "common/sae.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "rsn_supp/wpa.h"
19#include "rsn_supp/pmksa_cache.h"
20#include "config.h"
21#include "wpa_supplicant_i.h"
22#include "driver_i.h"
23#include "wpas_glue.h"
24#include "wps_supplicant.h"
25#include "p2p_supplicant.h"
26#include "notify.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "bss.h"
28#include "scan.h"
29#include "sme.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070030#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031
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);
Dmitry Shmidt04949592012-07-19 12:16:46 -070037static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#ifdef CONFIG_IEEE80211W
39static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
40#endif /* CONFIG_IEEE80211W */
41
42
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080043#ifdef CONFIG_SAE
44
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080045static int index_within_array(const int *array, int idx)
46{
47 int i;
48 for (i = 0; i < idx; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080049 if (array[i] <= 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080050 return 0;
51 }
52 return 1;
53}
54
55
56static int sme_set_sae_group(struct wpa_supplicant *wpa_s)
57{
58 int *groups = wpa_s->conf->sae_groups;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080059 int default_groups[] = { 19, 20, 21, 25, 26, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080060
Dmitry Shmidtcce06662013-11-04 18:44:24 -080061 if (!groups || groups[0] <= 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080062 groups = default_groups;
63
64 /* Configuration may have changed, so validate current index */
65 if (!index_within_array(groups, wpa_s->sme.sae_group_index))
66 return -1;
67
68 for (;;) {
69 int group = groups[wpa_s->sme.sae_group_index];
70 if (group < 0)
71 break;
72 if (sae_set_group(&wpa_s->sme.sae, group) == 0) {
73 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
74 wpa_s->sme.sae.group);
75 return 0;
76 }
77 wpa_s->sme.sae_group_index++;
78 }
79
80 return -1;
81}
82
83
84static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s,
85 struct wpa_ssid *ssid,
86 const u8 *bssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080087{
88 struct wpabuf *buf;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080089 size_t len;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080090
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080091 if (ssid->passphrase == NULL) {
92 wpa_printf(MSG_DEBUG, "SAE: No password available");
93 return NULL;
94 }
95
96 if (sme_set_sae_group(wpa_s) < 0) {
97 wpa_printf(MSG_DEBUG, "SAE: Failed to select group");
98 return NULL;
99 }
100
101 if (sae_prepare_commit(wpa_s->own_addr, bssid,
102 (u8 *) ssid->passphrase,
103 os_strlen(ssid->passphrase),
104 &wpa_s->sme.sae) < 0) {
105 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
106 return NULL;
107 }
108
109 len = wpa_s->sme.sae_token ? wpabuf_len(wpa_s->sme.sae_token) : 0;
110 buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800111 if (buf == NULL)
112 return NULL;
113
114 wpabuf_put_le16(buf, 1); /* Transaction seq# */
115 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800116 sae_write_commit(&wpa_s->sme.sae, buf, wpa_s->sme.sae_token);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800117
118 return buf;
119}
120
121
122static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s)
123{
124 struct wpabuf *buf;
125
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800126 buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127 if (buf == NULL)
128 return NULL;
129
130 wpabuf_put_le16(buf, 2); /* Transaction seq# */
131 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800132 sae_write_confirm(&wpa_s->sme.sae, buf);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800133
134 return buf;
135}
136
137#endif /* CONFIG_SAE */
138
139
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800140/**
141 * sme_auth_handle_rrm - Handle RRM aspects of current authentication attempt
142 * @wpa_s: Pointer to wpa_supplicant data
143 * @bss: Pointer to the bss which is the target of authentication attempt
144 */
145static void sme_auth_handle_rrm(struct wpa_supplicant *wpa_s,
146 struct wpa_bss *bss)
147{
148 const u8 rrm_ie_len = 5;
149 u8 *pos;
150 const u8 *rrm_ie;
151
152 wpa_s->rrm.rrm_used = 0;
153
154 wpa_printf(MSG_DEBUG,
155 "RRM: Determining whether RRM can be used - device support: 0x%x",
156 wpa_s->drv_rrm_flags);
157
158 rrm_ie = wpa_bss_get_ie(bss, WLAN_EID_RRM_ENABLED_CAPABILITIES);
159 if (!rrm_ie || !(bss->caps & IEEE80211_CAP_RRM)) {
160 wpa_printf(MSG_DEBUG, "RRM: No RRM in network");
161 return;
162 }
163
164 if (!(wpa_s->drv_rrm_flags &
165 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
166 !(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) {
167 wpa_printf(MSG_DEBUG,
168 "RRM: Insufficient RRM support in driver - do not use RRM");
169 return;
170 }
171
172 if (sizeof(wpa_s->sme.assoc_req_ie) <
173 wpa_s->sme.assoc_req_ie_len + rrm_ie_len + 2) {
174 wpa_printf(MSG_INFO,
175 "RRM: Unable to use RRM, no room for RRM IE");
176 return;
177 }
178
179 wpa_printf(MSG_DEBUG, "RRM: Adding RRM IE to Association Request");
180 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
181 os_memset(pos, 0, 2 + rrm_ie_len);
182 *pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
183 *pos++ = rrm_ie_len;
184
185 /* Set supported capabilites flags */
186 if (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)
187 *pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT;
188
189 wpa_s->sme.assoc_req_ie_len += rrm_ie_len + 2;
190 wpa_s->rrm.rrm_used = 1;
191}
192
193
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800194static void sme_send_authentication(struct wpa_supplicant *wpa_s,
195 struct wpa_bss *bss, struct wpa_ssid *ssid,
196 int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197{
198 struct wpa_driver_auth_params params;
199 struct wpa_ssid *old_ssid;
200#ifdef CONFIG_IEEE80211R
201 const u8 *ie;
202#endif /* CONFIG_IEEE80211R */
203#ifdef CONFIG_IEEE80211R
204 const u8 *md = NULL;
205#endif /* CONFIG_IEEE80211R */
206 int i, bssid_changed;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800207 struct wpabuf *resp = NULL;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -0700208 u8 ext_capab[18];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800209 int ext_capab_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210
211 if (bss == NULL) {
212 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
213 "the network");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800214 wpas_connect_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 return;
216 }
217
218 wpa_s->current_bss = bss;
219
220 os_memset(&params, 0, sizeof(params));
221 wpa_s->reassociate = 0;
222
223 params.freq = bss->freq;
224 params.bssid = bss->bssid;
225 params.ssid = bss->ssid;
226 params.ssid_len = bss->ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227 params.p2p = ssid->p2p_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228
229 if (wpa_s->sme.ssid_len != params.ssid_len ||
230 os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
231 wpa_s->sme.prev_bssid_set = 0;
232
233 wpa_s->sme.freq = params.freq;
234 os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
235 wpa_s->sme.ssid_len = params.ssid_len;
236
237 params.auth_alg = WPA_AUTH_ALG_OPEN;
238#ifdef IEEE8021X_EAPOL
239 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
240 if (ssid->leap) {
241 if (ssid->non_leap == 0)
242 params.auth_alg = WPA_AUTH_ALG_LEAP;
243 else
244 params.auth_alg |= WPA_AUTH_ALG_LEAP;
245 }
246 }
247#endif /* IEEE8021X_EAPOL */
248 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
249 params.auth_alg);
250 if (ssid->auth_alg) {
251 params.auth_alg = ssid->auth_alg;
252 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
253 "0x%x", params.auth_alg);
254 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800255#ifdef CONFIG_SAE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800256 wpa_s->sme.sae_pmksa_caching = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800257 if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
258 const u8 *rsn;
259 struct wpa_ie_data ied;
260
261 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800262 if (!rsn) {
263 wpa_dbg(wpa_s, MSG_DEBUG,
264 "SAE enabled, but target BSS does not advertise RSN");
265 } else if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
266 wpa_key_mgmt_sae(ied.key_mgmt)) {
267 wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
268 params.auth_alg = WPA_AUTH_ALG_SAE;
269 } else {
270 wpa_dbg(wpa_s, MSG_DEBUG,
271 "SAE enabled, but target BSS does not advertise SAE AKM for RSN");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800272 }
273 }
274#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275
276 for (i = 0; i < NUM_WEP_KEYS; i++) {
277 if (ssid->wep_key_len[i])
278 params.wep_key[i] = ssid->wep_key[i];
279 params.wep_key_len[i] = ssid->wep_key_len[i];
280 }
281 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
282
283 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
284 os_memset(wpa_s->bssid, 0, ETH_ALEN);
285 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
286 if (bssid_changed)
287 wpas_notify_bssid_changed(wpa_s);
288
289 if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
290 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 int try_opportunistic;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800293 try_opportunistic = (ssid->proactive_key_caching < 0 ?
294 wpa_s->conf->okc :
295 ssid->proactive_key_caching) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 (ssid->proto & WPA_PROTO_RSN);
297 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
298 wpa_s->current_ssid,
299 try_opportunistic) == 0)
300 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
301 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
302 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
303 wpa_s->sme.assoc_req_ie,
304 &wpa_s->sme.assoc_req_ie_len)) {
305 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
306 "key management and encryption suites");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 wpas_connect_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 return;
309 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700310 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
311 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
312 /*
313 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
314 * use non-WPA since the scan results did not indicate that the
315 * AP is using WPA or WPA2.
316 */
317 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
318 wpa_s->sme.assoc_req_ie_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800319 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
321 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
322 wpa_s->sme.assoc_req_ie,
323 &wpa_s->sme.assoc_req_ie_len)) {
324 wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
325 "key management and encryption suites (no "
326 "scan results)");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 wpas_connect_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 return;
329 }
330#ifdef CONFIG_WPS
331 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
332 struct wpabuf *wps_ie;
333 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
334 if (wps_ie && wpabuf_len(wps_ie) <=
335 sizeof(wpa_s->sme.assoc_req_ie)) {
336 wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
337 os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
338 wpa_s->sme.assoc_req_ie_len);
339 } else
340 wpa_s->sme.assoc_req_ie_len = 0;
341 wpabuf_free(wps_ie);
342 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
343#endif /* CONFIG_WPS */
344 } else {
345 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
346 wpa_s->sme.assoc_req_ie_len = 0;
347 }
348
349#ifdef CONFIG_IEEE80211R
350 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
351 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
352 md = ie + 2;
353 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
354 if (md) {
355 /* Prepare for the next transition */
356 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
357 }
358
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800359 if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 if (wpa_s->sme.assoc_req_ie_len + 5 <
361 sizeof(wpa_s->sme.assoc_req_ie)) {
362 struct rsn_mdie *mdie;
363 u8 *pos = wpa_s->sme.assoc_req_ie +
364 wpa_s->sme.assoc_req_ie_len;
365 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
366 *pos++ = sizeof(*mdie);
367 mdie = (struct rsn_mdie *) pos;
368 os_memcpy(mdie->mobility_domain, md,
369 MOBILITY_DOMAIN_ID_LEN);
370 mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
371 wpa_s->sme.assoc_req_ie_len += 5;
372 }
373
374 if (wpa_s->sme.ft_used &&
375 os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
376 wpa_sm_has_ptk(wpa_s->wpa)) {
377 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
378 "over-the-air");
379 params.auth_alg = WPA_AUTH_ALG_FT;
380 params.ie = wpa_s->sme.ft_ies;
381 params.ie_len = wpa_s->sme.ft_ies_len;
382 }
383 }
384#endif /* CONFIG_IEEE80211R */
385
386#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800387 wpa_s->sme.mfp = ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
388 wpa_s->conf->pmf : ssid->ieee80211w;
389 if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
391 struct wpa_ie_data _ie;
392 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
393 _ie.capabilities &
394 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
395 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
396 "MFP: require MFP");
397 wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
398 }
399 }
400#endif /* CONFIG_IEEE80211W */
401
402#ifdef CONFIG_P2P
403 if (wpa_s->global->p2p) {
404 u8 *pos;
405 size_t len;
406 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
408 len = sizeof(wpa_s->sme.assoc_req_ie) -
409 wpa_s->sme.assoc_req_ie_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800410 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
411 ssid->p2p_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 if (res >= 0)
413 wpa_s->sme.assoc_req_ie_len += res;
414 }
415#endif /* CONFIG_P2P */
416
Dmitry Shmidt04949592012-07-19 12:16:46 -0700417#ifdef CONFIG_HS20
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700418 if (is_hs20_network(wpa_s, ssid, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700419 struct wpabuf *hs20;
420 hs20 = wpabuf_alloc(20);
421 if (hs20) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800422 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700423 size_t len;
424
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800425 wpas_hs20_add_indication(hs20, pps_mo_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700426 len = sizeof(wpa_s->sme.assoc_req_ie) -
427 wpa_s->sme.assoc_req_ie_len;
428 if (wpabuf_len(hs20) <= len) {
429 os_memcpy(wpa_s->sme.assoc_req_ie +
430 wpa_s->sme.assoc_req_ie_len,
431 wpabuf_head(hs20), wpabuf_len(hs20));
432 wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
433 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434 wpabuf_free(hs20);
435 }
436 }
437#endif /* CONFIG_HS20 */
438
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -0700439 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
440 sizeof(ext_capab));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800441 if (ext_capab_len > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800442 u8 *pos = wpa_s->sme.assoc_req_ie;
443 if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
444 pos += 2 + pos[1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800445 os_memmove(pos + ext_capab_len, pos,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800446 wpa_s->sme.assoc_req_ie_len -
447 (pos - wpa_s->sme.assoc_req_ie));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800448 wpa_s->sme.assoc_req_ie_len += ext_capab_len;
449 os_memcpy(pos, ext_capab, ext_capab_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800450 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800451
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800452 if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
453 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
454 size_t len;
455
456 len = sizeof(wpa_s->sme.assoc_req_ie) -
457 wpa_s->sme.assoc_req_ie_len;
458 if (wpabuf_len(buf) <= len) {
459 os_memcpy(wpa_s->sme.assoc_req_ie +
460 wpa_s->sme.assoc_req_ie_len,
461 wpabuf_head(buf), wpabuf_len(buf));
462 wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
463 }
464 }
465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800466 sme_auth_handle_rrm(wpa_s, bss);
467
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800468#ifdef CONFIG_SAE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800469 if (params.auth_alg == WPA_AUTH_ALG_SAE &&
470 pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid, ssid, 0) == 0)
471 {
472 wpa_dbg(wpa_s, MSG_DEBUG,
473 "PMKSA cache entry found - try to use PMKSA caching instead of new SAE authentication");
474 params.auth_alg = WPA_AUTH_ALG_OPEN;
475 wpa_s->sme.sae_pmksa_caching = 1;
476 }
477
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800478 if (params.auth_alg == WPA_AUTH_ALG_SAE) {
479 if (start)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800480 resp = sme_auth_build_sae_commit(wpa_s, ssid,
481 bss->bssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800482 else
483 resp = sme_auth_build_sae_confirm(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800484 if (resp == NULL) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800485 wpas_connection_failed(wpa_s, bss->bssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800486 return;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800487 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800488 params.sae_data = wpabuf_head(resp);
489 params.sae_data_len = wpabuf_len(resp);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800490 wpa_s->sme.sae.state = start ? SAE_COMMITTED : SAE_CONFIRMED;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800491 }
492#endif /* CONFIG_SAE */
493
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800494 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 wpa_supplicant_cancel_scan(wpa_s);
496
497 wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
498 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
499 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
500
501 wpa_clear_keys(wpa_s, bss->bssid);
502 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
503 old_ssid = wpa_s->current_ssid;
504 wpa_s->current_ssid = ssid;
505 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
506 wpa_supplicant_initiate_eapol(wpa_s);
507 if (old_ssid != wpa_s->current_ssid)
508 wpas_notify_network_changed(wpa_s);
509
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700510#ifdef CONFIG_P2P
511 /*
512 * If multi-channel concurrency is not supported, check for any
513 * frequency conflict. In case of any frequency conflict, remove the
514 * least prioritized connection.
515 */
516 if (wpa_s->num_multichan_concurrent < 2) {
517 int freq, num;
518 num = get_shared_radio_freqs(wpa_s, &freq, 1);
519 if (num > 0 && freq > 0 && freq != params.freq) {
520 wpa_printf(MSG_DEBUG,
521 "Conflicting frequency found (%d != %d)",
522 freq, params.freq);
523 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
524 params.freq,
525 ssid) < 0) {
526 wpas_connection_failed(wpa_s, bss->bssid);
527 wpa_supplicant_mark_disassoc(wpa_s);
528 wpabuf_free(resp);
529 wpas_connect_work_done(wpa_s);
530 return;
531 }
532 }
533 }
534#endif /* CONFIG_P2P */
535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 wpa_s->sme.auth_alg = params.auth_alg;
537 if (wpa_drv_authenticate(wpa_s, &params) < 0) {
538 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
539 "driver failed");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800540 wpas_connection_failed(wpa_s, bss->bssid);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800541 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800542 wpabuf_free(resp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543 wpas_connect_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 return;
545 }
546
547 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
548 NULL);
549
550 /*
551 * Association will be started based on the authentication event from
552 * the driver.
553 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800554
555 wpabuf_free(resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556}
557
558
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800559static void sme_auth_start_cb(struct wpa_radio_work *work, int deinit)
560{
561 struct wpa_connect_work *cwork = work->ctx;
562 struct wpa_supplicant *wpa_s = work->wpa_s;
563
564 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800565 if (work->started)
566 wpa_s->connect_work = NULL;
567
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800568 wpas_connect_work_free(cwork);
569 return;
570 }
571
572 wpa_s->connect_work = work;
573
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800574 if (cwork->bss_removed ||
575 !wpas_valid_bss_ssid(wpa_s, cwork->bss, cwork->ssid)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800576 wpa_dbg(wpa_s, MSG_DEBUG, "SME: BSS/SSID entry for authentication not valid anymore - drop connection attempt");
577 wpas_connect_work_done(wpa_s);
578 return;
579 }
580
581 sme_send_authentication(wpa_s, cwork->bss, cwork->ssid, 1);
582}
583
584
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800585void sme_authenticate(struct wpa_supplicant *wpa_s,
586 struct wpa_bss *bss, struct wpa_ssid *ssid)
587{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800588 struct wpa_connect_work *cwork;
589
590 if (bss == NULL || ssid == NULL)
591 return;
592 if (wpa_s->connect_work) {
593 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reject sme_authenticate() call since connect_work exist");
594 return;
595 }
596
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800597 if (radio_work_pending(wpa_s, "sme-connect")) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700598 /*
599 * The previous sme-connect work might no longer be valid due to
600 * the fact that the BSS list was updated. In addition, it makes
601 * sense to adhere to the 'newer' decision.
602 */
603 wpa_dbg(wpa_s, MSG_DEBUG,
604 "SME: Remove previous pending sme-connect");
605 radio_remove_works(wpa_s, "sme-connect", 0);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800606 }
607
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800608 cwork = os_zalloc(sizeof(*cwork));
609 if (cwork == NULL)
610 return;
611 cwork->bss = bss;
612 cwork->ssid = ssid;
613 cwork->sme = 1;
614
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800615#ifdef CONFIG_SAE
616 wpa_s->sme.sae.state = SAE_NOTHING;
617 wpa_s->sme.sae.send_confirm = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800618 wpa_s->sme.sae_group_index = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800619#endif /* CONFIG_SAE */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800620
621 if (radio_add_work(wpa_s, bss->freq, "sme-connect", 1,
622 sme_auth_start_cb, cwork) < 0)
623 wpas_connect_work_free(cwork);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800624}
625
626
627#ifdef CONFIG_SAE
628
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800629static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
630 u16 status_code, const u8 *data, size_t len)
631{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800632 int *groups;
633
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800634 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
635 "status code %u", auth_transaction, status_code);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800636
637 if (auth_transaction == 1 &&
638 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
639 wpa_s->sme.sae.state == SAE_COMMITTED &&
640 wpa_s->current_bss && wpa_s->current_ssid) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800641 int default_groups[] = { 19, 20, 21, 25, 26, 0 };
642 u16 group;
643
644 groups = wpa_s->conf->sae_groups;
645 if (!groups || groups[0] <= 0)
646 groups = default_groups;
647
648 if (len < sizeof(le16)) {
649 wpa_dbg(wpa_s, MSG_DEBUG,
650 "SME: Too short SAE anti-clogging token request");
651 return -1;
652 }
653 group = WPA_GET_LE16(data);
654 wpa_dbg(wpa_s, MSG_DEBUG,
655 "SME: SAE anti-clogging token requested (group %u)",
656 group);
657 if (sae_group_allowed(&wpa_s->sme.sae, groups, group) !=
658 WLAN_STATUS_SUCCESS) {
659 wpa_dbg(wpa_s, MSG_ERROR,
660 "SME: SAE group %u of anti-clogging request is invalid",
661 group);
662 return -1;
663 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800664 wpabuf_free(wpa_s->sme.sae_token);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800665 wpa_s->sme.sae_token = wpabuf_alloc_copy(data + sizeof(le16),
666 len - sizeof(le16));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800667 sme_send_authentication(wpa_s, wpa_s->current_bss,
668 wpa_s->current_ssid, 1);
669 return 0;
670 }
671
672 if (auth_transaction == 1 &&
673 status_code == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
674 wpa_s->sme.sae.state == SAE_COMMITTED &&
675 wpa_s->current_bss && wpa_s->current_ssid) {
676 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE group not supported");
677 wpa_s->sme.sae_group_index++;
678 if (sme_set_sae_group(wpa_s) < 0)
679 return -1; /* no other groups enabled */
680 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Try next enabled SAE group");
681 sme_send_authentication(wpa_s, wpa_s->current_bss,
682 wpa_s->current_ssid, 1);
683 return 0;
684 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800685
686 if (status_code != WLAN_STATUS_SUCCESS)
687 return -1;
688
689 if (auth_transaction == 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800690 groups = wpa_s->conf->sae_groups;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800691
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800692 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
693 if (wpa_s->current_bss == NULL ||
694 wpa_s->current_ssid == NULL)
695 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800696 if (wpa_s->sme.sae.state != SAE_COMMITTED)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800697 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800698 if (groups && groups[0] <= 0)
699 groups = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800700 if (sae_parse_commit(&wpa_s->sme.sae, data, len, NULL, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800701 groups) != WLAN_STATUS_SUCCESS)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800702 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800703
704 if (sae_process_commit(&wpa_s->sme.sae) < 0) {
705 wpa_printf(MSG_DEBUG, "SAE: Failed to process peer "
706 "commit");
707 return -1;
708 }
709
710 wpabuf_free(wpa_s->sme.sae_token);
711 wpa_s->sme.sae_token = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800712 sme_send_authentication(wpa_s, wpa_s->current_bss,
713 wpa_s->current_ssid, 0);
714 return 0;
715 } else if (auth_transaction == 2) {
716 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800717 if (wpa_s->sme.sae.state != SAE_CONFIRMED)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800718 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800719 if (sae_check_confirm(&wpa_s->sme.sae, data, len) < 0)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800720 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800721 wpa_s->sme.sae.state = SAE_ACCEPTED;
722 sae_clear_temp_data(&wpa_s->sme.sae);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800723 return 1;
724 }
725
726 return -1;
727}
728#endif /* CONFIG_SAE */
729
730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
732{
733 struct wpa_ssid *ssid = wpa_s->current_ssid;
734
735 if (ssid == NULL) {
736 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
737 "when network is not selected");
738 return;
739 }
740
741 if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
742 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
743 "when not in authenticating state");
744 return;
745 }
746
747 if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
748 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
749 "unexpected peer " MACSTR,
750 MAC2STR(data->auth.peer));
751 return;
752 }
753
754 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800755 " auth_type=%d auth_transaction=%d status_code=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 MAC2STR(data->auth.peer), data->auth.auth_type,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800757 data->auth.auth_transaction, data->auth.status_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
759 data->auth.ies, data->auth.ies_len);
760
761 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
762
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800763#ifdef CONFIG_SAE
764 if (data->auth.auth_type == WLAN_AUTH_SAE) {
765 int res;
766 res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
767 data->auth.status_code, data->auth.ies,
768 data->auth.ies_len);
769 if (res < 0) {
770 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
771 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
772
773 }
774 if (res != 1)
775 return;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800776
777 wpa_printf(MSG_DEBUG, "SME: SAE completed - setting PMK for "
778 "4-way handshake");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800779 wpa_sm_set_pmk(wpa_s->wpa, wpa_s->sme.sae.pmk, PMK_LEN,
780 wpa_s->pending_bssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800781 }
782#endif /* CONFIG_SAE */
783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
785 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication failed (status "
786 "code %d)", data->auth.status_code);
787
788 if (data->auth.status_code !=
789 WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
790 wpa_s->sme.auth_alg == data->auth.auth_type ||
791 wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
792 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794 return;
795 }
796
Dmitry Shmidt97672262014-02-03 13:02:54 -0800797 wpas_connect_work_done(wpa_s);
798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 switch (data->auth.auth_type) {
800 case WLAN_AUTH_OPEN:
801 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
802
803 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
804 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
805 wpa_s->current_ssid);
806 return;
807
808 case WLAN_AUTH_SHARED_KEY:
809 wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
810
811 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
812 wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
813 wpa_s->current_ssid);
814 return;
815
816 default:
817 return;
818 }
819 }
820
821#ifdef CONFIG_IEEE80211R
822 if (data->auth.auth_type == WLAN_AUTH_FT) {
823 union wpa_event_data edata;
824 os_memset(&edata, 0, sizeof(edata));
825 edata.ft_ies.ies = data->auth.ies;
826 edata.ft_ies.ies_len = data->auth.ies_len;
827 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
828 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
829 }
830#endif /* CONFIG_IEEE80211R */
831
832 sme_associate(wpa_s, ssid->mode, data->auth.peer,
833 data->auth.auth_type);
834}
835
836
837void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
838 const u8 *bssid, u16 auth_type)
839{
840 struct wpa_driver_associate_params params;
841 struct ieee802_11_elems elems;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800842#ifdef CONFIG_HT_OVERRIDES
843 struct ieee80211_ht_capabilities htcaps;
844 struct ieee80211_ht_capabilities htcaps_mask;
845#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700846#ifdef CONFIG_VHT_OVERRIDES
847 struct ieee80211_vht_capabilities vhtcaps;
848 struct ieee80211_vht_capabilities vhtcaps_mask;
849#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850
851 os_memset(&params, 0, sizeof(params));
852 params.bssid = bssid;
853 params.ssid = wpa_s->sme.ssid;
854 params.ssid_len = wpa_s->sme.ssid_len;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700855 params.freq.freq = wpa_s->sme.freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 params.bg_scan_period = wpa_s->current_ssid ?
857 wpa_s->current_ssid->bg_scan_period : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
859 wpa_s->sme.assoc_req_ie : NULL;
860 params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800861 params.pairwise_suite = wpa_s->pairwise_cipher;
862 params.group_suite = wpa_s->group_cipher;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700863 params.key_mgmt_suite = wpa_s->key_mgmt;
864 params.wpa_proto = wpa_s->wpa_proto;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800865#ifdef CONFIG_HT_OVERRIDES
866 os_memset(&htcaps, 0, sizeof(htcaps));
867 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
868 params.htcaps = (u8 *) &htcaps;
869 params.htcaps_mask = (u8 *) &htcaps_mask;
870 wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
871#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700872#ifdef CONFIG_VHT_OVERRIDES
873 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
874 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
875 params.vhtcaps = &vhtcaps;
876 params.vhtcaps_mask = &vhtcaps_mask;
877 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
878#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879#ifdef CONFIG_IEEE80211R
880 if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
881 params.wpa_ie = wpa_s->sme.ft_ies;
882 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
883 }
884#endif /* CONFIG_IEEE80211R */
885 params.mode = mode;
886 params.mgmt_frame_protection = wpa_s->sme.mfp;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800887 params.rrm_used = wpa_s->rrm.rrm_used;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 if (wpa_s->sme.prev_bssid_set)
889 params.prev_bssid = wpa_s->sme.prev_bssid;
890
891 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
892 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
893 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700894 params.freq.freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895
896 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
897
898 if (params.wpa_ie == NULL ||
899 ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
900 < 0) {
901 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
902 os_memset(&elems, 0, sizeof(elems));
903 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904 if (elems.rsn_ie) {
905 params.wpa_proto = WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
907 elems.rsn_ie_len + 2);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800908 } else if (elems.wpa_ie) {
909 params.wpa_proto = WPA_PROTO_WPA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
911 elems.wpa_ie_len + 2);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800912 } else if (elems.osen) {
913 params.wpa_proto = WPA_PROTO_OSEN;
914 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.osen - 2,
915 elems.osen_len + 2);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800916 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800918 if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 params.p2p = 1;
920
921 if (wpa_s->parent->set_sta_uapsd)
922 params.uapsd = wpa_s->parent->sta_uapsd;
923 else
924 params.uapsd = -1;
925
926 if (wpa_drv_associate(wpa_s, &params) < 0) {
927 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
928 "driver failed");
929 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700930 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
932 return;
933 }
934
935 eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
936 NULL);
937}
938
939
940int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
941 const u8 *ies, size_t ies_len)
942{
943 if (md == NULL || ies == NULL) {
944 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
945 os_free(wpa_s->sme.ft_ies);
946 wpa_s->sme.ft_ies = NULL;
947 wpa_s->sme.ft_ies_len = 0;
948 wpa_s->sme.ft_used = 0;
949 return 0;
950 }
951
952 os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
953 wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
954 os_free(wpa_s->sme.ft_ies);
955 wpa_s->sme.ft_ies = os_malloc(ies_len);
956 if (wpa_s->sme.ft_ies == NULL)
957 return -1;
958 os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
959 wpa_s->sme.ft_ies_len = ies_len;
960 return 0;
961}
962
963
964static void sme_deauth(struct wpa_supplicant *wpa_s)
965{
966 int bssid_changed;
967
968 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
969
970 if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
971 WLAN_REASON_DEAUTH_LEAVING) < 0) {
972 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
973 "failed");
974 }
975 wpa_s->sme.prev_bssid_set = 0;
976
977 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
978 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
979 os_memset(wpa_s->bssid, 0, ETH_ALEN);
980 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
981 if (bssid_changed)
982 wpas_notify_bssid_changed(wpa_s);
983}
984
985
986void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
987 union wpa_event_data *data)
988{
989 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
990 "status code %d", MAC2STR(wpa_s->pending_bssid),
991 data->assoc_reject.status_code);
992
993 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800995#ifdef CONFIG_SAE
996 if (wpa_s->sme.sae_pmksa_caching && wpa_s->current_ssid &&
997 wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt)) {
998 wpa_dbg(wpa_s, MSG_DEBUG,
999 "PMKSA caching attempt rejected - drop PMKSA cache entry and fall back to SAE authentication");
1000 wpa_sm_aborted_cached(wpa_s->wpa);
1001 wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
1002 if (wpa_s->current_bss) {
1003 struct wpa_bss *bss = wpa_s->current_bss;
1004 struct wpa_ssid *ssid = wpa_s->current_ssid;
1005
1006 wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
1007 WLAN_REASON_DEAUTH_LEAVING);
1008 wpas_connect_work_done(wpa_s);
1009 wpa_supplicant_mark_disassoc(wpa_s);
1010 wpa_supplicant_connect(wpa_s, bss, ssid);
1011 return;
1012 }
1013 }
1014#endif /* CONFIG_SAE */
1015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 /*
1017 * For now, unconditionally terminate the previous authentication. In
1018 * theory, this should not be needed, but mac80211 gets quite confused
1019 * if the authentication is left pending.. Some roaming cases might
1020 * benefit from using the previous authentication, so this could be
1021 * optimized in the future.
1022 */
1023 sme_deauth(wpa_s);
1024}
1025
1026
1027void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
1028 union wpa_event_data *data)
1029{
1030 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
1031 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001032 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033}
1034
1035
1036void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
1037 union wpa_event_data *data)
1038{
1039 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
1040 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1041 wpa_supplicant_mark_disassoc(wpa_s);
1042}
1043
1044
1045void sme_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001046 struct disassoc_info *info)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047{
1048 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 if (wpa_s->sme.prev_bssid_set) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 /*
1051 * cfg80211/mac80211 can get into somewhat confused state if
1052 * the AP only disassociates us and leaves us in authenticated
1053 * state. For now, force the state to be cleared to avoid
1054 * confusing errors if we try to associate with the AP again.
1055 */
1056 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
1057 "driver state");
1058 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
1059 WLAN_REASON_DEAUTH_LEAVING);
1060 }
1061}
1062
1063
1064static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
1065{
1066 struct wpa_supplicant *wpa_s = eloop_ctx;
1067 if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
1068 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
1069 sme_deauth(wpa_s);
1070 }
1071}
1072
1073
1074static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
1075{
1076 struct wpa_supplicant *wpa_s = eloop_ctx;
1077 if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1078 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
1079 sme_deauth(wpa_s);
1080 }
1081}
1082
1083
1084void sme_state_changed(struct wpa_supplicant *wpa_s)
1085{
1086 /* Make sure timers are cleaned up appropriately. */
1087 if (wpa_s->wpa_state != WPA_ASSOCIATING)
1088 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1089 if (wpa_s->wpa_state != WPA_AUTHENTICATING)
1090 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1091}
1092
1093
1094void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
1095 const u8 *prev_pending_bssid)
1096{
1097 /*
1098 * mac80211-workaround to force deauth on failed auth cmd,
1099 * requires us to remain in authenticating state to allow the
1100 * second authentication attempt to be continued properly.
1101 */
1102 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
1103 "to proceed after disconnection event");
1104 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1105 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1106
1107 /*
1108 * Re-arm authentication timer in case auth fails for whatever reason.
1109 */
1110 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1111 eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
1112 NULL);
1113}
1114
1115
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001116void sme_clear_on_disassoc(struct wpa_supplicant *wpa_s)
1117{
1118 wpa_s->sme.prev_bssid_set = 0;
1119#ifdef CONFIG_SAE
1120 wpabuf_free(wpa_s->sme.sae_token);
1121 wpa_s->sme.sae_token = NULL;
1122 sae_clear_data(&wpa_s->sme.sae);
1123#endif /* CONFIG_SAE */
1124#ifdef CONFIG_IEEE80211R
1125 if (wpa_s->sme.ft_ies)
1126 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
1127#endif /* CONFIG_IEEE80211R */
1128}
1129
1130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131void sme_deinit(struct wpa_supplicant *wpa_s)
1132{
1133 os_free(wpa_s->sme.ft_ies);
1134 wpa_s->sme.ft_ies = NULL;
1135 wpa_s->sme.ft_ies_len = 0;
1136#ifdef CONFIG_IEEE80211W
1137 sme_stop_sa_query(wpa_s);
1138#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001139 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140
1141 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1142 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001143 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1144}
1145
1146
1147static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
1148 const u8 *chan_list, u8 num_channels,
1149 u8 num_intol)
1150{
1151 struct ieee80211_2040_bss_coex_ie *bc_ie;
1152 struct ieee80211_2040_intol_chan_report *ic_report;
1153 struct wpabuf *buf;
1154
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001155 wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR
1156 " (num_channels=%u num_intol=%u)",
1157 MAC2STR(wpa_s->bssid), num_channels, num_intol);
1158 wpa_hexdump(MSG_DEBUG, "SME: 20/40 BSS Intolerant Channels",
1159 chan_list, num_channels);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001160
1161 buf = wpabuf_alloc(2 + /* action.category + action_code */
1162 sizeof(struct ieee80211_2040_bss_coex_ie) +
1163 sizeof(struct ieee80211_2040_intol_chan_report) +
1164 num_channels);
1165 if (buf == NULL)
1166 return;
1167
1168 wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
1169 wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
1170
1171 bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
1172 bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
1173 bc_ie->length = 1;
1174 if (num_intol)
1175 bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
1176
1177 if (num_channels > 0) {
1178 ic_report = wpabuf_put(buf, sizeof(*ic_report));
1179 ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
1180 ic_report->length = num_channels + 1;
1181 ic_report->op_class = 0;
1182 os_memcpy(wpabuf_put(buf, num_channels), chan_list,
1183 num_channels);
1184 }
1185
1186 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1187 wpa_s->own_addr, wpa_s->bssid,
1188 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
1189 wpa_msg(wpa_s, MSG_INFO,
1190 "SME: Failed to send 20/40 BSS Coexistence frame");
1191 }
1192
1193 wpabuf_free(buf);
1194}
1195
1196
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
1198{
1199 struct wpa_bss *bss;
1200 const u8 *ie;
1201 u16 ht_cap;
1202 u8 chan_list[P2P_MAX_CHANNELS], channel;
1203 u8 num_channels = 0, num_intol = 0, i;
1204
1205 if (!wpa_s->sme.sched_obss_scan)
1206 return 0;
1207
1208 wpa_s->sme.sched_obss_scan = 0;
1209 if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
1210 return 1;
1211
1212 /*
1213 * Check whether AP uses regulatory triplet or channel triplet in
1214 * country info. Right now the operating class of the BSS channel
1215 * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
1216 * based on the assumption that operating class triplet is not used in
1217 * beacon frame. If the First Channel Number/Operating Extension
1218 * Identifier octet has a positive integer value of 201 or greater,
1219 * then its operating class triplet.
1220 *
1221 * TODO: If Supported Operating Classes element is present in beacon
1222 * frame, have to lookup operating class in Annex E and fill them in
1223 * 2040 coex frame.
1224 */
1225 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
1226 if (ie && (ie[1] >= 6) && (ie[5] >= 201))
1227 return 1;
1228
1229 os_memset(chan_list, 0, sizeof(chan_list));
1230
1231 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1232 /* Skip other band bss */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001233 enum hostapd_hw_mode mode;
1234 mode = ieee80211_freq_to_chan(bss->freq, &channel);
1235 if (mode != HOSTAPD_MODE_IEEE80211G &&
1236 mode != HOSTAPD_MODE_IEEE80211B)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001237 continue;
1238
1239 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
1240 ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001241 wpa_printf(MSG_DEBUG, "SME OBSS scan BSS " MACSTR
1242 " freq=%u chan=%u ht_cap=0x%x",
1243 MAC2STR(bss->bssid), bss->freq, channel, ht_cap);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001244
1245 if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001246 if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
1247 num_intol++;
1248
Dmitry Shmidt04949592012-07-19 12:16:46 -07001249 /* Check whether the channel is already considered */
1250 for (i = 0; i < num_channels; i++) {
1251 if (channel == chan_list[i])
1252 break;
1253 }
1254 if (i != num_channels)
1255 continue;
1256
Dmitry Shmidt04949592012-07-19 12:16:46 -07001257 chan_list[num_channels++] = channel;
1258 }
1259 }
1260
1261 sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
1262 return 1;
1263}
1264
1265
1266static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
1267 u16 num_modes,
1268 enum hostapd_hw_mode mode)
1269{
1270 u16 i;
1271
1272 for (i = 0; i < num_modes; i++) {
1273 if (modes[i].mode == mode)
1274 return &modes[i];
1275 }
1276
1277 return NULL;
1278}
1279
1280
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001281static void wpa_obss_scan_freqs_list(struct wpa_supplicant *wpa_s,
1282 struct wpa_driver_scan_params *params)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001283{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001284 /* Include only affected channels */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001285 struct hostapd_hw_modes *mode;
1286 int count, i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001287 int start, end;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001288
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001289 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
1290 HOSTAPD_MODE_IEEE80211G);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001291 if (mode == NULL) {
1292 /* No channels supported in this band - use empty list */
1293 params->freqs = os_zalloc(sizeof(int));
1294 return;
1295 }
1296
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001297 if (wpa_s->sme.ht_sec_chan == HT_SEC_CHAN_UNKNOWN &&
1298 wpa_s->current_bss) {
1299 const u8 *ie;
1300
1301 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_OPERATION);
1302 if (ie && ie[1] >= 2) {
1303 u8 o;
1304
1305 o = ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
1306 if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
1307 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
1308 else if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
1309 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
1310 }
1311 }
1312
1313 start = wpa_s->assoc_freq - 10;
1314 end = wpa_s->assoc_freq + 10;
1315 switch (wpa_s->sme.ht_sec_chan) {
1316 case HT_SEC_CHAN_UNKNOWN:
1317 /* HT40+ possible on channels 1..9 */
1318 if (wpa_s->assoc_freq <= 2452)
1319 start -= 20;
1320 /* HT40- possible on channels 5-13 */
1321 if (wpa_s->assoc_freq >= 2432)
1322 end += 20;
1323 break;
1324 case HT_SEC_CHAN_ABOVE:
1325 end += 20;
1326 break;
1327 case HT_SEC_CHAN_BELOW:
1328 start -= 20;
1329 break;
1330 }
1331 wpa_printf(MSG_DEBUG,
1332 "OBSS: assoc_freq %d possible affected range %d-%d",
1333 wpa_s->assoc_freq, start, end);
1334
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001335 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001336 if (params->freqs == NULL)
1337 return;
1338 for (count = 0, i = 0; i < mode->num_channels; i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001339 int freq;
1340
Dmitry Shmidt04949592012-07-19 12:16:46 -07001341 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
1342 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001343 freq = mode->channels[i].freq;
1344 if (freq - 10 >= end || freq + 10 <= start)
1345 continue; /* not affected */
1346 params->freqs[count++] = freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001347 }
1348}
1349
1350
1351static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1352{
1353 struct wpa_supplicant *wpa_s = eloop_ctx;
1354 struct wpa_driver_scan_params params;
1355
1356 if (!wpa_s->current_bss) {
1357 wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
1358 return;
1359 }
1360
1361 os_memset(&params, 0, sizeof(params));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001362 wpa_obss_scan_freqs_list(wpa_s, &params);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001363 params.low_priority = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001364 wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
1365
1366 if (wpa_supplicant_trigger_scan(wpa_s, &params))
1367 wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
1368 else
1369 wpa_s->sme.sched_obss_scan = 1;
1370 os_free(params.freqs);
1371
1372 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1373 sme_obss_scan_timeout, wpa_s, NULL);
1374}
1375
1376
1377void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
1378{
1379 const u8 *ie;
1380 struct wpa_bss *bss = wpa_s->current_bss;
1381 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382 struct hostapd_hw_modes *hw_mode = NULL;
1383 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001384
1385 eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1386 wpa_s->sme.sched_obss_scan = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001387 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001388 if (!enable)
1389 return;
1390
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001391 /*
1392 * Schedule OBSS scan if driver is using station SME in wpa_supplicant
1393 * or it expects OBSS scan to be performed by wpa_supplicant.
1394 */
1395 if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
1396 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
1397 ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
1398 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001399
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001400 if (!wpa_s->hw.modes)
1401 return;
1402
1403 /* only HT caps in 11g mode are relevant */
1404 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1405 hw_mode = &wpa_s->hw.modes[i];
1406 if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
1407 break;
1408 }
1409
1410 /* Driver does not support HT40 for 11g or doesn't have 11g. */
1411 if (i == wpa_s->hw.num_modes || !hw_mode ||
1412 !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1413 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001414
1415 if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
1416 return; /* Not associated on 2.4 GHz band */
1417
1418 /* Check whether AP supports HT40 */
1419 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
1420 if (!ie || ie[1] < 2 ||
1421 !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1422 return; /* AP does not support HT40 */
1423
1424 ie = wpa_bss_get_ie(wpa_s->current_bss,
1425 WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
1426 if (!ie || ie[1] < 14)
1427 return; /* AP does not request OBSS scans */
1428
1429 wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
1430 if (wpa_s->sme.obss_scan_int < 10) {
1431 wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
1432 "replaced with the minimum 10 sec",
1433 wpa_s->sme.obss_scan_int);
1434 wpa_s->sme.obss_scan_int = 10;
1435 }
1436 wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
1437 wpa_s->sme.obss_scan_int);
1438 eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1439 sme_obss_scan_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440}
1441
1442
1443#ifdef CONFIG_IEEE80211W
1444
1445static const unsigned int sa_query_max_timeout = 1000;
1446static const unsigned int sa_query_retry_timeout = 201;
1447
1448static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
1449{
1450 u32 tu;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001451 struct os_reltime now, passed;
1452 os_get_reltime(&now);
1453 os_reltime_sub(&now, &wpa_s->sme.sa_query_start, &passed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1455 if (sa_query_max_timeout < tu) {
1456 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
1457 sme_stop_sa_query(wpa_s);
1458 wpa_supplicant_deauthenticate(
1459 wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
1460 return 1;
1461 }
1462
1463 return 0;
1464}
1465
1466
1467static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
1468 const u8 *trans_id)
1469{
1470 u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
1471 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
1472 MACSTR, MAC2STR(wpa_s->bssid));
1473 wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
1474 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1475 req[0] = WLAN_ACTION_SA_QUERY;
1476 req[1] = WLAN_SA_QUERY_REQUEST;
1477 os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1478 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1479 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001480 req, sizeof(req), 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
1482 "Request");
1483}
1484
1485
1486static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1487{
1488 struct wpa_supplicant *wpa_s = eloop_ctx;
1489 unsigned int timeout, sec, usec;
1490 u8 *trans_id, *nbuf;
1491
1492 if (wpa_s->sme.sa_query_count > 0 &&
1493 sme_check_sa_query_timeout(wpa_s))
1494 return;
1495
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001496 nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
1497 wpa_s->sme.sa_query_count + 1,
1498 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499 if (nbuf == NULL)
1500 return;
1501 if (wpa_s->sme.sa_query_count == 0) {
1502 /* Starting a new SA Query procedure */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001503 os_get_reltime(&wpa_s->sme.sa_query_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 }
1505 trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1506 wpa_s->sme.sa_query_trans_id = nbuf;
1507 wpa_s->sme.sa_query_count++;
1508
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001509 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1510 wpa_printf(MSG_DEBUG, "Could not generate SA Query ID");
1511 return;
1512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513
1514 timeout = sa_query_retry_timeout;
1515 sec = ((timeout / 1000) * 1024) / 1000;
1516 usec = (timeout % 1000) * 1024;
1517 eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
1518
1519 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
1520 wpa_s->sme.sa_query_count);
1521
1522 sme_send_sa_query_req(wpa_s, trans_id);
1523}
1524
1525
1526static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
1527{
1528 sme_sa_query_timer(wpa_s, NULL);
1529}
1530
1531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001532static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533{
1534 eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
1535 os_free(wpa_s->sme.sa_query_trans_id);
1536 wpa_s->sme.sa_query_trans_id = NULL;
1537 wpa_s->sme.sa_query_count = 0;
1538}
1539
1540
1541void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
1542 const u8 *da, u16 reason_code)
1543{
1544 struct wpa_ssid *ssid;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001545 struct os_reltime now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 if (wpa_s->wpa_state != WPA_COMPLETED)
1548 return;
1549 ssid = wpa_s->current_ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001550 if (ssid == NULL ||
1551 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1552 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 return;
1554 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1555 return;
1556 if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
1557 reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
1558 return;
1559 if (wpa_s->sme.sa_query_count > 0)
1560 return;
1561
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001562 os_get_reltime(&now);
1563 if (wpa_s->sme.last_unprot_disconnect.sec &&
1564 !os_reltime_expired(&now, &wpa_s->sme.last_unprot_disconnect, 10))
1565 return; /* limit SA Query procedure frequency */
1566 wpa_s->sme.last_unprot_disconnect = now;
1567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
1569 "possible AP/STA state mismatch - trigger SA Query");
1570 sme_start_sa_query(wpa_s);
1571}
1572
1573
1574void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
1575 const u8 *data, size_t len)
1576{
1577 int i;
1578
1579 if (wpa_s->sme.sa_query_trans_id == NULL ||
1580 len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
1581 data[0] != WLAN_SA_QUERY_RESPONSE)
1582 return;
1583 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
1584 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
1585
1586 if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1587 return;
1588
1589 for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
1590 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
1591 i * WLAN_SA_QUERY_TR_ID_LEN,
1592 data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
1593 break;
1594 }
1595
1596 if (i >= wpa_s->sme.sa_query_count) {
1597 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
1598 "transaction identifier found");
1599 return;
1600 }
1601
1602 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
1603 "from " MACSTR, MAC2STR(sa));
1604 sme_stop_sa_query(wpa_s);
1605}
1606
1607#endif /* CONFIG_IEEE80211W */