]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/events.c
WPS ER: Only send Enrollee notification on Probe Request and M1
[thirdparty/hostap.git] / wpa_supplicant / events.c
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"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
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"
30 #include "notify.h"
31 #include "ieee802_11_defs.h"
32 #include "blacklist.h"
33 #include "wpas_glue.h"
34 #include "wps_supplicant.h"
35 #include "ibss_rsn.h"
36 #include "sme.h"
37 #include "bgscan.h"
38
39
40 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
41 {
42 struct wpa_ssid *ssid, *old_ssid;
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 |
65 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
66 WPA_KEY_MGMT_PSK_SHA256 |
67 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
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);
78 old_ssid = wpa_s->current_ssid;
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);
82 if (old_ssid != wpa_s->current_ssid)
83 wpas_notify_network_changed(wpa_s);
84
85 return 0;
86 }
87
88
89 static 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
103 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
104 {
105 int bssid_changed;
106
107 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
108 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
109 os_memset(wpa_s->bssid, 0, ETH_ALEN);
110 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
111 if (bssid_changed)
112 wpas_notify_bssid_changed(wpa_s);
113
114 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
115 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
116 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
117 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
118 wpa_s->ap_ies_from_associnfo = 0;
119 }
120
121
122 static 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
147 static 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
166 static 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 */
197 int 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
258 static 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
266 #ifdef CONFIG_WPS
267 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
268 return 1;
269 #endif /* CONFIG_WPS */
270
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
290 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
291 struct wpa_ssid *ssid,
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;
297 int ret;
298
299 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
300 if (ret >= 0)
301 return ret;
302
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
336 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
337 ssid->ieee80211w == IEEE80211W_REQUIRED) {
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
391 static struct wpa_scan_res *
392 wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
393 struct wpa_ssid *group,
394 struct wpa_ssid **selected_ssid)
395 {
396 struct wpa_ssid *ssid;
397 struct wpa_scan_res *bss;
398 size_t i;
399 struct wpa_blacklist *e;
400 const u8 *ie;
401
402 wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
403 for (i = 0; i < wpa_s->scan_res->num; i++) {
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);
423
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 (ssid_len == 0) {
431 wpa_printf(MSG_DEBUG, " skip - SSID not known");
432 continue;
433 }
434
435 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
436 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
437 continue;
438 }
439
440 for (ssid = group; ssid; ssid = ssid->pnext) {
441 int check_ssid = 1;
442
443 if (ssid->disabled) {
444 wpa_printf(MSG_DEBUG, " skip - disabled");
445 continue;
446 }
447
448 #ifdef CONFIG_WPS
449 if (ssid->ssid_len == 0 &&
450 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
451 check_ssid = 0;
452 #endif /* CONFIG_WPS */
453
454 if (check_ssid &&
455 (ssid_len != ssid->ssid_len ||
456 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
457 wpa_printf(MSG_DEBUG, " skip - "
458 "SSID mismatch");
459 continue;
460 }
461
462 if (ssid->bssid_set &&
463 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
464 {
465 wpa_printf(MSG_DEBUG, " skip - "
466 "BSSID mismatch");
467 continue;
468 }
469
470 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
471 continue;
472
473 wpa_printf(MSG_DEBUG, " selected WPA AP "
474 MACSTR " ssid='%s'",
475 MAC2STR(bss->bssid),
476 wpa_ssid_txt(ssid_, ssid_len));
477 *selected_ssid = ssid;
478 return bss;
479 }
480 }
481
482 return NULL;
483 }
484
485
486 static struct wpa_scan_res *
487 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
488 struct wpa_ssid *group,
489 struct wpa_ssid **selected_ssid)
490 {
491 struct wpa_ssid *ssid;
492 struct wpa_scan_res *bss;
493 size_t i;
494 struct wpa_blacklist *e;
495 const u8 *ie;
496
497 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
498 for (i = 0; i < wpa_s->scan_res->num; i++) {
499 const u8 *ssid_;
500 u8 wpa_ie_len, rsn_ie_len, ssid_len;
501 bss = wpa_s->scan_res->res[i];
502
503 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
504 ssid_ = ie ? ie + 2 : (u8 *) "";
505 ssid_len = ie ? ie[1] : 0;
506
507 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
508 wpa_ie_len = ie ? ie[1] : 0;
509
510 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
511 rsn_ie_len = ie ? ie[1] : 0;
512
513 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
514 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
515 (int) i, MAC2STR(bss->bssid),
516 wpa_ssid_txt(ssid_, ssid_len),
517 wpa_ie_len, rsn_ie_len, bss->caps);
518
519 e = wpa_blacklist_get(wpa_s, bss->bssid);
520 if (e && e->count > 1) {
521 wpa_printf(MSG_DEBUG, " skip - blacklisted");
522 continue;
523 }
524
525 if (ssid_len == 0) {
526 wpa_printf(MSG_DEBUG, " skip - SSID not known");
527 continue;
528 }
529
530 for (ssid = group; ssid; ssid = ssid->pnext) {
531 int check_ssid = ssid->ssid_len != 0;
532
533 if (ssid->disabled) {
534 wpa_printf(MSG_DEBUG, " skip - disabled");
535 continue;
536 }
537
538 #ifdef CONFIG_WPS
539 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
540 /* Only allow wildcard SSID match if an AP
541 * advertises active WPS operation that matches
542 * with our mode. */
543 check_ssid = 1;
544 if (ssid->ssid_len == 0 &&
545 wpas_wps_ssid_wildcard_ok(wpa_s, ssid,
546 bss))
547 check_ssid = 0;
548 }
549 #endif /* CONFIG_WPS */
550
551 if (check_ssid &&
552 (ssid_len != ssid->ssid_len ||
553 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
554 wpa_printf(MSG_DEBUG, " skip - "
555 "SSID mismatch");
556 continue;
557 }
558
559 if (ssid->bssid_set &&
560 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
561 {
562 wpa_printf(MSG_DEBUG, " skip - "
563 "BSSID mismatch");
564 continue;
565 }
566
567 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
568 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
569 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
570 {
571 wpa_printf(MSG_DEBUG, " skip - "
572 "non-WPA network not allowed");
573 continue;
574 }
575
576 if ((ssid->key_mgmt &
577 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
578 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
579 WPA_KEY_MGMT_IEEE8021X_SHA256 |
580 WPA_KEY_MGMT_PSK_SHA256)) &&
581 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
582 wpa_printf(MSG_DEBUG, " skip - "
583 "WPA network");
584 continue;
585 }
586
587 if (!wpa_supplicant_match_privacy(bss, ssid)) {
588 wpa_printf(MSG_DEBUG, " skip - "
589 "privacy mismatch");
590 continue;
591 }
592
593 if (bss->caps & IEEE80211_CAP_IBSS) {
594 wpa_printf(MSG_DEBUG, " skip - "
595 "IBSS (adhoc) network");
596 continue;
597 }
598
599 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
600 MACSTR " ssid='%s'",
601 MAC2STR(bss->bssid),
602 wpa_ssid_txt(ssid_, ssid_len));
603 *selected_ssid = ssid;
604 return bss;
605 }
606 }
607
608 return NULL;
609 }
610
611
612 static struct wpa_scan_res *
613 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
614 struct wpa_ssid **selected_ssid)
615 {
616 struct wpa_scan_res *selected;
617
618 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
619 group->priority);
620
621 /* First, try to find WPA-enabled AP */
622 selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
623 if (selected)
624 return selected;
625
626 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
627 * allows this. */
628 return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
629 }
630
631
632 static struct wpa_scan_res *
633 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
634 struct wpa_ssid **selected_ssid)
635 {
636 struct wpa_scan_res *selected = NULL;
637 int prio;
638
639 while (selected == NULL) {
640 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
641 selected = wpa_supplicant_select_bss(
642 wpa_s, wpa_s->conf->pssid[prio],
643 selected_ssid);
644 if (selected)
645 break;
646 }
647
648 if (selected == NULL && wpa_s->blacklist) {
649 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
650 "and try again");
651 wpa_blacklist_clear(wpa_s);
652 wpa_s->blacklist_cleared++;
653 } else if (selected == NULL)
654 break;
655 }
656
657 return selected;
658 }
659
660
661 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
662 int timeout)
663 {
664 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
665 /*
666 * Quick recovery if the initial scan results were not
667 * complete when fetched before the first scan request.
668 */
669 wpa_s->scan_res_tried++;
670 timeout = 0;
671 } else if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
672 /*
673 * No networks are enabled; short-circuit request so
674 * we don't wait timeout seconds before transitioning
675 * to INACTIVE state.
676 */
677 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
678 return;
679 }
680 wpa_supplicant_req_scan(wpa_s, timeout, 0);
681 }
682
683
684 static void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
685 struct wpa_scan_res *selected,
686 struct wpa_ssid *ssid)
687 {
688 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
689 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
690 "PBC session overlap");
691 wpa_supplicant_req_new_scan(wpa_s, 10);
692 return;
693 }
694
695 /*
696 * Do not trigger new association unless the BSSID has changed or if
697 * reassociation is requested. If we are in process of associating with
698 * the selected BSSID, do not trigger new attempt.
699 */
700 if (wpa_s->reassociate ||
701 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
702 (wpa_s->wpa_state != WPA_ASSOCIATING ||
703 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
704 0))) {
705 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
706 wpa_supplicant_req_new_scan(wpa_s, 10);
707 return;
708 }
709 wpa_supplicant_associate(wpa_s, selected, ssid);
710 } else {
711 wpa_printf(MSG_DEBUG, "Already associated with the selected "
712 "AP");
713 }
714 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
715 }
716
717
718 static struct wpa_ssid *
719 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
720 {
721 int prio;
722 struct wpa_ssid *ssid;
723
724 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
725 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
726 {
727 if (ssid->disabled)
728 continue;
729 if (ssid->mode == IEEE80211_MODE_IBSS ||
730 ssid->mode == IEEE80211_MODE_AP)
731 return ssid;
732 }
733 }
734 return NULL;
735 }
736
737
738 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
739 {
740 struct wpa_scan_res *selected;
741 struct wpa_ssid *ssid = NULL;
742
743 wpa_supplicant_notify_scanning(wpa_s, 0);
744
745 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
746 if (wpa_s->conf->ap_scan == 2)
747 return;
748 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
749 "scanning again");
750 wpa_supplicant_req_new_scan(wpa_s, 1);
751 return;
752 }
753
754 /*
755 * Don't post the results if this was the initial cached
756 * and there were no results.
757 */
758 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
759 wpa_s->scan_res->num == 0) {
760 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
761 "empty - not posting");
762 } else {
763 wpa_printf(MSG_DEBUG, "New scan results available");
764 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
765 wpas_notify_scan_results(wpa_s);
766 }
767
768 wpas_notify_scan_done(wpa_s, 1);
769
770 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
771 return;
772
773 if (wpa_s->disconnected) {
774 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
775 return;
776 }
777
778 if (bgscan_notify_scan(wpa_s) == 1)
779 return;
780
781 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
782 if (selected) {
783 wpa_supplicant_connect(wpa_s, selected, ssid);
784 } else {
785 wpa_printf(MSG_DEBUG, "No suitable network found");
786 ssid = wpa_supplicant_pick_new_network(wpa_s);
787 if (ssid) {
788 wpa_printf(MSG_DEBUG, "Setup a new network");
789 wpa_supplicant_associate(wpa_s, NULL, ssid);
790 } else
791 wpa_supplicant_req_new_scan(wpa_s, 5);
792 }
793 }
794 #endif /* CONFIG_NO_SCAN_PROCESSING */
795
796
797 #ifdef CONFIG_IEEE80211R
798 static void wpa_assoc_set_ft_params(struct wpa_supplicant *wpa_s,
799 const u8 *ftie, const u8 *mdie)
800 {
801 const u8 *mobility_domain = NULL;
802 const u8 *r0kh_id = NULL;
803 size_t r0kh_id_len = 0;
804 const u8 *r1kh_id = NULL;
805 struct rsn_ftie *hdr;
806 const u8 *pos, *end;
807
808 if (mdie == NULL || ftie == NULL)
809 return;
810
811 if (mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
812 mobility_domain = mdie + 2;
813 #ifdef CONFIG_SME
814 wpa_s->sme.ft_used = 1;
815 os_memcpy(wpa_s->sme.mobility_domain, mobility_domain, 2);
816 #endif /* CONFIG_SME */
817 }
818 if (ftie[1] >= sizeof(struct rsn_ftie)) {
819 end = ftie + 2 + ftie[1];
820 hdr = (struct rsn_ftie *) (ftie + 2);
821 pos = (const u8 *) (hdr + 1);
822 while (pos + 1 < end) {
823 if (pos + 2 + pos[1] > end)
824 break;
825 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
826 pos[1] == FT_R1KH_ID_LEN)
827 r1kh_id = pos + 2;
828 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
829 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
830 r0kh_id = pos + 2;
831 r0kh_id_len = pos[1];
832 }
833 pos += 2 + pos[1];
834 }
835 }
836 wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
837 r0kh_id_len, r1kh_id);
838 }
839 #endif /* CONFIG_IEEE80211R */
840
841 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
842 union wpa_event_data *data)
843 {
844 int l, len, found = 0, wpa_found, rsn_found;
845 const u8 *p;
846 #ifdef CONFIG_IEEE80211R
847 const u8 *mdie = NULL, *ftie = NULL;
848 #endif /* CONFIG_IEEE80211R */
849
850 wpa_printf(MSG_DEBUG, "Association info event");
851 if (data->assoc_info.req_ies)
852 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
853 data->assoc_info.req_ies_len);
854 if (data->assoc_info.resp_ies)
855 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
856 data->assoc_info.resp_ies_len);
857 if (data->assoc_info.beacon_ies)
858 wpa_hexdump(MSG_DEBUG, "beacon_ies",
859 data->assoc_info.beacon_ies,
860 data->assoc_info.beacon_ies_len);
861
862 p = data->assoc_info.req_ies;
863 l = data->assoc_info.req_ies_len;
864
865 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
866 while (p && l >= 2) {
867 len = p[1] + 2;
868 if (len > l) {
869 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
870 p, l);
871 break;
872 }
873 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
874 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
875 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
876 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
877 break;
878 found = 1;
879 wpa_find_assoc_pmkid(wpa_s);
880 break;
881 }
882 l -= len;
883 p += len;
884 }
885 if (!found && data->assoc_info.req_ies)
886 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
887
888 #ifdef CONFIG_IEEE80211R
889 p = data->assoc_info.resp_ies;
890 l = data->assoc_info.resp_ies_len;
891
892 /* Go through the IEs and make a copy of the FT/MD IE, if present. */
893 while (p && l >= 2) {
894 len = p[1] + 2;
895 if (len > l) {
896 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
897 p, l);
898 break;
899 }
900 if (p[0] == WLAN_EID_FAST_BSS_TRANSITION)
901 ftie = p;
902 else if (p[0] == WLAN_EID_MOBILITY_DOMAIN)
903 mdie = p;
904 l -= len;
905 p += len;
906 }
907
908 wpa_assoc_set_ft_params(wpa_s, ftie, mdie);
909 #endif /* CONFIG_IEEE80211R */
910
911 /* WPA/RSN IE from Beacon/ProbeResp */
912 p = data->assoc_info.beacon_ies;
913 l = data->assoc_info.beacon_ies_len;
914
915 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
916 */
917 wpa_found = rsn_found = 0;
918 while (p && l >= 2) {
919 len = p[1] + 2;
920 if (len > l) {
921 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
922 p, l);
923 break;
924 }
925 if (!wpa_found &&
926 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
927 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
928 wpa_found = 1;
929 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
930 }
931
932 if (!rsn_found &&
933 p[0] == WLAN_EID_RSN && p[1] >= 2) {
934 rsn_found = 1;
935 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
936 }
937
938 l -= len;
939 p += len;
940 }
941
942 if (!wpa_found && data->assoc_info.beacon_ies)
943 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
944 if (!rsn_found && data->assoc_info.beacon_ies)
945 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
946 if (wpa_found || rsn_found)
947 wpa_s->ap_ies_from_associnfo = 1;
948 }
949
950
951 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
952 union wpa_event_data *data)
953 {
954 u8 bssid[ETH_ALEN];
955 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
956 int bssid_changed;
957 struct wpa_driver_capa capa;
958
959 if (data)
960 wpa_supplicant_event_associnfo(wpa_s, data);
961
962 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
963 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
964 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
965 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
966 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
967 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
968 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
969 MACSTR, MAC2STR(bssid));
970 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
971 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
972 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
973 if (bssid_changed)
974 wpas_notify_bssid_changed(wpa_s);
975
976 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
977 wpa_clear_keys(wpa_s, bssid);
978 }
979 if (wpa_supplicant_select_config(wpa_s) < 0) {
980 wpa_supplicant_disassociate(
981 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
982 return;
983 }
984 }
985
986 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
987 if (wpa_s->current_ssid) {
988 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
989 * initialized before association, but for other modes,
990 * initialize PC/SC here, if the current configuration needs
991 * smartcard or SIM/USIM. */
992 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
993 }
994 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
995 l2_packet_notify_auth_start(wpa_s->l2);
996
997 /*
998 * Set portEnabled first to FALSE in order to get EAP state machine out
999 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1000 * state machine may transit to AUTHENTICATING state based on obsolete
1001 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1002 * AUTHENTICATED without ever giving chance to EAP state machine to
1003 * reset the state.
1004 */
1005 if (!ft_completed) {
1006 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1007 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1008 }
1009 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1010 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1011 /* 802.1X::portControl = Auto */
1012 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1013 wpa_s->eapol_received = 0;
1014 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1015 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1016 wpa_supplicant_cancel_auth_timeout(wpa_s);
1017 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1018 } else if (!ft_completed) {
1019 /* Timeout for receiving the first EAPOL packet */
1020 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1021 }
1022 wpa_supplicant_cancel_scan(wpa_s);
1023
1024 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1025 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1026 /*
1027 * We are done; the driver will take care of RSN 4-way
1028 * handshake.
1029 */
1030 wpa_supplicant_cancel_auth_timeout(wpa_s);
1031 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1032 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1033 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1034 }
1035
1036 if (wpa_s->pending_eapol_rx) {
1037 struct os_time now, age;
1038 os_get_time(&now);
1039 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1040 if (age.sec == 0 && age.usec < 100000 &&
1041 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1042 0) {
1043 wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1044 "that was received just before association "
1045 "notification");
1046 wpa_supplicant_rx_eapol(
1047 wpa_s, wpa_s->pending_eapol_rx_src,
1048 wpabuf_head(wpa_s->pending_eapol_rx),
1049 wpabuf_len(wpa_s->pending_eapol_rx));
1050 }
1051 wpabuf_free(wpa_s->pending_eapol_rx);
1052 wpa_s->pending_eapol_rx = NULL;
1053 }
1054
1055 #ifdef CONFIG_BGSCAN
1056 if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1057 bgscan_deinit(wpa_s);
1058 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1059 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1060 wpa_printf(MSG_DEBUG, "Failed to initialize "
1061 "bgscan");
1062 /*
1063 * Live without bgscan; it is only used as a
1064 * roaming optimization, so the initial
1065 * connection is not affected.
1066 */
1067 } else
1068 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1069 } else
1070 wpa_s->bgscan_ssid = NULL;
1071 }
1072 #endif /* CONFIG_BGSCAN */
1073
1074 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1075 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1076 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1077 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1078 /* Set static WEP keys again */
1079 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1080 }
1081 }
1082
1083
1084 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
1085 {
1086 const u8 *bssid;
1087 #ifdef CONFIG_SME
1088 int authenticating;
1089 u8 prev_pending_bssid[ETH_ALEN];
1090
1091 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1092 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1093 #endif /* CONFIG_SME */
1094
1095 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1096 /*
1097 * At least Host AP driver and a Prism3 card seemed to be
1098 * generating streams of disconnected events when configuring
1099 * IBSS for WPA-None. Ignore them for now.
1100 */
1101 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1102 "IBSS/WPA-None mode");
1103 return;
1104 }
1105
1106 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1107 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1108 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1109 "pre-shared key may be incorrect");
1110 }
1111 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1112 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1113 bssid = wpa_s->bssid;
1114 if (is_zero_ether_addr(bssid))
1115 bssid = wpa_s->pending_bssid;
1116 wpa_blacklist_add(wpa_s, bssid);
1117 wpa_sm_notify_disassoc(wpa_s->wpa);
1118 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
1119 "remove keys");
1120 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1121 wpa_s->keys_cleared = 0;
1122 wpa_clear_keys(wpa_s, wpa_s->bssid);
1123 }
1124 wpa_supplicant_mark_disassoc(wpa_s);
1125 bgscan_deinit(wpa_s);
1126 #ifdef CONFIG_SME
1127 if (authenticating &&
1128 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1129 /*
1130 * mac80211-workaround to force deauth on failed auth cmd,
1131 * requires us to remain in authenticating state to allow the
1132 * second authentication attempt to be continued properly.
1133 */
1134 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1135 "proceed after disconnection event");
1136 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1137 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1138 }
1139 #endif /* CONFIG_SME */
1140 }
1141
1142
1143 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1144 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1145 void *sock_ctx)
1146 {
1147 struct wpa_supplicant *wpa_s = eloop_ctx;
1148
1149 if (!wpa_s->pending_mic_error_report)
1150 return;
1151
1152 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1153 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1154 wpa_s->pending_mic_error_report = 0;
1155 }
1156 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1157
1158
1159 static void
1160 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1161 union wpa_event_data *data)
1162 {
1163 int pairwise;
1164 struct os_time t;
1165
1166 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1167 pairwise = (data && data->michael_mic_failure.unicast);
1168 os_get_time(&t);
1169 if ((wpa_s->last_michael_mic_error &&
1170 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1171 wpa_s->pending_mic_error_report) {
1172 if (wpa_s->pending_mic_error_report) {
1173 /*
1174 * Send the pending MIC error report immediately since
1175 * we are going to start countermeasures and AP better
1176 * do the same.
1177 */
1178 wpa_sm_key_request(wpa_s->wpa, 1,
1179 wpa_s->pending_mic_error_pairwise);
1180 }
1181
1182 /* Send the new MIC error report immediately since we are going
1183 * to start countermeasures and AP better do the same.
1184 */
1185 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1186
1187 /* initialize countermeasures */
1188 wpa_s->countermeasures = 1;
1189 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1190
1191 /*
1192 * Need to wait for completion of request frame. We do not get
1193 * any callback for the message completion, so just wait a
1194 * short while and hope for the best. */
1195 os_sleep(0, 10000);
1196
1197 wpa_drv_set_countermeasures(wpa_s, 1);
1198 wpa_supplicant_deauthenticate(wpa_s,
1199 WLAN_REASON_MICHAEL_MIC_FAILURE);
1200 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1201 wpa_s, NULL);
1202 eloop_register_timeout(60, 0,
1203 wpa_supplicant_stop_countermeasures,
1204 wpa_s, NULL);
1205 /* TODO: mark the AP rejected for 60 second. STA is
1206 * allowed to associate with another AP.. */
1207 } else {
1208 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1209 if (wpa_s->mic_errors_seen) {
1210 /*
1211 * Reduce the effectiveness of Michael MIC error
1212 * reports as a means for attacking against TKIP if
1213 * more than one MIC failure is noticed with the same
1214 * PTK. We delay the transmission of the reports by a
1215 * random time between 0 and 60 seconds in order to
1216 * force the attacker wait 60 seconds before getting
1217 * the information on whether a frame resulted in a MIC
1218 * failure.
1219 */
1220 u8 rval[4];
1221 int sec;
1222
1223 if (os_get_random(rval, sizeof(rval)) < 0)
1224 sec = os_random() % 60;
1225 else
1226 sec = WPA_GET_BE32(rval) % 60;
1227 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1228 "seconds", sec);
1229 wpa_s->pending_mic_error_report = 1;
1230 wpa_s->pending_mic_error_pairwise = pairwise;
1231 eloop_cancel_timeout(
1232 wpa_supplicant_delayed_mic_error_report,
1233 wpa_s, NULL);
1234 eloop_register_timeout(
1235 sec, os_random() % 1000000,
1236 wpa_supplicant_delayed_mic_error_report,
1237 wpa_s, NULL);
1238 } else {
1239 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1240 }
1241 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1242 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1243 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1244 }
1245 wpa_s->last_michael_mic_error = t.sec;
1246 wpa_s->mic_errors_seen++;
1247 }
1248
1249
1250 #ifdef CONFIG_TERMINATE_ONLASTIF
1251 static int any_interfaces(struct wpa_supplicant *head)
1252 {
1253 struct wpa_supplicant *wpa_s;
1254
1255 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1256 if (!wpa_s->interface_removed)
1257 return 1;
1258 return 0;
1259 }
1260 #endif /* CONFIG_TERMINATE_ONLASTIF */
1261
1262
1263 static void
1264 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1265 union wpa_event_data *data)
1266 {
1267 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1268 return;
1269
1270 switch (data->interface_status.ievent) {
1271 case EVENT_INTERFACE_ADDED:
1272 if (!wpa_s->interface_removed)
1273 break;
1274 wpa_s->interface_removed = 0;
1275 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1276 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1277 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1278 "after interface was added.");
1279 }
1280 break;
1281 case EVENT_INTERFACE_REMOVED:
1282 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1283 wpa_s->interface_removed = 1;
1284 wpa_supplicant_mark_disassoc(wpa_s);
1285 l2_packet_deinit(wpa_s->l2);
1286 wpa_s->l2 = NULL;
1287 #ifdef CONFIG_TERMINATE_ONLASTIF
1288 /* check if last interface */
1289 if (!any_interfaces(wpa_s->global->ifaces))
1290 eloop_terminate();
1291 #endif /* CONFIG_TERMINATE_ONLASTIF */
1292 break;
1293 }
1294 }
1295
1296
1297 #ifdef CONFIG_PEERKEY
1298 static void
1299 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1300 union wpa_event_data *data)
1301 {
1302 if (data == NULL)
1303 return;
1304 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1305 }
1306 #endif /* CONFIG_PEERKEY */
1307
1308
1309 #ifdef CONFIG_IEEE80211R
1310 static void
1311 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1312 union wpa_event_data *data)
1313 {
1314 if (data == NULL)
1315 return;
1316
1317 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1318 data->ft_ies.ies_len,
1319 data->ft_ies.ft_action,
1320 data->ft_ies.target_ap,
1321 data->ft_ies.ric_ies,
1322 data->ft_ies.ric_ies_len) < 0) {
1323 /* TODO: prevent MLME/driver from trying to associate? */
1324 }
1325 }
1326 #endif /* CONFIG_IEEE80211R */
1327
1328
1329 #ifdef CONFIG_IBSS_RSN
1330 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1331 union wpa_event_data *data)
1332 {
1333 if (data == NULL)
1334 return;
1335 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1336 }
1337 #endif /* CONFIG_IBSS_RSN */
1338
1339
1340 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1341 union wpa_event_data *data)
1342 {
1343 struct wpa_supplicant *wpa_s = ctx;
1344
1345 switch (event) {
1346 case EVENT_AUTH:
1347 sme_event_auth(wpa_s, data);
1348 break;
1349 case EVENT_ASSOC:
1350 wpa_supplicant_event_assoc(wpa_s, data);
1351 break;
1352 case EVENT_DEAUTH:
1353 case EVENT_DISASSOC:
1354 wpa_supplicant_event_disassoc(wpa_s);
1355 break;
1356 case EVENT_MICHAEL_MIC_FAILURE:
1357 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1358 break;
1359 #ifndef CONFIG_NO_SCAN_PROCESSING
1360 case EVENT_SCAN_RESULTS:
1361 wpa_supplicant_event_scan_results(wpa_s);
1362 break;
1363 #endif /* CONFIG_NO_SCAN_PROCESSING */
1364 case EVENT_ASSOCINFO:
1365 wpa_supplicant_event_associnfo(wpa_s, data);
1366 break;
1367 case EVENT_INTERFACE_STATUS:
1368 wpa_supplicant_event_interface_status(wpa_s, data);
1369 break;
1370 case EVENT_PMKID_CANDIDATE:
1371 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1372 break;
1373 #ifdef CONFIG_PEERKEY
1374 case EVENT_STKSTART:
1375 wpa_supplicant_event_stkstart(wpa_s, data);
1376 break;
1377 #endif /* CONFIG_PEERKEY */
1378 #ifdef CONFIG_IEEE80211R
1379 case EVENT_FT_RESPONSE:
1380 wpa_supplicant_event_ft_response(wpa_s, data);
1381 break;
1382 #endif /* CONFIG_IEEE80211R */
1383 #ifdef CONFIG_IBSS_RSN
1384 case EVENT_IBSS_RSN_START:
1385 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1386 break;
1387 #endif /* CONFIG_IBSS_RSN */
1388 case EVENT_ASSOC_REJECT:
1389 sme_event_assoc_reject(wpa_s, data);
1390 break;
1391 case EVENT_AUTH_TIMED_OUT:
1392 sme_event_auth_timed_out(wpa_s, data);
1393 break;
1394 case EVENT_ASSOC_TIMED_OUT:
1395 sme_event_assoc_timed_out(wpa_s, data);
1396 break;
1397 default:
1398 wpa_printf(MSG_INFO, "Unknown event %d", event);
1399 break;
1400 }
1401 }