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