]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/events.c
wpa_supplicant: Fix overlapping memcpy on WPS interface addition
[thirdparty/hostap.git] / wpa_supplicant / events.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Driver event processing
3d9975d5 3 * Copyright (c) 2003-2012, 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"
8bac466b 26#include "notify.h"
90973fb2 27#include "common/ieee802_11_defs.h"
54f489be 28#include "common/ieee802_11_common.h"
bbb921da 29#include "crypto/random.h"
6fc6879b
JM
30#include "blacklist.h"
31#include "wpas_glue.h"
351f09a2 32#include "wps_supplicant.h"
11ef8d35 33#include "ibss_rsn.h"
c2a04078 34#include "sme.h"
04ea7b79 35#include "gas_query.h"
9bae1be0 36#include "p2p_supplicant.h"
60b94c98 37#include "bgscan.h"
f8b1f695 38#include "ap.h"
6fa81a3b 39#include "bss.h"
9ba9fa07 40#include "scan.h"
24f6497c 41#include "offchannel.h"
6fc6879b
JM
42
43
44static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
45{
8bac466b 46 struct wpa_ssid *ssid, *old_ssid;
6fc6879b
JM
47
48 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
49 return 0;
50
f049052b
BG
51 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
52 "information");
6fc6879b
JM
53 ssid = wpa_supplicant_get_ssid(wpa_s);
54 if (ssid == NULL) {
f049052b
BG
55 wpa_msg(wpa_s, MSG_INFO,
56 "No network configuration found for the current AP");
6fc6879b
JM
57 return -1;
58 }
59
fea7c3a0 60 if (wpas_network_disabled(ssid)) {
f049052b 61 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
6fc6879b
JM
62 return -1;
63 }
64
f049052b
BG
65 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
66 "current AP");
0bf927a0 67 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
6fc6879b
JM
68 u8 wpa_ie[80];
69 size_t wpa_ie_len = sizeof(wpa_ie);
70 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
71 wpa_ie, &wpa_ie_len);
72 } else {
73 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
74 }
75
76 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
77 eapol_sm_invalidate_cached_session(wpa_s->eapol);
8bac466b 78 old_ssid = wpa_s->current_ssid;
6fc6879b
JM
79 wpa_s->current_ssid = ssid;
80 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
81 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
82 if (old_ssid != wpa_s->current_ssid)
83 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
84
85 return 0;
86}
87
88
01a17491 89void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
6fc6879b
JM
90{
91 struct wpa_supplicant *wpa_s = eloop_ctx;
92
93 if (wpa_s->countermeasures) {
94 wpa_s->countermeasures = 0;
95 wpa_drv_set_countermeasures(wpa_s, 0);
96 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
97 wpa_supplicant_req_scan(wpa_s, 0, 0);
98 }
99}
100
101
102void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
103{
8bac466b
JM
104 int bssid_changed;
105
78177a00
JM
106#ifdef CONFIG_IBSS_RSN
107 ibss_rsn_deinit(wpa_s->ibss_rsn);
108 wpa_s->ibss_rsn = NULL;
109#endif /* CONFIG_IBSS_RSN */
110
ca62e114
JM
111#ifdef CONFIG_AP
112 wpa_supplicant_ap_deinit(wpa_s);
113#endif /* CONFIG_AP */
114
8401a6b0
JM
115 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
116 return;
117
6fc6879b 118 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8bac466b 119 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
120 os_memset(wpa_s->bssid, 0, ETH_ALEN);
121 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
b1aebbc4
JM
122#ifdef CONFIG_P2P
123 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
124#endif /* CONFIG_P2P */
be8be671 125 wpa_s->current_bss = NULL;
3c85f144 126 wpa_s->assoc_freq = 0;
8fd0f0f3
HW
127#ifdef CONFIG_IEEE80211R
128#ifdef CONFIG_SME
129 if (wpa_s->sme.ft_ies)
130 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
131#endif /* CONFIG_SME */
132#endif /* CONFIG_IEEE80211R */
133
8bac466b
JM
134 if (bssid_changed)
135 wpas_notify_bssid_changed(wpa_s);
136
6fc6879b
JM
137 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
138 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
56586197 139 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
6fc6879b
JM
140 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
141 wpa_s->ap_ies_from_associnfo = 0;
142}
143
144
145static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
146{
147 struct wpa_ie_data ie;
148 int pmksa_set = -1;
149 size_t i;
150
151 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
152 ie.pmkid == NULL)
153 return;
154
155 for (i = 0; i < ie.num_pmkid; i++) {
156 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
157 ie.pmkid + i * PMKID_LEN,
158 NULL, NULL, 0);
159 if (pmksa_set == 0) {
160 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
161 break;
162 }
163 }
164
f049052b
BG
165 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
166 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
6fc6879b
JM
167}
168
169
170static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
171 union wpa_event_data *data)
172{
173 if (data == NULL) {
f049052b
BG
174 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
175 "event");
6fc6879b
JM
176 return;
177 }
f049052b
BG
178 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
179 " index=%d preauth=%d",
180 MAC2STR(data->pmkid_candidate.bssid),
181 data->pmkid_candidate.index,
182 data->pmkid_candidate.preauth);
6fc6879b
JM
183
184 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
185 data->pmkid_candidate.index,
186 data->pmkid_candidate.preauth);
187}
188
189
190static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
191{
192 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
193 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
194 return 0;
195
196#ifdef IEEE8021X_EAPOL
197 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
198 wpa_s->current_ssid &&
199 !(wpa_s->current_ssid->eapol_flags &
200 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
201 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
202 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
203 * plaintext or static WEP keys). */
204 return 0;
205 }
206#endif /* IEEE8021X_EAPOL */
207
208 return 1;
209}
210
211
212/**
213 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
214 * @wpa_s: pointer to wpa_supplicant data
215 * @ssid: Configuration data for the network
216 * Returns: 0 on success, -1 on failure
217 *
218 * This function is called when starting authentication with a network that is
219 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
220 */
221int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
222 struct wpa_ssid *ssid)
223{
224#ifdef IEEE8021X_EAPOL
b832d34c 225#ifdef PCSC_FUNCS
6fc6879b
JM
226 int aka = 0, sim = 0, type;
227
228 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
229 return 0;
230
231 if (ssid->eap.eap_methods == NULL) {
232 sim = 1;
233 aka = 1;
234 } else {
235 struct eap_method_type *eap = ssid->eap.eap_methods;
236 while (eap->vendor != EAP_VENDOR_IETF ||
237 eap->method != EAP_TYPE_NONE) {
238 if (eap->vendor == EAP_VENDOR_IETF) {
239 if (eap->method == EAP_TYPE_SIM)
240 sim = 1;
3d332fe7
JM
241 else if (eap->method == EAP_TYPE_AKA ||
242 eap->method == EAP_TYPE_AKA_PRIME)
6fc6879b
JM
243 aka = 1;
244 }
245 eap++;
246 }
247 }
248
249 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
250 sim = 0;
3d332fe7
JM
251 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
252 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
253 NULL)
6fc6879b
JM
254 aka = 0;
255
256 if (!sim && !aka) {
f049052b
BG
257 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
258 "use SIM, but neither EAP-SIM nor EAP-AKA are "
259 "enabled");
6fc6879b
JM
260 return 0;
261 }
262
f049052b
BG
263 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
264 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
6fc6879b
JM
265 if (sim && aka)
266 type = SCARD_TRY_BOTH;
267 else if (aka)
268 type = SCARD_USIM_ONLY;
269 else
270 type = SCARD_GSM_SIM_ONLY;
271
f64adcd7 272 wpa_s->scard = scard_init(type, NULL);
6fc6879b 273 if (wpa_s->scard == NULL) {
f049052b
BG
274 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
275 "(pcsc-lite)");
6fc6879b
JM
276 return -1;
277 }
278 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
279 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
b832d34c 280#endif /* PCSC_FUNCS */
6fc6879b
JM
281#endif /* IEEE8021X_EAPOL */
282
283 return 0;
284}
285
286
287#ifndef CONFIG_NO_SCAN_PROCESSING
288static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
289 struct wpa_ssid *ssid)
290{
291 int i, privacy = 0;
292
293 if (ssid->mixed_cell)
294 return 1;
295
0632542b
AT
296#ifdef CONFIG_WPS
297 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
298 return 1;
299#endif /* CONFIG_WPS */
300
6fc6879b
JM
301 for (i = 0; i < NUM_WEP_KEYS; i++) {
302 if (ssid->wep_key_len[i]) {
303 privacy = 1;
304 break;
305 }
306 }
307#ifdef IEEE8021X_EAPOL
308 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
309 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
310 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
311 privacy = 1;
312#endif /* IEEE8021X_EAPOL */
313
29fbc522
JM
314 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
315 privacy = 1;
316
6fc6879b
JM
317 if (bss->caps & IEEE80211_CAP_PRIVACY)
318 return privacy;
319 return !privacy;
320}
321
322
a6099152
JM
323static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
324 struct wpa_ssid *ssid,
6fc6879b
JM
325 struct wpa_scan_res *bss)
326{
327 struct wpa_ie_data ie;
328 int proto_match = 0;
329 const u8 *rsn_ie, *wpa_ie;
351f09a2 330 int ret;
43882f1e 331 int wep_ok;
6fc6879b 332
a6099152 333 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
351f09a2
JM
334 if (ret >= 0)
335 return ret;
ad08c363 336
43882f1e
JM
337 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
338 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
339 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
340 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
341 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
342
6fc6879b
JM
343 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
344 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
345 proto_match++;
346
347 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
f049052b
BG
348 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
349 "failed");
6fc6879b
JM
350 break;
351 }
43882f1e
JM
352
353 if (wep_ok &&
354 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
355 {
f049052b
BG
356 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
357 "in RSN IE");
43882f1e
JM
358 return 1;
359 }
360
6fc6879b 361 if (!(ie.proto & ssid->proto)) {
f049052b
BG
362 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
363 "mismatch");
6fc6879b
JM
364 break;
365 }
366
367 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
f049052b
BG
368 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
369 "cipher mismatch");
6fc6879b
JM
370 break;
371 }
372
373 if (!(ie.group_cipher & ssid->group_cipher)) {
f049052b
BG
374 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
375 "cipher mismatch");
6fc6879b
JM
376 break;
377 }
378
379 if (!(ie.key_mgmt & ssid->key_mgmt)) {
f049052b
BG
380 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
381 "mismatch");
6fc6879b
JM
382 break;
383 }
384
385#ifdef CONFIG_IEEE80211W
0b60b0aa 386 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
70f8cc8e 387 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
f049052b
BG
388 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
389 "frame protection");
6fc6879b
JM
390 break;
391 }
392#endif /* CONFIG_IEEE80211W */
393
f049052b 394 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
6fc6879b
JM
395 return 1;
396 }
397
398 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
399 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
400 proto_match++;
401
402 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
f049052b
BG
403 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
404 "failed");
6fc6879b
JM
405 break;
406 }
43882f1e
JM
407
408 if (wep_ok &&
409 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
410 {
f049052b
BG
411 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
412 "in WPA IE");
43882f1e
JM
413 return 1;
414 }
415
6fc6879b 416 if (!(ie.proto & ssid->proto)) {
f049052b
BG
417 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
418 "mismatch");
6fc6879b
JM
419 break;
420 }
421
422 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
f049052b
BG
423 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
424 "cipher mismatch");
6fc6879b
JM
425 break;
426 }
427
428 if (!(ie.group_cipher & ssid->group_cipher)) {
f049052b
BG
429 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
430 "cipher mismatch");
6fc6879b
JM
431 break;
432 }
433
434 if (!(ie.key_mgmt & ssid->key_mgmt)) {
f049052b
BG
435 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
436 "mismatch");
6fc6879b
JM
437 break;
438 }
439
f049052b 440 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
6fc6879b
JM
441 return 1;
442 }
443
cc5e390d 444 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
df83fb7d 445 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
f049052b 446 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
cc5e390d
JM
447 return 0;
448 }
6fc6879b 449
c2f1fe41
JM
450 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
451 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
452 return 1;
453 }
454
455 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
456 "WPA/WPA2");
457
458 return 0;
6fc6879b
JM
459}
460
461
b766a9a2
JM
462static int freq_allowed(int *freqs, int freq)
463{
464 int i;
465
466 if (freqs == NULL)
467 return 1;
468
469 for (i = 0; freqs[i]; i++)
470 if (freqs[i] == freq)
471 return 1;
472 return 0;
473}
474
475
e1e8cae3
CL
476static int ht_supported(const struct hostapd_hw_modes *mode)
477{
478 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
479 /*
480 * The driver did not indicate whether it supports HT. Assume
481 * it does to avoid connection issues.
482 */
483 return 1;
484 }
485
486 /*
487 * IEEE Std 802.11n-2009 20.1.1:
488 * An HT non-AP STA shall support all EQM rates for one spatial stream.
489 */
490 return mode->mcs_set[0] == 0xff;
491}
492
493
494static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
495{
496 const struct hostapd_hw_modes *mode = NULL, *modes;
497 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
498 const u8 *rate_ie;
499 int i, j, k;
500
a6d94e1b
JM
501 if (bss->freq == 0)
502 return 1; /* Cannot do matching without knowing band */
503
e1e8cae3
CL
504 modes = wpa_s->hw.modes;
505 if (modes == NULL) {
506 /*
507 * The driver does not provide any additional information
508 * about the utilized hardware, so allow the connection attempt
509 * to continue.
510 */
511 return 1;
512 }
513
514 for (i = 0; i < wpa_s->hw.num_modes; i++) {
515 for (j = 0; j < modes[i].num_channels; j++) {
516 int freq = modes[i].channels[j].freq;
517 if (freq == bss->freq) {
518 if (mode &&
519 mode->mode == HOSTAPD_MODE_IEEE80211G)
520 break; /* do not allow 802.11b replace
521 * 802.11g */
522 mode = &modes[i];
523 break;
524 }
525 }
526 }
527
528 if (mode == NULL)
529 return 0;
530
531 for (i = 0; i < (int) sizeof(scan_ie); i++) {
532 rate_ie = wpa_scan_get_ie(bss, scan_ie[i]);
533 if (rate_ie == NULL)
534 continue;
535
536 for (j = 2; j < rate_ie[1] + 2; j++) {
537 int flagged = !!(rate_ie[j] & 0x80);
538 int r = (rate_ie[j] & 0x7f) * 5;
539
540 /*
541 * IEEE Std 802.11n-2009 7.3.2.2:
542 * The new BSS Membership selector value is encoded
543 * like a legacy basic rate, but it is not a rate and
544 * only indicates if the BSS members are required to
545 * support the mandatory features of Clause 20 [HT PHY]
546 * in order to join the BSS.
547 */
548 if (flagged && ((rate_ie[j] & 0x7f) ==
549 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
550 if (!ht_supported(mode)) {
551 wpa_dbg(wpa_s, MSG_DEBUG,
552 " hardware does not support "
553 "HT PHY");
554 return 0;
555 }
556 continue;
557 }
558
559 if (!flagged)
560 continue;
561
562 /* check for legacy basic rates */
563 for (k = 0; k < mode->num_rates; k++) {
564 if (mode->rates[k] == r)
565 break;
566 }
567 if (k == mode->num_rates) {
568 /*
569 * IEEE Std 802.11-2007 7.3.2.2 demands that in
570 * order to join a BSS all required rates
571 * have to be supported by the hardware.
572 */
573 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
574 "not support required rate %d.%d Mbps",
575 r / 10, r % 10);
576 return 0;
577 }
578 }
579 }
580
581 return 1;
582}
583
584
d8d940b7
JM
585static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
586 int i, struct wpa_scan_res *bss,
587 struct wpa_ssid *group)
6fc6879b 588{
d8d940b7
JM
589 const u8 *ssid_;
590 u8 wpa_ie_len, rsn_ie_len, ssid_len;
591 int wpa;
6fc6879b
JM
592 struct wpa_blacklist *e;
593 const u8 *ie;
d8d940b7 594 struct wpa_ssid *ssid;
6fc6879b 595
d8d940b7
JM
596 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
597 ssid_ = ie ? ie + 2 : (u8 *) "";
598 ssid_len = ie ? ie[1] : 0;
6fc6879b 599
d8d940b7
JM
600 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
601 wpa_ie_len = ie ? ie[1] : 0;
6fc6879b 602
d8d940b7
JM
603 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
604 rsn_ie_len = ie ? ie[1] : 0;
6fc6879b 605
f049052b
BG
606 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
607 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
608 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
609 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
610 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
9cf32261 611
d8d940b7 612 e = wpa_blacklist_get(wpa_s, bss->bssid);
5471c343
JM
613 if (e) {
614 int limit = 1;
615 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
616 /*
617 * When only a single network is enabled, we can
618 * trigger blacklisting on the first failure. This
619 * should not be done with multiple enabled networks to
620 * avoid getting forced to move into a worse ESS on
621 * single error if there are no other BSSes of the
622 * current ESS.
623 */
624 limit = 0;
625 }
626 if (e->count > limit) {
f049052b
BG
627 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
628 "(count=%d limit=%d)", e->count, limit);
c2197bc9 629 return NULL;
5471c343 630 }
d8d940b7 631 }
6fc6879b 632
d8d940b7 633 if (ssid_len == 0) {
f049052b 634 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
c2197bc9 635 return NULL;
d8d940b7 636 }
e81634cd 637
d8d940b7 638 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
6fc6879b 639
d8d940b7
JM
640 for (ssid = group; ssid; ssid = ssid->pnext) {
641 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
ad08c363 642
fea7c3a0 643 if (wpas_network_disabled(ssid)) {
f049052b 644 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
0c703df3 645 continue;
d8d940b7 646 }
9cf32261 647
ad08c363 648#ifdef CONFIG_WPS
f648bc7d 649 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
f049052b
BG
650 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
651 "(WPS)");
f648bc7d
JM
652 continue;
653 }
654
d8d940b7
JM
655 if (wpa && ssid->ssid_len == 0 &&
656 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
657 check_ssid = 0;
658
659 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
660 /* Only allow wildcard SSID match if an AP
661 * advertises active WPS operation that matches
662 * with our mode. */
663 check_ssid = 1;
ad08c363 664 if (ssid->ssid_len == 0 &&
a6099152 665 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
ad08c363 666 check_ssid = 0;
d8d940b7 667 }
ad08c363
JM
668#endif /* CONFIG_WPS */
669
7d232e23
ZC
670 if (ssid->bssid_set && ssid->ssid_len == 0 &&
671 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
672 check_ssid = 0;
673
d8d940b7
JM
674 if (check_ssid &&
675 (ssid_len != ssid->ssid_len ||
676 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
f049052b 677 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
0c703df3 678 continue;
6fc6879b 679 }
6fc6879b 680
d8d940b7
JM
681 if (ssid->bssid_set &&
682 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
f049052b 683 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
0c703df3 684 continue;
d8d940b7 685 }
6fc6879b 686
cc5e390d 687 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
0c703df3 688 continue;
9cf32261 689
d8d940b7
JM
690 if (!wpa &&
691 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
692 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
693 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
f049052b
BG
694 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
695 "not allowed");
0c703df3 696 continue;
6fc6879b 697 }
9cf32261 698
29fbc522 699 if (!wpa_supplicant_match_privacy(bss, ssid)) {
f049052b
BG
700 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
701 "mismatch");
0c703df3 702 continue;
e81634cd
JM
703 }
704
29fbc522 705 if (bss->caps & IEEE80211_CAP_IBSS) {
f049052b
BG
706 wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
707 "network");
0c703df3 708 continue;
d8d940b7 709 }
b766a9a2 710
d8d940b7 711 if (!freq_allowed(ssid->freq_list, bss->freq)) {
f049052b
BG
712 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
713 "allowed");
0c703df3 714 continue;
6fc6879b 715 }
d8d940b7 716
e1e8cae3
CL
717 if (!rate_match(wpa_s, bss)) {
718 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
719 "not match");
720 continue;
721 }
722
73e49269
JM
723#ifdef CONFIG_P2P
724 /*
725 * TODO: skip the AP if its P2P IE has Group Formation
726 * bit set in the P2P Group Capability Bitmap and we
727 * are not in Group Formation with that device.
728 */
729#endif /* CONFIG_P2P */
730
d8d940b7
JM
731 /* Matching configuration found */
732 return ssid;
6fc6879b
JM
733 }
734
d8d940b7 735 /* No matching configuration found */
c2197bc9 736 return NULL;
9cf32261
JM
737}
738
739
6fa81a3b 740static struct wpa_bss *
a1fd2ce5
JM
741wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
742 struct wpa_scan_results *scan_res,
743 struct wpa_ssid *group,
9cf32261
JM
744 struct wpa_ssid **selected_ssid)
745{
d8d940b7 746 size_t i;
9cf32261 747
f049052b
BG
748 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
749 group->priority);
9cf32261 750
d8d940b7
JM
751 for (i = 0; i < scan_res->num; i++) {
752 struct wpa_scan_res *bss = scan_res->res[i];
753 const u8 *ie, *ssid;
754 u8 ssid_len;
755
756 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
757 if (!*selected_ssid)
758 continue;
759
760 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
761 ssid = ie ? ie + 2 : (u8 *) "";
762 ssid_len = ie ? ie[1] : 0;
9cf32261 763
f049052b
BG
764 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
765 " ssid='%s'",
766 MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
d8d940b7
JM
767 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
768 }
769
770 return NULL;
6fc6879b
JM
771}
772
773
6fa81a3b 774static struct wpa_bss *
09b9df4e 775wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
a1fd2ce5 776 struct wpa_scan_results *scan_res,
09b9df4e 777 struct wpa_ssid **selected_ssid)
6fc6879b 778{
6fa81a3b 779 struct wpa_bss *selected = NULL;
09b9df4e
JM
780 int prio;
781
782 while (selected == NULL) {
783 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
784 selected = wpa_supplicant_select_bss(
a1fd2ce5 785 wpa_s, scan_res, wpa_s->conf->pssid[prio],
09b9df4e
JM
786 selected_ssid);
787 if (selected)
788 break;
789 }
790
8945cc45
BM
791 if (selected == NULL && wpa_s->blacklist &&
792 !wpa_s->countermeasures) {
f049052b
BG
793 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
794 "blacklist and try again");
09b9df4e
JM
795 wpa_blacklist_clear(wpa_s);
796 wpa_s->blacklist_cleared++;
797 } else if (selected == NULL)
798 break;
799 }
800
801 return selected;
802}
803
804
805static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
977b1174 806 int timeout_sec, int timeout_usec)
09b9df4e 807{
24f76940 808 if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
4f34d51a
SL
809 /*
810 * No networks are enabled; short-circuit request so
811 * we don't wait timeout seconds before transitioning
812 * to INACTIVE state.
813 */
814 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
815 return;
09b9df4e 816 }
977b1174 817 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
09b9df4e
JM
818}
819
820
5cbd88d9
JJ
821int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
822 struct wpa_bss *selected,
823 struct wpa_ssid *ssid)
09b9df4e
JM
824{
825 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
826 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
827 "PBC session overlap");
b73bf0a7
JM
828#ifdef CONFIG_P2P
829 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
5cbd88d9 830 return -1;
b73bf0a7 831#endif /* CONFIG_P2P */
199716ad
BG
832
833#ifdef CONFIG_WPS
7736f18b 834 wpas_wps_cancel(wpa_s);
199716ad 835#endif /* CONFIG_WPS */
5cbd88d9 836 return -1;
09b9df4e
JM
837 }
838
839 /*
840 * Do not trigger new association unless the BSSID has changed or if
841 * reassociation is requested. If we are in process of associating with
842 * the selected BSSID, do not trigger new attempt.
843 */
844 if (wpa_s->reassociate ||
845 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
e29853bb
BG
846 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
847 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
09b9df4e
JM
848 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
849 0))) {
850 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
977b1174 851 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
5cbd88d9 852 return 0;
09b9df4e 853 }
e29853bb
BG
854 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
855 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
856 " pending: " MACSTR " wpa_state: %s",
857 wpa_s->reassociate, MAC2STR(selected->bssid),
858 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
859 wpa_supplicant_state_txt(wpa_s->wpa_state));
09b9df4e
JM
860 wpa_supplicant_associate(wpa_s, selected, ssid);
861 } else {
f049052b
BG
862 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
863 "selected AP");
09b9df4e 864 }
5cbd88d9
JJ
865
866 return 0;
09b9df4e
JM
867}
868
869
b55aaa5f
JM
870static struct wpa_ssid *
871wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
872{
873 int prio;
874 struct wpa_ssid *ssid;
875
876 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
877 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
878 {
fea7c3a0 879 if (wpas_network_disabled(ssid))
b55aaa5f
JM
880 continue;
881 if (ssid->mode == IEEE80211_MODE_IBSS ||
882 ssid->mode == IEEE80211_MODE_AP)
883 return ssid;
884 }
885 }
886 return NULL;
887}
888
889
a1fd2ce5
JM
890/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
891 * on BSS added and BSS changed events */
6ae93185 892static void wpa_supplicant_rsn_preauth_scan_results(
6d28fb96 893 struct wpa_supplicant *wpa_s)
6ae93185 894{
6d28fb96 895 struct wpa_bss *bss;
6ae93185
JM
896
897 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
898 return;
899
6d28fb96 900 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
6ae93185 901 const u8 *ssid, *rsn;
6ae93185 902
6d28fb96 903 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
6ae93185
JM
904 if (ssid == NULL)
905 continue;
906
6d28fb96 907 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
6ae93185
JM
908 if (rsn == NULL)
909 continue;
910
6d28fb96 911 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
6ae93185
JM
912 }
913
914}
915
916
48563d86
JM
917static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
918 struct wpa_bss *selected,
919 struct wpa_ssid *ssid,
920 struct wpa_scan_results *scan_res)
921{
922 size_t i;
923 struct wpa_scan_res *current_bss = NULL;
924 int min_diff;
925
926 if (wpa_s->reassociate)
927 return 1; /* explicit request to reassociate */
928 if (wpa_s->wpa_state < WPA_ASSOCIATED)
929 return 1; /* we are not associated; continue */
930 if (wpa_s->current_ssid == NULL)
931 return 1; /* unknown current SSID */
932 if (wpa_s->current_ssid != ssid)
933 return 1; /* different network block */
934
22628eca
JM
935 if (wpas_driver_bss_selection(wpa_s))
936 return 0; /* Driver-based roaming */
937
48563d86
JM
938 for (i = 0; i < scan_res->num; i++) {
939 struct wpa_scan_res *res = scan_res->res[i];
940 const u8 *ie;
941 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
942 continue;
943
944 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
945 if (ie == NULL)
946 continue;
947 if (ie[1] != wpa_s->current_ssid->ssid_len ||
948 os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
949 continue;
950 current_bss = res;
951 break;
952 }
953
954 if (!current_bss)
955 return 1; /* current BSS not seen in scan results */
956
e9af53ad 957#ifndef CONFIG_NO_ROAMING
f049052b
BG
958 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
959 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
960 MAC2STR(current_bss->bssid), current_bss->level);
961 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
962 MAC2STR(selected->bssid), selected->level);
48563d86 963
ac26ebd8
JM
964 if (wpa_s->current_ssid->bssid_set &&
965 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
966 0) {
f049052b
BG
967 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
968 "has preferred BSSID");
ac26ebd8
JM
969 return 1;
970 }
971
48563d86
JM
972 min_diff = 2;
973 if (current_bss->level < 0) {
974 if (current_bss->level < -85)
975 min_diff = 1;
976 else if (current_bss->level < -80)
977 min_diff = 2;
978 else if (current_bss->level < -75)
979 min_diff = 3;
980 else if (current_bss->level < -70)
981 min_diff = 4;
982 else
983 min_diff = 5;
984 }
985 if (abs(current_bss->level - selected->level) < min_diff) {
f049052b
BG
986 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
987 "in signal level");
48563d86
JM
988 return 0;
989 }
990
991 return 1;
e9af53ad
DS
992#else /* CONFIG_NO_ROAMING */
993 return 0;
994#endif /* CONFIG_NO_ROAMING */
48563d86
JM
995}
996
cd2f4ddf 997
e1504976
BG
998/* Return < 0 if no scan results could be fetched. */
999static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1000 union wpa_event_data *data)
09b9df4e 1001{
6fa81a3b 1002 struct wpa_bss *selected;
6fc6879b 1003 struct wpa_ssid *ssid = NULL;
a1fd2ce5 1004 struct wpa_scan_results *scan_res;
5bc0cdb7
JM
1005 int ap = 0;
1006
1007#ifdef CONFIG_AP
1008 if (wpa_s->ap_iface)
1009 ap = 1;
1010#endif /* CONFIG_AP */
6fc6879b 1011
cb8564b1
DW
1012 wpa_supplicant_notify_scanning(wpa_s, 0);
1013
39185dfa
JM
1014#ifdef CONFIG_P2P
1015 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
1016 wpa_s->global->p2p != NULL) {
1017 wpa_s->p2p_cb_on_scan_complete = 0;
1018 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1019 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1020 "stopped scan processing");
1021 return -1;
1022 }
1023 }
1024#endif /* CONFIG_P2P */
1025
a1fd2ce5
JM
1026 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1027 data ? &data->scan_info :
1028 NULL, 1);
1029 if (scan_res == NULL) {
5bc0cdb7 1030 if (wpa_s->conf->ap_scan == 2 || ap)
e1504976 1031 return -1;
f049052b
BG
1032 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1033 "scanning again");
977b1174 1034 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
e1504976 1035 return -1;
6fc6879b
JM
1036 }
1037
bbb921da
JM
1038#ifndef CONFIG_NO_RANDOM_POOL
1039 size_t i, num;
1040 num = scan_res->num;
1041 if (num > 10)
1042 num = 10;
1043 for (i = 0; i < num; i++) {
1044 u8 buf[5];
1045 struct wpa_scan_res *res = scan_res->res[i];
1046 buf[0] = res->bssid[5];
1047 buf[1] = res->qual & 0xff;
1048 buf[2] = res->noise & 0xff;
1049 buf[3] = res->level & 0xff;
1050 buf[4] = res->tsf & 0xff;
1051 random_add_randomness(buf, sizeof(buf));
1052 }
1053#endif /* CONFIG_NO_RANDOM_POOL */
1054
64e58f51 1055 if (wpa_s->scan_res_handler) {
860fddbb
JB
1056 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1057 struct wpa_scan_results *scan_res);
1058
1059 scan_res_handler = wpa_s->scan_res_handler;
64e58f51 1060 wpa_s->scan_res_handler = NULL;
860fddbb
JB
1061 scan_res_handler(wpa_s, scan_res);
1062
64e58f51 1063 wpa_scan_results_free(scan_res);
e1504976 1064 return 0;
64e58f51
JM
1065 }
1066
5bc0cdb7 1067 if (ap) {
f049052b 1068 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
c202f19c
JB
1069#ifdef CONFIG_AP
1070 if (wpa_s->ap_iface->scan_cb)
1071 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1072#endif /* CONFIG_AP */
5bc0cdb7 1073 wpa_scan_results_free(scan_res);
e1504976 1074 return 0;
5bc0cdb7
JM
1075 }
1076
f049052b 1077 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
24f76940
JM
1078 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1079 wpas_notify_scan_results(wpa_s);
6fc6879b 1080
8bac466b
JM
1081 wpas_notify_scan_done(wpa_s, 1);
1082
c3701c66
RM
1083 if (sme_proc_obss_scan(wpa_s) > 0) {
1084 wpa_scan_results_free(scan_res);
1085 return 0;
1086 }
1087
a1fd2ce5
JM
1088 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1089 wpa_scan_results_free(scan_res);
e1504976 1090 return 0;
a1fd2ce5 1091 }
6fc6879b 1092
3180d7a2
SO
1093 if (wpa_s->disconnected) {
1094 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
a1fd2ce5 1095 wpa_scan_results_free(scan_res);
e1504976 1096 return 0;
3180d7a2
SO
1097 }
1098
22628eca
JM
1099 if (!wpas_driver_bss_selection(wpa_s) &&
1100 bgscan_notify_scan(wpa_s, scan_res) == 1) {
a1fd2ce5 1101 wpa_scan_results_free(scan_res);
e1504976 1102 return 0;
a1fd2ce5
JM
1103 }
1104
a1fd2ce5 1105 selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
6ae93185 1106
6fc6879b 1107 if (selected) {
48563d86
JM
1108 int skip;
1109 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
1110 scan_res);
1111 wpa_scan_results_free(scan_res);
1bbff09e
JM
1112 if (skip) {
1113 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
e1504976 1114 return 0;
1bbff09e 1115 }
5cbd88d9
JJ
1116
1117 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1118 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1119 return -1;
1120 }
6d28fb96 1121 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
6fc6879b 1122 } else {
48563d86 1123 wpa_scan_results_free(scan_res);
f049052b 1124 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
b55aaa5f
JM
1125 ssid = wpa_supplicant_pick_new_network(wpa_s);
1126 if (ssid) {
f049052b 1127 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
b55aaa5f 1128 wpa_supplicant_associate(wpa_s, NULL, ssid);
6d28fb96 1129 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
977b1174 1130 } else {
67b9bd08 1131 int timeout_sec = wpa_s->scan_interval;
977b1174 1132 int timeout_usec = 0;
0817de90 1133#ifdef CONFIG_P2P
aa9bb764
JM
1134 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1135 return 0;
1136
0817de90
JM
1137 if (wpa_s->p2p_in_provisioning) {
1138 /*
1139 * Use shorter wait during P2P Provisioning
1140 * state to speed up group formation.
1141 */
1142 timeout_sec = 0;
1143 timeout_usec = 250000;
a4cba8f1
LC
1144 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1145 timeout_usec);
1146 return 0;
0817de90
JM
1147 }
1148#endif /* CONFIG_P2P */
a4cba8f1
LC
1149 if (wpa_supplicant_req_sched_scan(wpa_s))
1150 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1151 timeout_usec);
977b1174 1152 }
6fc6879b 1153 }
e1504976 1154 return 0;
6fc6879b 1155}
6859f1cb
BG
1156
1157
1158static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1159 union wpa_event_data *data)
1160{
1161 const char *rn, *rn2;
1162 struct wpa_supplicant *ifs;
1163
e1504976
BG
1164 if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
1165 /*
1166 * If no scan results could be fetched, then no need to
1167 * notify those interfaces that did not actually request
1168 * this scan.
1169 */
1170 return;
1171 }
6859f1cb
BG
1172
1173 /*
1174 * Check other interfaces to see if they have the same radio-name. If
1175 * so, they get updated with this same scan info.
1176 */
1177 if (!wpa_s->driver->get_radio_name)
1178 return;
1179
1180 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1181 if (rn == NULL || rn[0] == '\0')
1182 return;
1183
f049052b
BG
1184 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1185 "sharing same radio (%s) in event_scan_results", rn);
6859f1cb
BG
1186
1187 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1188 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1189 continue;
1190
1191 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1192 if (rn2 && os_strcmp(rn, rn2) == 0) {
1193 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1194 "sibling", ifs->ifname);
1195 _wpa_supplicant_event_scan_results(ifs, data);
1196 }
1197 }
1198}
1199
6fc6879b
JM
1200#endif /* CONFIG_NO_SCAN_PROCESSING */
1201
1202
579ce771
JM
1203static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1204 union wpa_event_data *data)
6fc6879b
JM
1205{
1206 int l, len, found = 0, wpa_found, rsn_found;
c2a04078 1207 const u8 *p;
6fc6879b 1208
f049052b 1209 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
6fc6879b
JM
1210 if (data->assoc_info.req_ies)
1211 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1212 data->assoc_info.req_ies_len);
52c9e6f3 1213 if (data->assoc_info.resp_ies) {
6fc6879b
JM
1214 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1215 data->assoc_info.resp_ies_len);
52c9e6f3
JM
1216#ifdef CONFIG_TDLS
1217 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1218 data->assoc_info.resp_ies_len);
1219#endif /* CONFIG_TDLS */
1220 }
6fc6879b
JM
1221 if (data->assoc_info.beacon_ies)
1222 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1223 data->assoc_info.beacon_ies,
1224 data->assoc_info.beacon_ies_len);
4832ecd7 1225 if (data->assoc_info.freq)
f049052b
BG
1226 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1227 data->assoc_info.freq);
6fc6879b
JM
1228
1229 p = data->assoc_info.req_ies;
1230 l = data->assoc_info.req_ies_len;
1231
1232 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1233 while (p && l >= 2) {
1234 len = p[1] + 2;
1235 if (len > l) {
1236 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1237 p, l);
1238 break;
1239 }
1240 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1241 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1242 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1243 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1244 break;
1245 found = 1;
1246 wpa_find_assoc_pmkid(wpa_s);
1247 break;
1248 }
1249 l -= len;
1250 p += len;
1251 }
1252 if (!found && data->assoc_info.req_ies)
1253 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1254
c2a04078 1255#ifdef CONFIG_IEEE80211R
62c72d72
JM
1256#ifdef CONFIG_SME
1257 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1258 u8 bssid[ETH_ALEN];
1259 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1260 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1261 data->assoc_info.resp_ies,
1262 data->assoc_info.resp_ies_len,
1263 bssid) < 0) {
f049052b
BG
1264 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1265 "Reassociation Response failed");
579ce771
JM
1266 wpa_supplicant_deauthenticate(
1267 wpa_s, WLAN_REASON_INVALID_IE);
1268 return -1;
62c72d72
JM
1269 }
1270 }
62c72d72 1271
c2a04078
JM
1272 p = data->assoc_info.resp_ies;
1273 l = data->assoc_info.resp_ies_len;
1274
54f489be 1275#ifdef CONFIG_WPS_STRICT
5dac11e0 1276 if (p && wpa_s->current_ssid &&
54f489be
JM
1277 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1278 struct wpabuf *wps;
1279 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1280 if (wps == NULL) {
f049052b
BG
1281 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1282 "include WPS IE in (Re)Association Response");
54f489be
JM
1283 return -1;
1284 }
1285
1286 if (wps_validate_assoc_resp(wps) < 0) {
1287 wpabuf_free(wps);
1288 wpa_supplicant_deauthenticate(
1289 wpa_s, WLAN_REASON_INVALID_IE);
1290 return -1;
1291 }
1292 wpabuf_free(wps);
1293 }
1294#endif /* CONFIG_WPS_STRICT */
1295
e7846b68 1296 /* Go through the IEs and make a copy of the MDIE, if present. */
c2a04078
JM
1297 while (p && l >= 2) {
1298 len = p[1] + 2;
1299 if (len > l) {
1300 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1301 p, l);
1302 break;
1303 }
e7846b68
JM
1304 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1305 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1306 wpa_s->sme.ft_used = 1;
1307 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1308 MOBILITY_DOMAIN_ID_LEN);
1309 break;
1310 }
c2a04078
JM
1311 l -= len;
1312 p += len;
1313 }
e7846b68 1314#endif /* CONFIG_SME */
c2a04078 1315
e7846b68
JM
1316 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1317 data->assoc_info.resp_ies_len);
c2a04078
JM
1318#endif /* CONFIG_IEEE80211R */
1319
6fc6879b
JM
1320 /* WPA/RSN IE from Beacon/ProbeResp */
1321 p = data->assoc_info.beacon_ies;
1322 l = data->assoc_info.beacon_ies_len;
1323
1324 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1325 */
1326 wpa_found = rsn_found = 0;
1327 while (p && l >= 2) {
1328 len = p[1] + 2;
1329 if (len > l) {
1330 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1331 p, l);
1332 break;
1333 }
1334 if (!wpa_found &&
1335 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1336 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1337 wpa_found = 1;
1338 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1339 }
1340
1341 if (!rsn_found &&
1342 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1343 rsn_found = 1;
1344 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1345 }
1346
1347 l -= len;
1348 p += len;
1349 }
1350
1351 if (!wpa_found && data->assoc_info.beacon_ies)
1352 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1353 if (!rsn_found && data->assoc_info.beacon_ies)
1354 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1355 if (wpa_found || rsn_found)
1356 wpa_s->ap_ies_from_associnfo = 1;
4832ecd7 1357
117e812d
JM
1358 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1359 wpa_s->assoc_freq != data->assoc_info.freq) {
1360 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1361 "%u to %u MHz",
1362 wpa_s->assoc_freq, data->assoc_info.freq);
1363 wpa_supplicant_update_scan_results(wpa_s);
1364 }
1365
4832ecd7 1366 wpa_s->assoc_freq = data->assoc_info.freq;
579ce771
JM
1367
1368 return 0;
6fc6879b
JM
1369}
1370
1371
cd2f4ddf
SM
1372static struct wpa_bss * wpa_supplicant_get_new_bss(
1373 struct wpa_supplicant *wpa_s, const u8 *bssid)
1374{
1375 struct wpa_bss *bss = NULL;
1376 struct wpa_ssid *ssid = wpa_s->current_ssid;
1377
1378 if (ssid->ssid_len > 0)
1379 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1380 if (!bss)
1381 bss = wpa_bss_get_bssid(wpa_s, bssid);
1382
1383 return bss;
1384}
1385
1386
ad9ee4d4
JM
1387static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1388{
1389 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1390
1391 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1392 return -1;
1393
1394 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1395 return 0;
1396
1397 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1398 WPA_IE_VENDOR_TYPE);
1399 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1400
1401 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1402 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1403 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1404 bss_rsn ? 2 + bss_rsn[1] : 0))
1405 return -1;
1406
1407 return 0;
1408}
1409
1410
6fc6879b
JM
1411static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1412 union wpa_event_data *data)
1413{
1414 u8 bssid[ETH_ALEN];
1d041bec 1415 int ft_completed;
8bac466b 1416 int bssid_changed;
0194fedb 1417 struct wpa_driver_capa capa;
6fc6879b 1418
1d041bec
JM
1419#ifdef CONFIG_AP
1420 if (wpa_s->ap_iface) {
1421 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1422 data->assoc_info.addr,
1423 data->assoc_info.req_ies,
39b08b5f
SP
1424 data->assoc_info.req_ies_len,
1425 data->assoc_info.reassoc);
1d041bec
JM
1426 return;
1427 }
1428#endif /* CONFIG_AP */
1429
1430 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
579ce771
JM
1431 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1432 return;
6fc6879b
JM
1433
1434 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
17fbb751
JM
1435 if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1436 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
f049052b 1437 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
6fc6879b 1438 MACSTR, MAC2STR(bssid));
bbb921da 1439 random_add_randomness(bssid, ETH_ALEN);
8bac466b 1440 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
6fc6879b
JM
1441 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1442 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
8bac466b
JM
1443 if (bssid_changed)
1444 wpas_notify_bssid_changed(wpa_s);
1445
6fc6879b
JM
1446 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1447 wpa_clear_keys(wpa_s, bssid);
1448 }
1449 if (wpa_supplicant_select_config(wpa_s) < 0) {
1450 wpa_supplicant_disassociate(
1451 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1452 return;
1453 }
8f770587
JM
1454 if (wpa_s->current_ssid) {
1455 struct wpa_bss *bss = NULL;
cd2f4ddf
SM
1456
1457 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1458 if (!bss) {
1459 wpa_supplicant_update_scan_results(wpa_s);
1460
1461 /* Get the BSS from the new scan results */
1462 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1463 }
1464
8f770587
JM
1465 if (bss)
1466 wpa_s->current_bss = bss;
1467 }
cd2f4ddf
SM
1468
1469 if (wpa_s->conf->ap_scan == 1 &&
1470 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1471 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1472 wpa_msg(wpa_s, MSG_WARNING,
1473 "WPA/RSN IEs not updated");
1474 }
6fc6879b
JM
1475 }
1476
62fa124c
JM
1477#ifdef CONFIG_SME
1478 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1479 wpa_s->sme.prev_bssid_set = 1;
1480#endif /* CONFIG_SME */
1481
6fc6879b
JM
1482 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1483 if (wpa_s->current_ssid) {
1484 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1485 * initialized before association, but for other modes,
1486 * initialize PC/SC here, if the current configuration needs
1487 * smartcard or SIM/USIM. */
1488 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1489 }
1490 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
3f967fe0
JM
1491 if (wpa_s->l2)
1492 l2_packet_notify_auth_start(wpa_s->l2);
6fc6879b
JM
1493
1494 /*
1495 * Set portEnabled first to FALSE in order to get EAP state machine out
1496 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1497 * state machine may transit to AUTHENTICATING state based on obsolete
1498 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1499 * AUTHENTICATED without ever giving chance to EAP state machine to
1500 * reset the state.
1501 */
1502 if (!ft_completed) {
1503 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1504 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1505 }
56586197 1506 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
6fc6879b
JM
1507 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1508 /* 802.1X::portControl = Auto */
1509 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1510 wpa_s->eapol_received = 0;
1511 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
9c972abb
JM
1512 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1513 (wpa_s->current_ssid &&
1514 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
6fc6879b
JM
1515 wpa_supplicant_cancel_auth_timeout(wpa_s);
1516 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1517 } else if (!ft_completed) {
1518 /* Timeout for receiving the first EAPOL packet */
1519 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1520 }
1521 wpa_supplicant_cancel_scan(wpa_s);
1522
c2a04078 1523 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
56586197 1524 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
1525 /*
1526 * We are done; the driver will take care of RSN 4-way
1527 * handshake.
1528 */
1529 wpa_supplicant_cancel_auth_timeout(wpa_s);
1530 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1531 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1532 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
98ea9431
JM
1533 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1534 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1535 /*
1536 * The driver will take care of RSN 4-way handshake, so we need
1537 * to allow EAPOL supplicant to complete its work without
1538 * waiting for WPA supplicant.
1539 */
1540 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
16a83d29
JM
1541 } else if (ft_completed) {
1542 /*
1543 * FT protocol completed - make sure EAPOL state machine ends
1544 * up in authenticated.
1545 */
1546 wpa_supplicant_cancel_auth_timeout(wpa_s);
1547 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1548 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1549 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
6fc6879b 1550 }
1ff73338
JM
1551
1552 if (wpa_s->pending_eapol_rx) {
1553 struct os_time now, age;
1554 os_get_time(&now);
1555 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1556 if (age.sec == 0 && age.usec < 100000 &&
1557 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1558 0) {
f049052b
BG
1559 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1560 "frame that was received just before "
1561 "association notification");
1ff73338
JM
1562 wpa_supplicant_rx_eapol(
1563 wpa_s, wpa_s->pending_eapol_rx_src,
1564 wpabuf_head(wpa_s->pending_eapol_rx),
1565 wpabuf_len(wpa_s->pending_eapol_rx));
1566 }
1567 wpabuf_free(wpa_s->pending_eapol_rx);
1568 wpa_s->pending_eapol_rx = NULL;
1569 }
60b94c98 1570
0194fedb
JB
1571 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1572 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1573 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1574 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1575 /* Set static WEP keys again */
1576 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1577 }
50b05780
JM
1578
1579#ifdef CONFIG_IBSS_RSN
1580 if (wpa_s->current_ssid &&
1581 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1582 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
78177a00
JM
1583 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1584 wpa_s->ibss_rsn == NULL) {
1585 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1586 if (!wpa_s->ibss_rsn) {
1587 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1588 wpa_supplicant_deauthenticate(
1589 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1590 return;
1591 }
1592
1593 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1594 }
50b05780 1595#endif /* CONFIG_IBSS_RSN */
6fc6879b
JM
1596}
1597
1598
d00821e9
JM
1599static int disconnect_reason_recoverable(u16 reason_code)
1600{
1601 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1602 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1603 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1604}
1605
1606
0544b242 1607static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
3d9975d5
JM
1608 u16 reason_code,
1609 int locally_generated)
6fc6879b
JM
1610{
1611 const u8 *bssid;
6d6f4bb8
JM
1612 int authenticating;
1613 u8 prev_pending_bssid[ETH_ALEN];
d00821e9
JM
1614 struct wpa_bss *fast_reconnect = NULL;
1615 struct wpa_ssid *fast_reconnect_ssid = NULL;
6d6f4bb8
JM
1616
1617 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1618 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
6fc6879b
JM
1619
1620 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1621 /*
1622 * At least Host AP driver and a Prism3 card seemed to be
1623 * generating streams of disconnected events when configuring
1624 * IBSS for WPA-None. Ignore them for now.
1625 */
f049052b
BG
1626 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1627 "IBSS/WPA-None mode");
6fc6879b
JM
1628 return;
1629 }
1630
1631 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
56586197 1632 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
1633 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1634 "pre-shared key may be incorrect");
1635 }
0d0a8ca1
AC
1636 if (!wpa_s->auto_reconnect_disabled ||
1637 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
d00821e9 1638 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
f7da5a9e
JM
1639 "reconnect (wps=%d wpa_state=%d)",
1640 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1641 wpa_s->wpa_state);
d00821e9
JM
1642 if (wpa_s->wpa_state == WPA_COMPLETED &&
1643 wpa_s->current_ssid &&
1644 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
3d9975d5 1645 !locally_generated &&
d00821e9
JM
1646 disconnect_reason_recoverable(reason_code)) {
1647 /*
1648 * It looks like the AP has dropped association with
1649 * us, but could allow us to get back in. Try to
1650 * reconnect to the same BSS without full scan to save
1651 * time for some common cases.
1652 */
1653 fast_reconnect = wpa_s->current_bss;
1654 fast_reconnect_ssid = wpa_s->current_ssid;
1655 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
0d0a8ca1 1656 wpa_supplicant_req_scan(wpa_s, 0, 100000);
f7da5a9e
JM
1657 else
1658 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1659 "immediate scan");
0d0a8ca1 1660 } else {
d00821e9 1661 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
f049052b 1662 "try to re-connect");
0d0a8ca1
AC
1663 wpa_s->reassociate = 0;
1664 wpa_s->disconnected = 1;
433cd2ce 1665 wpa_supplicant_cancel_sched_scan(wpa_s);
0d0a8ca1 1666 }
6fc6879b 1667 bssid = wpa_s->bssid;
a8e16edc 1668 if (is_zero_ether_addr(bssid))
6fc6879b 1669 bssid = wpa_s->pending_bssid;
56d24b4e
JM
1670 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1671 wpas_connection_failed(wpa_s, bssid);
6fc6879b 1672 wpa_sm_notify_disassoc(wpa_s->wpa);
ef48ff94
JM
1673 if (!is_zero_ether_addr(bssid) ||
1674 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1675 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1676 " reason=%d%s",
1677 MAC2STR(bssid), reason_code,
1678 locally_generated ? " locally_generated=1" : "");
1679 }
6fc6879b 1680 if (wpa_supplicant_dynamic_keys(wpa_s)) {
f049052b 1681 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
6fc6879b
JM
1682 wpa_s->keys_cleared = 0;
1683 wpa_clear_keys(wpa_s, wpa_s->bssid);
1684 }
1685 wpa_supplicant_mark_disassoc(wpa_s);
e29853bb
BG
1686
1687 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1688 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
d00821e9
JM
1689
1690 if (fast_reconnect) {
5928411e 1691#ifndef CONFIG_NO_SCAN_PROCESSING
d00821e9
JM
1692 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1693 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1694 fast_reconnect_ssid) < 0) {
1695 /* Recover through full scan */
1696 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1697 }
5928411e 1698#endif /* CONFIG_NO_SCAN_PROCESSING */
d00821e9 1699 }
6fc6879b
JM
1700}
1701
1702
46690a3b 1703#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
01a17491 1704void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
46690a3b
JM
1705{
1706 struct wpa_supplicant *wpa_s = eloop_ctx;
1707
1708 if (!wpa_s->pending_mic_error_report)
1709 return;
1710
f049052b 1711 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
46690a3b
JM
1712 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1713 wpa_s->pending_mic_error_report = 0;
1714}
1715#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1716
1717
6fc6879b
JM
1718static void
1719wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1720 union wpa_event_data *data)
1721{
1722 int pairwise;
1723 struct os_time t;
1724
1725 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1726 pairwise = (data && data->michael_mic_failure.unicast);
6fc6879b 1727 os_get_time(&t);
46690a3b
JM
1728 if ((wpa_s->last_michael_mic_error &&
1729 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1730 wpa_s->pending_mic_error_report) {
1731 if (wpa_s->pending_mic_error_report) {
1732 /*
1733 * Send the pending MIC error report immediately since
1734 * we are going to start countermeasures and AP better
1735 * do the same.
1736 */
1737 wpa_sm_key_request(wpa_s->wpa, 1,
1738 wpa_s->pending_mic_error_pairwise);
1739 }
1740
1741 /* Send the new MIC error report immediately since we are going
1742 * to start countermeasures and AP better do the same.
1743 */
1744 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1745
6fc6879b
JM
1746 /* initialize countermeasures */
1747 wpa_s->countermeasures = 1;
8945cc45
BM
1748
1749 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1750
6fc6879b
JM
1751 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1752
1753 /*
1754 * Need to wait for completion of request frame. We do not get
1755 * any callback for the message completion, so just wait a
1756 * short while and hope for the best. */
1757 os_sleep(0, 10000);
1758
1759 wpa_drv_set_countermeasures(wpa_s, 1);
1760 wpa_supplicant_deauthenticate(wpa_s,
1761 WLAN_REASON_MICHAEL_MIC_FAILURE);
1762 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1763 wpa_s, NULL);
1764 eloop_register_timeout(60, 0,
1765 wpa_supplicant_stop_countermeasures,
1766 wpa_s, NULL);
1767 /* TODO: mark the AP rejected for 60 second. STA is
1768 * allowed to associate with another AP.. */
46690a3b
JM
1769 } else {
1770#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1771 if (wpa_s->mic_errors_seen) {
1772 /*
1773 * Reduce the effectiveness of Michael MIC error
1774 * reports as a means for attacking against TKIP if
1775 * more than one MIC failure is noticed with the same
1776 * PTK. We delay the transmission of the reports by a
1777 * random time between 0 and 60 seconds in order to
1778 * force the attacker wait 60 seconds before getting
1779 * the information on whether a frame resulted in a MIC
1780 * failure.
1781 */
1782 u8 rval[4];
1783 int sec;
1784
1785 if (os_get_random(rval, sizeof(rval)) < 0)
1786 sec = os_random() % 60;
1787 else
1788 sec = WPA_GET_BE32(rval) % 60;
f049052b
BG
1789 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1790 "report %d seconds", sec);
46690a3b
JM
1791 wpa_s->pending_mic_error_report = 1;
1792 wpa_s->pending_mic_error_pairwise = pairwise;
1793 eloop_cancel_timeout(
1794 wpa_supplicant_delayed_mic_error_report,
1795 wpa_s, NULL);
1796 eloop_register_timeout(
1797 sec, os_random() % 1000000,
1798 wpa_supplicant_delayed_mic_error_report,
1799 wpa_s, NULL);
1800 } else {
1801 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1802 }
1803#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1804 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1805#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b
JM
1806 }
1807 wpa_s->last_michael_mic_error = t.sec;
46690a3b 1808 wpa_s->mic_errors_seen++;
6fc6879b
JM
1809}
1810
1811
a83d9c96
SL
1812#ifdef CONFIG_TERMINATE_ONLASTIF
1813static int any_interfaces(struct wpa_supplicant *head)
1814{
1815 struct wpa_supplicant *wpa_s;
1816
1817 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1818 if (!wpa_s->interface_removed)
1819 return 1;
1820 return 0;
1821}
1822#endif /* CONFIG_TERMINATE_ONLASTIF */
1823
1824
6fc6879b
JM
1825static void
1826wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1827 union wpa_event_data *data)
1828{
1829 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1830 return;
1831
1832 switch (data->interface_status.ievent) {
1833 case EVENT_INTERFACE_ADDED:
1834 if (!wpa_s->interface_removed)
1835 break;
1836 wpa_s->interface_removed = 0;
f049052b 1837 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
6fc6879b 1838 if (wpa_supplicant_driver_init(wpa_s) < 0) {
f049052b
BG
1839 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1840 "driver after interface was added");
6fc6879b 1841 }
cb6710a4 1842 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b
JM
1843 break;
1844 case EVENT_INTERFACE_REMOVED:
f049052b 1845 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
6fc6879b
JM
1846 wpa_s->interface_removed = 1;
1847 wpa_supplicant_mark_disassoc(wpa_s);
cb6710a4 1848 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
6fc6879b
JM
1849 l2_packet_deinit(wpa_s->l2);
1850 wpa_s->l2 = NULL;
ea244d21 1851#ifdef CONFIG_IBSS_RSN
78177a00
JM
1852 ibss_rsn_deinit(wpa_s->ibss_rsn);
1853 wpa_s->ibss_rsn = NULL;
ea244d21 1854#endif /* CONFIG_IBSS_RSN */
a83d9c96
SL
1855#ifdef CONFIG_TERMINATE_ONLASTIF
1856 /* check if last interface */
1857 if (!any_interfaces(wpa_s->global->ifaces))
1858 eloop_terminate();
1859#endif /* CONFIG_TERMINATE_ONLASTIF */
6fc6879b
JM
1860 break;
1861 }
1862}
1863
1864
1865#ifdef CONFIG_PEERKEY
1866static void
1867wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1868 union wpa_event_data *data)
1869{
1870 if (data == NULL)
1871 return;
1872 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1873}
1874#endif /* CONFIG_PEERKEY */
1875
1876
281ff0aa
GP
1877#ifdef CONFIG_TDLS
1878static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1879 union wpa_event_data *data)
1880{
1881 if (data == NULL)
1882 return;
1883 switch (data->tdls.oper) {
1884 case TDLS_REQUEST_SETUP:
1885 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1886 break;
1887 case TDLS_REQUEST_TEARDOWN:
7de27409
AN
1888 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
1889 data->tdls.reason_code);
281ff0aa
GP
1890 break;
1891 }
1892}
1893#endif /* CONFIG_TDLS */
1894
1895
6fc6879b
JM
1896#ifdef CONFIG_IEEE80211R
1897static void
1898wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1899 union wpa_event_data *data)
1900{
1901 if (data == NULL)
1902 return;
1903
1904 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1905 data->ft_ies.ies_len,
1906 data->ft_ies.ft_action,
babfbf15
JM
1907 data->ft_ies.target_ap,
1908 data->ft_ies.ric_ies,
1909 data->ft_ies.ric_ies_len) < 0) {
6fc6879b
JM
1910 /* TODO: prevent MLME/driver from trying to associate? */
1911 }
1912}
1913#endif /* CONFIG_IEEE80211R */
1914
1915
11ef8d35
JM
1916#ifdef CONFIG_IBSS_RSN
1917static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1918 union wpa_event_data *data)
1919{
df4bc509 1920 struct wpa_ssid *ssid;
df418245
XC
1921 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1922 return;
11ef8d35
JM
1923 if (data == NULL)
1924 return;
df4bc509
JM
1925 ssid = wpa_s->current_ssid;
1926 if (ssid == NULL)
1927 return;
1928 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1929 return;
1930
11ef8d35
JM
1931 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1932}
1933#endif /* CONFIG_IBSS_RSN */
1934
1935
036f7c4a
JM
1936#ifdef CONFIG_IEEE80211R
1937static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1938 size_t len)
1939{
1940 const u8 *sta_addr, *target_ap_addr;
1941 u16 status;
1942
1943 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1944 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1945 return; /* only SME case supported for now */
1946 if (len < 1 + 2 * ETH_ALEN + 2)
1947 return;
1948 if (data[0] != 2)
1949 return; /* Only FT Action Response is supported for now */
1950 sta_addr = data + 1;
1951 target_ap_addr = data + 1 + ETH_ALEN;
1952 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
f049052b
BG
1953 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1954 MACSTR " TargetAP " MACSTR " status %u",
1955 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
036f7c4a
JM
1956
1957 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
f049052b
BG
1958 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1959 " in FT Action Response", MAC2STR(sta_addr));
036f7c4a
JM
1960 return;
1961 }
1962
1963 if (status) {
f049052b
BG
1964 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1965 "failure (status code %d)", status);
036f7c4a
JM
1966 /* TODO: report error to FT code(?) */
1967 return;
1968 }
1969
1970 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1971 len - (1 + 2 * ETH_ALEN + 2), 1,
1972 target_ap_addr, NULL, 0) < 0)
1973 return;
1974
fe191985
JM
1975#ifdef CONFIG_SME
1976 {
1977 struct wpa_bss *bss;
1978 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1979 if (bss)
1980 wpa_s->sme.freq = bss->freq;
62c72d72 1981 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
fe191985
JM
1982 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1983 WLAN_AUTH_FT);
1984 }
1985#endif /* CONFIG_SME */
036f7c4a
JM
1986}
1987#endif /* CONFIG_IEEE80211R */
1988
1989
7d878ca7
JM
1990static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1991 struct unprot_deauth *e)
1992{
1993#ifdef CONFIG_IEEE80211W
1994 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1995 "dropped: " MACSTR " -> " MACSTR
1996 " (reason code %u)",
1997 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1998 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1999#endif /* CONFIG_IEEE80211W */
2000}
2001
2002
2003static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2004 struct unprot_disassoc *e)
2005{
2006#ifdef CONFIG_IEEE80211W
2007 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2008 "dropped: " MACSTR " -> " MACSTR
2009 " (reason code %u)",
2010 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2011 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2012#endif /* CONFIG_IEEE80211W */
2013}
2014
2015
71269b37
JM
2016static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2017{
2018 u8 action, mode;
2019 const u8 *pos, *end;
2020
2021 if (rx->data == NULL || rx->len == 0)
2022 return;
2023
2024 pos = rx->data;
2025 end = pos + rx->len;
2026 action = *pos++;
2027
2028 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2029 action, MAC2STR(rx->sa));
2030 switch (action) {
2031 case WNM_BSS_TRANS_MGMT_REQ:
2032 if (pos + 5 > end)
2033 break;
2034 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2035 "Request: dialog_token=%u request_mode=0x%x "
2036 "disassoc_timer=%u validity_interval=%u",
2037 pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2038 mode = pos[1];
2039 pos += 5;
2040 if (mode & 0x08)
2041 pos += 12; /* BSS Termination Duration */
2042 if (mode & 0x10) {
2043 char url[256];
2044 if (pos + 1 > end || pos + 1 + pos[0] > end) {
2045 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2046 "Transition Management Request "
2047 "(URL)");
2048 break;
2049 }
2050 os_memcpy(url, pos + 1, pos[0]);
2051 url[pos[0]] = '\0';
2052 wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2053 "Imminent - session_info_url=%s", url);
2054 }
2055 break;
2056 }
2057}
2058
2059
9646a8ab 2060void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
6fc6879b
JM
2061 union wpa_event_data *data)
2062{
2063 struct wpa_supplicant *wpa_s = ctx;
0544b242 2064 u16 reason_code = 0;
3d9975d5 2065 int locally_generated = 0;
6fc6879b 2066
8401a6b0
JM
2067 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2068 event != EVENT_INTERFACE_ENABLED &&
9b6f44cb
JM
2069 event != EVENT_INTERFACE_STATUS &&
2070 event != EVENT_SCHED_SCAN_STOPPED) {
6c3771d7
BG
2071 wpa_dbg(wpa_s, MSG_DEBUG,
2072 "Ignore event %s (%d) while interface is disabled",
2073 event_to_string(event), event);
8401a6b0
JM
2074 return;
2075 }
2076
74781dfc
JM
2077#ifndef CONFIG_NO_STDOUT_DEBUG
2078{
2079 int level = MSG_DEBUG;
2080
eab6f5e0 2081 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
74781dfc
JM
2082 const struct ieee80211_hdr *hdr;
2083 u16 fc;
2084 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2085 fc = le_to_host16(hdr->frame_control);
2086 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2087 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2088 level = MSG_EXCESSIVE;
2089 }
2090
2091 wpa_dbg(wpa_s, level, "Event %s (%d) received",
6c3771d7 2092 event_to_string(event), event);
74781dfc
JM
2093}
2094#endif /* CONFIG_NO_STDOUT_DEBUG */
9b7124b2 2095
6fc6879b 2096 switch (event) {
c2a04078
JM
2097 case EVENT_AUTH:
2098 sme_event_auth(wpa_s, data);
2099 break;
6fc6879b
JM
2100 case EVENT_ASSOC:
2101 wpa_supplicant_event_assoc(wpa_s, data);
2102 break;
2103 case EVENT_DISASSOC:
f049052b 2104 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2e75a2b3 2105 if (data) {
3d9975d5
JM
2106 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2107 data->disassoc_info.reason_code,
2108 data->disassoc_info.locally_generated ?
2109 " (locally generated)" : "");
2e75a2b3 2110 if (data->disassoc_info.addr)
f049052b
BG
2111 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2112 MAC2STR(data->disassoc_info.addr));
2e75a2b3 2113 }
1d041bec 2114#ifdef CONFIG_AP
7f542069 2115 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1d041bec
JM
2116 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2117 data->disassoc_info.addr);
2118 break;
2119 }
4551dd90
JM
2120 if (wpa_s->ap_iface) {
2121 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2122 "AP mode");
2123 break;
2124 }
1d041bec 2125#endif /* CONFIG_AP */
9fdd0fad
JM
2126 if (data) {
2127 reason_code = data->disassoc_info.reason_code;
3d9975d5
JM
2128 locally_generated =
2129 data->disassoc_info.locally_generated;
9fdd0fad
JM
2130 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2131 data->disassoc_info.ie,
2132 data->disassoc_info.ie_len);
2133#ifdef CONFIG_P2P
2134 wpas_p2p_disassoc_notif(
2135 wpa_s, data->disassoc_info.addr, reason_code,
2136 data->disassoc_info.ie,
3fc14102
JM
2137 data->disassoc_info.ie_len,
2138 locally_generated);
9fdd0fad
JM
2139#endif /* CONFIG_P2P */
2140 }
0e379654
JM
2141 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2142 sme_event_disassoc(wpa_s, data);
a84ed99e
JM
2143 /* fall through */
2144 case EVENT_DEAUTH:
0544b242 2145 if (event == EVENT_DEAUTH) {
f049052b
BG
2146 wpa_dbg(wpa_s, MSG_DEBUG,
2147 "Deauthentication notification");
2e75a2b3 2148 if (data) {
0544b242 2149 reason_code = data->deauth_info.reason_code;
3d9975d5
JM
2150 locally_generated =
2151 data->deauth_info.locally_generated;
2152 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2153 data->deauth_info.reason_code,
2154 data->deauth_info.locally_generated ?
2155 " (locally generated)" : "");
2e75a2b3 2156 if (data->deauth_info.addr) {
f049052b
BG
2157 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2158 MACSTR,
2159 MAC2STR(data->deauth_info.
2160 addr));
2e75a2b3 2161 }
9fdd0fad
JM
2162 wpa_hexdump(MSG_DEBUG,
2163 "Deauthentication frame IE(s)",
2164 data->deauth_info.ie,
2165 data->deauth_info.ie_len);
2e75a2b3 2166 }
0544b242 2167 }
1d041bec 2168#ifdef CONFIG_AP
7f542069 2169 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1d041bec
JM
2170 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2171 data->deauth_info.addr);
2172 break;
2173 }
4551dd90
JM
2174 if (wpa_s->ap_iface) {
2175 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2176 "AP mode");
2177 break;
2178 }
1d041bec 2179#endif /* CONFIG_AP */
3d9975d5
JM
2180 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2181 locally_generated);
3fc14102
JM
2182#ifdef CONFIG_P2P
2183 if (event == EVENT_DEAUTH && data) {
2184 wpas_p2p_deauth_notif(wpa_s, data->deauth_info.addr,
2185 reason_code,
2186 data->deauth_info.ie,
2187 data->deauth_info.ie_len,
2188 locally_generated);
2189 }
2190#endif /* CONFIG_P2P */
6fc6879b
JM
2191 break;
2192 case EVENT_MICHAEL_MIC_FAILURE:
2193 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2194 break;
2195#ifndef CONFIG_NO_SCAN_PROCESSING
2196 case EVENT_SCAN_RESULTS:
8d923a4a 2197 wpa_supplicant_event_scan_results(wpa_s, data);
6fc6879b
JM
2198 break;
2199#endif /* CONFIG_NO_SCAN_PROCESSING */
2200 case EVENT_ASSOCINFO:
2201 wpa_supplicant_event_associnfo(wpa_s, data);
2202 break;
2203 case EVENT_INTERFACE_STATUS:
2204 wpa_supplicant_event_interface_status(wpa_s, data);
2205 break;
2206 case EVENT_PMKID_CANDIDATE:
2207 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2208 break;
2209#ifdef CONFIG_PEERKEY
2210 case EVENT_STKSTART:
2211 wpa_supplicant_event_stkstart(wpa_s, data);
2212 break;
2213#endif /* CONFIG_PEERKEY */
281ff0aa
GP
2214#ifdef CONFIG_TDLS
2215 case EVENT_TDLS:
2216 wpa_supplicant_event_tdls(wpa_s, data);
2217 break;
2218#endif /* CONFIG_TDLS */
6fc6879b
JM
2219#ifdef CONFIG_IEEE80211R
2220 case EVENT_FT_RESPONSE:
2221 wpa_supplicant_event_ft_response(wpa_s, data);
2222 break;
2223#endif /* CONFIG_IEEE80211R */
11ef8d35
JM
2224#ifdef CONFIG_IBSS_RSN
2225 case EVENT_IBSS_RSN_START:
2226 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2227 break;
2228#endif /* CONFIG_IBSS_RSN */
efa46078 2229 case EVENT_ASSOC_REJECT:
c05d6d18
JM
2230 if (data->assoc_reject.bssid)
2231 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2232 "bssid=" MACSTR " status_code=%u",
2233 MAC2STR(data->assoc_reject.bssid),
2234 data->assoc_reject.status_code);
2235 else
2236 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2237 "status_code=%u",
2238 data->assoc_reject.status_code);
ea78c315
JM
2239 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2240 sme_event_assoc_reject(wpa_s, data);
efa46078 2241 break;
da1fb17c 2242 case EVENT_AUTH_TIMED_OUT:
ea78c315
JM
2243 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2244 sme_event_auth_timed_out(wpa_s, data);
da1fb17c
JM
2245 break;
2246 case EVENT_ASSOC_TIMED_OUT:
ea78c315
JM
2247 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2248 sme_event_assoc_timed_out(wpa_s, data);
da1fb17c 2249 break;
f8b1f695 2250 case EVENT_TX_STATUS:
f049052b
BG
2251 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2252 " type=%d stype=%d",
2253 MAC2STR(data->tx_status.dst),
2254 data->tx_status.type, data->tx_status.stype);
24f6497c 2255#ifdef CONFIG_AP
9bae1be0 2256 if (wpa_s->ap_iface == NULL) {
24f6497c 2257#ifdef CONFIG_OFFCHANNEL
9bae1be0
JM
2258 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2259 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
24f6497c 2260 offchannel_send_action_tx_status(
9bae1be0
JM
2261 wpa_s, data->tx_status.dst,
2262 data->tx_status.data,
2263 data->tx_status.data_len,
93b7ddd0 2264 data->tx_status.ack ?
24f6497c
JM
2265 OFFCHANNEL_SEND_ACTION_SUCCESS :
2266 OFFCHANNEL_SEND_ACTION_NO_ACK);
2267#endif /* CONFIG_OFFCHANNEL */
9bae1be0
JM
2268 break;
2269 }
24f6497c
JM
2270#endif /* CONFIG_AP */
2271#ifdef CONFIG_OFFCHANNEL
f049052b
BG
2272 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2273 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
9bae1be0
JM
2274 /*
2275 * Catch TX status events for Action frames we sent via group
2276 * interface in GO mode.
2277 */
2278 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2279 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2280 os_memcmp(wpa_s->parent->pending_action_dst,
2281 data->tx_status.dst, ETH_ALEN) == 0) {
24f6497c 2282 offchannel_send_action_tx_status(
9bae1be0
JM
2283 wpa_s->parent, data->tx_status.dst,
2284 data->tx_status.data,
2285 data->tx_status.data_len,
1d39378a 2286 data->tx_status.ack ?
24f6497c
JM
2287 OFFCHANNEL_SEND_ACTION_SUCCESS :
2288 OFFCHANNEL_SEND_ACTION_NO_ACK);
f8b1f695 2289 break;
9bae1be0 2290 }
24f6497c
JM
2291#endif /* CONFIG_OFFCHANNEL */
2292#ifdef CONFIG_AP
f8b1f695
JM
2293 switch (data->tx_status.type) {
2294 case WLAN_FC_TYPE_MGMT:
2295 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2296 data->tx_status.data_len,
2297 data->tx_status.stype,
2298 data->tx_status.ack);
2299 break;
2300 case WLAN_FC_TYPE_DATA:
2301 ap_tx_status(wpa_s, data->tx_status.dst,
2302 data->tx_status.data,
2303 data->tx_status.data_len,
2304 data->tx_status.ack);
2305 break;
2306 }
24f6497c 2307#endif /* CONFIG_AP */
f8b1f695 2308 break;
24f6497c 2309#ifdef CONFIG_AP
dd840f79
JB
2310 case EVENT_EAPOL_TX_STATUS:
2311 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2312 data->eapol_tx_status.data,
2313 data->eapol_tx_status.data_len,
2314 data->eapol_tx_status.ack);
2315 break;
bcf24348
JB
2316 case EVENT_DRIVER_CLIENT_POLL_OK:
2317 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2318 break;
f8b1f695
JM
2319 case EVENT_RX_FROM_UNKNOWN:
2320 if (wpa_s->ap_iface == NULL)
2321 break;
9b90955e
JB
2322 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2323 data->rx_from_unknown.wds);
f8b1f695 2324 break;
2d43d37f
JB
2325 case EVENT_RX_MGMT: {
2326 u16 fc, stype;
2327 const struct ieee80211_mgmt *mgmt;
2328
2329 mgmt = (const struct ieee80211_mgmt *)
2330 data->rx_mgmt.frame;
2331 fc = le_to_host16(mgmt->frame_control);
2332 stype = WLAN_FC_GET_STYPE(fc);
2333
9bae1be0
JM
2334 if (wpa_s->ap_iface == NULL) {
2335#ifdef CONFIG_P2P
9bae1be0
JM
2336 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2337 data->rx_mgmt.frame_len > 24) {
2338 const u8 *src = mgmt->sa;
2339 const u8 *ie = mgmt->u.probe_req.variable;
2340 size_t ie_len = data->rx_mgmt.frame_len -
2341 (mgmt->u.probe_req.variable -
2342 data->rx_mgmt.frame);
baf513d6
JB
2343 wpas_p2p_probe_req_rx(
2344 wpa_s, src, mgmt->da,
2345 mgmt->bssid, ie, ie_len,
2346 data->rx_mgmt.ssi_signal);
9bae1be0
JM
2347 break;
2348 }
2349#endif /* CONFIG_P2P */
f049052b
BG
2350 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2351 "management frame in non-AP mode");
f8b1f695 2352 break;
9bae1be0 2353 }
2d43d37f
JB
2354
2355 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2356 data->rx_mgmt.frame_len > 24) {
2357 const u8 *ie = mgmt->u.probe_req.variable;
2358 size_t ie_len = data->rx_mgmt.frame_len -
2359 (mgmt->u.probe_req.variable -
2360 data->rx_mgmt.frame);
2361
2362 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2363 mgmt->bssid, ie, ie_len,
2364 data->rx_mgmt.ssi_signal);
2d43d37f
JB
2365 }
2366
2a8b7416 2367 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
f8b1f695 2368 break;
2d43d37f 2369 }
f8b1f695 2370#endif /* CONFIG_AP */
036f7c4a 2371 case EVENT_RX_ACTION:
f049052b
BG
2372 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2373 " Category=%u DataLen=%d freq=%d MHz",
2374 MAC2STR(data->rx_action.sa),
2375 data->rx_action.category, (int) data->rx_action.len,
2376 data->rx_action.freq);
036f7c4a
JM
2377#ifdef CONFIG_IEEE80211R
2378 if (data->rx_action.category == WLAN_ACTION_FT) {
2379 ft_rx_action(wpa_s, data->rx_action.data,
2380 data->rx_action.len);
2381 break;
2382 }
2383#endif /* CONFIG_IEEE80211R */
7d878ca7
JM
2384#ifdef CONFIG_IEEE80211W
2385#ifdef CONFIG_SME
2386 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2387 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2388 data->rx_action.data,
2389 data->rx_action.len);
2390 break;
2391 }
2392#endif /* CONFIG_SME */
2393#endif /* CONFIG_IEEE80211W */
04ea7b79
JM
2394#ifdef CONFIG_GAS
2395 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2396 gas_query_rx(wpa_s->gas, data->rx_action.da,
2397 data->rx_action.sa, data->rx_action.bssid,
2398 data->rx_action.data, data->rx_action.len,
2399 data->rx_action.freq) == 0)
2400 break;
2401#endif /* CONFIG_GAS */
71269b37
JM
2402 if (data->rx_action.category == WLAN_ACTION_WNM) {
2403 wnm_action_rx(wpa_s, &data->rx_action);
2404 break;
2405 }
35287637
AN
2406#ifdef CONFIG_TDLS
2407 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2408 data->rx_action.len >= 4 &&
2409 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2410 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2411 "Response from " MACSTR,
2412 MAC2STR(data->rx_action.sa));
2413 break;
2414 }
2415#endif /* CONFIG_TDLS */
9bae1be0
JM
2416#ifdef CONFIG_P2P
2417 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2418 data->rx_action.sa,
2419 data->rx_action.bssid,
2420 data->rx_action.category,
2421 data->rx_action.data,
2422 data->rx_action.len, data->rx_action.freq);
e67b55fb
JM
2423#endif /* CONFIG_P2P */
2424 break;
2425 case EVENT_RX_PROBE_REQ:
b211f3eb
JM
2426 if (data->rx_probe_req.sa == NULL ||
2427 data->rx_probe_req.ie == NULL)
2428 break;
e67b55fb
JM
2429#ifdef CONFIG_AP
2430 if (wpa_s->ap_iface) {
2431 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2432 data->rx_probe_req.sa,
04a85e44
JM
2433 data->rx_probe_req.da,
2434 data->rx_probe_req.bssid,
e67b55fb 2435 data->rx_probe_req.ie,
baf513d6
JB
2436 data->rx_probe_req.ie_len,
2437 data->rx_probe_req.ssi_signal);
e67b55fb
JM
2438 break;
2439 }
2440#endif /* CONFIG_AP */
2441#ifdef CONFIG_P2P
2442 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
04a85e44
JM
2443 data->rx_probe_req.da,
2444 data->rx_probe_req.bssid,
e67b55fb 2445 data->rx_probe_req.ie,
baf513d6
JB
2446 data->rx_probe_req.ie_len,
2447 data->rx_probe_req.ssi_signal);
9bae1be0 2448#endif /* CONFIG_P2P */
036f7c4a 2449 break;
9bae1be0 2450 case EVENT_REMAIN_ON_CHANNEL:
24f6497c
JM
2451#ifdef CONFIG_OFFCHANNEL
2452 offchannel_remain_on_channel_cb(
2453 wpa_s, data->remain_on_channel.freq,
2454 data->remain_on_channel.duration);
2455#endif /* CONFIG_OFFCHANNEL */
2456#ifdef CONFIG_P2P
9bae1be0
JM
2457 wpas_p2p_remain_on_channel_cb(
2458 wpa_s, data->remain_on_channel.freq,
2459 data->remain_on_channel.duration);
24f6497c 2460#endif /* CONFIG_P2P */
9bae1be0
JM
2461 break;
2462 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
24f6497c
JM
2463#ifdef CONFIG_OFFCHANNEL
2464 offchannel_cancel_remain_on_channel_cb(
2465 wpa_s, data->remain_on_channel.freq);
2466#endif /* CONFIG_OFFCHANNEL */
2467#ifdef CONFIG_P2P
9bae1be0
JM
2468 wpas_p2p_cancel_remain_on_channel_cb(
2469 wpa_s, data->remain_on_channel.freq);
24f6497c 2470#endif /* CONFIG_P2P */
9bae1be0 2471 break;
24f6497c 2472#ifdef CONFIG_P2P
c5db8e51
KRK
2473 case EVENT_P2P_DEV_FOUND: {
2474 struct p2p_peer_info peer_info;
2475
2476 os_memset(&peer_info, 0, sizeof(peer_info));
2477 if (data->p2p_dev_found.dev_addr)
2478 os_memcpy(peer_info.p2p_device_addr,
2479 data->p2p_dev_found.dev_addr, ETH_ALEN);
2480 if (data->p2p_dev_found.pri_dev_type)
2481 os_memcpy(peer_info.pri_dev_type,
2482 data->p2p_dev_found.pri_dev_type,
2483 sizeof(peer_info.pri_dev_type));
2484 if (data->p2p_dev_found.dev_name)
2485 os_strlcpy(peer_info.device_name,
2486 data->p2p_dev_found.dev_name,
2487 sizeof(peer_info.device_name));
2488 peer_info.config_methods = data->p2p_dev_found.config_methods;
2489 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2490 peer_info.group_capab = data->p2p_dev_found.group_capab;
2491
8fd7dc1b
JB
2492 /*
2493 * FIX: new_device=1 is not necessarily correct. We should
2494 * maintain a P2P peer database in wpa_supplicant and update
2495 * this information based on whether the peer is truly new.
2496 */
2497 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
3ac17eba 2498 break;
c5db8e51 2499 }
3ac17eba
JM
2500 case EVENT_P2P_GO_NEG_REQ_RX:
2501 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2502 data->p2p_go_neg_req_rx.dev_passwd_id);
2503 break;
2504 case EVENT_P2P_GO_NEG_COMPLETED:
2505 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2506 break;
2507 case EVENT_P2P_PROV_DISC_REQUEST:
2508 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2509 data->p2p_prov_disc_req.config_methods,
2510 data->p2p_prov_disc_req.dev_addr,
2511 data->p2p_prov_disc_req.pri_dev_type,
2512 data->p2p_prov_disc_req.dev_name,
2513 data->p2p_prov_disc_req.supp_config_methods,
2514 data->p2p_prov_disc_req.dev_capab,
c3f42784
JM
2515 data->p2p_prov_disc_req.group_capab,
2516 NULL, 0);
3ac17eba
JM
2517 break;
2518 case EVENT_P2P_PROV_DISC_RESPONSE:
2519 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2520 data->p2p_prov_disc_resp.config_methods);
2521 break;
2522 case EVENT_P2P_SD_REQUEST:
2523 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2524 data->p2p_sd_req.sa,
2525 data->p2p_sd_req.dialog_token,
2526 data->p2p_sd_req.update_indic,
2527 data->p2p_sd_req.tlvs,
2528 data->p2p_sd_req.tlvs_len);
2529 break;
2530 case EVENT_P2P_SD_RESPONSE:
2531 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2532 data->p2p_sd_resp.update_indic,
2533 data->p2p_sd_resp.tlvs,
2534 data->p2p_sd_resp.tlvs_len);
2535 break;
9bae1be0 2536#endif /* CONFIG_P2P */
a8e0505b
JM
2537 case EVENT_EAPOL_RX:
2538 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2539 data->eapol_rx.data,
2540 data->eapol_rx.data_len);
2541 break;
e2f74005
JM
2542 case EVENT_SIGNAL_CHANGE:
2543 bgscan_notify_signal_change(
60a972a6 2544 wpa_s, data->signal_change.above_threshold,
174fa789
PS
2545 data->signal_change.current_signal,
2546 data->signal_change.current_noise,
2547 data->signal_change.current_txrate);
e2f74005 2548 break;
8401a6b0 2549 case EVENT_INTERFACE_ENABLED:
f049052b 2550 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
8401a6b0 2551 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
bfba8deb 2552 wpa_supplicant_update_mac_addr(wpa_s);
199716ad 2553#ifdef CONFIG_AP
9919f7a2
JB
2554 if (!wpa_s->ap_iface) {
2555 wpa_supplicant_set_state(wpa_s,
2556 WPA_DISCONNECTED);
2557 wpa_supplicant_req_scan(wpa_s, 0, 0);
2558 } else
2559 wpa_supplicant_set_state(wpa_s,
2560 WPA_COMPLETED);
199716ad
BG
2561#else /* CONFIG_AP */
2562 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2563 wpa_supplicant_req_scan(wpa_s, 0, 0);
2564#endif /* CONFIG_AP */
8401a6b0
JM
2565 }
2566 break;
2567 case EVENT_INTERFACE_DISABLED:
f049052b 2568 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
8401a6b0
JM
2569 wpa_supplicant_mark_disassoc(wpa_s);
2570 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2571 break;
b5c9da8d 2572 case EVENT_CHANNEL_LIST_CHANGED:
35c5d610
JM
2573 if (wpa_s->drv_priv == NULL)
2574 break; /* Ignore event during drv initialization */
6979582c
JM
2575
2576 free_hw_features(wpa_s);
2577 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2578 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2579
b5c9da8d
JM
2580#ifdef CONFIG_P2P
2581 wpas_p2p_update_channel_list(wpa_s);
c973f386
JM
2582#endif /* CONFIG_P2P */
2583 break;
2584 case EVENT_INTERFACE_UNAVAILABLE:
2585#ifdef CONFIG_P2P
2586 wpas_p2p_interface_unavailable(wpa_s);
7cfc4ac3
AGS
2587#endif /* CONFIG_P2P */
2588 break;
2589 case EVENT_BEST_CHANNEL:
f049052b
BG
2590 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2591 "(%d %d %d)",
2592 data->best_chan.freq_24, data->best_chan.freq_5,
2593 data->best_chan.freq_overall);
7cfc4ac3
AGS
2594 wpa_s->best_24_freq = data->best_chan.freq_24;
2595 wpa_s->best_5_freq = data->best_chan.freq_5;
2596 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2597#ifdef CONFIG_P2P
2598 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2599 data->best_chan.freq_5,
2600 data->best_chan.freq_overall);
b5c9da8d
JM
2601#endif /* CONFIG_P2P */
2602 break;
7d878ca7
JM
2603 case EVENT_UNPROT_DEAUTH:
2604 wpa_supplicant_event_unprot_deauth(wpa_s,
2605 &data->unprot_deauth);
2606 break;
2607 case EVENT_UNPROT_DISASSOC:
2608 wpa_supplicant_event_unprot_disassoc(wpa_s,
2609 &data->unprot_disassoc);
2610 break;
0d7e5a3a
JB
2611 case EVENT_STATION_LOW_ACK:
2612#ifdef CONFIG_AP
2613 if (wpa_s->ap_iface && data)
2614 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2615 data->low_ack.addr);
2616#endif /* CONFIG_AP */
8f15f711
AN
2617#ifdef CONFIG_TDLS
2618 if (data)
2619 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2620#endif /* CONFIG_TDLS */
0d7e5a3a 2621 break;
ea244d21
XC
2622 case EVENT_IBSS_PEER_LOST:
2623#ifdef CONFIG_IBSS_RSN
2624 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2625#endif /* CONFIG_IBSS_RSN */
2626 break;
b14a210c
JB
2627 case EVENT_DRIVER_GTK_REKEY:
2628 if (os_memcmp(data->driver_gtk_rekey.bssid,
2629 wpa_s->bssid, ETH_ALEN))
2630 break;
2631 if (!wpa_s->wpa)
2632 break;
2633 wpa_sm_update_replay_ctr(wpa_s->wpa,
2634 data->driver_gtk_rekey.replay_ctr);
2635 break;
cbdf3507
LC
2636 case EVENT_SCHED_SCAN_STOPPED:
2637 wpa_s->sched_scanning = 0;
2638 wpa_supplicant_notify_scanning(wpa_s, 0);
2639
9b6f44cb
JM
2640 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2641 break;
2642
cbdf3507
LC
2643 /*
2644 * If we timed out, start a new sched scan to continue
2645 * searching for more SSIDs.
2646 */
2647 if (wpa_s->sched_scan_timed_out)
2648 wpa_supplicant_req_sched_scan(wpa_s);
2649 break;
783fcb7d
GG
2650 case EVENT_WPS_BUTTON_PUSHED:
2651#ifdef CONFIG_WPS
2652 wpas_wps_start_pbc(wpa_s, NULL, 0);
2653#endif /* CONFIG_WPS */
2654 break;
6fc6879b 2655 default:
f049052b 2656 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
6fc6879b
JM
2657 break;
2658 }
2659}