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