blob: 495f81d3b8a3b292312081282567325a660dc4e2 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
3 * Copyright (c) 2003-2011, 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 "p2p/p2p.h"
32#include "notify.h"
33#include "common/ieee802_11_defs.h"
34#include "common/ieee802_11_common.h"
35#include "crypto/random.h"
36#include "blacklist.h"
37#include "wpas_glue.h"
38#include "wps_supplicant.h"
39#include "ibss_rsn.h"
40#include "sme.h"
41#include "p2p_supplicant.h"
42#include "bgscan.h"
43#include "ap.h"
44#include "bss.h"
45#include "mlme.h"
46#include "scan.h"
47
48
49static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
50{
51 struct wpa_ssid *ssid, *old_ssid;
52
53 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
54 return 0;
55
56 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
57 "information");
58 ssid = wpa_supplicant_get_ssid(wpa_s);
59 if (ssid == NULL) {
60 wpa_msg(wpa_s, MSG_INFO,
61 "No network configuration found for the current AP");
62 return -1;
63 }
64
65 if (ssid->disabled) {
66 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
67 return -1;
68 }
69
70 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
71 "current AP");
72 if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
73 WPA_KEY_MGMT_WPA_NONE |
74 WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
75 WPA_KEY_MGMT_PSK_SHA256 |
76 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
77 u8 wpa_ie[80];
78 size_t wpa_ie_len = sizeof(wpa_ie);
79 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
80 wpa_ie, &wpa_ie_len);
81 } else {
82 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
83 }
84
85 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
86 eapol_sm_invalidate_cached_session(wpa_s->eapol);
87 old_ssid = wpa_s->current_ssid;
88 wpa_s->current_ssid = ssid;
89 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
90 wpa_supplicant_initiate_eapol(wpa_s);
91 if (old_ssid != wpa_s->current_ssid)
92 wpas_notify_network_changed(wpa_s);
93
94 return 0;
95}
96
97
98static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
99 void *sock_ctx)
100{
101 struct wpa_supplicant *wpa_s = eloop_ctx;
102
103 if (wpa_s->countermeasures) {
104 wpa_s->countermeasures = 0;
105 wpa_drv_set_countermeasures(wpa_s, 0);
106 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
107 wpa_supplicant_req_scan(wpa_s, 0, 0);
108 }
109}
110
111
112void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
113{
114 int bssid_changed;
115
116#ifdef CONFIG_IBSS_RSN
117 ibss_rsn_deinit(wpa_s->ibss_rsn);
118 wpa_s->ibss_rsn = NULL;
119#endif /* CONFIG_IBSS_RSN */
120
121 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
122 return;
123
124 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700125 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
127 os_memset(wpa_s->bssid, 0, ETH_ALEN);
128 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
129 wpa_s->current_bss = NULL;
130 wpa_s->assoc_freq = 0;
131 if (bssid_changed)
132 wpas_notify_bssid_changed(wpa_s);
133
134 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
135 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
136 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
137 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
138 wpa_s->ap_ies_from_associnfo = 0;
139}
140
141
142static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
143{
144 struct wpa_ie_data ie;
145 int pmksa_set = -1;
146 size_t i;
147
148 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
149 ie.pmkid == NULL)
150 return;
151
152 for (i = 0; i < ie.num_pmkid; i++) {
153 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
154 ie.pmkid + i * PMKID_LEN,
155 NULL, NULL, 0);
156 if (pmksa_set == 0) {
157 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
158 break;
159 }
160 }
161
162 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
163 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
164}
165
166
167static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
168 union wpa_event_data *data)
169{
170 if (data == NULL) {
171 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
172 "event");
173 return;
174 }
175 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
176 " index=%d preauth=%d",
177 MAC2STR(data->pmkid_candidate.bssid),
178 data->pmkid_candidate.index,
179 data->pmkid_candidate.preauth);
180
181 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
182 data->pmkid_candidate.index,
183 data->pmkid_candidate.preauth);
184}
185
186
187static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
188{
189 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
190 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
191 return 0;
192
193#ifdef IEEE8021X_EAPOL
194 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
195 wpa_s->current_ssid &&
196 !(wpa_s->current_ssid->eapol_flags &
197 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
198 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
199 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
200 * plaintext or static WEP keys). */
201 return 0;
202 }
203#endif /* IEEE8021X_EAPOL */
204
205 return 1;
206}
207
208
209/**
210 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
211 * @wpa_s: pointer to wpa_supplicant data
212 * @ssid: Configuration data for the network
213 * Returns: 0 on success, -1 on failure
214 *
215 * This function is called when starting authentication with a network that is
216 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
217 */
218int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
219 struct wpa_ssid *ssid)
220{
221#ifdef IEEE8021X_EAPOL
222 int aka = 0, sim = 0, type;
223
224 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
225 return 0;
226
227 if (ssid->eap.eap_methods == NULL) {
228 sim = 1;
229 aka = 1;
230 } else {
231 struct eap_method_type *eap = ssid->eap.eap_methods;
232 while (eap->vendor != EAP_VENDOR_IETF ||
233 eap->method != EAP_TYPE_NONE) {
234 if (eap->vendor == EAP_VENDOR_IETF) {
235 if (eap->method == EAP_TYPE_SIM)
236 sim = 1;
237 else if (eap->method == EAP_TYPE_AKA)
238 aka = 1;
239 }
240 eap++;
241 }
242 }
243
244 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
245 sim = 0;
246 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
247 aka = 0;
248
249 if (!sim && !aka) {
250 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
251 "use SIM, but neither EAP-SIM nor EAP-AKA are "
252 "enabled");
253 return 0;
254 }
255
256 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
257 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
258 if (sim && aka)
259 type = SCARD_TRY_BOTH;
260 else if (aka)
261 type = SCARD_USIM_ONLY;
262 else
263 type = SCARD_GSM_SIM_ONLY;
264
265 wpa_s->scard = scard_init(type);
266 if (wpa_s->scard == NULL) {
267 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
268 "(pcsc-lite)");
269 return -1;
270 }
271 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
272 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
273#endif /* IEEE8021X_EAPOL */
274
275 return 0;
276}
277
278
279#ifndef CONFIG_NO_SCAN_PROCESSING
280static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
281 struct wpa_ssid *ssid)
282{
283 int i, privacy = 0;
284
285 if (ssid->mixed_cell)
286 return 1;
287
288#ifdef CONFIG_WPS
289 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
290 return 1;
291#endif /* CONFIG_WPS */
292
293 for (i = 0; i < NUM_WEP_KEYS; i++) {
294 if (ssid->wep_key_len[i]) {
295 privacy = 1;
296 break;
297 }
298 }
299#ifdef IEEE8021X_EAPOL
300 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
301 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
302 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
303 privacy = 1;
304#endif /* IEEE8021X_EAPOL */
305
306 if (bss->caps & IEEE80211_CAP_PRIVACY)
307 return privacy;
308 return !privacy;
309}
310
311
312static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
313 struct wpa_ssid *ssid,
314 struct wpa_scan_res *bss)
315{
316 struct wpa_ie_data ie;
317 int proto_match = 0;
318 const u8 *rsn_ie, *wpa_ie;
319 int ret;
320 int wep_ok;
321
322 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
323 if (ret >= 0)
324 return ret;
325
326 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
327 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
328 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
329 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
330 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
331
332 rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
333 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
334 proto_match++;
335
336 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
337 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
338 "failed");
339 break;
340 }
341
342 if (wep_ok &&
343 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
344 {
345 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
346 "in RSN IE");
347 return 1;
348 }
349
350 if (!(ie.proto & ssid->proto)) {
351 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
352 "mismatch");
353 break;
354 }
355
356 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
357 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
358 "cipher mismatch");
359 break;
360 }
361
362 if (!(ie.group_cipher & ssid->group_cipher)) {
363 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
364 "cipher mismatch");
365 break;
366 }
367
368 if (!(ie.key_mgmt & ssid->key_mgmt)) {
369 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
370 "mismatch");
371 break;
372 }
373
374#ifdef CONFIG_IEEE80211W
375 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
376 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
377 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
378 "frame protection");
379 break;
380 }
381#endif /* CONFIG_IEEE80211W */
382
383 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
384 return 1;
385 }
386
387 wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
388 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
389 proto_match++;
390
391 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
392 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
393 "failed");
394 break;
395 }
396
397 if (wep_ok &&
398 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
399 {
400 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
401 "in WPA IE");
402 return 1;
403 }
404
405 if (!(ie.proto & ssid->proto)) {
406 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
407 "mismatch");
408 break;
409 }
410
411 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
412 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
413 "cipher mismatch");
414 break;
415 }
416
417 if (!(ie.group_cipher & ssid->group_cipher)) {
418 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
419 "cipher mismatch");
420 break;
421 }
422
423 if (!(ie.key_mgmt & ssid->key_mgmt)) {
424 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
425 "mismatch");
426 break;
427 }
428
429 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
430 return 1;
431 }
432
433 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
434 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
435 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
436 return 0;
437 }
438
439 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
440 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
441 return 1;
442 }
443
444 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
445 "WPA/WPA2");
446
447 return 0;
448}
449
450
451static int freq_allowed(int *freqs, int freq)
452{
453 int i;
454
455 if (freqs == NULL)
456 return 1;
457
458 for (i = 0; freqs[i]; i++)
459 if (freqs[i] == freq)
460 return 1;
461 return 0;
462}
463
464
465static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
466 int i, struct wpa_scan_res *bss,
467 struct wpa_ssid *group)
468{
469 const u8 *ssid_;
470 u8 wpa_ie_len, rsn_ie_len, ssid_len;
471 int wpa;
472 struct wpa_blacklist *e;
473 const u8 *ie;
474 struct wpa_ssid *ssid;
475
476 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
477 ssid_ = ie ? ie + 2 : (u8 *) "";
478 ssid_len = ie ? ie[1] : 0;
479
480 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
481 wpa_ie_len = ie ? ie[1] : 0;
482
483 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
484 rsn_ie_len = ie ? ie[1] : 0;
485
486 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
487 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
488 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
489 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
490 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
491
492 e = wpa_blacklist_get(wpa_s, bss->bssid);
493 if (e) {
494 int limit = 1;
495 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
496 /*
497 * When only a single network is enabled, we can
498 * trigger blacklisting on the first failure. This
499 * should not be done with multiple enabled networks to
500 * avoid getting forced to move into a worse ESS on
501 * single error if there are no other BSSes of the
502 * current ESS.
503 */
504 limit = 0;
505 }
506 if (e->count > limit) {
507 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
508 "(count=%d limit=%d)", e->count, limit);
509 return NULL;
510 }
511 }
512
513 if (ssid_len == 0) {
514 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
515 return NULL;
516 }
517
518 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
519
520 for (ssid = group; ssid; ssid = ssid->pnext) {
521 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
522
523 if (ssid->disabled) {
524 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
525 continue;
526 }
527
528#ifdef CONFIG_WPS
529 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
530 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
531 "(WPS)");
532 continue;
533 }
534
535 if (wpa && ssid->ssid_len == 0 &&
536 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
537 check_ssid = 0;
538
539 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
540 /* Only allow wildcard SSID match if an AP
541 * advertises active WPS operation that matches
542 * with our mode. */
543 check_ssid = 1;
544 if (ssid->ssid_len == 0 &&
545 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
546 check_ssid = 0;
547 }
548#endif /* CONFIG_WPS */
549
550 if (check_ssid &&
551 (ssid_len != ssid->ssid_len ||
552 os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
553 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
554 continue;
555 }
556
557 if (ssid->bssid_set &&
558 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
559 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
560 continue;
561 }
562
563 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
564 continue;
565
566 if (!wpa &&
567 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
568 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
569 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
570 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
571 "not allowed");
572 continue;
573 }
574
575 if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
576 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
577 "mismatch");
578 continue;
579 }
580
581 if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
582 wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
583 "network");
584 continue;
585 }
586
587 if (!freq_allowed(ssid->freq_list, bss->freq)) {
588 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
589 "allowed");
590 continue;
591 }
592
593#ifdef CONFIG_P2P
594 /*
595 * TODO: skip the AP if its P2P IE has Group Formation
596 * bit set in the P2P Group Capability Bitmap and we
597 * are not in Group Formation with that device.
598 */
599#endif /* CONFIG_P2P */
600
601 /* Matching configuration found */
602 return ssid;
603 }
604
605 /* No matching configuration found */
606 return NULL;
607}
608
609
610static struct wpa_bss *
611wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
612 struct wpa_scan_results *scan_res,
613 struct wpa_ssid *group,
614 struct wpa_ssid **selected_ssid)
615{
616 size_t i;
617
618 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
619 group->priority);
620
621 for (i = 0; i < scan_res->num; i++) {
622 struct wpa_scan_res *bss = scan_res->res[i];
623 const u8 *ie, *ssid;
624 u8 ssid_len;
625
626 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
627 if (!*selected_ssid)
628 continue;
629
630 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
631 ssid = ie ? ie + 2 : (u8 *) "";
632 ssid_len = ie ? ie[1] : 0;
633
634 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
635 " ssid='%s'",
636 MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
637 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
638 }
639
640 return NULL;
641}
642
643
644static struct wpa_bss *
645wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
646 struct wpa_scan_results *scan_res,
647 struct wpa_ssid **selected_ssid)
648{
649 struct wpa_bss *selected = NULL;
650 int prio;
651
652 while (selected == NULL) {
653 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
654 selected = wpa_supplicant_select_bss(
655 wpa_s, scan_res, wpa_s->conf->pssid[prio],
656 selected_ssid);
657 if (selected)
658 break;
659 }
660
661 if (selected == NULL && wpa_s->blacklist) {
662 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
663 "blacklist and try again");
664 wpa_blacklist_clear(wpa_s);
665 wpa_s->blacklist_cleared++;
666 } else if (selected == NULL)
667 break;
668 }
669
670 return selected;
671}
672
673
674static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
675 int timeout_sec, int timeout_usec)
676{
677 if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
678 /*
679 * No networks are enabled; short-circuit request so
680 * we don't wait timeout seconds before transitioning
681 * to INACTIVE state.
682 */
683 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
684 return;
685 }
686 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
687}
688
689
690void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
691 struct wpa_bss *selected,
692 struct wpa_ssid *ssid)
693{
694 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
695 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
696 "PBC session overlap");
697#ifdef CONFIG_P2P
698 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
699 return;
700#endif /* CONFIG_P2P */
701
702#ifdef CONFIG_WPS
703 wpas_wps_cancel(wpa_s);
704#endif /* CONFIG_WPS */
705 return;
706 }
707
708 /*
709 * Do not trigger new association unless the BSSID has changed or if
710 * reassociation is requested. If we are in process of associating with
711 * the selected BSSID, do not trigger new attempt.
712 */
713 if (wpa_s->reassociate ||
714 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
715 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
716 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
717 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
718 0))) {
719 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
720 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
721 return;
722 }
723 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
724 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
725 " pending: " MACSTR " wpa_state: %s",
726 wpa_s->reassociate, MAC2STR(selected->bssid),
727 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
728 wpa_supplicant_state_txt(wpa_s->wpa_state));
729 wpa_supplicant_associate(wpa_s, selected, ssid);
730 } else {
731 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
732 "selected AP");
733 }
734}
735
736
737static struct wpa_ssid *
738wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
739{
740 int prio;
741 struct wpa_ssid *ssid;
742
743 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
744 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
745 {
746 if (ssid->disabled)
747 continue;
748 if (ssid->mode == IEEE80211_MODE_IBSS ||
749 ssid->mode == IEEE80211_MODE_AP)
750 return ssid;
751 }
752 }
753 return NULL;
754}
755
756
757/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
758 * on BSS added and BSS changed events */
759static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300760 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300762 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763
764 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
765 return;
766
Jouni Malinen87fd2792011-05-16 18:35:42 +0300767 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769
Jouni Malinen87fd2792011-05-16 18:35:42 +0300770 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 if (ssid == NULL)
772 continue;
773
Jouni Malinen87fd2792011-05-16 18:35:42 +0300774 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 if (rsn == NULL)
776 continue;
777
Jouni Malinen87fd2792011-05-16 18:35:42 +0300778 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 }
780
781}
782
783
784static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
785 struct wpa_bss *selected,
786 struct wpa_ssid *ssid,
787 struct wpa_scan_results *scan_res)
788{
789 size_t i;
790 struct wpa_scan_res *current_bss = NULL;
791 int min_diff;
792
793 if (wpa_s->reassociate)
794 return 1; /* explicit request to reassociate */
795 if (wpa_s->wpa_state < WPA_ASSOCIATED)
796 return 1; /* we are not associated; continue */
797 if (wpa_s->current_ssid == NULL)
798 return 1; /* unknown current SSID */
799 if (wpa_s->current_ssid != ssid)
800 return 1; /* different network block */
801
802 for (i = 0; i < scan_res->num; i++) {
803 struct wpa_scan_res *res = scan_res->res[i];
804 const u8 *ie;
805 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
806 continue;
807
808 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
809 if (ie == NULL)
810 continue;
811 if (ie[1] != wpa_s->current_ssid->ssid_len ||
812 os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
813 continue;
814 current_bss = res;
815 break;
816 }
817
818 if (!current_bss)
819 return 1; /* current BSS not seen in scan results */
820
821 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
822 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
823 MAC2STR(current_bss->bssid), current_bss->level);
824 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
825 MAC2STR(selected->bssid), selected->level);
826
827 if (wpa_s->current_ssid->bssid_set &&
828 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
829 0) {
830 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
831 "has preferred BSSID");
832 return 1;
833 }
834
835 min_diff = 2;
836 if (current_bss->level < 0) {
837 if (current_bss->level < -85)
838 min_diff = 1;
839 else if (current_bss->level < -80)
840 min_diff = 2;
841 else if (current_bss->level < -75)
842 min_diff = 3;
843 else if (current_bss->level < -70)
844 min_diff = 4;
845 else
846 min_diff = 5;
847 }
848 if (abs(current_bss->level - selected->level) < min_diff) {
849 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
850 "in signal level");
851 return 0;
852 }
853
854 return 1;
855}
856
857/* Return < 0 if no scan results could be fetched. */
858static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
859 union wpa_event_data *data)
860{
861 struct wpa_bss *selected;
862 struct wpa_ssid *ssid = NULL;
863 struct wpa_scan_results *scan_res;
864 int ap = 0;
865
866#ifdef CONFIG_AP
867 if (wpa_s->ap_iface)
868 ap = 1;
869#endif /* CONFIG_AP */
870
871 wpa_supplicant_notify_scanning(wpa_s, 0);
872
873 scan_res = wpa_supplicant_get_scan_results(wpa_s,
874 data ? &data->scan_info :
875 NULL, 1);
876 if (scan_res == NULL) {
877 if (wpa_s->conf->ap_scan == 2 || ap)
878 return -1;
879 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
880 "scanning again");
881 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
882 return -1;
883 }
884
885#ifndef CONFIG_NO_RANDOM_POOL
886 size_t i, num;
887 num = scan_res->num;
888 if (num > 10)
889 num = 10;
890 for (i = 0; i < num; i++) {
891 u8 buf[5];
892 struct wpa_scan_res *res = scan_res->res[i];
893 buf[0] = res->bssid[5];
894 buf[1] = res->qual & 0xff;
895 buf[2] = res->noise & 0xff;
896 buf[3] = res->level & 0xff;
897 buf[4] = res->tsf & 0xff;
898 random_add_randomness(buf, sizeof(buf));
899 }
900#endif /* CONFIG_NO_RANDOM_POOL */
901
902 if (wpa_s->scan_res_handler) {
903 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
904 struct wpa_scan_results *scan_res);
905
906 scan_res_handler = wpa_s->scan_res_handler;
907 wpa_s->scan_res_handler = NULL;
908 scan_res_handler(wpa_s, scan_res);
909
910 wpa_scan_results_free(scan_res);
911 return 0;
912 }
913
914 if (ap) {
915 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
916#ifdef CONFIG_AP
917 if (wpa_s->ap_iface->scan_cb)
918 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
919#endif /* CONFIG_AP */
920 wpa_scan_results_free(scan_res);
921 return 0;
922 }
923
924 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
925 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
926 wpas_notify_scan_results(wpa_s);
927
928 wpas_notify_scan_done(wpa_s, 1);
929
930 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
931 wpa_scan_results_free(scan_res);
932 return 0;
933 }
934
935 if (wpa_s->disconnected) {
936 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
937 wpa_scan_results_free(scan_res);
938 return 0;
939 }
940
941 if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
942 wpa_scan_results_free(scan_res);
943 return 0;
944 }
945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
947
948 if (selected) {
949 int skip;
950 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
951 scan_res);
952 wpa_scan_results_free(scan_res);
953 if (skip)
954 return 0;
955 wpa_supplicant_connect(wpa_s, selected, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300956 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 } else {
958 wpa_scan_results_free(scan_res);
959 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
960 ssid = wpa_supplicant_pick_new_network(wpa_s);
961 if (ssid) {
962 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
963 wpa_supplicant_associate(wpa_s, NULL, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +0300964 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 } else {
966 int timeout_sec = wpa_s->scan_interval;
967 int timeout_usec = 0;
968#ifdef CONFIG_P2P
969 if (wpa_s->p2p_in_provisioning) {
970 /*
971 * Use shorter wait during P2P Provisioning
972 * state to speed up group formation.
973 */
974 timeout_sec = 0;
975 timeout_usec = 250000;
976 }
977#endif /* CONFIG_P2P */
978 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
979 timeout_usec);
980 }
981 }
982 return 0;
983}
984
985
986static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
987 union wpa_event_data *data)
988{
989 const char *rn, *rn2;
990 struct wpa_supplicant *ifs;
991
992 if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
993 /*
994 * If no scan results could be fetched, then no need to
995 * notify those interfaces that did not actually request
996 * this scan.
997 */
998 return;
999 }
1000
1001 /*
1002 * Check other interfaces to see if they have the same radio-name. If
1003 * so, they get updated with this same scan info.
1004 */
1005 if (!wpa_s->driver->get_radio_name)
1006 return;
1007
1008 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1009 if (rn == NULL || rn[0] == '\0')
1010 return;
1011
1012 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1013 "sharing same radio (%s) in event_scan_results", rn);
1014
1015 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1016 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1017 continue;
1018
1019 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1020 if (rn2 && os_strcmp(rn, rn2) == 0) {
1021 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1022 "sibling", ifs->ifname);
1023 _wpa_supplicant_event_scan_results(ifs, data);
1024 }
1025 }
1026}
1027
1028#endif /* CONFIG_NO_SCAN_PROCESSING */
1029
1030
1031static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1032 union wpa_event_data *data)
1033{
1034 int l, len, found = 0, wpa_found, rsn_found;
1035 const u8 *p;
1036
1037 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1038 if (data->assoc_info.req_ies)
1039 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1040 data->assoc_info.req_ies_len);
1041 if (data->assoc_info.resp_ies) {
1042 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1043 data->assoc_info.resp_ies_len);
1044#ifdef CONFIG_TDLS
1045 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1046 data->assoc_info.resp_ies_len);
1047#endif /* CONFIG_TDLS */
1048 }
1049 if (data->assoc_info.beacon_ies)
1050 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1051 data->assoc_info.beacon_ies,
1052 data->assoc_info.beacon_ies_len);
1053 if (data->assoc_info.freq)
1054 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1055 data->assoc_info.freq);
1056
1057 p = data->assoc_info.req_ies;
1058 l = data->assoc_info.req_ies_len;
1059
1060 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1061 while (p && l >= 2) {
1062 len = p[1] + 2;
1063 if (len > l) {
1064 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1065 p, l);
1066 break;
1067 }
1068 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1069 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1070 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1071 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1072 break;
1073 found = 1;
1074 wpa_find_assoc_pmkid(wpa_s);
1075 break;
1076 }
1077 l -= len;
1078 p += len;
1079 }
1080 if (!found && data->assoc_info.req_ies)
1081 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1082
1083#ifdef CONFIG_IEEE80211R
1084#ifdef CONFIG_SME
1085 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1086 u8 bssid[ETH_ALEN];
1087 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1088 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1089 data->assoc_info.resp_ies,
1090 data->assoc_info.resp_ies_len,
1091 bssid) < 0) {
1092 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1093 "Reassociation Response failed");
1094 wpa_supplicant_deauthenticate(
1095 wpa_s, WLAN_REASON_INVALID_IE);
1096 return -1;
1097 }
1098 }
1099
1100 p = data->assoc_info.resp_ies;
1101 l = data->assoc_info.resp_ies_len;
1102
1103#ifdef CONFIG_WPS_STRICT
1104 if (p && wpa_s->current_ssid &&
1105 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1106 struct wpabuf *wps;
1107 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1108 if (wps == NULL) {
1109 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1110 "include WPS IE in (Re)Association Response");
1111 return -1;
1112 }
1113
1114 if (wps_validate_assoc_resp(wps) < 0) {
1115 wpabuf_free(wps);
1116 wpa_supplicant_deauthenticate(
1117 wpa_s, WLAN_REASON_INVALID_IE);
1118 return -1;
1119 }
1120 wpabuf_free(wps);
1121 }
1122#endif /* CONFIG_WPS_STRICT */
1123
1124 /* Go through the IEs and make a copy of the MDIE, if present. */
1125 while (p && l >= 2) {
1126 len = p[1] + 2;
1127 if (len > l) {
1128 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1129 p, l);
1130 break;
1131 }
1132 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1133 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1134 wpa_s->sme.ft_used = 1;
1135 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1136 MOBILITY_DOMAIN_ID_LEN);
1137 break;
1138 }
1139 l -= len;
1140 p += len;
1141 }
1142#endif /* CONFIG_SME */
1143
1144 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1145 data->assoc_info.resp_ies_len);
1146#endif /* CONFIG_IEEE80211R */
1147
1148 /* WPA/RSN IE from Beacon/ProbeResp */
1149 p = data->assoc_info.beacon_ies;
1150 l = data->assoc_info.beacon_ies_len;
1151
1152 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1153 */
1154 wpa_found = rsn_found = 0;
1155 while (p && l >= 2) {
1156 len = p[1] + 2;
1157 if (len > l) {
1158 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1159 p, l);
1160 break;
1161 }
1162 if (!wpa_found &&
1163 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1164 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1165 wpa_found = 1;
1166 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1167 }
1168
1169 if (!rsn_found &&
1170 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1171 rsn_found = 1;
1172 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1173 }
1174
1175 l -= len;
1176 p += len;
1177 }
1178
1179 if (!wpa_found && data->assoc_info.beacon_ies)
1180 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1181 if (!rsn_found && data->assoc_info.beacon_ies)
1182 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1183 if (wpa_found || rsn_found)
1184 wpa_s->ap_ies_from_associnfo = 1;
1185
Jouni Malinen87fd2792011-05-16 18:35:42 +03001186 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1187 wpa_s->assoc_freq != data->assoc_info.freq) {
1188 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1189 "%u to %u MHz",
1190 wpa_s->assoc_freq, data->assoc_info.freq);
1191 wpa_supplicant_update_scan_results(wpa_s);
1192 }
1193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 wpa_s->assoc_freq = data->assoc_info.freq;
1195
1196 return 0;
1197}
1198
1199
1200static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1201 union wpa_event_data *data)
1202{
1203 u8 bssid[ETH_ALEN];
1204 int ft_completed;
1205 int bssid_changed;
1206 struct wpa_driver_capa capa;
1207
1208#ifdef CONFIG_AP
1209 if (wpa_s->ap_iface) {
1210 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1211 data->assoc_info.addr,
1212 data->assoc_info.req_ies,
1213 data->assoc_info.req_ies_len,
1214 data->assoc_info.reassoc);
1215 return;
1216 }
1217#endif /* CONFIG_AP */
1218
1219 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1220 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1221 return;
1222
1223 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1224 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1225 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1226 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1227 (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1228 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1229 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1230 MACSTR, MAC2STR(bssid));
1231 random_add_randomness(bssid, ETH_ALEN);
1232 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1233 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1234 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1235 if (bssid_changed)
1236 wpas_notify_bssid_changed(wpa_s);
1237
1238 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1239 wpa_clear_keys(wpa_s, bssid);
1240 }
1241 if (wpa_supplicant_select_config(wpa_s) < 0) {
1242 wpa_supplicant_disassociate(
1243 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1244 return;
1245 }
1246 if (wpa_s->current_ssid) {
1247 struct wpa_bss *bss = NULL;
1248 struct wpa_ssid *ssid = wpa_s->current_ssid;
1249 if (ssid->ssid_len > 0)
1250 bss = wpa_bss_get(wpa_s, bssid,
1251 ssid->ssid, ssid->ssid_len);
1252 if (!bss)
1253 bss = wpa_bss_get_bssid(wpa_s, bssid);
1254 if (bss)
1255 wpa_s->current_bss = bss;
1256 }
1257 }
1258
1259#ifdef CONFIG_SME
1260 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1261 wpa_s->sme.prev_bssid_set = 1;
1262#endif /* CONFIG_SME */
1263
1264 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1265 if (wpa_s->current_ssid) {
1266 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1267 * initialized before association, but for other modes,
1268 * initialize PC/SC here, if the current configuration needs
1269 * smartcard or SIM/USIM. */
1270 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1271 }
1272 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1273 if (wpa_s->l2)
1274 l2_packet_notify_auth_start(wpa_s->l2);
1275
1276 /*
1277 * Set portEnabled first to FALSE in order to get EAP state machine out
1278 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1279 * state machine may transit to AUTHENTICATING state based on obsolete
1280 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1281 * AUTHENTICATED without ever giving chance to EAP state machine to
1282 * reset the state.
1283 */
1284 if (!ft_completed) {
1285 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1286 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1287 }
1288 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1289 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1290 /* 802.1X::portControl = Auto */
1291 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1292 wpa_s->eapol_received = 0;
1293 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1294 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1295 (wpa_s->current_ssid &&
1296 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1297 wpa_supplicant_cancel_auth_timeout(wpa_s);
1298 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1299 } else if (!ft_completed) {
1300 /* Timeout for receiving the first EAPOL packet */
1301 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1302 }
1303 wpa_supplicant_cancel_scan(wpa_s);
1304
1305 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1306 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1307 /*
1308 * We are done; the driver will take care of RSN 4-way
1309 * handshake.
1310 */
1311 wpa_supplicant_cancel_auth_timeout(wpa_s);
1312 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1313 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1314 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1315 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1316 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1317 /*
1318 * The driver will take care of RSN 4-way handshake, so we need
1319 * to allow EAPOL supplicant to complete its work without
1320 * waiting for WPA supplicant.
1321 */
1322 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1323 } else if (ft_completed) {
1324 /*
1325 * FT protocol completed - make sure EAPOL state machine ends
1326 * up in authenticated.
1327 */
1328 wpa_supplicant_cancel_auth_timeout(wpa_s);
1329 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1330 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1331 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1332 }
1333
1334 if (wpa_s->pending_eapol_rx) {
1335 struct os_time now, age;
1336 os_get_time(&now);
1337 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1338 if (age.sec == 0 && age.usec < 100000 &&
1339 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1340 0) {
1341 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1342 "frame that was received just before "
1343 "association notification");
1344 wpa_supplicant_rx_eapol(
1345 wpa_s, wpa_s->pending_eapol_rx_src,
1346 wpabuf_head(wpa_s->pending_eapol_rx),
1347 wpabuf_len(wpa_s->pending_eapol_rx));
1348 }
1349 wpabuf_free(wpa_s->pending_eapol_rx);
1350 wpa_s->pending_eapol_rx = NULL;
1351 }
1352
1353 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1354 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1355 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1356 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1357 /* Set static WEP keys again */
1358 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1359 }
1360
1361#ifdef CONFIG_IBSS_RSN
1362 if (wpa_s->current_ssid &&
1363 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1364 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1365 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1366 wpa_s->ibss_rsn == NULL) {
1367 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1368 if (!wpa_s->ibss_rsn) {
1369 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1370 wpa_supplicant_deauthenticate(
1371 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1372 return;
1373 }
1374
1375 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1376 }
1377#endif /* CONFIG_IBSS_RSN */
1378}
1379
1380
1381static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1382 u16 reason_code)
1383{
1384 const u8 *bssid;
1385 int authenticating;
1386 u8 prev_pending_bssid[ETH_ALEN];
1387
1388 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1389 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1390
1391 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1392 /*
1393 * At least Host AP driver and a Prism3 card seemed to be
1394 * generating streams of disconnected events when configuring
1395 * IBSS for WPA-None. Ignore them for now.
1396 */
1397 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1398 "IBSS/WPA-None mode");
1399 return;
1400 }
1401
1402 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1403 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1404 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1405 "pre-shared key may be incorrect");
1406 }
1407 if (!wpa_s->auto_reconnect_disabled ||
1408 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1409 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
1410 "reconnect (wps=%d)",
1411 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
1412 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001413 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 } else {
1415 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
1416 "try to re-connect");
1417 wpa_s->reassociate = 0;
1418 wpa_s->disconnected = 1;
1419 }
1420 bssid = wpa_s->bssid;
1421 if (is_zero_ether_addr(bssid))
1422 bssid = wpa_s->pending_bssid;
1423 wpas_connection_failed(wpa_s, bssid);
1424 wpa_sm_notify_disassoc(wpa_s->wpa);
1425 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1426 " reason=%d",
1427 MAC2STR(bssid), reason_code);
1428 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1429 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1430 wpa_s->keys_cleared = 0;
1431 wpa_clear_keys(wpa_s, wpa_s->bssid);
1432 }
1433 wpa_supplicant_mark_disassoc(wpa_s);
1434
1435 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1436 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1437}
1438
1439
1440#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1441static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1442 void *sock_ctx)
1443{
1444 struct wpa_supplicant *wpa_s = eloop_ctx;
1445
1446 if (!wpa_s->pending_mic_error_report)
1447 return;
1448
1449 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1450 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1451 wpa_s->pending_mic_error_report = 0;
1452}
1453#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1454
1455
1456static void
1457wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1458 union wpa_event_data *data)
1459{
1460 int pairwise;
1461 struct os_time t;
1462
1463 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1464 pairwise = (data && data->michael_mic_failure.unicast);
1465 os_get_time(&t);
1466 if ((wpa_s->last_michael_mic_error &&
1467 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1468 wpa_s->pending_mic_error_report) {
1469 if (wpa_s->pending_mic_error_report) {
1470 /*
1471 * Send the pending MIC error report immediately since
1472 * we are going to start countermeasures and AP better
1473 * do the same.
1474 */
1475 wpa_sm_key_request(wpa_s->wpa, 1,
1476 wpa_s->pending_mic_error_pairwise);
1477 }
1478
1479 /* Send the new MIC error report immediately since we are going
1480 * to start countermeasures and AP better do the same.
1481 */
1482 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1483
1484 /* initialize countermeasures */
1485 wpa_s->countermeasures = 1;
1486 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1487
1488 /*
1489 * Need to wait for completion of request frame. We do not get
1490 * any callback for the message completion, so just wait a
1491 * short while and hope for the best. */
1492 os_sleep(0, 10000);
1493
1494 wpa_drv_set_countermeasures(wpa_s, 1);
1495 wpa_supplicant_deauthenticate(wpa_s,
1496 WLAN_REASON_MICHAEL_MIC_FAILURE);
1497 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1498 wpa_s, NULL);
1499 eloop_register_timeout(60, 0,
1500 wpa_supplicant_stop_countermeasures,
1501 wpa_s, NULL);
1502 /* TODO: mark the AP rejected for 60 second. STA is
1503 * allowed to associate with another AP.. */
1504 } else {
1505#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1506 if (wpa_s->mic_errors_seen) {
1507 /*
1508 * Reduce the effectiveness of Michael MIC error
1509 * reports as a means for attacking against TKIP if
1510 * more than one MIC failure is noticed with the same
1511 * PTK. We delay the transmission of the reports by a
1512 * random time between 0 and 60 seconds in order to
1513 * force the attacker wait 60 seconds before getting
1514 * the information on whether a frame resulted in a MIC
1515 * failure.
1516 */
1517 u8 rval[4];
1518 int sec;
1519
1520 if (os_get_random(rval, sizeof(rval)) < 0)
1521 sec = os_random() % 60;
1522 else
1523 sec = WPA_GET_BE32(rval) % 60;
1524 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1525 "report %d seconds", sec);
1526 wpa_s->pending_mic_error_report = 1;
1527 wpa_s->pending_mic_error_pairwise = pairwise;
1528 eloop_cancel_timeout(
1529 wpa_supplicant_delayed_mic_error_report,
1530 wpa_s, NULL);
1531 eloop_register_timeout(
1532 sec, os_random() % 1000000,
1533 wpa_supplicant_delayed_mic_error_report,
1534 wpa_s, NULL);
1535 } else {
1536 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1537 }
1538#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1539 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1540#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1541 }
1542 wpa_s->last_michael_mic_error = t.sec;
1543 wpa_s->mic_errors_seen++;
1544}
1545
1546
1547#ifdef CONFIG_TERMINATE_ONLASTIF
1548static int any_interfaces(struct wpa_supplicant *head)
1549{
1550 struct wpa_supplicant *wpa_s;
1551
1552 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1553 if (!wpa_s->interface_removed)
1554 return 1;
1555 return 0;
1556}
1557#endif /* CONFIG_TERMINATE_ONLASTIF */
1558
1559
1560static void
1561wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1562 union wpa_event_data *data)
1563{
1564 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1565 return;
1566
1567 switch (data->interface_status.ievent) {
1568 case EVENT_INTERFACE_ADDED:
1569 if (!wpa_s->interface_removed)
1570 break;
1571 wpa_s->interface_removed = 0;
1572 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1573 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1574 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1575 "driver after interface was added");
1576 }
1577 break;
1578 case EVENT_INTERFACE_REMOVED:
1579 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1580 wpa_s->interface_removed = 1;
1581 wpa_supplicant_mark_disassoc(wpa_s);
1582 l2_packet_deinit(wpa_s->l2);
1583 wpa_s->l2 = NULL;
1584#ifdef CONFIG_IBSS_RSN
1585 ibss_rsn_deinit(wpa_s->ibss_rsn);
1586 wpa_s->ibss_rsn = NULL;
1587#endif /* CONFIG_IBSS_RSN */
1588#ifdef CONFIG_TERMINATE_ONLASTIF
1589 /* check if last interface */
1590 if (!any_interfaces(wpa_s->global->ifaces))
1591 eloop_terminate();
1592#endif /* CONFIG_TERMINATE_ONLASTIF */
1593 break;
1594 }
1595}
1596
1597
1598#ifdef CONFIG_PEERKEY
1599static void
1600wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1601 union wpa_event_data *data)
1602{
1603 if (data == NULL)
1604 return;
1605 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1606}
1607#endif /* CONFIG_PEERKEY */
1608
1609
1610#ifdef CONFIG_TDLS
1611static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1612 union wpa_event_data *data)
1613{
1614 if (data == NULL)
1615 return;
1616 switch (data->tdls.oper) {
1617 case TDLS_REQUEST_SETUP:
1618 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1619 break;
1620 case TDLS_REQUEST_TEARDOWN:
1621 /* request from driver to add FTIE */
1622 wpa_tdls_recv_teardown_notify(wpa_s->wpa, data->tdls.peer,
1623 data->tdls.reason_code);
1624 break;
1625 }
1626}
1627#endif /* CONFIG_TDLS */
1628
1629
1630#ifdef CONFIG_IEEE80211R
1631static void
1632wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1633 union wpa_event_data *data)
1634{
1635 if (data == NULL)
1636 return;
1637
1638 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1639 data->ft_ies.ies_len,
1640 data->ft_ies.ft_action,
1641 data->ft_ies.target_ap,
1642 data->ft_ies.ric_ies,
1643 data->ft_ies.ric_ies_len) < 0) {
1644 /* TODO: prevent MLME/driver from trying to associate? */
1645 }
1646}
1647#endif /* CONFIG_IEEE80211R */
1648
1649
1650#ifdef CONFIG_IBSS_RSN
1651static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1652 union wpa_event_data *data)
1653{
1654 struct wpa_ssid *ssid;
1655 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1656 return;
1657 if (data == NULL)
1658 return;
1659 ssid = wpa_s->current_ssid;
1660 if (ssid == NULL)
1661 return;
1662 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1663 return;
1664
1665 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1666}
1667#endif /* CONFIG_IBSS_RSN */
1668
1669
1670#ifdef CONFIG_IEEE80211R
1671static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1672 size_t len)
1673{
1674 const u8 *sta_addr, *target_ap_addr;
1675 u16 status;
1676
1677 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1678 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1679 return; /* only SME case supported for now */
1680 if (len < 1 + 2 * ETH_ALEN + 2)
1681 return;
1682 if (data[0] != 2)
1683 return; /* Only FT Action Response is supported for now */
1684 sta_addr = data + 1;
1685 target_ap_addr = data + 1 + ETH_ALEN;
1686 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1687 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1688 MACSTR " TargetAP " MACSTR " status %u",
1689 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1690
1691 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1692 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1693 " in FT Action Response", MAC2STR(sta_addr));
1694 return;
1695 }
1696
1697 if (status) {
1698 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1699 "failure (status code %d)", status);
1700 /* TODO: report error to FT code(?) */
1701 return;
1702 }
1703
1704 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1705 len - (1 + 2 * ETH_ALEN + 2), 1,
1706 target_ap_addr, NULL, 0) < 0)
1707 return;
1708
1709#ifdef CONFIG_SME
1710 {
1711 struct wpa_bss *bss;
1712 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1713 if (bss)
1714 wpa_s->sme.freq = bss->freq;
1715 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1716 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1717 WLAN_AUTH_FT);
1718 }
1719#endif /* CONFIG_SME */
1720}
1721#endif /* CONFIG_IEEE80211R */
1722
1723
1724static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1725 struct unprot_deauth *e)
1726{
1727#ifdef CONFIG_IEEE80211W
1728 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1729 "dropped: " MACSTR " -> " MACSTR
1730 " (reason code %u)",
1731 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1732 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1733#endif /* CONFIG_IEEE80211W */
1734}
1735
1736
1737static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1738 struct unprot_disassoc *e)
1739{
1740#ifdef CONFIG_IEEE80211W
1741 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1742 "dropped: " MACSTR " -> " MACSTR
1743 " (reason code %u)",
1744 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1745 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1746#endif /* CONFIG_IEEE80211W */
1747}
1748
1749
1750void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1751 union wpa_event_data *data)
1752{
1753 struct wpa_supplicant *wpa_s = ctx;
1754 u16 reason_code = 0;
1755
1756 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1757 event != EVENT_INTERFACE_ENABLED &&
1758 event != EVENT_INTERFACE_STATUS) {
1759 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore event %d while interface is "
1760 "disabled", event);
1761 return;
1762 }
1763
1764 wpa_dbg(wpa_s, MSG_DEBUG, "Event %d received on interface %s",
1765 event, wpa_s->ifname);
1766
1767 switch (event) {
1768 case EVENT_AUTH:
1769 sme_event_auth(wpa_s, data);
1770 break;
1771 case EVENT_ASSOC:
1772 wpa_supplicant_event_assoc(wpa_s, data);
1773 break;
1774 case EVENT_DISASSOC:
1775 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
1776 if (data) {
1777 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1778 data->disassoc_info.reason_code);
1779 if (data->disassoc_info.addr)
1780 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
1781 MAC2STR(data->disassoc_info.addr));
1782 }
1783#ifdef CONFIG_AP
1784 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1785 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1786 data->disassoc_info.addr);
1787 break;
1788 }
1789#endif /* CONFIG_AP */
1790 if (data) {
1791 reason_code = data->disassoc_info.reason_code;
1792 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1793 data->disassoc_info.ie,
1794 data->disassoc_info.ie_len);
1795#ifdef CONFIG_P2P
1796 wpas_p2p_disassoc_notif(
1797 wpa_s, data->disassoc_info.addr, reason_code,
1798 data->disassoc_info.ie,
1799 data->disassoc_info.ie_len);
1800#endif /* CONFIG_P2P */
1801 }
1802 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1803 sme_event_disassoc(wpa_s, data);
1804 /* fall through */
1805 case EVENT_DEAUTH:
1806 if (event == EVENT_DEAUTH) {
1807 wpa_dbg(wpa_s, MSG_DEBUG,
1808 "Deauthentication notification");
1809 if (data) {
1810 reason_code = data->deauth_info.reason_code;
1811 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1812 data->deauth_info.reason_code);
1813 if (data->deauth_info.addr) {
1814 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
1815 MACSTR,
1816 MAC2STR(data->deauth_info.
1817 addr));
1818 }
1819 wpa_hexdump(MSG_DEBUG,
1820 "Deauthentication frame IE(s)",
1821 data->deauth_info.ie,
1822 data->deauth_info.ie_len);
1823#ifdef CONFIG_P2P
1824 wpas_p2p_deauth_notif(
1825 wpa_s, data->deauth_info.addr,
1826 reason_code,
1827 data->deauth_info.ie,
1828 data->deauth_info.ie_len);
1829#endif /* CONFIG_P2P */
1830 }
1831 }
1832#ifdef CONFIG_AP
1833 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1834 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1835 data->deauth_info.addr);
1836 break;
1837 }
1838#endif /* CONFIG_AP */
1839 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1840 break;
1841 case EVENT_MICHAEL_MIC_FAILURE:
1842 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1843 break;
1844#ifndef CONFIG_NO_SCAN_PROCESSING
1845 case EVENT_SCAN_RESULTS:
1846 wpa_supplicant_event_scan_results(wpa_s, data);
1847 break;
1848#endif /* CONFIG_NO_SCAN_PROCESSING */
1849 case EVENT_ASSOCINFO:
1850 wpa_supplicant_event_associnfo(wpa_s, data);
1851 break;
1852 case EVENT_INTERFACE_STATUS:
1853 wpa_supplicant_event_interface_status(wpa_s, data);
1854 break;
1855 case EVENT_PMKID_CANDIDATE:
1856 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1857 break;
1858#ifdef CONFIG_PEERKEY
1859 case EVENT_STKSTART:
1860 wpa_supplicant_event_stkstart(wpa_s, data);
1861 break;
1862#endif /* CONFIG_PEERKEY */
1863#ifdef CONFIG_TDLS
1864 case EVENT_TDLS:
1865 wpa_supplicant_event_tdls(wpa_s, data);
1866 break;
1867#endif /* CONFIG_TDLS */
1868#ifdef CONFIG_IEEE80211R
1869 case EVENT_FT_RESPONSE:
1870 wpa_supplicant_event_ft_response(wpa_s, data);
1871 break;
1872#endif /* CONFIG_IEEE80211R */
1873#ifdef CONFIG_IBSS_RSN
1874 case EVENT_IBSS_RSN_START:
1875 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1876 break;
1877#endif /* CONFIG_IBSS_RSN */
1878 case EVENT_ASSOC_REJECT:
1879 if (data->assoc_reject.bssid)
1880 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1881 "bssid=" MACSTR " status_code=%u",
1882 MAC2STR(data->assoc_reject.bssid),
1883 data->assoc_reject.status_code);
1884 else
1885 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1886 "status_code=%u",
1887 data->assoc_reject.status_code);
1888 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1889 sme_event_assoc_reject(wpa_s, data);
1890 break;
1891 case EVENT_AUTH_TIMED_OUT:
1892 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1893 sme_event_auth_timed_out(wpa_s, data);
1894 break;
1895 case EVENT_ASSOC_TIMED_OUT:
1896 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1897 sme_event_assoc_timed_out(wpa_s, data);
1898 break;
1899#ifdef CONFIG_AP
1900 case EVENT_TX_STATUS:
1901 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
1902 " type=%d stype=%d",
1903 MAC2STR(data->tx_status.dst),
1904 data->tx_status.type, data->tx_status.stype);
1905 if (wpa_s->ap_iface == NULL) {
1906#ifdef CONFIG_P2P
1907 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1908 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1909 wpas_send_action_tx_status(
1910 wpa_s, data->tx_status.dst,
1911 data->tx_status.data,
1912 data->tx_status.data_len,
1913 data->tx_status.ack ?
1914 P2P_SEND_ACTION_SUCCESS :
1915 P2P_SEND_ACTION_NO_ACK);
1916#endif /* CONFIG_P2P */
1917 break;
1918 }
1919#ifdef CONFIG_P2P
1920 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
1921 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
1922 /*
1923 * Catch TX status events for Action frames we sent via group
1924 * interface in GO mode.
1925 */
1926 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1927 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1928 os_memcmp(wpa_s->parent->pending_action_dst,
1929 data->tx_status.dst, ETH_ALEN) == 0) {
1930 wpas_send_action_tx_status(
1931 wpa_s->parent, data->tx_status.dst,
1932 data->tx_status.data,
1933 data->tx_status.data_len,
1934 data->tx_status.ack ?
1935 P2P_SEND_ACTION_SUCCESS :
1936 P2P_SEND_ACTION_NO_ACK);
1937 break;
1938 }
1939#endif /* CONFIG_P2P */
1940 switch (data->tx_status.type) {
1941 case WLAN_FC_TYPE_MGMT:
1942 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1943 data->tx_status.data_len,
1944 data->tx_status.stype,
1945 data->tx_status.ack);
1946 break;
1947 case WLAN_FC_TYPE_DATA:
1948 ap_tx_status(wpa_s, data->tx_status.dst,
1949 data->tx_status.data,
1950 data->tx_status.data_len,
1951 data->tx_status.ack);
1952 break;
1953 }
1954 break;
1955 case EVENT_RX_FROM_UNKNOWN:
1956 if (wpa_s->ap_iface == NULL)
1957 break;
1958 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1959 data->rx_from_unknown.len);
1960 break;
1961 case EVENT_RX_MGMT:
1962 if (wpa_s->ap_iface == NULL) {
1963#ifdef CONFIG_P2P
1964 u16 fc, stype;
1965 const struct ieee80211_mgmt *mgmt;
1966 mgmt = (const struct ieee80211_mgmt *)
1967 data->rx_mgmt.frame;
1968 fc = le_to_host16(mgmt->frame_control);
1969 stype = WLAN_FC_GET_STYPE(fc);
1970 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1971 data->rx_mgmt.frame_len > 24) {
1972 const u8 *src = mgmt->sa;
1973 const u8 *ie = mgmt->u.probe_req.variable;
1974 size_t ie_len = data->rx_mgmt.frame_len -
1975 (mgmt->u.probe_req.variable -
1976 data->rx_mgmt.frame);
1977 wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
1978 break;
1979 }
1980#endif /* CONFIG_P2P */
1981 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
1982 "management frame in non-AP mode");
1983 break;
1984 }
1985 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1986 break;
1987#endif /* CONFIG_AP */
1988 case EVENT_RX_ACTION:
1989 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
1990 " Category=%u DataLen=%d freq=%d MHz",
1991 MAC2STR(data->rx_action.sa),
1992 data->rx_action.category, (int) data->rx_action.len,
1993 data->rx_action.freq);
1994#ifdef CONFIG_IEEE80211R
1995 if (data->rx_action.category == WLAN_ACTION_FT) {
1996 ft_rx_action(wpa_s, data->rx_action.data,
1997 data->rx_action.len);
1998 break;
1999 }
2000#endif /* CONFIG_IEEE80211R */
2001#ifdef CONFIG_IEEE80211W
2002#ifdef CONFIG_SME
2003 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2004 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2005 data->rx_action.data,
2006 data->rx_action.len);
2007 break;
2008 }
2009#endif /* CONFIG_SME */
2010#endif /* CONFIG_IEEE80211W */
2011#ifdef CONFIG_P2P
2012 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2013 data->rx_action.sa,
2014 data->rx_action.bssid,
2015 data->rx_action.category,
2016 data->rx_action.data,
2017 data->rx_action.len, data->rx_action.freq);
2018#endif /* CONFIG_P2P */
2019 break;
2020 case EVENT_RX_PROBE_REQ:
2021 if (data->rx_probe_req.sa == NULL ||
2022 data->rx_probe_req.ie == NULL)
2023 break;
2024#ifdef CONFIG_AP
2025 if (wpa_s->ap_iface) {
2026 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2027 data->rx_probe_req.sa,
2028 data->rx_probe_req.ie,
2029 data->rx_probe_req.ie_len);
2030 break;
2031 }
2032#endif /* CONFIG_AP */
2033#ifdef CONFIG_P2P
2034 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2035 data->rx_probe_req.ie,
2036 data->rx_probe_req.ie_len);
2037#endif /* CONFIG_P2P */
2038 break;
2039#ifdef CONFIG_P2P
2040 case EVENT_REMAIN_ON_CHANNEL:
2041 wpas_p2p_remain_on_channel_cb(
2042 wpa_s, data->remain_on_channel.freq,
2043 data->remain_on_channel.duration);
2044 break;
2045 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2046 wpas_p2p_cancel_remain_on_channel_cb(
2047 wpa_s, data->remain_on_channel.freq);
2048 break;
2049 case EVENT_P2P_DEV_FOUND: {
2050 struct p2p_peer_info peer_info;
2051
2052 os_memset(&peer_info, 0, sizeof(peer_info));
2053 if (data->p2p_dev_found.dev_addr)
2054 os_memcpy(peer_info.p2p_device_addr,
2055 data->p2p_dev_found.dev_addr, ETH_ALEN);
2056 if (data->p2p_dev_found.pri_dev_type)
2057 os_memcpy(peer_info.pri_dev_type,
2058 data->p2p_dev_found.pri_dev_type,
2059 sizeof(peer_info.pri_dev_type));
2060 if (data->p2p_dev_found.dev_name)
2061 os_strlcpy(peer_info.device_name,
2062 data->p2p_dev_found.dev_name,
2063 sizeof(peer_info.device_name));
2064 peer_info.config_methods = data->p2p_dev_found.config_methods;
2065 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2066 peer_info.group_capab = data->p2p_dev_found.group_capab;
2067
2068 /*
2069 * FIX: new_device=1 is not necessarily correct. We should
2070 * maintain a P2P peer database in wpa_supplicant and update
2071 * this information based on whether the peer is truly new.
2072 */
2073 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2074 break;
2075 }
2076 case EVENT_P2P_GO_NEG_REQ_RX:
2077 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2078 data->p2p_go_neg_req_rx.dev_passwd_id);
2079 break;
2080 case EVENT_P2P_GO_NEG_COMPLETED:
2081 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2082 break;
2083 case EVENT_P2P_PROV_DISC_REQUEST:
2084 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2085 data->p2p_prov_disc_req.config_methods,
2086 data->p2p_prov_disc_req.dev_addr,
2087 data->p2p_prov_disc_req.pri_dev_type,
2088 data->p2p_prov_disc_req.dev_name,
2089 data->p2p_prov_disc_req.supp_config_methods,
2090 data->p2p_prov_disc_req.dev_capab,
2091 data->p2p_prov_disc_req.group_capab);
2092 break;
2093 case EVENT_P2P_PROV_DISC_RESPONSE:
2094 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2095 data->p2p_prov_disc_resp.config_methods);
2096 break;
2097 case EVENT_P2P_SD_REQUEST:
2098 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2099 data->p2p_sd_req.sa,
2100 data->p2p_sd_req.dialog_token,
2101 data->p2p_sd_req.update_indic,
2102 data->p2p_sd_req.tlvs,
2103 data->p2p_sd_req.tlvs_len);
2104 break;
2105 case EVENT_P2P_SD_RESPONSE:
2106 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2107 data->p2p_sd_resp.update_indic,
2108 data->p2p_sd_resp.tlvs,
2109 data->p2p_sd_resp.tlvs_len);
2110 break;
2111#endif /* CONFIG_P2P */
2112#ifdef CONFIG_CLIENT_MLME
2113 case EVENT_MLME_RX: {
2114 struct ieee80211_rx_status rx_status;
2115 os_memset(&rx_status, 0, sizeof(rx_status));
2116 rx_status.freq = data->mlme_rx.freq;
2117 rx_status.channel = data->mlme_rx.channel;
2118 rx_status.ssi = data->mlme_rx.ssi;
2119 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
2120 &rx_status);
2121 break;
2122 }
2123#endif /* CONFIG_CLIENT_MLME */
2124 case EVENT_EAPOL_RX:
2125 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2126 data->eapol_rx.data,
2127 data->eapol_rx.data_len);
2128 break;
2129 case EVENT_SIGNAL_CHANGE:
2130 bgscan_notify_signal_change(
2131 wpa_s, data->signal_change.above_threshold,
2132 data->signal_change.current_signal,
2133 data->signal_change.current_noise,
2134 data->signal_change.current_txrate);
2135 break;
2136 case EVENT_INTERFACE_ENABLED:
2137 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2138 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2139#ifdef CONFIG_AP
2140 if (!wpa_s->ap_iface) {
2141 wpa_supplicant_set_state(wpa_s,
2142 WPA_DISCONNECTED);
2143 wpa_supplicant_req_scan(wpa_s, 0, 0);
2144 } else
2145 wpa_supplicant_set_state(wpa_s,
2146 WPA_COMPLETED);
2147#else /* CONFIG_AP */
2148 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2149 wpa_supplicant_req_scan(wpa_s, 0, 0);
2150#endif /* CONFIG_AP */
2151 }
2152 break;
2153 case EVENT_INTERFACE_DISABLED:
2154 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2155 wpa_supplicant_mark_disassoc(wpa_s);
2156 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2157 break;
2158 case EVENT_CHANNEL_LIST_CHANGED:
2159 if (wpa_s->drv_priv == NULL)
2160 break; /* Ignore event during drv initialization */
2161#ifdef CONFIG_P2P
2162 wpas_p2p_update_channel_list(wpa_s);
2163#endif /* CONFIG_P2P */
2164 break;
2165 case EVENT_INTERFACE_UNAVAILABLE:
2166#ifdef CONFIG_P2P
2167 wpas_p2p_interface_unavailable(wpa_s);
2168#endif /* CONFIG_P2P */
2169 break;
2170 case EVENT_BEST_CHANNEL:
2171 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2172 "(%d %d %d)",
2173 data->best_chan.freq_24, data->best_chan.freq_5,
2174 data->best_chan.freq_overall);
2175 wpa_s->best_24_freq = data->best_chan.freq_24;
2176 wpa_s->best_5_freq = data->best_chan.freq_5;
2177 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2178#ifdef CONFIG_P2P
2179 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2180 data->best_chan.freq_5,
2181 data->best_chan.freq_overall);
2182#endif /* CONFIG_P2P */
2183 break;
2184 case EVENT_UNPROT_DEAUTH:
2185 wpa_supplicant_event_unprot_deauth(wpa_s,
2186 &data->unprot_deauth);
2187 break;
2188 case EVENT_UNPROT_DISASSOC:
2189 wpa_supplicant_event_unprot_disassoc(wpa_s,
2190 &data->unprot_disassoc);
2191 break;
2192 case EVENT_STATION_LOW_ACK:
2193#ifdef CONFIG_AP
2194 if (wpa_s->ap_iface && data)
2195 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2196 data->low_ack.addr);
2197#endif /* CONFIG_AP */
2198 break;
2199 case EVENT_IBSS_PEER_LOST:
2200#ifdef CONFIG_IBSS_RSN
2201 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2202#endif /* CONFIG_IBSS_RSN */
2203 break;
2204 default:
2205 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2206 break;
2207 }
2208}