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