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