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