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