wpa_supplicant: Handle LCI request
[mech_eap.git] / wpa_supplicant / sme.c
1 /*
2  * wpa_supplicant - SME
3  * Copyright (c) 2009-2014, 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 "common/sae.h"
18 #include "rsn_supp/wpa.h"
19 #include "rsn_supp/pmksa_cache.h"
20 #include "config.h"
21 #include "wpa_supplicant_i.h"
22 #include "driver_i.h"
23 #include "wpas_glue.h"
24 #include "wps_supplicant.h"
25 #include "p2p_supplicant.h"
26 #include "notify.h"
27 #include "bss.h"
28 #include "scan.h"
29 #include "sme.h"
30 #include "hs20_supplicant.h"
31
32 #define SME_AUTH_TIMEOUT 5
33 #define SME_ASSOC_TIMEOUT 5
34
35 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx);
36 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx);
37 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx);
38 #ifdef CONFIG_IEEE80211W
39 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s);
40 #endif /* CONFIG_IEEE80211W */
41
42
43 #ifdef CONFIG_SAE
44
45 static int index_within_array(const int *array, int idx)
46 {
47         int i;
48         for (i = 0; i < idx; i++) {
49                 if (array[i] <= 0)
50                         return 0;
51         }
52         return 1;
53 }
54
55
56 static int sme_set_sae_group(struct wpa_supplicant *wpa_s)
57 {
58         int *groups = wpa_s->conf->sae_groups;
59         int default_groups[] = { 19, 20, 21, 25, 26, 0 };
60
61         if (!groups || groups[0] <= 0)
62                 groups = default_groups;
63
64         /* Configuration may have changed, so validate current index */
65         if (!index_within_array(groups, wpa_s->sme.sae_group_index))
66                 return -1;
67
68         for (;;) {
69                 int group = groups[wpa_s->sme.sae_group_index];
70                 if (group <= 0)
71                         break;
72                 if (sae_set_group(&wpa_s->sme.sae, group) == 0) {
73                         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
74                                 wpa_s->sme.sae.group);
75                        return 0;
76                 }
77                 wpa_s->sme.sae_group_index++;
78         }
79
80         return -1;
81 }
82
83
84 static struct wpabuf * sme_auth_build_sae_commit(struct wpa_supplicant *wpa_s,
85                                                  struct wpa_ssid *ssid,
86                                                  const u8 *bssid)
87 {
88         struct wpabuf *buf;
89         size_t len;
90
91         if (ssid->passphrase == NULL) {
92                 wpa_printf(MSG_DEBUG, "SAE: No password available");
93                 return NULL;
94         }
95
96         if (sme_set_sae_group(wpa_s) < 0) {
97                 wpa_printf(MSG_DEBUG, "SAE: Failed to select group");
98                 return NULL;
99         }
100
101         if (sae_prepare_commit(wpa_s->own_addr, bssid,
102                                (u8 *) ssid->passphrase,
103                                os_strlen(ssid->passphrase),
104                                &wpa_s->sme.sae) < 0) {
105                 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
106                 return NULL;
107         }
108
109         len = wpa_s->sme.sae_token ? wpabuf_len(wpa_s->sme.sae_token) : 0;
110         buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
111         if (buf == NULL)
112                 return NULL;
113
114         wpabuf_put_le16(buf, 1); /* Transaction seq# */
115         wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
116         sae_write_commit(&wpa_s->sme.sae, buf, wpa_s->sme.sae_token);
117
118         return buf;
119 }
120
121
122 static struct wpabuf * sme_auth_build_sae_confirm(struct wpa_supplicant *wpa_s)
123 {
124         struct wpabuf *buf;
125
126         buf = wpabuf_alloc(4 + SAE_CONFIRM_MAX_LEN);
127         if (buf == NULL)
128                 return NULL;
129
130         wpabuf_put_le16(buf, 2); /* Transaction seq# */
131         wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
132         sae_write_confirm(&wpa_s->sme.sae, buf);
133
134         return buf;
135 }
136
137 #endif /* CONFIG_SAE */
138
139
140 /**
141  * sme_auth_handle_rrm - Handle RRM aspects of current authentication attempt
142  * @wpa_s: Pointer to wpa_supplicant data
143  * @bss: Pointer to the bss which is the target of authentication attempt
144  */
145 static void sme_auth_handle_rrm(struct wpa_supplicant *wpa_s,
146                                 struct wpa_bss *bss)
147 {
148         const u8 rrm_ie_len = 5;
149         u8 *pos;
150         const u8 *rrm_ie;
151
152         wpa_s->rrm.rrm_used = 0;
153
154         wpa_printf(MSG_DEBUG,
155                    "RRM: Determining whether RRM can be used - device support: 0x%x",
156                    wpa_s->drv_rrm_flags);
157
158         rrm_ie = wpa_bss_get_ie(bss, WLAN_EID_RRM_ENABLED_CAPABILITIES);
159         if (!rrm_ie || !(bss->caps & IEEE80211_CAP_RRM)) {
160                 wpa_printf(MSG_DEBUG, "RRM: No RRM in network");
161                 return;
162         }
163
164         if (!(wpa_s->drv_rrm_flags &
165               WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
166             !(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) {
167                 wpa_printf(MSG_DEBUG,
168                            "RRM: Insufficient RRM support in driver - do not use RRM");
169                 return;
170         }
171
172         if (sizeof(wpa_s->sme.assoc_req_ie) <
173             wpa_s->sme.assoc_req_ie_len + rrm_ie_len + 2) {
174                 wpa_printf(MSG_INFO,
175                            "RRM: Unable to use RRM, no room for RRM IE");
176                 return;
177         }
178
179         wpa_printf(MSG_DEBUG, "RRM: Adding RRM IE to Association Request");
180         pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
181         os_memset(pos, 0, 2 + rrm_ie_len);
182         *pos++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
183         *pos++ = rrm_ie_len;
184
185         /* Set supported capabilites flags */
186         if (wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)
187                 *pos |= WLAN_RRM_CAPS_LINK_MEASUREMENT;
188
189         if (wpa_s->lci)
190                 pos[1] |= WLAN_RRM_CAPS_LCI_MEASUREMENT;
191
192         wpa_s->sme.assoc_req_ie_len += rrm_ie_len + 2;
193         wpa_s->rrm.rrm_used = 1;
194 }
195
196
197 static void sme_send_authentication(struct wpa_supplicant *wpa_s,
198                                     struct wpa_bss *bss, struct wpa_ssid *ssid,
199                                     int start)
200 {
201         struct wpa_driver_auth_params params;
202         struct wpa_ssid *old_ssid;
203 #ifdef CONFIG_IEEE80211R
204         const u8 *ie;
205 #endif /* CONFIG_IEEE80211R */
206 #ifdef CONFIG_IEEE80211R
207         const u8 *md = NULL;
208 #endif /* CONFIG_IEEE80211R */
209         int i, bssid_changed;
210         struct wpabuf *resp = NULL;
211         u8 ext_capab[18];
212         int ext_capab_len;
213         int skip_auth;
214 #ifdef CONFIG_MBO
215         const u8 *mbo;
216 #endif /* CONFIG_MBO */
217
218         if (bss == NULL) {
219                 wpa_msg(wpa_s, MSG_ERROR, "SME: No scan result available for "
220                         "the network");
221                 wpas_connect_work_done(wpa_s);
222                 return;
223         }
224
225         skip_auth = wpa_s->conf->reassoc_same_bss_optim &&
226                 wpa_s->reassoc_same_bss;
227         wpa_s->current_bss = bss;
228
229         os_memset(&params, 0, sizeof(params));
230         wpa_s->reassociate = 0;
231
232         params.freq = bss->freq;
233         params.bssid = bss->bssid;
234         params.ssid = bss->ssid;
235         params.ssid_len = bss->ssid_len;
236         params.p2p = ssid->p2p_group;
237
238         if (wpa_s->sme.ssid_len != params.ssid_len ||
239             os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
240                 wpa_s->sme.prev_bssid_set = 0;
241
242         wpa_s->sme.freq = params.freq;
243         os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
244         wpa_s->sme.ssid_len = params.ssid_len;
245
246         params.auth_alg = WPA_AUTH_ALG_OPEN;
247 #ifdef IEEE8021X_EAPOL
248         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
249                 if (ssid->leap) {
250                         if (ssid->non_leap == 0)
251                                 params.auth_alg = WPA_AUTH_ALG_LEAP;
252                         else
253                                 params.auth_alg |= WPA_AUTH_ALG_LEAP;
254                 }
255         }
256 #endif /* IEEE8021X_EAPOL */
257         wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
258                 params.auth_alg);
259         if (ssid->auth_alg) {
260                 params.auth_alg = ssid->auth_alg;
261                 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
262                         "0x%x", params.auth_alg);
263         }
264 #ifdef CONFIG_SAE
265         wpa_s->sme.sae_pmksa_caching = 0;
266         if (wpa_key_mgmt_sae(ssid->key_mgmt)) {
267                 const u8 *rsn;
268                 struct wpa_ie_data ied;
269
270                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
271                 if (!rsn) {
272                         wpa_dbg(wpa_s, MSG_DEBUG,
273                                 "SAE enabled, but target BSS does not advertise RSN");
274                 } else if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
275                            wpa_key_mgmt_sae(ied.key_mgmt)) {
276                         wpa_dbg(wpa_s, MSG_DEBUG, "Using SAE auth_alg");
277                         params.auth_alg = WPA_AUTH_ALG_SAE;
278                 } else {
279                         wpa_dbg(wpa_s, MSG_DEBUG,
280                                 "SAE enabled, but target BSS does not advertise SAE AKM for RSN");
281                 }
282         }
283 #endif /* CONFIG_SAE */
284
285         for (i = 0; i < NUM_WEP_KEYS; i++) {
286                 if (ssid->wep_key_len[i])
287                         params.wep_key[i] = ssid->wep_key[i];
288                 params.wep_key_len[i] = ssid->wep_key_len[i];
289         }
290         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
291
292         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
293         os_memset(wpa_s->bssid, 0, ETH_ALEN);
294         os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
295         if (bssid_changed)
296                 wpas_notify_bssid_changed(wpa_s);
297
298         if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
299              wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
300             wpa_key_mgmt_wpa(ssid->key_mgmt)) {
301                 int try_opportunistic;
302                 try_opportunistic = (ssid->proactive_key_caching < 0 ?
303                                      wpa_s->conf->okc :
304                                      ssid->proactive_key_caching) &&
305                         (ssid->proto & WPA_PROTO_RSN);
306                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
307                                             wpa_s->current_ssid,
308                                             try_opportunistic) == 0)
309                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
310                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
311                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
312                                               wpa_s->sme.assoc_req_ie,
313                                               &wpa_s->sme.assoc_req_ie_len)) {
314                         wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
315                                 "key management and encryption suites");
316                         wpas_connect_work_done(wpa_s);
317                         return;
318                 }
319         } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
320                    wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
321                 /*
322                  * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
323                  * use non-WPA since the scan results did not indicate that the
324                  * AP is using WPA or WPA2.
325                  */
326                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
327                 wpa_s->sme.assoc_req_ie_len = 0;
328         } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
329                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
330                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
331                                               wpa_s->sme.assoc_req_ie,
332                                               &wpa_s->sme.assoc_req_ie_len)) {
333                         wpa_msg(wpa_s, MSG_WARNING, "SME: Failed to set WPA "
334                                 "key management and encryption suites (no "
335                                 "scan results)");
336                         wpas_connect_work_done(wpa_s);
337                         return;
338                 }
339 #ifdef CONFIG_WPS
340         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
341                 struct wpabuf *wps_ie;
342                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
343                 if (wps_ie && wpabuf_len(wps_ie) <=
344                     sizeof(wpa_s->sme.assoc_req_ie)) {
345                         wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
346                         os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
347                                   wpa_s->sme.assoc_req_ie_len);
348                 } else
349                         wpa_s->sme.assoc_req_ie_len = 0;
350                 wpabuf_free(wps_ie);
351                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
352 #endif /* CONFIG_WPS */
353         } else {
354                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
355                 wpa_s->sme.assoc_req_ie_len = 0;
356         }
357
358 #ifdef CONFIG_IEEE80211R
359         ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
360         if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
361                 md = ie + 2;
362         wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
363         if (md) {
364                 /* Prepare for the next transition */
365                 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
366         }
367
368         if (md && wpa_key_mgmt_ft(ssid->key_mgmt)) {
369                 if (wpa_s->sme.assoc_req_ie_len + 5 <
370                     sizeof(wpa_s->sme.assoc_req_ie)) {
371                         struct rsn_mdie *mdie;
372                         u8 *pos = wpa_s->sme.assoc_req_ie +
373                                 wpa_s->sme.assoc_req_ie_len;
374                         *pos++ = WLAN_EID_MOBILITY_DOMAIN;
375                         *pos++ = sizeof(*mdie);
376                         mdie = (struct rsn_mdie *) pos;
377                         os_memcpy(mdie->mobility_domain, md,
378                                   MOBILITY_DOMAIN_ID_LEN);
379                         mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
380                         wpa_s->sme.assoc_req_ie_len += 5;
381                 }
382
383                 if (wpa_s->sme.ft_used &&
384                     os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
385                     wpa_sm_has_ptk(wpa_s->wpa)) {
386                         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying to use FT "
387                                 "over-the-air");
388                         params.auth_alg = WPA_AUTH_ALG_FT;
389                         params.ie = wpa_s->sme.ft_ies;
390                         params.ie_len = wpa_s->sme.ft_ies_len;
391                 }
392         }
393 #endif /* CONFIG_IEEE80211R */
394
395 #ifdef CONFIG_IEEE80211W
396         wpa_s->sme.mfp = wpas_get_ssid_pmf(wpa_s, ssid);
397         if (wpa_s->sme.mfp != NO_MGMT_FRAME_PROTECTION) {
398                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
399                 struct wpa_ie_data _ie;
400                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
401                     _ie.capabilities &
402                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
403                         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected AP supports "
404                                 "MFP: require MFP");
405                         wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
406                 }
407         }
408 #endif /* CONFIG_IEEE80211W */
409
410 #ifdef CONFIG_P2P
411         if (wpa_s->global->p2p) {
412                 u8 *pos;
413                 size_t len;
414                 int res;
415                 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
416                 len = sizeof(wpa_s->sme.assoc_req_ie) -
417                         wpa_s->sme.assoc_req_ie_len;
418                 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
419                                             ssid->p2p_group);
420                 if (res >= 0)
421                         wpa_s->sme.assoc_req_ie_len += res;
422         }
423 #endif /* CONFIG_P2P */
424
425 #ifdef CONFIG_FST
426         if (wpa_s->fst_ies) {
427                 int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
428
429                 if (wpa_s->sme.assoc_req_ie_len + fst_ies_len <=
430                     sizeof(wpa_s->sme.assoc_req_ie)) {
431                         os_memcpy(wpa_s->sme.assoc_req_ie +
432                                   wpa_s->sme.assoc_req_ie_len,
433                                   wpabuf_head(wpa_s->fst_ies),
434                                   fst_ies_len);
435                         wpa_s->sme.assoc_req_ie_len += fst_ies_len;
436                 }
437         }
438 #endif /* CONFIG_FST */
439
440         sme_auth_handle_rrm(wpa_s, bss);
441
442 #ifdef CONFIG_MBO
443         mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
444         if (mbo) {
445                 int len;
446
447                 len = wpas_mbo_supp_op_class_ie(
448                         wpa_s, bss->freq,
449                         wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len,
450                         sizeof(wpa_s->sme.assoc_req_ie) -
451                         wpa_s->sme.assoc_req_ie_len);
452                 if (len > 0)
453                         wpa_s->sme.assoc_req_ie_len += len;
454         }
455 #endif /* CONFIG_MBO */
456
457         ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
458                                              sizeof(ext_capab));
459         if (ext_capab_len > 0) {
460                 u8 *pos = wpa_s->sme.assoc_req_ie;
461                 if (wpa_s->sme.assoc_req_ie_len > 0 && pos[0] == WLAN_EID_RSN)
462                         pos += 2 + pos[1];
463                 os_memmove(pos + ext_capab_len, pos,
464                            wpa_s->sme.assoc_req_ie_len -
465                            (pos - wpa_s->sme.assoc_req_ie));
466                 wpa_s->sme.assoc_req_ie_len += ext_capab_len;
467                 os_memcpy(pos, ext_capab, ext_capab_len);
468         }
469
470 #ifdef CONFIG_HS20
471         if (is_hs20_network(wpa_s, ssid, bss)) {
472                 struct wpabuf *hs20;
473
474                 hs20 = wpabuf_alloc(20);
475                 if (hs20) {
476                         int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
477                         size_t len;
478
479                         wpas_hs20_add_indication(hs20, pps_mo_id);
480                         len = sizeof(wpa_s->sme.assoc_req_ie) -
481                                 wpa_s->sme.assoc_req_ie_len;
482                         if (wpabuf_len(hs20) <= len) {
483                                 os_memcpy(wpa_s->sme.assoc_req_ie +
484                                           wpa_s->sme.assoc_req_ie_len,
485                                           wpabuf_head(hs20), wpabuf_len(hs20));
486                                 wpa_s->sme.assoc_req_ie_len += wpabuf_len(hs20);
487                         }
488                         wpabuf_free(hs20);
489                 }
490         }
491 #endif /* CONFIG_HS20 */
492
493         if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
494                 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
495                 size_t len;
496
497                 len = sizeof(wpa_s->sme.assoc_req_ie) -
498                         wpa_s->sme.assoc_req_ie_len;
499                 if (wpabuf_len(buf) <= len) {
500                         os_memcpy(wpa_s->sme.assoc_req_ie +
501                                   wpa_s->sme.assoc_req_ie_len,
502                                   wpabuf_head(buf), wpabuf_len(buf));
503                         wpa_s->sme.assoc_req_ie_len += wpabuf_len(buf);
504                 }
505         }
506
507 #ifdef CONFIG_MBO
508         if (mbo) {
509                 int len;
510
511                 len = wpas_mbo_ie(wpa_s, wpa_s->sme.assoc_req_ie +
512                                   wpa_s->sme.assoc_req_ie_len,
513                                   sizeof(wpa_s->sme.assoc_req_ie) -
514                                   wpa_s->sme.assoc_req_ie_len);
515                 if (len >= 0)
516                         wpa_s->sme.assoc_req_ie_len += len;
517         }
518 #endif /* CONFIG_MBO */
519
520 #ifdef CONFIG_SAE
521         if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE &&
522             pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid, ssid, 0) == 0)
523         {
524                 wpa_dbg(wpa_s, MSG_DEBUG,
525                         "PMKSA cache entry found - try to use PMKSA caching instead of new SAE authentication");
526                 params.auth_alg = WPA_AUTH_ALG_OPEN;
527                 wpa_s->sme.sae_pmksa_caching = 1;
528         }
529
530         if (!skip_auth && params.auth_alg == WPA_AUTH_ALG_SAE) {
531                 if (start)
532                         resp = sme_auth_build_sae_commit(wpa_s, ssid,
533                                                          bss->bssid);
534                 else
535                         resp = sme_auth_build_sae_confirm(wpa_s);
536                 if (resp == NULL) {
537                         wpas_connection_failed(wpa_s, bss->bssid);
538                         return;
539                 }
540                 params.sae_data = wpabuf_head(resp);
541                 params.sae_data_len = wpabuf_len(resp);
542                 wpa_s->sme.sae.state = start ? SAE_COMMITTED : SAE_CONFIRMED;
543         }
544 #endif /* CONFIG_SAE */
545
546         wpa_supplicant_cancel_sched_scan(wpa_s);
547         wpa_supplicant_cancel_scan(wpa_s);
548
549         wpa_msg(wpa_s, MSG_INFO, "SME: Trying to authenticate with " MACSTR
550                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
551                 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
552
553         wpa_clear_keys(wpa_s, bss->bssid);
554         wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
555         old_ssid = wpa_s->current_ssid;
556         wpa_s->current_ssid = ssid;
557         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
558         wpa_supplicant_initiate_eapol(wpa_s);
559         if (old_ssid != wpa_s->current_ssid)
560                 wpas_notify_network_changed(wpa_s);
561
562 #ifdef CONFIG_HS20
563         hs20_configure_frame_filters(wpa_s);
564 #endif /* CONFIG_HS20 */
565
566 #ifdef CONFIG_P2P
567         /*
568          * If multi-channel concurrency is not supported, check for any
569          * frequency conflict. In case of any frequency conflict, remove the
570          * least prioritized connection.
571          */
572         if (wpa_s->num_multichan_concurrent < 2) {
573                 int freq, num;
574                 num = get_shared_radio_freqs(wpa_s, &freq, 1);
575                 if (num > 0 && freq > 0 && freq != params.freq) {
576                         wpa_printf(MSG_DEBUG,
577                                    "Conflicting frequency found (%d != %d)",
578                                    freq, params.freq);
579                         if (wpas_p2p_handle_frequency_conflicts(wpa_s,
580                                                                 params.freq,
581                                                                 ssid) < 0) {
582                                 wpas_connection_failed(wpa_s, bss->bssid);
583                                 wpa_supplicant_mark_disassoc(wpa_s);
584                                 wpabuf_free(resp);
585                                 wpas_connect_work_done(wpa_s);
586                                 return;
587                         }
588                 }
589         }
590 #endif /* CONFIG_P2P */
591
592         if (skip_auth) {
593                 wpa_msg(wpa_s, MSG_DEBUG,
594                         "SME: Skip authentication step on reassoc-to-same-BSS");
595                 wpabuf_free(resp);
596                 sme_associate(wpa_s, ssid->mode, bss->bssid, WLAN_AUTH_OPEN);
597                 return;
598         }
599
600
601         wpa_s->sme.auth_alg = params.auth_alg;
602         if (wpa_drv_authenticate(wpa_s, &params) < 0) {
603                 wpa_msg(wpa_s, MSG_INFO, "SME: Authentication request to the "
604                         "driver failed");
605                 wpas_connection_failed(wpa_s, bss->bssid);
606                 wpa_supplicant_mark_disassoc(wpa_s);
607                 wpabuf_free(resp);
608                 wpas_connect_work_done(wpa_s);
609                 return;
610         }
611
612         eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
613                                NULL);
614
615         /*
616          * Association will be started based on the authentication event from
617          * the driver.
618          */
619
620         wpabuf_free(resp);
621 }
622
623
624 static void sme_auth_start_cb(struct wpa_radio_work *work, int deinit)
625 {
626         struct wpa_connect_work *cwork = work->ctx;
627         struct wpa_supplicant *wpa_s = work->wpa_s;
628
629         if (deinit) {
630                 if (work->started)
631                         wpa_s->connect_work = NULL;
632
633                 wpas_connect_work_free(cwork);
634                 return;
635         }
636
637         wpa_s->connect_work = work;
638
639         if (cwork->bss_removed ||
640             !wpas_valid_bss_ssid(wpa_s, cwork->bss, cwork->ssid) ||
641             wpas_network_disabled(wpa_s, cwork->ssid)) {
642                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: BSS/SSID entry for authentication not valid anymore - drop connection attempt");
643                 wpas_connect_work_done(wpa_s);
644                 return;
645         }
646
647         sme_send_authentication(wpa_s, cwork->bss, cwork->ssid, 1);
648 }
649
650
651 void sme_authenticate(struct wpa_supplicant *wpa_s,
652                       struct wpa_bss *bss, struct wpa_ssid *ssid)
653 {
654         struct wpa_connect_work *cwork;
655
656         if (bss == NULL || ssid == NULL)
657                 return;
658         if (wpa_s->connect_work) {
659                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reject sme_authenticate() call since connect_work exist");
660                 return;
661         }
662
663         if (radio_work_pending(wpa_s, "sme-connect")) {
664                 /*
665                  * The previous sme-connect work might no longer be valid due to
666                  * the fact that the BSS list was updated. In addition, it makes
667                  * sense to adhere to the 'newer' decision.
668                  */
669                 wpa_dbg(wpa_s, MSG_DEBUG,
670                         "SME: Remove previous pending sme-connect");
671                 radio_remove_works(wpa_s, "sme-connect", 0);
672         }
673
674         wpas_abort_ongoing_scan(wpa_s);
675
676         cwork = os_zalloc(sizeof(*cwork));
677         if (cwork == NULL)
678                 return;
679         cwork->bss = bss;
680         cwork->ssid = ssid;
681         cwork->sme = 1;
682
683 #ifdef CONFIG_SAE
684         wpa_s->sme.sae.state = SAE_NOTHING;
685         wpa_s->sme.sae.send_confirm = 0;
686         wpa_s->sme.sae_group_index = 0;
687 #endif /* CONFIG_SAE */
688
689         if (radio_add_work(wpa_s, bss->freq, "sme-connect", 1,
690                            sme_auth_start_cb, cwork) < 0)
691                 wpas_connect_work_free(cwork);
692 }
693
694
695 #ifdef CONFIG_SAE
696
697 static int sme_sae_auth(struct wpa_supplicant *wpa_s, u16 auth_transaction,
698                         u16 status_code, const u8 *data, size_t len)
699 {
700         int *groups;
701
702         wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE authentication transaction %u "
703                 "status code %u", auth_transaction, status_code);
704
705         if (auth_transaction == 1 &&
706             status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
707             wpa_s->sme.sae.state == SAE_COMMITTED &&
708             wpa_s->current_bss && wpa_s->current_ssid) {
709                 int default_groups[] = { 19, 20, 21, 25, 26, 0 };
710                 u16 group;
711
712                 groups = wpa_s->conf->sae_groups;
713                 if (!groups || groups[0] <= 0)
714                         groups = default_groups;
715
716                 if (len < sizeof(le16)) {
717                         wpa_dbg(wpa_s, MSG_DEBUG,
718                                 "SME: Too short SAE anti-clogging token request");
719                         return -1;
720                 }
721                 group = WPA_GET_LE16(data);
722                 wpa_dbg(wpa_s, MSG_DEBUG,
723                         "SME: SAE anti-clogging token requested (group %u)",
724                         group);
725                 if (sae_group_allowed(&wpa_s->sme.sae, groups, group) !=
726                     WLAN_STATUS_SUCCESS) {
727                         wpa_dbg(wpa_s, MSG_ERROR,
728                                 "SME: SAE group %u of anti-clogging request is invalid",
729                                 group);
730                         return -1;
731                 }
732                 wpabuf_free(wpa_s->sme.sae_token);
733                 wpa_s->sme.sae_token = wpabuf_alloc_copy(data + sizeof(le16),
734                                                          len - sizeof(le16));
735                 sme_send_authentication(wpa_s, wpa_s->current_bss,
736                                         wpa_s->current_ssid, 1);
737                 return 0;
738         }
739
740         if (auth_transaction == 1 &&
741             status_code == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
742             wpa_s->sme.sae.state == SAE_COMMITTED &&
743             wpa_s->current_bss && wpa_s->current_ssid) {
744                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SAE group not supported");
745                 wpa_s->sme.sae_group_index++;
746                 if (sme_set_sae_group(wpa_s) < 0)
747                         return -1; /* no other groups enabled */
748                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Try next enabled SAE group");
749                 sme_send_authentication(wpa_s, wpa_s->current_bss,
750                                         wpa_s->current_ssid, 1);
751                 return 0;
752         }
753
754         if (status_code != WLAN_STATUS_SUCCESS)
755                 return -1;
756
757         if (auth_transaction == 1) {
758                 u16 res;
759
760                 groups = wpa_s->conf->sae_groups;
761
762                 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE commit");
763                 if (wpa_s->current_bss == NULL ||
764                     wpa_s->current_ssid == NULL)
765                         return -1;
766                 if (wpa_s->sme.sae.state != SAE_COMMITTED)
767                         return -1;
768                 if (groups && groups[0] <= 0)
769                         groups = NULL;
770                 res = sae_parse_commit(&wpa_s->sme.sae, data, len, NULL, NULL,
771                                        groups);
772                 if (res == SAE_SILENTLY_DISCARD) {
773                         wpa_printf(MSG_DEBUG,
774                                    "SAE: Drop commit message due to reflection attack");
775                         return 0;
776                 }
777                 if (res != WLAN_STATUS_SUCCESS)
778                         return -1;
779
780                 if (sae_process_commit(&wpa_s->sme.sae) < 0) {
781                         wpa_printf(MSG_DEBUG, "SAE: Failed to process peer "
782                                    "commit");
783                         return -1;
784                 }
785
786                 wpabuf_free(wpa_s->sme.sae_token);
787                 wpa_s->sme.sae_token = NULL;
788                 sme_send_authentication(wpa_s, wpa_s->current_bss,
789                                         wpa_s->current_ssid, 0);
790                 return 0;
791         } else if (auth_transaction == 2) {
792                 wpa_dbg(wpa_s, MSG_DEBUG, "SME SAE confirm");
793                 if (wpa_s->sme.sae.state != SAE_CONFIRMED)
794                         return -1;
795                 if (sae_check_confirm(&wpa_s->sme.sae, data, len) < 0)
796                         return -1;
797                 wpa_s->sme.sae.state = SAE_ACCEPTED;
798                 sae_clear_temp_data(&wpa_s->sme.sae);
799                 return 1;
800         }
801
802         return -1;
803 }
804 #endif /* CONFIG_SAE */
805
806
807 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
808 {
809         struct wpa_ssid *ssid = wpa_s->current_ssid;
810
811         if (ssid == NULL) {
812                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
813                         "when network is not selected");
814                 return;
815         }
816
817         if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
818                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication event "
819                         "when not in authenticating state");
820                 return;
821         }
822
823         if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
824                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Ignore authentication with "
825                         "unexpected peer " MACSTR,
826                         MAC2STR(data->auth.peer));
827                 return;
828         }
829
830         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
831                 " auth_type=%d auth_transaction=%d status_code=%d",
832                 MAC2STR(data->auth.peer), data->auth.auth_type,
833                 data->auth.auth_transaction, data->auth.status_code);
834         wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
835                     data->auth.ies, data->auth.ies_len);
836
837         eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
838
839 #ifdef CONFIG_SAE
840         if (data->auth.auth_type == WLAN_AUTH_SAE) {
841                 int res;
842                 res = sme_sae_auth(wpa_s, data->auth.auth_transaction,
843                                    data->auth.status_code, data->auth.ies,
844                                    data->auth.ies_len);
845                 if (res < 0) {
846                         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
847                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
848
849                 }
850                 if (res != 1)
851                         return;
852
853                 wpa_printf(MSG_DEBUG, "SME: SAE completed - setting PMK for "
854                            "4-way handshake");
855                 wpa_sm_set_pmk(wpa_s->wpa, wpa_s->sme.sae.pmk, PMK_LEN,
856                                wpa_s->sme.sae.pmkid, wpa_s->pending_bssid);
857         }
858 #endif /* CONFIG_SAE */
859
860         if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
861                 char *ie_txt = NULL;
862
863                 if (data->auth.ies && data->auth.ies_len) {
864                         size_t buflen = 2 * data->auth.ies_len + 1;
865                         ie_txt = os_malloc(buflen);
866                         if (ie_txt) {
867                                 wpa_snprintf_hex(ie_txt, buflen, data->auth.ies,
868                                                  data->auth.ies_len);
869                         }
870                 }
871                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AUTH_REJECT MACSTR
872                         " auth_type=%u auth_transaction=%u status_code=%u ie=%s",
873                         MAC2STR(data->auth.peer), data->auth.auth_type,
874                         data->auth.auth_transaction, data->auth.status_code,
875                         ie_txt);
876                 os_free(ie_txt);
877
878                 if (data->auth.status_code !=
879                     WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
880                     wpa_s->sme.auth_alg == data->auth.auth_type ||
881                     wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
882                         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
883                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
884                         return;
885                 }
886
887                 wpas_connect_work_done(wpa_s);
888
889                 switch (data->auth.auth_type) {
890                 case WLAN_AUTH_OPEN:
891                         wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
892
893                         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying SHARED auth");
894                         wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
895                                                  wpa_s->current_ssid);
896                         return;
897
898                 case WLAN_AUTH_SHARED_KEY:
899                         wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
900
901                         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Trying LEAP auth");
902                         wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
903                                                  wpa_s->current_ssid);
904                         return;
905
906                 default:
907                         return;
908                 }
909         }
910
911 #ifdef CONFIG_IEEE80211R
912         if (data->auth.auth_type == WLAN_AUTH_FT) {
913                 if (wpa_ft_process_response(wpa_s->wpa, data->auth.ies,
914                                             data->auth.ies_len, 0,
915                                             data->auth.peer, NULL, 0) < 0) {
916                         wpa_dbg(wpa_s, MSG_DEBUG,
917                                 "SME: FT Authentication response processing failed");
918                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid="
919                                 MACSTR
920                                 " reason=%d locally_generated=1",
921                                 MAC2STR(wpa_s->pending_bssid),
922                                 WLAN_REASON_DEAUTH_LEAVING);
923                         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
924                         wpa_supplicant_mark_disassoc(wpa_s);
925                         return;
926                 }
927         }
928 #endif /* CONFIG_IEEE80211R */
929
930         sme_associate(wpa_s, ssid->mode, data->auth.peer,
931                       data->auth.auth_type);
932 }
933
934
935 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
936                    const u8 *bssid, u16 auth_type)
937 {
938         struct wpa_driver_associate_params params;
939         struct ieee802_11_elems elems;
940 #ifdef CONFIG_HT_OVERRIDES
941         struct ieee80211_ht_capabilities htcaps;
942         struct ieee80211_ht_capabilities htcaps_mask;
943 #endif /* CONFIG_HT_OVERRIDES */
944 #ifdef CONFIG_VHT_OVERRIDES
945         struct ieee80211_vht_capabilities vhtcaps;
946         struct ieee80211_vht_capabilities vhtcaps_mask;
947 #endif /* CONFIG_VHT_OVERRIDES */
948
949         os_memset(&params, 0, sizeof(params));
950         params.bssid = bssid;
951         params.ssid = wpa_s->sme.ssid;
952         params.ssid_len = wpa_s->sme.ssid_len;
953         params.freq.freq = wpa_s->sme.freq;
954         params.bg_scan_period = wpa_s->current_ssid ?
955                 wpa_s->current_ssid->bg_scan_period : -1;
956         params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
957                 wpa_s->sme.assoc_req_ie : NULL;
958         params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
959         params.pairwise_suite = wpa_s->pairwise_cipher;
960         params.group_suite = wpa_s->group_cipher;
961         params.key_mgmt_suite = wpa_s->key_mgmt;
962         params.wpa_proto = wpa_s->wpa_proto;
963 #ifdef CONFIG_HT_OVERRIDES
964         os_memset(&htcaps, 0, sizeof(htcaps));
965         os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
966         params.htcaps = (u8 *) &htcaps;
967         params.htcaps_mask = (u8 *) &htcaps_mask;
968         wpa_supplicant_apply_ht_overrides(wpa_s, wpa_s->current_ssid, &params);
969 #endif /* CONFIG_HT_OVERRIDES */
970 #ifdef CONFIG_VHT_OVERRIDES
971         os_memset(&vhtcaps, 0, sizeof(vhtcaps));
972         os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
973         params.vhtcaps = &vhtcaps;
974         params.vhtcaps_mask = &vhtcaps_mask;
975         wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
976 #endif /* CONFIG_VHT_OVERRIDES */
977 #ifdef CONFIG_IEEE80211R
978         if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
979                 params.wpa_ie = wpa_s->sme.ft_ies;
980                 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
981         }
982 #endif /* CONFIG_IEEE80211R */
983         params.mode = mode;
984         params.mgmt_frame_protection = wpa_s->sme.mfp;
985         params.rrm_used = wpa_s->rrm.rrm_used;
986         if (wpa_s->sme.prev_bssid_set)
987                 params.prev_bssid = wpa_s->sme.prev_bssid;
988
989         wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
990                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
991                 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
992                 params.freq.freq);
993
994         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
995
996         if (params.wpa_ie == NULL ||
997             ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
998             < 0) {
999                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Could not parse own IEs?!");
1000                 os_memset(&elems, 0, sizeof(elems));
1001         }
1002         if (elems.rsn_ie) {
1003                 params.wpa_proto = WPA_PROTO_RSN;
1004                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
1005                                         elems.rsn_ie_len + 2);
1006         } else if (elems.wpa_ie) {
1007                 params.wpa_proto = WPA_PROTO_WPA;
1008                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
1009                                         elems.wpa_ie_len + 2);
1010         } else if (elems.osen) {
1011                 params.wpa_proto = WPA_PROTO_OSEN;
1012                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.osen - 2,
1013                                         elems.osen_len + 2);
1014         } else
1015                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1016         if (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group)
1017                 params.p2p = 1;
1018
1019         if (wpa_s->p2pdev->set_sta_uapsd)
1020                 params.uapsd = wpa_s->p2pdev->sta_uapsd;
1021         else
1022                 params.uapsd = -1;
1023
1024         if (wpa_drv_associate(wpa_s, &params) < 0) {
1025                 wpa_msg(wpa_s, MSG_INFO, "SME: Association request to the "
1026                         "driver failed");
1027                 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1028                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1029                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1030                 return;
1031         }
1032
1033         eloop_register_timeout(SME_ASSOC_TIMEOUT, 0, sme_assoc_timer, wpa_s,
1034                                NULL);
1035 }
1036
1037
1038 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
1039                       const u8 *ies, size_t ies_len)
1040 {
1041         if (md == NULL || ies == NULL) {
1042                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Remove mobility domain");
1043                 os_free(wpa_s->sme.ft_ies);
1044                 wpa_s->sme.ft_ies = NULL;
1045                 wpa_s->sme.ft_ies_len = 0;
1046                 wpa_s->sme.ft_used = 0;
1047                 return 0;
1048         }
1049
1050         os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
1051         wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
1052         os_free(wpa_s->sme.ft_ies);
1053         wpa_s->sme.ft_ies = os_malloc(ies_len);
1054         if (wpa_s->sme.ft_ies == NULL)
1055                 return -1;
1056         os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
1057         wpa_s->sme.ft_ies_len = ies_len;
1058         return 0;
1059 }
1060
1061
1062 static void sme_deauth(struct wpa_supplicant *wpa_s)
1063 {
1064         int bssid_changed;
1065
1066         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1067
1068         if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
1069                                    WLAN_REASON_DEAUTH_LEAVING) < 0) {
1070                 wpa_msg(wpa_s, MSG_INFO, "SME: Deauth request to the driver "
1071                         "failed");
1072         }
1073         wpa_s->sme.prev_bssid_set = 0;
1074
1075         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1076         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1077         os_memset(wpa_s->bssid, 0, ETH_ALEN);
1078         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1079         if (bssid_changed)
1080                 wpas_notify_bssid_changed(wpa_s);
1081 }
1082
1083
1084 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
1085                             union wpa_event_data *data)
1086 {
1087         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association with " MACSTR " failed: "
1088                 "status code %d", MAC2STR(wpa_s->pending_bssid),
1089                 data->assoc_reject.status_code);
1090
1091         eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1092
1093 #ifdef CONFIG_SAE
1094         if (wpa_s->sme.sae_pmksa_caching && wpa_s->current_ssid &&
1095             wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt)) {
1096                 wpa_dbg(wpa_s, MSG_DEBUG,
1097                         "PMKSA caching attempt rejected - drop PMKSA cache entry and fall back to SAE authentication");
1098                 wpa_sm_aborted_cached(wpa_s->wpa);
1099                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
1100                 if (wpa_s->current_bss) {
1101                         struct wpa_bss *bss = wpa_s->current_bss;
1102                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1103
1104                         wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
1105                                                WLAN_REASON_DEAUTH_LEAVING);
1106                         wpas_connect_work_done(wpa_s);
1107                         wpa_supplicant_mark_disassoc(wpa_s);
1108                         wpa_supplicant_connect(wpa_s, bss, ssid);
1109                         return;
1110                 }
1111         }
1112 #endif /* CONFIG_SAE */
1113
1114         /*
1115          * For now, unconditionally terminate the previous authentication. In
1116          * theory, this should not be needed, but mac80211 gets quite confused
1117          * if the authentication is left pending.. Some roaming cases might
1118          * benefit from using the previous authentication, so this could be
1119          * optimized in the future.
1120          */
1121         sme_deauth(wpa_s);
1122 }
1123
1124
1125 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
1126                               union wpa_event_data *data)
1127 {
1128         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Authentication timed out");
1129         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1130         wpa_supplicant_mark_disassoc(wpa_s);
1131 }
1132
1133
1134 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
1135                                union wpa_event_data *data)
1136 {
1137         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association timed out");
1138         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1139         wpa_supplicant_mark_disassoc(wpa_s);
1140 }
1141
1142
1143 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
1144                         struct disassoc_info *info)
1145 {
1146         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Disassociation event received");
1147         if (wpa_s->sme.prev_bssid_set) {
1148                 /*
1149                  * cfg80211/mac80211 can get into somewhat confused state if
1150                  * the AP only disassociates us and leaves us in authenticated
1151                  * state. For now, force the state to be cleared to avoid
1152                  * confusing errors if we try to associate with the AP again.
1153                  */
1154                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Deauthenticate to clear "
1155                         "driver state");
1156                 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
1157                                        WLAN_REASON_DEAUTH_LEAVING);
1158         }
1159 }
1160
1161
1162 static void sme_auth_timer(void *eloop_ctx, void *timeout_ctx)
1163 {
1164         struct wpa_supplicant *wpa_s = eloop_ctx;
1165         if (wpa_s->wpa_state == WPA_AUTHENTICATING) {
1166                 wpa_msg(wpa_s, MSG_DEBUG, "SME: Authentication timeout");
1167                 sme_deauth(wpa_s);
1168         }
1169 }
1170
1171
1172 static void sme_assoc_timer(void *eloop_ctx, void *timeout_ctx)
1173 {
1174         struct wpa_supplicant *wpa_s = eloop_ctx;
1175         if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1176                 wpa_msg(wpa_s, MSG_DEBUG, "SME: Association timeout");
1177                 sme_deauth(wpa_s);
1178         }
1179 }
1180
1181
1182 void sme_state_changed(struct wpa_supplicant *wpa_s)
1183 {
1184         /* Make sure timers are cleaned up appropriately. */
1185         if (wpa_s->wpa_state != WPA_ASSOCIATING)
1186                 eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1187         if (wpa_s->wpa_state != WPA_AUTHENTICATING)
1188                 eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1189 }
1190
1191
1192 void sme_disassoc_while_authenticating(struct wpa_supplicant *wpa_s,
1193                                        const u8 *prev_pending_bssid)
1194 {
1195         /*
1196          * mac80211-workaround to force deauth on failed auth cmd,
1197          * requires us to remain in authenticating state to allow the
1198          * second authentication attempt to be continued properly.
1199          */
1200         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Allow pending authentication "
1201                 "to proceed after disconnection event");
1202         wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1203         os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1204
1205         /*
1206          * Re-arm authentication timer in case auth fails for whatever reason.
1207          */
1208         eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1209         eloop_register_timeout(SME_AUTH_TIMEOUT, 0, sme_auth_timer, wpa_s,
1210                                NULL);
1211 }
1212
1213
1214 void sme_clear_on_disassoc(struct wpa_supplicant *wpa_s)
1215 {
1216         wpa_s->sme.prev_bssid_set = 0;
1217 #ifdef CONFIG_SAE
1218         wpabuf_free(wpa_s->sme.sae_token);
1219         wpa_s->sme.sae_token = NULL;
1220         sae_clear_data(&wpa_s->sme.sae);
1221 #endif /* CONFIG_SAE */
1222 #ifdef CONFIG_IEEE80211R
1223         if (wpa_s->sme.ft_ies)
1224                 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
1225 #endif /* CONFIG_IEEE80211R */
1226 }
1227
1228
1229 void sme_deinit(struct wpa_supplicant *wpa_s)
1230 {
1231         os_free(wpa_s->sme.ft_ies);
1232         wpa_s->sme.ft_ies = NULL;
1233         wpa_s->sme.ft_ies_len = 0;
1234 #ifdef CONFIG_IEEE80211W
1235         sme_stop_sa_query(wpa_s);
1236 #endif /* CONFIG_IEEE80211W */
1237         sme_clear_on_disassoc(wpa_s);
1238
1239         eloop_cancel_timeout(sme_assoc_timer, wpa_s, NULL);
1240         eloop_cancel_timeout(sme_auth_timer, wpa_s, NULL);
1241         eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1242 }
1243
1244
1245 static void sme_send_2040_bss_coex(struct wpa_supplicant *wpa_s,
1246                                    const u8 *chan_list, u8 num_channels,
1247                                    u8 num_intol)
1248 {
1249         struct ieee80211_2040_bss_coex_ie *bc_ie;
1250         struct ieee80211_2040_intol_chan_report *ic_report;
1251         struct wpabuf *buf;
1252
1253         wpa_printf(MSG_DEBUG, "SME: Send 20/40 BSS Coexistence to " MACSTR
1254                    " (num_channels=%u num_intol=%u)",
1255                    MAC2STR(wpa_s->bssid), num_channels, num_intol);
1256         wpa_hexdump(MSG_DEBUG, "SME: 20/40 BSS Intolerant Channels",
1257                     chan_list, num_channels);
1258
1259         buf = wpabuf_alloc(2 + /* action.category + action_code */
1260                            sizeof(struct ieee80211_2040_bss_coex_ie) +
1261                            sizeof(struct ieee80211_2040_intol_chan_report) +
1262                            num_channels);
1263         if (buf == NULL)
1264                 return;
1265
1266         wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
1267         wpabuf_put_u8(buf, WLAN_PA_20_40_BSS_COEX);
1268
1269         bc_ie = wpabuf_put(buf, sizeof(*bc_ie));
1270         bc_ie->element_id = WLAN_EID_20_40_BSS_COEXISTENCE;
1271         bc_ie->length = 1;
1272         if (num_intol)
1273                 bc_ie->coex_param |= WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ;
1274
1275         if (num_channels > 0) {
1276                 ic_report = wpabuf_put(buf, sizeof(*ic_report));
1277                 ic_report->element_id = WLAN_EID_20_40_BSS_INTOLERANT;
1278                 ic_report->length = num_channels + 1;
1279                 ic_report->op_class = 0;
1280                 os_memcpy(wpabuf_put(buf, num_channels), chan_list,
1281                           num_channels);
1282         }
1283
1284         if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1285                                 wpa_s->own_addr, wpa_s->bssid,
1286                                 wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
1287                 wpa_msg(wpa_s, MSG_INFO,
1288                         "SME: Failed to send 20/40 BSS Coexistence frame");
1289         }
1290
1291         wpabuf_free(buf);
1292 }
1293
1294
1295 int sme_proc_obss_scan(struct wpa_supplicant *wpa_s)
1296 {
1297         struct wpa_bss *bss;
1298         const u8 *ie;
1299         u16 ht_cap;
1300         u8 chan_list[P2P_MAX_CHANNELS], channel;
1301         u8 num_channels = 0, num_intol = 0, i;
1302
1303         if (!wpa_s->sme.sched_obss_scan)
1304                 return 0;
1305
1306         wpa_s->sme.sched_obss_scan = 0;
1307         if (!wpa_s->current_bss || wpa_s->wpa_state != WPA_COMPLETED)
1308                 return 1;
1309
1310         /*
1311          * Check whether AP uses regulatory triplet or channel triplet in
1312          * country info. Right now the operating class of the BSS channel
1313          * width trigger event is "unknown" (IEEE Std 802.11-2012 10.15.12),
1314          * based on the assumption that operating class triplet is not used in
1315          * beacon frame. If the First Channel Number/Operating Extension
1316          * Identifier octet has a positive integer value of 201 or greater,
1317          * then its operating class triplet.
1318          *
1319          * TODO: If Supported Operating Classes element is present in beacon
1320          * frame, have to lookup operating class in Annex E and fill them in
1321          * 2040 coex frame.
1322          */
1323         ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
1324         if (ie && (ie[1] >= 6) && (ie[5] >= 201))
1325                 return 1;
1326
1327         os_memset(chan_list, 0, sizeof(chan_list));
1328
1329         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1330                 /* Skip other band bss */
1331                 enum hostapd_hw_mode mode;
1332                 mode = ieee80211_freq_to_chan(bss->freq, &channel);
1333                 if (mode != HOSTAPD_MODE_IEEE80211G &&
1334                     mode != HOSTAPD_MODE_IEEE80211B)
1335                         continue;
1336
1337                 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_CAP);
1338                 ht_cap = (ie && (ie[1] == 26)) ? WPA_GET_LE16(ie + 2) : 0;
1339                 wpa_printf(MSG_DEBUG, "SME OBSS scan BSS " MACSTR
1340                            " freq=%u chan=%u ht_cap=0x%x",
1341                            MAC2STR(bss->bssid), bss->freq, channel, ht_cap);
1342
1343                 if (!ht_cap || (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)) {
1344                         if (ht_cap & HT_CAP_INFO_40MHZ_INTOLERANT)
1345                                 num_intol++;
1346
1347                         /* Check whether the channel is already considered */
1348                         for (i = 0; i < num_channels; i++) {
1349                                 if (channel == chan_list[i])
1350                                         break;
1351                         }
1352                         if (i != num_channels)
1353                                 continue;
1354
1355                         chan_list[num_channels++] = channel;
1356                 }
1357         }
1358
1359         sme_send_2040_bss_coex(wpa_s, chan_list, num_channels, num_intol);
1360         return 1;
1361 }
1362
1363
1364 static void wpa_obss_scan_freqs_list(struct wpa_supplicant *wpa_s,
1365                                      struct wpa_driver_scan_params *params)
1366 {
1367         /* Include only affected channels */
1368         struct hostapd_hw_modes *mode;
1369         int count, i;
1370         int start, end;
1371
1372         mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
1373                         HOSTAPD_MODE_IEEE80211G);
1374         if (mode == NULL) {
1375                 /* No channels supported in this band - use empty list */
1376                 params->freqs = os_zalloc(sizeof(int));
1377                 return;
1378         }
1379
1380         if (wpa_s->sme.ht_sec_chan == HT_SEC_CHAN_UNKNOWN &&
1381             wpa_s->current_bss) {
1382                 const u8 *ie;
1383
1384                 ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_OPERATION);
1385                 if (ie && ie[1] >= 2) {
1386                         u8 o;
1387
1388                         o = ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
1389                         if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
1390                                 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
1391                         else if (o == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
1392                                 wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
1393                 }
1394         }
1395
1396         start = wpa_s->assoc_freq - 10;
1397         end = wpa_s->assoc_freq + 10;
1398         switch (wpa_s->sme.ht_sec_chan) {
1399         case HT_SEC_CHAN_UNKNOWN:
1400                 /* HT40+ possible on channels 1..9 */
1401                 if (wpa_s->assoc_freq <= 2452)
1402                         start -= 20;
1403                 /* HT40- possible on channels 5-13 */
1404                 if (wpa_s->assoc_freq >= 2432)
1405                         end += 20;
1406                 break;
1407         case HT_SEC_CHAN_ABOVE:
1408                 end += 20;
1409                 break;
1410         case HT_SEC_CHAN_BELOW:
1411                 start -= 20;
1412                 break;
1413         }
1414         wpa_printf(MSG_DEBUG,
1415                    "OBSS: assoc_freq %d possible affected range %d-%d",
1416                    wpa_s->assoc_freq, start, end);
1417
1418         params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
1419         if (params->freqs == NULL)
1420                 return;
1421         for (count = 0, i = 0; i < mode->num_channels; i++) {
1422                 int freq;
1423
1424                 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
1425                         continue;
1426                 freq = mode->channels[i].freq;
1427                 if (freq - 10 >= end || freq + 10 <= start)
1428                         continue; /* not affected */
1429                 params->freqs[count++] = freq;
1430         }
1431 }
1432
1433
1434 static void sme_obss_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1435 {
1436         struct wpa_supplicant *wpa_s = eloop_ctx;
1437         struct wpa_driver_scan_params params;
1438
1439         if (!wpa_s->current_bss) {
1440                 wpa_printf(MSG_DEBUG, "SME OBSS: Ignore scan request");
1441                 return;
1442         }
1443
1444         os_memset(&params, 0, sizeof(params));
1445         wpa_obss_scan_freqs_list(wpa_s, &params);
1446         params.low_priority = 1;
1447         wpa_printf(MSG_DEBUG, "SME OBSS: Request an OBSS scan");
1448
1449         if (wpa_supplicant_trigger_scan(wpa_s, &params))
1450                 wpa_printf(MSG_DEBUG, "SME OBSS: Failed to trigger scan");
1451         else
1452                 wpa_s->sme.sched_obss_scan = 1;
1453         os_free(params.freqs);
1454
1455         eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1456                                sme_obss_scan_timeout, wpa_s, NULL);
1457 }
1458
1459
1460 void sme_sched_obss_scan(struct wpa_supplicant *wpa_s, int enable)
1461 {
1462         const u8 *ie;
1463         struct wpa_bss *bss = wpa_s->current_bss;
1464         struct wpa_ssid *ssid = wpa_s->current_ssid;
1465         struct hostapd_hw_modes *hw_mode = NULL;
1466         int i;
1467
1468         eloop_cancel_timeout(sme_obss_scan_timeout, wpa_s, NULL);
1469         wpa_s->sme.sched_obss_scan = 0;
1470         wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
1471         if (!enable)
1472                 return;
1473
1474         /*
1475          * Schedule OBSS scan if driver is using station SME in wpa_supplicant
1476          * or it expects OBSS scan to be performed by wpa_supplicant.
1477          */
1478         if (!((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
1479               (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OBSS_SCAN)) ||
1480             ssid == NULL || ssid->mode != IEEE80211_MODE_INFRA)
1481                 return;
1482
1483         if (!wpa_s->hw.modes)
1484                 return;
1485
1486         /* only HT caps in 11g mode are relevant */
1487         for (i = 0; i < wpa_s->hw.num_modes; i++) {
1488                 hw_mode = &wpa_s->hw.modes[i];
1489                 if (hw_mode->mode == HOSTAPD_MODE_IEEE80211G)
1490                         break;
1491         }
1492
1493         /* Driver does not support HT40 for 11g or doesn't have 11g. */
1494         if (i == wpa_s->hw.num_modes || !hw_mode ||
1495             !(hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1496                 return;
1497
1498         if (bss == NULL || bss->freq < 2400 || bss->freq > 2500)
1499                 return; /* Not associated on 2.4 GHz band */
1500
1501         /* Check whether AP supports HT40 */
1502         ie = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_HT_CAP);
1503         if (!ie || ie[1] < 2 ||
1504             !(WPA_GET_LE16(ie + 2) & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1505                 return; /* AP does not support HT40 */
1506
1507         ie = wpa_bss_get_ie(wpa_s->current_bss,
1508                             WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS);
1509         if (!ie || ie[1] < 14)
1510                 return; /* AP does not request OBSS scans */
1511
1512         wpa_s->sme.obss_scan_int = WPA_GET_LE16(ie + 6);
1513         if (wpa_s->sme.obss_scan_int < 10) {
1514                 wpa_printf(MSG_DEBUG, "SME: Invalid OBSS Scan Interval %u "
1515                            "replaced with the minimum 10 sec",
1516                            wpa_s->sme.obss_scan_int);
1517                 wpa_s->sme.obss_scan_int = 10;
1518         }
1519         wpa_printf(MSG_DEBUG, "SME: OBSS Scan Interval %u sec",
1520                    wpa_s->sme.obss_scan_int);
1521         eloop_register_timeout(wpa_s->sme.obss_scan_int, 0,
1522                                sme_obss_scan_timeout, wpa_s, NULL);
1523 }
1524
1525
1526 #ifdef CONFIG_IEEE80211W
1527
1528 static const unsigned int sa_query_max_timeout = 1000;
1529 static const unsigned int sa_query_retry_timeout = 201;
1530
1531 static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
1532 {
1533         u32 tu;
1534         struct os_reltime now, passed;
1535         os_get_reltime(&now);
1536         os_reltime_sub(&now, &wpa_s->sme.sa_query_start, &passed);
1537         tu = (passed.sec * 1000000 + passed.usec) / 1024;
1538         if (sa_query_max_timeout < tu) {
1539                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: SA Query timed out");
1540                 sme_stop_sa_query(wpa_s);
1541                 wpa_supplicant_deauthenticate(
1542                         wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
1543                 return 1;
1544         }
1545
1546         return 0;
1547 }
1548
1549
1550 static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
1551                                   const u8 *trans_id)
1552 {
1553         u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
1554         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Sending SA Query Request to "
1555                 MACSTR, MAC2STR(wpa_s->bssid));
1556         wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
1557                     trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1558         req[0] = WLAN_ACTION_SA_QUERY;
1559         req[1] = WLAN_SA_QUERY_REQUEST;
1560         os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1561         if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1562                                 wpa_s->own_addr, wpa_s->bssid,
1563                                 req, sizeof(req), 0) < 0)
1564                 wpa_msg(wpa_s, MSG_INFO, "SME: Failed to send SA Query "
1565                         "Request");
1566 }
1567
1568
1569 static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1570 {
1571         struct wpa_supplicant *wpa_s = eloop_ctx;
1572         unsigned int timeout, sec, usec;
1573         u8 *trans_id, *nbuf;
1574
1575         if (wpa_s->sme.sa_query_count > 0 &&
1576             sme_check_sa_query_timeout(wpa_s))
1577                 return;
1578
1579         nbuf = os_realloc_array(wpa_s->sme.sa_query_trans_id,
1580                                 wpa_s->sme.sa_query_count + 1,
1581                                 WLAN_SA_QUERY_TR_ID_LEN);
1582         if (nbuf == NULL)
1583                 return;
1584         if (wpa_s->sme.sa_query_count == 0) {
1585                 /* Starting a new SA Query procedure */
1586                 os_get_reltime(&wpa_s->sme.sa_query_start);
1587         }
1588         trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1589         wpa_s->sme.sa_query_trans_id = nbuf;
1590         wpa_s->sme.sa_query_count++;
1591
1592         if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1593                 wpa_printf(MSG_DEBUG, "Could not generate SA Query ID");
1594                 return;
1595         }
1596
1597         timeout = sa_query_retry_timeout;
1598         sec = ((timeout / 1000) * 1024) / 1000;
1599         usec = (timeout % 1000) * 1024;
1600         eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
1601
1602         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Association SA Query attempt %d",
1603                 wpa_s->sme.sa_query_count);
1604
1605         sme_send_sa_query_req(wpa_s, trans_id);
1606 }
1607
1608
1609 static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
1610 {
1611         sme_sa_query_timer(wpa_s, NULL);
1612 }
1613
1614
1615 static void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
1616 {
1617         eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
1618         os_free(wpa_s->sme.sa_query_trans_id);
1619         wpa_s->sme.sa_query_trans_id = NULL;
1620         wpa_s->sme.sa_query_count = 0;
1621 }
1622
1623
1624 void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
1625                                  const u8 *da, u16 reason_code)
1626 {
1627         struct wpa_ssid *ssid;
1628         struct os_reltime now;
1629
1630         if (wpa_s->wpa_state != WPA_COMPLETED)
1631                 return;
1632         ssid = wpa_s->current_ssid;
1633         if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION)
1634                 return;
1635         if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1636                 return;
1637         if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
1638             reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
1639                 return;
1640         if (wpa_s->sme.sa_query_count > 0)
1641                 return;
1642
1643         os_get_reltime(&now);
1644         if (wpa_s->sme.last_unprot_disconnect.sec &&
1645             !os_reltime_expired(&now, &wpa_s->sme.last_unprot_disconnect, 10))
1646                 return; /* limit SA Query procedure frequency */
1647         wpa_s->sme.last_unprot_disconnect = now;
1648
1649         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Unprotected disconnect dropped - "
1650                 "possible AP/STA state mismatch - trigger SA Query");
1651         sme_start_sa_query(wpa_s);
1652 }
1653
1654
1655 void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
1656                      const u8 *data, size_t len)
1657 {
1658         int i;
1659
1660         if (wpa_s->sme.sa_query_trans_id == NULL ||
1661             len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
1662             data[0] != WLAN_SA_QUERY_RESPONSE)
1663                 return;
1664         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Received SA Query response from "
1665                 MACSTR " (trans_id %02x%02x)", MAC2STR(sa), data[1], data[2]);
1666
1667         if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
1668                 return;
1669
1670         for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
1671                 if (os_memcmp(wpa_s->sme.sa_query_trans_id +
1672                               i * WLAN_SA_QUERY_TR_ID_LEN,
1673                               data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
1674                         break;
1675         }
1676
1677         if (i >= wpa_s->sme.sa_query_count) {
1678                 wpa_dbg(wpa_s, MSG_DEBUG, "SME: No matching SA Query "
1679                         "transaction identifier found");
1680                 return;
1681         }
1682
1683         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Reply to pending SA Query received "
1684                 "from " MACSTR, MAC2STR(sa));
1685         sme_stop_sa_query(wpa_s);
1686 }
1687
1688 #endif /* CONFIG_IEEE80211W */