]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/events.c
Re-initialize EAP ClientTimeout on for each session
[thirdparty/hostap.git] / wpa_supplicant / events.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Driver event processing
8d923a4a 3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
6fc6879b
JM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "eapol_supp/eapol_supp_sm.h"
3acb5005 19#include "rsn_supp/wpa.h"
6fc6879b 20#include "eloop.h"
6fc6879b
JM
21#include "config.h"
22#include "l2_packet/l2_packet.h"
23#include "wpa_supplicant_i.h"
2d5b792d 24#include "driver_i.h"
6fc6879b 25#include "pcsc_funcs.h"
3acb5005
JM
26#include "rsn_supp/preauth.h"
27#include "rsn_supp/pmksa_cache.h"
90973fb2 28#include "common/wpa_ctrl.h"
6fc6879b 29#include "eap_peer/eap.h"
1d041bec 30#include "ap/hostapd.h"
93b7ddd0 31#include "p2p/p2p.h"
8bac466b 32#include "notify.h"
90973fb2 33#include "common/ieee802_11_defs.h"
54f489be 34#include "common/ieee802_11_common.h"
bbb921da 35#include "crypto/random.h"
6fc6879b
JM
36#include "blacklist.h"
37#include "wpas_glue.h"
351f09a2 38#include "wps_supplicant.h"
11ef8d35 39#include "ibss_rsn.h"
c2a04078 40#include "sme.h"
9bae1be0 41#include "p2p_supplicant.h"
60b94c98 42#include "bgscan.h"
f8b1f695 43#include "ap.h"
6fa81a3b 44#include "bss.h"
245519e0 45#include "mlme.h"
9ba9fa07 46#include "scan.h"
6fc6879b
JM
47
48
49static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
50{
8bac466b 51 struct wpa_ssid *ssid, *old_ssid;
6fc6879b
JM
52
53 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
54 return 0;
55
56 wpa_printf(MSG_DEBUG, "Select network based on association "
57 "information");
58 ssid = wpa_supplicant_get_ssid(wpa_s);
59 if (ssid == NULL) {
60 wpa_printf(MSG_INFO, "No network configuration found for the "
61 "current AP");
62 return -1;
63 }
64
65 if (ssid->disabled) {
66 wpa_printf(MSG_DEBUG, "Selected network is disabled");
67 return -1;
68 }
69
70 wpa_printf(MSG_DEBUG, "Network configuration found for the current "
71 "AP");
72 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
73 WPA_KEY_MGMT_WPA_NONE |
56586197
JM
74 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
75 WPA_KEY_MGMT_PSK_SHA256 |
76 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
6fc6879b
JM
77 u8 wpa_ie[80];
78 size_t wpa_ie_len = sizeof(wpa_ie);
79 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
80 wpa_ie, &wpa_ie_len);
81 } else {
82 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
83 }
84
85 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
86 eapol_sm_invalidate_cached_session(wpa_s->eapol);
8bac466b 87 old_ssid = wpa_s->current_ssid;
6fc6879b
JM
88 wpa_s->current_ssid = ssid;
89 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
90 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
91 if (old_ssid != wpa_s->current_ssid)
92 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
93
94 return 0;
95}
96
97
98static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
99 void *sock_ctx)
100{
101 struct wpa_supplicant *wpa_s = eloop_ctx;
102
103 if (wpa_s->countermeasures) {
104 wpa_s->countermeasures = 0;
105 wpa_drv_set_countermeasures(wpa_s, 0);
106 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
107 wpa_supplicant_req_scan(wpa_s, 0, 0);
108 }
109}
110
111
112void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
113{
8bac466b
JM
114 int bssid_changed;
115
8401a6b0
JM
116 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
117 return;
118
6fc6879b 119 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8bac466b 120 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
121 os_memset(wpa_s->bssid, 0, ETH_ALEN);
122 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
be8be671 123 wpa_s->current_bss = NULL;
3c85f144 124 wpa_s->assoc_freq = 0;
8bac466b
JM
125 if (bssid_changed)
126 wpas_notify_bssid_changed(wpa_s);
127
6fc6879b
JM
128 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
129 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
56586197 130 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
6fc6879b
JM
131 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
132 wpa_s->ap_ies_from_associnfo = 0;
133}
134
135
136static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
137{
138 struct wpa_ie_data ie;
139 int pmksa_set = -1;
140 size_t i;
141
142 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
143 ie.pmkid == NULL)
144 return;
145
146 for (i = 0; i < ie.num_pmkid; i++) {
147 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
148 ie.pmkid + i * PMKID_LEN,
149 NULL, NULL, 0);
150 if (pmksa_set == 0) {
151 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
152 break;
153 }
154 }
155
156 wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
157 "cache", pmksa_set == 0 ? "" : "not ");
158}
159
160
161static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
162 union wpa_event_data *data)
163{
164 if (data == NULL) {
165 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
166 return;
167 }
168 wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
169 " index=%d preauth=%d",
170 MAC2STR(data->pmkid_candidate.bssid),
171 data->pmkid_candidate.index,
172 data->pmkid_candidate.preauth);
173
174 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
175 data->pmkid_candidate.index,
176 data->pmkid_candidate.preauth);
177}
178
179
180static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
181{
182 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
183 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
184 return 0;
185
186#ifdef IEEE8021X_EAPOL
187 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
188 wpa_s->current_ssid &&
189 !(wpa_s->current_ssid->eapol_flags &
190 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
191 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
192 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
193 * plaintext or static WEP keys). */
194 return 0;
195 }
196#endif /* IEEE8021X_EAPOL */
197
198 return 1;
199}
200
201
202/**
203 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
204 * @wpa_s: pointer to wpa_supplicant data
205 * @ssid: Configuration data for the network
206 * Returns: 0 on success, -1 on failure
207 *
208 * This function is called when starting authentication with a network that is
209 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
210 */
211int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
212 struct wpa_ssid *ssid)
213{
214#ifdef IEEE8021X_EAPOL
215 int aka = 0, sim = 0, type;
216
217 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
218 return 0;
219
220 if (ssid->eap.eap_methods == NULL) {
221 sim = 1;
222 aka = 1;
223 } else {
224 struct eap_method_type *eap = ssid->eap.eap_methods;
225 while (eap->vendor != EAP_VENDOR_IETF ||
226 eap->method != EAP_TYPE_NONE) {
227 if (eap->vendor == EAP_VENDOR_IETF) {
228 if (eap->method == EAP_TYPE_SIM)
229 sim = 1;
230 else if (eap->method == EAP_TYPE_AKA)
231 aka = 1;
232 }
233 eap++;
234 }
235 }
236
237 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
238 sim = 0;
239 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
240 aka = 0;
241
242 if (!sim && !aka) {
243 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
244 "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
245 return 0;
246 }
247
248 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
249 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
250 if (sim && aka)
251 type = SCARD_TRY_BOTH;
252 else if (aka)
253 type = SCARD_USIM_ONLY;
254 else
255 type = SCARD_GSM_SIM_ONLY;
256
257 wpa_s->scard = scard_init(type);
258 if (wpa_s->scard == NULL) {
259 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
260 "(pcsc-lite)");
261 return -1;
262 }
263 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
264 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
265#endif /* IEEE8021X_EAPOL */
266
267 return 0;
268}
269
270
271#ifndef CONFIG_NO_SCAN_PROCESSING
272static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
273 struct wpa_ssid *ssid)
274{
275 int i, privacy = 0;
276
277 if (ssid->mixed_cell)
278 return 1;
279
0632542b
AT
280#ifdef CONFIG_WPS
281 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
282 return 1;
283#endif /* CONFIG_WPS */
284
6fc6879b
JM
285 for (i = 0; i < NUM_WEP_KEYS; i++) {
286 if (ssid->wep_key_len[i]) {
287 privacy = 1;
288 break;
289 }
290 }
291#ifdef IEEE8021X_EAPOL
292 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
293 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
294 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
295 privacy = 1;
296#endif /* IEEE8021X_EAPOL */
297
298 if (bss->caps & IEEE80211_CAP_PRIVACY)
299 return privacy;
300 return !privacy;
301}
302
303
a6099152
JM
304static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
305 struct wpa_ssid *ssid,
6fc6879b
JM
306 struct wpa_scan_res *bss)
307{
308 struct wpa_ie_data ie;
309 int proto_match = 0;
310 const u8 *rsn_ie, *wpa_ie;
351f09a2 311 int ret;
43882f1e 312 int wep_ok;
6fc6879b 313
a6099152 314 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
351f09a2
JM
315 if (ret >= 0)
316 return ret;
ad08c363 317
43882f1e
JM
318 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
319 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
320 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
321 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
322 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
323
6fc6879b
JM
324 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
325 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
326 proto_match++;
327
328 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
329 wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
330 break;
331 }
43882f1e
JM
332
333 if (wep_ok &&
334 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
335 {
336 wpa_printf(MSG_DEBUG, " selected based on TSN in "
337 "RSN IE");
338 return 1;
339 }
340
6fc6879b
JM
341 if (!(ie.proto & ssid->proto)) {
342 wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
343 "mismatch");
344 break;
345 }
346
347 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
348 wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
349 "mismatch");
350 break;
351 }
352
353 if (!(ie.group_cipher & ssid->group_cipher)) {
354 wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
355 "mismatch");
356 break;
357 }
358
359 if (!(ie.key_mgmt & ssid->key_mgmt)) {
360 wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
361 "mismatch");
362 break;
363 }
364
365#ifdef CONFIG_IEEE80211W
0b60b0aa 366 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
70f8cc8e 367 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
6fc6879b
JM
368 wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
369 "protection");
370 break;
371 }
372#endif /* CONFIG_IEEE80211W */
373
374 wpa_printf(MSG_DEBUG, " selected based on RSN IE");
375 return 1;
376 }
377
378 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
379 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
380 proto_match++;
381
382 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
383 wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
384 break;
385 }
43882f1e
JM
386
387 if (wep_ok &&
388 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
389 {
390 wpa_printf(MSG_DEBUG, " selected based on TSN in "
391 "WPA IE");
392 return 1;
393 }
394
6fc6879b
JM
395 if (!(ie.proto & ssid->proto)) {
396 wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
397 "mismatch");
398 break;
399 }
400
401 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
402 wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
403 "mismatch");
404 break;
405 }
406
407 if (!(ie.group_cipher & ssid->group_cipher)) {
408 wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
409 "mismatch");
410 break;
411 }
412
413 if (!(ie.key_mgmt & ssid->key_mgmt)) {
414 wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
415 "mismatch");
416 break;
417 }
418
419 wpa_printf(MSG_DEBUG, " selected based on WPA IE");
420 return 1;
421 }
422
cc5e390d
JM
423 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
424 proto_match == 0) {
6fc6879b 425 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
cc5e390d
JM
426 return 0;
427 }
6fc6879b 428
cc5e390d
JM
429 /* Allow in non-WPA configuration */
430 return 1;
6fc6879b
JM
431}
432
433
b766a9a2
JM
434static int freq_allowed(int *freqs, int freq)
435{
436 int i;
437
438 if (freqs == NULL)
439 return 1;
440
441 for (i = 0; freqs[i]; i++)
442 if (freqs[i] == freq)
443 return 1;
444 return 0;
445}
446
447
d8d940b7
JM
448static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
449 int i, struct wpa_scan_res *bss,
450 struct wpa_ssid *group)
6fc6879b 451{
d8d940b7
JM
452 const u8 *ssid_;
453 u8 wpa_ie_len, rsn_ie_len, ssid_len;
454 int wpa;
6fc6879b
JM
455 struct wpa_blacklist *e;
456 const u8 *ie;
d8d940b7 457 struct wpa_ssid *ssid;
6fc6879b 458
d8d940b7
JM
459 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
460 ssid_ = ie ? ie + 2 : (u8 *) "";
461 ssid_len = ie ? ie[1] : 0;
6fc6879b 462
d8d940b7
JM
463 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
464 wpa_ie_len = ie ? ie[1] : 0;
6fc6879b 465
d8d940b7
JM
466 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
467 rsn_ie_len = ie ? ie[1] : 0;
6fc6879b 468
d8d940b7
JM
469 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
470 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
471 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
472 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
473 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
474 " wps" : "");
9cf32261 475
d8d940b7 476 e = wpa_blacklist_get(wpa_s, bss->bssid);
5471c343
JM
477 if (e) {
478 int limit = 1;
479 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
480 /*
481 * When only a single network is enabled, we can
482 * trigger blacklisting on the first failure. This
483 * should not be done with multiple enabled networks to
484 * avoid getting forced to move into a worse ESS on
485 * single error if there are no other BSSes of the
486 * current ESS.
487 */
488 limit = 0;
489 }
490 if (e->count > limit) {
491 wpa_printf(MSG_DEBUG, " skip - blacklisted "
492 "(count=%d limit=%d)", e->count, limit);
493 return 0;
494 }
d8d940b7 495 }
6fc6879b 496
d8d940b7
JM
497 if (ssid_len == 0) {
498 wpa_printf(MSG_DEBUG, " skip - SSID not known");
499 return 0;
500 }
e81634cd 501
d8d940b7 502 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
6fc6879b 503
d8d940b7
JM
504 for (ssid = group; ssid; ssid = ssid->pnext) {
505 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
ad08c363 506
d8d940b7
JM
507 if (ssid->disabled) {
508 wpa_printf(MSG_DEBUG, " skip - disabled");
0c703df3 509 continue;
d8d940b7 510 }
9cf32261 511
ad08c363 512#ifdef CONFIG_WPS
f648bc7d
JM
513 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
514 wpa_printf(MSG_DEBUG, " skip - blacklisted (WPS)");
515 continue;
516 }
517
d8d940b7
JM
518 if (wpa && ssid->ssid_len == 0 &&
519 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
520 check_ssid = 0;
521
522 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
523 /* Only allow wildcard SSID match if an AP
524 * advertises active WPS operation that matches
525 * with our mode. */
526 check_ssid = 1;
ad08c363 527 if (ssid->ssid_len == 0 &&
a6099152 528 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
ad08c363 529 check_ssid = 0;
d8d940b7 530 }
ad08c363
JM
531#endif /* CONFIG_WPS */
532
d8d940b7
JM
533 if (check_ssid &&
534 (ssid_len != ssid->ssid_len ||
535 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
536 wpa_printf(MSG_DEBUG, " skip - SSID mismatch");
0c703df3 537 continue;
6fc6879b 538 }
6fc6879b 539
d8d940b7
JM
540 if (ssid->bssid_set &&
541 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
542 wpa_printf(MSG_DEBUG, " skip - BSSID mismatch");
0c703df3 543 continue;
d8d940b7 544 }
6fc6879b 545
cc5e390d 546 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
0c703df3 547 continue;
9cf32261 548
d8d940b7
JM
549 if (!wpa &&
550 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
551 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
552 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
553 wpa_printf(MSG_DEBUG, " skip - non-WPA network not "
554 "allowed");
0c703df3 555 continue;
6fc6879b 556 }
9cf32261 557
d8d940b7
JM
558 if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
559 wpa_printf(MSG_DEBUG, " skip - privacy mismatch");
0c703df3 560 continue;
e81634cd
JM
561 }
562
d8d940b7
JM
563 if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
564 wpa_printf(MSG_DEBUG, " skip - IBSS (adhoc) "
565 "network");
0c703df3 566 continue;
d8d940b7 567 }
b766a9a2 568
d8d940b7
JM
569 if (!freq_allowed(ssid->freq_list, bss->freq)) {
570 wpa_printf(MSG_DEBUG, " skip - frequency not "
571 "allowed");
0c703df3 572 continue;
6fc6879b 573 }
d8d940b7 574
73e49269
JM
575#ifdef CONFIG_P2P
576 /*
577 * TODO: skip the AP if its P2P IE has Group Formation
578 * bit set in the P2P Group Capability Bitmap and we
579 * are not in Group Formation with that device.
580 */
581#endif /* CONFIG_P2P */
582
d8d940b7
JM
583 /* Matching configuration found */
584 return ssid;
6fc6879b
JM
585 }
586
d8d940b7
JM
587 /* No matching configuration found */
588 return 0;
9cf32261
JM
589}
590
591
6fa81a3b 592static struct wpa_bss *
a1fd2ce5
JM
593wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
594 struct wpa_scan_results *scan_res,
595 struct wpa_ssid *group,
9cf32261
JM
596 struct wpa_ssid **selected_ssid)
597{
d8d940b7 598 size_t i;
9cf32261
JM
599
600 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
601 group->priority);
602
d8d940b7
JM
603 for (i = 0; i < scan_res->num; i++) {
604 struct wpa_scan_res *bss = scan_res->res[i];
605 const u8 *ie, *ssid;
606 u8 ssid_len;
607
608 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
609 if (!*selected_ssid)
610 continue;
611
612 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
613 ssid = ie ? ie + 2 : (u8 *) "";
614 ssid_len = ie ? ie[1] : 0;
9cf32261 615
d8d940b7
JM
616 wpa_printf(MSG_DEBUG, " selected BSS " MACSTR " ssid='%s'",
617 MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
618 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
619 }
620
621 return NULL;
6fc6879b
JM
622}
623
624
6fa81a3b 625static struct wpa_bss *
09b9df4e 626wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
a1fd2ce5 627 struct wpa_scan_results *scan_res,
09b9df4e 628 struct wpa_ssid **selected_ssid)
6fc6879b 629{
6fa81a3b 630 struct wpa_bss *selected = NULL;
09b9df4e
JM
631 int prio;
632
633 while (selected == NULL) {
634 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
635 selected = wpa_supplicant_select_bss(
a1fd2ce5 636 wpa_s, scan_res, wpa_s->conf->pssid[prio],
09b9df4e
JM
637 selected_ssid);
638 if (selected)
639 break;
640 }
641
642 if (selected == NULL && wpa_s->blacklist) {
643 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
644 "and try again");
645 wpa_blacklist_clear(wpa_s);
646 wpa_s->blacklist_cleared++;
647 } else if (selected == NULL)
648 break;
649 }
650
651 return selected;
652}
653
654
655static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
977b1174 656 int timeout_sec, int timeout_usec)
09b9df4e 657{
24f76940 658 if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
4f34d51a
SL
659 /*
660 * No networks are enabled; short-circuit request so
661 * we don't wait timeout seconds before transitioning
662 * to INACTIVE state.
663 */
664 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
665 return;
09b9df4e 666 }
977b1174 667 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
09b9df4e
JM
668}
669
670
86d4f806
JM
671void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
672 struct wpa_bss *selected,
673 struct wpa_ssid *ssid)
09b9df4e
JM
674{
675 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
676 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
677 "PBC session overlap");
b73bf0a7
JM
678#ifdef CONFIG_P2P
679 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
680 return;
681#endif /* CONFIG_P2P */
199716ad
BG
682
683#ifdef CONFIG_WPS
7736f18b 684 wpas_wps_cancel(wpa_s);
199716ad 685#endif /* CONFIG_WPS */
09b9df4e
JM
686 return;
687 }
688
689 /*
690 * Do not trigger new association unless the BSSID has changed or if
691 * reassociation is requested. If we are in process of associating with
692 * the selected BSSID, do not trigger new attempt.
693 */
694 if (wpa_s->reassociate ||
695 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
696 (wpa_s->wpa_state != WPA_ASSOCIATING ||
697 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
698 0))) {
699 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
977b1174 700 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
09b9df4e
JM
701 return;
702 }
703 wpa_supplicant_associate(wpa_s, selected, ssid);
704 } else {
705 wpa_printf(MSG_DEBUG, "Already associated with the selected "
706 "AP");
707 }
09b9df4e
JM
708}
709
710
b55aaa5f
JM
711static struct wpa_ssid *
712wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
713{
714 int prio;
715 struct wpa_ssid *ssid;
716
717 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
718 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
719 {
720 if (ssid->disabled)
721 continue;
722 if (ssid->mode == IEEE80211_MODE_IBSS ||
723 ssid->mode == IEEE80211_MODE_AP)
724 return ssid;
725 }
726 }
727 return NULL;
728}
729
730
a1fd2ce5
JM
731/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
732 * on BSS added and BSS changed events */
6ae93185 733static void wpa_supplicant_rsn_preauth_scan_results(
a1fd2ce5 734 struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
6ae93185
JM
735{
736 int i;
737
738 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
739 return;
740
a1fd2ce5 741 for (i = scan_res->num - 1; i >= 0; i--) {
6ae93185
JM
742 const u8 *ssid, *rsn;
743 struct wpa_scan_res *r;
744
a1fd2ce5 745 r = scan_res->res[i];
6ae93185
JM
746
747 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
748 if (ssid == NULL)
749 continue;
750
751 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
752 if (rsn == NULL)
753 continue;
754
755 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
756 }
757
758}
759
760
48563d86
JM
761static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
762 struct wpa_bss *selected,
763 struct wpa_ssid *ssid,
764 struct wpa_scan_results *scan_res)
765{
766 size_t i;
767 struct wpa_scan_res *current_bss = NULL;
768 int min_diff;
769
770 if (wpa_s->reassociate)
771 return 1; /* explicit request to reassociate */
772 if (wpa_s->wpa_state < WPA_ASSOCIATED)
773 return 1; /* we are not associated; continue */
774 if (wpa_s->current_ssid == NULL)
775 return 1; /* unknown current SSID */
776 if (wpa_s->current_ssid != ssid)
777 return 1; /* different network block */
778
779 for (i = 0; i < scan_res->num; i++) {
780 struct wpa_scan_res *res = scan_res->res[i];
781 const u8 *ie;
782 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
783 continue;
784
785 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
786 if (ie == NULL)
787 continue;
788 if (ie[1] != wpa_s->current_ssid->ssid_len ||
789 os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
790 continue;
791 current_bss = res;
792 break;
793 }
794
795 if (!current_bss)
796 return 1; /* current BSS not seen in scan results */
797
798 wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
799 wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
800 MAC2STR(current_bss->bssid), current_bss->level);
801 wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
802 MAC2STR(selected->bssid), selected->level);
803
ac26ebd8
JM
804 if (wpa_s->current_ssid->bssid_set &&
805 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
806 0) {
807 wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
808 "preferred BSSID");
809 return 1;
810 }
811
48563d86
JM
812 min_diff = 2;
813 if (current_bss->level < 0) {
814 if (current_bss->level < -85)
815 min_diff = 1;
816 else if (current_bss->level < -80)
817 min_diff = 2;
818 else if (current_bss->level < -75)
819 min_diff = 3;
820 else if (current_bss->level < -70)
821 min_diff = 4;
822 else
823 min_diff = 5;
824 }
825 if (abs(current_bss->level - selected->level) < min_diff) {
826 wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
827 "signal level");
828 return 0;
829 }
830
831 return 1;
832}
833
834
6859f1cb
BG
835static void _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
836 union wpa_event_data *data)
09b9df4e 837{
6fa81a3b 838 struct wpa_bss *selected;
6fc6879b 839 struct wpa_ssid *ssid = NULL;
a1fd2ce5 840 struct wpa_scan_results *scan_res;
5bc0cdb7
JM
841 int ap = 0;
842
843#ifdef CONFIG_AP
844 if (wpa_s->ap_iface)
845 ap = 1;
846#endif /* CONFIG_AP */
6fc6879b 847
cb8564b1
DW
848 wpa_supplicant_notify_scanning(wpa_s, 0);
849
a1fd2ce5
JM
850 scan_res = wpa_supplicant_get_scan_results(wpa_s,
851 data ? &data->scan_info :
852 NULL, 1);
853 if (scan_res == NULL) {
5bc0cdb7 854 if (wpa_s->conf->ap_scan == 2 || ap)
6fc6879b
JM
855 return;
856 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
857 "scanning again");
977b1174 858 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
09b9df4e 859 return;
6fc6879b
JM
860 }
861
bbb921da
JM
862#ifndef CONFIG_NO_RANDOM_POOL
863 size_t i, num;
864 num = scan_res->num;
865 if (num > 10)
866 num = 10;
867 for (i = 0; i < num; i++) {
868 u8 buf[5];
869 struct wpa_scan_res *res = scan_res->res[i];
870 buf[0] = res->bssid[5];
871 buf[1] = res->qual & 0xff;
872 buf[2] = res->noise & 0xff;
873 buf[3] = res->level & 0xff;
874 buf[4] = res->tsf & 0xff;
875 random_add_randomness(buf, sizeof(buf));
876 }
877#endif /* CONFIG_NO_RANDOM_POOL */
878
64e58f51 879 if (wpa_s->scan_res_handler) {
860fddbb
JB
880 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
881 struct wpa_scan_results *scan_res);
882
883 scan_res_handler = wpa_s->scan_res_handler;
64e58f51 884 wpa_s->scan_res_handler = NULL;
860fddbb
JB
885 scan_res_handler(wpa_s, scan_res);
886
64e58f51
JM
887 wpa_scan_results_free(scan_res);
888 return;
889 }
890
5bc0cdb7
JM
891 if (ap) {
892 wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
893 wpa_scan_results_free(scan_res);
894 return;
895 }
896
c577db2a 897 wpa_printf(MSG_DEBUG, "%s: New scan results available", wpa_s->ifname);
24f76940
JM
898 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
899 wpas_notify_scan_results(wpa_s);
6fc6879b 900
8bac466b
JM
901 wpas_notify_scan_done(wpa_s, 1);
902
a1fd2ce5
JM
903 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
904 wpa_scan_results_free(scan_res);
6fc6879b 905 return;
a1fd2ce5 906 }
6fc6879b 907
3180d7a2
SO
908 if (wpa_s->disconnected) {
909 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
a1fd2ce5 910 wpa_scan_results_free(scan_res);
3180d7a2
SO
911 return;
912 }
913
c2594c36 914 if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
a1fd2ce5 915 wpa_scan_results_free(scan_res);
60b94c98 916 return;
a1fd2ce5
JM
917 }
918
919 wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
60b94c98 920
a1fd2ce5 921 selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
6ae93185 922
6fc6879b 923 if (selected) {
48563d86
JM
924 int skip;
925 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
926 scan_res);
927 wpa_scan_results_free(scan_res);
928 if (skip)
929 return;
09b9df4e 930 wpa_supplicant_connect(wpa_s, selected, ssid);
6fc6879b 931 } else {
48563d86 932 wpa_scan_results_free(scan_res);
09b9df4e 933 wpa_printf(MSG_DEBUG, "No suitable network found");
b55aaa5f
JM
934 ssid = wpa_supplicant_pick_new_network(wpa_s);
935 if (ssid) {
936 wpa_printf(MSG_DEBUG, "Setup a new network");
937 wpa_supplicant_associate(wpa_s, NULL, ssid);
977b1174
JM
938 } else {
939 int timeout_sec = 5;
940 int timeout_usec = 0;
0817de90
JM
941#ifdef CONFIG_P2P
942 if (wpa_s->p2p_in_provisioning) {
943 /*
944 * Use shorter wait during P2P Provisioning
945 * state to speed up group formation.
946 */
947 timeout_sec = 0;
948 timeout_usec = 250000;
949 }
950#endif /* CONFIG_P2P */
977b1174
JM
951 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
952 timeout_usec);
953 }
6fc6879b 954 }
6fc6879b 955}
6859f1cb
BG
956
957
958static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
959 union wpa_event_data *data)
960{
961 const char *rn, *rn2;
962 struct wpa_supplicant *ifs;
963
964 _wpa_supplicant_event_scan_results(wpa_s, data);
965
966 /*
967 * Check other interfaces to see if they have the same radio-name. If
968 * so, they get updated with this same scan info.
969 */
970 if (!wpa_s->driver->get_radio_name)
971 return;
972
973 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
974 if (rn == NULL || rn[0] == '\0')
975 return;
976
977 wpa_printf(MSG_DEBUG, "%s: Checking for other virtual interfaces "
978 "sharing same radio (%s) in event_scan_results",
979 wpa_s->ifname, rn);
980
981 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
982 if (ifs == wpa_s || !ifs->driver->get_radio_name)
983 continue;
984
985 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
986 if (rn2 && os_strcmp(rn, rn2) == 0) {
987 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
988 "sibling", ifs->ifname);
989 _wpa_supplicant_event_scan_results(ifs, data);
990 }
991 }
992}
993
6fc6879b
JM
994#endif /* CONFIG_NO_SCAN_PROCESSING */
995
996
579ce771
JM
997static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
998 union wpa_event_data *data)
6fc6879b
JM
999{
1000 int l, len, found = 0, wpa_found, rsn_found;
c2a04078 1001 const u8 *p;
6fc6879b
JM
1002
1003 wpa_printf(MSG_DEBUG, "Association info event");
1004 if (data->assoc_info.req_ies)
1005 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1006 data->assoc_info.req_ies_len);
1007 if (data->assoc_info.resp_ies)
1008 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1009 data->assoc_info.resp_ies_len);
1010 if (data->assoc_info.beacon_ies)
1011 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1012 data->assoc_info.beacon_ies,
1013 data->assoc_info.beacon_ies_len);
4832ecd7
JM
1014 if (data->assoc_info.freq)
1015 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
6fc6879b
JM
1016
1017 p = data->assoc_info.req_ies;
1018 l = data->assoc_info.req_ies_len;
1019
1020 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1021 while (p && l >= 2) {
1022 len = p[1] + 2;
1023 if (len > l) {
1024 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1025 p, l);
1026 break;
1027 }
1028 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1029 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1030 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1031 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1032 break;
1033 found = 1;
1034 wpa_find_assoc_pmkid(wpa_s);
1035 break;
1036 }
1037 l -= len;
1038 p += len;
1039 }
1040 if (!found && data->assoc_info.req_ies)
1041 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1042
c2a04078 1043#ifdef CONFIG_IEEE80211R
62c72d72
JM
1044#ifdef CONFIG_SME
1045 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1046 u8 bssid[ETH_ALEN];
1047 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1048 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1049 data->assoc_info.resp_ies,
1050 data->assoc_info.resp_ies_len,
1051 bssid) < 0) {
1052 wpa_printf(MSG_DEBUG, "FT: Validation of "
1053 "Reassociation Response failed");
579ce771
JM
1054 wpa_supplicant_deauthenticate(
1055 wpa_s, WLAN_REASON_INVALID_IE);
1056 return -1;
62c72d72
JM
1057 }
1058 }
62c72d72 1059
c2a04078
JM
1060 p = data->assoc_info.resp_ies;
1061 l = data->assoc_info.resp_ies_len;
1062
54f489be
JM
1063#ifdef CONFIG_WPS_STRICT
1064 if (wpa_s->current_ssid &&
1065 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1066 struct wpabuf *wps;
1067 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1068 if (wps == NULL) {
1069 wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
1070 "WPS IE in (Re)Association Response");
1071 return -1;
1072 }
1073
1074 if (wps_validate_assoc_resp(wps) < 0) {
1075 wpabuf_free(wps);
1076 wpa_supplicant_deauthenticate(
1077 wpa_s, WLAN_REASON_INVALID_IE);
1078 return -1;
1079 }
1080 wpabuf_free(wps);
1081 }
1082#endif /* CONFIG_WPS_STRICT */
1083
e7846b68 1084 /* Go through the IEs and make a copy of the MDIE, if present. */
c2a04078
JM
1085 while (p && l >= 2) {
1086 len = p[1] + 2;
1087 if (len > l) {
1088 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1089 p, l);
1090 break;
1091 }
e7846b68
JM
1092 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1093 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1094 wpa_s->sme.ft_used = 1;
1095 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1096 MOBILITY_DOMAIN_ID_LEN);
1097 break;
1098 }
c2a04078
JM
1099 l -= len;
1100 p += len;
1101 }
e7846b68 1102#endif /* CONFIG_SME */
c2a04078 1103
e7846b68
JM
1104 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1105 data->assoc_info.resp_ies_len);
c2a04078
JM
1106#endif /* CONFIG_IEEE80211R */
1107
6fc6879b
JM
1108 /* WPA/RSN IE from Beacon/ProbeResp */
1109 p = data->assoc_info.beacon_ies;
1110 l = data->assoc_info.beacon_ies_len;
1111
1112 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1113 */
1114 wpa_found = rsn_found = 0;
1115 while (p && l >= 2) {
1116 len = p[1] + 2;
1117 if (len > l) {
1118 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1119 p, l);
1120 break;
1121 }
1122 if (!wpa_found &&
1123 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1124 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1125 wpa_found = 1;
1126 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1127 }
1128
1129 if (!rsn_found &&
1130 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1131 rsn_found = 1;
1132 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1133 }
1134
1135 l -= len;
1136 p += len;
1137 }
1138
1139 if (!wpa_found && data->assoc_info.beacon_ies)
1140 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1141 if (!rsn_found && data->assoc_info.beacon_ies)
1142 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1143 if (wpa_found || rsn_found)
1144 wpa_s->ap_ies_from_associnfo = 1;
4832ecd7
JM
1145
1146 wpa_s->assoc_freq = data->assoc_info.freq;
579ce771
JM
1147
1148 return 0;
6fc6879b
JM
1149}
1150
1151
1152static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1153 union wpa_event_data *data)
1154{
1155 u8 bssid[ETH_ALEN];
1d041bec 1156 int ft_completed;
8bac466b 1157 int bssid_changed;
0194fedb 1158 struct wpa_driver_capa capa;
6fc6879b 1159
1d041bec
JM
1160#ifdef CONFIG_AP
1161 if (wpa_s->ap_iface) {
1162 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1163 data->assoc_info.addr,
1164 data->assoc_info.req_ies,
1165 data->assoc_info.req_ies_len);
1166 return;
1167 }
1168#endif /* CONFIG_AP */
1169
1170 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
579ce771
JM
1171 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1172 return;
6fc6879b
JM
1173
1174 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
c2a04078 1175 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
6fc6879b 1176 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
c2a04078 1177 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
6fc6879b
JM
1178 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1179 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1180 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1181 MACSTR, MAC2STR(bssid));
bbb921da 1182 random_add_randomness(bssid, ETH_ALEN);
8bac466b 1183 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
6fc6879b
JM
1184 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1185 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
8bac466b
JM
1186 if (bssid_changed)
1187 wpas_notify_bssid_changed(wpa_s);
1188
6fc6879b
JM
1189 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1190 wpa_clear_keys(wpa_s, bssid);
1191 }
1192 if (wpa_supplicant_select_config(wpa_s) < 0) {
1193 wpa_supplicant_disassociate(
1194 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1195 return;
1196 }
8f770587
JM
1197 if (wpa_s->current_ssid) {
1198 struct wpa_bss *bss = NULL;
1199 struct wpa_ssid *ssid = wpa_s->current_ssid;
1200 if (ssid->ssid_len > 0)
1201 bss = wpa_bss_get(wpa_s, bssid,
1202 ssid->ssid, ssid->ssid_len);
1203 if (!bss)
1204 bss = wpa_bss_get_bssid(wpa_s, bssid);
1205 if (bss)
1206 wpa_s->current_bss = bss;
1207 }
6fc6879b
JM
1208 }
1209
62fa124c
JM
1210#ifdef CONFIG_SME
1211 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1212 wpa_s->sme.prev_bssid_set = 1;
1213#endif /* CONFIG_SME */
1214
6fc6879b
JM
1215 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1216 if (wpa_s->current_ssid) {
1217 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1218 * initialized before association, but for other modes,
1219 * initialize PC/SC here, if the current configuration needs
1220 * smartcard or SIM/USIM. */
1221 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1222 }
1223 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
3f967fe0
JM
1224 if (wpa_s->l2)
1225 l2_packet_notify_auth_start(wpa_s->l2);
6fc6879b
JM
1226
1227 /*
1228 * Set portEnabled first to FALSE in order to get EAP state machine out
1229 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1230 * state machine may transit to AUTHENTICATING state based on obsolete
1231 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1232 * AUTHENTICATED without ever giving chance to EAP state machine to
1233 * reset the state.
1234 */
1235 if (!ft_completed) {
1236 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1237 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1238 }
56586197 1239 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
6fc6879b
JM
1240 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1241 /* 802.1X::portControl = Auto */
1242 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1243 wpa_s->eapol_received = 0;
1244 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
9c972abb
JM
1245 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1246 (wpa_s->current_ssid &&
1247 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
6fc6879b
JM
1248 wpa_supplicant_cancel_auth_timeout(wpa_s);
1249 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1250 } else if (!ft_completed) {
1251 /* Timeout for receiving the first EAPOL packet */
1252 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1253 }
1254 wpa_supplicant_cancel_scan(wpa_s);
1255
c2a04078 1256 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
56586197 1257 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
1258 /*
1259 * We are done; the driver will take care of RSN 4-way
1260 * handshake.
1261 */
1262 wpa_supplicant_cancel_auth_timeout(wpa_s);
1263 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1264 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1265 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1266 }
1ff73338
JM
1267
1268 if (wpa_s->pending_eapol_rx) {
1269 struct os_time now, age;
1270 os_get_time(&now);
1271 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1272 if (age.sec == 0 && age.usec < 100000 &&
1273 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1274 0) {
1275 wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1276 "that was received just before association "
1277 "notification");
1278 wpa_supplicant_rx_eapol(
1279 wpa_s, wpa_s->pending_eapol_rx_src,
1280 wpabuf_head(wpa_s->pending_eapol_rx),
1281 wpabuf_len(wpa_s->pending_eapol_rx));
1282 }
1283 wpabuf_free(wpa_s->pending_eapol_rx);
1284 wpa_s->pending_eapol_rx = NULL;
1285 }
60b94c98
JM
1286
1287#ifdef CONFIG_BGSCAN
1288 if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1289 bgscan_deinit(wpa_s);
1290 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1291 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1292 wpa_printf(MSG_DEBUG, "Failed to initialize "
1293 "bgscan");
1294 /*
1295 * Live without bgscan; it is only used as a
1296 * roaming optimization, so the initial
1297 * connection is not affected.
1298 */
1299 } else
1300 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1301 } else
1302 wpa_s->bgscan_ssid = NULL;
1303 }
1304#endif /* CONFIG_BGSCAN */
0194fedb
JB
1305
1306 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1307 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1308 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1309 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1310 /* Set static WEP keys again */
1311 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1312 }
6fc6879b
JM
1313}
1314
1315
0544b242
JM
1316static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1317 u16 reason_code)
6fc6879b
JM
1318{
1319 const u8 *bssid;
6d6f4bb8
JM
1320#ifdef CONFIG_SME
1321 int authenticating;
1322 u8 prev_pending_bssid[ETH_ALEN];
1323
1324 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1325 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1326#endif /* CONFIG_SME */
6fc6879b
JM
1327
1328 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1329 /*
1330 * At least Host AP driver and a Prism3 card seemed to be
1331 * generating streams of disconnected events when configuring
1332 * IBSS for WPA-None. Ignore them for now.
1333 */
1334 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1335 "IBSS/WPA-None mode");
1336 return;
1337 }
1338
1339 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
56586197 1340 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
1341 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1342 "pre-shared key may be incorrect");
1343 }
0d0a8ca1
AC
1344 if (!wpa_s->auto_reconnect_disabled ||
1345 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1346 wpa_printf(MSG_DEBUG, "WPA: Auto connect enabled: try to "
1347 "reconnect (wps=%d)",
1348 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
ea184114 1349 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
0d0a8ca1
AC
1350 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1351 } else {
1352 wpa_printf(MSG_DEBUG, "WPA: Auto connect disabled: do not try "
1353 "to re-connect");
1354 wpa_s->reassociate = 0;
1355 wpa_s->disconnected = 1;
1356 }
6fc6879b 1357 bssid = wpa_s->bssid;
a8e16edc 1358 if (is_zero_ether_addr(bssid))
6fc6879b 1359 bssid = wpa_s->pending_bssid;
0fb337c1 1360 wpas_connection_failed(wpa_s, bssid);
6fc6879b 1361 wpa_sm_notify_disassoc(wpa_s->wpa);
0544b242
JM
1362 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1363 " reason=%d",
1364 MAC2STR(bssid), reason_code);
6fc6879b 1365 if (wpa_supplicant_dynamic_keys(wpa_s)) {
0544b242 1366 wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
6fc6879b
JM
1367 wpa_s->keys_cleared = 0;
1368 wpa_clear_keys(wpa_s, wpa_s->bssid);
1369 }
1370 wpa_supplicant_mark_disassoc(wpa_s);
60b94c98 1371 bgscan_deinit(wpa_s);
3b038d79 1372 wpa_s->bgscan_ssid = NULL;
6d6f4bb8
JM
1373#ifdef CONFIG_SME
1374 if (authenticating &&
1375 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1376 /*
1377 * mac80211-workaround to force deauth on failed auth cmd,
1378 * requires us to remain in authenticating state to allow the
1379 * second authentication attempt to be continued properly.
1380 */
1381 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1382 "proceed after disconnection event");
1383 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1384 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1385 }
1386#endif /* CONFIG_SME */
6fc6879b
JM
1387}
1388
1389
46690a3b
JM
1390#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1391static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1392 void *sock_ctx)
1393{
1394 struct wpa_supplicant *wpa_s = eloop_ctx;
1395
1396 if (!wpa_s->pending_mic_error_report)
1397 return;
1398
1399 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1400 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1401 wpa_s->pending_mic_error_report = 0;
1402}
1403#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1404
1405
6fc6879b
JM
1406static void
1407wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1408 union wpa_event_data *data)
1409{
1410 int pairwise;
1411 struct os_time t;
1412
1413 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1414 pairwise = (data && data->michael_mic_failure.unicast);
6fc6879b 1415 os_get_time(&t);
46690a3b
JM
1416 if ((wpa_s->last_michael_mic_error &&
1417 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1418 wpa_s->pending_mic_error_report) {
1419 if (wpa_s->pending_mic_error_report) {
1420 /*
1421 * Send the pending MIC error report immediately since
1422 * we are going to start countermeasures and AP better
1423 * do the same.
1424 */
1425 wpa_sm_key_request(wpa_s->wpa, 1,
1426 wpa_s->pending_mic_error_pairwise);
1427 }
1428
1429 /* Send the new MIC error report immediately since we are going
1430 * to start countermeasures and AP better do the same.
1431 */
1432 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1433
6fc6879b
JM
1434 /* initialize countermeasures */
1435 wpa_s->countermeasures = 1;
1436 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1437
1438 /*
1439 * Need to wait for completion of request frame. We do not get
1440 * any callback for the message completion, so just wait a
1441 * short while and hope for the best. */
1442 os_sleep(0, 10000);
1443
1444 wpa_drv_set_countermeasures(wpa_s, 1);
1445 wpa_supplicant_deauthenticate(wpa_s,
1446 WLAN_REASON_MICHAEL_MIC_FAILURE);
1447 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1448 wpa_s, NULL);
1449 eloop_register_timeout(60, 0,
1450 wpa_supplicant_stop_countermeasures,
1451 wpa_s, NULL);
1452 /* TODO: mark the AP rejected for 60 second. STA is
1453 * allowed to associate with another AP.. */
46690a3b
JM
1454 } else {
1455#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1456 if (wpa_s->mic_errors_seen) {
1457 /*
1458 * Reduce the effectiveness of Michael MIC error
1459 * reports as a means for attacking against TKIP if
1460 * more than one MIC failure is noticed with the same
1461 * PTK. We delay the transmission of the reports by a
1462 * random time between 0 and 60 seconds in order to
1463 * force the attacker wait 60 seconds before getting
1464 * the information on whether a frame resulted in a MIC
1465 * failure.
1466 */
1467 u8 rval[4];
1468 int sec;
1469
1470 if (os_get_random(rval, sizeof(rval)) < 0)
1471 sec = os_random() % 60;
1472 else
1473 sec = WPA_GET_BE32(rval) % 60;
1474 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1475 "seconds", sec);
1476 wpa_s->pending_mic_error_report = 1;
1477 wpa_s->pending_mic_error_pairwise = pairwise;
1478 eloop_cancel_timeout(
1479 wpa_supplicant_delayed_mic_error_report,
1480 wpa_s, NULL);
1481 eloop_register_timeout(
1482 sec, os_random() % 1000000,
1483 wpa_supplicant_delayed_mic_error_report,
1484 wpa_s, NULL);
1485 } else {
1486 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1487 }
1488#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1489 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1490#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b
JM
1491 }
1492 wpa_s->last_michael_mic_error = t.sec;
46690a3b 1493 wpa_s->mic_errors_seen++;
6fc6879b
JM
1494}
1495
1496
a83d9c96
SL
1497#ifdef CONFIG_TERMINATE_ONLASTIF
1498static int any_interfaces(struct wpa_supplicant *head)
1499{
1500 struct wpa_supplicant *wpa_s;
1501
1502 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1503 if (!wpa_s->interface_removed)
1504 return 1;
1505 return 0;
1506}
1507#endif /* CONFIG_TERMINATE_ONLASTIF */
1508
1509
6fc6879b
JM
1510static void
1511wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1512 union wpa_event_data *data)
1513{
1514 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1515 return;
1516
1517 switch (data->interface_status.ievent) {
1518 case EVENT_INTERFACE_ADDED:
1519 if (!wpa_s->interface_removed)
1520 break;
1521 wpa_s->interface_removed = 0;
1522 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1523 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1524 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1525 "after interface was added.");
1526 }
1527 break;
1528 case EVENT_INTERFACE_REMOVED:
1529 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1530 wpa_s->interface_removed = 1;
1531 wpa_supplicant_mark_disassoc(wpa_s);
1532 l2_packet_deinit(wpa_s->l2);
1533 wpa_s->l2 = NULL;
a83d9c96
SL
1534#ifdef CONFIG_TERMINATE_ONLASTIF
1535 /* check if last interface */
1536 if (!any_interfaces(wpa_s->global->ifaces))
1537 eloop_terminate();
1538#endif /* CONFIG_TERMINATE_ONLASTIF */
6fc6879b
JM
1539 break;
1540 }
1541}
1542
1543
1544#ifdef CONFIG_PEERKEY
1545static void
1546wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1547 union wpa_event_data *data)
1548{
1549 if (data == NULL)
1550 return;
1551 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1552}
1553#endif /* CONFIG_PEERKEY */
1554
1555
1556#ifdef CONFIG_IEEE80211R
1557static void
1558wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1559 union wpa_event_data *data)
1560{
1561 if (data == NULL)
1562 return;
1563
1564 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1565 data->ft_ies.ies_len,
1566 data->ft_ies.ft_action,
babfbf15
JM
1567 data->ft_ies.target_ap,
1568 data->ft_ies.ric_ies,
1569 data->ft_ies.ric_ies_len) < 0) {
6fc6879b
JM
1570 /* TODO: prevent MLME/driver from trying to associate? */
1571 }
1572}
1573#endif /* CONFIG_IEEE80211R */
1574
1575
11ef8d35
JM
1576#ifdef CONFIG_IBSS_RSN
1577static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1578 union wpa_event_data *data)
1579{
df4bc509 1580 struct wpa_ssid *ssid;
11ef8d35
JM
1581 if (data == NULL)
1582 return;
df4bc509
JM
1583 ssid = wpa_s->current_ssid;
1584 if (ssid == NULL)
1585 return;
1586 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1587 return;
1588
11ef8d35
JM
1589 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1590}
1591#endif /* CONFIG_IBSS_RSN */
1592
1593
036f7c4a
JM
1594#ifdef CONFIG_IEEE80211R
1595static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1596 size_t len)
1597{
1598 const u8 *sta_addr, *target_ap_addr;
1599 u16 status;
1600
1601 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1602 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1603 return; /* only SME case supported for now */
1604 if (len < 1 + 2 * ETH_ALEN + 2)
1605 return;
1606 if (data[0] != 2)
1607 return; /* Only FT Action Response is supported for now */
1608 sta_addr = data + 1;
1609 target_ap_addr = data + 1 + ETH_ALEN;
1610 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1611 wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1612 " TargetAP " MACSTR " status %u",
1613 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1614
1615 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1616 wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1617 " in FT Action Response", MAC2STR(sta_addr));
1618 return;
1619 }
1620
1621 if (status) {
1622 wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1623 "failure (status code %d)", status);
1624 /* TODO: report error to FT code(?) */
1625 return;
1626 }
1627
1628 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1629 len - (1 + 2 * ETH_ALEN + 2), 1,
1630 target_ap_addr, NULL, 0) < 0)
1631 return;
1632
fe191985
JM
1633#ifdef CONFIG_SME
1634 {
1635 struct wpa_bss *bss;
1636 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1637 if (bss)
1638 wpa_s->sme.freq = bss->freq;
62c72d72 1639 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
fe191985
JM
1640 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1641 WLAN_AUTH_FT);
1642 }
1643#endif /* CONFIG_SME */
036f7c4a
JM
1644}
1645#endif /* CONFIG_IEEE80211R */
1646
1647
7d878ca7
JM
1648static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1649 struct unprot_deauth *e)
1650{
1651#ifdef CONFIG_IEEE80211W
1652 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1653 "dropped: " MACSTR " -> " MACSTR
1654 " (reason code %u)",
1655 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1656 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1657#endif /* CONFIG_IEEE80211W */
1658}
1659
1660
1661static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1662 struct unprot_disassoc *e)
1663{
1664#ifdef CONFIG_IEEE80211W
1665 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1666 "dropped: " MACSTR " -> " MACSTR
1667 " (reason code %u)",
1668 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1669 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1670#endif /* CONFIG_IEEE80211W */
1671}
1672
1673
9646a8ab 1674void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
6fc6879b
JM
1675 union wpa_event_data *data)
1676{
1677 struct wpa_supplicant *wpa_s = ctx;
0544b242 1678 u16 reason_code = 0;
6fc6879b 1679
8401a6b0
JM
1680 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1681 event != EVENT_INTERFACE_ENABLED &&
1682 event != EVENT_INTERFACE_STATUS) {
1683 wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
1684 "disabled", event);
1685 return;
1686 }
1687
9b7124b2
JM
1688 wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
1689 event, wpa_s->ifname);
1690
6fc6879b 1691 switch (event) {
c2a04078
JM
1692 case EVENT_AUTH:
1693 sme_event_auth(wpa_s, data);
1694 break;
6fc6879b
JM
1695 case EVENT_ASSOC:
1696 wpa_supplicant_event_assoc(wpa_s, data);
1697 break;
1698 case EVENT_DISASSOC:
159dd3e2 1699 wpa_printf(MSG_DEBUG, "Disassociation notification");
2e75a2b3
JM
1700 if (data) {
1701 wpa_printf(MSG_DEBUG, " * reason %u",
1702 data->disassoc_info.reason_code);
1703 if (data->disassoc_info.addr)
1704 wpa_printf(MSG_DEBUG, " * address " MACSTR,
1705 MAC2STR(data->disassoc_info.addr));
1706 }
1d041bec 1707#ifdef CONFIG_AP
7f542069 1708 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1d041bec
JM
1709 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1710 data->disassoc_info.addr);
1711 break;
1712 }
1713#endif /* CONFIG_AP */
9fdd0fad
JM
1714 if (data) {
1715 reason_code = data->disassoc_info.reason_code;
1716 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1717 data->disassoc_info.ie,
1718 data->disassoc_info.ie_len);
1719#ifdef CONFIG_P2P
1720 wpas_p2p_disassoc_notif(
1721 wpa_s, data->disassoc_info.addr, reason_code,
1722 data->disassoc_info.ie,
1723 data->disassoc_info.ie_len);
1724#endif /* CONFIG_P2P */
1725 }
0e379654
JM
1726 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1727 sme_event_disassoc(wpa_s, data);
a84ed99e
JM
1728 /* fall through */
1729 case EVENT_DEAUTH:
0544b242 1730 if (event == EVENT_DEAUTH) {
159dd3e2 1731 wpa_printf(MSG_DEBUG, "Deauthentication notification");
2e75a2b3 1732 if (data) {
0544b242 1733 reason_code = data->deauth_info.reason_code;
2e75a2b3
JM
1734 wpa_printf(MSG_DEBUG, " * reason %u",
1735 data->deauth_info.reason_code);
1736 if (data->deauth_info.addr) {
1737 wpa_printf(MSG_DEBUG, " * address "
1738 MACSTR,
1739 MAC2STR(data->deauth_info.
1740 addr));
1741 }
9fdd0fad
JM
1742 wpa_hexdump(MSG_DEBUG,
1743 "Deauthentication frame IE(s)",
1744 data->deauth_info.ie,
1745 data->deauth_info.ie_len);
1746#ifdef CONFIG_P2P
1747 wpas_p2p_deauth_notif(
1748 wpa_s, data->deauth_info.addr,
1749 reason_code,
1750 data->deauth_info.ie,
1751 data->deauth_info.ie_len);
1752#endif /* CONFIG_P2P */
2e75a2b3 1753 }
0544b242 1754 }
1d041bec 1755#ifdef CONFIG_AP
7f542069 1756 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1d041bec
JM
1757 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1758 data->deauth_info.addr);
1759 break;
1760 }
1761#endif /* CONFIG_AP */
0544b242 1762 wpa_supplicant_event_disassoc(wpa_s, reason_code);
6fc6879b
JM
1763 break;
1764 case EVENT_MICHAEL_MIC_FAILURE:
1765 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1766 break;
1767#ifndef CONFIG_NO_SCAN_PROCESSING
1768 case EVENT_SCAN_RESULTS:
8d923a4a 1769 wpa_supplicant_event_scan_results(wpa_s, data);
6fc6879b
JM
1770 break;
1771#endif /* CONFIG_NO_SCAN_PROCESSING */
1772 case EVENT_ASSOCINFO:
1773 wpa_supplicant_event_associnfo(wpa_s, data);
1774 break;
1775 case EVENT_INTERFACE_STATUS:
1776 wpa_supplicant_event_interface_status(wpa_s, data);
1777 break;
1778 case EVENT_PMKID_CANDIDATE:
1779 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1780 break;
1781#ifdef CONFIG_PEERKEY
1782 case EVENT_STKSTART:
1783 wpa_supplicant_event_stkstart(wpa_s, data);
1784 break;
1785#endif /* CONFIG_PEERKEY */
1786#ifdef CONFIG_IEEE80211R
1787 case EVENT_FT_RESPONSE:
1788 wpa_supplicant_event_ft_response(wpa_s, data);
1789 break;
1790#endif /* CONFIG_IEEE80211R */
11ef8d35
JM
1791#ifdef CONFIG_IBSS_RSN
1792 case EVENT_IBSS_RSN_START:
1793 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1794 break;
1795#endif /* CONFIG_IBSS_RSN */
efa46078 1796 case EVENT_ASSOC_REJECT:
c05d6d18
JM
1797 if (data->assoc_reject.bssid)
1798 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1799 "bssid=" MACSTR " status_code=%u",
1800 MAC2STR(data->assoc_reject.bssid),
1801 data->assoc_reject.status_code);
1802 else
1803 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1804 "status_code=%u",
1805 data->assoc_reject.status_code);
ea78c315
JM
1806 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1807 sme_event_assoc_reject(wpa_s, data);
efa46078 1808 break;
da1fb17c 1809 case EVENT_AUTH_TIMED_OUT:
ea78c315
JM
1810 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1811 sme_event_auth_timed_out(wpa_s, data);
da1fb17c
JM
1812 break;
1813 case EVENT_ASSOC_TIMED_OUT:
ea78c315
JM
1814 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1815 sme_event_assoc_timed_out(wpa_s, data);
da1fb17c 1816 break;
f8b1f695
JM
1817#ifdef CONFIG_AP
1818 case EVENT_TX_STATUS:
9bae1be0 1819 wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS on %s dst=" MACSTR
7a80af65 1820 " type=%d stype=%d",
9bae1be0 1821 wpa_s->ifname, MAC2STR(data->tx_status.dst),
7a80af65 1822 data->tx_status.type, data->tx_status.stype);
9bae1be0
JM
1823 if (wpa_s->ap_iface == NULL) {
1824#ifdef CONFIG_P2P
1825 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1826 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1827 wpas_send_action_tx_status(
1828 wpa_s, data->tx_status.dst,
1829 data->tx_status.data,
1830 data->tx_status.data_len,
93b7ddd0
JM
1831 data->tx_status.ack ?
1832 P2P_SEND_ACTION_SUCCESS :
1833 P2P_SEND_ACTION_NO_ACK);
9bae1be0
JM
1834#endif /* CONFIG_P2P */
1835 break;
1836 }
1837#ifdef CONFIG_P2P
7a80af65
JM
1838 wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS pending_dst=" MACSTR,
1839 MAC2STR(wpa_s->parent->pending_action_dst));
9bae1be0
JM
1840 /*
1841 * Catch TX status events for Action frames we sent via group
1842 * interface in GO mode.
1843 */
1844 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1845 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1846 os_memcmp(wpa_s->parent->pending_action_dst,
1847 data->tx_status.dst, ETH_ALEN) == 0) {
1848 wpas_send_action_tx_status(
1849 wpa_s->parent, data->tx_status.dst,
1850 data->tx_status.data,
1851 data->tx_status.data_len,
1d39378a
AL
1852 data->tx_status.ack ?
1853 P2P_SEND_ACTION_SUCCESS :
1854 P2P_SEND_ACTION_NO_ACK);
f8b1f695 1855 break;
9bae1be0
JM
1856 }
1857#endif /* CONFIG_P2P */
f8b1f695
JM
1858 switch (data->tx_status.type) {
1859 case WLAN_FC_TYPE_MGMT:
1860 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1861 data->tx_status.data_len,
1862 data->tx_status.stype,
1863 data->tx_status.ack);
1864 break;
1865 case WLAN_FC_TYPE_DATA:
1866 ap_tx_status(wpa_s, data->tx_status.dst,
1867 data->tx_status.data,
1868 data->tx_status.data_len,
1869 data->tx_status.ack);
1870 break;
1871 }
1872 break;
1873 case EVENT_RX_FROM_UNKNOWN:
1874 if (wpa_s->ap_iface == NULL)
1875 break;
fdbe50ed 1876 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
f8b1f695
JM
1877 data->rx_from_unknown.len);
1878 break;
1879 case EVENT_RX_MGMT:
9bae1be0
JM
1880 if (wpa_s->ap_iface == NULL) {
1881#ifdef CONFIG_P2P
1882 u16 fc, stype;
1883 const struct ieee80211_mgmt *mgmt;
1884 mgmt = (const struct ieee80211_mgmt *)
1885 data->rx_mgmt.frame;
1886 fc = le_to_host16(mgmt->frame_control);
1887 stype = WLAN_FC_GET_STYPE(fc);
1888 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1889 data->rx_mgmt.frame_len > 24) {
1890 const u8 *src = mgmt->sa;
1891 const u8 *ie = mgmt->u.probe_req.variable;
1892 size_t ie_len = data->rx_mgmt.frame_len -
1893 (mgmt->u.probe_req.variable -
1894 data->rx_mgmt.frame);
1895 wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
1896 break;
1897 }
1898#endif /* CONFIG_P2P */
1899 wpa_printf(MSG_DEBUG, "AP: ignore received management "
1900 "frame in non-AP mode");
f8b1f695 1901 break;
9bae1be0 1902 }
2a8b7416 1903 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
f8b1f695
JM
1904 break;
1905#endif /* CONFIG_AP */
036f7c4a
JM
1906 case EVENT_RX_ACTION:
1907 wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1908 " Category=%u DataLen=%d freq=%d MHz",
1909 MAC2STR(data->rx_action.sa),
1910 data->rx_action.category, (int) data->rx_action.len,
1911 data->rx_action.freq);
1912#ifdef CONFIG_IEEE80211R
1913 if (data->rx_action.category == WLAN_ACTION_FT) {
1914 ft_rx_action(wpa_s, data->rx_action.data,
1915 data->rx_action.len);
1916 break;
1917 }
1918#endif /* CONFIG_IEEE80211R */
7d878ca7
JM
1919#ifdef CONFIG_IEEE80211W
1920#ifdef CONFIG_SME
1921 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
1922 sme_sa_query_rx(wpa_s, data->rx_action.sa,
1923 data->rx_action.data,
1924 data->rx_action.len);
1925 break;
1926 }
1927#endif /* CONFIG_SME */
1928#endif /* CONFIG_IEEE80211W */
9bae1be0
JM
1929#ifdef CONFIG_P2P
1930 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
1931 data->rx_action.sa,
1932 data->rx_action.bssid,
1933 data->rx_action.category,
1934 data->rx_action.data,
1935 data->rx_action.len, data->rx_action.freq);
1936#endif /* CONFIG_P2P */
036f7c4a 1937 break;
9bae1be0
JM
1938#ifdef CONFIG_P2P
1939 case EVENT_REMAIN_ON_CHANNEL:
1940 wpas_p2p_remain_on_channel_cb(
1941 wpa_s, data->remain_on_channel.freq,
1942 data->remain_on_channel.duration);
1943 break;
1944 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
1945 wpas_p2p_cancel_remain_on_channel_cb(
1946 wpa_s, data->remain_on_channel.freq);
1947 break;
1948 case EVENT_RX_PROBE_REQ:
1949 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
1950 data->rx_probe_req.ie,
1951 data->rx_probe_req.ie_len);
1952 break;
1953#endif /* CONFIG_P2P */
245519e0
JM
1954#ifdef CONFIG_CLIENT_MLME
1955 case EVENT_MLME_RX: {
1956 struct ieee80211_rx_status rx_status;
1957 os_memset(&rx_status, 0, sizeof(rx_status));
1958 rx_status.freq = data->mlme_rx.freq;
1959 rx_status.channel = data->mlme_rx.channel;
1960 rx_status.ssi = data->mlme_rx.ssi;
1961 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1962 &rx_status);
1963 break;
1964 }
1965#endif /* CONFIG_CLIENT_MLME */
a8e0505b
JM
1966 case EVENT_EAPOL_RX:
1967 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1968 data->eapol_rx.data,
1969 data->eapol_rx.data_len);
1970 break;
e2f74005
JM
1971 case EVENT_SIGNAL_CHANGE:
1972 bgscan_notify_signal_change(
60a972a6 1973 wpa_s, data->signal_change.above_threshold,
174fa789
PS
1974 data->signal_change.current_signal,
1975 data->signal_change.current_noise,
1976 data->signal_change.current_txrate);
e2f74005 1977 break;
8401a6b0
JM
1978 case EVENT_INTERFACE_ENABLED:
1979 wpa_printf(MSG_DEBUG, "Interface was enabled");
1980 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
199716ad 1981#ifdef CONFIG_AP
9919f7a2
JB
1982 if (!wpa_s->ap_iface) {
1983 wpa_supplicant_set_state(wpa_s,
1984 WPA_DISCONNECTED);
1985 wpa_supplicant_req_scan(wpa_s, 0, 0);
1986 } else
1987 wpa_supplicant_set_state(wpa_s,
1988 WPA_COMPLETED);
199716ad
BG
1989#else /* CONFIG_AP */
1990 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1991 wpa_supplicant_req_scan(wpa_s, 0, 0);
1992#endif /* CONFIG_AP */
8401a6b0
JM
1993 }
1994 break;
1995 case EVENT_INTERFACE_DISABLED:
1996 wpa_printf(MSG_DEBUG, "Interface was disabled");
1997 wpa_supplicant_mark_disassoc(wpa_s);
1998 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1999 break;
b5c9da8d 2000 case EVENT_CHANNEL_LIST_CHANGED:
35c5d610
JM
2001 if (wpa_s->drv_priv == NULL)
2002 break; /* Ignore event during drv initialization */
b5c9da8d
JM
2003#ifdef CONFIG_P2P
2004 wpas_p2p_update_channel_list(wpa_s);
c973f386
JM
2005#endif /* CONFIG_P2P */
2006 break;
2007 case EVENT_INTERFACE_UNAVAILABLE:
2008#ifdef CONFIG_P2P
2009 wpas_p2p_interface_unavailable(wpa_s);
7cfc4ac3
AGS
2010#endif /* CONFIG_P2P */
2011 break;
2012 case EVENT_BEST_CHANNEL:
2013 wpa_printf(MSG_DEBUG, "Best channel event received (%d %d %d)",
2014 data->best_chan.freq_24, data->best_chan.freq_5,
2015 data->best_chan.freq_overall);
2016 wpa_s->best_24_freq = data->best_chan.freq_24;
2017 wpa_s->best_5_freq = data->best_chan.freq_5;
2018 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2019#ifdef CONFIG_P2P
2020 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2021 data->best_chan.freq_5,
2022 data->best_chan.freq_overall);
b5c9da8d
JM
2023#endif /* CONFIG_P2P */
2024 break;
7d878ca7
JM
2025 case EVENT_UNPROT_DEAUTH:
2026 wpa_supplicant_event_unprot_deauth(wpa_s,
2027 &data->unprot_deauth);
2028 break;
2029 case EVENT_UNPROT_DISASSOC:
2030 wpa_supplicant_event_unprot_disassoc(wpa_s,
2031 &data->unprot_disassoc);
2032 break;
6fc6879b
JM
2033 default:
2034 wpa_printf(MSG_INFO, "Unknown event %d", event);
2035 break;
2036 }
2037}