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