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