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