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