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