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