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