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