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