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