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