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