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