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