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