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