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