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