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