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