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