]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/events.c
Added initial step for IBSS RSN support
[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 "drivers/driver.h"
22 #include "config.h"
23 #include "l2_packet/l2_packet.h"
24 #include "wpa_supplicant_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 "ctrl_iface_dbus.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
37
38 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
39 {
40 struct wpa_ssid *ssid;
41
42 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
43 return 0;
44
45 wpa_printf(MSG_DEBUG, "Select network based on association "
46 "information");
47 ssid = wpa_supplicant_get_ssid(wpa_s);
48 if (ssid == NULL) {
49 wpa_printf(MSG_INFO, "No network configuration found for the "
50 "current AP");
51 return -1;
52 }
53
54 if (ssid->disabled) {
55 wpa_printf(MSG_DEBUG, "Selected network is disabled");
56 return -1;
57 }
58
59 wpa_printf(MSG_DEBUG, "Network configuration found for the current "
60 "AP");
61 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
62 WPA_KEY_MGMT_WPA_NONE |
63 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
64 WPA_KEY_MGMT_PSK_SHA256 |
65 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
66 u8 wpa_ie[80];
67 size_t wpa_ie_len = sizeof(wpa_ie);
68 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
69 wpa_ie, &wpa_ie_len);
70 } else {
71 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
72 }
73
74 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
75 eapol_sm_invalidate_cached_session(wpa_s->eapol);
76 wpa_s->current_ssid = ssid;
77 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
78 wpa_supplicant_initiate_eapol(wpa_s);
79
80 return 0;
81 }
82
83
84 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
85 void *sock_ctx)
86 {
87 struct wpa_supplicant *wpa_s = eloop_ctx;
88
89 if (wpa_s->countermeasures) {
90 wpa_s->countermeasures = 0;
91 wpa_drv_set_countermeasures(wpa_s, 0);
92 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
93 wpa_supplicant_req_scan(wpa_s, 0, 0);
94 }
95 }
96
97
98 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
99 {
100 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
101 os_memset(wpa_s->bssid, 0, ETH_ALEN);
102 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
103 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
104 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
105 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
106 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
107 wpa_s->ap_ies_from_associnfo = 0;
108 }
109
110
111 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
112 {
113 struct wpa_ie_data ie;
114 int pmksa_set = -1;
115 size_t i;
116
117 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
118 ie.pmkid == NULL)
119 return;
120
121 for (i = 0; i < ie.num_pmkid; i++) {
122 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
123 ie.pmkid + i * PMKID_LEN,
124 NULL, NULL, 0);
125 if (pmksa_set == 0) {
126 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
127 break;
128 }
129 }
130
131 wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
132 "cache", pmksa_set == 0 ? "" : "not ");
133 }
134
135
136 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
137 union wpa_event_data *data)
138 {
139 if (data == NULL) {
140 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
141 return;
142 }
143 wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
144 " index=%d preauth=%d",
145 MAC2STR(data->pmkid_candidate.bssid),
146 data->pmkid_candidate.index,
147 data->pmkid_candidate.preauth);
148
149 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
150 data->pmkid_candidate.index,
151 data->pmkid_candidate.preauth);
152 }
153
154
155 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
156 {
157 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
158 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
159 return 0;
160
161 #ifdef IEEE8021X_EAPOL
162 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
163 wpa_s->current_ssid &&
164 !(wpa_s->current_ssid->eapol_flags &
165 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
166 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
167 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
168 * plaintext or static WEP keys). */
169 return 0;
170 }
171 #endif /* IEEE8021X_EAPOL */
172
173 return 1;
174 }
175
176
177 /**
178 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
179 * @wpa_s: pointer to wpa_supplicant data
180 * @ssid: Configuration data for the network
181 * Returns: 0 on success, -1 on failure
182 *
183 * This function is called when starting authentication with a network that is
184 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
185 */
186 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
187 struct wpa_ssid *ssid)
188 {
189 #ifdef IEEE8021X_EAPOL
190 int aka = 0, sim = 0, type;
191
192 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
193 return 0;
194
195 if (ssid->eap.eap_methods == NULL) {
196 sim = 1;
197 aka = 1;
198 } else {
199 struct eap_method_type *eap = ssid->eap.eap_methods;
200 while (eap->vendor != EAP_VENDOR_IETF ||
201 eap->method != EAP_TYPE_NONE) {
202 if (eap->vendor == EAP_VENDOR_IETF) {
203 if (eap->method == EAP_TYPE_SIM)
204 sim = 1;
205 else if (eap->method == EAP_TYPE_AKA)
206 aka = 1;
207 }
208 eap++;
209 }
210 }
211
212 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
213 sim = 0;
214 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
215 aka = 0;
216
217 if (!sim && !aka) {
218 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
219 "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
220 return 0;
221 }
222
223 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
224 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
225 if (sim && aka)
226 type = SCARD_TRY_BOTH;
227 else if (aka)
228 type = SCARD_USIM_ONLY;
229 else
230 type = SCARD_GSM_SIM_ONLY;
231
232 wpa_s->scard = scard_init(type);
233 if (wpa_s->scard == NULL) {
234 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
235 "(pcsc-lite)");
236 return -1;
237 }
238 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
239 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
240 #endif /* IEEE8021X_EAPOL */
241
242 return 0;
243 }
244
245
246 #ifndef CONFIG_NO_SCAN_PROCESSING
247 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
248 struct wpa_ssid *ssid)
249 {
250 int i, privacy = 0;
251
252 if (ssid->mixed_cell)
253 return 1;
254
255 for (i = 0; i < NUM_WEP_KEYS; i++) {
256 if (ssid->wep_key_len[i]) {
257 privacy = 1;
258 break;
259 }
260 }
261 #ifdef IEEE8021X_EAPOL
262 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
263 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
264 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
265 privacy = 1;
266 #endif /* IEEE8021X_EAPOL */
267
268 if (bss->caps & IEEE80211_CAP_PRIVACY)
269 return privacy;
270 return !privacy;
271 }
272
273
274 static int wpa_supplicant_ssid_bss_match(struct wpa_ssid *ssid,
275 struct wpa_scan_res *bss)
276 {
277 struct wpa_ie_data ie;
278 int proto_match = 0;
279 const u8 *rsn_ie, *wpa_ie;
280 int ret;
281
282 ret = wpas_wps_ssid_bss_match(ssid, bss);
283 if (ret >= 0)
284 return ret;
285
286 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
287 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
288 proto_match++;
289
290 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
291 wpa_printf(MSG_DEBUG, " skip RSN IE - parse failed");
292 break;
293 }
294 if (!(ie.proto & ssid->proto)) {
295 wpa_printf(MSG_DEBUG, " skip RSN IE - proto "
296 "mismatch");
297 break;
298 }
299
300 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
301 wpa_printf(MSG_DEBUG, " skip RSN IE - PTK cipher "
302 "mismatch");
303 break;
304 }
305
306 if (!(ie.group_cipher & ssid->group_cipher)) {
307 wpa_printf(MSG_DEBUG, " skip RSN IE - GTK cipher "
308 "mismatch");
309 break;
310 }
311
312 if (!(ie.key_mgmt & ssid->key_mgmt)) {
313 wpa_printf(MSG_DEBUG, " skip RSN IE - key mgmt "
314 "mismatch");
315 break;
316 }
317
318 #ifdef CONFIG_IEEE80211W
319 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
320 ssid->ieee80211w == IEEE80211W_REQUIRED) {
321 wpa_printf(MSG_DEBUG, " skip RSN IE - no mgmt frame "
322 "protection");
323 break;
324 }
325 #endif /* CONFIG_IEEE80211W */
326
327 wpa_printf(MSG_DEBUG, " selected based on RSN IE");
328 return 1;
329 }
330
331 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
332 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
333 proto_match++;
334
335 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
336 wpa_printf(MSG_DEBUG, " skip WPA IE - parse failed");
337 break;
338 }
339 if (!(ie.proto & ssid->proto)) {
340 wpa_printf(MSG_DEBUG, " skip WPA IE - proto "
341 "mismatch");
342 break;
343 }
344
345 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
346 wpa_printf(MSG_DEBUG, " skip WPA IE - PTK cipher "
347 "mismatch");
348 break;
349 }
350
351 if (!(ie.group_cipher & ssid->group_cipher)) {
352 wpa_printf(MSG_DEBUG, " skip WPA IE - GTK cipher "
353 "mismatch");
354 break;
355 }
356
357 if (!(ie.key_mgmt & ssid->key_mgmt)) {
358 wpa_printf(MSG_DEBUG, " skip WPA IE - key mgmt "
359 "mismatch");
360 break;
361 }
362
363 wpa_printf(MSG_DEBUG, " selected based on WPA IE");
364 return 1;
365 }
366
367 if (proto_match == 0)
368 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN proto match");
369
370 return 0;
371 }
372
373
374 static struct wpa_scan_res *
375 wpa_supplicant_select_bss_wpa(struct wpa_supplicant *wpa_s,
376 struct wpa_ssid *group,
377 struct wpa_ssid **selected_ssid)
378 {
379 struct wpa_ssid *ssid;
380 struct wpa_scan_res *bss;
381 size_t i;
382 struct wpa_blacklist *e;
383 const u8 *ie;
384
385 wpa_printf(MSG_DEBUG, "Try to find WPA-enabled AP");
386 for (i = 0; i < wpa_s->scan_res->num; i++) {
387 const u8 *ssid_;
388 u8 wpa_ie_len, rsn_ie_len, ssid_len;
389 bss = wpa_s->scan_res->res[i];
390
391 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
392 ssid_ = ie ? ie + 2 : (u8 *) "";
393 ssid_len = ie ? ie[1] : 0;
394
395 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
396 wpa_ie_len = ie ? ie[1] : 0;
397
398 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
399 rsn_ie_len = ie ? ie[1] : 0;
400
401 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
402 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
403 (int) i, MAC2STR(bss->bssid),
404 wpa_ssid_txt(ssid_, ssid_len),
405 wpa_ie_len, rsn_ie_len, bss->caps);
406
407 e = wpa_blacklist_get(wpa_s, bss->bssid);
408 if (e && e->count > 1) {
409 wpa_printf(MSG_DEBUG, " skip - blacklisted");
410 continue;
411 }
412
413 if (wpa_ie_len == 0 && rsn_ie_len == 0) {
414 wpa_printf(MSG_DEBUG, " skip - no WPA/RSN IE");
415 continue;
416 }
417
418 for (ssid = group; ssid; ssid = ssid->pnext) {
419 int check_ssid = 1;
420
421 if (ssid->disabled) {
422 wpa_printf(MSG_DEBUG, " skip - disabled");
423 continue;
424 }
425
426 #ifdef CONFIG_WPS
427 if (ssid->ssid_len == 0 &&
428 wpas_wps_ssid_wildcard_ok(ssid, bss))
429 check_ssid = 0;
430 #endif /* CONFIG_WPS */
431
432 if (check_ssid &&
433 (ssid_len != ssid->ssid_len ||
434 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
435 wpa_printf(MSG_DEBUG, " skip - "
436 "SSID mismatch");
437 continue;
438 }
439
440 if (ssid->bssid_set &&
441 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
442 {
443 wpa_printf(MSG_DEBUG, " skip - "
444 "BSSID mismatch");
445 continue;
446 }
447
448 if (!wpa_supplicant_ssid_bss_match(ssid, bss))
449 continue;
450
451 wpa_printf(MSG_DEBUG, " selected WPA AP "
452 MACSTR " ssid='%s'",
453 MAC2STR(bss->bssid),
454 wpa_ssid_txt(ssid_, ssid_len));
455 *selected_ssid = ssid;
456 return bss;
457 }
458 }
459
460 return NULL;
461 }
462
463
464 static struct wpa_scan_res *
465 wpa_supplicant_select_bss_non_wpa(struct wpa_supplicant *wpa_s,
466 struct wpa_ssid *group,
467 struct wpa_ssid **selected_ssid)
468 {
469 struct wpa_ssid *ssid;
470 struct wpa_scan_res *bss;
471 size_t i;
472 struct wpa_blacklist *e;
473 const u8 *ie;
474
475 wpa_printf(MSG_DEBUG, "Try to find non-WPA AP");
476 for (i = 0; i < wpa_s->scan_res->num; i++) {
477 const u8 *ssid_;
478 u8 wpa_ie_len, rsn_ie_len, ssid_len;
479 bss = wpa_s->scan_res->res[i];
480
481 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
482 ssid_ = ie ? ie + 2 : (u8 *) "";
483 ssid_len = ie ? ie[1] : 0;
484
485 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
486 wpa_ie_len = ie ? ie[1] : 0;
487
488 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
489 rsn_ie_len = ie ? ie[1] : 0;
490
491 wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
492 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x",
493 (int) i, MAC2STR(bss->bssid),
494 wpa_ssid_txt(ssid_, ssid_len),
495 wpa_ie_len, rsn_ie_len, bss->caps);
496
497 e = wpa_blacklist_get(wpa_s, bss->bssid);
498 if (e && e->count > 1) {
499 wpa_printf(MSG_DEBUG, " skip - blacklisted");
500 continue;
501 }
502
503 for (ssid = group; ssid; ssid = ssid->pnext) {
504 int check_ssid = ssid->ssid_len != 0;
505
506 if (ssid->disabled) {
507 wpa_printf(MSG_DEBUG, " skip - disabled");
508 continue;
509 }
510
511 #ifdef CONFIG_WPS
512 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
513 /* Only allow wildcard SSID match if an AP
514 * advertises active WPS operation that matches
515 * with our mode. */
516 check_ssid = 1;
517 if (ssid->ssid_len == 0 &&
518 wpas_wps_ssid_wildcard_ok(ssid, bss))
519 check_ssid = 0;
520 }
521 #endif /* CONFIG_WPS */
522
523 if (check_ssid &&
524 (ssid_len != ssid->ssid_len ||
525 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
526 wpa_printf(MSG_DEBUG, " skip - "
527 "SSID mismatch");
528 continue;
529 }
530
531 if (ssid->bssid_set &&
532 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0)
533 {
534 wpa_printf(MSG_DEBUG, " skip - "
535 "BSSID mismatch");
536 continue;
537 }
538
539 if (!(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
540 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
541 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA))
542 {
543 wpa_printf(MSG_DEBUG, " skip - "
544 "non-WPA network not allowed");
545 continue;
546 }
547
548 if ((ssid->key_mgmt &
549 (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
550 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK |
551 WPA_KEY_MGMT_IEEE8021X_SHA256 |
552 WPA_KEY_MGMT_PSK_SHA256)) &&
553 (wpa_ie_len != 0 || rsn_ie_len != 0)) {
554 wpa_printf(MSG_DEBUG, " skip - "
555 "WPA network");
556 continue;
557 }
558
559 if (!wpa_supplicant_match_privacy(bss, ssid)) {
560 wpa_printf(MSG_DEBUG, " skip - "
561 "privacy mismatch");
562 continue;
563 }
564
565 if (bss->caps & IEEE80211_CAP_IBSS) {
566 wpa_printf(MSG_DEBUG, " skip - "
567 "IBSS (adhoc) network");
568 continue;
569 }
570
571 wpa_printf(MSG_DEBUG, " selected non-WPA AP "
572 MACSTR " ssid='%s'",
573 MAC2STR(bss->bssid),
574 wpa_ssid_txt(ssid_, ssid_len));
575 *selected_ssid = ssid;
576 return bss;
577 }
578 }
579
580 return NULL;
581 }
582
583
584 static struct wpa_scan_res *
585 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s, struct wpa_ssid *group,
586 struct wpa_ssid **selected_ssid)
587 {
588 struct wpa_scan_res *selected;
589
590 wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
591 group->priority);
592
593 /* First, try to find WPA-enabled AP */
594 selected = wpa_supplicant_select_bss_wpa(wpa_s, group, selected_ssid);
595 if (selected)
596 return selected;
597
598 /* If no WPA-enabled AP found, try to find non-WPA AP, if configuration
599 * allows this. */
600 return wpa_supplicant_select_bss_non_wpa(wpa_s, group, selected_ssid);
601 }
602
603
604 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s)
605 {
606 int prio, timeout;
607 struct wpa_scan_res *selected = NULL;
608 struct wpa_ssid *ssid = NULL;
609
610 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
611 if (wpa_s->conf->ap_scan == 2)
612 return;
613 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
614 "scanning again");
615 timeout = 1;
616 goto req_scan;
617 }
618
619 /*
620 * Don't post the results if this was the initial cached
621 * and there were no results.
622 */
623 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1 &&
624 wpa_s->scan_res->num == 0) {
625 wpa_msg(wpa_s, MSG_DEBUG, "Cached scan results are "
626 "empty - not posting");
627 } else {
628 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
629 wpa_supplicant_dbus_notify_scan_results(wpa_s);
630 wpas_wps_notify_scan_results(wpa_s);
631 }
632
633 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)) ||
634 wpa_s->disconnected)
635 return;
636
637 while (selected == NULL) {
638 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
639 selected = wpa_supplicant_select_bss(
640 wpa_s, wpa_s->conf->pssid[prio], &ssid);
641 if (selected)
642 break;
643 }
644
645 if (selected == NULL && wpa_s->blacklist) {
646 wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
647 "and try again");
648 wpa_blacklist_clear(wpa_s);
649 } else if (selected == NULL) {
650 break;
651 }
652 }
653
654 if (selected) {
655 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
656 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
657 "PBC session overlap");
658 timeout = 10;
659 goto req_scan;
660 }
661
662 /* Do not trigger new association unless the BSSID has changed
663 * or if reassociation is requested. If we are in process of
664 * associating with the selected BSSID, do not trigger new
665 * attempt. */
666 if (wpa_s->reassociate ||
667 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
668 (wpa_s->wpa_state != WPA_ASSOCIATING ||
669 os_memcmp(selected->bssid, wpa_s->pending_bssid,
670 ETH_ALEN) != 0))) {
671 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
672 wpa_supplicant_req_scan(wpa_s, 10, 0);
673 return;
674 }
675 wpa_supplicant_associate(wpa_s, selected, ssid);
676 } else {
677 wpa_printf(MSG_DEBUG, "Already associated with the "
678 "selected AP.");
679 }
680 rsn_preauth_scan_results(wpa_s->wpa, wpa_s->scan_res);
681 } else {
682 wpa_printf(MSG_DEBUG, "No suitable AP found.");
683 timeout = 5;
684 goto req_scan;
685 }
686
687 return;
688
689 req_scan:
690 if (wpa_s->scan_res_tried == 1 && wpa_s->conf->ap_scan == 1) {
691 /*
692 * Quick recovery if the initial scan results were not
693 * complete when fetched before the first scan request.
694 */
695 wpa_s->scan_res_tried++;
696 timeout = 0;
697 }
698 wpa_supplicant_req_scan(wpa_s, timeout, 0);
699 }
700 #endif /* CONFIG_NO_SCAN_PROCESSING */
701
702
703 static void wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
704 union wpa_event_data *data)
705 {
706 int l, len, found = 0, wpa_found, rsn_found;
707 u8 *p;
708
709 wpa_printf(MSG_DEBUG, "Association info event");
710 if (data->assoc_info.req_ies)
711 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
712 data->assoc_info.req_ies_len);
713 if (data->assoc_info.resp_ies)
714 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
715 data->assoc_info.resp_ies_len);
716 if (data->assoc_info.beacon_ies)
717 wpa_hexdump(MSG_DEBUG, "beacon_ies",
718 data->assoc_info.beacon_ies,
719 data->assoc_info.beacon_ies_len);
720
721 p = data->assoc_info.req_ies;
722 l = data->assoc_info.req_ies_len;
723
724 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
725 while (p && l >= 2) {
726 len = p[1] + 2;
727 if (len > l) {
728 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
729 p, l);
730 break;
731 }
732 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
733 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
734 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
735 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
736 break;
737 found = 1;
738 wpa_find_assoc_pmkid(wpa_s);
739 break;
740 }
741 l -= len;
742 p += len;
743 }
744 if (!found && data->assoc_info.req_ies)
745 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
746
747 /* WPA/RSN IE from Beacon/ProbeResp */
748 p = data->assoc_info.beacon_ies;
749 l = data->assoc_info.beacon_ies_len;
750
751 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
752 */
753 wpa_found = rsn_found = 0;
754 while (p && l >= 2) {
755 len = p[1] + 2;
756 if (len > l) {
757 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
758 p, l);
759 break;
760 }
761 if (!wpa_found &&
762 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
763 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
764 wpa_found = 1;
765 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
766 }
767
768 if (!rsn_found &&
769 p[0] == WLAN_EID_RSN && p[1] >= 2) {
770 rsn_found = 1;
771 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
772 }
773
774 l -= len;
775 p += len;
776 }
777
778 if (!wpa_found && data->assoc_info.beacon_ies)
779 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
780 if (!rsn_found && data->assoc_info.beacon_ies)
781 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
782 if (wpa_found || rsn_found)
783 wpa_s->ap_ies_from_associnfo = 1;
784 }
785
786
787 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
788 union wpa_event_data *data)
789 {
790 u8 bssid[ETH_ALEN];
791 int ft_completed = wpa_ft_is_completed(wpa_s->wpa);
792
793 if (data)
794 wpa_supplicant_event_associnfo(wpa_s, data);
795
796 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
797 if (wpa_s->use_client_mlme)
798 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
799 if (wpa_s->use_client_mlme ||
800 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
801 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
802 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
803 MACSTR, MAC2STR(bssid));
804 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
805 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
806 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
807 wpa_clear_keys(wpa_s, bssid);
808 }
809 if (wpa_supplicant_select_config(wpa_s) < 0) {
810 wpa_supplicant_disassociate(
811 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
812 return;
813 }
814 }
815
816 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
817 if (wpa_s->current_ssid) {
818 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
819 * initialized before association, but for other modes,
820 * initialize PC/SC here, if the current configuration needs
821 * smartcard or SIM/USIM. */
822 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
823 }
824 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
825 l2_packet_notify_auth_start(wpa_s->l2);
826
827 /*
828 * Set portEnabled first to FALSE in order to get EAP state machine out
829 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
830 * state machine may transit to AUTHENTICATING state based on obsolete
831 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
832 * AUTHENTICATED without ever giving chance to EAP state machine to
833 * reset the state.
834 */
835 if (!ft_completed) {
836 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
837 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
838 }
839 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
840 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
841 /* 802.1X::portControl = Auto */
842 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
843 wpa_s->eapol_received = 0;
844 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
845 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
846 wpa_supplicant_cancel_auth_timeout(wpa_s);
847 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
848 } else if (!ft_completed) {
849 /* Timeout for receiving the first EAPOL packet */
850 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
851 }
852 wpa_supplicant_cancel_scan(wpa_s);
853
854 if (wpa_s->driver_4way_handshake &&
855 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
856 /*
857 * We are done; the driver will take care of RSN 4-way
858 * handshake.
859 */
860 wpa_supplicant_cancel_auth_timeout(wpa_s);
861 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
862 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
863 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
864 }
865 }
866
867
868 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s)
869 {
870 const u8 *bssid;
871
872 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
873 /*
874 * At least Host AP driver and a Prism3 card seemed to be
875 * generating streams of disconnected events when configuring
876 * IBSS for WPA-None. Ignore them for now.
877 */
878 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
879 "IBSS/WPA-None mode");
880 return;
881 }
882
883 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
884 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
885 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
886 "pre-shared key may be incorrect");
887 }
888 if (wpa_s->wpa_state >= WPA_ASSOCIATED)
889 wpa_supplicant_req_scan(wpa_s, 0, 100000);
890 bssid = wpa_s->bssid;
891 if (is_zero_ether_addr(bssid))
892 bssid = wpa_s->pending_bssid;
893 wpa_blacklist_add(wpa_s, bssid);
894 wpa_sm_notify_disassoc(wpa_s->wpa);
895 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "- Disconnect event - "
896 "remove keys");
897 if (wpa_supplicant_dynamic_keys(wpa_s)) {
898 wpa_s->keys_cleared = 0;
899 wpa_clear_keys(wpa_s, wpa_s->bssid);
900 }
901 wpa_supplicant_mark_disassoc(wpa_s);
902 }
903
904
905 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
906 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
907 void *sock_ctx)
908 {
909 struct wpa_supplicant *wpa_s = eloop_ctx;
910
911 if (!wpa_s->pending_mic_error_report)
912 return;
913
914 wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
915 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
916 wpa_s->pending_mic_error_report = 0;
917 }
918 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
919
920
921 static void
922 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
923 union wpa_event_data *data)
924 {
925 int pairwise;
926 struct os_time t;
927
928 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
929 pairwise = (data && data->michael_mic_failure.unicast);
930 os_get_time(&t);
931 if ((wpa_s->last_michael_mic_error &&
932 t.sec - wpa_s->last_michael_mic_error <= 60) ||
933 wpa_s->pending_mic_error_report) {
934 if (wpa_s->pending_mic_error_report) {
935 /*
936 * Send the pending MIC error report immediately since
937 * we are going to start countermeasures and AP better
938 * do the same.
939 */
940 wpa_sm_key_request(wpa_s->wpa, 1,
941 wpa_s->pending_mic_error_pairwise);
942 }
943
944 /* Send the new MIC error report immediately since we are going
945 * to start countermeasures and AP better do the same.
946 */
947 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
948
949 /* initialize countermeasures */
950 wpa_s->countermeasures = 1;
951 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
952
953 /*
954 * Need to wait for completion of request frame. We do not get
955 * any callback for the message completion, so just wait a
956 * short while and hope for the best. */
957 os_sleep(0, 10000);
958
959 wpa_drv_set_countermeasures(wpa_s, 1);
960 wpa_supplicant_deauthenticate(wpa_s,
961 WLAN_REASON_MICHAEL_MIC_FAILURE);
962 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
963 wpa_s, NULL);
964 eloop_register_timeout(60, 0,
965 wpa_supplicant_stop_countermeasures,
966 wpa_s, NULL);
967 /* TODO: mark the AP rejected for 60 second. STA is
968 * allowed to associate with another AP.. */
969 } else {
970 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
971 if (wpa_s->mic_errors_seen) {
972 /*
973 * Reduce the effectiveness of Michael MIC error
974 * reports as a means for attacking against TKIP if
975 * more than one MIC failure is noticed with the same
976 * PTK. We delay the transmission of the reports by a
977 * random time between 0 and 60 seconds in order to
978 * force the attacker wait 60 seconds before getting
979 * the information on whether a frame resulted in a MIC
980 * failure.
981 */
982 u8 rval[4];
983 int sec;
984
985 if (os_get_random(rval, sizeof(rval)) < 0)
986 sec = os_random() % 60;
987 else
988 sec = WPA_GET_BE32(rval) % 60;
989 wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
990 "seconds", sec);
991 wpa_s->pending_mic_error_report = 1;
992 wpa_s->pending_mic_error_pairwise = pairwise;
993 eloop_cancel_timeout(
994 wpa_supplicant_delayed_mic_error_report,
995 wpa_s, NULL);
996 eloop_register_timeout(
997 sec, os_random() % 1000000,
998 wpa_supplicant_delayed_mic_error_report,
999 wpa_s, NULL);
1000 } else {
1001 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1002 }
1003 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1004 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1005 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1006 }
1007 wpa_s->last_michael_mic_error = t.sec;
1008 wpa_s->mic_errors_seen++;
1009 }
1010
1011
1012 static void
1013 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1014 union wpa_event_data *data)
1015 {
1016 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1017 return;
1018
1019 switch (data->interface_status.ievent) {
1020 case EVENT_INTERFACE_ADDED:
1021 if (!wpa_s->interface_removed)
1022 break;
1023 wpa_s->interface_removed = 0;
1024 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1025 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1026 wpa_printf(MSG_INFO, "Failed to initialize the driver "
1027 "after interface was added.");
1028 }
1029 break;
1030 case EVENT_INTERFACE_REMOVED:
1031 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1032 wpa_s->interface_removed = 1;
1033 wpa_supplicant_mark_disassoc(wpa_s);
1034 l2_packet_deinit(wpa_s->l2);
1035 wpa_s->l2 = NULL;
1036 break;
1037 }
1038 }
1039
1040
1041 #ifdef CONFIG_PEERKEY
1042 static void
1043 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1044 union wpa_event_data *data)
1045 {
1046 if (data == NULL)
1047 return;
1048 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1049 }
1050 #endif /* CONFIG_PEERKEY */
1051
1052
1053 #ifdef CONFIG_IEEE80211R
1054 static void
1055 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1056 union wpa_event_data *data)
1057 {
1058 if (data == NULL)
1059 return;
1060
1061 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1062 data->ft_ies.ies_len,
1063 data->ft_ies.ft_action,
1064 data->ft_ies.target_ap) < 0) {
1065 /* TODO: prevent MLME/driver from trying to associate? */
1066 }
1067 }
1068 #endif /* CONFIG_IEEE80211R */
1069
1070
1071 #ifdef CONFIG_IBSS_RSN
1072 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1073 union wpa_event_data *data)
1074 {
1075 if (data == NULL)
1076 return;
1077 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1078 }
1079 #endif /* CONFIG_IBSS_RSN */
1080
1081
1082 void wpa_supplicant_event(void *ctx, wpa_event_type event,
1083 union wpa_event_data *data)
1084 {
1085 struct wpa_supplicant *wpa_s = ctx;
1086
1087 switch (event) {
1088 case EVENT_ASSOC:
1089 wpa_supplicant_event_assoc(wpa_s, data);
1090 break;
1091 case EVENT_DISASSOC:
1092 wpa_supplicant_event_disassoc(wpa_s);
1093 break;
1094 case EVENT_MICHAEL_MIC_FAILURE:
1095 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1096 break;
1097 #ifndef CONFIG_NO_SCAN_PROCESSING
1098 case EVENT_SCAN_RESULTS:
1099 wpa_supplicant_event_scan_results(wpa_s);
1100 break;
1101 #endif /* CONFIG_NO_SCAN_PROCESSING */
1102 case EVENT_ASSOCINFO:
1103 wpa_supplicant_event_associnfo(wpa_s, data);
1104 break;
1105 case EVENT_INTERFACE_STATUS:
1106 wpa_supplicant_event_interface_status(wpa_s, data);
1107 break;
1108 case EVENT_PMKID_CANDIDATE:
1109 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1110 break;
1111 #ifdef CONFIG_PEERKEY
1112 case EVENT_STKSTART:
1113 wpa_supplicant_event_stkstart(wpa_s, data);
1114 break;
1115 #endif /* CONFIG_PEERKEY */
1116 #ifdef CONFIG_IEEE80211R
1117 case EVENT_FT_RESPONSE:
1118 wpa_supplicant_event_ft_response(wpa_s, data);
1119 break;
1120 #endif /* CONFIG_IEEE80211R */
1121 #ifdef CONFIG_IBSS_RSN
1122 case EVENT_IBSS_RSN_START:
1123 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1124 break;
1125 #endif /* CONFIG_IBSS_RSN */
1126 default:
1127 wpa_printf(MSG_INFO, "Unknown event %d", event);
1128 break;
1129 }
1130 }