]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa/wpa_supplicant/events.c
Add libbearssl
[FreeBSD/FreeBSD.git] / contrib / wpa / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2017, 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 "eapol_supp/eapol_supp_sm.h"
13 #include "rsn_supp/wpa.h"
14 #include "eloop.h"
15 #include "config.h"
16 #include "l2_packet/l2_packet.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "pcsc_funcs.h"
20 #include "rsn_supp/preauth.h"
21 #include "rsn_supp/pmksa_cache.h"
22 #include "common/wpa_ctrl.h"
23 #include "eap_peer/eap.h"
24 #include "ap/hostapd.h"
25 #include "p2p/p2p.h"
26 #include "fst/fst.h"
27 #include "wnm_sta.h"
28 #include "notify.h"
29 #include "common/ieee802_11_defs.h"
30 #include "common/ieee802_11_common.h"
31 #include "common/gas_server.h"
32 #include "crypto/random.h"
33 #include "blacklist.h"
34 #include "wpas_glue.h"
35 #include "wps_supplicant.h"
36 #include "ibss_rsn.h"
37 #include "sme.h"
38 #include "gas_query.h"
39 #include "p2p_supplicant.h"
40 #include "bgscan.h"
41 #include "autoscan.h"
42 #include "ap.h"
43 #include "bss.h"
44 #include "scan.h"
45 #include "offchannel.h"
46 #include "interworking.h"
47 #include "mesh.h"
48 #include "mesh_mpm.h"
49 #include "wmm_ac.h"
50 #include "dpp_supplicant.h"
51
52
53 #define MAX_OWE_TRANSITION_BSS_SELECT_COUNT 5
54
55
56 #ifndef CONFIG_NO_SCAN_PROCESSING
57 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
58                                               int new_scan, int own_request);
59 #endif /* CONFIG_NO_SCAN_PROCESSING */
60
61
62 int wpas_temp_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
63 {
64         struct os_reltime now;
65
66         if (ssid == NULL || ssid->disabled_until.sec == 0)
67                 return 0;
68
69         os_get_reltime(&now);
70         if (ssid->disabled_until.sec > now.sec)
71                 return ssid->disabled_until.sec - now.sec;
72
73         wpas_clear_temp_disabled(wpa_s, ssid, 0);
74
75         return 0;
76 }
77
78
79 #ifndef CONFIG_NO_SCAN_PROCESSING
80 /**
81  * wpas_reenabled_network_time - Time until first network is re-enabled
82  * @wpa_s: Pointer to wpa_supplicant data
83  * Returns: If all enabled networks are temporarily disabled, returns the time
84  *      (in sec) until the first network is re-enabled. Otherwise returns 0.
85  *
86  * This function is used in case all enabled networks are temporarily disabled,
87  * in which case it returns the time (in sec) that the first network will be
88  * re-enabled. The function assumes that at least one network is enabled.
89  */
90 static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
91 {
92         struct wpa_ssid *ssid;
93         int disabled_for, res = 0;
94
95 #ifdef CONFIG_INTERWORKING
96         if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
97             wpa_s->conf->cred)
98                 return 0;
99 #endif /* CONFIG_INTERWORKING */
100
101         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
102                 if (ssid->disabled)
103                         continue;
104
105                 disabled_for = wpas_temp_disabled(wpa_s, ssid);
106                 if (!disabled_for)
107                         return 0;
108
109                 if (!res || disabled_for < res)
110                         res = disabled_for;
111         }
112
113         return res;
114 }
115 #endif /* CONFIG_NO_SCAN_PROCESSING */
116
117
118 void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
119 {
120         struct wpa_supplicant *wpa_s = eloop_ctx;
121
122         if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
123                 return;
124
125         wpa_dbg(wpa_s, MSG_DEBUG,
126                 "Try to associate due to network getting re-enabled");
127         if (wpa_supplicant_fast_associate(wpa_s) != 1) {
128                 wpa_supplicant_cancel_sched_scan(wpa_s);
129                 wpa_supplicant_req_scan(wpa_s, 0, 0);
130         }
131 }
132
133
134 static struct wpa_bss * wpa_supplicant_get_new_bss(
135         struct wpa_supplicant *wpa_s, const u8 *bssid)
136 {
137         struct wpa_bss *bss = NULL;
138         struct wpa_ssid *ssid = wpa_s->current_ssid;
139
140         if (ssid->ssid_len > 0)
141                 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
142         if (!bss)
143                 bss = wpa_bss_get_bssid(wpa_s, bssid);
144
145         return bss;
146 }
147
148
149 static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
150 {
151         struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
152
153         if (!bss) {
154                 wpa_supplicant_update_scan_results(wpa_s);
155
156                 /* Get the BSS from the new scan results */
157                 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
158         }
159
160         if (bss)
161                 wpa_s->current_bss = bss;
162 }
163
164
165 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
166 {
167         struct wpa_ssid *ssid, *old_ssid;
168         u8 drv_ssid[SSID_MAX_LEN];
169         size_t drv_ssid_len;
170         int res;
171
172         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
173                 wpa_supplicant_update_current_bss(wpa_s);
174
175                 if (wpa_s->current_ssid->ssid_len == 0)
176                         return 0; /* current profile still in use */
177                 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
178                 if (res < 0) {
179                         wpa_msg(wpa_s, MSG_INFO,
180                                 "Failed to read SSID from driver");
181                         return 0; /* try to use current profile */
182                 }
183                 drv_ssid_len = res;
184
185                 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
186                     os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
187                               drv_ssid_len) == 0)
188                         return 0; /* current profile still in use */
189
190                 wpa_msg(wpa_s, MSG_DEBUG,
191                         "Driver-initiated BSS selection changed the SSID to %s",
192                         wpa_ssid_txt(drv_ssid, drv_ssid_len));
193                 /* continue selecting a new network profile */
194         }
195
196         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
197                 "information");
198         ssid = wpa_supplicant_get_ssid(wpa_s);
199         if (ssid == NULL) {
200                 wpa_msg(wpa_s, MSG_INFO,
201                         "No network configuration found for the current AP");
202                 return -1;
203         }
204
205         if (wpas_network_disabled(wpa_s, ssid)) {
206                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
207                 return -1;
208         }
209
210         if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
211             disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
212                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
213                 return -1;
214         }
215
216         res = wpas_temp_disabled(wpa_s, ssid);
217         if (res > 0) {
218                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
219                         "disabled for %d second(s)", res);
220                 return -1;
221         }
222
223         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
224                 "current AP");
225         if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
226                 u8 wpa_ie[80];
227                 size_t wpa_ie_len = sizeof(wpa_ie);
228                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
229                                               wpa_ie, &wpa_ie_len) < 0)
230                         wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
231         } else {
232                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
233         }
234
235         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
236                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
237         old_ssid = wpa_s->current_ssid;
238         wpa_s->current_ssid = ssid;
239
240         wpa_supplicant_update_current_bss(wpa_s);
241
242         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
243         wpa_supplicant_initiate_eapol(wpa_s);
244         if (old_ssid != wpa_s->current_ssid)
245                 wpas_notify_network_changed(wpa_s);
246
247         return 0;
248 }
249
250
251 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
252 {
253         struct wpa_supplicant *wpa_s = eloop_ctx;
254
255         if (wpa_s->countermeasures) {
256                 wpa_s->countermeasures = 0;
257                 wpa_drv_set_countermeasures(wpa_s, 0);
258                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
259
260                 /*
261                  * It is possible that the device is sched scanning, which means
262                  * that a connection attempt will be done only when we receive
263                  * scan results. However, in this case, it would be preferable
264                  * to scan and connect immediately, so cancel the sched_scan and
265                  * issue a regular scan flow.
266                  */
267                 wpa_supplicant_cancel_sched_scan(wpa_s);
268                 wpa_supplicant_req_scan(wpa_s, 0, 0);
269         }
270 }
271
272
273 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
274 {
275         int bssid_changed;
276
277         wnm_bss_keep_alive_deinit(wpa_s);
278
279 #ifdef CONFIG_IBSS_RSN
280         ibss_rsn_deinit(wpa_s->ibss_rsn);
281         wpa_s->ibss_rsn = NULL;
282 #endif /* CONFIG_IBSS_RSN */
283
284 #ifdef CONFIG_AP
285         wpa_supplicant_ap_deinit(wpa_s);
286 #endif /* CONFIG_AP */
287
288 #ifdef CONFIG_HS20
289         /* Clear possibly configured frame filters */
290         wpa_drv_configure_frame_filters(wpa_s, 0);
291 #endif /* CONFIG_HS20 */
292
293         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
294                 return;
295
296         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
297         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
298         os_memset(wpa_s->bssid, 0, ETH_ALEN);
299         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
300         sme_clear_on_disassoc(wpa_s);
301         wpa_s->current_bss = NULL;
302         wpa_s->assoc_freq = 0;
303
304         if (bssid_changed)
305                 wpas_notify_bssid_changed(wpa_s);
306
307         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
308         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
309         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
310             wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
311             wpa_s->key_mgmt == WPA_KEY_MGMT_DPP)
312                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
313         wpa_s->ap_ies_from_associnfo = 0;
314         wpa_s->current_ssid = NULL;
315         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
316         wpa_s->key_mgmt = 0;
317
318         wpas_rrm_reset(wpa_s);
319         wpa_s->wnmsleep_used = 0;
320         wnm_clear_coloc_intf_reporting(wpa_s);
321
322 #ifdef CONFIG_TESTING_OPTIONS
323         wpa_s->last_tk_alg = WPA_ALG_NONE;
324         os_memset(wpa_s->last_tk, 0, sizeof(wpa_s->last_tk));
325 #endif /* CONFIG_TESTING_OPTIONS */
326         wpa_s->ieee80211ac = 0;
327 }
328
329
330 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
331 {
332         struct wpa_ie_data ie;
333         int pmksa_set = -1;
334         size_t i;
335
336         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
337             ie.pmkid == NULL)
338                 return;
339
340         for (i = 0; i < ie.num_pmkid; i++) {
341                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
342                                                     ie.pmkid + i * PMKID_LEN,
343                                                     NULL, NULL, 0, NULL, 0);
344                 if (pmksa_set == 0) {
345                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
346                         break;
347                 }
348         }
349
350         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
351                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
352 }
353
354
355 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
356                                                  union wpa_event_data *data)
357 {
358         if (data == NULL) {
359                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
360                         "event");
361                 return;
362         }
363         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
364                 " index=%d preauth=%d",
365                 MAC2STR(data->pmkid_candidate.bssid),
366                 data->pmkid_candidate.index,
367                 data->pmkid_candidate.preauth);
368
369         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
370                             data->pmkid_candidate.index,
371                             data->pmkid_candidate.preauth);
372 }
373
374
375 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
376 {
377         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
378             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
379                 return 0;
380
381 #ifdef IEEE8021X_EAPOL
382         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
383             wpa_s->current_ssid &&
384             !(wpa_s->current_ssid->eapol_flags &
385               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
386                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
387                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
388                  * plaintext or static WEP keys). */
389                 return 0;
390         }
391 #endif /* IEEE8021X_EAPOL */
392
393         return 1;
394 }
395
396
397 /**
398  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
399  * @wpa_s: pointer to wpa_supplicant data
400  * @ssid: Configuration data for the network
401  * Returns: 0 on success, -1 on failure
402  *
403  * This function is called when starting authentication with a network that is
404  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
405  */
406 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
407                               struct wpa_ssid *ssid)
408 {
409 #ifdef IEEE8021X_EAPOL
410 #ifdef PCSC_FUNCS
411         int aka = 0, sim = 0;
412
413         if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
414             wpa_s->scard != NULL || wpa_s->conf->external_sim)
415                 return 0;
416
417         if (ssid == NULL || ssid->eap.eap_methods == NULL) {
418                 sim = 1;
419                 aka = 1;
420         } else {
421                 struct eap_method_type *eap = ssid->eap.eap_methods;
422                 while (eap->vendor != EAP_VENDOR_IETF ||
423                        eap->method != EAP_TYPE_NONE) {
424                         if (eap->vendor == EAP_VENDOR_IETF) {
425                                 if (eap->method == EAP_TYPE_SIM)
426                                         sim = 1;
427                                 else if (eap->method == EAP_TYPE_AKA ||
428                                          eap->method == EAP_TYPE_AKA_PRIME)
429                                         aka = 1;
430                         }
431                         eap++;
432                 }
433         }
434
435         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
436                 sim = 0;
437         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
438             eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
439             NULL)
440                 aka = 0;
441
442         if (!sim && !aka) {
443                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
444                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
445                         "enabled");
446                 return 0;
447         }
448
449         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
450                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
451
452         wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
453         if (wpa_s->scard == NULL) {
454                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
455                         "(pcsc-lite)");
456                 return -1;
457         }
458         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
459         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
460 #endif /* PCSC_FUNCS */
461 #endif /* IEEE8021X_EAPOL */
462
463         return 0;
464 }
465
466
467 #ifndef CONFIG_NO_SCAN_PROCESSING
468
469 static int has_wep_key(struct wpa_ssid *ssid)
470 {
471         int i;
472
473         for (i = 0; i < NUM_WEP_KEYS; i++) {
474                 if (ssid->wep_key_len[i])
475                         return 1;
476         }
477
478         return 0;
479 }
480
481
482 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
483                                         struct wpa_ssid *ssid)
484 {
485         int privacy = 0;
486
487         if (ssid->mixed_cell)
488                 return 1;
489
490 #ifdef CONFIG_WPS
491         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
492                 return 1;
493 #endif /* CONFIG_WPS */
494
495 #ifdef CONFIG_OWE
496         if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only)
497                 return 1;
498 #endif /* CONFIG_OWE */
499
500         if (has_wep_key(ssid))
501                 privacy = 1;
502
503 #ifdef IEEE8021X_EAPOL
504         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
505             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
506                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
507                 privacy = 1;
508 #endif /* IEEE8021X_EAPOL */
509
510         if (wpa_key_mgmt_wpa(ssid->key_mgmt))
511                 privacy = 1;
512
513         if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
514                 privacy = 1;
515
516         if (bss->caps & IEEE80211_CAP_PRIVACY)
517                 return privacy;
518         return !privacy;
519 }
520
521
522 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
523                                          struct wpa_ssid *ssid,
524                                          struct wpa_bss *bss, int debug_print)
525 {
526         struct wpa_ie_data ie;
527         int proto_match = 0;
528         const u8 *rsn_ie, *wpa_ie;
529         int ret;
530         int wep_ok;
531
532         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
533         if (ret >= 0)
534                 return ret;
535
536         /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
537         wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
538                 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
539                   ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
540                  (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
541
542         rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
543         while ((ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)) && rsn_ie) {
544                 proto_match++;
545
546                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
547                         if (debug_print)
548                                 wpa_dbg(wpa_s, MSG_DEBUG,
549                                         "   skip RSN IE - parse failed");
550                         break;
551                 }
552
553                 if (wep_ok &&
554                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
555                 {
556                         if (debug_print)
557                                 wpa_dbg(wpa_s, MSG_DEBUG,
558                                         "   selected based on TSN in RSN IE");
559                         return 1;
560                 }
561
562                 if (!(ie.proto & ssid->proto) &&
563                     !(ssid->proto & WPA_PROTO_OSEN)) {
564                         if (debug_print)
565                                 wpa_dbg(wpa_s, MSG_DEBUG,
566                                         "   skip RSN IE - proto mismatch");
567                         break;
568                 }
569
570                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
571                         if (debug_print)
572                                 wpa_dbg(wpa_s, MSG_DEBUG,
573                                         "   skip RSN IE - PTK cipher mismatch");
574                         break;
575                 }
576
577                 if (!(ie.group_cipher & ssid->group_cipher)) {
578                         if (debug_print)
579                                 wpa_dbg(wpa_s, MSG_DEBUG,
580                                         "   skip RSN IE - GTK cipher mismatch");
581                         break;
582                 }
583
584                 if (ssid->group_mgmt_cipher &&
585                     !(ie.mgmt_group_cipher & ssid->group_mgmt_cipher)) {
586                         if (debug_print)
587                                 wpa_dbg(wpa_s, MSG_DEBUG,
588                                         "   skip RSN IE - group mgmt cipher mismatch");
589                         break;
590                 }
591
592                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
593                         if (debug_print)
594                                 wpa_dbg(wpa_s, MSG_DEBUG,
595                                         "   skip RSN IE - key mgmt mismatch");
596                         break;
597                 }
598
599 #ifdef CONFIG_IEEE80211W
600                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
601                     wpas_get_ssid_pmf(wpa_s, ssid) ==
602                     MGMT_FRAME_PROTECTION_REQUIRED) {
603                         if (debug_print)
604                                 wpa_dbg(wpa_s, MSG_DEBUG,
605                                         "   skip RSN IE - no mgmt frame protection");
606                         break;
607                 }
608 #endif /* CONFIG_IEEE80211W */
609                 if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
610                     wpas_get_ssid_pmf(wpa_s, ssid) ==
611                     NO_MGMT_FRAME_PROTECTION) {
612                         if (debug_print)
613                                 wpa_dbg(wpa_s, MSG_DEBUG,
614                                         "   skip RSN IE - no mgmt frame protection enabled but AP requires it");
615                         break;
616                 }
617 #ifdef CONFIG_MBO
618                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
619                     wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND) &&
620                     wpas_get_ssid_pmf(wpa_s, ssid) !=
621                     NO_MGMT_FRAME_PROTECTION) {
622                         if (debug_print)
623                                 wpa_dbg(wpa_s, MSG_DEBUG,
624                                         "   skip RSN IE - no mgmt frame protection enabled on MBO AP");
625                         break;
626                 }
627 #endif /* CONFIG_MBO */
628
629                 if (debug_print)
630                         wpa_dbg(wpa_s, MSG_DEBUG,
631                                 "   selected based on RSN IE");
632                 return 1;
633         }
634
635 #ifdef CONFIG_IEEE80211W
636         if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED &&
637             (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE) || ssid->owe_only)) {
638                 if (debug_print)
639                         wpa_dbg(wpa_s, MSG_DEBUG,
640                                 "   skip - MFP Required but network not MFP Capable");
641                 return 0;
642         }
643 #endif /* CONFIG_IEEE80211W */
644
645         wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
646         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
647                 proto_match++;
648
649                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
650                         if (debug_print)
651                                 wpa_dbg(wpa_s, MSG_DEBUG,
652                                         "   skip WPA IE - parse failed");
653                         break;
654                 }
655
656                 if (wep_ok &&
657                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
658                 {
659                         if (debug_print)
660                                 wpa_dbg(wpa_s, MSG_DEBUG,
661                                         "   selected based on TSN in WPA IE");
662                         return 1;
663                 }
664
665                 if (!(ie.proto & ssid->proto)) {
666                         if (debug_print)
667                                 wpa_dbg(wpa_s, MSG_DEBUG,
668                                         "   skip WPA IE - proto mismatch");
669                         break;
670                 }
671
672                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
673                         if (debug_print)
674                                 wpa_dbg(wpa_s, MSG_DEBUG,
675                                         "   skip WPA IE - PTK cipher mismatch");
676                         break;
677                 }
678
679                 if (!(ie.group_cipher & ssid->group_cipher)) {
680                         if (debug_print)
681                                 wpa_dbg(wpa_s, MSG_DEBUG,
682                                         "   skip WPA IE - GTK cipher mismatch");
683                         break;
684                 }
685
686                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
687                         if (debug_print)
688                                 wpa_dbg(wpa_s, MSG_DEBUG,
689                                         "   skip WPA IE - key mgmt mismatch");
690                         break;
691                 }
692
693                 if (debug_print)
694                         wpa_dbg(wpa_s, MSG_DEBUG,
695                                 "   selected based on WPA IE");
696                 return 1;
697         }
698
699         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
700             !rsn_ie) {
701                 if (debug_print)
702                         wpa_dbg(wpa_s, MSG_DEBUG,
703                                 "   allow for non-WPA IEEE 802.1X");
704                 return 1;
705         }
706
707 #ifdef CONFIG_OWE
708         if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only &&
709             !wpa_ie && !rsn_ie) {
710                 if (wpa_s->owe_transition_select &&
711                     wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE) &&
712                     ssid->owe_transition_bss_select_count + 1 <=
713                     MAX_OWE_TRANSITION_BSS_SELECT_COUNT) {
714                         ssid->owe_transition_bss_select_count++;
715                         if (debug_print)
716                                 wpa_dbg(wpa_s, MSG_DEBUG,
717                                         "   skip OWE transition BSS (selection count %d does not exceed %d)",
718                                         ssid->owe_transition_bss_select_count,
719                                         MAX_OWE_TRANSITION_BSS_SELECT_COUNT);
720                         wpa_s->owe_transition_search = 1;
721                         return 0;
722                 }
723                 if (debug_print)
724                         wpa_dbg(wpa_s, MSG_DEBUG,
725                                 "   allow in OWE transition mode");
726                 return 1;
727         }
728 #endif /* CONFIG_OWE */
729
730         if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
731             wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
732                 if (debug_print)
733                         wpa_dbg(wpa_s, MSG_DEBUG,
734                                 "   skip - no WPA/RSN proto match");
735                 return 0;
736         }
737
738         if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
739             wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
740                 if (debug_print)
741                         wpa_dbg(wpa_s, MSG_DEBUG, "   allow in OSEN");
742                 return 1;
743         }
744
745         if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
746                 if (debug_print)
747                         wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
748                 return 1;
749         }
750
751         if (debug_print)
752                 wpa_dbg(wpa_s, MSG_DEBUG,
753                         "   reject due to mismatch with WPA/WPA2");
754
755         return 0;
756 }
757
758
759 static int freq_allowed(int *freqs, int freq)
760 {
761         int i;
762
763         if (freqs == NULL)
764                 return 1;
765
766         for (i = 0; freqs[i]; i++)
767                 if (freqs[i] == freq)
768                         return 1;
769         return 0;
770 }
771
772
773 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
774                       int debug_print)
775 {
776         const struct hostapd_hw_modes *mode = NULL, *modes;
777         const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
778         const u8 *rate_ie;
779         int i, j, k;
780
781         if (bss->freq == 0)
782                 return 1; /* Cannot do matching without knowing band */
783
784         modes = wpa_s->hw.modes;
785         if (modes == NULL) {
786                 /*
787                  * The driver does not provide any additional information
788                  * about the utilized hardware, so allow the connection attempt
789                  * to continue.
790                  */
791                 return 1;
792         }
793
794         for (i = 0; i < wpa_s->hw.num_modes; i++) {
795                 for (j = 0; j < modes[i].num_channels; j++) {
796                         int freq = modes[i].channels[j].freq;
797                         if (freq == bss->freq) {
798                                 if (mode &&
799                                     mode->mode == HOSTAPD_MODE_IEEE80211G)
800                                         break; /* do not allow 802.11b replace
801                                                 * 802.11g */
802                                 mode = &modes[i];
803                                 break;
804                         }
805                 }
806         }
807
808         if (mode == NULL)
809                 return 0;
810
811         for (i = 0; i < (int) sizeof(scan_ie); i++) {
812                 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
813                 if (rate_ie == NULL)
814                         continue;
815
816                 for (j = 2; j < rate_ie[1] + 2; j++) {
817                         int flagged = !!(rate_ie[j] & 0x80);
818                         int r = (rate_ie[j] & 0x7f) * 5;
819
820                         /*
821                          * IEEE Std 802.11n-2009 7.3.2.2:
822                          * The new BSS Membership selector value is encoded
823                          * like a legacy basic rate, but it is not a rate and
824                          * only indicates if the BSS members are required to
825                          * support the mandatory features of Clause 20 [HT PHY]
826                          * in order to join the BSS.
827                          */
828                         if (flagged && ((rate_ie[j] & 0x7f) ==
829                                         BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
830                                 if (!ht_supported(mode)) {
831                                         if (debug_print)
832                                                 wpa_dbg(wpa_s, MSG_DEBUG,
833                                                         "   hardware does not support HT PHY");
834                                         return 0;
835                                 }
836                                 continue;
837                         }
838
839                         /* There's also a VHT selector for 802.11ac */
840                         if (flagged && ((rate_ie[j] & 0x7f) ==
841                                         BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
842                                 if (!vht_supported(mode)) {
843                                         if (debug_print)
844                                                 wpa_dbg(wpa_s, MSG_DEBUG,
845                                                         "   hardware does not support VHT PHY");
846                                         return 0;
847                                 }
848                                 continue;
849                         }
850
851                         if (!flagged)
852                                 continue;
853
854                         /* check for legacy basic rates */
855                         for (k = 0; k < mode->num_rates; k++) {
856                                 if (mode->rates[k] == r)
857                                         break;
858                         }
859                         if (k == mode->num_rates) {
860                                 /*
861                                  * IEEE Std 802.11-2007 7.3.2.2 demands that in
862                                  * order to join a BSS all required rates
863                                  * have to be supported by the hardware.
864                                  */
865                                 if (debug_print)
866                                         wpa_dbg(wpa_s, MSG_DEBUG,
867                                                 "   hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
868                                                 r / 10, r % 10,
869                                                 bss->freq, mode->mode, mode->num_rates);
870                                 return 0;
871                         }
872                 }
873         }
874
875         return 1;
876 }
877
878
879 /*
880  * Test whether BSS is in an ESS.
881  * This is done differently in DMG (60 GHz) and non-DMG bands
882  */
883 static int bss_is_ess(struct wpa_bss *bss)
884 {
885         if (bss_is_dmg(bss)) {
886                 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
887                         IEEE80211_CAP_DMG_AP;
888         }
889
890         return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
891                 IEEE80211_CAP_ESS);
892 }
893
894
895 static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
896 {
897         size_t i;
898
899         for (i = 0; i < ETH_ALEN; i++) {
900                 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
901                         return 0;
902         }
903         return 1;
904 }
905
906
907 static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
908 {
909         size_t i;
910
911         for (i = 0; i < num; i++) {
912                 const u8 *a = list + i * ETH_ALEN * 2;
913                 const u8 *m = a + ETH_ALEN;
914
915                 if (match_mac_mask(a, addr, m))
916                         return 1;
917         }
918         return 0;
919 }
920
921
922 static void owe_trans_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
923                            const u8 **ret_ssid, size_t *ret_ssid_len)
924 {
925 #ifdef CONFIG_OWE
926         const u8 *owe, *pos, *end, *bssid;
927         u8 ssid_len;
928         struct wpa_bss *open_bss;
929
930         owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
931         if (!owe || !wpa_bss_get_ie(bss, WLAN_EID_RSN))
932                 return;
933
934         pos = owe + 6;
935         end = owe + 2 + owe[1];
936
937         if (end - pos < ETH_ALEN + 1)
938                 return;
939         bssid = pos;
940         pos += ETH_ALEN;
941         ssid_len = *pos++;
942         if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
943                 return;
944
945         /* Match the profile SSID against the OWE transition mode SSID on the
946          * open network. */
947         wpa_dbg(wpa_s, MSG_DEBUG, "OWE: transition mode BSSID: " MACSTR
948                 " SSID: %s", MAC2STR(bssid), wpa_ssid_txt(pos, ssid_len));
949         *ret_ssid = pos;
950         *ret_ssid_len = ssid_len;
951
952         if (bss->ssid_len > 0)
953                 return;
954
955         open_bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
956         if (!open_bss)
957                 return;
958         if (ssid_len != open_bss->ssid_len ||
959             os_memcmp(pos, open_bss->ssid, ssid_len) != 0) {
960                 wpa_dbg(wpa_s, MSG_DEBUG,
961                         "OWE: transition mode SSID mismatch: %s",
962                         wpa_ssid_txt(open_bss->ssid, open_bss->ssid_len));
963                 return;
964         }
965
966         owe = wpa_bss_get_vendor_ie(open_bss, OWE_IE_VENDOR_TYPE);
967         if (!owe || wpa_bss_get_ie(open_bss, WLAN_EID_RSN)) {
968                 wpa_dbg(wpa_s, MSG_DEBUG,
969                         "OWE: transition mode open BSS unexpected info");
970                 return;
971         }
972
973         pos = owe + 6;
974         end = owe + 2 + owe[1];
975
976         if (end - pos < ETH_ALEN + 1)
977                 return;
978         if (os_memcmp(pos, bss->bssid, ETH_ALEN) != 0) {
979                 wpa_dbg(wpa_s, MSG_DEBUG,
980                         "OWE: transition mode BSSID mismatch: " MACSTR,
981                         MAC2STR(pos));
982                 return;
983         }
984         pos += ETH_ALEN;
985         ssid_len = *pos++;
986         if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
987                 return;
988         wpa_dbg(wpa_s, MSG_DEBUG, "OWE: learned transition mode OWE SSID: %s",
989                 wpa_ssid_txt(pos, ssid_len));
990         os_memcpy(bss->ssid, pos, ssid_len);
991         bss->ssid_len = ssid_len;
992 #endif /* CONFIG_OWE */
993 }
994
995
996 struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
997                                      int i, struct wpa_bss *bss,
998                                      struct wpa_ssid *group,
999                                      int only_first_ssid, int debug_print)
1000 {
1001         u8 wpa_ie_len, rsn_ie_len;
1002         int wpa;
1003         struct wpa_blacklist *e;
1004         const u8 *ie;
1005         struct wpa_ssid *ssid;
1006         int osen, rsn_osen = 0;
1007 #ifdef CONFIG_MBO
1008         const u8 *assoc_disallow;
1009 #endif /* CONFIG_MBO */
1010         const u8 *match_ssid;
1011         size_t match_ssid_len;
1012         struct wpa_ie_data data;
1013
1014         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1015         wpa_ie_len = ie ? ie[1] : 0;
1016
1017         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1018         rsn_ie_len = ie ? ie[1] : 0;
1019         if (ie && wpa_parse_wpa_ie_rsn(ie, 2 + ie[1], &data) == 0 &&
1020             (data.key_mgmt & WPA_KEY_MGMT_OSEN))
1021                 rsn_osen = 1;
1022
1023         ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
1024         osen = ie != NULL;
1025
1026         if (debug_print) {
1027                 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
1028                         " ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
1029                         i, MAC2STR(bss->bssid),
1030                         wpa_ssid_txt(bss->ssid, bss->ssid_len),
1031                         wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
1032                         bss->freq,
1033                         wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
1034                         " wps" : "",
1035                         (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
1036                          wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
1037                         ? " p2p" : "",
1038                         osen ? " osen=1" : "");
1039         }
1040
1041         e = wpa_blacklist_get(wpa_s, bss->bssid);
1042         if (e) {
1043                 int limit = 1;
1044                 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
1045                         /*
1046                          * When only a single network is enabled, we can
1047                          * trigger blacklisting on the first failure. This
1048                          * should not be done with multiple enabled networks to
1049                          * avoid getting forced to move into a worse ESS on
1050                          * single error if there are no other BSSes of the
1051                          * current ESS.
1052                          */
1053                         limit = 0;
1054                 }
1055                 if (e->count > limit) {
1056                         if (debug_print) {
1057                                 wpa_dbg(wpa_s, MSG_DEBUG,
1058                                         "   skip - blacklisted (count=%d limit=%d)",
1059                                         e->count, limit);
1060                         }
1061                         return NULL;
1062                 }
1063         }
1064
1065         match_ssid = bss->ssid;
1066         match_ssid_len = bss->ssid_len;
1067         owe_trans_ssid(wpa_s, bss, &match_ssid, &match_ssid_len);
1068
1069         if (match_ssid_len == 0) {
1070                 if (debug_print)
1071                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
1072                 return NULL;
1073         }
1074
1075         if (disallowed_bssid(wpa_s, bss->bssid)) {
1076                 if (debug_print)
1077                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
1078                 return NULL;
1079         }
1080
1081         if (disallowed_ssid(wpa_s, match_ssid, match_ssid_len)) {
1082                 if (debug_print)
1083                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
1084                 return NULL;
1085         }
1086
1087         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
1088
1089         for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
1090                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
1091                 int res;
1092
1093                 if (wpas_network_disabled(wpa_s, ssid)) {
1094                         if (debug_print)
1095                                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
1096                         continue;
1097                 }
1098
1099                 res = wpas_temp_disabled(wpa_s, ssid);
1100                 if (res > 0) {
1101                         if (debug_print)
1102                                 wpa_dbg(wpa_s, MSG_DEBUG,
1103                                         "   skip - disabled temporarily for %d second(s)",
1104                                         res);
1105                         continue;
1106                 }
1107
1108 #ifdef CONFIG_WPS
1109                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
1110                         if (debug_print)
1111                                 wpa_dbg(wpa_s, MSG_DEBUG,
1112                                         "   skip - blacklisted (WPS)");
1113                         continue;
1114                 }
1115
1116                 if (wpa && ssid->ssid_len == 0 &&
1117                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
1118                         check_ssid = 0;
1119
1120                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1121                         /* Only allow wildcard SSID match if an AP
1122                          * advertises active WPS operation that matches
1123                          * with our mode. */
1124                         check_ssid = 1;
1125                         if (ssid->ssid_len == 0 &&
1126                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
1127                                 check_ssid = 0;
1128                 }
1129 #endif /* CONFIG_WPS */
1130
1131                 if (ssid->bssid_set && ssid->ssid_len == 0 &&
1132                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
1133                         check_ssid = 0;
1134
1135                 if (check_ssid &&
1136                     (match_ssid_len != ssid->ssid_len ||
1137                      os_memcmp(match_ssid, ssid->ssid, match_ssid_len) != 0)) {
1138                         if (debug_print)
1139                                 wpa_dbg(wpa_s, MSG_DEBUG,
1140                                         "   skip - SSID mismatch");
1141                         continue;
1142                 }
1143
1144                 if (ssid->bssid_set &&
1145                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
1146                         if (debug_print)
1147                                 wpa_dbg(wpa_s, MSG_DEBUG,
1148                                         "   skip - BSSID mismatch");
1149                         continue;
1150                 }
1151
1152                 /* check blacklist */
1153                 if (ssid->num_bssid_blacklist &&
1154                     addr_in_list(bss->bssid, ssid->bssid_blacklist,
1155                                  ssid->num_bssid_blacklist)) {
1156                         if (debug_print)
1157                                 wpa_dbg(wpa_s, MSG_DEBUG,
1158                                         "   skip - BSSID blacklisted");
1159                         continue;
1160                 }
1161
1162                 /* if there is a whitelist, only accept those APs */
1163                 if (ssid->num_bssid_whitelist &&
1164                     !addr_in_list(bss->bssid, ssid->bssid_whitelist,
1165                                   ssid->num_bssid_whitelist)) {
1166                         if (debug_print)
1167                                 wpa_dbg(wpa_s, MSG_DEBUG,
1168                                         "   skip - BSSID not in whitelist");
1169                         continue;
1170                 }
1171
1172                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss,
1173                                                    debug_print))
1174                         continue;
1175
1176                 if (!osen && !wpa &&
1177                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
1178                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
1179                     !(ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1180                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
1181                         if (debug_print)
1182                                 wpa_dbg(wpa_s, MSG_DEBUG,
1183                                         "   skip - non-WPA network not allowed");
1184                         continue;
1185                 }
1186
1187                 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
1188                     has_wep_key(ssid)) {
1189                         if (debug_print)
1190                                 wpa_dbg(wpa_s, MSG_DEBUG,
1191                                         "   skip - ignore WPA/WPA2 AP for WEP network block");
1192                         continue;
1193                 }
1194
1195                 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen &&
1196                     !rsn_osen) {
1197                         if (debug_print)
1198                                 wpa_dbg(wpa_s, MSG_DEBUG,
1199                                         "   skip - non-OSEN network not allowed");
1200                         continue;
1201                 }
1202
1203                 if (!wpa_supplicant_match_privacy(bss, ssid)) {
1204                         if (debug_print)
1205                                 wpa_dbg(wpa_s, MSG_DEBUG,
1206                                         "   skip - privacy mismatch");
1207                         continue;
1208                 }
1209
1210                 if (ssid->mode != IEEE80211_MODE_MESH && !bss_is_ess(bss) &&
1211                     !bss_is_pbss(bss)) {
1212                         if (debug_print)
1213                                 wpa_dbg(wpa_s, MSG_DEBUG,
1214                                         "   skip - not ESS, PBSS, or MBSS");
1215                         continue;
1216                 }
1217
1218                 if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
1219                         if (debug_print)
1220                                 wpa_dbg(wpa_s, MSG_DEBUG,
1221                                         "   skip - PBSS mismatch (ssid %d bss %d)",
1222                                         ssid->pbss, bss_is_pbss(bss));
1223                         continue;
1224                 }
1225
1226                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
1227                         if (debug_print)
1228                                 wpa_dbg(wpa_s, MSG_DEBUG,
1229                                         "   skip - frequency not allowed");
1230                         continue;
1231                 }
1232
1233 #ifdef CONFIG_MESH
1234                 if (ssid->mode == IEEE80211_MODE_MESH && ssid->frequency > 0 &&
1235                     ssid->frequency != bss->freq) {
1236                         if (debug_print)
1237                                 wpa_dbg(wpa_s, MSG_DEBUG,
1238                                         "   skip - frequency not allowed (mesh)");
1239                         continue;
1240                 }
1241 #endif /* CONFIG_MESH */
1242
1243                 if (!rate_match(wpa_s, bss, debug_print)) {
1244                         if (debug_print)
1245                                 wpa_dbg(wpa_s, MSG_DEBUG,
1246                                         "   skip - rate sets do not match");
1247                         continue;
1248                 }
1249
1250 #ifndef CONFIG_IBSS_RSN
1251                 if (ssid->mode == WPAS_MODE_IBSS &&
1252                     !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1253                                         WPA_KEY_MGMT_WPA_NONE))) {
1254                         if (debug_print)
1255                                 wpa_dbg(wpa_s, MSG_DEBUG,
1256                                         "   skip - IBSS RSN not supported in the build");
1257                         continue;
1258                 }
1259 #endif /* !CONFIG_IBSS_RSN */
1260
1261 #ifdef CONFIG_P2P
1262                 if (ssid->p2p_group &&
1263                     !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1264                     !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1265                         if (debug_print)
1266                                 wpa_dbg(wpa_s, MSG_DEBUG,
1267                                         "   skip - no P2P IE seen");
1268                         continue;
1269                 }
1270
1271                 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1272                         struct wpabuf *p2p_ie;
1273                         u8 dev_addr[ETH_ALEN];
1274
1275                         ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1276                         if (ie == NULL) {
1277                                 if (debug_print)
1278                                         wpa_dbg(wpa_s, MSG_DEBUG,
1279                                                 "   skip - no P2P element");
1280                                 continue;
1281                         }
1282                         p2p_ie = wpa_bss_get_vendor_ie_multi(
1283                                 bss, P2P_IE_VENDOR_TYPE);
1284                         if (p2p_ie == NULL) {
1285                                 if (debug_print)
1286                                         wpa_dbg(wpa_s, MSG_DEBUG,
1287                                                 "   skip - could not fetch P2P element");
1288                                 continue;
1289                         }
1290
1291                         if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1292                             || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1293                                          ETH_ALEN) != 0) {
1294                                 if (debug_print)
1295                                         wpa_dbg(wpa_s, MSG_DEBUG,
1296                                                 "   skip - no matching GO P2P Device Address in P2P element");
1297                                 wpabuf_free(p2p_ie);
1298                                 continue;
1299                         }
1300                         wpabuf_free(p2p_ie);
1301                 }
1302
1303                 /*
1304                  * TODO: skip the AP if its P2P IE has Group Formation
1305                  * bit set in the P2P Group Capability Bitmap and we
1306                  * are not in Group Formation with that device.
1307                  */
1308 #endif /* CONFIG_P2P */
1309
1310                 if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1311                 {
1312                         struct os_reltime diff;
1313
1314                         os_reltime_sub(&wpa_s->scan_min_time,
1315                                        &bss->last_update, &diff);
1316                         if (debug_print)
1317                                 wpa_dbg(wpa_s, MSG_DEBUG,
1318                                         "   skip - scan result not recent enough (%u.%06u seconds too old)",
1319                                 (unsigned int) diff.sec,
1320                                 (unsigned int) diff.usec);
1321                         continue;
1322                 }
1323 #ifdef CONFIG_MBO
1324 #ifdef CONFIG_TESTING_OPTIONS
1325                 if (wpa_s->ignore_assoc_disallow)
1326                         goto skip_assoc_disallow;
1327 #endif /* CONFIG_TESTING_OPTIONS */
1328                 assoc_disallow = wpas_mbo_get_bss_attr(
1329                         bss, MBO_ATTR_ID_ASSOC_DISALLOW);
1330                 if (assoc_disallow && assoc_disallow[1] >= 1) {
1331                         if (debug_print)
1332                                 wpa_dbg(wpa_s, MSG_DEBUG,
1333                                         "   skip - MBO association disallowed (reason %u)",
1334                                 assoc_disallow[2]);
1335                         continue;
1336                 }
1337
1338                 if (wpa_is_bss_tmp_disallowed(wpa_s, bss->bssid)) {
1339                         if (debug_print)
1340                                 wpa_dbg(wpa_s, MSG_DEBUG,
1341                                         "   skip - MBO retry delay has not passed yet");
1342                         continue;
1343                 }
1344 #ifdef CONFIG_TESTING_OPTIONS
1345         skip_assoc_disallow:
1346 #endif /* CONFIG_TESTING_OPTIONS */
1347 #endif /* CONFIG_MBO */
1348
1349 #ifdef CONFIG_DPP
1350                 if ((ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
1351                     !wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid) &&
1352                     (!ssid->dpp_connector ||
1353                      !ssid->dpp_netaccesskey ||
1354                      !ssid->dpp_csign)) {
1355                         if (debug_print)
1356                                 wpa_dbg(wpa_s, MSG_DEBUG,
1357                                         "   skip - no PMKSA entry for DPP");
1358                         continue;
1359                 }
1360 #endif /* CONFIG_DPP */
1361
1362                 /* Matching configuration found */
1363                 return ssid;
1364         }
1365
1366         /* No matching configuration found */
1367         return NULL;
1368 }
1369
1370
1371 static struct wpa_bss *
1372 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
1373                           struct wpa_ssid *group,
1374                           struct wpa_ssid **selected_ssid,
1375                           int only_first_ssid)
1376 {
1377         unsigned int i;
1378
1379         if (wpa_s->current_ssid) {
1380                 struct wpa_ssid *ssid;
1381
1382                 wpa_dbg(wpa_s, MSG_DEBUG,
1383                         "Scan results matching the currently selected network");
1384                 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1385                         struct wpa_bss *bss = wpa_s->last_scan_res[i];
1386
1387                         ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1388                                                   only_first_ssid, 0);
1389                         if (ssid != wpa_s->current_ssid)
1390                                 continue;
1391                         wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
1392                                 " freq=%d level=%d snr=%d est_throughput=%u",
1393                                 i, MAC2STR(bss->bssid), bss->freq, bss->level,
1394                                 bss->snr, bss->est_throughput);
1395                 }
1396         }
1397
1398         if (only_first_ssid)
1399                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1400                         group->id);
1401         else
1402                 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1403                         group->priority);
1404
1405         for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1406                 struct wpa_bss *bss = wpa_s->last_scan_res[i];
1407
1408                 wpa_s->owe_transition_select = 1;
1409                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1410                                                     only_first_ssid, 1);
1411                 wpa_s->owe_transition_select = 0;
1412                 if (!*selected_ssid)
1413                         continue;
1414                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
1415                         " ssid='%s'",
1416                         MAC2STR(bss->bssid),
1417                         wpa_ssid_txt(bss->ssid, bss->ssid_len));
1418                 return bss;
1419         }
1420
1421         return NULL;
1422 }
1423
1424
1425 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1426                                              struct wpa_ssid **selected_ssid)
1427 {
1428         struct wpa_bss *selected = NULL;
1429         int prio;
1430         struct wpa_ssid *next_ssid = NULL;
1431         struct wpa_ssid *ssid;
1432
1433         if (wpa_s->last_scan_res == NULL ||
1434             wpa_s->last_scan_res_used == 0)
1435                 return NULL; /* no scan results from last update */
1436
1437         if (wpa_s->next_ssid) {
1438                 /* check that next_ssid is still valid */
1439                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1440                         if (ssid == wpa_s->next_ssid)
1441                                 break;
1442                 }
1443                 next_ssid = ssid;
1444                 wpa_s->next_ssid = NULL;
1445         }
1446
1447         while (selected == NULL) {
1448                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1449                         if (next_ssid && next_ssid->priority ==
1450                             wpa_s->conf->pssid[prio]->priority) {
1451                                 selected = wpa_supplicant_select_bss(
1452                                         wpa_s, next_ssid, selected_ssid, 1);
1453                                 if (selected)
1454                                         break;
1455                         }
1456                         selected = wpa_supplicant_select_bss(
1457                                 wpa_s, wpa_s->conf->pssid[prio],
1458                                 selected_ssid, 0);
1459                         if (selected)
1460                                 break;
1461                 }
1462
1463                 if (selected == NULL && wpa_s->blacklist &&
1464                     !wpa_s->countermeasures) {
1465                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1466                                 "blacklist and try again");
1467                         wpa_blacklist_clear(wpa_s);
1468                         wpa_s->blacklist_cleared++;
1469                 } else if (selected == NULL)
1470                         break;
1471         }
1472
1473         ssid = *selected_ssid;
1474         if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1475             !ssid->passphrase && !ssid->ext_psk) {
1476                 const char *field_name, *txt = NULL;
1477
1478                 wpa_dbg(wpa_s, MSG_DEBUG,
1479                         "PSK/passphrase not yet available for the selected network");
1480
1481                 wpas_notify_network_request(wpa_s, ssid,
1482                                             WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1483
1484                 field_name = wpa_supplicant_ctrl_req_to_string(
1485                         WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1486                 if (field_name == NULL)
1487                         return NULL;
1488
1489                 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1490
1491                 selected = NULL;
1492         }
1493
1494         return selected;
1495 }
1496
1497
1498 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1499                                         int timeout_sec, int timeout_usec)
1500 {
1501         if (!wpa_supplicant_enabled_networks(wpa_s)) {
1502                 /*
1503                  * No networks are enabled; short-circuit request so
1504                  * we don't wait timeout seconds before transitioning
1505                  * to INACTIVE state.
1506                  */
1507                 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1508                         "since there are no enabled networks");
1509                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1510                 return;
1511         }
1512
1513         wpa_s->scan_for_connection = 1;
1514         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1515 }
1516
1517
1518 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
1519                            struct wpa_bss *selected,
1520                            struct wpa_ssid *ssid)
1521 {
1522         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1523                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1524                         "PBC session overlap");
1525                 wpas_notify_wps_event_pbc_overlap(wpa_s);
1526 #ifdef CONFIG_P2P
1527                 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1528                     wpa_s->p2p_in_provisioning) {
1529                         eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1530                                                wpa_s, NULL);
1531                         return -1;
1532                 }
1533 #endif /* CONFIG_P2P */
1534
1535 #ifdef CONFIG_WPS
1536                 wpas_wps_pbc_overlap(wpa_s);
1537                 wpas_wps_cancel(wpa_s);
1538 #endif /* CONFIG_WPS */
1539                 return -1;
1540         }
1541
1542         wpa_msg(wpa_s, MSG_DEBUG,
1543                 "Considering connect request: reassociate: %d  selected: "
1544                 MACSTR "  bssid: " MACSTR "  pending: " MACSTR
1545                 "  wpa_state: %s  ssid=%p  current_ssid=%p",
1546                 wpa_s->reassociate, MAC2STR(selected->bssid),
1547                 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1548                 wpa_supplicant_state_txt(wpa_s->wpa_state),
1549                 ssid, wpa_s->current_ssid);
1550
1551         /*
1552          * Do not trigger new association unless the BSSID has changed or if
1553          * reassociation is requested. If we are in process of associating with
1554          * the selected BSSID, do not trigger new attempt.
1555          */
1556         if (wpa_s->reassociate ||
1557             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1558              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1559                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
1560               (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1561                os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1562                0) ||
1563               (is_zero_ether_addr(wpa_s->pending_bssid) &&
1564                ssid != wpa_s->current_ssid)))) {
1565                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1566                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
1567                         return 0;
1568                 }
1569                 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1570                         MAC2STR(selected->bssid));
1571                 wpa_supplicant_associate(wpa_s, selected, ssid);
1572         } else {
1573                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1574                         "connect with the selected AP");
1575         }
1576
1577         return 0;
1578 }
1579
1580
1581 static struct wpa_ssid *
1582 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1583 {
1584         int prio;
1585         struct wpa_ssid *ssid;
1586
1587         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1588                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1589                 {
1590                         if (wpas_network_disabled(wpa_s, ssid))
1591                                 continue;
1592 #ifndef CONFIG_IBSS_RSN
1593                         if (ssid->mode == WPAS_MODE_IBSS &&
1594                             !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1595                                                 WPA_KEY_MGMT_WPA_NONE))) {
1596                                 wpa_msg(wpa_s, MSG_INFO,
1597                                         "IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
1598                                         wpa_ssid_txt(ssid->ssid,
1599                                                      ssid->ssid_len));
1600                                 continue;
1601                         }
1602 #endif /* !CONFIG_IBSS_RSN */
1603                         if (ssid->mode == IEEE80211_MODE_IBSS ||
1604                             ssid->mode == IEEE80211_MODE_AP ||
1605                             ssid->mode == IEEE80211_MODE_MESH)
1606                                 return ssid;
1607                 }
1608         }
1609         return NULL;
1610 }
1611
1612
1613 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1614  * on BSS added and BSS changed events */
1615 static void wpa_supplicant_rsn_preauth_scan_results(
1616         struct wpa_supplicant *wpa_s)
1617 {
1618         struct wpa_bss *bss;
1619
1620         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1621                 return;
1622
1623         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1624                 const u8 *ssid, *rsn;
1625
1626                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1627                 if (ssid == NULL)
1628                         continue;
1629
1630                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1631                 if (rsn == NULL)
1632                         continue;
1633
1634                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
1635         }
1636
1637 }
1638
1639
1640 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1641                                        struct wpa_bss *selected,
1642                                        struct wpa_ssid *ssid)
1643 {
1644         struct wpa_bss *current_bss = NULL;
1645 #ifndef CONFIG_NO_ROAMING
1646         int min_diff, diff;
1647         int to_5ghz;
1648         int cur_est, sel_est;
1649 #endif /* CONFIG_NO_ROAMING */
1650
1651         if (wpa_s->reassociate)
1652                 return 1; /* explicit request to reassociate */
1653         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1654                 return 1; /* we are not associated; continue */
1655         if (wpa_s->current_ssid == NULL)
1656                 return 1; /* unknown current SSID */
1657         if (wpa_s->current_ssid != ssid)
1658                 return 1; /* different network block */
1659
1660         if (wpas_driver_bss_selection(wpa_s))
1661                 return 0; /* Driver-based roaming */
1662
1663         if (wpa_s->current_ssid->ssid)
1664                 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1665                                           wpa_s->current_ssid->ssid,
1666                                           wpa_s->current_ssid->ssid_len);
1667         if (!current_bss)
1668                 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1669
1670         if (!current_bss)
1671                 return 1; /* current BSS not seen in scan results */
1672
1673         if (current_bss == selected)
1674                 return 0;
1675
1676         if (selected->last_update_idx > current_bss->last_update_idx)
1677                 return 1; /* current BSS not seen in the last scan */
1678
1679 #ifndef CONFIG_NO_ROAMING
1680         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1681         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1682                 " freq=%d level=%d snr=%d est_throughput=%u",
1683                 MAC2STR(current_bss->bssid),
1684                 current_bss->freq, current_bss->level,
1685                 current_bss->snr, current_bss->est_throughput);
1686         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1687                 " freq=%d level=%d snr=%d est_throughput=%u",
1688                 MAC2STR(selected->bssid), selected->freq, selected->level,
1689                 selected->snr, selected->est_throughput);
1690
1691         if (wpa_s->current_ssid->bssid_set &&
1692             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1693             0) {
1694                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1695                         "has preferred BSSID");
1696                 return 1;
1697         }
1698
1699         if (selected->est_throughput > current_bss->est_throughput + 5000) {
1700                 wpa_dbg(wpa_s, MSG_DEBUG,
1701                         "Allow reassociation - selected BSS has better estimated throughput");
1702                 return 1;
1703         }
1704
1705         to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
1706
1707         if (current_bss->level < 0 &&
1708             current_bss->level > selected->level + to_5ghz * 2) {
1709                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1710                         "signal level");
1711                 return 0;
1712         }
1713
1714         if (current_bss->est_throughput > selected->est_throughput + 5000) {
1715                 wpa_dbg(wpa_s, MSG_DEBUG,
1716                         "Skip roam - Current BSS has better estimated throughput");
1717                 return 0;
1718         }
1719
1720         cur_est = current_bss->est_throughput;
1721         sel_est = selected->est_throughput;
1722         min_diff = 2;
1723         if (current_bss->level < 0) {
1724                 if (current_bss->level < -85)
1725                         min_diff = 1;
1726                 else if (current_bss->level < -80)
1727                         min_diff = 2;
1728                 else if (current_bss->level < -75)
1729                         min_diff = 3;
1730                 else if (current_bss->level < -70)
1731                         min_diff = 4;
1732                 else
1733                         min_diff = 5;
1734                 if (cur_est > sel_est * 1.5)
1735                         min_diff += 10;
1736                 else if (cur_est > sel_est * 1.2)
1737                         min_diff += 5;
1738                 else if (cur_est > sel_est * 1.1)
1739                         min_diff += 2;
1740                 else if (cur_est > sel_est)
1741                         min_diff++;
1742         }
1743         if (to_5ghz) {
1744                 int reduce = 2;
1745
1746                 /* Make it easier to move to 5 GHz band */
1747                 if (sel_est > cur_est * 1.5)
1748                         reduce = 5;
1749                 else if (sel_est > cur_est * 1.2)
1750                         reduce = 4;
1751                 else if (sel_est > cur_est * 1.1)
1752                         reduce = 3;
1753
1754                 if (min_diff > reduce)
1755                         min_diff -= reduce;
1756                 else
1757                         min_diff = 0;
1758         }
1759         diff = abs(current_bss->level - selected->level);
1760         if (diff < min_diff) {
1761                 wpa_dbg(wpa_s, MSG_DEBUG,
1762                         "Skip roam - too small difference in signal level (%d < %d)",
1763                         diff, min_diff);
1764                 return 0;
1765         }
1766
1767         wpa_dbg(wpa_s, MSG_DEBUG,
1768                 "Allow reassociation due to difference in signal level (%d >= %d)",
1769                 diff, min_diff);
1770         return 1;
1771 #else /* CONFIG_NO_ROAMING */
1772         return 0;
1773 #endif /* CONFIG_NO_ROAMING */
1774 }
1775
1776
1777 /*
1778  * Return a negative value if no scan results could be fetched or if scan
1779  * results should not be shared with other virtual interfaces.
1780  * Return 0 if scan results were fetched and may be shared with other
1781  * interfaces.
1782  * Return 1 if scan results may be shared with other virtual interfaces but may
1783  * not trigger any operations.
1784  * Return 2 if the interface was removed and cannot be used.
1785  */
1786 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1787                                               union wpa_event_data *data,
1788                                               int own_request, int update_only)
1789 {
1790         struct wpa_scan_results *scan_res = NULL;
1791         int ret = 0;
1792         int ap = 0;
1793 #ifndef CONFIG_NO_RANDOM_POOL
1794         size_t i, num;
1795 #endif /* CONFIG_NO_RANDOM_POOL */
1796
1797 #ifdef CONFIG_AP
1798         if (wpa_s->ap_iface)
1799                 ap = 1;
1800 #endif /* CONFIG_AP */
1801
1802         wpa_supplicant_notify_scanning(wpa_s, 0);
1803
1804         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1805                                                    data ? &data->scan_info :
1806                                                    NULL, 1);
1807         if (scan_res == NULL) {
1808                 if (wpa_s->conf->ap_scan == 2 || ap ||
1809                     wpa_s->scan_res_handler == scan_only_handler)
1810                         return -1;
1811                 if (!own_request)
1812                         return -1;
1813                 if (data && data->scan_info.external_scan)
1814                         return -1;
1815                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1816                         "scanning again");
1817                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1818                 ret = -1;
1819                 goto scan_work_done;
1820         }
1821
1822 #ifndef CONFIG_NO_RANDOM_POOL
1823         num = scan_res->num;
1824         if (num > 10)
1825                 num = 10;
1826         for (i = 0; i < num; i++) {
1827                 u8 buf[5];
1828                 struct wpa_scan_res *res = scan_res->res[i];
1829                 buf[0] = res->bssid[5];
1830                 buf[1] = res->qual & 0xff;
1831                 buf[2] = res->noise & 0xff;
1832                 buf[3] = res->level & 0xff;
1833                 buf[4] = res->tsf & 0xff;
1834                 random_add_randomness(buf, sizeof(buf));
1835         }
1836 #endif /* CONFIG_NO_RANDOM_POOL */
1837
1838         if (update_only) {
1839                 ret = 1;
1840                 goto scan_work_done;
1841         }
1842
1843         if (own_request && wpa_s->scan_res_handler &&
1844             !(data && data->scan_info.external_scan)) {
1845                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1846                                          struct wpa_scan_results *scan_res);
1847
1848                 scan_res_handler = wpa_s->scan_res_handler;
1849                 wpa_s->scan_res_handler = NULL;
1850                 scan_res_handler(wpa_s, scan_res);
1851                 ret = 1;
1852                 goto scan_work_done;
1853         }
1854
1855         if (ap) {
1856                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1857 #ifdef CONFIG_AP
1858                 if (wpa_s->ap_iface->scan_cb)
1859                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1860 #endif /* CONFIG_AP */
1861                 goto scan_work_done;
1862         }
1863
1864         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1865                 wpa_s->own_scan_running,
1866                 data ? data->scan_info.external_scan : 0);
1867         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1868             wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
1869             own_request && !(data && data->scan_info.external_scan)) {
1870                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1871                              wpa_s->manual_scan_id);
1872                 wpa_s->manual_scan_use_id = 0;
1873         } else {
1874                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1875         }
1876         wpas_notify_scan_results(wpa_s);
1877
1878         wpas_notify_scan_done(wpa_s, 1);
1879
1880         if (data && data->scan_info.external_scan) {
1881                 wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
1882                 wpa_scan_results_free(scan_res);
1883                 return 0;
1884         }
1885
1886         if (wnm_scan_process(wpa_s, 1) > 0)
1887                 goto scan_work_done;
1888
1889         if (sme_proc_obss_scan(wpa_s) > 0)
1890                 goto scan_work_done;
1891
1892         if (own_request &&
1893             wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
1894                 goto scan_work_done;
1895
1896         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1897                 goto scan_work_done;
1898
1899         if (autoscan_notify_scan(wpa_s, scan_res))
1900                 goto scan_work_done;
1901
1902         if (wpa_s->disconnected) {
1903                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1904                 goto scan_work_done;
1905         }
1906
1907         if (!wpas_driver_bss_selection(wpa_s) &&
1908             bgscan_notify_scan(wpa_s, scan_res) == 1)
1909                 goto scan_work_done;
1910
1911         wpas_wps_update_ap_info(wpa_s, scan_res);
1912
1913         if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
1914             wpa_s->wpa_state < WPA_COMPLETED)
1915                 goto scan_work_done;
1916
1917         wpa_scan_results_free(scan_res);
1918
1919         if (own_request && wpa_s->scan_work) {
1920                 struct wpa_radio_work *work = wpa_s->scan_work;
1921                 wpa_s->scan_work = NULL;
1922                 radio_work_done(work);
1923         }
1924
1925         return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
1926
1927 scan_work_done:
1928         wpa_scan_results_free(scan_res);
1929         if (own_request && wpa_s->scan_work) {
1930                 struct wpa_radio_work *work = wpa_s->scan_work;
1931                 wpa_s->scan_work = NULL;
1932                 radio_work_done(work);
1933         }
1934         return ret;
1935 }
1936
1937
1938 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1939                                               int new_scan, int own_request)
1940 {
1941         struct wpa_bss *selected;
1942         struct wpa_ssid *ssid = NULL;
1943         int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1944
1945         if (time_to_reenable > 0) {
1946                 wpa_dbg(wpa_s, MSG_DEBUG,
1947                         "Postpone network selection by %d seconds since all networks are disabled",
1948                         time_to_reenable);
1949                 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1950                 eloop_register_timeout(time_to_reenable, 0,
1951                                        wpas_network_reenabled, wpa_s, NULL);
1952                 return 0;
1953         }
1954
1955         if (wpa_s->p2p_mgmt)
1956                 return 0; /* no normal connection on p2p_mgmt interface */
1957
1958         wpa_s->owe_transition_search = 0;
1959         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1960
1961 #ifdef CONFIG_MESH
1962         if (wpa_s->ifmsh) {
1963                 wpa_msg(wpa_s, MSG_INFO,
1964                         "Avoiding join because we already joined a mesh group");
1965                 return 0;
1966         }
1967 #endif /* CONFIG_MESH */
1968
1969         if (selected) {
1970                 int skip;
1971                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1972                 if (skip) {
1973                         if (new_scan)
1974                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1975                         return 0;
1976                 }
1977
1978                 if (ssid != wpa_s->current_ssid &&
1979                     wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1980                         wpa_s->own_disconnect_req = 1;
1981                         wpa_supplicant_deauthenticate(
1982                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1983                 }
1984
1985                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1986                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1987                         return -1;
1988                 }
1989                 if (new_scan)
1990                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1991                 /*
1992                  * Do not allow other virtual radios to trigger operations based
1993                  * on these scan results since we do not want them to start
1994                  * other associations at the same time.
1995                  */
1996                 return 1;
1997         } else {
1998                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1999                 ssid = wpa_supplicant_pick_new_network(wpa_s);
2000                 if (ssid) {
2001                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
2002                         wpa_supplicant_associate(wpa_s, NULL, ssid);
2003                         if (new_scan)
2004                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
2005                 } else if (own_request) {
2006                         /*
2007                          * No SSID found. If SCAN results are as a result of
2008                          * own scan request and not due to a scan request on
2009                          * another shared interface, try another scan.
2010                          */
2011                         int timeout_sec = wpa_s->scan_interval;
2012                         int timeout_usec = 0;
2013 #ifdef CONFIG_P2P
2014                         int res;
2015
2016                         res = wpas_p2p_scan_no_go_seen(wpa_s);
2017                         if (res == 2)
2018                                 return 2;
2019                         if (res == 1)
2020                                 return 0;
2021
2022                         if (wpa_s->p2p_in_provisioning ||
2023                             wpa_s->show_group_started ||
2024                             wpa_s->p2p_in_invitation) {
2025                                 /*
2026                                  * Use shorter wait during P2P Provisioning
2027                                  * state and during P2P join-a-group operation
2028                                  * to speed up group formation.
2029                                  */
2030                                 timeout_sec = 0;
2031                                 timeout_usec = 250000;
2032                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2033                                                             timeout_usec);
2034                                 return 0;
2035                         }
2036 #endif /* CONFIG_P2P */
2037 #ifdef CONFIG_INTERWORKING
2038                         if (wpa_s->conf->auto_interworking &&
2039                             wpa_s->conf->interworking &&
2040                             wpa_s->conf->cred) {
2041                                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
2042                                         "start ANQP fetch since no matching "
2043                                         "networks found");
2044                                 wpa_s->network_select = 1;
2045                                 wpa_s->auto_network_select = 1;
2046                                 interworking_start_fetch_anqp(wpa_s);
2047                                 return 1;
2048                         }
2049 #endif /* CONFIG_INTERWORKING */
2050 #ifdef CONFIG_WPS
2051                         if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
2052                                 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
2053                                 timeout_sec = 0;
2054                                 timeout_usec = 500000;
2055                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2056                                                             timeout_usec);
2057                                 return 0;
2058                         }
2059 #endif /* CONFIG_WPS */
2060 #ifdef CONFIG_OWE
2061                         if (wpa_s->owe_transition_search) {
2062                                 wpa_dbg(wpa_s, MSG_DEBUG,
2063                                         "OWE: Use shorter wait during transition mode search");
2064                                 timeout_sec = 0;
2065                                 timeout_usec = 500000;
2066                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2067                                                             timeout_usec);
2068                                 return 0;
2069                         }
2070 #endif /* CONFIG_OWE */
2071                         if (wpa_supplicant_req_sched_scan(wpa_s))
2072                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2073                                                             timeout_usec);
2074
2075                         wpa_msg_ctrl(wpa_s, MSG_INFO,
2076                                      WPA_EVENT_NETWORK_NOT_FOUND);
2077                 }
2078         }
2079         return 0;
2080 }
2081
2082
2083 static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
2084                                              union wpa_event_data *data)
2085 {
2086         struct wpa_supplicant *ifs;
2087         int res;
2088
2089         res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
2090         if (res == 2) {
2091                 /*
2092                  * Interface may have been removed, so must not dereference
2093                  * wpa_s after this.
2094                  */
2095                 return 1;
2096         }
2097
2098         if (res < 0) {
2099                 /*
2100                  * If no scan results could be fetched, then no need to
2101                  * notify those interfaces that did not actually request
2102                  * this scan. Similarly, if scan results started a new operation on this
2103                  * interface, do not notify other interfaces to avoid concurrent
2104                  * operations during a connection attempt.
2105                  */
2106                 return 0;
2107         }
2108
2109         /*
2110          * Check other interfaces to see if they share the same radio. If
2111          * so, they get updated with this same scan info.
2112          */
2113         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2114                          radio_list) {
2115                 if (ifs != wpa_s) {
2116                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
2117                                    "sibling", ifs->ifname);
2118                         res = _wpa_supplicant_event_scan_results(ifs, data, 0,
2119                                                                  res > 0);
2120                         if (res < 0)
2121                                 return 0;
2122                 }
2123         }
2124
2125         return 0;
2126 }
2127
2128 #endif /* CONFIG_NO_SCAN_PROCESSING */
2129
2130
2131 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
2132 {
2133 #ifdef CONFIG_NO_SCAN_PROCESSING
2134         return -1;
2135 #else /* CONFIG_NO_SCAN_PROCESSING */
2136         struct os_reltime now;
2137
2138         wpa_s->ignore_post_flush_scan_res = 0;
2139
2140         if (wpa_s->last_scan_res_used == 0)
2141                 return -1;
2142
2143         os_get_reltime(&now);
2144         if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
2145                 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
2146                 return -1;
2147         }
2148
2149         return wpas_select_network_from_last_scan(wpa_s, 0, 1);
2150 #endif /* CONFIG_NO_SCAN_PROCESSING */
2151 }
2152
2153 #ifdef CONFIG_WNM
2154
2155 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
2156 {
2157         struct wpa_supplicant *wpa_s = eloop_ctx;
2158
2159         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2160                 return;
2161
2162         if (!wpa_s->no_keep_alive) {
2163                 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
2164                            MAC2STR(wpa_s->bssid));
2165                 /* TODO: could skip this if normal data traffic has been sent */
2166                 /* TODO: Consider using some more appropriate data frame for
2167                  * this */
2168                 if (wpa_s->l2)
2169                         l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
2170                                        (u8 *) "", 0);
2171         }
2172
2173 #ifdef CONFIG_SME
2174         if (wpa_s->sme.bss_max_idle_period) {
2175                 unsigned int msec;
2176                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
2177                 if (msec > 100)
2178                         msec -= 100;
2179                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2180                                        wnm_bss_keep_alive, wpa_s, NULL);
2181         }
2182 #endif /* CONFIG_SME */
2183 }
2184
2185
2186 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
2187                                    const u8 *ies, size_t ies_len)
2188 {
2189         struct ieee802_11_elems elems;
2190
2191         if (ies == NULL)
2192                 return;
2193
2194         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
2195                 return;
2196
2197 #ifdef CONFIG_SME
2198         if (elems.bss_max_idle_period) {
2199                 unsigned int msec;
2200                 wpa_s->sme.bss_max_idle_period =
2201                         WPA_GET_LE16(elems.bss_max_idle_period);
2202                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
2203                            "TU)%s", wpa_s->sme.bss_max_idle_period,
2204                            (elems.bss_max_idle_period[2] & 0x01) ?
2205                            " (protected keep-live required)" : "");
2206                 if (wpa_s->sme.bss_max_idle_period == 0)
2207                         wpa_s->sme.bss_max_idle_period = 1;
2208                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
2209                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
2210                          /* msec times 1000 */
2211                         msec = wpa_s->sme.bss_max_idle_period * 1024;
2212                         if (msec > 100)
2213                                 msec -= 100;
2214                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2215                                                wnm_bss_keep_alive, wpa_s,
2216                                                NULL);
2217                 }
2218         }
2219 #endif /* CONFIG_SME */
2220 }
2221
2222 #endif /* CONFIG_WNM */
2223
2224
2225 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
2226 {
2227 #ifdef CONFIG_WNM
2228         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
2229 #endif /* CONFIG_WNM */
2230 }
2231
2232
2233 #ifdef CONFIG_INTERWORKING
2234
2235 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
2236                             size_t len)
2237 {
2238         int res;
2239
2240         wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
2241         res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
2242         if (res) {
2243                 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
2244         }
2245
2246         return res;
2247 }
2248
2249
2250 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
2251                                             const u8 *ies, size_t ies_len)
2252 {
2253         struct ieee802_11_elems elems;
2254
2255         if (ies == NULL)
2256                 return;
2257
2258         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
2259                 return;
2260
2261         if (elems.qos_map_set) {
2262                 wpas_qos_map_set(wpa_s, elems.qos_map_set,
2263                                  elems.qos_map_set_len);
2264         }
2265 }
2266
2267 #endif /* CONFIG_INTERWORKING */
2268
2269
2270 #ifdef CONFIG_FST
2271 static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
2272                                 const u8 *ie, size_t ie_len)
2273 {
2274         struct mb_ies_info mb_ies;
2275
2276         if (!ie || !ie_len || !wpa_s->fst)
2277             return -ENOENT;
2278
2279         os_memset(&mb_ies, 0, sizeof(mb_ies));
2280
2281         while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
2282                 size_t len;
2283
2284                 len = 2 + ie[1];
2285                 if (len > ie_len) {
2286                         wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
2287                                     ie, ie_len);
2288                         break;
2289                 }
2290
2291                 if (ie[0] == WLAN_EID_MULTI_BAND) {
2292                         wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
2293                                    (unsigned int) len);
2294                         mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
2295                         mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
2296                         mb_ies.nof_ies++;
2297                 }
2298
2299                 ie_len -= len;
2300                 ie += len;
2301         }
2302
2303         if (mb_ies.nof_ies > 0) {
2304                 wpabuf_free(wpa_s->received_mb_ies);
2305                 wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2306                 return 0;
2307         }
2308
2309         return -ENOENT;
2310 }
2311 #endif /* CONFIG_FST */
2312
2313
2314 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
2315                                           union wpa_event_data *data)
2316 {
2317         int l, len, found = 0, wpa_found, rsn_found;
2318         const u8 *p;
2319 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_OWE)
2320         u8 bssid[ETH_ALEN];
2321 #endif /* CONFIG_IEEE80211R || CONFIG_OWE */
2322
2323         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
2324         if (data->assoc_info.req_ies)
2325                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
2326                             data->assoc_info.req_ies_len);
2327         if (data->assoc_info.resp_ies) {
2328                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
2329                             data->assoc_info.resp_ies_len);
2330 #ifdef CONFIG_TDLS
2331                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
2332                                         data->assoc_info.resp_ies_len);
2333 #endif /* CONFIG_TDLS */
2334 #ifdef CONFIG_WNM
2335                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2336                                        data->assoc_info.resp_ies_len);
2337 #endif /* CONFIG_WNM */
2338 #ifdef CONFIG_INTERWORKING
2339                 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2340                                                 data->assoc_info.resp_ies_len);
2341 #endif /* CONFIG_INTERWORKING */
2342                 if (wpa_s->hw_capab == CAPAB_VHT &&
2343                     get_ie(data->assoc_info.resp_ies,
2344                            data->assoc_info.resp_ies_len, WLAN_EID_VHT_CAP))
2345                         wpa_s->ieee80211ac = 1;
2346         }
2347         if (data->assoc_info.beacon_ies)
2348                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
2349                             data->assoc_info.beacon_ies,
2350                             data->assoc_info.beacon_ies_len);
2351         if (data->assoc_info.freq)
2352                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
2353                         data->assoc_info.freq);
2354
2355         p = data->assoc_info.req_ies;
2356         l = data->assoc_info.req_ies_len;
2357
2358         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
2359         while (p && l >= 2) {
2360                 len = p[1] + 2;
2361                 if (len > l) {
2362                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2363                                     p, l);
2364                         break;
2365                 }
2366                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2367                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
2368                     (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
2369                      (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
2370                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
2371                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
2372                                 break;
2373                         found = 1;
2374                         wpa_find_assoc_pmkid(wpa_s);
2375                         break;
2376                 }
2377                 l -= len;
2378                 p += len;
2379         }
2380         if (!found && data->assoc_info.req_ies)
2381                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
2382
2383 #ifdef CONFIG_FILS
2384 #ifdef CONFIG_SME
2385         if ((wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
2386              wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS) &&
2387             (!data->assoc_info.resp_frame ||
2388              fils_process_assoc_resp(wpa_s->wpa,
2389                                      data->assoc_info.resp_frame,
2390                                      data->assoc_info.resp_frame_len) < 0)) {
2391                 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
2392                 return -1;
2393         }
2394 #endif /* CONFIG_SME */
2395
2396         /* Additional processing for FILS when SME is in driver */
2397         if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS &&
2398             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2399                 wpa_sm_set_reset_fils_completed(wpa_s->wpa, 1);
2400 #endif /* CONFIG_FILS */
2401
2402 #ifdef CONFIG_OWE
2403         if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
2404             (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2405              owe_process_assoc_resp(wpa_s->wpa, bssid,
2406                                     data->assoc_info.resp_ies,
2407                                     data->assoc_info.resp_ies_len) < 0)) {
2408                 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
2409                 return -1;
2410         }
2411 #endif /* CONFIG_OWE */
2412
2413 #ifdef CONFIG_IEEE80211R
2414 #ifdef CONFIG_SME
2415         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
2416                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2417                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2418                                                  data->assoc_info.resp_ies,
2419                                                  data->assoc_info.resp_ies_len,
2420                                                  bssid) < 0) {
2421                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2422                                 "Reassociation Response failed");
2423                         wpa_supplicant_deauthenticate(
2424                                 wpa_s, WLAN_REASON_INVALID_IE);
2425                         return -1;
2426                 }
2427         }
2428
2429         p = data->assoc_info.resp_ies;
2430         l = data->assoc_info.resp_ies_len;
2431
2432 #ifdef CONFIG_WPS_STRICT
2433         if (p && wpa_s->current_ssid &&
2434             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
2435                 struct wpabuf *wps;
2436                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
2437                 if (wps == NULL) {
2438                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
2439                                 "include WPS IE in (Re)Association Response");
2440                         return -1;
2441                 }
2442
2443                 if (wps_validate_assoc_resp(wps) < 0) {
2444                         wpabuf_free(wps);
2445                         wpa_supplicant_deauthenticate(
2446                                 wpa_s, WLAN_REASON_INVALID_IE);
2447                         return -1;
2448                 }
2449                 wpabuf_free(wps);
2450         }
2451 #endif /* CONFIG_WPS_STRICT */
2452
2453         /* Go through the IEs and make a copy of the MDIE, if present. */
2454         while (p && l >= 2) {
2455                 len = p[1] + 2;
2456                 if (len > l) {
2457                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2458                                     p, l);
2459                         break;
2460                 }
2461                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
2462                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
2463                         wpa_s->sme.ft_used = 1;
2464                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
2465                                   MOBILITY_DOMAIN_ID_LEN);
2466                         break;
2467                 }
2468                 l -= len;
2469                 p += len;
2470         }
2471 #endif /* CONFIG_SME */
2472
2473         /* Process FT when SME is in the driver */
2474         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2475             wpa_ft_is_completed(wpa_s->wpa)) {
2476                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2477                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2478                                                  data->assoc_info.resp_ies,
2479                                                  data->assoc_info.resp_ies_len,
2480                                                  bssid) < 0) {
2481                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2482                                 "Reassociation Response failed");
2483                         wpa_supplicant_deauthenticate(
2484                                 wpa_s, WLAN_REASON_INVALID_IE);
2485                         return -1;
2486                 }
2487                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
2488         }
2489
2490         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
2491                              data->assoc_info.resp_ies_len);
2492 #endif /* CONFIG_IEEE80211R */
2493
2494         /* WPA/RSN IE from Beacon/ProbeResp */
2495         p = data->assoc_info.beacon_ies;
2496         l = data->assoc_info.beacon_ies_len;
2497
2498         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
2499          */
2500         wpa_found = rsn_found = 0;
2501         while (p && l >= 2) {
2502                 len = p[1] + 2;
2503                 if (len > l) {
2504                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
2505                                     p, l);
2506                         break;
2507                 }
2508                 if (!wpa_found &&
2509                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2510                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
2511                         wpa_found = 1;
2512                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
2513                 }
2514
2515                 if (!rsn_found &&
2516                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
2517                         rsn_found = 1;
2518                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2519                 }
2520
2521                 l -= len;
2522                 p += len;
2523         }
2524
2525         if (!wpa_found && data->assoc_info.beacon_ies)
2526                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2527         if (!rsn_found && data->assoc_info.beacon_ies)
2528                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2529         if (wpa_found || rsn_found)
2530                 wpa_s->ap_ies_from_associnfo = 1;
2531
2532         if (wpa_s->assoc_freq && data->assoc_info.freq &&
2533             wpa_s->assoc_freq != data->assoc_info.freq) {
2534                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2535                            "%u to %u MHz",
2536                            wpa_s->assoc_freq, data->assoc_info.freq);
2537                 wpa_supplicant_update_scan_results(wpa_s);
2538         }
2539
2540         wpa_s->assoc_freq = data->assoc_info.freq;
2541
2542         return 0;
2543 }
2544
2545
2546 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2547 {
2548         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2549
2550         if (!wpa_s->current_bss || !wpa_s->current_ssid)
2551                 return -1;
2552
2553         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2554                 return 0;
2555
2556         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2557                                         WPA_IE_VENDOR_TYPE);
2558         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2559
2560         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2561                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
2562             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2563                                  bss_rsn ? 2 + bss_rsn[1] : 0))
2564                 return -1;
2565
2566         return 0;
2567 }
2568
2569
2570 static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
2571                                      union wpa_event_data *data)
2572 {
2573 #ifdef CONFIG_FST
2574         struct assoc_info *ai = data ? &data->assoc_info : NULL;
2575         struct wpa_bss *bss = wpa_s->current_bss;
2576         const u8 *ieprb, *iebcn;
2577
2578         wpabuf_free(wpa_s->received_mb_ies);
2579         wpa_s->received_mb_ies = NULL;
2580
2581         if (ai &&
2582             !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
2583                 wpa_printf(MSG_DEBUG,
2584                            "FST: MB IEs updated from Association Response frame");
2585                 return;
2586         }
2587
2588         if (ai &&
2589             !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
2590                 wpa_printf(MSG_DEBUG,
2591                            "FST: MB IEs updated from association event Beacon IEs");
2592                 return;
2593         }
2594
2595         if (!bss)
2596                 return;
2597
2598         ieprb = (const u8 *) (bss + 1);
2599         iebcn = ieprb + bss->ie_len;
2600
2601         if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
2602                 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
2603         else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
2604                 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
2605 #endif /* CONFIG_FST */
2606 }
2607
2608
2609 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2610                                        union wpa_event_data *data)
2611 {
2612         u8 bssid[ETH_ALEN];
2613         int ft_completed, already_authorized;
2614         int new_bss = 0;
2615
2616 #ifdef CONFIG_AP
2617         if (wpa_s->ap_iface) {
2618                 if (!data)
2619                         return;
2620                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2621                                     data->assoc_info.addr,
2622                                     data->assoc_info.req_ies,
2623                                     data->assoc_info.req_ies_len,
2624                                     data->assoc_info.reassoc);
2625                 return;
2626         }
2627 #endif /* CONFIG_AP */
2628
2629         eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2630
2631         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2632         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2633                 return;
2634         /*
2635          * FILS authentication can share the same mechanism to mark the
2636          * connection fully authenticated, so set ft_completed also based on
2637          * FILS result.
2638          */
2639         if (!ft_completed)
2640                 ft_completed = wpa_fils_is_completed(wpa_s->wpa);
2641
2642         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2643                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
2644                 wpa_supplicant_deauthenticate(
2645                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2646                 return;
2647         }
2648
2649         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
2650         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
2651                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2652                         MACSTR, MAC2STR(bssid));
2653                 new_bss = 1;
2654                 random_add_randomness(bssid, ETH_ALEN);
2655                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2656                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2657                 wpas_notify_bssid_changed(wpa_s);
2658
2659                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2660                         wpa_clear_keys(wpa_s, bssid);
2661                 }
2662                 if (wpa_supplicant_select_config(wpa_s) < 0) {
2663                         wpa_supplicant_deauthenticate(
2664                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2665                         return;
2666                 }
2667         }
2668
2669         if (wpa_s->conf->ap_scan == 1 &&
2670             wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
2671                 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2672                         wpa_msg(wpa_s, MSG_WARNING,
2673                                 "WPA/RSN IEs not updated");
2674         }
2675
2676         wpas_fst_update_mb_assoc(wpa_s, data);
2677
2678 #ifdef CONFIG_SME
2679         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2680         wpa_s->sme.prev_bssid_set = 1;
2681         wpa_s->sme.last_unprot_disconnect.sec = 0;
2682 #endif /* CONFIG_SME */
2683
2684         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2685         if (wpa_s->current_ssid) {
2686                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2687                  * initialized before association, but for other modes,
2688                  * initialize PC/SC here, if the current configuration needs
2689                  * smartcard or SIM/USIM. */
2690                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2691         }
2692         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2693         if (wpa_s->l2)
2694                 l2_packet_notify_auth_start(wpa_s->l2);
2695
2696         already_authorized = data && data->assoc_info.authorized;
2697
2698         /*
2699          * Set portEnabled first to FALSE in order to get EAP state machine out
2700          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2701          * state machine may transit to AUTHENTICATING state based on obsolete
2702          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2703          * AUTHENTICATED without ever giving chance to EAP state machine to
2704          * reset the state.
2705          */
2706         if (!ft_completed && !already_authorized) {
2707                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2708                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2709         }
2710         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2711             wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
2712             wpa_s->key_mgmt == WPA_KEY_MGMT_OWE || ft_completed ||
2713             already_authorized)
2714                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2715         /* 802.1X::portControl = Auto */
2716         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2717         wpa_s->eapol_received = 0;
2718         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2719             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2720             (wpa_s->current_ssid &&
2721              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
2722                 if (wpa_s->current_ssid &&
2723                     wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
2724                     (wpa_s->drv_flags &
2725                      WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2726                         /*
2727                          * Set the key after having received joined-IBSS event
2728                          * from the driver.
2729                          */
2730                         wpa_supplicant_set_wpa_none_key(wpa_s,
2731                                                         wpa_s->current_ssid);
2732                 }
2733                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2734                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2735         } else if (!ft_completed) {
2736                 /* Timeout for receiving the first EAPOL packet */
2737                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2738         }
2739         wpa_supplicant_cancel_scan(wpa_s);
2740
2741         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2742             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2743                 /*
2744                  * We are done; the driver will take care of RSN 4-way
2745                  * handshake.
2746                  */
2747                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2748                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2749                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2750                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2751         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2752                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2753                 /*
2754                  * The driver will take care of RSN 4-way handshake, so we need
2755                  * to allow EAPOL supplicant to complete its work without
2756                  * waiting for WPA supplicant.
2757                  */
2758                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2759         } else if (ft_completed) {
2760                 /*
2761                  * FT protocol completed - make sure EAPOL state machine ends
2762                  * up in authenticated.
2763                  */
2764                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2765                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2766                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2767                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2768         }
2769
2770         wpa_s->last_eapol_matches_bssid = 0;
2771
2772         if (wpa_s->pending_eapol_rx) {
2773                 struct os_reltime now, age;
2774                 os_get_reltime(&now);
2775                 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
2776                 if (age.sec == 0 && age.usec < 200000 &&
2777                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2778                     0) {
2779                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2780                                 "frame that was received just before "
2781                                 "association notification");
2782                         wpa_supplicant_rx_eapol(
2783                                 wpa_s, wpa_s->pending_eapol_rx_src,
2784                                 wpabuf_head(wpa_s->pending_eapol_rx),
2785                                 wpabuf_len(wpa_s->pending_eapol_rx));
2786                 }
2787                 wpabuf_free(wpa_s->pending_eapol_rx);
2788                 wpa_s->pending_eapol_rx = NULL;
2789         }
2790
2791         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2792              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
2793             wpa_s->current_ssid &&
2794             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2795                 /* Set static WEP keys again */
2796                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2797         }
2798
2799 #ifdef CONFIG_IBSS_RSN
2800         if (wpa_s->current_ssid &&
2801             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2802             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2803             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2804             wpa_s->ibss_rsn == NULL) {
2805                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
2806                 if (!wpa_s->ibss_rsn) {
2807                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2808                         wpa_supplicant_deauthenticate(
2809                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2810                         return;
2811                 }
2812
2813                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2814         }
2815 #endif /* CONFIG_IBSS_RSN */
2816
2817         wpas_wps_notify_assoc(wpa_s, bssid);
2818
2819         if (data) {
2820                 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2821                                     data->assoc_info.resp_ies_len,
2822                                     &data->assoc_info.wmm_params);
2823
2824                 if (wpa_s->reassoc_same_bss)
2825                         wmm_ac_restore_tspecs(wpa_s);
2826         }
2827
2828 #ifdef CONFIG_FILS
2829         if (wpa_key_mgmt_fils(wpa_s->key_mgmt)) {
2830                 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, bssid);
2831                 const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
2832
2833                 if (fils_cache_id)
2834                         wpa_sm_set_fils_cache_id(wpa_s->wpa, fils_cache_id);
2835         }
2836 #endif /* CONFIG_FILS */
2837 }
2838
2839
2840 static int disconnect_reason_recoverable(u16 reason_code)
2841 {
2842         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2843                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2844                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2845 }
2846
2847
2848 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
2849                                           u16 reason_code,
2850                                           int locally_generated)
2851 {
2852         const u8 *bssid;
2853
2854         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2855                 /*
2856                  * At least Host AP driver and a Prism3 card seemed to be
2857                  * generating streams of disconnected events when configuring
2858                  * IBSS for WPA-None. Ignore them for now.
2859                  */
2860                 return;
2861         }
2862
2863         bssid = wpa_s->bssid;
2864         if (is_zero_ether_addr(bssid))
2865                 bssid = wpa_s->pending_bssid;
2866
2867         if (!is_zero_ether_addr(bssid) ||
2868             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2869                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2870                         " reason=%d%s",
2871                         MAC2STR(bssid), reason_code,
2872                         locally_generated ? " locally_generated=1" : "");
2873         }
2874 }
2875
2876
2877 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2878                                  int locally_generated)
2879 {
2880         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2881             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2882                 return 0; /* Not in 4-way handshake with PSK */
2883
2884         /*
2885          * It looks like connection was lost while trying to go through PSK
2886          * 4-way handshake. Filter out known disconnection cases that are caused
2887          * by something else than PSK mismatch to avoid confusing reports.
2888          */
2889
2890         if (locally_generated) {
2891                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2892                         return 0;
2893         }
2894
2895         return 1;
2896 }
2897
2898
2899 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2900                                                  u16 reason_code,
2901                                                  int locally_generated)
2902 {
2903         const u8 *bssid;
2904         int authenticating;
2905         u8 prev_pending_bssid[ETH_ALEN];
2906         struct wpa_bss *fast_reconnect = NULL;
2907         struct wpa_ssid *fast_reconnect_ssid = NULL;
2908         struct wpa_ssid *last_ssid;
2909         struct wpa_bss *curr = NULL;
2910
2911         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2912         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2913
2914         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2915                 /*
2916                  * At least Host AP driver and a Prism3 card seemed to be
2917                  * generating streams of disconnected events when configuring
2918                  * IBSS for WPA-None. Ignore them for now.
2919                  */
2920                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2921                         "IBSS/WPA-None mode");
2922                 return;
2923         }
2924
2925         if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
2926             reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
2927             locally_generated)
2928                 /*
2929                  * Remove the inactive AP (which is probably out of range) from
2930                  * the BSS list after marking disassociation. In particular
2931                  * mac80211-based drivers use the
2932                  * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
2933                  * locally generated disconnection events for cases where the
2934                  * AP does not reply anymore.
2935                  */
2936                 curr = wpa_s->current_bss;
2937
2938         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
2939                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2940                         "pre-shared key may be incorrect");
2941                 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2942                         return; /* P2P group removed */
2943                 wpas_auth_failed(wpa_s, "WRONG_KEY");
2944         }
2945         if (!wpa_s->disconnected &&
2946             (!wpa_s->auto_reconnect_disabled ||
2947              wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2948              wpas_wps_searching(wpa_s) ||
2949              wpas_wps_reenable_networks_pending(wpa_s))) {
2950                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2951                         "reconnect (wps=%d/%d wpa_state=%d)",
2952                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2953                         wpas_wps_searching(wpa_s),
2954                         wpa_s->wpa_state);
2955                 if (wpa_s->wpa_state == WPA_COMPLETED &&
2956                     wpa_s->current_ssid &&
2957                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2958                     !locally_generated &&
2959                     disconnect_reason_recoverable(reason_code)) {
2960                         /*
2961                          * It looks like the AP has dropped association with
2962                          * us, but could allow us to get back in. Try to
2963                          * reconnect to the same BSS without full scan to save
2964                          * time for some common cases.
2965                          */
2966                         fast_reconnect = wpa_s->current_bss;
2967                         fast_reconnect_ssid = wpa_s->current_ssid;
2968                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2969                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2970                 else
2971                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2972                                 "immediate scan");
2973         } else {
2974                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2975                         "try to re-connect");
2976                 wpa_s->reassociate = 0;
2977                 wpa_s->disconnected = 1;
2978                 if (!wpa_s->pno)
2979                         wpa_supplicant_cancel_sched_scan(wpa_s);
2980         }
2981         bssid = wpa_s->bssid;
2982         if (is_zero_ether_addr(bssid))
2983                 bssid = wpa_s->pending_bssid;
2984         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2985                 wpas_connection_failed(wpa_s, bssid);
2986         wpa_sm_notify_disassoc(wpa_s->wpa);
2987         if (locally_generated)
2988                 wpa_s->disconnect_reason = -reason_code;
2989         else
2990                 wpa_s->disconnect_reason = reason_code;
2991         wpas_notify_disconnect_reason(wpa_s);
2992         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2993                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2994                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2995         }
2996         last_ssid = wpa_s->current_ssid;
2997         wpa_supplicant_mark_disassoc(wpa_s);
2998
2999         if (curr)
3000                 wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
3001
3002         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
3003                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
3004                 wpa_s->current_ssid = last_ssid;
3005         }
3006
3007         if (fast_reconnect &&
3008             !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
3009             !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
3010             !disallowed_ssid(wpa_s, fast_reconnect->ssid,
3011                              fast_reconnect->ssid_len) &&
3012             !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
3013             !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect->bssid)) {
3014 #ifndef CONFIG_NO_SCAN_PROCESSING
3015                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
3016                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
3017                                            fast_reconnect_ssid) < 0) {
3018                         /* Recover through full scan */
3019                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
3020                 }
3021 #endif /* CONFIG_NO_SCAN_PROCESSING */
3022         } else if (fast_reconnect) {
3023                 /*
3024                  * Could not reconnect to the same BSS due to network being
3025                  * disabled. Use a new scan to match the alternative behavior
3026                  * above, i.e., to continue automatic reconnection attempt in a
3027                  * way that enforces disabled network rules.
3028                  */
3029                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
3030         }
3031 }
3032
3033
3034 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
3035 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
3036 {
3037         struct wpa_supplicant *wpa_s = eloop_ctx;
3038
3039         if (!wpa_s->pending_mic_error_report)
3040                 return;
3041
3042         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
3043         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
3044         wpa_s->pending_mic_error_report = 0;
3045 }
3046 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
3047
3048
3049 static void
3050 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
3051                                          union wpa_event_data *data)
3052 {
3053         int pairwise;
3054         struct os_reltime t;
3055
3056         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
3057         pairwise = (data && data->michael_mic_failure.unicast);
3058         os_get_reltime(&t);
3059         if ((wpa_s->last_michael_mic_error.sec &&
3060              !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
3061             wpa_s->pending_mic_error_report) {
3062                 if (wpa_s->pending_mic_error_report) {
3063                         /*
3064                          * Send the pending MIC error report immediately since
3065                          * we are going to start countermeasures and AP better
3066                          * do the same.
3067                          */
3068                         wpa_sm_key_request(wpa_s->wpa, 1,
3069                                            wpa_s->pending_mic_error_pairwise);
3070                 }
3071
3072                 /* Send the new MIC error report immediately since we are going
3073                  * to start countermeasures and AP better do the same.
3074                  */
3075                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
3076
3077                 /* initialize countermeasures */
3078                 wpa_s->countermeasures = 1;
3079
3080                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
3081
3082                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
3083
3084                 /*
3085                  * Need to wait for completion of request frame. We do not get
3086                  * any callback for the message completion, so just wait a
3087                  * short while and hope for the best. */
3088                 os_sleep(0, 10000);
3089
3090                 wpa_drv_set_countermeasures(wpa_s, 1);
3091                 wpa_supplicant_deauthenticate(wpa_s,
3092                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
3093                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
3094                                      wpa_s, NULL);
3095                 eloop_register_timeout(60, 0,
3096                                        wpa_supplicant_stop_countermeasures,
3097                                        wpa_s, NULL);
3098                 /* TODO: mark the AP rejected for 60 second. STA is
3099                  * allowed to associate with another AP.. */
3100         } else {
3101 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
3102                 if (wpa_s->mic_errors_seen) {
3103                         /*
3104                          * Reduce the effectiveness of Michael MIC error
3105                          * reports as a means for attacking against TKIP if
3106                          * more than one MIC failure is noticed with the same
3107                          * PTK. We delay the transmission of the reports by a
3108                          * random time between 0 and 60 seconds in order to
3109                          * force the attacker wait 60 seconds before getting
3110                          * the information on whether a frame resulted in a MIC
3111                          * failure.
3112                          */
3113                         u8 rval[4];
3114                         int sec;
3115
3116                         if (os_get_random(rval, sizeof(rval)) < 0)
3117                                 sec = os_random() % 60;
3118                         else
3119                                 sec = WPA_GET_BE32(rval) % 60;
3120                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
3121                                 "report %d seconds", sec);
3122                         wpa_s->pending_mic_error_report = 1;
3123                         wpa_s->pending_mic_error_pairwise = pairwise;
3124                         eloop_cancel_timeout(
3125                                 wpa_supplicant_delayed_mic_error_report,
3126                                 wpa_s, NULL);
3127                         eloop_register_timeout(
3128                                 sec, os_random() % 1000000,
3129                                 wpa_supplicant_delayed_mic_error_report,
3130                                 wpa_s, NULL);
3131                 } else {
3132                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
3133                 }
3134 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
3135                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
3136 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
3137         }
3138         wpa_s->last_michael_mic_error = t;
3139         wpa_s->mic_errors_seen++;
3140 }
3141
3142
3143 #ifdef CONFIG_TERMINATE_ONLASTIF
3144 static int any_interfaces(struct wpa_supplicant *head)
3145 {
3146         struct wpa_supplicant *wpa_s;
3147
3148         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
3149                 if (!wpa_s->interface_removed)
3150                         return 1;
3151         return 0;
3152 }
3153 #endif /* CONFIG_TERMINATE_ONLASTIF */
3154
3155
3156 static void
3157 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
3158                                       union wpa_event_data *data)
3159 {
3160         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
3161                 return;
3162
3163         switch (data->interface_status.ievent) {
3164         case EVENT_INTERFACE_ADDED:
3165                 if (!wpa_s->interface_removed)
3166                         break;
3167                 wpa_s->interface_removed = 0;
3168                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
3169                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
3170                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
3171                                 "driver after interface was added");
3172                 }
3173
3174 #ifdef CONFIG_P2P
3175                 if (!wpa_s->global->p2p &&
3176                     !wpa_s->global->p2p_disabled &&
3177                     !wpa_s->conf->p2p_disabled &&
3178                     (wpa_s->drv_flags &
3179                      WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3180                     wpas_p2p_add_p2pdev_interface(
3181                             wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
3182                         wpa_printf(MSG_INFO,
3183                                    "P2P: Failed to enable P2P Device interface");
3184                         /* Try to continue without. P2P will be disabled. */
3185                 }
3186 #endif /* CONFIG_P2P */
3187
3188                 break;
3189         case EVENT_INTERFACE_REMOVED:
3190                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
3191                 wpa_s->interface_removed = 1;
3192                 wpa_supplicant_mark_disassoc(wpa_s);
3193                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3194                 l2_packet_deinit(wpa_s->l2);
3195                 wpa_s->l2 = NULL;
3196
3197 #ifdef CONFIG_P2P
3198                 if (wpa_s->global->p2p &&
3199                     wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
3200                     (wpa_s->drv_flags &
3201                      WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
3202                         wpa_dbg(wpa_s, MSG_DEBUG,
3203                                 "Removing P2P Device interface");
3204                         wpa_supplicant_remove_iface(
3205                                 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
3206                                 0);
3207                         wpa_s->global->p2p_init_wpa_s = NULL;
3208                 }
3209 #endif /* CONFIG_P2P */
3210
3211 #ifdef CONFIG_MATCH_IFACE
3212                 if (wpa_s->matched) {
3213                         wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
3214                         break;
3215                 }
3216 #endif /* CONFIG_MATCH_IFACE */
3217
3218 #ifdef CONFIG_TERMINATE_ONLASTIF
3219                 /* check if last interface */
3220                 if (!any_interfaces(wpa_s->global->ifaces))
3221                         eloop_terminate();
3222 #endif /* CONFIG_TERMINATE_ONLASTIF */
3223                 break;
3224         }
3225 }
3226
3227
3228 #ifdef CONFIG_TDLS
3229 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
3230                                       union wpa_event_data *data)
3231 {
3232         if (data == NULL)
3233                 return;
3234         switch (data->tdls.oper) {
3235         case TDLS_REQUEST_SETUP:
3236                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
3237                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
3238                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
3239                 else
3240                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
3241                 break;
3242         case TDLS_REQUEST_TEARDOWN:
3243                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
3244                         wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
3245                                                data->tdls.reason_code);
3246                 else
3247                         wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
3248                                           data->tdls.peer);
3249                 break;
3250         case TDLS_REQUEST_DISCOVER:
3251                         wpa_tdls_send_discovery_request(wpa_s->wpa,
3252                                                         data->tdls.peer);
3253                 break;
3254         }
3255 }
3256 #endif /* CONFIG_TDLS */
3257
3258
3259 #ifdef CONFIG_WNM
3260 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
3261                                      union wpa_event_data *data)
3262 {
3263         if (data == NULL)
3264                 return;
3265         switch (data->wnm.oper) {
3266         case WNM_OPER_SLEEP:
3267                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
3268                            "(action=%d, intval=%d)",
3269                            data->wnm.sleep_action, data->wnm.sleep_intval);
3270                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
3271                                              data->wnm.sleep_intval, NULL);
3272                 break;
3273         }
3274 }
3275 #endif /* CONFIG_WNM */
3276
3277
3278 #ifdef CONFIG_IEEE80211R
3279 static void
3280 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
3281                                  union wpa_event_data *data)
3282 {
3283         if (data == NULL)
3284                 return;
3285
3286         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
3287                                     data->ft_ies.ies_len,
3288                                     data->ft_ies.ft_action,
3289                                     data->ft_ies.target_ap,
3290                                     data->ft_ies.ric_ies,
3291                                     data->ft_ies.ric_ies_len) < 0) {
3292                 /* TODO: prevent MLME/driver from trying to associate? */
3293         }
3294 }
3295 #endif /* CONFIG_IEEE80211R */
3296
3297
3298 #ifdef CONFIG_IBSS_RSN
3299 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
3300                                                 union wpa_event_data *data)
3301 {
3302         struct wpa_ssid *ssid;
3303         if (wpa_s->wpa_state < WPA_ASSOCIATED)
3304                 return;
3305         if (data == NULL)
3306                 return;
3307         ssid = wpa_s->current_ssid;
3308         if (ssid == NULL)
3309                 return;
3310         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3311                 return;
3312
3313         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
3314 }
3315
3316
3317 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
3318                                            union wpa_event_data *data)
3319 {
3320         struct wpa_ssid *ssid = wpa_s->current_ssid;
3321
3322         if (ssid == NULL)
3323                 return;
3324
3325         /* check if the ssid is correctly configured as IBSS/RSN */
3326         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3327                 return;
3328
3329         ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
3330                              data->rx_mgmt.frame_len);
3331 }
3332 #endif /* CONFIG_IBSS_RSN */
3333
3334
3335 #ifdef CONFIG_IEEE80211R
3336 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
3337                          size_t len)
3338 {
3339         const u8 *sta_addr, *target_ap_addr;
3340         u16 status;
3341
3342         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
3343         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
3344                 return; /* only SME case supported for now */
3345         if (len < 1 + 2 * ETH_ALEN + 2)
3346                 return;
3347         if (data[0] != 2)
3348                 return; /* Only FT Action Response is supported for now */
3349         sta_addr = data + 1;
3350         target_ap_addr = data + 1 + ETH_ALEN;
3351         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
3352         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
3353                 MACSTR " TargetAP " MACSTR " status %u",
3354                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
3355
3356         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
3357                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
3358                         " in FT Action Response", MAC2STR(sta_addr));
3359                 return;
3360         }
3361
3362         if (status) {
3363                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
3364                         "failure (status code %d)", status);
3365                 /* TODO: report error to FT code(?) */
3366                 return;
3367         }
3368
3369         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
3370                                     len - (1 + 2 * ETH_ALEN + 2), 1,
3371                                     target_ap_addr, NULL, 0) < 0)
3372                 return;
3373
3374 #ifdef CONFIG_SME
3375         {
3376                 struct wpa_bss *bss;
3377                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
3378                 if (bss)
3379                         wpa_s->sme.freq = bss->freq;
3380                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
3381                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
3382                               WLAN_AUTH_FT);
3383         }
3384 #endif /* CONFIG_SME */
3385 }
3386 #endif /* CONFIG_IEEE80211R */
3387
3388
3389 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
3390                                                struct unprot_deauth *e)
3391 {
3392 #ifdef CONFIG_IEEE80211W
3393         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
3394                    "dropped: " MACSTR " -> " MACSTR
3395                    " (reason code %u)",
3396                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3397         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3398 #endif /* CONFIG_IEEE80211W */
3399 }
3400
3401
3402 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
3403                                                  struct unprot_disassoc *e)
3404 {
3405 #ifdef CONFIG_IEEE80211W
3406         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
3407                    "dropped: " MACSTR " -> " MACSTR
3408                    " (reason code %u)",
3409                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3410         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3411 #endif /* CONFIG_IEEE80211W */
3412 }
3413
3414
3415 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
3416                                   u16 reason_code, int locally_generated,
3417                                   const u8 *ie, size_t ie_len, int deauth)
3418 {
3419 #ifdef CONFIG_AP
3420         if (wpa_s->ap_iface && addr) {
3421                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
3422                 return;
3423         }
3424
3425         if (wpa_s->ap_iface) {
3426                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
3427                 return;
3428         }
3429 #endif /* CONFIG_AP */
3430
3431         if (!locally_generated)
3432                 wpa_s->own_disconnect_req = 0;
3433
3434         wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
3435
3436         if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
3437               ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
3438                 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
3439                eapol_sm_failed(wpa_s->eapol))) &&
3440              !wpa_s->eap_expected_failure))
3441                 wpas_auth_failed(wpa_s, "AUTH_FAILED");
3442
3443 #ifdef CONFIG_P2P
3444         if (deauth && reason_code > 0) {
3445                 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
3446                                           locally_generated) > 0) {
3447                         /*
3448                          * The interface was removed, so cannot continue
3449                          * processing any additional operations after this.
3450                          */
3451                         return;
3452                 }
3453         }
3454 #endif /* CONFIG_P2P */
3455
3456         wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
3457                                              locally_generated);
3458 }
3459
3460
3461 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
3462                                 struct disassoc_info *info)
3463 {
3464         u16 reason_code = 0;
3465         int locally_generated = 0;
3466         const u8 *addr = NULL;
3467         const u8 *ie = NULL;
3468         size_t ie_len = 0;
3469
3470         wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
3471
3472         if (info) {
3473                 addr = info->addr;
3474                 ie = info->ie;
3475                 ie_len = info->ie_len;
3476                 reason_code = info->reason_code;
3477                 locally_generated = info->locally_generated;
3478                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
3479                         locally_generated ? " (locally generated)" : "");
3480                 if (addr)
3481                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3482                                 MAC2STR(addr));
3483                 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
3484                             ie, ie_len);
3485         }
3486
3487 #ifdef CONFIG_AP
3488         if (wpa_s->ap_iface && info && info->addr) {
3489                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
3490                 return;
3491         }
3492
3493         if (wpa_s->ap_iface) {
3494                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
3495                 return;
3496         }
3497 #endif /* CONFIG_AP */
3498
3499 #ifdef CONFIG_P2P
3500         if (info) {
3501                 wpas_p2p_disassoc_notif(
3502                         wpa_s, info->addr, reason_code, info->ie, info->ie_len,
3503                         locally_generated);
3504         }
3505 #endif /* CONFIG_P2P */
3506
3507         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3508                 sme_event_disassoc(wpa_s, info);
3509
3510         wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
3511                               ie, ie_len, 0);
3512 }
3513
3514
3515 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
3516                               struct deauth_info *info)
3517 {
3518         u16 reason_code = 0;
3519         int locally_generated = 0;
3520         const u8 *addr = NULL;
3521         const u8 *ie = NULL;
3522         size_t ie_len = 0;
3523
3524         wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
3525
3526         if (info) {
3527                 addr = info->addr;
3528                 ie = info->ie;
3529                 ie_len = info->ie_len;
3530                 reason_code = info->reason_code;
3531                 locally_generated = info->locally_generated;
3532                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
3533                         reason_code,
3534                         locally_generated ? " (locally generated)" : "");
3535                 if (addr) {
3536                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3537                                 MAC2STR(addr));
3538                 }
3539                 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
3540                             ie, ie_len);
3541         }
3542
3543         wpa_reset_ft_completed(wpa_s->wpa);
3544
3545         wpas_event_disconnect(wpa_s, addr, reason_code,
3546                               locally_generated, ie, ie_len, 1);
3547 }
3548
3549
3550 static const char * reg_init_str(enum reg_change_initiator init)
3551 {
3552         switch (init) {
3553         case REGDOM_SET_BY_CORE:
3554                 return "CORE";
3555         case REGDOM_SET_BY_USER:
3556                 return "USER";
3557         case REGDOM_SET_BY_DRIVER:
3558                 return "DRIVER";
3559         case REGDOM_SET_BY_COUNTRY_IE:
3560                 return "COUNTRY_IE";
3561         case REGDOM_BEACON_HINT:
3562                 return "BEACON_HINT";
3563         }
3564         return "?";
3565 }
3566
3567
3568 static const char * reg_type_str(enum reg_type type)
3569 {
3570         switch (type) {
3571         case REGDOM_TYPE_UNKNOWN:
3572                 return "UNKNOWN";
3573         case REGDOM_TYPE_COUNTRY:
3574                 return "COUNTRY";
3575         case REGDOM_TYPE_WORLD:
3576                 return "WORLD";
3577         case REGDOM_TYPE_CUSTOM_WORLD:
3578                 return "CUSTOM_WORLD";
3579         case REGDOM_TYPE_INTERSECTION:
3580                 return "INTERSECTION";
3581         }
3582         return "?";
3583 }
3584
3585
3586 static void wpa_supplicant_update_channel_list(
3587         struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
3588 {
3589         struct wpa_supplicant *ifs;
3590         u8 dfs_domain;
3591
3592         /*
3593          * To allow backwards compatibility with higher level layers that
3594          * assumed the REGDOM_CHANGE event is sent over the initially added
3595          * interface. Find the highest parent of this interface and use it to
3596          * send the event.
3597          */
3598         for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
3599                 ;
3600
3601         wpa_msg(ifs, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
3602                 reg_init_str(info->initiator), reg_type_str(info->type),
3603                 info->alpha2[0] ? " alpha2=" : "",
3604                 info->alpha2[0] ? info->alpha2 : "");
3605
3606         if (wpa_s->drv_priv == NULL)
3607                 return; /* Ignore event during drv initialization */
3608
3609         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3610                          radio_list) {
3611                 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3612                            ifs->ifname);
3613                 free_hw_features(ifs);
3614                 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3615                         ifs, &ifs->hw.num_modes, &ifs->hw.flags, &dfs_domain);
3616
3617                 /* Restart PNO/sched_scan with updated channel list */
3618                 if (ifs->pno) {
3619                         wpas_stop_pno(ifs);
3620                         wpas_start_pno(ifs);
3621                 } else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
3622                         wpa_dbg(ifs, MSG_DEBUG,
3623                                 "Channel list changed - restart sched_scan");
3624                         wpas_scan_restart_sched_scan(ifs);
3625                 }
3626         }
3627
3628         wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
3629 }
3630
3631
3632 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
3633                                       const u8 *frame, size_t len, int freq,
3634                                       int rssi)
3635 {
3636         const struct ieee80211_mgmt *mgmt;
3637         const u8 *payload;
3638         size_t plen;
3639         u8 category;
3640
3641         if (len < IEEE80211_HDRLEN + 2)
3642                 return;
3643
3644         mgmt = (const struct ieee80211_mgmt *) frame;
3645         payload = frame + IEEE80211_HDRLEN;
3646         category = *payload++;
3647         plen = len - IEEE80211_HDRLEN - 1;
3648
3649         wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3650                 " Category=%u DataLen=%d freq=%d MHz",
3651                 MAC2STR(mgmt->sa), category, (int) plen, freq);
3652
3653         if (category == WLAN_ACTION_WMM) {
3654                 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3655                 return;
3656         }
3657
3658 #ifdef CONFIG_IEEE80211R
3659         if (category == WLAN_ACTION_FT) {
3660                 ft_rx_action(wpa_s, payload, plen);
3661                 return;
3662         }
3663 #endif /* CONFIG_IEEE80211R */
3664
3665 #ifdef CONFIG_IEEE80211W
3666 #ifdef CONFIG_SME
3667         if (category == WLAN_ACTION_SA_QUERY) {
3668                 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3669                 return;
3670         }
3671 #endif /* CONFIG_SME */
3672 #endif /* CONFIG_IEEE80211W */
3673
3674 #ifdef CONFIG_WNM
3675         if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3676                 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3677                 return;
3678         }
3679 #endif /* CONFIG_WNM */
3680
3681 #ifdef CONFIG_GAS
3682         if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3683              mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
3684             gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
3685                          mgmt->u.action.category,
3686                          payload, plen, freq) == 0)
3687                 return;
3688 #endif /* CONFIG_GAS */
3689
3690 #ifdef CONFIG_GAS_SERVER
3691         if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3692              mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
3693             gas_server_rx(wpa_s->gas_server, mgmt->da, mgmt->sa, mgmt->bssid,
3694                           mgmt->u.action.category,
3695                           payload, plen, freq) == 0)
3696                 return;
3697 #endif /* CONFIG_GAS_SERVER */
3698
3699 #ifdef CONFIG_TDLS
3700         if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3701             payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3702                 wpa_dbg(wpa_s, MSG_DEBUG,
3703                         "TDLS: Received Discovery Response from " MACSTR,
3704                         MAC2STR(mgmt->sa));
3705                 return;
3706         }
3707 #endif /* CONFIG_TDLS */
3708
3709 #ifdef CONFIG_INTERWORKING
3710         if (category == WLAN_ACTION_QOS && plen >= 1 &&
3711             payload[0] == QOS_QOS_MAP_CONFIG) {
3712                 const u8 *pos = payload + 1;
3713                 size_t qlen = plen - 1;
3714                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3715                         MACSTR, MAC2STR(mgmt->sa));
3716                 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3717                     qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3718                     pos[1] <= qlen - 2 && pos[1] >= 16)
3719                         wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3720                 return;
3721         }
3722 #endif /* CONFIG_INTERWORKING */
3723
3724         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3725             payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
3726                 wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
3727                                                           mgmt->da,
3728                                                           payload + 1,
3729                                                           plen - 1);
3730                 return;
3731         }
3732
3733         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3734             payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3735                 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3736                 return;
3737         }
3738
3739         if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3740             payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3741                 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3742                                                          payload + 1, plen - 1,
3743                                                          rssi);
3744                 return;
3745         }
3746
3747 #ifdef CONFIG_FST
3748         if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3749                 fst_rx_action(wpa_s->fst, mgmt, len);
3750                 return;
3751         }
3752 #endif /* CONFIG_FST */
3753
3754 #ifdef CONFIG_DPP
3755         if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
3756             payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
3757             WPA_GET_BE24(&payload[1]) == OUI_WFA &&
3758             payload[4] == DPP_OUI_TYPE) {
3759                 payload++;
3760                 plen--;
3761                 wpas_dpp_rx_action(wpa_s, mgmt->sa, payload, plen, freq);
3762                 return;
3763         }
3764 #endif /* CONFIG_DPP */
3765
3766         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3767                            category, payload, plen, freq);
3768         if (wpa_s->ifmsh)
3769                 mesh_mpm_action_rx(wpa_s, mgmt, len);
3770 }
3771
3772
3773 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3774                                              union wpa_event_data *event)
3775 {
3776         struct wpa_freq_range_list *list;
3777         char *str = NULL;
3778
3779         list = &event->freq_range;
3780
3781         if (list->num)
3782                 str = freq_range_list_str(list);
3783         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3784                 str ? str : "");
3785
3786 #ifdef CONFIG_P2P
3787         if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3788                 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3789                         __func__);
3790         } else {
3791                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
3792
3793                 /*
3794                  * The update channel flow will also take care of moving a GO
3795                  * from the unsafe frequency if needed.
3796                  */
3797                 wpas_p2p_update_channel_list(wpa_s,
3798                                              WPAS_P2P_CHANNEL_UPDATE_AVOID);
3799         }
3800 #endif /* CONFIG_P2P */
3801
3802         os_free(str);
3803 }
3804
3805
3806 static void wpa_supplicant_event_port_authorized(struct wpa_supplicant *wpa_s)
3807 {
3808         if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3809                 wpa_supplicant_cancel_auth_timeout(wpa_s);
3810                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3811                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3812                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3813         }
3814 }
3815
3816
3817 static unsigned int wpas_event_cac_ms(const struct wpa_supplicant *wpa_s,
3818                                       int freq)
3819 {
3820         size_t i;
3821         int j;
3822
3823         for (i = 0; i < wpa_s->hw.num_modes; i++) {
3824                 const struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
3825
3826                 for (j = 0; j < mode->num_channels; j++) {
3827                         const struct hostapd_channel_data *chan;
3828
3829                         chan = &mode->channels[j];
3830                         if (chan->freq == freq)
3831                                 return chan->dfs_cac_ms;
3832                 }
3833         }
3834
3835         return 0;
3836 }
3837
3838
3839 static void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
3840                                        struct dfs_event *radar)
3841 {
3842 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
3843         if (wpa_s->ap_iface) {
3844                 wpas_ap_event_dfs_cac_started(wpa_s, radar);
3845         } else
3846 #endif /* NEED_AP_MLME && CONFIG_AP */
3847         {
3848                 unsigned int cac_time = wpas_event_cac_ms(wpa_s, radar->freq);
3849
3850                 cac_time /= 1000; /* convert from ms to sec */
3851                 if (!cac_time)
3852                         cac_time = 10 * 60; /* max timeout: 10 minutes */
3853
3854                 /* Restart auth timeout: CAC time added to initial timeout */
3855                 wpas_auth_timeout_restart(wpa_s, cac_time);
3856         }
3857 }
3858
3859
3860 static void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
3861                                         struct dfs_event *radar)
3862 {
3863 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
3864         if (wpa_s->ap_iface) {
3865                 wpas_ap_event_dfs_cac_finished(wpa_s, radar);
3866         } else
3867 #endif /* NEED_AP_MLME && CONFIG_AP */
3868         {
3869                 /* Restart auth timeout with original value after CAC is
3870                  * finished */
3871                 wpas_auth_timeout_restart(wpa_s, 0);
3872         }
3873 }
3874
3875
3876 static void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
3877                                        struct dfs_event *radar)
3878 {
3879 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
3880         if (wpa_s->ap_iface) {
3881                 wpas_ap_event_dfs_cac_aborted(wpa_s, radar);
3882         } else
3883 #endif /* NEED_AP_MLME && CONFIG_AP */
3884         {
3885                 /* Restart auth timeout with original value after CAC is
3886                  * aborted */
3887                 wpas_auth_timeout_restart(wpa_s, 0);
3888         }
3889 }
3890
3891
3892 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3893                                             union wpa_event_data *data)
3894 {
3895         wpa_dbg(wpa_s, MSG_DEBUG,
3896                 "Connection authorized by device, previous state %d",
3897                 wpa_s->wpa_state);
3898
3899         wpa_supplicant_event_port_authorized(wpa_s);
3900
3901         wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3902         wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
3903                                data->assoc_info.ptk_kck_len,
3904                                data->assoc_info.ptk_kek,
3905                                data->assoc_info.ptk_kek_len);
3906 #ifdef CONFIG_FILS
3907         if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
3908                 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
3909                 const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
3910
3911                 /* Update ERP next sequence number */
3912                 eapol_sm_update_erp_next_seq_num(
3913                         wpa_s->eapol, data->assoc_info.fils_erp_next_seq_num);
3914
3915                 if (data->assoc_info.fils_pmk && data->assoc_info.fils_pmkid) {
3916                         /* Add the new PMK and PMKID to the PMKSA cache */
3917                         wpa_sm_pmksa_cache_add(wpa_s->wpa,
3918                                                data->assoc_info.fils_pmk,
3919                                                data->assoc_info.fils_pmk_len,
3920                                                data->assoc_info.fils_pmkid,
3921                                                wpa_s->bssid, fils_cache_id);
3922                 } else if (data->assoc_info.fils_pmkid) {
3923                         /* Update the current PMKSA used for this connection */
3924                         pmksa_cache_set_current(wpa_s->wpa,
3925                                                 data->assoc_info.fils_pmkid,
3926                                                 NULL, NULL, 0, NULL, 0);
3927                 }
3928         }
3929 #endif /* CONFIG_FILS */
3930 }
3931
3932
3933 static void wpas_event_assoc_reject(struct wpa_supplicant *wpa_s,
3934                                     union wpa_event_data *data)
3935 {
3936         const u8 *bssid = data->assoc_reject.bssid;
3937
3938         if (!bssid || is_zero_ether_addr(bssid))
3939                 bssid = wpa_s->pending_bssid;
3940
3941         if (data->assoc_reject.bssid)
3942                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3943                         "bssid=" MACSTR " status_code=%u%s%s%s",
3944                         MAC2STR(data->assoc_reject.bssid),
3945                         data->assoc_reject.status_code,
3946                         data->assoc_reject.timed_out ? " timeout" : "",
3947                         data->assoc_reject.timeout_reason ? "=" : "",
3948                         data->assoc_reject.timeout_reason ?
3949                         data->assoc_reject.timeout_reason : "");
3950         else
3951                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3952                         "status_code=%u%s%s%s",
3953                         data->assoc_reject.status_code,
3954                         data->assoc_reject.timed_out ? " timeout" : "",
3955                         data->assoc_reject.timeout_reason ? "=" : "",
3956                         data->assoc_reject.timeout_reason ?
3957                         data->assoc_reject.timeout_reason : "");
3958         wpa_s->assoc_status_code = data->assoc_reject.status_code;
3959         wpas_notify_assoc_status_code(wpa_s);
3960
3961 #ifdef CONFIG_OWE
3962         if (data->assoc_reject.status_code ==
3963             WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
3964             wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
3965             wpa_s->current_ssid &&
3966             wpa_s->current_ssid->owe_group == 0 &&
3967             wpa_s->last_owe_group != 21) {
3968                 struct wpa_ssid *ssid = wpa_s->current_ssid;
3969                 struct wpa_bss *bss = wpa_s->current_bss;
3970
3971                 if (!bss) {
3972                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
3973                         if (!bss) {
3974                                 wpas_connection_failed(wpa_s, bssid);
3975                                 wpa_supplicant_mark_disassoc(wpa_s);
3976                                 return;
3977                         }
3978                 }
3979                 wpa_printf(MSG_DEBUG, "OWE: Try next supported DH group");
3980                 wpas_connect_work_done(wpa_s);
3981                 wpa_supplicant_mark_disassoc(wpa_s);
3982                 wpa_supplicant_connect(wpa_s, bss, ssid);
3983                 return;
3984         }
3985 #endif /* CONFIG_OWE */
3986
3987         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
3988                 sme_event_assoc_reject(wpa_s, data);
3989                 return;
3990         }
3991
3992         /* Driver-based SME cases */
3993
3994 #ifdef CONFIG_SAE
3995         if (wpa_s->current_ssid &&
3996             wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt) &&
3997             !data->assoc_reject.timed_out) {
3998                 wpa_dbg(wpa_s, MSG_DEBUG, "SAE: Drop PMKSA cache entry");
3999                 wpa_sm_aborted_cached(wpa_s->wpa);
4000                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
4001         }
4002 #endif /* CONFIG_SAE */
4003
4004 #ifdef CONFIG_DPP
4005         if (wpa_s->current_ssid &&
4006             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_DPP &&
4007             !data->assoc_reject.timed_out) {
4008                 wpa_dbg(wpa_s, MSG_DEBUG, "DPP: Drop PMKSA cache entry");
4009                 wpa_sm_aborted_cached(wpa_s->wpa);
4010                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
4011         }
4012 #endif /* CONFIG_DPP */
4013
4014 #ifdef CONFIG_FILS
4015         /* Update ERP next sequence number */
4016         if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
4017                 eapol_sm_update_erp_next_seq_num(
4018                         wpa_s->eapol,
4019                         data->assoc_reject.fils_erp_next_seq_num);
4020                 fils_connection_failure(wpa_s);
4021         }
4022 #endif /* CONFIG_FILS */
4023
4024         wpas_connection_failed(wpa_s, bssid);
4025         wpa_supplicant_mark_disassoc(wpa_s);
4026 }
4027
4028
4029 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
4030                           union wpa_event_data *data)
4031 {
4032         struct wpa_supplicant *wpa_s = ctx;
4033         int resched;
4034 #ifndef CONFIG_NO_STDOUT_DEBUG
4035         int level = MSG_DEBUG;
4036 #endif /* CONFIG_NO_STDOUT_DEBUG */
4037
4038         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
4039             event != EVENT_INTERFACE_ENABLED &&
4040             event != EVENT_INTERFACE_STATUS &&
4041             event != EVENT_SCAN_RESULTS &&
4042             event != EVENT_SCHED_SCAN_STOPPED) {
4043                 wpa_dbg(wpa_s, MSG_DEBUG,
4044                         "Ignore event %s (%d) while interface is disabled",
4045                         event_to_string(event), event);
4046                 return;
4047         }
4048
4049 #ifndef CONFIG_NO_STDOUT_DEBUG
4050         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
4051                 const struct ieee80211_hdr *hdr;
4052                 u16 fc;
4053                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
4054                 fc = le_to_host16(hdr->frame_control);
4055                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4056                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
4057                         level = MSG_EXCESSIVE;
4058         }
4059
4060         wpa_dbg(wpa_s, level, "Event %s (%d) received",
4061                 event_to_string(event), event);
4062 #endif /* CONFIG_NO_STDOUT_DEBUG */
4063
4064         switch (event) {
4065         case EVENT_AUTH:
4066 #ifdef CONFIG_FST
4067                 if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
4068                                           data->auth.ies_len))
4069                         wpa_printf(MSG_DEBUG,
4070                                    "FST: MB IEs updated from auth IE");
4071 #endif /* CONFIG_FST */
4072                 sme_event_auth(wpa_s, data);
4073                 break;
4074         case EVENT_ASSOC:
4075 #ifdef CONFIG_TESTING_OPTIONS
4076                 if (wpa_s->ignore_auth_resp) {
4077                         wpa_printf(MSG_INFO,
4078                                    "EVENT_ASSOC - ignore_auth_resp active!");
4079                         break;
4080                 }
4081                 if (wpa_s->testing_resend_assoc) {
4082                         wpa_printf(MSG_INFO,
4083                                    "EVENT_DEAUTH - testing_resend_assoc");
4084                         break;
4085                 }
4086 #endif /* CONFIG_TESTING_OPTIONS */
4087                 wpa_supplicant_event_assoc(wpa_s, data);
4088                 wpa_s->assoc_status_code = WLAN_STATUS_SUCCESS;
4089                 if (data &&
4090                     (data->assoc_info.authorized ||
4091                      (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
4092                       wpa_fils_is_completed(wpa_s->wpa))))
4093                         wpa_supplicant_event_assoc_auth(wpa_s, data);
4094                 if (data) {
4095                         wpa_msg(wpa_s, MSG_INFO,
4096                                 WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
4097                                 data->assoc_info.subnet_status);
4098                 }
4099                 break;
4100         case EVENT_DISASSOC:
4101                 wpas_event_disassoc(wpa_s,
4102                                     data ? &data->disassoc_info : NULL);
4103                 break;
4104         case EVENT_DEAUTH:
4105 #ifdef CONFIG_TESTING_OPTIONS
4106                 if (wpa_s->ignore_auth_resp) {
4107                         wpa_printf(MSG_INFO,
4108                                    "EVENT_DEAUTH - ignore_auth_resp active!");
4109                         break;
4110                 }
4111                 if (wpa_s->testing_resend_assoc) {
4112                         wpa_printf(MSG_INFO,
4113                                    "EVENT_DEAUTH - testing_resend_assoc");
4114                         break;
4115                 }
4116 #endif /* CONFIG_TESTING_OPTIONS */
4117                 wpas_event_deauth(wpa_s,
4118                                   data ? &data->deauth_info : NULL);
4119                 break;
4120         case EVENT_MICHAEL_MIC_FAILURE:
4121                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
4122                 break;
4123 #ifndef CONFIG_NO_SCAN_PROCESSING
4124         case EVENT_SCAN_STARTED:
4125                 if (wpa_s->own_scan_requested ||
4126                     (data && !data->scan_info.external_scan)) {
4127                         struct os_reltime diff;
4128
4129                         os_get_reltime(&wpa_s->scan_start_time);
4130                         os_reltime_sub(&wpa_s->scan_start_time,
4131                                        &wpa_s->scan_trigger_time, &diff);
4132                         wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
4133                                 diff.sec, diff.usec);
4134                         wpa_s->own_scan_requested = 0;
4135                         wpa_s->own_scan_running = 1;
4136                         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
4137                             wpa_s->manual_scan_use_id) {
4138                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
4139                                              WPA_EVENT_SCAN_STARTED "id=%u",
4140                                              wpa_s->manual_scan_id);
4141                         } else {
4142                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
4143                                              WPA_EVENT_SCAN_STARTED);
4144                         }
4145                 } else {
4146                         wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
4147                         wpa_s->radio->external_scan_running = 1;
4148                         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
4149                 }
4150                 break;
4151         case EVENT_SCAN_RESULTS:
4152                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4153                         wpa_s->scan_res_handler = NULL;
4154                         wpa_s->own_scan_running = 0;
4155                         wpa_s->radio->external_scan_running = 0;
4156                         wpa_s->last_scan_req = NORMAL_SCAN_REQ;
4157                         break;
4158                 }
4159
4160                 if (!(data && data->scan_info.external_scan) &&
4161                     os_reltime_initialized(&wpa_s->scan_start_time)) {
4162                         struct os_reltime now, diff;
4163                         os_get_reltime(&now);
4164                         os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
4165                         wpa_s->scan_start_time.sec = 0;
4166                         wpa_s->scan_start_time.usec = 0;
4167                         wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
4168                                 diff.sec, diff.usec);
4169                 }
4170                 if (wpa_supplicant_event_scan_results(wpa_s, data))
4171                         break; /* interface may have been removed */
4172                 if (!(data && data->scan_info.external_scan))
4173                         wpa_s->own_scan_running = 0;
4174                 if (data && data->scan_info.nl_scan_event)
4175                         wpa_s->radio->external_scan_running = 0;
4176                 radio_work_check_next(wpa_s);
4177                 break;
4178 #endif /* CONFIG_NO_SCAN_PROCESSING */
4179         case EVENT_ASSOCINFO:
4180                 wpa_supplicant_event_associnfo(wpa_s, data);
4181                 break;
4182         case EVENT_INTERFACE_STATUS:
4183                 wpa_supplicant_event_interface_status(wpa_s, data);
4184                 break;
4185         case EVENT_PMKID_CANDIDATE:
4186                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
4187                 break;
4188 #ifdef CONFIG_TDLS
4189         case EVENT_TDLS:
4190                 wpa_supplicant_event_tdls(wpa_s, data);
4191                 break;
4192 #endif /* CONFIG_TDLS */
4193 #ifdef CONFIG_WNM
4194         case EVENT_WNM:
4195                 wpa_supplicant_event_wnm(wpa_s, data);
4196                 break;
4197 #endif /* CONFIG_WNM */
4198 #ifdef CONFIG_IEEE80211R
4199         case EVENT_FT_RESPONSE:
4200                 wpa_supplicant_event_ft_response(wpa_s, data);
4201                 break;
4202 #endif /* CONFIG_IEEE80211R */
4203 #ifdef CONFIG_IBSS_RSN
4204         case EVENT_IBSS_RSN_START:
4205                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
4206                 break;
4207 #endif /* CONFIG_IBSS_RSN */
4208         case EVENT_ASSOC_REJECT:
4209                 wpas_event_assoc_reject(wpa_s, data);
4210                 break;
4211         case EVENT_AUTH_TIMED_OUT:
4212                 /* It is possible to get this event from earlier connection */
4213                 if (wpa_s->current_ssid &&
4214                     wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
4215                         wpa_dbg(wpa_s, MSG_DEBUG,
4216                                 "Ignore AUTH_TIMED_OUT in mesh configuration");
4217                         break;
4218                 }
4219                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
4220                         sme_event_auth_timed_out(wpa_s, data);
4221                 break;
4222         case EVENT_ASSOC_TIMED_OUT:
4223                 /* It is possible to get this event from earlier connection */
4224                 if (wpa_s->current_ssid &&
4225                     wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
4226                         wpa_dbg(wpa_s, MSG_DEBUG,
4227                                 "Ignore ASSOC_TIMED_OUT in mesh configuration");
4228                         break;
4229                 }
4230                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
4231                         sme_event_assoc_timed_out(wpa_s, data);
4232                 break;
4233         case EVENT_TX_STATUS:
4234                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
4235                         " type=%d stype=%d",
4236                         MAC2STR(data->tx_status.dst),
4237                         data->tx_status.type, data->tx_status.stype);
4238 #ifdef CONFIG_AP
4239                 if (wpa_s->ap_iface == NULL) {
4240 #ifdef CONFIG_OFFCHANNEL
4241                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
4242                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
4243                                 offchannel_send_action_tx_status(
4244                                         wpa_s, data->tx_status.dst,
4245                                         data->tx_status.data,
4246                                         data->tx_status.data_len,
4247                                         data->tx_status.ack ?
4248                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
4249                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
4250 #endif /* CONFIG_OFFCHANNEL */
4251                         break;
4252                 }
4253 #endif /* CONFIG_AP */
4254 #ifdef CONFIG_OFFCHANNEL
4255                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
4256                         MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
4257                 /*
4258                  * Catch TX status events for Action frames we sent via group
4259                  * interface in GO mode, or via standalone AP interface.
4260                  * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
4261                  * except when the primary interface is used as a GO interface
4262                  * (for drivers which do not have group interface concurrency)
4263                  */
4264                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
4265                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
4266                     os_memcmp(wpa_s->p2pdev->pending_action_dst,
4267                               data->tx_status.dst, ETH_ALEN) == 0) {
4268                         offchannel_send_action_tx_status(
4269                                 wpa_s->p2pdev, data->tx_status.dst,
4270                                 data->tx_status.data,
4271                                 data->tx_status.data_len,
4272                                 data->tx_status.ack ?
4273                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
4274                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
4275                         break;
4276                 }
4277 #endif /* CONFIG_OFFCHANNEL */
4278 #ifdef CONFIG_AP
4279                 switch (data->tx_status.type) {
4280                 case WLAN_FC_TYPE_MGMT:
4281                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
4282                                       data->tx_status.data_len,
4283                                       data->tx_status.stype,
4284                                       data->tx_status.ack);
4285                         break;
4286                 case WLAN_FC_TYPE_DATA:
4287                         ap_tx_status(wpa_s, data->tx_status.dst,
4288                                      data->tx_status.data,
4289                                      data->tx_status.data_len,
4290                                      data->tx_status.ack);
4291                         break;
4292                 }
4293 #endif /* CONFIG_AP */
4294                 break;
4295 #ifdef CONFIG_AP
4296         case EVENT_EAPOL_TX_STATUS:
4297                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
4298                                    data->eapol_tx_status.data,
4299                                    data->eapol_tx_status.data_len,
4300                                    data->eapol_tx_status.ack);
4301                 break;
4302         case EVENT_DRIVER_CLIENT_POLL_OK:
4303                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
4304                 break;
4305         case EVENT_RX_FROM_UNKNOWN:
4306                 if (wpa_s->ap_iface == NULL)
4307                         break;
4308                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
4309                                        data->rx_from_unknown.wds);
4310                 break;
4311 #endif /* CONFIG_AP */
4312         case EVENT_CH_SWITCH:
4313                 if (!data || !wpa_s->current_ssid)
4314                         break;
4315
4316                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
4317                         "freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
4318                         data->ch_switch.freq,
4319                         data->ch_switch.ht_enabled,
4320                         data->ch_switch.ch_offset,
4321                         channel_width_to_string(data->ch_switch.ch_width),
4322                         data->ch_switch.cf1,
4323                         data->ch_switch.cf2);
4324
4325                 wpa_s->assoc_freq = data->ch_switch.freq;
4326                 wpa_s->current_ssid->frequency = data->ch_switch.freq;
4327
4328 #ifdef CONFIG_AP
4329                 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4330                     wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
4331                     wpa_s->current_ssid->mode ==
4332                     WPAS_MODE_P2P_GROUP_FORMATION) {
4333                         wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
4334                                           data->ch_switch.ht_enabled,
4335                                           data->ch_switch.ch_offset,
4336                                           data->ch_switch.ch_width,
4337                                           data->ch_switch.cf1,
4338                                           data->ch_switch.cf2);
4339                 }
4340 #endif /* CONFIG_AP */
4341
4342                 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
4343                 wnm_clear_coloc_intf_reporting(wpa_s);
4344                 break;
4345 #ifdef CONFIG_AP
4346 #ifdef NEED_AP_MLME
4347         case EVENT_DFS_RADAR_DETECTED:
4348                 if (data)
4349                         wpas_ap_event_dfs_radar_detected(wpa_s,
4350                                                          &data->dfs_event);
4351                 break;
4352         case EVENT_DFS_NOP_FINISHED:
4353                 if (data)
4354                         wpas_ap_event_dfs_cac_nop_finished(wpa_s,
4355                                                            &data->dfs_event);
4356                 break;
4357 #endif /* NEED_AP_MLME */
4358 #endif /* CONFIG_AP */
4359         case EVENT_DFS_CAC_STARTED:
4360                 if (data)
4361                         wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
4362                 break;
4363         case EVENT_DFS_CAC_FINISHED:
4364                 if (data)
4365                         wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
4366                 break;
4367         case EVENT_DFS_CAC_ABORTED:
4368                 if (data)
4369                         wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
4370                 break;
4371         case EVENT_RX_MGMT: {
4372                 u16 fc, stype;
4373                 const struct ieee80211_mgmt *mgmt;
4374
4375 #ifdef CONFIG_TESTING_OPTIONS
4376                 if (wpa_s->ext_mgmt_frame_handling) {
4377                         struct rx_mgmt *rx = &data->rx_mgmt;
4378                         size_t hex_len = 2 * rx->frame_len + 1;
4379                         char *hex = os_malloc(hex_len);
4380                         if (hex) {
4381                                 wpa_snprintf_hex(hex, hex_len,
4382                                                  rx->frame, rx->frame_len);
4383                                 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
4384                                         rx->freq, rx->datarate, rx->ssi_signal,
4385                                         hex);
4386                                 os_free(hex);
4387                         }
4388                         break;
4389                 }
4390 #endif /* CONFIG_TESTING_OPTIONS */
4391
4392                 mgmt = (const struct ieee80211_mgmt *)
4393                         data->rx_mgmt.frame;
4394                 fc = le_to_host16(mgmt->frame_control);
4395                 stype = WLAN_FC_GET_STYPE(fc);
4396
4397 #ifdef CONFIG_AP
4398                 if (wpa_s->ap_iface == NULL) {
4399 #endif /* CONFIG_AP */
4400 #ifdef CONFIG_P2P
4401                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
4402                             data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
4403                                 const u8 *src = mgmt->sa;
4404                                 const u8 *ie;
4405                                 size_t ie_len;
4406
4407                                 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
4408                                 ie_len = data->rx_mgmt.frame_len -
4409                                         IEEE80211_HDRLEN;
4410                                 wpas_p2p_probe_req_rx(
4411                                         wpa_s, src, mgmt->da,
4412                                         mgmt->bssid, ie, ie_len,
4413                                         data->rx_mgmt.freq,
4414                                         data->rx_mgmt.ssi_signal);
4415                                 break;
4416                         }
4417 #endif /* CONFIG_P2P */
4418 #ifdef CONFIG_IBSS_RSN
4419                         if (wpa_s->current_ssid &&
4420                             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
4421                             stype == WLAN_FC_STYPE_AUTH &&
4422                             data->rx_mgmt.frame_len >= 30) {
4423                                 wpa_supplicant_event_ibss_auth(wpa_s, data);
4424                                 break;
4425                         }
4426 #endif /* CONFIG_IBSS_RSN */
4427
4428                         if (stype == WLAN_FC_STYPE_ACTION) {
4429                                 wpas_event_rx_mgmt_action(
4430                                         wpa_s, data->rx_mgmt.frame,
4431                                         data->rx_mgmt.frame_len,
4432                                         data->rx_mgmt.freq,
4433                                         data->rx_mgmt.ssi_signal);
4434                                 break;
4435                         }
4436
4437                         if (wpa_s->ifmsh) {
4438                                 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
4439                                 break;
4440                         }
4441
4442 #ifdef CONFIG_SAE
4443                         if (stype == WLAN_FC_STYPE_AUTH &&
4444                             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
4445                             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
4446                                 sme_external_auth_mgmt_rx(
4447                                         wpa_s, data->rx_mgmt.frame,
4448                                         data->rx_mgmt.frame_len);
4449                                 break;
4450                         }
4451 #endif /* CONFIG_SAE */
4452                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
4453                                 "management frame in non-AP mode");
4454                         break;
4455 #ifdef CONFIG_AP
4456                 }
4457
4458                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
4459                     data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
4460                         const u8 *ie;
4461                         size_t ie_len;
4462
4463                         ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
4464                         ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
4465
4466                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
4467                                          mgmt->bssid, ie, ie_len,
4468                                          data->rx_mgmt.ssi_signal);
4469                 }
4470
4471                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
4472 #endif /* CONFIG_AP */
4473                 break;
4474                 }
4475         case EVENT_RX_PROBE_REQ:
4476                 if (data->rx_probe_req.sa == NULL ||
4477                     data->rx_probe_req.ie == NULL)
4478                         break;
4479 #ifdef CONFIG_AP
4480                 if (wpa_s->ap_iface) {
4481                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
4482                                              data->rx_probe_req.sa,
4483                                              data->rx_probe_req.da,
4484                                              data->rx_probe_req.bssid,
4485                                              data->rx_probe_req.ie,
4486                                              data->rx_probe_req.ie_len,
4487                                              data->rx_probe_req.ssi_signal);
4488                         break;
4489                 }
4490 #endif /* CONFIG_AP */
4491                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
4492                                       data->rx_probe_req.da,
4493                                       data->rx_probe_req.bssid,
4494                                       data->rx_probe_req.ie,
4495                                       data->rx_probe_req.ie_len,
4496                                       0,
4497                                       data->rx_probe_req.ssi_signal);
4498                 break;
4499         case EVENT_REMAIN_ON_CHANNEL:
4500 #ifdef CONFIG_OFFCHANNEL
4501                 offchannel_remain_on_channel_cb(
4502                         wpa_s, data->remain_on_channel.freq,
4503                         data->remain_on_channel.duration);
4504 #endif /* CONFIG_OFFCHANNEL */
4505                 wpas_p2p_remain_on_channel_cb(
4506                         wpa_s, data->remain_on_channel.freq,
4507                         data->remain_on_channel.duration);
4508                 break;
4509         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
4510 #ifdef CONFIG_OFFCHANNEL
4511                 offchannel_cancel_remain_on_channel_cb(
4512                         wpa_s, data->remain_on_channel.freq);
4513 #endif /* CONFIG_OFFCHANNEL */
4514                 wpas_p2p_cancel_remain_on_channel_cb(
4515                         wpa_s, data->remain_on_channel.freq);
4516 #ifdef CONFIG_DPP
4517                 wpas_dpp_cancel_remain_on_channel_cb(
4518                         wpa_s, data->remain_on_channel.freq);
4519 #endif /* CONFIG_DPP */
4520                 break;
4521         case EVENT_EAPOL_RX:
4522                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
4523                                         data->eapol_rx.data,
4524                                         data->eapol_rx.data_len);
4525                 break;
4526         case EVENT_SIGNAL_CHANGE:
4527                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
4528                         "above=%d signal=%d noise=%d txrate=%d",
4529                         data->signal_change.above_threshold,
4530                         data->signal_change.current_signal,
4531                         data->signal_change.current_noise,
4532                         data->signal_change.current_txrate);
4533                 wpa_bss_update_level(wpa_s->current_bss,
4534                                      data->signal_change.current_signal);
4535                 bgscan_notify_signal_change(
4536                         wpa_s, data->signal_change.above_threshold,
4537                         data->signal_change.current_signal,
4538                         data->signal_change.current_noise,
4539                         data->signal_change.current_txrate);
4540                 break;
4541         case EVENT_INTERFACE_MAC_CHANGED:
4542                 wpa_supplicant_update_mac_addr(wpa_s);
4543                 break;
4544         case EVENT_INTERFACE_ENABLED:
4545                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
4546                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4547                         wpa_supplicant_update_mac_addr(wpa_s);
4548                         if (wpa_s->p2p_mgmt) {
4549                                 wpa_supplicant_set_state(wpa_s,
4550                                                          WPA_DISCONNECTED);
4551                                 break;
4552                         }
4553
4554 #ifdef CONFIG_AP
4555                         if (!wpa_s->ap_iface) {
4556                                 wpa_supplicant_set_state(wpa_s,
4557                                                          WPA_DISCONNECTED);
4558                                 wpa_s->scan_req = NORMAL_SCAN_REQ;
4559                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
4560                         } else
4561                                 wpa_supplicant_set_state(wpa_s,
4562                                                          WPA_COMPLETED);
4563 #else /* CONFIG_AP */
4564                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4565                         wpa_supplicant_req_scan(wpa_s, 0, 0);
4566 #endif /* CONFIG_AP */
4567                 }
4568                 break;
4569         case EVENT_INTERFACE_DISABLED:
4570                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
4571 #ifdef CONFIG_P2P
4572                 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
4573                     (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
4574                      wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
4575                         /*
4576                          * Mark interface disabled if this happens to end up not
4577                          * being removed as a separate P2P group interface.
4578                          */
4579                         wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4580                         /*
4581                          * The interface was externally disabled. Remove
4582                          * it assuming an external entity will start a
4583                          * new session if needed.
4584                          */
4585                         if (wpa_s->current_ssid &&
4586                             wpa_s->current_ssid->p2p_group)
4587                                 wpas_p2p_interface_unavailable(wpa_s);
4588                         else
4589                                 wpas_p2p_disconnect(wpa_s);
4590                         /*
4591                          * wpa_s instance may have been freed, so must not use
4592                          * it here anymore.
4593                          */
4594                         break;
4595                 }
4596                 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
4597                     p2p_in_progress(wpa_s->global->p2p) > 1) {
4598                         /* This radio work will be cancelled, so clear P2P
4599                          * state as well.
4600                          */
4601                         p2p_stop_find(wpa_s->global->p2p);
4602                 }
4603 #endif /* CONFIG_P2P */
4604
4605                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
4606                         /*
4607                          * Indicate disconnection to keep ctrl_iface events
4608                          * consistent.
4609                          */
4610                         wpa_supplicant_event_disassoc(
4611                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
4612                 }
4613                 wpa_supplicant_mark_disassoc(wpa_s);
4614                 wpa_bss_flush(wpa_s);
4615                 radio_remove_works(wpa_s, NULL, 0);
4616
4617                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4618                 break;
4619         case EVENT_CHANNEL_LIST_CHANGED:
4620                 wpa_supplicant_update_channel_list(
4621                         wpa_s, &data->channel_list_changed);
4622                 break;
4623         case EVENT_INTERFACE_UNAVAILABLE:
4624                 wpas_p2p_interface_unavailable(wpa_s);
4625                 break;
4626         case EVENT_BEST_CHANNEL:
4627                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
4628                         "(%d %d %d)",
4629                         data->best_chan.freq_24, data->best_chan.freq_5,
4630                         data->best_chan.freq_overall);
4631                 wpa_s->best_24_freq = data->best_chan.freq_24;
4632                 wpa_s->best_5_freq = data->best_chan.freq_5;
4633                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
4634                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
4635                                               data->best_chan.freq_5,
4636                                               data->best_chan.freq_overall);
4637                 break;
4638         case EVENT_UNPROT_DEAUTH:
4639                 wpa_supplicant_event_unprot_deauth(wpa_s,
4640                                                    &data->unprot_deauth);
4641                 break;
4642         case EVENT_UNPROT_DISASSOC:
4643                 wpa_supplicant_event_unprot_disassoc(wpa_s,
4644                                                      &data->unprot_disassoc);
4645                 break;
4646         case EVENT_STATION_LOW_ACK:
4647 #ifdef CONFIG_AP
4648                 if (wpa_s->ap_iface && data)
4649                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
4650                                                   data->low_ack.addr);
4651 #endif /* CONFIG_AP */
4652 #ifdef CONFIG_TDLS
4653                 if (data)
4654                         wpa_tdls_disable_unreachable_link(wpa_s->wpa,
4655                                                           data->low_ack.addr);
4656 #endif /* CONFIG_TDLS */
4657                 break;
4658         case EVENT_IBSS_PEER_LOST:
4659 #ifdef CONFIG_IBSS_RSN
4660                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
4661 #endif /* CONFIG_IBSS_RSN */
4662                 break;
4663         case EVENT_DRIVER_GTK_REKEY:
4664                 if (os_memcmp(data->driver_gtk_rekey.bssid,
4665                               wpa_s->bssid, ETH_ALEN))
4666                         break;
4667                 if (!wpa_s->wpa)
4668                         break;
4669                 wpa_sm_update_replay_ctr(wpa_s->wpa,
4670                                          data->driver_gtk_rekey.replay_ctr);
4671                 break;
4672         case EVENT_SCHED_SCAN_STOPPED:
4673                 wpa_s->sched_scanning = 0;
4674                 resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
4675                 wpa_supplicant_notify_scanning(wpa_s, 0);
4676
4677                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4678                         break;
4679
4680                 /*
4681                  * If the driver stopped scanning without being requested to,
4682                  * request a new scan to continue scanning for networks.
4683                  */
4684                 if (!wpa_s->sched_scan_stop_req &&
4685                     wpa_s->wpa_state == WPA_SCANNING) {
4686                         wpa_dbg(wpa_s, MSG_DEBUG,
4687                                 "Restart scanning after unexpected sched_scan stop event");
4688                         wpa_supplicant_req_scan(wpa_s, 1, 0);
4689                         break;
4690                 }
4691
4692                 wpa_s->sched_scan_stop_req = 0;
4693
4694                 /*
4695                  * Start a new sched scan to continue searching for more SSIDs
4696                  * either if timed out or PNO schedule scan is pending.
4697                  */
4698                 if (wpa_s->sched_scan_timed_out) {
4699                         wpa_supplicant_req_sched_scan(wpa_s);
4700                 } else if (wpa_s->pno_sched_pending) {
4701                         wpa_s->pno_sched_pending = 0;
4702                         wpas_start_pno(wpa_s);
4703                 } else if (resched) {
4704                         wpa_supplicant_req_scan(wpa_s, 0, 0);
4705                 }
4706
4707                 break;
4708         case EVENT_WPS_BUTTON_PUSHED:
4709 #ifdef CONFIG_WPS
4710                 wpas_wps_start_pbc(wpa_s, NULL, 0);
4711 #endif /* CONFIG_WPS */
4712                 break;
4713         case EVENT_AVOID_FREQUENCIES:
4714                 wpa_supplicant_notify_avoid_freq(wpa_s, data);
4715                 break;
4716         case EVENT_CONNECT_FAILED_REASON:
4717 #ifdef CONFIG_AP
4718                 if (!wpa_s->ap_iface || !data)
4719                         break;
4720                 hostapd_event_connect_failed_reason(
4721                         wpa_s->ap_iface->bss[0],
4722                         data->connect_failed_reason.addr,
4723                         data->connect_failed_reason.code);
4724 #endif /* CONFIG_AP */
4725                 break;
4726         case EVENT_NEW_PEER_CANDIDATE:
4727 #ifdef CONFIG_MESH
4728                 if (!wpa_s->ifmsh || !data)
4729                         break;
4730                 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
4731                                      data->mesh_peer.ies,
4732                                      data->mesh_peer.ie_len);
4733 #endif /* CONFIG_MESH */
4734                 break;
4735         case EVENT_SURVEY:
4736 #ifdef CONFIG_AP
4737                 if (!wpa_s->ap_iface)
4738                         break;
4739                 hostapd_event_get_survey(wpa_s->ap_iface,
4740                                          &data->survey_results);
4741 #endif /* CONFIG_AP */
4742                 break;
4743         case EVENT_ACS_CHANNEL_SELECTED:
4744 #ifdef CONFIG_AP
4745 #ifdef CONFIG_ACS
4746                 if (!wpa_s->ap_iface)
4747                         break;
4748                 hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
4749                                              &data->acs_selected_channels);
4750 #endif /* CONFIG_ACS */
4751 #endif /* CONFIG_AP */
4752                 break;
4753         case EVENT_P2P_LO_STOP:
4754 #ifdef CONFIG_P2P
4755                 wpa_s->p2p_lo_started = 0;
4756                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
4757                         P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
4758                         data->p2p_lo_stop.reason_code);
4759 #endif /* CONFIG_P2P */
4760                 break;
4761         case EVENT_BEACON_LOSS:
4762                 if (!wpa_s->current_bss || !wpa_s->current_ssid)
4763                         break;
4764                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
4765                 bgscan_notify_beacon_loss(wpa_s);
4766                 break;
4767         case EVENT_EXTERNAL_AUTH:
4768 #ifdef CONFIG_SAE
4769                 if (!wpa_s->current_ssid) {
4770                         wpa_printf(MSG_DEBUG, "SAE: current_ssid is NULL");
4771                         break;
4772                 }
4773                 sme_external_auth_trigger(wpa_s, data);
4774 #endif /* CONFIG_SAE */
4775                 break;
4776         case EVENT_PORT_AUTHORIZED:
4777                 wpa_supplicant_event_port_authorized(wpa_s);
4778                 break;
4779         case EVENT_STATION_OPMODE_CHANGED:
4780 #ifdef CONFIG_AP
4781                 if (!wpa_s->ap_iface || !data)
4782                         break;
4783
4784                 hostapd_event_sta_opmode_changed(wpa_s->ap_iface->bss[0],
4785                                                  data->sta_opmode.addr,
4786                                                  data->sta_opmode.smps_mode,
4787                                                  data->sta_opmode.chan_width,
4788                                                  data->sta_opmode.rx_nss);
4789 #endif /* CONFIG_AP */
4790                 break;
4791         default:
4792                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
4793                 break;
4794         }
4795 }
4796
4797
4798 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
4799                                  union wpa_event_data *data)
4800 {
4801         struct wpa_supplicant *wpa_s;
4802
4803         if (event != EVENT_INTERFACE_STATUS)
4804                 return;
4805
4806         wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
4807         if (wpa_s && wpa_s->driver->get_ifindex) {
4808                 unsigned int ifindex;
4809
4810                 ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
4811                 if (ifindex != data->interface_status.ifindex) {
4812                         wpa_dbg(wpa_s, MSG_DEBUG,
4813                                 "interface status ifindex %d mismatch (%d)",
4814                                 ifindex, data->interface_status.ifindex);
4815                         return;
4816                 }
4817         }
4818 #ifdef CONFIG_MATCH_IFACE
4819         else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
4820                 struct wpa_interface *wpa_i;
4821
4822                 wpa_i = wpa_supplicant_match_iface(
4823                         ctx, data->interface_status.ifname);
4824                 if (!wpa_i)
4825                         return;
4826                 wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
4827                 os_free(wpa_i);
4828                 if (wpa_s)
4829                         wpa_s->matched = 1;
4830         }
4831 #endif /* CONFIG_MATCH_IFACE */
4832
4833         if (wpa_s)
4834                 wpa_supplicant_event(wpa_s, event, data);
4835 }