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