]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/events.c
Use driver event, EVENT_EAPOL_RX, for EAPOL frame indication
[thirdparty/hostap.git] / wpa_supplicant / events.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Driver event processing
8d923a4a 3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
6fc6879b
JM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "eapol_supp/eapol_supp_sm.h"
3acb5005 19#include "rsn_supp/wpa.h"
6fc6879b 20#include "eloop.h"
6fc6879b
JM
21#include "config.h"
22#include "l2_packet/l2_packet.h"
23#include "wpa_supplicant_i.h"
2d5b792d 24#include "driver_i.h"
6fc6879b 25#include "pcsc_funcs.h"
3acb5005
JM
26#include "rsn_supp/preauth.h"
27#include "rsn_supp/pmksa_cache.h"
90973fb2 28#include "common/wpa_ctrl.h"
6fc6879b 29#include "eap_peer/eap.h"
8bac466b 30#include "notify.h"
90973fb2 31#include "common/ieee802_11_defs.h"
6fc6879b
JM
32#include "blacklist.h"
33#include "wpas_glue.h"
351f09a2 34#include "wps_supplicant.h"
11ef8d35 35#include "ibss_rsn.h"
c2a04078 36#include "sme.h"
60b94c98 37#include "bgscan.h"
f8b1f695 38#include "ap.h"
6fa81a3b 39#include "bss.h"
245519e0 40#include "mlme.h"
6fc6879b
JM
41
42
43static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
44{
8bac466b 45 struct wpa_ssid *ssid, *old_ssid;
6fc6879b
JM
46
47 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
48 return 0;
49
50 wpa_printf(MSG_DEBUG, "Select network based on association "
51 "information");
52 ssid = wpa_supplicant_get_ssid(wpa_s);
53 if (ssid == NULL) {
54 wpa_printf(MSG_INFO, "No network configuration found for the "
55 "current AP");
56 return -1;
57 }
58
59 if (ssid->disabled) {
60 wpa_printf(MSG_DEBUG, "Selected network is disabled");
61 return -1;
62 }
63
64 wpa_printf(MSG_DEBUG, "Network configuration found for the current "
65 "AP");
66 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
67 WPA_KEY_MGMT_WPA_NONE |
56586197
JM
68 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
69 WPA_KEY_MGMT_PSK_SHA256 |
70 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
6fc6879b
JM
71 u8 wpa_ie[80];
72 size_t wpa_ie_len = sizeof(wpa_ie);
73 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
74 wpa_ie, &wpa_ie_len);
75 } else {
76 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
77 }
78
79 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
80 eapol_sm_invalidate_cached_session(wpa_s->eapol);
8bac466b 81 old_ssid = wpa_s->current_ssid;
6fc6879b
JM
82 wpa_s->current_ssid = ssid;
83 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
84 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
85 if (old_ssid != wpa_s->current_ssid)
86 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
87
88 return 0;
89}
90
91
92static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
93 void *sock_ctx)
94{
95 struct wpa_supplicant *wpa_s = eloop_ctx;
96
97 if (wpa_s->countermeasures) {
98 wpa_s->countermeasures = 0;
99 wpa_drv_set_countermeasures(wpa_s, 0);
100 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
101 wpa_supplicant_req_scan(wpa_s, 0, 0);
102 }
103}
104
105
106void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
107{
8bac466b
JM
108 int bssid_changed;
109
6fc6879b 110 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8bac466b 111 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
112 os_memset(wpa_s->bssid, 0, ETH_ALEN);
113 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
8bac466b
JM
114 if (bssid_changed)
115 wpas_notify_bssid_changed(wpa_s);
116
6fc6879b
JM
117 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
118 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
56586197 119 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
6fc6879b
JM
120 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
121 wpa_s->ap_ies_from_associnfo = 0;
122}
123
124
125static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
126{
127 struct wpa_ie_data ie;
128 int pmksa_set = -1;
129 size_t i;
130
131 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
132 ie.pmkid == NULL)
133 return;
134
135 for (i = 0; i < ie.num_pmkid; i++) {
136 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
137 ie.pmkid + i * PMKID_LEN,
138 NULL, NULL, 0);
139 if (pmksa_set == 0) {
140 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
141 break;
142 }
143 }
144
145 wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
146 "cache", pmksa_set == 0 ? "" : "not ");
147}
148
149
150static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
151 union wpa_event_data *data)
152{
153 if (data == NULL) {
154 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
155 return;
156 }
157 wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
158 " index=%d preauth=%d",
159 MAC2STR(data->pmkid_candidate.bssid),
160 data->pmkid_candidate.index,
161 data->pmkid_candidate.preauth);
162
163 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
164 data->pmkid_candidate.index,
165 data->pmkid_candidate.preauth);
166}
167
168
169static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
170{
171 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
172 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
173 return 0;
174
175#ifdef IEEE8021X_EAPOL
176 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
177 wpa_s->current_ssid &&
178 !(wpa_s->current_ssid->eapol_flags &
179 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
180 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
181 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
182 * plaintext or static WEP keys). */
183 return 0;
184 }
185#endif /* IEEE8021X_EAPOL */
186
187 return 1;
188}
189
190
191/**
192 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
193 * @wpa_s: pointer to wpa_supplicant data
194 * @ssid: Configuration data for the network
195 * Returns: 0 on success, -1 on failure
196 *
197 * This function is called when starting authentication with a network that is
198 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
199 */
200int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
201 struct wpa_ssid *ssid)
202{
203#ifdef IEEE8021X_EAPOL
204 int aka = 0, sim = 0, type;
205
206 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
207 return 0;
208
209 if (ssid->eap.eap_methods == NULL) {
210 sim = 1;
211 aka = 1;
212 } else {
213 struct eap_method_type *eap = ssid->eap.eap_methods;
214 while (eap->vendor != EAP_VENDOR_IETF ||
215 eap->method != EAP_TYPE_NONE) {
216 if (eap->vendor == EAP_VENDOR_IETF) {
217 if (eap->method == EAP_TYPE_SIM)
218 sim = 1;
219 else if (eap->method == EAP_TYPE_AKA)
220 aka = 1;
221 }
222 eap++;
223 }
224 }
225
226 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
227 sim = 0;
228 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
229 aka = 0;
230
231 if (!sim && !aka) {
232 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
233 "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
234 return 0;
235 }
236
237 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
238 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
239 if (sim && aka)
240 type = SCARD_TRY_BOTH;
241 else if (aka)
242 type = SCARD_USIM_ONLY;
243 else
244 type = SCARD_GSM_SIM_ONLY;
245
246 wpa_s->scard = scard_init(type);
247 if (wpa_s->scard == NULL) {
248 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
249 "(pcsc-lite)");
250 return -1;
251 }
252 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
253 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
254#endif /* IEEE8021X_EAPOL */
255
256 return 0;
257}
258
259
260#ifndef CONFIG_NO_SCAN_PROCESSING
261static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
262 struct wpa_ssid *ssid)
263{
264 int i, privacy = 0;
265
266 if (ssid->mixed_cell)
267 return 1;
268
0632542b
AT
269#ifdef CONFIG_WPS
270 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
271 return 1;
272#endif /* CONFIG_WPS */
273
6fc6879b
JM
274 for (i = 0; i < NUM_WEP_KEYS; i++) {
275 if (ssid->wep_key_len[i]) {
276 privacy = 1;
277 break;
278 }
279 }
280#ifdef IEEE8021X_EAPOL
281 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
282 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
283 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
284 privacy = 1;
285#endif /* IEEE8021X_EAPOL */
286
287 if (bss->caps & IEEE80211_CAP_PRIVACY)
288 return privacy;
289 return !privacy;
290}
291
292
a6099152
JM
293static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
294 struct wpa_ssid *ssid,
6fc6879b
JM
295 struct wpa_scan_res *bss)
296{
297 struct wpa_ie_data ie;
298 int proto_match = 0;
299 const u8 *rsn_ie, *wpa_ie;
351f09a2 300 int ret;
6fc6879b 301
a6099152 302 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
351f09a2
JM
303 if (ret >= 0)
304 return ret;
ad08c363 305
6fc6879b
JM
306 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
307 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
308 proto_match++;
309
310 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
311 wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
312 break;
313 }
314 if (!(ie.proto & ssid->proto)) {
315 wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
316 "mismatch");
317 break;
318 }
319
320 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
321 wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
322 "mismatch");
323 break;
324 }
325
326 if (!(ie.group_cipher & ssid->group_cipher)) {
327 wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
328 "mismatch");
329 break;
330 }
331
332 if (!(ie.key_mgmt & ssid->key_mgmt)) {
333 wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
334 "mismatch");
335 break;
336 }
337
338#ifdef CONFIG_IEEE80211W
0b60b0aa
JM
339 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
340 ssid->ieee80211w == IEEE80211W_REQUIRED) {
6fc6879b
JM
341 wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
342 "protection");
343 break;
344 }
345#endif /* CONFIG_IEEE80211W */
346
347 wpa_printf(MSG_DEBUG, " selected based on RSN IE");
348 return 1;
349 }
350
351 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
352 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
353 proto_match++;
354
355 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
356 wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
357 break;
358 }
359 if (!(ie.proto & ssid->proto)) {
360 wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
361 "mismatch");
362 break;
363 }
364
365 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
366 wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
367 "mismatch");
368 break;
369 }
370
371 if (!(ie.group_cipher & ssid->group_cipher)) {
372 wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
373 "mismatch");
374 break;
375 }
376
377 if (!(ie.key_mgmt & ssid->key_mgmt)) {
378 wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
379 "mismatch");
380 break;
381 }
382
383 wpa_printf(MSG_DEBUG, " selected based on WPA IE");
384 return 1;
385 }
386
387 if (proto_match == 0)
388 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
389
390 return 0;
391}
392
393
6fa81a3b 394static struct wpa_bss *
9cf32261 395wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
a1fd2ce5 396 struct wpa_scan_results *scan_res,
9cf32261
JM
397 struct wpa_ssid *group,
398 struct wpa_ssid **selected_ssid)
6fc6879b
JM
399{
400 struct wpa_ssid *ssid;
9cf32261 401 struct wpa_scan_res *bss;
6fc6879b
JM
402 size_t i;
403 struct wpa_blacklist *e;
404 const u8 *ie;
405
6fc6879b 406 wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
a1fd2ce5 407 for (i = 0; i < scan_res->num; i++) {
6fc6879b
JM
408 const u8 *ssid_;
409 u8 wpa_ie_len, rsn_ie_len, ssid_len;
a1fd2ce5 410 bss = scan_res->res[i];
6fc6879b
JM
411
412 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
413 ssid_ = ie ? ie + 2 : (u8 *) "";
414 ssid_len = ie ? ie[1] : 0;
415
416 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
417 wpa_ie_len = ie ? ie[1] : 0;
418
419 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
420 rsn_ie_len = ie ? ie[1] : 0;
421
422 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
423 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
424 (int) i, MAC2STR(bss->bssid),
425 wpa_ssid_txt(ssid_, ssid_len),
426 wpa_ie_len, rsn_ie_len, bss->caps);
9cf32261 427
6fc6879b
JM
428 e = wpa_blacklist_get(wpa_s, bss->bssid);
429 if (e && e->count > 1) {
430 wpa_printf(MSG_DEBUG, " skip - blacklisted");
431 continue;
432 }
433
e81634cd
JM
434 if (ssid_len == 0) {
435 wpa_printf(MSG_DEBUG, " skip - SSID not known");
436 continue;
437 }
438
6fc6879b
JM
439 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
440 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
441 continue;
442 }
443
444 for (ssid = group; ssid; ssid = ssid->pnext) {
ad08c363
JM
445 int check_ssid = 1;
446
6fc6879b
JM
447 if (ssid->disabled) {
448 wpa_printf(MSG_DEBUG, " skip - disabled");
449 continue;
450 }
9cf32261 451
ad08c363
JM
452#ifdef CONFIG_WPS
453 if (ssid->ssid_len == 0 &&
a6099152 454 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
ad08c363
JM
455 check_ssid = 0;
456#endif /* CONFIG_WPS */
457
458 if (check_ssid &&
459 (ssid_len != ssid->ssid_len ||
460 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
6fc6879b
JM
461 wpa_printf(MSG_DEBUG, " skip - "
462 "SSID mismatch");
463 continue;
464 }
9cf32261 465
6fc6879b
JM
466 if (ssid->bssid_set &&
467 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
468 {
469 wpa_printf(MSG_DEBUG, " skip - "
470 "BSSID mismatch");
471 continue;
472 }
9cf32261 473
a6099152 474 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
9cf32261
JM
475 continue;
476
477 wpa_printf(MSG_DEBUG, " selected WPA AP "
478 MACSTR " ssid='%s'",
479 MAC2STR(bss->bssid),
480 wpa_ssid_txt(ssid_, ssid_len));
481 *selected_ssid = ssid;
6fa81a3b 482 return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
6fc6879b
JM
483 }
484 }
485
9cf32261
JM
486 return NULL;
487}
488
489
6fa81a3b 490static struct wpa_bss *
9cf32261 491wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
a1fd2ce5 492 struct wpa_scan_results *scan_res,
9cf32261
JM
493 struct wpa_ssid *group,
494 struct wpa_ssid **selected_ssid)
495{
496 struct wpa_ssid *ssid;
497 struct wpa_scan_res *bss;
498 size_t i;
499 struct wpa_blacklist *e;
500 const u8 *ie;
501
6fc6879b 502 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
a1fd2ce5 503 for (i = 0; i < scan_res->num; i++) {
6fc6879b
JM
504 const u8 *ssid_;
505 u8 wpa_ie_len, rsn_ie_len, ssid_len;
a1fd2ce5 506 bss = scan_res->res[i];
6fc6879b
JM
507
508 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
509 ssid_ = ie ? ie + 2 : (u8 *) "";
510 ssid_len = ie ? ie[1] : 0;
511
512 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
513 wpa_ie_len = ie ? ie[1] : 0;
514
515 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
516 rsn_ie_len = ie ? ie[1] : 0;
517
518 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
519 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
520 (int) i, MAC2STR(bss->bssid),
521 wpa_ssid_txt(ssid_, ssid_len),
522 wpa_ie_len, rsn_ie_len, bss->caps);
9cf32261 523
6fc6879b
JM
524 e = wpa_blacklist_get(wpa_s, bss->bssid);
525 if (e && e->count > 1) {
526 wpa_printf(MSG_DEBUG, " skip - blacklisted");
527 continue;
528 }
9cf32261 529
e81634cd
JM
530 if (ssid_len == 0) {
531 wpa_printf(MSG_DEBUG, " skip - SSID not known");
532 continue;
533 }
534
6fc6879b 535 for (ssid = group; ssid; ssid = ssid->pnext) {
ad08c363
JM
536 int check_ssid = ssid->ssid_len != 0;
537
6fc6879b
JM
538 if (ssid->disabled) {
539 wpa_printf(MSG_DEBUG, " skip - disabled");
540 continue;
541 }
9cf32261 542
ad08c363
JM
543#ifdef CONFIG_WPS
544 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
545 /* Only allow wildcard SSID match if an AP
546 * advertises active WPS operation that matches
547 * with our mode. */
548 check_ssid = 1;
549 if (ssid->ssid_len == 0 &&
a6099152
JM
550 wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
551 bss))
ad08c363
JM
552 check_ssid = 0;
553 }
554#endif /* CONFIG_WPS */
555
556 if (check_ssid &&
6fc6879b
JM
557 (ssid_len != ssid->ssid_len ||
558 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
559 wpa_printf(MSG_DEBUG, " skip - "
560 "SSID mismatch");
561 continue;
562 }
563
564 if (ssid->bssid_set &&
565 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
566 {
567 wpa_printf(MSG_DEBUG, " skip - "
568 "BSSID mismatch");
569 continue;
570 }
1bd3f426 571
6fc6879b 572 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
ad08c363 573 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
6fc6879b
JM
574 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
575 {
576 wpa_printf(MSG_DEBUG, " skip - "
577 "non-WPA network not allowed");
578 continue;
579 }
580
1bd3f426 581 if ((ssid->key_mgmt &
56586197
JM
582 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
583 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
584 WPA_KEY_MGMT_IEEE8021X_SHA256 |
585 WPA_KEY_MGMT_PSK_SHA256)) &&
6fc6879b
JM
586 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
587 wpa_printf(MSG_DEBUG, " skip - "
588 "WPA network");
589 continue;
590 }
591
592 if (!wpa_supplicant_match_privacy(bss, ssid)) {
593 wpa_printf(MSG_DEBUG, " skip - "
594 "privacy mismatch");
595 continue;
596 }
597
598 if (bss->caps & IEEE80211_CAP_IBSS) {
599 wpa_printf(MSG_DEBUG, " skip - "
600 "IBSS (adhoc) network");
601 continue;
602 }
603
6fc6879b
JM
604 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
605 MACSTR " ssid='%s'",
606 MAC2STR(bss->bssid),
607 wpa_ssid_txt(ssid_, ssid_len));
9cf32261 608 *selected_ssid = ssid;
6fa81a3b 609 return wpa_bss_get(wpa_s, bss->bssid, ssid_, ssid_len);
6fc6879b
JM
610 }
611 }
612
9cf32261
JM
613 return NULL;
614}
615
616
6fa81a3b 617static struct wpa_bss *
a1fd2ce5
JM
618wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
619 struct wpa_scan_results *scan_res,
620 struct wpa_ssid *group,
9cf32261
JM
621 struct wpa_ssid **selected_ssid)
622{
6fa81a3b 623 struct wpa_bss *selected;
9cf32261
JM
624
625 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
626 group->priority);
627
628 /* First, try to find WPA-enabled AP */
a1fd2ce5
JM
629 selected = wpa_supplicant_select_bss_wpa(wpa_s, scan_res, group,
630 selected_ssid);
9cf32261
JM
631 if (selected)
632 return selected;
633
634 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
635 * allows this. */
a1fd2ce5
JM
636 return wpa_supplicant_select_bss_non_wpa(wpa_s, scan_res, group,
637 selected_ssid);
6fc6879b
JM
638}
639
640
6fa81a3b 641static struct wpa_bss *
09b9df4e 642wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
a1fd2ce5 643 struct wpa_scan_results *scan_res,
09b9df4e 644 struct wpa_ssid **selected_ssid)
6fc6879b 645{
6fa81a3b 646 struct wpa_bss *selected = NULL;
09b9df4e
JM
647 int prio;
648
649 while (selected == NULL) {
650 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
651 selected = wpa_supplicant_select_bss(
a1fd2ce5 652 wpa_s, scan_res, wpa_s->conf->pssid[prio],
09b9df4e
JM
653 selected_ssid);
654 if (selected)
655 break;
656 }
657
658 if (selected == NULL && wpa_s->blacklist) {
659 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
660 "and try again");
661 wpa_blacklist_clear(wpa_s);
662 wpa_s->blacklist_cleared++;
663 } else if (selected == NULL)
664 break;
665 }
666
667 return selected;
668}
669
670
671static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
672 int timeout)
673{
674 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
675 /*
676 * Quick recovery if the initial scan results were not
677 * complete when fetched before the first scan request.
678 */
679 wpa_s->scan_res_tried++;
680 timeout = 0;
4f34d51a
SL
681 } else if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
682 /*
683 * No networks are enabled; short-circuit request so
684 * we don't wait timeout seconds before transitioning
685 * to INACTIVE state.
686 */
687 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
688 return;
09b9df4e
JM
689 }
690 wpa_supplicant_req_scan(wpa_s, timeout, 0);
691}
692
693
694static void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
6fa81a3b 695 struct wpa_bss *selected,
09b9df4e
JM
696 struct wpa_ssid *ssid)
697{
698 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
699 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
700 "PBC session overlap");
701 wpa_supplicant_req_new_scan(wpa_s, 10);
702 return;
703 }
704
705 /*
706 * Do not trigger new association unless the BSSID has changed or if
707 * reassociation is requested. If we are in process of associating with
708 * the selected BSSID, do not trigger new attempt.
709 */
710 if (wpa_s->reassociate ||
711 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
712 (wpa_s->wpa_state != WPA_ASSOCIATING ||
713 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
714 0))) {
715 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
716 wpa_supplicant_req_new_scan(wpa_s, 10);
717 return;
718 }
719 wpa_supplicant_associate(wpa_s, selected, ssid);
720 } else {
721 wpa_printf(MSG_DEBUG, "Already associated with the selected "
722 "AP");
723 }
09b9df4e
JM
724}
725
726
b55aaa5f
JM
727static struct wpa_ssid *
728wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
729{
730 int prio;
731 struct wpa_ssid *ssid;
732
733 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
734 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
735 {
736 if (ssid->disabled)
737 continue;
738 if (ssid->mode == IEEE80211_MODE_IBSS ||
739 ssid->mode == IEEE80211_MODE_AP)
740 return ssid;
741 }
742 }
743 return NULL;
744}
745
746
a1fd2ce5
JM
747/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
748 * on BSS added and BSS changed events */
6ae93185 749static void wpa_supplicant_rsn_preauth_scan_results(
a1fd2ce5 750 struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
6ae93185
JM
751{
752 int i;
753
754 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
755 return;
756
a1fd2ce5 757 for (i = scan_res->num - 1; i >= 0; i--) {
6ae93185
JM
758 const u8 *ssid, *rsn;
759 struct wpa_scan_res *r;
760
a1fd2ce5 761 r = scan_res->res[i];
6ae93185
JM
762
763 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
764 if (ssid == NULL)
765 continue;
766
767 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
768 if (rsn == NULL)
769 continue;
770
771 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
772 }
773
774}
775
776
8d923a4a
JM
777static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
778 union wpa_event_data *data)
09b9df4e 779{
6fa81a3b 780 struct wpa_bss *selected;
6fc6879b 781 struct wpa_ssid *ssid = NULL;
a1fd2ce5 782 struct wpa_scan_results *scan_res;
6fc6879b 783
cb8564b1
DW
784 wpa_supplicant_notify_scanning(wpa_s, 0);
785
a1fd2ce5
JM
786 scan_res = wpa_supplicant_get_scan_results(wpa_s,
787 data ? &data->scan_info :
788 NULL, 1);
789 if (scan_res == NULL) {
6fc6879b
JM
790 if (wpa_s->conf->ap_scan == 2)
791 return;
792 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
793 "scanning again");
09b9df4e
JM
794 wpa_supplicant_req_new_scan(wpa_s, 1);
795 return;
6fc6879b
JM
796 }
797
23e07237
DN
798 /*
799 * Don't post the results if this was the initial cached
800 * and there were no results.
801 */
802 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
a1fd2ce5 803 scan_res->num == 0) {
23e07237
DN
804 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
805 "empty - not posting");
806 } else {
69856fad
JM
807 wpa_printf(MSG_DEBUG, "New scan results available");
808 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
8bac466b 809 wpas_notify_scan_results(wpa_s);
23e07237 810 }
6fc6879b 811
8bac466b
JM
812 wpas_notify_scan_done(wpa_s, 1);
813
a1fd2ce5
JM
814 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
815 wpa_scan_results_free(scan_res);
6fc6879b 816 return;
a1fd2ce5 817 }
6fc6879b 818
3180d7a2
SO
819 if (wpa_s->disconnected) {
820 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
a1fd2ce5 821 wpa_scan_results_free(scan_res);
3180d7a2
SO
822 return;
823 }
824
a1fd2ce5
JM
825 if (bgscan_notify_scan(wpa_s) == 1) {
826 wpa_scan_results_free(scan_res);
60b94c98 827 return;
a1fd2ce5
JM
828 }
829
830 wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
60b94c98 831
a1fd2ce5
JM
832 selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
833 wpa_scan_results_free(scan_res);
6ae93185 834
6fc6879b 835 if (selected) {
09b9df4e 836 wpa_supplicant_connect(wpa_s, selected, ssid);
6fc6879b 837 } else {
09b9df4e 838 wpa_printf(MSG_DEBUG, "No suitable network found");
b55aaa5f
JM
839 ssid = wpa_supplicant_pick_new_network(wpa_s);
840 if (ssid) {
841 wpa_printf(MSG_DEBUG, "Setup a new network");
842 wpa_supplicant_associate(wpa_s, NULL, ssid);
843 } else
844 wpa_supplicant_req_new_scan(wpa_s, 5);
6fc6879b 845 }
6fc6879b
JM
846}
847#endif /* CONFIG_NO_SCAN_PROCESSING */
848
849
c2a04078
JM
850#ifdef CONFIG_IEEE80211R
851static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
852 const u8 *ftie, const u8 *mdie)
853{
854 const u8 *mobility_domain = NULL;
855 const u8 *r0kh_id = NULL;
856 size_t r0kh_id_len = 0;
857 const u8 *r1kh_id = NULL;
858 struct rsn_ftie *hdr;
859 const u8 *pos, *end;
860
861 if (mdie == NULL || ftie == NULL)
862 return;
863
864 if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
865 mobility_domain = mdie + 2;
866#ifdef CONFIG_SME
867 wpa_s->sme.ft_used = 1;
868 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
869#endif /* CONFIG_SME */
870 }
871 if (ftie[1] >= sizeof(struct rsn_ftie)) {
872 end = ftie + 2 + ftie[1];
873 hdr = (struct rsn_ftie *) (ftie + 2);
874 pos = (const u8 *) (hdr + 1);
875 while (pos + 1 < end) {
876 if (pos + 2 + pos[1] > end)
877 break;
878 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
879 pos[1] == FT_R1KH_ID_LEN)
880 r1kh_id = pos + 2;
881 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
882 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
883 r0kh_id = pos + 2;
884 r0kh_id_len = pos[1];
885 }
886 pos += 2 + pos[1];
887 }
888 }
889 wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
890 r0kh_id_len, r1kh_id);
891}
892#endif /* CONFIG_IEEE80211R */
893
6fc6879b
JM
894static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
895 union wpa_event_data *data)
896{
897 int l, len, found = 0, wpa_found, rsn_found;
c2a04078
JM
898 const u8 *p;
899#ifdef CONFIG_IEEE80211R
900 const u8 *mdie = NULL, *ftie = NULL;
901#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
902
903 wpa_printf(MSG_DEBUG, "Association info event");
904 if (data->assoc_info.req_ies)
905 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
906 data->assoc_info.req_ies_len);
907 if (data->assoc_info.resp_ies)
908 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
909 data->assoc_info.resp_ies_len);
910 if (data->assoc_info.beacon_ies)
911 wpa_hexdump(MSG_DEBUG, "beacon_ies",
912 data->assoc_info.beacon_ies,
913 data->assoc_info.beacon_ies_len);
4832ecd7
JM
914 if (data->assoc_info.freq)
915 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
6fc6879b
JM
916
917 p = data->assoc_info.req_ies;
918 l = data->assoc_info.req_ies_len;
919
920 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
921 while (p && l >= 2) {
922 len = p[1] + 2;
923 if (len > l) {
924 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
925 p, l);
926 break;
927 }
928 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
929 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
930 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
931 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
932 break;
933 found = 1;
934 wpa_find_assoc_pmkid(wpa_s);
935 break;
936 }
937 l -= len;
938 p += len;
939 }
940 if (!found && data->assoc_info.req_ies)
941 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
942
c2a04078
JM
943#ifdef CONFIG_IEEE80211R
944 p = data->assoc_info.resp_ies;
945 l = data->assoc_info.resp_ies_len;
946
94873e3b 947 /* Go through the IEs and make a copy of the FT/MD IE, if present. */
c2a04078
JM
948 while (p && l >= 2) {
949 len = p[1] + 2;
950 if (len > l) {
951 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
952 p, l);
953 break;
954 }
955 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
956 ftie = p;
957 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
958 mdie = p;
959 l -= len;
960 p += len;
961 }
962
963 wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
964#endif /* CONFIG_IEEE80211R */
965
6fc6879b
JM
966 /* WPA/RSN IE from Beacon/ProbeResp */
967 p = data->assoc_info.beacon_ies;
968 l = data->assoc_info.beacon_ies_len;
969
970 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
971 */
972 wpa_found = rsn_found = 0;
973 while (p && l >= 2) {
974 len = p[1] + 2;
975 if (len > l) {
976 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
977 p, l);
978 break;
979 }
980 if (!wpa_found &&
981 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
982 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
983 wpa_found = 1;
984 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
985 }
986
987 if (!rsn_found &&
988 p[0] == WLAN_EID_RSN && p[1] >= 2) {
989 rsn_found = 1;
990 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
991 }
992
993 l -= len;
994 p += len;
995 }
996
997 if (!wpa_found && data->assoc_info.beacon_ies)
998 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
999 if (!rsn_found && data->assoc_info.beacon_ies)
1000 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1001 if (wpa_found || rsn_found)
1002 wpa_s->ap_ies_from_associnfo = 1;
4832ecd7
JM
1003
1004 wpa_s->assoc_freq = data->assoc_info.freq;
6fc6879b
JM
1005}
1006
1007
1008static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1009 union wpa_event_data *data)
1010{
1011 u8 bssid[ETH_ALEN];
1012 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
8bac466b 1013 int bssid_changed;
0194fedb 1014 struct wpa_driver_capa capa;
6fc6879b
JM
1015
1016 if (data)
1017 wpa_supplicant_event_associnfo(wpa_s, data);
1018
1019 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
c2a04078 1020 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
6fc6879b 1021 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
c2a04078 1022 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
6fc6879b
JM
1023 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1024 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1025 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1026 MACSTR, MAC2STR(bssid));
8bac466b 1027 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
6fc6879b
JM
1028 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1029 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
8bac466b
JM
1030 if (bssid_changed)
1031 wpas_notify_bssid_changed(wpa_s);
1032
6fc6879b
JM
1033 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1034 wpa_clear_keys(wpa_s, bssid);
1035 }
1036 if (wpa_supplicant_select_config(wpa_s) < 0) {
1037 wpa_supplicant_disassociate(
1038 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1039 return;
1040 }
8f770587
JM
1041 if (wpa_s->current_ssid) {
1042 struct wpa_bss *bss = NULL;
1043 struct wpa_ssid *ssid = wpa_s->current_ssid;
1044 if (ssid->ssid_len > 0)
1045 bss = wpa_bss_get(wpa_s, bssid,
1046 ssid->ssid, ssid->ssid_len);
1047 if (!bss)
1048 bss = wpa_bss_get_bssid(wpa_s, bssid);
1049 if (bss)
1050 wpa_s->current_bss = bss;
1051 }
6fc6879b
JM
1052 }
1053
62fa124c
JM
1054#ifdef CONFIG_SME
1055 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1056 wpa_s->sme.prev_bssid_set = 1;
1057#endif /* CONFIG_SME */
1058
6fc6879b
JM
1059 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1060 if (wpa_s->current_ssid) {
1061 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1062 * initialized before association, but for other modes,
1063 * initialize PC/SC here, if the current configuration needs
1064 * smartcard or SIM/USIM. */
1065 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1066 }
1067 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1068 l2_packet_notify_auth_start(wpa_s->l2);
1069
1070 /*
1071 * Set portEnabled first to FALSE in order to get EAP state machine out
1072 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1073 * state machine may transit to AUTHENTICATING state based on obsolete
1074 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1075 * AUTHENTICATED without ever giving chance to EAP state machine to
1076 * reset the state.
1077 */
1078 if (!ft_completed) {
1079 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1080 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1081 }
56586197 1082 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
6fc6879b
JM
1083 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1084 /* 802.1X::portControl = Auto */
1085 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1086 wpa_s->eapol_received = 0;
1087 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
9c972abb
JM
1088 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1089 (wpa_s->current_ssid &&
1090 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
6fc6879b
JM
1091 wpa_supplicant_cancel_auth_timeout(wpa_s);
1092 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1093 } else if (!ft_completed) {
1094 /* Timeout for receiving the first EAPOL packet */
1095 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1096 }
1097 wpa_supplicant_cancel_scan(wpa_s);
1098
c2a04078 1099 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
56586197 1100 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
1101 /*
1102 * We are done; the driver will take care of RSN 4-way
1103 * handshake.
1104 */
1105 wpa_supplicant_cancel_auth_timeout(wpa_s);
1106 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1107 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1108 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1109 }
1ff73338
JM
1110
1111 if (wpa_s->pending_eapol_rx) {
1112 struct os_time now, age;
1113 os_get_time(&now);
1114 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1115 if (age.sec == 0 && age.usec < 100000 &&
1116 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1117 0) {
1118 wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1119 "that was received just before association "
1120 "notification");
1121 wpa_supplicant_rx_eapol(
1122 wpa_s, wpa_s->pending_eapol_rx_src,
1123 wpabuf_head(wpa_s->pending_eapol_rx),
1124 wpabuf_len(wpa_s->pending_eapol_rx));
1125 }
1126 wpabuf_free(wpa_s->pending_eapol_rx);
1127 wpa_s->pending_eapol_rx = NULL;
1128 }
60b94c98
JM
1129
1130#ifdef CONFIG_BGSCAN
1131 if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1132 bgscan_deinit(wpa_s);
1133 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1134 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1135 wpa_printf(MSG_DEBUG, "Failed to initialize "
1136 "bgscan");
1137 /*
1138 * Live without bgscan; it is only used as a
1139 * roaming optimization, so the initial
1140 * connection is not affected.
1141 */
1142 } else
1143 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1144 } else
1145 wpa_s->bgscan_ssid = NULL;
1146 }
1147#endif /* CONFIG_BGSCAN */
0194fedb
JB
1148
1149 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1150 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1151 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1152 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1153 /* Set static WEP keys again */
1154 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1155 }
6fc6879b
JM
1156}
1157
1158
1159static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
1160{
1161 const u8 *bssid;
6d6f4bb8
JM
1162#ifdef CONFIG_SME
1163 int authenticating;
1164 u8 prev_pending_bssid[ETH_ALEN];
1165
1166 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1167 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1168#endif /* CONFIG_SME */
6fc6879b
JM
1169
1170 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1171 /*
1172 * At least Host AP driver and a Prism3 card seemed to be
1173 * generating streams of disconnected events when configuring
1174 * IBSS for WPA-None. Ignore them for now.
1175 */
1176 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1177 "IBSS/WPA-None mode");
1178 return;
1179 }
1180
1181 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
56586197 1182 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
1183 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1184 "pre-shared key may be incorrect");
1185 }
1186 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1187 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1188 bssid = wpa_s->bssid;
a8e16edc 1189 if (is_zero_ether_addr(bssid))
6fc6879b
JM
1190 bssid = wpa_s->pending_bssid;
1191 wpa_blacklist_add(wpa_s, bssid);
1192 wpa_sm_notify_disassoc(wpa_s->wpa);
1193 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
1194 "remove keys");
1195 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1196 wpa_s->keys_cleared = 0;
1197 wpa_clear_keys(wpa_s, wpa_s->bssid);
1198 }
1199 wpa_supplicant_mark_disassoc(wpa_s);
60b94c98 1200 bgscan_deinit(wpa_s);
6d6f4bb8
JM
1201#ifdef CONFIG_SME
1202 if (authenticating &&
1203 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1204 /*
1205 * mac80211-workaround to force deauth on failed auth cmd,
1206 * requires us to remain in authenticating state to allow the
1207 * second authentication attempt to be continued properly.
1208 */
1209 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1210 "proceed after disconnection event");
1211 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1212 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1213 }
1214#endif /* CONFIG_SME */
6fc6879b
JM
1215}
1216
1217
46690a3b
JM
1218#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1219static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1220 void *sock_ctx)
1221{
1222 struct wpa_supplicant *wpa_s = eloop_ctx;
1223
1224 if (!wpa_s->pending_mic_error_report)
1225 return;
1226
1227 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1228 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1229 wpa_s->pending_mic_error_report = 0;
1230}
1231#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1232
1233
6fc6879b
JM
1234static void
1235wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1236 union wpa_event_data *data)
1237{
1238 int pairwise;
1239 struct os_time t;
1240
1241 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1242 pairwise = (data && data->michael_mic_failure.unicast);
6fc6879b 1243 os_get_time(&t);
46690a3b
JM
1244 if ((wpa_s->last_michael_mic_error &&
1245 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1246 wpa_s->pending_mic_error_report) {
1247 if (wpa_s->pending_mic_error_report) {
1248 /*
1249 * Send the pending MIC error report immediately since
1250 * we are going to start countermeasures and AP better
1251 * do the same.
1252 */
1253 wpa_sm_key_request(wpa_s->wpa, 1,
1254 wpa_s->pending_mic_error_pairwise);
1255 }
1256
1257 /* Send the new MIC error report immediately since we are going
1258 * to start countermeasures and AP better do the same.
1259 */
1260 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1261
6fc6879b
JM
1262 /* initialize countermeasures */
1263 wpa_s->countermeasures = 1;
1264 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1265
1266 /*
1267 * Need to wait for completion of request frame. We do not get
1268 * any callback for the message completion, so just wait a
1269 * short while and hope for the best. */
1270 os_sleep(0, 10000);
1271
1272 wpa_drv_set_countermeasures(wpa_s, 1);
1273 wpa_supplicant_deauthenticate(wpa_s,
1274 WLAN_REASON_MICHAEL_MIC_FAILURE);
1275 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1276 wpa_s, NULL);
1277 eloop_register_timeout(60, 0,
1278 wpa_supplicant_stop_countermeasures,
1279 wpa_s, NULL);
1280 /* TODO: mark the AP rejected for 60 second. STA is
1281 * allowed to associate with another AP.. */
46690a3b
JM
1282 } else {
1283#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1284 if (wpa_s->mic_errors_seen) {
1285 /*
1286 * Reduce the effectiveness of Michael MIC error
1287 * reports as a means for attacking against TKIP if
1288 * more than one MIC failure is noticed with the same
1289 * PTK. We delay the transmission of the reports by a
1290 * random time between 0 and 60 seconds in order to
1291 * force the attacker wait 60 seconds before getting
1292 * the information on whether a frame resulted in a MIC
1293 * failure.
1294 */
1295 u8 rval[4];
1296 int sec;
1297
1298 if (os_get_random(rval, sizeof(rval)) < 0)
1299 sec = os_random() % 60;
1300 else
1301 sec = WPA_GET_BE32(rval) % 60;
1302 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1303 "seconds", sec);
1304 wpa_s->pending_mic_error_report = 1;
1305 wpa_s->pending_mic_error_pairwise = pairwise;
1306 eloop_cancel_timeout(
1307 wpa_supplicant_delayed_mic_error_report,
1308 wpa_s, NULL);
1309 eloop_register_timeout(
1310 sec, os_random() % 1000000,
1311 wpa_supplicant_delayed_mic_error_report,
1312 wpa_s, NULL);
1313 } else {
1314 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1315 }
1316#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1317 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1318#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b
JM
1319 }
1320 wpa_s->last_michael_mic_error = t.sec;
46690a3b 1321 wpa_s->mic_errors_seen++;
6fc6879b
JM
1322}
1323
1324
a83d9c96
SL
1325#ifdef CONFIG_TERMINATE_ONLASTIF
1326static int any_interfaces(struct wpa_supplicant *head)
1327{
1328 struct wpa_supplicant *wpa_s;
1329
1330 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1331 if (!wpa_s->interface_removed)
1332 return 1;
1333 return 0;
1334}
1335#endif /* CONFIG_TERMINATE_ONLASTIF */
1336
1337
6fc6879b
JM
1338static void
1339wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1340 union wpa_event_data *data)
1341{
1342 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1343 return;
1344
1345 switch (data->interface_status.ievent) {
1346 case EVENT_INTERFACE_ADDED:
1347 if (!wpa_s->interface_removed)
1348 break;
1349 wpa_s->interface_removed = 0;
1350 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1351 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1352 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1353 "after interface was added.");
1354 }
1355 break;
1356 case EVENT_INTERFACE_REMOVED:
1357 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1358 wpa_s->interface_removed = 1;
1359 wpa_supplicant_mark_disassoc(wpa_s);
1360 l2_packet_deinit(wpa_s->l2);
1361 wpa_s->l2 = NULL;
a83d9c96
SL
1362#ifdef CONFIG_TERMINATE_ONLASTIF
1363 /* check if last interface */
1364 if (!any_interfaces(wpa_s->global->ifaces))
1365 eloop_terminate();
1366#endif /* CONFIG_TERMINATE_ONLASTIF */
6fc6879b
JM
1367 break;
1368 }
1369}
1370
1371
1372#ifdef CONFIG_PEERKEY
1373static void
1374wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1375 union wpa_event_data *data)
1376{
1377 if (data == NULL)
1378 return;
1379 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1380}
1381#endif /* CONFIG_PEERKEY */
1382
1383
1384#ifdef CONFIG_IEEE80211R
1385static void
1386wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1387 union wpa_event_data *data)
1388{
1389 if (data == NULL)
1390 return;
1391
1392 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1393 data->ft_ies.ies_len,
1394 data->ft_ies.ft_action,
babfbf15
JM
1395 data->ft_ies.target_ap,
1396 data->ft_ies.ric_ies,
1397 data->ft_ies.ric_ies_len) < 0) {
6fc6879b
JM
1398 /* TODO: prevent MLME/driver from trying to associate? */
1399 }
1400}
1401#endif /* CONFIG_IEEE80211R */
1402
1403
11ef8d35
JM
1404#ifdef CONFIG_IBSS_RSN
1405static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1406 union wpa_event_data *data)
1407{
1408 if (data == NULL)
1409 return;
1410 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1411}
1412#endif /* CONFIG_IBSS_RSN */
1413
1414
9646a8ab 1415void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
6fc6879b
JM
1416 union wpa_event_data *data)
1417{
1418 struct wpa_supplicant *wpa_s = ctx;
1419
1420 switch (event) {
c2a04078
JM
1421 case EVENT_AUTH:
1422 sme_event_auth(wpa_s, data);
1423 break;
6fc6879b
JM
1424 case EVENT_ASSOC:
1425 wpa_supplicant_event_assoc(wpa_s, data);
1426 break;
1427 case EVENT_DISASSOC:
0e379654
JM
1428 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1429 sme_event_disassoc(wpa_s, data);
a84ed99e
JM
1430 /* fall through */
1431 case EVENT_DEAUTH:
6fc6879b
JM
1432 wpa_supplicant_event_disassoc(wpa_s);
1433 break;
1434 case EVENT_MICHAEL_MIC_FAILURE:
1435 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1436 break;
1437#ifndef CONFIG_NO_SCAN_PROCESSING
1438 case EVENT_SCAN_RESULTS:
8d923a4a 1439 wpa_supplicant_event_scan_results(wpa_s, data);
6fc6879b
JM
1440 break;
1441#endif /* CONFIG_NO_SCAN_PROCESSING */
1442 case EVENT_ASSOCINFO:
1443 wpa_supplicant_event_associnfo(wpa_s, data);
1444 break;
1445 case EVENT_INTERFACE_STATUS:
1446 wpa_supplicant_event_interface_status(wpa_s, data);
1447 break;
1448 case EVENT_PMKID_CANDIDATE:
1449 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1450 break;
1451#ifdef CONFIG_PEERKEY
1452 case EVENT_STKSTART:
1453 wpa_supplicant_event_stkstart(wpa_s, data);
1454 break;
1455#endif /* CONFIG_PEERKEY */
1456#ifdef CONFIG_IEEE80211R
1457 case EVENT_FT_RESPONSE:
1458 wpa_supplicant_event_ft_response(wpa_s, data);
1459 break;
1460#endif /* CONFIG_IEEE80211R */
11ef8d35
JM
1461#ifdef CONFIG_IBSS_RSN
1462 case EVENT_IBSS_RSN_START:
1463 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1464 break;
1465#endif /* CONFIG_IBSS_RSN */
efa46078
JM
1466 case EVENT_ASSOC_REJECT:
1467 sme_event_assoc_reject(wpa_s, data);
1468 break;
da1fb17c
JM
1469 case EVENT_AUTH_TIMED_OUT:
1470 sme_event_auth_timed_out(wpa_s, data);
1471 break;
1472 case EVENT_ASSOC_TIMED_OUT:
1473 sme_event_assoc_timed_out(wpa_s, data);
1474 break;
f8b1f695
JM
1475#ifdef CONFIG_AP
1476 case EVENT_TX_STATUS:
1477 if (wpa_s->ap_iface == NULL)
1478 break;
1479 switch (data->tx_status.type) {
1480 case WLAN_FC_TYPE_MGMT:
1481 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1482 data->tx_status.data_len,
1483 data->tx_status.stype,
1484 data->tx_status.ack);
1485 break;
1486 case WLAN_FC_TYPE_DATA:
1487 ap_tx_status(wpa_s, data->tx_status.dst,
1488 data->tx_status.data,
1489 data->tx_status.data_len,
1490 data->tx_status.ack);
1491 break;
1492 }
1493 break;
1494 case EVENT_RX_FROM_UNKNOWN:
1495 if (wpa_s->ap_iface == NULL)
1496 break;
fdbe50ed 1497 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
f8b1f695
JM
1498 data->rx_from_unknown.len);
1499 break;
1500 case EVENT_RX_MGMT:
1501 if (wpa_s->ap_iface == NULL)
1502 break;
2a8b7416 1503 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
f8b1f695
JM
1504 break;
1505#endif /* CONFIG_AP */
245519e0
JM
1506#ifdef CONFIG_CLIENT_MLME
1507 case EVENT_MLME_RX: {
1508 struct ieee80211_rx_status rx_status;
1509 os_memset(&rx_status, 0, sizeof(rx_status));
1510 rx_status.freq = data->mlme_rx.freq;
1511 rx_status.channel = data->mlme_rx.channel;
1512 rx_status.ssi = data->mlme_rx.ssi;
1513 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1514 &rx_status);
1515 break;
1516 }
1517#endif /* CONFIG_CLIENT_MLME */
a8e0505b
JM
1518 case EVENT_EAPOL_RX:
1519 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1520 data->eapol_rx.data,
1521 data->eapol_rx.data_len);
1522 break;
6fc6879b
JM
1523 default:
1524 wpa_printf(MSG_INFO, "Unknown event %d", event);
1525 break;
1526 }
1527}